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"
46 #include "gtkselectionprivate.h"
48 typedef struct _GtkDragSourceSite GtkDragSourceSite;
49 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
50 typedef struct _GtkDragDestSite GtkDragDestSite;
51 typedef struct _GtkDragDestInfo GtkDragDestInfo;
52 typedef struct _GtkDragFindData GtkDragFindData;
54 static void gtk_drag_find_widget (GtkWidget *widget,
55 GtkDragFindData *data);
56 static void gtk_drag_dest_site_destroy (gpointer data);
57 static void gtk_drag_dest_leave (GtkWidget *widget,
58 GdkDragContext *context,
60 static GtkDragDestInfo *gtk_drag_get_dest_info (GdkDragContext *context,
62 static void gtk_drag_source_site_destroy (gpointer data);
64 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
67 extern GdkDragContext *gdk_quartz_drag_source_context (); /* gdk/quartz/gdkdnd-quartz.c */
69 struct _GtkDragSourceSite
71 GdkModifierType start_button_mask;
72 GtkTargetList *target_list; /* Targets for drag data */
73 GdkDragAction actions; /* Possible actions */
76 GtkImageType icon_type;
79 GtkImagePixbufData pixbuf;
80 GtkImageStockData stock;
81 GtkImageIconNameData name;
84 /* Stored button press information to detect drag beginning */
89 struct _GtkDragSourceInfo
91 GtkWidget *source_widget;
93 GtkTargetList *target_list; /* Targets for drag data */
94 GdkDragAction possible_actions; /* Actions allowed by source */
95 GdkDragContext *context; /* drag context */
96 NSEvent *nsevent; /* what started it */
97 gint hot_x, hot_y; /* Hot spot for drag */
98 GdkPixbuf *icon_pixbuf;
103 struct _GtkDragDestSite
105 GtkDestDefaults flags;
106 GtkTargetList *target_list;
107 GdkDragAction actions;
109 guint track_motion : 1;
112 struct _GtkDragDestInfo
114 GtkWidget *widget; /* Widget in which drag is in */
115 GdkDragContext *context; /* Drag context */
116 guint dropped : 1; /* Set after we receive a drop */
117 gint drop_x, drop_y; /* Position of drop */
120 struct _GtkDragFindData
124 GdkDragContext *context;
125 GtkDragDestInfo *info;
128 gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
129 gint x, gint y, guint32 time);
134 @interface GtkDragSourceOwner : NSObject {
135 GtkDragSourceInfo *info;
140 @implementation GtkDragSourceOwner
141 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
144 GtkSelectionData selection_data;
146 selection_data.selection = GDK_NONE;
147 selection_data.data = NULL;
148 selection_data.length = -1;
149 selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
151 if (gtk_target_list_find (info->target_list,
152 selection_data.target,
155 g_signal_emit_by_name (info->widget, "drag-data-get",
161 if (selection_data.length >= 0)
162 _gtk_quartz_set_selection_data_for_pasteboard (sender, &selection_data);
164 g_free (selection_data.data);
168 - (id)initWithInfo:(GtkDragSourceInfo *)anInfo
183 gtk_drag_get_data (GtkWidget *widget,
184 GdkDragContext *context,
188 id <NSDraggingInfo> dragging_info;
189 NSPasteboard *pasteboard;
190 GtkSelectionData *selection_data;
191 GtkDragDestInfo *info;
192 GtkDragDestSite *site;
194 dragging_info = gdk_quartz_drag_context_get_dragging_info_libgtk_only (context);
195 pasteboard = [dragging_info draggingPasteboard];
197 info = gtk_drag_get_dest_info (context, FALSE);
198 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
200 selection_data = _gtk_quartz_get_selection_data_from_pasteboard (pasteboard,
203 if (site && site->target_list)
207 if (gtk_target_list_find (site->target_list,
208 selection_data->target,
211 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
212 selection_data->length >= 0)
213 g_signal_emit_by_name (widget,
214 "drag-data-received",
215 context, info->drop_x, info->drop_y,
222 g_signal_emit_by_name (widget,
223 "drag-data-received",
224 context, info->drop_x, info->drop_y,
229 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
231 gtk_drag_finish (context,
232 (selection_data->length >= 0),
233 (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
239 gtk_drag_finish (GdkDragContext *context,
244 GtkDragSourceInfo *info;
245 GdkDragContext* source_context = gdk_quartz_drag_source_context ();
249 info = gtk_drag_get_source_info (source_context, FALSE);
252 info->success = success;
259 gtk_drag_dest_info_destroy (gpointer data)
261 GtkDragDestInfo *info = data;
266 static GtkDragDestInfo *
267 gtk_drag_get_dest_info (GdkDragContext *context,
270 GtkDragDestInfo *info;
271 static GQuark info_quark = 0;
273 info_quark = g_quark_from_static_string ("gtk-dest-info");
275 info = g_object_get_qdata (G_OBJECT (context), info_quark);
278 info = g_new (GtkDragDestInfo, 1);
280 info->context = context;
281 info->dropped = FALSE;
282 g_object_set_qdata_full (G_OBJECT (context), info_quark,
283 info, gtk_drag_dest_info_destroy);
289 static GQuark dest_info_quark = 0;
291 static GtkDragSourceInfo *
292 gtk_drag_get_source_info (GdkDragContext *context,
295 GtkDragSourceInfo *info;
297 if (!dest_info_quark)
298 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
300 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
303 info = g_new0 (GtkDragSourceInfo, 1);
304 info->context = context;
305 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
312 gtk_drag_clear_source_info (GdkDragContext *context)
314 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
318 gtk_drag_get_source_widget (GdkDragContext *context)
320 GtkDragSourceInfo *info;
321 GdkDragContext* real_source_context = gdk_quartz_drag_source_context();
323 if (!real_source_context)
326 info = gtk_drag_get_source_info (real_source_context, FALSE);
330 return info->source_widget;
333 /*************************************************************
334 * gtk_drag_highlight_draw:
335 * Callback for expose_event for highlighted widgets.
341 *************************************************************/
344 gtk_drag_highlight_draw (GtkWidget *widget,
348 int width = gtk_widget_get_allocated_width (widget);
349 int height = gtk_widget_get_allocated_height (widget);
351 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
352 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
354 0, 0, width, height);
356 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
357 cairo_set_line_width (cr, 1.0);
360 width - 1, height - 1);
366 /*************************************************************
367 * gtk_drag_highlight:
368 * Highlight the given widget in the default manner.
372 *************************************************************/
375 gtk_drag_highlight (GtkWidget *widget)
377 g_return_if_fail (GTK_IS_WIDGET (widget));
379 g_signal_connect_after (widget, "draw",
380 G_CALLBACK (gtk_drag_highlight_draw),
383 gtk_widget_queue_draw (widget);
386 /*************************************************************
387 * gtk_drag_unhighlight:
388 * Refresh the given widget to remove the highlight.
392 *************************************************************/
395 gtk_drag_unhighlight (GtkWidget *widget)
397 g_return_if_fail (GTK_IS_WIDGET (widget));
399 g_signal_handlers_disconnect_by_func (widget,
400 gtk_drag_highlight_draw,
403 gtk_widget_queue_draw (widget);
407 get_toplevel_nswindow (GtkWidget *widget)
409 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
410 GdkWindow *window = gtk_widget_get_window (toplevel);
412 if (gtk_widget_is_toplevel (toplevel) && window)
413 return [gdk_quartz_window_get_nsview (window) window];
419 register_types (GtkWidget *widget, GtkDragDestSite *site)
421 if (site->target_list)
423 NSWindow *nswindow = get_toplevel_nswindow (widget);
425 NSAutoreleasePool *pool;
430 pool = [[NSAutoreleasePool alloc] init];
431 types = _gtk_quartz_target_list_to_pasteboard_types (site->target_list);
433 [nswindow registerForDraggedTypes:[types allObjects]];
441 gtk_drag_dest_realized (GtkWidget *widget,
444 GtkDragDestSite *site = user_data;
446 register_types (widget, site);
450 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
451 GtkWidget *previous_toplevel,
454 GtkDragDestSite *site = user_data;
456 register_types (widget, site);
460 gtk_drag_dest_site_destroy (gpointer data)
462 GtkDragDestSite *site = data;
464 if (site->target_list)
465 gtk_target_list_unref (site->target_list);
471 gtk_drag_dest_set (GtkWidget *widget,
472 GtkDestDefaults flags,
473 const GtkTargetEntry *targets,
475 GdkDragAction actions)
477 GtkDragDestSite *old_site, *site;
479 g_return_if_fail (GTK_IS_WIDGET (widget));
481 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
483 site = g_new (GtkDragDestSite, 1);
485 site->have_drag = FALSE;
487 site->target_list = gtk_target_list_new (targets, n_targets);
489 site->target_list = NULL;
490 site->actions = actions;
493 site->track_motion = old_site->track_motion;
495 site->track_motion = FALSE;
497 gtk_drag_dest_unset (widget);
499 if (gtk_widget_get_realized (widget))
500 gtk_drag_dest_realized (widget, site);
502 g_signal_connect (widget, "realize",
503 G_CALLBACK (gtk_drag_dest_realized), site);
504 g_signal_connect (widget, "hierarchy-changed",
505 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
507 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
508 site, gtk_drag_dest_site_destroy);
512 gtk_drag_dest_set_proxy (GtkWidget *widget,
513 GdkWindow *proxy_window,
514 GdkDragProtocol protocol,
515 gboolean use_coordinates)
517 g_warning ("gtk_drag_dest_set_proxy is not supported on Mac OS X.");
521 gtk_drag_dest_unset (GtkWidget *widget)
523 GtkDragDestSite *old_site;
525 g_return_if_fail (GTK_IS_WIDGET (widget));
527 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
530 g_signal_handlers_disconnect_by_func (widget,
531 gtk_drag_dest_realized,
533 g_signal_handlers_disconnect_by_func (widget,
534 gtk_drag_dest_hierarchy_changed,
538 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
542 gtk_drag_dest_get_target_list (GtkWidget *widget)
544 GtkDragDestSite *site;
546 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
548 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
550 return site ? site->target_list : NULL;
554 gtk_drag_dest_set_target_list (GtkWidget *widget,
555 GtkTargetList *target_list)
557 GtkDragDestSite *site;
559 g_return_if_fail (GTK_IS_WIDGET (widget));
561 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
565 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
566 "to make the widget into a drag destination");
571 gtk_target_list_ref (target_list);
573 if (site->target_list)
574 gtk_target_list_unref (site->target_list);
576 site->target_list = target_list;
578 register_types (widget, site);
582 gtk_drag_dest_add_text_targets (GtkWidget *widget)
584 GtkTargetList *target_list;
586 target_list = gtk_drag_dest_get_target_list (widget);
588 gtk_target_list_ref (target_list);
590 target_list = gtk_target_list_new (NULL, 0);
591 gtk_target_list_add_text_targets (target_list, 0);
592 gtk_drag_dest_set_target_list (widget, target_list);
593 gtk_target_list_unref (target_list);
597 gtk_drag_dest_add_image_targets (GtkWidget *widget)
599 GtkTargetList *target_list;
601 target_list = gtk_drag_dest_get_target_list (widget);
603 gtk_target_list_ref (target_list);
605 target_list = gtk_target_list_new (NULL, 0);
606 gtk_target_list_add_image_targets (target_list, 0, FALSE);
607 gtk_drag_dest_set_target_list (widget, target_list);
608 gtk_target_list_unref (target_list);
612 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
614 GtkTargetList *target_list;
616 target_list = gtk_drag_dest_get_target_list (widget);
618 gtk_target_list_ref (target_list);
620 target_list = gtk_target_list_new (NULL, 0);
621 gtk_target_list_add_uri_targets (target_list, 0);
622 gtk_drag_dest_set_target_list (widget, target_list);
623 gtk_target_list_unref (target_list);
627 prepend_and_ref_widget (GtkWidget *widget,
630 GSList **slist_p = data;
632 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
636 gtk_drag_find_widget (GtkWidget *widget,
637 GtkDragFindData *data)
639 GtkAllocation new_allocation;
640 gint allocation_to_window_x = 0;
641 gint allocation_to_window_y = 0;
645 if (data->found || !gtk_widget_get_mapped (widget) || !gtk_widget_get_sensitive (widget))
648 /* Note that in the following code, we only count the
649 * position as being inside a WINDOW widget if it is inside
650 * widget->window; points that are outside of widget->window
651 * but within the allocation are not counted. This is consistent
652 * with the way we highlight drag targets.
654 * data->x,y are relative to widget->parent->window (if
655 * widget is not a toplevel, widget->window otherwise).
656 * We compute the allocation of widget in the same coordinates,
657 * clipping to widget->window, and all intermediate
658 * windows. If data->x,y is inside that, then we translate
659 * our coordinates to be relative to widget->window and
662 gtk_widget_get_allocation (widget, &new_allocation);
664 if (gtk_widget_get_parent (widget))
667 GdkWindow *window = gtk_widget_get_window (widget);
668 GdkWindow *parent_window;
669 GtkAllocation allocation;
671 parent_window = gtk_widget_get_window (gtk_widget_get_parent (widget));
673 /* Compute the offset from allocation-relative to
674 * window-relative coordinates.
676 gtk_widget_get_allocation (widget, &allocation);
677 allocation_to_window_x = allocation.x;
678 allocation_to_window_y = allocation.y;
680 if (gtk_widget_get_has_window (widget))
682 /* The allocation is relative to the parent window for
683 * window widgets, not to widget->window.
685 gdk_window_get_position (window, &tx, &ty);
687 allocation_to_window_x -= tx;
688 allocation_to_window_y -= ty;
691 new_allocation.x = 0 + allocation_to_window_x;
692 new_allocation.y = 0 + allocation_to_window_y;
694 while (window && window != parent_window)
696 GdkRectangle window_rect = { 0, 0, 0, 0 };
698 window_rect.width = gdk_window_get_width (window);
699 window_rect.height = gdk_window_get_height (window);
701 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
703 gdk_window_get_position (window, &tx, &ty);
704 new_allocation.x += tx;
706 new_allocation.y += ty;
709 window = gdk_window_get_parent (window);
712 if (!window) /* Window and widget heirarchies didn't match. */
716 if (data->toplevel ||
717 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
718 (data->x < new_allocation.x + new_allocation.width) &&
719 (data->y < new_allocation.y + new_allocation.height)))
721 /* First, check if the drag is in a valid drop site in
722 * one of our children
724 if (GTK_IS_CONTAINER (widget))
726 GtkDragFindData new_data = *data;
727 GSList *children = NULL;
730 new_data.x -= x_offset;
731 new_data.y -= y_offset;
732 new_data.found = FALSE;
733 new_data.toplevel = FALSE;
735 /* need to reference children temporarily in case the
736 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
738 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
739 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
741 if (!new_data.found && gtk_widget_is_drawable (tmp_list->data))
742 gtk_drag_find_widget (tmp_list->data, &new_data);
743 g_object_unref (tmp_list->data);
745 g_slist_free (children);
747 data->found = new_data.found;
750 /* If not, and this widget is registered as a drop site, check to
751 * emit "drag-motion" to check if we are actually in
755 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
757 data->found = data->callback (widget,
759 data->x - x_offset - allocation_to_window_x,
760 data->y - y_offset - allocation_to_window_y,
762 /* If so, send a "drag-leave" to the last widget */
765 if (data->info->widget && data->info->widget != widget)
767 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
769 data->info->widget = widget;
776 gtk_drag_dest_leave (GtkWidget *widget,
777 GdkDragContext *context,
780 GtkDragDestSite *site;
782 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
783 g_return_if_fail (site != NULL);
785 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
786 gtk_drag_unhighlight (widget);
788 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
790 g_signal_emit_by_name (widget, "drag-leave", context, time);
792 site->have_drag = FALSE;
796 gtk_drag_dest_motion (GtkWidget *widget,
797 GdkDragContext *context,
802 GtkDragDestSite *site;
803 GdkDragAction action = 0;
806 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
807 g_return_val_if_fail (site != NULL, FALSE);
809 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
811 if (gdk_drag_context_get_suggested_action (context) & site->actions)
812 action = gdk_drag_context_get_suggested_action (context);
814 if (action && gtk_drag_dest_find_target (widget, context, NULL))
816 if (!site->have_drag)
818 site->have_drag = TRUE;
819 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
820 gtk_drag_highlight (widget);
823 gdk_drag_status (context, action, time);
827 gdk_drag_status (context, 0, time);
828 if (!site->track_motion)
833 g_signal_emit_by_name (widget, "drag-motion",
834 context, x, y, time, &retval);
836 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
840 gtk_drag_dest_drop (GtkWidget *widget,
841 GdkDragContext *context,
846 GtkDragDestSite *site;
847 GtkDragDestInfo *info;
850 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
851 g_return_val_if_fail (site != NULL, FALSE);
853 info = gtk_drag_get_dest_info (context, FALSE);
854 g_return_val_if_fail (info != NULL, FALSE);
859 if (site->flags & GTK_DEST_DEFAULT_DROP)
861 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
863 if (target == GDK_NONE)
865 gtk_drag_finish (context, FALSE, FALSE, time);
869 gtk_drag_get_data (widget, context, target, time);
872 g_signal_emit_by_name (widget, "drag-drop",
873 context, x, y, time, &retval);
875 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
879 gtk_drag_dest_set_track_motion (GtkWidget *widget,
880 gboolean track_motion)
882 GtkDragDestSite *site;
884 g_return_if_fail (GTK_IS_WIDGET (widget));
886 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
888 g_return_if_fail (site != NULL);
890 site->track_motion = track_motion != FALSE;
894 gtk_drag_dest_get_track_motion (GtkWidget *widget)
896 GtkDragDestSite *site;
898 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
900 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
903 return site->track_motion;
909 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
912 GtkDragDestInfo *info;
913 GdkDragContext *context;
915 g_return_if_fail (toplevel != NULL);
916 g_return_if_fail (event != NULL);
918 context = event->dnd.context;
920 info = gtk_drag_get_dest_info (context, TRUE);
922 /* Find the widget for the event */
931 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
936 case GDK_DRAG_MOTION:
939 GtkDragFindData data;
942 if (event->type == GDK_DROP_START)
944 info->dropped = TRUE;
945 /* We send a leave here so that the widget unhighlights
950 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
955 gdk_window_get_position (gtk_widget_get_window (toplevel), &tx, &ty);
957 data.x = event->dnd.x_root - tx;
958 data.y = event->dnd.y_root - ty;
959 data.context = context;
962 data.toplevel = TRUE;
963 data.callback = (event->type == GDK_DRAG_MOTION) ?
964 gtk_drag_dest_motion : gtk_drag_dest_drop;
965 data.time = event->dnd.time;
967 gtk_drag_find_widget (toplevel, &data);
969 if (info->widget && !data.found)
971 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
977 if (event->type == GDK_DRAG_MOTION)
980 gdk_drag_status (context, 0, event->dnd.time);
985 g_assert_not_reached ();
992 gtk_drag_dest_find_target (GtkWidget *widget,
993 GdkDragContext *context,
994 GtkTargetList *target_list)
996 id <NSDraggingInfo> dragging_info;
997 NSPasteboard *pasteboard;
998 GtkWidget *source_widget;
1000 GList *tmp_source = NULL;
1001 GList *source_targets;
1003 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1004 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), 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);
1114 info = gtk_drag_get_source_info (context, TRUE);
1116 info->source_widget = g_object_ref (widget);
1117 info->widget = g_object_ref (widget);
1118 info->target_list = target_list;
1119 gtk_target_list_ref (target_list);
1121 info->possible_actions = actions;
1123 g_signal_emit_by_name (widget, "drag-begin", info->context);
1125 /* Ensure that we have an icon before we start the drag; the
1126 * application may have set one in ::drag_begin, or it may
1129 if (!info->icon_pixbuf)
1131 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
1132 gtk_drag_set_icon_default (context);
1134 switch (site->icon_type)
1136 case GTK_IMAGE_PIXBUF:
1137 gtk_drag_set_icon_pixbuf (context,
1138 site->icon_data.pixbuf.pixbuf,
1141 case GTK_IMAGE_STOCK:
1142 gtk_drag_set_icon_stock (context,
1143 site->icon_data.stock.stock_id,
1146 case GTK_IMAGE_ICON_NAME:
1147 gtk_drag_set_icon_name (context,
1148 site->icon_data.name.icon_name,
1151 case GTK_IMAGE_EMPTY:
1153 g_assert_not_reached();
1158 nswindow = get_toplevel_nswindow (widget);
1159 info->nsevent = [nswindow currentEvent];
1160 [info->nsevent retain];
1162 /* drag will begin in an idle handler to avoid nested run loops */
1164 g_idle_add_full (G_PRIORITY_HIGH_IDLE, gtk_drag_begin_idle, context, NULL);
1166 gdk_pointer_ungrab (0);
1172 gtk_drag_begin (GtkWidget *widget,
1173 GtkTargetList *targets,
1174 GdkDragAction actions,
1178 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1179 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
1180 g_return_val_if_fail (targets != NULL, NULL);
1182 return gtk_drag_begin_internal (widget, NULL, targets,
1183 actions, button, event);
1188 gtk_drag_source_event_cb (GtkWidget *widget,
1192 GtkDragSourceSite *site;
1193 gboolean retval = FALSE;
1194 site = (GtkDragSourceSite *)data;
1196 switch (event->type)
1198 case GDK_BUTTON_PRESS:
1199 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1201 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
1202 site->x = event->button.x;
1203 site->y = event->button.y;
1207 case GDK_BUTTON_RELEASE:
1208 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1209 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
1212 case GDK_MOTION_NOTIFY:
1213 if (site->state & event->motion.state & site->start_button_mask)
1215 /* FIXME: This is really broken and can leave us
1221 if (site->state & event->motion.state &
1222 GDK_BUTTON1_MASK << (i - 1))
1226 if (gtk_drag_check_threshold (widget, site->x, site->y,
1227 event->motion.x, event->motion.y))
1230 gtk_drag_begin_internal (widget, site, site->target_list,
1239 default: /* hit for 2/3BUTTON_PRESS */
1247 gtk_drag_source_set (GtkWidget *widget,
1248 GdkModifierType start_button_mask,
1249 const GtkTargetEntry *targets,
1251 GdkDragAction actions)
1253 GtkDragSourceSite *site;
1255 g_return_if_fail (GTK_IS_WIDGET (widget));
1257 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1259 gtk_widget_add_events (widget,
1260 gtk_widget_get_events (widget) |
1261 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1262 GDK_BUTTON_MOTION_MASK);
1266 if (site->target_list)
1267 gtk_target_list_unref (site->target_list);
1271 site = g_new0 (GtkDragSourceSite, 1);
1273 site->icon_type = GTK_IMAGE_EMPTY;
1275 g_signal_connect (widget, "button-press-event",
1276 G_CALLBACK (gtk_drag_source_event_cb),
1278 g_signal_connect (widget, "button-release-event",
1279 G_CALLBACK (gtk_drag_source_event_cb),
1281 g_signal_connect (widget, "motion-notify-event",
1282 G_CALLBACK (gtk_drag_source_event_cb),
1285 g_object_set_data_full (G_OBJECT (widget),
1286 I_("gtk-site-data"),
1287 site, gtk_drag_source_site_destroy);
1290 site->start_button_mask = start_button_mask;
1292 site->target_list = gtk_target_list_new (targets, n_targets);
1294 site->actions = actions;
1297 /*************************************************************
1298 * gtk_drag_source_unset
1299 * Unregister this widget as a drag source.
1303 *************************************************************/
1306 gtk_drag_source_unset (GtkWidget *widget)
1308 GtkDragSourceSite *site;
1310 g_return_if_fail (GTK_IS_WIDGET (widget));
1312 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1316 g_signal_handlers_disconnect_by_func (widget,
1317 gtk_drag_source_event_cb,
1319 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
1324 gtk_drag_source_get_target_list (GtkWidget *widget)
1326 GtkDragSourceSite *site;
1328 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1330 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1332 return site ? site->target_list : NULL;
1337 gtk_drag_source_set_target_list (GtkWidget *widget,
1338 GtkTargetList *target_list)
1340 GtkDragSourceSite *site;
1342 g_return_if_fail (GTK_IS_WIDGET (widget));
1344 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1347 g_warning ("gtk_drag_source_set_target_list() requires the widget "
1348 "to already be a drag source.");
1353 gtk_target_list_ref (target_list);
1355 if (site->target_list)
1356 gtk_target_list_unref (site->target_list);
1358 site->target_list = target_list;
1362 * gtk_drag_source_add_text_targets:
1363 * @widget: a #GtkWidget that's is a drag source
1365 * Add the text targets supported by #GtkSelection to
1366 * the target list of the drag source. The targets
1367 * are added with @info = 0. If you need another value,
1368 * use gtk_target_list_add_text_targets() and
1369 * gtk_drag_source_set_target_list().
1374 gtk_drag_source_add_text_targets (GtkWidget *widget)
1376 GtkTargetList *target_list;
1378 target_list = gtk_drag_source_get_target_list (widget);
1380 gtk_target_list_ref (target_list);
1382 target_list = gtk_target_list_new (NULL, 0);
1383 gtk_target_list_add_text_targets (target_list, 0);
1384 gtk_drag_source_set_target_list (widget, target_list);
1385 gtk_target_list_unref (target_list);
1389 gtk_drag_source_add_image_targets (GtkWidget *widget)
1391 GtkTargetList *target_list;
1393 target_list = gtk_drag_source_get_target_list (widget);
1395 gtk_target_list_ref (target_list);
1397 target_list = gtk_target_list_new (NULL, 0);
1398 gtk_target_list_add_image_targets (target_list, 0, TRUE);
1399 gtk_drag_source_set_target_list (widget, target_list);
1400 gtk_target_list_unref (target_list);
1404 gtk_drag_source_add_uri_targets (GtkWidget *widget)
1406 GtkTargetList *target_list;
1408 target_list = gtk_drag_source_get_target_list (widget);
1410 gtk_target_list_ref (target_list);
1412 target_list = gtk_target_list_new (NULL, 0);
1413 gtk_target_list_add_uri_targets (target_list, 0);
1414 gtk_drag_source_set_target_list (widget, target_list);
1415 gtk_target_list_unref (target_list);
1419 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
1421 switch (site->icon_type)
1423 case GTK_IMAGE_EMPTY:
1425 case GTK_IMAGE_PIXBUF:
1426 g_object_unref (site->icon_data.pixbuf.pixbuf);
1428 case GTK_IMAGE_STOCK:
1429 g_free (site->icon_data.stock.stock_id);
1431 case GTK_IMAGE_ICON_NAME:
1432 g_free (site->icon_data.name.icon_name);
1435 g_assert_not_reached();
1438 site->icon_type = GTK_IMAGE_EMPTY;
1442 gtk_drag_source_site_destroy (gpointer data)
1444 GtkDragSourceSite *site = data;
1446 if (site->target_list)
1447 gtk_target_list_unref (site->target_list);
1449 gtk_drag_source_unset_icon (site);
1454 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
1457 GtkDragSourceSite *site;
1459 g_return_if_fail (GTK_IS_WIDGET (widget));
1460 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1462 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1463 g_return_if_fail (site != NULL);
1464 g_object_ref (pixbuf);
1466 gtk_drag_source_unset_icon (site);
1468 site->icon_type = GTK_IMAGE_PIXBUF;
1469 site->icon_data.pixbuf.pixbuf = pixbuf;
1473 * gtk_drag_source_set_icon_stock:
1474 * @widget: a #GtkWidget
1475 * @stock_id: the ID of the stock icon to use
1477 * Sets the icon that will be used for drags from a particular source
1481 gtk_drag_source_set_icon_stock (GtkWidget *widget,
1482 const gchar *stock_id)
1484 GtkDragSourceSite *site;
1486 g_return_if_fail (GTK_IS_WIDGET (widget));
1487 g_return_if_fail (stock_id != NULL);
1489 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1490 g_return_if_fail (site != NULL);
1492 gtk_drag_source_unset_icon (site);
1494 site->icon_type = GTK_IMAGE_STOCK;
1495 site->icon_data.stock.stock_id = g_strdup (stock_id);
1499 * gtk_drag_source_set_icon_name:
1500 * @widget: a #GtkWidget
1501 * @icon_name: name of icon to use
1503 * Sets the icon that will be used for drags from a particular source
1504 * to a themed icon. See the docs for #GtkIconTheme for more details.
1509 gtk_drag_source_set_icon_name (GtkWidget *widget,
1510 const gchar *icon_name)
1512 GtkDragSourceSite *site;
1514 g_return_if_fail (GTK_IS_WIDGET (widget));
1515 g_return_if_fail (icon_name != NULL);
1517 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1518 g_return_if_fail (site != NULL);
1520 gtk_drag_source_unset_icon (site);
1522 site->icon_type = GTK_IMAGE_ICON_NAME;
1523 site->icon_data.name.icon_name = g_strdup (icon_name);
1528 * gtk_drag_set_icon_widget:
1529 * @context: the context for a drag. (This must be called
1530 with a context for the source side of a drag)
1531 * @widget: a toplevel window to use as an icon.
1532 * @hot_x: the X offset within @widget of the hotspot.
1533 * @hot_y: the Y offset within @widget of the hotspot.
1535 * Changes the icon for a widget to a given widget. GTK+
1536 * will not destroy the icon, so if you don't want
1537 * it to persist, you should connect to the "drag-end"
1538 * signal and destroy it yourself.
1541 gtk_drag_set_icon_widget (GdkDragContext *context,
1546 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1547 g_return_if_fail (GTK_IS_WIDGET (widget));
1549 g_warning ("gtk_drag_set_icon_widget is not supported on Mac OS X");
1553 set_icon_stock_pixbuf (GdkDragContext *context,
1554 const gchar *stock_id,
1559 GtkDragSourceInfo *info;
1561 info = gtk_drag_get_source_info (context, FALSE);
1565 pixbuf = gtk_widget_render_icon_pixbuf (info->widget, stock_id,
1570 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
1575 g_object_ref (pixbuf);
1577 if (info->icon_pixbuf)
1578 g_object_unref (info->icon_pixbuf);
1579 info->icon_pixbuf = pixbuf;
1580 info->hot_x = hot_x;
1581 info->hot_y = hot_y;
1585 * gtk_drag_set_icon_pixbuf:
1586 * @context: the context for a drag. (This must be called
1587 * with a context for the source side of a drag)
1588 * @pixbuf: the #GdkPixbuf to use as the drag icon.
1589 * @hot_x: the X offset within @widget of the hotspot.
1590 * @hot_y: the Y offset within @widget of the hotspot.
1592 * Sets @pixbuf as the icon for a given drag.
1595 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
1600 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1601 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1603 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1607 * gtk_drag_set_icon_stock:
1608 * @context: the context for a drag. (This must be called
1609 * with a context for the source side of a drag)
1610 * @stock_id: the ID of the stock icon to use for the drag.
1611 * @hot_x: the X offset within the icon of the hotspot.
1612 * @hot_y: the Y offset within the icon of the hotspot.
1614 * Sets the icon for a given drag from a stock ID.
1617 gtk_drag_set_icon_stock (GdkDragContext *context,
1618 const gchar *stock_id,
1623 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1624 g_return_if_fail (stock_id != NULL);
1626 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
1630 /* XXX: This function is in gdk, too. Should it be in Cairo? */
1632 _gtk_cairo_surface_extents (cairo_surface_t *surface,
1633 GdkRectangle *extents)
1635 double x1, x2, y1, y2;
1638 g_return_val_if_fail (surface != NULL, FALSE);
1639 g_return_val_if_fail (extents != NULL, FALSE);
1641 cr = cairo_create (surface);
1642 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
1651 if (x1 < G_MININT || x1 > G_MAXINT ||
1652 y1 < G_MININT || y1 > G_MAXINT ||
1653 x2 > G_MAXINT || y2 > G_MAXINT)
1655 extents->x = extents->y = extents->width = extents->height = 0;
1661 extents->width = x2;
1662 extents->height = y2;
1668 * gtk_drag_set_icon_surface:
1669 * @context: the context for a drag. (This must be called
1670 * with a context for the source side of a drag)
1671 * @surface: the surface to use as icon
1673 * Sets @surface as the icon for a given drag. GTK+ retains
1674 * references for the arguments, and will release them when
1675 * they are no longer needed.
1677 * To position the surface relative to the mouse, use
1678 * cairo_surface_set_device_offset() on @surface. The mouse
1679 * cursor will be positioned at the (0,0) coordinate of the
1683 gtk_drag_set_icon_surface (GdkDragContext *context,
1684 cairo_surface_t *surface)
1687 GdkRectangle extents;
1688 double x_offset, y_offset;
1690 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1691 g_return_if_fail (surface != NULL);
1693 _gtk_cairo_surface_extents (surface, &extents);
1694 cairo_surface_get_device_offset (surface, &x_offset, &y_offset);
1696 pixbuf = gdk_pixbuf_get_from_surface (surface,
1697 extents.x, extents.y,
1698 extents.width, extents.height);
1699 gtk_drag_set_icon_pixbuf (context, pixbuf, -x_offset, -y_offset);
1700 g_object_unref (pixbuf);
1704 * gtk_drag_set_icon_name:
1705 * @context: the context for a drag. (This must be called
1706 * with a context for the source side of a drag)
1707 * @icon_name: name of icon to use
1708 * @hot_x: the X offset of the hotspot within the icon
1709 * @hot_y: the Y offset of the hotspot within the icon
1711 * Sets the icon for a given drag from a named themed icon. See
1712 * the docs for #GtkIconTheme for more details. Note that the
1713 * size of the icon depends on the icon theme (the icon is
1714 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
1715 * @hot_x and @hot_y have to be used with care.
1720 gtk_drag_set_icon_name (GdkDragContext *context,
1721 const gchar *icon_name,
1726 GtkSettings *settings;
1727 GtkIconTheme *icon_theme;
1729 gint width, height, icon_size;
1731 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1732 g_return_if_fail (icon_name != NULL);
1734 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
1735 g_return_if_fail (screen != NULL);
1737 settings = gtk_settings_get_for_screen (screen);
1738 if (gtk_icon_size_lookup_for_settings (settings,
1741 icon_size = MAX (width, height);
1743 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
1745 icon_theme = gtk_icon_theme_get_for_screen (screen);
1747 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
1748 icon_size, 0, NULL);
1750 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1752 g_warning ("Cannot load drag icon from icon name %s", icon_name);
1756 * gtk_drag_set_icon_default:
1757 * @context: the context for a drag. (This must be called
1758 with a context for the source side of a drag)
1760 * Sets the icon for a particular drag to the default
1764 gtk_drag_set_icon_default (GdkDragContext *context)
1766 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1768 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
1772 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
1774 if (info->icon_pixbuf)
1775 g_object_unref (info->icon_pixbuf);
1777 g_signal_emit_by_name (info->widget, "drag-end",
1780 if (info->source_widget)
1781 g_object_unref (info->source_widget);
1784 g_object_unref (info->widget);
1786 gtk_target_list_unref (info->target_list);
1788 gtk_drag_clear_source_info (info->context);
1789 g_object_unref (info->context);
1795 drag_drop_finished_idle_cb (gpointer data)
1797 gtk_drag_source_info_destroy (data);
1802 gtk_drag_drop_finished (GtkDragSourceInfo *info)
1804 if (info->success && info->delete)
1805 g_signal_emit_by_name (info->source_widget, "drag-data-delete",
1808 /* Workaround for the fact that the NS API blocks until the drag is
1809 * over. This way the context is still valid when returning from
1810 * drag_begin, even if it will still be quite useless. See bug #501588.
1812 g_idle_add (drag_drop_finished_idle_cb, info);
1815 /*************************************************************
1816 * _gtk_drag_source_handle_event:
1817 * Called from widget event handling code on Drag events
1821 * toplevel: Toplevel widget that received the event
1824 *************************************************************/
1827 _gtk_drag_source_handle_event (GtkWidget *widget,
1830 GtkDragSourceInfo *info;
1831 GdkDragContext *context;
1833 g_return_if_fail (widget != NULL);
1834 g_return_if_fail (event != NULL);
1836 context = event->dnd.context;
1837 info = gtk_drag_get_source_info (context, FALSE);
1841 switch (event->type)
1843 case GDK_DROP_FINISHED:
1844 gtk_drag_drop_finished (info);
1847 g_assert_not_reached ();
1853 gtk_drag_check_threshold (GtkWidget *widget,
1859 gint drag_threshold;
1861 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1863 g_object_get (gtk_widget_get_settings (widget),
1864 "gtk-dnd-drag-threshold", &drag_threshold,
1867 return (ABS (current_x - start_x) > drag_threshold ||
1868 ABS (current_y - start_y) > drag_threshold);