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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
29 // Revision 1.22 2004/11/08 20:50:29 pbartok
30 // - Fixed arguments for updated SetTopmost method
32 // Revision 1.21 2004/11/04 14:47:58 jordi
33 // collection completion, drawing issues, missing features
35 // Revision 1.20 2004/10/29 15:55:26 jordi
36 // Menu key navigation, itemcollection completion, menu fixes
38 // Revision 1.19 2004/10/20 03:56:23 pbartok
39 // - Added private FormParentWindow class which acts as the container for
40 // our form and as the non-client area where menus are drawn
41 // - Added/Moved required tie-ins to Jordi's menus
42 // - Fixed/Implemented the FormStartPosition functionality
44 // Revision 1.18 2004/10/18 04:47:09 pbartok
45 // - Fixed Form.ControlCollection to handle owner relations
46 // - Added Owner/OwnedForms handling
47 // - Implemented Z-Ordering for owned forms
48 // - Removed unneeded private overload of ShowDialog
49 // - Fixed ShowDialog, added the X11 incarnation of modal handling (or so I
51 // - Fixed Close(), had wrong default
52 // - Added firing of OnLoad event
53 // - Added some commented out debug code for Ownership handling
55 // Revision 1.17 2004/10/15 12:43:19 jordi
56 // menu work, mainmenu, subitems, etc
58 // Revision 1.16 2004/10/14 06:17:58 ravindra
59 // Fixed class signature. ShowDialog (Control) is not a public method.
61 // Revision 1.15 2004/10/08 08:50:29 jordi
64 // Revision 1.14 2004/10/02 19:05:52 pbartok
65 // - Added KeyPreview property
66 // - Added Menu property (still incomplete, pending Jordi's menu work)
67 // - Implemented ProcessCmdKey
68 // - Implemented ProcessDialogKey
69 // - Implemented ProcessKeyPreview
71 // Revision 1.13 2004/10/01 17:53:26 jackson
72 // Implement the Close method so work on MessageBox can continue.
74 // Revision 1.12 2004/09/23 19:08:59 jackson
77 // Revision 1.11 2004/09/22 20:09:44 pbartok
78 // - Added Form.ControllCollection class
79 // - Added handling for Form owners: Owner, OwnedForms, AddOwnedForm,
80 // RemoveOwnedForm (still incomplete, missing on-top and common
81 // minimize/maximize behaviour)
82 // - Added StartPosition property (still incomplete, does not use when
84 // - Added ShowDialog() methods (still incomplete, missing forcing the dialog
87 // Revision 1.10 2004/09/13 16:56:04 pbartok
88 // - Fixed #region names
89 // - Moved properties and methods into their proper #regions
91 // Revision 1.9 2004/09/13 16:51:29 pbartok
92 // - Added Accept and CancelButton properties
93 // - Added ProcessDialogKey() method
95 // Revision 1.8 2004/09/01 02:05:18 pbartok
96 // - Added (partial) implementation of DialogResult; rest needs to be
97 // implemented when the modal loop code is done
99 // Revision 1.7 2004/08/23 22:10:02 pbartok
100 // - Fixed handling of WM_CLOSE message
101 // - Removed debug output
103 // Revision 1.6 2004/08/22 21:10:30 pbartok
104 // - Removed OverlappedWindow style from Control, instead it's default
106 // - Made form windows OverlappedWindow by default
108 // Revision 1.5 2004/08/19 21:30:37 pbartok
109 // - Added handling of WM_CLOSE
111 // Revision 1.4 2004/08/11 22:20:59 pbartok
114 // Revision 1.3 2004/08/04 21:13:47 pbartok
115 // - Added AutoScale properties
117 // Revision 1.2 2004/07/13 15:31:45 jordi
118 // commit: new properties and fixes form size problems
120 // Revision 1.1 2004/07/09 05:21:25 pbartok
121 // - Initial check-in
128 using System.Drawing;
129 using System.ComponentModel;
130 using System.Collections;
131 using System.Runtime.InteropServices;
132 using System.Threading;
134 namespace System.Windows.Forms {
135 public class Form : ContainerControl {
136 #region Local Variables
137 internal bool closing;
139 private static bool autoscale;
140 private static Size autoscale_base_size;
141 private bool is_modal;
142 internal bool end_modal; // This var is being monitored by the application modal loop
143 private IButtonControl accept_button;
144 private IButtonControl cancel_button;
145 private DialogResult dialog_result;
146 private FormStartPosition start_position;
148 private Form.ControlCollection owned_forms;
149 private bool key_preview;
150 private MainMenu menu;
151 internal FormParentWindow form_parent_window;
152 #endregion // Local Variables
154 #region Private Classes
156 // This class will take over for the client area
157 internal class FormParentWindow : Control {
158 #region FormParentWindow Class Local Variables
160 #endregion // FormParentWindow Class Local Variables
162 #region FormParentWindow Class Constructor
163 internal FormParentWindow(Form owner) : base() {
169 BackColor = owner.BackColor;
171 this.Location = new Point(0, 0);
172 this.Dock = DockStyle.Fill;
174 MouseDown += new MouseEventHandler (OnMouseDownForm);
175 MouseMove += new MouseEventHandler (OnMouseMoveForm);
176 owner.TextChanged += new EventHandler(OnFormTextChanged);
178 #endregion // FormParentWindow Class Constructor
180 #region FormParentWindow Class Protected Instance Methods
\r
181 protected override CreateParams CreateParams {
\r
185 cp = base.CreateParams;
\r
187 cp.Style = (int)(WindowStyles.WS_OVERLAPPEDWINDOW |
188 WindowStyles.WS_VISIBLE |
189 WindowStyles.WS_CLIPSIBLINGS |
190 WindowStyles.WS_CLIPCHILDREN);
196 if (this.IsHandleCreated) {
\r
204 XplatUI.GetWindowPos(this.window.Handle, out x, out y, out width, out height, out cwidth, out cheight);
\r
205 UpdateBounds(x, y, width, height);
\r
206 owner.UpdateBounds(x, y, width, height);
\r
214 protected override void OnResize(EventArgs e) {
\r
216 //owner.SetBoundsCore(owner.Bounds.X, owner.Bounds.Y, ClientSize.Width, ClientSize.Height, BoundsSpecified.All);
217 if (owner.menu == null) {
218 owner.SetBoundsCore(0, 0, ClientSize.Width, ClientSize.Height, BoundsSpecified.All);
222 menu_height = MenuAPI.MenuBarCalcSize(DeviceContext, owner.Menu.menu_handle, ClientSize.Width);
223 Invalidate (new Rectangle (0, 0, ClientSize.Width, menu_height));
224 owner.SetBoundsCore(0, menu_height, ClientSize.Width, ClientSize.Height-menu_height, BoundsSpecified.All);
228 protected override void OnPaint(PaintEventArgs pevent) {
\r
229 OnDrawMenu (pevent.Graphics);
232 protected override void WndProc(ref Message m) {
\r
233 switch((Msg)m.Msg) {
\r
234 case Msg.WM_CLOSE: {
\r
235 CancelEventArgs args = new CancelEventArgs();
\r
237 owner.OnClosing(args);
\r
239 if (!args.Cancel) {
\r
240 owner.OnClosed(EventArgs.Empty);
\r
241 owner.closing = true;
\r
242 base.WndProc(ref m);
\r
249 base.WndProc (ref m);
\r
254 #endregion // FormParentWindow Class Protected Instance Methods
\r
256 #region FormParentWindow Class Private Methods
\r
257 private void OnMouseDownForm (object sender, MouseEventArgs e) {
258 if (owner.menu != null)
259 owner.menu.OnMouseDown (owner, e);
262 private void OnMouseMoveForm (object sender, MouseEventArgs e) {
263 if (owner.menu != null)
264 owner.menu.OnMouseMove (owner, e);
268 private void OnDrawMenu (Graphics dc) {
269 if (owner.menu != null) {
270 Rectangle rect = new Rectangle (0,0, Width, 0);
271 MenuAPI.DrawMenuBar (dc, owner.menu.Handle, rect);
274 private void OnFormTextChanged(object sender, EventArgs e) {
\r
275 this.Text = ((Control)sender).Text;
\r
277 #endregion // FormParentWindow Class Private Methods
279 #endregion // Private Classes
281 #region Public Classes
282 public class ControlCollection : Control.ControlCollection {
285 public ControlCollection(Form owner) : base(owner) {
286 this.form_owner = owner;
289 public override void Add(Control value) {
290 for (int i=0; i<list.Count; i++) {
291 if (list[i]==value) {
292 // Do we need to do anything here?
297 ((Form)value).owner=(Form)owner;
300 public override void Remove(Control value) {
\r
301 ((Form)value).owner = null;
\r
302 base.Remove (value);
\r
305 #endregion // Public Classes
307 #region Public Constructor & Destructor
312 dialog_result = DialogResult.None;
313 start_position = FormStartPosition.WindowsDefaultLocation;
317 owned_forms = new Form.ControlCollection(this);
319 #endregion // Public Constructor & Destructor
322 #region Public Static Properties
323 #endregion // Public Static Properties
325 #region Public Instance Properties
326 public IButtonControl AcceptButton {
328 return accept_button;
332 accept_button = value;
336 public bool AutoScale {
346 public virtual Size AutoScaleBaseSize {
348 return autoscale_base_size;
352 autoscale_base_size=value;
356 public IButtonControl CancelButton {
358 return cancel_button;
362 cancel_button = value;
366 public DialogResult DialogResult {
368 return dialog_result;
372 dialog_result = value;
374 if (is_modal && (dialog_result != DialogResult.None)) {
380 public bool KeyPreview {
390 public MainMenu Menu {
398 form_parent_window.Width = form_parent_window.Width; // Trigger a resize
403 // To simulate the non-client are for menus we create a
404 // new control as the 'client area' of our form. This
405 // way, the origin stays 0,0 and we don't have to fiddle with
406 // coordinates. The menu area is part of the original container
408 form_parent_window.Width = form_parent_window.Width; // Trigger a resize
412 MenuAPI.SetMenuBarWindow (menu.Handle, form_parent_window);
423 public Form[] OwnedForms {
427 form_list = new Form[owned_forms.Count];
429 for (int i=0; i<owned_forms.Count; i++) {
430 form_list[i] = (Form)owned_forms[i];
443 if (owner != value) {
445 owner.RemoveOwnedForm(this);
448 owner.AddOwnedForm(this);
450 XplatUI.SetTopmost(this.window.Handle, owner.window.Handle, true);
452 XplatUI.SetTopmost(this.window.Handle, IntPtr.Zero, false);
458 public FormStartPosition StartPosition {
460 return start_position;
464 if (start_position == FormStartPosition.WindowsDefaultLocation) { // Only do this if it's not set yet
465 start_position = value;
466 if (form_parent_window.IsHandleCreated) {
467 switch(start_position) {
468 case FormStartPosition.CenterParent: {
469 if (Parent!=null && Width>0 && Height>0) {
470 this.Location = new Point(Parent.Size.Width/2-Width/2, Parent.Size.Height/2-Height/2);
475 case FormStartPosition.CenterScreen: {
476 if (Width>0 && Height>0) {
479 XplatUI.GetDisplaySize(out DisplaySize);
480 this.Location = new Point(DisplaySize.Width/2-Width/2, DisplaySize.Height/2-Height/2);
493 #endregion // Public Instance Properties
495 #region Protected Instance Properties
496 [MonoTODO("Need to add MDI support")]
497 protected override CreateParams CreateParams {
499 CreateParams cp = new CreateParams();
501 if (this.form_parent_window == null) {
502 form_parent_window = new FormParentWindow(this);
505 cp.Caption = "ClientArea";
506 cp.ClassName=XplatUI.DefaultClassName;
510 cp.Parent = this.form_parent_window.window.Handle;
516 cp.Style = (int)WindowStyles.WS_CHILD;
517 cp.Style |= (int)WindowStyles.WS_VISIBLE;
518 cp.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
519 cp.Style |= (int)WindowStyles.WS_CLIPCHILDREN;
525 protected override Size DefaultSize {
527 return new Size (250, 250);
531 protected override void OnPaint (PaintEventArgs pevent)
533 base.OnPaint (pevent);
536 #endregion // Protected Instance Properties
539 #region Public Static Methods
540 #endregion // Public Static Methods
542 #region Public Instance Methods
543 public void AddOwnedForm(Form ownedForm) {
544 owned_forms.Add(ownedForm);
547 public void RemoveOwnedForm(Form ownedForm) {
548 owned_forms.Remove(ownedForm);
551 public DialogResult ShowDialog() {
552 return ShowDialog(null);
555 public DialogResult ShowDialog(IWin32Window ownerWin32) {
556 Control owner = null;
558 if (ownerWin32 != null) {
559 owner = Control.FromHandle(ownerWin32.Handle);
563 return DialogResult.None;
567 throw new InvalidOperationException("Already visible forms cannot be displayed as a modal dialog. Set the Visible property to 'false' prior to calling Form.ShowDialog.");
570 if (!IsHandleCreated) {
574 XplatUI.SetModal(window.Handle, true);
579 Application.ModalRun(this);
583 XplatUI.SetModal(window.Handle, false);
590 CancelEventArgs args = new CancelEventArgs ();
593 OnClosed (EventArgs.Empty);
599 #endregion // Public Instance Methods
601 #region Protected Instance Methods
602 protected override void CreateHandle() {
\r
603 base.CreateHandle ();
\r
606 protected override void OnCreateControl() {
\r
607 base.OnCreateControl ();
\r
608 OnLoad(EventArgs.Empty);
\r
611 protected override void OnHandleCreated(EventArgs e) {
\r
612 base.OnHandleCreated (e);
\r
615 protected override void OnHandleDestroyed(EventArgs e) {
\r
616 base.OnHandleDestroyed (e);
\r
620 protected override void OnResize(EventArgs e) {
624 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
\r
625 if (base.ProcessCmdKey (ref msg, keyData)) {
\r
629 // Give our menu a shot
\r
630 if (menu != null) {
\r
631 return menu.ProcessCmdKey(ref msg, keyData);
\r
637 protected override bool ProcessDialogKey(Keys keyData) {
638 if (keyData == Keys.Enter && accept_button != null) {
639 accept_button.PerformClick();
641 } else if (keyData == Keys.Enter && cancel_button != null) {
642 cancel_button.PerformClick();
645 return base.ProcessDialogKey(keyData);
648 protected override bool ProcessKeyPreview(ref Message msg) {
\r
650 if (ProcessKeyEventArgs(ref msg)) {
\r
654 return base.ProcessKeyPreview (ref msg);
\r
657 protected override void WndProc(ref Message m) {
\r
658 switch((Msg)m.Msg) {
\r
659 case Msg.WM_CLOSE: {
\r
660 CancelEventArgs args = new CancelEventArgs();
\r
664 if (!args.Cancel) {
\r
665 OnClosed(EventArgs.Empty);
\r
667 base.WndProc(ref m);
\r
673 #if topmost_workaround
674 case Msg.WM_ACTIVATE: {
\r
675 if (this.OwnedForms.Length>0) {
676 XplatUI.SetZOrder(this.OwnedForms[0].window.Handle, this.window.Handle, false, false);
682 base.WndProc (ref m);
\r
687 #endregion // Protected Instance Methods
690 protected virtual void OnActivated(EventArgs e) {
691 if (Activated != null) {
696 protected virtual void OnClosed(EventArgs e) {
697 if (Closed != null) {
702 protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e) {
703 if (Closing != null) {
708 protected virtual void OnDeactivate(EventArgs e) {
709 if (Deactivate != null) {
714 protected virtual void OnInputLanguageChanged(InputLanguageChangedEventArgs e) {
715 if (InputLanguageChanged!=null) {
716 InputLanguageChanged(this, e);
720 protected virtual void OnInputLanguageChanging(InputLanguageChangingEventArgs e) {
721 if (InputLanguageChanging!=null) {
722 InputLanguageChanging(this, e);
726 protected virtual void OnLoad(EventArgs e) {
732 protected virtual void OnMaximizedBoundsChanged(EventArgs e) {
733 if (MaximizedBoundsChanged != null) {
734 MaximizedBoundsChanged(this, e);
738 protected virtual void OnMaximumSizeChanged(EventArgs e) {
739 if (MaximumSizeChanged != null) {
740 MaximumSizeChanged(this, e);
744 protected virtual void OnMdiChildActivate(EventArgs e) {
745 if (MdiChildActivate != null) {
746 MdiChildActivate(this, e);
750 protected virtual void OnMenuComplete(EventArgs e) {
751 if (MenuComplete != null) {
752 MenuComplete(this, e);
756 protected virtual void OnMenuStart(EventArgs e) {
757 if (MenuStart != null) {
762 protected virtual void OnMinimumSizeChanged(EventArgs e) {
763 if (MinimumSizeChanged != null) {
764 MinimumSizeChanged(this, e);
768 public event EventHandler Activated;
769 public event EventHandler Closed;
770 public event CancelEventHandler Closing;
771 public event EventHandler Deactivate;
772 public event InputLanguageChangedEventHandler InputLanguageChanged;
773 public event InputLanguageChangingEventHandler InputLanguageChanging;
774 public event EventHandler Load;
775 public event EventHandler MaximizedBoundsChanged;
776 public event EventHandler MaximumSizeChanged;
777 public event EventHandler MdiChildActivate;
778 public event EventHandler MenuComplete;
779 public event EventHandler MenuStart;
780 public event EventHandler MinimumSizeChanged;