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) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok pbartok@novell.com
29 using System.Collections;
30 using System.ComponentModel;
32 using System.Runtime.InteropServices;
34 namespace System.Windows.Forms {
37 [ClassInterface(ClassInterfaceType.AutoDispatch)]
39 [DesignTimeVisible(false)]
41 public sealed class MdiClient : Control {
42 #region Local Variables
43 private int mdi_created;
44 private ImplicitHScrollBar hbar;
45 private ImplicitVScrollBar vbar;
46 private SizeGrip sizegrip;
47 private int hbar_value;
48 private int vbar_value;
49 private bool lock_sizing;
50 private bool initializing_scrollbars;
51 private int prev_bottom;
52 private bool setting_windowstates = false;
53 internal ArrayList mdi_child_list;
54 private string form_text;
55 private bool setting_form_text;
56 private Form active_child;
58 #endregion // Local Variables
60 #region Public Classes
64 public new class ControlCollection : Control.ControlCollection {
66 private MdiClient owner;
68 public ControlCollection(MdiClient owner) : base(owner) {
72 public override void Add(Control value) {
73 if ((value is Form) == false || !(((Form)value).IsMdiChild)) {
74 throw new ArgumentException("Form must be MdiChild");
76 owner.mdi_child_list.Add (value);
79 // newest member is the active one
80 Form form = (Form) value;
81 owner.ActiveMdiChild = form;
84 public override void Remove(Control value)
86 Form form = value as Form;
88 MdiWindowManager wm = form.WindowManager as MdiWindowManager;
90 form.Closed -= wm.form_closed_handler;
94 owner.mdi_child_list.Remove (value);
98 #endregion // Public Classes
100 #region Public Constructors
103 mdi_child_list = new ArrayList ();
104 BackColor = SystemColors.AppWorkspace;
105 Dock = DockStyle.Fill;
106 SetStyle (ControlStyles.Selectable, false);
108 #endregion // Public Constructors
110 internal void SendFocusToActiveChild ()
112 Form active = this.ActiveMdiChild;
113 if (active == null) {
114 ParentForm.SendControlFocus (this);
116 active.SendControlFocus (active);
117 ParentForm.ActiveControl = active;
121 internal bool HorizontalScrollbarVisible {
122 get { return hbar != null && hbar.Visible; }
124 internal bool VerticalScrollbarVisible {
125 get { return vbar != null && vbar.Visible; }
128 internal void SetParentText(bool text_changed)
130 if (setting_form_text)
133 setting_form_text = true;
136 form_text = ParentForm.Text;
138 if (ParentForm.ActiveMaximizedMdiChild == null) {
139 ParentForm.Text = form_text;
141 string childText = ParentForm.ActiveMaximizedMdiChild.form.Text;
142 if (childText.Length > 0) {
143 ParentForm.Text = form_text + " - [" + ParentForm.ActiveMaximizedMdiChild.form.Text + "]";
145 ParentForm.Text = form_text;
149 setting_form_text = false;
152 internal override void OnPaintBackgroundInternal (PaintEventArgs pe)
154 if (BackgroundImage != null)
157 if (Parent == null || Parent.BackgroundImage == null)
159 Parent.PaintControlBackground (pe);
162 internal Form ParentForm {
163 get { return (Form) Parent; }
166 protected override Control.ControlCollection CreateControlsInstance ()
168 return new MdiClient.ControlCollection (this);
171 protected override void WndProc(ref Message m) {
172 switch ((Msg)m.Msg) {
174 PaintEventArgs pe = XplatUI.PaintEventStart (Handle, false);
177 clip = new Rectangle (0, 0, Width, Height);
179 ControlPaint.DrawBorder3D (pe.Graphics, clip, Border3DStyle.Sunken);
180 XplatUI.PaintEventEnd (Handle, false);
181 m.Result = IntPtr.Zero;
185 base.WndProc (ref m);
188 protected override void OnResize (EventArgs e)
192 if (Parent != null && Parent.IsHandleCreated)
193 XplatUI.InvalidateNC (Parent.Handle);
194 // Should probably make this into one loop
199 [System.ComponentModel.EditorBrowsable (EditorBrowsableState.Never)]
201 protected override void ScaleCore (float dx, float dy)
203 base.ScaleCore (dx, dy);
206 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
208 base.SetBoundsCore (x, y, width, height, specified);
211 #region Public Instance Properties
213 public override System.Drawing.Image BackgroundImage {
215 return base.BackgroundImage;
218 base.BackgroundImage = value;
223 [EditorBrowsable (EditorBrowsableState.Never)]
225 public override ImageLayout BackgroundImageLayout {
227 return base.BackgroundImageLayout;
230 base.BackgroundImageLayout = value;
235 public Form [] MdiChildren {
237 if (mdi_child_list == null)
239 return (Form []) mdi_child_list.ToArray (typeof (Form));
242 #endregion // Public Instance Properties
244 #region Protected Instance Properties
245 protected override CreateParams CreateParams {
247 CreateParams result = base.CreateParams;
248 result.ExStyle |= (int) WindowExStyles.WS_EX_CLIENTEDGE;
252 #endregion // Protected Instance Properties
254 #region Public Instance Methods
255 public void LayoutMdi (MdiLayout value) {
257 int max_width = Int32.MaxValue;
258 int max_height = Int32.MaxValue;
260 if (Parent != null) {
261 max_width = Parent.Width;
262 max_height = Parent.Height;
266 case MdiLayout.Cascade: {
268 for (int c = Controls.Count - 1; c >= 0; c--) {
269 Form form = (Form) Controls [c];
271 if (form.WindowState == FormWindowState.Minimized)
277 if (i != 0 && (l + form.Width > max_width || t + form.Height > max_height)) {
290 case MdiLayout.ArrangeIcons:
291 ArrangeIconicWindows (true);
293 case MdiLayout.TileHorizontal:
294 case MdiLayout.TileVertical: {
295 // First count number of windows to tile
297 for (int i = 0; i < Controls.Count; i++) {
298 Form form = Controls [i] as Form;
306 if (form.WindowState == FormWindowState.Minimized)
314 // Calculate desired height and width
317 if (value == MdiLayout.TileHorizontal) {
318 newSize = new Size (ClientSize.Width, ClientSize.Height / total);
319 offset = new Size (0, newSize.Height);
321 newSize = new Size (ClientSize.Width / total, ClientSize.Height);
322 offset = new Size (newSize.Width, 0);
325 // Loop again and set the size and location.
326 Point nextLocation = Point.Empty;
328 for (int i = 0; i < Controls.Count; i++) {
329 Form form = Controls [i] as Form;
337 if (form.WindowState == FormWindowState.Minimized)
341 form.Location = nextLocation;
342 nextLocation += offset;
349 #endregion // Public Instance Methods
351 #region Protected Instance Methods
352 #endregion // Protected Instance Methods
354 internal void SizeScrollBars ()
359 if (!IsHandleCreated)
362 if (Controls.Count == 0 || ((Form) Controls [0]).WindowState == FormWindowState.Maximized) {
364 hbar.Visible = false;
366 vbar.Visible = false;
367 if (sizegrip != null)
368 sizegrip.Visible = false;
377 foreach (Form child in Controls) {
380 if (child.Right > right)
382 if (child.Left < left) {
386 if (child.Bottom > bottom)
387 bottom = child.Bottom;
393 int available_width = ClientSize.Width;
394 int available_height = ClientSize.Height;
396 bool need_hbar = false;
397 bool need_vbar = false;
399 if (right - left > available_width || left < 0) {
401 available_height -= SystemInformation.HorizontalScrollBarHeight;
403 if (bottom - top > available_height || top < 0) {
405 available_width -= SystemInformation.VerticalScrollBarWidth;
407 if (!need_hbar && (right - left > available_width || left < 0)) {
409 available_height -= SystemInformation.HorizontalScrollBarHeight;
415 hbar = new ImplicitHScrollBar ();
416 Controls.AddImplicit (hbar);
419 CalcHBar (left, right, need_vbar);
420 } else if (hbar != null)
421 hbar.Visible = false;
425 vbar = new ImplicitVScrollBar ();
426 Controls.AddImplicit (vbar);
429 CalcVBar (top, bottom, need_hbar);
430 } else if (vbar != null)
431 vbar.Visible = false;
433 if (need_hbar && need_vbar) {
434 if (sizegrip == null) {
435 sizegrip = new SizeGrip (this.ParentForm);
436 Controls.AddImplicit (sizegrip);
438 sizegrip.Location = new Point (hbar.Right, vbar.Bottom);
439 sizegrip.Visible = true;
440 XplatUI.SetZOrder (sizegrip.Handle, vbar.Handle, false, false);
441 } else if (sizegrip != null) {
442 sizegrip.Visible = false;
446 private void CalcHBar (int left, int right, bool vert_vis)
448 initializing_scrollbars = true;
451 hbar.Top = ClientRectangle.Bottom - hbar.Height;
452 hbar.Width = ClientRectangle.Width - (vert_vis ? SystemInformation.VerticalScrollBarWidth : 0);
453 hbar.LargeChange = 50;
454 hbar.Minimum = Math.Min (left, 0);
455 hbar.Maximum = Math.Max (right - ClientSize.Width + 51 + (vert_vis ? SystemInformation.VerticalScrollBarWidth : 0), 0);
458 hbar.ValueChanged += new EventHandler (HBarValueChanged);
459 XplatUI.SetZOrder (hbar.Handle, IntPtr.Zero, true, false);
461 initializing_scrollbars = false;
464 private void CalcVBar (int top, int bottom, bool horz_vis)
466 initializing_scrollbars = true;
469 vbar.Left = ClientRectangle.Right - vbar.Width;
470 vbar.Height = ClientRectangle.Height - (horz_vis ? SystemInformation.HorizontalScrollBarHeight : 0);
471 vbar.LargeChange = 50;
472 vbar.Minimum = Math.Min (top, 0);
473 vbar.Maximum = Math.Max (bottom - ClientSize.Height + 51 + (horz_vis ? SystemInformation.HorizontalScrollBarHeight : 0), 0);
476 vbar.ValueChanged += new EventHandler (VBarValueChanged);
477 XplatUI.SetZOrder (vbar.Handle, IntPtr.Zero, true, false);
479 initializing_scrollbars = false;
482 private void HBarValueChanged (object sender, EventArgs e)
484 if (initializing_scrollbars)
487 if (hbar.Value == hbar_value)
493 int diff = hbar_value - hbar.Value;
494 foreach (Form child in Controls) {
501 hbar_value = hbar.Value;
504 private void VBarValueChanged (object sender, EventArgs e)
506 if (initializing_scrollbars)
509 if (vbar.Value == vbar_value)
515 int diff = vbar_value - vbar.Value;
516 foreach (Form child in Controls) {
523 vbar_value = vbar.Value;
526 private void ArrangeWindows ()
528 if (!IsHandleCreated)
532 if (prev_bottom != -1)
533 change = Bottom - prev_bottom;
535 foreach (Control c in Controls) {
536 Form child = c as Form;
538 if (c == null || !child.Visible)
541 MdiWindowManager wm = child.WindowManager as MdiWindowManager;
542 if (wm.GetWindowState () == FormWindowState.Maximized)
543 child.Bounds = wm.MaximizedBounds;
545 if (wm.GetWindowState () == FormWindowState.Minimized) {
551 prev_bottom = Bottom;
554 internal void ArrangeIconicWindows (bool rearrange_all)
556 Rectangle rect = Rectangle.Empty;
559 foreach (Form form in Controls) {
560 if (form.WindowState != FormWindowState.Minimized)
563 MdiWindowManager wm = (MdiWindowManager) form.WindowManager;
565 if (wm.IconicBounds != Rectangle.Empty && !rearrange_all) {
566 if (form.Bounds != wm.IconicBounds)
567 form.Bounds = wm.IconicBounds;
572 int startx, starty, currentx, currenty;
574 rect.Size = wm.IconicSize;
577 starty = ClientSize.Height - rect.Height;
585 foreach (Form form2 in Controls) {
586 if (form2 == form || form2.window_state != FormWindowState.Minimized)
589 if (form2.Bounds.IntersectsWith(rect)) {
595 currentx += rect.Width;
596 if (currentx + rect.Width > Right) {
598 currenty -= rect.Height;
602 wm.IconicBounds = rect;
603 form.Bounds = wm.IconicBounds;
608 internal void ChildFormClosed (Form form)
610 if (Controls.Count > 1) {
611 Form next = (Form) Controls [1];
612 if (form.WindowState == FormWindowState.Maximized)
613 next.WindowState = FormWindowState.Maximized;
614 ActivateChild (next);
617 Controls.Remove (form);
619 if (Controls.Count == 0) {
620 XplatUI.RequestNCRecalc (Parent.Handle);
621 ParentForm.PerformLayout ();
624 SetParentText (false);
627 internal void ActivateNextChild ()
629 if (Controls.Count < 1)
631 if (Controls.Count == 1 && Controls[0] == ActiveMdiChild)
634 Form front = (Form) Controls [0];
635 Form form = (Form) Controls [1];
637 ActivateChild (form);
641 internal void ActivatePreviousChild ()
643 if (Controls.Count <= 1)
646 Form back = (Form) Controls [Controls.Count - 1];
648 ActivateChild (back);
651 internal void ActivateChild (Form form)
653 if (Controls.Count < 1)
656 if (ParentForm.is_changing_visible_state)
659 Form current = (Form) Controls [0];
661 // We want to resize the new active form before it is
662 // made active to avoid flickering. Can't do it in the
663 // normal way (form.WindowState = Maximized) since it's not
664 // active yet and everything would just return to before.
665 // We also won't suspend layout, this way the layout will
666 // happen before the form is made active (and in many cases
667 // before it is visible, which avoids flickering as well).
668 MdiWindowManager wm = (MdiWindowManager)form.WindowManager;
669 if (current.WindowState == FormWindowState.Maximized && form.WindowState != FormWindowState.Maximized && form.Visible) {
670 FormWindowState old_state = form.window_state;
671 SetWindowState (form, old_state, FormWindowState.Maximized, true);
672 wm.was_minimized = form.window_state == FormWindowState.Minimized;
673 form.window_state = FormWindowState.Maximized;
674 SetParentText (false);
676 form.BringToFront ();
677 form.SendControlFocus (form);
678 SetWindowStates (wm);
679 if (current != form) {
680 form.has_focus = false;
681 if (current.IsHandleCreated)
682 XplatUI.InvalidateNC (current.Handle);
683 if (form.IsHandleCreated)
684 XplatUI.InvalidateNC (form.Handle);
686 active_child = (Form) Controls [0];
688 if (active_child.Visible)
689 ParentForm.ActiveControl = active_child;
692 internal override IntPtr AfterTopMostControl ()
694 // order of scrollbars:
697 // bottom = horizontal
698 if (hbar != null && hbar.Visible)
700 // no need to check for sizegrip since it will only
701 // be visible if hbar is visible.
702 if (vbar != null && vbar.Visible)
705 return base.AfterTopMostControl ();
708 internal bool SetWindowStates (MdiWindowManager wm)
711 MDI WindowState behaviour:
712 - If the active window is maximized, all other maximized windows are normalized.
713 - If a normal window gets focus and the original active window was maximized,
714 the normal window gets maximized and the original window gets normalized.
715 - If a minimized window gets focus and the original window was maximized,
716 the minimzed window gets maximized and the original window gets normalized.
717 If the ex-minimized window gets deactivated, it will be normalized.
721 if (setting_windowstates) {
728 bool is_active = wm.IsActive;
729 bool maximize_this = false;
735 ArrayList minimize_these = new ArrayList ();
736 ArrayList normalize_these = new ArrayList ();
738 setting_windowstates = true;
739 foreach (Form frm in mdi_child_list) {
742 } else if (!frm.Visible){
745 if (frm.WindowState == FormWindowState.Maximized && is_active) {
746 maximize_this = true;
747 if (((MdiWindowManager) frm.window_manager).was_minimized) {
748 minimize_these.Add (frm);
750 normalize_these.Add (frm);
755 if (maximize_this && form.WindowState != FormWindowState.Maximized) {
756 wm.was_minimized = form.window_state == FormWindowState.Minimized;
757 form.WindowState = FormWindowState.Maximized;
760 foreach (Form frm in minimize_these)
761 frm.WindowState = FormWindowState.Minimized;
763 foreach (Form frm in normalize_these)
764 frm.WindowState = FormWindowState.Normal;
767 SetParentText (false);
769 XplatUI.RequestNCRecalc (ParentForm.Handle);
770 XplatUI.RequestNCRecalc (Handle);
774 setting_windowstates = false;
777 if (form.MdiParent.MainMenuStrip != null)
778 form.MdiParent.MainMenuStrip.RefreshMdiItems ();
781 return maximize_this;
784 internal void SetWindowState (Form form, FormWindowState old_window_state, FormWindowState new_window_state, bool is_activating_child)
786 bool mdiclient_layout;
788 MdiWindowManager wm = (MdiWindowManager) form.window_manager;
790 if (!is_activating_child && new_window_state == FormWindowState.Maximized && !wm.IsActive) {
791 ActivateChild (form);
795 if (SetWindowStates (wm))
798 if (old_window_state == new_window_state)
801 if (old_window_state == FormWindowState.Normal)
802 wm.NormalBounds = form.Bounds;
804 mdiclient_layout = old_window_state == FormWindowState.Maximized || new_window_state == FormWindowState.Maximized;
806 switch (new_window_state) {
807 case FormWindowState.Minimized:
808 ArrangeIconicWindows (false);
810 case FormWindowState.Maximized:
811 form.Bounds = wm.MaximizedBounds;
813 case FormWindowState.Normal:
814 form.Bounds = wm.NormalBounds;
818 wm.UpdateWindowDecorations (new_window_state);
822 if (mdiclient_layout)
823 Parent.PerformLayout ();
825 XplatUI.RequestNCRecalc (Parent.Handle);
826 XplatUI.RequestNCRecalc (form.Handle);
827 if (!setting_windowstates)
830 internal int ChildrenCreated {
831 get { return mdi_created; }
832 set { mdi_created = value; }
835 internal Form ActiveMdiChild {
838 if (!ParentForm.Visible)
841 if (Controls.Count < 1)
844 if (!ParentForm.IsHandleCreated)
847 if (!ParentForm.has_been_visible)
850 if (!ParentForm.Visible)
854 for (int i = 0; i < Controls.Count; i++) {
855 if (Controls [i].Visible) {
856 active_child = (Form) Controls [i];
863 ActivateChild (value);
867 internal void ActivateActiveMdiChild ()
869 if (ParentForm.is_changing_visible_state)
872 for (int i = 0; i < Controls.Count; i++) {
873 if (Controls [i].Visible) {
874 ActivateChild ((Form) Controls [i]);