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