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