Checkbox.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. int hot_pos = -1;
  16. Rune hot_key;
  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 + 4, 1))
  64. {
  65. Initialize (s, is_checked);
  66. }
  67. void Initialize (ustring s, bool is_checked)
  68. {
  69. Checked = is_checked;
  70. Text = s;
  71. CanFocus = true;
  72. Height = 1;
  73. Width = s.RuneCount + 4;
  74. // Things this view knows how to do
  75. AddCommand (Command.ToggleChecked, () => ToggleChecked ());
  76. // Default keybindings for this view
  77. AddKeyBinding ((Key)' ', Command.ToggleChecked);
  78. AddKeyBinding (Key.Space, Command.ToggleChecked);
  79. }
  80. /// <summary>
  81. /// The state of the <see cref="CheckBox"/>
  82. /// </summary>
  83. public bool Checked { get; set; }
  84. /// <summary>
  85. /// The text displayed by this <see cref="CheckBox"/>
  86. /// </summary>
  87. public new ustring Text {
  88. get {
  89. return text;
  90. }
  91. set {
  92. text = value;
  93. int i = 0;
  94. hot_pos = -1;
  95. hot_key = (char)0;
  96. foreach (Rune c in text) {
  97. //if (Rune.IsUpper (c)) {
  98. if (c == '_') {
  99. hot_key = text [i + 1];
  100. HotKey = (Key)(char)hot_key.ToString ().ToUpper () [0];
  101. text = text.ToString ().Replace ("_", "");
  102. hot_pos = i;
  103. break;
  104. }
  105. i++;
  106. }
  107. }
  108. }
  109. ///<inheritdoc/>
  110. public override void Redraw (Rect bounds)
  111. {
  112. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ());
  113. Move (0, 0);
  114. Driver.AddRune (Checked ? Driver.Checked : Driver.UnChecked);
  115. Driver.AddRune (' ');
  116. Move (2, 0);
  117. Driver.AddStr (Text);
  118. if (hot_pos != -1) {
  119. Move (2 + hot_pos, 0);
  120. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled);
  121. Driver.AddRune (hot_key);
  122. }
  123. }
  124. ///<inheritdoc/>
  125. public override void PositionCursor ()
  126. {
  127. Move (0, 0);
  128. }
  129. ///<inheritdoc/>
  130. public override bool ProcessKey (KeyEvent kb)
  131. {
  132. var result = InvokeKeybindings (kb);
  133. if (result != null)
  134. return (bool)result;
  135. return base.ProcessKey (kb);
  136. }
  137. ///<inheritdoc/>
  138. public override bool ProcessHotKey (KeyEvent kb)
  139. {
  140. if (kb.Key == (Key.AltMask | HotKey))
  141. return ToggleChecked ();
  142. return false;
  143. }
  144. bool ToggleChecked ()
  145. {
  146. if (!HasFocus) {
  147. SetFocus ();
  148. }
  149. var previousChecked = Checked;
  150. Checked = !Checked;
  151. OnToggled (previousChecked);
  152. SetNeedsDisplay ();
  153. return true;
  154. }
  155. ///<inheritdoc/>
  156. public override bool MouseEvent (MouseEvent me)
  157. {
  158. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
  159. return false;
  160. SetFocus ();
  161. var previousChecked = Checked;
  162. Checked = !Checked;
  163. OnToggled (previousChecked);
  164. SetNeedsDisplay ();
  165. return true;
  166. }
  167. ///<inheritdoc/>
  168. public override bool OnEnter (View view)
  169. {
  170. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  171. return base.OnEnter (view);
  172. }
  173. }
  174. }