2005-09-19 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / RadioButtonList.cs
1 //
2 // System.Web.UI.WebControls.RadioButtonList.cs
3 //
4 // Authors:
5 //    Jordi Mas i Hernandez (jordi@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 //
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //
31
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Collections.Specialized;
35
36 namespace System.Web.UI.WebControls {
37
38         [ValidationProperty ("SelectedItem")]
39         public class RadioButtonList : ListControl, IRepeatInfoUser,
40                 INamingContainer, IPostBackDataHandler {
41                 bool need_raise;
42
43                 public RadioButtonList ()
44                 {
45
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 {
56                                 if (ControlStyleCreated == false)
57                                         return -1; // default value
58
59                                 return ((TableStyle) ControlStyle).CellPadding;
60                         }
61
62                         set {
63                                 ((TableStyle) ControlStyle).CellPadding = value;
64                         }
65                 }
66
67 #if ONLY_1_1
68                 [Bindable (true)]
69 #endif          
70                 [DefaultValue (-1)]
71                 [WebSysDescription ("")]
72                 [WebCategory ("Layout")]
73                 public virtual int CellSpacing {
74                         get {
75                                 if (ControlStyleCreated == false)
76                                         return -1; // default value
77
78                                 return ((TableStyle) ControlStyle).CellSpacing;
79                         }
80
81                         set {
82                                 ((TableStyle) ControlStyle).CellSpacing = value;
83                         }
84                 }
85
86 #if ONLY_1_1
87                 [Bindable (true)]
88 #endif          
89                 [DefaultValue (0)]
90                 [WebSysDescription ("")]
91                 [WebCategory ("Layout")]
92                 public virtual int RepeatColumns  {
93                         get {
94                                 return ViewState.GetInt ("RepeatColumns", 0);
95                         }
96
97                         set {
98                                 if (value < 0)
99                                         throw new ArgumentOutOfRangeException ("The number of columns is set to a negative value.");
100
101                                 ViewState ["RepeatColumns"] = value;
102                         }
103                 }
104
105 #if ONLY_1_1
106                 [Bindable (true)]
107 #endif          
108                 [DefaultValue (RepeatDirection.Vertical)]
109                 [WebSysDescription ("")]
110                 [WebCategory ("Layout")]
111                 public virtual RepeatDirection RepeatDirection {
112                         get {
113                                 return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical);
114                         }
115
116                         set {
117                                 if (value != RepeatDirection.Horizontal && value != RepeatDirection.Vertical)
118                                         throw new ArgumentOutOfRangeException ("he display direction of the list is not one of the RepeatDirection values.");
119
120                                 ViewState ["RepeatDirection"] = value;
121                         }
122                 }
123
124 #if ONLY_1_1
125                 [Bindable (true)]
126 #endif          
127                 [DefaultValue (RepeatLayout.Table)]
128                 [WebSysDescription ("")]
129                 [WebCategory ("Layout")]
130                 public virtual RepeatLayout RepeatLayout {
131                         get {
132                                 return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table);
133                         }
134
135                         set {
136                                 if (value != RepeatLayout.Flow && value != RepeatLayout.Table)
137                                         throw new ArgumentOutOfRangeException ("The radio buttons layout is not one of the RepeatLayout values.");
138
139                                 ViewState ["RepeatLayout"] = value;
140                         }
141                 }
142
143 #if ONLY_1_1
144                 [Bindable (true)]
145 #endif          
146                 [DefaultValue (TextAlign.Right)]
147                 [WebSysDescription ("")]
148                 [WebCategory ("Appearance")]
149                 public virtual TextAlign TextAlign {
150                         get {
151                                 return (TextAlign )ViewState.GetInt ("TextAlign", (int) TextAlign.Right);
152                         }
153
154                         set {
155                                 if (value != TextAlign.Left && value != TextAlign.Right)
156                                         throw new ArgumentOutOfRangeException ("The label text alignment associated with the radio buttons is not one of the TextAlign values.");
157
158                                 ViewState ["TextAlign"] = value;
159                         }
160                 }
161
162                 // Interface properties
163
164 #if NET_2_0
165                 [MonoTODO]
166                 protected virtual bool HasFooter
167                 {
168                         get {
169                                 throw new NotImplementedException ();
170                         }
171                 }
172
173                 [MonoTODO]
174                 protected virtual bool HasHeader 
175                 {
176                         get {
177                                 throw new NotImplementedException ();
178                         }
179                 }
180
181                 [MonoTODO]
182                 protected virtual bool HasSeparators
183                 {
184                         get {
185                                 throw new NotImplementedException ();
186                         }
187                 }
188                 
189                 [MonoTODO]
190                 protected virtual int RepeatedItemCount
191                 {
192                         get {
193                                 throw new NotImplementedException ();
194                         }
195                 }
196 #endif
197                 
198                 bool IRepeatInfoUser.HasFooter {
199                         get { return false; }
200                 }
201
202                 bool IRepeatInfoUser.HasHeader {
203                         get { return false; }
204                 }
205
206                 bool IRepeatInfoUser.HasSeparators {
207                         get { return false; }
208                 }
209
210                 int IRepeatInfoUser.RepeatedItemCount {
211                         get { return Items.Count; }
212                 }
213
214                 protected override Style CreateControlStyle ()
215                 {
216                         return new TableStyle (ViewState);
217                 }
218
219 #if NET_2_0
220                 [MonoTODO]
221                 protected override Control FindControl (string id, int pathOffset)
222                 {
223                         throw new NotImplementedException ();
224                 }
225 #endif
226
227 #if NET_2_0
228                 protected virtual
229 #endif
230                 Style GetItemStyle (ListItemType itemType, int repeatIndex)
231                 {
232                         return null;
233                 }
234
235 #if NET_2_0
236                 protected virtual
237 #endif
238                 void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
239                 {
240                         RadioButton radio = new RadioButton ();
241                         radio.Text = Items [repeatIndex].Text;
242                         radio.ID = ClientID + "_"  + repeatIndex;
243                         radio.TextAlign = TextAlign;
244                         radio.GroupName = UniqueID;
245                         radio.Page = Page;
246                         radio.Checked = Items [repeatIndex].Selected;
247                         radio.Attributes["Value"] = Items [repeatIndex].Value;
248                         radio.RenderControl (writer);
249                 }
250 #if NET_2_0
251                 protected virtual
252 #endif
253                 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
254                 {
255                         string val = postCollection [postDataKey];
256                         ListItemCollection items = Items;
257                         int end = items.Count;
258                         int selected = SelectedIndex;
259                         for (int i = 0; i < end; i++) {
260                                 ListItem item = items [i];
261                                 if (item == null || val != item.Value)
262                                         continue;
263
264                                 if (i != selected) {
265                                         SelectedIndex = i;
266                                         need_raise = true;
267                                 }
268                                 return true;
269                         }
270
271                         return false;
272                 }
273
274 #if NET_2_0
275                 protected virtual
276 #endif
277                 void RaisePostDataChangedEvent ()
278                 {
279                         if (need_raise)
280                                 OnSelectedIndexChanged (EventArgs.Empty);
281                 }
282
283                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
284                 {
285                         return LoadPostData (postDataKey, postCollection);
286                 }
287                 
288                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
289                 {
290                         RaisePostDataChangedEvent ();
291                 }
292
293                 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,  int repeatIndex)
294                 {
295                         return GetItemStyle (itemType, repeatIndex);
296                 }
297
298                 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
299                 {
300                         RenderItem (itemType, repeatIndex, repeatInfo, writer);
301                 }
302
303 #if NET_2_0
304                 protected internal
305 #else           
306                 protected
307 #endif          
308                 override void Render (HtmlTextWriter writer)
309                 {
310                         RepeatInfo repeat = new RepeatInfo ();
311                         repeat.RepeatColumns = RepeatColumns;
312                         repeat.RepeatDirection = RepeatDirection;
313                         repeat.RepeatLayout = RepeatLayout;
314                         repeat.RenderRepeater (writer, this, ControlStyle, this);
315                 }
316         }
317
318 }
319