Add leftover from sync, fixes build.
[reactos.git] / base / applications / games / solitaire / solitaire.cpp
1 #include <windows.h>
2 #include <commctrl.h>
3 #include <tchar.h>
4 #include <stdlib.h>
5
6 #include "resource.h"
7 #include "cardlib.h"
8
9 #include "solitaire.h"
10
11 TCHAR szHelpPath[MAX_PATH];
12
13 DWORD dwAppStartTime;
14 HWND hwndMain;
15 HWND hwndStatus;
16 HINSTANCE hInstance;
17
18 TCHAR szAppName[128];
19 TCHAR MsgQuit[128];
20 TCHAR MsgAbout[128];
21 TCHAR MsgWin[128];
22 TCHAR MsgDeal[128];
23 DWORD dwOptions = OPTION_THREE_CARDS;
24
25 CardWindow SolWnd;
26
27 typedef struct _CardBack
28 {
29 HWND hSelf;
30 WNDPROC hOldProc;
31 INT hdcNum;
32 INT imgNum;
33 BOOL bSelected;
34 } CARDBACK, *PCARDBACK;
35
36 LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
37
38 void MakePath(TCHAR *szDest, UINT nDestLen, const TCHAR *szExt)
39 {
40 TCHAR *ptr;
41
42 ptr = szDest + GetModuleFileName(GetModuleHandle(0), szDest, nDestLen) - 1;
43 while(*ptr-- != '.');
44 lstrcpy(ptr + 1, szExt);
45 }
46
47 VOID LoadSettings(VOID)
48 {
49 DWORD dwDisposition;
50 DWORD dwSize;
51 DWORD dwBack;
52 HKEY hKey;
53
54 if (RegCreateKeyEx(HKEY_CURRENT_USER,
55 _T("Software\\ReactOS\\Solitaire"),
56 0,
57 NULL,
58 REG_OPTION_NON_VOLATILE,
59 KEY_READ,
60 NULL,
61 &hKey,
62 &dwDisposition))
63 return;
64
65 dwSize = sizeof(DWORD);
66 RegQueryValueEx(hKey,
67 _T("Options"),
68 NULL,
69 NULL,
70 (LPBYTE)&dwOptions,
71 &dwSize);
72
73 dwSize = sizeof(DWORD);
74 RegQueryValueEx(hKey,
75 _T("Back"),
76 NULL,
77 NULL,
78 (LPBYTE)&dwBack,
79 &dwSize);
80 SolWnd.SetBackCardIdx(dwBack);
81
82 RegCloseKey(hKey);
83 }
84
85 VOID SaveSettings(VOID)
86 {
87 DWORD dwDisposition;
88 DWORD dwBack;
89 HKEY hKey;
90
91 if (RegCreateKeyEx(HKEY_CURRENT_USER,
92 _T("Software\\ReactOS\\Solitaire"),
93 0,
94 NULL,
95 REG_OPTION_NON_VOLATILE,
96 KEY_WRITE,
97 NULL,
98 &hKey,
99 &dwDisposition))
100 return;
101
102 RegSetValueEx(hKey,
103 _T("Options"),
104 0,
105 REG_DWORD,
106 (CONST BYTE *)&dwOptions,
107 sizeof(DWORD));
108
109 dwBack = SolWnd.GetBackCardIdx();
110 RegSetValueEx(hKey,
111 _T("Back"),
112 0,
113 REG_DWORD,
114 (CONST BYTE *)&dwBack,
115 sizeof(DWORD));
116
117 RegCloseKey(hKey);
118 }
119
120 //
121 // Main entry point
122 //
123 int WINAPI _tWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int iCmdShow)
124 {
125 HWND hwnd;
126 MSG msg;
127 WNDCLASS wndclass;
128 INITCOMMONCONTROLSEX ice;
129 HACCEL hAccelTable;
130
131 hInstance = hInst;
132
133 // Load application title
134 LoadString(hInst, IDS_SOL_NAME, szAppName, sizeof(szAppName) / sizeof(szAppName[0]));
135 // Load MsgBox() texts here to avoid loading them many times later
136 LoadString(hInst, IDS_SOL_ABOUT, MsgAbout, sizeof(MsgAbout) / sizeof(MsgAbout[0]));
137 LoadString(hInst, IDS_SOL_QUIT, MsgQuit, sizeof(MsgQuit) / sizeof(MsgQuit[0]));
138 LoadString(hInst, IDS_SOL_WIN, MsgWin, sizeof(MsgWin) / sizeof(MsgWin[0]));
139 LoadString(hInst, IDS_SOL_DEAL, MsgDeal, sizeof(MsgDeal) / sizeof(MsgDeal[0]));
140
141 //Window class for the main application parent window
142 wndclass.style = 0;//CS_HREDRAW | CS_VREDRAW;
143 wndclass.lpfnWndProc = WndProc;
144 wndclass.cbClsExtra = 0;
145 wndclass.cbWndExtra = 0;
146 wndclass.hInstance = hInst;
147 wndclass.hIcon = LoadIcon (hInst, MAKEINTRESOURCE(IDI_SOLITAIRE));
148 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
149 wndclass.hbrBackground = (HBRUSH)NULL;
150 wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
151 wndclass.lpszClassName = szAppName;
152
153 RegisterClass(&wndclass);
154
155 ice.dwSize = sizeof(ice);
156 ice.dwICC = ICC_BAR_CLASSES;
157 InitCommonControlsEx(&ice);
158
159 srand((unsigned)GetTickCount());//timeGetTime());
160
161 // InitCardLib();
162
163 LoadSettings();
164
165 //Construct the path to our help file
166 MakePath(szHelpPath, MAX_PATH, _T(".hlp"));
167
168 hwnd = CreateWindow(szAppName, // window class name
169 szAppName, // window caption
170 WS_OVERLAPPEDWINDOW
171 ,//|WS_CLIPCHILDREN, // window style
172 CW_USEDEFAULT, // initial x position
173 CW_USEDEFAULT, // initial y position
174 0, // The real size will be computed in WndProc through WM_GETMINMAXINFO
175 0, // The real size will be computed in WndProc through WM_GETMINMAXINFO
176 NULL, // parent window handle
177 NULL, // use window class menu
178 hInst, // program instance handle
179 NULL); // creation parameters
180
181 hwndMain = hwnd;
182
183 ShowWindow(hwnd, iCmdShow);
184 UpdateWindow(hwnd);
185
186 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1));
187
188 while(GetMessage(&msg, NULL,0,0))
189 {
190 if(!TranslateAccelerator(hwnd, hAccelTable, &msg))
191 {
192 TranslateMessage(&msg);
193 DispatchMessage(&msg);
194 }
195 }
196
197 SaveSettings();
198
199 return msg.wParam;
200 }
201
202
203 INT_PTR CALLBACK OptionsDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
204 {
205 switch (uMsg)
206 {
207 case WM_INITDIALOG:
208 CheckRadioButton(hDlg, IDC_OPT_DRAWONE, IDC_OPT_DRAWTHREE,
209 (dwOptions & OPTION_THREE_CARDS) ? IDC_OPT_DRAWTHREE : IDC_OPT_DRAWONE);
210
211 CheckDlgButton(hDlg,
212 IDC_OPT_STATUSBAR,
213 (dwOptions & OPTION_SHOW_STATUS) ? BST_CHECKED : BST_UNCHECKED);
214 return TRUE;
215
216 case WM_COMMAND:
217 switch(LOWORD(wParam))
218 {
219 case IDOK:
220 dwOptions &= ~OPTION_THREE_CARDS;
221 if (IsDlgButtonChecked(hDlg, IDC_OPT_DRAWTHREE) == BST_CHECKED)
222 dwOptions |= OPTION_THREE_CARDS;
223
224 if (IsDlgButtonChecked(hDlg, IDC_OPT_STATUSBAR) == BST_CHECKED)
225 dwOptions |= OPTION_SHOW_STATUS;
226 else
227 dwOptions &= ~OPTION_SHOW_STATUS;
228
229 EndDialog(hDlg, TRUE);
230 return TRUE;
231
232 case IDCANCEL:
233 EndDialog(hDlg, FALSE);
234 return TRUE;
235 }
236 break;
237 }
238 return FALSE;
239 }
240
241 VOID ShowGameOptionsDlg(HWND hwnd)
242 {
243 DWORD dwOldOptions = dwOptions;
244 RECT rcMain, rcStatus;
245 int nWidth, nHeight, nStatusHeight;
246
247 if (DialogBox(hInstance, MAKEINTRESOURCE(IDD_OPTIONS), hwnd, OptionsDlgProc))
248 {
249 if ((dwOldOptions & OPTION_THREE_CARDS) != (dwOptions & OPTION_THREE_CARDS))
250 NewGame();
251
252 if ((dwOldOptions & OPTION_SHOW_STATUS) != (dwOptions & OPTION_SHOW_STATUS))
253 {
254 GetClientRect(hwndMain, &rcMain);
255 nHeight = rcMain.bottom - rcMain.top;
256 nWidth = rcMain.right - rcMain.left;
257
258 if (dwOptions & OPTION_SHOW_STATUS)
259 {
260 RECT rc;
261
262 ShowWindow(hwndStatus, SW_SHOW);
263 GetWindowRect(hwndStatus, &rcStatus);
264 nStatusHeight = rcStatus.bottom - rcStatus.top;
265 MoveWindow(SolWnd, 0, 0, nWidth, nHeight-nStatusHeight, TRUE);
266 MoveWindow(hwndStatus, 0, nHeight-nStatusHeight, nWidth, nHeight, TRUE);
267
268 // Force the window to process WM_GETMINMAXINFO again
269 GetWindowRect(hwndMain, &rc);
270 SetWindowPos(hwndMain, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER);
271 }
272 else
273 {
274 ShowWindow(hwndStatus, SW_HIDE);
275 MoveWindow(SolWnd, 0, 0, nWidth, nHeight, TRUE);
276 }
277 }
278 }
279 }
280
281
282 LRESULT CALLBACK
283 CardImageWndProc(HWND hwnd,
284 UINT msg,
285 WPARAM wParam,
286 LPARAM lParam)
287 {
288 PCARDBACK pCardBack = (PCARDBACK)GetWindowLongPtr(hwnd,
289 GWL_USERDATA);
290 static WNDPROC hOldProc = NULL;
291
292 if (!hOldProc && pCardBack)
293 hOldProc = pCardBack->hOldProc;
294
295 switch (msg)
296 {
297 case WM_PAINT:
298 {
299 HDC hdc;
300 PAINTSTRUCT ps;
301 HPEN hPen, hOldPen;
302 HBRUSH hBrush, hOldBrush;
303 RECT rc;
304
305 hdc = BeginPaint(hwnd, &ps);
306
307 if (pCardBack->bSelected)
308 {
309 hPen = CreatePen(PS_SOLID, 2, RGB(0,0,0));
310 }
311 else
312 {
313 DWORD Face = GetSysColor(COLOR_3DFACE);
314 hPen = CreatePen(PS_SOLID, 2, Face);
315 }
316
317 GetClientRect(hwnd, &rc);
318 hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
319 hOldPen = (HPEN)SelectObject(hdc, hPen);
320 hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
321
322 Rectangle(hdc,
323 rc.left+1,
324 rc.top+1,
325 rc.right,
326 rc.bottom);
327
328 StretchBlt(hdc,
329 2,
330 2,
331 CARDBACK_OPTIONS_WIDTH,
332 CARDBACK_OPTIONS_HEIGHT,
333 __hdcCardBitmaps,
334 pCardBack->hdcNum * __cardwidth,
335 0,
336 __cardwidth,
337 __cardheight,
338 SRCCOPY);
339
340 SelectObject(hdc, hOldPen);
341 SelectObject(hdc, hOldBrush);
342
343 EndPaint(hwnd, &ps);
344
345 break;
346 }
347
348 case WM_LBUTTONDOWN:
349 pCardBack->bSelected = pCardBack->bSelected ? FALSE : TRUE;
350 break;
351 }
352
353 return CallWindowProc(hOldProc,
354 hwnd,
355 msg,
356 wParam,
357 lParam);
358 }
359
360
361 INT_PTR CALLBACK CardBackDlgProc(HWND hDlg,
362 UINT uMsg,
363 WPARAM wParam,
364 LPARAM lParam)
365 {
366 static PCARDBACK pCardBacks = NULL;
367
368 switch (uMsg)
369 {
370 case WM_INITDIALOG:
371 {
372 INT i, c;
373 SIZE_T size = sizeof(CARDBACK) * NUM_CARDBACKS;
374
375 pCardBacks = (PCARDBACK)HeapAlloc(GetProcessHeap(),
376 0,
377 size);
378
379 for (i = 0, c = CARDBACK_START; c <= CARDBACK_END; i++, c++)
380 {
381 pCardBacks[i].hSelf = GetDlgItem(hDlg, c);
382 pCardBacks[i].bSelected = FALSE;
383 pCardBacks[i].hdcNum = CARDBACK_RES_START + i;
384 pCardBacks[i].imgNum = i + 1;
385 pCardBacks[i].hOldProc = (WNDPROC)SetWindowLongPtr(pCardBacks[i].hSelf,
386 GWLP_WNDPROC,
387 (LONG_PTR)CardImageWndProc);
388
389 SetWindowLongPtr(pCardBacks[i].hSelf,
390 GWL_USERDATA,
391 (LONG_PTR)&pCardBacks[i]);
392 }
393
394 return TRUE;
395 }
396
397 case WM_COMMAND:
398 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
399 {
400 INT i, num = 0;
401 for (i = 0; i < NUM_CARDBACKS; i++)
402 {
403 if (pCardBacks[i].bSelected)
404 {
405 num = pCardBacks[i].imgNum;
406 }
407 }
408
409 EndDialog(hDlg, LOWORD(wParam) == IDOK ? num : FALSE);
410 HeapFree(GetProcessHeap(), 0, pCardBacks);
411 return TRUE;
412 }
413
414 if (HIWORD(wParam) == STN_CLICKED)
415 {
416 INT i;
417 RECT rc;
418 for (i = 0; i < NUM_CARDBACKS; i++)
419 {
420 if (pCardBacks[i].hSelf == (HWND)lParam)
421 {
422 pCardBacks[i].bSelected = TRUE;
423 }
424 else
425 pCardBacks[i].bSelected = FALSE;
426
427 GetClientRect(pCardBacks[i].hSelf, &rc);
428 InvalidateRect(pCardBacks[i].hSelf, &rc, TRUE);
429 }
430
431 break;
432 }
433 }
434
435 return FALSE;
436 }
437
438
439 VOID ShowDeckOptionsDlg(HWND hwnd)
440 {
441 INT cardBack;
442
443 if ((cardBack = DialogBox(hInstance,
444 MAKEINTRESOURCE(IDD_CARDBACK),
445 hwnd,
446 CardBackDlgProc)))
447 {
448 SolWnd.SetBackCardIdx(CARDBACK_RES_START + (cardBack - 1));
449 SolWnd.Redraw();
450 }
451 }
452
453 //-----------------------------------------------------------------------------
454 LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
455 {
456 static int nWidth, nHeight, nStatusHeight;
457
458 switch(iMsg)
459 {
460 case WM_CREATE:
461 {
462 int parts[] = { 100, -1 };
463 RECT rcStatus;
464
465 hwndStatus = CreateStatusWindow(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, _T("Ready"), hwnd, 0);
466
467 //SendMessage(hwndStatus, SB_SIMPLE, (WPARAM)TRUE, 0);
468
469 SendMessage(hwndStatus, SB_SETPARTS, 2, (LPARAM)parts);
470 SendMessage(hwndStatus, SB_SETTEXT, 0 | SBT_NOBORDERS, (LPARAM)"");
471
472 SolWnd.Create(hwnd, WS_EX_CLIENTEDGE, WS_CHILD|WS_VISIBLE, 0, 0, 0, 0);
473
474 CreateSol();
475
476 // The status bar height is fixed and needed later in WM_SIZE and WM_GETMINMAXINFO
477 // Force the window to process WM_GETMINMAXINFO again
478 GetWindowRect(hwndStatus, &rcStatus);
479 nStatusHeight = rcStatus.bottom - rcStatus.top;
480
481 // Hide status bar if options say so
482 if (!(dwOptions & OPTION_SHOW_STATUS))
483 {
484 ShowWindow(hwndStatus, SW_HIDE);
485 }
486
487 SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOZORDER);
488
489 NewGame();
490
491 dwAppStartTime = GetTickCount();
492
493 return 0;
494 }
495
496 case WM_DESTROY:
497 PostQuitMessage(0);
498 return 0;
499
500 case WM_SIZE:
501 nWidth = LOWORD(lParam);
502 nHeight = HIWORD(lParam);
503
504 if (dwOptions & OPTION_SHOW_STATUS)
505 {
506 MoveWindow(SolWnd, 0, 0, nWidth, nHeight - nStatusHeight, TRUE);
507 MoveWindow(hwndStatus, 0, nHeight - nStatusHeight, nWidth, nStatusHeight, TRUE);
508 }
509 else
510 {
511 MoveWindow(SolWnd, 0, 0, nWidth, nHeight, TRUE);
512 }
513 //parts[0] = nWidth - 256;
514 //SendMessage(hwndStatus, SB_SETPARTS, 2, (LPARAM)parts);
515 return 0;
516
517 case WM_GETMINMAXINFO:
518 {
519 MINMAXINFO *mmi;
520
521 mmi = (MINMAXINFO *)lParam;
522 mmi->ptMinTrackSize.x = X_BORDER + NUM_ROW_STACKS * (__cardwidth + X_ROWSTACK_BORDER) + X_BORDER;
523 mmi->ptMinTrackSize.y = GetSystemMetrics(SM_CYCAPTION) +
524 GetSystemMetrics(SM_CYMENU) +
525 Y_BORDER +
526 __cardheight +
527 Y_ROWSTACK_BORDER +
528 6 * yRowStackCardOffset +
529 __cardheight +
530 Y_BORDER +
531 (dwOptions & OPTION_SHOW_STATUS ? nStatusHeight : 0);
532 return 0;
533 }
534
535 case WM_COMMAND:
536 switch(LOWORD(wParam))
537 {
538 case IDM_GAME_NEW:
539 //simulate a button click on the new button..
540 NewGame();
541 return 0;
542
543 case IDM_GAME_DECK:
544 ShowDeckOptionsDlg(hwnd);
545 return 0;
546
547 case IDM_GAME_OPTIONS:
548 ShowGameOptionsDlg(hwnd);
549 return 0;
550
551 case IDM_HELP_CONTENTS:
552 WinHelp(hwnd, szHelpPath, HELP_CONTENTS, 0);//HELP_KEY, (DWORD)"How to play");
553 return 0;
554
555 case IDM_HELP_ABOUT:
556 MessageBox(hwnd, MsgAbout, szAppName, MB_OK|MB_ICONINFORMATION);
557 return 0;
558
559 case IDM_GAME_EXIT:
560 PostMessage(hwnd, WM_CLOSE, 0, 0);
561 return 0;
562 }
563
564 return 0;
565
566 case WM_CLOSE:
567 if (fGameStarted == false)
568 {
569 DestroyWindow(hwnd);
570 return 0;
571 }
572 else
573 {
574 int ret;
575
576 ret = MessageBox(hwnd, MsgQuit, szAppName, MB_OKCANCEL|MB_ICONQUESTION);
577 if (ret == IDOK)
578 {
579 WinHelp(hwnd, szHelpPath, HELP_QUIT, 0);
580 DestroyWindow(hwnd);
581 }
582 }
583 return 0;
584 }
585
586 return DefWindowProc (hwnd, iMsg, wParam, lParam);
587 }
588
589
590