gtktreedatalist.c 14.1 KB
Newer Older
1
/* gtktreedatalist.c
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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.
18 19 20
 * 
 * This file contains code shared between GtkTreeStore and GtkListStore.  Please
 * do not use it.
21 22
 */

23
#include "config.h"
24
#include "gtktreedatalist.h"
25
#include "gtkalias.h"
26
#include <string.h>
27

28 29 30
/* node allocation
 */
GtkTreeDataList *
31
_gtk_tree_data_list_alloc (void)
32 33 34
{
  GtkTreeDataList *list;

35
  list = g_slice_new0 (GtkTreeDataList);
36 37 38 39 40

  return list;
}

void
41 42
_gtk_tree_data_list_free (GtkTreeDataList *list,
			  GType           *column_headers)
43
{
44
  GtkTreeDataList *tmp, *next;
45 46
  gint i = 0;

47 48 49
  tmp = list;

  while (tmp)
50
    {
51
      next = tmp->next;
52 53
      if (g_type_is_a (column_headers [i], G_TYPE_STRING))
	g_free ((gchar *) tmp->data.v_pointer);
54
      else if (g_type_is_a (column_headers [i], G_TYPE_OBJECT) && tmp->data.v_pointer != NULL)
Manish Singh's avatar
Manish Singh committed
55
	g_object_unref (tmp->data.v_pointer);
56
      else if (g_type_is_a (column_headers [i], G_TYPE_BOXED) && tmp->data.v_pointer != NULL)
57
	g_boxed_free (column_headers [i], (gpointer) tmp->data.v_pointer);
58

59
      g_slice_free (GtkTreeDataList, tmp);
60
      i++;
61
      tmp = next;
62
    }
63 64
}

65 66 67 68
gboolean
_gtk_tree_data_list_check_type (GType type)
{
  gint i = 0;
69
  static const GType type_list[] =
70 71 72 73 74 75
  {
    G_TYPE_BOOLEAN,
    G_TYPE_CHAR,
    G_TYPE_UCHAR,
    G_TYPE_INT,
    G_TYPE_UINT,
Kristian Rietveld's avatar
Kristian Rietveld committed
76 77 78 79
    G_TYPE_LONG,
    G_TYPE_ULONG,
    G_TYPE_INT64,
    G_TYPE_UINT64,
80 81 82 83 84 85 86 87 88 89 90
    G_TYPE_ENUM,
    G_TYPE_FLAGS,
    G_TYPE_FLOAT,
    G_TYPE_DOUBLE,
    G_TYPE_STRING,
    G_TYPE_POINTER,
    G_TYPE_BOXED,
    G_TYPE_OBJECT,
    G_TYPE_INVALID
  };

91 92 93 94
  if (! G_TYPE_IS_VALUE_TYPE (type))
    return FALSE;


95 96 97 98 99 100 101 102 103
  while (type_list[i] != G_TYPE_INVALID)
    {
      if (g_type_is_a (type, type_list[i]))
	return TRUE;
      i++;
    }
  return FALSE;
}

104 105 106 107 108 109 110 111 112 113 114 115
static inline GType
get_fundamental_type (GType type)
{
  GType result;

  result = G_TYPE_FUNDAMENTAL (type);

  if (result == G_TYPE_INTERFACE)
    {
      if (g_type_is_a (type, G_TYPE_OBJECT))
	result = G_TYPE_OBJECT;
    }
116

117 118
  return result;
}
119
void
120 121 122
_gtk_tree_data_list_node_to_value (GtkTreeDataList *list,
				   GType            type,
				   GValue          *value)
123 124 125
{
  g_value_init (value, type);

126
  switch (get_fundamental_type (type))
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    {
    case G_TYPE_BOOLEAN:
      g_value_set_boolean (value, (gboolean) list->data.v_int);
      break;
    case G_TYPE_CHAR:
      g_value_set_char (value, (gchar) list->data.v_char);
      break;
    case G_TYPE_UCHAR:
      g_value_set_uchar (value, (guchar) list->data.v_uchar);
      break;
    case G_TYPE_INT:
      g_value_set_int (value, (gint) list->data.v_int);
      break;
    case G_TYPE_UINT:
      g_value_set_uint (value, (guint) list->data.v_uint);
      break;
Daniel Elstner's avatar
Daniel Elstner committed
143 144 145 146 147 148 149 150 151 152 153 154
    case G_TYPE_LONG:
      g_value_set_long (value, list->data.v_long);
      break;
    case G_TYPE_ULONG:
      g_value_set_ulong (value, list->data.v_ulong);
      break;
    case G_TYPE_INT64:
      g_value_set_int64 (value, list->data.v_int64);
      break;
    case G_TYPE_UINT64:
      g_value_set_uint64 (value, list->data.v_uint64);
      break;
155 156 157 158
    case G_TYPE_ENUM:
      g_value_set_enum (value, list->data.v_int);
      break;
    case G_TYPE_FLAGS:
Daniel Elstner's avatar
Daniel Elstner committed
159
      g_value_set_flags (value, list->data.v_uint);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
      break;
    case G_TYPE_FLOAT:
      g_value_set_float (value, (gfloat) list->data.v_float);
      break;
    case G_TYPE_DOUBLE:
      g_value_set_double (value, (gdouble) list->data.v_double);
      break;
    case G_TYPE_STRING:
      g_value_set_string (value, (gchar *) list->data.v_pointer);
      break;
    case G_TYPE_POINTER:
      g_value_set_pointer (value, (gpointer) list->data.v_pointer);
      break;
    case G_TYPE_BOXED:
      g_value_set_boxed (value, (gpointer) list->data.v_pointer);
      break;
    case G_TYPE_OBJECT:
      g_value_set_object (value, (GObject *) list->data.v_pointer);
      break;
    default:
      g_warning ("%s: Unsupported type (%s) retrieved.", G_STRLOC, g_type_name (value->g_type));
      break;
    }
183 184 185
}

void
186 187
_gtk_tree_data_list_value_to_node (GtkTreeDataList *list,
				   GValue          *value)
188
{
189
  switch (get_fundamental_type (G_VALUE_TYPE (value)))
190 191 192 193 194 195 196 197 198 199 200 201 202
    {
    case G_TYPE_BOOLEAN:
      list->data.v_int = g_value_get_boolean (value);
      break;
    case G_TYPE_CHAR:
      list->data.v_char = g_value_get_char (value);
      break;
    case G_TYPE_UCHAR:
      list->data.v_uchar = g_value_get_uchar (value);
      break;
    case G_TYPE_INT:
      list->data.v_int = g_value_get_int (value);
      break;
Daniel Elstner's avatar
Daniel Elstner committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    case G_TYPE_UINT:
      list->data.v_uint = g_value_get_uint (value);
      break;
    case G_TYPE_LONG:
      list->data.v_long = g_value_get_long (value);
      break;
    case G_TYPE_ULONG:
      list->data.v_ulong = g_value_get_ulong (value);
      break;
    case G_TYPE_INT64:
      list->data.v_int64 = g_value_get_int64 (value);
      break;
    case G_TYPE_UINT64:
      list->data.v_uint64 = g_value_get_uint64 (value);
      break;
218 219 220 221
    case G_TYPE_ENUM:
      list->data.v_int = g_value_get_enum (value);
      break;
    case G_TYPE_FLAGS:
Daniel Elstner's avatar
Daniel Elstner committed
222
      list->data.v_uint = g_value_get_flags (value);
223 224 225 226 227 228 229 230 231 232 233
      break;
    case G_TYPE_POINTER:
      list->data.v_pointer = g_value_get_pointer (value);
      break;
    case G_TYPE_FLOAT:
      list->data.v_float = g_value_get_float (value);
      break;
    case G_TYPE_DOUBLE:
      list->data.v_double = g_value_get_double (value);
      break;
    case G_TYPE_STRING:
234
      g_free (list->data.v_pointer);
235 236 237
      list->data.v_pointer = g_value_dup_string (value);
      break;
    case G_TYPE_OBJECT:
238 239
      if (list->data.v_pointer)
	g_object_unref (list->data.v_pointer);
240 241 242
      list->data.v_pointer = g_value_dup_object (value);
      break;
    case G_TYPE_BOXED:
243 244
      if (list->data.v_pointer)
	g_boxed_free (G_VALUE_TYPE (value), list->data.v_pointer);
245 246 247 248 249 250
      list->data.v_pointer = g_value_dup_boxed (value);
      break;
    default:
      g_warning ("%s: Unsupported type (%s) stored.", G_STRLOC, g_type_name (G_VALUE_TYPE (value)));
      break;
    }
251 252
}

253
GtkTreeDataList *
254 255 256 257
_gtk_tree_data_list_node_copy (GtkTreeDataList *list,
                               GType            type)
{
  GtkTreeDataList *new_list;
258 259

  g_return_val_if_fail (list != NULL, NULL);
260 261
  
  new_list = _gtk_tree_data_list_alloc ();
262
  new_list->next = NULL;
263

264
  switch (get_fundamental_type (type))
265
    {
266
    case G_TYPE_BOOLEAN:
Daniel Elstner's avatar
Daniel Elstner committed
267 268 269 270 271 272 273 274 275 276
    case G_TYPE_CHAR:
    case G_TYPE_UCHAR:
    case G_TYPE_INT:
    case G_TYPE_UINT:
    case G_TYPE_LONG:
    case G_TYPE_ULONG:
    case G_TYPE_INT64:
    case G_TYPE_UINT64:
    case G_TYPE_ENUM:
    case G_TYPE_FLAGS:
277 278 279 280 281 282 283 284 285
    case G_TYPE_POINTER:
    case G_TYPE_FLOAT:
    case G_TYPE_DOUBLE:
      new_list->data = list->data;
      break;
    case G_TYPE_STRING:
      new_list->data.v_pointer = g_strdup (list->data.v_pointer);
      break;
    case G_TYPE_OBJECT:
286
    case G_TYPE_INTERFACE:
287 288
      new_list->data.v_pointer = list->data.v_pointer;
      if (new_list->data.v_pointer)
Manish Singh's avatar
Manish Singh committed
289
	g_object_ref (new_list->data.v_pointer);
290 291
      break;
    case G_TYPE_BOXED:
292 293
      if (list->data.v_pointer)
	new_list->data.v_pointer = g_boxed_copy (type, list->data.v_pointer);
294
      else
295
	new_list->data.v_pointer = NULL;
296 297 298 299
      break;
    default:
      g_warning ("Unsupported node type (%s) copied.", g_type_name (type));
      break;
300
    }
301

302 303
  return new_list;
}
304

305
gint
306 307 308 309
_gtk_tree_data_list_compare_func (GtkTreeModel *model,
				  GtkTreeIter  *a,
				  GtkTreeIter  *b,
				  gpointer      user_data)
310 311 312 313 314 315
{
  gint column = GPOINTER_TO_INT (user_data);
  GType type = gtk_tree_model_get_column_type (model, column);
  GValue a_value = {0, };
  GValue b_value = {0, };
  gint retval;
316
  const gchar *stra, *strb;
317

318 319 320
  gtk_tree_model_get_value (model, a, column, &a_value);
  gtk_tree_model_get_value (model, b, column, &b_value);

321
  switch (get_fundamental_type (type))
322 323
    {
    case G_TYPE_BOOLEAN:
324
      if (g_value_get_boolean (&a_value) < g_value_get_boolean (&b_value))
325
	retval = -1;
326
      else if (g_value_get_boolean (&a_value) == g_value_get_boolean (&b_value))
327 328 329
	retval = 0;
      else
	retval = 1;
330 331
      break;
    case G_TYPE_CHAR:
332 333 334 335 336 337
      if (g_value_get_char (&a_value) < g_value_get_char (&b_value))
	retval = -1;
      else if (g_value_get_char (&a_value) == g_value_get_char (&b_value))
	retval = 0;
      else
	retval = 1;
338 339
      break;
    case G_TYPE_UCHAR:
340 341 342 343 344 345
      if (g_value_get_uchar (&a_value) < g_value_get_uchar (&b_value))
	retval = -1;
      else if (g_value_get_uchar (&a_value) == g_value_get_uchar (&b_value))
	retval = 0;
      else
	retval = 1;
346 347
      break;
    case G_TYPE_INT:
348 349 350 351 352 353
      if (g_value_get_int (&a_value) < g_value_get_int (&b_value))
	retval = -1;
      else if (g_value_get_int (&a_value) == g_value_get_int (&b_value))
	retval = 0;
      else
	retval = 1;
354 355
      break;
    case G_TYPE_UINT:
356 357 358 359 360 361
      if (g_value_get_uint (&a_value) < g_value_get_uint (&b_value))
	retval = -1;
      else if (g_value_get_uint (&a_value) == g_value_get_uint (&b_value))
	retval = 0;
      else
	retval = 1;
362
      break;
Daniel Elstner's avatar
Daniel Elstner committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    case G_TYPE_LONG:
      if (g_value_get_long (&a_value) < g_value_get_long (&b_value))
	retval = -1;
      else if (g_value_get_long (&a_value) == g_value_get_long (&b_value))
	retval = 0;
      else
	retval = 1;
      break;
    case G_TYPE_ULONG:
      if (g_value_get_ulong (&a_value) < g_value_get_ulong (&b_value))
	retval = -1;
      else if (g_value_get_ulong (&a_value) == g_value_get_ulong (&b_value))
	retval = 0;
      else
	retval = 1;
      break;
    case G_TYPE_INT64:
      if (g_value_get_int64 (&a_value) < g_value_get_int64 (&b_value))
	retval = -1;
      else if (g_value_get_int64 (&a_value) == g_value_get_int64 (&b_value))
	retval = 0;
      else
	retval = 1;
      break;
    case G_TYPE_UINT64:
      if (g_value_get_uint64 (&a_value) < g_value_get_uint64 (&b_value))
	retval = -1;
      else if (g_value_get_uint64 (&a_value) == g_value_get_uint64 (&b_value))
	retval = 0;
      else
	retval = 1;
      break;
395 396
    case G_TYPE_ENUM:
      /* this is somewhat bogus. */
Daniel Elstner's avatar
Daniel Elstner committed
397
      if (g_value_get_enum (&a_value) < g_value_get_enum (&b_value))
398
	retval = -1;
Daniel Elstner's avatar
Daniel Elstner committed
399
      else if (g_value_get_enum (&a_value) == g_value_get_enum (&b_value))
400 401 402
	retval = 0;
      else
	retval = 1;
403 404
      break;
    case G_TYPE_FLAGS:
405
      /* this is even more bogus. */
Daniel Elstner's avatar
Daniel Elstner committed
406
      if (g_value_get_flags (&a_value) < g_value_get_flags (&b_value))
407
	retval = -1;
Daniel Elstner's avatar
Daniel Elstner committed
408
      else if (g_value_get_flags (&a_value) == g_value_get_flags (&b_value))
409 410 411
	retval = 0;
      else
	retval = 1;
412 413
      break;
    case G_TYPE_FLOAT:
414 415 416 417 418 419
      if (g_value_get_float (&a_value) < g_value_get_float (&b_value))
	retval = -1;
      else if (g_value_get_float (&a_value) == g_value_get_float (&b_value))
	retval = 0;
      else
	retval = 1;
420 421
      break;
    case G_TYPE_DOUBLE:
422 423 424 425 426 427
      if (g_value_get_double (&a_value) < g_value_get_double (&b_value))
	retval = -1;
      else if (g_value_get_double (&a_value) == g_value_get_double (&b_value))
	retval = 0;
      else
	retval = 1;
428 429
      break;
    case G_TYPE_STRING:
430 431
      stra = g_value_get_string (&a_value);
      strb = g_value_get_string (&b_value);
432 433 434
      if (stra == NULL) stra = "";
      if (strb == NULL) strb = "";
      retval = g_utf8_collate (stra, strb);
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
      break;
    case G_TYPE_POINTER:
    case G_TYPE_BOXED:
    case G_TYPE_OBJECT:
    default:
      g_warning ("Attempting to sort on invalid type %s\n", g_type_name (type));
      retval = FALSE;
      break;
    }

  g_value_unset (&a_value);
  g_value_unset (&b_value);

  return retval;
}


GList *
_gtk_tree_data_list_header_new (gint   n_columns,
				GType *types)
{
  GList *retval = NULL;

  gint i;

  for (i = 0; i < n_columns; i ++)
    {
      GtkTreeDataSortHeader *header;

464
      header = g_slice_new (GtkTreeDataSortHeader);
465 466 467

      retval = g_list_prepend (retval, header);
      header->sort_column_id = i;
468
      header->func = _gtk_tree_data_list_compare_func;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
      header->destroy = NULL;
      header->data = GINT_TO_POINTER (i);
    }
  return g_list_reverse (retval);
}

void
_gtk_tree_data_list_header_free (GList *list)
{
  GList *tmp;

  for (tmp = list; tmp; tmp = tmp->next)
    {
      GtkTreeDataSortHeader *header = (GtkTreeDataSortHeader *) tmp->data;

      if (header->destroy)
485
	{
486
	  GDestroyNotify d = header->destroy;
487 488 489 490

	  header->destroy = NULL;
	  d (header->data);
	}
491

492
      g_slice_free (GtkTreeDataSortHeader, header);
493 494 495
    }
  g_list_free (list);
}
496 497

GtkTreeDataSortHeader *
498 499
_gtk_tree_data_list_get_header (GList   *header_list,
				gint     sort_column_id)
500 501 502 503 504 505 506 507 508 509 510
{
  GtkTreeDataSortHeader *header = NULL;

  for (; header_list; header_list = header_list->next)
    {
      header = (GtkTreeDataSortHeader*) header_list->data;
      if (header->sort_column_id == sort_column_id)
	return header;
    }
  return NULL;
}
511 512 513 514 515 516 517


GList *
_gtk_tree_data_list_set_header (GList                  *header_list,
				gint                    sort_column_id,
				GtkTreeIterCompareFunc  func,
				gpointer                data,
518
				GDestroyNotify          destroy)
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
{
  GList *list = header_list;
  GtkTreeDataSortHeader *header = NULL;

  for (; list; list = list->next)
    {
      header = (GtkTreeDataSortHeader*) list->data;
      if (header->sort_column_id == sort_column_id)
	break;
      header = NULL;

      if (list->next == NULL)
	break;
    }
  
  if (header == NULL)
    {
      header = g_slice_new0 (GtkTreeDataSortHeader);
      header->sort_column_id = sort_column_id;
      if (list)
539
	list = g_list_append (list, header);
540 541 542 543 544 545
      else
	header_list = g_list_append (header_list, header);
    }

  if (header->destroy)
    {
546
      GDestroyNotify d = header->destroy;
547 548 549 550 551 552 553 554 555 556 557
      
      header->destroy = NULL;
      d (header->data);
    }
  
  header->func = func;
  header->data = data;
  header->destroy = destroy;

  return header_list;
}