// dialog_resign.cpp
#include "../common.h"
// dialog template
#define THIS_DIALOG DIALOG_RESIGN
// 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);
static int resign_type;
void DialogBox_Resign (int type)
{
// helper function to fire up the modeless dialog box
resign_type = type; // save the resign type for later use
is_dialogbox_displayed = true;
_beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new thread to display the dialog box
return; // return as soon as the thread is fired up
}
void DialogBox_Resign_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_resign_validated = false;
// see if we're playing against a remote opponent
network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
// is it an online game ?
if (network_player != NULL)
Player_SendBuffer_Add (network_player, 1000, L"resign\n"); // if so, send a resign command
// else we're playing locally (either vs. another human or vs. computer)
else
{
Scene_Shutdown (&the_scene); // release scene
Board_Shutdown (&the_board); // release chess game
Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, FENSTARTUP_STANDARDCHESS); // prepare a new human vs human game (by default)
Scene_Init (&the_scene, &the_board); // initialize scene
SetWindowText (hMainWnd, PROGRAM_NAME); // update window title
if (resign_type == RESIGNTYPE_NEWGAME)
DialogBox_NewGame (); // show the new game dialog box
else if (resign_type == RESIGNTYPE_LOADGAME)
DialogBox_Load (); // show the open dialog box
}
the_board.reevaluate = true; // evaluate the new board
the_scene.update = true; // update scene
return; // finished, we have resigned from game
}
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.
// display the dialog box
if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
is_dialogbox_resign_validated = true;
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;
// 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)
{
// center the window
CenterWindow (hWnd, hMainWnd);
// set dialog icons (small one for title bar & big one for task manager)
SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
// set window title and control texts
SetWindowText (hWnd, LOCALIZE (L"Quit_Title"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_RESIGN_QUESTION), LOCALIZE (L"Resign_Question"));
SetWindowText (GetDlgItem (hWnd, BUTTON_GIVEUP), LOCALIZE (L"Resign_GiveUp"));
SetWindowText (GetDlgItem (hWnd, BUTTON_BACKTOGAME), LOCALIZE (L"Resign_BackToGame"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_RESIGN_STATUSBAR), LOCALIZE (L"Resign_StatusBar"));
// play the "question" system sound
PlaySound ((LPCWSTR) SND_ALIAS_SYSTEMASTERISK, NULL, SND_ALIAS_ID | SND_ASYNC);
// convert the status bar message to a hyperlink
ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_RESIGN_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)
if (wParam_loword == IDCANCEL)
EndDialog (hWnd, 0); // close the dialog box
// else was it the quit button ?
else if (wParam_loword == BUTTON_GIVEUP)
EndDialog (hWnd, 1); // close the dialog box and return a success code
// else was it the back to game button ?
else if (wParam_loword == BUTTON_BACKTOGAME)
EndDialog (hWnd, 0); // close the dialog box
// else was it the status bar hyperlink ?
else if (wParam_loword == STATICTEXT_RESIGN_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);
}