TableItemStyle.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // System.Web.UI.WebControls.TableItemStyle.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.ComponentModel;
  29. using System.Globalization;
  30. using System.Security.Permissions;
  31. using System.Web.UI;
  32. namespace System.Web.UI.WebControls {
  33. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. public class TableItemStyle : Style {
  36. [Flags]
  37. enum TableItemStyles
  38. {
  39. HorizontalAlign = 0x00010000,
  40. VerticalAlign = 0x00020000,
  41. Wrap = 0x00040000,
  42. }
  43. public TableItemStyle ()
  44. {
  45. }
  46. public TableItemStyle (StateBag bag)
  47. : base (bag)
  48. {
  49. }
  50. [DefaultValue (HorizontalAlign.NotSet)]
  51. [NotifyParentProperty (true)]
  52. [WebSysDescription ("")]
  53. [WebCategory("Layout")]
  54. public virtual HorizontalAlign HorizontalAlign {
  55. get {
  56. if (!CheckBit ((int) TableItemStyles.HorizontalAlign))
  57. return HorizontalAlign.NotSet;
  58. return (HorizontalAlign) ViewState ["HorizontalAlign"];
  59. }
  60. set {
  61. // avoid reflection
  62. if ((value < HorizontalAlign.NotSet) || (value > HorizontalAlign.Justify)) {
  63. // LAMESPEC: documented as ArgumentException
  64. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid HorizontalAlign value."));
  65. }
  66. ViewState ["HorizontalAlign"] = value;
  67. SetBit ((int) TableItemStyles.HorizontalAlign);
  68. }
  69. }
  70. [DefaultValue (VerticalAlign.NotSet)]
  71. [NotifyParentProperty (true)]
  72. [WebSysDescription ("")]
  73. [WebCategory("Layout")]
  74. public virtual VerticalAlign VerticalAlign {
  75. get {
  76. if (!CheckBit ((int) TableItemStyles.VerticalAlign))
  77. return VerticalAlign.NotSet;
  78. return (VerticalAlign) ViewState ["VerticalAlign"];
  79. }
  80. set {
  81. // avoid reflection
  82. if ((value < VerticalAlign.NotSet) || (value > VerticalAlign.Bottom)) {
  83. // LAMESPEC: documented as ArgumentException
  84. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid VerticalAlign value."));
  85. }
  86. ViewState ["VerticalAlign"] = value;
  87. SetBit ((int) TableItemStyles.VerticalAlign);
  88. }
  89. }
  90. [DefaultValue (true)]
  91. [NotifyParentProperty (true)]
  92. [WebSysDescription ("")]
  93. [WebCategory("Layout")]
  94. public virtual bool Wrap {
  95. get {
  96. if (!CheckBit ((int) TableItemStyles.Wrap))
  97. return true;
  98. return (bool) ViewState ["Wrap"];
  99. }
  100. set {
  101. ViewState ["Wrap"] = value;
  102. SetBit ((int) TableItemStyles.Wrap);
  103. }
  104. }
  105. public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
  106. {
  107. base.AddAttributesToRender (writer, owner);
  108. if (writer == null)
  109. return;
  110. // note: avoid ToString on the enum
  111. switch (HorizontalAlign) {
  112. case HorizontalAlign.Left:
  113. writer.AddAttribute (HtmlTextWriterAttribute.Align, "left", false);
  114. break;
  115. case HorizontalAlign.Center:
  116. writer.AddAttribute (HtmlTextWriterAttribute.Align, "center", false);
  117. break;
  118. case HorizontalAlign.Right:
  119. writer.AddAttribute (HtmlTextWriterAttribute.Align, "right", false);
  120. break;
  121. case HorizontalAlign.Justify:
  122. writer.AddAttribute (HtmlTextWriterAttribute.Align, "justify", false);
  123. break;
  124. }
  125. // note: avoid ToString on the enum
  126. switch (VerticalAlign) {
  127. case VerticalAlign.Top:
  128. writer.AddAttribute (HtmlTextWriterAttribute.Valign, "top", false);
  129. break;
  130. case VerticalAlign.Middle:
  131. writer.AddAttribute (HtmlTextWriterAttribute.Valign, "middle", false);
  132. break;
  133. case VerticalAlign.Bottom:
  134. writer.AddAttribute (HtmlTextWriterAttribute.Valign, "bottom", false);
  135. break;
  136. }
  137. if (!Wrap) {
  138. writer.AddStyleAttribute (HtmlTextWriterStyle.WhiteSpace, "nowrap");
  139. }
  140. }
  141. void Copy (string name, TableItemStyles s, Style source)
  142. {
  143. if (source.CheckBit((int) s)) {
  144. object o = source.ViewState [name];
  145. if (o != null) {
  146. ViewState [name] = o;
  147. SetBit ((int) s);
  148. }
  149. }
  150. }
  151. public override void CopyFrom (Style s)
  152. {
  153. base.CopyFrom (s);
  154. if (s != null && !s.IsEmpty) {
  155. Copy ("HorizontalAlign", TableItemStyles.HorizontalAlign, s);
  156. Copy ("VerticalAlign", TableItemStyles.VerticalAlign, s);
  157. Copy ("Wrap", TableItemStyles.Wrap, s);
  158. }
  159. }
  160. void Merge (string name, TableItemStyles s, Style source)
  161. {
  162. if ((!CheckBit ((int) s)) && (source.CheckBit ((int) s))) {
  163. object o = source.ViewState [name];
  164. if (o != null) {
  165. ViewState [name] = o;
  166. SetBit ((int) s);
  167. }
  168. }
  169. }
  170. public override void MergeWith (Style s)
  171. {
  172. // if we're empty then it's like a copy
  173. if (IsEmpty) {
  174. CopyFrom (s);
  175. } else {
  176. base.MergeWith (s);
  177. if (s != null) {
  178. Merge ("HorizontalAlign", TableItemStyles.HorizontalAlign, s);
  179. Merge ("VerticalAlign", TableItemStyles.VerticalAlign, s);
  180. Merge ("Wrap", TableItemStyles.Wrap, s);
  181. }
  182. }
  183. }
  184. public override void Reset ()
  185. {
  186. if (CheckBit ((int) TableItemStyles.HorizontalAlign))
  187. ViewState.Remove ("HorizontalAlign");
  188. if (CheckBit ((int) TableItemStyles.VerticalAlign))
  189. ViewState.Remove ("VerticalAlign");
  190. if (CheckBit ((int) TableItemStyles.Wrap))
  191. ViewState.Remove ("Wrap");
  192. // call base at the end because "styles" will reset there
  193. base.Reset ();
  194. }
  195. }
  196. }