From e4d64122bbf1de79f20abf39775b0f7ddb1286a8 Mon Sep 17 00:00:00 2001 From: Sven Neumann Date: Thu, 17 Jan 2008 10:51:31 +0000 Subject: [PATCH] app/core/Makefile.am app/core/core-types.h define a simple interface for 2008-01-17 Sven Neumann * app/core/Makefile.am * app/core/core-types.h * app/core/gimptagged.[ch]: define a simple interface for tagged objects. * app/core/gimpdata.[ch]: implement the GimpTagged interface. svn path=/trunk/; revision=24633 --- ChangeLog | 9 +++ app/core/Makefile.am | 2 + app/core/core-types.h | 8 +++ app/core/gimpdata.c | 91 +++++++++++++++++++++++-- app/core/gimpdata.h | 2 + app/core/gimptagged.c | 154 ++++++++++++++++++++++++++++++++++++++++++ app/core/gimptagged.h | 62 +++++++++++++++++ 7 files changed, 322 insertions(+), 6 deletions(-) create mode 100644 app/core/gimptagged.c create mode 100644 app/core/gimptagged.h diff --git a/ChangeLog b/ChangeLog index d13d302996..f6760bf9c4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2008-01-17 Sven Neumann + + * app/core/Makefile.am + * app/core/core-types.h + * app/core/gimptagged.[ch]: define a simple interface for tagged + objects. + + * app/core/gimpdata.[ch]: implement the GimpTagged interface. + 2008-01-17 Sven Neumann * app/core/gimpviewable.h: formatting. diff --git a/app/core/Makefile.am b/app/core/Makefile.am index f42f8f31cb..dda34eab6e 100644 --- a/app/core/Makefile.am +++ b/app/core/Makefile.am @@ -290,6 +290,8 @@ libappcore_a_sources = \ gimpstrokeoptions.h \ gimpsubprogress.c \ gimpsubprogress.h \ + gimptagged.c \ + gimptagged.h \ gimptemplate.c \ gimptemplate.h \ gimptoolinfo.c \ diff --git a/app/core/core-types.h b/app/core/core-types.h index bac87cc9c0..ddd32c1c97 100644 --- a/app/core/core-types.h +++ b/app/core/core-types.h @@ -150,6 +150,7 @@ typedef struct _GimpStrokeDesc GimpStrokeDesc; typedef struct _GimpPickable GimpPickable; /* dummy typedef */ typedef struct _GimpProgress GimpProgress; /* dummy typedef */ +typedef struct _GimpTagged GimpTagged; /* dummy typedef */ /* non-object types */ @@ -163,6 +164,13 @@ typedef struct _GimpPaletteEntry GimpPaletteEntry; typedef struct _GimpScanConvert GimpScanConvert; +/* tags */ + +typedef GQuark GimpTag; +#define gimp_tag_new(name) g_quark_from_string (name) +#define gimp_tag_get_name(tag) g_quark_to_string (name) + + /* functions */ typedef void (* GimpInitStatusFunc) (const gchar *text1, diff --git a/app/core/gimpdata.c b/app/core/gimpdata.c index 21a99801ae..2fdd1b6c89 100644 --- a/app/core/gimpdata.c +++ b/app/core/gimpdata.c @@ -42,6 +42,7 @@ #include "gimp-utils.h" #include "gimpdata.h" #include "gimpmarshal.h" +#include "gimptagged.h" #include "gimp-intl.h" @@ -62,14 +63,15 @@ enum }; -static void gimp_data_class_init (GimpDataClass *klass); +static void gimp_data_class_init (GimpDataClass *klass); +static void gimp_data_tagged_iface_init (GimpTaggedInterface *iface); + +static GObject * gimp_data_constructor (GType type, + guint n_params, + GObjectConstructParam *params); + static void gimp_data_init (GimpData *data, GimpDataClass *data_class); - -static GObject * gimp_data_constructor (GType type, - guint n_params, - GObjectConstructParam *params); - static void gimp_data_finalize (GObject *object); static void gimp_data_set_property (GObject *object, guint property_id, @@ -85,6 +87,12 @@ static gint64 gimp_data_get_memsize (GimpObject *object, static void gimp_data_real_dirty (GimpData *data); +static gboolean gimp_data_add_tag (GimpTagged *tagged, + GimpTag tag); +static gboolean gimp_data_remove_tag (GimpTagged *tagged, + GimpTag tag); +static GList * gimp_data_get_tags (GimpTagged *tagged); + static guint data_signals[LAST_SIGNAL] = { 0 }; @@ -111,9 +119,18 @@ gimp_data_get_type (void) (GInstanceInitFunc) gimp_data_init, }; + const GInterfaceInfo tagged_info = + { + (GInterfaceInitFunc) gimp_data_tagged_iface_init, + NULL, /* interface_finalize */ + NULL /* interface_data */ + }; + data_type = g_type_register_static (GIMP_TYPE_VIEWABLE, "GimpData", &data_info, 0); + + g_type_add_interface_static (data_type, GIMP_TYPE_TAGGED, &tagged_info); } return data_type; @@ -170,6 +187,14 @@ gimp_data_class_init (GimpDataClass *klass) G_PARAM_CONSTRUCT_ONLY)); } +static void +gimp_data_tagged_iface_init (GimpTaggedInterface *iface) +{ + iface->add_tag = gimp_data_add_tag; + iface->remove_tag = gimp_data_remove_tag; + iface->get_tags = gimp_data_get_tags; +} + static void gimp_data_init (GimpData *data, GimpDataClass *data_class) @@ -182,6 +207,7 @@ gimp_data_init (GimpData *data, data->internal = FALSE; data->freeze_count = 0; data->mtime = 0; + data->tags = NULL; /* look at the passed class pointer, not at GIMP_DATA_GET_CLASS(data) * here, because the latter is always GimpDataClass itself @@ -204,6 +230,12 @@ gimp_data_finalize (GObject *object) data->filename = NULL; } + if (data->tags) + { + g_list_free (data->tags); + data->tags = NULL; + } + G_OBJECT_CLASS (parent_class)->finalize (object); } @@ -314,6 +346,53 @@ gimp_data_real_dirty (GimpData *data) gimp_object_name_changed (GIMP_OBJECT (data)); } +static gboolean +gimp_data_add_tag (GimpTagged *tagged, + GimpTag tag) +{ + GimpData *data = GIMP_DATA (tagged); + GList *list; + + for (list = data->tags; list; list = list->next) + { + GimpTag this = GPOINTER_TO_UINT (list->data); + + if (this == tag) + return FALSE; + } + + data->tags = g_list_prepend (data->tags, GUINT_TO_POINTER (tag)); + + return TRUE; +} + +static gboolean +gimp_data_remove_tag (GimpTagged *tagged, + GimpTag tag) +{ + GimpData *data = GIMP_DATA (tagged); + GList *list; + + for (list = data->tags; list; list = list->next) + { + GimpTag this = GPOINTER_TO_UINT (list->data); + + if (this == tag) + { + data->tags = g_list_delete_link (data->tags, list); + return TRUE; + } + } + + return FALSE; +} + +static GList * +gimp_data_get_tags (GimpTagged *tagged) +{ + return GIMP_DATA (tagged)->tags; +} + /** * gimp_data_save: * @data: object whose contents are to be saved. diff --git a/app/core/gimpdata.h b/app/core/gimpdata.h index 05f8b431b4..7e6ae2664f 100644 --- a/app/core/gimpdata.h +++ b/app/core/gimpdata.h @@ -58,6 +58,8 @@ struct _GimpData guint internal : 1; gint freeze_count; time_t mtime; + + GList *tags; }; struct _GimpDataClass diff --git a/app/core/gimptagged.c b/app/core/gimptagged.c new file mode 100644 index 0000000000..dfd9e685c5 --- /dev/null +++ b/app/core/gimptagged.c @@ -0,0 +1,154 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimptagged.c + * Copyright (C) 2008 Sven Neumann + * + * 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 "config.h" + +#include + +#include "core-types.h" + +#include "gimpmarshal.h" +#include "gimptagged.h" + + +enum +{ + TAG_ADDED, + TAG_REMOVED, + LAST_SIGNAL +}; + +static void gimp_tagged_base_init (gpointer klass); + +static guint gimp_tagged_signals[LAST_SIGNAL] = { 0, }; + + +GType +gimp_tagged_interface_get_type (void) +{ + static GType tagged_iface_type = 0; + + if (! tagged_iface_type) + { + const GTypeInfo tagged_iface_info = + { + sizeof (GimpTaggedInterface), + gimp_tagged_base_init, + (GBaseFinalizeFunc) NULL, + }; + + tagged_iface_type = g_type_register_static (G_TYPE_INTERFACE, + "GimpTaggedInterface", + &tagged_iface_info, + 0); + } + + return tagged_iface_type; +} + +static void +gimp_tagged_base_init (gpointer klass) +{ + static gboolean initialized = FALSE; + + if (! initialized) + { + gimp_tagged_signals[TAG_ADDED] = + g_signal_new ("tag-added", + GIMP_TYPE_TAGGED, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GimpTaggedInterface, tag_added), + NULL, NULL, + gimp_marshal_VOID__INT, + G_TYPE_NONE, 1, + G_TYPE_INT); + gimp_tagged_signals[TAG_REMOVED] = + g_signal_new ("tag-removed", + GIMP_TYPE_TAGGED, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (GimpTaggedInterface, tag_removed), + NULL, NULL, + gimp_marshal_VOID__INT, + G_TYPE_NONE, 1, + G_TYPE_INT); + + initialized = TRUE; + } +} + +/** + * gimp_tagged_add_tag: + * @tagged: an object that implements the %GimpTagged interface + * @tag: a %GimpTag + * + * Adds @tag to the @tagged object. The GimpTagged::tag-added signal + * is emitted if and only if the @tag was not already assigned to this + * object. + **/ +void +gimp_tagged_add_tag (GimpTagged *tagged, + const GimpTag tag) +{ + g_return_if_fail (GIMP_IS_TAGGED (tagged)); + + if (GIMP_TAGGED_GET_INTERFACE (tagged)->add_tag (tagged, tag)) + { + g_signal_emit (tagged, gimp_tagged_signals[TAG_ADDED], 0, tag); + } +} + +/** + * gimp_tagged_remove_tag: + * @tagged: an object that implements the %GimpTagged interface + * @tag: a %GimpTag + * + * Removes @tag from the @tagged object. The GimpTagged::tag-removed + * signal is emitted if and only if the @tag was actually assigned to + * this object. + **/ +void +gimp_tagged_remove_tag (GimpTagged *tagged, + GimpTag tag) +{ + g_return_if_fail (GIMP_IS_TAGGED (tagged)); + + if (GIMP_TAGGED_GET_INTERFACE (tagged)->remove_tag (tagged, tag)) + { + g_signal_emit (tagged, gimp_tagged_signals[TAG_REMOVED], 0, tag); + } +} + +/** + * gimp_tagged_get_tags: + * @tagged: an object that implements the %GimpTagged interface + * + * Returns the list of tags assigned to this object. The returned %GList + * is owned by the @tagged object and must not be modified or destroyed. + * + * Return value: a list of tags + **/ +GList * +gimp_tagged_get_get_tags (GimpTagged *tagged) +{ + g_return_val_if_fail (GIMP_IS_TAGGED (tagged), NULL); + + return GIMP_TAGGED_GET_INTERFACE (tagged)->get_tags (tagged); +} diff --git a/app/core/gimptagged.h b/app/core/gimptagged.h new file mode 100644 index 0000000000..0654e674c1 --- /dev/null +++ b/app/core/gimptagged.h @@ -0,0 +1,62 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis + * + * gimptagged.h + * Copyright (C) 2008 Sven Neumann + * + * 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. + */ + +#ifndef __GIMP_TAGGED_H__ +#define __GIMP_TAGGED_H__ + + +#define GIMP_TYPE_TAGGED (gimp_tagged_interface_get_type ()) +#define GIMP_IS_TAGGED(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_TAGGED)) +#define GIMP_TAGGED(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_TAGGED, GimpTagged)) +#define GIMP_TAGGED_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GIMP_TYPE_TAGGED, GimpTaggedInterface)) + + +typedef struct _GimpTaggedInterface GimpTaggedInterface; + +struct _GimpTaggedInterface +{ + GTypeInterface base_iface; + + /* signals */ + void (* tag_added) (GimpTagged *tagged, + GimpTag tag); + void (* tag_removed) (GimpTagged *tagged, + GimpTag tag); + + /* virtual functions */ + gboolean (* add_tag) (GimpTagged *tagged, + GimpTag tag); + gboolean (* remove_tag) (GimpTagged *tagged, + GimpTag tag); + GList * (* get_tags) (GimpTagged *tagged); +}; + + +GType gimp_tagged_interface_get_type (void) G_GNUC_CONST; + +void gimp_tagged_add_tag (GimpTagged *tagged, + GimpTag tag); +void gimp_tagged_remove_tag (GimpTagged *tagged, + GimpTag tag); +GList * gimp_tagged_get_get_tags (GimpTagged *tagged); + + +#endif /* __GIMP_TAGGED_H__ */