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, see <http://www.gnu.org/licenses/>.
20 #include "gtktreesortable.h"
21 #include "gtkmarshalers.h"
26 * SECTION:gtktreesortable
27 * @Short_description: The interface for sortable models used by GtkTreeView
28 * @Title: GtkTreeSortable
29 * @See_also:#GtkTreeModel, #GtkTreeView
31 * #GtkTreeSortable is an interface to be implemented by tree models which
32 * support sorting. The #GtkTreeView uses the methods provided by this interface
37 static void gtk_tree_sortable_base_init (gpointer g_class);
40 gtk_tree_sortable_get_type (void)
42 static GType tree_sortable_type = 0;
44 if (! tree_sortable_type)
46 const GTypeInfo tree_sortable_info =
48 sizeof (GtkTreeSortableIface), /* class_size */
49 gtk_tree_sortable_base_init, /* base_init */
50 NULL, /* base_finalize */
52 NULL, /* class_finalize */
53 NULL, /* class_data */
60 g_type_register_static (G_TYPE_INTERFACE, I_("GtkTreeSortable"),
61 &tree_sortable_info, 0);
63 g_type_interface_add_prerequisite (tree_sortable_type, GTK_TYPE_TREE_MODEL);
66 return tree_sortable_type;
70 gtk_tree_sortable_base_init (gpointer g_class)
72 static gboolean initialized = FALSE;
77 * GtkTreeSortable::sort-column-changed:
78 * @sortable: the object on which the signal is emitted
80 * The ::sort-column-changed signal is emitted when the sort column
81 * or sort order of @sortable is changed. The signal is emitted before
82 * the contents of @sortable are resorted.
84 g_signal_new (I_("sort-column-changed"),
85 GTK_TYPE_TREE_SORTABLE,
87 G_STRUCT_OFFSET (GtkTreeSortableIface, sort_column_changed),
89 _gtk_marshal_VOID__VOID,
96 * gtk_tree_sortable_sort_column_changed:
97 * @sortable: A #GtkTreeSortable
99 * Emits a #GtkTreeSortable::sort-column-changed signal on @sortable.
102 gtk_tree_sortable_sort_column_changed (GtkTreeSortable *sortable)
104 g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
106 g_signal_emit_by_name (sortable, "sort-column-changed");
110 * gtk_tree_sortable_get_sort_column_id:
111 * @sortable: A #GtkTreeSortable
112 * @sort_column_id: (out): The sort column id to be filled in
113 * @order: (out): The #GtkSortType to be filled in
115 * Fills in @sort_column_id and @order with the current sort column and the
116 * order. It returns %TRUE unless the @sort_column_id is
117 * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID or
118 * %GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID.
120 * Return value: %TRUE if the sort column is not one of the special sort
124 gtk_tree_sortable_get_sort_column_id (GtkTreeSortable *sortable,
125 gint *sort_column_id,
128 GtkTreeSortableIface *iface;
130 g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
132 iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
134 g_return_val_if_fail (iface != NULL, FALSE);
135 g_return_val_if_fail (iface->get_sort_column_id != NULL, FALSE);
137 return (* iface->get_sort_column_id) (sortable, sort_column_id, order);
141 * gtk_tree_sortable_set_sort_column_id:
142 * @sortable: A #GtkTreeSortable
143 * @sort_column_id: the sort column id to set
144 * @order: The sort order of the column
146 * Sets the current sort column to be @sort_column_id. The @sortable will
147 * resort itself to reflect this change, after emitting a
148 * #GtkTreeSortable::sort-column-changed signal. @sort_column_id may either be
149 * a regular column id, or one of the following special values:
152 * <term>%GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID</term>
153 * <listitem>the default sort function will be used, if it is set</listitem>
156 * <term>%GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID</term>
157 * <listitem>no sorting will occur</listitem>
162 gtk_tree_sortable_set_sort_column_id (GtkTreeSortable *sortable,
166 GtkTreeSortableIface *iface;
168 g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
170 iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
172 g_return_if_fail (iface != NULL);
173 g_return_if_fail (iface->set_sort_column_id != NULL);
175 (* iface->set_sort_column_id) (sortable, sort_column_id, order);
179 * gtk_tree_sortable_set_sort_func:
180 * @sortable: A #GtkTreeSortable
181 * @sort_column_id: the sort column id to set the function for
182 * @sort_func: The comparison function
183 * @user_data: (allow-none): User data to pass to @sort_func, or %NULL
184 * @destroy: (allow-none): Destroy notifier of @user_data, or %NULL
186 * Sets the comparison function used when sorting to be @sort_func. If the
187 * current sort column id of @sortable is the same as @sort_column_id, then
188 * the model will sort using this function.
191 gtk_tree_sortable_set_sort_func (GtkTreeSortable *sortable,
193 GtkTreeIterCompareFunc sort_func,
195 GDestroyNotify destroy)
197 GtkTreeSortableIface *iface;
199 g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
200 g_return_if_fail (sort_func != NULL);
202 iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
204 g_return_if_fail (iface != NULL);
205 g_return_if_fail (iface->set_sort_func != NULL);
206 g_return_if_fail (sort_column_id >= 0);
208 (* iface->set_sort_func) (sortable, sort_column_id, sort_func, user_data, destroy);
212 * gtk_tree_sortable_set_default_sort_func:
213 * @sortable: A #GtkTreeSortable
214 * @sort_func: The comparison function
215 * @user_data: (allow-none): User data to pass to @sort_func, or %NULL
216 * @destroy: (allow-none): Destroy notifier of @user_data, or %NULL
218 * Sets the default comparison function used when sorting to be @sort_func.
219 * If the current sort column id of @sortable is
220 * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, then the model will sort using
223 * If @sort_func is %NULL, then there will be no default comparison function.
224 * This means that once the model has been sorted, it can't go back to the
225 * default state. In this case, when the current sort column id of @sortable
226 * is %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, the model will be unsorted.
229 gtk_tree_sortable_set_default_sort_func (GtkTreeSortable *sortable,
230 GtkTreeIterCompareFunc sort_func,
232 GDestroyNotify destroy)
234 GtkTreeSortableIface *iface;
236 g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
238 iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
240 g_return_if_fail (iface != NULL);
241 g_return_if_fail (iface->set_default_sort_func != NULL);
243 (* iface->set_default_sort_func) (sortable, sort_func, user_data, destroy);
247 * gtk_tree_sortable_has_default_sort_func:
248 * @sortable: A #GtkTreeSortable
250 * Returns %TRUE if the model has a default sort function. This is used
251 * primarily by GtkTreeViewColumns in order to determine if a model can
252 * go back to the default state, or not.
254 * Return value: %TRUE, if the model has a default sort function
257 gtk_tree_sortable_has_default_sort_func (GtkTreeSortable *sortable)
259 GtkTreeSortableIface *iface;
261 g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
263 iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
265 g_return_val_if_fail (iface != NULL, FALSE);
266 g_return_val_if_fail (iface->has_default_sort_func != NULL, FALSE);
268 return (* iface->has_default_sort_func) (sortable);