]> Pileus Git - ~andy/gtk/blob - gtk/gtktreesortable.c
Add editable interface. This should be the last big GtkTreeView API
[~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 -1, then
123  * the default sort function will be used, if it is set.
124  **/
125 void
126 gtk_tree_sortable_set_sort_column_id (GtkTreeSortable  *sortable,
127                                       gint              sort_column_id,
128                                       GtkSortType       order)
129 {
130   GtkTreeSortableIface *iface;
131
132   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
133
134   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
135
136   g_return_if_fail (iface != NULL);
137   g_return_if_fail (iface->set_sort_column_id != NULL);
138   
139   (* iface->set_sort_column_id) (sortable, sort_column_id, order);
140
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   
171   (* iface->set_sort_func) (sortable, sort_column_id, sort_func, user_data, destroy);
172 }
173
174 /**
175  * gtk_tree_sortable_set_default_sort_func:
176  * @sortable: A #GtkTreeSortable
177  * @sort_func: The sorting function
178  * @user_data: User data to pass to the sort func, or %NULL
179  * @destroy: Destroy notifier of @user_data, or %NULL
180  * 
181  * Sets the default comparison function used when sorting to be @sort_func.  If
182  * the current sort column id of @sortable is the same as @sort_column_id, then
183  * the model will sort.
184  **/
185 void
186 gtk_tree_sortable_set_default_sort_func (GtkTreeSortable        *sortable,
187                                          GtkTreeIterCompareFunc  sort_func,
188                                          gpointer                user_data,
189                                          GtkDestroyNotify        destroy)
190 {
191   GtkTreeSortableIface *iface;
192
193   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
194
195   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
196
197   g_return_if_fail (iface != NULL);
198   g_return_if_fail (iface->set_default_sort_func != NULL);
199   
200   (* iface->set_default_sort_func) (sortable, sort_func, user_data, destroy);
201 }
202
203 /**
204  * gtk_tree_sortable_has_default_sort_func:
205  * @sortable: A #GtkTreeSortable
206  * 
207  * Returns %TRUE if the model has a default sort function.  This is used
208  * primarily by GtkTreeViewColumns in order to determine if a model can go back
209  * to the default state, or not.
210  * 
211  * Return value: %TRUE, if the model has a default sort function
212  **/
213 gboolean
214 gtk_tree_sortable_has_default_sort_func (GtkTreeSortable *sortable)
215 {
216   GtkTreeSortableIface *iface;
217
218   g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
219
220   iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);
221
222   g_return_if_fail (iface != NULL);
223   g_return_if_fail (iface->has_default_sort_func != NULL);
224   
225   (* iface->has_default_sort_func) (sortable);
226 }