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