Responder.cs 7.4 KB

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