3bbf69dcfc9c71200be43385f63e70f201eca15a
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DomainUpDown.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Miguel de Icaza (miguel@novell.com).
24 //
25 //
26
27 using System;
28 using System.ComponentModel;
29 using System.Collections;
30
31 namespace System.Windows.Forms {
32
33         [DefaultEvent ("SelectedItemChanged")]
34         [DefaultProperty ("Items")]
35         public class DomainUpDown : UpDownBase
36 #if NET_2_0
37                 , ISupportInitialize
38 #endif
39         {
40                 public class DomainUpDownItemCollection : ArrayList {
41                         DomainUpDown owner;
42                         
43                         internal DomainUpDownItemCollection (DomainUpDown owner)
44                         {
45                                 this.owner = owner;
46                         }
47
48                         public override int Add (object item)
49                         {
50                                 int ret = base.Add (item);
51                                 if (owner.sorted){
52                                         Sort ();
53
54                                         // Will trigger an update.
55                                         owner.SelectedIndex = owner.SelectedIndex;
56                                 }
57
58                                 return ret;
59                         }
60
61                         public override void Insert (int index, object item)
62                         {
63                                 base.Insert (index, item);
64                                 if (owner.sorted){
65                                         Sort ();
66                                         owner.SelectedIndex = owner.SelectedIndex;
67                                 } else {
68                                         if (index == owner.SelectedIndex)
69                                                 owner.UpdateEditText ();
70                                 }
71                         }
72
73                         public override void Remove (object item)
74                         {
75                                 base.Remove (item);
76                                 if (Count < owner.SelectedIndex)
77                                         owner.SelectedIndex -= 1;
78                                 if (owner.sorted){
79                                         Sort ();
80                                         owner.UpdateEditText ();
81                                 }
82                         }
83
84                         public override void RemoveAt (int item)
85                         {
86                                 base.RemoveAt (item);
87
88                                 if (Count < owner.SelectedIndex)
89                                         owner.SelectedIndex -= 1;
90                                 if (owner.SelectedIndex == item)
91                                         owner.UpdateEditText ();
92                                 if (owner.sorted){
93                                         Sort ();
94                                         owner.UpdateEditText ();
95                                 }
96                         }
97                 }
98
99                 int selected_index = -1;
100                 bool sorted = false;
101                 bool wrap = false;
102                 DomainUpDownItemCollection items;
103
104 #if NET_2_0
105 #region ISupportInitialize methods
106                 
107                 public void BeginInit ()
108                 {
109                 }
110
111                 public void EndInit ()
112                 {
113                 }
114 #endregion
115 #endif
116                 
117                 public event EventHandler SelectedItemChanged;
118                 
119                 public DomainUpDown () : base () {
120                         items = new DomainUpDownItemCollection (this);
121                 }
122                 
123                 public override void DownButton ()
124                 {
125                         if (wrap)
126                                 selected_index %= items.Count;
127                         else if (selected_index < items.Count)
128                                 selected_index++;
129
130                         UpdateEditText ();
131                 }
132
133                 public override void UpButton ()
134                 {
135                         if (wrap){
136                                 selected_index--;
137                                 if (selected_index == -1)
138                                         selected_index = items.Count-1;
139                         } else {
140                                 if (selected_index > -1)
141                                         selected_index--;
142                         }
143                         UpdateEditText ();
144                 }
145
146                 public override void UpdateEditText ()
147                 {
148                         ChangingText = true;
149                         if (selected_index == -1)
150                                 Text = "";
151                         else
152                                 Text = items [selected_index].ToString ();
153                         UserEdit = false;
154                 }
155
156                 protected override void OnChanged (object source, EventArgs e)
157                 {
158                         OnSelectedItemChanged (source, e);
159                 }
160                 
161                 protected void OnSelectedItemChanged (object source, EventArgs e)
162                 {
163                         if (SelectedItemChanged != null)
164                                 SelectedItemChanged (source, e);
165                 }
166                 
167                 protected override AccessibleObject CreateAccessibilityInstance ()
168                 {
169                         AccessibleObject ao;
170                         
171                         ao = base.CreateAccessibilityInstance ();
172                         ao.description = "DomainUpDown";
173
174                         return ao;
175                 }
176
177                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
178                 [Localizable(true)]
179                 public DomainUpDownItemCollection Items {
180                         get {
181                                 return items;
182                         }
183                 }
184
185                 [DefaultValue(-1)]
186                 [Browsable(false)]
187                 public int SelectedIndex {
188                         get {
189                                 return selected_index;
190                         }
191
192                         set {
193                                 if (value < -1 || value > items.Count)
194                                         throw new ArgumentException (String.Format ("Boundaries are -1 to {0}", items.Count-1));
195
196                                 selected_index = value;
197                                 UpdateEditText ();
198                         }
199                 }
200
201                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Item)]
202                 [Browsable(false)]
203                 public object SelectedItem {
204                         get {
205                                 if (selected_index == -1)
206                                         return null;
207
208                                 return items [selected_index];
209                         }
210
211                         set {
212                                 for (int i = 0; i < items.Count; i++)
213                                         if (items [i] == value){
214                                                 SelectedIndex = i;
215                                                 break;
216                                         }
217                         }
218                 }
219
220                 [DefaultValue(false)]
221                 public bool Sorted {
222                         get {
223                                 return sorted;
224                         }
225
226                         set {
227                                 //
228                                 // It never returns to unsorted state
229                                 //
230
231                                 sorted = value;
232                                 
233                                 if (sorted)
234                                         items.Sort ();
235                         }
236                 }
237
238                 [DefaultValue (false)]
239                 public bool Wrap {
240                         get {
241                                 return wrap;
242                         }
243
244                         set {
245                                 wrap = value;
246                         }
247                 }
248
249                 public override string ToString ()
250                 {
251                         return String.Format ("{0} Count={1} Selected={2}", base.ToString (), items.Count, selected_index);
252                 }
253         }
254 }