Style.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Dennis Bartok ([email protected])
  24. //
  25. //
  26. using System.ComponentModel;
  27. using System.Drawing;
  28. using System.Security.Permissions;
  29. namespace System.Web.UI.WebControls {
  30. // CAS
  31. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  32. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. // attributes
  34. #if NET_2_0
  35. // Not until we actually have StyleConverter
  36. // [TypeConverter(typeof(System.Web.UI.WebControls.StyleConverter))]
  37. #else
  38. [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
  39. #endif
  40. [ToolboxItem("")]
  41. public class Style : System.ComponentModel.Component, System.Web.UI.IStateManager
  42. {
  43. internal const string BitStateKey = "_!SB";
  44. [Flags]
  45. public enum Styles
  46. {
  47. BackColor = 0x00000008,
  48. BorderColor = 0x00000010,
  49. BorderStyle = 0x00000040,
  50. BorderWidth = 0x00000020,
  51. CssClass = 0x00000002,
  52. Font = 0x00000001,
  53. ForeColor = 0x00000004,
  54. Height = 0x00000080,
  55. Width = 0x00000100,
  56. FontAll = 0xFE00,
  57. FontBold = 0x800,
  58. FontItalic = 0x1000,
  59. FontNames = 0x200,
  60. FontOverline = 0x4000,
  61. FontSize = 0x400,
  62. FontStrikeout = 0x8000,
  63. FontUnderline = 0x2000
  64. }
  65. #region Fields
  66. private int styles;
  67. private int stylesTraked;
  68. internal StateBag viewstate;
  69. private FontInfo fontinfo;
  70. private bool tracking;
  71. bool _isSharedViewState;
  72. #if NET_2_0
  73. private string registered_class;
  74. #endif
  75. #endregion // Fields
  76. #region Public Constructors
  77. public Style()
  78. {
  79. viewstate = new StateBag ();
  80. }
  81. public Style(System.Web.UI.StateBag bag)
  82. {
  83. viewstate = bag;
  84. if (viewstate == null)
  85. viewstate = new StateBag ();
  86. _isSharedViewState = true;
  87. }
  88. #endregion // Public Constructors
  89. #region Public Instance Properties
  90. #if !NET_2_0
  91. [Bindable(true)]
  92. #endif
  93. [DefaultValue(typeof (Color), "")]
  94. [NotifyParentProperty(true)]
  95. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  96. [WebSysDescription ("")]
  97. [WebCategory ("Appearance")]
  98. public Color BackColor
  99. {
  100. get
  101. {
  102. if (!CheckBit ((int) Styles.BackColor))
  103. {
  104. return Color.Empty;
  105. }
  106. return (Color)viewstate["BackColor"];
  107. }
  108. set
  109. {
  110. viewstate["BackColor"] = value;
  111. SetBit ((int) Styles.BackColor);
  112. }
  113. }
  114. #if !NET_2_0
  115. [Bindable(true)]
  116. #endif
  117. [DefaultValue(typeof (Color), "")]
  118. [NotifyParentProperty(true)]
  119. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  120. [WebSysDescription ("")]
  121. [WebCategory ("Appearance")]
  122. public Color BorderColor
  123. {
  124. get
  125. {
  126. if (!CheckBit ((int) Styles.BorderColor))
  127. {
  128. return Color.Empty;
  129. }
  130. return (Color)viewstate["BorderColor"];
  131. }
  132. set
  133. {
  134. viewstate["BorderColor"] = value;
  135. SetBit ((int) Styles.BorderColor);
  136. }
  137. }
  138. #if !NET_2_0
  139. [Bindable(true)]
  140. #endif
  141. [DefaultValue(BorderStyle.NotSet)]
  142. [NotifyParentProperty(true)]
  143. [WebSysDescription ("")]
  144. [WebCategory ("Appearance")]
  145. public BorderStyle BorderStyle
  146. {
  147. get
  148. {
  149. if (!CheckBit ((int) Styles.BorderStyle))
  150. {
  151. return BorderStyle.NotSet;
  152. }
  153. return (BorderStyle)viewstate["BorderStyle"];
  154. }
  155. set
  156. {
  157. viewstate["BorderStyle"] = value;
  158. SetBit ((int) Styles.BorderStyle);
  159. }
  160. }
  161. #if !NET_2_0
  162. [Bindable(true)]
  163. #endif
  164. [DefaultValue(typeof (Unit), "")]
  165. [NotifyParentProperty(true)]
  166. [WebSysDescription ("")]
  167. [WebCategory ("Appearance")]
  168. public Unit BorderWidth
  169. {
  170. get
  171. {
  172. if (!CheckBit ((int) Styles.BorderWidth))
  173. {
  174. return Unit.Empty;
  175. }
  176. return (Unit)viewstate["BorderWidth"];
  177. }
  178. set
  179. {
  180. if (value.Value < 0)
  181. {
  182. throw new ArgumentOutOfRangeException("Value", value.Value, "BorderWidth must not be negative");
  183. }
  184. viewstate["BorderWidth"] = value;
  185. SetBit ((int) Styles.BorderWidth);
  186. }
  187. }
  188. [DefaultValue("")]
  189. [NotifyParentProperty(true)]
  190. [WebSysDescription ("")]
  191. [WebCategory ("Appearance")]
  192. public string CssClass
  193. {
  194. get
  195. {
  196. if (!CheckBit ((int) Styles.CssClass))
  197. {
  198. return string.Empty;
  199. }
  200. return (string)viewstate["CssClass"];
  201. }
  202. set
  203. {
  204. viewstate["CssClass"] = value;
  205. SetBit ((int) Styles.CssClass);
  206. }
  207. }
  208. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  209. [NotifyParentProperty(true)]
  210. [WebSysDescription ("")]
  211. [WebCategory ("Appearance")]
  212. public FontInfo Font
  213. {
  214. get
  215. {
  216. if (fontinfo == null)
  217. {
  218. fontinfo = new FontInfo(this);
  219. }
  220. return fontinfo;
  221. }
  222. }
  223. #if !NET_2_0
  224. [Bindable(true)]
  225. #endif
  226. [DefaultValue(typeof (Color), "")]
  227. [NotifyParentProperty(true)]
  228. [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
  229. [WebSysDescription ("")]
  230. [WebCategory ("Appearance")]
  231. public Color ForeColor
  232. {
  233. get
  234. {
  235. if (!CheckBit ((int) Styles.ForeColor))
  236. {
  237. return Color.Empty;
  238. }
  239. return (Color)viewstate["ForeColor"];
  240. }
  241. set
  242. {
  243. viewstate["ForeColor"] = value;
  244. SetBit ((int) Styles.ForeColor);
  245. }
  246. }
  247. #if !NET_2_0
  248. [Bindable(true)]
  249. #endif
  250. [DefaultValue(typeof (Unit), "")]
  251. [NotifyParentProperty(true)]
  252. [WebSysDescription ("")]
  253. [WebCategory ("Appearance")]
  254. public Unit Height
  255. {
  256. get
  257. {
  258. if (!CheckBit ((int) Styles.Height))
  259. {
  260. return Unit.Empty;
  261. }
  262. return (Unit)viewstate["Height"];
  263. }
  264. set
  265. {
  266. if (value.Value < 0)
  267. {
  268. throw new ArgumentOutOfRangeException("Value", value.Value, "Height must not be negative");
  269. }
  270. viewstate["Height"] = value;
  271. SetBit ((int) Styles.Height);
  272. }
  273. }
  274. #if !NET_2_0
  275. [Bindable(true)]
  276. #endif
  277. [DefaultValue(typeof (Unit), "")]
  278. [NotifyParentProperty(true)]
  279. [WebSysDescription ("")]
  280. [WebCategory ("Appearance")]
  281. public Unit Width
  282. {
  283. get
  284. {
  285. if (!CheckBit ((int) Styles.Width))
  286. {
  287. return Unit.Empty;
  288. }
  289. return (Unit)viewstate["Width"];
  290. }
  291. set
  292. {
  293. if (value.Value < 0)
  294. {
  295. throw new ArgumentOutOfRangeException("Value", value.Value, "Width must not be negative");
  296. }
  297. viewstate["Width"] = value;
  298. SetBit ((int) Styles.Width);
  299. }
  300. }
  301. #endregion // Public Instance Properties
  302. #region Protected Instance Properties
  303. #if NET_2_0
  304. [Browsable (false)]
  305. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  306. public virtual bool IsEmpty
  307. #else
  308. protected internal virtual bool IsEmpty
  309. #endif
  310. {
  311. get
  312. {
  313. #if NET_2_0
  314. return (styles == 0 && RegisteredCssClass.Length == 0);
  315. #else
  316. return (styles == 0);
  317. #endif
  318. }
  319. }
  320. protected bool IsTrackingViewState
  321. {
  322. get
  323. {
  324. return tracking;
  325. }
  326. }
  327. [Browsable(false)]
  328. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  329. protected internal StateBag ViewState
  330. {
  331. get
  332. {
  333. return viewstate;
  334. }
  335. }
  336. #endregion // Protected Instance Properties
  337. #region Internal Instance Properties
  338. internal bool AlwaysRenderTextDecoration
  339. {
  340. get
  341. {
  342. if (viewstate["AlwaysRenderTextDecoration"] == null)
  343. return false;
  344. return (bool)viewstate["AlwaysRenderTextDecoration"];
  345. }
  346. set
  347. {
  348. viewstate["AlwaysRenderTextDecoration"] = value;
  349. }
  350. }
  351. #endregion // Internal Instance Properties
  352. #region Public Instance Methods
  353. public void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
  354. {
  355. AddAttributesToRender(writer, null);
  356. }
  357. public virtual void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer, WebControl owner)
  358. {
  359. #if NET_2_0
  360. if (RegisteredCssClass.Length > 0) {
  361. if (CssClass.Length > 0)
  362. writer.AddAttribute (HtmlTextWriterAttribute.Class, CssClass + " " + RegisteredCssClass);
  363. else
  364. writer.AddAttribute (HtmlTextWriterAttribute.Class, RegisteredCssClass);
  365. }
  366. else
  367. #endif
  368. {
  369. if (CssClass.Length > 0)
  370. writer.AddAttribute (HtmlTextWriterAttribute.Class, CssClass);
  371. #if NET_2_0
  372. CssStyleCollection col = new CssStyleCollection (new StateBag ());
  373. FillStyleAttributes (col, owner);
  374. foreach (string key in col.Keys) {
  375. writer.AddStyleAttribute (key, col [key]);
  376. }
  377. #else
  378. WriteStyleAttributes (writer);
  379. #endif
  380. }
  381. }
  382. #if ONLY_1_1
  383. void WriteStyleAttributes (HtmlTextWriter writer)
  384. {
  385. string s;
  386. Color color;
  387. BorderStyle bs;
  388. Unit u;
  389. if (CheckBit ((int) Styles.BackColor)) {
  390. color = (Color)viewstate["BackColor"];
  391. if (!color.IsEmpty)
  392. writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
  393. }
  394. if (CheckBit ((int) Styles.BorderColor)) {
  395. color = (Color)viewstate["BorderColor"];
  396. if (!color.IsEmpty)
  397. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
  398. }
  399. bool have_width = false;
  400. if (CheckBit ((int) Styles.BorderWidth)) {
  401. u = (Unit)viewstate["BorderWidth"];
  402. if (!u.IsEmpty) {
  403. if (u.Value > 0)
  404. have_width = true;
  405. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, u.ToString());
  406. }
  407. }
  408. if (CheckBit ((int) Styles.BorderStyle)) {
  409. bs = (BorderStyle)viewstate["BorderStyle"];
  410. if (bs != BorderStyle.NotSet)
  411. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, bs.ToString());
  412. else {
  413. if (CheckBit ((int) Styles.BorderWidth))
  414. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "solid");
  415. }
  416. } else if (have_width) {
  417. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "solid");
  418. }
  419. if (CheckBit ((int) Styles.ForeColor)) {
  420. color = (Color)viewstate["ForeColor"];
  421. if (!color.IsEmpty)
  422. writer.AddStyleAttribute (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
  423. }
  424. if (CheckBit ((int) Styles.Height)) {
  425. u = (Unit)viewstate["Height"];
  426. if (!u.IsEmpty)
  427. writer.AddStyleAttribute (HtmlTextWriterStyle.Height, u.ToString());
  428. }
  429. if (CheckBit ((int) Styles.Width)) {
  430. u = (Unit)viewstate["Width"];
  431. if (!u.IsEmpty)
  432. writer.AddStyleAttribute (HtmlTextWriterStyle.Width, u.ToString());
  433. }
  434. if (CheckBit ((int) Style.Styles.FontAll)) {
  435. // Fonts are a bit weird
  436. if (fontinfo.Name != string.Empty) {
  437. s = fontinfo.Names[0];
  438. for (int i = 1; i < fontinfo.Names.Length; i++)
  439. s += "," + fontinfo.Names[i];
  440. writer.AddStyleAttribute (HtmlTextWriterStyle.FontFamily, s);
  441. }
  442. if (fontinfo.Bold)
  443. writer.AddStyleAttribute (HtmlTextWriterStyle.FontWeight, "bold");
  444. if (fontinfo.Italic)
  445. writer.AddStyleAttribute (HtmlTextWriterStyle.FontStyle, "italic");
  446. if (!fontinfo.Size.IsEmpty)
  447. writer.AddStyleAttribute (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
  448. // These styles are munged into a attribute decoration
  449. s = string.Empty;
  450. if (fontinfo.Overline)
  451. s += "overline ";
  452. if (fontinfo.Strikeout)
  453. s += "line-through ";
  454. if (fontinfo.Underline)
  455. s += "underline ";
  456. s = (s != "") ? s : AlwaysRenderTextDecoration ? "none" : "";
  457. if (s != "")
  458. writer.AddStyleAttribute (HtmlTextWriterStyle.TextDecoration, s);
  459. }
  460. }
  461. #endif
  462. #if NET_2_0
  463. protected virtual void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
  464. {
  465. Color color;
  466. BorderStyle bs;
  467. Unit u;
  468. if (CheckBit ((int) Styles.BackColor))
  469. {
  470. color = (Color)viewstate["BackColor"];
  471. if (!color.IsEmpty)
  472. attributes.Add (HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(color));
  473. }
  474. if (CheckBit ((int) Styles.BorderColor))
  475. {
  476. color = (Color)viewstate["BorderColor"];
  477. if (!color.IsEmpty)
  478. attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
  479. }
  480. bool have_width = false;
  481. if (CheckBit ((int) Styles.BorderWidth)) {
  482. u = (Unit) viewstate ["BorderWidth"];
  483. if (!u.IsEmpty) {
  484. if (u.Value > 0)
  485. have_width = true;
  486. attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString ());
  487. }
  488. }
  489. if (CheckBit ((int) Styles.BorderStyle)) {
  490. bs = (BorderStyle) viewstate ["BorderStyle"];
  491. if (bs != BorderStyle.NotSet)
  492. attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString ());
  493. else if (have_width)
  494. attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
  495. }
  496. else if (have_width) {
  497. attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
  498. }
  499. if (CheckBit ((int) Styles.ForeColor))
  500. {
  501. color = (Color)viewstate["ForeColor"];
  502. if (!color.IsEmpty)
  503. attributes.Add (HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(color));
  504. }
  505. if (CheckBit ((int) Styles.Height))
  506. {
  507. u = (Unit)viewstate["Height"];
  508. if (!u.IsEmpty)
  509. attributes.Add (HtmlTextWriterStyle.Height, u.ToString());
  510. }
  511. if (CheckBit ((int) Styles.Width))
  512. {
  513. u = (Unit)viewstate["Width"];
  514. if (!u.IsEmpty)
  515. attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
  516. }
  517. Font.FillStyleAttributes (attributes, AlwaysRenderTextDecoration);
  518. }
  519. #endif
  520. public virtual void CopyFrom(Style s)
  521. {
  522. if ((s == null) || s.IsEmpty)
  523. {
  524. return;
  525. }
  526. if (s.fontinfo != null)
  527. {
  528. Font.CopyFrom(s.fontinfo);
  529. }
  530. if ((s.CheckBit ((int) Styles.BackColor)) && (s.BackColor != Color.Empty))
  531. {
  532. this.BackColor = s.BackColor;
  533. }
  534. if ((s.CheckBit ((int) Styles.BorderColor)) && (s.BorderColor != Color.Empty))
  535. {
  536. this.BorderColor = s.BorderColor;
  537. }
  538. if ((s.CheckBit ((int) Styles.BorderStyle)) && (s.BorderStyle != BorderStyle.NotSet))
  539. {
  540. this.BorderStyle = s.BorderStyle;
  541. }
  542. if ((s.CheckBit ((int) Styles.BorderWidth)) && (!s.BorderWidth.IsEmpty))
  543. {
  544. this.BorderWidth = s.BorderWidth;
  545. }
  546. if ((s.CheckBit ((int) Styles.CssClass)) && (s.CssClass != string.Empty))
  547. {
  548. this.CssClass = s.CssClass;
  549. }
  550. if ((s.CheckBit ((int) Styles.ForeColor)) && (s.ForeColor != Color.Empty))
  551. {
  552. this.ForeColor = s.ForeColor;
  553. }
  554. if ((s.CheckBit ((int) Styles.Height)) && (!s.Height.IsEmpty))
  555. {
  556. this.Height = s.Height;
  557. }
  558. if ((s.CheckBit ((int) Styles.Width)) && (!s.Width.IsEmpty))
  559. {
  560. this.Width = s.Width;
  561. }
  562. }
  563. public virtual void MergeWith(Style s)
  564. {
  565. if ((s == null) || (s.IsEmpty))
  566. {
  567. return;
  568. }
  569. if (s.fontinfo != null)
  570. {
  571. Font.MergeWith(s.fontinfo);
  572. }
  573. if ((!CheckBit ((int) Styles.BackColor)) && (s.CheckBit ((int) Styles.BackColor)) && (s.BackColor != Color.Empty))
  574. {
  575. this.BackColor = s.BackColor;
  576. }
  577. if ((!CheckBit ((int) Styles.BorderColor)) && (s.CheckBit ((int) Styles.BorderColor)) && (s.BorderColor != Color.Empty))
  578. {
  579. this.BorderColor = s.BorderColor;
  580. }
  581. if ((!CheckBit ((int) Styles.BorderStyle)) && (s.CheckBit ((int) Styles.BorderStyle)) && (s.BorderStyle != BorderStyle.NotSet))
  582. {
  583. this.BorderStyle = s.BorderStyle;
  584. }
  585. if ((!CheckBit ((int) Styles.BorderWidth)) && (s.CheckBit ((int) Styles.BorderWidth)) && (!s.BorderWidth.IsEmpty))
  586. {
  587. this.BorderWidth = s.BorderWidth;
  588. }
  589. if ((!CheckBit ((int) Styles.CssClass)) && (s.CheckBit ((int) Styles.CssClass)) && (s.CssClass != string.Empty))
  590. {
  591. this.CssClass = s.CssClass;
  592. }
  593. if ((!CheckBit ((int) Styles.ForeColor)) && (s.CheckBit ((int) Styles.ForeColor)) && (s.ForeColor != Color.Empty))
  594. {
  595. this.ForeColor = s.ForeColor;
  596. }
  597. if ((!CheckBit ((int) Styles.Height)) && (s.CheckBit ((int) Styles.Height)) && (!s.Height.IsEmpty))
  598. {
  599. this.Height = s.Height;
  600. }
  601. if ((!CheckBit ((int) Styles.Width)) && (s.CheckBit ((int) Styles.Width)) && (!s.Width.IsEmpty))
  602. {
  603. this.Width = s.Width;
  604. }
  605. }
  606. /*
  607. internal void Print ()
  608. {
  609. Console.WriteLine ("BackColor: {0}", BackColor);
  610. Console.WriteLine ("BorderColor: {0}", BorderColor);
  611. Console.WriteLine ("BorderStyle: {0}", BorderStyle);
  612. Console.WriteLine ("BorderWidth: {0}", BorderWidth);
  613. Console.WriteLine ("CssClass: {0}", CssClass);
  614. Console.WriteLine ("ForeColor: {0}", ForeColor);
  615. Console.WriteLine ("Height: {0}", Height);
  616. Console.WriteLine ("Width: {0}", Width);
  617. }
  618. */
  619. public virtual void Reset()
  620. {
  621. viewstate.Remove("BackColor");
  622. viewstate.Remove("BorderColor");
  623. viewstate.Remove("BorderStyle");
  624. viewstate.Remove("BorderWidth");
  625. viewstate.Remove("CssClass");
  626. viewstate.Remove("ForeColor");
  627. viewstate.Remove("Height");
  628. viewstate.Remove("Width");
  629. if (fontinfo != null)
  630. {
  631. fontinfo.Reset();
  632. }
  633. styles = 0;
  634. viewstate.Remove (BitStateKey);
  635. stylesTraked = 0;
  636. }
  637. #if ONLY_1_1
  638. public override string ToString()
  639. {
  640. return string.Empty;
  641. }
  642. #endif
  643. #endregion // Public Instance Methods
  644. #region Protected Instance Methods
  645. protected internal void LoadViewState(object state)
  646. {
  647. viewstate.LoadViewState(state);
  648. LoadBitState ();
  649. }
  650. protected internal virtual object SaveViewState ()
  651. {
  652. SaveBitState ();
  653. if (_isSharedViewState)
  654. return null;
  655. return viewstate.SaveViewState ();
  656. }
  657. internal void SaveBitState ()
  658. {
  659. if (stylesTraked != 0)
  660. viewstate [BitStateKey] = stylesTraked;
  661. }
  662. internal void LoadBitState () {
  663. if (viewstate [BitStateKey] == null)
  664. return;
  665. int bit = (int) viewstate [BitStateKey];
  666. styles |= bit;
  667. stylesTraked |= bit;
  668. }
  669. protected internal virtual void SetBit( int bit )
  670. {
  671. styles |= bit;
  672. if (tracking)
  673. stylesTraked |= bit;
  674. }
  675. internal void RemoveBit (int bit) {
  676. styles &= ~bit;
  677. if (tracking) {
  678. stylesTraked &= ~bit;
  679. if (stylesTraked == 0)
  680. viewstate.Remove (BitStateKey);
  681. }
  682. }
  683. internal bool CheckBit (int bit) {
  684. return (styles & bit) != 0;
  685. }
  686. protected internal virtual void TrackViewState ()
  687. {
  688. tracking = true;
  689. viewstate.TrackViewState();
  690. }
  691. #endregion // Protected Instance Methods
  692. #region IStateManager Properties & Methods
  693. void IStateManager.LoadViewState(object state)
  694. {
  695. LoadViewState(state);
  696. }
  697. object IStateManager.SaveViewState()
  698. {
  699. return SaveViewState();
  700. }
  701. void IStateManager.TrackViewState()
  702. {
  703. TrackViewState();
  704. }
  705. bool IStateManager.IsTrackingViewState
  706. {
  707. get
  708. {
  709. return this.IsTrackingViewState;
  710. }
  711. }
  712. #endregion // IStateManager Properties & Methods
  713. #if NET_2_0
  714. internal void SetRegisteredCssClass (string name)
  715. {
  716. registered_class = name;
  717. }
  718. public CssStyleCollection GetStyleAttributes (IUrlResolutionService resolver)
  719. {
  720. CssStyleCollection col = new CssStyleCollection (new StateBag ());
  721. FillStyleAttributes (col, resolver);
  722. return col;
  723. }
  724. [EditorBrowsable(EditorBrowsableState.Advanced)]
  725. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  726. [Browsable(false)]
  727. public string RegisteredCssClass {
  728. get {
  729. if (registered_class == null)
  730. registered_class = String.Empty;
  731. return registered_class;
  732. }
  733. }
  734. internal void CopyTextStylesFrom (Style source) {
  735. // Used primary for TreeView and Menu
  736. if (source.CheckBit ((int) Styles.ForeColor)) {
  737. ForeColor = source.ForeColor;
  738. }
  739. if (source.CheckBit((int) Styles.FontAll)) {
  740. Font.CopyFrom (source.Font);
  741. }
  742. }
  743. internal void RemoveTextStyles () {
  744. ForeColor = Color.Empty;
  745. fontinfo = null;
  746. }
  747. internal void AddCssClass (string cssClass) {
  748. if (String.IsNullOrEmpty (cssClass))
  749. return;
  750. if (CssClass.Length > 0)
  751. CssClass += " ";
  752. CssClass += cssClass;
  753. }
  754. public void SetDirty ()
  755. {
  756. if (viewstate != null)
  757. viewstate.SetDirty (true);
  758. stylesTraked = styles;
  759. }
  760. #endif
  761. }
  762. }