Image.cs 7.0 KB

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