// dialog_quit.cpp
 
 
 
#include "../common.h"
 
 
 
 
 
// dialog template
 
#define THIS_DIALOG DIALOG_QUIT
 
 
 
 
 
// prototypes of local functions
 
static void StartThread_ThisDialog (void *thread_parms);
 
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
 
 
 
 
 
void DialogBox_Quit (void)
 
{
 
   // helper function to fire up the modeless dialog box
 
 
 
   is_dialogbox_displayed = true;
 
   _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new thread to display the dialog box
 
 
 
   return; // return as soon as the thread is fired up
 
}
 
 
 
 
 
void DialogBox_Quit_Validated (void)
 
{
 
   // callback function called by the main game thread when the dialog box is validated
 
 
 
   // remember this callback is no longer to be called
 
   is_dialogbox_quit_validated = false;
 
 
 
   // destroy the main window, which in turn will raise the "terminate_everything" global flag
 
   DestroyWindow (hMainWnd);
 
 
 
   return; // finished, we're now quitting the program
 
}
 
 
 
 
 
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 message box using the Common Controls library.
 
 
 
   int retval;
 
 
 
   // display the dialog box and grab its return value
 
   retval = DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog);
 
   if (retval == 1)
 
      is_dialogbox_quit_validated = true; // if we simply want to quit, flag the quit dialog box as confirmed
 
   else if (retval == 2)
 
      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)
 
   is_dialogbox_displayed = false;
 
 
 
   the_board.reevaluate = true; // refresh the GUI buttons if needed
 
   return; // _endthread() implied
 
}
 
 
 
 
 
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
 
{
 
   // message handler for the dialog box
 
 
 
   unsigned short wParam_hiword;
 
   unsigned short wParam_loword;
 
 
 
   // filter out the commonly used message values
 
   wParam_hiword = HIWORD (wParam);
 
   wParam_loword = LOWORD (wParam);
 
 
 
   // have we just fired up this window ?
 
   if (message == WM_INITDIALOG)
 
   {
 
      // center the window
 
      CenterWindow (hWnd, hMainWnd);
 
 
 
      // set dialog icons (small one for title bar & big one for task manager)
 
      SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
 
      SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
 
 
 
      // set window title and control texts
 
      SetWindowText (hWnd, LOCALIZE (L"Quit_Title"));
 
 
 
      // are we online AND has a game started, or are we playing locally in which case it is acceptable to simply quit now ?
 
      if ((Player_FindByType (PLAYER_INTERNET) != NULL) && (the_board.move_count > 1))
 
      {
 
         Static_SetText (GetDlgItem (hWnd, STATICTEXT_QUIT_QUESTION), LOCALIZE (L"Quit_OnlineQuestion"));
 
         SetWindowText (GetDlgItem (hWnd, BUTTON_GIVEUPANDQUIT), LOCALIZE (L"Quit_GiveUpAndQuit"));
 
         SetWindowText (GetDlgItem (hWnd, BUTTON_BACKTOGAME), LOCALIZE (L"Quit_BackToGame"));
 
      }
 
      else
 
      {
 
         Static_SetText (GetDlgItem (hWnd, STATICTEXT_QUIT_QUESTION), LOCALIZE (L"Quit_OfflineQuestion"));
 
         SetWindowText (GetDlgItem (hWnd, BUTTON_GIVEUPANDQUIT), LOCALIZE (L"Quit_SaveAndQuit"));
 
         SetWindowText (GetDlgItem (hWnd, BUTTON_BACKTOGAME), LOCALIZE (L"Quit_SimplyQuit"));
 
      }
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_QUIT_STATUSBAR), LOCALIZE (L"Quit_StatusBar"));
 
 
 
      // play the "important notification" sound
 
      Audio_PlaySound (SOUNDTYPE_IMPORTANT, 0.0f, 0.0f, 0.04f); // play at the center of the board
 
 
 
      // convert the status bar message to a hyperlink
 
      ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_QUIT_STATUSBAR));
 
   }
 
 
 
   // else did we click the close button on the title bar ?
 
   else if (message == WM_CLOSE)
 
      EndDialog (hWnd, 0); // close the dialog box
 
 
 
   // else did we take action on one of the controls ?
 
   else if (message == WM_COMMAND)
 
   {
 
      // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
 
      if (wParam_loword == IDCANCEL)
 
         EndDialog (hWnd, 0); // close the dialog box
 
 
 
      // else was it the "give up and quit" or "save and quit" button ?
 
      else if (wParam_loword == BUTTON_GIVEUPANDQUIT)
 
      {
 
         // are we online AND has a game started, or are we playing locally ?
 
         if ((Player_FindByType (PLAYER_INTERNET) != NULL) && (the_board.move_count > 1))
 
            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
 
         else
 
            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
 
      }
 
 
 
      // else was it the "back to game" or "simply quit" button ?
 
      else if (wParam_loword == BUTTON_BACKTOGAME)
 
      {
 
         // are we online AND has a game started, or are we playing locally ?
 
         if ((Player_FindByType (PLAYER_INTERNET) != NULL) && (the_board.move_count > 1))
 
            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
 
         else
 
            EndDialog (hWnd, 1); // when playing locally, button is labeled "just quit" --> close the dialog box and return a value that tells to quit
 
      }
 
 
 
      // else was it the status bar hyperlink ?
 
      else if (wParam_loword == STATICTEXT_QUIT_STATUSBAR)
 
         ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
 
   }
 
 
 
   // call the default dialog message processing function to keep things going
 
   return (false);
 
}