1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/*
* This file is part of the XForms library package.
*
* XForms is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1, or
* (at your option) any later version.
*
* XForms is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with XForms. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file clock.c
*
* This file is part of the XForms library package.
* Copyright (c) 1996-2002 T.C. Zhao and Mark Overmars
* All rights reserved.
*
* clocks
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "include/forms.h"
#include "flinternal.h"
#include <math.h>
#include <time.h>
#ifndef M_PI
#define M_PI 3.14159265359
#endif
typedef struct
{
time_t sec;
long offset;
int nstep;
int am_pm; /* 12hr clock */
} SPEC;
static double hourhand[ 4 ][ 2 ] =
{
{ -0.6, 0.0 },
{ 0.0, -1.6 },
{ 0.6, 0.0 },
{ 0.0, 7.0 }
};
static double minhand[ 4 ][ 2 ] =
{
{ -0.6, 0.0 },
{ 0.0, -1.6 },
{ 0.6, 0.0 },
{ 0.0, 11.6 }
};
static double sechand[ 4 ][ 2 ] =
{
{ -0.3, 0.0 },
{ 0.0, -2.0 },
{ 0.3, 0.0 },
{ 0.0, 11.6 }
};
#define ROTxy( xx, yy, x, y, a ) \
do \
{ \
double s = sin( a ); \
double c = cos( a ); \
xx = FL_crnd( xc + ( ( x ) - xc ) * c + ( ( y ) - yc ) * s ); \
yy = FL_crnd( yc - ( ( x ) - xc ) * s + ( ( y ) - yc ) * c ); \
} while ( 0 )
/***************************************
***************************************/
static void
draw_hand( FL_Coord x,
FL_Coord y,
FL_Coord w,
FL_Coord h,
double a[ ][ 2 ],
double ra,
FL_COLOR fc,
FL_COLOR bc )
{
int i;
double ccp[ 4 ][ 2 ];
double xc = x + 0.5 * w,
yc = y + 0.5 * h;
FL_POINT xp[ 5 ]; /* Needs one extra point! */
for ( i = 0; i < 4; i++ )
{
ccp[ i ][ 0 ] = xc + a[ i ][ 0 ] * w / 28.0;
ccp[ i ][ 1 ] = yc + a[ i ][ 1 ] * h / 28.0;
ROTxy( xp[ i ].x, xp[ i ].y, ccp[ i ][ 0 ], ccp[ i ][ 1 ], ra );
}
fl_polyf( xp, 4, fc );
fl_polyl( xp, 4, bc );
}
static int hours,
minutes,
seconds;
static int updating;
/***************************************
***************************************/
static void
show_hands( FL_Coord x,
FL_Coord y,
FL_Coord w,
FL_Coord h,
FL_COLOR fcolor,
FL_COLOR bcolor )
{
double ra;
double fact = - M_PI / 180.0;
ra = fact * ( 180 + 30 * hours + 0.5 * minutes );
draw_hand( x, y, w, h, hourhand, ra, fcolor, bcolor );
ra = fact * ( 180 + 6 * minutes + seconds / 10 );
draw_hand( x, y, w, h, minhand, ra, fcolor, bcolor );
ra = fact * ( 180 + 6 * seconds );
draw_hand( x, y, w, h, sechand, ra, fcolor, bcolor );
}
/***************************************
***************************************/
static void
draw_clock( int type FL_UNUSED_ARG,
FL_Coord x,
FL_Coord y,
FL_Coord w,
FL_Coord h,
FL_COLOR col1 FL_UNUSED_ARG,
FL_COLOR col2 )
{
double xc = x + 0.5 * w,
yc = y + 0.5 * h;
int i;
double ra;
FL_POINT xp[ 5 ]; /* need one extra for closing of polygon! */
double f1,
f2,
f3;
#if FL_DEBUG >= ML_DEBUG
M_info( "draw_clock", "entering" );
#endif
w -= 4;
h -= 4;
/* draw hour ticks */
f2 = 0.40 * h;
f3 = 0.44 * h;
for ( ra = 0.0, i = 0; i < 12; i++, ra += M_PI / 6 )
{
f1 = ( i % 3 ? 0.01 : 0.02 ) * w;
ROTxy( xp[ 0 ].x, xp[ 0 ].y, xc - f1, yc + f2, ra );
ROTxy( xp[ 1 ].x, xp[ 1 ].y, xc + f1, yc + f2, ra );
ROTxy( xp[ 2 ].x, xp[ 2 ].y, xc + f1, yc + f3, ra );
ROTxy( xp[ 3 ].x, xp[ 3 ].y, xc - f1, yc + f3, ra );
fl_polyf( xp, 4, FL_LEFT_BCOL );
}
show_hands( x + 2 + 0.02 * w, y + 2 + 0.02 * h,
w, h, FL_RIGHT_BCOL, FL_RIGHT_BCOL );
show_hands( x, y, w, h, col2, FL_LEFT_BCOL );
#if FL_DEBUG >= ML_DEBUG
M_info( "draw_clock", "done" );
#endif
}
/***************************************
***************************************/
static void
draw_digitalclock( FL_OBJECT * ob )
{
char buf[ 12 ];
SPEC *sp = ob->spec;
if ( sp->am_pm )
sprintf( buf, "%d:%02d:%02d %s", hours > 12 ? hours - 12 : hours,
minutes, seconds, hours > 12 ? "pm" : "am" );
else
sprintf( buf, "%d:%02d:%02d", hours, minutes, seconds );
fl_drw_text( FL_ALIGN_CENTER, ob->x, ob->y, ob->w, ob->h, ob->col2,
ob->lstyle, ob->lsize, buf );
}
/***************************************
***************************************/
static int
handle_clock( FL_OBJECT * ob,
int event,
FL_Coord x FL_UNUSED_ARG,
FL_Coord y FL_UNUSED_ARG,
int k FL_UNUSED_ARG,
void * ev FL_UNUSED_ARG )
{
time_t ticks;
struct tm *timeofday;
SPEC *sp = ob->spec;
switch ( event )
{
case FL_DRAW :
fl_drw_box( ob->boxtype, ob->x, ob->y, ob->w, ob->h,
ob->col1, ob->bw );
if ( ob->type == FL_DIGITAL_CLOCK )
draw_digitalclock( ob );
else
draw_clock( ob->type, ob->x, ob->y, ob->w, ob->h,
ob->col1, ob->col2 );
/* fall through */
case FL_DRAWLABEL :
if ( ! updating )
fl_drw_text_beside( ob->align & ~ FL_ALIGN_INSIDE,
ob->x, ob->y, ob->w, ob->h,
ob->lcol, ob->lstyle, ob->lsize,
ob->label );
updating = 0;
break;
case FL_STEP:
/* Clock has a resolution of about 1 sec. FL_STEP is sent about
every 0.05 sec. If there are more than 10 clocks, we might run
into trouble */
if ( ++sp->nstep & 1 )
break;
sp->nstep = 0;
ticks = time( 0 ) + sp->offset;
if ( ticks != sp->sec )
{
updating = 1;
sp->sec = ticks;
timeofday = localtime( &ticks );
seconds = timeofday->tm_sec;
hours = timeofday->tm_hour;
minutes = timeofday->tm_min;
fl_redraw_object( ob );
}
break;
case FL_FREEMEM:
fl_free( ob->spec );
break;
}
return FL_RETURN_NONE;
}
/***************************************
***************************************/
FL_OBJECT *
fl_create_clock( int type,
FL_Coord x,
FL_Coord y,
FL_Coord w,
FL_Coord h,
const char * s )
{
FL_OBJECT *obj;
obj = fl_make_object( FL_CLOCK, type, x, y, w, h, s, handle_clock );
obj->boxtype = FL_CLOCK_BOXTYPE;
obj->col1 = FL_CLOCK_COL1;
obj->col2 = FL_CLOCK_COL2;
obj->lcol = FL_CLOCK_LCOL;
obj->align = FL_CLOCK_ALIGN;
obj->automatic = obj->active = 1;
obj->spec = fl_calloc( 1, sizeof *obj->spec );
return obj;
}
/***************************************
***************************************/
FL_OBJECT *
fl_add_clock( int type,
FL_Coord x,
FL_Coord y,
FL_Coord w,
FL_Coord h,
const char * s )
{
FL_OBJECT *ob = fl_create_clock( type, x, y, w, h, s );
fl_add_object( fl_current_form, ob );
fl_set_object_dblbuffer( ob, 1 );
return ob;
}
/***************************************
***************************************/
long
fl_set_clock_adjustment( FL_OBJECT * ob,
long offset )
{
SPEC *sp = ob->spec;
long old = sp->offset;
sp->offset = offset;
return old;
}
/***************************************
***************************************/
void
fl_get_clock( FL_OBJECT * ob,
int * h,
int * m,
int * s )
{
SPEC *sp = ob->spec;
time_t ticks;
struct tm *tm;
ticks = time( 0 ) + sp->offset;
tm = localtime( &ticks );
*h = tm->tm_hour;
*m = tm->tm_min;
*s = tm->tm_sec;
}
/***************************************
***************************************/
void
fl_set_clock_ampm( FL_OBJECT * ob,
int y )
{
SPEC *sp = ob->spec;
if ( sp->am_pm != y )
{
sp->am_pm = y;
fl_redraw_object( ob );
}
}
/*
* Local variables:
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|