gtktreesortable.c 7.44 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 "gtkintl.h"
25
#include "gtkalias.h"
26 27 28

static void gtk_tree_sortable_base_init (gpointer g_class);

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

  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
48 49

      tree_sortable_type =
50
	g_type_register_static (G_TYPE_INTERFACE, I_("GtkTreeSortable"),
Manish Singh's avatar
Manish Singh committed
51 52
				&tree_sortable_info, 0);

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

  return tree_sortable_type;
}

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

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

77 78 79 80 81 82
/**
 * gtk_tree_sortable_sort_column_changed:
 * @sortable: A #GtkTreeSortable
 * 
 * Emits a GtkTreeSortable::sort_column_changed signal on 
 **/
83 84 85 86 87
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
88
  g_signal_emit_by_name (sortable, "sort_column_changed");
89 90
}

91 92 93 94 95 96 97
/**
 * 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
98 99 100 101 102 103
 * 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.
104
 **/
105 106 107
gboolean
gtk_tree_sortable_get_sort_column_id (GtkTreeSortable  *sortable,
				      gint             *sort_column_id,
108
				      GtkSortType      *order)
109 110 111 112 113 114 115 116 117 118 119 120 121
{
  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);
}

122 123 124 125 126 127 128 129
/**
 * 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
130 131 132
 * 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.
133
 **/
134 135 136
void
gtk_tree_sortable_set_sort_column_id (GtkTreeSortable  *sortable,
				      gint              sort_column_id,
137
				      GtkSortType       order)
138 139 140 141 142 143 144 145 146 147 148 149 150
{
  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);
}

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

  g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

  g_return_if_fail (iface != NULL);
177
  g_return_if_fail (iface->set_sort_func != NULL);
178
  g_return_if_fail (sort_column_id >= 0);
179

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

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

218 219 220 221 222 223 224 225 226 227
/**
 * 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
 **/
228 229 230 231 232
gboolean
gtk_tree_sortable_has_default_sort_func (GtkTreeSortable *sortable)
{
  GtkTreeSortableIface *iface;

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

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

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

#define __GTK_TREE_SORTABLE_C__
#include "gtkaliasdef.c"