2 // ToolStripPanelRow.cs
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
31 using System
.Runtime
.InteropServices
;
32 using System
.ComponentModel
;
33 using System
.Windows
.Forms
.Layout
;
34 using System
.Collections
.Generic
;
36 namespace System
.Windows
.Forms
39 public class ToolStripPanelRow
: Component
, IComponent
, IDisposable
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
;
48 #region Public Constructors
49 public ToolStripPanelRow (ToolStripPanel parent
)
51 this.bounds
= Rectangle
.Empty
;
52 this.controls
= new List
<Control
> ();
53 this.layout_engine
= new DefaultLayout ();
58 #region Public Properties
59 public Rectangle Bounds
{
60 get { return this.bounds; }
64 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
65 public Control
[] Controls
{
66 get { return this.controls.ToArray (); }
69 public Rectangle DisplayRectangle
{
70 get { return this.Bounds; }
73 public LayoutEngine LayoutEngine
{
74 get { return this.layout_engine; }
77 public Padding Margin
{
78 get { return this.margin; }
79 set { this.margin = value; }
82 public Orientation Orientation
{
83 get { return this.parent.Orientation; }
86 public virtual Padding Padding
{
87 get { return this.padding; }
88 set { this.padding = value; }
91 public ToolStripPanel ToolStripPanel
{
92 get { return this.parent; }
96 #region Protected Properties
97 protected virtual Padding DefaultMargin { get { return Padding.Empty; }
}
98 protected virtual Padding DefaultPadding { get { return Padding.Empty; }
}
101 #region Public Methods
102 public bool CanMove (ToolStrip toolStripToDrag
)
104 // If something uses Stretch, it gets a whole Row to itself
105 if (this.controls
.Count
> 0)
106 if (toolStripToDrag
.Stretch
|| (this.controls
[0] as ToolStrip
).Stretch
)
111 foreach (ToolStrip ts
in this.controls
)
112 width
+= (ts
.Width
+ ts
.Margin
.Horizontal
);
114 if (width
+ toolStripToDrag
.Width
+ toolStripToDrag
.Margin
.Horizontal
<= this.bounds
.Width
)
121 #region Protected Methods
122 protected override void Dispose (bool disposing
)
124 base.Dispose (disposing
);
127 protected void OnBoundsChanged (Rectangle oldBounds
, Rectangle newBounds
)
131 protected internal virtual void OnControlAdded (Control control
, int index
)
133 control
.SizeChanged
+= new EventHandler (control_SizeChanged
);
134 controls
.Add (control
);
135 this.OnLayout (new LayoutEventArgs (control
, ""));
138 protected internal virtual void OnControlRemoved (Control control
, int index
)
140 control
.SizeChanged
-= new EventHandler (control_SizeChanged
);
141 controls
.Remove (control
);
142 this.OnLayout (new LayoutEventArgs (control
, ""));
145 protected virtual void OnLayout (LayoutEventArgs e
)
149 foreach (ToolStrip ts
in this.controls
)
150 if (ts
.Height
> height
)
153 if (height
!= this.bounds
.Height
)
154 this.bounds
.Height
= height
;
156 this.Layout (this, e
);
159 protected internal virtual void OnOrientationChanged ()
164 #region Private/Internal Methods
165 internal void SetBounds (Rectangle bounds
)
167 if (this.bounds
!= bounds
) {
168 Rectangle old_bounds
= this.bounds
;
169 this.bounds
= bounds
;
170 this.OnBoundsChanged (old_bounds
, bounds
);
171 this.OnLayout (new LayoutEventArgs (null, "Bounds"));
175 private bool Layout (object container
, LayoutEventArgs args
)
177 ToolStripPanelRow tspr
= (ToolStripPanelRow
)container
;
178 Point position
= tspr
.DisplayRectangle
.Location
;
179 foreach (ToolStrip ts
in tspr
.Controls
)
182 ts
.Width
= this.bounds
.Width
- ts
.Margin
.Horizontal
- this.Padding
.Horizontal
;
184 ts
.Width
= ts
.GetPreferredSize (Size
.Empty
).Width
;
186 position
.X
+= ts
.Margin
.Left
;
187 ts
.Location
= position
;
189 position
.X
+= (ts
.Width
+ ts
.Margin
.Left
);
195 void control_SizeChanged (object sender
, EventArgs e
)
197 this.OnLayout (new LayoutEventArgs ((Control
)sender
, ""));