Rev 140 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | // dialog_challenge.cpp |
| 2 | |||
| 3 | #include "../common.h" |
||
| 4 | |||
| 5 | |||
| 6 | // dialog template |
||
| 7 | #define THIS_DIALOG DIALOG_CHALLENGE |
||
| 8 | |||
| 9 | |||
| 10 | // local definitions |
||
| 11 | #define TIMER_REFRESHDIALOG 1 |
||
| 12 | |||
| 13 | |||
| 14 | // global variables used in this module only |
||
| 15 | static bool is_challengeaccepted = false; |
||
| 172 | pmbaty | 16 | static wchar_t challenge_gametype[32]; |
| 1 | pmbaty | 17 | static wchar_t challenger_nickname[32]; |
| 18 | |||
| 19 | |||
| 20 | // prototypes of local functions |
||
| 21 | static void StartThread_ThisDialog (void *thread_parms); |
||
| 22 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam); |
||
| 23 | |||
| 24 | |||
| 25 | void DialogBox_Challenge (int challenge_index) |
||
| 26 | { |
||
| 27 | // helper function to fire up the modeless dialog box |
||
| 28 | |||
| 140 | pmbaty | 29 | is_dialogbox_displayed = true; |
| 1 | pmbaty | 30 | _beginthread (StartThread_ThisDialog, 0, (void *) challenge_index); // fire up the thread |
| 31 | return; // return as soon as the thread is fired up |
||
| 32 | } |
||
| 33 | |||
| 34 | |||
| 35 | void DialogBox_Challenge_Validated (void) |
||
| 36 | { |
||
| 37 | // callback function called by the main game thread when the dialog box is validated |
||
| 38 | |||
| 39 | player_t *network_player; |
||
| 40 | interlocutor_t *interlocutor; |
||
| 41 | |||
| 42 | // remember this callback is no longer to be called |
||
| 43 | is_dialogbox_challenge_validated = false; |
||
| 44 | |||
| 45 | network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player |
||
| 46 | if (network_player == NULL) |
||
| 47 | return; // consistency check |
||
| 48 | |||
| 49 | // did we accept the challenge ? if so, send an accept reply, else decline it |
||
| 50 | if (is_challengeaccepted) |
||
| 51 | { |
||
| 52 | // send the accept notification and print a notification in this player's chat window |
||
| 53 | Player_SendBuffer_Add (network_player, 1000, L"accept %s\n", challenger_nickname); |
||
| 54 | interlocutor = Interlocutor_FindOrCreate (challenger_nickname); |
||
| 55 | Interlocutor_Notify (interlocutor, LOCALIZE (L"Chat_InvitationAcceptedByYou"), challenger_nickname); |
||
| 56 | if (IsWindow (interlocutor->hWnd)) |
||
| 57 | ShowWindow (interlocutor->hWnd, SW_MINIMIZE); // minimize chat window immediately |
||
| 172 | pmbaty | 58 | |
| 59 | // remember the game rules |
||
| 60 | wcscpy_s (the_board.game_rules, WCHAR_SIZEOF (the_board.game_rules), challenge_gametype); |
||
| 1 | pmbaty | 61 | } |
| 62 | else |
||
| 63 | { |
||
| 64 | // send the decline notification and print a notification in this player's chat window |
||
| 65 | Player_SendBuffer_Add (network_player, 1000, L"decline %s\n", challenger_nickname); |
||
| 66 | Interlocutor_Notify (Interlocutor_FindOrCreate (challenger_nickname), LOCALIZE (L"Chat_InvitationDeclinedByYou"), challenger_nickname); |
||
| 67 | } |
||
| 68 | |||
| 69 | return; // finished, challenge has been either accepted or declined |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | static void StartThread_ThisDialog (void *thread_parms) |
||
| 74 | { |
||
| 75 | // this function runs in a separate thread, for that's the only way (seemingly) |
||
| 76 | // to implement a non-modal message box using the Common Controls library. |
||
| 77 | |||
| 78 | int challenge_index; |
||
| 79 | int interlocutor_index; |
||
| 80 | challenge_t *challenge; |
||
| 81 | HWND hParentWnd; |
||
| 82 | int retval; |
||
| 140 | pmbaty | 83 | //int error; |
| 1 | pmbaty | 84 | |
| 85 | challenge_index = (int) thread_parms; |
||
| 86 | |||
| 87 | challenge = &challenges[challenge_index]; // quick access to challenge |
||
| 88 | |||
| 89 | // see if we are already talking to this interlocutor, loop through all of them... |
||
| 90 | hParentWnd = hMainWnd; // assume parent window will be the main window, until told otherwise |
||
| 91 | for (interlocutor_index = 0; interlocutor_index < interlocutor_count; interlocutor_index++) |
||
| 92 | if (interlocutors[interlocutor_index].is_active && (_wcsicmp (challenge->challenger, interlocutors[interlocutor_index].nickname) == 0)) |
||
| 93 | { |
||
| 94 | hParentWnd = interlocutors[interlocutor_index].hWnd; // if found, parent window will be this interlocutor's chat window |
||
| 95 | break; // break as soon as we find it |
||
| 96 | } |
||
| 97 | |||
| 98 | // display the dialog box |
||
| 99 | retval = DialogBoxParam (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hParentWnd, DialogProc_ThisDialog, (LPARAM) challenge_index); |
||
| 140 | pmbaty | 100 | is_dialogbox_displayed = false; |
| 101 | //error = GetLastError (); |
||
| 1 | pmbaty | 102 | is_challengeaccepted = (retval != 0); |
| 103 | is_dialogbox_challenge_validated = true; |
||
| 104 | |||
| 105 | challenge = &challenges[challenge_index]; // restore access to challenge (it may have relocated !) |
||
| 106 | challenge->is_active = false; // remember challenge has gone away |
||
| 107 | |||
| 124 | pmbaty | 108 | the_board.reevaluate = true; // refresh the GUI buttons if needed |
| 1 | pmbaty | 109 | return; // _endthread() implied |
| 110 | } |
||
| 111 | |||
| 112 | |||
| 113 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam) |
||
| 114 | { |
||
| 115 | // message handler for the dialog box |
||
| 116 | |||
| 117 | unsigned short wParam_hiword; |
||
| 118 | unsigned short wParam_loword; |
||
| 119 | challenge_t *challenge; |
||
| 120 | wchar_t game_name[64]; |
||
| 121 | wchar_t temp_string[256]; |
||
| 122 | |||
| 123 | // filter out the commonly used message values |
||
| 124 | wParam_hiword = HIWORD (wParam); |
||
| 125 | wParam_loword = LOWORD (wParam); |
||
| 126 | |||
| 127 | // have we just fired up this window ? |
||
| 128 | if (message == WM_INITDIALOG) |
||
| 129 | { |
||
| 130 | challenge = &challenges[lParam]; // quick access to challenge |
||
| 131 | challenge->hWnd = hWnd; // save challenge window handle |
||
| 132 | SetWindowLongPtr (hWnd, DWLP_USER, lParam); // and attach challenge number to window |
||
| 133 | |||
| 134 | // center the window |
||
| 135 | CenterWindow (hWnd, hMainWnd); |
||
| 136 | |||
| 137 | // set window title and control texts |
||
| 138 | SetWindowText (hWnd, LOCALIZE (L"Challenge_Title")); |
||
| 139 | SetDlgItemText (hWnd, BUTTON_ACCEPT, LOCALIZE (L"Button_Accept")); |
||
| 140 | SetDlgItemText (hWnd, BUTTON_DECLINE, LOCALIZE (L"Button_Decline")); |
||
| 141 | SetDlgItemText (hWnd, STATICTEXT_CHALLENGE_STATUSBAR, LOCALIZE (L"Challenge_StatusBar")); |
||
| 142 | |||
| 143 | // start refreshing the window every second |
||
| 144 | SetTimer (hWnd, TIMER_REFRESHDIALOG, 1000, NULL); |
||
| 145 | SendMessage (hWnd, WM_TIMER, TIMER_REFRESHDIALOG, 0); |
||
| 146 | |||
| 147 | // convert the challenger name and the status bar message to hyperlinks |
||
| 148 | ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_CHALLENGE_CHALLENGER)); |
||
| 149 | ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_CHALLENGE_STATUSBAR)); |
||
| 150 | } |
||
| 151 | |||
| 152 | // else did we click the close button on the title bar ? |
||
| 153 | else if (message == WM_CLOSE) |
||
| 154 | { |
||
| 155 | KillTimer (hWnd, TIMER_REFRESHDIALOG); // destroy the timer we used to refresh the dialog text |
||
| 156 | EndDialog (hWnd, 0); // close the dialog box |
||
| 157 | } |
||
| 158 | |||
| 159 | // else did we take action on one of the controls ? |
||
| 160 | else if (message == WM_COMMAND) |
||
| 161 | { |
||
| 162 | // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key) |
||
| 163 | // OR was it the "decline" button ? |
||
| 164 | if ((wParam_loword == IDCANCEL) || (wParam_loword == BUTTON_DECLINE)) |
||
| 165 | { |
||
| 166 | challenge = &challenges[GetWindowLongPtr (hWnd, DWLP_USER)]; // quick access to challenge |
||
| 167 | wcscpy_s (challenger_nickname, WCHAR_SIZEOF (challenger_nickname), challenge->challenger); // have a copy of challenger's name |
||
| 172 | pmbaty | 168 | wcscpy_s (challenge_gametype, WCHAR_SIZEOF (challenge_gametype), challenge->game_type); // have a copy of the game type |
| 1 | pmbaty | 169 | KillTimer (hWnd, TIMER_REFRESHDIALOG); // destroy the timer we used to refresh the dialog text |
| 170 | EndDialog (hWnd, 0); // close the dialog box |
||
| 171 | } |
||
| 172 | |||
| 173 | // else was it the "accept" button ? |
||
| 174 | else if (wParam_loword == BUTTON_ACCEPT) |
||
| 175 | { |
||
| 176 | challenge = &challenges[GetWindowLongPtr (hWnd, DWLP_USER)]; // quick access to challenge |
||
| 177 | wcscpy_s (challenger_nickname, WCHAR_SIZEOF (challenger_nickname), challenge->challenger); // have a copy of challenger's name |
||
| 172 | pmbaty | 178 | wcscpy_s (challenge_gametype, WCHAR_SIZEOF (challenge_gametype), challenge->game_type); // have a copy of the game type |
| 1 | pmbaty | 179 | KillTimer (hWnd, TIMER_REFRESHDIALOG); // destroy the timer we used to refresh the dialog text |
| 180 | EndDialog (hWnd, 1); // close the dialog box and return OK |
||
| 181 | } |
||
| 182 | |||
| 183 | // else did we click the challenger name ? |
||
| 184 | else if (wParam_loword == STATICTEXT_CHALLENGE_CHALLENGER) |
||
| 185 | { |
||
| 186 | challenge = &challenges[GetWindowLongPtr (hWnd, DWLP_USER)]; // quick access to challenge |
||
| 187 | PlayerCard_FindOrCreate (challenge->challenger); // fire up the dialog box (TODO: move to ._Validated()) |
||
| 188 | } |
||
| 189 | |||
| 190 | // else was it the status bar hyperlink ? |
||
| 191 | else if (wParam_loword == STATICTEXT_CHALLENGE_STATUSBAR) |
||
| 192 | ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized |
||
| 193 | } |
||
| 194 | |||
| 195 | // else is it a timer event AND is it our refresh timer ? |
||
| 196 | else if ((message == WM_TIMER) && (wParam == TIMER_REFRESHDIALOG)) |
||
| 197 | { |
||
| 198 | challenge = &challenges[GetWindowLongPtr (hWnd, DWLP_USER)]; // quick access to challenge |
||
| 199 | |||
| 200 | // do we need to update dialog ? |
||
| 201 | if (challenge->update_dialog) |
||
| 202 | { |
||
| 203 | // update challenge info |
||
| 204 | SetDlgItemText (hWnd, STATICTEXT_CHALLENGE_CHALLENGER, challenge->challenger); |
||
| 205 | swprintf_s (game_name, WCHAR_SIZEOF (game_name), L"\"%s\" (%.0f %.0f)", challenge->game_type, challenge->initial_time, challenge->increment); |
||
| 206 | swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Challenge_Question"), game_name); |
||
| 207 | SetDlgItemText (hWnd, STATICTEXT_CHALLENGE_QUESTION, temp_string); |
||
| 208 | if (challenge->color == COLOR_BLACK) |
||
| 209 | swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Challenge_OpponentWillPlayAs"), LOCALIZE (L"Games_Black")); |
||
| 210 | else if (challenge->color == COLOR_WHITE) |
||
| 211 | swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Challenge_OpponentWillPlayAs"), LOCALIZE (L"Games_White")); |
||
| 212 | else |
||
| 213 | temp_string[0] = 0; |
||
| 214 | SetDlgItemText (hWnd, STATICTEXT_CHALLENGE_OPPONENTWILLPLAYAS, temp_string); |
||
| 215 | SetDlgItemText (hWnd, STATICTEXT_CHALLENGE_ISRATEDORNOT, (challenge->is_rated ? LOCALIZE (L"Challenge_Rated"): LOCALIZE (L"Challenge_Unrated"))); |
||
| 216 | |||
| 217 | challenge->update_dialog = false; // remember we refreshed challenge text |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | // call the default dialog message processing function to keep things going |
||
| 222 | return (false); |
||
| 223 | } |