Button.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. TextAlignment textAlignment = TextAlignment.Centered;
  32. /// <summary>
  33. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in 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. SetWidthHeight (Text, is_default);
  41. Update ();
  42. }
  43. }
  44. /// <summary>
  45. /// Clicked <see cref="Action"/>, raised when the button is clicked.
  46. /// </summary>
  47. /// <remarks>
  48. /// Client code can hook up to this event, it is
  49. /// raised when the button is activated either with
  50. /// the mouse or the keyboard.
  51. /// </remarks>
  52. public Action Clicked;
  53. /// <summary>
  54. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  55. /// </summary>
  56. /// <remarks>
  57. /// The width of the <see cref="Button"/> is computed based on the
  58. /// text length. The height will always be 1.
  59. /// </remarks>
  60. public Button () : this (text: string.Empty, is_default: false) { }
  61. /// <summary>
  62. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  63. /// </summary>
  64. /// <remarks>
  65. /// The width of the <see cref="Button"/> is computed based on the
  66. /// text length. The height will always be 1.
  67. /// </remarks>
  68. /// <param name="text">The button's text</param>
  69. /// <param name="is_default">
  70. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  71. /// in a <see cref="Dialog"/> will implicitly activate this button.
  72. /// </param>
  73. public Button (ustring text, bool is_default = false) : base ()
  74. {
  75. Init (text, is_default);
  76. }
  77. /// <summary>
  78. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text
  79. /// </summary>
  80. /// <remarks>
  81. /// The width of the <see cref="Button"/> is computed based on the
  82. /// text length. The height will always be 1.
  83. /// </remarks>
  84. /// <param name="x">X position where the button will be shown.</param>
  85. /// <param name="y">Y position where the button will be shown.</param>
  86. /// <param name="text">The button's text</param>
  87. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  88. /// <summary>
  89. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text.
  90. /// </summary>
  91. /// <remarks>
  92. /// The width of the <see cref="Button"/> is computed based on the
  93. /// text length. The height will always be 1.
  94. /// </remarks>
  95. /// <param name="x">X position where the button will be shown.</param>
  96. /// <param name="y">Y position where the button will be shown.</param>
  97. /// <param name="text">The button's text</param>
  98. /// <param name="is_default">
  99. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  100. /// in a <see cref="Dialog"/> will implicitly activate this button.
  101. /// </param>
  102. public Button (int x, int y, ustring text, bool is_default)
  103. : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
  104. {
  105. Init (text, is_default);
  106. }
  107. Rune _leftBracket;
  108. Rune _rightBracket;
  109. Rune _leftDefault;
  110. Rune _rightDefault;
  111. void Init (ustring text, bool is_default)
  112. {
  113. _leftBracket = new Rune (Driver != null ? Driver.LeftBracket : '[');
  114. _rightBracket = new Rune (Driver != null ? Driver.RightBracket : ']');
  115. _leftDefault = new Rune (Driver != null ? Driver.LeftDefaultIndicator : '<');
  116. _rightDefault = new Rune (Driver != null ? Driver.RightDefaultIndicator : '>');
  117. CanFocus = true;
  118. Text = text ?? string.Empty;
  119. this.IsDefault = is_default;
  120. int w = SetWidthHeight (text, is_default);
  121. Frame = new Rect (Frame.Location, new Size (w, 1));
  122. }
  123. int SetWidthHeight (ustring text, bool is_default)
  124. {
  125. int w = text.Length + 4 + (is_default ? 2 : 0);
  126. Width = w;
  127. Height = 1;
  128. Frame = new Rect (Frame.Location, new Size (w, 1));
  129. return w;
  130. }
  131. /// <summary>
  132. /// The text displayed by this <see cref="Button"/>.
  133. /// </summary>
  134. public ustring Text {
  135. get {
  136. return text;
  137. }
  138. set {
  139. SetWidthHeight (value, is_default);
  140. text = value;
  141. Update ();
  142. }
  143. }
  144. /// <summary>
  145. /// Sets or gets the text alignment for the <see cref="Button"/>.
  146. /// </summary>
  147. public TextAlignment TextAlignment {
  148. get => textAlignment;
  149. set {
  150. textAlignment = value;
  151. Update ();
  152. }
  153. }
  154. internal void Update ()
  155. {
  156. if (IsDefault)
  157. shown_text = ustring.Make (_leftBracket) + ustring.Make (_leftDefault) + " " + text + " " + ustring.Make (_rightDefault) + ustring.Make (_rightBracket);
  158. else
  159. shown_text = ustring.Make (_leftBracket) + " " + text + " " + ustring.Make (_rightBracket);
  160. shown_text = shown_text
  161. .Replace ("\f", "\u21a1") // U+21A1 ↡ DOWNWARDS TWO HEADED ARROW
  162. .Replace ("\n", "\u240a") // U+240A (SYMBOL FOR LINE FEED, ␊)
  163. .Replace ("\r", "\u240d") // U+240D (SYMBOL FOR CARRIAGE RETURN, ␍)
  164. .Replace ("\t", "\u2409") // U+2409 ␉ SYMBOL FOR HORIZONTAL TABULATION
  165. .Replace ("\v", "\u240b") // U+240B ␋ SYMBOL FOR VERTICAL TABULATION
  166. .TrimSpace ();
  167. shown_text = GetTextFromHotKey (shown_text, '_', out hot_pos, out hot_key);
  168. SetNeedsDisplay ();
  169. }
  170. int c_hot_pos;
  171. ///<inheritdoc/>
  172. public override void Redraw (Rect bounds)
  173. {
  174. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  175. Move (0, 0);
  176. var caption = GetTextAlignment (shown_text, hot_pos, out int s_hot_pos, TextAlignment);
  177. c_hot_pos = s_hot_pos;
  178. Driver.AddStr (caption);
  179. if (c_hot_pos != -1) {
  180. Move (c_hot_pos, 0);
  181. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  182. Driver.AddRune (hot_key);
  183. }
  184. }
  185. ///<inheritdoc/>
  186. public override void PositionCursor ()
  187. {
  188. Move (c_hot_pos == -1 ? 1 : c_hot_pos, 0);
  189. }
  190. bool CheckKey (KeyEvent key)
  191. {
  192. if ((char)key.KeyValue == hot_key) {
  193. this.SuperView.SetFocus (this);
  194. Clicked?.Invoke ();
  195. return true;
  196. }
  197. return false;
  198. }
  199. ///<inheritdoc/>
  200. public override bool ProcessHotKey (KeyEvent kb)
  201. {
  202. if (kb.IsAlt)
  203. return CheckKey (kb);
  204. return false;
  205. }
  206. ///<inheritdoc/>
  207. public override bool ProcessColdKey (KeyEvent kb)
  208. {
  209. if (IsDefault && kb.KeyValue == '\n') {
  210. Clicked?.Invoke ();
  211. return true;
  212. }
  213. return CheckKey (kb);
  214. }
  215. ///<inheritdoc/>
  216. public override bool ProcessKey (KeyEvent kb)
  217. {
  218. var c = kb.KeyValue;
  219. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == hot_key) {
  220. Clicked?.Invoke ();
  221. return true;
  222. }
  223. return base.ProcessKey (kb);
  224. }
  225. ///<inheritdoc/>
  226. public override bool MouseEvent (MouseEvent me)
  227. {
  228. if (me.Flags == MouseFlags.Button1Clicked) {
  229. if (!HasFocus) {
  230. SuperView.SetFocus (this);
  231. SetNeedsDisplay ();
  232. }
  233. Clicked?.Invoke ();
  234. return true;
  235. }
  236. return false;
  237. }
  238. }
  239. }