Checkbox.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 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. hot_key = c;
  92. hot_pos = i;
  93. break;
  94. }
  95. i++;
  96. }
  97. }
  98. }
  99. ///<inheritdoc/>
  100. public override void Redraw (Rect bounds)
  101. {
  102. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  103. Move (0, 0);
  104. Driver.AddStr (Checked ? "[x] " : "[ ] ");
  105. Move (4, 0);
  106. Driver.AddStr (Text);
  107. if (hot_pos != -1) {
  108. Move (4 + hot_pos, 0);
  109. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  110. Driver.AddRune (hot_key);
  111. }
  112. }
  113. ///<inheritdoc/>
  114. public override void PositionCursor ()
  115. {
  116. Move (1, 0);
  117. }
  118. ///<inheritdoc/>
  119. public override bool ProcessKey (KeyEvent kb)
  120. {
  121. if (kb.KeyValue == ' ') {
  122. var previousChecked = Checked;
  123. Checked = !Checked;
  124. OnToggled (previousChecked);
  125. SetNeedsDisplay ();
  126. return true;
  127. }
  128. return base.ProcessKey (kb);
  129. }
  130. ///<inheritdoc/>
  131. public override bool MouseEvent (MouseEvent me)
  132. {
  133. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked))
  134. return false;
  135. SuperView.SetFocus (this);
  136. var previousChecked = Checked;
  137. Checked = !Checked;
  138. OnToggled (previousChecked);
  139. SetNeedsDisplay ();
  140. return true;
  141. }
  142. }
  143. }