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

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Positioner.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Positioner 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 
00029 // The positioner widget from Forms, gives 2D input
00030 // Written by: Mark Overmars
00031 
00032 #include <FL/Fl.H>
00033 #include <FL/Fl_Positioner.H>
00034 #include <FL/fl_draw.H>
00035 
00036 static double flinear(double val, double smin, double smax, double gmin, double gmax)
00037 {
00038   if (smin == smax) return gmax;
00039   else return gmin + (gmax - gmin) * (val - smin) / (smax - smin);
00040 }
00041 
00042 void Fl_Positioner::draw(int X, int Y, int W, int H) {
00043   int x1 = X + 4;
00044   int y1 = Y + 4;
00045   int w1 = W - 2 * 4;
00046   int h1 = H - 2 * 4;
00047   int xx = int(flinear(xvalue(), xmin, xmax, x1, x1+w1-1)+.5);
00048   int yy = int(flinear(yvalue(), ymin, ymax, y1, y1+h1-1)+.5);
00049   draw_box(box(), X, Y, W, H, color());
00050   fl_color(selection_color());
00051   fl_xyline(x1, yy, x1+w1);
00052   fl_yxline(xx, y1, y1+h1);
00053 }
00054 
00055 void Fl_Positioner::draw() {
00056   draw(x(), y(), w(), h());
00057   draw_label();
00058 }
00059 
00061 int Fl_Positioner::value(double X, double Y) {
00062   clear_changed();
00063   if (X == xvalue_ && Y == yvalue_) return 0;
00064   xvalue_ = X; yvalue_ = Y;
00065   redraw();
00066   return 1;
00067 }
00068 
00070 int Fl_Positioner::xvalue(double X) {
00071   return(value(X, yvalue_));
00072 }
00073 
00075 int Fl_Positioner::yvalue(double Y) {
00076   return(value(xvalue_, Y));
00077 }
00078 
00079 int Fl_Positioner::handle(int event, int X, int Y, int W, int H) {
00080   switch (event) {
00081   case FL_PUSH:
00082   case FL_DRAG:
00083   case FL_RELEASE: {
00084     double x1 = X + 4;
00085     double y1 = Y + 4;
00086     double w1 = W - 2 * 4;
00087     double h1 = H - 2 * 4;
00088     double xx = flinear(Fl::event_x(), x1, x1+w1-1.0, xmin, xmax);
00089     if (xstep_) xx = int(xx/xstep_+0.5) * xstep_;
00090     if (xmin < xmax) {
00091       if (xx < xmin) xx = xmin;
00092       if (xx > xmax) xx = xmax;
00093     } else {
00094       if (xx > xmin) xx = xmin;
00095       if (xx < xmax) xx = xmax;
00096     }
00097     double yy = flinear(Fl::event_y(), y1, y1+h1-1.0, ymin, ymax);
00098     if (ystep_) yy = int(yy/ystep_+0.5) * ystep_;
00099     if (ymin < ymax) {
00100       if (yy < ymin) yy = ymin;
00101       if (yy > ymax) yy = ymax;
00102     } else {
00103       if (yy > ymin) yy = ymin;
00104       if (yy < ymax) yy = ymax;
00105     }
00106     if (xx != xvalue_ || yy != yvalue_) {
00107       xvalue_ = xx; yvalue_ = yy;
00108       set_changed();
00109       redraw();
00110                    } }
00111     if (!(when() & FL_WHEN_CHANGED ||
00112           (when() & FL_WHEN_RELEASE && event == FL_RELEASE))) return 1;
00113     if (changed() || when()&FL_WHEN_NOT_CHANGED) {
00114       if (event == FL_RELEASE) clear_changed();
00115       do_callback();
00116     }
00117     return 1;
00118   default:
00119     return 0;
00120   }
00121 }
00122 
00123 int Fl_Positioner::handle(int e) {
00124   return handle(e, x(), y(), w(), h());
00125 }
00126 
00131 Fl_Positioner::Fl_Positioner(int X, int Y, int W, int H, const char* l)
00132 : Fl_Widget(X, Y, W, H, l) {
00133   box(FL_DOWN_BOX);
00134   selection_color(FL_RED);
00135   align(FL_ALIGN_BOTTOM);
00136   when(FL_WHEN_CHANGED);
00137   xmin = ymin = 0;
00138   xmax = ymax = 1;
00139   xvalue_ = yvalue_ = .5;
00140   xstep_ = ystep_ = 0;
00141 }
00142 
00144 void Fl_Positioner::xbounds(double a, double b) {
00145   if (a != xmin || b != xmax) {
00146     xmin = a; xmax = b;
00147     redraw();
00148   }
00149 }
00150 
00152 void Fl_Positioner::ybounds(double a, double b) {
00153   if (a != ymin || b != ymax) {
00154     ymin = a; ymax = b;
00155     redraw();
00156   }
00157 }
00158 
00159 //
00160 // End of "$Id: Fl_Positioner.cxx 7903 2010-11-28 21:06:39Z matt $".
00161 //