New tests.
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlInputImage.cs
1 //
2 // Permission is hereby granted, free of charge, to any person obtaining
3 // a copy of this software and associated documentation files (the
4 // "Software"), to deal in the Software without restriction, including
5 // without limitation the rights to use, copy, modify, merge, publish,
6 // distribute, sublicense, and/or sell copies of the Software, and to
7 // permit persons to whom the Software is furnished to do so, subject to
8 // the following conditions:
9 // 
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
12 // 
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 //
21 //
22 // System.Web.UI.HtmlControls.HtmlInputImage.cs
23 //
24 // Authors:
25 //      Jackson Harper (jackson@ximian.com)
26 //
27 // (C) 2005-2010 Novell, Inc.
28
29
30 //
31 // TODO: getting the .x and .y in LoadData doesn't work with mozilla
32 //
33
34 using System.Globalization;
35 using System.Collections.Specialized;
36 using System.ComponentModel;
37 using System.Security.Permissions;
38 using System.Web.Util;
39
40 namespace System.Web.UI.HtmlControls
41 {
42         // CAS
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         // attributes
46         [DefaultEvent("ServerClick")]
47         [SupportsEventValidation]
48         public class HtmlInputImage : HtmlInputControl, IPostBackDataHandler, IPostBackEventHandler 
49         {
50                 static readonly object ServerClickEvent = new object ();
51
52                 int clicked_x;
53                 int clicked_y;
54
55                 public HtmlInputImage () : base ("image")
56                 {
57                 }
58
59                 [DefaultValue(true)]
60                 [WebSysDescription("")]
61                 [WebCategory("Behavior")]
62                 public virtual bool CausesValidation {
63                         get {
64                                 return ViewState.GetBool ("CausesValidation", true);
65                         }
66                         set {
67                                 ViewState ["CausesValidation"] = value;
68                         }
69                 }
70
71                 [DefaultValue("")]
72                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
73                 [WebSysDescription("")]
74                 [WebCategory("Appearance")]
75                 public string Align {
76                         get { return GetAtt ("align"); }
77                         set { SetAtt ("align", value); }
78                 }
79
80                 [DefaultValue("")]
81                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
82                 [Localizable (true)]
83                 [WebSysDescription("")]
84                 [WebCategory("Appearance")]
85                 public string Alt {
86                         get { return GetAtt ("alt"); }
87                         set { SetAtt ("alt", value); }
88                 }
89
90                 [DefaultValue("")]
91                 [WebSysDescription("")]
92                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
93                 [WebCategory("Appearance")]
94                 [UrlProperty]
95                 public string Src {
96                         get { return GetAtt ("src"); }
97                         set { SetAtt ("src", value); }
98                 }
99
100                 [DefaultValue("-1")]
101                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
102                 [WebSysDescription("")]
103                 [WebCategory("Appearance")]
104                 public int Border {
105                         get {
106                                 string border = Attributes ["border"];
107                                 if (border == null)
108                                         return -1;
109                                 return Int32.Parse (border, Helpers.InvariantCulture);
110                         }
111                         set {
112                                 if (value == -1) {
113                                         Attributes.Remove ("border");
114                                         return;
115                                 }
116                                 Attributes ["border"] = value.ToString (Helpers.InvariantCulture);
117                         }
118                 }
119
120                 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
121                 {
122                         string x = postCollection [UniqueID + ".x"];
123                         string y = postCollection [UniqueID + ".y"];
124
125                         if (x != null && x.Length != 0 &&
126                                         y != null && y.Length != 0) {
127                                 clicked_x = Int32.Parse (x, Helpers.InvariantCulture);
128                                 clicked_y = Int32.Parse (y, Helpers.InvariantCulture);
129                                 Page.RegisterRequiresRaiseEvent (this);
130                                 return true;
131                         }
132
133                         return false;
134                 }
135
136                 
137                 void RaisePostBackEventInternal (string eventArgument)
138                 {
139                         if (CausesValidation)
140                                 Page.Validate (ValidationGroup);
141
142                         OnServerClick (new ImageClickEventArgs (clicked_x, clicked_y));
143                 }
144
145                 void RaisePostDataChangedEventInternal ()
146                 {
147                         /* no events to raise */
148                 }
149
150                 [DefaultValue ("")]
151                 public virtual string ValidationGroup
152                 {
153                         get {
154                                 return ViewState.GetString ("ValidationGroup", "");
155                         }
156                         set {
157                                 ViewState ["ValidationGroup"] = value;
158                         }
159                 }
160
161                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
162                 {
163                         return LoadPostDataInternal (postDataKey, postCollection);
164                 }
165
166                 protected virtual void RaisePostBackEvent (string eventArgument)
167                 {
168                         RaisePostBackEventInternal (eventArgument);
169                 }
170
171                 protected virtual void RaisePostDataChangedEvent ()
172                 {
173                         ValidateEvent (UniqueID, String.Empty);
174                         RaisePostDataChangedEventInternal ();
175                 }
176
177                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
178                 {
179                         return LoadPostData (postDataKey, postCollection);
180                 }
181                 
182                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
183                 {
184                         RaisePostDataChangedEvent();
185                 }
186                                 
187                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
188                 {
189                         RaisePostBackEvent (eventArgument);
190                 }
191
192                 protected internal override void OnPreRender (EventArgs e)
193                 {
194                         base.OnPreRender (e);
195
196                         Page page = Page;
197                         if (page != null && !Disabled) {
198                                 page.RegisterRequiresPostBack (this);
199                                 page.RegisterEnabledControl (this);
200                         }
201                 }
202
203                 protected virtual void OnServerClick (ImageClickEventArgs e)
204                 {
205                         ImageClickEventHandler handler = Events [ServerClickEvent] as ImageClickEventHandler;
206                         if (handler != null)
207                                 handler (this, e);
208                 }
209
210                 protected override void RenderAttributes (HtmlTextWriter writer)
211                 {
212                         Page page = Page;
213                         if (page != null)
214                                 page.ClientScript.RegisterForEventValidation (UniqueID);
215                         
216                         if (CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup)) {
217                                 ClientScriptManager csm = page.ClientScript;
218                                 Attributes ["onclick"] += csm.GetClientValidationEvent (ValidationGroup);
219                         }
220
221                         PreProcessRelativeReference (writer,"src");
222                         base.RenderAttributes (writer);
223                 }
224
225                 void SetAtt (string name, string value)
226                 {
227                         if ((value == null) || (value.Length == 0))
228                                 Attributes.Remove (name);
229                         else
230                                 Attributes [name] = value;
231                 }
232
233                 string GetAtt (string name)
234                 {
235                         string res = Attributes [name];
236                         if (res == null)
237                                 return String.Empty;
238                         return res;
239                 }
240
241                 [WebSysDescription("")]
242                 [WebCategory("Action")]
243                 public event ImageClickEventHandler ServerClick {
244                         add { Events.AddHandler (ServerClickEvent, value); }
245                         remove { Events.AddHandler (ServerClickEvent, value); }
246                 }
247         }
248 }