Button.cs 5.6 KB

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