1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
27 using System.ComponentModel;
30 namespace System.Web.UI.WebControls {
32 [PersistChildrenAttribute (false, false)]
33 [ParseChildrenAttribute (true, ChildControlType = typeof(Control))]
36 [PersistChildrenAttribute (false)]
37 [ParseChildrenAttribute (true)]
39 public class WebControl : Control, IAttributeAccessor {
41 HtmlTextWriterTag tag;
43 AttributeCollection attributes;
44 StateBag attribute_state;
47 public WebControl (HtmlTextWriterTag tag)
53 protected WebControl () : this (HtmlTextWriterTag.Span)
57 protected WebControl (string tag)
59 this.tag = HtmlTextWriterTag.Unknown;
68 [WebSysDescription ("")]
69 [WebCategory ("Behavior")]
70 public virtual string AccessKey {
72 return ViewState.GetString ("AccessKey", string.Empty);
75 if (value == null || value.Length < 2)
76 ViewState ["AccessKey"] = value;
78 throw new ArgumentException ("AccessKey can only be null, empty or a single character", "value");
83 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
84 [WebSysDescription ("")]
85 [WebCategory ("Behavior")]
86 public AttributeCollection Attributes {
88 if (attributes == null) {
89 attribute_state = new StateBag (true);
90 if (IsTrackingViewState)
91 attribute_state.TrackViewState ();
93 attributes = new AttributeCollection (attribute_state);
102 [DefaultValue(typeof (Color), "")]
103 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
104 [WebSysDescription ("")]
105 [WebCategory ("Appearance")]
106 public virtual Color BackColor {
111 return style.BackColor;
114 ControlStyle.BackColor = value;
121 [DefaultValue(typeof (Color), "")]
122 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
123 [WebSysDescription ("")]
124 [WebCategory ("Appearance")]
125 public virtual Color BorderColor {
130 return style.BorderColor;
134 ControlStyle.BorderColor = value;
141 [DefaultValue(BorderStyle.NotSet)]
142 [WebSysDescription ("")]
143 [WebCategory ("Appearance")]
144 public virtual BorderStyle BorderStyle {
147 return BorderStyle.NotSet;
149 return style.BorderStyle;
152 if (value < BorderStyle.NotSet || value > BorderStyle.Outset)
153 throw new ArgumentOutOfRangeException ("value");
155 ControlStyle.BorderStyle = value;
162 [DefaultValue(typeof (Unit), "")]
163 [WebSysDescription ("")]
164 [WebCategory ("Appearance")]
165 public virtual Unit BorderWidth {
170 return style.BorderWidth;
173 ControlStyle.BorderWidth = value;
178 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
179 [WebSysDescription ("")]
180 [WebCategory ("Appearance")]
181 public Style ControlStyle {
184 style = this.CreateControlStyle ();
186 if (IsTrackingViewState)
187 style.TrackViewState ();
195 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
196 public bool ControlStyleCreated {
198 return style != null;
206 [WebSysDescription ("")]
207 [WebCategory ("Appearance")]
208 public virtual string CssClass {
213 return style.CssClass;
216 ControlStyle.CssClass = value;
225 public virtual bool Enabled {
231 if (enabled != value) {
232 ViewState ["Enabled"] = value;
241 public virtual new bool EnableTheming
244 throw new NotImplementedException ();
247 throw new NotImplementedException ();
255 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
256 [NotifyParentProperty(true)]
257 [WebSysDescription ("")]
258 [WebCategory ("Appearance")]
259 public virtual FontInfo Font {
261 // Oddly enough, it looks like we have to let it create the style
262 // since we can't create a FontInfo without a style owner
263 return ControlStyle.Font;
270 [DefaultValue(typeof (Color), "")]
271 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
272 [WebSysDescription ("")]
273 [WebCategory ("Appearance")]
274 public virtual Color ForeColor {
279 return style.ForeColor;
282 ControlStyle.ForeColor = value;
288 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
290 public bool HasAttributes
293 throw new NotImplementedException ();
301 [DefaultValue(typeof (Unit), "")]
302 [WebSysDescription ("")]
303 [WebCategory ("Layout")]
304 public virtual Unit Height {
312 ControlStyle.Height = value;
319 public virtual new string SkinID
322 throw new NotImplementedException ();
325 throw new NotImplementedException ();
331 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
332 [WebSysDescription ("")]
333 [WebCategory ("Style")]
334 public CssStyleCollection Style {
336 return Attributes.CssStyle;
340 [DefaultValue((short)0)]
341 [WebSysDescription ("")]
342 [WebCategory ("Behavior")]
343 public virtual short TabIndex {
345 return ViewState.GetShort ("TabIndex", 0);
348 ViewState ["TabIndex"] = value;
359 [WebSysDescription ("")]
360 [WebCategory ("Behavior")]
361 public virtual string ToolTip {
363 return ViewState.GetString ("ToolTip", string.Empty);
366 ViewState ["ToolTip"] = value;
373 [DefaultValue(typeof (Unit), "")]
374 [WebSysDescription ("")]
375 [WebCategory ("Layout")]
376 public virtual Unit Width {
384 ControlStyle.Width = value;
389 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
390 protected virtual HtmlTextWriterTag TagKey {
397 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
398 protected virtual string TagName {
400 // do this here to avoid potentially costly lookups on every control
401 if (tag_name == null)
402 tag_name = HtmlTextWriter.StaticGetTagName (TagKey);
410 protected internal bool IsEnabled
413 throw new NotImplementedException ();
419 public void ApplyStyle (Style s)
421 if (s != null && !s.IsEmpty)
422 ControlStyle.CopyFrom(s);
425 public void CopyBaseAttributes (WebControl controlSrc)
429 if (controlSrc == null)
432 o = controlSrc.ViewState ["Enabled"];
437 o = controlSrc.ViewState ["AccessKey"];
439 ViewState ["AccessKey"] = o;
441 o = controlSrc.ViewState ["TabIndex"];
443 ViewState ["TabIndex"] = o;
445 o = controlSrc.ViewState ["ToolTip"];
447 ViewState ["ToolTip"] = o;
449 if (controlSrc.attributes != null)
450 foreach (string s in controlSrc.attributes.Keys)
451 Attributes [s] = controlSrc.attributes [s];
454 public void MergeStyle (Style s)
456 if (s != null && !s.IsEmpty)
457 ControlStyle.MergeWith(s);
460 public virtual void RenderBeginTag (HtmlTextWriter writer)
462 AddAttributesToRender (writer);
464 if (TagKey == HtmlTextWriterTag.Unknown)
465 writer.RenderBeginTag (TagName);
467 writer.RenderBeginTag (TagKey);
471 public virtual void RenderEndTag (HtmlTextWriter writer)
473 writer.RenderEndTag ();
476 protected virtual void AddAttributesToRender (HtmlTextWriter writer)
479 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
481 if (AccessKey != string.Empty)
482 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
485 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
487 if (ToolTip != string.Empty)
488 writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
491 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, TabIndex.ToString ());
493 if (style != null && !style.IsEmpty)
494 style.AddAttributesToRender(writer, this);
496 if (attributes != null)
497 foreach(string s in attributes.Keys)
498 writer.AddAttribute (s, attributes [s]);
501 protected virtual Style CreateControlStyle()
503 style = new Style (ViewState);
507 protected override void LoadViewState (object savedState)
509 if (savedState == null) {
510 base.LoadViewState(null);
514 Triplet triplet = (Triplet) savedState;
516 base.LoadViewState (triplet.First);
518 if (triplet.Second != null) {
519 if (attribute_state == null) {
520 attribute_state = new StateBag ();
521 if (IsTrackingViewState)
522 attribute_state.TrackViewState ();
524 attribute_state.LoadViewState (triplet.Second);
525 attributes = new AttributeCollection(attribute_state);
528 if (triplet.Third != null) {
530 style = CreateControlStyle ();
532 style.LoadViewState (triplet.Third);
535 enabled = ViewState.GetBool("Enabled", true);
543 override void Render (HtmlTextWriter writer)
545 RenderBeginTag (writer);
546 RenderContents (writer);
547 RenderEndTag (writer);
555 virtual void RenderContents (HtmlTextWriter writer)
557 base.Render (writer);
560 protected override object SaveViewState ()
563 object attr_view_state = null;
564 object style_view_state = null;
566 view_state = base.SaveViewState ();
568 if (attribute_state != null)
569 attr_view_state = attribute_state.SaveViewState ();
572 style_view_state = style.SaveViewState ();
574 if (view_state == null && attr_view_state == null && style_view_state == null)
577 return new Triplet (view_state, attr_view_state, style_view_state);
580 protected override void TrackViewState()
583 style.TrackViewState ();
585 if (attribute_state != null)
586 attribute_state.TrackViewState ();
588 base.TrackViewState ();
591 string IAttributeAccessor.GetAttribute (string key)
593 if (attributes != null)
594 return attributes [key];
599 void IAttributeAccessor.SetAttribute (string key, string value)
601 Attributes [key] = value;