Button.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 NStack;
  10. namespace Terminal.Gui {
  11. /// <summary>
  12. /// Button is a <see cref="View"/> that provides an item that invokes an <see cref="Action"/> when activated by the user.
  13. /// </summary>
  14. /// <remarks>
  15. /// <para>
  16. /// Provides a button showing text invokes an <see cref="Action"/> when clicked on with a mouse
  17. /// or when the user presses SPACE, ENTER, or hotkey. The hotkey is specified by the first uppercase
  18. /// letter in the button.
  19. /// </para>
  20. /// <para>
  21. /// When the button is configured as the default (<see cref="IsDefault"/>) and the user presses
  22. /// the ENTER key, if no other <see cref="View"/> processes the <see cref="KeyEvent"/>, the <see cref="Button"/>'s
  23. /// <see cref="Action"/> will be invoked.
  24. /// </para>
  25. /// </remarks>
  26. public class Button : View {
  27. ustring text;
  28. ustring shown_text;
  29. Rune hot_key;
  30. int hot_pos = -1;
  31. bool is_default;
  32. TextAlignment textAlignment = TextAlignment.Centered;
  33. /// <summary>
  34. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
  35. /// </summary>
  36. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  37. public bool IsDefault {
  38. get => is_default;
  39. set {
  40. is_default = value;
  41. SetWidthHeight (Text, is_default);
  42. Update ();
  43. }
  44. }
  45. /// <summary>
  46. /// Clicked <see cref="Action"/>, raised when the button is clicked.
  47. /// </summary>
  48. /// <remarks>
  49. /// Client code can hook up to this event, it is
  50. /// raised when the button is activated either with
  51. /// the mouse or the keyboard.
  52. /// </remarks>
  53. public Action Clicked;
  54. /// <summary>
  55. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  56. /// </summary>
  57. /// <remarks>
  58. /// The width of the <see cref="Button"/> is computed based on the
  59. /// text length. The height will always be 1.
  60. /// </remarks>
  61. public Button () : this (string.Empty) { }
  62. /// <summary>
  63. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  64. /// </summary>
  65. /// <remarks>
  66. /// The width of the <see cref="Button"/> is computed based on the
  67. /// text length. The height will always be 1.
  68. /// </remarks>
  69. /// <param name="text">The button's text</param>
  70. /// <param name="is_default">
  71. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  72. /// in a <see cref="Dialog"/> will implicitly activate this button.
  73. /// </param>
  74. public Button (ustring text, bool is_default = false) : base ()
  75. {
  76. CanFocus = true;
  77. Text = text ?? string.Empty;
  78. this.IsDefault = is_default;
  79. int w = SetWidthHeight (text, is_default);
  80. Frame = new Rect (Frame.Location, new Size (w, 1));
  81. }
  82. /// <summary>
  83. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text
  84. /// </summary>
  85. /// <remarks>
  86. /// The width of the <see cref="Button"/> is computed based on the
  87. /// text length. The height will always be 1.
  88. /// </remarks>
  89. /// <param name="x">X position where the button will be shown.</param>
  90. /// <param name="y">Y position where the button will be shown.</param>
  91. /// <param name="text">The button's text</param>
  92. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  93. /// <summary>
  94. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text.
  95. /// </summary>
  96. /// <remarks>
  97. /// The width of the <see cref="Button"/> is computed based on the
  98. /// text length. The height will always be 1.
  99. /// </remarks>
  100. /// <param name="x">X position where the button will be shown.</param>
  101. /// <param name="y">Y position where the button will be shown.</param>
  102. /// <param name="text">The button's text</param>
  103. /// <param name="is_default">
  104. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  105. /// in a <see cref="Dialog"/> will implicitly activate this button.
  106. /// </param>
  107. public Button (int x, int y, ustring text, bool is_default)
  108. : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
  109. {
  110. CanFocus = true;
  111. Text = text ?? string.Empty;
  112. this.IsDefault = is_default;
  113. }
  114. int SetWidthHeight (ustring text, bool is_default)
  115. {
  116. int w = text.Length + 4 + (is_default ? 2 : 0);
  117. Width = w;
  118. Height = 1;
  119. Frame = new Rect (Frame.Location, new Size (w, 1));
  120. return w;
  121. }
  122. /// <summary>
  123. /// The text displayed by this <see cref="Button"/>.
  124. /// </summary>
  125. public ustring Text {
  126. get {
  127. return text;
  128. }
  129. set {
  130. SetWidthHeight (value, is_default);
  131. text = value;
  132. Update ();
  133. }
  134. }
  135. /// <summary>
  136. /// Sets or gets the text alignment for the <see cref="Button"/>.
  137. /// </summary>
  138. public TextAlignment TextAlignment {
  139. get => textAlignment;
  140. set {
  141. textAlignment = value;
  142. Update ();
  143. }
  144. }
  145. Rune _leftBracket = new Rune (Driver.LeftBracket);
  146. Rune _rightBracket = new Rune (Driver.RightBracket);
  147. Rune _leftDefault = new Rune (Driver.LeftDefaultIndicator);
  148. Rune _rightDefault = new Rune (Driver.RightDefaultIndicator);
  149. internal void Update ()
  150. {
  151. if (IsDefault)
  152. shown_text = ustring.Make (_leftBracket) + ustring.Make (_leftDefault) + " " + text + " " + ustring.Make (_rightDefault) + ustring.Make (_rightBracket);
  153. else
  154. shown_text = ustring.Make (_leftBracket) + " " + text + " " + ustring.Make (_rightBracket);
  155. ustring ReplaceNonPrintables (ustring str)
  156. {
  157. var runes = new List<Rune> ();
  158. foreach (var r in str.ToRunes ()) {
  159. if (r < 0x20) {
  160. runes.Add (new Rune (r + 0x2400)); // U+25A1 □ WHITE SQUARE
  161. } else {
  162. runes.Add (r);
  163. }
  164. }
  165. return ustring.Make (runes); ;
  166. }
  167. shown_text = ReplaceNonPrintables (text);
  168. shown_text = GetTextFromHotKey (shown_text, '_', out hot_pos, out hot_key);
  169. SetNeedsDisplay ();
  170. }
  171. int c_hot_pos;
  172. ///<inheritdoc/>
  173. public override void Redraw (Rect bounds)
  174. {
  175. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  176. Move (0, 0);
  177. var caption = GetTextAlignment (shown_text, hot_pos, out int s_hot_pos, TextAlignment);
  178. c_hot_pos = s_hot_pos;
  179. Driver.AddStr (caption);
  180. if (c_hot_pos != -1) {
  181. Move (c_hot_pos, 0);
  182. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  183. Driver.AddRune (hot_key);
  184. }
  185. }
  186. ///<inheritdoc/>
  187. public override void PositionCursor ()
  188. {
  189. Move (c_hot_pos == -1 ? 1 : c_hot_pos, 0);
  190. }
  191. bool CheckKey (KeyEvent key)
  192. {
  193. if ((char)key.KeyValue == hot_key) {
  194. this.SuperView.SetFocus (this);
  195. Clicked?.Invoke ();
  196. return true;
  197. }
  198. return false;
  199. }
  200. ///<inheritdoc/>
  201. public override bool ProcessHotKey (KeyEvent kb)
  202. {
  203. if (kb.IsAlt)
  204. return CheckKey (kb);
  205. return false;
  206. }
  207. ///<inheritdoc/>
  208. public override bool ProcessColdKey (KeyEvent kb)
  209. {
  210. if (IsDefault && kb.KeyValue == '\n') {
  211. Clicked?.Invoke ();
  212. return true;
  213. }
  214. return CheckKey (kb);
  215. }
  216. ///<inheritdoc/>
  217. public override bool ProcessKey (KeyEvent kb)
  218. {
  219. var c = kb.KeyValue;
  220. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == hot_key) {
  221. Clicked?.Invoke ();
  222. return true;
  223. }
  224. return base.ProcessKey (kb);
  225. }
  226. ///<inheritdoc/>
  227. public override bool MouseEvent (MouseEvent me)
  228. {
  229. if (me.Flags == MouseFlags.Button1Clicked) {
  230. if (!HasFocus) {
  231. SuperView.SetFocus (this);
  232. SetNeedsDisplay ();
  233. }
  234. Clicked?.Invoke ();
  235. return true;
  236. }
  237. return false;
  238. }
  239. }
  240. }