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