2007-08-28 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripSplitButton.cs
1 //
2 // ToolStripSplitButton.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) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28 #if NET_2_0
29
30 using System;
31 using System.Drawing;
32 using System.ComponentModel;
33 using System.Windows.Forms.Design;
34
35 namespace System.Windows.Forms
36 {
37         [DefaultEvent ("ButtonClick")]
38         [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
39         public class ToolStripSplitButton : ToolStripDropDownItem
40         {
41                 private bool button_pressed;
42                 private ToolStripItem default_item;
43                 private bool drop_down_button_selected;
44                 private int drop_down_button_width;
45                 
46                 #region Public Constructors
47                 public ToolStripSplitButton()
48                         : this (string.Empty, null, null, string.Empty)
49                 {
50                 }
51                 
52                 public ToolStripSplitButton (Image image)
53                         : this (string.Empty, image, null, string.Empty)
54                 {
55                 }
56                 
57                 public ToolStripSplitButton (string text)
58                         : this (text, null, null, string.Empty)
59                 {
60                 }
61                 
62                 public ToolStripSplitButton (string text, Image image)
63                         : this (text, image, null, string.Empty)
64                 {
65                 }
66                 
67                 public ToolStripSplitButton (string text, Image image, EventHandler onClick)
68                         : this (text, image, onClick, string.Empty)
69                 {
70                 }
71                 
72                 public ToolStripSplitButton (string text, Image image, params ToolStripItem[] dropDownItems)
73                         : base (text, image, dropDownItems)
74                 {
75                         this.ResetDropDownButtonWidth ();
76                 }
77
78                 public ToolStripSplitButton (string text, Image image, EventHandler onClick, string name)
79                         : base (text, image, onClick, name)
80                 {
81                         this.ResetDropDownButtonWidth ();
82                 }
83                 #endregion
84
85                 #region Public Properties
86                 [DefaultValue (true)]
87                 public new bool AutoToolTip {
88                         get { return base.AutoToolTip; }
89                         set { base.AutoToolTip = value; }
90                 }
91
92                 [Browsable (false)]
93                 public Rectangle ButtonBounds {
94                         get { return new Rectangle (this.Bounds.Left, this.Bounds.Top, this.Bounds.Width - this.drop_down_button_width - 1, this.Height); }
95                 }
96
97                 [Browsable (false)]
98                 public bool ButtonPressed {
99                         get { return this.button_pressed; }
100                 }
101
102                 [Browsable (false)]
103                 public bool ButtonSelected {
104                         get { return base.Selected; }
105                 }
106
107                 [Browsable (false)]
108                 [DefaultValue (null)]
109                 public ToolStripItem DefaultItem {
110                         get { return this.default_item; }
111                         set {
112                                 if (this.default_item != value) {
113                                         this.default_item = value;
114                                         this.OnDefaultItemChanged (EventArgs.Empty);
115                                 }
116                         }
117                 }
118                 
119                 [Browsable (false)]
120                 public Rectangle DropDownButtonBounds {
121                         get { return new Rectangle (this.Bounds.Right - this.drop_down_button_width, this.Bounds.Top, this.drop_down_button_width, this.Bounds.Height); }
122                 }
123
124                 [Browsable (false)]
125                 public bool DropDownButtonPressed {
126                         get { return this.drop_down_button_selected || (this.HasDropDownItems && this.DropDown.Visible); }
127                 }
128
129                 [Browsable (false)]
130                 public bool DropDownButtonSelected {
131                         get { return base.Selected; }
132                 }
133                 
134                 public int DropDownButtonWidth {
135                         get { return this.drop_down_button_width; }
136                         set { 
137                                 if (value < 0)
138                                         throw new ArgumentOutOfRangeException ();
139                                         
140                                 this.drop_down_button_width = value;
141                         }
142                 }
143
144                 [Browsable (false)]
145                 public Rectangle SplitterBounds {
146                         get { return new Rectangle (this.Bounds.Width - this.drop_down_button_width - 1, this.Bounds.Top, 1, this.Height); }
147                 }
148                 #endregion
149
150                 #region Protected Properties
151                 protected override bool DefaultAutoToolTip {
152                         get { return true; }
153                 }
154
155                 protected internal override bool DismissWhenClicked {
156                         get { return true; }
157                 }
158                 #endregion
159
160                 #region Public Methods
161                 public override Size GetPreferredSize (Size constrainingSize)
162                 {
163                         // base should calculate the button part for us, add the splitter
164                         // and drop down arrow part to that
165                         Size s = base.GetPreferredSize (constrainingSize);
166
167                         if (s.Width < 23)
168                                 s.Width = 23;
169
170                         s.Width += (this.drop_down_button_width - 2);
171                         
172                         return s;
173                 }
174                 
175                 public virtual void OnButtonDoubleClick (EventArgs e)
176                 {
177                         EventHandler eh = (EventHandler)(Events [ButtonDoubleClickEvent]);
178                         if (eh != null)
179                                 eh (this, e);
180                 }
181                 
182                 public void PerformButtonClick ()
183                 {
184                         if (this.Enabled)
185                                 this.OnButtonClick (EventArgs.Empty);
186                 }
187                 
188                 [EditorBrowsable (EditorBrowsableState.Never)]
189                 public virtual void ResetDropDownButtonWidth ()
190                 {
191                         this.DropDownButtonWidth = 11;
192                 }
193                 #endregion
194
195                 #region Protected Methods
196                 protected override AccessibleObject CreateAccessibilityInstance ()
197                 {
198                         return new ToolStripSplitButtonAccessibleObject (this);
199                 }
200                 
201                 protected override ToolStripDropDown CreateDefaultDropDown ()
202                 {
203                         ToolStripDropDownMenu tsddm = new ToolStripDropDownMenu ();
204                         tsddm.OwnerItem = this;
205                         return tsddm;
206                 }
207                 
208                 protected virtual void OnButtonClick (EventArgs e)
209                 {
210                         EventHandler eh = (EventHandler)Events [ButtonClickEvent];
211                         if (eh != null)
212                                 eh (this, e);
213                 }
214                 
215                 protected virtual void OnDefaultItemChanged (EventArgs e)
216                 {
217                         EventHandler eh = (EventHandler)Events [DefaultItemChangedEvent];
218                         if (eh != null)
219                                 eh (this, e);
220                 }
221
222                 protected override void OnMouseDown (MouseEventArgs e)
223                 {
224                         if (this.ButtonBounds.Contains (e.Location))
225                         {
226                                 this.button_pressed = true;
227                                 this.Invalidate ();
228                                 base.OnMouseDown (e);
229                         }
230                         else if (this.DropDownButtonBounds.Contains (e.Location))
231                         {
232                                 if (this.DropDown.Visible)
233                                         this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
234                                 else
235                                         this.ShowDropDown ();
236                         
237                                 this.Invalidate ();
238                         }
239                 }
240
241                 protected override void OnMouseLeave (EventArgs e)
242                 {
243                         this.drop_down_button_selected = false;
244                         this.button_pressed = false;
245                         
246                         this.Invalidate ();
247                         
248                         base.OnMouseLeave (e);
249                 }
250
251                 protected override void OnMouseUp (MouseEventArgs e)
252                 {
253                         this.button_pressed = false;
254                         this.Invalidate ();
255                         
256                         if (this.ButtonBounds.Contains (e.Location))
257                                 base.OnMouseUp (e);
258                 }
259                 
260                 protected override void OnPaint (PaintEventArgs e)
261                 {
262                         base.OnPaint (e);
263
264                         if (this.Owner != null) {
265                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
266                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
267
268                                 this.Owner.Renderer.DrawSplitButton (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
269
270                                 Rectangle text_layout_rect;
271                                 Rectangle image_layout_rect;
272
273                                 Rectangle r = this.ContentRectangle;
274                                 r.Width -= (this.drop_down_button_width + 1);
275                                 
276                                 this.CalculateTextAndImageRectangles (r, out text_layout_rect, out image_layout_rect);
277
278                                 if (text_layout_rect != Rectangle.Empty)
279                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
280                                 if (image_layout_rect != Rectangle.Empty)
281                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
282
283                                 this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Width - 9, 1, 6, this.Height), Color.Black, ArrowDirection.Down));
284                                 
285                                 return;
286                         }
287                 }
288
289                 protected override void OnRightToLeftChanged (EventArgs e)
290                 {
291                         base.OnRightToLeftChanged (e);
292                 }
293                 
294                 protected internal override bool ProcessDialogKey (Keys keyData)
295                 {
296                         if (this.Selected && keyData == Keys.Enter && this.DefaultItem != null) {
297                                 this.DefaultItem.FireEvent (EventArgs.Empty, ToolStripItemEventType.Click);
298                                 return true;
299                         }
300
301                         return base.ProcessDialogKey (keyData);
302                 }
303
304                 protected internal override bool ProcessMnemonic (char charCode)
305                 {
306                         if (!this.Selected)
307                                 this.Parent.ChangeSelection (this);
308
309                         if (this.HasDropDownItems)
310                                 this.ShowDropDown ();
311                         else
312                                 this.PerformClick ();
313
314                         return true;
315                 }
316                 #endregion
317                 
318                 #region Public Events
319                 static object ButtonClickEvent = new object ();
320                 static object ButtonDoubleClickEvent = new object ();
321                 static object DefaultItemChangedEvent = new object ();
322
323                 public event EventHandler ButtonClick {
324                         add { Events.AddHandler (ButtonClickEvent, value); }
325                         remove {Events.RemoveHandler (ButtonClickEvent, value); }
326                 }
327                 public event EventHandler ButtonDoubleClick {
328                         add { Events.AddHandler (ButtonDoubleClickEvent, value); }
329                         remove {Events.RemoveHandler (ButtonDoubleClickEvent, value); }
330                 }
331                 public event EventHandler DefaultItemChanged {
332                         add { Events.AddHandler (DefaultItemChangedEvent, value); }
333                         remove {Events.RemoveHandler (DefaultItemChangedEvent, value); }
334                 }
335                 #endregion
336
337                 #region ToolStripSplitButtonAccessibleObject Class
338                 public class ToolStripSplitButtonAccessibleObject : ToolStripItemAccessibleObject
339                 {
340                         #region Public Constructor
341                         public ToolStripSplitButtonAccessibleObject (ToolStripSplitButton item) : base (item)
342                         {
343                         }
344                         #endregion
345
346                         #region Public Method
347                         public override void DoDefaultAction ()
348                         {
349                                 (owner_item as ToolStripSplitButton).PerformButtonClick ();
350                         }
351                         #endregion
352                 }
353                 #endregion
354         }
355 }
356 #endif