gimplist.c 6.67 KB
Newer Older
1
/* The GIMP -- an image manipulation program
2
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
3
 *
Michael Natterer's avatar
Michael Natterer committed
4 5 6
 * gimplist.c
 * Copyright (C) 2001 Michael Natterer <mitch@gimp.org>
 *
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */
21

Sven Neumann's avatar
Sven Neumann committed
22 23
#include "config.h"

24 25
#include <string.h> /* strcmp */

26
#include <glib-object.h>
Sven Neumann's avatar
Sven Neumann committed
27

Michael Natterer's avatar
Michael Natterer committed
28
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
29

30
#include "gimplist.h"
31

Sven Neumann's avatar
Sven Neumann committed
32

33 34
static void         gimp_list_class_init         (GimpListClass       *klass);
static void         gimp_list_init               (GimpList            *list);
35

36
static void         gimp_list_dispose            (GObject             *object);
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static void         gimp_list_add                (GimpContainer       *container,
						  GimpObject          *object);
static void         gimp_list_remove             (GimpContainer       *container,
						  GimpObject          *object);
static void         gimp_list_reorder            (GimpContainer       *container,
						  GimpObject          *object,
						  gint                 new_index);
static gboolean     gimp_list_have               (const GimpContainer *container,
						  const GimpObject    *object);
static void         gimp_list_foreach            (const GimpContainer *container,
						  GFunc                func,
						  gpointer             user_data);
static GimpObject * gimp_list_get_child_by_name  (const GimpContainer *container,
						  const gchar         *name);
static GimpObject * gimp_list_get_child_by_index (const GimpContainer *container,
						  gint                 index);
static gint         gimp_list_get_child_index    (const GimpContainer *container,
						  const GimpObject    *object);
56

57

58
static GimpContainerClass *parent_class = NULL;
59 60


61
GType
62
gimp_list_get_type (void)
63
{
64
  static GType list_type = 0;
65

66 67
  if (! list_type)
    {
68
      static const GTypeInfo list_info =
69
      {
70 71 72 73 74 75
        sizeof (GimpListClass),
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) gimp_list_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data     */
76
	sizeof (GimpList),
77 78
	0,              /* n_preallocs    */
	(GInstanceInitFunc) gimp_list_init,
79 80
      };

81 82 83
      list_type = g_type_register_static (GIMP_TYPE_CONTAINER,
					  "GimpList", 
					  &list_info, 0);
84
    }
85

86
  return list_type;
87
}
88

89 90
static void
gimp_list_class_init (GimpListClass *klass)
91
{
92
  GObjectClass       *object_class;
93
  GimpContainerClass *container_class;
94

95 96
  object_class    = G_OBJECT_CLASS (klass);
  container_class = GIMP_CONTAINER_CLASS (klass);
97

98
  parent_class = g_type_class_peek_parent (klass);
99

100
  object_class->dispose               = gimp_list_dispose;
101

102 103
  container_class->add                = gimp_list_add;
  container_class->remove             = gimp_list_remove;
104
  container_class->reorder            = gimp_list_reorder;
105 106 107 108 109
  container_class->have               = gimp_list_have;
  container_class->foreach            = gimp_list_foreach;
  container_class->get_child_by_name  = gimp_list_get_child_by_name;
  container_class->get_child_by_index = gimp_list_get_child_by_index;
  container_class->get_child_index    = gimp_list_get_child_index;
110 111 112
}

static void
113 114 115 116 117 118
gimp_list_init (GimpList *list)
{
  list->list = NULL;
}

static void
119
gimp_list_dispose (GObject *object)
120
{
121
  GimpList *list;
122

123
  list = GIMP_LIST (object);
124

125 126 127 128 129 130
  while (list->list)
    {
      gimp_container_remove (GIMP_CONTAINER (list),
			     GIMP_OBJECT (list->list->data));
    }

131
  G_OBJECT_CLASS (parent_class)->dispose (object);
132 133 134
}

static void
135 136
gimp_list_add (GimpContainer *container,
	       GimpObject    *object)
137
{
138 139 140 141 142
  GimpList *list;

  list = GIMP_LIST (container);

  list->list = g_list_prepend (list->list, object);
143 144 145
}

static void
146 147
gimp_list_remove (GimpContainer *container,
		  GimpObject    *object)
148
{
149 150 151 152 153
  GimpList *list;

  list = GIMP_LIST (container);

  list->list = g_list_remove (list->list, object);
154 155
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
static void
gimp_list_reorder (GimpContainer *container,
		   GimpObject    *object,
		   gint           new_index)
{
  GimpList *list;

  list = GIMP_LIST (container);

  list->list = g_list_remove (list->list, object);

  if (new_index == -1 || new_index == container->num_children - 1)
    list->list = g_list_append (list->list, object);
  else
    list->list = g_list_insert (list->list, object, new_index);
}

173
static gboolean
174 175
gimp_list_have (const GimpContainer *container,
		const GimpObject    *object)
176
{
177
  GimpList *list;
178

179
  list = GIMP_LIST (container);
180

181
  return g_list_find (list->list, object) ? TRUE : FALSE;
182 183
}

184
static void
185 186 187
gimp_list_foreach (const GimpContainer *container,
		   GFunc                func,
		   gpointer             user_data)
188
{
189
  GimpList *list;
190

191
  list = GIMP_LIST (container);
192

193 194
  g_list_foreach (list->list, func, user_data);
}
195

196
GimpContainer *
Michael Natterer's avatar
Michael Natterer committed
197
gimp_list_new (GType                children_type,
198 199 200 201
	       GimpContainerPolicy  policy)
{
  GimpList *list;

202
  g_return_val_if_fail (g_type_is_a (children_type, GIMP_TYPE_OBJECT), NULL);
203 204
  g_return_val_if_fail (policy == GIMP_CONTAINER_POLICY_STRONG ||
                        policy == GIMP_CONTAINER_POLICY_WEAK, NULL);
205

206 207 208 209
  list = g_object_new (GIMP_TYPE_LIST,
                       "children_type", children_type,
                       "policy",        policy,
                       NULL);
210

211
  return GIMP_CONTAINER (list);
212 213
}

214
static GimpObject *
215 216
gimp_list_get_child_by_name (const GimpContainer *container,
			     const gchar         *name)
217
{
218
  GimpList   *list;
219 220 221
  GimpObject *object;
  GList      *glist;

222
  list = GIMP_LIST (container);
223 224 225 226 227 228 229 230 231 232

  for (glist = list->list; glist; glist = g_list_next (glist))
    {
      object = (GimpObject *) glist->data;

      if (! strcmp (object->name, name))
	return object;
    }

  return NULL;
233 234
}

235
static GimpObject *
236 237
gimp_list_get_child_by_index (const GimpContainer *container,
			      gint                 index)
238
{
239 240
  GimpList *list;
  GList    *glist;
241

242
  list = GIMP_LIST (container);
243 244 245 246 247 248 249

  glist = g_list_nth (list->list, index);

  if (glist)
    return (GimpObject *) glist->data;

  return NULL;
250 251
}

252
static gint
253 254
gimp_list_get_child_index (const GimpContainer *container,
			   const GimpObject    *object)
255
{
256 257 258
  GimpList *list;

  list = GIMP_LIST (container);
259 260

  return g_list_index (list->list, (gpointer) object);
261
}