[eglib] Prefer <langinfo.h> to <localcharset.h>
[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         [SupportsEventValidation]
42         public class Table : WebControl, IPostBackEventHandler
43         {
44                 TableRowCollection rows;
45                 bool generateTableSections;
46
47                 public Table ()
48                         : base (HtmlTextWriterTag.Table)
49                 {
50                 }
51
52                 internal bool GenerateTableSections {
53                         get { return generateTableSections; }
54                         set { generateTableSections = value; }
55                 }
56                 
57                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
58                 [UrlProperty]
59                 [DefaultValue ("")]
60                 [WebSysDescription ("")]
61                 [WebCategory ("Appearance")]
62                 public virtual string BackImageUrl {
63                         get {
64                                 if (!ControlStyleCreated)
65                                         return String.Empty; // default value
66                                 return TableStyle.BackImageUrl;
67                         }
68                         set { TableStyle.BackImageUrl = value; }
69                 }
70
71                 // note: it seems that Caption and CaptionAlign appeared in 1.1 SP1
72
73                 [DefaultValue ("")]
74                 [Localizable (true)]
75                 [WebSysDescription ("")]
76                 [WebCategory ("Accessibility")]
77                 public virtual string Caption {
78                         get {
79                                 object o = ViewState ["Caption"];
80                                 return (o == null) ? String.Empty : (string) o;
81                         }
82                         set {
83                                 if (value == null)
84                                         ViewState.Remove ("Caption");
85                                 else
86                                         ViewState ["Caption"] = value;
87                         }
88                 }
89
90                 [DefaultValue (TableCaptionAlign.NotSet)]
91                 [WebCategory ("Accessibility")]
92                 public virtual TableCaptionAlign CaptionAlign {
93                         get {
94                                 object o = ViewState ["CaptionAlign"];
95                                 return (o == null) ? TableCaptionAlign.NotSet : (TableCaptionAlign) o;
96                         }
97                         set {
98                                 if ((value < TableCaptionAlign.NotSet) || (value > TableCaptionAlign.Right)) {
99                                         throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid TableCaptionAlign value."));
100                                 }
101                                 ViewState ["CaptionAlign"] = value;
102                         }
103                 }
104
105                 [DefaultValue (-1)]
106                 [WebSysDescription ("")]
107                 [WebCategory ("Appearance")]
108                 public virtual int CellPadding {
109                         get {
110                                 if (!ControlStyleCreated)
111                                         return -1; // default value
112                                 return TableStyle.CellPadding;
113                         }
114                         set { TableStyle.CellPadding = value; }
115                 }
116
117                 [DefaultValue (-1)]
118                 [WebSysDescription ("")]
119                 [WebCategory ("Appearance")]
120                 public virtual int CellSpacing {
121                         get {
122                                 if (!ControlStyleCreated)
123                                         return -1; // default value
124                                 return TableStyle.CellSpacing;
125                         }
126                         set { TableStyle.CellSpacing = value; }
127                 }
128
129                 [DefaultValue (GridLines.None)]
130                 [WebSysDescription ("")]
131                 [WebCategory ("Appearance")]
132                 public virtual GridLines GridLines {
133                         get {
134                                 if (!ControlStyleCreated)
135                                         return GridLines.None; // default value
136                                 return TableStyle.GridLines;
137                         }
138                         set { TableStyle.GridLines = value; }
139                 }
140
141                 [DefaultValue (HorizontalAlign.NotSet)]
142                 [WebSysDescription ("")]
143                 [WebCategory ("Layout")]
144                 public virtual HorizontalAlign HorizontalAlign {
145                         get {
146                                 if (!ControlStyleCreated)
147                                         return HorizontalAlign.NotSet; // default value
148                                 return TableStyle.HorizontalAlign;
149                         }
150                         set { TableStyle.HorizontalAlign = value; }
151                 }
152
153                 [MergableProperty (false)]
154                 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
155                 [WebSysDescription ("")]
156                 public virtual TableRowCollection Rows {
157                         get {
158                                 if (rows == null)
159                                         rows = new TableRowCollection (this);
160                                 return rows;
161                         }
162                 }
163
164                 private TableStyle TableStyle {
165                         get { return (ControlStyle as TableStyle); }
166                 }
167                 public override bool SupportsDisabledAttribute {
168                         get { return RenderingCompatibilityLessThan40; }
169                 }
170                 protected override void AddAttributesToRender (HtmlTextWriter writer)
171                 {
172                         base.AddAttributesToRender (writer);
173                 }
174
175                 protected override ControlCollection CreateControlCollection ()
176                 {
177                         return new RowControlCollection (this);
178                 }
179
180                 protected override Style CreateControlStyle ()
181                 {
182                         return new TableStyle (ViewState);
183                 }
184
185                 protected internal
186                 override void RenderContents (HtmlTextWriter writer)
187                 {
188                         TableRowSection currentTableSection = TableRowSection.TableHeader;
189                         TableRowSection rowSection;
190                         bool sectionStarted = false;
191                         
192                         if (Rows.Count > 0) {
193                                 foreach (TableRow row in Rows) {
194                                         if (generateTableSections) {
195                                                 rowSection = row.TableSection;
196                                                 if (rowSection < currentTableSection)
197                                                         throw new HttpException ("The table " + ID + " must contain row sections in order of header, body, then footer.");
198
199                                                 if (currentTableSection != rowSection) {
200                                                         if (sectionStarted) {
201                                                                 writer.RenderEndTag ();
202                                                                 sectionStarted = false;
203                                                         }
204                                                         
205                                                         currentTableSection = rowSection;
206                                                 }
207                                                 
208                                                 if (!sectionStarted) {
209                                                         switch (rowSection) {
210                                                                 case TableRowSection.TableHeader:
211                                                                         writer.RenderBeginTag (HtmlTextWriterTag.Thead);
212                                                                         break;
213
214                                                                 case TableRowSection.TableBody:
215                                                                         writer.RenderBeginTag (HtmlTextWriterTag.Tbody);
216                                                                         break;
217
218                                                                 case TableRowSection.TableFooter:
219                                                                         writer.RenderBeginTag (HtmlTextWriterTag.Tfoot);
220                                                                         break;
221                                                         }
222                                                         sectionStarted = true;
223                                                 }
224                                         }
225                                         if (row != null)
226                                                 row.RenderControl (writer);
227                                 }
228
229                                 if (sectionStarted)
230                                         writer.RenderEndTag ();
231                         }
232                 }
233
234
235                 // new in Fx 1.1 SP1 (to support Caption and CaptionAlign)
236
237                 public override void RenderBeginTag (HtmlTextWriter writer)
238                 {
239                         base.RenderBeginTag (writer);
240
241                         string s = Caption;
242                         if (s.Length > 0) {
243                                 TableCaptionAlign tca = CaptionAlign;
244                                 if (tca != TableCaptionAlign.NotSet)
245                                         writer.AddAttribute (HtmlTextWriterAttribute.Align, tca.ToString ());
246                                 
247                                 writer.RenderBeginTag (HtmlTextWriterTag.Caption);
248                                 writer.Write (s);
249                                 writer.RenderEndTag ();
250                         }
251 // #if !NET_4_0
252 //                      else if (HasControls ()) {
253 //                              writer.Indent++;
254 //                      }
255 // #endif
256                 }
257
258                 void IPostBackEventHandler.RaisePostBackEvent (string argument)
259                 {
260                         RaisePostBackEvent (argument);
261                 }
262
263                 protected virtual void RaisePostBackEvent (string argument)
264                 {
265                         ValidateEvent (UniqueID, argument);
266                 }
267
268                 // inner class
269                 protected class RowControlCollection : ControlCollection {
270
271                         internal RowControlCollection (Table owner)
272                                 : base (owner)
273                         {
274                         }
275
276
277                         public override void Add (Control child)
278                         {
279                                 if (child == null)
280                                         throw new NullReferenceException ("null");
281                                 if (!(child is TableRow))
282                                         throw new ArgumentException ("child", Locale.GetText ("Must be an TableRow instance."));
283
284                                 base.Add (child);
285                         }
286
287                         public override void AddAt (int index, Control child)
288                         {
289                                 if (child == null)
290                                         throw new NullReferenceException ("null");
291                                 if (!(child is TableRow))
292                                         throw new ArgumentException ("child", Locale.GetText ("Must be an TableRow instance."));
293
294                                 base.AddAt (index, child);
295                         }
296                 }
297         }
298 }