-
Notifications
You must be signed in to change notification settings - Fork 0
/
winclock.c
429 lines (360 loc) · 10.9 KB
/
winclock.c
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Clock (winclock.c)
*
* Copyright 1998 by Marcel Baur <mbaur@g26.ethz.ch>
*
* This file is based on rolex.c by Jim Peterson.
*
* I just managed to move the relevant parts into the Clock application
* and made it look like the original Windows one. You can find the original
* rolex.c in the wine /libtest directory.
*
* This library 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 of the License, or (at your option) any later version.
*
* This library 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
// C lib headers
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dos.h>
// Windows headers
#include "windows.h"
// Application headers
#include "winclock.h"
#include "main.h"
#define M_PI 3.1415926535
typedef struct
{
POINT Start;
POINT End;
POINT p2;
POINT p3;
POINT p4;
} HandData;
static HandData HourHand, MinuteHand, SecondHand;
static void DrawTicks(HDC dc, const POINT* centre, int radius)
{
int t;
HPEN oldhPen, hPen;
HBRUSH oldhBrush, hBrush;
int hourWidth=0.09*radius;
/* Minute divisions */
if (radius>64)
{
hPen=CreatePen(PS_SOLID, 2, TickColor);
oldhPen=SelectObject(dc, hPen);
for(t=0; t<60; t++) {
MoveToEx(dc,
centre->x + sin(t*M_PI/30)*0.95*radius,
centre->y - cos(t*M_PI/30)*0.95*radius,
NULL);
LineTo(dc,
centre->x + sin(t*M_PI/30)*0.94*radius,
centre->y - cos(t*M_PI/30)*0.94*radius);
}
SelectObject(dc, oldhPen);
DeleteObject(hPen);
}
hBrush=CreateSolidBrush(RGB(00, 128,128));
oldhBrush=SelectObject(dc, hBrush);
/* Hour divisions */
for(t=0; t<12; t++) {
hPen=CreatePen(PS_SOLID, 1, RGB(0,0,0));
oldhPen=SelectObject(dc, hPen);
Rectangle(dc,
centre->x + sin(t*M_PI/6)*0.95*radius-hourWidth/2,
centre->y - cos(t*M_PI/6)*0.95*radius-hourWidth/2,
centre->x + sin(t*M_PI/6)*0.95*radius+hourWidth/2,
centre->y - cos(t*M_PI/6)*0.95*radius+hourWidth/2);
SelectObject(dc, oldhPen);
DeleteObject(hPen);
hPen=CreatePen(PS_SOLID, 1, RGB(0,255,255));
oldhPen=SelectObject(dc, hPen);
MoveToEx(dc,
centre->x + sin(t*M_PI/6)*0.95*radius-hourWidth/2,
centre->y - cos(t*M_PI/6)*0.95*radius+hourWidth/2,
NULL);
LineTo(dc,
centre->x + sin(t*M_PI/6)*0.95*radius-hourWidth/2,
centre->y - cos(t*M_PI/6)*0.95*radius-hourWidth/2);
LineTo(dc,
centre->x + sin(t*M_PI/6)*0.95*radius+hourWidth/2,
centre->y - cos(t*M_PI/6)*0.95*radius-hourWidth/2);
SelectObject(dc, oldhPen);
DeleteObject(hPen);
}
SelectObject(dc, oldhBrush);
DeleteObject(hBrush);
}
static void DrawHand(HDC dc,HandData* hand)
{
MoveToEx(dc, hand->End.x, hand->End.y, NULL);
LineTo(dc, hand->p2.x, hand->p2.y);
LineTo(dc, hand->p3.x, hand->p3.y);
LineTo(dc, hand->p4.x, hand->p4.y);
LineTo(dc, hand->End.x, hand->End.y);
}
static void DrawHands(HDC dc, BOOL bSeconds)
{
if (bSeconds) {
SelectObject(dc, CreatePen(PS_SOLID, 1, HandColor));
MoveToEx(dc, SecondHand.Start.x, SecondHand.Start.y, NULL);
LineTo(dc, SecondHand.End.x, SecondHand.End.y);
DeleteObject(SelectObject(dc, GetStockObject(NULL_PEN)));
}
SelectObject(dc, CreatePen(PS_SOLID, 1, HandColor));
DrawHand(dc, &MinuteHand);
DrawHand(dc, &HourHand);
DeleteObject(SelectObject(dc, GetStockObject(NULL_PEN)));
}
static void PositionHand(const POINT* centre, double length, double angle, HandData* hand)
{
hand->Start = *centre;
hand->End.x = centre->x + sin(angle)*length;
hand->End.y = centre->y - cos(angle)*length;
hand->p2.x=centre->x + sin(angle+M_PI/2)*length*0.07;
hand->p2.y=centre->y - cos(angle+M_PI/2)*length*0.07;
hand->p3.x=centre->x + sin(angle+M_PI)*length*0.2;
hand->p3.y=centre->y - cos(angle+M_PI)*length*0.2;
hand->p4.x=centre->x + sin(angle-M_PI/2)*length*0.07;
hand->p4.y=centre->y - cos(angle-M_PI/2)*length*0.07;
}
static void PositionHands(const POINT* centre, int radius, BOOL bSeconds)
{
// SYSTEMTIME st;
double hour, minute, second;
struct dostime_t t;
_dos_gettime (&t);
hour = t.hour%12;
minute = t.minute;
second = t.second;
/* 0 <= hour,minute,second < 2pi */
PositionHand(centre, radius * 0.6, ((hour*5+minute/12)/(12*5)) * 2*M_PI, &HourHand);
PositionHand(centre, radius * 0.79, minute/60 * 2*M_PI, &MinuteHand);
if (bSeconds)
PositionHand(centre, radius * 0.79, second/60 * 2*M_PI, &SecondHand);
}
void AnalogClock(HDC dc, int x, int y, BOOL bSeconds)
{
POINT centre;
int radius;
radius = min(x, y)/2;
if (radius < 0)
return;
centre.x = x/2;
centre.y = y/2;
DrawTicks(dc, ¢re, radius);
PositionHands(¢re, radius, bSeconds);
DrawHands(dc, bSeconds);
}
void IconAnalogClock(HDC dc, int x, int y)
{
POINT centre;
int radius;
int t;
radius = min(x, y)/2;
if (radius < 0)
return;
centre.x = x/2;
centre.y = y/2;
for(t=0; t<12; t++) {
SetPixel(dc, centre.x + sin(t*M_PI/6)*0.8*radius, centre.y - cos(t*M_PI/6)*0.8*radius, RGB(255,255,255));
SetPixel(dc, centre.x + sin(t*M_PI/6)*0.8*radius+1, centre.y - cos(t*M_PI/6)*0.8*radius, RGB(0,0,0));
SetPixel(dc, centre.x + sin(t*M_PI/6)*0.8*radius+1, centre.y - cos(t*M_PI/6)*0.8*radius+1, RGB(0,0,0));
SetPixel(dc, centre.x + sin(t*M_PI/6)*0.8*radius, centre.y - cos(t*M_PI/6)*0.8*radius+1, RGB(0,0,0));
}
PositionHands(¢re, radius, FALSE);
SelectObject(dc, CreatePen(PS_SOLID, 1, HandColor));
MoveToEx(dc, MinuteHand.Start.x, MinuteHand.Start.y, NULL);
LineTo(dc, MinuteHand.End.x, MinuteHand.End.y);
// DrawHand(dc, &MinuteHand);
MoveToEx(dc, HourHand.Start.x, HourHand.Start.y, NULL);
LineTo(dc, HourHand.End.x, HourHand.End.y);
//DrawHand(dc, &HourHand);
DeleteObject(SelectObject(dc, GetStockObject(NULL_PEN)));
}
void FormatDate(char * szDate, BOOL bFull)
{
struct dosdate_t d;
char f[3][5]={"","",""};
int i;
char dFormat[20]="";
char buf[20]="";
/*
strcpy(dFormat,"%[dMy]");
strcat(dFormat, Globals.sDate);
strcat(dFormat, "%[dMy]");
strcat(dFormat, Globals.sDate);
strcat(dFormat, "%[dMy]");
*/
strcpy(dFormat,"%[dMy]/%[dMy]/%[dMy]");
_dos_getdate (&d);
if (3==sscanf(Globals.sShortDate, dFormat, &f[0],&f[1],&f[2]))
{
for (i=0; i<3; i++)
{
if (!strcmp("d", f[i])) {
sprintf(buf, "%d", d.day);
strcat(szDate, buf);
}
else if (!strcmp("dd", f[i])) {
sprintf(buf, "%02d", d.day);
strcat(szDate, buf);
}
else if (!strcmp("M", f[i])) {
sprintf(buf, "%d", d.month);
strcat(szDate, buf);
}
else if (!strcmp("MM", f[i])) {
sprintf(buf, "%02d", d.month);
strcat(szDate, buf);
}
else if (!strcmp("yy", f[i]) && bFull) {
sprintf(buf, "%02d", d.year % 100);
strcat(szDate, buf);
}
else if (!strcmp("yyyy", f[i]) && bFull) {
sprintf(buf, "%04d", d.year);
strcat(szDate, buf);
} else {
//strcat(szDate, "error");
};
if ((i<2 && bFull) || (i<1 && !bFull)) strcat(szDate, Globals.sDate);
}
}
}
void FormatTime(char * szTime, BOOL bFull)
{
char tFormat[20]="";
struct dostime_t t;
int hour;
char buf[3]="";
_dos_gettime (&t);
if (Globals.iTime) // 24h
{
hour=t.hour;
} else { // 12h
hour=(t.hour % 12)?(t.hour % 12):12;
}
if (Globals.iTLZero)
{
sprintf(tFormat, "%02d%s%02d%s", hour, Globals.sTime, t.minute, (Globals.bSeconds && bFull)?Globals.sTime:"");
} else {
if (hour<10)
{
sprintf(tFormat, " %d%s%02d%s", hour, Globals.sTime, t.minute, (Globals.bSeconds && bFull)?Globals.sTime:"");
} else {
sprintf(tFormat, "%d%s%02d%s", hour, Globals.sTime, t.minute, (Globals.bSeconds && bFull)?Globals.sTime:"");
}
}
if (bFull)
{
if (Globals.bSeconds) strcat(tFormat, "%02d");
if (!Globals.iTime)
{
strcat(tFormat, " ");
strcat(tFormat, (t.hour<12)?Globals.s1159:Globals.s2359);
}
}
sprintf(szTime, tFormat, t.second);
}
HFONT SizeFont(HDC dc, int x, int y, BOOL bSeconds, const LOGFONT* font)
{
SIZE extent;
LOGFONT lf;
double xscale, yscale;
HFONT oldFont, newFont;
char szTime[255];
int chars;
FormatTime(szTime, TRUE);
chars=lstrlen(szTime);
lf = *font;
lf.lfHeight = -20;
oldFont = SelectObject(dc, CreateFontIndirect(&lf));
GetTextExtentPoint(dc, szTime, chars, &extent);
DeleteObject(SelectObject(dc, oldFont));
xscale = (double)x/extent.cx;
yscale = (double)y/extent.cy;
lf.lfHeight *= min(xscale, yscale);
newFont = CreateFontIndirect(&lf);
return newFont;
}
void DigitalClock(HDC dc, int x, int y, BOOL bSeconds)
{
SIZE extent;
HFONT oldFont;
char szTime[255]="";
char szDate[255]="";
int tchars;
int dchars;
int upshift = 0;
BOOL shift = FALSE;
FormatDate(szDate, TRUE);
dchars=lstrlen(szDate);
FormatTime(szTime, TRUE);
tchars=lstrlen(szTime);
oldFont = SelectObject(dc, Globals.hFont);
GetTextExtentPoint(dc, szTime, tchars, &extent);
if (extent.cy>63) {
shift=TRUE;
}
if (Globals.bDate) upshift=extent.cy/2;
SetBkColor(dc, BackgroundColor);
SetBkMode(dc, TRANSPARENT);
if (shift) {
SetTextColor(dc, RGB(255,255,255));
TextOut(dc, (x - extent.cx)/2-2 , (y - extent.cy)/2-2 - upshift , szTime, tchars);
SetTextColor(dc, ShadowColor);
TextOut(dc, (x - extent.cx)/2+2 , (y - extent.cy)/2+2 - upshift , szTime, tchars);
}
if (shift)
{
SetTextColor(dc, FaceColor);
} else {
SetTextColor(dc, HandColor);
}
TextOut(dc, (x - extent.cx)/2, (y - extent.cy)/2 - upshift, szTime, tchars);
if (Globals.bDate)
{
SelectObject(dc, Globals.hDateFont);
SetTextColor(dc, HandColor);
GetTextExtentPoint(dc, szDate, dchars, &extent);
TextOut(dc, (x - extent.cx)/2, y/2, szDate, dchars);
}
SelectObject(dc, oldFont);
}
void IconDigitalClock(HDC dc, int x, int y)
{
SIZE extent;
HFONT oldFont, font;
LOGFONT logfont;
char szTime[255]="";
int tchars;
FormatTime(szTime, FALSE);
tchars=lstrlen(szTime);
memset(&logfont, 0, sizeof(logfont));
logfont.lfHeight = 16;
logfont.lfWidth = 7;
lstrcpy(logfont.lfFaceName, "Symbol");
font = CreateFontIndirect(&logfont);
oldFont = SelectObject(dc, font);
GetTextExtentPoint(dc, szTime, tchars, &extent);
SetBkColor(dc, BackgroundColor);
SetBkMode(dc, TRANSPARENT);
SetTextColor(dc, RGB(0,0,0));
TextOut(dc, 4, (y - extent.cy)/2, szTime, tchars);
SelectObject(dc, oldFont);
DeleteObject(font);
}