Button.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. char 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. public Button (string s) : this (0, 0, s) { }
  60. /// <summary>
  61. /// Public constructor, creates a button based on
  62. /// the given text.
  63. /// </summary>
  64. /// <remarks>
  65. /// If the value for is_default is true, a special
  66. /// decoration is used, and the enter key on a
  67. /// dialog would implicitly activate this button.
  68. /// </remarks>
  69. public Button (string s, bool is_default) : this (0, 0, s, is_default) { }
  70. /// <summary>
  71. /// Public constructor, creates a button based on
  72. /// the given text at the given position.
  73. /// </summary>
  74. /// <remarks>
  75. /// The size of the button is computed based on the
  76. /// text length. This button is not a default button.
  77. /// </remarks>
  78. public Button (int x, int y, string s) : this (x, y, s, 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 = (char)0;
  99. int i = 0;
  100. foreach (char c in shown_text) {
  101. if (Char.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. public Button (int x, int y, string s, bool is_default)
  120. : base (new Rect (x, y, s.Length + 4 + (is_default ? 2 : 0), 1))
  121. {
  122. CanFocus = true;
  123. this.IsDefault = is_default;
  124. Text = s;
  125. }
  126. public override void Redraw (Rect region)
  127. {
  128. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  129. Move (0, 0);
  130. Driver.AddStr (shown_text);
  131. if (hot_pos != -1) {
  132. Move (hot_pos, 0);
  133. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  134. Driver.AddRune (hot_key);
  135. }
  136. }
  137. public override void PositionCursor ()
  138. {
  139. Move (hot_pos, 0);
  140. }
  141. bool CheckKey (KeyEvent key)
  142. {
  143. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  144. this.SuperView.SetFocus (this);
  145. if (Clicked != null)
  146. Clicked ();
  147. return true;
  148. }
  149. return false;
  150. }
  151. public override bool ProcessHotKey (KeyEvent kb)
  152. {
  153. if (kb.IsAlt)
  154. return CheckKey (kb);
  155. return false;
  156. }
  157. public override bool ProcessColdKey (KeyEvent kb)
  158. {
  159. if (IsDefault && kb.KeyValue == '\n') {
  160. if (Clicked != null)
  161. Clicked ();
  162. return true;
  163. }
  164. return CheckKey (kb);
  165. }
  166. public override bool ProcessKey (KeyEvent kb)
  167. {
  168. var c = kb.KeyValue;
  169. if (c == '\n' || c == ' ' || Char.ToUpper ((char)c) == hot_key) {
  170. if (Clicked != null)
  171. Clicked ();
  172. return true;
  173. }
  174. return base.ProcessKey (kb);
  175. }
  176. public override bool MouseEvent(MouseEvent me)
  177. {
  178. if (me.Flags == MouseFlags.Button1Clicked) {
  179. SuperView.SetFocus (this);
  180. SetNeedsDisplay ();
  181. if (Clicked != null)
  182. Clicked ();
  183. return true;
  184. }
  185. return false;
  186. }
  187. }
  188. }