Responder.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // Core.cs: The core engine for gui.cs
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // Pending:
  8. // - Check for NeedDisplay on the hierarchy and repaint
  9. // - Layout support
  10. // - "Colors" type or "Attributes" type?
  11. // - What to surface as "BackgroundCOlor" when clearing a window, an attribute or colors?
  12. //
  13. // Optimziations
  14. // - Add rendering limitation to the exposed area
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. namespace Terminal.Gui {
  19. /// <summary>
  20. /// Responder base class implemented by objects that want to participate on keyboard and mouse input.
  21. /// </summary>
  22. public class Responder : IDisposable {
  23. bool disposedValue;
  24. #if DEBUG_IDISPOSABLE
  25. /// <summary>
  26. /// For debug purposes to verify objects are being disposed properly
  27. /// </summary>
  28. public bool WasDisposed = false;
  29. /// <summary>
  30. /// For debug purposes to verify objects are being disposed properly
  31. /// </summary>
  32. public int DisposedCount = 0;
  33. /// <summary>
  34. /// For debug purposes
  35. /// </summary>
  36. public static List<Responder> Instances = new List<Responder> ();
  37. /// <summary>
  38. /// For debug purposes
  39. /// </summary>
  40. public Responder ()
  41. {
  42. Instances.Add (this);
  43. }
  44. #endif
  45. /// <summary>
  46. /// Gets or sets a value indicating whether this <see cref="Responder"/> can focus.
  47. /// </summary>
  48. /// <value><c>true</c> if can focus; otherwise, <c>false</c>.</value>
  49. public virtual bool CanFocus { get; set; }
  50. /// <summary>
  51. /// Gets or sets a value indicating whether this <see cref="Responder"/> has focus.
  52. /// </summary>
  53. /// <value><c>true</c> if has focus; otherwise, <c>false</c>.</value>
  54. public virtual bool HasFocus { get; }
  55. // Key handling
  56. /// <summary>
  57. /// This method can be overwritten by view that
  58. /// want to provide accelerator functionality
  59. /// (Alt-key for example).
  60. /// </summary>
  61. /// <remarks>
  62. /// <para>
  63. /// Before keys are sent to the subview on the
  64. /// current view, all the views are
  65. /// processed and the key is passed to the widgets
  66. /// to allow some of them to process the keystroke
  67. /// as a hot-key. </para>
  68. /// <para>
  69. /// For example, if you implement a button that
  70. /// has a hotkey ok "o", you would catch the
  71. /// combination Alt-o here. If the event is
  72. /// caught, you must return true to stop the
  73. /// keystroke from being dispatched to other
  74. /// views.
  75. /// </para>
  76. /// </remarks>
  77. public virtual bool ProcessHotKey (KeyEvent kb)
  78. {
  79. return false;
  80. }
  81. /// <summary>
  82. /// If the view is focused, gives the view a
  83. /// chance to process the keystroke.
  84. /// </summary>
  85. /// <remarks>
  86. /// <para>
  87. /// Views can override this method if they are
  88. /// interested in processing the given keystroke.
  89. /// If they consume the keystroke, they must
  90. /// return true to stop the keystroke from being
  91. /// processed by other widgets or consumed by the
  92. /// widget engine. If they return false, the
  93. /// keystroke will be passed using the ProcessColdKey
  94. /// method to other views to process.
  95. /// </para>
  96. /// <para>
  97. /// The View implementation does nothing but return false,
  98. /// so it is not necessary to call base.ProcessKey if you
  99. /// derive directly from View, but you should if you derive
  100. /// other View subclasses.
  101. /// </para>
  102. /// </remarks>
  103. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  104. public virtual bool ProcessKey (KeyEvent keyEvent)
  105. {
  106. return false;
  107. }
  108. /// <summary>
  109. /// This method can be overwritten by views that
  110. /// want to provide accelerator functionality
  111. /// (Alt-key for example), but without
  112. /// interefering with normal ProcessKey behavior.
  113. /// </summary>
  114. /// <remarks>
  115. /// <para>
  116. /// After keys are sent to the subviews on the
  117. /// current view, all the view are
  118. /// processed and the key is passed to the views
  119. /// to allow some of them to process the keystroke
  120. /// as a cold-key. </para>
  121. /// <para>
  122. /// This functionality is used, for example, by
  123. /// default buttons to act on the enter key.
  124. /// Processing this as a hot-key would prevent
  125. /// non-default buttons from consuming the enter
  126. /// keypress when they have the focus.
  127. /// </para>
  128. /// </remarks>
  129. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  130. public virtual bool ProcessColdKey (KeyEvent keyEvent)
  131. {
  132. return false;
  133. }
  134. /// <summary>
  135. /// Method invoked when a key is pressed.
  136. /// </summary>
  137. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  138. /// <returns>true if the event was handled</returns>
  139. public virtual bool OnKeyDown (KeyEvent keyEvent)
  140. {
  141. return false;
  142. }
  143. /// <summary>
  144. /// Method invoked when a key is released.
  145. /// </summary>
  146. /// <param name="keyEvent">Contains the details about the key that produced the event.</param>
  147. /// <returns>true if the event was handled</returns>
  148. public virtual bool OnKeyUp (KeyEvent keyEvent)
  149. {
  150. return false;
  151. }
  152. /// <summary>
  153. /// Method invoked when a mouse event is generated
  154. /// </summary>
  155. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  156. /// <param name="mouseEvent">Contains the details about the mouse event.</param>
  157. public virtual bool MouseEvent (MouseEvent mouseEvent)
  158. {
  159. return false;
  160. }
  161. /// <summary>
  162. /// Method invoked when a mouse event is generated for the first time.
  163. /// </summary>
  164. /// <param name="mouseEvent"></param>
  165. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  166. public virtual bool OnMouseEnter (MouseEvent mouseEvent)
  167. {
  168. return false;
  169. }
  170. /// <summary>
  171. /// Method invoked when a mouse event is generated for the last time.
  172. /// </summary>
  173. /// <param name="mouseEvent"></param>
  174. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  175. public virtual bool OnMouseLeave (MouseEvent mouseEvent)
  176. {
  177. return false;
  178. }
  179. /// <summary>
  180. /// Method invoked when a view gets focus.
  181. /// </summary>
  182. /// <param name="view">The view that is losing focus.</param>
  183. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  184. public virtual bool OnEnter (View view)
  185. {
  186. return false;
  187. }
  188. /// <summary>
  189. /// Method invoked when a view loses focus.
  190. /// </summary>
  191. /// <param name="view">The view that is getting focus.</param>
  192. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  193. public virtual bool OnLeave (View view)
  194. {
  195. return false;
  196. }
  197. /// <summary>
  198. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  199. /// </summary>
  200. /// <remarks>
  201. /// If disposing equals true, the method has been called directly
  202. /// or indirectly by a user's code. Managed and unmanaged resources
  203. /// can be disposed.
  204. /// If disposing equals false, the method has been called by the
  205. /// runtime from inside the finalizer and you should not reference
  206. /// other objects. Only unmanaged resources can be disposed.
  207. /// </remarks>
  208. /// <param name="disposing"></param>
  209. protected virtual void Dispose (bool disposing)
  210. {
  211. if (!disposedValue) {
  212. if (disposing) {
  213. // TODO: dispose managed state (managed objects)
  214. }
  215. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  216. // TODO: set large fields to null
  217. disposedValue = true;
  218. }
  219. }
  220. /// <summary>
  221. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resource.
  222. /// </summary>
  223. public void Dispose ()
  224. {
  225. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  226. Dispose (disposing: true);
  227. GC.SuppressFinalize (this);
  228. #if DEBUG_IDISPOSABLE
  229. WasDisposed = true;
  230. #endif
  231. }
  232. }
  233. }