CheckBox.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. SetInitialProperties (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. SetInitialProperties (s, is_checked);
  68. }
  69. // TODO: v2 - Remove constructors with parameters
  70. /// <summary>
  71. /// Private helper to set the initial properties of the View that were provided via constructors.
  72. /// </summary>
  73. /// <param name="s"></param>
  74. /// <param name="is_checked"></param>
  75. void SetInitialProperties (ustring s, bool is_checked)
  76. {
  77. charNullChecked = new Rune (Driver != null ? Driver.NullChecked : '?');
  78. charChecked = new Rune (Driver != null ? Driver.Checked : '√');
  79. charUnChecked = new Rune (Driver != null ? Driver.UnChecked : '╴');
  80. Checked = is_checked;
  81. HotKeySpecifier = new Rune ('_');
  82. CanFocus = true;
  83. AutoSize = true;
  84. Text = s;
  85. ProcessResizeView ();
  86. // Things this view knows how to do
  87. AddCommand (Command.ToggleChecked, () => ToggleChecked ());
  88. // Default keybindings for this view
  89. AddKeyBinding ((Key)' ', Command.ToggleChecked);
  90. AddKeyBinding (Key.Space, Command.ToggleChecked);
  91. }
  92. /// <inheritdoc/>
  93. protected override void UpdateTextFormatterText ()
  94. {
  95. switch (TextAlignment) {
  96. case TextAlignment.Left:
  97. case TextAlignment.Centered:
  98. case TextAlignment.Justified:
  99. TextFormatter.Text = ustring.Make (GetCheckedState ()) + " " + GetFormatterText ();
  100. break;
  101. case TextAlignment.Right:
  102. TextFormatter.Text = GetFormatterText () + " " + ustring.Make (GetCheckedState ());
  103. break;
  104. }
  105. }
  106. Rune GetCheckedState ()
  107. {
  108. switch (Checked) {
  109. case true: return charChecked;
  110. case false: return charUnChecked;
  111. default: return charNullChecked;
  112. }
  113. }
  114. ustring GetFormatterText ()
  115. {
  116. if (AutoSize || ustring.IsNullOrEmpty (Text) || Frame.Width <= 2) {
  117. return Text;
  118. }
  119. return Text.RuneSubstring (0, Math.Min (Frame.Width - 2, Text.RuneCount));
  120. }
  121. /// <summary>
  122. /// The state of the <see cref="CheckBox"/>
  123. /// </summary>
  124. public bool? Checked {
  125. get => @checked;
  126. set {
  127. if (value == null && !AllowNullChecked) {
  128. return;
  129. }
  130. @checked = value;
  131. UpdateTextFormatterText ();
  132. ProcessResizeView ();
  133. }
  134. }
  135. /// <summary>
  136. /// If <see langword="true"/> allows <see cref="Checked"/> to be null, true or false.
  137. /// If <see langword="false"/> only allows <see cref="Checked"/> to be true or false.
  138. /// </summary>
  139. public bool AllowNullChecked {
  140. get => allowNullChecked;
  141. set {
  142. allowNullChecked = value;
  143. if (Checked == null) {
  144. Checked = false;
  145. }
  146. }
  147. }
  148. ///<inheritdoc/>
  149. public override void PositionCursor ()
  150. {
  151. Move (0, 0);
  152. }
  153. ///<inheritdoc/>
  154. public override bool ProcessKey (KeyEvent kb)
  155. {
  156. var result = InvokeKeybindings (kb);
  157. if (result != null)
  158. return (bool)result;
  159. return base.ProcessKey (kb);
  160. }
  161. ///<inheritdoc/>
  162. public override bool ProcessHotKey (KeyEvent kb)
  163. {
  164. if (kb.Key == (Key.AltMask | HotKey))
  165. return ToggleChecked ();
  166. return false;
  167. }
  168. bool ToggleChecked ()
  169. {
  170. if (!HasFocus) {
  171. SetFocus ();
  172. }
  173. var previousChecked = Checked;
  174. if (AllowNullChecked) {
  175. switch (previousChecked) {
  176. case null:
  177. Checked = true;
  178. break;
  179. case true:
  180. Checked = false;
  181. break;
  182. case false:
  183. Checked = null;
  184. break;
  185. }
  186. } else {
  187. Checked = !Checked;
  188. }
  189. OnToggled (previousChecked);
  190. SetNeedsDisplay ();
  191. return true;
  192. }
  193. ///<inheritdoc/>
  194. public override bool MouseEvent (MouseEvent me)
  195. {
  196. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
  197. return false;
  198. ToggleChecked ();
  199. return true;
  200. }
  201. ///<inheritdoc/>
  202. public override bool OnEnter (View view)
  203. {
  204. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  205. return base.OnEnter (view);
  206. }
  207. }
  208. }