Re: How to use GtkTreeSortable.
- From: Olexiy Avramchenko <ath beast stu cn ua>
- To: Johan <jo_ni telia com>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: How to use GtkTreeSortable.
- Date: Tue, 22 Oct 2002 17:13:26 +0300
Johan wrote:
On Tue, 22 Oct 2002 10:08:34 +0300
Olexiy Avramchenko <ath beast stu cn ua> wrote:
As I think there's a file size column in your tree model (of type G_TYPE_INT
or so). All you need is to call gtk_tree_view_column_set_sort_column_id()
on that column.
enum {
<skipped>
COLUMN_FILE_SIZE,
<skipped>
};
<skipped>
{
GtkTreeViewColumn *column;
<skipped>
column = gtk_tree_view_column_new_with_attributes(...);
gtk_tree_view_column_set_sort_column_id (column, COLUMN_FILE_SIZE)
<skipped>
}
Olexiy
Hello.
I have a file size column of type G_TYPE_STRING and displays the
file size like 3.3 MB, 454 Kb, 544 B etc.
I am currently using gtk_tree_view_set_sort_column_id, but the sort
function sorts the size in alphabetic order, which is incorect
for this column.
I need to build my own method to sort this column. Do you know
how gtk_tree_sortable_set_sort_func() works?
I've attached a small example. You also can take a look at
"gtktreedatalist.c" -> gtk_tree_data_list_compare_func().
Hope this helps.
Olexiy
gint special_cmp_function(
GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer data
)
{
gint column = (gint)data;
GType type = gtk_tree_model_get_column_type(model, column);
GValue va = {0, };
GValue vb = {0, };
gchar *sa, *sb;
gdouble ba, bb;
/* getting string values from model */
gtk_tree_model_get_value(model, a, column, &va);
gtk_tree_model_get_value(model, b, column, &vb);
/* if values are not of type G_STRING - abort,
return value means '==', so nothing special will be performed */
g_return_val_if_fail(G_TYPE_FUNDAMENTAL(type) != G_TYPE_STRING, 0);
/* getting strings from values */
sa = (gchar*)g_value_get_string(&va);
sb = (gchar*)g_value_get_string(&vb);
/* getting byte values from strings, only "<float>MB" "<float>KB" and
"<float>" format handled */
if (!sa) {
ba = 0;
}
else {
if (sscanf(sa, "%fMB", &ba) == 1)
ba *= 1024*1024;
else
if (sscanf(sa, "%fKB", &ba) == 1)
ba *= 1024;
else sscanf(sa, "%f", &ba);
}
if (!sb) {
bb = 0;
}
else {
if (sscanf(sb, "%fMB", &bb) == 1)
bb *= 1024*1024;
else
if (sscanf(sb, "%fKB", &bb) == 1)
bb *= 1024;
else sscanf(sa, "%f", &bb);
}
/* freeing values */
g_value_unset(&va);
g_value_unset(&vb);
if (ba < bb)
return -1;
else if (ba > bb)
return 1;
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]