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