Responder.cs 6.4 KB

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