Style.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Text;
  33. using System.Collections;
  34. using System.Drawing;
  35. using System.Globalization;
  36. using System.ComponentModel;
  37. using System.Web;
  38. using System.Web.UI;
  39. namespace System.Web.UI.WebControls
  40. {
  41. [ToolboxItem(false)]
  42. [TypeConverter(typeof(ExpandableObjectConverter))]
  43. public class Style : Component , IStateManager
  44. {
  45. internal static int MARKED = (0x01 << 0);
  46. internal static int BACKCOLOR = (0x01 << 1);
  47. internal static int BORDERCOLOR = (0x01 << 2);
  48. internal static int BORDERSTYLE = (0x01 << 3);
  49. internal static int BORDERWIDTH = (0x01 << 4);
  50. internal static int CSSCLASS = (0x01 << 5);
  51. internal static int FORECOLOR = (0x01 << 6);
  52. internal static int HEIGHT = (0x01 << 7);
  53. internal static int WIDTH = (0x01 << 8);
  54. internal static int FONT_BOLD = (0x01 << 9);
  55. internal static int FONT_ITALIC = (0x01 << 10);
  56. internal static int FONT_NAMES = (0x01 << 11);
  57. internal static int FONT_SIZE = (0x01 << 12);
  58. internal static int FONT_STRIKE = (0x01 << 13);
  59. internal static int FONT_OLINE = (0x01 << 14);
  60. internal static int FONT_ULINE = (0x01 << 15);
  61. internal static string selectionBitString = "_SBS";
  62. StateBag viewState;
  63. int selectionBits;
  64. bool selfStateBag;
  65. bool marked;
  66. #if NET_2_0
  67. string regClass;
  68. #endif
  69. private FontInfo font;
  70. public Style ()
  71. {
  72. Initialize(null);
  73. selfStateBag = true;
  74. }
  75. public Style (StateBag bag)
  76. {
  77. Initialize (bag);
  78. selfStateBag = false;
  79. }
  80. private void Initialize (StateBag bag)
  81. {
  82. viewState = bag;
  83. selectionBits = 0x00;
  84. }
  85. [Browsable (false)]
  86. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  87. protected internal StateBag ViewState {
  88. get {
  89. if (viewState == null) {
  90. viewState = new StateBag (false);
  91. if (IsTrackingViewState)
  92. viewState.TrackViewState ();
  93. }
  94. return viewState;
  95. }
  96. }
  97. internal bool IsSet (int bit)
  98. {
  99. return ((selectionBits & bit) != 0x00);
  100. }
  101. internal virtual void Set (int bit)
  102. {
  103. selectionBits |= bit;
  104. if (IsTrackingViewState)
  105. selectionBits |= MARKED;
  106. }
  107. [NotifyParentProperty (true)]
  108. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  109. [TypeConverter (typeof (WebColorConverter))]
  110. [WebSysDescription ("The background color for the WebControl.")]
  111. public Color BackColor {
  112. get {
  113. if(IsSet(BACKCOLOR))
  114. return (Color)ViewState["BackColor"];
  115. return Color.Empty;
  116. }
  117. set {
  118. ViewState["BackColor"] = value;
  119. Set(BACKCOLOR);
  120. }
  121. }
  122. [NotifyParentProperty (true)]
  123. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  124. [TypeConverter (typeof (WebColorConverter))]
  125. [WebSysDescription ("The border color for the WebControl.")]
  126. public Color BorderColor {
  127. get {
  128. if (IsSet (BORDERCOLOR))
  129. return (Color) ViewState ["BorderColor"];
  130. return Color.Empty;
  131. }
  132. set {
  133. ViewState ["BorderColor"] = value;
  134. Set (BORDERCOLOR);
  135. }
  136. }
  137. [NotifyParentProperty (true)]
  138. [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
  139. [WebSysDescription ("The style/type of the border used for the WebControl.")]
  140. public BorderStyle BorderStyle {
  141. get {
  142. if (IsSet (BORDERSTYLE))
  143. return (BorderStyle) ViewState ["BorderStyle"];
  144. return BorderStyle.NotSet;
  145. }
  146. set {
  147. ViewState ["BorderStyle"] = value;
  148. Set (BORDERSTYLE);
  149. }
  150. }
  151. [NotifyParentProperty (true)]
  152. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  153. [WebSysDescription ("The width of the border used for the WebControl.")]
  154. public Unit BorderWidth {
  155. get {
  156. if (IsSet (BORDERWIDTH))
  157. return (Unit) ViewState ["BorderWidth"];
  158. return Unit.Empty;
  159. }
  160. set {
  161. ViewState ["BorderWidth"] = value;
  162. Set (BORDERWIDTH);
  163. }
  164. }
  165. [NotifyParentProperty (true)]
  166. [DefaultValue (""), WebCategory ("Appearance")]
  167. [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
  168. public string CssClass {
  169. get {
  170. if (IsSet (CSSCLASS))
  171. return (string) ViewState["CssClass"];
  172. return string.Empty;
  173. }
  174. set {
  175. ViewState ["CssClass"] = value;
  176. Set (CSSCLASS);
  177. }
  178. }
  179. [NotifyParentProperty (true)]
  180. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  181. [TypeConverter (typeof (WebColorConverter))]
  182. [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
  183. public Color ForeColor {
  184. get {
  185. if (IsSet (FORECOLOR))
  186. return (Color) ViewState ["ForeColor"];
  187. return Color.Empty;
  188. }
  189. set {
  190. ViewState ["ForeColor"] = value;
  191. Set (FORECOLOR);
  192. }
  193. }
  194. [NotifyParentProperty (true)]
  195. [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
  196. [WebSysDescription ("The height of this WebControl.")]
  197. public Unit Height {
  198. get {
  199. if (IsSet (HEIGHT))
  200. return (Unit) ViewState ["Height"];
  201. return Unit.Empty;
  202. }
  203. set {
  204. ViewState ["Height"] = value;
  205. Set (HEIGHT);
  206. }
  207. }
  208. [NotifyParentProperty (true)]
  209. [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
  210. [WebSysDescription ("The width of this WebControl.")]
  211. public Unit Width {
  212. get {
  213. if (IsSet(WIDTH))
  214. return (Unit) ViewState ["Width"];
  215. return Unit.Empty;
  216. }
  217. set {
  218. ViewState ["Width"] = value;
  219. Set (WIDTH);
  220. }
  221. }
  222. [NotifyParentProperty (true)]
  223. [WebCategory ("Appearance")]
  224. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  225. [WebSysDescription ("The font of this WebControl.")]
  226. public FontInfo Font {
  227. get {
  228. if (font==null)
  229. font = new FontInfo (this);
  230. return font;
  231. }
  232. }
  233. protected internal virtual bool IsEmpty
  234. {
  235. get { return (selectionBits == 0); }
  236. }
  237. private void AddColor (CssStyleCollection attributes, HtmlTextWriterStyle style, Color color)
  238. {
  239. if (!color.IsEmpty)
  240. attributes.Add (style, ColorTranslator.ToHtml (color));
  241. }
  242. private static string StringArrayToString (string [] array, char separator)
  243. {
  244. if (array.Length == 0)
  245. return String.Empty;
  246. StringBuilder sb = new StringBuilder ();
  247. for (int i = 0; i < array.Length; i++) {
  248. if (i == 0) {
  249. sb.Append (array [0]);
  250. } else {
  251. sb.Append (separator);
  252. sb.Append (array [i]);
  253. }
  254. }
  255. return sb.ToString ();
  256. }
  257. public void AddAttributesToRender (HtmlTextWriter writer)
  258. {
  259. AddAttributesToRender (writer, null);
  260. }
  261. public virtual void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
  262. {
  263. if (IsSet (CSSCLASS)) {
  264. string cssClass = (string) ViewState ["CssClass"];
  265. if (cssClass.Length > 0)
  266. writer.AddAttribute (HtmlTextWriterAttribute.Class, cssClass);
  267. }
  268. CssStyleCollection ats = new CssStyleCollection ();
  269. #if NET_2_0
  270. FillStyleAttributes (ats, owner);
  271. #else
  272. FillAttributes (ats);
  273. #endif
  274. foreach (string key in ats.Keys)
  275. writer.AddStyleAttribute (key, ats [key]);
  276. }
  277. #if NET_2_0
  278. public CssStyleCollection FillStyleAttributes (IUrlResolutionService urlResolver)
  279. {
  280. CssStyleCollection ats = new CssStyleCollection ();
  281. FillStyleAttributes (ats, urlResolver);
  282. return ats;
  283. }
  284. protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
  285. {
  286. FillAttributes (attributes);
  287. }
  288. #endif
  289. void FillAttributes (CssStyleCollection attributes)
  290. {
  291. if (IsSet (BACKCOLOR))
  292. AddColor (attributes, HtmlTextWriterStyle.BackgroundColor, BackColor);
  293. if (IsSet(BORDERCOLOR))
  294. AddColor (attributes, HtmlTextWriterStyle.BorderColor, BorderColor);
  295. if (IsSet (FORECOLOR))
  296. AddColor (attributes, HtmlTextWriterStyle.Color, ForeColor);
  297. if (!BorderWidth.IsEmpty) {
  298. attributes.Add (HtmlTextWriterStyle.BorderWidth,
  299. BorderWidth.ToString (CultureInfo.InvariantCulture));
  300. if (BorderStyle != BorderStyle.NotSet) {
  301. attributes.Add (HtmlTextWriterStyle.BorderStyle,
  302. Enum.Format (typeof (BorderStyle), BorderStyle, "G"));
  303. } else {
  304. if (BorderWidth.Value != 0.0)
  305. attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
  306. }
  307. } else {
  308. if (BorderStyle != BorderStyle.NotSet)
  309. attributes.Add (HtmlTextWriterStyle.BorderStyle,
  310. Enum.Format (typeof (BorderStyle), BorderStyle, "G"));
  311. }
  312. if (Font.Names.Length > 0)
  313. attributes.Add (HtmlTextWriterStyle.FontFamily,
  314. StringArrayToString (Font.Names, ','));
  315. if (!Font.Size.IsEmpty)
  316. attributes.Add (HtmlTextWriterStyle.FontSize,
  317. Font.Size.ToString (CultureInfo.InvariantCulture));
  318. if (Font.Bold)
  319. attributes.Add (HtmlTextWriterStyle.FontWeight, "bold");
  320. if (Font.Italic)
  321. attributes.Add (HtmlTextWriterStyle.FontStyle, "italic");
  322. string textDecoration = String.Empty;
  323. if (Font.Strikeout)
  324. textDecoration += " line-through";
  325. if (Font.Underline)
  326. textDecoration += " underline";
  327. if (Font.Overline)
  328. textDecoration += " overline";
  329. if (textDecoration.Length > 0)
  330. attributes.Add (HtmlTextWriterStyle.TextDecoration, textDecoration);
  331. Unit u = Unit.Empty;
  332. if (IsSet (HEIGHT)) {
  333. u = (Unit) ViewState ["Height"];
  334. if (!u.IsEmpty)
  335. attributes.Add (HtmlTextWriterStyle.Height,
  336. u.ToString (CultureInfo.InvariantCulture));
  337. }
  338. if (IsSet (WIDTH)) {
  339. u = (Unit) ViewState ["Width"];
  340. if (!u.IsEmpty)
  341. attributes.Add (HtmlTextWriterStyle.Width,
  342. u.ToString (CultureInfo.InvariantCulture));
  343. }
  344. }
  345. public virtual void CopyFrom (Style source)
  346. {
  347. if (source == null || source.IsEmpty)
  348. return;
  349. Font.CopyFrom (source.Font);
  350. if (source.IsSet (HEIGHT)&& (source.Height != Unit.Empty))
  351. Height = source.Height;
  352. if (source.IsSet (WIDTH)&& (source.Width != Unit.Empty))
  353. Width = source.Width;
  354. if (source.IsSet (BORDERCOLOR)&& (source.BorderColor != Color.Empty))
  355. BorderColor = source.BorderColor;
  356. if (source.IsSet (BORDERWIDTH)&& (source.BorderWidth != Unit.Empty))
  357. BorderWidth = source.BorderWidth;
  358. if (source.IsSet (BORDERSTYLE))
  359. BorderStyle = source.BorderStyle;
  360. if (source.IsSet (BACKCOLOR)&& (source.BackColor != Color.Empty))
  361. BackColor = source.BackColor;
  362. if (source.IsSet (CSSCLASS))
  363. CssClass = source.CssClass;
  364. if (source.IsSet (FORECOLOR)&& (source.ForeColor != Color.Empty))
  365. ForeColor = source.ForeColor;
  366. }
  367. public virtual void MergeWith (Style with)
  368. {
  369. if (with == null || with.IsEmpty)
  370. return;
  371. if (IsEmpty) {
  372. CopyFrom (with);
  373. return;
  374. }
  375. Font.MergeWith (with.Font);
  376. if (!IsSet (HEIGHT) && with.Height != Unit.Empty)
  377. Height = with.Height;
  378. if (!IsSet(WIDTH) && with.Width != Unit.Empty)
  379. Width = with.Width;
  380. if (!IsSet (BORDERCOLOR) && with.BorderColor != Color.Empty)
  381. BorderColor = with.BorderColor;
  382. if (!IsSet (BORDERWIDTH) && with.BorderWidth != Unit.Empty)
  383. BorderWidth = with.BorderWidth;
  384. if (!IsSet (BORDERSTYLE) && with.BorderStyle != BorderStyle.NotSet)
  385. BorderStyle = with.BorderStyle;
  386. if (!IsSet (BACKCOLOR) && with.BackColor != Color.Empty)
  387. BackColor = with.BackColor;
  388. if (!IsSet (CSSCLASS) && with.CssClass != String.Empty)
  389. CssClass = with.CssClass;
  390. if (!IsSet (FORECOLOR) && with.ForeColor != Color.Empty)
  391. ForeColor = with.ForeColor;
  392. }
  393. public virtual void Reset ()
  394. {
  395. if (IsSet (BACKCOLOR))
  396. ViewState.Remove ("BackColor");
  397. if (IsSet (BORDERCOLOR))
  398. ViewState.Remove ("BorderColor");
  399. if (IsSet (BORDERSTYLE))
  400. ViewState.Remove ("BorderStyle");
  401. if (IsSet (BORDERWIDTH))
  402. ViewState.Remove ("BorderWidth");
  403. if (IsSet (CSSCLASS))
  404. ViewState.Remove ("CssClass");
  405. if (IsSet (FORECOLOR))
  406. ViewState.Remove ("ForeColor");
  407. if (IsSet (HEIGHT))
  408. ViewState.Remove ("Height");
  409. if (IsSet (WIDTH))
  410. ViewState.Remove( "Width");
  411. if (font != null)
  412. font.Reset ();
  413. selectionBits = 0x00;
  414. }
  415. protected bool IsTrackingViewState {
  416. get { return marked; }
  417. }
  418. protected internal virtual void TrackViewState ()
  419. {
  420. if (selfStateBag)
  421. ViewState.TrackViewState ();
  422. marked = true;
  423. }
  424. protected internal virtual object SaveViewState ()
  425. {
  426. if (viewState != null) {
  427. if (marked && IsSet (MARKED))
  428. ViewState [selectionBitString] = selectionBits;
  429. if (selfStateBag)
  430. return ViewState.SaveViewState ();
  431. }
  432. return null;
  433. }
  434. protected internal void LoadViewState (object state)
  435. {
  436. if (state != null && selfStateBag)
  437. ViewState.LoadViewState (state);
  438. if (viewState != null) {
  439. object o = ViewState [selectionBitString];
  440. if (o != null)
  441. selectionBits = (int) o;
  442. }
  443. }
  444. void IStateManager.LoadViewState(object state)
  445. {
  446. LoadViewState(state);
  447. }
  448. object IStateManager.SaveViewState ()
  449. {
  450. return SaveViewState ();
  451. }
  452. void IStateManager.TrackViewState ()
  453. {
  454. TrackViewState ();
  455. }
  456. bool IStateManager.IsTrackingViewState {
  457. get { return IsTrackingViewState; }
  458. }
  459. public override string ToString ()
  460. {
  461. return String.Empty;
  462. }
  463. protected internal void SetBit (int bit)
  464. {
  465. Set (bit);
  466. }
  467. internal virtual void CopyTextStylesFrom (Style source)
  468. {
  469. if (source.IsSet (FORECOLOR)&& (source.ForeColor != Color.Empty))
  470. ForeColor = source.ForeColor;
  471. }
  472. #if NET_2_0
  473. public void SetDirty ()
  474. {
  475. if (selectionBits != 0x00)
  476. Set (MARKED);
  477. if (viewState != null)
  478. viewState.SetDirty ();
  479. }
  480. public string RegisteredCssClass {
  481. get { return regClass; }
  482. }
  483. internal void SetRegisteredCssClass (string name)
  484. {
  485. regClass = name;
  486. }
  487. #endif
  488. }
  489. }