/cui/textfield.d

http://github.com/wilkie/djehuty · D · 210 lines · 131 code · 49 blank · 30 comment · 26 complexity · d62712edcbad227e4fe1f1c9f75a7269 MD5 · raw file

  1. /*
  2. * textfield.d
  3. *
  4. * This module implements a text field widget for TUI applications.
  5. *
  6. * Author: Dave Wilkinson, Lindsey Bieda
  7. *
  8. */
  9. module cui.textfield;
  10. import djehuty;
  11. import io.console;
  12. import cui.widget;
  13. // Section: Console
  14. // Description: This console control abstracts a simple one line text field.
  15. class CuiTextField : CuiWidget {
  16. // Constructors
  17. this( uint x, uint y, uint width, string value = "") {
  18. super(x,y,width,1);
  19. _max = width-2;
  20. _value = value.dup;
  21. }
  22. // Events
  23. override void onAdd() {
  24. }
  25. override void onInit() {
  26. onDraw();
  27. }
  28. override void onKeyDown(Key key) {
  29. Console.forecolor = _forecolor;
  30. Console.backcolor = _backcolor;
  31. if (key.code == Key.Backspace) {
  32. if (_pos > 0) {
  33. if (_pos == _max) {
  34. Console.put(' ');
  35. _value = _value.substring(0,_value.length-1);
  36. }
  37. else {
  38. Console.put(cast(char)0x8);
  39. Console.put(' ');
  40. _value = _value.substring(0, _pos-1) ~ _value.substring(_pos);
  41. }
  42. Console.put(cast(char)0x8);
  43. _pos--;
  44. }
  45. }
  46. else if (key.code == Key.Return) {
  47. tabForward();
  48. }
  49. }
  50. override void onKeyChar(dchar keyChar) {
  51. Console.forecolor = _forecolor;
  52. Console.backcolor = _backcolor;
  53. if (keyChar != 0x8 && keyChar != '\t' && keyChar != '\n' && keyChar != '\r') {
  54. if (_pos <= _max) {
  55. Console.put(keyChar);
  56. _pos++;
  57. if (_pos >= _max) {
  58. _pos = _max;
  59. _value = _value.substring(0, _value.length-1) ~ Unicode.toUtf8([keyChar]);
  60. Console.put(cast(char)(0x8));
  61. }
  62. else {
  63. _value = _value.substring(0, _pos-1) ~ Unicode.toUtf8([keyChar]) ~ _value.substring(_pos-1);
  64. }
  65. }
  66. }
  67. }
  68. override void onGotFocus() {
  69. Console.showCaret();
  70. positionCursor();
  71. Console.forecolor = _forecolor;
  72. Console.backcolor = _backcolor;
  73. }
  74. // Properties
  75. // Description: This property returns the current text within the field.
  76. // Returns: The value of the field.
  77. string text() {
  78. return _value.dup;
  79. }
  80. // Description: This property sets the current text within the field.
  81. // text: The new value for the field.
  82. void text(string text) {
  83. _value = text.dup;
  84. onDraw();
  85. }
  86. // Description: This property returns the current forecolor of the text in the field
  87. // Returns: forecolor of text
  88. Color forecolor() {
  89. return _forecolor;
  90. }
  91. // Description: This property sets the current forecolor of the text in the field
  92. // value: The new forecolor
  93. void forecolor(Color value) {
  94. _forecolor = value;
  95. onDraw();
  96. }
  97. // Description: This property returns the current backcolor of the text in the field
  98. // Returns: backcolor of text
  99. Color backcolor() {
  100. return _backcolor;
  101. }
  102. // Description: This property sets the current backcolor of the text in the field
  103. // value: The new backcolor
  104. void backcolor(Color value) {
  105. _backcolor = value;
  106. onDraw();
  107. }
  108. // Description: This property returns the current forecolor of the borders of the field
  109. // Returns: forecolor of borders
  110. Color basecolor() {
  111. return _color;
  112. }
  113. // Description: This property sets the current forecolor of the borders of the field
  114. // value: The new forecolor of the borders
  115. void basecolor(Color value) {
  116. _color = value;
  117. }
  118. // Methods
  119. override bool isTabStop() {
  120. return true;
  121. }
  122. override void onDraw() {
  123. if (canDraw) {
  124. Console.position(this.left, this.top);
  125. Console.forecolor = _color;
  126. Console.backcolor = Color.Black;
  127. Console.put("[");
  128. Console.forecolor = _forecolor;
  129. Console.backcolor = _backcolor;
  130. foreach(chr; _value.substring(0,_max)) {
  131. Console.put(chr);
  132. }
  133. _pos = _value.length;
  134. if (_pos > _max) {
  135. _pos = _max;
  136. }
  137. for (int i=_value.length; i<_max; i++) {
  138. Console.put(' ');
  139. }
  140. Console.forecolor = _color;
  141. Console.backcolor = Color.Black;
  142. Console.put("]");
  143. positionCursor();
  144. }
  145. }
  146. protected:
  147. Color _color = Color.Blue;
  148. Color _forecolor = Color.White;
  149. Color _backcolor = Color.Black;
  150. uint _pos = 0;
  151. uint _max = 0;
  152. string _value;
  153. void positionCursor() {
  154. if (_pos == _max) {
  155. Console.position(this.left+_max, this.top);
  156. }
  157. else {
  158. Console.position(this.left+1+_pos, this.top);
  159. }
  160. }
  161. }