gimpclipboard.c 14.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* The GIMP -- an image manipulation program
 * 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 "widgets-types.h"
26
27
28
29

#include "core/gimp.h"
#include "core/gimpbuffer.h"

30
#include "gimpclipboard.h"
31
#include "gimppixbuf.h"
32
#include "gimpselectiondata.h"
33
34
35
36

#include "gimp-intl.h"


37
#define GIMP_CLIPBOARD_KEY "gimp-clipboard"
38

39

40
41
42
43
typedef struct _GimpClipboard GimpClipboard;

struct _GimpClipboard
{
44
  GSList         *pixbuf_formats;
45

46
47
  GtkTargetEntry *target_entries;
  gint            n_target_entries;
48
49
50
51
52
};


static GimpClipboard * gimp_clipboard_get        (Gimp             *gimp);
static void            gimp_clipboard_free       (GimpClipboard    *gimp_clip);
53

54
static void      gimp_clipboard_buffer_changed   (Gimp             *gimp);
55
static void      gimp_clipboard_set_buffer       (Gimp             *gimp,
56
57
                                                  GimpBuffer       *buffer);

58
static void      gimp_clipboard_send_buffer      (GtkClipboard     *clipboard,
59
60
61
                                                  GtkSelectionData *selection_data,
                                                  guint             info,
                                                  Gimp             *gimp);
62

63
64
static GdkAtom * gimp_clipboard_wait_for_targets (gint             *n_targets);
static GdkAtom   gimp_clipboard_wait_for_buffer  (Gimp             *gimp);
65
static GdkAtom   gimp_clipboard_wait_for_svg     (Gimp             *gimp);
66
67
68


void
69
gimp_clipboard_init (Gimp *gimp)
70
{
71
72
  GimpClipboard *gimp_clip;
  GSList        *list;
73

74
75
  g_return_if_fail (GIMP_IS_GIMP (gimp));

76
77
78
79
80
81
82
83
84
85
  gimp_clip = gimp_clipboard_get (gimp);

  g_return_if_fail (gimp_clip == NULL);

  gimp_clip = g_new0 (GimpClipboard, 1);

  g_object_set_data_full (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY,
                          gimp_clip, (GDestroyNotify) gimp_clipboard_free);

  gimp_clipboard_set_buffer (gimp, gimp->global_buffer);
86

87
  g_signal_connect_object (gimp, "buffer-changed",
88
                           G_CALLBACK (gimp_clipboard_buffer_changed),
89
                           NULL, 0);
90

91
  gimp_clip->pixbuf_formats = gimp_pixbuf_get_formats ();
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

  for (list = gimp_clip->pixbuf_formats; list; list = g_slist_next (list))
    {
      GdkPixbufFormat *format = list->data;

      if (gdk_pixbuf_format_is_writable (format))
        {
          gchar **mime_types;
          gchar **type;

          mime_types = gdk_pixbuf_format_get_mime_types (format);

          for (type = mime_types; *type; type++)
            gimp_clip->n_target_entries++;

          g_strfreev (mime_types);
        }
    }

  if (gimp_clip->n_target_entries > 0)
    {
      gint i = 0;

      gimp_clip->target_entries = g_new0 (GtkTargetEntry,
                                          gimp_clip->n_target_entries);

      for (list = gimp_clip->pixbuf_formats; list; list = g_slist_next (list))
        {
          GdkPixbufFormat *format = list->data;
121

122
123
124
125
126
127
128
129
130
131
132
          if (gdk_pixbuf_format_is_writable (format))
            {
              gchar  *format_name;
              gchar **mime_types;
              gchar **type;

              format_name = gdk_pixbuf_format_get_name (format);
              mime_types  = gdk_pixbuf_format_get_mime_types (format);

              for (type = mime_types; *type; type++)
                {
133
                  const gchar *mime_type = *type;
134

135
136
137
                  if (gimp->be_verbose)
                    g_print ("GimpClipboard: writable pixbuf format: %s\n",
                             mime_type);
138
139
140
141
142
143
144
145
146
147
148
149
150

                  gimp_clip->target_entries[i].target = g_strdup (mime_type);
                  gimp_clip->target_entries[i].flags  = 0;
                  gimp_clip->target_entries[i].info   = i;

                  i++;
                }

              g_strfreev (mime_types);
              g_free (format_name);
            }
        }
    }
151
152
153
}

void
154
gimp_clipboard_exit (Gimp *gimp)
155
{
156
157
  GtkClipboard *clipboard;

158
159
  g_return_if_fail (GIMP_IS_GIMP (gimp));

160
161
162
163
164
165
  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

  if (clipboard && gtk_clipboard_get_owner (clipboard) == G_OBJECT (gimp))
    gtk_clipboard_store (clipboard);

166
  g_signal_handlers_disconnect_by_func (gimp,
167
                                        G_CALLBACK (gimp_clipboard_buffer_changed),
168
                                        NULL);
169
  gimp_clipboard_set_buffer (gimp, NULL);
170

171
  g_object_set_data (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY, NULL);
172
173
174
}

/**
175
 * gimp_clipboard_has_buffer:
176
177
178
179
180
181
182
183
184
185
186
 * @gimp: pointer to #Gimp
 *
 * Tests if there's image data in the clipboard. If the global cut
 * buffer of @gimp is empty, this function checks if there's image
 * data in %GDK_SELECTION_CLIPBOARD. This is done in a main-loop
 * similar to gtk_clipboard_wait_is_text_available(). The same caveats
 * apply here.
 *
 * Return value: %TRUE if there's image data in the clipboard, %FALSE otherwise
 **/
gboolean
187
gimp_clipboard_has_buffer (Gimp *gimp)
188
189
190
191
192
193
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

  if (gimp->global_buffer)
    return TRUE;

194
  return (gimp_clipboard_wait_for_buffer (gimp) != GDK_NONE);
195
196
}

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
 * gimp_clipboard_has_svg:
 * @gimp: pointer to #Gimp
 *
 * Tests if there's SVG data in %GDK_SELECTION_CLIPBOARD.
 * This is done in a main-loop similar to
 * gtk_clipboard_wait_is_text_available(). The same caveats apply here.
 *
 * Return value: %TRUE if there's SVG data in the clipboard, %FALSE otherwise
 **/
gboolean
gimp_clipboard_has_svg (Gimp *gimp)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

  return (gimp_clipboard_wait_for_svg (gimp) != GDK_NONE);
}

215
/**
216
 * gimp_clipboard_get_buffer:
217
218
219
220
221
222
223
224
225
226
227
228
 * @gimp: pointer to #Gimp
 *
 * Retrieves either image data from %GDK_SELECTION_CLIPBOARD or from
 * the global cut buffer of @gimp.
 *
 * The returned #GimpBuffer needs to be unref'ed when it's no longer
 * needed.
 *
 * Return value: a reference to a #GimpBuffer or %NULL if there's no
 *               image data
 **/
GimpBuffer *
229
gimp_clipboard_get_buffer (Gimp *gimp)
230
231
232
{
  GimpBuffer   *buffer = NULL;
  GtkClipboard *clipboard;
233
  GdkAtom       atom;
234
235
236
237
238
239

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

240
241
242
  if (clipboard                                                         &&
      gtk_clipboard_get_owner (clipboard)            != G_OBJECT (gimp) &&
      (atom = gimp_clipboard_wait_for_buffer (gimp)) != GDK_NONE)
243
244
245
246
247
248
249
250
251
    {
      GtkSelectionData *data;

      gimp_set_busy (gimp);

      data = gtk_clipboard_wait_for_contents (clipboard, atom);

      if (data)
        {
252
          GdkPixbuf *pixbuf = gtk_selection_data_get_pixbuf (data);
253
254
255
256
257

          gtk_selection_data_free (data);

          if (pixbuf)
            {
258
              buffer = gimp_buffer_new_from_pixbuf (pixbuf, _("Clipboard"));
259
260
261
262
263
264
265
266
267
268
269
270
271
              g_object_unref (pixbuf);
            }
        }

      gimp_unset_busy (gimp);
    }

  if (! buffer && gimp->global_buffer)
    buffer = g_object_ref (gimp->global_buffer);

  return buffer;
}

272
273
274
/**
 * gimp_clipboard_get_svg:
 * @gimp: pointer to #Gimp
Sven Neumann's avatar
Sven Neumann committed
275
 * @svg_length: returns the size of the SVG stream in bytes
276
277
278
 *
 * Retrieves SVG data from %GDK_SELECTION_CLIPBOARD.
 *
Sven Neumann's avatar
Sven Neumann committed
279
 * The returned data needs to be freed when it's no longer needed.
280
281
282
283
 *
 * Return value: a reference to a #GimpBuffer or %NULL if there's no
 *               image data
 **/
284
gchar *
285
gimp_clipboard_get_svg (Gimp  *gimp,
Sven Neumann's avatar
Sven Neumann committed
286
                        gsize *svg_length)
287
288
289
290
291
{
  GtkClipboard *clipboard;
  GdkAtom       atom;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
Sven Neumann's avatar
Sven Neumann committed
292
  g_return_val_if_fail (svg_length != NULL, NULL);
293
294
295
296
297
298
299
300
301
302
303
304
305
306

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

  if (clipboard                                                      &&
      gtk_clipboard_get_owner (clipboard)         != G_OBJECT (gimp) &&
      (atom = gimp_clipboard_wait_for_svg (gimp)) != GDK_NONE)
    {
      GtkSelectionData *data;

      data = gtk_clipboard_wait_for_contents (clipboard, atom);

      if (data)
        {
307
308
          const guchar *stream;
          gchar        *svg = NULL;
309

310
          stream = gimp_selection_data_get_stream (data, svg_length);
311

312
313
          if (stream)
            svg = g_memdup (stream, *svg_length);
314
315
316
317
318
319
320
321
322
323

          gtk_selection_data_free (data);

          return svg;
        }
    }

  return NULL;
}

324
325
326
327
328
329
330
331
332
333
334
335

/*  private functions  */

static GimpClipboard *
gimp_clipboard_get (Gimp *gimp)
{
  return g_object_get_data (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY);
}

static void
gimp_clipboard_free (GimpClipboard *gimp_clip)
{
336
337
  gint i;

338
  g_slist_free (gimp_clip->pixbuf_formats);
339
340
341
342

  for (i = 0; i < gimp_clip->n_target_entries; i++)
    g_free (gimp_clip->target_entries[i].target);

343
344
345
346
  g_free (gimp_clip->target_entries);
  g_free (gimp_clip);
}

347
static void
348
gimp_clipboard_buffer_changed (Gimp *gimp)
349
{
350
  gimp_clipboard_set_buffer (gimp, gimp->global_buffer);
351
352
353
}

static void
354
355
gimp_clipboard_set_buffer (Gimp       *gimp,
                           GimpBuffer *buffer)
356
{
357
358
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
  GtkClipboard  *clipboard;
359
360
361
362
363
364
365
366
367

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);
  if (! clipboard)
    return;

  if (buffer)
    {
      gtk_clipboard_set_with_owner (clipboard,
368
369
                                    gimp_clip->target_entries,
                                    gimp_clip->n_target_entries,
370
                                    (GtkClipboardGetFunc) gimp_clipboard_send_buffer,
371
372
                                    (GtkClipboardClearFunc) NULL,
                                    G_OBJECT (gimp));
373
374
375

      /*  mark the first entry (PNG) as suitable for storing  */
      gtk_clipboard_set_can_store (clipboard, gimp_clip->target_entries, 1);
376
377
378
379
380
381
382
    }
  else if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (gimp))
    {
      gtk_clipboard_clear (clipboard);
    }
}

383
384
static GdkAtom *
gimp_clipboard_wait_for_targets (gint *n_targets)
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
{
  GtkClipboard *clipboard;

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

  if (clipboard)
    {
      GtkSelectionData *data;

      data = gtk_clipboard_wait_for_contents (clipboard,
                                              gdk_atom_intern ("TARGETS",
                                                               FALSE));
      if (data)
        {
400
401
402
403
404
405
406
407
408
409
410
411
          GdkAtom  *targets;
          gboolean  success;

          success = gtk_selection_data_get_targets (data, &targets, n_targets);

          gtk_selection_data_free (data);

          if (success)
            {
              gint i;

              for (i = 0; i < *n_targets; i++)
412
                g_printerr ("offered type: %s\n", gdk_atom_name (targets[i]));
413

414
              g_printerr ("\n");
415
416
417
418
419
420
421
422
423
424
425
426

              return targets;
            }
        }
    }

  return NULL;
}

static GdkAtom
gimp_clipboard_wait_for_buffer (Gimp *gimp)
{
427
428
429
430
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
  GdkAtom       *targets;
  gint           n_targets;
  GdkAtom        result    = GDK_NONE;
431
432
433
434
435
436
437

  targets = gimp_clipboard_wait_for_targets (&n_targets);

  if (targets)
    {
      GSList *list;

438
      for (list = gimp_clip->pixbuf_formats; list; list = g_slist_next (list))
439
440
441
442
443
        {
          GdkPixbufFormat  *format = list->data;
          gchar           **mime_types;
          gchar           **type;

444
445
          g_printerr ("checking pixbuf format '%s'\n",
                      gdk_pixbuf_format_get_name (format));
446

447
448
449
          mime_types = gdk_pixbuf_format_get_mime_types (format);

          for (type = mime_types; *type; type++)
450
            {
451
452
              gchar   *mime_type = *type;
              GdkAtom  atom      = gdk_atom_intern (mime_type, FALSE);
453
454
              gint     i;

455
              g_printerr (" - checking mime type '%s'\n", mime_type);
456

457
              for (i = 0; i < n_targets; i++)
458
459
                {
                  if (targets[i] == atom)
460
461
462
463
                    {
                      result = atom;
                      break;
                    }
464
                }
465

466
467
              if (result != GDK_NONE)
                break;
468
469
            }

470
471
472
473
          g_strfreev (mime_types);

          if (result != GDK_NONE)
            break;
474
        }
475
476

      g_free (targets);
477
478
479
480
481
    }

  return result;
}

482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
static GdkAtom
gimp_clipboard_wait_for_svg (Gimp *gimp)
{
  GdkAtom *targets;
  gint     n_targets;
  GdkAtom  result = GDK_NONE;

  targets = gimp_clipboard_wait_for_targets (&n_targets);

  if (targets)
    {
      GdkAtom svg_atom     = gdk_atom_intern ("image/svg",     FALSE);
      GdkAtom svg_xml_atom = gdk_atom_intern ("image/svg+xml", FALSE);
      gint    i;

      for (i = 0; i < n_targets; i++)
        {
          if (targets[i] == svg_atom)
            {
              result = svg_atom;
              break;
            }
          else if (targets[i] == svg_xml_atom)
            {
              result = svg_xml_atom;
              break;
            }
        }

      g_free (targets);
    }

  return result;
}

517
static void
518
519
520
521
gimp_clipboard_send_buffer (GtkClipboard     *clipboard,
                            GtkSelectionData *selection_data,
                            guint             info,
                            Gimp             *gimp)
522
{
523
524
525
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
  GimpBuffer    *buffer    = gimp->global_buffer;
  GdkPixbuf     *pixbuf;
526
527
528
529
530
531
532
533
534

  gimp_set_busy (gimp);

  pixbuf = gimp_viewable_get_pixbuf (GIMP_VIEWABLE (buffer),
                                     gimp_buffer_get_width (buffer),
                                     gimp_buffer_get_height (buffer));

  if (pixbuf)
    {
535
536
      g_printerr ("sending pixbuf data as '%s'\n",
                  gimp_clip->target_entries[info].target);
537

538
      gtk_selection_data_set_pixbuf (selection_data, pixbuf);
539
540
541
542
543
544
545
546
    }
  else
    {
      g_warning ("%s: gimp_viewable_get_pixbuf() failed", G_STRFUNC);
    }

  gimp_unset_busy (gimp);
}