ImageMap.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // System.Web.UI.WebControls.ImageMap.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.ComponentModel;
  31. using System.Security.Permissions;
  32. namespace System.Web.UI.WebControls
  33. {
  34. [ParseChildren (true, "HotSpots")]
  35. [DefaultProperty ("HotSpots")]
  36. [DefaultEvent ("Click")]
  37. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public class ImageMap: Image, IPostBackEventHandler
  40. {
  41. HotSpotCollection spots;
  42. private static readonly object ClickEvent = new object();
  43. [Category ("Action")]
  44. public event ImageMapEventHandler Click
  45. {
  46. add { Events.AddHandler (ClickEvent, value); }
  47. remove { Events.RemoveHandler (ClickEvent, value); }
  48. }
  49. protected virtual void OnClick (ImageMapEventArgs e)
  50. {
  51. if (Events != null) {
  52. ImageMapEventHandler eh = (ImageMapEventHandler) Events [ClickEvent];
  53. if (eh!= null) eh (this, e);
  54. }
  55. }
  56. [DefaultValueAttribute (HotSpotMode.NotSet)]
  57. public virtual HotSpotMode HotSpotMode {
  58. get {
  59. object o = ViewState ["HotSpotMode"];
  60. return o != null ? (HotSpotMode) o : HotSpotMode.NotSet;
  61. }
  62. set {
  63. ViewState ["HotSpotMode"] = value;
  64. }
  65. }
  66. [DefaultValueAttribute ("")]
  67. public virtual string Target {
  68. get {
  69. object o = ViewState ["Target"];
  70. return o != null ? (string) o : "";
  71. }
  72. set {
  73. ViewState ["Target"] = value;
  74. }
  75. }
  76. [NotifyParentPropertyAttribute (true)]
  77. [PersistenceModeAttribute (PersistenceMode.InnerDefaultProperty)]
  78. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  79. public HotSpotCollection HotSpots {
  80. get {
  81. if (spots == null) {
  82. spots = new HotSpotCollection ();
  83. if (IsTrackingViewState)
  84. ((IStateManager)spots).TrackViewState ();
  85. }
  86. return spots;
  87. }
  88. }
  89. protected override void TrackViewState ()
  90. {
  91. base.TrackViewState ();
  92. if (spots != null) ((IStateManager)spots).TrackViewState ();
  93. }
  94. protected override object SaveViewState ()
  95. {
  96. object ob1 = base.SaveViewState ();
  97. object ob2 = spots != null ? ((IStateManager)spots).SaveViewState () : null;
  98. if (ob1 != null || ob2 != null)
  99. return new Pair (ob1, ob2);
  100. else
  101. return null;
  102. }
  103. protected override void LoadViewState (object savedState)
  104. {
  105. if (savedState == null) {
  106. base.LoadViewState (null);
  107. return;
  108. }
  109. Pair pair = (Pair) savedState;
  110. base.LoadViewState (pair.First);
  111. ((IStateManager)HotSpots).LoadViewState (pair.Second);
  112. }
  113. public void RaisePostBackEvent (string eventArgument)
  114. {
  115. HotSpot spot = HotSpots [int.Parse (eventArgument)];
  116. OnClick (new ImageMapEventArgs (spot.PostBackValue));
  117. }
  118. protected override void AddAttributesToRender (HtmlTextWriter writer)
  119. {
  120. base.AddAttributesToRender (writer);
  121. if (spots != null && spots.Count > 0)
  122. writer.AddAttribute (HtmlTextWriterAttribute.Usemap, "#ImageMap" + ClientID);
  123. }
  124. protected internal override void Render (HtmlTextWriter writer)
  125. {
  126. base.Render (writer);
  127. if (spots != null && spots.Count > 0) {
  128. writer.AddAttribute (HtmlTextWriterAttribute.Name, "ImageMap" + ClientID);
  129. writer.RenderBeginTag (HtmlTextWriterTag.Map);
  130. for (int n=0; n<spots.Count; n++) {
  131. HotSpot spot = spots [n];
  132. writer.AddAttribute (HtmlTextWriterAttribute.Shape, spot.MarkupName);
  133. writer.AddAttribute (HtmlTextWriterAttribute.Coords, spot.GetCoordinates ());
  134. writer.AddAttribute (HtmlTextWriterAttribute.Title, spot.AlternateText);
  135. writer.AddAttribute (HtmlTextWriterAttribute.Alt, spot.AlternateText);
  136. if (spot.AccessKey.Length > 0)
  137. writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, spot.AccessKey);
  138. if (spot.TabIndex != 0)
  139. writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, spot.TabIndex.ToString ());
  140. HotSpotMode mode = spot.HotSpotMode != HotSpotMode.NotSet ? spot.HotSpotMode : HotSpotMode;
  141. switch (mode) {
  142. case HotSpotMode.Inactive:
  143. writer.AddAttribute ("nohref", "true");
  144. break;
  145. case HotSpotMode.Navigate:
  146. string target = spot.Target.Length > 0 ? spot.Target : Target;
  147. if (!String.IsNullOrEmpty (target))
  148. writer.AddAttribute (HtmlTextWriterAttribute.Target, target);
  149. #if TARGET_J2EE
  150. string navUrl = ResolveClientUrl (spot.NavigateUrl, String.Compare (target, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0);
  151. #else
  152. string navUrl = ResolveClientUrl (spot.NavigateUrl);
  153. #endif
  154. writer.AddAttribute (HtmlTextWriterAttribute.Href, navUrl);
  155. break;
  156. case HotSpotMode.PostBack:
  157. writer.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, n.ToString()));
  158. break;
  159. }
  160. writer.RenderBeginTag (HtmlTextWriterTag.Area);
  161. writer.RenderEndTag ();
  162. }
  163. writer.RenderEndTag ();
  164. }
  165. }
  166. }
  167. }
  168. #endif