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 [SupportsEventValidation
]
44 public class ImageMap
: Image
, IPostBackEventHandler
46 HotSpotCollection spots
;
48 static readonly object ClickEvent
= new object();
51 public event ImageMapEventHandler Click
53 add { Events.AddHandler (ClickEvent, value); }
54 remove { Events.RemoveHandler (ClickEvent, value); }
57 protected virtual void OnClick (ImageMapEventArgs e
)
60 ImageMapEventHandler eh
= (ImageMapEventHandler
) Events
[ClickEvent
];
61 if (eh
!= null) eh (this, e
);
65 [DefaultValueAttribute (HotSpotMode
.NotSet
)]
66 public virtual HotSpotMode HotSpotMode
{
68 object o
= ViewState
["HotSpotMode"];
69 return o
!= null ? (HotSpotMode
) o
: HotSpotMode
.NotSet
;
72 ViewState
["HotSpotMode"] = value;
76 [DefaultValueAttribute ("")]
77 public virtual string Target
{
79 object o
= ViewState
["Target"];
80 return o
!= null ? (string) o
: "";
83 ViewState
["Target"] = value;
87 [NotifyParentPropertyAttribute (true)]
88 [PersistenceModeAttribute (PersistenceMode
.InnerDefaultProperty
)]
89 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility
.Content
)]
90 public HotSpotCollection HotSpots
{
93 spots
= new HotSpotCollection ();
94 if (IsTrackingViewState
)
95 ((IStateManager
)spots
).TrackViewState ();
101 protected override void TrackViewState ()
103 base.TrackViewState ();
104 if (spots
!= null) ((IStateManager
)spots
).TrackViewState ();
107 protected override object SaveViewState ()
109 object ob1
= base.SaveViewState ();
110 object ob2
= spots
!= null ? ((IStateManager
)spots
).SaveViewState () : null;
112 if (ob1
!= null || ob2
!= null)
113 return new Pair (ob1
, ob2
);
118 protected override void LoadViewState (object savedState
)
120 if (savedState
== null) {
121 base.LoadViewState (null);
125 Pair pair
= (Pair
) savedState
;
126 base.LoadViewState (pair
.First
);
127 ((IStateManager
)HotSpots
).LoadViewState (pair
.Second
);
130 public void RaisePostBackEvent (string eventArgument
)
132 ValidateEvent (UniqueID
, eventArgument
);
133 HotSpot spot
= HotSpots
[int.Parse (eventArgument
)];
134 OnClick (new ImageMapEventArgs (spot
.PostBackValue
));
137 protected override void AddAttributesToRender (HtmlTextWriter writer
)
139 base.AddAttributesToRender (writer
);
140 if (spots
!= null && spots
.Count
> 0)
141 writer
.AddAttribute (HtmlTextWriterAttribute
.Usemap
, "#ImageMap" + ClientID
);
144 protected internal override void Render (HtmlTextWriter writer
)
146 base.Render (writer
);
148 if (spots
!= null && spots
.Count
> 0) {
149 writer
.AddAttribute (HtmlTextWriterAttribute
.Name
, "ImageMap" + ClientID
);
150 writer
.RenderBeginTag (HtmlTextWriterTag
.Map
);
151 for (int n
=0; n
<spots
.Count
; n
++) {
152 HotSpot spot
= spots
[n
];
153 writer
.AddAttribute (HtmlTextWriterAttribute
.Shape
, spot
.MarkupName
);
154 writer
.AddAttribute (HtmlTextWriterAttribute
.Coords
, spot
.GetCoordinates ());
155 writer
.AddAttribute (HtmlTextWriterAttribute
.Title
, spot
.AlternateText
);
156 writer
.AddAttribute (HtmlTextWriterAttribute
.Alt
, spot
.AlternateText
);
157 if (spot
.AccessKey
.Length
> 0)
158 writer
.AddAttribute (HtmlTextWriterAttribute
.Accesskey
, spot
.AccessKey
);
159 if (spot
.TabIndex
!= 0)
160 writer
.AddAttribute (HtmlTextWriterAttribute
.Tabindex
, spot
.TabIndex
.ToString ());
162 HotSpotMode mode
= spot
.HotSpotMode
!= HotSpotMode
.NotSet
? spot
.HotSpotMode
: HotSpotMode
;
164 case HotSpotMode
.Inactive
:
165 writer
.AddAttribute ("nohref", "true", false);
167 case HotSpotMode
.Navigate
:
168 string target
= spot
.Target
.Length
> 0 ? spot
.Target
: Target
;
169 if (!String
.IsNullOrEmpty (target
))
170 writer
.AddAttribute (HtmlTextWriterAttribute
.Target
, target
);
172 string navUrl
= ResolveClientUrl (spot
.NavigateUrl
, String
.Compare (target
, "_blank", StringComparison
.InvariantCultureIgnoreCase
) != 0);
174 string navUrl
= ResolveClientUrl (spot
.NavigateUrl
);
176 writer
.AddAttribute (HtmlTextWriterAttribute
.Href
, navUrl
);
178 case HotSpotMode
.PostBack
:
179 writer
.AddAttribute (HtmlTextWriterAttribute
.Href
, Page
.ClientScript
.GetPostBackClientHyperlink (this, n
.ToString(), true));
183 writer
.RenderBeginTag (HtmlTextWriterTag
.Area
);
184 writer
.RenderEndTag ();
186 writer
.RenderEndTag ();