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