2004-05-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TableCell.cs
1 //
2 // System.Web.UI.WebControls.TableCell.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 //\r
11 \r
12 using System;\r
13 using System.Globalization;\r
14 using System.Web;\r
15 using System.Web.UI;\r
16 using System.ComponentModel;\r
17 \r
18 namespace System.Web.UI.WebControls\r
19 {\r
20         [DefaultProperty("Text")]\r
21         [ToolboxItem(false)]\r
22         [ControlBuilder(typeof(TableCellControlBuilder))]\r
23         [ParseChildren(false)]\r
24         [PersistChildren(true)]\r
25         public class TableCell: WebControl\r
26         {\r
27                 public TableCell () : base (HtmlTextWriterTag.Td)\r
28                 {\r
29                         PreventAutoID ();\r
30                 }\r
31 \r
32                 internal TableCell (HtmlTextWriterTag tag) : base (tag)\r
33                 {\r
34                         PreventAutoID ();\r
35                 }\r
36
37                 [DefaultValue (0), Bindable (true), WebCategory ("Appearance")]
38                 [WebSysDescription ("The number of columns spanned by this cell.")]\r
39                 public virtual int ColumnSpan\r
40                 {\r
41                         get {\r
42                                 object o = ViewState ["ColumnSpan"];\r
43                                 return (o == null) ? 0 : (int) o;\r
44                         }\r
45 \r
46                         set {
47                                 if (value < 0)
48                                         throw new ArgumentOutOfRangeException ("value", "ColumnSpan value has to be >= 0.");
49                                 ViewState ["ColumnSpan"] = value;
50                         }\r
51                 }\r
52
53                 [DefaultValue (0), Bindable (true), WebCategory ("Layout")]
54                 [WebSysDescription ("The number of rows spanned by this cell.")]\r
55                 public virtual int RowSpan\r
56                 {\r
57                         get {\r
58                                 object o = ViewState ["RowSpan"];\r
59                                 return (o == null) ? 0 : (int) o;\r
60                         }\r
61 \r
62                         set {
63                                 if (value < 0)
64                                         throw new ArgumentOutOfRangeException ("value", "RowSpan value has to be >= 0.");
65                                 ViewState ["RowSpan"] = value;
66                         }\r
67                 }\r
68
69                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
70                 [WebSysDescription ("The text that is shown in this cell.")]\r
71                 public virtual string Text\r
72                 {\r
73                         get {\r
74                                 object o = ViewState ["Text"];\r
75                                 return (o == null) ? String.Empty : (string) o;\r
76                         }\r
77 \r
78                         set {
79                                 if (HasControls ())
80                                         Controls.Clear ();
81                                 ViewState ["Text"] = value;
82                         }
83                 }\r
84
85                 [DefaultValue (typeof (HorizontalAlign), "NotSet"), Bindable (true), WebCategory ("Layout")]
86                 [WebSysDescription ("The horizontal alignment for this cell.")]\r
87                 public virtual HorizontalAlign HorizontalAlign\r
88                 {\r
89                         get {\r
90                                 if (ControlStyleCreated)\r
91                                         return ((TableItemStyle) ControlStyle).HorizontalAlign;\r
92                                 return HorizontalAlign.NotSet;\r
93                         }\r
94                         set { ((TableItemStyle) ControlStyle).HorizontalAlign = value; }\r
95                 }\r
96
97                 [DefaultValue (typeof (VerticalAlign), "NotSet"), Bindable (true), WebCategory ("Layout")]
98                 [WebSysDescription ("The horizontal alignment for this cell.")]\r
99                 public virtual VerticalAlign VerticalAlign\r
100                 {\r
101                         get {\r
102                                 if (ControlStyleCreated)\r
103                                         return ((TableItemStyle) ControlStyle).VerticalAlign;\r
104                                 return VerticalAlign.NotSet;\r
105                         }\r
106 \r
107                         set { ((TableItemStyle) ControlStyle).VerticalAlign = value; }\r
108                 }\r
109
110                 [DefaultValue (true), Bindable (true), WebCategory ("Layout")]
111                 [WebSysDescription ("Determines if the text in the cell should be wraped at line-end.")]\r
112                 public virtual bool Wrap\r
113                 {\r
114                         get {\r
115                                 if (ControlStyleCreated)\r
116                                         return ((TableItemStyle) ControlStyle).Wrap;\r
117                                 return true;\r
118                         }\r
119                         set { ((TableItemStyle) ControlStyle).Wrap = value; }\r
120                 }\r
121 \r
122                 protected override void AddAttributesToRender (HtmlTextWriter writer)\r
123                 {\r
124                         base.AddAttributesToRender (writer);\r
125                         if (ColumnSpan > 0)\r
126                                 writer.AddAttribute (HtmlTextWriterAttribute.Colspan,\r
127                                                      ColumnSpan.ToString (NumberFormatInfo.InvariantInfo));\r
128 \r
129                         if (RowSpan > 0)\r
130                                 writer.AddAttribute (HtmlTextWriterAttribute.Rowspan,\r
131                                                      RowSpan.ToString (NumberFormatInfo.InvariantInfo));\r
132                 }\r
133 \r
134                 protected override void AddParsedSubObject (object obj)\r
135                 {\r
136                         if (HasControls ()){\r
137                                 base.AddParsedSubObject (obj);\r
138                                 return;\r
139                         }\r
140 \r
141                         if (obj is LiteralControl){\r
142                                 Text = ((LiteralControl) obj).Text;\r
143                                 return;\r
144                         }\r
145 \r
146                         string text = Text;\r
147                         if (text.Length > 0){\r
148                                 Text = String.Empty;\r
149                                 base.AddParsedSubObject (new LiteralControl (text));\r
150                         }\r
151 \r
152                         base.AddParsedSubObject (obj);\r
153                 }\r
154 \r
155                 protected override Style CreateControlStyle ()\r
156                 {\r
157                         return new TableItemStyle (ViewState);\r
158                 }\r
159 \r
160                 protected override void RenderContents (HtmlTextWriter writer)\r
161                 {\r
162                         if (HasControls ())\r
163                                 base.RenderContents (writer);\r
164                         else\r
165                                 writer.Write (Text);\r
166                 }\r
167         }\r
168 }\r
169 \r