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/.
35 #include "gtkiconfactory.h"
36 #include "gtkicontheme.h"
38 #include "gtkinvisible.h"
42 #include "gtkwindow.h"
44 #include "gtkquartz.h"
45 #include "gdk/quartz/gdkquartz.h"
47 typedef struct _GtkDragSourceSite GtkDragSourceSite;
48 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
49 typedef struct _GtkDragDestSite GtkDragDestSite;
50 typedef struct _GtkDragDestInfo GtkDragDestInfo;
51 typedef struct _GtkDragFindData GtkDragFindData;
53 static void gtk_drag_find_widget (GtkWidget *widget,
54 GtkDragFindData *data);
55 static void gtk_drag_dest_site_destroy (gpointer data);
56 static void gtk_drag_dest_leave (GtkWidget *widget,
57 GdkDragContext *context,
59 static GtkDragDestInfo *gtk_drag_get_dest_info (GdkDragContext *context,
61 static void gtk_drag_source_site_destroy (gpointer data);
63 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
66 extern GdkDragContext *gdk_quartz_drag_source_context (); /* gdk/quartz/gdkdnd-quartz.c */
68 struct _GtkDragSourceSite
70 GdkModifierType start_button_mask;
71 GtkTargetList *target_list; /* Targets for drag data */
72 GdkDragAction actions; /* Possible actions */
75 GtkImageType icon_type;
78 GtkImagePixbufData pixbuf;
79 GtkImageStockData stock;
80 GtkImageIconNameData name;
83 /* Stored button press information to detect drag beginning */
88 struct _GtkDragSourceInfo
90 GtkWidget *source_widget;
92 GtkTargetList *target_list; /* Targets for drag data */
93 GdkDragAction possible_actions; /* Actions allowed by source */
94 GdkDragContext *context; /* drag context */
95 NSEvent *nsevent; /* what started it */
96 gint hot_x, hot_y; /* Hot spot for drag */
97 GdkPixbuf *icon_pixbuf;
102 struct _GtkDragDestSite
104 GtkDestDefaults flags;
105 GtkTargetList *target_list;
106 GdkDragAction actions;
108 guint track_motion : 1;
111 struct _GtkDragDestInfo
113 GtkWidget *widget; /* Widget in which drag is in */
114 GdkDragContext *context; /* Drag context */
115 guint dropped : 1; /* Set after we receive a drop */
116 gint drop_x, drop_y; /* Position of drop */
119 struct _GtkDragFindData
123 GdkDragContext *context;
124 GtkDragDestInfo *info;
127 gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
128 gint x, gint y, guint32 time);
133 @interface GtkDragSourceOwner : NSObject {
134 GtkDragSourceInfo *info;
139 @implementation GtkDragSourceOwner
140 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
143 GtkSelectionData selection_data;
145 selection_data.selection = GDK_NONE;
146 selection_data.data = NULL;
147 selection_data.length = -1;
148 selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
150 if (gtk_target_list_find (info->target_list,
151 selection_data.target,
154 g_signal_emit_by_name (info->widget, "drag-data-get",
160 if (selection_data.length >= 0)
161 _gtk_quartz_set_selection_data_for_pasteboard (sender, &selection_data);
163 g_free (selection_data.data);
167 - (id)initWithInfo:(GtkDragSourceInfo *)anInfo
182 gtk_drag_get_data (GtkWidget *widget,
183 GdkDragContext *context,
187 id <NSDraggingInfo> dragging_info;
188 NSPasteboard *pasteboard;
189 GtkSelectionData *selection_data;
190 GtkDragDestInfo *info;
191 GtkDragDestSite *site;
193 dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
194 pasteboard = [dragging_info draggingPasteboard];
196 info = gtk_drag_get_dest_info (context, FALSE);
197 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
199 selection_data = _gtk_quartz_get_selection_data_from_pasteboard (pasteboard,
202 if (site && site->target_list)
206 if (gtk_target_list_find (site->target_list,
207 selection_data->target,
210 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
211 selection_data->length >= 0)
212 g_signal_emit_by_name (widget,
213 "drag-data-received",
214 context, info->drop_x, info->drop_y,
221 g_signal_emit_by_name (widget,
222 "drag-data-received",
223 context, info->drop_x, info->drop_y,
228 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
230 gtk_drag_finish (context,
231 (selection_data->length >= 0),
232 (context->action == GDK_ACTION_MOVE),
238 gtk_drag_finish (GdkDragContext *context,
243 GtkDragSourceInfo *info;
244 GdkDragContext* source_context = gdk_quartz_drag_source_context ();
248 info = gtk_drag_get_source_info (source_context, FALSE);
251 info->success = success;
258 gtk_drag_dest_info_destroy (gpointer data)
260 GtkDragDestInfo *info = data;
265 static GtkDragDestInfo *
266 gtk_drag_get_dest_info (GdkDragContext *context,
269 GtkDragDestInfo *info;
270 static GQuark info_quark = 0;
272 info_quark = g_quark_from_static_string ("gtk-dest-info");
274 info = g_object_get_qdata (G_OBJECT (context), info_quark);
277 info = g_new (GtkDragDestInfo, 1);
279 info->context = context;
280 info->dropped = FALSE;
281 g_object_set_qdata_full (G_OBJECT (context), info_quark,
282 info, gtk_drag_dest_info_destroy);
288 static GQuark dest_info_quark = 0;
290 static GtkDragSourceInfo *
291 gtk_drag_get_source_info (GdkDragContext *context,
294 GtkDragSourceInfo *info;
296 if (!dest_info_quark)
297 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
299 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
302 info = g_new0 (GtkDragSourceInfo, 1);
303 info->context = context;
304 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
311 gtk_drag_clear_source_info (GdkDragContext *context)
313 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
317 gtk_drag_get_source_widget (GdkDragContext *context)
319 GtkDragSourceInfo *info;
320 GdkDragContext* real_source_context = gdk_quartz_drag_source_context();
322 if (!real_source_context)
325 info = gtk_drag_get_source_info (real_source_context, FALSE);
329 return info->source_widget;
332 /*************************************************************
333 * gtk_drag_highlight_draw:
334 * Callback for expose_event for highlighted widgets.
340 *************************************************************/
343 gtk_drag_highlight_draw (GtkWidget *widget,
347 int width = gtk_widget_get_allocated_width (widget);
348 int height = gtk_widget_get_allocated_height (widget);
350 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
351 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
353 0, 0, width, height);
355 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
356 cairo_set_line_width (cr, 1.0);
359 width - 1, height - 1);
365 /*************************************************************
366 * gtk_drag_highlight:
367 * Highlight the given widget in the default manner.
371 *************************************************************/
374 gtk_drag_highlight (GtkWidget *widget)
376 g_return_if_fail (GTK_IS_WIDGET (widget));
378 g_signal_connect_after (widget, "draw",
379 G_CALLBACK (gtk_drag_highlight_draw),
382 gtk_widget_queue_draw (widget);
385 /*************************************************************
386 * gtk_drag_unhighlight:
387 * Refresh the given widget to remove the highlight.
391 *************************************************************/
394 gtk_drag_unhighlight (GtkWidget *widget)
396 g_return_if_fail (GTK_IS_WIDGET (widget));
398 g_signal_handlers_disconnect_by_func (widget,
399 gtk_drag_highlight_draw,
402 gtk_widget_queue_draw (widget);
406 get_toplevel_nswindow (GtkWidget *widget)
408 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
409 GdkWindow *window = gtk_widget_get_window (toplevel);
411 if (gtk_widget_is_toplevel (toplevel) && window)
412 return [gdk_quartz_window_get_nsview (window) window];
418 register_types (GtkWidget *widget, GtkDragDestSite *site)
420 if (site->target_list)
422 NSWindow *nswindow = get_toplevel_nswindow (widget);
424 NSAutoreleasePool *pool;
429 pool = [[NSAutoreleasePool alloc] init];
430 types = _gtk_quartz_target_list_to_pasteboard_types (site->target_list);
432 [nswindow registerForDraggedTypes:[types allObjects]];
440 gtk_drag_dest_realized (GtkWidget *widget,
443 GtkDragDestSite *site = user_data;
445 register_types (widget, site);
449 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
450 GtkWidget *previous_toplevel,
453 GtkDragDestSite *site = user_data;
455 register_types (widget, site);
459 gtk_drag_dest_site_destroy (gpointer data)
461 GtkDragDestSite *site = data;
463 if (site->target_list)
464 gtk_target_list_unref (site->target_list);
470 gtk_drag_dest_set (GtkWidget *widget,
471 GtkDestDefaults flags,
472 const GtkTargetEntry *targets,
474 GdkDragAction actions)
476 GtkDragDestSite *old_site, *site;
478 g_return_if_fail (GTK_IS_WIDGET (widget));
480 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
482 site = g_new (GtkDragDestSite, 1);
484 site->have_drag = FALSE;
486 site->target_list = gtk_target_list_new (targets, n_targets);
488 site->target_list = NULL;
489 site->actions = actions;
492 site->track_motion = old_site->track_motion;
494 site->track_motion = FALSE;
496 gtk_drag_dest_unset (widget);
498 if (gtk_widget_get_realized (widget))
499 gtk_drag_dest_realized (widget, site);
501 g_signal_connect (widget, "realize",
502 G_CALLBACK (gtk_drag_dest_realized), site);
503 g_signal_connect (widget, "hierarchy-changed",
504 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
506 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
507 site, gtk_drag_dest_site_destroy);
511 gtk_drag_dest_set_proxy (GtkWidget *widget,
512 GdkWindow *proxy_window,
513 GdkDragProtocol protocol,
514 gboolean use_coordinates)
516 g_warning ("gtk_drag_dest_set_proxy is not supported on Mac OS X.");
520 gtk_drag_dest_unset (GtkWidget *widget)
522 GtkDragDestSite *old_site;
524 g_return_if_fail (GTK_IS_WIDGET (widget));
526 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
529 g_signal_handlers_disconnect_by_func (widget,
530 gtk_drag_dest_realized,
532 g_signal_handlers_disconnect_by_func (widget,
533 gtk_drag_dest_hierarchy_changed,
537 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
541 gtk_drag_dest_get_target_list (GtkWidget *widget)
543 GtkDragDestSite *site;
545 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
547 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
549 return site ? site->target_list : NULL;
553 gtk_drag_dest_set_target_list (GtkWidget *widget,
554 GtkTargetList *target_list)
556 GtkDragDestSite *site;
558 g_return_if_fail (GTK_IS_WIDGET (widget));
560 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
564 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
565 "to make the widget into a drag destination");
570 gtk_target_list_ref (target_list);
572 if (site->target_list)
573 gtk_target_list_unref (site->target_list);
575 site->target_list = target_list;
577 register_types (widget, site);
581 gtk_drag_dest_add_text_targets (GtkWidget *widget)
583 GtkTargetList *target_list;
585 target_list = gtk_drag_dest_get_target_list (widget);
587 gtk_target_list_ref (target_list);
589 target_list = gtk_target_list_new (NULL, 0);
590 gtk_target_list_add_text_targets (target_list, 0);
591 gtk_drag_dest_set_target_list (widget, target_list);
592 gtk_target_list_unref (target_list);
596 gtk_drag_dest_add_image_targets (GtkWidget *widget)
598 GtkTargetList *target_list;
600 target_list = gtk_drag_dest_get_target_list (widget);
602 gtk_target_list_ref (target_list);
604 target_list = gtk_target_list_new (NULL, 0);
605 gtk_target_list_add_image_targets (target_list, 0, FALSE);
606 gtk_drag_dest_set_target_list (widget, target_list);
607 gtk_target_list_unref (target_list);
611 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
613 GtkTargetList *target_list;
615 target_list = gtk_drag_dest_get_target_list (widget);
617 gtk_target_list_ref (target_list);
619 target_list = gtk_target_list_new (NULL, 0);
620 gtk_target_list_add_uri_targets (target_list, 0);
621 gtk_drag_dest_set_target_list (widget, target_list);
622 gtk_target_list_unref (target_list);
626 prepend_and_ref_widget (GtkWidget *widget,
629 GSList **slist_p = data;
631 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
635 gtk_drag_find_widget (GtkWidget *widget,
636 GtkDragFindData *data)
638 GtkAllocation new_allocation;
639 gint allocation_to_window_x = 0;
640 gint allocation_to_window_y = 0;
644 if (data->found || !gtk_widget_get_mapped (widget) || !gtk_widget_get_sensitive (widget))
647 /* Note that in the following code, we only count the
648 * position as being inside a WINDOW widget if it is inside
649 * widget->window; points that are outside of widget->window
650 * but within the allocation are not counted. This is consistent
651 * with the way we highlight drag targets.
653 * data->x,y are relative to widget->parent->window (if
654 * widget is not a toplevel, widget->window otherwise).
655 * We compute the allocation of widget in the same coordinates,
656 * clipping to widget->window, and all intermediate
657 * windows. If data->x,y is inside that, then we translate
658 * our coordinates to be relative to widget->window and
661 gtk_widget_get_allocation (widget, &new_allocation);
663 if (gtk_widget_get_parent (widget))
666 GdkWindow *window = gtk_widget_get_window (widget);
667 GdkWindow *parent_window;
668 GtkAllocation allocation;
670 parent_window = gtk_widget_get_window (gtk_widget_get_parent (widget));
672 /* Compute the offset from allocation-relative to
673 * window-relative coordinates.
675 gtk_widget_get_allocation (widget, &allocation);
676 allocation_to_window_x = allocation.x;
677 allocation_to_window_y = allocation.y;
679 if (gtk_widget_get_has_window (widget))
681 /* The allocation is relative to the parent window for
682 * window widgets, not to widget->window.
684 gdk_window_get_position (window, &tx, &ty);
686 allocation_to_window_x -= tx;
687 allocation_to_window_y -= ty;
690 new_allocation.x = 0 + allocation_to_window_x;
691 new_allocation.y = 0 + allocation_to_window_y;
693 while (window && window != parent_window)
695 GdkRectangle window_rect = { 0, 0, 0, 0 };
697 window_rect.width = gdk_window_get_width (window);
698 window_rect.height = gdk_window_get_height (window);
700 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
702 gdk_window_get_position (window, &tx, &ty);
703 new_allocation.x += tx;
705 new_allocation.y += ty;
708 window = gdk_window_get_parent (window);
711 if (!window) /* Window and widget heirarchies didn't match. */
715 if (data->toplevel ||
716 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
717 (data->x < new_allocation.x + new_allocation.width) &&
718 (data->y < new_allocation.y + new_allocation.height)))
720 /* First, check if the drag is in a valid drop site in
721 * one of our children
723 if (GTK_IS_CONTAINER (widget))
725 GtkDragFindData new_data = *data;
726 GSList *children = NULL;
729 new_data.x -= x_offset;
730 new_data.y -= y_offset;
731 new_data.found = FALSE;
732 new_data.toplevel = FALSE;
734 /* need to reference children temporarily in case the
735 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
737 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
738 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
740 if (!new_data.found && gtk_widget_is_drawable (tmp_list->data))
741 gtk_drag_find_widget (tmp_list->data, &new_data);
742 g_object_unref (tmp_list->data);
744 g_slist_free (children);
746 data->found = new_data.found;
749 /* If not, and this widget is registered as a drop site, check to
750 * emit "drag-motion" to check if we are actually in
754 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
756 data->found = data->callback (widget,
758 data->x - x_offset - allocation_to_window_x,
759 data->y - y_offset - allocation_to_window_y,
761 /* If so, send a "drag-leave" to the last widget */
764 if (data->info->widget && data->info->widget != widget)
766 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
768 data->info->widget = widget;
775 gtk_drag_dest_leave (GtkWidget *widget,
776 GdkDragContext *context,
779 GtkDragDestSite *site;
781 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
782 g_return_if_fail (site != NULL);
784 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
785 gtk_drag_unhighlight (widget);
787 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
789 g_signal_emit_by_name (widget, "drag-leave", context, time);
791 site->have_drag = FALSE;
795 gtk_drag_dest_motion (GtkWidget *widget,
796 GdkDragContext *context,
801 GtkDragDestSite *site;
802 GdkDragAction action = 0;
805 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
806 g_return_val_if_fail (site != NULL, FALSE);
808 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
810 if (context->suggested_action & site->actions)
811 action = context->suggested_action;
813 if (action && gtk_drag_dest_find_target (widget, context, NULL))
815 if (!site->have_drag)
817 site->have_drag = TRUE;
818 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
819 gtk_drag_highlight (widget);
822 gdk_drag_status (context, action, time);
826 gdk_drag_status (context, 0, time);
827 if (!site->track_motion)
832 g_signal_emit_by_name (widget, "drag-motion",
833 context, x, y, time, &retval);
835 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
839 gtk_drag_dest_drop (GtkWidget *widget,
840 GdkDragContext *context,
845 GtkDragDestSite *site;
846 GtkDragDestInfo *info;
849 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
850 g_return_val_if_fail (site != NULL, FALSE);
852 info = gtk_drag_get_dest_info (context, FALSE);
853 g_return_val_if_fail (info != NULL, FALSE);
858 if (site->flags & GTK_DEST_DEFAULT_DROP)
860 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
862 if (target == GDK_NONE)
864 gtk_drag_finish (context, FALSE, FALSE, time);
868 gtk_drag_get_data (widget, context, target, time);
871 g_signal_emit_by_name (widget, "drag-drop",
872 context, x, y, time, &retval);
874 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
878 gtk_drag_dest_set_track_motion (GtkWidget *widget,
879 gboolean track_motion)
881 GtkDragDestSite *site;
883 g_return_if_fail (GTK_IS_WIDGET (widget));
885 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
887 g_return_if_fail (site != NULL);
889 site->track_motion = track_motion != FALSE;
893 gtk_drag_dest_get_track_motion (GtkWidget *widget)
895 GtkDragDestSite *site;
897 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
899 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
902 return site->track_motion;
908 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
911 GtkDragDestInfo *info;
912 GdkDragContext *context;
914 g_return_if_fail (toplevel != NULL);
915 g_return_if_fail (event != NULL);
917 context = event->dnd.context;
919 info = gtk_drag_get_dest_info (context, TRUE);
921 /* Find the widget for the event */
930 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
935 case GDK_DRAG_MOTION:
938 GtkDragFindData data;
941 if (event->type == GDK_DROP_START)
943 info->dropped = TRUE;
944 /* We send a leave here so that the widget unhighlights
949 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
954 gdk_window_get_position (gtk_widget_get_window (toplevel), &tx, &ty);
956 data.x = event->dnd.x_root - tx;
957 data.y = event->dnd.y_root - ty;
958 data.context = context;
961 data.toplevel = TRUE;
962 data.callback = (event->type == GDK_DRAG_MOTION) ?
963 gtk_drag_dest_motion : gtk_drag_dest_drop;
964 data.time = event->dnd.time;
966 gtk_drag_find_widget (toplevel, &data);
968 if (info->widget && !data.found)
970 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
976 if (event->type == GDK_DRAG_MOTION)
979 gdk_drag_status (context, 0, event->dnd.time);
984 g_assert_not_reached ();
991 gtk_drag_dest_find_target (GtkWidget *widget,
992 GdkDragContext *context,
993 GtkTargetList *target_list)
995 id <NSDraggingInfo> dragging_info;
996 NSPasteboard *pasteboard;
997 GtkWidget *source_widget;
999 GList *tmp_source = NULL;
1000 GList *source_targets;
1002 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1003 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1004 g_return_val_if_fail (!context->is_source, GDK_NONE);
1006 dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
1007 pasteboard = [dragging_info draggingPasteboard];
1009 source_widget = gtk_drag_get_source_widget (context);
1011 if (target_list == NULL)
1012 target_list = gtk_drag_dest_get_target_list (widget);
1014 if (target_list == NULL)
1017 source_targets = _gtk_quartz_pasteboard_types_to_atom_list ([pasteboard types]);
1018 tmp_target = target_list->list;
1021 GtkTargetPair *pair = tmp_target->data;
1022 tmp_source = source_targets;
1025 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1027 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1028 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
1030 g_list_free (source_targets);
1031 return pair->target;
1036 tmp_source = tmp_source->next;
1038 tmp_target = tmp_target->next;
1041 g_list_free (source_targets);
1046 gtk_drag_begin_idle (gpointer arg)
1048 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1049 GdkDragContext* context = (GdkDragContext*) arg;
1050 GtkDragSourceInfo* info = gtk_drag_get_source_info (context, FALSE);
1052 NSPasteboard *pasteboard;
1053 GtkDragSourceOwner *owner;
1056 NSImage *drag_image;
1058 g_assert (info != NULL);
1060 pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
1061 owner = [[GtkDragSourceOwner alloc] initWithInfo:info];
1063 types = _gtk_quartz_target_list_to_pasteboard_types (info->target_list);
1065 [pasteboard declareTypes:[types allObjects] owner:owner];
1070 if ((nswindow = get_toplevel_nswindow (info->source_widget)) == NULL)
1073 /* Ref the context. It's unreffed when the drag has been aborted */
1074 g_object_ref (info->context);
1076 /* FIXME: If the event isn't a mouse event, use the global cursor position instead */
1077 point = [info->nsevent locationInWindow];
1079 drag_image = _gtk_quartz_create_image_from_pixbuf (info->icon_pixbuf);
1081 point.x -= info->hot_x;
1082 point.y -= info->hot_y;
1084 [nswindow dragImage:drag_image
1086 offset:NSMakeSize(0, 0)
1088 pasteboard:pasteboard
1092 [info->nsevent release];
1093 [drag_image release];
1100 static GdkDragContext *
1101 gtk_drag_begin_internal (GtkWidget *widget,
1102 GtkDragSourceSite *site,
1103 GtkTargetList *target_list,
1104 GdkDragAction actions,
1108 GtkDragSourceInfo *info;
1109 GdkDragContext *context;
1112 context = gdk_drag_begin (NULL, NULL);
1113 context->is_source = TRUE;
1115 info = gtk_drag_get_source_info (context, TRUE);
1117 info->source_widget = g_object_ref (widget);
1118 info->widget = g_object_ref (widget);
1119 info->target_list = target_list;
1120 gtk_target_list_ref (target_list);
1122 info->possible_actions = actions;
1124 g_signal_emit_by_name (widget, "drag-begin", info->context);
1126 /* Ensure that we have an icon before we start the drag; the
1127 * application may have set one in ::drag_begin, or it may
1130 if (!info->icon_pixbuf)
1132 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
1133 gtk_drag_set_icon_default (context);
1135 switch (site->icon_type)
1137 case GTK_IMAGE_PIXBUF:
1138 gtk_drag_set_icon_pixbuf (context,
1139 site->icon_data.pixbuf.pixbuf,
1142 case GTK_IMAGE_STOCK:
1143 gtk_drag_set_icon_stock (context,
1144 site->icon_data.stock.stock_id,
1147 case GTK_IMAGE_ICON_NAME:
1148 gtk_drag_set_icon_name (context,
1149 site->icon_data.name.icon_name,
1152 case GTK_IMAGE_EMPTY:
1154 g_assert_not_reached();
1159 nswindow = get_toplevel_nswindow (widget);
1160 info->nsevent = [nswindow currentEvent];
1161 [info->nsevent retain];
1163 /* drag will begin in an idle handler to avoid nested run loops */
1165 g_idle_add_full (G_PRIORITY_HIGH_IDLE, gtk_drag_begin_idle, context, NULL);
1167 gdk_pointer_ungrab (0);
1173 gtk_drag_begin (GtkWidget *widget,
1174 GtkTargetList *targets,
1175 GdkDragAction actions,
1179 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1180 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
1181 g_return_val_if_fail (targets != NULL, NULL);
1183 return gtk_drag_begin_internal (widget, NULL, targets,
1184 actions, button, event);
1189 gtk_drag_source_event_cb (GtkWidget *widget,
1193 GtkDragSourceSite *site;
1194 gboolean retval = FALSE;
1195 site = (GtkDragSourceSite *)data;
1197 switch (event->type)
1199 case GDK_BUTTON_PRESS:
1200 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1202 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
1203 site->x = event->button.x;
1204 site->y = event->button.y;
1208 case GDK_BUTTON_RELEASE:
1209 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1210 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
1213 case GDK_MOTION_NOTIFY:
1214 if (site->state & event->motion.state & site->start_button_mask)
1216 /* FIXME: This is really broken and can leave us
1222 if (site->state & event->motion.state &
1223 GDK_BUTTON1_MASK << (i - 1))
1227 if (gtk_drag_check_threshold (widget, site->x, site->y,
1228 event->motion.x, event->motion.y))
1231 gtk_drag_begin_internal (widget, site, site->target_list,
1240 default: /* hit for 2/3BUTTON_PRESS */
1248 gtk_drag_source_set (GtkWidget *widget,
1249 GdkModifierType start_button_mask,
1250 const GtkTargetEntry *targets,
1252 GdkDragAction actions)
1254 GtkDragSourceSite *site;
1256 g_return_if_fail (GTK_IS_WIDGET (widget));
1258 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1260 gtk_widget_add_events (widget,
1261 gtk_widget_get_events (widget) |
1262 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1263 GDK_BUTTON_MOTION_MASK);
1267 if (site->target_list)
1268 gtk_target_list_unref (site->target_list);
1272 site = g_new0 (GtkDragSourceSite, 1);
1274 site->icon_type = GTK_IMAGE_EMPTY;
1276 g_signal_connect (widget, "button-press-event",
1277 G_CALLBACK (gtk_drag_source_event_cb),
1279 g_signal_connect (widget, "button-release-event",
1280 G_CALLBACK (gtk_drag_source_event_cb),
1282 g_signal_connect (widget, "motion-notify-event",
1283 G_CALLBACK (gtk_drag_source_event_cb),
1286 g_object_set_data_full (G_OBJECT (widget),
1287 I_("gtk-site-data"),
1288 site, gtk_drag_source_site_destroy);
1291 site->start_button_mask = start_button_mask;
1293 site->target_list = gtk_target_list_new (targets, n_targets);
1295 site->actions = actions;
1298 /*************************************************************
1299 * gtk_drag_source_unset
1300 * Unregister this widget as a drag source.
1304 *************************************************************/
1307 gtk_drag_source_unset (GtkWidget *widget)
1309 GtkDragSourceSite *site;
1311 g_return_if_fail (GTK_IS_WIDGET (widget));
1313 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1317 g_signal_handlers_disconnect_by_func (widget,
1318 gtk_drag_source_event_cb,
1320 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
1325 gtk_drag_source_get_target_list (GtkWidget *widget)
1327 GtkDragSourceSite *site;
1329 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1331 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1333 return site ? site->target_list : NULL;
1338 gtk_drag_source_set_target_list (GtkWidget *widget,
1339 GtkTargetList *target_list)
1341 GtkDragSourceSite *site;
1343 g_return_if_fail (GTK_IS_WIDGET (widget));
1345 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1348 g_warning ("gtk_drag_source_set_target_list() requires the widget "
1349 "to already be a drag source.");
1354 gtk_target_list_ref (target_list);
1356 if (site->target_list)
1357 gtk_target_list_unref (site->target_list);
1359 site->target_list = target_list;
1363 * gtk_drag_source_add_text_targets:
1364 * @widget: a #GtkWidget that's is a drag source
1366 * Add the text targets supported by #GtkSelection to
1367 * the target list of the drag source. The targets
1368 * are added with @info = 0. If you need another value,
1369 * use gtk_target_list_add_text_targets() and
1370 * gtk_drag_source_set_target_list().
1375 gtk_drag_source_add_text_targets (GtkWidget *widget)
1377 GtkTargetList *target_list;
1379 target_list = gtk_drag_source_get_target_list (widget);
1381 gtk_target_list_ref (target_list);
1383 target_list = gtk_target_list_new (NULL, 0);
1384 gtk_target_list_add_text_targets (target_list, 0);
1385 gtk_drag_source_set_target_list (widget, target_list);
1386 gtk_target_list_unref (target_list);
1390 gtk_drag_source_add_image_targets (GtkWidget *widget)
1392 GtkTargetList *target_list;
1394 target_list = gtk_drag_source_get_target_list (widget);
1396 gtk_target_list_ref (target_list);
1398 target_list = gtk_target_list_new (NULL, 0);
1399 gtk_target_list_add_image_targets (target_list, 0, TRUE);
1400 gtk_drag_source_set_target_list (widget, target_list);
1401 gtk_target_list_unref (target_list);
1405 gtk_drag_source_add_uri_targets (GtkWidget *widget)
1407 GtkTargetList *target_list;
1409 target_list = gtk_drag_source_get_target_list (widget);
1411 gtk_target_list_ref (target_list);
1413 target_list = gtk_target_list_new (NULL, 0);
1414 gtk_target_list_add_uri_targets (target_list, 0);
1415 gtk_drag_source_set_target_list (widget, target_list);
1416 gtk_target_list_unref (target_list);
1420 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
1422 switch (site->icon_type)
1424 case GTK_IMAGE_EMPTY:
1426 case GTK_IMAGE_PIXBUF:
1427 g_object_unref (site->icon_data.pixbuf.pixbuf);
1429 case GTK_IMAGE_STOCK:
1430 g_free (site->icon_data.stock.stock_id);
1432 case GTK_IMAGE_ICON_NAME:
1433 g_free (site->icon_data.name.icon_name);
1436 g_assert_not_reached();
1439 site->icon_type = GTK_IMAGE_EMPTY;
1443 gtk_drag_source_site_destroy (gpointer data)
1445 GtkDragSourceSite *site = data;
1447 if (site->target_list)
1448 gtk_target_list_unref (site->target_list);
1450 gtk_drag_source_unset_icon (site);
1455 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
1458 GtkDragSourceSite *site;
1460 g_return_if_fail (GTK_IS_WIDGET (widget));
1461 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1463 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1464 g_return_if_fail (site != NULL);
1465 g_object_ref (pixbuf);
1467 gtk_drag_source_unset_icon (site);
1469 site->icon_type = GTK_IMAGE_PIXBUF;
1470 site->icon_data.pixbuf.pixbuf = pixbuf;
1474 * gtk_drag_source_set_icon_stock:
1475 * @widget: a #GtkWidget
1476 * @stock_id: the ID of the stock icon to use
1478 * Sets the icon that will be used for drags from a particular source
1482 gtk_drag_source_set_icon_stock (GtkWidget *widget,
1483 const gchar *stock_id)
1485 GtkDragSourceSite *site;
1487 g_return_if_fail (GTK_IS_WIDGET (widget));
1488 g_return_if_fail (stock_id != NULL);
1490 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1491 g_return_if_fail (site != NULL);
1493 gtk_drag_source_unset_icon (site);
1495 site->icon_type = GTK_IMAGE_STOCK;
1496 site->icon_data.stock.stock_id = g_strdup (stock_id);
1500 * gtk_drag_source_set_icon_name:
1501 * @widget: a #GtkWidget
1502 * @icon_name: name of icon to use
1504 * Sets the icon that will be used for drags from a particular source
1505 * to a themed icon. See the docs for #GtkIconTheme for more details.
1510 gtk_drag_source_set_icon_name (GtkWidget *widget,
1511 const gchar *icon_name)
1513 GtkDragSourceSite *site;
1515 g_return_if_fail (GTK_IS_WIDGET (widget));
1516 g_return_if_fail (icon_name != NULL);
1518 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1519 g_return_if_fail (site != NULL);
1521 gtk_drag_source_unset_icon (site);
1523 site->icon_type = GTK_IMAGE_ICON_NAME;
1524 site->icon_data.name.icon_name = g_strdup (icon_name);
1529 * gtk_drag_set_icon_widget:
1530 * @context: the context for a drag. (This must be called
1531 with a context for the source side of a drag)
1532 * @widget: a toplevel window to use as an icon.
1533 * @hot_x: the X offset within @widget of the hotspot.
1534 * @hot_y: the Y offset within @widget of the hotspot.
1536 * Changes the icon for a widget to a given widget. GTK+
1537 * will not destroy the icon, so if you don't want
1538 * it to persist, you should connect to the "drag-end"
1539 * signal and destroy it yourself.
1542 gtk_drag_set_icon_widget (GdkDragContext *context,
1547 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1548 g_return_if_fail (context->is_source);
1549 g_return_if_fail (GTK_IS_WIDGET (widget));
1551 g_warning ("gtk_drag_set_icon_widget is not supported on Mac OS X");
1555 set_icon_stock_pixbuf (GdkDragContext *context,
1556 const gchar *stock_id,
1561 GtkDragSourceInfo *info;
1563 info = gtk_drag_get_source_info (context, FALSE);
1567 pixbuf = gtk_widget_render_icon_pixbuf (info->widget, stock_id,
1568 GTK_ICON_SIZE_DND, NULL);
1572 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
1577 g_object_ref (pixbuf);
1579 if (info->icon_pixbuf)
1580 g_object_unref (info->icon_pixbuf);
1581 info->icon_pixbuf = pixbuf;
1582 info->hot_x = hot_x;
1583 info->hot_y = hot_y;
1587 * gtk_drag_set_icon_pixbuf:
1588 * @context: the context for a drag. (This must be called
1589 * with a context for the source side of a drag)
1590 * @pixbuf: the #GdkPixbuf to use as the drag icon.
1591 * @hot_x: the X offset within @widget of the hotspot.
1592 * @hot_y: the Y offset within @widget of the hotspot.
1594 * Sets @pixbuf as the icon for a given drag.
1597 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
1602 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1603 g_return_if_fail (context->is_source);
1604 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1606 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1610 * gtk_drag_set_icon_stock:
1611 * @context: the context for a drag. (This must be called
1612 * with a context for the source side of a drag)
1613 * @stock_id: the ID of the stock icon to use for the drag.
1614 * @hot_x: the X offset within the icon of the hotspot.
1615 * @hot_y: the Y offset within the icon of the hotspot.
1617 * Sets the icon for a given drag from a stock ID.
1620 gtk_drag_set_icon_stock (GdkDragContext *context,
1621 const gchar *stock_id,
1626 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1627 g_return_if_fail (context->is_source);
1628 g_return_if_fail (stock_id != NULL);
1630 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
1634 /* XXX: This function is in gdk, too. Should it be in Cairo? */
1636 _gtk_cairo_surface_extents (cairo_surface_t *surface,
1637 GdkRectangle *extents)
1639 double x1, x2, y1, y2;
1642 g_return_val_if_fail (surface != NULL, FALSE);
1643 g_return_val_if_fail (extents != NULL, FALSE);
1645 cr = cairo_create (surface);
1646 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
1655 if (x1 < G_MININT || x1 > G_MAXINT ||
1656 y1 < G_MININT || y1 > G_MAXINT ||
1657 x2 > G_MAXINT || y2 > G_MAXINT)
1659 extents->x = extents->y = extents->width = extents->height = 0;
1665 extents->width = x2;
1666 extents->height = y2;
1672 * gtk_drag_set_icon_surface:
1673 * @context: the context for a drag. (This must be called
1674 * with a context for the source side of a drag)
1675 * @surface: the surface to use as icon
1677 * Sets @surface as the icon for a given drag. GTK+ retains
1678 * references for the arguments, and will release them when
1679 * they are no longer needed.
1681 * To position the surface relative to the mouse, use
1682 * cairo_surface_set_device_offset() on @surface. The mouse
1683 * cursor will be positioned at the (0,0) coordinate of the
1687 gtk_drag_set_icon_surface (GdkDragContext *context,
1688 cairo_surface_t *surface)
1691 GdkRectangle extents;
1692 double x_offset, y_offset;
1694 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1695 g_return_if_fail (context->is_source);
1696 g_return_if_fail (surface != NULL);
1698 _gtk_cairo_surface_extents (surface, &extents);
1699 cairo_surface_get_device_offset (surface, &x_offset, &y_offset);
1701 pixbuf = gdk_pixbuf_get_from_surface (surface,
1702 extents.x, extents.y,
1703 extents.width, extents.height);
1704 gtk_drag_set_icon_pixbuf (context, pixbuf, -x_offset, -y_offset);
1705 g_object_unref (pixbuf);
1709 * gtk_drag_set_icon_name:
1710 * @context: the context for a drag. (This must be called
1711 * with a context for the source side of a drag)
1712 * @icon_name: name of icon to use
1713 * @hot_x: the X offset of the hotspot within the icon
1714 * @hot_y: the Y offset of the hotspot within the icon
1716 * Sets the icon for a given drag from a named themed icon. See
1717 * the docs for #GtkIconTheme for more details. Note that the
1718 * size of the icon depends on the icon theme (the icon is
1719 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
1720 * @hot_x and @hot_y have to be used with care.
1725 gtk_drag_set_icon_name (GdkDragContext *context,
1726 const gchar *icon_name,
1731 GtkSettings *settings;
1732 GtkIconTheme *icon_theme;
1734 gint width, height, icon_size;
1736 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1737 g_return_if_fail (context->is_source);
1738 g_return_if_fail (icon_name != NULL);
1740 screen = gdk_window_get_screen (context->source_window);
1741 g_return_if_fail (screen != NULL);
1743 settings = gtk_settings_get_for_screen (screen);
1744 if (gtk_icon_size_lookup_for_settings (settings,
1747 icon_size = MAX (width, height);
1749 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
1751 icon_theme = gtk_icon_theme_get_for_screen (screen);
1753 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
1754 icon_size, 0, NULL);
1756 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1758 g_warning ("Cannot load drag icon from icon name %s", icon_name);
1762 * gtk_drag_set_icon_default:
1763 * @context: the context for a drag. (This must be called
1764 with a context for the source side of a drag)
1766 * Sets the icon for a particular drag to the default
1770 gtk_drag_set_icon_default (GdkDragContext *context)
1772 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1773 g_return_if_fail (context->is_source);
1775 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
1779 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
1781 if (info->icon_pixbuf)
1782 g_object_unref (info->icon_pixbuf);
1784 g_signal_emit_by_name (info->widget, "drag-end",
1787 if (info->source_widget)
1788 g_object_unref (info->source_widget);
1791 g_object_unref (info->widget);
1793 gtk_target_list_unref (info->target_list);
1795 gtk_drag_clear_source_info (info->context);
1796 g_object_unref (info->context);
1802 drag_drop_finished_idle_cb (gpointer data)
1804 gtk_drag_source_info_destroy (data);
1809 gtk_drag_drop_finished (GtkDragSourceInfo *info)
1811 if (info->success && info->delete)
1812 g_signal_emit_by_name (info->source_widget, "drag-data-delete",
1815 /* Workaround for the fact that the NS API blocks until the drag is
1816 * over. This way the context is still valid when returning from
1817 * drag_begin, even if it will still be quite useless. See bug #501588.
1819 g_idle_add (drag_drop_finished_idle_cb, info);
1822 /*************************************************************
1823 * _gtk_drag_source_handle_event:
1824 * Called from widget event handling code on Drag events
1828 * toplevel: Toplevel widget that received the event
1831 *************************************************************/
1834 _gtk_drag_source_handle_event (GtkWidget *widget,
1837 GtkDragSourceInfo *info;
1838 GdkDragContext *context;
1840 g_return_if_fail (widget != NULL);
1841 g_return_if_fail (event != NULL);
1843 context = event->dnd.context;
1844 info = gtk_drag_get_source_info (context, FALSE);
1848 switch (event->type)
1850 case GDK_DROP_FINISHED:
1851 gtk_drag_drop_finished (info);
1854 g_assert_not_reached ();
1860 gtk_drag_check_threshold (GtkWidget *widget,
1866 gint drag_threshold;
1868 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1870 g_object_get (gtk_widget_get_settings (widget),
1871 "gtk-dnd-drag-threshold", &drag_threshold,
1874 return (ABS (current_x - start_x) > drag_threshold ||
1875 ABS (current_y - start_y) > drag_threshold);