Rev 124 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | // dialog_takeback.cpp |
2 | |||
3 | #include "../common.h" |
||
4 | |||
5 | |||
6 | // dialog template |
||
7 | #define THIS_DIALOG DIALOG_TAKEBACK |
||
8 | |||
9 | |||
10 | // global variables used in this module only |
||
11 | static bool is_takebackaccepted = false; |
||
12 | |||
13 | |||
14 | // prototypes of local functions |
||
15 | static void StartThread_ThisDialog (void *thread_parms); |
||
16 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam); |
||
17 | |||
18 | |||
19 | void DialogBox_Takeback (int howmany_halfmoves) |
||
20 | { |
||
21 | // helper function to fire up the modeless dialog box |
||
22 | |||
23 | _beginthread (StartThread_ThisDialog, 0, (void *) howmany_halfmoves); // fire up the thread |
||
24 | return; // return as soon as the thread is fired up |
||
25 | } |
||
26 | |||
27 | |||
28 | void DialogBox_Takeback_Validated (void) |
||
29 | { |
||
30 | // callback function called by the main game thread when the dialog box is validated |
||
31 | |||
32 | player_t *network_player; |
||
33 | |||
34 | // remember this callback is no longer to be called |
||
35 | is_dialogbox_takeback_validated = false; |
||
36 | |||
37 | network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player |
||
38 | if (network_player == NULL) |
||
39 | return; // consistency check |
||
40 | |||
41 | // did we accept the challenge ? if so, send an accept reply, else decline it |
||
42 | if (is_takebackaccepted) |
||
43 | Player_SendBuffer_Add (network_player, 1000, L"accept %s\n", network_player->name); // send the accept notification |
||
44 | else |
||
45 | Player_SendBuffer_Add (network_player, 1000, L"decline %s\n", network_player->name); // send the decline notification |
||
46 | |||
47 | return; // finished, challenge has been either accepted or declined |
||
48 | } |
||
49 | |||
50 | |||
51 | static void StartThread_ThisDialog (void *thread_parms) |
||
52 | { |
||
53 | // this function runs in a separate thread, for that's the only way (seemingly) |
||
54 | // to implement a non-modal message box using the Common Controls library. |
||
55 | |||
56 | player_t *network_player; |
||
57 | int interlocutor_index; |
||
58 | int howmany_halfmoves; |
||
59 | HWND hParentWnd; |
||
60 | int retval; |
||
61 | int error; |
||
62 | |||
63 | howmany_halfmoves = (int) thread_parms; |
||
64 | network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player |
||
65 | if (network_player == NULL) |
||
66 | return; // consistency check |
||
67 | |||
68 | // see if we are already talking to this interlocutor, loop through all of them... |
||
69 | hParentWnd = hMainWnd; // assume parent window will be the main window, until told otherwise |
||
70 | for (interlocutor_index = 0; interlocutor_index < interlocutor_count; interlocutor_index++) |
||
71 | if (interlocutors[interlocutor_index].is_active && (_wcsicmp (network_player->name, interlocutors[interlocutor_index].nickname) == 0)) |
||
72 | { |
||
73 | hParentWnd = interlocutors[interlocutor_index].hWnd; // if found, parent window will be this interlocutor's chat window |
||
74 | break; // break as soon as we find it |
||
75 | } |
||
76 | |||
77 | // display the dialog box |
||
78 | retval = DialogBoxParam (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hParentWnd, DialogProc_ThisDialog, (LPARAM) howmany_halfmoves); |
||
79 | error = GetLastError (); |
||
80 | is_takebackaccepted = (retval != 0); |
||
81 | is_dialogbox_takeback_validated = true; |
||
82 | |||
83 | return; // _endthread() implied |
||
84 | } |
||
85 | |||
86 | |||
87 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam) |
||
88 | { |
||
89 | // message handler for the dialog box |
||
90 | |||
91 | unsigned short wParam_hiword; |
||
92 | unsigned short wParam_loword; |
||
93 | int howmany_halfmoves; |
||
94 | player_t *network_player; |
||
95 | wchar_t temp_string[256]; |
||
96 | |||
97 | // filter out the commonly used message values |
||
98 | wParam_hiword = HIWORD (wParam); |
||
99 | wParam_loword = LOWORD (wParam); |
||
100 | |||
101 | // have we just fired up this window ? |
||
102 | if (message == WM_INITDIALOG) |
||
103 | { |
||
104 | howmany_halfmoves = (int) lParam; |
||
105 | network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player |
||
106 | if (network_player == NULL) |
||
107 | return (false); // consistency check |
||
108 | |||
109 | // center the window |
||
110 | CenterWindow (hWnd, hMainWnd); |
||
111 | |||
112 | // set window title and control texts |
||
113 | SetWindowText (hWnd, LOCALIZE (L"ImportantMessage")); |
||
114 | swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Chat_TakebackRequestReceived"), network_player->name, howmany_halfmoves); |
||
115 | SetDlgItemText (hWnd, STATICTEXT_TAKEBACK_QUESTION, temp_string); |
||
116 | SetDlgItemText (hWnd, BUTTON_ACCEPT, LOCALIZE (L"Button_Accept")); |
||
117 | SetDlgItemText (hWnd, BUTTON_DECLINE, LOCALIZE (L"Button_Decline")); |
||
118 | SetDlgItemText (hWnd, STATICTEXT_TAKEBACK_STATUSBAR, LOCALIZE (L"Chat_StatusBar")); |
||
119 | |||
120 | // convert the status bar message to a hyperlink |
||
121 | ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_TAKEBACK_STATUSBAR)); |
||
122 | } |
||
123 | |||
124 | // else did we click the close button on the title bar ? |
||
125 | else if (message == WM_CLOSE) |
||
126 | EndDialog (hWnd, 0); // close the dialog box |
||
127 | |||
128 | // else did we take action on one of the controls ? |
||
129 | else if (message == WM_COMMAND) |
||
130 | { |
||
131 | // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key) |
||
132 | // OR was it the "decline" button ? |
||
133 | if ((wParam_loword == IDCANCEL) || (wParam_loword == BUTTON_DECLINE)) |
||
134 | EndDialog (hWnd, 0); // close the dialog box |
||
135 | |||
136 | // else was it the "accept" button ? |
||
137 | else if (wParam_loword == BUTTON_ACCEPT) |
||
138 | EndDialog (hWnd, 1); // close the dialog box and return OK |
||
139 | |||
140 | // else was it the status bar hyperlink ? |
||
141 | else if (wParam_loword == STATICTEXT_CHALLENGE_STATUSBAR) |
||
142 | ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized |
||
143 | } |
||
144 | |||
145 | // call the default dialog message processing function to keep things going |
||
146 | return (false); |
||
147 | } |