DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame^] | 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 | |
| 28 | #include <FL/Fl.H> |
| 29 | #include <FL/Fl_Adjuster.H> |
| 30 | #include <FL/Fl_Bitmap.H> |
| 31 | #include <FL/fl_draw.H> |
| 32 | |
| 33 | #include "fastarrow.h" |
| 34 | static Fl_Bitmap fastarrow(fastarrow_bits, fastarrow_width, fastarrow_height); |
| 35 | #include "mediumarrow.h" |
| 36 | static Fl_Bitmap mediumarrow(mediumarrow_bits, mediumarrow_width, mediumarrow_height); |
| 37 | #include "slowarrow.h" |
| 38 | static Fl_Bitmap slowarrow(slowarrow_bits, slowarrow_width, slowarrow_height); |
| 39 | |
| 40 | // changing the value does not change the appearance: |
| 41 | void Fl_Adjuster::value_damage() {} |
| 42 | |
| 43 | void Fl_Adjuster::draw() { |
| 44 | int dx, dy, W, H; |
| 45 | if (w()>=h()) { |
| 46 | dx = W = w()/3; |
| 47 | dy = 0; H = h(); |
| 48 | } else { |
| 49 | dx = 0; W = w(); |
| 50 | dy = H = h()/3; |
| 51 | } |
| 52 | draw_box(drag==1?FL_DOWN_BOX:box(), x(), y()+2*dy, W, H, color()); |
| 53 | draw_box(drag==2?FL_DOWN_BOX:box(), x()+dx, y()+dy, W, H, color()); |
| 54 | draw_box(drag==3?FL_DOWN_BOX:box(), x()+2*dx, y(), W, H, color()); |
| 55 | if (active_r()) |
| 56 | fl_color(selection_color()); |
| 57 | else |
| 58 | fl_color(fl_inactive(selection_color())); |
| 59 | fastarrow.draw(x()+(W-fastarrow_width)/2, |
| 60 | y()+2*dy+(H-fastarrow_height)/2, W, H); |
| 61 | mediumarrow.draw(x()+dx+(W-mediumarrow_width)/2, |
| 62 | y()+dy+(H-mediumarrow_height)/2, W, H); |
| 63 | slowarrow.draw(x()+2*dx+(W-slowarrow_width)/2, |
| 64 | y()+(H-slowarrow_width)/2, W, H); |
| 65 | if (Fl::focus() == this) draw_focus(); |
| 66 | } |
| 67 | |
| 68 | int Fl_Adjuster::handle(int event) { |
| 69 | double v; |
| 70 | int delta; |
| 71 | int mx = Fl::event_x(); |
| 72 | // Fl_Widget_Tracker wp(this); |
| 73 | switch (event) { |
| 74 | case FL_PUSH: |
| 75 | if (Fl::visible_focus()) Fl::focus(this); |
| 76 | ix = mx; |
| 77 | if (w()>=h()) |
| 78 | drag = 3*(mx-x())/w() + 1; |
| 79 | else |
| 80 | drag = 3-3*(Fl::event_y()-y()-1)/h(); |
| 81 | { Fl_Widget_Tracker wp(this); |
| 82 | handle_push(); |
| 83 | if (wp.deleted()) return 1; |
| 84 | } |
| 85 | redraw(); |
| 86 | return 1; |
| 87 | case FL_DRAG: |
| 88 | if (w() >= h()) { |
| 89 | delta = x()+(drag-1)*w()/3; // left edge of button |
| 90 | if (mx < delta) |
| 91 | delta = mx-delta; |
| 92 | else if (mx > (delta+w()/3)) // right edge of button |
| 93 | delta = mx-delta-w()/3; |
| 94 | else |
| 95 | delta = 0; |
| 96 | } else { |
| 97 | if (mx < x()) |
| 98 | delta = mx-x(); |
| 99 | else if (mx > (x()+w())) |
| 100 | delta = mx-x()-w(); |
| 101 | else |
| 102 | delta = 0; |
| 103 | } |
| 104 | switch (drag) { |
| 105 | case 3: v = increment(previous_value(), delta); break; |
| 106 | case 2: v = increment(previous_value(), delta*10); break; |
| 107 | default:v = increment(previous_value(), delta*100); break; |
| 108 | } |
| 109 | handle_drag(soft() ? softclamp(v) : clamp(v)); |
| 110 | return 1; |
| 111 | case FL_RELEASE: |
| 112 | if (Fl::event_is_click()) { // detect click but no drag |
| 113 | if (Fl::event_state()&0xF0000) delta = -10; |
| 114 | else delta = 10; |
| 115 | switch (drag) { |
| 116 | case 3: v = increment(previous_value(), delta); break; |
| 117 | case 2: v = increment(previous_value(), delta*10); break; |
| 118 | default:v = increment(previous_value(), delta*100); break; |
| 119 | } |
| 120 | Fl_Widget_Tracker wp(this); |
| 121 | handle_drag(soft() ? softclamp(v) : clamp(v)); |
| 122 | if (wp.deleted()) return 1; |
| 123 | } |
| 124 | drag = 0; |
| 125 | redraw(); |
| 126 | handle_release(); |
| 127 | return 1; |
| 128 | case FL_KEYBOARD : |
| 129 | switch (Fl::event_key()) { |
| 130 | case FL_Up: |
| 131 | if (w() > h()) return 0; |
| 132 | handle_drag(clamp(increment(value(),-1))); |
| 133 | return 1; |
| 134 | case FL_Down: |
| 135 | if (w() > h()) return 0; |
| 136 | handle_drag(clamp(increment(value(),1))); |
| 137 | return 1; |
| 138 | case FL_Left: |
| 139 | if (w() < h()) return 0; |
| 140 | handle_drag(clamp(increment(value(),-1))); |
| 141 | return 1; |
| 142 | case FL_Right: |
| 143 | if (w() < h()) return 0; |
| 144 | handle_drag(clamp(increment(value(),1))); |
| 145 | return 1; |
| 146 | default: |
| 147 | return 0; |
| 148 | } |
| 149 | // break not required because of switch... |
| 150 | |
| 151 | case FL_FOCUS: |
| 152 | case FL_UNFOCUS: |
| 153 | if (Fl::visible_focus()) { |
| 154 | redraw(); |
| 155 | return 1; |
| 156 | } else return 0; |
| 157 | |
| 158 | case FL_ENTER : |
| 159 | case FL_LEAVE : |
| 160 | return 1; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | Creates a new Fl_Adjuster widget using the given position, |
| 167 | size, and label string. It looks best if one of the dimensions is 3 |
| 168 | times the other. |
| 169 | <P> Inherited destructor destroys the Valuator. |
| 170 | */ |
| 171 | Fl_Adjuster::Fl_Adjuster(int X, int Y, int W, int H, const char* l) |
| 172 | : Fl_Valuator(X, Y, W, H, l) { |
| 173 | box(FL_UP_BOX); |
| 174 | step(1, 10000); |
| 175 | selection_color(FL_SELECTION_COLOR); |
| 176 | drag = 0; |
| 177 | soft_ = 1; |
| 178 | } |
| 179 | |
| 180 | // |
| 181 | // End of "$Id: Fl_Adjuster.cxx 7903 2010-11-28 21:06:39Z matt $". |
| 182 | // |