[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / ToolBarButton.cs
blob16e253147fb534f597830e4e3d6222e8398e3b5f
1 // System.Windows.Forms.ToolBarButton.cs
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
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)
24 // Authors:
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;
31 using System.Drawing;
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)]
40 [ToolboxItem (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;
51 private object tag;
52 private string text = "";
53 private string tooltip = "";
54 private bool visible = true;
55 private string image_key = string.Empty;
56 private string name;
57 #endregion
59 #region constructors
61 public ToolBarButton () { }
63 public ToolBarButton (string text)
65 this.text = text;
68 #endregion
70 #region internal properties
72 internal Image Image {
73 get {
74 if (Parent == null || Parent.ImageList == null)
75 return 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];
84 return null;
88 #endregion internal properties
90 #region properties
92 [DefaultValue (null)]
93 [TypeConverter (typeof (ReferenceConverter))]
94 public Menu DropDownMenu {
95 get { return menu; }
97 set {
98 if (value is ContextMenu)
99 menu = (ContextMenu) value;
100 else
101 throw new ArgumentException ("DropDownMenu must be of type ContextMenu.");
103 OnUIADropDownMenuChanged (EventArgs.Empty);
107 [DefaultValue (true)]
108 [Localizable (true)]
109 public bool Enabled {
110 get { return enabled; }
111 set {
112 if (value == enabled)
113 return;
115 enabled = value;
116 Invalidate ();
118 OnUIAEnabledChanged (EventArgs.Empty);
122 [RefreshProperties (RefreshProperties.Repaint)]
123 [DefaultValue (-1)]
124 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
125 [Localizable (true)]
126 [TypeConverter (typeof (ImageIndexConverter))]
127 public int ImageIndex {
128 get { return image_index; }
129 set {
130 if (value < -1)
131 throw new ArgumentException ("ImageIndex value must be above or equal to -1.");
133 if (value == image_index)
134 return;
136 bool layout = (Parent != null) && ((value == -1) || (image_index == -1));
138 image_index = value;
139 image_key = string.Empty;
141 if (layout)
142 Parent.Redraw (true);
143 else
144 Invalidate ();
148 [Localizable (true)]
149 [DefaultValue ("")]
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; }
155 set {
156 if (image_key == value)
157 return;
159 bool layout = (Parent != null) && ((value == string.Empty) || (image_key == string.Empty));
161 image_index = -1;
162 image_key = value;
164 if (layout)
165 Parent.Redraw (true);
166 else
167 Invalidate ();
171 [Browsable (false)]
172 public string Name {
173 get {
174 if (name == null)
175 return string.Empty;
177 return name;
179 set {
180 name = value;
184 [Browsable (false)]
185 public ToolBar Parent {
186 get { return parent; }
189 [DefaultValue (false)]
190 public bool PartialPush {
191 get { return partial_push; }
192 set {
193 if (value == partial_push)
194 return;
196 partial_push = value;
197 Invalidate ();
201 [DefaultValue (false)]
202 public bool Pushed {
203 get { return pushed; }
204 set {
205 if (value == pushed)
206 return;
208 pushed = value;
209 Invalidate ();
213 public Rectangle Rectangle {
214 get {
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; }
228 set {
229 if (value == style)
230 return;
232 style = value;
234 if (parent != null)
235 parent.Redraw (true);
237 OnUIAStyleChanged (EventArgs.Empty);
241 [Bindable (true)]
242 [DefaultValue (null)]
243 [Localizable (false)]
244 [TypeConverter (typeof (StringConverter))]
245 public object Tag {
246 get { return tag; }
247 set { tag = value; }
250 [DefaultValue ("")]
251 [Localizable (true)]
252 public string Text {
253 get { return text; }
254 set {
255 if (value == null) value = "";
257 if (value == text)
258 return;
260 text = value;
262 OnUIATextChanged (EventArgs.Empty);
264 if (Parent != null)
265 Parent.Redraw (true);
269 [DefaultValue ("")]
270 [Localizable (true)]
271 public string ToolTipText {
272 get { return tooltip; }
273 set {
274 if (value == null) value = "";
275 tooltip = value;
279 [DefaultValue (true)]
280 [Localizable (true)]
281 public bool Visible {
282 get { return visible; }
283 set {
284 if (value == visible)
285 return;
287 visible = value;
288 if (Parent != null)
289 Parent.Redraw (true);
293 #endregion
295 #region internal methods
297 internal void SetParent (ToolBar parent)
299 if (Parent == parent)
300 return;
302 if (Parent != null)
303 Parent.Buttons.Remove (this);
305 this.parent = parent;
308 internal void Invalidate ()
310 if (Parent != null)
311 Parent.Invalidate (Rectangle);
314 bool uiaHasFocus = false;
315 internal bool UIAHasFocus {
316 get { return uiaHasFocus; }
317 set {
318 uiaHasFocus = value;
319 EventHandler eh =
320 (EventHandler) (value ? Events [UIAGotFocusEvent] : Events [UIALostFocusEvent]);
321 if (eh != null)
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]);
366 if (eh != null)
367 eh (this, e);
370 private void OnUIAEnabledChanged (EventArgs e)
372 EventHandler eh = (EventHandler)(Events [UIAEnabledChangedEvent]);
373 if (eh != null)
374 eh (this, e);
377 private void OnUIADropDownMenuChanged (EventArgs e)
379 EventHandler eh = (EventHandler)(Events [UIADropDownMenuChangedEvent]);
380 if (eh != null)
381 eh (this, e);
384 private void OnUIAStyleChanged (EventArgs e)
386 EventHandler eh = (EventHandler)(Events [UIAStyleChangedEvent]);
387 if (eh != null)
388 eh (this, e);
391 #endregion Internal Methods
393 #region 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);
405 #endregion