Redo New Shuffle Game dialog with generic popup
[xboard.git] / xoptions.c
index b47f9a4..9612b1f 100644 (file)
@@ -104,132 +104,6 @@ void SetFocus(Widget w, XtPointer data, XEvent *event, Boolean *b)
     previous = w;
 }
 
-//--------------------------- New Shuffle Game --------------------------------------------
-int shuffleUp;
-Widget shuffleShell;
-
-void ShufflePopDown()
-{
-    if (!shuffleUp) return;
-    XtPopdown(shuffleShell);
-    XtDestroyWidget(shuffleShell);
-    shuffleUp = False;
-    ModeHighlight();
-}
-
-void ShuffleCallback(w, client_data, call_data)
-     Widget w;
-     XtPointer client_data, call_data;
-{
-    String name;
-    Widget w2;
-    Arg args[16];
-    char buf[MSG_SIZ];
-
-    XtSetArg(args[0], XtNlabel, &name);
-    XtGetValues(w, args, 1);
-
-    if (strcmp(name, _("cancel")) == 0) {
-        ShufflePopDown();
-        return;
-    }
-    if (strcmp(name, _("off")) == 0) {
-        ShufflePopDown();
-       shuffleOpenings = False; // [HGM] should be moved to New Variant menu, once we have it!
-       ResetGameEvent();
-        return;
-    }
-    if (strcmp(name, _("random")) == 0) {
-      snprintf(buf, MSG_SIZ,  "%d", rand());
-       XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
-       XtSetValues(XtParent(w), args, 1);
-        return;
-    }
-    if (strcmp(name, _("ok")) == 0) {
-       int nr; String name;
-        name = XawDialogGetValueString(w2 = XtParent(w));
-       if(sscanf(name ,"%d",&nr) != 1) {
-         snprintf(buf, MSG_SIZ,  "%d", appData.defaultFrcPosition);
-           XtSetArg(args[0],XtNvalue, buf); // erase bad (non-numeric) value
-           XtSetValues(w2, args, 1);
-           return;
-       }
-       appData.defaultFrcPosition = nr;
-       shuffleOpenings = True;
-        ShufflePopDown();
-       ResetGameEvent();
-        return;
-    }
-}
-
-void ShufflePopUp()
-{
-    Arg args[16];
-    Widget popup, layout, dialog, edit;
-    Window root, child;
-    int x, y, i;
-    int win_x, win_y;
-    unsigned int mask;
-    char def[MSG_SIZ];
-
-    i = 0;
-    XtSetArg(args[i], XtNresizable, True); i++;
-    XtSetArg(args[i], XtNwidth, DIALOG_SIZE); i++;
-    shuffleShell = popup =
-      XtCreatePopupShell(_("New Shuffle Game"), transientShellWidgetClass,
-                        shellWidget, args, i);
-
-    layout =
-      XtCreateManagedWidget(layoutName, formWidgetClass, popup,
-                           layoutArgs, XtNumber(layoutArgs));
-
-    snprintf(def, MSG_SIZ,  "%d\n", appData.defaultFrcPosition);
-    i = 0;
-    XtSetArg(args[i], XtNlabel, _("Start-position number:")); i++;
-    XtSetArg(args[i], XtNvalue, def); i++;
-    XtSetArg(args[i], XtNborderWidth, 0); i++;
-    dialog = XtCreateManagedWidget(_("Shuffle"), dialogWidgetClass,
-                                  layout, args, i);
-
-//    XtSetArg(args[0], XtNeditType, XawtextEdit);  // [HGM] can't get edit to work decently
-//    XtSetArg(args[1], XtNuseStringInPlace, False);
-//    XtSetValues(dialog, args, 2);
-
-    XawDialogAddButton(dialog, _("ok"), ShuffleCallback, (XtPointer) dialog);
-    XawDialogAddButton(dialog, _("cancel"), ShuffleCallback, (XtPointer) dialog);
-    XawDialogAddButton(dialog, _("random"), ShuffleCallback, (XtPointer) dialog);
-    XawDialogAddButton(dialog, _("off"), ShuffleCallback, (XtPointer) dialog);
-
-    XtRealizeWidget(popup);
-    CatchDeleteWindow(popup, "ShufflePopDown");
-
-    XQueryPointer(xDisplay, xBoardWindow, &root, &child,
-                 &x, &y, &win_x, &win_y, &mask);
-
-    XtSetArg(args[0], XtNx, x - 10);
-    XtSetArg(args[1], XtNy, y - 30);
-    XtSetValues(popup, args, 2);
-
-    XtPopup(popup, XtGrabExclusive);
-    shuffleUp = True;
-
-    edit = XtNameToWidget(dialog, "*value");
-
-    XtSetKeyboardFocus(popup, edit);
-}
-
-void ShuffleMenuProc(w, event, prms, nprms)
-     Widget w;
-     XEvent *event;
-     String *prms;
-     Cardinal *nprms;
-{
-//    if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
-//     Reset(FALSE, TRUE);
-//    }
-    ShufflePopUp();
-}
-
 //--------------------------- Time-Control Menu Popup ----------------------------------
 int TimeControlUp;
 Widget TimeControlShell;
@@ -780,7 +654,7 @@ void GenericPopDown(w, event, prms, nprms)
     PopDown(prms[0][0] - '0');
 }
 
-char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params;
+char *engineName, *engineDir, *engineChoice, *engineLine, *nickName, *params, *tfName;
 Boolean isUCI, hasBook, storeVariant, v1, addToList, useNick;
 extern Option installOptions[], matchOptions[];
 char *engineNr[] = { N_("First Engine"), N_("Second Engine"), NULL };
@@ -803,14 +677,14 @@ int MatchOK(int n)
 {
     if(appData.participants && appData.participants[0]) free(appData.participants);
     appData.participants = strdup(engineName);
-    if(!CreateTourney(appData.tourneyFile)) return 0;
+    if(!CreateTourney(tfName)) return !appData.participants[0];
     PopDown(0); // early popdown to prevent FreezeUI called through MatchEvent from causing XtGrab warning
     MatchEvent(2); // start tourney
     return 1;
 }
 
 Option matchOptions[] = {
-{ 0,  0,          0, NULL, (void*) &appData.tourneyFile, ".trn", NULL, FileName, N_("Tournament file:") },
+{ 0,  0,          0, NULL, (void*) &tfName, ".trn", NULL, FileName, N_("Tournament file:") },
 { 0,  0,          0, NULL, (void*) &appData.roundSync, "", NULL, CheckBox, N_("Sync after round    (for concurrent playing of a single") },
 { 0,  0,          0, NULL, (void*) &appData.cycleSync, "", NULL, CheckBox, N_("Sync after cycle      tourney with multiple XBoards)") },
 { 0xD, 150,       0, NULL, (void*) &engineName, "", NULL, TextBox, "Tourney participants:" },
@@ -1771,6 +1645,7 @@ void MatchOptionsProc(w, event, prms, nprms)
    NamesToList(firstChessProgramNames, engineList, engineMnemonic);
    comboCallback = &AddToTourney;
    matchOptions[5].min = -(appData.pairingEngine[0] != NULLCHAR); // with pairing engine, allow Swiss
+   ASSIGN(tfName, appData.tourneyFile[0] ? appData.tourneyFile : MakeName(appData.defName));
    GenericPopUp(matchOptions, _("Match Options"), 0);
 }
 
@@ -2097,6 +1972,43 @@ void EditBookProc(w, event, prms, nprms)
     EditBookEvent();
 }
 
+void SetRandom P((int n));
+
+int ShuffleOK(int n)
+{
+    ResetGameEvent();
+    return 1;
+}
+
+Option shuffleOptions[] = {
+{   0,  0,   50, NULL, (void*) &shuffleOpenings, NULL, NULL, CheckBox, "shuffle" },
+{ 0,-1,1000000000, NULL, (void*) &appData.defaultFrcPosition, "", NULL, Spin, N_("Start-position number:") },
+{   0,  0,    0, NULL, (void*) &SetRandom, NULL, NULL, Button, "randomize" },
+{   0,  1,    0, NULL, (void*) &SetRandom, NULL, NULL, Button, "pick fixed" },
+{   0,  1,    0, NULL, (void*) &ShuffleOK, "", NULL, EndMark , "" }
+};
+
+void SetRandom(int n)
+{
+    int r = n==2 ? -1 : rand();
+    char buf[MSG_SIZ];
+    Arg args[2];
+    snprintf(buf, MSG_SIZ,  "%d", r);
+    XtSetArg(args[0],XtNstring, buf);
+    XtSetValues(shuffleOptions[1].handle, args, 1);
+    XtSetArg(args[0],XtNstate, True);
+    XtSetValues(shuffleOptions[0].handle, args, 1);
+}
+
+void ShuffleMenuProc(w, event, prms, nprms)
+     Widget w;
+     XEvent *event;
+     String *prms;
+     Cardinal *nprms;
+{
+    GenericPopUp(shuffleOptions, _("New Shuffle Game"), 0);
+}
+
 //---------------------------- Chat Windows ----------------------------------------------
 
 void OutputChatMessage(int partner, char *mess)