Re: TreeView - set border on individual cells
- From: cecashon aol com
- To: redtux1 gmail com, gtk-app-devel-list gnome org
- Subject: Re: TreeView - set border on individual cells
- Date: Sat, 6 Apr 2019 18:05:36 +0000 (UTC)
Hi Mike,
I gave what you said a try and it looks to work well enough. Then, how do you get a box around a selected
cell. It seems to me if you let the built in renderer do it's thing and then get a cairo_t to just draw a
rectangle after, it should work, right? OK, a bit of a hack but it works on my computer. GTK3.18. On a newer
version of GTK I get a deprecated warning about gdk_cairo_create() but it still works.
Eric
//gcc -Wall stripe_list2.c -o stripe_list2 `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 *tree_view, cairo_t *cr, gpointer data)
{
GtkTreePath *path=gtk_tree_path_new_from_indices(row_g, -1);
g_print("Draw Rectangle %i %i\n", row_g, column_g);
GdkWindow *win=gtk_tree_view_get_bin_window(GTK_TREE_VIEW(tree_view));
cairo_t *cr2=gdk_cairo_create(win);
GdkRectangle rect;
cairo_set_line_width(cr2, 4.0);
cairo_set_source_rgba(cr2, 1.0, 1.0, 1.0, 1.0);
GtkTreeViewColumn *column=gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view), column_g);
gtk_tree_view_get_cell_area(GTK_TREE_VIEW(tree_view), path, column, &rect);
cairo_rectangle(cr2, rect.x+2, rect.y+2, rect.width-2, rect.height-2);
cairo_stroke(cr2);
cairo_destroy(cr2);
gtk_tree_path_free(path);
return TRUE;
}
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);
g_signal_connect_after(tree, "draw", G_CALLBACK(draw_rectangle), 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 *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), tree, 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]