1 // System.Windows.Forms.ToolBarButton.cs
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // Copyright (C) 2004-2006 Novell, Inc. (http://www.novell.com)
25 // Ravindra (rkumar@novell.com)
26 // Mike Kestner <mkestner@novell.com>
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
32 using System.Drawing.Imaging;
34 namespace System.Windows.Forms
36 [DefaultProperty ("Text")]
37 [Designer ("System.Windows.Forms.Design.ToolBarButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
38 [DesignTimeVisible (false)]
40 public class ToolBarButton : Component
42 #region instance variable
43 private bool enabled = true;
44 private int image_index = -1;
45 private ContextMenu menu;
46 private ToolBar parent;
47 private bool partial_push = false;
48 private bool pushed = false;
49 private ToolBarButtonStyle style = ToolBarButtonStyle.PushButton;
51 private string text = "";
52 private string tooltip = "";
53 private bool visible = true;
54 internal bool dd_pressed = false; // to check for a mouse down on dropdown rect
55 internal bool hilight = false; // to hilight buttons in flat style
56 internal bool inside = false; // to handle the mouse move event with mouse pressed
57 internal bool pressed = false; // this is to check for mouse down on a button
61 public ToolBarButton () { }
63 public ToolBarButton (string text)
69 #region internal properties
70 internal bool Hilight {
71 get { return hilight; }
80 internal Image Image {
82 if (Parent == null || Parent.ImageList == null)
85 ImageList list = Parent.ImageList;
86 if (ImageIndex > -1 && ImageIndex < list.Images.Count)
87 return list.Images [ImageIndex];
94 internal Rectangle ImageRectangle {
96 Rectangle result = image_rect;
104 internal Rectangle TextRectangle {
106 Rectangle result = text_rect;
107 result.X += bounds.X;
108 result.Y += bounds.Y;
114 internal Point Location {
115 //get { return location; }
117 if (bounds.Location == value)
120 if (bounds != Rectangle.Empty)
123 bounds.Location = value;
128 internal bool Pressed {
130 if (pressed && inside)
135 set { pressed = value; }
137 #endregion internal properties
140 [DefaultValue (null)]
141 [TypeConverter (typeof (ReferenceConverter))]
142 public Menu DropDownMenu {
146 if (value is ContextMenu)
147 menu = (ContextMenu) value;
149 throw new ArgumentException ("DropDownMenu must be of type ContextMenu.");
153 [DefaultValue (true)]
155 public bool Enabled {
156 get { return enabled; }
158 if (value == enabled)
167 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
169 [TypeConverter (typeof (ImageIndexConverter))]
170 public int ImageIndex {
171 get { return image_index; }
174 throw new ArgumentException ("ImageIndex value must be above or equal to -1.");
176 if (value == image_index)
185 public ToolBar Parent {
186 get { return parent; }
189 [DefaultValue (false)]
190 public bool PartialPush {
191 get { return partial_push; }
193 if (value == partial_push)
196 partial_push = value;
201 [DefaultValue (false)]
203 get { return pushed; }
215 public Rectangle Rectangle {
217 if (Visible && Parent != null && Parent.Visible) {
218 Rectangle result = bounds;
219 if (Style == ToolBarButtonStyle.DropDownButton && Parent.DropDownArrows)
220 result.Width += ThemeEngine.Current.ToolBarDropDownWidth;
223 return Rectangle.Empty;
227 [DefaultValue (ToolBarButtonStyle.PushButton)]
228 [RefreshProperties (RefreshProperties.Repaint)]
229 public ToolBarButtonStyle Style {
230 get { return style; }
241 [DefaultValue (null)]
242 [Localizable (false)]
243 [TypeConverter (typeof (StringConverter))]
264 public string ToolTipText {
265 get { return tooltip; }
266 set { tooltip = value; }
269 [DefaultValue (true)]
271 public bool Visible {
272 get { return visible; }
274 if (value == visible)
279 Parent.Redraw (true);
284 #region internal methods
285 internal void SetParent (ToolBar parent)
287 if (Parent == parent)
291 Parent.Buttons.Remove (this);
293 this.parent = parent;
296 internal void Layout ()
298 if (Parent == null || !Visible)
301 Layout (Parent.ButtonSize);
304 internal void Layout (Size size)
306 if (Parent == null || !Visible)
313 text_rect = new Rectangle (Point.Empty, size);
314 image_rect = Rectangle.Empty;
318 int grip = ThemeEngine.Current.ToolBarImageGripWidth;
320 if (Parent.TextAlign == ToolBarTextAlign.Underneath) {
321 image_rect = new Rectangle ((size.Width - image.Width) / 2 - grip, 0, image.Width + 2 + grip, image.Height + 2 * grip);
323 text_rect = new Rectangle (0, image_rect.Bottom, size.Width, size.Height - image_rect.Height);
325 image_rect = new Rectangle (0, 0, image.Width + 2 * grip, image.Height + 2 * grip);
327 text_rect = new Rectangle (image_rect.Right, 0, size.Width - image_rect.Width, size.Height);
331 const int text_padding = 3;
333 Size CalculateSize ()
335 Theme theme = ThemeEngine.Current;
337 int ht = Parent.ButtonSize.Height + 2 * theme.ToolBarGripWidth;
339 if (Style == ToolBarButtonStyle.Separator)
340 return new Size (theme.ToolBarSeparatorWidth, ht);
342 SizeF sz = Parent.DeviceContext.MeasureString (Text, Parent.Font);
343 Size size = new Size ((int) Math.Ceiling (sz.Width) + 2 * text_padding,
344 (int) Math.Ceiling (sz.Height));
351 int image_width = image.Width + 2 * theme.ToolBarImageGripWidth;
352 int image_height = image.Height + 2 * theme.ToolBarImageGripWidth;
354 if (Parent.TextAlign == ToolBarTextAlign.Right) {
355 size.Width = image_width + size.Width;
356 size.Height = (size.Height > image_height) ? size.Height : image_height;
358 size.Height = image_height + size.Height;
359 size.Width = (size.Width > image_width) ? size.Width : image_width;
362 size.Width += theme.ToolBarGripWidth;
363 size.Height += theme.ToolBarGripWidth;
370 Parent.Invalidate (Rectangle);
373 #endregion Internal Methods
376 protected override void Dispose (bool disposing)
378 base.Dispose (disposing);
381 public override string ToString ()
383 return string.Format ("ToolBarButton: {0}, Style: {1}", text, style);