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 copy]];
466 gtk_drag_dest_realized (GtkWidget *widget,
469 GtkDragDestSite *site = user_data;
471 register_types (widget, site);
475 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
476 GtkWidget *previous_toplevel,
479 GtkDragDestSite *site = user_data;
481 register_types (widget, site);
485 gtk_drag_dest_site_destroy (gpointer data)
487 GtkDragDestSite *site = data;
489 if (site->target_list)
490 gtk_target_list_unref (site->target_list);
496 gtk_drag_dest_set (GtkWidget *widget,
497 GtkDestDefaults flags,
498 const GtkTargetEntry *targets,
500 GdkDragAction actions)
502 GtkDragDestSite *old_site, *site;
504 g_return_if_fail (GTK_IS_WIDGET (widget));
506 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
508 gtk_drag_dest_unset (widget);
510 site = g_new (GtkDragDestSite, 1);
512 site->have_drag = FALSE;
514 site->target_list = gtk_target_list_new (targets, n_targets);
516 site->target_list = NULL;
517 site->actions = actions;
520 site->track_motion = old_site->track_motion;
522 site->track_motion = FALSE;
524 if (GTK_WIDGET_REALIZED (widget))
525 gtk_drag_dest_realized (widget, site);
527 g_signal_connect (widget, "realize",
528 G_CALLBACK (gtk_drag_dest_realized), site);
529 g_signal_connect (widget, "hierarchy-changed",
530 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
532 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
533 site, gtk_drag_dest_site_destroy);
537 gtk_drag_dest_set_proxy (GtkWidget *widget,
538 GdkWindow *proxy_window,
539 GdkDragProtocol protocol,
540 gboolean use_coordinates)
542 g_warning ("gtk_drag_dest_set_proxy is not supported on Mac OS X.");
546 gtk_drag_dest_unset (GtkWidget *widget)
548 GtkDragDestSite *old_site;
550 g_return_if_fail (GTK_IS_WIDGET (widget));
552 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
555 g_signal_handlers_disconnect_by_func (widget,
556 gtk_drag_dest_realized,
558 g_signal_handlers_disconnect_by_func (widget,
559 gtk_drag_dest_hierarchy_changed,
563 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
567 gtk_drag_dest_get_target_list (GtkWidget *widget)
569 GtkDragDestSite *site;
571 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
573 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
575 return site ? site->target_list : NULL;
579 gtk_drag_dest_set_target_list (GtkWidget *widget,
580 GtkTargetList *target_list)
582 GtkDragDestSite *site;
584 g_return_if_fail (GTK_IS_WIDGET (widget));
586 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
590 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
591 "to make the widget into a drag destination");
596 gtk_target_list_ref (target_list);
598 if (site->target_list)
599 gtk_target_list_unref (site->target_list);
601 site->target_list = target_list;
603 register_types (widget, site);
607 gtk_drag_dest_add_text_targets (GtkWidget *widget)
609 GtkTargetList *target_list;
611 target_list = gtk_drag_dest_get_target_list (widget);
613 gtk_target_list_ref (target_list);
615 target_list = gtk_target_list_new (NULL, 0);
616 gtk_target_list_add_text_targets (target_list, 0);
617 gtk_drag_dest_set_target_list (widget, target_list);
618 gtk_target_list_unref (target_list);
622 gtk_drag_dest_add_image_targets (GtkWidget *widget)
624 GtkTargetList *target_list;
626 target_list = gtk_drag_dest_get_target_list (widget);
628 gtk_target_list_ref (target_list);
630 target_list = gtk_target_list_new (NULL, 0);
631 gtk_target_list_add_image_targets (target_list, 0, FALSE);
632 gtk_drag_dest_set_target_list (widget, target_list);
633 gtk_target_list_unref (target_list);
637 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
639 GtkTargetList *target_list;
641 target_list = gtk_drag_dest_get_target_list (widget);
643 gtk_target_list_ref (target_list);
645 target_list = gtk_target_list_new (NULL, 0);
646 gtk_target_list_add_uri_targets (target_list, 0);
647 gtk_drag_dest_set_target_list (widget, target_list);
648 gtk_target_list_unref (target_list);
652 prepend_and_ref_widget (GtkWidget *widget,
655 GSList **slist_p = data;
657 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
661 gtk_drag_find_widget (GtkWidget *widget,
662 GtkDragFindData *data)
664 GtkAllocation new_allocation;
665 gint allocation_to_window_x = 0;
666 gint allocation_to_window_y = 0;
670 if (data->found || !GTK_WIDGET_MAPPED (widget) || !GTK_WIDGET_SENSITIVE (widget))
673 /* Note that in the following code, we only count the
674 * position as being inside a WINDOW widget if it is inside
675 * widget->window; points that are outside of widget->window
676 * but within the allocation are not counted. This is consistent
677 * with the way we highlight drag targets.
679 * data->x,y are relative to widget->parent->window (if
680 * widget is not a toplevel, widget->window otherwise).
681 * We compute the allocation of widget in the same coordinates,
682 * clipping to widget->window, and all intermediate
683 * windows. If data->x,y is inside that, then we translate
684 * our coordinates to be relative to widget->window and
687 new_allocation = widget->allocation;
692 GdkWindow *window = widget->window;
694 /* Compute the offset from allocation-relative to
695 * window-relative coordinates.
697 allocation_to_window_x = widget->allocation.x;
698 allocation_to_window_y = widget->allocation.y;
700 if (!GTK_WIDGET_NO_WINDOW (widget))
702 /* The allocation is relative to the parent window for
703 * window widgets, not to widget->window.
705 gdk_window_get_position (window, &tx, &ty);
707 allocation_to_window_x -= tx;
708 allocation_to_window_y -= ty;
711 new_allocation.x = 0 + allocation_to_window_x;
712 new_allocation.y = 0 + allocation_to_window_y;
714 while (window && window != widget->parent->window)
716 GdkRectangle window_rect = { 0, 0, 0, 0 };
718 gdk_drawable_get_size (window, &window_rect.width, &window_rect.height);
720 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
722 gdk_window_get_position (window, &tx, &ty);
723 new_allocation.x += tx;
725 new_allocation.y += ty;
728 window = gdk_window_get_parent (window);
731 if (!window) /* Window and widget heirarchies didn't match. */
735 if (data->toplevel ||
736 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
737 (data->x < new_allocation.x + new_allocation.width) &&
738 (data->y < new_allocation.y + new_allocation.height)))
740 /* First, check if the drag is in a valid drop site in
741 * one of our children
743 if (GTK_IS_CONTAINER (widget))
745 GtkDragFindData new_data = *data;
746 GSList *children = NULL;
749 new_data.x -= x_offset;
750 new_data.y -= y_offset;
751 new_data.found = FALSE;
752 new_data.toplevel = FALSE;
754 /* need to reference children temporarily in case the
755 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
757 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
758 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
760 if (!new_data.found && GTK_WIDGET_DRAWABLE (tmp_list->data))
761 gtk_drag_find_widget (tmp_list->data, &new_data);
762 g_object_unref (tmp_list->data);
764 g_slist_free (children);
766 data->found = new_data.found;
769 /* If not, and this widget is registered as a drop site, check to
770 * emit "drag-motion" to check if we are actually in
774 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
776 data->found = data->callback (widget,
778 data->x - x_offset - allocation_to_window_x,
779 data->y - y_offset - allocation_to_window_y,
781 /* If so, send a "drag-leave" to the last widget */
784 if (data->info->widget && data->info->widget != widget)
786 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
788 data->info->widget = widget;
795 gtk_drag_dest_leave (GtkWidget *widget,
796 GdkDragContext *context,
799 GtkDragDestSite *site;
801 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
802 g_return_if_fail (site != NULL);
804 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
805 gtk_drag_unhighlight (widget);
807 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
809 g_signal_emit_by_name (widget, "drag-leave", context, time);
811 site->have_drag = FALSE;
815 gtk_drag_dest_motion (GtkWidget *widget,
816 GdkDragContext *context,
821 GtkDragDestSite *site;
822 GdkDragAction action = 0;
825 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
826 g_return_val_if_fail (site != NULL, FALSE);
828 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
830 if (context->suggested_action & site->actions)
831 action = context->suggested_action;
833 if (action && gtk_drag_dest_find_target (widget, context, NULL))
835 if (!site->have_drag)
837 site->have_drag = TRUE;
838 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
839 gtk_drag_highlight (widget);
842 gdk_drag_status (context, action, time);
846 gdk_drag_status (context, 0, time);
847 if (!site->track_motion)
852 g_signal_emit_by_name (widget, "drag-motion",
853 context, x, y, time, &retval);
855 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
859 gtk_drag_dest_drop (GtkWidget *widget,
860 GdkDragContext *context,
865 GtkDragDestSite *site;
866 GtkDragDestInfo *info;
869 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
870 g_return_val_if_fail (site != NULL, FALSE);
872 info = gtk_drag_get_dest_info (context, FALSE);
873 g_return_val_if_fail (info != NULL, FALSE);
878 if (site->flags & GTK_DEST_DEFAULT_DROP)
880 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
882 if (target == GDK_NONE)
884 gtk_drag_finish (context, FALSE, FALSE, time);
888 gtk_drag_get_data (widget, context, target, time);
891 g_signal_emit_by_name (widget, "drag-drop",
892 context, x, y, time, &retval);
894 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
898 gtk_drag_dest_set_track_motion (GtkWidget *widget,
899 gboolean track_motion)
901 GtkDragDestSite *site;
903 g_return_if_fail (GTK_IS_WIDGET (widget));
905 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
907 g_return_if_fail (site != NULL);
909 site->track_motion = track_motion != FALSE;
913 gtk_drag_dest_get_track_motion (GtkWidget *widget)
915 GtkDragDestSite *site;
917 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
919 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
922 return site->track_motion;
928 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
931 GtkDragDestInfo *info;
932 GdkDragContext *context;
934 g_return_if_fail (toplevel != NULL);
935 g_return_if_fail (event != NULL);
937 context = event->dnd.context;
939 info = gtk_drag_get_dest_info (context, TRUE);
941 /* Find the widget for the event */
950 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
955 case GDK_DRAG_MOTION:
958 GtkDragFindData data;
961 if (event->type == GDK_DROP_START)
963 info->dropped = TRUE;
964 /* We send a leave here so that the widget unhighlights
969 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
974 gdk_window_get_position (toplevel->window, &tx, &ty);
976 data.x = event->dnd.x_root - tx;
977 data.y = event->dnd.y_root - ty;
978 data.context = context;
981 data.toplevel = TRUE;
982 data.callback = (event->type == GDK_DRAG_MOTION) ?
983 gtk_drag_dest_motion : gtk_drag_dest_drop;
984 data.time = event->dnd.time;
986 gtk_drag_find_widget (toplevel, &data);
988 if (info->widget && !data.found)
990 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
996 if (event->type == GDK_DRAG_MOTION)
999 gdk_drag_status (context, 0, event->dnd.time);
1004 g_assert_not_reached ();
1011 gtk_drag_dest_find_target (GtkWidget *widget,
1012 GdkDragContext *context,
1013 GtkTargetList *target_list)
1015 id <NSDraggingInfo> dragging_info;
1016 NSPasteboard *pasteboard;
1017 GtkWidget *source_widget;
1019 GList *tmp_source = NULL;
1020 GList *source_targets;
1022 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1023 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1024 g_return_val_if_fail (!context->is_source, GDK_NONE);
1026 dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
1027 pasteboard = [dragging_info draggingPasteboard];
1029 source_widget = gtk_drag_get_source_widget (context);
1031 if (target_list == NULL)
1032 target_list = gtk_drag_dest_get_target_list (widget);
1034 if (target_list == NULL)
1037 source_targets = _gtk_quartz_pasteboard_types_to_atom_list ([pasteboard types]);
1038 tmp_target = target_list->list;
1041 GtkTargetPair *pair = tmp_target->data;
1042 tmp_source = source_targets;
1045 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1047 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1048 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
1050 g_list_free (source_targets);
1051 return pair->target;
1056 tmp_source = tmp_source->next;
1058 tmp_target = tmp_target->next;
1061 g_list_free (source_targets);
1066 gtk_drag_begin_idle (gpointer arg)
1068 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1069 GdkDragContext* context = (GdkDragContext*) arg;
1070 GtkDragSourceInfo* info = gtk_drag_get_source_info (context, FALSE);
1072 NSPasteboard *pasteboard;
1073 GtkDragSourceOwner *owner;
1076 g_assert (info != NULL);
1078 pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
1079 owner = [[GtkDragSourceOwner alloc] initWithInfo:info];
1081 [pasteboard declareTypes:_gtk_quartz_target_list_to_pasteboard_types (info->target_list) owner:owner];
1083 if ((nswindow = get_toplevel_nswindow (info->source_widget)) == NULL)
1086 /* Ref the context. It's unreffed when the drag has been aborted */
1087 g_object_ref (info->context);
1089 /* FIXME: If the event isn't a mouse event, use the global cursor position instead */
1090 point = [info->nsevent locationInWindow];
1092 [nswindow dragImage:_gtk_quartz_create_image_from_pixbuf (info->icon_pixbuf)
1094 offset:NSMakeSize(0, 0)
1096 pasteboard:pasteboard
1100 [info->nsevent release];
1107 static GdkDragContext *
1108 gtk_drag_begin_internal (GtkWidget *widget,
1109 GtkDragSourceSite *site,
1110 GtkTargetList *target_list,
1111 GdkDragAction actions,
1115 GtkDragSourceInfo *info;
1116 GdkDragContext *context;
1119 context = gdk_drag_begin (NULL, NULL);
1120 context->is_source = TRUE;
1122 info = gtk_drag_get_source_info (context, TRUE);
1124 info->source_widget = g_object_ref (widget);
1125 info->widget = g_object_ref (widget);
1126 info->target_list = target_list;
1127 gtk_target_list_ref (target_list);
1129 info->possible_actions = actions;
1131 g_signal_emit_by_name (widget, "drag-begin", info->context);
1133 /* Ensure that we have an icon before we start the drag; the
1134 * application may have set one in ::drag_begin, or it may
1137 if (!info->icon_pixbuf)
1139 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
1140 gtk_drag_set_icon_default (context);
1142 switch (site->icon_type)
1144 case GTK_IMAGE_PIXMAP:
1145 /* This is not supported, so just set a small transparent pixbuf
1146 * since we need to have something.
1149 gtk_drag_set_icon_pixmap (context,
1151 site->icon_data.pixmap.pixmap,
1158 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 1, 1);
1159 gdk_pixbuf_fill (pixbuf, 0xffffff);
1161 gtk_drag_set_icon_pixbuf (context,
1165 g_object_unref (pixbuf);
1168 case GTK_IMAGE_PIXBUF:
1169 gtk_drag_set_icon_pixbuf (context,
1170 site->icon_data.pixbuf.pixbuf,
1173 case GTK_IMAGE_STOCK:
1174 gtk_drag_set_icon_stock (context,
1175 site->icon_data.stock.stock_id,
1178 case GTK_IMAGE_ICON_NAME:
1179 gtk_drag_set_icon_name (context,
1180 site->icon_data.name.icon_name,
1183 case GTK_IMAGE_EMPTY:
1185 g_assert_not_reached();
1190 nswindow = get_toplevel_nswindow (widget);
1191 info->nsevent = [nswindow currentEvent];
1192 [info->nsevent retain];
1194 /* drag will begin in an idle handler to avoid nested run loops */
1196 g_idle_add_full (G_PRIORITY_HIGH_IDLE, gtk_drag_begin_idle, context, NULL);
1198 gdk_pointer_ungrab (0);
1204 gtk_drag_begin (GtkWidget *widget,
1205 GtkTargetList *targets,
1206 GdkDragAction actions,
1210 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1211 g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), NULL);
1212 g_return_val_if_fail (targets != NULL, NULL);
1214 return gtk_drag_begin_internal (widget, NULL, targets,
1215 actions, button, event);
1220 gtk_drag_source_event_cb (GtkWidget *widget,
1224 GtkDragSourceSite *site;
1225 gboolean retval = FALSE;
1226 site = (GtkDragSourceSite *)data;
1228 switch (event->type)
1230 case GDK_BUTTON_PRESS:
1231 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1233 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
1234 site->x = event->button.x;
1235 site->y = event->button.y;
1239 case GDK_BUTTON_RELEASE:
1240 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1241 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
1244 case GDK_MOTION_NOTIFY:
1245 if (site->state & event->motion.state & site->start_button_mask)
1247 /* FIXME: This is really broken and can leave us
1253 if (site->state & event->motion.state &
1254 GDK_BUTTON1_MASK << (i - 1))
1258 if (gtk_drag_check_threshold (widget, site->x, site->y,
1259 event->motion.x, event->motion.y))
1262 gtk_drag_begin_internal (widget, site, site->target_list,
1271 default: /* hit for 2/3BUTTON_PRESS */
1279 gtk_drag_source_set (GtkWidget *widget,
1280 GdkModifierType start_button_mask,
1281 const GtkTargetEntry *targets,
1283 GdkDragAction actions)
1285 GtkDragSourceSite *site;
1287 g_return_if_fail (GTK_IS_WIDGET (widget));
1289 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1291 gtk_widget_add_events (widget,
1292 gtk_widget_get_events (widget) |
1293 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1294 GDK_BUTTON_MOTION_MASK);
1298 if (site->target_list)
1299 gtk_target_list_unref (site->target_list);
1303 site = g_new0 (GtkDragSourceSite, 1);
1305 site->icon_type = GTK_IMAGE_EMPTY;
1307 g_signal_connect (widget, "button-press-event",
1308 G_CALLBACK (gtk_drag_source_event_cb),
1310 g_signal_connect (widget, "button-release-event",
1311 G_CALLBACK (gtk_drag_source_event_cb),
1313 g_signal_connect (widget, "motion-notify-event",
1314 G_CALLBACK (gtk_drag_source_event_cb),
1317 g_object_set_data_full (G_OBJECT (widget),
1318 I_("gtk-site-data"),
1319 site, gtk_drag_source_site_destroy);
1322 site->start_button_mask = start_button_mask;
1324 site->target_list = gtk_target_list_new (targets, n_targets);
1326 site->actions = actions;
1329 /*************************************************************
1330 * gtk_drag_source_unset
1331 * Unregister this widget as a drag source.
1335 *************************************************************/
1338 gtk_drag_source_unset (GtkWidget *widget)
1340 GtkDragSourceSite *site;
1342 g_return_if_fail (GTK_IS_WIDGET (widget));
1344 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1348 g_signal_handlers_disconnect_by_func (widget,
1349 gtk_drag_source_event_cb,
1351 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
1356 gtk_drag_source_get_target_list (GtkWidget *widget)
1358 GtkDragSourceSite *site;
1360 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1362 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1364 return site ? site->target_list : NULL;
1369 gtk_drag_source_set_target_list (GtkWidget *widget,
1370 GtkTargetList *target_list)
1372 GtkDragSourceSite *site;
1374 g_return_if_fail (GTK_IS_WIDGET (widget));
1376 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1379 g_warning ("gtk_drag_source_set_target_list() requires the widget "
1380 "to already be a drag source.");
1385 gtk_target_list_ref (target_list);
1387 if (site->target_list)
1388 gtk_target_list_unref (site->target_list);
1390 site->target_list = target_list;
1394 * gtk_drag_source_add_text_targets:
1395 * @widget: a #GtkWidget that's is a drag source
1397 * Add the text targets supported by #GtkSelection to
1398 * the target list of the drag source. The targets
1399 * are added with @info = 0. If you need another value,
1400 * use gtk_target_list_add_text_targets() and
1401 * gtk_drag_source_set_target_list().
1406 gtk_drag_source_add_text_targets (GtkWidget *widget)
1408 GtkTargetList *target_list;
1410 target_list = gtk_drag_source_get_target_list (widget);
1412 gtk_target_list_ref (target_list);
1414 target_list = gtk_target_list_new (NULL, 0);
1415 gtk_target_list_add_text_targets (target_list, 0);
1416 gtk_drag_source_set_target_list (widget, target_list);
1417 gtk_target_list_unref (target_list);
1421 gtk_drag_source_add_image_targets (GtkWidget *widget)
1423 GtkTargetList *target_list;
1425 target_list = gtk_drag_source_get_target_list (widget);
1427 gtk_target_list_ref (target_list);
1429 target_list = gtk_target_list_new (NULL, 0);
1430 gtk_target_list_add_image_targets (target_list, 0, TRUE);
1431 gtk_drag_source_set_target_list (widget, target_list);
1432 gtk_target_list_unref (target_list);
1436 gtk_drag_source_add_uri_targets (GtkWidget *widget)
1438 GtkTargetList *target_list;
1440 target_list = gtk_drag_source_get_target_list (widget);
1442 gtk_target_list_ref (target_list);
1444 target_list = gtk_target_list_new (NULL, 0);
1445 gtk_target_list_add_uri_targets (target_list, 0);
1446 gtk_drag_source_set_target_list (widget, target_list);
1447 gtk_target_list_unref (target_list);
1451 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
1453 switch (site->icon_type)
1455 case GTK_IMAGE_EMPTY:
1457 case GTK_IMAGE_PIXMAP:
1458 if (site->icon_data.pixmap.pixmap)
1459 g_object_unref (site->icon_data.pixmap.pixmap);
1460 if (site->icon_mask)
1461 g_object_unref (site->icon_mask);
1463 case GTK_IMAGE_PIXBUF:
1464 g_object_unref (site->icon_data.pixbuf.pixbuf);
1466 case GTK_IMAGE_STOCK:
1467 g_free (site->icon_data.stock.stock_id);
1469 case GTK_IMAGE_ICON_NAME:
1470 g_free (site->icon_data.name.icon_name);
1473 g_assert_not_reached();
1476 site->icon_type = GTK_IMAGE_EMPTY;
1479 g_object_unref (site->colormap);
1480 site->colormap = NULL;
1484 gtk_drag_source_site_destroy (gpointer data)
1486 GtkDragSourceSite *site = data;
1488 if (site->target_list)
1489 gtk_target_list_unref (site->target_list);
1491 gtk_drag_source_unset_icon (site);
1496 gtk_drag_source_set_icon (GtkWidget *widget,
1497 GdkColormap *colormap,
1501 GtkDragSourceSite *site;
1503 g_return_if_fail (GTK_IS_WIDGET (widget));
1504 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1505 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
1506 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
1508 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1509 g_return_if_fail (site != NULL);
1511 g_object_ref (colormap);
1512 g_object_ref (pixmap);
1514 g_object_ref (mask);
1516 gtk_drag_source_unset_icon (site);
1518 site->icon_type = GTK_IMAGE_PIXMAP;
1520 site->icon_data.pixmap.pixmap = pixmap;
1521 site->icon_mask = mask;
1522 site->colormap = colormap;
1526 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
1529 GtkDragSourceSite *site;
1531 g_return_if_fail (GTK_IS_WIDGET (widget));
1532 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1534 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1535 g_return_if_fail (site != NULL);
1536 g_object_ref (pixbuf);
1538 gtk_drag_source_unset_icon (site);
1540 site->icon_type = GTK_IMAGE_PIXBUF;
1541 site->icon_data.pixbuf.pixbuf = pixbuf;
1545 * gtk_drag_source_set_icon_stock:
1546 * @widget: a #GtkWidget
1547 * @stock_id: the ID of the stock icon to use
1549 * Sets the icon that will be used for drags from a particular source
1553 gtk_drag_source_set_icon_stock (GtkWidget *widget,
1554 const gchar *stock_id)
1556 GtkDragSourceSite *site;
1558 g_return_if_fail (GTK_IS_WIDGET (widget));
1559 g_return_if_fail (stock_id != NULL);
1561 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1562 g_return_if_fail (site != NULL);
1564 gtk_drag_source_unset_icon (site);
1566 site->icon_type = GTK_IMAGE_STOCK;
1567 site->icon_data.stock.stock_id = g_strdup (stock_id);
1571 * gtk_drag_source_set_icon_name:
1572 * @widget: a #GtkWidget
1573 * @icon_name: name of icon to use
1575 * Sets the icon that will be used for drags from a particular source
1576 * to a themed icon. See the docs for #GtkIconTheme for more details.
1581 gtk_drag_source_set_icon_name (GtkWidget *widget,
1582 const gchar *icon_name)
1584 GtkDragSourceSite *site;
1586 g_return_if_fail (GTK_IS_WIDGET (widget));
1587 g_return_if_fail (icon_name != NULL);
1589 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1590 g_return_if_fail (site != NULL);
1592 gtk_drag_source_unset_icon (site);
1594 site->icon_type = GTK_IMAGE_ICON_NAME;
1595 site->icon_data.name.icon_name = g_strdup (icon_name);
1600 * gtk_drag_set_icon_widget:
1601 * @context: the context for a drag. (This must be called
1602 with a context for the source side of a drag)
1603 * @widget: a toplevel window to use as an icon.
1604 * @hot_x: the X offset within @widget of the hotspot.
1605 * @hot_y: the Y offset within @widget of the hotspot.
1607 * Changes the icon for a widget to a given widget. GTK+
1608 * will not destroy the icon, so if you don't want
1609 * it to persist, you should connect to the "drag-end"
1610 * signal and destroy it yourself.
1613 gtk_drag_set_icon_widget (GdkDragContext *context,
1618 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1619 g_return_if_fail (context->is_source);
1620 g_return_if_fail (GTK_IS_WIDGET (widget));
1622 g_warning ("gtk_drag_set_icon_widget is not supported on Mac OS X");
1626 set_icon_stock_pixbuf (GdkDragContext *context,
1627 const gchar *stock_id,
1632 GtkDragSourceInfo *info;
1634 info = gtk_drag_get_source_info (context, FALSE);
1638 pixbuf = gtk_widget_render_icon (info->widget, stock_id,
1639 GTK_ICON_SIZE_DND, NULL);
1643 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
1648 g_object_ref (pixbuf);
1650 if (info->icon_pixbuf)
1651 g_object_unref (info->icon_pixbuf);
1652 info->icon_pixbuf = pixbuf;
1653 info->hot_x = hot_x;
1654 info->hot_y = hot_y;
1658 * gtk_drag_set_icon_pixbuf:
1659 * @context: the context for a drag. (This must be called
1660 * with a context for the source side of a drag)
1661 * @pixbuf: the #GdkPixbuf to use as the drag icon.
1662 * @hot_x: the X offset within @widget of the hotspot.
1663 * @hot_y: the Y offset within @widget of the hotspot.
1665 * Sets @pixbuf as the icon for a given drag.
1668 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
1673 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1674 g_return_if_fail (context->is_source);
1675 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1677 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1681 * gtk_drag_set_icon_stock:
1682 * @context: the context for a drag. (This must be called
1683 * with a context for the source side of a drag)
1684 * @stock_id: the ID of the stock icon to use for the drag.
1685 * @hot_x: the X offset within the icon of the hotspot.
1686 * @hot_y: the Y offset within the icon of the hotspot.
1688 * Sets the icon for a given drag from a stock ID.
1691 gtk_drag_set_icon_stock (GdkDragContext *context,
1692 const gchar *stock_id,
1697 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1698 g_return_if_fail (context->is_source);
1699 g_return_if_fail (stock_id != NULL);
1701 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
1705 * gtk_drag_set_icon_pixmap:
1706 * @context: the context for a drag. (This must be called
1707 * with a context for the source side of a drag)
1708 * @colormap: the colormap of the icon
1709 * @pixmap: the image data for the icon
1710 * @mask: the transparency mask for the icon
1711 * @hot_x: the X offset within @pixmap of the hotspot.
1712 * @hot_y: the Y offset within @pixmap of the hotspot.
1714 * Sets @pixmap as the icon for a given drag. GTK+ retains
1715 * references for the arguments, and will release them when
1716 * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
1717 * will be more convenient to use.
1720 gtk_drag_set_icon_pixmap (GdkDragContext *context,
1721 GdkColormap *colormap,
1729 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1730 g_return_if_fail (context->is_source);
1731 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1732 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
1734 pixbuf = gdk_pixbuf_get_from_drawable (NULL, pixmap, colormap,
1739 gtk_drag_set_icon_pixbuf (context, pixbuf, hot_x, hot_y);
1740 g_object_unref (pixbuf);
1744 * gtk_drag_set_icon_name:
1745 * @context: the context for a drag. (This must be called
1746 * with a context for the source side of a drag)
1747 * @icon_name: name of icon to use
1748 * @hot_x: the X offset of the hotspot within the icon
1749 * @hot_y: the Y offset of the hotspot within the icon
1751 * Sets the icon for a given drag from a named themed icon. See
1752 * the docs for #GtkIconTheme for more details. Note that the
1753 * size of the icon depends on the icon theme (the icon is
1754 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
1755 * @hot_x and @hot_y have to be used with care.
1760 gtk_drag_set_icon_name (GdkDragContext *context,
1761 const gchar *icon_name,
1766 GtkSettings *settings;
1767 GtkIconTheme *icon_theme;
1769 gint width, height, icon_size;
1771 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1772 g_return_if_fail (context->is_source);
1773 g_return_if_fail (icon_name != NULL);
1775 screen = gdk_drawable_get_screen (context->source_window);
1776 g_return_if_fail (screen != NULL);
1778 settings = gtk_settings_get_for_screen (screen);
1779 if (gtk_icon_size_lookup_for_settings (settings,
1782 icon_size = MAX (width, height);
1784 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
1786 icon_theme = gtk_icon_theme_get_for_screen (screen);
1788 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
1789 icon_size, 0, NULL);
1791 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1793 g_warning ("Cannot load drag icon from icon name %s", icon_name);
1797 * gtk_drag_set_icon_default:
1798 * @context: the context for a drag. (This must be called
1799 with a context for the source side of a drag)
1801 * Sets the icon for a particular drag to the default
1805 gtk_drag_set_icon_default (GdkDragContext *context)
1807 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1808 g_return_if_fail (context->is_source);
1810 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
1814 gtk_drag_set_default_icon (GdkColormap *colormap,
1820 g_warning ("gtk_drag_set_default_icon is not supported on Mac OS X.");
1824 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
1826 if (info->icon_pixbuf)
1827 g_object_unref (info->icon_pixbuf);
1829 g_signal_emit_by_name (info->widget, "drag-end",
1832 if (info->source_widget)
1833 g_object_unref (info->source_widget);
1836 g_object_unref (info->widget);
1838 gtk_target_list_unref (info->target_list);
1840 gtk_drag_clear_source_info (info->context);
1841 g_object_unref (info->context);
1847 drag_drop_finished_idle_cb (gpointer data)
1849 gtk_drag_source_info_destroy (data);
1854 gtk_drag_drop_finished (GtkDragSourceInfo *info)
1856 if (info->success && info->delete)
1857 g_signal_emit_by_name (info->source_widget, "drag-data-delete",
1860 /* Workaround for the fact that the NS API blocks until the drag is
1861 * over. This way the context is still valid when returning from
1862 * drag_begin, even if it will still be quite useless. See bug #501588.
1864 g_idle_add (drag_drop_finished_idle_cb, info);
1867 /*************************************************************
1868 * _gtk_drag_source_handle_event:
1869 * Called from widget event handling code on Drag events
1873 * toplevel: Toplevel widget that received the event
1876 *************************************************************/
1879 _gtk_drag_source_handle_event (GtkWidget *widget,
1882 GtkDragSourceInfo *info;
1883 GdkDragContext *context;
1885 g_return_if_fail (widget != NULL);
1886 g_return_if_fail (event != NULL);
1888 context = event->dnd.context;
1889 info = gtk_drag_get_source_info (context, FALSE);
1893 switch (event->type)
1895 case GDK_DROP_FINISHED:
1896 gtk_drag_drop_finished (info);
1899 g_assert_not_reached ();
1905 gtk_drag_check_threshold (GtkWidget *widget,
1911 gint drag_threshold;
1913 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1915 g_object_get (gtk_widget_get_settings (widget),
1916 "gtk-dnd-drag-threshold", &drag_threshold,
1919 return (ABS (current_x - start_x) > drag_threshold ||
1920 ABS (current_y - start_y) > drag_threshold);
1923 #define __GTK_DND_C__
1924 #include "gtkaliasdef.c"