2003-10-11 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 \r
12 using System;\r
13 using System.Drawing;\r
14 using System.Globalization;\r
15 using System.Web;\r
16 using System.Web.UI;\r
17 using System.ComponentModel;
18 using System.ComponentModel.Design;\r
19 \r
20 namespace System.Web.UI.WebControls\r
21 {\r
22         [DefaultProperty("Rows")]\r
23         [Designer ("System.Web.UI.Design.WebControls.TableDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]\r
24         [ParseChildren(true, "Rows")]\r
25         public class Table: WebControl\r
26         {\r
27                 private TableRowCollection rows;\r
28 \r
29                 private class TableRowControlCollection : ControlCollection\r
30                 {\r
31                         public TableRowControlCollection (Control owner) : base (owner)\r
32                         {\r
33                         }\r
34 \r
35                         public override void Add (Control child)\r
36                         {\r
37                                 if (!(child is TableRow))\r
38                                         throw new ArgumentException (HttpRuntime.FormatResourceString (\r
39                                                                         "Cannot_Have_Children_Of_Type",\r
40                                                                         "Table",\r
41                                                                         child.GetType ().Name.ToString ()));\r
42                                 base.Add (child);\r
43                         }\r
44 \r
45                         public override void AddAt(int index, Control child)\r
46                         {\r
47                                 if (!(child is TableRow))\r
48                                         throw new ArgumentException (HttpRuntime.FormatResourceString (\r
49                                                                         "Cannot_Have_Children_Of_Type",\r
50                                                                         "Table",\r
51                                                                         child.GetType ().Name.ToString ()));\r
52                                 base.AddAt (index, child);\r
53                         }\r
54                 }\r
55 \r
56                 public Table () : base (HtmlTextWriterTag.Table)\r
57                 {\r
58                 }\r
59
60                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
61                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
62                 [WebSysDescription ("An Url specifying the background image for the table.")]\r
63                 public virtual string BackImageUrl\r
64                 {\r
65                         get {\r
66                                 if (ControlStyleCreated)\r
67                                         return ((TableStyle) ControlStyle).BackImageUrl;\r
68                                 return String.Empty;\r
69                         }\r
70 \r
71                         set { ((TableStyle) ControlStyle).BackImageUrl = value; }\r
72                 }\r
73
74                 [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
75                 [WebSysDescription ("The space left around the borders within a cell.")]\r
76                 public virtual int CellPadding\r
77                 {\r
78                         get {\r
79                                 if (ControlStyleCreated)\r
80                                         return ((TableStyle) ControlStyle).CellPadding;\r
81                                 return -1;\r
82                         }\r
83 \r
84                         set { ((TableStyle) ControlStyle).CellPadding = value; }\r
85                 }\r
86
87                 [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
88                 [WebSysDescription ("The space left between cells.")]\r
89                 public virtual int CellSpacing\r
90                 {\r
91                         get {\r
92                                 if (ControlStyleCreated)\r
93                                         return ((TableStyle) ControlStyle).CellSpacing;\r
94                                 return -1;\r
95                         }\r
96 \r
97                         set { ((TableStyle) ControlStyle).CellSpacing = value; }\r
98                 }\r
99
100                 [DefaultValue (typeof (GridLines), "None"), Bindable (true), WebCategory ("Appearance")]
101                 [WebSysDescription ("The type of grid that a table uses.")]\r
102                 public virtual GridLines GridLines\r
103                 {\r
104                         get {\r
105                                 if (ControlStyleCreated)\r
106                                         return ((TableStyle) ControlStyle).GridLines;\r
107                                 return GridLines.None;\r
108                         }\r
109 \r
110                         set { ((TableStyle) ControlStyle).GridLines = value; }\r
111                 }\r
112
113                 [DefaultValue (typeof (HorizontalAlign), "NotSet"), Bindable (true), WebCategory ("Layout")]
114                 [WebSysDescription ("The horizonal alignment of the table.")]\r
115                 public virtual HorizontalAlign HorizontalAlign\r
116                 {\r
117                         get {\r
118                                 if (ControlStyleCreated)\r
119                                         return ((TableStyle) ControlStyle).HorizontalAlign;\r
120                                 return HorizontalAlign.NotSet;\r
121                         }\r
122 \r
123                         set { ((TableStyle) ControlStyle).HorizontalAlign = value; }\r
124                 }\r
125
126                 [MergableProperty (false), PersistenceMode (PersistenceMode.InnerDefaultProperty)]
127                 [WebSysDescription ("The rows of this table.")]\r
128                 public virtual TableRowCollection Rows\r
129                 {\r
130                         get {\r
131                                 if (rows == null)\r
132                                         rows = new TableRowCollection (this);\r
133                                 return rows;\r
134                         }\r
135                 }\r
136 \r
137                 protected override void AddAttributesToRender (HtmlTextWriter writer)\r
138                 {\r
139                         base.AddAttributesToRender (writer);\r
140                         if(!BorderColor.IsEmpty)\r
141                                 writer.AddAttribute (HtmlTextWriterAttribute.Bordercolor,\r
142                                                      ColorTranslator.ToHtml (BorderColor));\r
143 \r
144                         Unit bw = BorderWidth;\r
145                         if (GridLines == GridLines.None)\r
146                                 bw = Unit.Pixel (0);\r
147                         else if (bw.IsEmpty || bw.Type != UnitType.Pixel)\r
148                                 bw = Unit.Pixel(1);\r
149 \r
150                         writer.AddAttribute (HtmlTextWriterAttribute.Border,\r
151                                              ((int) bw.Value).ToString (NumberFormatInfo.InvariantInfo));\r
152                 }\r
153 \r
154                 protected override ControlCollection CreateControlCollection ()\r
155                 {\r
156                         return new TableRowControlCollection (this);\r
157                 }\r
158 \r
159                 protected override Style CreateControlStyle ()\r
160                 {\r
161                         return new TableStyle (ViewState);\r
162                 }\r
163 \r
164                 protected override void RenderContents (HtmlTextWriter writer)\r
165                 {\r
166                         foreach (TableRow current in Rows)\r
167                                  current.RenderControl (writer);\r
168                 }\r
169         }\r
170 }\r
171 \r