Responder.cs 8.8 KB

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