Subversion Repositories Games.Chess Giants

Rev

Rev 19 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // dialog_resign.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // dialog template
  7. #define THIS_DIALOG DIALOG_RESIGN
  8.  
  9.  
  10. // prototypes of local functions
  11. static void StartThread_ThisDialog (void *thread_parms);
  12. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
  13.  
  14.  
  15. void DialogBox_Resign (void)
  16. {
  17.    // helper function to fire up the modeless dialog box
  18.  
  19.    _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
  20.  
  21.    return; // return as soon as the thread is fired up
  22. }
  23.  
  24.  
  25. void DialogBox_Resign_Validated (void)
  26. {
  27.    // callback function called by the main game thread when the dialog box is validated
  28.  
  29.    player_t *network_player;
  30.  
  31.    // remember this callback is no longer to be called
  32.    is_dialogbox_resign_validated = false;
  33.  
  34.    // see if we're playing against a remote opponent
  35.    network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
  36.  
  37.    // is it an online game ?
  38.    if (network_player != NULL)
  39.       Player_SendBuffer_Add (network_player, 1000, L"resign\n"); // if so, send a resign command
  40.  
  41.    // else we're playing locally (either vs. another human or vs. computer)
  42.    else
  43.    {
  44.       Scene_Shutdown (&the_scene); // release scene
  45.       Board_Shutdown (&the_board); // release chess game
  46.       Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, FENSTARTUP_STANDARDCHESS); // prepare a new human vs human game (by default)
  47.       Scene_Init (&the_scene, &the_board); // initialize scene
  48.       SetWindowText (hMainWnd, PROGRAM_NAME); // update window title
  49.  
  50.       DialogBox_NewGame (); // show the new game dialog box
  51.    }
  52.  
  53.    the_board.reevaluate = true; // evaluate the new board
  54.    the_scene.update = true; // update scene
  55.  
  56.    return; // finished, we have resigned from game
  57. }
  58.  
  59.  
  60. static void StartThread_ThisDialog (void *thread_parms)
  61. {
  62.    // this function runs in a separate thread, for that's the only way (seemingly)
  63.    // to implement a non-modal message box using the Common Controls library.
  64.  
  65.    // display the dialog box
  66.    if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
  67.       is_dialogbox_resign_validated = true;
  68.  
  69.    return; // _endthread() implied
  70. }
  71.  
  72.  
  73. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
  74. {
  75.    // message handler for the dialog box
  76.  
  77.    unsigned short wParam_hiword;
  78.    unsigned short wParam_loword;
  79.  
  80.    // filter out the commonly used message values
  81.    wParam_hiword = HIWORD (wParam);
  82.    wParam_loword = LOWORD (wParam);
  83.  
  84.    // have we just fired up this window ?
  85.    if (message == WM_INITDIALOG)
  86.    {
  87.       // center the window
  88.       CenterWindow (hWnd, hMainWnd);
  89.  
  90.       // set dialog icons (small one for title bar & big one for task manager)
  91.       SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  92.       SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  93.  
  94.       // set window title and control texts
  95.       SetWindowText (hWnd, LOCALIZE (L"Quit_Title"));
  96.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_RESIGN_QUESTION), LOCALIZE (L"Resign_Question"));
  97.       SetWindowText (GetDlgItem (hWnd, BUTTON_GIVEUP), LOCALIZE (L"Resign_GiveUp"));
  98.       SetWindowText (GetDlgItem (hWnd, BUTTON_BACKTOGAME), LOCALIZE (L"Resign_BackToGame"));
  99.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_RESIGN_STATUSBAR), LOCALIZE (L"Resign_StatusBar"));
  100.  
  101.       // convert the status bar message to a hyperlink
  102.       ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_RESIGN_STATUSBAR));
  103.    }
  104.  
  105.    // else did we click the close button on the title bar ?
  106.    else if (message == WM_CLOSE)
  107.       EndDialog (hWnd, 0); // close the dialog box
  108.  
  109.    // else did we take action on one of the controls ?
  110.    else if (message == WM_COMMAND)
  111.    {
  112.       // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
  113.       if (wParam_loword == IDCANCEL)
  114.          EndDialog (hWnd, 0); // close the dialog box
  115.  
  116.       // else was it the quit button ?
  117.       else if (wParam_loword == BUTTON_GIVEUP)
  118.          EndDialog (hWnd, 1); // close the dialog box and return a success code
  119.  
  120.       // else was it the back to game button ?
  121.       else if (wParam_loword == BUTTON_BACKTOGAME)
  122.          EndDialog (hWnd, 0); // close the dialog box
  123.  
  124.       // else was it the status bar hyperlink ?
  125.       else if (wParam_loword == STATICTEXT_RESIGN_STATUSBAR)
  126.          ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
  127.    }
  128.  
  129.    // call the default dialog message processing function to keep things going
  130.    return (false);
  131. }
  132.