From: Thierry Lorthiois Date: Sun, 28 Mar 2010 22:50:26 +0000 (+0000) Subject: some changed in tint2conf theme switcher. not yet ready. X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=e92c129cb3795408bcb0a305e4d52c03e39af56b;p=chaz%2Ftint2 some changed in tint2conf theme switcher. not yet ready. --- diff --git a/src/tint.c b/src/tint.c index 1042ce5..235c2e2 100644 --- a/src/tint.c +++ b/src/tint.c @@ -170,7 +170,6 @@ void get_snapshot(const char *path) panel->area.width = server.monitor[0].width; panel->temp_pmap = XCreatePixmap(server.dsp, server.root_win, panel->area.width, panel->area.height, server.depth); - refresh(&panel->area); Imlib_Image img = NULL; @@ -180,6 +179,7 @@ void get_snapshot(const char *path) imlib_context_set_image(img); imlib_save_image(path); imlib_free_image(); + XFreePixmap(server.dsp, panel->temp_pmap); } diff --git a/src/tint2conf/main.c b/src/tint2conf/main.c index 874c7e6..e8c385b 100644 --- a/src/tint2conf/main.c +++ b/src/tint2conf/main.c @@ -38,16 +38,26 @@ char *g_path_config = NULL; char *g_path_dir = NULL; char *g_default_theme = NULL; -int g_width; -int g_height; +int g_width, g_height; +int g_width_list, g_height_list; GtkWidget *g_window; GtkWidget *g_theme_view; GtkCellRenderer *g_renderer; +CustomList *customlist; + +// The data columns that we export via the tree model interface +GtkListStore *g_store; +enum { COL_THEME_FILE = 0, COL_SNAPSHOT, NB_COL, }; +static void custom_list_append(const gchar *name); +#define STEP 0.01 +static gboolean increasing = TRUE; +//gboolean increase_timeout(GtkCellRenderer *renderer); static GtkUIManager *globalUIManager = NULL; static void menuAddWidget (GtkUIManager *, GtkWidget *, GtkContainer *); +static GtkWidget *create_view(); // action on menus static void menuAdd (GtkWindow * parent); @@ -180,6 +190,129 @@ int main (int argc, char ** argv) } +GtkWidget *create_view() +{ + GtkTreeViewColumn *col; + GtkCellRenderer *renderer; + GtkWidget *view; + + g_store = gtk_list_store_new(NB_COL, G_TYPE_STRING, GDK_TYPE_PIXBUF); +// g_store = gtk_list_store_new(NB_COL, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_FLOAT); + + view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(g_store)); + gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE); + //gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(view), TRUE); + + g_object_unref(g_store); // destroy store automatically with view + + 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_title (col, "Theme"); + gtk_tree_view_append_column(GTK_TREE_VIEW(view),col); + + 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); + 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_column_set_title(col, "Snapshot"); + gtk_tree_view_append_column(GTK_TREE_VIEW(view),col); +/* + renderer = custom_list_new(); + col = gtk_tree_view_column_new(); + gtk_tree_view_column_pack_start (col, renderer, TRUE); + gtk_tree_view_column_add_attribute (col, renderer, "snapshot", COL_SNAPSHOT); + gtk_tree_view_column_set_title (col, "Snapshot"); + gtk_tree_view_append_column(GTK_TREE_VIEW(view),col); +*/ + //g_timeout_add(50, (GSourceFunc) increase_timeout, NULL); + + return view; +} + + +/* +gboolean increase_timeout (GtkCellRenderer *renderer) +{ + GtkTreeIter iter; + gfloat perc = 0.0; + //gchar buf[20]; + + gtk_tree_model_get_iter_first(GTK_TREE_MODEL(g_store), &iter); + + gtk_tree_model_get (GTK_TREE_MODEL(g_store), &iter, COL_SNAPSHOT, &perc, -1); + + if ( perc > (1.0-STEP) || (perc < STEP && perc > 0.0) ) + { + increasing = (!increasing); + } + + if (increasing) + perc = perc + STEP; + else + perc = perc - STEP; + + //g_snprintf(buf, sizeof(buf), "%u %%", (guint)(perc*100)); + + gtk_list_store_set (g_store, &iter, COL_SNAPSHOT, perc, -1); + + return TRUE; +} +*/ +/***************************************************************************** + * + * custom_list_append_record: Empty lists are boring. This function can + * be used in your own code to add rows to the + * list. Note how we emit the "row-inserted" + * signal after we have appended the row + * internally, so the tree view and other + * interested objects know about the new row. + * + *****************************************************************************/ + +void custom_list_append(const gchar *name) +{ + GtkTreeIter iter; + gchar *snap, *cmd; + gint pixWidth, pixHeight; + gboolean changeSize = FALSE; + GdkPixbuf *icon; + + // build panel's snapshot + snap = g_build_filename (g_get_user_config_dir(), "tint2", "snap.jpg", NULL); + cmd = g_strdup_printf("tint2 -c %s -s %s", name, snap); + system(cmd); + g_free(cmd); + + // load + icon = gdk_pixbuf_new_from_file(snap, NULL); + g_free(snap); + if (!icon) + return; + + pixWidth = gdk_pixbuf_get_width(icon); + pixHeight = gdk_pixbuf_get_height(icon); + if (g_width_list < pixWidth) { + g_width_list = pixWidth; + changeSize = TRUE; + } + if (g_height_list < (pixHeight+6)) { + g_height_list = pixHeight+6; + changeSize = TRUE; + } + if (changeSize) + gtk_cell_renderer_set_fixed_size(g_renderer, g_width_list, g_height_list); + + gtk_list_store_append(g_store, &iter); + gtk_list_store_set(g_store, &iter, COL_THEME_FILE, name, COL_SNAPSHOT, icon, -1); +} + + static void menuAddWidget (GtkUIManager * p_uiManager, GtkWidget * p_widget, GtkContainer * p_box) { gtk_box_pack_start(GTK_BOX(p_box), p_widget, FALSE, FALSE, 0); @@ -234,7 +367,7 @@ static void menuAdd (GtkWindow *parent) name = g_strdup(pt1); path = g_build_filename (g_get_user_config_dir(), "tint2", name, NULL); copy_file(file, path); - add_to_list(g_theme_view, path); + custom_list_append(path); g_free(path); g_free(name); } @@ -275,17 +408,17 @@ static void menuDelete (void) GtkTreeSelection *sel; GtkTreeIter iter; GtkTreeModel *model; - char *value; + char *file; 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_tree_model_get(model, &iter, COL_TEXT, &value, -1); + gtk_tree_model_get(model, &iter, COL_THEME_FILE, &file, -1); gtk_tree_selection_unselect_all(sel); // remove (gui and disk) gtk_list_store_remove(GTK_LIST_STORE(model), &iter); - g_remove(value); - g_free(value); + g_remove(file); + g_free(file); } } @@ -295,17 +428,17 @@ static void menuProperties (void) GtkTreeSelection *sel; GtkTreeIter iter; GtkTreeModel *model; - char *value, *cmd; + char *file, *cmd; 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_tree_model_get(model, &iter, COL_TEXT, &value, -1); + gtk_tree_model_get(model, &iter, COL_THEME_FILE, &file, -1); - cmd = g_strdup_printf("tintwizard.py \'%s\'", value); + cmd = g_strdup_printf("tintwizard.py \'%s\'", file); system(cmd); g_free(cmd); - g_free(value); + g_free(file); } } @@ -313,8 +446,6 @@ static void menuProperties (void) static void menuQuit (void) { write_config(); - - g_object_unref(g_store); gtk_main_quit (); } @@ -342,14 +473,15 @@ static void menuApply (void) GtkTreeSelection *sel; GtkTreeIter iter; GtkTreeModel *model; - char *value; + char *file; 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_tree_model_get(model, &iter, COL_TEXT, &value, -1); + gtk_tree_model_get(model, &iter, COL_THEME_FILE, &file, -1); - copy_file(value, g_path_config); - g_free(value); + // overwrite tint2rc + copy_file(file, g_path_config); + g_free(file); // restart panel system("killall -SIGUSR1 tint2"); @@ -407,7 +539,7 @@ static void viewRowActivated(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeV static void load_theme(GtkWidget *list) { GDir *dir; - gchar *file, *pt1, *name; + gchar *pt1, *name, *file; int found_theme = FALSE; dir = g_dir_open(g_path_dir, 0, NULL); @@ -417,7 +549,7 @@ static void load_theme(GtkWidget *list) if (pt1) { found_theme = TRUE; name = g_build_filename (g_path_dir, file, NULL); - add_to_list(list, name); + custom_list_append(name); g_free(name); } } @@ -425,10 +557,10 @@ static void load_theme(GtkWidget *list) if (!found_theme) { // create default theme file - file = g_build_filename (g_get_user_config_dir(), "tint2", "default.tint2rc", NULL); - copy_file(g_path_config, file); - add_to_list(list, file); - g_free(file); + name = g_build_filename (g_get_user_config_dir(), "tint2", "default.tint2rc", NULL); + copy_file(g_path_config, name); + custom_list_append(name); + g_free(name); } // search default theme diff --git a/src/tint2conf/theme_view.c b/src/tint2conf/theme_view.c index a89f73d..f8f4df5 100644 --- a/src/tint2conf/theme_view.c +++ b/src/tint2conf/theme_view.c @@ -1,86 +1,234 @@ -#include + #include "theme_view.h" -GtkListStore *g_store; +enum { PROP_PERCENTAGE = 1, PROP_THEME, PROP_SNAPSHOT, }; + +static gpointer parent_class = NULL; + +static void custom_list_init(CustomList *cellprogress); +static void custom_list_class_init(CustomListClass *klass); +static void custom_list_get_property(GObject *object, guint param_id, GValue *value, GParamSpec *pspec); +static void custom_list_set_property(GObject *object, guint param_id, const GValue *value, GParamSpec *pspec); +static void custom_list_finalize(GObject *gobject); + + +// These functions are the heart of our custom cell renderer +static void custom_list_get_size(GtkCellRenderer *cell, GtkWidget *widget, GdkRectangle *cell_area, gint *x_offset, gint *y_offset, gint *width, gint *height); +static void custom_list_render(GtkCellRenderer *cell, GdkWindow *window, GtkWidget *widget, GdkRectangle *background_area, GdkRectangle *cell_area, GdkRectangle *expose_area, guint flags); + -enum { PROP_TITLE = 1, }; +// register our type with the GObject type system if we haven't done so yet. +// Everything else is done in the callbacks. +GType custom_list_get_type() +{ + static GType type = 0; + + if (type) + return type; + + if (1) { + static const GTypeInfo cell_info = + { + sizeof (CustomListClass), + NULL, + NULL, + (GClassInitFunc)custom_list_class_init, + NULL, + NULL, + sizeof (CustomList), + 0, + (GInstanceInitFunc)custom_list_init, + }; + + // Derive from GtkCellRenderer + type = g_type_register_static(GTK_TYPE_CELL_RENDERER, "CustomList", &cell_info, 0); + } + + return type; +} -GtkWidget *create_view(void) +// klass initialisation +// - override the parent's functions that we need to implement. +// - declare our property +// - If you want cells that are editable, you need to override 'activate' and 'start_editing'. +static void custom_list_class_init(CustomListClass *klass) { - GtkCellRenderer *renderer; - GtkTreeViewColumn *col; - GtkTreeSelection *sel; - GtkListStore *liststore; - GtkWidget *view; - - g_store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING, GDK_TYPE_PIXBUF); - view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(g_store)); - //gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE); - gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE); - //gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(view), TRUE); - - 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_TEXT); - gtk_tree_view_column_set_title(col, " items 1 "); - gtk_tree_view_append_column(GTK_TREE_VIEW(view), col); - - renderer = gtk_cell_renderer_pixbuf_new(); - col = gtk_tree_view_column_new(); - gtk_tree_view_column_pack_start(col, renderer, TRUE); - gtk_tree_view_column_add_attribute(col, renderer, "pixbuf", COL_PIX); - gtk_tree_view_column_set_title(col, " image "); - gtk_tree_view_append_column(GTK_TREE_VIEW(view), col); - - sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); - gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE); - g_signal_connect(sel, "changed", G_CALLBACK(on_changed), g_store); - - return view; + GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(klass); + GObjectClass *object_class = G_OBJECT_CLASS(klass); + +printf("custom_list_class_init : deb\n"); + parent_class = g_type_class_peek_parent (klass); + + cell_class->render = custom_list_render; + cell_class->get_size = custom_list_get_size; + object_class->get_property = custom_list_get_property; + object_class->set_property = custom_list_set_property; + object_class->finalize = custom_list_finalize; + + // Install our very own properties + g_object_class_install_property(object_class, PROP_PERCENTAGE, g_param_spec_double("percentage", "Percentage", "The fractional progress to display", 0.0, 1.0, 0.0, G_PARAM_READWRITE)); + g_object_class_install_property(object_class, PROP_THEME, g_param_spec_string("theme", "Theme", "Theme file name", NULL, G_PARAM_READWRITE)); + g_object_class_install_property(object_class, PROP_SNAPSHOT, g_param_spec_string("snapshot", "Snapshot", "Snapshot file name", NULL, G_PARAM_READWRITE)); } -void on_changed(GtkWidget *widget, gpointer label) +// CustomList renderer initialisation +static void custom_list_init(CustomList *custom_list) { - GtkTreeIter iter; - GtkTreeModel *model; - char *value; - - if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) { - gtk_tree_model_get(model, &iter, COL_TEXT, &value, -1); - //gtk_label_set_text(GTK_LABEL(label), value); - g_free(value); + printf("custom_list_init : deb\n"); + // set some default properties of the parent (GtkCellRenderer). + GTK_CELL_RENDERER(custom_list)->mode = GTK_CELL_RENDERER_MODE_INERT; + GTK_CELL_RENDERER(custom_list)->xpad = 2; + GTK_CELL_RENDERER(custom_list)->ypad = 2; +} + + +static void custom_list_finalize(GObject *object) +{ +/* + CustomList *cellrendererprogress = CUSTOM_LIST(object); +*/ + +printf("custom_list_finalize\n"); + // Free any dynamically allocated resources here + + (* G_OBJECT_CLASS (parent_class)->finalize) (object); +} + + +static void custom_list_set_property(GObject *object, guint param_id, const GValue *value, GParamSpec *pspec) +{ + CustomList *custom_list = CUSTOM_LIST(object); + + switch (param_id) { + case PROP_PERCENTAGE: + printf("custom_list_set_property : PROP_PERCENTAGE\n"); + custom_list->progress = g_value_get_double(value); + break; + + case PROP_THEME: + printf("custom_list_set_property : PROP_THEME\n"); + //if (custom_list->nameTheme) g_free(custom_list->nameTheme); + custom_list->nameTheme = g_strdup(g_value_get_string(value)); + break; + + case PROP_SNAPSHOT: + printf("custom_list_set_property : PROP_SNAPSHOT\n"); + custom_list->nameSnapshot = g_strdup(g_value_get_string(value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec); + break; } } -void add_to_list(GtkWidget *list, const gchar *str) +static void custom_list_get_property(GObject *object, guint param_id, GValue *value, GParamSpec *psec) { - GtkTreeIter iter; - gchar *cmd, *name, *snapshot; - GdkPixbuf *icon; - GError *error = NULL; - -//printf(" %s\n", str); - snapshot = g_build_filename (g_get_user_config_dir(), "tint2", "snap.jpg", NULL); - cmd = g_strdup_printf("tint2 -c \'%s\' -s \'%s\'", str, snapshot, NULL); - system(cmd); - - icon = gdk_pixbuf_new_from_file(snapshot, &error); - g_free(snapshot); - if (error) { - g_warning ("Could not load icon: %s\n", error->message); - g_error_free(error); - return; + CustomList *custom_list = CUSTOM_LIST(object); + + switch (param_id) { + case PROP_PERCENTAGE: + printf("custom_list_get_property : PROP_PERCENTAGE\n"); + g_value_set_double(value, custom_list->progress); + break; + + case PROP_THEME: + printf("custom_list_get_property : PROP_THEME\n"); + g_value_set_string(value, g_strdup(custom_list->nameTheme)); + break; + + case PROP_SNAPSHOT: + printf("custom_list_get_property : PROP_SNAPSHOT\n"); + g_value_set_string(value, g_strdup(custom_list->nameSnapshot)); + break; + //g_value_set_object (value, (GObject *)priv->image); + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, psec); + break; } +} - gtk_list_store_append(g_store, &iter); - gtk_list_store_set(g_store, &iter, COL_TEXT, str, COL_PIX, icon, -1); +GtkCellRenderer *custom_list_new() +{ + return g_object_new(CUSTOM_LIST_TYPE, NULL); } +/*************************************************************************** + * + * custom_cell_renderer_progress_get_size: crucial - calculate the size + * of our cell, taking into account + * padding and alignment properties + * of parent. + * + ***************************************************************************/ + +#define FIXED_WIDTH 100 +#define FIXED_HEIGHT 20 + +static void custom_list_get_size(GtkCellRenderer *cell, GtkWidget *widget, GdkRectangle *cell_area, gint *x_offset, gint *y_offset, gint *width, gint *height) +{ + gint calc_width; + gint calc_height; + + calc_width = (gint) cell->xpad * 2 + FIXED_WIDTH; + calc_height = (gint) cell->ypad * 2 + FIXED_HEIGHT; + + if (width) + *width = calc_width; + + if (height) + *height = calc_height; + + if (cell_area) { + if (x_offset) { + *x_offset = cell->xalign * (cell_area->width - calc_width); + *x_offset = MAX (*x_offset, 0); + } + + if (y_offset) { + *y_offset = cell->yalign * (cell_area->height - calc_height); + *y_offset = MAX (*y_offset, 0); + } + } +} + + +/*************************************************************************** + * + * custom_cell_renderer_progress_render: crucial - do the rendering. + * + ***************************************************************************/ + +static void custom_list_render(GtkCellRenderer *cell, GdkWindow *window, GtkWidget *widget, GdkRectangle *background_area, GdkRectangle *cell_area, GdkRectangle *expose_area, guint flags) +{ + CustomList *custom_list = CUSTOM_LIST(cell); + GtkStateType state; + gint width, height; + gint x_offset, y_offset; + + //printf("custom_list_render\n"); + custom_list_get_size (cell, widget, cell_area, &x_offset, &y_offset, &width, &height); + + if (GTK_WIDGET_HAS_FOCUS (widget)) + state = GTK_STATE_ACTIVE; + else + state = GTK_STATE_NORMAL; + + width -= cell->xpad*2; + height -= cell->ypad*2; + + gtk_paint_box (widget->style, window, GTK_STATE_NORMAL, GTK_SHADOW_IN, NULL, widget, "trough", cell_area->x + x_offset + cell->xpad, cell_area->y + y_offset + cell->ypad, width - 1, height - 1); + + gtk_paint_box (widget->style, window, state, GTK_SHADOW_OUT, NULL, widget, "bar", cell_area->x + x_offset + cell->xpad, cell_area->y + y_offset + cell->ypad, width * custom_list->progress, height - 1); +} + + + diff --git a/src/tint2conf/theme_view.h b/src/tint2conf/theme_view.h index 7f20207..3556ed1 100644 --- a/src/tint2conf/theme_view.h +++ b/src/tint2conf/theme_view.h @@ -1,19 +1,45 @@ + #ifndef THEME_VIEW #define THEME_VIEW #include -extern GtkListStore *g_store; +#define CUSTOM_LIST_TYPE (custom_list_get_type()) +#define CUSTOM_LIST(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), CUSTOM_LIST_TYPE, CustomList)) +#define CUSTOM_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CUSTOM_LIST_TYPE, CustomListClass)) +#define CUSTOM_IS_LIST(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CUSTOM_LIST_TYPE)) +#define CUSTOM_IS_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CUSTOM_LIST_TYPE)) +#define CUSTOM_LIST_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CUSTOM_LIST_TYPE, CustomListClass)) + +typedef struct _CustomList CustomList; +typedef struct _CustomListClass CustomListClass; + + +struct _CustomList +{ + GtkCellRenderer parent; -enum { COL_TEXT = 0, COL_PIX, N_COLUMNS }; + gdouble progress; + gchar *nameTheme; + gchar *nameSnapshot; + GdkPixbuf *pixbuf; +}; +struct _CustomListClass +{ + GtkCellRendererClass parent_class; +}; -GtkWidget *create_view(void); -void on_changed(GtkWidget *widget, gpointer label); +// return the type CustomList +GType custom_list_get_type(); + +// return a new cell renderer instance +GtkCellRenderer *custom_list_new(); -void add_to_list(GtkWidget *list, const gchar *str); #endif + +