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)  

forms_timer.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: forms_timer.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Forms timer object 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 // Emulate the Forms Timer object
00029 // You don't want to use this if you just want a timeout, call
00030 // Fl::add_timeout directly!
00031 
00032 #include <FL/Fl.H>
00033 #include <FL/Fl_Timer.H>
00034 #include <FL/fl_draw.H>
00035 #ifdef WIN32
00036 #  ifdef __MWERKS__
00037 #    include <time.h>
00038 #  else
00039 #    include <sys/types.h> 
00040 #    include <sys/timeb.h>
00041 #  endif
00042 #else
00043 #  include <time.h>
00044 #  include <sys/time.h>
00045 #endif
00046 #include <stdio.h>
00047 
00048 #define FL_TIMER_BLINKRATE      0.2
00049 
00050 void fl_gettime(long* sec, long* usec) {
00051 #ifdef WIN32
00052 # ifdef __MWERKS__
00053   time_t localTime = time(NULL);
00054   struct tm *now = localtime(&localTime);
00055   *sec = now->tm_sec + 60*now->tm_min + 3600*now->tm_hour + 24*3600*now->tm_yday;
00056   *usec = 0;
00057 # else
00058   struct timeb tp;
00059   ftime(&tp);
00060   *sec = tp.time;
00061   *usec = tp.millitm * 1000;
00062 # endif
00063 #else
00064   struct timeval tp;
00065   struct timezone tzp;
00066   gettimeofday(&tp, &tzp);
00067   *sec = tp.tv_sec;
00068   *usec = tp.tv_usec;
00069 #endif
00070 }
00071 
00072 void Fl_Timer::draw() {
00073   int tt;
00074   Fl_Color col;
00075   char str[32];
00076   if (!on || delay>0.0)
00077     col = color();
00078   else if ((int) (delay / FL_TIMER_BLINKRATE) % 2)
00079     col = color();
00080   else
00081     col = selection_color();
00082   draw_box(box(), col);
00083   if (type() == FL_VALUE_TIMER && delay>0.0) {
00084     double d = direction_ ? total-delay : delay;
00085     if (d < 60.0)
00086       sprintf(str, "%.1f", d);
00087     else {
00088       tt = (int) ((d+0.05) / 60.0);
00089       sprintf(str, "%d:%04.1f", tt, d - 60.0 * tt);
00090     }
00091     fl_font(labelfont(), labelsize());
00092     fl_color(labelcolor());
00093     fl_draw(str, x(), y(), w(), h(), FL_ALIGN_CENTER);
00094   } else
00095     draw_label();
00096 }
00097 
00098 void Fl_Timer::stepcb(void* v) {
00099   ((Fl_Timer*)v)->step();
00100 }
00101 
00102 void Fl_Timer::step() {
00103   if (!on) return;
00104   double lastdelay = delay;
00105   long sec, usec; fl_gettime(&sec, &usec);
00106   delay -= (double) (sec - lastsec) + (double) (usec - lastusec) / 1000000.0;
00107   lastsec = sec; lastusec = usec;
00108   if (lastdelay > 0.0 && delay <= 0.0) {
00109     if (type() == FL_HIDDEN_TIMER) {
00110       on = 0;
00111       delay = 0;
00112     } else {
00113       redraw();
00114       Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this);
00115     }
00116     set_changed();
00117     do_callback();
00118   } else {
00119     if (type() == FL_VALUE_TIMER) redraw();
00120     Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this);
00121   }
00122 }
00123 
00124 int Fl_Timer::handle(int event) {
00125   if (event == FL_RELEASE && delay <= 0) value(0.0);
00126   return 0;
00127 }
00128 
00132 Fl_Timer::~Fl_Timer() {
00133   Fl::remove_timeout(stepcb, this);
00134 }
00135 
00148 Fl_Timer::Fl_Timer(uchar t, int X, int Y, int W, int H, const char* l)
00149 
00150 : Fl_Widget(X, Y, W, H, l) {
00151   box(FL_DOWN_BOX);
00152   selection_color(FL_RED);
00153   delay = 0;
00154   on = 0;
00155   direction_ = 0;
00156   type(t);
00157   if (t == FL_HIDDEN_TIMER) clear_visible();
00158   if (t == FL_VALUE_TIMER) align(FL_ALIGN_LEFT);
00159 }
00161 void Fl_Timer::value(double d) {
00162   delay = total = d;
00163   on = (d > 0.0);
00164   fl_gettime(&(lastsec), &(lastusec));
00165   if (type() != FL_HIDDEN_TIMER) redraw();
00166   Fl::remove_timeout(stepcb, this);
00167   if (on) Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this);
00168 }
00169 
00171 void Fl_Timer::suspended(char d) {
00172   if (!d) {
00173     if (on) return;
00174     on = (delay > 0.0);
00175     fl_gettime(&(lastsec), &(lastusec));
00176     if (on) Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this);
00177   } else {
00178     if (!on) return;
00179     on = 0;
00180     Fl::remove_timeout(stepcb, this);
00181   }
00182 }
00183 
00184 //
00185 // End of "$Id: forms_timer.cxx 7903 2010-11-28 21:06:39Z matt $".
00186 //