CheckBox.cs 4.9 KB

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