~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to app/widgets/gimpclipboard.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <string.h>
 
22
 
 
23
#include <gtk/gtk.h>
 
24
 
 
25
#include "widgets-types.h"
 
26
 
 
27
#include "base/pixel-region.h"
 
28
#include "base/tile-manager.h"
 
29
 
 
30
#include "core/gimp.h"
 
31
#include "core/gimpbuffer.h"
 
32
#include "core/gimpviewable.h"
 
33
 
 
34
#include "gimpclipboard.h"
 
35
#include "gimpdnd.h"
 
36
#include "gimpselectiondata.h"
 
37
 
 
38
#include "gimp-intl.h"
 
39
 
 
40
 
 
41
#define GIMP_CLIPBOARD_KEY "gimp-clipboard"
 
42
 
 
43
 
 
44
typedef struct _GimpClipboard GimpClipboard;
 
45
 
 
46
struct _GimpClipboard
 
47
{
 
48
  GSList          *pixbuf_formats;
 
49
 
 
50
  GtkTargetEntry  *target_entries;
 
51
  gint             n_target_entries;
 
52
  gchar          **savers;
 
53
};
 
54
 
 
55
 
 
56
static GimpClipboard * gimp_clipboard_get        (Gimp             *gimp);
 
57
static void            gimp_clipboard_free       (GimpClipboard    *gimp_clip);
 
58
 
 
59
static void      gimp_clipboard_buffer_changed   (Gimp             *gimp);
 
60
static void      gimp_clipboard_set_buffer       (Gimp             *gimp,
 
61
                                                  GimpBuffer       *buffer);
 
62
 
 
63
static void      gimp_clipboard_send_buffer      (GtkClipboard     *clipboard,
 
64
                                                  GtkSelectionData *selection_data,
 
65
                                                  guint             info,
 
66
                                                  Gimp             *gimp);
 
67
 
 
68
static GdkAtom * gimp_clipboard_wait_for_targets (gint             *n_targets);
 
69
static GdkAtom   gimp_clipboard_wait_for_buffer  (Gimp             *gimp);
 
70
 
 
71
static gint      gimp_clipboard_format_compare   (GdkPixbufFormat  *a,
 
72
                                                  GdkPixbufFormat  *b);
 
73
 
 
74
 
 
75
void
 
76
gimp_clipboard_init (Gimp *gimp)
 
77
{
 
78
  GimpClipboard *gimp_clip;
 
79
  GSList        *list;
 
80
 
 
81
  g_return_if_fail (GIMP_IS_GIMP (gimp));
 
82
 
 
83
  gimp_clip = gimp_clipboard_get (gimp);
 
84
 
 
85
  g_return_if_fail (gimp_clip == NULL);
 
86
 
 
87
  gimp_clip = g_new0 (GimpClipboard, 1);
 
88
 
 
89
  g_object_set_data_full (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY,
 
90
                          gimp_clip, (GDestroyNotify) gimp_clipboard_free);
 
91
 
 
92
  gimp_clipboard_set_buffer (gimp, gimp->global_buffer);
 
93
 
 
94
  g_signal_connect_object (gimp, "buffer_changed",
 
95
                           G_CALLBACK (gimp_clipboard_buffer_changed),
 
96
                           NULL, 0);
 
97
 
 
98
  gimp_clip->pixbuf_formats =
 
99
    g_slist_sort (gdk_pixbuf_get_formats (),
 
100
                  (GCompareFunc) gimp_clipboard_format_compare);
 
101
 
 
102
  for (list = gimp_clip->pixbuf_formats; list; list = g_slist_next (list))
 
103
    {
 
104
      GdkPixbufFormat *format = list->data;
 
105
 
 
106
      if (gdk_pixbuf_format_is_writable (format))
 
107
        {
 
108
          gchar **mime_types;
 
109
          gchar **type;
 
110
 
 
111
          mime_types = gdk_pixbuf_format_get_mime_types (format);
 
112
 
 
113
          for (type = mime_types; *type; type++)
 
114
            gimp_clip->n_target_entries++;
 
115
 
 
116
          g_strfreev (mime_types);
 
117
        }
 
118
    }
 
119
 
 
120
  if (gimp_clip->n_target_entries > 0)
 
121
    {
 
122
      gint i = 0;
 
123
 
 
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);
 
128
 
 
129
      for (list = gimp_clip->pixbuf_formats; list; list = g_slist_next (list))
 
130
        {
 
131
          GdkPixbufFormat *format = list->data;
 
132
 
 
133
          if (gdk_pixbuf_format_is_writable (format))
 
134
            {
 
135
              gchar  *format_name;
 
136
              gchar **mime_types;
 
137
              gchar **type;
 
138
 
 
139
              format_name = gdk_pixbuf_format_get_name (format);
 
140
              mime_types  = gdk_pixbuf_format_get_mime_types (format);
 
141
 
 
142
              for (type = mime_types; *type; type++)
 
143
                {
 
144
                  gchar *mime_type = *type;
 
145
 
 
146
                  if (gimp->be_verbose)
 
147
                    g_print ("GimpClipboard: writable pixbuf format: %s\n",
 
148
                             mime_type);
 
149
 
 
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;
 
153
 
 
154
                  gimp_clip->savers[i]                = g_strdup (format_name);
 
155
 
 
156
                  i++;
 
157
                }
 
158
 
 
159
              g_strfreev (mime_types);
 
160
              g_free (format_name);
 
161
            }
 
162
        }
 
163
    }
 
164
}
 
165
 
 
166
void
 
167
gimp_clipboard_exit (Gimp *gimp)
 
168
{
 
169
  g_return_if_fail (GIMP_IS_GIMP (gimp));
 
170
 
 
171
  g_signal_handlers_disconnect_by_func (gimp,
 
172
                                        G_CALLBACK (gimp_clipboard_buffer_changed),
 
173
                                        NULL);
 
174
  gimp_clipboard_set_buffer (gimp, NULL);
 
175
 
 
176
  g_object_set_data (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY, NULL);
 
177
}
 
178
 
 
179
/**
 
180
 * gimp_clipboard_has_buffer:
 
181
 * @gimp: pointer to #Gimp
 
182
 *
 
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
 
187
 * apply here.
 
188
 *
 
189
 * Return value: %TRUE if there's image data in the clipboard, %FALSE otherwise
 
190
 **/
 
191
gboolean
 
192
gimp_clipboard_has_buffer (Gimp *gimp)
 
193
{
 
194
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
 
195
 
 
196
  if (gimp->global_buffer)
 
197
    return TRUE;
 
198
 
 
199
  return (gimp_clipboard_wait_for_buffer (gimp) != GDK_NONE);
 
200
}
 
201
 
 
202
 
 
203
static TileManager *
 
204
tile_manager_new_from_pixbuf (GdkPixbuf *pixbuf)
 
205
{
 
206
  TileManager *tiles;
 
207
  guchar      *pixels;
 
208
  PixelRegion  destPR;
 
209
  gint         width;
 
210
  gint         height;
 
211
  gint         rowstride;
 
212
  gint         channels;
 
213
  gint         y;
 
214
 
 
215
  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
 
216
 
 
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);
 
221
 
 
222
  tiles = tile_manager_new (width, height, channels);
 
223
 
 
224
  pixel_region_init (&destPR, tiles, 0, 0, width, height, TRUE);
 
225
 
 
226
  for (y = 0, pixels = gdk_pixbuf_get_pixels (pixbuf);
 
227
       y < height;
 
228
       y++, pixels += rowstride)
 
229
    {
 
230
      pixel_region_set_row (&destPR, 0, y, width, pixels);
 
231
   }
 
232
 
 
233
  return tiles;
 
234
}
 
235
 
 
236
/**
 
237
 * gimp_clipboard_get_buffer:
 
238
 * @gimp: pointer to #Gimp
 
239
 *
 
240
 * Retrieves either image data from %GDK_SELECTION_CLIPBOARD or from
 
241
 * the global cut buffer of @gimp.
 
242
 *
 
243
 * The returned #GimpBuffer needs to be unref'ed when it's no longer
 
244
 * needed.
 
245
 *
 
246
 * Return value: a reference to a #GimpBuffer or %NULL if there's no
 
247
 *               image data
 
248
 **/
 
249
GimpBuffer *
 
250
gimp_clipboard_get_buffer (Gimp *gimp)
 
251
{
 
252
  GimpBuffer   *buffer = NULL;
 
253
  GtkClipboard *clipboard;
 
254
  GdkAtom       atom;
 
255
 
 
256
  g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
 
257
 
 
258
  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
 
259
                                             GDK_SELECTION_CLIPBOARD);
 
260
 
 
261
  if (clipboard                                                         &&
 
262
      gtk_clipboard_get_owner (clipboard)            != G_OBJECT (gimp) &&
 
263
      (atom = gimp_clipboard_wait_for_buffer (gimp)) != GDK_NONE)
 
264
    {
 
265
      GtkSelectionData *data;
 
266
 
 
267
      gimp_set_busy (gimp);
 
268
 
 
269
      data = gtk_clipboard_wait_for_contents (clipboard, atom);
 
270
 
 
271
      if (data)
 
272
        {
 
273
          GdkPixbuf *pixbuf = gimp_selection_data_get_pixbuf (data);
 
274
 
 
275
          gtk_selection_data_free (data);
 
276
 
 
277
          if (pixbuf)
 
278
            {
 
279
              TileManager *tiles = tile_manager_new_from_pixbuf (pixbuf);
 
280
 
 
281
              g_object_unref (pixbuf);
 
282
 
 
283
              buffer = gimp_buffer_new (tiles, _("Clipboard"), FALSE);
 
284
            }
 
285
        }
 
286
 
 
287
      gimp_unset_busy (gimp);
 
288
    }
 
289
 
 
290
  if (! buffer && gimp->global_buffer)
 
291
    buffer = g_object_ref (gimp->global_buffer);
 
292
 
 
293
  return buffer;
 
294
}
 
295
 
 
296
 
 
297
/*  private functions  */
 
298
 
 
299
static GimpClipboard *
 
300
gimp_clipboard_get (Gimp *gimp)
 
301
{
 
302
  return g_object_get_data (G_OBJECT (gimp), GIMP_CLIPBOARD_KEY);
 
303
}
 
304
 
 
305
static void
 
306
gimp_clipboard_free (GimpClipboard *gimp_clip)
 
307
{
 
308
  g_slist_free (gimp_clip->pixbuf_formats);
 
309
  g_free (gimp_clip->target_entries);
 
310
  g_strfreev (gimp_clip->savers);
 
311
  g_free (gimp_clip);
 
312
}
 
313
 
 
314
static void
 
315
gimp_clipboard_buffer_changed (Gimp *gimp)
 
316
{
 
317
  gimp_clipboard_set_buffer (gimp, gimp->global_buffer);
 
318
}
 
319
 
 
320
static void
 
321
gimp_clipboard_set_buffer (Gimp       *gimp,
 
322
                           GimpBuffer *buffer)
 
323
{
 
324
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
 
325
  GtkClipboard  *clipboard;
 
326
 
 
327
  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
 
328
                                             GDK_SELECTION_CLIPBOARD);
 
329
  if (! clipboard)
 
330
    return;
 
331
 
 
332
  if (buffer)
 
333
    {
 
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,
 
339
                                    G_OBJECT (gimp));
 
340
    }
 
341
  else if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (gimp))
 
342
    {
 
343
      gtk_clipboard_clear (clipboard);
 
344
    }
 
345
}
 
346
 
 
347
static GdkAtom *
 
348
gimp_clipboard_wait_for_targets (gint *n_targets)
 
349
{
 
350
  GtkClipboard *clipboard;
 
351
 
 
352
  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
 
353
                                             GDK_SELECTION_CLIPBOARD);
 
354
 
 
355
  if (clipboard)
 
356
    {
 
357
      GtkSelectionData *data;
 
358
 
 
359
      data = gtk_clipboard_wait_for_contents (clipboard,
 
360
                                              gdk_atom_intern ("TARGETS",
 
361
                                                               FALSE));
 
362
      if (data)
 
363
        {
 
364
          GdkAtom  *targets;
 
365
          gboolean  success;
 
366
 
 
367
          success = gtk_selection_data_get_targets (data, &targets, n_targets);
 
368
 
 
369
          gtk_selection_data_free (data);
 
370
 
 
371
          if (success)
 
372
            {
 
373
              gint i;
 
374
 
 
375
              for (i = 0; i < *n_targets; i++)
 
376
                g_print ("offered type: %s\n", gdk_atom_name (targets[i]));
 
377
 
 
378
              g_print ("\n");
 
379
 
 
380
              return targets;
 
381
            }
 
382
        }
 
383
    }
 
384
 
 
385
  return NULL;
 
386
}
 
387
 
 
388
static GdkAtom
 
389
gimp_clipboard_wait_for_buffer (Gimp *gimp)
 
390
{
 
391
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
 
392
  GdkAtom       *targets;
 
393
  gint           n_targets;
 
394
  GdkAtom        result    = GDK_NONE;
 
395
 
 
396
  targets = gimp_clipboard_wait_for_targets (&n_targets);
 
397
 
 
398
  if (targets)
 
399
    {
 
400
      GSList *list;
 
401
 
 
402
      for (list = gimp_clip->pixbuf_formats; list; list = g_slist_next (list))
 
403
        {
 
404
          GdkPixbufFormat  *format = list->data;
 
405
          gchar           **mime_types;
 
406
          gchar           **type;
 
407
 
 
408
          g_print ("checking pixbuf format '%s'\n",
 
409
                   gdk_pixbuf_format_get_name (format));
 
410
 
 
411
          mime_types = gdk_pixbuf_format_get_mime_types (format);
 
412
 
 
413
          for (type = mime_types; *type; type++)
 
414
            {
 
415
              gchar   *mime_type = *type;
 
416
              GdkAtom  atom      = gdk_atom_intern (mime_type, FALSE);
 
417
              gint     i;
 
418
 
 
419
              g_print (" - checking mime type '%s'\n", mime_type);
 
420
 
 
421
              for (i = 0; i < n_targets; i++)
 
422
                {
 
423
                  if (targets[i] == atom)
 
424
                    {
 
425
                      result = atom;
 
426
                      break;
 
427
                    }
 
428
                }
 
429
 
 
430
              if (result != GDK_NONE)
 
431
                break;
 
432
            }
 
433
 
 
434
          g_strfreev (mime_types);
 
435
 
 
436
          if (result != GDK_NONE)
 
437
            break;
 
438
        }
 
439
 
 
440
      g_free (targets);
 
441
    }
 
442
 
 
443
  return result;
 
444
}
 
445
 
 
446
static void
 
447
gimp_clipboard_send_buffer (GtkClipboard     *clipboard,
 
448
                            GtkSelectionData *selection_data,
 
449
                            guint             info,
 
450
                            Gimp             *gimp)
 
451
{
 
452
  GimpClipboard *gimp_clip = gimp_clipboard_get (gimp);
 
453
  GimpBuffer    *buffer    = gimp->global_buffer;
 
454
  GdkPixbuf     *pixbuf;
 
455
 
 
456
  gimp_set_busy (gimp);
 
457
 
 
458
  pixbuf = gimp_viewable_get_pixbuf (GIMP_VIEWABLE (buffer),
 
459
                                     gimp_buffer_get_width (buffer),
 
460
                                     gimp_buffer_get_height (buffer));
 
461
 
 
462
  if (pixbuf)
 
463
    {
 
464
      GdkAtom atom = gdk_atom_intern (gimp_clip->target_entries[info].target,
 
465
                                      FALSE);
 
466
 
 
467
      g_print ("sending pixbuf data as '%s' (%s)\n",
 
468
               gimp_clip->target_entries[info].target,
 
469
               gimp_clip->savers[info]);
 
470
 
 
471
      gimp_selection_data_set_pixbuf (selection_data, atom, pixbuf,
 
472
                                      gimp_clip->savers[info]);
 
473
    }
 
474
  else
 
475
    {
 
476
      g_warning ("%s: gimp_viewable_get_pixbuf() failed", G_STRFUNC);
 
477
    }
 
478
 
 
479
  gimp_unset_busy (gimp);
 
480
}
 
481
 
 
482
static gint
 
483
gimp_clipboard_format_compare (GdkPixbufFormat *a,
 
484
                               GdkPixbufFormat *b)
 
485
{
 
486
  gchar *a_name = gdk_pixbuf_format_get_name (a);
 
487
  gchar *b_name = gdk_pixbuf_format_get_name (b);
 
488
  gint   retval = 0;
 
489
 
 
490
  /*  move PNG to the front of the list  */
 
491
  if (strcmp (a_name, "png") == 0)
 
492
    retval = -1;
 
493
  else if (strcmp (b_name, "png") == 0)
 
494
    retval = 1;
 
495
 
 
496
  /*  move JPEG to the end of the list  */
 
497
  else if (strcmp (a_name, "jpeg") == 0)
 
498
    retval = 1;
 
499
  else if (strcmp (b_name, "jpeg") == 0)
 
500
    retval = -1;
 
501
 
 
502
  /*  move GIF to the end of the list  */
 
503
  else if (strcmp (a_name, "gif") == 0)
 
504
    retval = 1;
 
505
  else if (strcmp (b_name, "gif") == 0)
 
506
    retval = -1;
 
507
 
 
508
  g_free (a_name);
 
509
  g_free (b_name);
 
510
 
 
511
  return retval;
 
512
}