1,要点
1) 一列(GtkTreeColumn)可以有多个Cell Renderer (GtkCellRenderer),gtk_tree_store_new/gtk_list_store_new的第一个参数是
GtkCellRenderer的总数,而不是GtkTreeColumn的总数。
2) Toggle类型的Cell Renderer(GtkCellRenderToggle)将在对应位置显示Check/Radio Box,对GtkCellRenderToggle的选择和取消选择应该用
gtk_list_store_set重新设置他的值,而不是直接调用接口 gtk_cell_renderer_toggle_set_active,因为这个接口会导致不确定的行被选择
或取消选择
2,例子
#include
enum
{
COL_MARKED,
COL_FIRST_NAME = 0,
COL_LAST_NAME,
NUM_COLS
} ;
//GtkCellRenderToggle的选择/取消选择处理函数
fixed_toggled (GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
{
GtkTreeModel *model = (GtkTreeModel *) data;
GtkTreeIter iter;
GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
gboolean fixed;
/* get toggled iter */
gtk_tree_model_get_iter (model, &iter, path);
/* Get previous value*/
gtk_tree_model_get (model, &iter, COL_MARKED, &fixed, -1);
/* Toggle the previous value */
fixed ^= 1;
/* set new value */
gtk_list_store_set (GTK_LIST_STORE (model), &iter, COL_MARKED, fixed, -1);
/* clean up allocated path*/
gtk_tree_path_free (path);
}
static GtkTreeModel *create_and_fill_model (void)
{
GtkTreeStore *treestore;
GtkTreeIter toplevel, child;
GtkListStore *liststore;
/* Allocate list store*/
liststore = gtk_list_store_new(NUM_COLS, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING);
/* Append a second top level row, and fill it with some data */
gtk_tree_store_append(treestore, &toplevel, NULL);
gtk_tree_store_set(treestore, &toplevel,
COL_MARKED, FALSE,
COL_FIRST_NAME, "Joe",
COL_LAST_NAME, "Average",
-1);
/* Append a child to the second top level row, and fill in some data */
gtk_tree_store_append(treestore, &child, &toplevel);
gtk_tree_store_set(treestore, &child,
COL_MARKED, FALSE,
COL_FIRST_NAME, "Jane",
COL_LAST_NAME, "Average",
-1);
return GTK_LIST_MODEL(treestore);
}
static GtkWidget *create_view_and_model (void)
{
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
GtkWidget *view;
GtkTreeModel *model;
model = create_and_fill_model();
view = gtk_list_view_new();
/* --- Column #1 --- */
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "First Name");
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
/* the first cell renderer, its index is COL_MARKED */
renderer = gtk_cell_renderer_toggle_new();
g_signal_connect(renderer, "toggled", G_CALLBACK(fixed_toggled),model) ;
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_add_attribute(col, renderer, "active", COL_MARKED);
/* the second cell renderer, its index is COL_MARKED */
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_add_attribute(col, renderer, "text", COL_FIRST_NAME, NULL);
/* --- Column #2 --- */
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Last Name");
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
/* The renderer of column 2*/
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_add_attribute(col, renderer, "text", COL_SECOND_NAME, NULL);
/* set 'cell-background' property of the cell renderer */
g_object_set(renderer,
"cell-background", "Orange",
"cell-background-set", TRUE,
NULL);
gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
g_object_unref(model); /* destroy model automatically with view */
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), GTK_SELECTION_NONE);
return view;
}
int main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *view;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete_event", gtk_main_quit, NULL); /* dirty */
view = create_view_and_model();
gtk_container_add(GTK_CONTAINER(window), view);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
3,备注:
文中代码未经编译调试。
4,参考文献
1) http://library.gnome.org/devel/gtk/stable/TreeWidgetObjects.html
2)
3)
阅读(3037) | 评论(0) | 转发(0) |