2
0

Checkbox.cs 3.4 KB

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