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
gtk_tree_sortable_get_type(void)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
gtk_tree_sortable_base_init(gpointer g_class)60 gtk_tree_sortable_base_init (gpointer g_class)
61 {
62   static gboolean initialized = FALSE;
63 
64   if (! initialized)
65     {
66       /**
67        * GtkTreeSortable::sort-column-changed:
68        * @sortable: the object on which the signal is emitted
69        *
70        * The ::sort-column-changed signal is emitted when the sort column
71        * or sort order of @sortable is changed. The signal is emitted before
72        * the contents of @sortable are resorted.
73        */
74       g_signal_new (I_("sort-column-changed"),
75                     GTK_TYPE_TREE_SORTABLE,
76                     G_SIGNAL_RUN_LAST,
77                     G_STRUCT_OFFSET (GtkTreeSortableIface, sort_column_changed),
78                     NULL, NULL,
79                     _gtk_marshal_VOID__VOID,
80                     G_TYPE_NONE, 0);
81       initialized = TRUE;
82     }
83 }
84 
85 /**
86  * gtk_tree_sortable_sort_column_changed:
87  * @sortable: A #GtkTreeSortable
88  *
89  * Emits a #GtkTreeSortable::sort-column-changed signal on @sortable.
90  */
91 void
gtk_tree_sortable_sort_column_changed(GtkTreeSortable * sortable)92 gtk_tree_sortable_sort_column_changed (GtkTreeSortable *sortable)
93 {
94   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
95 
96   g_signal_emit_by_name (sortable, "sort-column-changed");
97 }
98 
99 /**
100  * gtk_tree_sortable_get_sort_column_id:
101  * @sortable: A #GtkTreeSortable
102  * @sort_column_id: The sort column id to be filled in
103  * @order: The #GtkSortType to be filled in
104  *
105  * Fills in @sort_column_id and @order with the current sort column and the
106  * order. It returns %TRUE unless the @sort_column_id is
107  * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID or
108  * %GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID.
109  *
110  * Return value: %TRUE if the sort column is not one of the special sort
111  *   column ids.
112  **/
113 gboolean
gtk_tree_sortable_get_sort_column_id(GtkTreeSortable * sortable,gint * sort_column_id,GtkSortType * order)114 gtk_tree_sortable_get_sort_column_id (GtkTreeSortable  *sortable,
115 				      gint             *sort_column_id,
116 				      GtkSortType      *order)
117 {
118   GtkTreeSortableIface *iface;
119 
120   g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
121 
122   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
123 
124   g_return_val_if_fail (iface != NULL, FALSE);
125   g_return_val_if_fail (iface->get_sort_column_id != NULL, FALSE);
126 
127   return (* iface->get_sort_column_id) (sortable, sort_column_id, order);
128 }
129 
130 /**
131  * gtk_tree_sortable_set_sort_column_id:
132  * @sortable: A #GtkTreeSortable
133  * @sort_column_id: the sort column id to set
134  * @order: The sort order of the column
135  *
136  * Sets the current sort column to be @sort_column_id. The @sortable will
137  * resort itself to reflect this change, after emitting a
138  * #GtkTreeSortable::sort-column-changed signal. @sort_column_id may either be
139  * a regular column id, or one of the following special values:
140  * <variablelist>
141  * <varlistentry>
142  *   <term>%GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID</term>
143  *   <listitem>the default sort function will be used, if it is set</listitem>
144  * </varlistentry>
145  * <varlistentry>
146  *   <term>%GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID</term>
147  *   <listitem>no sorting will occur</listitem>
148  * </varlistentry>
149  * </variablelist>
150  */
151 void
gtk_tree_sortable_set_sort_column_id(GtkTreeSortable * sortable,gint sort_column_id,GtkSortType order)152 gtk_tree_sortable_set_sort_column_id (GtkTreeSortable  *sortable,
153 				      gint              sort_column_id,
154 				      GtkSortType       order)
155 {
156   GtkTreeSortableIface *iface;
157 
158   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
159 
160   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
161 
162   g_return_if_fail (iface != NULL);
163   g_return_if_fail (iface->set_sort_column_id != NULL);
164 
165   (* iface->set_sort_column_id) (sortable, sort_column_id, order);
166 }
167 
168 /**
169  * gtk_tree_sortable_set_sort_func:
170  * @sortable: A #GtkTreeSortable
171  * @sort_column_id: the sort column id to set the function for
172  * @sort_func: The comparison function
173  * @user_data: (allow-none): User data to pass to @sort_func, or %NULL
174  * @destroy: (allow-none): Destroy notifier of @user_data, or %NULL
175  *
176  * Sets the comparison function used when sorting to be @sort_func. If the
177  * current sort column id of @sortable is the same as @sort_column_id, then
178  * the model will sort using this function.
179  */
180 void
gtk_tree_sortable_set_sort_func(GtkTreeSortable * sortable,gint sort_column_id,GtkTreeIterCompareFunc sort_func,gpointer user_data,GDestroyNotify destroy)181 gtk_tree_sortable_set_sort_func (GtkTreeSortable        *sortable,
182 				 gint                    sort_column_id,
183 				 GtkTreeIterCompareFunc  sort_func,
184 				 gpointer                user_data,
185 				 GDestroyNotify          destroy)
186 {
187   GtkTreeSortableIface *iface;
188 
189   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
190   g_return_if_fail (sort_func != NULL);
191 
192   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
193 
194   g_return_if_fail (iface != NULL);
195   g_return_if_fail (iface->set_sort_func != NULL);
196   g_return_if_fail (sort_column_id >= 0);
197 
198   (* iface->set_sort_func) (sortable, sort_column_id, sort_func, user_data, destroy);
199 }
200 
201 /**
202  * gtk_tree_sortable_set_default_sort_func:
203  * @sortable: A #GtkTreeSortable
204  * @sort_func: The comparison function
205  * @user_data: (allow-none): User data to pass to @sort_func, or %NULL
206  * @destroy: (allow-none): Destroy notifier of @user_data, or %NULL
207  *
208  * Sets the default comparison function used when sorting to be @sort_func.
209  * If the current sort column id of @sortable is
210  * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, then the model will sort using
211  * this function.
212  *
213  * If @sort_func is %NULL, then there will be no default comparison function.
214  * This means that once the model  has been sorted, it can't go back to the
215  * default state. In this case, when the current sort column id of @sortable
216  * is %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, the model will be unsorted.
217  */
218 void
gtk_tree_sortable_set_default_sort_func(GtkTreeSortable * sortable,GtkTreeIterCompareFunc sort_func,gpointer user_data,GDestroyNotify destroy)219 gtk_tree_sortable_set_default_sort_func (GtkTreeSortable        *sortable,
220 					 GtkTreeIterCompareFunc  sort_func,
221 					 gpointer                user_data,
222 					 GDestroyNotify          destroy)
223 {
224   GtkTreeSortableIface *iface;
225 
226   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
227 
228   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
229 
230   g_return_if_fail (iface != NULL);
231   g_return_if_fail (iface->set_default_sort_func != NULL);
232 
233   (* iface->set_default_sort_func) (sortable, sort_func, user_data, destroy);
234 }
235 
236 /**
237  * gtk_tree_sortable_has_default_sort_func:
238  * @sortable: A #GtkTreeSortable
239  *
240  * Returns %TRUE if the model has a default sort function. This is used
241  * primarily by GtkTreeViewColumns in order to determine if a model can
242  * go back to the default state, or not.
243  *
244  * Return value: %TRUE, if the model has a default sort function
245  */
246 gboolean
gtk_tree_sortable_has_default_sort_func(GtkTreeSortable * sortable)247 gtk_tree_sortable_has_default_sort_func (GtkTreeSortable *sortable)
248 {
249   GtkTreeSortableIface *iface;
250 
251   g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
252 
253   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
254 
255   g_return_val_if_fail (iface != NULL, FALSE);
256   g_return_val_if_fail (iface->has_default_sort_func != NULL, FALSE);
257 
258   return (* iface->has_default_sort_func) (sortable);
259 }
260 
261 #define __GTK_TREE_SORTABLE_C__
262 #include "gtkaliasdef.c"
263