Checkbox.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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
  28. /// the given text at the given position.
  29. /// </summary>
  30. /// <remarks>
  31. /// The size of CheckButton is computed based on the
  32. /// text length. This CheckButton is not toggled.
  33. /// </remarks>
  34. public CheckBox (int x, int y, ustring s) : this (x, y, s, false)
  35. {
  36. }
  37. /// <summary>
  38. /// Public constructor, creates a CheckButton based on
  39. /// the given text at the given position and a state.
  40. /// </summary>
  41. /// <remarks>
  42. /// The size of CheckButton is computed based on the
  43. /// text length.
  44. /// </remarks>
  45. public CheckBox (int x, int y, ustring s, bool is_checked) : base (new Rect (x, y, s.Length + 4, 1))
  46. {
  47. Checked = is_checked;
  48. Text = s;
  49. CanFocus = true;
  50. }
  51. /// <summary>
  52. /// The state of the checkbox.
  53. /// </summary>
  54. public bool Checked { get; set; }
  55. /// <summary>
  56. /// The text displayed by this widget.
  57. /// </summary>
  58. public ustring Text {
  59. get {
  60. return text;
  61. }
  62. set {
  63. text = value;
  64. int i = 0;
  65. hot_pos = -1;
  66. hot_key = (char)0;
  67. foreach (Rune c in text) {
  68. if (Rune.IsUpper (c)) {
  69. hot_key = c;
  70. hot_pos = i;
  71. break;
  72. }
  73. i++;
  74. }
  75. }
  76. }
  77. public override void Redraw (Rect region)
  78. {
  79. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
  80. Move (0, 0);
  81. Driver.AddStr (Checked ? "[x] " : "[ ] ");
  82. Move (4, 0);
  83. Driver.AddStr (Text);
  84. if (hot_pos != -1) {
  85. Move (4 + hot_pos, 0);
  86. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  87. Driver.AddRune (hot_key);
  88. }
  89. }
  90. public override void PositionCursor ()
  91. {
  92. Move (1, 0);
  93. }
  94. public override bool ProcessKey (KeyEvent kb)
  95. {
  96. if (kb.KeyValue == ' ') {
  97. Checked = !Checked;
  98. if (Toggled != null)
  99. Toggled (this, EventArgs.Empty);
  100. SetNeedsDisplay ();
  101. return true;
  102. }
  103. return base.ProcessKey (kb);
  104. }
  105. public override bool MouseEvent (MouseEvent me)
  106. {
  107. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked))
  108. return false;
  109. SuperView.SetFocus (this);
  110. Checked = !Checked;
  111. SetNeedsDisplay ();
  112. if (Toggled != null)
  113. Toggled (this, EventArgs.Empty);
  114. return true;
  115. }
  116. }
  117. }