copying the latest Sys.Web.Services from trunk.
[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
32
33 // NOT COMPLETE
34
35 using System.ComponentModel;
36 using System.ComponentModel.Design;
37 using System.Drawing;
38 using System.Drawing.Imaging;
39
40 namespace System.Windows.Forms
41 {
42         [DefaultProperty ("Text")]
43         [Designer ("System.Windows.Forms.Design.ToolBarButtonDesigner, " + Consts.AssemblySystem_Design)]
44         [DesignTimeVisible (false)]
45         [ToolboxItem (false)]
46         public class ToolBarButton : Component
47         {
48                 #region instance variable
49                 private bool enabled = true;
50                 private int image_index = -1;
51                 private ContextMenu menu;
52                 private ToolBar parent;
53                 private bool partial_push = false;
54                 private bool pushed = false;
55                 private ToolBarButtonStyle style = ToolBarButtonStyle.PushButton;
56                 private object tag;
57                 private string text = "";
58                 private string tooltip = "";
59                 private bool visible = true;
60                 private Point location = new Point (ThemeEngine.Current.ToolBarGripWidth,
61                                                     ThemeEngine.Current.ToolBarGripWidth);
62                 internal bool dd_pressed = false; // to check for a mouse down on dropdown rect
63                 internal bool hilight = false;    // to hilight buttons in flat style
64                 internal bool inside = false;     // to handle the mouse move event with mouse pressed
65                 internal bool wrapper = false;    // to mark a wrapping button
66                 internal bool pressed = false;    // this is to check for mouse down on a button
67                 #endregion
68
69                 #region constructors
70                 public ToolBarButton () { }
71
72                 public ToolBarButton (string text)
73                 {
74                         this.text = text;
75                 }
76                 #endregion
77
78                 #region internal properties
79                 internal bool Hilight {
80                         get { return hilight; }
81                         set {
82                                 if (! pushed)
83                                         hilight = value;
84                                 else
85                                         hilight = false;        
86                         }
87                 }
88
89                 internal Point Location {
90                         get { return location; }
91                         set { location = value; }
92                 }
93
94                 internal bool Pressed {
95                         get {
96                                 if (pressed && inside)
97                                         return true;
98                                 else
99                                         return false;
100                         }
101                         set { pressed = value; }
102                 }
103
104                 internal bool Wrapper {
105                         get { return wrapper; }
106                         set { wrapper = value; }
107                 }
108                 #endregion internal properties
109
110                 #region properties
111                 [DefaultValue (null)]
112                 [TypeConverter (typeof (ReferenceConverter))]
113                 public Menu DropDownMenu {
114                         get { return menu; }
115
116                         set {
117                                 if (value is ContextMenu)
118                                         menu = (ContextMenu) value;
119                                 else
120                                         throw new ArgumentException ("DropDownMenu must be of type ContextMenu.");
121                         }
122                 }
123
124                 [DefaultValue (true)]
125                 [Localizable (true)]
126                 public bool Enabled {
127                         get { return enabled; }
128                         set {
129                                 if (value == enabled)
130                                         return;
131
132                                 enabled = value;
133                                 if (parent != null)
134                                         parent.Redraw (false);
135                         }
136                 }
137
138                 [DefaultValue (-1)]
139                 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
140                 [Localizable (true)]
141                 [TypeConverter (typeof (ImageIndexConverter))]
142                 public int ImageIndex {
143                         get { return image_index; }
144                         set {
145                                 if (value < -1)
146                                         throw new ArgumentException ("ImageIndex value must be above or equal to -1.");
147
148                                 if (value == image_index)
149                                         return;
150
151                                 image_index = value;
152                                 if (parent != null)
153                                         parent.Redraw (true);
154                         }
155                 }
156
157                 [Browsable (false)]
158                 public ToolBar Parent {
159                         get { return parent; }
160                 }
161
162                 [DefaultValue (false)]
163                 public bool PartialPush {
164                         get { return partial_push; }
165                         set {
166                                 if (value == partial_push)
167                                         return;
168
169                                 partial_push = value;
170                                 if (parent != null)
171                                         parent.Redraw (false);
172                         }
173                 }
174
175                 [DefaultValue (false)]
176                 public bool Pushed {
177                         get { return pushed; }
178                         set {
179                                 if (value == pushed)
180                                         return;
181
182                                 pushed = value;
183                                 if (pushed)
184                                         hilight = false;
185                                 if (parent != null)
186                                         parent.Redraw (false);
187                         }
188                 }
189
190                 public Rectangle Rectangle {
191                         get {
192                                 if (parent == null)
193                                         return Rectangle.Empty;
194                                 else if (visible && parent.Visible)
195                                         return parent.GetChildBounds (this);
196                                 else
197                                         return Rectangle.Empty;
198                         }
199                 }
200
201                 [DefaultValue (ToolBarButtonStyle.PushButton)]
202                 [RefreshProperties (RefreshProperties.Repaint)]
203                 public ToolBarButtonStyle Style {
204                         get { return style; }
205                         set {
206                                 if (value == style)
207                                         return;
208
209                                 style = value;
210                                 if (parent != null)
211                                         parent.Redraw (true);
212                         }
213                 }
214
215                 [Bindable (true)]
216                 [DefaultValue (null)]
217                 [Localizable (false)]
218                 [TypeConverter (typeof (StringConverter))]
219                 public object Tag {
220                         get { return tag; }
221                         set { tag = value; }
222                 }
223
224                 [DefaultValue ("")]
225                 [Localizable (true)]
226                 public string Text {
227                         get { return text; }
228                         set {
229                                 if (value == text)
230                                         return;
231
232                                 text = value;
233                                 if (parent != null)
234                                         parent.Redraw (true);
235                         }
236                 }
237
238                 [DefaultValue ("")]
239                 [Localizable (true)]
240                 public string ToolTipText {
241                         get { return tooltip; }
242                         set { tooltip = value; }
243                 }
244
245                 [DefaultValue (true)]
246                 [Localizable (true)]
247                 public bool Visible {
248                         get { return visible; }
249                         set {
250                                 if (value == visible)
251                                         return;
252
253                                 visible = value;
254                                 if (parent != null)
255                                         parent.Redraw (true);
256                         }
257                 }
258                 #endregion
259
260                 #region internal methods
261                 internal void SetParent (ToolBar parent)
262                 {
263                         this.parent = parent;
264                 }
265
266                 internal void Dump ()
267                 {
268                         Console.WriteLine ("TBButton: style: " + this.Style);
269                         Console.WriteLine ("TBButton: wrapper: " + this.Wrapper);
270                         Console.WriteLine ("TBButton: hilight: " + this.Hilight);
271                         Console.WriteLine ("TBButton: loc: " + this.Location);
272                         Console.WriteLine ("TBButton: rect: " + this.Rectangle);
273                         Console.WriteLine ("TBButton: txt: " + this.Text);
274                         Console.WriteLine ("TBButton: visible " + this.Visible);
275                         Console.WriteLine ("TBButton: enabled: " + this.Enabled);
276                         Console.WriteLine ("TBButton: image index: " + this.ImageIndex);
277                         Console.WriteLine ("TBButton: pushed: " + this.Pushed);
278                         Console.WriteLine ("TBButton: partial push: " + this.PartialPush);
279                 }
280                 #endregion
281
282                 #region methods
283                 protected override void Dispose (bool disposing)
284                 {
285                         base.Dispose (disposing);
286                 }
287
288                 public override string ToString ()
289                 {
290                         return string.Format ("ToolBarButton: {0}, Style: {1}", text, style);
291                 }
292                 #endregion
293         }
294 }