// dialog_sendseek.cpp
#include "../common.h"
// dialog template
#define THIS_DIALOG DIALOG_SENDSEEK
// local type definitions
typedef struct sendseek_s
{
int color; // color we want to play
int initial_time; // initial time of the game for each color
int increment; // eventual time increment (Fischer pace)
int rating_from; // opponent rating must be at least
int rating_to; // opponent rating must be at most
bool is_rated; // whether this game is rated or not
bool is_autostart; // whether this game is rated or not
} sendseek_t;
// global variables used in this module only
static sendseek_t sendseek = { COLOR_UNSPECIFIED, 2, 12, 0, 0, false, false };
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_SendSeek (void)
{
// helper function to fire up the modeless dialog box
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_SendSeek_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_sendseek_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"seek %d %d %s %s %s %d-%d\n",
sendseek.initial_time,
sendseek.increment,
(sendseek.is_rated ? L"rated" : L"unrated"),
(sendseek.color == COLOR_BLACK ? L"black" : (sendseek.color == COLOR_WHITE ? L"white" : L"")),
(sendseek.is_autostart ? L"auto" : L"manual"),
sendseek.rating_from,
sendseek.rating_to);
// display a message box to the user
messagebox.hWndParent = hMainWnd;
wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"SendSeek_Title"));
wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"SendSeek_SeekSent"));
messagebox.flags = MB_ICONINFORMATION | MB_OK;
DialogBox_Message (&messagebox);
return; // finished, game request 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_sendseek_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;
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"SendSeek_Title"));
SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_QUESTION, LOCALIZE (L"SendSeek_Question"));
SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_COLOR, LOCALIZE (L"SendSeek_ColorIWishToPlay"));
ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), L"");
ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), LOCALIZE (L"Games_White"));
ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), LOCALIZE (L"Games_Black"));
ComboBox_SetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), 1 + sendseek.color);
SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_INITIALTIME, LOCALIZE (L"SendSeek_InitialTime"));
SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INITIALTIME, sendseek.initial_time, false);
SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_INCREMENT, LOCALIZE (L"SendSeek_Increment"));
SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INCREMENT, sendseek.increment, false);
SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_RATINGFROM, LOCALIZE (L"SendSeek_RatingFrom"));
if ((sendseek.rating_from > 0) && (sendseek.rating_from < 9999))
SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGFROM, sendseek.rating_from, false);
SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_RATINGTO, LOCALIZE (L"SendSeek_RatingTo"));
if ((sendseek.rating_to > 0) && (sendseek.rating_to < 9999))
SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGTO, sendseek.rating_to, false);
SetDlgItemText (hWnd, CHECKBOX_SENDSEEK_RATETHISGAME, LOCALIZE (L"SendSeek_RatedUnrated"));
Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_RATETHISGAME), (sendseek.is_rated ? BST_CHECKED : BST_UNCHECKED));
SetDlgItemText (hWnd, CHECKBOX_SENDSEEK_AUTOSTART, LOCALIZE (L"SendSeek_AutoStart"));
Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_AUTOSTART), (sendseek.is_autostart ? BST_CHECKED : BST_UNCHECKED));
SetDlgItemText (hWnd, BUTTON_SENDSEEK, LOCALIZE (L"Button_Send"));
SetDlgItemText (hWnd, BUTTON_CANCEL, LOCALIZE (L"Button_Cancel"));
SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_STATUSBAR, LOCALIZE (L"SendSeek_StatusBar"));
// convert the status bar message to hyperlink
ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_SENDSEEK_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 "send" button ?
else if (wParam_loword == BUTTON_SENDSEEK)
{
// retrieve the control values
sendseek.color = ComboBox_GetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR)) - 1;
sendseek.initial_time = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INITIALTIME, &was_translated, false);
if (!was_translated || (sendseek.initial_time == 0))
sendseek.initial_time = 2; // default value
sendseek.increment = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INCREMENT, &was_translated, false);
if (!was_translated)
sendseek.increment = 12; // default value
sendseek.rating_from = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGFROM, &was_translated, false);
if (!was_translated)
sendseek.rating_from = 0; // default value
sendseek.rating_to = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGTO, &was_translated, false);
if (!was_translated || (sendseek.rating_to == 0) || (sendseek.rating_to < sendseek.rating_from))
sendseek.rating_to = 9999; // default value
sendseek.is_rated = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_RATETHISGAME)) != 0);
sendseek.is_autostart = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_AUTOSTART)) != 0);
EndDialog (hWnd, 1); // close the dialog box and return OK
}
// else was it the status bar hyperlink ?
else if (wParam_loword == STATICTEXT_SENDSEEK_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);
}