* support-test-*.cs: Rename from test-*-p2.cs.
[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 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
39 namespace System.Web.UI.HtmlControls {
40
41         // CAS
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         // attributes
45         [DefaultEvent("ServerClick")]
46 #if NET_2_0
47         [SupportsEventValidation]
48 #endif
49         public class HtmlInputImage : HtmlInputControl, IPostBackDataHandler,
50                       IPostBackEventHandler {
51
52                 private static readonly object ServerClickEvent = new object ();
53
54                 private int clicked_x;
55                 private int clicked_y;
56
57                 public HtmlInputImage () : base ("image")
58                 {
59                 }
60
61                 [DefaultValue(true)]
62                 [WebSysDescription("")]
63                 [WebCategory("Behavior")]
64 #if NET_2_0
65                 public virtual
66 #else
67                 public
68 #endif          
69                 bool CausesValidation {
70                         get {
71                                 return ViewState.GetBool ("CausesValidation", true);
72                         }
73                         set {
74                                 ViewState ["CausesValidation"] = value;
75                         }
76                 }
77
78                 [DefaultValue("")]
79                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
80                 [WebSysDescription("")]
81                 [WebCategory("Appearance")]
82                 public string Align {
83                         get { return GetAtt ("align"); }
84                         set { SetAtt ("align", value); }
85                 }
86
87                 [DefaultValue("")]
88                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
89 #if NET_2_0
90                 [Localizable (true)]
91 #endif          
92                 [WebSysDescription("")]
93                 [WebCategory("Appearance")]
94                 public string Alt {
95                         get { return GetAtt ("alt"); }
96                         set { SetAtt ("alt", value); }
97                 }
98
99                 [DefaultValue("")]
100                 [WebSysDescription("")]
101                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
102                 [WebCategory("Appearance")]
103 #if NET_2_0
104                 [UrlProperty]
105 #endif
106                 public string Src {
107                         get { return GetAtt ("src"); }
108                         set { SetAtt ("src", value); }
109                 }
110
111 #if NET_2_0
112                 [DefaultValue("-1")]
113 #else           
114                 [DefaultValue("")]
115 #endif          
116                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
117                 [WebSysDescription("")]
118                 [WebCategory("Appearance")]
119                 public int Border {
120                         get {
121                                 string border = Attributes ["border"];
122                                 if (border == null)
123                                         return -1;
124                                 return Int32.Parse (border, CultureInfo.InvariantCulture);
125                         }
126                         set {
127                                 if (value == -1) {
128                                         Attributes.Remove ("border");
129                                         return;
130                                 }
131                                 Attributes ["border"] = value.ToString (CultureInfo.InvariantCulture);
132                         }
133                 }
134
135                 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
136                 {
137                         string x = postCollection [UniqueID + ".x"];
138                         string y = postCollection [UniqueID + ".y"];
139
140                         if (x != null && x.Length != 0 &&
141                                         y != null && y.Length != 0) {
142                                 clicked_x = Int32.Parse (x, CultureInfo.InvariantCulture);
143                                 clicked_y = Int32.Parse (y, CultureInfo.InvariantCulture);
144                                 Page.RegisterRequiresRaiseEvent (this);
145                                 return true;
146                         }
147
148                         return false;
149                 }
150
151                 
152                 void RaisePostBackEventInternal (string eventArgument)
153                 {
154                         if (CausesValidation)
155 #if NET_2_0
156                                 Page.Validate (ValidationGroup);
157 #else
158                                 Page.Validate ();
159 #endif
160                         OnServerClick (new ImageClickEventArgs (clicked_x, clicked_y));
161                 }
162
163                 void RaisePostDataChangedEventInternal ()
164                 {
165                         /* no events to raise */
166                 }
167
168 #if NET_2_0
169                 [DefaultValue ("")]
170                 public string ValidationGroup
171                 {
172                         get {
173                                 return ViewState.GetString ("ValidationGroup", "");
174                         }
175                         set {
176                                 ViewState ["ValidationGroup"] = value;
177                         }
178                 }
179
180                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
181                 {
182                         return LoadPostDataInternal (postDataKey, postCollection);
183                 }
184
185                 protected virtual void RaisePostBackEvent (string eventArgument)
186                 {
187                         RaisePostBackEventInternal (eventArgument);
188                 }
189
190                 protected virtual void RaisePostDataChangedEvent ()
191                 {
192                         RaisePostDataChangedEventInternal ();
193                 }
194 #endif          
195
196                 bool IPostBackDataHandler.LoadPostData (string postDataKey,
197                                 NameValueCollection postCollection)
198                 {
199 #if NET_2_0
200                         return LoadPostData (postDataKey, postCollection);
201 #else
202                         return LoadPostDataInternal (postDataKey, postCollection);
203 #endif
204                 }
205
206                 
207                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
208                 {
209 #if NET_2_0
210                         RaisePostDataChangedEvent();
211 #else
212                         RaisePostDataChangedEventInternal ();
213 #endif
214                 }
215                                 
216                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
217                 {
218 #if NET_2_0
219                         RaisePostBackEvent (eventArgument);
220 #else
221                         RaisePostBackEventInternal (eventArgument);
222 #endif
223                 }
224
225 #if NET_2_0
226                 protected internal
227 #else           
228                 protected
229 #endif          
230                 override void OnPreRender (EventArgs e)
231                 {
232                         base.OnPreRender (e);
233
234                         if (Page != null) {
235                                 Page.RegisterRequiresPostBack (this);
236                         }
237                 }
238
239                 protected virtual void OnServerClick (ImageClickEventArgs e)
240                 {
241                         EventHandler handler = Events [ServerClickEvent] as EventHandler;
242                         if (handler != null)
243                                 handler (this, e);
244                 }
245
246                 protected override void RenderAttributes (HtmlTextWriter writer)
247                 {
248                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
249                                 ClientScriptManager csm = new ClientScriptManager (Page);
250                                 writer.WriteAttribute ("onclick", csm.GetClientValidationEvent ());
251                         }
252
253                         base.RenderAttributes (writer);
254                 }
255
256                 private void SetAtt (string name, string value)
257                 {
258                         if (value == null)
259                                 Attributes.Remove (name);
260                         else
261                                 Attributes [name] = value;
262                 }
263
264                 private string GetAtt (string name)
265                 {
266                         string res = Attributes [name];
267                         if (res == null)
268                                 return String.Empty;
269                         return res;
270                 }
271
272                 [WebSysDescription("")]
273                 [WebCategory("Action")]
274                 public event ImageClickEventHandler ServerClick {
275                         add { Events.AddHandler (ServerClickEvent, value); }
276                         remove { Events.AddHandler (ServerClickEvent, value); }
277                 }
278         }
279 }
280