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

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Roller.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Roller 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 // Rapid-App style knob
00029 
00030 #include <FL/Fl.H>
00031 #include <FL/Fl_Roller.H>
00032 #include <FL/fl_draw.H>
00033 #include <math.h>
00034 
00035 int Fl_Roller::handle(int event) {
00036   static int ipos;
00037   int newpos = horizontal() ? Fl::event_x() : Fl::event_y();
00038   switch (event) {
00039   case FL_PUSH:
00040     if (Fl::visible_focus()) {
00041       Fl::focus(this);
00042       redraw();
00043     }
00044     handle_push();
00045     ipos = newpos;
00046     return 1;
00047   case FL_DRAG:
00048     handle_drag(clamp(round(increment(previous_value(),newpos-ipos))));
00049     return 1;
00050   case FL_RELEASE:
00051     handle_release();
00052     return 1;
00053   case FL_KEYBOARD :
00054     switch (Fl::event_key()) {
00055       case FL_Up:
00056         if (horizontal()) return 0;
00057         handle_drag(clamp(increment(value(),-1)));
00058         return 1;
00059       case FL_Down:
00060         if (horizontal()) return 0;
00061         handle_drag(clamp(increment(value(),1)));
00062         return 1;
00063       case FL_Left:
00064         if (!horizontal()) return 0;
00065         handle_drag(clamp(increment(value(),-1)));
00066         return 1;
00067       case FL_Right:
00068         if (!horizontal()) return 0;
00069         handle_drag(clamp(increment(value(),1)));
00070         return 1;
00071       default:
00072         return 0;
00073     }
00074     // break not required because of switch...
00075   case FL_FOCUS :
00076   case FL_UNFOCUS :
00077     if (Fl::visible_focus()) {
00078       redraw();
00079       return 1;
00080     } else return 0;
00081   case FL_ENTER :
00082   case FL_LEAVE :
00083     return 1;
00084   default:
00085     return 0;
00086   }
00087 }
00088 
00089 void Fl_Roller::draw() {
00090   if (damage()&FL_DAMAGE_ALL) draw_box();
00091   int X = x()+Fl::box_dx(box());
00092   int Y = y()+Fl::box_dy(box());
00093   int W = w()-Fl::box_dw(box())-1;
00094   int H = h()-Fl::box_dh(box())-1;
00095   if (W<=0 || H <=0) return;
00096   int offset = step() ? int(value()/step()) : 0;
00097   const double ARC = 1.5; // 1/2 the number of radians visible
00098   const double delta = .2; // radians per knurl
00099   if (horizontal()) { // horizontal one
00100     // draw shaded ends of wheel:
00101     int h1 = W/4+1; // distance from end that shading starts
00102     fl_color(color()); fl_rectf(X+h1,Y,W-2*h1,H);
00103     for (int i=0; h1; i++) {
00104       fl_color((Fl_Color)(FL_GRAY-i-1));
00105       int h2 = FL_GRAY-i-1 > FL_DARK3 ? 2*h1/3+1 : 0;
00106       fl_rectf(X+h2,Y,h1-h2,H);
00107       fl_rectf(X+W-h1,Y,h1-h2,H);
00108       h1 = h2;
00109     }
00110     if (active_r()) {
00111       // draw ridges:
00112       double junk;
00113       for (double yy = -ARC+modf(offset*sin(ARC)/(W/2)/delta,&junk)*delta;;
00114            yy += delta) {
00115         int yy1 = int((sin(yy)/sin(ARC)+1)*W/2);
00116         if (yy1 <= 0) continue; else if (yy1 >= W-1) break;
00117         fl_color(FL_DARK3); fl_yxline(X+yy1,Y+1,Y+H-1);
00118         if (yy < 0) yy1--; else yy1++;
00119         fl_color(FL_LIGHT1);fl_yxline(X+yy1,Y+1,Y+H-1);
00120       }
00121       // draw edges:
00122       h1 = W/8+1; // distance from end the color inverts
00123       fl_color(FL_DARK2);
00124       fl_xyline(X+h1,Y+H-1,X+W-h1);
00125       fl_color(FL_DARK3);
00126       fl_yxline(X,Y+H,Y,X+h1);
00127       fl_xyline(X+W-h1,Y,X+W);
00128       fl_color(FL_LIGHT2);
00129       fl_xyline(X+h1,Y-1,X+W-h1);
00130       fl_yxline(X+W,Y,Y+H,X+W-h1);
00131       fl_xyline(X+h1,Y+H,X);
00132     }
00133   } else { // vertical one
00134     // draw shaded ends of wheel:
00135     int h1 = H/4+1; // distance from end that shading starts
00136     fl_color(color()); fl_rectf(X,Y+h1,W,H-2*h1);
00137     for (int i=0; h1; i++) {
00138       fl_color((Fl_Color)(FL_GRAY-i-1));
00139       int h2 = FL_GRAY-i-1 > FL_DARK3 ? 2*h1/3+1 : 0;
00140       fl_rectf(X,Y+h2,W,h1-h2);
00141       fl_rectf(X,Y+H-h1,W,h1-h2);
00142       h1 = h2;
00143     }
00144     if (active_r()) {
00145       // draw ridges:
00146       double junk;
00147       for (double yy = -ARC+modf(offset*sin(ARC)/(H/2)/delta,&junk)*delta;
00148            ; yy += delta) {
00149         int yy1 = int((sin(yy)/sin(ARC)+1)*H/2);
00150         if (yy1 <= 0) continue; else if (yy1 >= H-1) break;
00151         fl_color(FL_DARK3); fl_xyline(X+1,Y+yy1,X+W-1);
00152         if (yy < 0) yy1--; else yy1++;
00153         fl_color(FL_LIGHT1);fl_xyline(X+1,Y+yy1,X+W-1);
00154       }
00155       // draw edges:
00156       h1 = H/8+1; // distance from end the color inverts
00157       fl_color(FL_DARK2);
00158       fl_yxline(X+W-1,Y+h1,Y+H-h1);
00159       fl_color(FL_DARK3);
00160       fl_xyline(X+W,Y,X,Y+h1);
00161       fl_yxline(X,Y+H-h1,Y+H);
00162       fl_color(FL_LIGHT2);
00163       fl_yxline(X,Y+h1,Y+H-h1);
00164       fl_xyline(X,Y+H,X+W,Y+H-h1);
00165       fl_yxline(X+W,Y+h1,Y);
00166     }
00167   }
00168 
00169   if (Fl::focus() == this) draw_focus(FL_THIN_UP_FRAME, x(), y(), w(), h());
00170 }
00171 
00177 Fl_Roller::Fl_Roller(int X,int Y,int W,int H,const char* L)
00178   : Fl_Valuator(X,Y,W,H,L) {
00179   box(FL_UP_BOX);
00180   step(1,1000);
00181 }
00182 
00183 //
00184 // End of "$Id: Fl_Roller.cxx 7903 2010-11-28 21:06:39Z matt $".
00185 //