]> Pileus Git - ~andy/gtk/blob - gtk/gtktreesortable.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / gtk / gtktreesortable.c
1 /* gtktreesortable.c
2  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20
21 #include <config.h>
22 #include "gtktreesortable.h"
23 #include "gtkmarshalers.h"
24
25 static void gtk_tree_sortable_base_init (gpointer g_class);
26
27 GType
28 gtk_tree_sortable_get_type (void)
29 {
30   static GType tree_sortable_type = 0;
31
32   if (! tree_sortable_type)
33     {
34       static const GTypeInfo tree_sortable_info =
35       {
36         sizeof (GtkTreeSortableIface), /* class_size */
37         gtk_tree_sortable_base_init,   /* base_init */
38         NULL,           /* base_finalize */
39         NULL,
40         NULL,           /* class_finalize */
41         NULL,           /* class_data */
42         0,
43         0,
44         NULL
45       };
46
47       tree_sortable_type =
48         g_type_register_static (G_TYPE_INTERFACE, "GtkTreeSortable",
49                                 &tree_sortable_info, 0);
50
51       g_type_interface_add_prerequisite (tree_sortable_type, GTK_TYPE_TREE_MODEL);
52     }
53
54   return tree_sortable_type;
55 }
56
57 static void
58 gtk_tree_sortable_base_init (gpointer g_class)
59 {
60   static gboolean initialized = FALSE;
61
62   if (! initialized)
63     {
64       g_signal_new ("sort_column_changed",
65                     GTK_TYPE_TREE_SORTABLE,
66                     G_SIGNAL_RUN_LAST,
67                     G_STRUCT_OFFSET (GtkTreeSortableIface, sort_column_changed),
68                     NULL, NULL,
69                     _gtk_marshal_VOID__VOID,
70                     G_TYPE_NONE, 0);
71       initialized = TRUE;
72     }
73 }
74
75 /**
76  * gtk_tree_sortable_sort_column_changed:
77  * @sortable: A #GtkTreeSortable
78  * 
79  * Emits a GtkTreeSortable::sort_column_changed signal on 
80  **/
81 void
82 gtk_tree_sortable_sort_column_changed (GtkTreeSortable *sortable)
83 {
84   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
85
86   g_signal_emit_by_name (sortable, "sort_column_changed");
87 }
88
89 /**
90  * gtk_tree_sortable_get_sort_column_id:
91  * @sortable: A #GtkTreeSortable
92  * @sort_column_id: The sort column id to be filled in
93  * @order: The #GtkSortType to be filled in
94  * 
95  * Fills in @sort_column_id and @order with the current sort column and the
96  * order, if applicable.  If the sort column is not set, then FALSE is returned,
97  * and the values in @sort_column_id and @order are unchanged.
98   * 
99  * Return value: %TRUE, if the sort column has been set
100  **/
101 gboolean
102 gtk_tree_sortable_get_sort_column_id (GtkTreeSortable  *sortable,
103                                       gint             *sort_column_id,
104                                       GtkSortType      *order)
105 {
106   GtkTreeSortableIface *iface;
107
108   g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
109
110   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
111
112   g_return_val_if_fail (iface != NULL, FALSE);
113   g_return_val_if_fail (iface->get_sort_column_id != NULL, FALSE);
114
115   return (* iface->get_sort_column_id) (sortable, sort_column_id, order);
116 }
117
118 /**
119  * gtk_tree_sortable_set_sort_column_id:
120  * @sortable: A #GtkTreeSortable
121  * @sort_column_id: the sort column id to set
122  * @order: The sort order of the column
123  * 
124  * Sets the current sort column to be @sort_column_id.  The @sortable will
125  * resort itself to reflect this change, after emitting a
126  * GtkTreeSortable::sort_column_changed signal.  If @sort_column_id is
127  * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, then the default sort function
128  * will be used, if it is set.
129  **/
130 void
131 gtk_tree_sortable_set_sort_column_id (GtkTreeSortable  *sortable,
132                                       gint              sort_column_id,
133                                       GtkSortType       order)
134 {
135   GtkTreeSortableIface *iface;
136
137   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
138
139   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
140
141   g_return_if_fail (iface != NULL);
142   g_return_if_fail (iface->set_sort_column_id != NULL);
143   
144   (* iface->set_sort_column_id) (sortable, sort_column_id, order);
145 }
146
147 /**
148  * gtk_tree_sortable_set_sort_func:
149  * @sortable: A #GtkTreeSortable
150  * @sort_column_id: the sort column id to set the function for
151  * @sort_func: The sorting function
152  * @user_data: User data to pass to the sort func, or %NULL
153  * @destroy: Destroy notifier of @user_data, or %NULL
154  * 
155  * Sets the comparison function used when sorting to be @sort_func.  If the
156  * current sort column id of @sortable is the same as @sort_column_id, then the
157  * model will sort using this function.
158  **/
159 void
160 gtk_tree_sortable_set_sort_func (GtkTreeSortable        *sortable,
161                                  gint                    sort_column_id,
162                                  GtkTreeIterCompareFunc  sort_func,
163                                  gpointer                user_data,
164                                  GtkDestroyNotify        destroy)
165 {
166   GtkTreeSortableIface *iface;
167
168   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
169
170   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
171
172   g_return_if_fail (iface != NULL);
173   g_return_if_fail (iface->set_sort_func != NULL);
174   g_return_if_fail (sort_column_id >= 0);
175
176   (* iface->set_sort_func) (sortable, sort_column_id, sort_func, user_data, destroy);
177 }
178
179 /**
180  * gtk_tree_sortable_set_default_sort_func:
181  * @sortable: A #GtkTreeSortable
182  * @sort_func: The sorting function
183  * @user_data: User data to pass to the sort func, or %NULL
184  * @destroy: Destroy notifier of @user_data, or %NULL
185  * 
186  * Sets the default comparison function used when sorting to be @sort_func.  If
187  * the current sort column id of @sortable is
188  * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, then the model will sort using this function.
189  *
190  * If @sort_func is %NULL, then there will be no default comparison function.
191  * This means that once the model  has been sorted, it can't go back to the
192  * default state. In this case, when the current sort column id of @sortable is
193  * GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, the model will be unsorted.
194  **/
195 void
196 gtk_tree_sortable_set_default_sort_func (GtkTreeSortable        *sortable,
197                                          GtkTreeIterCompareFunc  sort_func,
198                                          gpointer                user_data,
199                                          GtkDestroyNotify        destroy)
200 {
201   GtkTreeSortableIface *iface;
202
203   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
204
205   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
206
207   g_return_if_fail (iface != NULL);
208   g_return_if_fail (iface->set_default_sort_func != NULL);
209   
210   (* iface->set_default_sort_func) (sortable, sort_func, user_data, destroy);
211 }
212
213 /**
214  * gtk_tree_sortable_has_default_sort_func:
215  * @sortable: A #GtkTreeSortable
216  * 
217  * Returns %TRUE if the model has a default sort function.  This is used
218  * primarily by GtkTreeViewColumns in order to determine if a model can go back
219  * to the default state, or not.
220  * 
221  * Return value: %TRUE, if the model has a default sort function
222  **/
223 gboolean
224 gtk_tree_sortable_has_default_sort_func (GtkTreeSortable *sortable)
225 {
226   GtkTreeSortableIface *iface;
227
228   g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
229
230   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
231
232   g_return_val_if_fail (iface != NULL, FALSE);
233   g_return_val_if_fail (iface->has_default_sort_func != NULL, FALSE);
234   
235   return (* iface->has_default_sort_func) (sortable);
236 }