oleaut32: Check the return value from VariantCopy when copying values
[wine/multimedia.git] / programs / clock / main.c
blobcd6a7722b48c3b44cd79e1421e46b0ca2d18b285
1 /*
2 * Clock
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <stdio.h>
28 #include "windows.h"
29 #include "commdlg.h"
31 #include "main.h"
32 #include "winclock.h"
34 #define INITIAL_WINDOW_SIZE 200
35 #define TIMER_ID 1
37 CLOCK_GLOBALS Globals;
39 static VOID WineLicense(HWND Wnd)
41 char cap[20], text[1024];
42 LoadString(Globals.hInstance, IDS_LICENSE, text, sizeof text);
43 LoadString(Globals.hInstance, IDS_LICENSE_CAPTION, cap, sizeof cap);
44 MessageBox(Wnd, text, cap, MB_ICONINFORMATION | MB_OK);
47 static VOID WineWarranty(HWND Wnd)
49 char cap[20], text[1024];
50 LoadString(Globals.hInstance, IDS_WARRANTY, text, sizeof text);
51 LoadString(Globals.hInstance, IDS_WARRANTY_CAPTION, cap, sizeof cap);
52 MessageBox(Wnd, text, cap, MB_ICONEXCLAMATION | MB_OK);
55 static VOID CLOCK_UpdateMenuCheckmarks(VOID)
57 HMENU hPropertiesMenu;
58 hPropertiesMenu = GetSubMenu(Globals.hMainMenu, 0);
59 if (!hPropertiesMenu)
60 return;
62 if(Globals.bAnalog) {
64 /* analog clock */
65 CheckMenuRadioItem(hPropertiesMenu, IDM_ANALOG, IDM_DIGITAL, IDM_ANALOG, MF_CHECKED);
66 EnableMenuItem(hPropertiesMenu, IDM_FONT, MF_GRAYED);
68 else
70 /* digital clock */
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 CHAR szCaption[MAX_STRING_LEN];
85 int chars = 0;
87 /* Set frame caption */
88 if (Globals.bDate) {
89 chars = GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, NULL, NULL,
90 szCaption, sizeof(szCaption));
91 if (chars) {
92 --chars;
93 szCaption[chars++] = ' ';
94 szCaption[chars++] = '-';
95 szCaption[chars++] = ' ';
96 szCaption[chars] = '\0';
99 LoadString(0, IDS_CLOCK, szCaption + chars, sizeof(szCaption) - chars);
100 SetWindowText(Globals.hMainWnd, szCaption);
103 /***********************************************************************
105 * CLOCK_ResetTimer
107 static BOOL CLOCK_ResetTimer(void)
109 UINT period; /* milliseconds */
111 KillTimer(Globals.hMainWnd, TIMER_ID);
113 if (Globals.bSeconds)
114 if (Globals.bAnalog)
115 period = 50;
116 else
117 period = 500;
118 else
119 period = 1000;
121 if (!SetTimer (Globals.hMainWnd, TIMER_ID, period, NULL)) {
122 CHAR szApp[MAX_STRING_LEN];
123 LoadString(Globals.hInstance, IDS_CLOCK, szApp, sizeof(szApp));
124 MessageBox(0, "No available timers", szApp, MB_ICONEXCLAMATION | MB_OK);
125 return FALSE;
127 return TRUE;
130 /***********************************************************************
132 * CLOCK_ResetFont
134 static VOID CLOCK_ResetFont(VOID)
136 HFONT newfont;
137 HDC dc = GetDC(Globals.hMainWnd);
138 newfont = SizeFont(dc, Globals.MaxX, Globals.MaxY, Globals.bSeconds, &Globals.logfont);
139 if (newfont) {
140 DeleteObject(Globals.hFont);
141 Globals.hFont = newfont;
144 ReleaseDC(Globals.hMainWnd, dc);
148 /***********************************************************************
150 * CLOCK_ChooseFont
152 static VOID CLOCK_ChooseFont(VOID)
154 LOGFONT lf;
155 CHOOSEFONT cf;
156 memset(&cf, 0, sizeof(cf));
157 lf = Globals.logfont;
158 cf.lStructSize = sizeof(cf);
159 cf.hwndOwner = Globals.hMainWnd;
160 cf.lpLogFont = &lf;
161 cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
162 if (ChooseFont(&cf)) {
163 Globals.logfont = lf;
164 CLOCK_ResetFont();
168 /***********************************************************************
170 * CLOCK_ToggleTitle
172 static VOID CLOCK_ToggleTitle(VOID)
174 /* Also shows/hides the menu */
175 LONG style = GetWindowLong(Globals.hMainWnd, GWL_STYLE);
176 if ((Globals.bWithoutTitle = !Globals.bWithoutTitle)) {
177 style = (style & ~WS_OVERLAPPEDWINDOW) | WS_POPUP|WS_THICKFRAME;
178 SetMenu(Globals.hMainWnd, 0);
180 else {
181 style = (style & ~(WS_POPUP|WS_THICKFRAME)) | WS_OVERLAPPEDWINDOW;
182 SetMenu(Globals.hMainWnd, Globals.hMainMenu);
184 SetWindowLong(Globals.hMainWnd, GWL_STYLE, style);
185 SetWindowPos(Globals.hMainWnd, 0,0,0,0,0,
186 SWP_DRAWFRAME|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER);
188 CLOCK_UpdateMenuCheckmarks();
189 CLOCK_UpdateWindowCaption();
192 /***********************************************************************
194 * CLOCK_ToggleOnTop
196 static VOID CLOCK_ToggleOnTop(VOID)
198 if ((Globals.bAlwaysOnTop = !Globals.bAlwaysOnTop)) {
199 SetWindowPos(Globals.hMainWnd, HWND_TOPMOST, 0,0,0,0,
200 SWP_NOMOVE|SWP_NOSIZE);
202 else {
203 SetWindowPos(Globals.hMainWnd, HWND_NOTOPMOST, 0,0,0,0,
204 SWP_NOMOVE|SWP_NOSIZE);
206 CLOCK_UpdateMenuCheckmarks();
208 /***********************************************************************
210 * CLOCK_MenuCommand
212 * All handling of main menu events
215 static int CLOCK_MenuCommand (WPARAM wParam)
217 CHAR szApp[MAX_STRING_LEN];
218 CHAR szAppRelease[MAX_STRING_LEN];
219 switch (wParam) {
220 /* switch to analog */
221 case IDM_ANALOG: {
222 Globals.bAnalog = TRUE;
223 CLOCK_UpdateMenuCheckmarks();
224 CLOCK_ResetTimer();
225 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
226 break;
228 /* switch to digital */
229 case IDM_DIGITAL: {
230 Globals.bAnalog = FALSE;
231 CLOCK_UpdateMenuCheckmarks();
232 CLOCK_ResetTimer();
233 CLOCK_ResetFont();
234 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
235 break;
237 /* change font */
238 case IDM_FONT: {
239 CLOCK_ChooseFont();
240 break;
242 /* hide title bar */
243 case IDM_NOTITLE: {
244 CLOCK_ToggleTitle();
245 break;
247 /* always on top */
248 case IDM_ONTOP: {
249 CLOCK_ToggleOnTop();
250 break;
252 /* show or hide seconds */
253 case IDM_SECONDS: {
254 Globals.bSeconds = !Globals.bSeconds;
255 CLOCK_UpdateMenuCheckmarks();
256 CLOCK_ResetTimer();
257 if (!Globals.bAnalog)
258 CLOCK_ResetFont();
259 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
260 break;
262 /* show or hide date */
263 case IDM_DATE: {
264 Globals.bDate = !Globals.bDate;
265 CLOCK_UpdateMenuCheckmarks();
266 CLOCK_UpdateWindowCaption();
267 break;
269 /* show license */
270 case IDM_LICENSE: {
271 WineLicense(Globals.hMainWnd);
272 break;
274 /* show warranties */
275 case IDM_NOWARRANTY: {
276 WineWarranty(Globals.hMainWnd);
277 break;
279 /* show "about" box */
280 case IDM_ABOUT: {
281 LoadString(Globals.hInstance, IDS_CLOCK, szApp, sizeof(szApp));
282 lstrcpy(szAppRelease,szApp);
283 ShellAbout(Globals.hMainWnd, szApp, szAppRelease, 0);
284 break;
287 return 0;
290 /***********************************************************************
292 * CLOCK_Paint
294 static VOID CLOCK_Paint(HWND hWnd)
296 PAINTSTRUCT ps;
297 HDC dcMem, dc;
298 HBITMAP bmMem, bmOld;
300 dc = BeginPaint(hWnd, &ps);
302 /* Use an offscreen dc to avoid flicker */
303 dcMem = CreateCompatibleDC(dc);
304 bmMem = CreateCompatibleBitmap(dc, ps.rcPaint.right - ps.rcPaint.left,
305 ps.rcPaint.bottom - ps.rcPaint.top);
307 bmOld = SelectObject(dcMem, bmMem);
309 SetViewportOrgEx(dcMem, -ps.rcPaint.left, -ps.rcPaint.top, NULL);
310 /* Erase the background */
311 FillRect(dcMem, &ps.rcPaint, GetStockObject(LTGRAY_BRUSH));
313 if(Globals.bAnalog)
314 AnalogClock(dcMem, Globals.MaxX, Globals.MaxY, Globals.bSeconds);
315 else
316 DigitalClock(dcMem, Globals.MaxX, Globals.MaxY, Globals.bSeconds, Globals.hFont);
318 /* Blit the changes to the screen */
319 BitBlt(dc,
320 ps.rcPaint.left, ps.rcPaint.top,
321 ps.rcPaint.right - ps.rcPaint.left, ps.rcPaint.bottom - ps.rcPaint.top,
322 dcMem,
323 ps.rcPaint.left, ps.rcPaint.top,
324 SRCCOPY);
326 SelectObject(dcMem, bmOld);
327 DeleteObject(bmMem);
328 DeleteDC(dcMem);
330 EndPaint(hWnd, &ps);
333 /***********************************************************************
335 * CLOCK_WndProc
338 static LRESULT WINAPI CLOCK_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
340 switch (msg) {
341 /* L button drag moves the window */
342 case WM_NCHITTEST: {
343 LRESULT ret = DefWindowProc (hWnd, msg, wParam, lParam);
344 if (ret == HTCLIENT)
345 ret = HTCAPTION;
346 return ret;
349 case WM_NCLBUTTONDBLCLK:
350 case WM_LBUTTONDBLCLK: {
351 CLOCK_ToggleTitle();
352 break;
355 case WM_PAINT: {
356 CLOCK_Paint(hWnd);
357 break;
361 case WM_SIZE: {
362 Globals.MaxX = LOWORD(lParam);
363 Globals.MaxY = HIWORD(lParam);
364 CLOCK_ResetFont();
365 break;
368 case WM_COMMAND: {
369 CLOCK_MenuCommand(wParam);
370 break;
373 case WM_TIMER: {
374 /* Could just invalidate what has changed,
375 * but it doesn't really seem worth the effort
377 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
378 break;
381 case WM_DESTROY: {
382 PostQuitMessage (0);
383 break;
386 default:
387 return DefWindowProc (hWnd, msg, wParam, lParam);
389 return 0;
393 /***********************************************************************
395 * WinMain
398 int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
400 MSG msg;
401 WNDCLASS class;
403 static const char szClassName[] = "CLClass"; /* To make sure className >= 0x10000 */
404 static const char szWinName[] = "Clock";
406 /* Setup Globals */
407 memset(&Globals.hFont, 0, sizeof (Globals.hFont));
408 Globals.bAnalog = TRUE;
409 Globals.bSeconds = TRUE;
411 if (!prev){
412 class.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
413 class.lpfnWndProc = CLOCK_WndProc;
414 class.cbClsExtra = 0;
415 class.cbWndExtra = 0;
416 class.hInstance = hInstance;
417 class.hIcon = LoadIcon (0, IDI_APPLICATION);
418 class.hCursor = LoadCursor (0, IDC_ARROW);
419 class.hbrBackground = 0;
420 class.lpszMenuName = 0;
421 class.lpszClassName = szClassName;
424 if (!RegisterClass (&class)) return FALSE;
426 Globals.MaxX = Globals.MaxY = INITIAL_WINDOW_SIZE;
427 Globals.hMainWnd = CreateWindow (szClassName, szWinName, WS_OVERLAPPEDWINDOW,
428 CW_USEDEFAULT, CW_USEDEFAULT,
429 Globals.MaxX, Globals.MaxY, 0,
430 0, hInstance, 0);
432 if (!CLOCK_ResetTimer())
433 return FALSE;
435 Globals.hMainMenu = LoadMenu(0, MAKEINTRESOURCE(MAIN_MENU));
436 SetMenu(Globals.hMainWnd, Globals.hMainMenu);
437 CLOCK_UpdateMenuCheckmarks();
438 CLOCK_UpdateWindowCaption();
440 ShowWindow (Globals.hMainWnd, show);
441 UpdateWindow (Globals.hMainWnd);
443 while (GetMessage(&msg, 0, 0, 0)) {
444 TranslateMessage(&msg);
445 DispatchMessage(&msg);
448 KillTimer(Globals.hMainWnd, TIMER_ID);
449 DeleteObject(Globals.hFont);
451 return 0;