blackedpi
blackedpi

Reputation: 21

GtkTreeView C change individual text color for a specific row or cell and not the whole column

I'm racking my brain to figure out how to change text color in a GtkTreeView cell without having the whole column change color. I want to dynamically change from default color to to green or red depending on conditions. Like pass or fail a test and the individual cell text color turns red or green for instance in C programming.

if(strstr(string,"PASS"))
{
    g_object_set(renderer_pass_fail,"foreground", "Green","foreground-set", TRUE,NULL);
}
else
{
    g_object_set(renderer_pass_fail,"foreground", "Red","foreground-set", TRUE,NULL);
}

Upvotes: 0

Views: 373

Answers (2)

blackedpi
blackedpi

Reputation: 21

With a few more googles and help from first answer above, and using keyword glade I found the answer in the following link how to accomplish the above using Glade.

Found Answer while using glade XML UI App

if(strstr(string,"PASS"))
{
    gdk_color_parse ("green", &color);
    gtk_list_store_set (get_gtk_list_store(), &iterr,COL, "PASS",COL_COLOR, &color,-1);
}
else if(strstr(string,"FAIL"))
{
    gdk_color_parse ("red", &color);
    gtk_list_store_set (get_gtk_list_store(), &iterr,COL, "FAIL",COL_COLOR, &color,-1);
}
else if(strstr(string,"ABORT"))
{
    gdk_color_parse ("red", &color);
    gtk_list_store_set (get_gtk_list_store(), &iterr,COL, "FAIL",COL_COLOR, &color,-1);
}
else
{
    gdk_color_parse ("yellow", &color);
    gtk_list_store_set (get_gtk_list_store(), &iterr,COL, "N / A",COL_COLOR, &color,-1);
}

Upvotes: 0

Alexander Dmitriev
Alexander Dmitriev

Reputation: 2525

Yoh have to add one more column to your model and store color there.


enum
{
    TEXT_COL,
    COLOR_COL,
    N_COLUMNS
};

/* TreeModel */
store = gtk_list_store_new (N_COLUMNS,
                            G_TYPE_STRING,
                            GDK_TYPE_RGBA);

/* Column */                            
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes (_("Name"),
                                                   renderer,
                                                   "text",
                                                   TEXT_COL,
                                                   "foreground-rgba",
                                                   COLOR_COL,
                                                   NULL);

/* Adding an element */
gtk_list_store_append (store, &ls_iter);
gtk_list_store_set (store, &ls_iter,
                    TEXT_COL, "hello",
                    COLOR_COL, gdk_rgba_copy (color),
                    -1);

Upvotes: 2

Related Questions