// dialog_sendchallenge.cpp
#include "../common.h"
// dialog template
#define THIS_DIALOG DIALOG_SENDCHALLENGE
// local type definitions
typedef struct sendchallenge_s
{
wchar_t challengee[32]; // challengee's nickname
wchar_t game_rules[32]; // the rules with which to play (standard, losers, etc)
int color; // color we want to play
bool is_rated; // whether this game is rated or not
int initial_time; // initial time of the game for each color
int increment; // eventual time increment (Fischer pace)
} sendchallenge_t;
// global variables used in this module only
static sendchallenge_t sendchallenge = { L"", L"", COLOR_UNSPECIFIED, false, 2, 12};
static HWND hThisWnd = NULL;
// 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_SendChallenge (wchar_t *challengee_name)
{
// helper function to fire up the modeless dialog box
// prepare the sendchallenge structure
wcscpy_s (sendchallenge.challengee, WCHAR_SIZEOF (sendchallenge.challengee), challengee_name);
sendchallenge.game_rules[0] = 0; // no special rules until told otherwise
is_dialogbox_displayed = true;
_beginthread (StartThread_ThisDialog, 0, NULL); // fire up the thread
return; // return as soon as the thread is fired up
}
void DialogBox_SendChallenge_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_sendchallenge_validated = false;
network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
if (network_player == NULL)
return; // consistency check
// send the challenge
Player_SendBuffer_Add (network_player, 1000, L"match %s %s %d %d %s %s\n",
sendchallenge.challengee,
(sendchallenge.is_rated ? L"rated" : L"unrated"),
sendchallenge.initial_time,
sendchallenge.increment,
(sendchallenge.color == COLOR_BLACK ? L"black" : (sendchallenge.color == COLOR_WHITE ? L"white" : L"")),
sendchallenge.game_rules);
// send a notification to this player's chat window
Interlocutor_Notify (Interlocutor_FindOrCreate (sendchallenge.challengee), LOCALIZE (L"Chat_InvitationSent"), sendchallenge.challengee);
return; // finished, invitation is sent
}
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.
if (IsWindow (hThisWnd))
SendMessage (hThisWnd, WM_CLOSE, 0, 0); // if such a dialog already exists, close it
// display the dialog box
if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
is_dialogbox_sendchallenge_validated = true; // notify main game thread to fire up our callback
is_dialogbox_displayed = false;
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;
wchar_t temp_string[256];
BOOL was_translated; // needs to be of type BOOL (int)
// 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)
{
hThisWnd = hWnd; // save the dialog window handle
// center the window
CenterWindow (hWnd, hMainWnd);
// set window title and control texts
SetWindowText (hWnd, LOCALIZE (L"SendChallenge_Title"));
swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"SendChallenge_Question"), sendchallenge.challengee);
SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_QUESTION, temp_string);
SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_COLOR, LOCALIZE (L"SendChallenge_ColorIWishToPlay"));
ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), L"");
ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), LOCALIZE (L"Games_Black"));
ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), LOCALIZE (L"Games_White"));
ComboBox_SetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), 1 + sendchallenge.color);
SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_INITIALTIME, LOCALIZE (L"SendChallenge_InitialTime"));
SetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INITIALTIME, sendchallenge.initial_time, false);
SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_INCREMENT, LOCALIZE (L"SendChallenge_Increment"));
SetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INCREMENT, sendchallenge.increment, false);
SetDlgItemText (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME, LOCALIZE (L"SendChallenge_RatedUnrated"));
Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME), (sendchallenge.is_rated ? BST_CHECKED : BST_UNCHECKED));
SetDlgItemText (hWnd, BUTTON_INVITE, LOCALIZE (L"Button_Invite"));
SetDlgItemText (hWnd, BUTTON_CANCEL, LOCALIZE (L"Button_Cancel"));
SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_STATUSBAR, LOCALIZE (L"SendChallenge_StatusBar"));
// convert the status bar message to hyperlink
ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_SENDCHALLENGE_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 "cancel" button ?
if ((wParam_loword == IDCANCEL) || (wParam_loword == BUTTON_CANCEL))
EndDialog (hWnd, 0); // close the dialog box
// else was it the "invite" button ?
else if (wParam_loword == BUTTON_INVITE)
{
// retrieve the control values
sendchallenge.color = ComboBox_GetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR)) - 1;
sendchallenge.initial_time = GetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INITIALTIME, &was_translated, false);
if (!was_translated || (sendchallenge.initial_time == 0))
sendchallenge.initial_time = 2; // default value
sendchallenge.increment = GetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INCREMENT, &was_translated, false);
if (!was_translated)
sendchallenge.increment = 12; // default value
sendchallenge.is_rated = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME)) != 0);
sendchallenge.game_rules[0] = 0; // TODO: support other game rules
EndDialog (hWnd, 1); // close the dialog box and return OK
}
// else was it the status bar hyperlink ?
else if (wParam_loword == STATICTEXT_SENDCHALLENGE_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);
}