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, IBounds
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 {
75 if (this.layout_engine == null)
76 this.layout_engine = new DefaultLayout ();
78 return this.layout_engine;
82 public Padding Margin {
83 get { return this.margin; }
84 set { this.margin = value; }
87 public Orientation Orientation {
88 get { return this.parent.Orientation; }
91 public virtual Padding Padding {
92 get { return this.padding; }
93 set { this.padding = value; }
96 public ToolStripPanel ToolStripPanel {
97 get { return this.parent; }
101 #region Protected Properties
102 protected virtual Padding DefaultMargin { get { return Padding.Empty; } }
103 protected virtual Padding DefaultPadding { get { return Padding.Empty; } }
106 #region Public Methods
107 public bool CanMove (ToolStrip toolStripToDrag)
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)
116 foreach (ToolStrip ts in this.controls)
117 width += (ts.Width + ts.Margin.Horizontal);
119 if (width + toolStripToDrag.Width + toolStripToDrag.Margin.Horizontal <= this.bounds.Width)
126 #region Protected Methods
127 protected override void Dispose (bool disposing)
129 base.Dispose (disposing);
132 protected void OnBoundsChanged (Rectangle oldBounds, Rectangle newBounds)
136 protected internal virtual void OnControlAdded (Control control, int index)
138 control.SizeChanged += new EventHandler (control_SizeChanged);
139 controls.Add (control);
140 this.OnLayout (new LayoutEventArgs (control, string.Empty));
143 protected internal virtual void OnControlRemoved (Control control, int index)
145 control.SizeChanged -= new EventHandler (control_SizeChanged);
146 controls.Remove (control);
147 this.OnLayout (new LayoutEventArgs (control, string.Empty));
150 protected virtual void OnLayout (LayoutEventArgs e)
154 if (this.Orientation == Orientation.Horizontal) {
155 foreach (ToolStrip ts in this.controls)
156 if (ts.Height > height)
159 if (height != this.bounds.Height)
160 this.bounds.Height = height;
162 foreach (ToolStrip ts in this.controls)
163 if (ts.GetPreferredSize (Size.Empty).Width > height)
164 height = ts.GetPreferredSize (Size.Empty).Width;
166 if (height != this.bounds.Width)
167 this.bounds.Width = height;
170 this.Layout (this, e);
173 protected internal virtual void OnOrientationChanged ()
178 #region Private/Internal Methods
179 internal void SetBounds (Rectangle bounds)
181 if (this.bounds != bounds) {
182 Rectangle old_bounds = this.bounds;
183 this.bounds = bounds;
184 this.OnBoundsChanged (old_bounds, bounds);
185 this.OnLayout (new LayoutEventArgs (null, "Bounds"));
189 private bool Layout (object container, LayoutEventArgs args)
191 ToolStripPanelRow tspr = (ToolStripPanelRow)container;
192 Point position = tspr.DisplayRectangle.Location;
193 foreach (ToolStrip ts in tspr.Controls)
195 if (Orientation == Orientation.Horizontal) {
197 ts.Width = this.bounds.Width - ts.Margin.Horizontal - this.Padding.Horizontal;
199 ts.Width = ts.GetToolStripPreferredSize (Size.Empty).Width;
201 position.X += ts.Margin.Left;
202 ts.Location = position;
204 position.X += (ts.Width + ts.Margin.Left);
207 ts.Size = new Size (ts.GetToolStripPreferredSize (Size.Empty).Width, this.bounds.Height - ts.Margin.Vertical - this.Padding.Vertical);
209 ts.Size = ts.GetToolStripPreferredSize (Size.Empty);
211 position.Y += ts.Margin.Top;
212 ts.Location = position;
214 position.Y += (ts.Height + ts.Margin.Top);
221 void control_SizeChanged (object sender, EventArgs e)
223 this.OnLayout (new LayoutEventArgs ((Control)sender, string.Empty));