Subversion Repositories Games.Chess Giants

Rev

Rev 124 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 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
   _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
15
 
16
   return; // return as soon as the thread is fired up
17
}
18
 
19
 
20
void DialogBox_Load_Validated (void)
21
{
22
   // callback function called by the main game thread when the dialog box is validated
23
 
24
   wchar_t fen_string[128];
25
   int length;
26
 
27
   // remember this callback is no longer to be called
28
   is_dialogbox_load_validated = false;
29
 
30
   length = wcslen (load_pathname); // get pathname length
31
 
32
   // is it a FEN or a PGN filename ?
33
   if ((length > 4) && (_wcsicmp (&load_pathname[length - 4], L".fen") == 0))
34
   {
35
      //////////////////////////////
36
      // load the requested FEN game
37
      if (FENFile_Load (load_pathname, fen_string, WCHAR_SIZEOF (fen_string))
38
          && Board_Reset (&the_board, fen_string))
39
      {
40
         animation_endtime = current_time + ANIMATION_DURATION; // start with an animation
41
         sound_playtime = current_time + ANIMATION_DURATION - 0.1f; // play sound near the end of 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
               sound_playtime = current_time + ANIMATION_DURATION - 0.1f; // play sound near the end of animation
67
               the_board.reevaluate = true; // evaluate the new board
68
               the_scene.update = true; // update scene
69
            }
70
            else
71
            {
72
               messagebox.hWndParent = hMainWnd;
73
               wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage"));
74
               wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_LoadFailed"));
75
               messagebox.flags = MB_ICONWARNING | MB_OK;
76
               DialogBox_Message (&messagebox); // on error, display a modeless error message box
77
            }
78
         }
79
 
80
         // else is there more than one game in this PGN file ?
81
         else if (game_count > 1)
82
            Window_Games (); // if so, show the PGN games window
83
      }
84
      else
85
      {
86
         messagebox.hWndParent = hMainWnd;
87
         wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage"));
88
         wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_LoadFailed"));
89
         messagebox.flags = MB_ICONWARNING | MB_OK;
90
         DialogBox_Message (&messagebox); // on error, display a modeless error message box
91
      }
92
   }
93
 
94
   return; // finished, game is loaded
95
}
96
 
97
 
98
static void StartThread_ThisDialog (void *thread_parms)
99
{
100
   // this function runs in a separate thread, for that's the only way (seemingly)
101
   // to implement a non-modal Open dialog box using the Common Controls library.
102
 
103
   OPENFILENAME ofn;
104
 
105
   // reset and prepare the open file name structure
106
   memset (&ofn, 0, sizeof (ofn));
107
   ofn.lStructSize = sizeof (ofn);
108
   ofn.hwndOwner = hMainWnd;
109
   ofn.lpstrFilter = LOCALIZE (L"GameFileFilter");
110
   ofn.nFilterIndex = 1; // first filter (list is 1-based)
111
   ofn.lpstrFile = load_pathname;
112
   ofn.nMaxFile = WCHAR_SIZEOF (load_pathname);
113
   ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
114
 
115
   // display the dialog box and get the file name
116
   if (GetOpenFileName (&ofn) != 0)
117
      is_dialogbox_load_validated = true;
118
 
119
   return; // _endthread() implied
120
}