Checkbox.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // Checkbox.cs: Checkbox control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using NStack;
  9. namespace Terminal.Gui {
  10. /// <summary>
  11. /// The <see cref="CheckBox"/> <see cref="View"/> shows an on/off toggle that the user can set
  12. /// </summary>
  13. public class CheckBox : View {
  14. ustring text;
  15. Key hotKey = Key.Null;
  16. Rune hotKeySpecifier;
  17. Rune charChecked;
  18. Rune charUnChecked;
  19. bool @checked;
  20. /// <summary>
  21. /// Toggled event, raised when the <see cref="CheckBox"/> is toggled.
  22. /// </summary>
  23. /// <remarks>
  24. /// Client code can hook up to this event, it is
  25. /// raised when the <see cref="CheckBox"/> is activated either with
  26. /// the mouse or the keyboard. The passed <c>bool</c> contains the previous state.
  27. /// </remarks>
  28. public event Action<bool> Toggled;
  29. /// <summary>
  30. /// Called when the <see cref="Checked"/> property changes. Invokes the <see cref="Toggled"/> event.
  31. /// </summary>
  32. public virtual void OnToggled (bool previousChecked)
  33. {
  34. Toggled?.Invoke (previousChecked);
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of <see cref="CheckBox"/> based on the given text, using <see cref="LayoutStyle.Computed"/> layout.
  38. /// </summary>
  39. public CheckBox () : this (string.Empty) { }
  40. /// <summary>
  41. /// Initializes a new instance of <see cref="CheckBox"/> based on the given text, using <see cref="LayoutStyle.Computed"/> layout.
  42. /// </summary>
  43. /// <param name="s">S.</param>
  44. /// <param name="is_checked">If set to <c>true</c> is checked.</param>
  45. public CheckBox (ustring s, bool is_checked = false) : base ()
  46. {
  47. Initialize (s, is_checked);
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of <see cref="CheckBox"/> using <see cref="LayoutStyle.Absolute"/> layout.
  51. /// </summary>
  52. /// <remarks>
  53. /// The size of <see cref="CheckBox"/> is computed based on the
  54. /// text length. This <see cref="CheckBox"/> is not toggled.
  55. /// </remarks>
  56. public CheckBox (int x, int y, ustring s) : this (x, y, s, false)
  57. {
  58. }
  59. /// <summary>
  60. /// Initializes a new instance of <see cref="CheckBox"/> using <see cref="LayoutStyle.Absolute"/> layout.
  61. /// </summary>
  62. /// <remarks>
  63. /// The size of <see cref="CheckBox"/> is computed based on the
  64. /// text length.
  65. /// </remarks>
  66. public CheckBox (int x, int y, ustring s, bool is_checked) : base (new Rect (x, y, s.Length + 4, 1))
  67. {
  68. Initialize (s, is_checked);
  69. }
  70. void Initialize (ustring s, bool is_checked)
  71. {
  72. charChecked = new Rune (Driver != null ? Driver.Checked : '√');
  73. charUnChecked = new Rune (Driver != null ? Driver.UnChecked : '╴');
  74. Checked = is_checked;
  75. HotKeySpecifier = new Rune ('_');
  76. CanFocus = true;
  77. AutoSize = true;
  78. Text = s;
  79. Update ();
  80. // Things this view knows how to do
  81. AddCommand (Command.ToggleChecked, () => ToggleChecked ());
  82. // Default keybindings for this view
  83. AddKeyBinding ((Key)' ', Command.ToggleChecked);
  84. AddKeyBinding (Key.Space, Command.ToggleChecked);
  85. }
  86. void Update ()
  87. {
  88. switch (TextAlignment) {
  89. case TextAlignment.Left:
  90. case TextAlignment.Centered:
  91. case TextAlignment.Justified:
  92. if (Checked)
  93. TextFormatter.Text = ustring.Make (charChecked) + " " + GetFormatterText ();
  94. else
  95. TextFormatter.Text = ustring.Make (charUnChecked) + " " + GetFormatterText ();
  96. break;
  97. case TextAlignment.Right:
  98. if (Checked)
  99. TextFormatter.Text = GetFormatterText () + " " + ustring.Make (charChecked);
  100. else
  101. TextFormatter.Text = GetFormatterText () + " " + ustring.Make (charUnChecked);
  102. break;
  103. }
  104. int w = TextFormatter.Size.Width - (TextFormatter.Text.Contains (HotKeySpecifier)
  105. ? Math.Max (Rune.ColumnWidth (HotKeySpecifier), 0) : 0);
  106. GetCurrentWidth (out int cWidth);
  107. var canSetWidth = SetWidth (w, out int rWidth);
  108. if (canSetWidth && (cWidth < rWidth || AutoSize)) {
  109. Width = rWidth;
  110. w = rWidth;
  111. } else if (!canSetWidth || !AutoSize) {
  112. w = cWidth;
  113. }
  114. var layout = LayoutStyle;
  115. bool layoutChanged = false;
  116. if (!(Height is Dim.DimAbsolute)) {
  117. // The height is always equal to 1 and must be Dim.DimAbsolute.
  118. layoutChanged = true;
  119. LayoutStyle = LayoutStyle.Absolute;
  120. }
  121. Height = 1;
  122. if (layoutChanged) {
  123. LayoutStyle = layout;
  124. }
  125. Frame = new Rect (Frame.Location, new Size (w, 1));
  126. SetNeedsDisplay ();
  127. }
  128. ustring GetFormatterText ()
  129. {
  130. if (AutoSize || ustring.IsNullOrEmpty (text)) {
  131. return text;
  132. }
  133. return text.RuneSubstring (0, Math.Min (Frame.Width - 2, text.RuneCount));
  134. }
  135. /// <inheritdoc/>
  136. public override Key HotKey {
  137. get => hotKey;
  138. set {
  139. if (hotKey != value) {
  140. var v = value == Key.Unknown ? Key.Null : value;
  141. hotKey = v;
  142. }
  143. }
  144. }
  145. /// <inheritdoc/>
  146. public override Rune HotKeySpecifier {
  147. get => hotKeySpecifier;
  148. set {
  149. hotKeySpecifier = TextFormatter.HotKeySpecifier = value;
  150. }
  151. }
  152. /// <inheritdoc/>
  153. public override bool AutoSize {
  154. get => base.AutoSize;
  155. set {
  156. base.AutoSize = value;
  157. Update ();
  158. }
  159. }
  160. /// <summary>
  161. /// The state of the <see cref="CheckBox"/>
  162. /// </summary>
  163. public bool Checked {
  164. get => @checked;
  165. set {
  166. @checked = value;
  167. Update ();
  168. }
  169. }
  170. /// <summary>
  171. /// The text displayed by this <see cref="CheckBox"/>
  172. /// </summary>
  173. public new ustring Text {
  174. get {
  175. return text;
  176. }
  177. set {
  178. text = value;
  179. TextFormatter.FindHotKey (text, HotKeySpecifier, true, out _, out Key hk);
  180. if (hotKey != hk) {
  181. HotKey = hk;
  182. }
  183. Update ();
  184. }
  185. }
  186. ///<inheritdoc/>
  187. public override TextAlignment TextAlignment {
  188. get => base.TextAlignment;
  189. set {
  190. base.TextAlignment = value;
  191. Update ();
  192. }
  193. }
  194. ///<inheritdoc/>
  195. public override void PositionCursor ()
  196. {
  197. Move (0, 0);
  198. }
  199. ///<inheritdoc/>
  200. public override bool ProcessKey (KeyEvent kb)
  201. {
  202. var result = InvokeKeybindings (kb);
  203. if (result != null)
  204. return (bool)result;
  205. return base.ProcessKey (kb);
  206. }
  207. ///<inheritdoc/>
  208. public override bool ProcessHotKey (KeyEvent kb)
  209. {
  210. if (kb.Key == (Key.AltMask | HotKey))
  211. return ToggleChecked ();
  212. return false;
  213. }
  214. bool ToggleChecked ()
  215. {
  216. if (!HasFocus) {
  217. SetFocus ();
  218. }
  219. var previousChecked = Checked;
  220. Checked = !Checked;
  221. OnToggled (previousChecked);
  222. SetNeedsDisplay ();
  223. return true;
  224. }
  225. ///<inheritdoc/>
  226. public override bool MouseEvent (MouseEvent me)
  227. {
  228. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
  229. return false;
  230. SetFocus ();
  231. var previousChecked = Checked;
  232. Checked = !Checked;
  233. OnToggled (previousChecked);
  234. SetNeedsDisplay ();
  235. return true;
  236. }
  237. ///<inheritdoc/>
  238. public override bool OnEnter (View view)
  239. {
  240. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  241. return base.OnEnter (view);
  242. }
  243. }
  244. }