Responder.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.Linq;
  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. /// <summary>
  65. /// Method invoked when a mouse event is generated
  66. /// </summary>
  67. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  68. /// <param name="mouseEvent">Contains the details about the mouse event.</param>
  69. public virtual bool MouseEvent (MouseEvent mouseEvent)
  70. {
  71. return false;
  72. }
  73. /// <summary>
  74. /// Called when the mouse first enters the view; the view will now
  75. /// receives mouse events until the mouse leaves the view. At which time, <see cref="OnMouseLeave(Gui.MouseEvent)"/>
  76. /// will be called.
  77. /// </summary>
  78. /// <param name="mouseEvent"></param>
  79. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  80. public virtual bool OnMouseEnter (MouseEvent mouseEvent)
  81. {
  82. return false;
  83. }
  84. /// <summary>
  85. /// Called when the mouse has moved outside of the view; the view will no longer receive mouse events (until
  86. /// the mouse moves within the view again and <see cref="OnMouseEnter(Gui.MouseEvent)"/> is called).
  87. /// </summary>
  88. /// <param name="mouseEvent"></param>
  89. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  90. public virtual bool OnMouseLeave (MouseEvent mouseEvent)
  91. {
  92. return false;
  93. }
  94. /// <summary>
  95. /// Method invoked when a view gets focus.
  96. /// </summary>
  97. /// <param name="view">The view that is losing focus.</param>
  98. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  99. public virtual bool OnEnter (View view)
  100. {
  101. return false;
  102. }
  103. /// <summary>
  104. /// Method invoked when a view loses focus.
  105. /// </summary>
  106. /// <param name="view">The view that is getting focus.</param>
  107. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  108. public virtual bool OnLeave (View view)
  109. {
  110. return false;
  111. }
  112. /// <summary>
  113. /// Method invoked when the <see cref="CanFocus"/> property from a view is changed.
  114. /// </summary>
  115. public virtual void OnCanFocusChanged () { }
  116. /// <summary>
  117. /// Method invoked when the <see cref="Enabled"/> property from a view is changed.
  118. /// </summary>
  119. public virtual void OnEnabledChanged () { }
  120. /// <summary>
  121. /// Method invoked when the <see cref="Visible"/> property from a view is changed.
  122. /// </summary>
  123. public virtual void OnVisibleChanged () { }
  124. // TODO: v2 - nuke this
  125. /// <summary>
  126. /// Utilty function to determine <paramref name="method"/> is overridden in the <paramref name="subclass"/>.
  127. /// </summary>
  128. /// <param name="subclass">The view.</param>
  129. /// <param name="method">The method name.</param>
  130. /// <returns><see langword="true"/> if it's overridden, <see langword="false"/> otherwise.</returns>
  131. internal static bool IsOverridden (Responder subclass, string method)
  132. {
  133. MethodInfo m = subclass.GetType ().GetMethod (method,
  134. BindingFlags.Instance
  135. | BindingFlags.Public
  136. | BindingFlags.NonPublic
  137. | BindingFlags.DeclaredOnly);
  138. if (m == null) {
  139. return false;
  140. }
  141. return m.GetBaseDefinition ().DeclaringType != m.DeclaringType;
  142. }
  143. /// <summary>
  144. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  145. /// </summary>
  146. /// <remarks>
  147. /// If disposing equals true, the method has been called directly
  148. /// or indirectly by a user's code. Managed and unmanaged resources
  149. /// can be disposed.
  150. /// If disposing equals false, the method has been called by the
  151. /// runtime from inside the finalizer and you should not reference
  152. /// other objects. Only unmanaged resources can be disposed.
  153. /// </remarks>
  154. /// <param name="disposing"></param>
  155. protected virtual void Dispose (bool disposing)
  156. {
  157. if (!disposedValue) {
  158. if (disposing) {
  159. // TODO: dispose managed state (managed objects)
  160. }
  161. disposedValue = true;
  162. }
  163. }
  164. /// <summary>
  165. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resource.
  166. /// </summary>
  167. public void Dispose ()
  168. {
  169. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  170. Dispose (disposing: true);
  171. GC.SuppressFinalize (this);
  172. #if DEBUG_IDISPOSABLE
  173. WasDisposed = true;
  174. foreach (var instance in Instances.Where (x => x.WasDisposed).ToList ()) {
  175. Instances.Remove (instance);
  176. }
  177. #endif
  178. }
  179. }