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