Checkbox.cs 2.8 KB

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