Style.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: Style
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Text;
  15. using System.Collections;
  16. using System.Drawing;
  17. using System.Globalization;
  18. using System.ComponentModel;
  19. using System.Web;
  20. using System.Web.UI;
  21. namespace System.Web.UI.WebControls
  22. {
  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. internal virtual 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. public Color BackColor
  85. {
  86. get
  87. {
  88. if(IsSet(BACKCOLOR))
  89. return (Color)ViewState["BackColor"];
  90. return Color.Empty;
  91. }
  92. set
  93. {
  94. ViewState["BackColor"] = value;
  95. Set(BACKCOLOR);
  96. }
  97. }
  98. public Color BorderColor
  99. {
  100. get
  101. {
  102. if(IsSet(BORDERCOLOR))
  103. return (Color)ViewState["BorderColor"];
  104. return Color.Empty;
  105. }
  106. set
  107. {
  108. ViewState["BorderColor"] = value;
  109. Set(BORDERCOLOR);
  110. }
  111. }
  112. public BorderStyle BorderStyle
  113. {
  114. get
  115. {
  116. if(IsSet(BORDERSTYLE))
  117. return (BorderStyle)ViewState["BorderStyle"];
  118. return BorderStyle.NotSet;
  119. }
  120. set
  121. {
  122. ViewState["BorderStyle"] = value;
  123. Set(BORDERSTYLE);
  124. }
  125. }
  126. public Unit BorderWidth
  127. {
  128. get
  129. {
  130. if(IsSet(BORDERWIDTH))
  131. return (Unit)ViewState["BorderWidth"];
  132. return Unit.Empty;
  133. }
  134. set
  135. {
  136. ViewState["BorderWidth"] = value;
  137. Set(BORDERWIDTH);
  138. }
  139. }
  140. public string CssClass
  141. {
  142. get
  143. {
  144. if(IsSet(CSSCLASS))
  145. return (string)ViewState["CssClass"];
  146. return string.Empty;
  147. }
  148. set
  149. {
  150. ViewState["CssClass"] = value;
  151. Set(CSSCLASS);
  152. }
  153. }
  154. public Color ForeColor
  155. {
  156. get
  157. {
  158. if(IsSet(FORECOLOR))
  159. return (Color)ViewState["ForeColor"];
  160. return Color.Empty;
  161. }
  162. set
  163. {
  164. ViewState["ForeColor"] = value;
  165. Set(FORECOLOR);
  166. }
  167. }
  168. public Unit Height
  169. {
  170. get
  171. {
  172. if(IsSet(HEIGHT))
  173. return (Unit)ViewState["Height"];
  174. return Unit.Empty;
  175. }
  176. set
  177. {
  178. ViewState["Height"] = value;
  179. Set(HEIGHT);
  180. }
  181. }
  182. public Unit Width
  183. {
  184. get
  185. {
  186. if(IsSet(WIDTH))
  187. return (Unit)ViewState["Width"];
  188. return Unit.Empty;
  189. }
  190. set
  191. {
  192. ViewState["Width"] = value;
  193. Set(HEIGHT);
  194. }
  195. }
  196. public FontInfo Font
  197. {
  198. get
  199. {
  200. if(font==null)
  201. font = new FontInfo(this);
  202. return font;
  203. }
  204. }
  205. internal virtual bool IsEmpty
  206. {
  207. get
  208. {
  209. return (selectionBits != 0);
  210. }
  211. }
  212. private void AddColor(HtmlTextWriter writer, HtmlTextWriterStyle style, Color color)
  213. {
  214. if(!color.IsEmpty)
  215. writer.AddStyleAttribute(style, ColorTranslator.ToHtml(color));
  216. }
  217. private static string StringArrayToString(string[] array, char separator)
  218. {
  219. if(array.Length == 0)
  220. return String.Empty;
  221. StringBuilder sb = new StringBuilder();
  222. for(int i=0; i < array.Length; i++)
  223. {
  224. if(i==0)
  225. {
  226. sb.Append(array[0]);
  227. } else
  228. {
  229. sb.Append(separator);
  230. sb.Append(array[i]);
  231. }
  232. }
  233. return sb.ToString();
  234. }
  235. public void AddAttributesToRender(HtmlTextWriter writer)
  236. {
  237. AddAttributesToRender(writer, null);
  238. }
  239. public void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
  240. {
  241. if(IsSet(BACKCOLOR))
  242. {
  243. AddColor(writer, HtmlTextWriterStyle.BackgroundColor, (Color)ViewState["BackColor"]);
  244. }
  245. if(IsSet(BORDERCOLOR))
  246. {
  247. AddColor(writer, HtmlTextWriterStyle.BorderColor, (Color)ViewState["BorderColor"]);
  248. }
  249. if(IsSet(FORECOLOR))
  250. {
  251. AddColor(writer, HtmlTextWriterStyle.Color, (Color)ViewState["ForeColor"]);
  252. }
  253. if(IsSet(CSSCLASS))
  254. {
  255. string cssClass = (string)ViewState["CssClass"];
  256. if(cssClass.Length > 0)
  257. writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);
  258. }
  259. if(!BorderWidth.IsEmpty)
  260. {
  261. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, BorderWidth.ToString(CultureInfo.InvariantCulture));
  262. if(BorderStyle!=BorderStyle.NotSet)
  263. {
  264. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, Enum.Format(typeof(BorderStyle), BorderStyle, "G"));
  265. } else
  266. {
  267. if(BorderWidth.Value != 0.0)
  268. {
  269. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
  270. }
  271. }
  272. } else
  273. {
  274. if(BorderStyle!=BorderStyle.NotSet)
  275. {
  276. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, Enum.Format(typeof(BorderStyle), BorderStyle, "G"));
  277. }
  278. }
  279. if(Font.Names.Length > 0)
  280. {
  281. writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, StringArrayToString(Font.Names,','));
  282. }
  283. if(!Font.Size.IsEmpty)
  284. {
  285. writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, Font.Size.ToString(CultureInfo.InvariantCulture));
  286. }
  287. if(Font.Bold)
  288. {
  289. writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, "bold");
  290. }
  291. if(Font.Italic)
  292. {
  293. writer.AddStyleAttribute(HtmlTextWriterStyle.FontStyle, "italic");
  294. }
  295. string textDecoration = String.Empty;
  296. if(Font.Strikeout)
  297. {
  298. textDecoration += " strikeout";
  299. }
  300. if(Font.Underline)
  301. {
  302. textDecoration += " underline";
  303. }
  304. if(Font.Overline)
  305. {
  306. textDecoration += " overline";
  307. }
  308. if(textDecoration.Length > 0)
  309. {
  310. writer.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, textDecoration);
  311. }
  312. Unit u = Unit.Empty;
  313. if(IsSet(HEIGHT))
  314. {
  315. u = (Unit)ViewState["Height"];
  316. writer.AddStyleAttribute(HtmlTextWriterStyle.Height, u.ToString(CultureInfo.InvariantCulture));
  317. }
  318. if(IsSet(WIDTH))
  319. {
  320. u = (Unit)ViewState["Width"];
  321. writer.AddStyleAttribute(HtmlTextWriterStyle.Width, u.ToString(CultureInfo.InvariantCulture));
  322. }
  323. }
  324. public virtual void CopyFrom(Style source)
  325. {
  326. if(source!=null && !source.IsEmpty)
  327. {
  328. Font.CopyFrom(source.Font);
  329. if(source.Height!=Unit.Empty)
  330. {
  331. Height = source.Height;
  332. }
  333. if(source.Width!=Unit.Empty)
  334. {
  335. Width = source.Width;
  336. }
  337. if(source.BorderColor!=Color.Empty)
  338. {
  339. BorderColor = source.BorderColor;
  340. }
  341. if(source.BorderWidth!=Unit.Empty)
  342. {
  343. BorderWidth = source.BorderWidth;
  344. }
  345. if(source.BorderStyle!=BorderStyle.NotSet)
  346. {
  347. BorderStyle = source.BorderStyle;
  348. }
  349. if(source.BackColor!=Color.Empty)
  350. {
  351. BackColor = source.BackColor;
  352. }
  353. if(source.CssClass!=String.Empty)
  354. {
  355. CssClass = source.CssClass;
  356. }
  357. if(source.ForeColor!=Color.Empty)
  358. {
  359. ForeColor = source.ForeColor;
  360. }
  361. }
  362. }
  363. public virtual void MergeWith(Style with)
  364. {
  365. if(with!=null && !with.IsEmpty)
  366. {
  367. if(IsEmpty)
  368. {
  369. CopyFrom(with);
  370. return;
  371. }
  372. Font.MergeWith(with.Font);
  373. if(!IsSet(HEIGHT) && with.Height!=Unit.Empty)
  374. {
  375. Height = with.Height;
  376. }
  377. if(!IsSet(WIDTH) && with.Width!=Unit.Empty)
  378. {
  379. Width = with.Width;
  380. }
  381. if(!IsSet(BORDERCOLOR) && with.BorderColor!=Color.Empty)
  382. {
  383. BorderColor = with.BorderColor;
  384. }
  385. if(!IsSet(BORDERWIDTH) && with.BorderWidth!=Unit.Empty)
  386. {
  387. BorderWidth = with.BorderWidth;
  388. }
  389. if(!IsSet(BORDERSTYLE) && with.BorderStyle!=BorderStyle.NotSet)
  390. {
  391. BorderStyle = with.BorderStyle;
  392. }
  393. if(!IsSet(BACKCOLOR) && with.BackColor!=Color.Empty)
  394. {
  395. BackColor = with.BackColor;
  396. }
  397. if(!IsSet(CSSCLASS) && with.CssClass!=String.Empty)
  398. {
  399. CssClass = with.CssClass;
  400. }
  401. if(!IsSet(FORECOLOR) && with.ForeColor!=Color.Empty)
  402. {
  403. ForeColor = with.ForeColor;
  404. }
  405. }
  406. }
  407. public virtual void Reset()
  408. {
  409. if(IsSet(BACKCOLOR))
  410. ViewState.Remove("BackColor");
  411. if(IsSet(BORDERCOLOR))
  412. ViewState.Remove("BorderColor");
  413. if(IsSet(BORDERSTYLE))
  414. ViewState.Remove("BorderStyle");
  415. if(IsSet(BORDERWIDTH))
  416. ViewState.Remove("BorderWidth");
  417. if(IsSet(CSSCLASS))
  418. ViewState.Remove("CssClass");
  419. if(IsSet(FORECOLOR))
  420. ViewState.Remove("ForeColor");
  421. if(IsSet(HEIGHT))
  422. ViewState.Remove("Height");
  423. if(IsSet(WIDTH))
  424. ViewState.Remove("Width");
  425. if(font!=null)
  426. font.Reset();
  427. selectionBits = 0x00;
  428. }
  429. protected bool IsTrackingViewState
  430. {
  431. get
  432. {
  433. return ( (selectionBits & MARKED) != 0x00 );
  434. }
  435. }
  436. protected internal virtual void TrackViewState()
  437. {
  438. if(viewState!=null)
  439. ViewState.TrackViewState();
  440. Set(MARKED);
  441. }
  442. protected internal object SaveViewState()
  443. {
  444. if(viewState != null)
  445. {
  446. if(IsSet(MARKED))
  447. {
  448. ViewState[selectionBitString] = selectionBits;
  449. }
  450. if(selfStateBag)
  451. {
  452. return ViewState.SaveViewState();
  453. }
  454. }
  455. return null;
  456. }
  457. protected internal void LoadViewState(object state)
  458. {
  459. if(state!=null && selfStateBag)
  460. {
  461. ViewState.LoadViewState(state);
  462. }
  463. if(viewState!=null)
  464. {
  465. selectionBits = (int)ViewState[selectionBitString];
  466. }
  467. }
  468. void IStateManager.LoadViewState(object state)
  469. {
  470. LoadViewState(state);
  471. }
  472. object IStateManager.SaveViewState()
  473. {
  474. return SaveViewState();
  475. }
  476. void IStateManager.TrackViewState()
  477. {
  478. TrackViewState();
  479. }
  480. bool IStateManager.IsTrackingViewState
  481. {
  482. get
  483. {
  484. return IsTrackingViewState;
  485. }
  486. }
  487. }
  488. }