fix the 2.0 build after my last commit
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ListBox.cs
1 //
2 // System.Web.UI.WebControls.Literal.cs
3 //
4 // Authors:
5 //      Jackson Harper (jackson@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Collections;
30 using System.ComponentModel;
31 using System.Drawing;
32 using System.Globalization;
33 using System.Collections.Specialized;
34 using System.Security.Permissions;
35
36 namespace System.Web.UI.WebControls {
37
38         // CAS
39         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         // attributes
42         [ValidationProperty("SelectedItem")]
43 #if NET_2_0
44         [SupportsEventValidation]
45 #endif
46         public class ListBox : ListControl, IPostBackDataHandler {
47
48                 public ListBox ()
49                 {
50                 }
51
52                 [Browsable(false)]
53 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
54                 public virtual new
55 #else           
56                 public override
57 #endif
58                 Color BorderColor {
59                         get { return base.BorderColor; }
60                         set { base.BorderColor = value; }
61                 }
62
63                 [Browsable(false)]
64 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
65                 public virtual new
66 #else           
67                 public override
68 #endif
69                 BorderStyle BorderStyle {
70                         get { return base.BorderStyle; }
71                         set { base.BorderStyle = value; }
72                 }
73
74                 [Browsable(false)]
75 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
76                 public virtual new
77 #else           
78                 public override
79 #endif
80                 Unit BorderWidth {
81                         get { return base.BorderWidth; }
82                         set { base.BorderWidth = value; }
83                 }
84
85 #if ONLY_1_1
86                 [Bindable(true)]
87 #endif          
88                 [DefaultValue(4)]
89                 [WebSysDescription ("")]
90                 [WebCategory ("Appearance")]
91 #if NET_2_0
92                 public virtual
93 #else           
94                 public
95 #endif
96                 int Rows {
97                         get {
98                                 return ViewState.GetInt ("Rows", 4);
99                         }
100                         set {
101                                 if (value < 1 || value > 2000)
102                                         throw new ArgumentOutOfRangeException ("value");
103                                 ViewState ["Rows"] = value;
104                         }
105                 }
106
107                 [DefaultValue(ListSelectionMode.Single)]
108                 [WebSysDescription ("")]
109                 [WebCategory ("Behavior")]
110 #if NET_2_0
111                 public virtual
112 #else           
113                 public
114 #endif          
115                 ListSelectionMode SelectionMode {
116                         get {
117                                 return (ListSelectionMode) ViewState.GetInt ("SelectionMode",
118                                                 (int) ListSelectionMode.Single);
119                         }
120                         set {
121                                 if (!Enum.IsDefined (typeof (ListSelectionMode), value))
122                                         throw new ArgumentOutOfRangeException ("value");
123                                 ViewState ["SelectionMode"] = value;
124                         }
125                 }
126
127 #if ONLY_1_1
128                 [Bindable(false)]
129                 [Browsable(false)]
130                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
131                 [EditorBrowsable(EditorBrowsableState.Never)]
132                 public override string ToolTip {
133                         get { return String.Empty; }
134                         set { /* Tooltip is always String.Empty */ }
135                 }
136 #endif          
137
138 #if NET_2_0
139                 public virtual int[] GetSelectedIndices ()
140                 {
141                         return (int []) GetSelectedIndicesInternal ().ToArray (typeof (int));
142                 }
143 #endif          
144                 
145                 protected override void AddAttributesToRender (HtmlTextWriter writer)
146                 {
147                         if (Page != null)
148                                 Page.VerifyRenderingInServerForm (this);
149
150                         base.AddAttributesToRender (writer);
151 #if NET_2_0
152                         if (ID != null)
153                                 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
154 #else
155                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
156 #endif
157
158                         if (SelectionMode == ListSelectionMode.Multiple)
159                                 writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
160                                                 "multiple");
161                         writer.AddAttribute (HtmlTextWriterAttribute.Size,
162                                         Rows.ToString (CultureInfo.InvariantCulture));
163                 }
164
165 #if NET_2_0
166                 protected internal
167 #else           
168                 protected
169 #endif          
170                 override void RenderContents (HtmlTextWriter writer)
171                 {
172                         base.RenderContents (writer);
173
174                         foreach (ListItem item in Items) {
175                                 writer.WriteBeginTag ("option");
176                                 if (item.Selected) {
177                                         writer.WriteAttribute ("selected", "selected", false);
178                                 }
179                                 writer.WriteAttribute ("value", item.Value, true);
180
181                                 writer.Write (">");
182                                 string encoded = HttpUtility.HtmlEncode (item.Text);
183                                 writer.Write (encoded);
184                                 writer.WriteEndTag ("option");
185                                 writer.WriteLine ();
186                         }
187                 }
188
189 #if NET_2_0
190                 protected internal
191 #else           
192                 protected
193 #endif          
194                 override void OnPreRender (EventArgs e)
195                 {
196                         base.OnPreRender (e);
197                         if (Page != null)
198                                 Page.RegisterRequiresPostBack (this);
199                 }
200
201 #if NET_2_0
202                 protected virtual
203 #endif
204                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
205                 {
206                         string [] values = postCollection.GetValues (postDataKey);
207                         if (values == null || values.Length == 0) {
208                                 int prev_index = SelectedIndex;
209                                 SelectedIndex = -1;
210                                 return (prev_index != -1);
211                         }
212
213                         if (SelectionMode == ListSelectionMode.Single)
214                                 return SelectSingle (values);
215                         return SelectMultiple (values);
216                 }
217
218                 bool SelectSingle (string [] values)
219                 {
220                         string val = values [0];
221                         int idx = Items.IndexOf (val);
222                         int prev_index = SelectedIndex;
223                         if (idx != prev_index) {
224                                 // This will set both the index value and the item.Selected property
225                                 SelectedIndex = idx;
226                                 return true;
227                         }
228                         return false;
229                 }
230
231                 bool SelectMultiple (string [] values)
232                 {
233                         ArrayList prev_selected = GetSelectedIndicesInternal ();
234                         ClearSelection ();
235                         foreach (string val in values) {
236                                 ListItem item = Items.FindByValue (val);
237                                 if (item != null)
238                                         item.Selected = true;
239                         }
240
241                         ArrayList new_selection = GetSelectedIndicesInternal ();
242                         int i = prev_selected.Count;
243                         if (new_selection.Count != i)
244                                 return true;
245
246                         while (--i >= 0) {
247                                 if ((int) prev_selected [i] != (int) new_selection [i])
248                                         return true;
249                         }
250
251                         return false;
252                 }
253
254 #if NET_2_0
255                 protected virtual
256 #endif
257                 void RaisePostDataChangedEvent ()
258                 {
259                         OnSelectedIndexChanged (EventArgs.Empty);
260                 }
261                         
262                 bool IPostBackDataHandler.LoadPostData (string postDataKey,
263                                                         NameValueCollection postCollection)
264                 {
265                         return LoadPostData (postDataKey, postCollection);
266                 }
267
268                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
269                 {
270                         RaisePostDataChangedEvent ();
271                 }
272         }
273 }
274