Checkbox.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. namespace Terminal {
  3. public class CheckBox : View {
  4. string text;
  5. int hot_pos = -1;
  6. char hot_key;
  7. /// <summary>
  8. /// Toggled event, raised when the CheckButton is toggled.
  9. /// </summary>
  10. /// <remarks>
  11. /// Client code can hook up to this event, it is
  12. /// raised when the checkbutton is activated either with
  13. /// the mouse or the keyboard.
  14. /// </remarks>
  15. public event EventHandler Toggled;
  16. /// <summary>
  17. /// Public constructor, creates a CheckButton based on
  18. /// the given text at the given position.
  19. /// </summary>
  20. /// <remarks>
  21. /// The size of CheckButton is computed based on the
  22. /// text length. This CheckButton is not toggled.
  23. /// </remarks>
  24. public CheckBox (int x, int y, string s) : this (x, y, s, false)
  25. {
  26. }
  27. /// <summary>
  28. /// Public constructor, creates a CheckButton based on
  29. /// the given text at the given position and a state.
  30. /// </summary>
  31. /// <remarks>
  32. /// The size of CheckButton is computed based on the
  33. /// text length.
  34. /// </remarks>
  35. public CheckBox (int x, int y, string s, bool is_checked) : base (new Rect (x, y, s.Length + 4, 1))
  36. {
  37. Checked = is_checked;
  38. Text = s;
  39. CanFocus = true;
  40. }
  41. /// <summary>
  42. /// The state of the checkbox.
  43. /// </summary>
  44. public bool Checked { get; set; }
  45. /// <summary>
  46. /// The text displayed by this widget.
  47. /// </summary>
  48. public string Text {
  49. get {
  50. return text;
  51. }
  52. set {
  53. text = value;
  54. int i = 0;
  55. hot_pos = -1;
  56. hot_key = (char)0;
  57. foreach (char c in text) {
  58. if (Char.IsUpper (c)) {
  59. hot_key = c;
  60. hot_pos = i;
  61. break;
  62. }
  63. i++;
  64. }
  65. }
  66. }
  67. public override void Redraw (Rect region)
  68. {
  69. Driver.SetAttribute (HasFocus ? Colors.Base.Focus : Colors.Base.Normal);
  70. Move (0, 0);
  71. Driver.AddStr (Checked ? "[x] " : "[ ] ");
  72. Move (4, 0);
  73. Driver.AddStr (Text);
  74. if (hot_pos != -1) {
  75. Move (4 + hot_pos, 0);
  76. Driver.SetAttribute (HasFocus ? Colors.Base.HotFocus : Colors.Base.HotNormal);
  77. Driver.AddCh (hot_key);
  78. }
  79. }
  80. public override void PositionCursor ()
  81. {
  82. Move (1, 0);
  83. }
  84. public override bool ProcessKey (KeyEvent kb)
  85. {
  86. if (kb.KeyValue == ' ') {
  87. Checked = !Checked;
  88. if (Toggled != null)
  89. Toggled (this, EventArgs.Empty);
  90. SetNeedsDisplay ();
  91. return true;
  92. }
  93. return false;
  94. }
  95. #if false
  96. public override void ProcessMouse (Curses.MouseEvent ev)
  97. {
  98. if ((ev.ButtonState & Curses.Event.Button1Clicked) != 0){
  99. Container.SetFocus (this);
  100. Container.Redraw ();
  101. Checked = !Checked;
  102. if (Toggled != null)
  103. Toggled (this, EventArgs.Empty);
  104. Redraw ();
  105. }
  106. }
  107. #endif
  108. }
  109. }