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_sendseek.cpp |
2 | |||
3 | #include "../common.h" |
||
4 | |||
5 | |||
6 | // dialog template |
||
7 | #define THIS_DIALOG DIALOG_SENDSEEK |
||
8 | |||
9 | |||
10 | // local type definitions |
||
11 | typedef struct sendseek_s |
||
12 | { |
||
13 | int color; // color we want to play |
||
14 | int initial_time; // initial time of the game for each color |
||
15 | int increment; // eventual time increment (Fischer pace) |
||
16 | int rating_from; // opponent rating must be at least |
||
17 | int rating_to; // opponent rating must be at most |
||
18 | bool is_rated; // whether this game is rated or not |
||
19 | bool is_autostart; // whether this game is rated or not |
||
20 | } sendseek_t; |
||
21 | |||
22 | |||
23 | // global variables used in this module only |
||
24 | static sendseek_t sendseek = { COLOR_UNSPECIFIED, 2, 12, 0, 0, false, false }; |
||
25 | static HWND hThisWnd = NULL; |
||
26 | |||
27 | |||
28 | // prototypes of local functions |
||
29 | static void StartThread_ThisDialog (void *thread_parms); |
||
30 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam); |
||
31 | |||
32 | |||
33 | void DialogBox_SendSeek (void) |
||
34 | { |
||
35 | // helper function to fire up the modeless dialog box |
||
36 | |||
140 | pmbaty | 37 | is_dialogbox_displayed = true; |
1 | pmbaty | 38 | _beginthread (StartThread_ThisDialog, 0, NULL); // fire up the thread |
39 | return; // return as soon as the thread is fired up |
||
40 | } |
||
41 | |||
42 | |||
43 | void DialogBox_SendSeek_Validated (void) |
||
44 | { |
||
45 | // callback function called by the main game thread when the dialog box is validated |
||
46 | |||
47 | player_t *network_player; |
||
48 | |||
49 | // remember this callback is no longer to be called |
||
50 | is_dialogbox_sendseek_validated = false; |
||
51 | |||
52 | network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player |
||
53 | if (network_player == NULL) |
||
54 | return; // consistency check |
||
55 | |||
56 | // send the challenge |
||
57 | Player_SendBuffer_Add (network_player, 1000, L"seek %d %d %s %s %s %d-%d\n", |
||
58 | sendseek.initial_time, |
||
59 | sendseek.increment, |
||
60 | (sendseek.is_rated ? L"rated" : L"unrated"), |
||
61 | (sendseek.color == COLOR_BLACK ? L"black" : (sendseek.color == COLOR_WHITE ? L"white" : L"")), |
||
62 | (sendseek.is_autostart ? L"auto" : L"manual"), |
||
63 | sendseek.rating_from, |
||
64 | sendseek.rating_to); |
||
65 | |||
66 | // display a message box to the user |
||
67 | messagebox.hWndParent = hMainWnd; |
||
68 | wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"SendSeek_Title")); |
||
69 | wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"SendSeek_SeekSent")); |
||
70 | messagebox.flags = MB_ICONINFORMATION | MB_OK; |
||
71 | DialogBox_Message (&messagebox); |
||
72 | |||
73 | return; // finished, game request is sent |
||
74 | } |
||
75 | |||
76 | |||
77 | static void StartThread_ThisDialog (void *thread_parms) |
||
78 | { |
||
79 | // this function runs in a separate thread, for that's the only way (seemingly) |
||
80 | // to implement a non-modal message box using the Common Controls library. |
||
81 | |||
82 | if (IsWindow (hThisWnd)) |
||
83 | SendMessage (hThisWnd, WM_CLOSE, 0, 0); // if such a dialog already exists, close it |
||
84 | |||
85 | // display the dialog box |
||
86 | if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1) |
||
87 | is_dialogbox_sendseek_validated = true; // notify main game thread to fire up our callback |
||
140 | pmbaty | 88 | is_dialogbox_displayed = false; |
1 | pmbaty | 89 | |
124 | pmbaty | 90 | the_board.reevaluate = true; // refresh the GUI buttons if needed |
1 | pmbaty | 91 | return; // _endthread() implied |
92 | } |
||
93 | |||
94 | |||
95 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam) |
||
96 | { |
||
97 | // message handler for the dialog box |
||
98 | |||
99 | unsigned short wParam_hiword; |
||
100 | unsigned short wParam_loword; |
||
101 | BOOL was_translated; // needs to be of type BOOL (int) |
||
102 | |||
103 | // filter out the commonly used message values |
||
104 | wParam_hiword = HIWORD (wParam); |
||
105 | wParam_loword = LOWORD (wParam); |
||
106 | |||
107 | // have we just fired up this window ? |
||
108 | if (message == WM_INITDIALOG) |
||
109 | { |
||
110 | hThisWnd = hWnd; // save the dialog window handle |
||
111 | |||
112 | // center the window |
||
113 | CenterWindow (hWnd, hMainWnd); |
||
114 | |||
115 | // set window title and control texts |
||
116 | SetWindowText (hWnd, LOCALIZE (L"SendSeek_Title")); |
||
117 | |||
118 | SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_QUESTION, LOCALIZE (L"SendSeek_Question")); |
||
119 | |||
120 | SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_COLOR, LOCALIZE (L"SendSeek_ColorIWishToPlay")); |
||
121 | ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), L""); |
||
122 | ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), LOCALIZE (L"Games_White")); |
||
123 | ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), LOCALIZE (L"Games_Black")); |
||
124 | ComboBox_SetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), 1 + sendseek.color); |
||
125 | |||
126 | SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_INITIALTIME, LOCALIZE (L"SendSeek_InitialTime")); |
||
127 | SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INITIALTIME, sendseek.initial_time, false); |
||
128 | SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_INCREMENT, LOCALIZE (L"SendSeek_Increment")); |
||
129 | SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INCREMENT, sendseek.increment, false); |
||
130 | SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_RATINGFROM, LOCALIZE (L"SendSeek_RatingFrom")); |
||
131 | if ((sendseek.rating_from > 0) && (sendseek.rating_from < 9999)) |
||
132 | SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGFROM, sendseek.rating_from, false); |
||
133 | SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_RATINGTO, LOCALIZE (L"SendSeek_RatingTo")); |
||
134 | if ((sendseek.rating_to > 0) && (sendseek.rating_to < 9999)) |
||
135 | SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGTO, sendseek.rating_to, false); |
||
136 | SetDlgItemText (hWnd, CHECKBOX_SENDSEEK_RATETHISGAME, LOCALIZE (L"SendSeek_RatedUnrated")); |
||
137 | Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_RATETHISGAME), (sendseek.is_rated ? BST_CHECKED : BST_UNCHECKED)); |
||
138 | SetDlgItemText (hWnd, CHECKBOX_SENDSEEK_AUTOSTART, LOCALIZE (L"SendSeek_AutoStart")); |
||
139 | Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_AUTOSTART), (sendseek.is_autostart ? BST_CHECKED : BST_UNCHECKED)); |
||
140 | |||
141 | SetDlgItemText (hWnd, BUTTON_SENDSEEK, LOCALIZE (L"Button_Send")); |
||
142 | SetDlgItemText (hWnd, BUTTON_CANCEL, LOCALIZE (L"Button_Cancel")); |
||
143 | SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_STATUSBAR, LOCALIZE (L"SendSeek_StatusBar")); |
||
144 | |||
145 | // convert the status bar message to hyperlink |
||
146 | ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_SENDSEEK_STATUSBAR)); |
||
147 | } |
||
148 | |||
149 | // else did we click the close button on the title bar ? |
||
150 | else if (message == WM_CLOSE) |
||
151 | EndDialog (hWnd, 0); // close the dialog box |
||
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 "cancel" button ? |
||
158 | if ((wParam_loword == IDCANCEL) || (wParam_loword == BUTTON_CANCEL)) |
||
159 | EndDialog (hWnd, 0); // close the dialog box |
||
160 | |||
161 | // else was it the "send" button ? |
||
162 | else if (wParam_loword == BUTTON_SENDSEEK) |
||
163 | { |
||
164 | // retrieve the control values |
||
165 | sendseek.color = ComboBox_GetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR)) - 1; |
||
166 | sendseek.initial_time = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INITIALTIME, &was_translated, false); |
||
167 | if (!was_translated || (sendseek.initial_time == 0)) |
||
168 | sendseek.initial_time = 2; // default value |
||
169 | sendseek.increment = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INCREMENT, &was_translated, false); |
||
170 | if (!was_translated) |
||
171 | sendseek.increment = 12; // default value |
||
172 | sendseek.rating_from = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGFROM, &was_translated, false); |
||
173 | if (!was_translated) |
||
174 | sendseek.rating_from = 0; // default value |
||
175 | sendseek.rating_to = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGTO, &was_translated, false); |
||
176 | if (!was_translated || (sendseek.rating_to == 0) || (sendseek.rating_to < sendseek.rating_from)) |
||
177 | sendseek.rating_to = 9999; // default value |
||
178 | sendseek.is_rated = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_RATETHISGAME)) != 0); |
||
179 | sendseek.is_autostart = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_AUTOSTART)) != 0); |
||
180 | |||
181 | EndDialog (hWnd, 1); // close the dialog box and return OK |
||
182 | } |
||
183 | |||
184 | // else was it the status bar hyperlink ? |
||
185 | else if (wParam_loword == STATICTEXT_SENDSEEK_STATUSBAR) |
||
186 | ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized |
||
187 | } |
||
188 | |||
189 | // call the default dialog message processing function to keep things going |
||
190 | return (false); |
||
191 | } |