Merge pull request #1458 from BrzVlad/feature-fat-cas
[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-2010 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         [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43         [SupportsEventValidation]
44         public class ImageButton : Image, IPostBackDataHandler, IPostBackEventHandler, IButtonControl
45         {
46                 static readonly object ClickEvent = new object ();
47                 static readonly object CommandEvent = new object ();
48                 int pos_x, pos_y;
49
50                 public ImageButton ()
51                 {
52
53                 }
54
55                 [DefaultValue(true)]
56                 [WebSysDescription ("")]
57                 [WebCategory ("Behavior")]
58                 [Themeable (false)]
59                 public virtual bool CausesValidation {
60                         get { return ViewState.GetBool ("CausesValidation", true); }
61                         set { ViewState ["CausesValidation"] = value; }
62                 }
63
64                 [Bindable(true)]
65                 [DefaultValue("")]
66                 [WebSysDescription ("")]
67                 [WebCategory ("Behavior")]
68                 [Themeable (false)]
69                 public string CommandArgument {
70                         get { return ViewState.GetString ("CommandArgument", String.Empty); }
71                         set { ViewState ["CommandArgument"] = value; }
72                 }
73
74                 [DefaultValue("")]
75                 [WebSysDescription ("")]
76                 [WebCategory ("Behavior")]
77                 [Themeable (false)]
78                 public string CommandName {
79                         get { return ViewState.GetString ("CommandName", String.Empty); }
80                         set { ViewState ["CommandName"] = value; }
81                 }
82
83                 [EditorBrowsable (EditorBrowsableState.Always)]
84                 [Browsable (true)]
85                 [DefaultValue (true)]
86                 [Bindable (true)]
87                 public virtual new bool Enabled {
88                         // Should there be any special code below? Doesn't look so...
89                         get { return base.Enabled; }
90                         set { base.Enabled = value; }
91                 }
92
93                 [Browsable (false)]
94                 [EditorBrowsable (EditorBrowsableState.Never)]
95                 [Themeable (false)]
96                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
97                 public override bool GenerateEmptyAlternateText {
98                         get { return false; }
99                         set { throw new NotSupportedException (); }
100                 }
101
102                 [DefaultValue ("")]
103                 [Themeable (false)]
104                 public virtual string OnClientClick {
105                         get { return ViewState.GetString ("OnClientClick", String.Empty); }
106                         set { ViewState ["OnClientClick"] = value; }
107                 }
108
109                 [Themeable (false)]
110                 [UrlProperty ("*.aspx")]
111                 [DefaultValue ("")]
112                 [Editor ("System.Web.UI.Design.UrlEditor, "  + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
113                 public virtual string PostBackUrl {
114                         get { return ViewState.GetString ("PostBackUrl", String.Empty); }
115                         set { ViewState["PostBackUrl"] = value; }
116                 }
117
118                 [Themeable (false)]
119                 [DefaultValue ("")]
120                 [WebSysDescription ("")]
121                 [WebCategory ("Behavior")]
122                 public virtual string ValidationGroup {
123                         get { return ViewState.GetString ("ValidationGroup", String.Empty); }
124                         set { ViewState ["ValidationGroup"] = value; }
125                 }
126
127                 [Browsable(false)]
128                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
129                 protected override HtmlTextWriterTag TagKey {
130                         get { return HtmlTextWriterTag.Input; }
131                 }
132
133                 // Gets or sets the value of the ImageButton control's AlternateText property. (MSDN)
134                 protected virtual string Text {
135                         get { return AlternateText; }
136                         set { AlternateText = value; }
137                 }
138                 public override bool SupportsDisabledAttribute {
139                         get { return RenderingCompatibilityLessThan40; }
140                 }
141                 protected override void AddAttributesToRender (HtmlTextWriter writer)
142                 {                       
143                         Page page = Page;
144                         if (page != null)
145                                 page.VerifyRenderingInServerForm (this);
146                         
147                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "image", false);
148                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
149
150                         base.AddAttributesToRender (writer);
151
152                         string onclick = OnClientClick;
153                         if (!String.IsNullOrEmpty (onclick))
154                                 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
155                         else
156                                 onclick = String.Empty;
157                         
158                         if (HasAttributes && Attributes ["onclick"] != null) {
159                                 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
160                                 Attributes.Remove ("onclick");
161                         }
162                         
163                         if (page != null)
164                                 onclick += GetClientScriptEventReference ();
165                         
166                         if (onclick.Length > 0)
167                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
168                 }
169
170                 internal virtual string GetClientScriptEventReference ()
171                 {
172                         PostBackOptions options = GetPostBackOptions ();
173                         Page page = Page;
174                         
175                         if (options.PerformValidation || !String.IsNullOrEmpty (options.ActionUrl))
176                                 return page != null ? page.ClientScript.GetPostBackEventReference (options, true) : String.Empty;
177                         else {
178                                 if (page != null)
179                                         page.ClientScript.RegisterForEventValidation (options);
180                                 return String.Empty;
181                         }
182                 }
183
184                 protected virtual PostBackOptions GetPostBackOptions ()
185                 {
186                         PostBackOptions options = new PostBackOptions (this);
187                         Page page = Page;
188                         
189                         options.ActionUrl = (PostBackUrl.Length > 0 ?
190                                              (page != null ? page.ResolveClientUrl (PostBackUrl) : null)
191                                              : null);
192
193                         options.Argument = String.Empty;
194                         options.ClientSubmit = true;
195                         options.RequiresJavaScriptProtocol = true;
196                         options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
197                         if (options.PerformValidation)
198                                 options.ValidationGroup = ValidationGroup;
199                         else
200                                 options.ValidationGroup = null;
201                         
202                         return options;
203                 }
204
205                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection) 
206                 {
207                         string x, y;
208                         string unique = UniqueID;
209                         x = postCollection [unique + ".x"];
210                         y = postCollection [unique + ".y"];
211                         if (!String.IsNullOrEmpty (x) && !String.IsNullOrEmpty (y)) {
212                                 pos_x = Int32.Parse(x);
213                                 pos_y = Int32.Parse(y);
214                                 Page.RegisterRequiresRaiseEvent (this);
215                                 return true;
216                         } else {
217                                 x = postCollection [unique];
218                                 if (!String.IsNullOrEmpty (x)) {
219                                         pos_x = Int32.Parse (x);
220                                         pos_y = 0;
221                                         Page.RegisterRequiresRaiseEvent (this);
222                                         return true;
223                                 }
224                         }
225
226                         return false;
227                 }
228
229                 protected virtual void RaisePostDataChangedEvent ()
230                 {
231                 }
232                 
233                 protected virtual void RaisePostBackEvent (string eventArgument)
234                 {
235                         ValidateEvent (UniqueID, String.Empty);
236                         if (CausesValidation) {
237                                 Page page = Page;
238                                 if (page != null)
239                                         page.Validate (ValidationGroup);
240                         }
241                         
242                         OnClick (new ImageClickEventArgs (pos_x, pos_y));
243                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
244                 }
245
246                 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
247                 {
248                         return LoadPostData (postDataKey, postCollection);
249                 }
250
251
252                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
253                 {
254                         RaisePostDataChangedEvent ();
255                 }
256
257                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
258                 {
259                         RaisePostBackEvent (eventArgument);
260                 }
261
262                 protected virtual void OnClick (ImageClickEventArgs e)
263                 {
264                         if (Events != null) {
265                                 ImageClickEventHandler eh = (ImageClickEventHandler) (Events [ClickEvent]);
266                                 if (eh != null)
267                                         eh (this, e);
268                         }
269                 }
270
271                 protected virtual void OnCommand (CommandEventArgs e)
272                 {
273                         if (Events != null) {
274                                 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
275                                 if (eh != null)
276                                         eh (this, e);
277                         }
278
279                         RaiseBubbleEvent (this, e);
280                 }
281
282                 protected internal override void OnPreRender (EventArgs e)
283                 {
284                         Page page = Page;
285                         if (page != null && IsEnabled)
286                                 page.RegisterRequiresPostBack (this);
287                 }
288
289                 [WebSysDescription ("")]
290                 [WebCategory ("Action")]
291                 public event ImageClickEventHandler Click {
292                         add { Events.AddHandler (ClickEvent, value); }
293                         remove { Events.RemoveHandler (ClickEvent, value); }
294                 }
295
296                 [WebSysDescription ("")]
297                 [WebCategory ("Action")]
298                 public event CommandEventHandler Command {
299                         add { Events.AddHandler (CommandEvent, value); }
300                         remove { Events.RemoveHandler (CommandEvent, value); }
301                 }
302
303                 string IButtonControl.Text  {
304                         get { return Text; }
305                         set { Text = value; }
306                 }
307
308                 event EventHandler IButtonControl.Click {
309                         add { Events.AddHandler (ClickEvent, value); }
310                         remove { Events.RemoveHandler (ClickEvent, value); }
311                 }
312         }
313 }
314
315