2 // System.Windows.Forms.Form
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
15 using System.ComponentModel;
16 using System.Collections;
18 namespace System.Windows.Forms {
20 public class Form : ContainerControl {
21 DialogResult dialogResult;
26 // Temperary varibles that may be replaced
27 // with win32 functions
30 private bool controlBox;
31 private bool minimizeBox;
32 private bool maximizeBox;
33 private bool helpButton;
36 FormBorderStyle formBorderStyle;
38 // End of temperay varibles
40 internal class MdiClient : Control {
41 public MdiClient ( Control parent ) : base (parent, "") {
43 protected override CreateParams CreateParams {
45 CreateParams pars = new CreateParams();
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 );
57 pars.Parent = Parent.Handle;
58 CLIENTCREATESTRUCT cs = new CLIENTCREATESTRUCT();
59 cs.hWindowMenu = IntPtr.Zero;
60 cs.idFirstChild = 100;
67 public void DestroyControl ( ) {
72 MdiClient mdiClientWnd;
78 public Form () : base ()
83 dialogResult = DialogResult.None;
91 // --- Public Properties
94 public IButtonControl AcceptButton {
96 throw new NotImplementedException ();
104 public static Form ActiveForm {
106 throw new NotImplementedException ();
111 public Form ActiveMdiChild {
113 throw new NotImplementedException ();
118 public bool AutoScale {
120 throw new NotImplementedException ();
126 internal Size autoscalebasesize; //debug/test only
129 public virtual Size AutoScaleBaseSize {
131 return autoscalebasesize;
134 autoscalebasesize = value;
138 public override bool AutoScroll {
140 return base.AutoScroll;
143 base.AutoScroll = value;
147 public override Color BackColor {
149 return base.BackColor;
152 base.BackColor = value;
157 public IButtonControl CancelButton {
159 throw new NotImplementedException ();
167 public new Size ClientSize {
169 return base.ClientSize;
172 base.ClientSize = value;
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
185 public bool ControlBox {
196 public Rectangle DesktopBounds {
198 throw new NotImplementedException ();
206 public Point DesktopLocation {
208 throw new NotImplementedException ();
217 public DialogResult DialogResult {
222 if ( !Enum.IsDefined ( typeof(DialogResult), value ) )
223 throw new InvalidEnumArgumentException( "DialogResult",
225 typeof(DialogResult));
227 dialogResult = value;
228 if ( Modal && dialogResult != DialogResult.None ) {
229 Application.exitModalLoop ( this );
236 public FormBorderStyle FormBorderStyle {
238 return formBorderStyle;
241 formBorderStyle = value;
246 public bool HelpButton {
259 throw new NotImplementedException ();
266 public bool IsMdiChild {
267 get { return mdiParent != null; }
271 public bool KeyPreview {
273 throw new NotImplementedException ();
282 public bool MaximizeBox {
292 public Size MaximumSize {
302 public Form[] MdiChildren {
304 Form[] forms = new Form[0];
310 public Form MdiParent {
315 if ( !value.IsMdiContainer || ( value.IsMdiContainer && value.IsMdiChild ) )
316 throw new Exception( );
319 mdiParent.MdiClientControl.Controls.Add ( this );
321 if ( mdiParent.IsHandleCreated )
328 private MainMenu mainMenu_ = null;
330 private void assignMenu()
332 if ( mainMenu_ != null )
333 mainMenu_.setForm ( this );
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);
347 Win32.SetMenu( Handle, IntPtr.Zero);
352 public MainMenu Menu {
359 // update size of the form
360 ClientSize = ClientSize;
365 //public MainMenu MergedMenu {
367 // throw new NotImplementedException ();
373 public bool MinimizeBox {
383 public Size MinimumSize {
393 get { return modal; }
397 public double Opacity {
407 public bool IsMdiContainer {
408 get { return mdiClientWnd != null; }
413 destroyMdiClient ( );
418 public Form[] OwnedForms {
420 throw new NotImplementedException ();
427 throw new NotImplementedException ();
435 public bool ShowInTaskbar {
437 throw new NotImplementedException ();
445 public override ISite Site {
455 public SizeGripStyle SizeGripStyle {
457 throw new NotImplementedException ();
465 public FormStartPosition StartPosition {
467 throw new NotImplementedException ();
475 public bool TopLevel {
485 public bool TopMost {
487 throw new NotImplementedException ();
495 public Color TransparencyKey {
497 throw new NotImplementedException ();
507 public FormWindowState WindowState {
509 WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
511 //bool ReturnValue = Win32.GetWindowPlacement(Handle, ref placement ) ;
512 //if(placement.showCmd == SW_MINIMIZE){
513 // return FormWindowState.Minimized;
515 //if(placement.showCmd == SW_MAXIMIZE){
516 // return FormWindowState.Maximized;
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.
529 // --- Public Methods
530 public void Activate ()
532 Win32.SetActiveWindow (Handle);
536 public void AddOwnedForm (Form ownedForm)
544 //Win32.DestroyWindow (Handle);
545 if ( IsHandleCreated )
546 Win32.SendMessage ( Handle, Msg.WM_CLOSE, 0, 0 );
549 public void LayoutMdi (MdiLayout value)
551 if ( IsMdiContainer && mdiClientWnd.IsHandleCreated ) {
556 case MdiLayout.Cascade:
557 mes = (int)Msg.WM_MDICASCADE;
559 case MdiLayout.ArrangeIcons:
560 mes = (int)Msg.WM_MDIICONARRANGE;
562 case MdiLayout.TileHorizontal:
563 mes = (int)Msg.WM_MDITILE;
566 case MdiLayout.TileVertical:
567 mes = (int)Msg.WM_MDITILE;
572 Win32.SendMessage ( mdiClientWnd.Handle, mes, wp, 0 );
577 public void RemoveOwnedForm (Form ownedForm)
583 public void SetDesktopLocation (int x, int y)
585 Win32.SetWindowPos ((IntPtr) Handle, SetWindowPosZOrder.HWND_TOPMOST,
587 SetWindowPosFlags.SWP_NOSIZE |
588 SetWindowPosFlags.SWP_NOZORDER);
593 public DialogResult ShowDialog ()
595 Control owner = Control.getOwnerWindow ( this );
596 Control oldOwner = dlgOwner;
599 // needs to be recreated because ownership can't be changed
600 if ( IsHandleCreated ) {
601 if ( oldOwner != owner )
608 owner.Enabled = false;
613 Application.enterModalLoop ( this );
616 if ( owner != null ) {
617 owner.Enabled = true;
618 Win32.SetFocus ( owner.Handle );
627 public override string ToString ()
630 return base.ToString();
635 public event EventHandler Activated;
637 public event EventHandler Closed;
638 public event CancelEventHandler Closing;
641 // CancelEventHandler not yet implemented/stubbed
642 //public event CancelEventHandler Closing;
644 public event EventHandler Deactivate;
645 public event InputLanguageChangedEventHandler InputLanguageChanged;
646 public event InputLanguageChangingEventHandler InputLanguageChanging;
649 public event EventHandler Load;
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;
659 // --- Protected Properties
661 protected override CreateParams CreateParams {
663 CreateParams pars = base.CreateParams;
666 pars.Style |= (int)( WindowStyles.WS_CHILD | WindowStyles.WS_VISIBLE );
667 pars.ExStyle |= (int)WindowExStyles.WS_EX_MDICHILD;
670 pars.X = (int)CreateWindowCoordinates.CW_USEDEFAULT;
671 pars.Y = (int)CreateWindowCoordinates.CW_USEDEFAULT;
673 pars.Style = (int)( WindowStyles.WS_OVERLAPPEDWINDOW |
674 WindowStyles.WS_CLIPSIBLINGS /* |
675 WindowStyles.WS_CLIPCHILDREN */);
677 if ( TopLevel && Parent == null )
678 pars.Parent = IntPtr.Zero;
680 // this property is used for modal dialogs
681 if ( dlgOwner != null )
682 pars.Parent = dlgOwner.Handle;
684 // should have WS_CLIPCHILDREN style but there are
685 // problems with GroupBox at the moment
691 protected override bool MenuPresent {
692 get { return mainMenu_ != null; }
695 protected override ImeMode DefaultImeMode {
697 return base.DefaultImeMode;
701 protected override Size DefaultSize {
703 return new Size(300,300);
708 public new Size Size {
718 protected Rectangle MaximizedBounds {
720 throw new NotImplementedException ();
728 // --- Protected Methods
730 protected override void AdjustFormScrollbars (
731 bool displayScrollbars)
733 base.AdjustFormScrollbars (displayScrollbars);
736 protected override Control.ControlCollection
737 CreateControlsInstance ()
739 return base.CreateControlsInstance ();
742 protected override void CreateHandle ()
744 base.CreateHandle ();
747 protected override void DefWndProc (ref Message m)
750 window.DefMDIChildProc ( ref m );
751 else if ( IsMdiContainer && mdiClientWnd.IsHandleCreated ) {
752 if ( m.Msg != Msg.WM_SIZE )
753 window.DefFrameProc ( ref m, mdiClientWnd );
756 window.DefWndProc (ref m);
760 protected virtual void OnClosed (EventArgs e)
767 protected virtual void OnClosing(CancelEventArgs e)
769 if ( Closing != null )
773 e.Cancel = true; // don't destroy modal form
774 DialogResult = DialogResult.Cancel;
778 protected override void OnCreateControl ()
780 OnLoad ( EventArgs.Empty );
781 base.OnCreateControl ();
784 protected override void OnFontChanged (EventArgs e)
786 base.OnFontChanged (e);
789 protected override void OnHandleCreated (EventArgs e)
791 base.OnHandleCreated (e);
793 activateMdiChild ( );
797 protected override void OnHandleDestroyed (EventArgs e)
799 base.OnHandleDestroyed (e);
802 protected virtual void OnInputLanguageChanged (
803 InputLanguageChangedEventArgs e)
805 if (InputLanguageChanged != null)
806 InputLanguageChanged (this, e);
809 protected virtual void OnInputLanguagedChanging (
810 InputLanguageChangingEventArgs e)
812 if (InputLanguageChanging != null)
813 InputLanguageChanging (this, e);
817 protected virtual void OnLoad (EventArgs e)
823 protected virtual void OnMaximizedBoundsChanged (EventArgs e)
825 if (MaximizedBoundsChanged != null)
826 MaximizedBoundsChanged (this, e);
829 protected virtual void OnMaximumSizeChanged (EventArgs e)
831 if (MaximumSizeChanged != null)
832 MaximumSizeChanged (this, e);
835 protected virtual void OnMdiChildActivate (EventArgs e)
837 if (MdiChildActivate != null)
838 MdiChildActivate (this, e);
841 protected virtual void OnMenuComplete (EventArgs e)
843 if (MenuComplete != null)
844 MenuComplete (this, e);
847 protected virtual void OnMenuStart (EventArgs e)
849 if (MenuStart != null)
853 protected virtual void OnMinimumSizeChanged (EventArgs e)
859 protected override void OnPaint (PaintEventArgs e)
865 protected override void OnResize (EventArgs e)
868 // resizeMdiClient ();
871 protected override void OnStyleChanged (EventArgs e)
873 base.OnStyleChanged (e);
877 protected override void OnTextChanged (EventArgs e)
879 base.OnTextChanged (e);
882 protected override void OnVisibleChanged (EventArgs e)
884 base.OnVisibleChanged (e);
887 protected virtual IntPtr OnMenuCommand (uint id)
889 IntPtr result = (IntPtr)1;
890 System.Console.WriteLine("Form on command {0}", id);
892 MenuItem mi = Menu.GetMenuItemByID( id);
895 result = IntPtr.Zero;
901 protected override void OnWmCommand (ref Message m)
903 int wNotifyCode = (int)m.HiWordWParam;
904 int wID = (int)m.LoWordWParam;
906 if( m.LParam.ToInt32() == 0) {
907 if( wNotifyCode == 0) {
909 m.Result = OnMenuCommand( (uint)wID);
911 else if( wNotifyCode == 1) {
913 m.Result = (IntPtr)1;
916 // just pass it to DefWindowProc
917 m.Result = (IntPtr)1;
921 if ( IsMdiContainer && m.LParam != IntPtr.Zero ) {
922 // we need to pass unhandled commands
924 m.Result = (IntPtr)1;
927 base.OnWmCommand(ref m);
931 protected override bool ProcessCmdKey ( ref Message msg, Keys keyData)
933 return base.ProcessCmdKey (ref msg, keyData);
936 protected override bool ProcessDialogKey (Keys keyData)
938 return base.ProcessDialogKey (keyData);
941 protected override bool ProcessKeyPreview (ref Message m)
943 return base.ProcessKeyPreview (ref m);
946 protected override bool ProcessTabKey (bool forward)
948 return base.ProcessTabKey (forward);
951 protected override void ScaleCore (float x, float y)
953 ClientSize = new Size ( (int) ( ClientSize.Width * x ), (int) ( ClientSize.Height * y) );
956 protected override void SetBoundsCore (
957 int x, int y, int width, int height,
958 BoundsSpecified specified)
960 base.SetBoundsCore (x, y, width, height, specified);
963 protected override void SetClientSizeCore (int x, int y)
965 base.SetClientSizeCore (x, y);
968 protected override void SetVisibleCore (bool value)
970 base.SetVisibleCore (value);
973 protected override void WndProc (ref Message m)
977 CancelEventArgs args = new CancelEventArgs( false );
979 if ( !args.Cancel ) {
980 OnClosed ( EventArgs.Empty );
981 base.WndProc ( ref m );
987 case Msg.WM_FONTCHANGE:
988 EventArgs fontChangedArgs = new EventArgs();
989 OnFontChanged (fontChangedArgs);
992 EventArgs handleCreatedArgs = new EventArgs();
993 OnHandleCreated (handleCreatedArgs);
996 EventArgs destroyArgs = new EventArgs();
997 OnHandleDestroyed (destroyArgs);
999 case Msg.WM_INPUTLANGCHANGE:
1000 //InputLanguageChangedEventArgs ilChangedArgs =
1001 // new InputLanguageChangedEventArgs();
1002 //OnInputLanguageChanged (ilChangedArgs);
1004 case Msg.WM_INPUTLANGCHANGEREQUEST:
1005 //InputLanguageChangingEventArgs ilChangingArgs =
1006 // new InputLanguageChangingEventArgs();
1007 //OnInputLanguagedChanging (ilChangingArgs);
1010 case Win32.WM_SHOWWINDOW:
1016 // OnMaximizedBoundsChanged(EventArgs e)
1019 // OnMaximumSizedChanged(EventArgs e)
1021 case Msg.WM_MDIACTIVATE:
1022 EventArgs mdiActivateArgs = new EventArgs();
1023 OnMdiChildActivate (mdiActivateArgs);
1024 base.WndProc ( ref m );
1026 case Msg.WM_EXITMENULOOP:
1027 EventArgs menuCompleteArgs = new EventArgs();
1028 OnMenuComplete (menuCompleteArgs);
1030 case Msg.WM_ENTERMENULOOP:
1031 EventArgs enterMenuLoopArgs = new EventArgs();
1032 OnMenuStart (enterMenuLoopArgs);
1035 // OnMinimumSizeChanged(EventArgs e)
1038 case Msg.WM_PAINT: {
1039 Rectangle rect = new Rectangle();
1040 PaintEventArgs paintArgs = new PaintEventArgs(CreateGraphics(), rect);
1041 OnPaint (paintArgs);
1042 paintArgs.Dispose();
1046 EventArgs resizeArgs = new EventArgs();
1047 OnResize (resizeArgs);
1050 //OnStyleChanged(EventArgs e)
1052 case Msg.WM_SETTEXT:
1053 EventArgs textChangedArgs = new EventArgs();
1054 OnTextChanged (textChangedArgs);
1056 case Msg.WM_SHOWWINDOW:
1057 EventArgs visibleChangedArgs = new EventArgs();
1058 OnVisibleChanged (visibleChangedArgs);
1061 case Msg.WM_INITMENU:
1062 OnWmInitMenu (ref m);
1064 case Msg.WM_INITMENUPOPUP:
1065 OnWmInitMenuPopup (ref m);
1067 case Msg.WM_CTLCOLORLISTBOX:
1068 Control.ReflectMessage( m.LParam, ref m);
1071 base.WndProc (ref m);
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);
1084 protected virtual void OnWmInitMenuPopup (ref Message m) {
1085 Menu mn = System.Windows.Forms.Menu.GetMenuByHandle( m.WParam);
1087 mn.OnWmInitMenuPopup();
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 );
1108 private void destroyMdiClient ( ) {
1109 if ( mdiClientWnd != null ) {
1110 Controls.Remove ( mdiClientWnd );
1111 mdiClientWnd.DestroyControl ( );
1112 mdiClientWnd = null;
1115 private void resizeMdiClient ( ) {
1116 if ( IsMdiContainer && mdiClientWnd.IsHandleCreated ) {
1117 Win32.MoveWindow ( mdiClientWnd.Handle,
1118 Location.X, Location.Y,
1120 ClientSize.Height, true );
1124 private void activateMdiChild ( ) {
1125 Win32.SendMessage ( Parent.Handle, Msg.WM_MDIACTIVATE, Handle.ToInt32(), 0 );
1128 internal Control MdiClientControl {
1129 get { return this.mdiClientWnd; }
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 ( ) );
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
1142 if ( IsHandleCreated )
1143 Win32.DrawMenuBar ( Handle );
1147 internal Control dlgOwner {
1148 get { return dialog_owner; }
1149 set { dialog_owner = value;}
1152 internal bool ExitModalLoop {
1153 get { return exitModalLoop; }
1154 set { exitModalLoop = value; }
1158 //System.Windows.Forms.Form.ControlCollection.cs
1161 // stubbed out by Daniel Carrera (dcarrera@math.toronto.edu)
1163 // (C) 2002 Ximian, Inc
1167 // This is only a template. Nothing is implemented yet.
1170 // TODO: implement support classes and derive from
1172 // FIXME: use this or the one defined on Control?
1173 public class ControlCollectionX :
1174 System.Windows.Forms.Control.ControlCollection
1178 // base class not defined (yet!)
1179 public ControlCollectionX (Form owner) : base(owner) {
1183 // --- Public Methods
1185 // TODO: see what causes this compile error
1186 public override void Add(Control value) {
1190 public override bool Equals (object obj) {
1192 return base.Equals(obj);
1195 public override int GetHashCode () {
1196 //FIXME add our proprities
1197 return base.GetHashCode ();
1200 public override void Remove(Control value) {
1201 base.Remove (value);
1203 } // end of Subclass