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