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