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_expose:
336 * Callback for expose_event for highlighted widgets.
342 *************************************************************/
345 gtk_drag_highlight_expose (GtkWidget *widget,
346 GdkEventExpose *event,
349 gint x, y, width, height;
351 if (gtk_widget_is_drawable (widget))
354 GdkWindow *window = gtk_widget_get_window (widget);
356 if (!gtk_widget_get_has_window (widget))
358 GtkAllocation allocation;
360 gtk_widget_get_allocation (widget, &allocation);
364 width = allocation.width;
365 height = allocation.height;
371 gdk_drawable_get_size (window, &width, &height);
374 gtk_paint_shadow (gtk_widget_get_style (widget), window,
375 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
377 x, y, width, height);
379 cr = gdk_cairo_create (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);
436 GdkWindow *window = gtk_widget_get_window (toplevel);
438 if (gtk_widget_is_toplevel (toplevel) && window)
439 return [gdk_quartz_window_get_nsview (window) window];
445 register_types (GtkWidget *widget, GtkDragDestSite *site)
447 if (site->target_list)
449 NSWindow *nswindow = get_toplevel_nswindow (widget);
451 NSAutoreleasePool *pool;
456 pool = [[NSAutoreleasePool alloc] init];
457 types = _gtk_quartz_target_list_to_pasteboard_types (site->target_list);
459 [nswindow registerForDraggedTypes:[types allObjects]];
467 gtk_drag_dest_realized (GtkWidget *widget,
470 GtkDragDestSite *site = user_data;
472 register_types (widget, site);
476 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
477 GtkWidget *previous_toplevel,
480 GtkDragDestSite *site = user_data;
482 register_types (widget, site);
486 gtk_drag_dest_site_destroy (gpointer data)
488 GtkDragDestSite *site = data;
490 if (site->target_list)
491 gtk_target_list_unref (site->target_list);
497 gtk_drag_dest_set (GtkWidget *widget,
498 GtkDestDefaults flags,
499 const GtkTargetEntry *targets,
501 GdkDragAction actions)
503 GtkDragDestSite *old_site, *site;
505 g_return_if_fail (GTK_IS_WIDGET (widget));
507 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
509 site = g_new (GtkDragDestSite, 1);
511 site->have_drag = FALSE;
513 site->target_list = gtk_target_list_new (targets, n_targets);
515 site->target_list = NULL;
516 site->actions = actions;
519 site->track_motion = old_site->track_motion;
521 site->track_motion = FALSE;
523 gtk_drag_dest_unset (widget);
525 if (gtk_widget_get_realized (widget))
526 gtk_drag_dest_realized (widget, site);
528 g_signal_connect (widget, "realize",
529 G_CALLBACK (gtk_drag_dest_realized), site);
530 g_signal_connect (widget, "hierarchy-changed",
531 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
533 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
534 site, gtk_drag_dest_site_destroy);
538 gtk_drag_dest_set_proxy (GtkWidget *widget,
539 GdkWindow *proxy_window,
540 GdkDragProtocol protocol,
541 gboolean use_coordinates)
543 g_warning ("gtk_drag_dest_set_proxy is not supported on Mac OS X.");
547 gtk_drag_dest_unset (GtkWidget *widget)
549 GtkDragDestSite *old_site;
551 g_return_if_fail (GTK_IS_WIDGET (widget));
553 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
556 g_signal_handlers_disconnect_by_func (widget,
557 gtk_drag_dest_realized,
559 g_signal_handlers_disconnect_by_func (widget,
560 gtk_drag_dest_hierarchy_changed,
564 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
568 gtk_drag_dest_get_target_list (GtkWidget *widget)
570 GtkDragDestSite *site;
572 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
574 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
576 return site ? site->target_list : NULL;
580 gtk_drag_dest_set_target_list (GtkWidget *widget,
581 GtkTargetList *target_list)
583 GtkDragDestSite *site;
585 g_return_if_fail (GTK_IS_WIDGET (widget));
587 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
591 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
592 "to make the widget into a drag destination");
597 gtk_target_list_ref (target_list);
599 if (site->target_list)
600 gtk_target_list_unref (site->target_list);
602 site->target_list = target_list;
604 register_types (widget, site);
608 gtk_drag_dest_add_text_targets (GtkWidget *widget)
610 GtkTargetList *target_list;
612 target_list = gtk_drag_dest_get_target_list (widget);
614 gtk_target_list_ref (target_list);
616 target_list = gtk_target_list_new (NULL, 0);
617 gtk_target_list_add_text_targets (target_list, 0);
618 gtk_drag_dest_set_target_list (widget, target_list);
619 gtk_target_list_unref (target_list);
623 gtk_drag_dest_add_image_targets (GtkWidget *widget)
625 GtkTargetList *target_list;
627 target_list = gtk_drag_dest_get_target_list (widget);
629 gtk_target_list_ref (target_list);
631 target_list = gtk_target_list_new (NULL, 0);
632 gtk_target_list_add_image_targets (target_list, 0, FALSE);
633 gtk_drag_dest_set_target_list (widget, target_list);
634 gtk_target_list_unref (target_list);
638 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
640 GtkTargetList *target_list;
642 target_list = gtk_drag_dest_get_target_list (widget);
644 gtk_target_list_ref (target_list);
646 target_list = gtk_target_list_new (NULL, 0);
647 gtk_target_list_add_uri_targets (target_list, 0);
648 gtk_drag_dest_set_target_list (widget, target_list);
649 gtk_target_list_unref (target_list);
653 prepend_and_ref_widget (GtkWidget *widget,
656 GSList **slist_p = data;
658 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
662 gtk_drag_find_widget (GtkWidget *widget,
663 GtkDragFindData *data)
665 GtkAllocation new_allocation;
666 gint allocation_to_window_x = 0;
667 gint allocation_to_window_y = 0;
671 if (data->found || !gtk_widget_get_mapped (widget) || !gtk_widget_get_sensitive (widget))
674 /* Note that in the following code, we only count the
675 * position as being inside a WINDOW widget if it is inside
676 * widget->window; points that are outside of widget->window
677 * but within the allocation are not counted. This is consistent
678 * with the way we highlight drag targets.
680 * data->x,y are relative to widget->parent->window (if
681 * widget is not a toplevel, widget->window otherwise).
682 * We compute the allocation of widget in the same coordinates,
683 * clipping to widget->window, and all intermediate
684 * windows. If data->x,y is inside that, then we translate
685 * our coordinates to be relative to widget->window and
688 gtk_widget_get_allocation (widget, &new_allocation);
690 if (gtk_widget_get_parent (widget))
693 GdkWindow *window = gtk_widget_get_window (widget);
694 GdkWindow *parent_window;
695 GtkAllocation allocation;
697 parent_window = gtk_widget_get_window (gtk_widget_get_parent (widget));
699 /* Compute the offset from allocation-relative to
700 * window-relative coordinates.
702 gtk_widget_get_allocation (widget, &allocation);
703 allocation_to_window_x = allocation.x;
704 allocation_to_window_y = allocation.y;
706 if (gtk_widget_get_has_window (widget))
708 /* The allocation is relative to the parent window for
709 * window widgets, not to widget->window.
711 gdk_window_get_position (window, &tx, &ty);
713 allocation_to_window_x -= tx;
714 allocation_to_window_y -= ty;
717 new_allocation.x = 0 + allocation_to_window_x;
718 new_allocation.y = 0 + allocation_to_window_y;
720 while (window && window != parent_window)
722 GdkRectangle window_rect = { 0, 0, 0, 0 };
724 gdk_drawable_get_size (window, &window_rect.width, &window_rect.height);
726 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
728 gdk_window_get_position (window, &tx, &ty);
729 new_allocation.x += tx;
731 new_allocation.y += ty;
734 window = gdk_window_get_parent (window);
737 if (!window) /* Window and widget heirarchies didn't match. */
741 if (data->toplevel ||
742 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
743 (data->x < new_allocation.x + new_allocation.width) &&
744 (data->y < new_allocation.y + new_allocation.height)))
746 /* First, check if the drag is in a valid drop site in
747 * one of our children
749 if (GTK_IS_CONTAINER (widget))
751 GtkDragFindData new_data = *data;
752 GSList *children = NULL;
755 new_data.x -= x_offset;
756 new_data.y -= y_offset;
757 new_data.found = FALSE;
758 new_data.toplevel = FALSE;
760 /* need to reference children temporarily in case the
761 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
763 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
764 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
766 if (!new_data.found && gtk_widget_is_drawable (tmp_list->data))
767 gtk_drag_find_widget (tmp_list->data, &new_data);
768 g_object_unref (tmp_list->data);
770 g_slist_free (children);
772 data->found = new_data.found;
775 /* If not, and this widget is registered as a drop site, check to
776 * emit "drag-motion" to check if we are actually in
780 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
782 data->found = data->callback (widget,
784 data->x - x_offset - allocation_to_window_x,
785 data->y - y_offset - allocation_to_window_y,
787 /* If so, send a "drag-leave" to the last widget */
790 if (data->info->widget && data->info->widget != widget)
792 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
794 data->info->widget = widget;
801 gtk_drag_dest_leave (GtkWidget *widget,
802 GdkDragContext *context,
805 GtkDragDestSite *site;
807 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
808 g_return_if_fail (site != NULL);
810 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
811 gtk_drag_unhighlight (widget);
813 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
815 g_signal_emit_by_name (widget, "drag-leave", context, time);
817 site->have_drag = FALSE;
821 gtk_drag_dest_motion (GtkWidget *widget,
822 GdkDragContext *context,
827 GtkDragDestSite *site;
828 GdkDragAction action = 0;
831 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
832 g_return_val_if_fail (site != NULL, FALSE);
834 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
836 if (context->suggested_action & site->actions)
837 action = context->suggested_action;
839 if (action && gtk_drag_dest_find_target (widget, context, NULL))
841 if (!site->have_drag)
843 site->have_drag = TRUE;
844 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
845 gtk_drag_highlight (widget);
848 gdk_drag_status (context, action, time);
852 gdk_drag_status (context, 0, time);
853 if (!site->track_motion)
858 g_signal_emit_by_name (widget, "drag-motion",
859 context, x, y, time, &retval);
861 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
865 gtk_drag_dest_drop (GtkWidget *widget,
866 GdkDragContext *context,
871 GtkDragDestSite *site;
872 GtkDragDestInfo *info;
875 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
876 g_return_val_if_fail (site != NULL, FALSE);
878 info = gtk_drag_get_dest_info (context, FALSE);
879 g_return_val_if_fail (info != NULL, FALSE);
884 if (site->flags & GTK_DEST_DEFAULT_DROP)
886 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
888 if (target == GDK_NONE)
890 gtk_drag_finish (context, FALSE, FALSE, time);
894 gtk_drag_get_data (widget, context, target, time);
897 g_signal_emit_by_name (widget, "drag-drop",
898 context, x, y, time, &retval);
900 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
904 gtk_drag_dest_set_track_motion (GtkWidget *widget,
905 gboolean track_motion)
907 GtkDragDestSite *site;
909 g_return_if_fail (GTK_IS_WIDGET (widget));
911 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
913 g_return_if_fail (site != NULL);
915 site->track_motion = track_motion != FALSE;
919 gtk_drag_dest_get_track_motion (GtkWidget *widget)
921 GtkDragDestSite *site;
923 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
925 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
928 return site->track_motion;
934 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
937 GtkDragDestInfo *info;
938 GdkDragContext *context;
940 g_return_if_fail (toplevel != NULL);
941 g_return_if_fail (event != NULL);
943 context = event->dnd.context;
945 info = gtk_drag_get_dest_info (context, TRUE);
947 /* Find the widget for the event */
956 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
961 case GDK_DRAG_MOTION:
964 GtkDragFindData data;
967 if (event->type == GDK_DROP_START)
969 info->dropped = TRUE;
970 /* We send a leave here so that the widget unhighlights
975 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
980 gdk_window_get_position (gtk_widget_get_window (toplevel), &tx, &ty);
982 data.x = event->dnd.x_root - tx;
983 data.y = event->dnd.y_root - ty;
984 data.context = context;
987 data.toplevel = TRUE;
988 data.callback = (event->type == GDK_DRAG_MOTION) ?
989 gtk_drag_dest_motion : gtk_drag_dest_drop;
990 data.time = event->dnd.time;
992 gtk_drag_find_widget (toplevel, &data);
994 if (info->widget && !data.found)
996 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1002 if (event->type == GDK_DRAG_MOTION)
1005 gdk_drag_status (context, 0, event->dnd.time);
1010 g_assert_not_reached ();
1017 gtk_drag_dest_find_target (GtkWidget *widget,
1018 GdkDragContext *context,
1019 GtkTargetList *target_list)
1021 id <NSDraggingInfo> dragging_info;
1022 NSPasteboard *pasteboard;
1023 GtkWidget *source_widget;
1025 GList *tmp_source = NULL;
1026 GList *source_targets;
1028 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1029 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1030 g_return_val_if_fail (!context->is_source, GDK_NONE);
1032 dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
1033 pasteboard = [dragging_info draggingPasteboard];
1035 source_widget = gtk_drag_get_source_widget (context);
1037 if (target_list == NULL)
1038 target_list = gtk_drag_dest_get_target_list (widget);
1040 if (target_list == NULL)
1043 source_targets = _gtk_quartz_pasteboard_types_to_atom_list ([pasteboard types]);
1044 tmp_target = target_list->list;
1047 GtkTargetPair *pair = tmp_target->data;
1048 tmp_source = source_targets;
1051 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1053 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1054 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
1056 g_list_free (source_targets);
1057 return pair->target;
1062 tmp_source = tmp_source->next;
1064 tmp_target = tmp_target->next;
1067 g_list_free (source_targets);
1072 gtk_drag_begin_idle (gpointer arg)
1074 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1075 GdkDragContext* context = (GdkDragContext*) arg;
1076 GtkDragSourceInfo* info = gtk_drag_get_source_info (context, FALSE);
1078 NSPasteboard *pasteboard;
1079 GtkDragSourceOwner *owner;
1082 NSImage *drag_image;
1084 g_assert (info != NULL);
1086 pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
1087 owner = [[GtkDragSourceOwner alloc] initWithInfo:info];
1089 types = _gtk_quartz_target_list_to_pasteboard_types (info->target_list);
1091 [pasteboard declareTypes:[types allObjects] owner:owner];
1096 if ((nswindow = get_toplevel_nswindow (info->source_widget)) == NULL)
1099 /* Ref the context. It's unreffed when the drag has been aborted */
1100 g_object_ref (info->context);
1102 /* FIXME: If the event isn't a mouse event, use the global cursor position instead */
1103 point = [info->nsevent locationInWindow];
1105 drag_image = _gtk_quartz_create_image_from_pixbuf (info->icon_pixbuf);
1107 [nswindow dragImage:drag_image
1109 offset:NSMakeSize(0, 0)
1111 pasteboard:pasteboard
1115 [info->nsevent release];
1116 [drag_image release];
1123 static GdkDragContext *
1124 gtk_drag_begin_internal (GtkWidget *widget,
1125 GtkDragSourceSite *site,
1126 GtkTargetList *target_list,
1127 GdkDragAction actions,
1131 GtkDragSourceInfo *info;
1132 GdkDragContext *context;
1135 context = gdk_drag_begin (NULL, NULL);
1136 context->is_source = TRUE;
1138 info = gtk_drag_get_source_info (context, TRUE);
1140 info->source_widget = g_object_ref (widget);
1141 info->widget = g_object_ref (widget);
1142 info->target_list = target_list;
1143 gtk_target_list_ref (target_list);
1145 info->possible_actions = actions;
1147 g_signal_emit_by_name (widget, "drag-begin", info->context);
1149 /* Ensure that we have an icon before we start the drag; the
1150 * application may have set one in ::drag_begin, or it may
1153 if (!info->icon_pixbuf)
1155 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
1156 gtk_drag_set_icon_default (context);
1158 switch (site->icon_type)
1160 case GTK_IMAGE_PIXBUF:
1161 gtk_drag_set_icon_pixbuf (context,
1162 site->icon_data.pixbuf.pixbuf,
1165 case GTK_IMAGE_STOCK:
1166 gtk_drag_set_icon_stock (context,
1167 site->icon_data.stock.stock_id,
1170 case GTK_IMAGE_ICON_NAME:
1171 gtk_drag_set_icon_name (context,
1172 site->icon_data.name.icon_name,
1175 case GTK_IMAGE_EMPTY:
1177 g_assert_not_reached();
1182 nswindow = get_toplevel_nswindow (widget);
1183 info->nsevent = [nswindow currentEvent];
1184 [info->nsevent retain];
1186 /* drag will begin in an idle handler to avoid nested run loops */
1188 g_idle_add_full (G_PRIORITY_HIGH_IDLE, gtk_drag_begin_idle, context, NULL);
1190 gdk_pointer_ungrab (0);
1196 gtk_drag_begin (GtkWidget *widget,
1197 GtkTargetList *targets,
1198 GdkDragAction actions,
1202 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1203 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
1204 g_return_val_if_fail (targets != NULL, NULL);
1206 return gtk_drag_begin_internal (widget, NULL, targets,
1207 actions, button, event);
1212 gtk_drag_source_event_cb (GtkWidget *widget,
1216 GtkDragSourceSite *site;
1217 gboolean retval = FALSE;
1218 site = (GtkDragSourceSite *)data;
1220 switch (event->type)
1222 case GDK_BUTTON_PRESS:
1223 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1225 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
1226 site->x = event->button.x;
1227 site->y = event->button.y;
1231 case GDK_BUTTON_RELEASE:
1232 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1233 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
1236 case GDK_MOTION_NOTIFY:
1237 if (site->state & event->motion.state & site->start_button_mask)
1239 /* FIXME: This is really broken and can leave us
1245 if (site->state & event->motion.state &
1246 GDK_BUTTON1_MASK << (i - 1))
1250 if (gtk_drag_check_threshold (widget, site->x, site->y,
1251 event->motion.x, event->motion.y))
1254 gtk_drag_begin_internal (widget, site, site->target_list,
1263 default: /* hit for 2/3BUTTON_PRESS */
1271 gtk_drag_source_set (GtkWidget *widget,
1272 GdkModifierType start_button_mask,
1273 const GtkTargetEntry *targets,
1275 GdkDragAction actions)
1277 GtkDragSourceSite *site;
1279 g_return_if_fail (GTK_IS_WIDGET (widget));
1281 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1283 gtk_widget_add_events (widget,
1284 gtk_widget_get_events (widget) |
1285 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1286 GDK_BUTTON_MOTION_MASK);
1290 if (site->target_list)
1291 gtk_target_list_unref (site->target_list);
1295 site = g_new0 (GtkDragSourceSite, 1);
1297 site->icon_type = GTK_IMAGE_EMPTY;
1299 g_signal_connect (widget, "button-press-event",
1300 G_CALLBACK (gtk_drag_source_event_cb),
1302 g_signal_connect (widget, "button-release-event",
1303 G_CALLBACK (gtk_drag_source_event_cb),
1305 g_signal_connect (widget, "motion-notify-event",
1306 G_CALLBACK (gtk_drag_source_event_cb),
1309 g_object_set_data_full (G_OBJECT (widget),
1310 I_("gtk-site-data"),
1311 site, gtk_drag_source_site_destroy);
1314 site->start_button_mask = start_button_mask;
1316 site->target_list = gtk_target_list_new (targets, n_targets);
1318 site->actions = actions;
1321 /*************************************************************
1322 * gtk_drag_source_unset
1323 * Unregister this widget as a drag source.
1327 *************************************************************/
1330 gtk_drag_source_unset (GtkWidget *widget)
1332 GtkDragSourceSite *site;
1334 g_return_if_fail (GTK_IS_WIDGET (widget));
1336 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1340 g_signal_handlers_disconnect_by_func (widget,
1341 gtk_drag_source_event_cb,
1343 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
1348 gtk_drag_source_get_target_list (GtkWidget *widget)
1350 GtkDragSourceSite *site;
1352 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1354 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1356 return site ? site->target_list : NULL;
1361 gtk_drag_source_set_target_list (GtkWidget *widget,
1362 GtkTargetList *target_list)
1364 GtkDragSourceSite *site;
1366 g_return_if_fail (GTK_IS_WIDGET (widget));
1368 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1371 g_warning ("gtk_drag_source_set_target_list() requires the widget "
1372 "to already be a drag source.");
1377 gtk_target_list_ref (target_list);
1379 if (site->target_list)
1380 gtk_target_list_unref (site->target_list);
1382 site->target_list = target_list;
1386 * gtk_drag_source_add_text_targets:
1387 * @widget: a #GtkWidget that's is a drag source
1389 * Add the text targets supported by #GtkSelection to
1390 * the target list of the drag source. The targets
1391 * are added with @info = 0. If you need another value,
1392 * use gtk_target_list_add_text_targets() and
1393 * gtk_drag_source_set_target_list().
1398 gtk_drag_source_add_text_targets (GtkWidget *widget)
1400 GtkTargetList *target_list;
1402 target_list = gtk_drag_source_get_target_list (widget);
1404 gtk_target_list_ref (target_list);
1406 target_list = gtk_target_list_new (NULL, 0);
1407 gtk_target_list_add_text_targets (target_list, 0);
1408 gtk_drag_source_set_target_list (widget, target_list);
1409 gtk_target_list_unref (target_list);
1413 gtk_drag_source_add_image_targets (GtkWidget *widget)
1415 GtkTargetList *target_list;
1417 target_list = gtk_drag_source_get_target_list (widget);
1419 gtk_target_list_ref (target_list);
1421 target_list = gtk_target_list_new (NULL, 0);
1422 gtk_target_list_add_image_targets (target_list, 0, TRUE);
1423 gtk_drag_source_set_target_list (widget, target_list);
1424 gtk_target_list_unref (target_list);
1428 gtk_drag_source_add_uri_targets (GtkWidget *widget)
1430 GtkTargetList *target_list;
1432 target_list = gtk_drag_source_get_target_list (widget);
1434 gtk_target_list_ref (target_list);
1436 target_list = gtk_target_list_new (NULL, 0);
1437 gtk_target_list_add_uri_targets (target_list, 0);
1438 gtk_drag_source_set_target_list (widget, target_list);
1439 gtk_target_list_unref (target_list);
1443 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
1445 switch (site->icon_type)
1447 case GTK_IMAGE_EMPTY:
1449 case GTK_IMAGE_PIXBUF:
1450 g_object_unref (site->icon_data.pixbuf.pixbuf);
1452 case GTK_IMAGE_STOCK:
1453 g_free (site->icon_data.stock.stock_id);
1455 case GTK_IMAGE_ICON_NAME:
1456 g_free (site->icon_data.name.icon_name);
1459 g_assert_not_reached();
1462 site->icon_type = GTK_IMAGE_EMPTY;
1466 gtk_drag_source_site_destroy (gpointer data)
1468 GtkDragSourceSite *site = data;
1470 if (site->target_list)
1471 gtk_target_list_unref (site->target_list);
1473 gtk_drag_source_unset_icon (site);
1478 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
1481 GtkDragSourceSite *site;
1483 g_return_if_fail (GTK_IS_WIDGET (widget));
1484 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1486 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1487 g_return_if_fail (site != NULL);
1488 g_object_ref (pixbuf);
1490 gtk_drag_source_unset_icon (site);
1492 site->icon_type = GTK_IMAGE_PIXBUF;
1493 site->icon_data.pixbuf.pixbuf = pixbuf;
1497 * gtk_drag_source_set_icon_stock:
1498 * @widget: a #GtkWidget
1499 * @stock_id: the ID of the stock icon to use
1501 * Sets the icon that will be used for drags from a particular source
1505 gtk_drag_source_set_icon_stock (GtkWidget *widget,
1506 const gchar *stock_id)
1508 GtkDragSourceSite *site;
1510 g_return_if_fail (GTK_IS_WIDGET (widget));
1511 g_return_if_fail (stock_id != NULL);
1513 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1514 g_return_if_fail (site != NULL);
1516 gtk_drag_source_unset_icon (site);
1518 site->icon_type = GTK_IMAGE_STOCK;
1519 site->icon_data.stock.stock_id = g_strdup (stock_id);
1523 * gtk_drag_source_set_icon_name:
1524 * @widget: a #GtkWidget
1525 * @icon_name: name of icon to use
1527 * Sets the icon that will be used for drags from a particular source
1528 * to a themed icon. See the docs for #GtkIconTheme for more details.
1533 gtk_drag_source_set_icon_name (GtkWidget *widget,
1534 const gchar *icon_name)
1536 GtkDragSourceSite *site;
1538 g_return_if_fail (GTK_IS_WIDGET (widget));
1539 g_return_if_fail (icon_name != NULL);
1541 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1542 g_return_if_fail (site != NULL);
1544 gtk_drag_source_unset_icon (site);
1546 site->icon_type = GTK_IMAGE_ICON_NAME;
1547 site->icon_data.name.icon_name = g_strdup (icon_name);
1552 * gtk_drag_set_icon_widget:
1553 * @context: the context for a drag. (This must be called
1554 with a context for the source side of a drag)
1555 * @widget: a toplevel window to use as an icon.
1556 * @hot_x: the X offset within @widget of the hotspot.
1557 * @hot_y: the Y offset within @widget of the hotspot.
1559 * Changes the icon for a widget to a given widget. GTK+
1560 * will not destroy the icon, so if you don't want
1561 * it to persist, you should connect to the "drag-end"
1562 * signal and destroy it yourself.
1565 gtk_drag_set_icon_widget (GdkDragContext *context,
1570 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1571 g_return_if_fail (context->is_source);
1572 g_return_if_fail (GTK_IS_WIDGET (widget));
1574 g_warning ("gtk_drag_set_icon_widget is not supported on Mac OS X");
1578 set_icon_stock_pixbuf (GdkDragContext *context,
1579 const gchar *stock_id,
1584 GtkDragSourceInfo *info;
1586 info = gtk_drag_get_source_info (context, FALSE);
1590 pixbuf = gtk_widget_render_icon (info->widget, stock_id,
1591 GTK_ICON_SIZE_DND, NULL);
1595 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
1600 g_object_ref (pixbuf);
1602 if (info->icon_pixbuf)
1603 g_object_unref (info->icon_pixbuf);
1604 info->icon_pixbuf = pixbuf;
1605 info->hot_x = hot_x;
1606 info->hot_y = hot_y;
1610 * gtk_drag_set_icon_pixbuf:
1611 * @context: the context for a drag. (This must be called
1612 * with a context for the source side of a drag)
1613 * @pixbuf: the #GdkPixbuf to use as the drag icon.
1614 * @hot_x: the X offset within @widget of the hotspot.
1615 * @hot_y: the Y offset within @widget of the hotspot.
1617 * Sets @pixbuf as the icon for a given drag.
1620 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
1625 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1626 g_return_if_fail (context->is_source);
1627 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1629 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1633 * gtk_drag_set_icon_stock:
1634 * @context: the context for a drag. (This must be called
1635 * with a context for the source side of a drag)
1636 * @stock_id: the ID of the stock icon to use for the drag.
1637 * @hot_x: the X offset within the icon of the hotspot.
1638 * @hot_y: the Y offset within the icon of the hotspot.
1640 * Sets the icon for a given drag from a stock ID.
1643 gtk_drag_set_icon_stock (GdkDragContext *context,
1644 const gchar *stock_id,
1649 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1650 g_return_if_fail (context->is_source);
1651 g_return_if_fail (stock_id != NULL);
1653 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
1657 * gtk_drag_set_icon_name:
1658 * @context: the context for a drag. (This must be called
1659 * with a context for the source side of a drag)
1660 * @icon_name: name of icon to use
1661 * @hot_x: the X offset of the hotspot within the icon
1662 * @hot_y: the Y offset of the hotspot within the icon
1664 * Sets the icon for a given drag from a named themed icon. See
1665 * the docs for #GtkIconTheme for more details. Note that the
1666 * size of the icon depends on the icon theme (the icon is
1667 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
1668 * @hot_x and @hot_y have to be used with care.
1673 gtk_drag_set_icon_name (GdkDragContext *context,
1674 const gchar *icon_name,
1679 GtkSettings *settings;
1680 GtkIconTheme *icon_theme;
1682 gint width, height, icon_size;
1684 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1685 g_return_if_fail (context->is_source);
1686 g_return_if_fail (icon_name != NULL);
1688 screen = gdk_window_get_screen (context->source_window);
1689 g_return_if_fail (screen != NULL);
1691 settings = gtk_settings_get_for_screen (screen);
1692 if (gtk_icon_size_lookup_for_settings (settings,
1695 icon_size = MAX (width, height);
1697 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
1699 icon_theme = gtk_icon_theme_get_for_screen (screen);
1701 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
1702 icon_size, 0, NULL);
1704 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1706 g_warning ("Cannot load drag icon from icon name %s", icon_name);
1710 * gtk_drag_set_icon_default:
1711 * @context: the context for a drag. (This must be called
1712 with a context for the source side of a drag)
1714 * Sets the icon for a particular drag to the default
1718 gtk_drag_set_icon_default (GdkDragContext *context)
1720 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1721 g_return_if_fail (context->is_source);
1723 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
1727 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
1729 if (info->icon_pixbuf)
1730 g_object_unref (info->icon_pixbuf);
1732 g_signal_emit_by_name (info->widget, "drag-end",
1735 if (info->source_widget)
1736 g_object_unref (info->source_widget);
1739 g_object_unref (info->widget);
1741 gtk_target_list_unref (info->target_list);
1743 gtk_drag_clear_source_info (info->context);
1744 g_object_unref (info->context);
1750 drag_drop_finished_idle_cb (gpointer data)
1752 gtk_drag_source_info_destroy (data);
1757 gtk_drag_drop_finished (GtkDragSourceInfo *info)
1759 if (info->success && info->delete)
1760 g_signal_emit_by_name (info->source_widget, "drag-data-delete",
1763 /* Workaround for the fact that the NS API blocks until the drag is
1764 * over. This way the context is still valid when returning from
1765 * drag_begin, even if it will still be quite useless. See bug #501588.
1767 g_idle_add (drag_drop_finished_idle_cb, info);
1770 /*************************************************************
1771 * _gtk_drag_source_handle_event:
1772 * Called from widget event handling code on Drag events
1776 * toplevel: Toplevel widget that received the event
1779 *************************************************************/
1782 _gtk_drag_source_handle_event (GtkWidget *widget,
1785 GtkDragSourceInfo *info;
1786 GdkDragContext *context;
1788 g_return_if_fail (widget != NULL);
1789 g_return_if_fail (event != NULL);
1791 context = event->dnd.context;
1792 info = gtk_drag_get_source_info (context, FALSE);
1796 switch (event->type)
1798 case GDK_DROP_FINISHED:
1799 gtk_drag_drop_finished (info);
1802 g_assert_not_reached ();
1808 gtk_drag_check_threshold (GtkWidget *widget,
1814 gint drag_threshold;
1816 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1818 g_object_get (gtk_widget_get_settings (widget),
1819 "gtk-dnd-drag-threshold", &drag_threshold,
1822 return (ABS (current_x - start_x) > drag_threshold ||
1823 ABS (current_y - start_y) > drag_threshold);