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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
|
// 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.
//
// Copyright (c) 2004 Novell, Inc.
//
// Authors:
// Peter Bartok pbartok@novell.com
//
// NOT COMPLETE
using System;
using System.Drawing;
using System.ComponentModel;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading;
namespace System.Windows.Forms {
public class Form : ContainerControl {
#region Local Variables
internal bool closing;
[Flags]
enum Flags {
ShowInTaskbar = 0x0001,
MaximizeBox = 0x0002,
MinimizeBox = 0x0004,
TopMost = 0x0008
}
Flags flags = Flags.ShowInTaskbar | Flags.MaximizeBox | Flags.MinimizeBox;
FormBorderStyle formBorderStyle = FormBorderStyle.Sizable;
private static bool autoscale;
private static Size autoscale_base_size;
private bool is_modal;
internal bool end_modal; // This var is being monitored by the application modal loop
private IButtonControl accept_button;
private IButtonControl cancel_button;
private DialogResult dialog_result;
private FormStartPosition start_position;
private Form owner;
private Form.ControlCollection owned_forms;
private bool key_preview;
private MainMenu menu;
internal FormParentWindow form_parent_window;
#endregion // Local Variables
#region Private Classes
// This class will take over for the client area
internal class FormParentWindow : Control {
#region FormParentWindow Class Local Variables
internal Form owner;
#endregion // FormParentWindow Class Local Variables
#region FormParentWindow Class Constructor
internal FormParentWindow(Form owner) : base() {
this.owner = owner;
this.Width = 250;
this.Height = 250;
BackColor = owner.BackColor;
Text = "FormParent";
this.Location = new Point(0, 0);
this.Dock = DockStyle.Fill;
MouseDown += new MouseEventHandler (OnMouseDownForm);
MouseMove += new MouseEventHandler (OnMouseMoveForm);
owner.TextChanged += new EventHandler(OnFormTextChanged);
}
#endregion // FormParentWindow Class Constructor
#region FormParentWindow Class Protected Instance Methods
protected override CreateParams CreateParams {
get {
CreateParams cp;
cp = base.CreateParams;
cp.Style = (int)(WindowStyles.WS_OVERLAPPEDWINDOW |
WindowStyles.WS_VISIBLE |
WindowStyles.WS_CLIPSIBLINGS |
WindowStyles.WS_CLIPCHILDREN);
cp.Width = 250;
cp.Height = 250;
#if later
if (this.IsHandleCreated) {
int x;
int y;
int width;
int height;
int cwidth;
int cheight;
XplatUI.GetWindowPos(this.window.Handle, out x, out y, out width, out height, out cwidth, out cheight);
UpdateBounds(x, y, width, height);
owner.UpdateBounds(x, y, width, height);
}
#endif
return cp;
}
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
//owner.SetBoundsCore(owner.Bounds.X, owner.Bounds.Y, ClientSize.Width, ClientSize.Height, BoundsSpecified.All);
if (owner.menu == null) {
owner.SetBoundsCore(0, 0, ClientSize.Width, ClientSize.Height, BoundsSpecified.All);
} else {
int menu_height;
menu_height = MenuAPI.MenuBarCalcSize(DeviceContext, owner.Menu.menu_handle, ClientSize.Width);
Invalidate (new Rectangle (0, 0, ClientSize.Width, menu_height));
owner.SetBoundsCore(0, menu_height, ClientSize.Width, ClientSize.Height-menu_height, BoundsSpecified.All);
}
}
protected override void OnPaint(PaintEventArgs pevent) {
OnDrawMenu (pevent.Graphics);
}
protected override void WndProc(ref Message m) {
switch((Msg)m.Msg) {
case Msg.WM_CLOSE: {
CancelEventArgs args = new CancelEventArgs();
owner.OnClosing(args);
if (!args.Cancel) {
owner.OnClosed(EventArgs.Empty);
owner.closing = true;
base.WndProc(ref m);
break;
}
break;
}
default: {
base.WndProc (ref m);
break;
}
}
}
#endregion // FormParentWindow Class Protected Instance Methods
#region FormParentWindow Class Private Methods
private void OnMouseDownForm (object sender, MouseEventArgs e) {
if (owner.menu != null)
owner.menu.OnMouseDown (owner, e);
}
private void OnMouseMoveForm (object sender, MouseEventArgs e) {
if (owner.menu != null)
owner.menu.OnMouseMove (owner, e);
}
private void OnDrawMenu (Graphics dc) {
if (owner.menu != null) {
Rectangle rect = new Rectangle (0,0, Width, 0);
MenuAPI.DrawMenuBar (dc, owner.menu.Handle, rect);
}
}
private void OnFormTextChanged(object sender, EventArgs e) {
this.Text = ((Control)sender).Text;
}
#endregion // FormParentWindow Class Private Methods
}
#endregion // Private Classes
#region Public Classes
public class ControlCollection : Control.ControlCollection {
Form form_owner;
public ControlCollection(Form owner) : base(owner) {
this.form_owner = owner;
}
public override void Add(Control value) {
for (int i=0; i<list.Count; i++) {
if (list[i]==value) {
// Do we need to do anything here?
return;
}
}
list.Add(value);
((Form)value).owner=(Form)owner;
}
public override void Remove(Control value) {
((Form)value).owner = null;
base.Remove (value);
}
}
#endregion // Public Classes
#region Public Constructor & Destructor
public Form() {
closing = false;
is_modal = false;
end_modal = false;
dialog_result = DialogResult.None;
start_position = FormStartPosition.WindowsDefaultLocation;
key_preview = false;
menu = null;
owned_forms = new Form.ControlCollection(this);
}
#endregion // Public Constructor & Destructor
#region Public Static Properties
public static Form ActiveForm {
get {
//FIXME: create next driver call
return null;
}
}
#endregion // Public Static Properties
#region Public Instance Properties
public IButtonControl AcceptButton {
get {
return accept_button;
}
set {
accept_button = value;
}
}
public bool AutoScale {
get {
return autoscale;
}
set {
autoscale=value;
}
}
public virtual Size AutoScaleBaseSize {
get {
return autoscale_base_size;
}
set {
autoscale_base_size=value;
}
}
public IButtonControl CancelButton {
get {
return cancel_button;
}
set {
cancel_button = value;
}
}
public DialogResult DialogResult {
get {
return dialog_result;
}
set {
dialog_result = value;
if (is_modal && (dialog_result != DialogResult.None)) {
end_modal = true;
}
}
}
public FormBorderStyle FormBorderStyle {
get {
return formBorderStyle;
}
set {
formBorderStyle = value;
Invalidate ();
}
}
public bool KeyPreview {
get {
return key_preview;
}
set {
key_preview = value;
}
}
public bool MaximizeBox {
get {
return (flags & Flags.MaximizeBox) != 0;
}
set {
if (value)
flags &= ~Flags.MaximizeBox;
else
flags |= Flags.MaximizeBox;
}
}
public MainMenu Menu {
get {
return menu;
}
set {
if (menu != value) {
if (value == null) {
form_parent_window.Width = form_parent_window.Width; // Trigger a resize
}
menu = value;
// To simulate the non-client are for menus we create a
// new control as the 'client area' of our form. This
// way, the origin stays 0,0 and we don't have to fiddle with
// coordinates. The menu area is part of the original container
if (menu != null) {
form_parent_window.Width = form_parent_window.Width; // Trigger a resize
}
menu.SetForm (this);
MenuAPI.SetMenuBarWindow (menu.Handle, form_parent_window);
}
}
}
public bool MinimizeBox {
get {
return (flags & Flags.MinimizeBox) != 0;
}
set {
if (value)
flags &= ~Flags.MinimizeBox;
else
flags |= Flags.MinimizeBox;
}
}
public bool Modal {
get {
return is_modal;
}
}
public Form[] OwnedForms {
get {
Form[] form_list;
form_list = new Form[owned_forms.Count];
for (int i=0; i<owned_forms.Count; i++) {
form_list[i] = (Form)owned_forms[i];
}
return form_list;
}
}
public Form Owner {
get {
return owner;
}
set {
if (owner != value) {
if (owner != null) {
owner.RemoveOwnedForm(this);
}
owner = value;
owner.AddOwnedForm(this);
if (owner != null) {
XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
} else {
XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
}
}
}
}
public bool ShowInTaskbar {
get {
return (flags & Flags.ShowInTaskbar) != 0;
}
set {
if (value)
flags &= ~Flags.ShowInTaskbar;
else
flags |= Flags.ShowInTaskbar;
}
}
public FormStartPosition StartPosition {
get {
return start_position;
}
set {
if (start_position == FormStartPosition.WindowsDefaultLocation) { // Only do this if it's not set yet
start_position = value;
if (form_parent_window.IsHandleCreated) {
switch(start_position) {
case FormStartPosition.CenterParent: {
if (Parent!=null && Width>0 && Height>0) {
this.Location = new Point(Parent.Size.Width/2-Width/2, Parent.Size.Height/2-Height/2);
}
break;
}
case FormStartPosition.CenterScreen: {
if (Width>0 && Height>0) {
Size DisplaySize;
XplatUI.GetDisplaySize(out DisplaySize);
this.Location = new Point(DisplaySize.Width/2-Width/2, DisplaySize.Height/2-Height/2);
}
break;
}
default: {
break;
}
}
}
}
}
}
public bool TopMost {
get {
return (flags & Flags.TopMost) != 0;
}
set {
if (TopMost == value)
return;
if (value)
flags &= ~Flags.TopMost;
else
flags |= Flags.TopMost;
XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, TopMost);
}
}
#endregion // Public Instance Properties
#region Protected Instance Properties
[MonoTODO("Need to add MDI support")]
protected override CreateParams CreateParams {
get {
CreateParams cp = new CreateParams();
if (this.form_parent_window == null) {
form_parent_window = new FormParentWindow(this);
}
cp.Caption = "ClientArea";
cp.ClassName=XplatUI.DefaultClassName;
cp.ClassStyle = 0;
cp.ExStyle=0;
cp.Param=0;
cp.Parent = this.form_parent_window.window.Handle;
cp.X = Left;
cp.Y = Top;
cp.Width = Width;
cp.Height = Height;
cp.Style = (int)WindowStyles.WS_CHILD;
cp.Style |= (int)WindowStyles.WS_VISIBLE;
cp.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
cp.Style |= (int)WindowStyles.WS_CLIPCHILDREN;
if (ShowInTaskbar)
cp.ExStyle |= (int)WindowStyles.WS_EX_APPWINDOW;
if (MaximizeBox)
cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
if (MinimizeBox)
cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
return cp;
}
}
protected override Size DefaultSize {
get {
return new Size (250, 250);
}
}
protected override void OnPaint (PaintEventArgs pevent)
{
base.OnPaint (pevent);
}
#endregion // Protected Instance Properties
#region Public Static Methods
#endregion // Public Static Methods
#region Public Instance Methods
public void AddOwnedForm(Form ownedForm) {
owned_forms.Add(ownedForm);
}
public void RemoveOwnedForm(Form ownedForm) {
owned_forms.Remove(ownedForm);
}
public DialogResult ShowDialog() {
return ShowDialog(null);
}
public DialogResult ShowDialog(IWin32Window ownerWin32) {
Control owner = null;
if (ownerWin32 != null) {
owner = Control.FromHandle(ownerWin32.Handle);
}
if (is_modal) {
return DialogResult.None;
}
if (Visible) {
throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
}
if (!IsHandleCreated) {
CreateControl();
}
XplatUI.SetModal(window.Handle, true);
Show();
is_modal = true;
Application.ModalRun(this);
is_modal = false;
Hide();
XplatUI.SetModal(window.Handle, false);
return DialogResult;
}
public void Close ()
{
CancelEventArgs args = new CancelEventArgs ();
OnClosing (args);
if (!args.Cancel) {
OnClosed (EventArgs.Empty);
closing = true;
return;
}
}
#endregion // Public Instance Methods
#region Protected Instance Methods
protected override void CreateHandle() {
base.CreateHandle ();
}
protected override void OnCreateControl() {
base.OnCreateControl ();
OnLoad(EventArgs.Empty);
}
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated (e);
}
protected override void OnHandleDestroyed(EventArgs e) {
base.OnHandleDestroyed (e);
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (base.ProcessCmdKey (ref msg, keyData)) {
return true;
}
// Give our menu a shot
if (menu != null) {
return menu.ProcessCmdKey(ref msg, keyData);
}
return false;
}
protected override bool ProcessDialogKey(Keys keyData) {
if (keyData == Keys.Enter && accept_button != null) {
accept_button.PerformClick();
return true;
} else if (keyData == Keys.Enter && cancel_button != null) {
cancel_button.PerformClick();
return true;
}
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessKeyPreview(ref Message msg) {
if (key_preview) {
if (ProcessKeyEventArgs(ref msg)) {
return true;
}
}
return base.ProcessKeyPreview (ref msg);
}
protected override void WndProc(ref Message m) {
switch((Msg)m.Msg) {
case Msg.WM_CLOSE: {
CancelEventArgs args = new CancelEventArgs();
OnClosing(args);
if (!args.Cancel) {
OnClosed(EventArgs.Empty);
closing = true;
base.WndProc(ref m);
break;
}
break;
}
#if topmost_workaround
case Msg.WM_ACTIVATE: {
if (this.OwnedForms.Length>0) {
XplatUI.SetZOrder(this.OwnedForms[0].window.Handle, this.window.Handle, false, false);
}
break;
}
#endif
default: {
base.WndProc (ref m);
break;
}
}
}
#endregion // Protected Instance Methods
#region Events
protected virtual void OnActivated(EventArgs e) {
if (Activated != null) {
Activated(this, e);
}
}
protected virtual void OnClosed(EventArgs e) {
if (Closed != null) {
Closed(this, e);
}
}
protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
if (Closing != null) {
Closing(this, e);
}
}
protected virtual void OnDeactivate(EventArgs e) {
if (Deactivate != null) {
Deactivate(this, e);
}
}
protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
if (InputLanguageChanged!=null) {
InputLanguageChanged(this, e);
}
}
protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
if (InputLanguageChanging!=null) {
InputLanguageChanging(this, e);
}
}
protected virtual void OnLoad(EventArgs e) {
if (Load != null) {
Load(this, e);
}
}
protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
if (MaximizedBoundsChanged != null) {
MaximizedBoundsChanged(this, e);
}
}
protected virtual void OnMaximumSizeChanged(EventArgs e) {
if (MaximumSizeChanged != null) {
MaximumSizeChanged(this, e);
}
}
protected virtual void OnMdiChildActivate(EventArgs e) {
if (MdiChildActivate != null) {
MdiChildActivate(this, e);
}
}
protected virtual void OnMenuComplete(EventArgs e) {
if (MenuComplete != null) {
MenuComplete(this, e);
}
}
protected virtual void OnMenuStart(EventArgs e) {
if (MenuStart != null) {
MenuStart(this, e);
}
}
protected virtual void OnMinimumSizeChanged(EventArgs e) {
if (MinimumSizeChanged != null) {
MinimumSizeChanged(this, e);
}
}
public event EventHandler Activated;
public event EventHandler Closed;
public event CancelEventHandler Closing;
public event EventHandler Deactivate;
public event InputLanguageChangedEventHandler InputLanguageChanged;
public event InputLanguageChangingEventHandler InputLanguageChanging;
public event EventHandler Load;
public event EventHandler MaximizedBoundsChanged;
public event EventHandler MaximumSizeChanged;
public event EventHandler MdiChildActivate;
public event EventHandler MenuComplete;
public event EventHandler MenuStart;
public event EventHandler MinimumSizeChanged;
#endregion // Events
}
}
|