2
0

Style.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. internal virtual 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(HEIGHT);
  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. internal virtual bool IsEmpty
  208. {
  209. get
  210. {
  211. return (selectionBits != 0);
  212. }
  213. }
  214. private void AddColor(HtmlTextWriter writer, HtmlTextWriterStyle style, Color color)
  215. {
  216. if(!color.IsEmpty)
  217. writer.AddStyleAttribute(style, ColorTranslator.ToHtml(color));
  218. }
  219. private static string StringArrayToString(string[] array, char separator)
  220. {
  221. if(array.Length == 0)
  222. return String.Empty;
  223. StringBuilder sb = new StringBuilder();
  224. for(int i=0; i < array.Length; i++)
  225. {
  226. if(i==0)
  227. {
  228. sb.Append(array[0]);
  229. } else
  230. {
  231. sb.Append(separator);
  232. sb.Append(array[i]);
  233. }
  234. }
  235. return sb.ToString();
  236. }
  237. public void AddAttributesToRender(HtmlTextWriter writer)
  238. {
  239. AddAttributesToRender(writer, null);
  240. }
  241. public virtual void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
  242. {
  243. if(IsSet(BACKCOLOR))
  244. {
  245. AddColor(writer, HtmlTextWriterStyle.BackgroundColor, (Color)ViewState["BackColor"]);
  246. }
  247. if(IsSet(BORDERCOLOR))
  248. {
  249. AddColor(writer, HtmlTextWriterStyle.BorderColor, (Color)ViewState["BorderColor"]);
  250. }
  251. if(IsSet(FORECOLOR))
  252. {
  253. AddColor(writer, HtmlTextWriterStyle.Color, (Color)ViewState["ForeColor"]);
  254. }
  255. if(IsSet(CSSCLASS))
  256. {
  257. string cssClass = (string)ViewState["CssClass"];
  258. if(cssClass.Length > 0)
  259. writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClass);
  260. }
  261. if(!BorderWidth.IsEmpty)
  262. {
  263. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, BorderWidth.ToString(CultureInfo.InvariantCulture));
  264. if(BorderStyle!=BorderStyle.NotSet)
  265. {
  266. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, Enum.Format(typeof(BorderStyle), BorderStyle, "G"));
  267. } else
  268. {
  269. if(BorderWidth.Value != 0.0)
  270. {
  271. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
  272. }
  273. }
  274. } else
  275. {
  276. if(BorderStyle!=BorderStyle.NotSet)
  277. {
  278. writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, Enum.Format(typeof(BorderStyle), BorderStyle, "G"));
  279. }
  280. }
  281. if(Font.Names.Length > 0)
  282. {
  283. writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, StringArrayToString(Font.Names,','));
  284. }
  285. if(!Font.Size.IsEmpty)
  286. {
  287. writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, Font.Size.ToString(CultureInfo.InvariantCulture));
  288. }
  289. if(Font.Bold)
  290. {
  291. writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight, "bold");
  292. }
  293. if(Font.Italic)
  294. {
  295. writer.AddStyleAttribute(HtmlTextWriterStyle.FontStyle, "italic");
  296. }
  297. string textDecoration = String.Empty;
  298. if(Font.Strikeout)
  299. {
  300. textDecoration += " strikeout";
  301. }
  302. if(Font.Underline)
  303. {
  304. textDecoration += " underline";
  305. }
  306. if(Font.Overline)
  307. {
  308. textDecoration += " overline";
  309. }
  310. if(textDecoration.Length > 0)
  311. {
  312. writer.AddStyleAttribute(HtmlTextWriterStyle.TextDecoration, textDecoration);
  313. }
  314. Unit u = Unit.Empty;
  315. if(IsSet(HEIGHT))
  316. {
  317. u = (Unit)ViewState["Height"];
  318. writer.AddStyleAttribute(HtmlTextWriterStyle.Height, u.ToString(CultureInfo.InvariantCulture));
  319. }
  320. if(IsSet(WIDTH))
  321. {
  322. u = (Unit)ViewState["Width"];
  323. writer.AddStyleAttribute(HtmlTextWriterStyle.Width, u.ToString(CultureInfo.InvariantCulture));
  324. }
  325. }
  326. public virtual void CopyFrom(Style source)
  327. {
  328. if(source!=null && !source.IsEmpty)
  329. {
  330. Font.CopyFrom(source.Font);
  331. if(source.Height!=Unit.Empty)
  332. {
  333. Height = source.Height;
  334. }
  335. if(source.Width!=Unit.Empty)
  336. {
  337. Width = source.Width;
  338. }
  339. if(source.BorderColor!=Color.Empty)
  340. {
  341. BorderColor = source.BorderColor;
  342. }
  343. if(source.BorderWidth!=Unit.Empty)
  344. {
  345. BorderWidth = source.BorderWidth;
  346. }
  347. if(source.BorderStyle!=BorderStyle.NotSet)
  348. {
  349. BorderStyle = source.BorderStyle;
  350. }
  351. if(source.BackColor!=Color.Empty)
  352. {
  353. BackColor = source.BackColor;
  354. }
  355. if(source.CssClass!=String.Empty)
  356. {
  357. CssClass = source.CssClass;
  358. }
  359. if(source.ForeColor!=Color.Empty)
  360. {
  361. ForeColor = source.ForeColor;
  362. }
  363. }
  364. }
  365. public virtual void MergeWith(Style with)
  366. {
  367. if(with!=null && !with.IsEmpty)
  368. {
  369. if(IsEmpty)
  370. {
  371. CopyFrom(with);
  372. return;
  373. }
  374. Font.MergeWith(with.Font);
  375. if(!IsSet(HEIGHT) && with.Height!=Unit.Empty)
  376. {
  377. Height = with.Height;
  378. }
  379. if(!IsSet(WIDTH) && with.Width!=Unit.Empty)
  380. {
  381. Width = with.Width;
  382. }
  383. if(!IsSet(BORDERCOLOR) && with.BorderColor!=Color.Empty)
  384. {
  385. BorderColor = with.BorderColor;
  386. }
  387. if(!IsSet(BORDERWIDTH) && with.BorderWidth!=Unit.Empty)
  388. {
  389. BorderWidth = with.BorderWidth;
  390. }
  391. if(!IsSet(BORDERSTYLE) && with.BorderStyle!=BorderStyle.NotSet)
  392. {
  393. BorderStyle = with.BorderStyle;
  394. }
  395. if(!IsSet(BACKCOLOR) && with.BackColor!=Color.Empty)
  396. {
  397. BackColor = with.BackColor;
  398. }
  399. if(!IsSet(CSSCLASS) && with.CssClass!=String.Empty)
  400. {
  401. CssClass = with.CssClass;
  402. }
  403. if(!IsSet(FORECOLOR) && with.ForeColor!=Color.Empty)
  404. {
  405. ForeColor = with.ForeColor;
  406. }
  407. }
  408. }
  409. public virtual void Reset()
  410. {
  411. if(IsSet(BACKCOLOR))
  412. ViewState.Remove("BackColor");
  413. if(IsSet(BORDERCOLOR))
  414. ViewState.Remove("BorderColor");
  415. if(IsSet(BORDERSTYLE))
  416. ViewState.Remove("BorderStyle");
  417. if(IsSet(BORDERWIDTH))
  418. ViewState.Remove("BorderWidth");
  419. if(IsSet(CSSCLASS))
  420. ViewState.Remove("CssClass");
  421. if(IsSet(FORECOLOR))
  422. ViewState.Remove("ForeColor");
  423. if(IsSet(HEIGHT))
  424. ViewState.Remove("Height");
  425. if(IsSet(WIDTH))
  426. ViewState.Remove("Width");
  427. if(font!=null)
  428. font.Reset();
  429. selectionBits = 0x00;
  430. }
  431. protected bool IsTrackingViewState
  432. {
  433. get
  434. {
  435. return ( (selectionBits & MARKED) != 0x00 );
  436. }
  437. }
  438. protected internal virtual void TrackViewState()
  439. {
  440. if(viewState!=null)
  441. ViewState.TrackViewState();
  442. Set(MARKED);
  443. }
  444. protected internal object SaveViewState()
  445. {
  446. if(viewState != null)
  447. {
  448. if(IsSet(MARKED))
  449. {
  450. ViewState[selectionBitString] = selectionBits;
  451. }
  452. if(selfStateBag)
  453. {
  454. return ViewState.SaveViewState();
  455. }
  456. }
  457. return null;
  458. }
  459. protected internal void LoadViewState(object state)
  460. {
  461. if(state!=null && selfStateBag)
  462. {
  463. ViewState.LoadViewState(state);
  464. }
  465. if(viewState!=null)
  466. {
  467. selectionBits = (int)ViewState[selectionBitString];
  468. }
  469. }
  470. void IStateManager.LoadViewState(object state)
  471. {
  472. LoadViewState(state);
  473. }
  474. object IStateManager.SaveViewState()
  475. {
  476. return SaveViewState();
  477. }
  478. void IStateManager.TrackViewState()
  479. {
  480. TrackViewState();
  481. }
  482. bool IStateManager.IsTrackingViewState
  483. {
  484. get
  485. {
  486. return IsTrackingViewState;
  487. }
  488. }
  489. public override string ToString()
  490. {
  491. return String.Empty;
  492. }
  493. }
  494. }