Button.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 = GetTextFromHotKey (shown_text, '_', out hot_pos, out hot_key);
  161. SetNeedsDisplay ();
  162. }
  163. int c_hot_pos;
  164. ///<inheritdoc/>
  165. public override void Redraw (Rect bounds)
  166. {
  167. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  168. Move (0, 0);
  169. var caption = GetTextAlignment (shown_text, hot_pos, out int s_hot_pos, TextAlignment);
  170. c_hot_pos = s_hot_pos;
  171. Driver.AddStr (caption);
  172. if (c_hot_pos != -1) {
  173. Move (c_hot_pos, 0);
  174. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  175. Driver.AddRune (hot_key);
  176. }
  177. }
  178. ///<inheritdoc/>
  179. public override void PositionCursor ()
  180. {
  181. Move (c_hot_pos == -1 ? 1 : c_hot_pos, 0);
  182. }
  183. bool CheckKey (KeyEvent key)
  184. {
  185. if ((char)key.KeyValue == hot_key) {
  186. this.SuperView.SetFocus (this);
  187. Clicked?.Invoke ();
  188. return true;
  189. }
  190. return false;
  191. }
  192. ///<inheritdoc/>
  193. public override bool ProcessHotKey (KeyEvent kb)
  194. {
  195. if (kb.IsAlt)
  196. return CheckKey (kb);
  197. return false;
  198. }
  199. ///<inheritdoc/>
  200. public override bool ProcessColdKey (KeyEvent kb)
  201. {
  202. if (IsDefault && kb.KeyValue == '\n') {
  203. Clicked?.Invoke ();
  204. return true;
  205. }
  206. return CheckKey (kb);
  207. }
  208. ///<inheritdoc/>
  209. public override bool ProcessKey (KeyEvent kb)
  210. {
  211. var c = kb.KeyValue;
  212. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == hot_key) {
  213. Clicked?.Invoke ();
  214. return true;
  215. }
  216. return base.ProcessKey (kb);
  217. }
  218. ///<inheritdoc/>
  219. public override bool MouseEvent (MouseEvent me)
  220. {
  221. if (me.Flags == MouseFlags.Button1Clicked) {
  222. if (!HasFocus) {
  223. SuperView.SetFocus (this);
  224. SetNeedsDisplay ();
  225. }
  226. Clicked?.Invoke ();
  227. return true;
  228. }
  229. return false;
  230. }
  231. }
  232. }