- Implement 2.0 image key feature.
[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 virtual 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 { return this.layout_engine; }
75                 }
76
77                 public Padding Margin {
78                         get { return this.margin; }
79                         set { this.margin = value; }
80                 }
81
82                 public Orientation Orientation {
83                         get { return this.parent.Orientation; }
84                 }
85
86                 public virtual Padding Padding {
87                         get { return this.padding; }
88                         set { this.padding = value; }
89                 }
90
91                 public ToolStripPanel ToolStripPanel {
92                         get { return this.parent; }
93                 } 
94                 #endregion
95
96                 #region Protected Properties
97                 protected virtual Padding DefaultMargin { get { return Padding.Empty; } }
98                 protected virtual Padding DefaultPadding { get { return Padding.Empty; } }
99                 #endregion
100
101                 #region Public Methods
102                 public bool CanMove (ToolStrip toolStripToDrag)
103                 {
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)
107                                         return false;
108                         
109                         int width = 0;
110                         
111                         foreach (ToolStrip ts in this.controls)
112                                 width += (ts.Width + ts.Margin.Horizontal);
113                                 
114                         if (width + toolStripToDrag.Width + toolStripToDrag.Margin.Horizontal <= this.bounds.Width)
115                                 return true;
116                                 
117                         return false;
118                 }
119                 #endregion
120
121                 #region Protected Methods
122                 protected override void Dispose (bool disposing)
123                 {
124                         base.Dispose (disposing);
125                 }
126                 
127                 protected void OnBoundsChanged (Rectangle oldBounds, Rectangle newBounds)
128                 {
129                 }
130                 
131                 protected internal virtual void OnControlAdded (Control control, int index)
132                 {
133                         control.SizeChanged += new EventHandler (control_SizeChanged);
134                         controls.Add (control);
135                         this.OnLayout (new LayoutEventArgs (control, ""));
136                 }
137                 
138                 protected internal virtual void OnControlRemoved (Control control, int index)
139                 {
140                         control.SizeChanged -= new EventHandler (control_SizeChanged);
141                         controls.Remove (control);
142                         this.OnLayout (new LayoutEventArgs (control, ""));
143                 }
144                 
145                 protected virtual void OnLayout (LayoutEventArgs e)
146                 {
147                         int height = 0;
148                         
149                         foreach (ToolStrip ts in this.controls)
150                                 if (ts.Height > height)
151                                         height = ts.Height;
152                                         
153                         if (height != this.bounds.Height)
154                                 this.bounds.Height = height;
155                                 
156                         this.Layout (this, e);
157                 }
158                 
159                 protected internal virtual void OnOrientationChanged ()
160                 {
161                 }
162                 #endregion
163
164                 #region Private/Internal Methods
165                 internal void SetBounds (Rectangle bounds)
166                 {
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"));
172                         }
173                 }
174                 
175                 private bool Layout (object container, LayoutEventArgs args)
176                 {
177                         ToolStripPanelRow tspr = (ToolStripPanelRow)container;
178                         Point position = tspr.DisplayRectangle.Location;
179                         foreach (ToolStrip ts in tspr.Controls)
180                         {
181                                 if (ts.Stretch)
182                                         ts.Width = this.bounds.Width - ts.Margin.Horizontal - this.Padding.Horizontal;
183                                 else
184                                         ts.Width = ts.GetPreferredSize (Size.Empty).Width;
185                                         
186                                 position.X += ts.Margin.Left;
187                                 ts.Location = position;
188                                 
189                                 position.X += (ts.Width + ts.Margin.Left);
190                         }
191                         
192                         return false;
193                 }
194
195                 void control_SizeChanged (object sender, EventArgs e)
196                 {
197                         this.OnLayout (new LayoutEventArgs ((Control)sender, ""));
198                 }
199                 #endregion
200         }
201 }
202 #endif