Checkbox.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 EventHandler<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. Toggled?.Invoke (this, previousChecked);
  31. }
  32. /// <summary>
  33. /// Initializes a new instance of <see cref="CheckBox"/> based on the given text, uses Computed layout and sets the height and width.
  34. /// </summary>
  35. /// <param name="s">S.</param>
  36. /// <param name="is_checked">If set to <c>true</c> is checked.</param>
  37. public CheckBox (ustring s, bool is_checked = false) : base ()
  38. {
  39. Checked = is_checked;
  40. Text = s;
  41. CanFocus = true;
  42. Height = 1;
  43. Width = s.Length + 4;
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of <see cref="CheckBox"/> based on the given text at the given position and a state.
  47. /// </summary>
  48. /// <remarks>
  49. /// The size of <see cref="CheckBox"/> is computed based on the
  50. /// text length. This <see cref="CheckBox"/> is not toggled.
  51. /// </remarks>
  52. public CheckBox (int x, int y, ustring s) : this (x, y, s, false)
  53. {
  54. }
  55. /// <summary>
  56. /// Initializes a new instance of <see cref="CheckBox"/> based on the given text at the given position and a state.
  57. /// </summary>
  58. /// <remarks>
  59. /// The size of <see cref="CheckBox"/> is computed based on the
  60. /// text length.
  61. /// </remarks>
  62. public CheckBox (int x, int y, ustring s, bool is_checked) : base (new Rect (x, y, s.Length + 4, 1))
  63. {
  64. Checked = is_checked;
  65. Text = s;
  66. CanFocus = true;
  67. }
  68. /// <summary>
  69. /// The state of the <see cref="CheckBox"/>
  70. /// </summary>
  71. public bool Checked { get; set; }
  72. /// <summary>
  73. /// The text displayed by this <see cref="CheckBox"/>
  74. /// </summary>
  75. public ustring Text {
  76. get {
  77. return text;
  78. }
  79. set {
  80. text = value;
  81. int i = 0;
  82. hot_pos = -1;
  83. hot_key = (char)0;
  84. foreach (Rune c in text) {
  85. if (Rune.IsUpper (c)) {
  86. hot_key = c;
  87. hot_pos = i;
  88. break;
  89. }
  90. i++;
  91. }
  92. }
  93. }
  94. ///<inheritdoc/>
  95. public override void Redraw (Rect bounds)
  96. {
  97. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  98. Move (0, 0);
  99. Driver.AddStr (Checked ? "[x] " : "[ ] ");
  100. Move (4, 0);
  101. Driver.AddStr (Text);
  102. if (hot_pos != -1) {
  103. Move (4 + hot_pos, 0);
  104. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  105. Driver.AddRune (hot_key);
  106. }
  107. }
  108. ///<inheritdoc/>
  109. public override void PositionCursor ()
  110. {
  111. Move (1, 0);
  112. }
  113. ///<inheritdoc/>
  114. public override bool ProcessKey (KeyEvent kb)
  115. {
  116. if (kb.KeyValue == ' ') {
  117. var previousChecked = Checked;
  118. Checked = !Checked;
  119. OnToggled (previousChecked);
  120. SetNeedsDisplay ();
  121. return true;
  122. }
  123. return base.ProcessKey (kb);
  124. }
  125. ///<inheritdoc/>
  126. public override bool MouseEvent (MouseEvent me)
  127. {
  128. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked))
  129. return false;
  130. SuperView.SetFocus (this);
  131. var previousChecked = Checked;
  132. Checked = !Checked;
  133. OnToggled (previousChecked);
  134. SetNeedsDisplay ();
  135. return true;
  136. }
  137. }
  138. }