Subversion Repositories Games.Chess Giants

Rev

Rev 124 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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