Fix #926: Use system background color in window mode
[geeqie.git] / src / main.c
index 6b19d98..c117f71 100644 (file)
@@ -38,7 +38,9 @@
 #include "filedata.h"
 #include "filefilter.h"
 #include "history_list.h"
+#include "image.h"
 #include "image-overlay.h"
+#include "img-view.h"
 #include "layout.h"
 #include "layout_image.h"
 #include "layout_util.h"
@@ -58,6 +60,7 @@
 #include "histogram.h"
 #include "pixbuf_util.h"
 #include "glua.h"
+#include "whereami.h"
 
 #ifdef HAVE_CLUTTER
 #include <clutter-gtk/clutter-gtk.h>
@@ -269,7 +272,9 @@ gchar *gq_helpdir;
 gchar *gq_htmldir;
 gchar *gq_app_dir;
 gchar *gq_bin_dir;
+gchar *gq_executable_path;
 gchar *desktop_file_template;
+gchar *instance_identifier;
 
 /*
  *-----------------------------------------------------------------------------
@@ -1075,6 +1080,8 @@ static void exit_program_final(void)
        LayoutWindow *lw = NULL;
        GList *list;
        LayoutWindow *tmp_lw;
+       gchar *archive_dir;
+       GFile *archive_file;
 
         /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
        layout_editors_reload_finish();
@@ -1107,6 +1114,27 @@ static void exit_program_final(void)
                layout_free(lw);
                }
 
+       /* Delete any files/folders in /tmp that have been created by the open archive function */
+       archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, NULL);
+       if (isdir(archive_dir))
+               {
+               archive_file = g_file_new_for_path(archive_dir);
+               rmdir_recursive(archive_file, NULL, NULL);
+               g_free(archive_dir);
+               g_object_unref(archive_file);
+               }
+
+       /* If there are still sub-dirs created by another instance, this will fail
+        * but that does not matter */
+       archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, NULL);
+       if (isdir(archive_dir))
+               {
+               archive_file = g_file_new_for_path(archive_dir);
+               g_file_delete(archive_file, NULL, NULL);
+               g_free(archive_dir);
+               g_object_unref(archive_file);
+               }
+
        secure_close(command_line->ssi);
 
        gtk_main_quit();
@@ -1213,6 +1241,46 @@ static void setup_sigbus_handler(void)
 #endif
 }
 
+static void set_theme_bg_color()
+{
+#if GTK_CHECK_VERSION(3,0,0)
+       GdkRGBA bg_color;
+       GdkColor theme_color;
+       GtkStyleContext *style_context;
+       GList *work;
+       LayoutWindow *lw;
+
+       if (!options->image.use_custom_border_color)
+               {
+               work = layout_window_list;
+               lw = work->data;
+
+               style_context = gtk_widget_get_style_context(lw->window);
+               gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
+
+               theme_color.red = bg_color.red * 65535;
+               theme_color.green = bg_color.green * 65535;
+               theme_color.blue = bg_color.blue * 65535;
+
+               while (work)
+                       {
+                       lw = work->data;
+                       image_background_set_color(lw->image, &theme_color);
+                       work = work->next;
+                       }
+               }
+
+       view_window_colors_update();
+#endif
+}
+
+static gboolean theme_change_cb(GObject *gobject, GParamSpec *pspec, gpointer data)
+{
+       set_theme_bg_color();
+
+       return FALSE;
+}
+
 /**
  * @brief Set up the application paths
  * 
@@ -1222,22 +1290,20 @@ static void setup_sigbus_handler(void)
  * They are now variables, all defined relative to one level above the
  * directory that the executable is run from.
  */
-static void create_application_paths()
+static void create_application_paths(gchar *argv[])
 {
-       gchar buf[1024];
        gchar *dirname;
        gchar *tmp;
+       gint length;
+       gchar *path;
 
-       memset(buf, 0, sizeof(buf));
-       if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) < 0)
-               {
-               /* There was an error. Perhaps the path does not exist
-                * or the buffer is not big enough. */
-               log_printf("Can't get path from /proc/self/exe");
-               exit(1);
-               }
+       length = wai_getExecutablePath(NULL, 0, NULL);
+       path = (gchar *)malloc(length + 1);
+       wai_getExecutablePath(path, length, NULL);
+       path[length] = '\0';
 
-       dirname = g_path_get_dirname(buf); // default is /usr/bin/
+       gq_executable_path = g_strdup(path);
+       dirname = g_path_get_dirname(gq_executable_path); // default is /usr/bin/
        gq_prefix = g_path_get_dirname(dirname);
 
        gq_localedir = g_build_filename(gq_prefix, "share", "locale", NULL);
@@ -1250,6 +1316,7 @@ static void create_application_paths()
 
        g_free(tmp);
        g_free(dirname);
+       g_free(path);
 }
 
 gint main(gint argc, gchar *argv[])
@@ -1260,6 +1327,7 @@ gint main(gint argc, gchar *argv[])
        gboolean disable_clutter = FALSE;
        gboolean single_dir = TRUE;
        LayoutWindow *lw;
+       GtkSettings *default_settings;
 
 #ifdef HAVE_GTHREAD
 #if !GLIB_CHECK_VERSION(2,32,0)
@@ -1273,7 +1341,7 @@ gint main(gint argc, gchar *argv[])
        /* init execution time counter (debug only) */
        init_exec_time();
 
-       create_application_paths();
+       create_application_paths(argv);
 
        /* setup locale, i18n */
        setlocale(LC_ALL, "");
@@ -1347,6 +1415,9 @@ gint main(gint argc, gchar *argv[])
                options->disable_gpu = TRUE;
                }
 
+       /* Generate a unique identifier used by the open archive function */
+       instance_identifier = g_strdup_printf("%x", g_random_int());
+
        DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
        /* these functions don't depend on config file */
        mkdir_if_not_exists(get_rc_dir());
@@ -1547,6 +1618,10 @@ gint main(gint argc, gchar *argv[])
                marks_load();
        }
 
+       default_settings = gtk_settings_get_default();
+       g_signal_connect(default_settings, "notify::gtk-theme-name", G_CALLBACK(theme_change_cb), NULL);
+       set_theme_bg_color();
+
        DEBUG_1("%s main: gtk_main", get_exec_time());
        gtk_main();
 #ifdef HAVE_GTHREAD