1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpaction.c
5  * Copyright (C) 2004-2019 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 <gegl.h>
24 #include <gtk/gtk.h>
25 
26 #include "libgimpwidgets/gimpwidgets.h"
27 
28 #include "widgets-types.h"
29 
30 #include "core/gimpmarshal.h"
31 
32 #include "gimpaction.h"
33 
34 
35 enum
36 {
37   ACTIVATE,
38   CHANGE_STATE,
39   LAST_SIGNAL
40 };
41 
42 
43 static void   gimp_action_set_proxy_tooltip (GimpAction       *action,
44                                              GtkWidget        *proxy);
45 static void   gimp_action_tooltip_notify    (GimpAction       *action,
46                                              const GParamSpec *pspec,
47                                              gpointer          data);
48 
49 
G_DEFINE_INTERFACE(GimpAction,gimp_action,GTK_TYPE_ACTION)50 G_DEFINE_INTERFACE (GimpAction, gimp_action, GTK_TYPE_ACTION)
51 
52 static guint action_signals[LAST_SIGNAL];
53 
54 
55 static void
56 gimp_action_default_init (GimpActionInterface *iface)
57 {
58   action_signals[ACTIVATE] =
59     g_signal_new ("gimp-activate",
60                   G_TYPE_FROM_INTERFACE (iface),
61                   G_SIGNAL_RUN_FIRST,
62                   G_STRUCT_OFFSET (GimpActionInterface, activate),
63                   NULL, NULL,
64                   gimp_marshal_VOID__VARIANT,
65                   G_TYPE_NONE, 1,
66                   G_TYPE_VARIANT);
67 
68   action_signals[CHANGE_STATE] =
69     g_signal_new ("gimp-change-state",
70                   G_TYPE_FROM_INTERFACE (iface),
71                   G_SIGNAL_RUN_FIRST,
72                   G_STRUCT_OFFSET (GimpActionInterface, change_state),
73                   NULL, NULL,
74                   gimp_marshal_VOID__VARIANT,
75                   G_TYPE_NONE, 1,
76                   G_TYPE_VARIANT);
77 }
78 
79 void
gimp_action_init(GimpAction * action)80 gimp_action_init (GimpAction *action)
81 {
82   g_return_if_fail (GIMP_IS_ACTION (action));
83 
84   g_signal_connect (action, "notify::tooltip",
85                     G_CALLBACK (gimp_action_tooltip_notify),
86                     NULL);
87 }
88 
89 
90 /*  public functions  */
91 
92 void
gimp_action_emit_activate(GimpAction * action,GVariant * value)93 gimp_action_emit_activate (GimpAction *action,
94                            GVariant   *value)
95 {
96   g_return_if_fail (GIMP_IS_ACTION (action));
97 
98   if (value)
99     g_variant_ref_sink (value);
100 
101   g_signal_emit (action, action_signals[ACTIVATE], 0, value);
102 
103   if (value)
104     g_variant_unref (value);
105 }
106 
107 void
gimp_action_emit_change_state(GimpAction * action,GVariant * value)108 gimp_action_emit_change_state (GimpAction *action,
109                                GVariant   *value)
110 {
111   g_return_if_fail (GIMP_IS_ACTION (action));
112 
113   if (value)
114     g_variant_ref_sink (value);
115 
116   g_signal_emit (action, action_signals[CHANGE_STATE], 0, value);
117 
118   if (value)
119     g_variant_unref (value);
120 }
121 
122 void
gimp_action_set_proxy(GimpAction * action,GtkWidget * proxy)123 gimp_action_set_proxy (GimpAction *action,
124                        GtkWidget  *proxy)
125 {
126   g_return_if_fail (GIMP_IS_ACTION (action));
127   g_return_if_fail (GTK_IS_WIDGET (proxy));
128 
129   gimp_action_set_proxy_tooltip (action, proxy);
130 }
131 
132 const gchar *
gimp_action_get_name(GimpAction * action)133 gimp_action_get_name (GimpAction *action)
134 {
135   return gtk_action_get_name ((GtkAction *) action);
136 }
137 
138 void
gimp_action_set_label(GimpAction * action,const gchar * label)139 gimp_action_set_label (GimpAction  *action,
140                        const gchar *label)
141 {
142   gtk_action_set_label ((GtkAction *) action, label);
143 }
144 
145 const gchar *
gimp_action_get_label(GimpAction * action)146 gimp_action_get_label (GimpAction *action)
147 {
148   return gtk_action_get_label ((GtkAction *) action);
149 }
150 
151 void
gimp_action_set_tooltip(GimpAction * action,const gchar * tooltip)152 gimp_action_set_tooltip (GimpAction  *action,
153                          const gchar *tooltip)
154 {
155   gtk_action_set_tooltip ((GtkAction *) action, tooltip);
156 }
157 
158 const gchar *
gimp_action_get_tooltip(GimpAction * action)159 gimp_action_get_tooltip (GimpAction *action)
160 {
161   return gtk_action_get_tooltip ((GtkAction *) action);
162 }
163 
164 void
gimp_action_set_icon_name(GimpAction * action,const gchar * icon_name)165 gimp_action_set_icon_name (GimpAction  *action,
166                            const gchar *icon_name)
167 {
168   gtk_action_set_icon_name ((GtkAction *) action, icon_name);
169 }
170 
171 const gchar *
gimp_action_get_icon_name(GimpAction * action)172 gimp_action_get_icon_name (GimpAction *action)
173 {
174   return gtk_action_get_icon_name ((GtkAction *) action);
175 }
176 
177 void
gimp_action_set_gicon(GimpAction * action,GIcon * icon)178 gimp_action_set_gicon (GimpAction *action,
179                        GIcon      *icon)
180 {
181   gtk_action_set_gicon ((GtkAction *) action, icon);
182 }
183 
184 GIcon *
gimp_action_get_gicon(GimpAction * action)185 gimp_action_get_gicon (GimpAction *action)
186 {
187   return gtk_action_get_gicon ((GtkAction *) action);
188 }
189 
190 void
gimp_action_set_help_id(GimpAction * action,const gchar * help_id)191 gimp_action_set_help_id (GimpAction  *action,
192                          const gchar *help_id)
193 {
194   g_return_if_fail (GIMP_IS_ACTION (action));
195 
196   g_object_set_qdata_full (G_OBJECT (action), GIMP_HELP_ID,
197                            g_strdup (help_id),
198                            (GDestroyNotify) g_free);
199 }
200 
201 const gchar *
gimp_action_get_help_id(GimpAction * action)202 gimp_action_get_help_id (GimpAction *action)
203 {
204   g_return_val_if_fail (GIMP_IS_ACTION (action), NULL);
205 
206   return g_object_get_qdata (G_OBJECT (action), GIMP_HELP_ID);
207 }
208 
209 void
gimp_action_set_visible(GimpAction * action,gboolean visible)210 gimp_action_set_visible (GimpAction *action,
211                          gboolean    visible)
212 {
213   gtk_action_set_visible ((GtkAction *) action, visible);
214 }
215 
216 gboolean
gimp_action_get_visible(GimpAction * action)217 gimp_action_get_visible (GimpAction *action)
218 {
219   return gtk_action_get_visible ((GtkAction *) action);
220 }
221 
222 gboolean
gimp_action_is_visible(GimpAction * action)223 gimp_action_is_visible (GimpAction *action)
224 {
225   return gtk_action_is_visible ((GtkAction *) action);
226 }
227 
228 void
gimp_action_set_sensitive(GimpAction * action,gboolean sensitive)229 gimp_action_set_sensitive (GimpAction *action,
230                            gboolean    sensitive)
231 {
232   gtk_action_set_sensitive ((GtkAction *) action, sensitive);
233 }
234 
235 gboolean
gimp_action_get_sensitive(GimpAction * action)236 gimp_action_get_sensitive (GimpAction *action)
237 {
238   return gtk_action_get_sensitive ((GtkAction *) action);
239 }
240 
241 gboolean
gimp_action_is_sensitive(GimpAction * action)242 gimp_action_is_sensitive (GimpAction *action)
243 {
244   return gtk_action_is_sensitive ((GtkAction *) action);
245 }
246 
247 GClosure *
gimp_action_get_accel_closure(GimpAction * action)248 gimp_action_get_accel_closure (GimpAction *action)
249 {
250   return gtk_action_get_accel_closure ((GtkAction *) action);
251 }
252 
253 void
gimp_action_set_accel_path(GimpAction * action,const gchar * accel_path)254 gimp_action_set_accel_path (GimpAction  *action,
255                             const gchar *accel_path)
256 {
257   gtk_action_set_accel_path ((GtkAction *) action, accel_path);
258 }
259 
260 const gchar *
gimp_action_get_accel_path(GimpAction * action)261 gimp_action_get_accel_path (GimpAction *action)
262 {
263   return gtk_action_get_accel_path ((GtkAction *) action);
264 }
265 
266 void
gimp_action_set_accel_group(GimpAction * action,GtkAccelGroup * accel_group)267 gimp_action_set_accel_group (GimpAction  *action,
268                              GtkAccelGroup *accel_group)
269 {
270   gtk_action_set_accel_group ((GtkAction *) action, accel_group);
271 }
272 
273 void
gimp_action_connect_accelerator(GimpAction * action)274 gimp_action_connect_accelerator (GimpAction  *action)
275 {
276   gtk_action_connect_accelerator ((GtkAction *) action);
277 }
278 
279 GSList *
gimp_action_get_proxies(GimpAction * action)280 gimp_action_get_proxies (GimpAction *action)
281 {
282   return gtk_action_get_proxies ((GtkAction *) action);
283 }
284 
285 void
gimp_action_activate(GimpAction * action)286 gimp_action_activate (GimpAction *action)
287 {
288   gtk_action_activate ((GtkAction *) action);
289 }
290 
291 gint
gimp_action_name_compare(GimpAction * action1,GimpAction * action2)292 gimp_action_name_compare (GimpAction  *action1,
293                           GimpAction  *action2)
294 {
295   return strcmp (gimp_action_get_name (action1),
296                  gimp_action_get_name (action2));
297 }
298 
299 gboolean
gimp_action_is_gui_blacklisted(const gchar * action_name)300 gimp_action_is_gui_blacklisted (const gchar *action_name)
301 {
302   static const gchar *suffixes[] =
303     {
304       "-menu",
305       "-popup"
306     };
307 
308   static const gchar *prefixes[] =
309     {
310       "<",
311       "tools-color-average-radius-",
312       "tools-paintbrush-size-",
313       "tools-paintbrush-aspect-ratio-",
314       "tools-paintbrush-angle-",
315       "tools-paintbrush-spacing-",
316       "tools-paintbrush-hardness-",
317       "tools-paintbrush-force-",
318       "tools-ink-blob-size-",
319       "tools-ink-blob-aspect-",
320       "tools-ink-blob-angle-",
321       "tools-mypaint-brush-radius-",
322       "tools-mypaint-brush-hardness-",
323       "tools-foreground-select-brush-size-",
324       "tools-transform-preview-opacity-",
325       "tools-warp-effect-size-",
326       "tools-warp-effect-hardness-"
327     };
328 
329   static const gchar *actions[] =
330     {
331       "tools-brightness-contrast",
332       "tools-curves",
333       "tools-levels",
334       "tools-offset",
335       "tools-threshold"
336     };
337 
338   gint i;
339 
340   if (! (action_name && *action_name))
341     return TRUE;
342 
343   for (i = 0; i < G_N_ELEMENTS (suffixes); i++)
344     {
345       if (g_str_has_suffix (action_name, suffixes[i]))
346         return TRUE;
347     }
348 
349   for (i = 0; i < G_N_ELEMENTS (prefixes); i++)
350     {
351       if (g_str_has_prefix (action_name, prefixes[i]))
352         return TRUE;
353     }
354 
355   for (i = 0; i < G_N_ELEMENTS (actions); i++)
356     {
357       if (! strcmp (action_name, actions[i]))
358         return TRUE;
359     }
360 
361   return FALSE;
362 }
363 
364 
365 /*  private functions  */
366 
367 static void
gimp_action_set_proxy_tooltip(GimpAction * action,GtkWidget * proxy)368 gimp_action_set_proxy_tooltip (GimpAction *action,
369                                GtkWidget  *proxy)
370 {
371   const gchar *tooltip = gimp_action_get_tooltip (action);
372 
373   if (tooltip)
374     gimp_help_set_help_data (proxy, tooltip,
375                              g_object_get_qdata (G_OBJECT (proxy),
376                                                  GIMP_HELP_ID));
377 }
378 
379 static void
gimp_action_tooltip_notify(GimpAction * action,const GParamSpec * pspec,gpointer data)380 gimp_action_tooltip_notify (GimpAction       *action,
381                             const GParamSpec *pspec,
382                             gpointer          data)
383 {
384   GSList *list;
385 
386   for (list = gimp_action_get_proxies (action);
387        list;
388        list = g_slist_next (list))
389     {
390       gimp_action_set_proxy_tooltip (action, list->data);
391     }
392 }
393