2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Table.cs
1 //
2 // System.Web.UI.WebControls.Table.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 \r
33 using System;\r
34 using System.Drawing;\r
35 using System.Globalization;\r
36 using System.Web;\r
37 using System.Web.UI;\r
38 using System.ComponentModel;
39 using System.ComponentModel.Design;\r
40 \r
41 namespace System.Web.UI.WebControls\r
42 {\r
43         [DefaultProperty("Rows")]\r
44         [Designer ("System.Web.UI.Design.WebControls.TableDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]\r
45         [ParseChildren(true, "Rows")]\r
46         public class Table: WebControl\r
47         {\r
48                 private TableRowCollection rows;\r
49 \r
50                 private class TableRowControlCollection : ControlCollection\r
51                 {\r
52                         public TableRowControlCollection (Control owner) : base (owner)\r
53                         {\r
54                         }\r
55 \r
56                         public override void Add (Control child)\r
57                         {\r
58                                 if (!(child is TableRow))\r
59                                         throw new ArgumentException (HttpRuntime.FormatResourceString (\r
60                                                                         "Cannot_Have_Children_Of_Type",\r
61                                                                         "Table",\r
62                                                                         child.GetType ().Name.ToString ()));\r
63                                 base.Add (child);\r
64                         }\r
65 \r
66                         public override void AddAt(int index, Control child)\r
67                         {\r
68                                 if (!(child is TableRow))\r
69                                         throw new ArgumentException (HttpRuntime.FormatResourceString (\r
70                                                                         "Cannot_Have_Children_Of_Type",\r
71                                                                         "Table",\r
72                                                                         child.GetType ().Name.ToString ()));\r
73                                 base.AddAt (index, child);\r
74                         }\r
75                 }\r
76 \r
77                 public Table () : base (HtmlTextWriterTag.Table)\r
78                 {\r
79                 }\r
80 \r
81 #if !NET_2_0\r
82                 [Bindable (true)]\r
83 #else\r
84                 [UrlProperty]\r
85 #endif
86                 [DefaultValue (""), WebCategory ("Appearance")]
87                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
88                 [WebSysDescription ("An Url specifying the background image for the table.")]\r
89                 public virtual string BackImageUrl\r
90                 {\r
91                         get {\r
92                                 if (ControlStyleCreated)\r
93                                         return ((TableStyle) ControlStyle).BackImageUrl;\r
94                                 return String.Empty;\r
95                         }\r
96 \r
97                         set { ((TableStyle) ControlStyle).BackImageUrl = value; }\r
98                 }\r
99
100 #if NET_2_0\r
101                 [DefaultValue (""), WebCategory ("Accessibility")]\r
102                 public virtual string Caption\r
103                 {\r
104                         get {\r
105                                 object o = ViewState ["Caption"];\r
106                                 if(o != null) return (string)o;\r
107                                 return "";\r
108                         }\r
109                         set {\r
110                                 ViewState ["Caption"] = value;\r
111                         }\r
112                 }\r
113 \r
114                 [DefaultValue (""), WebCategory ("Accessibility")]\r
115                 public virtual TableCaptionAlign CaptionAlign\r
116                 {\r
117                         get {\r
118                                 object o = ViewState ["CaptionAlign"];\r
119                                 if(o != null) return (TableCaptionAlign) o;\r
120                                 return TableCaptionAlign.NotSet;\r
121                         }\r
122                         set {\r
123                                 ViewState ["CaptionAlign"] = value;\r
124                         }\r
125                 }\r
126 #endif\r
127 \r
128 #if !NET_2_0\r
129                 [Bindable (true)]\r
130 #endif\r
131                 [DefaultValue (-1), WebCategory ("Appearance")]
132                 [WebSysDescription ("The space left around the borders within a cell.")]\r
133                 public virtual int CellPadding\r
134                 {\r
135                         get {\r
136                                 if (ControlStyleCreated)\r
137                                         return ((TableStyle) ControlStyle).CellPadding;\r
138                                 return -1;\r
139                         }\r
140 \r
141                         set { ((TableStyle) ControlStyle).CellPadding = value; }\r
142                 }\r
143
144 #if !NET_2_0\r
145                 [Bindable (true)]\r
146 #endif\r
147                 [DefaultValue (-1), WebCategory ("Appearance")]
148                 [WebSysDescription ("The space left between cells.")]\r
149                 public virtual int CellSpacing\r
150                 {\r
151                         get {\r
152                                 if (ControlStyleCreated)\r
153                                         return ((TableStyle) ControlStyle).CellSpacing;\r
154                                 return -1;\r
155                         }\r
156 \r
157                         set { ((TableStyle) ControlStyle).CellSpacing = value; }\r
158                 }\r
159
160 #if !NET_2_0\r
161                 [Bindable (true)]\r
162 #endif\r
163                 [DefaultValue (typeof (GridLines), "None"), WebCategory ("Appearance")]
164                 [WebSysDescription ("The type of grid that a table uses.")]\r
165                 public virtual GridLines GridLines\r
166                 {\r
167                         get {\r
168                                 if (ControlStyleCreated)\r
169                                         return ((TableStyle) ControlStyle).GridLines;\r
170                                 return GridLines.None;\r
171                         }\r
172 \r
173                         set { ((TableStyle) ControlStyle).GridLines = value; }\r
174                 }\r
175
176 #if !NET_2_0\r
177                 [Bindable (true)]\r
178 #endif\r
179                 [DefaultValue (typeof (HorizontalAlign), "NotSet"), WebCategory ("Layout")]
180                 [WebSysDescription ("The horizonal alignment of the table.")]\r
181                 public virtual HorizontalAlign HorizontalAlign\r
182                 {\r
183                         get {\r
184                                 if (ControlStyleCreated)\r
185                                         return ((TableStyle) ControlStyle).HorizontalAlign;\r
186                                 return HorizontalAlign.NotSet;\r
187                         }\r
188 \r
189                         set { ((TableStyle) ControlStyle).HorizontalAlign = value; }\r
190                 }\r
191
192                 [MergableProperty (false), PersistenceMode (PersistenceMode.InnerDefaultProperty)]
193                 [WebSysDescription ("The rows of this table.")]\r
194                 public virtual TableRowCollection Rows\r
195                 {\r
196                         get {\r
197                                 if (rows == null)\r
198                                         rows = new TableRowCollection (this);\r
199                                 return rows;\r
200                         }\r
201                 }\r
202 \r
203                 protected override void AddAttributesToRender (HtmlTextWriter writer)\r
204                 {\r
205                         base.AddAttributesToRender (writer);\r
206                         if(!BorderColor.IsEmpty)\r
207                                 writer.AddAttribute (HtmlTextWriterAttribute.Bordercolor,\r
208                                                      ColorTranslator.ToHtml (BorderColor));\r
209 \r
210                         Unit bw = BorderWidth;\r
211                         if (GridLines == GridLines.None)\r
212                                 bw = Unit.Pixel (0);\r
213                         else if (bw.IsEmpty || bw.Type != UnitType.Pixel)\r
214                                 bw = Unit.Pixel(1);\r
215 \r
216                         writer.AddAttribute (HtmlTextWriterAttribute.Border,\r
217                                              ((int) bw.Value).ToString (NumberFormatInfo.InvariantInfo));\r
218                 }\r
219 \r
220                 protected override ControlCollection CreateControlCollection ()\r
221                 {\r
222                         return new TableRowControlCollection (this);\r
223                 }\r
224 \r
225                 protected override Style CreateControlStyle ()\r
226                 {\r
227                         return new TableStyle (ViewState);\r
228                 }\r
229 \r
230                 protected override void RenderContents (HtmlTextWriter writer)\r
231                 {\r
232                         foreach (TableRow current in Rows)\r
233                                  current.RenderControl (writer);\r
234                 }\r
235         }\r
236 }\r
237 \r