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