Button.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // Button.cs: Button control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using NStack;
  9. namespace Terminal.Gui {
  10. /// <summary>
  11. /// Button is a <see cref="View"/> that provides an item that invokes an <see cref="Action"/> when activated by the user.
  12. /// </summary>
  13. /// <remarks>
  14. /// <para>
  15. /// Provides a button showing text invokes an <see cref="Action"/> when clicked on with a mouse
  16. /// or when the user presses SPACE, ENTER, or hotkey. The hotkey is specified by the first uppercase
  17. /// letter in the button.
  18. /// </para>
  19. /// <para>
  20. /// When the button is configured as the default (<see cref="IsDefault"/>) and the user presses
  21. /// the ENTER key, if no other <see cref="View"/> processes the <see cref="KeyEvent"/>, the <see cref="Button"/>'s
  22. /// <see cref="Action"/> will be invoked.
  23. /// </para>
  24. /// </remarks>
  25. public class Button : View {
  26. ustring text;
  27. ustring shown_text;
  28. Rune hot_key;
  29. int hot_pos = -1;
  30. bool is_default;
  31. /// <summary>
  32. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
  33. /// </summary>
  34. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  35. public bool IsDefault {
  36. get => is_default;
  37. set {
  38. is_default = value;
  39. SetWidthHeight (Text, is_default);
  40. Update ();
  41. }
  42. }
  43. /// <summary>
  44. /// Clicked <see cref="Action"/>, raised when the button is clicked.
  45. /// </summary>
  46. /// <remarks>
  47. /// Client code can hook up to this event, it is
  48. /// raised when the button is activated either with
  49. /// the mouse or the keyboard.
  50. /// </remarks>
  51. public Action Clicked;
  52. /// <summary>
  53. /// Initializes a new instance of <see cref="Button"/> based on the given text at position 0,0
  54. /// </summary>
  55. /// <remarks>
  56. /// The size of the <see cref="Button"/> is computed based on the
  57. /// text length.
  58. /// </remarks>
  59. /// <param name="text">The button's text</param>
  60. /// <param name="is_default">If set, this makes the button the default button in the current view. <seealso cref="IsDefault"/></param>
  61. public Button (ustring text, bool is_default = false) : base ()
  62. {
  63. CanFocus = true;
  64. Text = text ?? string.Empty;
  65. this.IsDefault = is_default;
  66. int w = SetWidthHeight (text, is_default);
  67. Frame = new Rect (0, 0, w, 1);
  68. }
  69. /// <summary>
  70. /// Initializes a new instance of <see cref="Button"/> at the given coordinates, based on the given text
  71. /// </summary>
  72. /// <remarks>
  73. /// The size of the <see cref="Button"/> is computed based on the
  74. /// text length.
  75. /// </remarks>
  76. /// <param name="x">X position where the button will be shown.</param>
  77. /// <param name="y">Y position where the button will be shown.</param>
  78. /// <param name="text">The button's text</param>
  79. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  80. /// <summary>
  81. /// Initializes a new instance of <see cref="Button"/> at the given coordinates, based on the given text, and with the specified <see cref="IsDefault"/> value
  82. /// </summary>
  83. /// <remarks>
  84. /// If the value for is_default is true, a special
  85. /// decoration is used, and the enter key on a
  86. /// dialog would implicitly activate this button.
  87. /// </remarks>
  88. /// <param name="x">X position where the button will be shown.</param>
  89. /// <param name="y">Y position where the button will be shown.</param>
  90. /// <param name="text">The button's text</param>
  91. /// <param name="is_default">If set, this makes the button the default button in the current view, which means that if the user presses return on a view that does not handle return, it will be treated as if he had clicked on the button</param>
  92. public Button (int x, int y, ustring text, bool is_default)
  93. : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
  94. {
  95. CanFocus = true;
  96. Text = text ?? string.Empty;
  97. this.IsDefault = is_default;
  98. }
  99. int SetWidthHeight (ustring text, bool is_default)
  100. {
  101. int w = text.Length + 4 + (is_default ? 2 : 0);
  102. Width = w;
  103. Height = 1;
  104. return w;
  105. }
  106. /// <summary>
  107. /// The text displayed by this <see cref="Button"/>.
  108. /// </summary>
  109. public ustring Text {
  110. get {
  111. return text;
  112. }
  113. set {
  114. if (text?.Length != value?.Length) {
  115. SetWidthHeight (value, is_default);
  116. }
  117. text = value;
  118. Update ();
  119. }
  120. }
  121. internal void Update ()
  122. {
  123. if (IsDefault)
  124. shown_text = "[< " + text + " >]";
  125. else
  126. shown_text = "[ " + text + " ]";
  127. hot_pos = -1;
  128. hot_key = (Rune)0;
  129. int i = 0;
  130. foreach (Rune c in shown_text) {
  131. if (Rune.IsUpper (c)) {
  132. hot_key = c;
  133. hot_pos = i;
  134. break;
  135. }
  136. i++;
  137. }
  138. SetNeedsDisplay ();
  139. }
  140. ///<inheritdoc cref="Redraw(Rect)"/>
  141. public override void Redraw (Rect region)
  142. {
  143. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  144. Move (0, 0);
  145. Driver.AddStr (shown_text);
  146. if (hot_pos != -1) {
  147. Move (hot_pos, 0);
  148. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  149. Driver.AddRune (hot_key);
  150. }
  151. }
  152. ///<inheritdoc cref="PositionCursor"/>
  153. public override void PositionCursor ()
  154. {
  155. Move (hot_pos == -1 ? 1 : hot_pos, 0);
  156. }
  157. bool CheckKey (KeyEvent key)
  158. {
  159. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  160. this.SuperView.SetFocus (this);
  161. Clicked?.Invoke ();
  162. return true;
  163. }
  164. return false;
  165. }
  166. ///<inheritdoc cref="ProcessHotKey"/>
  167. public override bool ProcessHotKey (KeyEvent kb)
  168. {
  169. if (kb.IsAlt)
  170. return CheckKey (kb);
  171. return false;
  172. }
  173. ///<inheritdoc cref="ProcessColdKey"/>
  174. public override bool ProcessColdKey (KeyEvent kb)
  175. {
  176. if (IsDefault && kb.KeyValue == '\n') {
  177. if (Clicked != null)
  178. Clicked ();
  179. return true;
  180. }
  181. return CheckKey (kb);
  182. }
  183. ///<inheritdoc cref="ProcessKey"/>
  184. public override bool ProcessKey (KeyEvent kb)
  185. {
  186. var c = kb.KeyValue;
  187. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == hot_key) {
  188. if (Clicked != null)
  189. Clicked ();
  190. return true;
  191. }
  192. return base.ProcessKey (kb);
  193. }
  194. ///<inheritdoc cref="MouseEvent"/>
  195. public override bool MouseEvent(MouseEvent me)
  196. {
  197. if (me.Flags == MouseFlags.Button1Clicked) {
  198. SuperView.SetFocus (this);
  199. SetNeedsDisplay ();
  200. if (Clicked != null)
  201. Clicked ();
  202. return true;
  203. }
  204. return false;
  205. }
  206. }
  207. }