2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlControl.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlControl.cs
3 //
4 // Author
5 //   Bob Smith <bob@thestuff.net>
6 //
7 //
8 // (C) Bob Smith
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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 using System.ComponentModel;
32 using System.ComponentModel.Design;
33 using System.Globalization;
34 using System.Security.Permissions;
35
36 namespace System.Web.UI.HtmlControls{
37         
38         // CAS
39         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         // attributes
42         [ToolboxItem(false)]
43         [Designer ("System.Web.UI.Design.HtmlIntrinsicControlDesigner, " + Consts.AssemblySystem_Design,
44                         "System.ComponentModel.Design.IDesigner")]
45         public abstract class HtmlControl : Control, IAttributeAccessor
46         {
47                 internal string _tagName;
48                 AttributeCollection _attributes;
49
50                 
51 #if NET_2_0
52                 protected
53 #else
54                 public
55 #endif
56                 HtmlControl() : this ("span") {}
57                 
58 #if NET_2_0
59                 protected
60 #else
61                 public
62 #endif
63                 HtmlControl(string tag)
64                 {
65                         _tagName = tag;
66                 }
67                 
68                 protected override ControlCollection CreateControlCollection ()
69                 {
70                         return new EmptyControlCollection (this);
71                 }
72
73                 internal static string AttributeToString(int n){
74                         if (n != -1)return n.ToString(NumberFormatInfo.InvariantInfo);
75                         return null;
76                 }
77                 
78                 internal static string AttributeToString(string s){
79                         if (s != null && s.Length != 0) return s;
80                         return null;
81                 }
82                 
83                 internal void PreProcessRelativeReference(HtmlTextWriter writer, string attribName){
84                         string attr = Attributes[attribName];
85                         if (attr != null){
86                                 if (attr.Length != 0){
87                                         try{
88                                                 attr = ResolveClientUrl(attr);
89                                         }
90                                         catch (Exception) {
91                                                 throw new HttpException(attribName + " property had malformed url");
92                                         }
93                                         writer.WriteAttribute(attribName, attr, true);
94                                         Attributes.Remove(attribName);
95                                 }
96                         }
97                 }
98
99 #if NET_2_0
100                 /* keep these two methods in sync with the
101                  * IAttributeAccessor iface methods below */
102                 protected virtual string GetAttribute (string name)
103                 {
104                         return Attributes[name];
105                 }
106
107                 protected virtual void SetAttribute (string name, string value)
108                 {
109                         Attributes[name] = value;
110                 }
111 #endif          
112                 
113                 string System.Web.UI.IAttributeAccessor.GetAttribute(string name){
114                         return Attributes[name];
115                 }
116                 
117                 void System.Web.UI.IAttributeAccessor.SetAttribute(string name, string value){
118                         Attributes[name] = value;
119                 }
120                 
121                 protected virtual void RenderBeginTag (HtmlTextWriter writer)
122                 {
123                         writer.WriteBeginTag (TagName);
124                         RenderAttributes (writer);
125                         writer.Write ('>');
126                 }
127
128 #if NET_2_0
129                 protected internal
130 #else
131                 protected
132 #endif
133                 override void Render (HtmlTextWriter writer)
134                 {
135                         RenderBeginTag (writer);
136                 }
137                 
138                 protected virtual void RenderAttributes(HtmlTextWriter writer){
139                         if (ID != null){
140                                 writer.WriteAttribute("id",ClientID);
141                         }
142                         Attributes.Render(writer);
143                 }
144                 
145                 [Browsable(false)]
146                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
147                 public AttributeCollection Attributes
148                 {
149                         get { 
150                                 if (_attributes == null)
151                                         _attributes = new AttributeCollection (ViewState);
152                                 return _attributes;
153                         }
154                 }
155
156                 [DefaultValue(false)]
157                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
158                 [WebSysDescription("")]
159                 [WebCategory("Behavior")]
160 #if NET_2_0
161                 [TypeConverter (typeof(MinimizableAttributeTypeConverter))]
162 #endif
163                 public bool Disabled
164                 {
165                         get {
166                                 string disableAttr = Attributes["disabled"] as string;
167                                 return (disableAttr != null);
168                         }
169                         set {
170                                 if (!value)
171                                         Attributes.Remove ("disabled");
172                                 else
173                                         Attributes["disabled"] = "disabled";
174                         }
175                 }
176
177                 [Browsable(false)]
178                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
179                 public CssStyleCollection Style
180                 {
181                         get { return Attributes.CssStyle; }
182                 }
183
184                 [DefaultValue("")]
185                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
186                 [WebSysDescription("")]
187                 [WebCategory("Appearance")]
188                 public virtual string TagName
189                 {
190                         get { return _tagName; }
191                 }
192
193                 protected override bool ViewStateIgnoresCase 
194                 {
195                         get {
196                                 return true;
197                         }
198                 }
199         }
200 }