gimptoolbox-dnd.c 12.2 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

21 22
#include <string.h>

23 24
#include <gtk/gtk.h>

25
#include "libgimpbase/gimpbase.h"
26 27 28 29 30
#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "core/gimp.h"
31
#include "core/gimp-edit.h"
32 33 34 35 36 37 38 39
#include "core/gimpbuffer.h"
#include "core/gimpcontext.h"
#include "core/gimpimage.h"
#include "core/gimpimage-colormap.h"
#include "core/gimplayer.h"
#include "core/gimplayermask.h"
#include "core/gimptoolinfo.h"

40 41 42
#include "file/file-open.h"
#include "file/file-utils.h"

43 44 45 46
#include "gimpdnd.h"
#include "gimptoolbox.h"
#include "gimptoolbox-dnd.h"

47 48
#include "gimp-intl.h"

49 50 51

/*  local function prototypes  */

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static void   gimp_toolbox_drop_uri_list  (GtkWidget       *widget,
                                           gint             x,
                                           gint             y,
                                           GList           *uri_list,
                                           gpointer         data);
static void   gimp_toolbox_drop_drawable  (GtkWidget       *widget,
                                           gint             x,
                                           gint             y,
                                           GimpViewable    *viewable,
                                           gpointer         data);
static void   gimp_toolbox_drop_tool      (GtkWidget       *widget,
                                           gint             x,
                                           gint             y,
                                           GimpViewable    *viewable,
                                           gpointer         data);
static void   gimp_toolbox_drop_buffer    (GtkWidget       *widget,
                                           gint             x,
                                           gint             y,
                                           GimpViewable    *viewable,
                                           gpointer         data);
static void   gimp_toolbox_drop_component (GtkWidget       *widget,
                                           gint             x,
                                           gint             y,
                                           GimpImage       *image,
                                           GimpChannelType  component,
                                           gpointer         data);
78 79 80 81 82
static void   gimp_toolbox_drop_pixbuf    (GtkWidget       *widget,
                                           gint             x,
                                           gint             y,
                                           GdkPixbuf       *pixbuf,
                                           gpointer         data);
83 84 85 86 87 88 89 90 91 92 93 94 95


/*  public functions  */

void
gimp_toolbox_dnd_init (GimpToolbox *toolbox)
{
  GimpDock *dock;

  g_return_if_fail (GIMP_IS_TOOLBOX (toolbox));

  dock = GIMP_DOCK (toolbox);

96 97 98 99 100 101
  gimp_dnd_uri_list_dest_add (GTK_WIDGET (toolbox),
                              gimp_toolbox_drop_uri_list,
                              dock->context);
  gimp_dnd_uri_list_dest_add (toolbox->tool_wbox,
                              gimp_toolbox_drop_uri_list,
                              dock->context);
102

103
  gimp_dnd_viewable_dest_add (toolbox->tool_wbox, GIMP_TYPE_LAYER,
104 105
                              gimp_toolbox_drop_drawable,
                              dock->context);
106
  gimp_dnd_viewable_dest_add (toolbox->tool_wbox, GIMP_TYPE_LAYER_MASK,
107 108
                              gimp_toolbox_drop_drawable,
                              dock->context);
109
  gimp_dnd_viewable_dest_add (toolbox->tool_wbox, GIMP_TYPE_CHANNEL,
110 111
                              gimp_toolbox_drop_drawable,
                              dock->context);
112
  gimp_dnd_viewable_dest_add (toolbox->tool_wbox, GIMP_TYPE_TOOL_INFO,
113 114
                              gimp_toolbox_drop_tool,
                              dock->context);
115
  gimp_dnd_viewable_dest_add (toolbox->tool_wbox, GIMP_TYPE_BUFFER,
116 117
                              gimp_toolbox_drop_buffer,
                              dock->context);
118 119 120 121

  gimp_dnd_component_dest_add (toolbox->tool_wbox,
                               gimp_toolbox_drop_component,
                               dock->context);
122 123 124
  gimp_dnd_pixbuf_dest_add    (toolbox->tool_wbox,
                               gimp_toolbox_drop_pixbuf,
                               dock->context);
125 126 127 128 129
}


/*  private functions  */

130
static void
131
gimp_toolbox_drop_uri_list (GtkWidget *widget,
132 133
                            gint       x,
                            gint       y,
134 135
                            GList     *uri_list,
                            gpointer   data)
136 137 138 139
{
  GimpContext *context = GIMP_CONTEXT (data);
  GList       *list;

140 141 142
  if (context->gimp->busy)
    return;

143
  for (list = uri_list; list; list = g_list_next (list))
144 145
    {
      const gchar       *uri   = list->data;
146
      GimpImage         *image;
147 148 149
      GimpPDBStatusType  status;
      GError            *error = NULL;

150
      image = file_open_with_display (context->gimp, context, NULL,
151
                                      uri, FALSE, &status, &error);
152

153
      if (! image && status != GIMP_PDB_CANCEL)
154
        {
155
          gchar *filename = file_utils_uri_display_name (uri);
156

157 158 159
          gimp_message (context->gimp, NULL, GIMP_MESSAGE_ERROR,
                        _("Opening '%s' failed:\n\n%s"),
                        filename, error->message);
160 161 162 163 164 165 166

          g_clear_error (&error);
          g_free (filename);
        }
    }
}

167 168
static void
gimp_toolbox_drop_drawable (GtkWidget    *widget,
169 170
                            gint          x,
                            gint          y,
171 172 173
                            GimpViewable *viewable,
                            gpointer      data)
{
174
  GimpContext       *context = GIMP_CONTEXT (data);
175 176
  GimpDrawable      *drawable;
  GimpItem          *item;
177
  GimpImage         *image;
178
  GimpImage         *new_image;
179
  GimpLayer         *new_layer;
180
  GType              new_type;
181 182 183 184 185
  gint               width, height;
  gint               off_x, off_y;
  gint               bytes;
  GimpImageBaseType  type;

186 187 188
  if (context->gimp->busy)
    return;

189 190
  drawable = GIMP_DRAWABLE (viewable);
  item     = GIMP_ITEM (viewable);
191
  image    = gimp_item_get_image (item);
192 193 194 195 196 197 198

  width  = gimp_item_width  (item);
  height = gimp_item_height (item);
  bytes  = gimp_drawable_bytes (drawable);

  type = GIMP_IMAGE_TYPE_BASE_TYPE (gimp_drawable_type (drawable));

199
  new_image = gimp_create_image (image->gimp, width, height, type, TRUE);
200
  gimp_image_undo_disable (new_image);
201

202 203
  if (type == GIMP_INDEXED)
    gimp_image_set_colormap (new_image,
204 205
                             gimp_image_get_colormap (image),
                             gimp_image_get_colormap_size (image),
206 207
                             FALSE);

208 209
  gimp_image_set_resolution (new_image, image->xresolution, image->yresolution);
  gimp_image_set_unit (new_image, gimp_image_get_unit (image));
210 211

  if (GIMP_IS_LAYER (drawable))
212
    new_type = G_TYPE_FROM_INSTANCE (drawable);
213
  else
214
    new_type = GIMP_TYPE_LAYER;
215

216 217
  new_layer = GIMP_LAYER (gimp_item_convert (GIMP_ITEM (drawable), new_image,
                                             new_type, FALSE));
218 219

  gimp_object_set_name (GIMP_OBJECT (new_layer),
220
                        gimp_object_get_name (GIMP_OBJECT (drawable)));
221 222 223 224

  gimp_item_offsets (GIMP_ITEM (new_layer), &off_x, &off_y);
  gimp_item_translate (GIMP_ITEM (new_layer), -off_x, -off_y, FALSE);

225
  gimp_image_add_layer (new_image, new_layer, 0);
226

227
  gimp_image_undo_enable (new_image);
228

229
  gimp_create_display (image->gimp, new_image, GIMP_UNIT_PIXEL, 1.0);
230
  g_object_unref (new_image);
231 232 233 234
}

static void
gimp_toolbox_drop_tool (GtkWidget    *widget,
235 236
                        gint          x,
                        gint          y,
237 238 239 240 241
                        GimpViewable *viewable,
                        gpointer      data)
{
  GimpContext *context = GIMP_CONTEXT (data);

242 243 244
  if (context->gimp->busy)
    return;

245 246 247 248 249
  gimp_context_set_tool (context, GIMP_TOOL_INFO (viewable));
}

static void
gimp_toolbox_drop_buffer (GtkWidget    *widget,
250 251
                          gint          x,
                          gint          y,
252 253 254 255
                          GimpViewable *viewable,
                          gpointer      data)
{
  GimpContext *context = GIMP_CONTEXT (data);
256
  GimpImage   *image;
257 258 259 260

  if (context->gimp->busy)
    return;

261 262 263 264 265 266 267
  image = gimp_edit_paste_as_new (context->gimp, NULL, GIMP_BUFFER (viewable));

  if (image)
    {
      gimp_create_display (image->gimp, image, GIMP_UNIT_PIXEL, 1.0);
      g_object_unref (image);
    }
268
}
269 270 271 272 273 274 275 276 277

static void
gimp_toolbox_drop_component (GtkWidget       *widget,
                             gint             x,
                             gint             y,
                             GimpImage       *image,
                             GimpChannelType  component,
                             gpointer         data)
{
278
  GimpContext *context = GIMP_CONTEXT (data);
279 280 281 282 283
  GimpChannel *channel;
  GimpImage   *new_image;
  GimpLayer   *new_layer;
  const gchar *desc;

284 285 286
  if (context->gimp->busy)
    return;

287 288 289
  new_image = gimp_create_image (image->gimp,
                                 gimp_image_get_width  (image),
                                 gimp_image_get_height (image),
290
                                 GIMP_GRAY, TRUE);
291 292 293

  gimp_image_undo_disable (new_image);

294
  gimp_image_set_resolution (new_image, image->xresolution, image->yresolution);
295 296 297 298 299 300 301 302 303 304 305
  gimp_image_set_unit (new_image, gimp_image_get_unit (image));

  channel = gimp_channel_new_from_component (image, component, NULL, NULL);

  new_layer = GIMP_LAYER (gimp_item_convert (GIMP_ITEM (channel), new_image,
                                             GIMP_TYPE_LAYER, FALSE));

  g_object_unref (channel);

  gimp_enum_get_value (GIMP_TYPE_CHANNEL_TYPE, component,
                       NULL, NULL, &desc, NULL);
306 307
  gimp_object_take_name (GIMP_OBJECT (new_layer),
                         g_strdup_printf (_("%s Channel Copy"), desc));
308 309 310 311 312 313 314 315

  gimp_image_add_layer (new_image, new_layer, 0);

  gimp_image_undo_enable (new_image);

  gimp_create_display (new_image->gimp, new_image, GIMP_UNIT_PIXEL, 1.0);
  g_object_unref (new_image);
}
316 317 318 319 320 321 322 323

static void
gimp_toolbox_drop_pixbuf (GtkWidget *widget,
                          gint       x,
                          gint       y,
                          GdkPixbuf *pixbuf,
                          gpointer   data)
{
324 325 326 327
  GimpContext   *context = GIMP_CONTEXT (data);
  GimpImageType  image_type;
  GimpImage     *new_image;
  GimpLayer     *new_layer;
328 329 330 331 332 333

  if (context->gimp->busy)
    return;

  switch (gdk_pixbuf_get_n_channels (pixbuf))
    {
334 335 336 337
    case 1: image_type = GIMP_GRAY_IMAGE;  break;
    case 2: image_type = GIMP_GRAYA_IMAGE; break;
    case 3: image_type = GIMP_RGB_IMAGE;   break;
    case 4: image_type = GIMP_RGBA_IMAGE;  break;
338 339 340 341 342 343 344 345 346 347
      break;

    default:
      g_return_if_reached ();
      break;
    }

  new_image = gimp_create_image (context->gimp,
                                 gdk_pixbuf_get_width  (pixbuf),
                                 gdk_pixbuf_get_height (pixbuf),
348 349
                                 GIMP_IMAGE_TYPE_BASE_TYPE (image_type),
                                 FALSE);
350 351 352 353

  gimp_image_undo_disable (new_image);

  new_layer =
354
    gimp_layer_new_from_pixbuf (pixbuf, new_image, image_type,
355 356 357 358 359 360 361 362 363 364
                                _("Dropped Buffer"),
                                GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);

  gimp_image_add_layer (new_image, new_layer, 0);

  gimp_image_undo_enable (new_image);

  gimp_create_display (new_image->gimp, new_image, GIMP_UNIT_PIXEL, 1.0);
  g_object_unref (new_image);
}