Include port.h so we define M_PI on Mingw build.
[wine/gsoc_dplay.git] / programs / clock / winclock.c
blobb75e03d51d03bd5742b4681a32137a26bbdec4af
1 /*
2 * Clock (winclock.c)
4 * Copyright 1998 by Marcel Baur <mbaur@g26.ethz.ch>
6 * This file is based on rolex.c by Jim Peterson.
8 * I just managed to move the relevant parts into the Clock application
9 * and made it look like the original Windows one. You can find the original
10 * rolex.c in the wine /libtest directory.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <math.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "windows.h"
34 #include "winnls.h"
35 #include "winclock.h"
37 #define MIN(a,b) (((a)<(b))?(a):(b))
39 COLORREF FaceColor = RGB(192,192,192);
40 COLORREF HandColor = RGB(0,0,0);
41 COLORREF EtchColor = RGB(0,0,0);
43 typedef struct
45 POINT Start;
46 POINT End;
47 } HandData;
49 HandData HourHand, MinuteHand, SecondHand;
51 static void DrawFace(HDC dc, const POINT* centre, int radius)
53 int t;
55 SelectObject(dc,CreateSolidBrush(FaceColor));
56 SelectObject(dc,CreatePen(PS_SOLID,1,EtchColor));
57 Ellipse(dc,
58 centre->x - radius, centre->y - radius,
59 centre->x + radius, centre->y + radius);
61 for(t=0; t<12; t++) {
62 MoveToEx(dc,
63 centre->x + sin(t*M_PI/6)*0.9*radius,
64 centre->y - cos(t*M_PI/6)*0.9*radius,
65 NULL);
66 LineTo(dc,
67 centre->x + sin(t*M_PI/6)*0.8*radius,
68 centre->y - cos(t*M_PI/6)*0.8*radius);
70 if (radius>64)
71 for(t=0; t<60; t++)
72 SetPixel(dc,
73 centre->x + sin(t*M_PI/30)*0.9*radius,
74 centre->y - cos(t*M_PI/30)*0.9*radius,
75 EtchColor);
76 DeleteObject(SelectObject(dc,GetStockObject(NULL_BRUSH)));
77 DeleteObject(SelectObject(dc,GetStockObject(NULL_PEN)));
80 static void DrawHand(HDC dc,HandData* hand)
82 MoveToEx(dc, hand->Start.x, hand->Start.y, NULL);
83 LineTo(dc, hand->End.x, hand->End.y);
86 static void DrawHands(HDC dc)
88 SelectObject(dc,CreatePen(PS_SOLID,1,HandColor));
89 DrawHand(dc, &SecondHand);
90 DrawHand(dc, &MinuteHand);
91 DrawHand(dc, &HourHand);
92 DeleteObject(SelectObject(dc,GetStockObject(NULL_PEN)));
95 static void PositionHand(const POINT* centre, double length, double angle, HandData* hand)
97 hand->Start = *centre;
98 hand->End.x = centre->x + sin(angle)*length;
99 hand->End.y = centre->y - cos(angle)*length;
102 static void PositionHands(const POINT* centre, int radius)
104 SYSTEMTIME st;
105 double hour, minute, second;
107 /* 0 <= hour,minute,second < 2pi */
108 /* Adding the millisecond count makes the second hand move more smoothly */
110 GetLocalTime(&st);
111 second = st.wSecond + st.wMilliseconds/1000.0;
112 minute = st.wMinute + second/60.0;
113 hour = st.wHour % 12 + minute/60.0;
115 PositionHand(centre, radius * 0.5, hour/12 * 2*M_PI, &HourHand);
116 PositionHand(centre, radius * 0.65, minute/60 * 2*M_PI, &MinuteHand);
117 PositionHand(centre, radius * 0.79, second/60 * 2*M_PI, &SecondHand);
120 void AnalogClock(HDC dc, int x, int y)
122 POINT centre;
123 int radius;
124 radius = MIN(x, y)/2;
126 centre.x = x/2;
127 centre.y = y/2;
129 DrawFace(dc, &centre, radius);
130 PositionHands(&centre, radius);
131 DrawHands(dc);
134 void DigitalClock(HDC dc, int X, int Y)
136 /* FIXME - this doesn't work very well */
137 CHAR szTime[255];
138 static short xChar, yChar;
139 TEXTMETRIC tm;
140 SYSTEMTIME st;
142 GetLocalTime(&st);
143 GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, &st, NULL,
144 szTime, sizeof szTime);
145 xChar = tm.tmAveCharWidth;
146 yChar = tm.tmHeight;
147 xChar = 100;
148 yChar = 100;
150 SelectObject(dc,CreatePen(PS_SOLID,1,FaceColor));
151 TextOut (dc, xChar, yChar, szTime, strlen (szTime));
152 DeleteObject(SelectObject(dc,GetStockObject(NULL_PEN)));