// dialog_load.cpp
 
 
 
#include "../common.h"
 
 
 
 
 
// local prototypes
 
static void StartThread_ThisDialog (void *thread_parms);
 
 
 
 
 
void DialogBox_Load (void)
 
{
 
   // helper function to fire up the modeless dialog box
 
 
 
   _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
 
 
 
   return; // return as soon as the thread is fired up
 
}
 
 
 
 
 
void DialogBox_Load_Validated (void)
 
{
 
   // callback function called by the main game thread when the dialog box is validated
 
 
 
   wchar_t fen_string[128];
 
   int length;
 
 
 
   // remember this callback is no longer to be called
 
   is_dialogbox_load_validated = false;
 
 
 
   length = wcslen (load_pathname); // get pathname length
 
 
 
   // is it a FEN or a PGN filename ?
 
   if ((length > 4) && (_wcsicmp (&load_pathname[length - 4], L".fen") == 0))
 
   {
 
      //////////////////////////////
 
      // load the requested FEN game
 
      if (FENFile_Load (load_pathname, fen_string, WCHAR_SIZEOF (fen_string))
 
          && Board_Reset (&the_board, fen_string))
 
      {
 
         animation_endtime = current_time + ANIMATION_DURATION; // start with an animation
 
         sound_playtime = current_time + ANIMATION_DURATION - 0.1f; // play sound near the end of animation
 
         the_board.reevaluate = true; // evaluate the new board
 
         the_scene.update = true; // update scene
 
      }
 
      else
 
      {
 
         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_LoadFailed"));
 
         messagebox.flags = MB_ICONWARNING | MB_OK;
 
         DialogBox_Message (&messagebox); // on error, display a modeless error message box
 
      }
 
   }
 
   else
 
   {
 
      //////////////////////////////
 
      // load the requested PGN file
 
      if (PGNFile_Load (load_pathname))
 
      {
 
         // is there only one game in this PGN file ?
 
         if (game_count == 1)
 
         {
 
            if (PGNFile_LoadGame (&the_board, load_pathname, &games[0])) // then automatically choose the first one
 
            {
 
               animation_endtime = current_time + ANIMATION_DURATION; // start with an animation
 
               sound_playtime = current_time + ANIMATION_DURATION - 0.1f; // play sound near the end of animation
 
               the_board.reevaluate = true; // evaluate the new board
 
               the_scene.update = true; // update scene
 
            }
 
            else
 
            {
 
               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_LoadFailed"));
 
               messagebox.flags = MB_ICONWARNING | MB_OK;
 
               DialogBox_Message (&messagebox); // on error, display a modeless error message box
 
            }
 
         }
 
 
 
         // else is there more than one game in this PGN file ?
 
         else if (game_count > 1)
 
            Window_Games (); // if so, show the PGN games window
 
      }
 
      else
 
      {
 
         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_LoadFailed"));
 
         messagebox.flags = MB_ICONWARNING | MB_OK;
 
         DialogBox_Message (&messagebox); // on error, display a modeless error message box
 
      }
 
   }
 
 
 
   return; // finished, game is loaded
 
}
 
 
 
 
 
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 Open dialog box using the Common Controls library.
 
 
 
   OPENFILENAME ofn;
 
 
 
   // 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");
 
   ofn.nFilterIndex = 1; // first filter (list is 1-based)
 
   ofn.lpstrFile = load_pathname;
 
   ofn.nMaxFile = WCHAR_SIZEOF (load_pathname);
 
   ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
 
 
 
   // display the dialog box and get the file name
 
   if (GetOpenFileName (&ofn) != 0)
 
      is_dialogbox_load_validated = true;
 
 
 
   return; // _endthread() implied
 
}