Do not abuse the 'private' keyword
[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, IPostBackEventHandler 
50         {
51                 static readonly object ServerClickEvent = new object ();
52
53                 int clicked_x;
54                 int clicked_y;
55
56                 public HtmlInputImage () : base ("image")
57                 {
58                 }
59
60                 [DefaultValue(true)]
61                 [WebSysDescription("")]
62                 [WebCategory("Behavior")]
63 #if NET_2_0
64                 public virtual
65 #else
66                 public
67 #endif          
68                 bool CausesValidation {
69                         get {
70                                 return ViewState.GetBool ("CausesValidation", true);
71                         }
72                         set {
73                                 ViewState ["CausesValidation"] = value;
74                         }
75                 }
76
77                 [DefaultValue("")]
78                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
79                 [WebSysDescription("")]
80                 [WebCategory("Appearance")]
81                 public string Align {
82                         get { return GetAtt ("align"); }
83                         set { SetAtt ("align", value); }
84                 }
85
86                 [DefaultValue("")]
87                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
88 #if NET_2_0
89                 [Localizable (true)]
90 #endif          
91                 [WebSysDescription("")]
92                 [WebCategory("Appearance")]
93                 public string Alt {
94                         get { return GetAtt ("alt"); }
95                         set { SetAtt ("alt", value); }
96                 }
97
98                 [DefaultValue("")]
99                 [WebSysDescription("")]
100                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
101                 [WebCategory("Appearance")]
102 #if NET_2_0
103                 [UrlProperty]
104 #endif
105                 public string Src {
106                         get { return GetAtt ("src"); }
107                         set { SetAtt ("src", value); }
108                 }
109
110 #if NET_2_0
111                 [DefaultValue("-1")]
112 #else           
113                 [DefaultValue("")]
114 #endif          
115                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
116                 [WebSysDescription("")]
117                 [WebCategory("Appearance")]
118                 public int Border {
119                         get {
120                                 string border = Attributes ["border"];
121                                 if (border == null)
122                                         return -1;
123                                 return Int32.Parse (border, CultureInfo.InvariantCulture);
124                         }
125                         set {
126                                 if (value == -1) {
127                                         Attributes.Remove ("border");
128                                         return;
129                                 }
130                                 Attributes ["border"] = value.ToString (CultureInfo.InvariantCulture);
131                         }
132                 }
133
134                 bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
135                 {
136                         string x = postCollection [UniqueID + ".x"];
137                         string y = postCollection [UniqueID + ".y"];
138
139                         if (x != null && x.Length != 0 &&
140                                         y != null && y.Length != 0) {
141                                 clicked_x = Int32.Parse (x, CultureInfo.InvariantCulture);
142                                 clicked_y = Int32.Parse (y, CultureInfo.InvariantCulture);
143                                 Page.RegisterRequiresRaiseEvent (this);
144                                 return true;
145                         }
146
147                         return false;
148                 }
149
150                 
151                 void RaisePostBackEventInternal (string eventArgument)
152                 {
153                         if (CausesValidation)
154 #if NET_2_0
155                                 Page.Validate (ValidationGroup);
156 #else
157                                 Page.Validate ();
158 #endif
159                         OnServerClick (new ImageClickEventArgs (clicked_x, clicked_y));
160                 }
161
162                 void RaisePostDataChangedEventInternal ()
163                 {
164                         /* no events to raise */
165                 }
166
167 #if NET_2_0
168                 [DefaultValue ("")]
169                 public virtual string ValidationGroup
170                 {
171                         get {
172                                 return ViewState.GetString ("ValidationGroup", "");
173                         }
174                         set {
175                                 ViewState ["ValidationGroup"] = value;
176                         }
177                 }
178
179                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
180                 {
181                         return LoadPostDataInternal (postDataKey, postCollection);
182                 }
183
184                 protected virtual void RaisePostBackEvent (string eventArgument)
185                 {
186                         RaisePostBackEventInternal (eventArgument);
187                 }
188
189                 protected virtual void RaisePostDataChangedEvent ()
190                 {
191                         ValidateEvent (UniqueID, String.Empty);
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 && !Disabled) {
235                                 Page.RegisterRequiresPostBack (this);
236 #if NET_2_0
237                                 Page.RegisterEnabledControl (this);
238 #endif
239                         }
240                 }
241
242                 protected virtual void OnServerClick (ImageClickEventArgs e)
243                 {
244                         ImageClickEventHandler handler = Events [ServerClickEvent] as ImageClickEventHandler;
245                         if (handler != null)
246                                 handler (this, e);
247                 }
248
249                 protected override void RenderAttributes (HtmlTextWriter writer)
250                 {
251 #if NET_2_0
252                         if (Page != null)
253                                 Page.ClientScript.RegisterForEventValidation (UniqueID);
254                         
255                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup)) {
256                                 ClientScriptManager csm = Page.ClientScript;
257                                 Attributes ["onclick"] += csm.GetClientValidationEvent (ValidationGroup);
258                         }
259 #else           
260                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
261                                 ClientScriptManager csm = new ClientScriptManager (Page);
262                                 writer.WriteAttribute ("onclick", csm.GetClientValidationEvent ());
263                         }
264 #endif          
265
266                         PreProcessRelativeReference (writer,"src");
267                         base.RenderAttributes (writer);
268                 }
269
270                 void SetAtt (string name, string value)
271                 {
272                         if ((value == null) || (value.Length == 0))
273                                 Attributes.Remove (name);
274                         else
275                                 Attributes [name] = value;
276                 }
277
278                 string GetAtt (string name)
279                 {
280                         string res = Attributes [name];
281                         if (res == null)
282                                 return String.Empty;
283                         return res;
284                 }
285
286                 [WebSysDescription("")]
287                 [WebCategory("Action")]
288                 public event ImageClickEventHandler ServerClick {
289                         add { Events.AddHandler (ServerClickEvent, value); }
290                         remove { Events.AddHandler (ServerClickEvent, value); }
291                 }
292         }
293 }
294