1

How can I change the font of a leaf in my gtktreeview? I'd like set the font bold for a particular leaf

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
Paul
  • 147
  • 1
  • 2
  • 5
  • Which language? You'll get more answers if you add the language you're using, also adding some example code (i.e. how you add your items to the GtkTreeView) would be helpful. – Jens Mühlenhoff Sep 12 '11 at 15:03
  • Maybe this thread is some help to you: http://mail.gnome.org/archives/gtk-app-devel-list/2006-November/msg00187.html – Jens Mühlenhoff Sep 12 '11 at 15:04
  • Thank you for your answer. I've seen the thread, but I don't understand how to set the font for an existing column and a particular node in the tree. Ah, I'm programming in C. – Paul Sep 12 '11 at 19:21
  • Ok, you should edit your question with this additional information, I already added the "c" and "gtk" tags to the question so more people with a GTK+ background will see it. – Jens Mühlenhoff Sep 13 '11 at 08:41

3 Answers3

1

I'm using python, but the way to do this must be the same, only adapt the syntax.

In GTK, use PANGO to change fonts. Here in a treeview :

import pango, GTK
....
cols = ['Date', 'Index', 'Program', 'Comments', 'Name']
self.treeView.cell = [None] * len(cols)
....
fontT = pango.FontDescription("serif light Oblique 8")
fontO = pango.FontDescription("serif bold 8")
treeView.cell[2].set_property('font-desc', fontT)
treeView.cell[3].set_property('font-desc', fontO)

This makes columns 2 ('Program') and 3 ('Comments') of different fonts. Column 3 is bold.

Hope this was helpful.


EDIT :

Just found a C link :

http://www.ibm.com/developerworks/library/l-u-pango2/

Louis
  • 2,854
  • 2
  • 19
  • 24
0

You can use Pango Markup. It What you then need is:

  1. Use Pango Markup Language within you model (here with GtkListStore):
GtkListStore *listStore = gtk_list_store_new(1, G_TYPE_STRING);
GtkTreeIter rowIter;
gtk_list_store_append(listStore, &rowIter);
gtk_list_store_set(listStore, &rowIter,
    LIST_COL_NAME, "<span foreground='blue'>Blue Title</span>"\
    " usual content", -1);

And later, when you create the columns for the TreeView, you need to specify markup instead of text:

#define LIST_COL_INDEX_NAME 0 // column index

// ...

GtkTreeViewColumn * col = gtk_tree_view_column_new_with_attributes (
         "Column title", renderer,
         "markup", // <--- This is important!
         LIST_COL_INDEX_NAME, NULL);
gtk_tree_view_append_column (treeview, col);

References

DarkTrick
  • 2,447
  • 1
  • 21
  • 39
0

You must set a data function for the column like this:

gtk_tree_view_column_set_cell_data_func(column, renderer, data_func, NULL, NULL);

The data function could look like this:

void data_func (GtkTreeViewColumn *col,
                GtkCellRenderer   *renderer,
                GtkTreeModel      *model,
                GtkTreeIter       *iter,
                gpointer           user_data)
{
  gboolean active;
  gtk_tree_model_get(model, iter, CHECKED_COLUMN, &active, -1);

  g_debug("xxxxx %u", active);
  if (active)
  {
    g_object_set(renderer, "weight", PANGO_WEIGHT_BOLD, NULL);
  }
  else
  {
    g_object_set(renderer, "weight", PANGO_WEIGHT_NORMAL, NULL);
  }
}