Style.cs 11 KB

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