* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TableCell.cs
1 //
2 // System.Web.UI.WebControls.TableCell.cs
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.ComponentModel;
30 using System.Globalization;
31 using System.Security.Permissions;
32 using System.Text;
33
34 namespace System.Web.UI.WebControls {
35
36         // CAS
37         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         // attributes
40         [ControlBuilder (typeof (TableCellControlBuilder))]
41         [DefaultProperty ("Text")]
42         [ParseChildren (false)]
43         [ToolboxItem ("")]
44 #if NET_2_0
45         [Bindable (false)]
46         [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
47 #else
48         [PersistChildren (true)]
49 #endif
50         public class TableCell : WebControl {
51
52                 public TableCell ()
53                         : base (HtmlTextWriterTag.Td)
54                 {
55                         AutoID = false;
56                 }
57
58                 // FIXME: is there a clean way to change the tag's name without using a ctor ?
59                 // if not then this truly limits the usefulness of inheritance
60                 internal TableCell (HtmlTextWriterTag tag)
61                         : base (tag)
62                 {
63                         AutoID = false;
64                 }
65
66
67 #if NET_2_0
68                 [DefaultValue (null)]
69                 [TypeConverter (typeof (StringArrayConverter))]
70                 public virtual string[] AssociatedHeaderCellID {
71                         get {
72                                 object o = ViewState ["AssociatedHeaderCellID"];
73                                 return (o == null) ? new string[0] : (string[]) o;
74                         }
75                         set {
76                                 if (value == null)
77                                         ViewState.Remove ("AssociatedHeaderCellID");
78                                 else
79                                         ViewState ["AssociatedHeaderCellID"] = value;
80                         }
81                 }
82 #endif
83
84 #if ONLY_1_1
85                 [Bindable (true)]
86 #endif
87                 [DefaultValue (0)]
88                 [WebSysDescription ("")]
89                 [WebCategory ("Appearance")]
90                 public virtual int ColumnSpan {
91                         get {
92                                 object o = ViewState ["ColumnSpan"];
93                                 return (o == null) ? 0 : (int) o;
94                         }
95                         set {
96                                 // LAMESPEC: undocumented (but like Table.CellPadding)
97                                 if (value < 0)
98                                         throw new ArgumentOutOfRangeException ("< 0");
99                                 ViewState ["ColumnSpan"] = value;
100                         }
101                 }
102
103 #if ONLY_1_1
104                 [Bindable (true)]
105 #endif
106                 [DefaultValue (HorizontalAlign.NotSet)]
107                 [WebSysDescription ("")]
108                 [WebCategory ("Layout")]
109                 public virtual HorizontalAlign HorizontalAlign {
110                         get {
111                                 if (!ControlStyleCreated)
112                                         return HorizontalAlign.NotSet; // default value
113                                 return TableItemStyle.HorizontalAlign;
114                         }
115                         set { TableItemStyle.HorizontalAlign = value; }
116                 }
117
118 #if ONLY_1_1
119                 [Bindable (true)]
120 #endif
121                 [DefaultValue (0)]
122                 [WebSysDescription ("")]
123                 [WebCategory ("Layout")]
124                 public virtual int RowSpan {
125                         get {
126                                 object o = ViewState ["RowSpan"];
127                                 return (o == null) ? 0 : (int) o;
128                         }
129                         set {
130                                 // LAMESPEC: undocumented (but like Table.CellPadding)
131                                 if (value < 0)
132                                         throw new ArgumentOutOfRangeException ("< 0");
133                                 ViewState ["RowSpan"] = value;
134                         }
135                 }
136
137 #if NET_2_0
138                 [Localizable (true)]
139                 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
140 #else
141                 [Bindable (true)]
142 #endif
143                 [DefaultValue ("")]
144                 [WebSysDescription ("")]
145                 [WebCategory ("Appearance")]
146                 public virtual string Text {
147                         get {
148                                 object o = ViewState ["Text"];
149                                 return (o == null) ? String.Empty : (string) o;
150                         }
151                         set {
152                                 if (value == null)
153                                         ViewState.Remove ("Text");
154                                 else {
155                                         ViewState ["Text"] = value;
156                                         if (HasControls ())
157                                                 Controls.Clear ();
158                                 }
159                         }
160                 }
161
162 #if ONLY_1_1
163                 [Bindable (true)]
164 #endif
165                 [DefaultValue (VerticalAlign.NotSet)]
166                 [WebSysDescription ("")]
167                 [WebCategory ("Layout")]
168                 public virtual VerticalAlign VerticalAlign {
169                         get {
170                                 if (!ControlStyleCreated)
171                                         return VerticalAlign.NotSet; // default value
172                                 return TableItemStyle.VerticalAlign;
173                         }
174                         set { TableItemStyle.VerticalAlign = value; }
175                 }
176
177 #if ONLY_1_1
178                 [Bindable (true)]
179 #endif
180                 [DefaultValue (true)]
181                 [WebSysDescription ("")]
182                 [WebCategory ("Layout")]
183                 public virtual bool Wrap {
184                         get {
185                                 if (!ControlStyleCreated)
186                                         return true; // default value
187                                 return TableItemStyle.Wrap;
188                         }
189                         set { TableItemStyle.Wrap = value; }
190                 }
191
192                 private TableItemStyle TableItemStyle {
193                         get { return (ControlStyle as TableItemStyle); }
194                 }
195
196
197                 protected override void AddAttributesToRender (HtmlTextWriter writer)
198                 {
199                         base.AddAttributesToRender (writer);
200                         if (writer == null)
201                                 return;
202
203                         int i = ColumnSpan;
204                         if (i > 0)
205                                 writer.AddAttribute (HtmlTextWriterAttribute.Colspan, i.ToString (CultureInfo.InvariantCulture));
206
207                         i = RowSpan;
208                         if (i > 0)
209                                 writer.AddAttribute (HtmlTextWriterAttribute.Rowspan, i.ToString (CultureInfo.InvariantCulture));
210 #if NET_2_0
211                         string[] ahci = AssociatedHeaderCellID;
212                         if (ahci.Length > 1) {
213                                 StringBuilder sb = new StringBuilder ();
214                                 for (i = 0; i < ahci.Length - 1; i++) {
215                                         sb.Append (ahci [i]);
216                                         sb.Append (",");
217                                 }
218                                 sb.Append (ahci.Length - 1);
219                                 writer.AddAttribute (HtmlTextWriterAttribute.Headers, sb.ToString ());
220                         } else if (ahci.Length == 1) {
221                                 // most common case (without a StringBuilder)
222                                 writer.AddAttribute (HtmlTextWriterAttribute.Headers, ahci [0]);
223                         }
224 #endif
225                 }
226
227                 protected override void AddParsedSubObject (object obj)
228                 {
229                         if (HasControls ()) {
230                                 base.AddParsedSubObject (obj);
231                                 return;
232                         }
233                         
234                         LiteralControl lc = (obj as LiteralControl);
235                         if (lc == null) {
236                                 string s = Text;
237                                 if (s.Length > 0) {
238                                         Controls.Add (new LiteralControl (s));
239                                         // remove from viewstate
240                                         Text = null;
241                                 }
242                                 base.AddParsedSubObject(obj);
243                         } else {
244                                 // this will clear any existing controls
245                                 Text = lc.Text;
246                         }
247                 }
248
249                 protected override Style CreateControlStyle ()
250                 {
251                         return new TableItemStyle (ViewState);
252                 }
253
254 #if NET_2_0
255                 protected internal
256 #else           
257                 protected
258 #endif          
259                 override void RenderContents (HtmlTextWriter writer)
260                 {
261                         if (HasControls ())
262                                 base.RenderContents (writer);
263                         else
264                                 writer.Write (Text);
265                 }
266         }
267 }
268