Subversion Repositories Games.Chess Giants

Rev

Rev 130 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // dialog_load.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // local prototypes
  7. static void StartThread_ThisDialog (void *thread_parms);
  8.  
  9.  
  10. void DialogBox_Load (void)
  11. {
  12.    // helper function to fire up the modeless dialog box
  13.  
  14.    is_dialogbox_displayed = true;
  15.    _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
  16.  
  17.    return; // return as soon as the thread is fired up
  18. }
  19.  
  20.  
  21. void DialogBox_Load_Validated (void)
  22. {
  23.    // callback function called by the main game thread when the dialog box is validated
  24.  
  25.    wchar_t fen_string[128];
  26.    int length;
  27.  
  28.    // remember this callback is no longer to be called
  29.    is_dialogbox_load_validated = false;
  30.  
  31.    length = wcslen (load_pathname); // get pathname length
  32.  
  33.    // is it a FEN or a PGN filename ?
  34.    if ((length > 4) && (_wcsicmp (&load_pathname[length - 4], L".fen") == 0))
  35.    {
  36.       //////////////////////////////
  37.       // load the requested FEN game
  38.       if (FENFile_Load (load_pathname, fen_string, WCHAR_SIZEOF (fen_string))
  39.           && Board_Reset (&the_board, fen_string))
  40.       {
  41.          animation_endtime = current_time + ANIMATION_DURATION; // start with an animation
  42.          the_board.reevaluate = true; // evaluate the new board
  43.          the_scene.update = true; // update scene
  44.       }
  45.       else
  46.       {
  47.          messagebox.hWndParent = hMainWnd;
  48.          wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage"));
  49.          wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_LoadFailed"));
  50.          messagebox.flags = MB_ICONWARNING | MB_OK;
  51.          DialogBox_Message (&messagebox); // on error, display a modeless error message box
  52.       }
  53.    }
  54.    else
  55.    {
  56.       //////////////////////////////
  57.       // load the requested PGN file
  58.       if (PGNFile_Load (load_pathname))
  59.       {
  60.          // is there only one game in this PGN file ?
  61.          if (game_count == 1)
  62.          {
  63.             if (PGNFile_LoadGame (&the_board, load_pathname, &games[0])) // then automatically choose the first one
  64.             {
  65.                animation_endtime = current_time + ANIMATION_DURATION; // start with an animation
  66.                the_board.reevaluate = true; // evaluate the new board
  67.                the_scene.update = true; // update scene
  68.             }
  69.             else
  70.             {
  71.                messagebox.hWndParent = hMainWnd;
  72.                wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage"));
  73.                wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_LoadFailed"));
  74.                messagebox.flags = MB_ICONWARNING | MB_OK;
  75.                DialogBox_Message (&messagebox); // on error, display a modeless error message box
  76.             }
  77.          }
  78.  
  79.          // else is there more than one game in this PGN file ?
  80.          else if (game_count > 1)
  81.             Window_Games (); // if so, show the PGN games window
  82.       }
  83.       else
  84.       {
  85.          messagebox.hWndParent = hMainWnd;
  86.          wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage"));
  87.          wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_LoadFailed"));
  88.          messagebox.flags = MB_ICONWARNING | MB_OK;
  89.          DialogBox_Message (&messagebox); // on error, display a modeless error message box
  90.       }
  91.    }
  92.  
  93.    return; // finished, game is loaded
  94. }
  95.  
  96.  
  97. static void StartThread_ThisDialog (void *thread_parms)
  98. {
  99.    // this function runs in a separate thread, for that's the only way (seemingly)
  100.    // to implement a non-modal Open dialog box using the Common Controls library.
  101.  
  102.    OPENFILENAME ofn;
  103.  
  104.    // reset and prepare the open file name structure
  105.    memset (&ofn, 0, sizeof (ofn));
  106.    ofn.lStructSize = sizeof (ofn);
  107.    ofn.hwndOwner = hMainWnd;
  108.    ofn.lpstrFilter = LOCALIZE (L"GameFileFilter");
  109.    ofn.nFilterIndex = 1; // first filter (list is 1-based)
  110.    ofn.lpstrFile = load_pathname;
  111.    ofn.nMaxFile = WCHAR_SIZEOF (load_pathname);
  112.    ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  113.  
  114.    // display the dialog box and get the file name
  115.    if (GetOpenFileName (&ofn) != 0)
  116.       is_dialogbox_load_validated = true;
  117.    is_dialogbox_displayed = false;
  118.  
  119.    the_board.reevaluate = true; // refresh the GUI buttons if needed
  120.    return; // _endthread() implied
  121. }
  122.