gtkclipboard-quartz.c 30.2 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Anders Carlsson's avatar
Anders Carlsson committed
2 3
 * Copyright (C) 2000 Red Hat, Inc.
 * Copyright (C) 2004 Nokia Corporation
4
 * Copyright (C) 2006-2008 Imendio AB
Anders Carlsson's avatar
Anders Carlsson committed
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Anders Carlsson's avatar
Anders Carlsson committed
18 19 20
 *
 */

21
#include "config.h"
Anders Carlsson's avatar
Anders Carlsson committed
22 23 24 25 26 27 28 29 30 31
#include <string.h>

#import <Cocoa/Cocoa.h>

#include "gtkclipboard.h"
#include "gtkinvisible.h"
#include "gtkmain.h"
#include "gtkmarshalers.h"
#include "gtkintl.h"
#include "gtktextbuffer.h"
32
#include "gtkselectionprivate.h"
Anders Carlsson's avatar
Anders Carlsson committed
33
#include "gtkquartz.h"
34

Anders Carlsson's avatar
Anders Carlsson committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

enum {
  OWNER_CHANGE,
  LAST_SIGNAL
};

typedef struct _GtkClipboardClass GtkClipboardClass;

struct _GtkClipboard 
{
  GObject parent_instance;

  NSPasteboard *pasteboard;

  GdkAtom selection;

  GtkClipboardGetFunc get_func;
  GtkClipboardClearFunc clear_func;
  gpointer user_data;
  gboolean have_owner;
55
  GtkTargetList *target_list;
Anders Carlsson's avatar
Anders Carlsson committed
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

  gboolean have_selection;
  GdkDisplay *display;

  GdkAtom *cached_targets;
  gint     n_cached_targets;

  guint      notify_signal_id;
  gboolean   storing_selection;
  GMainLoop *store_loop;
  guint      store_timeout;
  gint       n_storable_targets;
  GdkAtom   *storable_targets;
};

struct _GtkClipboardClass
{
  GObjectClass parent_class;

  void (*owner_change) (GtkClipboard        *clipboard,
			GdkEventOwnerChange *event);
};

@interface GtkClipboardOwner : NSObject {
  GtkClipboard *clipboard;

  GtkClipboardGetFunc get_func;
  GtkClipboardClearFunc clear_func;
  gpointer user_data;
  
}

@end

@implementation GtkClipboardOwner
-(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
{
  GtkSelectionData selection_data;
94
  guint info;
Anders Carlsson's avatar
Anders Carlsson committed
95

96 97 98 99 100
  if (!clipboard->target_list)
    return;

  memset (&selection_data, 0, sizeof (GtkSelectionData));

Anders Carlsson's avatar
Anders Carlsson committed
101 102
  selection_data.selection = clipboard->selection;
  selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
103 104
  selection_data.display = gdk_display_get_default ();
  selection_data.length = -1;
Anders Carlsson's avatar
Anders Carlsson committed
105

106
  if (gtk_target_list_find (clipboard->target_list, selection_data.target, &info))
107 108 109 110
    {
      clipboard->get_func (clipboard, &selection_data,
                           info,
                           clipboard->user_data);
111 112 113 114

      if (selection_data.length >= 0)
        _gtk_quartz_set_selection_data_for_pasteboard (clipboard->pasteboard,
                                                       &selection_data);
Anders Carlsson's avatar
Anders Carlsson committed
115

116 117
      g_free (selection_data.data);
    }
Anders Carlsson's avatar
Anders Carlsson committed
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
}

- (void)pasteboardChangedOwner:(NSPasteboard *)sender
{
  if (clear_func)
    clear_func (clipboard, user_data);

  [self release];
}

- (id)initWithClipboard:(GtkClipboard *)aClipboard
{
  self = [super init];

  if (self) 
    {
      clipboard = aClipboard;
    }

  return self;
}

@end

static void gtk_clipboard_class_init   (GtkClipboardClass   *class);
static void gtk_clipboard_finalize     (GObject             *object);
static void gtk_clipboard_owner_change (GtkClipboard        *clipboard,
					GdkEventOwnerChange *event);

static void          clipboard_unset      (GtkClipboard     *clipboard);
static GtkClipboard *clipboard_peek       (GdkDisplay       *display,
					   GdkAtom           selection,
					   gboolean          only_if_exists);

static const gchar clipboards_owned_key[] = "gtk-clipboards-owned";
static GQuark clipboards_owned_key_id = 0;

static GObjectClass *parent_class;
static guint         clipboard_signals[LAST_SIGNAL] = { 0 };

GType
gtk_clipboard_get_type (void)
{
  static GType clipboard_type = 0;
  
  if (!clipboard_type)
    {
165
      const GTypeInfo clipboard_info =
Anders Carlsson's avatar
Anders Carlsson committed
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
      {
	sizeof (GtkClipboardClass),
	NULL,           /* base_init */
	NULL,           /* base_finalize */
	(GClassInitFunc) gtk_clipboard_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data */
	sizeof (GtkClipboard),
	0,              /* n_preallocs */
	(GInstanceInitFunc) NULL,
      };
      
      clipboard_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkClipboard"),
					       &clipboard_info, 0);
    }
  
  return clipboard_type;
}

static void
gtk_clipboard_class_init (GtkClipboardClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);

  parent_class = g_type_class_peek_parent (class);
  
  gobject_class->finalize = gtk_clipboard_finalize;

  class->owner_change = gtk_clipboard_owner_change;

  clipboard_signals[OWNER_CHANGE] =
197
    g_signal_new (I_("owner-change"),
Anders Carlsson's avatar
Anders Carlsson committed
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
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkClipboardClass, owner_change),
		  NULL, NULL,
		  _gtk_marshal_VOID__BOXED,
		  G_TYPE_NONE, 1,
		  GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
}

static void
gtk_clipboard_finalize (GObject *object)
{
  GtkClipboard *clipboard;
  GSList *clipboards;

  clipboard = GTK_CLIPBOARD (object);

  clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
  if (g_slist_index (clipboards, clipboard) >= 0)
    g_warning ("GtkClipboard prematurely finalized");

  clipboard_unset (clipboard);
  
  clipboards = g_object_get_data (G_OBJECT (clipboard->display), "gtk-clipboard-list");
  clipboards = g_slist_remove (clipboards, clipboard);
  g_object_set_data (G_OBJECT (clipboard->display), I_("gtk-clipboard-list"), clipboards);

  if (clipboard->store_loop && g_main_loop_is_running (clipboard->store_loop))
    g_main_loop_quit (clipboard->store_loop);

  if (clipboard->store_timeout != 0)
    g_source_remove (clipboard->store_timeout);

  g_free (clipboard->storable_targets);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
clipboard_display_closed (GdkDisplay   *display,
			  gboolean      is_error,
			  GtkClipboard *clipboard)
{
  GSList *clipboards;

  clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");
  g_object_run_dispose (G_OBJECT (clipboard));
  clipboards = g_slist_remove (clipboards, clipboard);
  g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
  g_object_unref (clipboard);
}

250 251 252 253 254
/**
 * gtk_clipboard_get_for_display:
 * @display: the display for which the clipboard is to be retrieved or created
 * @selection: a #GdkAtom which identifies the clipboard to use.
 */
Anders Carlsson's avatar
Anders Carlsson committed
255
GtkClipboard *
256
gtk_clipboard_get_for_display (GdkDisplay *display,
Anders Carlsson's avatar
Anders Carlsson committed
257 258 259
			       GdkAtom     selection)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
260
  g_return_val_if_fail (!gdk_display_is_closed (display), NULL);
Anders Carlsson's avatar
Anders Carlsson committed
261 262 263 264

  return clipboard_peek (display, selection, FALSE);
}

265 266 267 268
/**
 * gtk_clipboard_get:
 * @selection: a #GdkAtom which identifies the clipboard to use
 */
Anders Carlsson's avatar
Anders Carlsson committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
GtkClipboard *
gtk_clipboard_get (GdkAtom selection)
{
  return gtk_clipboard_get_for_display (gdk_display_get_default (), selection);
}

static void
clipboard_owner_destroyed (gpointer data)
{
  GSList *clipboards = data;
  GSList *tmp_list;

  tmp_list = clipboards;
  while (tmp_list)
    {
      GtkClipboard *clipboard = tmp_list->data;

      clipboard->get_func = NULL;
      clipboard->clear_func = NULL;
      clipboard->user_data = NULL;
      clipboard->have_owner = FALSE;

291 292 293 294 295 296
      if (clipboard->target_list)
        {
          gtk_target_list_unref (clipboard->target_list);
          clipboard->target_list = NULL;
        }

Anders Carlsson's avatar
Anders Carlsson committed
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
      gtk_clipboard_clear (clipboard);

      tmp_list = tmp_list->next;
    }
  
  g_slist_free (clipboards);
}

static void
clipboard_add_owner_notify (GtkClipboard *clipboard)
{
  if (!clipboards_owned_key_id)
    clipboards_owned_key_id = g_quark_from_static_string (clipboards_owned_key);
  
  if (clipboard->have_owner)
    g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
			     g_slist_prepend (g_object_steal_qdata (clipboard->user_data,
								    clipboards_owned_key_id),
					      clipboard),
			     clipboard_owner_destroyed);
}

static void
clipboard_remove_owner_notify (GtkClipboard *clipboard)
{
  if (clipboard->have_owner)
     g_object_set_qdata_full (clipboard->user_data, clipboards_owned_key_id,
			      g_slist_remove (g_object_steal_qdata (clipboard->user_data,
								    clipboards_owned_key_id),
					      clipboard),
			      clipboard_owner_destroyed);
}

static gboolean
gtk_clipboard_set_contents (GtkClipboard         *clipboard,
			    const GtkTargetEntry *targets,
			    guint                 n_targets,
			    GtkClipboardGetFunc   get_func,
			    GtkClipboardClearFunc clear_func,
			    gpointer              user_data,
			    gboolean              have_owner)
{
  GtkClipboardOwner *owner;
340
  NSSet *types;
Richard Hult's avatar
Richard Hult committed
341 342 343
  NSAutoreleasePool *pool;

  pool = [[NSAutoreleasePool alloc] init];
Anders Carlsson's avatar
Anders Carlsson committed
344 345

  owner = [[GtkClipboardOwner alloc] initWithClipboard:clipboard];
Richard Hult's avatar
Richard Hult committed
346

347 348 349 350 351 352 353 354 355 356 357 358 359 360
  if (!(clipboard->have_owner && have_owner) ||
      clipboard->user_data != user_data)
    {
      clipboard_unset (clipboard);

      if (clipboard->get_func)
        {
          /* Calling unset() caused the clipboard contents to be reset!
           * Avoid leaking and return
           */
          if (!(clipboard->have_owner && have_owner) ||
              clipboard->user_data != user_data)
            {
              (*clear_func) (clipboard, user_data);
361
              [pool release];
362 363 364
              return FALSE;
            }
          else
365 366 367 368
            {
              [pool release];
              return TRUE;
            }
369 370 371
        }
    }

Anders Carlsson's avatar
Anders Carlsson committed
372 373 374 375 376 377 378
  clipboard->user_data = user_data;
  clipboard->have_owner = have_owner;
  if (have_owner)
    clipboard_add_owner_notify (clipboard);
  clipboard->get_func = get_func;
  clipboard->clear_func = clear_func;

379 380 381 382
  if (clipboard->target_list)
    gtk_target_list_unref (clipboard->target_list);
  clipboard->target_list = gtk_target_list_new (targets, n_targets);

383 384 385
  types = _gtk_quartz_target_entries_to_pasteboard_types (targets, n_targets);

  [clipboard->pasteboard declareTypes:[types allObjects] owner:owner];
386
  [types release];
Richard Hult's avatar
Richard Hult committed
387 388
  [pool release];

389
  return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
390 391
}

392 393 394 395 396 397 398 399 400 401 402 403
/**
 * gtk_clipboard_set_with_data: (skip)
 * @clipboard: a #GtkClipboard
 * @targets: (array length=n_targets): array containing information
 *     about the available forms for the clipboard data
 * @n_targets: number of elements in @targets
 * @get_func: (scope async): function to call to get the actual clipboard data
 * @clear_func: (scope async): when the clipboard contents are set again,
 *     this function will be called, and @get_func will not be subsequently
 *     called.
 * @user_data: user data to pass to @get_func and @clear_func.
 */
Anders Carlsson's avatar
Anders Carlsson committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
gboolean
gtk_clipboard_set_with_data (GtkClipboard          *clipboard,
			     const GtkTargetEntry  *targets,
			     guint                  n_targets,
			     GtkClipboardGetFunc    get_func,
			     GtkClipboardClearFunc  clear_func,
			     gpointer               user_data)
{
  g_return_val_if_fail (clipboard != NULL, FALSE);
  g_return_val_if_fail (targets != NULL, FALSE);
  g_return_val_if_fail (get_func != NULL, FALSE);

  return gtk_clipboard_set_contents (clipboard, targets, n_targets,
				     get_func, clear_func, user_data,
				     FALSE);
}

421 422 423 424 425 426 427 428 429 430 431 432 433
/**
 * gtk_clipboard_set_with_owner: (skip)
 * @clipboard: a #GtkClipboard
 * @targets: (array length=n_targets): array containing information
 *     about the available forms for the clipboard data
 * @n_targets: number of elements in @targets
 * @get_func: (scope async): function to call to get the actual clipboard data
 * @clear_func: (scope async): when the clipboard contents are set again,
 *     this function will be called, and @get_func will not be subsequently
 *     called
 * @owner: an object that "owns" the data. This object will be passed
 *     to the callbacks when called
 */
Anders Carlsson's avatar
Anders Carlsson committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
gboolean
gtk_clipboard_set_with_owner (GtkClipboard          *clipboard,
			      const GtkTargetEntry  *targets,
			      guint                  n_targets,
			      GtkClipboardGetFunc    get_func,
			      GtkClipboardClearFunc  clear_func,
			      GObject               *owner)
{
  g_return_val_if_fail (clipboard != NULL, FALSE);
  g_return_val_if_fail (targets != NULL, FALSE);
  g_return_val_if_fail (get_func != NULL, FALSE);
  g_return_val_if_fail (G_IS_OBJECT (owner), FALSE);

  return gtk_clipboard_set_contents (clipboard, targets, n_targets,
				     get_func, clear_func, owner,
				     TRUE);
}

452 453 454 455
/**
 * gtk_clipboard_get_owner:
 * @clipboard: a #GtkClipboard
 */
Anders Carlsson's avatar
Anders Carlsson committed
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
GObject *
gtk_clipboard_get_owner (GtkClipboard *clipboard)
{
  g_return_val_if_fail (clipboard != NULL, NULL);

  if (clipboard->have_owner)
    return clipboard->user_data;
  else
    return NULL;
}

static void
clipboard_unset (GtkClipboard *clipboard)
{
  GtkClipboardClearFunc old_clear_func;
  gpointer old_data;
  gboolean old_have_owner;
  gint old_n_storable_targets;
  
  old_clear_func = clipboard->clear_func;
  old_data = clipboard->user_data;
  old_have_owner = clipboard->have_owner;
  old_n_storable_targets = clipboard->n_storable_targets;
  
  if (old_have_owner)
    {
482
      clipboard_remove_owner_notify (clipboard);
Anders Carlsson's avatar
Anders Carlsson committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496
      clipboard->have_owner = FALSE;
    }

  clipboard->n_storable_targets = -1;
  g_free (clipboard->storable_targets);
  clipboard->storable_targets = NULL;
      
  clipboard->get_func = NULL;
  clipboard->clear_func = NULL;
  clipboard->user_data = NULL;
  
  if (old_clear_func)
    old_clear_func (clipboard, old_data);

497 498 499 500 501 502
  if (clipboard->target_list)
    {
      gtk_target_list_unref (clipboard->target_list);
      clipboard->target_list = NULL;
    }

Anders Carlsson's avatar
Anders Carlsson committed
503 504 505 506 507 508 509 510
  /* If we've transferred the clipboard data to the manager,
   * unref the owner
   */
  if (old_have_owner &&
      old_n_storable_targets != -1)
    g_object_unref (old_data);
}

511 512 513 514
/**
 * gtk_clipboard_clear:
 * @clipboard:  a #GtkClipboard
 */
Anders Carlsson's avatar
Anders Carlsson committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
void
gtk_clipboard_clear (GtkClipboard *clipboard)
{
  [clipboard->pasteboard declareTypes:nil owner:nil];
}

static void 
text_get_func (GtkClipboard     *clipboard,
	       GtkSelectionData *selection_data,
	       guint             info,
	       gpointer          data)
{
  gtk_selection_data_set_text (selection_data, data, -1);
}

static void 
text_clear_func (GtkClipboard *clipboard,
		 gpointer      data)
{
  g_free (data);
}

537 538 539 540 541 542 543
/**
 * gtk_clipboard_set_text:
 * @clipboard: a #GtkClipboard object
 * @text:      a UTF-8 string.
 * @len:       length of @text, in bytes, or -1, in which case
 *             the length will be determined with <function>strlen()</function>.
 */
Anders Carlsson's avatar
Anders Carlsson committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
void 
gtk_clipboard_set_text (GtkClipboard *clipboard,
			const gchar  *text,
			gint          len)
{
  GtkTargetEntry target = { "UTF8_STRING", 0, 0 };

  g_return_if_fail (clipboard != NULL);
  g_return_if_fail (text != NULL);
  
  if (len < 0)
    len = strlen (text);
  
  gtk_clipboard_set_with_data (clipboard, 
			       &target, 1,
			       text_get_func, text_clear_func,
			       g_strndup (text, len));
  gtk_clipboard_set_can_store (clipboard, NULL, 0);
}


static void 
pixbuf_get_func (GtkClipboard     *clipboard,
		 GtkSelectionData *selection_data,
		 guint             info,
		 gpointer          data)
{
  gtk_selection_data_set_pixbuf (selection_data, data);
}

static void 
pixbuf_clear_func (GtkClipboard *clipboard,
		   gpointer      data)
{
  g_object_unref (data);
}

581 582 583 584 585
/**
 * gtk_clipboard_set_image:
 * @clipboard: a #GtkClipboard object
 * @pixbuf:    a #GdkPixbuf 
 */
Anders Carlsson's avatar
Anders Carlsson committed
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 618 619 620
void
gtk_clipboard_set_image (GtkClipboard *clipboard,
			 GdkPixbuf    *pixbuf)
{
  GtkTargetList *list;
  GList *l;
  GtkTargetEntry *targets;
  gint n_targets, i;

  g_return_if_fail (clipboard != NULL);
  g_return_if_fail (GDK_IS_PIXBUF (pixbuf));

  list = gtk_target_list_new (NULL, 0);
  gtk_target_list_add_image_targets (list, 0, TRUE);

  n_targets = g_list_length (list->list);
  targets = g_new0 (GtkTargetEntry, n_targets);
  for (l = list->list, i = 0; l; l = l->next, i++)
    {
      GtkTargetPair *pair = (GtkTargetPair *)l->data;
      targets[i].target = gdk_atom_name (pair->target);
    }

  gtk_clipboard_set_with_data (clipboard, 
			       targets, n_targets,
			       pixbuf_get_func, pixbuf_clear_func,
			       g_object_ref (pixbuf));
  gtk_clipboard_set_can_store (clipboard, NULL, 0);

  for (i = 0; i < n_targets; i++)
    g_free (targets[i].target);
  g_free (targets);
  gtk_target_list_unref (list);
}

621 622 623 624 625 626 627 628 629 630
/**
 * gtk_clipboard_request_contents:
 * @clipboard: a #GtkClipboard
 * @target: an atom representing the form into which the clipboard
 *     owner should convert the selection.
 * @callback: (scope async): A function to call when the results are received
 *     (or the retrieval fails). If the retrieval fails the length field of
 *     @selection_data will be negative.
 * @user_data: user data to pass to @callback
 */
Anders Carlsson's avatar
Anders Carlsson committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
void 
gtk_clipboard_request_contents (GtkClipboard            *clipboard,
				GdkAtom                  target,
				GtkClipboardReceivedFunc callback,
				gpointer                 user_data)
{
  GtkSelectionData *data;

  data = gtk_clipboard_wait_for_contents (clipboard, target);

  callback (clipboard, data, user_data);

  gtk_selection_data_free (data);
}

646 647 648 649 650 651 652
/**
 * gtk_clipboard_request_text:
 * @clipboard: a #GtkClipboard
 * @callback: (scope async): a function to call when the text is received,
 *     or the retrieval fails. (It will always be called one way or the other.)
 * @user_data: user data to pass to @callback.
 */
Anders Carlsson's avatar
Anders Carlsson committed
653 654 655 656 657 658 659 660 661 662 663 664
void 
gtk_clipboard_request_text (GtkClipboard                *clipboard,
			    GtkClipboardTextReceivedFunc callback,
			    gpointer                     user_data)
{
  gchar *data = gtk_clipboard_wait_for_text (clipboard);

  callback (clipboard, data, user_data);

  g_free (data);
}

665 666 667 668 669 670 671 672
/**
 * gtk_clipboard_request_rich_text:
 * @clipboard: a #GtkClipboard
 * @buffer: a #GtkTextBuffer
 * @callback: (scope async): a function to call when the text is received,
 *     or the retrieval fails. (It will always be called one way or the other.)
 * @user_data: user data to pass to @callback.
 */
Anders Carlsson's avatar
Anders Carlsson committed
673 674 675 676 677 678 679 680 681
void
gtk_clipboard_request_rich_text (GtkClipboard                    *clipboard,
                                 GtkTextBuffer                   *buffer,
                                 GtkClipboardRichTextReceivedFunc callback,
                                 gpointer                         user_data)
{
  /* FIXME: Implement */
}

682 683 684 685 686 687 688 689 690 691 692

guint8 *
gtk_clipboard_wait_for_rich_text (GtkClipboard  *clipboard,
                                  GtkTextBuffer *buffer,
                                  GdkAtom       *format,
                                  gsize         *length)
{
  /* FIXME: Implement */
  return NULL;
}

693 694 695 696 697 698 699
/**
 * gtk_clipboard_request_image:
 * @clipboard: a #GtkClipboard
 * @callback: (scope async): a function to call when the image is received,
 *     or the retrieval fails. (It will always be called one way or the other.)
 * @user_data: user data to pass to @callback.
 */
Anders Carlsson's avatar
Anders Carlsson committed
700 701 702 703 704 705 706 707 708 709 710 711 712
void 
gtk_clipboard_request_image (GtkClipboard                  *clipboard,
			     GtkClipboardImageReceivedFunc  callback,
			     gpointer                       user_data)
{
  GdkPixbuf *pixbuf = gtk_clipboard_wait_for_image (clipboard);

  callback (clipboard, pixbuf, user_data);

  if (pixbuf)
    g_object_unref (pixbuf);
}

713 714 715 716 717 718 719
/**
 * gtk_clipboard_request_uris:
 * @clipboard: a #GtkClipboard
 * @callback: (scope async): a function to call when the URIs are received,
 *     or the retrieval fails. (It will always be called one way or the other.)
 * @user_data: user data to pass to @callback.
 */
720 721 722 723 724 725 726 727 728 729 730 731
void 
gtk_clipboard_request_uris (GtkClipboard                *clipboard,
			    GtkClipboardURIReceivedFunc  callback,
			    gpointer                     user_data)
{
  gchar **uris = gtk_clipboard_wait_for_uris (clipboard);

  callback (clipboard, uris, user_data);

  g_strfreev (uris);
}

732 733 734 735 736 737 738 739
/**
 * gtk_clipboard_request_targets:
 * @clipboard: a #GtkClipboard
 * @callback: (scope async): a function to call when the targets are
 *     received, or the retrieval fails. (It will always be called
 *     one way or the other.)
 * @user_data: user data to pass to @callback.
 */
Anders Carlsson's avatar
Anders Carlsson committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753
void 
gtk_clipboard_request_targets (GtkClipboard                *clipboard,
			       GtkClipboardTargetsReceivedFunc callback,
			       gpointer                     user_data)
{
  GdkAtom *targets;
  gint n_targets;

  gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets);

  callback (clipboard, targets, n_targets, user_data);
}


754 755 756 757 758 759
/**
 * gtk_clipboard_wait_for_contents:
 * @clipboard: a #GtkClipboard
 * @target: an atom representing the form into which the clipboard
 *          owner should convert the selection.
 */
Anders Carlsson's avatar
Anders Carlsson committed
760 761 762 763 764 765 766 767 768 769
GtkSelectionData *
gtk_clipboard_wait_for_contents (GtkClipboard *clipboard,
				 GdkAtom       target)
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  GtkSelectionData *selection_data = NULL;

  if (target == gdk_atom_intern_static_string ("TARGETS")) 
    {
      NSArray *types = [clipboard->pasteboard types];
770
      int i, length;
Anders Carlsson's avatar
Anders Carlsson committed
771 772 773
      GList *atom_list, *l;
      GdkAtom *atoms;

774
      length = [types count] * sizeof (GdkAtom);
Anders Carlsson's avatar
Anders Carlsson committed
775
      
776
      selection_data = g_slice_new0 (GtkSelectionData);
Anders Carlsson's avatar
Anders Carlsson committed
777 778 779
      selection_data->selection = clipboard->selection;
      selection_data->target = target;

780 781 782
      atoms = g_malloc (length);

      atom_list = _gtk_quartz_pasteboard_types_to_atom_list (types);
Anders Carlsson's avatar
Anders Carlsson committed
783 784
      for (l = atom_list, i = 0; l ; l = l->next, i++)
	atoms[i] = GDK_POINTER_TO_ATOM (l->data);
785
      g_list_free (atom_list);
Anders Carlsson's avatar
Anders Carlsson committed
786

787 788 789
      gtk_selection_data_set (selection_data,
                              GDK_SELECTION_TYPE_ATOM, 32,
                              (guchar *)atoms, length);
Anders Carlsson's avatar
Anders Carlsson committed
790 791 792 793 794 795 796 797 798 799 800

      [pool release];

      return selection_data;
    }

  selection_data = _gtk_quartz_get_selection_data_from_pasteboard (clipboard->pasteboard,
								   target,
								   clipboard->selection);

  [pool release];
801

Anders Carlsson's avatar
Anders Carlsson committed
802 803 804
  return selection_data;
}

805 806 807 808
/**
 * gtk_clipboard_wait_for_text:
 * @clipboard: a #GtkClipboard
 */
Anders Carlsson's avatar
Anders Carlsson committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
gchar *
gtk_clipboard_wait_for_text (GtkClipboard *clipboard)
{
  GtkSelectionData *data;
  gchar *result;

  data = gtk_clipboard_wait_for_contents (clipboard, 
					  gdk_atom_intern_static_string ("UTF8_STRING"));

  result = (gchar *)gtk_selection_data_get_text (data);

  gtk_selection_data_free (data);

  return result;
}

825 826 827 828
/**
 * gtk_clipboard_wait_for_image:
 * @clipboard: a #GtkClipboard
 */
Anders Carlsson's avatar
Anders Carlsson committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
GdkPixbuf *
gtk_clipboard_wait_for_image (GtkClipboard *clipboard)
{
  const gchar *priority[] = { "image/png", "image/tiff", "image/jpeg", "image/gif", "image/bmp" };
  int i;
  GtkSelectionData *data;

  for (i = 0; i < G_N_ELEMENTS (priority); i++) 
    {    
      data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string (priority[i]));

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

	  gtk_selection_data_free (data);

	  return pixbuf;
	}  
  }

  return NULL;
}

853 854 855 856
/**
 * gtk_clipboard_wait_for_uris:
 * @clipboard: a #GtkClipboard
 */
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
gchar **
gtk_clipboard_wait_for_uris (GtkClipboard *clipboard)
{
  GtkSelectionData *data;

  data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("text/uri-list"));
  if (data)
    {
      gchar **uris;

      uris = gtk_selection_data_get_uris (data);
      gtk_selection_data_free (data);

      return uris;
    }  

  return NULL;
}

876 877 878 879
/**
 * gtk_clipboard_get_display:
 * @clipboard: a #GtkClipboard
 */
Anders Carlsson's avatar
Anders Carlsson committed
880 881 882 883 884 885 886 887
GdkDisplay *
gtk_clipboard_get_display (GtkClipboard *clipboard)
{
  g_return_val_if_fail (clipboard != NULL, NULL);

  return clipboard->display;
}

888 889 890 891
/**
 * gtk_clipboard_wait_is_text_available:
 * @clipboard: a #GtkClipboard
 */
Anders Carlsson's avatar
Anders Carlsson committed
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
gboolean
gtk_clipboard_wait_is_text_available (GtkClipboard *clipboard)
{
  GtkSelectionData *data;
  gboolean result = FALSE;

  data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
  if (data)
    {
      result = gtk_selection_data_targets_include_text (data);
      gtk_selection_data_free (data);
    }

  return result;
}

908 909 910 911 912
/**
 * gtk_clipboard_wait_is_rich_text_available:
 * @clipboard: a #GtkClipboard
 * @buffer: a #GtkTextBuffer
 */
Anders Carlsson's avatar
Anders Carlsson committed
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
gboolean
gtk_clipboard_wait_is_rich_text_available (GtkClipboard  *clipboard,
                                           GtkTextBuffer *buffer)
{
  GtkSelectionData *data;
  gboolean result = FALSE;

  g_return_val_if_fail (GTK_IS_CLIPBOARD (clipboard), FALSE);
  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);

  data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));
  if (data)
    {
      result = gtk_selection_data_targets_include_rich_text (data, buffer);
      gtk_selection_data_free (data);
    }

  return result;
}

933 934 935 936
/**
 * gtk_clipboard_wait_is_image_available:
 * @clipboard: a #GtkClipboard
 */
Anders Carlsson's avatar
Anders Carlsson committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
gboolean
gtk_clipboard_wait_is_image_available (GtkClipboard *clipboard)
{
  GtkSelectionData *data;
  gboolean result = FALSE;

  data = gtk_clipboard_wait_for_contents (clipboard, 
					  gdk_atom_intern_static_string ("TARGETS"));
  if (data)
    {
      result = gtk_selection_data_targets_include_image (data, FALSE);
      gtk_selection_data_free (data);
    }

  return result;
}

954 955 956 957
/**
 * gtk_clipboard_wait_is_uris_available:
 * @clipboard: a #GtkClipboard
 */
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
gboolean
gtk_clipboard_wait_is_uris_available (GtkClipboard *clipboard)
{
  GtkSelectionData *data;
  gboolean result = FALSE;

  data = gtk_clipboard_wait_for_contents (clipboard, 
					  gdk_atom_intern_static_string ("TARGETS"));
  if (data)
    {
      result = gtk_selection_data_targets_include_uri (data);
      gtk_selection_data_free (data);
    }

  return result;
}

975 976 977 978 979 980 981 982
/**
 * gtk_clipboard_wait_for_targets:
 * @clipboard: a #GtkClipboard
 * @targets: (out) (array length=n_targets) (transfer container): location
 *           to store an array of targets. The result stored here must
 *           be freed with g_free().
 * @n_targets: location to store number of items in @targets.
 */
Anders Carlsson's avatar
Anders Carlsson committed
983 984 985 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 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
gboolean
gtk_clipboard_wait_for_targets (GtkClipboard  *clipboard, 
				GdkAtom      **targets,
				gint          *n_targets)
{
  GtkSelectionData *data;
  gboolean result = FALSE;
  
  g_return_val_if_fail (clipboard != NULL, FALSE);

  /* If the display supports change notification we cache targets */
  if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)) &&
      clipboard->n_cached_targets != -1)
    {
      if (n_targets)
 	*n_targets = clipboard->n_cached_targets;
 
      if (targets)
 	*targets = g_memdup (clipboard->cached_targets,
 			     clipboard->n_cached_targets * sizeof (GdkAtom));

       return TRUE;
    }
  
  if (n_targets)
    *n_targets = 0;
      
  if (targets)
    *targets = NULL;      

  data = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern_static_string ("TARGETS"));

  if (data)
    {
      GdkAtom *tmp_targets;
      gint tmp_n_targets;
       
      result = gtk_selection_data_get_targets (data, &tmp_targets, &tmp_n_targets);
 
      if (gdk_display_supports_selection_notification (gtk_clipboard_get_display (clipboard)))
 	{
 	  clipboard->n_cached_targets = tmp_n_targets;
 	  clipboard->cached_targets = g_memdup (tmp_targets,
 						tmp_n_targets * sizeof (GdkAtom));
 	}
 
      if (n_targets)
 	*n_targets = tmp_n_targets;
 
      if (targets)
 	*targets = tmp_targets;
      else
 	g_free (tmp_targets);
      
      gtk_selection_data_free (data);
    }

  return result;
}

static GtkClipboard *
clipboard_peek (GdkDisplay *display, 
		GdkAtom     selection,
		gboolean    only_if_exists)
{
  GtkClipboard *clipboard = NULL;
  GSList *clipboards;
  GSList *tmp_list;

  if (selection == GDK_NONE)
    selection = GDK_SELECTION_CLIPBOARD;

  clipboards = g_object_get_data (G_OBJECT (display), "gtk-clipboard-list");

  tmp_list = clipboards;
  while (tmp_list)
    {
      clipboard = tmp_list->data;
      if (clipboard->selection == selection)
	break;

      tmp_list = tmp_list->next;
    }

  if (!tmp_list && !only_if_exists)
    {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      NSString *pasteboard_name;
      clipboard = g_object_new (GTK_TYPE_CLIPBOARD, NULL);

      if (selection == GDK_SELECTION_CLIPBOARD) 
	pasteboard_name = NSGeneralPboard;
      else 
	{
	  char *atom_string = gdk_atom_name (selection);

	  pasteboard_name = [NSString stringWithFormat:@"_GTK_%@", 
			     [NSString stringWithUTF8String:atom_string]];
	  g_free (atom_string);
	}

      clipboard->pasteboard = [NSPasteboard pasteboardWithName:pasteboard_name];

      [pool release];

      clipboard->selection = selection;
      clipboard->display = display;
      clipboard->n_cached_targets = -1;
      clipboard->n_storable_targets = -1;
      clipboards = g_slist_prepend (clipboards, clipboard);
      g_object_set_data (G_OBJECT (display), I_("gtk-clipboard-list"), clipboards);
      g_signal_connect (display, "closed",
			G_CALLBACK (clipboard_display_closed), clipboard);
      gdk_display_request_selection_notification (display, selection);
    }
  
  return clipboard;
}

static void
gtk_clipboard_owner_change (GtkClipboard        *clipboard,
			    GdkEventOwnerChange *event)
{
  if (clipboard->n_cached_targets != -1)
    {
      clipboard->n_cached_targets = -1;
      g_free (clipboard->cached_targets);
    }
}

1113 1114 1115 1116 1117
/**
 * gtk_clipboard_wait_is_target_available:
 * @clipboard: a #GtkClipboard
 * @target:    A #GdkAtom indicating which target to look for.
 */
Anders Carlsson's avatar
Anders Carlsson committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
gboolean
gtk_clipboard_wait_is_target_available (GtkClipboard *clipboard,
					GdkAtom       target)
{
  GdkAtom *targets;
  gint i, n_targets;
  gboolean retval = FALSE;
    
  if (!gtk_clipboard_wait_for_targets (clipboard, &targets, &n_targets))
    return FALSE;

  for (i = 0; i < n_targets; i++)
    {
      if (targets[i] == target)
	{
	  retval = TRUE;
	  break;
	}
    }

  g_free (targets);
  
  return retval;
}

1143 1144 1145 1146
/**
 * _gtk_clipboard_handle_event:
 * @event: a owner change event
 */
Anders Carlsson's avatar
Anders Carlsson committed
1147 1148 1149 1150 1151
void 
_gtk_clipboard_handle_event (GdkEventOwnerChange *event)
{
}

1152 1153 1154 1155 1156 1157 1158 1159
/**
 * gtk_clipboard_set_can_store:
 * @clipboard: a #GtkClipboard
 * @targets: (allow-none) (array length=n_targets): array containing
 *           information about which forms should be stored or %NULL
 *           to indicate that all forms should be stored.
 * @n_targets: number of elements in @targets
 */
Anders Carlsson's avatar
Anders Carlsson committed
1160 1161 1162 1163 1164 1165 1166 1167
void
gtk_clipboard_set_can_store (GtkClipboard         *clipboard,
 			     const GtkTargetEntry *targets,
 			     gint                  n_targets)
{
  /* FIXME: Implement */
}

1168 1169 1170 1171
/**
 * gtk_clipboard_store:
 * @clipboard: a #GtkClipboard
 */
Anders Carlsson's avatar
Anders Carlsson committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
void
gtk_clipboard_store (GtkClipboard *clipboard)
{
  /* FIXME: Implement */
}

void
_gtk_clipboard_store_all (void)
{
  /* FIXME: Implement */
}