Fix MAXENGINES in WinBoard
[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 *engineNr[] = { N_("First"), N_("Second"), NULL };\r
40 char *engineList[MAXENGINES] = {" "}, *engineMnemonic[MAXENGINES] = {""};\r
41 void (*okFunc)();\r
42 ChessProgramState *activeCps;\r
43 Option *activeList;\r
44 int InstallOK P((void));\r
45 typedef int ButtonCallback(HWND h);\r
46 ButtonCallback *comboCallback;\r
47 \r
48 void\r
49 PrintOpt(int i, int right, Option *optionList)\r
50 {\r
51     if(i<0) {\r
52         if(!right) fprintf(debugFP, "%30s", "");\r
53     } else {\r
54         Option opt = optionList[i];\r
55         switch(opt.type) {\r
56             case Slider:\r
57             case Spin:\r
58                 fprintf(debugFP, "%20.20s [    +/-]", opt.name);\r
59                 break;\r
60             case TextBox:\r
61             case FileName:\r
62             case PathName:\r
63                 fprintf(debugFP, "%20.20s [______________________________________]", opt.name);\r
64                 break;\r
65             case Label:\r
66                 fprintf(debugFP, "%41.41s", opt.name);\r
67                 break;\r
68             case CheckBox:\r
69                 fprintf(debugFP, "[x] %-26.25s", opt.name);\r
70                 break;\r
71             case ComboBox:\r
72                 fprintf(debugFP, "%20.20s [ COMBO ]", opt.name);\r
73                 break;\r
74             case Button:\r
75             case SaveButton:\r
76             case ResetButton:\r
77                 fprintf(debugFP, "[ %26.26s ]", opt.name);\r
78             case Message:\r
79             default:\r
80                 break;\r
81         }\r
82     }\r
83     fprintf(debugFP, right ? "\n" : " ");\r
84 }\r
85 \r
86 void\r
87 CreateOptionDialogTest(int *list, int nr, Option *optionList)\r
88 {\r
89     int line;\r
90 \r
91     for(line = 0; line < nr; line+=2) {\r
92         PrintOpt(list[line+1], 0, optionList);\r
93         PrintOpt(list[line], 1, optionList);\r
94     }\r
95 }\r
96 \r
97 void\r
98 LayoutOptions(int firstOption, int endOption, char *groupName, Option *optionList)\r
99 {\r
100     int i, b = strlen(groupName), stop, prefix, right, nextOption, firstButton = buttons;\r
101     Control lastType, nextType;\r
102 \r
103     nextOption = firstOption;\r
104     while(nextOption < endOption) {\r
105         checks = combos = 0; stop = 0;\r
106         lastType = Button; // kludge to make sure leading Spin will not be prefixed\r
107         // first separate out buttons for later treatment, and collect consecutive checks and combos\r
108         while(nextOption < endOption && !stop) {\r
109             switch(nextType = optionList[nextOption].type) {\r
110                 case CheckBox: checkList[checks++] = nextOption; lastType = CheckBox; break;\r
111                 case ComboBox: comboList[combos++] = nextOption; lastType = ComboBox; break;\r
112                 case ResetButton:\r
113                 case SaveButton:\r
114                 case Button:  buttonList[buttons++] = nextOption; lastType = Button; break;\r
115                 case TextBox:\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 == 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 == 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 == TextBox || nextType == FileName || nextType == PathName || nextType == Label) {\r
161             // A textBox is double width, so must be left-adjusted, and the right column remains empty\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                 layoutList[layout++] = -1;\r
167                 layoutList[layout++] = -1;\r
168             }\r
169         } else if(nextType == Spin) {\r
170             // A spin will go in the next available position (right to left!). If it had to be prefixed with\r
171             // a check or combo, this next position must be to the right, and the prefix goes left to it.\r
172             layoutList[layout++] = nextOption - 1;\r
173             if(prefix >= 0) layoutList[layout++] = prefix;\r
174         }\r
175     }\r
176     // take a new line if needed\r
177     if(layout&1) layoutList[layout++] = -1;\r
178     // emit the buttons belonging in this group; loose buttons are saved for last, to appear at bottom of dialog\r
179     if(b) {\r
180         while(buttons > firstButton)\r
181             layoutList[layout++] = buttonList[--buttons];\r
182         if(layout&1) layoutList[layout++] = -1;\r
183     }\r
184 }\r
185 \r
186 char *\r
187 EndMatch(char *s1, char *s2)\r
188 {\r
189         char *p, *q;\r
190         p = s1; while(*p) p++;\r
191         q = s2; while(*q) q++;\r
192         while(p > s1 && q > s2 && *p == *q) { p--; q--; }\r
193         if(p[1] == 0) return NULL;\r
194         return p+1;\r
195 }\r
196 \r
197 void\r
198 DesignOptionDialog(int nrOpt, Option *optionList)\r
199 {\r
200     int k=0, n=0;\r
201     char buf[MSG_SIZ];\r
202 \r
203     layout = 0;\r
204     buttons = groups = 0;\r
205     while(k < nrOpt) { // k steps through 'solitary' options\r
206         // look if we hit a group of options having names that start with the same word\r
207         int groupSize = 1, groupNameLength = 50;\r
208         sscanf(optionList[k].name, "%s", buf); // get first word of option name\r
209         while(k + groupSize < nrOpt &&\r
210               strstr(optionList[k+groupSize].name, buf) == optionList[k+groupSize].name) {\r
211                 int j;\r
212                 for(j=0; j<groupNameLength; j++) // determine common initial part of option names\r
213                     if( optionList[k].name[j] != optionList[k+groupSize].name[j]) break;\r
214                 groupNameLength = j;\r
215                 groupSize++;\r
216 \r
217         }\r
218         if(groupSize > 3) {\r
219                 // We found a group to terminates the current section\r
220                 LayoutOptions(n, k, "", optionList); // flush all solitary options appearing before the group\r
221                 groupNameList[groups] = groupNameLength;\r
222                 boxList[groups++] = layout; // group start in even entries\r
223                 LayoutOptions(k, k+groupSize, buf, optionList); // flush the group\r
224                 boxList[groups++] = layout; // group end in odd entries\r
225                 k = n = k + groupSize;\r
226         } else k += groupSize; // small groups are grouped with the solitary options\r
227     }\r
228     if(n != k) LayoutOptions(n, k, "", optionList); // flush remaining solitary options\r
229     // decide if and where we break into two column pairs\r
230 \r
231     // Emit buttons and add OK and cancel\r
232 //    for(k=0; k<buttons; k++) layoutList[layout++] = buttonList[k];\r
233  \r
234    // Create the dialog window\r
235     if(appData.debugMode) CreateOptionDialogTest(layoutList, layout, optionList);\r
236 //    CreateOptionDialog(layoutList, layout, optionList);\r
237     if(!activeCps) okFunc = optionList[nrOpt].target;\r
238 }\r
239 \r
240 #include <windows.h>\r
241 \r
242 extern HINSTANCE hInst;\r
243 \r
244 typedef struct {\r
245     DLGITEMTEMPLATE item;\r
246     WORD code;\r
247     WORD controlType;\r
248     wchar_t d1, data;\r
249     WORD creationData;\r
250 } Item;\r
251 \r
252 struct {\r
253     DLGTEMPLATE header;\r
254     WORD menu;\r
255     WORD winClass;\r
256     wchar_t title[20];\r
257     WORD pointSize;\r
258     wchar_t fontName[14];\r
259     Item control[MAX_OPTIONS];\r
260 } template = {\r
261     { DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_SETFONT, 0, 0, 0, 0, 295, 300 },\r
262     0x0000, 0x0000, L"Engine #1 Settings ", 8, L"MS Sans Serif"\r
263 };\r
264 \r
265 char *\r
266 AddCR(char *s)\r
267 {\r
268     char *p=s, *q;\r
269     int n=0;\r
270     while(p = strchr(p, '\n')) p++, n++; // count linefeeds\r
271     p = q = malloc(strlen(s) + n + 1);\r
272     while(*p++ = *s++) if(p[-1] == '\n') p[-1] = '\r', *p++ = '\n';\r
273     return q;\r
274 }\r
275 \r
276 void\r
277 SetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)\r
278 // Put all current option values in controls, and write option names next to them\r
279 {\r
280     HANDLE hwndCombo;\r
281     int i, k;\r
282     char **choices, *name;\r
283 \r
284     for(i=0; i<layout+buttons; i++) {\r
285         int j=layoutList[i];\r
286         if(j == -2) SetDlgItemText( hDlg, 2000+2*i, ". . ." );\r
287         if(j<0) continue;\r
288         name = cps ? optionList[j].name : _(optionList[j].name);\r
289         if(strstr(name, "Polyglot ") == name) name += 9;\r
290         SetDlgItemText( hDlg, 2000+2*i, name );\r
291 //if(appData.debugMode) fprintf(debugFP, "# %s = %d\n",optionList[j].name, optionList[j].value );\r
292         switch(optionList[j].type) {\r
293             case Spin:\r
294                 SetDlgItemInt( hDlg, 2001+2*i, cps ? optionList[j].value : *(int*)optionList[j].target, TRUE );\r
295                 break;\r
296             case TextBox:\r
297             case FileName:\r
298             case PathName:\r
299                 name = AddCR(cps ? optionList[j].textValue : *(char**)optionList[j].target); // stupid CR...\r
300                 SetDlgItemText( hDlg, 2001+2*i, name);\r
301                 free(name);\r
302                 break;\r
303             case CheckBox:\r
304                 CheckDlgButton( hDlg, 2000+2*i, (cps ? optionList[j].value : *(Boolean*)optionList[j].target) != 0);\r
305                 break;\r
306             case ComboBox:\r
307                 choices = (char**) optionList[j].textValue;\r
308                 hwndCombo = GetDlgItem(hDlg, 2001+2*i);\r
309                 SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);\r
310                 for(k=0; k<optionList[j].max; k++) {\r
311                     SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) choices[k]);\r
312                 }\r
313                 SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) choices[optionList[j].value]);\r
314                 break;\r
315             case Button:\r
316             case SaveButton:\r
317             default:\r
318                 break;\r
319         }\r
320     }\r
321     SetDlgItemText( hDlg, IDOK, _("OK") );\r
322     SetDlgItemText( hDlg, IDCANCEL, _("Cancel") );\r
323     title[0] &= ~32; // capitalize\r
324     SetWindowText( hDlg, title);\r
325     for(i=0; i<groups; i+=2) {\r
326         int id, p; char buf[MSG_SIZ];\r
327         id = k = boxList[i];\r
328         if(layoutList[k] < 0) k++;\r
329         if(layoutList[k] < 0) continue;\r
330         for(p=0; p<groupNameList[i]; p++) buf[p] = optionList[layoutList[k]].name[p];\r
331         buf[p] = 0;\r
332         SetDlgItemText( hDlg, 2000+2*(id+MAX_OPTIONS), buf );\r
333     }\r
334 }\r
335 \r
336 \r
337 int\r
338 GetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)\r
339 // read out all controls, and if value is altered, remember it and send it to the engine\r
340 {\r
341     HANDLE hwndCombo;\r
342     int i, k, new=0, changed=0, len;\r
343     char **choices, newText[MSG_SIZ], buf[MSG_SIZ], *text;\r
344     BOOL success;\r
345 \r
346     for(i=0; i<layout; i++) {\r
347         int j=layoutList[i];\r
348         if(j<0) continue;\r
349         switch(optionList[j].type) {\r
350             case Spin:\r
351                 new = GetDlgItemInt( hDlg, 2001+2*i, &success, TRUE );\r
352                 if(!success) break;\r
353                 if(new < optionList[j].min) new = optionList[j].min;\r
354                 if(new > optionList[j].max) new = optionList[j].max;\r
355                 if(!cps) { *(int*)optionList[j].target = new; break; }\r
356                 changed = 2*(optionList[j].value != new);\r
357                 optionList[j].value = new;\r
358                 break;\r
359             case TextBox:\r
360             case FileName:\r
361             case PathName:\r
362                 if(cps) len = MSG_SIZ - strlen(optionList[j].name) - 9, text = newText;\r
363                 else    len = GetWindowTextLength(GetDlgItem(hDlg, 2001+2*i)) + 1, text = (char*) malloc(len);\r
364                 success = GetDlgItemText( hDlg, 2001+2*i, text, len );\r
365                 if(!success) text[0] = NULLCHAR; // empty string can be valid input\r
366                 if(!cps) {\r
367                     char *p;\r
368                     p = (optionList[j].type != FileName ? strdup(text) : InterpretFileName(text, homeDir)); // all files relative to homeDir!\r
369                     FREE(*(char**)optionList[j].target); *(char**)optionList[j].target = p;\r
370                     free(text); text = p;\r
371                     while(*p++ = *text++) if(p[-1] == '\r') p--; // crush CR\r
372                     break;\r
373                 }\r
374                 changed = strcmp(optionList[j].textValue, newText) != 0;\r
375                 safeStrCpy(optionList[j].textValue, newText, MSG_SIZ - (optionList[j].textValue - optionList[j].name) );\r
376                 break;\r
377             case CheckBox:\r
378                 new = IsDlgButtonChecked( hDlg, 2000+2*i );\r
379                 if(!cps) { *(Boolean*)optionList[j].target = new; break; }\r
380                 changed = 2*(optionList[j].value != new);\r
381                 optionList[j].value = new;\r
382                 break;\r
383             case ComboBox:\r
384                 choices = (char**) optionList[j].textValue;\r
385                 hwndCombo = GetDlgItem(hDlg, 2001+2*i);\r
386                 success = GetDlgItemText( hDlg, 2001+2*i, newText, MSG_SIZ );\r
387                 if(!success) break;\r
388                 new = -1;\r
389                 for(k=0; k<optionList[j].max; k++) {\r
390                     if(choices[k] && !strcmp(choices[k], newText)) new = k;\r
391                 }\r
392                 if(!cps && new > 0) {\r
393                     if(*(char**)optionList[j].target) free(*(char**)optionList[j].target);\r
394                     *(char**)optionList[j].target = strdup(optionList[j].choice[new]);\r
395                     break;\r
396                 }\r
397                 changed = new >= 0 && (optionList[j].value != new);\r
398                 if(changed) optionList[j].value = new;\r
399                 break;\r
400             case Button:\r
401             default:\r
402                 break; // are treated instantly, so they have been sent already\r
403         }\r
404         if(changed == 2)\r
405           snprintf(buf, MSG_SIZ, "option %s=%d\n", optionList[j].name, new); else\r
406         if(changed == 1)\r
407           snprintf(buf, MSG_SIZ, "option %s=%s\n", optionList[j].name, newText);\r
408         if(changed) SendToProgram(buf, cps);\r
409     }\r
410     if(!cps && okFunc) return ((ButtonCallback*) okFunc)(0);\r
411     return 1;\r
412 }\r
413 \r
414 char *defaultExt[] = { NULL, "pgn", "fen", "exe", "trn", "bin", "log", "ini" };\r
415 \r
416 LRESULT CALLBACK SettingsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
417 {\r
418     char buf[MSG_SIZ];\r
419     int i, j, ext;\r
420 \r
421     switch( message )\r
422     {\r
423     case WM_INITDIALOG:\r
424 \r
425 //        CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));\r
426         SetOptionValues(hDlg, activeCps, activeList);\r
427 \r
428         SetFocus(GetDlgItem(hDlg, IDCANCEL));\r
429 \r
430         break;\r
431 \r
432     case WM_COMMAND:\r
433         switch( LOWORD(wParam) ) {\r
434         case IDOK:\r
435             if(!GetOptionValues(hDlg, activeCps, activeList)) return FALSE;\r
436             EndDialog( hDlg, 0 );\r
437             comboCallback = NULL; activeCps = NULL;\r
438             return TRUE;\r
439 \r
440         case IDCANCEL:\r
441             EndDialog( hDlg, 1 );\r
442             comboCallback = NULL; activeCps = NULL;\r
443             return TRUE;\r
444 \r
445         default:\r
446             // program-defined push buttons\r
447             i = LOWORD(wParam);\r
448             if( i>=2000 &&  i < 2000+2*(layout+buttons)) {\r
449                 j = layoutList[(i - 2000)/2];\r
450                 if(j == -2) {\r
451                           char filter[] =\r
452                                 "All files\0*.*\0Game files\0*.pgn;*.gam\0Position files\0*.fen;*.epd;*.pos\0"\r
453                                 "EXE files\0*.exe\0Tournament files (*.trn)\0*.trn\0"\r
454                                 "BIN Files\0*.bin\0LOG Files\0*.log\0INI Files\0*.ini\0\0";\r
455                           OPENFILENAME ofn;\r
456 \r
457                           safeStrCpy( buf, "" , sizeof( buf)/sizeof( buf[0]) );\r
458 \r
459                           ZeroMemory( &ofn, sizeof(ofn) );\r
460 \r
461                           ofn.lStructSize = sizeof(ofn);\r
462                           ofn.hwndOwner = hDlg;\r
463                           ofn.hInstance = hInst;\r
464                           ofn.lpstrFilter = filter;\r
465                           ofn.nFilterIndex      = 1L + (ext = activeCps ? 0 : activeList[layoutList[(i-2000)/2+1]].max);\r
466                           ofn.lpstrDefExt       = defaultExt[ext];\r
467                           ofn.lpstrFile = buf;\r
468                           ofn.nMaxFile = sizeof(buf);\r
469                           ofn.lpstrTitle = _("Choose File");\r
470                           ofn.Flags = OFN_FILEMUSTEXIST | OFN_LONGNAMES | OFN_HIDEREADONLY;\r
471 \r
472                           if( GetOpenFileName( &ofn ) ) {\r
473                               SetDlgItemText( hDlg, i+3, buf );\r
474                           }\r
475                 } else\r
476                 if(j == -3) {\r
477                     if( BrowseForFolder( _("Choose Folder:"), buf ) ) {\r
478                         SetDlgItemText( hDlg, i+3, buf );\r
479                     }\r
480                 }\r
481                 if(j < 0) break;\r
482                 if(comboCallback && activeList[j].type == ComboBox && HIWORD(wParam) == CBN_SELCHANGE) {\r
483                     if(j > 5) break; // Yegh! Must solve problem with more than one ombobox in dialog\r
484                     (*comboCallback)(hDlg);\r
485                     break;\r
486                 } else\r
487                 if( activeList[j].type  == SaveButton)\r
488                      GetOptionValues(hDlg, activeCps, activeList);\r
489                 else if( activeList[j].type  != Button) break;\r
490                 else if( !activeCps ) { (*(ButtonCallback*) activeList[j].target)(hDlg); break; }\r
491                 snprintf(buf, MSG_SIZ, "option %s\n", activeList[j].name);\r
492                 SendToProgram(buf, activeCps);\r
493             }\r
494             break;\r
495         }\r
496 \r
497         break;\r
498     }\r
499 \r
500     return FALSE;\r
501 }\r
502 \r
503 void AddControl(int x, int y, int w, int h, int type, int style, int n)\r
504 {\r
505     int i;\r
506 \r
507     i = template.header.cdit++;\r
508     template.control[i].item.style = style;\r
509     template.control[i].item.dwExtendedStyle = 0;\r
510     template.control[i].item.x = x;\r
511     template.control[i].item.y = y;\r
512     template.control[i].item.cx = w;\r
513     template.control[i].item.cy = h;\r
514     template.control[i].item.id = 2000 + n;\r
515     template.control[i].code = 0xFFFF;\r
516     template.control[i].controlType = type;\r
517     template.control[i].d1 = ' ';\r
518     template.control[i].data = 0;\r
519     template.control[i].creationData = 0;\r
520 }\r
521 \r
522 void AddOption(int x, int y, Control type, int i)\r
523 {\r
524     int extra;\r
525 \r
526     switch(type) {\r
527         case Slider:\r
528         case Spin:\r
529             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
530             AddControl(x+95, y, 50, 11, 0x0081, ES_AUTOHSCROLL | ES_NUMBER | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
531             break;\r
532         case TextBox:\r
533             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
534             extra = 13*activeList[layoutList[i/2]].min;\r
535             AddControl(x+95, y, 200, 11+extra, 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP | \r
536                                                                 (extra ? ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL :0), i+1);\r
537             break;\r
538         case Label:\r
539             extra = activeList[layoutList[i/2]].value;\r
540             AddControl(x+extra, y+1, 290-extra, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
541             break;\r
542         case FileName:\r
543         case PathName:\r
544             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
545             AddControl(x+95, y, 180, 11, 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
546             AddControl(x+275, y, 20, 12, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i-2);\r
547             layoutList[i/2-1] = -2 - (type == PathName);\r
548             break;\r
549         case CheckBox:\r
550             AddControl(x, y, 145, 11, 0x0080, BS_AUTOCHECKBOX | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
551             break;\r
552         case ComboBox:\r
553             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
554             AddControl(x+95, y-1, !activeCps && x<10 ? 120 : 50, 500, 0x0085,\r
555                         CBS_AUTOHSCROLL | CBS_DROPDOWN | WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_VSCROLL, i+1);\r
556             break;\r
557         case Button:\r
558         case ResetButton:\r
559         case SaveButton:\r
560             AddControl(x-2, y, 65, 13, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
561         case Message:\r
562         default:\r
563             break;\r
564     }\r
565 \r
566 }\r
567 \r
568 void\r
569 CreateDialogTemplate(int *layoutList, int nr, Option *optionList)\r
570 {\r
571     int i, ii, j, x=1, y=0, buttonRows, breakPoint = -1, k=0;\r
572 \r
573     template.header.cdit = 0;\r
574     template.header.cx = 307;\r
575     buttonRows = (buttons + 1 + 3)/4; // 4 per row, rounded up\r
576     if(nr > 50) {\r
577         breakPoint = (nr+2*buttonRows+1)/2 & ~1;\r
578         template.header.cx = 625;\r
579     }\r
580 \r
581     for(ii=0; ii<nr; ii++) {\r
582         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
583         if(k < groups && ii == boxList[k]) {\r
584             y += 10;\r
585             AddControl(x+2, y+13*(i>>1)-2, 301, 13*(boxList[k+1]-boxList[k]>>1)+8,\r
586                                                 0x0082, WS_VISIBLE | WS_CHILD | SS_BLACKFRAME, 2400);\r
587             AddControl(x+60, y+13*(i>>1)-6, 10*groupNameList[k]/3, 10,\r
588                                                 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, 2*(ii+MAX_OPTIONS));\r
589         }\r
590         j = layoutList[i];\r
591         if(j >= 0)\r
592             AddOption(x+155-150*(i&1), y+13*(i>>1)+5, optionList[j].type, 2*i);\r
593         if(k < groups && ii+1 == boxList[k+1]) {\r
594             k += 2; y += 4;\r
595         }\r
596         if(ii+1 == breakPoint) { x += 318; y = -13*(breakPoint>>1); }\r
597     }\r
598     // add butons at the bottom of dialog window\r
599     y += 13*(nr>>1)+5;\r
600 \r
601     for(i=0; i<buttons; i++) {\r
602         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
603         layoutList[nr+i] = buttonList[i];\r
604     }\r
605     AddControl(x+225, y+18*(buttonRows-1), 30, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, IDOK-2000);\r
606     AddControl(x+260, y+18*(buttonRows-1), 40, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, IDCANCEL-2000);\r
607     template.title[8] = optionList == first.option ? '1' :  '2';\r
608     template.header.cy = y += 18*buttonRows+2;\r
609     template.header.style &= ~WS_VSCROLL;\r
610 }\r
611 \r
612 void\r
613 EngineOptionsPopup(HWND hwnd, ChessProgramState *cps)\r
614 {\r
615     FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );\r
616 \r
617     activeCps = cps; activeList = cps->option;\r
618     snprintf(title, MSG_SIZ, _("%s Engine Settings (%s)"), T_(cps->which), cps->tidy);\r
619     DesignOptionDialog(cps->nrOptions, cps->option);\r
620     CreateDialogTemplate(layoutList, layout, cps->option);\r
621 \r
622 \r
623     DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );\r
624 \r
625     FreeProcInstance(lpProc);\r
626 \r
627     return;\r
628 }\r
629 \r
630 int InstallOK()\r
631 {\r
632     if(engineLine[0] == '#') { DisplayError(_("Select single engine from the group"), 0); return 0; }\r
633     if(isUCCI) isUCI = 2;\r
634     if(engineChoice[0] == engineNr[0][0])  Load(&first, 0); else Load(&second, 1);\r
635     return 1;\r
636 }\r
637 \r
638 Option installOptions[] = {\r
639   {   0,  0,    0, NULL, (void*) &engineLine, (char*) engineMnemonic, engineList, ComboBox, N_("Select engine from list:") },\r
640   {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("or specify one below:") },\r
641   {   0,  0,    0, NULL, (void*) &nickName, NULL, NULL, TextBox, N_("Nickname (optional):") },\r
642   {   0,  0,    0, NULL, (void*) &useNick, NULL, NULL, CheckBox, N_("Use nickname in PGN tag") },\r
643   {   0,  0,    3, NULL, (void*) &engineName, NULL, NULL, FileName, N_("Engine (*.exe):") },\r
644   {   0,  0,    0, NULL, (void*) &params, NULL, NULL, TextBox, N_("command-line parameters:") },\r
645   {   0,  0,    0, NULL, (void*) &engineDir, NULL, NULL, PathName, N_("directory:") },\r
646   {  95,  0,    0, NULL, NULL, NULL, NULL, Label, N_("(Directory will be derived from engine path when left empty)") },\r
647   {   0,  0,    0, NULL, (void*) &addToList, NULL, NULL, CheckBox, N_("Add this engine to the list") },\r
648   {   0,  0,    0, NULL, (void*) &hasBook, NULL, NULL, CheckBox, N_("Must not use GUI book") },\r
649   {   0,  0,    0, NULL, (void*) &isUCI, NULL, NULL, CheckBox, N_("UCI") },\r
650   {   0,  0,    0, NULL, (void*) &v1, NULL, NULL, CheckBox, N_("WB protocol v1 (skip waiting for features)") },\r
651   {   0,  0,    0, NULL, (void*) &isUCCI, NULL, NULL, CheckBox, N_("UCCI / USI (uses specified /uxiAdapter)") },\r
652   {   0,  0,    0, NULL, (void*) &storeVariant, NULL, NULL, CheckBox, N_("Force current variant with this engine") },\r
653   {   0,  0,    2, NULL, (void*) &engineChoice, (char*) engineNr, engineNr, ComboBox, N_("Load mentioned engine as") },\r
654   {   0,  1,    0, NULL, (void*) &InstallOK, "", NULL, EndMark , "" }\r
655 };\r
656 \r
657 void\r
658 GenericPopup(HWND hwnd, Option *optionList)\r
659 {\r
660     FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );\r
661     int n=0;\r
662 \r
663     while(optionList[n].type != EndMark) n++;\r
664     activeCps = NULL; activeList = optionList;\r
665     DesignOptionDialog(n, optionList);\r
666     CreateDialogTemplate(layoutList, layout, optionList);\r
667 \r
668     DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );\r
669 \r
670     FreeProcInstance(lpProc);\r
671 \r
672     return;\r
673 }\r
674 \r
675 int\r
676 EnterGroup(HWND hDlg)\r
677 {\r
678     char buf[MSG_SIZ];\r
679     HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*1);\r
680     int i = ComboBox_GetCurSel(hwndCombo);\r
681     if(i == 0) buf[0] = NULLCHAR; // back to top level\r
682     else if(engineList[i][0] == '#') safeStrCpy(buf, engineList[i], MSG_SIZ); // group header, open group\r
683     else return 0; // normal line, select engine\r
684     installOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, buf); // replace list by only the group contents\r
685     SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);\r
686     SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) buf);\r
687     for(i=1; i<installOptions[0].max; i++) {\r
688             SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
689     }\r
690     SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) 0, (LPARAM) buf);\r
691     return 0;\r
692 }\r
693 \r
694 void LoadEnginePopUp(HWND hwnd)\r
695 {\r
696     isUCI = isUCCI = storeVariant = v1 = useNick = FALSE; addToList = hasBook = TRUE; // defaults\r
697     if(engineDir)    free(engineDir);    engineDir = strdup("");\r
698     if(params)       free(params);       params = strdup("");\r
699     if(nickName)     free(nickName);     nickName = strdup("");\r
700     if(engineChoice) free(engineChoice); engineChoice = strdup(engineNr[0]);\r
701     if(engineLine)   free(engineLine);   engineLine = strdup("");\r
702     if(engineName)   free(engineName);   engineName = strdup("");\r
703     installOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, ""); // only top level\r
704     snprintf(title, MSG_SIZ, _("Load Engine"));\r
705     comboCallback = &EnterGroup;\r
706 \r
707     GenericPopup(hwnd, installOptions);\r
708 }\r
709 \r
710 Boolean autoinc, twice, swiss;\r
711 char *tfName;\r
712 \r
713 int MatchOK()\r
714 {\r
715     if(autoinc) appData.loadGameIndex = appData.loadPositionIndex = -(twice + 1);\r
716     if(!appData.loadGameFile[0]) appData.loadGameIndex = -2*twice; // kludge to pass value of "twice" for use in GUI book\r
717     if(swiss) { appData.defaultMatchGames = 1; appData.tourneyType = -1; }\r
718     if(CreateTourney(tfName) && !matchMode) { // CreateTourney reloads original settings if file already existed\r
719         MatchEvent(2);\r
720         return 1; // close dialog\r
721     }\r
722     return matchMode || !appData.participants[0]; // if we failed to create and are not in playing, forbid popdown if there are participants\r
723 }\r
724 \r
725 char *GetParticipants(HWND hDlg)\r
726 {\r
727     int len = GetWindowTextLength(GetDlgItem(hDlg, 2001+2*9)) + 1;\r
728     char *participants,*p, *q;\r
729     if(len < 4) return NULL; // box is empty (enough)\r
730     participants = (char*) malloc(len);\r
731     GetDlgItemText(hDlg, 2001+2*9, participants, len );\r
732     p = q = participants;\r
733     while(*p++ = *q++) if(p[-1] == '\r') p--;\r
734     return participants;\r
735 }\r
736 \r
737 void ReplaceParticipant(HWND hDlg)\r
738 {\r
739     char *participants = GetParticipants(hDlg);\r
740     Substitute(participants, TRUE);\r
741 }\r
742         \r
743 void UpgradeParticipant(HWND hDlg)\r
744 {\r
745     char *participants = GetParticipants(hDlg);\r
746     Substitute(participants, FALSE);\r
747 }\r
748 \r
749 void Inspect(HWND hDlg)\r
750 {\r
751     FILE *f;\r
752     char name[MSG_SIZ];\r
753     GetDlgItemText(hDlg, 2001+2*1, name, MSG_SIZ );\r
754     if(name && name[0] && (f = fopen(name, "r")) ) {\r
755         char *saveSaveFile;\r
756         saveSaveFile = appData.saveGameFile; appData.saveGameFile = NULL; // this is a persistent option, protect from change\r
757         ParseArgsFromFile(f);\r
758         autoinc = ((appData.loadPositionFile[0] ? appData.loadGameIndex : appData.loadPositionIndex) < 0);\r
759         twice = ((appData.loadPositionFile[0] ? appData.loadGameIndex : appData.loadPositionIndex) == -2);\r
760         swiss = appData.tourneyType < 0;\r
761         SetOptionValues(hDlg, NULL, activeList);\r
762         FREE(appData.saveGameFile); appData.saveGameFile = saveSaveFile;\r
763     } else DisplayError(_("First you must specify an existing tourney file to clone"), 0);\r
764 }\r
765 \r
766 void TimeControlOptionsPopup P((HWND hDlg));\r
767 void UciOptionsPopup P((HWND hDlg));\r
768 \r
769 Option tourneyOptions[] = {\r
770   { 0,  0,          4, NULL, (void*) &tfName, "", NULL, FileName, N_("Tournament file:") },\r
771   { 30, 0,          0, NULL, NULL, NULL, NULL, Label, N_("If you specify an existing file, the rest of this dialog will be ignored.") },\r
772   { 30, 0,          0, NULL, NULL, NULL, NULL, Label, N_("Otherwise, the file will be created, with the settings you specify below:") },\r
773   { 0,  1,          0, NULL, (void*) &engineChoice, (char*) engineMnemonic, engineMnemonic, ComboBox, N_("Select Engine:") },\r
774   { 0xD, 7,         0, NULL, (void*) &appData.participants, "", NULL, TextBox, N_("Tourney participants:") },\r
775   { 0,  0,          0, NULL, (void*) &swiss, "", NULL, CheckBox, N_("Use Swiss pairing engine (cycles = rounds)") },\r
776   { 0,  0,         10, NULL, (void*) &appData.tourneyType, "", NULL, Spin, N_("Tourney type (0=RR, 1=gauntlet):") },\r
777   { 0,  0,          0, NULL, (void*) &appData.cycleSync, "", NULL, CheckBox, N_("Sync after cycle") },\r
778   { 0,  1, 1000000000, NULL, (void*) &appData.tourneyCycles, "", NULL, Spin, N_("Number of tourney cycles:") },\r
779   { 0,  0,          0, NULL, (void*) &appData.roundSync, "", NULL, CheckBox, N_("Sync after round") },\r
780   { 0,  1, 1000000000, NULL, (void*) &appData.defaultMatchGames, "", NULL, Spin, N_("Games per Match / Pairing:") },\r
781   { 0,  0,          1, NULL, (void*) &appData.saveGameFile, "", NULL, FileName, N_("File for saving tourney games:") },\r
782   { 0,  0,          1, NULL, (void*) &appData.loadGameFile, "", NULL, FileName, N_("Game File with Opening Lines:") },\r
783   { 0, -2, 1000000000, NULL, (void*) &appData.loadGameIndex, "", NULL, Spin, N_("Game Number:") },\r
784   { 0,  0,          2, NULL, (void*) &appData.loadPositionFile, "", NULL, FileName, N_("File with Start Positions:") },\r
785   { 0, -2, 1000000000, NULL, (void*) &appData.loadPositionIndex, "", NULL, Spin, N_("Position Number:") },\r
786   { 0,  0,          0, NULL, (void*) &autoinc, "", NULL, CheckBox, N_("Step through lines/positions in file") },\r
787   { 0,  0, 1000000000, NULL, (void*) &appData.rewindIndex, "", NULL, Spin, N_("Rewind after (0 = never):") },\r
788   { 0,  0,          0, NULL, (void*) &twice, "", NULL, CheckBox, N_("Use each line/position twice") },\r
789   { 0,  0,          0, NULL, (void*) &appData.defNoBook, "", NULL, CheckBox, N_("Make all use GUI book by default") },\r
790   { 0,  0, 1000000000, NULL, (void*) &appData.matchPause, "", NULL, Spin, N_("Pause between Games (ms):") },\r
791   { 0,  0,          0, NULL, (void*) &ReplaceParticipant, "", NULL, Button, N_("Replace Engine") },\r
792   { 0,  0,          0, NULL, (void*) &UpgradeParticipant, "", NULL, Button, N_("Upgrade Engine") },\r
793   { 0,  0,          0, NULL, (void*) &TimeControlOptionsPopup, "", NULL, Button, N_("Time Control...") },\r
794   { 0,  0,          0, NULL, (void*) &UciOptionsPopup, "", NULL, Button, N_("Common Engine...") },\r
795   { 0,  0,          0, NULL, (void*) &Inspect, "", NULL, Button, N_("Clone Tourney") },\r
796   { 0, 0, 0, NULL, (void*) &MatchOK, "", NULL, EndMark , "" }\r
797 };\r
798 \r
799 int AddToTourney(HWND hDlg)\r
800 {\r
801   char buf[MSG_SIZ];\r
802   HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*7);\r
803   int i = ComboBox_GetCurSel(hwndCombo);\r
804   if(i == 0) buf[0] = NULLCHAR; // back to top level\r
805   else if(engineList[i][0] == '#') safeStrCpy(buf, engineList[i], MSG_SIZ); // group header, open group\r
806   else { // normal line, select engine\r
807     snprintf(buf, MSG_SIZ, "%s\r\n", engineMnemonic[i]);\r
808     SendMessage( GetDlgItem(hDlg, 2001+2*9), EM_SETSEL, 99999, 99999 );\r
809     SendMessage( GetDlgItem(hDlg, 2001+2*9), EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) buf );\r
810     return 0;\r
811   }\r
812   installOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, buf); // replace list by only the group contents\r
813   SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);\r
814   SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) buf);\r
815   for(i=1; i<installOptions[0].max; i++) {\r
816     SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
817   }\r
818   SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) 0, (LPARAM) buf);\r
819   return 0;\r
820 }\r
821 \r
822 void TourneyPopup(HWND hwnd)\r
823 {\r
824     int n = NamesToList(firstChessProgramNames, engineList, engineMnemonic, "");\r
825     comboCallback = &AddToTourney;\r
826     autoinc = appData.loadGameIndex < 0 || appData.loadPositionIndex < 0;\r
827     twice = FALSE; swiss = appData.tourneyType < 0;\r
828     tourneyOptions[3].max = n;\r
829     snprintf(title, MSG_SIZ, _("Tournament and Match Options"));\r
830     ASSIGN(tfName, appData.tourneyFile[0] ? appData.tourneyFile : MakeName(appData.defName));\r
831 \r
832     GenericPopup(hwnd, tourneyOptions);\r
833 }\r