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