* TabControl.cs: Show the tooltip depending on the value
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripDropDownButton.cs
1 //
2 // ToolStripDropDownButton.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
29 #if NET_2_0
30 using System.Drawing;
31 using System.Runtime.InteropServices;
32 using System.Windows.Forms.Design;
33 using System.ComponentModel;
34
35 namespace System.Windows.Forms
36 {
37         [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
38         public class ToolStripDropDownButton : ToolStripDropDownItem
39         {
40                 private bool show_drop_down_arrow = true;
41
42                 #region Public Constructors
43                 public ToolStripDropDownButton()
44                         : this (string.Empty, null, null, string.Empty)
45                 {
46                 }
47                 
48                 public ToolStripDropDownButton (Image image)
49                         : this (string.Empty, image, null, string.Empty)
50                 {
51                 }
52                 
53                 public ToolStripDropDownButton (string text)
54                         : this (text, null, null, string.Empty)
55                 {
56                 }
57                 
58                 public ToolStripDropDownButton (string text, Image image)
59                         : this (text, image, null, string.Empty)
60                 {
61                 }
62                 
63                 public ToolStripDropDownButton (string text, Image image, EventHandler onClick)
64                         : this (text, image, onClick, string.Empty)
65                 {
66                 }
67                 
68                 public ToolStripDropDownButton (string text, Image image, params ToolStripItem[] dropDownItems)
69                         : base (text, image, dropDownItems)
70                 {
71                 }
72                 
73                 public ToolStripDropDownButton (string text, Image image, EventHandler onClick, string name)
74                         : base (text, image, onClick, name)
75                 {
76                 }
77                 #endregion
78
79                 #region Public Properties
80                 [DefaultValue (true)]
81                 public new bool AutoToolTip {
82                         get { return base.AutoToolTip; }
83                         set { base.AutoToolTip = value; }
84                 }
85                 
86                 [DefaultValue (true)]
87                 public bool ShowDropDownArrow {
88                         get { return this.show_drop_down_arrow; }
89                         set { 
90                                 if (this.show_drop_down_arrow != value) {
91                                         this.show_drop_down_arrow = value;
92                                         CalculateAutoSize ();
93                                 }
94                         }
95                 }
96                 #endregion
97
98                 #region Protected Properties
99                 protected override bool DefaultAutoToolTip {
100                         get { return true; }
101                 }
102                 #endregion
103
104                 #region Protected Methods
105                 protected override ToolStripDropDown CreateDefaultDropDown ()
106                 {
107                         ToolStripDropDownMenu tsdd = new ToolStripDropDownMenu ();
108                         tsdd.OwnerItem = this;
109                         return tsdd;
110                 }
111
112                 protected override void OnMouseDown (MouseEventArgs e)
113                 {
114                         if (e.Button == MouseButtons.Left) {
115                                 if (this.DropDown.Visible)
116                                         this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
117                                 else
118                                         this.ShowDropDown ();
119                         }
120                         
121                         base.OnMouseDown (e);
122                 }
123
124                 protected override void OnMouseLeave (EventArgs e)
125                 {
126                         base.OnMouseLeave (e);
127                 }
128
129                 protected override void OnMouseUp (MouseEventArgs e)
130                 {
131                         base.OnMouseUp (e);
132                 }
133                 
134                 protected override void OnPaint (PaintEventArgs e)
135                 {
136                         base.OnPaint (e);
137
138                         if (this.Owner != null) {
139                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
140                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
141
142                                 this.Owner.Renderer.DrawDropDownButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
143
144                                 Rectangle text_layout_rect;
145                                 Rectangle image_layout_rect;
146
147                                 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
148
149                                 if (text_layout_rect != Rectangle.Empty)
150                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
151                                 if (image_layout_rect != Rectangle.Empty)
152                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
153                                 if (this.ShowDropDownArrow)
154                                         this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Width - 10, 0, 6, this.Height), Color.Black, ArrowDirection.Down));
155                                 return;
156                         }
157                 }
158
159                 protected internal override bool ProcessMnemonic (char charCode)
160                 {
161                         if (!this.Selected)
162                                 this.Parent.ChangeSelection (this);
163
164                         if (this.HasDropDownItems)
165                                 this.ShowDropDown ();
166                         else
167                                 this.PerformClick ();
168
169                         return true;
170                 }
171                 #endregion
172
173                 #region Internal Methods
174                 internal override Size CalculatePreferredSize (Size constrainingSize)
175                 {
176                         Size preferred_size = base.CalculatePreferredSize (constrainingSize);
177
178                         if (this.ShowDropDownArrow)
179                                 preferred_size.Width += 9;
180
181                         return preferred_size;
182                 }
183                 #endregion
184         }
185 }
186 #endif