Checkbox.cs 3.6 KB

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