521ce78ca993a9ce6a0c7a382bdd2c7bdd6a138e
[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                 if(j == 0) { // WinBoard save button\r
545                     SaveEngineSettings(activeCps == &second);\r
546                     EndDialog( hDlg, 0 );\r
547                     comboCallback = NULL; activeCps = NULL; settingsDlg = NULL;\r
548                     return TRUE;\r
549                 }\r
550                 snprintf(buf, MSG_SIZ, "option %s\n", activeList[j].name);\r
551                 SendToProgram(buf, activeCps);\r
552             }\r
553             break;\r
554         }\r
555 \r
556         break;\r
557     }\r
558 \r
559     return FALSE;\r
560 }\r
561 \r
562 void AddControl(int x, int y, int w, int h, int type, int style, int n)\r
563 {\r
564     int i;\r
565 \r
566     i = template.header.cdit++;\r
567     template.control[i].item.style = style;\r
568     template.control[i].item.dwExtendedStyle = 0;\r
569     template.control[i].item.x = x;\r
570     template.control[i].item.y = y;\r
571     template.control[i].item.cx = w;\r
572     template.control[i].item.cy = h;\r
573     template.control[i].item.id = 2000 + n;\r
574     template.control[i].code = 0xFFFF;\r
575     template.control[i].controlType = type;\r
576     template.control[i].d1 = ' ';\r
577     template.control[i].data = 0;\r
578     template.control[i].creationData = 0;\r
579 }\r
580 \r
581 void AddOption(int x, int y, Control type, int i)\r
582 {\r
583     int extra, num = ES_NUMBER;\r
584 \r
585     switch(type) {\r
586         case Spin+100:\r
587             num = 0; // needs text control for accepting negative numbers\r
588         case Slider:\r
589         case Spin:\r
590             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
591             AddControl(x+95, y, 50, 11, 0x0081, ES_AUTOHSCROLL | num | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
592             break;\r
593         case TextBox:\r
594             extra = 13*activeList[layoutList[i/2]].min; // when extra high, left-align and put description text above it\r
595             AddControl(x+(extra?50:0), y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
596             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
597                                         WS_CHILD | WS_TABSTOP | (extra ? ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL :0), i+1);\r
598             break;\r
599         case ListBox:\r
600             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
601             extra = 13*activeList[layoutList[i/2]].min;\r
602             AddControl(x, y+13, 105, 11+extra-13, 0x0083, LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_HSCROLL | WS_BORDER | LBS_NOTIFY |\r
603                                                                  WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
604             break;\r
605         case Label:\r
606             extra = activeList[layoutList[i/2]].value;\r
607             AddControl(x+extra, y+1, 290-extra, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
608             break;\r
609         case FileName:\r
610         case PathName:\r
611             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
612             AddControl(x+95, y, 180, 11, 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
613             AddControl(x+275, y, 20, 12, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i-2);\r
614             layoutList[i/2-1] = -2 - (type == PathName);\r
615             break;\r
616         case CheckBox:\r
617             AddControl(x, y, 145, 11, 0x0080, BS_AUTOCHECKBOX | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
618             break;\r
619         case ComboBox:\r
620             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
621             AddControl(x+95, y-1, !activeCps && x<10 ? 120 : 50, 500, 0x0085,\r
622                         CBS_AUTOHSCROLL | CBS_DROPDOWN | WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_VSCROLL, i+1);\r
623             break;\r
624         case Button:\r
625         case ResetButton:\r
626         case SaveButton:\r
627             AddControl(x-2, y, 65, 13, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
628         case Message:\r
629         default:\r
630             break;\r
631     }\r
632 \r
633 }\r
634 \r
635 void\r
636 CreateDialogTemplate(int *layoutList, int nr, Option *optionList)\r
637 {\r
638     int i, ii, j, x=1, y=0, maxY=0, buttonRows, breakPoint = 1000, k=0;\r
639 \r
640     template.header.cdit = 0;\r
641     template.header.cx = 307;\r
642     buttonRows = (buttons + 1 + 3)/4; // 4 per row, rounded up\r
643     if(nr > 50) {\r
644         breakPoint = (nr+2*buttonRows+1)/2 & ~1;\r
645         template.header.cx = 625;\r
646     }\r
647 \r
648     for(ii=0; ii<nr; ii++) {\r
649         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
650         if(k < groups && ii == boxList[k]) {\r
651             y += 10;\r
652             AddControl(x+2, y+13*(i>>1)-2, 301, 13*(boxList[k+1]-boxList[k]>>1)+8,\r
653                                                 0x0082, WS_VISIBLE | WS_CHILD | SS_BLACKFRAME, 2400);\r
654             AddControl(x+60, y+13*(i>>1)-6, 10*groupNameList[k]/3, 10,\r
655                                                 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, 2*(ii+MAX_OPTIONS));\r
656         }\r
657         j = layoutList[i];\r
658         if(j >= 0) {\r
659             int neg = (optionList[j].type == Spin && optionList[j].min < 0 ? 100 : 0); // flags spin with negative range\r
660             AddOption(x+155-150*(i&1), y+13*(i>>1)+5, optionList[j].type + neg, 2*i);\r
661             // listboxes have the special power to adjust the width of the column they are in\r
662             if(optionList[j].type == ListBox) x -= optionList[j].value, template.header.cx -= optionList[j].value;\r
663         }\r
664         if(k < groups && ii+1 == boxList[k+1]) {\r
665             k += 2; y += 4;\r
666         }\r
667         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
668     }\r
669     // add butons at the bottom of dialog window\r
670     y += 13*(nr>>1)+5;\r
671 \r
672     for(i=0; i<buttons; i++) {\r
673         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
674         layoutList[nr+i] = buttonList[i];\r
675     }\r
676     AddControl(x+225, y+18*(buttonRows-1), 30, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, IDOK-2000);\r
677     AddControl(x+260, y+18*(buttonRows-1), 40, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, IDCANCEL-2000);\r
678     y += 18*buttonRows; if(y < maxY) y = maxY;\r
679     template.title[8] = optionList == first.option ? '1' :  '2';\r
680     template.header.cy = y+2;\r
681     template.header.style &= ~WS_VSCROLL;\r
682 }\r
683 \r
684 void\r
685 EngineOptionsPopup(HWND hwnd, ChessProgramState *cps)\r
686 {\r
687     FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );\r
688 \r
689     activeCps = cps; activeList = cps->option;\r
690     snprintf(title, MSG_SIZ, _("%s Engine Settings (%s)"), T_(cps->which), cps->tidy);\r
691     DesignOptionDialog(cps->nrOptions, cps->option);\r
692     CreateDialogTemplate(layoutList, layout, cps->option);\r
693 \r
694 \r
695     DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );\r
696 \r
697     FreeProcInstance(lpProc);\r
698 \r
699     return;\r
700 }\r
701 \r
702 void\r
703 RefreshSettingsDialog (ChessProgramState *cps, int val)\r
704 {\r
705     int isUp = (settingsDlg != NULL);\r
706     if(val == 1) {\r
707         if(activeCps == cps && isUp) SetOptionValues(settingsDlg, cps, activeList);\r
708         return;\r
709     }\r
710     if(settingsDlg) EndDialog(settingsDlg, 1);\r
711     comboCallback = NULL; activeCps = NULL; settingsDlg = NULL;\r
712     if(val == 3 || isUp) EngineOptionsPopup(hwndMain, cps);\r
713 }\r
714 \r
715 int EnterGroup P((HWND hDlg));\r
716 \r
717 static int engineNr, selected;\r
718 \r
719 int InstallOK()\r
720 {\r
721     if(selected >= 0) { ASSIGN(engineLine, engineList[selected]); }\r
722     if(engineLine[0] == '#') { DisplayError(_("Select single engine from the group"), 0); return 0; }\r
723     if(isUCCI) isUCI = 2;\r
724     if(!engineNr) Load(&first, 0); else Load(&second, 1);\r
725     return 1;\r
726 }\r
727 \r
728 Option installOptions[] = {\r
729 //  {   0,  0,    0, NULL, (void*) &engineLine, (char*) engineMnemonic, engineList, ComboBox, N_("Select engine from list:") },\r
730   { 195, 14,    0, NULL, (void*) &EnterGroup, (char*) &selected, engineMnemonic, ListBox, N_("Select engine from list:") },\r
731   {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("or specify one below:") },\r
732   {   0,  0,    0, NULL, (void*) &nickName, NULL, NULL, TextBox, N_("Nickname (optional):") },\r
733   {   0,  0,    0, NULL, (void*) &useNick, NULL, NULL, CheckBox, N_("Use nickname in PGN tag") },\r
734   {   0,  0, 32+3, NULL, (void*) &engineName, NULL, NULL, FileName, N_("Engine (.exe or .jar):") },\r
735   {   0,  0,    0, NULL, (void*) &params, NULL, NULL, TextBox, N_("command-line parameters:") },\r
736   {   0,  0,    0, NULL, (void*) &wbOptions, NULL, NULL, TextBox, N_("Special WinBoard options:") },\r
737   {   0,  0,    0, NULL, (void*) &engineDir, NULL, NULL, PathName, N_("directory:") },\r
738   {  95,  0,    0, NULL, NULL, NULL, NULL, Label, N_("(Directory will be derived from engine path when left empty)") },\r
739   {   0,  0,    0, NULL, (void*) &addToList, NULL, NULL, CheckBox, N_("Add this engine to the list") },\r
740   {   0,  0,    0, NULL, (void*) &hasBook, NULL, NULL, CheckBox, N_("Must not use GUI book") },\r
741   {   0,  0,    0, NULL, (void*) &storeVariant, NULL, NULL, CheckBox, N_("Force current variant with this engine") },\r
742   {   0,  0,    0, NULL, (void*) &isUCI, NULL, NULL, CheckBox, N_("UCI") },\r
743   {   0,  0,    0, NULL, (void*) &v1, NULL, NULL, CheckBox, N_("WB protocol v1 (skip waiting for features)") },\r
744   {   0,  0,    0, NULL, (void*) &isUCCI, NULL, NULL, CheckBox, N_("UCCI/USI/Arena960 (through /uxiAdapter)") },\r
745   {   0,  1,    0, NULL, (void*) &InstallOK, "", NULL, EndMark , "" }\r
746 };\r
747 \r
748 void\r
749 GenericPopup(HWND hwnd, Option *optionList)\r
750 {\r
751     FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );\r
752     int n=0;\r
753 \r
754     while(optionList[n].type != EndMark) n++;\r
755     activeCps = NULL; activeList = optionList;\r
756     DesignOptionDialog(n, optionList);\r
757     CreateDialogTemplate(layoutList, layout, optionList);\r
758 \r
759     DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );\r
760 \r
761     FreeProcInstance(lpProc);\r
762 \r
763     return;\r
764 }\r
765 \r
766 int\r
767 EnterGroup(HWND hDlg)\r
768 {\r
769     char buf[MSG_SIZ];\r
770     HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*1);\r
771     int i = SendDlgItemMessage(hDlg, 2001+2*1, LB_GETCURSEL, 0, 0);\r
772     if(i == 0) buf[0] = NULLCHAR; // back to top level\r
773     else if(engineList[i][0] == '#') safeStrCpy(buf, engineList[i], MSG_SIZ); // group header, open group\r
774     else {\r
775         ASSIGN(engineLine, engineList[i]);\r
776         if(isUCCI) isUCI = 2;\r
777         if(!engineNr) Load(&first, 0); else Load(&second, 1);\r
778         EndDialog( hDlg, 0 );\r
779         return 0; // normal line, select engine\r
780     }\r
781     installOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, buf); // replace list by only the group contents\r
782     SendMessage(hwndCombo, LB_RESETCONTENT, 0, 0);\r
783     SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) buf);\r
784     for(i=1; i<installOptions[0].max; i++) {\r
785             SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
786     }\r
787 //    SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) 0, (LPARAM) buf);\r
788     return 0;\r
789 }\r
790 \r
791 void LoadEnginePopUp(HWND hwnd, int nr)\r
792 {\r
793     isUCI = isUCCI = storeVariant = v1 = useNick = FALSE; addToList = hasBook = TRUE; // defaults\r
794     engineNr = nr;\r
795     if(engineDir)    free(engineDir);    engineDir = strdup("");\r
796     if(params)       free(params);       params = strdup("");\r
797     if(nickName)     free(nickName);     nickName = strdup("");\r
798     if(engineLine)   free(engineLine);   engineLine = strdup("");\r
799     if(engineName)   free(engineName);   engineName = strdup("");\r
800     ASSIGN(wbOptions, "");\r
801     installOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, ""); // only top level\r
802     snprintf(title, MSG_SIZ, _("Load %s Engine"), nr ? _("second") : _("first"));\r
803 \r
804     GenericPopup(hwnd, installOptions);\r
805 }\r
806 \r
807 int PickTheme P((HWND hDlg));\r
808 void DeleteTheme P((HWND hDlg));\r
809 \r
810 int ThemeOK()\r
811 {\r
812     if(selected >= 0) { ASSIGN(engineLine, engineList[selected]); }\r
813     if(engineLine[0] == '#') { DisplayError(_("Select single theme from the group"), 0); return 0; }\r
814     LoadTheme();\r
815     return 1;\r
816 }\r
817 \r
818 Option themeOptions[] = {\r
819   { 195, 14,    0, NULL, (void*) &PickTheme, (char*) &selected, engineMnemonic, ListBox, N_("Select theme from list:") },\r
820   {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("or specify new theme below:") },\r
821   {   0,  0,    0, NULL, (void*) &nickName, NULL, NULL, TextBox, N_("Theme name:") },\r
822   {   0,  0,    0, NULL, (void*) &appData.useBitmaps, NULL, NULL, CheckBox, N_("Use board textures") },\r
823   {   0,  0, 32+0, NULL, (void*) &appData.liteBackTextureFile, NULL, NULL, FileName, N_("Light-square texture:") },\r
824   {   0,  0, 32+0, NULL, (void*) &appData.darkBackTextureFile, NULL, NULL, FileName, N_("Dark-square texture:") },\r
825   {   0,  0,    3, NULL, (void*) &appData.darkBackTextureMode, "", NULL, Spin, N_("Dark reorientation mode:") },\r
826   {   0,  0,    3, NULL, (void*) &appData.liteBackTextureMode, "", NULL, Spin, N_("Light reorientation mode:") },\r
827   {   0,  0,    0, NULL, (void*) &appData.useBorder, NULL, NULL, CheckBox, N_("Draw border around board") },\r
828   {   0,  0, 32+0, NULL, (void*) &appData.border, NULL, NULL, FileName, N_("Optional border bitmap:") },\r
829   {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("        Beware: a specified piece font will prevail over piece bitmaps") },\r
830   {   0,  0,    0, NULL, (void*) &appData.pieceDirectory, NULL, NULL, PathName, N_("Directory with piece bitmaps:") },\r
831   {   0,  0,    0, NULL, (void*) &appData.useFont, NULL, NULL, CheckBox, N_("Use piece font") },\r
832   {   0, 50,  150, NULL, (void*) &appData.fontPieceSize, "", NULL, Spin, N_("Font size (%):") },\r
833   {   0,  0,    0, NULL, (void*) &appData.renderPiecesWithFont, NULL, NULL, TextBox, N_("Font name:") },\r
834   {   0,  0,    0, NULL, (void*) &appData.fontToPieceTable, NULL, NULL, TextBox, N_("Font piece to char:") },\r
835 //  {   0,  0,    0, NULL, (void*) &DeleteTheme, NULL, NULL, Button, N_("Up") },\r
836 //  {   0,  0,    0, NULL, (void*) &DeleteTheme, NULL, NULL, Button, N_("Down") },\r
837   {   0,  0,    0, NULL, (void*) &DeleteTheme, NULL, NULL, Button, N_("Delete Theme") },\r
838   {   0,  1,    0, NULL, (void*) &ThemeOK, "", NULL, EndMark , "" }\r
839 };\r
840 \r
841 void\r
842 DeleteTheme (HWND hDlg)\r
843 {\r
844     char *p, *q;\r
845     int i, selected = SendDlgItemMessage(hDlg, 2001+2*1, LB_GETCURSEL, 0, 0);\r
846     HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*1);\r
847     if(selected < 0) return;\r
848     if(p = strstr(appData.themeNames, engineList[selected])) {\r
849         if(q = strchr(p, '\n')) strcpy(p, q+1);\r
850     }\r
851     themeOptions[0].max = NamesToList(appData.themeNames, engineList, engineMnemonic, ""); // replace list by only the group contents\r
852     SendMessage(hwndCombo, LB_RESETCONTENT, 0, 0);\r
853     SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) "");\r
854     for(i=1; i<themeOptions[0].max; i++) {\r
855             SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
856     }\r
857 }\r
858 \r
859 int\r
860 PickTheme (HWND hDlg)\r
861 {\r
862     char buf[MSG_SIZ];\r
863     HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*1);\r
864     int i = SendDlgItemMessage(hDlg, 2001+2*1, LB_GETCURSEL, 0, 0);\r
865     if(i == 0) buf[0] = NULLCHAR; // back to top level\r
866     else if(engineList[i][0] == '#') safeStrCpy(buf, engineList[i], MSG_SIZ); // group header, open group\r
867     else {\r
868         ASSIGN(engineLine, engineList[i]);\r
869         LoadTheme();\r
870         EndDialog( hDlg, 0 );\r
871         return 0; // normal line, select engine\r
872     }\r
873     themeOptions[0].max = NamesToList(appData.themeNames, engineList, engineMnemonic, buf); // replace list by only the group contents\r
874     SendMessage(hwndCombo, LB_RESETCONTENT, 0, 0);\r
875     SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) buf);\r
876     for(i=1; i<themeOptions[0].max; i++) {\r
877             SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
878     }\r
879     return 0;\r
880 }\r
881 \r
882 void ThemeOptionsPopup(HWND hwnd)\r
883 {\r
884     addToList = TRUE; // defaults\r
885     if(nickName)     free(nickName);     nickName = strdup("");\r
886     if(engineLine)   free(engineLine);   engineLine = strdup("");\r
887     themeOptions[0].max = NamesToList(appData.themeNames, engineList, engineMnemonic, ""); // only top level\r
888     snprintf(title, MSG_SIZ, _("Board themes"));\r
889 \r
890     GenericPopup(hwnd, themeOptions);\r
891 }\r
892 \r
893 Boolean autoinc, twice, swiss;\r
894 char *tfName;\r
895 \r
896 int MatchOK()\r
897 {\r
898     if(autoinc) appData.loadGameIndex = appData.loadPositionIndex = -(twice + 1); else\r
899     if(!appData.loadGameFile[0]) appData.loadGameIndex = -2*twice; // kludge to pass value of "twice" for use in GUI book\r
900     if(swiss) { appData.defaultMatchGames = 1; appData.tourneyType = -1; }\r
901     if(CreateTourney(tfName) && !matchMode) { // CreateTourney reloads original settings if file already existed\r
902         MatchEvent(2);\r
903         return 1; // close dialog\r
904     }\r
905     return matchMode || !appData.participants[0]; // if we failed to create and are not in playing, forbid popdown if there are participants\r
906 }\r
907 \r
908 void PseudoOK(HWND hDlg)\r
909 {\r
910     if(matchMode) return;\r
911     okFunc = 0;\r
912     GetOptionValues(hDlg, activeCps, activeList);\r
913     EndDialog( hDlg, 0 );\r
914     comboCallback = NULL; activeCps = NULL;\r
915 \r
916     if(autoinc) appData.loadGameIndex = appData.loadPositionIndex = -(twice + 1); else\r
917     if(!appData.loadGameFile[0]) appData.loadGameIndex = -2*twice; // kludge to pass value of "twice" for use in GUI book\r
918     if(!autoinc && !twice) { // prevent auto-inc being remembered in index value if checkboxes not ticked\r
919         if(appData.loadGameIndex < 0) appData.loadGameIndex = 0;\r
920         if(appData.loadPositionIndex < 0) appData.loadPositionIndex = 0;\r
921     }\r
922     if(swiss) { appData.defaultMatchGames = 1; appData.tourneyType = -1; }\r
923     ASSIGN(appData.tourneyFile, tfName);\r
924 }\r
925 \r
926 char *GetParticipants(HWND hDlg)\r
927 {\r
928     int len = GetWindowTextLength(GetDlgItem(hDlg, 2001+2*0)) + 1;\r
929     char *participants,*p, *q;\r
930     if(len < 4) return NULL; // box is empty (enough)\r
931     participants = (char*) malloc(len);\r
932     GetDlgItemText(hDlg, 2001+2*0, participants, len );\r
933     p = q = participants;\r
934     while(*p++ = *q++) if(p[-1] == '\r') p--;\r
935     return participants;\r
936 }\r
937 \r
938 void ReplaceParticipant(HWND hDlg)\r
939 {\r
940     char *participants = GetParticipants(hDlg);\r
941     Substitute(participants, TRUE);\r
942 }\r
943         \r
944 void UpgradeParticipant(HWND hDlg)\r
945 {\r
946     char *participants = GetParticipants(hDlg);\r
947     Substitute(participants, FALSE);\r
948 }\r
949 \r
950 void Inspect(HWND hDlg)\r
951 {\r
952     FILE *f;\r
953     char name[MSG_SIZ];\r
954     GetDlgItemText(hDlg, 2001+2*33, name, MSG_SIZ );\r
955     if(name[0] && (f = fopen(name, "r")) ) {\r
956         char *saveSaveFile;\r
957         saveSaveFile = appData.saveGameFile; appData.saveGameFile = NULL; // this is a persistent option, protect from change\r
958         ParseArgsFromFile(f);\r
959         autoinc = ((appData.loadPositionFile[0] ? appData.loadGameIndex : appData.loadPositionIndex) < 0);\r
960         twice = ((appData.loadPositionFile[0] ? appData.loadGameIndex : appData.loadPositionIndex) == -2);\r
961         swiss = appData.tourneyType < 0;\r
962         SetOptionValues(hDlg, NULL, activeList);\r
963         FREE(appData.saveGameFile); appData.saveGameFile = saveSaveFile;\r
964     } else DisplayError(_("First you must specify an existing tourney file to clone"), 0);\r
965 }\r
966 \r
967 void TimeControlOptionsPopup P((HWND hDlg));\r
968 void UciOptionsPopup P((HWND hDlg));\r
969 int AddToTourney P((HWND hDlg));\r
970 \r
971 Option tourneyOptions[] = {\r
972   { 80,  15,        0, NULL, (void*) &AddToTourney, NULL, engineMnemonic, ListBox, N_("Select Engine:") },\r
973   { 0xD, 15,        0, NULL, (void*) &appData.participants, "", NULL, TextBox, N_("Tourney participants:") },\r
974   { 0,  0,          4, NULL, (void*) &tfName, "", NULL, FileName, N_("Tournament file:") },\r
975   { 30, 0,          0, NULL, NULL, NULL, NULL, Label, N_("If you specify an existing file, the rest of this dialog will be ignored.") },\r
976   { 30, 0,          0, NULL, NULL, NULL, NULL, Label, N_("Otherwise, the file will be created, with the settings you specify below:") },\r
977   { 0,  0,          0, NULL, (void*) &swiss, "", NULL, CheckBox, N_("Use Swiss pairing engine (cycles = rounds)") },\r
978   { 0,  0,         10, NULL, (void*) &appData.tourneyType, "", NULL, Spin, N_("Tourney type (0=RR, 1=gauntlet):") },\r
979   { 0,  0,          0, NULL, (void*) &appData.cycleSync, "", NULL, CheckBox, N_("Sync after cycle") },\r
980   { 0,  1, 1000000000, NULL, (void*) &appData.tourneyCycles, "", NULL, Spin, N_("Number of tourney cycles:") },\r
981   { 0,  0,          0, NULL, (void*) &appData.roundSync, "", NULL, CheckBox, N_("Sync after round") },\r
982   { 0,  1, 1000000000, NULL, (void*) &appData.defaultMatchGames, "", NULL, Spin, N_("Games per Match / Pairing:") },\r
983   { 0,  0,          1, NULL, (void*) &appData.saveGameFile, "", NULL, FileName, N_("File for saving tourney games:") },\r
984   { 0,  0,       32+1, NULL, (void*) &appData.loadGameFile, "", NULL, FileName, N_("Game File with Opening Lines:") },\r
985   { 0, -2, 1000000000, NULL, (void*) &appData.loadGameIndex, "", NULL, Spin, N_("Game Number:") },\r
986   { 0,  0,       32+2, NULL, (void*) &appData.loadPositionFile, "", NULL, FileName, N_("File with Start Positions:") },\r
987   { 0, -2, 1000000000, NULL, (void*) &appData.loadPositionIndex, "", NULL, Spin, N_("Position Number:") },\r
988   { 0,  0,          0, NULL, (void*) &autoinc, "", NULL, CheckBox, N_("Step through lines/positions in file") },\r
989   { 0,  0, 1000000000, NULL, (void*) &appData.rewindIndex, "", NULL, Spin, N_("Rewind after (0 = never):") },\r
990   { 0,  0,          0, NULL, (void*) &twice, "", NULL, CheckBox, N_("Use each line/position twice") },\r
991   { 0,  0,          0, NULL, (void*) &appData.defNoBook, "", NULL, CheckBox, N_("Make all use GUI book by default") },\r
992   { 0,  0, 1000000000, NULL, (void*) &appData.matchPause, "", NULL, Spin, N_("Pause between Games (ms):") },\r
993   { 0,  0,          0, NULL, (void*) &ReplaceParticipant, "", NULL, Button, N_("Replace Engine") },\r
994   { 0,  0,          0, NULL, (void*) &UpgradeParticipant, "", NULL, Button, N_("Upgrade Engine") },\r
995   { 0,  0,          0, NULL, (void*) &TimeControlOptionsPopup, "", NULL, Button, N_("Time Control...") },\r
996   { 0,  0,          0, NULL, (void*) &UciOptionsPopup, "", NULL, Button, N_("Common Engine...") },\r
997   { 0,  0,          0, NULL, (void*) &Inspect, "", NULL, Button, N_("Clone Tourney") },\r
998   { 0,  0,          0, NULL, (void*) &PseudoOK, "", NULL, Button, N_("Continue Later") },\r
999   { 0, 0, 0, NULL, (void*) &MatchOK, "", NULL, EndMark , "" }\r
1000 };\r
1001 \r
1002 int AddToTourney(HWND hDlg)\r
1003 {\r
1004   char buf[MSG_SIZ];\r
1005   HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*1);\r
1006   int i = SendDlgItemMessage(hDlg, 2001+2*1, LB_GETCURSEL, 0, 0);\r
1007   if(i<0) return 0;\r
1008   if(i == 0) buf[0] = NULLCHAR; // back to top level\r
1009   else if(engineList[i][0] == '#') safeStrCpy(buf, engineList[i], MSG_SIZ); // group header, open group\r
1010   else { // normal line, select engine\r
1011     snprintf(buf, MSG_SIZ, "%s\r\n", engineMnemonic[i]);\r
1012     SendMessage( GetDlgItem(hDlg, 2001+2*0), EM_SETSEL, 99999, 99999 );\r
1013     SendMessage( GetDlgItem(hDlg, 2001+2*0), EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) buf );\r
1014     return 0;\r
1015   }\r
1016   tourneyOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, buf); // replace list by only the group contents\r
1017   SendMessage(hwndCombo, LB_RESETCONTENT, 0, 0);\r
1018   SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) buf);\r
1019   for(i=1; i<tourneyOptions[0].max; i++) {\r
1020     SendMessage(hwndCombo, LB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
1021   }\r
1022 //  SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) 0, (LPARAM) buf);\r
1023   return 0;\r
1024 }\r
1025 \r
1026 void TourneyPopup(HWND hwnd)\r
1027 {\r
1028     int n = NamesToList(firstChessProgramNames, engineList, engineMnemonic, "");\r
1029     autoinc = appData.loadGameIndex < 0 || appData.loadPositionIndex < 0;\r
1030     twice = appData.loadGameIndex == -2 || appData.loadPositionIndex == -2; swiss = appData.tourneyType < 0;\r
1031     tourneyOptions[0].max = n;\r
1032     snprintf(title, MSG_SIZ, _("Tournament and Match Options"));\r
1033     ASSIGN(tfName, appData.tourneyFile[0] ? appData.tourneyFile : MakeName(appData.defName));\r
1034 \r
1035     GenericPopup(hwnd, tourneyOptions);\r
1036 }\r