[gnome-commander/gcmd-1-10] Get rid of gnome_icon_entry and use gtk_file_chooser instead



commit 583e76d7ea02a59931b796dcd684b29470ba761c
Author: Uwe Scholz <u scholz83 gmx de>
Date:   Tue May 26 22:44:58 2020 +0200

    Get rid of gnome_icon_entry and use gtk_file_chooser instead
    
    I used commit 6b663ab53 in the https://gitlab.gnome.org/GNOME/anjuta
    repository for help here.

 libgcmd/libgcmd-deps.h                  |   3 +-
 libgcmd/libgcmd-widget-factory.cc       | 106 ++++++++++++++++++++++++++++----
 libgcmd/libgcmd-widget-factory.h        |   2 +-
 src/dialogs/gnome-cmd-options-dialog.cc |  25 ++++----
 4 files changed, 108 insertions(+), 28 deletions(-)
---
diff --git a/libgcmd/libgcmd-deps.h b/libgcmd/libgcmd-deps.h
index 70fab90c..4b9717cd 100644
--- a/libgcmd/libgcmd-deps.h
+++ b/libgcmd/libgcmd-deps.h
@@ -1,4 +1,4 @@
-/** 
+/**
  * @file libgcmd-deps.h
  * @copyright (C) 2001-2006 Marcus Bjurman\n
  * @copyright (C) 2007-2012 Piotr Eljasiak\n
@@ -30,7 +30,6 @@
 #include <libgnome/gnome-config.h>
 #include <libgnome/gnome-exec.h>
 #include <libgnomeui/gnome-app-helper.h>
-#include <libgnomeui/gnome-icon-entry.h>
 #include <libgnomevfs/gnome-vfs.h>
 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
 
diff --git a/libgcmd/libgcmd-widget-factory.cc b/libgcmd/libgcmd-widget-factory.cc
index 1e105854..1f80a13b 100644
--- a/libgcmd/libgcmd-widget-factory.cc
+++ b/libgcmd/libgcmd-widget-factory.cc
@@ -76,7 +76,7 @@ GtkWidget *create_tabframe (GtkWidget *parent)
 
 GtkWidget *create_space_frame (GtkWidget *parent, gint space)
 {
-    GtkWidget *frame = create_frame (parent, NULL, space);
+    GtkWidget *frame = create_frame (parent, nullptr, space);
     gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
     return frame;
 }
@@ -310,15 +310,95 @@ GtkWidget *create_color_button (GtkWidget *parent, const gchar *name)
 }
 
 
-GtkWidget *create_icon_entry (GtkWidget *parent, const gchar *name, const gchar *icon_path)
+static void preview_update (GtkFileChooser *fileChooser, GtkImage *preview)
 {
-    GtkWidget *icon_entry = gnome_icon_entry_new (NULL, NULL);
-    g_object_ref (icon_entry);
-    g_object_set_data_full (G_OBJECT (parent), name, icon_entry, g_object_unref);
-    gtk_widget_show (icon_entry);
-    if (icon_path)
-        gnome_icon_entry_set_filename (GNOME_ICON_ENTRY (icon_entry), icon_path);
-    return icon_entry;
+    char *filename;
+    GdkPixbuf *pixbuf;
+
+    filename = gtk_file_chooser_get_preview_filename (fileChooser);
+    if (filename)
+    {
+        pixbuf = gdk_pixbuf_new_from_file (filename, nullptr);
+
+        gtk_file_chooser_set_preview_widget_active (fileChooser, pixbuf != nullptr);
+
+        if (pixbuf)
+        {
+            gtk_image_set_from_pixbuf (preview, pixbuf);
+            g_object_unref (pixbuf);
+        }
+
+        g_free (filename);
+    }
+}
+
+
+static void icon_button_clicked (GtkButton *button, const gchar* iconPath)
+{
+    GtkWidget *dialog;
+    GtkFileFilter *filter;
+    GtkWidget *preview;
+    int responseValue;
+
+    dialog = gtk_file_chooser_dialog_new (_("Select an Image File"),
+                                            GTK_WINDOW (gtk_widget_get_ancestor ((GtkWidget*)button, 
GTK_TYPE_WINDOW)),
+                                            GTK_FILE_CHOOSER_ACTION_OPEN,
+                                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                            GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+                                            nullptr);
+    if (iconPath)
+    {
+        auto folderPath = g_path_get_dirname(iconPath);
+        gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), folderPath);
+        g_free(folderPath);
+    }
+    else
+    {
+        gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), PIXMAPS_DIR);
+    }
+
+    filter = gtk_file_filter_new ();
+    gtk_file_filter_add_pixbuf_formats (filter);
+    gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);
+
+    preview = gtk_image_new ();
+    gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER (dialog), preview);
+    g_signal_connect (dialog, "update-preview", G_CALLBACK (preview_update), preview);
+
+    responseValue = gtk_dialog_run (GTK_DIALOG (dialog));
+
+    if (responseValue == GTK_RESPONSE_ACCEPT)
+    {
+        auto icon_path = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+        gtk_image_set_from_file (GTK_IMAGE (gtk_button_get_image (button)), icon_path);
+        gtk_button_set_label (button, icon_path == nullptr ? _("Choose Icon") : nullptr);
+        gtk_widget_set_tooltip_text(GTK_WIDGET(button), icon_path);
+    }
+
+    gtk_widget_destroy (dialog);
+}
+
+
+GtkWidget *create_icon_button_widget (GtkWidget *parent, const gchar *name, const gchar *iconPath)
+{
+    auto image = gtk_image_new ();
+    auto gtkButton = gtk_button_new ();
+    if (iconPath && *iconPath != '\0')
+    {
+        gtk_image_set_from_file (GTK_IMAGE (image), iconPath);
+        gtk_widget_set_tooltip_text(gtkButton, iconPath);
+    }
+    else
+    {
+        gtk_button_set_label (GTK_BUTTON(gtkButton), _("Choose Icon"));
+    }
+    gtk_button_set_image (GTK_BUTTON (gtkButton), image);
+    g_signal_connect (gtkButton, "clicked", G_CALLBACK (icon_button_clicked), (gpointer) iconPath);
+    g_object_ref (gtkButton);
+    g_object_set_data_full (G_OBJECT (parent), name, gtkButton, g_object_unref);
+    gtk_widget_show (gtkButton);
+
+    return gtkButton;
 }
 
 
@@ -380,7 +460,7 @@ GtkWidget *create_clist (GtkWidget *parent, const gchar *name, gint cols, gint r
 {
     GtkWidget *sw, *clist;
 
-    sw = gtk_scrolled_window_new (NULL, NULL);
+    sw = gtk_scrolled_window_new (nullptr, nullptr);
     g_object_ref (sw);
     g_object_set_data_full (G_OBJECT (parent), "sw", sw, g_object_unref);
     gtk_widget_show (sw);
@@ -488,7 +568,7 @@ GtkWidget *create_progress_bar (GtkWidget *parent)
 
 GtkWidget *create_sw (GtkWidget *parent)
 {
-    GtkWidget *scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
+    GtkWidget *scrolledwindow = gtk_scrolled_window_new (nullptr, nullptr);
     g_object_ref (scrolledwindow);
     g_object_set_data_full (G_OBJECT (parent), "scrolledwindow", scrolledwindow, g_object_unref);
     gtk_widget_show (scrolledwindow);
@@ -510,8 +590,8 @@ void progress_bar_update (GtkWidget *pbar, gint max)
 const char *get_entry_text (GtkWidget *parent, const gchar *entry_name)
 {
     GtkWidget *entry = lookup_widget (parent, entry_name);
-    if (!entry) return NULL;
-    if (!GTK_IS_ENTRY (entry)) return NULL;
+    if (!entry) return nullptr;
+    if (!GTK_IS_ENTRY (entry)) return nullptr;
 
     return gtk_entry_get_text (GTK_ENTRY (entry));
 }
diff --git a/libgcmd/libgcmd-widget-factory.h b/libgcmd/libgcmd-widget-factory.h
index 8c4b7860..55c34ccb 100644
--- a/libgcmd/libgcmd-widget-factory.h
+++ b/libgcmd/libgcmd-widget-factory.h
@@ -105,7 +105,7 @@ GtkWidget *create_spin (GtkWidget *parent, const gchar *name, gint min, gint max
 
 GtkWidget *create_color_button (GtkWidget *parent, const gchar *name);
 
-GtkWidget *create_icon_entry (GtkWidget *parent, const gchar *name, const gchar *icon_path);
+GtkWidget *create_icon_button_widget (GtkWidget *parent, const gchar *name, const gchar *icon_path);
 
 GtkWidget *create_scale (GtkWidget *parent, const gchar *name, gint value, gint min, gint max);
 
diff --git a/src/dialogs/gnome-cmd-options-dialog.cc b/src/dialogs/gnome-cmd-options-dialog.cc
index 6e2b7b67..0dd39977 100644
--- a/src/dialogs/gnome-cmd-options-dialog.cc
+++ b/src/dialogs/gnome-cmd-options-dialog.cc
@@ -1318,7 +1318,7 @@ static void get_app_dialog_values (GtkWidget *dialog, gchar **name, gchar **cmd,
 {
     GtkWidget *name_entry = lookup_widget (dialog, "name_entry");
     GtkWidget *cmd_entry = lookup_widget (dialog, "cmd_entry");
-    GtkWidget *icon_entry = lookup_widget (dialog, "icon_entry");
+    GtkWidget *iconWidget = lookup_widget (dialog, "icon_entry");
     GtkWidget *pattern_entry = lookup_widget (dialog, "pattern_entry");
     GtkWidget *target_files = lookup_widget (dialog, "show_for_all_files");
     GtkWidget *target_dirs = lookup_widget (dialog, "show_for_all_dirs");
@@ -1329,7 +1329,8 @@ static void get_app_dialog_values (GtkWidget *dialog, gchar **name, gchar **cmd,
 
     *name = (gchar *) gtk_entry_get_text (GTK_ENTRY (name_entry));
     *cmd = (gchar *) gtk_entry_get_text (GTK_ENTRY (cmd_entry));
-    *icon_path = (gchar *) gnome_icon_entry_get_filename (GNOME_ICON_ENTRY (icon_entry));
+    // Get icon_path string
+    g_object_get (G_OBJECT (gtk_button_get_image (GTK_BUTTON (iconWidget))), "file", icon_path, NULL);
     *pattern_string = NULL;
     if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (target_files)))
         *target = APP_TARGET_ALL_FILES;
@@ -1453,8 +1454,9 @@ static GtkWidget *create_app_dialog (GnomeCmdApp *app, GtkSignalFunc on_ok, GtkS
     entry = create_entry (dialog, "cmd_entry", s);
     table_add (table, entry, 1, 1, (GtkAttachOptions) (GTK_EXPAND|GTK_FILL));
 
-    if (app) s = gnome_cmd_app_get_icon_path (app);
-    entry = create_icon_entry (dialog, "icon_entry", s);
+    s = gnome_cmd_app_get_icon_path (app);
+    entry = create_icon_button_widget (dialog, "icon_entry", s);
+
     table_add (table, entry, 1, 2, GTK_FILL);
 
 
@@ -1804,7 +1806,7 @@ inline void get_device_dialog_values (GtkWidget *dialog, gchar **alias, gchar **
     GtkWidget *alias_entry = lookup_widget (dialog, "alias_entry");
     GtkWidget *device_entry = lookup_widget (dialog, "device_entry");
     GtkWidget *mountp_entry = lookup_widget (dialog, "mountp_entry");
-    GtkWidget *icon_entry = lookup_widget (dialog, "device_iconentry");
+    GtkWidget *iconWidget = lookup_widget (dialog, "device_iconentry");
 
     gchar* device = (gchar *) gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (device_entry));
     gchar* mountp = (gchar *) gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (mountp_entry));
@@ -1812,7 +1814,9 @@ inline void get_device_dialog_values (GtkWidget *dialog, gchar **alias, gchar **
     *alias = (gchar *) gtk_entry_get_text (GTK_ENTRY (alias_entry));
     *device_utf8 = g_filename_to_utf8(device, -1, nullptr, nullptr, nullptr);
     *mountp_utf8 = g_filename_to_utf8(mountp, -1, nullptr, nullptr, nullptr);
-    *icon_path = gnome_icon_entry_get_filename (GNOME_ICON_ENTRY (icon_entry));
+    // Get device_iconentry path
+    g_object_get (G_OBJECT (gtk_button_get_image (GTK_BUTTON (iconWidget))), "file", icon_path, NULL);
+
 
     g_free(device);
     g_free(mountp);
@@ -1868,7 +1872,6 @@ static GtkWidget *create_device_dialog (GnomeCmdConDevice *dev, GtkSignalFunc on
     GtkWidget *table, *entry, *label;
     GtkWidget *dialog;
     const gchar *s = NULL;
-    gchar *icon_dir;
 
     dialog = gnome_cmd_dialog_new ("");
     g_object_ref (dialog);
@@ -1903,11 +1906,9 @@ static GtkWidget *create_device_dialog (GnomeCmdConDevice *dev, GtkSignalFunc on
     entry = create_directory_chooser_button (dialog, "mountp_entry", s);
     table_add (table, entry, 1, 2, (GtkAttachOptions) (GTK_EXPAND|GTK_FILL));
 
-    if (dev) s = gnome_cmd_con_device_get_icon_path (dev);
-    entry = create_icon_entry (dialog, "device_iconentry", s);
-    icon_dir = g_build_filename (PIXMAPS_DIR, "device-icons", NULL);
-    gnome_icon_entry_set_pixmap_subdir (GNOME_ICON_ENTRY (entry), icon_dir);
-    g_free (icon_dir);
+    s = gnome_cmd_con_device_get_icon_path (dev);
+    entry = create_icon_button_widget (dialog, "device_iconentry", s);
+
     table_add (table, entry, 1, 3, GTK_FILL);
 
     gnome_cmd_dialog_add_button (


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]