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

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Counter.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Counter 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_Counter.H>
00030 #include <FL/fl_draw.H>
00031 
00032 void Fl_Counter::draw() {
00033   int i; Fl_Boxtype boxtype[5];
00034   Fl_Color selcolor;
00035 
00036   boxtype[0] = box();
00037   if (boxtype[0] == FL_UP_BOX) boxtype[0] = FL_DOWN_BOX;
00038   if (boxtype[0] == FL_THIN_UP_BOX) boxtype[0] = FL_THIN_DOWN_BOX;
00039   for (i=1; i<5; i++)
00040     if (mouseobj == i)
00041       boxtype[i] = fl_down(box());
00042     else
00043       boxtype[i] = box();
00044 
00045   int xx[5], ww[5];
00046   if (type() == FL_NORMAL_COUNTER) {
00047     int W = w()*15/100;
00048     xx[1] = x();         ww[1] = W;
00049     xx[2] = x()+1*W;     ww[2] = W;
00050     xx[0] = x()+2*W;     ww[0] = w()-4*W;
00051     xx[3] = x()+w()-2*W; ww[3] = W;
00052     xx[4] = x()+w()-1*W; ww[4] = W;
00053   } else {
00054     int W = w()*20/100;
00055     xx[1] = 0;           ww[1] = 0;
00056     xx[2] = x();         ww[2] = W;
00057     xx[0] = x()+W;       ww[0] = w()-2*W;
00058     xx[3] = x()+w()-1*W; ww[3] = W;
00059     xx[4] = 0;           ww[4] = 0;
00060   }
00061 
00062   draw_box(boxtype[0], xx[0], y(), ww[0], h(), FL_BACKGROUND2_COLOR);
00063   fl_font(textfont(), textsize());
00064   fl_color(active_r() ? textcolor() : fl_inactive(textcolor()));
00065   char str[128]; format(str);
00066   fl_draw(str, xx[0], y(), ww[0], h(), FL_ALIGN_CENTER);
00067   if (Fl::focus() == this) draw_focus(boxtype[0], xx[0], y(), ww[0], h());
00068   if (!(damage()&FL_DAMAGE_ALL)) return; // only need to redraw text
00069 
00070   if (active_r())
00071     selcolor = labelcolor();
00072   else
00073     selcolor = fl_inactive(labelcolor());
00074 
00075   if (type() == FL_NORMAL_COUNTER) {
00076     draw_box(boxtype[1], xx[1], y(), ww[1], h(), color());
00077     fl_draw_symbol("@-4<<", xx[1], y(), ww[1], h(), selcolor);
00078   }
00079   draw_box(boxtype[2], xx[2], y(), ww[2], h(), color());
00080   fl_draw_symbol("@-4<",  xx[2], y(), ww[2], h(), selcolor);
00081   draw_box(boxtype[3], xx[3], y(), ww[3], h(), color());
00082   fl_draw_symbol("@-4>",  xx[3], y(), ww[3], h(), selcolor);
00083   if (type() == FL_NORMAL_COUNTER) {
00084     draw_box(boxtype[4], xx[4], y(), ww[4], h(), color());
00085     fl_draw_symbol("@-4>>", xx[4], y(), ww[4], h(), selcolor);
00086   }
00087 }
00088 
00089 void Fl_Counter::increment_cb() {
00090   if (!mouseobj) return;
00091   double v = value();
00092   switch (mouseobj) {
00093   case 1: v -= lstep_; break;
00094   case 2: v = increment(v, -1); break;
00095   case 3: v = increment(v, 1); break;
00096   case 4: v += lstep_; break;
00097   }
00098   handle_drag(clamp(round(v)));
00099 }
00100 
00101 #define INITIALREPEAT .5
00102 #define REPEAT .1
00103 
00104 void Fl_Counter::repeat_callback(void* v) {
00105   Fl_Counter* b = (Fl_Counter*)v;
00106   if (b->mouseobj) {
00107     Fl::add_timeout(REPEAT, repeat_callback, b);
00108     b->increment_cb();
00109   }
00110 }
00111 
00112 int Fl_Counter::calc_mouseobj() {
00113   if (type() == FL_NORMAL_COUNTER) {
00114     int W = w()*15/100;
00115     if (Fl::event_inside(x(), y(), W, h())) return 1;
00116     if (Fl::event_inside(x()+W, y(), W, h())) return 2;
00117     if (Fl::event_inside(x()+w()-2*W, y(), W, h())) return 3;
00118     if (Fl::event_inside(x()+w()-W, y(), W, h())) return 4;
00119   } else {
00120     int W = w()*20/100;
00121     if (Fl::event_inside(x(), y(), W, h())) return 2;
00122     if (Fl::event_inside(x()+w()-W, y(), W, h())) return 3;
00123   }
00124   return -1;
00125 }
00126 
00127 int Fl_Counter::handle(int event) {
00128   int i;
00129   switch (event) {
00130   case FL_RELEASE:
00131     if (mouseobj) {
00132       Fl::remove_timeout(repeat_callback, this);
00133       mouseobj = 0;
00134       redraw();
00135     }
00136     handle_release();
00137     return 1;
00138   case FL_PUSH:
00139     if (Fl::visible_focus()) Fl::focus(this);
00140     { Fl_Widget_Tracker wp(this);
00141       handle_push();
00142       if (wp.deleted()) return 1;
00143     }
00144   case FL_DRAG:
00145     i = calc_mouseobj();
00146     if (i != mouseobj) {
00147       Fl::remove_timeout(repeat_callback, this);
00148       mouseobj = (uchar)i;
00149       if (i) Fl::add_timeout(INITIALREPEAT, repeat_callback, this);
00150       Fl_Widget_Tracker wp(this);
00151       increment_cb();
00152       if (wp.deleted()) return 1;
00153       redraw();
00154     }
00155     return 1;
00156   case FL_KEYBOARD :
00157     switch (Fl::event_key()) {
00158       case FL_Left:
00159         handle_drag(clamp(increment(value(),-1)));
00160         return 1;
00161       case FL_Right:
00162         handle_drag(clamp(increment(value(),1)));
00163         return 1;
00164       default:
00165         return 0;
00166     }
00167     // break not required because of switch...
00168   case FL_FOCUS : /* FALLTHROUGH */
00169   case FL_UNFOCUS :
00170     if (Fl::visible_focus()) {
00171       redraw();
00172       return 1;
00173     } else return 0;
00174   case FL_ENTER : /* FALLTHROUGH */
00175   case FL_LEAVE :
00176     return 1;
00177   default:
00178     return 0;
00179   }
00180 }
00181 
00185 Fl_Counter::~Fl_Counter() {
00186   Fl::remove_timeout(repeat_callback, this);
00187 }
00188 
00195 Fl_Counter::Fl_Counter(int X, int Y, int W, int H, const char* L)
00196   : Fl_Valuator(X, Y, W, H, L) {
00197   box(FL_UP_BOX);
00198   selection_color(FL_INACTIVE_COLOR); // was FL_BLUE
00199   align(FL_ALIGN_BOTTOM);
00200   bounds(-1000000.0, 1000000.0);
00201   Fl_Valuator::step(1, 10);
00202   lstep_ = 1.0;
00203   mouseobj = 0;
00204   textfont_ = FL_HELVETICA;
00205   textsize_ = FL_NORMAL_SIZE;
00206   textcolor_ = FL_FOREGROUND_COLOR;
00207 }
00208 
00209 //
00210 // End of "$Id: Fl_Counter.cxx 7903 2010-11-28 21:06:39Z matt $".
00211 //