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