2 // System.Web.UI.WebControls.Image.cs
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
30 using System.Security.Permissions;
32 namespace System.Web.UI.WebControls {
34 // Note: this control can live inside or outside a <form> element
37 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [DefaultProperty ("ImageUrl")]
42 [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
44 public class Image : WebControl {
47 : base (HtmlTextWriterTag.Img)
57 [WebSysDescription ("")]
58 [WebCategory ("Appearance")]
59 public virtual string AlternateText {
61 string s = (string) ViewState ["AlternateText"];
62 return (s == null) ? String.Empty : s;
66 ViewState.Remove ("AlternateText");
68 ViewState ["AlternateText"] = value;
72 // not applicable to Image
74 [EditorBrowsable (EditorBrowsableState.Never)]
75 public override bool Enabled {
76 get { return base.Enabled; }
77 set {base.Enabled = value; }
80 // not applicable to Image
82 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
83 [EditorBrowsable (EditorBrowsableState.Never)]
84 public override FontInfo Font {
85 get { return base.Font; }
91 [DefaultValue (ImageAlign.NotSet)]
92 [WebSysDescription ("")]
93 [WebCategory ("Layout")]
94 public virtual ImageAlign ImageAlign {
96 object o = ViewState ["ImageAlign"];
97 return (o == null) ? ImageAlign.NotSet : (ImageAlign) o;
101 if ((value < ImageAlign.NotSet) || (value > ImageAlign.TextTop)) {
102 // invalid ImageAlign (note: 2.0 beta2 documents ArgumentException)
103 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid ImageAlign value."));
105 ViewState ["ImageAlign"] = value;
112 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
115 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
117 [WebSysDescription ("")]
118 [WebCategory ("Appearance")]
119 public virtual string ImageUrl {
121 string s = (string) ViewState ["ImageUrl"];
122 return (s == null) ? String.Empty : s;
126 ViewState.Remove ("ImageUrl");
128 ViewState ["ImageUrl"] = value;
132 // this was added in Fx 1.1 SP1
135 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
138 [WebSysDescription ("")]
139 [WebCategory ("Accessibility")]
140 public virtual string DescriptionUrl {
142 string s = (string) ViewState ["DescriptionUrl"];
143 return (s == null) ? String.Empty : s;
147 ViewState.Remove ("DescriptionUrl");
149 ViewState ["DescriptionUrl"] = value;
154 [DefaultValue (false)]
155 [WebSysDescription ("")]
156 [WebCategory ("Accessibility")]
157 public virtual bool GenerateEmptyAlternateText {
159 object o = ViewState ["GenerateEmptyAlternateText"];
160 return (o == null) ? false : (bool) o;
163 ViewState ["GenerateEmptyAlternateText"] = value;
169 protected override void AddAttributesToRender (HtmlTextWriter writer)
171 base.AddAttributesToRender (writer);
173 // src is always present, even if empty, in 2.0
174 writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (ImageUrl));
175 string s = AlternateText;
176 if ((s.Length > 0) || GenerateEmptyAlternateText)
177 writer.AddAttribute (HtmlTextWriterAttribute.Alt, s);
180 writer.AddAttribute (HtmlTextWriterAttribute.Longdesc, ResolveClientUrl (s));
184 writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (s));
185 // alt is always present, even if empty, in 1.x
186 writer.AddAttribute (HtmlTextWriterAttribute.Alt, AlternateText);
187 // added in Fx 1.1 SP1 but the HtmlTextWriterAttribute wasn't
190 writer.AddAttribute ("longdesc", s);
192 switch (ImageAlign) {
193 case ImageAlign.Left:
194 writer.AddAttribute (HtmlTextWriterAttribute.Align, "left", false);
196 case ImageAlign.Right:
197 writer.AddAttribute (HtmlTextWriterAttribute.Align, "right", false);
199 case ImageAlign.Baseline:
200 writer.AddAttribute (HtmlTextWriterAttribute.Align, "baseline", false);
203 writer.AddAttribute (HtmlTextWriterAttribute.Align, "top", false);
205 case ImageAlign.Middle:
206 writer.AddAttribute (HtmlTextWriterAttribute.Align, "middle", false);
208 case ImageAlign.Bottom:
209 writer.AddAttribute (HtmlTextWriterAttribute.Align, "bottom", false);
211 case ImageAlign.AbsBottom:
212 writer.AddAttribute (HtmlTextWriterAttribute.Align, "absbottom", false);
214 case ImageAlign.AbsMiddle:
215 writer.AddAttribute (HtmlTextWriterAttribute.Align, "absmiddle", false);
217 case ImageAlign.TextTop:
218 writer.AddAttribute (HtmlTextWriterAttribute.Align, "texttop", false);
223 if (Context.Request.Browser.SupportsCss)
225 if (BorderWidth.IsEmpty)
226 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
231 // if border-with is not specified in style or
232 // no style is defined - set image to no border
233 if (!ControlStyleCreated || ControlStyle.BorderWidth.IsEmpty)
234 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
243 override void RenderContents (HtmlTextWriter writer)
245 base.RenderContents (writer);