DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame^] | 1 | // |
| 2 | // "$Id: Fl_Value_Input.cxx 7903 2010-11-28 21:06:39Z matt $" |
| 3 | // |
| 4 | // Value input 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 | // FLTK widget for drag-adjusting a floating point value. |
| 29 | // Warning: this works by making a child Fl_Input object, even |
| 30 | // though this object is *not* an Fl_Group. May be a kludge? |
| 31 | |
| 32 | #include <FL/Fl.H> |
| 33 | #include <FL/Fl_Value_Input.H> |
| 34 | #include <FL/Fl_Group.H> |
| 35 | #include <stdlib.h> |
| 36 | #include <FL/math.h> |
| 37 | |
| 38 | |
| 39 | void Fl_Value_Input::input_cb(Fl_Widget*, void* v) { |
| 40 | Fl_Value_Input& t = *(Fl_Value_Input*)v; |
| 41 | double nv; |
| 42 | if ((t.step() - floor(t.step()))>0.0 || t.step() == 0.0) nv = strtod(t.input.value(), 0); |
| 43 | else nv = strtol(t.input.value(), 0, 0); |
| 44 | if (nv != t.value() || t.when() & FL_WHEN_NOT_CHANGED) { |
| 45 | t.set_value(nv); |
| 46 | t.set_changed(); |
| 47 | if (t.when()) t.do_callback(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void Fl_Value_Input::draw() { |
| 52 | if (damage()&~FL_DAMAGE_CHILD) input.clear_damage(FL_DAMAGE_ALL); |
| 53 | input.box(box()); |
| 54 | input.color(color(), selection_color()); |
| 55 | Fl_Widget *i = &input; i->draw(); // calls protected input.draw() |
| 56 | input.clear_damage(); |
| 57 | } |
| 58 | |
| 59 | void Fl_Value_Input::resize(int X, int Y, int W, int H) { |
| 60 | Fl_Valuator::resize(X, Y, W, H); |
| 61 | input.resize(X, Y, W, H); |
| 62 | } |
| 63 | |
| 64 | void Fl_Value_Input::value_damage() { |
| 65 | char buf[128]; |
| 66 | format(buf); |
| 67 | input.value(buf); |
| 68 | input.mark(input.position()); // turn off selection highlight |
| 69 | } |
| 70 | |
| 71 | int Fl_Value_Input::handle(int event) { |
| 72 | double v; |
| 73 | int delta; |
| 74 | int mx = Fl::event_x_root(); |
| 75 | static int ix, drag; |
| 76 | input.when(when()); |
| 77 | switch (event) { |
| 78 | case FL_PUSH: |
| 79 | if (!step()) goto DEFAULT; |
| 80 | ix = mx; |
| 81 | drag = Fl::event_button(); |
| 82 | handle_push(); |
| 83 | return 1; |
| 84 | case FL_DRAG: |
| 85 | if (!step()) goto DEFAULT; |
| 86 | delta = mx-ix; |
| 87 | if (delta > 5) delta -= 5; |
| 88 | else if (delta < -5) delta += 5; |
| 89 | else delta = 0; |
| 90 | switch (drag) { |
| 91 | case 3: v = increment(previous_value(), delta*100); break; |
| 92 | case 2: v = increment(previous_value(), delta*10); break; |
| 93 | default:v = increment(previous_value(), delta); break; |
| 94 | } |
| 95 | v = round(v); |
| 96 | handle_drag(soft()?softclamp(v):clamp(v));; |
| 97 | return 1; |
| 98 | case FL_RELEASE: |
| 99 | if (!step()) goto DEFAULT; |
| 100 | if (value() != previous_value() || !Fl::event_is_click()) |
| 101 | handle_release(); |
| 102 | else { |
| 103 | Fl_Widget_Tracker wp(&input); |
| 104 | input.handle(FL_PUSH); |
| 105 | if (wp.exists()) |
| 106 | input.handle(FL_RELEASE); |
| 107 | } |
| 108 | return 1; |
| 109 | case FL_FOCUS: |
| 110 | return input.take_focus(); |
| 111 | case FL_SHORTCUT: |
| 112 | return input.handle(event); |
| 113 | default: |
| 114 | DEFAULT: |
| 115 | input.type(((step() - floor(step()))>0.0 || step() == 0.0) ? FL_FLOAT_INPUT : FL_INT_INPUT); |
| 116 | return input.handle(event); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | Creates a new Fl_Value_Input widget using the given |
| 122 | position, size, and label string. The default boxtype is |
| 123 | FL_DOWN_BOX. |
| 124 | */ |
| 125 | Fl_Value_Input::Fl_Value_Input(int X, int Y, int W, int H, const char* l) |
| 126 | : Fl_Valuator(X, Y, W, H, l), input(X, Y, W, H, 0) { |
| 127 | soft_ = 0; |
| 128 | if (input.parent()) // defeat automatic-add |
| 129 | input.parent()->remove(input); |
| 130 | input.parent((Fl_Group *)this); // kludge! |
| 131 | input.callback(input_cb, this); |
| 132 | input.when(FL_WHEN_CHANGED); |
| 133 | box(input.box()); |
| 134 | color(input.color()); |
| 135 | selection_color(input.selection_color()); |
| 136 | align(FL_ALIGN_LEFT); |
| 137 | value_damage(); |
| 138 | set_flag(SHORTCUT_LABEL); |
| 139 | } |
| 140 | |
| 141 | Fl_Value_Input::~Fl_Value_Input() { |
| 142 | |
| 143 | if (input.parent() == (Fl_Group *)this) |
| 144 | input.parent(0); // *revert* ctor kludge! |
| 145 | } |
| 146 | |
| 147 | // |
| 148 | // End of "$Id: Fl_Value_Input.cxx 7903 2010-11-28 21:06:39Z matt $". |
| 149 | // |