This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / WebControl.cs
1 //
2 // System.Web.UI.WebControls.WebControl.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.ComponentModel;
36 using System.Web;
37 using System.Web.UI;
38 using System.Drawing;
39 using System.Collections.Specialized;
40
41 namespace System.Web.UI.WebControls
42 {
43         [PersistChildrenAttribute(false)]
44         [ParseChildrenAttribute(true)]
45         public class WebControl : Control, IAttributeAccessor
46         {
47                 HtmlTextWriterTag tagKey;
48                 AttributeCollection attributes;
49                 StateBag attributeState;
50                 Style controlStyle;
51                 bool enabled = true;
52                 string tagName;
53
54                 protected WebControl () : this (HtmlTextWriterTag.Span)
55                 {
56                 }
57
58                 public WebControl (HtmlTextWriterTag tag)
59                 {
60                         tagKey = tag;
61                 }
62
63                 protected WebControl (string tag)
64                 {
65                         tagName = tag;
66                 }
67
68                 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
69                 [WebSysDescription ("A keyboard shortcut for the WebControl.")]\r
70                 public virtual string AccessKey\r
71                 {\r
72                         get\r
73                         {\r
74                                 object o = ViewState["AccessKey"];\r
75                                 if(o!=null)\r
76                                         return (string)o;\r
77                                 return String.Empty;\r
78                         }\r
79                         set\r
80                         {
81                                 if (value != null && value.Length > 1)
82                                         throw new ArgumentOutOfRangeException ("value");
83                                 ViewState["AccessKey"] = value;\r
84                         }\r
85                 }\r
86 \r
87                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
88                 [WebSysDescription ("Attribute tags for the Webcontrol.")]\r
89                 public AttributeCollection Attributes\r
90                 {\r
91                         get\r
92                         {\r
93                                 if(attributes==null)\r
94                                 {\r
95                                         //FIXME: From where to get StateBag and how? I think this method is OK!\r
96                                         if(attributeState == null)\r
97                                         {\r
98                                                 attributeState = new StateBag(true);\r
99                                                 if(IsTrackingViewState)\r
100                                                 {\r
101                                                         attributeState.TrackViewState();\r
102                                                 }\r
103                                         }\r
104                                         attributes = new AttributeCollection(attributeState);\r
105                                 }\r
106                                 return attributes;\r
107                         }\r
108                 }\r
109
110                 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
111                 [TypeConverter (typeof (WebColorConverter))]
112                 [WebSysDescription ("The background color for the WebControl.")]\r
113                 public virtual Color BackColor\r
114                 {\r
115                         get {\r
116                                 if (!ControlStyleCreated)\r
117                                         return Color.Empty;\r
118                                 return ControlStyle.BackColor;\r
119                         }\r
120 \r
121                         set {\r
122                                 ControlStyle.BackColor = value;\r
123                         }\r
124                 }\r
125
126                 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
127                 [TypeConverter (typeof (WebColorConverter))]
128                 [WebSysDescription ("The border color for the WebControl.")]\r
129                 public virtual Color BorderColor\r
130                 {\r
131                         get {\r
132                                 if (!ControlStyleCreated)\r
133                                         return Color.Empty;\r
134                                 return ControlStyle.BorderColor;\r
135                         }\r
136 \r
137                         set {\r
138                                 ControlStyle.BorderColor = value;\r
139                         }\r
140                 }\r
141
142                 [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
143                 [WebSysDescription ("The style/type of the border used for the WebControl.")]\r
144                 public virtual BorderStyle BorderStyle\r
145                 {\r
146                         get {\r
147                                 if (!ControlStyleCreated)\r
148                                         return BorderStyle.NotSet;\r
149                                 return ControlStyle.BorderStyle;\r
150                         }\r
151 \r
152                         set {\r
153                                 ControlStyle.BorderStyle = value;\r
154                         }\r
155                 }\r
156
157                 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
158                 [WebSysDescription ("The width of the border used for the WebControl.")]\r
159                 public virtual Unit BorderWidth\r
160                 {\r
161                         get {\r
162                                 if (!ControlStyleCreated)\r
163                                         return Unit.Empty;\r
164                                 return ControlStyle.BorderWidth;\r
165                         }\r
166 \r
167                         set {\r
168                                 if (value.Value < 0)\r
169                                         throw new ArgumentOutOfRangeException ("value");\r
170                                 ControlStyle.BorderWidth = value;\r
171                         }\r
172                 }\r
173
174                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
175                 [WebSysDescription ("The style used to display this Webcontrol.")]\r
176                 public Style ControlStyle\r
177                 {\r
178                         get\r
179                         {\r
180                                 if(controlStyle == null)\r
181                                 {\r
182                                         controlStyle = CreateControlStyle();\r
183                                         if(IsTrackingViewState)\r
184                                         {\r
185                                                 controlStyle.TrackViewState();\r
186                                         }\r
187                                         controlStyle.LoadViewState(null);\r
188                                 }\r
189                                 return controlStyle;\r
190                         }\r
191                 }\r
192
193                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
194                 [WebSysDescription ("Determines if a style exists for this Webcontrol.")]\r
195                 public bool ControlStyleCreated\r
196                 {\r
197                         get\r
198                         {\r
199                                 return (controlStyle!=null);\r
200                         }\r
201                 }\r
202
203                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
204                 [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]\r
205                 public virtual string CssClass\r
206                 {\r
207                         get\r
208                         {\r
209                                 return ControlStyle.CssClass;\r
210                         }\r
211                         set\r
212                         {\r
213                                 ControlStyle.CssClass = value;\r
214                         }\r
215                 }\r
216
217                 [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
218                 [WebSysDescription ("The activation state of this WebControl.")]
219                 public virtual bool Enabled {
220                         get {
221                                 return enabled;
222                         }
223                         set {
224                                 if (enabled != value) {
225                                         ViewState ["Enabled"] = value;
226                                         if (IsTrackingViewState)
227                                                 EnableViewState = true;
228                                 }
229
230                                 enabled = value;
231                         }
232                 }
233
234                 [DefaultValue (null), NotifyParentProperty (true), WebCategory ("Appearance")]
235                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
236                 [WebSysDescription ("The font of this WebControl.")]\r
237                 public virtual FontInfo Font\r
238                 {\r
239                         get\r
240                         {\r
241                                 return ControlStyle.Font;\r
242                         }\r
243                 }\r
244
245                 [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
246                 [TypeConverter (typeof (WebColorConverter))]
247                 [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]\r
248                 public virtual Color ForeColor\r
249                 {\r
250                         get {\r
251                                 if (!ControlStyleCreated)\r
252                                         return Color.Empty;\r
253                                 return ControlStyle.ForeColor;\r
254                         }\r
255 \r
256                         set {\r
257                                 ControlStyle.ForeColor = value;\r
258                         }\r
259                 }\r
260
261                 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
262                 [WebSysDescription ("The height of this WebControl.")]\r
263                 public virtual Unit Height\r
264                 {\r
265                         get\r
266                         {\r
267                                 return ControlStyle.Height;\r
268                         }\r
269                         set\r
270                         {\r
271                                 if (value.Value < 0)
272                                         throw new ArgumentOutOfRangeException ("value");
273                                 ControlStyle.Height = value;\r
274                         }\r
275                 }\r
276
277                 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
278                 [WebSysDescription ("Direct access to the styles used for this Webcontrol.")]\r
279                 public CssStyleCollection Style\r
280                 {\r
281                         get\r
282                         {\r
283                                 return Attributes.CssStyle;\r
284                         }\r
285                 }\r
286
287                 [DefaultValue (0), WebCategory ("Behavior")]
288                 [WebSysDescription ("The order in which this WebControl gets tabbed through.")]\r
289                 public virtual short TabIndex\r
290                 {\r
291                         get\r
292                         {\r
293                                 object o = ViewState["TabIndex"];\r
294                                 if(o!=null)\r
295                                         return (short)o;\r
296                                 return 0;\r
297                         }\r
298                         set\r
299                         {\r
300                                 if(value < short.MinValue || value > short.MaxValue)\r
301                                         throw new ArgumentOutOfRangeException ("value");\r
302                                 ViewState["TabIndex"] = value;\r
303                         }\r
304                 }\r
305
306                 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
307                 [WebSysDescription ("A tooltip that is shown when hovering the mouse above the WebControl.")]\r
308                 public virtual string ToolTip\r
309                 {\r
310                         get\r
311                         {\r
312                                 object o = ViewState["ToolTip"];\r
313                                 if(o!=null)\r
314                                         return (string)o;\r
315                                 return String.Empty;\r
316                         }\r
317                         set\r
318                         {\r
319                                 ViewState["ToolTip"] = value;\r
320                         }\r
321                 }\r
322
323                 [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
324                 [WebSysDescription ("The width of this WebControl.")]\r
325                 public virtual Unit Width\r
326                 {\r
327                         get\r
328                         {\r
329                                 return ControlStyle.Width;\r
330                         }\r
331                         set\r
332                         {\r
333                                 if (value.Value < 0)
334                                         throw new ArgumentOutOfRangeException ("value");
335                                 ControlStyle.Width = value;\r
336                         }\r
337                 }\r
338
339                 public void ApplyStyle(Style s)
340                 {
341                         if (s != null && !s.IsEmpty)
342                                 ControlStyle.CopyFrom (s);
343                 }
344
345                 public void CopyBaseAttributes(WebControl controlSrc)\r
346                 {\r
347                         /*\r
348                          * AccessKey, Enabled, ToolTip, TabIndex, Attributes\r
349                         */\r
350                         AccessKey  = controlSrc.AccessKey;\r
351                         Enabled    = controlSrc.Enabled;\r
352                         ToolTip    = controlSrc.ToolTip;\r
353                         TabIndex   = controlSrc.TabIndex;\r
354                         AttributeCollection otherAtt = controlSrc.Attributes;\r
355                         foreach (string key in otherAtt.Keys)\r
356                                 Attributes [key] = otherAtt [key];\r
357                 }\r
358 \r
359                 public void MergeStyle(Style s)\r
360                 {\r
361                         ControlStyle.MergeWith(s);\r
362                 }\r
363 \r
364                 public virtual void RenderBeginTag(HtmlTextWriter writer)\r
365                 {\r
366                         AddAttributesToRender(writer);\r
367                         writer.RenderBeginTag(TagName);\r
368                 }\r
369 \r
370                 public virtual void RenderEndTag(HtmlTextWriter writer)\r
371                 {\r
372                         writer.RenderEndTag();\r
373                 }\r
374 \r
375                 [Browsable (false)]
376                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
377                 protected virtual HtmlTextWriterTag TagKey\r
378                 {\r
379                         get\r
380                         {\r
381                                 return tagKey;\r
382                         }\r
383                 }\r
384 \r
385                 [Browsable (false)]
386                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
387                 protected virtual string TagName\r
388                 {\r
389                         get\r
390                         {\r
391                                 if(tagName == null && TagKey != 0)\r
392                                 {\r
393                                         tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToString();\r
394                                 }\r
395                                 // What if tagName is null and tagKey 0?\r
396                                 return tagName;\r
397                         }\r
398                 }\r
399 \r
400                 protected virtual void AddAttributesToRender(HtmlTextWriter writer)\r
401                 {\r
402                         if(ID!=null)\r
403                         {\r
404                                 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);\r
405                         }\r
406                         if(AccessKey.Length>0)\r
407                         {\r
408                                 writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);\r
409                         }\r
410                         if(!Enabled)\r
411                         {\r
412                                 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");\r
413                         }\r
414                         if(ToolTip.Length>0)\r
415                         {\r
416                                 writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);\r
417                         }\r
418                         if(TabIndex != 0)\r
419                         {\r
420                                 writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());\r
421                         }\r
422                         if(ControlStyleCreated)\r
423                         {\r
424                                 if(!ControlStyle.IsEmpty)\r
425                                 {\r
426                                         ControlStyle.AddAttributesToRender(writer, this);\r
427                                 }\r
428                         }\r
429                         if(attributeState != null){\r
430                                 IEnumerator ie = Attributes.Keys.GetEnumerator ();\r
431                                 while (ie.MoveNext ()){\r
432                                         string key = (string) ie.Current;\r
433                                         writer.AddAttribute (key, Attributes [key]);\r
434                                 }\r
435                         }\r
436                 }\r
437
438                 protected virtual Style CreateControlStyle ()
439                 {
440                         return new Style (ViewState);
441                 }
442
443                 protected override void LoadViewState (object savedState)
444                 {
445                         if (savedState == null)
446                                 return;
447
448                         Pair saved = (Pair) savedState;
449                         base.LoadViewState (saved.First);
450                         
451                         if (ControlStyleCreated || ViewState [System.Web.UI.WebControls.Style.selectionBitString] != null)
452                                 ControlStyle.LoadViewState (null);
453
454                         if (saved.Second != null)
455                         {\r
456                                 if (attributeState == null)\r
457                                 {
458                                         attributeState = new StateBag(true);
459                                         attributeState.TrackViewState();\r
460                                 }
461                                 attributeState.LoadViewState (saved.Second);
462                         }\r
463                         \r
464                         object enable = ViewState["Enabled"];\r
465                         if (enable!=null)\r
466                         {\r
467                                 Enabled = (bool)enable;\r
468                                 EnableViewState = true; \r
469                         }
470                 }
471
472                 protected override void Render(HtmlTextWriter writer)
473                 {
474                         RenderBeginTag (writer);
475                         RenderContents (writer);
476                         RenderEndTag (writer);
477                 }
478
479                 protected virtual void RenderContents(HtmlTextWriter writer)
480                 {
481                         base.Render (writer);
482                 }
483
484                 protected override object SaveViewState()
485                 {\r
486                         if (EnableViewState)\r
487                                 ViewState["Enabled"] = enabled;
488                         if (ControlStyleCreated)
489                                 ControlStyle.SaveViewState ();
490                         
491                         object baseView = base.SaveViewState ();
492                         object attrView = null;
493                         if (attributeState != null)
494                                 attrView = attributeState.SaveViewState ();
495                         
496                         if (baseView == null && attrView == null)
497                                 return null;
498
499                         return new Pair (baseView, attrView);
500                 }
501
502                 protected override void TrackViewState()
503                 {
504                         base.TrackViewState();
505                         if (ControlStyleCreated)
506                                 ControlStyle.TrackViewState ();
507                         if (attributeState != null)
508                                 attributeState.TrackViewState ();
509                 }
510
511                 string IAttributeAccessor.GetAttribute(string key)
512                 {
513                         if (attributes != null)
514                                 return Attributes [key] as string;
515
516                         return null;
517                 }
518
519                 void IAttributeAccessor.SetAttribute(string key, string value)
520                 {
521                         Attributes [key] = value;
522                 }
523         }
524 }
525