From 38ef80637fba5c8d24a0d08e53980fd4ea5ebd14 Mon Sep 17 00:00:00 2001 From: o9000 Date: Tue, 8 Mar 2016 23:42:35 +0100 Subject: [PATCH] tint2conf: Support for loading themes from /usr/share/tint2 --- src/tint2conf/main.c | 165 ++++++++++++++++++++++++------------- src/tint2conf/main.h | 2 +- src/tint2conf/theme_view.c | 82 +++++++++++------- src/tint2conf/theme_view.h | 4 +- 4 files changed, 159 insertions(+), 94 deletions(-) diff --git a/src/tint2conf/main.c b/src/tint2conf/main.c index cc98593..3abd04e 100644 --- a/src/tint2conf/main.c +++ b/src/tint2conf/main.c @@ -62,6 +62,7 @@ static void menuAddWidget(GtkUIManager *ui_manager, GtkWidget *p_widget, GtkCont static void menuAddWidget(GtkUIManager *, GtkWidget *, GtkContainer *); static void menuImport(); +static void menuImportSelected(); static void menuImportDefault(); static void menuSaveAs(); static void menuDelete(); @@ -88,6 +89,8 @@ GtkWidget *g_window; static GtkUIManager *globalUIManager = NULL; GtkWidget *tint_cmd; +GtkActionGroup *actionGroup = NULL; + static const char *global_ui = "" " " @@ -113,10 +116,12 @@ static const char *global_ui = " " " " " " + " " " " " " " " " " + " " " " " " " " @@ -130,8 +135,6 @@ int main(int argc, char **argv) textdomain(GETTEXT_PACKAGE); GtkWidget *vBox = NULL, *scrollbar = NULL; - GtkActionGroup *actionGroup; - gtk_init(&argc, &argv); #if !GLIB_CHECK_VERSION(2, 31, 0) @@ -154,7 +157,7 @@ int main(int argc, char **argv) // define main layout : container, menubar, toolbar g_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(g_window), _("Panel theming")); - gtk_window_resize(GTK_WINDOW(g_window), 800, 600); + gtk_window_resize(GTK_WINDOW(g_window), 920, 600); g_signal_connect(G_OBJECT(g_window), "destroy", G_CALLBACK(gtk_main_quit), NULL); vBox = gtk_vbox_new(FALSE, 0); gtk_container_add(GTK_CONTAINER(g_window), vBox); @@ -165,6 +168,7 @@ int main(int argc, char **argv) GtkActionEntry entries[] = { {"ThemeMenu", NULL, _("Theme"), NULL, NULL, NULL}, {"ThemeAdd", GTK_STOCK_ADD, _("_Import theme..."), "N", _("Import theme"), G_CALLBACK(menuImport)}, + {"ThemeAddIt", GTK_STOCK_ADD, _("_Import theme"), NULL, _("Import theme"), G_CALLBACK(menuImportSelected)}, {"ThemeDefault", GTK_STOCK_NEW, _("_Import default theme..."), NULL, _("Import default theme"), G_CALLBACK(menuImportDefault)}, {"ThemeSaveAs", GTK_STOCK_SAVE_AS, _("_Save as..."), NULL, _("Save theme as"), G_CALLBACK(menuSaveAs)}, {"ThemeDelete", GTK_STOCK_DELETE, _("_Delete"), NULL, _("Delete theme"), G_CALLBACK(menuDelete)}, @@ -288,6 +292,37 @@ static void menuImport() load_all_themes(); } +static void menuImportSelected() +{ + GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view)); + GtkTreeModel *model; + GtkTreeIter iter; + if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) { + char *file; + gtk_tree_model_get(model, &iter, COL_THEME_FILE, &file, -1); + + if (strstr(file, g_get_user_config_dir()) == file) + return; + gchar *pt1 = strrchr(file, '/'); + if (!pt1) + return; + pt1++; + if (!*pt1) + return; + + gchar *name = pt1; + gchar *path = g_build_filename(g_get_user_config_dir(), "tint2", name, NULL); + if (g_file_test(path, G_FILE_TEST_EXISTS)) { + g_free(path); + return; + } + copy_file(file, path); + theme_list_append(path, NULL); + } + + g_timeout_add(SNAPSHOT_TICK, (GSourceFunc)update_snapshot, NULL); +} + static void menuImportDefault() { GtkWidget *dialog = gtk_file_chooser_dialog_new(_("Save default theme as"), GTK_WINDOW(g_window), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); @@ -419,10 +454,10 @@ static gboolean view_onPopupMenu(GtkWidget *treeview, gpointer userdata) // ====== Theme selection ====== gboolean theme_selected(GtkTreeSelection *selection, - GtkTreeModel *model, - GtkTreePath *path, - gboolean path_currently_selected, - gpointer userdata) + GtkTreeModel *model, + GtkTreePath *path, + gboolean path_currently_selected, + gpointer userdata) { GtkTreeIter iter; if (gtk_tree_model_get_iter(model, &iter, path)) { @@ -432,6 +467,10 @@ gboolean theme_selected(GtkTreeSelection *selection, gchar *text = g_strdup_printf("tint2 -c %s", current_theme); gtk_entry_set_text(GTK_ENTRY(tint_cmd), text); g_free(text); + gboolean editable = strstr(current_theme, g_get_user_config_dir()) == current_theme; + gtk_action_set_sensitive(gtk_action_group_get_action(actionGroup, "ThemeProperties"), editable); + gtk_action_set_sensitive(gtk_action_group_get_action(actionGroup, "ThemeDelete"), editable); + gtk_action_set_sensitive(gtk_action_group_get_action(actionGroup, "ThemeAddIt"), !editable); } else { gtk_entry_set_text(GTK_ENTRY(tint_cmd), ""); } @@ -529,68 +568,32 @@ static void viewRowActivated(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeV // ====== Theme load/reload ====== -#if 0 -static void copy_default_themes() +static void ensure_default_theme_exists() { + // Without a user tint2rc file, copy the default gchar *path_home = g_build_filename(g_get_user_config_dir(), "tint2", "tint2rc", NULL); if (!g_file_test(path_home, G_FILE_TEST_EXISTS)) { const gchar * const * system_dirs = g_get_system_config_dirs(); - int i; - for (i = 0; system_dirs[i]; i++) { + for (int i = 0; system_dirs[i]; i++) { gchar *path = g_build_filename(system_dirs[i], "tint2", "tint2rc", NULL); if (g_file_test(path, G_FILE_TEST_EXISTS)) { copy_file(path, path_home); + break; } g_free(path); } } g_free(path_home); - - const gchar * const * data_dirs = g_get_system_data_dirs(); - int i; - for (i = 0; data_dirs[i]; i++) { - gchar *path_tint2 = g_build_filename(data_dirs[i], "tint2", NULL); - fprintf(stderr, "%s\n", path_tint2); - GDir *dir = g_dir_open(path_tint2, 0, NULL); - if (dir) { - const gchar *file_name; - while ((file_name = g_dir_read_name(dir))) { - if (!g_file_test(file_name, G_FILE_TEST_IS_DIR) && - !strstr(file_name, "backup") && - !strstr(file_name, "copy") && - !strstr(file_name, "~") && - (endswith(file_name, "tint2rc") || - endswith(file_name, ".conf"))) { - gchar *path_home = g_build_filename(g_get_user_config_dir(), "tint2", file_name, NULL); - if (!g_file_test(path_home, G_FILE_TEST_EXISTS)) { - gchar *path_usr = g_build_filename(path_tint2, file_name, NULL); - copy_file(path_usr, path_home); - g_free(path_usr); - } - g_free(path_home); - } - } - g_dir_close(dir); - } - g_free(path_tint2); - } } -#endif -static void load_all_themes() +static gboolean load_user_themes() { - // We don't do this anymore since it has proven unpopular... -#if 0 - copy_default_themes(); -#endif - - gtk_list_store_clear(GTK_LIST_STORE(g_store)); - + // Load configs from home directory gchar *tint2_config_dir = g_build_filename(g_get_user_config_dir(), "tint2", NULL); GDir *dir = g_dir_open(tint2_config_dir, 0, NULL); if (dir == NULL) { g_free(tint2_config_dir); - return; + return FALSE; } gboolean found_theme = FALSE; @@ -603,13 +606,60 @@ static void load_all_themes() (endswith(file_name, "tint2rc") || endswith(file_name, ".conf"))) { found_theme = TRUE; - gchar *name = g_build_filename(tint2_config_dir, file_name, NULL); - custom_list_append(name); - g_free(name); + gchar *path = g_build_filename(tint2_config_dir, file_name, NULL); + theme_list_append(path, NULL); + g_free(path); } } + g_dir_close(dir); + g_free(tint2_config_dir); - if (found_theme) { + return found_theme; +} + +static gboolean load_system_themes() +{ + gboolean found_theme = FALSE; + // Load configs from /usr etc + const gchar * const * data_dirs = g_get_system_data_dirs(); + for (int i = 0; data_dirs[i]; i++) { + gchar *path_tint2 = g_build_filename(data_dirs[i], "tint2", NULL); + GDir *dir = g_dir_open(path_tint2, 0, NULL); + if (dir) { + const gchar *file_name; + while ((file_name = g_dir_read_name(dir))) { + if (!g_file_test(file_name, G_FILE_TEST_IS_DIR) && + !strstr(file_name, "backup") && + !strstr(file_name, "copy") && + !strstr(file_name, "~") && + (endswith(file_name, "tint2rc") || + endswith(file_name, ".conf"))) { + found_theme = TRUE; + gchar *path = g_build_filename(path_tint2, file_name, NULL); + theme_list_append(path, data_dirs[i]); + g_free(path); + } + } + g_dir_close(dir); + } + g_free(path_tint2); + } + return found_theme; +} + +static void load_all_themes() +{ + ensure_default_theme_exists(); + + gtk_list_store_clear(GTK_LIST_STORE(theme_list_store)); + + gboolean found_themes = FALSE; + if (load_user_themes()) + found_themes = TRUE; + if (load_system_themes()) + found_themes = TRUE; + + if (found_themes) { select_first_theme(); GtkTreeIter iter; @@ -619,15 +669,12 @@ static void load_all_themes() model = gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view)); have_iter = gtk_tree_model_get_iter_first(model, &iter); while (have_iter) { - gtk_list_store_set(g_store, &iter, COL_SNAPSHOT, NULL, -1); + gtk_list_store_set(theme_list_store, &iter, COL_SNAPSHOT, NULL, -1); have_iter = gtk_tree_model_iter_next(model, &iter); } g_timeout_add(SNAPSHOT_TICK, (GSourceFunc)update_snapshot, NULL); } - - g_dir_close(dir); - g_free(tint2_config_dir); } static void refresh_current_theme() @@ -638,7 +685,7 @@ static void refresh_current_theme() sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(g_theme_view)); if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(sel), &model, &iter)) { - gtk_list_store_set(g_store, &iter, COL_SNAPSHOT, NULL, -1); + gtk_list_store_set(theme_list_store, &iter, COL_SNAPSHOT, NULL, -1); } g_timeout_add(SNAPSHOT_TICK, (GSourceFunc)update_snapshot, NULL); diff --git a/src/tint2conf/main.h b/src/tint2conf/main.h index e7e4d6c..b9622f1 100644 --- a/src/tint2conf/main.h +++ b/src/tint2conf/main.h @@ -16,4 +16,4 @@ #define SNAPSHOT_TICK 190 gboolean update_snapshot(); -void menuApply(); \ No newline at end of file +void menuApply(); diff --git a/src/tint2conf/theme_view.c b/src/tint2conf/theme_view.c index 1c324b8..156311f 100644 --- a/src/tint2conf/theme_view.c +++ b/src/tint2conf/theme_view.c @@ -24,7 +24,7 @@ // The data columns that we export via the tree model interface GtkWidget *g_theme_view; -GtkListStore *g_store; +GtkListStore *theme_list_store; int g_width_list, g_height_list; GtkCellRenderer *g_renderer; @@ -39,13 +39,13 @@ GtkWidget *create_view() GtkCellRenderer *renderer; GtkWidget *view; - g_store = gtk_list_store_new(NB_COL, G_TYPE_STRING, G_TYPE_STRING, GDK_TYPE_PIXBUF); + theme_list_store = gtk_list_store_new(NB_COL, G_TYPE_STRING, G_TYPE_STRING, GDK_TYPE_PIXBUF); - view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(g_store)); + view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(theme_list_store)); gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE); gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE); - g_object_unref(g_store); // destroy store automatically with view + g_object_unref(theme_list_store); // destroy store automatically with view renderer = gtk_cell_renderer_text_new(); col = gtk_tree_view_column_new(); @@ -73,7 +73,7 @@ GtkWidget *create_view() gtk_tree_view_append_column(GTK_TREE_VIEW(view),col); GtkTreeSortable *sortable; - sortable = GTK_TREE_SORTABLE(g_store); + sortable = GTK_TREE_SORTABLE(theme_list_store); gtk_tree_sortable_set_sort_column_id(sortable, COL_THEME_FILE, GTK_SORT_ASCENDING); gtk_tree_sortable_set_sort_func(sortable, COL_THEME_FILE, theme_name_compare, NULL, NULL); return view; @@ -87,6 +87,15 @@ gint theme_name_compare(GtkTreeModel *model, gchar *path_a, *path_b; gtk_tree_model_get(model, a, COL_THEME_FILE, &path_a, -1); gtk_tree_model_get(model, b, COL_THEME_FILE, &path_b, -1); + + gboolean home_a = strstr(path_a, g_get_user_config_dir()) == path_a; + gboolean home_b = strstr(path_b, g_get_user_config_dir()) == path_b; + + if (home_a && !home_b) + return -1; + if (!home_a && home_b) + return 1; + gchar *name_a = path_a; gchar *p; for (p = name_a; *p; p++) { @@ -110,15 +119,23 @@ gint theme_name_compare(GtkTreeModel *model, return result; } -void custom_list_append(const gchar *path) +void theme_list_append(const gchar *path, const gchar *suffix) { - GtkTreeIter iter; + GtkTreeIter iter; - gtk_list_store_append(g_store, &iter); - gtk_list_store_set(g_store, &iter, COL_THEME_FILE, path, -1); + gtk_list_store_append(theme_list_store, &iter); + gtk_list_store_set(theme_list_store, &iter, COL_THEME_FILE, path, -1); - gchar *name = g_strdup(strrchr(path, '/') + 1); - gtk_list_store_set(g_store, &iter, COL_THEME_NAME, name, -1); + gchar *name = strrchr(path, '/') + 1; + + gchar *full_name; + if (suffix) { + full_name = g_strdup_printf("%s\n(%s)", name, suffix); + } else { + full_name = g_strdup(name); + } + gtk_list_store_set(theme_list_store, &iter, COL_THEME_NAME, full_name, -1); + g_free(full_name); } @@ -126,7 +143,6 @@ gboolean update_snapshot() { GtkTreeModel *model; GtkTreeIter iter; - GdkPixbuf *icon; gboolean have_iter; gint pixWidth = 200, pixHeight = 30; @@ -134,20 +150,27 @@ gboolean update_snapshot() model = gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view)); have_iter = gtk_tree_model_get_iter_first(model, &iter); while (have_iter) { - gtk_tree_model_get(model, &iter, COL_SNAPSHOT, &icon, -1); - if (icon != NULL) { - g_object_unref(icon); + GdkPixbuf *pixbuf; + gtk_tree_model_get(model, &iter, COL_SNAPSHOT, &pixbuf, -1); + if (pixbuf) { + pixWidth = MAX(pixWidth, gdk_pixbuf_get_width(pixbuf)); + pixHeight = MAX(pixHeight, gdk_pixbuf_get_height(pixbuf)); + g_object_unref(pixbuf); + have_iter = gtk_tree_model_iter_next(model, &iter); + continue; } // build panel's snapshot - GdkPixbuf *pixbuf = NULL; - gchar *name, *snap, *cmd; + gchar *path; + gtk_tree_model_get(model, &iter, + COL_THEME_FILE, &path, + -1); - snap = g_build_filename(g_get_user_config_dir(), "tint2", "snap.jpg", NULL); + + gchar *snap = g_build_filename(g_get_user_config_dir(), "tint2", "snap.jpg", NULL); g_remove(snap); - gtk_tree_model_get(model, &iter, COL_THEME_FILE, &name, -1); - cmd = g_strdup_printf("tint2 -c \'%s\' -s \'%s\'", name, snap); + gchar *cmd = g_strdup_printf("tint2 -c \'%s\' -s \'%s\' 1>/dev/null 2>/dev/null", path, snap); if (system(cmd) == 0) { // load pixbuf = gdk_pixbuf_new_from_file(snap, NULL); @@ -155,18 +178,16 @@ gboolean update_snapshot() printf("snapshot NULL : %s\n", cmd); } } - g_free(snap); g_free(cmd); - g_free(name); + g_free(snap); + g_free(path); - gint w, h; - w = gdk_pixbuf_get_width(pixbuf); - h = gdk_pixbuf_get_height(pixbuf); - pixWidth = w > pixWidth ? w : pixWidth; - pixHeight = h > pixHeight ? h : pixHeight; - - gtk_list_store_set(g_store, &iter, COL_SNAPSHOT, pixbuf, -1); + pixWidth = MAX(pixWidth, gdk_pixbuf_get_width(pixbuf)); + pixHeight = MAX(pixHeight, gdk_pixbuf_get_height(pixbuf)); + gtk_list_store_set(theme_list_store, &iter, COL_SNAPSHOT, pixbuf, -1); + if (pixbuf) + g_object_unref(pixbuf); have_iter = gtk_tree_model_iter_next(model, &iter); } @@ -174,6 +195,3 @@ gboolean update_snapshot() return FALSE; } - - - diff --git a/src/tint2conf/theme_view.h b/src/tint2conf/theme_view.h index 2404828..3a0d771 100644 --- a/src/tint2conf/theme_view.h +++ b/src/tint2conf/theme_view.h @@ -5,12 +5,12 @@ #include extern GtkWidget *g_theme_view; -extern GtkListStore *g_store; +extern GtkListStore *theme_list_store; enum { COL_THEME_FILE = 0, COL_THEME_NAME, COL_SNAPSHOT, NB_COL, }; GtkWidget *create_view(); -void custom_list_append(const gchar *path); +void theme_list_append(const gchar *path, const gchar *suffix); #endif