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 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
201 // Never change the MdiClient's location
202 specified &= ~BoundsSpecified.Location;
204 base.ScaleControl (factor, specified);
207 [System.ComponentModel.EditorBrowsable (EditorBrowsableState.Never)]
209 protected override void ScaleCore (float dx, float dy)
211 base.ScaleCore (dx, dy);
214 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
216 base.SetBoundsCore (x, y, width, height, specified);
219 #region Public Instance Properties
221 public override System.Drawing.Image BackgroundImage {
223 return base.BackgroundImage;
226 base.BackgroundImage = value;
231 [EditorBrowsable (EditorBrowsableState.Never)]
233 public override ImageLayout BackgroundImageLayout {
235 return base.BackgroundImageLayout;
238 base.BackgroundImageLayout = value;
243 public Form [] MdiChildren {
245 if (mdi_child_list == null)
247 return (Form []) mdi_child_list.ToArray (typeof (Form));
250 #endregion // Public Instance Properties
252 #region Protected Instance Properties
253 protected override CreateParams CreateParams {
255 CreateParams result = base.CreateParams;
256 result.ExStyle |= (int) WindowExStyles.WS_EX_CLIENTEDGE;
260 #endregion // Protected Instance Properties
262 #region Public Instance Methods
263 public void LayoutMdi (MdiLayout value) {
265 int max_width = Int32.MaxValue;
266 int max_height = Int32.MaxValue;
268 if (Parent != null) {
269 max_width = Parent.Width;
270 max_height = Parent.Height;
274 case MdiLayout.Cascade: {
276 for (int c = Controls.Count - 1; c >= 0; c--) {
277 Form form = (Form) Controls [c];
279 if (form.WindowState == FormWindowState.Minimized)
285 if (i != 0 && (l + form.Width > max_width || t + form.Height > max_height)) {
298 case MdiLayout.ArrangeIcons:
299 ArrangeIconicWindows (true);
301 case MdiLayout.TileHorizontal:
302 case MdiLayout.TileVertical: {
303 // First count number of windows to tile
305 for (int i = 0; i < Controls.Count; i++) {
306 Form form = Controls [i] as Form;
314 if (form.WindowState == FormWindowState.Minimized)
322 // Calculate desired height and width
325 if (value == MdiLayout.TileHorizontal) {
326 newSize = new Size (ClientSize.Width, ClientSize.Height / total);
327 offset = new Size (0, newSize.Height);
329 newSize = new Size (ClientSize.Width / total, ClientSize.Height);
330 offset = new Size (newSize.Width, 0);
333 // Loop again and set the size and location.
334 Point nextLocation = Point.Empty;
336 for (int i = 0; i < Controls.Count; i++) {
337 Form form = Controls [i] as Form;
345 if (form.WindowState == FormWindowState.Minimized)
349 form.Location = nextLocation;
350 nextLocation += offset;
357 #endregion // Public Instance Methods
359 #region Protected Instance Methods
360 #endregion // Protected Instance Methods
362 internal void SizeScrollBars ()
367 if (!IsHandleCreated)
370 if (Controls.Count == 0 || ((Form) Controls [0]).WindowState == FormWindowState.Maximized) {
372 hbar.Visible = false;
374 vbar.Visible = false;
375 if (sizegrip != null)
376 sizegrip.Visible = false;
385 foreach (Form child in Controls) {
388 if (child.Right > right)
390 if (child.Left < left) {
394 if (child.Bottom > bottom)
395 bottom = child.Bottom;
401 int available_width = ClientSize.Width;
402 int available_height = ClientSize.Height;
404 bool need_hbar = false;
405 bool need_vbar = false;
407 if (right - left > available_width || left < 0) {
409 available_height -= SystemInformation.HorizontalScrollBarHeight;
411 if (bottom - top > available_height || top < 0) {
413 available_width -= SystemInformation.VerticalScrollBarWidth;
415 if (!need_hbar && (right - left > available_width || left < 0)) {
417 available_height -= SystemInformation.HorizontalScrollBarHeight;
423 hbar = new ImplicitHScrollBar ();
424 Controls.AddImplicit (hbar);
427 CalcHBar (left, right, need_vbar);
428 } else if (hbar != null)
429 hbar.Visible = false;
433 vbar = new ImplicitVScrollBar ();
434 Controls.AddImplicit (vbar);
437 CalcVBar (top, bottom, need_hbar);
438 } else if (vbar != null)
439 vbar.Visible = false;
441 if (need_hbar && need_vbar) {
442 if (sizegrip == null) {
443 sizegrip = new SizeGrip (this.ParentForm);
444 Controls.AddImplicit (sizegrip);
446 sizegrip.Location = new Point (hbar.Right, vbar.Bottom);
447 sizegrip.Visible = true;
448 XplatUI.SetZOrder (sizegrip.Handle, vbar.Handle, false, false);
449 } else if (sizegrip != null) {
450 sizegrip.Visible = false;
454 private void CalcHBar (int left, int right, bool vert_vis)
456 initializing_scrollbars = true;
459 hbar.Top = ClientRectangle.Bottom - hbar.Height;
460 hbar.Width = ClientRectangle.Width - (vert_vis ? SystemInformation.VerticalScrollBarWidth : 0);
461 hbar.LargeChange = 50;
462 hbar.Minimum = Math.Min (left, 0);
463 hbar.Maximum = Math.Max (right - ClientSize.Width + 51 + (vert_vis ? SystemInformation.VerticalScrollBarWidth : 0), 0);
466 hbar.ValueChanged += new EventHandler (HBarValueChanged);
467 XplatUI.SetZOrder (hbar.Handle, IntPtr.Zero, true, false);
469 initializing_scrollbars = false;
472 private void CalcVBar (int top, int bottom, bool horz_vis)
474 initializing_scrollbars = true;
477 vbar.Left = ClientRectangle.Right - vbar.Width;
478 vbar.Height = ClientRectangle.Height - (horz_vis ? SystemInformation.HorizontalScrollBarHeight : 0);
479 vbar.LargeChange = 50;
480 vbar.Minimum = Math.Min (top, 0);
481 vbar.Maximum = Math.Max (bottom - ClientSize.Height + 51 + (horz_vis ? SystemInformation.HorizontalScrollBarHeight : 0), 0);
484 vbar.ValueChanged += new EventHandler (VBarValueChanged);
485 XplatUI.SetZOrder (vbar.Handle, IntPtr.Zero, true, false);
487 initializing_scrollbars = false;
490 private void HBarValueChanged (object sender, EventArgs e)
492 if (initializing_scrollbars)
495 if (hbar.Value == hbar_value)
501 int diff = hbar_value - hbar.Value;
502 foreach (Form child in Controls) {
509 hbar_value = hbar.Value;
512 private void VBarValueChanged (object sender, EventArgs e)
514 if (initializing_scrollbars)
517 if (vbar.Value == vbar_value)
523 int diff = vbar_value - vbar.Value;
524 foreach (Form child in Controls) {
531 vbar_value = vbar.Value;
534 private void ArrangeWindows ()
536 if (!IsHandleCreated)
540 if (prev_bottom != -1)
541 change = Bottom - prev_bottom;
543 foreach (Control c in Controls) {
544 Form child = c as Form;
546 if (c == null || !child.Visible)
549 MdiWindowManager wm = child.WindowManager as MdiWindowManager;
550 if (wm.GetWindowState () == FormWindowState.Maximized)
551 child.Bounds = wm.MaximizedBounds;
553 if (wm.GetWindowState () == FormWindowState.Minimized) {
559 prev_bottom = Bottom;
562 internal void ArrangeIconicWindows (bool rearrange_all)
564 Rectangle rect = Rectangle.Empty;
567 foreach (Form form in Controls) {
568 if (form.WindowState != FormWindowState.Minimized)
571 MdiWindowManager wm = (MdiWindowManager) form.WindowManager;
573 if (wm.IconicBounds != Rectangle.Empty && !rearrange_all) {
574 if (form.Bounds != wm.IconicBounds)
575 form.Bounds = wm.IconicBounds;
580 int startx, starty, currentx, currenty;
582 rect.Size = wm.IconicSize;
585 starty = ClientSize.Height - rect.Height;
593 foreach (Form form2 in Controls) {
594 if (form2 == form || form2.window_state != FormWindowState.Minimized)
597 if (form2.Bounds.IntersectsWith(rect)) {
603 currentx += rect.Width;
604 if (currentx + rect.Width > Right) {
606 currenty -= rect.Height;
610 wm.IconicBounds = rect;
611 form.Bounds = wm.IconicBounds;
616 internal void ChildFormClosed (Form form)
618 if (Controls.Count > 1) {
619 Form next = (Form) Controls [1];
620 if (form.WindowState == FormWindowState.Maximized)
621 next.WindowState = FormWindowState.Maximized;
622 ActivateChild (next);
625 form.Visible = false;
626 Controls.Remove (form);
628 if (Controls.Count == 0) {
629 ((MdiWindowManager) form.window_manager).RaiseDeactivate ();
632 if (Controls.Count == 0) {
633 XplatUI.RequestNCRecalc (Parent.Handle);
634 ParentForm.PerformLayout ();
637 // If we closed the last child, unmerge the menus.
638 // If it's not the last child, the menu will be unmerged
639 // when another child takes focus.
640 MenuStrip parent_menu = form.MdiParent.MainMenuStrip;
642 if (parent_menu != null)
643 if (parent_menu.IsCurrentlyMerged)
644 ToolStripManager.RevertMerge (parent_menu);
648 SetParentText (false);
651 internal void ActivateNextChild ()
653 if (Controls.Count < 1)
655 if (Controls.Count == 1 && Controls[0] == ActiveMdiChild)
658 Form front = (Form) Controls [0];
659 Form form = (Form) Controls [1];
661 ActivateChild (form);
665 internal void ActivatePreviousChild ()
667 if (Controls.Count <= 1)
670 Form back = (Form) Controls [Controls.Count - 1];
672 ActivateChild (back);
675 internal void ActivateChild (Form form)
677 if (Controls.Count < 1)
680 if (ParentForm.is_changing_visible_state > 0)
683 Form current = (Form) Controls [0];
684 bool raise_deactivate = ParentForm.ActiveControl == current;
686 // We want to resize the new active form before it is
687 // made active to avoid flickering. Can't do it in the
688 // normal way (form.WindowState = Maximized) since it's not
689 // active yet and everything would just return to before.
690 // We also won't suspend layout, this way the layout will
691 // happen before the form is made active (and in many cases
692 // before it is visible, which avoids flickering as well).
693 MdiWindowManager wm = (MdiWindowManager)form.WindowManager;
695 if (current.WindowState == FormWindowState.Maximized && form.WindowState != FormWindowState.Maximized && form.Visible) {
696 FormWindowState old_state = form.window_state;
697 SetWindowState (form, old_state, FormWindowState.Maximized, true);
698 wm.was_minimized = form.window_state == FormWindowState.Minimized;
699 form.window_state = FormWindowState.Maximized;
700 SetParentText (false);
703 form.BringToFront ();
704 form.SendControlFocus (form);
705 SetWindowStates (wm);
706 if (current != form) {
707 form.has_focus = false;
708 if (current.IsHandleCreated)
709 XplatUI.InvalidateNC (current.Handle);
710 if (form.IsHandleCreated)
711 XplatUI.InvalidateNC (form.Handle);
712 if (raise_deactivate) {
713 MdiWindowManager current_wm = (MdiWindowManager) current.window_manager;
714 current_wm.RaiseDeactivate ();
718 active_child = (Form) Controls [0];
720 if (active_child.Visible) {
721 bool raise_activated = ParentForm.ActiveControl != active_child;
722 ParentForm.ActiveControl = active_child;
723 if (raise_activated) {
724 MdiWindowManager active_wm = (MdiWindowManager) active_child.window_manager;
725 active_wm.RaiseActivated ();
730 internal override IntPtr AfterTopMostControl ()
732 // order of scrollbars:
735 // bottom = horizontal
736 if (hbar != null && hbar.Visible)
738 // no need to check for sizegrip since it will only
739 // be visible if hbar is visible.
740 if (vbar != null && vbar.Visible)
743 return base.AfterTopMostControl ();
746 internal bool SetWindowStates (MdiWindowManager wm)
749 MDI WindowState behaviour:
750 - If the active window is maximized, all other maximized windows are normalized.
751 - If a normal window gets focus and the original active window was maximized,
752 the normal window gets maximized and the original window gets normalized.
753 - If a minimized window gets focus and the original window was maximized,
754 the minimzed window gets maximized and the original window gets normalized.
755 If the ex-minimized window gets deactivated, it will be normalized.
759 if (setting_windowstates) {
766 bool is_active = wm.IsActive;
767 bool maximize_this = false;
773 ArrayList minimize_these = new ArrayList ();
774 ArrayList normalize_these = new ArrayList ();
776 setting_windowstates = true;
777 foreach (Form frm in mdi_child_list) {
780 } else if (!frm.Visible){
783 if (frm.WindowState == FormWindowState.Maximized && is_active) {
784 maximize_this = true;
785 if (((MdiWindowManager) frm.window_manager).was_minimized) {
786 minimize_these.Add (frm);
788 normalize_these.Add (frm);
793 if (maximize_this && form.WindowState != FormWindowState.Maximized) {
794 wm.was_minimized = form.window_state == FormWindowState.Minimized;
795 form.WindowState = FormWindowState.Maximized;
798 foreach (Form frm in minimize_these)
799 frm.WindowState = FormWindowState.Minimized;
801 foreach (Form frm in normalize_these)
802 frm.WindowState = FormWindowState.Normal;
805 SetParentText (false);
807 XplatUI.RequestNCRecalc (ParentForm.Handle);
808 XplatUI.RequestNCRecalc (Handle);
812 setting_windowstates = false;
815 if (form.MdiParent.MainMenuStrip != null)
816 form.MdiParent.MainMenuStrip.RefreshMdiItems ();
818 // Implicit menu strip merging
819 // - When child is activated
820 // - Parent form must have a MainMenuStrip
821 // - Find the first menustrip on the child
823 MenuStrip parent_menu = form.MdiParent.MainMenuStrip;
825 if (parent_menu != null) {
826 if (parent_menu.IsCurrentlyMerged)
827 ToolStripManager.RevertMerge (parent_menu);
829 MenuStrip child_menu = null;
831 foreach (Control c in form.Controls)
833 child_menu = (MenuStrip)c;
835 if (child_menu != null)
836 ToolStripManager.Merge (child_menu, parent_menu);
840 return maximize_this;
843 internal void SetWindowState (Form form, FormWindowState old_window_state, FormWindowState new_window_state, bool is_activating_child)
845 bool mdiclient_layout;
847 MdiWindowManager wm = (MdiWindowManager) form.window_manager;
849 if (!is_activating_child && new_window_state == FormWindowState.Maximized && !wm.IsActive) {
850 ActivateChild (form);
854 if (SetWindowStates (wm))
857 if (old_window_state == new_window_state)
860 if (old_window_state == FormWindowState.Normal)
861 wm.NormalBounds = form.Bounds;
863 mdiclient_layout = old_window_state == FormWindowState.Maximized || new_window_state == FormWindowState.Maximized;
865 switch (new_window_state) {
866 case FormWindowState.Minimized:
867 ArrangeIconicWindows (false);
869 case FormWindowState.Maximized:
870 form.Bounds = wm.MaximizedBounds;
872 case FormWindowState.Normal:
873 form.Bounds = wm.NormalBounds;
877 wm.UpdateWindowDecorations (new_window_state);
881 if (mdiclient_layout)
882 Parent.PerformLayout ();
884 XplatUI.RequestNCRecalc (Parent.Handle);
885 XplatUI.RequestNCRecalc (form.Handle);
886 if (!setting_windowstates)
889 internal int ChildrenCreated {
890 get { return mdi_created; }
891 set { mdi_created = value; }
894 internal Form ActiveMdiChild {
897 if (!ParentForm.Visible)
900 if (Controls.Count < 1)
903 if (!ParentForm.IsHandleCreated)
906 if (!ParentForm.has_been_visible)
909 if (!ParentForm.Visible)
913 for (int i = 0; i < Controls.Count; i++) {
914 if (Controls [i].Visible) {
915 active_child = (Form) Controls [i];
922 ActivateChild (value);
926 internal void ActivateActiveMdiChild ()
928 if (ParentForm.is_changing_visible_state > 0)
931 for (int i = 0; i < Controls.Count; i++) {
932 if (Controls [i].Visible) {
933 ActivateChild ((Form) Controls [i]);