Fix key bindings for non-menu functions
[xboard.git] / xaw / xboard.c
index d93743f..f90fe06 100644 (file)
@@ -5,7 +5,7 @@
  * Massachusetts.
  *
  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
- * 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
+ * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  *
  * The following terms apply to Digital Equipment Corporation's copyright
  * interest in XBoard:
@@ -496,7 +496,8 @@ ParseFont (char *name, int number)
   if(sscanf(name, "size%d:", &size)) {
     // [HGM] font: font is meant for specific boardSize (likely from settings file);
     //       defer processing it until we know if it matches our board size
-    if(size >= 0 && size<MAX_SIZE) { // for now, fixed limit
+    if(strstr(name, "-*-") &&        // only pay attention to things that look like X-fonts
+       size >= 0 && size<MAX_SIZE) { // for now, fixed limit
        fontTable[number][size] = strdup(strchr(name, ':')+1);
        fontValid[number][size] = True;
     }
@@ -537,6 +538,12 @@ ParseColor (int n, char *name)
   if(colorVariable[n]) *(char**)colorVariable[n] = strdup(name);
 }
 
+char *
+Col2Text (int n)
+{
+    return *(char**)colorVariable[n];
+}
+
 void
 ParseTextAttribs (ColorClass cc, char *s)
 {
@@ -639,6 +646,12 @@ GetActualPlacement (Widget wg, WindowPlacement *wp)
 }
 
 void
+GetPlacement (DialogClass dlg, WindowPlacement *wp)
+{ // wrapper to shield back-end from widget type
+  if(shellUp[dlg]) GetActualPlacement(shells[dlg], wp);
+}
+
+void
 GetWindowCoords ()
 { // wrapper to shield use of window handles from back-end (make addressible by number?)
   // In XBoard this will have to wait until awareness of window parameters is implemented
@@ -831,17 +844,18 @@ char *
 GenerateGlobalTranslationTable (void)
 {
   /* go through all menu items and extract the keyboard shortcuts, so that X11 can load them */
-  char *output;
+  char *output[2];
 
-  int i,j;
+  int i,j,n=0;
   MenuItem *mi;
 
-  output = strdup("");
+  output[0] = strdup(""); // build keystrokes with and wo mod keys separately
+  output[1] = strdup(""); // so the more specific can preceed the other
 
   /* loop over all menu entries */
-  for( i=0; menuBar[i].mi ; i++)
+  for( i=0; menuBar[i-n].mi || !n++; i++)
     {
-      mi = menuBar[i].mi;
+      mi = menuBar[i+n].mi; // kludge to access 'noMenu' behind sentinel
       for(j=0; mi[j].proc; j++)
        {
          if (mi[j].accel)
@@ -906,17 +920,16 @@ GenerateGlobalTranslationTable (void)
                mods[strlen(mods)-1]='\0';
 
              /* get the name for the callback, we can use MenuItem() here that will call KeyBindingProc */
-             size_t namesize = snprintf(NULL, 0, "%s.%s", menuBar[i].ref, mi[j].ref);
-             char *name = malloc(namesize+1);
-             snprintf(name, namesize+1, "%s.%s", menuBar[i].ref, mi[j].ref);
+             char *name = malloc(MSG_SIZ);
+             if(n) snprintf(name, MSG_SIZ, "%s", mi[j].ref);
+             else  snprintf(name, MSG_SIZ, "%s.%s", menuBar[i].ref, mi[j].ref);
 
-             size_t buffersize = snprintf(NULL, 0, ":%s<Key>%s: MenuItem(%s) \n ", mods, key, name);
-             char *buffer = malloc(buffersize+1);
-             snprintf(buffer, buffersize+1, ":%s<Key>%s: MenuItem(%s) \n ", mods, key, name);
+             char *buffer = malloc(MSG_SIZ);
+             snprintf(buffer, MSG_SIZ, ":%s<Key>%s: MenuItem(%s) \n ", mods, key, name);
 
              /* add string to the output */
-             output = realloc(output, strlen(output) + strlen(buffer)+1);
-             strncat(output, buffer, strlen(buffer));
+             output[shift|alt|ctrl] = realloc(output[shift|alt|ctrl], strlen(output[shift|alt|ctrl]) + strlen(buffer)+1);
+             strncat(output[shift|alt|ctrl], buffer, strlen(buffer));
 
              /* clean up */
              free(key);
@@ -926,7 +939,10 @@ GenerateGlobalTranslationTable (void)
            }
        }
     }
-  return output;
+  output[1] = realloc(output[1], strlen(output[1]) + strlen(output[0])+1);
+  strncat(output[1], output[0], strlen(output[0]));
+  free(output[0]);
+  return output[1];
 }
 
 
@@ -990,6 +1006,23 @@ main (int argc, char **argv)
        exit(0);
     }
 
+    if(argc > 1 && !strcmp(argv[1], "--show-config")) { // [HGM] install: called to print config info
+       typedef struct {char *name, *value; } Config;
+       static Config configList[] = {
+         { "Datadir", DATADIR },
+         { "Sysconfdir", SYSCONFDIR },
+         { NULL }
+       };
+       int i;
+
+       for(i=0; configList[i].name; i++) {
+           if(argc > 2 && strcmp(argv[2], configList[i].name)) continue;
+           if(argc > 2) printf("%s", configList[i].value);
+           else printf("%-12s: %s\n", configList[i].name, configList[i].value);
+       }
+       exit(0);
+    }
+
     programName = strrchr(argv[0], '/');
     if (programName == NULL)
       programName = argv[0];
@@ -1086,6 +1119,8 @@ main (int argc, char **argv)
                    programName, appData.boardSize);
            exit(2);
        }
+       if(BOARD_WIDTH > 8)
+           squareSize = (squareSize*8 + BOARD_WIDTH/2)/BOARD_WIDTH; // scale height
        if (i < 7) {
            /* Find some defaults; use the nearest known size */
            SizeDefaults *szd, *nearest;
@@ -1109,8 +1144,8 @@ main (int argc, char **argv)
     } else {
         SizeDefaults *szd = sizeDefaults;
         if (*appData.boardSize == NULLCHAR) {
-           while (DisplayWidth(xDisplay, xScreen) < szd->minScreenSize ||
-                  DisplayHeight(xDisplay, xScreen) < szd->minScreenSize) {
+           while (DisplayWidth(xDisplay, xScreen)  < (szd->minScreenSize*BOARD_WIDTH  + 4)/8 ||
+                  DisplayHeight(xDisplay, xScreen) < (szd->minScreenSize*BOARD_HEIGHT + 4)/8) {
              szd++;
            }
            if (szd->name == NULL) szd--;
@@ -1289,6 +1324,9 @@ main (int argc, char **argv)
       EngineOutputPopUp();
     }
 
+    gameInfo.boardWidth = 0; // [HGM] pieces: kludge to ensure InitPosition() calls InitDrawingSizes()
+    InitPosition(TRUE);
+
     InitBackEnd2();
 
     if (errorExitStatus == -1) {
@@ -1309,8 +1347,6 @@ main (int argc, char **argv)
        }
     }
 
-    gameInfo.boardWidth = 0; // [HGM] pieces: kludge to ensure InitPosition() calls InitDrawingSizes()
-    InitPosition(TRUE);
     UpdateLogos(TRUE);
 //    XtSetKeyboardFocus(shellWidget, formWidget);
     XSetInputFocus(xDisplay, XtWindow(formWidget), RevertToPointerRoot, CurrentTime);
@@ -1320,6 +1356,13 @@ main (int argc, char **argv)
     return 0;
 }
 
+void
+DoEvents ()
+{
+    XtInputMask m;
+    while((m = XtAppPending(appContext))) XtAppProcessEvent(appContext, m);
+}
+
 RETSIGTYPE
 TermSizeSigHandler (int sig)
 {