1
/* The GIMP -- an image manipulation program
2
* 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 2 of the License, or
7
* (at your option) any later version.
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.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
#include "widgets-types.h"
27
#include "base/pixel-region.h"
28
#include "base/tile-manager.h"
30
#include "core/gimp.h"
31
#include "core/gimpbuffer.h"
32
#include "core/gimpviewable.h"
34
#include "gimpclipboard.h"
36
#include "gimpselectiondata.h"
38
#include "gimp-intl.h"
41
#define GIMP_CLIPBOARD_KEY "gimp-clipboard"
44
typedef struct _GimpClipboard GimpClipboard;
48
GSList *pixbuf_formats;
50
GtkTargetEntry *target_entries;
51
gint n_target_entries;
56
static GimpClipboard * gimp_clipboard_get (Gimp *gimp);
57
static void gimp_clipboard_free (GimpClipboard *gimp_clip);
59
static void gimp_clipboard_buffer_changed (Gimp *gimp);
60
static void gimp_clipboard_set_buffer (Gimp *gimp,
63
static void gimp_clipboard_send_buffer (GtkClipboard *clipboard,
64
GtkSelectionData *selection_data,
68
static GdkAtom * gimp_clipboard_wait_for_targets (gint *n_targets);
69
static GdkAtom gimp_clipboard_wait_for_buffer (Gimp *gimp);
71
static gint gimp_clipboard_format_compare (GdkPixbufFormat *a,
76
gimp_clipboard_init (Gimp *gimp)
78
GimpClipboard *gimp_clip;
81
g_return_if_fail (GIMP_IS_GIMP (gimp));
83
gimp_clip = gimp_clipboard_get (gimp);
85
g_return_if_fail (gimp_clip == NULL);
87
gimp_clip = g_new0 (GimpClipboard, 1);
89
g_object_set_data_full (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY,
90
gimp_clip, (GDestroyNotify) gimp_clipboard_free);
92
gimp_clipboard_set_buffer (gimp, gimp->global_buffer);
94
g_signal_connect_object (gimp, "buffer_changed",
95
G_CALLBACK (gimp_clipboard_buffer_changed),
98
gimp_clip->pixbuf_formats =
99
g_slist_sort (gdk_pixbuf_get_formats (),
100
(GCompareFunc) gimp_clipboard_format_compare);
102
for (list = gimp_clip->pixbuf_formats; list; list = g_slist_next (list))
104
GdkPixbufFormat *format = list->data;
106
if (gdk_pixbuf_format_is_writable (format))
111
mime_types = gdk_pixbuf_format_get_mime_types (format);
113
for (type = mime_types; *type; type++)
114
gimp_clip->n_target_entries++;
116
g_strfreev (mime_types);
120
if (gimp_clip->n_target_entries > 0)
124
gimp_clip->target_entries = g_new0 (GtkTargetEntry,
125
gimp_clip->n_target_entries);
126
gimp_clip->savers = g_new0 (gchar *,
127
gimp_clip->n_target_entries + 1);
129
for (list = gimp_clip->pixbuf_formats; list; list = g_slist_next (list))
131
GdkPixbufFormat *format = list->data;
133
if (gdk_pixbuf_format_is_writable (format))
139
format_name = gdk_pixbuf_format_get_name (format);
140
mime_types = gdk_pixbuf_format_get_mime_types (format);
142
for (type = mime_types; *type; type++)
144
gchar *mime_type = *type;
146
if (gimp->be_verbose)
147
g_print ("GimpClipboard: writable pixbuf format: %s\n",
150
gimp_clip->target_entries[i].target = g_strdup (mime_type);
151
gimp_clip->target_entries[i].flags = 0;
152
gimp_clip->target_entries[i].info = i;
154
gimp_clip->savers[i] = g_strdup (format_name);
159
g_strfreev (mime_types);
160
g_free (format_name);
167
gimp_clipboard_exit (Gimp *gimp)
169
g_return_if_fail (GIMP_IS_GIMP (gimp));
171
g_signal_handlers_disconnect_by_func (gimp,
172
G_CALLBACK (gimp_clipboard_buffer_changed),
174
gimp_clipboard_set_buffer (gimp, NULL);
176
g_object_set_data (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY, NULL);
180
* gimp_clipboard_has_buffer:
181
* @gimp: pointer to #Gimp
183
* Tests if there's image data in the clipboard. If the global cut
184
* buffer of @gimp is empty, this function checks if there's image
185
* data in %GDK_SELECTION_CLIPBOARD. This is done in a main-loop
186
* similar to gtk_clipboard_wait_is_text_available(). The same caveats
189
* Return value: %TRUE if there's image data in the clipboard, %FALSE otherwise
192
gimp_clipboard_has_buffer (Gimp *gimp)
194
g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
196
if (gimp->global_buffer)
199
return (gimp_clipboard_wait_for_buffer (gimp) != GDK_NONE);
204
tile_manager_new_from_pixbuf (GdkPixbuf *pixbuf)
215
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
217
width = gdk_pixbuf_get_width (pixbuf);
218
height = gdk_pixbuf_get_height (pixbuf);
219
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
220
channels = gdk_pixbuf_get_n_channels (pixbuf);
222
tiles = tile_manager_new (width, height, channels);
224
pixel_region_init (&destPR, tiles, 0, 0, width, height, TRUE);
226
for (y = 0, pixels = gdk_pixbuf_get_pixels (pixbuf);
228
y++, pixels += rowstride)
230
pixel_region_set_row (&destPR, 0, y, width, pixels);
237
* gimp_clipboard_get_buffer:
238
* @gimp: pointer to #Gimp
240
* Retrieves either image data from %GDK_SELECTION_CLIPBOARD or from
241
* the global cut buffer of @gimp.
243
* The returned #GimpBuffer needs to be unref'ed when it's no longer
246
* Return value: a reference to a #GimpBuffer or %NULL if there's no
250
gimp_clipboard_get_buffer (Gimp *gimp)
252
GimpBuffer *buffer = NULL;
253
GtkClipboard *clipboard;
256
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
258
clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
259
GDK_SELECTION_CLIPBOARD);
262
gtk_clipboard_get_owner (clipboard) != G_OBJECT (gimp) &&
263
(atom = gimp_clipboard_wait_for_buffer (gimp)) != GDK_NONE)
265
GtkSelectionData *data;
267
gimp_set_busy (gimp);
269
data = gtk_clipboard_wait_for_contents (clipboard, atom);
273
GdkPixbuf *pixbuf = gimp_selection_data_get_pixbuf (data);
275
gtk_selection_data_free (data);
279
TileManager *tiles = tile_manager_new_from_pixbuf (pixbuf);
281
g_object_unref (pixbuf);
283
buffer = gimp_buffer_new (tiles, _("Clipboard"), FALSE);
287
gimp_unset_busy (gimp);
290
if (! buffer && gimp->global_buffer)
291
buffer = g_object_ref (gimp->global_buffer);
297
/* private functions */
299
static GimpClipboard *
300
gimp_clipboard_get (Gimp *gimp)
302
return g_object_get_data (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY);
306
gimp_clipboard_free (GimpClipboard *gimp_clip)
308
g_slist_free (gimp_clip->pixbuf_formats);
309
g_free (gimp_clip->target_entries);
310
g_strfreev (gimp_clip->savers);
315
gimp_clipboard_buffer_changed (Gimp *gimp)
317
gimp_clipboard_set_buffer (gimp, gimp->global_buffer);
321
gimp_clipboard_set_buffer (Gimp *gimp,
324
GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
325
GtkClipboard *clipboard;
327
clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
328
GDK_SELECTION_CLIPBOARD);
334
gtk_clipboard_set_with_owner (clipboard,
335
gimp_clip->target_entries,
336
gimp_clip->n_target_entries,
337
(GtkClipboardGetFunc) gimp_clipboard_send_buffer,
338
(GtkClipboardClearFunc) NULL,
341
else if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (gimp))
343
gtk_clipboard_clear (clipboard);
348
gimp_clipboard_wait_for_targets (gint *n_targets)
350
GtkClipboard *clipboard;
352
clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
353
GDK_SELECTION_CLIPBOARD);
357
GtkSelectionData *data;
359
data = gtk_clipboard_wait_for_contents (clipboard,
360
gdk_atom_intern ("TARGETS",
367
success = gtk_selection_data_get_targets (data, &targets, n_targets);
369
gtk_selection_data_free (data);
375
for (i = 0; i < *n_targets; i++)
376
g_print ("offered type: %s\n", gdk_atom_name (targets[i]));
389
gimp_clipboard_wait_for_buffer (Gimp *gimp)
391
GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
394
GdkAtom result = GDK_NONE;
396
targets = gimp_clipboard_wait_for_targets (&n_targets);
402
for (list = gimp_clip->pixbuf_formats; list; list = g_slist_next (list))
404
GdkPixbufFormat *format = list->data;
408
g_print ("checking pixbuf format '%s'\n",
409
gdk_pixbuf_format_get_name (format));
411
mime_types = gdk_pixbuf_format_get_mime_types (format);
413
for (type = mime_types; *type; type++)
415
gchar *mime_type = *type;
416
GdkAtom atom = gdk_atom_intern (mime_type, FALSE);
419
g_print (" - checking mime type '%s'\n", mime_type);
421
for (i = 0; i < n_targets; i++)
423
if (targets[i] == atom)
430
if (result != GDK_NONE)
434
g_strfreev (mime_types);
436
if (result != GDK_NONE)
447
gimp_clipboard_send_buffer (GtkClipboard *clipboard,
448
GtkSelectionData *selection_data,
452
GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
453
GimpBuffer *buffer = gimp->global_buffer;
456
gimp_set_busy (gimp);
458
pixbuf = gimp_viewable_get_pixbuf (GIMP_VIEWABLE (buffer),
459
gimp_buffer_get_width (buffer),
460
gimp_buffer_get_height (buffer));
464
GdkAtom atom = gdk_atom_intern (gimp_clip->target_entries[info].target,
467
g_print ("sending pixbuf data as '%s' (%s)\n",
468
gimp_clip->target_entries[info].target,
469
gimp_clip->savers[info]);
471
gimp_selection_data_set_pixbuf (selection_data, atom, pixbuf,
472
gimp_clip->savers[info]);
476
g_warning ("%s: gimp_viewable_get_pixbuf() failed", G_STRFUNC);
479
gimp_unset_busy (gimp);
483
gimp_clipboard_format_compare (GdkPixbufFormat *a,
486
gchar *a_name = gdk_pixbuf_format_get_name (a);
487
gchar *b_name = gdk_pixbuf_format_get_name (b);
490
#ifdef GDK_WINDOWING_WIN32
491
/* move BMP to the front of the list */
492
if (strcmp (a_name, "bmp") == 0)
494
else if (strcmp (b_name, "bmp") == 0)
499
/* move PNG to the front of the list */
500
if (strcmp (a_name, "png") == 0)
502
else if (strcmp (b_name, "png") == 0)
505
/* move JPEG to the end of the list */
506
else if (strcmp (a_name, "jpeg") == 0)
508
else if (strcmp (b_name, "jpeg") == 0)
511
/* move GIF to the end of the list */
512
else if (strcmp (a_name, "gif") == 0)
514
else if (strcmp (b_name, "gif") == 0)