FontInfo.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // System.Web.UI.WebControls.FontInfo.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. // (C) Gaurav Vaish (2002)
  11. // (C) 2003 Andreas Nahr
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Text;
  35. using System.Reflection;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Drawing;
  39. using System.ComponentModel;
  40. namespace System.Web.UI.WebControls
  41. {
  42. [TypeConverter(typeof(ExpandableObjectConverter))]
  43. public sealed class FontInfo
  44. {
  45. private Style infoOwner;
  46. internal FontInfo(Style owner)
  47. {
  48. infoOwner = owner;
  49. }
  50. /// <summary>
  51. /// Default constructor
  52. /// <remarks>
  53. /// The default constructor is made private to prevent any instances being made.
  54. /// </remarks>
  55. /// </summary>
  56. private FontInfo()
  57. {
  58. }
  59. [DefaultValue (false), Bindable (true), WebCategory ("Font")]
  60. [NotifyParentProperty (true)]
  61. [WebSysDescription ("The 'bold' style of the font.")]
  62. public bool Bold
  63. {
  64. get
  65. {
  66. if(infoOwner.IsSet(Style.FONT_BOLD))
  67. return (bool)(infoOwner.ViewState["FontInfoBold"]);
  68. return false;
  69. }
  70. set
  71. {
  72. infoOwner.ViewState["FontInfoBold"] = value;
  73. infoOwner.Set(Style.FONT_BOLD);
  74. }
  75. }
  76. [DefaultValue (false), Bindable (true), WebCategory ("Font")]
  77. [NotifyParentProperty (true)]
  78. [WebSysDescription ("The 'italic' style of the font.")]
  79. public bool Italic
  80. {
  81. get
  82. {
  83. if(infoOwner.IsSet(Style.FONT_ITALIC))
  84. return (bool)(infoOwner.ViewState["FontInfoItalic"]);
  85. return false;
  86. }
  87. set
  88. {
  89. infoOwner.ViewState["FontInfoItalic"] = value;
  90. infoOwner.Set(Style.FONT_ITALIC);
  91. }
  92. }
  93. [DefaultValue (false), Bindable (true), WebCategory ("Font")]
  94. [NotifyParentProperty (true)]
  95. [WebSysDescription ("The 'overline' style of the font.")]
  96. public bool Overline
  97. {
  98. get
  99. {
  100. if(infoOwner.IsSet(Style.FONT_OLINE))
  101. return (bool)(infoOwner.ViewState["FontInfoOverline"]);
  102. return false;
  103. }
  104. set
  105. {
  106. infoOwner.ViewState["FontInfoOverline"] = value;
  107. infoOwner.Set(Style.FONT_OLINE);
  108. }
  109. }
  110. [DefaultValue (false), Bindable (true), WebCategory ("Font")]
  111. [NotifyParentProperty (true)]
  112. [WebSysDescription ("The 'strikeout' style of the font.")]
  113. public bool Strikeout
  114. {
  115. get
  116. {
  117. if(infoOwner.IsSet(Style.FONT_STRIKE))
  118. return (bool)(infoOwner.ViewState["FontInfoStrikeout"]);
  119. return false;
  120. }
  121. set
  122. {
  123. infoOwner.ViewState["FontInfoStrikeout"] = value;
  124. infoOwner.Set(Style.FONT_STRIKE);
  125. }
  126. }
  127. [DefaultValue (false), Bindable (true), WebCategory ("Font")]
  128. [NotifyParentProperty (true)]
  129. [WebSysDescription ("The 'underline' style of the font.")]
  130. public bool Underline
  131. {
  132. get
  133. {
  134. if(infoOwner.IsSet(Style.FONT_ULINE))
  135. return (bool)(infoOwner.ViewState["FontInfoUnderline"]);
  136. return false;
  137. }
  138. set
  139. {
  140. infoOwner.ViewState["FontInfoUnderline"] = value;
  141. infoOwner.Set(Style.FONT_ULINE);
  142. }
  143. }
  144. [DefaultValue (null), Bindable (true), WebCategory ("Font")]
  145. [NotifyParentProperty (true)]
  146. [WebSysDescription ("The size of the font.")]
  147. public FontUnit Size
  148. {
  149. get
  150. {
  151. if(infoOwner.IsSet(Style.FONT_SIZE))
  152. return (FontUnit)(infoOwner.ViewState["FontInfoSize"]);
  153. return FontUnit.Empty;
  154. }
  155. set
  156. {
  157. if ((value.Type == FontSize.AsUnit) && (value.Unit.Value < 0))
  158. throw new ArgumentOutOfRangeException("value");
  159. infoOwner.ViewState["FontInfoSize"] = value;
  160. infoOwner.Set(Style.FONT_SIZE);
  161. }
  162. }
  163. [DefaultValue (""), Bindable (true), WebCategory ("Font")]
  164. [NotifyParentProperty (true), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  165. [Editor ("System.Drawing.Design.FontNameEditor, " + Consts.AssemblySystem_Drawing_Design, typeof (System.Drawing.Design.UITypeEditor))]
  166. [TypeConverter (typeof (FontConverter.FontNameConverter))]
  167. [WebSysDescription ("The name of the font that this control should be rendered with.")]
  168. public string Name
  169. {
  170. get
  171. {
  172. if(Names!=null && Names.Length > 0)
  173. return Names[0];
  174. return String.Empty;
  175. }
  176. set
  177. {
  178. if(value == null)
  179. throw new ArgumentException();
  180. string[] strArray = null;
  181. if(value.Length > 0)
  182. {
  183. strArray = new string[1];
  184. strArray[0] = value;
  185. }
  186. Names = strArray;
  187. }
  188. }
  189. [WebCategory ("Font")]
  190. [NotifyParentProperty (true)]
  191. [Editor ("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  192. [TypeConverter (typeof (FontNamesConverter))]
  193. [WebSysDescription ("Multiple fonts that can be used to render the control.")]
  194. public string[] Names
  195. {
  196. get
  197. {
  198. if(infoOwner.IsSet(Style.FONT_NAMES))
  199. return (string[])(infoOwner.ViewState["FontInfoNames"]);
  200. return (new string[0]);
  201. }
  202. set
  203. {
  204. if(value!=null)
  205. {
  206. infoOwner.ViewState["FontInfoNames"] = value;
  207. infoOwner.Set(Style.FONT_NAMES);
  208. }
  209. }
  210. }
  211. internal void Reset()
  212. {
  213. if(infoOwner.IsSet(Style.FONT_NAMES))
  214. infoOwner.ViewState.Remove("FontInfoNames");
  215. if(infoOwner.IsSet(Style.FONT_BOLD))
  216. infoOwner.ViewState.Remove("FontInfoBold");
  217. if(infoOwner.IsSet(Style.FONT_ITALIC))
  218. infoOwner.ViewState.Remove("FontInfoItalic");
  219. if(infoOwner.IsSet(Style.FONT_STRIKE))
  220. infoOwner.ViewState.Remove("FontInfoStrikeout");
  221. if(infoOwner.IsSet(Style.FONT_OLINE))
  222. infoOwner.ViewState.Remove("FontInfoOverline");
  223. if(infoOwner.IsSet(Style.FONT_ULINE))
  224. infoOwner.ViewState.Remove("FontInfoUnderline");
  225. if(infoOwner.IsSet(Style.FONT_SIZE) && infoOwner.Font.Size != FontUnit.Empty)
  226. infoOwner.ViewState.Remove("FontInfoSize");
  227. }
  228. internal Style Owner
  229. {
  230. get
  231. {
  232. return infoOwner;
  233. }
  234. }
  235. public void CopyFrom(FontInfo source)
  236. {
  237. if(source!=null)
  238. {
  239. if(source.Owner.IsSet(Style.FONT_NAMES))
  240. Names = source.Names;
  241. if(source.Owner.IsSet(Style.FONT_BOLD)&& source.Bold)
  242. Bold = source.Bold;
  243. if(source.Owner.IsSet(Style.FONT_ITALIC)&& source.Italic)
  244. Italic = source.Italic;
  245. if(source.Owner.IsSet(Style.FONT_STRIKE)&& source.Strikeout)
  246. Strikeout = source.Strikeout;
  247. if(source.Owner.IsSet(Style.FONT_OLINE)&& source.Overline)
  248. Overline = source.Overline;
  249. if(source.Owner.IsSet(Style.FONT_ULINE)&& source.Underline)
  250. Underline = source.Underline;
  251. if(source.Owner.IsSet(Style.FONT_SIZE) && source.Size != FontUnit.Empty)
  252. Size = source.Size;
  253. }
  254. }
  255. public void MergeWith(FontInfo with)
  256. {
  257. if(with!=null)
  258. {
  259. if(with.Owner.IsSet(Style.FONT_NAMES) && !infoOwner.IsSet(Style.FONT_NAMES))
  260. Names = with.Names;
  261. if(with.Owner.IsSet(Style.FONT_BOLD) && !infoOwner.IsSet(Style.FONT_BOLD))
  262. Bold = with.Bold;
  263. if(with.Owner.IsSet(Style.FONT_ITALIC) && !infoOwner.IsSet(Style.FONT_ITALIC))
  264. Italic = with.Italic;
  265. if(with.Owner.IsSet(Style.FONT_STRIKE) && !infoOwner.IsSet(Style.FONT_STRIKE))
  266. Strikeout = with.Strikeout;
  267. if(with.Owner.IsSet(Style.FONT_OLINE) && !infoOwner.IsSet(Style.FONT_OLINE))
  268. Overline = with.Overline;
  269. if(with.Owner.IsSet(Style.FONT_ULINE) && !infoOwner.IsSet(Style.FONT_ULINE))
  270. Underline = with.Underline;
  271. if(with.Owner.IsSet(Style.FONT_SIZE) && with.Size != FontUnit.Empty && !infoOwner.IsSet(Style.FONT_SIZE))
  272. Size = with.Size;
  273. }
  274. }
  275. public bool ShouldSerializeNames()
  276. {
  277. return (Names.Length > 0);
  278. }
  279. public override string ToString()
  280. {
  281. return ( (Name.Length > 0) ? (Name.ToString() + ", " + Size.ToString()) : Size.ToString() );
  282. }
  283. }
  284. }