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 #ifdef GDK_WINDOWING_X11
37 #include <X11/keysym.h>
38 #include "gdk/x11/gdkx.h"
42 #include "gtkiconfactory.h"
43 #include "gtkicontheme.h"
45 #include "gtkinvisible.h"
49 #include "gtktooltip.h"
50 #include "gtkwindow.h"
52 #include "gtkdndcursors.h"
53 #include "gtkselectionprivate.h"
55 static GSList *source_widgets = NULL;
57 typedef struct _GtkDragSourceSite GtkDragSourceSite;
58 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
59 typedef struct _GtkDragDestSite GtkDragDestSite;
60 typedef struct _GtkDragDestInfo GtkDragDestInfo;
61 typedef struct _GtkDragAnim GtkDragAnim;
71 struct _GtkDragSourceSite
73 GdkModifierType start_button_mask;
74 GtkTargetList *target_list; /* Targets for drag data */
75 GdkDragAction actions; /* Possible actions */
78 GtkImageType icon_type;
81 GtkImagePixbufData pixbuf;
82 GtkImageStockData stock;
83 GtkImageIconNameData name;
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 */
97 GtkWidget *icon_window; /* Window for drag */
98 GtkWidget *fallback_icon; /* Window for drag used on other screens */
99 GtkWidget *ipc_widget; /* GtkInvisible for grab, message passing */
100 GdkCursor *cursor; /* Cursor for drag */
101 gint hot_x, hot_y; /* Hot spot for drag */
102 gint button; /* mouse button starting drag */
104 GtkDragStatus status; /* drag status */
105 GdkEvent *last_event; /* pending event */
107 gint start_x, start_y; /* Initial position */
108 gint cur_x, cur_y; /* Current Position */
109 GdkScreen *cur_screen; /* Current screen for pointer */
111 guint32 grab_time; /* timestamp for initial grab */
112 GList *selections; /* selections we've claimed */
114 GtkDragDestInfo *proxy_dest; /* Set if this is a proxy drag */
116 guint update_idle; /* Idle function to update the drag */
117 guint drop_timeout; /* Timeout for aborting drop */
118 guint destroy_icon : 1; /* If true, destroy icon_window */
119 guint have_grab : 1; /* Do we still have the pointer grab */
120 GdkPixbuf *icon_pixbuf;
121 GdkCursor *drag_cursors[6];
124 struct _GtkDragDestSite
126 GtkDestDefaults flags;
127 GtkTargetList *target_list;
128 GdkDragAction actions;
129 GdkWindow *proxy_window;
130 GdkDragProtocol proxy_protocol;
132 guint proxy_coords : 1;
134 guint track_motion : 1;
137 struct _GtkDragDestInfo
139 GtkWidget *widget; /* Widget in which drag is in */
140 GdkDragContext *context; /* Drag context */
141 GtkDragSourceInfo *proxy_source; /* Set if this is a proxy drag */
142 GtkSelectionData *proxy_data; /* Set while retrieving proxied data */
143 guint32 proxy_drop_time; /* Timestamp for proxied drop */
144 guint proxy_drop_wait : 1; /* Set if we are waiting for a
145 * status reply before sending
148 guint dropped : 1; /* Set after we receive a drop */
149 gint drop_x, drop_y; /* Position of drop */
152 #define DROP_ABORT_TIME 300000
154 #define ANIM_STEP_TIME 50
155 #define ANIM_STEP_LENGTH 50
156 #define ANIM_MIN_STEPS 5
157 #define ANIM_MAX_STEPS 10
161 GtkDragSourceInfo *info;
166 typedef gboolean (* GtkDragDestCallback) (GtkWidget *widget,
167 GdkDragContext *context,
172 /* Enumeration for some targets we handle internally */
175 TARGET_MOTIF_SUCCESS = 0x40000000,
176 TARGET_MOTIF_FAILURE,
180 /* Forward declarations */
181 static void gtk_drag_get_event_actions (GdkEvent *event,
183 GdkDragAction actions,
184 GdkDragAction *suggested_action,
185 GdkDragAction *possible_actions);
186 static GdkCursor * gtk_drag_get_cursor (GdkDisplay *display,
187 GdkDragAction action,
188 GtkDragSourceInfo *info);
189 static void gtk_drag_update_cursor (GtkDragSourceInfo *info);
190 static GtkWidget *gtk_drag_get_ipc_widget (GtkWidget *widget);
191 static GtkWidget *gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen);
192 static void gtk_drag_release_ipc_widget (GtkWidget *widget);
194 static void gtk_drag_selection_received (GtkWidget *widget,
195 GtkSelectionData *selection_data,
198 static gboolean gtk_drag_find_widget (GtkWidget *widget,
199 GdkDragContext *context,
200 GtkDragDestInfo *info,
204 GtkDragDestCallback callback);
205 static void gtk_drag_proxy_begin (GtkWidget *widget,
206 GtkDragDestInfo *dest_info,
208 static void gtk_drag_dest_realized (GtkWidget *widget);
209 static void gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
210 GtkWidget *previous_toplevel);
211 static void gtk_drag_dest_site_destroy (gpointer data);
212 static void gtk_drag_dest_leave (GtkWidget *widget,
213 GdkDragContext *context,
215 static gboolean gtk_drag_dest_motion (GtkWidget *widget,
216 GdkDragContext *context,
220 static gboolean gtk_drag_dest_drop (GtkWidget *widget,
221 GdkDragContext *context,
226 static GtkDragDestInfo * gtk_drag_get_dest_info (GdkDragContext *context,
228 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
230 static void gtk_drag_clear_source_info (GdkDragContext *context);
232 static void gtk_drag_source_check_selection (GtkDragSourceInfo *info,
235 static void gtk_drag_source_release_selections (GtkDragSourceInfo *info,
237 static void gtk_drag_drop (GtkDragSourceInfo *info,
239 static void gtk_drag_drop_finished (GtkDragSourceInfo *info,
240 GtkDragResult result,
242 static void gtk_drag_cancel (GtkDragSourceInfo *info,
243 GtkDragResult result,
246 static gboolean gtk_drag_source_event_cb (GtkWidget *widget,
249 static void gtk_drag_source_site_destroy (gpointer data);
250 static void gtk_drag_selection_get (GtkWidget *widget,
251 GtkSelectionData *selection_data,
255 static gboolean gtk_drag_anim_timeout (gpointer data);
256 static void gtk_drag_remove_icon (GtkDragSourceInfo *info);
257 static void gtk_drag_source_info_destroy (GtkDragSourceInfo *info);
258 static void gtk_drag_add_update_idle (GtkDragSourceInfo *info);
260 static void gtk_drag_update (GtkDragSourceInfo *info,
265 static gboolean gtk_drag_motion_cb (GtkWidget *widget,
266 GdkEventMotion *event,
268 static gboolean gtk_drag_key_cb (GtkWidget *widget,
271 static gboolean gtk_drag_grab_broken_event_cb (GtkWidget *widget,
272 GdkEventGrabBroken *event,
274 static void gtk_drag_grab_notify_cb (GtkWidget *widget,
275 gboolean was_grabbed,
277 static gboolean gtk_drag_button_release_cb (GtkWidget *widget,
278 GdkEventButton *event,
280 static gboolean gtk_drag_abort_timeout (gpointer data);
282 static void set_icon_stock_pixbuf (GdkDragContext *context,
283 const gchar *stock_id,
287 gboolean force_window);
289 /************************
290 * Cursor and Icon data *
291 ************************/
294 GdkDragAction action;
300 { GDK_ACTION_DEFAULT, NULL },
301 { GDK_ACTION_ASK, "dnd-ask", dnd_cursor_ask, NULL, NULL },
302 { GDK_ACTION_COPY, "dnd-copy", dnd_cursor_copy, NULL, NULL },
303 { GDK_ACTION_MOVE, "dnd-move", dnd_cursor_move, NULL, NULL },
304 { GDK_ACTION_LINK, "dnd-link", dnd_cursor_link, NULL, NULL },
305 { 0 , "dnd-none", dnd_cursor_none, NULL, NULL },
308 static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
310 /*********************
311 * Utility functions *
312 *********************/
315 set_can_change_screen (GtkWidget *widget,
316 gboolean can_change_screen)
318 can_change_screen = can_change_screen != FALSE;
320 g_object_set_data (G_OBJECT (widget), I_("gtk-dnd-can-change-screen"),
321 GUINT_TO_POINTER (can_change_screen));
325 get_can_change_screen (GtkWidget *widget)
327 return g_object_get_data (G_OBJECT (widget), "gtk-dnd-can-change-screen") != NULL;
332 gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen)
335 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
336 "gtk-dnd-ipc-widgets");
340 GSList *tmp = drag_widgets;
341 result = drag_widgets->data;
342 drag_widgets = drag_widgets->next;
343 g_object_set_data (G_OBJECT (screen),
344 I_("gtk-dnd-ipc-widgets"),
346 g_slist_free_1 (tmp);
350 result = gtk_window_new (GTK_WINDOW_POPUP);
351 gtk_window_set_screen (GTK_WINDOW (result), screen);
352 gtk_window_resize (GTK_WINDOW (result), 1, 1);
353 gtk_window_move (GTK_WINDOW (result), -100, -100);
354 gtk_widget_show (result);
361 gtk_drag_get_ipc_widget (GtkWidget *widget)
366 result = gtk_drag_get_ipc_widget_for_screen (gtk_widget_get_screen (widget));
368 toplevel = gtk_widget_get_toplevel (widget);
370 if (GTK_IS_WINDOW (toplevel))
372 if (gtk_window_has_group (GTK_WINDOW (toplevel)))
373 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
374 GTK_WINDOW (result));
380 /* FIXME: modifying the XEvent window as in root_key_filter() isn't
381 * going to work with XGE/XI2, since the actual event to handle would
382 * be allocated/freed before GDK gets to translate the event.
383 * Active grabs on the keyboard are used instead at the moment...
385 #if defined (GDK_WINDOWING_X11) && !defined (XINPUT_2)
388 * We want to handle a handful of keys during DND, e.g. Escape to abort.
389 * Grabbing the keyboard has the unfortunate side-effect of preventing
390 * useful things such as using Alt-Tab to cycle between windows or
391 * switching workspaces. Therefore, we just grab the few keys we are
392 * interested in. Note that we need to put the grabs on the root window
393 * in order for them to still work when the focus is moved to another
396 * GDK needs a little help to successfully deliver root key events...
399 static GdkFilterReturn
400 root_key_filter (GdkXEvent *xevent,
404 XEvent *ev = (XEvent *) xevent;
406 if ((ev->type == KeyPress || ev->type == KeyRelease) &&
407 ev->xkey.root == ev->xkey.window)
408 ev->xkey.window = (Window)data;
410 return GDK_FILTER_CONTINUE;
418 static GrabKey grab_keys[] = {
427 { XK_Down, Mod1Mask },
429 { XK_Left, Mod1Mask },
431 { XK_Right, Mod1Mask },
433 { XK_KP_Up, Mod1Mask },
435 { XK_KP_Down, Mod1Mask },
437 { XK_KP_Left, Mod1Mask },
439 { XK_KP_Right, Mod1Mask }
443 grab_dnd_keys (GtkWidget *widget,
448 GdkWindow *window, *root;
451 window = gtk_widget_get_window (widget);
452 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
454 gdk_error_trap_push ();
456 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
458 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
459 XGrabKey (GDK_WINDOW_XDISPLAY (window),
460 keycode, grab_keys[i].modifiers,
461 GDK_WINDOW_XID (root),
468 gdk_error_trap_pop ();
470 gdk_window_add_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
474 ungrab_dnd_keys (GtkWidget *widget,
479 GdkWindow *window, *root;
482 window = gtk_widget_get_window (widget);
483 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
485 gdk_window_remove_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
487 gdk_error_trap_push ();
489 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
491 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
492 XUngrabKey (GDK_WINDOW_XDISPLAY (window),
493 keycode, grab_keys[i].modifiers,
494 GDK_WINDOW_XID (root));
498 gdk_error_trap_pop ();
501 #else /* GDK_WINDOWING_X11 && !XINPUT_2 */
504 grab_dnd_keys (GtkWidget *widget,
508 gdk_device_grab (device,
509 gtk_widget_get_window (widget),
510 GDK_OWNERSHIP_APPLICATION, FALSE,
511 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
516 ungrab_dnd_keys (GtkWidget *widget,
520 gdk_device_ungrab (device, time);
523 #endif /* GDK_WINDOWING_X11 */
526 /***************************************************************
527 * gtk_drag_release_ipc_widget:
528 * Releases widget retrieved with gtk_drag_get_ipc_widget ()
530 * widget: the widget to release.
532 ***************************************************************/
535 gtk_drag_release_ipc_widget (GtkWidget *widget)
537 GtkWindow *window = GTK_WINDOW (widget);
538 GdkScreen *screen = gtk_widget_get_screen (widget);
539 GdkDragContext *context = g_object_get_data (G_OBJECT (widget), "drag-context");
540 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
541 "gtk-dnd-ipc-widgets");
542 GdkDevice *pointer, *keyboard;
546 pointer = gdk_drag_context_get_device (context);
547 keyboard = gdk_device_get_associated_device (pointer);
550 ungrab_dnd_keys (widget, keyboard, GDK_CURRENT_TIME);
553 if (gtk_window_has_group (window))
554 gtk_window_group_remove_window (gtk_window_get_group (window),
556 drag_widgets = g_slist_prepend (drag_widgets, widget);
557 g_object_set_data (G_OBJECT (screen),
558 I_("gtk-dnd-ipc-widgets"),
563 gtk_drag_get_event_time (GdkEvent *event)
565 guint32 tm = GDK_CURRENT_TIME;
570 case GDK_MOTION_NOTIFY:
571 tm = event->motion.time; break;
572 case GDK_BUTTON_PRESS:
573 case GDK_2BUTTON_PRESS:
574 case GDK_3BUTTON_PRESS:
575 case GDK_BUTTON_RELEASE:
576 tm = event->button.time; break;
578 case GDK_KEY_RELEASE:
579 tm = event->key.time; break;
580 case GDK_ENTER_NOTIFY:
581 case GDK_LEAVE_NOTIFY:
582 tm = event->crossing.time; break;
583 case GDK_PROPERTY_NOTIFY:
584 tm = event->property.time; break;
585 case GDK_SELECTION_CLEAR:
586 case GDK_SELECTION_REQUEST:
587 case GDK_SELECTION_NOTIFY:
588 tm = event->selection.time; break;
589 case GDK_PROXIMITY_IN:
590 case GDK_PROXIMITY_OUT:
591 tm = event->proximity.time; break;
592 default: /* use current time */
600 gtk_drag_get_event_actions (GdkEvent *event,
602 GdkDragAction actions,
603 GdkDragAction *suggested_action,
604 GdkDragAction *possible_actions)
606 *suggested_action = 0;
607 *possible_actions = 0;
611 GdkModifierType state = 0;
615 case GDK_MOTION_NOTIFY:
616 state = event->motion.state;
618 case GDK_BUTTON_PRESS:
619 case GDK_2BUTTON_PRESS:
620 case GDK_3BUTTON_PRESS:
621 case GDK_BUTTON_RELEASE:
622 state = event->button.state;
625 case GDK_KEY_RELEASE:
626 state = event->key.state;
628 case GDK_ENTER_NOTIFY:
629 case GDK_LEAVE_NOTIFY:
630 state = event->crossing.state;
636 if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
638 *suggested_action = GDK_ACTION_ASK;
639 *possible_actions = actions;
641 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
643 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
645 if (actions & GDK_ACTION_LINK)
647 *suggested_action = GDK_ACTION_LINK;
648 *possible_actions = GDK_ACTION_LINK;
651 else if (state & GDK_CONTROL_MASK)
653 if (actions & GDK_ACTION_COPY)
655 *suggested_action = GDK_ACTION_COPY;
656 *possible_actions = GDK_ACTION_COPY;
662 if (actions & GDK_ACTION_MOVE)
664 *suggested_action = GDK_ACTION_MOVE;
665 *possible_actions = GDK_ACTION_MOVE;
672 *possible_actions = actions;
674 if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
675 *suggested_action = GDK_ACTION_ASK;
676 else if (actions & GDK_ACTION_COPY)
677 *suggested_action = GDK_ACTION_COPY;
678 else if (actions & GDK_ACTION_MOVE)
679 *suggested_action = GDK_ACTION_MOVE;
680 else if (actions & GDK_ACTION_LINK)
681 *suggested_action = GDK_ACTION_LINK;
686 *possible_actions = actions;
688 if (actions & GDK_ACTION_COPY)
689 *suggested_action = GDK_ACTION_COPY;
690 else if (actions & GDK_ACTION_MOVE)
691 *suggested_action = GDK_ACTION_MOVE;
692 else if (actions & GDK_ACTION_LINK)
693 *suggested_action = GDK_ACTION_LINK;
698 gtk_drag_can_use_rgba_cursor (GdkDisplay *display,
702 guint max_width, max_height;
704 if (!gdk_display_supports_cursor_color (display))
707 if (!gdk_display_supports_cursor_alpha (display))
710 gdk_display_get_maximal_cursor_size (display,
713 if (width > max_width || height > max_height)
715 /* can't use rgba cursor (too large) */
723 gtk_drag_get_cursor (GdkDisplay *display,
724 GdkDragAction action,
725 GtkDragSourceInfo *info)
729 /* reconstruct the cursors for each new drag (thus !info),
730 * to catch cursor theme changes
734 for (i = 0 ; i < n_drag_cursors - 1; i++)
735 if (drag_cursors[i].cursor != NULL)
737 g_object_unref (drag_cursors[i].cursor);
738 drag_cursors[i].cursor = NULL;
742 for (i = 0 ; i < n_drag_cursors - 1; i++)
743 if (drag_cursors[i].action == action)
746 if (drag_cursors[i].pixbuf == NULL)
747 drag_cursors[i].pixbuf =
748 gdk_pixbuf_new_from_inline (-1, drag_cursors[i].data, FALSE, NULL);
750 if (drag_cursors[i].cursor != NULL)
752 if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
754 g_object_unref (drag_cursors[i].cursor);
755 drag_cursors[i].cursor = NULL;
759 if (drag_cursors[i].cursor == NULL)
760 drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
762 if (drag_cursors[i].cursor == NULL)
763 drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);
765 if (info && info->icon_pixbuf)
767 gint cursor_width, cursor_height;
768 gint icon_width, icon_height;
770 GdkPixbuf *cursor_pixbuf, *pixbuf;
772 gint icon_x, icon_y, ref_x, ref_y;
774 if (info->drag_cursors[i] != NULL)
776 if (display == gdk_cursor_get_display (info->drag_cursors[i]))
777 return info->drag_cursors[i];
779 g_object_unref (info->drag_cursors[i]);
780 info->drag_cursors[i] = NULL;
783 icon_x = info->hot_x;
784 icon_y = info->hot_y;
785 icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
786 icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);
789 cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
791 cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
794 if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
795 hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
797 if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
798 hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
801 /* The code below is an attempt to let cursor themes
802 * determine the attachment of the icon to enable things
803 * like the following:
811 * It does not work since Xcursor doesn't allow to attach
812 * any additional information to cursors in a retrievable
813 * way (there are comments, but no way to get at them
814 * short of searching for the actual cursor file).
815 * If this code ever gets used, the icon_window placement
816 * must be changed to recognize these placement options
817 * as well. Note that this code ignores info->hot_x/y.
819 for (j = 0; j < 10; j++)
824 GtkAnchorType icon_anchor;
826 g_snprintf (key, 32, "comment%d", j);
827 opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
828 if (opt && g_str_has_prefix ("icon-attach:", opt))
830 toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
831 if (g_strv_length (toks) != 3)
836 icon_anchor = atoi (toks[0]);
837 icon_x = atoi (toks[1]);
838 icon_y = atoi (toks[2]);
842 case GTK_ANCHOR_NORTH:
843 case GTK_ANCHOR_CENTER:
844 case GTK_ANCHOR_SOUTH:
845 icon_x += icon_width / 2;
847 case GTK_ANCHOR_NORTH_EAST:
848 case GTK_ANCHOR_EAST:
849 case GTK_ANCHOR_SOUTH_EAST:
850 icon_x += icon_width;
857 case GTK_ANCHOR_WEST:
858 case GTK_ANCHOR_CENTER:
859 case GTK_ANCHOR_EAST:
860 icon_y += icon_height / 2;
862 case GTK_ANCHOR_SOUTH_WEST:
863 case GTK_ANCHOR_SOUTH:
864 case GTK_ANCHOR_SOUTH_EAST:
865 icon_x += icon_height;
877 cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
878 cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
880 ref_x = MAX (hot_x, icon_x);
881 ref_y = MAX (hot_y, icon_y);
882 width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
883 height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
885 if (gtk_drag_can_use_rgba_cursor (display, width, height))
887 /* Composite cursor and icon so that both hotspots
888 * end up at (ref_x, ref_y)
890 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
893 gdk_pixbuf_fill (pixbuf, 0xff000000);
895 gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
896 ref_x - icon_x, ref_y - icon_y,
897 icon_width, icon_height,
898 ref_x - icon_x, ref_y - icon_y,
900 GDK_INTERP_BILINEAR, 255);
902 gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
903 ref_x - hot_x, ref_y - hot_y,
904 cursor_width, cursor_height,
905 ref_x - hot_x, ref_y - hot_y,
907 GDK_INTERP_BILINEAR, 255);
909 info->drag_cursors[i] =
910 gdk_cursor_new_from_pixbuf (display, pixbuf, ref_x, ref_y);
912 g_object_unref (pixbuf);
915 g_object_unref (cursor_pixbuf);
917 if (info->drag_cursors[i] != NULL)
918 return info->drag_cursors[i];
921 return drag_cursors[i].cursor;
925 gtk_drag_update_cursor (GtkDragSourceInfo *info)
930 if (!info->have_grab)
933 for (i = 0 ; i < n_drag_cursors - 1; i++)
934 if (info->cursor == drag_cursors[i].cursor ||
935 info->cursor == info->drag_cursors[i])
938 if (i == n_drag_cursors)
941 cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor),
942 drag_cursors[i].action, info);
944 if (cursor != info->cursor)
948 pointer = gdk_drag_context_get_device (info->context);
949 gdk_device_grab (pointer,
950 gtk_widget_get_window (info->ipc_widget),
951 GDK_OWNERSHIP_APPLICATION, FALSE,
952 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
953 cursor, info->grab_time);
954 info->cursor = cursor;
958 /********************
960 ********************/
962 /*************************************************************
964 * Get the data for a drag or drop
966 * context - drag context
967 * target - format to retrieve the data in.
968 * time - timestamp of triggering event.
971 *************************************************************/
974 gtk_drag_get_data (GtkWidget *widget,
975 GdkDragContext *context,
979 GtkWidget *selection_widget;
981 g_return_if_fail (GTK_IS_WIDGET (widget));
982 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
984 selection_widget = gtk_drag_get_ipc_widget (widget);
986 g_object_ref (context);
987 g_object_ref (widget);
989 g_signal_connect (selection_widget, "selection-received",
990 G_CALLBACK (gtk_drag_selection_received), widget);
992 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
994 gtk_selection_convert (selection_widget,
995 gdk_drag_get_selection (context),
1002 * gtk_drag_get_source_widget:
1003 * @context: a (destination side) drag context
1005 * Determines the source widget for a drag.
1007 * Return value: (transfer none): if the drag is occurring
1008 * within a single application, a pointer to the source widget.
1012 gtk_drag_get_source_widget (GdkDragContext *context)
1016 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
1018 tmp_list = source_widgets;
1021 GtkWidget *ipc_widget = tmp_list->data;
1023 if (gtk_widget_get_window (ipc_widget) == gdk_drag_context_get_source_window (context))
1025 GtkDragSourceInfo *info;
1026 info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
1028 return info ? info->widget : NULL;
1031 tmp_list = tmp_list->next;
1037 /*************************************************************
1039 * Notify the drag source that the transfer of data
1042 * context: The drag context for this drag
1043 * success: Was the data successfully transferred?
1044 * time: The timestamp to use when notifying the destination.
1046 *************************************************************/
1049 gtk_drag_finish (GdkDragContext *context,
1054 GdkAtom target = GDK_NONE;
1056 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1060 target = gdk_atom_intern_static_string ("DELETE");
1062 else if (gdk_drag_context_get_protocol (context) == GDK_DRAG_PROTO_MOTIF)
1064 target = gdk_atom_intern_static_string (success ?
1065 "XmTRANSFER_SUCCESS" :
1066 "XmTRANSFER_FAILURE");
1069 if (target != GDK_NONE)
1071 GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_window_get_screen (gdk_drag_context_get_source_window (context)));
1073 g_object_ref (context);
1075 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1076 g_signal_connect (selection_widget, "selection-received",
1077 G_CALLBACK (gtk_drag_selection_received),
1080 gtk_selection_convert (selection_widget,
1081 gdk_drag_get_selection (context),
1086 if (!(success && del))
1087 gdk_drop_finish (context, success, time);
1090 /*************************************************************
1091 * gtk_drag_highlight_draw:
1092 * Callback for expose_event for highlighted widgets.
1098 *************************************************************/
1101 gtk_drag_highlight_draw (GtkWidget *widget,
1105 int width = gtk_widget_get_allocated_width (widget);
1106 int height = gtk_widget_get_allocated_height (widget);
1108 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
1109 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1111 0, 0, width, height);
1113 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
1114 cairo_set_line_width (cr, 1.0);
1115 cairo_rectangle (cr,
1117 width - 1, height - 1);
1123 /*************************************************************
1124 * gtk_drag_highlight:
1125 * Highlight the given widget in the default manner.
1129 *************************************************************/
1132 gtk_drag_highlight (GtkWidget *widget)
1134 g_return_if_fail (GTK_IS_WIDGET (widget));
1136 g_signal_connect_after (widget, "draw",
1137 G_CALLBACK (gtk_drag_highlight_draw),
1140 gtk_widget_queue_draw (widget);
1143 /*************************************************************
1144 * gtk_drag_unhighlight:
1145 * Refresh the given widget to remove the highlight.
1149 *************************************************************/
1152 gtk_drag_unhighlight (GtkWidget *widget)
1154 g_return_if_fail (GTK_IS_WIDGET (widget));
1156 g_signal_handlers_disconnect_by_func (widget,
1157 gtk_drag_highlight_draw,
1160 gtk_widget_queue_draw (widget);
1164 gtk_drag_dest_set_internal (GtkWidget *widget,
1165 GtkDragDestSite *site)
1167 GtkDragDestSite *old_site;
1169 g_return_if_fail (widget != NULL);
1171 /* HACK, do this in the destroy */
1172 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1175 g_signal_handlers_disconnect_by_func (widget,
1176 gtk_drag_dest_realized,
1178 g_signal_handlers_disconnect_by_func (widget,
1179 gtk_drag_dest_hierarchy_changed,
1182 site->track_motion = old_site->track_motion;
1185 if (gtk_widget_get_realized (widget))
1186 gtk_drag_dest_realized (widget);
1188 g_signal_connect (widget, "realize",
1189 G_CALLBACK (gtk_drag_dest_realized), site);
1190 g_signal_connect (widget, "hierarchy-changed",
1191 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1193 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
1194 site, gtk_drag_dest_site_destroy);
1198 * gtk_drag_dest_set:
1199 * @widget: a #GtkWidget
1200 * @flags: which types of default drag behavior to use
1201 * @targets: (allow-none) (array length=n_targets): a pointer to an array of #GtkTargetEntry<!-- -->s
1202 * indicating the drop types that this @widget will accept, or %NULL.
1203 * Later you can access the list with gtk_drag_dest_get_target_list()
1204 * and gtk_drag_dest_find_target().
1205 * @n_targets: the number of entries in @targets
1206 * @actions: a bitmask of possible actions for a drop onto this @widget.
1208 * Sets a widget as a potential drop destination, and adds default behaviors.
1210 * The default behaviors listed in @flags have an effect similar
1211 * to installing default handlers for the widget's drag-and-drop signals
1212 * (#GtkWidget:drag-motion, #GtkWidget:drag-drop, ...). They all exist
1213 * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
1214 * sufficient to connect to the widget's #GtkWidget::drag-data-received
1215 * signal to get primitive, but consistent drag-and-drop support.
1217 * Things become more complicated when you try to preview the dragged data,
1218 * as described in the documentation for #GtkWidget:drag-motion. The default
1219 * behaviors described by @flags make some assumptions, that can conflict
1220 * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
1221 * invokations of gdk_drag_status() in the context of #GtkWidget:drag-motion,
1222 * and invokations of gtk_drag_finish() in #GtkWidget:drag-data-received.
1223 * Especially the later is dramatic, when your own #GtkWidget:drag-motion
1224 * handler calls gtk_drag_get_data() to inspect the dragged data.
1226 * There's no way to set a default action here, you can use the
1227 * #GtkWidget:drag-motion callback for that. Here's an example which selects
1228 * the action to use depending on whether the control key is pressed or not:
1231 * drag_motion (GtkWidget *widget,
1232 * GdkDragContext *context,
1237 * GdkModifierType mask;
1239 * gdk_window_get_pointer (gtk_widget_get_window (widget),
1240 * NULL, NULL, &mask);
1241 * if (mask & GDK_CONTROL_MASK)
1242 * gdk_drag_status (context, GDK_ACTION_COPY, time);
1244 * gdk_drag_status (context, GDK_ACTION_MOVE, time);
1249 gtk_drag_dest_set (GtkWidget *widget,
1250 GtkDestDefaults flags,
1251 const GtkTargetEntry *targets,
1253 GdkDragAction actions)
1255 GtkDragDestSite *site;
1257 g_return_if_fail (GTK_IS_WIDGET (widget));
1259 site = g_slice_new0 (GtkDragDestSite);
1261 site->flags = flags;
1262 site->have_drag = FALSE;
1264 site->target_list = gtk_target_list_new (targets, n_targets);
1266 site->target_list = NULL;
1267 site->actions = actions;
1268 site->do_proxy = FALSE;
1269 site->proxy_window = NULL;
1270 site->track_motion = FALSE;
1272 gtk_drag_dest_set_internal (widget, site);
1275 /*************************************************************
1276 * gtk_drag_dest_set_proxy:
1277 * Set up this widget to proxy drags elsewhere.
1280 * proxy_window: window to which forward drag events
1281 * protocol: Drag protocol which the dest widget accepts
1282 * use_coordinates: If true, send the same coordinates to the
1283 * destination, because it is a embedded
1286 *************************************************************/
1289 gtk_drag_dest_set_proxy (GtkWidget *widget,
1290 GdkWindow *proxy_window,
1291 GdkDragProtocol protocol,
1292 gboolean use_coordinates)
1294 GtkDragDestSite *site;
1296 g_return_if_fail (GTK_IS_WIDGET (widget));
1297 g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1299 site = g_slice_new (GtkDragDestSite);
1302 site->have_drag = FALSE;
1303 site->target_list = NULL;
1305 site->proxy_window = proxy_window;
1307 g_object_ref (proxy_window);
1308 site->do_proxy = TRUE;
1309 site->proxy_protocol = protocol;
1310 site->proxy_coords = use_coordinates;
1311 site->track_motion = FALSE;
1313 gtk_drag_dest_set_internal (widget, site);
1316 /*************************************************************
1317 * gtk_drag_dest_unset
1318 * Unregister this widget as a drag target.
1322 *************************************************************/
1325 gtk_drag_dest_unset (GtkWidget *widget)
1327 GtkDragDestSite *old_site;
1329 g_return_if_fail (GTK_IS_WIDGET (widget));
1331 old_site = g_object_get_data (G_OBJECT (widget),
1335 g_signal_handlers_disconnect_by_func (widget,
1336 gtk_drag_dest_realized,
1338 g_signal_handlers_disconnect_by_func (widget,
1339 gtk_drag_dest_hierarchy_changed,
1343 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1347 * gtk_drag_dest_get_target_list:
1348 * @widget: a #GtkWidget
1350 * Returns the list of targets this widget can accept from
1353 * Return value: the #GtkTargetList, or %NULL if none
1356 gtk_drag_dest_get_target_list (GtkWidget *widget)
1358 GtkDragDestSite *site;
1360 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1362 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1364 return site ? site->target_list : NULL;
1368 * gtk_drag_dest_set_target_list:
1369 * @widget: a #GtkWidget that's a drag destination
1370 * @target_list: (allow-none): list of droppable targets, or %NULL for none
1372 * Sets the target types that this widget can accept from drag-and-drop.
1373 * The widget must first be made into a drag destination with
1374 * gtk_drag_dest_set().
1377 gtk_drag_dest_set_target_list (GtkWidget *widget,
1378 GtkTargetList *target_list)
1380 GtkDragDestSite *site;
1382 g_return_if_fail (GTK_IS_WIDGET (widget));
1384 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1388 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1389 "to make the widget into a drag destination");
1394 gtk_target_list_ref (target_list);
1396 if (site->target_list)
1397 gtk_target_list_unref (site->target_list);
1399 site->target_list = target_list;
1403 * gtk_drag_dest_add_text_targets:
1404 * @widget: a #GtkWidget that's a drag destination
1406 * Add the text targets supported by #GtkSelection to
1407 * the target list of the drag destination. The targets
1408 * are added with @info = 0. If you need another value,
1409 * use gtk_target_list_add_text_targets() and
1410 * gtk_drag_dest_set_target_list().
1415 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1417 GtkTargetList *target_list;
1419 target_list = gtk_drag_dest_get_target_list (widget);
1421 gtk_target_list_ref (target_list);
1423 target_list = gtk_target_list_new (NULL, 0);
1424 gtk_target_list_add_text_targets (target_list, 0);
1425 gtk_drag_dest_set_target_list (widget, target_list);
1426 gtk_target_list_unref (target_list);
1430 * gtk_drag_dest_add_image_targets:
1431 * @widget: a #GtkWidget that's a drag destination
1433 * Add the image targets supported by #GtkSelection to
1434 * the target list of the drag destination. The targets
1435 * are added with @info = 0. If you need another value,
1436 * use gtk_target_list_add_image_targets() and
1437 * gtk_drag_dest_set_target_list().
1442 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1444 GtkTargetList *target_list;
1446 target_list = gtk_drag_dest_get_target_list (widget);
1448 gtk_target_list_ref (target_list);
1450 target_list = gtk_target_list_new (NULL, 0);
1451 gtk_target_list_add_image_targets (target_list, 0, FALSE);
1452 gtk_drag_dest_set_target_list (widget, target_list);
1453 gtk_target_list_unref (target_list);
1457 * gtk_drag_dest_add_uri_targets:
1458 * @widget: a #GtkWidget that's a drag destination
1460 * Add the URI targets supported by #GtkSelection to
1461 * the target list of the drag destination. The targets
1462 * are added with @info = 0. If you need another value,
1463 * use gtk_target_list_add_uri_targets() and
1464 * gtk_drag_dest_set_target_list().
1469 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1471 GtkTargetList *target_list;
1473 target_list = gtk_drag_dest_get_target_list (widget);
1475 gtk_target_list_ref (target_list);
1477 target_list = gtk_target_list_new (NULL, 0);
1478 gtk_target_list_add_uri_targets (target_list, 0);
1479 gtk_drag_dest_set_target_list (widget, target_list);
1480 gtk_target_list_unref (target_list);
1484 * gtk_drag_dest_set_track_motion:
1485 * @widget: a #GtkWidget that's a drag destination
1486 * @track_motion: whether to accept all targets
1488 * Tells the widget to emit ::drag-motion and ::drag-leave
1489 * events regardless of the targets and the %GTK_DEST_DEFAULT_MOTION
1492 * This may be used when a widget wants to do generic
1493 * actions regardless of the targets that the source offers.
1498 gtk_drag_dest_set_track_motion (GtkWidget *widget,
1499 gboolean track_motion)
1501 GtkDragDestSite *site;
1503 g_return_if_fail (GTK_IS_WIDGET (widget));
1505 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1507 g_return_if_fail (site != NULL);
1509 site->track_motion = track_motion != FALSE;
1513 * gtk_drag_dest_get_track_motion:
1514 * @widget: a #GtkWidget that's a drag destination
1516 * Returns whether the widget has been configured to always
1517 * emit ::drag-motion signals.
1519 * Return Value: %TRUE if the widget always emits ::drag-motion events
1524 gtk_drag_dest_get_track_motion (GtkWidget *widget)
1526 GtkDragDestSite *site;
1528 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1530 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1533 return site->track_motion;
1538 /*************************************************************
1539 * _gtk_drag_dest_handle_event:
1540 * Called from widget event handling code on Drag events
1544 * toplevel: Toplevel widget that received the event
1547 *************************************************************/
1550 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1553 GtkDragDestInfo *info;
1554 GdkDragContext *context;
1556 g_return_if_fail (toplevel != NULL);
1557 g_return_if_fail (event != NULL);
1559 context = event->dnd.context;
1561 info = gtk_drag_get_dest_info (context, TRUE);
1563 /* Find the widget for the event */
1564 switch (event->type)
1566 case GDK_DRAG_ENTER:
1569 case GDK_DRAG_LEAVE:
1572 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1573 info->widget = NULL;
1577 case GDK_DRAG_MOTION:
1578 case GDK_DROP_START:
1584 if (event->type == GDK_DROP_START)
1586 info->dropped = TRUE;
1587 /* We send a leave here so that the widget unhighlights
1592 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1593 info->widget = NULL;
1597 window = gtk_widget_get_window (toplevel);
1599 #ifdef GDK_WINDOWING_X11
1600 /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1602 * Currently gdk_window_get_position doesn't provide reliable
1603 * information for embedded windows, so we call the much more
1604 * expensive gdk_window_get_origin().
1606 if (GTK_IS_PLUG (toplevel))
1607 gdk_window_get_origin (window, &tx, &ty);
1609 #endif /* GDK_WINDOWING_X11 */
1610 gdk_window_get_position (window, &tx, &ty);
1612 found = gtk_drag_find_widget (toplevel,
1615 event->dnd.x_root - tx,
1616 event->dnd.y_root - ty,
1618 (event->type == GDK_DRAG_MOTION) ?
1619 gtk_drag_dest_motion :
1620 gtk_drag_dest_drop);
1622 if (info->widget && !found)
1624 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1625 info->widget = NULL;
1630 if (event->type == GDK_DRAG_MOTION)
1633 gdk_drag_status (context, 0, event->dnd.time);
1635 else if (event->type == GDK_DROP_START && !info->proxy_source)
1637 gdk_drop_reply (context, found, event->dnd.time);
1638 if ((gdk_drag_context_get_protocol (context) == GDK_DRAG_PROTO_MOTIF) && !found)
1639 gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1645 g_assert_not_reached ();
1650 * gtk_drag_dest_find_target:
1651 * @widget: drag destination widget
1652 * @context: drag context
1653 * @target_list: (allow-none): list of droppable targets, or %NULL to use
1654 * gtk_drag_dest_get_target_list (@widget).
1656 * Looks for a match between the supported targets of @context and the
1657 * @dest_target_list, returning the first matching target, otherwise
1658 * returning %GDK_NONE. @dest_target_list should usually be the return
1659 * value from gtk_drag_dest_get_target_list(), but some widgets may
1660 * have different valid targets for different parts of the widget; in
1661 * that case, they will have to implement a drag_motion handler that
1662 * passes the correct target list to this function.
1664 * Return value: first target that the source offers and the dest can
1665 * accept, or %GDK_NONE
1668 gtk_drag_dest_find_target (GtkWidget *widget,
1669 GdkDragContext *context,
1670 GtkTargetList *target_list)
1673 GList *tmp_source = NULL;
1674 GtkWidget *source_widget;
1676 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1677 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1680 source_widget = gtk_drag_get_source_widget (context);
1682 if (target_list == NULL)
1683 target_list = gtk_drag_dest_get_target_list (widget);
1685 if (target_list == NULL)
1688 tmp_target = target_list->list;
1691 GtkTargetPair *pair = tmp_target->data;
1692 tmp_source = gdk_drag_context_list_targets (context);
1695 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1697 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1698 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)) &&
1699 (!(pair->flags & GTK_TARGET_OTHER_APP) || !source_widget) &&
1700 (!(pair->flags & GTK_TARGET_OTHER_WIDGET) || (source_widget != widget)))
1701 return pair->target;
1705 tmp_source = tmp_source->next;
1707 tmp_target = tmp_target->next;
1714 gtk_drag_selection_received (GtkWidget *widget,
1715 GtkSelectionData *selection_data,
1719 GdkDragContext *context;
1720 GtkDragDestInfo *info;
1721 GtkWidget *drop_widget;
1726 context = g_object_get_data (G_OBJECT (widget), "drag-context");
1727 info = gtk_drag_get_dest_info (context, FALSE);
1729 if (info->proxy_data &&
1730 gtk_selection_data_get_target (info->proxy_data) == gtk_selection_data_get_target (selection_data))
1732 gtk_selection_data_set (info->proxy_data,
1733 gtk_selection_data_get_data_type (selection_data),
1734 gtk_selection_data_get_format (selection_data),
1735 gtk_selection_data_get_data (selection_data),
1736 gtk_selection_data_get_length (selection_data));
1741 target = gtk_selection_data_get_target (selection_data);
1742 if (target == gdk_atom_intern_static_string ("DELETE"))
1744 gtk_drag_finish (context, TRUE, FALSE, time);
1746 else if ((target == gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS")) ||
1747 (target == gdk_atom_intern_static_string ("XmTRANSFER_FAILURE")))
1753 GtkDragDestSite *site;
1755 site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1757 if (site && site->target_list)
1761 if (gtk_target_list_find (site->target_list,
1765 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1766 gtk_selection_data_get_length (selection_data) >= 0)
1767 g_signal_emit_by_name (drop_widget,
1768 "drag-data-received",
1769 context, info->drop_x, info->drop_y,
1776 g_signal_emit_by_name (drop_widget,
1777 "drag-data-received",
1778 context, info->drop_x, info->drop_y,
1783 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1786 gtk_drag_finish (context,
1787 (gtk_selection_data_get_length (selection_data) >= 0),
1788 (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
1792 g_object_unref (drop_widget);
1795 g_signal_handlers_disconnect_by_func (widget,
1796 gtk_drag_selection_received,
1799 g_object_set_data (G_OBJECT (widget), I_("drag-context"), NULL);
1800 g_object_unref (context);
1802 gtk_drag_release_ipc_widget (widget);
1805 /*************************************************************
1806 * gtk_drag_find_widget:
1807 * Function used to locate widgets for
1808 * DRAG_MOTION and DROP_START events.
1809 *************************************************************/
1812 gtk_drag_find_widget (GtkWidget *widget,
1813 GdkDragContext *context,
1814 GtkDragDestInfo *info,
1818 GtkDragDestCallback callback)
1820 if (!gtk_widget_get_mapped (widget) ||
1821 !gtk_widget_get_sensitive (widget))
1824 /* Get the widget at the pointer coordinates and travel up
1825 * the widget hierarchy from there.
1827 widget = _gtk_widget_find_at_coords (gtk_widget_get_window (widget),
1835 GList *hierarchy = NULL;
1836 gboolean found = FALSE;
1838 if (!gtk_widget_get_mapped (widget) ||
1839 !gtk_widget_get_sensitive (widget))
1842 /* need to reference the entire hierarchy temporarily in case the
1843 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
1845 for (parent = widget;
1847 parent = gtk_widget_get_parent (parent))
1849 hierarchy = g_list_prepend (hierarchy, g_object_ref (parent));
1852 /* If the current widget is registered as a drop site, check to
1853 * emit "drag-motion" to check if we are actually in a drop
1856 if (g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1858 found = callback (widget, context, x, y, time);
1860 /* If so, send a "drag-leave" to the last widget */
1863 if (info->widget && info->widget != widget)
1865 gtk_drag_dest_leave (info->widget, context, time);
1868 info->widget = widget;
1874 /* Get the parent before unreffing the hierarchy because
1875 * invoking the callback might have destroyed the widget
1877 parent = gtk_widget_get_parent (widget);
1879 /* The parent might be going away when unreffing the
1880 * hierarchy, so also protect againt that
1883 g_object_add_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1886 g_list_foreach (hierarchy, (GFunc) g_object_unref, NULL);
1887 g_list_free (hierarchy);
1893 g_object_remove_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1897 if (!gtk_widget_translate_coordinates (widget, parent, x, y, &x, &y))
1907 gtk_drag_proxy_begin (GtkWidget *widget,
1908 GtkDragDestInfo *dest_info,
1911 GtkDragSourceInfo *source_info;
1913 GdkDragContext *context;
1914 GtkWidget *ipc_widget;
1916 if (dest_info->proxy_source)
1918 gdk_drag_abort (dest_info->proxy_source->context, time);
1919 gtk_drag_source_info_destroy (dest_info->proxy_source);
1920 dest_info->proxy_source = NULL;
1923 ipc_widget = gtk_drag_get_ipc_widget (widget);
1924 context = gdk_drag_begin (gtk_widget_get_window (ipc_widget),
1925 gdk_drag_context_list_targets (dest_info->context));
1927 source_info = gtk_drag_get_source_info (context, TRUE);
1929 source_info->ipc_widget = ipc_widget;
1930 source_info->widget = g_object_ref (widget);
1932 source_info->target_list = gtk_target_list_new (NULL, 0);
1933 tmp_list = gdk_drag_context_list_targets (dest_info->context);
1936 gtk_target_list_add (source_info->target_list,
1937 GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
1938 tmp_list = tmp_list->next;
1941 source_info->proxy_dest = dest_info;
1943 g_signal_connect (ipc_widget,
1945 G_CALLBACK (gtk_drag_selection_get),
1948 dest_info->proxy_source = source_info;
1952 gtk_drag_dest_info_destroy (gpointer data)
1954 g_slice_free (GtkDragDestInfo, data);
1957 static GtkDragDestInfo *
1958 gtk_drag_get_dest_info (GdkDragContext *context,
1961 GtkDragDestInfo *info;
1962 static GQuark info_quark = 0;
1964 info_quark = g_quark_from_static_string ("gtk-dest-info");
1966 info = g_object_get_qdata (G_OBJECT (context), info_quark);
1967 if (!info && create)
1969 info = g_slice_new0 (GtkDragDestInfo);
1970 info->context = context;
1971 g_object_set_qdata_full (G_OBJECT (context), info_quark,
1972 info, gtk_drag_dest_info_destroy);
1978 static GQuark dest_info_quark = 0;
1980 static GtkDragSourceInfo *
1981 gtk_drag_get_source_info (GdkDragContext *context,
1984 GtkDragSourceInfo *info;
1985 if (!dest_info_quark)
1986 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
1988 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
1989 if (!info && create)
1991 info = g_new0 (GtkDragSourceInfo, 1);
1992 info->context = context;
1993 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
2000 gtk_drag_clear_source_info (GdkDragContext *context)
2002 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
2006 gtk_drag_dest_realized (GtkWidget *widget)
2008 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2010 if (gtk_widget_is_toplevel (toplevel))
2011 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2015 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
2016 GtkWidget *previous_toplevel)
2018 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2020 if (gtk_widget_is_toplevel (toplevel) && gtk_widget_get_realized (toplevel))
2021 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2025 gtk_drag_dest_site_destroy (gpointer data)
2027 GtkDragDestSite *site = data;
2029 if (site->proxy_window)
2030 g_object_unref (site->proxy_window);
2032 if (site->target_list)
2033 gtk_target_list_unref (site->target_list);
2035 g_slice_free (GtkDragDestSite, site);
2039 * Default drag handlers
2042 gtk_drag_dest_leave (GtkWidget *widget,
2043 GdkDragContext *context,
2046 GtkDragDestSite *site;
2048 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2049 g_return_if_fail (site != NULL);
2053 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2055 if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
2057 gdk_drag_abort (info->proxy_source->context, time);
2058 gtk_drag_source_info_destroy (info->proxy_source);
2059 info->proxy_source = NULL;
2066 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
2067 gtk_drag_unhighlight (widget);
2069 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
2071 g_signal_emit_by_name (widget, "drag-leave", context, time);
2073 site->have_drag = FALSE;
2078 gtk_drag_dest_motion (GtkWidget *widget,
2079 GdkDragContext *context,
2084 GtkDragDestSite *site;
2085 GdkDragAction action = 0;
2088 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2089 g_return_val_if_fail (site != NULL, FALSE);
2094 GdkEvent *current_event;
2095 GdkWindow *dest_window;
2096 GdkDragProtocol proto;
2098 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2100 if (!info->proxy_source || info->proxy_source->widget != widget)
2101 gtk_drag_proxy_begin (widget, info, time);
2103 current_event = gtk_get_current_event ();
2105 if (site->proxy_window)
2107 dest_window = site->proxy_window;
2108 proto = site->proxy_protocol;
2112 gdk_drag_find_window_for_screen (info->proxy_source->context,
2114 gdk_window_get_screen (current_event->dnd.window),
2115 current_event->dnd.x_root,
2116 current_event->dnd.y_root,
2117 &dest_window, &proto);
2120 gdk_drag_motion (info->proxy_source->context,
2122 current_event->dnd.x_root,
2123 current_event->dnd.y_root,
2124 gdk_drag_context_get_suggested_action (context),
2125 gdk_drag_context_get_actions (context),
2128 if (!site->proxy_window && dest_window)
2129 g_object_unref (dest_window);
2131 selection = gdk_drag_get_selection (info->proxy_source->context);
2133 selection != gdk_drag_get_selection (info->context))
2134 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2136 gdk_event_free (current_event);
2141 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
2143 if (gdk_drag_context_get_suggested_action (context) & site->actions)
2144 action = gdk_drag_context_get_suggested_action (context);
2149 for (i = 0; i < 8; i++)
2151 if ((site->actions & (1 << i)) &&
2152 (gdk_drag_context_get_actions (context) & (1 << i)))
2160 if (action && gtk_drag_dest_find_target (widget, context, NULL))
2162 if (!site->have_drag)
2164 site->have_drag = TRUE;
2165 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
2166 gtk_drag_highlight (widget);
2169 gdk_drag_status (context, action, time);
2173 gdk_drag_status (context, 0, time);
2174 if (!site->track_motion)
2179 g_signal_emit_by_name (widget, "drag-motion",
2180 context, x, y, time, &retval);
2182 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
2186 gtk_drag_dest_drop (GtkWidget *widget,
2187 GdkDragContext *context,
2192 GtkDragDestSite *site;
2193 GtkDragDestInfo *info;
2195 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2196 g_return_val_if_fail (site != NULL, FALSE);
2198 info = gtk_drag_get_dest_info (context, FALSE);
2199 g_return_val_if_fail (info != NULL, FALSE);
2206 if (info->proxy_source ||
2207 (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_ROOTWIN))
2209 gtk_drag_drop (info->proxy_source, time);
2213 /* We need to synthesize a motion event, wait for a status,
2214 * and, if we get a good one, do a drop.
2217 GdkEvent *current_event;
2219 GdkWindow *dest_window;
2220 GdkDragProtocol proto;
2222 gtk_drag_proxy_begin (widget, info, time);
2223 info->proxy_drop_wait = TRUE;
2224 info->proxy_drop_time = time;
2226 current_event = gtk_get_current_event ();
2228 if (site->proxy_window)
2230 dest_window = site->proxy_window;
2231 proto = site->proxy_protocol;
2235 gdk_drag_find_window_for_screen (info->proxy_source->context,
2237 gdk_window_get_screen (current_event->dnd.window),
2238 current_event->dnd.x_root,
2239 current_event->dnd.y_root,
2240 &dest_window, &proto);
2243 gdk_drag_motion (info->proxy_source->context,
2245 current_event->dnd.x_root,
2246 current_event->dnd.y_root,
2247 gdk_drag_context_get_suggested_action (context),
2248 gdk_drag_context_get_actions (context),
2251 if (!site->proxy_window && dest_window)
2252 g_object_unref (dest_window);
2254 selection = gdk_drag_get_selection (info->proxy_source->context);
2256 selection != gdk_drag_get_selection (info->context))
2257 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2259 gdk_event_free (current_event);
2268 if (site->flags & GTK_DEST_DEFAULT_DROP)
2270 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2272 if (target == GDK_NONE)
2274 gtk_drag_finish (context, FALSE, FALSE, time);
2278 gtk_drag_get_data (widget, context, target, time);
2281 g_signal_emit_by_name (widget, "drag-drop",
2282 context, x, y, time, &retval);
2284 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2292 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2293 * so that we can set the icon from the source site information
2295 static GdkDragContext *
2296 gtk_drag_begin_internal (GtkWidget *widget,
2297 GtkDragSourceSite *site,
2298 GtkTargetList *target_list,
2299 GdkDragAction actions,
2303 GtkDragSourceInfo *info;
2304 GList *targets = NULL;
2306 guint32 time = GDK_CURRENT_TIME;
2307 GdkDragAction possible_actions, suggested_action;
2308 GdkDragContext *context;
2309 GtkWidget *ipc_widget;
2311 GdkDevice *pointer, *keyboard;
2312 GdkWindow *ipc_window;
2314 pointer = keyboard = NULL;
2315 ipc_widget = gtk_drag_get_ipc_widget (widget);
2317 gtk_drag_get_event_actions (event, button, actions,
2318 &suggested_action, &possible_actions);
2320 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2326 time = gdk_event_get_time (event);
2327 if (time == GDK_CURRENT_TIME)
2328 time = gtk_get_current_event_time ();
2330 pointer = gdk_event_get_device (event);
2332 if (gdk_device_get_source (pointer) == GDK_SOURCE_KEYBOARD)
2335 pointer = gdk_device_get_associated_device (keyboard);
2338 keyboard = gdk_device_get_associated_device (pointer);
2342 GdkDeviceManager *device_manager;
2344 device_manager = gdk_display_get_device_manager (gtk_widget_get_display (widget));
2345 pointer = gdk_device_manager_get_client_pointer (device_manager);
2346 keyboard = gdk_device_get_associated_device (pointer);
2352 ipc_window = gtk_widget_get_window (ipc_widget);
2354 if (gdk_device_grab (pointer, ipc_window,
2355 GDK_OWNERSHIP_APPLICATION, FALSE,
2356 GDK_POINTER_MOTION_MASK |
2357 GDK_BUTTON_RELEASE_MASK,
2358 cursor, time) != GDK_GRAB_SUCCESS)
2360 gtk_drag_release_ipc_widget (ipc_widget);
2365 grab_dnd_keys (ipc_widget, keyboard, time);
2367 /* We use a GTK grab here to override any grabs that the widget
2368 * we are dragging from might have held
2370 gtk_device_grab_add (ipc_widget, pointer, FALSE);
2372 tmp_list = g_list_last (target_list->list);
2375 GtkTargetPair *pair = tmp_list->data;
2376 targets = g_list_prepend (targets,
2377 GINT_TO_POINTER (pair->target));
2378 tmp_list = tmp_list->prev;
2381 source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2383 context = gdk_drag_begin (ipc_window, targets);
2384 gdk_drag_context_set_device (context, pointer);
2385 g_list_free (targets);
2387 info = gtk_drag_get_source_info (context, TRUE);
2389 info->ipc_widget = ipc_widget;
2390 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), info);
2392 info->widget = g_object_ref (widget);
2394 info->button = button;
2395 info->cursor = cursor;
2396 info->target_list = target_list;
2397 gtk_target_list_ref (target_list);
2399 info->possible_actions = actions;
2401 info->status = GTK_DRAG_STATUS_DRAG;
2402 info->last_event = NULL;
2403 info->selections = NULL;
2404 info->icon_window = NULL;
2405 info->destroy_icon = FALSE;
2407 /* Set cur_x, cur_y here so if the "drag-begin" signal shows
2408 * the drag icon, it will be in the right place
2410 if (event && event->type == GDK_MOTION_NOTIFY)
2412 info->cur_screen = gtk_widget_get_screen (widget);
2413 info->cur_x = event->motion.x_root;
2414 info->cur_y = event->motion.y_root;
2418 gdk_display_get_device_state (gtk_widget_get_display (widget), pointer,
2419 &info->cur_screen, &info->cur_x, &info->cur_y, NULL);
2422 g_signal_emit_by_name (widget, "drag-begin", info->context);
2424 /* Ensure that we have an icon before we start the drag; the
2425 * application may have set one in ::drag_begin, or it may
2428 if (!info->icon_window && !info->icon_pixbuf)
2430 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2431 gtk_drag_set_icon_default (context);
2433 switch (site->icon_type)
2435 case GTK_IMAGE_PIXBUF:
2436 gtk_drag_set_icon_pixbuf (context,
2437 site->icon_data.pixbuf.pixbuf,
2440 case GTK_IMAGE_STOCK:
2441 gtk_drag_set_icon_stock (context,
2442 site->icon_data.stock.stock_id,
2445 case GTK_IMAGE_ICON_NAME:
2446 gtk_drag_set_icon_name (context,
2447 site->icon_data.name.icon_name,
2450 case GTK_IMAGE_EMPTY:
2452 g_assert_not_reached();
2457 /* We need to composite the icon into the cursor, if we are
2458 * not using an icon window.
2460 if (info->icon_pixbuf)
2462 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2466 if (cursor != info->cursor)
2468 gdk_device_grab (pointer, gtk_widget_get_window (widget),
2469 GDK_OWNERSHIP_APPLICATION, FALSE,
2470 GDK_POINTER_MOTION_MASK |
2471 GDK_BUTTON_RELEASE_MASK,
2473 info->cursor = cursor;
2477 if (event && event->type == GDK_MOTION_NOTIFY)
2478 gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2480 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, event);
2482 info->start_x = info->cur_x;
2483 info->start_y = info->cur_y;
2485 g_signal_connect (info->ipc_widget, "grab-broken-event",
2486 G_CALLBACK (gtk_drag_grab_broken_event_cb), info);
2487 g_signal_connect (info->ipc_widget, "grab-notify",
2488 G_CALLBACK (gtk_drag_grab_notify_cb), info);
2489 g_signal_connect (info->ipc_widget, "button-release-event",
2490 G_CALLBACK (gtk_drag_button_release_cb), info);
2491 g_signal_connect (info->ipc_widget, "motion-notify-event",
2492 G_CALLBACK (gtk_drag_motion_cb), info);
2493 g_signal_connect (info->ipc_widget, "key-press-event",
2494 G_CALLBACK (gtk_drag_key_cb), info);
2495 g_signal_connect (info->ipc_widget, "key-release-event",
2496 G_CALLBACK (gtk_drag_key_cb), info);
2497 g_signal_connect (info->ipc_widget, "selection-get",
2498 G_CALLBACK (gtk_drag_selection_get), info);
2500 info->have_grab = TRUE;
2501 info->grab_time = time;
2503 return info->context;
2508 * @widget: the source widget.
2509 * @targets: The targets (data formats) in which the
2510 * source can provide the data.
2511 * @actions: A bitmask of the allowed drag actions for this drag.
2512 * @button: The button the user clicked to start the drag.
2513 * @event: The event that triggered the start of the drag.
2515 * Initiates a drag on the source side. The function
2516 * only needs to be used when the application is
2517 * starting drags itself, and is not needed when
2518 * gtk_drag_source_set() is used.
2520 * The @event is used to retrieve the timestamp that will be used internally to
2521 * grab the pointer. If @event is #NULL, then GDK_CURRENT_TIME will be used.
2522 * However, you should try to pass a real event in all cases, since that can be
2523 * used by GTK+ to get information about the start position of the drag, for
2524 * example if the @event is a GDK_MOTION_NOTIFY.
2526 * Generally there are three cases when you want to start a drag by hand by
2527 * calling this function:
2529 * 1. During a button-press-event handler, if you want to start a drag
2530 * immediately when the user presses the mouse button. Pass the @event
2531 * that you have in your button-press-event handler.
2533 * 2. During a motion-notify-event handler, if you want to start a drag
2534 * when the mouse moves past a certain threshold distance after a button-press.
2535 * Pass the @event that you have in your motion-notify-event handler.
2537 * 3. During a timeout handler, if you want to start a drag after the mouse
2538 * button is held down for some time. Try to save the last event that you got
2539 * from the mouse, using gdk_event_copy(), and pass it to this function
2540 * (remember to free the event with gdk_event_free() when you are done).
2541 * If you can really not pass a real event, pass #NULL instead.
2543 * Return value: the context for this drag.
2546 gtk_drag_begin (GtkWidget *widget,
2547 GtkTargetList *targets,
2548 GdkDragAction actions,
2552 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2553 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
2554 g_return_val_if_fail (targets != NULL, NULL);
2556 return gtk_drag_begin_internal (widget, NULL, targets,
2557 actions, button, event);
2561 * gtk_drag_source_set:
2562 * @widget: a #GtkWidget
2563 * @start_button_mask: the bitmask of buttons that can start the drag
2564 * @targets: (allow-none) (array length=n_targets): the table of targets that the drag will support,
2566 * @n_targets: the number of items in @targets
2567 * @actions: the bitmask of possible actions for a drag from this widget
2569 * Sets up a widget so that GTK+ will start a drag operation when the user
2570 * clicks and drags on the widget. The widget must have a window.
2573 gtk_drag_source_set (GtkWidget *widget,
2574 GdkModifierType start_button_mask,
2575 const GtkTargetEntry *targets,
2577 GdkDragAction actions)
2579 GtkDragSourceSite *site;
2581 g_return_if_fail (GTK_IS_WIDGET (widget));
2583 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2585 gtk_widget_add_events (widget,
2586 gtk_widget_get_events (widget) |
2587 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2588 GDK_BUTTON_MOTION_MASK);
2592 if (site->target_list)
2593 gtk_target_list_unref (site->target_list);
2597 site = g_slice_new0 (GtkDragSourceSite);
2599 site->icon_type = GTK_IMAGE_EMPTY;
2601 g_signal_connect (widget, "button-press-event",
2602 G_CALLBACK (gtk_drag_source_event_cb),
2604 g_signal_connect (widget, "button-release-event",
2605 G_CALLBACK (gtk_drag_source_event_cb),
2607 g_signal_connect (widget, "motion-notify-event",
2608 G_CALLBACK (gtk_drag_source_event_cb),
2611 g_object_set_data_full (G_OBJECT (widget),
2612 I_("gtk-site-data"),
2613 site, gtk_drag_source_site_destroy);
2616 site->start_button_mask = start_button_mask;
2618 site->target_list = gtk_target_list_new (targets, n_targets);
2620 site->actions = actions;
2623 /*************************************************************
2624 * gtk_drag_source_unset
2625 * Unregister this widget as a drag source.
2629 *************************************************************/
2632 gtk_drag_source_unset (GtkWidget *widget)
2634 GtkDragSourceSite *site;
2636 g_return_if_fail (GTK_IS_WIDGET (widget));
2638 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2642 g_signal_handlers_disconnect_by_func (widget,
2643 gtk_drag_source_event_cb,
2645 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
2650 * gtk_drag_source_get_target_list:
2651 * @widget: a #GtkWidget
2653 * Gets the list of targets this widget can provide for
2656 * Return value: the #GtkTargetList, or %NULL if none
2661 gtk_drag_source_get_target_list (GtkWidget *widget)
2663 GtkDragSourceSite *site;
2665 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2667 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2669 return site ? site->target_list : NULL;
2673 * gtk_drag_source_set_target_list:
2674 * @widget: a #GtkWidget that's a drag source
2675 * @target_list: (allow-none): list of draggable targets, or %NULL for none
2677 * Changes the target types that this widget offers for drag-and-drop.
2678 * The widget must first be made into a drag source with
2679 * gtk_drag_source_set().
2684 gtk_drag_source_set_target_list (GtkWidget *widget,
2685 GtkTargetList *target_list)
2687 GtkDragSourceSite *site;
2689 g_return_if_fail (GTK_IS_WIDGET (widget));
2691 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2694 g_warning ("gtk_drag_source_set_target_list() requires the widget "
2695 "to already be a drag source.");
2700 gtk_target_list_ref (target_list);
2702 if (site->target_list)
2703 gtk_target_list_unref (site->target_list);
2705 site->target_list = target_list;
2709 * gtk_drag_source_add_text_targets:
2710 * @widget: a #GtkWidget that's is a drag source
2712 * Add the text targets supported by #GtkSelection to
2713 * the target list of the drag source. The targets
2714 * are added with @info = 0. If you need another value,
2715 * use gtk_target_list_add_text_targets() and
2716 * gtk_drag_source_set_target_list().
2721 gtk_drag_source_add_text_targets (GtkWidget *widget)
2723 GtkTargetList *target_list;
2725 target_list = gtk_drag_source_get_target_list (widget);
2727 gtk_target_list_ref (target_list);
2729 target_list = gtk_target_list_new (NULL, 0);
2730 gtk_target_list_add_text_targets (target_list, 0);
2731 gtk_drag_source_set_target_list (widget, target_list);
2732 gtk_target_list_unref (target_list);
2736 * gtk_drag_source_add_image_targets:
2737 * @widget: a #GtkWidget that's is a drag source
2739 * Add the writable image targets supported by #GtkSelection to
2740 * the target list of the drag source. The targets
2741 * are added with @info = 0. If you need another value,
2742 * use gtk_target_list_add_image_targets() and
2743 * gtk_drag_source_set_target_list().
2748 gtk_drag_source_add_image_targets (GtkWidget *widget)
2750 GtkTargetList *target_list;
2752 target_list = gtk_drag_source_get_target_list (widget);
2754 gtk_target_list_ref (target_list);
2756 target_list = gtk_target_list_new (NULL, 0);
2757 gtk_target_list_add_image_targets (target_list, 0, TRUE);
2758 gtk_drag_source_set_target_list (widget, target_list);
2759 gtk_target_list_unref (target_list);
2763 * gtk_drag_source_add_uri_targets:
2764 * @widget: a #GtkWidget that's is a drag source
2766 * Add the URI targets supported by #GtkSelection to
2767 * the target list of the drag source. The targets
2768 * are added with @info = 0. If you need another value,
2769 * use gtk_target_list_add_uri_targets() and
2770 * gtk_drag_source_set_target_list().
2775 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2777 GtkTargetList *target_list;
2779 target_list = gtk_drag_source_get_target_list (widget);
2781 gtk_target_list_ref (target_list);
2783 target_list = gtk_target_list_new (NULL, 0);
2784 gtk_target_list_add_uri_targets (target_list, 0);
2785 gtk_drag_source_set_target_list (widget, target_list);
2786 gtk_target_list_unref (target_list);
2790 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2792 switch (site->icon_type)
2794 case GTK_IMAGE_EMPTY:
2796 case GTK_IMAGE_PIXBUF:
2797 g_object_unref (site->icon_data.pixbuf.pixbuf);
2799 case GTK_IMAGE_STOCK:
2800 g_free (site->icon_data.stock.stock_id);
2802 case GTK_IMAGE_ICON_NAME:
2803 g_free (site->icon_data.name.icon_name);
2806 g_assert_not_reached();
2809 site->icon_type = GTK_IMAGE_EMPTY;
2813 * gtk_drag_source_set_icon_pixbuf:
2814 * @widget: a #GtkWidget
2815 * @pixbuf: the #GdkPixbuf for the drag icon
2817 * Sets the icon that will be used for drags from a particular widget
2818 * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
2819 * release it when it is no longer needed.
2822 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
2825 GtkDragSourceSite *site;
2827 g_return_if_fail (GTK_IS_WIDGET (widget));
2828 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2830 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2831 g_return_if_fail (site != NULL);
2832 g_object_ref (pixbuf);
2834 gtk_drag_source_unset_icon (site);
2836 site->icon_type = GTK_IMAGE_PIXBUF;
2837 site->icon_data.pixbuf.pixbuf = pixbuf;
2841 * gtk_drag_source_set_icon_stock:
2842 * @widget: a #GtkWidget
2843 * @stock_id: the ID of the stock icon to use
2845 * Sets the icon that will be used for drags from a particular source
2849 gtk_drag_source_set_icon_stock (GtkWidget *widget,
2850 const gchar *stock_id)
2852 GtkDragSourceSite *site;
2854 g_return_if_fail (GTK_IS_WIDGET (widget));
2855 g_return_if_fail (stock_id != NULL);
2857 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2858 g_return_if_fail (site != NULL);
2860 gtk_drag_source_unset_icon (site);
2862 site->icon_type = GTK_IMAGE_STOCK;
2863 site->icon_data.stock.stock_id = g_strdup (stock_id);
2867 * gtk_drag_source_set_icon_name:
2868 * @widget: a #GtkWidget
2869 * @icon_name: name of icon to use
2871 * Sets the icon that will be used for drags from a particular source
2872 * to a themed icon. See the docs for #GtkIconTheme for more details.
2877 gtk_drag_source_set_icon_name (GtkWidget *widget,
2878 const gchar *icon_name)
2880 GtkDragSourceSite *site;
2882 g_return_if_fail (GTK_IS_WIDGET (widget));
2883 g_return_if_fail (icon_name != NULL);
2885 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2886 g_return_if_fail (site != NULL);
2888 gtk_drag_source_unset_icon (site);
2890 site->icon_type = GTK_IMAGE_ICON_NAME;
2891 site->icon_data.name.icon_name = g_strdup (icon_name);
2895 gtk_drag_get_icon (GtkDragSourceInfo *info,
2896 GtkWidget **icon_window,
2900 if (get_can_change_screen (info->icon_window))
2901 gtk_window_set_screen (GTK_WINDOW (info->icon_window),
2904 if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
2906 if (!info->fallback_icon)
2908 gint save_hot_x, save_hot_y;
2909 gboolean save_destroy_icon;
2910 GtkWidget *save_icon_window;
2912 /* HACK to get the appropriate icon
2914 save_icon_window = info->icon_window;
2915 save_hot_x = info->hot_x;
2916 save_hot_y = info->hot_x;
2917 save_destroy_icon = info->destroy_icon;
2919 info->icon_window = NULL;
2920 set_icon_stock_pixbuf (info->context,
2921 GTK_STOCK_DND, NULL, -2, -2, TRUE);
2922 info->fallback_icon = info->icon_window;
2924 info->icon_window = save_icon_window;
2925 info->hot_x = save_hot_x;
2926 info->hot_y = save_hot_y;
2927 info->destroy_icon = save_destroy_icon;
2930 gtk_widget_hide (info->icon_window);
2932 *icon_window = info->fallback_icon;
2933 gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
2940 if (info->fallback_icon)
2941 gtk_widget_hide (info->fallback_icon);
2943 *icon_window = info->icon_window;
2944 *hot_x = info->hot_x;
2945 *hot_y = info->hot_y;
2950 gtk_drag_update_icon (GtkDragSourceInfo *info)
2952 if (info->icon_window)
2954 GtkWidget *icon_window;
2957 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
2959 gtk_window_move (GTK_WINDOW (icon_window),
2960 info->cur_x - hot_x,
2961 info->cur_y - hot_y);
2963 if (gtk_widget_get_visible (icon_window))
2964 gdk_window_raise (gtk_widget_get_window (icon_window));
2966 gtk_widget_show (icon_window);
2971 gtk_drag_set_icon_window (GdkDragContext *context,
2975 gboolean destroy_on_release)
2977 GtkDragSourceInfo *info;
2979 info = gtk_drag_get_source_info (context, FALSE);
2982 if (destroy_on_release)
2983 gtk_widget_destroy (widget);
2987 gtk_drag_remove_icon (info);
2990 g_object_ref (widget);
2992 info->icon_window = widget;
2993 info->hot_x = hot_x;
2994 info->hot_y = hot_y;
2995 info->destroy_icon = destroy_on_release;
2997 if (widget && info->icon_pixbuf)
2999 g_object_unref (info->icon_pixbuf);
3000 info->icon_pixbuf = NULL;
3003 gtk_drag_update_cursor (info);
3004 gtk_drag_update_icon (info);
3008 * gtk_drag_set_icon_widget:
3009 * @context: the context for a drag. (This must be called
3010 with a context for the source side of a drag)
3011 * @widget: a toplevel window to use as an icon.
3012 * @hot_x: the X offset within @widget of the hotspot.
3013 * @hot_y: the Y offset within @widget of the hotspot.
3015 * Changes the icon for a widget to a given widget. GTK+
3016 * will not destroy the icon, so if you don't want
3017 * it to persist, you should connect to the "drag-end"
3018 * signal and destroy it yourself.
3021 gtk_drag_set_icon_widget (GdkDragContext *context,
3026 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3027 g_return_if_fail (GTK_IS_WIDGET (widget));
3029 gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
3033 icon_window_realize (GtkWidget *window,
3036 cairo_surface_t *surface;
3037 cairo_pattern_t *pattern;
3040 surface = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3041 CAIRO_CONTENT_COLOR,
3042 gdk_pixbuf_get_width (pixbuf),
3043 gdk_pixbuf_get_height (pixbuf));
3045 cr = cairo_create (surface);
3046 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3047 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3049 cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3051 cairo_pop_group_to_source (cr);
3055 pattern = cairo_pattern_create_for_surface (surface);
3056 gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3057 cairo_pattern_destroy (pattern);
3059 cairo_surface_destroy (surface);
3061 if (gdk_pixbuf_get_has_alpha (pixbuf))
3063 cairo_region_t *region;
3065 surface = cairo_image_surface_create (CAIRO_FORMAT_A1,
3066 gdk_pixbuf_get_width (pixbuf),
3067 gdk_pixbuf_get_height (pixbuf));
3069 cr = cairo_create (surface);
3070 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3074 region = gdk_cairo_region_create_from_surface (surface);
3075 gtk_widget_shape_combine_region (window, region);
3076 cairo_region_destroy (region);
3078 cairo_surface_destroy (surface);
3083 set_icon_stock_pixbuf (GdkDragContext *context,
3084 const gchar *stock_id,
3088 gboolean force_window)
3093 GdkDisplay *display;
3095 g_return_if_fail (context != NULL);
3096 g_return_if_fail (pixbuf != NULL || stock_id != NULL);
3097 g_return_if_fail (pixbuf == NULL || stock_id == NULL);
3099 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3101 window = gtk_window_new (GTK_WINDOW_POPUP);
3102 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3103 gtk_window_set_screen (GTK_WINDOW (window), screen);
3104 set_can_change_screen (window, TRUE);
3106 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3107 gtk_widget_set_app_paintable (window, TRUE);
3111 pixbuf = gtk_widget_render_icon_pixbuf (window, stock_id,
3116 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
3117 gtk_widget_destroy (window);
3123 g_object_ref (pixbuf);
3125 display = gdk_window_get_display (gdk_drag_context_get_source_window (context));
3126 width = gdk_pixbuf_get_width (pixbuf);
3127 height = gdk_pixbuf_get_height (pixbuf);
3129 if (!force_window &&
3130 gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
3132 GtkDragSourceInfo *info;
3134 gtk_widget_destroy (window);
3136 info = gtk_drag_get_source_info (context, FALSE);
3138 if (info->icon_pixbuf)
3139 g_object_unref (info->icon_pixbuf);
3140 info->icon_pixbuf = pixbuf;
3142 gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
3146 gtk_widget_set_size_request (window, width, height);
3148 g_signal_connect_closure (window, "realize",
3149 g_cclosure_new (G_CALLBACK (icon_window_realize),
3151 (GClosureNotify)g_object_unref),
3154 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3159 * gtk_drag_set_icon_pixbuf:
3160 * @context: the context for a drag. (This must be called
3161 * with a context for the source side of a drag)
3162 * @pixbuf: the #GdkPixbuf to use as the drag icon.
3163 * @hot_x: the X offset within @widget of the hotspot.
3164 * @hot_y: the Y offset within @widget of the hotspot.
3166 * Sets @pixbuf as the icon for a given drag.
3169 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
3174 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3175 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3177 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3181 * gtk_drag_set_icon_stock:
3182 * @context: the context for a drag. (This must be called
3183 * with a context for the source side of a drag)
3184 * @stock_id: the ID of the stock icon to use for the drag.
3185 * @hot_x: the X offset within the icon of the hotspot.
3186 * @hot_y: the Y offset within the icon of the hotspot.
3188 * Sets the icon for a given drag from a stock ID.
3191 gtk_drag_set_icon_stock (GdkDragContext *context,
3192 const gchar *stock_id,
3196 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3197 g_return_if_fail (stock_id != NULL);
3199 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
3202 /* XXX: This function is in gdk, too. Should it be in Cairo? */
3204 _gtk_cairo_surface_extents (cairo_surface_t *surface,
3205 GdkRectangle *extents)
3207 double x1, x2, y1, y2;
3210 g_return_val_if_fail (surface != NULL, FALSE);
3211 g_return_val_if_fail (extents != NULL, FALSE);
3213 cr = cairo_create (surface);
3214 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
3223 if (x1 < G_MININT || x1 > G_MAXINT ||
3224 y1 < G_MININT || y1 > G_MAXINT ||
3225 x2 > G_MAXINT || y2 > G_MAXINT)
3227 extents->x = extents->y = extents->width = extents->height = 0;
3233 extents->width = x2;
3234 extents->height = y2;
3240 * gtk_drag_set_icon_surface:
3241 * @context: the context for a drag. (This must be called
3242 * with a context for the source side of a drag)
3243 * @surface: the surface to use as icon
3245 * Sets @surface as the icon for a given drag. GTK+ retains
3246 * references for the arguments, and will release them when
3247 * they are no longer needed.
3249 * To position the surface relative to the mouse, use
3250 * cairo_surface_set_device_offset() on @surface. The mouse
3251 * cursor will be positioned at the (0,0) coordinate of the
3255 gtk_drag_set_icon_surface (GdkDragContext *context,
3256 cairo_surface_t *surface)
3260 GdkRectangle extents;
3261 cairo_pattern_t *pattern;
3263 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3264 g_return_if_fail (surface != NULL);
3266 _gtk_cairo_surface_extents (surface, &extents);
3269 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3271 window = gtk_window_new (GTK_WINDOW_POPUP);
3272 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3273 gtk_window_set_screen (GTK_WINDOW (window), screen);
3274 set_can_change_screen (window, TRUE);
3276 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3277 gtk_widget_set_app_paintable (window, TRUE);
3279 gtk_widget_set_size_request (window, extents.width, extents.height);
3280 gtk_widget_realize (window);
3282 if (cairo_surface_get_content (surface) != CAIRO_CONTENT_COLOR)
3284 cairo_surface_t *saturated;
3285 cairo_region_t *region;
3288 region = gdk_cairo_region_create_from_surface (surface);
3289 cairo_region_translate (region, -extents.x, -extents.y);
3291 gtk_widget_shape_combine_region (window, region);
3292 cairo_region_destroy (region);
3294 /* Need to saturate the colors, so it doesn't look like semi-transparent
3295 * pixels were painted on black. */
3296 saturated = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3297 CAIRO_CONTENT_COLOR,
3301 cr = cairo_create (saturated);
3302 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3303 cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
3305 cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3307 cairo_pop_group_to_source (cr);
3311 pattern = cairo_pattern_create_for_surface (saturated);
3313 cairo_surface_destroy (saturated);
3317 cairo_matrix_t matrix;
3319 pattern = cairo_pattern_create_for_surface (surface);
3320 cairo_matrix_init_translate (&matrix, extents.x, extents.y);
3321 cairo_pattern_set_matrix (pattern, &matrix);
3324 gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3326 gtk_drag_set_icon_window (context, window, extents.x, extents.y, TRUE);
3330 * gtk_drag_set_icon_name:
3331 * @context: the context for a drag. (This must be called
3332 * with a context for the source side of a drag)
3333 * @icon_name: name of icon to use
3334 * @hot_x: the X offset of the hotspot within the icon
3335 * @hot_y: the Y offset of the hotspot within the icon
3337 * Sets the icon for a given drag from a named themed icon. See
3338 * the docs for #GtkIconTheme for more details. Note that the
3339 * size of the icon depends on the icon theme (the icon is
3340 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
3341 * @hot_x and @hot_y have to be used with care.
3346 gtk_drag_set_icon_name (GdkDragContext *context,
3347 const gchar *icon_name,
3352 GtkSettings *settings;
3353 GtkIconTheme *icon_theme;
3355 gint width, height, icon_size;
3357 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3358 g_return_if_fail (icon_name != NULL);
3360 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3361 g_return_if_fail (screen != NULL);
3363 settings = gtk_settings_get_for_screen (screen);
3364 if (gtk_icon_size_lookup_for_settings (settings,
3367 icon_size = MAX (width, height);
3369 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
3371 icon_theme = gtk_icon_theme_get_for_screen (screen);
3373 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
3374 icon_size, 0, NULL);
3376 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3378 g_warning ("Cannot load drag icon from icon name %s", icon_name);
3382 * gtk_drag_set_icon_default:
3383 * @context: the context for a drag. (This must be called
3384 with a context for the source side of a drag)
3386 * Sets the icon for a particular drag to the default
3390 gtk_drag_set_icon_default (GdkDragContext *context)
3392 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3394 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
3397 /*************************************************************
3398 * _gtk_drag_source_handle_event:
3399 * Called from widget event handling code on Drag events
3403 * toplevel: Toplevel widget that received the event
3406 *************************************************************/
3409 _gtk_drag_source_handle_event (GtkWidget *widget,
3412 GtkDragSourceInfo *info;
3413 GdkDragContext *context;
3415 g_return_if_fail (widget != NULL);
3416 g_return_if_fail (event != NULL);
3418 context = event->dnd.context;
3419 info = gtk_drag_get_source_info (context, FALSE);
3423 switch (event->type)
3425 case GDK_DRAG_STATUS:
3429 if (info->proxy_dest)
3431 if (!event->dnd.send_event)
3433 if (info->proxy_dest->proxy_drop_wait)
3435 gboolean result = gdk_drag_context_get_selected_action (context) != 0;
3437 /* Aha - we can finally pass the MOTIF DROP on... */
3438 gdk_drop_reply (info->proxy_dest->context, result, info->proxy_dest->proxy_drop_time);
3440 gdk_drag_drop (info->context, info->proxy_dest->proxy_drop_time);
3442 gtk_drag_finish (info->proxy_dest->context, FALSE, FALSE, info->proxy_dest->proxy_drop_time);
3446 gdk_drag_status (info->proxy_dest->context,
3447 gdk_drag_context_get_selected_action (event->dnd.context),
3452 else if (info->have_grab)
3454 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
3455 gdk_drag_context_get_selected_action (event->dnd.context),
3457 if (info->cursor != cursor)
3461 pointer = gdk_drag_context_get_device (context);
3462 gdk_device_grab (pointer, gtk_widget_get_window (widget),
3463 GDK_OWNERSHIP_APPLICATION, FALSE,
3464 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3465 cursor, info->grab_time);
3466 info->cursor = cursor;
3469 gtk_drag_add_update_idle (info);
3474 case GDK_DROP_FINISHED:
3475 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, event->dnd.time);
3478 g_assert_not_reached ();
3482 /*************************************************************
3483 * gtk_drag_source_check_selection:
3484 * Check if we've set up handlers/claimed the selection
3485 * for a given drag. If not, add them.
3489 *************************************************************/
3492 gtk_drag_source_check_selection (GtkDragSourceInfo *info,
3498 tmp_list = info->selections;
3501 if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3503 tmp_list = tmp_list->next;
3506 gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3510 info->selections = g_list_prepend (info->selections,
3511 GUINT_TO_POINTER (selection));
3513 tmp_list = info->target_list->list;
3516 GtkTargetPair *pair = tmp_list->data;
3518 gtk_selection_add_target (info->ipc_widget,
3522 tmp_list = tmp_list->next;
3525 if (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_MOTIF)
3527 gtk_selection_add_target (info->ipc_widget,
3529 gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS"),
3530 TARGET_MOTIF_SUCCESS);
3531 gtk_selection_add_target (info->ipc_widget,
3533 gdk_atom_intern_static_string ("XmTRANSFER_FAILURE"),
3534 TARGET_MOTIF_FAILURE);
3537 gtk_selection_add_target (info->ipc_widget,
3539 gdk_atom_intern_static_string ("DELETE"),
3543 /*************************************************************
3544 * gtk_drag_drop_finished:
3545 * Clean up from the drag, and display snapback, if necessary.
3551 *************************************************************/
3554 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3555 GtkDragResult result,
3560 success = (result == GTK_DRAG_RESULT_SUCCESS);
3561 gtk_drag_source_release_selections (info, time);
3563 if (info->proxy_dest)
3565 /* The time from the event isn't reliable for Xdnd drags */
3566 gtk_drag_finish (info->proxy_dest->context, success, FALSE,
3567 info->proxy_dest->proxy_drop_time);
3568 gtk_drag_source_info_destroy (info);
3573 g_signal_emit_by_name (info->widget, "drag-failed",
3574 info->context, result, &success);
3578 gtk_drag_source_info_destroy (info);
3582 GtkDragAnim *anim = g_slice_new0 (GtkDragAnim);
3586 anim->n_steps = MAX (info->cur_x - info->start_x,
3587 info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3588 anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3590 info->cur_screen = gtk_widget_get_screen (info->widget);
3592 if (!info->icon_window)
3593 set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf,
3596 gtk_drag_update_icon (info);
3598 /* Mark the context as dead, so if the destination decides
3599 * to respond really late, we still are OK.
3601 gtk_drag_clear_source_info (info->context);
3602 gdk_threads_add_timeout (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3608 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3611 GdkDisplay *display = gtk_widget_get_display (info->widget);
3612 GList *tmp_list = info->selections;
3616 GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3617 if (gdk_selection_owner_get_for_display (display, selection) == gtk_widget_get_window (info->ipc_widget))
3618 gtk_selection_owner_set_for_display (display, NULL, selection, time);
3620 tmp_list = tmp_list->next;
3623 g_list_free (info->selections);
3624 info->selections = NULL;
3627 /*************************************************************
3629 * Send a drop event.
3633 *************************************************************/
3636 gtk_drag_drop (GtkDragSourceInfo *info,
3639 if (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_ROOTWIN)
3641 GtkSelectionData selection_data;
3643 /* GTK+ traditionally has used application/x-rootwin-drop, but the
3644 * XDND spec specifies x-rootwindow-drop.
3646 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3647 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3649 tmp_list = info->target_list->list;
3652 GtkTargetPair *pair = tmp_list->data;
3654 if (pair->target == target1 || pair->target == target2)
3656 selection_data.selection = GDK_NONE;
3657 selection_data.target = pair->target;
3658 selection_data.data = NULL;
3659 selection_data.length = -1;
3661 g_signal_emit_by_name (info->widget, "drag-data-get",
3662 info->context, &selection_data,
3666 /* FIXME: Should we check for length >= 0 here? */
3667 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3670 tmp_list = tmp_list->next;
3672 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3676 if (info->icon_window)
3677 gtk_widget_hide (info->icon_window);
3679 gdk_drag_drop (info->context, time);
3680 info->drop_timeout = gdk_threads_add_timeout (DROP_ABORT_TIME,
3681 gtk_drag_abort_timeout,
3687 * Source side callbacks.
3691 gtk_drag_source_event_cb (GtkWidget *widget,
3695 GtkDragSourceSite *site;
3696 gboolean retval = FALSE;
3697 site = (GtkDragSourceSite *)data;
3699 switch (event->type)
3701 case GDK_BUTTON_PRESS:
3702 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3704 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3705 site->x = event->button.x;
3706 site->y = event->button.y;
3710 case GDK_BUTTON_RELEASE:
3711 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3712 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3715 case GDK_MOTION_NOTIFY:
3716 if (site->state & event->motion.state & site->start_button_mask)
3718 /* FIXME: This is really broken and can leave us
3724 if (site->state & event->motion.state &
3725 GDK_BUTTON1_MASK << (i - 1))
3729 if (gtk_drag_check_threshold (widget, site->x, site->y,
3730 event->motion.x, event->motion.y))
3733 gtk_drag_begin_internal (widget, site, site->target_list,
3742 default: /* hit for 2/3BUTTON_PRESS */
3750 gtk_drag_source_site_destroy (gpointer data)
3752 GtkDragSourceSite *site = data;
3754 if (site->target_list)
3755 gtk_target_list_unref (site->target_list);
3757 gtk_drag_source_unset_icon (site);
3758 g_slice_free (GtkDragSourceSite, site);
3762 gtk_drag_selection_get (GtkWidget *widget,
3763 GtkSelectionData *selection_data,
3768 GtkDragSourceInfo *info = data;
3769 static GdkAtom null_atom = GDK_NONE;
3773 null_atom = gdk_atom_intern_static_string ("NULL");
3778 g_signal_emit_by_name (info->widget,
3781 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3783 case TARGET_MOTIF_SUCCESS:
3784 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3785 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3787 case TARGET_MOTIF_FAILURE:
3788 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3789 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3792 if (info->proxy_dest)
3794 /* This is sort of dangerous and needs to be thought
3797 info->proxy_dest->proxy_data = selection_data;
3798 gtk_drag_get_data (info->widget,
3799 info->proxy_dest->context,
3800 gtk_selection_data_get_target (selection_data),
3803 info->proxy_dest->proxy_data = NULL;
3807 if (gtk_target_list_find (info->target_list,
3808 gtk_selection_data_get_target (selection_data),
3811 g_signal_emit_by_name (info->widget, "drag-data-get",
3823 gtk_drag_anim_timeout (gpointer data)
3826 GtkDragSourceInfo *info;
3833 if (anim->step == anim->n_steps)
3835 gtk_drag_source_info_destroy (anim->info);
3836 g_slice_free (GtkDragAnim, anim);
3842 x = (info->start_x * (anim->step + 1) +
3843 info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3844 y = (info->start_y * (anim->step + 1) +
3845 info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3846 if (info->icon_window)
3848 GtkWidget *icon_window;
3851 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
3852 gtk_window_move (GTK_WINDOW (icon_window),
3866 gtk_drag_remove_icon (GtkDragSourceInfo *info)
3868 if (info->icon_window)
3870 gtk_widget_hide (info->icon_window);
3871 if (info->destroy_icon)
3872 gtk_widget_destroy (info->icon_window);
3874 if (info->fallback_icon)
3876 gtk_widget_destroy (info->fallback_icon);
3877 info->fallback_icon = NULL;
3880 g_object_unref (info->icon_window);
3881 info->icon_window = NULL;
3886 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
3890 for (i = 0; i < n_drag_cursors; i++)
3892 if (info->drag_cursors[i] != NULL)
3894 g_object_unref (info->drag_cursors[i]);
3895 info->drag_cursors[i] = NULL;
3899 gtk_drag_remove_icon (info);
3901 if (info->icon_pixbuf)
3903 g_object_unref (info->icon_pixbuf);
3904 info->icon_pixbuf = NULL;
3907 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3908 gtk_drag_grab_broken_event_cb,
3910 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3911 gtk_drag_grab_notify_cb,
3913 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3914 gtk_drag_button_release_cb,
3916 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3919 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3922 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3923 gtk_drag_selection_get,
3926 if (!info->proxy_dest)
3927 g_signal_emit_by_name (info->widget, "drag-end", info->context);
3930 g_object_unref (info->widget);
3932 gtk_selection_remove_all (info->ipc_widget);
3933 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), NULL);
3934 source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
3935 gtk_drag_release_ipc_widget (info->ipc_widget);
3937 gtk_target_list_unref (info->target_list);
3939 gtk_drag_clear_source_info (info->context);
3940 g_object_unref (info->context);
3942 if (info->drop_timeout)
3943 g_source_remove (info->drop_timeout);
3945 if (info->update_idle)
3946 g_source_remove (info->update_idle);
3952 gtk_drag_update_idle (gpointer data)
3954 GtkDragSourceInfo *info = data;
3955 GdkWindow *dest_window;
3956 GdkDragProtocol protocol;
3959 GdkDragAction action;
3960 GdkDragAction possible_actions;
3963 info->update_idle = 0;
3965 if (info->last_event)
3967 time = gtk_drag_get_event_time (info->last_event);
3968 gtk_drag_get_event_actions (info->last_event,
3970 info->possible_actions,
3971 &action, &possible_actions);
3972 gtk_drag_update_icon (info);
3973 gdk_drag_find_window_for_screen (info->context,
3974 info->icon_window ? gtk_widget_get_window (info->icon_window) : NULL,
3975 info->cur_screen, info->cur_x, info->cur_y,
3976 &dest_window, &protocol);
3978 if (!gdk_drag_motion (info->context, dest_window, protocol,
3979 info->cur_x, info->cur_y, action,
3983 gdk_event_free ((GdkEvent *)info->last_event);
3984 info->last_event = NULL;
3988 g_object_unref (dest_window);
3990 selection = gdk_drag_get_selection (info->context);
3992 gtk_drag_source_check_selection (info, selection, time);
4000 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
4002 /* We use an idle lower than GDK_PRIORITY_REDRAW so that exposes
4003 * from the last move can catch up before we move again.
4005 if (!info->update_idle)
4006 info->update_idle = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW + 5,
4007 gtk_drag_update_idle,
4014 * @info: DragSourceInfo for the drag
4015 * @screen: new screen
4016 * @x_root: new X position
4017 * @y_root: new y position
4018 * @event: event received requiring update
4020 * Updates the status of the drag; called when the
4021 * cursor moves or the modifier changes
4024 gtk_drag_update (GtkDragSourceInfo *info,
4030 info->cur_screen = screen;
4031 info->cur_x = x_root;
4032 info->cur_y = y_root;
4033 if (info->last_event)
4035 gdk_event_free ((GdkEvent *)info->last_event);
4036 info->last_event = NULL;
4039 info->last_event = gdk_event_copy ((GdkEvent *)event);
4041 gtk_drag_add_update_idle (info);
4044 /*************************************************************
4046 * Called when the user finishes to drag, either by
4047 * releasing the mouse, or by pressing Esc.
4049 * info: Source info for the drag
4050 * time: Timestamp for ending the drag
4052 *************************************************************/
4055 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
4057 GdkEvent *send_event;
4058 GtkWidget *source_widget = info->widget;
4059 GdkDevice *pointer, *keyboard;
4061 pointer = gdk_drag_context_get_device (info->context);
4062 keyboard = gdk_device_get_associated_device (pointer);
4064 /* Prevent ungrab before grab (see bug 623865) */
4065 if (info->grab_time == GDK_CURRENT_TIME)
4066 time = GDK_CURRENT_TIME;
4068 if (info->update_idle)
4070 g_source_remove (info->update_idle);
4071 info->update_idle = 0;
4074 if (info->last_event)
4076 gdk_event_free (info->last_event);
4077 info->last_event = NULL;
4080 info->have_grab = FALSE;
4082 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4083 gtk_drag_grab_broken_event_cb,
4085 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4086 gtk_drag_grab_notify_cb,
4088 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4089 gtk_drag_button_release_cb,
4091 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4094 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4098 gdk_device_ungrab (pointer, time);
4099 ungrab_dnd_keys (info->ipc_widget, keyboard, time);
4100 gtk_device_grab_remove (info->ipc_widget, pointer);
4102 /* Send on a release pair to the original
4103 * widget to convince it to release its grab. We need to
4104 * call gtk_propagate_event() here, instead of
4105 * gtk_widget_event() because widget like GtkList may
4106 * expect propagation.
4109 send_event = gdk_event_new (GDK_BUTTON_RELEASE);
4110 send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
4111 send_event->button.send_event = TRUE;
4112 send_event->button.time = time;
4113 send_event->button.x = 0;
4114 send_event->button.y = 0;
4115 send_event->button.axes = NULL;
4116 send_event->button.state = 0;
4117 send_event->button.button = info->button;
4118 send_event->button.device = pointer;
4119 send_event->button.x_root = 0;
4120 send_event->button.y_root = 0;
4122 gtk_propagate_event (source_widget, send_event);
4123 gdk_event_free (send_event);
4126 /*************************************************************
4128 * Called on cancellation of a drag, either by the user
4129 * or programmatically.
4131 * info: Source info for the drag
4132 * time: Timestamp for ending the drag
4134 *************************************************************/
4137 gtk_drag_cancel (GtkDragSourceInfo *info, GtkDragResult result, guint32 time)
4139 gtk_drag_end (info, time);
4140 gdk_drag_abort (info->context, time);
4141 gtk_drag_drop_finished (info, result, time);
4144 /*************************************************************
4145 * gtk_drag_motion_cb:
4146 * "motion-notify-event" callback during drag.
4150 *************************************************************/
4153 gtk_drag_motion_cb (GtkWidget *widget,
4154 GdkEventMotion *event,
4157 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4159 gint x_root, y_root;
4163 GdkDisplay *display = gtk_widget_get_display (widget);
4165 gdk_display_get_device_state (display, event->device,
4166 &screen, &x_root, &y_root, NULL);
4167 event->x_root = x_root;
4168 event->y_root = y_root;
4171 screen = gdk_event_get_screen ((GdkEvent *)event);
4173 x_root = (gint)(event->x_root + 0.5);
4174 y_root = (gint)(event->y_root + 0.5);
4175 gtk_drag_update (info, screen, x_root, y_root, (GdkEvent *) event);
4180 /*************************************************************
4182 * "key-press/release-event" callback during drag.
4186 *************************************************************/
4189 #define SMALL_STEP 1
4192 gtk_drag_key_cb (GtkWidget *widget,
4196 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4197 GdkModifierType state;
4198 GdkWindow *root_window;
4203 state = event->state & gtk_accelerator_get_default_mod_mask ();
4204 pointer = gdk_device_get_associated_device (gdk_event_get_device ((GdkEvent *) event));
4206 if (event->type == GDK_KEY_PRESS)
4208 switch (event->keyval)
4210 case GDK_KEY_Escape:
4211 gtk_drag_cancel (info, GTK_DRAG_RESULT_USER_CANCELLED, event->time);
4215 case GDK_KEY_Return:
4216 case GDK_KEY_ISO_Enter:
4217 case GDK_KEY_KP_Enter:
4218 case GDK_KEY_KP_Space:
4219 if ((gdk_drag_context_get_selected_action (info->context) != 0) &&
4220 (gdk_drag_context_get_dest_window (info->context) != NULL))
4222 gtk_drag_end (info, event->time);
4223 gtk_drag_drop (info, event->time);
4227 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4234 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4238 case GDK_KEY_KP_Down:
4239 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4243 case GDK_KEY_KP_Left:
4244 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4248 case GDK_KEY_KP_Right:
4249 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4254 /* Now send a "motion" so that the modifier state is updated */
4256 /* The state is not yet updated in the event, so we need
4257 * to query it here. We could use XGetModifierMapping, but
4258 * that would be overkill.
4260 root_window = gtk_widget_get_root_window (widget);
4261 gdk_window_get_device_position (root_window, pointer, NULL, NULL, &state);
4262 event->state = state;
4264 if (dx != 0 || dy != 0)
4268 gdk_device_warp (pointer,
4269 gtk_widget_get_screen (widget),
4270 info->cur_x, info->cur_y);
4273 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
4279 gtk_drag_grab_broken_event_cb (GtkWidget *widget,
4280 GdkEventGrabBroken *event,
4283 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4285 /* Don't cancel if we break the implicit grab from the initial button_press.
4286 * Also, don't cancel if we re-grab on the widget or on our IPC window, for
4287 * example, when changing the drag cursor.
4290 || event->grab_window == gtk_widget_get_window (info->widget)
4291 || event->grab_window == gtk_widget_get_window (info->ipc_widget))
4294 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4299 gtk_drag_grab_notify_cb (GtkWidget *widget,
4300 gboolean was_grabbed,
4303 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4306 pointer = gdk_drag_context_get_device (info->context);
4308 if (gtk_widget_device_is_shadowed (widget, pointer))
4310 /* We have to block callbacks to avoid recursion here, because
4311 gtk_drag_cancel calls gtk_grab_remove (via gtk_drag_end) */
4312 g_signal_handlers_block_by_func (widget, gtk_drag_grab_notify_cb, data);
4313 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4314 g_signal_handlers_unblock_by_func (widget, gtk_drag_grab_notify_cb, data);
4319 /*************************************************************
4320 * gtk_drag_button_release_cb:
4321 * "button-release-event" callback during drag.
4325 *************************************************************/
4328 gtk_drag_button_release_cb (GtkWidget *widget,
4329 GdkEventButton *event,
4332 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4334 if (event->button != info->button)
4337 if ((gdk_drag_context_get_selected_action (info->context) != 0) &&
4338 (gdk_drag_context_get_dest_window (info->context) != NULL))
4340 gtk_drag_end (info, event->time);
4341 gtk_drag_drop (info, event->time);
4345 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4352 gtk_drag_abort_timeout (gpointer data)
4354 GtkDragSourceInfo *info = data;
4355 guint32 time = GDK_CURRENT_TIME;
4357 if (info->proxy_dest)
4358 time = info->proxy_dest->proxy_drop_time;
4360 info->drop_timeout = 0;
4361 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_TIMEOUT_EXPIRED, time);
4367 * gtk_drag_check_threshold:
4368 * @widget: a #GtkWidget
4369 * @start_x: X coordinate of start of drag
4370 * @start_y: Y coordinate of start of drag
4371 * @current_x: current X coordinate
4372 * @current_y: current Y coordinate
4374 * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
4375 * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
4376 * should trigger the beginning of a drag-and-drop operation.
4378 * Return Value: %TRUE if the drag threshold has been passed.
4381 gtk_drag_check_threshold (GtkWidget *widget,
4387 gint drag_threshold;
4389 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4391 g_object_get (gtk_widget_get_settings (widget),
4392 "gtk-dnd-drag-threshold", &drag_threshold,
4395 return (ABS (current_x - start_x) > drag_threshold ||
4396 ABS (current_y - start_y) > drag_threshold);