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