Subversion Repositories Games.Chess Giants

Rev

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

  1. // dialog_takeback.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // dialog template
  7. #define THIS_DIALOG DIALOG_TAKEBACK
  8.  
  9.  
  10. // global variables used in this module only
  11. static bool is_takebackaccepted = false;
  12.  
  13.  
  14. // prototypes of local functions
  15. static void StartThread_ThisDialog (void *thread_parms);
  16. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
  17.  
  18.  
  19. void DialogBox_Takeback (int howmany_halfmoves)
  20. {
  21.    // helper function to fire up the modeless dialog box
  22.  
  23.    _beginthread (StartThread_ThisDialog, 0, (void *) howmany_halfmoves); // fire up the thread
  24.    return; // return as soon as the thread is fired up
  25. }
  26.  
  27.  
  28. void DialogBox_Takeback_Validated (void)
  29. {
  30.    // callback function called by the main game thread when the dialog box is validated
  31.  
  32.    player_t *network_player;
  33.  
  34.    // remember this callback is no longer to be called
  35.    is_dialogbox_takeback_validated = false;
  36.  
  37.    network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
  38.    if (network_player == NULL)
  39.       return; // consistency check
  40.  
  41.    // did we accept the challenge ? if so, send an accept reply, else decline it
  42.    if (is_takebackaccepted)
  43.       Player_SendBuffer_Add (network_player, 1000, L"accept %s\n", network_player->name); // send the accept notification
  44.    else
  45.       Player_SendBuffer_Add (network_player, 1000, L"decline %s\n", network_player->name); // send the decline notification
  46.  
  47.    return; // finished, challenge has been either accepted or declined
  48. }
  49.  
  50.  
  51. static void StartThread_ThisDialog (void *thread_parms)
  52. {
  53.    // this function runs in a separate thread, for that's the only way (seemingly)
  54.    // to implement a non-modal message box using the Common Controls library.
  55.  
  56.    player_t *network_player;
  57.    int interlocutor_index;
  58.    int howmany_halfmoves;
  59.    HWND hParentWnd;
  60.    int retval;
  61.    int error;
  62.  
  63.    howmany_halfmoves = (int) thread_parms;
  64.    network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
  65.    if (network_player == NULL)
  66.       return; // consistency check
  67.  
  68.    // see if we are already talking to this interlocutor, loop through all of them...
  69.    hParentWnd = hMainWnd; // assume parent window will be the main window, until told otherwise
  70.    for (interlocutor_index = 0; interlocutor_index < interlocutor_count; interlocutor_index++)
  71.       if (interlocutors[interlocutor_index].is_active && (_wcsicmp (network_player->name, interlocutors[interlocutor_index].nickname) == 0))
  72.       {
  73.          hParentWnd = interlocutors[interlocutor_index].hWnd; // if found, parent window will be this interlocutor's chat window
  74.          break; // break as soon as we find it
  75.       }
  76.  
  77.    // display the dialog box
  78.    retval = DialogBoxParam (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hParentWnd, DialogProc_ThisDialog, (LPARAM) howmany_halfmoves);
  79.    error = GetLastError ();
  80.    is_takebackaccepted = (retval != 0);
  81.    is_dialogbox_takeback_validated = true;
  82.  
  83.    the_board.reevaluate = true; // refresh the GUI buttons if needed
  84.    return; // _endthread() implied
  85. }
  86.  
  87.  
  88. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
  89. {
  90.    // message handler for the dialog box
  91.  
  92.    unsigned short wParam_hiword;
  93.    unsigned short wParam_loword;
  94.    int howmany_halfmoves;
  95.    player_t *network_player;
  96.    wchar_t temp_string[256];
  97.  
  98.    // filter out the commonly used message values
  99.    wParam_hiword = HIWORD (wParam);
  100.    wParam_loword = LOWORD (wParam);
  101.  
  102.    // have we just fired up this window ?
  103.    if (message == WM_INITDIALOG)
  104.    {
  105.       howmany_halfmoves = (int) lParam;
  106.       network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
  107.       if (network_player == NULL)
  108.          return (false); // consistency check
  109.  
  110.       // center the window
  111.       CenterWindow (hWnd, hMainWnd);
  112.  
  113.       // set window title and control texts
  114.       SetWindowText (hWnd, LOCALIZE (L"ImportantMessage"));
  115.       swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Chat_TakebackRequestReceived"), network_player->name, howmany_halfmoves);
  116.       SetDlgItemText (hWnd, STATICTEXT_TAKEBACK_QUESTION, temp_string);
  117.       SetDlgItemText (hWnd, BUTTON_ACCEPT, LOCALIZE (L"Button_Accept"));
  118.       SetDlgItemText (hWnd, BUTTON_DECLINE, LOCALIZE (L"Button_Decline"));
  119.       SetDlgItemText (hWnd, STATICTEXT_TAKEBACK_STATUSBAR, LOCALIZE (L"Chat_StatusBar"));
  120.  
  121.       // convert the status bar message to a hyperlink
  122.       ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_TAKEBACK_STATUSBAR));
  123.    }
  124.  
  125.    // else did we click the close button on the title bar ?
  126.    else if (message == WM_CLOSE)
  127.       EndDialog (hWnd, 0); // close the dialog box
  128.  
  129.    // else did we take action on one of the controls ?
  130.    else if (message == WM_COMMAND)
  131.    {
  132.       // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
  133.       // OR was it the "decline" button ?
  134.       if ((wParam_loword == IDCANCEL) || (wParam_loword == BUTTON_DECLINE))
  135.          EndDialog (hWnd, 0); // close the dialog box
  136.  
  137.       // else was it the "accept" button ?
  138.       else if (wParam_loword == BUTTON_ACCEPT)
  139.          EndDialog (hWnd, 1); // close the dialog box and return OK
  140.  
  141.       // else was it the status bar hyperlink ?
  142.       else if (wParam_loword == STATICTEXT_CHALLENGE_STATUSBAR)
  143.          ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
  144.    }
  145.  
  146.    // call the default dialog message processing function to keep things going
  147.    return (false);
  148. }
  149.