2010-07-23 Marek Habersack <mhabersack@novell.com>
[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 using System.Web.Util;
36
37 namespace System.Web.UI.WebControls {
38
39         // CAS
40         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         // attributes
43         [ValidationProperty("SelectedItem")]
44 #if NET_2_0
45         [SupportsEventValidation]
46 #endif
47         public class ListBox : ListControl, IPostBackDataHandler {
48
49                 public ListBox ()
50                 {
51                 }
52
53                 [Browsable(false)]
54 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
55                 public virtual new
56 #else           
57                 public override
58 #endif
59                 Color BorderColor {
60                         get { return base.BorderColor; }
61                         set { base.BorderColor = value; }
62                 }
63
64                 [Browsable(false)]
65 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
66                 public virtual new
67 #else           
68                 public override
69 #endif
70                 BorderStyle BorderStyle {
71                         get { return base.BorderStyle; }
72                         set { base.BorderStyle = value; }
73                 }
74
75                 [Browsable(false)]
76 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
77                 public virtual new
78 #else           
79                 public override
80 #endif
81                 Unit BorderWidth {
82                         get { return base.BorderWidth; }
83                         set { base.BorderWidth = value; }
84                 }
85
86 #if ONLY_1_1
87                 [Bindable(true)]
88 #endif          
89                 [DefaultValue(4)]
90                 [WebSysDescription ("")]
91                 [WebCategory ("Appearance")]
92                 public virtual int Rows {
93                         get {
94                                 return ViewState.GetInt ("Rows", 4);
95                         }
96                         set {
97                                 if (value < 1 || value > 2000)
98                                         throw new ArgumentOutOfRangeException ("value");
99                                 ViewState ["Rows"] = value;
100                         }
101                 }
102
103                 [DefaultValue(ListSelectionMode.Single)]
104                 [WebSysDescription ("")]
105                 [WebCategory ("Behavior")]
106                 public virtual ListSelectionMode SelectionMode {
107                         get {
108                                 return (ListSelectionMode) ViewState.GetInt ("SelectionMode",
109                                                 (int) ListSelectionMode.Single);
110                         }
111                         set {
112                                 if (!Enum.IsDefined (typeof (ListSelectionMode), value))
113                                         throw new ArgumentOutOfRangeException ("value");
114                                 ViewState ["SelectionMode"] = value;
115                         }
116                 }
117
118 #if ONLY_1_1
119                 [Bindable(false)]
120                 [Browsable(false)]
121                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
122                 [EditorBrowsable(EditorBrowsableState.Never)]
123                 public override string ToolTip {
124                         get { return String.Empty; }
125                         set { /* Tooltip is always String.Empty */ }
126                 }
127 #endif          
128
129 #if NET_2_0
130                 public virtual int[] GetSelectedIndices ()
131                 {
132                         return (int []) GetSelectedIndicesInternal ().ToArray (typeof (int));
133                 }
134 #endif          
135                 
136                 protected override void AddAttributesToRender (HtmlTextWriter writer)
137                 {
138                         if (Page != null)
139                                 Page.VerifyRenderingInServerForm (this);
140
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 #if NET_2_0
150                                 string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
151                                 onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
152                                 writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
153 #else
154                                 writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
155                                                      BuildScriptAttribute ("onchange",
156                                                                            Page.ClientScript.GetPostBackClientHyperlink (this, "")));
157 #endif
158                         }
159                         
160                         if (SelectionMode == ListSelectionMode.Multiple)
161                                 writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
162                                                 "multiple", false);
163                         writer.AddAttribute (HtmlTextWriterAttribute.Size,
164                                         Rows.ToString (Helpers.InvariantCulture));
165                         
166                         base.AddAttributesToRender (writer);
167                 }
168
169 #if NET_2_0
170                 PostBackOptions GetPostBackOptions () {
171                         PostBackOptions options = new PostBackOptions (this);
172                         options.ActionUrl = null;
173                         options.ValidationGroup = null;
174                         options.Argument = String.Empty;
175                         options.RequiresJavaScriptProtocol = false;
176                         options.ClientSubmit = true;
177                         options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
178                         if (options.PerformValidation)
179                                 options.ValidationGroup = ValidationGroup;
180
181                         return options;
182                 }
183 #endif          
184
185 #if ONLY_1_1
186                 protected override void RenderContents (HtmlTextWriter writer)
187                 {
188                         foreach (ListItem item in Items) {
189                                 if (item.Selected) {
190                                         writer.AddAttribute (HtmlTextWriterAttribute.Selected, "selected", false);
191                                 }
192                                 writer.AddAttribute (HtmlTextWriterAttribute.Value, item.Value, true);
193                                 writer.RenderBeginTag (HtmlTextWriterTag.Option);
194                                 
195                                 string encoded = HttpUtility.HtmlEncode (item.Text);
196                                 writer.Write (encoded);
197                                 writer.RenderEndTag ();
198                                 writer.WriteLine ();
199                         }
200                 }
201 #endif
202
203 #if NET_2_0
204                 protected internal
205 #else           
206                 protected
207 #endif          
208                 override void OnPreRender (EventArgs e)
209                 {
210                         base.OnPreRender (e);
211                         Page page = Page;
212                         if (page != null && IsEnabled)
213                                 page.RegisterRequiresPostBack (this);
214                 }
215
216 #if NET_2_0
217                 protected virtual
218 #endif
219                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
220                 {
221 #if NET_2_0
222                         EnsureDataBound ();
223 #endif
224                         string [] values = postCollection.GetValues (postDataKey);
225                         if (values == null || values.Length == 0) {
226                                 int prev_index = SelectedIndex;
227                                 SelectedIndex = -1;
228                                 return (prev_index != -1);
229                         }
230 #if NET_2_0
231                         ValidateEvent (UniqueID, values [0]);
232 #endif
233
234                         if (SelectionMode == ListSelectionMode.Single)
235                                 return SelectSingle (values);
236                         return SelectMultiple (values);
237                 }
238
239                 bool SelectSingle (string [] values)
240                 {
241                         string val = values [0];
242                         int idx = Items.IndexOf (val);
243                         int prev_index = SelectedIndex;
244                         if (idx != prev_index) {
245                                 // This will set both the index value and the item.Selected property
246                                 SelectedIndex = idx;
247                                 return true;
248                         }
249                         return false;
250                 }
251
252                 bool SelectMultiple (string [] values)
253                 {
254                         ArrayList prev_selected = GetSelectedIndicesInternal ();
255                         ClearSelection ();
256                         foreach (string val in values) {
257                                 ListItem item = Items.FindByValue (val);
258                                 if (item != null)
259                                         item.Selected = true;
260                         }
261
262                         ArrayList new_selection = GetSelectedIndicesInternal ();
263                         int i = prev_selected.Count;
264                         if (new_selection.Count != i)
265                                 return true;
266
267                         while (--i >= 0) {
268                                 if ((int) prev_selected [i] != (int) new_selection [i])
269                                         return true;
270                         }
271
272                         return false;
273                 }
274
275 #if NET_2_0
276                 protected virtual
277 #endif
278                 void RaisePostDataChangedEvent ()
279                 {
280 #if NET_2_0
281                         if (CausesValidation)
282                                 Page.Validate (ValidationGroup);
283 #endif
284                         OnSelectedIndexChanged (EventArgs.Empty);
285                 }
286                         
287                 bool IPostBackDataHandler.LoadPostData (string postDataKey,
288                                                         NameValueCollection postCollection)
289                 {
290                         return LoadPostData (postDataKey, postCollection);
291                 }
292
293                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
294                 {
295                         RaisePostDataChangedEvent ();
296                 }
297 #if NET_2_0
298                 internal override bool MultiSelectOk ()
299                 {
300                         return this.SelectionMode == ListSelectionMode.Multiple;
301                 }
302 #endif
303         }
304 }
305
306
307