开发者

Change the font in a GtkTreeView

开发者 https://www.devze.com 2023-04-04 16:11 出处:网络
How can I cha开发者_如何学Cnge the font of a leaf in my gtktreeview? I\'d like set the font bold for a particular leafI\'m using python, but the way to do this must be the same, only adapt the syntax.

How can I cha开发者_如何学Cnge the font of a leaf in my gtktreeview? I'd like set the font bold for a particular leaf


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/


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


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

  • Pango markup reference
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号