Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WindowFrame.cs « Xwt « Xwt - github.com/mono/xwt.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 890b9dc422479ce1ee3531a82e9422f8e31c281e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// 
// WindowFrame.cs
//  
// Author:
//       Lluis Sanchez <lluis@xamarin.com>
//       Konrad M. Kruczynski <kkruczynski@antmicro.com>
// 
// Copyright (c) 2011 Xamarin Inc
// Copyright (c) 2016 Antmicro Ltd
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// 
// WindowFrame.cs
//  
// Author:
//       Lluis Sanchez <lluis@xamarin.com>
// 
// Copyright (c) 2011 Xamarin Inc
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Xwt.Backends;

using System.ComponentModel;
using Xwt.Drawing;
using Xwt.Motion;

namespace Xwt
{
	[BackendType (typeof(IWindowFrameBackend))]
	public class WindowFrame: XwtComponent, IAnimatable
	{
		EventHandler boundsChanged;
		EventHandler shown;
		EventHandler hidden;
		CloseRequestedHandler closeRequested;
		EventHandler closed;

		Point location;
		Size size;
		bool pendingReallocation;
		Image icon;
		WindowFrame transientFor;
		
		protected class WindowBackendHost: BackendHost<WindowFrame,IWindowFrameBackend>, IWindowFrameEventSink
		{
			protected override void OnBackendCreated ()
			{
				Backend.Initialize (this);
				base.OnBackendCreated ();
				Parent.location = Backend.Bounds.Location;
				Parent.size = Backend.Bounds.Size;
				Backend.EnableEvent (WindowFrameEvent.BoundsChanged);
			}
			
			public void OnBoundsChanged (Rectangle bounds)
			{
				Parent.OnBoundsChanged (new BoundsChangedEventArgs () { Bounds = bounds });
			}

			public virtual void OnShown ()
			{
				Parent.OnShown ();
			}

			public virtual void OnHidden ()
			{
				Parent.OnHidden ();
			}

			public virtual bool OnCloseRequested ()
			{
				return Parent.OnCloseRequested ();
			}

			public virtual void OnClosed ()
			{
				Parent.OnClosed ();
			}
		}

		static WindowFrame ()
		{
			MapEvent (WindowFrameEvent.Shown, typeof(WindowFrame), "OnShown");
			MapEvent (WindowFrameEvent.Hidden, typeof(WindowFrame), "OnHidden");
			MapEvent (WindowFrameEvent.CloseRequested, typeof(WindowFrame), "OnCloseRequested");
			MapEvent (WindowFrameEvent.Closed, typeof(WindowFrame), "OnClosed");
		}

		public WindowFrame ()
		{
			if (!(base.BackendHost is WindowBackendHost))
				throw new InvalidOperationException ("CreateBackendHost for WindowFrame did not return a WindowBackendHost instance");
		}
		
		public WindowFrame (string title): this ()
		{
			Backend.Title = title;
		}
		
		protected override void Dispose (bool disposing)
		{
			base.Dispose (disposing);
			
			// Don't dispose the backend if this object is being finalized
			// The backend has to handle the finalizing on its own
			if (disposing && BackendHost.BackendCreated)
				Backend.Dispose ();
		}
		
		IWindowFrameBackend Backend {
			get { return (IWindowFrameBackend) BackendHost.Backend; } 
		}
		
		protected override BackendHost CreateBackendHost ()
		{
			return new WindowBackendHost ();
		}
		
		protected new WindowBackendHost BackendHost {
			get { return (WindowBackendHost) base.BackendHost; }
		}

		/// <summary>
		/// Gets or sets the name (not title) of this window.
		/// </summary>
		/// <value>The window name.</value>
		/// <remarks>The name can be used to identify this window by e.g. designers.
		/// The name of the window is not visible to the user. Use the Title property
		/// to modify the visible window title.</remarks>
		[DefaultValue (null)]
		public override string Name {
			get { return Backend.Name; }
			set { Backend.Name = value; }
		}
		
		public Rectangle ScreenBounds {
			get {
				return BackendBounds;
			}
			set {
				if (value.Width < 0)
					value.Width = 0;
				if (value.Height < 0)
					value.Height = 0;
				BackendBounds = value;
				if (Visible)
					AdjustSize ();
			}
		}

		public double X {
			get { return BackendBounds.X; }
			set { SetBackendLocation (value, Y); }
		}
		
		public double Y {
			get { return BackendBounds.Y; }
			set { SetBackendLocation (X, value); }
		}
		
		public double Width {
			get { return BackendBounds.Width; }
			set {
				if (value < 0)
					value = 0;
				SetBackendSize (value, -1);
				if (Visible)
					AdjustSize ();
			}
		}
		
		public double Height {
			get { return BackendBounds.Height; }
			set {
				if (value < 0)
					value = 0;
				SetBackendSize (-1, value);
				if (Visible)
					AdjustSize ();
			}
		}

		/// <summary>
		/// Size of the window, not including the decorations
		/// </summary>
		/// <value>The size.</value>
		public Size Size {
			get { return BackendBounds.Size; }
			set {
				if (value.Width < 0)
					value.Width = 0;
				if (value.Height < 0)
					value.Height = 0;
				SetBackendSize (value.Width, value.Height);
				if (Visible)
					AdjustSize ();
			}
		}
		
		public Point Location {
			get { return BackendBounds.Location; }
			set { SetBackendLocation (value.X, value.Y); }
		}
		
		public string Title {
			get { return Backend.Title; }
			set { Backend.Title = value; }
		}

		public Image Icon {
			get { return icon; }
			set { icon = value; Backend.SetIcon (icon != null ? icon.GetImageDescription (BackendHost.ToolkitEngine) : ImageDescription.Null); }
		}
		
		public bool Decorated {
			get { return Backend.Decorated; }
			set { Backend.Decorated = value; }
		}
		
		public bool ShowInTaskbar {
			get { return Backend.ShowInTaskbar; }
			set { Backend.ShowInTaskbar = value; }
		}

		public WindowFrame TransientFor {
			get { return transientFor; }
			set {
				transientFor = value;
				Backend.SetTransientFor ((IWindowFrameBackend)(value as IFrontend).Backend);
			}
		}

		public bool Resizable {
			get { return Backend.Resizable; }
			set { Backend.Resizable = value; }
		}
		
		public bool Visible {
			get { return Backend.Visible; }
			set { Backend.Visible = value; }
		}

		[DefaultValue (true)]
		public bool Sensitive {
			get { return Backend.Sensitive; }
			set { Backend.Sensitive = value; }
		}

		public double Opacity {
			get { return Backend.Opacity; }
			set { Backend.Opacity = value; }
		}

		public bool HasFocus {
			get { return Backend.HasFocus; }
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether this window is in full screen mode
		/// </summary>
		/// <value><c>true</c> if the window is in full screen mode; otherwise, <c>false</c>.</value>
		public bool FullScreen {
			get { return Backend.FullScreen; }
			set { Backend.FullScreen = value; }
		}

		/// <summary>
		/// Gets the screen on which most of the area of this window is placed
		/// </summary>
		/// <value>The screen.</value>
		public Screen Screen {
			get {
				if (!Visible)
					throw new InvalidOperationException ("The window is not visible");
				return Desktop.GetScreen (Backend.Screen);
			}
		}

		public void Show ()
		{
			if (!Visible) {
				AdjustSize ();
				Visible = true;
			}
		}
		
		internal virtual void AdjustSize ()
		{
		}

		/// <summary>
		/// Presents a window to the user. This may mean raising the window in the stacking order,
		/// deiconifying it, moving it to the current desktop, and/or giving it the keyboard focus
		/// </summary>
		public void Present ()
		{
			if (!Visible)
				AdjustSize ();
			Backend.Present ();
		}

		protected virtual void OnShown ()
		{
			if(shown != null)
				shown (this, EventArgs.Empty);
		}
		
		public void Hide ()
		{
			Visible = false;
		}

		protected virtual void OnHidden ()
		{
			if (hidden != null)
				hidden (this, EventArgs.Empty);
		}

		/// <summary>
		/// Closes the window
		/// </summary>
		/// <remarks>>
		/// Closes the window like if the user clicked on the close window button.
		/// The CloseRequested event is fired and subscribers can cancel the closing,
		/// so there is no guarantee that the window will actually close.
		/// This method doesn't dispose the window. The Dispose method has to be called.
		/// </remarks>
		public bool Close ()
		{
			return Backend.Close ();
		}

		/// <summary>
		/// Called to check if the window can be closed
		/// </summary>
		/// <returns><c>true</c> if the window can be closed, <c>false</c> otherwise</returns>
		protected virtual bool OnCloseRequested ()
		{
			if (closeRequested == null)
				return true;
			var eventArgs = new CloseRequestedEventArgs();
			closeRequested (this, eventArgs);
			return eventArgs.AllowClose;
		}

		/// <summary>
		/// Called when the window has been closed by the user, or by a call to Close
		/// </summary>
		/// <remarks>
		/// This method is not called when the window is disposed, only when explicitly closed (either by code or by the user)
		/// </remarks>
		protected virtual void OnClosed ()
		{
			if (closed != null)
				closed (this, EventArgs.Empty);
		}

		internal virtual void SetBackendSize (double width, double height)
		{
			Backend.SetSize (width, height);
		}

		internal virtual void SetBackendLocation (double x, double y)
		{
			location = new Point (x, y);
			Backend.Move (x, y);
		}

		internal virtual Rectangle BackendBounds {
			get {
				BackendHost.EnsureBackendLoaded ();
				return new Rectangle (location, size);
			}
			set {
				size = value.Size;
				location = value.Location;
				Backend.Bounds = value;
			}
		}
		
		protected virtual void OnBoundsChanged (BoundsChangedEventArgs a)
		{
			var bounds = new Rectangle (location, size);
			if (bounds != a.Bounds) {
				size = a.Bounds.Size;
				location = a.Bounds.Location;
				Reallocate ();
				if (boundsChanged != null)
					boundsChanged (this, a);
			}
		}
		
		internal void Reallocate ()
		{
			if (!pendingReallocation) {
				pendingReallocation = true;
				BackendHost.ToolkitEngine.QueueExitAction (delegate {
					pendingReallocation = false;
					OnReallocate ();
				});
			}
		}
		
		protected virtual void OnReallocate ()
		{
		}
		
		void IAnimatable.BatchBegin ()
		{
		}

		void IAnimatable.BatchCommit ()
		{
		}

		public event EventHandler BoundsChanged {
			add {
				boundsChanged += value;
			}
			remove {
				boundsChanged -= value;
			}
		}

		public event EventHandler Shown {
			add {
				BackendHost.OnBeforeEventAdd (WindowFrameEvent.Shown, shown);
				shown += value;
			}
			remove {
				shown -= value;
				BackendHost.OnAfterEventRemove (WindowFrameEvent.Shown, shown);
			}
		}

		public event EventHandler Hidden {
			add {
				BackendHost.OnBeforeEventAdd (WindowFrameEvent.Hidden, hidden);
				hidden += value;
			}
			remove {
				hidden -= value;
				BackendHost.OnAfterEventRemove (WindowFrameEvent.Hidden, hidden);
			}
		}

		public event CloseRequestedHandler CloseRequested {
			add {
				BackendHost.OnBeforeEventAdd (WindowFrameEvent.CloseRequested, closeRequested);
				closeRequested += value;
			}
			remove {
				closeRequested -= value;
				BackendHost.OnAfterEventRemove (WindowFrameEvent.CloseRequested, closeRequested);
			}
		}

		/// <summary>
		/// Raised when the window has been closed by the user, or by a call to Close
		/// </summary>
		/// <remarks>
		/// This event is not raised when the window is disposed, only when explicitly closed (either by code or by the user)
		/// </remarks>
		public event EventHandler Closed {
			add {
				BackendHost.OnBeforeEventAdd (WindowFrameEvent.Closed, closed);
				closed += value;
			}
			remove {
				closed -= value;
				BackendHost.OnAfterEventRemove (WindowFrameEvent.Closed, closed);
			}
		}
	}
	
	public class BoundsChangedEventArgs: EventArgs
	{
		public Rectangle Bounds { get; set; }
	}
}