2006-12-04 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripComboBox.cs
1 //
2 // ToolStripComboBox.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 using System.Drawing;
30 using System.ComponentModel;
31
32 namespace System.Windows.Forms
33 {
34         [DefaultProperty ("Items")]
35         public class ToolStripComboBox : ToolStripControlHost
36         {
37                 #region Public Constructors
38                 public ToolStripComboBox () : base (new ToolStripComboBoxControl ())
39                 {
40                 }
41
42                 public ToolStripComboBox (Control c) : base (c)
43                 {
44                         throw new NotSupportedException ();
45                 }
46
47                 public ToolStripComboBox (string name) : this ()
48                 {
49                         base.Control.Name = name;
50                 }
51                 #endregion
52
53                 #region Public Properties
54                 [Browsable (false)]
55                 public ComboBox ComboBox {
56                         get { return (ComboBox)base.Control; }
57                 }
58
59                 [MonoTODO ("Stub, will not actually affect anything.")]
60                 public int DropDownHeight {
61                         get { return 50; }
62                         set { }
63                 }
64                 
65                 [DefaultValue (ComboBoxStyle.DropDown)]
66                 public ComboBoxStyle DropDownStyle {
67                         get { return this.ComboBox.DropDownStyle; }
68                         set { this.ComboBox.DropDownStyle = value; }
69                 }
70
71                 public int DropDownWidth {
72                         get { return this.ComboBox.DropDownWidth; }
73                         set { this.ComboBox.DropDownWidth = value; }
74                 }
75
76                 [Browsable (false)]
77                 public bool DroppedDown {
78                         get { return this.ComboBox.DroppedDown; }
79                         set { this.ComboBox.DroppedDown = value; }
80                 }
81
82                 [Localizable (true)]
83                 [DefaultValue (true)]
84                 public bool IntegralHeight {
85                         get { return this.ComboBox.IntegralHeight; }
86                         set { this.ComboBox.IntegralHeight = value; }
87                 }
88
89                 [Localizable (true)]
90                 public ComboBox.ObjectCollection Items {
91                         get { return this.ComboBox.Items; }
92                 }
93
94                 [Localizable (true)]
95                 [DefaultValue (8)]
96                 public int MaxDropDownItems {
97                         get { return this.ComboBox.MaxDropDownItems; }
98                         set { this.ComboBox.MaxDropDownItems = value; }
99                 }
100
101                 [Localizable (true)]
102                 [DefaultValue (0)]
103                 public int MaxLength {
104                         get { return this.ComboBox.MaxLength; }
105                         set { this.ComboBox.MaxLength = value; }
106                 }
107
108                 [Browsable (false)]
109                 public int SelectedIndex {
110                         get { return this.ComboBox.SelectedIndex; }
111                         set { this.ComboBox.SelectedIndex = value; }
112                 }
113
114                 [Bindable (true)]
115                 [Browsable (false)]
116                 public Object SelectedItem {
117                         get { return this.ComboBox.SelectedItem; }
118                         set { this.ComboBox.SelectedItem = value; }
119                 }
120
121                 [Browsable (false)]
122                 public string SelectedText {
123                         get { return this.ComboBox.SelectedText; }
124                         set { this.ComboBox.SelectedText = value; }
125                 }
126
127                 [Browsable (false)]
128                 public int SelectionLength {
129                         get { return this.ComboBox.SelectionLength; }
130                         set { this.ComboBox.SelectionLength = value; }
131                 }
132
133                 [Browsable (false)]
134                 public int SelectionStart {
135                         get { return this.ComboBox.SelectionStart; }
136                         set { this.ComboBox.SelectionStart = value; }
137                 }
138
139                 [DefaultValue (false)]
140                 public bool Sorted {
141                         get { return this.ComboBox.Sorted; }
142                         set { this.ComboBox.Sorted = value; }
143                 }
144                 #endregion
145
146                 #region Protected Properties
147                 protected internal override Padding DefaultMargin { get { return new Padding (2); } }
148                 protected override Size DefaultSize { get { return new Size (100, 22); } }
149                 #endregion
150
151                 #region Public Methods
152                 public void BeginUpdate ()
153                 {
154                         this.ComboBox.BeginUpdate ();
155                 }
156
157                 public void EndUpdate ()
158                 {
159                         this.ComboBox.EndUpdate ();
160                 }
161
162                 public int FindString (string s)
163                 {
164                         return this.ComboBox.FindString (s);
165                 }
166
167                 public int FindString (string s, int startIndex)
168                 {
169                         return this.ComboBox.FindString (s, startIndex);
170                 }
171
172                 public int FindStringExact (string s)
173                 {
174                         return this.ComboBox.FindStringExact (s);
175                 }
176
177                 public int FindStringExact (string s, int startIndex)
178                 {
179                         return this.ComboBox.FindStringExact (s, startIndex);
180                 }
181
182                 public int GetItemHeight (int index)
183                 {
184                         return this.ComboBox.GetItemHeight (index);
185                 }
186
187                 public override Size GetPreferredSize (Size constrainingSize)
188                 {
189                         return base.GetPreferredSize (constrainingSize);
190                 }
191
192                 public void Select (int start, int length)
193                 {
194                         this.ComboBox.Select (start, length);
195                 }
196
197                 public void SelectAll ()
198                 {
199                         this.ComboBox.SelectAll ();
200                 }
201
202                 public override string ToString ()
203                 {
204                         return this.ComboBox.ToString ();
205                 }
206                 #endregion
207
208                 #region Protected Methods
209                 protected virtual void OnDropDown (EventArgs e)
210                 {
211                         if (DropDown != null) DropDown (this, e);
212                 }
213
214                 protected virtual void OnDropDownClosed (EventArgs e)
215                 {
216                         if (DropDownClosed != null) DropDownClosed (this, e);
217                 }
218
219                 protected virtual void OnDropDownStyleChanged (EventArgs e)
220                 {
221                         if (DropDownStyleChanged != null) DropDownStyleChanged (this, e);
222                 }
223
224                 protected virtual void OnSelectedIndexChanged (EventArgs e)
225                 {
226                         if (SelectedIndexChanged != null) SelectedIndexChanged (this, e);
227                 }
228
229                 protected virtual void OnSelectionChangeCommitted (EventArgs e)
230                 {
231                 }
232
233                 [MonoTODO ("Needs to hook into DropDownClosed and TextUpdate when ComboBox 2.0 has them.")]
234                 protected override void OnSubscribeControlEvents (Control control)
235                 {
236                         base.OnSubscribeControlEvents (control);
237
238                         this.ComboBox.DropDown += new EventHandler (HandleDropDown);
239                         this.ComboBox.DropDownStyleChanged += new EventHandler (HandleDropDownStyleChanged);
240                         this.ComboBox.SelectedIndexChanged += new EventHandler (HandleSelectedIndexChanged);
241                 }
242
243                 protected virtual void OnTextUpdate (EventArgs e)
244                 {
245                         if (TextUpdate != null) TextUpdate (this, e);
246                 }
247
248                 protected override void OnUnsubscribeControlEvents (Control control)
249                 {
250                         base.OnUnsubscribeControlEvents (control);
251                 }
252                 #endregion
253
254                 #region Public Events
255                 [Browsable (false)]
256                 [EditorBrowsable (EditorBrowsableState.Never)]
257                 public new event EventHandler DoubleClick;
258                 public event EventHandler DropDown;
259                 public event EventHandler DropDownClosed;
260                 public event EventHandler DropDownStyleChanged;
261                 public event EventHandler SelectedIndexChanged;
262                 public event EventHandler TextUpdate;
263                 #endregion
264
265                 #region Private Methods
266                 private void HandleDropDown (object sender, EventArgs e)
267                 {
268                         OnDropDown (e);
269                 }
270
271                 private void HandleDropDownStyleChanged (object sender, EventArgs e)
272                 {
273                         OnDisplayStyleChanged (e);
274                 }
275
276                 private void HandleSelectedIndexChanged (object sender, EventArgs e)
277                 {
278                         OnSelectedIndexChanged (e);
279                 }
280                 #endregion
281
282                 private class ToolStripComboBoxControl : ComboBox
283                 {
284                         public ToolStripComboBoxControl () : base ()
285                         {
286                                 this.border_style = BorderStyle.None;
287                         }
288                 }
289         }
290 }
291 #endif