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"
46 #include "gtkquartz.h"
48 #include "gdk/quartz/gdkquartz.h"
50 typedef struct _GtkDragSourceSite GtkDragSourceSite;
51 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
52 typedef struct _GtkDragDestSite GtkDragDestSite;
53 typedef struct _GtkDragDestInfo GtkDragDestInfo;
54 typedef struct _GtkDragFindData GtkDragFindData;
56 static void gtk_drag_find_widget (GtkWidget *widget,
57 GtkDragFindData *data);
58 static void gtk_drag_dest_site_destroy (gpointer data);
59 static void gtk_drag_dest_leave (GtkWidget *widget,
60 GdkDragContext *context,
62 static GtkDragDestInfo *gtk_drag_get_dest_info (GdkDragContext *context,
64 static void gtk_drag_source_site_destroy (gpointer data);
66 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
69 extern GdkDragContext *gdk_quartz_drag_source_context (); /* gdk/quartz/gdkdnd-quartz.c */
71 struct _GtkDragSourceSite
73 GdkModifierType start_button_mask;
74 GtkTargetList *target_list; /* Targets for drag data */
75 GdkDragAction actions; /* Possible actions */
78 GtkImageType icon_type;
81 GtkImagePixmapData pixmap;
82 GtkImagePixbufData pixbuf;
83 GtkImageStockData stock;
84 GtkImageIconNameData name;
88 GdkColormap *colormap; /* Colormap for drag icon */
90 /* Stored button press information to detect drag beginning */
95 struct _GtkDragSourceInfo
97 GtkWidget *source_widget;
99 GtkTargetList *target_list; /* Targets for drag data */
100 GdkDragAction possible_actions; /* Actions allowed by source */
101 GdkDragContext *context; /* drag context */
102 NSEvent *nsevent; /* what started it */
103 gint hot_x, hot_y; /* Hot spot for drag */
104 GdkPixbuf *icon_pixbuf;
109 struct _GtkDragDestSite
111 GtkDestDefaults flags;
112 GtkTargetList *target_list;
113 GdkDragAction actions;
115 guint track_motion : 1;
118 struct _GtkDragDestInfo
120 GtkWidget *widget; /* Widget in which drag is in */
121 GdkDragContext *context; /* Drag context */
122 guint dropped : 1; /* Set after we receive a drop */
123 gint drop_x, drop_y; /* Position of drop */
126 struct _GtkDragFindData
130 GdkDragContext *context;
131 GtkDragDestInfo *info;
134 gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
135 gint x, gint y, guint32 time);
140 @interface GtkDragSourceOwner : NSObject {
141 GtkDragSourceInfo *info;
146 @implementation GtkDragSourceOwner
147 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
150 GtkSelectionData selection_data;
152 selection_data.selection = GDK_NONE;
153 selection_data.data = NULL;
154 selection_data.length = -1;
155 selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
157 if (gtk_target_list_find (info->target_list,
158 selection_data.target,
161 g_signal_emit_by_name (info->widget, "drag-data-get",
167 if (selection_data.length >= 0)
168 _gtk_quartz_set_selection_data_for_pasteboard (sender, &selection_data);
170 g_free (selection_data.data);
174 - (id)initWithInfo:(GtkDragSourceInfo *)anInfo
189 gtk_drag_get_data (GtkWidget *widget,
190 GdkDragContext *context,
194 id <NSDraggingInfo> dragging_info;
195 NSPasteboard *pasteboard;
196 GtkSelectionData *selection_data;
197 GtkDragDestInfo *info;
198 GtkDragDestSite *site;
200 dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
201 pasteboard = [dragging_info draggingPasteboard];
203 info = gtk_drag_get_dest_info (context, FALSE);
204 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
206 selection_data = _gtk_quartz_get_selection_data_from_pasteboard (pasteboard,
209 if (site && site->target_list)
213 if (gtk_target_list_find (site->target_list,
214 selection_data->target,
217 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
218 selection_data->length >= 0)
219 g_signal_emit_by_name (widget,
220 "drag-data-received",
221 context, info->drop_x, info->drop_y,
228 g_signal_emit_by_name (widget,
229 "drag-data-received",
230 context, info->drop_x, info->drop_y,
235 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
237 gtk_drag_finish (context,
238 (selection_data->length >= 0),
239 (context->action == GDK_ACTION_MOVE),
245 gtk_drag_finish (GdkDragContext *context,
250 GtkDragSourceInfo *info;
251 GdkDragContext* source_context = gdk_quartz_drag_source_context ();
255 info = gtk_drag_get_source_info (source_context, FALSE);
258 info->success = success;
265 gtk_drag_dest_info_destroy (gpointer data)
267 GtkDragDestInfo *info = data;
272 static GtkDragDestInfo *
273 gtk_drag_get_dest_info (GdkDragContext *context,
276 GtkDragDestInfo *info;
277 static GQuark info_quark = 0;
279 info_quark = g_quark_from_static_string ("gtk-dest-info");
281 info = g_object_get_qdata (G_OBJECT (context), info_quark);
284 info = g_new (GtkDragDestInfo, 1);
286 info->context = context;
287 info->dropped = FALSE;
288 g_object_set_qdata_full (G_OBJECT (context), info_quark,
289 info, gtk_drag_dest_info_destroy);
295 static GQuark dest_info_quark = 0;
297 static GtkDragSourceInfo *
298 gtk_drag_get_source_info (GdkDragContext *context,
301 GtkDragSourceInfo *info;
303 if (!dest_info_quark)
304 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
306 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
309 info = g_new0 (GtkDragSourceInfo, 1);
310 info->context = context;
311 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
318 gtk_drag_clear_source_info (GdkDragContext *context)
320 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
324 gtk_drag_get_source_widget (GdkDragContext *context)
326 GtkDragSourceInfo *info;
327 GdkDragContext* real_source_context = gdk_quartz_drag_source_context();
329 if (!real_source_context)
332 info = gtk_drag_get_source_info (real_source_context, FALSE);
336 return info->source_widget;
339 /*************************************************************
340 * gtk_drag_highlight_expose:
341 * Callback for expose_event for highlighted widgets.
347 *************************************************************/
350 gtk_drag_highlight_expose (GtkWidget *widget,
351 GdkEventExpose *event,
354 gint x, y, width, height;
356 if (GTK_WIDGET_DRAWABLE (widget))
360 if (GTK_WIDGET_NO_WINDOW (widget))
362 x = widget->allocation.x;
363 y = widget->allocation.y;
364 width = widget->allocation.width;
365 height = widget->allocation.height;
371 gdk_drawable_get_size (widget->window, &width, &height);
374 gtk_paint_shadow (widget->style, widget->window,
375 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
377 x, y, width, height);
379 cr = gdk_cairo_create (widget->window);
380 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
381 cairo_set_line_width (cr, 1.0);
384 width - 1, height - 1);
392 /*************************************************************
393 * gtk_drag_highlight:
394 * Highlight the given widget in the default manner.
398 *************************************************************/
401 gtk_drag_highlight (GtkWidget *widget)
403 g_return_if_fail (GTK_IS_WIDGET (widget));
405 g_signal_connect_after (widget, "expose-event",
406 G_CALLBACK (gtk_drag_highlight_expose),
409 gtk_widget_queue_draw (widget);
412 /*************************************************************
413 * gtk_drag_unhighlight:
414 * Refresh the given widget to remove the highlight.
418 *************************************************************/
421 gtk_drag_unhighlight (GtkWidget *widget)
423 g_return_if_fail (GTK_IS_WIDGET (widget));
425 g_signal_handlers_disconnect_by_func (widget,
426 gtk_drag_highlight_expose,
429 gtk_widget_queue_draw (widget);
433 get_toplevel_nswindow (GtkWidget *widget)
435 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
437 if (GTK_WIDGET_TOPLEVEL (toplevel) && toplevel->window)
438 return [gdk_quartz_window_get_nsview (toplevel->window) window];
444 register_types (GtkWidget *widget, GtkDragDestSite *site)
446 if (site->target_list)
448 NSWindow *nswindow = get_toplevel_nswindow (widget);
450 NSAutoreleasePool *pool;
455 pool = [[NSAutoreleasePool alloc] init];
456 types = _gtk_quartz_target_list_to_pasteboard_types (site->target_list);
458 [nswindow registerForDraggedTypes:types];
464 gtk_drag_dest_realized (GtkWidget *widget,
467 GtkDragDestSite *site = user_data;
469 register_types (widget, site);
473 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
474 GtkWidget *previous_toplevel,
477 GtkDragDestSite *site = user_data;
479 register_types (widget, site);
483 gtk_drag_dest_site_destroy (gpointer data)
485 GtkDragDestSite *site = data;
487 if (site->target_list)
488 gtk_target_list_unref (site->target_list);
494 gtk_drag_dest_set (GtkWidget *widget,
495 GtkDestDefaults flags,
496 const GtkTargetEntry *targets,
498 GdkDragAction actions)
500 GtkDragDestSite *old_site, *site;
502 g_return_if_fail (GTK_IS_WIDGET (widget));
504 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
506 gtk_drag_dest_unset (widget);
508 site = g_new (GtkDragDestSite, 1);
510 site->have_drag = FALSE;
512 site->target_list = gtk_target_list_new (targets, n_targets);
514 site->target_list = NULL;
515 site->actions = actions;
518 site->track_motion = old_site->track_motion;
520 site->track_motion = FALSE;
522 if (GTK_WIDGET_REALIZED (widget))
523 gtk_drag_dest_realized (widget, site);
525 g_signal_connect (widget, "realize",
526 G_CALLBACK (gtk_drag_dest_realized), site);
527 g_signal_connect (widget, "hierarchy-changed",
528 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
530 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
531 site, gtk_drag_dest_site_destroy);
535 gtk_drag_dest_set_proxy (GtkWidget *widget,
536 GdkWindow *proxy_window,
537 GdkDragProtocol protocol,
538 gboolean use_coordinates)
540 g_warning ("gtk_drag_dest_set_proxy is not supported on Mac OS X.");
544 gtk_drag_dest_unset (GtkWidget *widget)
546 GtkDragDestSite *old_site;
548 g_return_if_fail (GTK_IS_WIDGET (widget));
550 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
553 g_signal_handlers_disconnect_by_func (widget,
554 gtk_drag_dest_realized,
556 g_signal_handlers_disconnect_by_func (widget,
557 gtk_drag_dest_hierarchy_changed,
561 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
565 gtk_drag_dest_get_target_list (GtkWidget *widget)
567 GtkDragDestSite *site;
569 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
571 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
573 return site ? site->target_list : NULL;
577 gtk_drag_dest_set_target_list (GtkWidget *widget,
578 GtkTargetList *target_list)
580 GtkDragDestSite *site;
582 g_return_if_fail (GTK_IS_WIDGET (widget));
584 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
588 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
589 "to make the widget into a drag destination");
594 gtk_target_list_ref (target_list);
596 if (site->target_list)
597 gtk_target_list_unref (site->target_list);
599 site->target_list = target_list;
601 register_types (widget, site);
605 gtk_drag_dest_add_text_targets (GtkWidget *widget)
607 GtkTargetList *target_list;
609 target_list = gtk_drag_dest_get_target_list (widget);
611 gtk_target_list_ref (target_list);
613 target_list = gtk_target_list_new (NULL, 0);
614 gtk_target_list_add_text_targets (target_list, 0);
615 gtk_drag_dest_set_target_list (widget, target_list);
616 gtk_target_list_unref (target_list);
620 gtk_drag_dest_add_image_targets (GtkWidget *widget)
622 GtkTargetList *target_list;
624 target_list = gtk_drag_dest_get_target_list (widget);
626 gtk_target_list_ref (target_list);
628 target_list = gtk_target_list_new (NULL, 0);
629 gtk_target_list_add_image_targets (target_list, 0, FALSE);
630 gtk_drag_dest_set_target_list (widget, target_list);
631 gtk_target_list_unref (target_list);
635 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
637 GtkTargetList *target_list;
639 target_list = gtk_drag_dest_get_target_list (widget);
641 gtk_target_list_ref (target_list);
643 target_list = gtk_target_list_new (NULL, 0);
644 gtk_target_list_add_uri_targets (target_list, 0);
645 gtk_drag_dest_set_target_list (widget, target_list);
646 gtk_target_list_unref (target_list);
650 prepend_and_ref_widget (GtkWidget *widget,
653 GSList **slist_p = data;
655 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
659 gtk_drag_find_widget (GtkWidget *widget,
660 GtkDragFindData *data)
662 GtkAllocation new_allocation;
663 gint allocation_to_window_x = 0;
664 gint allocation_to_window_y = 0;
668 if (data->found || !GTK_WIDGET_MAPPED (widget) || !GTK_WIDGET_SENSITIVE (widget))
671 /* Note that in the following code, we only count the
672 * position as being inside a WINDOW widget if it is inside
673 * widget->window; points that are outside of widget->window
674 * but within the allocation are not counted. This is consistent
675 * with the way we highlight drag targets.
677 * data->x,y are relative to widget->parent->window (if
678 * widget is not a toplevel, widget->window otherwise).
679 * We compute the allocation of widget in the same coordinates,
680 * clipping to widget->window, and all intermediate
681 * windows. If data->x,y is inside that, then we translate
682 * our coordinates to be relative to widget->window and
685 new_allocation = widget->allocation;
690 GdkWindow *window = widget->window;
692 /* Compute the offset from allocation-relative to
693 * window-relative coordinates.
695 allocation_to_window_x = widget->allocation.x;
696 allocation_to_window_y = widget->allocation.y;
698 if (!GTK_WIDGET_NO_WINDOW (widget))
700 /* The allocation is relative to the parent window for
701 * window widgets, not to widget->window.
703 gdk_window_get_position (window, &tx, &ty);
705 allocation_to_window_x -= tx;
706 allocation_to_window_y -= ty;
709 new_allocation.x = 0 + allocation_to_window_x;
710 new_allocation.y = 0 + allocation_to_window_y;
712 while (window && window != widget->parent->window)
714 GdkRectangle window_rect = { 0, 0, 0, 0 };
716 gdk_drawable_get_size (window, &window_rect.width, &window_rect.height);
718 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
720 gdk_window_get_position (window, &tx, &ty);
721 new_allocation.x += tx;
723 new_allocation.y += ty;
726 window = gdk_window_get_parent (window);
729 if (!window) /* Window and widget heirarchies didn't match. */
733 if (data->toplevel ||
734 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
735 (data->x < new_allocation.x + new_allocation.width) &&
736 (data->y < new_allocation.y + new_allocation.height)))
738 /* First, check if the drag is in a valid drop site in
739 * one of our children
741 if (GTK_IS_CONTAINER (widget))
743 GtkDragFindData new_data = *data;
744 GSList *children = NULL;
747 new_data.x -= x_offset;
748 new_data.y -= y_offset;
749 new_data.found = FALSE;
750 new_data.toplevel = FALSE;
752 /* need to reference children temporarily in case the
753 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
755 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
756 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
758 if (!new_data.found && GTK_WIDGET_DRAWABLE (tmp_list->data))
759 gtk_drag_find_widget (tmp_list->data, &new_data);
760 g_object_unref (tmp_list->data);
762 g_slist_free (children);
764 data->found = new_data.found;
767 /* If not, and this widget is registered as a drop site, check to
768 * emit "drag-motion" to check if we are actually in
772 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
774 data->found = data->callback (widget,
776 data->x - x_offset - allocation_to_window_x,
777 data->y - y_offset - allocation_to_window_y,
779 /* If so, send a "drag-leave" to the last widget */
782 if (data->info->widget && data->info->widget != widget)
784 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
786 data->info->widget = widget;
793 gtk_drag_dest_leave (GtkWidget *widget,
794 GdkDragContext *context,
797 GtkDragDestSite *site;
799 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
800 g_return_if_fail (site != NULL);
802 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
803 gtk_drag_unhighlight (widget);
805 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
807 g_signal_emit_by_name (widget, "drag-leave", context, time);
809 site->have_drag = FALSE;
813 gtk_drag_dest_motion (GtkWidget *widget,
814 GdkDragContext *context,
819 GtkDragDestSite *site;
820 GdkDragAction action = 0;
823 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
824 g_return_val_if_fail (site != NULL, FALSE);
826 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
828 if (context->suggested_action & site->actions)
829 action = context->suggested_action;
831 if (action && gtk_drag_dest_find_target (widget, context, NULL))
833 if (!site->have_drag)
835 site->have_drag = TRUE;
836 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
837 gtk_drag_highlight (widget);
840 gdk_drag_status (context, action, time);
844 gdk_drag_status (context, 0, time);
845 if (!site->track_motion)
850 g_signal_emit_by_name (widget, "drag-motion",
851 context, x, y, time, &retval);
853 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
857 gtk_drag_dest_drop (GtkWidget *widget,
858 GdkDragContext *context,
863 GtkDragDestSite *site;
864 GtkDragDestInfo *info;
867 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
868 g_return_val_if_fail (site != NULL, FALSE);
870 info = gtk_drag_get_dest_info (context, FALSE);
871 g_return_val_if_fail (info != NULL, FALSE);
876 if (site->flags & GTK_DEST_DEFAULT_DROP)
878 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
880 if (target == GDK_NONE)
882 gtk_drag_finish (context, FALSE, FALSE, time);
886 gtk_drag_get_data (widget, context, target, time);
889 g_signal_emit_by_name (widget, "drag-drop",
890 context, x, y, time, &retval);
892 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
896 gtk_drag_dest_set_track_motion (GtkWidget *widget,
897 gboolean track_motion)
899 GtkDragDestSite *site;
901 g_return_if_fail (GTK_IS_WIDGET (widget));
903 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
905 g_return_if_fail (site != NULL);
907 site->track_motion = track_motion != FALSE;
911 gtk_drag_dest_get_track_motion (GtkWidget *widget)
913 GtkDragDestSite *site;
915 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
917 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
920 return site->track_motion;
926 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
929 GtkDragDestInfo *info;
930 GdkDragContext *context;
932 g_return_if_fail (toplevel != NULL);
933 g_return_if_fail (event != NULL);
935 context = event->dnd.context;
937 info = gtk_drag_get_dest_info (context, TRUE);
939 /* Find the widget for the event */
948 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
953 case GDK_DRAG_MOTION:
956 GtkDragFindData data;
959 if (event->type == GDK_DROP_START)
961 info->dropped = TRUE;
962 /* We send a leave here so that the widget unhighlights
967 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
972 gdk_window_get_position (toplevel->window, &tx, &ty);
974 data.x = event->dnd.x_root - tx;
975 data.y = event->dnd.y_root - ty;
976 data.context = context;
979 data.toplevel = TRUE;
980 data.callback = (event->type == GDK_DRAG_MOTION) ?
981 gtk_drag_dest_motion : gtk_drag_dest_drop;
982 data.time = event->dnd.time;
984 gtk_drag_find_widget (toplevel, &data);
986 if (info->widget && !data.found)
988 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
994 if (event->type == GDK_DRAG_MOTION)
997 gdk_drag_status (context, 0, event->dnd.time);
1002 g_assert_not_reached ();
1009 gtk_drag_dest_find_target (GtkWidget *widget,
1010 GdkDragContext *context,
1011 GtkTargetList *target_list)
1013 id <NSDraggingInfo> dragging_info;
1014 NSPasteboard *pasteboard;
1015 GtkWidget *source_widget;
1017 GList *tmp_source = NULL;
1018 GList *source_targets;
1020 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1021 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1022 g_return_val_if_fail (!context->is_source, GDK_NONE);
1024 dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
1025 pasteboard = [dragging_info draggingPasteboard];
1027 source_widget = gtk_drag_get_source_widget (context);
1029 if (target_list == NULL)
1030 target_list = gtk_drag_dest_get_target_list (widget);
1032 if (target_list == NULL)
1035 source_targets = _gtk_quartz_pasteboard_types_to_atom_list ([pasteboard types]);
1036 tmp_target = target_list->list;
1039 GtkTargetPair *pair = tmp_target->data;
1040 tmp_source = source_targets;
1043 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1045 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1046 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
1048 g_list_free (source_targets);
1049 return pair->target;
1054 tmp_source = tmp_source->next;
1056 tmp_target = tmp_target->next;
1059 g_list_free (source_targets);
1064 gtk_drag_begin_idle (gpointer arg)
1066 GdkDragContext* context = (GdkDragContext*) arg;
1067 GtkDragSourceInfo* info = gtk_drag_get_source_info (context, FALSE);
1069 NSPasteboard *pasteboard;
1070 GtkDragSourceOwner *owner;
1073 g_assert (info != NULL);
1075 pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
1076 owner = [[GtkDragSourceOwner alloc] initWithInfo:info];
1078 [pasteboard declareTypes:_gtk_quartz_target_list_to_pasteboard_types (info->target_list) owner:owner];
1080 if ((nswindow = get_toplevel_nswindow (info->source_widget)) == NULL)
1083 /* Ref the context. It's unreffed when the drag has been aborted */
1084 g_object_ref (info->context);
1086 /* FIXME: If the event isn't a mouse event, use the global cursor position instead */
1087 point = [info->nsevent locationInWindow];
1089 [nswindow dragImage:_gtk_quartz_create_image_from_pixbuf (info->icon_pixbuf)
1091 offset:NSMakeSize(0, 0)
1093 pasteboard:pasteboard
1097 [info->nsevent release];
1102 static GdkDragContext *
1103 gtk_drag_begin_internal (GtkWidget *widget,
1104 GtkDragSourceSite *site,
1105 GtkTargetList *target_list,
1106 GdkDragAction actions,
1110 GtkDragSourceInfo *info;
1111 GdkDragContext *context;
1114 context = gdk_drag_begin (NULL, NULL);
1115 context->is_source = TRUE;
1117 info = gtk_drag_get_source_info (context, TRUE);
1119 info->source_widget = g_object_ref (widget);
1120 info->widget = g_object_ref (widget);
1121 info->target_list = target_list;
1122 gtk_target_list_ref (target_list);
1124 info->possible_actions = actions;
1126 g_signal_emit_by_name (widget, "drag-begin", info->context);
1128 /* Ensure that we have an icon before we start the drag; the
1129 * application may have set one in ::drag_begin, or it may
1132 if (!info->icon_pixbuf)
1134 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
1135 gtk_drag_set_icon_default (context);
1137 switch (site->icon_type)
1139 case GTK_IMAGE_PIXMAP:
1140 /* This is not supported, so just set a small transparent pixbuf
1141 * since we need to have something.
1144 gtk_drag_set_icon_pixmap (context,
1146 site->icon_data.pixmap.pixmap,
1153 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 1, 1);
1154 gdk_pixbuf_fill (pixbuf, 0xffffff);
1156 gtk_drag_set_icon_pixbuf (context,
1160 g_object_unref (pixbuf);
1163 case GTK_IMAGE_PIXBUF:
1164 gtk_drag_set_icon_pixbuf (context,
1165 site->icon_data.pixbuf.pixbuf,
1168 case GTK_IMAGE_STOCK:
1169 gtk_drag_set_icon_stock (context,
1170 site->icon_data.stock.stock_id,
1173 case GTK_IMAGE_ICON_NAME:
1174 gtk_drag_set_icon_name (context,
1175 site->icon_data.name.icon_name,
1178 case GTK_IMAGE_EMPTY:
1180 g_assert_not_reached();
1185 nswindow = get_toplevel_nswindow (widget);
1186 info->nsevent = [nswindow currentEvent];
1187 [info->nsevent retain];
1189 /* drag will begin in an idle handler to avoid nested run loops */
1191 g_idle_add_full (G_PRIORITY_HIGH_IDLE, gtk_drag_begin_idle, context, NULL);
1193 gdk_pointer_ungrab (0);
1199 gtk_drag_begin (GtkWidget *widget,
1200 GtkTargetList *targets,
1201 GdkDragAction actions,
1205 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1206 g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), NULL);
1207 g_return_val_if_fail (targets != NULL, NULL);
1209 return gtk_drag_begin_internal (widget, NULL, targets,
1210 actions, button, event);
1215 gtk_drag_source_event_cb (GtkWidget *widget,
1219 GtkDragSourceSite *site;
1220 gboolean retval = FALSE;
1221 site = (GtkDragSourceSite *)data;
1223 switch (event->type)
1225 case GDK_BUTTON_PRESS:
1226 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1228 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
1229 site->x = event->button.x;
1230 site->y = event->button.y;
1234 case GDK_BUTTON_RELEASE:
1235 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1236 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
1239 case GDK_MOTION_NOTIFY:
1240 if (site->state & event->motion.state & site->start_button_mask)
1242 /* FIXME: This is really broken and can leave us
1248 if (site->state & event->motion.state &
1249 GDK_BUTTON1_MASK << (i - 1))
1253 if (gtk_drag_check_threshold (widget, site->x, site->y,
1254 event->motion.x, event->motion.y))
1257 gtk_drag_begin_internal (widget, site, site->target_list,
1266 default: /* hit for 2/3BUTTON_PRESS */
1274 gtk_drag_source_set (GtkWidget *widget,
1275 GdkModifierType start_button_mask,
1276 const GtkTargetEntry *targets,
1278 GdkDragAction actions)
1280 GtkDragSourceSite *site;
1282 g_return_if_fail (GTK_IS_WIDGET (widget));
1284 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1286 gtk_widget_add_events (widget,
1287 gtk_widget_get_events (widget) |
1288 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1289 GDK_BUTTON_MOTION_MASK);
1293 if (site->target_list)
1294 gtk_target_list_unref (site->target_list);
1298 site = g_new0 (GtkDragSourceSite, 1);
1300 site->icon_type = GTK_IMAGE_EMPTY;
1302 g_signal_connect (widget, "button-press-event",
1303 G_CALLBACK (gtk_drag_source_event_cb),
1305 g_signal_connect (widget, "button-release-event",
1306 G_CALLBACK (gtk_drag_source_event_cb),
1308 g_signal_connect (widget, "motion-notify-event",
1309 G_CALLBACK (gtk_drag_source_event_cb),
1312 g_object_set_data_full (G_OBJECT (widget),
1313 I_("gtk-site-data"),
1314 site, gtk_drag_source_site_destroy);
1317 site->start_button_mask = start_button_mask;
1319 site->target_list = gtk_target_list_new (targets, n_targets);
1321 site->actions = actions;
1324 /*************************************************************
1325 * gtk_drag_source_unset
1326 * Unregister this widget as a drag source.
1330 *************************************************************/
1333 gtk_drag_source_unset (GtkWidget *widget)
1335 GtkDragSourceSite *site;
1337 g_return_if_fail (GTK_IS_WIDGET (widget));
1339 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1343 g_signal_handlers_disconnect_by_func (widget,
1344 gtk_drag_source_event_cb,
1346 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
1351 gtk_drag_source_get_target_list (GtkWidget *widget)
1353 GtkDragSourceSite *site;
1355 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1357 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1359 return site ? site->target_list : NULL;
1364 gtk_drag_source_set_target_list (GtkWidget *widget,
1365 GtkTargetList *target_list)
1367 GtkDragSourceSite *site;
1369 g_return_if_fail (GTK_IS_WIDGET (widget));
1371 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1374 g_warning ("gtk_drag_source_set_target_list() requires the widget "
1375 "to already be a drag source.");
1380 gtk_target_list_ref (target_list);
1382 if (site->target_list)
1383 gtk_target_list_unref (site->target_list);
1385 site->target_list = target_list;
1389 * gtk_drag_source_add_text_targets:
1390 * @widget: a #GtkWidget that's is a drag source
1392 * Add the text targets supported by #GtkSelection to
1393 * the target list of the drag source. The targets
1394 * are added with @info = 0. If you need another value,
1395 * use gtk_target_list_add_text_targets() and
1396 * gtk_drag_source_set_target_list().
1401 gtk_drag_source_add_text_targets (GtkWidget *widget)
1403 GtkTargetList *target_list;
1405 target_list = gtk_drag_source_get_target_list (widget);
1407 gtk_target_list_ref (target_list);
1409 target_list = gtk_target_list_new (NULL, 0);
1410 gtk_target_list_add_text_targets (target_list, 0);
1411 gtk_drag_source_set_target_list (widget, target_list);
1412 gtk_target_list_unref (target_list);
1416 gtk_drag_source_add_image_targets (GtkWidget *widget)
1418 GtkTargetList *target_list;
1420 target_list = gtk_drag_source_get_target_list (widget);
1422 gtk_target_list_ref (target_list);
1424 target_list = gtk_target_list_new (NULL, 0);
1425 gtk_target_list_add_image_targets (target_list, 0, TRUE);
1426 gtk_drag_source_set_target_list (widget, target_list);
1427 gtk_target_list_unref (target_list);
1431 gtk_drag_source_add_uri_targets (GtkWidget *widget)
1433 GtkTargetList *target_list;
1435 target_list = gtk_drag_source_get_target_list (widget);
1437 gtk_target_list_ref (target_list);
1439 target_list = gtk_target_list_new (NULL, 0);
1440 gtk_target_list_add_uri_targets (target_list, 0);
1441 gtk_drag_source_set_target_list (widget, target_list);
1442 gtk_target_list_unref (target_list);
1446 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
1448 switch (site->icon_type)
1450 case GTK_IMAGE_EMPTY:
1452 case GTK_IMAGE_PIXMAP:
1453 if (site->icon_data.pixmap.pixmap)
1454 g_object_unref (site->icon_data.pixmap.pixmap);
1455 if (site->icon_mask)
1456 g_object_unref (site->icon_mask);
1458 case GTK_IMAGE_PIXBUF:
1459 g_object_unref (site->icon_data.pixbuf.pixbuf);
1461 case GTK_IMAGE_STOCK:
1462 g_free (site->icon_data.stock.stock_id);
1464 case GTK_IMAGE_ICON_NAME:
1465 g_free (site->icon_data.name.icon_name);
1468 g_assert_not_reached();
1471 site->icon_type = GTK_IMAGE_EMPTY;
1474 g_object_unref (site->colormap);
1475 site->colormap = NULL;
1479 gtk_drag_source_site_destroy (gpointer data)
1481 GtkDragSourceSite *site = data;
1483 if (site->target_list)
1484 gtk_target_list_unref (site->target_list);
1486 gtk_drag_source_unset_icon (site);
1491 gtk_drag_source_set_icon (GtkWidget *widget,
1492 GdkColormap *colormap,
1496 GtkDragSourceSite *site;
1498 g_return_if_fail (GTK_IS_WIDGET (widget));
1499 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1500 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
1501 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
1503 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1504 g_return_if_fail (site != NULL);
1506 g_object_ref (colormap);
1507 g_object_ref (pixmap);
1509 g_object_ref (mask);
1511 gtk_drag_source_unset_icon (site);
1513 site->icon_type = GTK_IMAGE_PIXMAP;
1515 site->icon_data.pixmap.pixmap = pixmap;
1516 site->icon_mask = mask;
1517 site->colormap = colormap;
1521 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
1524 GtkDragSourceSite *site;
1526 g_return_if_fail (GTK_IS_WIDGET (widget));
1527 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1529 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1530 g_return_if_fail (site != NULL);
1531 g_object_ref (pixbuf);
1533 gtk_drag_source_unset_icon (site);
1535 site->icon_type = GTK_IMAGE_PIXBUF;
1536 site->icon_data.pixbuf.pixbuf = pixbuf;
1540 * gtk_drag_source_set_icon_stock:
1541 * @widget: a #GtkWidget
1542 * @stock_id: the ID of the stock icon to use
1544 * Sets the icon that will be used for drags from a particular source
1548 gtk_drag_source_set_icon_stock (GtkWidget *widget,
1549 const gchar *stock_id)
1551 GtkDragSourceSite *site;
1553 g_return_if_fail (GTK_IS_WIDGET (widget));
1554 g_return_if_fail (stock_id != NULL);
1556 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1557 g_return_if_fail (site != NULL);
1559 gtk_drag_source_unset_icon (site);
1561 site->icon_type = GTK_IMAGE_STOCK;
1562 site->icon_data.stock.stock_id = g_strdup (stock_id);
1566 * gtk_drag_source_set_icon_name:
1567 * @widget: a #GtkWidget
1568 * @icon_name: name of icon to use
1570 * Sets the icon that will be used for drags from a particular source
1571 * to a themed icon. See the docs for #GtkIconTheme for more details.
1576 gtk_drag_source_set_icon_name (GtkWidget *widget,
1577 const gchar *icon_name)
1579 GtkDragSourceSite *site;
1581 g_return_if_fail (GTK_IS_WIDGET (widget));
1582 g_return_if_fail (icon_name != NULL);
1584 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1585 g_return_if_fail (site != NULL);
1587 gtk_drag_source_unset_icon (site);
1589 site->icon_type = GTK_IMAGE_ICON_NAME;
1590 site->icon_data.name.icon_name = g_strdup (icon_name);
1595 * gtk_drag_set_icon_widget:
1596 * @context: the context for a drag. (This must be called
1597 with a context for the source side of a drag)
1598 * @widget: a toplevel window to use as an icon.
1599 * @hot_x: the X offset within @widget of the hotspot.
1600 * @hot_y: the Y offset within @widget of the hotspot.
1602 * Changes the icon for a widget to a given widget. GTK+
1603 * will not destroy the icon, so if you don't want
1604 * it to persist, you should connect to the "drag-end"
1605 * signal and destroy it yourself.
1608 gtk_drag_set_icon_widget (GdkDragContext *context,
1613 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1614 g_return_if_fail (context->is_source);
1615 g_return_if_fail (GTK_IS_WIDGET (widget));
1617 g_warning ("gtk_drag_set_icon_widget is not supported on Mac OS X");
1621 set_icon_stock_pixbuf (GdkDragContext *context,
1622 const gchar *stock_id,
1627 GtkDragSourceInfo *info;
1629 info = gtk_drag_get_source_info (context, FALSE);
1633 pixbuf = gtk_widget_render_icon (info->widget, stock_id,
1634 GTK_ICON_SIZE_DND, NULL);
1638 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
1643 g_object_ref (pixbuf);
1645 if (info->icon_pixbuf)
1646 g_object_unref (info->icon_pixbuf);
1647 info->icon_pixbuf = pixbuf;
1648 info->hot_x = hot_x;
1649 info->hot_y = hot_y;
1653 * gtk_drag_set_icon_pixbuf:
1654 * @context: the context for a drag. (This must be called
1655 * with a context for the source side of a drag)
1656 * @pixbuf: the #GdkPixbuf to use as the drag icon.
1657 * @hot_x: the X offset within @widget of the hotspot.
1658 * @hot_y: the Y offset within @widget of the hotspot.
1660 * Sets @pixbuf as the icon for a given drag.
1663 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
1668 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1669 g_return_if_fail (context->is_source);
1670 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1672 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1676 * gtk_drag_set_icon_stock:
1677 * @context: the context for a drag. (This must be called
1678 * with a context for the source side of a drag)
1679 * @stock_id: the ID of the stock icon to use for the drag.
1680 * @hot_x: the X offset within the icon of the hotspot.
1681 * @hot_y: the Y offset within the icon of the hotspot.
1683 * Sets the icon for a given drag from a stock ID.
1686 gtk_drag_set_icon_stock (GdkDragContext *context,
1687 const gchar *stock_id,
1692 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1693 g_return_if_fail (context->is_source);
1694 g_return_if_fail (stock_id != NULL);
1696 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
1700 * gtk_drag_set_icon_pixmap:
1701 * @context: the context for a drag. (This must be called
1702 * with a context for the source side of a drag)
1703 * @colormap: the colormap of the icon
1704 * @pixmap: the image data for the icon
1705 * @mask: the transparency mask for the icon
1706 * @hot_x: the X offset within @pixmap of the hotspot.
1707 * @hot_y: the Y offset within @pixmap of the hotspot.
1709 * Sets @pixmap as the icon for a given drag. GTK+ retains
1710 * references for the arguments, and will release them when
1711 * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
1712 * will be more convenient to use.
1715 gtk_drag_set_icon_pixmap (GdkDragContext *context,
1716 GdkColormap *colormap,
1724 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1725 g_return_if_fail (context->is_source);
1726 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1727 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
1729 pixbuf = gdk_pixbuf_get_from_drawable (NULL, pixmap, colormap,
1734 gtk_drag_set_icon_pixbuf (context, pixbuf, hot_x, hot_y);
1735 g_object_unref (pixbuf);
1739 * gtk_drag_set_icon_name:
1740 * @context: the context for a drag. (This must be called
1741 * with a context for the source side of a drag)
1742 * @icon_name: name of icon to use
1743 * @hot_x: the X offset of the hotspot within the icon
1744 * @hot_y: the Y offset of the hotspot within the icon
1746 * Sets the icon for a given drag from a named themed icon. See
1747 * the docs for #GtkIconTheme for more details. Note that the
1748 * size of the icon depends on the icon theme (the icon is
1749 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
1750 * @hot_x and @hot_y have to be used with care.
1755 gtk_drag_set_icon_name (GdkDragContext *context,
1756 const gchar *icon_name,
1761 GtkSettings *settings;
1762 GtkIconTheme *icon_theme;
1764 gint width, height, icon_size;
1766 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1767 g_return_if_fail (context->is_source);
1768 g_return_if_fail (icon_name != NULL);
1770 screen = gdk_drawable_get_screen (context->source_window);
1771 g_return_if_fail (screen != NULL);
1773 settings = gtk_settings_get_for_screen (screen);
1774 if (gtk_icon_size_lookup_for_settings (settings,
1777 icon_size = MAX (width, height);
1779 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
1781 icon_theme = gtk_icon_theme_get_for_screen (screen);
1783 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
1784 icon_size, 0, NULL);
1786 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1788 g_warning ("Cannot load drag icon from icon name %s", icon_name);
1792 * gtk_drag_set_icon_default:
1793 * @context: the context for a drag. (This must be called
1794 with a context for the source side of a drag)
1796 * Sets the icon for a particular drag to the default
1800 gtk_drag_set_icon_default (GdkDragContext *context)
1802 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1803 g_return_if_fail (context->is_source);
1805 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
1809 gtk_drag_set_default_icon (GdkColormap *colormap,
1815 g_warning ("gtk_drag_set_default_icon is not supported on Mac OS X.");
1819 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
1821 if (info->icon_pixbuf)
1822 g_object_unref (info->icon_pixbuf);
1824 g_signal_emit_by_name (info->widget, "drag-end",
1827 if (info->source_widget)
1828 g_object_unref (info->source_widget);
1831 g_object_unref (info->widget);
1833 gtk_target_list_unref (info->target_list);
1835 gtk_drag_clear_source_info (info->context);
1836 g_object_unref (info->context);
1842 drag_drop_finished_idle_cb (gpointer data)
1844 gtk_drag_source_info_destroy (data);
1849 gtk_drag_drop_finished (GtkDragSourceInfo *info)
1851 if (info->success && info->delete)
1852 g_signal_emit_by_name (info->source_widget, "drag-data-delete",
1855 /* Workaround for the fact that the NS API blocks until the drag is
1856 * over. This way the context is still valid when returning from
1857 * drag_begin, even if it will still be quite useless. See bug #501588.
1859 g_idle_add (drag_drop_finished_idle_cb, info);
1862 /*************************************************************
1863 * _gtk_drag_source_handle_event:
1864 * Called from widget event handling code on Drag events
1868 * toplevel: Toplevel widget that received the event
1871 *************************************************************/
1874 _gtk_drag_source_handle_event (GtkWidget *widget,
1877 GtkDragSourceInfo *info;
1878 GdkDragContext *context;
1880 g_return_if_fail (widget != NULL);
1881 g_return_if_fail (event != NULL);
1883 context = event->dnd.context;
1884 info = gtk_drag_get_source_info (context, FALSE);
1888 switch (event->type)
1890 case GDK_DROP_FINISHED:
1891 gtk_drag_drop_finished (info);
1894 g_assert_not_reached ();
1900 gtk_drag_check_threshold (GtkWidget *widget,
1906 gint drag_threshold;
1908 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1910 g_object_get (gtk_widget_get_settings (widget),
1911 "gtk-dnd-drag-threshold", &drag_threshold,
1914 return (ABS (current_x - start_x) > drag_threshold ||
1915 ABS (current_y - start_y) > drag_threshold);
1918 #define __GTK_DND_C__
1919 #include "gtkaliasdef.c"