CheckBox.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // Checkbox.cs: Checkbox control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Text;
  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 EventHandler<ToggleEventArgs> 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 (ToggleEventArgs e)
  32. {
  33. Toggled?.Invoke (this, e);
  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 (string 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, string 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, string 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 (string s, bool is_checked)
  76. {
  77. charNullChecked = CM.Glyphs.NullChecked;
  78. charChecked = CM.Glyphs.Checked;
  79. charUnChecked = CM.Glyphs.UnChecked;
  80. Checked = is_checked;
  81. HotKeySpecifier = (Rune)'_';
  82. CanFocus = true;
  83. AutoSize = true;
  84. Text = s;
  85. OnResizeNeeded ();
  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 = $"{GetCheckedState ()} {GetFormatterText ()}";
  100. break;
  101. case TextAlignment.Right:
  102. TextFormatter.Text = $"{GetFormatterText ()} {GetCheckedState ()}";
  103. break;
  104. }
  105. }
  106. Rune GetCheckedState ()
  107. {
  108. return Checked switch {
  109. true => charChecked,
  110. false => charUnChecked,
  111. var _ => charNullChecked
  112. };
  113. }
  114. string GetFormatterText ()
  115. {
  116. if (AutoSize || string.IsNullOrEmpty (Text) || Frame.Width <= 2) {
  117. return Text;
  118. }
  119. return Text [..Math.Min (Frame.Width - 2, Text.GetRuneCount ())];
  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. OnResizeNeeded ();
  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. Checked ??= false;
  144. }
  145. }
  146. ///<inheritdoc/>
  147. public override void PositionCursor ()
  148. {
  149. Move (0, 0);
  150. }
  151. ///<inheritdoc/>
  152. public override bool ProcessKey (KeyEvent kb)
  153. {
  154. var result = InvokeKeybindings (kb);
  155. if (result != null)
  156. return (bool)result;
  157. return base.ProcessKey (kb);
  158. }
  159. ///<inheritdoc/>
  160. public override bool ProcessHotKey (KeyEvent kb)
  161. {
  162. if (kb.Key == (Key.AltMask | HotKey))
  163. return ToggleChecked ();
  164. return false;
  165. }
  166. bool ToggleChecked ()
  167. {
  168. if (!HasFocus) {
  169. SetFocus ();
  170. }
  171. var previousChecked = Checked;
  172. if (AllowNullChecked) {
  173. switch (previousChecked) {
  174. case null:
  175. Checked = true;
  176. break;
  177. case true:
  178. Checked = false;
  179. break;
  180. case false:
  181. Checked = null;
  182. break;
  183. }
  184. } else {
  185. Checked = !Checked;
  186. }
  187. OnToggled (new ToggleEventArgs (previousChecked, Checked));
  188. SetNeedsDisplay ();
  189. return true;
  190. }
  191. ///<inheritdoc/>
  192. public override bool MouseEvent (MouseEvent me)
  193. {
  194. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
  195. return false;
  196. ToggleChecked ();
  197. return true;
  198. }
  199. ///<inheritdoc/>
  200. public override bool OnEnter (View view)
  201. {
  202. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  203. return base.OnEnter (view);
  204. }
  205. }
  206. }