4 * Copyright 1998 Marcel Baur <mbaur@g26.ethz.ch>
6 * Clock is partially based on
7 * - Program Manager by Ulrich Schmied
8 * - rolex.c by Jim Peterson
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #define INITIAL_WINDOW_SIZE 200
38 CLOCK_GLOBALS Globals
;
40 static VOID
CLOCK_UpdateMenuCheckmarks(VOID
)
42 HMENU hPropertiesMenu
;
43 hPropertiesMenu
= GetSubMenu(Globals
.hMainMenu
, 0);
50 CheckMenuRadioItem(hPropertiesMenu
, IDM_ANALOG
, IDM_DIGITAL
, IDM_ANALOG
, MF_CHECKED
);
51 EnableMenuItem(hPropertiesMenu
, IDM_FONT
, MF_GRAYED
);
56 CheckMenuRadioItem(hPropertiesMenu
, IDM_ANALOG
, IDM_DIGITAL
, IDM_DIGITAL
, MF_CHECKED
);
57 EnableMenuItem(hPropertiesMenu
, IDM_FONT
, 0);
60 CheckMenuItem(hPropertiesMenu
, IDM_NOTITLE
, (Globals
.bWithoutTitle
? MF_CHECKED
: MF_UNCHECKED
));
62 CheckMenuItem(hPropertiesMenu
, IDM_ONTOP
, (Globals
.bAlwaysOnTop
? MF_CHECKED
: MF_UNCHECKED
));
63 CheckMenuItem(hPropertiesMenu
, IDM_SECONDS
, (Globals
.bSeconds
? MF_CHECKED
: MF_UNCHECKED
));
64 CheckMenuItem(hPropertiesMenu
, IDM_DATE
, (Globals
.bDate
? MF_CHECKED
: MF_UNCHECKED
));
67 static VOID
CLOCK_UpdateWindowCaption(VOID
)
69 WCHAR szCaption
[MAX_STRING_LEN
];
72 /* Set frame caption */
74 chars
= GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_LONGDATE
, NULL
, NULL
,
75 szCaption
, ARRAY_SIZE(szCaption
));
78 szCaption
[chars
++] = ' ';
79 szCaption
[chars
++] = '-';
80 szCaption
[chars
++] = ' ';
81 szCaption
[chars
] = '\0';
84 LoadStringW(0, IDS_CLOCK
, szCaption
+ chars
, MAX_STRING_LEN
- chars
);
85 SetWindowTextW(Globals
.hMainWnd
, szCaption
);
88 /***********************************************************************
92 static BOOL
CLOCK_ResetTimer(void)
94 UINT period
; /* milliseconds */
96 KillTimer(Globals
.hMainWnd
, TIMER_ID
);
106 if (!SetTimer (Globals
.hMainWnd
, TIMER_ID
, period
, NULL
)) {
107 WCHAR szApp
[MAX_STRING_LEN
];
108 LoadStringW(Globals
.hInstance
, IDS_CLOCK
, szApp
, MAX_STRING_LEN
);
109 MessageBoxW(0, L
"No available timers", szApp
, MB_ICONEXCLAMATION
| MB_OK
);
115 /***********************************************************************
119 static VOID
CLOCK_ResetFont(VOID
)
122 HDC dc
= GetDC(Globals
.hMainWnd
);
123 newfont
= SizeFont(dc
, Globals
.MaxX
, Globals
.MaxY
, Globals
.bSeconds
, &Globals
.logfont
);
125 DeleteObject(Globals
.hFont
);
126 Globals
.hFont
= newfont
;
129 ReleaseDC(Globals
.hMainWnd
, dc
);
133 /***********************************************************************
137 static VOID
CLOCK_ChooseFont(VOID
)
141 memset(&cf
, 0, sizeof(cf
));
142 lf
= Globals
.logfont
;
143 cf
.lStructSize
= sizeof(cf
);
144 cf
.hwndOwner
= Globals
.hMainWnd
;
146 cf
.Flags
= CF_SCREENFONTS
| CF_INITTOLOGFONTSTRUCT
| CF_NOVERTFONTS
;
147 if (ChooseFontW(&cf
)) {
148 Globals
.logfont
= lf
;
153 /***********************************************************************
157 static VOID
CLOCK_ToggleTitle(VOID
)
159 /* Also shows/hides the menu */
160 LONG style
= GetWindowLongW(Globals
.hMainWnd
, GWL_STYLE
);
161 if ((Globals
.bWithoutTitle
= !Globals
.bWithoutTitle
)) {
162 style
= (style
& ~WS_OVERLAPPEDWINDOW
) | WS_POPUP
|WS_THICKFRAME
;
163 SetMenu(Globals
.hMainWnd
, 0);
166 style
= (style
& ~(WS_POPUP
|WS_THICKFRAME
)) | WS_OVERLAPPEDWINDOW
;
167 SetMenu(Globals
.hMainWnd
, Globals
.hMainMenu
);
168 SetWindowRgn(Globals
.hMainWnd
, 0, TRUE
);
170 SetWindowLongW(Globals
.hMainWnd
, GWL_STYLE
, style
);
171 SetWindowPos(Globals
.hMainWnd
, 0,0,0,0,0,
172 SWP_DRAWFRAME
|SWP_NOMOVE
|SWP_NOSIZE
|SWP_NOZORDER
);
174 CLOCK_UpdateMenuCheckmarks();
175 CLOCK_UpdateWindowCaption();
178 /***********************************************************************
182 static VOID
CLOCK_ToggleOnTop(VOID
)
184 if ((Globals
.bAlwaysOnTop
= !Globals
.bAlwaysOnTop
)) {
185 SetWindowPos(Globals
.hMainWnd
, HWND_TOPMOST
, 0,0,0,0,
186 SWP_NOMOVE
|SWP_NOSIZE
);
189 SetWindowPos(Globals
.hMainWnd
, HWND_NOTOPMOST
, 0,0,0,0,
190 SWP_NOMOVE
|SWP_NOSIZE
);
192 CLOCK_UpdateMenuCheckmarks();
194 /***********************************************************************
198 * All handling of main menu events
201 static int CLOCK_MenuCommand (WPARAM wParam
)
203 WCHAR szApp
[MAX_STRING_LEN
];
204 WCHAR szAppRelease
[MAX_STRING_LEN
];
206 /* switch to analog */
208 Globals
.bAnalog
= TRUE
;
209 CLOCK_UpdateMenuCheckmarks();
211 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
214 /* switch to digital */
216 Globals
.bAnalog
= FALSE
;
217 CLOCK_UpdateMenuCheckmarks();
220 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
238 /* show or hide seconds */
240 Globals
.bSeconds
= !Globals
.bSeconds
;
241 CLOCK_UpdateMenuCheckmarks();
243 if (!Globals
.bAnalog
)
245 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
248 /* show or hide date */
250 Globals
.bDate
= !Globals
.bDate
;
251 CLOCK_UpdateMenuCheckmarks();
252 CLOCK_UpdateWindowCaption();
255 /* show "about" box */
257 LoadStringW(Globals
.hInstance
, IDS_CLOCK
, szApp
, ARRAY_SIZE(szApp
));
258 lstrcpyW(szAppRelease
,szApp
);
259 ShellAboutW(Globals
.hMainWnd
, szApp
, szAppRelease
, 0);
266 /***********************************************************************
270 static VOID
CLOCK_Paint(HWND hWnd
)
274 HBITMAP bmMem
, bmOld
;
276 dc
= BeginPaint(hWnd
, &ps
);
278 /* Use an offscreen dc to avoid flicker */
279 dcMem
= CreateCompatibleDC(dc
);
280 bmMem
= CreateCompatibleBitmap(dc
, ps
.rcPaint
.right
- ps
.rcPaint
.left
,
281 ps
.rcPaint
.bottom
- ps
.rcPaint
.top
);
283 bmOld
= SelectObject(dcMem
, bmMem
);
285 SetViewportOrgEx(dcMem
, -ps
.rcPaint
.left
, -ps
.rcPaint
.top
, NULL
);
286 /* Erase the background */
287 FillRect(dcMem
, &ps
.rcPaint
, GetSysColorBrush(COLOR_3DFACE
));
290 AnalogClock(dcMem
, Globals
.MaxX
, Globals
.MaxY
, Globals
.bSeconds
, Globals
.bWithoutTitle
);
292 DigitalClock(dcMem
, Globals
.MaxX
, Globals
.MaxY
, Globals
.bSeconds
, Globals
.hFont
);
294 /* Blit the changes to the screen */
296 ps
.rcPaint
.left
, ps
.rcPaint
.top
,
297 ps
.rcPaint
.right
- ps
.rcPaint
.left
, ps
.rcPaint
.bottom
- ps
.rcPaint
.top
,
299 ps
.rcPaint
.left
, ps
.rcPaint
.top
,
302 SelectObject(dcMem
, bmOld
);
309 /***********************************************************************
314 static LRESULT WINAPI
CLOCK_WndProc (HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
317 /* L button drag moves the window */
319 LRESULT ret
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
325 case WM_NCLBUTTONDBLCLK
:
326 case WM_LBUTTONDBLCLK
: {
338 Globals
.MaxX
= LOWORD(lParam
);
339 Globals
.MaxY
= HIWORD(lParam
);
340 if (Globals
.bAnalog
&& Globals
.bWithoutTitle
)
343 INT diameter
= min( Globals
.MaxX
, Globals
.MaxY
);
344 HRGN hrgn
= CreateEllipticRgn( (Globals
.MaxX
- diameter
) / 2,
345 (Globals
.MaxY
- diameter
) / 2,
346 (Globals
.MaxX
+ diameter
) / 2,
347 (Globals
.MaxY
+ diameter
) / 2 );
348 GetWindowRect( hWnd
, &rect
);
349 MapWindowPoints( 0, hWnd
, (LPPOINT
)&rect
, 2 );
350 OffsetRgn( hrgn
, -rect
.left
, -rect
.top
);
351 SetWindowRgn( Globals
.hMainWnd
, hrgn
, TRUE
);
358 CLOCK_MenuCommand(wParam
);
363 /* Could just invalidate what has changed,
364 * but it doesn't really seem worth the effort
366 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
376 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
382 /***********************************************************************
387 int PASCAL
WinMain (HINSTANCE hInstance
, HINSTANCE prev
, LPSTR cmdline
, int show
)
393 memset(&Globals
.hFont
, 0, sizeof (Globals
.hFont
));
394 Globals
.bAnalog
= TRUE
;
395 Globals
.bSeconds
= TRUE
;
398 class.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
399 class.lpfnWndProc
= CLOCK_WndProc
;
400 class.cbClsExtra
= 0;
401 class.cbWndExtra
= 0;
402 class.hInstance
= hInstance
;
403 class.hIcon
= LoadIconW(0, (LPCWSTR
)IDI_APPLICATION
);
404 class.hCursor
= LoadCursorW(0, (LPCWSTR
)IDC_ARROW
);
405 class.hbrBackground
= 0;
406 class.lpszMenuName
= 0;
407 class.lpszClassName
= L
"CLClass";
410 if (!RegisterClassW(&class)) return FALSE
;
412 Globals
.MaxX
= Globals
.MaxY
= INITIAL_WINDOW_SIZE
;
413 Globals
.hMainWnd
= CreateWindowW(L
"CLClass", L
"Clock", WS_OVERLAPPEDWINDOW
,
414 CW_USEDEFAULT
, CW_USEDEFAULT
,
415 Globals
.MaxX
, Globals
.MaxY
, 0,
418 if (!CLOCK_ResetTimer())
421 Globals
.hMainMenu
= LoadMenuW(0, MAKEINTRESOURCEW(MAIN_MENU
));
422 SetMenu(Globals
.hMainWnd
, Globals
.hMainMenu
);
423 CLOCK_UpdateMenuCheckmarks();
424 CLOCK_UpdateWindowCaption();
426 ShowWindow (Globals
.hMainWnd
, show
);
427 UpdateWindow (Globals
.hMainWnd
);
429 while (GetMessageW(&msg
, 0, 0, 0)) {
430 TranslateMessage(&msg
);
431 DispatchMessageW(&msg
);
434 KillTimer(Globals
.hMainWnd
, TIMER_ID
);
435 DeleteObject(Globals
.hFont
);