2004-06-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TableStyle.cs
1 //
2 // System.Web.UI.WebControls.TableStyle.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 //\r
11 \r
12 using System;
13 using System.ComponentModel;\r
14 using System.Globalization;\r
15 using System.Web;\r
16 using System.Web.UI;\r
17 \r
18 namespace System.Web.UI.WebControls\r
19 {\r
20         public class TableStyle : Style\r
21         {\r
22                 private static int IMAGE_URL = (0x01 << 16);\r
23                 private static int CELL_PADD = (0x01 << 17);\r
24                 private static int CELL_SPAC = (0x01 << 18);\r
25                 private static int GRID_LINE = (0x01 << 19);\r
26                 private static int HOR_ALIGN = (0x01 << 20);\r
27 \r
28                 public TableStyle(): base()\r
29                 {\r
30                 }\r
31 \r
32                 public TableStyle(StateBag bag): base(bag)\r
33                 {\r
34                 }\r
35
36                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
37                 [WebSysDescription ("An Url specifying the background image for the table.")]\r
38                 public virtual string BackImageUrl\r
39                 {\r
40                         get\r
41                         {\r
42                                 if(IsSet(IMAGE_URL))\r
43                                         return (string)(ViewState["BackImageUrl"]);\r
44                                 return String.Empty;\r
45                         }\r
46                         set\r
47                         {\r
48                                 if(value == null)\r
49                                         throw new ArgumentNullException("value");\r
50                                 ViewState["BackImageUrl"] = value;\r
51                                 Set(IMAGE_URL);\r
52                         }\r
53                 }\r
54
55                 [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
56                 [WebSysDescription ("The space left around the borders within a cell.")]\r
57                 public virtual int CellPadding\r
58                 {\r
59                         get\r
60                         {\r
61                                 if(IsSet(CELL_PADD))\r
62                                         return (int)(ViewState["CellPadding"]);\r
63                                 return -1;\r
64                         }\r
65                         set\r
66                         {\r
67                                 if(value < -1)\r
68                                         throw new ArgumentOutOfRangeException("value", "CellPadding value has to be -1 for 'not set' or a value >= 0");\r
69                                 ViewState["CellPadding"] = value;\r
70                                 Set(CELL_PADD);\r
71                         }\r
72                 }\r
73
74                 [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
75                 [WebSysDescription ("The space left between cells.")]\r
76                 public virtual int CellSpacing\r
77                 {\r
78                         get\r
79                         {\r
80                                 if(IsSet(CELL_SPAC))\r
81                                         return (int)(ViewState["CellSpacing"]);\r
82                                 return -1;\r
83                         }\r
84                         set\r
85                         {\r
86                                 if(value < -1)\r
87                                         throw new ArgumentOutOfRangeException("value"," CellSpacing value has to be -1 for 'not set' or a value >= 0");\r
88                                 ViewState["CellSpacing"] = value;\r
89                                 Set(CELL_SPAC);\r
90                         }\r
91                 }\r
92
93                 [DefaultValue (typeof (GridLines), "None"), Bindable (true), WebCategory ("Appearance")]
94                 [WebSysDescription ("The type of grid that a table uses.")]\r
95                 public virtual GridLines GridLines\r
96                 {\r
97                         get\r
98                         {\r
99                                 if(IsSet(GRID_LINE))\r
100                                         return (GridLines)(ViewState["GridLines"]);\r
101                                 return GridLines.None;\r
102                         }\r
103                         set\r
104                         {\r
105                                 if(!Enum.IsDefined(typeof(GridLines), value))\r
106                                         throw new ArgumentOutOfRangeException("value"," Gridlines value has to be a valid enumeration member");\r
107                                 ViewState["GridLines"] = value;\r
108                                 Set(GRID_LINE);\r
109                         }\r
110                 }\r
111
112                 [DefaultValue (typeof (HorizontalAlign), "NotSet"), Bindable (true), WebCategory ("Layout")]
113                 [WebSysDescription ("The horizonal alignment of the table.")]\r
114                 public virtual HorizontalAlign HorizontalAlign\r
115                 {\r
116                         get\r
117                         {\r
118                                 if(IsSet(HOR_ALIGN))\r
119                                         return (HorizontalAlign)(ViewState["HorizontalAlign"]);\r
120                                 return HorizontalAlign.NotSet;\r
121                         }\r
122                         set\r
123                         {\r
124                                 if(!Enum.IsDefined(typeof(HorizontalAlign), value))\r
125                                         throw new ArgumentOutOfRangeException("value"," Gridlines value has to be a valid enumeration member");\r
126                                 ViewState["HorizontalAlign"] = value;\r
127                                 Set(HOR_ALIGN);\r
128                         }\r
129                 }\r
130 \r
131                 public override void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)\r
132                 {\r
133                         base.AddAttributesToRender(writer, owner);\r
134                         if(BackImageUrl.Length > 0)\r
135                         {\r
136                                 writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, "url(" + owner.ResolveUrl(BackImageUrl) + ")");\r
137                         }\r
138                         if(CellSpacing >= 0)\r
139                         {\r
140                                 writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, CellSpacing.ToString(NumberFormatInfo.InvariantInfo));\r
141                                 if(CellSpacing == 0)
142                                         writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
143                                 
144                         }\r
145                         if(CellPadding >= 0)\r
146                         {\r
147                                 writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, CellPadding.ToString(NumberFormatInfo.InvariantInfo));\r
148                         }\r
149                         if(HorizontalAlign != HorizontalAlign.NotSet)\r
150                         {\r
151                                 writer.AddAttribute(HtmlTextWriterAttribute.Align, Enum.Format(typeof(HorizontalAlign), HorizontalAlign, "G"));\r
152                         }\r
153                         string gd = String.Empty;
154                         switch(GridLines)\r
155                         {\r
156                                 case GridLines.None:       break;
157                                 case GridLines.Horizontal: gd = "rows";\r
158                                                            break;\r
159                                 case GridLines.Vertical:   gd = "cols";\r
160                                                            break;\r
161                                 case GridLines.Both:       gd = "all";\r
162                                                            break;\r
163                         }
164                                 writer.AddAttribute(HtmlTextWriterAttribute.Rules, gd);
165                 }
166
167                 public override void CopyFrom(Style s)
168                 {
169                         if (s == null || s.IsEmpty)
170                                 return;
171
172                         base.CopyFrom (s);
173                         TableStyle from = s as TableStyle;
174                         if (from == null)
175                                 return;
176
177                         if (from.IsSet (HOR_ALIGN))
178                                 HorizontalAlign = from.HorizontalAlign;
179
180                         if (from.IsSet (IMAGE_URL))
181                                 BackImageUrl = from.BackImageUrl;
182
183                         if (from.IsSet (CELL_PADD))
184                                 CellPadding = from.CellPadding;
185
186                         if (from.IsSet (CELL_SPAC))
187                                 CellSpacing = from.CellSpacing;
188
189                         if (from.IsSet (GRID_LINE))
190                                 GridLines = from.GridLines;
191                 }
192
193                 public override void MergeWith(Style s)\r
194                 {\r
195                         if(s != null && !s.IsEmpty)\r
196                         {\r
197                                 if (IsEmpty) {
198                                         CopyFrom (s);
199                                         return;
200                                 }
201                                 base.MergeWith(s);\r
202
203                                 if (!(s is TableStyle))
204                                         return;
205                                 
206                                 TableStyle with = (TableStyle)s;\r
207                                 if(with.IsSet(HOR_ALIGN) && !IsSet(HOR_ALIGN))\r
208                                 {\r
209                                         HorizontalAlign = with.HorizontalAlign;\r
210                                 }\r
211                                 if(with.IsSet(IMAGE_URL) && !IsSet(IMAGE_URL))\r
212                                 {\r
213                                         BackImageUrl = with.BackImageUrl;\r
214                                 }\r
215                                 if(with.IsSet(CELL_PADD) && !IsSet(CELL_PADD))\r
216                                 {\r
217                                         CellPadding = with.CellPadding;\r
218                                 }\r
219                                 if(with.IsSet(CELL_SPAC) && !IsSet(CELL_SPAC))\r
220                                 {\r
221                                         CellSpacing = with.CellSpacing;\r
222                                 }\r
223                                 if(with.IsSet(GRID_LINE) && !IsSet(GRID_LINE))\r
224                                 {\r
225                                         GridLines = with.GridLines;\r
226                                 }\r
227                         }\r
228                 }\r
229 \r
230                 public override void Reset()\r
231                 {\r
232                         if(IsSet(IMAGE_URL))\r
233                                 ViewState.Remove("BackImageUrl");\r
234                         if(IsSet(HOR_ALIGN))\r
235                                 ViewState.Remove("HorizontalAlign");\r
236                         if(IsSet(CELL_PADD))\r
237                                 ViewState.Remove("CellPadding");\r
238                         if(IsSet(CELL_SPAC))\r
239                                 ViewState.Remove("CellSpacing");\r
240                         if(IsSet(GRID_LINE))\r
241                                 ViewState.Remove("GridLines");\r
242                         base.Reset();\r
243                 }\r
244         }\r
245 }\r