2001-12-19 Gaurav Vaish <gvaish@iitk.ac.in>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / WebControl.cs
1 /**\r
2  * Namespace: System.Web.UI.WebControls\r
3  * Class:     WebControl\r
4  * \r
5  * Author:  Gaurav Vaish\r
6  * Maintainer: gvaish@iitk.ac.in\r
7  * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>\r
8  * Implementation: yes\r
9  * Status:  40%\r
10  * \r
11  * (C) Gaurav Vaish (2001)\r
12  */\r
13 \r
14 using System;\r
15 using System.Collections;\r
16 using System.Web;\r
17 using System.Web.UI;\r
18 using System.Drawing;\r
19 using System.Collections.Specialized;\r
20 \r
21 namespace System.Web.UI.WebControls\r
22 {\r
23         public class WebControl : Control, IAttributeAccessor\r
24         {\r
25                 //TODO: A list of private members may be incomplete\r
26 \r
27                 private HtmlTextWriterTag   writerTag;\r
28                 private string              stringTag;\r
29                 private AttributeCollection attributes;\r
30                 private StateBag            attributeState;\r
31                 private Style               controlStyle;\r
32                 private bool                enabled;\r
33                 private HtmlTextWriterTag   tagKey;\r
34                 private string              tagName;\r
35 \r
36                 // TODO: The constructors definitions\r
37                 protected WebControl(): base()\r
38                 {\r
39                         //todo: what now? To be rendered as SPAN tag!\r
40                         Initialize();\r
41                 }\r
42                 \r
43                 public WebControl(HtmlTextWriterTag tag): base()\r
44                 {\r
45                         //FIXME: am i right?\r
46                         writerTag = tag;\r
47                         //stringTag = null;\r
48                         Initialize();\r
49                 }\r
50 \r
51                 protected WebControl(string tag): base()\r
52                 {\r
53                         //FIXME: am i right?\r
54                         stringTag = tag;\r
55                         //writerTag = null;\r
56                         Initialize();\r
57                 }\r
58                 \r
59                 private void Initialize()\r
60                 {\r
61                         controlStyle   = null;\r
62                         enabled        = true;\r
63                         tagName        = null;\r
64                         attributeState = null;\r
65                 }\r
66                 \r
67                 public virtual string AccessKey\r
68                 {\r
69                         get\r
70                         {\r
71                                 object o = ViewState["AccessKey"];\r
72                                 if(o!=null)\r
73                                         return (string)o;\r
74                                 return String.Empty;\r
75                         }\r
76                         set\r
77                         {\r
78                                 ViewState["AccessKey"] = value;\r
79                         }\r
80                 }\r
81                 \r
82                 public AttributeCollection Attributes\r
83                 {\r
84                         get\r
85                         {\r
86                                 throw new NotImplementedException("FIXME: \"Internal\" method calls");\r
87                                 if(attributes==null)\r
88                                 {\r
89                                         //TODO: From where to get StateBag and how? I think this method is OK!\r
90                                         if(attributeState == null)\r
91                                         {\r
92                                                 attributeState = new StateBag(true);\r
93                                                 //FIXME: Uncomment the following in the final release\r
94                                                 // commented because of the assembly problem.\r
95                                                 //The function TrackViewState() is internal\r
96                                                 /*\r
97                                                 if(IsTrackingViewState)\r
98                                                 {\r
99                                                         attributeState.TrackViewState();\r
100                                                 }\r
101                                                 */\r
102                                         }\r
103                                         attributes = new AttributeCollection(attributeState);\r
104                                 }\r
105                                 return attributes;\r
106                         }\r
107                 }\r
108                 \r
109                 public Style ControlStyle               \r
110                 {\r
111                         get\r
112                         {\r
113                                 throw new NotImplementedException("FIXME: \"Internal\" method calls");\r
114                                 if(controlStyle == null)\r
115                                 {\r
116                                         controlStyle = CreateControlStyle();\r
117                                         //FIXME: Uncomment the following in the final release\r
118                                         // commented because of the assembly problem.\r
119                                         //The functions TrackViewState() and LoadViewState() are internal\r
120                                         /*\r
121                                         if(IsTrackingViewState)\r
122                                         {\r
123                                                 controlStyle.TrackViewState();\r
124                                         }\r
125                                         controlStyle.LoadViewState(null);\r
126                                         */\r
127                                 }\r
128                                 return controlStyle;\r
129                         }\r
130                 }\r
131                 \r
132                 public bool ControlStyleCreated\r
133                 {\r
134                         get\r
135                         {\r
136                                 return (controlStyle!=null);\r
137                         }\r
138                 }\r
139                 \r
140                 public virtual string CssClass\r
141                 {\r
142                         get\r
143                         {\r
144                                 return ControlStyle.CssClass;\r
145                         }\r
146                         set\r
147                         {\r
148                                 ControlStyle.CssClass = value;\r
149                         }\r
150                 }\r
151                 \r
152                 public virtual bool Enabled\r
153                 {\r
154                         get\r
155                         {\r
156                                 return enabled;\r
157                         }\r
158                         set\r
159                         {\r
160                                 enabled = value;\r
161                         }\r
162                 }\r
163 \r
164                 public virtual FontInfo Font\r
165                 {\r
166                         get\r
167                         {\r
168                                 return ControlStyle.Font;\r
169                         }\r
170                 }\r
171                 \r
172                 public virtual Color ForeColor\r
173                 {\r
174                         get\r
175                         {\r
176                                 return ControlStyle.ForeColor;\r
177                         }\r
178                         set\r
179                         {\r
180                                 ControlStyle.ForeColor = value;\r
181                         }\r
182                 }\r
183                 \r
184                 public virtual Unit Height\r
185                 {\r
186                         get\r
187                         {\r
188                                 return ControlStyle.Height;\r
189                         }\r
190                         set\r
191                         {\r
192                                 ControlStyle.Height = value;\r
193                         }\r
194                 }\r
195                 \r
196                 public CssStyleCollection Style\r
197                 {\r
198                         get\r
199                         {\r
200                                 return Attributes.CssStyle;\r
201                         }\r
202                 }\r
203                 \r
204                 public virtual short TabIndex\r
205                 {\r
206                         get\r
207                         {\r
208                                 object o = ViewState["TabIndex"];\r
209                                 if(o!=null)\r
210                                         return (short)o;\r
211                                 return 0;\r
212                         }\r
213                         set\r
214                         {\r
215                                 if(value < -32768 || value > 32767)\r
216                                         throw new ArgumentException();\r
217                                 ViewState["TabIndex"] = value;\r
218                         }\r
219                 }\r
220                 \r
221                 public virtual string ToolTip\r
222                 {\r
223                         get\r
224                         {\r
225                                 object o = ViewState["ToolTip"];\r
226                                 if(o!=null)\r
227                                         return (string)o;\r
228                                 return String.Empty;\r
229                         }\r
230                         set\r
231                         {\r
232                                 ViewState["ToolTip"] = value;\r
233                         }\r
234                 }\r
235                 \r
236                 public virtual Unit Width\r
237                 {\r
238                         get\r
239                         {\r
240                                 return ControlStyle.Width;\r
241                         }\r
242                         set\r
243                         {\r
244                                 ControlStyle.Width = value;\r
245                         }\r
246                 }\r
247                 \r
248                 public void ApplyStyle(Style s)\r
249                 {\r
250                         /* FIXME: Again internal problem\r
251                         if(!ControlStyle.IsEmpty)\r
252                         {\r
253                         */\r
254                                 ControlStyle.CopyFrom(s);\r
255                         //}\r
256                         throw new NotImplementedException("FIXME: \"Internal\" method calls");\r
257                 }\r
258                 \r
259                 public void CopyBaseAttributes(WebControl controlSrc)\r
260                 {\r
261                         //TODO: tocopy\r
262                         /*\r
263                          * AccessKey, Enabled, ToolTip, TabIndex, Attributes\r
264                         */\r
265                         AccessKey  = controlSrc.AccessKey;\r
266                         Enabled    = controlSrc.Enabled;\r
267                         ToolTip    = controlSrc.ToolTip;\r
268                         TabIndex   = controlSrc.TabIndex;\r
269                         attributes = controlSrc.Attributes;\r
270                         throw new NotImplementedException();\r
271                 }\r
272                 \r
273                 public void MergeStyle(Style s)\r
274                 {\r
275                         ControlStyle.MergeWith(s);\r
276                 }\r
277                 \r
278                 public virtual void RenderBeginTag(HtmlTextWriter writer)\r
279                 {\r
280                         AddAttributesToRender(writer);\r
281                         if( Enum.IsDefined(typeof(HtmlTextWriterTag), TagKey) )\r
282                         {\r
283                                 writer.RenderBeginTag(TagKey);\r
284                                 return;\r
285                         }\r
286                         writer.RenderBeginTag(tagName);\r
287                 }\r
288                 \r
289                 public virtual void RenderEndTag(HtmlTextWriter writer)\r
290                 {\r
291                         writer.RenderEndTag();\r
292                 }\r
293                 \r
294                 protected virtual HtmlTextWriterTag TagKey\r
295                 {\r
296                         get\r
297                         {\r
298                                 return tagKey;\r
299                         }\r
300                 }\r
301                 \r
302                 protected virtual string TagName\r
303                 {\r
304                         get\r
305                         {\r
306                                 if(tagName==null && Enum.IsDefined(typeof(HtmlTextWriterTag), TagKey) )\r
307                                 {\r
308                                         tagName = Enum.Format(typeof(HtmlTextWriterTag), tagKey, "G").ToString();\r
309                                 }\r
310                                 return tagName;\r
311                         }\r
312                 }\r
313                 \r
314                 protected virtual void AddAttributesToRender(HtmlTextWriter writer)\r
315                 {\r
316                         if(ID!=null)\r
317                         {\r
318                                 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);\r
319                         }\r
320                         if(AccessKey.Length>0)\r
321                         {\r
322                                 writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);\r
323                         }\r
324                         if(!Enabled)\r
325                         {\r
326                                 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");\r
327                         }\r
328                         if(ToolTip.Length>0)\r
329                         {\r
330                                 writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);\r
331                         }\r
332                         if(TabIndex != 0)\r
333                         {\r
334                                 writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());\r
335                         }\r
336                         if(ControlStyleCreated)\r
337                         {\r
338                                 if(!ControlStyle.IsEmpty)\r
339                                 {\r
340                                         ControlStyle.AddAttributesToRender(writer, this);\r
341                                 }\r
342                         }\r
343                         if(attributeState!=null)\r
344                         {\r
345                                 IEnumerator ie = Attributes.Keys.GetEnumerator();\r
346                                 do\r
347                                 {\r
348                                         writer.AddAttribute((string)ie.Current, Attributes[(string)ie.Current]);\r
349                                 } while(ie.MoveNext());\r
350                         }\r
351                 }\r
352                 \r
353                 protected virtual Style CreateControlStyle()\r
354                 {\r
355                         return new Style(ViewState);\r
356                 }\r
357                 \r
358                 protected override void LoadViewState(object savedState)\r
359                 {\r
360                         throw new NotImplementedException();\r
361                         //TODO: Load viewStates\r
362                         /*\r
363                          * May be will have to first look at Control::LoadViewState \r
364                         */\r
365                 }\r
366                 \r
367                 protected override void Render(HtmlTextWriter writer)\r
368                 {\r
369                         RenderBeginTag(writer);\r
370                         RenderContents(writer);\r
371                         RenderEndTag(writer);\r
372                 }\r
373                 \r
374                 protected virtual void RenderContents(HtmlTextWriter writer)\r
375                 {\r
376                         base.Render(writer);\r
377                 }\r
378                 \r
379                 protected override object SaveViewState()\r
380                 {\r
381                         throw new NotImplementedException();\r
382                         //TODO: Implement me!\r
383                 }\r
384                 \r
385                 protected override void TrackViewState()\r
386                 {\r
387                         base.TrackViewState();\r
388                         if(ControlStyleCreated)\r
389                         {\r
390                                 ControlStyle.TrackViewState();\r
391                         }\r
392                         if(attributeState!=null)\r
393                         {\r
394                                 attributeState.TrackViewState();\r
395                         }\r
396                 }\r
397                 \r
398                 // Implemented procedures\r
399                 //TODO: The scope of the functions - is public valid. Test thru Reflection\r
400                 string IAttributeAccessor.GetAttribute(string key)\r
401                 {\r
402                         if(Attributes!=null)\r
403                                 return (string)Attributes[key];\r
404                         return null;\r
405                 }\r
406                 \r
407                 void IAttributeAccessor.SetAttribute(string key, string value)\r
408                 {\r
409                         Attributes[key] = value;\r
410                 }\r
411         }\r
412 }\r