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