Redesign WinBoard Load Engine dialog
[xboard.git] / winboard / wsettings.c
1 /*\r
2  * woptions.h -- Options dialog box routines for WinBoard\r
3  *\r
4  * Copyright 2003, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free\r
5  * Software Foundation, Inc.\r
6  *\r
7  * ------------------------------------------------------------------------\r
8  *\r
9  * GNU XBoard is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or (at\r
12  * your option) any later version.\r
13  *\r
14  * GNU XBoard is distributed in the hope that it will be useful, but\r
15  * WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
17  * General Public License for more details.\r
18  *\r
19  * You should have received a copy of the GNU General Public License\r
20  * along with this program. If not, see http://www.gnu.org/licenses/.  *\r
21  *\r
22  *------------------------------------------------------------------------\r
23  ** See the file ChangeLog for a revision history.  */\r
24 \r
25 /*\r
26  * Engine-settings dialog. The complexity come from an attempt to present the engine-defined options\r
27  * in a nicey formatted layout. To this end we first run a back-end pre-formatter, which will distribute\r
28  * the controls over two columns (the minimum required, as some are double width). It also takes care of\r
29  * grouping options that start with the same word (mainly for "Polyglot ..." options). It assigns relative\r
30  * suitability to break points between lines, and in the end decides if and where to break up the list\r
31  * for display in multiple (2*N) columns.\r
32  * The thus obtained list representing the topology of the layout is then passed to a front-end routine\r
33  * that generates the actual dialog box from it.\r
34  */\r
35 \r
36 #include "config.h"\r
37 \r
38 #include <windows.h>\r
39 #include <Windowsx.h>\r
40 #include <stdio.h>\r
41 #include <string.h>\r
42 #include "common.h"\r
43 #include "frontend.h"\r
44 #include "backend.h"\r
45 #include "winboard.h"\r
46 #include "backendz.h"\r
47 \r
48 #define _(s) T_(s)\r
49 #define N_(s) s\r
50 \r
51 int layoutList[2*MAX_OPTIONS];\r
52 int checkList[2*MAX_OPTIONS];\r
53 int comboList[2*MAX_OPTIONS];\r
54 int buttonList[2*MAX_OPTIONS];\r
55 int boxList[2*MAX_OPTIONS];\r
56 int groupNameList[2*MAX_OPTIONS];\r
57 int breaks[MAX_OPTIONS];\r
58 int checks, combos, buttons, layout, groups;\r
59 char title[MSG_SIZ];\r
60 char *engineName, *engineDir, *protocolChoice, *engineLine, *nickName, *params;\r
61 Boolean isUCI, hasBook, storeVariant, v1, addToList, useNick, isUCCI;\r
62 extern Option installOptions[], matchOptions[];\r
63 char *engineList[MAXENGINES] = {""}, *engineMnemonic[MAXENGINES] = {""};\r
64 void (*okFunc)();\r
65 ChessProgramState *activeCps;\r
66 Option *activeList;\r
67 int InstallOK P((void));\r
68 typedef int ButtonCallback(HWND h);\r
69 ButtonCallback *comboCallback;\r
70 \r
71 void\r
72 PrintOpt(int i, int right, Option *optionList)\r
73 {\r
74     if(i<0) {\r
75         if(!right) fprintf(debugFP, "%30s", "");\r
76     } else {\r
77         Option opt = optionList[i];\r
78         switch(opt.type) {\r
79             case Slider:\r
80             case Spin:\r
81                 fprintf(debugFP, "%20.20s [    +/-]", opt.name);\r
82                 break;\r
83             case TextBox:\r
84             case FileName:\r
85             case PathName:\r
86                 fprintf(debugFP, "%20.20s [______________________________________]", opt.name);\r
87                 break;\r
88             case Label:\r
89                 fprintf(debugFP, "%41.41s", opt.name);\r
90                 break;\r
91             case CheckBox:\r
92                 fprintf(debugFP, "[x] %-26.25s", opt.name);\r
93                 break;\r
94             case ComboBox:\r
95                 fprintf(debugFP, "%20.20s [ COMBO ]", opt.name);\r
96                 break;\r
97             case Button:\r
98             case SaveButton:\r
99             case ResetButton:\r
100                 fprintf(debugFP, "[ %26.26s ]", opt.name);\r
101             case Message:\r
102             default:\r
103                 break;\r
104         }\r
105     }\r
106     fprintf(debugFP, right ? "\n" : " ");\r
107 }\r
108 \r
109 void\r
110 CreateOptionDialogTest(int *list, int nr, Option *optionList)\r
111 {\r
112     int line;\r
113 \r
114     for(line = 0; line < nr; line+=2) {\r
115         PrintOpt(list[line+1], 0, optionList);\r
116         PrintOpt(list[line], 1, optionList);\r
117     }\r
118 }\r
119 \r
120 void\r
121 LayoutOptions(int firstOption, int endOption, char *groupName, Option *optionList)\r
122 {\r
123     int i, b = strlen(groupName), stop, prefix, right, nextOption, firstButton = buttons;\r
124     Control lastType, nextType=Label;\r
125 \r
126     nextOption = firstOption;\r
127     while(nextOption < endOption) {\r
128         checks = combos = 0; stop = 0;\r
129         lastType = Button; // kludge to make sure leading Spin will not be prefixed\r
130         // first separate out buttons for later treatment, and collect consecutive checks and combos\r
131         while(nextOption < endOption && !stop) {\r
132             switch(nextType = optionList[nextOption].type) {\r
133                 case CheckBox: checkList[checks++] = nextOption; lastType = CheckBox; break;\r
134                 case ComboBox: comboList[combos++] = nextOption; lastType = ComboBox; break;\r
135                 case ResetButton:\r
136                 case SaveButton:\r
137                 case Button:  buttonList[buttons++] = nextOption; lastType = Button; break;\r
138                 case TextBox:\r
139                 case ListBox:\r
140                 case FileName:\r
141                 case PathName:\r
142                 case Slider:\r
143                 case Label:\r
144                 case GroupBox:\r
145                 case Spin: stop++;\r
146                 default:\r
147                 case Message: ; // cannot happen\r
148             }\r
149             nextOption++;\r
150         }\r
151         // We now must be at the end, or looking at a spin or textbox (in nextType)\r
152         if(!stop)\r
153             nextType = Button; // kudge to flush remaining checks and combos undistorted\r
154         // Take a new line if a spin follows combos or checks, or when we encounter a textbox\r
155         if((combos+checks || nextType == TextBox || nextType == ListBox || nextType == FileName || nextType == PathName || nextType == Label) && layout&1) {\r
156             layoutList[layout++] = -1;\r
157         }\r
158         // The last check or combo before a spin will be put on the same line as that spin (prefix)\r
159         // and will thus not be grouped with other checks and combos\r
160         prefix = -1;\r
161         if(nextType == Spin && lastType != Button) {\r
162             if(lastType == CheckBox) prefix = checkList[--checks]; else\r
163             if(lastType == ComboBox) prefix = comboList[--combos];\r
164         }\r
165         // if a combo is followed by a textbox, it must stay at the end of the combo/checks list to appear\r
166         // immediately above the textbox, so treat it as check. (A check would automatically be and remain there.)\r
167         if((nextType == TextBox || nextType == ListBox || nextType == FileName || nextType == PathName) && lastType == ComboBox)\r
168             checkList[checks++] = comboList[--combos];\r
169         // Now append the checks behind the (remaining) combos to treat them as one group\r
170         for(i=0; i< checks; i++)\r
171             comboList[combos++] = checkList[i];\r
172         // emit the consecutive checks and combos in two columns\r
173         right = combos/2; // rounded down if odd!\r
174         for(i=0; i<right; i++) {\r
175             breaks[layout/2] = 2;\r
176             layoutList[layout++] = comboList[i];\r
177             layoutList[layout++] = comboList[i + right];\r
178         }\r
179         // An odd check or combo (which could belong to following textBox) will be put in the left column\r
180         // If there was an even number of checks and combos the last one will automatically be in that position\r
181         if(combos&1) {\r
182             layoutList[layout++] = -1;\r
183             layoutList[layout++] = comboList[2*right];\r
184         }\r
185         if(nextType == ListBox) {\r
186             // A listBox will be left-adjusted, and cause rearrangement of the elements before it to the right column\r
187             breaks[layout/2] = lastType == Button ? 0 : 100;\r
188             layoutList[layout++] = -1;\r
189             layoutList[layout++] = nextOption - 1;\r
190             for(i=optionList[nextOption-1].min; i>0; i--) { // extra high text edit\r
191                 breaks[layout/2] = -1;\r
192                 layoutList[layout++] = -1;\r
193                 layoutList[layout++] = -1;\r
194             }\r
195         } else \r
196         if(nextType == TextBox || nextType == FileName || nextType == PathName || nextType == Label) {\r
197             // A textBox is double width, so must be left-adjusted, and the right column remains empty\r
198             breaks[layout/2] = lastType == Button ? 0 : 100;\r
199             layoutList[layout++] = -1;\r
200             layoutList[layout++] = nextOption - 1;\r
201             if(optionList[nextOption-1].min) { // extra high text edit: goes right of existing listbox\r
202                 layout -= 2; // remove\r
203                 layoutList[layout-2*optionList[nextOption-1].min-2] = nextOption - 1;\r
204             }\r
205         } else if(nextType == Spin) {\r
206             // A spin will go in the next available position (right to left!). If it had to be prefixed with\r
207             // a check or combo, this next position must be to the right, and the prefix goes left to it.\r
208             layoutList[layout++] = nextOption - 1;\r
209             if(prefix >= 0) layoutList[layout++] = prefix;\r
210         }\r
211     }\r
212     // take a new line if needed\r
213     if(layout&1) layoutList[layout++] = -1;\r
214     // emit the buttons belonging in this group; loose buttons are saved for last, to appear at bottom of dialog\r
215     if(b) {\r
216         while(buttons > firstButton)\r
217             layoutList[layout++] = buttonList[--buttons];\r
218         if(layout&1) layoutList[layout++] = -1;\r
219     }\r
220 }\r
221 \r
222 char *\r
223 EndMatch(char *s1, char *s2)\r
224 {\r
225         char *p, *q;\r
226         p = s1; while(*p) p++;\r
227         q = s2; while(*q) q++;\r
228         while(p > s1 && q > s2 && *p == *q) { p--; q--; }\r
229         if(p[1] == 0) return NULL;\r
230         return p+1;\r
231 }\r
232 \r
233 void\r
234 DesignOptionDialog(int nrOpt, Option *optionList)\r
235 {\r
236     int k=0, n=0;\r
237     char buf[MSG_SIZ];\r
238 \r
239     layout = 0;\r
240     buttons = groups = 0;\r
241     while(k < nrOpt) { // k steps through 'solitary' options\r
242         // look if we hit a group of options having names that start with the same word\r
243         int groupSize = 1, groupNameLength = 50;\r
244         sscanf(optionList[k].name, "%s", buf); // get first word of option name\r
245         if(optionList[k].type == GroupBox) groupSize = optionList[k].max, groupNameLength = -strlen(optionList[k].name), groupNameList[groups+1] = k; else\r
246         while(k + groupSize < nrOpt &&\r
247               strstr(optionList[k+groupSize].name, buf) == optionList[k+groupSize].name) {\r
248                 int j;\r
249                 for(j=0; j<groupNameLength; j++) // determine common initial part of option names\r
250                     if( optionList[k].name[j] != optionList[k+groupSize].name[j]) break;\r
251                 groupNameLength = j;\r
252                 groupSize++;\r
253 \r
254         }\r
255         if(groupSize > 3) {\r
256                 // We found a group to terminates the current section\r
257                 LayoutOptions(n, k, "", optionList); // flush all solitary options appearing before the group\r
258                 groupNameList[groups] = groupNameLength;\r
259                 boxList[groups++] = layout; // group start in even entries\r
260                 if(groupNameLength <= 0) n = ++k;\r
261                 LayoutOptions(k, k+groupSize, buf, optionList); // flush the group\r
262                 boxList[groups++] = layout; // group end in odd entries\r
263                 k = n = k + groupSize;\r
264         } else k += groupSize; // small groups are grouped with the solitary options\r
265     }\r
266     if(n != k) LayoutOptions(n, k, "", optionList); // flush remaining solitary options\r
267     // decide if and where we break into two column pairs\r
268 \r
269     // Emit buttons and add OK and cancel\r
270 //    for(k=0; k<buttons; k++) layoutList[layout++] = buttonList[k];\r
271  \r
272    // Create the dialog window\r
273     if(appData.debugMode) CreateOptionDialogTest(layoutList, layout, optionList);\r
274 //    CreateOptionDialog(layoutList, layout, optionList);\r
275     if(!activeCps) okFunc = optionList[nrOpt].target;\r
276 }\r
277 \r
278 #include <windows.h>\r
279 \r
280 extern HINSTANCE hInst;\r
281 \r
282 typedef struct {\r
283     DLGITEMTEMPLATE item;\r
284     WORD code;\r
285     WORD controlType;\r
286     wchar_t d1, data;\r
287     WORD creationData;\r
288 } Item;\r
289 \r
290 struct {\r
291     DLGTEMPLATE header;\r
292     WORD menu;\r
293     WORD winClass;\r
294     wchar_t title[20];\r
295     WORD pointSize;\r
296     wchar_t fontName[14];\r
297     Item control[MAX_OPTIONS];\r
298 } template = {\r
299     { DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_SETFONT, 0, 0, 0, 0, 295, 300 },\r
300     0x0000, 0x0000, L"Engine #1 Settings ", 8, L"MS Sans Serif"\r
301 };\r
302 \r
303 char *\r
304 AddCR(char *s)\r
305 {\r
306     char *p=s, *q;\r
307     int n=0;\r
308     while(p = strchr(p, '\n')) p++, n++; // count linefeeds\r
309     p = q = malloc(strlen(s) + n + 1);\r
310     while(*p++ = *s++) if(p[-1] == '\n') p[-1] = '\r', *p++ = '\n';\r
311     return q;\r
312 }\r
313 \r
314 void\r
315 SetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)\r
316 // Put all current option values in controls, and write option names next to them\r
317 {\r
318     HANDLE hwndCombo;\r
319     int i, k;\r
320     char **choices, *name;\r
321 \r
322     for(i=0; i<layout+buttons; i++) {\r
323         int j=layoutList[i];\r
324         if(j == -2) SetDlgItemText( hDlg, 2000+2*i, ". . ." );\r
325         if(j<0) continue;\r
326         name = cps ? optionList[j].name : _(optionList[j].name);\r
327         if(strstr(name, "Polyglot ") == name) name += 9;\r
328         SetDlgItemText( hDlg, 2000+2*i, name );\r
329 //if(appData.debugMode) fprintf(debugFP, "# %s = %d\n",optionList[j].name, optionList[j].value );\r
330         switch(optionList[j].type) {\r
331             case Spin:\r
332                 SetDlgItemInt( hDlg, 2001+2*i, cps ? optionList[j].value : *(int*)optionList[j].target, TRUE );\r
333                 break;\r
334             case TextBox:\r
335             case FileName:\r
336             case PathName:\r
337                 name = AddCR(cps ? optionList[j].textValue : *(char**)optionList[j].target); // stupid CR...\r
338                 SetDlgItemText( hDlg, 2001+2*i, name);\r
339                 free(name);\r
340                 break;\r
341             case CheckBox:\r
342                 CheckDlgButton( hDlg, 2000+2*i, (cps ? optionList[j].value : *(Boolean*)optionList[j].target) != 0);\r
343                 break;\r
344             case ComboBox:\r
345                 choices = (char**) optionList[j].textValue;\r
346                 hwndCombo = GetDlgItem(hDlg, 2001+2*i);\r
347                 SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);\r
348                 for(k=0; k<optionList[j].max; k++) {\r
349                     SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) choices[k]);\r
350                 }\r
351                 SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) choices[optionList[j].value]);\r
352                 break;\r
353             case ListBox:\r
354                 choices = (char**) optionList[j].choice;\r
355                 hwndCombo = GetDlgItem(hDlg, 2001+2*i);\r
356                 SendMessage(hwndCombo, LB_RESETCONTENT, 0, 0);\r
357                 for(k=0; k<optionList[j].max; k++) {\r
358                     SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) choices[k]);\r
359                 }\r
360                 break;\r
361             case Button:\r
362             case SaveButton:\r
363             default:\r
364                 break;\r
365         }\r
366     }\r
367     SetDlgItemText( hDlg, IDOK, _("OK") );\r
368     SetDlgItemText( hDlg, IDCANCEL, _("Cancel") );\r
369     title[0] &= ~32; // capitalize\r
370     SetWindowText( hDlg, title);\r
371     for(i=0; i<groups; i+=2) {\r
372         int id, p; char buf[MSG_SIZ];\r
373         id = k = boxList[i];\r
374         if(layoutList[k] < 0) k++;\r
375         if(layoutList[k] < 0) continue;\r
376         for(p=0; p<groupNameList[i]; p++) buf[p] = optionList[layoutList[k]].name[p];\r
377         buf[p] = 0;\r
378         if(groupNameList[i] < 0) safeStrCpy(buf, optionList[groupNameList[i+1]].name, MSG_SIZ);\r
379         SetDlgItemText( hDlg, 2000+2*(id+MAX_OPTIONS), buf );\r
380     }\r
381 }\r
382 \r
383 \r
384 int\r
385 GetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)\r
386 // read out all controls, and if value is altered, remember it and send it to the engine\r
387 {\r
388     int i, k, new=0, changed=0, len;\r
389     char **choices, newText[MSG_SIZ], buf[MSG_SIZ], *text;\r
390     BOOL success;\r
391 \r
392     for(i=0; i<layout; i++) {\r
393         int j=layoutList[i];\r
394         if(j<0) continue;\r
395         switch(optionList[j].type) {\r
396             case Spin:\r
397                 new = GetDlgItemInt( hDlg, 2001+2*i, &success, TRUE );\r
398                 if(!success) break;\r
399                 if(new < optionList[j].min) new = optionList[j].min;\r
400                 if(new > optionList[j].max) new = optionList[j].max;\r
401                 if(!cps) { *(int*)optionList[j].target = new; break; }\r
402                 changed = 2*(optionList[j].value != new);\r
403                 optionList[j].value = new;\r
404                 break;\r
405             case TextBox:\r
406             case FileName:\r
407             case PathName:\r
408                 if(cps) len = MSG_SIZ - strlen(optionList[j].name) - 9, text = newText;\r
409                 else    len = GetWindowTextLength(GetDlgItem(hDlg, 2001+2*i)) + 1, text = (char*) malloc(len);\r
410                 success = GetDlgItemText( hDlg, 2001+2*i, text, len );\r
411                 if(!success) text[0] = NULLCHAR; // empty string can be valid input\r
412                 if(!cps) {\r
413                     char *p;\r
414                     p = (optionList[j].type != FileName ? strdup(text) : InterpretFileName(text, homeDir)); // all files relative to homeDir!\r
415                     FREE(*(char**)optionList[j].target); *(char**)optionList[j].target = p;\r
416                     free(text); text = p;\r
417                     while(*p++ = *text++) if(p[-1] == '\r') p--; // crush CR\r
418                     break;\r
419                 }\r
420                 changed = strcmp(optionList[j].textValue, newText) != 0;\r
421                 safeStrCpy(optionList[j].textValue, newText, MSG_SIZ - (optionList[j].textValue - optionList[j].name) );\r
422                 break;\r
423             case CheckBox:\r
424                 new = IsDlgButtonChecked( hDlg, 2000+2*i );\r
425                 if(!cps) { *(Boolean*)optionList[j].target = new; break; }\r
426                 changed = 2*(optionList[j].value != new);\r
427                 optionList[j].value = new;\r
428                 break;\r
429             case ComboBox:\r
430                 choices = (char**) optionList[j].textValue;\r
431                 success = GetDlgItemText( hDlg, 2001+2*i, newText, MSG_SIZ );\r
432                 if(!success) break;\r
433                 new = -1;\r
434                 for(k=0; k<optionList[j].max; k++) {\r
435                     if(choices[k] && !strcmp(choices[k], newText)) new = k;\r
436                 }\r
437                 if(!cps && new >= 0) {\r
438                     if(*(char**)optionList[j].target) free(*(char**)optionList[j].target);\r
439                     *(char**)optionList[j].target = strdup(optionList[j].choice[new]);\r
440                     break;\r
441                 }\r
442                 changed = new >= 0 && (optionList[j].value != new);\r
443                 if(changed) optionList[j].value = new;\r
444                 break;\r
445             case ListBox:\r
446                 if(optionList[j].textValue)\r
447                     *(int*) optionList[j].textValue = SendDlgItemMessage(hDlg, 2001+2*i, LB_GETCURSEL, 0, 0);\r
448             case Button:\r
449             default:\r
450                 break; // are treated instantly, so they have been sent already\r
451         }\r
452         if(changed == 2)\r
453           snprintf(buf, MSG_SIZ, "option %s=%d\n", optionList[j].name, new); else\r
454         if(changed == 1)\r
455           snprintf(buf, MSG_SIZ, "option %s=%s\n", optionList[j].name, newText);\r
456         if(changed) SendToProgram(buf, cps);\r
457     }\r
458     if(!cps && okFunc) return ((ButtonCallback*) okFunc)(0);\r
459     return 1;\r
460 }\r
461 \r
462 char *defaultExt[] = { NULL, "pgn", "fen", "exe", "trn", "bin", "log", "ini" };\r
463 HWND settingsDlg;\r
464 \r
465 LRESULT CALLBACK SettingsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
466 {\r
467     char buf[MSG_SIZ];\r
468     int i, j, ext;\r
469 \r
470     switch( message )\r
471     {\r
472     case WM_INITDIALOG:\r
473 \r
474 //        CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));\r
475         SetOptionValues(hDlg, activeCps, activeList);\r
476         settingsDlg = hDlg;\r
477         SetFocus(GetDlgItem(hDlg, IDCANCEL));\r
478 \r
479         break;\r
480 \r
481     case WM_COMMAND:\r
482         switch( LOWORD(wParam) ) {\r
483         case IDOK:\r
484             if(!GetOptionValues(hDlg, activeCps, activeList)) return FALSE;\r
485             EndDialog( hDlg, 0 );\r
486             comboCallback = NULL; activeCps = NULL; settingsDlg = NULL;\r
487             return TRUE;\r
488 \r
489         case IDCANCEL:\r
490             EndDialog( hDlg, 1 );\r
491             comboCallback = NULL; activeCps = NULL; settingsDlg = NULL;\r
492             return TRUE;\r
493 \r
494         default:\r
495             // program-defined push buttons\r
496             i = LOWORD(wParam);\r
497             if( i>=2000 &&  i < 2000+2*(layout+buttons)) {\r
498                 j = layoutList[(i - 2000)/2];\r
499                 if(j == -2) {\r
500                           char filter[] =\r
501                                 "All files\0*.*\0Game files\0*.pgn;*.gam\0Position files\0*.fen;*.epd;*.pos\0"\r
502                                 "EXE files\0*.exe;*.jar\0Tournament files (*.trn)\0*.trn\0"\r
503                                 "BIN Files\0*.bin\0LOG Files\0*.log\0INI Files\0*.ini\0"\r
504                                 "Image files\0*.bmp\0\0";\r
505                           OPENFILENAME ofn;\r
506 \r
507                           GetDlgItemText( hDlg, i+3, buf, MSG_SIZ );\r
508 \r
509                           ZeroMemory( &ofn, sizeof(ofn) );\r
510 \r
511                           ofn.lStructSize = sizeof(ofn);\r
512                           ofn.hwndOwner = hDlg;\r
513                           ofn.hInstance = hInst;\r
514                           ofn.lpstrFilter = filter;\r
515                           ofn.nFilterIndex      = 1L + (ext = activeCps ? 0 : activeList[layoutList[(i-2000)/2+1]].max & 31);\r
516                           ofn.lpstrDefExt       = defaultExt[ext];\r
517                           ofn.lpstrFile = buf;\r
518                           ofn.nMaxFile = sizeof(buf);\r
519                           ofn.lpstrTitle = _("Choose File");\r
520                           ofn.Flags = OFN_FILEMUSTEXIST | OFN_LONGNAMES | OFN_HIDEREADONLY;\r
521 \r
522                           if( activeList[layoutList[(i-2000)/2+1]].max & 32 ?\r
523                                                        GetOpenFileName( &ofn ) :\r
524                                                        GetSaveFileName( &ofn ) ) {\r
525                               SetDlgItemText( hDlg, i+3, buf );\r
526                           }\r
527                 } else\r
528                 if(j == -3) {\r
529                     GetDlgItemText( hDlg, i+3, buf, MSG_SIZ );\r
530                     if( BrowseForFolder( _("Choose Folder:"), buf ) ) {\r
531                         SetDlgItemText( hDlg, i+3, buf );\r
532                     }\r
533                 }\r
534                 if(j < 0) break;\r
535                 if(comboCallback && activeList[j].type == ComboBox && HIWORD(wParam) == CBN_SELCHANGE) {\r
536                     if(j > 5) break; // Yegh! Must solve problem with more than one combobox in dialog\r
537                     (*comboCallback)(hDlg);\r
538                     break;\r
539                 } else\r
540                 if(activeList[j].type == ListBox && HIWORD(wParam) == /*LBN_SELCHANGE*/ LBN_DBLCLK) {\r
541                     ((ButtonCallback *) activeList[j].target)(hDlg);\r
542                     break;\r
543                 } else\r
544                 if( activeList[j].type  == SaveButton)\r
545                      GetOptionValues(hDlg, activeCps, activeList);\r
546                 else if( activeList[j].type  != Button) break;\r
547                 else if( !activeCps ) { (*(ButtonCallback*) activeList[j].target)(hDlg); break; }\r
548                 if(j == 0) { // WinBoard save button\r
549                     SaveEngineSettings(activeCps == &second);\r
550                     EndDialog( hDlg, 0 );\r
551                     comboCallback = NULL; activeCps = NULL; settingsDlg = NULL;\r
552                     return TRUE;\r
553                 }\r
554                 snprintf(buf, MSG_SIZ, "option %s\n", activeList[j].name);\r
555                 SendToProgram(buf, activeCps);\r
556             }\r
557             break;\r
558         }\r
559 \r
560         break;\r
561     }\r
562 \r
563     return FALSE;\r
564 }\r
565 \r
566 void AddControl(int x, int y, int w, int h, int type, int style, int n)\r
567 {\r
568     int i;\r
569 \r
570     i = template.header.cdit++;\r
571     template.control[i].item.style = style;\r
572     template.control[i].item.dwExtendedStyle = 0;\r
573     template.control[i].item.x = x;\r
574     template.control[i].item.y = y;\r
575     template.control[i].item.cx = w;\r
576     template.control[i].item.cy = h;\r
577     template.control[i].item.id = 2000 + n;\r
578     template.control[i].code = 0xFFFF;\r
579     template.control[i].controlType = type;\r
580     template.control[i].d1 = ' ';\r
581     template.control[i].data = 0;\r
582     template.control[i].creationData = 0;\r
583 }\r
584 \r
585 void AddOption(int x, int y, Control type, int i)\r
586 {\r
587     int extra, num = ES_NUMBER;\r
588 \r
589     switch(type) {\r
590         case Spin+100:\r
591             num = 0; // needs text control for accepting negative numbers\r
592         case Slider:\r
593         case Spin:\r
594             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
595             AddControl(x+95, y, 50, 11, 0x0081, ES_AUTOHSCROLL | num | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
596             break;\r
597         case TextBox:\r
598             extra = 13*activeList[layoutList[i/2]].min; // when extra high, left-align and put description text above it\r
599             AddControl(x+(extra?50:0), y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
600             AddControl(x+(extra?50:95), y+(extra?13:0), extra?105:200, 11+(extra?extra-13:0), 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE |\r
601                                         WS_CHILD | WS_TABSTOP | (extra ? ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL :0), i+1);\r
602             break;\r
603         case ListBox:\r
604             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
605             extra = 13*activeList[layoutList[i/2]].min;\r
606             AddControl(x, y+13, 105, 11+extra-13, 0x0083, LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_HSCROLL | WS_BORDER | LBS_NOTIFY |\r
607                                                                  WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
608             break;\r
609         case Label:\r
610             extra = activeList[layoutList[i/2]].value;\r
611             AddControl(x+extra, y+1, 290-extra, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
612             break;\r
613         case FileName:\r
614         case PathName:\r
615             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
616             AddControl(x+95, y, 180, 11, 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
617             AddControl(x+275, y, 20, 12, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i-2);\r
618             layoutList[i/2-1] = -2 - (type == PathName);\r
619             break;\r
620         case CheckBox:\r
621             AddControl(x, y, 145, 11, 0x0080, BS_AUTOCHECKBOX | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
622             break;\r
623         case ComboBox:\r
624             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
625             AddControl(x+95, y-1, !activeCps && x<10 ? 120 : 50, 500, 0x0085,\r
626                         CBS_AUTOHSCROLL | CBS_DROPDOWN | WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_VSCROLL, i+1);\r
627             break;\r
628         case Button:\r
629         case ResetButton:\r
630         case SaveButton:\r
631             AddControl(x-2, y, 65, 13, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
632         case Message:\r
633         default:\r
634             break;\r
635     }\r
636 \r
637 }\r
638 \r
639 void\r
640 CreateDialogTemplate(int *layoutList, int nr, Option *optionList)\r
641 {\r
642     int i, ii, j, x=1, y=0, maxY=0, buttonRows, breakPoint = 1000, k=0;\r
643 \r
644     template.header.cdit = 0;\r
645     template.header.cx = 307;\r
646     buttonRows = (buttons + 1 + 3)/4; // 4 per row, rounded up\r
647     if(nr > 48) {\r
648         breakPoint = (nr+2*buttonRows+1)/2 & ~1;\r
649         template.header.cx = 625;\r
650     }\r
651 \r
652     for(ii=0; ii<nr; ii++) {\r
653         i = ii^1; if(i == nr) i = ii; // if two on one line, swap order of treatment, to get good (left to right) tabbing order.\r
654         if(k < groups && ii == boxList[k]) {\r
655             int tlen = groupNameList[k]; if(tlen < 0) tlen = -tlen;\r
656             y += 10;\r
657             AddControl(x+2, y+13*(i>>1)-2, 301, 13*(boxList[k+1]-boxList[k]>>1)+8,\r
658                                                 0x0082, WS_VISIBLE | WS_CHILD | SS_BLACKFRAME, 2400);\r
659             AddControl(x+60, y+13*(i>>1)-6, 10*tlen/3, 10,\r
660                                                 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, 2*(ii+MAX_OPTIONS));\r
661         }\r
662         j = layoutList[i];\r
663         if(j >= 0) {\r
664             int neg = (optionList[j].type == Spin && optionList[j].min < 0 ? 100 : 0); // flags spin with negative range\r
665             AddOption(x+155-150*(i&1), y+13*(i>>1)+5, optionList[j].type + neg, 2*i);\r
666             // listboxes have the special power to adjust the width of the column they are in\r
667             if(optionList[j].type == ListBox) x -= optionList[j].value, template.header.cx -= optionList[j].value;\r
668         }\r
669         if(k < groups && ii+1 == boxList[k+1]) {\r
670             k += 2; y += 4;\r
671         }\r
672         if(ii+1 >= breakPoint && breaks[ii+1>>1] >= 0) { x += 318; maxY = y+13*(ii+1>>1)+5; y = -13*(ii+1>>1); breakPoint = 1000; }\r
673     }\r
674     // add butons at the bottom of dialog window\r
675     y += 13*(nr>>1)+5;\r
676 \r
677     for(i=0; i<buttons; i++) {\r
678         AddControl(x+70*(i%4)+5, y+18*(i/4), 65, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, 2*(nr+i));\r
679         layoutList[nr+i] = buttonList[i];\r
680     }\r
681     AddControl(x+225, y+18*(buttonRows-1), 30, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, IDOK-2000);\r
682     AddControl(x+260, y+18*(buttonRows-1), 40, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, IDCANCEL-2000);\r
683     y += 18*buttonRows; if(y < maxY) y = maxY;\r
684     template.title[8] = optionList == first.option ? '1' :  '2';\r
685     template.header.cy = y+2;\r
686     template.header.style &= ~WS_VSCROLL;\r
687 }\r
688 \r
689 void\r
690 EngineOptionsPopup(HWND hwnd, ChessProgramState *cps)\r
691 {\r
692     FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );\r
693 \r
694     activeCps = cps; activeList = cps->option;\r
695     snprintf(title, MSG_SIZ, _("%s Engine Settings (%s)"), T_(cps->which), cps->tidy);\r
696     DesignOptionDialog(cps->nrOptions, cps->option);\r
697     CreateDialogTemplate(layoutList, layout, cps->option);\r
698 \r
699 \r
700     DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );\r
701 \r
702     FreeProcInstance(lpProc);\r
703 \r
704     return;\r
705 }\r
706 \r
707 void\r
708 RefreshSettingsDialog (ChessProgramState *cps, int val)\r
709 {\r
710     int isUp = (settingsDlg != NULL);\r
711     if(val == 1) {\r
712         if(activeCps == cps && isUp) SetOptionValues(settingsDlg, cps, activeList);\r
713         return;\r
714     }\r
715     if(settingsDlg) EndDialog(settingsDlg, 1);\r
716     comboCallback = NULL; activeCps = NULL; settingsDlg = NULL;\r
717     if(val == 3 || isUp) EngineOptionsPopup(hwndMain, cps);\r
718 }\r
719 \r
720 int EnterGroup P((HWND hDlg));\r
721 \r
722 static int engineNr, selected;\r
723 char *protocols[] = { "autodetect", "WB", "UCI", "USI/UCCI", "WB v1", NULL };\r
724 \r
725 int InstallOK()\r
726 {\r
727     if(selected >= 0) { ASSIGN(engineLine, engineList[selected]); }\r
728     if(engineLine[0] == '#') { DisplayError(_("Select single engine from the group"), 0); return 0; }\r
729     if(!strcmp(protocolChoice, protocols[0])) isUCI = 3; else\r
730     if(!strcmp(protocolChoice, protocols[2])) isUCI = 1; else\r
731     if(!strcmp(protocolChoice, protocols[3])) isUCCI = 1; else\r
732     if(!strcmp(protocolChoice, protocols[4])) v1 = 1;\r
733     if(isUCCI) isUCI = 2;\r
734     if(!engineNr) Load(&first, 0); else Load(&second, 1);\r
735     return 1;\r
736 }\r
737 \r
738 Option installOptions[] = {\r
739 //  {   0,  0,    0, NULL, (void*) &engineLine, (char*) engineMnemonic, engineList, ComboBox, N_("Select engine from list:") },\r
740   { 195, 14,    0, NULL, (void*) &EnterGroup, (char*) &selected, engineMnemonic, ListBox, N_("Select engine from list:") },\r
741   {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("or specify one below:") },\r
742   {   0,  0, 32+3, NULL, (void*) &engineName, NULL, NULL, FileName, N_("Engine (.exe or .jar):") },\r
743   {   0,  0,    5, NULL, (void*) &protocolChoice, (char*) protocols, protocols, ComboBox, N_("Engine protocol") },\r
744   {   0,  0,    5, NULL, NULL, NULL, NULL, GroupBox, N_("Optional user preferences") },\r
745   {   0,  0,    0, NULL, (void*) &nickName, NULL, NULL, TextBox, N_("Nickname (optional):") },\r
746   {   0,  0,    0, NULL, (void*) &addToList, NULL, NULL, CheckBox, N_("Add this engine to the list") },\r
747   {   0,  0,    0, NULL, (void*) &hasBook, NULL, NULL, CheckBox, N_("Must not use GUI book") },\r
748   {   0,  0,    0, NULL, (void*) &useNick, NULL, NULL, CheckBox, N_("Use nickname in PGN tag") },\r
749   {   0,  0,    0, NULL, (void*) &storeVariant, NULL, NULL, CheckBox, N_("Force current variant with this engine") },\r
750   {   0,  0,    4, NULL, NULL, NULL, NULL, GroupBox, N_("Advanced (special cases only, as per engine README file)") },\r
751   {   0,  0,    0, NULL, (void*) &params, NULL, NULL, TextBox, N_("command-line parameters:") },\r
752   {   0,  0,    0, NULL, (void*) &wbOptions, NULL, NULL, TextBox, N_("Special WinBoard options:") },\r
753   {   0,  0,    0, NULL, (void*) &engineDir, NULL, NULL, PathName, N_("directory:") },\r
754   {  95,  0,    0, NULL, NULL, NULL, NULL, Label, N_("(Directory will be derived from engine path when left empty)") },\r
755   {   0,  1,    0, NULL, (void*) &InstallOK, "", NULL, EndMark , "" }\r
756 };\r
757 \r
758 void\r
759 GenericPopup(HWND hwnd, Option *optionList)\r
760 {\r
761     FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );\r
762     int n=0;\r
763 \r
764     while(optionList[n].type != EndMark) n++;\r
765     activeCps = NULL; activeList = optionList;\r
766     DesignOptionDialog(n, optionList);\r
767     CreateDialogTemplate(layoutList, layout, optionList);\r
768 \r
769     DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );\r
770 \r
771     FreeProcInstance(lpProc);\r
772 \r
773     return;\r
774 }\r
775 \r
776 int\r
777 EnterGroup(HWND hDlg)\r
778 {\r
779     char buf[MSG_SIZ];\r
780     HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*1);\r
781     int i = SendDlgItemMessage(hDlg, 2001+2*1, LB_GETCURSEL, 0, 0);\r
782     if(i == 0) buf[0] = NULLCHAR; // back to top level\r
783     else if(engineList[i][0] == '#') safeStrCpy(buf, engineList[i], MSG_SIZ); // group header, open group\r
784     else {\r
785         ASSIGN(engineLine, engineList[i]);\r
786         if(isUCCI) isUCI = 2;\r
787         if(!engineNr) Load(&first, 0); else Load(&second, 1);\r
788         EndDialog( hDlg, 0 );\r
789         return 0; // normal line, select engine\r
790     }\r
791     installOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, buf); // replace list by only the group contents\r
792     SendMessage(hwndCombo, LB_RESETCONTENT, 0, 0);\r
793     SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) buf);\r
794     for(i=1; i<installOptions[0].max; i++) {\r
795             SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
796     }\r
797 //    SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) 0, (LPARAM) buf);\r
798     return 0;\r
799 }\r
800 \r
801 void LoadEnginePopUp(HWND hwnd, int nr)\r
802 {\r
803     isUCI = isUCCI = storeVariant = v1 = useNick = FALSE; addToList = hasBook = TRUE; // defaults\r
804     engineNr = nr;\r
805     if(engineDir)    free(engineDir);    engineDir = strdup("");\r
806     if(params)       free(params);       params = strdup("");\r
807     if(nickName)     free(nickName);     nickName = strdup("");\r
808     if(engineLine)   free(engineLine);   engineLine = strdup("");\r
809     if(engineName)   free(engineName);   engineName = strdup("");\r
810     ASSIGN(wbOptions, "");\r
811     installOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, ""); // only top level\r
812     snprintf(title, MSG_SIZ, _("Load %s Engine"), nr ? _("second") : _("first"));\r
813 \r
814     GenericPopup(hwnd, installOptions);\r
815 }\r
816 \r
817 int PickTheme P((HWND hDlg));\r
818 void DeleteTheme P((HWND hDlg));\r
819 \r
820 int ThemeOK()\r
821 {\r
822     if(selected >= 0) { ASSIGN(engineLine, engineList[selected]); }\r
823     if(engineLine[0] == '#') { DisplayError(_("Select single theme from the group"), 0); return 0; }\r
824     LoadTheme();\r
825     return 1;\r
826 }\r
827 \r
828 Option themeOptions[] = {\r
829   { 195, 14,    0, NULL, (void*) &PickTheme, (char*) &selected, engineMnemonic, ListBox, N_("Select theme from list:") },\r
830   {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("or specify new theme below:") },\r
831   {   0,  0,    0, NULL, (void*) &nickName, NULL, NULL, TextBox, N_("Theme name:") },\r
832   {   0,  0,    0, NULL, (void*) &appData.useBitmaps, NULL, NULL, CheckBox, N_("Use board textures") },\r
833   {   0,  0, 32+0, NULL, (void*) &appData.liteBackTextureFile, NULL, NULL, FileName, N_("Light-square texture:") },\r
834   {   0,  0, 32+0, NULL, (void*) &appData.darkBackTextureFile, NULL, NULL, FileName, N_("Dark-square texture:") },\r
835   {   0,  0,    3, NULL, (void*) &appData.darkBackTextureMode, "", NULL, Spin, N_("Dark reorientation mode:") },\r
836   {   0,  0,    3, NULL, (void*) &appData.liteBackTextureMode, "", NULL, Spin, N_("Light reorientation mode:") },\r
837   {   0,  0,    0, NULL, (void*) &appData.useBorder, NULL, NULL, CheckBox, N_("Draw border around board") },\r
838   {   0,  0, 32+0, NULL, (void*) &appData.border, NULL, NULL, FileName, N_("Optional border bitmap:") },\r
839   {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("        Beware: a specified piece font will prevail over piece bitmaps") },\r
840   {   0,  0,    0, NULL, (void*) &appData.pieceDirectory, NULL, NULL, PathName, N_("Directory with piece bitmaps:") },\r
841   {   0,  0,    0, NULL, (void*) &appData.useFont, NULL, NULL, CheckBox, N_("Use piece font") },\r
842   {   0, 50,  150, NULL, (void*) &appData.fontPieceSize, "", NULL, Spin, N_("Font size (%):") },\r
843   {   0,  0,    0, NULL, (void*) &appData.renderPiecesWithFont, NULL, NULL, TextBox, N_("Font name:") },\r
844   {   0,  0,    0, NULL, (void*) &appData.fontToPieceTable, NULL, NULL, TextBox, N_("Font piece to char:") },\r
845 //  {   0,  0,    0, NULL, (void*) &DeleteTheme, NULL, NULL, Button, N_("Up") },\r
846 //  {   0,  0,    0, NULL, (void*) &DeleteTheme, NULL, NULL, Button, N_("Down") },\r
847   {   0,  0,    0, NULL, (void*) &DeleteTheme, NULL, NULL, Button, N_("Delete Theme") },\r
848   {   0,  1,    0, NULL, (void*) &ThemeOK, "", NULL, EndMark , "" }\r
849 };\r
850 \r
851 void\r
852 DeleteTheme (HWND hDlg)\r
853 {\r
854     char *p, *q;\r
855     int i, selected = SendDlgItemMessage(hDlg, 2001+2*1, LB_GETCURSEL, 0, 0);\r
856     HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*1);\r
857     if(selected < 0) return;\r
858     if(p = strstr(appData.themeNames, engineList[selected])) {\r
859         if(q = strchr(p, '\n')) strcpy(p, q+1);\r
860     }\r
861     themeOptions[0].max = NamesToList(appData.themeNames, engineList, engineMnemonic, ""); // replace list by only the group contents\r
862     SendMessage(hwndCombo, LB_RESETCONTENT, 0, 0);\r
863     SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) "");\r
864     for(i=1; i<themeOptions[0].max; i++) {\r
865             SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
866     }\r
867 }\r
868 \r
869 int\r
870 PickTheme (HWND hDlg)\r
871 {\r
872     char buf[MSG_SIZ];\r
873     HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*1);\r
874     int i = SendDlgItemMessage(hDlg, 2001+2*1, LB_GETCURSEL, 0, 0);\r
875     if(i == 0) buf[0] = NULLCHAR; // back to top level\r
876     else if(engineList[i][0] == '#') safeStrCpy(buf, engineList[i], MSG_SIZ); // group header, open group\r
877     else {\r
878         ASSIGN(engineLine, engineList[i]);\r
879         LoadTheme();\r
880         EndDialog( hDlg, 0 );\r
881         return 0; // normal line, select engine\r
882     }\r
883     themeOptions[0].max = NamesToList(appData.themeNames, engineList, engineMnemonic, buf); // replace list by only the group contents\r
884     SendMessage(hwndCombo, LB_RESETCONTENT, 0, 0);\r
885     SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) buf);\r
886     for(i=1; i<themeOptions[0].max; i++) {\r
887             SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
888     }\r
889     return 0;\r
890 }\r
891 \r
892 void ThemeOptionsPopup(HWND hwnd)\r
893 {\r
894     addToList = TRUE; // defaults\r
895     if(nickName)     free(nickName);     nickName = strdup("");\r
896     if(engineLine)   free(engineLine);   engineLine = strdup("");\r
897     themeOptions[0].max = NamesToList(appData.themeNames, engineList, engineMnemonic, ""); // only top level\r
898     snprintf(title, MSG_SIZ, _("Board themes"));\r
899 \r
900     GenericPopup(hwnd, themeOptions);\r
901 }\r
902 \r
903 Boolean autoinc, twice, swiss;\r
904 char *tfName;\r
905 \r
906 int MatchOK()\r
907 {\r
908     if(autoinc) appData.loadGameIndex = appData.loadPositionIndex = -(twice + 1); else\r
909     if(!appData.loadGameFile[0]) appData.loadGameIndex = -2*twice; // kludge to pass value of "twice" for use in GUI book\r
910     if(swiss) { appData.defaultMatchGames = 1; appData.tourneyType = -1; }\r
911     if(CreateTourney(tfName) && !matchMode) { // CreateTourney reloads original settings if file already existed\r
912         MatchEvent(2);\r
913         return 1; // close dialog\r
914     }\r
915     return matchMode || !appData.participants[0]; // if we failed to create and are not in playing, forbid popdown if there are participants\r
916 }\r
917 \r
918 void PseudoOK(HWND hDlg)\r
919 {\r
920     if(matchMode) return;\r
921     okFunc = 0;\r
922     GetOptionValues(hDlg, activeCps, activeList);\r
923     EndDialog( hDlg, 0 );\r
924     comboCallback = NULL; activeCps = NULL;\r
925 \r
926     if(autoinc) appData.loadGameIndex = appData.loadPositionIndex = -(twice + 1); else\r
927     if(!appData.loadGameFile[0]) appData.loadGameIndex = -2*twice; // kludge to pass value of "twice" for use in GUI book\r
928     if(!autoinc && !twice) { // prevent auto-inc being remembered in index value if checkboxes not ticked\r
929         if(appData.loadGameIndex < 0) appData.loadGameIndex = 0;\r
930         if(appData.loadPositionIndex < 0) appData.loadPositionIndex = 0;\r
931     }\r
932     if(swiss) { appData.defaultMatchGames = 1; appData.tourneyType = -1; }\r
933     ASSIGN(appData.tourneyFile, tfName);\r
934 }\r
935 \r
936 char *GetParticipants(HWND hDlg)\r
937 {\r
938     int len = GetWindowTextLength(GetDlgItem(hDlg, 2001+2*0)) + 1;\r
939     char *participants,*p, *q;\r
940     if(len < 4) return NULL; // box is empty (enough)\r
941     participants = (char*) malloc(len);\r
942     GetDlgItemText(hDlg, 2001+2*0, participants, len );\r
943     p = q = participants;\r
944     while(*p++ = *q++) if(p[-1] == '\r') p--;\r
945     return participants;\r
946 }\r
947 \r
948 void ReplaceParticipant(HWND hDlg)\r
949 {\r
950     char *participants = GetParticipants(hDlg);\r
951     Substitute(participants, TRUE);\r
952 }\r
953         \r
954 void UpgradeParticipant(HWND hDlg)\r
955 {\r
956     char *participants = GetParticipants(hDlg);\r
957     Substitute(participants, FALSE);\r
958 }\r
959 \r
960 void Inspect(HWND hDlg)\r
961 {\r
962     FILE *f;\r
963     char name[MSG_SIZ];\r
964     GetDlgItemText(hDlg, 2001+2*33, name, MSG_SIZ );\r
965     if(name[0] && (f = fopen(name, "r")) ) {\r
966         char *saveSaveFile;\r
967         saveSaveFile = appData.saveGameFile; appData.saveGameFile = NULL; // this is a persistent option, protect from change\r
968         ParseArgsFromFile(f);\r
969         autoinc = ((appData.loadPositionFile[0] ? appData.loadGameIndex : appData.loadPositionIndex) < 0);\r
970         twice = ((appData.loadPositionFile[0] ? appData.loadGameIndex : appData.loadPositionIndex) == -2);\r
971         swiss = appData.tourneyType < 0;\r
972         SetOptionValues(hDlg, NULL, activeList);\r
973         FREE(appData.saveGameFile); appData.saveGameFile = saveSaveFile;\r
974     } else DisplayError(_("First you must specify an existing tourney file to clone"), 0);\r
975 }\r
976 \r
977 void TimeControlOptionsPopup P((HWND hDlg));\r
978 void UciOptionsPopup P((HWND hDlg));\r
979 int AddToTourney P((HWND hDlg));\r
980 \r
981 Option tourneyOptions[] = {\r
982   { 80,  15,        0, NULL, (void*) &AddToTourney, NULL, engineMnemonic, ListBox, N_("Select Engine:") },\r
983   { 0xD, 15,        0, NULL, (void*) &appData.participants, "", NULL, TextBox, N_("Tourney participants:") },\r
984   { 0,  0,          4, NULL, (void*) &tfName, "", NULL, FileName, N_("Tournament file:") },\r
985   { 30, 0,          0, NULL, NULL, NULL, NULL, Label, N_("If you specify an existing file, the rest of this dialog will be ignored.") },\r
986   { 30, 0,          0, NULL, NULL, NULL, NULL, Label, N_("Otherwise, the file will be created, with the settings you specify below:") },\r
987   { 0,  0,          0, NULL, (void*) &swiss, "", NULL, CheckBox, N_("Use Swiss pairing engine (cycles = rounds)") },\r
988   { 0,  0,         10, NULL, (void*) &appData.tourneyType, "", NULL, Spin, N_("Tourney type (0=RR, 1=gauntlet):") },\r
989   { 0,  0,          0, NULL, (void*) &appData.cycleSync, "", NULL, CheckBox, N_("Sync after cycle") },\r
990   { 0,  1, 1000000000, NULL, (void*) &appData.tourneyCycles, "", NULL, Spin, N_("Number of tourney cycles:") },\r
991   { 0,  0,          0, NULL, (void*) &appData.roundSync, "", NULL, CheckBox, N_("Sync after round") },\r
992   { 0,  1, 1000000000, NULL, (void*) &appData.defaultMatchGames, "", NULL, Spin, N_("Games per Match / Pairing:") },\r
993   { 0,  0,          1, NULL, (void*) &appData.saveGameFile, "", NULL, FileName, N_("File for saving tourney games:") },\r
994   { 0,  0,       32+1, NULL, (void*) &appData.loadGameFile, "", NULL, FileName, N_("Game File with Opening Lines:") },\r
995   { 0, -2, 1000000000, NULL, (void*) &appData.loadGameIndex, "", NULL, Spin, N_("Game Number:") },\r
996   { 0,  0,       32+2, NULL, (void*) &appData.loadPositionFile, "", NULL, FileName, N_("File with Start Positions:") },\r
997   { 0, -2, 1000000000, NULL, (void*) &appData.loadPositionIndex, "", NULL, Spin, N_("Position Number:") },\r
998   { 0,  0,          0, NULL, (void*) &autoinc, "", NULL, CheckBox, N_("Step through lines/positions in file") },\r
999   { 0,  0, 1000000000, NULL, (void*) &appData.rewindIndex, "", NULL, Spin, N_("Rewind after (0 = never):") },\r
1000   { 0,  0,          0, NULL, (void*) &twice, "", NULL, CheckBox, N_("Use each line/position twice") },\r
1001   { 0,  0,          0, NULL, (void*) &appData.defNoBook, "", NULL, CheckBox, N_("Make all use GUI book by default") },\r
1002   { 0,  0, 1000000000, NULL, (void*) &appData.matchPause, "", NULL, Spin, N_("Pause between Games (ms):") },\r
1003   { 0,  0,          0, NULL, (void*) &ReplaceParticipant, "", NULL, Button, N_("Replace Engine") },\r
1004   { 0,  0,          0, NULL, (void*) &UpgradeParticipant, "", NULL, Button, N_("Upgrade Engine") },\r
1005   { 0,  0,          0, NULL, (void*) &TimeControlOptionsPopup, "", NULL, Button, N_("Time Control...") },\r
1006   { 0,  0,          0, NULL, (void*) &UciOptionsPopup, "", NULL, Button, N_("Common Engine...") },\r
1007   { 0,  0,          0, NULL, (void*) &Inspect, "", NULL, Button, N_("Clone Tourney") },\r
1008   { 0,  0,          0, NULL, (void*) &PseudoOK, "", NULL, Button, N_("Continue Later") },\r
1009   { 0, 0, 0, NULL, (void*) &MatchOK, "", NULL, EndMark , "" }\r
1010 };\r
1011 \r
1012 int AddToTourney(HWND hDlg)\r
1013 {\r
1014   char buf[MSG_SIZ];\r
1015   HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*1);\r
1016   int i = SendDlgItemMessage(hDlg, 2001+2*1, LB_GETCURSEL, 0, 0);\r
1017   if(i<0) return 0;\r
1018   if(i == 0) buf[0] = NULLCHAR; // back to top level\r
1019   else if(engineList[i][0] == '#') safeStrCpy(buf, engineList[i], MSG_SIZ); // group header, open group\r
1020   else { // normal line, select engine\r
1021     snprintf(buf, MSG_SIZ, "%s\r\n", engineMnemonic[i]);\r
1022     SendMessage( GetDlgItem(hDlg, 2001+2*0), EM_SETSEL, 99999, 99999 );\r
1023     SendMessage( GetDlgItem(hDlg, 2001+2*0), EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) buf );\r
1024     return 0;\r
1025   }\r
1026   tourneyOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, buf); // replace list by only the group contents\r
1027   SendMessage(hwndCombo, LB_RESETCONTENT, 0, 0);\r
1028   SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) buf);\r
1029   for(i=1; i<tourneyOptions[0].max; i++) {\r
1030     SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
1031   }\r
1032 //  SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) 0, (LPARAM) buf);\r
1033   return 0;\r
1034 }\r
1035 \r
1036 void TourneyPopup(HWND hwnd)\r
1037 {\r
1038     int n = NamesToList(firstChessProgramNames, engineList, engineMnemonic, "");\r
1039     autoinc = appData.loadGameIndex < 0 || appData.loadPositionIndex < 0;\r
1040     twice = appData.loadGameIndex == -2 || appData.loadPositionIndex == -2; swiss = appData.tourneyType < 0;\r
1041     tourneyOptions[0].max = n;\r
1042     snprintf(title, MSG_SIZ, _("Tournament and Match Options"));\r
1043     ASSIGN(tfName, appData.tourneyFile[0] ? appData.tourneyFile : MakeName(appData.defName));\r
1044 \r
1045     GenericPopup(hwnd, tourneyOptions);\r
1046 }\r