fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_Value_Input.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Value_Input.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Value input widget for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 // FLTK widget for drag-adjusting a floating point value.
00029 // Warning: this works by making a child Fl_Input object, even
00030 // though this object is *not* an Fl_Group.  May be a kludge?
00031 
00032 #include <FL/Fl.H>
00033 #include <FL/Fl_Value_Input.H>
00034 #include <FL/Fl_Group.H>
00035 #include <stdlib.h>
00036 #include <FL/math.h>
00037 
00038 
00039 void Fl_Value_Input::input_cb(Fl_Widget*, void* v) {
00040   Fl_Value_Input& t = *(Fl_Value_Input*)v;
00041   double nv;
00042   if ((t.step() - floor(t.step()))>0.0 || t.step() == 0.0) nv = strtod(t.input.value(), 0);
00043   else nv = strtol(t.input.value(), 0, 0);
00044   if (nv != t.value() || t.when() & FL_WHEN_NOT_CHANGED) {
00045     t.set_value(nv);
00046     t.set_changed();
00047     if (t.when()) t.do_callback();
00048   }
00049 }
00050 
00051 void Fl_Value_Input::draw() {
00052   if (damage()&~FL_DAMAGE_CHILD) input.clear_damage(FL_DAMAGE_ALL);
00053   input.box(box());
00054   input.color(color(), selection_color());
00055   Fl_Widget *i = &input; i->draw(); // calls protected input.draw()
00056   input.clear_damage();
00057 }
00058 
00059 void Fl_Value_Input::resize(int X, int Y, int W, int H) {
00060   Fl_Valuator::resize(X, Y, W, H);
00061   input.resize(X, Y, W, H);
00062 }
00063 
00064 void Fl_Value_Input::value_damage() {
00065   char buf[128];
00066   format(buf);
00067   input.value(buf);
00068   input.mark(input.position()); // turn off selection highlight
00069 }
00070 
00071 int Fl_Value_Input::handle(int event) {
00072   double v;
00073   int delta;
00074   int mx = Fl::event_x_root();
00075   static int ix, drag;
00076   input.when(when());
00077   switch (event) {
00078   case FL_PUSH:
00079     if (!step()) goto DEFAULT;
00080     ix = mx;
00081     drag = Fl::event_button();
00082     handle_push();
00083     return 1;
00084   case FL_DRAG:
00085     if (!step()) goto DEFAULT;
00086     delta = mx-ix;
00087     if (delta > 5) delta -= 5;
00088     else if (delta < -5) delta += 5;
00089     else delta = 0;
00090     switch (drag) {
00091     case 3: v = increment(previous_value(), delta*100); break;
00092     case 2: v = increment(previous_value(), delta*10); break;
00093     default:v = increment(previous_value(), delta); break;
00094     }
00095     v = round(v);
00096     handle_drag(soft()?softclamp(v):clamp(v));;
00097     return 1;
00098   case FL_RELEASE:
00099     if (!step()) goto DEFAULT;
00100     if (value() != previous_value() || !Fl::event_is_click())
00101       handle_release();
00102     else {
00103       Fl_Widget_Tracker wp(&input);
00104       input.handle(FL_PUSH);
00105       if (wp.exists())
00106         input.handle(FL_RELEASE);
00107     }
00108     return 1;
00109   case FL_FOCUS:
00110     return input.take_focus();
00111   case FL_SHORTCUT:
00112     return input.handle(event);
00113   default:
00114   DEFAULT:
00115     input.type(((step() - floor(step()))>0.0 || step() == 0.0) ? FL_FLOAT_INPUT : FL_INT_INPUT);
00116     return input.handle(event);
00117   }
00118 }
00119 
00125 Fl_Value_Input::Fl_Value_Input(int X, int Y, int W, int H, const char* l)
00126 : Fl_Valuator(X, Y, W, H, l), input(X, Y, W, H, 0) {
00127   soft_ = 0;
00128   if (input.parent())  // defeat automatic-add
00129     input.parent()->remove(input);
00130   input.parent((Fl_Group *)this); // kludge!
00131   input.callback(input_cb, this);
00132   input.when(FL_WHEN_CHANGED);
00133   box(input.box());
00134   color(input.color());
00135   selection_color(input.selection_color());
00136   align(FL_ALIGN_LEFT);
00137   value_damage();
00138   set_flag(SHORTCUT_LABEL);
00139 }
00140 
00141 Fl_Value_Input::~Fl_Value_Input() {
00142 
00143   if (input.parent() == (Fl_Group *)this)
00144     input.parent(0);   // *revert* ctor kludge!
00145 }
00146 
00147 //
00148 // End of "$Id: Fl_Value_Input.cxx 7903 2010-11-28 21:06:39Z matt $".
00149 //