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 <string.h>
21 
22 #include <gegl.h>
23 #include <gtk/gtk.h>
24 
25 #include "libgimpconfig/gimpconfig.h"
26 #include "libgimpwidgets/gimpwidgets.h"
27 
28 #include "actions-types.h"
29 
30 #include "core/gimp.h"
31 #include "core/gimpcontainer.h"
32 #include "core/gimpdatafactory.h"
33 #include "core/gimptoolinfo.h"
34 #include "core/gimptooloptions.h"
35 #include "core/gimptoolpreset.h"
36 
37 #include "widgets/gimpdataeditor.h"
38 #include "widgets/gimpdialogfactory.h"
39 #include "widgets/gimpeditor.h"
40 #include "widgets/gimphelp-ids.h"
41 #include "widgets/gimpmessagebox.h"
42 #include "widgets/gimpmessagedialog.h"
43 #include "widgets/gimptooloptionseditor.h"
44 #include "widgets/gimpuimanager.h"
45 #include "widgets/gimpwidgets-utils.h"
46 #include "widgets/gimpwindowstrategy.h"
47 
48 #include "dialogs/data-delete-dialog.h"
49 
50 #include "tool-options-commands.h"
51 
52 #include "gimp-intl.h"
53 
54 
55 /*  local function prototypes  */
56 
57 static void   tool_options_show_preset_editor (Gimp           *gimp,
58                                                GimpEditor     *editor,
59                                                GimpToolPreset *preset);
60 
61 
62 /*  public functions  */
63 
64 void
tool_options_save_new_preset_cmd_callback(GimpAction * action,GVariant * value,gpointer user_data)65 tool_options_save_new_preset_cmd_callback (GimpAction *action,
66                                            GVariant   *value,
67                                            gpointer    user_data)
68 {
69   GimpEditor  *editor  = GIMP_EDITOR (user_data);
70   Gimp        *gimp    = gimp_editor_get_ui_manager (editor)->gimp;
71   GimpContext *context = gimp_get_user_context (gimp);
72   GimpData    *data;
73 
74   data = gimp_data_factory_data_new (context->gimp->tool_preset_factory,
75                                      context, _("Untitled"));
76 
77   tool_options_show_preset_editor (gimp, editor, GIMP_TOOL_PRESET (data));
78 }
79 
80 void
tool_options_save_preset_cmd_callback(GimpAction * action,GVariant * value,gpointer data)81 tool_options_save_preset_cmd_callback (GimpAction *action,
82                                        GVariant   *value,
83                                        gpointer    data)
84 {
85   GimpEditor     *editor    = GIMP_EDITOR (data);
86   Gimp           *gimp      = gimp_editor_get_ui_manager (editor)->gimp;
87   GimpContext    *context   = gimp_get_user_context (gimp);
88   GimpToolInfo   *tool_info = gimp_context_get_tool (context);
89   GimpToolPreset *preset;
90   gint            index;
91 
92   index = g_variant_get_int32 (value);
93 
94   preset = (GimpToolPreset *)
95     gimp_container_get_child_by_index (tool_info->presets, index);
96 
97   if (preset)
98     {
99       gimp_config_sync (G_OBJECT (tool_info->tool_options),
100                         G_OBJECT (preset->tool_options), 0);
101 
102       tool_options_show_preset_editor (gimp, editor, preset);
103     }
104 }
105 
106 void
tool_options_restore_preset_cmd_callback(GimpAction * action,GVariant * value,gpointer data)107 tool_options_restore_preset_cmd_callback (GimpAction *action,
108                                           GVariant   *value,
109                                           gpointer    data)
110 {
111   GimpEditor     *editor    = GIMP_EDITOR (data);
112   Gimp           *gimp      = gimp_editor_get_ui_manager (editor)->gimp;
113   GimpContext    *context   = gimp_get_user_context (gimp);
114   GimpToolInfo   *tool_info = gimp_context_get_tool (context);
115   GimpToolPreset *preset;
116   gint            index;
117 
118   index = g_variant_get_int32 (value);
119 
120   preset = (GimpToolPreset *)
121     gimp_container_get_child_by_index (tool_info->presets, index);
122 
123   if (preset)
124     {
125       if (gimp_context_get_tool_preset (context) != preset)
126         gimp_context_set_tool_preset (context, preset);
127       else
128         gimp_context_tool_preset_changed (context);
129     }
130 }
131 
132 void
tool_options_edit_preset_cmd_callback(GimpAction * action,GVariant * value,gpointer data)133 tool_options_edit_preset_cmd_callback (GimpAction *action,
134                                        GVariant   *value,
135                                        gpointer    data)
136 {
137   GimpEditor     *editor    = GIMP_EDITOR (data);
138   Gimp           *gimp      = gimp_editor_get_ui_manager (editor)->gimp;
139   GimpContext    *context   = gimp_get_user_context (gimp);
140   GimpToolInfo   *tool_info = gimp_context_get_tool (context);
141   GimpToolPreset *preset;
142   gint            index;
143 
144   index = g_variant_get_int32 (value);
145 
146   preset = (GimpToolPreset *)
147     gimp_container_get_child_by_index (tool_info->presets, index);
148 
149   if (preset)
150     {
151       tool_options_show_preset_editor (gimp, editor, preset);
152     }
153 }
154 
155 void
tool_options_delete_preset_cmd_callback(GimpAction * action,GVariant * value,gpointer data)156 tool_options_delete_preset_cmd_callback (GimpAction *action,
157                                          GVariant   *value,
158                                          gpointer    data)
159 {
160   GimpEditor     *editor    = GIMP_EDITOR (data);
161   GimpContext    *context   = gimp_get_user_context (gimp_editor_get_ui_manager (editor)->gimp);
162   GimpToolInfo   *tool_info = gimp_context_get_tool (context);
163   GimpToolPreset *preset;
164   gint            index;
165 
166   index = g_variant_get_int32 (value);
167 
168   preset = (GimpToolPreset *)
169     gimp_container_get_child_by_index (tool_info->presets, index);
170 
171   if (preset &&
172       gimp_data_is_deletable (GIMP_DATA (preset)))
173     {
174       GimpDataFactory *factory = context->gimp->tool_preset_factory;
175       GtkWidget       *dialog;
176 
177       dialog = data_delete_dialog_new (factory, GIMP_DATA (preset), NULL,
178                                        GTK_WIDGET (editor));
179       gtk_widget_show (dialog);
180     }
181 }
182 
183 void
tool_options_reset_cmd_callback(GimpAction * action,GVariant * value,gpointer data)184 tool_options_reset_cmd_callback (GimpAction *action,
185                                  GVariant   *value,
186                                  gpointer    data)
187 {
188   GimpEditor   *editor    = GIMP_EDITOR (data);
189   GimpContext  *context   = gimp_get_user_context (gimp_editor_get_ui_manager (editor)->gimp);
190   GimpToolInfo *tool_info = gimp_context_get_tool (context);
191 
192   gimp_config_reset (GIMP_CONFIG (tool_info->tool_options));
193 }
194 
195 void
tool_options_reset_all_cmd_callback(GimpAction * action,GVariant * value,gpointer data)196 tool_options_reset_all_cmd_callback (GimpAction *action,
197                                      GVariant   *value,
198                                      gpointer    data)
199 {
200   GimpEditor *editor = GIMP_EDITOR (data);
201   GtkWidget  *dialog;
202 
203   dialog = gimp_message_dialog_new (_("Reset All Tool Options"),
204                                     GIMP_ICON_DIALOG_QUESTION,
205                                     GTK_WIDGET (editor),
206                                     GTK_DIALOG_MODAL |
207                                     GTK_DIALOG_DESTROY_WITH_PARENT,
208                                     gimp_standard_help_func, NULL,
209 
210                                     _("_Cancel"), GTK_RESPONSE_CANCEL,
211                                     _("_Reset"),  GTK_RESPONSE_OK,
212 
213                                     NULL);
214 
215   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
216   gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
217                                            GTK_RESPONSE_OK,
218                                            GTK_RESPONSE_CANCEL,
219                                            -1);
220 
221   g_signal_connect_object (gtk_widget_get_toplevel (GTK_WIDGET (editor)),
222                            "unmap",
223                            G_CALLBACK (gtk_widget_destroy),
224                            dialog, G_CONNECT_SWAPPED);
225 
226   gimp_message_box_set_primary_text (GIMP_MESSAGE_DIALOG (dialog)->box,
227                                      _("Do you really want to reset all "
228                                        "tool options to default values?"));
229 
230   if (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK)
231     {
232       Gimp  *gimp = gimp_editor_get_ui_manager (editor)->gimp;
233       GList *list;
234 
235       for (list = gimp_get_tool_info_iter (gimp);
236            list;
237            list = g_list_next (list))
238         {
239           GimpToolInfo *tool_info = list->data;
240 
241           gimp_config_reset (GIMP_CONFIG (tool_info->tool_options));
242         }
243     }
244 
245   gtk_widget_destroy (dialog);
246 }
247 
248 
249 /*  private functions  */
250 
251 static void
tool_options_show_preset_editor(Gimp * gimp,GimpEditor * editor,GimpToolPreset * preset)252 tool_options_show_preset_editor (Gimp           *gimp,
253                                  GimpEditor     *editor,
254                                  GimpToolPreset *preset)
255 {
256   GtkWidget *dockable;
257 
258   dockable =
259     gimp_window_strategy_show_dockable_dialog (GIMP_WINDOW_STRATEGY (gimp_get_window_strategy (gimp)),
260                                                gimp,
261                                                gimp_dialog_factory_get_singleton (),
262                                                gtk_widget_get_screen (GTK_WIDGET (editor)),
263                                                gimp_widget_get_monitor (GTK_WIDGET (editor)),
264                                                "gimp-tool-preset-editor");
265 
266   gimp_data_editor_set_data (GIMP_DATA_EDITOR (gtk_bin_get_child (GTK_BIN (dockable))),
267                              GIMP_DATA (preset));
268 }
269