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