2010-11-02 11:40:50 +00:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* Tint2conf
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Thierry lorthiois (lorthiois@bbsoft.fr) from Omega distribution
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**************************************************************************/
|
2010-03-28 22:50:26 +00:00
|
|
|
|
2010-06-14 15:08:51 +00:00
|
|
|
#include "main.h"
|
2015-07-12 15:01:44 +00:00
|
|
|
#include "strnatcmp.h"
|
2009-11-17 10:03:39 +00:00
|
|
|
#include "theme_view.h"
|
|
|
|
|
2010-03-31 22:13:24 +00:00
|
|
|
// The data columns that we export via the tree model interface
|
2010-04-03 22:52:52 +00:00
|
|
|
GtkWidget *g_theme_view;
|
2016-03-08 22:42:35 +00:00
|
|
|
GtkListStore *theme_list_store;
|
2010-03-31 22:13:24 +00:00
|
|
|
int g_width_list, g_height_list;
|
|
|
|
GtkCellRenderer *g_renderer;
|
2009-11-17 10:03:39 +00:00
|
|
|
|
2015-07-12 15:01:44 +00:00
|
|
|
gint theme_name_compare(GtkTreeModel *model,
|
|
|
|
GtkTreeIter *a,
|
|
|
|
GtkTreeIter *b,
|
|
|
|
gpointer user_data);
|
|
|
|
|
2010-03-31 22:13:24 +00:00
|
|
|
GtkWidget *create_view()
|
2009-11-17 10:03:39 +00:00
|
|
|
{
|
2010-05-04 18:47:34 +00:00
|
|
|
GtkTreeViewColumn *col;
|
|
|
|
GtkCellRenderer *renderer;
|
2010-03-31 22:13:24 +00:00
|
|
|
|
2016-03-08 22:42:35 +00:00
|
|
|
theme_list_store = gtk_list_store_new(NB_COL, G_TYPE_STRING, G_TYPE_STRING, GDK_TYPE_PIXBUF);
|
2010-03-31 22:13:24 +00:00
|
|
|
|
2016-03-20 12:08:40 +00:00
|
|
|
GtkWidget *view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(theme_list_store));
|
2010-03-31 22:13:24 +00:00
|
|
|
gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE);
|
|
|
|
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
|
|
|
|
|
2016-03-08 22:42:35 +00:00
|
|
|
g_object_unref(theme_list_store); // destroy store automatically with view
|
2010-03-31 22:13:24 +00:00
|
|
|
|
|
|
|
renderer = gtk_cell_renderer_text_new();
|
|
|
|
col = gtk_tree_view_column_new();
|
|
|
|
gtk_tree_view_column_pack_start(col, renderer, TRUE);
|
|
|
|
gtk_tree_view_column_add_attribute(col, renderer, "text", COL_THEME_FILE);
|
|
|
|
gtk_tree_view_column_set_visible(col, FALSE);
|
|
|
|
gtk_tree_view_append_column(GTK_TREE_VIEW(view),col);
|
|
|
|
|
2011-06-06 09:16:51 +00:00
|
|
|
renderer = gtk_cell_renderer_text_new();
|
|
|
|
col = gtk_tree_view_column_new();
|
|
|
|
gtk_tree_view_column_pack_start(col, renderer, TRUE);
|
|
|
|
gtk_tree_view_column_add_attribute(col, renderer, "text", COL_THEME_NAME);
|
|
|
|
gtk_tree_view_append_column(GTK_TREE_VIEW(view),col);
|
|
|
|
|
2010-03-31 22:13:24 +00:00
|
|
|
g_width_list = 200;
|
|
|
|
g_height_list = 30;
|
|
|
|
g_renderer = gtk_cell_renderer_pixbuf_new();
|
|
|
|
g_object_set(g_renderer, "xalign", 0.0, NULL);
|
|
|
|
gtk_cell_renderer_set_fixed_size(g_renderer, g_width_list, g_height_list);
|
|
|
|
// specific to gtk-2.18 or higher
|
2016-03-20 12:08:40 +00:00
|
|
|
// gtk_cell_renderer_set_padding(g_renderer, 5, 5);
|
2010-03-31 22:13:24 +00:00
|
|
|
col = gtk_tree_view_column_new();
|
|
|
|
gtk_tree_view_column_pack_start(col, g_renderer, TRUE);
|
|
|
|
gtk_tree_view_column_add_attribute(col, g_renderer, "pixbuf", COL_SNAPSHOT);
|
|
|
|
gtk_tree_view_append_column(GTK_TREE_VIEW(view),col);
|
|
|
|
|
2016-03-20 12:08:40 +00:00
|
|
|
GtkTreeSortable *sortable = GTK_TREE_SORTABLE(theme_list_store);
|
2010-05-04 18:47:34 +00:00
|
|
|
gtk_tree_sortable_set_sort_column_id(sortable, COL_THEME_FILE, GTK_SORT_ASCENDING);
|
2015-07-12 15:01:44 +00:00
|
|
|
gtk_tree_sortable_set_sort_func(sortable, COL_THEME_FILE, theme_name_compare, NULL, NULL);
|
2010-03-31 22:13:24 +00:00
|
|
|
return view;
|
2010-03-28 22:50:26 +00:00
|
|
|
}
|
|
|
|
|
2015-07-12 15:01:44 +00:00
|
|
|
gint theme_name_compare(GtkTreeModel *model,
|
|
|
|
GtkTreeIter *a,
|
|
|
|
GtkTreeIter *b,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
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);
|
2016-03-08 22:42:35 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-07-12 15:01:44 +00:00
|
|
|
gchar *name_a = path_a;
|
|
|
|
gchar *p;
|
|
|
|
for (p = name_a; *p; p++) {
|
|
|
|
if (*p == '/')
|
|
|
|
name_a = p + 1;
|
|
|
|
}
|
|
|
|
gchar *name_b = path_b;
|
|
|
|
for (p = name_b; *p; p++) {
|
|
|
|
if (*p == '/')
|
|
|
|
name_b = p + 1;
|
|
|
|
}
|
|
|
|
if (g_str_equal(name_a, name_b))
|
|
|
|
return 0;
|
|
|
|
if (g_str_equal(name_a, "tint2rc"))
|
|
|
|
return -1;
|
|
|
|
if (g_str_equal(name_b, "tint2rc"))
|
|
|
|
return 1;
|
|
|
|
gint result = strnatcasecmp(name_a, name_b);
|
|
|
|
g_free(path_a);
|
|
|
|
g_free(path_b);
|
|
|
|
return result;
|
|
|
|
}
|
2010-03-28 22:50:26 +00:00
|
|
|
|
2016-03-20 12:08:40 +00:00
|
|
|
gboolean theme_list_contains(const char *given_path)
|
2010-04-03 22:52:52 +00:00
|
|
|
{
|
2016-03-20 12:08:40 +00:00
|
|
|
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(g_theme_view));
|
2016-03-08 22:42:35 +00:00
|
|
|
GtkTreeIter iter;
|
2010-04-03 22:52:52 +00:00
|
|
|
|
2016-03-20 12:08:40 +00:00
|
|
|
gboolean have_iter = gtk_tree_model_get_iter_first(model, &iter);
|
|
|
|
while (have_iter) {
|
|
|
|
gchar *filepath;
|
|
|
|
gtk_tree_model_get(model, &iter, COL_THEME_FILE, &filepath, -1);
|
|
|
|
if (g_str_equal(filepath, given_path)) {
|
|
|
|
gtk_list_store_set(theme_list_store, &iter, COL_SNAPSHOT, NULL, -1);
|
|
|
|
g_free(filepath);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
g_free(filepath);
|
|
|
|
have_iter = gtk_tree_model_iter_next(model, &iter);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void theme_list_append(const gchar *path, const gchar *suffix)
|
|
|
|
{
|
|
|
|
if (theme_list_contains(path))
|
|
|
|
return;
|
|
|
|
|
|
|
|
GtkTreeIter iter;
|
2016-03-08 22:42:35 +00:00
|
|
|
gtk_list_store_append(theme_list_store, &iter);
|
2011-06-06 09:16:51 +00:00
|
|
|
|
2016-03-08 22:42:35 +00:00
|
|
|
gchar *name = strrchr(path, '/') + 1;
|
|
|
|
|
2016-03-20 12:08:40 +00:00
|
|
|
gchar *display_name;
|
2016-03-08 22:42:35 +00:00
|
|
|
if (suffix) {
|
2016-03-20 12:08:40 +00:00
|
|
|
display_name = g_strdup_printf("%s\n(%s)", name, suffix);
|
2016-03-08 22:42:35 +00:00
|
|
|
} else {
|
2016-03-20 12:08:40 +00:00
|
|
|
display_name = g_strdup(name);
|
2016-03-08 22:42:35 +00:00
|
|
|
}
|
2016-03-20 12:08:40 +00:00
|
|
|
gtk_list_store_set(theme_list_store, &iter, COL_THEME_FILE, path, COL_THEME_NAME, display_name, -1);
|
|
|
|
g_free(display_name);
|
2010-04-03 22:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean update_snapshot()
|
|
|
|
{
|
|
|
|
GtkTreeModel *model;
|
|
|
|
GtkTreeIter iter;
|
2015-01-28 15:31:20 +00:00
|
|
|
gboolean have_iter;
|
|
|
|
|
|
|
|
gint pixWidth = 200, pixHeight = 30;
|
2010-04-03 22:52:52 +00:00
|
|
|
|
|
|
|
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) {
|
2016-03-08 22:42:35 +00:00
|
|
|
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;
|
2010-04-03 22:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// build panel's snapshot
|
2016-03-08 22:42:35 +00:00
|
|
|
gchar *path;
|
|
|
|
gtk_tree_model_get(model, &iter,
|
|
|
|
COL_THEME_FILE, &path,
|
|
|
|
-1);
|
2010-04-03 22:52:52 +00:00
|
|
|
|
2016-03-22 21:42:15 +00:00
|
|
|
char fname[128];
|
|
|
|
sprintf(fname, "tint2-%d.jpg", (int)getpid());
|
2016-03-08 22:42:35 +00:00
|
|
|
|
2016-03-22 21:42:15 +00:00
|
|
|
gchar *snap = g_build_filename(g_get_tmp_dir(), fname, NULL);
|
2010-04-03 22:52:52 +00:00
|
|
|
g_remove(snap);
|
|
|
|
|
2016-03-08 22:42:35 +00:00
|
|
|
gchar *cmd = g_strdup_printf("tint2 -c \'%s\' -s \'%s\' 1>/dev/null 2>/dev/null", path, snap);
|
2015-01-28 15:31:20 +00:00
|
|
|
if (system(cmd) == 0) {
|
|
|
|
// load
|
|
|
|
pixbuf = gdk_pixbuf_new_from_file(snap, NULL);
|
|
|
|
if (pixbuf == NULL) {
|
|
|
|
printf("snapshot NULL : %s\n", cmd);
|
|
|
|
}
|
2010-04-03 22:52:52 +00:00
|
|
|
}
|
|
|
|
g_free(cmd);
|
2016-03-22 21:42:15 +00:00
|
|
|
|
|
|
|
g_remove(snap);
|
2016-03-08 22:42:35 +00:00
|
|
|
g_free(snap);
|
|
|
|
g_free(path);
|
2010-04-03 22:52:52 +00:00
|
|
|
|
2016-03-08 22:42:35 +00:00
|
|
|
pixWidth = MAX(pixWidth, gdk_pixbuf_get_width(pixbuf));
|
|
|
|
pixHeight = MAX(pixHeight, gdk_pixbuf_get_height(pixbuf));
|
2015-01-28 15:31:20 +00:00
|
|
|
|
2016-03-08 22:42:35 +00:00
|
|
|
gtk_list_store_set(theme_list_store, &iter, COL_SNAPSHOT, pixbuf, -1);
|
|
|
|
if (pixbuf)
|
|
|
|
g_object_unref(pixbuf);
|
2015-01-28 15:31:20 +00:00
|
|
|
have_iter = gtk_tree_model_iter_next(model, &iter);
|
2010-04-03 22:52:52 +00:00
|
|
|
}
|
2015-01-28 15:31:20 +00:00
|
|
|
|
|
|
|
gtk_cell_renderer_set_fixed_size(g_renderer, pixWidth + 30, pixHeight + 30);
|
|
|
|
|
|
|
|
return FALSE;
|
2010-04-03 22:52:52 +00:00
|
|
|
}
|