* attribute.cs (GetMarshal): Work even if "DefineCustom" is
[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 #if NET_2_0
130                 [MonoTODO]
131                 [DefaultValue ("")]
132                 public string ValidationGroup
133                 {
134                         get {
135                                 throw new NotImplementedException ();
136                         }
137                         set {
138                                 throw new NotImplementedException ();
139                         }
140                 }
141
142                 [MonoTODO]
143                 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
144                 {
145                         throw new NotImplementedException ();
146                 }
147
148                 [MonoTODO]
149                 protected virtual void RaisePostBackEvent (string eventArgument)
150                 {
151                         throw new NotImplementedException ();
152                 }
153
154                 [MonoTODO]
155                 protected virtual void RaisePostDataChangedEvent ()
156                 {
157                         throw new NotImplementedException ();
158                 }
159 #endif          
160
161                 bool IPostBackDataHandler.LoadPostData (string postDataKey,
162                                 NameValueCollection postCollection)
163                 {
164                         string x = postCollection [UniqueID + ".x"];
165                         string y = postCollection [UniqueID + ".y"];
166
167                         if (x != null && x.Length != 0 &&
168                                         y != null && y.Length != 0) {
169                                 clicked_x = Int32.Parse (x, CultureInfo.InvariantCulture);
170                                 clicked_y = Int32.Parse (y, CultureInfo.InvariantCulture);
171                                 Page.RegisterRequiresRaiseEvent (this);
172                                 return true;
173                         }
174
175                         return false;
176                 }
177
178                 
179                 void IPostBackDataHandler.RaisePostDataChangedEvent ()
180                 {
181                 }
182                                 
183                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
184                 {
185                         if (CausesValidation)
186 #if NET_2_0
187                                 Page.Validate (ValidationGroup);
188 #else
189                                 Page.Validate ();
190 #endif
191                         OnServerClick (new ImageClickEventArgs (clicked_x, clicked_y));
192                 }
193
194 #if NET_2_0
195                 protected internal
196 #else           
197                 protected
198 #endif          
199                 override void OnPreRender (EventArgs e)
200                 {
201                         base.OnPreRender (e);
202                         if (Page != null)
203                                 Page.RegisterRequiresPostBack (this);
204                 }
205
206                 protected virtual void OnServerClick (ImageClickEventArgs e)
207                 {
208                         EventHandler handler = Events [ServerClickEvent] as EventHandler;
209                         if (handler != null)
210                                 handler (this, e);
211                 }
212
213                 protected override void RenderAttributes (HtmlTextWriter writer)
214                 {
215                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
216                                 ClientScriptManager csm = new ClientScriptManager (Page);
217                                 writer.WriteAttribute ("onclick", csm.GetClientValidationEvent ());
218                         }
219
220                         base.RenderAttributes (writer);
221                 }
222
223                 private void SetAtt (string name, string value)
224                 {
225                         if (value == null)
226                                 Attributes.Remove (name);
227                         else
228                                 Attributes [name] = value;
229                 }
230
231                 private string GetAtt (string name)
232                 {
233                         string res = Attributes [name];
234                         if (res == null)
235                                 return String.Empty;
236                         return res;
237                 }
238
239                 [WebSysDescription("")]
240                 [WebCategory("Action")]
241                 public event ImageClickEventHandler ServerClick {
242                         add { Events.AddHandler (ServerClickEvent, value); }
243                         remove { Events.AddHandler (ServerClickEvent, value); }
244                 }
245         }
246 }
247