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

View File

@ -31,15 +31,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "windows.h" #include "windows.h"
#include "winnls.h"
#include "winclock.h" #include "winclock.h"
#define MIN(a,b) (((a)<(b))?(a):(b)) static COLORREF FaceColor = RGB(192,192,192);
static COLORREF HandColor = RGB(0,0,0);
COLORREF FaceColor = RGB(192,192,192); static COLORREF EtchColor = RGB(0,0,0);
COLORREF HandColor = RGB(0,0,0); static COLORREF GRAY = RGB(128,128,128);
COLORREF EtchColor = RGB(0,0,0); static const int ETCH_DEPTH = 2;
typedef struct typedef struct
{ {
POINT Start; POINT Start;
@ -83,10 +82,11 @@ static void DrawHand(HDC dc,HandData* hand)
LineTo(dc, hand->End.x, hand->End.y); 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)); SelectObject(dc,CreatePen(PS_SOLID,1,HandColor));
DrawHand(dc, &SecondHand); if (bSeconds)
DrawHand(dc, &SecondHand);
DrawHand(dc, &MinuteHand); DrawHand(dc, &MinuteHand);
DrawHand(dc, &HourHand); DrawHand(dc, &HourHand);
DeleteObject(SelectObject(dc,GetStockObject(NULL_PEN))); 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; 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; SYSTEMTIME st;
double hour, minute, second; 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.5, hour/12 * 2*M_PI, &HourHand);
PositionHand(centre, radius * 0.65, minute/60 * 2*M_PI, &MinuteHand); 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; POINT centre;
int radius; int radius;
radius = MIN(x, y)/2; radius = min(x, y)/2;
centre.x = x/2; centre.x = x/2;
centre.y = y/2; centre.y = y/2;
DrawFace(dc, &centre, radius); DrawFace(dc, &centre, radius);
PositionHands(&centre, radius); PositionHands(&centre, radius, bSeconds);
DrawHands(dc); 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]; CHAR szTime[255];
static short xChar, yChar; SIZE extent;
TEXTMETRIC tm; LOGFONT lf;
SYSTEMTIME st; double xscale, yscale;
HFONT oldFont;
GetLocalTime(&st);
GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, &st, NULL,
szTime, sizeof szTime);
xChar = tm.tmAveCharWidth;
yChar = tm.tmHeight;
xChar = 100;
yChar = 100;
SelectObject(dc,CreatePen(PS_SOLID,1,FaceColor)); GetTimeFormat(LOCALE_USER_DEFAULT, bSeconds ? 0 : TIME_NOSECONDS, NULL,
TextOut (dc, xChar, yChar, szTime, strlen (szTime)); NULL, szTime, sizeof (szTime));
DeleteObject(SelectObject(dc,GetStockObject(NULL_PEN)));
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 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
void AnalogClock(HDC dc, int X, int Y); void AnalogClock(HDC dc, int X, int Y, BOOL bSeconds);
void DigitalClock(HDC dc, int X, int Y); void DigitalClock(HDC dc, int X, int Y, BOOL bSeconds);