Setting attributes per cell in treeview (list store)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Setting attributes per cell in treeview (list store)

Gtk+ - Apps Dev mailing list
Hi I have a simple tree view based on a list store

What is the best way to set attributes per row/column is for a specific
cell not for a row or column?

Basically it is used to set various options and some cells are not used, so
I want them to be non-activatable /selectable to prevent any confusion

It a 12 by two table so performance not an issue

Is setting a cell_data_func based on row the best way to go? Or is there a
better way to achieve what I want?

Thanks

Mike
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Setting attributes per cell in treeview (list store)

Gtk+ - Apps Dev mailing list
 
Hi Mike,

I think that you would need to set up individual color columns so that you can color individual cells in the table. For colors, “cell-background-rgba” gives better color control but “cell-background” with html basic colors might be simpler.

Eric
  
//gcc -Wall stripe_list1.c -o stripe_list1 `pkg-config --cflags --libs gtk+-3.0`

#include<gtk/gtk.h>

enum
{
   ID,
   PROGRAM,
   COLOR,
   COLOR2,
   COLUMNS
};

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);

    GtkCellRenderer *renderer1=gtk_cell_renderer_text_new();
    g_object_set(renderer1, "editable", FALSE, NULL);
  
    //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);

    GError *css_error=NULL;
    //gchar css_string[]="#main_window{background: purple;} #test_tree:selected{background: green;}";
    gchar css_string[]="#test_grid{background: purple;} #test_label{background: rgba(0, 255, 0, 255);} #test_tree{background: rgba(0, 55, 200, 255);} #test_tree:selected{background: red;}";
    GtkCssProvider *provider = gtk_css_provider_new();
    GdkDisplay *display = gdk_display_get_default();
    GdkScreen *screen = gdk_display_get_default_screen(display);
    gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    gtk_css_provider_load_from_data(provider, css_string, -1, &css_error);
    if(css_error!=NULL) g_print("CSS loader error %s\n", css_error->message);
    g_object_unref(provider);
  
    gtk_widget_show_all(window);
    gtk_main();
    return 0;  
  }


_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list