* ImageList.cs: When the image stream is set pull all the images
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Form.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25
26 // NOT COMPLETE
27
28 using System;
29 using System.Drawing;
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
32 using System.ComponentModel.Design.Serialization;
33 using System.Collections;
34 using System.Runtime.InteropServices;
35 using System.Threading;
36
37 namespace System.Windows.Forms {
38         [DesignerCategory("Form")]
39         [DesignTimeVisible(false)]
40         [Designer("System.Windows.Forms.Design.FormDocumentDesigner, " + Consts.AssemblySystem_Design, typeof(IRootDesigner))]
41         [DefaultEvent("Load")]
42         [ToolboxItem(false)]
43         public class Form : ContainerControl {
44                 #region Local Variables
45                 internal static Form            active_form;
46                 internal bool                   closing;
47                 FormBorderStyle                 formBorderStyle;
48                 private static bool             autoscale;
49                 private static Size             autoscale_base_size;
50                 internal bool                   is_modal;
51                 internal bool                   end_modal;                      // This var is being monitored by the application modal loop
52                 private bool                    control_box;
53                 private bool                    minimize_box;
54                 private bool                    maximize_box;
55                 private bool                    help_button;
56                 private bool                    show_in_taskbar;
57                 private bool                    topmost;
58                 private IButtonControl          accept_button;
59                 private IButtonControl          cancel_button;
60                 private DialogResult            dialog_result;
61                 private FormStartPosition       start_position;
62                 private Form                    owner;
63                 private Form.ControlCollection  owned_forms;
64                 private MdiClient               mdi_container;
65                 private Form                    mdi_parent;
66                 private bool                    key_preview;
67                 private MainMenu                menu;
68                 private Icon                    icon;
69                 private Size                    maximum_size;
70                 private Size                    minimum_size;
71                 private SizeGripStyle           size_grip_style;
72                 private Rectangle               maximized_bounds;
73                 private Rectangle               default_maximized_bounds;
74                 #endregion      // Local Variables
75
76                 #region Private & Internal Methods
77                 #endregion      // Private & Internal Methods
78
79                 #region Public Classes
80                 public new class ControlCollection : Control.ControlCollection {
81                         Form    form_owner;
82
83                         public ControlCollection(Form owner) : base(owner) {
84                                 this.form_owner = owner;
85                         }
86
87                         public override void Add(Control value) {
88                                 for (int i=0; i<list.Count; i++) {
89                                         if (list[i]==value) {
90                                                 // Do we need to do anything here?
91                                                 return;
92                                         }
93                                 }
94                                 list.Add(value);
95                                 ((Form)value).owner=(Form)owner;
96                         }
97
98                         public override void Remove(Control value) {
99                                 ((Form)value).owner = null;
100                                 base.Remove (value);
101                         }
102                 }
103                 #endregion      // Public Classes
104
105                 #region Public Constructor & Destructor
106                 public Form() {
107                         closing = false;
108                         is_modal = false;
109                         end_modal = false;
110                         dialog_result = DialogResult.None;
111                         start_position = FormStartPosition.WindowsDefaultLocation;
112                         formBorderStyle = FormBorderStyle.Sizable;
113                         key_preview = false;
114                         menu = null;
115                         icon = null;
116                         minimum_size = new Size(0, 0);
117                         maximum_size = new Size(0, 0);
118                         control_box = true;
119                         minimize_box = true;
120                         maximize_box = true;
121                         help_button = false;
122                         show_in_taskbar = true;
123                         ime_mode = ImeMode.NoControl;
124                         is_visible = false;
125                         is_toplevel = true;
126                         size_grip_style = SizeGripStyle.Auto;
127                         maximized_bounds = Rectangle.Empty;
128                         default_maximized_bounds = Rectangle.Empty;
129                         owned_forms = new Form.ControlCollection(this);
130                 }
131                 #endregion      // Public Constructor & Destructor
132
133                 #region Public Static Properties
134
135                 public static Form ActiveForm {
136                         get {
137                                 Control active;
138
139                                 active = FromHandle(XplatUI.GetActive());
140
141                                 if (active != null) {
142                                         if ( !(active is Form)) {
143                                                 Control parent;
144
145                                                 parent = active.Parent;
146                                                 while (parent != null) {
147                                                         if (parent is Form) {
148                                                                 return (Form)parent;
149                                                         }
150                                                         parent = parent.Parent;
151                                                 }
152                                         } else {
153                                                 return (Form)active;
154                                         }
155                                 }
156                                 return null;
157                         }
158                 }
159
160                 #endregion      // Public Static Properties
161
162                 #region Public Instance Properties
163                 [DefaultValue(null)]
164                 public IButtonControl AcceptButton {
165                         get {
166                                 return accept_button;
167                         }
168
169                         set {
170                                 accept_button = value;
171                         }
172                 }
173
174                 [MonoTODO("Figure out a way for transparency support in windows")]
175                 [Browsable(false)]
176                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
177                 public bool AllowTransparency {
178                         get {
179                                 return false;
180                         }
181
182                         set {
183                         }
184                 }
185                         
186                 [DefaultValue(true)]
187                 public bool AutoScale {
188                         get {
189                                 return autoscale;
190                         }
191
192                         set {
193                                 autoscale=value;
194                         }
195                 }
196
197                 [Localizable(true)]
198                 [Browsable(false)]
199                 [EditorBrowsable(EditorBrowsableState.Advanced)]
200                 public virtual Size AutoScaleBaseSize {
201                         get {
202                                 return autoscale_base_size;
203                         }
204
205                         set {
206                                 autoscale_base_size=value;
207                         }
208                 }
209
210                 [Localizable(true)]
211                 public override bool AutoScroll {
212                         get {
213                                 return base.AutoScroll;
214                         }
215                         set {
216                                 base.AutoScroll = value;
217                         }
218                 }
219
220                 public override Color BackColor {
221                         get {
222                                 return base.BackColor;
223                         }
224                         set {
225                                 base.BackColor = value;
226                         }
227                 }
228
229                 [DefaultValue(null)]
230                 public IButtonControl CancelButton {
231                         get {
232                                 return cancel_button;
233                         }
234
235                         set {
236                                 cancel_button = value;
237                         }
238                 }
239
240                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
241                 [Localizable(true)]
242                 public Size ClientSize {
243                         get {
244                                 return base.ClientSize;
245                         }
246
247                         set {
248                                 base.ClientSize = value;
249                         }
250                 }
251
252                 [DefaultValue(true)]
253                 public bool ControlBox {
254                         get {
255                                 return control_box;
256                         }
257
258                         set {
259                                 if (control_box != value) {
260                                         control_box = value;
261                                         UpdateStyles();
262                                 }
263                         }
264                 }
265
266                 [Browsable(false)]
267                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
268                 public Rectangle DesktopBounds {
269                         get {
270                                 return new Rectangle(Location, Size);
271                         }
272
273                         set {
274                                 Bounds = value;
275                         }
276                 }
277
278                 [Browsable(false)]
279                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
280                 public Point DesktopLocation {
281                         get {
282                                 return Location;
283                         }
284
285                         set {
286                                 Location = value;
287                         }
288                 }
289
290                 [Browsable(false)]
291                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
292                 public DialogResult DialogResult {
293                         get {
294                                 return dialog_result;
295                         }
296
297                         set {
298                                 dialog_result = value;
299
300                                 if (is_modal && (dialog_result != DialogResult.None)) {
301                                         end_modal = true;
302                                 }
303                         }
304                 }
305
306                 [DefaultValue(FormBorderStyle.Sizable)]
307                 [DispId(-504)]
308                 public FormBorderStyle FormBorderStyle {
309                         get {
310                                 return formBorderStyle;
311                         }
312                         set {
313                                 formBorderStyle = value;
314                                 UpdateStyles();
315                         }
316                 }
317
318                 [DefaultValue(false)]
319                 public bool HelpButton {
320                         get {
321                                 return help_button;
322                         }
323
324                         set {
325                                 if (help_button != value) {
326                                         help_button = value;
327                                         UpdateStyles();
328                                 }
329                         }
330                 }
331
332                 [Localizable(true)]
333                 [AmbientValue(null)]
334                 public Icon Icon {
335                         get {
336                                 return icon;
337                         }
338
339                         set {
340                                 if (icon != value) {
341                                         icon = value;
342
343                                         XplatUI.SetIcon(Handle, icon);
344                                 }
345                         }
346                 }
347
348                 [Browsable(false)]
349                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
350                 public bool IsMdiChild {
351                         get {
352                                 return mdi_parent != null;
353                         }
354                 }
355
356                 [DefaultValue(false)]
357                 public bool IsMdiContainer {
358                         get {
359                                 return mdi_container != null;
360                         }
361
362                         set {
363                                 if (value && mdi_container == null) {
364                                         mdi_container = new MdiClient();
365                                         Controls.Add(mdi_container);
366                                 } else if (!value && mdi_container != null) {
367                                         Controls.Remove(mdi_container);
368                                         mdi_container.Dispose();
369                                 }
370                         }
371                 }
372
373                 [Browsable(false)]
374                 [EditorBrowsable(EditorBrowsableState.Advanced)]
375                 public bool IsRestrictedWindow {
376                         get {
377                                 return false;
378                         }
379                 }
380
381                 [DefaultValue(false)]
382                 public bool KeyPreview {
383                         get {
384                                 return key_preview;
385                         }
386
387                         set {
388                                 key_preview = value;
389                         }
390                 }
391
392                 [DefaultValue(true)]
393                 public bool MaximizeBox {
394                         get {
395                                 return maximize_box;
396                         }
397                         set {
398                                 if (maximize_box != value) {
399                                         maximize_box = value;
400                                         UpdateStyles();
401                                 }
402                         }
403                 }
404
405                 [DefaultValue(typeof(Size), "{Width=0, Height=0}")]
406                 [Localizable(true)]
407                 [RefreshProperties(RefreshProperties.Repaint)]
408                 public Size MaximumSize {
409                         get {
410                                 return maximum_size;
411                         }
412
413                         set {
414                                 if (maximum_size != value) {
415                                         maximum_size = value;
416                                         OnMaximumSizeChanged(EventArgs.Empty);
417                                 }
418                         }
419                 }
420
421                 [Browsable(false)]
422                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
423                 public Form[] MdiChildren {
424                         get {
425                                 if (mdi_container != null) {
426                                         Form[] form_list;
427
428                                         form_list = new Form[mdi_container.Controls.Count];
429                                         for (int i = 0; i < mdi_container.Controls.Count; i++) {
430                                                 form_list[i] = (Form)mdi_container.Controls[i];
431                                         }
432                                         return form_list;
433                                 } else {
434                                         return new Form[0];
435                                 }
436                         }
437                 }
438
439                 [MonoTODO("Finish setter")]
440                 [Browsable(false)]
441                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
442                 public Form MdiParent {
443                         get {
444                                 return mdi_parent;
445                         }
446
447                         set {
448                                 if (mdi_parent != null) {
449                                         mdi_parent.Controls.Remove(this);
450                                 }
451
452                                 mdi_parent = value;
453                                 if (mdi_parent != null) {
454                                         mdi_parent.Controls.Add(this);
455                                 }
456                         }
457                 }
458
459                 [DefaultValue(null)]
460                 public MainMenu Menu {
461                         get {
462                                 return menu;
463                         }
464
465                         set {                           
466                                 if (menu != value) {                                    
467                                         menu = value;
468
469                                         menu.SetForm (this);
470                                         MenuAPI.SetMenuBarWindow (menu.Handle, this);
471                                         
472                                         XplatUI.SetMenu(window.Handle, menu.Handle);
473
474                                         // FIXME - Do we still need this?
475                                         this.SetBoundsCore(0, 0, 0, 0, BoundsSpecified.None);
476
477                                         ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu.Handle, ClientSize.Width);
478                                 }
479                         }
480                 }
481
482                 [DefaultValue(true)]
483                 public bool MinimizeBox {
484                         get {
485                                 return minimize_box;
486                         }
487                         set {
488                                 if (minimize_box != value) {
489                                         minimize_box = value;
490                                         UpdateStyles();
491                                 }
492                         }
493                 }
494
495                 [DefaultValue(typeof(Size), "{Width=0, Height=0}")]
496                 [Localizable(true)]
497                 [RefreshProperties(RefreshProperties.Repaint)]
498                 public Size MinimumSize {
499                         get {
500                                 return minimum_size;
501                         }
502
503                         set {
504                                 if (minimum_size != value) {
505                                         minimum_size = value;
506                                         OnMinimumSizeChanged(EventArgs.Empty);
507                                 }
508                         }
509                 }
510
511                 [Browsable(false)]
512                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
513                 public bool Modal  {
514                         get {
515                                 return is_modal;
516                         }
517                 }
518
519                 [Browsable(false)]
520                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
521                 public Form[] OwnedForms {
522                         get {
523                                 Form[] form_list;
524
525                                 form_list = new Form[owned_forms.Count];
526
527                                 for (int i=0; i<owned_forms.Count; i++) {
528                                         form_list[i] = (Form)owned_forms[i];
529                                 }
530
531                                 return form_list;
532                         }
533                 }
534
535                 [Browsable(false)]
536                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
537                 public Form Owner {
538                         get {
539                                 return owner;
540                         }
541
542                         set {
543                                 if (owner != value) {
544                                         if (owner != null) {
545                                                 owner.RemoveOwnedForm(this);
546                                         }
547                                         owner = value;
548                                         owner.AddOwnedForm(this);
549                                         if (owner != null) {
550                                                 XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
551                                         } else {
552                                                 XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
553                                         }
554                                 }
555                         }
556                 }
557
558                 [DefaultValue(true)]
559                 public bool ShowInTaskbar {
560                         get {
561                                 return show_in_taskbar;
562                         }
563                         set {
564                                 if (show_in_taskbar != value) {
565                                         show_in_taskbar = value;
566                                         UpdateStyles();
567                                 }
568                         }
569                 }
570
571                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
572                 [Localizable(false)]
573                 public Size Size {
574                         get {
575                                 return Size;
576                         }
577
578                         set {
579                                 base.Size = value;
580                         }
581                 }
582
583                 [MonoTODO("Trigger something when GripStyle is set")]
584                 [DefaultValue(SizeGripStyle.Auto)]
585                 public SizeGripStyle SizeGripStyle {
586                         get {
587                                 return size_grip_style;
588                         }
589
590                         set {
591                                 size_grip_style = value;
592                         }
593                 }
594
595                 [DefaultValue(FormStartPosition.WindowsDefaultLocation)]
596                 [Localizable(true)]
597                 public FormStartPosition StartPosition {
598                         get {
599                                 return start_position;
600                         }
601
602                         set {
603                                 if (start_position == FormStartPosition.WindowsDefaultLocation) {               // Only do this if it's not set yet
604                                         start_position = value;
605                                         if (IsHandleCreated) {
606                                                 switch(start_position) {
607                                                         case FormStartPosition.CenterParent: {
608                                                                 CenterToParent();
609                                                                 break;
610                                                         }
611
612                                                         case FormStartPosition.CenterScreen: {
613                                                                 CenterToScreen();
614                                                                 break;
615                                                         }
616
617                                                         default: {
618                                                                 break;
619                                                         }
620                                                 }
621                                         }
622                                 }
623                         }
624                 }
625
626                 [Browsable(false)]
627                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
628                 [EditorBrowsable(EditorBrowsableState.Never)]
629                 public int TabIndex {
630                         get {
631                                 return base.TabIndex;
632                         }
633
634                         set {
635                                 base.TabIndex = value;
636                         }
637                 }
638
639                 [Browsable(false)]
640                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
641                 [EditorBrowsable(EditorBrowsableState.Advanced)]
642                 public bool TopLevel {
643                         get {
644                                 return GetTopLevel();
645                         }
646
647                         set {
648                                 SetTopLevel(value);
649                         }
650                 }
651
652                 [DefaultValue(false)]
653                 public bool TopMost {
654                         get {
655                                 return topmost;
656                         }
657
658                         set {
659                                 if (topmost != value) {
660                                         topmost = value;
661                                         XplatUI.SetTopmost(window.Handle, owner != null ? owner.window.Handle : IntPtr.Zero, value);
662                                 }
663                         }
664                 }
665
666                 [DefaultValue(FormWindowState.Normal)]
667                 public FormWindowState WindowState {
668                         get {
669                                 return XplatUI.GetWindowState(window.Handle);
670                         }
671
672                         set {
673                                 XplatUI.SetWindowState(window.Handle, value);
674                         }
675                 }
676
677                 #endregion      // Public Instance Properties
678
679                 #region Protected Instance Properties
680                 [MonoTODO("Need to add MDI support")]
681                 protected override CreateParams CreateParams {
682                         get {
683                                 CreateParams cp;
684
685                                 cp = new CreateParams();
686
687                                 cp.Caption = "FormWindow";
688                                 cp.ClassName = XplatUI.DefaultClassName;
689                                 cp.ClassStyle = 0;
690                                 cp.ExStyle = 0;
691                                 cp.Param = 0;
692                                 cp.Parent = IntPtr.Zero;
693 //                              if (start_position == FormStartPosition.WindowsDefaultLocation) {
694                                         cp.X = unchecked((int)0x80000000);
695                                         cp.Y = unchecked((int)0x80000000);
696 //                              } else {
697 //                                      cp.X = Left;
698 //                                      cp.Y = Top;
699 //                              }
700                                 cp.Width = Width;
701                                 cp.Height = Height;
702                                 
703                                 cp.Style = (int)(WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN);
704
705                                 switch (FormBorderStyle) {
706                                         case FormBorderStyle.Fixed3D: {
707                                                 cp.Style |= (int)WindowStyles.WS_CAPTION;
708                                                 cp.ExStyle |= (int)WindowStyles.WS_EX_OVERLAPPEDWINDOW;
709                                                 break;
710                                         }
711
712                                         case FormBorderStyle.FixedDialog: {
713                                                 cp.Style |= (int)WindowStyles.WS_CAPTION;
714                                                 cp.ExStyle |= (int)(WindowStyles.WS_EX_DLGMODALFRAME | WindowStyles.WS_EX_WINDOWEDGE);
715                                                 break;
716                                         }
717
718                                         case FormBorderStyle.FixedSingle: {
719                                                 cp.Style |= (int)WindowStyles.WS_CAPTION;
720                                                 cp.ExStyle |= (int)(WindowStyles.WS_EX_WINDOWEDGE);
721                                                 break;
722                                         }
723
724                                         case FormBorderStyle.FixedToolWindow: {
725                                                 cp.Style |= (int)WindowStyles.WS_CAPTION;
726                                                 cp.ExStyle |= (int)(WindowStyles.WS_EX_WINDOWEDGE | WindowStyles.WS_EX_TOOLWINDOW);
727                                                 break;
728                                         }
729
730                                         case FormBorderStyle.Sizable: {
731                                                 cp.Style |= (int)WindowStyles.WS_OVERLAPPEDWINDOW;
732                                                 cp.ExStyle |= (int)(WindowStyles.WS_EX_WINDOWEDGE);
733                                                 break;
734                                         }
735
736                                         case FormBorderStyle.SizableToolWindow: {
737                                                 cp.Style |= (int)WindowStyles.WS_OVERLAPPEDWINDOW;
738                                                 cp.ExStyle |= (int)(WindowStyles.WS_EX_WINDOWEDGE | WindowStyles.WS_EX_TOOLWINDOW);
739                                                 break;
740                                         }
741                                 }
742
743                                 if (ShowInTaskbar) {
744                                         cp.ExStyle |= (int)WindowStyles.WS_EX_APPWINDOW;
745                                 }
746
747                                 if (MaximizeBox) {
748                                         cp.Style |= (int)WindowStyles.WS_MAXIMIZEBOX;
749                                 }
750
751                                 if (MinimizeBox) {
752                                         cp.Style |= (int)WindowStyles.WS_MINIMIZEBOX;
753                                 }
754
755                                 if (ControlBox) {
756                                         cp.Style |= (int)WindowStyles.WS_SYSMENU;
757                                 }
758
759                                 if (HelpButton) {
760                                         cp.ExStyle |= (int)WindowStyles.WS_EX_CONTEXTHELP;
761                                 }
762                                 return cp;
763                         }
764                 }
765
766                 protected override ImeMode DefaultImeMode {
767                         get {
768                                 return ImeMode.NoControl;
769                         }
770                 }
771
772                 protected override Size DefaultSize {
773                         get {
774                                 return new Size (250, 250);
775                         }
776                 }               
777
778                 protected Rectangle MaximizedBounds {
779                         get {
780                                 if (maximized_bounds != Rectangle.Empty) {
781                                         return maximized_bounds;
782                                 }
783                                 return default_maximized_bounds;
784                         }
785
786                         set {
787                                 maximized_bounds = value;
788                                 OnMaximizedBoundsChanged(EventArgs.Empty);
789                         }
790                 }
791                 #endregion      // Protected Instance Properties
792
793                 #region Public Static Methods
794                 [MonoTODO("Figure out the math")]
795                 [EditorBrowsable(EditorBrowsableState.Advanced)]
796                 public static SizeF GetAutoScaleSize(Font font) {
797                         SizeF   result;
798
799                         result = new SizeF(250, 250);
800                         result.Width *= font.SizeInPoints / 12;
801                         result.Height *= font.SizeInPoints / 12;
802                         return result;
803                 }
804                 #endregion      // Public Static Methods
805
806                 #region Public Instance Methods
807                 public void Activate() {
808                         Form    active;
809
810                         // The docs say activate only activates if our app is already active
811                         active = ActiveForm;
812                         if ((active != null) && (this != active)) {
813                                 XplatUI.Activate(window.Handle);
814                         }
815                 }
816
817                 public void AddOwnedForm(Form ownedForm) {
818                         owned_forms.Add(ownedForm);
819                 }
820
821                 public void Close () {
822                         CancelEventArgs args = new CancelEventArgs ();
823                         OnClosing (args);
824                         if (!args.Cancel) {
825                                 OnClosed (EventArgs.Empty);
826                                 closing = true;
827                                 return;
828                         }
829                 }
830
831                 public void LayoutMdi(MdiLayout value) {
832                         if (mdi_container != null) {
833                                 mdi_container.LayoutMdi(value);
834                         }
835                 }
836
837                 public void RemoveOwnedForm(Form ownedForm) {
838                         owned_forms.Remove(ownedForm);
839                 }
840
841                 public void SetDesktopBounds(int x, int y, int width, int height) {
842                         DesktopBounds = new Rectangle(x, y, width, height);
843                 }
844
845                 public void SetDesktopLocation(int x, int y) {
846                         DesktopLocation = new Point(x, y);
847                 }
848
849                 public DialogResult ShowDialog() {
850                         return ShowDialog(null);
851                 }
852
853                 public DialogResult ShowDialog(IWin32Window ownerWin32) {
854                         Form            previous;
855
856                         #if broken
857                         Control         owner = null;
858
859                         if (ownerWin32 != null) {
860                                 owner = Control.FromHandle(ownerWin32.Handle);
861                         }
862                         #endif
863
864                         if (is_modal) {
865                                 return DialogResult.None;
866                         }
867
868                         if (Visible) {
869                                 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
870                         }
871
872                         #if broken
873                         // Can't do this, will screw us in the modal loop
874                         form_parent_window.Parent = owner;
875                         #endif
876
877                         previous = Form.ActiveForm;
878
879                         if (!IsHandleCreated) {
880                                 CreateControl();
881                         }
882
883                         XplatUI.SetModal(window.Handle, true);
884
885                         Show();
886                         PerformLayout();
887
888                         is_modal = true;
889                         Application.ModalRun(this);
890                         is_modal = false;
891                         Hide();
892
893                         XplatUI.SetModal(window.Handle, false);
894
895                         if (previous != null) {
896                                 // Cannot use Activate(), it has a check for the current active window...
897                                 XplatUI.Activate(previous.window.Handle);
898                         }
899
900                         return DialogResult;
901                 }
902
903                 public override string ToString() {
904                         return GetType().FullName.ToString() + ", Text: " + Text;
905                 }
906                 #endregion      // Public Instance Methods
907
908                 #region Protected Instance Methods
909                 [MonoTODO("Finish when MDI is more complete")]
910                 protected void ActivateMdiChild(Form form) {
911                         OnMdiChildActivate(EventArgs.Empty);
912                         throw new NotImplementedException();
913                 }
914
915                 [EditorBrowsable(EditorBrowsableState.Advanced)]
916                 protected override void AdjustFormScrollbars(bool displayScrollbars) {
917                         base.AdjustFormScrollbars (displayScrollbars);
918                 }
919
920                 [EditorBrowsable(EditorBrowsableState.Advanced)]
921                 protected void ApplyAutoScaling() {
922                         // Hm, not sure what this does
923                 }
924
925                 protected void CenterToParent() {
926                         Control ctl;
927                         int     w;
928                         int     h;
929
930                         if (Width > 0) {
931                                 w = Width;
932                         } else {
933                                 w = DefaultSize.Width;
934                         }
935
936                         if (Height > 0) {
937                                 h = Height;
938                         } else {
939                                 h = DefaultSize.Height;
940                         }
941
942                         ctl = null;
943                         if (parent != null) {
944                                 ctl = parent;
945                         } else if (owner != null) {
946                                 ctl = owner;
947                         }
948
949                         if (owner != null) {
950                                 this.Location = new Point(ctl.Left + ctl.Width / 2 - w /2, ctl.Top + ctl.Height / 2 - h / 2);
951                         }
952                 }
953
954                 protected void CenterToScreen() {
955                         Size    DisplaySize;
956                         int     w;
957                         int     h;
958
959                         if (Width > 0) {
960                                 w = Width;
961                         } else {
962                                 w = DefaultSize.Width;
963                         }
964
965                         if (Height > 0) {
966                                 h = Height;
967                         } else {
968                                 h = DefaultSize.Height;
969                         }
970
971                         XplatUI.GetDisplaySize(out DisplaySize);
972                         this.Location = new Point(DisplaySize.Width / 2 - w / 2, DisplaySize.Height / 2 - h / 2);
973                 }
974
975                 [EditorBrowsable(EditorBrowsableState.Advanced)]
976                 protected override Control.ControlCollection CreateControlsInstance() {
977                         return base.CreateControlsInstance ();
978                 }
979
980                 [EditorBrowsable(EditorBrowsableState.Advanced)]
981                 protected override void CreateHandle() {
982                         base.CreateHandle ();
983                 }
984
985                 [EditorBrowsable(EditorBrowsableState.Advanced)]
986                 protected override void DefWndProc(ref Message m) {
987                         base.DefWndProc (ref m);
988                 }
989
990                 protected override void Dispose(bool disposing) {
991                         base.Dispose (disposing);
992                 }
993
994                 [EditorBrowsable(EditorBrowsableState.Advanced)]
995                 protected virtual void OnActivated(EventArgs e) {
996                         if (Activated != null) {
997                                 Activated(this, e);
998                         }
999                 }
1000
1001                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1002                 protected virtual void OnClosed(EventArgs e) {
1003                         if (Closed != null) {
1004                                 Closed(this, e);
1005                         }
1006                 }
1007
1008                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1009                 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
1010                         if (Closing != null) {
1011                                 Closing(this, e);
1012                         }
1013                 }
1014
1015                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1016                 protected override void OnCreateControl() {
1017                         base.OnCreateControl ();
1018                         if (this.ActiveControl == null) {
1019                                 bool visible;
1020
1021                                 // This visible hack is to work around CanSelect always being false if one of the parents
1022                                 // is not visible; and we by default create Form invisible...
1023                                 visible = this.is_visible;
1024                                 this.is_visible = true;
1025
1026                                 if (SelectNextControl(this, true, true, true, true) == false) {
1027                                         Select(this);
1028                                 }
1029
1030                                 this.is_visible = visible;
1031                         }
1032                         OnLoad(EventArgs.Empty);
1033
1034                         // Send initial location
1035                         OnLocationChanged(EventArgs.Empty);
1036                 }
1037
1038                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1039                 protected virtual void OnDeactivate(EventArgs e) {
1040                         if (Deactivate != null) {
1041                                 Deactivate(this, e);
1042                         }
1043                 }
1044
1045                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1046                 protected override void OnFontChanged(EventArgs e) {
1047                         base.OnFontChanged (e);
1048                 }
1049
1050                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1051                 protected override void OnHandleCreated(EventArgs e) {
1052                         base.OnHandleCreated (e);
1053                 }
1054
1055                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1056                 protected override void OnHandleDestroyed(EventArgs e) {
1057                         base.OnHandleDestroyed (e);
1058                 }
1059
1060                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1061                 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
1062                         if (InputLanguageChanged!=null) {
1063                                 InputLanguageChanged(this, e);
1064                         }
1065                 }
1066
1067                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1068                 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
1069                         if (InputLanguageChanging!=null) {
1070                                 InputLanguageChanging(this, e);
1071                         }
1072                 }
1073
1074                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1075                 protected virtual void OnLoad(EventArgs e) {
1076                         if (Load != null) {
1077                                 Load(this, e);
1078                         }
1079                 }
1080
1081                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1082                 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
1083                         if (MaximizedBoundsChanged != null) {
1084                                 MaximizedBoundsChanged(this, e);
1085                         }
1086                 }
1087
1088                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1089                 protected virtual void OnMaximumSizeChanged(EventArgs e) {
1090                         if (MaximumSizeChanged != null) {
1091                                 MaximumSizeChanged(this, e);
1092                         }
1093                 }
1094
1095                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1096                 protected virtual void OnMdiChildActivate(EventArgs e) {
1097                         if (MdiChildActivate != null) {
1098                                 MdiChildActivate(this, e);
1099                         }
1100                 }
1101
1102                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1103                 protected virtual void OnMenuComplete(EventArgs e) {
1104                         if (MenuComplete != null) {
1105                                 MenuComplete(this, e);
1106                         }
1107                 }
1108
1109                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1110                 protected virtual void OnMenuStart(EventArgs e) {
1111                         if (MenuStart != null) {
1112                                 MenuStart(this, e);
1113                         }
1114                 }
1115
1116                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1117                 protected virtual void OnMinimumSizeChanged(EventArgs e) {
1118                         if (MinimumSizeChanged != null) {
1119                                 MinimumSizeChanged(this, e);
1120                         }
1121                 }
1122
1123                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1124                 protected override void OnPaint (PaintEventArgs pevent) {
1125                         base.OnPaint (pevent);
1126                 }               
1127                 
1128                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1129                 protected override void OnResize(EventArgs e) {
1130                         base.OnResize(e);
1131                 }
1132
1133                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1134                 protected override void OnStyleChanged(EventArgs e) {
1135                         base.OnStyleChanged (e);
1136                 }
1137
1138                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1139                 protected override void OnTextChanged(EventArgs e) {
1140                         base.OnTextChanged (e);
1141                 }
1142
1143                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1144                 protected override void OnVisibleChanged(EventArgs e) {
1145                         base.OnVisibleChanged (e);
1146                 }
1147
1148                 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
1149                         if (base.ProcessCmdKey (ref msg, keyData)) {
1150                                 return true;
1151                         }
1152
1153                         // Give our menu a shot
1154                         if (menu != null) {
1155                                 return menu.ProcessCmdKey(ref msg, keyData);
1156                         }
1157
1158                         return false;
1159                 }
1160
1161                 // LAMESPEC - Not documented that Form overrides ProcessDialogChar; class-status showed
1162                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1163                 protected override bool ProcessDialogChar(char charCode) {
1164                         return base.ProcessDialogChar (charCode);
1165                 }
1166
1167                 protected override bool ProcessDialogKey(Keys keyData) {
1168                         if ((keyData & Keys.Modifiers) == 0) {
1169                                 if (keyData == Keys.Enter && accept_button != null) {
1170                                         accept_button.PerformClick();
1171                                         return true;
1172                                 } else if (keyData == Keys.Escape && cancel_button != null) {
1173                                         cancel_button.PerformClick();
1174                                         return true;
1175                                 }
1176                         }
1177                         return base.ProcessDialogKey(keyData);
1178                 }
1179
1180                 protected override bool ProcessKeyPreview(ref Message msg) {
1181                         if (key_preview) {
1182                                 if (ProcessKeyEventArgs(ref msg)) {
1183                                         return true;
1184                                 }
1185                         }
1186                         return base.ProcessKeyPreview (ref msg);
1187                 }
1188
1189                 protected override bool ProcessTabKey(bool forward) {
1190                         return SelectNextControl(ActiveControl, forward, true, true, true);
1191                 }
1192
1193                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1194                 protected override void ScaleCore(float dx, float dy) {
1195                         base.ScaleCore (dx, dy);
1196                 }
1197
1198                 protected override void Select(bool directed, bool forward) {
1199                         Form    parent;
1200
1201                         if (directed) {
1202                                 base.SelectNextControl(null, forward, true, true, true);
1203                         }
1204
1205                         parent = this.ParentForm;
1206                         if (parent != null) {
1207                                 parent.ActiveControl = this;
1208                         }
1209
1210                         Activate();
1211                 }
1212
1213                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1214                 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
1215                         base.SetBoundsCore (x, y, width, height, specified);
1216                 }
1217
1218                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1219                 protected override void SetClientSizeCore(int x, int y) {
1220                         if ((minimum_size.Width != 0) && (x < minimum_size.Width)) {
1221                                 x = minimum_size.Width;
1222                         } else if ((maximum_size.Width != 0) && (x > maximum_size.Width)) {
1223                                 x = maximum_size.Width;
1224                         }
1225
1226                         if ((minimum_size.Height != 0) && (y < minimum_size.Height)) {
1227                                 y = minimum_size.Height;
1228                         } else if ((maximum_size.Height != 0) && (y > maximum_size.Height)) {
1229                                 y = maximum_size.Height;
1230                         }
1231
1232                         base.SetClientSizeCore (x, y);
1233                 }
1234
1235                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1236                 protected override void SetVisibleCore(bool value) {
1237                         base.SetVisibleCore (value);
1238                 }
1239
1240                 protected override void UpdateDefaultButton() {
1241                         base.UpdateDefaultButton ();
1242                 }
1243
1244                 [EditorBrowsable(EditorBrowsableState.Advanced)]
1245                 protected override void WndProc(ref Message m) {
1246                         switch((Msg)m.Msg) {
1247                                 case Msg.WM_CLOSE: {
1248                                         CancelEventArgs args = new CancelEventArgs();
1249
1250                                         OnClosing(args);
1251
1252                                         if (!args.Cancel) {
1253                                                 OnClosed(EventArgs.Empty);
1254                                                 closing = true;
1255                                                 base.WndProc(ref m);
1256                                                 break;
1257                                         }
1258                                         break;
1259                                 }
1260
1261                                 case Msg.WM_ACTIVATE: {
1262                                         if (m.WParam != (IntPtr)WindowActiveFlags.WA_INACTIVE) {
1263                                                 OnActivated(EventArgs.Empty);
1264                                         } else {
1265                                                 OnDeactivate(EventArgs.Empty);
1266                                         }
1267                                         return;
1268                                 }
1269
1270                                 case Msg.WM_KILLFOCUS: {
1271                                         base.WndProc(ref m);
1272                                         return;
1273                                 }
1274
1275                                 case Msg.WM_SETFOCUS: {
1276 #if not
1277                                         if (this.ActiveControl != null) {
1278                                                 ActiveControl.Focus();
1279                                         }
1280 #endif
1281                                         base.WndProc(ref m);
1282                                         return;
1283                                 }
1284
1285                                 // Menu drawing
1286                                 case Msg.WM_NCLBUTTONDOWN: {
1287                                         if (this.menu != null) {
1288                                                 int x = LowOrder ((int) m.LParam.ToInt32 ()) ;
1289                                                 int y = HighOrder ((int) m.LParam.ToInt32 ());                                          
1290                                                 menu.OnMouseDown(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), mouse_clicks, x, y, 0));
1291                                         }
1292                                         base.WndProc(ref m);
1293                                         return;
1294                                 }
1295
1296                                 case Msg.WM_NCMOUSEMOVE: {
1297                                         if (this.menu != null) {
1298                                                 menu.OnMouseMove(this, new MouseEventArgs (FromParamToMouseButtons ((int) m.WParam.ToInt32()), 
1299                                                         mouse_clicks, LowOrder ((int) m.LParam.ToInt32 ()), HighOrder ((int) m.LParam.ToInt32 ()), 0));
1300                                         }
1301                                         base.WndProc(ref m);
1302                                         return;
1303                                 }
1304
1305                                 case Msg.WM_NCPAINT: {
1306                                         if (this.menu != null) {
1307                                                 Point pnt = XplatUI.GetMenuOrigin(window.Handle);
1308                                                 MenuAPI.DrawMenuBar (menu.Handle, new Rectangle (pnt.X, pnt.Y, ClientSize.Width, 0));
1309                                         }
1310
1311                                         base.WndProc(ref m);
1312                                         return;
1313                                 }
1314
1315                                 // This message is only received under Win32
1316                                 case Msg.WM_NCCALCSIZE: {
1317                                         XplatUIWin32.NCCALCSIZE_PARAMS  ncp;
1318
1319                                         if ((menu != null) && (m.WParam == (IntPtr)1)) {
1320                                                 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.NCCALCSIZE_PARAMS));
1321
1322                                                 // Adjust for menu
1323                                                 ncp.rgrc1.top += ThemeEngine.Current.CalcMenuBarSize (DeviceContext, menu.menu_handle, ClientSize.Width);
1324                                                 Marshal.StructureToPtr(ncp, m.LParam, true);
1325                                         }
1326                                         DefWndProc(ref m);
1327                                         break;
1328                                 }
1329
1330                                 case Msg.WM_GETMINMAXINFO: {
1331                                         XplatUIWin32.MINMAXINFO mmi;
1332
1333                                         if (m.LParam != IntPtr.Zero) {
1334                                                 mmi = (XplatUIWin32.MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(XplatUIWin32.MINMAXINFO));
1335                                                 default_maximized_bounds = new Rectangle(mmi.ptMaxPosition.x, mmi.ptMaxPosition.y, mmi.ptMaxSize.x, mmi.ptMaxSize.y);
1336                                                 if (maximized_bounds != Rectangle.Empty) {
1337                                                         mmi.ptMaxSize.x = maximized_bounds.Width;
1338                                                         mmi.ptMaxSize.y = maximized_bounds.Height;
1339                                                 }
1340
1341                                                 Marshal.StructureToPtr(mmi, m.LParam, false);
1342                                         }
1343                                         break;
1344                                 }
1345
1346                                 default: {
1347                                         base.WndProc (ref m);
1348                                         break;
1349                                 }
1350                         }
1351                 }
1352                 #endregion      // Protected Instance Methods
1353
1354                 #region Events
1355                 public event EventHandler Activated;
1356                 public event EventHandler Closed;
1357                 public event CancelEventHandler Closing;
1358                 public event EventHandler Deactivate;
1359                 public event InputLanguageChangedEventHandler InputLanguageChanged;
1360                 public event InputLanguageChangingEventHandler InputLanguageChanging;
1361                 public event EventHandler Load;
1362                 public event EventHandler MaximizedBoundsChanged;
1363                 public event EventHandler MaximumSizeChanged;
1364                 public event EventHandler MdiChildActivate;
1365                 public event EventHandler MenuComplete;
1366                 public event EventHandler MenuStart;
1367                 public event EventHandler MinimumSizeChanged;
1368
1369                 [Browsable(false)]
1370                 [EditorBrowsable(EditorBrowsableState.Never)]
1371                 public new event EventHandler TabIndexChanged;
1372                 #endregion      // Events
1373         }
1374 }