Button.cs 5.7 KB

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