~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/PopoverBackend.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// PopoverBackend.cs
 
3
//
 
4
// Author:
 
5
//       Alan McGovern <alan@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 Xamarin, Inc.
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using Xwt.Backends;
 
29
using System.Windows.Media;
 
30
 
 
31
namespace Xwt.WPFBackend
 
32
{
 
33
        public class PopoverBackend : IPopoverBackend
 
34
        {
 
35
                public Xwt.Popover.Position ActualPosition {
 
36
                        get; set;
 
37
                }
 
38
 
 
39
                System.Windows.Controls.Border Border {
 
40
                        get; set;
 
41
                }
 
42
 
 
43
                IPopoverEventSink EventSink {
 
44
                        get; set;
 
45
                }
 
46
 
 
47
                Popover Frontend {
 
48
                        get; set;
 
49
                }
 
50
 
 
51
                System.Windows.Controls.Primitives.Popup NativeWidget {
 
52
                        get; set;
 
53
                }
 
54
 
 
55
                public PopoverBackend ()
 
56
                {
 
57
                        Border = new System.Windows.Controls.Border {
 
58
                                BorderBrush = Brushes.Black,
 
59
                                CornerRadius = new System.Windows.CornerRadius (15),
 
60
                                Padding = new System.Windows.Thickness (15),
 
61
                                BorderThickness = new System.Windows.Thickness (1)
 
62
                        };
 
63
 
 
64
                        NativeWidget = new System.Windows.Controls.Primitives.Popup {
 
65
                                AllowsTransparency = true,
 
66
                                Child = Border,
 
67
                                Placement = System.Windows.Controls.Primitives.PlacementMode.Custom,
 
68
                                StaysOpen = false,
 
69
                                Margin = new System.Windows.Thickness (10),
 
70
                        };
 
71
 
 
72
                        NativeWidget.CustomPopupPlacementCallback = (popupSize, targetSize, offset) => {
 
73
                                var location = new System.Windows.Point (targetSize.Width / 2 - popupSize.Width / 2, 0);
 
74
                                if (ActualPosition == Popover.Position.Top)
 
75
                                        location.Y = targetSize.Height;
 
76
                                else
 
77
                                        location.Y = -popupSize.Height;
 
78
 
 
79
                                return new[] {
 
80
                                        new System.Windows.Controls.Primitives.CustomPopupPlacement (location, System.Windows.Controls.Primitives.PopupPrimaryAxis.Horizontal)
 
81
                                };
 
82
                        };
 
83
                }
 
84
 
 
85
                public void Initialize (IPopoverEventSink sink)
 
86
                {
 
87
                        EventSink = sink;
 
88
                }
 
89
 
 
90
                public void InitializeBackend (object frontend)
 
91
                {
 
92
                        Frontend = (Popover) frontend;
 
93
                }
 
94
 
 
95
                public void EnableEvent (object eventId)
 
96
                {
 
97
                        if (eventId is PopoverEvent)
 
98
                                if ((PopoverEvent)eventId == PopoverEvent.Closed)
 
99
                                        NativeWidget.Closed +=new EventHandler(NativeWidget_Closed);
 
100
                }
 
101
 
 
102
                public void DisableEvent (object eventId)
 
103
                {
 
104
                        if (eventId is PopoverEvent)
 
105
                                if ((PopoverEvent) eventId == PopoverEvent.Closed)
 
106
                                        NativeWidget.Closed += new EventHandler (NativeWidget_Closed);
 
107
                }
 
108
 
 
109
                public void Show (Xwt.Popover.Position orientation, Xwt.Widget reference, Xwt.Rectangle positionRect, Widget child)
 
110
                {
 
111
                        ActualPosition = orientation;
 
112
                        Border.Child = (System.Windows.FrameworkElement) Xwt.Engine.WidgetRegistry.GetNativeWidget (child);
 
113
                        NativeWidget.PlacementTarget =(System.Windows.FrameworkElement) Xwt.Engine.WidgetRegistry.GetNativeWidget (reference);
 
114
                        NativeWidget.IsOpen = true;
 
115
                }
 
116
 
 
117
                void NativeWidget_Closed (object sender, EventArgs e)
 
118
                {
 
119
                        EventSink.OnClosed ();
 
120
                }
 
121
 
 
122
                public void Hide ()
 
123
                {
 
124
                        NativeWidget.IsOpen = false;
 
125
                }
 
126
 
 
127
                public void Dispose ()
 
128
                {
 
129
                        if (NativeWidget != null)
 
130
                                NativeWidget.Closed -= NativeWidget_Closed;
 
131
                }
 
132
        }
 
133
}
 
 
b'\\ No newline at end of file'