2004-09-12 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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
81                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
82                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
83                 [WebSysDescription ("An Url specifying the background image for the table.")]\r
84                 public virtual string BackImageUrl\r
85                 {\r
86                         get {\r
87                                 if (ControlStyleCreated)\r
88                                         return ((TableStyle) ControlStyle).BackImageUrl;\r
89                                 return String.Empty;\r
90                         }\r
91 \r
92                         set { ((TableStyle) ControlStyle).BackImageUrl = value; }\r
93                 }\r
94
95                 [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
96                 [WebSysDescription ("The space left around the borders within a cell.")]\r
97                 public virtual int CellPadding\r
98                 {\r
99                         get {\r
100                                 if (ControlStyleCreated)\r
101                                         return ((TableStyle) ControlStyle).CellPadding;\r
102                                 return -1;\r
103                         }\r
104 \r
105                         set { ((TableStyle) ControlStyle).CellPadding = value; }\r
106                 }\r
107
108                 [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
109                 [WebSysDescription ("The space left between cells.")]\r
110                 public virtual int CellSpacing\r
111                 {\r
112                         get {\r
113                                 if (ControlStyleCreated)\r
114                                         return ((TableStyle) ControlStyle).CellSpacing;\r
115                                 return -1;\r
116                         }\r
117 \r
118                         set { ((TableStyle) ControlStyle).CellSpacing = value; }\r
119                 }\r
120
121                 [DefaultValue (typeof (GridLines), "None"), Bindable (true), WebCategory ("Appearance")]
122                 [WebSysDescription ("The type of grid that a table uses.")]\r
123                 public virtual GridLines GridLines\r
124                 {\r
125                         get {\r
126                                 if (ControlStyleCreated)\r
127                                         return ((TableStyle) ControlStyle).GridLines;\r
128                                 return GridLines.None;\r
129                         }\r
130 \r
131                         set { ((TableStyle) ControlStyle).GridLines = value; }\r
132                 }\r
133
134                 [DefaultValue (typeof (HorizontalAlign), "NotSet"), Bindable (true), WebCategory ("Layout")]
135                 [WebSysDescription ("The horizonal alignment of the table.")]\r
136                 public virtual HorizontalAlign HorizontalAlign\r
137                 {\r
138                         get {\r
139                                 if (ControlStyleCreated)\r
140                                         return ((TableStyle) ControlStyle).HorizontalAlign;\r
141                                 return HorizontalAlign.NotSet;\r
142                         }\r
143 \r
144                         set { ((TableStyle) ControlStyle).HorizontalAlign = value; }\r
145                 }\r
146
147                 [MergableProperty (false), PersistenceMode (PersistenceMode.InnerDefaultProperty)]
148                 [WebSysDescription ("The rows of this table.")]\r
149                 public virtual TableRowCollection Rows\r
150                 {\r
151                         get {\r
152                                 if (rows == null)\r
153                                         rows = new TableRowCollection (this);\r
154                                 return rows;\r
155                         }\r
156                 }\r
157 \r
158                 protected override void AddAttributesToRender (HtmlTextWriter writer)\r
159                 {\r
160                         base.AddAttributesToRender (writer);\r
161                         if(!BorderColor.IsEmpty)\r
162                                 writer.AddAttribute (HtmlTextWriterAttribute.Bordercolor,\r
163                                                      ColorTranslator.ToHtml (BorderColor));\r
164 \r
165                         Unit bw = BorderWidth;\r
166                         if (GridLines == GridLines.None)\r
167                                 bw = Unit.Pixel (0);\r
168                         else if (bw.IsEmpty || bw.Type != UnitType.Pixel)\r
169                                 bw = Unit.Pixel(1);\r
170 \r
171                         writer.AddAttribute (HtmlTextWriterAttribute.Border,\r
172                                              ((int) bw.Value).ToString (NumberFormatInfo.InvariantInfo));\r
173                 }\r
174 \r
175                 protected override ControlCollection CreateControlCollection ()\r
176                 {\r
177                         return new TableRowControlCollection (this);\r
178                 }\r
179 \r
180                 protected override Style CreateControlStyle ()\r
181                 {\r
182                         return new TableStyle (ViewState);\r
183                 }\r
184 \r
185                 protected override void RenderContents (HtmlTextWriter writer)\r
186                 {\r
187                         foreach (TableRow current in Rows)\r
188                                  current.RenderControl (writer);\r
189                 }\r
190         }\r
191 }\r
192 \r