Mark tests as not working under TARGET_JVM
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBoxList.cs
1 //
2 // System.Web.UI.WebControls.CheckBoxList.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.Globalization;
30 using System.Collections.Specialized;
31 using System.ComponentModel;
32 using System.Security.Permissions;
33
34 namespace System.Web.UI.WebControls {
35
36         // CAS
37         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         public class CheckBoxList : ListControl, IRepeatInfoUser,
40                                     INamingContainer, IPostBackDataHandler {
41
42                 private CheckBox check_box;
43
44                 public CheckBoxList ()
45                 {
46                         check_box = new CheckBox ();
47                         Controls.Add (check_box);
48                 }
49
50 #if ONLY_1_1
51                 [Bindable(true)]
52 #endif          
53                 [DefaultValue(-1)]
54                 [WebSysDescription ("")]
55                 [WebCategory ("Layout")]
56                 public virtual int CellPadding {
57                         get { return TableStyle.CellPadding; }
58                         set { TableStyle.CellPadding = value; }
59                 }
60
61 #if ONLY_1_1
62                 [Bindable(true)]
63 #endif          
64                 [DefaultValue(-1)]
65                 [WebSysDescription ("")]
66                 [WebCategory ("Layout")]
67                 public virtual int CellSpacing {
68                         get { return TableStyle.CellSpacing; }
69                         set { TableStyle.CellSpacing = value; }
70                 }
71
72 #if ONLY_1_1
73                 [Bindable(true)]
74 #endif          
75                 [DefaultValue(0)]
76                 [WebSysDescription ("")]
77                 [WebCategory ("Layout")]
78                 public virtual int RepeatColumns {
79                         get { return ViewState.GetInt ("RepeatColumns", 0); }
80                         set {
81                                 if (value < 0)
82                                         throw new ArgumentOutOfRangeException ("value");
83                                 ViewState ["RepeatColumns"] = value;
84                         }
85                 }
86
87 #if ONLY_1_1
88                 [Bindable(true)]
89 #endif          
90                 [DefaultValue(RepeatDirection.Vertical)]
91                 [WebSysDescription ("")]
92                 [WebCategory ("Layout")]
93                 public virtual RepeatDirection RepeatDirection {
94                         get {
95                                 return (RepeatDirection) ViewState.GetInt ("RepeatDirection",
96                                                 (int) RepeatDirection.Vertical);
97                         }
98                         set {
99                                 if (value < RepeatDirection.Horizontal ||
100                                                 value > RepeatDirection.Vertical)
101                                         throw new ArgumentOutOfRangeException ("value");
102                                 ViewState ["RepeatDirection"] = value;
103                         }
104                 }
105
106 #if ONLY_1_1
107                 [Bindable(true)]
108 #endif          
109                 [DefaultValue(RepeatLayout.Table)]
110                 [WebSysDescription ("")]
111                 [WebCategory ("Layout")]
112                 public virtual RepeatLayout RepeatLayout {
113                         get {
114                                 return (RepeatLayout) ViewState.GetInt ("RepeatLayout",
115                                                 (int) RepeatLayout.Table);
116                         }
117                         set {
118                                 if (value < RepeatLayout.Table ||
119                                                 value > RepeatLayout.Flow)
120                                         throw new ArgumentOutOfRangeException ("value");
121                                 ViewState ["RepeatLayout"] = value;
122                         }
123                 }
124
125 #if ONLY_1_1
126                 [Bindable(true)]
127 #endif          
128                 [DefaultValue(TextAlign.Right)]
129                 [WebSysDescription ("")]
130                 [WebCategory ("Appearance")]
131                 public virtual TextAlign TextAlign {
132                         get {
133                                 return (TextAlign) ViewState.GetInt ("TextAlign",
134                                                 (int) TextAlign.Right);
135                         }
136                         set {
137                                 if (value < TextAlign.Left || value > TextAlign.Right)
138                                         throw new ArgumentOutOfRangeException ("value");
139                                 ViewState ["TextAlign"] = value;
140                         }
141                 }
142
143                 private TableStyle TableStyle {
144                         get { return (TableStyle) ControlStyle; }
145                 }
146
147                 protected override Style CreateControlStyle ()
148                 {
149                         return new TableStyle (ViewState);
150                 }
151
152                 protected override Control FindControl (string id, int pathOffset)
153                 {
154                         // Always, or in just all my tests?
155                         return this;
156                 }
157
158 #if NET_2_0
159                 protected internal
160 #else           
161                 protected
162 #endif          
163                 override void OnPreRender (EventArgs e)
164                 {
165                         base.OnPreRender (e);
166
167                         // Register all of the checked controls so we can
168                         // find out when they are unchecked.
169                         for (int i = 0; i < Items.Count; i++) {
170                                 if (Items [i].Selected) {
171                                         check_box.ID = i.ToString (CultureInfo.InvariantCulture);
172                                         Page.RegisterRequiresPostBack (check_box);
173                                 }
174                         }
175                 }
176
177 #if NET_2_0
178                 protected internal
179 #else           
180                 protected
181 #endif          
182                 override void Render (HtmlTextWriter writer)
183                 {
184 #if NET_2_0
185                         if (Items.Count == 0)
186                                 return;
187 #endif
188                         RepeatInfo ri = new RepeatInfo ();
189                         ri.RepeatColumns = RepeatColumns;
190                         ri.RepeatDirection = RepeatDirection;
191                         ri.RepeatLayout = RepeatLayout;
192
193                         short ti = 0;
194                         if (TabIndex != 0) {
195                                 check_box.TabIndex = TabIndex;
196                                 ti = TabIndex;
197                                 TabIndex = 0;
198                         }
199 #if NET_2_0
200                         string ak = AccessKey;
201                         check_box.AccessKey = ak;
202                         this.AccessKey = null;
203 #endif
204
205                         ri.RenderRepeater (writer, this, TableStyle, this);
206
207                         if (ti != 0)
208                                 TabIndex = ti;
209 #if NET_2_0
210                         this.AccessKey = ak;
211 #endif
212                 }
213
214 #if NET_2_0
215                 protected virtual
216 #endif
217                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
218                 {
219 #if NET_2_0
220                         EnsureDataBound ();
221 #endif
222                         int checkbox = -1;
223
224                         try {
225                                 string id = postDataKey.Substring (ClientID.Length + 1);
226                                 if (Char.IsDigit (id [0]))
227                                         checkbox = Int32.Parse (id, CultureInfo.InvariantCulture);
228                         } catch {
229                                 return false;
230                         }
231
232                         if (checkbox == -1)
233                                 return false;
234
235                         string val = postCollection [postDataKey];
236                         bool ischecked = val == "on";
237                         ListItem item = Items [checkbox];
238
239                         if (item.Selected != ischecked) {
240                                 item.Selected = ischecked;
241                                 return true;
242                         }
243
244                         return false;
245                 }
246
247 #if NET_2_0
248                 protected virtual
249 #endif
250                 void RaisePostDataChangedEvent ()
251                 {
252 #if NET_2_0
253                         if (CausesValidation)
254                                 Page.Validate (ValidationGroup);
255 #endif
256                         OnSelectedIndexChanged (EventArgs.Empty);
257                 }
258
259                 bool IPostBackDataHandler.LoadPostData (string postDataKey,
260                                                         NameValueCollection postCollection)
261                 {
262                         return LoadPostData (postDataKey, postCollection);
263                 }
264
265                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
266                 {
267                         RaisePostDataChangedEvent ();
268                 }
269
270 #if NET_2_0
271                 protected virtual
272 #endif
273                 bool HasFooter 
274                 {
275                         get {
276                                 return false;
277                         }
278                 }
279
280                 bool IRepeatInfoUser.HasFooter {
281                         get { return HasFooter; }
282                 }
283
284 #if NET_2_0
285                 protected virtual
286 #endif
287                 bool HasHeader
288                 {
289                         get {
290                                 return false;
291                         }
292                 }
293
294                 bool IRepeatInfoUser.HasHeader {
295                         get { return HasHeader; }
296                 }
297
298
299 #if NET_2_0
300                 protected virtual
301 #endif
302                 bool HasSeparators
303                 {
304                         get {
305                                 return false;
306                         }
307                 }
308
309                 bool IRepeatInfoUser.HasSeparators {
310                         get { return HasSeparators; }
311                 }
312
313 #if NET_2_0
314                 protected virtual
315 #endif
316                 int RepeatedItemCount
317                 {
318                         get {
319                                 return Items.Count;
320                         }
321                 }
322
323                 int IRepeatInfoUser.RepeatedItemCount {
324                         get { return RepeatedItemCount; }
325                 }
326
327 #if NET_2_0
328                 protected virtual
329 #endif
330                 Style GetItemStyle (ListItemType itemType,
331                                     int repeatIndex)
332                 {
333                         return null;
334                 }
335
336                 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,
337                                                     int repeatIndex)
338                 {
339                         return GetItemStyle (itemType, repeatIndex);
340                 }
341
342 #if NET_2_0
343                 protected virtual
344 #endif
345                 void RenderItem (ListItemType itemType,
346                                  int repeatIndex,
347                                  RepeatInfo repeatInfo,
348                                  HtmlTextWriter writer)
349                 {
350                         ListItem item = Items [repeatIndex];
351
352                         check_box.ID = repeatIndex.ToString (CultureInfo.InvariantCulture);
353                         check_box.Text = item.Text;
354                         check_box.AutoPostBack = AutoPostBack;
355                         check_box.Checked = item.Selected;
356                         check_box.TextAlign = TextAlign;
357                         check_box.Enabled = Enabled;
358 #if NET_2_0
359                         check_box.ValidationGroup = ValidationGroup;
360                         check_box.CausesValidation = CausesValidation;
361                         if (check_box.HasAttributes)
362                                 check_box.Attributes.Clear ();
363                         if (item.HasAttributes)
364                                 check_box.Attributes.CopyFrom (item.Attributes);
365 #endif
366                         check_box.RenderControl (writer);
367                 }
368
369                 void IRepeatInfoUser.RenderItem (ListItemType itemType,
370                                                  int repeatIndex, RepeatInfo repeatInfo,
371                                                  HtmlTextWriter writer)
372                 {
373                         RenderItem (itemType, repeatIndex, repeatInfo, writer);
374                 }
375 #if NET_2_0
376         protected internal override void VerifyMultiSelect()
377         {
378             //by default the ListControl will throw an exception in this method,
379             //therefor we should override the method if the class is supporting
380             //MultiSelect option.
381         }
382 #endif
383     }
384 }