2005-09-13 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 // 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                 }
56
57                 // FIXME: is there a clean way to change the tag's name without using a ctor ?
58                 // if not then this truly limits the usefulness of inheritance
59                 internal TableCell (HtmlTextWriterTag tag)
60                         : base (tag)
61                 {
62                 }
63
64
65 #if NET_2_0
66                 [DefaultValue (null)]
67                 [TypeConverter (typeof (StringArrayConverter))]
68                 public virtual string[] AssociatedHeaderCellID {
69                         get {
70                                 object o = ViewState ["AssociatedHeaderCellID"];
71                                 return (o == null) ? new string[0] : (string[]) o;
72                         }
73                         set {
74                                 if (value == null)
75                                         ViewState.Remove ("AssociatedHeaderCellID");
76                                 else
77                                         ViewState ["AssociatedHeaderCellID"] = value;
78                         }
79                 }
80 #endif
81
82 #if ONLY_1_1
83                 [Bindable (true)]
84 #endif
85                 [DefaultValue (0)]
86                 [WebSysDescription ("")]
87                 [WebCategory ("Appearance")]
88                 public virtual int ColumnSpan {
89                         get {
90                                 object o = ViewState ["ColumnSpan"];
91                                 return (o == null) ? 0 : (int) o;
92                         }
93                         set {
94                                 // LAMESPEC: undocumented (but like Table.CellPadding)
95                                 if (value < 0)
96                                         throw new ArgumentOutOfRangeException ("< 0");
97                                 ViewState ["ColumnSpan"] = value;
98                         }
99                 }
100
101 #if ONLY_1_1
102                 [Bindable (true)]
103 #endif
104                 [DefaultValue (HorizontalAlign.NotSet)]
105                 [WebSysDescription ("")]
106                 [WebCategory ("Layout")]
107                 public virtual HorizontalAlign HorizontalAlign {
108                         get {
109                                 if (!ControlStyleCreated)
110                                         return HorizontalAlign.NotSet; // default value
111                                 return TableItemStyle.HorizontalAlign;
112                         }
113                         set { TableItemStyle.HorizontalAlign = value; }
114                 }
115
116 #if ONLY_1_1
117                 [Bindable (true)]
118 #endif
119                 [DefaultValue (0)]
120                 [WebSysDescription ("")]
121                 [WebCategory ("Layout")]
122                 public virtual int RowSpan {
123                         get {
124                                 object o = ViewState ["RowSpan"];
125                                 return (o == null) ? 0 : (int) o;
126                         }
127                         set {
128                                 // LAMESPEC: undocumented (but like Table.CellPadding)
129                                 if (value < 0)
130                                         throw new ArgumentOutOfRangeException ("< 0");
131                                 ViewState ["RowSpan"] = value;
132                         }
133                 }
134
135 #if NET_2_0
136                 [Localizable (true)]
137                 [PersistenceMode (PersistenceMode.EncodedInnerDefaultProperty)]
138 #else
139                 [Bindable (true)]
140 #endif
141                 [DefaultValue ("")]
142                 [WebSysDescription ("")]
143                 [WebCategory ("Appearance")]
144                 public virtual string Text {
145                         get {
146                                 object o = ViewState ["Text"];
147                                 return (o == null) ? String.Empty : (string) o;
148                         }
149                         set {
150                                 if (value == null)
151                                         ViewState.Remove ("Text");
152                                 else {
153                                         ViewState ["Text"] = value;
154                                         if (HasControls ())
155                                                 Controls.Clear ();
156                                 }
157                         }
158                 }
159
160 #if ONLY_1_1
161                 [Bindable (true)]
162 #endif
163                 [DefaultValue (VerticalAlign.NotSet)]
164                 [WebSysDescription ("")]
165                 [WebCategory ("Layout")]
166                 public virtual VerticalAlign VerticalAlign {
167                         get {
168                                 if (!ControlStyleCreated)
169                                         return VerticalAlign.NotSet; // default value
170                                 return TableItemStyle.VerticalAlign;
171                         }
172                         set { TableItemStyle.VerticalAlign = value; }
173                 }
174
175 #if ONLY_1_1
176                 [Bindable (true)]
177 #endif
178                 [DefaultValue (true)]
179                 [WebSysDescription ("")]
180                 [WebCategory ("Layout")]
181                 public virtual bool Wrap {
182                         get {
183                                 if (!ControlStyleCreated)
184                                         return true; // default value
185                                 return TableItemStyle.Wrap;
186                         }
187                         set { TableItemStyle.Wrap = value; }
188                 }
189
190                 private TableItemStyle TableItemStyle {
191                         get { return (ControlStyle as TableItemStyle); }
192                 }
193
194
195                 protected override void AddAttributesToRender (HtmlTextWriter writer)
196                 {
197                         base.AddAttributesToRender (writer);
198                         if (writer == null)
199                                 return;
200
201                         int i = ColumnSpan;
202                         if (i > 0)
203                                 writer.AddAttribute (HtmlTextWriterAttribute.Colspan, i.ToString (CultureInfo.InvariantCulture));
204
205                         i = RowSpan;
206                         if (i > 0)
207                                 writer.AddAttribute (HtmlTextWriterAttribute.Rowspan, i.ToString (CultureInfo.InvariantCulture));
208 #if NET_2_0
209                         string[] ahci = AssociatedHeaderCellID;
210                         if (ahci.Length > 1) {
211                                 StringBuilder sb = new StringBuilder ();
212                                 for (i = 0; i < ahci.Length - 1; i++) {
213                                         sb.Append (ahci [i]);
214                                         sb.Append (",");
215                                 }
216                                 sb.Append (ahci.Length - 1);
217                                 writer.AddAttribute (HtmlTextWriterAttribute.Headers, sb.ToString ());
218                         } else if (ahci.Length == 1) {
219                                 // most common case (without a StringBuilder)
220                                 writer.AddAttribute (HtmlTextWriterAttribute.Headers, ahci [0]);
221                         }
222 #endif
223                 }
224
225                 protected override void AddParsedSubObject (object obj)
226                 {
227                         if (HasControls ()) {
228                                 base.AddParsedSubObject (obj);
229                                 return;
230                         }
231                         
232                         LiteralControl lc = (obj as LiteralControl);
233                         if (lc == null) {
234                                 string s = Text;
235                                 if (s.Length > 0) {
236                                         Controls.Add (new LiteralControl (s));
237                                         // remove from viewstate
238                                         Text = null;
239                                 }
240                                 base.AddParsedSubObject(obj);
241                         } else {
242                                 // this will clear any existing controls
243                                 Text = lc.Text;
244                         }
245                 }
246
247                 protected override Style CreateControlStyle ()
248                 {
249                         return new TableItemStyle (ViewState);
250                 }
251
252 #if NET_2_0
253                 protected internal
254 #else           
255                 protected
256 #endif          
257                 override void RenderContents (HtmlTextWriter writer)
258                 {
259                         object o = ViewState ["Text"];
260                         if (o != null) {
261                                 writer.Write (o as string);
262                         }
263                         base.RenderContents (writer);
264                 }
265         }
266 }