refactoring
[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                 [MonoTODO]
145                 public virtual string OnClientClick 
146                 {
147                         get {
148                                 throw new NotImplementedException ();
149                         }
150                         set {
151                                 throw new NotImplementedException ();
152                         }
153                 }
154
155                 [Themeable (false)]
156 #if NET_2_0
157                 [UrlProperty ("*.aspx")]
158 #else
159                 [UrlProperty]
160 #endif
161                 [DefaultValue ("")]
162                 [Editor ("System.Web.UI.Design.UrlEditor, "  + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
163                 public virtual string PostBackUrl {
164                         get {
165                                 return ViewState.GetString ("PostBackUrl", String.Empty);
166                         }
167                         set {
168                                 ViewState["PostBackUrl"] = value;
169                         }
170                 }
171
172                 [Themeable (false)]
173                 [DefaultValue ("")]
174                 [WebSysDescription ("")]
175                 [WebCategory ("Behavior")]
176                 public virtual string ValidationGroup
177                 {
178                         get {
179                                 return ViewState.GetString ("ValidationGroup", "");
180                         }
181                         set {
182                                 ViewState ["ValidationGroup"] = value;
183                         }
184                 }
185 #endif          
186
187                 [Browsable(false)]
188                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
189 #if NET_2_0 && HAVE_CONTROL_ADAPTERS
190                 protected virtual new
191 #else           
192                 protected override
193 #endif
194                 HtmlTextWriterTag TagKey {
195                         get { return HtmlTextWriterTag.Input; }
196                 }
197
198 #if NET_2_0
199                 [MonoTODO]
200                 protected virtual string Text 
201                 {
202                         get {
203                                 throw new NotImplementedException ();
204                         }
205                         set {
206                                 throw new NotImplementedException ();
207                         }
208                 }
209 #endif          
210
211                 protected override void AddAttributesToRender (HtmlTextWriter writer)
212                 {
213                         if (Page != null)
214                                 Page.VerifyRenderingInServerForm (this);
215
216                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "image");
217                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
218                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
219                                 ClientScriptManager csm = new ClientScriptManager (Page);
220                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, csm.GetClientValidationEvent ());
221                                 writer.AddAttribute ("language", "javascript");
222                         }
223                         base.AddAttributesToRender (writer);
224                 }
225
226 #if NET_2_0
227                 protected virtual PostBackOptions GetPostBackOptions ()
228                 {
229                         return new PostBackOptions(this, PostBackUrl, null, false, true,
230                                                    false, true, CausesValidation,
231                                                    ValidationGroup);
232                 }
233 #endif          
234
235
236 #if NET_2_0
237                 protected virtual
238 #endif
239                 bool LoadPostData (string postDataKey, NameValueCollection postCollection) 
240                 {
241                         string x, y;
242                         string unique = UniqueID;
243                         x = postCollection [unique + ".x"];
244                         y = postCollection [unique + ".y"];
245                         if (x != null && x != "" && y != null && y != "") {
246                                 pos_x = Int32.Parse(x);
247                                 pos_y = Int32.Parse(y);
248                                 Page.RegisterRequiresRaiseEvent (this);
249                                 return true;
250                         } else {
251                                 x = postCollection [unique];
252                                 if (x != null && x != "") {
253                                         pos_x = Int32.Parse (x);
254                                         pos_y = 0;
255                                         Page.RegisterRequiresRaiseEvent (this);
256                                         return true;
257                                 }
258                         }
259
260                         return false;
261                 }
262 #if NET_2_0
263                 protected virtual
264 #endif
265                 void RaisePostDataChangedEvent ()
266                 {
267                         if (CausesValidation)
268 #if NET_2_0
269                                 Page.Validate (ValidationGroup);
270 #else
271                                 Page.Validate ();
272 #endif
273
274                         OnClick (new ImageClickEventArgs (pos_x, pos_y));
275                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
276                 }
277                 
278                 [MonoTODO]
279 #if NET_2_0
280                 protected virtual
281 #endif
282                 void RaisePostBackEvent (string eventArgument)
283                 {
284                 }
285
286                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
287                 {
288                         return LoadPostData (postDataKey, postCollection);
289                 }
290
291
292                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
293                 {
294                         RaisePostDataChangedEvent ();
295                 }
296
297                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
298                 {
299                         RaisePostBackEvent (eventArgument);
300                 }
301
302                 protected virtual void OnClick (ImageClickEventArgs e)
303                 {
304                         if (Events != null) {
305                                 ImageClickEventHandler eh = (ImageClickEventHandler) (Events [ClickEvent]);
306                                 if (eh != null)
307                                         eh (this, e);
308                         }
309                 }
310
311                 protected virtual void OnCommand (CommandEventArgs e)
312                 {
313                         if (Events != null) {
314                                 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
315                                 if (eh != null)
316                                         eh (this, e);
317                         }
318
319                         RaiseBubbleEvent (this, e);
320                 }
321
322 #if NET_2_0
323                 protected internal
324 #else           
325                 protected
326 #endif          
327                 override void OnPreRender (EventArgs e)
328                 {
329                         if (Page != null)
330                                 Page.RegisterRequiresPostBack (this);
331                 }
332
333                 [WebSysDescription ("")]
334                 [WebCategory ("Action")]
335                 public event ImageClickEventHandler Click
336                 {
337                         add {
338                                 Events.AddHandler (ClickEvent, value);
339                         }
340                         remove {
341                                 Events.RemoveHandler (ClickEvent, value);
342                         }
343                 }
344
345                 [WebSysDescription ("")]
346                 [WebCategory ("Action")]
347                 public event CommandEventHandler Command
348                 {
349                         add {
350                                 Events.AddHandler (CommandEvent, value);
351                         }
352                         remove {
353                                 Events.RemoveHandler (CommandEvent, value);
354                         }
355                 }
356
357 #if NET_2_0
358                 [MonoTODO]
359                 string IButtonControl.Text 
360                 {
361                         get {
362                                 throw new NotImplementedException ();
363                         }
364                         set {
365                                 throw new NotImplementedException ();
366                         }
367                 }
368
369                 [MonoTODO]
370                 event EventHandler IButtonControl.Click
371                 {
372                         add {
373                                 throw new NotImplementedException ();
374                         }
375                         remove {
376                                 throw new NotImplementedException ();
377                         }
378                 }
379                 
380 #endif
381         }
382 }
383