1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <gegl.h>
21 #include <gtk/gtk.h>
22 
23 #include "libgimpbase/gimpbase.h"
24 #include "libgimpwidgets/gimpwidgets.h"
25 
26 #include "actions-types.h"
27 
28 #include "core/gimp.h"
29 #include "core/gimp-filter-history.h"
30 #include "core/gimpcontainer.h"
31 #include "core/gimpcontext.h"
32 #include "core/gimpimage.h"
33 #include "core/gimpitem.h"
34 #include "core/gimpparamspecs.h"
35 #include "core/gimpprogress.h"
36 
37 #include "pdb/gimpprocedure.h"
38 
39 #include "plug-in/gimppluginmanager.h"
40 #include "plug-in/gimppluginmanager-data.h"
41 
42 #include "widgets/gimpbufferview.h"
43 #include "widgets/gimpcontainerview.h"
44 #include "widgets/gimpdatafactoryview.h"
45 #include "widgets/gimphelp-ids.h"
46 #include "widgets/gimpimageeditor.h"
47 #include "widgets/gimpitemtreeview.h"
48 #include "widgets/gimpmessagebox.h"
49 #include "widgets/gimpmessagedialog.h"
50 
51 #include "dialogs/dialogs.h"
52 
53 #include "actions.h"
54 #include "plug-in-commands.h"
55 #include "procedure-commands.h"
56 
57 #include "gimp-intl.h"
58 
59 
60 /*  local function prototypes  */
61 
62 static void   plug_in_reset_all_response (GtkWidget *dialog,
63                                           gint       response_id,
64                                           Gimp      *gimp);
65 
66 
67 /*  public functions  */
68 
69 void
plug_in_run_cmd_callback(GimpAction * action,GVariant * value,gpointer data)70 plug_in_run_cmd_callback (GimpAction *action,
71                           GVariant   *value,
72                           gpointer    data)
73 {
74   Gimp           *gimp;
75   GimpValueArray *args    = NULL;
76   GimpDisplay    *display = NULL;
77   GimpProcedure  *procedure;
78   gsize           hack;
79   return_if_no_gimp (gimp, data);
80 
81   hack = g_variant_get_uint64 (value);
82 
83   procedure = GSIZE_TO_POINTER (hack);
84 
85   switch (procedure->proc_type)
86     {
87     case GIMP_EXTENSION:
88       args = procedure_commands_get_run_mode_arg (procedure);
89       break;
90 
91     case GIMP_PLUGIN:
92     case GIMP_TEMPORARY:
93       if (GIMP_IS_DATA_FACTORY_VIEW (data) ||
94           GIMP_IS_BUFFER_VIEW (data))
95         {
96           GimpContainerEditor *editor = GIMP_CONTAINER_EDITOR (data);
97           GimpContainer       *container;
98           GimpContext         *context;
99           GimpObject          *object;
100 
101           container = gimp_container_view_get_container (editor->view);
102           context   = gimp_container_view_get_context (editor->view);
103 
104           object = gimp_context_get_by_type (context,
105                                              gimp_container_get_children_type (container));
106 
107           args = procedure_commands_get_data_args (procedure, object);
108         }
109       else if (GIMP_IS_IMAGE_EDITOR (data))
110         {
111           GimpImageEditor *editor = GIMP_IMAGE_EDITOR (data);
112           GimpImage       *image;
113 
114           image = gimp_image_editor_get_image (editor);
115 
116           args = procedure_commands_get_image_args (procedure, image);
117         }
118       else if (GIMP_IS_ITEM_TREE_VIEW (data))
119         {
120           GimpItemTreeView *view = GIMP_ITEM_TREE_VIEW (data);
121           GimpImage        *image;
122           GimpItem         *item;
123 
124           image = gimp_item_tree_view_get_image (view);
125 
126           if (image)
127             item = GIMP_ITEM_TREE_VIEW_GET_CLASS (view)->get_active_item (image);
128           else
129             item = NULL;
130 
131           args = procedure_commands_get_item_args (procedure, image, item);
132         }
133       else
134         {
135           display = action_data_get_display (data);
136 
137           args = procedure_commands_get_display_args (procedure, display, NULL);
138         }
139       break;
140 
141     case GIMP_INTERNAL:
142       g_warning ("Unhandled procedure type.");
143       break;
144     }
145 
146   if (args)
147     {
148       if (procedure_commands_run_procedure_async (procedure, gimp,
149                                                   GIMP_PROGRESS (display),
150                                                   GIMP_RUN_INTERACTIVE, args,
151                                                   display))
152         {
153           /* remember only image plug-ins */
154           if (procedure->num_args >= 2 &&
155               GIMP_IS_PARAM_SPEC_IMAGE_ID (procedure->args[1]))
156             {
157               gimp_filter_history_add (gimp, procedure);
158             }
159         }
160 
161       gimp_value_array_unref (args);
162     }
163 }
164 
165 void
plug_in_reset_all_cmd_callback(GimpAction * action,GVariant * value,gpointer data)166 plug_in_reset_all_cmd_callback (GimpAction *action,
167                                 GVariant   *value,
168                                 gpointer    data)
169 {
170   Gimp      *gimp;
171   GtkWidget *dialog;
172   return_if_no_gimp (gimp, data);
173 
174 #define RESET_FILTERS_DIALOG_KEY "gimp-reset-all-filters-dialog"
175 
176   dialog = dialogs_get_dialog (G_OBJECT (gimp), RESET_FILTERS_DIALOG_KEY);
177 
178   if (! dialog)
179     {
180       dialog = gimp_message_dialog_new (_("Reset all Filters"),
181                                         GIMP_ICON_DIALOG_QUESTION,
182                                         NULL, 0,
183                                         gimp_standard_help_func, NULL,
184 
185                                         _("_Cancel"), GTK_RESPONSE_CANCEL,
186                                         _("_Reset"),  GTK_RESPONSE_OK,
187 
188                                         NULL);
189 
190       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
191                                                GTK_RESPONSE_OK,
192                                                GTK_RESPONSE_CANCEL,
193                                                -1);
194 
195       g_signal_connect (dialog, "response",
196                         G_CALLBACK (plug_in_reset_all_response),
197                         gimp);
198 
199       gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
200                                          _("Do you really want to reset all "
201                                            "filters to default values?"));
202 
203       dialogs_attach_dialog (G_OBJECT (gimp), RESET_FILTERS_DIALOG_KEY, dialog);
204     }
205 
206   gtk_window_present (GTK_WINDOW (dialog));
207 }
208 
209 
210 /*  private functions  */
211 
212 static void
plug_in_reset_all_response(GtkWidget * dialog,gint response_id,Gimp * gimp)213 plug_in_reset_all_response (GtkWidget *dialog,
214                             gint       response_id,
215                             Gimp      *gimp)
216 {
217   gtk_widget_destroy (dialog);
218 
219   if (response_id == GTK_RESPONSE_OK)
220     gimp_plug_in_manager_data_free (gimp->plug_in_manager);
221 }
222