* ListView.cs: When doing layout calculations don't use a ref
[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 #if NET_2_0     
30 using System.Drawing;
31 using System.Runtime.InteropServices;
32 using System.ComponentModel;
33 using System.Windows.Forms.Layout;
34 using System.Collections.Generic;
35
36 namespace System.Windows.Forms
37 {
38         [ToolboxItem (false)]
39         public class ToolStripPanelRow : Component, IComponent, IDisposable
40         {
41                 private Rectangle bounds;
42                 internal List<Control> controls;
43                 private LayoutEngine layout_engine;
44                 private Padding margin;
45                 private Padding padding;
46                 private ToolStripPanel parent;
47
48                 #region Public Constructors
49                 public ToolStripPanelRow (ToolStripPanel parent)
50                 {
51                         this.bounds = Rectangle.Empty;
52                         this.controls = new List<Control> ();
53                         this.layout_engine = new DefaultLayout ();
54                         this.parent = parent;
55                 }
56                 #endregion
57
58                 #region Public Properties
59                 public Rectangle Bounds {
60                         get { return this.bounds; }
61                 }
62
63                 [Browsable (false)]
64                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
65                 public Control[] Controls {
66                         get { return this.controls.ToArray (); }
67                 }
68
69                 public Rectangle DisplayRectangle {
70                         get { return this.Bounds; }
71                 }
72
73                 public LayoutEngine LayoutEngine {
74                         get { 
75                                 if (this.layout_engine == null)
76                                         this.layout_engine = new DefaultLayout ();
77                                         
78                                 return this.layout_engine;
79                         }
80                 }
81
82                 public Padding Margin {
83                         get { return this.margin; }
84                         set { this.margin = value; }
85                 }
86
87                 public Orientation Orientation {
88                         get { return this.parent.Orientation; }
89                 }
90
91                 public virtual Padding Padding {
92                         get { return this.padding; }
93                         set { this.padding = value; }
94                 }
95
96                 public ToolStripPanel ToolStripPanel {
97                         get { return this.parent; }
98                 } 
99                 #endregion
100
101                 #region Protected Properties
102                 protected virtual Padding DefaultMargin { get { return Padding.Empty; } }
103                 protected virtual Padding DefaultPadding { get { return Padding.Empty; } }
104                 #endregion
105
106                 #region Public Methods
107                 public bool CanMove (ToolStrip toolStripToDrag)
108                 {
109                         // If something uses Stretch, it gets a whole Row to itself
110                         if (this.controls.Count > 0)
111                                 if (toolStripToDrag.Stretch || (this.controls[0] as ToolStrip).Stretch)
112                                         return false;
113                         
114                         int width = 0;
115                         
116                         foreach (ToolStrip ts in this.controls)
117                                 width += (ts.Width + ts.Margin.Horizontal);
118                                 
119                         if (width + toolStripToDrag.Width + toolStripToDrag.Margin.Horizontal <= this.bounds.Width)
120                                 return true;
121                                 
122                         return false;
123                 }
124                 #endregion
125
126                 #region Protected Methods
127                 protected override void Dispose (bool disposing)
128                 {
129                         base.Dispose (disposing);
130                 }
131                 
132                 protected void OnBoundsChanged (Rectangle oldBounds, Rectangle newBounds)
133                 {
134                 }
135                 
136                 protected internal virtual void OnControlAdded (Control control, int index)
137                 {
138                         control.SizeChanged += new EventHandler (control_SizeChanged);
139                         controls.Add (control);
140                         this.OnLayout (new LayoutEventArgs (control, ""));
141                 }
142                 
143                 protected internal virtual void OnControlRemoved (Control control, int index)
144                 {
145                         control.SizeChanged -= new EventHandler (control_SizeChanged);
146                         controls.Remove (control);
147                         this.OnLayout (new LayoutEventArgs (control, ""));
148                 }
149                 
150                 protected virtual void OnLayout (LayoutEventArgs e)
151                 {
152                         int height = 0;
153                         
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                                 
161                         this.Layout (this, e);
162                 }
163                 
164                 protected internal virtual void OnOrientationChanged ()
165                 {
166                 }
167                 #endregion
168
169                 #region Private/Internal Methods
170                 internal void SetBounds (Rectangle bounds)
171                 {
172                         if (this.bounds != bounds) {
173                                 Rectangle old_bounds = this.bounds;
174                                 this.bounds = bounds;
175                                 this.OnBoundsChanged (old_bounds, bounds);
176                                 this.OnLayout (new LayoutEventArgs (null, "Bounds"));
177                         }
178                 }
179                 
180                 private bool Layout (object container, LayoutEventArgs args)
181                 {
182                         ToolStripPanelRow tspr = (ToolStripPanelRow)container;
183                         Point position = tspr.DisplayRectangle.Location;
184                         foreach (ToolStrip ts in tspr.Controls)
185                         {
186                                 if (ts.Stretch)
187                                         ts.Width = this.bounds.Width - ts.Margin.Horizontal - this.Padding.Horizontal;
188                                 else
189                                         ts.Width = ts.GetToolStripPreferredSize (Size.Empty).Width;
190                                         
191                                 position.X += ts.Margin.Left;
192                                 ts.Location = position;
193                                 
194                                 position.X += (ts.Width + ts.Margin.Left);
195                         }
196                         
197                         return false;
198                 }
199
200                 void control_SizeChanged (object sender, EventArgs e)
201                 {
202                         this.OnLayout (new LayoutEventArgs ((Control)sender, ""));
203                 }
204                 #endregion
205         }
206 }
207 #endif