Button.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 view that provides an item that invokes a callback when activated.
  12. /// </summary>
  13. /// <remarks>
  14. /// <para>
  15. /// Provides a button that can be clicked, or pressed with
  16. /// the enter key and processes hotkeys (the first uppercase
  17. /// letter in the button becomes the hotkey).
  18. /// </para>
  19. /// <para>
  20. /// If the button is configured as the default (IsDefault) the button
  21. /// will respond to the return key is no other view processes it, and
  22. /// turns this into a clicked event.
  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 a value indicating whether this <see cref="T:Terminal.Gui.Button"/> is the default action to activate on return on 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 event, 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. /// Public constructor, creates a button based on
  53. /// the given text at position 0,0
  54. /// </summary>
  55. /// <remarks>
  56. /// The size of the button is computed based on the
  57. /// text length. This button is not a default button.
  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, 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>
  61. public Button (ustring text, bool is_default = false) : base ()
  62. {
  63. CanFocus = true;
  64. this.IsDefault = is_default;
  65. Text = text;
  66. int w = SetWidthHeight (text, is_default);
  67. Frame = new Rect (0, 0, w, 1);
  68. }
  69. int SetWidthHeight (ustring text, bool is_default)
  70. {
  71. int w = text.Length + 4 + (is_default ? 2 : 0);
  72. Width = w;
  73. Height = 1;
  74. return w;
  75. }
  76. /// <summary>
  77. /// Public constructor, creates a button based on
  78. /// the given text at the given position.
  79. /// </summary>
  80. /// <remarks>
  81. /// The size of the button is computed based on the
  82. /// text length. This button is not a default button.
  83. /// </remarks>
  84. /// <param name="x">X position where the button will be shown.</param>
  85. /// <param name="y">Y position where the button will be shown.</param>
  86. /// <param name="text">The button's text</param>
  87. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  88. /// <summary>
  89. /// The text displayed by this widget.
  90. /// </summary>
  91. public ustring Text {
  92. get {
  93. return text;
  94. }
  95. set {
  96. if (text?.Length != value?.Length) {
  97. SetWidthHeight (value, is_default);
  98. }
  99. text = value;
  100. Update ();
  101. }
  102. }
  103. internal void Update ()
  104. {
  105. if (IsDefault)
  106. shown_text = "[< " + text + " >]";
  107. else
  108. shown_text = "[ " + text + " ]";
  109. hot_pos = -1;
  110. hot_key = (Rune)0;
  111. int i = 0;
  112. foreach (Rune c in shown_text) {
  113. if (Rune.IsUpper (c)) {
  114. hot_key = c;
  115. hot_pos = i;
  116. break;
  117. }
  118. i++;
  119. }
  120. SetNeedsDisplay ();
  121. }
  122. /// <summary>
  123. /// Public constructor, creates a button based on
  124. /// the given text at the given position.
  125. /// </summary>
  126. /// <remarks>
  127. /// If the value for is_default is true, a special
  128. /// decoration is used, and the enter key on a
  129. /// dialog would implicitly activate this button.
  130. /// </remarks>
  131. /// <param name="x">X position where the button will be shown.</param>
  132. /// <param name="y">Y position where the button will be shown.</param>
  133. /// <param name="text">The button's text</param>
  134. /// <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>
  135. public Button (int x, int y, ustring text, bool is_default)
  136. : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
  137. {
  138. CanFocus = true;
  139. this.IsDefault = is_default;
  140. Text = text;
  141. }
  142. ///<inheritdoc cref="Redraw(Rect)"/>
  143. public override void Redraw (Rect region)
  144. {
  145. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  146. Move (0, 0);
  147. Driver.AddStr (shown_text);
  148. if (hot_pos != -1) {
  149. Move (hot_pos, 0);
  150. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  151. Driver.AddRune (hot_key);
  152. }
  153. }
  154. ///<inheritdoc cref="PositionCursor"/>
  155. public override void PositionCursor ()
  156. {
  157. Move (hot_pos == -1 ? 1 : hot_pos, 0);
  158. }
  159. bool CheckKey (KeyEvent key)
  160. {
  161. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  162. this.SuperView.SetFocus (this);
  163. Clicked?.Invoke ();
  164. return true;
  165. }
  166. return false;
  167. }
  168. ///<inheritdoc cref="ProcessHotKey"/>
  169. public override bool ProcessHotKey (KeyEvent kb)
  170. {
  171. if (kb.IsAlt)
  172. return CheckKey (kb);
  173. return false;
  174. }
  175. ///<inheritdoc cref="ProcessColdKey"/>
  176. public override bool ProcessColdKey (KeyEvent kb)
  177. {
  178. if (IsDefault && kb.KeyValue == '\n') {
  179. if (Clicked != null)
  180. Clicked ();
  181. return true;
  182. }
  183. return CheckKey (kb);
  184. }
  185. ///<inheritdoc cref="ProcessKey"/>
  186. public override bool ProcessKey (KeyEvent kb)
  187. {
  188. var c = kb.KeyValue;
  189. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == hot_key) {
  190. if (Clicked != null)
  191. Clicked ();
  192. return true;
  193. }
  194. return base.ProcessKey (kb);
  195. }
  196. ///<inheritdoc cref="MouseEvent"/>
  197. public override bool MouseEvent(MouseEvent me)
  198. {
  199. if (me.Flags == MouseFlags.Button1Clicked) {
  200. SuperView.SetFocus (this);
  201. SetNeedsDisplay ();
  202. if (Clicked != null)
  203. Clicked ();
  204. return true;
  205. }
  206. return false;
  207. }
  208. }
  209. }