2 // System.Web.UI.WebControls.ImageMap.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.ComponentModel;
34 using System.Security.Permissions;
36 namespace System.Web.UI.WebControls
38 [ParseChildren (true, "HotSpots")]
39 [DefaultProperty ("HotSpots")]
40 [DefaultEvent ("Click")]
41 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public class ImageMap: Image, IPostBackEventHandler
45 HotSpotCollection spots;
47 private static readonly object ClickEvent = new object();
50 public event ImageMapEventHandler Click
52 add { Events.AddHandler (ClickEvent, value); }
53 remove { Events.RemoveHandler (ClickEvent, value); }
56 protected virtual void OnClick (ImageMapEventArgs e)
59 ImageMapEventHandler eh = (ImageMapEventHandler) Events [ClickEvent];
60 if (eh!= null) eh (this, e);
64 [DefaultValueAttribute (HotSpotMode.NotSet)]
65 public virtual HotSpotMode HotSpotMode {
67 object o = ViewState ["HotSpotMode"];
68 return o != null ? (HotSpotMode) o : HotSpotMode.NotSet;
71 ViewState ["HotSpotMode"] = value;
75 [DefaultValueAttribute ("")]
76 public virtual string Target {
78 object o = ViewState ["Target"];
79 return o != null ? (string) o : "";
82 ViewState ["Target"] = value;
86 [NotifyParentPropertyAttribute (true)]
87 [PersistenceModeAttribute (PersistenceMode.InnerDefaultProperty)]
88 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
89 public HotSpotCollection HotSpots {
92 spots = new HotSpotCollection ();
93 if (IsTrackingViewState)
94 ((IStateManager)spots).TrackViewState ();
100 protected override void TrackViewState ()
102 base.TrackViewState ();
103 if (spots != null) ((IStateManager)spots).TrackViewState ();
106 protected override object SaveViewState ()
108 object ob1 = base.SaveViewState ();
109 object ob2 = spots != null ? ((IStateManager)spots).SaveViewState () : null;
111 if (ob1 != null || ob2 != null)
112 return new Pair (ob1, ob2);
117 protected override void LoadViewState (object savedState)
119 if (savedState == null) {
120 base.LoadViewState (null);
124 Pair pair = (Pair) savedState;
125 base.LoadViewState (pair.First);
126 ((IStateManager)HotSpots).LoadViewState (pair.Second);
129 public void RaisePostBackEvent (string eventArgument)
131 HotSpot spot = HotSpots [int.Parse (eventArgument)];
132 OnClick (new ImageMapEventArgs (spot.PostBackValue));
135 protected override void AddAttributesToRender (HtmlTextWriter writer)
137 base.AddAttributesToRender (writer);
138 if (spots != null && spots.Count > 0)
139 writer.AddAttribute (HtmlTextWriterAttribute.Usemap, "#ImageMap" + ClientID);
142 protected internal override void Render (HtmlTextWriter writer)
144 base.Render (writer);
146 if (spots != null && spots.Count > 0) {
147 writer.AddAttribute (HtmlTextWriterAttribute.Name, "ImageMap" + ClientID);
148 writer.RenderBeginTag (HtmlTextWriterTag.Map);
149 for (int n=0; n<spots.Count; n++) {
150 HotSpot spot = spots [n];
151 writer.AddAttribute (HtmlTextWriterAttribute.Shape, spot.MarkupName);
152 writer.AddAttribute (HtmlTextWriterAttribute.Coords, spot.GetCoordinates ());
153 writer.AddAttribute (HtmlTextWriterAttribute.Title, spot.AlternateText);
154 writer.AddAttribute (HtmlTextWriterAttribute.Alt, spot.AlternateText);
155 if (spot.AccessKey.Length > 0)
156 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, spot.AccessKey);
157 if (spot.TabIndex != 0)
158 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, spot.TabIndex.ToString ());
160 HotSpotMode mode = spot.HotSpotMode != HotSpotMode.NotSet ? spot.HotSpotMode : HotSpotMode;
162 case HotSpotMode.Inactive:
163 writer.AddAttribute ("nohref", "true", false);
165 case HotSpotMode.Navigate:
166 string target = spot.Target.Length > 0 ? spot.Target : Target;
167 if (!String.IsNullOrEmpty (target))
168 writer.AddAttribute (HtmlTextWriterAttribute.Target, target);
170 string navUrl = ResolveClientUrl (spot.NavigateUrl, String.Compare (target, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0);
172 string navUrl = ResolveClientUrl (spot.NavigateUrl);
174 writer.AddAttribute (HtmlTextWriterAttribute.Href, navUrl);
176 case HotSpotMode.PostBack:
177 writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, n.ToString()));
181 writer.RenderBeginTag (HtmlTextWriterTag.Area);
182 writer.RenderEndTag ();
184 writer.RenderEndTag ();