2005-08-29 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBox.cs
1 //
2 // System.Web.UI.WebControls.CheckBox.cs
3 //
4 // Author:
5 //      Dick Porter  <dick@ximian.com>
6 //
7 // Copyright (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.Web.UI;
30 using System.Collections.Specialized;
31 using System.ComponentModel;
32
33 namespace System.Web.UI.WebControls {
34         [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
35         [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
36         [DefaultEvent ("CheckedChanged")]
37         [DefaultProperty ("Text")]
38 #if NET_2_0
39         [ControlValueProperty ("Checked", null)]
40 #endif          
41         public class CheckBox : WebControl, IPostBackDataHandler
42 #if NET_2_0
43         , ICheckBoxControl
44 #endif
45         {
46                 string render_type;
47                 
48                 public CheckBox () : base (HtmlTextWriterTag.Input)
49                 {
50                         render_type = "checkbox";
51                 }
52
53                 internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
54                 {
55                         this.render_type = render_type;
56                 }
57
58                 [DefaultValue (false)]
59 #if NET_2_0
60                 [Themeable (false)]
61 #endif          
62                 public virtual bool AutoPostBack 
63                 {
64                         get {
65                                 return (ViewState.GetBool ("AutoPostBack",
66                                                            false));
67                         }
68                         set {
69                                 ViewState["AutoPostBack"] = value;
70                         }
71                 }
72
73 #if NET_2_0
74                 [DefaultValue (false)]
75                 [Themeable (false)]
76                 [MonoTODO]
77                 public virtual bool CausesValidation 
78                 {
79                         get {
80                                 throw new NotImplementedException ();
81                         }
82                         set {
83                                 throw new NotImplementedException ();
84                         }
85                 }
86 #endif          
87                 
88
89                 [DefaultValue (false)]
90 #if NET_2_0
91                 [Bindable (true, BindingDirection.TwoWay)]
92                 [Themeable (false)]
93 #else           
94                 [Bindable (true)]
95 #endif          
96                 public virtual bool Checked 
97                 {
98                         get {
99                                 return (ViewState.GetBool ("Checked", false));
100                         }
101                         set {
102                                 ViewState["Checked"] = value;
103                         }
104                 }
105
106 #if NET_2_0
107                 [Browsable (false)]
108                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
109                 [MonoTODO]
110                 public AttributeCollection InputAttributes 
111                 {
112                         get {
113                                 throw new NotImplementedException ();
114                         }
115                         set {
116                                 throw new NotImplementedException ();
117                         }
118                 }
119
120                 [Browsable (false)]
121                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
122                 [MonoTODO]
123                 public AttributeCollection LabelAttributes
124                 {
125                         get {
126                                 throw new NotImplementedException ();
127                         }
128                         set {
129                                 throw new NotImplementedException ();
130                         }
131                 }
132 #endif          
133
134                 [DefaultValue ("")]
135                 [Bindable (true)]
136 #if NET_2_0
137                 [Localizable (true)]
138 #endif          
139                 public virtual string Text 
140                 {
141                         get {
142                                 return (ViewState.GetString ("Text",
143                                                              String.Empty));
144                         }
145                         set {
146                                 ViewState["Text"] = value;
147                         }
148                 }
149
150                 [DefaultValue (TextAlign.Right)]
151 #if ONLY_1_1
152                 [Bindable (true)]
153 #endif          
154                 public virtual TextAlign TextAlign
155                 {
156                         get {
157                                 object o = ViewState["TextAlign"];
158
159                                 if (o == null) {
160                                         return (TextAlign.Right);
161                                 } else {
162                                         return ((TextAlign)o);
163                                 }
164                         }
165                         set {
166                                 if (value != TextAlign.Left &&
167                                     value != TextAlign.Right) {
168                                         throw new ArgumentOutOfRangeException ("value");
169                                 }
170                                 
171                                 ViewState["TextAlign"] = value;
172                         }
173                 }
174
175 #if NET_2_0
176                 [Themeable (false)]
177                 [DefaultValue ("")]
178                 [MonoTODO]
179                 public string ValidationGroup
180                 {
181                         get {
182                                 throw new NotImplementedException ();
183                         }
184                         set {
185                                 throw new NotImplementedException ();
186                         }
187                 }
188 #endif          
189
190                 private static readonly object EventCheckedChanged = new object ();
191                 public event EventHandler CheckedChanged 
192                 {
193                         add {
194                                 Events.AddHandler (EventCheckedChanged, value);
195                         }
196                         remove {
197                                 Events.RemoveHandler (EventCheckedChanged, value);
198                         }
199                 }
200
201                 protected virtual void OnCheckedChanged (EventArgs e)
202                 {
203                         EventHandler handler = (EventHandler)Events[EventCheckedChanged];
204                         
205                         if (handler != null) {
206                                 handler (this, e);
207                         }
208                 }
209
210                 internal virtual string NameAttribute 
211                 {
212                         get {
213                                 return (this.UniqueID);
214                         }
215                 }
216                 
217                 protected override void AddAttributesToRender (HtmlTextWriter w)
218                 {
219                         if (Page != null)
220                                 Page.VerifyRenderingInServerForm (this);
221
222                         /* This is a nasty kludge to avoid rendering
223                          * "style" and the ControlStyle in checkboxes
224                          * (we use a surrounding <span> instead), but
225                          * still pass the unit test that shows the
226                          * style being rendered in multiple calls to
227                          * Render () (which means we can't just delete
228                          * Attributes["style"] or ControlStyle.Reset()
229                          * in Render ())
230                          */
231                         string css_style = Attributes ["style"];
232                         if (css_style != null) {
233                                 Attributes.Remove ("style");
234                         }
235
236                         Style style = new Style ();
237                         if (ControlStyleCreated) {
238                                 style.CopyFrom (ControlStyle);
239                                 ControlStyle.Reset ();
240                         }
241                         
242                         base.AddAttributesToRender (w);
243
244                         if (css_style != null) {
245                                 Attributes ["style"] = css_style;
246                         }
247                         if (!style.IsEmpty) {
248                                 ApplyStyle (style);
249                         }
250                         
251                         InternalAddAttributesToRender (w);
252                         
253                         w.AddAttribute (HtmlTextWriterAttribute.Type,
254                                         render_type);
255                         w.AddAttribute (HtmlTextWriterAttribute.Name,
256                                         NameAttribute);
257                         
258                         if (AutoPostBack) {
259                                 w.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
260                         }
261
262                         if (Checked) {
263                                 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
264                         }
265                 }
266
267 #if NET_2_0
268                 [MonoTODO]
269                 protected override void LoadViewState (object savedState)
270                 {
271                         throw new NotImplementedException ();
272                 }
273
274                 [MonoTODO]
275                 protected override object SaveViewState ()
276                 {
277                         throw new NotImplementedException ();
278                 }
279
280                 [MonoTODO]
281                 protected override void TrackViewState ()
282                 {
283                         throw new NotImplementedException ();
284                 }
285 #endif          
286
287 #if NET_2_0
288                 protected internal
289 #else           
290                 protected
291 #endif          
292                 override void OnPreRender (EventArgs e)
293                 {
294                         base.OnPreRender (e);
295
296                         if (Page != null) {
297                                 Page.RegisterRequiresPostBack (this);
298                         }
299                 }
300
301                 void RenderLabel (HtmlTextWriter w)
302                 {
303                         if (Text.Length > 0) {
304                                 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
305                                 w.RenderBeginTag (HtmlTextWriterTag.Label);
306                                 w.Write (this.Text);
307                                 w.RenderEndTag ();
308                         }
309                 }
310                 
311 #if NET_2_0
312                 protected internal
313 #else           
314                 protected
315 #endif          
316                 override void Render (HtmlTextWriter w)
317                 {
318                         bool control_style = false;
319                         
320                         /* Need to apply the styles around the text
321                          * label too
322                          */
323                         if (ControlStyleCreated) {
324                                 ControlStyle.AddAttributesToRender (w, this);
325                                 w.RenderBeginTag (HtmlTextWriterTag.Span);
326
327                                 control_style = true;
328                         } else if (Attributes ["style"] != null) {
329                                 /* TODO: check if this or the style
330                                  * has precendence, or if they should
331                                  * be merged (if I can figure out how
332                                  * to turn a CssStyleCollection into a
333                                  * Style)
334                                  */
335                                 CssStyleCollection style = Attributes.CssStyle;
336                                 
337                                 w.AddAttribute (HtmlTextWriterAttribute.Style,
338                                                 style.BagToString ());
339                                 w.RenderBeginTag (HtmlTextWriterTag.Span);
340
341                                 control_style = true;
342                         }
343                         
344                         if (TextAlign == TextAlign.Left) {
345                                 RenderLabel (w);
346                                 base.Render (w);
347                         } else {
348                                 base.Render (w);
349                                 RenderLabel (w);
350                         }
351
352                         if (control_style) {
353                                 w.RenderEndTag ();
354                         }
355                 }
356
357 #if NET_2_0
358                 [MonoTODO]
359                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
360                 {
361                         throw new NotImplementedException ();
362                 }
363
364                 [MonoTODO]
365                 protected virtual void RaisePostDataChangedEvent ()
366                 {
367                         throw new NotImplementedException ();
368                 }
369 #endif
370
371                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
372                 {
373                         string postedValue = postCollection[postDataKey];
374                         bool postedBool = ((postedValue != null) &&
375                                            (postedValue.Length > 0));
376                         
377                         if (Checked != postedBool) {
378                                 Checked = postedBool;
379                                 return (true);
380                         }
381
382                         return (false);
383                 }
384
385                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
386                 {
387                         OnCheckedChanged (EventArgs.Empty);
388                 }
389
390                 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
391                 {
392                 }
393         }
394 }