Coloring an entire row at a GtkTreeView



Sorry to bother you with, which seems to be a simple question, but I am
 out of ideas.

I am trying to coloring an entire row of a GtkTreeView.
After some search I discovered "GTK+ 2.0 Tree View Tutorial" from
Tim-Philipp M�which taught me A LOT about tree views and the
paradigm of model-view.
Ok, said that, I am now able to paint an entire row, and just the row,
with a color of my choice but, the problem is that I am unable to do
that at execution time, just at the compilation time because the column
which has to be read for the renderer knows if the color should be
displayed or not isn't and I do not know why.
If I connect the the renderer's property "cell-background-set" to an
column and don't connect the "cell-background" property to another
column model I am able to control whenever the background should be
printed or not but, when I connect the "cell-background" to one column
at the model the column with the "cell-background-set" is just forgotten.

I did an example, based on a piece of code that I found, to show this
behavior.
Someone can light me on?
Thank you for your time.

PS: Just an off-topic question: I never knows if I am suppose to attach
the code example or put it with along with my e-mail. What is best?

/***************************************************************************/
#include <gtk/gtk.h>

enum
{
    COL_FIRST_NAME,
    COL_LAST_NAME,
    COLOR,
    CHOICE,
    NUM_COLS
};

static GtkTreeModel *
create_and_fill_model (void)
{
    GtkTreeStore  *treestore;
    GtkTreeIter    toplevel;

    treestore = gtk_tree_store_new(NUM_COLS,G_TYPE_STRING,G_TYPE_STRING,
				   G_TYPE_STRING, G_TYPE_BOOLEAN);
    gtk_tree_store_append(treestore, &toplevel, NULL);
    gtk_tree_store_set(treestore, &toplevel,
		       COL_FIRST_NAME, "Joe",
		       COL_LAST_NAME, "Average",
		       COLOR, "red",
		       CHOICE, FALSE, /* Should not display color */
		       -1);

    return GTK_TREE_MODEL(treestore);
}


static GtkWidget *
create_view_and_model (void)
{
    GtkTreeViewColumn   *col;
    GtkCellRenderer     *renderer;
    GtkWidget           *view;
    GtkTreeModel        *model;

    view = gtk_tree_view_new();

    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "First Name");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);

    /* The Column Problem */
    gtk_tree_view_column_set_attributes( col, renderer,
					 "text", COL_FIRST_NAME,
					 "cell-background", COLOR,
					 "cell-background-set", CHOICE,
					 NULL );

    col = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(col, "Second Name");
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, renderer, TRUE);

    /* A column that can be controled */
    g_object_set(renderer, "cell-background", "red", NULL);
    gtk_tree_view_column_set_attributes( col, renderer,
					 "text", COL_FIRST_NAME,
					 "cell-background-set", CHOICE,
					 NULL );

    model = create_and_fill_model();
    gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
    g_object_unref(model);

gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
				GTK_SELECTION_NONE);
    return view;
}

int
main (int argc, char **argv)
{
    GtkWidget *window;
    GtkWidget *view;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
    view = create_view_and_model();
    gtk_container_add(GTK_CONTAINER(window), view);
    gtk_widget_show_all(window);

    gtk_main();
    return 0;
}

/***************************************************************************/

-- 
Diogo F. S. Ramos



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]