Fix range of tourneyType spin 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;\r
38 extern Option installOptions[], matchOptions[];\r
39 char *engineNr[] = { N_("First"), N_("Second"), NULL };\r
40 char *engineList[1000] = {" "}, *engineMnemonic[1000] = {""};\r
41 void (*okFunc)();\r
42 ChessProgramState *activeCps;\r
43 Option *activeList;\r
44 void InstallOK P((void));\r
45 typedef void 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 void\r
266 SetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)\r
267 // Put all current option values in controls, and write option names next to them\r
268 {\r
269     HANDLE hwndCombo;\r
270     int i, k;\r
271     char **choices, *name;\r
272 \r
273     for(i=0; i<layout+buttons; i++) {\r
274         int j=layoutList[i];\r
275         if(j == -2) SetDlgItemText( hDlg, 2000+2*i, ". . ." );\r
276         if(j<0) continue;\r
277         name = optionList[j].name;\r
278         if(strstr(name, "Polyglot ") == name) name += 9;\r
279         SetDlgItemText( hDlg, 2000+2*i, name );\r
280 //if(appData.debugMode) fprintf(debugFP, "# %s = %d\n",optionList[j].name, optionList[j].value );\r
281         switch(optionList[j].type) {\r
282             case Spin:\r
283                 SetDlgItemInt( hDlg, 2001+2*i, cps ? optionList[j].value : *(int*)optionList[j].target, TRUE );\r
284                 break;\r
285             case TextBox:\r
286             case FileName:\r
287             case PathName:\r
288                 SetDlgItemText( hDlg, 2001+2*i, cps ? optionList[j].textValue : *(char**)optionList[j].target );\r
289                 break;\r
290             case CheckBox:\r
291                 CheckDlgButton( hDlg, 2000+2*i, (cps ? optionList[j].value : *(Boolean*)optionList[j].target) != 0);\r
292                 break;\r
293             case ComboBox:\r
294                 choices = (char**) optionList[j].textValue;\r
295                 hwndCombo = GetDlgItem(hDlg, 2001+2*i);\r
296                 SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);\r
297                 for(k=0; k<optionList[j].max; k++) {\r
298                     SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) choices[k]);\r
299                 }\r
300                 SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) choices[optionList[j].value]);\r
301                 break;\r
302             case Button:\r
303             case SaveButton:\r
304             default:\r
305                 break;\r
306         }\r
307     }\r
308     SetDlgItemText( hDlg, IDOK, _("OK") );\r
309     SetDlgItemText( hDlg, IDCANCEL, _("Cancel") );\r
310     title[0] &= ~32; // capitalize\r
311     SetWindowText( hDlg, title);\r
312     for(i=0; i<groups; i+=2) {\r
313         int id, p; char buf[MSG_SIZ];\r
314         id = k = boxList[i];\r
315         if(layoutList[k] < 0) k++;\r
316         if(layoutList[k] < 0) continue;\r
317         for(p=0; p<groupNameList[i]; p++) buf[p] = optionList[layoutList[k]].name[p];\r
318         buf[p] = 0;\r
319         SetDlgItemText( hDlg, 2000+2*(id+MAX_OPTIONS), buf );\r
320     }\r
321 }\r
322 \r
323 \r
324 void\r
325 GetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)\r
326 // read out all controls, and if value is altered, remember it and send it to the engine\r
327 {\r
328     HANDLE hwndCombo;\r
329     int i, k, new=0, changed=0, len;\r
330     char **choices, newText[MSG_SIZ], buf[MSG_SIZ], *text;\r
331     BOOL success;\r
332 \r
333     for(i=0; i<layout; i++) {\r
334         int j=layoutList[i];\r
335         if(j<0) continue;\r
336         switch(optionList[j].type) {\r
337             case Spin:\r
338                 new = GetDlgItemInt( hDlg, 2001+2*i, &success, TRUE );\r
339                 if(!success) break;\r
340                 if(new < optionList[j].min) new = optionList[j].min;\r
341                 if(new > optionList[j].max) new = optionList[j].max;\r
342                 if(!cps) { *(int*)optionList[j].target = new; break; }\r
343                 changed = 2*(optionList[j].value != new);\r
344                 optionList[j].value = new;\r
345                 break;\r
346             case TextBox:\r
347             case FileName:\r
348             case PathName:\r
349                 if(cps) len = MSG_SIZ - strlen(optionList[j].name) - 9, text = newText;\r
350                 else    len = GetWindowTextLength(GetDlgItem(hDlg, 2001+2*i)) + 1, text = (char*) malloc(len);\r
351                 success = GetDlgItemText( hDlg, 2001+2*i, text, len );\r
352                 if(!success) break;\r
353                 if(!cps) {\r
354                     char *p;\r
355                     if(*(char**)optionList[j].target) free(*(char**)optionList[j].target);\r
356                     *(char**)optionList[j].target = p = text;\r
357                     while(*p++ = *text++) if(p[-1] == '\r') p--; // crush CR\r
358                     break;\r
359                 }\r
360                 changed = strcmp(optionList[j].textValue, newText) != 0;\r
361                 safeStrCpy(optionList[j].textValue, newText, MSG_SIZ - (optionList[j].textValue - optionList[j].name) );\r
362                 break;\r
363             case CheckBox:\r
364                 new = IsDlgButtonChecked( hDlg, 2000+2*i );\r
365                 if(!cps) { *(Boolean*)optionList[j].target = new; break; }\r
366                 changed = 2*(optionList[j].value != new);\r
367                 optionList[j].value = new;\r
368                 break;\r
369             case ComboBox:\r
370                 choices = (char**) optionList[j].textValue;\r
371                 hwndCombo = GetDlgItem(hDlg, 2001+2*i);\r
372                 success = GetDlgItemText( hDlg, 2001+2*i, newText, MSG_SIZ );\r
373                 if(!success) break;\r
374                 new = -1;\r
375                 for(k=0; k<optionList[j].max; k++) {\r
376                     if(!strcmp(choices[k], newText)) new = k;\r
377                 }\r
378                 if(!cps && new) {\r
379                     if(*(char**)optionList[j].target) free(*(char**)optionList[j].target);\r
380                     *(char**)optionList[j].target = strdup(optionList[j].choice[new]);\r
381                     break;\r
382                 }\r
383                 changed = new >= 0 && (optionList[j].value != new);\r
384                 if(changed) optionList[j].value = new;\r
385                 break;\r
386             case Button:\r
387             default:\r
388                 break; // are treated instantly, so they have been sent already\r
389         }\r
390         if(changed == 2)\r
391           snprintf(buf, MSG_SIZ, "option %s=%d\n", optionList[j].name, new); else\r
392         if(changed == 1)\r
393           snprintf(buf, MSG_SIZ, "option %s=%s\n", optionList[j].name, newText);\r
394         if(changed) SendToProgram(buf, cps);\r
395     }\r
396     if(!cps && okFunc) ((ButtonCallback*) okFunc)(0);\r
397 }\r
398 \r
399 char *defaultExt[] = { NULL, "pgn", "fen", "exe", "trn", "bin", "log", "ini" };\r
400 \r
401 LRESULT CALLBACK SettingsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
402 {\r
403     char buf[MSG_SIZ];\r
404     int i, j, ext;\r
405 \r
406     switch( message )\r
407     {\r
408     case WM_INITDIALOG:\r
409 \r
410 //        CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));\r
411         SetOptionValues(hDlg, activeCps, activeList);\r
412 \r
413 //        SetFocus(GetDlgItem(hDlg, IDC_NFG_Edit));\r
414 \r
415         break;\r
416 \r
417     case WM_COMMAND:\r
418         switch( LOWORD(wParam) ) {\r
419         case IDOK:\r
420             GetOptionValues(hDlg, activeCps, activeList);\r
421             EndDialog( hDlg, 0 );\r
422             comboCallback = NULL; activeCps = NULL;\r
423             return TRUE;\r
424 \r
425         case IDCANCEL:\r
426             EndDialog( hDlg, 1 );\r
427             comboCallback = NULL; activeCps = NULL;\r
428             return TRUE;\r
429 \r
430         default:\r
431             // program-defined push buttons\r
432             i = LOWORD(wParam);\r
433             if( i>=2000 &&  i < 2000+2*(layout+buttons)) {\r
434                 j = layoutList[(i - 2000)/2];\r
435                 if(j == -2) {\r
436                           char filter[] =\r
437                                 "All files\0*.*\0Game files\0*.pgn;*.gam\0Position files\0*.fen;*.epd;*.pos\0"\r
438                                 "EXE files\0*.exe\0Tournament files (*.trn)\0*.trn\0"\r
439                                 "BIN Files\0*.bin\0LOG Files\0*.log\0INI Files\0*.ini\0\0";\r
440                           OPENFILENAME ofn;\r
441 \r
442                           safeStrCpy( buf, "" , sizeof( buf)/sizeof( buf[0]) );\r
443 \r
444                           ZeroMemory( &ofn, sizeof(ofn) );\r
445 \r
446                           ofn.lStructSize = sizeof(ofn);\r
447                           ofn.hwndOwner = hDlg;\r
448                           ofn.hInstance = hInst;\r
449                           ofn.lpstrFilter = filter;\r
450                           ofn.nFilterIndex      = 1L + (ext = activeCps ? 0 : activeList[layoutList[(i-2000)/2+1]].max);\r
451                           ofn.lpstrDefExt       = defaultExt[ext];\r
452                           ofn.lpstrFile = buf;\r
453                           ofn.nMaxFile = sizeof(buf);\r
454                           ofn.lpstrTitle = _("Choose File");\r
455                           ofn.Flags = OFN_FILEMUSTEXIST | OFN_LONGNAMES | OFN_HIDEREADONLY;\r
456 \r
457                           if( GetOpenFileName( &ofn ) ) {\r
458                               SetDlgItemText( hDlg, i+3, buf );\r
459                           }\r
460                 } else\r
461                 if(j == -3) {\r
462                     if( BrowseForFolder( _("Choose Folder:"), buf ) ) {\r
463                         SetDlgItemText( hDlg, i+3, buf );\r
464                     }\r
465                 }\r
466                 if(j < 0) break;\r
467                 if(comboCallback && activeList[j].type == ComboBox && HIWORD(wParam) == CBN_SELCHANGE) {\r
468                     (*comboCallback)(hDlg);\r
469                     break;\r
470                 } else\r
471                 if( activeList[j].type  == SaveButton)\r
472                      GetOptionValues(hDlg, activeCps, activeList);\r
473                 else if( activeList[j].type  != Button) break;\r
474                 snprintf(buf, MSG_SIZ, "option %s\n", activeList[j].name);\r
475                 SendToProgram(buf, activeCps);\r
476             }\r
477             break;\r
478         }\r
479 \r
480         break;\r
481     }\r
482 \r
483     return FALSE;\r
484 }\r
485 \r
486 void AddControl(int x, int y, int w, int h, int type, int style, int n)\r
487 {\r
488     int i;\r
489 \r
490     i = template.header.cdit++;\r
491     template.control[i].item.style = style;\r
492     template.control[i].item.dwExtendedStyle = 0;\r
493     template.control[i].item.x = x;\r
494     template.control[i].item.y = y;\r
495     template.control[i].item.cx = w;\r
496     template.control[i].item.cy = h;\r
497     template.control[i].item.id = 2000 + n;\r
498     template.control[i].code = 0xFFFF;\r
499     template.control[i].controlType = type;\r
500     template.control[i].d1 = ' ';\r
501     template.control[i].data = 0;\r
502     template.control[i].creationData = 0;\r
503 }\r
504 \r
505 void AddOption(int x, int y, Control type, int i)\r
506 {\r
507     int extra;\r
508 \r
509     switch(type) {\r
510         case Slider:\r
511         case Spin:\r
512             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
513             AddControl(x+95, y, 50, 11, 0x0081, ES_AUTOHSCROLL | ES_NUMBER | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
514             break;\r
515         case TextBox:\r
516             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
517             extra = 13*activeList[layoutList[i/2]].min;\r
518             AddControl(x+95, y, 200, 11+extra, 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP | \r
519                                                                 (extra ? ES_MULTILINE | WS_VSCROLL :0), i+1);\r
520             break;\r
521         case Label:\r
522             extra = activeList[layoutList[i/2]].value;\r
523             AddControl(x+extra, y+1, 290-extra, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
524             break;\r
525         case FileName:\r
526         case PathName:\r
527             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
528             AddControl(x+95, y, 180, 11, 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
529             AddControl(x+275, y, 20, 12, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i-2);\r
530             layoutList[i/2-1] = -2 - (type == PathName);\r
531             break;\r
532         case CheckBox:\r
533             AddControl(x, y, 145, 11, 0x0080, BS_AUTOCHECKBOX | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
534             break;\r
535         case ComboBox:\r
536             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
537             AddControl(x+95, y-1, !activeCps && x<10 ? 120 : 50, 500, 0x0085,\r
538                         CBS_AUTOHSCROLL | CBS_DROPDOWN | WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_VSCROLL, i+1);\r
539             break;\r
540         case Button:\r
541         case ResetButton:\r
542         case SaveButton:\r
543             AddControl(x-2, y, 65, 13, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, i);\r
544         case Message:\r
545         default:\r
546             break;\r
547     }\r
548 \r
549 }\r
550 \r
551 void\r
552 CreateDialogTemplate(int *layoutList, int nr, Option *optionList)\r
553 {\r
554     int i, j, x=1, y=0, buttonRows, breakPoint = -1, k=0;\r
555 \r
556     template.header.cdit = 0;\r
557     template.header.cx = 307;\r
558     buttonRows = (buttons + 1 + 3)/4; // 4 per row, rounded up\r
559     if(nr > 50) {\r
560         breakPoint = (nr+2*buttonRows+1)/2 & ~1;\r
561         template.header.cx = 625;\r
562     }\r
563 \r
564     for(i=0; i<nr; i++) {\r
565         if(k < groups && i == boxList[k]) {\r
566             y += 10;\r
567             AddControl(x+2, y+13*(i>>1)-2, 301, 13*(boxList[k+1]-boxList[k]>>1)+8,\r
568                                                 0x0082, WS_VISIBLE | WS_CHILD | SS_BLACKFRAME, 2400);\r
569             AddControl(x+60, y+13*(i>>1)-6, 10*groupNameList[k]/3, 10,\r
570                                                 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, 2*(i+MAX_OPTIONS));\r
571         }\r
572         j = layoutList[i];\r
573         if(j >= 0)\r
574             AddOption(x+155-150*(i&1), y+13*(i>>1)+5, optionList[j].type, 2*i);\r
575         if(k < groups && i+1 == boxList[k+1]) {\r
576             k += 2; y += 4;\r
577         }\r
578         if(i+1 == breakPoint) { x += 318; y = -13*(breakPoint>>1); }\r
579     }\r
580     // add butons at the bottom of dialog window\r
581     y += 13*(nr>>1)+5;\r
582 \r
583     AddControl(x+225, y+18*(buttonRows-1), 30, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, IDOK-2000);\r
584     AddControl(x+260, y+18*(buttonRows-1), 40, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, IDCANCEL-2000);\r
585     for(i=0; i<buttons; i++) {\r
586         AddControl(x+70*(i%4)+5, y+18*(i/4), 65, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 2*(nr+i));\r
587         layoutList[nr+i] = buttonList[i];\r
588     }\r
589     template.title[8] = optionList == first.option ? '1' :  '2';\r
590     template.header.cy = y += 18*buttonRows+2;\r
591     template.header.style &= ~WS_VSCROLL;\r
592 }\r
593 \r
594 void\r
595 EngineOptionsPopup(HWND hwnd, ChessProgramState *cps)\r
596 {\r
597     FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );\r
598 \r
599     activeCps = cps; activeList = cps->option;\r
600     snprintf(title, MSG_SIZ, _("%s Engine Settings (%s)"), T_(cps->which), cps->tidy);\r
601     DesignOptionDialog(cps->nrOptions, cps->option);\r
602     CreateDialogTemplate(layoutList, layout, cps->option);\r
603 \r
604 \r
605     DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );\r
606 \r
607     FreeProcInstance(lpProc);\r
608 \r
609     return;\r
610 }\r
611 \r
612 void InstallOK()\r
613 {\r
614     if(engineChoice[0] == engineNr[0][0])  Load(&first, 0); else Load(&second, 1);\r
615 }\r
616 \r
617 Option installOptions[] = {\r
618   {   0,  0,    0, NULL, (void*) &engineLine, (char*) engineMnemonic, engineList, ComboBox, N_("Select engine from list:") },\r
619   {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("or specify one below:") },\r
620   {   0,  0,    0, NULL, (void*) &nickName, NULL, NULL, TextBox, N_("Nickname (optional):") },\r
621   {   0,  0,    3, NULL, (void*) &engineName, NULL, NULL, FileName, N_("Engine Executable:") },\r
622   {   0,  0,    0, NULL, (void*) &params, NULL, NULL, TextBox, N_("Engine command-line Parameters:") },\r
623   {   0,  0,    0, NULL, (void*) &engineDir, NULL, NULL, PathName, N_("Engine Directory:") },\r
624   {  95,  0,    0, NULL, NULL, NULL, NULL, Label, N_("(Directory will be derived from engine path when empty)") },\r
625   {   0,  0,    0, NULL, (void*) &isUCI, NULL, NULL, CheckBox, N_("UCI") },\r
626   {   0,  0,    0, NULL, (void*) &v1, NULL, NULL, CheckBox, N_("WB protocol v1 (skip waiting for features)") },\r
627   {   0,  0,    0, NULL, (void*) &addToList, NULL, NULL, CheckBox, N_("Add this engine to the list") },\r
628   {   0,  0,    0, NULL, (void*) &hasBook, NULL, NULL, CheckBox, N_("Must not use GUI book") },\r
629   {   0,  0,    0, NULL, (void*) &storeVariant, NULL, NULL, CheckBox, N_("Force current variant with this engine") },\r
630   {   0,  0,    2, NULL, (void*) &engineChoice, (char*) engineNr, engineNr, ComboBox, N_("Load mentioned engine as") },\r
631   {   0,  1,    0, NULL, (void*) &InstallOK, "", NULL, EndMark , "" }\r
632 };\r
633 \r
634 void\r
635 GenericPopup(HWND hwnd, Option *optionList)\r
636 {\r
637     FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );\r
638     int n=0;\r
639 \r
640     while(optionList[n].type != EndMark) n++;\r
641     activeCps = NULL; activeList = optionList;\r
642     DesignOptionDialog(n, optionList);\r
643     CreateDialogTemplate(layoutList, layout, optionList);\r
644 \r
645     DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );\r
646 \r
647     FreeProcInstance(lpProc);\r
648 \r
649     return;\r
650 }\r
651 \r
652 void LoadEnginePopUp(HWND hwnd)\r
653 {\r
654     int n=0;\r
655 \r
656     isUCI = addToList = storeVariant = v1 = FALSE; hasBook = TRUE; // defaults\r
657     if(engineDir)    free(engineDir);    engineDir = strdup("");\r
658     if(params)       free(params);       params = strdup("");\r
659     if(nickName)     free(nickName);     nickName = strdup("");\r
660     if(engineChoice) free(engineChoice); engineChoice = strdup(engineNr[0]);\r
661     if(engineLine)   free(engineLine);   engineLine = strdup("");\r
662     if(engineName)   free(engineName);   engineName = strdup("");\r
663     NamesToList(firstChessProgramNames, engineList, engineMnemonic);\r
664     while(engineList[n]) n++; installOptions[0].max = n;\r
665     snprintf(title, MSG_SIZ, _("Load Engine"));\r
666 \r
667     GenericPopup(hwnd, installOptions);\r
668 }\r
669 \r
670 Boolean autoinc, twice;\r
671 \r
672 void MatchOK()\r
673 {\r
674     if(autoinc) appData.loadGameIndex = appData.loadPositionIndex = -(twice + 1);\r
675     if(appData.participants) free(appData.participants);\r
676     appData.participants = strdup(engineName);\r
677     if(CreateTourney(appData.tourneyFile)) MatchEvent(2); \r
678 //      ScheduleDelayedEvent(MatchEvent(2), 10); // start tourney\r
679 }\r
680 \r
681 Option tourneyOptions[] = {\r
682   { 0,  0,          4, NULL, (void*) &appData.tourneyFile, "", NULL, FileName, N_("Tournament file:") },\r
683   { 0,  1,          0, NULL, (void*) &engineChoice, (char*) (engineMnemonic+1), (engineMnemonic+1), ComboBox, N_("Select Engine:") },\r
684   { 0xD, 7,         0, NULL, (void*) &engineName, "", NULL, TextBox, "Tourney participants:" },\r
685   { 0,  0,         10, NULL, (void*) &appData.tourneyType, "", NULL, Spin, N_("Tourney type (0=RR, 1=gauntlet):") },\r
686   { 0,  0,          0, NULL, (void*) &appData.cycleSync, "", NULL, CheckBox, N_("Sync after cycle") },\r
687   { 0,  1, 1000000000, NULL, (void*) &appData.tourneyCycles, "", NULL, Spin, N_("Number of tourney cycles:") },\r
688   { 0,  0,          0, NULL, (void*) &appData.roundSync, "", NULL, CheckBox, N_("Sync after round") },\r
689   { 0,  1, 1000000000, NULL, (void*) &appData.defaultMatchGames, "", NULL, Spin, N_("Games per Match / Pairing:") },\r
690   { 0,  0,          1, NULL, (void*) &appData.loadGameFile, "", NULL, FileName, N_("Game File with Opening Lines:") },\r
691   { 0, -2, 1000000000, NULL, (void*) &appData.loadGameIndex, "", NULL, Spin, N_("Game Number:") },\r
692   { 0,  0,          2, NULL, (void*) &appData.loadPositionFile, "", NULL, FileName, N_("File with Start Positions:") },\r
693   { 0, -2, 1000000000, NULL, (void*) &appData.loadPositionIndex, "", NULL, Spin, N_("Position Number:") },\r
694   { 0,  0,          0, NULL, (void*) &autoinc, "", NULL, CheckBox, N_("Step through lines/positions in file") },\r
695   { 0,  0, 1000000000, NULL, (void*) &appData.rewindIndex, "", NULL, Spin, N_("Rewind after (0 = never):") },\r
696   { 0,  0,          0, NULL, (void*) &twice, "", NULL, CheckBox, N_("Use each line/position twice") },\r
697   { 0,  0, 1000000000, NULL, (void*) &appData.matchPause, "", NULL, Spin, N_("Pause between Games (ms):") },\r
698   { 0, 0, 0, NULL, (void*) &MatchOK, "", NULL, EndMark , "" }\r
699 };\r
700 \r
701 void AddToTourney(HWND hDlg)\r
702 {\r
703     char buf[MSG_SIZ];\r
704 //    GetDlgItemText( hDlg, 2001+2*3, buf, MSG_SIZ-3 ); // this gives the previous selection !!!\r
705 //    strncat(buf, "\r\n", MSG_SIZ);\r
706     int i = ComboBox_GetCurSel(GetDlgItem(hDlg, 2001+2*3));\r
707     snprintf(buf, MSG_SIZ, "%s\r\n", engineMnemonic[i+1]);\r
708     SendMessage( GetDlgItem(hDlg, 2001+2*5), EM_SETSEL, 0, 0 );\r
709     SendMessage( GetDlgItem(hDlg, 2001+2*5), EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) buf );\r
710 }\r
711 \r
712 void TourneyPopup(HWND hwnd)\r
713 {\r
714     int n=0;\r
715 \r
716     NamesToList(firstChessProgramNames, engineList, engineMnemonic);\r
717     comboCallback = &AddToTourney;\r
718     autoinc = appData.loadGameIndex < 0 || appData.loadPositionIndex < 0;\r
719     twice = TRUE;\r
720     while(engineList[n]) n++; tourneyOptions[1].max = n-1;\r
721     snprintf(title, MSG_SIZ, _("Tournament and Match Options"));\r
722 \r
723     GenericPopup(hwnd, tourneyOptions);\r
724 }\r