[clock06] Get more stuff working

Richard Cohen richard at daijobu.co.uk
Sat Nov 29 10:58:00 CST 2003


Changelog
	+ Properly disable the second hand
	+ Remove unneeded #include "winnls", #define MIN
	+ Get the digital clock working

-------------- next part --------------
Only in programs/clock.5/: fixup.sed
diff -u -r programs/clock.5/main.c programs/clock/main.c
--- programs/clock.5/main.c	2003-11-28 16:23:39.000000000 +0000
+++ programs/clock/main.c	2003-11-29 16:46:27.000000000 +0000
@@ -37,12 +37,37 @@
 
 #define INITIAL_WINDOW_SIZE 200
 #define TIMER_ID 1
-#define TIMER_PERIOD 50 /* milliseconds */
 
 CLOCK_GLOBALS Globals;
 
 /***********************************************************************
  *
+ *           CLOCK_ResetTimer
+ */
+static BOOL CLOCK_ResetTimer(void)
+{
+    KillTimer(Globals.hMainWnd, TIMER_ID);
+    UINT period; /* milliseconds */
+
+    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
  *
  *  All handling of main menu events
@@ -57,14 +82,16 @@
         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 +116,8 @@
         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 +202,9 @@
             
             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 +221,7 @@
         }
             
         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 +240,6 @@
 }
 
 
-
 /***********************************************************************
  *
  *           WinMain
@@ -260,10 +287,8 @@
                                      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 +303,7 @@
         DispatchMessage(&msg);
     }
 
+    KillTimer(Globals.hMainWnd, TIMER_ID);
+
     return 0;
 }
diff -u -r programs/clock.5/winclock.c programs/clock/winclock.c
--- programs/clock.5/winclock.c	2003-08-02 07:52:02.000000000 +0100
+++ programs/clock/winclock.c	2003-11-29 15:56:14.000000000 +0000
@@ -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 @@
     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 @@
     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 @@
 
     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;
-
-    SelectObject(dc,CreatePen(PS_SOLID,1,FaceColor));
-    TextOut (dc, xChar, yChar, szTime, strlen (szTime));
-    DeleteObject(SelectObject(dc,GetStockObject(NULL_PEN)));   
+    SIZE extent;
+    LOGFONT lf;
+    double xscale, yscale;
+    HFONT oldFont;
+
+    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));
 }
diff -u -r programs/clock.5/winclock.h programs/clock/winclock.h
--- programs/clock.5/winclock.h	2003-07-17 08:49:18.000000000 +0100
+++ programs/clock/winclock.h	2003-11-28 17:39:42.000000000 +0000
@@ -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);


More information about the wine-patches mailing list