Button.cs 7.2 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. 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 (string.Empty) { }
  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. CanFocus = true;
  76. Text = text ?? string.Empty;
  77. this.IsDefault = is_default;
  78. int w = SetWidthHeight (text, is_default);
  79. Frame = new Rect (Frame.Location, new Size (w, 1));
  80. }
  81. /// <summary>
  82. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text
  83. /// </summary>
  84. /// <remarks>
  85. /// The width of the <see cref="Button"/> is computed based on the
  86. /// text length. The height will always be 1.
  87. /// </remarks>
  88. /// <param name="x">X position where the button will be shown.</param>
  89. /// <param name="y">Y position where the button will be shown.</param>
  90. /// <param name="text">The button's text</param>
  91. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  92. /// <summary>
  93. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text.
  94. /// </summary>
  95. /// <remarks>
  96. /// The width of the <see cref="Button"/> is computed based on the
  97. /// text length. The height will always be 1.
  98. /// </remarks>
  99. /// <param name="x">X position where the button will be shown.</param>
  100. /// <param name="y">Y position where the button will be shown.</param>
  101. /// <param name="text">The button's text</param>
  102. /// <param name="is_default">
  103. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  104. /// in a <see cref="Dialog"/> will implicitly activate this button.
  105. /// </param>
  106. public Button (int x, int y, ustring text, bool is_default)
  107. : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
  108. {
  109. CanFocus = true;
  110. Text = text ?? string.Empty;
  111. this.IsDefault = is_default;
  112. }
  113. int SetWidthHeight (ustring text, bool is_default)
  114. {
  115. int w = text.Length + 4 + (is_default ? 2 : 0);
  116. Width = w;
  117. Height = 1;
  118. Frame = new Rect (Frame.Location, new Size (w, 1));
  119. return w;
  120. }
  121. /// <summary>
  122. /// The text displayed by this <see cref="Button"/>.
  123. /// </summary>
  124. public ustring Text {
  125. get {
  126. return text;
  127. }
  128. set {
  129. SetWidthHeight (value, is_default);
  130. text = value;
  131. Update ();
  132. }
  133. }
  134. /// <summary>
  135. /// Sets or gets the text alignment for the <see cref="Button"/>.
  136. /// </summary>
  137. public TextAlignment TextAlignment {
  138. get => textAlignment;
  139. set {
  140. textAlignment = value;
  141. Update ();
  142. }
  143. }
  144. Rune _leftBracket = new Rune (Driver.LeftBracket);
  145. Rune _rightBracket = new Rune (Driver.RightBracket);
  146. Rune _leftDefault = new Rune (Driver.LeftDefaultIndicator);
  147. Rune _rightDefault = new Rune (Driver.RightDefaultIndicator);
  148. internal void Update ()
  149. {
  150. if (IsDefault)
  151. shown_text = ustring.Make (_leftBracket) + ustring.Make (_leftDefault) + " " + text + " " + ustring.Make (_rightDefault) + ustring.Make (_rightBracket);
  152. else
  153. shown_text = ustring.Make (_leftBracket) + " " + text + " " + ustring.Make (_rightBracket);
  154. shown_text = GetTextFromHotKey (shown_text, '_', out hot_pos, out hot_key);
  155. SetNeedsDisplay ();
  156. }
  157. int c_hot_pos;
  158. ///<inheritdoc/>
  159. public override void Redraw (Rect bounds)
  160. {
  161. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  162. Move (0, 0);
  163. var caption = GetTextAlignment (shown_text, hot_pos, out int s_hot_pos, TextAlignment);
  164. c_hot_pos = s_hot_pos;
  165. Driver.AddStr (caption);
  166. if (c_hot_pos != -1) {
  167. Move (c_hot_pos, 0);
  168. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  169. Driver.AddRune (hot_key);
  170. }
  171. }
  172. ///<inheritdoc/>
  173. public override void PositionCursor ()
  174. {
  175. Move (c_hot_pos == -1 ? 1 : c_hot_pos, 0);
  176. }
  177. bool CheckKey (KeyEvent key)
  178. {
  179. if ((char)key.KeyValue == hot_key) {
  180. this.SuperView.SetFocus (this);
  181. Clicked?.Invoke ();
  182. return true;
  183. }
  184. return false;
  185. }
  186. ///<inheritdoc/>
  187. public override bool ProcessHotKey (KeyEvent kb)
  188. {
  189. if (kb.IsAlt)
  190. return CheckKey (kb);
  191. return false;
  192. }
  193. ///<inheritdoc/>
  194. public override bool ProcessColdKey (KeyEvent kb)
  195. {
  196. if (IsDefault && kb.KeyValue == '\n') {
  197. Clicked?.Invoke ();
  198. return true;
  199. }
  200. return CheckKey (kb);
  201. }
  202. ///<inheritdoc/>
  203. public override bool ProcessKey (KeyEvent kb)
  204. {
  205. var c = kb.KeyValue;
  206. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == hot_key) {
  207. Clicked?.Invoke ();
  208. return true;
  209. }
  210. return base.ProcessKey (kb);
  211. }
  212. ///<inheritdoc/>
  213. public override bool MouseEvent (MouseEvent me)
  214. {
  215. if (me.Flags == MouseFlags.Button1Clicked) {
  216. if (!HasFocus) {
  217. SuperView.SetFocus (this);
  218. SetNeedsDisplay ();
  219. }
  220. Clicked?.Invoke ();
  221. return true;
  222. }
  223. return false;
  224. }
  225. }
  226. }