Image.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // System.Web.UI.WebControls.Image.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.Security.Permissions;
  30. namespace System.Web.UI.WebControls {
  31. // Note: this control can live inside or outside a <form> element
  32. // CAS
  33. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. // attributes
  36. [DefaultProperty ("ImageUrl")]
  37. #if NET_2_0
  38. [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  39. #endif
  40. public class Image : WebControl {
  41. private bool enabled; // not applicable to image
  42. public Image ()
  43. : base (HtmlTextWriterTag.Img)
  44. {
  45. enabled = true;
  46. }
  47. [Bindable (true)]
  48. [DefaultValue ("")]
  49. #if NET_2_0
  50. [Localizable (true)]
  51. #endif
  52. [WebSysDescription ("")]
  53. [WebCategory ("Appearance")]
  54. public virtual string AlternateText {
  55. get {
  56. string s = (string) ViewState ["AlternateText"];
  57. return (s == null) ? String.Empty : s;
  58. }
  59. set {
  60. if (value == null)
  61. ViewState.Remove ("AlternateText");
  62. else
  63. ViewState ["AlternateText"] = value;
  64. }
  65. }
  66. // not applicable to Image
  67. [Browsable (false)]
  68. [EditorBrowsable (EditorBrowsableState.Never)]
  69. public override bool Enabled {
  70. get { return enabled; }
  71. set { enabled = value; }
  72. }
  73. // not applicable to Image
  74. [Browsable (false)]
  75. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  76. [EditorBrowsable (EditorBrowsableState.Never)]
  77. public override FontInfo Font {
  78. get { return base.Font; }
  79. }
  80. #if ONLY_1_1
  81. [Bindable (true)]
  82. #endif
  83. [DefaultValue (ImageAlign.NotSet)]
  84. [WebSysDescription ("")]
  85. [WebCategory ("Layout")]
  86. public virtual ImageAlign ImageAlign {
  87. get {
  88. object o = ViewState ["ImageAlign"];
  89. return (o == null) ? ImageAlign.NotSet : (ImageAlign) o;
  90. }
  91. set {
  92. // avoid reflection
  93. if ((value < ImageAlign.NotSet) || (value > ImageAlign.TextTop)) {
  94. // invalid ImageAlign (note: 2.0 beta2 documents ArgumentException)
  95. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid ImageAlign value."));
  96. }
  97. ViewState ["ImageAlign"] = value;
  98. }
  99. }
  100. [Bindable (true)]
  101. [DefaultValue ("")]
  102. #if NET_2_0
  103. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  104. [UrlProperty]
  105. #else
  106. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  107. #endif
  108. [WebSysDescription ("")]
  109. [WebCategory ("Appearance")]
  110. public virtual string ImageUrl {
  111. get {
  112. string s = (string) ViewState ["ImageUrl"];
  113. return (s == null) ? String.Empty : s;
  114. }
  115. set {
  116. if (value == null)
  117. ViewState.Remove ("ImageUrl");
  118. else
  119. ViewState ["ImageUrl"] = value;
  120. }
  121. }
  122. // this was added in Fx 1.1 SP1
  123. [DefaultValue ("")]
  124. #if NET_2_0
  125. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  126. [UrlProperty]
  127. #endif
  128. [WebSysDescription ("")]
  129. [WebCategory ("Accessibility")]
  130. public virtual string DescriptionUrl {
  131. get {
  132. string s = (string) ViewState ["DescriptionUrl"];
  133. return (s == null) ? String.Empty : s;
  134. }
  135. set {
  136. if (value == null)
  137. ViewState.Remove ("DescriptionUrl");
  138. else
  139. ViewState ["DescriptionUrl"] = value;
  140. }
  141. }
  142. #if NET_2_0
  143. [DefaultValue (false)]
  144. [WebSysDescription ("")]
  145. [WebCategory ("Accessibility")]
  146. public virtual bool GenerateEmptyAlternateText {
  147. get {
  148. object o = ViewState ["GenerateEmptyAlternateText"];
  149. return (o == null) ? false : (bool) o;
  150. }
  151. set {
  152. ViewState ["GenerateEmptyAlternateText"] = value;
  153. }
  154. }
  155. #endif
  156. protected override void AddAttributesToRender (HtmlTextWriter writer)
  157. {
  158. base.AddAttributesToRender (writer);
  159. #if NET_2_0
  160. // src is always present, even if empty, in 2.0
  161. writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveUrl (ImageUrl));
  162. string s = AlternateText;
  163. if ((s.Length > 0) || GenerateEmptyAlternateText)
  164. writer.AddAttribute (HtmlTextWriterAttribute.Alt, s);
  165. s = DescriptionUrl;
  166. if (s.Length > 0)
  167. writer.AddAttribute (HtmlTextWriterAttribute.Longdesc, ResolveUrl (s));
  168. #else
  169. // alt is always present, even if empty, in 1.x
  170. writer.AddAttribute (HtmlTextWriterAttribute.Alt, AlternateText);
  171. string s = ImageUrl;
  172. if (s.Length > 0)
  173. writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveUrl (s));
  174. // added in Fx 1.1 SP1 but the HtmlTextWriterAttribute wasn't
  175. s = DescriptionUrl;
  176. if (s.Length > 0)
  177. writer.AddAttribute ("longdesc", s);
  178. #endif
  179. if (!enabled)
  180. writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
  181. // avoid reflection
  182. switch (ImageAlign) {
  183. case ImageAlign.Left:
  184. writer.AddAttribute (HtmlTextWriterAttribute.Align, "left");
  185. break;
  186. case ImageAlign.Right:
  187. writer.AddAttribute (HtmlTextWriterAttribute.Align, "right");
  188. break;
  189. case ImageAlign.Baseline:
  190. writer.AddAttribute (HtmlTextWriterAttribute.Align, "baseline");
  191. break;
  192. case ImageAlign.Top:
  193. writer.AddAttribute (HtmlTextWriterAttribute.Align, "top");
  194. break;
  195. case ImageAlign.Middle:
  196. writer.AddAttribute (HtmlTextWriterAttribute.Align, "middle");
  197. break;
  198. case ImageAlign.Bottom:
  199. writer.AddAttribute (HtmlTextWriterAttribute.Align, "bottom");
  200. break;
  201. case ImageAlign.AbsBottom:
  202. writer.AddAttribute (HtmlTextWriterAttribute.Align, "absbottom");
  203. break;
  204. case ImageAlign.AbsMiddle:
  205. writer.AddAttribute (HtmlTextWriterAttribute.Align, "absmiddle");
  206. break;
  207. case ImageAlign.TextTop:
  208. writer.AddAttribute (HtmlTextWriterAttribute.Align, "texttop");
  209. break;
  210. }
  211. #if NET_2_0
  212. #if BUG_78875_FIXED
  213. if (Context.Request.Browser.SupportsCss)
  214. #endif
  215. if (BorderWidth.IsEmpty)
  216. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
  217. #if BUG_78875_FIXED
  218. else
  219. #endif
  220. #else
  221. // if border-with is not specified in style or
  222. // no style is defined - set image to no border
  223. if (!ControlStyleCreated || ControlStyle.BorderWidth.IsEmpty)
  224. writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
  225. #endif
  226. }
  227. #if NET_2_0
  228. protected internal
  229. #else
  230. protected
  231. #endif
  232. override void RenderContents (HtmlTextWriter writer)
  233. {
  234. base.RenderContents (writer);
  235. }
  236. }
  237. }