897bb094712964b763502566d9d7169b0516306b
[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                 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
200                 {
201                         // Never change the MdiClient's location
202                         specified &= ~BoundsSpecified.Location;
203
204                         base.ScaleControl (factor, specified);
205                 }
206                 
207                 [System.ComponentModel.EditorBrowsable (EditorBrowsableState.Never)]
208 #endif
209                 protected override void ScaleCore (float dx, float dy)
210                 {
211                         base.ScaleCore (dx, dy);
212                 }
213
214                 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
215                 {
216                         base.SetBoundsCore (x, y, width, height, specified);
217                 }
218
219                 #region Public Instance Properties
220                 [Localizable(true)]
221                 public override System.Drawing.Image BackgroundImage {
222                         get {
223                                 return base.BackgroundImage;
224                         }
225                         set {
226                                 base.BackgroundImage = value;
227                         }
228                 }
229
230 #if NET_2_0
231                 [EditorBrowsable (EditorBrowsableState.Never)]
232                 [Browsable (false)]
233                 public override ImageLayout BackgroundImageLayout {
234                         get {
235                                 return base.BackgroundImageLayout;
236                         }
237                         set {
238                                 base.BackgroundImageLayout = value;
239                         }
240                 }
241 #endif
242
243                 public Form [] MdiChildren {
244                         get {
245                                 if (mdi_child_list == null)
246                                         return new Form [0];
247                                 return (Form []) mdi_child_list.ToArray (typeof (Form));
248                         }
249                 }
250                 #endregion      // Public Instance Properties
251
252 #region Protected Instance Properties
253                 protected override CreateParams CreateParams {
254                         get {
255                                 CreateParams result = base.CreateParams;
256                                 result.ExStyle |= (int) WindowExStyles.WS_EX_CLIENTEDGE;
257                                 return result;
258                         }
259                 }
260                 #endregion      // Protected Instance Properties
261
262                 #region Public Instance Methods
263                 public void LayoutMdi (MdiLayout value) {
264
265                         int max_width = Int32.MaxValue;
266                         int max_height = Int32.MaxValue;
267
268                         if (Parent != null) {
269                                 max_width = Parent.Width;
270                                 max_height = Parent.Height;
271                         }
272
273                         switch (value) {
274                         case MdiLayout.Cascade: {
275                                 int i = 0;
276                                 for (int c = Controls.Count - 1; c >= 0; c--) {
277                                         Form form = (Form) Controls [c];
278
279                                         if (form.WindowState == FormWindowState.Minimized)
280                                                 continue;
281                 
282                                         int l = 22 * i;
283                                         int t = 22 * i;
284
285                                         if (i != 0 && (l + form.Width > max_width || t + form.Height > max_height)) {
286                                                 i = 0;
287                                                 l = 22 * i;
288                                                 t = 22 * i;
289                                         }
290
291                                         form.Left = l;
292                                         form.Top = t;
293
294                                         i++;
295                                 }
296                                 break;
297                                 }
298                         case MdiLayout.ArrangeIcons:
299                                 ArrangeIconicWindows (true);
300                                 break;
301                         case MdiLayout.TileHorizontal:
302                         case MdiLayout.TileVertical: {
303                                 // First count number of windows to tile
304                                 int total = 0;
305                                 for (int i = 0; i < Controls.Count; i++) {
306                                         Form form = Controls [i] as Form;
307                                         
308                                         if (form == null)
309                                                 continue;
310                                         
311                                         if (!form.Visible)
312                                                 continue;
313                                         
314                                         if (form.WindowState == FormWindowState.Minimized)
315                                                 continue;
316                                                 
317                                         total++;
318                                 }
319                                 if (total <= 0)
320                                         return;
321
322                                 // Calculate desired height and width
323                                 Size newSize;
324                                 Size offset;
325                                 if (value == MdiLayout.TileHorizontal) {
326                                         newSize = new Size (ClientSize.Width, ClientSize.Height / total);
327                                         offset = new Size (0, newSize.Height);
328                                 } else {
329                                         newSize = new Size (ClientSize.Width / total, ClientSize.Height);
330                                         offset = new Size (newSize.Width, 0);
331                                 }
332                                 
333                                 // Loop again and set the size and location.
334                                 Point nextLocation = Point.Empty;
335                                 
336                                 for (int i = 0; i < Controls.Count; i++) {
337                                         Form form = Controls [i] as Form;
338
339                                         if (form == null)
340                                                 continue;
341
342                                         if (!form.Visible)
343                                                 continue;
344
345                                         if (form.WindowState == FormWindowState.Minimized)
346                                                 continue;
347
348                                         form.Size = newSize;
349                                         form.Location = nextLocation;
350                                         nextLocation += offset;
351                                 }
352                                 
353                                 break;
354                                 }
355                         }
356                 }
357                 #endregion      // Public Instance Methods
358
359                 #region Protected Instance Methods
360                 #endregion      // Protected Instance Methods
361
362                 internal void SizeScrollBars ()
363                 {
364                         if (lock_sizing)
365                                 return;
366                         
367                         if (!IsHandleCreated)
368                                 return;
369
370                         if (Controls.Count == 0 || ((Form) Controls [0]).WindowState == FormWindowState.Maximized) {
371                                 if (hbar != null)
372                                         hbar.Visible = false;
373                                 if (vbar != null)
374                                         vbar.Visible = false;
375                                 if (sizegrip != null)
376                                         sizegrip.Visible = false;
377                                 return;
378                         }
379
380                         int right = 0;
381                         int left = 0;
382                         int top = 0;
383                         int bottom = 0;
384
385                         foreach (Form child in Controls) {
386                                 if (!child.Visible)
387                                         continue;
388                                 if (child.Right > right)
389                                         right = child.Right;
390                                 if (child.Left < left) {
391                                         left = child.Left;
392                                 }
393                                 
394                                 if (child.Bottom > bottom)
395                                         bottom = child.Bottom;
396                                 if (child.Top < 0) {
397                                         top = child.Top;
398                                 }
399                         }
400
401                         int available_width = ClientSize.Width;
402                         int available_height = ClientSize.Height;
403
404                         bool need_hbar = false;
405                         bool need_vbar = false;
406
407                         if (right - left > available_width || left < 0) {
408                                 need_hbar = true;
409                                 available_height -= SystemInformation.HorizontalScrollBarHeight;
410                         }
411                         if (bottom - top > available_height || top < 0) {
412                                 need_vbar = true;
413                                 available_width -= SystemInformation.VerticalScrollBarWidth;
414
415                                 if (!need_hbar && (right - left > available_width || left < 0)) {
416                                         need_hbar = true;
417                                         available_height -= SystemInformation.HorizontalScrollBarHeight;
418                                 }
419                         }
420                         
421                         if (need_hbar) {
422                                 if (hbar == null) {
423                                         hbar = new ImplicitHScrollBar ();
424                                         Controls.AddImplicit (hbar);
425                                 }
426                                 hbar.Visible = true;
427                                 CalcHBar (left, right, need_vbar);
428                         } else if (hbar != null)
429                                 hbar.Visible = false;
430
431                         if (need_vbar) {
432                                 if (vbar == null) {
433                                         vbar = new ImplicitVScrollBar ();
434                                         Controls.AddImplicit (vbar);
435                                 }
436                                 vbar.Visible = true;
437                                 CalcVBar (top, bottom, need_hbar);
438                         } else if (vbar != null)
439                                 vbar.Visible = false;
440
441                         if (need_hbar && need_vbar) {
442                                 if (sizegrip == null) {
443                                         sizegrip = new SizeGrip (this.ParentForm);
444                                         Controls.AddImplicit (sizegrip);
445                                 }
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;
451                         }
452                 }
453
454                 private void CalcHBar (int left, int right, bool vert_vis)
455                 {
456                         initializing_scrollbars = true;
457
458                         hbar.Left = 0;
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);
464                         hbar.Value = 0;
465                         hbar_value = 0;
466                         hbar.ValueChanged += new EventHandler (HBarValueChanged);
467                         XplatUI.SetZOrder (hbar.Handle, IntPtr.Zero, true, false);
468                         
469                         initializing_scrollbars = false;
470                 }
471
472                 private void CalcVBar (int top, int bottom, bool horz_vis)
473                 {
474                         initializing_scrollbars = true;
475                         
476                         vbar.Top = 0;
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);
482                         vbar.Value = 0;
483                         vbar_value = 0;
484                         vbar.ValueChanged += new EventHandler (VBarValueChanged);
485                         XplatUI.SetZOrder (vbar.Handle, IntPtr.Zero, true, false);
486                         
487                         initializing_scrollbars = false;
488                 }
489
490                 private void HBarValueChanged (object sender, EventArgs e)
491                 {
492                         if (initializing_scrollbars)
493                                 return;
494                         
495                         if (hbar.Value == hbar_value)
496                                 return;
497
498                         lock_sizing = true;
499
500                         try {
501                                 int diff = hbar_value - hbar.Value;
502                                 foreach (Form child in Controls) {
503                                         child.Left += diff;
504                                 }
505                         } finally {
506                                 lock_sizing = false;
507                         }
508
509                         hbar_value = hbar.Value;
510                 }
511
512                 private void VBarValueChanged (object sender, EventArgs e)
513                 {
514                         if (initializing_scrollbars)
515                                 return;
516                                 
517                         if (vbar.Value == vbar_value)
518                                 return;
519
520                         lock_sizing = true;
521
522                         try {
523                                 int diff = vbar_value - vbar.Value;
524                                 foreach (Form child in Controls) {
525                                         child.Top += diff;
526                                 }
527                         } finally {
528                                 lock_sizing = false;
529                         }
530
531                         vbar_value = vbar.Value;
532                 }
533
534                 private void ArrangeWindows ()
535                 {
536                         if (!IsHandleCreated)
537                                 return;
538                                 
539                         int change = 0;
540                         if (prev_bottom != -1)
541                                 change = Bottom - prev_bottom;
542
543                         foreach (Control c in Controls) {
544                                 Form child = c as Form;
545
546                                 if (c == null || !child.Visible)
547                                         continue;
548
549                                 MdiWindowManager wm = child.WindowManager as MdiWindowManager;
550                                 if (wm.GetWindowState () == FormWindowState.Maximized)
551                                         child.Bounds = wm.MaximizedBounds;
552
553                                 if (wm.GetWindowState () == FormWindowState.Minimized) {
554                                         child.Top += change;
555                                 }
556                                         
557                         }
558
559                         prev_bottom = Bottom;
560                 }
561
562                 internal void ArrangeIconicWindows (bool rearrange_all)
563                 {
564                         Rectangle rect = Rectangle.Empty;
565
566                         lock_sizing = true;
567                         foreach (Form form in Controls) {
568                                 if (form.WindowState != FormWindowState.Minimized)
569                                         continue;
570
571                                 MdiWindowManager wm = (MdiWindowManager) form.WindowManager;
572                                 
573                                 if (wm.IconicBounds != Rectangle.Empty && !rearrange_all) {
574                                         if (form.Bounds != wm.IconicBounds)
575                                                 form.Bounds = wm.IconicBounds;
576                                         continue;
577                                 }
578                                 
579                                 bool success = true;
580                                 int startx, starty, currentx, currenty;
581
582                                 rect.Size = wm.IconicSize;
583                                 
584                                 startx = 0;
585                                 starty = ClientSize.Height - rect.Height;
586                                 currentx = startx;
587                                 currenty = starty;
588                                 
589                                 do {
590                                         rect.X = currentx;
591                                         rect.Y = currenty;
592                                         success = true;
593                                         foreach (Form form2 in Controls) {
594                                                 if (form2 == form || form2.window_state != FormWindowState.Minimized)
595                                                         continue;
596                                                 
597                                                 if (form2.Bounds.IntersectsWith(rect)) {
598                                                         success = false;
599                                                         break;
600                                                 }
601                                         }
602                                         if (!success) { 
603                                                 currentx += rect.Width;
604                                                 if (currentx + rect.Width > Right) {
605                                                         currentx = startx;
606                                                         currenty -= rect.Height;
607                                                 } 
608                                         }
609                                 } while (!success);
610                                 wm.IconicBounds = rect;
611                                 form.Bounds = wm.IconicBounds;
612                         }
613                         lock_sizing = false;
614                 }
615
616                 internal void ChildFormClosed (Form form)
617                 {
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);
623                         }
624         
625                         form.Visible = false;
626                         Controls.Remove (form);
627                         
628                         if (Controls.Count == 0) {
629                                 ((MdiWindowManager) form.window_manager).RaiseDeactivate ();
630                         }
631
632                         if (Controls.Count == 0) {
633                                 XplatUI.RequestNCRecalc (Parent.Handle);
634                                 ParentForm.PerformLayout ();
635
636 #if NET_2_0
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;
641
642                                 if (parent_menu != null)
643                                         if (parent_menu.IsCurrentlyMerged)
644                                                 ToolStripManager.RevertMerge (parent_menu);
645 #endif
646                         }
647                         SizeScrollBars ();
648                         SetParentText (false);
649                 }
650
651                 internal void ActivateNextChild ()
652                 {
653                         if (Controls.Count < 1)
654                                 return;
655                         if (Controls.Count == 1 && Controls[0] == ActiveMdiChild)
656                                 return;
657                                 
658                         Form front = (Form) Controls [0];
659                         Form form = (Form) Controls [1];
660
661                         ActivateChild (form);
662                         front.SendToBack ();
663                 }
664
665                 internal void ActivatePreviousChild ()
666                 {
667                         if (Controls.Count <= 1)
668                                 return;
669                         
670                         Form back = (Form) Controls [Controls.Count - 1];
671                         
672                         ActivateChild (back);
673                 }
674
675                 internal void ActivateChild (Form form)
676                 {
677                         if (Controls.Count < 1)
678                                 return;
679
680                         if (ParentForm.is_changing_visible_state > 0)
681                                 return;
682                         
683                         Form current = (Form) Controls [0];
684                         bool raise_deactivate = ParentForm.ActiveControl == current;
685
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;
694                         
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);
701                         }
702
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 ();
715                                         
716                                 }
717                         }
718                         active_child = (Form) Controls [0];
719                         
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 ();
726                                 }
727                         }
728                 }
729
730                 internal override IntPtr AfterTopMostControl ()
731                 {
732                         // order of scrollbars:
733                         // top = vertical
734                         //       sizegrid
735                         // bottom = horizontal
736                         if (hbar != null && hbar.Visible)
737                                 return hbar.Handle;
738                         // no need to check for sizegrip since it will only
739                         // be visible if hbar is visible.
740                         if (vbar != null && vbar.Visible)
741                                 return vbar.Handle;
742                                 
743                         return base.AfterTopMostControl ();
744                 }
745                 
746                 internal bool SetWindowStates (MdiWindowManager wm)
747                 {
748                 /*
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.
756                 */
757                         Form form = wm.form;
758
759                         if (setting_windowstates) {
760                                 return false;
761                         }
762                         
763                         if (!form.Visible)
764                                 return false;
765                         
766                         bool is_active = wm.IsActive;
767                         bool maximize_this = false;
768                         
769                         if (!is_active){
770                                 return false;
771                         }
772                         
773                         ArrayList minimize_these = new ArrayList ();
774                         ArrayList normalize_these = new ArrayList ();
775
776                         setting_windowstates = true;
777                         foreach (Form frm in mdi_child_list) {
778                                 if (frm == form) {
779                                         continue;
780                                 } else if (!frm.Visible){
781                                         continue;
782                                 }
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); 
787                                         } else {
788                                                 normalize_these.Add (frm); 
789                                         }
790                                 }
791                         }
792
793                         if (maximize_this && form.WindowState != FormWindowState.Maximized) {
794                                 wm.was_minimized = form.window_state == FormWindowState.Minimized;
795                                 form.WindowState = FormWindowState.Maximized;
796                         }
797                         
798                         foreach (Form frm in minimize_these)
799                                 frm.WindowState = FormWindowState.Minimized;
800
801                         foreach (Form frm in normalize_these)
802                                 frm.WindowState = FormWindowState.Normal;
803
804
805                         SetParentText (false);
806                         
807                         XplatUI.RequestNCRecalc (ParentForm.Handle);
808                         XplatUI.RequestNCRecalc (Handle);
809
810                         SizeScrollBars ();
811
812                         setting_windowstates = false;
813
814 #if NET_2_0
815                         if (form.MdiParent.MainMenuStrip != null)
816                                 form.MdiParent.MainMenuStrip.RefreshMdiItems ();
817
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
822                         // - Merge
823                         MenuStrip parent_menu = form.MdiParent.MainMenuStrip;
824
825                         if (parent_menu != null) {
826                                 if (parent_menu.IsCurrentlyMerged)
827                                         ToolStripManager.RevertMerge (parent_menu);
828                                         
829                                 MenuStrip child_menu = null;
830
831                                 foreach (Control c in form.Controls)
832                                         if (c is MenuStrip)
833                                                 child_menu = (MenuStrip)c;
834
835                                 if (child_menu != null)
836                                         ToolStripManager.Merge (child_menu, parent_menu);
837                         }
838 #endif
839
840                         return maximize_this;
841                 }
842
843                 internal void SetWindowState (Form form, FormWindowState old_window_state, FormWindowState new_window_state, bool is_activating_child)
844                 {
845                         bool mdiclient_layout;
846
847                         MdiWindowManager wm = (MdiWindowManager) form.window_manager;
848
849                         if (!is_activating_child && new_window_state == FormWindowState.Maximized && !wm.IsActive) {
850                                 ActivateChild (form);
851                                 return;
852                         }
853
854                         if (SetWindowStates (wm))
855                                 return;
856
857                         if (old_window_state == new_window_state)
858                                 return;
859                                 
860                         if (old_window_state == FormWindowState.Normal)
861                                 wm.NormalBounds = form.Bounds;
862
863                         mdiclient_layout = old_window_state == FormWindowState.Maximized || new_window_state == FormWindowState.Maximized;
864
865                         switch (new_window_state) {
866                         case FormWindowState.Minimized:
867                                 ArrangeIconicWindows (false);
868                                 break;
869                         case FormWindowState.Maximized:
870                                 form.Bounds = wm.MaximizedBounds;
871                                 break;
872                         case FormWindowState.Normal:
873                                 form.Bounds = wm.NormalBounds;
874                                 break;
875                         }
876
877                         wm.UpdateWindowDecorations (new_window_state);
878
879                         form.ResetCursor ();
880
881                         if (mdiclient_layout)
882                                 Parent.PerformLayout ();
883
884                         XplatUI.RequestNCRecalc (Parent.Handle);
885                         XplatUI.RequestNCRecalc (form.Handle);
886                         if (!setting_windowstates)
887                                 SizeScrollBars ();
888                 }
889                 internal int ChildrenCreated {
890                         get { return mdi_created; }
891                         set { mdi_created = value; }
892                 }
893
894                 internal Form ActiveMdiChild {
895                         get {
896 #if NET_2_0
897                                 if (!ParentForm.Visible)
898                                         return null;
899 #endif
900                                 if (Controls.Count < 1)
901                                         return null;
902                                         
903                                 if (!ParentForm.IsHandleCreated)
904                                         return null;
905                                 
906                                 if (!ParentForm.has_been_visible)
907                                         return null;
908                                         
909                                 if (!ParentForm.Visible)
910                                         return active_child;
911                                 
912                                 active_child = null;
913                                 for (int i = 0; i < Controls.Count; i++) {
914                                         if (Controls [i].Visible) {
915                                                 active_child = (Form) Controls [i];
916                                                 break;
917                                         }
918                                 }
919                                 return active_child;
920                         }
921                         set {
922                                 ActivateChild (value);
923                         }
924                 }
925                 
926                 internal void ActivateActiveMdiChild ()
927                 {
928                         if (ParentForm.is_changing_visible_state > 0)
929                                 return;
930                                 
931                         for (int i = 0; i < Controls.Count; i++) {
932                                 if (Controls [i].Visible) {
933                                         ActivateChild ((Form) Controls [i]);
934                                         return;
935                                 }
936                         }
937                 }
938         }
939 }
940