Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Back  Login

tut-treeview-renderer-bold Diff - Ruby-GNOME2 Project Website

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

=How to Make a Whole Row Bold or Coloured
{{link "tut-treeview-renderer-example", "tut-treeview-renderer", "tut-treeview", "tut-treeview-selection-handle"}}

This seems to be a frequently asked question, so it is worth mentioning it here. You have the two approaches mentioned above: either you use cell data functions, and check in each whether a particular row should be highlighted in a particular way (bold, coloured, whatever), and then set the renderer properties accordingly (and unset them if you want that row to look normal), or you use attributes. Cell data functions are most likely not the right choice in this case though.

If you only want every second line to have a different background to make it easier for the user to see which data belongs to which line in wide tree views, then you do not have to bother with the stuff mentioned here. Instead just set the rules hint on the tree view as described in ((<TreeView Look and Feel section|tut-treeview-view-look>)), and everything will be done automatically, in colors that conform to the chosen theme even (unless the theme disables rule hints, that is).

Otherwise, the most suitable approach is to use attributes.  The simplest approach is to set the "background" attribute and add a column to your model which determines whether a row is to be colored and connect it to the "background_set" attribute:

  liststore = Gtk::ListStore.new(String, String, TrueClass)

  # Append a row and fill in some data
  iter = liststore.append
  iter[0] = "Joe"
  iter[1] = "Average"
  iter[2] = false

  # Append a second row and fill in some data
  iter = liststore.append
  iter[0] = "Jane"
  iter[1] = "Common"
  iter[2] = true

  view = Gtk::TreeView.new(liststore)

  # Create a renderer with the background property set
  renderer = Gtk::CellRendererText.new
  renderer.background = "pink"

  # Add the columns, with the background_set property mapped to the appropriate column
  col = Gtk::TreeViewColumn.new("First Name", renderer, :text => 0, :background_set => 2)
  view.append_column(col)

  col = Gtk::TreeViewColumn.new("Last Name", renderer, :text => 1, :background_set => 2)
  view.append_column(col)

An alternate approach, if you would like to have a different background color, would be to add a second column listing the background color desired, and map the background attribute to that column rather than setting it directly on the cell renderer.  Note that this method can be used with any attribute that provides a corresponding foo-set attribute; e.g. foreground or weight.

As a general rule, you should not change the text colour or the background colour of a cell unless you have a really good reason for it. To quote Havoc Pennington: "Because colors in GTK+ represent a theme the user has chosen, you should never set colors purely for aesthetic reasons. If users don't like GTK+ gray, they can change it themselves to their favorite shade of orange."