Merge pull request #2916 from ludovic-henry/fix-40306
[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                 [DefaultValue (0)]
80                 [WebSysDescription ("")]
81                 [WebCategory ("Appearance")]
82                 public virtual int ColumnSpan {
83                         get {
84                                 object o = ViewState ["ColumnSpan"];
85                                 return (o == null) ? 0 : (int) o;
86                         }
87                         set {
88                                 // LAMESPEC: undocumented (but like Table.CellPadding)
89                                 if (value < 0)
90                                         throw new ArgumentOutOfRangeException ("< 0");
91                                 ViewState ["ColumnSpan"] = value;
92                         }
93                 }
94
95                 [DefaultValue (HorizontalAlign.NotSet)]
96                 [WebSysDescription ("")]
97                 [WebCategory ("Layout")]
98                 public virtual HorizontalAlign HorizontalAlign {
99                         get {
100                                 if (!ControlStyleCreated)
101                                         return HorizontalAlign.NotSet; // default value
102                                 return TableItemStyle.HorizontalAlign;
103                         }
104                         set { TableItemStyle.HorizontalAlign = value; }
105                 }
106
107                 [DefaultValue (0)]
108                 [WebSysDescription ("")]
109                 [WebCategory ("Layout")]
110                 public virtual int RowSpan {
111                         get {
112                                 object o = ViewState ["RowSpan"];
113                                 return (o == null) ? 0 : (int) o;
114                         }
115                         set {
116                                 // LAMESPEC: undocumented (but like Table.CellPadding)
117                                 if (value < 0)
118                                         throw new ArgumentOutOfRangeException ("< 0");
119                                 ViewState ["RowSpan"] = value;
120                         }
121                 }
122
123                 [Localizable (true)]
124                 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
125                 [DefaultValue ("")]
126                 [WebSysDescription ("")]
127                 [WebCategory ("Appearance")]
128                 public virtual string Text {
129                         get {
130                                 object o = ViewState ["Text"];
131                                 return (o == null) ? String.Empty : (string) o;
132                         }
133                         set {
134                                 if (value == null)
135                                         ViewState.Remove ("Text");
136                                 else {
137                                         ViewState ["Text"] = value;
138                                         if (HasControls ())
139                                                 Controls.Clear ();
140                                 }
141                         }
142                 }
143
144                 [DefaultValue (VerticalAlign.NotSet)]
145                 [WebSysDescription ("")]
146                 [WebCategory ("Layout")]
147                 public virtual VerticalAlign VerticalAlign {
148                         get {
149                                 if (!ControlStyleCreated)
150                                         return VerticalAlign.NotSet; // default value
151                                 return TableItemStyle.VerticalAlign;
152                         }
153                         set { TableItemStyle.VerticalAlign = value; }
154                 }
155
156                 [DefaultValue (true)]
157                 [WebSysDescription ("")]
158                 [WebCategory ("Layout")]
159                 public virtual bool Wrap {
160                         get {
161                                 if (!ControlStyleCreated)
162                                         return true; // default value
163                                 return TableItemStyle.Wrap;
164                         }
165                         set { TableItemStyle.Wrap = value; }
166                 }
167                 public override bool SupportsDisabledAttribute {
168                         get { return RenderingCompatibilityLessThan40; }
169                 }
170                 TableItemStyle TableItemStyle {
171                         get { return (ControlStyle as TableItemStyle); }
172                 }
173
174                 protected override void AddAttributesToRender (HtmlTextWriter writer)
175                 {
176                         base.AddAttributesToRender (writer);
177                         if (writer == null)
178                                 return;
179
180                         int i = ColumnSpan;
181                         if (i > 0)
182                                 writer.AddAttribute (HtmlTextWriterAttribute.Colspan, i.ToString (Helpers.InvariantCulture), false);
183
184                         i = RowSpan;
185                         if (i > 0)
186                                 writer.AddAttribute (HtmlTextWriterAttribute.Rowspan, i.ToString (Helpers.InvariantCulture), false);
187                         string[] ahci = AssociatedHeaderCellID;
188                         if (ahci.Length > 1) {
189                                 StringBuilder sb = new StringBuilder ();
190                                 for (i = 0; i < ahci.Length - 1; i++) {
191                                         sb.Append (ahci [i]);
192                                         sb.Append (",");
193                                 }
194                                 sb.Append (ahci.Length - 1);
195                                 writer.AddAttribute (HtmlTextWriterAttribute.Headers, sb.ToString ());
196                         } else if (ahci.Length == 1) {
197                                 // most common case (without a StringBuilder)
198                                 writer.AddAttribute (HtmlTextWriterAttribute.Headers, ahci [0]);
199                         }
200                 }
201
202                 protected override void AddParsedSubObject (object obj)
203                 {
204                         if (HasControls ()) {
205                                 base.AddParsedSubObject (obj);
206                                 return;
207                         }
208                         
209                         LiteralControl lc = (obj as LiteralControl);
210                         if (lc == null) {
211                                 string s = Text;
212                                 if (s.Length > 0) {
213                                         Controls.Add (new LiteralControl (s));
214                                         // remove from viewstate
215                                         Text = null;
216                                 }
217                                 base.AddParsedSubObject(obj);
218                         } else {
219                                 // this will clear any existing controls
220                                 Text = lc.Text;
221                         }
222                 }
223
224                 protected override Style CreateControlStyle ()
225                 {
226                         return new TableItemStyle (ViewState);
227                 }
228
229                 protected internal
230                 override void RenderContents (HtmlTextWriter writer)
231                 {
232                         if (HasControls () || HasRenderMethodDelegate ())
233                                 base.RenderContents (writer);
234                         else
235                                 writer.Write (Text);
236                 }
237         }
238 }
239