Fl_Adjuster.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // "$Id: Fl_Adjuster.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Adjuster widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. #include <FL/Fl.H>
  28. #include <FL/Fl_Adjuster.H>
  29. #include <FL/Fl_Bitmap.H>
  30. #include <FL/fl_draw.H>
  31. #include "fastarrow.h"
  32. static Fl_Bitmap fastarrow(fastarrow_bits, fastarrow_width, fastarrow_height);
  33. #include "mediumarrow.h"
  34. static Fl_Bitmap mediumarrow(mediumarrow_bits, mediumarrow_width, mediumarrow_height);
  35. #include "slowarrow.h"
  36. static Fl_Bitmap slowarrow(slowarrow_bits, slowarrow_width, slowarrow_height);
  37. // changing the value does not change the appearance:
  38. void Fl_Adjuster::value_damage() {}
  39. void Fl_Adjuster::draw() {
  40. int dx, dy, W, H;
  41. if (w()>=h()) {
  42. dx = W = w()/3;
  43. dy = 0; H = h();
  44. } else {
  45. dx = 0; W = w();
  46. dy = H = h()/3;
  47. }
  48. draw_box(drag==1?FL_DOWN_BOX:box(), x(), y()+2*dy, W, H, color());
  49. draw_box(drag==2?FL_DOWN_BOX:box(), x()+dx, y()+dy, W, H, color());
  50. draw_box(drag==3?FL_DOWN_BOX:box(), x()+2*dx, y(), W, H, color());
  51. if (active_r())
  52. fl_color(selection_color());
  53. else
  54. fl_color(fl_inactive(selection_color()));
  55. fastarrow.draw(x()+(W-fastarrow_width)/2,
  56. y()+2*dy+(H-fastarrow_height)/2, W, H);
  57. mediumarrow.draw(x()+dx+(W-mediumarrow_width)/2,
  58. y()+dy+(H-mediumarrow_height)/2, W, H);
  59. slowarrow.draw(x()+2*dx+(W-slowarrow_width)/2,
  60. y()+(H-slowarrow_width)/2, W, H);
  61. if (Fl::focus() == this) draw_focus();
  62. }
  63. int Fl_Adjuster::handle(int event) {
  64. double v;
  65. int delta;
  66. int mx = Fl::event_x();
  67. // Fl_Widget_Tracker wp(this);
  68. switch (event) {
  69. case FL_PUSH:
  70. if (Fl::visible_focus()) Fl::focus(this);
  71. ix = mx;
  72. if (w()>=h())
  73. drag = 3*(mx-x())/w() + 1;
  74. else
  75. drag = 3-3*(Fl::event_y()-y()-1)/h();
  76. { Fl_Widget_Tracker wp(this);
  77. handle_push();
  78. if (wp.deleted()) return 1;
  79. }
  80. redraw();
  81. return 1;
  82. case FL_DRAG:
  83. if (w() >= h()) {
  84. delta = x()+(drag-1)*w()/3; // left edge of button
  85. if (mx < delta)
  86. delta = mx-delta;
  87. else if (mx > (delta+w()/3)) // right edge of button
  88. delta = mx-delta-w()/3;
  89. else
  90. delta = 0;
  91. } else {
  92. if (mx < x())
  93. delta = mx-x();
  94. else if (mx > (x()+w()))
  95. delta = mx-x()-w();
  96. else
  97. delta = 0;
  98. }
  99. switch (drag) {
  100. case 3: v = increment(previous_value(), delta); break;
  101. case 2: v = increment(previous_value(), delta*10); break;
  102. default:v = increment(previous_value(), delta*100); break;
  103. }
  104. handle_drag(soft() ? softclamp(v) : clamp(v));
  105. return 1;
  106. case FL_RELEASE:
  107. if (Fl::event_is_click()) { // detect click but no drag
  108. if (Fl::event_state()&0xF0000) delta = -10;
  109. else delta = 10;
  110. switch (drag) {
  111. case 3: v = increment(previous_value(), delta); break;
  112. case 2: v = increment(previous_value(), delta*10); break;
  113. default:v = increment(previous_value(), delta*100); break;
  114. }
  115. Fl_Widget_Tracker wp(this);
  116. handle_drag(soft() ? softclamp(v) : clamp(v));
  117. if (wp.deleted()) return 1;
  118. }
  119. drag = 0;
  120. redraw();
  121. handle_release();
  122. return 1;
  123. case FL_KEYBOARD :
  124. switch (Fl::event_key()) {
  125. case FL_Up:
  126. if (w() > h()) return 0;
  127. handle_drag(clamp(increment(value(),-1)));
  128. return 1;
  129. case FL_Down:
  130. if (w() > h()) return 0;
  131. handle_drag(clamp(increment(value(),1)));
  132. return 1;
  133. case FL_Left:
  134. if (w() < h()) return 0;
  135. handle_drag(clamp(increment(value(),-1)));
  136. return 1;
  137. case FL_Right:
  138. if (w() < h()) return 0;
  139. handle_drag(clamp(increment(value(),1)));
  140. return 1;
  141. default:
  142. return 0;
  143. }
  144. // break not required because of switch...
  145. case FL_FOCUS:
  146. case FL_UNFOCUS:
  147. if (Fl::visible_focus()) {
  148. redraw();
  149. return 1;
  150. } else return 0;
  151. case FL_ENTER :
  152. case FL_LEAVE :
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. /**
  158. Creates a new Fl_Adjuster widget using the given position,
  159. size, and label string. It looks best if one of the dimensions is 3
  160. times the other.
  161. <P> Inherited destructor destroys the Valuator.
  162. */
  163. Fl_Adjuster::Fl_Adjuster(int X, int Y, int W, int H, const char* l)
  164. : Fl_Valuator(X, Y, W, H, l) {
  165. box(FL_UP_BOX);
  166. step(1, 10000);
  167. selection_color(FL_SELECTION_COLOR);
  168. drag = 0;
  169. soft_ = 1;
  170. }
  171. //
  172. // End of "$Id: Fl_Adjuster.cxx 7903 2010-11-28 21:06:39Z matt $".
  173. //