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 allObjects]];
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 NSImage *drag_image;
1078 g_assert (info != NULL);
1080 pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
1081 owner = [[GtkDragSourceOwner alloc] initWithInfo:info];
1083 types = _gtk_quartz_target_list_to_pasteboard_types (info->target_list);
1085 [pasteboard declareTypes:[types allObjects] owner:owner];
1090 if ((nswindow = get_toplevel_nswindow (info->source_widget)) == NULL)
1093 /* Ref the context. It's unreffed when the drag has been aborted */
1094 g_object_ref (info->context);
1096 /* FIXME: If the event isn't a mouse event, use the global cursor position instead */
1097 point = [info->nsevent locationInWindow];
1099 drag_image = _gtk_quartz_create_image_from_pixbuf (info->icon_pixbuf);
1101 [nswindow dragImage:drag_image
1103 offset:NSMakeSize(0, 0)
1105 pasteboard:pasteboard
1109 [info->nsevent release];
1110 [drag_image release];
1117 static GdkDragContext *
1118 gtk_drag_begin_internal (GtkWidget *widget,
1119 GtkDragSourceSite *site,
1120 GtkTargetList *target_list,
1121 GdkDragAction actions,
1125 GtkDragSourceInfo *info;
1126 GdkDragContext *context;
1129 context = gdk_drag_begin (NULL, NULL);
1130 context->is_source = TRUE;
1132 info = gtk_drag_get_source_info (context, TRUE);
1134 info->source_widget = g_object_ref (widget);
1135 info->widget = g_object_ref (widget);
1136 info->target_list = target_list;
1137 gtk_target_list_ref (target_list);
1139 info->possible_actions = actions;
1141 g_signal_emit_by_name (widget, "drag-begin", info->context);
1143 /* Ensure that we have an icon before we start the drag; the
1144 * application may have set one in ::drag_begin, or it may
1147 if (!info->icon_pixbuf)
1149 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
1150 gtk_drag_set_icon_default (context);
1152 switch (site->icon_type)
1154 case GTK_IMAGE_PIXMAP:
1155 /* This is not supported, so just set a small transparent pixbuf
1156 * since we need to have something.
1159 gtk_drag_set_icon_pixmap (context,
1161 site->icon_data.pixmap.pixmap,
1168 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 1, 1);
1169 gdk_pixbuf_fill (pixbuf, 0xffffff);
1171 gtk_drag_set_icon_pixbuf (context,
1175 g_object_unref (pixbuf);
1178 case GTK_IMAGE_PIXBUF:
1179 gtk_drag_set_icon_pixbuf (context,
1180 site->icon_data.pixbuf.pixbuf,
1183 case GTK_IMAGE_STOCK:
1184 gtk_drag_set_icon_stock (context,
1185 site->icon_data.stock.stock_id,
1188 case GTK_IMAGE_ICON_NAME:
1189 gtk_drag_set_icon_name (context,
1190 site->icon_data.name.icon_name,
1193 case GTK_IMAGE_EMPTY:
1195 g_assert_not_reached();
1200 nswindow = get_toplevel_nswindow (widget);
1201 info->nsevent = [nswindow currentEvent];
1202 [info->nsevent retain];
1204 /* drag will begin in an idle handler to avoid nested run loops */
1206 g_idle_add_full (G_PRIORITY_HIGH_IDLE, gtk_drag_begin_idle, context, NULL);
1208 gdk_pointer_ungrab (0);
1214 gtk_drag_begin (GtkWidget *widget,
1215 GtkTargetList *targets,
1216 GdkDragAction actions,
1220 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1221 g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), NULL);
1222 g_return_val_if_fail (targets != NULL, NULL);
1224 return gtk_drag_begin_internal (widget, NULL, targets,
1225 actions, button, event);
1230 gtk_drag_source_event_cb (GtkWidget *widget,
1234 GtkDragSourceSite *site;
1235 gboolean retval = FALSE;
1236 site = (GtkDragSourceSite *)data;
1238 switch (event->type)
1240 case GDK_BUTTON_PRESS:
1241 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1243 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
1244 site->x = event->button.x;
1245 site->y = event->button.y;
1249 case GDK_BUTTON_RELEASE:
1250 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1251 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
1254 case GDK_MOTION_NOTIFY:
1255 if (site->state & event->motion.state & site->start_button_mask)
1257 /* FIXME: This is really broken and can leave us
1263 if (site->state & event->motion.state &
1264 GDK_BUTTON1_MASK << (i - 1))
1268 if (gtk_drag_check_threshold (widget, site->x, site->y,
1269 event->motion.x, event->motion.y))
1272 gtk_drag_begin_internal (widget, site, site->target_list,
1281 default: /* hit for 2/3BUTTON_PRESS */
1289 gtk_drag_source_set (GtkWidget *widget,
1290 GdkModifierType start_button_mask,
1291 const GtkTargetEntry *targets,
1293 GdkDragAction actions)
1295 GtkDragSourceSite *site;
1297 g_return_if_fail (GTK_IS_WIDGET (widget));
1299 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1301 gtk_widget_add_events (widget,
1302 gtk_widget_get_events (widget) |
1303 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1304 GDK_BUTTON_MOTION_MASK);
1308 if (site->target_list)
1309 gtk_target_list_unref (site->target_list);
1313 site = g_new0 (GtkDragSourceSite, 1);
1315 site->icon_type = GTK_IMAGE_EMPTY;
1317 g_signal_connect (widget, "button-press-event",
1318 G_CALLBACK (gtk_drag_source_event_cb),
1320 g_signal_connect (widget, "button-release-event",
1321 G_CALLBACK (gtk_drag_source_event_cb),
1323 g_signal_connect (widget, "motion-notify-event",
1324 G_CALLBACK (gtk_drag_source_event_cb),
1327 g_object_set_data_full (G_OBJECT (widget),
1328 I_("gtk-site-data"),
1329 site, gtk_drag_source_site_destroy);
1332 site->start_button_mask = start_button_mask;
1334 site->target_list = gtk_target_list_new (targets, n_targets);
1336 site->actions = actions;
1339 /*************************************************************
1340 * gtk_drag_source_unset
1341 * Unregister this widget as a drag source.
1345 *************************************************************/
1348 gtk_drag_source_unset (GtkWidget *widget)
1350 GtkDragSourceSite *site;
1352 g_return_if_fail (GTK_IS_WIDGET (widget));
1354 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1358 g_signal_handlers_disconnect_by_func (widget,
1359 gtk_drag_source_event_cb,
1361 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
1366 gtk_drag_source_get_target_list (GtkWidget *widget)
1368 GtkDragSourceSite *site;
1370 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1372 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1374 return site ? site->target_list : NULL;
1379 gtk_drag_source_set_target_list (GtkWidget *widget,
1380 GtkTargetList *target_list)
1382 GtkDragSourceSite *site;
1384 g_return_if_fail (GTK_IS_WIDGET (widget));
1386 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1389 g_warning ("gtk_drag_source_set_target_list() requires the widget "
1390 "to already be a drag source.");
1395 gtk_target_list_ref (target_list);
1397 if (site->target_list)
1398 gtk_target_list_unref (site->target_list);
1400 site->target_list = target_list;
1404 * gtk_drag_source_add_text_targets:
1405 * @widget: a #GtkWidget that's is a drag source
1407 * Add the text targets supported by #GtkSelection to
1408 * the target list of the drag source. The targets
1409 * are added with @info = 0. If you need another value,
1410 * use gtk_target_list_add_text_targets() and
1411 * gtk_drag_source_set_target_list().
1416 gtk_drag_source_add_text_targets (GtkWidget *widget)
1418 GtkTargetList *target_list;
1420 target_list = gtk_drag_source_get_target_list (widget);
1422 gtk_target_list_ref (target_list);
1424 target_list = gtk_target_list_new (NULL, 0);
1425 gtk_target_list_add_text_targets (target_list, 0);
1426 gtk_drag_source_set_target_list (widget, target_list);
1427 gtk_target_list_unref (target_list);
1431 gtk_drag_source_add_image_targets (GtkWidget *widget)
1433 GtkTargetList *target_list;
1435 target_list = gtk_drag_source_get_target_list (widget);
1437 gtk_target_list_ref (target_list);
1439 target_list = gtk_target_list_new (NULL, 0);
1440 gtk_target_list_add_image_targets (target_list, 0, TRUE);
1441 gtk_drag_source_set_target_list (widget, target_list);
1442 gtk_target_list_unref (target_list);
1446 gtk_drag_source_add_uri_targets (GtkWidget *widget)
1448 GtkTargetList *target_list;
1450 target_list = gtk_drag_source_get_target_list (widget);
1452 gtk_target_list_ref (target_list);
1454 target_list = gtk_target_list_new (NULL, 0);
1455 gtk_target_list_add_uri_targets (target_list, 0);
1456 gtk_drag_source_set_target_list (widget, target_list);
1457 gtk_target_list_unref (target_list);
1461 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
1463 switch (site->icon_type)
1465 case GTK_IMAGE_EMPTY:
1467 case GTK_IMAGE_PIXMAP:
1468 if (site->icon_data.pixmap.pixmap)
1469 g_object_unref (site->icon_data.pixmap.pixmap);
1470 if (site->icon_mask)
1471 g_object_unref (site->icon_mask);
1473 case GTK_IMAGE_PIXBUF:
1474 g_object_unref (site->icon_data.pixbuf.pixbuf);
1476 case GTK_IMAGE_STOCK:
1477 g_free (site->icon_data.stock.stock_id);
1479 case GTK_IMAGE_ICON_NAME:
1480 g_free (site->icon_data.name.icon_name);
1483 g_assert_not_reached();
1486 site->icon_type = GTK_IMAGE_EMPTY;
1489 g_object_unref (site->colormap);
1490 site->colormap = NULL;
1494 gtk_drag_source_site_destroy (gpointer data)
1496 GtkDragSourceSite *site = data;
1498 if (site->target_list)
1499 gtk_target_list_unref (site->target_list);
1501 gtk_drag_source_unset_icon (site);
1506 gtk_drag_source_set_icon (GtkWidget *widget,
1507 GdkColormap *colormap,
1511 GtkDragSourceSite *site;
1513 g_return_if_fail (GTK_IS_WIDGET (widget));
1514 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1515 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
1516 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
1518 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1519 g_return_if_fail (site != NULL);
1521 g_object_ref (colormap);
1522 g_object_ref (pixmap);
1524 g_object_ref (mask);
1526 gtk_drag_source_unset_icon (site);
1528 site->icon_type = GTK_IMAGE_PIXMAP;
1530 site->icon_data.pixmap.pixmap = pixmap;
1531 site->icon_mask = mask;
1532 site->colormap = colormap;
1536 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
1539 GtkDragSourceSite *site;
1541 g_return_if_fail (GTK_IS_WIDGET (widget));
1542 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1544 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1545 g_return_if_fail (site != NULL);
1546 g_object_ref (pixbuf);
1548 gtk_drag_source_unset_icon (site);
1550 site->icon_type = GTK_IMAGE_PIXBUF;
1551 site->icon_data.pixbuf.pixbuf = pixbuf;
1555 * gtk_drag_source_set_icon_stock:
1556 * @widget: a #GtkWidget
1557 * @stock_id: the ID of the stock icon to use
1559 * Sets the icon that will be used for drags from a particular source
1563 gtk_drag_source_set_icon_stock (GtkWidget *widget,
1564 const gchar *stock_id)
1566 GtkDragSourceSite *site;
1568 g_return_if_fail (GTK_IS_WIDGET (widget));
1569 g_return_if_fail (stock_id != NULL);
1571 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1572 g_return_if_fail (site != NULL);
1574 gtk_drag_source_unset_icon (site);
1576 site->icon_type = GTK_IMAGE_STOCK;
1577 site->icon_data.stock.stock_id = g_strdup (stock_id);
1581 * gtk_drag_source_set_icon_name:
1582 * @widget: a #GtkWidget
1583 * @icon_name: name of icon to use
1585 * Sets the icon that will be used for drags from a particular source
1586 * to a themed icon. See the docs for #GtkIconTheme for more details.
1591 gtk_drag_source_set_icon_name (GtkWidget *widget,
1592 const gchar *icon_name)
1594 GtkDragSourceSite *site;
1596 g_return_if_fail (GTK_IS_WIDGET (widget));
1597 g_return_if_fail (icon_name != NULL);
1599 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1600 g_return_if_fail (site != NULL);
1602 gtk_drag_source_unset_icon (site);
1604 site->icon_type = GTK_IMAGE_ICON_NAME;
1605 site->icon_data.name.icon_name = g_strdup (icon_name);
1610 * gtk_drag_set_icon_widget:
1611 * @context: the context for a drag. (This must be called
1612 with a context for the source side of a drag)
1613 * @widget: a toplevel window to use as an icon.
1614 * @hot_x: the X offset within @widget of the hotspot.
1615 * @hot_y: the Y offset within @widget of the hotspot.
1617 * Changes the icon for a widget to a given widget. GTK+
1618 * will not destroy the icon, so if you don't want
1619 * it to persist, you should connect to the "drag-end"
1620 * signal and destroy it yourself.
1623 gtk_drag_set_icon_widget (GdkDragContext *context,
1628 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1629 g_return_if_fail (context->is_source);
1630 g_return_if_fail (GTK_IS_WIDGET (widget));
1632 g_warning ("gtk_drag_set_icon_widget is not supported on Mac OS X");
1636 set_icon_stock_pixbuf (GdkDragContext *context,
1637 const gchar *stock_id,
1642 GtkDragSourceInfo *info;
1644 info = gtk_drag_get_source_info (context, FALSE);
1648 pixbuf = gtk_widget_render_icon (info->widget, stock_id,
1649 GTK_ICON_SIZE_DND, NULL);
1653 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
1658 g_object_ref (pixbuf);
1660 if (info->icon_pixbuf)
1661 g_object_unref (info->icon_pixbuf);
1662 info->icon_pixbuf = pixbuf;
1663 info->hot_x = hot_x;
1664 info->hot_y = hot_y;
1668 * gtk_drag_set_icon_pixbuf:
1669 * @context: the context for a drag. (This must be called
1670 * with a context for the source side of a drag)
1671 * @pixbuf: the #GdkPixbuf to use as the drag icon.
1672 * @hot_x: the X offset within @widget of the hotspot.
1673 * @hot_y: the Y offset within @widget of the hotspot.
1675 * Sets @pixbuf as the icon for a given drag.
1678 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
1683 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1684 g_return_if_fail (context->is_source);
1685 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1687 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1691 * gtk_drag_set_icon_stock:
1692 * @context: the context for a drag. (This must be called
1693 * with a context for the source side of a drag)
1694 * @stock_id: the ID of the stock icon to use for the drag.
1695 * @hot_x: the X offset within the icon of the hotspot.
1696 * @hot_y: the Y offset within the icon of the hotspot.
1698 * Sets the icon for a given drag from a stock ID.
1701 gtk_drag_set_icon_stock (GdkDragContext *context,
1702 const gchar *stock_id,
1707 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1708 g_return_if_fail (context->is_source);
1709 g_return_if_fail (stock_id != NULL);
1711 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
1715 * gtk_drag_set_icon_pixmap:
1716 * @context: the context for a drag. (This must be called
1717 * with a context for the source side of a drag)
1718 * @colormap: the colormap of the icon
1719 * @pixmap: the image data for the icon
1720 * @mask: the transparency mask for the icon
1721 * @hot_x: the X offset within @pixmap of the hotspot.
1722 * @hot_y: the Y offset within @pixmap of the hotspot.
1724 * Sets @pixmap as the icon for a given drag. GTK+ retains
1725 * references for the arguments, and will release them when
1726 * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
1727 * will be more convenient to use.
1730 gtk_drag_set_icon_pixmap (GdkDragContext *context,
1731 GdkColormap *colormap,
1739 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1740 g_return_if_fail (context->is_source);
1741 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1742 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
1744 pixbuf = gdk_pixbuf_get_from_drawable (NULL, pixmap, colormap,
1749 gtk_drag_set_icon_pixbuf (context, pixbuf, hot_x, hot_y);
1750 g_object_unref (pixbuf);
1754 * gtk_drag_set_icon_name:
1755 * @context: the context for a drag. (This must be called
1756 * with a context for the source side of a drag)
1757 * @icon_name: name of icon to use
1758 * @hot_x: the X offset of the hotspot within the icon
1759 * @hot_y: the Y offset of the hotspot within the icon
1761 * Sets the icon for a given drag from a named themed icon. See
1762 * the docs for #GtkIconTheme for more details. Note that the
1763 * size of the icon depends on the icon theme (the icon is
1764 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
1765 * @hot_x and @hot_y have to be used with care.
1770 gtk_drag_set_icon_name (GdkDragContext *context,
1771 const gchar *icon_name,
1776 GtkSettings *settings;
1777 GtkIconTheme *icon_theme;
1779 gint width, height, icon_size;
1781 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1782 g_return_if_fail (context->is_source);
1783 g_return_if_fail (icon_name != NULL);
1785 screen = gdk_drawable_get_screen (context->source_window);
1786 g_return_if_fail (screen != NULL);
1788 settings = gtk_settings_get_for_screen (screen);
1789 if (gtk_icon_size_lookup_for_settings (settings,
1792 icon_size = MAX (width, height);
1794 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
1796 icon_theme = gtk_icon_theme_get_for_screen (screen);
1798 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
1799 icon_size, 0, NULL);
1801 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1803 g_warning ("Cannot load drag icon from icon name %s", icon_name);
1807 * gtk_drag_set_icon_default:
1808 * @context: the context for a drag. (This must be called
1809 with a context for the source side of a drag)
1811 * Sets the icon for a particular drag to the default
1815 gtk_drag_set_icon_default (GdkDragContext *context)
1817 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1818 g_return_if_fail (context->is_source);
1820 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
1824 gtk_drag_set_default_icon (GdkColormap *colormap,
1830 g_warning ("gtk_drag_set_default_icon is not supported on Mac OS X.");
1834 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
1836 if (info->icon_pixbuf)
1837 g_object_unref (info->icon_pixbuf);
1839 g_signal_emit_by_name (info->widget, "drag-end",
1842 if (info->source_widget)
1843 g_object_unref (info->source_widget);
1846 g_object_unref (info->widget);
1848 gtk_target_list_unref (info->target_list);
1850 gtk_drag_clear_source_info (info->context);
1851 g_object_unref (info->context);
1857 drag_drop_finished_idle_cb (gpointer data)
1859 gtk_drag_source_info_destroy (data);
1864 gtk_drag_drop_finished (GtkDragSourceInfo *info)
1866 if (info->success && info->delete)
1867 g_signal_emit_by_name (info->source_widget, "drag-data-delete",
1870 /* Workaround for the fact that the NS API blocks until the drag is
1871 * over. This way the context is still valid when returning from
1872 * drag_begin, even if it will still be quite useless. See bug #501588.
1874 g_idle_add (drag_drop_finished_idle_cb, info);
1877 /*************************************************************
1878 * _gtk_drag_source_handle_event:
1879 * Called from widget event handling code on Drag events
1883 * toplevel: Toplevel widget that received the event
1886 *************************************************************/
1889 _gtk_drag_source_handle_event (GtkWidget *widget,
1892 GtkDragSourceInfo *info;
1893 GdkDragContext *context;
1895 g_return_if_fail (widget != NULL);
1896 g_return_if_fail (event != NULL);
1898 context = event->dnd.context;
1899 info = gtk_drag_get_source_info (context, FALSE);
1903 switch (event->type)
1905 case GDK_DROP_FINISHED:
1906 gtk_drag_drop_finished (info);
1909 g_assert_not_reached ();
1915 gtk_drag_check_threshold (GtkWidget *widget,
1921 gint drag_threshold;
1923 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1925 g_object_get (gtk_widget_get_settings (widget),
1926 "gtk-dnd-drag-threshold", &drag_threshold,
1929 return (ABS (current_x - start_x) > drag_threshold ||
1930 ABS (current_y - start_y) > drag_threshold);
1933 #define __GTK_DND_C__
1934 #include "gtkaliasdef.c"