Implement Load Engine dialog 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 <stdio.h>\r
16 #include <string.h>\r
17 #include "common.h"\r
18 #include "frontend.h"\r
19 #include "backend.h"\r
20 #include "winboard.h"\r
21 #include "backendz.h"\r
22 \r
23 #define _(s) T_(s)\r
24 #define N_(s) s\r
25 \r
26 int layoutList[2*MAX_OPTIONS];\r
27 int checkList[2*MAX_OPTIONS];\r
28 int comboList[2*MAX_OPTIONS];\r
29 int buttonList[2*MAX_OPTIONS];\r
30 int boxList[2*MAX_OPTIONS];\r
31 int groupNameList[2*MAX_OPTIONS];\r
32 int breaks[MAX_OPTIONS];\r
33 int checks, combos, buttons, layout, groups;\r
34 char title[MSG_SIZ];\r
35 char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params;\r
36 Boolean isUCI, hasBook, storeVariant, v1, addToList;\r
37 extern Option installOptions[], matchOptions[];\r
38 char *engineNr[] = { N_("First"), N_("Second"), NULL };\r
39 char *engineList[1000] = {" "}, *engineMnemonic[1000] = {""};\r
40 void (*okFunc)();\r
41 ChessProgramState *activeCps;\r
42 Option *activeList;\r
43 void InstallOK P((void));\r
44 typedef void ButtonCallback(HWND h);\r
45 \r
46 void\r
47 PrintOpt(int i, int right, Option *optionList)\r
48 {\r
49     if(i<0) {\r
50         if(!right) fprintf(debugFP, "%30s", "");\r
51     } else {\r
52         Option opt = optionList[i];\r
53         switch(opt.type) {\r
54             case Slider:\r
55             case Spin:\r
56                 fprintf(debugFP, "%20.20s [    +/-]", opt.name);\r
57                 break;\r
58             case TextBox:\r
59             case FileName:\r
60             case PathName:\r
61                 fprintf(debugFP, "%20.20s [______________________________________]", opt.name);\r
62                 break;\r
63             case Label:\r
64                 fprintf(debugFP, "%41.41s", opt.name);\r
65                 break;\r
66             case CheckBox:\r
67                 fprintf(debugFP, "[x] %-26.25s", opt.name);\r
68                 break;\r
69             case ComboBox:\r
70                 fprintf(debugFP, "%20.20s [ COMBO ]", opt.name);\r
71                 break;\r
72             case Button:\r
73             case SaveButton:\r
74             case ResetButton:\r
75                 fprintf(debugFP, "[ %26.26s ]", opt.name);\r
76             case Message:\r
77             default:\r
78                 break;\r
79         }\r
80     }\r
81     fprintf(debugFP, right ? "\n" : " ");\r
82 }\r
83 \r
84 void\r
85 CreateOptionDialogTest(int *list, int nr, Option *optionList)\r
86 {\r
87     int line;\r
88 \r
89     for(line = 0; line < nr; line+=2) {\r
90         PrintOpt(list[line+1], 0, optionList);\r
91         PrintOpt(list[line], 1, optionList);\r
92     }\r
93 }\r
94 \r
95 void\r
96 LayoutOptions(int firstOption, int endOption, char *groupName, Option *optionList)\r
97 {\r
98     int i, b = strlen(groupName), stop, prefix, right, nextOption, firstButton = buttons;\r
99     Control lastType, nextType;\r
100 \r
101     nextOption = firstOption;\r
102     while(nextOption < endOption) {\r
103         checks = combos = 0; stop = 0;\r
104         lastType = Button; // kludge to make sure leading Spin will not be prefixed\r
105         // first separate out buttons for later treatment, and collect consecutive checks and combos\r
106         while(nextOption < endOption && !stop) {\r
107             switch(nextType = optionList[nextOption].type) {\r
108                 case CheckBox: checkList[checks++] = nextOption; lastType = CheckBox; break;\r
109                 case ComboBox: comboList[combos++] = nextOption; lastType = ComboBox; break;\r
110                 case ResetButton:\r
111                 case SaveButton:\r
112                 case Button:  buttonList[buttons++] = nextOption; lastType = Button; break;\r
113                 case TextBox:\r
114                 case FileName:\r
115                 case PathName:\r
116                 case Slider:\r
117                 case Label:\r
118                 case Spin: stop++;\r
119                 default:\r
120                 case Message: ; // cannot happen\r
121             }\r
122             nextOption++;\r
123         }\r
124         // We now must be at the end, or looking at a spin or textbox (in nextType)\r
125         if(!stop)\r
126             nextType = Button; // kudge to flush remaining checks and combos undistorted\r
127         // Take a new line if a spin follows combos or checks, or when we encounter a textbox\r
128         if((combos+checks || nextType == TextBox || nextType == FileName || nextType == PathName || nextType == Label) && layout&1) {\r
129             layoutList[layout++] = -1;\r
130         }\r
131         // The last check or combo before a spin will be put on the same line as that spin (prefix)\r
132         // and will thus not be grouped with other checks and combos\r
133         prefix = -1;\r
134         if(nextType == Spin && lastType != Button) {\r
135             if(lastType == CheckBox) prefix = checkList[--checks]; else\r
136             if(lastType == ComboBox) prefix = comboList[--combos];\r
137         }\r
138         // if a combo is followed by a textbox, it must stay at the end of the combo/checks list to appear\r
139         // immediately above the textbox, so treat it as check. (A check would automatically be and remain there.)\r
140         if((nextType == TextBox || nextType == FileName || nextType == PathName) && lastType == ComboBox)\r
141             checkList[checks++] = comboList[--combos];\r
142         // Now append the checks behind the (remaining) combos to treat them as one group\r
143         for(i=0; i< checks; i++)\r
144             comboList[combos++] = checkList[i];\r
145         // emit the consecutive checks and combos in two columns\r
146         right = combos/2; // rounded down if odd!\r
147         for(i=0; i<right; i++) {\r
148             breaks[layout/2] = 2;\r
149             layoutList[layout++] = comboList[i];\r
150             layoutList[layout++] = comboList[i + right];\r
151         }\r
152         // An odd check or combo (which could belong to following textBox) will be put in the left column\r
153         // If there was an even number of checks and combos the last one will automatically be in that position\r
154         if(combos&1) {\r
155             layoutList[layout++] = -1;\r
156             layoutList[layout++] = comboList[2*right];\r
157         }\r
158         if(nextType == TextBox || nextType == FileName || nextType == PathName || nextType == Label) {\r
159             // A textBox is double width, so must be left-adjusted, and the right column remains empty\r
160             breaks[layout/2] = lastType == Button ? 0 : 100;\r
161             layoutList[layout++] = -1;\r
162             layoutList[layout++] = nextOption - 1;\r
163         } else if(nextType == Spin) {\r
164             // A spin will go in the next available position (right to left!). If it had to be prefixed with\r
165             // a check or combo, this next position must be to the right, and the prefix goes left to it.\r
166             layoutList[layout++] = nextOption - 1;\r
167             if(prefix >= 0) layoutList[layout++] = prefix;\r
168         }\r
169     }\r
170     // take a new line if needed\r
171     if(layout&1) layoutList[layout++] = -1;\r
172     // emit the buttons belonging in this group; loose buttons are saved for last, to appear at bottom of dialog\r
173     if(b) {\r
174         while(buttons > firstButton)\r
175             layoutList[layout++] = buttonList[--buttons];\r
176         if(layout&1) layoutList[layout++] = -1;\r
177     }\r
178 }\r
179 \r
180 char *\r
181 EndMatch(char *s1, char *s2)\r
182 {\r
183         char *p, *q;\r
184         p = s1; while(*p) p++;\r
185         q = s2; while(*q) q++;\r
186         while(p > s1 && q > s2 && *p == *q) { p--; q--; }\r
187         if(p[1] == 0) return NULL;\r
188         return p+1;\r
189 }\r
190 \r
191 void\r
192 DesignOptionDialog(int nrOpt, Option *optionList)\r
193 {\r
194     int k=0, n=0;\r
195     char buf[MSG_SIZ];\r
196 \r
197     layout = 0;\r
198     buttons = groups = 0;\r
199     while(k < nrOpt) { // k steps through 'solitary' options\r
200         // look if we hit a group of options having names that start with the same word\r
201         int groupSize = 1, groupNameLength = 50;\r
202         sscanf(optionList[k].name, "%s", buf); // get first word of option name\r
203         while(k + groupSize < nrOpt &&\r
204               strstr(optionList[k+groupSize].name, buf) == optionList[k+groupSize].name) {\r
205                 int j;\r
206                 for(j=0; j<groupNameLength; j++) // determine common initial part of option names\r
207                     if( optionList[k].name[j] != optionList[k+groupSize].name[j]) break;\r
208                 groupNameLength = j;\r
209                 groupSize++;\r
210 \r
211         }\r
212         if(groupSize > 3) {\r
213                 // We found a group to terminates the current section\r
214                 LayoutOptions(n, k, "", optionList); // flush all solitary options appearing before the group\r
215                 groupNameList[groups] = groupNameLength;\r
216                 boxList[groups++] = layout; // group start in even entries\r
217                 LayoutOptions(k, k+groupSize, buf, optionList); // flush the group\r
218                 boxList[groups++] = layout; // group end in odd entries\r
219                 k = n = k + groupSize;\r
220         } else k += groupSize; // small groups are grouped with the solitary options\r
221     }\r
222     if(n != k) LayoutOptions(n, k, "", optionList); // flush remaining solitary options\r
223     // decide if and where we break into two column pairs\r
224 \r
225     // Emit buttons and add OK and cancel\r
226 //    for(k=0; k<buttons; k++) layoutList[layout++] = buttonList[k];\r
227  \r
228    // Create the dialog window\r
229     if(appData.debugMode) CreateOptionDialogTest(layoutList, layout, optionList);\r
230 //    CreateOptionDialog(layoutList, layout, optionList);\r
231     if(!activeCps) okFunc = optionList[nrOpt].target;\r
232 }\r
233 \r
234 #include <windows.h>\r
235 \r
236 extern HINSTANCE hInst;\r
237 \r
238 typedef struct {\r
239     DLGITEMTEMPLATE item;\r
240     WORD code;\r
241     WORD controlType;\r
242     wchar_t d1, data;\r
243     WORD creationData;\r
244 } Item;\r
245 \r
246 struct {\r
247     DLGTEMPLATE header;\r
248     WORD menu;\r
249     WORD winClass;\r
250     wchar_t title[20];\r
251     WORD pointSize;\r
252     wchar_t fontName[14];\r
253     Item control[MAX_OPTIONS];\r
254 } template = {\r
255     { DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_SETFONT, 0, 0, 0, 0, 295, 300 },\r
256     0x0000, 0x0000, L"Engine #1 Settings ", 8, L"MS Sans Serif"\r
257 };\r
258 \r
259 void\r
260 SetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)\r
261 // Put all current option values in controls, and write option names next to them\r
262 {\r
263     HANDLE hwndCombo;\r
264     int i, k;\r
265     char **choices, *name;\r
266 \r
267     for(i=0; i<layout+buttons; i++) {\r
268         int j=layoutList[i];\r
269         if(j == -2) SetDlgItemText( hDlg, 2000+2*i, ". . ." );\r
270         if(j<0) continue;\r
271         name = optionList[j].name;\r
272         if(strstr(name, "Polyglot ") == name) name += 9;\r
273         SetDlgItemText( hDlg, 2000+2*i, name );\r
274 //if(appData.debugMode) fprintf(debugFP, "# %s = %d\n",optionList[j].name, optionList[j].value );\r
275         switch(optionList[j].type) {\r
276             case Spin:\r
277                 SetDlgItemInt( hDlg, 2001+2*i, cps ? optionList[j].value : *(int*)optionList[j].target, TRUE );\r
278                 break;\r
279             case TextBox:\r
280             case FileName:\r
281             case PathName:\r
282                 SetDlgItemText( hDlg, 2001+2*i, cps ? optionList[j].textValue : *(char**)optionList[j].target );\r
283                 break;\r
284             case CheckBox:\r
285                 CheckDlgButton( hDlg, 2000+2*i, (cps ? optionList[j].value : *(Boolean*)optionList[j].target) != 0);\r
286                 break;\r
287             case ComboBox:\r
288                 choices = (char**) optionList[j].textValue;\r
289                 hwndCombo = GetDlgItem(hDlg, 2001+2*i);\r
290                 SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);\r
291                 for(k=0; k<optionList[j].max; k++) {\r
292                     SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) choices[k]);\r
293                 }\r
294                 SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) choices[optionList[j].value]);\r
295                 break;\r
296             case Button:\r
297             case SaveButton:\r
298             default:\r
299                 break;\r
300         }\r
301     }\r
302     SetDlgItemText( hDlg, IDOK, _("OK") );\r
303     SetDlgItemText( hDlg, IDCANCEL, _("Cancel") );\r
304     title[0] &= ~32; // capitalize\r
305     SetWindowText( hDlg, title);\r
306     for(i=0; i<groups; i+=2) {\r
307         int id, p; char buf[MSG_SIZ];\r
308         id = k = boxList[i];\r
309         if(layoutList[k] < 0) k++;\r
310         if(layoutList[k] < 0) continue;\r
311         for(p=0; p<groupNameList[i]; p++) buf[p] = optionList[layoutList[k]].name[p];\r
312         buf[p] = 0;\r
313         SetDlgItemText( hDlg, 2000+2*(id+MAX_OPTIONS), buf );\r
314     }\r
315 }\r
316 \r
317 \r
318 void\r
319 GetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)\r
320 // read out all controls, and if value is altered, remember it and send it to the engine\r
321 {\r
322     HANDLE hwndCombo;\r
323     int i, k, new=0, changed=0;\r
324     char **choices, newText[MSG_SIZ], buf[MSG_SIZ];\r
325     BOOL success;\r
326 \r
327     for(i=0; i<layout; i++) {\r
328         int j=layoutList[i];\r
329         if(j<0) continue;\r
330         switch(optionList[j].type) {\r
331             case Spin:\r
332                 new = GetDlgItemInt( hDlg, 2001+2*i, &success, TRUE );\r
333                 if(!success) break;\r
334                 if(new < optionList[j].min) new = optionList[j].min;\r
335                 if(new > optionList[j].max) new = optionList[j].max;\r
336                 if(!cps) { *(int*)optionList[j].target = new; break; }\r
337                 changed = 2*(optionList[j].value != new);\r
338                 optionList[j].value = new;\r
339                 break;\r
340             case TextBox:\r
341             case FileName:\r
342             case PathName:\r
343                 success = GetDlgItemText( hDlg, 2001+2*i, newText, MSG_SIZ - strlen(optionList[j].name) - 9 );\r
344                 if(!success) break;\r
345                 if(!cps) {\r
346                     if(*(char**)optionList[j].target) free(*(char**)optionList[j].target);\r
347                     *(char**)optionList[j].target = strdup(newText);\r
348                     break;\r
349                 }\r
350                 changed = strcmp(optionList[j].textValue, newText) != 0;\r
351                 safeStrCpy(optionList[j].textValue, newText, MSG_SIZ - (optionList[j].textValue - optionList[j].name) );\r
352                 break;\r
353             case CheckBox:\r
354                 new = IsDlgButtonChecked( hDlg, 2000+2*i );\r
355                 if(!cps) { *(Boolean*)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 ComboBox:\r
360                 choices = (char**) optionList[j].textValue;\r
361                 hwndCombo = GetDlgItem(hDlg, 2001+2*i);\r
362                 success = GetDlgItemText( hDlg, 2001+2*i, newText, MSG_SIZ );\r
363                 if(!success) break;\r
364                 new = -1;\r
365                 for(k=0; k<optionList[j].max; k++) {\r
366                     if(!strcmp(choices[k], newText)) new = k;\r
367                 }\r
368                 if(!cps && new) {\r
369                     if(*(char**)optionList[j].target) free(*(char**)optionList[j].target);\r
370                     *(char**)optionList[j].target = strdup(optionList[j].choice[new]);\r
371                     break;\r
372                 }\r
373                 changed = new >= 0 && (optionList[j].value != new);\r
374                 if(changed) optionList[j].value = new;\r
375                 break;\r
376             case Button:\r
377             default:\r
378                 break; // are treated instantly, so they have been sent already\r
379         }\r
380         if(changed == 2)\r
381           snprintf(buf, MSG_SIZ, "option %s=%d\n", optionList[j].name, new); else\r
382         if(changed == 1)\r
383           snprintf(buf, MSG_SIZ, "option %s=%s\n", optionList[j].name, newText);\r
384         if(changed) SendToProgram(buf, cps);\r
385     }\r
386     if(!cps && okFunc) ((ButtonCallback*) okFunc)(0);\r
387 }\r
388 \r
389 LRESULT CALLBACK SettingsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)\r
390 {\r
391     char buf[MSG_SIZ];\r
392     int i, j;\r
393 \r
394     switch( message )\r
395     {\r
396     case WM_INITDIALOG:\r
397 \r
398 //        CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));\r
399         SetOptionValues(hDlg, activeCps, activeList);\r
400 \r
401 //        SetFocus(GetDlgItem(hDlg, IDC_NFG_Edit));\r
402 \r
403         break;\r
404 \r
405     case WM_COMMAND:\r
406         switch( LOWORD(wParam) ) {\r
407         case IDOK:\r
408             GetOptionValues(hDlg, activeCps, activeList);\r
409             EndDialog( hDlg, 0 );\r
410             activeCps = NULL;\r
411             return TRUE;\r
412 \r
413         case IDCANCEL:\r
414             EndDialog( hDlg, 1 );\r
415             activeCps = NULL;\r
416             return TRUE;\r
417 \r
418         default:\r
419             // program-defined push buttons\r
420             i = LOWORD(wParam);\r
421             if( i>=2000 &&  i < 2000+2*(layout+buttons)) {\r
422                 j = layoutList[(i - 2000)/2];\r
423                 if(j == -2) {\r
424                           char filter[] =\r
425                                 "All files\0*.*\0BIN Files\0*.bin\0LOG Files\0*.log\0INI Files\0*.ini\0\0";\r
426 /*\r
427 {\r
428                               'A','l','l',' ','F','i','l','e','s', 0,\r
429                               '*','.','*', 0,\r
430                               'B','I','N',' ','F','i','l','e','s', 0,\r
431                               '*','.','b','i','n', 0,\r
432                               0 };\r
433 */\r
434                           OPENFILENAME ofn;\r
435 \r
436                           safeStrCpy( buf, "" , sizeof( buf)/sizeof( buf[0]) );\r
437 \r
438                           ZeroMemory( &ofn, sizeof(ofn) );\r
439 \r
440                           ofn.lStructSize = sizeof(ofn);\r
441                           ofn.hwndOwner = hDlg;\r
442                           ofn.hInstance = hInst;\r
443                           ofn.lpstrFilter = filter;\r
444                           ofn.lpstrFile = buf;\r
445                           ofn.nMaxFile = sizeof(buf);\r
446                           ofn.lpstrTitle = _("Choose File");\r
447                           ofn.Flags = OFN_FILEMUSTEXIST | OFN_LONGNAMES | OFN_HIDEREADONLY;\r
448 \r
449                           if( GetOpenFileName( &ofn ) ) {\r
450                               SetDlgItemText( hDlg, i+3, buf );\r
451                           }\r
452                 } else\r
453                 if(j == -3) {\r
454                     if( BrowseForFolder( _("Choose Folder:"), buf ) ) {\r
455                         SetDlgItemText( hDlg, i+3, buf );\r
456                     }\r
457                 }\r
458                 if(j < 0) break;\r
459                 if( activeList[j].type  == SaveButton)\r
460                      GetOptionValues(hDlg, activeCps, activeList);\r
461                 else if( activeList[j].type  != Button) break;\r
462                 snprintf(buf, MSG_SIZ, "option %s\n", activeList[j].name);\r
463                 SendToProgram(buf, activeCps);\r
464             }\r
465             break;\r
466         }\r
467 \r
468         break;\r
469     }\r
470 \r
471     return FALSE;\r
472 }\r
473 \r
474 void AddControl(int x, int y, int w, int h, int type, int style, int n)\r
475 {\r
476     int i;\r
477 \r
478     i = template.header.cdit++;\r
479     template.control[i].item.style = style;\r
480     template.control[i].item.dwExtendedStyle = 0;\r
481     template.control[i].item.x = x;\r
482     template.control[i].item.y = y;\r
483     template.control[i].item.cx = w;\r
484     template.control[i].item.cy = h;\r
485     template.control[i].item.id = 2000 + n;\r
486     template.control[i].code = 0xFFFF;\r
487     template.control[i].controlType = type;\r
488     template.control[i].d1 = ' ';\r
489     template.control[i].data = 0;\r
490     template.control[i].creationData = 0;\r
491 }\r
492 \r
493 void AddOption(int x, int y, Control type, int i)\r
494 {\r
495 \r
496     switch(type) {\r
497         case Slider:\r
498         case Spin:\r
499             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
500             AddControl(x+95, y, 50, 11, 0x0081, ES_AUTOHSCROLL | ES_NUMBER | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
501             break;\r
502         case TextBox:\r
503             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
504             AddControl(x+95, y, 200, 11, 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
505             break;\r
506         case Label:\r
507             extra = activeList[layoutList[i/2]].value;\r
508             AddControl(x+extra, y+1, 290-extra, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
509             break;\r
510         case FileName:\r
511         case PathName:\r
512             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
513             AddControl(x+95, y, 180, 11, 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);\r
514             AddControl(x+275, y, 20, 12, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i-2);\r
515             layoutList[i/2-1] = -2 - (type == PathName);\r
516             break;\r
517         case CheckBox:\r
518             AddControl(x, y, 145, 11, 0x0080, BS_AUTOCHECKBOX | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
519             break;\r
520         case ComboBox:\r
521             AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
522             AddControl(x+95, y-1, !activeCps && x<10 ? 120 : 50, 500, 0x0085,\r
523                         CBS_AUTOHSCROLL | CBS_DROPDOWN | WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_VSCROLL, i+1);\r
524             break;\r
525         case Button:\r
526         case ResetButton:\r
527         case SaveButton:\r
528             AddControl(x-2, y, 65, 13, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, i);\r
529         case Message:\r
530         default:\r
531             break;\r
532     }\r
533 \r
534 }\r
535 \r
536 void\r
537 CreateDialogTemplate(int *layoutList, int nr, Option *optionList)\r
538 {\r
539     int i, j, x=1, y=0, buttonRows, breakPoint = -1, k=0;\r
540 \r
541     template.header.cdit = 0;\r
542     template.header.cx = 307;\r
543     buttonRows = (buttons + 1 + 3)/4; // 4 per row, rounded up\r
544     if(nr > 50) {\r
545         breakPoint = (nr+2*buttonRows+1)/2 & ~1;\r
546         template.header.cx = 625;\r
547     }\r
548 \r
549     for(i=0; i<nr; i++) {\r
550         if(k < groups && i == boxList[k]) {\r
551             y += 10;\r
552             AddControl(x+2, y+13*(i>>1)-2, 301, 13*(boxList[k+1]-boxList[k]>>1)+8,\r
553                                                 0x0082, WS_VISIBLE | WS_CHILD | SS_BLACKFRAME, 2400);\r
554             AddControl(x+60, y+13*(i>>1)-6, 10*groupNameList[k]/3, 10,\r
555                                                 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, 2*(i+MAX_OPTIONS));\r
556         }\r
557         j = layoutList[i];\r
558         if(j >= 0)\r
559             AddOption(x+155-150*(i&1), y+13*(i>>1)+5, optionList[j].type, 2*i);\r
560         if(k < groups && i+1 == boxList[k+1]) {\r
561             k += 2; y += 4;\r
562         }\r
563         if(i+1 == breakPoint) { x += 318; y = -13*(breakPoint>>1); }\r
564     }\r
565     // add butons at the bottom of dialog window\r
566     y += 13*(nr>>1)+5;\r
567 \r
568     AddControl(x+225, y+18*(buttonRows-1), 30, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, IDOK-2000);\r
569     AddControl(x+260, y+18*(buttonRows-1), 40, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, IDCANCEL-2000);\r
570     for(i=0; i<buttons; i++) {\r
571         AddControl(x+70*(i%4)+5, y+18*(i/4), 65, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 2*(nr+i));\r
572         layoutList[nr+i] = buttonList[i];\r
573     }\r
574     template.title[8] = optionList == first.option ? '1' :  '2';\r
575     template.header.cy = y += 18*buttonRows+2;\r
576     template.header.style &= ~WS_VSCROLL;\r
577 }\r
578 \r
579 void\r
580 EngineOptionsPopup(HWND hwnd, ChessProgramState *cps)\r
581 {\r
582     FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );\r
583 \r
584     activeCps = cps; activeList = cps->option;\r
585     snprintf(title, MSG_SIZ, _("%s Engine Settings (%s)"), T_(cps->which), cps->tidy);\r
586     DesignOptionDialog(cps->nrOptions, cps->option);\r
587     CreateDialogTemplate(layoutList, layout, cps->option);\r
588 \r
589 \r
590     DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );\r
591 \r
592     FreeProcInstance(lpProc);\r
593 \r
594     return;\r
595 }\r
596 \r
597 void InstallOK()\r
598 {\r
599     if(engineChoice[0] == engineNr[0][0])  Load(&first, 0); else Load(&second, 1);\r
600 }\r
601 \r
602 Option installOptions[] = {\r
603   {   0,  0,    0, NULL, (void*) &engineLine, (char*) engineMnemonic, engineList, ComboBox, N_("Select engine from list:") },\r
604   {   0,  0,    0, NULL, NULL, NULL, NULL, Label, N_("or specify one below:") },\r
605   {   0,  0,    0, NULL, (void*) &nickName, NULL, NULL, TextBox, N_("Nickname (optional):") },\r
606   {   0,  0,    3, NULL, (void*) &engineName, NULL, NULL, FileName, N_("Engine Executable:") },\r
607   {   0,  0,    0, NULL, (void*) &params, NULL, NULL, TextBox, N_("Engine command-line Parameters:") },\r
608   {   0,  0,    0, NULL, (void*) &engineDir, NULL, NULL, PathName, N_("Engine Directory:") },\r
609   {  95,  0,    0, NULL, NULL, NULL, NULL, Label, N_("(Directory will be derived from engine path when empty)") },\r
610   {   0,  0,    0, NULL, (void*) &isUCI, NULL, NULL, CheckBox, N_("UCI") },\r
611   {   0,  0,    0, NULL, (void*) &v1, NULL, NULL, CheckBox, N_("WB protocol v1 (skip waiting for features)") },\r
612   {   0,  0,    0, NULL, (void*) &addToList, NULL, NULL, CheckBox, N_("Add this engine to the list") },\r
613   {   0,  0,    0, NULL, (void*) &hasBook, NULL, NULL, CheckBox, N_("Must not use GUI book") },\r
614   {   0,  0,    0, NULL, (void*) &storeVariant, NULL, NULL, CheckBox, N_("Force current variant with this engine") },\r
615   {   0,  0,    2, NULL, (void*) &engineChoice, (char*) engineNr, engineNr, ComboBox, N_("Load mentioned engine as") },\r
616   {   0,  1,    0, NULL, (void*) &InstallOK, "", NULL, EndMark , "" }\r
617 };\r
618 \r
619 void\r
620 GenericPopup(HWND hwnd, Option *optionList)\r
621 {\r
622     FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );\r
623     int n=0;\r
624 \r
625     while(optionList[n].type != EndMark) n++;\r
626     activeCps = NULL; activeList = optionList;\r
627     DesignOptionDialog(n, optionList);\r
628     CreateDialogTemplate(layoutList, layout, optionList);\r
629 \r
630     DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );\r
631 \r
632     FreeProcInstance(lpProc);\r
633 \r
634     return;\r
635 }\r
636 \r
637 void LoadEnginePopUp(HWND hwnd)\r
638 {\r
639     int n=0;\r
640 \r
641     isUCI = addToList = storeVariant = v1 = FALSE; hasBook = TRUE; // defaults\r
642     if(engineDir)    free(engineDir);    engineDir = strdup("");\r
643     if(params)       free(params);       params = strdup("");\r
644     if(nickName)     free(nickName);     nickName = strdup("");\r
645     if(engineChoice) free(engineChoice); engineChoice = strdup(engineNr[0]);\r
646     if(engineLine)   free(engineLine);   engineLine = strdup("");\r
647     NamesToList(firstChessProgramNames, engineList, engineMnemonic);\r
648     while(engineList[n]) n++; installOptions[0].max = n;\r
649     snprintf(title, MSG_SIZ, _("Load Engine"));\r
650 \r
651     GenericPopup(hwnd, installOptions);\r
652 }\r