/
Window.cs
736 lines (663 loc) · 21.4 KB
/
Window.cs
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
using System;
using System.ComponentModel;
using Eto.Drawing;
namespace Eto.Forms
{
/// <summary>
/// State of a <see cref="Window"/>
/// </summary>
public enum WindowState
{
/// <summary>
/// Normal, windowed state
/// </summary>
Normal,
/// <summary>
/// Window is maximized, taking the entire screen space
/// </summary>
Maximized,
/// <summary>
/// Window is minimized to the dock/taskbar/etc.
/// </summary>
Minimized
}
/// <summary>
/// Style of a <see cref="Window"/>
/// </summary>
public enum WindowStyle
{
/// <summary>
/// Default, bordered style
/// </summary>
Default,
/// <summary>
/// Window with no border
/// </summary>
None
}
/// <summary>
/// Base window
/// </summary>
public abstract class Window : Panel
{
new IHandler Handler { get { return (IHandler)base.Handler; } }
#region Events
/// <summary>
/// Identifier for handlers when attaching the <see cref="Closed"/> event.
/// </summary>
public const string ClosedEvent = "Window.Closed";
/// <summary>
/// Occurs when the window is closed.
/// </summary>
public event EventHandler<EventArgs> Closed
{
add { Properties.AddHandlerEvent(ClosedEvent, value); }
remove { Properties.RemoveEvent(ClosedEvent, value); }
}
/// <summary>
/// Raises the <see cref="Closed"/> event.
/// </summary>
/// <param name="e">Event arguments</param>
protected virtual void OnClosed(EventArgs e)
{
OnUnLoad(EventArgs.Empty);
Properties.TriggerEvent(ClosedEvent, this, e);
}
/// <summary>
/// Identifier for handlers when attaching the <see cref="Closing"/> event.
/// </summary>
public const string ClosingEvent = "Window.Closing";
/// <summary>
/// Occurs before the window is closed, giving an opportunity to cancel the close operation.
/// </summary>
public event EventHandler<CancelEventArgs> Closing
{
add { Properties.AddHandlerEvent(ClosingEvent, value); }
remove { Properties.RemoveEvent(ClosingEvent, value); }
}
/// <summary>
/// Raises the <see cref="Closing"/> event.
/// </summary>
/// <param name="e">Event arguments</param>
protected virtual void OnClosing(CancelEventArgs e)
{
Properties.TriggerEvent(ClosingEvent, this, e);
}
/// <summary>
/// Identifier for handlers when attaching the <see cref="LocationChanged"/> event.
/// </summary>
public const string LocationChangedEvent = "Window.LocationChanged";
/// <summary>
/// Occurs when the <see cref="Location"/> of the window is changed.
/// </summary>
public event EventHandler<EventArgs> LocationChanged
{
add { Properties.AddHandlerEvent(LocationChangedEvent, value); }
remove { Properties.RemoveEvent(LocationChangedEvent, value); }
}
/// <summary>
/// Raises the <see cref="LocationChanged"/> event.
/// </summary>
/// <param name="e">Event arguments</param>
protected virtual void OnLocationChanged(EventArgs e)
{
Properties.TriggerEvent(LocationChangedEvent, this, e);
}
/// <summary>
/// Identifier for handlers when attaching the <see cref="OwnerChanged"/> event.
/// </summary>
private const string OwnerChangedEvent = "Window.OwnerChanged";
/// <summary>
/// Occurs when the <see cref="Owner"/> is changed.
/// </summary>
public event EventHandler<EventArgs> OwnerChanged
{
add { Properties.AddEvent(OwnerChangedEvent, value); }
remove { Properties.RemoveEvent(OwnerChangedEvent, value); }
}
/// <summary>
/// Raises the <see cref="OwnerChanged"/> event.
/// </summary>
/// <param name="e">Event arguments</param>
protected virtual void OnOwnerChanged(EventArgs e)
{
Properties.TriggerEvent(OwnerChangedEvent, this, e);
}
/// <summary>
/// Identifier for handlers when attaching the <see cref="WindowStateChanged"/> event.
/// </summary>
public const string WindowStateChangedEvent = "Window.WindowStateChanged";
/// <summary>
/// Occurs when the <see cref="WindowState"/> is changed.
/// </summary>
public event EventHandler<EventArgs> WindowStateChanged
{
add { Properties.AddHandlerEvent(WindowStateChangedEvent, value); }
remove { Properties.RemoveEvent(WindowStateChangedEvent, value); }
}
/// <summary>
/// Raises the <see cref="WindowStateChanged"/> event.
/// </summary>
/// <param name="e">Event arguments</param>
protected virtual void OnWindowStateChanged(EventArgs e)
{
Properties.TriggerEvent(WindowStateChangedEvent, this, e);
}
#endregion
static Window()
{
EventLookup.Register<Window>(c => c.OnClosed(null), ClosedEvent);
EventLookup.Register<Window>(c => c.OnClosing(null), ClosingEvent);
EventLookup.Register<Window>(c => c.OnLocationChanged(null), LocationChangedEvent);
EventLookup.Register<Window>(c => c.OnWindowStateChanged(null), WindowStateChangedEvent);
}
/// <summary>
/// Initializes a new instance of the <see cref="Eto.Forms.Window"/> class.
/// </summary>
protected Window()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Window"/> with the specified handler
/// </summary>
/// <param name="handler">Pre-created handler to attach to this instance</param>
protected Window(IHandler handler)
: base(handler)
{
}
/// <summary>
/// Gets or sets the title of the window
/// </summary>
/// <remarks>
/// The title of the window is displayed to the user usually at the top of the window, but in cases where
/// you show a window in a mobile environment, this may be the title shown in a navigation controller.
/// </remarks>
/// <value>The title of the window</value>
public string Title
{
get { return Handler.Title; }
set { Handler.Title = value; }
}
/// <summary>
/// Gets or sets the location of the window
/// </summary>
/// <remarks>
/// Note that in multi-monitor setups, the origin of the location is at the upper-left of <see cref="Eto.Forms.Screen.PrimaryScreen"/>
/// </remarks>
public new Point Location
{
get { return Handler.Location; }
set { Handler.Location = value; }
}
/// <summary>
/// Gets or sets the size and location of the window
/// </summary>
/// <value>The bounding rectangle of the window</value>
public new Rectangle Bounds
{
get { return new Rectangle(Handler.Location, Handler.Size); }
set
{
Handler.Location = value.Location;
Handler.Size = value.Size;
}
}
/// <summary>
/// Gets or sets the tool bar for the window.
/// </summary>
/// <remarks>
/// Note that each window can only have a single tool bar
/// </remarks>
/// <value>The tool bar for the window</value>
public ToolBar ToolBar
{
get { return Handler.ToolBar; }
set
{
var toolbar = Handler.ToolBar;
if (toolbar != null)
{
toolbar.TriggerUnLoad(EventArgs.Empty);
toolbar.Parent = null;
}
if (value != null)
{
value.Parent = this;
value.TriggerPreLoad(EventArgs.Empty);
}
Handler.ToolBar = value;
if (value != null)
value.TriggerLoad(EventArgs.Empty);
}
}
/// <summary>
/// Gets or sets the opacity of the window
/// </summary>
/// <value>The window opacity.</value>
public double Opacity
{
get { return Handler.Opacity; }
set { Handler.Opacity = value; }
}
/// <summary>
/// Closes the window
/// </summary>
/// <remarks>
/// Note that once a window is closed, it cannot be shown again in most platforms.
/// </remarks>
public virtual void Close()
{
Handler.Close();
}
static readonly object OwnerKey = new object();
/// <summary>
/// Gets or sets the owner of this window.
/// </summary>
/// <remarks>
/// This sets the parent window that has ownership over this window.
/// For a <see cref="Dialog"/>, this will be the window that will be disabled while the modal dialog is shown.
/// With a <see cref="Form"/>, the specified owner will always be below the current window when shown, and will
/// still be responsive to user input. Typically, but not always, the window will move along with the owner.
/// </remarks>
/// <value>The owner of this window.</value>
public Window Owner
{
get { return Properties.Get<Window>(OwnerKey); }
set {
Properties.Set(OwnerKey, value, () =>
{
Handler.SetOwner(value);
OnOwnerChanged(EventArgs.Empty);
});
}
}
/// <summary>
/// Gets the screen this window is mostly contained in. Typically defined by the screen center of the window is visible.
/// </summary>
/// <value>The window's current screen.</value>
public Screen Screen
{
get { return Handler.Screen; }
}
/// <summary>
/// Gets or sets the menu bar for this window
/// </summary>
/// <remarks>
/// Some platforms have a global menu bar (e.g. ubuntu, OS X).
/// When the winow is in focus, the global menu bar will be changed to reflect the menu assigned.
/// </remarks>
/// <value>The menu.</value>
public virtual MenuBar Menu
{
get { return Handler.Menu; }
set
{
var menu = Handler.Menu;
if (menu != null)
{
menu.OnUnLoad(EventArgs.Empty);
menu.Parent = null;
}
if (value != null)
{
value.Parent = this;
value.OnPreLoad(EventArgs.Empty);
}
Handler.Menu = value;
if (value != null)
value.OnLoad(EventArgs.Empty);
}
}
/// <summary>
/// Gets or sets the icon for the window to show in the menu bar.
/// </summary>
/// <remarks>
/// The icon should have many variations, such as 16x16, 24x24, 32x32, 48x48, 64x64, etc. This ensures that
/// the many places it is used (title bar, task bar, switch window, etc) all have optimized icon sizes.
///
/// For OS X, the application icon is specified in the .app bundle, not by this value.
/// </remarks>
/// <value>The icon for this window.</value>
public Icon Icon
{
get { return Handler.Icon; }
set { Handler.Icon = value; }
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Eto.Forms.Window"/> is resizable.
/// </summary>
/// <value><c>true</c> if resizable; otherwise, <c>false</c>.</value>
public bool Resizable
{
get { return Handler.Resizable; }
set { Handler.Resizable = value; }
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Eto.Forms.Window"/> can be maximized.
/// </summary>
/// <remarks>
/// This may hide or disable the minimize button on the title bar.
/// </remarks>
/// <value><c>true</c> if maximizable; otherwise, <c>false</c>.</value>
public bool Maximizable
{
get { return Handler.Maximizable; }
set { Handler.Maximizable = value; }
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Eto.Forms.Window"/> can be minimized.
/// </summary>
/// <remarks>
/// This may hide or disable the maximize button on the title bar.
/// </remarks>
/// <value><c>true</c> if minimizable; otherwise, <c>false</c>.</value>
public bool Minimizable
{
get { return Handler.Minimizable; }
set { Handler.Minimizable = value; }
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Eto.Forms.Window"/> will show in the taskbar.
/// </summary>
/// <remarks>
/// Some platforms, e.g. OS X do not show a separate icon for each running window.
/// </remarks>
/// <value><c>true</c> if the window will show in taskbar; otherwise, <c>false</c>.</value>
public bool ShowInTaskbar
{
get { return Handler.ShowInTaskbar; }
set { Handler.ShowInTaskbar = value; }
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Eto.Forms.Window"/> is above all other windows.
/// </summary>
/// <remarks>
/// The window should be above all other windows when this is true. In some platforms, this will show above all other windows only
/// when the application has focus.
/// </remarks>
/// <value><c>true</c> if the window should be topmost; otherwise, <c>false</c>.</value>
public bool Topmost
{
get { return Handler.Topmost; }
set { Handler.Topmost = value; }
}
/// <summary>
/// Gets or sets the state of the window.
/// </summary>
/// <value>The state of the window.</value>
public WindowState WindowState
{
get { return Handler.WindowState; }
set { Handler.WindowState = value; }
}
/// <summary>
/// Gets the bounds of the window before it was minimized or maximized, or the current bounds if <see cref="WindowState"/> is Normal.
/// </summary>
/// <remarks>
/// This is useful to retrieve the desired size and position of the window even though it is currently maximized or minimized.
/// </remarks>
/// <value>The restore bounds.</value>
public Rectangle RestoreBounds
{
get { return Handler.RestoreBounds; }
}
/// <summary>
/// Sets <see cref="WindowState"/> to <see cref="Eto.Forms.WindowState.Minimized"/>
/// </summary>
public void Minimize()
{
Handler.WindowState = WindowState.Minimized;
}
/// <summary>
/// Sets <see cref="WindowState"/> to <see cref="Eto.Forms.WindowState.Maximized"/>
/// </summary>
public void Maximize()
{
Handler.WindowState = WindowState.Maximized;
}
/// <summary>
/// Gets or sets the style of this window.
/// </summary>
/// <value>The window style.</value>
public WindowStyle WindowStyle
{
get { return Handler.WindowStyle; }
set { Handler.WindowStyle = value; }
}
/// <summary>
/// Brings the window in front of all other windows in the z-order.
/// </summary>
public void BringToFront()
{
Handler.BringToFront();
}
/// <summary>
/// Sends the window behind all other windows in the z-order.
/// </summary>
public void SendToBack()
{
Handler.SendToBack();
}
/// <summary>
/// Raises the <see cref="BindableWidget.DataContextChanged"/> event
/// </summary>
/// <remarks>
/// Implementors may override this to fire this event on child widgets in a heirarchy.
/// This allows a control to be bound to its own <see cref="BindableWidget.DataContext"/>, which would be set
/// on one of the parent control(s).
/// </remarks>
/// <param name="e">Event arguments</param>
protected override void OnDataContextChanged(EventArgs e)
{
base.OnDataContextChanged(e);
var tb = ToolBar;
if (tb != null)
tb.TriggerDataContextChanged(e);
var menu = Menu;
if (menu != null)
menu.TriggerDataContextChanged(e);
}
#region Callback
static readonly object callback = new Callback();
/// <summary>
/// Gets an instance of an object used to perform callbacks to the widget from handler implementations
/// </summary>
/// <returns>The callback instance to use for this widget</returns>
protected override object GetCallback() { return callback; }
/// <summary>
/// Callback interface for instances of <see cref="Window"/>
/// </summary>
public new interface ICallback : Panel.ICallback
{
/// <summary>
/// Raises the closed event.
/// </summary>
void OnClosed(Window widget, EventArgs e);
/// <summary>
/// Raises the closing event.
/// </summary>
void OnClosing(Window widget, CancelEventArgs e);
/// <summary>
/// Raises the location changed event.
/// </summary>
void OnLocationChanged(Window widget, EventArgs e);
/// <summary>
/// Raises the window state changed event.
/// </summary>
void OnWindowStateChanged(Window widget, EventArgs e);
}
/// <summary>
/// Callback methods for handlers of <see cref="Control"/>
/// </summary>
protected new class Callback : Panel.Callback, ICallback
{
/// <summary>
/// Raises the closed event.
/// </summary>
public void OnClosed(Window widget, EventArgs e)
{
widget.Platform.Invoke(() => widget.OnClosed(e));
}
/// <summary>
/// Raises the closing event.
/// </summary>
public void OnClosing(Window widget, CancelEventArgs e)
{
widget.Platform.Invoke(() => widget.OnClosing(e));
}
/// <summary>
/// Raises the location changed event.
/// </summary>
public void OnLocationChanged(Window widget, EventArgs e)
{
widget.Platform.Invoke(() => widget.OnLocationChanged(e));
}
/// <summary>
/// Raises the window state changed event.
/// </summary>
public void OnWindowStateChanged(Window widget, EventArgs e)
{
widget.Platform.Invoke(() => widget.OnWindowStateChanged(e));
}
}
#endregion
#region Handler
/// <summary>
/// Handler interface for the <see cref="Window"/>
/// </summary>
public new interface IHandler : Panel.IHandler
{
/// <summary>
/// Gets or sets the tool bar for the window.
/// </summary>
/// <remarks>
/// Note that each window can only have a single tool bar
/// </remarks>
/// <value>The tool bar for the window</value>
ToolBar ToolBar { get; set; }
/// <summary>
/// Closes the window
/// </summary>
/// <remarks>
/// Note that once a window is closed, it cannot be shown again in most platforms.
/// </remarks>
void Close();
/// <summary>
/// Gets or sets the location of the window
/// </summary>
/// <remarks>
/// Note that in multi-monitor setups, the origin of the location is at the upper-left of <see cref="Eto.Forms.Screen.PrimaryScreen"/>
/// </remarks>
new Point Location { get; set; }
/// <summary>
/// Gets or sets the opacity of the window
/// </summary>
/// <value>The window opacity.</value>
double Opacity { get; set; }
/// <summary>
/// Gets or sets the title of the window
/// </summary>
/// <remarks>
/// The title of the window is displayed to the user usually at the top of the window, but in cases where
/// you show a window in a mobile environment, this may be the title shown in a navigation controller.
/// </remarks>
/// <value>The title of the window</value>
string Title { get; set; }
/// <summary>
/// Gets the screen this window is mostly contained in. Typically defined by the screen center of the window is visible.
/// </summary>
/// <value>The window's current screen.</value>
Screen Screen { get; }
/// <summary>
/// Gets or sets the menu bar for this window
/// </summary>
/// <remarks>
/// Some platforms have a global menu bar (e.g. ubuntu, OS X).
/// When the winow is in focus, the global menu bar will be changed to reflect the menu assigned.
/// </remarks>
/// <value>The menu.</value>
MenuBar Menu { get; set; }
/// <summary>
/// Gets or sets the icon for the window to show in the menu bar.
/// </summary>
/// <remarks>
/// The icon should have many variations, such as 16x16, 24x24, 32x32, 48x48, 64x64, etc. This ensures that
/// the many places it is used (title bar, task bar, switch window, etc) all have optimized icon sizes.
///
/// For OS X, the application icon is specified in the .app bundle, not by this value.
/// </remarks>
/// <value>The icon for this window.</value>
Icon Icon { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Eto.Forms.Window"/> is resizable.
/// </summary>
/// <value><c>true</c> if resizable; otherwise, <c>false</c>.</value>
bool Resizable { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Eto.Forms.Window"/> can be maximized.
/// </summary>
/// <remarks>
/// This may hide or disable the minimize button on the title bar.
/// </remarks>
/// <value><c>true</c> if maximizable; otherwise, <c>false</c>.</value>
bool Maximizable { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Eto.Forms.Window"/> can be minimized.
/// </summary>
/// <remarks>
/// This may hide or disable the maximize button on the title bar.
/// </remarks>
/// <value><c>true</c> if minimizable; otherwise, <c>false</c>.</value>
bool Minimizable { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Eto.Forms.Window"/> will show in the taskbar.
/// </summary>
/// <remarks>
/// Some platforms, e.g. OS X do not show a separate icon for each running window.
/// </remarks>
/// <value><c>true</c> if the window will show in taskbar; otherwise, <c>false</c>.</value>
bool ShowInTaskbar { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Eto.Forms.Window"/> is above all other windows.
/// </summary>
/// <remarks>
/// The window should be above all other windows when this is true. In some platforms, this will show above all other windows only
/// when the application has focus.
/// </remarks>
/// <value><c>true</c> if the window should be topmost; otherwise, <c>false</c>.</value>
bool Topmost { get; set; }
/// <summary>
/// Gets or sets the state of the window.
/// </summary>
/// <value>The state of the window.</value>
WindowState WindowState { get; set; }
/// <summary>
/// Gets the bounds of the window before it was minimized or maximized.
/// </summary>
/// <remarks>
/// This is useful to retrieve the desired size and position of the window even though it is currently maximized or minimized.
/// </remarks>
/// <value>The restore bounds.</value>
Rectangle RestoreBounds { get; }
/// <summary>
/// Gets or sets the style of this window.
/// </summary>
/// <value>The window style.</value>
WindowStyle WindowStyle { get; set; }
/// <summary>
/// Brings the window in front of all other windows in the z-order.
/// </summary>
void BringToFront();
/// <summary>
/// Sends the window behind all other windows in the z-order.
/// </summary>
void SendToBack();
/// <summary>
/// Sets the owner of the window
/// </summary>
/// <param name="owner">Owner of the window</param>
void SetOwner(Window owner);
}
#endregion
}
}