Subversion Repositories Games.Chess Giants

Rev

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