Button.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. public Button (ustring text) : this (0, 0, text) { }
  61. /// <summary>
  62. /// Public constructor, creates a button based on
  63. /// the given text.
  64. /// </summary>
  65. /// <remarks>
  66. /// If the value for is_default is true, a special
  67. /// decoration is used, and the enter key on a
  68. /// dialog would implicitly activate this button.
  69. /// </remarks>
  70. /// <param name="text">The button's text</param>
  71. /// <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>
  72. public Button (ustring text, bool is_default) : this (0, 0, text, is_default) { }
  73. /// <summary>
  74. /// Public constructor, creates a button based on
  75. /// the given text at the given position.
  76. /// </summary>
  77. /// <remarks>
  78. /// The size of the button is computed based on the
  79. /// text length. This button is not a default button.
  80. /// </remarks>
  81. /// <param name="x">X position where the button will be shown.</param>
  82. /// <param name="y">Y position where the button will be shown.</param>
  83. /// <param name="text">The button's text</param>
  84. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  85. /// <summary>
  86. /// The text displayed by this widget.
  87. /// </summary>
  88. public ustring Text {
  89. get {
  90. return text;
  91. }
  92. set {
  93. text = value;
  94. Update ();
  95. }
  96. }
  97. internal void Update ()
  98. {
  99. if (IsDefault)
  100. shown_text = "[< " + text + " >]";
  101. else
  102. shown_text = "[ " + text + " ]";
  103. hot_pos = -1;
  104. hot_key = (Rune)0;
  105. int i = 0;
  106. foreach (Rune c in shown_text) {
  107. if (Rune.IsUpper (c)) {
  108. hot_key = c;
  109. hot_pos = i;
  110. break;
  111. }
  112. i++;
  113. }
  114. SetNeedsDisplay ();
  115. }
  116. /// <summary>
  117. /// Public constructor, creates a button based on
  118. /// the given text at the given position.
  119. /// </summary>
  120. /// <remarks>
  121. /// If the value for is_default is true, a special
  122. /// decoration is used, and the enter key on a
  123. /// dialog would implicitly activate this button.
  124. /// </remarks>
  125. /// <param name="x">X position where the button will be shown.</param>
  126. /// <param name="y">Y position where the button will be shown.</param>
  127. /// <param name="text">The button's text</param>
  128. /// <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>
  129. public Button (int x, int y, ustring text, bool is_default)
  130. : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
  131. {
  132. CanFocus = true;
  133. this.IsDefault = is_default;
  134. Text = text;
  135. }
  136. public override void Redraw (Rect region)
  137. {
  138. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  139. Move (0, 0);
  140. Driver.AddStr (shown_text);
  141. if (hot_pos != -1) {
  142. Move (hot_pos, 0);
  143. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  144. Driver.AddRune (hot_key);
  145. }
  146. }
  147. public override void PositionCursor ()
  148. {
  149. Move (hot_pos, 0);
  150. }
  151. bool CheckKey (KeyEvent key)
  152. {
  153. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  154. this.SuperView.SetFocus (this);
  155. if (Clicked != null)
  156. Clicked ();
  157. return true;
  158. }
  159. return false;
  160. }
  161. public override bool ProcessHotKey (KeyEvent kb)
  162. {
  163. if (kb.IsAlt)
  164. return CheckKey (kb);
  165. return false;
  166. }
  167. public override bool ProcessColdKey (KeyEvent kb)
  168. {
  169. if (IsDefault && kb.KeyValue == '\n') {
  170. if (Clicked != null)
  171. Clicked ();
  172. return true;
  173. }
  174. return CheckKey (kb);
  175. }
  176. public override bool ProcessKey (KeyEvent kb)
  177. {
  178. var c = kb.KeyValue;
  179. if (c == '\n' || c == ' ' || Rune.ToUpper ((Rune)c) == hot_key) {
  180. if (Clicked != null)
  181. Clicked ();
  182. return true;
  183. }
  184. return base.ProcessKey (kb);
  185. }
  186. public override bool MouseEvent(MouseEvent me)
  187. {
  188. if (me.Flags == MouseFlags.Button1Clicked) {
  189. SuperView.SetFocus (this);
  190. SetNeedsDisplay ();
  191. if (Clicked != null)
  192. Clicked ();
  193. return true;
  194. }
  195. return false;
  196. }
  197. }
  198. }