**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / TableStyle.cs
bloba1653a11928f558c0b48c0cdf60c732b31be625c
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
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.
33 using System;
34 using System.ComponentModel;
35 using System.Globalization;
36 using System.Web;
37 using System.Web.UI;
39 namespace System.Web.UI.WebControls
41 public class TableStyle : Style
43 private static int IMAGE_URL = (0x01 << 16);
44 private static int CELL_PADD = (0x01 << 17);
45 private static int CELL_SPAC = (0x01 << 18);
46 private static int GRID_LINE = (0x01 << 19);
47 private static int HOR_ALIGN = (0x01 << 20);
49 public TableStyle(): base()
53 public TableStyle(StateBag bag): base(bag)
57 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
58 [WebSysDescription ("An Url specifying the background image for the table.")]
59 public virtual string BackImageUrl
61 get
63 if(IsSet(IMAGE_URL))
64 return (string)(ViewState["BackImageUrl"]);
65 return String.Empty;
67 set
69 if(value == null)
70 throw new ArgumentNullException("value");
71 ViewState["BackImageUrl"] = value;
72 Set(IMAGE_URL);
76 [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
77 [WebSysDescription ("The space left around the borders within a cell.")]
78 public virtual int CellPadding
80 get
82 if(IsSet(CELL_PADD))
83 return (int)(ViewState["CellPadding"]);
84 return -1;
86 set
88 if(value < -1)
89 throw new ArgumentOutOfRangeException("value", "CellPadding value has to be -1 for 'not set' or a value >= 0");
90 ViewState["CellPadding"] = value;
91 Set(CELL_PADD);
95 [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
96 [WebSysDescription ("The space left between cells.")]
97 public virtual int CellSpacing
99 get
101 if(IsSet(CELL_SPAC))
102 return (int)(ViewState["CellSpacing"]);
103 return -1;
107 if(value < -1)
108 throw new ArgumentOutOfRangeException("value"," CellSpacing value has to be -1 for 'not set' or a value >= 0");
109 ViewState["CellSpacing"] = value;
110 Set(CELL_SPAC);
114 [DefaultValue (typeof (GridLines), "None"), Bindable (true), WebCategory ("Appearance")]
115 [WebSysDescription ("The type of grid that a table uses.")]
116 public virtual GridLines GridLines
120 if(IsSet(GRID_LINE))
121 return (GridLines)(ViewState["GridLines"]);
122 return GridLines.None;
126 if(!Enum.IsDefined(typeof(GridLines), value))
127 throw new ArgumentOutOfRangeException("value"," Gridlines value has to be a valid enumeration member");
128 ViewState["GridLines"] = value;
129 Set(GRID_LINE);
133 [DefaultValue (typeof (HorizontalAlign), "NotSet"), Bindable (true), WebCategory ("Layout")]
134 [WebSysDescription ("The horizonal alignment of the table.")]
135 public virtual HorizontalAlign HorizontalAlign
139 if(IsSet(HOR_ALIGN))
140 return (HorizontalAlign)(ViewState["HorizontalAlign"]);
141 return HorizontalAlign.NotSet;
145 if(!Enum.IsDefined(typeof(HorizontalAlign), value))
146 throw new ArgumentOutOfRangeException("value"," Gridlines value has to be a valid enumeration member");
147 ViewState["HorizontalAlign"] = value;
148 Set(HOR_ALIGN);
152 public override void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
154 base.AddAttributesToRender(writer, owner);
155 if(BackImageUrl.Length > 0)
157 writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, "url(" + owner.ResolveUrl(BackImageUrl) + ")");
159 if(CellSpacing >= 0)
161 writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, CellSpacing.ToString(NumberFormatInfo.InvariantInfo));
162 if(CellSpacing == 0)
163 writer.AddStyleAttribute(HtmlTextWriterStyle.BorderCollapse, "collapse");
166 if(CellPadding >= 0)
168 writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, CellPadding.ToString(NumberFormatInfo.InvariantInfo));
170 if(HorizontalAlign != HorizontalAlign.NotSet)
172 writer.AddAttribute(HtmlTextWriterAttribute.Align, Enum.Format(typeof(HorizontalAlign), HorizontalAlign, "G"));
174 string gd = null;
175 switch(GridLines)
177 case GridLines.None: break;
178 case GridLines.Horizontal: gd = "rows";
179 break;
180 case GridLines.Vertical: gd = "cols";
181 break;
182 case GridLines.Both: gd = "all";
183 break;
186 if (gd != null)
187 writer.AddAttribute(HtmlTextWriterAttribute.Rules, gd);
190 public override void CopyFrom(Style s)
192 if (s == null || s.IsEmpty)
193 return;
195 base.CopyFrom (s);
196 TableStyle from = s as TableStyle;
197 if (from == null)
198 return;
200 if (from.IsSet (HOR_ALIGN))
201 HorizontalAlign = from.HorizontalAlign;
203 if (from.IsSet (IMAGE_URL))
204 BackImageUrl = from.BackImageUrl;
206 if (from.IsSet (CELL_PADD))
207 CellPadding = from.CellPadding;
209 if (from.IsSet (CELL_SPAC))
210 CellSpacing = from.CellSpacing;
212 if (from.IsSet (GRID_LINE))
213 GridLines = from.GridLines;
216 public override void MergeWith(Style s)
218 if(s != null && !s.IsEmpty)
220 if (IsEmpty) {
221 CopyFrom (s);
222 return;
224 base.MergeWith(s);
226 if (!(s is TableStyle))
227 return;
229 TableStyle with = (TableStyle)s;
230 if(with.IsSet(HOR_ALIGN) && !IsSet(HOR_ALIGN))
232 HorizontalAlign = with.HorizontalAlign;
234 if(with.IsSet(IMAGE_URL) && !IsSet(IMAGE_URL))
236 BackImageUrl = with.BackImageUrl;
238 if(with.IsSet(CELL_PADD) && !IsSet(CELL_PADD))
240 CellPadding = with.CellPadding;
242 if(with.IsSet(CELL_SPAC) && !IsSet(CELL_SPAC))
244 CellSpacing = with.CellSpacing;
246 if(with.IsSet(GRID_LINE) && !IsSet(GRID_LINE))
248 GridLines = with.GridLines;
253 public override void Reset()
255 if(IsSet(IMAGE_URL))
256 ViewState.Remove("BackImageUrl");
257 if(IsSet(HOR_ALIGN))
258 ViewState.Remove("HorizontalAlign");
259 if(IsSet(CELL_PADD))
260 ViewState.Remove("CellPadding");
261 if(IsSet(CELL_SPAC))
262 ViewState.Remove("CellSpacing");
263 if(IsSet(GRID_LINE))
264 ViewState.Remove("GridLines");
265 base.Reset();