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
14 #include <windows.h>
\r
18 #include "backend.h"
\r
19 #include "backendz.h"
\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
31 PrintOpt(int i, int right, ChessProgramState *cps)
\r
34 if(!right) fprintf(debugFP, "%30s", "");
\r
36 Option opt = cps->option[i];
\r
40 fprintf(debugFP, "%20.20s [ +/-]", opt.name);
\r
45 fprintf(debugFP, "%20.20s [______________________________________]", opt.name);
\r
48 fprintf(debugFP, "[x] %-26.25s", opt.name);
\r
51 fprintf(debugFP, "%20.20s [ COMBO ]", opt.name);
\r
56 fprintf(debugFP, "[ %26.26s ]", opt.name);
\r
61 fprintf(debugFP, right ? "\n" : " ");
\r
65 CreateOptionDialogTest(int *list, int nr, ChessProgramState *cps)
\r
69 for(line = 0; line < nr; line+=2) {
\r
70 PrintOpt(list[line+1], 0, cps);
\r
71 PrintOpt(list[line], 1, cps);
\r
76 LayoutOptions(int firstOption, int endOption, char *groupName, Option *optionList)
\r
78 int i, b = strlen(groupName), stop, prefix, right, nextOption, firstButton = buttons;
\r
79 Control lastType, nextType;
\r
81 nextOption = firstOption;
\r
82 while(nextOption < endOption) {
\r
83 checks = combos = 0; stop = 0;
\r
84 lastType = Button; // kludge to make sure leading Spin will not be prefixed
\r
85 // first separate out buttons for later treatment, and collect consecutive checks and combos
\r
86 while(nextOption < endOption && !stop) {
\r
87 switch(nextType = optionList[nextOption].type) {
\r
88 case CheckBox: checkList[checks++] = nextOption; lastType = CheckBox; break;
\r
89 case ComboBox: comboList[combos++] = nextOption; lastType = ComboBox; break;
\r
92 case Button: buttonList[buttons++] = nextOption; lastType = Button; break;
\r
98 case Message: ; // cannot happen
\r
102 // We now must be at the end, or looking at a spin or textbox (in nextType)
\r
104 nextType = Button; // kudge to flush remaining checks and combos undistorted
\r
105 // Take a new line if a spin follows combos or checks, or when we encounter a textbox
\r
106 if((combos+checks || nextType == TextBox) && layout&1) {
\r
107 layoutList[layout++] = -1;
\r
109 // The last check or combo before a spin will be put on the same line as that spin (prefix)
\r
110 // and will thus not be grouped with other checks and combos
\r
112 if(nextType == Spin && lastType != Button) {
\r
113 if(lastType == CheckBox) prefix = checkList[--checks]; else
\r
114 if(lastType == ComboBox) prefix = comboList[--combos];
\r
116 // if a combo is followed by a textbox, it must stay at the end of the combo/checks list to appear
\r
117 // immediately above the textbox, so treat it as check. (A check would automatically be and remain there.)
\r
118 if(nextType == TextBox && lastType == ComboBox)
\r
119 checkList[checks++] = comboList[--combos];
\r
120 // Now append the checks behind the (remaining) combos to treat them as one group
\r
121 for(i=0; i< checks; i++)
\r
122 comboList[combos++] = checkList[i];
\r
123 // emit the consecutive checks and combos in two columns
\r
124 right = combos/2; // rounded down if odd!
\r
125 for(i=0; i<right; i++) {
\r
126 breaks[layout/2] = 2;
\r
127 layoutList[layout++] = comboList[i];
\r
128 layoutList[layout++] = comboList[i + right];
\r
130 // An odd check or combo (which could belong to following textBox) will be put in the left column
\r
131 // If there was an even number of checks and combos the last one will automatically be in that position
\r
133 layoutList[layout++] = -1;
\r
134 layoutList[layout++] = comboList[2*right];
\r
136 if(nextType == TextBox) {
\r
137 // A textBox is double width, so must be left-adjusted, and the right column remains empty
\r
138 breaks[layout/2] = lastType == Button ? 0 : 100;
\r
139 layoutList[layout++] = -1;
\r
140 layoutList[layout++] = nextOption - 1;
\r
141 } else if(nextType == Spin) {
\r
142 // A spin will go in the next available position (right to left!). If it had to be prefixed with
\r
143 // a check or combo, this next position must be to the right, and the prefix goes left to it.
\r
144 layoutList[layout++] = nextOption - 1;
\r
145 if(prefix >= 0) layoutList[layout++] = prefix;
\r
148 // take a new line if needed
\r
149 if(layout&1) layoutList[layout++] = -1;
\r
150 // emit the buttons belonging in this group; loose buttons are saved for last, to appear at bottom of dialog
\r
152 while(buttons > firstButton)
\r
153 layoutList[layout++] = buttonList[--buttons];
\r
154 if(layout&1) layoutList[layout++] = -1;
\r
159 EndMatch(char *s1, char *s2)
\r
162 p = s1; while(*p) p++;
\r
163 q = s2; while(*q) q++;
\r
164 while(p > s1 && q > s2 && *p == *q) { p--; q--; }
\r
165 if(p[1] == 0) return NULL;
\r
170 DesignOptionDialog(ChessProgramState *cps)
\r
176 buttons = groups = 0;
\r
177 while(k < cps->nrOptions) { // k steps through 'solitary' options
\r
178 // look if we hit a group of options having names that start with the same word
\r
179 int groupSize = 1, groupNameLength = 50;
\r
180 sscanf(cps->option[k].name, "%s", buf); // get first word of option name
\r
181 while(k + groupSize < cps->nrOptions &&
\r
182 strstr(cps->option[k+groupSize].name, buf) == cps->option[k+groupSize].name) {
\r
184 for(j=0; j<groupNameLength; j++) // determine common initial part of option names
\r
185 if( cps->option[k].name[j] != cps->option[k+groupSize].name[j]) break;
\r
186 groupNameLength = j;
\r
190 if(groupSize > 3) {
\r
191 // We found a group to terminates the current section
\r
192 LayoutOptions(n, k, "", cps->option); // flush all solitary options appearing before the group
\r
193 groupNameList[groups] = groupNameLength;
\r
194 boxList[groups++] = layout; // group start in even entries
\r
195 LayoutOptions(k, k+groupSize, buf, cps->option); // flush the group
\r
196 boxList[groups++] = layout; // group end in odd entries
\r
197 k = n = k + groupSize;
\r
198 } else k += groupSize; // small groups are grouped with the solitary options
\r
200 if(n != k) LayoutOptions(n, k, "", cps->option); // flush remaining solitary options
\r
201 // decide if and where we break into two column pairs
\r
203 // Emit buttons and add OK and cancel
\r
204 // for(k=0; k<buttons; k++) layoutList[layout++] = buttonList[k];
\r
205 // Create the dialog window
\r
206 if(appData.debugMode) CreateOptionDialogTest(layoutList, layout, cps);
\r
207 // CreateOptionDialog(layoutList, layout, cps);
\r
210 #include <windows.h>
\r
212 extern HINSTANCE hInst;
\r
215 DLGITEMTEMPLATE item;
\r
223 DLGTEMPLATE header;
\r
228 wchar_t fontName[14];
\r
229 Item control[MAX_OPTIONS];
\r
231 { DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_SETFONT, 0, 0, 0, 0, 295, 300 },
\r
232 0x0000, 0x0000, L"Engine #1 Settings ", 8, L"MS Sans Serif"
\r
235 ChessProgramState *activeCps;
\r
238 SetOptionValues(HWND hDlg, ChessProgramState *cps)
\r
239 // Put all current option values in controls, and write option names next to them
\r
243 char **choices, title[MSG_SIZ], *name;
\r
245 for(i=0; i<layout+buttons; i++) {
\r
246 int j=layoutList[i];
\r
247 if(j == -2) SetDlgItemText( hDlg, 2000+2*i, ". . ." );
\r
249 name = cps->option[j].name;
\r
250 if(strstr(name, "Polyglot ") == name) name += 9;
\r
251 SetDlgItemText( hDlg, 2000+2*i, name );
\r
252 //if(appData.debugMode) fprintf(debugFP, "# %s = %d\n",cps->option[j].name, cps->option[j].value );
\r
253 switch(cps->option[j].type) {
\r
255 SetDlgItemInt( hDlg, 2001+2*i, cps->option[j].value, TRUE );
\r
258 SetDlgItemText( hDlg, 2001+2*i, cps->option[j].textValue );
\r
261 CheckDlgButton( hDlg, 2000+2*i, cps->option[j].value != 0);
\r
264 choices = (char**) cps->option[j].textValue;
\r
265 hwndCombo = GetDlgItem(hDlg, 2001+2*i);
\r
266 SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);
\r
267 for(k=0; k<cps->option[j].max; k++) {
\r
268 SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) choices[k]);
\r
270 SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) choices[cps->option[j].value]);
\r
278 SetDlgItemText( hDlg, IDOK, "OK" );
\r
279 SetDlgItemText( hDlg, IDCANCEL, "Cancel" );
\r
280 sprintf(title, "%s Engine Settings (%s)", cps->which, cps->tidy);
\r
281 title[0] &= ~32; // capitalize
\r
282 SetWindowText( hDlg, title);
\r
283 for(i=0; i<groups; i+=2) {
\r
284 int id, p; char buf[MSG_SIZ];
\r
285 id = k = boxList[i];
\r
286 if(layoutList[k] < 0) k++;
\r
287 if(layoutList[k] < 0) continue;
\r
288 for(p=0; p<groupNameList[i]; p++) buf[p] = cps->option[layoutList[k]].name[p];
\r
290 SetDlgItemText( hDlg, 2000+2*(id+MAX_OPTIONS), buf );
\r
296 GetOptionValues(HWND hDlg, ChessProgramState *cps)
\r
297 // read out all controls, and if value is altered, remember it and send it to the engine
\r
300 int i, k, new=0, changed=0;
\r
301 char **choices, newText[MSG_SIZ], buf[MSG_SIZ];
\r
304 for(i=0; i<layout; i++) {
\r
305 int j=layoutList[i];
\r
307 switch(cps->option[j].type) {
\r
309 new = GetDlgItemInt( hDlg, 2001+2*i, &success, TRUE );
\r
310 if(!success) break;
\r
311 if(new < cps->option[j].min) new = cps->option[j].min;
\r
312 if(new > cps->option[j].max) new = cps->option[j].max;
\r
313 changed = 2*(cps->option[j].value != new);
\r
314 cps->option[j].value = new;
\r
319 success = GetDlgItemText( hDlg, 2001+2*i, newText, MSG_SIZ - strlen(cps->option[j].name) - 9 );
\r
320 if(!success) break;
\r
321 changed = strcmp(cps->option[j].textValue, newText) != 0;
\r
322 strcpy(cps->option[j].textValue, newText);
\r
325 new = IsDlgButtonChecked( hDlg, 2000+2*i );
\r
326 changed = 2*(cps->option[j].value != new);
\r
327 cps->option[j].value = new;
\r
330 choices = (char**) cps->option[j].textValue;
\r
331 hwndCombo = GetDlgItem(hDlg, 2001+2*i);
\r
332 success = GetDlgItemText( hDlg, 2001+2*i, newText, MSG_SIZ );
\r
333 if(!success) break;
\r
335 for(k=0; k<cps->option[j].max; k++) {
\r
336 if(!strcmp(choices[k], newText)) new = k;
\r
338 changed = new >= 0 && (cps->option[j].value != new);
\r
339 if(changed) cps->option[j].value = new;
\r
343 break; // are treated instantly, so they have been sent already
\r
345 if(changed == 2) sprintf(buf, "option %s=%d\n", cps->option[j].name, new); else
\r
346 if(changed == 1) sprintf(buf, "option %s=%s\n", cps->option[j].name, newText);
\r
347 if(changed) SendToProgram(buf, cps);
\r
351 LRESULT CALLBACK SettingsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
\r
358 case WM_INITDIALOG:
\r
360 // CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));
\r
361 SetOptionValues(hDlg, activeCps);
\r
363 // SetFocus(GetDlgItem(hDlg, IDC_NFG_Edit));
\r
368 switch( LOWORD(wParam) ) {
\r
370 GetOptionValues(hDlg, activeCps);
\r
371 EndDialog( hDlg, 0 );
\r
375 EndDialog( hDlg, 1 );
\r
379 // program-defined push buttons
\r
380 i = LOWORD(wParam);
\r
381 if( i>=2000 && i < 2000+2*(layout+buttons)) {
\r
382 j = layoutList[(i - 2000)/2];
\r
385 "All files\0*.*\0BIN Files\0*.bin\0LOG Files\0*.log\0INI Files\0*.ini\0\0";
\r
388 'A','l','l',' ','F','i','l','e','s', 0,
\r
390 'B','I','N',' ','F','i','l','e','s', 0,
\r
391 '*','.','b','i','n', 0,
\r
398 ZeroMemory( &ofn, sizeof(ofn) );
\r
400 ofn.lStructSize = sizeof(ofn);
\r
401 ofn.hwndOwner = hDlg;
\r
402 ofn.hInstance = hInst;
\r
403 ofn.lpstrFilter = filter;
\r
404 ofn.lpstrFile = buf;
\r
405 ofn.nMaxFile = sizeof(buf);
\r
406 ofn.lpstrTitle = "Choose Book";
\r
407 ofn.Flags = OFN_FILEMUSTEXIST | OFN_LONGNAMES | OFN_HIDEREADONLY;
\r
409 if( GetOpenFileName( &ofn ) ) {
\r
410 SetDlgItemText( hDlg, i+3, buf );
\r
414 if( activeCps->option[j].type == SaveButton)
\r
415 GetOptionValues(hDlg, activeCps);
\r
416 else if( activeCps->option[j].type != Button) break;
\r
417 sprintf(buf, "option %s\n", activeCps->option[j].name);
\r
418 SendToProgram(buf, activeCps);
\r
429 void AddControl(int x, int y, int w, int h, int type, int style, int n)
\r
433 i = template.header.cdit++;
\r
434 template.control[i].item.style = style;
\r
435 template.control[i].item.dwExtendedStyle = 0;
\r
436 template.control[i].item.x = x;
\r
437 template.control[i].item.y = y;
\r
438 template.control[i].item.cx = w;
\r
439 template.control[i].item.cy = h;
\r
440 template.control[i].item.id = 2000 + n;
\r
441 template.control[i].code = 0xFFFF;
\r
442 template.control[i].controlType = type;
\r
443 template.control[i].d1 = ' ';
\r
444 template.control[i].data = 0;
\r
445 template.control[i].creationData = 0;
\r
448 void AddOption(int x, int y, Control type, int i)
\r
454 AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);
\r
455 AddControl(x+95, y, 50, 11, 0x0081, ES_AUTOHSCROLL | ES_NUMBER | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);
\r
458 AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);
\r
459 AddControl(x+95, y, 190, 11, 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);
\r
461 case TextBox: // For now all text edits get a browse button, as long as -file and -path options are not yet implemented
\r
464 AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);
\r
465 AddControl(x+95, y, 180, 11, 0x0081, ES_AUTOHSCROLL | WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);
\r
466 AddControl(x+275, y, 20, 12, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i-2);
\r
467 layoutList[i/2-1] = -2;
\r
470 AddControl(x, y, 145, 11, 0x0080, BS_AUTOCHECKBOX | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);
\r
473 AddControl(x, y+1, 95, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);
\r
474 AddControl(x+95, y-1, 50, 500, 0x0085, CBS_AUTOHSCROLL | CBS_DROPDOWN | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i+1);
\r
479 AddControl(x-2, y, 65, 13, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, i);
\r
487 CreateDialogTemplate(int *layoutList, int nr, ChessProgramState *cps)
\r
489 int i, j, x=1, y=0, buttonRows, breakPoint = -1, k=0;
\r
491 template.header.cdit = 0;
\r
492 template.header.cx = 307;
\r
493 buttonRows = (buttons + 1 + 3)/4; // 4 per row, rounded up
\r
495 breakPoint = (nr+2*buttonRows+1)/2 & ~1;
\r
496 template.header.cx = 625;
\r
499 for(i=0; i<nr; i++) {
\r
500 if(k < groups && i == boxList[k]) {
\r
502 AddControl(x+2, y+13*(i>>1)-2, 301, 13*(boxList[k+1]-boxList[k]>>1)+8,
\r
503 0x0082, WS_VISIBLE | WS_CHILD | SS_BLACKFRAME, 2400);
\r
504 AddControl(x+60, y+13*(i>>1)-6, 10*groupNameList[k]/3, 10,
\r
505 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, 2*(i+MAX_OPTIONS));
\r
509 AddOption(x+155-150*(i&1), y+13*(i>>1)+5, cps->option[j].type, 2*i);
\r
510 if(k < groups && i+1 == boxList[k+1]) {
\r
513 if(i+1 == breakPoint) { x += 318; y = -13*(breakPoint>>1); }
\r
515 // add butons at the bottom of dialog window
\r
518 AddControl(x+275, y+18*(buttonRows-1), 25, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, IDOK-2000);
\r
519 AddControl(x+235, y+18*(buttonRows-1), 35, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, IDCANCEL-2000);
\r
520 for(i=0; i<buttons; i++) {
\r
521 AddControl(x+70*(i%4)+5, y+18*(i/4), 65, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 2*(nr+i));
\r
522 layoutList[nr+i] = buttonList[i];
\r
524 template.title[8] = cps == &first ? '1' : '2';
\r
525 template.header.cy = y += 18*buttonRows+2;
\r
526 template.header.style &= ~WS_VSCROLL;
\r
530 EngineOptionsPopup(HWND hwnd, ChessProgramState *cps)
\r
532 FARPROC lpProc = MakeProcInstance( (FARPROC) SettingsProc, hInst );
\r
535 DesignOptionDialog(cps);
\r
536 CreateDialogTemplate(layoutList, layout, cps);
\r
539 DialogBoxIndirect( hInst, &template.header, hwnd, (DLGPROC)lpProc );
\r
541 FreeProcInstance(lpProc);
\r