Style.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. //
  2. // System.Web.UI.WebControls.Style.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. using System;
  12. using System.Text;
  13. using System.Collections;
  14. using System.Drawing;
  15. using System.Globalization;
  16. using System.ComponentModel;
  17. using System.Web;
  18. using System.Web.UI;
  19. namespace System.Web.UI.WebControls
  20. {
  21. [ToolboxItem(false)]
  22. [TypeConverter(typeof(ExpandableObjectConverter))]
  23. public class Style : Component , IStateManager
  24. {
  25. internal static int MARKED = (0x01 << 0);
  26. internal static int BACKCOLOR = (0x01 << 1);
  27. internal static int BORDERCOLOR = (0x01 << 2);
  28. internal static int BORDERSTYLE = (0x01 << 3);
  29. internal static int BORDERWIDTH = (0x01 << 4);
  30. internal static int CSSCLASS = (0x01 << 5);
  31. internal static int FORECOLOR = (0x01 << 6);
  32. internal static int HEIGHT = (0x01 << 7);
  33. internal static int WIDTH = (0x01 << 8);
  34. internal static int FONT_BOLD = (0x01 << 9);
  35. internal static int FONT_ITALIC = (0x01 << 10);
  36. internal static int FONT_NAMES = (0x01 << 11);
  37. internal static int FONT_SIZE = (0x01 << 12);
  38. internal static int FONT_STRIKE = (0x01 << 13);
  39. internal static int FONT_OLINE = (0x01 << 14);
  40. internal static int FONT_ULINE = (0x01 << 15);
  41. internal static string selectionBitString = "_SystemWebUIWebControlsStyle_SBS";
  42. private StateBag viewState;
  43. private int selectionBits;
  44. private bool selfStateBag;
  45. private FontInfo font;
  46. public Style()
  47. {
  48. Initialize(null);
  49. selfStateBag = true;
  50. }
  51. public Style(StateBag bag): base()
  52. {
  53. Initialize(bag);
  54. selfStateBag = false;
  55. }
  56. private void Initialize(StateBag bag)
  57. {
  58. viewState = bag;
  59. selectionBits = 0x00;
  60. }
  61. protected internal StateBag ViewState
  62. {
  63. get
  64. {
  65. if(viewState == null)
  66. {
  67. viewState = new StateBag(false);
  68. if(IsTrackingViewState)
  69. viewState.TrackViewState();
  70. }
  71. return viewState;
  72. }
  73. }
  74. internal bool IsSet(int bit)
  75. {
  76. return ( (selectionBits & bit) != 0x00);
  77. }
  78. internal virtual void Set(int bit)
  79. {
  80. selectionBits |= bit;
  81. if(IsTrackingViewState)
  82. selectionBits |= MARKED;
  83. }
  84. [NotifyParentProperty (true)]
  85. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  86. [TypeConverter (typeof (WebColorConverter))]
  87. [WebSysDescription ("The background color for the WebControl.")]
  88. public Color BackColor
  89. {
  90. get
  91. {
  92. if(IsSet(BACKCOLOR))
  93. return (Color)ViewState["BackColor"];
  94. return Color.Empty;
  95. }
  96. set
  97. {
  98. ViewState["BackColor"] = value;
  99. Set(BACKCOLOR);
  100. }
  101. }
  102. [NotifyParentProperty (true)]
  103. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  104. [TypeConverter (typeof (WebColorConverter))]
  105. [WebSysDescription ("The border color for the WebControl.")]
  106. public Color BorderColor
  107. {
  108. get
  109. {
  110. if(IsSet(BORDERCOLOR))
  111. return (Color)ViewState["BorderColor"];
  112. return Color.Empty;
  113. }
  114. set
  115. {
  116. ViewState["BorderColor"] = value;
  117. Set(BORDERCOLOR);
  118. }
  119. }
  120. [NotifyParentProperty (true)]
  121. [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
  122. [WebSysDescription ("The style/type of the border used for the WebControl.")]
  123. public BorderStyle BorderStyle
  124. {
  125. get
  126. {
  127. if(IsSet(BORDERSTYLE))
  128. return (BorderStyle)ViewState["BorderStyle"];
  129. return BorderStyle.NotSet;
  130. }
  131. set
  132. {
  133. ViewState["BorderStyle"] = value;
  134. Set(BORDERSTYLE);
  135. }
  136. }
  137. [NotifyParentProperty (true)]
  138. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  139. [WebSysDescription ("The width of the border used for the WebControl.")]
  140. public Unit BorderWidth
  141. {
  142. get
  143. {
  144. if(IsSet(BORDERWIDTH))
  145. return (Unit)ViewState["BorderWidth"];
  146. return Unit.Empty;
  147. }
  148. set
  149. {
  150. ViewState["BorderWidth"] = value;
  151. Set(BORDERWIDTH);
  152. }
  153. }
  154. [NotifyParentProperty (true)]
  155. [DefaultValue (""), WebCategory ("Appearance")]
  156. [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
  157. public string CssClass
  158. {
  159. get
  160. {
  161. if(IsSet(CSSCLASS))
  162. return (string)ViewState["CssClass"];
  163. return string.Empty;
  164. }
  165. set
  166. {
  167. ViewState["CssClass"] = value;
  168. Set(CSSCLASS);
  169. }
  170. }
  171. [NotifyParentProperty (true)]
  172. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  173. [TypeConverter (typeof (WebColorConverter))]
  174. [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
  175. public Color ForeColor
  176. {
  177. get
  178. {
  179. if(IsSet(FORECOLOR))
  180. return (Color)ViewState["ForeColor"];
  181. return Color.Empty;
  182. }
  183. set
  184. {
  185. ViewState["ForeColor"] = value;
  186. Set(FORECOLOR);
  187. }
  188. }
  189. [NotifyParentProperty (true)]
  190. [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
  191. [WebSysDescription ("The height of this WebControl.")]
  192. public Unit Height
  193. {
  194. get
  195. {
  196. if(IsSet(HEIGHT))
  197. return (Unit)ViewState["Height"];
  198. return Unit.Empty;
  199. }
  200. set
  201. {
  202. ViewState["Height"] = value;
  203. Set(HEIGHT);
  204. }
  205. }
  206. [NotifyParentProperty (true)]
  207. [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
  208. [WebSysDescription ("The width of this WebControl.")]
  209. public Unit Width
  210. {
  211. get
  212. {
  213. if(IsSet(WIDTH))
  214. return (Unit)ViewState["Width"];
  215. return Unit.Empty;
  216. }
  217. set
  218. {
  219. ViewState["Width"] = value;
  220. Set(WIDTH);
  221. }
  222. }
  223. [NotifyParentProperty (true)]
  224. [WebCategory ("Appearance")]
  225. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  226. [WebSysDescription ("The font of this WebControl.")]
  227. public FontInfo Font
  228. {
  229. get
  230. {
  231. if(font==null)
  232. font = new FontInfo(this);
  233. return font;
  234. }
  235. }
  236. protected internal virtual bool IsEmpty
  237. {
  238. get { return (selectionBits == 0); }
  239. }
  240. private void AddColor(HtmlTextWriter writer, HtmlTextWriterStyle style, Color color)
  241. {
  242. if(!color.IsEmpty)
  243. writer.AddStyleAttribute(style, ColorTranslator.ToHtml(color));
  244. }
  245. private static string StringArrayToString(string[] array, char separator)
  246. {
  247. if(array.Length == 0)
  248. return String.Empty;
  249. StringBuilder sb = new StringBuilder();
  250. for(int i=0; i < array.Length; i++)
  251. {
  252. if(i==0)
  253. {
  254. sb.Append(array[0]);
  255. } else
  256. {
  257. sb.Append(separator);
  258. sb.Append(array[i]);
  259. }
  260. }
  261. return sb.ToString();
  262. }
  263. public void AddAttributesToRender(HtmlTextWriter writer)
  264. {
  265. AddAttributesToRender(writer, null);
  266. }
  267. public virtual void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
  268. {
  269. if(IsSet(BACKCOLOR))
  270. {
  271. AddColor(writer, HtmlTextWriterStyle.BackgroundColor, (Color)ViewState["BackColor"]);
  272. }
  273. if(IsSet(BORDERCOLOR))
  274. {
  275. AddColor(writer, HtmlTextWriterStyle.BorderColor, (Color)ViewState["BorderColor"]);
  276. }
  277. if(IsSet(FORECOLOR))
  278. {
  279. AddColor(writer, HtmlTextWriterStyle.Color, (Color)ViewState["ForeColor"]);
  280. }
  281. if(IsSet(CSSCLASS))
  282. {
  283. string cssClass = (string)ViewState["CssClass"];
  284. if(cssClass.Length > 0)
  285. writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);
  286. }
  287. if(!BorderWidth.IsEmpty)
  288. {
  289. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, BorderWidth.ToString(CultureInfo.InvariantCulture));
  290. if(BorderStyle!=BorderStyle.NotSet)
  291. {
  292. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, Enum.Format(typeof(BorderStyle), BorderStyle, "G"));
  293. } else
  294. {
  295. if(BorderWidth.Value != 0.0)
  296. {
  297. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
  298. }
  299. }
  300. } else
  301. {
  302. if(BorderStyle!=BorderStyle.NotSet)
  303. {
  304. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, Enum.Format(typeof(BorderStyle), BorderStyle, "G"));
  305. }
  306. }
  307. if(Font.Names.Length > 0)
  308. {
  309. writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, StringArrayToString(Font.Names,','));
  310. }
  311. if(!Font.Size.IsEmpty)
  312. {
  313. writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, Font.Size.ToString(CultureInfo.InvariantCulture));
  314. }
  315. if(Font.Bold)
  316. {
  317. writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, "bold");
  318. }
  319. if(Font.Italic)
  320. {
  321. writer.AddStyleAttribute(HtmlTextWriterStyle.FontStyle, "italic");
  322. }
  323. string textDecoration = String.Empty;
  324. if(Font.Strikeout)
  325. {
  326. textDecoration += " strikeout";
  327. }
  328. if(Font.Underline)
  329. {
  330. textDecoration += " underline";
  331. }
  332. if(Font.Overline)
  333. {
  334. textDecoration += " overline";
  335. }
  336. if(textDecoration.Length > 0)
  337. {
  338. writer.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, textDecoration);
  339. }
  340. Unit u = Unit.Empty;
  341. if(IsSet(HEIGHT))
  342. {
  343. u = (Unit)ViewState["Height"];
  344. writer.AddStyleAttribute(HtmlTextWriterStyle.Height, u.ToString(CultureInfo.InvariantCulture));
  345. }
  346. if(IsSet(WIDTH))
  347. {
  348. u = (Unit)ViewState["Width"];
  349. writer.AddStyleAttribute(HtmlTextWriterStyle.Width, u.ToString(CultureInfo.InvariantCulture));
  350. }
  351. }
  352. public virtual void CopyFrom(Style source)
  353. {
  354. if(source!=null && !source.IsEmpty)
  355. {
  356. Font.CopyFrom(source.Font);
  357. if(source.Height!=Unit.Empty)
  358. {
  359. Height = source.Height;
  360. }
  361. if(source.Width!=Unit.Empty)
  362. {
  363. Width = source.Width;
  364. }
  365. if(source.BorderColor!=Color.Empty)
  366. {
  367. BorderColor = source.BorderColor;
  368. }
  369. if(source.BorderWidth!=Unit.Empty)
  370. {
  371. BorderWidth = source.BorderWidth;
  372. }
  373. if(source.BorderStyle!=BorderStyle.NotSet)
  374. {
  375. BorderStyle = source.BorderStyle;
  376. }
  377. if(source.BackColor!=Color.Empty)
  378. {
  379. BackColor = source.BackColor;
  380. }
  381. if(source.CssClass!=String.Empty)
  382. {
  383. CssClass = source.CssClass;
  384. }
  385. if(source.ForeColor!=Color.Empty)
  386. {
  387. ForeColor = source.ForeColor;
  388. }
  389. }
  390. }
  391. public virtual void MergeWith(Style with)
  392. {
  393. if(with!=null && !with.IsEmpty)
  394. {
  395. if(IsEmpty)
  396. {
  397. CopyFrom(with);
  398. return;
  399. }
  400. Font.MergeWith(with.Font);
  401. if(!IsSet(HEIGHT) && with.Height!=Unit.Empty)
  402. {
  403. Height = with.Height;
  404. }
  405. if(!IsSet(WIDTH) && with.Width!=Unit.Empty)
  406. {
  407. Width = with.Width;
  408. }
  409. if(!IsSet(BORDERCOLOR) && with.BorderColor!=Color.Empty)
  410. {
  411. BorderColor = with.BorderColor;
  412. }
  413. if(!IsSet(BORDERWIDTH) && with.BorderWidth!=Unit.Empty)
  414. {
  415. BorderWidth = with.BorderWidth;
  416. }
  417. if(!IsSet(BORDERSTYLE) && with.BorderStyle!=BorderStyle.NotSet)
  418. {
  419. BorderStyle = with.BorderStyle;
  420. }
  421. if(!IsSet(BACKCOLOR) && with.BackColor!=Color.Empty)
  422. {
  423. BackColor = with.BackColor;
  424. }
  425. if(!IsSet(CSSCLASS) && with.CssClass!=String.Empty)
  426. {
  427. CssClass = with.CssClass;
  428. }
  429. if(!IsSet(FORECOLOR) && with.ForeColor!=Color.Empty)
  430. {
  431. ForeColor = with.ForeColor;
  432. }
  433. }
  434. }
  435. public virtual void Reset()
  436. {
  437. if(IsSet(BACKCOLOR))
  438. ViewState.Remove("BackColor");
  439. if(IsSet(BORDERCOLOR))
  440. ViewState.Remove("BorderColor");
  441. if(IsSet(BORDERSTYLE))
  442. ViewState.Remove("BorderStyle");
  443. if(IsSet(BORDERWIDTH))
  444. ViewState.Remove("BorderWidth");
  445. if(IsSet(CSSCLASS))
  446. ViewState.Remove("CssClass");
  447. if(IsSet(FORECOLOR))
  448. ViewState.Remove("ForeColor");
  449. if(IsSet(HEIGHT))
  450. ViewState.Remove("Height");
  451. if(IsSet(WIDTH))
  452. ViewState.Remove("Width");
  453. if(font!=null)
  454. font.Reset();
  455. selectionBits = 0x00;
  456. }
  457. protected bool IsTrackingViewState
  458. {
  459. get
  460. {
  461. return ( (selectionBits & MARKED) != 0x00 );
  462. }
  463. }
  464. protected internal virtual void TrackViewState()
  465. {
  466. if(viewState!=null)
  467. ViewState.TrackViewState();
  468. Set(MARKED);
  469. }
  470. protected internal virtual object SaveViewState()
  471. {
  472. if(viewState != null)
  473. {
  474. if(IsSet(MARKED))
  475. {
  476. ViewState[selectionBitString] = selectionBits;
  477. }
  478. if(selfStateBag)
  479. {
  480. return ViewState.SaveViewState();
  481. }
  482. }
  483. return null;
  484. }
  485. protected internal void LoadViewState(object state)
  486. {
  487. if (state != null && selfStateBag)
  488. ViewState.LoadViewState(state);
  489. if (viewState != null) {
  490. object o = ViewState[selectionBitString];
  491. if (o != null)
  492. selectionBits = (int) o;
  493. }
  494. }
  495. void IStateManager.LoadViewState(object state)
  496. {
  497. LoadViewState(state);
  498. }
  499. object IStateManager.SaveViewState()
  500. {
  501. return SaveViewState();
  502. }
  503. void IStateManager.TrackViewState()
  504. {
  505. TrackViewState();
  506. }
  507. bool IStateManager.IsTrackingViewState
  508. {
  509. get
  510. {
  511. return IsTrackingViewState;
  512. }
  513. }
  514. public override string ToString()
  515. {
  516. return String.Empty;
  517. }
  518. }
  519. }