Subversion Repositories Games.Chess Giants

Rev

Rev 119 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// dialog_save.cpp
2
 
3
#include "../common.h"
4
 
5
 
6
// global variables used in this module only
7
static wchar_t ofn_customfilter[256] = L"";
119 pmbaty 8
static bool quit_after_save = false;
1 pmbaty 9
 
10
 
11
// local prototypes
12
static void StartThread_ThisDialog (void *thread_parms);
13
 
14
 
119 pmbaty 15
void DialogBox_Save (bool want_quit_after_save)
1 pmbaty 16
{
17
   // helper function to fire up the modeless Save dialog box
18
 
119 pmbaty 19
   quit_after_save = want_quit_after_save;
1 pmbaty 20
   _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
21
 
22
   return; // return as soon as the thread is fired up
23
}
24
 
25
 
26
void DialogBox_Save_Validated (void)
27
{
28
   // callback function called by the main game thread when the dialog box is validated
29
 
30
   int length;
31
 
32
   // remember this callback is no longer to be called
33
   is_dialogbox_save_validated = false;
34
 
35
   length = wcslen (save_pathname); // get pathname length
36
 
37
   // is it a FEN or a PGN filename ?
38
   if ((length > 4) && (_wcsicmp (&save_pathname[length - 4], L".fen") == 0))
39
   {
40
      // save the requested FEN file
41
      if (!FENFile_Save (save_pathname, the_board.moves[the_board.viewed_move].fen_string))
42
      {
43
         messagebox.hWndParent = hMainWnd;
44
         wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage"));
45
         wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_SaveFailed"));
46
         messagebox.flags = MB_ICONWARNING | MB_OK;
47
         DialogBox_Message (&messagebox); // on error, display a modeless error message box
48
      }
49
   }
50
   else
51
   {
52
      // save the requested PGN file
53
      if (!PGNFile_Save (&the_board, save_pathname))
54
      {
55
         messagebox.hWndParent = hMainWnd;
56
         wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage"));
57
         wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_SaveFailed"));
58
         messagebox.flags = MB_ICONWARNING | MB_OK;
59
         DialogBox_Message (&messagebox); // on error, display a modeless error message box
60
      }
61
   }
62
 
119 pmbaty 63
   // did we want to quit after save ?
64
   if (quit_after_save)
65
      is_dialogbox_quit_validated = true; // if so, act as if the quit dialog box was validated
66
 
1 pmbaty 67
   return; // finished, game has been saved
68
}
69
 
70
 
71
static void StartThread_ThisDialog (void *thread_parms)
72
{
73
   // this function runs in a separate thread, for that's the only way (seemingly)
74
   // to implement a non-modal Save dialog box using the Common Controls library.
75
 
76
   OPENFILENAME ofn;
77
   int length;
78
 
79
   length = wcslen (save_pathname); // get current save pathname length
80
 
81
   // reset and prepare the open file name structure
82
   memset (&ofn, 0, sizeof (ofn));
83
   ofn.lStructSize = sizeof (ofn);
84
   ofn.hwndOwner = hMainWnd;
85
   ofn.lpstrFilter = LOCALIZE (L"GameFileFilter");
86
   if ((length > 4) && (_wcsicmp (&save_pathname[length - 4], L".fen") == 0))
87
   {
88
      ofn.lpstrDefExt = L"fen"; // default extension will be .fen (because it was selected previously)
89
      ofn.nFilterIndex = 2; // second filter (list is 1-based)
90
   }
91
   else
92
   {
93
      ofn.lpstrDefExt = L"pgn"; // default extension will be .pgn
94
      ofn.nFilterIndex = 1; // first filter (list is 1-based)
95
   }
96
   ofn.lpstrCustomFilter = ofn_customfilter;
97
   ofn.nMaxCustFilter = WCHAR_SIZEOF (ofn_customfilter);
98
   ofn.lpstrFile = save_pathname;
99
   ofn.nMaxFile = WCHAR_SIZEOF (save_pathname);
100
   ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_OVERWRITEPROMPT;
101
 
102
   // display the dialog box and get the file name
103
   if (GetSaveFileName (&ofn) != 0)
104
      is_dialogbox_save_validated = true;
105
 
124 pmbaty 106
   the_board.reevaluate = true; // refresh the GUI buttons if needed
1 pmbaty 107
   return; // _endthread() implied
108
}