gtktreesortable.c 7.28 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 25 26

static void gtk_tree_sortable_base_init (gpointer g_class);

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

  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
46 47 48 49 50

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

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

  return tree_sortable_type;
}

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

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

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

89 90 91 92 93 94 95 96 97 98 99 100
/**
 * 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
 * order, if applicable.  If the sort column is not set, then FALSE is returned,
 * and the values in @sort_column_id and @order are unchanged.
  * 
 * Return value: %TRUE, if the sort column has been set
 **/
101 102 103
gboolean
gtk_tree_sortable_get_sort_column_id (GtkTreeSortable  *sortable,
				      gint             *sort_column_id,
104
				      GtkSortType      *order)
105 106 107 108 109 110 111 112 113 114 115 116 117
{
  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);
}

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

147 148 149 150 151 152 153 154 155 156
/**
 * gtk_tree_sortable_set_sort_func:
 * @sortable: A #GtkTreeSortable
 * @sort_column_id: the sort column id to set the function for
 * @sort_func: The sorting function
 * @user_data: User data to pass to the sort func, or %NULL
 * @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
157
 * model will sort using this function.
158
 **/
159
void
160 161
gtk_tree_sortable_set_sort_func (GtkTreeSortable        *sortable,
				 gint                    sort_column_id,
162 163
				 GtkTreeIterCompareFunc  sort_func,
				 gpointer                user_data,
164
				 GtkDestroyNotify        destroy)
165 166 167 168 169 170 171 172
{
  GtkTreeSortableIface *iface;

  g_return_if_fail (GTK_IS_TREE_SORTABLE (sortable));

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

  g_return_if_fail (iface != NULL);
173
  g_return_if_fail (iface->set_sort_func != NULL);
174
  g_return_if_fail (sort_column_id >= 0);
Jonathan Blandford's avatar
Jonathan Blandford committed
175

176
  (* iface->set_sort_func) (sortable, sort_column_id, sort_func, user_data, destroy);
177 178
}

179 180 181 182 183 184 185 186
/**
 * gtk_tree_sortable_set_default_sort_func:
 * @sortable: A #GtkTreeSortable
 * @sort_func: The sorting function
 * @user_data: User data to pass to the sort func, or %NULL
 * @destroy: Destroy notifier of @user_data, or %NULL
 * 
 * Sets the default comparison function used when sorting to be @sort_func.  If
Jonathan Blandford's avatar
Jonathan Blandford committed
187
 * the current sort column id of @sortable is
188 189 190 191 192 193
 * %GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, then the model will sort using this function.
 *
 * 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.
194
 **/
195 196
void
gtk_tree_sortable_set_default_sort_func (GtkTreeSortable        *sortable,
197 198
					 GtkTreeIterCompareFunc  sort_func,
					 gpointer                user_data,
199 200 201 202 203 204 205 206 207 208 209
					 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);
  
210
  (* iface->set_default_sort_func) (sortable, sort_func, user_data, destroy);
211
}
212

213 214 215 216 217 218 219 220 221 222
/**
 * 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
 **/
223 224 225 226 227
gboolean
gtk_tree_sortable_has_default_sort_func (GtkTreeSortable *sortable)
{
  GtkTreeSortableIface *iface;

228
  g_return_val_if_fail (GTK_IS_TREE_SORTABLE (sortable), FALSE);
229 230 231

  iface = GTK_TREE_SORTABLE_GET_IFACE (sortable);

232 233
  g_return_val_if_fail (iface != NULL, FALSE);
  g_return_val_if_fail (iface->has_default_sort_func != NULL, FALSE);
234
  
235
  return (* iface->has_default_sort_func) (sortable);
236
}