/gui/togglefield.d

http://github.com/wilkie/djehuty · D · 282 lines · 187 code · 79 blank · 16 comment · 25 complexity · 5d3034bcd09fb627b28ff2b64d3d7c45 MD5 · raw file

  1. /*
  2. * togglefield.d
  3. *
  4. * This module implements a GUI widget for an optional selection.
  5. *
  6. * Author: Dave Wilkinson
  7. *
  8. */
  9. module gui.togglefield;
  10. import gui.widget;
  11. import core.color;
  12. import core.definitions;
  13. import core.string;
  14. import graphics.graphics;
  15. template ControlPrintCSTRList() {
  16. const char[] ControlPrintCSTRList = `
  17. this(int x, int y, int width, int height, String value) {
  18. super(x,y,width,height,value);
  19. }
  20. this(int x, int y, int width, int height, string value) {
  21. super(x,y,width,height,value);
  22. }
  23. `;
  24. }
  25. // Description: This control provides a standard toggle field. When grouped, these will act as a exclusive list of options, essentially a 'radio' or 'option' field. Otherwise they are 'check' fields.
  26. class ToggleField : Widget {
  27. enum Signal : uint {
  28. Selected,
  29. Unselected,
  30. }
  31. this(int x, int y, int width, int height, string value) {
  32. super(x,y,width,height);
  33. _value = value.dup;
  34. }
  35. void unselect() {
  36. _btnstate = 0;
  37. }
  38. void select() {
  39. _btnstate = 1;
  40. }
  41. void text(string newTitle) {
  42. _value = newTitle.dup;
  43. }
  44. string text() {
  45. return _value.dup;
  46. }
  47. // handle events
  48. override void onAdd() {
  49. _brsh = new Brush(Color.White);
  50. _clroutline = Color.fromRGB(0x80, 0x80, 0x80);
  51. _clrhighlight = Color.fromRGB(0xdd,0xdd,0xdd);
  52. _clrnormal = Color.fromRGB(0xaa,0xaa,0xaa);
  53. _clrforeground = Color.fromRGB(0,0,0);
  54. _clrbackground = Color.fromRGB(0xff,0xff,0xff);
  55. Graphics grp = _view.lock();
  56. _font = new Font(FontSans, 8, 400, false, false, false);
  57. grp.font = _font;
  58. grp.measureText(_value,_valueBounds);
  59. _view.unlock();
  60. //FIRE_EVENT(id,EventCreated,0,0);
  61. }
  62. override void onDraw(ref Graphics g) {
  63. //Draw Background of Button
  64. Brush brush;
  65. Pen pen;
  66. Rect chkRect;
  67. chkRect.left = this.left + 2;
  68. chkRect.top = this.top + 2;
  69. chkRect.right = (chkRect.left + this.height) - 4;
  70. chkRect.bottom = this.bottom - 2;
  71. if (_is_grouped) {
  72. if (chkRect.right > this.right) {
  73. chkRect.right = this.right;
  74. chkRect.top += (this.height - this.width) / 2;
  75. chkRect.bottom = chkRect.top + this.width;
  76. }
  77. brush = new Brush(_clrbackground);
  78. pen = new Pen(_clroutline);
  79. g.brush = brush;
  80. g.pen = pen;
  81. g.drawRect(chkRect.left, chkRect.top, chkRect.right-chkRect.left, chkRect.bottom-chkRect.top);
  82. pen.setColor(_clrhighlight);
  83. g.drawLine(chkRect.left+1, chkRect.top+1, chkRect.right - 1, chkRect.top+1);
  84. g.drawLine(chkRect.left+1, chkRect.top+2, chkRect.left + 1, chkRect.bottom-1);
  85. //Draw Check
  86. if (_mouseholdstate == 1) {
  87. pen.setColor(_clrnormal);
  88. brush.setColor(_clrbackground);
  89. g.drawOval(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom-3);
  90. }
  91. else if (_btnstate == 1) {
  92. brush.setColor(_clrnormal);
  93. pen.setColor(_clrnormal);
  94. g.drawOval(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom - 3);
  95. if (_hovered) {
  96. pen.setColor(_clrhighlight);
  97. g.drawOval(chkRect.left + 4, chkRect.top + 4, chkRect.right - 4, chkRect.bottom-4);
  98. }
  99. }
  100. else if (_hovered) {
  101. pen.setColor(_clrhighlight);
  102. brush.setColor(_clrbackground);
  103. g.drawOval(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom-3);
  104. }
  105. }
  106. else {
  107. //Draw Background of Button
  108. chkRect.left = this.left + 2;
  109. chkRect.top = this.top + 2;
  110. chkRect.right = (chkRect.left + this.height) - 4;
  111. chkRect.bottom = this.bottom - 2;
  112. if (chkRect.right > this.right) {
  113. chkRect.right = this.right;
  114. chkRect.top += (this.height - this.width) / 2;
  115. chkRect.bottom = chkRect.top + this.width;
  116. }
  117. pen = new Pen(_clroutline);
  118. brush = new Brush(_clrbackground);
  119. g.brush = brush;
  120. g.pen = pen;
  121. g.drawRect(chkRect.left, chkRect.top, chkRect.right-chkRect.left, chkRect.bottom-chkRect.top);
  122. pen.setColor(_clrhighlight);
  123. g.drawLine(chkRect.left+1, chkRect.top+1, chkRect.right - 1, chkRect.top+1);
  124. g.drawLine(chkRect.left+1, chkRect.top+2, chkRect.left + 1, chkRect.bottom-1);
  125. //Draw Check
  126. if (_mouseholdstate == 1) {
  127. pen.setColor(_clrnormal);
  128. brush.setColor(_clrbackground);
  129. g.drawRect(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom-3);
  130. }
  131. else if (_btnstate == 1) {
  132. brush.setColor(_clrnormal);
  133. pen.setColor(_clrnormal);
  134. g.drawRect(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom - 3);
  135. if (_hovered) {
  136. pen.setColor(_clrhighlight);
  137. g.drawRect(chkRect.left + 4, chkRect.top + 4, chkRect.right - 4, chkRect.bottom-4);
  138. }
  139. }
  140. else if (_hovered) {
  141. pen.setColor(_clrhighlight);
  142. brush.setColor(_clrbackground);
  143. g.drawRect(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom-3);
  144. }
  145. }
  146. //Draw the text
  147. g.forecolor = _clrforeground;
  148. g.setTextModeTransparent();
  149. g.font = _font;
  150. Rect ctrlrt;
  151. ctrlrt.left = chkRect.right;
  152. ctrlrt.right = this.right;
  153. ctrlrt.top = this.top;
  154. ctrlrt.bottom = this.bottom;
  155. g.drawClippedText(chkRect.right + 4, (this.bottom + this.top-_valueBounds.y)/2, ctrlrt, _value);
  156. }
  157. override bool onPrimaryMouseDown(ref Mouse mouseProps) {
  158. if (!_enabled) { return false; }
  159. _mouseholdstate = 1;
  160. requestCapture();
  161. return true;
  162. }
  163. override bool onPrimaryMouseUp(ref Mouse mouseProps) {
  164. if (!_enabled) { return false; }
  165. requestRelease();
  166. _mouseholdstate = 0;
  167. if (_hovered) {
  168. if (!(_is_grouped && _btnstate)) {
  169. _btnstate = !_btnstate;
  170. raiseSignal(Signal.Selected);
  171. }
  172. }
  173. return true;
  174. }
  175. override bool onMouseEnter() {
  176. Graphics g = _view.lock();
  177. _brsh.setColor(Color.White);
  178. _view.unlock();
  179. return true;
  180. }
  181. override bool onMouseLeave() {
  182. Graphics g = _view.lock();
  183. Color c;
  184. c.fromRGB(0xc8, 0xc8, 0xc8);
  185. _brsh.setColor(c);
  186. _view.unlock();
  187. return true;
  188. }
  189. private:
  190. string _value;
  191. package bool _is_grouped = false;
  192. Brush _brsh;
  193. Pen _pen;
  194. Font _font;
  195. Color _clroutline;
  196. Color _clrhighlight;
  197. Color _clrnormal;
  198. Color _clrforeground;
  199. Color _clrbackground;
  200. int _btnstate = 0;
  201. int _mouseholdstate = 0;
  202. Size _valueBounds;
  203. }