Allow grouping of engines in engine list
[xboard.git] / winboard / wsettings.c
index 023eb2d..10bb427 100644 (file)
@@ -285,7 +285,7 @@ SetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)
        int j=layoutList[i];\r
        if(j == -2) SetDlgItemText( hDlg, 2000+2*i, ". . ." );\r
        if(j<0) continue;\r
-       name = optionList[j].name;\r
+       name = cps ? optionList[j].name : _(optionList[j].name);\r
        if(strstr(name, "Polyglot ") == name) name += 9;\r
        SetDlgItemText( hDlg, 2000+2*i, name );\r
 //if(appData.debugMode) fprintf(debugFP, "# %s = %d\n",optionList[j].name, optionList[j].value );\r
@@ -362,11 +362,11 @@ GetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)
                if(cps) len = MSG_SIZ - strlen(optionList[j].name) - 9, text = newText;\r
                else    len = GetWindowTextLength(GetDlgItem(hDlg, 2001+2*i)) + 1, text = (char*) malloc(len);\r
                success = GetDlgItemText( hDlg, 2001+2*i, text, len );\r
-               if(!success) break;\r
+               if(!success) text[0] = NULLCHAR; // empty string can be valid input\r
                if(!cps) {\r
                    char *p;\r
                    p = (optionList[j].type != FileName ? strdup(text) : InterpretFileName(text, homeDir)); // all files relative to homeDir!\r
-                   FREE(*(char**)optionList[j].target); *(char**)optionList[j].target = p;
+                   FREE(*(char**)optionList[j].target); *(char**)optionList[j].target = p;\r
                    free(text); text = p;\r
                    while(*p++ = *text++) if(p[-1] == '\r') p--; // crush CR\r
                    break;\r
@@ -389,7 +389,7 @@ GetOptionValues(HWND hDlg, ChessProgramState *cps, Option *optionList)
                for(k=0; k<optionList[j].max; k++) {\r
                    if(!strcmp(choices[k], newText)) new = k;\r
                }\r
-               if(!cps && new) {\r
+               if(!cps && new > 0) {\r
                    if(*(char**)optionList[j].target) free(*(char**)optionList[j].target);\r
                    *(char**)optionList[j].target = strdup(optionList[j].choice[new]);\r
                    break;\r
@@ -425,7 +425,7 @@ LRESULT CALLBACK SettingsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lPa
 //        CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));\r
        SetOptionValues(hDlg, activeCps, activeList);\r
 \r
-//        SetFocus(GetDlgItem(hDlg, IDC_NFG_Edit));\r
+        SetFocus(GetDlgItem(hDlg, IDCANCEL));\r
 \r
         break;\r
 \r
@@ -480,12 +480,14 @@ LRESULT CALLBACK SettingsProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lPa
                }\r
                if(j < 0) break;\r
                if(comboCallback && activeList[j].type == ComboBox && HIWORD(wParam) == CBN_SELCHANGE) {\r
+                   if(j > 5) break; // Yegh! Must solve problem with more than one ombobox in dialog\r
                    (*comboCallback)(hDlg);\r
                    break;\r
                } else\r
                if( activeList[j].type  == SaveButton)\r
                     GetOptionValues(hDlg, activeCps, activeList);\r
                else if( activeList[j].type  != Button) break;\r
+               else if( !activeCps ) { (*(ButtonCallback*) activeList[j].target)(hDlg); break; }\r
                snprintf(buf, MSG_SIZ, "option %s\n", activeList[j].name);\r
                SendToProgram(buf, activeCps);\r
            }\r
@@ -535,7 +537,7 @@ void AddOption(int x, int y, Control type, int i)
            break;\r
        case Label:\r
            extra = activeList[layoutList[i/2]].value;\r
-           AddControl(x+extra, y+1, 290-extra, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, i);\r
+           AddControl(x+extra, y+1, 290-extra, 9, 0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
            break;\r
        case FileName:\r
        case PathName:\r
@@ -555,7 +557,7 @@ void AddOption(int x, int y, Control type, int i)
        case Button:\r
        case ResetButton:\r
        case SaveButton:\r
-           AddControl(x-2, y, 65, 13, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, i);\r
+           AddControl(x-2, y, 65, 13, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, i);\r
        case Message:\r
        default:\r
            break;\r
@@ -566,7 +568,7 @@ void AddOption(int x, int y, Control type, int i)
 void\r
 CreateDialogTemplate(int *layoutList, int nr, Option *optionList)\r
 {\r
-    int i, j, x=1, y=0, buttonRows, breakPoint = -1, k=0;\r
+    int i, ii, j, x=1, y=0, buttonRows, breakPoint = -1, k=0;\r
 \r
     template.header.cdit = 0;\r
     template.header.cx = 307;\r
@@ -576,31 +578,32 @@ CreateDialogTemplate(int *layoutList, int nr, Option *optionList)
        template.header.cx = 625;\r
     }\r
 \r
-    for(i=0; i<nr; i++) {\r
-       if(k < groups && i == boxList[k]) {\r
+    for(ii=0; ii<nr; ii++) {\r
+       i = ii^1; if(i == nr) i = ii; // if two on one line, swap order of treatment, to get good (left to right) tabbing order.\r
+       if(k < groups && ii == boxList[k]) {\r
            y += 10;\r
            AddControl(x+2, y+13*(i>>1)-2, 301, 13*(boxList[k+1]-boxList[k]>>1)+8,\r
                                                0x0082, WS_VISIBLE | WS_CHILD | SS_BLACKFRAME, 2400);\r
            AddControl(x+60, y+13*(i>>1)-6, 10*groupNameList[k]/3, 10,\r
-                                               0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, 2*(i+MAX_OPTIONS));\r
+                                               0x0082, SS_ENDELLIPSIS | WS_VISIBLE | WS_CHILD, 2*(ii+MAX_OPTIONS));\r
        }\r
        j = layoutList[i];\r
        if(j >= 0)\r
            AddOption(x+155-150*(i&1), y+13*(i>>1)+5, optionList[j].type, 2*i);\r
-       if(k < groups && i+1 == boxList[k+1]) {\r
+       if(k < groups && ii+1 == boxList[k+1]) {\r
            k += 2; y += 4;\r
        }\r
-       if(i+1 == breakPoint) { x += 318; y = -13*(breakPoint>>1); }\r
+       if(ii+1 == breakPoint) { x += 318; y = -13*(breakPoint>>1); }\r
     }\r
     // add butons at the bottom of dialog window\r
     y += 13*(nr>>1)+5;\r
 \r
-    AddControl(x+225, y+18*(buttonRows-1), 30, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, IDOK-2000);\r
-    AddControl(x+260, y+18*(buttonRows-1), 40, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, IDCANCEL-2000);\r
     for(i=0; i<buttons; i++) {\r
-       AddControl(x+70*(i%4)+5, y+18*(i/4), 65, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 2*(nr+i));\r
+       AddControl(x+70*(i%4)+5, y+18*(i/4), 65, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, 2*(nr+i));\r
        layoutList[nr+i] = buttonList[i];\r
     }\r
+    AddControl(x+225, y+18*(buttonRows-1), 30, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, IDOK-2000);\r
+    AddControl(x+260, y+18*(buttonRows-1), 40, 15, 0x0080, BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP, IDCANCEL-2000);\r
     template.title[8] = optionList == first.option ? '1' :  '2';\r
     template.header.cy = y += 18*buttonRows+2;\r
     template.header.style &= ~WS_VSCROLL;\r
@@ -626,6 +629,7 @@ EngineOptionsPopup(HWND hwnd, ChessProgramState *cps)
 \r
 int InstallOK()\r
 {\r
+    if(engineLine[0] == '#') { DisplayError(_("Select single engine from the group"), 0); return 0; }\r
     if(isUCCI) isUCI = 2;\r
     if(engineChoice[0] == engineNr[0][0])  Load(&first, 0); else Load(&second, 1);\r
     return 1;\r
@@ -668,10 +672,27 @@ GenericPopup(HWND hwnd, Option *optionList)
     return;\r
 }\r
 \r
-void LoadEnginePopUp(HWND hwnd)\r
+int\r
+EnterGroup(HWND hDlg)\r
 {\r
-    int n=0;\r
+    char buf[MSG_SIZ];\r
+    HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*1);\r
+    int i = ComboBox_GetCurSel(hwndCombo);\r
+    if(i == 0) buf[0] = NULLCHAR; // back to top level\r
+    else if(engineList[i][0] == '#') safeStrCpy(buf, engineList[i], MSG_SIZ); // group header, open group\r
+    else return 0; // normal line, select engine\r
+    installOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, buf); // replace list by only the group contents\r
+    SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);\r
+    SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) buf);\r
+    for(i=1; i<installOptions[0].max; i++) {\r
+           SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
+    }\r
+    SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) 0, (LPARAM) buf);\r
+    return 0;\r
+}\r
 \r
+void LoadEnginePopUp(HWND hwnd)\r
+{\r
     isUCI = isUCCI = storeVariant = v1 = useNick = FALSE; addToList = hasBook = TRUE; // defaults\r
     if(engineDir)    free(engineDir);    engineDir = strdup("");\r
     if(params)       free(params);       params = strdup("");\r
@@ -679,29 +700,78 @@ void LoadEnginePopUp(HWND hwnd)
     if(engineChoice) free(engineChoice); engineChoice = strdup(engineNr[0]);\r
     if(engineLine)   free(engineLine);   engineLine = strdup("");\r
     if(engineName)   free(engineName);   engineName = strdup("");\r
-    NamesToList(firstChessProgramNames, engineList, engineMnemonic);\r
-    while(engineList[n]) n++; installOptions[0].max = n;\r
+    installOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, ""); // only top level\r
     snprintf(title, MSG_SIZ, _("Load Engine"));\r
+    comboCallback = &EnterGroup;\r
 \r
     GenericPopup(hwnd, installOptions);\r
 }\r
 \r
 Boolean autoinc, twice, swiss;\r
+char *tfName;\r
 \r
 int MatchOK()\r
 {\r
     if(autoinc) appData.loadGameIndex = appData.loadPositionIndex = -(twice + 1);\r
+    if(!appData.loadGameFile[0]) appData.loadGameIndex = -2*twice; // kludge to pass value of "twice" for use in GUI book\r
     if(swiss) { appData.defaultMatchGames = 1; appData.tourneyType = -1; }\r
-    if(CreateTourney(appData.tourneyFile)) MatchEvent(2); else return 0;\r
-    return 1;\r
+    if(CreateTourney(tfName) && !matchMode) { // CreateTourney reloads original settings if file already existed\r
+       MatchEvent(2);\r
+       return 1; // close dialog\r
+    }\r
+    return matchMode || !appData.participants[0]; // if we failed to create and are not in playing, forbid popdown if there are participants\r
 }\r
 \r
+char *GetParticipants(HWND hDlg)\r
+{\r
+    int len = GetWindowTextLength(GetDlgItem(hDlg, 2001+2*9)) + 1;\r
+    char *participants,*p, *q;\r
+    if(len < 4) return NULL; // box is empty (enough)\r
+    participants = (char*) malloc(len);\r
+    GetDlgItemText(hDlg, 2001+2*9, participants, len );\r
+    p = q = participants;\r
+    while(*p++ = *q++) if(p[-1] == '\r') p--;\r
+    return participants;\r
+}\r
+\r
+void ReplaceParticipant(HWND hDlg)\r
+{\r
+    char *participants = GetParticipants(hDlg);\r
+    Substitute(participants, TRUE);\r
+}\r
+       \r
+void UpgradeParticipant(HWND hDlg)\r
+{\r
+    char *participants = GetParticipants(hDlg);\r
+    Substitute(participants, FALSE);\r
+}\r
+\r
+void Inspect(HWND hDlg)\r
+{\r
+    FILE *f;\r
+    char name[MSG_SIZ];\r
+    GetDlgItemText(hDlg, 2001+2*1, name, MSG_SIZ );\r
+    if(name && name[0] && (f = fopen(name, "r")) ) {\r
+       char *saveSaveFile;\r
+       saveSaveFile = appData.saveGameFile; appData.saveGameFile = NULL; // this is a persistent option, protect from change\r
+       ParseArgsFromFile(f);\r
+       autoinc = ((appData.loadPositionFile[0] ? appData.loadGameIndex : appData.loadPositionIndex) < 0);\r
+       twice = ((appData.loadPositionFile[0] ? appData.loadGameIndex : appData.loadPositionIndex) == -2);\r
+       swiss = appData.tourneyType < 0;\r
+       SetOptionValues(hDlg, NULL, activeList);\r
+       FREE(appData.saveGameFile); appData.saveGameFile = saveSaveFile;\r
+    } else DisplayError(_("First you must specify an existing tourney file to clone"), 0);\r
+}\r
+\r
+void TimeControlOptionsPopup P((HWND hDlg));\r
+void UciOptionsPopup P((HWND hDlg));\r
+\r
 Option tourneyOptions[] = {\r
-  { 0,  0,          4, NULL, (void*) &appData.tourneyFile, "", NULL, FileName, N_("Tournament file:") },\r
+  { 0,  0,          4, NULL, (void*) &tfName, "", NULL, FileName, N_("Tournament file:") },\r
   { 30, 0,          0, NULL, NULL, NULL, NULL, Label, N_("If you specify an existing file, the rest of this dialog will be ignored.") },\r
   { 30, 0,          0, NULL, NULL, NULL, NULL, Label, N_("Otherwise, the file will be created, with the settings you specify below:") },\r
-  { 0,  1,          0, NULL, (void*) &engineChoice, (char*) (engineMnemonic+1), (engineMnemonic+1), ComboBox, N_("Select Engine:") },\r
-  { 0xD, 7,         0, NULL, (void*) &appData.participants, "", NULL, TextBox, "Tourney participants:" },\r
+  { 0,  1,          0, NULL, (void*) &engineChoice, (char*) engineMnemonic, engineMnemonic, ComboBox, N_("Select Engine:") },\r
+  { 0xD, 7,         0, NULL, (void*) &appData.participants, "", NULL, TextBox, N_("Tourney participants:") },\r
   { 0,  0,          0, NULL, (void*) &swiss, "", NULL, CheckBox, N_("Use Swiss pairing engine (cycles = rounds)") },\r
   { 0,  0,         10, NULL, (void*) &appData.tourneyType, "", NULL, Spin, N_("Tourney type (0=RR, 1=gauntlet):") },\r
   { 0,  0,          0, NULL, (void*) &appData.cycleSync, "", NULL, CheckBox, N_("Sync after cycle") },\r
@@ -716,33 +786,48 @@ Option tourneyOptions[] = {
   { 0,  0,          0, NULL, (void*) &autoinc, "", NULL, CheckBox, N_("Step through lines/positions in file") },\r
   { 0,  0, 1000000000, NULL, (void*) &appData.rewindIndex, "", NULL, Spin, N_("Rewind after (0 = never):") },\r
   { 0,  0,          0, NULL, (void*) &twice, "", NULL, CheckBox, N_("Use each line/position twice") },\r
+  { 0,  0,          0, NULL, (void*) &appData.defNoBook, "", NULL, CheckBox, N_("Make all use GUI book by default") },\r
   { 0,  0, 1000000000, NULL, (void*) &appData.matchPause, "", NULL, Spin, N_("Pause between Games (ms):") },\r
+  { 0,  0,          0, NULL, (void*) &ReplaceParticipant, "", NULL, Button, N_("Replace Engine") },\r
+  { 0,  0,          0, NULL, (void*) &UpgradeParticipant, "", NULL, Button, N_("Upgrade Engine") },\r
+  { 0,  0,          0, NULL, (void*) &TimeControlOptionsPopup, "", NULL, Button, N_("Time Control...") },\r
+  { 0,  0,          0, NULL, (void*) &UciOptionsPopup, "", NULL, Button, N_("Common Engine...") },\r
+  { 0,  0,          0, NULL, (void*) &Inspect, "", NULL, Button, N_("Clone Tourney") },\r
   { 0, 0, 0, NULL, (void*) &MatchOK, "", NULL, EndMark , "" }\r
 };\r
 \r
 int AddToTourney(HWND hDlg)\r
 {\r
-    char buf[MSG_SIZ];\r
-//    GetDlgItemText( hDlg, 2001+2*7, buf, MSG_SIZ-3 ); // this gives the previous selection !!!\r
-//    strncat(buf, "\r\n", MSG_SIZ);\r
-    int i = ComboBox_GetCurSel(GetDlgItem(hDlg, 2001+2*7));\r
-    snprintf(buf, MSG_SIZ, "%s\r\n", engineMnemonic[i+1]);\r
+  char buf[MSG_SIZ];\r
+  HANDLE hwndCombo = GetDlgItem(hDlg, 2001+2*7);\r
+  int i = ComboBox_GetCurSel(hwndCombo);\r
+  if(i == 0) buf[0] = NULLCHAR; // back to top level\r
+  else if(engineList[i][0] == '#') safeStrCpy(buf, engineList[i], MSG_SIZ); // group header, open group\r
+  else { // normal line, select engine\r
+    snprintf(buf, MSG_SIZ, "%s\r\n", engineMnemonic[i]);\r
     SendMessage( GetDlgItem(hDlg, 2001+2*9), EM_SETSEL, 99999, 99999 );\r
     SendMessage( GetDlgItem(hDlg, 2001+2*9), EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) buf );\r
     return 0;\r
+  }\r
+  installOptions[0].max = NamesToList(firstChessProgramNames, engineList, engineMnemonic, buf); // replace list by only the group contents\r
+  SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);\r
+  SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) buf);\r
+  for(i=1; i<installOptions[0].max; i++) {\r
+    SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) engineMnemonic[i]);\r
+  }\r
+  SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) 0, (LPARAM) buf);\r
+  return 0;\r
 }\r
 \r
 void TourneyPopup(HWND hwnd)\r
 {\r
-    int n=0;\r
-\r
-    NamesToList(firstChessProgramNames, engineList, engineMnemonic);\r
+    int n = NamesToList(firstChessProgramNames, engineList, engineMnemonic, "");\r
     comboCallback = &AddToTourney;\r
     autoinc = appData.loadGameIndex < 0 || appData.loadPositionIndex < 0;\r
-    twice = TRUE; swiss = appData.tourneyType < 0;\r
-    while(engineList[n]) n++; tourneyOptions[3].max = n-1;\r
+    twice = FALSE; swiss = appData.tourneyType < 0;\r
+    tourneyOptions[3].max = n;\r
     snprintf(title, MSG_SIZ, _("Tournament and Match Options"));\r
-    if(appData.pairingEngine[0]) tourneyOptions[5].min = -19;\r
+    ASSIGN(tfName, appData.tourneyFile[0] ? appData.tourneyFile : MakeName(appData.defName));\r
 \r
     GenericPopup(hwnd, tourneyOptions);\r
 }\r