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