1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpactionfactory.c
5  * Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include <string.h>
24 
25 #include <gegl.h>
26 #include <gtk/gtk.h>
27 
28 #include "libgimpwidgets/gimpwidgets.h"
29 
30 #include "widgets-types.h"
31 
32 #include "core/gimp.h"
33 
34 #include "gimpactionfactory.h"
35 #include "gimpactiongroup.h"
36 
37 
38 static void   gimp_action_factory_finalize (GObject *object);
39 
40 
G_DEFINE_TYPE(GimpActionFactory,gimp_action_factory,GIMP_TYPE_OBJECT)41 G_DEFINE_TYPE (GimpActionFactory, gimp_action_factory, GIMP_TYPE_OBJECT)
42 
43 #define parent_class gimp_action_factory_parent_class
44 
45 
46 static void
47 gimp_action_factory_class_init (GimpActionFactoryClass *klass)
48 {
49   GObjectClass *object_class = G_OBJECT_CLASS (klass);
50 
51   object_class->finalize = gimp_action_factory_finalize;
52 }
53 
54 static void
gimp_action_factory_init(GimpActionFactory * factory)55 gimp_action_factory_init (GimpActionFactory *factory)
56 {
57   factory->gimp              = NULL;
58   factory->registered_groups = NULL;
59 }
60 
61 static void
gimp_action_factory_finalize(GObject * object)62 gimp_action_factory_finalize (GObject *object)
63 {
64   GimpActionFactory *factory = GIMP_ACTION_FACTORY (object);
65   GList             *list;
66 
67   for (list = factory->registered_groups; list; list = g_list_next (list))
68     {
69       GimpActionFactoryEntry *entry = list->data;
70 
71       g_free (entry->identifier);
72       g_free (entry->label);
73       g_free (entry->icon_name);
74 
75       g_slice_free (GimpActionFactoryEntry, entry);
76     }
77 
78   g_list_free (factory->registered_groups);
79   factory->registered_groups = NULL;
80 
81   G_OBJECT_CLASS (parent_class)->finalize (object);
82 }
83 
84 GimpActionFactory *
gimp_action_factory_new(Gimp * gimp)85 gimp_action_factory_new (Gimp *gimp)
86 {
87   GimpActionFactory *factory;
88 
89   g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
90 
91   factory = g_object_new (GIMP_TYPE_ACTION_FACTORY, NULL);
92 
93   factory->gimp = gimp;
94 
95   return factory;
96 }
97 
98 void
gimp_action_factory_group_register(GimpActionFactory * factory,const gchar * identifier,const gchar * label,const gchar * icon_name,GimpActionGroupSetupFunc setup_func,GimpActionGroupUpdateFunc update_func)99 gimp_action_factory_group_register (GimpActionFactory         *factory,
100                                     const gchar               *identifier,
101                                     const gchar               *label,
102                                     const gchar               *icon_name,
103                                     GimpActionGroupSetupFunc   setup_func,
104                                     GimpActionGroupUpdateFunc  update_func)
105 {
106   GimpActionFactoryEntry *entry;
107 
108   g_return_if_fail (GIMP_IS_ACTION_FACTORY (factory));
109   g_return_if_fail (identifier != NULL);
110   g_return_if_fail (label != NULL);
111   g_return_if_fail (setup_func != NULL);
112   g_return_if_fail (update_func != NULL);
113 
114   entry = g_slice_new0 (GimpActionFactoryEntry);
115 
116   entry->identifier  = g_strdup (identifier);
117   entry->label       = g_strdup (label);
118   entry->icon_name   = g_strdup (icon_name);
119   entry->setup_func  = setup_func;
120   entry->update_func = update_func;
121 
122   factory->registered_groups = g_list_prepend (factory->registered_groups,
123                                                entry);
124 }
125 
126 GimpActionGroup *
gimp_action_factory_group_new(GimpActionFactory * factory,const gchar * identifier,gpointer user_data)127 gimp_action_factory_group_new (GimpActionFactory *factory,
128                                const gchar       *identifier,
129                                gpointer           user_data)
130 {
131   GList *list;
132 
133   g_return_val_if_fail (GIMP_IS_ACTION_FACTORY (factory), NULL);
134   g_return_val_if_fail (identifier != NULL, NULL);
135 
136   for (list = factory->registered_groups; list; list = g_list_next (list))
137     {
138       GimpActionFactoryEntry *entry = list->data;
139 
140       if (! strcmp (entry->identifier, identifier))
141         {
142           GimpActionGroup *group;
143 
144           group = gimp_action_group_new (factory->gimp,
145                                          entry->identifier,
146                                          entry->label,
147                                          entry->icon_name,
148                                          user_data,
149                                          entry->update_func);
150 
151           if (entry->setup_func)
152             entry->setup_func (group);
153 
154           return group;
155         }
156     }
157 
158   g_warning ("%s: no entry registered for \"%s\"",
159              G_STRFUNC, identifier);
160 
161   return NULL;
162 }
163