Add this for backwards compatibility
[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                 public virtual int Rows {
92                         get {
93                                 return ViewState.GetInt ("Rows", 4);
94                         }
95                         set {
96                                 if (value < 1 || value > 2000)
97                                         throw new ArgumentOutOfRangeException ("value");
98                                 ViewState ["Rows"] = value;
99                         }
100                 }
101
102                 [DefaultValue(ListSelectionMode.Single)]
103                 [WebSysDescription ("")]
104                 [WebCategory ("Behavior")]
105                 public virtual ListSelectionMode SelectionMode {
106                         get {
107                                 return (ListSelectionMode) ViewState.GetInt ("SelectionMode",
108                                                 (int) ListSelectionMode.Single);
109                         }
110                         set {
111                                 if (!Enum.IsDefined (typeof (ListSelectionMode), value))
112                                         throw new ArgumentOutOfRangeException ("value");
113                                 ViewState ["SelectionMode"] = value;
114                         }
115                 }
116
117 #if ONLY_1_1
118                 [Bindable(false)]
119                 [Browsable(false)]
120                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
121                 [EditorBrowsable(EditorBrowsableState.Never)]
122                 public override string ToolTip {
123                         get { return String.Empty; }
124                         set { /* Tooltip is always String.Empty */ }
125                 }
126 #endif          
127
128 #if NET_2_0
129                 public virtual int[] GetSelectedIndices ()
130                 {
131                         return (int []) GetSelectedIndicesInternal ().ToArray (typeof (int));
132                 }
133 #endif          
134                 
135                 protected override void AddAttributesToRender (HtmlTextWriter writer)
136                 {
137                         if (Page != null)
138                                 Page.VerifyRenderingInServerForm (this);
139
140                         base.AddAttributesToRender (writer);
141 #if NET_2_0
142                         if (ID != null)
143                                 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
144 #else
145                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
146 #endif
147
148                         if (AutoPostBack)
149                                 writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
150                                                 Page.ClientScript.GetPostBackClientHyperlink (this, ""));
151
152                         if (SelectionMode == ListSelectionMode.Multiple)
153                                 writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
154                                                 "multiple");
155                         writer.AddAttribute (HtmlTextWriterAttribute.Size,
156                                         Rows.ToString (CultureInfo.InvariantCulture));
157                 }
158
159 #if NET_2_0
160                 protected internal
161 #else           
162                 protected
163 #endif          
164                 override void RenderContents (HtmlTextWriter writer)
165                 {
166                         base.RenderContents (writer);
167
168                         foreach (ListItem item in Items) {
169                                 writer.WriteBeginTag ("option");
170                                 if (item.Selected) {
171                                         writer.WriteAttribute ("selected", "selected", false);
172                                 }
173                                 writer.WriteAttribute ("value", item.Value, true);
174
175                                 writer.Write (">");
176                                 string encoded = HttpUtility.HtmlEncode (item.Text);
177                                 writer.Write (encoded);
178                                 writer.WriteEndTag ("option");
179                                 writer.WriteLine ();
180                         }
181                 }
182
183 #if NET_2_0
184                 protected internal
185 #else           
186                 protected
187 #endif          
188                 override void OnPreRender (EventArgs e)
189                 {
190                         base.OnPreRender (e);
191                         if (Page != null)
192                                 Page.RegisterRequiresPostBack (this);
193                 }
194
195 #if NET_2_0
196                 protected virtual
197 #endif
198                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
199                 {
200                         string [] values = postCollection.GetValues (postDataKey);
201                         if (values == null || values.Length == 0) {
202                                 int prev_index = SelectedIndex;
203                                 SelectedIndex = -1;
204                                 return (prev_index != -1);
205                         }
206
207                         if (SelectionMode == ListSelectionMode.Single)
208                                 return SelectSingle (values);
209                         return SelectMultiple (values);
210                 }
211
212                 bool SelectSingle (string [] values)
213                 {
214                         string val = values [0];
215                         int idx = Items.IndexOf (val);
216                         int prev_index = SelectedIndex;
217                         if (idx != prev_index) {
218                                 // This will set both the index value and the item.Selected property
219                                 SelectedIndex = idx;
220                                 return true;
221                         }
222                         return false;
223                 }
224
225                 bool SelectMultiple (string [] values)
226                 {
227                         ArrayList prev_selected = GetSelectedIndicesInternal ();
228                         ClearSelection ();
229                         foreach (string val in values) {
230                                 ListItem item = Items.FindByValue (val);
231                                 if (item != null)
232                                         item.Selected = true;
233                         }
234
235                         ArrayList new_selection = GetSelectedIndicesInternal ();
236                         int i = prev_selected.Count;
237                         if (new_selection.Count != i)
238                                 return true;
239
240                         while (--i >= 0) {
241                                 if ((int) prev_selected [i] != (int) new_selection [i])
242                                         return true;
243                         }
244
245                         return false;
246                 }
247
248 #if NET_2_0
249                 protected virtual
250 #endif
251                 void RaisePostDataChangedEvent ()
252                 {
253                         OnSelectedIndexChanged (EventArgs.Empty);
254                 }
255                         
256                 bool IPostBackDataHandler.LoadPostData (string postDataKey,
257                                                         NameValueCollection postCollection)
258                 {
259                         return LoadPostData (postDataKey, postCollection);
260                 }
261
262                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
263                 {
264                         RaisePostDataChangedEvent ();
265                 }
266         }
267 }
268