// dialog_challenge.cpp
#include "../common.h"
// dialog template
#define THIS_DIALOG DIALOG_CHALLENGE
// local definitions
#define TIMER_REFRESHDIALOG 1
// global variables used in this module only
static bool is_challengeaccepted = false;
static wchar_t challenger_nickname[32];
// 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_Challenge (int challenge_index)
{
// helper function to fire up the modeless dialog box
_beginthread (StartThread_ThisDialog, 0, (void *) challenge_index); // fire up the thread
return; // return as soon as the thread is fired up
}
void DialogBox_Challenge_Validated (void)
{
// callback function called by the main game thread when the dialog box is validated
player_t *network_player;
interlocutor_t *interlocutor;
// remember this callback is no longer to be called
is_dialogbox_challenge_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_challengeaccepted)
{
// send the accept notification and print a notification in this player's chat window
Player_SendBuffer_Add (network_player, 1000, L"accept %s\n", challenger_nickname);
interlocutor = Interlocutor_FindOrCreate (challenger_nickname);
Interlocutor_Notify (interlocutor, LOCALIZE (L"Chat_InvitationAcceptedByYou"), challenger_nickname);
if (IsWindow (interlocutor->hWnd))
ShowWindow (interlocutor->hWnd, SW_MINIMIZE); // minimize chat window immediately
}
else
{
// send the decline notification and print a notification in this player's chat window
Player_SendBuffer_Add (network_player, 1000, L"decline %s\n", challenger_nickname);
Interlocutor_Notify (Interlocutor_FindOrCreate (challenger_nickname), LOCALIZE (L"Chat_InvitationDeclinedByYou"), challenger_nickname);
}
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.
int challenge_index;
int interlocutor_index;
challenge_t *challenge;
HWND hParentWnd;
int retval;
int error;
challenge_index = (int) thread_parms;
challenge = &challenges[challenge_index]; // quick access to challenge
// 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 (challenge->challenger, 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) challenge_index);
error = GetLastError ();
is_challengeaccepted = (retval != 0);
is_dialogbox_challenge_validated = true;
challenge = &challenges[challenge_index]; // restore access to challenge (it may have relocated !)
challenge->is_active = false; // remember challenge has gone away
the_board.reevaluate = true; // refresh the GUI buttons if needed
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;
challenge_t *challenge;
wchar_t game_name[64];
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)
{
challenge = &challenges[lParam]; // quick access to challenge
challenge->hWnd = hWnd; // save challenge window handle
SetWindowLongPtr (hWnd, DWLP_USER, lParam); // and attach challenge number to window
// center the window
CenterWindow (hWnd, hMainWnd);
// set window title and control texts
SetWindowText (hWnd, LOCALIZE (L"Challenge_Title"));
SetDlgItemText (hWnd, BUTTON_ACCEPT, LOCALIZE (L"Button_Accept"));
SetDlgItemText (hWnd, BUTTON_DECLINE, LOCALIZE (L"Button_Decline"));
SetDlgItemText (hWnd, STATICTEXT_CHALLENGE_STATUSBAR, LOCALIZE (L"Challenge_StatusBar"));
// start refreshing the window every second
SetTimer (hWnd, TIMER_REFRESHDIALOG, 1000, NULL);
SendMessage (hWnd, WM_TIMER, TIMER_REFRESHDIALOG, 0);
// convert the challenger name and the status bar message to hyperlinks
ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_CHALLENGE_CHALLENGER));
ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_CHALLENGE_STATUSBAR));
}
// else did we click the close button on the title bar ?
else if (message == WM_CLOSE)
{
KillTimer (hWnd, TIMER_REFRESHDIALOG); // destroy the timer we used to refresh the dialog text
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))
{
challenge = &challenges[GetWindowLongPtr (hWnd, DWLP_USER)]; // quick access to challenge
wcscpy_s (challenger_nickname, WCHAR_SIZEOF (challenger_nickname), challenge->challenger); // have a copy of challenger's name
KillTimer (hWnd, TIMER_REFRESHDIALOG); // destroy the timer we used to refresh the dialog text
EndDialog (hWnd, 0); // close the dialog box
}
// else was it the "accept" button ?
else if (wParam_loword == BUTTON_ACCEPT)
{
challenge = &challenges[GetWindowLongPtr (hWnd, DWLP_USER)]; // quick access to challenge
wcscpy_s (challenger_nickname, WCHAR_SIZEOF (challenger_nickname), challenge->challenger); // have a copy of challenger's name
KillTimer (hWnd, TIMER_REFRESHDIALOG); // destroy the timer we used to refresh the dialog text
EndDialog (hWnd, 1); // close the dialog box and return OK
}
// else did we click the challenger name ?
else if (wParam_loword == STATICTEXT_CHALLENGE_CHALLENGER)
{
challenge = &challenges[GetWindowLongPtr (hWnd, DWLP_USER)]; // quick access to challenge
PlayerCard_FindOrCreate (challenge->challenger); // fire up the dialog box (TODO: move to ._Validated())
}
// 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
}
// else is it a timer event AND is it our refresh timer ?
else if ((message == WM_TIMER) && (wParam == TIMER_REFRESHDIALOG))
{
challenge = &challenges[GetWindowLongPtr (hWnd, DWLP_USER)]; // quick access to challenge
// do we need to update dialog ?
if (challenge->update_dialog)
{
// update challenge info
SetDlgItemText (hWnd, STATICTEXT_CHALLENGE_CHALLENGER, challenge->challenger);
swprintf_s (game_name, WCHAR_SIZEOF (game_name), L"\"%s\" (%.0f %.0f)", challenge->game_type, challenge->initial_time, challenge->increment);
swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Challenge_Question"), game_name);
SetDlgItemText (hWnd, STATICTEXT_CHALLENGE_QUESTION, temp_string);
if (challenge->color == COLOR_BLACK)
swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Challenge_OpponentWillPlayAs"), LOCALIZE (L"Games_Black"));
else if (challenge->color == COLOR_WHITE)
swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Challenge_OpponentWillPlayAs"), LOCALIZE (L"Games_White"));
else
temp_string[0] = 0;
SetDlgItemText (hWnd, STATICTEXT_CHALLENGE_OPPONENTWILLPLAYAS, temp_string);
SetDlgItemText (hWnd, STATICTEXT_CHALLENGE_ISRATEDORNOT, (challenge->is_rated ? LOCALIZE (L"Challenge_Rated"): LOCALIZE (L"Challenge_Unrated")));
challenge->update_dialog = false; // remember we refreshed challenge text
}
}
// call the default dialog message processing function to keep things going
return (false);
}