[go: up one dir, main page]

File: clock.c

package info (click to toggle)
libforms1 1.0-8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,648 kB
  • ctags: 14,351
  • sloc: ansic: 90,441; makefile: 9,902; sh: 8
file content (319 lines) | stat: -rw-r--r-- 7,301 bytes parent folder | download | duplicates (3)
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
/*
 *
 *  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; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 *
 */


/*
 * $Id: clock.c,v 1.0 2002/05/08 05:16:30 zhao Release $
 *
 *.
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *.
 *
 *   clocks
 */

#include "forms.h"
#include <math.h>
#include <time.h>

#ifndef M_PI
#define M_PI   3.1415926f
#endif

static float hourhand[4][2] =
{
    {-0.6f, 0.0f},
    {0.0f, -1.6f},
    {0.6f, 0.0f},
    {0.0f, 7.0f}
};

static float minhand[4][2] =
{
    {-0.6f, 0.0f},
    {0.0f, -1.6f},
    {0.6f, 0.0f},
    {0.0f, 11.6f}
};

static float sechand[4][2] =
{
    {-0.3f, 0.0f},
    {0.0f, -2.0f},
    {0.3f, 0.0f},
    {0.0f, 11.6f}
};

#define ROTx(x,y,a) (xc + ((x)-xc)*cos(a) + ((y)-yc)*sin(a))
#define ROTy(x,y,a) (yc - ((x)-xc)*sin(a) + ((y)-yc)*cos(a))
#define ROTxy(xx,yy,x,y,a)  xx = ROTx(x,y,a); yy = ROTy(x,y,a)

static void
draw_hand(FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h,
	  float a[][2], float ra, FL_COLOR fc, FL_COLOR bc)
{
    int i;
    float ccp[4][2], xx[4], yy[4];
    float xc = x + w / 2, yc = y + h / 2;
    FL_POINT xp[5];

    for (i = 0; i < 4; i++)
    {
	ccp[i][0] = (xc + a[i][0] * w / 28.0f);
	ccp[i][1] = (yc + a[i][1] * h / 28.0f);
    }

    ROTxy(xx[0], yy[0], ccp[0][0], ccp[0][1], ra);
    ROTxy(xx[1], yy[1], ccp[1][0], ccp[1][1], ra);
    ROTxy(xx[2], yy[2], ccp[2][0], ccp[2][1], ra);
    ROTxy(xx[3], yy[3], ccp[3][0], ccp[3][1], ra);

    for (i = 0; i < 4; i++)
    {
	xp[i].x = (short)FL_nint(xx[i]);
	xp[i].y = (short)FL_nint(yy[i]);
    }

    fl_polyf(xp, 4, fc);
    fl_polyl(xp, 4, bc);
}


static int hours, minutes, seconds;	/* hr: 0-23, minutes:0-59 */
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)
{
    float ra;
    float fact = M_PI / 180.0f;

    ra = -(180.0f + hours * 360 / 12 + (minutes * 0.5f)) * fact;
    draw_hand(x, y, w, h, hourhand, ra, fcolor, bcolor);
    ra = -(180 + minutes * 360 / 60 + (seconds / 10)) * fact;
    draw_hand(x, y, w, h, minhand, ra, fcolor, bcolor);
    ra = -(180 + seconds * 360 / 60) * fact;
    draw_hand(x, y, w, h, sechand, ra, fcolor, bcolor);
}


static void
draw_clock(int type, FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h,
	   FL_COLOR col1, FL_COLOR col2)
{
    FL_Coord xc = x + w / 2, yc = y + h / 2;
    int a, i, j;
    float ra;
    FL_Coord xx[5], yy[5];
    FL_POINT xp[5];
    float f1, f2, f3;

#if (FL_DEBUG >= M_DEBUG)
    M_info("DrawClock", "entering");
#endif

    w -= 4;
    h -= 4;
    /* draw hour ticks */
    f2 = 0.40f * h;
    f3 = 0.44f * h;
    for (a = 0, i = 0; i < 12; i++, a += 30)
    {
	ra = a * M_PI / 180.0f;
	if (i == 0 || i == 3 || i == 6 || i == 9)
	{
	    f1 = 0.02f * w;
	    ROTxy(xx[0], yy[0], xc - f1, yc + f2, ra);
	    ROTxy(xx[1], yy[1], xc + f1, yc + f2, ra);
	    ROTxy(xx[2], yy[2], xc + f1, yc + f3, ra);
	    ROTxy(xx[3], yy[3], xc - f1, yc + f3, ra);
	}
	else
	{
	    f1 = 0.01f * w;
	    ROTxy(xx[0], yy[0], xc - f1, yc + f2, ra);
	    ROTxy(xx[1], yy[1], xc + f1, yc + f2, ra);
	    ROTxy(xx[2], yy[2], xc + f1, yc + f3, ra);
	    ROTxy(xx[3], yy[3], xc - f1, yc + f3, ra);
	}

	for (j = 0; j < 4; j++)
	{
	    xp[j].x = FL_nint(xx[j]);
	    xp[j].y = FL_nint(yy[j]);
	}

	fl_polygon(1, xp, 4, FL_LEFT_BCOL);
    }

    show_hands(x + 2 + 0.02f * w, y + 2 + 0.02f * h, w, h, FL_RIGHT_BCOL, FL_RIGHT_BCOL);
    show_hands(x, y, w, h, col2, FL_LEFT_BCOL);

#if (FL_DEBUG >= M_DEBUG)
    M_info("DrawClock", "done");
#endif
}

typedef struct
{
    long sec;
    long offset;
    int nstep;
    int am_pm;			/* 12hr clock */
}
SPEC;

static void
draw_digitalclock(FL_OBJECT * ob)
{
    char buf[40];
    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_Coord y,
	     int k, void *ev)
{
    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 only has resolution about 1sec. FL_STEP is sent about every
	   0.05 sec. If more than 10 clocks, we might run into trouble   */
	if (++sp->nstep & 1)
	    break;

	ticks = time(0);
	if (sp->sec != ticks)
	{
	    updating = 1;
	    sp->sec = ticks;
	    ticks += sp->offset;
	    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 0;
}

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(sizeof(SPEC), 1);
    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;

    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);
    ticks += 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);
    }
}