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 {
35 [DesignTimeVisible(false)]
37 public sealed class MdiClient : Control {
38 #region Local Variables
39 private int mdi_created;
40 private ImplicitHScrollBar hbar;
41 private ImplicitVScrollBar vbar;
42 private SizeGrip sizegrip;
43 private int hbar_value;
44 private int vbar_value;
45 private bool lock_sizing;
46 private bool initializing_scrollbars;
47 private int prev_bottom;
48 private bool setting_windowstates = false;
49 internal ArrayList mdi_child_list;
50 private string form_text;
51 private bool setting_form_text;
52 private Form active_child;
54 #endregion // Local Variables
56 #region Public Classes
57 public new class ControlCollection : Control.ControlCollection {
59 private MdiClient owner;
61 public ControlCollection(MdiClient owner) : base(owner) {
65 public override void Add(Control value) {
66 if ((value is Form) == false || !(((Form)value).IsMdiChild)) {
67 throw new ArgumentException("Form must be MdiChild");
69 owner.mdi_child_list.Add (value);
72 // newest member is the active one
73 Form form = (Form) value;
74 owner.ActiveMdiChild = form;
77 public override void Remove(Control value)
79 Form form = value as Form;
81 MdiWindowManager wm = form.WindowManager as MdiWindowManager;
83 form.Closed -= wm.form_closed_handler;
87 owner.mdi_child_list.Remove (value);
91 #endregion // Public Classes
93 #region Public Constructors
96 mdi_child_list = new ArrayList ();
97 BackColor = SystemColors.AppWorkspace;
98 Dock = DockStyle.Fill;
99 SetStyle (ControlStyles.Selectable, false);
101 #endregion // Public Constructors
104 internal void SetParentText(bool text_changed)
106 if (setting_form_text)
109 setting_form_text = true;
112 form_text = ParentForm.Text;
114 if (ParentForm.ActiveMaximizedMdiChild == null) {
115 ParentForm.Text = form_text;
117 string childText = ParentForm.ActiveMaximizedMdiChild.form.Text;
118 if (childText.Length > 0) {
119 ParentForm.Text = form_text + " - [" + ParentForm.ActiveMaximizedMdiChild.form.Text + "]";
121 ParentForm.Text = form_text;
125 setting_form_text = false;
128 internal override void OnPaintBackgroundInternal (PaintEventArgs pe)
130 if (BackgroundImage != null)
133 if (Parent == null || Parent.BackgroundImage == null)
135 Parent.PaintControlBackground (pe);
138 internal Form ParentForm {
139 get { return (Form) Parent; }
142 protected override Control.ControlCollection CreateControlsInstance ()
144 return new MdiClient.ControlCollection (this);
147 protected override void WndProc(ref Message m) {
149 switch ((Msg) m.Msg) {
151 Console.WriteLine ("ignoring paint");
156 switch ((Msg)m.Msg) {
157 case Msg.WM_NCCALCSIZE:
158 XplatUIWin32.NCCALCSIZE_PARAMS ncp;
160 if (m.WParam == (IntPtr) 1) {
161 ncp = (XplatUIWin32.NCCALCSIZE_PARAMS) Marshal.PtrToStructure (m.LParam,
162 typeof (XplatUIWin32.NCCALCSIZE_PARAMS));
167 ncp.rgrc1.bottom -= bw;
168 ncp.rgrc1.left += bw;
169 ncp.rgrc1.right -= bw;
171 Marshal.StructureToPtr (ncp, m.LParam, true);
177 PaintEventArgs pe = XplatUI.PaintEventStart (Handle, false);
180 clip = new Rectangle (0, 0, Width, Height);
182 ControlPaint.DrawBorder3D (pe.Graphics, clip, Border3DStyle.Sunken);
183 XplatUI.PaintEventEnd (Handle, false);
184 m.Result = IntPtr.Zero;
188 base.WndProc (ref m);
191 protected override void OnResize (EventArgs e)
196 XplatUI.InvalidateNC (Parent.Handle);
197 // Should probably make this into one loop
202 protected override void ScaleCore (float dx, float dy)
204 base.ScaleCore (dx, dy);
207 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
209 base.SetBoundsCore (x, y, width, height, specified);
212 #region Public Instance Properties
214 public override System.Drawing.Image BackgroundImage {
216 return base.BackgroundImage;
219 base.BackgroundImage = value;
223 public Form [] MdiChildren {
225 if (mdi_child_list == null)
227 return (Form []) mdi_child_list.ToArray (typeof (Form));
230 #endregion // Public Instance Properties
232 #region Protected Instance Properties
233 protected override CreateParams CreateParams {
235 return base.CreateParams;
238 #endregion // Protected Instance Properties
240 #region Public Instance Methods
241 public void LayoutMdi (MdiLayout value) {
243 int max_width = Int32.MaxValue;
244 int max_height = Int32.MaxValue;
246 if (Parent != null) {
247 max_width = Parent.Width;
248 max_height = Parent.Height;
252 case MdiLayout.Cascade:
254 for (int c = Controls.Count - 1; c >= 0; c--) {
255 Form form = (Form) Controls [c];
260 if (i != 0 && (l + form.Width > max_width || t + form.Height > max_height)) {
273 throw new NotImplementedException();
276 #endregion // Public Instance Methods
278 #region Protected Instance Methods
279 #endregion // Protected Instance Methods
281 internal void SizeScrollBars ()
286 if (Controls.Count == 0 || ((Form) Controls [0]).WindowState == FormWindowState.Maximized) {
288 hbar.Visible = false;
290 vbar.Visible = false;
291 if (sizegrip != null)
292 sizegrip.Visible = false;
296 bool hbar_required = false;
297 bool vbar_required = false;
304 foreach (Form child in Controls) {
307 if (child.Right > right)
309 if (child.Left < left) {
310 hbar_required = true;
314 if (child.Bottom > bottom)
315 bottom = child.Bottom;
317 vbar_required = true;
322 int first_right = Width;
323 int first_bottom = Height;
324 int right_edge = first_right;
325 int bottom_edge = first_bottom;
327 int prev_bottom_edge;
329 bool need_hbar = false;
330 bool need_vbar = false;
333 prev_right_edge = right_edge;
334 prev_bottom_edge = bottom_edge;
336 if (hbar_required || right > right_edge) {
338 bottom_edge = first_bottom - SystemInformation.HorizontalScrollBarHeight;
341 bottom_edge = first_bottom;
344 if (vbar_required || bottom > bottom_edge) {
346 right_edge = first_right - SystemInformation.VerticalScrollBarWidth;
349 right_edge = first_right;
352 } while (right_edge != prev_right_edge || bottom_edge != prev_bottom_edge);
356 hbar = new ImplicitHScrollBar ();
357 Controls.AddImplicit (hbar);
360 CalcHBar (left, right, right_edge, need_vbar);
361 } else if (hbar != null)
362 hbar.Visible = false;
366 vbar = new ImplicitVScrollBar ();
367 Controls.AddImplicit (vbar);
370 CalcVBar (top, bottom, bottom_edge, need_hbar);
371 } else if (vbar != null)
372 vbar.Visible = false;
374 if (need_hbar && need_vbar) {
375 if (sizegrip == null) {
376 sizegrip = new SizeGrip ();
377 Controls.AddImplicit (sizegrip);
379 sizegrip.Location = new Point (hbar.Right, vbar.Bottom);
380 sizegrip.Width = vbar.Width;
381 sizegrip.Height = hbar.Height;
382 sizegrip.Visible = true;
383 } else if (sizegrip != null) {
384 sizegrip.Visible = false;
388 private void CalcHBar (int left, int right, int right_edge, bool vert_vis)
390 initializing_scrollbars = true;
391 int virtual_left = Math.Min (left, 0);
392 int virtual_right = Math.Max (right, right_edge);
393 int diff = (virtual_right - virtual_left) - right_edge;
396 hbar.Top = ClientRectangle.Bottom - hbar.Height;
397 hbar.Width = ClientRectangle.Width - (vert_vis ? SystemInformation.VerticalScrollBarWidth : 0);
398 hbar.LargeChange = 50;
399 hbar.Maximum = diff + 51 + (vert_vis ? SystemInformation.VerticalScrollBarWidth : 0);
400 hbar.Value = -virtual_left;
401 hbar.ValueChanged += new EventHandler (HBarValueChanged);
402 XplatUI.SetZOrder (hbar.Handle, IntPtr.Zero, true, false);
403 initializing_scrollbars = false;
406 private void CalcVBar (int top, int bottom, int bottom_edge, bool horz_vis)
408 initializing_scrollbars = true;
409 int virtual_top = Math.Min (top, 0);
410 int virtual_bottom = Math.Max (bottom, bottom_edge);
411 int diff = (virtual_bottom - virtual_top) - bottom_edge;
414 vbar.Left = ClientRectangle.Right - vbar.Width;
415 vbar.Height = ClientRectangle.Height - (horz_vis ? SystemInformation.HorizontalScrollBarHeight : 0);
416 vbar.LargeChange = 50;
417 vbar.Minimum = virtual_top;
418 vbar.Maximum = diff + 51 + (horz_vis ? SystemInformation.HorizontalScrollBarHeight : 0);
419 vbar.ValueChanged += new EventHandler (VBarValueChanged);
420 XplatUI.SetZOrder (vbar.Handle, IntPtr.Zero, true, false);
421 initializing_scrollbars = false;
424 private void HBarValueChanged (object sender, EventArgs e)
426 if (initializing_scrollbars)
429 if (hbar.Value == hbar_value)
435 foreach (Form child in Controls) {
436 child.Left += hbar_value - hbar.Value;
442 hbar_value = hbar.Value;
446 private void VBarValueChanged (object sender, EventArgs e)
448 if (initializing_scrollbars)
451 if (vbar.Value == vbar_value)
457 foreach (Form child in Controls) {
458 child.Top += vbar_value - vbar.Value;
464 vbar_value = vbar.Value;
468 private void ArrangeWindows ()
471 if (prev_bottom != -1)
472 change = Bottom - prev_bottom;
474 foreach (Control c in Controls) {
475 Form child = c as Form;
477 if (c == null || !child.Visible)
480 MdiWindowManager wm = child.WindowManager as MdiWindowManager;
481 if (wm.GetWindowState () == FormWindowState.Maximized)
484 if (wm.GetWindowState () == FormWindowState.Minimized) {
490 prev_bottom = Bottom;
493 internal void ArrangeIconicWindows ()
498 Rectangle rect = new Rectangle (0, 0, xspacing, yspacing);
501 foreach (Form form in Controls) {
502 if (form.WindowState != FormWindowState.Minimized)
505 MdiWindowManager wm = (MdiWindowManager) form.WindowManager;
507 if (wm.IconicBounds != Rectangle.Empty) {
508 if (form.Bounds != wm.IconicBounds)
509 form.Bounds = wm.IconicBounds;
513 // Need to get the width in the loop cause some themes might have
514 // different widths for different styles
515 int bw = ThemeEngine.Current.ManagedWindowBorderWidth (wm);
517 // The extra one pixel is a cheap hack for now until we
518 // handle 0 client sizes properly in the driver
519 int height = wm.TitleBarHeight + (bw * 2) + 1;
522 int startx, starty, currentx, currenty;
525 starty = Bottom - yspacing - 1;
526 if (this.hbar != null && this.hbar.Visible)
527 starty -= this.hbar.Height;
534 rect.Height = height;
536 foreach (Form form2 in Controls) {
537 if (form2 == form || form2.window_state != FormWindowState.Minimized)
540 if (form2.Bounds.IntersectsWith(rect)) {
546 currentx += xspacing;
547 if (currentx + xspacing > Right) {
549 currenty -= Math.Max(yspacing, height);
553 wm.IconicBounds = rect;
554 form.Bounds = wm.IconicBounds;
559 internal void CloseChildForm (Form form)
561 if (Controls.Count > 1) {
562 Form next = (Form) Controls [1];
563 if (form.WindowState == FormWindowState.Maximized)
564 next.WindowState = FormWindowState.Maximized;
565 ActivateChild (next);
568 Controls.Remove (form);
571 XplatUI.RequestNCRecalc (Handle);
572 if (Controls.Count == 0) {
573 XplatUI.RequestNCRecalc (Parent.Handle);
574 ParentForm.PerformLayout ();
577 SetParentText (false);
580 internal void ActivateNextChild ()
582 if (Controls.Count < 1)
584 if (Controls.Count == 1 && Controls[0] == ActiveMdiChild)
587 Form front = (Form) Controls [0];
588 Form form = (Form) Controls [1];
591 ActivateChild (form);
594 internal void ActivateChild (Form form)
596 if (Controls.Count < 1)
599 if (ParentForm.is_changing_visible_state)
602 Form current = (Form) Controls [0];
603 form.SuspendLayout ();
604 form.BringToFront ();
605 if (vbar != null && vbar.Visible)
606 XplatUI.SetZOrder (vbar.Handle, IntPtr.Zero, true, false);
607 if (hbar != null && hbar.Visible)
608 XplatUI.SetZOrder (hbar.Handle, IntPtr.Zero, true, false);
609 SetWindowStates ((MdiWindowManager) form.window_manager);
610 form.ResumeLayout (false);
611 if (current != form) {
612 form.has_focus = false;
613 XplatUI.InvalidateNC (current.Handle);
614 XplatUI.InvalidateNC (form.Handle);
616 active_child = (Form) Controls [0];
619 internal bool SetWindowStates (MdiWindowManager wm)
622 MDI WindowState behaviour:
623 - If the active window is maximized, all other maximized windows are normalized.
624 - If a normal window gets focus and the original active window was maximized,
625 the normal window gets maximized and the original window gets normalized.
626 - If a minimized window gets focus and the original window was maximized,
627 the minimzed window gets maximized and the original window gets normalized.
628 If the ex-minimized window gets deactivated, it will be normalized.
632 if (setting_windowstates) {
639 bool is_active = wm.IsActive();
640 bool maximize_this = false;
646 setting_windowstates = true;
647 foreach (Form frm in mdi_child_list) {
650 } else if (!frm.Visible){
653 if (frm.WindowState == FormWindowState.Maximized && is_active) {
654 maximize_this = true;
655 if (((MdiWindowManager) frm.window_manager).was_minimized)
656 frm.WindowState = FormWindowState.Minimized;
658 frm.WindowState = FormWindowState.Normal;//
662 wm.was_minimized = form.window_state == FormWindowState.Minimized;
663 form.WindowState = FormWindowState.Maximized;
665 SetParentText(false);
667 XplatUI.RequestNCRecalc(ParentForm.Handle);
668 XplatUI.RequestNCRecalc (Handle);
672 setting_windowstates = false;
674 return maximize_this;
677 internal int ChildrenCreated {
678 get { return mdi_created; }
679 set { mdi_created = value; }
682 internal Form ActiveMdiChild {
685 if (!ParentForm.Visible)
688 if (Controls.Count < 1)
691 if (!ParentForm.IsHandleCreated)
694 if (!ParentForm.has_been_visible)
697 if (!ParentForm.Visible)
701 for (int i = 0; i < Controls.Count; i++) {
702 if (Controls [i].Visible) {
703 active_child = (Form) Controls [i];
710 ActivateChild (value);
714 internal void ActivateActiveMdiChild ()
716 if (ParentForm.is_changing_visible_state)
719 for (int i = 0; i < Controls.Count; i++) {
720 if (Controls [i].Visible) {
721 ActivateChild ((Form) Controls [i]);