2007-07-03 Jonathan Chambers <joncham@gmail.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Table.cs
1 //
2 // System.Web.UI.WebControls.Table.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.Security.Permissions;
31
32 namespace System.Web.UI.WebControls {
33
34         // CAS
35         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         // attributes
38         [DefaultProperty ("Rows")]
39         [Designer ("System.Web.UI.Design.WebControls.TableDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
40         [ParseChildren (true, "Rows")]
41 #if NET_2_0
42         [SupportsEventValidation]
43         public class Table : WebControl, IPostBackEventHandler {
44 #else
45         public class Table : WebControl {
46 #endif
47                 private TableRowCollection rows;
48
49
50                 public Table ()
51                         : base (HtmlTextWriterTag.Table)
52                 {
53                 }
54
55
56 #if NET_2_0
57                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
58                 [UrlProperty]
59 #else
60                 [Bindable (true)]
61                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
62 #endif
63                 [DefaultValue ("")]
64                 [WebSysDescription ("")]
65                 [WebCategory ("Appearance")]
66                 public virtual string BackImageUrl {
67                         get {
68                                 if (!ControlStyleCreated)
69                                         return String.Empty; // default value
70                                 return TableStyle.BackImageUrl;
71                         }
72                         set { TableStyle.BackImageUrl = value; }
73                 }
74
75                 // note: it seems that Caption and CaptionAlign appeared in 1.1 SP1
76
77                 [DefaultValue ("")]
78 #if NET_2_0
79                 [Localizable (true)]
80 #endif
81                 [WebSysDescription ("")]
82                 [WebCategory ("Accessibility")]
83                 public virtual string Caption {
84                         get {
85                                 object o = ViewState ["Caption"];
86                                 return (o == null) ? String.Empty : (string) o;
87                         }
88                         set {
89                                 if (value == null)
90                                         ViewState.Remove ("Caption");
91                                 else
92                                         ViewState ["Caption"] = value;
93                         }
94                 }
95
96                 [DefaultValue (TableCaptionAlign.NotSet)]
97                 [WebCategory ("Accessibility")]
98                 public virtual TableCaptionAlign CaptionAlign {
99                         get {
100                                 object o = ViewState ["CaptionAlign"];
101                                 return (o == null) ? TableCaptionAlign.NotSet : (TableCaptionAlign) o;
102                         }
103                         set {
104                                 if ((value < TableCaptionAlign.NotSet) || (value > TableCaptionAlign.Right)) {
105                                         throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid TableCaptionAlign value."));
106                                 }
107                                 ViewState ["CaptionAlign"] = value;
108                         }
109                 }
110
111 #if ONLY_1_1
112                 [Bindable (true)]
113 #endif
114                 [DefaultValue (-1)]
115                 [WebSysDescription ("")]
116                 [WebCategory ("Appearance")]
117                 public virtual int CellPadding {
118                         get {
119                                 if (!ControlStyleCreated)
120                                         return -1; // default value
121                                 return TableStyle.CellPadding;
122                         }
123                         set { TableStyle.CellPadding = value; }
124                 }
125
126 #if ONLY_1_1
127                 [Bindable (true)]
128 #endif
129                 [DefaultValue (-1)]
130                 [WebSysDescription ("")]
131                 [WebCategory ("Appearance")]
132                 public virtual int CellSpacing {
133                         get {
134                                 if (!ControlStyleCreated)
135                                         return -1; // default value
136                                 return TableStyle.CellSpacing;
137                         }
138                         set { TableStyle.CellSpacing = value; }
139                 }
140
141 #if ONLY_1_1
142                 [Bindable (true)]
143 #endif
144                 [DefaultValue (GridLines.None)]
145                 [WebSysDescription ("")]
146                 [WebCategory ("Appearance")]
147                 public virtual GridLines GridLines {
148                         get {
149                                 if (!ControlStyleCreated)
150                                         return GridLines.None; // default value
151                                 return TableStyle.GridLines;
152                         }
153                         set { TableStyle.GridLines = value; }
154                 }
155
156 #if ONLY_1_1
157                 [Bindable (true)]
158 #endif
159                 [DefaultValue (HorizontalAlign.NotSet)]
160                 [WebSysDescription ("")]
161                 [WebCategory ("Layout")]
162                 public virtual HorizontalAlign HorizontalAlign {
163                         get {
164                                 if (!ControlStyleCreated)
165                                         return HorizontalAlign.NotSet; // default value
166                                 return TableStyle.HorizontalAlign;
167                         }
168                         set { TableStyle.HorizontalAlign = value; }
169                 }
170
171                 [MergableProperty (false)]
172                 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
173                 [WebSysDescription ("")]
174                 public virtual TableRowCollection Rows {
175                         get {
176                                 if (rows == null)
177                                         rows = new TableRowCollection (this);
178                                 return rows;
179                         }
180                 }
181
182                 private TableStyle TableStyle {
183                         get { return (ControlStyle as TableStyle); }
184                 }
185
186
187                 protected override void AddAttributesToRender (HtmlTextWriter writer)
188                 {
189                         base.AddAttributesToRender (writer);
190                         if (!ControlStyleCreated || TableStyle.IsEmpty) {
191                                 // for some reason border=X seems to be always present
192                                 // and isn't rendered as a style attribute
193                                 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
194                         }
195                 }
196
197                 protected override ControlCollection CreateControlCollection ()
198                 {
199                         return new RowControlCollection (this);
200                 }
201
202                 protected override Style CreateControlStyle ()
203                 {
204                         return new TableStyle (ViewState);
205                 }
206
207 #if NET_2_0
208                 protected internal
209 #else           
210                 protected
211 #endif          
212                 override void RenderContents (HtmlTextWriter writer)
213                 {
214                         if (Rows.Count > 0) {
215                                 writer.Indent++;
216                                 foreach (TableRow row in Rows)
217                                         if (row != null)
218                                                 row.RenderControl (writer);
219                                 writer.Indent--;
220                         }
221                 }
222
223
224                 // new in Fx 1.1 SP1 (to support Caption and CaptionAlign)
225
226                 public override void RenderBeginTag (HtmlTextWriter writer)
227                 {
228                         base.RenderBeginTag (writer);
229
230                         string s = Caption;
231                         if (s.Length > 0) {
232                                 writer.WriteBeginTag ("caption");
233
234                                 TableCaptionAlign tca = CaptionAlign;
235                                 if (tca != TableCaptionAlign.NotSet) {
236                                         writer.WriteAttribute ("align", tca.ToString ());
237                                 }
238
239                                 writer.Write (HtmlTextWriter.TagRightChar);
240                                 writer.Indent++;
241                                 writer.WriteLine ();
242                                 writer.Write (s);
243                                 writer.Indent--;
244                                 writer.WriteLine ();
245                                 writer.WriteEndTag ("caption");
246                         } else if (HasControls ()) {
247                                 writer.Indent++;
248                         }
249                 }
250
251 #if NET_2_0
252                 void IPostBackEventHandler.RaisePostBackEvent (string argument)
253                 {
254                         RaisePostBackEvent (argument);
255                 }
256
257                 protected virtual void RaisePostBackEvent (string argument)
258                 {
259                 }
260 #endif
261
262                 // inner class
263                 protected class RowControlCollection : ControlCollection {
264
265                         internal RowControlCollection (Table owner)
266                                 : base (owner)
267                         {
268                         }
269
270
271                         public override void Add (Control child)
272                         {
273                                 if (child == null)
274                                         throw new NullReferenceException ("null");
275                                 if (!(child is TableRow))
276                                         throw new ArgumentException ("child", Locale.GetText ("Must be an TableRow instance."));
277
278                                 base.Add (child);
279                         }
280
281                         public override void AddAt (int index, Control child)
282                         {
283                                 if (child == null)
284                                         throw new NullReferenceException ("null");
285                                 if (!(child is TableRow))
286                                         throw new ArgumentException ("child", Locale.GetText ("Must be an TableRow instance."));
287
288                                 base.AddAt (index, child);
289                         }
290                 }
291         }
292 }