1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gdkconfig.h"
34 #include "gdk/gdkkeysyms.h"
37 #include "gtkiconfactory.h"
38 #include "gtkicontheme.h"
40 #include "gtkinvisible.h"
44 #include "gtkwindow.h"
48 #include "gtkquartz.h"
49 #include "gdk/quartz/gdkquartz.h"
51 typedef struct _GtkDragSourceSite GtkDragSourceSite;
52 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
53 typedef struct _GtkDragDestSite GtkDragDestSite;
54 typedef struct _GtkDragDestInfo GtkDragDestInfo;
55 typedef struct _GtkDragFindData GtkDragFindData;
57 static void gtk_drag_find_widget (GtkWidget *widget,
58 GtkDragFindData *data);
59 static void gtk_drag_dest_site_destroy (gpointer data);
60 static void gtk_drag_dest_leave (GtkWidget *widget,
61 GdkDragContext *context,
63 static GtkDragDestInfo *gtk_drag_get_dest_info (GdkDragContext *context,
65 static void gtk_drag_source_site_destroy (gpointer data);
67 struct _GtkDragSourceSite
69 GdkModifierType start_button_mask;
70 GtkTargetList *target_list; /* Targets for drag data */
71 GdkDragAction actions; /* Possible actions */
74 GtkImageType icon_type;
77 GtkImagePixmapData pixmap;
78 GtkImagePixbufData pixbuf;
79 GtkImageStockData stock;
80 GtkImageIconNameData name;
84 GdkColormap *colormap; /* Colormap for drag icon */
86 /* Stored button press information to detect drag beginning */
91 struct _GtkDragSourceInfo
94 GtkTargetList *target_list; /* Targets for drag data */
95 GdkDragAction possible_actions; /* Actions allowed by source */
96 GdkDragContext *context; /* drag context */
98 gint hot_x, hot_y; /* Hot spot for drag */
99 GdkPixbuf *icon_pixbuf;
102 struct _GtkDragDestSite
104 GtkDestDefaults flags;
105 GtkTargetList *target_list;
106 GdkDragAction actions;
110 struct _GtkDragDestInfo
112 GtkWidget *widget; /* Widget in which drag is in */
113 GdkDragContext *context; /* Drag context */
114 guint dropped : 1; /* Set after we receive a drop */
115 gint drop_x, drop_y; /* Position of drop */
118 struct _GtkDragFindData
122 GdkDragContext *context;
123 GtkDragDestInfo *info;
126 gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
127 gint x, gint y, guint32 time);
132 @interface GtkDragSourceOwner : NSObject {
133 GtkDragSourceInfo *info;
138 @implementation GtkDragSourceOwner
139 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
142 GtkSelectionData selection_data;
144 selection_data.selection = GDK_NONE;
145 selection_data.data = NULL;
146 selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
148 if (gtk_target_list_find (info->target_list,
149 selection_data.target,
152 g_signal_emit_by_name (info->widget, "drag_data_get",
158 _gtk_quartz_set_selection_data_for_pasteboard (sender, &selection_data);
160 g_free (selection_data.data);
164 - (id)initWithInfo:(GtkDragSourceInfo *)anInfo
179 gtk_drag_get_data (GtkWidget *widget,
180 GdkDragContext *context,
184 id <NSDraggingInfo> dragging_info = GDK_DRAG_CONTEXT_PRIVATE (context)->dragging_info;
185 NSPasteboard *pasteboard = [dragging_info draggingPasteboard];
186 GtkSelectionData *selection_data;
187 GtkDragDestInfo *info;
188 GtkDragDestSite *site;
190 info = gtk_drag_get_dest_info (context, FALSE);
191 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
193 selection_data = _gtk_quartz_get_selection_data_from_pasteboard (pasteboard,
196 if (site && site->target_list)
200 if (gtk_target_list_find (site->target_list,
201 selection_data->target,
204 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
205 selection_data->length >= 0)
206 g_signal_emit_by_name (widget,
207 "drag_data_received",
208 context, info->drop_x, info->drop_y,
215 g_signal_emit_by_name (widget,
216 "drag_data_received",
217 context, info->drop_x, info->drop_y,
222 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
224 gtk_drag_finish (context,
225 (selection_data->length >= 0),
226 (context->action == GDK_ACTION_MOVE),
233 gtk_drag_get_source_widget (GdkDragContext *context)
239 gtk_drag_finish (GdkDragContext *context,
247 gtk_drag_dest_info_destroy (gpointer data)
249 GtkDragDestInfo *info = data;
254 static GtkDragDestInfo *
255 gtk_drag_get_dest_info (GdkDragContext *context,
258 GtkDragDestInfo *info;
259 static GQuark info_quark = 0;
261 info_quark = g_quark_from_static_string ("gtk-dest-info");
263 info = g_object_get_qdata (G_OBJECT (context), info_quark);
266 info = g_new (GtkDragDestInfo, 1);
268 info->context = context;
269 info->dropped = FALSE;
270 g_object_set_qdata_full (G_OBJECT (context), info_quark,
271 info, gtk_drag_dest_info_destroy);
277 static GQuark dest_info_quark = 0;
279 static GtkDragSourceInfo *
280 gtk_drag_get_source_info (GdkDragContext *context,
283 GtkDragSourceInfo *info;
284 if (!dest_info_quark)
285 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
287 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
290 info = g_new0 (GtkDragSourceInfo, 1);
291 info->context = context;
292 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
299 gtk_drag_clear_source_info (GdkDragContext *context)
301 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
304 /*************************************************************
305 * gtk_drag_highlight_expose:
306 * Callback for expose_event for highlighted widgets.
312 *************************************************************/
315 gtk_drag_highlight_expose (GtkWidget *widget,
316 GdkEventExpose *event,
319 gint x, y, width, height;
321 if (GTK_WIDGET_DRAWABLE (widget))
325 if (GTK_WIDGET_NO_WINDOW (widget))
327 x = widget->allocation.x;
328 y = widget->allocation.y;
329 width = widget->allocation.width;
330 height = widget->allocation.height;
336 gdk_drawable_get_size (widget->window, &width, &height);
339 gtk_paint_shadow (widget->style, widget->window,
340 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
342 x, y, width, height);
344 cr = gdk_cairo_create (widget->window);
345 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
346 cairo_set_line_width (cr, 1.0);
349 width - 1, height - 1);
357 /*************************************************************
358 * gtk_drag_highlight:
359 * Highlight the given widget in the default manner.
363 *************************************************************/
366 gtk_drag_highlight (GtkWidget *widget)
368 g_return_if_fail (GTK_IS_WIDGET (widget));
370 g_signal_connect_after (widget, "expose_event",
371 G_CALLBACK (gtk_drag_highlight_expose),
374 gtk_widget_queue_draw (widget);
377 /*************************************************************
378 * gtk_drag_unhighlight:
379 * Refresh the given widget to remove the highlight.
383 *************************************************************/
386 gtk_drag_unhighlight (GtkWidget *widget)
388 g_return_if_fail (GTK_IS_WIDGET (widget));
390 g_signal_handlers_disconnect_by_func (widget,
391 gtk_drag_highlight_expose,
394 gtk_widget_queue_draw (widget);
398 get_toplevel_nswindow (GtkWidget *widget)
400 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
402 if (GTK_WIDGET_TOPLEVEL (toplevel) && toplevel->window)
403 return [gdk_quartz_window_get_nsview (toplevel->window) window];
409 register_types (GtkWidget *widget, GtkDragDestSite *site)
411 if (site->target_list)
413 NSWindow *nswindow = get_toplevel_nswindow (widget);
415 NSAutoreleasePool *pool;
420 pool = [[NSAutoreleasePool alloc] init];
421 types = _gtk_quartz_target_list_to_pasteboard_types (site->target_list);
423 [nswindow registerForDraggedTypes:types];
429 gtk_drag_dest_realized (GtkWidget *widget,
432 GtkDragDestSite *site = user_data;
434 register_types (widget, site);
438 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
439 GtkWidget *previous_toplevel,
442 GtkDragDestSite *site = user_data;
444 register_types (widget, site);
448 gtk_drag_dest_site_destroy (gpointer data)
450 GtkDragDestSite *site = data;
452 if (site->target_list)
453 gtk_target_list_unref (site->target_list);
459 gtk_drag_dest_set (GtkWidget *widget,
460 GtkDestDefaults flags,
461 const GtkTargetEntry *targets,
463 GdkDragAction actions)
465 GtkDragDestSite *old_site, *site;
467 g_return_if_fail (GTK_IS_WIDGET (widget));
469 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
472 g_signal_handlers_disconnect_by_func (widget,
473 gtk_drag_dest_realized,
475 g_signal_handlers_disconnect_by_func (widget,
476 gtk_drag_dest_hierarchy_changed,
480 site = g_new (GtkDragDestSite, 1);
482 site->have_drag = FALSE;
484 site->target_list = gtk_target_list_new (targets, n_targets);
486 site->target_list = NULL;
487 site->actions = actions;
489 if (GTK_WIDGET_REALIZED (widget))
490 gtk_drag_dest_realized (widget, site);
492 g_signal_connect (widget, "realize",
493 G_CALLBACK (gtk_drag_dest_realized), site);
494 g_signal_connect (widget, "hierarchy_changed",
495 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
497 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
498 site, gtk_drag_dest_site_destroy);
502 gtk_drag_dest_set_proxy (GtkWidget *widget,
503 GdkWindow *proxy_window,
504 GdkDragProtocol protocol,
505 gboolean use_coordinates)
507 g_warning ("gtk_drag_dest_set_proxy is not supported on Mac OS X.");
511 gtk_drag_dest_unset (GtkWidget *widget)
513 g_return_if_fail (GTK_IS_WIDGET (widget));
515 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
519 gtk_drag_dest_get_target_list (GtkWidget *widget)
521 GtkDragDestSite *site;
523 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
525 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
527 return site ? site->target_list : NULL;
531 gtk_drag_dest_set_target_list (GtkWidget *widget,
532 GtkTargetList *target_list)
534 GtkDragDestSite *site;
536 g_return_if_fail (GTK_IS_WIDGET (widget));
538 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
542 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
543 "to make the widget into a drag destination");
548 gtk_target_list_ref (target_list);
550 if (site->target_list)
551 gtk_target_list_unref (site->target_list);
553 site->target_list = target_list;
555 register_types (widget, site);
559 gtk_drag_dest_add_text_targets (GtkWidget *widget)
561 GtkTargetList *target_list;
563 target_list = gtk_drag_dest_get_target_list (widget);
565 gtk_target_list_ref (target_list);
567 target_list = gtk_target_list_new (NULL, 0);
568 gtk_target_list_add_text_targets (target_list, 0);
569 gtk_drag_dest_set_target_list (widget, target_list);
570 gtk_target_list_unref (target_list);
574 gtk_drag_dest_add_image_targets (GtkWidget *widget)
576 GtkTargetList *target_list;
578 target_list = gtk_drag_dest_get_target_list (widget);
580 gtk_target_list_ref (target_list);
582 target_list = gtk_target_list_new (NULL, 0);
583 gtk_target_list_add_image_targets (target_list, 0, FALSE);
584 gtk_drag_dest_set_target_list (widget, target_list);
585 gtk_target_list_unref (target_list);
589 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
591 GtkTargetList *target_list;
593 target_list = gtk_drag_dest_get_target_list (widget);
595 gtk_target_list_ref (target_list);
597 target_list = gtk_target_list_new (NULL, 0);
598 gtk_target_list_add_uri_targets (target_list, 0);
599 gtk_drag_dest_set_target_list (widget, target_list);
600 gtk_target_list_unref (target_list);
604 prepend_and_ref_widget (GtkWidget *widget,
607 GSList **slist_p = data;
609 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
613 gtk_drag_find_widget (GtkWidget *widget,
614 GtkDragFindData *data)
616 GtkAllocation new_allocation;
617 gint allocation_to_window_x = 0;
618 gint allocation_to_window_y = 0;
622 if (data->found || !GTK_WIDGET_MAPPED (widget) || !GTK_WIDGET_SENSITIVE (widget))
625 /* Note that in the following code, we only count the
626 * position as being inside a WINDOW widget if it is inside
627 * widget->window; points that are outside of widget->window
628 * but within the allocation are not counted. This is consistent
629 * with the way we highlight drag targets.
631 * data->x,y are relative to widget->parent->window (if
632 * widget is not a toplevel, widget->window otherwise).
633 * We compute the allocation of widget in the same coordinates,
634 * clipping to widget->window, and all intermediate
635 * windows. If data->x,y is inside that, then we translate
636 * our coordinates to be relative to widget->window and
639 new_allocation = widget->allocation;
644 GdkWindow *window = widget->window;
646 /* Compute the offset from allocation-relative to
647 * window-relative coordinates.
649 allocation_to_window_x = widget->allocation.x;
650 allocation_to_window_y = widget->allocation.y;
652 if (!GTK_WIDGET_NO_WINDOW (widget))
654 /* The allocation is relative to the parent window for
655 * window widgets, not to widget->window.
657 gdk_window_get_position (window, &tx, &ty);
659 allocation_to_window_x -= tx;
660 allocation_to_window_y -= ty;
663 new_allocation.x = 0 + allocation_to_window_x;
664 new_allocation.y = 0 + allocation_to_window_y;
666 while (window && window != widget->parent->window)
668 GdkRectangle window_rect = { 0, 0, 0, 0 };
670 gdk_drawable_get_size (window, &window_rect.width, &window_rect.height);
672 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
674 gdk_window_get_position (window, &tx, &ty);
675 new_allocation.x += tx;
677 new_allocation.y += ty;
680 window = gdk_window_get_parent (window);
683 if (!window) /* Window and widget heirarchies didn't match. */
687 if (data->toplevel ||
688 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
689 (data->x < new_allocation.x + new_allocation.width) &&
690 (data->y < new_allocation.y + new_allocation.height)))
692 /* First, check if the drag is in a valid drop site in
693 * one of our children
695 if (GTK_IS_CONTAINER (widget))
697 GtkDragFindData new_data = *data;
698 GSList *children = NULL;
701 new_data.x -= x_offset;
702 new_data.y -= y_offset;
703 new_data.found = FALSE;
704 new_data.toplevel = FALSE;
706 /* need to reference children temporarily in case the
707 * ::drag_motion/::drag_drop callbacks change the widget heirarchy.
709 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
710 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
712 if (!new_data.found && GTK_WIDGET_DRAWABLE (tmp_list->data))
713 gtk_drag_find_widget (tmp_list->data, &new_data);
714 g_object_unref (tmp_list->data);
716 g_slist_free (children);
718 data->found = new_data.found;
721 /* If not, and this widget is registered as a drop site, check to
722 * emit "drag_motion" to check if we are actually in
726 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
728 data->found = data->callback (widget,
730 data->x - x_offset - allocation_to_window_x,
731 data->y - y_offset - allocation_to_window_y,
733 /* If so, send a "drag_leave" to the last widget */
736 if (data->info->widget && data->info->widget != widget)
738 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
740 data->info->widget = widget;
747 gtk_drag_dest_leave (GtkWidget *widget,
748 GdkDragContext *context,
751 GtkDragDestSite *site;
753 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
754 g_return_if_fail (site != NULL);
756 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
757 gtk_drag_unhighlight (widget);
759 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag)
760 g_signal_emit_by_name (widget, "drag_leave",
763 site->have_drag = FALSE;
767 gtk_drag_dest_motion (GtkWidget *widget,
768 GdkDragContext *context,
773 GtkDragDestSite *site;
774 GdkDragAction action = 0;
777 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
778 g_return_val_if_fail (site != NULL, FALSE);
780 if (site->flags & GTK_DEST_DEFAULT_MOTION)
782 if (context->suggested_action & site->actions)
783 action = context->suggested_action;
785 if (action && gtk_drag_dest_find_target (widget, context, NULL))
787 if (!site->have_drag)
789 site->have_drag = TRUE;
790 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
791 gtk_drag_highlight (widget);
794 gdk_drag_status (context, action, time);
798 gdk_drag_status (context, 0, time);
803 g_signal_emit_by_name (widget, "drag_motion",
804 context, x, y, time, &retval);
806 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
810 gtk_drag_dest_drop (GtkWidget *widget,
811 GdkDragContext *context,
816 GtkDragDestSite *site;
817 GtkDragDestInfo *info;
820 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
821 g_return_val_if_fail (site != NULL, FALSE);
823 info = gtk_drag_get_dest_info (context, FALSE);
824 g_return_val_if_fail (info != NULL, FALSE);
829 if (site->flags & GTK_DEST_DEFAULT_DROP)
831 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
833 if (target == GDK_NONE)
835 gtk_drag_finish (context, FALSE, FALSE, time);
839 gtk_drag_get_data (widget, context, target, time);
842 g_signal_emit_by_name (widget, "drag_drop",
843 context, x, y, time, &retval);
845 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
849 gtk_drag_dest_set_track_motion (GtkWidget *widget,
850 gboolean track_motion)
852 g_return_if_fail (GTK_IS_WIDGET (widget));
854 /* FIXME: Implement */
858 gtk_drag_dest_get_track_motion (GtkWidget *widget)
860 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
862 /* FIXME: Implement */
868 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
871 GtkDragDestInfo *info;
872 GdkDragContext *context;
874 g_return_if_fail (toplevel != NULL);
875 g_return_if_fail (event != NULL);
877 context = event->dnd.context;
879 info = gtk_drag_get_dest_info (context, TRUE);
881 /* Find the widget for the event */
890 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
895 case GDK_DRAG_MOTION:
898 GtkDragFindData data;
901 if (event->type == GDK_DROP_START)
903 info->dropped = TRUE;
904 /* We send a leave here so that the widget unhighlights
909 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
914 gdk_window_get_position (toplevel->window, &tx, &ty);
916 data.x = event->dnd.x_root - tx;
917 data.y = event->dnd.y_root - ty;
918 data.context = context;
921 data.toplevel = TRUE;
922 data.callback = (event->type == GDK_DRAG_MOTION) ?
923 gtk_drag_dest_motion : gtk_drag_dest_drop;
924 data.time = event->dnd.time;
926 gtk_drag_find_widget (toplevel, &data);
928 if (info->widget && !data.found)
930 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
936 if (event->type == GDK_DRAG_MOTION)
939 gdk_drag_status (context, 0, event->dnd.time);
944 g_assert_not_reached ();
951 gtk_drag_dest_find_target (GtkWidget *widget,
952 GdkDragContext *context,
953 GtkTargetList *target_list)
955 id <NSDraggingInfo> dragging_info = GDK_DRAG_CONTEXT_PRIVATE (context)->dragging_info;
956 NSPasteboard *pasteboard = [dragging_info draggingPasteboard];
957 GtkWidget *source_widget;
959 GList *tmp_source = NULL;
960 GList *source_targets;
962 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
963 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
964 g_return_val_if_fail (!context->is_source, GDK_NONE);
966 source_widget = gtk_drag_get_source_widget (context);
968 if (target_list == NULL)
969 target_list = gtk_drag_dest_get_target_list (widget);
971 if (target_list == NULL)
974 source_targets = _gtk_quartz_pasteboard_types_to_atom_list ([pasteboard types]);
975 tmp_target = target_list->list;
978 GtkTargetPair *pair = tmp_target->data;
979 tmp_source = source_targets;
982 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
984 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
985 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
987 g_list_free (source_targets);
993 tmp_source = tmp_source->next;
995 tmp_target = tmp_target->next;
998 g_list_free (source_targets);
1002 static GdkDragContext *
1003 gtk_drag_begin_internal (GtkWidget *widget,
1004 GtkDragSourceSite *site,
1005 GtkTargetList *target_list,
1006 GdkDragAction actions,
1010 GtkDragSourceInfo *info;
1011 GdkDragContext *context;
1013 NSPasteboard *pasteboard;
1014 GtkDragSourceOwner *owner;
1018 context = gdk_drag_begin (NULL, NULL);
1019 context->is_source = TRUE;
1021 info = gtk_drag_get_source_info (context, TRUE);
1023 info->widget = gtk_widget_ref (widget);
1024 info->target_list = target_list;
1025 gtk_target_list_ref (target_list);
1027 info->possible_actions = actions;
1029 g_signal_emit_by_name (widget, "drag_begin", info->context);
1031 /* Ensure that we have an icon before we start the drag; the
1032 * application may have set one in ::drag_begin, or it may
1035 if (!info->icon_pixbuf)
1037 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
1038 gtk_drag_set_icon_default (context);
1040 switch (site->icon_type)
1042 case GTK_IMAGE_PIXMAP:
1043 /* This is not supported, so just set a small transparent pixbuf
1044 * since we need to have something.
1047 gtk_drag_set_icon_pixmap (context,
1049 site->icon_data.pixmap.pixmap,
1056 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 1, 1);
1057 gdk_pixbuf_fill (pixbuf, 0xffffff);
1059 gtk_drag_set_icon_pixbuf (context,
1063 g_object_unref (pixbuf);
1066 case GTK_IMAGE_PIXBUF:
1067 gtk_drag_set_icon_pixbuf (context,
1068 site->icon_data.pixbuf.pixbuf,
1071 case GTK_IMAGE_STOCK:
1072 gtk_drag_set_icon_stock (context,
1073 site->icon_data.stock.stock_id,
1076 case GTK_IMAGE_ICON_NAME:
1077 gtk_drag_set_icon_name (context,
1078 site->icon_data.name.icon_name,
1081 case GTK_IMAGE_EMPTY:
1083 g_assert_not_reached();
1088 gdk_pointer_ungrab (0);
1090 pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
1091 owner = [[GtkDragSourceOwner alloc] initWithInfo:info];
1093 [pasteboard declareTypes:_gtk_quartz_target_list_to_pasteboard_types (target_list) owner:owner];
1095 /* Ref the context. It's unreffed when the drag has been aborted */
1096 g_object_ref (info->context);
1098 nswindow = get_toplevel_nswindow (widget);
1100 /* FIXME: If the event isn't a mouse event, use the global cursor position instead */
1101 nsevent = [nswindow currentEvent];
1102 point = [nsevent locationInWindow];
1104 [nswindow dragImage:_gtk_quartz_create_image_from_pixbuf (info->icon_pixbuf)
1106 offset:NSMakeSize(0, 0)
1108 pasteboard:pasteboard
1112 return info->context;
1116 gtk_drag_begin (GtkWidget *widget,
1117 GtkTargetList *targets,
1118 GdkDragAction actions,
1122 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1123 g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), NULL);
1124 g_return_val_if_fail (targets != NULL, NULL);
1126 return gtk_drag_begin_internal (widget, NULL, targets,
1127 actions, button, event);
1132 gtk_drag_source_event_cb (GtkWidget *widget,
1136 GtkDragSourceSite *site;
1137 gboolean retval = FALSE;
1138 site = (GtkDragSourceSite *)data;
1140 switch (event->type)
1142 case GDK_BUTTON_PRESS:
1143 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1145 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
1146 site->x = event->button.x;
1147 site->y = event->button.y;
1151 case GDK_BUTTON_RELEASE:
1152 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1153 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
1156 case GDK_MOTION_NOTIFY:
1157 if (site->state & event->motion.state & site->start_button_mask)
1159 /* FIXME: This is really broken and can leave us
1165 if (site->state & event->motion.state &
1166 GDK_BUTTON1_MASK << (i - 1))
1170 if (gtk_drag_check_threshold (widget, site->x, site->y,
1171 event->motion.x, event->motion.y))
1174 gtk_drag_begin_internal (widget, site, site->target_list,
1183 default: /* hit for 2/3BUTTON_PRESS */
1191 gtk_drag_source_set (GtkWidget *widget,
1192 GdkModifierType start_button_mask,
1193 const GtkTargetEntry *targets,
1195 GdkDragAction actions)
1197 GtkDragSourceSite *site;
1199 g_return_if_fail (GTK_IS_WIDGET (widget));
1201 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1203 gtk_widget_add_events (widget,
1204 gtk_widget_get_events (widget) |
1205 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1206 GDK_BUTTON_MOTION_MASK);
1210 if (site->target_list)
1211 gtk_target_list_unref (site->target_list);
1215 site = g_new0 (GtkDragSourceSite, 1);
1217 site->icon_type = GTK_IMAGE_EMPTY;
1219 g_signal_connect (widget, "button_press_event",
1220 G_CALLBACK (gtk_drag_source_event_cb),
1222 g_signal_connect (widget, "button_release_event",
1223 G_CALLBACK (gtk_drag_source_event_cb),
1225 g_signal_connect (widget, "motion_notify_event",
1226 G_CALLBACK (gtk_drag_source_event_cb),
1229 g_object_set_data_full (G_OBJECT (widget),
1230 I_("gtk-site-data"),
1231 site, gtk_drag_source_site_destroy);
1234 site->start_button_mask = start_button_mask;
1236 site->target_list = gtk_target_list_new (targets, n_targets);
1238 site->actions = actions;
1241 /*************************************************************
1242 * gtk_drag_source_unset
1243 * Unregister this widget as a drag source.
1247 *************************************************************/
1250 gtk_drag_source_unset (GtkWidget *widget)
1252 GtkDragSourceSite *site;
1254 g_return_if_fail (GTK_IS_WIDGET (widget));
1256 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1260 g_signal_handlers_disconnect_by_func (widget,
1261 gtk_drag_source_event_cb,
1263 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
1268 gtk_drag_source_get_target_list (GtkWidget *widget)
1270 GtkDragSourceSite *site;
1272 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1274 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1276 return site ? site->target_list : NULL;
1281 gtk_drag_source_set_target_list (GtkWidget *widget,
1282 GtkTargetList *target_list)
1284 GtkDragSourceSite *site;
1286 g_return_if_fail (GTK_IS_WIDGET (widget));
1288 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1291 g_warning ("gtk_drag_source_set_target_list() requires the widget "
1292 "to already be a drag source.");
1297 gtk_target_list_ref (target_list);
1299 if (site->target_list)
1300 gtk_target_list_unref (site->target_list);
1302 site->target_list = target_list;
1306 * gtk_drag_source_add_text_targets:
1307 * @widget: a #GtkWidget that's is a drag source
1309 * Add the text targets supported by #GtkSelection to
1310 * the target list of the drag source. The targets
1311 * are added with @info = 0. If you need another value,
1312 * use gtk_target_list_add_text_targets() and
1313 * gtk_drag_source_set_target_list().
1318 gtk_drag_source_add_text_targets (GtkWidget *widget)
1320 GtkTargetList *target_list;
1322 target_list = gtk_drag_source_get_target_list (widget);
1324 gtk_target_list_ref (target_list);
1326 target_list = gtk_target_list_new (NULL, 0);
1327 gtk_target_list_add_text_targets (target_list, 0);
1328 gtk_drag_source_set_target_list (widget, target_list);
1329 gtk_target_list_unref (target_list);
1333 gtk_drag_source_add_image_targets (GtkWidget *widget)
1335 GtkTargetList *target_list;
1337 target_list = gtk_drag_source_get_target_list (widget);
1339 gtk_target_list_ref (target_list);
1341 target_list = gtk_target_list_new (NULL, 0);
1342 gtk_target_list_add_image_targets (target_list, 0, TRUE);
1343 gtk_drag_source_set_target_list (widget, target_list);
1344 gtk_target_list_unref (target_list);
1348 gtk_drag_source_add_uri_targets (GtkWidget *widget)
1350 GtkTargetList *target_list;
1352 target_list = gtk_drag_source_get_target_list (widget);
1354 gtk_target_list_ref (target_list);
1356 target_list = gtk_target_list_new (NULL, 0);
1357 gtk_target_list_add_uri_targets (target_list, 0);
1358 gtk_drag_source_set_target_list (widget, target_list);
1359 gtk_target_list_unref (target_list);
1363 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
1365 switch (site->icon_type)
1367 case GTK_IMAGE_EMPTY:
1369 case GTK_IMAGE_PIXMAP:
1370 if (site->icon_data.pixmap.pixmap)
1371 g_object_unref (site->icon_data.pixmap.pixmap);
1372 if (site->icon_mask)
1373 g_object_unref (site->icon_mask);
1375 case GTK_IMAGE_PIXBUF:
1376 g_object_unref (site->icon_data.pixbuf.pixbuf);
1378 case GTK_IMAGE_STOCK:
1379 g_free (site->icon_data.stock.stock_id);
1381 case GTK_IMAGE_ICON_NAME:
1382 g_free (site->icon_data.name.icon_name);
1385 g_assert_not_reached();
1388 site->icon_type = GTK_IMAGE_EMPTY;
1391 g_object_unref (site->colormap);
1392 site->colormap = NULL;
1396 gtk_drag_source_site_destroy (gpointer data)
1398 GtkDragSourceSite *site = data;
1400 if (site->target_list)
1401 gtk_target_list_unref (site->target_list);
1403 gtk_drag_source_unset_icon (site);
1408 gtk_drag_source_set_icon (GtkWidget *widget,
1409 GdkColormap *colormap,
1413 GtkDragSourceSite *site;
1415 g_return_if_fail (GTK_IS_WIDGET (widget));
1416 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1417 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
1418 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
1420 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1421 g_return_if_fail (site != NULL);
1423 g_object_ref (colormap);
1424 g_object_ref (pixmap);
1426 g_object_ref (mask);
1428 gtk_drag_source_unset_icon (site);
1430 site->icon_type = GTK_IMAGE_PIXMAP;
1432 site->icon_data.pixmap.pixmap = pixmap;
1433 site->icon_mask = mask;
1434 site->colormap = colormap;
1438 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
1441 GtkDragSourceSite *site;
1443 g_return_if_fail (GTK_IS_WIDGET (widget));
1444 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1446 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1447 g_return_if_fail (site != NULL);
1448 g_object_ref (pixbuf);
1450 gtk_drag_source_unset_icon (site);
1452 site->icon_type = GTK_IMAGE_PIXBUF;
1453 site->icon_data.pixbuf.pixbuf = pixbuf;
1457 * gtk_drag_source_set_icon_stock:
1458 * @widget: a #GtkWidget
1459 * @stock_id: the ID of the stock icon to use
1461 * Sets the icon that will be used for drags from a particular source
1465 gtk_drag_source_set_icon_stock (GtkWidget *widget,
1466 const gchar *stock_id)
1468 GtkDragSourceSite *site;
1470 g_return_if_fail (GTK_IS_WIDGET (widget));
1471 g_return_if_fail (stock_id != NULL);
1473 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1474 g_return_if_fail (site != NULL);
1476 gtk_drag_source_unset_icon (site);
1478 site->icon_type = GTK_IMAGE_STOCK;
1479 site->icon_data.stock.stock_id = g_strdup (stock_id);
1483 * gtk_drag_source_set_icon_name:
1484 * @widget: a #GtkWidget
1485 * @icon_name: name of icon to use
1487 * Sets the icon that will be used for drags from a particular source
1488 * to a themed icon. See the docs for #GtkIconTheme for more details.
1493 gtk_drag_source_set_icon_name (GtkWidget *widget,
1494 const gchar *icon_name)
1496 GtkDragSourceSite *site;
1498 g_return_if_fail (GTK_IS_WIDGET (widget));
1499 g_return_if_fail (icon_name != NULL);
1501 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1502 g_return_if_fail (site != NULL);
1504 gtk_drag_source_unset_icon (site);
1506 site->icon_type = GTK_IMAGE_ICON_NAME;
1507 site->icon_data.name.icon_name = g_strdup (icon_name);
1512 * gtk_drag_set_icon_widget:
1513 * @context: the context for a drag. (This must be called
1514 with a context for the source side of a drag)
1515 * @widget: a toplevel window to use as an icon.
1516 * @hot_x: the X offset within @widget of the hotspot.
1517 * @hot_y: the Y offset within @widget of the hotspot.
1519 * Changes the icon for a widget to a given widget. GTK+
1520 * will not destroy the icon, so if you don't want
1521 * it to persist, you should connect to the "drag_end"
1522 * signal and destroy it yourself.
1525 gtk_drag_set_icon_widget (GdkDragContext *context,
1530 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1531 g_return_if_fail (context->is_source);
1532 g_return_if_fail (GTK_IS_WIDGET (widget));
1534 g_warning ("gtk_drag_set_icon_widget is not supported on Mac OS X");
1538 set_icon_stock_pixbuf (GdkDragContext *context,
1539 const gchar *stock_id,
1544 GtkDragSourceInfo *info;
1546 info = gtk_drag_get_source_info (context, FALSE);
1550 pixbuf = gtk_widget_render_icon (info->widget, stock_id,
1551 GTK_ICON_SIZE_DND, NULL);
1555 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
1560 g_object_ref (pixbuf);
1562 if (info->icon_pixbuf)
1563 g_object_unref (info->icon_pixbuf);
1564 info->icon_pixbuf = pixbuf;
1565 info->hot_x = hot_x;
1566 info->hot_y = hot_y;
1570 * gtk_drag_set_icon_pixbuf:
1571 * @context: the context for a drag. (This must be called
1572 * with a context for the source side of a drag)
1573 * @pixbuf: the #GdkPixbuf to use as the drag icon.
1574 * @hot_x: the X offset within @widget of the hotspot.
1575 * @hot_y: the Y offset within @widget of the hotspot.
1577 * Sets @pixbuf as the icon for a given drag.
1580 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
1585 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1586 g_return_if_fail (context->is_source);
1587 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1589 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1593 * gtk_drag_set_icon_stock:
1594 * @context: the context for a drag. (This must be called
1595 * with a context for the source side of a drag)
1596 * @stock_id: the ID of the stock icon to use for the drag.
1597 * @hot_x: the X offset within the icon of the hotspot.
1598 * @hot_y: the Y offset within the icon of the hotspot.
1600 * Sets the icon for a given drag from a stock ID.
1603 gtk_drag_set_icon_stock (GdkDragContext *context,
1604 const gchar *stock_id,
1609 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1610 g_return_if_fail (context->is_source);
1611 g_return_if_fail (stock_id != NULL);
1613 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
1617 * gtk_drag_set_icon_pixmap:
1618 * @context: the context for a drag. (This must be called
1619 * with a context for the source side of a drag)
1620 * @colormap: the colormap of the icon
1621 * @pixmap: the image data for the icon
1622 * @mask: the transparency mask for the icon
1623 * @hot_x: the X offset within @pixmap of the hotspot.
1624 * @hot_y: the Y offset within @pixmap of the hotspot.
1626 * Sets @pixmap as the icon for a given drag. GTK+ retains
1627 * references for the arguments, and will release them when
1628 * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
1629 * will be more convenient to use.
1632 gtk_drag_set_icon_pixmap (GdkDragContext *context,
1633 GdkColormap *colormap,
1639 g_warning ("gtk_drag_set_icon_pixmap is not supported on Mac OS X");
1643 * gtk_drag_set_icon_name:
1644 * @context: the context for a drag. (This must be called
1645 * with a context for the source side of a drag)
1646 * @icon_name: name of icon to use
1647 * @hot_x: the X offset of the hotspot within the icon
1648 * @hot_y: the Y offset of the hotspot within the icon
1650 * Sets the icon for a given drag from a named themed icon. See
1651 * the docs for #GtkIconTheme for more details. Note that the
1652 * size of the icon depends on the icon theme (the icon is
1653 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
1654 * @hot_x and @hot_y have to be used with care.
1659 gtk_drag_set_icon_name (GdkDragContext *context,
1660 const gchar *icon_name,
1665 GtkSettings *settings;
1666 GtkIconTheme *icon_theme;
1668 gint width, height, icon_size;
1670 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1671 g_return_if_fail (context->is_source);
1672 g_return_if_fail (icon_name != NULL);
1674 screen = gdk_drawable_get_screen (context->source_window);
1675 g_return_if_fail (screen != NULL);
1677 settings = gtk_settings_get_for_screen (screen);
1678 if (gtk_icon_size_lookup_for_settings (settings,
1681 icon_size = MAX (width, height);
1683 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
1685 icon_theme = gtk_icon_theme_get_for_screen (screen);
1687 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
1688 icon_size, 0, NULL);
1690 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1692 g_warning ("Cannot load drag icon from icon name %s", icon_name);
1696 * gtk_drag_set_icon_default:
1697 * @context: the context for a drag. (This must be called
1698 with a context for the source side of a drag)
1700 * Sets the icon for a particular drag to the default
1704 gtk_drag_set_icon_default (GdkDragContext *context)
1706 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1707 g_return_if_fail (context->is_source);
1709 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
1713 gtk_drag_set_default_icon (GdkColormap *colormap,
1719 g_warning ("gtk_drag_set_default_icon is not supported on Mac OS X.");
1723 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
1725 if (info->icon_pixbuf)
1726 g_object_unref (info->icon_pixbuf);
1728 g_signal_emit_by_name (info->widget, "drag_end",
1732 g_object_unref (info->widget);
1734 gtk_target_list_unref (info->target_list);
1736 gtk_drag_clear_source_info (info->context);
1737 g_object_unref (info->context);
1743 gtk_drag_drop_finished (GtkDragSourceInfo *info)
1745 gtk_drag_source_info_destroy (info);
1748 /*************************************************************
1749 * _gtk_drag_source_handle_event:
1750 * Called from widget event handling code on Drag events
1754 * toplevel: Toplevel widget that received the event
1757 *************************************************************/
1760 _gtk_drag_source_handle_event (GtkWidget *widget,
1763 GtkDragSourceInfo *info;
1764 GdkDragContext *context;
1766 g_return_if_fail (widget != NULL);
1767 g_return_if_fail (event != NULL);
1769 context = event->dnd.context;
1770 info = gtk_drag_get_source_info (context, FALSE);
1774 switch (event->type)
1776 case GDK_DROP_FINISHED:
1777 gtk_drag_drop_finished (info);
1780 g_assert_not_reached ();
1786 gtk_drag_check_threshold (GtkWidget *widget,
1792 gint drag_threshold;
1794 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1796 g_object_get (gtk_widget_get_settings (widget),
1797 "gtk-dnd-drag-threshold", &drag_threshold,
1800 return (ABS (current_x - start_x) > drag_threshold ||
1801 ABS (current_y - start_y) > drag_threshold);
1804 #define __GTK_DND_C__
1805 #include "gtkaliasdef.c"