gtktreesortable.c 7.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


21
#include <config.h>
22
#include "gtktreesortable.h"
23
#include "gtkmarshalers.h"
24
#include "gtkalias.h"
25 26 27

static void gtk_tree_sortable_base_init (gpointer g_class);

Manish Singh's avatar
Manish Singh committed
28
GType
29 30
gtk_tree_sortable_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
31
  static GType tree_sortable_type = 0;
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

  if (! tree_sortable_type)
    {
      static const GTypeInfo tree_sortable_info =
      {
	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
47 48 49 50 51

      tree_sortable_type =
	g_type_register_static (G_TYPE_INTERFACE, "GtkTreeSortable",
				&tree_sortable_info, 0);

52
      g_type_interface_add_prerequisite (tree_sortable_type, GTK_TYPE_TREE_MODEL);
53 54 55 56 57 58 59 60 61 62 63 64
    }

  return tree_sortable_type;
}

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

  if (! initialized)
    {
65 66 67 68 69
      g_signal_new ("sort_column_changed",
                    GTK_TYPE_TREE_SORTABLE,
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GtkTreeSortableIface, sort_column_changed),
                    NULL, NULL,
70
                    _gtk_marshal_VOID__VOID,
71
                    G_TYPE_NONE, 0);
72 73 74 75
      initialized = TRUE;
    }
}

76 77 78 79 80 81
/**
 * gtk_tree_sortable_sort_column_changed:
 * @sortable: A #GtkTreeSortable
 * 
 * Emits a GtkTreeSortable::sort_column_changed signal on 
 **/
82 83 84 85 86
void
gtk_tree_sortable_sort_column_changed (GtkTreeSortable *sortable)
{
  g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));

Manish Singh's avatar
Manish Singh committed
87
  g_signal_emit_by_name (sortable, "sort_column_changed");
88 89
}

90 91 92 93 94 95 96
/**
 * gtk_tree_sortable_get_sort_column_id:
 * @sortable: A #GtkTreeSortable
 * @sort_column_id: The sort column id to be filled in
 * @order: The #GtkSortType to be filled in
 * 
 * Fills in @sort_column_id and @order with the current sort column and the
97 98 99 100 101 102
 * order.  It returns %TRUE unless the @sort_column_id is 
 * %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.
103
 **/
104 105 106
gboolean
gtk_tree_sortable_get_sort_column_id (GtkTreeSortable  *sortable,
				      gint             *sort_column_id,
107
				      GtkSortType      *order)
108 109 110 111 112 113 114 115 116 117 118 119 120
{
  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);
}

121 122 123 124 125 126 127 128
/**
 * 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
 * 
 * Sets the current sort column to be @sort_column_id.  The @sortable will
 * resort itself to reflect this change, after emitting a
Jonathan Blandford's avatar
Jonathan Blandford committed
129 130 131
 * GtkTreeSortable::sort_column_changed signal.  If @sort_column_id is
 * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, then the default sort function
 * will be used, if it is set.
132
 **/
133 134 135
void
gtk_tree_sortable_set_sort_column_id (GtkTreeSortable  *sortable,
				      gint              sort_column_id,
136
				      GtkSortType       order)
137 138 139 140 141 142 143 144 145 146 147 148 149
{
  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);
}

150 151 152 153
/**
 * 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
154 155
 * @sort_func: The comparison function
 * @user_data: User data to pass to @sort_func, or %NULL
156 157 158 159
 * @destroy: Destroy notifier of @user_data, or %NULL
 * 
 * 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
160
 * model will sort using this function.
161
 **/
162
void
163 164
gtk_tree_sortable_set_sort_func (GtkTreeSortable        *sortable,
				 gint                    sort_column_id,
165 166
				 GtkTreeIterCompareFunc  sort_func,
				 gpointer                user_data,
167
				 GtkDestroyNotify        destroy)
168 169 170 171 172 173 174 175
{
  GtkTreeSortableIface *iface;

  g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

  g_return_if_fail (iface != NULL);
176
  g_return_if_fail (iface->set_sort_func != NULL);
177
  g_return_if_fail (sort_column_id >= 0);
Jonathan Blandford's avatar
Jonathan Blandford committed
178

179
  (* iface->set_sort_func) (sortable, sort_column_id, sort_func, user_data, destroy);
180 181
}

182 183 184
/**
 * gtk_tree_sortable_set_default_sort_func:
 * @sortable: A #GtkTreeSortable
Matthias Clasen's avatar
Matthias Clasen committed
185 186
 * @sort_func: The comparison function
 * @user_data: User data to pass to @sort_func, or %NULL
187 188
 * @destroy: Destroy notifier of @user_data, or %NULL
 * 
Matthias Clasen's avatar
Matthias Clasen committed
189 190 191 192
 * 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.
193 194 195 196 197
 *
 * 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
 * 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.
198
 **/
199 200
void
gtk_tree_sortable_set_default_sort_func (GtkTreeSortable        *sortable,
201 202
					 GtkTreeIterCompareFunc  sort_func,
					 gpointer                user_data,
203 204 205 206 207 208 209 210 211 212 213
					 GtkDestroyNotify        destroy)
{
  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);
  
214
  (* iface->set_default_sort_func) (sortable, sort_func, user_data, destroy);
215
}
216

217 218 219 220 221 222 223 224 225 226
/**
 * gtk_tree_sortable_has_default_sort_func:
 * @sortable: A #GtkTreeSortable
 * 
 * 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.
 * 
 * Return value: %TRUE, if the model has a default sort function
 **/
227 228 229 230 231
gboolean
gtk_tree_sortable_has_default_sort_func (GtkTreeSortable *sortable)
{
  GtkTreeSortableIface *iface;

232
  g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
233 234 235

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

236 237
  g_return_val_if_fail (iface != NULL, FALSE);
  g_return_val_if_fail (iface->has_default_sort_func != NULL, FALSE);
238
  
239
  return (* iface->has_default_sort_func) (sortable);
240
}
241 242 243

#define __GTK_TREE_SORTABLE_C__
#include "gtkaliasdef.c"