**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / DomainUpDown.cs
blob6a02baf822e70f34f5f6cddf114188236796404f
1 //
2 // System.Windows.Forms.DomainUpDown
3 //
4 // Author:
5 // stubbed out by Richard Baumann (biochem333@nyc.rr.com)
6 // Dennis Hayes (dennish@Raytek.com)
7 // implemented by Aleksey Ryabchuk (ryabchuk@yahoo.com)
8 // (C) Ximian, Inc., 2002
9 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Globalization;
35 namespace System.Windows.Forms {
37 // <summary>
38 // Represents a Windows up-down control that displays string values.
39 // </summary>
40 public class DomainUpDown : UpDownBase {
43 DomainUpDownItemCollection items;
44 int selectedIndex;
45 bool sorted;
46 bool wrap;
48 [MonoTODO]
49 public DomainUpDown() : base()
51 selectedIndex = -1;
52 sorted = false;
53 wrap = false;
54 TextChanged += new EventHandler ( this.BuddyTextChanged );
58 public override void DownButton()
60 int newIndex = SelectedIndex + 1;
61 if ( newIndex < Items.Count )
62 SelectedIndex = newIndex;
63 else if ( Wrap && Items.Count > 0)
64 SelectedIndex = 0;
68 public override string ToString()
70 return GetType( ).FullName.ToString( ) + ", Items.Count: " + Items.Count.ToString ( ) +
71 ", SelectedIndex: " + SelectedIndex;
74 public override void UpButton()
76 int newIndex = SelectedIndex - 1;
77 if ( newIndex > -1 && newIndex < Items.Count )
78 SelectedIndex = newIndex;
79 else if ( Wrap && Items.Count > 0 )
80 SelectedIndex = Items.Count - 1;
83 [MonoTODO]
84 protected override AccessibleObject CreateAccessibilityInstance()
86 //FIXME:
87 return base.CreateAccessibilityInstance();
90 protected void OnSelectedItemChanged(object source, EventArgs e)
92 if (SelectedItemChanged != null)
93 SelectedItemChanged(this, e);
96 [MonoTODO]
97 protected override void OnChanged(object source, EventArgs e) {
98 base.OnChanged(source, e);
101 [MonoTODO]
102 protected override void OnTextBoxKeyDown(object source, KeyEventArgs e)
104 if ( ReadOnly ) {
105 char symbol = System.Convert.ToChar( (int)e.KeyCode );
107 if ( Char.IsLetterOrDigit ( symbol ) ) {
108 string lower = Char.ToLower ( symbol ).ToString ( );
109 string upper = Char.ToUpper ( symbol ).ToString ( );
111 foreach ( object item in Items ) {
112 string sitem = item.ToString ( );
113 if ( sitem.StartsWith ( upper ) || sitem.StartsWith ( lower ) ) {
114 SelectedItem = item;
115 break;
118 e.Handled = true;
121 base.OnTextBoxKeyDown ( source, e );
124 protected override void UpdateEditText ( )
126 if ( SelectedIndex != -1 )
127 Text = Items [ SelectedIndex ].ToString ( );
128 else
129 Text = String.Empty;
132 public event EventHandler SelectedItemChanged;
134 public DomainUpDown.DomainUpDownItemCollection Items {
135 get {
136 if ( items == null )
137 items = new DomainUpDownItemCollection ( this );
138 return items;
142 [MonoTODO]
143 public int SelectedIndex {
144 get { return selectedIndex; }
145 set {
146 if ( value < -1 || value >= Items.Count )
147 throw new ArgumentException ( ); // FIXME: message
149 if ( selectedIndex != value ) {
150 selectedIndex = value;
151 UpdateEditText ( );
156 [MonoTODO]
157 public object SelectedItem {
158 get {
159 if ( SelectedIndex == -1 )
160 return null;
161 return Items[ SelectedIndex ];
163 set {
164 SelectedIndex = Items.IndexOf ( value );
168 [MonoTODO]
169 public bool Sorted {
170 get { return sorted; }
171 set {
172 if ( sorted != value ) {
173 object selectedItem = SelectedItem;
174 Items.Sort ( );
175 SelectedItem = selectedItem;
180 public bool Wrap {
181 get { return wrap; }
182 set { wrap = value; }
185 private void itemAdded ( object item )
189 private void itemInserted ( int index, object item )
193 private void itemRemoved ( object item )
197 private void itemRemoved ( int index )
201 private void itemChanged ( int index )
203 if ( index == SelectedIndex )
204 UpdateEditText ( );
207 private void BuddyTextChanged ( object sender, EventArgs e )
209 OnSelectedItemChanged ( this, EventArgs.Empty );
212 //System.Windows.Forms.DomainUpDown.DomainUpDownItemCollection
214 //Author:
215 //stubbed out by Richard Baumann (biochem333@nyc.rr.com)
217 //(C) Ximian, Inc., 2002
219 //<summary>
220 //Encapsulates a collection of objects for use by the DomainUpDown class.
221 //</summary>
222 public class DomainUpDownItemCollection : ArrayList {
224 DomainUpDown owner;
226 internal DomainUpDownItemCollection( DomainUpDown owner )
228 this.owner = owner;
231 public override int Add( object value )
233 int index = base.Add ( value );
234 owner.itemAdded ( value );
235 return index;
238 public override void Insert( int index, object value )
240 base.Insert ( index, value );
241 owner.itemInserted ( index, value );
244 public override void Remove( object obj )
246 base.Remove ( obj );
247 owner.itemRemoved ( obj );
250 public override void RemoveAt( int index )
252 base.RemoveAt ( index );
253 owner.itemRemoved ( index );
256 public override object this[ int index ]
258 get {
259 return base[index];
261 set {
262 base[index] = value;
263 owner.itemChanged ( index );