Button.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Button.cs: Button control
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. namespace Terminal {
  11. /// <summary>
  12. /// Button view
  13. /// </summary>
  14. /// <remarks>
  15. /// Provides a button that can be clicked, or pressed with
  16. /// the enter key and processes hotkeys (the first uppercase
  17. /// letter in the button becomes the hotkey).
  18. /// </remarks>
  19. public class Button : View {
  20. string text;
  21. string shown_text;
  22. char hot_key;
  23. int hot_pos = -1;
  24. bool is_default;
  25. /// <summary>
  26. /// Clicked event, raised when the button is clicked.
  27. /// </summary>
  28. /// <remarks>
  29. /// Client code can hook up to this event, it is
  30. /// raised when the button is activated either with
  31. /// the mouse or the keyboard.
  32. /// </remarks>
  33. public event EventHandler Clicked;
  34. /// <summary>
  35. /// Public constructor, creates a button based on
  36. /// the given text at position 0,0
  37. /// </summary>
  38. /// <remarks>
  39. /// The size of the button is computed based on the
  40. /// text length. This button is not a default button.
  41. /// </remarks>
  42. public Button (string s) : this (0, 0, s) { }
  43. /// <summary>
  44. /// Public constructor, creates a button based on
  45. /// the given text.
  46. /// </summary>
  47. /// <remarks>
  48. /// If the value for is_default is true, a special
  49. /// decoration is used, and the enter key on a
  50. /// dialog would implicitly activate this button.
  51. /// </remarks>
  52. public Button (string s, bool is_default) : this (0, 0, s, is_default) { }
  53. /// <summary>
  54. /// Public constructor, creates a button based on
  55. /// the given text at the given position.
  56. /// </summary>
  57. /// <remarks>
  58. /// The size of the button is computed based on the
  59. /// text length. This button is not a default button.
  60. /// </remarks>
  61. public Button (int x, int y, string s) : this (x, y, s, false) { }
  62. /// <summary>
  63. /// The text displayed by this widget.
  64. /// </summary>
  65. public string Text {
  66. get {
  67. return text;
  68. }
  69. set {
  70. text = value;
  71. if (is_default)
  72. shown_text = "[< " + value + " >]";
  73. else
  74. shown_text = "[ " + value + " ]";
  75. hot_pos = -1;
  76. hot_key = (char)0;
  77. int i = 0;
  78. foreach (char c in shown_text) {
  79. if (Char.IsUpper (c)) {
  80. hot_key = c;
  81. hot_pos = i;
  82. break;
  83. }
  84. i++;
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// Public constructor, creates a button based on
  90. /// the given text at the given position.
  91. /// </summary>
  92. /// <remarks>
  93. /// If the value for is_default is true, a special
  94. /// decoration is used, and the enter key on a
  95. /// dialog would implicitly activate this button.
  96. /// </remarks>
  97. public Button (int x, int y, string s, bool is_default)
  98. : base (new Rect (x, y, s.Length + 4 + (is_default ? 2 : 0), 1))
  99. {
  100. CanFocus = true;
  101. this.is_default = is_default;
  102. Text = s;
  103. }
  104. public override void Redraw (Rect region)
  105. {
  106. Driver.SetAttribute (HasFocus ? Colors.Base.Focus : Colors.Base.Normal);
  107. Move (0, 0);
  108. Driver.AddStr (shown_text);
  109. if (hot_pos != -1) {
  110. Move (hot_pos, 0);
  111. Driver.SetAttribute (HasFocus ? Colors.Base.HotFocus : Colors.Base.HotNormal);
  112. Driver.AddCh (hot_key);
  113. }
  114. }
  115. public override void PositionCursor ()
  116. {
  117. Move (hot_pos, 0);
  118. }
  119. bool CheckKey (KeyEvent key)
  120. {
  121. if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
  122. this.SuperView.SetFocus (this);
  123. if (Clicked != null)
  124. Clicked (this, EventArgs.Empty);
  125. return true;
  126. }
  127. return false;
  128. }
  129. public override bool ProcessHotKey (KeyEvent kb)
  130. {
  131. if (kb.IsAlt)
  132. return CheckKey (kb);
  133. return false;
  134. }
  135. public override bool ProcessColdKey (KeyEvent kb)
  136. {
  137. if (is_default && kb.KeyValue == '\n') {
  138. if (Clicked != null)
  139. Clicked (this, EventArgs.Empty);
  140. return true;
  141. }
  142. return CheckKey (kb);
  143. }
  144. public override bool ProcessKey (KeyEvent kb)
  145. {
  146. var c = kb.KeyValue;
  147. if (c == '\n' || c == ' ' || Char.ToUpper ((char)c) == hot_key) {
  148. if (Clicked != null)
  149. Clicked (this, EventArgs.Empty);
  150. return true;
  151. }
  152. return base.ProcessKey (kb);
  153. }
  154. #if false
  155. public override void ProcessMouse (Curses.MouseEvent ev)
  156. {
  157. if ((ev.ButtonState & Curses.Event.Button1Clicked) != 0) {
  158. Container.SetFocus (this);
  159. Container.Redraw ();
  160. if (Clicked != null)
  161. Clicked (this, EventArgs.Empty);
  162. }
  163. }
  164. #endif
  165. }
  166. }