Merge pull request #2916 from ludovic-henry/fix-40306
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ImageMap.cs
1 //
2 // System.Web.UI.WebControls.ImageMap.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.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.ComponentModel;
32 using System.Security.Permissions;
33
34 namespace System.Web.UI.WebControls
35 {
36         [ParseChildren (true, "HotSpots")]
37         [DefaultProperty ("HotSpots")]
38         [DefaultEvent ("Click")]
39         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [SupportsEventValidation]
42         public class ImageMap: Image, IPostBackEventHandler
43         {
44                 HotSpotCollection spots;
45                 
46                 static readonly object ClickEvent = new object();
47                 
48                 [Category ("Action")]
49                 public event ImageMapEventHandler Click {
50                         add { Events.AddHandler (ClickEvent, value); }
51                         remove { Events.RemoveHandler (ClickEvent, value); }
52                 }
53                 
54                 protected virtual void OnClick (ImageMapEventArgs e)
55                 {
56                         if (Events != null) {
57                                 ImageMapEventHandler eh = (ImageMapEventHandler) Events [ClickEvent];
58                                 if (eh!= null)
59                                         eh (this, e);
60                         }
61                 }
62
63                 // Why override?
64                 [Browsable (true)]
65                 [EditorBrowsable (EditorBrowsableState.Always)]
66                 public override bool Enabled {
67                         get { return base.Enabled; }
68                         set { base.Enabled = value; }
69                 }
70                 
71                 [DefaultValueAttribute (HotSpotMode.NotSet)]
72                 public virtual HotSpotMode HotSpotMode {
73                         get {
74                                 object o = ViewState ["HotSpotMode"];
75                                 return o != null ? (HotSpotMode) o : HotSpotMode.NotSet;
76                         }
77                         set { ViewState ["HotSpotMode"] = value; }
78                 }
79                 
80                 [DefaultValueAttribute ("")]
81                 public virtual string Target {
82                         get {
83                                 object o = ViewState ["Target"];
84                                 return o != null ? (string) o : String.Empty;
85                         }
86                         set { ViewState ["Target"] = value; }
87                 }
88
89                 [NotifyParentPropertyAttribute (true)]
90                 [PersistenceModeAttribute (PersistenceMode.InnerDefaultProperty)]
91                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
92                 public HotSpotCollection HotSpots {
93                         get {
94                                 if (spots == null) {
95                                         spots = new HotSpotCollection ();
96                                         if (IsTrackingViewState)
97                                                 ((IStateManager)spots).TrackViewState ();
98                                 }
99                                 return spots;
100                         }
101                 }
102                 
103                 protected override void TrackViewState ()
104                 {
105                         base.TrackViewState ();
106                         if (spots != null)
107                                 ((IStateManager)spots).TrackViewState ();
108                 }
109                 
110                 protected override object SaveViewState ()
111                 {
112                         object ob1 = base.SaveViewState ();
113                         object ob2 = spots != null ? ((IStateManager)spots).SaveViewState () : null;
114                         
115                         if (ob1 != null || ob2 != null)
116                                 return new Pair (ob1, ob2);
117                         else
118                                 return null;
119                 }
120                 
121                 protected override void LoadViewState (object savedState)
122                 {
123                         if (savedState == null) {
124                                 base.LoadViewState (null);
125                                 return;
126                         }
127                         
128                         Pair pair = (Pair) savedState;
129                         base.LoadViewState (pair.First);
130                         ((IStateManager)HotSpots).LoadViewState (pair.Second);
131                 }
132
133                 protected virtual void RaisePostBackEvent (string eventArgument)
134                 {
135                         ValidateEvent (UniqueID, eventArgument);
136                         HotSpot spot = HotSpots [int.Parse (eventArgument)];
137                         OnClick (new ImageMapEventArgs (spot.PostBackValue));
138                 }
139
140                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
141                 {
142                         RaisePostBackEvent (eventArgument);
143                 }
144                 
145                 protected override void AddAttributesToRender (HtmlTextWriter writer)
146                 {
147                         base.AddAttributesToRender (writer);
148                         if (spots != null && spots.Count > 0)
149                                 writer.AddAttribute (HtmlTextWriterAttribute.Usemap, "#ImageMap" + ClientID);
150                 }
151                 
152                 protected internal override void Render (HtmlTextWriter writer)
153                 {
154                         base.Render (writer);
155
156                         if (spots != null && spots.Count > 0) {
157                                 bool enabled = Enabled;
158                                 writer.AddAttribute (HtmlTextWriterAttribute.Id, "ImageMap" + ClientID);
159                                 writer.AddAttribute (HtmlTextWriterAttribute.Name, "ImageMap" + ClientID);
160                                 writer.RenderBeginTag (HtmlTextWriterTag.Map);
161                                 for (int n=0; n<spots.Count; n++) {
162                                         HotSpot spot = spots [n];
163                                         writer.AddAttribute (HtmlTextWriterAttribute.Shape, spot.MarkupName);
164                                         writer.AddAttribute (HtmlTextWriterAttribute.Coords, spot.GetCoordinates ());
165                                         writer.AddAttribute (HtmlTextWriterAttribute.Title, spot.AlternateText);
166                                         writer.AddAttribute (HtmlTextWriterAttribute.Alt, spot.AlternateText);
167                                         if (spot.AccessKey.Length > 0)
168                                                 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, spot.AccessKey);
169                                         if (spot.TabIndex != 0)
170                                                 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, spot.TabIndex.ToString ());
171                                         
172                                         HotSpotMode mode = spot.HotSpotMode != HotSpotMode.NotSet ? spot.HotSpotMode : HotSpotMode;
173                                         switch (mode) {
174                                                 case HotSpotMode.Inactive:
175                                                         writer.AddAttribute ("nohref", "true", false);
176                                                         break;
177                                                 case HotSpotMode.Navigate:
178                                                         string target = spot.Target.Length > 0 ? spot.Target : Target;
179                                                         if (!String.IsNullOrEmpty (target))
180                                                                 writer.AddAttribute (HtmlTextWriterAttribute.Target, target);
181                                                         if (enabled) {
182                                                                 string navUrl = ResolveClientUrl (spot.NavigateUrl);
183                                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, navUrl);
184                                                         }
185                                                         break;
186                                                 case HotSpotMode.PostBack:
187                                                         writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, n.ToString(), true));
188                                                         break;
189                                         }
190                                                 
191                                         writer.RenderBeginTag (HtmlTextWriterTag.Area);
192                                         writer.RenderEndTag ();
193                                 }
194                                 writer.RenderEndTag ();
195                         } 
196                 }
197         }
198 }
199