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