CheckBox.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. Rune charNullChecked;
  15. Rune charChecked;
  16. Rune charUnChecked;
  17. bool? @checked;
  18. bool allowNullChecked;
  19. /// <summary>
  20. /// Toggled event, raised when the <see cref="CheckBox"/> is toggled.
  21. /// </summary>
  22. /// <remarks>
  23. /// Client code can hook up to this event, it is
  24. /// raised when the <see cref="CheckBox"/> is activated either with
  25. /// the mouse or the keyboard. The passed <c>bool</c> contains the previous state.
  26. /// </remarks>
  27. public event Action<bool?> Toggled;
  28. /// <summary>
  29. /// Called when the <see cref="Checked"/> property changes. Invokes the <see cref="Toggled"/> event.
  30. /// </summary>
  31. public virtual void OnToggled (bool? previousChecked)
  32. {
  33. Toggled?.Invoke (previousChecked);
  34. }
  35. /// <summary>
  36. /// Initializes a new instance of <see cref="CheckBox"/> based on the given text, using <see cref="LayoutStyle.Computed"/> layout.
  37. /// </summary>
  38. public CheckBox () : this (string.Empty) { }
  39. /// <summary>
  40. /// Initializes a new instance of <see cref="CheckBox"/> based on the given text, using <see cref="LayoutStyle.Computed"/> layout.
  41. /// </summary>
  42. /// <param name="s">S.</param>
  43. /// <param name="is_checked">If set to <c>true</c> is checked.</param>
  44. public CheckBox (ustring s, bool is_checked = false) : base ()
  45. {
  46. Initialize (s, is_checked);
  47. }
  48. /// <summary>
  49. /// Initializes a new instance of <see cref="CheckBox"/> using <see cref="LayoutStyle.Absolute"/> layout.
  50. /// </summary>
  51. /// <remarks>
  52. /// The size of <see cref="CheckBox"/> is computed based on the
  53. /// text length. This <see cref="CheckBox"/> is not toggled.
  54. /// </remarks>
  55. public CheckBox (int x, int y, ustring s) : this (x, y, s, false)
  56. {
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of <see cref="CheckBox"/> using <see cref="LayoutStyle.Absolute"/> layout.
  60. /// </summary>
  61. /// <remarks>
  62. /// The size of <see cref="CheckBox"/> is computed based on the
  63. /// text length.
  64. /// </remarks>
  65. public CheckBox (int x, int y, ustring s, bool is_checked) : base (new Rect (x, y, s.Length, 1))
  66. {
  67. Initialize (s, is_checked);
  68. }
  69. void Initialize (ustring s, bool is_checked)
  70. {
  71. charNullChecked = new Rune (Driver != null ? Driver.NullChecked : '?');
  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. UpdateTextFormatterText ();
  80. ProcessResizeView ();
  81. // Things this view knows how to do
  82. AddCommand (Command.ToggleChecked, () => ToggleChecked ());
  83. // Default keybindings for this view
  84. AddKeyBinding ((Key)' ', Command.ToggleChecked);
  85. AddKeyBinding (Key.Space, Command.ToggleChecked);
  86. }
  87. /// <inheritdoc/>
  88. protected override void UpdateTextFormatterText ()
  89. {
  90. switch (TextAlignment) {
  91. case TextAlignment.Left:
  92. case TextAlignment.Centered:
  93. case TextAlignment.Justified:
  94. TextFormatter.Text = ustring.Make (GetCheckedState ()) + " " + GetFormatterText ();
  95. break;
  96. case TextAlignment.Right:
  97. TextFormatter.Text = GetFormatterText () + " " + ustring.Make (GetCheckedState ());
  98. break;
  99. }
  100. }
  101. Rune GetCheckedState ()
  102. {
  103. switch (Checked) {
  104. case true: return charChecked;
  105. case false: return charUnChecked;
  106. default: return charNullChecked;
  107. }
  108. }
  109. ustring GetFormatterText ()
  110. {
  111. if (AutoSize || ustring.IsNullOrEmpty (Text) || Frame.Width <= 2) {
  112. return Text;
  113. }
  114. return Text.RuneSubstring (0, Math.Min (Frame.Width - 2, Text.RuneCount));
  115. }
  116. /// <summary>
  117. /// The state of the <see cref="CheckBox"/>
  118. /// </summary>
  119. public bool? Checked {
  120. get => @checked;
  121. set {
  122. if (value == null && !AllowNullChecked) {
  123. return;
  124. }
  125. @checked = value;
  126. UpdateTextFormatterText ();
  127. ProcessResizeView ();
  128. }
  129. }
  130. /// <summary>
  131. /// If <see langword="true"/> allows <see cref="Checked"/> to be null, true or false.
  132. /// If <see langword="false"/> only allows <see cref="Checked"/> to be true or false.
  133. /// </summary>
  134. public bool AllowNullChecked {
  135. get => allowNullChecked;
  136. set {
  137. allowNullChecked = value;
  138. if (Checked == null) {
  139. Checked = false;
  140. }
  141. }
  142. }
  143. ///<inheritdoc/>
  144. public override void PositionCursor ()
  145. {
  146. Move (0, 0);
  147. }
  148. ///<inheritdoc/>
  149. public override bool ProcessKey (KeyEvent kb)
  150. {
  151. var result = InvokeKeybindings (kb);
  152. if (result != null)
  153. return (bool)result;
  154. return base.ProcessKey (kb);
  155. }
  156. ///<inheritdoc/>
  157. public override bool ProcessHotKey (KeyEvent kb)
  158. {
  159. if (kb.Key == (Key.AltMask | HotKey))
  160. return ToggleChecked ();
  161. return false;
  162. }
  163. bool ToggleChecked ()
  164. {
  165. if (!HasFocus) {
  166. SetFocus ();
  167. }
  168. var previousChecked = Checked;
  169. if (AllowNullChecked) {
  170. switch (previousChecked) {
  171. case null:
  172. Checked = true;
  173. break;
  174. case true:
  175. Checked = false;
  176. break;
  177. case false:
  178. Checked = null;
  179. break;
  180. }
  181. } else {
  182. Checked = !Checked;
  183. }
  184. OnToggled (previousChecked);
  185. SetNeedsDisplay ();
  186. return true;
  187. }
  188. ///<inheritdoc/>
  189. public override bool MouseEvent (MouseEvent me)
  190. {
  191. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
  192. return false;
  193. ToggleChecked ();
  194. return true;
  195. }
  196. ///<inheritdoc/>
  197. public override bool OnEnter (View view)
  198. {
  199. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  200. return base.OnEnter (view);
  201. }
  202. }
  203. }