2002-02-06 Gaurav Vaish <gvaish@iitk.ac.in>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / TableStyle.cs
1 /**\r
2  * Namespace: System.Web.UI.WebControls\r
3  * Class:     TableStyle\r
4  * \r
5  * Author:  Gaurav Vaish\r
6  * Maintainer: gvaish@iitk.ac.in\r
7  * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>\r
8  * Implementation: yes\r
9  * Status:  100%\r
10  * \r
11  * (C) Gaurav Vaish (2002)\r
12  */\r
13 \r
14 using System;\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                 \r
36                 public virtual string BackImageUrl\r
37                 {\r
38                         get\r
39                         {\r
40                                 if(IsSet(IMAGE_URL))\r
41                                         return (string)(ViewState["BackImageUrl"]);\r
42                                 return String.Empty;\r
43                         }\r
44                         set\r
45                         {\r
46                                 if(value == null)\r
47                                         throw new ArgumentNullException("BackImageUrl");\r
48                                 ViewState["BackImageUrl"] = value;\r
49                                 Set(IMAGE_URL);\r
50                         }\r
51                 }\r
52                 \r
53                 public virtual int CellPadding\r
54                 {\r
55                         get\r
56                         {\r
57                                 if(IsSet(CELL_PADD))\r
58                                         return (int)(ViewState["CellPadding"]);\r
59                                 return -1;\r
60                         }\r
61                         set\r
62                         {\r
63                                 if(value < -1)\r
64                                         throw new ArgumentOutOfRangeException("CellPadding");\r
65                                 ViewState["CellPadding"] = value;\r
66                                 Set(CELL_PADD);\r
67                         }\r
68                 }\r
69                 \r
70                 public virtual int CellSpacing\r
71                 {\r
72                         get\r
73                         {\r
74                                 if(IsSet(CELL_SPAC))\r
75                                         return (int)(ViewState["CellSpacing"]);\r
76                                 return -1;\r
77                         }\r
78                         set\r
79                         {\r
80                                 if(value < -1)\r
81                                         throw new ArgumentOutOfRangeException("CellSpacing");\r
82                                 ViewState["CellSpacing"] = value;\r
83                                 Set(CELL_SPAC);\r
84                         }\r
85                 }\r
86                 \r
87                 public virtual GridLines GridLines\r
88                 {\r
89                         get\r
90                         {\r
91                                 if(IsSet(GRID_LINE))\r
92                                         return (string)(ViewState["GridLines"]);\r
93                                 return GridLines.Both;\r
94                         }\r
95                         set\r
96                         {\r
97                                 if(!Enum.IsDefined(typeof(GridLines), value))\r
98                                         throw new ArgumentException();\r
99                                 ViewState["GridLines"] = value;\r
100                                 Set(GRID_LINE);\r
101                         }\r
102                 }\r
103                 \r
104                 public virtual HorizontalAlign HorizontalAlign\r
105                 {\r
106                         get\r
107                         {\r
108                                 if(IsSet(HOR_ALIGN))\r
109                                         return (string)(ViewState["HorizontalAlign"]);\r
110                                 return HorizontalAlign.NotSet;\r
111                         }\r
112                         set\r
113                         {\r
114                                 if(!Enum.IsDefined(typeof(HorizontalAlign), value))\r
115                                         throw new ArgumentException();\r
116                                 ViewState["HorizontalAlign"] = value;\r
117                                 Set(HOR_ALIGN);\r
118                         }\r
119                 }\r
120                 \r
121                 public override void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)\r
122                 {\r
123                         base.AddAttributesToRender(writer, owner);\r
124                         if(BackImageUrl.Length > 0)\r
125                         {\r
126                                 writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, "url(" + ResolveUrl(BackImageUrl) + ")");\r
127                         }\r
128                         if(CellSpacing >= 0)\r
129                         {\r
130                                 writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, CellSpacing.ToString(NumerFormatInfo.InvariantInfo));\r
131                         }\r
132                         if(CellPadding >= 0)\r
133                         {\r
134                                 writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, CellPadding.ToString(NumerFormatInfo.InvariantInfo));\r
135                         }\r
136                         if(HorizontalAlign != HorizontalAlign.NotSet)\r
137                         {\r
138                                 writer.AddAttribute(HtmlTextWriterAttribute.Align, Enum.Format(typeof(HorizontalAlign), HorizontalAlign, "G"));\r
139                         }\r
140                         string gd = "";\r
141                         switch(GridLines)\r
142                         {\r
143                                 case GridLines.None:       gd = "";\r
144                                                            break;\r
145                                 case GridLines.Horizontal: gd = "cols";\r
146                                                            break;\r
147                                 case GridLines.Vertical:   gd = "rows";\r
148                                                            break;\r
149                                 case GridLines.Both:       gd = "all";\r
150                                                            break;\r
151                         }\r
152                         writer.AddAttribute(HtmlTextWriterAttribute.Rules, gd);\r
153                 }\r
154                 \r
155                 private static int IMAGE_URL = (0x01 << 16);\r
156                 private static int CELL_PADD = (0x01 << 17);\r
157                 private static int CELL_SPAC = (0x01 << 18);\r
158                 private static int GRID_LINE = (0x01 << 19);\r
159                 private static int HOR_ALIGN = (0x01 << 20);\r
160                 \r
161                 public override void CopyFrom(Style s)\r
162                 {\r
163                         if(s != null && s is TableStyle && !s.IsEmpty)\r
164                         {\r
165                                 base.CopyFrom(s);\r
166                                 TableStyle from = (TableStyle)s;\r
167                                 if(from.IsSet(HOR_ALIGN))\r
168                                 {\r
169                                         HorizontalAlign = from.HorizontalAlign;\r
170                                 }\r
171                                 if(from.IsSet(IMAGE_URL))\r
172                                 {\r
173                                         BackImageUrl = from.BackImageUrl;\r
174                                 }\r
175                                 if(from.IsSet(CELL_PADD))\r
176                                 {\r
177                                         CellPadding = from.CellPadding;\r
178                                 }\r
179                                 if(from.IsSet(CELL_SPAC))\r
180                                 {\r
181                                         CellSpacing = from.CellSpacing;\r
182                                 }\r
183                                 if(from.IsSet(GRID_LINE))\r
184                                 {\r
185                                         GridLines = from.GridLines;\r
186                                 }\r
187                         }\r
188                 }\r
189                 \r
190                 public override void MergeWith(Style s)\r
191                 {\r
192                         if(s != null && s is TableStyle && !s.IsEmpty)\r
193                         {\r
194                                 base.MergeWith(s);\r
195                                 TableStyle with = (TableStyle)s;\r
196                                 if(from.IsSet(HOR_ALIGN) && IsSet(HOR_ALIGN))\r
197                                 {\r
198                                         HorizontalAlign = with.HorizontalAlign;\r
199                                 }\r
200                                 if(from.IsSet(IMAGE_URL) && IsSet(IMAGE_URL))\r
201                                 {\r
202                                         BackImageUrl = with.BackImageUrl;\r
203                                 }\r
204                                 if(from.IsSet(CELL_PADD) && IsSet(CELLL_PADD))\r
205                                 {\r
206                                         CellPadding = with.CellPadding;\r
207                                 }\r
208                                 if(from.IsSet(CELL_SPAC) && IsSet(CELL_SPAC))\r
209                                 {\r
210                                         CellSpacing = with.CellSpacing;\r
211                                 }\r
212                                 if(from.IsSet(GRID_LINE) && IsSet(GRID_LINE))\r
213                                 {\r
214                                         GridLines = with.GridLines;\r
215                                 }\r
216                         }\r
217                 }\r
218                 \r
219                 public override void Reset()\r
220                 {\r
221                         if(IsSet(IMAGE_URL))\r
222                                 ViewState.Remove("BackImageUrl");\r
223                         if(IsSet(HOR_ALIGN))\r
224                                 ViewState.Remove("HorizontalAlign");\r
225                         if(IsSet(CELL_PADD))\r
226                                 ViewState.Remove("CellPadding");\r
227                         if(IsSet(CELL_SPAC))\r
228                                 ViewState.Remove("CellSpacing");\r
229                         if(IsSet(GRID_LINE))\r
230                                 ViewState.Remove("GridLines");\r
231                         base.Reset();\r
232                 }\r
233         }\r
234 }\r