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 WineLicense(HWND Wnd)
41 WCHAR cap[20], text[1024];
42 LoadStringW(Globals.hInstance, IDS_LICENSE, text, sizeof(text)/sizeof(WCHAR));
43 LoadStringW(Globals.hInstance, IDS_LICENSE_CAPTION, cap, sizeof(cap)/sizeof(WCHAR));
44 MessageBoxW(Wnd, text, cap, MB_ICONINFORMATION | MB_OK);
47 static VOID WineWarranty(HWND Wnd)
49 WCHAR cap[20], text[1024];
50 LoadStringW(Globals.hInstance, IDS_WARRANTY, text, sizeof(text)/sizeof(WCHAR));
51 LoadStringW(Globals.hInstance, IDS_WARRANTY_CAPTION, cap, sizeof(cap)/sizeof(WCHAR));
52 MessageBoxW(Wnd, text, cap, MB_ICONEXCLAMATION | MB_OK);
55 static VOID CLOCK_UpdateMenuCheckmarks(VOID)
57 HMENU hPropertiesMenu;
58 hPropertiesMenu = GetSubMenu(Globals.hMainMenu, 0);
65 CheckMenuRadioItem(hPropertiesMenu, IDM_ANALOG, IDM_DIGITAL, IDM_ANALOG, MF_CHECKED);
66 EnableMenuItem(hPropertiesMenu, IDM_FONT, MF_GRAYED);
71 CheckMenuRadioItem(hPropertiesMenu, IDM_ANALOG, IDM_DIGITAL, IDM_DIGITAL, MF_CHECKED);
72 EnableMenuItem(hPropertiesMenu, IDM_FONT, 0);
75 CheckMenuItem(hPropertiesMenu, IDM_NOTITLE, (Globals.bWithoutTitle ? MF_CHECKED : MF_UNCHECKED));
77 CheckMenuItem(hPropertiesMenu, IDM_ONTOP, (Globals.bAlwaysOnTop ? MF_CHECKED : MF_UNCHECKED));
78 CheckMenuItem(hPropertiesMenu, IDM_SECONDS, (Globals.bSeconds ? MF_CHECKED : MF_UNCHECKED));
79 CheckMenuItem(hPropertiesMenu, IDM_DATE, (Globals.bDate ? MF_CHECKED : MF_UNCHECKED));
82 static VOID CLOCK_UpdateWindowCaption(VOID)
84 WCHAR szCaption[MAX_STRING_LEN];
87 /* Set frame caption */
89 chars = GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, NULL, NULL,
90 szCaption, sizeof(szCaption)/sizeof(WCHAR));
93 szCaption[chars++] = ' ';
94 szCaption[chars++] = '-';
95 szCaption[chars++] = ' ';
96 szCaption[chars] = '\0';
99 LoadStringW(0, IDS_CLOCK, szCaption + chars, MAX_STRING_LEN - chars);
100 SetWindowTextW(Globals.hMainWnd, szCaption);
103 /***********************************************************************
107 static BOOL CLOCK_ResetTimer(void)
109 UINT period; /* milliseconds */
111 KillTimer(Globals.hMainWnd, TIMER_ID);
113 if (Globals.bSeconds)
121 if (!SetTimer (Globals.hMainWnd, TIMER_ID, period, NULL)) {
122 static const WCHAR notimersW[] = {'N','o',' ','a','v','a','i','l','a','b','l','e',' ','t','i','m','e','r','s',0};
123 WCHAR szApp[MAX_STRING_LEN];
124 LoadStringW(Globals.hInstance, IDS_CLOCK, szApp, MAX_STRING_LEN);
125 MessageBoxW(0, notimersW, szApp, MB_ICONEXCLAMATION | MB_OK);
131 /***********************************************************************
135 static VOID CLOCK_ResetFont(VOID)
138 HDC dc = GetDC(Globals.hMainWnd);
139 newfont = SizeFont(dc, Globals.MaxX, Globals.MaxY, Globals.bSeconds, &Globals.logfont);
141 DeleteObject(Globals.hFont);
142 Globals.hFont = newfont;
145 ReleaseDC(Globals.hMainWnd, dc);
149 /***********************************************************************
153 static VOID CLOCK_ChooseFont(VOID)
157 memset(&cf, 0, sizeof(cf));
158 lf = Globals.logfont;
159 cf.lStructSize = sizeof(cf);
160 cf.hwndOwner = Globals.hMainWnd;
162 cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
163 if (ChooseFontW(&cf)) {
164 Globals.logfont = lf;
169 /***********************************************************************
173 static VOID CLOCK_ToggleTitle(VOID)
175 /* Also shows/hides the menu */
176 LONG style = GetWindowLongW(Globals.hMainWnd, GWL_STYLE);
177 if ((Globals.bWithoutTitle = !Globals.bWithoutTitle)) {
178 style = (style & ~WS_OVERLAPPEDWINDOW) | WS_POPUP|WS_THICKFRAME;
179 SetMenu(Globals.hMainWnd, 0);
182 style = (style & ~(WS_POPUP|WS_THICKFRAME)) | WS_OVERLAPPEDWINDOW;
183 SetMenu(Globals.hMainWnd, Globals.hMainMenu);
184 SetWindowRgn(Globals.hMainWnd, 0, TRUE);
186 SetWindowLongW(Globals.hMainWnd, GWL_STYLE, style);
187 SetWindowPos(Globals.hMainWnd, 0,0,0,0,0,
188 SWP_DRAWFRAME|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER);
190 CLOCK_UpdateMenuCheckmarks();
191 CLOCK_UpdateWindowCaption();
194 /***********************************************************************
198 static VOID CLOCK_ToggleOnTop(VOID)
200 if ((Globals.bAlwaysOnTop = !Globals.bAlwaysOnTop)) {
201 SetWindowPos(Globals.hMainWnd, HWND_TOPMOST, 0,0,0,0,
202 SWP_NOMOVE|SWP_NOSIZE);
205 SetWindowPos(Globals.hMainWnd, HWND_NOTOPMOST, 0,0,0,0,
206 SWP_NOMOVE|SWP_NOSIZE);
208 CLOCK_UpdateMenuCheckmarks();
210 /***********************************************************************
214 * All handling of main menu events
217 static int CLOCK_MenuCommand (WPARAM wParam)
219 WCHAR szApp[MAX_STRING_LEN];
220 WCHAR szAppRelease[MAX_STRING_LEN];
222 /* switch to analog */
224 Globals.bAnalog = TRUE;
225 CLOCK_UpdateMenuCheckmarks();
227 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
230 /* switch to digital */
232 Globals.bAnalog = FALSE;
233 CLOCK_UpdateMenuCheckmarks();
236 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
254 /* show or hide seconds */
256 Globals.bSeconds = !Globals.bSeconds;
257 CLOCK_UpdateMenuCheckmarks();
259 if (!Globals.bAnalog)
261 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
264 /* show or hide date */
266 Globals.bDate = !Globals.bDate;
267 CLOCK_UpdateMenuCheckmarks();
268 CLOCK_UpdateWindowCaption();
273 WineLicense(Globals.hMainWnd);
276 /* show warranties */
277 case IDM_NOWARRANTY: {
278 WineWarranty(Globals.hMainWnd);
281 /* show "about" box */
283 LoadStringW(Globals.hInstance, IDS_CLOCK, szApp, sizeof(szApp)/sizeof(WCHAR));
284 lstrcpyW(szAppRelease,szApp);
285 ShellAboutW(Globals.hMainWnd, szApp, szAppRelease, 0);
292 /***********************************************************************
296 static VOID CLOCK_Paint(HWND hWnd)
300 HBITMAP bmMem, bmOld;
302 dc = BeginPaint(hWnd, &ps);
304 /* Use an offscreen dc to avoid flicker */
305 dcMem = CreateCompatibleDC(dc);
306 bmMem = CreateCompatibleBitmap(dc, ps.rcPaint.right - ps.rcPaint.left,
307 ps.rcPaint.bottom - ps.rcPaint.top);
309 bmOld = SelectObject(dcMem, bmMem);
311 SetViewportOrgEx(dcMem, -ps.rcPaint.left, -ps.rcPaint.top, NULL);
312 /* Erase the background */
313 FillRect(dcMem, &ps.rcPaint, GetSysColorBrush(COLOR_3DFACE));
316 AnalogClock(dcMem, Globals.MaxX, Globals.MaxY, Globals.bSeconds, Globals.bWithoutTitle);
318 DigitalClock(dcMem, Globals.MaxX, Globals.MaxY, Globals.bSeconds, Globals.hFont);
320 /* Blit the changes to the screen */
322 ps.rcPaint.left, ps.rcPaint.top,
323 ps.rcPaint.right - ps.rcPaint.left, ps.rcPaint.bottom - ps.rcPaint.top,
325 ps.rcPaint.left, ps.rcPaint.top,
328 SelectObject(dcMem, bmOld);
335 /***********************************************************************
340 static LRESULT WINAPI CLOCK_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
343 /* L button drag moves the window */
345 LRESULT ret = DefWindowProcW(hWnd, msg, wParam, lParam);
351 case WM_NCLBUTTONDBLCLK:
352 case WM_LBUTTONDBLCLK: {
364 Globals.MaxX = LOWORD(lParam);
365 Globals.MaxY = HIWORD(lParam);
366 if (Globals.bAnalog && Globals.bWithoutTitle)
369 INT diameter = min( Globals.MaxX, Globals.MaxY );
370 HRGN hrgn = CreateEllipticRgn( (Globals.MaxX - diameter) / 2,
371 (Globals.MaxY - diameter) / 2,
372 (Globals.MaxX + diameter) / 2,
373 (Globals.MaxY + diameter) / 2 );
374 GetWindowRect( hWnd, &rect );
375 MapWindowPoints( 0, hWnd, (LPPOINT)&rect, 2 );
376 OffsetRgn( hrgn, -rect.left, -rect.top );
377 SetWindowRgn( Globals.hMainWnd, hrgn, TRUE );
384 CLOCK_MenuCommand(wParam);
389 /* Could just invalidate what has changed,
390 * but it doesn't really seem worth the effort
392 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
402 return DefWindowProcW(hWnd, msg, wParam, lParam);
408 /***********************************************************************
413 int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
418 static const WCHAR szClassName[] = {'C','L','C','l','a','s','s',0};
419 static const WCHAR szWinName[] = {'C','l','o','c','k',0};
422 memset(&Globals.hFont, 0, sizeof (Globals.hFont));
423 Globals.bAnalog = TRUE;
424 Globals.bSeconds = TRUE;
427 class.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
428 class.lpfnWndProc = CLOCK_WndProc;
429 class.cbClsExtra = 0;
430 class.cbWndExtra = 0;
431 class.hInstance = hInstance;
432 class.hIcon = LoadIconW(0, (LPCWSTR)IDI_APPLICATION);
433 class.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
434 class.hbrBackground = 0;
435 class.lpszMenuName = 0;
436 class.lpszClassName = szClassName;
439 if (!RegisterClassW(&class)) return FALSE;
441 Globals.MaxX = Globals.MaxY = INITIAL_WINDOW_SIZE;
442 Globals.hMainWnd = CreateWindowW(szClassName, szWinName, WS_OVERLAPPEDWINDOW,
443 CW_USEDEFAULT, CW_USEDEFAULT,
444 Globals.MaxX, Globals.MaxY, 0,
447 if (!CLOCK_ResetTimer())
450 Globals.hMainMenu = LoadMenuW(0, MAKEINTRESOURCEW(MAIN_MENU));
451 SetMenu(Globals.hMainWnd, Globals.hMainMenu);
452 CLOCK_UpdateMenuCheckmarks();
453 CLOCK_UpdateWindowCaption();
455 ShowWindow (Globals.hMainWnd, show);
456 UpdateWindow (Globals.hMainWnd);
458 while (GetMessageW(&msg, 0, 0, 0)) {
459 TranslateMessage(&msg);
460 DispatchMessageW(&msg);
463 KillTimer(Globals.hMainWnd, TIMER_ID);
464 DeleteObject(Globals.hFont);