Re: TreeView - set border on individual cells
- From: cecashon aol com
- To: ebassi gmail com
- Cc: gtk-app-devel-list gnome org
- Subject: Re: TreeView - set border on individual cells
- Date: Tue, 9 Apr 2019 18:46:27 +0000 (UTC)
I looked into this a little more. It looks like using an overlay is the easiest solution that I could find.
You can override the render function of the cell renderer but then you will probably need all the code in the
text cell renderer. This isn't too bad since the text cell renderer is modular and you don't have to deal
with a tangle of header files. Or you can just make changes to the text cell renderer and compile the changes
in GTK. Changing the cell renderer has the advantage that you can order what you want to draw from back to
front. A little more flexible than drawing a rectangle over the top.
This does seem something to be useful to be able to do.
Eric
//gcc -Wall stripe_list3.c -o stripe_list3 `pkg-config --cflags --libs gtk+-3.0`
#include<gtk/gtk.h>
#include<stdlib.h>
enum
{
ID,
PROGRAM,
COLOR,
COLOR2,
COLUMNS
};
static gint row_g=0;
static gint column_g=0;
static void get_path(GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column, gpointer renderer1)
{
g_print("Get Path\n");
gchar *string=gtk_tree_path_to_string(path);
g_print("%s %s\n", string, gtk_tree_view_column_get_title(column));
row_g=atoi(string);
g_free(string);
gint c1=0;
if(g_strcmp0("ID", gtk_tree_view_column_get_title(column))==0) c1=2;
if(g_strcmp0("Program", gtk_tree_view_column_get_title(column))==0) c1=3;
column_g=c1-2;
GtkTreeIter iter;
GtkTreeModel *tree_model=gtk_tree_view_get_model(tree_view);
gtk_tree_model_get_iter(tree_model, &iter, path);
gtk_list_store_set(GTK_LIST_STORE(tree_model), &iter, c1, "SpringGreen", -1);
gtk_widget_queue_draw(GTK_WIDGET(tree_view));
}
static gboolean draw_rectangle(GtkWidget *overlay, cairo_t *cr, GtkWidget *tree_view)
{
g_print("Draw Rectangle %i %i\n", row_g, column_g);
GtkTreePath *path=gtk_tree_path_new_from_indices(row_g, -1);
GtkTreeViewColumn *column=gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view), column_g);
GdkRectangle rect;
gint x=0;
gint y=0;
gtk_tree_view_convert_bin_window_to_widget_coords(GTK_TREE_VIEW(tree_view), 0, 0, &x, &y);
cairo_save(cr);
cairo_translate(cr, x, y);
cairo_set_line_width(cr, 2.0);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
gtk_tree_view_get_cell_area(GTK_TREE_VIEW(tree_view), path, column, &rect);
cairo_rectangle(cr, rect.x+1, rect.y+1, rect.width-1, rect.height-1);
cairo_stroke(cr);
cairo_restore(cr);
gtk_tree_path_free(path);
return FALSE;
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Stripes");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 300);
gtk_widget_set_name(window, "main_window");
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkTreeIter iter;
GtkListStore *store=gtk_list_store_new(COLUMNS, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, ID, 0, PROGRAM, "Gedit", COLOR, "cyan", COLOR2, "chartreuse", -1);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, ID, 1, PROGRAM, "Gimp", COLOR, "yellow", COLOR2, "chartreuse", -1);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, ID, 2, PROGRAM, "Inkscape", COLOR, "cyan", COLOR2, "chartreuse", -1);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, ID, 3, PROGRAM, "Firefox", COLOR, "yellow", COLOR2, "fuchsia", -1);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, ID, 4, PROGRAM, "Calculator", COLOR, "cyan", COLOR2, "chartreuse", -1);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, ID, 5, PROGRAM, "Devhelp", COLOR, "yellow", COLOR2, "chartreuse", -1);
GtkWidget *tree=gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
gtk_widget_set_name(tree, "test_tree");
gtk_widget_set_hexpand(tree, TRUE);
gtk_widget_set_vexpand(tree, FALSE);
g_object_set(tree, "activate-on-single-click", TRUE, NULL);
GtkTreeSelection *selection=gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE);
GtkCellRenderer *renderer1=gtk_cell_renderer_text_new();
g_object_set(renderer1, "editable", FALSE, NULL);
g_signal_connect(tree, "row-activated", G_CALLBACK(get_path), renderer1);
//Bind the COLOR column to the "cell-background" property.
GtkTreeViewColumn *column1 = gtk_tree_view_column_new_with_attributes("ID", renderer1, "text", ID,
"cell-background", COLOR, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column1);
GtkTreeViewColumn *column2 = gtk_tree_view_column_new_with_attributes("Program", renderer1, "text",
PROGRAM, "cell-background", COLOR2, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column2);
GtkWidget *overlay=gtk_overlay_new();
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), tree);
gtk_overlay_set_overlay_pass_through(GTK_OVERLAY(overlay), tree, TRUE);
g_signal_connect_after(overlay, "draw", G_CALLBACK(draw_rectangle), tree);
GtkWidget *label=gtk_label_new("Test Label");
gtk_widget_set_name(label, "test_label");
gtk_widget_set_hexpand(label, TRUE);
GtkWidget *grid=gtk_grid_new();
gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE);
gtk_widget_set_name(grid, "test_grid");
gtk_grid_attach(GTK_GRID(grid), overlay, 0, 0, 1, 9);
gtk_grid_attach(GTK_GRID(grid), label, 0, 10, 1, 1);
gtk_container_add(GTK_CONTAINER(window), grid);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]