Responder.cs 9.5 KB

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