Button.cs 9.0 KB

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