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_Adjuster.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Adjuster.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Adjuster 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 #include <FL/Fl.H>
00029 #include <FL/Fl_Adjuster.H>
00030 #include <FL/Fl_Bitmap.H>
00031 #include <FL/fl_draw.H>
00032 
00033 #include "fastarrow.h"
00034 static Fl_Bitmap fastarrow(fastarrow_bits, fastarrow_width, fastarrow_height);
00035 #include "mediumarrow.h"
00036 static Fl_Bitmap mediumarrow(mediumarrow_bits, mediumarrow_width, mediumarrow_height);
00037 #include "slowarrow.h"
00038 static Fl_Bitmap slowarrow(slowarrow_bits, slowarrow_width, slowarrow_height);
00039 
00040 // changing the value does not change the appearance:
00041 void Fl_Adjuster::value_damage() {}
00042 
00043 void Fl_Adjuster::draw() {
00044   int dx, dy, W, H;
00045   if (w()>=h()) {
00046     dx = W = w()/3;
00047     dy = 0; H = h();
00048   } else {
00049     dx = 0; W = w();
00050     dy = H = h()/3;
00051   }
00052   draw_box(drag==1?FL_DOWN_BOX:box(), x(),  y()+2*dy, W, H, color());
00053   draw_box(drag==2?FL_DOWN_BOX:box(), x()+dx, y()+dy, W, H, color());
00054   draw_box(drag==3?FL_DOWN_BOX:box(), x()+2*dx,  y(), W, H, color());
00055   if (active_r())
00056     fl_color(selection_color());
00057   else
00058     fl_color(fl_inactive(selection_color()));
00059   fastarrow.draw(x()+(W-fastarrow_width)/2,
00060                  y()+2*dy+(H-fastarrow_height)/2, W, H);
00061   mediumarrow.draw(x()+dx+(W-mediumarrow_width)/2,
00062                    y()+dy+(H-mediumarrow_height)/2, W, H);
00063   slowarrow.draw(x()+2*dx+(W-slowarrow_width)/2,
00064                  y()+(H-slowarrow_width)/2, W, H);
00065   if (Fl::focus() == this) draw_focus();
00066 }
00067 
00068 int Fl_Adjuster::handle(int event) {
00069   double v;
00070   int delta;
00071   int mx = Fl::event_x();
00072   // Fl_Widget_Tracker wp(this);
00073   switch (event) {
00074     case FL_PUSH:
00075       if (Fl::visible_focus()) Fl::focus(this);
00076       ix = mx;
00077       if (w()>=h())
00078         drag = 3*(mx-x())/w() + 1;
00079       else
00080         drag = 3-3*(Fl::event_y()-y()-1)/h();
00081       { Fl_Widget_Tracker wp(this);
00082         handle_push();
00083         if (wp.deleted()) return 1;
00084       }
00085       redraw();
00086       return 1;
00087     case FL_DRAG:
00088       if (w() >= h()) {
00089         delta = x()+(drag-1)*w()/3;     // left edge of button
00090         if (mx < delta)
00091           delta = mx-delta;
00092         else if (mx > (delta+w()/3)) // right edge of button
00093           delta = mx-delta-w()/3;
00094         else
00095           delta = 0;
00096       } else {
00097         if (mx < x())
00098           delta = mx-x();
00099         else if (mx > (x()+w()))
00100           delta = mx-x()-w();
00101         else
00102           delta = 0;
00103       }
00104       switch (drag) {
00105         case 3: v = increment(previous_value(), delta); break;
00106         case 2: v = increment(previous_value(), delta*10); break;
00107         default:v = increment(previous_value(), delta*100); break;
00108       }
00109       handle_drag(soft() ? softclamp(v) : clamp(v));
00110       return 1;
00111     case FL_RELEASE:
00112       if (Fl::event_is_click()) { // detect click but no drag
00113         if (Fl::event_state()&0xF0000) delta = -10;
00114         else delta = 10;
00115         switch (drag) {
00116           case 3: v = increment(previous_value(), delta); break;
00117           case 2: v = increment(previous_value(), delta*10); break;
00118           default:v = increment(previous_value(), delta*100); break;
00119         }
00120         Fl_Widget_Tracker wp(this);
00121         handle_drag(soft() ? softclamp(v) : clamp(v));
00122         if (wp.deleted()) return 1;
00123       }
00124       drag = 0;
00125       redraw();
00126       handle_release();
00127       return 1;
00128     case FL_KEYBOARD :
00129       switch (Fl::event_key()) {
00130         case FL_Up:
00131           if (w() > h()) return 0;
00132           handle_drag(clamp(increment(value(),-1)));
00133           return 1;
00134         case FL_Down:
00135           if (w() > h()) return 0;
00136           handle_drag(clamp(increment(value(),1)));
00137           return 1;
00138         case FL_Left:
00139           if (w() < h()) return 0;
00140           handle_drag(clamp(increment(value(),-1)));
00141           return 1;
00142         case FL_Right:
00143           if (w() < h()) return 0;
00144           handle_drag(clamp(increment(value(),1)));
00145           return 1;
00146         default:
00147           return 0;
00148       }
00149       // break not required because of switch...
00150 
00151     case FL_FOCUS:
00152     case FL_UNFOCUS:
00153       if (Fl::visible_focus()) {
00154         redraw();
00155         return 1;
00156       } else return 0;
00157 
00158     case FL_ENTER :
00159     case FL_LEAVE :
00160       return 1;
00161   }
00162   return 0;
00163 }
00164 
00171 Fl_Adjuster::Fl_Adjuster(int X, int Y, int W, int H, const char* l)
00172   : Fl_Valuator(X, Y, W, H, l) {
00173   box(FL_UP_BOX);
00174   step(1, 10000);
00175   selection_color(FL_SELECTION_COLOR);
00176   drag = 0;
00177   soft_ = 1;
00178 }
00179 
00180 //
00181 // End of "$Id: Fl_Adjuster.cxx 7903 2010-11-28 21:06:39Z matt $".
00182 //