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 "libgimpbase/gimpbase.h"
26 #include "libgimpwidgets/gimpwidgets.h"
27 
28 #include "widgets-types.h"
29 
30 #include "core/gimp.h"
31 #include "core/gimpbuffer.h"
32 #include "core/gimpcontext.h"
33 #include "core/gimpimage.h"
34 #include "core/gimpimage-new.h"
35 #include "core/gimpimage-undo.h"
36 #include "core/gimplayer.h"
37 #include "core/gimplayermask.h"
38 #include "core/gimptoolinfo.h"
39 
40 #include "file/file-open.h"
41 
42 #include "gimpdnd.h"
43 #include "gimptoolbox.h"
44 #include "gimptoolbox-dnd.h"
45 #include "gimpwidgets-utils.h"
46 
47 #include "gimp-intl.h"
48 
49 
50 /*  local function prototypes  */
51 
52 static void   gimp_toolbox_drop_uri_list  (GtkWidget       *widget,
53                                            gint             x,
54                                            gint             y,
55                                            GList           *uri_list,
56                                            gpointer         data);
57 static void   gimp_toolbox_drop_drawable  (GtkWidget       *widget,
58                                            gint             x,
59                                            gint             y,
60                                            GimpViewable    *viewable,
61                                            gpointer         data);
62 static void   gimp_toolbox_drop_tool      (GtkWidget       *widget,
63                                            gint             x,
64                                            gint             y,
65                                            GimpViewable    *viewable,
66                                            gpointer         data);
67 static void   gimp_toolbox_drop_buffer    (GtkWidget       *widget,
68                                            gint             x,
69                                            gint             y,
70                                            GimpViewable    *viewable,
71                                            gpointer         data);
72 static void   gimp_toolbox_drop_component (GtkWidget       *widget,
73                                            gint             x,
74                                            gint             y,
75                                            GimpImage       *image,
76                                            GimpChannelType  component,
77                                            gpointer         data);
78 static void   gimp_toolbox_drop_pixbuf    (GtkWidget       *widget,
79                                            gint             x,
80                                            gint             y,
81                                            GdkPixbuf       *pixbuf,
82                                            gpointer         data);
83 
84 
85 /*  public functions  */
86 
87 void
gimp_toolbox_dnd_init(GimpToolbox * toolbox,GtkWidget * vbox)88 gimp_toolbox_dnd_init (GimpToolbox *toolbox,
89                        GtkWidget   *vbox)
90 {
91   GimpContext *context = NULL;
92 
93   g_return_if_fail (GIMP_IS_TOOLBOX (toolbox));
94   g_return_if_fail (GTK_IS_BOX (vbox));
95 
96   context = gimp_toolbox_get_context (toolbox);
97 
98   /* Before caling any dnd helper functions, setup the drag
99    * destination manually since we want to handle all drag events
100    * manually, otherwise we would not be able to give the drag handler
101    * a chance to handle drag events
102    */
103   gtk_drag_dest_set (vbox,
104                      0, NULL, 0,
105                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
106 
107   gimp_dnd_viewable_dest_add  (vbox,
108                                GIMP_TYPE_LAYER,
109                                gimp_toolbox_drop_drawable,
110                                context);
111   gimp_dnd_viewable_dest_add  (vbox,
112                                GIMP_TYPE_LAYER_MASK,
113                                gimp_toolbox_drop_drawable,
114                                context);
115   gimp_dnd_viewable_dest_add  (vbox,
116                                GIMP_TYPE_CHANNEL,
117                                gimp_toolbox_drop_drawable,
118                                context);
119   gimp_dnd_viewable_dest_add  (vbox,
120                                GIMP_TYPE_TOOL_INFO,
121                                gimp_toolbox_drop_tool,
122                                context);
123   gimp_dnd_viewable_dest_add  (vbox,
124                                GIMP_TYPE_BUFFER,
125                                gimp_toolbox_drop_buffer,
126                                context);
127   gimp_dnd_component_dest_add (vbox,
128                                gimp_toolbox_drop_component,
129                                context);
130   gimp_dnd_uri_list_dest_add  (vbox,
131                                gimp_toolbox_drop_uri_list,
132                                context);
133   gimp_dnd_pixbuf_dest_add    (vbox,
134                                gimp_toolbox_drop_pixbuf,
135                                context);
136 }
137 
138 
139 /*  private functions  */
140 
141 static void
gimp_toolbox_drop_uri_list(GtkWidget * widget,gint x,gint y,GList * uri_list,gpointer data)142 gimp_toolbox_drop_uri_list (GtkWidget *widget,
143                             gint       x,
144                             gint       y,
145                             GList     *uri_list,
146                             gpointer   data)
147 {
148   GimpContext *context = GIMP_CONTEXT (data);
149   GList       *list;
150 
151   if (context->gimp->busy)
152     return;
153 
154   for (list = uri_list; list; list = g_list_next (list))
155     {
156       GFile             *file = g_file_new_for_uri (list->data);
157       GimpImage         *image;
158       GimpPDBStatusType  status;
159       GError            *error = NULL;
160 
161       image = file_open_with_display (context->gimp, context, NULL,
162                                       file, FALSE,
163                                       G_OBJECT (gtk_widget_get_screen (widget)),
164                                       gimp_widget_get_monitor (widget),
165                                       &status, &error);
166 
167       if (! image && status != GIMP_PDB_CANCEL)
168         {
169           gimp_message (context->gimp, G_OBJECT (widget), GIMP_MESSAGE_ERROR,
170                         _("Opening '%s' failed:\n\n%s"),
171                         gimp_file_get_utf8_name (file), error->message);
172           g_clear_error (&error);
173         }
174 
175       g_object_unref (file);
176     }
177 }
178 
179 static void
gimp_toolbox_drop_drawable(GtkWidget * widget,gint x,gint y,GimpViewable * viewable,gpointer data)180 gimp_toolbox_drop_drawable (GtkWidget    *widget,
181                             gint          x,
182                             gint          y,
183                             GimpViewable *viewable,
184                             gpointer      data)
185 {
186   GimpContext *context = GIMP_CONTEXT (data);
187   GimpImage   *new_image;
188 
189   if (context->gimp->busy)
190     return;
191 
192   new_image = gimp_image_new_from_drawable (context->gimp,
193                                             GIMP_DRAWABLE (viewable));
194   gimp_create_display (context->gimp, new_image, GIMP_UNIT_PIXEL, 1.0,
195                        G_OBJECT (gtk_widget_get_screen (widget)),
196                        gimp_widget_get_monitor (widget));
197   g_object_unref (new_image);
198 }
199 
200 static void
gimp_toolbox_drop_tool(GtkWidget * widget,gint x,gint y,GimpViewable * viewable,gpointer data)201 gimp_toolbox_drop_tool (GtkWidget    *widget,
202                         gint          x,
203                         gint          y,
204                         GimpViewable *viewable,
205                         gpointer      data)
206 {
207   GimpContext *context = GIMP_CONTEXT (data);
208 
209   if (context->gimp->busy)
210     return;
211 
212   gimp_context_set_tool (context, GIMP_TOOL_INFO (viewable));
213 }
214 
215 static void
gimp_toolbox_drop_buffer(GtkWidget * widget,gint x,gint y,GimpViewable * viewable,gpointer data)216 gimp_toolbox_drop_buffer (GtkWidget    *widget,
217                           gint          x,
218                           gint          y,
219                           GimpViewable *viewable,
220                           gpointer      data)
221 {
222   GimpContext *context = GIMP_CONTEXT (data);
223   GimpImage   *image;
224 
225   if (context->gimp->busy)
226     return;
227 
228   image = gimp_image_new_from_buffer (context->gimp,
229                                       GIMP_BUFFER (viewable));
230   gimp_create_display (image->gimp, image, GIMP_UNIT_PIXEL, 1.0,
231                        G_OBJECT (gtk_widget_get_screen (widget)),
232                        gimp_widget_get_monitor (widget));
233   g_object_unref (image);
234 }
235 
236 static void
gimp_toolbox_drop_component(GtkWidget * widget,gint x,gint y,GimpImage * image,GimpChannelType component,gpointer data)237 gimp_toolbox_drop_component (GtkWidget       *widget,
238                              gint             x,
239                              gint             y,
240                              GimpImage       *image,
241                              GimpChannelType  component,
242                              gpointer         data)
243 {
244   GimpContext *context = GIMP_CONTEXT (data);
245   GimpImage   *new_image;
246 
247   if (context->gimp->busy)
248     return;
249 
250   new_image = gimp_image_new_from_component (context->gimp,
251                                              image, component);
252   gimp_create_display (new_image->gimp, new_image, GIMP_UNIT_PIXEL, 1.0,
253                        G_OBJECT (gtk_widget_get_screen (widget)),
254                        gimp_widget_get_monitor (widget));
255   g_object_unref (new_image);
256 }
257 
258 static void
gimp_toolbox_drop_pixbuf(GtkWidget * widget,gint x,gint y,GdkPixbuf * pixbuf,gpointer data)259 gimp_toolbox_drop_pixbuf (GtkWidget *widget,
260                           gint       x,
261                           gint       y,
262                           GdkPixbuf *pixbuf,
263                           gpointer   data)
264 {
265   GimpContext   *context = GIMP_CONTEXT (data);
266   GimpImage     *new_image;
267 
268   if (context->gimp->busy)
269     return;
270 
271   new_image = gimp_image_new_from_pixbuf (context->gimp, pixbuf,
272                                           _("Dropped Buffer"));
273   gimp_create_display (new_image->gimp, new_image, GIMP_UNIT_PIXEL, 1.0,
274                        G_OBJECT (gtk_widget_get_screen (widget)),
275                        gimp_widget_get_monitor (widget));
276   g_object_unref (new_image);
277 }
278