2002-03-16 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               borderStyle;\r
32                 private Style               controlStyle;\r
33                 private bool                enabled;\r
34                 private HtmlTextWriterTag   tagKey;\r
35                 private string              tagName;\r
36 \r
37                 // TODO: The constructors definitions\r
38                 protected WebControl(): base()\r
39                 {\r
40                         //todo: what now? To be rendered as SPAN tag!\r
41                         Initialize();\r
42                 }\r
43 \r
44                 public WebControl(HtmlTextWriterTag tag): base()\r
45                 {\r
46                         //FIXME: am i right?\r
47                         writerTag = tag;\r
48                         //stringTag = null;\r
49                         Initialize();\r
50                 }\r
51 \r
52                 protected WebControl(string tag): base()\r
53                 {\r
54                         //FIXME: am i right?\r
55                         stringTag = tag;\r
56                         //writerTag = null;\r
57                         Initialize();\r
58                 }\r
59 \r
60                 private void Initialize()\r
61                 {\r
62                         controlStyle   = null;\r
63                         enabled        = true;\r
64                         tagName        = null;\r
65                         attributeState = null;\r
66                 }\r
67 \r
68                 public virtual string AccessKey\r
69                 {\r
70                         get\r
71                         {\r
72                                 object o = ViewState["AccessKey"];\r
73                                 if(o!=null)\r
74                                         return (string)o;\r
75                                 return String.Empty;\r
76                         }\r
77                         set\r
78                         {\r
79                                 ViewState["AccessKey"] = value;\r
80                         }\r
81                 }\r
82 \r
83                 [MonoTODO("FIXME_Internal_method_calls")]\r
84                 public AttributeCollection Attributes\r
85                 {\r
86                         get\r
87                         {\r
88                                 throw new NotImplementedException();\r
89                                 if(attributes==null)\r
90                                 {\r
91                                         //FIXME: 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 virtual Color BackColor\r
112                 {\r
113                         get\r
114                         {\r
115                                 object o = ViewState["BackColor"];\r
116                                 if(o != null)\r
117                                 {\r
118                                         return (Color)o;\r
119                                 }\r
120                                 return Color.Empty;\r
121                         }\r
122                         set\r
123                         {\r
124                                 ViewState["BackColor"] = value;\r
125                         }\r
126                 }\r
127 \r
128                 public virtual Color BorderColor\r
129                 {\r
130                         get\r
131                         {\r
132                                 object o = ViewState["BorderColor"];\r
133                                 if(o != null)\r
134                                 {\r
135                                         return (Color)o;\r
136                                 }\r
137                                 return Color.Empty;\r
138                         }\r
139                         set\r
140                         {\r
141                                 ViewState["BorderColor"] = value;\r
142                         }\r
143                 }\r
144 \r
145                 [MonoTODO("FIXME_Internal_method_calls")]\r
146                 public virtual Style BorderStyle\r
147                 {\r
148                         get\r
149                         {\r
150                                 return borderStyle;\r
151                         }\r
152                         set\r
153                         {\r
154                                 borderStyle = value;\r
155                         }\r
156                 }\r
157 \r
158                 public virtual Unit BorderWidth\r
159                 {\r
160                         get\r
161                         {\r
162                                 object o = ViewState["BorderWidth"];\r
163                                 if(o != null)\r
164                                 {\r
165                                         return (Unit)o;\r
166                                 }\r
167                                 return Unit.Empty;\r
168                         }\r
169                         set\r
170                         {\r
171                                 if(value.Value < 0)\r
172                                 {\r
173                                         throw new ArgumentException();\r
174                                 }\r
175                                 ViewState["BorderWidth"] = value;\r
176                         }\r
177                 }\r
178 \r
179                 [MonoTODO("FIXME_Internal_method_calls")]\r
180                 public virtual Style ControlStyle\r
181                 {\r
182                         get\r
183                         {\r
184                                 if(controlStyle == null)\r
185                                 {\r
186                                         controlStyle = CreateControlStyle();\r
187                                         //FIXME: Uncomment the following in the final release\r
188                                         // commented because of the assembly problem.\r
189                                         //The functions TrackViewState() and LoadViewState() are internal\r
190                                         /*\r
191                                         if(IsTrackingViewState)\r
192                                         {\r
193                                                 controlStyle.TrackViewState();\r
194                                         }\r
195                                         controlStyle.LoadViewState(null);\r
196                                         */\r
197                                 }\r
198                                 return controlStyle;\r
199                         }\r
200                 }\r
201 \r
202                 public bool ControlStyleCreated\r
203                 {\r
204                         get\r
205                         {\r
206                                 return (controlStyle!=null);\r
207                         }\r
208                 }\r
209 \r
210                 public virtual string CssClass\r
211                 {\r
212                         get\r
213                         {\r
214                                 return ControlStyle.CssClass;\r
215                         }\r
216                         set\r
217                         {\r
218                                 ControlStyle.CssClass = value;\r
219                         }\r
220                 }\r
221 \r
222                 public virtual bool Enabled\r
223                 {\r
224                         get\r
225                         {\r
226                                 return enabled;\r
227                         }\r
228                         set\r
229                         {\r
230                                 enabled = value;\r
231                         }\r
232                 }\r
233 \r
234                 public virtual FontInfo Font\r
235                 {\r
236                         get\r
237                         {\r
238                                 return ControlStyle.Font;\r
239                         }\r
240                 }\r
241 \r
242                 public virtual Color ForeColor\r
243                 {\r
244                         get\r
245                         {\r
246                                 return ControlStyle.ForeColor;\r
247                         }\r
248                         set\r
249                         {\r
250                                 ControlStyle.ForeColor = value;\r
251                         }\r
252                 }\r
253 \r
254                 public virtual Unit Height\r
255                 {\r
256                         get\r
257                         {\r
258                                 return ControlStyle.Height;\r
259                         }\r
260                         set\r
261                         {\r
262                                 ControlStyle.Height = value;\r
263                         }\r
264                 }\r
265 \r
266                 public CssStyleCollection Style\r
267                 {\r
268                         get\r
269                         {\r
270                                 return Attributes.CssStyle;\r
271                         }\r
272                 }\r
273 \r
274                 public virtual short TabIndex\r
275                 {\r
276                         get\r
277                         {\r
278                                 object o = ViewState["TabIndex"];\r
279                                 if(o!=null)\r
280                                         return (short)o;\r
281                                 return 0;\r
282                         }\r
283                         set\r
284                         {\r
285                                 if(value < -32768 || value > 32767)\r
286                                         throw new ArgumentException();\r
287                                 ViewState["TabIndex"] = value;\r
288                         }\r
289                 }\r
290 \r
291                 public virtual string ToolTip\r
292                 {\r
293                         get\r
294                         {\r
295                                 object o = ViewState["ToolTip"];\r
296                                 if(o!=null)\r
297                                         return (string)o;\r
298                                 return String.Empty;\r
299                         }\r
300                         set\r
301                         {\r
302                                 ViewState["ToolTip"] = value;\r
303                         }\r
304                 }\r
305 \r
306                 public virtual Unit Width\r
307                 {\r
308                         get\r
309                         {\r
310                                 return ControlStyle.Width;\r
311                         }\r
312                         set\r
313                         {\r
314                                 ControlStyle.Width = value;\r
315                         }\r
316                 }\r
317 \r
318                 [MonoTODO("FIXME_Internal_method_calls")]\r
319                 public void ApplyStyle(Style s)\r
320                 {\r
321                         /* FIXME: Again internal problem\r
322                         if(!ControlStyle.IsEmpty)\r
323                         {\r
324                         */\r
325                                 ControlStyle.CopyFrom(s);\r
326                         //}\r
327                 }\r
328 \r
329                 [MonoTODO]\r
330                 public void CopyBaseAttributes(WebControl controlSrc)\r
331                 {\r
332                         //TODO: tocopy\r
333                         /*\r
334                          * AccessKey, Enabled, ToolTip, TabIndex, Attributes\r
335                         */\r
336                         AccessKey  = controlSrc.AccessKey;\r
337                         Enabled    = controlSrc.Enabled;\r
338                         ToolTip    = controlSrc.ToolTip;\r
339                         TabIndex   = controlSrc.TabIndex;\r
340                         attributes = controlSrc.Attributes;\r
341                         throw new NotImplementedException();\r
342                 }\r
343 \r
344                 public void MergeStyle(Style s)\r
345                 {\r
346                         ControlStyle.MergeWith(s);\r
347                 }\r
348 \r
349                 public virtual void RenderBeginTag(HtmlTextWriter writer)\r
350                 {\r
351                         AddAttributesToRender(writer);\r
352                         if(Enum.IsDefined(typeof(HtmlTextWriterTag), TagKey) )\r
353                         {\r
354                                 writer.RenderBeginTag(TagKey);\r
355                                 return;\r
356                         }\r
357                         writer.RenderBeginTag(tagName);\r
358                 }\r
359 \r
360                 public virtual void RenderEndTag(HtmlTextWriter writer)\r
361                 {\r
362                         writer.RenderEndTag();\r
363                 }\r
364 \r
365                 protected virtual HtmlTextWriterTag TagKey\r
366                 {\r
367                         get\r
368                         {\r
369                                 return tagKey;\r
370                         }\r
371                 }\r
372 \r
373                 protected virtual string TagName\r
374                 {\r
375                         get\r
376                         {\r
377                                 if(tagName==null && Enum.IsDefined(typeof(HtmlTextWriterTag), tagKey) )\r
378                                 {\r
379                                         tagName = Enum.Format(typeof(HtmlTextWriterTag), tagKey, "G").ToString();\r
380                                 }\r
381                                 return tagName;\r
382                         }\r
383                 }\r
384 \r
385                 protected virtual void AddAttributesToRender(HtmlTextWriter writer)\r
386                 {\r
387                         if(ID!=null)\r
388                         {\r
389                                 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);\r
390                         }\r
391                         if(AccessKey.Length>0)\r
392                         {\r
393                                 writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);\r
394                         }\r
395                         if(!Enabled)\r
396                         {\r
397                                 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");\r
398                         }\r
399                         if(ToolTip.Length>0)\r
400                         {\r
401                                 writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);\r
402                         }\r
403                         if(TabIndex != 0)\r
404                         {\r
405                                 writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());\r
406                         }\r
407                         if(ControlStyleCreated)\r
408                         {\r
409                                 if(!ControlStyle.IsEmpty)\r
410                                 {\r
411                                         ControlStyle.AddAttributesToRender(writer, this);\r
412                                 }\r
413                         }\r
414                         if(attributeState!=null)\r
415                         {\r
416                                 IEnumerator ie = Attributes.Keys.GetEnumerator();\r
417                                 do\r
418                                 {\r
419                                         writer.AddAttribute((string)ie.Current, Attributes[(string)ie.Current]);\r
420                                 } while(ie.MoveNext());\r
421                         }\r
422                 }\r
423 \r
424                 protected virtual Style CreateControlStyle()\r
425                 {\r
426                         return new Style(ViewState);\r
427                 }\r
428 \r
429                 [MonoTODO]\r
430                 protected override void LoadViewState(object savedState)\r
431                 {\r
432                         throw new NotImplementedException();\r
433                         //TODO: Load viewStates\r
434                         /*\r
435                          * May be will have to first look at Control::LoadViewState\r
436                         */\r
437                 }\r
438 \r
439                 protected override void Render(HtmlTextWriter writer)\r
440                 {\r
441                         RenderBeginTag(writer);\r
442                         RenderContents(writer);\r
443                         RenderEndTag(writer);\r
444                 }\r
445 \r
446                 protected virtual void RenderContents(HtmlTextWriter writer)\r
447                 {\r
448                         base.Render(writer);\r
449                 }\r
450 \r
451                 [MonoTODO]\r
452                 protected override object SaveViewState()\r
453                 {\r
454                         throw new NotImplementedException();\r
455                         //TODO: Implement me!\r
456                 }\r
457 \r
458                 protected override void TrackViewState()\r
459                 {\r
460                         TrackViewState();\r
461                         if(ControlStyleCreated)\r
462                         {\r
463                                 ControlStyle.TrackViewState();\r
464                         }\r
465                         if(attributeState!=null)\r
466                         {\r
467                                 attributeState.TrackViewState();\r
468                         }\r
469                 }\r
470 \r
471                 string IAttributeAccessor.GetAttribute(string key)\r
472                 {\r
473                         if(Attributes!=null)\r
474                                 return (string)Attributes[key];\r
475                         return null;\r
476                 }\r
477 \r
478                 void IAttributeAccessor.SetAttribute(string key, string value)\r
479                 {\r
480                         Attributes[key] = value;\r
481                 }\r
482         }\r
483 }\r