GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Sep 03, 2014 4:40 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Setting colours of individual list cell colours -help please
PostPosted: Tue Dec 13, 2011 9:49 pm 
Offline
GTK+ Guru

Joined: Sun Jan 17, 2010 6:48 pm
Posts: 102
I'm gradually pulling out what precious little is left of my hair in the vain effort of coming up with a simple solution to the problem of creating a single function to set the colour properties of the text in a listview widget. The examples that I have found on the topic take the approach of setting colour attributes when cell data is changed. I don't want to that. I want to change foreground/background colour as desired. I need to put together a function pretty much like:

Code:
void set_cell_colors (widget,row,col, fgClr, bgClr) {
...
...
}



Any suggestions, please!

WJG

_________________
"If you don't stand for something, then you'll fall for anything."


Top
 Profile  
 
 Post subject: Re: Setting colours of individual list cell colours -help pl
PostPosted: Wed Dec 14, 2011 1:22 am 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
1) I believe you mean the GtkTreeView widget.
2) What's your platform? i.e. Linux, Mac, Windows.
3) What's your OS. i.e. Ubuntu, OS X Lion, Windows 7
4) What's your GTK version ?
5) What programming language are you using, i.e. C, C++ ect.
6) How many rows and columns do you plan on having?


Top
 Profile  
 
 Post subject: Re: Setting colours of individual list cell colours -help pl
PostPosted: Wed Dec 14, 2011 11:10 am 
Offline
GTK+ Guru

Joined: Sun Jan 17, 2010 6:48 pm
Posts: 102
There doesn't seem to be a simple way around this. As what I want to do only really effects strings, it looks like using Pango markup is the way forward.

Will

_________________
"If you don't stand for something, then you'll fall for anything."


Top
 Profile  
 
 Post subject: Re: Setting colours of individual list cell colours -help pl
PostPosted: Thu Dec 15, 2011 12:41 am 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 177
Location: USA
I can't help that much unless you can answer at least some of my questions but here's C code that works with Gtk+-3.2.0 and Ubuntu 11.04 (Natty)
Code:
/* COMPILE WITH: gcc -Wall -o tree1 `pkg-config --cflags --libs gtk+-3.0` tree1.c */

#include <gtk/gtk.h>
enum
{
  LIST_ITEM = 0,
  N_COLUMNS
};

static void
init_list(GtkWidget *list)
{
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  GtkListStore *store;

/********************  CSS  *****************************************************/

  GtkCssProvider *provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (provider,"GtkTreeView row:selected:focused{\n"
                                             "background-color: red;\n"
                                             "color: green;\n"  // font color
                                             "}\n"
                                             "GtkTreeView {\n"
                                             "background-color: yellow;\n"
                                             "color: blue;\n" 
                                             "}\n", -1, NULL);
  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);
  g_object_unref (provider);

/*********************************************************************************/
  renderer = gtk_cell_renderer_text_new();
  column = gtk_tree_view_column_new_with_attributes("List Items",
          renderer, "text", LIST_ITEM, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

  store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING);

  gtk_tree_view_set_model(GTK_TREE_VIEW(list),
      GTK_TREE_MODEL(store));

  g_object_unref(store);
}

static void
add_to_list(GtkWidget *list, const gchar *str)
{
  GtkListStore *store;
  GtkTreeIter iter;

  store = GTK_LIST_STORE(gtk_tree_view_get_model
      (GTK_TREE_VIEW(list)));

  gtk_list_store_append(store, &iter);
  gtk_list_store_set(store, &iter, LIST_ITEM, str, -1);
}

void  on_changed(GtkWidget *widget, gpointer label)
{
  GtkTreeIter iter;
  GtkTreeModel *model;
  char *value;

  if (gtk_tree_selection_get_selected(
      GTK_TREE_SELECTION(widget), &model, &iter)) {

    gtk_tree_model_get(model, &iter, LIST_ITEM, &value,  -1);
    gtk_label_set_text(GTK_LABEL(label), value);
    g_free(value);
  }
}

int main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *list;
  GtkWidget *vbox;
  GtkWidget *label;
  GtkTreeSelection *selection;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);
  gtk_widget_set_size_request(window, 270, 250);
  gtk_window_set_title(GTK_WINDOW(window), "List View");

  list = gtk_tree_view_new();
  gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);

  vbox = gtk_vbox_new(FALSE, 0);

  gtk_box_pack_start(GTK_BOX(vbox), list, TRUE, TRUE, 5);

  label = gtk_label_new("");
  gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
  gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);

  gtk_container_add(GTK_CONTAINER(window), vbox);

  init_list(list);
  add_to_list(list, "Aliens");
  add_to_list(list, "Leon");
  add_to_list(list, "Capote");
  add_to_list(list, "Saving private Ryan");
  add_to_list(list, "Der Untergang");

  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));

  g_signal_connect(selection, "changed",
      G_CALLBACK(on_changed), label);

  g_signal_connect(G_OBJECT (window), "destroy",
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);

  gtk_main ();

  return 0;
}


Attachments:
GtkTreeView.png
GtkTreeView.png [ 13.05 KiB | Viewed 921 times ]
Top
 Profile  
 
 Post subject: Re: Setting colours of individual list cell colours -help pl
PostPosted: Thu Dec 15, 2011 3:48 pm 
Offline
GTK+ Guru

Joined: Sun Jan 17, 2010 6:48 pm
Posts: 102
Hi Zerohour

Thanks for the reply. I can see that you're using Gtk+3.3, I'm still working with 2.24! I pretty well resolved my need using markup strings, although of course, this is not a robust solution. I will study your example as I'm sure that there is much that I can learn from it.

My approach to implementing an instance of a listview widget is somewhat different as I'm actually working on the sources for Tcl-Gtk language binding (www.gnocl.org) rather directly coding a C-based app. For instance, gtk_tree_view_column_new_with_attributes is not used whereas gtk_tree_store_newv and gtk_list_store_newv are. Having said that, I think that I need to think over how these alternative library calls can be used to offer new functionality to the binding.

Will

_________________
"If you don't stand for something, then you'll fall for anything."


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google Adsense [Bot], ldries46 and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group