Button.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 the first letter or digit following the first underscore ('_')
  17. /// in the button text.
  18. /// </para>
  19. /// <para>
  20. /// Use <see cref="View.HotKeySpecifier"/> to change the hotkey specifier from the default of ('_').
  21. /// </para>
  22. /// <para>
  23. /// If no hotkey specifier is found, the first uppercase letter encountered will be used as the hotkey.
  24. /// </para>
  25. /// <para>
  26. /// When the button is configured as the default (<see cref="IsDefault"/>) and the user presses
  27. /// the ENTER key, if no other <see cref="View"/> processes the <see cref="KeyEvent"/>, the <see cref="Button"/>'s
  28. /// <see cref="Action"/> will be invoked.
  29. /// </para>
  30. /// </remarks>
  31. public class Button : View {
  32. ustring text;
  33. bool is_default;
  34. /// <summary>
  35. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  36. /// </summary>
  37. /// <remarks>
  38. /// The width of the <see cref="Button"/> is computed based on the
  39. /// text length. The height will always be 1.
  40. /// </remarks>
  41. public Button () : this (text: string.Empty, is_default: false) { }
  42. /// <summary>
  43. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  44. /// </summary>
  45. /// <remarks>
  46. /// The width of the <see cref="Button"/> is computed based on the
  47. /// text length. The height will always be 1.
  48. /// </remarks>
  49. /// <param name="text">The button's text</param>
  50. /// <param name="is_default">
  51. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  52. /// in a <see cref="Dialog"/> will implicitly activate this button.
  53. /// </param>
  54. public Button (ustring text, bool is_default = false) : base ()
  55. {
  56. Init (text, is_default);
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text
  60. /// </summary>
  61. /// <remarks>
  62. /// The width of the <see cref="Button"/> is computed based on the
  63. /// text length. The height will always be 1.
  64. /// </remarks>
  65. /// <param name="x">X position where the button will be shown.</param>
  66. /// <param name="y">Y position where the button will be shown.</param>
  67. /// <param name="text">The button's text</param>
  68. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  69. /// <summary>
  70. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text.
  71. /// </summary>
  72. /// <remarks>
  73. /// The width of the <see cref="Button"/> is computed based on the
  74. /// text length. The height will always be 1.
  75. /// </remarks>
  76. /// <param name="x">X position where the button will be shown.</param>
  77. /// <param name="y">Y position where the button will be shown.</param>
  78. /// <param name="text">The button's text</param>
  79. /// <param name="is_default">
  80. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  81. /// in a <see cref="Dialog"/> will implicitly activate this button.
  82. /// </param>
  83. public Button (int x, int y, ustring text, bool is_default)
  84. : base (new Rect (x, y, text.RuneCount + 4 + (is_default ? 2 : 0), 1))
  85. {
  86. Init (text, is_default);
  87. }
  88. Rune _leftBracket;
  89. Rune _rightBracket;
  90. Rune _leftDefault;
  91. Rune _rightDefault;
  92. void Init (ustring text, bool is_default)
  93. {
  94. HotKeySpecifier = new Rune ('_');
  95. _leftBracket = new Rune (Driver != null ? Driver.LeftBracket : '[');
  96. _rightBracket = new Rune (Driver != null ? Driver.RightBracket : ']');
  97. _leftDefault = new Rune (Driver != null ? Driver.LeftDefaultIndicator : '<');
  98. _rightDefault = new Rune (Driver != null ? Driver.RightDefaultIndicator : '>');
  99. CanFocus = true;
  100. this.IsDefault = is_default;
  101. Text = text ?? string.Empty;
  102. }
  103. /// <summary>
  104. /// The text displayed by this <see cref="Button"/>.
  105. /// </summary>
  106. public new ustring Text {
  107. get {
  108. return text;
  109. }
  110. set {
  111. text = value;
  112. Update ();
  113. }
  114. }
  115. /// <summary>
  116. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
  117. /// </summary>
  118. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  119. public bool IsDefault {
  120. get => is_default;
  121. set {
  122. is_default = value;
  123. Update ();
  124. }
  125. }
  126. internal void Update ()
  127. {
  128. if (IsDefault)
  129. base.Text = ustring.Make (_leftBracket) + ustring.Make (_leftDefault) + " " + text + " " + ustring.Make (_rightDefault) + ustring.Make (_rightBracket);
  130. else
  131. base.Text = ustring.Make (_leftBracket) + " " + text + " " + ustring.Make (_rightBracket);
  132. int w = base.Text.RuneCount - (base.Text.Contains (HotKeySpecifier) ? 1 : 0);
  133. try {
  134. Width = w;
  135. } catch (Exception) {
  136. // It's a Dim.DimCombine and so can't be assigned. Let it have it's own anchor.
  137. var width = Width;
  138. LayoutStyle = LayoutStyle.Absolute;
  139. Width = w;
  140. LayoutStyle = LayoutStyle.Computed;
  141. Width = width;
  142. w = width.Anchor (w);
  143. }
  144. Height = 1;
  145. Frame = new Rect (Frame.Location, new Size (w, 1));
  146. SetNeedsDisplay ();
  147. }
  148. bool CheckKey (KeyEvent key)
  149. {
  150. if (key.Key == (Key.AltMask | HotKey)) {
  151. SetFocus ();
  152. Clicked?.Invoke ();
  153. return true;
  154. }
  155. return false;
  156. }
  157. ///<inheritdoc/>
  158. public override bool ProcessHotKey (KeyEvent kb)
  159. {
  160. if (kb.IsAlt)
  161. return CheckKey (kb);
  162. return false;
  163. }
  164. ///<inheritdoc/>
  165. public override bool ProcessColdKey (KeyEvent kb)
  166. {
  167. if (IsDefault && kb.KeyValue == '\n') {
  168. Clicked?.Invoke ();
  169. return true;
  170. }
  171. return CheckKey (kb);
  172. }
  173. ///<inheritdoc/>
  174. public override bool ProcessKey (KeyEvent kb)
  175. {
  176. var c = kb.KeyValue;
  177. if (c == '\n' || c == ' ' || kb.Key == HotKey) {
  178. Clicked?.Invoke ();
  179. return true;
  180. }
  181. return base.ProcessKey (kb);
  182. }
  183. /// <summary>
  184. /// Clicked <see cref="Action"/>, raised when the user clicks the primary mouse button within the Bounds of this <see cref="View"/>
  185. /// or if the user presses the action key while this view is focused. (TODO: IsDefault)
  186. /// </summary>
  187. /// <remarks>
  188. /// Client code can hook up to this event, it is
  189. /// raised when the button is activated either with
  190. /// the mouse or the keyboard.
  191. /// </remarks>
  192. public event Action Clicked;
  193. ///<inheritdoc/>
  194. public override bool MouseEvent (MouseEvent me)
  195. {
  196. if (me.Flags == MouseFlags.Button1Clicked || me.Flags == MouseFlags.Button1DoubleClicked ||
  197. me.Flags == MouseFlags.Button1TripleClicked) {
  198. if (CanFocus) {
  199. if (!HasFocus) {
  200. SetFocus ();
  201. SetNeedsDisplay ();
  202. }
  203. Clicked?.Invoke ();
  204. }
  205. return true;
  206. }
  207. return false;
  208. }
  209. ///<inheritdoc/>
  210. public override void PositionCursor ()
  211. {
  212. if (HotKey == Key.Unknown) {
  213. for (int i = 0; i < base.Text.RuneCount; i++) {
  214. if (base.Text [i] == text [0]) {
  215. Move (i, 0);
  216. return;
  217. }
  218. }
  219. }
  220. base.PositionCursor ();
  221. }
  222. }
  223. }