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