home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / gimplist.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-20  |  5.0 KB  |  232 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <stdio.h>
  22.  
  23. #include <gtk/gtk.h>
  24.  
  25. #include "apptypes.h"
  26.  
  27. #include "gimpobject.h"
  28. #include "gimpsignal.h"
  29. #include "gimplist.h"
  30.  
  31.  
  32. /*  code mostly ripped from nether's gimpset class  */
  33.  
  34. enum
  35. {
  36.   ADD,
  37.   REMOVE,
  38.   LAST_SIGNAL
  39. };
  40.  
  41. static guint gimp_list_signals[LAST_SIGNAL] = { 0 };
  42.  
  43. static GimpObjectClass *parent_class = NULL;
  44.  
  45.  
  46. static void gimp_list_add_func    (GimpList *list,
  47.                    gpointer  object);
  48. static void gimp_list_remove_func (GimpList *list,
  49.                    gpointer  object);
  50.  
  51.  
  52. static void
  53. gimp_list_destroy (GtkObject *object)
  54. {
  55.   GimpList *list = GIMP_LIST (object);
  56.  
  57.   while (list->list) /* ought to put a sanity check in here... */
  58.     {
  59.       gimp_list_remove (list, list->list->data);
  60.     }
  61.   g_slist_free (list->list);
  62.  
  63.   if (GTK_OBJECT_CLASS (parent_class)->destroy)
  64.     GTK_OBJECT_CLASS (parent_class)->destroy (object);
  65. }
  66.  
  67. static void
  68. gimp_list_init (GimpList *list)
  69. {
  70.   list->list = NULL;
  71.   list->type = GTK_TYPE_OBJECT;
  72. }
  73.  
  74. static void
  75. gimp_list_class_init (GimpListClass *klass)
  76. {
  77.   GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
  78.   GtkType type = object_class->type;
  79.  
  80.   parent_class = gtk_type_parent_class (type);
  81.  
  82.   object_class->destroy = gimp_list_destroy;
  83.  
  84.   gimp_list_signals[ADD]=
  85.     gimp_signal_new ("add", GTK_RUN_FIRST, type, 0,
  86.              gimp_sigtype_pointer);
  87.   gimp_list_signals[REMOVE]=
  88.     gimp_signal_new ("remove", GTK_RUN_FIRST, type, 0,
  89.              gimp_sigtype_pointer);
  90.  
  91.   gtk_object_class_add_signals (object_class,
  92.                 gimp_list_signals,
  93.                 LAST_SIGNAL);
  94.  
  95.   klass->add    = gimp_list_add_func;
  96.   klass->remove = gimp_list_remove_func;
  97. }
  98.  
  99. GtkType
  100. gimp_list_get_type (void)
  101. {
  102.   static GtkType type;
  103.  
  104.   GIMP_TYPE_INIT (type,
  105.           GimpList,
  106.           GimpListClass,
  107.           gimp_list_init,
  108.           gimp_list_class_init,
  109.           GIMP_TYPE_OBJECT);
  110.  
  111.   return type;
  112. }
  113.  
  114. GimpList *
  115. gimp_list_new (GtkType  type,
  116.            gboolean weak)
  117. {
  118.   GimpList *list;
  119.  
  120.   list = gtk_type_new (gimp_list_get_type ());
  121.  
  122.   list->type = type;
  123.   list->weak = weak;
  124.  
  125.   return list;
  126. }
  127.  
  128. static void
  129. gimp_list_destroy_cb (GtkObject *object,
  130.               gpointer   data)
  131. {
  132.   GimpList *list;
  133.  
  134.   list = GIMP_LIST (data);
  135.  
  136.   gimp_list_remove (list, object);
  137. }
  138.  
  139. static void
  140. gimp_list_add_func (GimpList *list,
  141.             gpointer  object)
  142. {
  143.   list->list = g_slist_prepend (list->list, object);
  144. }
  145.  
  146. static void
  147. gimp_list_remove_func (GimpList *list,
  148.                gpointer  object)
  149. {
  150.   list->list = g_slist_remove (list->list, object);
  151. }
  152.  
  153. gboolean
  154. gimp_list_add (GimpList *list,
  155.            gpointer  object)
  156. {
  157.   g_return_val_if_fail (list, FALSE);
  158.   g_return_val_if_fail (GTK_CHECK_TYPE (object, list->type), FALSE);
  159.     
  160.   if (g_slist_find (list->list, object))
  161.     return FALSE;
  162.     
  163.   if (list->weak)
  164.     {
  165.       gtk_signal_connect (GTK_OBJECT (object), "destroy",
  166.               GTK_SIGNAL_FUNC (gimp_list_destroy_cb),
  167.               list);
  168.     }
  169.   else
  170.     {
  171.       gtk_object_ref (GTK_OBJECT (object));
  172.       gtk_object_sink (GTK_OBJECT (object));
  173.     }
  174.  
  175.   GIMP_LIST_CLASS (GTK_OBJECT (list)->klass)->add (list, object);
  176.  
  177.   gtk_signal_emit (GTK_OBJECT(list), gimp_list_signals[ADD], object);
  178.  
  179.   return TRUE;
  180. }
  181.  
  182. gboolean
  183. gimp_list_remove (GimpList *list,
  184.           gpointer  object)
  185. {
  186.   g_return_val_if_fail (list, FALSE);
  187.  
  188.   if (!g_slist_find (list->list, object))
  189.     {
  190.       g_warning ("gimp_list_remove: can't find val");
  191.       return FALSE;
  192.     }
  193.  
  194.   GIMP_LIST_CLASS (GTK_OBJECT (list)->klass)->remove (list, object);
  195.  
  196.   gtk_signal_emit (GTK_OBJECT (list), gimp_list_signals[REMOVE], object);
  197.  
  198.   if (list->weak)
  199.     {
  200.       gtk_signal_disconnect_by_func (GTK_OBJECT (object),
  201.                      GTK_SIGNAL_FUNC (gimp_list_destroy_cb),
  202.                      list);
  203.     }
  204.   else
  205.     {
  206.       gtk_object_unref (GTK_OBJECT (object));
  207.     }
  208.  
  209.   return TRUE;
  210. }
  211.  
  212. gboolean
  213. gimp_list_have (GimpList *list,
  214.         gpointer  object)
  215. {
  216.   return g_slist_find (list->list, object) ? TRUE : FALSE;
  217. }
  218.  
  219. void
  220. gimp_list_foreach (GimpList *list,
  221.            GFunc     func,
  222.            gpointer  user_data)
  223. {
  224.   g_slist_foreach (list->list, func, user_data);
  225. }
  226.  
  227. GtkType
  228. gimp_list_type (GimpList *list)
  229. {
  230.   return list->type;
  231. }
  232.