gimplist.c 4.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include "gimpsignal.h"
#include "gimplistP.h"

/* code mostly ripped from nether's gimpset class  */

enum
{
  ADD,
  REMOVE,
  LAST_SIGNAL
};

static guint gimp_list_signals [LAST_SIGNAL];

static GimpObjectClass* parent_class;

static void gimp_list_add_func(GimpList *list, void *);
static void gimp_list_remove_func(GimpList *list, void *);

static void
gimp_list_destroy (GtkObject* ob)
{
	GimpList* list=GIMP_LIST(ob);
41
42
43
	while(list->list) /* ought to put a sanity check in here... */
	{
	  gimp_list_remove(list, list->list->data);
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	}
	g_slist_free(list->list);
	GTK_OBJECT_CLASS(parent_class)->destroy (ob);
}

static void
gimp_list_init (GimpList* list)
{
	list->list=NULL;
	list->type=GTK_TYPE_OBJECT;
}

static void
gimp_list_class_init (GimpListClass* klass)
{
	GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
	GtkType type = object_class->type;

	parent_class=gtk_type_parent_class(type);
	
	object_class->destroy = gimp_list_destroy;
	
	gimp_list_signals[ADD]=
67
	  gimp_signal_new ("add", GTK_RUN_FIRST, type, 0,
68
69
			   gimp_sigtype_pointer);
	gimp_list_signals[REMOVE]=
70
	  gimp_signal_new ("remove", GTK_RUN_FIRST, type, 0,
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
			   gimp_sigtype_pointer);
	gtk_object_class_add_signals (object_class,
				      gimp_list_signals,
				      LAST_SIGNAL);

	klass->add    = gimp_list_add_func;
	klass->remove = gimp_list_remove_func;
}

GtkType gimp_list_get_type (void)
{
	static GtkType type;
	GIMP_TYPE_INIT(type,
		       GimpList,
		       GimpListClass,
		       gimp_list_init,
		       gimp_list_class_init,
		       GIMP_TYPE_OBJECT);
	return type;
}


GimpList*
gimp_list_new (GtkType type, gboolean weak){
	GimpList* list=gtk_type_new (gimp_list_get_type ());
	list->type=type;
	list->weak=weak;
	return list;
}

static void
gimp_list_destroy_cb (GtkObject* ob, gpointer data){
	GimpList* list=GIMP_LIST(data);
	gimp_list_remove(list, ob);
}

static void
gimp_list_add_func(GimpList* list, gpointer val)
{
  list->list=g_slist_prepend(list->list, val);
}

static void
gimp_list_remove_func(GimpList* list, gpointer val)
{
  list->list=g_slist_remove(list->list, val);
}

gboolean
gimp_list_add (GimpList* list, gpointer val)
{
  g_return_val_if_fail(list, FALSE);
  g_return_val_if_fail(GTK_CHECK_TYPE(val, list->type), FALSE);
	
  if(g_slist_find(list->list, val))
    return FALSE;
	
  if(list->weak)
    gtk_signal_connect(GTK_OBJECT(val), "destroy",
		       GTK_SIGNAL_FUNC(gimp_list_destroy_cb), list);
  else
    gtk_object_ref(GTK_OBJECT(val));
	
134
135
  GIMP_LIST_CLASS(GTK_OBJECT(list)->klass)->add(list, val);

136
137
138
139
140
141
142
143
144
145
  gtk_signal_emit (GTK_OBJECT(list), gimp_list_signals[ADD], val);
  return TRUE;
}

gboolean
gimp_list_remove (GimpList* list, gpointer val)
{
	g_return_val_if_fail(list, FALSE);

	if(!g_slist_find(list->list, val))
146
147
148
149
150
	{
	  printf("can't find val\n");
	  return FALSE;
	}
	GIMP_LIST_CLASS(GTK_OBJECT(list)->klass)->remove(list, val);
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

	gtk_signal_emit (GTK_OBJECT(list), gimp_list_signals[REMOVE], val);

	if(list->weak)
		gtk_signal_disconnect_by_func
			(GTK_OBJECT(val),
			 GTK_SIGNAL_FUNC(gimp_list_destroy_cb),
			 list);
	else
		gtk_object_unref(GTK_OBJECT(val));

	return TRUE;
}

gboolean
gimp_list_have (GimpList* list, gpointer val) {
	return g_slist_find(list->list, val)?TRUE:FALSE;
}

void
gimp_list_foreach(GimpList* list, GFunc func, gpointer user_data)
{
	g_slist_foreach(list->list, func, user_data);
}

GtkType
gimp_list_type (GimpList* list){
	return list->type;
}