ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

solitaire.cpp
Go to the documentation of this file.
00001 
00002 #include "solitaire.h"
00003 
00004 TCHAR szHelpPath[MAX_PATH];
00005 
00006 DWORD        dwAppStartTime;
00007 HWND        hwndMain;
00008 HWND        hwndStatus;
00009 HINSTANCE    hInstance;
00010 
00011 TCHAR szAppName[128];
00012 TCHAR MsgQuit[128];
00013 TCHAR MsgAbout[128];
00014 TCHAR MsgWin[128];
00015 TCHAR MsgDeal[128];
00016 DWORD dwOptions = OPTION_THREE_CARDS;
00017 
00018 CardWindow SolWnd;
00019 
00020 typedef struct _CardBack
00021 {
00022     HWND hSelf;
00023     WNDPROC hOldProc;
00024     INT hdcNum;
00025     INT imgNum;
00026     BOOL bSelected;
00027 } CARDBACK, *PCARDBACK;
00028 
00029 LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
00030 
00031 void MakePath(TCHAR *szDest, UINT nDestLen, const TCHAR *szExt)
00032 {
00033     TCHAR *ptr;
00034 
00035     ptr = szDest + GetModuleFileName(GetModuleHandle(0), szDest, nDestLen) - 1;
00036     while(*ptr-- != '.');
00037     lstrcpy(ptr + 1, szExt);
00038 }
00039 
00040 VOID LoadSettings(VOID)
00041 {
00042     DWORD dwDisposition;
00043     DWORD dwSize;
00044     DWORD dwBack;
00045     HKEY hKey;
00046 
00047     if (RegCreateKeyEx(HKEY_CURRENT_USER,
00048                        _T("Software\\ReactOS\\Solitaire"),
00049                        0,
00050                        NULL,
00051                        REG_OPTION_NON_VOLATILE,
00052                        KEY_READ,
00053                        NULL,
00054                        &hKey,
00055                        &dwDisposition))
00056         return;
00057 
00058     dwSize = sizeof(DWORD);
00059     RegQueryValueEx(hKey,
00060                     _T("Options"),
00061                     NULL,
00062                     NULL,
00063                     (LPBYTE)&dwOptions,
00064                     &dwSize);
00065 
00066     dwSize = sizeof(DWORD);
00067     RegQueryValueEx(hKey,
00068                     _T("Back"),
00069                     NULL,
00070                     NULL,
00071                     (LPBYTE)&dwBack,
00072                     &dwSize);
00073     SolWnd.SetBackCardIdx(dwBack);
00074 
00075     RegCloseKey(hKey);
00076 }
00077 
00078 VOID SaveSettings(VOID)
00079 {
00080     DWORD dwDisposition;
00081     DWORD dwBack;
00082     HKEY hKey;
00083 
00084     if (RegCreateKeyEx(HKEY_CURRENT_USER,
00085                        _T("Software\\ReactOS\\Solitaire"),
00086                        0,
00087                        NULL,
00088                        REG_OPTION_NON_VOLATILE,
00089                        KEY_WRITE,
00090                        NULL,
00091                        &hKey,
00092                        &dwDisposition))
00093         return;
00094 
00095     RegSetValueEx(hKey,
00096                   _T("Options"),
00097                   0,
00098                   REG_DWORD,
00099                   (CONST BYTE *)&dwOptions,
00100                   sizeof(DWORD));
00101 
00102     dwBack = SolWnd.GetBackCardIdx();
00103     RegSetValueEx(hKey,
00104                   _T("Back"),
00105                   0,
00106                   REG_DWORD,
00107                   (CONST BYTE *)&dwBack,
00108                   sizeof(DWORD));
00109 
00110     RegCloseKey(hKey);
00111 }
00112 
00113 //
00114 //    Main entry point
00115 //
00116 int WINAPI _tWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int iCmdShow)
00117 {
00118     HWND        hwnd;
00119     MSG            msg;
00120     WNDCLASS    wndclass;
00121     INITCOMMONCONTROLSEX ice;
00122     HACCEL        hAccelTable;
00123 
00124     hInstance = hInst;
00125 
00126     // Load application title
00127     LoadString(hInst, IDS_SOL_NAME, szAppName, sizeof(szAppName) / sizeof(szAppName[0]));
00128     // Load MsgBox() texts here to avoid loading them many times later
00129     LoadString(hInst, IDS_SOL_ABOUT, MsgAbout, sizeof(MsgAbout) / sizeof(MsgAbout[0]));
00130     LoadString(hInst, IDS_SOL_QUIT, MsgQuit, sizeof(MsgQuit) / sizeof(MsgQuit[0]));
00131     LoadString(hInst, IDS_SOL_WIN, MsgWin, sizeof(MsgWin) / sizeof(MsgWin[0]));
00132     LoadString(hInst, IDS_SOL_DEAL, MsgDeal, sizeof(MsgDeal) / sizeof(MsgDeal[0]));
00133 
00134     //Window class for the main application parent window
00135     wndclass.style            = 0;//CS_HREDRAW | CS_VREDRAW;
00136     wndclass.lpfnWndProc    = WndProc;
00137     wndclass.cbClsExtra        = 0;
00138     wndclass.cbWndExtra        = 0;
00139     wndclass.hInstance        = hInst;
00140     wndclass.hIcon            = LoadIcon (hInst, MAKEINTRESOURCE(IDI_SOLITAIRE));
00141     wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
00142     wndclass.hbrBackground    = (HBRUSH)NULL;
00143     wndclass.lpszMenuName    = MAKEINTRESOURCE(IDR_MENU1);
00144     wndclass.lpszClassName    = szAppName;
00145 
00146     RegisterClass(&wndclass);
00147 
00148     ice.dwSize = sizeof(ice);
00149     ice.dwICC = ICC_BAR_CLASSES;
00150     InitCommonControlsEx(&ice);
00151 
00152     srand((unsigned)GetTickCount());//timeGetTime());
00153 
00154 //    InitCardLib();
00155 
00156     LoadSettings();
00157 
00158     //Construct the path to our help file
00159     MakePath(szHelpPath, MAX_PATH, _T(".hlp"));
00160 
00161     hwnd = CreateWindow(szAppName,        // window class name
00162                 szAppName,                // window caption
00163                 WS_OVERLAPPEDWINDOW
00164                 ,//|WS_CLIPCHILDREN,      // window style
00165                 CW_USEDEFAULT,            // initial x position
00166                 CW_USEDEFAULT,            // initial y position
00167                 0,                        // The real size will be computed in WndProc through WM_GETMINMAXINFO
00168                 0,                        // The real size will be computed in WndProc through WM_GETMINMAXINFO
00169                 NULL,                     // parent window handle
00170                 NULL,                     // use window class menu
00171                 hInst,                    // program instance handle
00172                 NULL);                    // creation parameters
00173 
00174     hwndMain = hwnd;
00175 
00176     ShowWindow(hwnd, iCmdShow);
00177     UpdateWindow(hwnd);
00178 
00179     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1));
00180 
00181     while(GetMessage(&msg, NULL,0,0))
00182     {
00183         if(!TranslateAccelerator(hwnd, hAccelTable, &msg))
00184         {
00185             TranslateMessage(&msg);
00186             DispatchMessage(&msg);
00187         }
00188     }
00189 
00190     SaveSettings();
00191 
00192     return msg.wParam;
00193 }
00194 
00195 
00196 INT_PTR CALLBACK OptionsDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
00197 {
00198     switch (uMsg)
00199     {
00200     case WM_INITDIALOG:
00201         CheckRadioButton(hDlg, IDC_OPT_DRAWONE, IDC_OPT_DRAWTHREE,
00202                          (dwOptions & OPTION_THREE_CARDS) ? IDC_OPT_DRAWTHREE : IDC_OPT_DRAWONE);
00203 
00204         CheckDlgButton(hDlg,
00205                        IDC_OPT_STATUSBAR,
00206                        (dwOptions & OPTION_SHOW_STATUS) ? BST_CHECKED : BST_UNCHECKED);
00207         return TRUE;
00208 
00209     case WM_COMMAND:
00210         switch(LOWORD(wParam))
00211         {
00212         case IDOK:
00213             dwOptions &= ~OPTION_THREE_CARDS;
00214             if (IsDlgButtonChecked(hDlg, IDC_OPT_DRAWTHREE) == BST_CHECKED)
00215                 dwOptions |= OPTION_THREE_CARDS;
00216 
00217             if (IsDlgButtonChecked(hDlg, IDC_OPT_STATUSBAR) == BST_CHECKED)
00218                 dwOptions |= OPTION_SHOW_STATUS;
00219             else
00220                 dwOptions &= ~OPTION_SHOW_STATUS;
00221 
00222             EndDialog(hDlg, TRUE);
00223             return TRUE;
00224 
00225         case IDCANCEL:
00226             EndDialog(hDlg, FALSE);
00227             return TRUE;
00228         }
00229         break;
00230     }
00231     return FALSE;
00232 }
00233 
00234 VOID ShowGameOptionsDlg(HWND hwnd)
00235 {
00236     DWORD dwOldOptions = dwOptions;
00237     RECT rcMain, rcStatus;
00238 
00239     if (DialogBox(hInstance, MAKEINTRESOURCE(IDD_OPTIONS), hwnd, OptionsDlgProc))
00240     {
00241         if ((dwOldOptions & OPTION_THREE_CARDS) != (dwOptions & OPTION_THREE_CARDS))
00242             NewGame();
00243 
00244         if ((dwOldOptions & OPTION_SHOW_STATUS) != (dwOptions & OPTION_SHOW_STATUS))
00245         {
00246             int nWidth, nHeight, nStatusHeight;
00247 
00248             GetClientRect(hwndMain, &rcMain);
00249             nHeight = rcMain.bottom - rcMain.top;
00250             nWidth = rcMain.right - rcMain.left;
00251 
00252             if (dwOptions & OPTION_SHOW_STATUS)
00253             {
00254                 RECT rc;
00255 
00256                 ShowWindow(hwndStatus, SW_SHOW);
00257                 GetWindowRect(hwndStatus, &rcStatus);
00258                 nStatusHeight = rcStatus.bottom - rcStatus.top;
00259                 MoveWindow(SolWnd, 0, 0, nWidth, nHeight-nStatusHeight, TRUE);
00260                 MoveWindow(hwndStatus, 0, nHeight-nStatusHeight, nWidth, nHeight, TRUE);
00261 
00262                 // Force the window to process WM_GETMINMAXINFO again
00263                 GetWindowRect(hwndMain, &rc);
00264                 SetWindowPos(hwndMain, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER);
00265             }
00266             else
00267             {
00268                 ShowWindow(hwndStatus, SW_HIDE);
00269                 MoveWindow(SolWnd, 0, 0, nWidth, nHeight, TRUE);
00270             }
00271         }
00272     }
00273 }
00274 
00275 
00276 LRESULT CALLBACK
00277 CardImageWndProc(HWND hwnd,
00278                  UINT msg,
00279                  WPARAM wParam,
00280                  LPARAM lParam)
00281 {
00282     PCARDBACK pCardBack = (PCARDBACK)GetWindowLongPtr(hwnd,
00283                                                       GWL_USERDATA);
00284     static WNDPROC hOldProc = NULL;
00285 
00286     if (!hOldProc && pCardBack)
00287         hOldProc = pCardBack->hOldProc;
00288 
00289     switch (msg)
00290     {
00291     case WM_PAINT:
00292     {
00293         HDC hdc;
00294         PAINTSTRUCT ps;
00295         HPEN hPen, hOldPen;
00296         HBRUSH hBrush, hOldBrush;
00297         RECT rc;
00298 
00299         hdc = BeginPaint(hwnd, &ps);
00300 
00301         if (pCardBack->bSelected)
00302         {
00303             hPen = CreatePen(PS_SOLID, 2, RGB(0,0,0));
00304         }
00305         else
00306         {
00307             DWORD Face = GetSysColor(COLOR_3DFACE);
00308             hPen = CreatePen(PS_SOLID, 2, Face);
00309         }
00310 
00311         GetClientRect(hwnd, &rc);
00312         hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
00313         hOldPen = (HPEN)SelectObject(hdc, hPen);
00314         hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
00315 
00316         Rectangle(hdc,
00317                   rc.left+1,
00318                   rc.top+1,
00319                   rc.right,
00320                   rc.bottom);
00321 
00322         StretchBlt(hdc,
00323                    2,
00324                    2,
00325                    CARDBACK_OPTIONS_WIDTH,
00326                    CARDBACK_OPTIONS_HEIGHT,
00327                    __hdcCardBitmaps,
00328                    pCardBack->hdcNum * __cardwidth,
00329                    0,
00330                    __cardwidth,
00331                    __cardheight,
00332                    SRCCOPY);
00333 
00334         SelectObject(hdc, hOldPen);
00335         SelectObject(hdc, hOldBrush);
00336 
00337         EndPaint(hwnd, &ps);
00338 
00339         break;
00340     }
00341 
00342     case WM_LBUTTONDOWN:
00343         pCardBack->bSelected = pCardBack->bSelected ? FALSE : TRUE;
00344         break;
00345     }
00346 
00347     return CallWindowProc(hOldProc,
00348                           hwnd,
00349                           msg,
00350                           wParam,
00351                           lParam);
00352 }
00353 
00354 
00355 INT_PTR CALLBACK CardBackDlgProc(HWND hDlg,
00356                               UINT uMsg,
00357                               WPARAM wParam,
00358                               LPARAM lParam)
00359 {
00360     static PCARDBACK pCardBacks = NULL;
00361 
00362     switch (uMsg)
00363     {
00364     case WM_INITDIALOG:
00365     {
00366         INT i, c;
00367         SIZE_T size = sizeof(CARDBACK) * NUM_CARDBACKS;
00368 
00369         pCardBacks = (PCARDBACK)HeapAlloc(GetProcessHeap(),
00370                                           0,
00371                                           size);
00372 
00373         for (i = 0, c = CARDBACK_START; c <= CARDBACK_END; i++, c++)
00374         {
00375             pCardBacks[i].hSelf = GetDlgItem(hDlg, c);
00376             pCardBacks[i].bSelected = FALSE;
00377             pCardBacks[i].hdcNum = CARDBACK_RES_START + i;
00378             pCardBacks[i].imgNum = i + 1;
00379             pCardBacks[i].hOldProc = (WNDPROC)SetWindowLongPtr(pCardBacks[i].hSelf,
00380                                                                GWLP_WNDPROC,
00381                                                                (LONG_PTR)CardImageWndProc);
00382 
00383             SetWindowLongPtr(pCardBacks[i].hSelf,
00384                              GWL_USERDATA,
00385                              (LONG_PTR)&pCardBacks[i]);
00386         }
00387 
00388         return TRUE;
00389     }
00390 
00391     case WM_COMMAND:
00392         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
00393         {
00394             INT i, num = 0;
00395             for (i = 0; i < NUM_CARDBACKS; i++)
00396             {
00397                 if (pCardBacks[i].bSelected)
00398                 {
00399                     num = pCardBacks[i].imgNum;
00400                 }
00401             }
00402 
00403             EndDialog(hDlg, LOWORD(wParam) == IDOK ? num : FALSE);
00404             HeapFree(GetProcessHeap(), 0, pCardBacks);
00405             return TRUE;
00406         }
00407 
00408         if (HIWORD(wParam) == STN_CLICKED)
00409         {
00410             INT i;
00411             RECT rc;
00412             for (i = 0; i < NUM_CARDBACKS; i++)
00413             {
00414                 if (pCardBacks[i].hSelf == (HWND)lParam)
00415                 {
00416                     pCardBacks[i].bSelected = TRUE;
00417                 }
00418                 else
00419                     pCardBacks[i].bSelected = FALSE;
00420 
00421                 GetClientRect(pCardBacks[i].hSelf, &rc);
00422                 InvalidateRect(pCardBacks[i].hSelf, &rc, TRUE);
00423             }
00424 
00425             break;
00426         }
00427     }
00428 
00429     return FALSE;
00430 }
00431 
00432 
00433 VOID ShowDeckOptionsDlg(HWND hwnd)
00434 {
00435     INT cardBack;
00436 
00437     if ((cardBack = DialogBox(hInstance,
00438                               MAKEINTRESOURCE(IDD_CARDBACK),
00439                               hwnd,
00440                               CardBackDlgProc)))
00441     {
00442         SolWnd.SetBackCardIdx(CARDBACK_RES_START + (cardBack - 1));
00443         SolWnd.Redraw();
00444     }
00445 }
00446 
00447 //-----------------------------------------------------------------------------
00448 LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
00449 {
00450     static int nWidth, nHeight, nStatusHeight;
00451 
00452     switch(iMsg)
00453     {
00454         case WM_CREATE:
00455         {
00456             int parts[] = { 100, -1 };
00457             RECT rcStatus;
00458 
00459             hwndStatus = CreateStatusWindow(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, _T("Ready"), hwnd, 0);
00460 
00461             //SendMessage(hwndStatus, SB_SIMPLE, (WPARAM)TRUE, 0);
00462 
00463             SendMessage(hwndStatus, SB_SETPARTS, 2, (LPARAM)parts);
00464             SendMessage(hwndStatus, SB_SETTEXT, 0 | SBT_NOBORDERS, (LPARAM)"");
00465 
00466             SolWnd.Create(hwnd, WS_EX_CLIENTEDGE, WS_CHILD|WS_VISIBLE, 0, 0, 0, 0);
00467 
00468             CreateSol();
00469 
00470             // The status bar height is fixed and needed later in WM_SIZE and WM_GETMINMAXINFO
00471             // Force the window to process WM_GETMINMAXINFO again
00472             GetWindowRect(hwndStatus, &rcStatus);
00473             nStatusHeight = rcStatus.bottom - rcStatus.top;
00474 
00475             // Hide status bar if options say so
00476             if (!(dwOptions & OPTION_SHOW_STATUS))
00477             {
00478                 ShowWindow(hwndStatus, SW_HIDE);
00479             }
00480 
00481             SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOZORDER);
00482 
00483             NewGame();
00484 
00485             dwAppStartTime = GetTickCount();
00486 
00487             return 0;
00488         }
00489 
00490         case WM_DESTROY:
00491             PostQuitMessage(0);
00492             return 0;
00493 
00494         case WM_SIZE:
00495             nWidth  = LOWORD(lParam);
00496             nHeight = HIWORD(lParam);
00497 
00498             if (dwOptions & OPTION_SHOW_STATUS)
00499             {
00500                 MoveWindow(SolWnd, 0, 0, nWidth, nHeight - nStatusHeight, TRUE);
00501                 MoveWindow(hwndStatus, 0, nHeight - nStatusHeight, nWidth, nStatusHeight, TRUE);
00502             }
00503             else
00504             {
00505                 MoveWindow(SolWnd, 0, 0, nWidth, nHeight, TRUE);
00506             }
00507             //parts[0] = nWidth - 256;
00508             //SendMessage(hwndStatus, SB_SETPARTS, 2, (LPARAM)parts);
00509             return 0;
00510 
00511         case WM_GETMINMAXINFO:
00512         {
00513             MINMAXINFO *mmi;
00514 
00515             mmi = (MINMAXINFO *)lParam;
00516             mmi->ptMinTrackSize.x = X_BORDER + NUM_ROW_STACKS * (__cardwidth + X_ROWSTACK_BORDER) + X_BORDER;
00517             mmi->ptMinTrackSize.y = GetSystemMetrics(SM_CYCAPTION) +
00518                                     GetSystemMetrics(SM_CYMENU) +
00519                                     Y_BORDER +
00520                                     __cardheight +
00521                                     Y_ROWSTACK_BORDER +
00522                                     6 * yRowStackCardOffset +
00523                                     __cardheight +
00524                                     Y_BORDER +
00525                                     (dwOptions & OPTION_SHOW_STATUS ? nStatusHeight : 0);
00526             return 0;
00527         }
00528 
00529         case WM_COMMAND:
00530             switch(LOWORD(wParam))
00531             {
00532             case IDM_GAME_NEW:
00533                 //simulate a button click on the new button..
00534                 NewGame();
00535                 return 0;
00536 
00537             case IDM_GAME_DECK:
00538                 ShowDeckOptionsDlg(hwnd);
00539                 return 0;
00540 
00541             case IDM_GAME_OPTIONS:
00542                 ShowGameOptionsDlg(hwnd);
00543                 return 0;
00544 
00545             case IDM_HELP_CONTENTS:
00546                 WinHelp(hwnd, szHelpPath, HELP_CONTENTS, 0);//HELP_KEY, (DWORD)"How to play");
00547                 return 0;
00548 
00549             case IDM_HELP_ABOUT:
00550                 MessageBox(hwnd, MsgAbout, szAppName, MB_OK|MB_ICONINFORMATION);
00551                 return 0;
00552 
00553             case IDM_GAME_EXIT:
00554                 PostMessage(hwnd, WM_CLOSE, 0, 0);
00555                 return 0;
00556             }
00557 
00558             return 0;
00559 
00560         case WM_CLOSE:
00561             if (fGameStarted == false)
00562             {
00563                 DestroyWindow(hwnd);
00564                 return 0;
00565             }
00566             else
00567             {
00568                 int ret;
00569 
00570                 ret = MessageBox(hwnd, MsgQuit, szAppName, MB_YESNO|MB_ICONQUESTION);
00571                 if (ret == IDYES)
00572                 {
00573                     WinHelp(hwnd, szHelpPath, HELP_QUIT, 0);
00574                     DestroyWindow(hwnd);
00575                 }
00576             }
00577             return 0;
00578     }
00579 
00580     return DefWindowProc (hwnd, iMsg, wParam, lParam);
00581 }
00582 
00583 
00584 

Generated on Sun May 27 2012 04:16:36 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.