2008-02-25 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / HyperLink.cs
1 //
2 // System.Web.UI.WebControls.Label.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // TODO: Are we missing something in LoadViewState?
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.ComponentModel;
32 using System.Security.Permissions;
33
34 namespace System.Web.UI.WebControls {
35
36         // CAS
37         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         // attributes
40         [ControlBuilder(typeof(HyperLinkControlBuilder))]
41         [DataBindingHandler("System.Web.UI.Design.HyperLinkDataBindingHandler, " + Consts.AssemblySystem_Design)]
42         [ParseChildren (false)]
43 #if NET_2_0
44         [ToolboxData("<{0}:HyperLink runat=\"server\">HyperLink</{0}:HyperLink>")]
45 #else           
46         [ToolboxData("<{0}:HyperLink runat=server>HyperLink</{0}:HyperLink>")]
47 #endif          
48         [DefaultProperty("Text")]
49         [Designer("System.Web.UI.Design.WebControls.HyperLinkDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
50         public class HyperLink : WebControl {
51
52                 public HyperLink () : base (HtmlTextWriterTag.A)
53                 {
54                 }
55                                 
56                 protected override void AddAttributesToRender (HtmlTextWriter w)
57                 {
58                         base.AddAttributesToRender (w);
59 #if NET_2_0
60                         AddDisplayStyleAttribute (w);
61 #endif
62                         if (!Enabled)
63                                 return;
64                         // add attributes - only if they're not empty
65                         string t = Target;
66                         string s = NavigateUrl;
67                         if (s.Length > 0)
68 #if TARGET_J2EE
69                                 w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveClientUrl (s, String.Compare (t, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0));
70 #else
71                                 w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveClientUrl (s));
72 #endif
73                         if (t.Length > 0)
74                                 w.AddAttribute (HtmlTextWriterAttribute.Target, t);
75                 }
76                 
77                 protected override void AddParsedSubObject (object obj)
78                 {
79                         if (HasControls ()) {
80                                 base.AddParsedSubObject (obj);
81                                 return;
82                         }
83                         
84                         LiteralControl lc = obj as LiteralControl;
85
86                         if (lc == null) {
87                                 string s = Text;
88                                 if (s.Length != 0) {
89                                         Text = null;
90                                         Controls.Add (new LiteralControl (s));
91                                 }
92                                 base.AddParsedSubObject (obj);
93                         } else {
94                                 Text = lc.Text;
95                         }
96                 }
97                 
98
99                 [MonoTODO ("Why override?")]
100                 protected override void LoadViewState (object savedState)
101                 {
102                         base.LoadViewState (savedState);
103                 }
104                 
105 #if NET_2_0
106                 protected internal
107 #else           
108                 protected
109 #endif          
110                 override void RenderContents (HtmlTextWriter w) 
111                 {
112                         if (HasControls () || HasRenderMethodDelegate ()) {
113                                 base.RenderContents (w);
114                                 return;
115                         }
116                         string image_url = ImageUrl;
117                         if (image_url != "") {
118                                 Image img = new Image ();
119                                 img.ImageUrl = ResolveClientUrl (image_url);
120                                 string str = Text;
121                                 if (str != "")
122                                         img.AlternateText = str;
123                                 str = ToolTip;
124                                 if (str != "")
125                                         img.ToolTip = str;
126                                 img.RenderControl (w);
127                         } else {
128                                 w.Write (Text);
129                         }
130                         
131                 }
132
133                 [Bindable(true)]
134                 [DefaultValue("")]
135                 [Editor("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
136 #if NET_2_0
137                 [UrlProperty]
138 #endif          
139                 [WebSysDescription ("")]
140                 [WebCategory ("Appearance")]
141                 public virtual string ImageUrl {
142                         get {
143                                 return ViewState.GetString ("ImageUrl", "");
144                         }
145                         set {
146                                 ViewState ["ImageUrl"] = value;
147                         }       
148                 }
149                 
150                 [Bindable(true)]
151                 [DefaultValue("")]
152                 [Editor("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
153 #if NET_2_0
154                 [UrlProperty]
155 #endif          
156                 [WebSysDescription ("")]
157                 [WebCategory ("Navigation")]
158                 public string NavigateUrl {
159                         get {
160                                 return ViewState.GetString ("NavigateUrl", "");
161                         }
162                         set {
163                                 ViewState ["NavigateUrl"] = value;
164                         }       
165                 }
166                 
167 #if ONLY_1_1
168                 [Bindable(true)]
169 #endif          
170                 [DefaultValue("")]
171                 [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
172                 [WebSysDescription ("")]
173                 [WebCategory ("Navigation")]
174                 public string Target {
175                         get {
176                                 return ViewState.GetString ("Target", "");
177                         }
178                         set {
179                                 ViewState ["Target"] = value;
180                         }
181                 }
182                 
183                 [Bindable(true)]
184                 [DefaultValue("")]
185                 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
186 #if NET_2_0
187                 [Localizable (true)]
188 #endif          
189                 [WebSysDescription ("")]
190                 [WebCategory ("Appearance")]
191                 public virtual string Text {
192                         get {
193                                 return ViewState.GetString ("Text", "");
194                         }
195                         set {
196                                 ViewState ["Text"] = value;
197                                 if (HasControls ())
198                                         Controls.Clear ();
199                         }
200                 }
201         }
202 }