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