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