Button.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <see cref="View"/> that provides an item that invokes an <see cref="Action"/> when activated by the user.
  12. /// </summary>
  13. /// <remarks>
  14. /// <para>
  15. /// Provides a button showing text invokes an <see cref="Action"/> when clicked on with a mouse
  16. /// or when the user presses SPACE, ENTER, or hotkey. The hotkey is specified by the first uppercase
  17. /// letter in the button.
  18. /// </para>
  19. /// <para>
  20. /// When the button is configured as the default (<see cref="IsDefault"/>) and the user presses
  21. /// the ENTER key, if no other <see cref="View"/> processes the <see cref="KeyEvent"/>, the <see cref="Button"/>'s
  22. /// <see cref="Action"/> will be invoked.
  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 whether the <see cref="Button"/> is the default action to activate in 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. SetWidthHeight (Text, is_default);
  40. Update ();
  41. }
  42. }
  43. /// <summary>
  44. /// Clicked <see cref="Action"/>, 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. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  54. /// </summary>
  55. /// <remarks>
  56. /// The width of the <see cref="Button"/> is computed based on the
  57. /// text length. The height will always be 1.
  58. /// </remarks>
  59. public Button () : this (string.Empty) { }
  60. /// <summary>
  61. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  62. /// </summary>
  63. /// <remarks>
  64. /// The width of the <see cref="Button"/> is computed based on the
  65. /// text length. The height will always be 1.
  66. /// </remarks>
  67. /// <param name="text">The button's text</param>
  68. /// <param name="is_default">
  69. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  70. /// in a <see cref="Dialog"/> will implicitly activate this button.
  71. /// </param>
  72. public Button (ustring text, bool is_default = false) : base ()
  73. {
  74. CanFocus = true;
  75. Text = text ?? string.Empty;
  76. this.IsDefault = is_default;
  77. int w = SetWidthHeight (text, is_default);
  78. Frame = new Rect (Frame.Location, new Size (w, 1));
  79. }
  80. /// <summary>
  81. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text
  82. /// </summary>
  83. /// <remarks>
  84. /// The width of the <see cref="Button"/> is computed based on the
  85. /// text length. The height will always be 1.
  86. /// </remarks>
  87. /// <param name="x">X position where the button will be shown.</param>
  88. /// <param name="y">Y position where the button will be shown.</param>
  89. /// <param name="text">The button's text</param>
  90. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  91. /// <summary>
  92. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text.
  93. /// </summary>
  94. /// <remarks>
  95. /// The width of the <see cref="Button"/> is computed based on the
  96. /// text length. The height will always be 1.
  97. /// </remarks>
  98. /// <param name="x">X position where the button will be shown.</param>
  99. /// <param name="y">Y position where the button will be shown.</param>
  100. /// <param name="text">The button's text</param>
  101. /// <param name="is_default">
  102. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  103. /// in a <see cref="Dialog"/> will implicitly activate this button.
  104. /// </param>
  105. public Button (int x, int y, ustring text, bool is_default)
  106. : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
  107. {
  108. CanFocus = true;
  109. Text = text ?? string.Empty;
  110. this.IsDefault = is_default;
  111. }
  112. int SetWidthHeight (ustring text, bool is_default)
  113. {
  114. int w = text.Length + 4 + (is_default ? 2 : 0);
  115. Width = w;
  116. Height = 1;
  117. Frame = new Rect (Frame.Location, new Size (w, 1));
  118. return w;
  119. }
  120. /// <summary>
  121. /// The text displayed by this <see cref="Button"/>.
  122. /// </summary>
  123. public ustring Text {
  124. get {
  125. return text;
  126. }
  127. set {
  128. if (text?.Length != value?.Length) {
  129. SetWidthHeight (value, is_default);
  130. }
  131. text = value;
  132. Update ();
  133. }
  134. }
  135. internal void Update ()
  136. {
  137. if (IsDefault)
  138. shown_text = "[< " + text + " >]";
  139. else
  140. shown_text = "[ " + text + " ]";
  141. hot_key = (Rune)0;
  142. hot_pos = shown_text.IndexOf ('_');
  143. if (hot_pos == -1) {
  144. // Use first upper-case char
  145. int i = 0;
  146. foreach (Rune c in shown_text) {
  147. if (Rune.IsUpper (c)) {
  148. hot_key = c;
  149. hot_pos = i;
  150. break;
  151. }
  152. i++;
  153. }
  154. } else {
  155. // Use char after '_'
  156. var start = shown_text [0, hot_pos];
  157. shown_text = start + shown_text [hot_pos + 1, shown_text.Length];
  158. hot_key = Char.ToUpper((char)shown_text [hot_pos]);
  159. }
  160. SetNeedsDisplay ();
  161. }
  162. ///<inheritdoc/>
  163. public override void Redraw (Rect bounds)
  164. {
  165. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  166. Move (0, 0);
  167. Driver.AddStr (shown_text);
  168. if (hot_pos != -1) {
  169. Move (hot_pos, 0);
  170. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  171. Driver.AddRune (hot_key);
  172. }
  173. }
  174. ///<inheritdoc/>
  175. public override void PositionCursor ()
  176. {
  177. Move (hot_pos == -1 ? 1 : hot_pos, 0);
  178. }
  179. bool CheckKey (KeyEvent key)
  180. {
  181. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  182. this.SuperView.SetFocus (this);
  183. Clicked?.Invoke ();
  184. return true;
  185. }
  186. return false;
  187. }
  188. ///<inheritdoc/>
  189. public override bool ProcessHotKey (KeyEvent kb)
  190. {
  191. if (kb.IsAlt)
  192. return CheckKey (kb);
  193. return false;
  194. }
  195. ///<inheritdoc/>
  196. public override bool ProcessColdKey (KeyEvent kb)
  197. {
  198. if (IsDefault && kb.KeyValue == '\n') {
  199. if (Clicked != null)
  200. Clicked ();
  201. return true;
  202. }
  203. return CheckKey (kb);
  204. }
  205. ///<inheritdoc/>
  206. public override bool ProcessKey (KeyEvent kb)
  207. {
  208. var c = kb.KeyValue;
  209. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == hot_key) {
  210. if (Clicked != null)
  211. Clicked ();
  212. return true;
  213. }
  214. return base.ProcessKey (kb);
  215. }
  216. ///<inheritdoc/>
  217. public override bool MouseEvent (MouseEvent me)
  218. {
  219. if (me.Flags == MouseFlags.Button1Clicked) {
  220. SuperView.SetFocus (this);
  221. SetNeedsDisplay ();
  222. if (Clicked != null)
  223. Clicked ();
  224. return true;
  225. }
  226. return false;
  227. }
  228. }
  229. }