2008-03-13 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 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 #if NET_2_0
42         [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 #endif
44         public class Image : WebControl {
45
46                 public Image ()
47                         : base (HtmlTextWriterTag.Img)
48                 {
49                 }
50
51
52                 [Bindable (true)]
53                 [DefaultValue ("")]
54 #if NET_2_0
55                 [Localizable (true)]
56 #endif
57                 [WebSysDescription ("")]
58                 [WebCategory ("Appearance")]
59                 public virtual string AlternateText {
60                         get {
61                                 string s = (string) ViewState ["AlternateText"];
62                                 return (s == null) ? String.Empty : s;
63                         }
64                         set {
65                                 if (value == null)
66                                         ViewState.Remove ("AlternateText");
67                                 else
68                                         ViewState ["AlternateText"] = value;
69                         }
70                 }
71
72                 // not applicable to Image
73                 [Browsable (false)]
74                 [EditorBrowsable (EditorBrowsableState.Never)]
75                 public override bool Enabled {
76                         get { return base.Enabled; }
77                         set {base.Enabled = value; }
78                 }
79
80                 // not applicable to Image
81                 [Browsable (false)]
82                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
83                 [EditorBrowsable (EditorBrowsableState.Never)]
84                 public override FontInfo Font {
85                         get { return base.Font; }
86                 }
87
88 #if ONLY_1_1
89                 [Bindable (true)]
90 #endif
91                 [DefaultValue (ImageAlign.NotSet)]
92                 [WebSysDescription ("")]
93                 [WebCategory ("Layout")]
94                 public virtual ImageAlign ImageAlign {
95                         get {
96                                 object o = ViewState ["ImageAlign"];
97                                 return (o == null) ? ImageAlign.NotSet : (ImageAlign) o;
98                         }
99                         set {
100                                 // avoid reflection
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."));
104                                 }
105                                 ViewState ["ImageAlign"] = value;
106                         }
107                 }
108
109                 [Bindable (true)]
110                 [DefaultValue ("")]
111 #if NET_2_0
112                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
113                 [UrlProperty]
114 #else
115                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
116 #endif
117                 [WebSysDescription ("")]
118                 [WebCategory ("Appearance")]
119                 public virtual string ImageUrl {
120                         get {
121                                 string s = (string) ViewState ["ImageUrl"];
122                                 return (s == null) ? String.Empty : s;
123                         }
124                         set {
125                                 if (value == null)
126                                         ViewState.Remove ("ImageUrl");
127                                 else
128                                         ViewState ["ImageUrl"] = value;
129                         }
130                 }
131
132                 // this was added in Fx 1.1 SP1
133                 [DefaultValue ("")]
134 #if NET_2_0
135                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
136                 [UrlProperty]
137 #endif
138                 [WebSysDescription ("")]
139                 [WebCategory ("Accessibility")]
140                 public virtual string DescriptionUrl {
141                         get {
142                                 string s = (string) ViewState ["DescriptionUrl"];
143                                 return (s == null) ? String.Empty : s;
144                         }
145                         set {
146                                 if (value == null)
147                                         ViewState.Remove ("DescriptionUrl");
148                                 else
149                                         ViewState ["DescriptionUrl"] = value;
150                         }
151                 }
152
153 #if NET_2_0
154                 [DefaultValue (false)]
155                 [WebSysDescription ("")]
156                 [WebCategory ("Accessibility")]
157                 public virtual bool GenerateEmptyAlternateText {
158                         get {
159                                 object o = ViewState ["GenerateEmptyAlternateText"];
160                                 return (o == null) ? false : (bool) o;
161                         }
162                         set {
163                                 ViewState ["GenerateEmptyAlternateText"] = value;
164                         }
165                 }
166 #endif
167
168
169                 protected override void AddAttributesToRender (HtmlTextWriter writer)
170                 {
171                         base.AddAttributesToRender (writer);
172 #if NET_2_0
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);
178                         s = DescriptionUrl;
179                         if (s.Length > 0)
180                                 writer.AddAttribute (HtmlTextWriterAttribute.Longdesc, ResolveClientUrl (s));
181 #else
182                         string s = ImageUrl;
183                         if (s.Length > 0)
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
188                         s = DescriptionUrl;
189                         if (s.Length > 0)
190                                 writer.AddAttribute ("longdesc", s);
191 #endif
192                         switch (ImageAlign) {
193                         case ImageAlign.Left:
194                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "left", false);
195                                 break;
196                         case ImageAlign.Right:
197                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "right", false);
198                                 break;
199                         case ImageAlign.Baseline:
200                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "baseline", false);
201                                 break;
202                         case ImageAlign.Top:
203                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "top", false);
204                                 break;
205                         case ImageAlign.Middle:
206                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "middle", false);
207                                 break;
208                         case ImageAlign.Bottom:
209                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "bottom", false);
210                                 break;
211                         case ImageAlign.AbsBottom:
212                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "absbottom", false);
213                                 break;
214                         case ImageAlign.AbsMiddle:
215                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "absmiddle", false);
216                                 break;
217                         case ImageAlign.TextTop:
218                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, "texttop", false);
219                                 break;
220                         }
221 #if NET_2_0
222 #if BUG_78875_FIXED
223                         if (Context.Request.Browser.SupportsCss)
224 #endif
225                         if (BorderWidth.IsEmpty)
226                                 writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
227 #if BUG_78875_FIXED
228                         else
229 #endif
230 #else
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");
235 #endif
236                 }
237
238 #if NET_2_0
239                 protected internal
240 #else           
241                 protected
242 #endif          
243                 override void RenderContents (HtmlTextWriter writer)
244                 {
245                         base.RenderContents (writer);
246                 }
247         }
248 }