gimpclipboard.c 33.8 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
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 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20 21
#include <string.h>

22
#include <gegl.h>
23 24
#include <gtk/gtk.h>

25
#include "widgets-types.h"
26 27 28

#include "core/gimp.h"
#include "core/gimpbuffer.h"
29
#include "core/gimpcurve.h"
30 31
#include "core/gimpimage.h"
#include "core/gimppickable.h"
32

33
#include "gimpclipboard.h"
34
#include "gimppixbuf.h"
35
#include "gimpselectiondata.h"
36 37 38 39

#include "gimp-intl.h"


40
#define GIMP_CLIPBOARD_KEY "gimp-clipboard"
41

42

43 44 45 46
typedef struct _GimpClipboard GimpClipboard;

struct _GimpClipboard
{
47
  GSList         *pixbuf_formats;
48

49 50 51 52 53
  GtkTargetEntry *image_target_entries;
  gint            n_image_target_entries;

  GtkTargetEntry *buffer_target_entries;
  gint            n_buffer_target_entries;
54 55 56 57

  GtkTargetEntry *svg_target_entries;
  gint            n_svg_target_entries;

58 59 60
  GtkTargetEntry *curve_target_entries;
  gint            n_curve_target_entries;

61
  GimpImage      *image;
62
  GimpBuffer     *buffer;
63
  gchar          *svg;
64
  GimpCurve      *curve;
65 66 67 68
};


static GimpClipboard * gimp_clipboard_get        (Gimp             *gimp);
69 70

static GimpClipboard * gimp_clipboard_new        (gboolean          verbose);
71
static void            gimp_clipboard_free       (GimpClipboard    *gimp_clip);
72

73 74
static void      gimp_clipboard_clear            (GimpClipboard    *gimp_clip);

75 76
static GdkAtom * gimp_clipboard_wait_for_targets (Gimp             *gimp,
                                                  gint             *n_targets);
77
static GdkAtom   gimp_clipboard_wait_for_image   (Gimp             *gimp);
78 79
static GdkAtom   gimp_clipboard_wait_for_buffer  (Gimp             *gimp);
static GdkAtom   gimp_clipboard_wait_for_svg     (Gimp             *gimp);
80
static GdkAtom   gimp_clipboard_wait_for_curve   (Gimp             *gimp);
81

82
static void      gimp_clipboard_send_image       (GtkClipboard     *clipboard,
83
                                                  GtkSelectionData *data,
84 85
                                                  guint             info,
                                                  Gimp             *gimp);
86
static void      gimp_clipboard_send_buffer      (GtkClipboard     *clipboard,
87
                                                  GtkSelectionData *data,
88 89
                                                  guint             info,
                                                  Gimp             *gimp);
90
static void      gimp_clipboard_send_svg         (GtkClipboard     *clipboard,
91
                                                  GtkSelectionData *data,
92 93
                                                  guint             info,
                                                  Gimp             *gimp);
94
static void      gimp_clipboard_send_curve       (GtkClipboard     *clipboard,
95
                                                  GtkSelectionData *data,
96 97
                                                  guint             info,
                                                  Gimp             *gimp);
98 99


100
/*  public functions  */
101 102

void
103
gimp_clipboard_init (Gimp *gimp)
104
{
105
  GimpClipboard *gimp_clip;
106

107 108
  g_return_if_fail (GIMP_IS_GIMP (gimp));

109 110 111 112
  gimp_clip = gimp_clipboard_get (gimp);

  g_return_if_fail (gimp_clip == NULL);

113
  gimp_clip = gimp_clipboard_new (gimp->be_verbose);
114 115 116

  g_object_set_data_full (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY,
                          gimp_clip, (GDestroyNotify) gimp_clipboard_free);
117 118 119
}

void
120
gimp_clipboard_exit (Gimp *gimp)
121
{
122 123
  GtkClipboard *clipboard;

124 125
  g_return_if_fail (GIMP_IS_GIMP (gimp));

126 127 128
  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

129 130 131 132 133
  if (clipboard &&
      gtk_clipboard_get_owner (clipboard) == G_OBJECT (gimp))
    {
      gtk_clipboard_store (clipboard);
    }
134

135
  g_object_set_data (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY, NULL);
136 137
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/**
 * gimp_clipboard_has_image:
 * @gimp: pointer to #Gimp
 *
 * Tests if there's an image in the clipboard. If the global image cut
 * buffer of @gimp is empty, this function returns %NULL.
 *
 * Return value: %TRUE if there's an image in the clipboard, %FALSE otherwise
 **/
gboolean
gimp_clipboard_has_image (Gimp *gimp)
{
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

158 159
  if (clipboard &&
      gtk_clipboard_get_owner (clipboard) != G_OBJECT (gimp))
160
    {
161 162 163 164 165 166
      if (gimp_clipboard_wait_for_image (gimp) != GDK_NONE)
        {
          return TRUE;
        }

      return FALSE;
167 168 169 170 171 172 173
    }

  gimp_clip = gimp_clipboard_get (gimp);

  return (gimp_clip->image != NULL);
}

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
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;

192 193
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

194 195 196
  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

197 198
  if (clipboard &&
      gtk_clipboard_get_owner (clipboard) != G_OBJECT (gimp))
199
    {
200 201 202 203 204 205
      if (gimp_clipboard_wait_for_buffer (gimp) != GDK_NONE)
        {
          return TRUE;
        }

      return FALSE;
206
    }
207

208 209
  gimp_clip = gimp_clipboard_get (gimp);

210
  return (gimp_clip->buffer != NULL);
211 212
}

213 214 215 216 217 218 219 220 221 222 223 224 225
/**
 * 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)
{
226 227 228
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;

229 230
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

231 232 233
  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

234 235
  if (clipboard &&
      gtk_clipboard_get_owner (clipboard) != G_OBJECT (gimp))
236
    {
237
      if (gimp_clipboard_wait_for_svg (gimp) != GDK_NONE)
238 239 240 241 242
        {
          return TRUE;
        }

      return FALSE;
243 244
    }

245 246
  gimp_clip = gimp_clipboard_get (gimp);

247
  return (gimp_clip->svg != NULL);
248 249
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
/**
 * gimp_clipboard_has_curve:
 * @gimp: pointer to #Gimp
 *
 * Tests if there's curve 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 curve data in the clipboard, %FALSE otherwise
 **/
gboolean
gimp_clipboard_has_curve (Gimp *gimp)
{
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

271 272
  if (clipboard &&
      gtk_clipboard_get_owner (clipboard) != G_OBJECT (gimp))
273
    {
274 275 276 277 278 279
      if (gimp_clipboard_wait_for_curve (gimp) != GDK_NONE)
        {
          return TRUE;
        }

      return FALSE;
280 281 282 283 284 285 286
    }

  gimp_clip = gimp_clipboard_get (gimp);

  return (gimp_clip->curve != NULL);
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
/**
 * gimp_clipboard_get_object:
 * @gimp: pointer to #Gimp
 *
 * Retrieves either an image or a buffer from the global image cut
 * buffer of @gimp.
 *
 * The returned #GimpObject needs to be unref'ed when it's no longer
 * needed.
 *
 * Return value: a reference to a #GimpObject or %NULL if there's no
 *               image or buffer in the clipboard
 **/
GimpObject *
gimp_clipboard_get_object (Gimp *gimp)
{
  GimpObject *object;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

  object = GIMP_OBJECT (gimp_clipboard_get_image (gimp));

  if (! object)
    object = GIMP_OBJECT (gimp_clipboard_get_buffer (gimp));

  return object;
}

315 316 317 318
/**
 * gimp_clipboard_get_image:
 * @gimp: pointer to #Gimp
 *
319
 * Retrieves an image from the global image cut buffer of @gimp.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
 *
 * The returned #GimpImage needs to be unref'ed when it's no longer
 * needed.
 *
 * Return value: a reference to a #GimpImage or %NULL if there's no
 *               image in the clipboard
 **/
GimpImage *
gimp_clipboard_get_image (Gimp *gimp)
{
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;
  GimpImage     *image = NULL;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

339 340
  if (clipboard &&
      gtk_clipboard_get_owner (clipboard) != G_OBJECT (gimp))
341
    {
342
      GdkAtom atom = gimp_clipboard_wait_for_image (gimp);
343

344 345 346
      if (atom != GDK_NONE)
        {
          GtkSelectionData *data;
347

348
          gimp_set_busy (gimp);
349

350 351 352 353 354 355 356 357
          data = gtk_clipboard_wait_for_contents (clipboard, atom);

          if (data)
            {
              image = gimp_selection_data_get_xcf (data, gimp);

              gtk_selection_data_free (data);
            }
358

359
          gimp_unset_busy (gimp);
360 361
        }

362
      return image;
363 364 365 366 367 368 369 370 371 372
    }

  gimp_clip = gimp_clipboard_get (gimp);

  if (! image && gimp_clip->image)
    image = g_object_ref (gimp_clip->image);

  return image;
}

373
/**
374
 * gimp_clipboard_get_buffer:
375 376 377 378 379 380 381 382 383 384 385 386
 * @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 *
387
gimp_clipboard_get_buffer (Gimp *gimp)
388
{
389 390 391
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;
  GimpBuffer    *buffer = NULL;
392 393 394 395 396 397

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

398 399
  if (clipboard &&
      gtk_clipboard_get_owner (clipboard) != G_OBJECT (gimp))
400
    {
401
      GdkAtom atom = gimp_clipboard_wait_for_buffer (gimp);
402

403
      if (atom != GDK_NONE)
404
        {
405
          GtkSelectionData *data;
406

407
          gimp_set_busy (gimp);
408

409 410 411
          data = gtk_clipboard_wait_for_contents (clipboard, atom);

          if (data)
412
            {
413 414 415 416 417 418 419 420 421 422
              GdkPixbuf *pixbuf = gtk_selection_data_get_pixbuf (data);

              gtk_selection_data_free (data);

              if (pixbuf)
                {
                  buffer = gimp_buffer_new_from_pixbuf (pixbuf, _("Clipboard"),
                                                        0, 0);
                  g_object_unref (pixbuf);
                }
423
            }
424 425

          gimp_unset_busy (gimp);
426 427
        }

428
      return buffer;
429 430
    }

431 432
  gimp_clip = gimp_clipboard_get (gimp);

433 434
  if (! buffer && gimp_clip->buffer)
    buffer = g_object_ref (gimp_clip->buffer);
435 436 437 438

  return buffer;
}

439 440 441
/**
 * gimp_clipboard_get_svg:
 * @gimp: pointer to #Gimp
Sven Neumann's avatar
Sven Neumann committed
442
 * @svg_length: returns the size of the SVG stream in bytes
443
 *
444 445
 * Retrieves SVG data from %GDK_SELECTION_CLIPBOARD or from the global
 * SVG buffer of @gimp.
446
 *
Sven Neumann's avatar
Sven Neumann committed
447
 * The returned data needs to be freed when it's no longer needed.
448 449 450 451
 *
 * Return value: a reference to a #GimpBuffer or %NULL if there's no
 *               image data
 **/
452
gchar *
453
gimp_clipboard_get_svg (Gimp  *gimp,
Sven Neumann's avatar
Sven Neumann committed
454
                        gsize *svg_length)
455
{
456 457 458
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;
  gchar         *svg = NULL;
459 460

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
Sven Neumann's avatar
Sven Neumann committed
461
  g_return_val_if_fail (svg_length != NULL, NULL);
462

463 464
  *svg_length = 0;

465 466 467
  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

468 469
  if (clipboard &&
      gtk_clipboard_get_owner (clipboard) != G_OBJECT (gimp))
470
    {
471
      GdkAtom atom = gimp_clipboard_wait_for_svg (gimp);
472

473 474 475
      if (atom != GDK_NONE)
        {
          GtkSelectionData *data;
476

477
          gimp_set_busy (gimp);
478

479
          data = gtk_clipboard_wait_for_contents (clipboard, atom);
480

481 482 483
          if (data)
            {
              const guchar *stream;
484

485
              stream = gimp_selection_data_get_stream (data, svg_length);
486

487 488 489 490 491 492 493
              if (stream)
                svg = g_memdup (stream, *svg_length);

              gtk_selection_data_free (data);
            }

          gimp_unset_busy (gimp);
494
        }
495

496
      return svg;
497 498
    }

499 500
  gimp_clip = gimp_clipboard_get (gimp);

501 502 503 504 505 506 507 508 509
  if (! svg && gimp_clip->svg)
    {
      svg = g_strdup (gimp_clip->svg);
      *svg_length = strlen (svg);
    }

  return svg;
}

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
/**
 * gimp_clipboard_get_curve:
 * @gimp: pointer to #Gimp
 *
 * Retrieves curve data from %GDK_SELECTION_CLIPBOARD or from the global
 * curve buffer of @gimp.
 *
 * The returned curve needs to be unref'ed when it's no longer needed.
 *
 * Return value: a reference to a #GimpCurve or %NULL if there's no
 *               curve data
 **/
GimpCurve *
gimp_clipboard_get_curve (Gimp *gimp)
{
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;
  GimpCurve     *curve = NULL;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

534 535
  if (clipboard &&
      gtk_clipboard_get_owner (clipboard) != G_OBJECT (gimp))
536
    {
537
      GdkAtom atom = gimp_clipboard_wait_for_curve (gimp);
538

539 540 541
      if (atom != GDK_NONE)
        {
          GtkSelectionData *data;
542

543
          gimp_set_busy (gimp);
544

545 546 547 548 549 550 551 552
          data = gtk_clipboard_wait_for_contents (clipboard, atom);

          if (data)
            {
              curve = gimp_selection_data_get_curve (data);

              gtk_selection_data_free (data);
            }
553

554
          gimp_unset_busy (gimp);
555 556
        }

557
      return curve;
558 559 560 561 562 563 564 565 566 567
    }

  gimp_clip = gimp_clipboard_get (gimp);

  if (! curve && gimp_clip->curve)
    curve = g_object_ref (gimp_clip->curve);

  return curve;
}

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
/**
 * gimp_clipboard_set_image:
 * @gimp:  pointer to #Gimp
 * @image: a #GimpImage, or %NULL.
 *
 * Offers the image in %GDK_SELECTION_CLIPBOARD.
 **/
void
gimp_clipboard_set_image (Gimp      *gimp,
                          GimpImage *image)
{
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;

  g_return_if_fail (GIMP_IS_GIMP (gimp));
  g_return_if_fail (image == NULL || GIMP_IS_IMAGE (image));

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

  gimp_clip = gimp_clipboard_get (gimp);

  gimp_clipboard_clear (gimp_clip);

  if (image)
    {
      gimp_clip->image = g_object_ref (image);

      gtk_clipboard_set_with_owner (clipboard,
                                    gimp_clip->image_target_entries,
                                    gimp_clip->n_image_target_entries,
                                    (GtkClipboardGetFunc) gimp_clipboard_send_image,
                                    (GtkClipboardClearFunc) NULL,
                                    G_OBJECT (gimp));

      /*  mark the first two entries (image/x-xcf and image/png) as
       *  suitable for storing
       */
      gtk_clipboard_set_can_store (clipboard,
                                   gimp_clip->image_target_entries,
                                   MIN (2, gimp_clip->n_image_target_entries));
    }
  else if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (gimp))
    {
      gtk_clipboard_clear (clipboard);
    }
}

618 619
/**
 * gimp_clipboard_set_buffer:
620 621
 * @gimp:   pointer to #Gimp
 * @buffer: a #GimpBuffer, or %NULL.
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
 *
 * Offers the buffer in %GDK_SELECTION_CLIPBOARD.
 **/
void
gimp_clipboard_set_buffer (Gimp       *gimp,
                           GimpBuffer *buffer)
{
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;

  g_return_if_fail (GIMP_IS_GIMP (gimp));
  g_return_if_fail (buffer == NULL || GIMP_IS_BUFFER (buffer));

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

640 641
  gimp_clip = gimp_clipboard_get (gimp);

642
  gimp_clipboard_clear (gimp_clip);
643 644 645 646 647 648

  if (buffer)
    {
      gimp_clip->buffer = g_object_ref (buffer);

      gtk_clipboard_set_with_owner (clipboard,
649 650
                                    gimp_clip->buffer_target_entries,
                                    gimp_clip->n_buffer_target_entries,
651 652 653 654 655
                                    (GtkClipboardGetFunc) gimp_clipboard_send_buffer,
                                    (GtkClipboardClearFunc) NULL,
                                    G_OBJECT (gimp));

      /*  mark the first entry (image/png) as suitable for storing  */
656
      if (gimp_clip->n_buffer_target_entries > 0)
657 658
        gtk_clipboard_set_can_store (clipboard,
                                     gimp_clip->buffer_target_entries, 1);
659 660 661 662 663
    }
  else if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (gimp))
    {
      gtk_clipboard_clear (clipboard);
    }
664 665
}

666 667 668
/**
 * gimp_clipboard_set_svg:
 * @gimp: pointer to #Gimp
669
 * @svg: a string containing the SVG data, or %NULL
670 671 672 673
 *
 * Offers SVG data in %GDK_SELECTION_CLIPBOARD.
 **/
void
674 675
gimp_clipboard_set_svg (Gimp        *gimp,
                        const gchar *svg)
676
{
677
  GimpClipboard *gimp_clip;
678 679 680 681 682 683 684 685 686
  GtkClipboard  *clipboard;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

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

687 688
  gimp_clip = gimp_clipboard_get (gimp);

689
  gimp_clipboard_clear (gimp_clip);
690 691 692 693 694 695 696 697 698 699 700 701 702

  if (svg)
    {
      gimp_clip->svg = g_strdup (svg);

      gtk_clipboard_set_with_owner (clipboard,
                                    gimp_clip->svg_target_entries,
                                    gimp_clip->n_svg_target_entries,
                                    (GtkClipboardGetFunc) gimp_clipboard_send_svg,
                                    (GtkClipboardClearFunc) NULL,
                                    G_OBJECT (gimp));

      /*  mark the first entry (image/svg) as suitable for storing  */
703 704
      gtk_clipboard_set_can_store (clipboard,
                                   gimp_clip->svg_target_entries, 1);
705 706 707 708 709 710 711
    }
  else if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (gimp))
    {
      gtk_clipboard_clear (clipboard);
    }
}

712 713
/**
 * gimp_clipboard_set_text:
714
 * @gimp: pointer to #Gimp
715 716
 * @text: a %NULL-terminated string in UTF-8 encoding
 *
717
 * Offers @text in %GDK_SELECTION_CLIPBOARD and %GDK_SELECTION_PRIMARY.
718 719
 **/
void
720 721
gimp_clipboard_set_text (Gimp        *gimp,
                         const gchar *text)
722 723 724
{
  GtkClipboard *clipboard;

725
  g_return_if_fail (GIMP_IS_GIMP (gimp));
726 727
  g_return_if_fail (text != NULL);

728 729
  gimp_clipboard_clear (gimp_clipboard_get (gimp));

730 731
  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);
732 733
  if (clipboard)
    gtk_clipboard_set_text (clipboard, text, -1);
734 735 736

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_PRIMARY);
737 738
  if (clipboard)
    gtk_clipboard_set_text (clipboard, text, -1);
739 740
}

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
/**
 * gimp_clipboard_set_curve:
 * @gimp: pointer to #Gimp
 * @curve: a #GimpCurve, or %NULL
 *
 * Offers curve data in %GDK_SELECTION_CLIPBOARD.
 **/
void
gimp_clipboard_set_curve (Gimp      *gimp,
                          GimpCurve *curve)
{
  GimpClipboard *gimp_clip;
  GtkClipboard  *clipboard;

  g_return_if_fail (GIMP_IS_GIMP (gimp));
  g_return_if_fail (curve == NULL || GIMP_IS_CURVE (curve));

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

  gimp_clip = gimp_clipboard_get (gimp);

  gimp_clipboard_clear (gimp_clip);

  if (curve)
    {
      gimp_clip->curve = g_object_ref (curve);

      gtk_clipboard_set_with_owner (clipboard,
                                    gimp_clip->curve_target_entries,
                                    gimp_clip->n_curve_target_entries,
                                    (GtkClipboardGetFunc) gimp_clipboard_send_curve,
                                    (GtkClipboardClearFunc) NULL,
                                    G_OBJECT (gimp));

778 779
      gtk_clipboard_set_can_store (clipboard,
                                   gimp_clip->curve_target_entries, 1);
780 781 782 783 784 785 786
    }
  else if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (gimp))
    {
      gtk_clipboard_clear (clipboard);
    }
}

787 788 789 790 791 792 793 794 795

/*  private functions  */

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

796 797
static GimpClipboard *
gimp_clipboard_new (gboolean verbose)
798
{
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
  GimpClipboard *gimp_clip = g_slice_new0 (GimpClipboard);
  GSList        *list;

  gimp_clip->pixbuf_formats = gimp_pixbuf_get_formats ();

  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_buffer_target_entries++;

          g_strfreev (mime_types);
        }
    }

  /*  the image_target_entries have the XCF target, and all pixbuf
   *  targets that are also in buffer_target_entries
   */
  gimp_clip->n_image_target_entries = gimp_clip->n_buffer_target_entries + 1;
  gimp_clip->image_target_entries   = g_new0 (GtkTargetEntry,
                                              gimp_clip->n_image_target_entries);

  gimp_clip->image_target_entries[0].target = g_strdup ("image/x-xcf");
  gimp_clip->image_target_entries[0].flags  = 0;
  gimp_clip->image_target_entries[0].info   = 0;

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

      gimp_clip->buffer_target_entries = g_new0 (GtkTargetEntry,
                                                 gimp_clip->n_buffer_target_entries);

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

          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++)
                {
                  const gchar *mime_type = *type;

                  if (verbose)
                    g_printerr ("clipboard: writable pixbuf format: %s\n",
                                mime_type);

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

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

                  i++;
                }

              g_strfreev (mime_types);
              g_free (format_name);
            }
        }
    }

  gimp_clip->n_svg_target_entries = 2;
  gimp_clip->svg_target_entries   = g_new0 (GtkTargetEntry, 2);

  gimp_clip->svg_target_entries[0].target = g_strdup ("image/svg");
  gimp_clip->svg_target_entries[0].flags  = 0;
  gimp_clip->svg_target_entries[0].info   = 0;

  gimp_clip->svg_target_entries[1].target = g_strdup ("image/svg+xml");
  gimp_clip->svg_target_entries[1].flags  = 0;
  gimp_clip->svg_target_entries[1].info   = 1;

  gimp_clip->n_curve_target_entries = 1;
  gimp_clip->curve_target_entries   = g_new0 (GtkTargetEntry, 1);

  gimp_clip->curve_target_entries[0].target = g_strdup ("application/x-gimp-curve");
  gimp_clip->curve_target_entries[0].flags  = 0;
  gimp_clip->curve_target_entries[0].info   = 0;

  return gimp_clip;
897 898
}

899 900 901
static void
gimp_clipboard_free (GimpClipboard *gimp_clip)
{
902 903
  gint i;

904 905
  gimp_clipboard_clear (gimp_clip);

906
  g_slist_free (gimp_clip->pixbuf_formats);
907

908 909 910 911 912 913 914
  for (i = 0; i < gimp_clip->n_image_target_entries; i++)
    g_free ((gchar *) gimp_clip->image_target_entries[i].target);

  g_free (gimp_clip->image_target_entries);

  for (i = 0; i < gimp_clip->n_buffer_target_entries; i++)
    g_free ((gchar *) gimp_clip->buffer_target_entries[i].target);
915

916
  g_free (gimp_clip->buffer_target_entries);
917 918

  for (i = 0; i < gimp_clip->n_svg_target_entries; i++)
919
    g_free ((gchar *) gimp_clip->svg_target_entries[i].target);
920 921 922

  g_free (gimp_clip->svg_target_entries);

923
  for (i = 0; i < gimp_clip->n_curve_target_entries; i++)
924
    g_free ((gchar *) gimp_clip->curve_target_entries[i].target);
925 926 927

  g_free (gimp_clip->curve_target_entries);

928
  g_slice_free (GimpClipboard, gimp_clip);
929 930
}

931 932 933 934 935 936 937 938 939
static void
gimp_clipboard_clear (GimpClipboard *gimp_clip)
{
  g_clear_object (&gimp_clip->image);
  g_clear_object (&gimp_clip->buffer);
  g_clear_pointer (&gimp_clip->svg, g_free);
  g_clear_object (&gimp_clip->curve);
}

940
static GdkAtom *
941 942
gimp_clipboard_wait_for_targets (Gimp *gimp,
                                 gint *n_targets)
943 944 945 946 947 948 949 950 951
{
  GtkClipboard *clipboard;

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);

  if (clipboard)
    {
      GtkSelectionData *data;
952 953 954
      GdkAtom           atom = gdk_atom_intern_static_string ("TARGETS");

      data = gtk_clipboard_wait_for_contents (clipboard, atom);
955 956 957

      if (data)
        {
958 959 960 961 962 963 964 965 966
          GdkAtom  *targets;
          gboolean  success;

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

          gtk_selection_data_free (data);

          if (success)
            {
967 968 969
              if (gimp->be_verbose)
                {
                  gint i;
970

971 972 973
                  for (i = 0; i < *n_targets; i++)
                    g_printerr ("clipboard: offered type: %s\n",
                                gdk_atom_name (targets[i]));
974

975 976
                  g_printerr ("\n");
                }
977 978 979 980 981 982 983 984 985

              return targets;
            }
        }
    }

  return NULL;
}

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
static GdkAtom
gimp_clipboard_wait_for_image (Gimp *gimp)
{
  GdkAtom *targets;
  gint     n_targets;
  GdkAtom  result = GDK_NONE;

  targets = gimp_clipboard_wait_for_targets (gimp, &n_targets);

  if (targets)
    {
      GdkAtom image_atom = gdk_atom_intern_static_string ("image/x-xcf");
      gint    i;

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

      g_free (targets);
    }

  return result;
}

1015 1016 1017
static GdkAtom
gimp_clipboard_wait_for_buffer (Gimp *gimp)
{
1018 1019 1020 1021
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
  GdkAtom       *targets;
  gint           n_targets;
  GdkAtom        result    = GDK_NONE;
1022

1023
  targets = gimp_clipboard_wait_for_targets (gimp, &n_targets);
1024 1025 1026 1027 1028

  if (targets)
    {
      GSList *list;

1029
      for (list = gimp_clip->pixbuf_formats; list; list = g_slist_next (list))
1030 1031 1032 1033 1034
        {
          GdkPixbufFormat  *format = list->data;
          gchar           **mime_types;
          gchar           **type;

1035 1036 1037
          if (gimp->be_verbose)
            g_printerr ("clipboard: checking pixbuf format '%s'\n",
                        gdk_pixbuf_format_get_name (format));
1038

1039 1040 1041
          mime_types = gdk_pixbuf_format_get_mime_types (format);

          for (type = mime_types; *type; type++)
1042
            {
1043 1044
              gchar   *mime_type = *type;
              GdkAtom  atom      = gdk_atom_intern (mime_type, FALSE);
1045 1046
              gint     i;

1047 1048
              if (gimp->be_verbose)
                g_printerr ("  - checking mime type '%s'\n", mime_type);
1049

1050
              for (i = 0; i < n_targets; i++)
1051 1052
                {
                  if (targets[i] == atom)
1053 1054 1055 1056
                    {
                      result = atom;
                      break;
                    }
1057
                }
1058

1059 1060
              if (result != GDK_NONE)
                break;
1061 1062
            }

1063 1064 1065 1066
          g_strfreev (mime_types);

          if (result != GDK_NONE)
            break;
1067
        }
1068 1069

      g_free (targets);
1070 1071 1072 1073 1074
    }

  return result;
}

1075 1076 1077 1078 1079 1080 1081
static GdkAtom
gimp_clipboard_wait_for_svg (Gimp *gimp)
{
  GdkAtom *targets;
  gint     n_targets;
  GdkAtom  result = GDK_NONE;

1082
  targets = gimp_clipboard_wait_for_targets (gimp, &n_targets);
1083 1084 1085

  if (targets)
    {
1086 1087
      GdkAtom svg_atom     = gdk_atom_intern_static_string ("image/svg");
      GdkAtom svg_xml_atom = gdk_atom_intern_static_string ("image/svg+xml");
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
      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;
}

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
static GdkAtom
gimp_clipboard_wait_for_curve (Gimp *gimp)
{
  GdkAtom *targets;
  gint     n_targets;
  GdkAtom  result = GDK_NONE;

  targets = gimp_clipboard_wait_for_targets (gimp, &n_targets);

  if (targets)
    {
      GdkAtom curve_atom = gdk_atom_intern_static_string ("application/x-gimp-curve");
      gint    i;

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

      g_free (targets);
    }

  return result;
}

1139 1140
static void
gimp_clipboard_send_image (GtkClipboard     *clipboard,
1141
                           GtkSelectionData *data,
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
                           guint             info,
                           Gimp             *gimp)
{
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);

  gimp_set_busy (gimp);

  if (info == 0)
    {
      if (gimp->be_verbose)
        g_printerr ("clipboard: sending image data as '%s'\n",
                    gimp_clip->image_target_entries[info].target);

1155
      gimp_selection_data_set_xcf (data, gimp_clip->image);
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
    }
  else
    {
      GdkPixbuf *pixbuf;

      gimp_pickable_flush (GIMP_PICKABLE (gimp_clip->image));

      pixbuf = gimp_viewable_get_pixbuf (GIMP_VIEWABLE (gimp_clip->image),
                                         gimp_get_user_context (gimp),
                                         gimp_image_get_width (gimp_clip->image),
                                         gimp_image_get_height (gimp_clip->image));

      if (pixbuf)
        {
          if (gimp->be_verbose)
            g_printerr ("clipboard: sending image data as '%s'\n",
                        gimp_clip->image_target_entries[info].target);

1174
          gtk_selection_data_set_pixbuf (data, pixbuf);
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
        }
      else
        {
          g_warning ("%s: gimp_viewable_get_pixbuf() failed", G_STRFUNC);
        }
    }

  gimp_unset_busy (gimp);
}

1185
static void
1186
gimp_clipboard_send_buffer (GtkClipboard     *clipboard,
1187
                            GtkSelectionData *data,
1188 1189
                            guint             info,
                            Gimp             *gimp)
1190
{
1191 1192
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
  GdkPixbuf     *pixbuf;
1193 1194 1195

  gimp_set_busy (gimp);

1196
  pixbuf = gimp_viewable_get_pixbuf (GIMP_VIEWABLE (gimp_clip->buffer),
1197
                                     gimp_get_user_context (gimp),
1198 1199
                                     gimp_buffer_get_width (gimp_clip->buffer),
                                     gimp_buffer_get_height (gimp_clip->buffer));
1200 1201 1202

  if (pixbuf)
    {
1203 1204
      if (gimp->be_verbose)
        g_printerr ("clipboard: sending pixbuf data as '%s'\n",
1205
                    gimp_clip->buffer_target_entries[info].target);
1206

1207
      gtk_selection_data_set_pixbuf (data, pixbuf);
1208 1209 1210 1211 1212 1213 1214 1215
    }
  else
    {
      g_warning ("%s: gimp_viewable_get_pixbuf() failed", G_STRFUNC);
    }

  gimp_unset_busy (gimp);
}
1216 1217 1218

static void
gimp_clipboard_send_svg (GtkClipboard     *clipboard,
1219
                         GtkSelectionData *data,
1220 1221 1222 1223 1224 1225 1226 1227 1228
                         guint             info,
                         Gimp             *gimp)
{
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);

  gimp_set_busy (gimp);

  if (gimp_clip->svg)
    {
1229 1230 1231
      if (gimp->be_verbose)
        g_printerr ("clipboard: sending SVG data as '%s'\n",
                    gimp_clip->svg_target_entries[info].target);
1232

1233
      gimp_selection_data_set_stream (data,
1234
                                      (const guchar *) gimp_clip->svg,
1235 1236 1237 1238 1239
                                      strlen (gimp_clip->svg));
    }

  gimp_unset_busy (gimp);
}
1240 1241 1242

static void
gimp_clipboard_send_curve (GtkClipboard     *clipboard,
1243
                           GtkSelectionData *data,
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
                           guint             info,
                           Gimp             *gimp)
{
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);

  gimp_set_busy (gimp);

  if (gimp_clip->curve)
    {
      if (gimp->be_verbose)
        g_printerr ("clipboard: sending curve data as '%s'\n",
                    gimp_clip->curve_target_entries[info].target);

1257
      gimp_selection_data_set_curve (data, gimp_clip->curve);
1258 1259 1260 1261
    }

  gimp_unset_busy (gimp);
}