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; }
81 internal Image Image {
83 if (Parent == null || Parent.ImageList == null)
86 ImageList list = Parent.ImageList;
87 if (ImageIndex > -1 && ImageIndex < list.Images.Count)
88 return list.Images [ImageIndex];
95 internal Rectangle ImageRectangle {
97 Rectangle result = image_rect;
105 internal Rectangle TextRectangle {
107 Rectangle result = text_rect;
108 result.X += bounds.X;
109 result.Y += bounds.Y;
115 internal Point Location {
116 get { return bounds.Location; }
118 if (bounds.Location == value)
121 if (bounds != Rectangle.Empty)
124 bounds.Location = value;
129 internal bool Pressed {
131 if (pressed && inside)
136 set { pressed = value; }
138 #endregion internal properties
141 [DefaultValue (null)]
142 [TypeConverter (typeof (ReferenceConverter))]
143 public Menu DropDownMenu {
147 if (value is ContextMenu)
148 menu = (ContextMenu) value;
150 throw new ArgumentException ("DropDownMenu must be of type ContextMenu.");
154 [DefaultValue (true)]
156 public bool Enabled {
157 get { return enabled; }
159 if (value == enabled)
168 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
170 [TypeConverter (typeof (ImageIndexConverter))]
171 public int ImageIndex {
172 get { return image_index; }
175 throw new ArgumentException ("ImageIndex value must be above or equal to -1.");
177 if (value == image_index)
181 if (bounds.Size != CalculateSize ())
182 Parent.Redraw (true);
189 public ToolBar Parent {
190 get { return parent; }
193 [DefaultValue (false)]
194 public bool PartialPush {
195 get { return partial_push; }
197 if (value == partial_push)
200 partial_push = value;
205 [DefaultValue (false)]
207 get { return pushed; }
217 public Rectangle Rectangle {
219 if (Visible && Parent != null) {
220 Rectangle result = bounds;
221 if (Style == ToolBarButtonStyle.DropDownButton && Parent.DropDownArrows)
222 result.Width += ThemeEngine.Current.ToolBarDropDownWidth;
225 return Rectangle.Empty;
229 [DefaultValue (ToolBarButtonStyle.PushButton)]
230 [RefreshProperties (RefreshProperties.Repaint)]
231 public ToolBarButtonStyle Style {
232 get { return style; }
243 [DefaultValue (null)]
244 [Localizable (false)]
245 [TypeConverter (typeof (StringConverter))]
256 if (value == null) value = "";
263 Parent.Redraw (true);
269 public string ToolTipText {
270 get { return tooltip; }
272 if (value == null) value = "";
277 [DefaultValue (true)]
279 public bool Visible {
280 get { return visible; }
282 if (value == visible)
287 Parent.Redraw (true);
292 #region internal methods
293 internal void SetParent (ToolBar parent)
295 if (Parent == parent)
299 Parent.Buttons.Remove (this);
301 this.parent = parent;
304 internal bool Layout ()
306 if (Parent == null || !Visible)
309 Size psize = Parent.ButtonSize;
311 if ((!Parent.SizeSpecified) || (Style == ToolBarButtonStyle.Separator)) {
312 size = CalculateSize ();
313 if (size.Width == 0 || size.Height == 0)
316 return Layout (size);
319 internal bool Layout (Size size)
321 if (Parent == null || !Visible)
326 Size image_size = (Parent.ImageSize == Size.Empty) ? new Size (16, 16) : Parent.ImageSize;
327 int grip = ThemeEngine.Current.ToolBarImageGripWidth;
329 Rectangle new_image_rect, new_text_rect;
331 if (Parent.TextAlign == ToolBarTextAlign.Underneath) {
332 new_image_rect = new Rectangle ((bounds.Size.Width - image_size.Width) / 2 - grip, 0, image_size.Width + 2 + grip, image_size.Height + 2 * grip);
333 new_text_rect = new Rectangle (0, new_image_rect.Height, bounds.Size.Width, bounds.Size.Height - new_image_rect.Height);
335 new_image_rect = new Rectangle (0, 0, image_size.Width + 2 * grip, image_size.Height + 2 * grip);
336 new_text_rect = new Rectangle (new_image_rect.Width, 0, bounds.Size.Width - new_image_rect.Width, bounds.Size.Height);
339 bool changed = false;
341 if (new_image_rect != image_rect || new_text_rect != text_rect)
344 image_rect = new_image_rect;
345 text_rect = new_text_rect;
350 const int text_padding = 3;
354 SizeF sz = Parent.DeviceContext.MeasureString (Text, Parent.Font);
355 if (sz == SizeF.Empty)
357 return new Size ((int) Math.Ceiling (sz.Width) + 2 * text_padding, (int) Math.Ceiling (sz.Height));
361 Size CalculateSize ()
366 Theme theme = ThemeEngine.Current;
368 int ht = Parent.ButtonSize.Height + 2 * theme.ToolBarGripWidth;
370 if (Style == ToolBarButtonStyle.Separator)
371 return new Size (theme.ToolBarSeparatorWidth, ht);
373 Size size = TextSize;
374 Size image_size = (Parent.ImageSize == Size.Empty) ? new Size (16, 16) : Parent.ImageSize;
376 int image_width = image_size.Width + 2 * theme.ToolBarImageGripWidth;
377 int image_height = image_size.Height + 2 * theme.ToolBarImageGripWidth;
379 if (Parent.TextAlign == ToolBarTextAlign.Right) {
380 size.Width = image_width + size.Width;
381 size.Height = (size.Height > image_height) ? size.Height : image_height;
383 size.Height = image_height + size.Height;
384 size.Width = (size.Width > image_width) ? size.Width : image_width;
387 size.Width += theme.ToolBarGripWidth;
388 size.Height += theme.ToolBarGripWidth;
392 internal void InvalidateBorder ()
394 if (ThemeEngine.Current.ToolBarInvalidateEntireButton) {
398 if (Rectangle == Rectangle.Empty)
401 /* invalidate the four sides of our border */
402 Parent.Invalidate (new Rectangle (Rectangle.X - 2, Rectangle.Y - 2,
403 Rectangle.Width + 4, 4));
404 Parent.Invalidate (new Rectangle (Rectangle.X - 2, Rectangle.Y - 2,
405 4, Rectangle.Height + 4));
406 Parent.Invalidate (new Rectangle (Rectangle.X - 2, Rectangle.Y + Rectangle.Height - 2,
407 Rectangle.Width + 4, 4));
408 Parent.Invalidate (new Rectangle (Rectangle.X + Rectangle.Width - 2, Rectangle.Y - 2,
409 4, Rectangle.Height + 4));
411 if ((Style == ToolBarButtonStyle.DropDownButton) && Parent.DropDownArrows)
412 Parent.Invalidate (new Rectangle (Rectangle.X + (Rectangle.Width - ThemeEngine.Current.ToolBarDropDownWidth - 1),
413 Rectangle.Y, 2, Rectangle.Height));
417 internal void Invalidate ()
420 Parent.Invalidate (Rectangle);
423 #endregion Internal Methods
426 protected override void Dispose (bool disposing)
428 base.Dispose (disposing);
431 public override string ToString ()
433 return string.Format ("ToolBarButton: {0}, Style: {1}", text, style);