typo fix
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ImageButton.cs
1 //
2 // System.Web.UI.WebControls.ImageButton.cs
3 //
4 // Authors:
5 //      Jordi Mas i Hernandez (jordi@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 //
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.Collections.Specialized;
32 using System.ComponentModel;
33 using System.Security.Permissions;
34
35 namespace System.Web.UI.WebControls {
36
37         // CAS
38         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         // attributes
41         [DefaultEvent("Click")]
42 #if NET_2_0
43         [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
44         [SupportsEventValidation]
45         public class ImageButton : Image, IPostBackDataHandler, IPostBackEventHandler, IButtonControl {
46 #else
47         public class ImageButton : Image, IPostBackDataHandler, IPostBackEventHandler {
48 #endif
49                 private static readonly object ClickEvent = new object ();
50                 private static readonly object CommandEvent = new object ();
51                 private int pos_x, pos_y;
52
53                 public ImageButton ()
54                 {
55
56                 }
57
58 #if ONLY_1_1
59                 [Bindable(false)]
60 #endif          
61                 [DefaultValue(true)]
62                 [WebSysDescription ("")]
63                 [WebCategory ("Behavior")]
64 #if NET_2_0
65                 [Themeable (false)]
66                 public virtual
67 #else           
68                 public
69 #endif          
70                 bool CausesValidation {
71                         get {
72                                 return ViewState.GetBool ("CausesValidation", true);
73                         }
74
75                         set {
76                                 ViewState ["CausesValidation"] = value;
77                         }
78                 }
79
80                 [Bindable(true)]
81                 [DefaultValue("")]
82                 [WebSysDescription ("")]
83                 [WebCategory ("Behavior")]
84 #if NET_2_0
85                 [Themeable (false)]
86 #endif          
87                 public string CommandArgument {
88                         get {
89                                 return ViewState.GetString ("CommandArgument", "");
90                         }
91                         set {
92                                 ViewState ["CommandArgument"] = value;
93                         }
94                 }
95
96                 [DefaultValue("")]
97                 [WebSysDescription ("")]
98                 [WebCategory ("Behavior")]
99 #if NET_2_0
100                 [Themeable (false)]
101 #endif          
102                 public string CommandName {
103                         get {
104                                 return ViewState.GetString ("CommandName", "");
105                         }
106                         set {
107                                 ViewState ["CommandName"] = value;
108                         }
109                 }
110
111 #if NET_2_0
112                 [EditorBrowsable (EditorBrowsableState.Always)]
113                 [Browsable (true)]
114                 [DefaultValue (true)]
115                 [Bindable (true)]
116                 [MonoTODO]
117                 public virtual new bool Enabled
118                 {
119                         get {
120                                 throw new NotImplementedException ();
121                         }
122                         set {
123                                 throw new NotImplementedException ();
124                         }
125                 }
126
127                 [Browsable (false)]
128                 [EditorBrowsable (EditorBrowsableState.Never)]
129                 [Themeable (false)]
130                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
131                 [MonoTODO]
132                 public virtual new bool GenerateEmptyAlternateText
133                 {
134                         get {
135                                 throw new NotImplementedException ();
136                         }
137                         set {
138                                 throw new NotImplementedException ();
139                         }
140                 }
141
142                 [DefaultValue ("")]
143                 [Themeable (false)]
144                 public virtual string OnClientClick 
145                 {
146                         get {
147                                 return ViewState.GetString ("OnClientClick", "");
148                         }
149                         set {
150                                 ViewState ["OnClientClick"] = value;
151                         }
152                 }
153
154                 [Themeable (false)]
155 #if NET_2_0
156                 [UrlProperty ("*.aspx")]
157 #else
158                 [UrlProperty]
159 #endif
160                 [DefaultValue ("")]
161                 [Editor ("System.Web.UI.Design.UrlEditor, "  + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
162                 public virtual string PostBackUrl {
163                         get {
164                                 return ViewState.GetString ("PostBackUrl", String.Empty);
165                         }
166                         set {
167                                 ViewState["PostBackUrl"] = value;
168                         }
169                 }
170
171                 [Themeable (false)]
172                 [DefaultValue ("")]
173                 [WebSysDescription ("")]
174                 [WebCategory ("Behavior")]
175                 public virtual string ValidationGroup
176                 {
177                         get {
178                                 return ViewState.GetString ("ValidationGroup", "");
179                         }
180                         set {
181                                 ViewState ["ValidationGroup"] = value;
182                         }
183                 }
184 #endif          
185
186                 [Browsable(false)]
187                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
188 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
189                 protected virtual new
190 #else           
191                 protected override
192 #endif
193                 HtmlTextWriterTag TagKey {
194                         get { return HtmlTextWriterTag.Input; }
195                 }
196
197 #if NET_2_0
198                 // Gets or sets the value of the ImageButton control's AlternateText property. (MSDN)
199                 protected virtual string Text 
200                 {
201                         get {
202                                 return AlternateText;
203                         }
204                         set {
205                                 AlternateText = value;
206                         }
207                 }
208 #endif          
209
210                 protected override void AddAttributesToRender (HtmlTextWriter writer)
211                 {
212                         if (Page != null)
213                                 Page.VerifyRenderingInServerForm (this);
214
215                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "image");
216                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
217 #if NET_2_0
218                         string onclick = OnClientClick;
219                         onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
220                         if (Attributes ["onclick"] != null) {
221                                 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
222                                 Attributes.Remove ("onclick");
223                         }
224
225                         if (Page != null) {
226                                 PostBackOptions options = GetPostBackOptions ();
227                                 onclick += Page.ClientScript.GetPostBackEventReference (options);
228                         }
229
230                         if (onclick.Length > 0)
231                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
232 #else
233                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
234                                 ClientScriptManager csm = new ClientScriptManager (Page);
235                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, csm.GetClientValidationEvent ());
236                                 writer.AddAttribute ("language", "javascript");
237                         }
238 #endif
239                         base.AddAttributesToRender (writer);
240                 }
241
242 #if NET_2_0
243                 protected virtual PostBackOptions GetPostBackOptions ()
244                 {
245                         PostBackOptions options = new PostBackOptions (this);
246                         options.ActionUrl = (PostBackUrl.Length > 0 ? Page.ResolveClientUrl (PostBackUrl) : null);
247                         options.ValidationGroup = null;
248                         options.Argument = "";
249                         options.ClientSubmit = false;
250                         options.RequiresJavaScriptProtocol = false;
251                         options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
252                         if (options.PerformValidation)
253                                 options.ValidationGroup = ValidationGroup;
254
255                         return options;
256                 }
257 #endif          
258
259
260 #if NET_2_0
261                 protected virtual
262 #endif
263                 bool LoadPostData (string postDataKey, NameValueCollection postCollection) 
264                 {
265                         string x, y;
266                         string unique = UniqueID;
267                         x = postCollection [unique + ".x"];
268                         y = postCollection [unique + ".y"];
269                         if (x != null && x != "" && y != null && y != "") {
270                                 pos_x = Int32.Parse(x);
271                                 pos_y = Int32.Parse(y);
272                                 Page.RegisterRequiresRaiseEvent (this);
273                                 return true;
274                         } else {
275                                 x = postCollection [unique];
276                                 if (x != null && x != "") {
277                                         pos_x = Int32.Parse (x);
278                                         pos_y = 0;
279                                         Page.RegisterRequiresRaiseEvent (this);
280                                         return true;
281                                 }
282                         }
283
284                         return false;
285                 }
286 #if NET_2_0
287                 protected virtual
288 #endif
289                 void RaisePostDataChangedEvent ()
290                 {
291                         if (CausesValidation)
292 #if NET_2_0
293                                 Page.Validate (ValidationGroup);
294 #else
295                                 Page.Validate ();
296 #endif
297
298                         OnClick (new ImageClickEventArgs (pos_x, pos_y));
299                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
300                 }
301                 
302                 [MonoTODO]
303 #if NET_2_0
304                 protected virtual
305 #endif
306                 void RaisePostBackEvent (string eventArgument)
307                 {
308                 }
309
310                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
311                 {
312                         return LoadPostData (postDataKey, postCollection);
313                 }
314
315
316                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
317                 {
318                         RaisePostDataChangedEvent ();
319                 }
320
321                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
322                 {
323                         RaisePostBackEvent (eventArgument);
324                 }
325
326                 protected virtual void OnClick (ImageClickEventArgs e)
327                 {
328                         if (Events != null) {
329                                 ImageClickEventHandler eh = (ImageClickEventHandler) (Events [ClickEvent]);
330                                 if (eh != null)
331                                         eh (this, e);
332                         }
333                 }
334
335                 protected virtual void OnCommand (CommandEventArgs e)
336                 {
337                         if (Events != null) {
338                                 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
339                                 if (eh != null)
340                                         eh (this, e);
341                         }
342
343                         RaiseBubbleEvent (this, e);
344                 }
345
346 #if NET_2_0
347                 protected internal
348 #else           
349                 protected
350 #endif          
351                 override void OnPreRender (EventArgs e)
352                 {
353                         if (Page != null)
354                                 Page.RegisterRequiresPostBack (this);
355                 }
356
357                 [WebSysDescription ("")]
358                 [WebCategory ("Action")]
359                 public event ImageClickEventHandler Click
360                 {
361                         add {
362                                 Events.AddHandler (ClickEvent, value);
363                         }
364                         remove {
365                                 Events.RemoveHandler (ClickEvent, value);
366                         }
367                 }
368
369                 [WebSysDescription ("")]
370                 [WebCategory ("Action")]
371                 public event CommandEventHandler Command
372                 {
373                         add {
374                                 Events.AddHandler (CommandEvent, value);
375                         }
376                         remove {
377                                 Events.RemoveHandler (CommandEvent, value);
378                         }
379                 }
380
381 #if NET_2_0
382                 string IButtonControl.Text 
383                 {
384                         get {
385                                 return Text;
386                         }
387                         set {
388                                 Text = value;
389                         }
390                 }
391
392                 [MonoTODO]
393                 event EventHandler IButtonControl.Click
394                 {
395                         add {
396                                 throw new NotImplementedException ();
397                         }
398                         remove {
399                                 throw new NotImplementedException ();
400                         }
401                 }
402                 
403 #endif
404         }
405 }
406