Button.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Button.cs: Button control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. namespace Terminal {
  11. /// <summary>
  12. /// Button view
  13. /// </summary>
  14. /// <remarks>
  15. /// Provides a button that can be clicked, or pressed with
  16. /// the enter key and processes hotkeys (the first uppercase
  17. /// letter in the button becomes the hotkey).
  18. /// </remarks>
  19. public class Button : View {
  20. string text;
  21. string shown_text;
  22. char hot_key;
  23. int hot_pos = -1;
  24. bool is_default;
  25. /// <summary>
  26. /// Gets or sets a value indicating whether this <see cref="T:Terminal.Button"/> is the default action to activate on return on a dialog.
  27. /// </summary>
  28. /// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
  29. public bool IsDefault {
  30. get => is_default;
  31. set {
  32. is_default = value;
  33. Update ();
  34. }
  35. }
  36. /// <summary>
  37. /// Clicked event, raised when the button is clicked.
  38. /// </summary>
  39. /// <remarks>
  40. /// Client code can hook up to this event, it is
  41. /// raised when the button is activated either with
  42. /// the mouse or the keyboard.
  43. /// </remarks>
  44. public Action Clicked;
  45. /// <summary>
  46. /// Public constructor, creates a button based on
  47. /// the given text at position 0,0
  48. /// </summary>
  49. /// <remarks>
  50. /// The size of the button is computed based on the
  51. /// text length. This button is not a default button.
  52. /// </remarks>
  53. public Button (string s) : this (0, 0, s) { }
  54. /// <summary>
  55. /// Public constructor, creates a button based on
  56. /// the given text.
  57. /// </summary>
  58. /// <remarks>
  59. /// If the value for is_default is true, a special
  60. /// decoration is used, and the enter key on a
  61. /// dialog would implicitly activate this button.
  62. /// </remarks>
  63. public Button (string s, bool is_default) : this (0, 0, s, is_default) { }
  64. /// <summary>
  65. /// Public constructor, creates a button based on
  66. /// the given text at the given position.
  67. /// </summary>
  68. /// <remarks>
  69. /// The size of the button is computed based on the
  70. /// text length. This button is not a default button.
  71. /// </remarks>
  72. public Button (int x, int y, string s) : this (x, y, s, false) { }
  73. /// <summary>
  74. /// The text displayed by this widget.
  75. /// </summary>
  76. public string Text {
  77. get {
  78. return text;
  79. }
  80. set {
  81. text = value;
  82. Update ();
  83. }
  84. }
  85. internal void Update ()
  86. {
  87. if (IsDefault)
  88. shown_text = "[< " + text + " >]";
  89. else
  90. shown_text = "[ " + text + " ]";
  91. hot_pos = -1;
  92. hot_key = (char)0;
  93. int i = 0;
  94. foreach (char c in shown_text) {
  95. if (Char.IsUpper (c)) {
  96. hot_key = c;
  97. hot_pos = i;
  98. break;
  99. }
  100. i++;
  101. }
  102. SetNeedsDisplay ();
  103. }
  104. /// <summary>
  105. /// Public constructor, creates a button based on
  106. /// the given text at the given position.
  107. /// </summary>
  108. /// <remarks>
  109. /// If the value for is_default is true, a special
  110. /// decoration is used, and the enter key on a
  111. /// dialog would implicitly activate this button.
  112. /// </remarks>
  113. public Button (int x, int y, string s, bool is_default)
  114. : base (new Rect (x, y, s.Length + 4 + (is_default ? 2 : 0), 1))
  115. {
  116. CanFocus = true;
  117. this.IsDefault = is_default;
  118. Text = s;
  119. }
  120. public override void Redraw (Rect region)
  121. {
  122. Driver.SetAttribute (HasFocus ? Colors.Base.Focus : Colors.Base.Normal);
  123. Move (0, 0);
  124. Driver.AddStr (shown_text);
  125. if (hot_pos != -1) {
  126. Move (hot_pos, 0);
  127. Driver.SetAttribute (HasFocus ? Colors.Base.HotFocus : Colors.Base.HotNormal);
  128. Driver.AddCh (hot_key);
  129. }
  130. }
  131. public override void PositionCursor ()
  132. {
  133. Move (hot_pos, 0);
  134. }
  135. bool CheckKey (KeyEvent key)
  136. {
  137. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  138. this.SuperView.SetFocus (this);
  139. if (Clicked != null)
  140. Clicked ();
  141. return true;
  142. }
  143. return false;
  144. }
  145. public override bool ProcessHotKey (KeyEvent kb)
  146. {
  147. if (kb.IsAlt)
  148. return CheckKey (kb);
  149. return false;
  150. }
  151. public override bool ProcessColdKey (KeyEvent kb)
  152. {
  153. if (IsDefault && kb.KeyValue == '\n') {
  154. if (Clicked != null)
  155. Clicked ();
  156. return true;
  157. }
  158. return CheckKey (kb);
  159. }
  160. public override bool ProcessKey (KeyEvent kb)
  161. {
  162. var c = kb.KeyValue;
  163. if (c == '\n' || c == ' ' || Char.ToUpper ((char)c) == hot_key) {
  164. if (Clicked != null)
  165. Clicked ();
  166. return true;
  167. }
  168. return base.ProcessKey (kb);
  169. }
  170. public override bool MouseEvent(MouseEvent me)
  171. {
  172. if (me.Flags == MouseFlags.Button1Clicked) {
  173. SuperView.SetFocus (this);
  174. SetNeedsDisplay ();
  175. if (Clicked != null)
  176. Clicked ();
  177. return true;
  178. }
  179. return false;
  180. }
  181. }
  182. }