Button.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // Button.cs: Button control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. namespace Terminal {
  11. /// <summary>
  12. /// Button is a view that provides an item that invokes a callback when activated.
  13. /// </summary>
  14. /// <remarks>
  15. /// <para>
  16. /// Provides a button that can be clicked, or pressed with
  17. /// the enter key and processes hotkeys (the first uppercase
  18. /// letter in the button becomes the hotkey).
  19. /// </para>
  20. /// <para>
  21. /// If the button is configured as the default (IsDefault) the button
  22. /// will respond to the return key is no other view processes it, and
  23. /// turns this into a clicked event.
  24. /// </para>
  25. /// </remarks>
  26. public class Button : View {
  27. string text;
  28. string shown_text;
  29. char hot_key;
  30. int hot_pos = -1;
  31. bool is_default;
  32. /// <summary>
  33. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Button"/> is the default action to activate on return on a dialog.
  34. /// </summary>
  35. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  36. public bool IsDefault {
  37. get => is_default;
  38. set {
  39. is_default = value;
  40. Update ();
  41. }
  42. }
  43. /// <summary>
  44. /// Clicked event, raised when the button is clicked.
  45. /// </summary>
  46. /// <remarks>
  47. /// Client code can hook up to this event, it is
  48. /// raised when the button is activated either with
  49. /// the mouse or the keyboard.
  50. /// </remarks>
  51. public Action Clicked;
  52. /// <summary>
  53. /// Public constructor, creates a button based on
  54. /// the given text at position 0,0
  55. /// </summary>
  56. /// <remarks>
  57. /// The size of the button is computed based on the
  58. /// text length. This button is not a default button.
  59. /// </remarks>
  60. public Button (string s) : this (0, 0, s) { }
  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. public Button (string s, bool is_default) : this (0, 0, s, is_default) { }
  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. public Button (int x, int y, string s) : this (x, y, s, false) { }
  80. /// <summary>
  81. /// The text displayed by this widget.
  82. /// </summary>
  83. public string Text {
  84. get {
  85. return text;
  86. }
  87. set {
  88. text = value;
  89. Update ();
  90. }
  91. }
  92. internal void Update ()
  93. {
  94. if (IsDefault)
  95. shown_text = "[< " + text + " >]";
  96. else
  97. shown_text = "[ " + text + " ]";
  98. hot_pos = -1;
  99. hot_key = (char)0;
  100. int i = 0;
  101. foreach (char c in shown_text) {
  102. if (Char.IsUpper (c)) {
  103. hot_key = c;
  104. hot_pos = i;
  105. break;
  106. }
  107. i++;
  108. }
  109. SetNeedsDisplay ();
  110. }
  111. /// <summary>
  112. /// Public constructor, creates a button based on
  113. /// the given text at the given position.
  114. /// </summary>
  115. /// <remarks>
  116. /// If the value for is_default is true, a special
  117. /// decoration is used, and the enter key on a
  118. /// dialog would implicitly activate this button.
  119. /// </remarks>
  120. public Button (int x, int y, string s, bool is_default)
  121. : base (new Rect (x, y, s.Length + 4 + (is_default ? 2 : 0), 1))
  122. {
  123. CanFocus = true;
  124. this.IsDefault = is_default;
  125. Text = s;
  126. }
  127. public override void Redraw (Rect region)
  128. {
  129. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  130. Move (0, 0);
  131. Driver.AddStr (shown_text);
  132. if (hot_pos != -1) {
  133. Move (hot_pos, 0);
  134. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  135. Driver.AddCh (hot_key);
  136. }
  137. }
  138. public override void PositionCursor ()
  139. {
  140. Move (hot_pos, 0);
  141. }
  142. bool CheckKey (KeyEvent key)
  143. {
  144. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  145. this.SuperView.SetFocus (this);
  146. if (Clicked != null)
  147. Clicked ();
  148. return true;
  149. }
  150. return false;
  151. }
  152. public override bool ProcessHotKey (KeyEvent kb)
  153. {
  154. if (kb.IsAlt)
  155. return CheckKey (kb);
  156. return false;
  157. }
  158. public override bool ProcessColdKey (KeyEvent kb)
  159. {
  160. if (IsDefault && kb.KeyValue == '\n') {
  161. if (Clicked != null)
  162. Clicked ();
  163. return true;
  164. }
  165. return CheckKey (kb);
  166. }
  167. public override bool ProcessKey (KeyEvent kb)
  168. {
  169. var c = kb.KeyValue;
  170. if (c == '\n' || c == ' ' || Char.ToUpper ((char)c) == hot_key) {
  171. if (Clicked != null)
  172. Clicked ();
  173. return true;
  174. }
  175. return base.ProcessKey (kb);
  176. }
  177. public override bool MouseEvent(MouseEvent me)
  178. {
  179. if (me.Flags == MouseFlags.Button1Clicked) {
  180. SuperView.SetFocus (this);
  181. SetNeedsDisplay ();
  182. if (Clicked != null)
  183. Clicked ();
  184. return true;
  185. }
  186. return false;
  187. }
  188. }
  189. }