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)
30 using System.Runtime.InteropServices;
31 using System.ComponentModel;
32 using System.Windows.Forms.Layout;
33 using System.Collections.Generic;
35 namespace System.Windows.Forms
38 public class ToolStripPanelRow : Component, IComponent, IDisposable, IBounds
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;
47 #region Public Constructors
48 public ToolStripPanelRow (ToolStripPanel parent)
50 this.bounds = Rectangle.Empty;
51 this.controls = new List<Control> ();
52 this.layout_engine = new DefaultLayout ();
57 #region Public Properties
58 public Rectangle Bounds {
59 get { return this.bounds; }
63 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
64 public Control[] Controls {
65 get { return this.controls.ToArray (); }
68 public Rectangle DisplayRectangle {
69 get { return this.Bounds; }
72 public LayoutEngine LayoutEngine {
74 if (this.layout_engine == null)
75 this.layout_engine = new DefaultLayout ();
77 return this.layout_engine;
81 public Padding Margin {
82 get { return this.margin; }
83 set { this.margin = value; }
86 public Orientation Orientation {
87 get { return this.parent.Orientation; }
90 public virtual Padding Padding {
91 get { return this.padding; }
92 set { this.padding = value; }
95 public ToolStripPanel ToolStripPanel {
96 get { return this.parent; }
100 #region Protected Properties
101 protected virtual Padding DefaultMargin { get { return Padding.Empty; } }
102 protected virtual Padding DefaultPadding { get { return Padding.Empty; } }
105 #region Public Methods
106 public bool CanMove (ToolStrip toolStripToDrag)
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)
115 foreach (ToolStrip ts in this.controls)
116 width += (ts.Width + ts.Margin.Horizontal);
118 if (width + toolStripToDrag.Width + toolStripToDrag.Margin.Horizontal <= this.bounds.Width)
125 #region Protected Methods
126 protected override void Dispose (bool disposing)
128 base.Dispose (disposing);
131 protected void OnBoundsChanged (Rectangle oldBounds, Rectangle newBounds)
135 protected internal virtual void OnControlAdded (Control control, int index)
137 control.SizeChanged += new EventHandler (control_SizeChanged);
138 controls.Add (control);
139 this.OnLayout (new LayoutEventArgs (control, string.Empty));
142 protected internal virtual void OnControlRemoved (Control control, int index)
144 control.SizeChanged -= new EventHandler (control_SizeChanged);
145 controls.Remove (control);
146 this.OnLayout (new LayoutEventArgs (control, string.Empty));
149 protected virtual void OnLayout (LayoutEventArgs e)
153 if (this.Orientation == Orientation.Horizontal) {
154 foreach (ToolStrip ts in this.controls)
155 if (ts.Height > height)
158 if (height != this.bounds.Height)
159 this.bounds.Height = height;
161 foreach (ToolStrip ts in this.controls)
162 if (ts.GetPreferredSize (Size.Empty).Width > height)
163 height = ts.GetPreferredSize (Size.Empty).Width;
165 if (height != this.bounds.Width)
166 this.bounds.Width = height;
169 this.Layout (this, e);
172 protected internal virtual void OnOrientationChanged ()
177 #region Private/Internal Methods
178 internal void SetBounds (Rectangle bounds)
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"));
188 private bool Layout (object container, LayoutEventArgs args)
190 ToolStripPanelRow tspr = (ToolStripPanelRow)container;
191 Point position = tspr.DisplayRectangle.Location;
192 foreach (ToolStrip ts in tspr.Controls)
194 if (Orientation == Orientation.Horizontal) {
196 ts.Width = this.bounds.Width - ts.Margin.Horizontal - this.Padding.Horizontal;
198 ts.Width = ts.GetToolStripPreferredSize (Size.Empty).Width;
200 position.X += ts.Margin.Left;
201 ts.Location = position;
203 position.X += (ts.Width + ts.Margin.Left);
206 ts.Size = new Size (ts.GetToolStripPreferredSize (Size.Empty).Width, this.bounds.Height - ts.Margin.Vertical - this.Padding.Vertical);
208 ts.Size = ts.GetToolStripPreferredSize (Size.Empty);
210 position.Y += ts.Margin.Top;
211 ts.Location = position;
213 position.Y += (ts.Height + ts.Margin.Top);
220 void control_SizeChanged (object sender, EventArgs e)
222 this.OnLayout (new LayoutEventArgs ((Control)sender, string.Empty));