**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / WebControl.cs
blob92db72c6b69f827c9ea97dae2e3d34279112e506
1 //
2 // System.Web.UI.WebControls.WebControl.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
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Collections;
35 using System.ComponentModel;
36 using System.Web;
37 using System.Web.UI;
38 using System.Drawing;
39 using System.Collections.Specialized;
41 namespace System.Web.UI.WebControls
43 [PersistChildrenAttribute(false)]
44 [ParseChildrenAttribute(true)]
45 public class WebControl : Control, IAttributeAccessor
47 HtmlTextWriterTag tagKey;
48 AttributeCollection attributes;
49 StateBag attributeState;
50 Style controlStyle;
51 bool enabled = true;
52 string tagName;
54 protected WebControl () : this (HtmlTextWriterTag.Span)
58 public WebControl (HtmlTextWriterTag tag)
60 tagKey = tag;
63 protected WebControl (string tag)
65 tagName = tag;
68 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
69 [WebSysDescription ("A keyboard shortcut for the WebControl.")]
70 public virtual string AccessKey
72 get
74 object o = ViewState["AccessKey"];
75 if(o!=null)
76 return (string)o;
77 return String.Empty;
79 set
81 if (value != null && value.Length > 1)
82 throw new ArgumentOutOfRangeException ("value");
83 ViewState["AccessKey"] = value;
87 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
88 [WebSysDescription ("Attribute tags for the Webcontrol.")]
89 public AttributeCollection Attributes
91 get
93 if(attributes==null)
95 //FIXME: From where to get StateBag and how? I think this method is OK!
96 if(attributeState == null)
98 attributeState = new StateBag(true);
99 if(IsTrackingViewState)
101 attributeState.TrackViewState();
104 attributes = new AttributeCollection(attributeState);
106 return attributes;
110 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
111 [TypeConverter (typeof (WebColorConverter))]
112 [WebSysDescription ("The background color for the WebControl.")]
113 public virtual Color BackColor
115 get {
116 if (!ControlStyleCreated)
117 return Color.Empty;
118 return ControlStyle.BackColor;
121 set {
122 ControlStyle.BackColor = value;
126 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
127 [TypeConverter (typeof (WebColorConverter))]
128 [WebSysDescription ("The border color for the WebControl.")]
129 public virtual Color BorderColor
131 get {
132 if (!ControlStyleCreated)
133 return Color.Empty;
134 return ControlStyle.BorderColor;
137 set {
138 ControlStyle.BorderColor = value;
142 [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
143 [WebSysDescription ("The style/type of the border used for the WebControl.")]
144 public virtual BorderStyle BorderStyle
146 get {
147 if (!ControlStyleCreated)
148 return BorderStyle.NotSet;
149 return ControlStyle.BorderStyle;
152 set {
153 ControlStyle.BorderStyle = value;
157 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
158 [WebSysDescription ("The width of the border used for the WebControl.")]
159 public virtual Unit BorderWidth
161 get {
162 if (!ControlStyleCreated)
163 return Unit.Empty;
164 return ControlStyle.BorderWidth;
167 set {
168 if (value.Value < 0)
169 throw new ArgumentOutOfRangeException ("value");
170 ControlStyle.BorderWidth = value;
174 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
175 [WebSysDescription ("The style used to display this Webcontrol.")]
176 public Style ControlStyle
180 if(controlStyle == null)
182 controlStyle = CreateControlStyle();
183 if(IsTrackingViewState)
185 controlStyle.TrackViewState();
187 controlStyle.LoadViewState(null);
189 return controlStyle;
193 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
194 [WebSysDescription ("Determines if a style exists for this Webcontrol.")]
195 public bool ControlStyleCreated
199 return (controlStyle!=null);
203 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
204 [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
205 public virtual string CssClass
209 return ControlStyle.CssClass;
213 ControlStyle.CssClass = value;
217 [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
218 [WebSysDescription ("The activation state of this WebControl.")]
219 public virtual bool Enabled {
220 get {
221 return enabled;
223 set {
224 if (enabled != value) {
225 ViewState ["Enabled"] = value;
226 if (IsTrackingViewState)
227 EnableViewState = true;
230 enabled = value;
234 [DefaultValue (null), NotifyParentProperty (true), WebCategory ("Appearance")]
235 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
236 [WebSysDescription ("The font of this WebControl.")]
237 public virtual FontInfo Font
241 return ControlStyle.Font;
245 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
246 [TypeConverter (typeof (WebColorConverter))]
247 [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
248 public virtual Color ForeColor
250 get {
251 if (!ControlStyleCreated)
252 return Color.Empty;
253 return ControlStyle.ForeColor;
256 set {
257 ControlStyle.ForeColor = value;
261 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
262 [WebSysDescription ("The height of this WebControl.")]
263 public virtual Unit Height
267 return ControlStyle.Height;
271 if (value.Value < 0)
272 throw new ArgumentOutOfRangeException ("value");
273 ControlStyle.Height = value;
277 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
278 [WebSysDescription ("Direct access to the styles used for this Webcontrol.")]
279 public CssStyleCollection Style
283 return Attributes.CssStyle;
287 [DefaultValue (0), WebCategory ("Behavior")]
288 [WebSysDescription ("The order in which this WebControl gets tabbed through.")]
289 public virtual short TabIndex
293 object o = ViewState["TabIndex"];
294 if(o!=null)
295 return (short)o;
296 return 0;
300 if(value < short.MinValue || value > short.MaxValue)
301 throw new ArgumentOutOfRangeException ("value");
302 ViewState["TabIndex"] = value;
306 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
307 [WebSysDescription ("A tooltip that is shown when hovering the mouse above the WebControl.")]
308 public virtual string ToolTip
312 object o = ViewState["ToolTip"];
313 if(o!=null)
314 return (string)o;
315 return String.Empty;
319 ViewState["ToolTip"] = value;
323 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
324 [WebSysDescription ("The width of this WebControl.")]
325 public virtual Unit Width
329 return ControlStyle.Width;
333 if (value.Value < 0)
334 throw new ArgumentOutOfRangeException ("value");
335 ControlStyle.Width = value;
339 public void ApplyStyle(Style s)
341 if (s != null && !s.IsEmpty)
342 ControlStyle.CopyFrom (s);
345 public void CopyBaseAttributes(WebControl controlSrc)
348 * AccessKey, Enabled, ToolTip, TabIndex, Attributes
350 AccessKey = controlSrc.AccessKey;
351 Enabled = controlSrc.Enabled;
352 ToolTip = controlSrc.ToolTip;
353 TabIndex = controlSrc.TabIndex;
354 AttributeCollection otherAtt = controlSrc.Attributes;
355 foreach (string key in otherAtt.Keys)
356 Attributes [key] = otherAtt [key];
359 public void MergeStyle(Style s)
361 ControlStyle.MergeWith(s);
364 public virtual void RenderBeginTag(HtmlTextWriter writer)
366 AddAttributesToRender(writer);
367 writer.RenderBeginTag(TagKey);
370 public virtual void RenderEndTag(HtmlTextWriter writer)
372 writer.RenderEndTag();
375 [Browsable (false)]
376 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
377 protected virtual HtmlTextWriterTag TagKey
381 return tagKey;
385 [Browsable (false)]
386 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
387 protected virtual string TagName
391 if(tagName == null && TagKey != 0)
393 tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToString();
395 // What if tagName is null and tagKey 0?
396 return tagName;
400 protected virtual void AddAttributesToRender(HtmlTextWriter writer)
402 if(ID!=null)
404 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
406 if(AccessKey.Length>0)
408 writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
410 if(!Enabled)
412 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
414 if(ToolTip.Length>0)
416 writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
418 if(TabIndex != 0)
420 writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
422 if(ControlStyleCreated)
424 if(!ControlStyle.IsEmpty)
426 ControlStyle.AddAttributesToRender(writer, this);
429 if(attributeState != null){
430 IEnumerator ie = Attributes.Keys.GetEnumerator ();
431 while (ie.MoveNext ()){
432 string key = (string) ie.Current;
433 writer.AddAttribute (key, Attributes [key]);
438 protected virtual Style CreateControlStyle ()
440 return new Style (ViewState);
443 protected override void LoadViewState (object savedState)
445 if (savedState == null)
446 return;
448 Pair saved = (Pair) savedState;
449 base.LoadViewState (saved.First);
451 if (ControlStyleCreated || ViewState [System.Web.UI.WebControls.Style.selectionBitString] != null)
452 ControlStyle.LoadViewState (null);
454 if (saved.Second != null)
456 if (attributeState == null)
458 attributeState = new StateBag(true);
459 attributeState.TrackViewState();
461 attributeState.LoadViewState (saved.Second);
464 object enable = ViewState["Enabled"];
465 if (enable!=null)
467 Enabled = (bool)enable;
468 EnableViewState = true;
472 protected override void Render(HtmlTextWriter writer)
474 RenderBeginTag (writer);
475 RenderContents (writer);
476 RenderEndTag (writer);
479 protected virtual void RenderContents(HtmlTextWriter writer)
481 base.Render (writer);
484 protected override object SaveViewState()
486 if (EnableViewState)
487 ViewState["Enabled"] = enabled;
488 if (ControlStyleCreated)
489 ControlStyle.SaveViewState ();
491 object baseView = base.SaveViewState ();
492 object attrView = null;
493 if (attributeState != null)
494 attrView = attributeState.SaveViewState ();
496 if (baseView == null && attrView == null)
497 return null;
499 return new Pair (baseView, attrView);
502 protected override void TrackViewState()
504 base.TrackViewState();
505 if (ControlStyleCreated)
506 ControlStyle.TrackViewState ();
507 if (attributeState != null)
508 attributeState.TrackViewState ();
511 string IAttributeAccessor.GetAttribute(string key)
513 if (attributes != null)
514 return Attributes [key] as string;
516 return null;
519 void IAttributeAccessor.SetAttribute(string key, string value)
521 Attributes [key] = value;