// dialog_save.cpp
#include "../common.h"
// global variables used in this module only
static wchar_t ofn_customfilter[256] = L"";
static bool quit_after_save = false;
// local prototypes
static void StartThread_ThisDialog (void *thread_parms);
void DialogBox_Save (bool want_quit_after_save)
{
// helper function to fire up the modeless Save dialog box
quit_after_save = want_quit_after_save;
_beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
return; // return as soon as the thread is fired up
}
void DialogBox_Save_Validated (void)
{
// callback function called by the main game thread when the dialog box is validated
int length;
// remember this callback is no longer to be called
is_dialogbox_save_validated = false;
length = wcslen (save_pathname); // get pathname length
// is it a FEN or a PGN filename ?
if ((length > 4) && (_wcsicmp (&save_pathname[length - 4], L".fen") == 0))
{
// save the requested FEN file
if (!FENFile_Save (save_pathname, the_board.moves[the_board.viewed_move].fen_string))
{
messagebox.hWndParent = hMainWnd;
wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage"));
wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_SaveFailed"));
messagebox.flags = MB_ICONWARNING | MB_OK;
DialogBox_Message (&messagebox); // on error, display a modeless error message box
}
}
else
{
// save the requested PGN file
if (!PGNFile_Save (&the_board, save_pathname))
{
messagebox.hWndParent = hMainWnd;
wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage"));
wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_SaveFailed"));
messagebox.flags = MB_ICONWARNING | MB_OK;
DialogBox_Message (&messagebox); // on error, display a modeless error message box
}
}
// did we want to quit after save ?
if (quit_after_save)
is_dialogbox_quit_validated = true; // if so, act as if the quit dialog box was validated
return; // finished, game has been saved
}
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 Save dialog box using the Common Controls library.
OPENFILENAME ofn;
int length;
length = wcslen (save_pathname); // get current save pathname length
// reset and prepare the open file name structure
memset (&ofn, 0, sizeof (ofn));
ofn.lStructSize = sizeof (ofn);
ofn.hwndOwner = hMainWnd;
ofn.lpstrFilter = LOCALIZE (L"GameFileFilter");
if ((length > 4) && (_wcsicmp (&save_pathname[length - 4], L".fen") == 0))
{
ofn.lpstrDefExt = L"fen"; // default extension will be .fen (because it was selected previously)
ofn.nFilterIndex = 2; // second filter (list is 1-based)
}
else
{
ofn.lpstrDefExt = L"pgn"; // default extension will be .pgn
ofn.nFilterIndex = 1; // first filter (list is 1-based)
}
ofn.lpstrCustomFilter = ofn_customfilter;
ofn.nMaxCustFilter = WCHAR_SIZEOF (ofn_customfilter);
ofn.lpstrFile = save_pathname;
ofn.nMaxFile = WCHAR_SIZEOF (save_pathname);
ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_OVERWRITEPROMPT;
// display the dialog box and get the file name
if (GetSaveFileName (&ofn) != 0)
is_dialogbox_save_validated = true;
the_board.reevaluate = true; // refresh the GUI buttons if needed
return; // _endthread() implied
}