Button.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. Update ();
  40. }
  41. }
  42. /// <summary>
  43. /// Clicked <see cref="Action"/>, raised when the button is clicked.
  44. /// </summary>
  45. /// <remarks>
  46. /// Client code can hook up to this event, it is
  47. /// raised when the button is activated either with
  48. /// the mouse or the keyboard.
  49. /// </remarks>
  50. public Action Clicked;
  51. /// <summary>
  52. /// Initializes a new instance of <see cref="Button"/> based on the given text at position 0,0
  53. /// </summary>
  54. /// <remarks>
  55. /// The size of the <see cref="Button"/> is computed based on the
  56. /// text length.
  57. /// </remarks>
  58. /// <param name="text">The button's text</param>
  59. /// <param name="is_default">If set, this makes the button the default button in the current view. <seealso cref="IsDefault"/></param>
  60. public Button (ustring text, bool is_default = false) : base ()
  61. {
  62. CanFocus = true;
  63. this.IsDefault = is_default;
  64. Text = text;
  65. int w = SetWidthHeight (text, is_default);
  66. Frame = new Rect (0, 0, w, 1);
  67. }
  68. int SetWidthHeight (ustring text, bool is_default)
  69. {
  70. int w = text.Length + 4 + (is_default ? 2 : 0);
  71. Width = w;
  72. Height = 1;
  73. return w;
  74. }
  75. /// <summary>
  76. /// Initializes a new instance of <see cref="Button"/> at the given coordinates, based on the given text
  77. /// </summary>
  78. /// <remarks>
  79. /// The size of the <see cref="Button"/> is computed based on the
  80. /// text length.
  81. /// </remarks>
  82. /// <param name="x">X position where the button will be shown.</param>
  83. /// <param name="y">Y position where the button will be shown.</param>
  84. /// <param name="text">The button's text</param>
  85. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  86. /// <summary>
  87. /// The text displayed by this <see cref="Button"/>.
  88. /// </summary>
  89. public ustring Text {
  90. get {
  91. return text;
  92. }
  93. set {
  94. if (text?.Length != value?.Length) {
  95. SetWidthHeight (value, is_default);
  96. }
  97. text = value;
  98. Update ();
  99. }
  100. }
  101. internal void Update ()
  102. {
  103. if (IsDefault)
  104. shown_text = "[< " + text + " >]";
  105. else
  106. shown_text = "[ " + text + " ]";
  107. hot_pos = -1;
  108. hot_key = (Rune)0;
  109. int i = 0;
  110. foreach (Rune c in shown_text) {
  111. if (Rune.IsUpper (c)) {
  112. hot_key = c;
  113. hot_pos = i;
  114. break;
  115. }
  116. i++;
  117. }
  118. SetNeedsDisplay ();
  119. }
  120. /// <summary>
  121. /// 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
  122. /// </summary>
  123. /// <remarks>
  124. /// If the value for is_default is true, a special
  125. /// decoration is used, and the enter key on a
  126. /// dialog would implicitly activate this button.
  127. /// </remarks>
  128. /// <param name="x">X position where the button will be shown.</param>
  129. /// <param name="y">Y position where the button will be shown.</param>
  130. /// <param name="text">The button's text</param>
  131. /// <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>
  132. public Button (int x, int y, ustring text, bool is_default)
  133. : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
  134. {
  135. CanFocus = true;
  136. this.IsDefault = is_default;
  137. Text = text;
  138. }
  139. ///<inheritdoc cref="Redraw(Rect)"/>
  140. public override void Redraw (Rect region)
  141. {
  142. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  143. Move (0, 0);
  144. Driver.AddStr (shown_text);
  145. if (hot_pos != -1) {
  146. Move (hot_pos, 0);
  147. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  148. Driver.AddRune (hot_key);
  149. }
  150. }
  151. ///<inheritdoc cref="PositionCursor"/>
  152. public override void PositionCursor ()
  153. {
  154. Move (hot_pos == -1 ? 1 : hot_pos, 0);
  155. }
  156. bool CheckKey (KeyEvent key)
  157. {
  158. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  159. this.SuperView.SetFocus (this);
  160. Clicked?.Invoke ();
  161. return true;
  162. }
  163. return false;
  164. }
  165. ///<inheritdoc cref="ProcessHotKey"/>
  166. public override bool ProcessHotKey (KeyEvent kb)
  167. {
  168. if (kb.IsAlt)
  169. return CheckKey (kb);
  170. return false;
  171. }
  172. ///<inheritdoc cref="ProcessColdKey"/>
  173. public override bool ProcessColdKey (KeyEvent kb)
  174. {
  175. if (IsDefault && kb.KeyValue == '\n') {
  176. if (Clicked != null)
  177. Clicked ();
  178. return true;
  179. }
  180. return CheckKey (kb);
  181. }
  182. ///<inheritdoc cref="ProcessKey"/>
  183. public override bool ProcessKey (KeyEvent kb)
  184. {
  185. var c = kb.KeyValue;
  186. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == hot_key) {
  187. if (Clicked != null)
  188. Clicked ();
  189. return true;
  190. }
  191. return base.ProcessKey (kb);
  192. }
  193. ///<inheritdoc cref="MouseEvent"/>
  194. public override bool MouseEvent(MouseEvent me)
  195. {
  196. if (me.Flags == MouseFlags.Button1Clicked) {
  197. SuperView.SetFocus (this);
  198. SetNeedsDisplay ();
  199. if (Clicked != null)
  200. Clicked ();
  201. return true;
  202. }
  203. return false;
  204. }
  205. }
  206. }