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