diff options
| author | Michael R Sweet <michael.r.sweet@gmail.com> | 1998-10-06 18:21:25 +0000 |
|---|---|---|
| committer | Michael R Sweet <michael.r.sweet@gmail.com> | 1998-10-06 18:21:25 +0000 |
| commit | f9039b2ae21988783feae9b362818e7923e82d14 (patch) | |
| tree | 6d6fe3679d73448758f9794e7d4d4f6b22a4adad /src/forms_timer.cxx | |
| parent | 67e89232f9ba067825a158734a09e0fa21aacbe3 (diff) | |
Initial revision
git-svn-id: file:///fltk/svn/fltk/trunk@2 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src/forms_timer.cxx')
| -rw-r--r-- | src/forms_timer.cxx | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/src/forms_timer.cxx b/src/forms_timer.cxx new file mode 100644 index 000000000..2b7694f80 --- /dev/null +++ b/src/forms_timer.cxx @@ -0,0 +1,127 @@ +// forms_timer.H + +// Emulate the Forms Timer object +// You don't want to use this if you just want a timeout, call +// Fl::add_timeout directly! + +#include <FL/Fl.H> +#include <FL/Fl_Timer.H> +#include <FL/fl_draw.H> +#ifdef WIN32 +# include <sys/types.h> +# include <sys/timeb.h> +#else +# include <sys/time.h> +#endif +#include <stdio.h> + +#define FL_TIMER_BLINKRATE 0.2 + +void fl_gettime(long* sec, long* usec) { +#ifdef WIN32 + struct timeb tp; + ftime(&tp); + *sec = tp.time; + *usec = tp.millitm * 1000; +#else + struct timeval tp; + struct timezone tzp; + gettimeofday(&tp, &tzp); + *sec = tp.tv_sec; + *usec = tp.tv_usec; +#endif +} + +void Fl_Timer::draw() { + int tt; + Fl_Color col; + char str[32]; + if (!on || delay>0.0) + col = color(); + else if ((int) (delay / FL_TIMER_BLINKRATE) % 2) + col = color(); + else + col = selection_color(); + draw_box(box(), col); + if (type() == FL_VALUE_TIMER && delay>0.0) { + double d = direction_ ? total-delay : delay; + if (d < 60.0) + sprintf(str, "%.1f", d); + else { + tt = (int) ((d+0.05) / 60.0); + sprintf(str, "%d:%04.1f", tt, d - 60.0 * tt); + } + fl_font(labelfont(), labelsize()); + fl_color(labelcolor()); + fl_draw(str, x(), y(), w(), h(), FL_ALIGN_CENTER); + } else + draw_label(); +} + +void Fl_Timer::stepcb(void* v) { + ((Fl_Timer*)v)->step(); +} + +void Fl_Timer::step() { + if (!on) return; + double lastdelay = delay; + long sec, usec; fl_gettime(&sec, &usec); + delay -= (double) (sec - lastsec) + (double) (usec - lastusec) / 1000000.0; + lastsec = sec; lastusec = usec; + if (lastdelay > 0.0 && delay <= 0.0) { + if (type() == FL_HIDDEN_TIMER) { + on = 0; + delay = 0; + } else { + redraw(); + Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this); + } + do_callback(); + } else { + if (type() == FL_VALUE_TIMER) redraw(); + Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this); + } +} + +int Fl_Timer::handle(int event) { + if (event == FL_RELEASE && delay <= 0) value(0.0); + return 0; +} + +Fl_Timer::~Fl_Timer() { + Fl::remove_timeout(stepcb, this); +} + +Fl_Timer::Fl_Timer(uchar t, int x, int y, int w, int h, const char* l) +: Fl_Widget(x, y, w, h, l) { + box(FL_DOWN_BOX); + selection_color(FL_RED); + delay = 0; + on = 0; + direction_ = 0; + type(t); + if (t == FL_HIDDEN_TIMER) clear_visible(); + if (t == FL_VALUE_TIMER) align(FL_ALIGN_LEFT); +} + +void Fl_Timer::value(double d) { + delay = total = d; + on = (d > 0.0); + fl_gettime(&(lastsec), &(lastusec)); + if (type() != FL_HIDDEN_TIMER) redraw(); + Fl::remove_timeout(stepcb, this); + if (on) Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this); +} + +void Fl_Timer::suspended(char d) { + if (!d) { + if (on) return; + on = (delay > 0.0); + fl_gettime(&(lastsec), &(lastusec)); + if (on) Fl::add_timeout(FL_TIMER_BLINKRATE, stepcb, this); + } else { + if (!on) return; + on = 0; + Fl::remove_timeout(stepcb, this); + } +} |
