2006-12-24 Miguel de Icaza <miguel@novell.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 {
112                                 this.ComboBox.SelectedIndex = value;
113                                 Text = Items [value].ToString ();
114                         }
115                 }
116
117                 [Bindable (true)]
118                 [Browsable (false)]
119                 public Object SelectedItem {
120                         get { return this.ComboBox.SelectedItem; }
121                         set { this.ComboBox.SelectedItem = value; }
122                 }
123
124                 [Browsable (false)]
125                 public string SelectedText {
126                         get { return this.ComboBox.SelectedText; }
127                         set { this.ComboBox.SelectedText = value; }
128                 }
129
130                 [Browsable (false)]
131                 public int SelectionLength {
132                         get { return this.ComboBox.SelectionLength; }
133                         set { this.ComboBox.SelectionLength = value; }
134                 }
135
136                 [Browsable (false)]
137                 public int SelectionStart {
138                         get { return this.ComboBox.SelectionStart; }
139                         set { this.ComboBox.SelectionStart = value; }
140                 }
141
142                 [DefaultValue (false)]
143                 public bool Sorted {
144                         get { return this.ComboBox.Sorted; }
145                         set { this.ComboBox.Sorted = value; }
146                 }
147                 #endregion
148
149                 #region Protected Properties
150                 protected internal override Padding DefaultMargin { get { return new Padding (2); } }
151                 protected override Size DefaultSize { get { return new Size (100, 22); } }
152                 #endregion
153
154                 #region Public Methods
155                 public void BeginUpdate ()
156                 {
157                         this.ComboBox.BeginUpdate ();
158                 }
159
160                 public void EndUpdate ()
161                 {
162                         this.ComboBox.EndUpdate ();
163                 }
164
165                 public int FindString (string s)
166                 {
167                         return this.ComboBox.FindString (s);
168                 }
169
170                 public int FindString (string s, int startIndex)
171                 {
172                         return this.ComboBox.FindString (s, startIndex);
173                 }
174
175                 public int FindStringExact (string s)
176                 {
177                         return this.ComboBox.FindStringExact (s);
178                 }
179
180                 public int FindStringExact (string s, int startIndex)
181                 {
182                         return this.ComboBox.FindStringExact (s, startIndex);
183                 }
184
185                 public int GetItemHeight (int index)
186                 {
187                         return this.ComboBox.GetItemHeight (index);
188                 }
189
190                 public override Size GetPreferredSize (Size constrainingSize)
191                 {
192                         return base.GetPreferredSize (constrainingSize);
193                 }
194
195                 public void Select (int start, int length)
196                 {
197                         this.ComboBox.Select (start, length);
198                 }
199
200                 public void SelectAll ()
201                 {
202                         this.ComboBox.SelectAll ();
203                 }
204
205                 public override string ToString ()
206                 {
207                         return this.ComboBox.ToString ();
208                 }
209                 #endregion
210
211                 #region Protected Methods
212                 protected virtual void OnDropDown (EventArgs e)
213                 {
214                         EventHandler eh = (EventHandler)(Events [DropDownEvent]);
215                         if (eh != null)
216                                 eh (this, e);
217                 }
218
219                 protected virtual void OnDropDownClosed (EventArgs e)
220                 {
221                         EventHandler eh = (EventHandler)(Events [DropDownClosedEvent]);
222                         if (eh != null)
223                                 eh (this, e);
224                 }
225
226                 protected virtual void OnDropDownStyleChanged (EventArgs e)
227                 {
228                         EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
229                         if (eh != null)
230                                 eh (this, e);
231                 }
232
233                 protected virtual void OnSelectedIndexChanged (EventArgs e)
234                 {
235                         EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
236                         if (eh != null)
237                                 eh (this, e);
238                 }
239
240                 protected virtual void OnSelectionChangeCommitted (EventArgs e)
241                 {
242                 }
243
244                 [MonoTODO ("Needs to hook into DropDownClosed and TextUpdate when ComboBox 2.0 has them.")]
245                 protected override void OnSubscribeControlEvents (Control control)
246                 {
247                         base.OnSubscribeControlEvents (control);
248
249                         this.ComboBox.DropDown += new EventHandler (HandleDropDown);
250                         this.ComboBox.DropDownStyleChanged += new EventHandler (HandleDropDownStyleChanged);
251                         this.ComboBox.SelectedIndexChanged += new EventHandler (HandleSelectedIndexChanged);
252                 }
253
254                 protected virtual void OnTextUpdate (EventArgs e)
255                 {
256                         EventHandler eh = (EventHandler)(Events [TextUpdateEvent]);
257                         if (eh != null)
258                                 eh (this, e);
259                 }
260
261                 protected override void OnUnsubscribeControlEvents (Control control)
262                 {
263                         base.OnUnsubscribeControlEvents (control);
264                 }
265                 #endregion
266
267                 #region Public Events
268                 static object DropDownEvent = new object ();
269                 static object DropDownClosedEvent = new object ();
270                 static object DropDownStyleChangedEvent = new object ();
271                 static object SelectedIndexChangedEvent = new object ();
272                 static object TextUpdateEvent = new object ();
273
274                 [Browsable (false)]
275                 [EditorBrowsable (EditorBrowsableState.Never)]
276                 public new event EventHandler DoubleClick {
277                         add { base.DoubleClick += value; }
278                         remove { base.DoubleClick -= value; }
279                 }
280
281                 public event EventHandler DropDown {
282                         add { Events.AddHandler (DropDownEvent, value); }
283                         remove { Events.RemoveHandler (DropDownEvent, value); }
284                 }
285
286                 public event EventHandler DropDownClosed {
287                         add { Events.AddHandler (DropDownClosedEvent, value); }
288                         remove { Events.RemoveHandler (DropDownClosedEvent, value); }
289                 }
290
291                 public event EventHandler DropDownStyleChanged {
292                         add { Events.AddHandler (DropDownStyleChangedEvent, value); }
293                         remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
294                 }
295
296                 public event EventHandler SelectedIndexChanged {
297                         add { Events.AddHandler (SelectedIndexChangedEvent, value); }
298                         remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
299                 }
300
301                 public event EventHandler TextUpdate {
302                         add { Events.AddHandler (TextUpdateEvent, value); }
303                         remove { Events.RemoveHandler (TextUpdateEvent, value); }
304                 }
305                 #endregion
306
307                 #region Private Methods
308                 private void HandleDropDown (object sender, EventArgs e)
309                 {
310                         OnDropDown (e);
311                 }
312
313                 private void HandleDropDownStyleChanged (object sender, EventArgs e)
314                 {
315                         OnDisplayStyleChanged (e);
316                 }
317
318                 private void HandleSelectedIndexChanged (object sender, EventArgs e)
319                 {
320                         OnSelectedIndexChanged (e);
321                 }
322                 #endregion
323
324                 private class ToolStripComboBoxControl : ComboBox
325                 {
326                         public ToolStripComboBoxControl () : base ()
327                         {
328                                 this.border_style = BorderStyle.None;
329                         }
330                 }
331         }
332 }
333 #endif