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"
47 #include "gdk/quartz/gdkquartz.h"
49 typedef struct _GtkDragSourceSite GtkDragSourceSite;
50 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
51 typedef struct _GtkDragDestSite GtkDragDestSite;
52 typedef struct _GtkDragDestInfo GtkDragDestInfo;
53 typedef struct _GtkDragFindData GtkDragFindData;
55 static void gtk_drag_find_widget (GtkWidget *widget,
56 GtkDragFindData *data);
57 static void gtk_drag_dest_site_destroy (gpointer data);
58 static void gtk_drag_dest_leave (GtkWidget *widget,
59 GdkDragContext *context,
61 static GtkDragDestInfo *gtk_drag_get_dest_info (GdkDragContext *context,
63 static void gtk_drag_source_site_destroy (gpointer data);
65 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
68 extern GdkDragContext *gdk_quartz_drag_source_context (); /* gdk/quartz/gdkdnd-quartz.c */
70 struct _GtkDragSourceSite
72 GdkModifierType start_button_mask;
73 GtkTargetList *target_list; /* Targets for drag data */
74 GdkDragAction actions; /* Possible actions */
77 GtkImageType icon_type;
80 GtkImagePixbufData pixbuf;
81 GtkImageStockData stock;
82 GtkImageIconNameData name;
85 /* Stored button press information to detect drag beginning */
90 struct _GtkDragSourceInfo
92 GtkWidget *source_widget;
94 GtkTargetList *target_list; /* Targets for drag data */
95 GdkDragAction possible_actions; /* Actions allowed by source */
96 GdkDragContext *context; /* drag context */
97 NSEvent *nsevent; /* what started it */
98 gint hot_x, hot_y; /* Hot spot for drag */
99 GdkPixbuf *icon_pixbuf;
104 struct _GtkDragDestSite
106 GtkDestDefaults flags;
107 GtkTargetList *target_list;
108 GdkDragAction actions;
110 guint track_motion : 1;
113 struct _GtkDragDestInfo
115 GtkWidget *widget; /* Widget in which drag is in */
116 GdkDragContext *context; /* Drag context */
117 guint dropped : 1; /* Set after we receive a drop */
118 gint drop_x, drop_y; /* Position of drop */
121 struct _GtkDragFindData
125 GdkDragContext *context;
126 GtkDragDestInfo *info;
129 gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
130 gint x, gint y, guint32 time);
135 @interface GtkDragSourceOwner : NSObject {
136 GtkDragSourceInfo *info;
141 @implementation GtkDragSourceOwner
142 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
145 GtkSelectionData selection_data;
147 selection_data.selection = GDK_NONE;
148 selection_data.data = NULL;
149 selection_data.length = -1;
150 selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
152 if (gtk_target_list_find (info->target_list,
153 selection_data.target,
156 g_signal_emit_by_name (info->widget, "drag-data-get",
162 if (selection_data.length >= 0)
163 _gtk_quartz_set_selection_data_for_pasteboard (sender, &selection_data);
165 g_free (selection_data.data);
169 - (id)initWithInfo:(GtkDragSourceInfo *)anInfo
184 gtk_drag_get_data (GtkWidget *widget,
185 GdkDragContext *context,
189 id <NSDraggingInfo> dragging_info;
190 NSPasteboard *pasteboard;
191 GtkSelectionData *selection_data;
192 GtkDragDestInfo *info;
193 GtkDragDestSite *site;
195 dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
196 pasteboard = [dragging_info draggingPasteboard];
198 info = gtk_drag_get_dest_info (context, FALSE);
199 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
201 selection_data = _gtk_quartz_get_selection_data_from_pasteboard (pasteboard,
204 if (site && site->target_list)
208 if (gtk_target_list_find (site->target_list,
209 selection_data->target,
212 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
213 selection_data->length >= 0)
214 g_signal_emit_by_name (widget,
215 "drag-data-received",
216 context, info->drop_x, info->drop_y,
223 g_signal_emit_by_name (widget,
224 "drag-data-received",
225 context, info->drop_x, info->drop_y,
230 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
232 gtk_drag_finish (context,
233 (selection_data->length >= 0),
234 (context->action == GDK_ACTION_MOVE),
240 gtk_drag_finish (GdkDragContext *context,
245 GtkDragSourceInfo *info;
246 GdkDragContext* source_context = gdk_quartz_drag_source_context ();
250 info = gtk_drag_get_source_info (source_context, FALSE);
253 info->success = success;
260 gtk_drag_dest_info_destroy (gpointer data)
262 GtkDragDestInfo *info = data;
267 static GtkDragDestInfo *
268 gtk_drag_get_dest_info (GdkDragContext *context,
271 GtkDragDestInfo *info;
272 static GQuark info_quark = 0;
274 info_quark = g_quark_from_static_string ("gtk-dest-info");
276 info = g_object_get_qdata (G_OBJECT (context), info_quark);
279 info = g_new (GtkDragDestInfo, 1);
281 info->context = context;
282 info->dropped = FALSE;
283 g_object_set_qdata_full (G_OBJECT (context), info_quark,
284 info, gtk_drag_dest_info_destroy);
290 static GQuark dest_info_quark = 0;
292 static GtkDragSourceInfo *
293 gtk_drag_get_source_info (GdkDragContext *context,
296 GtkDragSourceInfo *info;
298 if (!dest_info_quark)
299 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
301 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
304 info = g_new0 (GtkDragSourceInfo, 1);
305 info->context = context;
306 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
313 gtk_drag_clear_source_info (GdkDragContext *context)
315 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
319 gtk_drag_get_source_widget (GdkDragContext *context)
321 GtkDragSourceInfo *info;
322 GdkDragContext* real_source_context = gdk_quartz_drag_source_context();
324 if (!real_source_context)
327 info = gtk_drag_get_source_info (real_source_context, FALSE);
331 return info->source_widget;
334 /*************************************************************
335 * gtk_drag_highlight_draw:
336 * Callback for expose_event for highlighted widgets.
342 *************************************************************/
345 gtk_drag_highlight_draw (GtkWidget *widget,
349 int width = gtk_widget_get_allocated_width (widget);
350 int height = gtk_widget_get_allocated_height (widget);
352 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
353 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
355 0, 0, width, height);
357 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
358 cairo_set_line_width (cr, 1.0);
361 width - 1, height - 1);
367 /*************************************************************
368 * gtk_drag_highlight:
369 * Highlight the given widget in the default manner.
373 *************************************************************/
376 gtk_drag_highlight (GtkWidget *widget)
378 g_return_if_fail (GTK_IS_WIDGET (widget));
380 g_signal_connect_after (widget, "draw",
381 G_CALLBACK (gtk_drag_highlight_draw),
384 gtk_widget_queue_draw (widget);
387 /*************************************************************
388 * gtk_drag_unhighlight:
389 * Refresh the given widget to remove the highlight.
393 *************************************************************/
396 gtk_drag_unhighlight (GtkWidget *widget)
398 g_return_if_fail (GTK_IS_WIDGET (widget));
400 g_signal_handlers_disconnect_by_func (widget,
401 gtk_drag_highlight_draw,
404 gtk_widget_queue_draw (widget);
408 get_toplevel_nswindow (GtkWidget *widget)
410 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
411 GdkWindow *window = gtk_widget_get_window (toplevel);
413 if (gtk_widget_is_toplevel (toplevel) && window)
414 return [gdk_quartz_window_get_nsview (window) window];
420 register_types (GtkWidget *widget, GtkDragDestSite *site)
422 if (site->target_list)
424 NSWindow *nswindow = get_toplevel_nswindow (widget);
426 NSAutoreleasePool *pool;
431 pool = [[NSAutoreleasePool alloc] init];
432 types = _gtk_quartz_target_list_to_pasteboard_types (site->target_list);
434 [nswindow registerForDraggedTypes:[types allObjects]];
442 gtk_drag_dest_realized (GtkWidget *widget,
445 GtkDragDestSite *site = user_data;
447 register_types (widget, site);
451 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
452 GtkWidget *previous_toplevel,
455 GtkDragDestSite *site = user_data;
457 register_types (widget, site);
461 gtk_drag_dest_site_destroy (gpointer data)
463 GtkDragDestSite *site = data;
465 if (site->target_list)
466 gtk_target_list_unref (site->target_list);
472 gtk_drag_dest_set (GtkWidget *widget,
473 GtkDestDefaults flags,
474 const GtkTargetEntry *targets,
476 GdkDragAction actions)
478 GtkDragDestSite *old_site, *site;
480 g_return_if_fail (GTK_IS_WIDGET (widget));
482 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
484 site = g_new (GtkDragDestSite, 1);
486 site->have_drag = FALSE;
488 site->target_list = gtk_target_list_new (targets, n_targets);
490 site->target_list = NULL;
491 site->actions = actions;
494 site->track_motion = old_site->track_motion;
496 site->track_motion = FALSE;
498 gtk_drag_dest_unset (widget);
500 if (gtk_widget_get_realized (widget))
501 gtk_drag_dest_realized (widget, site);
503 g_signal_connect (widget, "realize",
504 G_CALLBACK (gtk_drag_dest_realized), site);
505 g_signal_connect (widget, "hierarchy-changed",
506 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
508 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
509 site, gtk_drag_dest_site_destroy);
513 gtk_drag_dest_set_proxy (GtkWidget *widget,
514 GdkWindow *proxy_window,
515 GdkDragProtocol protocol,
516 gboolean use_coordinates)
518 g_warning ("gtk_drag_dest_set_proxy is not supported on Mac OS X.");
522 gtk_drag_dest_unset (GtkWidget *widget)
524 GtkDragDestSite *old_site;
526 g_return_if_fail (GTK_IS_WIDGET (widget));
528 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
531 g_signal_handlers_disconnect_by_func (widget,
532 gtk_drag_dest_realized,
534 g_signal_handlers_disconnect_by_func (widget,
535 gtk_drag_dest_hierarchy_changed,
539 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
543 gtk_drag_dest_get_target_list (GtkWidget *widget)
545 GtkDragDestSite *site;
547 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
549 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
551 return site ? site->target_list : NULL;
555 gtk_drag_dest_set_target_list (GtkWidget *widget,
556 GtkTargetList *target_list)
558 GtkDragDestSite *site;
560 g_return_if_fail (GTK_IS_WIDGET (widget));
562 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
566 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
567 "to make the widget into a drag destination");
572 gtk_target_list_ref (target_list);
574 if (site->target_list)
575 gtk_target_list_unref (site->target_list);
577 site->target_list = target_list;
579 register_types (widget, site);
583 gtk_drag_dest_add_text_targets (GtkWidget *widget)
585 GtkTargetList *target_list;
587 target_list = gtk_drag_dest_get_target_list (widget);
589 gtk_target_list_ref (target_list);
591 target_list = gtk_target_list_new (NULL, 0);
592 gtk_target_list_add_text_targets (target_list, 0);
593 gtk_drag_dest_set_target_list (widget, target_list);
594 gtk_target_list_unref (target_list);
598 gtk_drag_dest_add_image_targets (GtkWidget *widget)
600 GtkTargetList *target_list;
602 target_list = gtk_drag_dest_get_target_list (widget);
604 gtk_target_list_ref (target_list);
606 target_list = gtk_target_list_new (NULL, 0);
607 gtk_target_list_add_image_targets (target_list, 0, FALSE);
608 gtk_drag_dest_set_target_list (widget, target_list);
609 gtk_target_list_unref (target_list);
613 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
615 GtkTargetList *target_list;
617 target_list = gtk_drag_dest_get_target_list (widget);
619 gtk_target_list_ref (target_list);
621 target_list = gtk_target_list_new (NULL, 0);
622 gtk_target_list_add_uri_targets (target_list, 0);
623 gtk_drag_dest_set_target_list (widget, target_list);
624 gtk_target_list_unref (target_list);
628 prepend_and_ref_widget (GtkWidget *widget,
631 GSList **slist_p = data;
633 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
637 gtk_drag_find_widget (GtkWidget *widget,
638 GtkDragFindData *data)
640 GtkAllocation new_allocation;
641 gint allocation_to_window_x = 0;
642 gint allocation_to_window_y = 0;
646 if (data->found || !gtk_widget_get_mapped (widget) || !gtk_widget_get_sensitive (widget))
649 /* Note that in the following code, we only count the
650 * position as being inside a WINDOW widget if it is inside
651 * widget->window; points that are outside of widget->window
652 * but within the allocation are not counted. This is consistent
653 * with the way we highlight drag targets.
655 * data->x,y are relative to widget->parent->window (if
656 * widget is not a toplevel, widget->window otherwise).
657 * We compute the allocation of widget in the same coordinates,
658 * clipping to widget->window, and all intermediate
659 * windows. If data->x,y is inside that, then we translate
660 * our coordinates to be relative to widget->window and
663 gtk_widget_get_allocation (widget, &new_allocation);
665 if (gtk_widget_get_parent (widget))
668 GdkWindow *window = gtk_widget_get_window (widget);
669 GdkWindow *parent_window;
670 GtkAllocation allocation;
672 parent_window = gtk_widget_get_window (gtk_widget_get_parent (widget));
674 /* Compute the offset from allocation-relative to
675 * window-relative coordinates.
677 gtk_widget_get_allocation (widget, &allocation);
678 allocation_to_window_x = allocation.x;
679 allocation_to_window_y = allocation.y;
681 if (gtk_widget_get_has_window (widget))
683 /* The allocation is relative to the parent window for
684 * window widgets, not to widget->window.
686 gdk_window_get_position (window, &tx, &ty);
688 allocation_to_window_x -= tx;
689 allocation_to_window_y -= ty;
692 new_allocation.x = 0 + allocation_to_window_x;
693 new_allocation.y = 0 + allocation_to_window_y;
695 while (window && window != parent_window)
697 GdkRectangle window_rect = { 0, 0, 0, 0 };
699 window_rect.width = gdk_window_get_width (window);
700 window_rect.height = gdk_window_get_height (window);
702 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
704 gdk_window_get_position (window, &tx, &ty);
705 new_allocation.x += tx;
707 new_allocation.y += ty;
710 window = gdk_window_get_parent (window);
713 if (!window) /* Window and widget heirarchies didn't match. */
717 if (data->toplevel ||
718 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
719 (data->x < new_allocation.x + new_allocation.width) &&
720 (data->y < new_allocation.y + new_allocation.height)))
722 /* First, check if the drag is in a valid drop site in
723 * one of our children
725 if (GTK_IS_CONTAINER (widget))
727 GtkDragFindData new_data = *data;
728 GSList *children = NULL;
731 new_data.x -= x_offset;
732 new_data.y -= y_offset;
733 new_data.found = FALSE;
734 new_data.toplevel = FALSE;
736 /* need to reference children temporarily in case the
737 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
739 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
740 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
742 if (!new_data.found && gtk_widget_is_drawable (tmp_list->data))
743 gtk_drag_find_widget (tmp_list->data, &new_data);
744 g_object_unref (tmp_list->data);
746 g_slist_free (children);
748 data->found = new_data.found;
751 /* If not, and this widget is registered as a drop site, check to
752 * emit "drag-motion" to check if we are actually in
756 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
758 data->found = data->callback (widget,
760 data->x - x_offset - allocation_to_window_x,
761 data->y - y_offset - allocation_to_window_y,
763 /* If so, send a "drag-leave" to the last widget */
766 if (data->info->widget && data->info->widget != widget)
768 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
770 data->info->widget = widget;
777 gtk_drag_dest_leave (GtkWidget *widget,
778 GdkDragContext *context,
781 GtkDragDestSite *site;
783 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
784 g_return_if_fail (site != NULL);
786 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
787 gtk_drag_unhighlight (widget);
789 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
791 g_signal_emit_by_name (widget, "drag-leave", context, time);
793 site->have_drag = FALSE;
797 gtk_drag_dest_motion (GtkWidget *widget,
798 GdkDragContext *context,
803 GtkDragDestSite *site;
804 GdkDragAction action = 0;
807 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
808 g_return_val_if_fail (site != NULL, FALSE);
810 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
812 if (context->suggested_action & site->actions)
813 action = context->suggested_action;
815 if (action && gtk_drag_dest_find_target (widget, context, NULL))
817 if (!site->have_drag)
819 site->have_drag = TRUE;
820 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
821 gtk_drag_highlight (widget);
824 gdk_drag_status (context, action, time);
828 gdk_drag_status (context, 0, time);
829 if (!site->track_motion)
834 g_signal_emit_by_name (widget, "drag-motion",
835 context, x, y, time, &retval);
837 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
841 gtk_drag_dest_drop (GtkWidget *widget,
842 GdkDragContext *context,
847 GtkDragDestSite *site;
848 GtkDragDestInfo *info;
851 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
852 g_return_val_if_fail (site != NULL, FALSE);
854 info = gtk_drag_get_dest_info (context, FALSE);
855 g_return_val_if_fail (info != NULL, FALSE);
860 if (site->flags & GTK_DEST_DEFAULT_DROP)
862 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
864 if (target == GDK_NONE)
866 gtk_drag_finish (context, FALSE, FALSE, time);
870 gtk_drag_get_data (widget, context, target, time);
873 g_signal_emit_by_name (widget, "drag-drop",
874 context, x, y, time, &retval);
876 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
880 gtk_drag_dest_set_track_motion (GtkWidget *widget,
881 gboolean track_motion)
883 GtkDragDestSite *site;
885 g_return_if_fail (GTK_IS_WIDGET (widget));
887 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
889 g_return_if_fail (site != NULL);
891 site->track_motion = track_motion != FALSE;
895 gtk_drag_dest_get_track_motion (GtkWidget *widget)
897 GtkDragDestSite *site;
899 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
901 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
904 return site->track_motion;
910 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
913 GtkDragDestInfo *info;
914 GdkDragContext *context;
916 g_return_if_fail (toplevel != NULL);
917 g_return_if_fail (event != NULL);
919 context = event->dnd.context;
921 info = gtk_drag_get_dest_info (context, TRUE);
923 /* Find the widget for the event */
932 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
937 case GDK_DRAG_MOTION:
940 GtkDragFindData data;
943 if (event->type == GDK_DROP_START)
945 info->dropped = TRUE;
946 /* We send a leave here so that the widget unhighlights
951 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
956 gdk_window_get_position (gtk_widget_get_window (toplevel), &tx, &ty);
958 data.x = event->dnd.x_root - tx;
959 data.y = event->dnd.y_root - ty;
960 data.context = context;
963 data.toplevel = TRUE;
964 data.callback = (event->type == GDK_DRAG_MOTION) ?
965 gtk_drag_dest_motion : gtk_drag_dest_drop;
966 data.time = event->dnd.time;
968 gtk_drag_find_widget (toplevel, &data);
970 if (info->widget && !data.found)
972 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
978 if (event->type == GDK_DRAG_MOTION)
981 gdk_drag_status (context, 0, event->dnd.time);
986 g_assert_not_reached ();
993 gtk_drag_dest_find_target (GtkWidget *widget,
994 GdkDragContext *context,
995 GtkTargetList *target_list)
997 id <NSDraggingInfo> dragging_info;
998 NSPasteboard *pasteboard;
999 GtkWidget *source_widget;
1001 GList *tmp_source = NULL;
1002 GList *source_targets;
1004 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1005 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1006 g_return_val_if_fail (!context->is_source, GDK_NONE);
1008 dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
1009 pasteboard = [dragging_info draggingPasteboard];
1011 source_widget = gtk_drag_get_source_widget (context);
1013 if (target_list == NULL)
1014 target_list = gtk_drag_dest_get_target_list (widget);
1016 if (target_list == NULL)
1019 source_targets = _gtk_quartz_pasteboard_types_to_atom_list ([pasteboard types]);
1020 tmp_target = target_list->list;
1023 GtkTargetPair *pair = tmp_target->data;
1024 tmp_source = source_targets;
1027 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1029 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1030 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
1032 g_list_free (source_targets);
1033 return pair->target;
1038 tmp_source = tmp_source->next;
1040 tmp_target = tmp_target->next;
1043 g_list_free (source_targets);
1048 gtk_drag_begin_idle (gpointer arg)
1050 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1051 GdkDragContext* context = (GdkDragContext*) arg;
1052 GtkDragSourceInfo* info = gtk_drag_get_source_info (context, FALSE);
1054 NSPasteboard *pasteboard;
1055 GtkDragSourceOwner *owner;
1058 NSImage *drag_image;
1060 g_assert (info != NULL);
1062 pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
1063 owner = [[GtkDragSourceOwner alloc] initWithInfo:info];
1065 types = _gtk_quartz_target_list_to_pasteboard_types (info->target_list);
1067 [pasteboard declareTypes:[types allObjects] owner:owner];
1072 if ((nswindow = get_toplevel_nswindow (info->source_widget)) == NULL)
1075 /* Ref the context. It's unreffed when the drag has been aborted */
1076 g_object_ref (info->context);
1078 /* FIXME: If the event isn't a mouse event, use the global cursor position instead */
1079 point = [info->nsevent locationInWindow];
1081 drag_image = _gtk_quartz_create_image_from_pixbuf (info->icon_pixbuf);
1083 [nswindow dragImage:drag_image
1085 offset:NSMakeSize(0, 0)
1087 pasteboard:pasteboard
1091 [info->nsevent release];
1092 [drag_image release];
1099 static GdkDragContext *
1100 gtk_drag_begin_internal (GtkWidget *widget,
1101 GtkDragSourceSite *site,
1102 GtkTargetList *target_list,
1103 GdkDragAction actions,
1107 GtkDragSourceInfo *info;
1108 GdkDragContext *context;
1111 context = gdk_drag_begin (NULL, NULL);
1112 context->is_source = TRUE;
1114 info = gtk_drag_get_source_info (context, TRUE);
1116 info->source_widget = g_object_ref (widget);
1117 info->widget = g_object_ref (widget);
1118 info->target_list = target_list;
1119 gtk_target_list_ref (target_list);
1121 info->possible_actions = actions;
1123 g_signal_emit_by_name (widget, "drag-begin", info->context);
1125 /* Ensure that we have an icon before we start the drag; the
1126 * application may have set one in ::drag_begin, or it may
1129 if (!info->icon_pixbuf)
1131 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
1132 gtk_drag_set_icon_default (context);
1134 switch (site->icon_type)
1136 case GTK_IMAGE_PIXBUF:
1137 gtk_drag_set_icon_pixbuf (context,
1138 site->icon_data.pixbuf.pixbuf,
1141 case GTK_IMAGE_STOCK:
1142 gtk_drag_set_icon_stock (context,
1143 site->icon_data.stock.stock_id,
1146 case GTK_IMAGE_ICON_NAME:
1147 gtk_drag_set_icon_name (context,
1148 site->icon_data.name.icon_name,
1151 case GTK_IMAGE_EMPTY:
1153 g_assert_not_reached();
1158 nswindow = get_toplevel_nswindow (widget);
1159 info->nsevent = [nswindow currentEvent];
1160 [info->nsevent retain];
1162 /* drag will begin in an idle handler to avoid nested run loops */
1164 g_idle_add_full (G_PRIORITY_HIGH_IDLE, gtk_drag_begin_idle, context, NULL);
1166 gdk_pointer_ungrab (0);
1172 gtk_drag_begin (GtkWidget *widget,
1173 GtkTargetList *targets,
1174 GdkDragAction actions,
1178 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1179 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
1180 g_return_val_if_fail (targets != NULL, NULL);
1182 return gtk_drag_begin_internal (widget, NULL, targets,
1183 actions, button, event);
1188 gtk_drag_source_event_cb (GtkWidget *widget,
1192 GtkDragSourceSite *site;
1193 gboolean retval = FALSE;
1194 site = (GtkDragSourceSite *)data;
1196 switch (event->type)
1198 case GDK_BUTTON_PRESS:
1199 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1201 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
1202 site->x = event->button.x;
1203 site->y = event->button.y;
1207 case GDK_BUTTON_RELEASE:
1208 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1209 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
1212 case GDK_MOTION_NOTIFY:
1213 if (site->state & event->motion.state & site->start_button_mask)
1215 /* FIXME: This is really broken and can leave us
1221 if (site->state & event->motion.state &
1222 GDK_BUTTON1_MASK << (i - 1))
1226 if (gtk_drag_check_threshold (widget, site->x, site->y,
1227 event->motion.x, event->motion.y))
1230 gtk_drag_begin_internal (widget, site, site->target_list,
1239 default: /* hit for 2/3BUTTON_PRESS */
1247 gtk_drag_source_set (GtkWidget *widget,
1248 GdkModifierType start_button_mask,
1249 const GtkTargetEntry *targets,
1251 GdkDragAction actions)
1253 GtkDragSourceSite *site;
1255 g_return_if_fail (GTK_IS_WIDGET (widget));
1257 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1259 gtk_widget_add_events (widget,
1260 gtk_widget_get_events (widget) |
1261 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1262 GDK_BUTTON_MOTION_MASK);
1266 if (site->target_list)
1267 gtk_target_list_unref (site->target_list);
1271 site = g_new0 (GtkDragSourceSite, 1);
1273 site->icon_type = GTK_IMAGE_EMPTY;
1275 g_signal_connect (widget, "button-press-event",
1276 G_CALLBACK (gtk_drag_source_event_cb),
1278 g_signal_connect (widget, "button-release-event",
1279 G_CALLBACK (gtk_drag_source_event_cb),
1281 g_signal_connect (widget, "motion-notify-event",
1282 G_CALLBACK (gtk_drag_source_event_cb),
1285 g_object_set_data_full (G_OBJECT (widget),
1286 I_("gtk-site-data"),
1287 site, gtk_drag_source_site_destroy);
1290 site->start_button_mask = start_button_mask;
1292 site->target_list = gtk_target_list_new (targets, n_targets);
1294 site->actions = actions;
1297 /*************************************************************
1298 * gtk_drag_source_unset
1299 * Unregister this widget as a drag source.
1303 *************************************************************/
1306 gtk_drag_source_unset (GtkWidget *widget)
1308 GtkDragSourceSite *site;
1310 g_return_if_fail (GTK_IS_WIDGET (widget));
1312 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1316 g_signal_handlers_disconnect_by_func (widget,
1317 gtk_drag_source_event_cb,
1319 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
1324 gtk_drag_source_get_target_list (GtkWidget *widget)
1326 GtkDragSourceSite *site;
1328 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1330 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1332 return site ? site->target_list : NULL;
1337 gtk_drag_source_set_target_list (GtkWidget *widget,
1338 GtkTargetList *target_list)
1340 GtkDragSourceSite *site;
1342 g_return_if_fail (GTK_IS_WIDGET (widget));
1344 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1347 g_warning ("gtk_drag_source_set_target_list() requires the widget "
1348 "to already be a drag source.");
1353 gtk_target_list_ref (target_list);
1355 if (site->target_list)
1356 gtk_target_list_unref (site->target_list);
1358 site->target_list = target_list;
1362 * gtk_drag_source_add_text_targets:
1363 * @widget: a #GtkWidget that's is a drag source
1365 * Add the text targets supported by #GtkSelection to
1366 * the target list of the drag source. The targets
1367 * are added with @info = 0. If you need another value,
1368 * use gtk_target_list_add_text_targets() and
1369 * gtk_drag_source_set_target_list().
1374 gtk_drag_source_add_text_targets (GtkWidget *widget)
1376 GtkTargetList *target_list;
1378 target_list = gtk_drag_source_get_target_list (widget);
1380 gtk_target_list_ref (target_list);
1382 target_list = gtk_target_list_new (NULL, 0);
1383 gtk_target_list_add_text_targets (target_list, 0);
1384 gtk_drag_source_set_target_list (widget, target_list);
1385 gtk_target_list_unref (target_list);
1389 gtk_drag_source_add_image_targets (GtkWidget *widget)
1391 GtkTargetList *target_list;
1393 target_list = gtk_drag_source_get_target_list (widget);
1395 gtk_target_list_ref (target_list);
1397 target_list = gtk_target_list_new (NULL, 0);
1398 gtk_target_list_add_image_targets (target_list, 0, TRUE);
1399 gtk_drag_source_set_target_list (widget, target_list);
1400 gtk_target_list_unref (target_list);
1404 gtk_drag_source_add_uri_targets (GtkWidget *widget)
1406 GtkTargetList *target_list;
1408 target_list = gtk_drag_source_get_target_list (widget);
1410 gtk_target_list_ref (target_list);
1412 target_list = gtk_target_list_new (NULL, 0);
1413 gtk_target_list_add_uri_targets (target_list, 0);
1414 gtk_drag_source_set_target_list (widget, target_list);
1415 gtk_target_list_unref (target_list);
1419 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
1421 switch (site->icon_type)
1423 case GTK_IMAGE_EMPTY:
1425 case GTK_IMAGE_PIXBUF:
1426 g_object_unref (site->icon_data.pixbuf.pixbuf);
1428 case GTK_IMAGE_STOCK:
1429 g_free (site->icon_data.stock.stock_id);
1431 case GTK_IMAGE_ICON_NAME:
1432 g_free (site->icon_data.name.icon_name);
1435 g_assert_not_reached();
1438 site->icon_type = GTK_IMAGE_EMPTY;
1442 gtk_drag_source_site_destroy (gpointer data)
1444 GtkDragSourceSite *site = data;
1446 if (site->target_list)
1447 gtk_target_list_unref (site->target_list);
1449 gtk_drag_source_unset_icon (site);
1454 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
1457 GtkDragSourceSite *site;
1459 g_return_if_fail (GTK_IS_WIDGET (widget));
1460 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1462 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1463 g_return_if_fail (site != NULL);
1464 g_object_ref (pixbuf);
1466 gtk_drag_source_unset_icon (site);
1468 site->icon_type = GTK_IMAGE_PIXBUF;
1469 site->icon_data.pixbuf.pixbuf = pixbuf;
1473 * gtk_drag_source_set_icon_stock:
1474 * @widget: a #GtkWidget
1475 * @stock_id: the ID of the stock icon to use
1477 * Sets the icon that will be used for drags from a particular source
1481 gtk_drag_source_set_icon_stock (GtkWidget *widget,
1482 const gchar *stock_id)
1484 GtkDragSourceSite *site;
1486 g_return_if_fail (GTK_IS_WIDGET (widget));
1487 g_return_if_fail (stock_id != NULL);
1489 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1490 g_return_if_fail (site != NULL);
1492 gtk_drag_source_unset_icon (site);
1494 site->icon_type = GTK_IMAGE_STOCK;
1495 site->icon_data.stock.stock_id = g_strdup (stock_id);
1499 * gtk_drag_source_set_icon_name:
1500 * @widget: a #GtkWidget
1501 * @icon_name: name of icon to use
1503 * Sets the icon that will be used for drags from a particular source
1504 * to a themed icon. See the docs for #GtkIconTheme for more details.
1509 gtk_drag_source_set_icon_name (GtkWidget *widget,
1510 const gchar *icon_name)
1512 GtkDragSourceSite *site;
1514 g_return_if_fail (GTK_IS_WIDGET (widget));
1515 g_return_if_fail (icon_name != NULL);
1517 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1518 g_return_if_fail (site != NULL);
1520 gtk_drag_source_unset_icon (site);
1522 site->icon_type = GTK_IMAGE_ICON_NAME;
1523 site->icon_data.name.icon_name = g_strdup (icon_name);
1528 * gtk_drag_set_icon_widget:
1529 * @context: the context for a drag. (This must be called
1530 with a context for the source side of a drag)
1531 * @widget: a toplevel window to use as an icon.
1532 * @hot_x: the X offset within @widget of the hotspot.
1533 * @hot_y: the Y offset within @widget of the hotspot.
1535 * Changes the icon for a widget to a given widget. GTK+
1536 * will not destroy the icon, so if you don't want
1537 * it to persist, you should connect to the "drag-end"
1538 * signal and destroy it yourself.
1541 gtk_drag_set_icon_widget (GdkDragContext *context,
1546 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1547 g_return_if_fail (context->is_source);
1548 g_return_if_fail (GTK_IS_WIDGET (widget));
1550 g_warning ("gtk_drag_set_icon_widget is not supported on Mac OS X");
1554 set_icon_stock_pixbuf (GdkDragContext *context,
1555 const gchar *stock_id,
1560 GtkDragSourceInfo *info;
1562 info = gtk_drag_get_source_info (context, FALSE);
1566 pixbuf = gtk_widget_render_icon (info->widget, stock_id,
1567 GTK_ICON_SIZE_DND, NULL);
1571 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
1576 g_object_ref (pixbuf);
1578 if (info->icon_pixbuf)
1579 g_object_unref (info->icon_pixbuf);
1580 info->icon_pixbuf = pixbuf;
1581 info->hot_x = hot_x;
1582 info->hot_y = hot_y;
1586 * gtk_drag_set_icon_pixbuf:
1587 * @context: the context for a drag. (This must be called
1588 * with a context for the source side of a drag)
1589 * @pixbuf: the #GdkPixbuf to use as the drag icon.
1590 * @hot_x: the X offset within @widget of the hotspot.
1591 * @hot_y: the Y offset within @widget of the hotspot.
1593 * Sets @pixbuf as the icon for a given drag.
1596 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
1601 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1602 g_return_if_fail (context->is_source);
1603 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1605 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1609 * gtk_drag_set_icon_stock:
1610 * @context: the context for a drag. (This must be called
1611 * with a context for the source side of a drag)
1612 * @stock_id: the ID of the stock icon to use for the drag.
1613 * @hot_x: the X offset within the icon of the hotspot.
1614 * @hot_y: the Y offset within the icon of the hotspot.
1616 * Sets the icon for a given drag from a stock ID.
1619 gtk_drag_set_icon_stock (GdkDragContext *context,
1620 const gchar *stock_id,
1625 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1626 g_return_if_fail (context->is_source);
1627 g_return_if_fail (stock_id != NULL);
1629 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
1633 /* XXX: This function is in gdk, too. Should it be in Cairo? */
1635 _gtk_cairo_surface_extents (cairo_surface_t *surface,
1636 GdkRectangle *extents)
1638 double x1, x2, y1, y2;
1641 g_return_val_if_fail (surface != NULL, FALSE);
1642 g_return_val_if_fail (extents != NULL, FALSE);
1644 cr = cairo_create (surface);
1645 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
1654 if (x1 < G_MININT || x1 > G_MAXINT ||
1655 y1 < G_MININT || y1 > G_MAXINT ||
1656 x2 > G_MAXINT || y2 > G_MAXINT)
1658 extents->x = extents->y = extents->width = extents->height = 0;
1664 extents->width = x2;
1665 extents->height = y2;
1671 * gtk_drag_set_icon_surface:
1672 * @context: the context for a drag. (This must be called
1673 * with a context for the source side of a drag)
1674 * @surface: the surface to use as icon
1676 * Sets @surface as the icon for a given drag. GTK+ retains
1677 * references for the arguments, and will release them when
1678 * they are no longer needed.
1680 * To position the surface relative to the mouse, use
1681 * cairo_surface_set_device_offset() on @surface. The mouse
1682 * cursor will be positioned at the (0,0) coordinate of the
1686 gtk_drag_set_icon_surface (GdkDragContext *context,
1687 cairo_surface_t *surface)
1690 GdkRectangle extents;
1692 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1693 g_return_if_fail (context->is_source);
1694 g_return_if_fail (surface != NULL);
1696 _gtk_cairo_surface_extents (surface, &extents);
1698 pixbuf = gdk_pixbuf_get_from_surface (surface,
1699 extents.x, extents.y,
1700 extents.width, extents.height);
1701 gtk_drag_set_icon_pixbuf (context, pixbuf, 0, 0);
1702 g_object_unref (pixbuf);
1706 * gtk_drag_set_icon_name:
1707 * @context: the context for a drag. (This must be called
1708 * with a context for the source side of a drag)
1709 * @icon_name: name of icon to use
1710 * @hot_x: the X offset of the hotspot within the icon
1711 * @hot_y: the Y offset of the hotspot within the icon
1713 * Sets the icon for a given drag from a named themed icon. See
1714 * the docs for #GtkIconTheme for more details. Note that the
1715 * size of the icon depends on the icon theme (the icon is
1716 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
1717 * @hot_x and @hot_y have to be used with care.
1722 gtk_drag_set_icon_name (GdkDragContext *context,
1723 const gchar *icon_name,
1728 GtkSettings *settings;
1729 GtkIconTheme *icon_theme;
1731 gint width, height, icon_size;
1733 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1734 g_return_if_fail (context->is_source);
1735 g_return_if_fail (icon_name != NULL);
1737 screen = gdk_window_get_screen (context->source_window);
1738 g_return_if_fail (screen != NULL);
1740 settings = gtk_settings_get_for_screen (screen);
1741 if (gtk_icon_size_lookup_for_settings (settings,
1744 icon_size = MAX (width, height);
1746 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
1748 icon_theme = gtk_icon_theme_get_for_screen (screen);
1750 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
1751 icon_size, 0, NULL);
1753 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1755 g_warning ("Cannot load drag icon from icon name %s", icon_name);
1759 * gtk_drag_set_icon_default:
1760 * @context: the context for a drag. (This must be called
1761 with a context for the source side of a drag)
1763 * Sets the icon for a particular drag to the default
1767 gtk_drag_set_icon_default (GdkDragContext *context)
1769 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1770 g_return_if_fail (context->is_source);
1772 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
1776 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
1778 if (info->icon_pixbuf)
1779 g_object_unref (info->icon_pixbuf);
1781 g_signal_emit_by_name (info->widget, "drag-end",
1784 if (info->source_widget)
1785 g_object_unref (info->source_widget);
1788 g_object_unref (info->widget);
1790 gtk_target_list_unref (info->target_list);
1792 gtk_drag_clear_source_info (info->context);
1793 g_object_unref (info->context);
1799 drag_drop_finished_idle_cb (gpointer data)
1801 gtk_drag_source_info_destroy (data);
1806 gtk_drag_drop_finished (GtkDragSourceInfo *info)
1808 if (info->success && info->delete)
1809 g_signal_emit_by_name (info->source_widget, "drag-data-delete",
1812 /* Workaround for the fact that the NS API blocks until the drag is
1813 * over. This way the context is still valid when returning from
1814 * drag_begin, even if it will still be quite useless. See bug #501588.
1816 g_idle_add (drag_drop_finished_idle_cb, info);
1819 /*************************************************************
1820 * _gtk_drag_source_handle_event:
1821 * Called from widget event handling code on Drag events
1825 * toplevel: Toplevel widget that received the event
1828 *************************************************************/
1831 _gtk_drag_source_handle_event (GtkWidget *widget,
1834 GtkDragSourceInfo *info;
1835 GdkDragContext *context;
1837 g_return_if_fail (widget != NULL);
1838 g_return_if_fail (event != NULL);
1840 context = event->dnd.context;
1841 info = gtk_drag_get_source_info (context, FALSE);
1845 switch (event->type)
1847 case GDK_DROP_FINISHED:
1848 gtk_drag_drop_finished (info);
1851 g_assert_not_reached ();
1857 gtk_drag_check_threshold (GtkWidget *widget,
1863 gint drag_threshold;
1865 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1867 g_object_get (gtk_widget_get_settings (widget),
1868 "gtk-dnd-drag-threshold", &drag_threshold,
1871 return (ABS (current_x - start_x) > drag_threshold ||
1872 ABS (current_y - start_y) > drag_threshold);