2004-06-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBox.cs
1 //
2 // System.Web.UI.WebControls.CheckBox.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 // Thanks to Leen Toelen (toelen@hotmail.com)'s classes that helped me
11 // to write the contents of the function LoadPostData(...)
12 //
13
14 using System;
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Globalization;
18 using System.Web;
19 using System.Web.UI;
20 using System.ComponentModel;
21 using System.ComponentModel.Design;
22
23 namespace System.Web.UI.WebControls
24 {
25         [DefaultEvent("CheckedChanged")]
26         [DefaultProperty("Text")]
27         [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
28         [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
29         public class CheckBox : WebControl, IPostBackDataHandler
30         {
31                 private static readonly object CheckedChangedEvent = new object();
32                 
33                 public CheckBox(): base(HtmlTextWriterTag.Input)
34                 {
35                 }
36
37                 [DefaultValue (false), WebCategory ("Behavior")]
38                 [WebSysDescription ("The control automatically posts back after changing the text.")]
39                 public virtual bool AutoPostBack
40                 {
41                         get {
42                                  object o = ViewState ["AutoPostBack"];
43                                  return (o == null) ? false : (bool) o;
44                         }
45
46                         set { ViewState ["AutoPostBack"] = value; }
47                 }
48
49
50                 [DefaultValue (false), Bindable (true)]
51                 [WebSysDescription ("Determines if the control is checked.")]
52                 public virtual bool Checked
53                 {
54                         get {
55                                 object o = ViewState ["Checked"];
56                                 return (o == null) ? false : (bool) o;
57                         }
58
59                         set { ViewState ["Checked"] = value; }
60                 }
61
62                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
63                 [WebSysDescription ("The text that this control displays.")]
64                 public virtual string Text
65                 {
66                         get {
67                                 object o = ViewState ["Text"];
68                                 return (o == null) ? String.Empty : (string) o;
69                         }
70
71                         set { ViewState ["Text"] = value; }
72                 }
73                 
74                 private bool SaveCheckedViewState
75                 {
76                         get {
77                                 if (Events [CheckedChangedEvent] != null || !Enabled)
78                                         return true;
79
80                                 Type type = GetType ();
81                                 return (type != typeof (CheckBox) && type != typeof (RadioButton));
82                         }
83                 }
84
85
86                 [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
87                 [WebSysDescription ("The alignment of the text.")]
88                 public virtual TextAlign TextAlign
89                 {
90                         get {
91                                 object o = ViewState ["TextAlign"];
92                                 return (o == null) ? TextAlign.Right : (TextAlign) o;
93                         }
94
95                         set {
96                                 if (!System.Enum.IsDefined (typeof (TextAlign), value))
97                                         throw new ArgumentException ();
98                                 ViewState ["TextAlign"] = value;
99                         }
100                 }
101
102                 [WebCategory ("Action")]
103                 [WebSysDescription ("Raised when the control is checked or unchecked.")]
104                 public event EventHandler CheckedChanged
105                 {
106                         add { Events.AddHandler (CheckedChangedEvent, value); }
107                         remove { Events.RemoveHandler (CheckedChangedEvent, value); }
108                 }
109                 
110                 protected virtual void OnCheckedChanged(EventArgs e)
111                 {
112                         if(Events != null){
113                                 EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
114                                 if(eh != null)
115                                         eh (this, e);
116                         }
117                 }
118                 
119                 protected override void OnPreRender(EventArgs e)
120                 {
121                         if (Page != null && Enabled) {
122                                 Page.RegisterRequiresPostBack (this);
123                                 if (AutoPostBack)
124                                         Page.RequiresPostBackScript ();
125                         }
126
127                         if (!SaveCheckedViewState)
128                                 ViewState.SetItemDirty ("Checked", false);
129                 }
130                 
131                 protected override void Render (HtmlTextWriter writer)
132                 {
133                         bool hasBeginRendering = false;
134                         if(ControlStyleCreated && !ControlStyle.IsEmpty){
135                                 hasBeginRendering = true;
136                                 ControlStyle.AddAttributesToRender (writer, this);
137                         }
138                         
139                         if (!Enabled)\r
140                         {
141                                 hasBeginRendering = true;\r
142                                 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");                              \r
143                         }
144
145                         if (ToolTip.Length > 0){
146                                 hasBeginRendering = true;
147                                 writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
148                         }
149
150                         if (Attributes.Count > 0){
151                                 string val = Attributes ["value"];
152                                 Attributes.Remove ("value");
153                                 if (Attributes.Count > 0){
154                                         hasBeginRendering = true;
155                                         Attributes.AddAttributes (writer);
156                                 }
157
158                                 if (val != null)
159                                         Attributes ["value"] = val;
160                         }
161
162                         if (hasBeginRendering)
163                                 writer.RenderBeginTag (HtmlTextWriterTag.Span);
164
165                         if (Text.Length > 0){
166                                 TextAlign ta = TextAlign;
167                                 if(ta == TextAlign.Right)
168                                         RenderInputTag (writer, ClientID);
169                                 writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
170                                 writer.RenderBeginTag (HtmlTextWriterTag.Label);
171                                 writer.Write (Text);
172                                 writer.RenderEndTag ();
173                                 if(ta == TextAlign.Left)
174                                         RenderInputTag (writer, ClientID);
175                         }
176                         else
177                                 RenderInputTag (writer, ClientID);
178
179                         if (hasBeginRendering)
180                                 writer.RenderEndTag ();
181                 }
182                 
183                 internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
184                 {
185                         if (!Enabled)
186                                 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
187
188                         writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
189                         writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
190                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
191                         if (Checked)
192                                 writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
193
194                         if (AutoPostBack){
195                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
196                                                      Page.GetPostBackClientEvent (this, String.Empty));
197                                 writer.AddAttribute ("language", "javascript");
198                         }
199
200                         if (AccessKey.Length > 0)
201                                 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
202
203                         if (TabIndex != 0)
204                                 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
205                                                      TabIndex.ToString (NumberFormatInfo.InvariantInfo));
206
207                         writer.RenderBeginTag (HtmlTextWriterTag.Input);
208                         writer.RenderEndTag ();
209                 }
210                 
211                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
212                 {
213                         string postedVal = postCollection [postDataKey];                        \r
214                         bool haveData = ((postedVal != null)&& (postedVal.Length > 0));\r
215                         bool diff  = (haveData != Checked);
216                         Checked = haveData;
217                         return diff ;
218                 }
219                 
220                 void IPostBackDataHandler.RaisePostDataChangedEvent()
221                 {
222                         OnCheckedChanged (EventArgs.Empty);
223                 }
224         }
225 }