Fix bug #395
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripPanelRow.cs
1 //
2 // ToolStripPanelRow.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28
29 using System.Drawing;
30 using System.Runtime.InteropServices;
31 using System.ComponentModel;
32 using System.Windows.Forms.Layout;
33 using System.Collections.Generic;
34
35 namespace System.Windows.Forms
36 {
37         [ToolboxItem (false)]
38         public class ToolStripPanelRow : Component, IComponent, IDisposable, IBounds
39         {
40                 private Rectangle bounds;
41                 internal List<Control> controls;
42                 private LayoutEngine layout_engine;
43                 private Padding margin;
44                 private Padding padding;
45                 private ToolStripPanel parent;
46
47                 #region Public Constructors
48                 public ToolStripPanelRow (ToolStripPanel parent)
49                 {
50                         this.bounds = Rectangle.Empty;
51                         this.controls = new List<Control> ();
52                         this.layout_engine = new DefaultLayout ();
53                         this.parent = parent;
54                 }
55                 #endregion
56
57                 #region Public Properties
58                 public Rectangle Bounds {
59                         get { return this.bounds; }
60                 }
61
62                 [Browsable (false)]
63                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
64                 public Control[] Controls {
65                         get { return this.controls.ToArray (); }
66                 }
67
68                 public Rectangle DisplayRectangle {
69                         get { return this.Bounds; }
70                 }
71
72                 public LayoutEngine LayoutEngine {
73                         get { 
74                                 if (this.layout_engine == null)
75                                         this.layout_engine = new DefaultLayout ();
76                                         
77                                 return this.layout_engine;
78                         }
79                 }
80
81                 public Padding Margin {
82                         get { return this.margin; }
83                         set { this.margin = value; }
84                 }
85
86                 public Orientation Orientation {
87                         get { return this.parent.Orientation; }
88                 }
89
90                 public virtual Padding Padding {
91                         get { return this.padding; }
92                         set { this.padding = value; }
93                 }
94
95                 public ToolStripPanel ToolStripPanel {
96                         get { return this.parent; }
97                 } 
98                 #endregion
99
100                 #region Protected Properties
101                 protected virtual Padding DefaultMargin { get { return Padding.Empty; } }
102                 protected virtual Padding DefaultPadding { get { return Padding.Empty; } }
103                 #endregion
104
105                 #region Public Methods
106                 public bool CanMove (ToolStrip toolStripToDrag)
107                 {
108                         // If something uses Stretch, it gets a whole Row to itself
109                         if (this.controls.Count > 0)
110                                 if (toolStripToDrag.Stretch || (this.controls[0] as ToolStrip).Stretch)
111                                         return false;
112                         
113                         int width = 0;
114                         
115                         foreach (ToolStrip ts in this.controls)
116                                 width += (ts.Width + ts.Margin.Horizontal);
117                                 
118                         if (width + toolStripToDrag.Width + toolStripToDrag.Margin.Horizontal <= this.bounds.Width)
119                                 return true;
120                                 
121                         return false;
122                 }
123                 #endregion
124
125                 #region Protected Methods
126                 protected override void Dispose (bool disposing)
127                 {
128                         base.Dispose (disposing);
129                 }
130                 
131                 protected void OnBoundsChanged (Rectangle oldBounds, Rectangle newBounds)
132                 {
133                 }
134                 
135                 protected internal virtual void OnControlAdded (Control control, int index)
136                 {
137                         control.SizeChanged += new EventHandler (control_SizeChanged);
138                         controls.Add (control);
139                         this.OnLayout (new LayoutEventArgs (control, string.Empty));
140                 }
141                 
142                 protected internal virtual void OnControlRemoved (Control control, int index)
143                 {
144                         control.SizeChanged -= new EventHandler (control_SizeChanged);
145                         controls.Remove (control);
146                         this.OnLayout (new LayoutEventArgs (control, string.Empty));
147                 }
148                 
149                 protected virtual void OnLayout (LayoutEventArgs e)
150                 {
151                         int height = 0;
152                         
153                         if (this.Orientation == Orientation.Horizontal) {
154                                 foreach (ToolStrip ts in this.controls)
155                                         if (ts.Height > height)
156                                                 height = ts.Height;
157                                                 
158                                 if (height != this.bounds.Height)
159                                         this.bounds.Height = height;
160                         } else {
161                                 foreach (ToolStrip ts in this.controls)
162                                         if (ts.GetPreferredSize (Size.Empty).Width > height)
163                                                 height = ts.GetPreferredSize (Size.Empty).Width;
164
165                                 if (height != this.bounds.Width)
166                                         this.bounds.Width = height;
167                         }
168                                 
169                         this.Layout (this, e);
170                 }
171                 
172                 protected internal virtual void OnOrientationChanged ()
173                 {
174                 }
175                 #endregion
176
177                 #region Private/Internal Methods
178                 internal void SetBounds (Rectangle bounds)
179                 {
180                         if (this.bounds != bounds) {
181                                 Rectangle old_bounds = this.bounds;
182                                 this.bounds = bounds;
183                                 this.OnBoundsChanged (old_bounds, bounds);
184                                 this.OnLayout (new LayoutEventArgs (null, "Bounds"));
185                         }
186                 }
187                 
188                 private bool Layout (object container, LayoutEventArgs args)
189                 {
190                         ToolStripPanelRow tspr = (ToolStripPanelRow)container;
191                         Point position = tspr.DisplayRectangle.Location;
192                         foreach (ToolStrip ts in tspr.Controls)
193                         {
194                                 if (Orientation == Orientation.Horizontal) {
195                                         if (ts.Stretch)
196                                                 ts.Width = this.bounds.Width - ts.Margin.Horizontal - this.Padding.Horizontal;
197                                         else
198                                                 ts.Width = ts.GetToolStripPreferredSize (Size.Empty).Width;
199                                                 
200                                         position.X += ts.Margin.Left;
201                                         ts.Location = position;
202                                         
203                                         position.X += (ts.Width + ts.Margin.Left);
204                                 } else {
205                                         if (ts.Stretch)
206                                                 ts.Size = new Size (ts.GetToolStripPreferredSize (Size.Empty).Width, this.bounds.Height - ts.Margin.Vertical - this.Padding.Vertical);
207                                         else
208                                                 ts.Size = ts.GetToolStripPreferredSize (Size.Empty);
209
210                                         position.Y += ts.Margin.Top;
211                                         ts.Location = position;
212
213                                         position.Y += (ts.Height + ts.Margin.Top);
214                                 }
215                         }
216                         
217                         return false;
218                 }
219
220                 void control_SizeChanged (object sender, EventArgs e)
221                 {
222                         this.OnLayout (new LayoutEventArgs ((Control)sender, string.Empty));
223                 }
224                 #endregion
225         }
226 }