Responder.cs 7.1 KB

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