TableItemStyle.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. public TableItemStyle ()
  37. {
  38. }
  39. public TableItemStyle (StateBag bag)
  40. : base (bag)
  41. {
  42. }
  43. #if ONLY_1_1
  44. [Bindable (true)]
  45. #endif
  46. [DefaultValue (HorizontalAlign.NotSet)]
  47. [NotifyParentProperty (true)]
  48. [WebSysDescription ("")]
  49. [WebCategory("Layout")]
  50. public virtual HorizontalAlign HorizontalAlign {
  51. get {
  52. if ((styles & Styles.HorizontalAlign) == 0)
  53. return HorizontalAlign.NotSet;
  54. return (HorizontalAlign) ViewState ["HorizontalAlign"];
  55. }
  56. set {
  57. // avoid reflection
  58. if ((value < HorizontalAlign.NotSet) || (value > HorizontalAlign.Justify)) {
  59. // LAMESPEC: documented as ArgumentException
  60. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid HorizontalAlign value."));
  61. }
  62. ViewState ["HorizontalAlign"] = value;
  63. styles |= Styles.HorizontalAlign;
  64. }
  65. }
  66. #if ONLY_1_1
  67. [Bindable (true)]
  68. #endif
  69. [DefaultValue (VerticalAlign.NotSet)]
  70. [NotifyParentProperty (true)]
  71. [WebSysDescription ("")]
  72. [WebCategory("Layout")]
  73. public virtual VerticalAlign VerticalAlign {
  74. get {
  75. if ((styles & Styles.VerticalAlign) == 0)
  76. return VerticalAlign.NotSet;
  77. return (VerticalAlign) ViewState ["VerticalAlign"];
  78. }
  79. set {
  80. // avoid reflection
  81. if ((value < VerticalAlign.NotSet) || (value > VerticalAlign.Bottom)) {
  82. // LAMESPEC: documented as ArgumentException
  83. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid VerticalAlign value."));
  84. }
  85. ViewState ["VerticalAlign"] = value;
  86. styles |= Styles.VerticalAlign;
  87. }
  88. }
  89. #if ONLY_1_1
  90. [Bindable (true)]
  91. #endif
  92. [DefaultValue (true)]
  93. [NotifyParentProperty (true)]
  94. [WebSysDescription ("")]
  95. [WebCategory("Layout")]
  96. public virtual bool Wrap {
  97. get {
  98. if ((styles & Styles.Wrap) == 0)
  99. return true;
  100. return (bool) ViewState ["Wrap"];
  101. }
  102. set {
  103. ViewState ["Wrap"] = value;
  104. styles |= Styles.Wrap;
  105. }
  106. }
  107. public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner)
  108. {
  109. base.AddAttributesToRender (writer, owner);
  110. if (writer == null)
  111. return;
  112. // note: avoid ToString on the enum
  113. switch (HorizontalAlign) {
  114. case HorizontalAlign.Left:
  115. writer.AddAttribute (HtmlTextWriterAttribute.Align, "left");
  116. break;
  117. case HorizontalAlign.Center:
  118. writer.AddAttribute (HtmlTextWriterAttribute.Align, "center");
  119. break;
  120. case HorizontalAlign.Right:
  121. writer.AddAttribute (HtmlTextWriterAttribute.Align, "right");
  122. break;
  123. case HorizontalAlign.Justify:
  124. writer.AddAttribute (HtmlTextWriterAttribute.Align, "justify");
  125. break;
  126. }
  127. // note: avoid ToString on the enum
  128. switch (VerticalAlign) {
  129. case VerticalAlign.Top:
  130. writer.AddAttribute (HtmlTextWriterAttribute.Valign, "top");
  131. break;
  132. case VerticalAlign.Middle:
  133. writer.AddAttribute (HtmlTextWriterAttribute.Valign, "middle");
  134. break;
  135. case VerticalAlign.Bottom:
  136. writer.AddAttribute (HtmlTextWriterAttribute.Valign, "bottom");
  137. break;
  138. }
  139. if (!Wrap) {
  140. #if NET_2_0
  141. writer.AddStyleAttribute (HtmlTextWriterStyle.WhiteSpace, "nowrap");
  142. #else
  143. writer.AddAttribute (HtmlTextWriterAttribute.Nowrap, "nowrap");
  144. #endif
  145. }
  146. }
  147. private void Copy (string name, Styles s, Style source)
  148. {
  149. if ((source.styles & s) != 0) {
  150. object o = source.ViewState [name];
  151. if (o != null) {
  152. ViewState [name] = o;
  153. styles |= s;
  154. }
  155. }
  156. }
  157. public override void CopyFrom (Style s)
  158. {
  159. base.CopyFrom (s);
  160. if (s != null && !s.IsEmpty) {
  161. Copy ("HorizontalAlign", Styles.HorizontalAlign, s);
  162. Copy ("VerticalAlign", Styles.VerticalAlign, s);
  163. Copy ("Wrap", Styles.Wrap, s);
  164. }
  165. }
  166. private void Merge (string name, Styles s, Style source)
  167. {
  168. if ((styles & s) == 0 && (source.styles & s) != 0) {
  169. object o = source.ViewState [name];
  170. if (o != null) {
  171. ViewState [name] = o;
  172. styles |= s;
  173. }
  174. }
  175. }
  176. public override void MergeWith (Style s)
  177. {
  178. // if we're empty then it's like a copy
  179. if (IsEmpty) {
  180. CopyFrom (s);
  181. } else {
  182. base.MergeWith (s);
  183. if (s != null) {
  184. Merge ("HorizontalAlign", Styles.HorizontalAlign, s);
  185. Merge ("VerticalAlign", Styles.VerticalAlign, s);
  186. Merge ("Wrap", Styles.Wrap, s);
  187. }
  188. }
  189. }
  190. public override void Reset ()
  191. {
  192. if ((styles & Styles.HorizontalAlign) != 0)
  193. ViewState.Remove ("HorizontalAlign");
  194. if ((styles & Styles.VerticalAlign) != 0)
  195. ViewState.Remove ("VerticalAlign");
  196. if ((styles & Styles.Wrap) != 0)
  197. ViewState.Remove ("Wrap");
  198. // call base at the end because "styles" will reset there
  199. base.Reset ();
  200. }
  201. internal override void LoadViewStateInternal()
  202. {
  203. if (viewstate["VerticalAlign"] != null) {
  204. styles |= Styles.VerticalAlign;
  205. }
  206. if (viewstate["Wrap"] != null) {
  207. styles |= Styles.Wrap;
  208. }
  209. base.LoadViewStateInternal();
  210. }
  211. }
  212. }