Subversion Repositories Games.Chess Giants

Rev

Rev 119 | Rev 140 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// dialog_quit.cpp
2
 
3
#include "../common.h"
4
 
5
 
6
// dialog template
7
#define THIS_DIALOG DIALOG_QUIT
8
 
9
 
10
// prototypes of local functions
11
static void StartThread_ThisDialog (void *thread_parms);
12
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
13
 
14
 
15
void DialogBox_Quit (void)
16
{
17
   // helper function to fire up the modeless dialog box
18
 
19 pmbaty 19
   _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new thread to display the dialog box
1 pmbaty 20
 
21
   return; // return as soon as the thread is fired up
22
}
23
 
24
 
25
void DialogBox_Quit_Validated (void)
26
{
27
   // callback function called by the main game thread when the dialog box is validated
28
 
29
   // remember this callback is no longer to be called
30
   is_dialogbox_quit_validated = false;
31
 
32
   // destroy the main window, which in turn will raise the "terminate_everything" global flag
33
   DestroyWindow (hMainWnd);
34
 
35
   return; // finished, we're now quitting the program
36
}
37
 
38
 
39
static void StartThread_ThisDialog (void *thread_parms)
40
{
41
   // this function runs in a separate thread, for that's the only way (seemingly)
42
   // to implement a non-modal message box using the Common Controls library.
43
 
119 pmbaty 44
   int retval;
1 pmbaty 45
 
119 pmbaty 46
   // display the dialog box and grab its return value
47
   retval = DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog);
48
   if (retval == 1)
49
      is_dialogbox_quit_validated = true; // if we simply want to quit, flag the quit dialog box as confirmed
50
   else if (retval == 2)
51
      DialogBox_Save (true); // else if we want to save THEN quit, fire up the save dialog box first (and tell it to raise the quit confirmation flag after it returns)
52
 
124 pmbaty 53
   the_board.reevaluate = true; // refresh the GUI buttons if needed
1 pmbaty 54
   return; // _endthread() implied
55
}
56
 
57
 
58
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
59
{
60
   // message handler for the dialog box
61
 
62
   unsigned short wParam_hiword;
63
   unsigned short wParam_loword;
64
 
65
   // filter out the commonly used message values
66
   wParam_hiword = HIWORD (wParam);
67
   wParam_loword = LOWORD (wParam);
68
 
69
   // have we just fired up this window ?
70
   if (message == WM_INITDIALOG)
71
   {
72
      // center the window
73
      CenterWindow (hWnd, hMainWnd);
74
 
75
      // set dialog icons (small one for title bar & big one for task manager)
76
      SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
77
      SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
78
 
79
      // set window title and control texts
80
      SetWindowText (hWnd, LOCALIZE (L"Quit_Title"));
119 pmbaty 81
 
82
      // are we online AND has a game started, or are we playing locally in which case it is acceptable to simply quit now ?
83
      if ((Player_FindByType (PLAYER_INTERNET) != NULL) && (the_board.move_count > 1))
84
      {
85
         Static_SetText (GetDlgItem (hWnd, STATICTEXT_QUIT_QUESTION), LOCALIZE (L"Quit_OnlineQuestion"));
86
         SetWindowText (GetDlgItem (hWnd, BUTTON_GIVEUPANDQUIT), LOCALIZE (L"Quit_GiveUpAndQuit"));
87
         SetWindowText (GetDlgItem (hWnd, BUTTON_BACKTOGAME), LOCALIZE (L"Quit_BackToGame"));
88
      }
89
      else
90
      {
91
         Static_SetText (GetDlgItem (hWnd, STATICTEXT_QUIT_QUESTION), LOCALIZE (L"Quit_OfflineQuestion"));
92
         SetWindowText (GetDlgItem (hWnd, BUTTON_GIVEUPANDQUIT), LOCALIZE (L"Quit_SaveAndQuit"));
93
         SetWindowText (GetDlgItem (hWnd, BUTTON_BACKTOGAME), LOCALIZE (L"Quit_SimplyQuit"));
94
      }
1 pmbaty 95
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_QUIT_STATUSBAR), LOCALIZE (L"Quit_StatusBar"));
96
 
19 pmbaty 97
      // play the "question" system sound
98
      PlaySound ((LPCWSTR) SND_ALIAS_SYSTEMASTERISK, NULL, SND_ALIAS_ID | SND_ASYNC);
99
 
1 pmbaty 100
      // convert the status bar message to a hyperlink
101
      ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_QUIT_STATUSBAR));
102
   }
103
 
104
   // else did we click the close button on the title bar ?
105
   else if (message == WM_CLOSE)
106
      EndDialog (hWnd, 0); // close the dialog box
107
 
108
   // else did we take action on one of the controls ?
109
   else if (message == WM_COMMAND)
110
   {
111
      // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
112
      if (wParam_loword == IDCANCEL)
113
         EndDialog (hWnd, 0); // close the dialog box
114
 
119 pmbaty 115
      // else was it the "give up and quit" or "save and quit" button ?
1 pmbaty 116
      else if (wParam_loword == BUTTON_GIVEUPANDQUIT)
119 pmbaty 117
      {
118
         // are we online AND has a game started, or are we playing locally ?
119
         if ((Player_FindByType (PLAYER_INTERNET) != NULL) && (the_board.move_count > 1))
120
            EndDialog (hWnd, 1); // when playing remotely, button is labeled "give up and quit" --> close the dialog box and return a value that tells to quit
121
         else
122
            EndDialog (hWnd, 2); // when playing locally, button is labeled "save and quit" --> close the dialog box and return a value that tells to display a "save" dialog box
123
      }
1 pmbaty 124
 
119 pmbaty 125
      // else was it the "back to game" or "simply quit" button ?
1 pmbaty 126
      else if (wParam_loword == BUTTON_BACKTOGAME)
119 pmbaty 127
      {
128
         // are we online AND has a game started, or are we playing locally ?
129
         if ((Player_FindByType (PLAYER_INTERNET) != NULL) && (the_board.move_count > 1))
130
            EndDialog (hWnd, 0); // when playing remotely, button is labeled "back to game" --> close the dialog box and return a value that tells to do nothing
131
         else
132
            EndDialog (hWnd, 1); // when playing locally, button is labeled "just quit" --> close the dialog box and return a value that tells to quit
133
      }
1 pmbaty 134
 
135
      // else was it the status bar hyperlink ?
136
      else if (wParam_loword == STATICTEXT_QUIT_STATUSBAR)
137
         ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
138
   }
139
 
140
   // call the default dialog message processing function to keep things going
141
   return (false);
142
}