Fix bug #395
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolBarButton.cs
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.
21 //
22 // Copyright (C) 2004-2006 Novell, Inc. (http://www.novell.com)
23 //
24 // Authors:
25 //      Ravindra (rkumar@novell.com)
26 //      Mike Kestner <mkestner@novell.com>
27 //      Everaldo Canuto <ecanuto@novell.com>
28
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
31 using System.Drawing;
32 using System.Drawing.Text;
33 using System.Drawing.Imaging;
34
35 namespace System.Windows.Forms
36 {
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
42         {
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
58
59                 #region constructors
60                 
61                 public ToolBarButton () { }
62
63                 public ToolBarButton (string text)
64                 {
65                         this.text = text;
66                 }
67                 
68                 #endregion
69
70                 #region internal properties
71
72                 internal Image Image {
73                         get {
74                                 if (Parent == null || Parent.ImageList == null)
75                                         return null;
76
77                                 ImageList list = Parent.ImageList;
78                                 if (ImageIndex > -1 && ImageIndex < list.Images.Count)
79                                         return list.Images [ImageIndex];
80
81                                 if (!string.IsNullOrEmpty (image_key))
82                                         return list.Images [image_key];
83
84                                 return null;
85                         }
86                 }
87
88                 #endregion internal properties
89
90                 #region properties
91
92                 [DefaultValue (null)]
93                 [TypeConverter (typeof (ReferenceConverter))]
94                 public Menu DropDownMenu {
95                         get { return menu; }
96
97                         set {
98                                 if (value is ContextMenu)
99                                         menu = (ContextMenu) value;
100                                 else
101                                         throw new ArgumentException ("DropDownMenu must be of type ContextMenu.");
102
103                                 OnUIADropDownMenuChanged (EventArgs.Empty);
104                         }
105                 }
106
107                 [DefaultValue (true)]
108                 [Localizable (true)]
109                 public bool Enabled {
110                         get { return enabled; }
111                         set {
112                                 if (value == enabled)
113                                         return;
114
115                                 enabled = value;
116                                 Invalidate ();
117                                 
118                                 OnUIAEnabledChanged (EventArgs.Empty);
119                         }
120                 }
121
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.");
132
133                                 if (value == image_index)
134                                         return;
135
136                                 bool layout = (Parent != null) && ((value == -1) || (image_index == -1));
137                                 
138                                 image_index = value;
139                                 image_key = string.Empty;
140
141                                 if (layout)
142                                         Parent.Redraw (true);
143                                 else
144                                         Invalidate ();
145                         }
146                 }
147
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;
158                                 
159                                 bool layout = (Parent != null) && ((value == string.Empty) || (image_key == string.Empty));
160                                 
161                                 image_index = -1;
162                                 image_key = value;
163                                 
164                                 if (layout)
165                                         Parent.Redraw (true);
166                                 else
167                                         Invalidate ();
168                         }
169                 }
170
171                 [Browsable (false)]
172                 public string Name {
173                         get {
174                                 if (name == null)
175                                         return string.Empty;
176                                         
177                                 return name;
178                         }
179                         set {
180                                 name = value;
181                         }
182                 }
183
184                 [Browsable (false)]
185                 public ToolBar Parent {
186                         get { return parent; }
187                 }
188
189                 [DefaultValue (false)]
190                 public bool PartialPush {
191                         get { return partial_push; }
192                         set {
193                                 if (value == partial_push)
194                                         return;
195
196                                 partial_push = value;
197                                 Invalidate ();
198                         }
199                 }
200
201                 [DefaultValue (false)]
202                 public bool Pushed {
203                         get { return pushed; }
204                         set {
205                                 if (value == pushed)
206                                         return;
207
208                                 pushed = value;
209                                 Invalidate ();
210                         }
211                 }
212
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;
219                                         
220                                 return Rectangle.Empty;
221                         }
222                 }
223
224                 [DefaultValue (ToolBarButtonStyle.PushButton)]
225                 [RefreshProperties (RefreshProperties.Repaint)]
226                 public ToolBarButtonStyle Style {
227                         get { return style; }
228                         set {
229                                 if (value == style)
230                                         return;
231
232                                 style = value;
233                                 
234                                 if (parent != null)
235                                         parent.Redraw (true);
236                                 
237                                 OnUIAStyleChanged (EventArgs.Empty);
238                         }
239                 }
240
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; }
248                 }
249
250                 [DefaultValue ("")]
251                 [Localizable (true)]
252                 public string Text {
253                         get { return text; }
254                         set {
255                                 if (value == null) value = "";
256
257                                 if (value == text)
258                                         return;
259
260                                 text = value;
261
262                                 OnUIATextChanged (EventArgs.Empty);
263                                 
264                                 if (Parent != null)
265                                         Parent.Redraw (true);
266                         }
267                 }
268
269                 [DefaultValue ("")]
270                 [Localizable (true)]
271                 public string ToolTipText {
272                         get { return tooltip; }
273                         set {
274                                 if (value == null) value = "";
275                                 tooltip = value;
276                         }
277                 }
278
279                 [DefaultValue (true)]
280                 [Localizable (true)]
281                 public bool Visible {
282                         get { return visible; }
283                         set {
284                                 if (value == visible)
285                                         return;
286
287                                 visible = value;
288                                 if (Parent != null)
289                                         Parent.Redraw (true);
290                         }
291                 }
292
293                 #endregion
294
295                 #region internal methods
296
297                 internal void SetParent (ToolBar parent)
298                 {
299                         if (Parent == parent)
300                                 return;
301
302                         if (Parent != null)
303                                 Parent.Buttons.Remove (this);
304
305                         this.parent = parent;
306                 }
307
308                 internal void Invalidate ()
309                 {
310                         if (Parent != null)
311                                 Parent.Invalidate (Rectangle);
312                 }
313                 
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);
323                         }
324                 }
325
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 ();
332                 
333                 internal event EventHandler UIAGotFocus {
334                         add { Events.AddHandler (UIAGotFocusEvent, value); }
335                         remove { Events.RemoveHandler (UIAGotFocusEvent, value); }
336                 }
337                 
338                 internal event EventHandler UIALostFocus {
339                         add { Events.AddHandler (UIALostFocusEvent, value); }
340                         remove { Events.RemoveHandler (UIALostFocusEvent, value); }
341                 }
342                 
343                 internal event EventHandler UIATextChanged {
344                         add { Events.AddHandler (UIATextChangedEvent, value); }
345                         remove { Events.RemoveHandler (UIATextChangedEvent, value); }
346                 }
347                 
348                 internal event EventHandler UIAEnabledChanged {
349                         add { Events.AddHandler (UIAEnabledChangedEvent, value); }
350                         remove { Events.RemoveHandler (UIAEnabledChangedEvent, value); }
351                 }
352                 
353                 internal event EventHandler UIADropDownMenuChanged {
354                         add { Events.AddHandler (UIADropDownMenuChangedEvent, value); }
355                         remove { Events.RemoveHandler (UIADropDownMenuChangedEvent, value); }
356                 }
357                 
358                 internal event EventHandler UIAStyleChanged {
359                         add { Events.AddHandler (UIAStyleChangedEvent, value); }
360                         remove { Events.RemoveHandler (UIAStyleChangedEvent, value); }
361                 }
362                 
363                 private void OnUIATextChanged(EventArgs e)
364                 {
365                         EventHandler eh = (EventHandler)(Events [UIATextChangedEvent]);
366                         if (eh != null)
367                                 eh (this, e);
368                 }
369                 
370                 private void OnUIAEnabledChanged (EventArgs e)
371                 {
372                         EventHandler eh = (EventHandler)(Events [UIAEnabledChangedEvent]);
373                         if (eh != null)
374                                 eh (this, e);
375                 }
376                 
377                 private void OnUIADropDownMenuChanged (EventArgs e)
378                 {
379                         EventHandler eh = (EventHandler)(Events [UIADropDownMenuChangedEvent]);
380                         if (eh != null)
381                                 eh (this, e);
382                 }
383                 
384                 private void OnUIAStyleChanged (EventArgs e)
385                 {
386                         EventHandler eh = (EventHandler)(Events [UIAStyleChangedEvent]);
387                         if (eh != null)
388                                 eh (this, e);
389                 }
390                 
391                 #endregion Internal Methods
392
393                 #region methods
394
395                 protected override void Dispose (bool disposing)
396                 {
397                         base.Dispose (disposing);
398                 }
399
400                 public override string ToString ()
401                 {
402                         return string.Format ("ToolBarButton: {0}, Style: {1}", text, style);
403                 }
404                 
405                 #endregion
406         }
407 }