2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtktreesortable.h"
23 #include "gtkmarshalers.h"
28 * SECTION:gtktreesortable
29 * @Short_description: The interface for sortable models used by GtkTreeView
30 * @Title: GtkTreeSortable
31 * @See_also:#GtkTreeModel, #GtkTreeView
33 * #GtkTreeSortable is an interface to be implemented by tree models which
34 * support sorting. The #GtkTreeView uses the methods provided by this interface
39 static void gtk_tree_sortable_base_init (gpointer g_class);
42 gtk_tree_sortable_get_type (void)
44 static GType tree_sortable_type = 0;
46 if (! tree_sortable_type)
48 const GTypeInfo tree_sortable_info =
50 sizeof (GtkTreeSortableIface), /* class_size */
51 gtk_tree_sortable_base_init, /* base_init */
52 NULL, /* base_finalize */
54 NULL, /* class_finalize */
55 NULL, /* class_data */
62 g_type_register_static (G_TYPE_INTERFACE, I_("GtkTreeSortable"),
63 &tree_sortable_info, 0);
65 g_type_interface_add_prerequisite (tree_sortable_type, GTK_TYPE_TREE_MODEL);
68 return tree_sortable_type;
72 gtk_tree_sortable_base_init (gpointer g_class)
74 static gboolean initialized = FALSE;
79 * GtkTreeSortable::sort-column-changed:
80 * @sortable: the object on which the signal is emitted
82 * The ::sort-column-changed signal is emitted when the sort column
83 * or sort order of @sortable is changed. The signal is emitted before
84 * the contents of @sortable are resorted.
86 g_signal_new (I_("sort-column-changed"),
87 GTK_TYPE_TREE_SORTABLE,
89 G_STRUCT_OFFSET (GtkTreeSortableIface, sort_column_changed),
91 _gtk_marshal_VOID__VOID,
98 * gtk_tree_sortable_sort_column_changed:
99 * @sortable: A #GtkTreeSortable
101 * Emits a #GtkTreeSortable::sort-column-changed signal on @sortable.
104 gtk_tree_sortable_sort_column_changed (GtkTreeSortable *sortable)
106 g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
108 g_signal_emit_by_name (sortable, "sort-column-changed");
112 * gtk_tree_sortable_get_sort_column_id:
113 * @sortable: A #GtkTreeSortable
114 * @sort_column_id: (out): The sort column id to be filled in
115 * @order: (out): The #GtkSortType to be filled in
117 * Fills in @sort_column_id and @order with the current sort column and the
118 * order. It returns %TRUE unless the @sort_column_id is
119 * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID or
120 * %GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID.
122 * Return value: %TRUE if the sort column is not one of the special sort
126 gtk_tree_sortable_get_sort_column_id (GtkTreeSortable *sortable,
127 gint *sort_column_id,
130 GtkTreeSortableIface *iface;
132 g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
134 iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
136 g_return_val_if_fail (iface != NULL, FALSE);
137 g_return_val_if_fail (iface->get_sort_column_id != NULL, FALSE);
139 return (* iface->get_sort_column_id) (sortable, sort_column_id, order);
143 * gtk_tree_sortable_set_sort_column_id:
144 * @sortable: A #GtkTreeSortable
145 * @sort_column_id: the sort column id to set
146 * @order: The sort order of the column
148 * Sets the current sort column to be @sort_column_id. The @sortable will
149 * resort itself to reflect this change, after emitting a
150 * #GtkTreeSortable::sort-column-changed signal. @sort_column_id may either be
151 * a regular column id, or one of the following special values:
154 * <term>%GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID</term>
155 * <listitem>the default sort function will be used, if it is set</listitem>
158 * <term>%GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID</term>
159 * <listitem>no sorting will occur</listitem>
164 gtk_tree_sortable_set_sort_column_id (GtkTreeSortable *sortable,
168 GtkTreeSortableIface *iface;
170 g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
172 iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
174 g_return_if_fail (iface != NULL);
175 g_return_if_fail (iface->set_sort_column_id != NULL);
177 (* iface->set_sort_column_id) (sortable, sort_column_id, order);
181 * gtk_tree_sortable_set_sort_func:
182 * @sortable: A #GtkTreeSortable
183 * @sort_column_id: the sort column id to set the function for
184 * @sort_func: The comparison function
185 * @user_data: (allow-none): User data to pass to @sort_func, or %NULL
186 * @destroy: (allow-none): Destroy notifier of @user_data, or %NULL
188 * Sets the comparison function used when sorting to be @sort_func. If the
189 * current sort column id of @sortable is the same as @sort_column_id, then
190 * the model will sort using this function.
193 gtk_tree_sortable_set_sort_func (GtkTreeSortable *sortable,
195 GtkTreeIterCompareFunc sort_func,
197 GDestroyNotify destroy)
199 GtkTreeSortableIface *iface;
201 g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
202 g_return_if_fail (sort_func != NULL);
204 iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
206 g_return_if_fail (iface != NULL);
207 g_return_if_fail (iface->set_sort_func != NULL);
208 g_return_if_fail (sort_column_id >= 0);
210 (* iface->set_sort_func) (sortable, sort_column_id, sort_func, user_data, destroy);
214 * gtk_tree_sortable_set_default_sort_func:
215 * @sortable: A #GtkTreeSortable
216 * @sort_func: The comparison function
217 * @user_data: (allow-none): User data to pass to @sort_func, or %NULL
218 * @destroy: (allow-none): Destroy notifier of @user_data, or %NULL
220 * Sets the default comparison function used when sorting to be @sort_func.
221 * If the current sort column id of @sortable is
222 * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, then the model will sort using
225 * If @sort_func is %NULL, then there will be no default comparison function.
226 * This means that once the model has been sorted, it can't go back to the
227 * default state. In this case, when the current sort column id of @sortable
228 * is %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, the model will be unsorted.
231 gtk_tree_sortable_set_default_sort_func (GtkTreeSortable *sortable,
232 GtkTreeIterCompareFunc sort_func,
234 GDestroyNotify destroy)
236 GtkTreeSortableIface *iface;
238 g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
240 iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
242 g_return_if_fail (iface != NULL);
243 g_return_if_fail (iface->set_default_sort_func != NULL);
245 (* iface->set_default_sort_func) (sortable, sort_func, user_data, destroy);
249 * gtk_tree_sortable_has_default_sort_func:
250 * @sortable: A #GtkTreeSortable
252 * Returns %TRUE if the model has a default sort function. This is used
253 * primarily by GtkTreeViewColumns in order to determine if a model can
254 * go back to the default state, or not.
256 * Return value: %TRUE, if the model has a default sort function
259 gtk_tree_sortable_has_default_sort_func (GtkTreeSortable *sortable)
261 GtkTreeSortableIface *iface;
263 g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
265 iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
267 g_return_val_if_fail (iface != NULL, FALSE);
268 g_return_val_if_fail (iface->has_default_sort_func != NULL, FALSE);
270 return (* iface->has_default_sort_func) (sortable);