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>
27 // Everaldo Canuto <ecanuto@novell.com>
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
32 using System.Drawing.Text;
33 using System.Drawing.Imaging;
35 namespace System.Windows.Forms
37 [DefaultProperty ("Text")]
38 [Designer ("System.Windows.Forms.Design.ToolBarButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
39 [DesignTimeVisible (false)]
41 public class ToolBarButton : Component
43 #region instance variable
44 private bool enabled = true;
45 private int image_index = -1;
46 private ContextMenu menu;
47 private ToolBar parent;
48 private bool partial_push = false;
49 private bool pushed = false;
50 private ToolBarButtonStyle style = ToolBarButtonStyle.PushButton;
52 private string text = "";
53 private string tooltip = "";
54 private bool visible = true;
55 private string image_key = string.Empty;
61 public ToolBarButton () { }
63 public ToolBarButton (string text)
70 #region internal properties
72 internal Image Image {
74 if (Parent == null || Parent.ImageList == null)
77 ImageList list = Parent.ImageList;
78 if (ImageIndex > -1 && ImageIndex < list.Images.Count)
79 return list.Images [ImageIndex];
81 if (!string.IsNullOrEmpty (image_key))
82 return list.Images [image_key];
88 #endregion internal properties
93 [TypeConverter (typeof (ReferenceConverter))]
94 public Menu DropDownMenu {
98 if (value is ContextMenu)
99 menu = (ContextMenu) value;
101 throw new ArgumentException ("DropDownMenu must be of type ContextMenu.");
103 OnUIADropDownMenuChanged (EventArgs.Empty);
107 [DefaultValue (true)]
109 public bool Enabled {
110 get { return enabled; }
112 if (value == enabled)
118 OnUIAEnabledChanged (EventArgs.Empty);
122 [RefreshProperties (RefreshProperties.Repaint)]
124 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
126 [TypeConverter (typeof (ImageIndexConverter))]
127 public int ImageIndex {
128 get { return image_index; }
131 throw new ArgumentException ("ImageIndex value must be above or equal to -1.");
133 if (value == image_index)
136 bool layout = (Parent != null) && ((value == -1) || (image_index == -1));
139 image_key = string.Empty;
142 Parent.Redraw (true);
150 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
151 [RefreshProperties (RefreshProperties.Repaint)]
152 [TypeConverter (typeof (ImageKeyConverter))]
153 public string ImageKey {
154 get { return image_key; }
156 if (image_key == value)
159 bool layout = (Parent != null) && ((value == string.Empty) || (image_key == string.Empty));
165 Parent.Redraw (true);
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; }
213 public Rectangle Rectangle {
215 if (Visible && Parent != null && Parent.items != null)
216 foreach (ToolBarItem item in Parent.items)
217 if (item.Button == this)
218 return item.Rectangle;
220 return Rectangle.Empty;
224 [DefaultValue (ToolBarButtonStyle.PushButton)]
225 [RefreshProperties (RefreshProperties.Repaint)]
226 public ToolBarButtonStyle Style {
227 get { return style; }
235 parent.Redraw (true);
237 OnUIAStyleChanged (EventArgs.Empty);
242 [DefaultValue (null)]
243 [Localizable (false)]
244 [TypeConverter (typeof (StringConverter))]
255 if (value == null) value = "";
262 OnUIATextChanged (EventArgs.Empty);
265 Parent.Redraw (true);
271 public string ToolTipText {
272 get { return tooltip; }
274 if (value == null) value = "";
279 [DefaultValue (true)]
281 public bool Visible {
282 get { return visible; }
284 if (value == visible)
289 Parent.Redraw (true);
295 #region internal methods
297 internal void SetParent (ToolBar parent)
299 if (Parent == parent)
303 Parent.Buttons.Remove (this);
305 this.parent = parent;
308 internal void Invalidate ()
311 Parent.Invalidate (Rectangle);
314 bool uiaHasFocus = false;
315 internal bool UIAHasFocus {
316 get { return uiaHasFocus; }
320 (EventHandler) (value ? Events [UIAGotFocusEvent] : Events [UIALostFocusEvent]);
322 eh (this, EventArgs.Empty);
326 static object UIAGotFocusEvent = new object ();
327 static object UIALostFocusEvent = new object ();
328 static object UIATextChangedEvent = new object ();
329 static object UIAEnabledChangedEvent = new object ();
330 static object UIADropDownMenuChangedEvent = new object ();
331 static object UIAStyleChangedEvent = new object ();
333 internal event EventHandler UIAGotFocus {
334 add { Events.AddHandler (UIAGotFocusEvent, value); }
335 remove { Events.RemoveHandler (UIAGotFocusEvent, value); }
338 internal event EventHandler UIALostFocus {
339 add { Events.AddHandler (UIALostFocusEvent, value); }
340 remove { Events.RemoveHandler (UIALostFocusEvent, value); }
343 internal event EventHandler UIATextChanged {
344 add { Events.AddHandler (UIATextChangedEvent, value); }
345 remove { Events.RemoveHandler (UIATextChangedEvent, value); }
348 internal event EventHandler UIAEnabledChanged {
349 add { Events.AddHandler (UIAEnabledChangedEvent, value); }
350 remove { Events.RemoveHandler (UIAEnabledChangedEvent, value); }
353 internal event EventHandler UIADropDownMenuChanged {
354 add { Events.AddHandler (UIADropDownMenuChangedEvent, value); }
355 remove { Events.RemoveHandler (UIADropDownMenuChangedEvent, value); }
358 internal event EventHandler UIAStyleChanged {
359 add { Events.AddHandler (UIAStyleChangedEvent, value); }
360 remove { Events.RemoveHandler (UIAStyleChangedEvent, value); }
363 private void OnUIATextChanged(EventArgs e)
365 EventHandler eh = (EventHandler)(Events [UIATextChangedEvent]);
370 private void OnUIAEnabledChanged (EventArgs e)
372 EventHandler eh = (EventHandler)(Events [UIAEnabledChangedEvent]);
377 private void OnUIADropDownMenuChanged (EventArgs e)
379 EventHandler eh = (EventHandler)(Events [UIADropDownMenuChangedEvent]);
384 private void OnUIAStyleChanged (EventArgs e)
386 EventHandler eh = (EventHandler)(Events [UIAStyleChangedEvent]);
391 #endregion Internal Methods
395 protected override void Dispose (bool disposing)
397 base.Dispose (disposing);
400 public override string ToString ()
402 return string.Format ("ToolBarButton: {0}, Style: {1}", text, style);