Also supply shortcut for start directory in GTK file chooser
[xboard.git] / gtk / xoptions.c
index 4f68db7..fbe1c23 100644 (file)
@@ -279,13 +279,35 @@ ApplyFont (Option *opt, char *font)
       case ListBox:
       case Label:   w = opt->handle; break;
       case Button:  if(opt->handle) w = gtk_bin_get_child(GTK_BIN(opt->handle)); break;
-      case TextBox: w = opt->textValue; break;
+      case TextBox: w = (GtkWidget *) opt->textValue; if(!w) w = opt->handle; break;
       default: ;
     }
-printf("set font %s\n", font);
     if(w && font) SetWidgetFont(w, &font);
 }
 
+GtkWidget *fbutton;
+
+void
+FontCallback (GtkWidget *widget, gpointer gdata)
+{
+    Option *opt = (Option *) gdata;
+    gchar *p = (char *) gtk_font_button_get_font_name(GTK_FONT_BUTTON(fbutton));
+    SetWidgetText(opt, p, TransientDlg);
+    ApplyFont(opt, p);
+}
+
+void
+ColorCallback (GtkWidget *widget, gpointer gdata)
+{
+    Option *opt = (Option *) gdata;
+    GdkColor rgba;
+    char buf[MSG_SIZ]; 
+    gtk_color_button_get_color(GTK_COLOR_BUTTON(widget), &rgba);
+    snprintf(buf, MSG_SIZ, "#%02x%02x%02x", rgba.red>>8, rgba.green>>8, rgba.blue>>8);
+    gtk_widget_modify_bg ( GTK_WIDGET(opt[1].handle), GTK_STATE_NORMAL, &rgba );
+    SetWidgetText(opt, buf, TransientDlg);
+}
+
 void
 SetListBoxItem (GtkListStore *store, int n, char *msg)
 {
@@ -481,7 +503,7 @@ CreateMenuPopup (Option *opt, int n, int def)
          } else
            entry = gtk_menu_item_new_with_label(msg);
          gtk_signal_connect_object (GTK_OBJECT (entry), "activate", GTK_SIGNAL_FUNC(MenuSelect), (gpointer) (intptr_t) ((n<<16)+i));
-         g_signal_connect(entry, "button-release-event", G_CALLBACK (HelpEvent), (gpointer) mb[i].string );
+         g_signal_connect(entry, "button-release-event", G_CALLBACK (HelpEvent), (gpointer) (mb[i].proc ? mb[i].string : "Recently Used Engines"));
          if(mb[i].accel) {
            guint accelerator_key;
            GdkModifierType accelerator_mods;
@@ -616,6 +638,11 @@ AppendColorized (Option *opt, char *s, int count)
     static GtkTextIter end;
     static GtkTextTag *fgTags[8], *bgTags[8], *font, *bold, *normal, *attr = NULL;
 
+    if(!s) {
+       font = NULL;
+       return;
+    }
+
     if(!font) {
        font = gtk_text_buffer_create_tag(opt->handle, NULL, "font", appData.icsFont, NULL);
        gtk_widget_modify_base(GTK_WIDGET(opt->textValue), GTK_STATE_NORMAL, &backgroundColor);
@@ -818,6 +845,8 @@ MemoEvent(GtkWidget *widget, GdkEvent *event, gpointer gdata)
 #endif
 }
 
+int messedUp;
+
 int
 PopDown (DialogClass n)
 {
@@ -847,6 +876,7 @@ PopDown (DialogClass n)
     RaiseWindow(parents[n]); // automatic in GTK?
     if(parents[n] == BoardWindow) XtSetKeyboardFocus(shellWidget, formWidget); // also automatic???
 #endif
+    if(messedUp) Preview(0, NULL); messedUp = FALSE; // Board Options dialog can need this to cancel preview
     return 1;
 }
 
@@ -998,6 +1028,7 @@ GraphEventProc(GtkWidget *widget, GdkEvent *event, gpointer gdata)
        case GDK_SCROLL:
            if(sevent->direction == GDK_SCROLL_UP) button = 4;
            if(sevent->direction == GDK_SCROLL_DOWN) button = 5;
+           w = h = 0; // to keep Clang happy
            break;
        case GDK_MOTION_NOTIFY:
            f = 0;
@@ -1073,21 +1104,38 @@ void GenericCallback(GtkWidget *widget, gpointer gdata)
     shells[dlg] = oldSh; // in case of multiple instances, restore previous (as this one could be popped down now)
 }
 
+int
+BrowseCallback (GtkFileChooser *chooser, gpointer data)
+{
+    char *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(chooser));
+    Option *opt = currentOption + (int)(intptr_t) data;
+    int n = (int) (intptr_t) opt->choice;
+    if(name) {
+       Preview(n, name);
+       messedUp = TRUE;
+       g_free(name);
+    }
+    return FALSE;
+}
+
 void BrowseGTK(GtkWidget *widget, gpointer gdata)
 {
     GtkWidget *entry;
     GtkWidget *dialog;
     GtkFileFilter *gtkfilter;
     GtkFileFilter *gtkfilter_all;
-    int opt_i = (intptr_t) gdata;
+    int n, opt_i = (intptr_t) gdata;
     GtkFileChooserAction fc_action;
+    char buf[MSG_SIZ], *p;
 
     gtkfilter     = gtk_file_filter_new();
     gtkfilter_all = gtk_file_filter_new();
 
-    char fileext[MSG_SIZ], *filter = currentOption[opt_i].textValue;
+    char fileext[MSG_SIZ], *filter = currentOption[opt_i].textValue, *old = NULL;
 
-    StartDir(filter, NULL); // change to start directory for this file type
+    if(currentCps) filter = NULL; else if(currentOption[opt_i].type == PathName && filter) filter = "dir";
+    GetWidgetText(&currentOption[opt_i], &old); // start in same directory as current widget contents
+    StartDir(filter, old); // change to start directory for this file type
 
     /* select file or folder depending on option_type */
     if (currentOption[opt_i].type == PathName)
@@ -1102,13 +1150,21 @@ void BrowseGTK(GtkWidget *widget, gpointer gdata)
                       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                       NULL);
 
+    if(*chessDir && (!(p = strstr(chessDir, "/home/")) || strchr(p+6, '/')))
+    gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog), chessDir, NULL);
+    gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog), dataDir, NULL);
+    snprintf(buf, MSG_SIZ, "%s/themes", dataDir);
+    gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog), buf, NULL);
+    snprintf(buf, MSG_SIZ, "%s/themes/textures", dataDir);
+    gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog), buf, NULL);
+
     /* one filter to show everything */
     gtk_file_filter_add_pattern(gtkfilter_all, "*");
     gtk_file_filter_set_name   (gtkfilter_all, "All Files");
     gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),gtkfilter_all);
 
     /* filter for specific filetypes e.g. pgn or fen */
-    if (currentOption[opt_i].textValue != NULL)
+    if (currentOption[opt_i].textValue != NULL && !currentCps) // no filters for engine options!
       {
         char *q, *p = currentOption[opt_i].textValue;
         gtk_file_filter_set_name (gtkfilter, p);
@@ -1125,6 +1181,13 @@ void BrowseGTK(GtkWidget *widget, gpointer gdata)
     else
       gtk_file_chooser_set_filter (GTK_FILE_CHOOSER(dialog),gtkfilter_all);
 
+    messedUp = FALSE;
+    n = (int)(intptr_t) currentOption[opt_i].choice;
+    if (n && !currentCps) {
+      g_signal_connect (GTK_DIALOG (dialog), "selection-changed", G_CALLBACK(BrowseCallback), (gpointer)(intptr_t) opt_i);
+      gtk_window_set_title(GTK_WINDOW(dialog), _("*** Board window shows preview of selection ***"));
+    }
+
     if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
       {
         char *filename;
@@ -1134,7 +1197,10 @@ void BrowseGTK(GtkWidget *widget, gpointer gdata)
        StartDir(filter, filename); // back to original, and remember this one
         g_free (filename);
       }
-    else StartDir(filter, ""); // change back to original directory
+    else {
+       StartDir(filter, ""); // change back to original directory
+       if(n && messedUp) Preview(n, old); // undo any board preview of the parameter browsed for
+    }
     gtk_widget_destroy (dialog);
     dialog = NULL;
 }
@@ -1364,7 +1430,7 @@ if(appData.debugMode) printf("n=%d, h=%d, w=%d\n",n,height,width);
                GtkAttachOptions x = GTK_FILL;
                // make sure hbox is always available when we have more options on same row
                 hbox = gtk_hbox_new (option[i].type == Button && option[i].textValue || option[i].type == Graph, 0);
-               if(!currentCps && option[i].value > 80) x |= GTK_EXPAND; // only vertically extended widgets should size vertically
+               if(!currentCps && option[i].value > 80 && option[i].type == TextBox) x |= GTK_EXPAND; // only vertically extended widgets should size vertically
                 if (strcmp(option[i].name, "") == 0 || option[i].type == Label || option[i].type == Button)
                     // for Label and Button name is contained inside option
                     gtk_table_attach(GTK_TABLE(table), hbox, left, left+r, top, top+1, GTK_FILL | GTK_EXPAND, x, 2, 1);
@@ -1384,7 +1450,7 @@ if(appData.debugMode) printf("n=%d, h=%d, w=%d\n",n,height,width);
          case FileName:
          case PathName:
           tBox:
-            label = gtk_label_new(option[i].name);
+            label = gtk_label_new(_(option[i].name));
             /* Left Justify */
             gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
 
@@ -1479,7 +1545,7 @@ if(appData.debugMode) printf("n=%d, h=%d, w=%d\n",n,height,width);
             }
             break;
           case CheckBox:
-            checkbutton = gtk_check_button_new_with_label(option[i].name);
+            checkbutton = gtk_check_button_new_with_label(_(option[i].name));
            g_signal_connect(checkbutton, "button-press-event", G_CALLBACK (HelpEvent), (gpointer) option[i].name );
             if(!currentCps) option[i].value = *(Boolean*)option[i].target;
             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), option[i].value);
@@ -1492,16 +1558,16 @@ if(appData.debugMode) printf("n=%d, h=%d, w=%d\n",n,height,width);
             Pack(hbox, table, label, left, left+2, top, 0);
             break;
          case Label:
-            option[i].handle = (void *) (label = gtk_label_new(option[i].name));
+            option[i].handle = (void *) (label = gtk_label_new(_(option[i].name)));
             /* Left Justify */
             gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
             SetWidgetFont(label, option[i].font);
+            gtk_widget_set_size_request(label, option[i].max ? option[i].max : -1, -1);
            if(option[i].min & BORDER) {
                GtkWidget *frame = gtk_frame_new(NULL);
                 gtk_container_add(GTK_CONTAINER(frame), label);
                label = frame;
            }
-            gtk_widget_set_size_request(label, option[i].max ? option[i].max : -1, -1);
            if(option[i].target || dlgNr != ErrorDlg && option[i].name) { // allow user to specify event handler for button presses
                button = gtk_event_box_new();
                 gtk_container_add(GTK_CONTAINER(button), label);
@@ -1516,7 +1582,23 @@ if(appData.debugMode) printf("n=%d, h=%d, w=%d\n",n,height,width);
            break;
           case SaveButton:
           case Button:
-            button = gtk_button_new_with_label (option[i].name);
+            if(!strcmp(option[i].name, "fontsel")) {
+                option[i].handle = (void *) (fbutton = gtk_font_button_new());
+                Pack(hbox, table, fbutton, left, left+r, top, 0);
+                break;
+            }
+            if(!strcmp(option[i].name, "R") || !strcmp(option[i].name, "G") ||
+               !strcmp(option[i].name, "B") && !strcmp(option[i+1].name, "D")) {
+                break;
+            } else
+            if(!strcmp(option[i].name, "D")) {
+                GdkColor color;
+                char *name;
+               GetWidgetText(&option[i-5], &name);
+                gdk_color_parse(name, &color);
+                option[i].handle = (void *) (button = gtk_color_button_new_with_color(&color));
+            } else
+            button = gtk_button_new_with_label (_(option[i].name));
             SetWidgetFont(gtk_bin_get_child(GTK_BIN(button)), option[i].font);
 
             /* set button color on view board dialog */
@@ -1540,12 +1622,18 @@ if(appData.debugMode) printf("n=%d, h=%d, w=%d\n",n,height,width);
             }
 
             Pack(hbox, table, button, left, left+1, top, 0);
+            if(!strcmp(option[i].name, "D")) // color button
+            g_signal_connect (button, "color-set", G_CALLBACK (ColorCallback), (gpointer) &option[i-5]);
+            else
+            if(option[i].value == 666 && !strcmp(option[i].name, "*")) // font-assignment buttons
+            g_signal_connect (button, "clicked", G_CALLBACK (FontCallback), (gpointer) &option[i-5]);
+            else
             g_signal_connect (button, "clicked", G_CALLBACK (GenericCallback), (gpointer)(intptr_t) i + (dlgNr<<16));
            g_signal_connect(button, "button-press-event", G_CALLBACK (HelpEvent), (gpointer) option[i].name );
             option[i].handle = (void*)button;
             break;
          case ComboBox:
-            label = gtk_label_new(option[i].name);
+            label = gtk_label_new(_(option[i].name));
             /* Left Justify */
             gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
            button = gtk_event_box_new();
@@ -1621,10 +1709,6 @@ if(appData.debugMode) printf("n=%d, h=%d, w=%d\n",n,height,width);
          case Graph:
            option[i].handle = (void*) (graph = gtk_drawing_area_new());
             gtk_widget_set_size_request(graph, option[i].max, option[i].value);
-           if(0){ GtkAllocation a;
-               a.x = 0; a.y = 0; a.width = option[i].max, a.height = option[i].value;
-               gtk_widget_set_allocation(graph, &a);
-           }
             g_signal_connect (graph, "expose-event", G_CALLBACK (GraphEventProc), (gpointer) &option[i]);
            gtk_widget_add_events(GTK_WIDGET(graph), GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
             g_signal_connect (graph, "button-press-event", G_CALLBACK (GraphEventProc), (gpointer) &option[i]);
@@ -1781,8 +1865,10 @@ if(appData.debugMode) printf("n=%d, h=%d, w=%d\n",n,height,width);
        gtk_window_resize(GTK_WINDOW(dialog), wp[dlgNr]->width, wp[dlgNr]->height);
     }
 
-    for(i=0; option[i].type != EndMark; i++) if(option[i].type == Graph)
+    for(i=0; option[i].type != EndMark; i++) if(option[i].type == Graph || dlgNr == BoardWindow && option[i].handle && !appData.fixedSize) {
        gtk_widget_set_size_request(option[i].handle, -1, -1); // remove size requests after realization, so user can shrink
+       if(option[i].type == Label) gtk_label_set_ellipsize(option[i].handle, PANGO_ELLIPSIZE_END);
+    }
 
     return 1; // tells caller he must do initialization (e.g. add specific event handlers)
 }