2010-02-18 Marek Habersack <mhabersack@novell.com>
[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 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 #if NET_2_0
32
33 using System.ComponentModel;
34 using System.Security.Permissions;
35
36 namespace System.Web.UI.WebControls
37 {
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
45         {
46                 HotSpotCollection spots;
47                 
48                 static readonly object ClickEvent = new object();
49                 
50                 [Category ("Action")]
51                 public event ImageMapEventHandler Click
52                 {
53                         add { Events.AddHandler (ClickEvent, value); }
54                         remove { Events.RemoveHandler (ClickEvent, value); }
55                 }
56                 
57                 protected virtual void OnClick (ImageMapEventArgs e)
58                 {
59                         if (Events != null) {
60                                 ImageMapEventHandler eh = (ImageMapEventHandler) Events [ClickEvent];
61                                 if (eh!= null) eh (this, e);
62                         }
63                 }
64
65                 [DefaultValueAttribute (HotSpotMode.NotSet)]
66                 public virtual HotSpotMode HotSpotMode {
67                         get {
68                                 object o = ViewState ["HotSpotMode"];
69                                 return o != null ? (HotSpotMode) o : HotSpotMode.NotSet;
70                         }
71                         set {
72                                 ViewState ["HotSpotMode"] = value;
73                         }
74                 }
75                 
76                 [DefaultValueAttribute ("")]
77                 public virtual string Target {
78                         get {
79                                 object o = ViewState ["Target"];
80                                 return o != null ? (string) o : "";
81                         }
82                         set {
83                                 ViewState ["Target"] = value;
84                         }
85                 }
86
87                 [NotifyParentPropertyAttribute (true)]
88                 [PersistenceModeAttribute (PersistenceMode.InnerDefaultProperty)]
89                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
90                 public HotSpotCollection HotSpots {
91                         get {
92                                 if (spots == null) {
93                                         spots = new HotSpotCollection ();
94                                         if (IsTrackingViewState)
95                                                 ((IStateManager)spots).TrackViewState ();
96                                 }
97                                 return spots;
98                         }
99                 }
100                 
101                 protected override void TrackViewState ()
102                 {
103                         base.TrackViewState ();
104                         if (spots != null) ((IStateManager)spots).TrackViewState ();
105                 }
106                 
107                 protected override object SaveViewState ()
108                 {
109                         object ob1 = base.SaveViewState ();
110                         object ob2 = spots != null ? ((IStateManager)spots).SaveViewState () : null;
111                         
112                         if (ob1 != null || ob2 != null)
113                                 return new Pair (ob1, ob2);
114                         else
115                                 return null;
116                 }
117                 
118                 protected override void LoadViewState (object savedState)
119                 {
120                         if (savedState == null) {
121                                 base.LoadViewState (null);
122                                 return;
123                         }
124                         
125                         Pair pair = (Pair) savedState;
126                         base.LoadViewState (pair.First);
127                         ((IStateManager)HotSpots).LoadViewState (pair.Second);
128                 }
129                 
130                 public void RaisePostBackEvent (string eventArgument)
131                 {
132                         ValidateEvent (UniqueID, eventArgument);
133                         HotSpot spot = HotSpots [int.Parse (eventArgument)];
134                         OnClick (new ImageMapEventArgs (spot.PostBackValue));
135                 }
136
137                 protected override void AddAttributesToRender (HtmlTextWriter writer)
138                 {
139                         base.AddAttributesToRender (writer);
140                         if (spots != null && spots.Count > 0)
141                                 writer.AddAttribute (HtmlTextWriterAttribute.Usemap, "#ImageMap" + ClientID);
142                 }
143                 
144                 protected internal override void Render (HtmlTextWriter writer)
145                 {
146                         base.Render (writer);
147
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 ());
161                                         
162                                         HotSpotMode mode = spot.HotSpotMode != HotSpotMode.NotSet ? spot.HotSpotMode : HotSpotMode;
163                                         switch (mode) {
164                                                 case HotSpotMode.Inactive:
165                                                         writer.AddAttribute ("nohref", "true", false);
166                                                         break;
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);
171 #if TARGET_J2EE
172                                                         string navUrl = ResolveClientUrl (spot.NavigateUrl, String.Compare (target, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0);
173 #else
174                                                         string navUrl = ResolveClientUrl (spot.NavigateUrl);
175 #endif
176                                                         writer.AddAttribute (HtmlTextWriterAttribute.Href, navUrl);
177                                                         break;
178                                                 case HotSpotMode.PostBack:
179                                                         writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, n.ToString(), true));
180                                                         break;
181                                         }
182                                                 
183                                         writer.RenderBeginTag (HtmlTextWriterTag.Area);
184                                         writer.RenderEndTag ();
185                                 }
186                                 writer.RenderEndTag ();
187                         } 
188                 }
189         }
190 }
191
192 #endif