Button.cs 8.6 KB

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