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
34 #define INITIAL_WINDOW_SIZE 200
37 CLOCK_GLOBALS Globals
;
39 static VOID
CLOCK_UpdateMenuCheckmarks(VOID
)
41 HMENU hPropertiesMenu
;
42 hPropertiesMenu
= GetSubMenu(Globals
.hMainMenu
, 0);
49 CheckMenuRadioItem(hPropertiesMenu
, IDM_ANALOG
, IDM_DIGITAL
, IDM_ANALOG
, MF_CHECKED
);
50 EnableMenuItem(hPropertiesMenu
, IDM_FONT
, MF_GRAYED
);
55 CheckMenuRadioItem(hPropertiesMenu
, IDM_ANALOG
, IDM_DIGITAL
, IDM_DIGITAL
, MF_CHECKED
);
56 EnableMenuItem(hPropertiesMenu
, IDM_FONT
, 0);
59 CheckMenuItem(hPropertiesMenu
, IDM_NOTITLE
, (Globals
.bWithoutTitle
? MF_CHECKED
: MF_UNCHECKED
));
61 CheckMenuItem(hPropertiesMenu
, IDM_ONTOP
, (Globals
.bAlwaysOnTop
? MF_CHECKED
: MF_UNCHECKED
));
62 CheckMenuItem(hPropertiesMenu
, IDM_SECONDS
, (Globals
.bSeconds
? MF_CHECKED
: MF_UNCHECKED
));
63 CheckMenuItem(hPropertiesMenu
, IDM_DATE
, (Globals
.bDate
? MF_CHECKED
: MF_UNCHECKED
));
66 static VOID
CLOCK_UpdateWindowCaption(VOID
)
68 WCHAR szCaption
[MAX_STRING_LEN
];
71 /* Set frame caption */
73 chars
= GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_LONGDATE
, NULL
, NULL
,
74 szCaption
, sizeof(szCaption
)/sizeof(WCHAR
));
77 szCaption
[chars
++] = ' ';
78 szCaption
[chars
++] = '-';
79 szCaption
[chars
++] = ' ';
80 szCaption
[chars
] = '\0';
83 LoadStringW(0, IDS_CLOCK
, szCaption
+ chars
, MAX_STRING_LEN
- chars
);
84 SetWindowTextW(Globals
.hMainWnd
, szCaption
);
87 /***********************************************************************
91 static BOOL
CLOCK_ResetTimer(void)
93 UINT period
; /* milliseconds */
95 KillTimer(Globals
.hMainWnd
, TIMER_ID
);
105 if (!SetTimer (Globals
.hMainWnd
, TIMER_ID
, period
, NULL
)) {
106 static const WCHAR notimersW
[] = {'N','o',' ','a','v','a','i','l','a','b','l','e',' ','t','i','m','e','r','s',0};
107 WCHAR szApp
[MAX_STRING_LEN
];
108 LoadStringW(Globals
.hInstance
, IDS_CLOCK
, szApp
, MAX_STRING_LEN
);
109 MessageBoxW(0, notimersW
, 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
;
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
, sizeof(szApp
)/sizeof(WCHAR
));
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
)
392 static const WCHAR szClassName
[] = {'C','L','C','l','a','s','s',0};
393 static const WCHAR szWinName
[] = {'C','l','o','c','k',0};
396 memset(&Globals
.hFont
, 0, sizeof (Globals
.hFont
));
397 Globals
.bAnalog
= TRUE
;
398 Globals
.bSeconds
= TRUE
;
401 class.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
402 class.lpfnWndProc
= CLOCK_WndProc
;
403 class.cbClsExtra
= 0;
404 class.cbWndExtra
= 0;
405 class.hInstance
= hInstance
;
406 class.hIcon
= LoadIconW(0, (LPCWSTR
)IDI_APPLICATION
);
407 class.hCursor
= LoadCursorW(0, (LPCWSTR
)IDC_ARROW
);
408 class.hbrBackground
= 0;
409 class.lpszMenuName
= 0;
410 class.lpszClassName
= szClassName
;
413 if (!RegisterClassW(&class)) return FALSE
;
415 Globals
.MaxX
= Globals
.MaxY
= INITIAL_WINDOW_SIZE
;
416 Globals
.hMainWnd
= CreateWindowW(szClassName
, szWinName
, WS_OVERLAPPEDWINDOW
,
417 CW_USEDEFAULT
, CW_USEDEFAULT
,
418 Globals
.MaxX
, Globals
.MaxY
, 0,
421 if (!CLOCK_ResetTimer())
424 Globals
.hMainMenu
= LoadMenuW(0, MAKEINTRESOURCEW(MAIN_MENU
));
425 SetMenu(Globals
.hMainWnd
, Globals
.hMainMenu
);
426 CLOCK_UpdateMenuCheckmarks();
427 CLOCK_UpdateWindowCaption();
429 ShowWindow (Globals
.hMainWnd
, show
);
430 UpdateWindow (Globals
.hMainWnd
);
432 while (GetMessageW(&msg
, 0, 0, 0)) {
433 TranslateMessage(&msg
);
434 DispatchMessageW(&msg
);
437 KillTimer(Globals
.hMainWnd
, TIMER_ID
);
438 DeleteObject(Globals
.hFont
);