Checkbox.cs 2.7 KB

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