File: gimpclipboard.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (521 lines) | stat: -rw-r--r-- 14,420 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
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
517
518
519
520
521
/* 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"

#include <string.h>

#include <gtk/gtk.h>

#include "widgets-types.h"

#include "base/pixel-region.h"
#include "base/tile-manager.h"

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

#include "gimpclipboard.h"
#include "gimpdnd.h"
#include "gimpselectiondata.h"

#include "gimp-intl.h"


#define GIMP_CLIPBOARD_KEY "gimp-clipboard"


typedef struct _GimpClipboard GimpClipboard;

struct _GimpClipboard
{
  GSList          *pixbuf_formats;

  GtkTargetEntry  *target_entries;
  gint             n_target_entries;
  gchar          **savers;
};


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

static void      gimp_clipboard_buffer_changed   (Gimp             *gimp);
static void      gimp_clipboard_set_buffer       (Gimp             *gimp,
                                                  GimpBuffer       *buffer);

static void      gimp_clipboard_send_buffer      (GtkClipboard     *clipboard,
                                                  GtkSelectionData *selection_data,
                                                  guint             info,
                                                  Gimp             *gimp);

static GdkAtom * gimp_clipboard_wait_for_targets (gint             *n_targets);
static GdkAtom   gimp_clipboard_wait_for_buffer  (Gimp             *gimp);

static gint      gimp_clipboard_format_compare   (GdkPixbufFormat  *a,
                                                  GdkPixbufFormat  *b);


void
gimp_clipboard_init (Gimp *gimp)
{
  GimpClipboard *gimp_clip;
  GSList        *list;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  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);

  g_signal_connect_object (gimp, "buffer_changed",
                           G_CALLBACK (gimp_clipboard_buffer_changed),
                           NULL, 0);

  gimp_clip->pixbuf_formats =
    g_slist_sort (gdk_pixbuf_get_formats (),
                  (GCompareFunc) gimp_clipboard_format_compare);

  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);
      gimp_clip->savers         = g_new0 (gchar *,
                                          gimp_clip->n_target_entries + 1);

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

                  if (gimp->be_verbose)
                    g_print ("GimpClipboard: writable pixbuf format: %s\n",
                             mime_type);

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

                  gimp_clip->savers[i]                = g_strdup (format_name);

                  i++;
                }

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

void
gimp_clipboard_exit (Gimp *gimp)
{
  g_return_if_fail (GIMP_IS_GIMP (gimp));

  g_signal_handlers_disconnect_by_func (gimp,
                                        G_CALLBACK (gimp_clipboard_buffer_changed),
                                        NULL);
  gimp_clipboard_set_buffer (gimp, NULL);

  g_object_set_data (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY, NULL);
}

/**
 * gimp_clipboard_has_buffer:
 * @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
gimp_clipboard_has_buffer (Gimp *gimp)
{
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

  if (gimp->global_buffer)
    return TRUE;

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


static TileManager *
tile_manager_new_from_pixbuf (GdkPixbuf *pixbuf)
{
  TileManager *tiles;
  guchar      *pixels;
  PixelRegion  destPR;
  gint         width;
  gint         height;
  gint         rowstride;
  gint         channels;
  gint         y;

  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

  width     = gdk_pixbuf_get_width (pixbuf);
  height    = gdk_pixbuf_get_height (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  channels  = gdk_pixbuf_get_n_channels (pixbuf);

  tiles = tile_manager_new (width, height, channels);

  pixel_region_init (&destPR, tiles, 0, 0, width, height, TRUE);

  for (y = 0, pixels = gdk_pixbuf_get_pixels (pixbuf);
       y < height;
       y++, pixels += rowstride)
    {
      pixel_region_set_row (&destPR, 0, y, width, pixels);
   }

  return tiles;
}

/**
 * gimp_clipboard_get_buffer:
 * @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 *
gimp_clipboard_get_buffer (Gimp *gimp)
{
  GimpBuffer   *buffer = NULL;
  GtkClipboard *clipboard;
  GdkAtom       atom;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);

  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_buffer (gimp)) != GDK_NONE)
    {
      GtkSelectionData *data;

      gimp_set_busy (gimp);

      data = gtk_clipboard_wait_for_contents (clipboard, atom);

      if (data)
        {
          GdkPixbuf *pixbuf = gimp_selection_data_get_pixbuf (data);

          gtk_selection_data_free (data);

          if (pixbuf)
            {
              TileManager *tiles = tile_manager_new_from_pixbuf (pixbuf);

              g_object_unref (pixbuf);

              buffer = gimp_buffer_new (tiles, _("Clipboard"), FALSE);
            }
        }

      gimp_unset_busy (gimp);
    }

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

  return buffer;
}


/*  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)
{
  g_slist_free (gimp_clip->pixbuf_formats);
  g_free (gimp_clip->target_entries);
  g_strfreev (gimp_clip->savers);
  g_free (gimp_clip);
}

static void
gimp_clipboard_buffer_changed (Gimp *gimp)
{
  gimp_clipboard_set_buffer (gimp, gimp->global_buffer);
}

static void
gimp_clipboard_set_buffer (Gimp       *gimp,
                           GimpBuffer *buffer)
{
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
  GtkClipboard  *clipboard;

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

  if (buffer)
    {
      gtk_clipboard_set_with_owner (clipboard,
                                    gimp_clip->target_entries,
                                    gimp_clip->n_target_entries,
                                    (GtkClipboardGetFunc)   gimp_clipboard_send_buffer,
                                    (GtkClipboardClearFunc) NULL,
                                    G_OBJECT (gimp));
    }
  else if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (gimp))
    {
      gtk_clipboard_clear (clipboard);
    }
}

static GdkAtom *
gimp_clipboard_wait_for_targets (gint *n_targets)
{
  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)
        {
          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++)
                g_print ("offered type: %s\n", gdk_atom_name (targets[i]));

              g_print ("\n");

              return targets;
            }
        }
    }

  return NULL;
}

static GdkAtom
gimp_clipboard_wait_for_buffer (Gimp *gimp)
{
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
  GdkAtom       *targets;
  gint           n_targets;
  GdkAtom        result    = GDK_NONE;

  targets = gimp_clipboard_wait_for_targets (&n_targets);

  if (targets)
    {
      GSList *list;

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

          g_print ("checking pixbuf format '%s'\n",
                   gdk_pixbuf_format_get_name (format));

          mime_types = gdk_pixbuf_format_get_mime_types (format);

          for (type = mime_types; *type; type++)
            {
              gchar   *mime_type = *type;
              GdkAtom  atom      = gdk_atom_intern (mime_type, FALSE);
              gint     i;

              g_print (" - checking mime type '%s'\n", mime_type);

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

              if (result != GDK_NONE)
                break;
            }

          g_strfreev (mime_types);

          if (result != GDK_NONE)
            break;
        }

      g_free (targets);
    }

  return result;
}

static void
gimp_clipboard_send_buffer (GtkClipboard     *clipboard,
                            GtkSelectionData *selection_data,
                            guint             info,
                            Gimp             *gimp)
{
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
  GimpBuffer    *buffer    = gimp->global_buffer;
  GdkPixbuf     *pixbuf;

  gimp_set_busy (gimp);

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

  if (pixbuf)
    {
      GdkAtom atom = gdk_atom_intern (gimp_clip->target_entries[info].target,
                                      FALSE);

      g_print ("sending pixbuf data as '%s' (%s)\n",
               gimp_clip->target_entries[info].target,
               gimp_clip->savers[info]);

      gimp_selection_data_set_pixbuf (selection_data, atom, pixbuf,
                                      gimp_clip->savers[info]);
    }
  else
    {
      g_warning ("%s: gimp_viewable_get_pixbuf() failed", G_STRFUNC);
    }

  gimp_unset_busy (gimp);
}

static gint
gimp_clipboard_format_compare (GdkPixbufFormat *a,
                               GdkPixbufFormat *b)
{
  gchar *a_name = gdk_pixbuf_format_get_name (a);
  gchar *b_name = gdk_pixbuf_format_get_name (b);
  gint   retval = 0;

#ifdef GDK_WINDOWING_WIN32
  /*  move BMP to the front of the list  */
  if (strcmp (a_name, "bmp") == 0)
    retval = -1;
  else if (strcmp (b_name, "bmp") == 0)
    retval = 1;
  else
#endif

  /*  move PNG to the front of the list  */
  if (strcmp (a_name, "png") == 0)
    retval = -1;
  else if (strcmp (b_name, "png") == 0)
    retval = 1;

  /*  move JPEG to the end of the list  */
  else if (strcmp (a_name, "jpeg") == 0)
    retval = 1;
  else if (strcmp (b_name, "jpeg") == 0)
    retval = -1;

  /*  move GIF to the end of the list  */
  else if (strcmp (a_name, "gif") == 0)
    retval = 1;
  else if (strcmp (b_name, "gif") == 0)
    retval = -1;

  g_free (a_name);
  g_free (b_name);

  return retval;
}