Style.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. private FontInfo font;
  67. public Style ()
  68. {
  69. Initialize(null);
  70. selfStateBag = true;
  71. }
  72. public Style (StateBag bag)
  73. {
  74. Initialize (bag);
  75. selfStateBag = false;
  76. }
  77. private void Initialize (StateBag bag)
  78. {
  79. viewState = bag;
  80. selectionBits = 0x00;
  81. }
  82. [Browsable (false)]
  83. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  84. protected internal StateBag ViewState {
  85. get {
  86. if (viewState == null) {
  87. viewState = new StateBag (false);
  88. if (IsTrackingViewState)
  89. viewState.TrackViewState ();
  90. }
  91. return viewState;
  92. }
  93. }
  94. internal bool IsSet (int bit)
  95. {
  96. return ((selectionBits & bit) != 0x00);
  97. }
  98. internal virtual void Set (int bit)
  99. {
  100. selectionBits |= bit;
  101. if (IsTrackingViewState)
  102. selectionBits |= MARKED;
  103. }
  104. [NotifyParentProperty (true)]
  105. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  106. [TypeConverter (typeof (WebColorConverter))]
  107. [WebSysDescription ("The background color for the WebControl.")]
  108. public Color BackColor {
  109. get {
  110. if(IsSet(BACKCOLOR))
  111. return (Color)ViewState["BackColor"];
  112. return Color.Empty;
  113. }
  114. set {
  115. ViewState["BackColor"] = value;
  116. Set(BACKCOLOR);
  117. }
  118. }
  119. [NotifyParentProperty (true)]
  120. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  121. [TypeConverter (typeof (WebColorConverter))]
  122. [WebSysDescription ("The border color for the WebControl.")]
  123. public Color BorderColor {
  124. get {
  125. if (IsSet (BORDERCOLOR))
  126. return (Color) ViewState ["BorderColor"];
  127. return Color.Empty;
  128. }
  129. set {
  130. ViewState ["BorderColor"] = value;
  131. Set (BORDERCOLOR);
  132. }
  133. }
  134. [NotifyParentProperty (true)]
  135. [DefaultValue (typeof(BorderStyle), "NotSet"), Bindable (true), WebCategory ("Appearance")]
  136. [WebSysDescription ("The style/type of the border used for the WebControl.")]
  137. public BorderStyle BorderStyle {
  138. get {
  139. if (IsSet (BORDERSTYLE))
  140. return (BorderStyle) ViewState ["BorderStyle"];
  141. return BorderStyle.NotSet;
  142. }
  143. set {
  144. ViewState ["BorderStyle"] = value;
  145. Set (BORDERSTYLE);
  146. }
  147. }
  148. [NotifyParentProperty (true)]
  149. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  150. [WebSysDescription ("The width of the border used for the WebControl.")]
  151. public Unit BorderWidth {
  152. get {
  153. if (IsSet (BORDERWIDTH))
  154. return (Unit) ViewState ["BorderWidth"];
  155. return Unit.Empty;
  156. }
  157. set {
  158. ViewState ["BorderWidth"] = value;
  159. Set (BORDERWIDTH);
  160. }
  161. }
  162. [NotifyParentProperty (true)]
  163. [DefaultValue (""), WebCategory ("Appearance")]
  164. [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
  165. public string CssClass {
  166. get {
  167. if (IsSet (CSSCLASS))
  168. return (string) ViewState["CssClass"];
  169. return string.Empty;
  170. }
  171. set {
  172. ViewState ["CssClass"] = value;
  173. Set (CSSCLASS);
  174. }
  175. }
  176. [NotifyParentProperty (true)]
  177. [DefaultValue (null), Bindable (true), WebCategory ("Appearance")]
  178. [TypeConverter (typeof (WebColorConverter))]
  179. [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
  180. public Color ForeColor {
  181. get {
  182. if (IsSet (FORECOLOR))
  183. return (Color) ViewState ["ForeColor"];
  184. return Color.Empty;
  185. }
  186. set {
  187. ViewState ["ForeColor"] = value;
  188. Set (FORECOLOR);
  189. }
  190. }
  191. [NotifyParentProperty (true)]
  192. [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
  193. [WebSysDescription ("The height of this WebControl.")]
  194. public Unit Height {
  195. get {
  196. if (IsSet (HEIGHT))
  197. return (Unit) ViewState ["Height"];
  198. return Unit.Empty;
  199. }
  200. set {
  201. ViewState ["Height"] = value;
  202. Set (HEIGHT);
  203. }
  204. }
  205. [NotifyParentProperty (true)]
  206. [DefaultValue (null), Bindable (true), WebCategory ("Layout")]
  207. [WebSysDescription ("The width of this WebControl.")]
  208. public Unit Width {
  209. get {
  210. if (IsSet(WIDTH))
  211. return (Unit) ViewState ["Width"];
  212. return Unit.Empty;
  213. }
  214. set {
  215. ViewState ["Width"] = value;
  216. Set (WIDTH);
  217. }
  218. }
  219. [NotifyParentProperty (true)]
  220. [WebCategory ("Appearance")]
  221. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  222. [WebSysDescription ("The font of this WebControl.")]
  223. public FontInfo Font {
  224. get {
  225. if (font==null)
  226. font = new FontInfo (this);
  227. return font;
  228. }
  229. }
  230. protected internal virtual bool IsEmpty
  231. {
  232. get { return (selectionBits == 0); }
  233. }
  234. private void AddColor (HtmlTextWriter writer, HtmlTextWriterStyle style, Color color)
  235. {
  236. if (!color.IsEmpty)
  237. writer.AddStyleAttribute (style, ColorTranslator.ToHtml (color));
  238. }
  239. private static string StringArrayToString (string [] array, char separator)
  240. {
  241. if (array.Length == 0)
  242. return String.Empty;
  243. StringBuilder sb = new StringBuilder ();
  244. for (int i = 0; i < array.Length; i++) {
  245. if (i == 0) {
  246. sb.Append (array [0]);
  247. } else {
  248. sb.Append (separator);
  249. sb.Append (array [i]);
  250. }
  251. }
  252. return sb.ToString ();
  253. }
  254. public void AddAttributesToRender (HtmlTextWriter writer)
  255. {
  256. AddAttributesToRender (writer, null);
  257. }
  258. public virtual void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
  259. {
  260. if (IsSet (BACKCOLOR))
  261. AddColor (writer, HtmlTextWriterStyle.BackgroundColor, BackColor);
  262. if (IsSet(BORDERCOLOR))
  263. AddColor (writer, HtmlTextWriterStyle.BorderColor, BorderColor);
  264. if (IsSet (FORECOLOR))
  265. AddColor (writer, HtmlTextWriterStyle.Color, ForeColor);
  266. if (IsSet (CSSCLASS)) {
  267. string cssClass = (string) ViewState ["CssClass"];
  268. if (cssClass.Length > 0)
  269. writer.AddAttribute (HtmlTextWriterAttribute.Class, cssClass);
  270. }
  271. if (!BorderWidth.IsEmpty) {
  272. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth,
  273. BorderWidth.ToString (CultureInfo.InvariantCulture));
  274. if (BorderStyle != BorderStyle.NotSet) {
  275. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
  276. Enum.Format (typeof (BorderStyle), BorderStyle, "G"));
  277. } else {
  278. if (BorderWidth.Value != 0.0)
  279. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
  280. "solid");
  281. }
  282. } else {
  283. if (BorderStyle != BorderStyle.NotSet)
  284. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle,
  285. Enum.Format (typeof (BorderStyle), BorderStyle, "G"));
  286. }
  287. if (Font.Names.Length > 0)
  288. writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily,
  289. StringArrayToString (Font.Names, ','));
  290. if (!Font.Size.IsEmpty)
  291. writer.AddStyleAttribute (HtmlTextWriterStyle.FontSize,
  292. Font.Size.ToString (CultureInfo.InvariantCulture));
  293. if (Font.Bold)
  294. writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
  295. if (Font.Italic)
  296. writer.AddStyleAttribute (HtmlTextWriterStyle.FontStyle, "italic");
  297. string textDecoration = String.Empty;
  298. if (Font.Strikeout)
  299. textDecoration += " line-through";
  300. if (Font.Underline)
  301. textDecoration += " underline";
  302. if (Font.Overline)
  303. textDecoration += " overline";
  304. if (textDecoration.Length > 0)
  305. writer.AddStyleAttribute( HtmlTextWriterStyle.TextDecoration, textDecoration);
  306. Unit u = Unit.Empty;
  307. if (IsSet (HEIGHT)) {
  308. u = (Unit) ViewState ["Height"];
  309. if (!u.IsEmpty)
  310. writer.AddStyleAttribute (HtmlTextWriterStyle.Height,
  311. u.ToString (CultureInfo.InvariantCulture));
  312. }
  313. if (IsSet (WIDTH)) {
  314. u = (Unit) ViewState ["Width"];
  315. if (!u.IsEmpty)
  316. writer.AddStyleAttribute (HtmlTextWriterStyle.Width,
  317. u.ToString (CultureInfo.InvariantCulture));
  318. }
  319. }
  320. public virtual void CopyFrom (Style source)
  321. {
  322. if (source == null || source.IsEmpty)
  323. return;
  324. Font.CopyFrom (source.Font);
  325. if (source.IsSet (HEIGHT)&& (source.Height != Unit.Empty))
  326. Height = source.Height;
  327. if (source.IsSet (WIDTH)&& (source.Width != Unit.Empty))
  328. Width = source.Width;
  329. if (source.IsSet (BORDERCOLOR)&& (source.BorderColor != Color.Empty))
  330. BorderColor = source.BorderColor;
  331. if (source.IsSet (BORDERWIDTH)&& (source.BorderWidth != Unit.Empty))
  332. BorderWidth = source.BorderWidth;
  333. if (source.IsSet (BORDERSTYLE))
  334. BorderStyle = source.BorderStyle;
  335. if (source.IsSet (BACKCOLOR)&& (source.BackColor != Color.Empty))
  336. BackColor = source.BackColor;
  337. if (source.IsSet (CSSCLASS))
  338. CssClass = source.CssClass;
  339. if (source.IsSet (FORECOLOR)&& (source.ForeColor != Color.Empty))
  340. ForeColor = source.ForeColor;
  341. }
  342. public virtual void MergeWith (Style with)
  343. {
  344. if (with == null || with.IsEmpty)
  345. return;
  346. if (IsEmpty) {
  347. CopyFrom (with);
  348. return;
  349. }
  350. Font.MergeWith (with.Font);
  351. if (!IsSet (HEIGHT) && with.Height != Unit.Empty)
  352. Height = with.Height;
  353. if (!IsSet(WIDTH) && with.Width != Unit.Empty)
  354. Width = with.Width;
  355. if (!IsSet (BORDERCOLOR) && with.BorderColor != Color.Empty)
  356. BorderColor = with.BorderColor;
  357. if (!IsSet (BORDERWIDTH) && with.BorderWidth != Unit.Empty)
  358. BorderWidth = with.BorderWidth;
  359. if (!IsSet (BORDERSTYLE) && with.BorderStyle != BorderStyle.NotSet)
  360. BorderStyle = with.BorderStyle;
  361. if (!IsSet (BACKCOLOR) && with.BackColor != Color.Empty)
  362. BackColor = with.BackColor;
  363. if (!IsSet (CSSCLASS) && with.CssClass != String.Empty)
  364. CssClass = with.CssClass;
  365. if (!IsSet (FORECOLOR) && with.ForeColor != Color.Empty)
  366. ForeColor = with.ForeColor;
  367. }
  368. public virtual void Reset ()
  369. {
  370. if (IsSet (BACKCOLOR))
  371. ViewState.Remove ("BackColor");
  372. if (IsSet (BORDERCOLOR))
  373. ViewState.Remove ("BorderColor");
  374. if (IsSet (BORDERSTYLE))
  375. ViewState.Remove ("BorderStyle");
  376. if (IsSet (BORDERWIDTH))
  377. ViewState.Remove ("BorderWidth");
  378. if (IsSet (CSSCLASS))
  379. ViewState.Remove ("CssClass");
  380. if (IsSet (FORECOLOR))
  381. ViewState.Remove ("ForeColor");
  382. if (IsSet (HEIGHT))
  383. ViewState.Remove ("Height");
  384. if (IsSet (WIDTH))
  385. ViewState.Remove( "Width");
  386. if (font != null)
  387. font.Reset ();
  388. selectionBits = 0x00;
  389. }
  390. protected bool IsTrackingViewState {
  391. get { return marked; }
  392. }
  393. protected internal virtual void TrackViewState ()
  394. {
  395. if (selfStateBag)
  396. ViewState.TrackViewState ();
  397. marked = true;
  398. }
  399. protected internal virtual object SaveViewState ()
  400. {
  401. if (viewState != null) {
  402. if (marked && IsSet (MARKED))
  403. ViewState [selectionBitString] = selectionBits;
  404. if (selfStateBag)
  405. return ViewState.SaveViewState ();
  406. }
  407. return null;
  408. }
  409. protected internal void LoadViewState (object state)
  410. {
  411. if (state != null && selfStateBag)
  412. ViewState.LoadViewState (state);
  413. if (viewState != null) {
  414. object o = ViewState [selectionBitString];
  415. if (o != null)
  416. selectionBits = (int) o;
  417. }
  418. }
  419. void IStateManager.LoadViewState(object state)
  420. {
  421. LoadViewState(state);
  422. }
  423. object IStateManager.SaveViewState ()
  424. {
  425. return SaveViewState ();
  426. }
  427. void IStateManager.TrackViewState ()
  428. {
  429. TrackViewState ();
  430. }
  431. bool IStateManager.IsTrackingViewState {
  432. get { return IsTrackingViewState; }
  433. }
  434. public override string ToString ()
  435. {
  436. return String.Empty;
  437. }
  438. protected internal void SetBit (int bit)
  439. {
  440. Set (bit);
  441. }
  442. }
  443. }