HtmlInputImage.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 Novell, Inc.
  28. //
  29. // TODO: getting the .x and .y in LoadData doesn't work with mozilla
  30. //
  31. using System;
  32. using System.Globalization;
  33. using System.Collections.Specialized;
  34. using System.ComponentModel;
  35. namespace System.Web.UI.HtmlControls {
  36. [DefaultEvent("ServerClick")]
  37. public class HtmlInputImage : HtmlInputControl, IPostBackDataHandler,
  38. IPostBackEventHandler {
  39. private static readonly object ServerClickEvent = new object ();
  40. private int clicked_x;
  41. private int clicked_y;
  42. public HtmlInputImage () : base ("image")
  43. {
  44. }
  45. [DefaultValue(true)]
  46. #if NET_2_0
  47. public virtual
  48. #else
  49. public
  50. #endif
  51. bool CausesValidation {
  52. get {
  53. return ViewState.GetBool ("CausesValidation", true);
  54. }
  55. set {
  56. ViewState ["CausesValidation"] = value;
  57. }
  58. }
  59. [DefaultValue("")]
  60. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  61. public string Align {
  62. get { return GetAtt ("align"); }
  63. set { SetAtt ("align", value); }
  64. }
  65. [DefaultValue("")]
  66. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  67. #if NET_2_0
  68. [Localizable (true)]
  69. #endif
  70. public string Alt {
  71. get { return GetAtt ("alt"); }
  72. set { SetAtt ("alt", value); }
  73. }
  74. [DefaultValue("")]
  75. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  76. public string Src {
  77. get { return GetAtt ("src"); }
  78. set { SetAtt ("src", value); }
  79. }
  80. #if NET_2_0
  81. [DefaultValue("-1")]
  82. #else
  83. [DefaultValue("")]
  84. #endif
  85. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  86. public int Border {
  87. get {
  88. string border = Attributes ["border"];
  89. if (border == null)
  90. return -1;
  91. return Int32.Parse (border, CultureInfo.InvariantCulture);
  92. }
  93. set {
  94. if (value == -1) {
  95. Attributes.Remove ("border");
  96. return;
  97. }
  98. Attributes ["border"] = value.ToString (CultureInfo.InvariantCulture);
  99. }
  100. }
  101. #if NET_2_0
  102. [MonoTODO]
  103. [DefaultValue ("")]
  104. public string ValidationGroup
  105. {
  106. get {
  107. throw new NotImplementedException ();
  108. }
  109. set {
  110. throw new NotImplementedException ();
  111. }
  112. }
  113. [MonoTODO]
  114. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  115. {
  116. throw new NotImplementedException ();
  117. }
  118. [MonoTODO]
  119. protected virtual void RaisePostBackEvent (string eventArgument)
  120. {
  121. throw new NotImplementedException ();
  122. }
  123. [MonoTODO]
  124. protected virtual void RaisePostDataChangedEvent ()
  125. {
  126. throw new NotImplementedException ();
  127. }
  128. #endif
  129. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  130. NameValueCollection postCollection)
  131. {
  132. string x = postCollection [UniqueID + ".x"];
  133. string y = postCollection [UniqueID + ".y"];
  134. if (x != null && x.Length != 0 &&
  135. y != null && y.Length != 0) {
  136. clicked_x = Int32.Parse (x, CultureInfo.InvariantCulture);
  137. clicked_y = Int32.Parse (y, CultureInfo.InvariantCulture);
  138. Page.RegisterRequiresRaiseEvent (this);
  139. return true;
  140. }
  141. return false;
  142. }
  143. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  144. {
  145. }
  146. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  147. {
  148. if (CausesValidation)
  149. #if NET_2_0
  150. Page.Validate (ValidationGroup);
  151. #else
  152. Page.Validate ();
  153. #endif
  154. OnServerClick (new ImageClickEventArgs (clicked_x, clicked_y));
  155. }
  156. #if NET_2_0
  157. protected internal
  158. #else
  159. protected
  160. #endif
  161. override void OnPreRender (EventArgs e)
  162. {
  163. base.OnPreRender (e);
  164. if (Page != null)
  165. Page.RegisterRequiresPostBack (this);
  166. }
  167. protected virtual void OnServerClick (ImageClickEventArgs e)
  168. {
  169. EventHandler handler = Events [ServerClickEvent] as EventHandler;
  170. if (handler != null)
  171. handler (this, e);
  172. }
  173. protected override void RenderAttributes (HtmlTextWriter writer)
  174. {
  175. if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
  176. ClientScriptManager csm = new ClientScriptManager (Page);
  177. writer.WriteAttribute ("onclick", csm.GetClientValidationEvent ());
  178. }
  179. base.RenderAttributes (writer);
  180. }
  181. private void SetAtt (string name, string value)
  182. {
  183. if (value == null)
  184. Attributes.Remove (name);
  185. else
  186. Attributes [name] = value;
  187. }
  188. private string GetAtt (string name)
  189. {
  190. string res = Attributes [name];
  191. if (res == null)
  192. return String.Empty;
  193. return res;
  194. }
  195. public event ImageClickEventHandler ServerClick {
  196. add { Events.AddHandler (ServerClickEvent, value); }
  197. remove { Events.AddHandler (ServerClickEvent, value); }
  198. }
  199. }
  200. }