Image.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // System.Web.UI.WebControls.Image.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005-2010 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. {
  32. // Note: this control can live inside or outside a <form> element
  33. // CAS
  34. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. // attributes
  37. [DefaultProperty ("ImageUrl")]
  38. [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  39. public class Image : WebControl
  40. {
  41. public Image ()
  42. : base (HtmlTextWriterTag.Img)
  43. {
  44. }
  45. [Bindable (true)]
  46. [DefaultValue ("")]
  47. [Localizable (true)]
  48. [WebSysDescription ("")]
  49. [WebCategory ("Appearance")]
  50. public virtual string AlternateText {
  51. get {
  52. string s = (string) ViewState ["AlternateText"];
  53. return (s == null) ? String.Empty : s;
  54. }
  55. set {
  56. if (value == null)
  57. ViewState.Remove ("AlternateText");
  58. else
  59. ViewState ["AlternateText"] = value;
  60. }
  61. }
  62. // not applicable to Image
  63. [Browsable (false)]
  64. [EditorBrowsable (EditorBrowsableState.Never)]
  65. public override bool Enabled {
  66. get { return base.Enabled; }
  67. set {base.Enabled = value; }
  68. }
  69. // not applicable to Image
  70. [Browsable (false)]
  71. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  72. [EditorBrowsable (EditorBrowsableState.Never)]
  73. public override FontInfo Font {
  74. get { return base.Font; }
  75. }
  76. [DefaultValue (ImageAlign.NotSet)]
  77. [WebSysDescription ("")]
  78. [WebCategory ("Layout")]
  79. public virtual ImageAlign ImageAlign {
  80. get {
  81. object o = ViewState ["ImageAlign"];
  82. return (o == null) ? ImageAlign.NotSet : (ImageAlign) o;
  83. }
  84. set {
  85. // avoid reflection
  86. if ((value < ImageAlign.NotSet) || (value > ImageAlign.TextTop)) {
  87. // invalid ImageAlign (note: 2.0 beta2 documents ArgumentException)
  88. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid ImageAlign value."));
  89. }
  90. ViewState ["ImageAlign"] = value;
  91. }
  92. }
  93. [Bindable (true)]
  94. [DefaultValue ("")]
  95. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  96. [UrlProperty]
  97. [WebSysDescription ("")]
  98. [WebCategory ("Appearance")]
  99. public virtual string ImageUrl {
  100. get {
  101. string s = (string) ViewState ["ImageUrl"];
  102. return (s == null) ? String.Empty : s;
  103. }
  104. set {
  105. if (value == null)
  106. ViewState.Remove ("ImageUrl");
  107. else
  108. ViewState ["ImageUrl"] = value;
  109. }
  110. }
  111. // this was added in Fx 1.1 SP1
  112. [DefaultValue ("")]
  113. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  114. [UrlProperty]
  115. [WebSysDescription ("")]
  116. [WebCategory ("Accessibility")]
  117. public virtual string DescriptionUrl {
  118. get {
  119. string s = (string) ViewState ["DescriptionUrl"];
  120. return (s == null) ? String.Empty : s;
  121. }
  122. set {
  123. if (value == null)
  124. ViewState.Remove ("DescriptionUrl");
  125. else
  126. ViewState ["DescriptionUrl"] = value;
  127. }
  128. }
  129. [DefaultValue (false)]
  130. [WebSysDescription ("")]
  131. [WebCategory ("Accessibility")]
  132. public virtual bool GenerateEmptyAlternateText {
  133. get {
  134. object o = ViewState ["GenerateEmptyAlternateText"];
  135. return (o == null) ? false : (bool) o;
  136. }
  137. set { ViewState ["GenerateEmptyAlternateText"] = value; }
  138. }
  139. #if NET_4_0
  140. public override bool SupportsDisabledAttribute {
  141. get { return RenderingCompatibilityLessThan40; }
  142. }
  143. #endif
  144. protected override void AddAttributesToRender (HtmlTextWriter writer)
  145. {
  146. base.AddAttributesToRender (writer);
  147. // src is always present, even if empty, in 2.0
  148. writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (ImageUrl));
  149. string s = AlternateText;
  150. if ((s.Length > 0) || GenerateEmptyAlternateText)
  151. writer.AddAttribute (HtmlTextWriterAttribute.Alt, s);
  152. s = DescriptionUrl;
  153. if (s.Length > 0)
  154. writer.AddAttribute (HtmlTextWriterAttribute.Longdesc, ResolveClientUrl (s));
  155. switch (ImageAlign) {
  156. case ImageAlign.Left:
  157. writer.AddAttribute (HtmlTextWriterAttribute.Align, "left", false);
  158. break;
  159. case ImageAlign.Right:
  160. writer.AddAttribute (HtmlTextWriterAttribute.Align, "right", false);
  161. break;
  162. case ImageAlign.Baseline:
  163. writer.AddAttribute (HtmlTextWriterAttribute.Align, "baseline", false);
  164. break;
  165. case ImageAlign.Top:
  166. writer.AddAttribute (HtmlTextWriterAttribute.Align, "top", false);
  167. break;
  168. case ImageAlign.Middle:
  169. writer.AddAttribute (HtmlTextWriterAttribute.Align, "middle", false);
  170. break;
  171. case ImageAlign.Bottom:
  172. writer.AddAttribute (HtmlTextWriterAttribute.Align, "bottom", false);
  173. break;
  174. case ImageAlign.AbsBottom:
  175. writer.AddAttribute (HtmlTextWriterAttribute.Align, "absbottom", false);
  176. break;
  177. case ImageAlign.AbsMiddle:
  178. writer.AddAttribute (HtmlTextWriterAttribute.Align, "absmiddle", false);
  179. break;
  180. case ImageAlign.TextTop:
  181. writer.AddAttribute (HtmlTextWriterAttribute.Align, "texttop", false);
  182. break;
  183. }
  184. #if BUG_78875_FIXED
  185. if (Context.Request.Browser.SupportsCss)
  186. #endif
  187. #if !NET_4_0
  188. if (BorderWidth.IsEmpty)
  189. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
  190. #endif
  191. }
  192. protected internal override void RenderContents (HtmlTextWriter writer)
  193. {
  194. base.RenderContents (writer);
  195. }
  196. }
  197. }