2 // System.Web.UI.WebControls.ImageMap.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005-2010 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.
31 using System.ComponentModel;
32 using System.Security.Permissions;
34 namespace System.Web.UI.WebControls
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
44 HotSpotCollection spots;
46 static readonly object ClickEvent = new object();
49 public event ImageMapEventHandler Click {
50 add { Events.AddHandler (ClickEvent, value); }
51 remove { Events.RemoveHandler (ClickEvent, value); }
54 protected virtual void OnClick (ImageMapEventArgs e)
57 ImageMapEventHandler eh = (ImageMapEventHandler) Events [ClickEvent];
65 [EditorBrowsable (EditorBrowsableState.Always)]
66 public override bool Enabled {
67 get { return base.Enabled; }
68 set { base.Enabled = value; }
71 [DefaultValueAttribute (HotSpotMode.NotSet)]
72 public virtual HotSpotMode HotSpotMode {
74 object o = ViewState ["HotSpotMode"];
75 return o != null ? (HotSpotMode) o : HotSpotMode.NotSet;
77 set { ViewState ["HotSpotMode"] = value; }
80 [DefaultValueAttribute ("")]
81 public virtual string Target {
83 object o = ViewState ["Target"];
84 return o != null ? (string) o : String.Empty;
86 set { ViewState ["Target"] = value; }
89 [NotifyParentPropertyAttribute (true)]
90 [PersistenceModeAttribute (PersistenceMode.InnerDefaultProperty)]
91 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
92 public HotSpotCollection HotSpots {
95 spots = new HotSpotCollection ();
96 if (IsTrackingViewState)
97 ((IStateManager)spots).TrackViewState ();
103 protected override void TrackViewState ()
105 base.TrackViewState ();
107 ((IStateManager)spots).TrackViewState ();
110 protected override object SaveViewState ()
112 object ob1 = base.SaveViewState ();
113 object ob2 = spots != null ? ((IStateManager)spots).SaveViewState () : null;
115 if (ob1 != null || ob2 != null)
116 return new Pair (ob1, ob2);
121 protected override void LoadViewState (object savedState)
123 if (savedState == null) {
124 base.LoadViewState (null);
128 Pair pair = (Pair) savedState;
129 base.LoadViewState (pair.First);
130 ((IStateManager)HotSpots).LoadViewState (pair.Second);
133 protected virtual void RaisePostBackEvent (string eventArgument)
135 ValidateEvent (UniqueID, eventArgument);
136 HotSpot spot = HotSpots [int.Parse (eventArgument)];
137 OnClick (new ImageMapEventArgs (spot.PostBackValue));
140 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
142 RaisePostBackEvent (eventArgument);
145 protected override void AddAttributesToRender (HtmlTextWriter writer)
147 base.AddAttributesToRender (writer);
148 if (spots != null && spots.Count > 0)
149 writer.AddAttribute (HtmlTextWriterAttribute.Usemap, "#ImageMap" + ClientID);
152 protected internal override void Render (HtmlTextWriter writer)
154 base.Render (writer);
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 ());
172 HotSpotMode mode = spot.HotSpotMode != HotSpotMode.NotSet ? spot.HotSpotMode : HotSpotMode;
174 case HotSpotMode.Inactive:
175 writer.AddAttribute ("nohref", "true", false);
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);
182 string navUrl = ResolveClientUrl (spot.NavigateUrl);
183 writer.AddAttribute (HtmlTextWriterAttribute.Href, navUrl);
186 case HotSpotMode.PostBack:
187 writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, n.ToString(), true));
191 writer.RenderBeginTag (HtmlTextWriterTag.Area);
192 writer.RenderEndTag ();
194 writer.RenderEndTag ();