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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/WindowFrameBackend.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
// WindowFrameBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Carlos Alberto Cortez <calberto.cortez@gmail.com>
 
6
// 
 
7
// Copyright (c) 2011 Carlos Alberto Cortez
 
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 System.Collections.Generic;
 
29
using System.Linq;
 
30
using System.Text;
 
31
using System.Windows;
 
32
 
 
33
using Xwt.Backends;
 
34
using Xwt.Engine;
 
35
 
 
36
namespace Xwt.WPFBackend
 
37
{
 
38
        public class WindowFrameBackend : IWindowFrameBackend
 
39
        {
 
40
                System.Windows.Window window;
 
41
                IWindowFrameEventSink eventSink;
 
42
                WindowFrame frontend;
 
43
 
 
44
                public WindowFrameBackend ()
 
45
                {
 
46
                }
 
47
 
 
48
                void IBackend.InitializeBackend (object frontend)
 
49
                {
 
50
                        this.frontend = (WindowFrame) frontend;
 
51
                }
 
52
 
 
53
                void IWindowFrameBackend.Initialize (IWindowFrameEventSink eventSink)
 
54
                {
 
55
                        this.eventSink = eventSink;
 
56
                        Initialize ();
 
57
                }
 
58
 
 
59
                public virtual void Initialize ()
 
60
                {
 
61
                }
 
62
 
 
63
                public virtual void Dispose ()
 
64
                {       
 
65
                        Window.Close ();
 
66
                }
 
67
 
 
68
                public System.Windows.Window Window {
 
69
                        get { return window; }
 
70
                        set { window = value; }
 
71
                }
 
72
 
 
73
                public virtual bool HasMenu {
 
74
                        get { return false;  }
 
75
                }
 
76
 
 
77
                protected WindowFrame Frontend {
 
78
                        get { return frontend; }
 
79
                }
 
80
 
 
81
                public IWindowFrameEventSink EventSink
 
82
                {
 
83
                        get { return eventSink; }
 
84
                }
 
85
 
 
86
                bool IWindowFrameBackend.Decorated {
 
87
                        get { return window.WindowStyle != WindowStyle.None; }
 
88
                        set { window.WindowStyle = value ? WindowStyle.SingleBorderWindow : WindowStyle.None; }
 
89
                }
 
90
 
 
91
                bool IWindowFrameBackend.ShowInTaskbar {
 
92
                        get { return window.ShowInTaskbar; }
 
93
                        set { window.ShowInTaskbar = value; }
 
94
                }
 
95
 
 
96
                void IWindowFrameBackend.SetTransientFor (IWindowFrameBackend window)
 
97
                {
 
98
                        this.Window.Owner = ((WindowFrameBackend) window).Window;
 
99
                }
 
100
 
 
101
                bool IWindowFrameBackend.Resizable {
 
102
                        get {
 
103
                                return window.ResizeMode == ResizeMode.CanResize;
 
104
                        }
 
105
                        set {
 
106
                                if (value != ((IWindowFrameBackend)this).Resizable) {
 
107
                                        var bounds = Bounds;
 
108
                                        window.ResizeMode = value ? ResizeMode.CanResize : ResizeMode.NoResize;
 
109
                                        if (window.IsLoaded && bounds != Bounds) {
 
110
                                                // The size of the border of resizable windows is different from fixed windows.
 
111
                                                // If we change the resize mode, the border size will change, and the client
 
112
                                                // area will then change. Here, we restore the client area to the size it
 
113
                                                // had before the mode change.
 
114
                                                Bounds = bounds;
 
115
                                        }
 
116
                                }
 
117
                        }
 
118
                }
 
119
 
 
120
                public void SetIcon (object imageBackend)
 
121
                {
 
122
                        window.Icon = DataConverter.AsImageSource (imageBackend);
 
123
                }
 
124
 
 
125
                string IWindowFrameBackend.Title {
 
126
                        get { return window.Title; }
 
127
                        set { window.Title = value; }
 
128
                }
 
129
 
 
130
                bool IWindowFrameBackend.Visible
 
131
                {
 
132
                        get { return window.Visibility == Visibility.Visible; }
 
133
                        set { window.Visibility = value ? Visibility.Visible : Visibility.Hidden; }
 
134
                }
 
135
 
 
136
                void IWindowFrameBackend.Present ()
 
137
                {
 
138
                        window.Activate ();
 
139
                }
 
140
 
 
141
                public void Move (double x, double y)
 
142
                {
 
143
                        var value = ToNonClientRect (new Rectangle (x, y, 1, 1));
 
144
                        window.Top = value.Top;
 
145
                        window.Left = value.Left;
 
146
                        Toolkit.Invoke (delegate
 
147
                        {
 
148
                                eventSink.OnBoundsChanged (Bounds);
 
149
                        });
 
150
                }
 
151
 
 
152
                public void Resize (double width, double height)
 
153
                {
 
154
                        var value = ToNonClientRect (new Rectangle (0, 0, width, height));
 
155
                        window.Width = value.Width;
 
156
                        window.Height = value.Height;
 
157
                        Toolkit.Invoke (delegate
 
158
                        {
 
159
                                eventSink.OnBoundsChanged (Bounds);
 
160
                        });
 
161
                }
 
162
 
 
163
                public Rectangle Bounds {
 
164
                        get {
 
165
                                double width = Double.IsNaN (window.Width) ? window.ActualWidth : window.Width;
 
166
                                double height = Double.IsNaN (window.Height) ? window.ActualHeight : window.Height;
 
167
                                return ToClientRect (new Rectangle (window.Left, window.Top, width, height));
 
168
                        }
 
169
                        set {
 
170
                                value = ToNonClientRect (value);
 
171
                                window.Top = value.Top;
 
172
                                window.Left = value.Left;
 
173
                                window.Width = value.Width;
 
174
                                window.Height = value.Height;
 
175
                                Toolkit.Invoke (delegate {
 
176
                                        eventSink.OnBoundsChanged (Bounds);
 
177
                                });
 
178
                        }
 
179
                }
 
180
 
 
181
                public virtual void EnableEvent (object eventId)
 
182
                {
 
183
                        if (eventId is WindowFrameEvent) {
 
184
                                switch ((WindowFrameEvent)eventId) {
 
185
                                        case WindowFrameEvent.BoundsChanged:
 
186
                                                window.LocationChanged += BoundsChangedHandler;
 
187
                                                window.SizeChanged += BoundsChangedHandler;
 
188
                                                break;
 
189
                                        case WindowFrameEvent.Shown:
 
190
                                                window.IsVisibleChanged += ShownHandler;
 
191
                                                break;
 
192
                                        case WindowFrameEvent.Hidden:
 
193
                                                window.IsVisibleChanged += HiddenHandler;
 
194
                                                break;
 
195
                                        case WindowFrameEvent.CloseRequested:
 
196
                                                window.Closing += ClosingHandler;
 
197
                                                break;
 
198
                                }
 
199
                        }
 
200
                }
 
201
 
 
202
        
 
203
 
 
204
                public virtual void DisableEvent (object eventId)
 
205
                {
 
206
                        if (eventId is WindowFrameEvent) {
 
207
                                switch ((WindowFrameEvent)eventId) {
 
208
                                        case WindowFrameEvent.BoundsChanged:
 
209
                                                window.LocationChanged -= BoundsChangedHandler;
 
210
                                                window.SizeChanged -= BoundsChangedHandler;
 
211
                                                break;
 
212
                                        case WindowFrameEvent.Shown:
 
213
                                                window.IsVisibleChanged -= ShownHandler;
 
214
                                                break;
 
215
                                        case WindowFrameEvent.Hidden:
 
216
                                                window.IsVisibleChanged -= HiddenHandler;
 
217
                                                break;
 
218
                                        case WindowFrameEvent.CloseRequested:
 
219
                                                window.Closing -= ClosingHandler;
 
220
                                                break;
 
221
                                }
 
222
                        }
 
223
                }
 
224
 
 
225
                void BoundsChangedHandler (object o, EventArgs args)
 
226
                {
 
227
                        Toolkit.Invoke (delegate () {
 
228
                                eventSink.OnBoundsChanged (Bounds);
 
229
                        });
 
230
                }
 
231
 
 
232
                private void ShownHandler (object sender, DependencyPropertyChangedEventArgs e)
 
233
                {
 
234
                        if((bool)e.NewValue)
 
235
                        {
 
236
                                Toolkit.Invoke (delegate ()
 
237
                                {
 
238
                                        eventSink.OnShown ();
 
239
                                });
 
240
                        }
 
241
                }
 
242
 
 
243
                private void HiddenHandler (object sender, DependencyPropertyChangedEventArgs e)
 
244
                {
 
245
                        if((bool)e.NewValue == false)
 
246
                        {
 
247
                                Toolkit.Invoke (delegate ()
 
248
                                {
 
249
                                        eventSink.OnHidden ();
 
250
                                });
 
251
                        }
 
252
                }
 
253
 
 
254
                private void ClosingHandler (object sender, System.ComponentModel.CancelEventArgs e)
 
255
                {
 
256
                        Toolkit.Invoke (delegate ()
 
257
                        {
 
258
                                e.Cancel = eventSink.OnCloseRequested ();
 
259
                        });
 
260
                }
 
261
 
 
262
                Size GetBorderSize ()
 
263
                {
 
264
                        if (window.ResizeMode == ResizeMode.CanResize)
 
265
                                return new Size (SystemParameters.ResizeFrameVerticalBorderWidth, SystemParameters.ResizeFrameHorizontalBorderHeight);
 
266
                        else
 
267
                                return new Size (SystemParameters.FixedFrameVerticalBorderWidth, SystemParameters.FixedFrameHorizontalBorderHeight);
 
268
                }
 
269
 
 
270
                protected Rectangle ToNonClientRect (Rectangle rect)
 
271
                {
 
272
                        var size = rect.Size;
 
273
                        var loc = rect.Location;
 
274
 
 
275
                        var border = GetBorderSize ();
 
276
                        size.Height += border.Height * 2;
 
277
                        size.Width += border.Width * 2;
 
278
                        loc.X -= border.Width;
 
279
                        loc.Y -= border.Height;
 
280
 
 
281
                        if (((IWindowFrameBackend)this).Decorated) {
 
282
                                size.Height += SystemParameters.WindowCaptionHeight;
 
283
                                loc.Y -= SystemParameters.CaptionHeight;
 
284
                        }
 
285
                        if (HasMenu) {
 
286
                                size.Height += SystemParameters.MenuBarHeight;
 
287
                                loc.Y -= SystemParameters.MenuBarHeight;
 
288
                        }
 
289
 
 
290
                        return new Rectangle (loc, size);
 
291
                }
 
292
 
 
293
                protected Rectangle ToClientRect (Rectangle rect)
 
294
                {
 
295
                        var size = rect.Size;
 
296
                        var loc = rect.Location;
 
297
 
 
298
                        var border = GetBorderSize ();
 
299
                        size.Height -= border.Height * 2;
 
300
                        size.Width -= border.Width * 2;
 
301
                        loc.X += border.Width;
 
302
                        loc.Y += border.Height;
 
303
 
 
304
                        if (((IWindowFrameBackend)this).Decorated) {
 
305
                                size.Height -= SystemParameters.CaptionHeight;
 
306
                                loc.Y += SystemParameters.CaptionHeight;
 
307
                        }
 
308
                        if (HasMenu) {
 
309
                                size.Height -= SystemParameters.MenuBarHeight;
 
310
                                loc.Y += SystemParameters.MenuBarHeight;
 
311
                        }
 
312
 
 
313
                        size.Width = Math.Max (0, size.Width);
 
314
                        size.Height = Math.Max (0, size.Height);
 
315
 
 
316
                        return new Rectangle (loc, size);
 
317
                }
 
318
        }
 
319
}