Checkbox.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. Checked = is_checked;
  45. Text = s;
  46. CanFocus = true;
  47. Height = 1;
  48. Width = s.RuneCount + 4;
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of <see cref="CheckBox"/> using <see cref="LayoutStyle.Absolute"/> layout.
  52. /// </summary>
  53. /// <remarks>
  54. /// The size of <see cref="CheckBox"/> is computed based on the
  55. /// text length. This <see cref="CheckBox"/> is not toggled.
  56. /// </remarks>
  57. public CheckBox (int x, int y, ustring s) : this (x, y, s, false)
  58. {
  59. }
  60. /// <summary>
  61. /// Initializes a new instance of <see cref="CheckBox"/> using <see cref="LayoutStyle.Absolute"/> layout.
  62. /// </summary>
  63. /// <remarks>
  64. /// The size of <see cref="CheckBox"/> is computed based on the
  65. /// text length.
  66. /// </remarks>
  67. public CheckBox (int x, int y, ustring s, bool is_checked) : base (new Rect (x, y, s.Length + 4, 1))
  68. {
  69. Checked = is_checked;
  70. Text = s;
  71. CanFocus = true;
  72. }
  73. /// <summary>
  74. /// The state of the <see cref="CheckBox"/>
  75. /// </summary>
  76. public bool Checked { get; set; }
  77. /// <summary>
  78. /// The text displayed by this <see cref="CheckBox"/>
  79. /// </summary>
  80. public new ustring Text {
  81. get {
  82. return text;
  83. }
  84. set {
  85. text = value;
  86. int i = 0;
  87. hot_pos = -1;
  88. hot_key = (char)0;
  89. foreach (Rune c in text) {
  90. //if (Rune.IsUpper (c)) {
  91. if (c == '_') {
  92. hot_key = text [i + 1];
  93. HotKey = (Key)(char)hot_key.ToString ().ToUpper () [0];
  94. text = text.ToString ().Replace ("_", "");
  95. hot_pos = i;
  96. break;
  97. }
  98. i++;
  99. }
  100. }
  101. }
  102. ///<inheritdoc/>
  103. public override void Redraw (Rect bounds)
  104. {
  105. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ());
  106. Move (0, 0);
  107. Driver.AddRune (Checked ? Driver.Checked : Driver.UnChecked);
  108. Driver.AddRune (' ');
  109. Move (2, 0);
  110. Driver.AddStr (Text);
  111. if (hot_pos != -1) {
  112. Move (2 + hot_pos, 0);
  113. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled);
  114. Driver.AddRune (hot_key);
  115. }
  116. }
  117. ///<inheritdoc/>
  118. public override void PositionCursor ()
  119. {
  120. Move (0, 0);
  121. }
  122. ///<inheritdoc/>
  123. public override bool ProcessKey (KeyEvent kb)
  124. {
  125. if (kb.KeyValue == ' ') {
  126. var previousChecked = Checked;
  127. Checked = !Checked;
  128. OnToggled (previousChecked);
  129. SetNeedsDisplay ();
  130. return true;
  131. }
  132. return base.ProcessKey (kb);
  133. }
  134. ///<inheritdoc/>
  135. public override bool ProcessHotKey (KeyEvent ke)
  136. {
  137. if (ke.Key == (Key.AltMask | HotKey)) {
  138. SetFocus ();
  139. var previousChecked = Checked;
  140. Checked = !Checked;
  141. OnToggled (previousChecked);
  142. SetNeedsDisplay ();
  143. return true;
  144. }
  145. return false;
  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. }