1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// Clock widget for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 1998-2010 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/COPYING.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Clock.H>
|
2012-07-24 08:37:22 +04:00
|
|
|
#include <FL/Fl_Round_Clock.H>
|
1998-10-20 00:46:58 +04:00
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
1999-02-01 23:27:16 +03:00
|
|
|
#ifndef WIN32
|
1999-01-13 22:43:13 +03:00
|
|
|
# include <sys/time.h>
|
1999-02-01 23:27:16 +03:00
|
|
|
#endif /* !WIN32 */
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
// Original clock display written by Paul Haeberli at SGI.
|
|
|
|
// Modifications by Mark Overmars for Forms
|
|
|
|
// Further changes by Bill Spitzak for fltk
|
|
|
|
|
|
|
|
const float hourhand[4][2] = {{-0.5f, 0}, {0, 1.5f}, {0.5f, 0}, {0, -7.0f}};
|
|
|
|
const float minhand[4][2] = {{-0.5f, 0}, {0, 1.5f}, {0.5f, 0}, {0, -11.5f}};
|
|
|
|
const float sechand[4][2] = {{-0.1f, 0}, {0, 2.0f}, {0.1f, 0}, {0, -11.5f}};
|
|
|
|
|
|
|
|
static void drawhand(double ang,const float v[][2],Fl_Color fill,Fl_Color line)
|
|
|
|
{
|
|
|
|
fl_push_matrix();
|
|
|
|
fl_rotate(ang);
|
|
|
|
fl_color(fill); fl_begin_polygon();
|
|
|
|
int i; for (i=0; i<4; i++) fl_vertex(v[i][0],v[i][1]); fl_end_polygon();
|
|
|
|
fl_color(line); fl_begin_loop();
|
|
|
|
for (i=0; i<4; i++) fl_vertex(v[i][0],v[i][1]); fl_end_loop();
|
|
|
|
fl_pop_matrix();
|
|
|
|
}
|
|
|
|
|
1999-01-13 18:45:50 +03:00
|
|
|
void Fl_Clock_Output::drawhands(Fl_Color fill, Fl_Color line) {
|
2006-09-20 07:03:14 +04:00
|
|
|
if (!active_r()) {
|
|
|
|
fill = fl_inactive(fill);
|
|
|
|
line = fl_inactive(line);
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
drawhand(-360*(hour()+minute()/60.0)/12, hourhand, fill, line);
|
|
|
|
drawhand(-360*(minute()+second()/60.0)/60, minhand, fill, line);
|
|
|
|
drawhand(-360*(second()/60.0), sechand, fill, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rect(double x, double y, double w, double h) {
|
|
|
|
double r = x+w;
|
|
|
|
double t = y+h;
|
|
|
|
fl_begin_polygon();
|
|
|
|
fl_vertex(x, y);
|
|
|
|
fl_vertex(r, y);
|
|
|
|
fl_vertex(r, t);
|
|
|
|
fl_vertex(x, t);
|
|
|
|
fl_end_polygon();
|
|
|
|
}
|
|
|
|
|
2008-09-21 12:00:32 +04:00
|
|
|
/**
|
|
|
|
Draw clock with the given position and size.
|
|
|
|
\param[in] X, Y, W, H position and size
|
|
|
|
*/
|
2002-08-09 05:09:49 +04:00
|
|
|
void Fl_Clock_Output::draw(int X, int Y, int W, int H) {
|
2005-11-02 14:04:26 +03:00
|
|
|
Fl_Color box_color = type()==FL_ROUND_CLOCK ? FL_GRAY : color();
|
|
|
|
Fl_Color shadow_color = fl_color_average(box_color, FL_BLACK, 0.5);
|
|
|
|
draw_box(box(), X, Y, W, H, box_color);
|
1998-10-06 22:21:25 +04:00
|
|
|
fl_push_matrix();
|
2002-08-09 05:09:49 +04:00
|
|
|
fl_translate(X+W/2.0-.5, Y+H/2.0-.5);
|
|
|
|
fl_scale((W-1)/28.0, (H-1)/28.0);
|
1998-10-06 22:21:25 +04:00
|
|
|
if (type() == FL_ROUND_CLOCK) {
|
2006-09-20 07:03:14 +04:00
|
|
|
fl_color(active_r() ? color() : fl_inactive(color()));
|
1998-10-06 22:21:25 +04:00
|
|
|
fl_begin_polygon(); fl_circle(0,0,14); fl_end_polygon();
|
2006-09-20 07:03:14 +04:00
|
|
|
fl_color(active_r() ? FL_FOREGROUND_COLOR : fl_inactive(FL_FOREGROUND_COLOR));
|
1998-10-06 22:21:25 +04:00
|
|
|
fl_begin_loop(); fl_circle(0,0,14); fl_end_loop();
|
|
|
|
}
|
|
|
|
// draw the shadows:
|
|
|
|
fl_push_matrix();
|
|
|
|
fl_translate(0.60, 0.60);
|
2005-11-02 14:04:26 +03:00
|
|
|
drawhands(shadow_color, shadow_color);
|
1998-10-06 22:21:25 +04:00
|
|
|
fl_pop_matrix();
|
|
|
|
// draw the tick marks:
|
|
|
|
fl_push_matrix();
|
2006-09-20 07:03:14 +04:00
|
|
|
fl_color(active_r() ? FL_FOREGROUND_COLOR : fl_inactive(FL_FOREGROUND_COLOR));
|
1998-10-06 22:21:25 +04:00
|
|
|
for (int i=0; i<12; i++) {
|
|
|
|
if (i==6) rect(-0.5, 9, 1, 2);
|
|
|
|
else if (i==3 || i==0 || i== 9) rect(-0.5, 9.5, 1, 1);
|
|
|
|
else rect(-0.25, 9.5, .5, 1);
|
|
|
|
fl_rotate(-30);
|
|
|
|
}
|
|
|
|
fl_pop_matrix();
|
|
|
|
// draw the hands:
|
2005-11-02 14:04:26 +03:00
|
|
|
drawhands(selection_color(), FL_FOREGROUND_COLOR); // color was 54
|
1998-10-06 22:21:25 +04:00
|
|
|
fl_pop_matrix();
|
|
|
|
}
|
|
|
|
|
2008-09-21 12:00:32 +04:00
|
|
|
/**
|
|
|
|
Draw clock with current position and size.
|
|
|
|
*/
|
1999-01-13 18:45:50 +03:00
|
|
|
void Fl_Clock_Output::draw() {
|
1998-10-06 22:21:25 +04:00
|
|
|
draw(x(), y(), w(), h());
|
|
|
|
draw_label();
|
|
|
|
}
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
|
|
|
Set the displayed time.
|
|
|
|
Set the time in hours, minutes, and seconds.
|
|
|
|
\param[in] H, m, s displayed time
|
|
|
|
\see hour(), minute(), second()
|
|
|
|
*/
|
2002-08-09 05:09:49 +04:00
|
|
|
void Fl_Clock_Output::value(int H, int m, int s) {
|
|
|
|
if (H!=hour_ || m!=minute_ || s!=second_) {
|
|
|
|
hour_ = H; minute_ = m; second_ = s;
|
2005-03-13 02:18:48 +03:00
|
|
|
value_ = (H * 60 + m) * 60 + s;
|
1999-01-13 18:45:50 +03:00
|
|
|
damage(FL_DAMAGE_CHILD);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
|
|
|
Set the displayed time.
|
|
|
|
Set the time in seconds since the UNIX epoch (January 1, 1970).
|
|
|
|
\param[in] v seconds since epoch
|
|
|
|
\see value()
|
|
|
|
*/
|
1999-01-13 18:45:50 +03:00
|
|
|
void Fl_Clock_Output::value(ulong v) {
|
2005-03-13 02:18:48 +03:00
|
|
|
value_ = v;
|
1998-10-06 22:21:25 +04:00
|
|
|
struct tm *timeofday;
|
2005-12-14 03:34:34 +03:00
|
|
|
// Some platforms, notably Windows, now use a 64-bit time_t value...
|
|
|
|
time_t vv = (time_t)v;
|
|
|
|
timeofday = localtime(&vv);
|
1998-10-06 22:21:25 +04:00
|
|
|
value(timeofday->tm_hour, timeofday->tm_min, timeofday->tm_sec);
|
|
|
|
}
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
|
|
|
Create a new Fl_Clock_Output widget with the given position, size and label.
|
|
|
|
The default boxtype is \c FL_NO_BOX.
|
|
|
|
\param[in] X, Y, W, H position and size of the widget
|
|
|
|
\param[in] L widget label, default is no label
|
|
|
|
*/
|
|
|
|
Fl_Clock_Output::Fl_Clock_Output(int X, int Y, int W, int H, const char *L)
|
|
|
|
: Fl_Widget(X, Y, W, H, L) {
|
1999-01-13 18:45:50 +03:00
|
|
|
box(FL_UP_BOX);
|
1998-10-06 22:21:25 +04:00
|
|
|
selection_color(fl_gray_ramp(5));
|
|
|
|
align(FL_ALIGN_BOTTOM);
|
1999-01-13 18:45:50 +03:00
|
|
|
hour_ = 0;
|
|
|
|
minute_ = 0;
|
|
|
|
second_ = 0;
|
|
|
|
value_ = 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
1999-01-13 18:45:50 +03:00
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
|
|
|
Create an Fl_Clock widget using the given position, size, and label string.
|
|
|
|
The default boxtype is \c FL_NO_BOX.
|
|
|
|
\param[in] X, Y, W, H position and size of the widget
|
|
|
|
\param[in] L widget label, default is no label
|
|
|
|
*/
|
|
|
|
Fl_Clock::Fl_Clock(int X, int Y, int W, int H, const char *L)
|
|
|
|
: Fl_Clock_Output(X, Y, W, H, L) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Create an Fl_Clock widget using the given boxtype, position, size, and
|
|
|
|
label string.
|
|
|
|
\param[in] t boxtype
|
|
|
|
\param[in] X, Y, W, H position and size of the widget
|
|
|
|
\param[in] L widget label, default is no label
|
|
|
|
*/
|
|
|
|
Fl_Clock::Fl_Clock(uchar t, int X, int Y, int W, int H, const char *L)
|
|
|
|
: Fl_Clock_Output(X, Y, W, H, L) {
|
1998-10-06 22:21:25 +04:00
|
|
|
type(t);
|
|
|
|
box(t==FL_ROUND_CLOCK ? FL_NO_BOX : FL_UP_BOX);
|
|
|
|
}
|
|
|
|
|
1999-01-13 18:45:50 +03:00
|
|
|
static void tick(void *v) {
|
2012-04-05 09:12:30 +04:00
|
|
|
((Fl_Clock*)v)->value((ulong) time(0));
|
1999-01-13 18:58:33 +03:00
|
|
|
Fl::add_timeout(1.0, tick, v);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int Fl_Clock::handle(int event) {
|
|
|
|
switch (event) {
|
1999-01-13 18:45:50 +03:00
|
|
|
case FL_SHOW:
|
|
|
|
tick(this);
|
|
|
|
break;
|
1998-10-06 22:21:25 +04:00
|
|
|
case FL_HIDE:
|
|
|
|
Fl::remove_timeout(tick, this);
|
|
|
|
break;
|
|
|
|
}
|
1999-01-13 18:45:50 +03:00
|
|
|
return Fl_Clock_Output::handle(event);
|
|
|
|
}
|
|
|
|
|
2008-09-16 10:49:08 +04:00
|
|
|
/**
|
|
|
|
The destructor removes the clock.
|
|
|
|
*/
|
1999-01-13 18:45:50 +03:00
|
|
|
Fl_Clock::~Fl_Clock() {
|
|
|
|
Fl::remove_timeout(tick, this);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
1998-10-20 00:46:58 +04:00
|
|
|
|
2012-07-24 08:37:22 +04:00
|
|
|
|
|
|
|
Fl_Round_Clock::Fl_Round_Clock(int X,int Y,int W,int H, const char *L)
|
|
|
|
: Fl_Clock(X, Y, W, H, L)
|
|
|
|
{
|
|
|
|
type(FL_ROUND_CLOCK);
|
|
|
|
box(FL_NO_BOX);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|