* Application.cs : new internal functions enterModalLoop, exitModalLoop
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / Form.cs
1     //
2     // System.Windows.Forms.Form
3     //
4     // Author:
5     //   Miguel de Icaza (miguel@ximian.com)
6     //   stubbed out by Daniel Carrera (dcarrera@math.toronto.edu)
7     //  Dennis Hayes (dennish@raytek.com)
8     //   WINELib implementation started by John Sohn (jsohn@columbus.rr.com)
9     //  Aleksey Ryabchuk (ryabchuk@yahoo.com)
10     // (C) 2002/3 Ximian, Inc
11     //
12     
13     using System;
14     using System.Drawing;
15     using System.ComponentModel;
16     using System.Collections;
17     
18     namespace System.Windows.Forms {
19     
20         public class Form : ContainerControl  {
21                         DialogResult dialogResult;
22                         Size maximumSize;
23                         Size minimizeSize;
24                         double opacity;
25                         bool   topLevel;
26                         // Temperary varibles that may be replaced
27                         // with win32 functions
28
29                         // owner draw 
30                         private bool controlBox;
31                         private bool minimizeBox;
32                         private bool maximizeBox;
33                         private bool helpButton;
34                         //end owner draw
35                         
36                         FormBorderStyle formBorderStyle;
37
38                         // End of temperay varibles
39
40                 internal class MdiClient : Control {
41                         public MdiClient ( Control parent ) : base (parent, "") {
42                         }
43                         protected override CreateParams CreateParams {
44                                 get {
45                                         CreateParams pars = new CreateParams();
46
47                                         pars.ClassName = Win32.MDICLIENTCLASSNAME;
48                                         pars.Style = (int) (    WindowStyles.WS_CHILDWINDOW |
49                                                                 WindowStyles.WS_CLIPCHILDREN |
50                                                                 WindowStyles.WS_CLIPSIBLINGS |
51                                                                 WindowStyles.WS_OVERLAPPED |
52                                                                 WindowStyles.WS_VISIBLE |
53                                                                 WindowStyles.WS_VSCROLL |
54                                                                 WindowStyles.WS_HSCROLL );
55                                         pars.ExStyle = (int) (  WindowExStyles.WS_EX_CLIENTEDGE );
56
57                                         pars.Parent = Parent.Handle;
58                                         CLIENTCREATESTRUCT cs = new CLIENTCREATESTRUCT();
59                                         cs.hWindowMenu = IntPtr.Zero;
60                                         cs.idFirstChild = 100;
61
62                                         pars.Param = cs;
63                                         
64                                         return pars;
65                                 }
66                         }
67                         public void DestroyControl ( ) {
68                                 DestroyHandle ( );
69                         }
70                 }
71
72                 MdiClient mdiClientWnd;
73                 Form      mdiParent;
74                 Control   dialog_owner;
75                 bool      modal;
76                 bool      exitModalLoop;
77
78                 public Form () : base ()
79                 {
80                         opacity = 0;
81                         topLevel = true;
82                         modal    = false;
83                         dialogResult = DialogResult.None;
84                 }
85                 
86                 static Form ()
87                 {
88
89                 }
90                 
91                 //  --- Public Properties
92                 //
93                 [MonoTODO]
94                 public IButtonControl AcceptButton {
95                         get {
96                                 throw new NotImplementedException ();
97                         }
98                         set {
99                                         //FIXME:
100                                 }
101                 }
102     
103                 [MonoTODO]
104                 public static Form ActiveForm {
105                         get {
106                                 throw new NotImplementedException ();
107                         }
108                 }
109     
110                 [MonoTODO]
111                 public Form ActiveMdiChild {
112                         get {
113                                 throw new NotImplementedException ();
114                         }
115                 }
116     
117                 [MonoTODO]
118                 public bool AutoScale {
119                         get {
120                                 throw new NotImplementedException ();
121                         }
122                         set {
123                                         //FIXME:
124                                 }
125                 }
126                         internal Size autoscalebasesize; //debug/test only
127
128                 [MonoTODO]
129                 public virtual Size AutoScaleBaseSize {
130                         get {
131                                 return autoscalebasesize;
132                         }
133                         set {
134                                 autoscalebasesize = value;
135                         }
136                 }
137     
138                 public override bool AutoScroll {
139                         get {
140                                 return base.AutoScroll;
141                         }
142                         set {
143                                 base.AutoScroll = value;
144                         }
145                 }
146     
147                 public override Color BackColor {
148                         get {
149                                 return base.BackColor;
150                         }
151                         set {
152                                 base.BackColor = value;
153                         }
154                 }
155     
156                 [MonoTODO]
157                 public IButtonControl CancelButton {
158                         get {
159                                 throw new NotImplementedException ();
160                         }
161                         set {
162                                         //FIXME:
163                                 }
164                 }
165     
166                 [MonoTODO]
167                 public new Size ClientSize {
168                         get {
169                                 return base.ClientSize;
170                         }
171                         set {
172                                 base.ClientSize = value;
173                         }
174                 }
175     
176                         //Compact Framework
177                         //FIXME:
178                         // In .NET this can be changed at any time.
179                         // In WIN32 this is fixed when the window is created.
180                         // In WIN32 to change this after the window is created,
181                         // like in .NET, we must draw the caption bar our self.
182                         // In the mean time, just set/return a bool.
183                         // This might be the start of the drawing
184                 [MonoTODO]
185                 public bool ControlBox {
186                         get {
187                                 return controlBox;
188                         }
189                         set {
190                                 controlBox = value;
191                                         //force paint
192                         }
193                 }
194     
195                 [MonoTODO]
196                 public Rectangle DesktopBounds {
197                         get {
198                                 throw new NotImplementedException ();
199                         }
200                         set {
201                                         //FIXME:
202                                 }
203                 }
204     
205                 [MonoTODO]
206                 public Point DesktopLocation {
207                         get {
208                                 throw new NotImplementedException ();
209                         }
210                         set {
211                                         //FIXME:
212                                 }
213                 }
214     
215                         //Compact Framework
216                 [MonoTODO]
217                 public DialogResult DialogResult {
218                         get {
219                                 return dialogResult;
220                         }
221                         set {
222                                 if ( !Enum.IsDefined ( typeof(DialogResult), value ) )
223                                         throw new InvalidEnumArgumentException( "DialogResult",
224                                                 (int)value,
225                                                 typeof(DialogResult));
226
227                                 dialogResult = value;
228                                 if ( Modal && dialogResult != DialogResult.None ) {
229                                         Application.exitModalLoop ( this );
230                                 }
231                         }
232                 }
233     
234                         //Compact Framework
235                 [MonoTODO]
236                 public FormBorderStyle FormBorderStyle {
237                         get {
238                                 return formBorderStyle;
239                         }
240                         set {
241                                 formBorderStyle = value;
242                         }
243                 }
244     
245                 [MonoTODO]
246                 public bool HelpButton {
247                         get {
248                                 return helpButton;
249                         }
250                         set {
251                                 helpButton = value;
252                         }
253                 }
254     
255                         //Compact Framework
256                         //[MonoTODO]
257                 public Icon Icon {
258                          get {
259                                  throw new NotImplementedException ();
260                          }
261                          set {
262                                          //FIXME:
263                                  }
264                 }
265
266                 public bool IsMdiChild {
267                         get {   return mdiParent != null; }
268                 }
269
270                 [MonoTODO]
271                 public bool KeyPreview {
272                         get {
273                                 throw new NotImplementedException ();
274                         }
275                         set {
276                                         //FIXME:
277                                 }
278                 }
279     
280                         //Compact Framework
281                 [MonoTODO]
282                 public bool MaximizeBox {
283                         get {
284                                 return maximizeBox;
285                         }
286                         set {
287                                 maximizeBox = value;
288                         }
289                 }
290     
291                 [MonoTODO]
292                 public Size MaximumSize {
293                         get {
294                                 return maximumSize;
295                         }
296                         set {
297                                 maximumSize = value;
298                         }
299                 }
300     
301                 [MonoTODO]
302                 public Form[] MdiChildren {
303                         get {
304                                 Form[] forms = new Form[0];
305                                 return forms;
306                         }
307                 }
308     
309                 [MonoTODO]
310                 public Form MdiParent {
311                         get {
312                                 return mdiParent;
313                         }
314                         set {
315                                 if ( !value.IsMdiContainer || ( value.IsMdiContainer && value.IsMdiChild ) )
316                                         throw new Exception( );
317
318                                 mdiParent = value;
319                                 mdiParent.MdiClientControl.Controls.Add ( this );
320                                 
321                                 if ( mdiParent.IsHandleCreated )
322                                         CreateControl ( );
323                         }
324                 }
325     
326                         //Compact Framework
327                         //[MonoTODO]
328                                 private MainMenu mainMenu_ = null;
329
330                                 private void assignMenu()
331                                 {
332                                         if ( mainMenu_ != null )
333                                                 mainMenu_.setForm ( this );
334
335                                         if( IsHandleCreated ) {
336                                         // FIXME: If Form's window has no style for menu,  probably, better to add it
337                                         // if menu have to be removed, remove the style.
338                                         // Attention to the repainting.
339                                                 if( mainMenu_ != null) {
340 //                                                      //long myStyle = Win32.GetWindowLongA( Handle, Win32.GWL_STYLE);
341 //                                                      //myStyle |= (long)Win32.WS_OVERLAPPEDWINDOW;
342 //                                                      //Win32.SetWindowLongA( Handle, Win32.GWL_STYLE, myStyle);
343                                                         int res = Win32.SetMenu( Handle, mainMenu_.Handle);
344                                                         Console.WriteLine ("Form.assignMenu. result {0}", res);
345                                                 }
346                                                 else {
347                                                         Win32.SetMenu( Handle, IntPtr.Zero);
348                                                 }
349                                         }
350                                 }
351
352                 public MainMenu Menu {
353                         get {
354                                 return mainMenu_;
355                         }
356                         set {
357                                 mainMenu_ = value;
358                                 assignMenu();
359                                 // update size of the form                              
360                                 ClientSize = ClientSize;
361                         }
362                 }
363
364                 //[MonoTODO]
365                 //public MainMenu MergedMenu {
366                 //      get {
367                 //              throw new NotImplementedException ();
368                 //      }
369                 //}
370     
371                         //Compact Framework
372                 [MonoTODO]
373                 public bool MinimizeBox {
374                         get {
375                                 return minimizeBox;
376                         }
377                         set {
378                                 minimizeBox = value;
379                         }
380                 }
381     
382                 [MonoTODO]
383                 public Size MinimumSize {
384                         get {
385                                 return maximumSize;
386                         }
387                         set {
388                                 maximumSize = value;
389                         }
390                 }
391     
392                 public bool Modal {
393                         get {   return modal;   }
394                 }
395     
396                 [MonoTODO]
397                 public double Opacity {
398                         get {
399                                 return opacity;
400                         }
401                         set {
402                                 opacity = value;
403                         }
404                 }
405
406                 [MonoTODO]
407                 public bool IsMdiContainer {
408                         get {   return mdiClientWnd != null; }
409                         set {
410                                 if ( value )
411                                         createMdiClient ( );
412                                 else
413                                         destroyMdiClient ( );
414                         }
415                 }
416
417                 [MonoTODO]
418                 public Form[] OwnedForms {
419                         get {
420                                 throw new NotImplementedException ();
421                         }
422                 }
423     
424                 [MonoTODO]
425                 public Form Owner {
426                         get {
427                                 throw new NotImplementedException ();
428                         }
429                         set {
430                                         //FIXME:
431                                 }
432                 }
433     
434                 [MonoTODO]
435                 public bool ShowInTaskbar {
436                         get {
437                                 throw new NotImplementedException ();
438                         }
439                         set {
440                                         //FIXME:
441                                 }
442                 }
443     
444     
445                 public override ISite Site {
446                         get {
447                                 return base.Site;
448                         }
449                         set {
450                                 base.Site = value;
451                         }
452                 }
453     
454                 [MonoTODO]
455                 public SizeGripStyle SizeGripStyle {
456                         get {
457                                 throw new NotImplementedException ();
458                         }
459                         set {
460                                         //FIXME:
461                                 }
462                 }
463     
464                 [MonoTODO]
465                 public FormStartPosition StartPosition {
466                         get {
467                                 throw new NotImplementedException ();
468                         }
469                         set {
470                                         //FIXME:
471                                 }
472                 }
473     
474                 [MonoTODO]
475                 public bool TopLevel {
476                         get {
477                                 return topLevel;
478                         }
479                         set {
480                                 topLevel = value;
481                         }
482                 }
483     
484                 [MonoTODO]
485                 public bool TopMost {
486                         get {
487                                 throw new NotImplementedException ();
488                         }
489                         set {
490                                         //FIXME:
491                                 }
492                 }
493     
494                 [MonoTODO]
495                 public Color TransparencyKey {
496                         get {
497                                 throw new NotImplementedException ();
498                         }
499                         set {
500                                         //FIXME:
501                                 }
502                 }
503     
504     
505                         //Compact Framework
506                 [MonoTODO]
507                 public FormWindowState WindowState {
508                         get {
509                                         WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
510
511                                 //bool ReturnValue = Win32.GetWindowPlacement(Handle, ref placement ) ;
512                                         //if(placement.showCmd == SW_MINIMIZE){
513                                         //      return FormWindowState.Minimized;
514                                         //}
515                                         //if(placement.showCmd == SW_MAXIMIZE){
516                                         //      return FormWindowState.Maximized;
517                                         //}
518                                         return FormWindowState.Normal;
519                                         //Other options such as hide are possible in win32, but not in this part of .NET
520                                         // also this may not work because it looks like showCmd is for setting, and might not be set
521                                         // by win32 in a get.
522                                 }
523                         set {
524                                         //FIXME:
525                                 }
526                 }
527     
528                 
529                 //  --- Public Methods
530                 public void Activate ()
531                 {
532                         Win32.SetActiveWindow (Handle);
533                 }
534     
535                 [MonoTODO]
536                 public void AddOwnedForm (Form ownedForm)
537                 {
538                                 //FIXME:
539                         }
540     
541                         //Compact Framework
542                 public void Close ()
543                 {
544                         //Win32.DestroyWindow (Handle);
545                         if ( IsHandleCreated )
546                                 Win32.SendMessage ( Handle, Msg.WM_CLOSE, 0, 0 );
547                 }
548     
549                 public void LayoutMdi (MdiLayout value)
550                 {
551                         if ( IsMdiContainer && mdiClientWnd.IsHandleCreated ) {
552                                 int mes = 0;
553                                 int wp  = 0;
554
555                                 switch ( value ) {
556                                 case MdiLayout.Cascade:
557                                         mes = (int)Msg.WM_MDICASCADE;
558                                 break;
559                                 case MdiLayout.ArrangeIcons:
560                                         mes = (int)Msg.WM_MDIICONARRANGE;
561                                 break;
562                                 case MdiLayout.TileHorizontal:
563                                         mes = (int)Msg.WM_MDITILE;
564                                         wp = 1;
565                                 break;
566                                 case MdiLayout.TileVertical:
567                                         mes = (int)Msg.WM_MDITILE;
568                                 break;
569                                 }
570                                 
571                                 if ( mes != 0 )
572                                         Win32.SendMessage ( mdiClientWnd.Handle, mes, wp, 0 );
573                         }
574                 }
575     
576                 [MonoTODO]
577                 public void RemoveOwnedForm (Form ownedForm)
578                 {
579                                 //FIXME:
580                         }
581     
582      
583                 public void SetDesktopLocation (int x, int y)
584                 {
585                         Win32.SetWindowPos ((IntPtr) Handle, SetWindowPosZOrder.HWND_TOPMOST, 
586                                             x, y, 0, 0, 
587                                             SetWindowPosFlags.SWP_NOSIZE | 
588                                             SetWindowPosFlags.SWP_NOZORDER);
589                 }
590     
591     
592                 [MonoTODO]
593                 public DialogResult ShowDialog ()
594                 {
595                         Control owner = Control.getOwnerWindow ( this );
596                         Control oldOwner = dlgOwner;
597                         dlgOwner = owner;
598
599                          // needs to be recreated because ownership can't be changed
600                         if ( IsHandleCreated ) {
601                                 if ( oldOwner != owner )
602                                         RecreateHandle();
603                         }
604                         else
605                                 CreateControl ( );
606
607                         if ( owner != null )
608                                 owner.Enabled = false;
609
610                         Show ( );
611                         
612                         modal = true;
613                         Application.enterModalLoop ( this );
614                         modal = false;
615
616                         if ( owner != null ) {
617                                 owner.Enabled = true;
618                                 Win32.SetFocus ( owner.Handle );
619                         }
620                         Hide ( );
621
622                         return DialogResult;
623                 }
624     
625                         //Compact Framework
626                 [MonoTODO]
627                 public override string ToString ()
628                 {
629                                 //FIXME:
630                                 return base.ToString();
631                 }
632     
633                 //  --- Public Events
634                 
635                 public event EventHandler Activated;
636                 
637                 public event EventHandler Closed;
638                 public event CancelEventHandler Closing;
639                  
640                         //Compact Framework
641                 // CancelEventHandler not yet implemented/stubbed
642                 //public event CancelEventHandler Closing;
643                 
644                 public event EventHandler Deactivate;
645                 public event InputLanguageChangedEventHandler InputLanguageChanged;
646                 public event InputLanguageChangingEventHandler InputLanguageChanging;
647
648                         //Compact Framework
649                 public event EventHandler  Load;
650                 
651                         public event EventHandler  MaximizedBoundsChanged;
652                 public event EventHandler MaximumSizeChanged;
653                 public event EventHandler  MdiChildActivate;
654                 public event EventHandler  MenuComplete;
655                 public event EventHandler  MenuStart;
656                 public event EventHandler  MinimumSizedChanged;
657     
658                 
659                 //  --- Protected Properties
660                 
661                 protected override CreateParams CreateParams {
662                         get {
663                                 CreateParams pars = base.CreateParams;
664                         
665                                 if ( IsMdiChild ) {
666                                         pars.Style |= (int)( WindowStyles.WS_CHILD | WindowStyles.WS_VISIBLE );
667                                         pars.ExStyle |= (int)WindowExStyles.WS_EX_MDICHILD;
668                                 }
669                                 else {
670                                         pars.X = (int)CreateWindowCoordinates.CW_USEDEFAULT;
671                                         pars.Y = (int)CreateWindowCoordinates.CW_USEDEFAULT;
672
673                                         pars.Style = (int)( WindowStyles.WS_OVERLAPPEDWINDOW | 
674                                                         WindowStyles.WS_CLIPSIBLINGS /* |
675                                                         WindowStyles.WS_CLIPCHILDREN */);
676                                 }
677                                 if ( TopLevel && Parent == null ) 
678                                         pars.Parent = IntPtr.Zero;
679                                 
680                                 // this property is used for modal dialogs
681                                 if ( dlgOwner != null )
682                                         pars.Parent = dlgOwner.Handle;
683                                 
684                                 // should have WS_CLIPCHILDREN style but there are
685                                 // problems with GroupBox at the moment
686
687                                 return pars;
688                         }
689                 }
690     
691                 protected override bool MenuPresent {
692                         get { return mainMenu_ != null; }
693                 }
694
695                 protected override ImeMode DefaultImeMode {
696                         get {
697                                 return base.DefaultImeMode;
698                         }
699                 }
700     
701                 protected override Size DefaultSize {
702                                 get {
703                                         return new Size(300,300);
704                                 }
705                         }
706     
707                 [MonoTODO]
708                         public new Size Size {
709                                 get {
710                                         return base.Size;
711                                 }
712                                 set {
713                                         base.Size = value;
714                                 }
715                         }
716  
717                         [MonoTODO]
718                 protected Rectangle MaximizedBounds {
719                         get {
720                                 throw new NotImplementedException ();
721                         }
722                         set {
723                                         //FIXME:
724                                 }
725                 }
726     
727                 
728                 //  --- Protected Methods
729                 
730                 protected override void AdjustFormScrollbars (
731                         bool displayScrollbars)
732                 {
733                         base.AdjustFormScrollbars (displayScrollbars);
734                 }
735     
736                 protected override Control.ControlCollection 
737                 CreateControlsInstance ()
738                 {
739                         return base.CreateControlsInstance ();
740                 }
741     
742                 protected override void CreateHandle ()
743                 {
744                         base.CreateHandle ();
745                 }
746     
747                 protected override void DefWndProc (ref Message m)
748                 {
749                         if ( IsMdiChild )
750                                 window.DefMDIChildProc ( ref m );
751                         else if ( IsMdiContainer && mdiClientWnd.IsHandleCreated ) {
752                                 if ( m.Msg != Msg.WM_SIZE )
753                                         window.DefFrameProc ( ref m, mdiClientWnd );
754                         }
755                         else
756                                 window.DefWndProc (ref m);
757                 }
758
759                         //Compact Framework
760                 protected virtual void OnClosed (EventArgs e)
761                 {
762                         if (Closed != null)
763                                 Closed (this, e);
764                 }
765     
766                         //Compact Framework
767                 protected virtual void  OnClosing(CancelEventArgs e)
768                 {
769                         if ( Closing != null )
770                                 Closing ( this, e);
771
772                         if ( Modal ) {
773                                 e.Cancel = true; // don't destroy modal form
774                                 DialogResult = DialogResult.Cancel;
775                         }
776                 }
777     
778                 protected override void OnCreateControl ()
779                 {
780                         OnLoad ( EventArgs.Empty );
781                         base.OnCreateControl ();
782                 }
783     
784                 protected override void OnFontChanged (EventArgs e)
785                 {
786                         base.OnFontChanged (e);
787                 }
788     
789                 protected override void OnHandleCreated (EventArgs e)
790                 {
791                         base.OnHandleCreated (e);
792                         if ( IsMdiChild ) 
793                                 activateMdiChild ( );
794                         assignMenu();
795                 }
796     
797                 protected override void OnHandleDestroyed (EventArgs e)
798                 {
799                         base.OnHandleDestroyed (e);
800                 }
801     
802                 protected virtual void OnInputLanguageChanged (
803                         InputLanguageChangedEventArgs e)
804                 {
805                         if (InputLanguageChanged != null)
806                                 InputLanguageChanged (this, e);
807                 }
808     
809                 protected virtual void OnInputLanguagedChanging (
810                         InputLanguageChangingEventArgs e)
811                 {
812                         if (InputLanguageChanging != null)
813                                 InputLanguageChanging (this, e);
814                 }
815     
816                         //Compact Framework
817                 protected virtual void OnLoad (EventArgs e)
818                 {
819                         if (Load != null)
820                                 Load (this, e);
821                 }
822     
823                 protected virtual void OnMaximizedBoundsChanged (EventArgs e)
824                 {
825                         if (MaximizedBoundsChanged != null)
826                                 MaximizedBoundsChanged (this, e);
827                 }
828     
829                 protected virtual void OnMaximumSizeChanged (EventArgs e)
830                 {
831                         if (MaximumSizeChanged != null)
832                                 MaximumSizeChanged (this, e);
833                 }
834     
835                 protected virtual void OnMdiChildActivate (EventArgs e)
836                 {
837                         if (MdiChildActivate != null)
838                                 MdiChildActivate (this, e);
839                 }
840     
841                 protected virtual void OnMenuComplete (EventArgs e)
842                 {
843                         if (MenuComplete != null)
844                                 MenuComplete (this, e);
845                 }
846     
847                 protected virtual void OnMenuStart (EventArgs e)
848                 {
849                         if (MenuStart != null)
850                                 MenuStart (this, e);
851                 }
852     
853                 protected virtual void OnMinimumSizeChanged (EventArgs e)
854                 {
855                                 //FIXME:
856                         }
857     
858                         //Compact Framework
859                 protected override void  OnPaint (PaintEventArgs e)
860                 {
861                         base.OnPaint (e);
862                 }
863     
864                         //Compact Framework
865                 protected override void  OnResize (EventArgs e)
866                 {
867                         base.OnResize (e);
868 //                      resizeMdiClient ();
869                 }
870     
871                 protected override void  OnStyleChanged (EventArgs e)
872                 {
873                         base.OnStyleChanged (e);
874                 }
875     
876                         //Compact Framework
877                 protected override void  OnTextChanged (EventArgs e)
878                 {
879                         base.OnTextChanged (e);
880                 }
881     
882                 protected override void  OnVisibleChanged (EventArgs e)
883                 {
884                         base.OnVisibleChanged (e);
885                 }
886
887                 protected virtual IntPtr OnMenuCommand (uint id)
888                 {
889                                 IntPtr result = (IntPtr)1;
890                                 System.Console.WriteLine("Form on command {0}", id);
891                                 if(Menu != null) {
892                                         MenuItem mi = Menu.GetMenuItemByID( id);
893                                         if( mi != null) {
894                                                 mi.PerformClick();
895                                                 result = IntPtr.Zero;
896                                         }
897                                 }
898                                 return result;
899                 }
900
901                         protected override void OnWmCommand (ref Message m)
902                         {
903                                 int wNotifyCode = (int)m.HiWordWParam;
904                                 int wID = (int)m.LoWordWParam;
905
906                                 if( m.LParam.ToInt32() == 0) {
907                                         if( wNotifyCode == 0) {
908                                                 // Menu
909                                                 m.Result = OnMenuCommand( (uint)wID);
910                                         }
911                                         else if( wNotifyCode == 1) {
912                                                 // Accelerator
913                                                 m.Result = (IntPtr)1;
914                                         }
915                                         else {
916                                                 // just pass it to DefWindowProc
917                                                 m.Result = (IntPtr)1;
918                                         }
919                                 }
920                                 else {
921                                         if ( IsMdiContainer && m.LParam != IntPtr.Zero ) {
922                                                 // we need to pass unhandled commands
923                                                 // to DefFrameProc
924                                                 m.Result = (IntPtr)1;
925                                                 return;
926                                         }
927                                         base.OnWmCommand(ref m);
928                                 }
929                         }
930
931                         protected override bool ProcessCmdKey ( ref Message msg, Keys keyData)
932                 {
933                         return base.ProcessCmdKey (ref msg, keyData);
934                 }
935     
936                 protected override bool ProcessDialogKey (Keys keyData)
937                 {
938                         return base.ProcessDialogKey (keyData);
939                 }
940     
941                 protected override bool ProcessKeyPreview (ref Message m)
942                 {
943                         return base.ProcessKeyPreview (ref m);
944                 }
945
946                 protected override bool ProcessTabKey (bool forward)
947                 {
948                         return base.ProcessTabKey (forward);
949                 }
950     
951                 protected override void ScaleCore (float x, float y)
952                 {
953                         ClientSize = new Size ( (int) ( ClientSize.Width * x ), (int) ( ClientSize.Height * y) );
954                 }
955     
956                 protected override void SetBoundsCore (
957                         int x, int y,  int width, int height,  
958                         BoundsSpecified specified)
959                 {
960                         base.SetBoundsCore (x, y, width, height, specified);
961                 }
962     
963                 protected override void SetClientSizeCore (int x, int y)
964                 {
965                         base.SetClientSizeCore (x, y);
966                 }
967     
968                 protected override void SetVisibleCore (bool value)
969                 {
970                         base.SetVisibleCore (value);
971                 }
972
973                 protected override void WndProc (ref Message m)
974                 {
975                         switch (m.Msg) {
976                         case Msg.WM_CLOSE:
977                                 CancelEventArgs args = new CancelEventArgs( false );
978                                 OnClosing( args );
979                                 if ( !args.Cancel ) {
980                                         OnClosed ( EventArgs.Empty );
981                                         base.WndProc ( ref m );
982                                 }
983                                 break;
984                                 //case ?:
985                                 //OnCreateControl()
986                                 //break;
987                         case Msg.WM_FONTCHANGE:
988                                 EventArgs fontChangedArgs = new EventArgs();
989                                 OnFontChanged (fontChangedArgs);
990                                 break;
991                         case Msg.WM_CREATE:
992                                 EventArgs handleCreatedArgs = new EventArgs(); 
993                                 OnHandleCreated (handleCreatedArgs);
994                                 break;
995                         case Msg.WM_DESTROY:
996                                 EventArgs destroyArgs = new EventArgs();
997                                 OnHandleDestroyed (destroyArgs);
998                                 break;
999                         case Msg.WM_INPUTLANGCHANGE:
1000                                 //InputLanguageChangedEventArgs ilChangedArgs =
1001                                 //      new InputLanguageChangedEventArgs();
1002                                 //OnInputLanguageChanged (ilChangedArgs);
1003                                 break;
1004                         case Msg.WM_INPUTLANGCHANGEREQUEST:
1005                                 //InputLanguageChangingEventArgs ilChangingArgs =
1006                                 //      new InputLanguageChangingEventArgs();
1007                                 //OnInputLanguagedChanging (ilChangingArgs);
1008                                 break;
1009                                 /*
1010                                   case Win32.WM_SHOWWINDOW:
1011                                   EventArgs e;
1012                                   OnLoad (e);
1013                                   break;
1014                                 */
1015                                 // case ?:
1016                                 // OnMaximizedBoundsChanged(EventArgs e)
1017                                 // break;
1018                                 // case ?:
1019                                 // OnMaximumSizedChanged(EventArgs e)
1020                                 //break;
1021                         case Msg.WM_MDIACTIVATE:
1022                                 EventArgs mdiActivateArgs = new EventArgs();
1023                                 OnMdiChildActivate (mdiActivateArgs);
1024                                 base.WndProc ( ref m );
1025                                 break;
1026                         case Msg.WM_EXITMENULOOP:
1027                                 EventArgs menuCompleteArgs = new EventArgs();
1028                                 OnMenuComplete (menuCompleteArgs);
1029                                 break;
1030                         case Msg.WM_ENTERMENULOOP:
1031                                 EventArgs enterMenuLoopArgs = new EventArgs();
1032                                 OnMenuStart (enterMenuLoopArgs);
1033                                 break;
1034                                 // case ?:
1035                                 // OnMinimumSizeChanged(EventArgs e)
1036                                 // break;
1037 /*
1038                                 case Msg.WM_PAINT: {
1039                                         Rectangle rect = new Rectangle();
1040                                         PaintEventArgs paintArgs = new PaintEventArgs(CreateGraphics(), rect);
1041                                         OnPaint (paintArgs);
1042                                         paintArgs.Dispose();
1043                                         }
1044                                                 break;
1045                         case Msg.WM_SIZE:
1046                                 EventArgs resizeArgs = new EventArgs();
1047                                 OnResize (resizeArgs);
1048                                 break;
1049                                 //case ?:
1050                                 //OnStyleChanged(EventArgs e)
1051                                 //break;
1052                         case Msg.WM_SETTEXT:
1053                                 EventArgs textChangedArgs = new EventArgs();
1054                                 OnTextChanged (textChangedArgs);
1055                                 break;
1056                         case Msg.WM_SHOWWINDOW:
1057                                 EventArgs visibleChangedArgs = new EventArgs();
1058                                 OnVisibleChanged (visibleChangedArgs);
1059                                 break;
1060 */                                      
1061                                 case Msg.WM_INITMENU:
1062                                         OnWmInitMenu (ref m);
1063                                         break;
1064                                 case Msg.WM_INITMENUPOPUP:
1065                                         OnWmInitMenuPopup (ref m);
1066                                         break;
1067                                 case Msg.WM_CTLCOLORLISTBOX:
1068                                         Control.ReflectMessage( m.LParam, ref m);
1069                                         break;
1070                                 default:
1071                                         base.WndProc (ref m);
1072                                         break;    
1073                                 }
1074                 }
1075                 
1076                         #region new 11.26.2002 from Alexandre Pigolkine (pigolkine@gmx.de)
1077                         protected virtual void OnWmInitMenu (ref Message m) {
1078                                 Menu mn = System.Windows.Forms.Menu.GetMenuByHandle( m.WParam);
1079                                 if( mn != null) {
1080                                         mn.OnWmInitMenu();
1081                                 }
1082                         }
1083
1084                         protected virtual void OnWmInitMenuPopup (ref Message m) {
1085                                 Menu mn = System.Windows.Forms.Menu.GetMenuByHandle( m.WParam);
1086                                 if( mn != null) {
1087                                         mn.OnWmInitMenuPopup();
1088                                 }
1089                         }
1090                         #endregion
1091                         
1092                 private void createMdiClient ( ) {
1093                         if(  mdiClientWnd == null ) {
1094                                 mdiClientWnd = new MdiClient ( this );
1095                                 Controls.Add ( mdiClientWnd );
1096                                 mdiClientWnd.Dock = DockStyle.Fill;
1097                                 if ( IsHandleCreated ) {
1098                                         mdiClientWnd.CreateControl ( );
1099                                         if ( Menu != null ) {
1100                                                 MenuItem mdiListItem = Menu.MdiListItem;
1101                                                 if ( mdiListItem != null ) 
1102                                                         replaceMdiWindowMenu ( mdiListItem.Handle );
1103                                         } 
1104                                 }
1105                         }
1106                 }
1107
1108                 private void destroyMdiClient ( ) {
1109                         if ( mdiClientWnd != null ) {
1110                                 Controls.Remove ( mdiClientWnd );
1111                                 mdiClientWnd.DestroyControl ( );
1112                                 mdiClientWnd = null;
1113                         }
1114                 }
1115                 private void resizeMdiClient ( ) {
1116                         if ( IsMdiContainer && mdiClientWnd.IsHandleCreated ) {
1117                                 Win32.MoveWindow ( mdiClientWnd.Handle,
1118                                         Location.X, Location.Y,
1119                                         ClientSize.Width,
1120                                         ClientSize.Height, true );
1121                         }
1122                 }
1123
1124                 private void activateMdiChild ( ) {
1125                         Win32.SendMessage ( Parent.Handle, Msg.WM_MDIACTIVATE, Handle.ToInt32(), 0 );
1126                 }
1127
1128                 internal Control MdiClientControl {
1129                         get { return this.mdiClientWnd; }
1130                 }
1131
1132                 internal void replaceMdiWindowMenu ( IntPtr hMenu ) {
1133                         Control mdiClient = MdiClientControl;
1134                         if ( mdiClient != null && mdiClient.Handle != IntPtr.Zero )
1135                                 if ( hMenu != IntPtr.Zero )
1136                                         Win32.SendMessage ( mdiClient.Handle, Msg.WM_MDISETMENU, 0, hMenu.ToInt32 ( ) );
1137                                 else
1138                                         Win32.SendMessage ( mdiClient.Handle, Msg.WM_MDISETMENU, 1, 0 );
1139                                         // this probably won't work on Wine anyway
1140                                         // because such behaviour is not impl. there
1141
1142                         if ( IsHandleCreated )
1143                                 Win32.DrawMenuBar ( Handle );
1144                                 
1145                 }
1146
1147                 internal Control dlgOwner {
1148                         get { return dialog_owner; }
1149                         set { dialog_owner = value;}
1150                 }
1151
1152                 internal bool ExitModalLoop {
1153                         get { return exitModalLoop; }
1154                         set { exitModalLoop = value; }
1155                 }
1156
1157                 //sub class
1158                 //System.Windows.Forms.Form.ControlCollection.cs
1159                 //
1160                 //Author:
1161                 //  stubbed out by Daniel Carrera (dcarrera@math.toronto.edu)
1162                 //
1163                 // (C) 2002 Ximian, Inc
1164                 //
1165                 //
1166                 // <summary>
1167                 //      This is only a template.  Nothing is implemented yet.
1168                 //
1169                 // </summary>
1170                 // TODO: implement support classes and derive from 
1171                 // proper classes
1172                 // FIXME: use this or the one defined on Control?
1173                         public class  ControlCollectionX : 
1174                         System.Windows.Forms.Control.ControlCollection 
1175                         /*,ICollection*/ {
1176     
1177                         //  --- Constructor
1178                         // base class not defined (yet!)
1179                         public ControlCollectionX (Form owner) : base(owner) {
1180     
1181                         }
1182                 
1183                         //  --- Public Methods
1184     
1185                         // TODO: see what causes this compile error
1186                         public override void Add(Control value) {
1187                                 base.Add (value);
1188                         }
1189     
1190                         public override bool Equals (object obj) {
1191                                         //FIXME:
1192                                         return base.Equals(obj);
1193                         }
1194
1195                         public override int GetHashCode () {
1196                                 //FIXME add our proprities
1197                                 return base.GetHashCode ();
1198                         }
1199     
1200                         public override void Remove(Control value) {
1201                                 base.Remove (value);
1202                         }
1203                 } // end of Subclass
1204         }
1205     }