Button.cs 5.6 KB

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