Responder.cs 7.6 KB

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