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