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"
48 #include "gtkquartz.h"
49 #include "gdk/quartz/gdkquartz.h"
51 typedef struct _GtkDragSourceSite GtkDragSourceSite;
52 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
53 typedef struct _GtkDragDestSite GtkDragDestSite;
54 typedef struct _GtkDragDestInfo GtkDragDestInfo;
55 typedef struct _GtkDragFindData GtkDragFindData;
57 static void gtk_drag_find_widget (GtkWidget *widget,
58 GtkDragFindData *data);
59 static void gtk_drag_dest_site_destroy (gpointer data);
60 static void gtk_drag_dest_leave (GtkWidget *widget,
61 GdkDragContext *context,
63 static GtkDragDestInfo *gtk_drag_get_dest_info (GdkDragContext *context,
65 static void gtk_drag_source_site_destroy (gpointer data);
67 struct _GtkDragSourceSite
69 GdkModifierType start_button_mask;
70 GtkTargetList *target_list; /* Targets for drag data */
71 GdkDragAction actions; /* Possible actions */
74 GtkImageType icon_type;
77 GtkImagePixmapData pixmap;
78 GtkImagePixbufData pixbuf;
79 GtkImageStockData stock;
80 GtkImageIconNameData name;
84 GdkColormap *colormap; /* Colormap for drag icon */
86 /* Stored button press information to detect drag beginning */
91 struct _GtkDragSourceInfo
94 GtkTargetList *target_list; /* Targets for drag data */
95 GdkDragAction possible_actions; /* Actions allowed by source */
96 GdkDragContext *context; /* drag context */
98 gint hot_x, hot_y; /* Hot spot for drag */
99 GdkPixbuf *icon_pixbuf;
102 struct _GtkDragDestSite
104 GtkDestDefaults flags;
105 GtkTargetList *target_list;
106 GdkDragAction actions;
110 struct _GtkDragDestInfo
112 GtkWidget *widget; /* Widget in which drag is in */
113 GdkDragContext *context; /* Drag context */
114 guint dropped : 1; /* Set after we receive a drop */
115 gint drop_x, drop_y; /* Position of drop */
118 struct _GtkDragFindData
122 GdkDragContext *context;
123 GtkDragDestInfo *info;
126 gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
127 gint x, gint y, guint32 time);
132 @interface GtkDragSourceOwner : NSObject {
133 GtkDragSourceInfo *info;
138 @implementation GtkDragSourceOwner
139 -(void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
142 GtkSelectionData selection_data;
144 selection_data.selection = GDK_NONE;
145 selection_data.data = NULL;
146 selection_data.target = _gtk_quartz_pasteboard_type_to_atom (type);
148 if (gtk_target_list_find (info->target_list,
149 selection_data.target,
152 g_signal_emit_by_name (info->widget, "drag_data_get",
158 _gtk_quartz_set_selection_data_for_pasteboard (sender, &selection_data);
160 g_free (selection_data.data);
164 - (id)initWithInfo:(GtkDragSourceInfo *)anInfo
179 gtk_drag_get_data (GtkWidget *widget,
180 GdkDragContext *context,
184 id <NSDraggingInfo> dragging_info = GDK_DRAG_CONTEXT_PRIVATE (context)->dragging_info;
185 NSPasteboard *pasteboard = [dragging_info draggingPasteboard];
186 GtkSelectionData *selection_data;
187 GtkDragDestInfo *info;
188 GtkDragDestSite *site;
190 info = gtk_drag_get_dest_info (context, FALSE);
191 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
193 selection_data = _gtk_quartz_get_selection_data_from_pasteboard (pasteboard,
196 if (site && site->target_list)
200 if (gtk_target_list_find (site->target_list,
201 selection_data->target,
204 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
205 selection_data->length >= 0)
206 g_signal_emit_by_name (widget,
207 "drag_data_received",
208 context, info->drop_x, info->drop_y,
215 g_signal_emit_by_name (widget,
216 "drag_data_received",
217 context, info->drop_x, info->drop_y,
222 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
224 gtk_drag_finish (context,
225 (selection_data->length >= 0),
226 (context->action == GDK_ACTION_MOVE),
233 gtk_drag_get_source_widget (GdkDragContext *context)
239 gtk_drag_finish (GdkDragContext *context,
247 gtk_drag_dest_info_destroy (gpointer data)
249 GtkDragDestInfo *info = data;
254 static GtkDragDestInfo *
255 gtk_drag_get_dest_info (GdkDragContext *context,
258 GtkDragDestInfo *info;
259 static GQuark info_quark = 0;
261 info_quark = g_quark_from_static_string ("gtk-dest-info");
263 info = g_object_get_qdata (G_OBJECT (context), info_quark);
266 info = g_new (GtkDragDestInfo, 1);
268 info->context = context;
269 info->dropped = FALSE;
270 g_object_set_qdata_full (G_OBJECT (context), info_quark,
271 info, gtk_drag_dest_info_destroy);
277 static GQuark dest_info_quark = 0;
279 static GtkDragSourceInfo *
280 gtk_drag_get_source_info (GdkDragContext *context,
283 GtkDragSourceInfo *info;
284 if (!dest_info_quark)
285 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
287 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
290 info = g_new0 (GtkDragSourceInfo, 1);
291 info->context = context;
292 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
299 gtk_drag_clear_source_info (GdkDragContext *context)
301 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
304 /*************************************************************
305 * gtk_drag_highlight_expose:
306 * Callback for expose_event for highlighted widgets.
312 *************************************************************/
315 gtk_drag_highlight_expose (GtkWidget *widget,
316 GdkEventExpose *event,
319 gint x, y, width, height;
321 if (GTK_WIDGET_DRAWABLE (widget))
325 if (GTK_WIDGET_NO_WINDOW (widget))
327 x = widget->allocation.x;
328 y = widget->allocation.y;
329 width = widget->allocation.width;
330 height = widget->allocation.height;
336 gdk_drawable_get_size (widget->window, &width, &height);
339 gtk_paint_shadow (widget->style, widget->window,
340 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
342 x, y, width, height);
344 cr = gdk_cairo_create (widget->window);
345 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
346 cairo_set_line_width (cr, 1.0);
349 width - 1, height - 1);
357 /*************************************************************
358 * gtk_drag_highlight:
359 * Highlight the given widget in the default manner.
363 *************************************************************/
366 gtk_drag_highlight (GtkWidget *widget)
368 g_return_if_fail (GTK_IS_WIDGET (widget));
370 g_signal_connect_after (widget, "expose_event",
371 G_CALLBACK (gtk_drag_highlight_expose),
374 gtk_widget_queue_draw (widget);
377 /*************************************************************
378 * gtk_drag_unhighlight:
379 * Refresh the given widget to remove the highlight.
383 *************************************************************/
386 gtk_drag_unhighlight (GtkWidget *widget)
388 g_return_if_fail (GTK_IS_WIDGET (widget));
390 g_signal_handlers_disconnect_by_func (widget,
391 gtk_drag_highlight_expose,
394 gtk_widget_queue_draw (widget);
398 get_toplevel_nswindow (GtkWidget *widget)
400 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
402 if (GTK_WIDGET_TOPLEVEL (toplevel) && toplevel->window)
403 return [gdk_quartz_window_get_nsview (toplevel->window) window];
409 register_types (GtkWidget *widget, GtkDragDestSite *site)
411 if (site->target_list)
413 NSWindow *nswindow = get_toplevel_nswindow (widget);
415 NSAutoreleasePool *pool;
420 pool= [[NSAutoreleasePool alloc] init];
421 types = _gtk_quartz_target_list_to_pasteboard_types (site->target_list);
423 [nswindow registerForDraggedTypes:types];
429 gtk_drag_dest_realized (GtkWidget *widget,
432 GtkDragDestSite *site = user_data;
434 register_types (widget, site);
438 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
439 GtkWidget *previous_toplevel,
442 GtkDragDestSite *site = user_data;
444 register_types (widget, site);
448 gtk_drag_dest_site_destroy (gpointer data)
450 GtkDragDestSite *site = data;
452 if (site->target_list)
453 gtk_target_list_unref (site->target_list);
459 gtk_drag_dest_set (GtkWidget *widget,
460 GtkDestDefaults flags,
461 const GtkTargetEntry *targets,
463 GdkDragAction actions)
465 GtkDragDestSite *old_site, *site;
467 g_return_if_fail (GTK_IS_WIDGET (widget));
469 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
472 g_signal_handlers_disconnect_by_func (widget,
473 gtk_drag_dest_realized,
475 g_signal_handlers_disconnect_by_func (widget,
476 gtk_drag_dest_hierarchy_changed,
480 site = g_new (GtkDragDestSite, 1);
482 site->have_drag = FALSE;
484 site->target_list = gtk_target_list_new (targets, n_targets);
486 site->target_list = NULL;
487 site->actions = actions;
489 if (GTK_WIDGET_REALIZED (widget))
490 gtk_drag_dest_realized (widget, site);
492 g_signal_connect (widget, "realize",
493 G_CALLBACK (gtk_drag_dest_realized), site);
494 g_signal_connect (widget, "hierarchy_changed",
495 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
497 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
498 site, gtk_drag_dest_site_destroy);
502 gtk_drag_dest_set_proxy (GtkWidget *widget,
503 GdkWindow *proxy_window,
504 GdkDragProtocol protocol,
505 gboolean use_coordinates)
507 g_warning ("gtk_drag_dest_set_proxy is not supported on Mac OS X.");
511 gtk_drag_dest_unset (GtkWidget *widget)
513 g_return_if_fail (GTK_IS_WIDGET (widget));
515 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
519 gtk_drag_dest_get_target_list (GtkWidget *widget)
521 GtkDragDestSite *site;
523 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
525 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
527 return site ? site->target_list : NULL;
531 gtk_drag_dest_set_target_list (GtkWidget *widget,
532 GtkTargetList *target_list)
534 GtkDragDestSite *site;
536 g_return_if_fail (GTK_IS_WIDGET (widget));
538 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
542 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
543 "to make the widget into a drag destination");
548 gtk_target_list_ref (target_list);
550 if (site->target_list)
551 gtk_target_list_unref (site->target_list);
553 site->target_list = target_list;
555 register_types (widget, site);
559 gtk_drag_dest_add_text_targets (GtkWidget *widget)
561 GtkTargetList *target_list;
563 target_list = gtk_drag_dest_get_target_list (widget);
565 gtk_target_list_ref (target_list);
567 target_list = gtk_target_list_new (NULL, 0);
568 gtk_target_list_add_text_targets (target_list, 0);
569 gtk_drag_dest_set_target_list (widget, target_list);
570 gtk_target_list_unref (target_list);
574 gtk_drag_dest_add_image_targets (GtkWidget *widget)
576 GtkTargetList *target_list;
578 target_list = gtk_drag_dest_get_target_list (widget);
580 gtk_target_list_ref (target_list);
582 target_list = gtk_target_list_new (NULL, 0);
583 gtk_target_list_add_image_targets (target_list, 0, FALSE);
584 gtk_drag_dest_set_target_list (widget, target_list);
585 gtk_target_list_unref (target_list);
589 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
591 GtkTargetList *target_list;
593 target_list = gtk_drag_dest_get_target_list (widget);
595 gtk_target_list_ref (target_list);
597 target_list = gtk_target_list_new (NULL, 0);
598 gtk_target_list_add_uri_targets (target_list, 0);
599 gtk_drag_dest_set_target_list (widget, target_list);
600 gtk_target_list_unref (target_list);
604 prepend_and_ref_widget (GtkWidget *widget,
607 GSList **slist_p = data;
609 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
613 gtk_drag_find_widget (GtkWidget *widget,
614 GtkDragFindData *data)
616 GtkAllocation new_allocation;
617 gint allocation_to_window_x = 0;
618 gint allocation_to_window_y = 0;
622 if (data->found || !GTK_WIDGET_MAPPED (widget) || !GTK_WIDGET_SENSITIVE (widget))
625 /* Note that in the following code, we only count the
626 * position as being inside a WINDOW widget if it is inside
627 * widget->window; points that are outside of widget->window
628 * but within the allocation are not counted. This is consistent
629 * with the way we highlight drag targets.
631 * data->x,y are relative to widget->parent->window (if
632 * widget is not a toplevel, widget->window otherwise).
633 * We compute the allocation of widget in the same coordinates,
634 * clipping to widget->window, and all intermediate
635 * windows. If data->x,y is inside that, then we translate
636 * our coordinates to be relative to widget->window and
639 new_allocation = widget->allocation;
644 GdkWindow *window = widget->window;
646 /* Compute the offset from allocation-relative to
647 * window-relative coordinates.
649 allocation_to_window_x = widget->allocation.x;
650 allocation_to_window_y = widget->allocation.y;
652 if (!GTK_WIDGET_NO_WINDOW (widget))
654 /* The allocation is relative to the parent window for
655 * window widgets, not to widget->window.
657 gdk_window_get_position (window, &tx, &ty);
659 allocation_to_window_x -= tx;
660 allocation_to_window_y -= ty;
663 new_allocation.x = 0 + allocation_to_window_x;
664 new_allocation.y = 0 + allocation_to_window_y;
666 while (window && window != widget->parent->window)
668 GdkRectangle window_rect = { 0, 0, 0, 0 };
670 gdk_drawable_get_size (window, &window_rect.width, &window_rect.height);
672 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
674 gdk_window_get_position (window, &tx, &ty);
675 new_allocation.x += tx;
677 new_allocation.y += ty;
680 window = gdk_window_get_parent (window);
683 if (!window) /* Window and widget heirarchies didn't match. */
687 if (data->toplevel ||
688 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
689 (data->x < new_allocation.x + new_allocation.width) &&
690 (data->y < new_allocation.y + new_allocation.height)))
692 /* First, check if the drag is in a valid drop site in
693 * one of our children
695 if (GTK_IS_CONTAINER (widget))
697 GtkDragFindData new_data = *data;
698 GSList *children = NULL;
701 new_data.x -= x_offset;
702 new_data.y -= y_offset;
703 new_data.found = FALSE;
704 new_data.toplevel = FALSE;
706 /* need to reference children temporarily in case the
707 * ::drag_motion/::drag_drop callbacks change the widget heirarchy.
709 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
710 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
712 if (!new_data.found && GTK_WIDGET_DRAWABLE (tmp_list->data))
713 gtk_drag_find_widget (tmp_list->data, &new_data);
714 g_object_unref (tmp_list->data);
716 g_slist_free (children);
718 data->found = new_data.found;
721 /* If not, and this widget is registered as a drop site, check to
722 * emit "drag_motion" to check if we are actually in
726 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
728 data->found = data->callback (widget,
730 data->x - x_offset - allocation_to_window_x,
731 data->y - y_offset - allocation_to_window_y,
733 /* If so, send a "drag_leave" to the last widget */
736 if (data->info->widget && data->info->widget != widget)
738 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
740 data->info->widget = widget;
747 gtk_drag_dest_leave (GtkWidget *widget,
748 GdkDragContext *context,
751 GtkDragDestSite *site;
753 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
754 g_return_if_fail (site != NULL);
756 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
757 gtk_drag_unhighlight (widget);
759 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag)
760 g_signal_emit_by_name (widget, "drag_leave",
763 site->have_drag = FALSE;
767 gtk_drag_dest_motion (GtkWidget *widget,
768 GdkDragContext *context,
773 GtkDragDestSite *site;
774 GdkDragAction action = 0;
777 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
778 g_return_val_if_fail (site != NULL, FALSE);
780 if (site->flags & GTK_DEST_DEFAULT_MOTION)
782 if (context->suggested_action & site->actions)
783 action = context->suggested_action;
785 if (action && gtk_drag_dest_find_target (widget, context, NULL))
787 if (!site->have_drag)
789 site->have_drag = TRUE;
790 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
791 gtk_drag_highlight (widget);
794 gdk_drag_status (context, action, time);
798 gdk_drag_status (context, 0, time);
803 g_signal_emit_by_name (widget, "drag_motion",
804 context, x, y, time, &retval);
806 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
810 gtk_drag_dest_drop (GtkWidget *widget,
811 GdkDragContext *context,
816 GtkDragDestSite *site;
817 GtkDragDestInfo *info;
820 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
821 g_return_val_if_fail (site != NULL, FALSE);
823 info = gtk_drag_get_dest_info (context, FALSE);
824 g_return_val_if_fail (info != NULL, FALSE);
829 if (site->flags & GTK_DEST_DEFAULT_DROP)
831 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
833 if (target == GDK_NONE)
835 gtk_drag_finish (context, FALSE, FALSE, time);
839 gtk_drag_get_data (widget, context, target, time);
842 g_signal_emit_by_name (widget, "drag_drop",
843 context, x, y, time, &retval);
845 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
849 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
852 GtkDragDestInfo *info;
853 GdkDragContext *context;
855 g_return_if_fail (toplevel != NULL);
856 g_return_if_fail (event != NULL);
858 context = event->dnd.context;
860 info = gtk_drag_get_dest_info (context, TRUE);
862 /* Find the widget for the event */
871 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
876 case GDK_DRAG_MOTION:
879 GtkDragFindData data;
882 if (event->type == GDK_DROP_START)
884 info->dropped = TRUE;
885 /* We send a leave here so that the widget unhighlights
890 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
895 gdk_window_get_position (toplevel->window, &tx, &ty);
897 data.x = event->dnd.x_root - tx;
898 data.y = event->dnd.y_root - ty;
899 data.context = context;
902 data.toplevel = TRUE;
903 data.callback = (event->type == GDK_DRAG_MOTION) ?
904 gtk_drag_dest_motion : gtk_drag_dest_drop;
905 data.time = event->dnd.time;
907 gtk_drag_find_widget (toplevel, &data);
909 if (info->widget && !data.found)
911 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
917 if (event->type == GDK_DRAG_MOTION)
920 gdk_drag_status (context, 0, event->dnd.time);
925 g_assert_not_reached ();
932 gtk_drag_dest_find_target (GtkWidget *widget,
933 GdkDragContext *context,
934 GtkTargetList *target_list)
936 id <NSDraggingInfo> dragging_info = GDK_DRAG_CONTEXT_PRIVATE (context)->dragging_info;
937 NSPasteboard *pasteboard = [dragging_info draggingPasteboard];
938 GtkWidget *source_widget;
940 GList *tmp_source = NULL;
941 GList *source_targets;
943 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
944 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
945 g_return_val_if_fail (!context->is_source, GDK_NONE);
947 source_widget = gtk_drag_get_source_widget (context);
949 if (target_list == NULL)
950 target_list = gtk_drag_dest_get_target_list (widget);
952 if (target_list == NULL)
955 source_targets = _gtk_quartz_pasteboard_types_to_atom_list ([pasteboard types]);
956 tmp_target = target_list->list;
959 GtkTargetPair *pair = tmp_target->data;
960 tmp_source = source_targets;
963 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
965 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
966 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
968 g_list_free (source_targets);
974 tmp_source = tmp_source->next;
976 tmp_target = tmp_target->next;
979 g_list_free (source_targets);
983 static GdkDragContext *
984 gtk_drag_begin_internal (GtkWidget *widget,
985 GtkDragSourceSite *site,
986 GtkTargetList *target_list,
987 GdkDragAction actions,
991 GtkDragSourceInfo *info;
992 GdkDragContext *context;
994 NSPasteboard *pasteboard;
995 GtkDragSourceOwner *owner;
999 context = gdk_drag_begin (NULL, NULL);
1000 context->is_source = TRUE;
1002 info = gtk_drag_get_source_info (context, TRUE);
1004 info->widget = gtk_widget_ref (widget);
1005 info->target_list = target_list;
1006 gtk_target_list_ref (target_list);
1008 info->possible_actions = actions;
1010 g_signal_emit_by_name (widget, "drag_begin", info->context);
1012 /* Ensure that we have an icon before we start the drag; the
1013 * application may have set one in ::drag_begin, or it may
1016 if (!info->icon_pixbuf)
1018 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
1019 gtk_drag_set_icon_default (context);
1021 switch (site->icon_type)
1023 case GTK_IMAGE_PIXMAP:
1024 gtk_drag_set_icon_pixmap (context,
1026 site->icon_data.pixmap.pixmap,
1030 case GTK_IMAGE_PIXBUF:
1031 gtk_drag_set_icon_pixbuf (context,
1032 site->icon_data.pixbuf.pixbuf,
1035 case GTK_IMAGE_STOCK:
1036 gtk_drag_set_icon_stock (context,
1037 site->icon_data.stock.stock_id,
1040 case GTK_IMAGE_ICON_NAME:
1041 gtk_drag_set_icon_name (context,
1042 site->icon_data.name.icon_name,
1045 case GTK_IMAGE_EMPTY:
1047 g_assert_not_reached();
1052 gdk_pointer_ungrab (0);
1054 pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
1055 owner = [[GtkDragSourceOwner alloc] initWithInfo:info];
1057 [pasteboard declareTypes:_gtk_quartz_target_list_to_pasteboard_types (target_list) owner:owner];
1059 /* Ref the context. It's unreffed when the drag has been aborted */
1060 g_object_ref (info->context);
1062 nswindow = get_toplevel_nswindow (widget);
1064 /* FIXME: If the event isn't a mouse event, use the global cursor position instead */
1065 nsevent = [nswindow currentEvent];
1066 point = [nsevent locationInWindow];
1068 [nswindow dragImage:_gtk_quartz_create_image_from_pixbuf (info->icon_pixbuf)
1070 offset:NSMakeSize(0, 0)
1072 pasteboard:pasteboard
1076 return info->context;
1080 gtk_drag_begin (GtkWidget *widget,
1081 GtkTargetList *targets,
1082 GdkDragAction actions,
1086 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1087 g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), NULL);
1088 g_return_val_if_fail (targets != NULL, NULL);
1090 return gtk_drag_begin_internal (widget, NULL, targets,
1091 actions, button, event);
1096 gtk_drag_source_event_cb (GtkWidget *widget,
1100 GtkDragSourceSite *site;
1101 gboolean retval = FALSE;
1102 site = (GtkDragSourceSite *)data;
1104 switch (event->type)
1106 case GDK_BUTTON_PRESS:
1107 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1109 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
1110 site->x = event->button.x;
1111 site->y = event->button.y;
1115 case GDK_BUTTON_RELEASE:
1116 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
1117 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
1120 case GDK_MOTION_NOTIFY:
1121 if (site->state & event->motion.state & site->start_button_mask)
1123 /* FIXME: This is really broken and can leave us
1129 if (site->state & event->motion.state &
1130 GDK_BUTTON1_MASK << (i - 1))
1134 if (gtk_drag_check_threshold (widget, site->x, site->y,
1135 event->motion.x, event->motion.y))
1138 gtk_drag_begin_internal (widget, site, site->target_list,
1147 default: /* hit for 2/3BUTTON_PRESS */
1155 gtk_drag_source_set (GtkWidget *widget,
1156 GdkModifierType start_button_mask,
1157 const GtkTargetEntry *targets,
1159 GdkDragAction actions)
1161 GtkDragSourceSite *site;
1163 g_return_if_fail (GTK_IS_WIDGET (widget));
1165 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1167 gtk_widget_add_events (widget,
1168 gtk_widget_get_events (widget) |
1169 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1170 GDK_BUTTON_MOTION_MASK);
1174 if (site->target_list)
1175 gtk_target_list_unref (site->target_list);
1179 site = g_new0 (GtkDragSourceSite, 1);
1181 site->icon_type = GTK_IMAGE_EMPTY;
1183 g_signal_connect (widget, "button_press_event",
1184 G_CALLBACK (gtk_drag_source_event_cb),
1186 g_signal_connect (widget, "button_release_event",
1187 G_CALLBACK (gtk_drag_source_event_cb),
1189 g_signal_connect (widget, "motion_notify_event",
1190 G_CALLBACK (gtk_drag_source_event_cb),
1193 g_object_set_data_full (G_OBJECT (widget),
1194 I_("gtk-site-data"),
1195 site, gtk_drag_source_site_destroy);
1198 site->start_button_mask = start_button_mask;
1200 site->target_list = gtk_target_list_new (targets, n_targets);
1202 site->actions = actions;
1205 /*************************************************************
1206 * gtk_drag_source_unset
1207 * Unregister this widget as a drag source.
1211 *************************************************************/
1214 gtk_drag_source_unset (GtkWidget *widget)
1216 GtkDragSourceSite *site;
1218 g_return_if_fail (GTK_IS_WIDGET (widget));
1220 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1224 g_signal_handlers_disconnect_by_func (widget,
1225 gtk_drag_source_event_cb,
1227 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
1232 gtk_drag_source_get_target_list (GtkWidget *widget)
1234 GtkDragSourceSite *site;
1236 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1238 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1240 return site ? site->target_list : NULL;
1245 gtk_drag_source_set_target_list (GtkWidget *widget,
1246 GtkTargetList *target_list)
1248 GtkDragSourceSite *site;
1250 g_return_if_fail (GTK_IS_WIDGET (widget));
1252 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1255 g_warning ("gtk_drag_source_set_target_list() requires the widget "
1256 "to already be a drag source.");
1261 gtk_target_list_ref (target_list);
1263 if (site->target_list)
1264 gtk_target_list_unref (site->target_list);
1266 site->target_list = target_list;
1270 * gtk_drag_source_add_text_targets:
1271 * @widget: a #GtkWidget that's is a drag source
1273 * Add the text targets supported by #GtkSelection to
1274 * the target list of the drag source. The targets
1275 * are added with @info = 0. If you need another value,
1276 * use gtk_target_list_add_text_targets() and
1277 * gtk_drag_source_set_target_list().
1282 gtk_drag_source_add_text_targets (GtkWidget *widget)
1284 GtkTargetList *target_list;
1286 target_list = gtk_drag_source_get_target_list (widget);
1288 gtk_target_list_ref (target_list);
1290 target_list = gtk_target_list_new (NULL, 0);
1291 gtk_target_list_add_text_targets (target_list, 0);
1292 gtk_drag_source_set_target_list (widget, target_list);
1293 gtk_target_list_unref (target_list);
1297 gtk_drag_source_add_image_targets (GtkWidget *widget)
1299 GtkTargetList *target_list;
1301 target_list = gtk_drag_source_get_target_list (widget);
1303 gtk_target_list_ref (target_list);
1305 target_list = gtk_target_list_new (NULL, 0);
1306 gtk_target_list_add_image_targets (target_list, 0, TRUE);
1307 gtk_drag_source_set_target_list (widget, target_list);
1308 gtk_target_list_unref (target_list);
1312 gtk_drag_source_add_uri_targets (GtkWidget *widget)
1314 GtkTargetList *target_list;
1316 target_list = gtk_drag_source_get_target_list (widget);
1318 gtk_target_list_ref (target_list);
1320 target_list = gtk_target_list_new (NULL, 0);
1321 gtk_target_list_add_uri_targets (target_list, 0);
1322 gtk_drag_source_set_target_list (widget, target_list);
1323 gtk_target_list_unref (target_list);
1327 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
1329 switch (site->icon_type)
1331 case GTK_IMAGE_EMPTY:
1333 case GTK_IMAGE_PIXMAP:
1334 if (site->icon_data.pixmap.pixmap)
1335 g_object_unref (site->icon_data.pixmap.pixmap);
1336 if (site->icon_mask)
1337 g_object_unref (site->icon_mask);
1339 case GTK_IMAGE_PIXBUF:
1340 g_object_unref (site->icon_data.pixbuf.pixbuf);
1342 case GTK_IMAGE_STOCK:
1343 g_free (site->icon_data.stock.stock_id);
1345 case GTK_IMAGE_ICON_NAME:
1346 g_free (site->icon_data.name.icon_name);
1349 g_assert_not_reached();
1352 site->icon_type = GTK_IMAGE_EMPTY;
1355 g_object_unref (site->colormap);
1356 site->colormap = NULL;
1360 gtk_drag_source_site_destroy (gpointer data)
1362 GtkDragSourceSite *site = data;
1364 if (site->target_list)
1365 gtk_target_list_unref (site->target_list);
1367 gtk_drag_source_unset_icon (site);
1372 gtk_drag_source_set_icon (GtkWidget *widget,
1373 GdkColormap *colormap,
1377 GtkDragSourceSite *site;
1379 g_return_if_fail (GTK_IS_WIDGET (widget));
1380 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1381 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
1382 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
1384 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1385 g_return_if_fail (site != NULL);
1387 g_object_ref (colormap);
1388 g_object_ref (pixmap);
1390 g_object_ref (mask);
1392 gtk_drag_source_unset_icon (site);
1394 site->icon_type = GTK_IMAGE_PIXMAP;
1396 site->icon_data.pixmap.pixmap = pixmap;
1397 site->icon_mask = mask;
1398 site->colormap = colormap;
1402 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
1405 GtkDragSourceSite *site;
1407 g_return_if_fail (GTK_IS_WIDGET (widget));
1408 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1410 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1411 g_return_if_fail (site != NULL);
1412 g_object_ref (pixbuf);
1414 gtk_drag_source_unset_icon (site);
1416 site->icon_type = GTK_IMAGE_PIXBUF;
1417 site->icon_data.pixbuf.pixbuf = pixbuf;
1421 * gtk_drag_source_set_icon_stock:
1422 * @widget: a #GtkWidget
1423 * @stock_id: the ID of the stock icon to use
1425 * Sets the icon that will be used for drags from a particular source
1429 gtk_drag_source_set_icon_stock (GtkWidget *widget,
1430 const gchar *stock_id)
1432 GtkDragSourceSite *site;
1434 g_return_if_fail (GTK_IS_WIDGET (widget));
1435 g_return_if_fail (stock_id != NULL);
1437 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1438 g_return_if_fail (site != NULL);
1440 gtk_drag_source_unset_icon (site);
1442 site->icon_type = GTK_IMAGE_STOCK;
1443 site->icon_data.stock.stock_id = g_strdup (stock_id);
1447 * gtk_drag_source_set_icon_name:
1448 * @widget: a #GtkWidget
1449 * @icon_name: name of icon to use
1451 * Sets the icon that will be used for drags from a particular source
1452 * to a themed icon. See the docs for #GtkIconTheme for more details.
1457 gtk_drag_source_set_icon_name (GtkWidget *widget,
1458 const gchar *icon_name)
1460 GtkDragSourceSite *site;
1462 g_return_if_fail (GTK_IS_WIDGET (widget));
1463 g_return_if_fail (icon_name != NULL);
1465 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
1466 g_return_if_fail (site != NULL);
1468 gtk_drag_source_unset_icon (site);
1470 site->icon_type = GTK_IMAGE_ICON_NAME;
1471 site->icon_data.name.icon_name = g_strdup (icon_name);
1476 * gtk_drag_set_icon_widget:
1477 * @context: the context for a drag. (This must be called
1478 with a context for the source side of a drag)
1479 * @widget: a toplevel window to use as an icon.
1480 * @hot_x: the X offset within @widget of the hotspot.
1481 * @hot_y: the Y offset within @widget of the hotspot.
1483 * Changes the icon for a widget to a given widget. GTK+
1484 * will not destroy the icon, so if you don't want
1485 * it to persist, you should connect to the "drag_end"
1486 * signal and destroy it yourself.
1489 gtk_drag_set_icon_widget (GdkDragContext *context,
1494 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1495 g_return_if_fail (context->is_source);
1496 g_return_if_fail (GTK_IS_WIDGET (widget));
1498 g_warning ("gtk_drag_set_icon_widget is not supported on Mac OS X");
1502 set_icon_stock_pixbuf (GdkDragContext *context,
1503 const gchar *stock_id,
1508 GtkDragSourceInfo *info;
1510 info = gtk_drag_get_source_info (context, FALSE);
1514 pixbuf = gtk_widget_render_icon (info->widget, stock_id,
1515 GTK_ICON_SIZE_DND, NULL);
1519 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
1524 g_object_ref (pixbuf);
1526 if (info->icon_pixbuf)
1527 g_object_unref (info->icon_pixbuf);
1528 info->icon_pixbuf = pixbuf;
1529 info->hot_x = hot_x;
1530 info->hot_y = hot_y;
1534 * gtk_drag_set_icon_pixbuf:
1535 * @context: the context for a drag. (This must be called
1536 * with a context for the source side of a drag)
1537 * @pixbuf: the #GdkPixbuf to use as the drag icon.
1538 * @hot_x: the X offset within @widget of the hotspot.
1539 * @hot_y: the Y offset within @widget of the hotspot.
1541 * Sets @pixbuf as the icon for a given drag.
1544 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
1549 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1550 g_return_if_fail (context->is_source);
1551 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1553 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1557 * gtk_drag_set_icon_stock:
1558 * @context: the context for a drag. (This must be called
1559 * with a context for the source side of a drag)
1560 * @stock_id: the ID of the stock icon to use for the drag.
1561 * @hot_x: the X offset within the icon of the hotspot.
1562 * @hot_y: the Y offset within the icon of the hotspot.
1564 * Sets the icon for a given drag from a stock ID.
1567 gtk_drag_set_icon_stock (GdkDragContext *context,
1568 const gchar *stock_id,
1573 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1574 g_return_if_fail (context->is_source);
1575 g_return_if_fail (stock_id != NULL);
1577 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y);
1581 * gtk_drag_set_icon_pixmap:
1582 * @context: the context for a drag. (This must be called
1583 * with a context for the source side of a drag)
1584 * @colormap: the colormap of the icon
1585 * @pixmap: the image data for the icon
1586 * @mask: the transparency mask for the icon
1587 * @hot_x: the X offset within @pixmap of the hotspot.
1588 * @hot_y: the Y offset within @pixmap of the hotspot.
1590 * Sets @pixmap as the icon for a given drag. GTK+ retains
1591 * references for the arguments, and will release them when
1592 * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
1593 * will be more convenient to use.
1596 gtk_drag_set_icon_pixmap (GdkDragContext *context,
1597 GdkColormap *colormap,
1603 g_warning ("gtk_drag_set_icon_pixmap is not supported on Mac OS X");
1607 * gtk_drag_set_icon_name:
1608 * @context: the context for a drag. (This must be called
1609 * with a context for the source side of a drag)
1610 * @icon_name: name of icon to use
1611 * @hot_x: the X offset of the hotspot within the icon
1612 * @hot_y: the Y offset of the hotspot within the icon
1614 * Sets the icon for a given drag from a named themed icon. See
1615 * the docs for #GtkIconTheme for more details. Note that the
1616 * size of the icon depends on the icon theme (the icon is
1617 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
1618 * @hot_x and @hot_y have to be used with care.
1623 gtk_drag_set_icon_name (GdkDragContext *context,
1624 const gchar *icon_name,
1629 GtkSettings *settings;
1630 GtkIconTheme *icon_theme;
1632 gint width, height, icon_size;
1634 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1635 g_return_if_fail (context->is_source);
1636 g_return_if_fail (icon_name != NULL);
1638 screen = gdk_drawable_get_screen (context->source_window);
1639 g_return_if_fail (screen != NULL);
1641 settings = gtk_settings_get_for_screen (screen);
1642 if (gtk_icon_size_lookup_for_settings (settings,
1645 icon_size = MAX (width, height);
1647 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
1649 icon_theme = gtk_icon_theme_get_for_screen (screen);
1651 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
1652 icon_size, 0, NULL);
1654 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y);
1656 g_warning ("Cannot load drag icon from icon name %s", icon_name);
1660 * gtk_drag_set_icon_default:
1661 * @context: the context for a drag. (This must be called
1662 with a context for the source side of a drag)
1664 * Sets the icon for a particular drag to the default
1668 gtk_drag_set_icon_default (GdkDragContext *context)
1670 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1671 g_return_if_fail (context->is_source);
1673 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
1677 gtk_drag_set_default_icon (GdkColormap *colormap,
1683 g_warning ("gtk_drag_set_default_icon is not supported on Mac OS X.");
1687 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
1689 if (info->icon_pixbuf)
1690 g_object_unref (info->icon_pixbuf);
1692 g_signal_emit_by_name (info->widget, "drag_end",
1696 g_object_unref (info->widget);
1698 gtk_target_list_unref (info->target_list);
1700 gtk_drag_clear_source_info (info->context);
1701 g_object_unref (info->context);
1707 gtk_drag_drop_finished (GtkDragSourceInfo *info)
1709 gtk_drag_source_info_destroy (info);
1712 /*************************************************************
1713 * _gtk_drag_source_handle_event:
1714 * Called from widget event handling code on Drag events
1718 * toplevel: Toplevel widget that received the event
1721 *************************************************************/
1724 _gtk_drag_source_handle_event (GtkWidget *widget,
1727 GtkDragSourceInfo *info;
1728 GdkDragContext *context;
1730 g_return_if_fail (widget != NULL);
1731 g_return_if_fail (event != NULL);
1733 context = event->dnd.context;
1734 info = gtk_drag_get_source_info (context, FALSE);
1738 switch (event->type)
1740 case GDK_DROP_FINISHED:
1741 gtk_drag_drop_finished (info);
1744 g_assert_not_reached ();
1750 gtk_drag_check_threshold (GtkWidget *widget,
1756 gint drag_threshold;
1758 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1760 g_object_get (gtk_widget_get_settings (widget),
1761 "gtk-dnd-drag-threshold", &drag_threshold,
1764 return (ABS (current_x - start_x) > drag_threshold ||
1765 ABS (current_y - start_y) > drag_threshold);
1768 #define __GTK_DND_C__
1769 #include "gtkaliasdef.c"