Button.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. if (text?.Length != value?.Length) {
  130. SetWidthHeight (value, is_default);
  131. }
  132. text = value;
  133. Update ();
  134. }
  135. }
  136. /// <summary>
  137. /// Sets or gets the text alignment for the <see cref="Button"/>.
  138. /// </summary>
  139. public TextAlignment TextAlignment {
  140. get => textAlignment;
  141. set {
  142. textAlignment = value;
  143. SetNeedsDisplay ();
  144. }
  145. }
  146. Rune _leftBracket = new Rune ('[');
  147. Rune _rightBracket = new Rune (']');
  148. Rune _leftDefault = new Rune ('<');
  149. Rune _rightDefault = new Rune ('>');
  150. internal void Update ()
  151. {
  152. if (IsDefault)
  153. shown_text = ustring.Make (_leftBracket) + ustring.Make (_leftDefault) + " " + text + " " + ustring.Make (_rightDefault) + ustring.Make (_rightBracket);
  154. else
  155. shown_text = ustring.Make (_leftBracket) + " " + text + " " + ustring.Make (_rightBracket);
  156. hot_key = (Rune)0;
  157. hot_pos = shown_text.IndexOf ('_');
  158. if (hot_pos == -1) {
  159. // Use first upper-case char
  160. int i = 0;
  161. foreach (Rune c in shown_text) {
  162. if (Rune.IsUpper (c)) {
  163. hot_key = c;
  164. hot_pos = i;
  165. break;
  166. }
  167. i++;
  168. }
  169. } else {
  170. // Use char after '_'
  171. var start = shown_text [0, hot_pos];
  172. shown_text = start + shown_text [hot_pos + 1, shown_text.Length];
  173. hot_key = Char.ToUpper((char)shown_text [hot_pos]);
  174. }
  175. SetNeedsDisplay ();
  176. }
  177. int c_hot_pos;
  178. ///<inheritdoc/>
  179. public override void Redraw (Rect bounds)
  180. {
  181. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  182. Move (0, 0);
  183. var caption = shown_text;
  184. c_hot_pos = hot_pos;
  185. int start;
  186. if (Frame.Width > shown_text.Length + 1) {
  187. switch (TextAlignment) {
  188. case TextAlignment.Left:
  189. caption += new string (' ', Frame.Width - caption.Length);
  190. break;
  191. case TextAlignment.Right:
  192. start = Frame.Width - caption.Length;
  193. caption = $"{new string (' ', Frame.Width - caption.Length)}{caption}";
  194. if (c_hot_pos > -1) {
  195. c_hot_pos += start;
  196. }
  197. break;
  198. case TextAlignment.Centered:
  199. start = Frame.Width / 2 - caption.Length / 2;
  200. caption = $"{new string (' ', start)}{caption}{new string (' ', Frame.Width - caption.Length - start)}";
  201. if (c_hot_pos > -1) {
  202. c_hot_pos += start;
  203. }
  204. break;
  205. case TextAlignment.Justified:
  206. var words = caption.ToString ().Split (new string [] { " " }, StringSplitOptions.RemoveEmptyEntries);
  207. var wLen = GetWordsLength (words);
  208. var space = (Frame.Width - wLen) / (caption.Length - wLen);
  209. caption = "";
  210. for (int i = 0; i < words.Length; i++) {
  211. if (i == words.Length - 1) {
  212. caption += new string (' ', Frame.Width - caption.Length - 1);
  213. caption += words [i];
  214. } else {
  215. caption += words [i];
  216. }
  217. if (i < words.Length - 1) {
  218. caption += new string (' ', space);
  219. }
  220. }
  221. if (c_hot_pos > -1) {
  222. c_hot_pos += space - 1;
  223. }
  224. break;
  225. }
  226. }
  227. Driver.AddStr (caption);
  228. if (c_hot_pos != -1) {
  229. Move (c_hot_pos, 0);
  230. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  231. Driver.AddRune (hot_key);
  232. }
  233. }
  234. int GetWordsLength (string[] words)
  235. {
  236. int length = 0;
  237. for (int i = 0; i < words.Length; i++) {
  238. length += words [i].Length;
  239. }
  240. return length;
  241. }
  242. ///<inheritdoc/>
  243. public override void PositionCursor ()
  244. {
  245. Move (c_hot_pos == -1 ? 1 : c_hot_pos, 0);
  246. }
  247. bool CheckKey (KeyEvent key)
  248. {
  249. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  250. this.SuperView.SetFocus (this);
  251. Clicked?.Invoke ();
  252. return true;
  253. }
  254. return false;
  255. }
  256. ///<inheritdoc/>
  257. public override bool ProcessHotKey (KeyEvent kb)
  258. {
  259. if (kb.IsAlt)
  260. return CheckKey (kb);
  261. return false;
  262. }
  263. ///<inheritdoc/>
  264. public override bool ProcessColdKey (KeyEvent kb)
  265. {
  266. if (IsDefault && kb.KeyValue == '\n') {
  267. Clicked?.Invoke ();
  268. return true;
  269. }
  270. return CheckKey (kb);
  271. }
  272. ///<inheritdoc/>
  273. public override bool ProcessKey (KeyEvent kb)
  274. {
  275. var c = kb.KeyValue;
  276. if (c == '\n' || c == ' ' || Rune.ToUpper ((uint)c) == hot_key) {
  277. Clicked?.Invoke ();
  278. return true;
  279. }
  280. return base.ProcessKey (kb);
  281. }
  282. ///<inheritdoc/>
  283. public override bool MouseEvent (MouseEvent me)
  284. {
  285. if (me.Flags == MouseFlags.Button1Clicked) {
  286. if (!HasFocus) {
  287. SuperView.SetFocus (this);
  288. SetNeedsDisplay ();
  289. }
  290. Clicked?.Invoke ();
  291. return true;
  292. }
  293. return false;
  294. }
  295. }
  296. }