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