* X11Keyboard.cs: Detect and use the num lock mask.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolBarButton.cs
1 //
2 // System.Windows.Forms.ToolBarButton.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (C) 2004 Novell, Inc. (http://www.novell.com)
24 //
25 // Authors:
26 //      Ravindra (rkumar@novell.com)
27 //
28 // TODO:
29 //     - Adding a button to two toolbars
30 //
31 // $Revision: 1.9 $
32 // $Modtime: $
33 // $Log: ToolBarButton.cs,v $
34 // Revision 1.9  2004/10/14 11:10:47  ravindra
35 //      - Added an internal member 'inside' to handle mouse move
36 //      with mouse pressed ie mouse drag event.
37 //      - Changed 'Pressed' property to return true only when
38 //      'inside' and 'pressed' are both true.
39 //      - Some coding style love.
40 //
41 // Revision 1.8  2004/10/05 08:42:20  ravindra
42 // Added an internal member dd_pressed to handle clicks on DropDown arrow.
43 //
44 // Revision 1.7  2004/09/09 11:23:05  ravindra
45 // Changes in ToolBarButton need to make it's parent redraw.
46 //
47 // Revision 1.6  2004/08/27 22:12:56  ravindra
48 // Added TypeConverter attribute.
49 //
50 // Revision 1.5  2004/08/25 20:04:40  ravindra
51 // Added the missing divider code and grip for ToolBar Control.
52 //
53 // Revision 1.4  2004/08/22 00:03:20  ravindra
54 // Fixed toolbar control signatures.
55 //
56 // Revision 1.3  2004/08/21 01:52:08  ravindra
57 // Improvments in mouse event handling in the ToolBar control.
58 //
59 // Revision 1.2  2004/08/17 02:00:54  ravindra
60 // Added attributes.
61 //
62 // Revision 1.1  2004/08/15 23:13:15  ravindra
63 // First Implementation of ToolBar control.
64 //
65 //
66
67 // NOT COMPLETE
68
69 using System.ComponentModel;
70 using System.ComponentModel.Design;
71 using System.Drawing;
72 using System.Drawing.Imaging;
73
74 namespace System.Windows.Forms
75 {
76         [DefaultProperty ("Text")]
77         [Designer ("System.Windows.Forms.Design.ToolBarButtonDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
78         [DesignTimeVisible (false)]
79         [ToolboxItem (false)]
80         public class ToolBarButton : Component
81         {
82                 #region instance variable
83                 private bool enabled = true;
84                 private int image_index = -1;
85                 private ContextMenu menu;
86                 private ToolBar parent;
87                 private bool partial_push = false;
88                 private bool pushed = false;
89                 private ToolBarButtonStyle style = ToolBarButtonStyle.PushButton;
90                 private object tag;
91                 private string text = "";
92                 private string tooltip = "";
93                 private bool visible = true;
94                 private Point location = new Point (ThemeEngine.Current.ToolBarGripWidth,
95                                                     ThemeEngine.Current.ToolBarGripWidth);
96                 internal bool dd_pressed = false; // to check for a mouse down on dropdown rect
97                 internal bool hilight = false;    // to hilight buttons in flat style
98                 internal bool inside = false;     // to handle the mouse move event with mouse pressed
99                 internal bool wrapper = false;    // to mark a wrapping button
100                 internal bool pressed = false;    // this is to check for mouse down on a button
101                 #endregion
102
103                 #region constructors
104                 public ToolBarButton () { }
105
106                 public ToolBarButton (string text)
107                 {
108                         this.text = text;
109                 }
110                 #endregion
111
112                 #region internal properties
113                 internal bool Hilight {
114                         get { return hilight; }
115                         set {
116                                 if (! pushed)
117                                         hilight = value;
118                                 else
119                                         hilight = false;        
120                         }
121                 }
122
123                 internal Point Location {
124                         get { return location; }
125                         set { location = value; }
126                 }
127
128                 internal bool Pressed {
129                         get {
130                                 if (pressed && inside)
131                                         return true;
132                                 else
133                                         return false;
134                         }
135                         set { pressed = value; }
136                 }
137
138                 internal bool Wrapper {
139                         get { return wrapper; }
140                         set { wrapper = value; }
141                 }
142                 #endregion internal properties
143
144                 #region properties
145                 [DefaultValue (null)]
146                 [TypeConverter (typeof (ReferenceConverter))]
147                 public Menu DropDownMenu {
148                         get { return menu; }
149
150                         set {
151                                 if (value is ContextMenu)
152                                         menu = (ContextMenu) value;
153                                 else
154                                         throw new ArgumentException ("DropDownMenu must be of type ContextMenu.");
155                         }
156                 }
157
158                 [DefaultValue (true)]
159                 [Localizable (true)]
160                 public bool Enabled {
161                         get { return enabled; }
162                         set {
163                                 if (value == enabled)
164                                         return;
165
166                                 enabled = value;
167                                 if (parent != null)
168                                         parent.Redraw (false);
169                         }
170                 }
171
172                 [DefaultValue (-1)]
173                 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
174                 [Localizable (true)]
175                 [TypeConverter (typeof (ImageIndexConverter))]
176                 public int ImageIndex {
177                         get { return image_index; }
178                         set {
179                                 if (value < -1)
180                                         throw new ArgumentException ("ImageIndex value must be above or equal to -1.");
181
182                                 if (value == image_index)
183                                         return;
184
185                                 image_index = value;
186                                 if (parent != null)
187                                         parent.Redraw (true);
188                         }
189                 }
190
191                 [Browsable (false)]
192                 public ToolBar Parent {
193                         get { return parent; }
194                 }
195
196                 [DefaultValue (false)]
197                 public bool PartialPush {
198                         get { return partial_push; }
199                         set {
200                                 if (value == partial_push)
201                                         return;
202
203                                 partial_push = value;
204                                 if (parent != null)
205                                         parent.Redraw (false);
206                         }
207                 }
208
209                 [DefaultValue (false)]
210                 public bool Pushed {
211                         get { return pushed; }
212                         set {
213                                 if (value == pushed)
214                                         return;
215
216                                 pushed = value;
217                                 if (pushed)
218                                         hilight = false;
219                                 if (parent != null)
220                                         parent.Redraw (false);
221                         }
222                 }
223
224                 public Rectangle Rectangle {
225                         get {
226                                 if (parent == null)
227                                         return Rectangle.Empty;
228                                 else if (visible && parent.Visible)
229                                         return parent.GetChildBounds (this);
230                                 else
231                                         return Rectangle.Empty;
232                         }
233                 }
234
235                 [DefaultValue (ToolBarButtonStyle.PushButton)]
236                 [RefreshProperties (RefreshProperties.Repaint)]
237                 public ToolBarButtonStyle Style {
238                         get { return style; }
239                         set {
240                                 if (value == style)
241                                         return;
242
243                                 style = value;
244                                 if (parent != null)
245                                         parent.Redraw (true);
246                         }
247                 }
248
249                 [Bindable (true)]
250                 [DefaultValue (null)]
251                 [Localizable (false)]
252                 [TypeConverter (typeof (StringConverter))]
253                 public object Tag {
254                         get { return tag; }
255                         set { tag = value; }
256                 }
257
258                 [DefaultValue (null)]
259                 [Localizable (true)]
260                 public string Text {
261                         get { return text; }
262                         set {
263                                 if (value == text)
264                                         return;
265
266                                 text = value;
267                                 if (parent != null)
268                                         parent.Redraw (true);
269                         }
270                 }
271
272                 [DefaultValue (null)]
273                 [Localizable (true)]
274                 public string ToolTipText {
275                         get { return tooltip; }
276                         set { tooltip = value; }
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                 #endregion
293
294                 #region internal methods
295                 internal void SetParent (ToolBar parent)
296                 {
297                         this.parent = parent;
298                 }
299
300                 internal void Dump ()
301                 {
302                         Console.WriteLine ("TBButton: style: " + this.Style);
303                         Console.WriteLine ("TBButton: wrapper: " + this.Wrapper);
304                         Console.WriteLine ("TBButton: hilight: " + this.Hilight);
305                         Console.WriteLine ("TBButton: loc: " + this.Location);
306                         Console.WriteLine ("TBButton: rect: " + this.Rectangle);
307                         Console.WriteLine ("TBButton: txt: " + this.Text);
308                         Console.WriteLine ("TBButton: visible " + this.Visible);
309                         Console.WriteLine ("TBButton: enabled: " + this.Enabled);
310                         Console.WriteLine ("TBButton: image index: " + this.ImageIndex);
311                         Console.WriteLine ("TBButton: pushed: " + this.Pushed);
312                         Console.WriteLine ("TBButton: partial push: " + this.PartialPush);
313                 }
314                 #endregion
315
316                 #region methods
317                 protected override void Dispose (bool disposing)
318                 {
319                         base.Dispose (disposing);
320                 }
321
322                 public override string ToString ()
323                 {
324                         return string.Format ("ToolBarButton: {0}, Style: {1}", text, style);
325                 }
326                 #endregion
327         }
328 }