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