6 #include <glib-object.h>
12 static gint compare_uint(GtkTreeModel *model,
13 GtkTreeIter *a, GtkTreeIter *b,
16 gint sort_col = GPOINTER_TO_INT(p);
19 gtk_tree_model_get(model, a, sort_col, &no1, -1);
20 gtk_tree_model_get(model, b, sort_col, &no2, -1);
25 static gint compare_int(GtkTreeModel *model,
26 GtkTreeIter *a, GtkTreeIter *b,
29 gint sort_col = GPOINTER_TO_INT(p);
32 gtk_tree_model_get(model, a, sort_col, &no1, -1);
33 gtk_tree_model_get(model, b, sort_col, &no2, -1);
38 static gint compare_double(GtkTreeModel *model,
39 GtkTreeIter *a, GtkTreeIter *b,
42 gint sort_col = GPOINTER_TO_INT(p);
45 gtk_tree_model_get(model, a, sort_col, &no1, -1);
46 gtk_tree_model_get(model, b, sort_col, &no2, -1);
57 static gint compare_str(GtkTreeModel *model,
58 GtkTreeIter *a, GtkTreeIter *b,
61 gint sort_col = GPOINTER_TO_INT(p);
67 gtk_tree_model_get(model, a, sort_col, &str1, -1);
68 gtk_tree_model_get(model, b, sort_col, &str2, -1);
70 if (str1 == NULL || str2 == NULL) {
71 if (str1 == NULL && str2 == NULL)
74 ret = (str1 == NULL) ? -1 : 1;
76 ret = g_utf8_collate(str1, str2);
86 struct table *table_new_with_columns(gint col_nums, ...)
89 GtkTreeViewColumn *column;
90 const char *titles[col_nums];
97 va_start(args, col_nums);
98 table = g_new(
struct table, 1);
99 table->types = g_new(GType, col_nums);
100 for (i = 0; i < col_nums; i++) {
101 titles[i] = va_arg(args,
const char *);
102 table->types[i] = va_arg(args, GType);
106 table->column_nums = col_nums;
107 table->list_store = gtk_list_store_newv(col_nums, table->types);
108 table->widget = gtk_tree_view_new_with_model(
109 GTK_TREE_MODEL(table->list_store));
111 g_object_unref(GTK_TREE_MODEL(table->list_store));
113 table->renderers = g_new(GtkCellRenderer *, col_nums);
114 for (i = 0; i < col_nums; i++) {
115 table->renderers[i] = gtk_cell_renderer_text_new();
116 column = gtk_tree_view_column_new();
117 gtk_tree_view_column_set_title(column, titles[i]);
118 gtk_tree_view_column_pack_start(column,
119 table->renderers[i], FALSE);
121 gtk_tree_view_column_add_attribute(column,
122 table->renderers[i],
"text", i);
123 gtk_tree_view_append_column(GTK_TREE_VIEW(table->widget), column);
129 void table_destroy(
struct table *table)
131 gtk_widget_destroy(table->widget);
132 g_free(table->types);
136 void table_set_sortable(
struct table *table)
138 GtkTreeSortable *sortable = GTK_TREE_SORTABLE(table->list_store);
139 GtkTreeViewColumn *column;
140 gint i, first_sort_col = -1;
142 for (i = 0; i < table->column_nums; i++) {
143 column = gtk_tree_view_get_column(
144 GTK_TREE_VIEW(table->widget), i);
145 switch (table->types[i]) {
147 gtk_tree_sortable_set_sort_func(sortable, i,
148 &compare_uint, GINT_TO_POINTER(i),
150 if (first_sort_col == -1)
154 gtk_tree_sortable_set_sort_func(sortable, i,
155 &compare_int, GINT_TO_POINTER(i),
157 if (first_sort_col == -1)
161 gtk_tree_sortable_set_sort_func(sortable, i,
162 &compare_double, GINT_TO_POINTER(i),
164 if (first_sort_col == -1)
168 gtk_tree_sortable_set_sort_func(sortable, i,
169 &compare_str, GINT_TO_POINTER(i),
171 if (first_sort_col == -1)
176 switch (table->types[i]) {
181 gtk_tree_view_column_set_sort_column_id(column, i);
185 if (first_sort_col != -1)
186 gtk_tree_sortable_set_sort_column_id(sortable,
187 first_sort_col, GTK_SORT_ASCENDING);
192 void table_set_column_align(
struct table *table, gint column_num, gfloat align)
194 g_object_set(G_OBJECT(table->renderers[column_num]),
195 "xalign", align, NULL);
199 int table_add_row(
struct table *table, ...)
208 va_start(args, table);
209 gtk_list_store_append(table->list_store, &iter);
210 for (i = 0; i < table->column_nums; i++) {
211 memset(&val, 0,
sizeof(GValue));
212 g_value_init(&val, table->types[i]);
213 switch (table->types[i]) {
215 g_value_set_static_string(&val,
216 va_arg(args,
const char *));
219 g_value_set_int(&val, va_arg(args,
int));
222 g_value_set_uint(&val, va_arg(args,
unsigned int));
225 g_value_set_double(&val, va_arg(args,
double));
228 g_assert_not_reached();
230 gtk_list_store_set_value(table->list_store, &iter, i, &val);