// dialog_takeback.cpp
 
 
 
#include "../common.h"
 
 
 
 
 
// dialog template
 
#define THIS_DIALOG DIALOG_TAKEBACK
 
 
 
 
 
// global variables used in this module only
 
static bool is_takebackaccepted = false;
 
 
 
 
 
// prototypes of local functions
 
static void StartThread_ThisDialog (void *thread_parms);
 
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
 
 
 
 
 
void DialogBox_Takeback (int howmany_halfmoves)
 
{
 
   // helper function to fire up the modeless dialog box
 
 
 
   _beginthread (StartThread_ThisDialog, 0, (void *) howmany_halfmoves); // fire up the thread
 
   return; // return as soon as the thread is fired up
 
}
 
 
 
 
 
void DialogBox_Takeback_Validated (void)
 
{
 
   // callback function called by the main game thread when the dialog box is validated
 
 
 
   player_t *network_player;
 
 
 
   // remember this callback is no longer to be called
 
   is_dialogbox_takeback_validated = false;
 
 
 
   network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
 
   if (network_player == NULL)
 
      return; // consistency check
 
 
 
   // did we accept the challenge ? if so, send an accept reply, else decline it
 
   if (is_takebackaccepted)
 
      Player_SendBuffer_Add (network_player, 1000, L"accept %s\n", network_player->name); // send the accept notification
 
   else
 
      Player_SendBuffer_Add (network_player, 1000, L"decline %s\n", network_player->name); // send the decline notification
 
 
 
   return; // finished, challenge has been either accepted or declined
 
}
 
 
 
 
 
static void StartThread_ThisDialog (void *thread_parms)
 
{
 
   // this function runs in a separate thread, for that's the only way (seemingly)
 
   // to implement a non-modal message box using the Common Controls library.
 
 
 
   player_t *network_player;
 
   int interlocutor_index;
 
   int howmany_halfmoves;
 
   HWND hParentWnd;
 
   int retval;
 
   int error;
 
 
 
   howmany_halfmoves = (int) thread_parms;
 
   network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
 
   if (network_player == NULL)
 
      return; // consistency check
 
 
 
   // see if we are already talking to this interlocutor, loop through all of them...
 
   hParentWnd = hMainWnd; // assume parent window will be the main window, until told otherwise
 
   for (interlocutor_index = 0; interlocutor_index < interlocutor_count; interlocutor_index++)
 
      if (interlocutors[interlocutor_index].is_active && (_wcsicmp (network_player->name, interlocutors[interlocutor_index].nickname) == 0))
 
      {
 
         hParentWnd = interlocutors[interlocutor_index].hWnd; // if found, parent window will be this interlocutor's chat window
 
         break; // break as soon as we find it
 
      }
 
 
 
   // display the dialog box
 
   retval = DialogBoxParam (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hParentWnd, DialogProc_ThisDialog, (LPARAM) howmany_halfmoves);
 
   error = GetLastError ();
 
   is_takebackaccepted = (retval != 0);
 
   is_dialogbox_takeback_validated = true;
 
 
 
   return; // _endthread() implied
 
}
 
 
 
 
 
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
 
{
 
   // message handler for the dialog box
 
 
 
   unsigned short wParam_hiword;
 
   unsigned short wParam_loword;
 
   int howmany_halfmoves;
 
   player_t *network_player;
 
   wchar_t temp_string[256];
 
 
 
   // filter out the commonly used message values
 
   wParam_hiword = HIWORD (wParam);
 
   wParam_loword = LOWORD (wParam);
 
 
 
   // have we just fired up this window ?
 
   if (message == WM_INITDIALOG)
 
   {
 
      howmany_halfmoves = (int) lParam;
 
      network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
 
      if (network_player == NULL)
 
         return (false); // consistency check
 
 
 
      // center the window
 
      CenterWindow (hWnd, hMainWnd);
 
 
 
      // set window title and control texts
 
      SetWindowText (hWnd, LOCALIZE (L"ImportantMessage"));
 
      swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Chat_TakebackRequestReceived"), network_player->name, howmany_halfmoves);
 
      SetDlgItemText (hWnd, STATICTEXT_TAKEBACK_QUESTION, temp_string);
 
      SetDlgItemText (hWnd, BUTTON_ACCEPT, LOCALIZE (L"Button_Accept"));
 
      SetDlgItemText (hWnd, BUTTON_DECLINE, LOCALIZE (L"Button_Decline"));
 
      SetDlgItemText (hWnd, STATICTEXT_TAKEBACK_STATUSBAR, LOCALIZE (L"Chat_StatusBar"));
 
 
 
      // convert the status bar message to a hyperlink
 
      ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_TAKEBACK_STATUSBAR));
 
   }
 
 
 
   // else did we click the close button on the title bar ?
 
   else if (message == WM_CLOSE)
 
      EndDialog (hWnd, 0); // close the dialog box
 
 
 
   // else did we take action on one of the controls ?
 
   else if (message == WM_COMMAND)
 
   {
 
      // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
 
      // OR was it the "decline" button ?
 
      if ((wParam_loword == IDCANCEL) || (wParam_loword == BUTTON_DECLINE))
 
         EndDialog (hWnd, 0); // close the dialog box
 
 
 
      // else was it the "accept" button ?
 
      else if (wParam_loword == BUTTON_ACCEPT)
 
         EndDialog (hWnd, 1); // close the dialog box and return OK
 
 
 
      // else was it the status bar hyperlink ?
 
      else if (wParam_loword == STATICTEXT_CHALLENGE_STATUSBAR)
 
         ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
 
   }
 
 
 
   // call the default dialog message processing function to keep things going
 
   return (false);
 
}