gtktreesortable.c 8.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* gtktreesortable.c
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18
 */


19
#include "config.h"
20
#include "gtktreesortable.h"
21
#include "gtkmarshalers.h"
22
#include "gtkintl.h"
23

24

25 26 27 28 29 30 31 32 33 34 35 36
/**
 * SECTION:gtktreesortable
 * @Short_description: The interface for sortable models used by GtkTreeView
 * @Title: GtkTreeSortable
 * @See_also:#GtkTreeModel, #GtkTreeView
 *
 * #GtkTreeSortable is an interface to be implemented by tree models which
 * support sorting. The #GtkTreeView uses the methods provided by this interface
 * to sort the model.
 */


37 38
static void gtk_tree_sortable_base_init (gpointer g_class);

Manish Singh's avatar
Manish Singh committed
39
GType
40 41
gtk_tree_sortable_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
42
  static GType tree_sortable_type = 0;
43 44 45

  if (! tree_sortable_type)
    {
46
      const GTypeInfo tree_sortable_info =
47 48 49 50 51 52 53 54 55 56 57
      {
	sizeof (GtkTreeSortableIface), /* class_size */
	gtk_tree_sortable_base_init,   /* base_init */
	NULL,		/* base_finalize */
	NULL,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	0,
	0,
	NULL
      };
Manish Singh's avatar
Manish Singh committed
58 59

      tree_sortable_type =
60
	g_type_register_static (G_TYPE_INTERFACE, I_("GtkTreeSortable"),
Manish Singh's avatar
Manish Singh committed
61 62
				&tree_sortable_info, 0);

63
      g_type_interface_add_prerequisite (tree_sortable_type, GTK_TYPE_TREE_MODEL);
64 65 66 67 68 69 70 71 72 73 74 75
    }

  return tree_sortable_type;
}

static void
gtk_tree_sortable_base_init (gpointer g_class)
{
  static gboolean initialized = FALSE;

  if (! initialized)
    {
Matthias Clasen's avatar
Matthias Clasen committed
76 77 78 79 80 81 82 83
      /**
       * GtkTreeSortable::sort-column-changed:
       * @sortable: the object on which the signal is emitted
       *
       * The ::sort-column-changed signal is emitted when the sort column
       * or sort order of @sortable is changed. The signal is emitted before
       * the contents of @sortable are resorted.
       */
84
      g_signal_new (I_("sort-column-changed"),
85 86 87 88
                    GTK_TYPE_TREE_SORTABLE,
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GtkTreeSortableIface, sort_column_changed),
                    NULL, NULL,
89
                    _gtk_marshal_VOID__VOID,
90
                    G_TYPE_NONE, 0);
91 92 93 94
      initialized = TRUE;
    }
}

95 96 97 98
/**
 * gtk_tree_sortable_sort_column_changed:
 * @sortable: A #GtkTreeSortable
 * 
Matthias Clasen's avatar
Matthias Clasen committed
99 100
 * Emits a #GtkTreeSortable::sort-column-changed signal on @sortable.
 */
101 102 103 104 105
void
gtk_tree_sortable_sort_column_changed (GtkTreeSortable *sortable)
{
  g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));

106
  g_signal_emit_by_name (sortable, "sort-column-changed");
107 108
}

109 110 111
/**
 * gtk_tree_sortable_get_sort_column_id:
 * @sortable: A #GtkTreeSortable
112 113
 * @sort_column_id: (out): The sort column id to be filled in
 * @order: (out): The #GtkSortType to be filled in
114 115
 * 
 * Fills in @sort_column_id and @order with the current sort column and the
Matthias Clasen's avatar
Matthias Clasen committed
116
 * order. It returns %TRUE unless the @sort_column_id is 
117 118 119 120 121
 * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID or 
 * %GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID.
 * 
 * Return value: %TRUE if the sort column is not one of the special sort
 *   column ids.
122
 **/
123 124 125
gboolean
gtk_tree_sortable_get_sort_column_id (GtkTreeSortable  *sortable,
				      gint             *sort_column_id,
126
				      GtkSortType      *order)
127 128 129 130 131 132 133 134 135 136 137 138 139
{
  GtkTreeSortableIface *iface;

  g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

  g_return_val_if_fail (iface != NULL, FALSE);
  g_return_val_if_fail (iface->get_sort_column_id != NULL, FALSE);

  return (* iface->get_sort_column_id) (sortable, sort_column_id, order);
}

140 141 142 143 144 145
/**
 * gtk_tree_sortable_set_sort_column_id:
 * @sortable: A #GtkTreeSortable
 * @sort_column_id: the sort column id to set
 * @order: The sort order of the column
 * 
Matthias Clasen's avatar
Matthias Clasen committed
146
 * Sets the current sort column to be @sort_column_id. The @sortable will
147
 * resort itself to reflect this change, after emitting a
148
 * #GtkTreeSortable::sort-column-changed signal. @sort_column_id may either be
Matthias Clasen's avatar
Matthias Clasen committed
149 150 151 152 153 154 155 156 157 158 159 160
 * a regular column id, or one of the following special values:
 * <variablelist>
 * <varlistentry>
 *   <term>%GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID</term>
 *   <listitem>the default sort function will be used, if it is set</listitem>
 * </varlistentry>
 * <varlistentry>
 *   <term>%GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID</term>
 *   <listitem>no sorting will occur</listitem>
 * </varlistentry>
 * </variablelist>
 */
161 162 163
void
gtk_tree_sortable_set_sort_column_id (GtkTreeSortable  *sortable,
				      gint              sort_column_id,
164
				      GtkSortType       order)
165 166 167 168 169 170 171 172 173 174 175 176 177
{
  GtkTreeSortableIface *iface;

  g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

  g_return_if_fail (iface != NULL);
  g_return_if_fail (iface->set_sort_column_id != NULL);
  
  (* iface->set_sort_column_id) (sortable, sort_column_id, order);
}

178 179 180 181
/**
 * gtk_tree_sortable_set_sort_func:
 * @sortable: A #GtkTreeSortable
 * @sort_column_id: the sort column id to set the function for
Matthias Clasen's avatar
Matthias Clasen committed
182
 * @sort_func: The comparison function
183 184
 * @user_data: (allow-none): User data to pass to @sort_func, or %NULL
 * @destroy: (allow-none): Destroy notifier of @user_data, or %NULL
185
 * 
Matthias Clasen's avatar
Matthias Clasen committed
186 187 188 189
 * Sets the comparison function used when sorting to be @sort_func. If the
 * current sort column id of @sortable is the same as @sort_column_id, then 
 * the model will sort using this function.
 */
190
void
191 192
gtk_tree_sortable_set_sort_func (GtkTreeSortable        *sortable,
				 gint                    sort_column_id,
193 194
				 GtkTreeIterCompareFunc  sort_func,
				 gpointer                user_data,
195
				 GDestroyNotify          destroy)
196 197 198 199
{
  GtkTreeSortableIface *iface;

  g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));
200
  g_return_if_fail (sort_func != NULL);
201 202 203 204

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

  g_return_if_fail (iface != NULL);
205
  g_return_if_fail (iface->set_sort_func != NULL);
206
  g_return_if_fail (sort_column_id >= 0);
207

208
  (* iface->set_sort_func) (sortable, sort_column_id, sort_func, user_data, destroy);
209 210
}

211 212 213
/**
 * gtk_tree_sortable_set_default_sort_func:
 * @sortable: A #GtkTreeSortable
Matthias Clasen's avatar
Matthias Clasen committed
214
 * @sort_func: The comparison function
215 216
 * @user_data: (allow-none): User data to pass to @sort_func, or %NULL
 * @destroy: (allow-none): Destroy notifier of @user_data, or %NULL
217
 * 
Matthias Clasen's avatar
Matthias Clasen committed
218 219 220 221
 * Sets the default comparison function used when sorting to be @sort_func.  
 * If the current sort column id of @sortable is
 * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, then the model will sort using 
 * this function.
222 223 224
 *
 * If @sort_func is %NULL, then there will be no default comparison function.
 * This means that once the model  has been sorted, it can't go back to the
Matthias Clasen's avatar
Matthias Clasen committed
225 226 227
 * default state. In this case, when the current sort column id of @sortable 
 * is %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, the model will be unsorted.
 */
228 229
void
gtk_tree_sortable_set_default_sort_func (GtkTreeSortable        *sortable,
230 231
					 GtkTreeIterCompareFunc  sort_func,
					 gpointer                user_data,
232
					 GDestroyNotify          destroy)
233 234 235 236 237 238 239 240 241 242
{
  GtkTreeSortableIface *iface;

  g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

  g_return_if_fail (iface != NULL);
  g_return_if_fail (iface->set_default_sort_func != NULL);
  
243
  (* iface->set_default_sort_func) (sortable, sort_func, user_data, destroy);
244
}
245

246 247 248 249
/**
 * gtk_tree_sortable_has_default_sort_func:
 * @sortable: A #GtkTreeSortable
 * 
Matthias Clasen's avatar
Matthias Clasen committed
250 251 252
 * Returns %TRUE if the model has a default sort function. This is used
 * primarily by GtkTreeViewColumns in order to determine if a model can 
 * go back to the default state, or not.
253 254
 * 
 * Return value: %TRUE, if the model has a default sort function
Matthias Clasen's avatar
Matthias Clasen committed
255
 */
256 257 258 259 260
gboolean
gtk_tree_sortable_has_default_sort_func (GtkTreeSortable *sortable)
{
  GtkTreeSortableIface *iface;

261
  g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
262 263 264

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

265 266
  g_return_val_if_fail (iface != NULL, FALSE);
  g_return_val_if_fail (iface->has_default_sort_func != NULL, FALSE);
267
  
268
  return (* iface->has_default_sort_func) (sortable);
269
}