1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpstringaction.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 <gtk/gtk.h>
24 
25 #include "widgets-types.h"
26 
27 #include "gimpaction.h"
28 #include "gimpaction-history.h"
29 #include "gimpstringaction.h"
30 
31 
32 enum
33 {
34   PROP_0,
35   PROP_VALUE
36 };
37 
38 
39 static void   gimp_string_action_finalize     (GObject      *object);
40 static void   gimp_string_action_set_property (GObject      *object,
41                                                guint         prop_id,
42                                                const GValue *value,
43                                                GParamSpec   *pspec);
44 static void   gimp_string_action_get_property (GObject      *object,
45                                                guint         prop_id,
46                                                GValue       *value,
47                                                GParamSpec   *pspec);
48 
49 static void   gimp_string_action_activate     (GtkAction    *action);
50 
51 
G_DEFINE_TYPE(GimpStringAction,gimp_string_action,GIMP_TYPE_ACTION_IMPL)52 G_DEFINE_TYPE (GimpStringAction, gimp_string_action, GIMP_TYPE_ACTION_IMPL)
53 
54 #define parent_class gimp_string_action_parent_class
55 
56 
57 static void
58 gimp_string_action_class_init (GimpStringActionClass *klass)
59 {
60   GObjectClass   *object_class = G_OBJECT_CLASS (klass);
61   GtkActionClass *action_class = GTK_ACTION_CLASS (klass);
62 
63   object_class->finalize     = gimp_string_action_finalize;
64   object_class->set_property = gimp_string_action_set_property;
65   object_class->get_property = gimp_string_action_get_property;
66 
67   action_class->activate = gimp_string_action_activate;
68 
69   g_object_class_install_property (object_class, PROP_VALUE,
70                                    g_param_spec_string ("value",
71                                                         NULL, NULL,
72                                                         NULL,
73                                                         GIMP_PARAM_READWRITE));
74 }
75 
76 static void
gimp_string_action_init(GimpStringAction * action)77 gimp_string_action_init (GimpStringAction *action)
78 {
79 }
80 
81 static void
gimp_string_action_finalize(GObject * object)82 gimp_string_action_finalize (GObject *object)
83 {
84   GimpStringAction *action = GIMP_STRING_ACTION (object);
85 
86   g_clear_pointer (&action->value, g_free);
87 
88   G_OBJECT_CLASS (parent_class)->finalize (object);
89 }
90 
91 static void
gimp_string_action_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)92 gimp_string_action_get_property (GObject    *object,
93                                  guint       prop_id,
94                                  GValue     *value,
95                                  GParamSpec *pspec)
96 {
97   GimpStringAction *action = GIMP_STRING_ACTION (object);
98 
99   switch (prop_id)
100     {
101     case PROP_VALUE:
102       g_value_set_string (value, action->value);
103       break;
104     default:
105       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106       break;
107     }
108 }
109 
110 static void
gimp_string_action_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)111 gimp_string_action_set_property (GObject      *object,
112                                  guint         prop_id,
113                                  const GValue *value,
114                                  GParamSpec   *pspec)
115 {
116   GimpStringAction *action = GIMP_STRING_ACTION (object);
117 
118   switch (prop_id)
119     {
120     case PROP_VALUE:
121       g_free (action->value);
122       action->value = g_value_dup_string (value);
123       break;
124     default:
125       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
126       break;
127     }
128 }
129 
130 GimpStringAction *
gimp_string_action_new(const gchar * name,const gchar * label,const gchar * tooltip,const gchar * icon_name,const gchar * help_id,const gchar * value)131 gimp_string_action_new (const gchar *name,
132                         const gchar *label,
133                         const gchar *tooltip,
134                         const gchar *icon_name,
135                         const gchar *help_id,
136                         const gchar *value)
137 {
138   GimpStringAction *action;
139 
140   action = g_object_new (GIMP_TYPE_STRING_ACTION,
141                          "name",      name,
142                          "label",     label,
143                          "tooltip",   tooltip,
144                          "icon-name", icon_name,
145                          "value",     value,
146                          NULL);
147 
148   gimp_action_set_help_id (GIMP_ACTION (action), help_id);
149 
150   return action;
151 }
152 
153 static void
gimp_string_action_activate(GtkAction * action)154 gimp_string_action_activate (GtkAction *action)
155 {
156   GimpStringAction *string_action = GIMP_STRING_ACTION (action);
157 
158   gimp_action_emit_activate (GIMP_ACTION (action),
159                              g_variant_new_string (string_action->value));
160 
161   gimp_action_history_action_activated (GIMP_ACTION (action));
162 }
163