* MdiWindowManager.cs: Update function name.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / MdiClient.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 //
26
27 // NOT COMPLETE
28
29 using System.Collections;
30 using System.ComponentModel;
31 using System.Drawing;
32 using System.Runtime.InteropServices;
33
34 namespace System.Windows.Forms {
35 #if NET_2_0
36         [ComVisible (true)]
37         [ClassInterface(ClassInterfaceType.AutoDispatch)]
38 #endif
39         [DesignTimeVisible(false)]
40         [ToolboxItem(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;
57
58                 #endregion      // Local Variables
59
60                 #region Public Classes
61 #if NET_2_0
62                 [ComVisible (false)]
63 #endif
64                 public new class ControlCollection : Control.ControlCollection {
65
66                         private MdiClient owner;
67                         
68                         public ControlCollection(MdiClient owner) : base(owner) {
69                                 this.owner = owner;
70                         }
71
72                         public override void Add(Control value) {
73                                 if ((value is Form) == false || !(((Form)value).IsMdiChild)) {
74                                         throw new ArgumentException("Form must be MdiChild");
75                                 }
76                                 owner.mdi_child_list.Add (value);
77                                 base.Add (value);
78
79                                 // newest member is the active one
80                                 Form form = (Form) value;
81                                 owner.ActiveMdiChild = form;
82                         }
83
84                         public override void Remove(Control value)
85                         {
86                                 Form form = value as Form;
87                                 if (form != null) {
88                                         MdiWindowManager wm = form.WindowManager as MdiWindowManager;
89                                         if (wm != null) {
90                                                 form.Closed -= wm.form_closed_handler;
91                                         }
92                                 }
93
94                                 owner.mdi_child_list.Remove (value);
95                                 base.Remove (value);
96                         }
97                 }
98                 #endregion      // Public Classes
99
100                 #region Public Constructors
101                 public MdiClient()
102                 {
103                         mdi_child_list = new ArrayList ();
104                         BackColor = SystemColors.AppWorkspace;
105                         Dock = DockStyle.Fill;
106                         SetStyle (ControlStyles.Selectable, false);
107                 }
108                 #endregion      // Public Constructors
109
110                 internal void SendFocusToActiveChild ()
111                 {
112                         Form active = this.ActiveMdiChild;
113                         if (active == null) {
114                                 ParentForm.SendControlFocus (this);
115                         } else {
116                                 active.SendControlFocus (active);
117                                 ParentForm.ActiveControl = active;
118                         }
119                 }
120
121                 internal bool HorizontalScrollbarVisible {
122                         get { return hbar != null && hbar.Visible; }
123                 }
124                 internal bool VerticalScrollbarVisible {
125                         get { return vbar != null && vbar.Visible; }
126                 }
127
128                 internal void SetParentText(bool text_changed)
129                 {
130                         if (setting_form_text)
131                                 return;
132
133                         setting_form_text = true;
134
135                         if (text_changed)
136                                 form_text = ParentForm.Text;
137
138                         if (ParentForm.ActiveMaximizedMdiChild == null) {
139                                 ParentForm.Text = form_text;
140                         } else {
141                                 string childText = ParentForm.ActiveMaximizedMdiChild.form.Text;
142                                 if (childText.Length > 0) {
143                                         ParentForm.Text = form_text + " - [" + ParentForm.ActiveMaximizedMdiChild.form.Text + "]";
144                                 } else {
145                                         ParentForm.Text = form_text;
146                                 }
147                         }
148
149                         setting_form_text = false;
150                 }
151
152                 internal override void OnPaintBackgroundInternal (PaintEventArgs pe)
153                 {
154                         if (BackgroundImage != null)
155                                 return;
156
157                         if (Parent == null || Parent.BackgroundImage == null)
158                                 return;
159                         Parent.PaintControlBackground (pe);
160                 }
161
162                 internal Form ParentForm {
163                         get { return (Form) Parent; }
164                 }
165
166                 protected override Control.ControlCollection CreateControlsInstance ()
167                 {
168                         return new MdiClient.ControlCollection (this);
169                 }
170
171                 protected override void WndProc(ref Message m) {
172                         switch ((Msg)m.Msg) {
173                         case Msg.WM_NCPAINT:
174                                 PaintEventArgs pe = XplatUI.PaintEventStart (Handle, false);
175
176                                 Rectangle clip;
177                                 clip = new Rectangle (0, 0, Width, Height);
178
179                                 ControlPaint.DrawBorder3D (pe.Graphics, clip, Border3DStyle.Sunken);
180                                 XplatUI.PaintEventEnd (Handle, false);
181                                 m.Result = IntPtr.Zero;
182                                 return ;
183                         }
184
185                         base.WndProc (ref m);
186                 }
187
188                 protected override void OnResize (EventArgs e)
189                 {
190                         base.OnResize (e);
191
192                         if (Parent != null && Parent.IsHandleCreated)
193                                 XplatUI.InvalidateNC (Parent.Handle);
194                         // Should probably make this into one loop
195                         SizeScrollBars ();
196                         ArrangeWindows ();
197                 }
198 #if NET_2_0
199                 [System.ComponentModel.EditorBrowsable (EditorBrowsableState.Never)]
200 #endif
201                 protected override void ScaleCore (float dx, float dy)
202                 {
203                         base.ScaleCore (dx, dy);
204                 }
205
206                 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
207                 {
208                         base.SetBoundsCore (x, y, width, height, specified);
209                 }
210
211                 #region Public Instance Properties
212                 [Localizable(true)]
213                 public override System.Drawing.Image BackgroundImage {
214                         get {
215                                 return base.BackgroundImage;
216                         }
217                         set {
218                                 base.BackgroundImage = value;
219                         }
220                 }
221
222 #if NET_2_0
223                 [EditorBrowsable (EditorBrowsableState.Never)]
224                 [Browsable (false)]
225                 public override ImageLayout BackgroundImageLayout {
226                         get {
227                                 return base.BackgroundImageLayout;
228                         }
229                         set {
230                                 base.BackgroundImageLayout = value;
231                         }
232                 }
233 #endif
234
235                 public Form [] MdiChildren {
236                         get {
237                                 if (mdi_child_list == null)
238                                         return new Form [0];
239                                 return (Form []) mdi_child_list.ToArray (typeof (Form));
240                         }
241                 }
242                 #endregion      // Public Instance Properties
243
244 #region Protected Instance Properties
245                 protected override CreateParams CreateParams {
246                         get {
247                                 CreateParams result = base.CreateParams;
248                                 result.ExStyle |= (int) WindowExStyles.WS_EX_CLIENTEDGE;
249                                 return result;
250                         }
251                 }
252                 #endregion      // Protected Instance Properties
253
254                 #region Public Instance Methods
255                 public void LayoutMdi (MdiLayout value) {
256
257                         int max_width = Int32.MaxValue;
258                         int max_height = Int32.MaxValue;
259
260                         if (Parent != null) {
261                                 max_width = Parent.Width;
262                                 max_height = Parent.Height;
263                         }
264
265                         switch (value) {
266                         case MdiLayout.Cascade: {
267                                 int i = 0;
268                                 for (int c = Controls.Count - 1; c >= 0; c--) {
269                                         Form form = (Form) Controls [c];
270
271                                         if (form.WindowState == FormWindowState.Minimized)
272                                                 continue;
273                 
274                                         int l = 22 * i;
275                                         int t = 22 * i;
276
277                                         if (i != 0 && (l + form.Width > max_width || t + form.Height > max_height)) {
278                                                 i = 0;
279                                                 l = 22 * i;
280                                                 t = 22 * i;
281                                         }
282
283                                         form.Left = l;
284                                         form.Top = t;
285
286                                         i++;
287                                 }
288                                 break;
289                                 }
290                         case MdiLayout.ArrangeIcons:
291                                 ArrangeIconicWindows (true);
292                                 break;
293                         case MdiLayout.TileHorizontal:
294                         case MdiLayout.TileVertical: {
295                                 // First count number of windows to tile
296                                 int total = 0;
297                                 for (int i = 0; i < Controls.Count; i++) {
298                                         Form form = Controls [i] as Form;
299                                         
300                                         if (form == null)
301                                                 continue;
302                                         
303                                         if (!form.Visible)
304                                                 continue;
305                                         
306                                         if (form.WindowState == FormWindowState.Minimized)
307                                                 continue;
308                                                 
309                                         total++;
310                                 }
311                                 if (total <= 0)
312                                         return;
313
314                                 // Calculate desired height and width
315                                 Size newSize;
316                                 Size offset;
317                                 if (value == MdiLayout.TileHorizontal) {
318                                         newSize = new Size (ClientSize.Width, ClientSize.Height / total);
319                                         offset = new Size (0, newSize.Height);
320                                 } else {
321                                         newSize = new Size (ClientSize.Width / total, ClientSize.Height);
322                                         offset = new Size (newSize.Width, 0);
323                                 }
324                                 
325                                 // Loop again and set the size and location.
326                                 Point nextLocation = Point.Empty;
327                                 
328                                 for (int i = 0; i < Controls.Count; i++) {
329                                         Form form = Controls [i] as Form;
330
331                                         if (form == null)
332                                                 continue;
333
334                                         if (!form.Visible)
335                                                 continue;
336
337                                         if (form.WindowState == FormWindowState.Minimized)
338                                                 continue;
339
340                                         form.Size = newSize;
341                                         form.Location = nextLocation;
342                                         nextLocation += offset;
343                                 }
344                                 
345                                 break;
346                                 }
347                         }
348                 }
349                 #endregion      // Public Instance Methods
350
351                 #region Protected Instance Methods
352                 #endregion      // Protected Instance Methods
353
354                 internal void SizeScrollBars ()
355                 {
356                         if (lock_sizing)
357                                 return;
358                         
359                         if (!IsHandleCreated)
360                                 return;
361
362                         if (Controls.Count == 0 || ((Form) Controls [0]).WindowState == FormWindowState.Maximized) {
363                                 if (hbar != null)
364                                         hbar.Visible = false;
365                                 if (vbar != null)
366                                         vbar.Visible = false;
367                                 if (sizegrip != null)
368                                         sizegrip.Visible = false;
369                                 return;
370                         }
371
372                         int right = 0;
373                         int left = 0;
374                         int top = 0;
375                         int bottom = 0;
376
377                         foreach (Form child in Controls) {
378                                 if (!child.Visible)
379                                         continue;
380                                 if (child.Right > right)
381                                         right = child.Right;
382                                 if (child.Left < left) {
383                                         left = child.Left;
384                                 }
385                                 
386                                 if (child.Bottom > bottom)
387                                         bottom = child.Bottom;
388                                 if (child.Top < 0) {
389                                         top = child.Top;
390                                 }
391                         }
392
393                         int available_width = ClientSize.Width;
394                         int available_height = ClientSize.Height;
395
396                         bool need_hbar = false;
397                         bool need_vbar = false;
398
399                         if (right - left > available_width || left < 0) {
400                                 need_hbar = true;
401                                 available_height -= SystemInformation.HorizontalScrollBarHeight;
402                         }
403                         if (bottom - top > available_height || top < 0) {
404                                 need_vbar = true;
405                                 available_width -= SystemInformation.VerticalScrollBarWidth;
406
407                                 if (!need_hbar && (right - left > available_width || left < 0)) {
408                                         need_hbar = true;
409                                         available_height -= SystemInformation.HorizontalScrollBarHeight;
410                                 }
411                         }
412                         
413                         if (need_hbar) {
414                                 if (hbar == null) {
415                                         hbar = new ImplicitHScrollBar ();
416                                         Controls.AddImplicit (hbar);
417                                 }
418                                 hbar.Visible = true;
419                                 CalcHBar (left, right, need_vbar);
420                         } else if (hbar != null)
421                                 hbar.Visible = false;
422
423                         if (need_vbar) {
424                                 if (vbar == null) {
425                                         vbar = new ImplicitVScrollBar ();
426                                         Controls.AddImplicit (vbar);
427                                 }
428                                 vbar.Visible = true;
429                                 CalcVBar (top, bottom, need_hbar);
430                         } else if (vbar != null)
431                                 vbar.Visible = false;
432
433                         if (need_hbar && need_vbar) {
434                                 if (sizegrip == null) {
435                                         sizegrip = new SizeGrip (this.ParentForm);
436                                         Controls.AddImplicit (sizegrip);
437                                 }
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;
443                         }
444                 }
445
446                 private void CalcHBar (int left, int right, bool vert_vis)
447                 {
448                         initializing_scrollbars = true;
449
450                         hbar.Left = 0;
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);
456                         hbar.Value = 0;
457                         hbar_value = 0;
458                         hbar.ValueChanged += new EventHandler (HBarValueChanged);
459                         XplatUI.SetZOrder (hbar.Handle, IntPtr.Zero, true, false);
460                         
461                         initializing_scrollbars = false;
462                 }
463
464                 private void CalcVBar (int top, int bottom, bool horz_vis)
465                 {
466                         initializing_scrollbars = true;
467                         
468                         vbar.Top = 0;
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);
474                         vbar.Value = 0;
475                         vbar_value = 0;
476                         vbar.ValueChanged += new EventHandler (VBarValueChanged);
477                         XplatUI.SetZOrder (vbar.Handle, IntPtr.Zero, true, false);
478                         
479                         initializing_scrollbars = false;
480                 }
481
482                 private void HBarValueChanged (object sender, EventArgs e)
483                 {
484                         if (initializing_scrollbars)
485                                 return;
486                         
487                         if (hbar.Value == hbar_value)
488                                 return;
489
490                         lock_sizing = true;
491
492                         try {
493                                 int diff = hbar_value - hbar.Value;
494                                 foreach (Form child in Controls) {
495                                         child.Left += diff;
496                                 }
497                         } finally {
498                                 lock_sizing = false;
499                         }
500
501                         hbar_value = hbar.Value;
502                 }
503
504                 private void VBarValueChanged (object sender, EventArgs e)
505                 {
506                         if (initializing_scrollbars)
507                                 return;
508                                 
509                         if (vbar.Value == vbar_value)
510                                 return;
511
512                         lock_sizing = true;
513
514                         try {
515                                 int diff = vbar_value - vbar.Value;
516                                 foreach (Form child in Controls) {
517                                         child.Top += diff;
518                                 }
519                         } finally {
520                                 lock_sizing = false;
521                         }
522
523                         vbar_value = vbar.Value;
524                 }
525
526                 private void ArrangeWindows ()
527                 {
528                         if (!IsHandleCreated)
529                                 return;
530                                 
531                         int change = 0;
532                         if (prev_bottom != -1)
533                                 change = Bottom - prev_bottom;
534
535                         foreach (Control c in Controls) {
536                                 Form child = c as Form;
537
538                                 if (c == null || !child.Visible)
539                                         continue;
540
541                                 MdiWindowManager wm = child.WindowManager as MdiWindowManager;
542                                 if (wm.GetWindowState () == FormWindowState.Maximized)
543                                         child.Bounds = wm.MaximizedBounds;
544
545                                 if (wm.GetWindowState () == FormWindowState.Minimized) {
546                                         child.Top += change;
547                                 }
548                                         
549                         }
550
551                         prev_bottom = Bottom;
552                 }
553
554                 internal void ArrangeIconicWindows (bool rearrange_all)
555                 {
556                         Rectangle rect = Rectangle.Empty;
557
558                         lock_sizing = true;
559                         foreach (Form form in Controls) {
560                                 if (form.WindowState != FormWindowState.Minimized)
561                                         continue;
562
563                                 MdiWindowManager wm = (MdiWindowManager) form.WindowManager;
564                                 
565                                 if (wm.IconicBounds != Rectangle.Empty && !rearrange_all) {
566                                         if (form.Bounds != wm.IconicBounds)
567                                                 form.Bounds = wm.IconicBounds;
568                                         continue;
569                                 }
570                                 
571                                 bool success = true;
572                                 int startx, starty, currentx, currenty;
573
574                                 rect.Size = wm.IconicSize;
575                                 
576                                 startx = 0;
577                                 starty = ClientSize.Height - rect.Height;
578                                 currentx = startx;
579                                 currenty = starty;
580                                 
581                                 do {
582                                         rect.X = currentx;
583                                         rect.Y = currenty;
584                                         success = true;
585                                         foreach (Form form2 in Controls) {
586                                                 if (form2 == form || form2.window_state != FormWindowState.Minimized)
587                                                         continue;
588                                                 
589                                                 if (form2.Bounds.IntersectsWith(rect)) {
590                                                         success = false;
591                                                         break;
592                                                 }
593                                         }
594                                         if (!success) { 
595                                                 currentx += rect.Width;
596                                                 if (currentx + rect.Width > Right) {
597                                                         currentx = startx;
598                                                         currenty -= rect.Height;
599                                                 } 
600                                         }
601                                 } while (!success);
602                                 wm.IconicBounds = rect;
603                                 form.Bounds = wm.IconicBounds;
604                         }
605                         lock_sizing = false;
606                 }
607
608                 internal void ChildFormClosed (Form form)
609                 {
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);
615                         }
616
617                         Controls.Remove (form);
618
619                         if (Controls.Count == 0) {
620                                 XplatUI.RequestNCRecalc (Parent.Handle);
621                                 ParentForm.PerformLayout ();
622                         }
623                         SizeScrollBars ();
624                         SetParentText (false);
625                 }
626
627                 internal void ActivateNextChild ()
628                 {
629                         if (Controls.Count < 1)
630                                 return;
631                         if (Controls.Count == 1 && Controls[0] == ActiveMdiChild)
632                                 return;
633                                 
634                         Form front = (Form) Controls [0];
635                         Form form = (Form) Controls [1];
636
637                         ActivateChild (form);
638                         front.SendToBack ();
639                 }
640
641                 internal void ActivatePreviousChild ()
642                 {
643                         if (Controls.Count <= 1)
644                                 return;
645                         
646                         Form back = (Form) Controls [Controls.Count - 1];
647                         
648                         ActivateChild (back);
649                 }
650
651                 internal void ActivateChild (Form form)
652                 {
653                         if (Controls.Count < 1)
654                                 return;
655                         
656                         if (ParentForm.is_changing_visible_state)
657                                 return;
658                         
659                         Form current = (Form) Controls [0];
660
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);
675                         }
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);
685                         }
686                         active_child = (Form) Controls [0];
687                         
688                         if (active_child.Visible)
689                                 ParentForm.ActiveControl = active_child;
690                 }
691
692                 internal override IntPtr AfterTopMostControl ()
693                 {
694                         // order of scrollbars:
695                         // top = vertical
696                         //       sizegrid
697                         // bottom = horizontal
698                         if (hbar != null && hbar.Visible)
699                                 return hbar.Handle;
700                         // no need to check for sizegrip since it will only
701                         // be visible if hbar is visible.
702                         if (vbar != null && vbar.Visible)
703                                 return vbar.Handle;
704                                 
705                         return base.AfterTopMostControl ();
706                 }
707                 
708                 internal bool SetWindowStates (MdiWindowManager wm)
709                 {
710                 /*
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.
718                 */
719                         Form form = wm.form;
720
721                         if (setting_windowstates) {
722                                 return false;
723                         }
724                         
725                         if (!form.Visible)
726                                 return false;
727                         
728                         bool is_active = wm.IsActive;
729                         bool maximize_this = false;
730                         
731                         if (!is_active){
732                                 return false;
733                         }
734                         
735                         ArrayList minimize_these = new ArrayList ();
736                         ArrayList normalize_these = new ArrayList ();
737
738                         setting_windowstates = true;
739                         foreach (Form frm in mdi_child_list) {
740                                 if (frm == form) {
741                                         continue;
742                                 } else if (!frm.Visible){
743                                         continue;
744                                 }
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); 
749                                         } else {
750                                                 normalize_these.Add (frm); 
751                                         }
752                                 }
753                         }
754
755                         if (maximize_this && form.WindowState != FormWindowState.Maximized) {
756                                 wm.was_minimized = form.window_state == FormWindowState.Minimized;
757                                 form.WindowState = FormWindowState.Maximized;
758                         }
759                         
760                         foreach (Form frm in minimize_these)
761                                 frm.WindowState = FormWindowState.Minimized;
762
763                         foreach (Form frm in normalize_these)
764                                 frm.WindowState = FormWindowState.Normal;
765
766
767                         SetParentText (false);
768                         
769                         XplatUI.RequestNCRecalc (ParentForm.Handle);
770                         XplatUI.RequestNCRecalc (Handle);
771
772                         SizeScrollBars ();
773
774                         setting_windowstates = false;
775
776 #if NET_2_0
777                         if (form.MdiParent.MainMenuStrip != null)
778                                 form.MdiParent.MainMenuStrip.RefreshMdiItems ();
779 #endif
780
781                         return maximize_this;
782                 }
783
784                 internal void SetWindowState (Form form, FormWindowState old_window_state, FormWindowState new_window_state, bool is_activating_child)
785                 {
786                         bool mdiclient_layout;
787
788                         MdiWindowManager wm = (MdiWindowManager) form.window_manager;
789
790                         if (!is_activating_child && new_window_state == FormWindowState.Maximized && !wm.IsActive) {
791                                 ActivateChild (form);
792                                 return;
793                         }
794
795                         if (SetWindowStates (wm))
796                                 return;
797
798                         if (old_window_state == new_window_state)
799                                 return;
800                                 
801                         if (old_window_state == FormWindowState.Normal)
802                                 wm.NormalBounds = form.Bounds;
803
804                         mdiclient_layout = old_window_state == FormWindowState.Maximized || new_window_state == FormWindowState.Maximized;
805
806                         switch (new_window_state) {
807                         case FormWindowState.Minimized:
808                                 ArrangeIconicWindows (false);
809                                 break;
810                         case FormWindowState.Maximized:
811                                 form.Bounds = wm.MaximizedBounds;
812                                 break;
813                         case FormWindowState.Normal:
814                                 form.Bounds = wm.NormalBounds;
815                                 break;
816                         }
817
818                         wm.UpdateWindowDecorations (new_window_state);
819
820                         form.ResetCursor ();
821
822                         if (mdiclient_layout)
823                                 Parent.PerformLayout ();
824
825                         XplatUI.RequestNCRecalc (Parent.Handle);
826                         XplatUI.RequestNCRecalc (form.Handle);
827                         if (!setting_windowstates)
828                                 SizeScrollBars ();
829                 }
830                 internal int ChildrenCreated {
831                         get { return mdi_created; }
832                         set { mdi_created = value; }
833                 }
834
835                 internal Form ActiveMdiChild {
836                         get {
837 #if NET_2_0
838                                 if (!ParentForm.Visible)
839                                         return null;
840 #endif
841                                 if (Controls.Count < 1)
842                                         return null;
843                                         
844                                 if (!ParentForm.IsHandleCreated)
845                                         return null;
846                                 
847                                 if (!ParentForm.has_been_visible)
848                                         return null;
849                                         
850                                 if (!ParentForm.Visible)
851                                         return active_child;
852                                 
853                                 active_child = null;
854                                 for (int i = 0; i < Controls.Count; i++) {
855                                         if (Controls [i].Visible) {
856                                                 active_child = (Form) Controls [i];
857                                                 break;
858                                         }
859                                 }
860                                 return active_child;
861                         }
862                         set {
863                                 ActivateChild (value);
864                         }
865                 }
866                 
867                 internal void ActivateActiveMdiChild ()
868                 {
869                         if (ParentForm.is_changing_visible_state)
870                                 return;
871                                 
872                         for (int i = 0; i < Controls.Count; i++) {
873                                 if (Controls [i].Visible) {
874                                         ActivateChild ((Form) Controls [i]);
875                                         return;
876                                 }
877                         }
878                 }
879         }
880 }
881