Button.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. /// <summary>
  32. /// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
  33. /// </summary>
  34. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  35. public bool IsDefault {
  36. get => is_default;
  37. set {
  38. is_default = value;
  39. SetWidthHeight (Text, is_default);
  40. Update ();
  41. }
  42. }
  43. /// <summary>
  44. /// Clicked <see cref="Action"/>, raised when the button is clicked.
  45. /// </summary>
  46. /// <remarks>
  47. /// Client code can hook up to this event, it is
  48. /// raised when the button is activated either with
  49. /// the mouse or the keyboard.
  50. /// </remarks>
  51. public Action Clicked;
  52. /// <summary>
  53. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Computed"/> layout.
  54. /// </summary>
  55. /// <remarks>
  56. /// The width of the <see cref="Button"/> is computed based on the
  57. /// text length. The height will always be 1.
  58. /// </remarks>
  59. /// <param name="text">The button's text</param>
  60. /// <param name="is_default">
  61. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  62. /// in a <see cref="Dialog"/> will implicitly activate this button.
  63. /// </param>
  64. public Button (ustring text, bool is_default = false) : base ()
  65. {
  66. CanFocus = true;
  67. Text = text ?? string.Empty;
  68. this.IsDefault = is_default;
  69. int w = SetWidthHeight (text, is_default);
  70. Frame = new Rect (Frame.Location, new Size (w, 1));
  71. }
  72. /// <summary>
  73. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text
  74. /// </summary>
  75. /// <remarks>
  76. /// The width of the <see cref="Button"/> is computed based on the
  77. /// text length. The height will always be 1.
  78. /// </remarks>
  79. /// <param name="x">X position where the button will be shown.</param>
  80. /// <param name="y">Y position where the button will be shown.</param>
  81. /// <param name="text">The button's text</param>
  82. public Button (int x, int y, ustring text) : this (x, y, text, false) { }
  83. /// <summary>
  84. /// Initializes a new instance of <see cref="Button"/> using <see cref="LayoutStyle.Absolute"/> layout, based on the given text.
  85. /// </summary>
  86. /// <remarks>
  87. /// The width of the <see cref="Button"/> is computed based on the
  88. /// text length. The height will always be 1.
  89. /// </remarks>
  90. /// <param name="x">X position where the button will be shown.</param>
  91. /// <param name="y">Y position where the button will be shown.</param>
  92. /// <param name="text">The button's text</param>
  93. /// <param name="is_default">
  94. /// If <c>true</c>, a special decoration is used, and the user pressing the enter key
  95. /// in a <see cref="Dialog"/> will implicitly activate this button.
  96. /// </param>
  97. public Button (int x, int y, ustring text, bool is_default)
  98. : base (new Rect (x, y, text.Length + 4 + (is_default ? 2 : 0), 1))
  99. {
  100. CanFocus = true;
  101. Text = text ?? string.Empty;
  102. this.IsDefault = is_default;
  103. }
  104. int SetWidthHeight (ustring text, bool is_default)
  105. {
  106. int w = text.Length + 4 + (is_default ? 2 : 0);
  107. Width = w;
  108. Height = 1;
  109. Frame = new Rect (Frame.Location, new Size (w, 1));
  110. return w;
  111. }
  112. /// <summary>
  113. /// The text displayed by this <see cref="Button"/>.
  114. /// </summary>
  115. public ustring Text {
  116. get {
  117. return text;
  118. }
  119. set {
  120. if (text?.Length != value?.Length) {
  121. SetWidthHeight (value, is_default);
  122. }
  123. text = value;
  124. Update ();
  125. }
  126. }
  127. internal void Update ()
  128. {
  129. if (IsDefault)
  130. shown_text = "[< " + text + " >]";
  131. else
  132. shown_text = "[ " + text + " ]";
  133. hot_key = (Rune)0;
  134. hot_pos = shown_text.IndexOf ('_');
  135. if (hot_pos == -1) {
  136. // Use first upper-case char
  137. int i = 0;
  138. foreach (Rune c in shown_text) {
  139. if (Rune.IsUpper (c)) {
  140. hot_key = c;
  141. hot_pos = i;
  142. break;
  143. }
  144. i++;
  145. }
  146. } else {
  147. // Use char after '_'
  148. var start = shown_text [0, hot_pos];
  149. shown_text = start + shown_text [hot_pos + 1, shown_text.Length];
  150. hot_key = Char.ToUpper((char)shown_text [hot_pos]);
  151. }
  152. SetNeedsDisplay ();
  153. }
  154. ///<inheritdoc/>
  155. public override void Redraw (Rect bounds)
  156. {
  157. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  158. Move (0, 0);
  159. Driver.AddStr (shown_text);
  160. if (hot_pos != -1) {
  161. Move (hot_pos, 0);
  162. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  163. Driver.AddRune (hot_key);
  164. }
  165. }
  166. ///<inheritdoc/>
  167. public override void PositionCursor ()
  168. {
  169. Move (hot_pos == -1 ? 1 : hot_pos, 0);
  170. }
  171. bool CheckKey (KeyEvent key)
  172. {
  173. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  174. this.SuperView.SetFocus (this);
  175. Clicked?.Invoke ();
  176. return true;
  177. }
  178. return false;
  179. }
  180. ///<inheritdoc/>
  181. public override bool ProcessHotKey (KeyEvent kb)
  182. {
  183. if (kb.IsAlt)
  184. return CheckKey (kb);
  185. return false;
  186. }
  187. ///<inheritdoc/>
  188. public override bool ProcessColdKey (KeyEvent kb)
  189. {
  190. if (IsDefault && kb.KeyValue == '\n') {
  191. if (Clicked != null)
  192. Clicked ();
  193. return true;
  194. }
  195. return CheckKey (kb);
  196. }
  197. ///<inheritdoc/>
  198. public override bool ProcessKey (KeyEvent kb)
  199. {
  200. var c = kb.KeyValue;
  201. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == hot_key) {
  202. if (Clicked != null)
  203. Clicked ();
  204. return true;
  205. }
  206. return base.ProcessKey (kb);
  207. }
  208. ///<inheritdoc/>
  209. public override bool MouseEvent (MouseEvent me)
  210. {
  211. if (me.Flags == MouseFlags.Button1Clicked) {
  212. SuperView.SetFocus (this);
  213. SetNeedsDisplay ();
  214. if (Clicked != null)
  215. Clicked ();
  216. return true;
  217. }
  218. return false;
  219. }
  220. }
  221. }