- Properly disable the second hand.

- Remove unneeded #include "winnls", #define MIN.
- Get the digital clock working.
This commit is contained in:
Richard Cohen 2003-12-03 04:44:18 +00:00 committed by Alexandre Julliard
parent 64fb191cf6
commit fa7060cba4
3 changed files with 89 additions and 44 deletions

View File

@ -37,10 +37,36 @@
#define INITIAL_WINDOW_SIZE 200
#define TIMER_ID 1
#define TIMER_PERIOD 50 /* milliseconds */
CLOCK_GLOBALS Globals;
/***********************************************************************
*
* CLOCK_ResetTimer
*/
static BOOL CLOCK_ResetTimer(void)
{
UINT period; /* milliseconds */
KillTimer(Globals.hMainWnd, TIMER_ID);
if (Globals.bSeconds)
if (Globals.bAnalog)
period = 50;
else
period = 500;
else
period = 1000;
if (!SetTimer (Globals.hMainWnd, TIMER_ID, period, NULL)) {
CHAR szApp[MAX_STRING_LEN];
LoadString(Globals.hInstance, IDS_CLOCK, szApp, sizeof(szApp));
MessageBox(0, "No available timers", szApp, MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
return TRUE;
}
/***********************************************************************
*
* CLOCK_MenuCommand
@ -57,14 +83,16 @@ int CLOCK_MenuCommand (WPARAM wParam)
case IDM_ANALOG: {
Globals.bAnalog = TRUE;
LANGUAGE_UpdateMenuCheckmarks();
SendMessage(Globals.hMainWnd, WM_PAINT, 0, 0);
CLOCK_ResetTimer();
InvalidateRect(Globals.hMainWnd, NULL, FALSE);
break;
}
/* switch to digital */
case IDM_DIGITAL: {
Globals.bAnalog = FALSE;
LANGUAGE_UpdateMenuCheckmarks();
SendMessage(Globals.hMainWnd, WM_PAINT, 0, 0);
CLOCK_ResetTimer();
InvalidateRect(Globals.hMainWnd, NULL, FALSE);
break;
}
/* change font */
@ -89,7 +117,8 @@ int CLOCK_MenuCommand (WPARAM wParam)
case IDM_SECONDS: {
Globals.bSeconds = !Globals.bSeconds;
LANGUAGE_UpdateMenuCheckmarks();
SendMessage(Globals.hMainWnd, WM_PAINT, 0, 0);
CLOCK_ResetTimer();
InvalidateRect(Globals.hMainWnd, NULL, FALSE);
break;
}
/* show or hide date */
@ -174,9 +203,9 @@ LRESULT WINAPI CLOCK_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
context = BeginPaint(hWnd, &ps);
if(Globals.bAnalog)
AnalogClock(context, Globals.MaxX, Globals.MaxY);
AnalogClock(context, Globals.MaxX, Globals.MaxY, Globals.bSeconds);
else
DigitalClock(context, Globals.MaxX, Globals.MaxY);
DigitalClock(context, Globals.MaxX, Globals.MaxY, Globals.bSeconds);
EndPaint(hWnd, &ps);
break;
}
@ -193,7 +222,7 @@ LRESULT WINAPI CLOCK_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
}
case WM_TIMER: {
/* Could just invalidate the changed hands,
/* Could just invalidate what has changed,
* but it doesn't really seem worth the effort
*/
InvalidateRect(Globals.hMainWnd, NULL, FALSE);
@ -212,7 +241,6 @@ LRESULT WINAPI CLOCK_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
}
/***********************************************************************
*
* WinMain
@ -260,10 +288,8 @@ int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show
Globals.MaxX, Globals.MaxY, 0,
0, Globals.hInstance, 0);
if (!SetTimer (Globals.hMainWnd, TIMER_ID, TIMER_PERIOD, NULL)) {
MessageBox(0, "No available timers", szWinName, MB_ICONEXCLAMATION | MB_OK);
if (!CLOCK_ResetTimer())
return FALSE;
}
LANGUAGE_LoadMenus();
SetMenu(Globals.hMainWnd, Globals.hMainMenu);
@ -278,5 +304,7 @@ int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show
DispatchMessage(&msg);
}
KillTimer(Globals.hMainWnd, TIMER_ID);
return 0;
}

View File

@ -31,15 +31,14 @@
#include <stdlib.h>
#include <string.h>
#include "windows.h"
#include "winnls.h"
#include "winclock.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
COLORREF FaceColor = RGB(192,192,192);
COLORREF HandColor = RGB(0,0,0);
COLORREF EtchColor = RGB(0,0,0);
static COLORREF FaceColor = RGB(192,192,192);
static COLORREF HandColor = RGB(0,0,0);
static COLORREF EtchColor = RGB(0,0,0);
static COLORREF GRAY = RGB(128,128,128);
static const int ETCH_DEPTH = 2;
typedef struct
{
POINT Start;
@ -83,10 +82,11 @@ static void DrawHand(HDC dc,HandData* hand)
LineTo(dc, hand->End.x, hand->End.y);
}
static void DrawHands(HDC dc)
static void DrawHands(HDC dc, BOOL bSeconds)
{
SelectObject(dc,CreatePen(PS_SOLID,1,HandColor));
DrawHand(dc, &SecondHand);
if (bSeconds)
DrawHand(dc, &SecondHand);
DrawHand(dc, &MinuteHand);
DrawHand(dc, &HourHand);
DeleteObject(SelectObject(dc,GetStockObject(NULL_PEN)));
@ -99,7 +99,7 @@ static void PositionHand(const POINT* centre, double length, double angle, HandD
hand->End.y = centre->y - cos(angle)*length;
}
static void PositionHands(const POINT* centre, int radius)
static void PositionHands(const POINT* centre, int radius, BOOL bSeconds)
{
SYSTEMTIME st;
double hour, minute, second;
@ -114,40 +114,57 @@ static void PositionHands(const POINT* centre, int radius)
PositionHand(centre, radius * 0.5, hour/12 * 2*M_PI, &HourHand);
PositionHand(centre, radius * 0.65, minute/60 * 2*M_PI, &MinuteHand);
PositionHand(centre, radius * 0.79, second/60 * 2*M_PI, &SecondHand);
if (bSeconds)
PositionHand(centre, radius * 0.79, second/60 * 2*M_PI, &SecondHand);
}
void AnalogClock(HDC dc, int x, int y)
void AnalogClock(HDC dc, int x, int y, BOOL bSeconds)
{
POINT centre;
int radius;
radius = MIN(x, y)/2;
radius = min(x, y)/2;
centre.x = x/2;
centre.y = y/2;
DrawFace(dc, &centre, radius);
PositionHands(&centre, radius);
DrawHands(dc);
PositionHands(&centre, radius, bSeconds);
DrawHands(dc, bSeconds);
}
void DigitalClock(HDC dc, int X, int Y)
void DigitalClock(HDC dc, int x, int y, BOOL bSeconds)
{
/* FIXME - this doesn't work very well */
CHAR szTime[255];
static short xChar, yChar;
TEXTMETRIC tm;
SYSTEMTIME st;
GetLocalTime(&st);
GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, &st, NULL,
szTime, sizeof szTime);
xChar = tm.tmAveCharWidth;
yChar = tm.tmHeight;
xChar = 100;
yChar = 100;
SIZE extent;
LOGFONT lf;
double xscale, yscale;
HFONT oldFont;
SelectObject(dc,CreatePen(PS_SOLID,1,FaceColor));
TextOut (dc, xChar, yChar, szTime, strlen (szTime));
DeleteObject(SelectObject(dc,GetStockObject(NULL_PEN)));
GetTimeFormat(LOCALE_USER_DEFAULT, bSeconds ? 0 : TIME_NOSECONDS, NULL,
NULL, szTime, sizeof (szTime));
memset(&lf, 0, sizeof (lf));
lf.lfHeight = -20;
x -= 2 * ETCH_DEPTH;
y -= 2 * ETCH_DEPTH;
oldFont = SelectObject(dc, CreateFontIndirect(&lf));
GetTextExtentPoint(dc, szTime, strlen(szTime), &extent);
xscale = (double)x/extent.cx;
yscale = (double)y/extent.cy;
lf.lfHeight *= min(xscale, yscale);
DeleteObject(SelectObject(dc, CreateFontIndirect(&lf)));
GetTextExtentPoint(dc, szTime, strlen(szTime), &extent);
SetBkColor(dc, GRAY); /* to match the background brush */
SetTextColor(dc, EtchColor);
TextOut(dc, (x - extent.cx)/2 + ETCH_DEPTH, (y - extent.cy)/2 + ETCH_DEPTH,
szTime, strlen(szTime));
SetBkMode(dc, TRANSPARENT);
SetTextColor(dc, FaceColor);
TextOut(dc, (x - extent.cx)/2, (y - extent.cy)/2, szTime, strlen(szTime));
DeleteObject(SelectObject(dc, oldFont));
}

View File

@ -21,5 +21,5 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
void AnalogClock(HDC dc, int X, int Y);
void DigitalClock(HDC dc, int X, int Y);
void AnalogClock(HDC dc, int X, int Y, BOOL bSeconds);
void DigitalClock(HDC dc, int X, int Y, BOOL bSeconds);