HtmlInputImage.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. //
  22. // System.Web.UI.HtmlControls.HtmlInputImage.cs
  23. //
  24. // Authors:
  25. // Jackson Harper ([email protected])
  26. //
  27. // (C) 2005-2010 Novell, Inc.
  28. //
  29. // TODO: getting the .x and .y in LoadData doesn't work with mozilla
  30. //
  31. using System.Globalization;
  32. using System.Collections.Specialized;
  33. using System.ComponentModel;
  34. using System.Security.Permissions;
  35. using System.Web.Util;
  36. namespace System.Web.UI.HtmlControls
  37. {
  38. // CAS
  39. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. // attributes
  42. [DefaultEvent("ServerClick")]
  43. [SupportsEventValidation]
  44. public class HtmlInputImage : HtmlInputControl, IPostBackDataHandler, IPostBackEventHandler
  45. {
  46. static readonly object ServerClickEvent = new object ();
  47. int clicked_x;
  48. int clicked_y;
  49. public HtmlInputImage () : base ("image")
  50. {
  51. }
  52. [DefaultValue(true)]
  53. [WebSysDescription("")]
  54. [WebCategory("Behavior")]
  55. public virtual bool CausesValidation {
  56. get {
  57. return ViewState.GetBool ("CausesValidation", true);
  58. }
  59. set {
  60. ViewState ["CausesValidation"] = value;
  61. }
  62. }
  63. [DefaultValue("")]
  64. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  65. [WebSysDescription("")]
  66. [WebCategory("Appearance")]
  67. public string Align {
  68. get { return GetAtt ("align"); }
  69. set { SetAtt ("align", value); }
  70. }
  71. [DefaultValue("")]
  72. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  73. [Localizable (true)]
  74. [WebSysDescription("")]
  75. [WebCategory("Appearance")]
  76. public string Alt {
  77. get { return GetAtt ("alt"); }
  78. set { SetAtt ("alt", value); }
  79. }
  80. [DefaultValue("")]
  81. [WebSysDescription("")]
  82. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  83. [WebCategory("Appearance")]
  84. [UrlProperty]
  85. public string Src {
  86. get { return GetAtt ("src"); }
  87. set { SetAtt ("src", value); }
  88. }
  89. [DefaultValue("-1")]
  90. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  91. [WebSysDescription("")]
  92. [WebCategory("Appearance")]
  93. public int Border {
  94. get {
  95. string border = Attributes ["border"];
  96. if (border == null)
  97. return -1;
  98. return Int32.Parse (border, Helpers.InvariantCulture);
  99. }
  100. set {
  101. if (value == -1) {
  102. Attributes.Remove ("border");
  103. return;
  104. }
  105. Attributes ["border"] = value.ToString (Helpers.InvariantCulture);
  106. }
  107. }
  108. bool LoadPostDataInternal (string postDataKey, NameValueCollection postCollection)
  109. {
  110. string x = postCollection [UniqueID + ".x"];
  111. string y = postCollection [UniqueID + ".y"];
  112. if (x != null && x.Length != 0 &&
  113. y != null && y.Length != 0) {
  114. clicked_x = Int32.Parse (x, Helpers.InvariantCulture);
  115. clicked_y = Int32.Parse (y, Helpers.InvariantCulture);
  116. Page.RegisterRequiresRaiseEvent (this);
  117. return true;
  118. }
  119. return false;
  120. }
  121. void RaisePostBackEventInternal (string eventArgument)
  122. {
  123. if (CausesValidation)
  124. Page.Validate (ValidationGroup);
  125. OnServerClick (new ImageClickEventArgs (clicked_x, clicked_y));
  126. }
  127. void RaisePostDataChangedEventInternal ()
  128. {
  129. /* no events to raise */
  130. }
  131. [DefaultValue ("")]
  132. public virtual string ValidationGroup
  133. {
  134. get {
  135. return ViewState.GetString ("ValidationGroup", "");
  136. }
  137. set {
  138. ViewState ["ValidationGroup"] = value;
  139. }
  140. }
  141. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  142. {
  143. return LoadPostDataInternal (postDataKey, postCollection);
  144. }
  145. protected virtual void RaisePostBackEvent (string eventArgument)
  146. {
  147. RaisePostBackEventInternal (eventArgument);
  148. }
  149. protected virtual void RaisePostDataChangedEvent ()
  150. {
  151. ValidateEvent (UniqueID, String.Empty);
  152. RaisePostDataChangedEventInternal ();
  153. }
  154. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  155. {
  156. return LoadPostData (postDataKey, postCollection);
  157. }
  158. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  159. {
  160. RaisePostDataChangedEvent();
  161. }
  162. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  163. {
  164. RaisePostBackEvent (eventArgument);
  165. }
  166. protected internal override void OnPreRender (EventArgs e)
  167. {
  168. base.OnPreRender (e);
  169. Page page = Page;
  170. if (page != null && !Disabled) {
  171. page.RegisterRequiresPostBack (this);
  172. page.RegisterEnabledControl (this);
  173. }
  174. }
  175. protected virtual void OnServerClick (ImageClickEventArgs e)
  176. {
  177. ImageClickEventHandler handler = Events [ServerClickEvent] as ImageClickEventHandler;
  178. if (handler != null)
  179. handler (this, e);
  180. }
  181. protected override void RenderAttributes (HtmlTextWriter writer)
  182. {
  183. Page page = Page;
  184. if (page != null)
  185. page.ClientScript.RegisterForEventValidation (UniqueID);
  186. if (CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup)) {
  187. ClientScriptManager csm = page.ClientScript;
  188. Attributes ["onclick"] += csm.GetClientValidationEvent (ValidationGroup);
  189. }
  190. PreProcessRelativeReference (writer,"src");
  191. base.RenderAttributes (writer);
  192. }
  193. void SetAtt (string name, string value)
  194. {
  195. if ((value == null) || (value.Length == 0))
  196. Attributes.Remove (name);
  197. else
  198. Attributes [name] = value;
  199. }
  200. string GetAtt (string name)
  201. {
  202. string res = Attributes [name];
  203. if (res == null)
  204. return String.Empty;
  205. return res;
  206. }
  207. [WebSysDescription("")]
  208. [WebCategory("Action")]
  209. public event ImageClickEventHandler ServerClick {
  210. add { Events.AddHandler (ServerClickEvent, value); }
  211. remove { Events.AddHandler (ServerClickEvent, value); }
  212. }
  213. }
  214. }