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"
40 #include <X11/extensions/XInput2.h>
45 #include "gtkiconfactory.h"
46 #include "gtkicontheme.h"
47 #include "gtkimageprivate.h"
48 #include "gtkinvisible.h"
52 #include "gtktooltip.h"
53 #include "gtkwindow.h"
55 #include "gtkdndcursors.h"
56 #include "gtkselectionprivate.h"
61 * @Short_description: Functions for controlling drag and drop handling
62 * @Title: Drag and Drop
64 * GTK+ has a rich set of functions for doing inter-process
65 * communication via the drag-and-drop metaphor. GTK+
66 * can do drag-and-drop (DND) via multiple protocols.
67 * The currently supported protocols are the Xdnd and
70 * As well as the functions listed here, applications
71 * may need to use some facilities provided for
72 * <link linkend="gtk-Selections">Selections</link>.
73 * Also, the Drag and Drop API makes use of signals
74 * in the #GtkWidget class.
78 static GSList *source_widgets = NULL;
80 typedef struct _GtkDragSourceSite GtkDragSourceSite;
81 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
82 typedef struct _GtkDragDestSite GtkDragDestSite;
83 typedef struct _GtkDragDestInfo GtkDragDestInfo;
84 typedef struct _GtkDragAnim GtkDragAnim;
94 struct _GtkDragSourceSite
96 GdkModifierType start_button_mask;
97 GtkTargetList *target_list; /* Targets for drag data */
98 GdkDragAction actions; /* Possible actions */
101 GtkImageType icon_type;
104 GtkImagePixbufData pixbuf;
105 GtkImageStockData stock;
106 GtkImageIconNameData name;
107 GtkImageGIconData gicon;
110 /* Stored button press information to detect drag beginning */
115 struct _GtkDragSourceInfo
118 GtkTargetList *target_list; /* Targets for drag data */
119 GdkDragAction possible_actions; /* Actions allowed by source */
120 GdkDragContext *context; /* drag context */
121 GtkWidget *icon_window; /* Window for drag */
122 GtkWidget *fallback_icon; /* Window for drag used on other screens */
123 GtkWidget *ipc_widget; /* GtkInvisible for grab, message passing */
124 GdkCursor *cursor; /* Cursor for drag */
125 gint hot_x, hot_y; /* Hot spot for drag */
126 gint button; /* mouse button starting drag */
128 GtkDragStatus status; /* drag status */
129 GdkEvent *last_event; /* pending event */
131 gint start_x, start_y; /* Initial position */
132 gint cur_x, cur_y; /* Current Position */
133 GdkScreen *cur_screen; /* Current screen for pointer */
135 guint32 grab_time; /* timestamp for initial grab */
136 GList *selections; /* selections we've claimed */
138 GtkDragDestInfo *proxy_dest; /* Set if this is a proxy drag */
140 guint update_idle; /* Idle function to update the drag */
141 guint drop_timeout; /* Timeout for aborting drop */
142 guint destroy_icon : 1; /* If true, destroy icon_window */
143 guint have_grab : 1; /* Do we still have the pointer grab */
144 GdkPixbuf *icon_pixbuf;
145 GdkCursor *drag_cursors[6];
148 struct _GtkDragDestSite
150 GtkDestDefaults flags;
151 GtkTargetList *target_list;
152 GdkDragAction actions;
153 GdkWindow *proxy_window;
154 GdkDragProtocol proxy_protocol;
156 guint proxy_coords : 1;
158 guint track_motion : 1;
161 struct _GtkDragDestInfo
163 GtkWidget *widget; /* Widget in which drag is in */
164 GdkDragContext *context; /* Drag context */
165 GtkDragSourceInfo *proxy_source; /* Set if this is a proxy drag */
166 GtkSelectionData *proxy_data; /* Set while retrieving proxied data */
167 guint32 proxy_drop_time; /* Timestamp for proxied drop */
168 guint proxy_drop_wait : 1; /* Set if we are waiting for a
169 * status reply before sending
172 guint dropped : 1; /* Set after we receive a drop */
173 gint drop_x, drop_y; /* Position of drop */
176 #define DROP_ABORT_TIME 300000
178 #define ANIM_STEP_TIME 50
179 #define ANIM_STEP_LENGTH 50
180 #define ANIM_MIN_STEPS 5
181 #define ANIM_MAX_STEPS 10
185 GtkDragSourceInfo *info;
190 typedef gboolean (* GtkDragDestCallback) (GtkWidget *widget,
191 GdkDragContext *context,
196 /* Enumeration for some targets we handle internally */
199 TARGET_MOTIF_SUCCESS = 0x40000000,
200 TARGET_MOTIF_FAILURE,
204 /* Forward declarations */
205 static void gtk_drag_get_event_actions (GdkEvent *event,
207 GdkDragAction actions,
208 GdkDragAction *suggested_action,
209 GdkDragAction *possible_actions);
210 static GdkCursor * gtk_drag_get_cursor (GdkDisplay *display,
211 GdkDragAction action,
212 GtkDragSourceInfo *info);
213 static void gtk_drag_update_cursor (GtkDragSourceInfo *info);
214 static GtkWidget *gtk_drag_get_ipc_widget (GtkWidget *widget);
215 static GtkWidget *gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen);
216 static void gtk_drag_release_ipc_widget (GtkWidget *widget);
218 static void gtk_drag_selection_received (GtkWidget *widget,
219 GtkSelectionData *selection_data,
222 static gboolean gtk_drag_find_widget (GtkWidget *widget,
223 GdkDragContext *context,
224 GtkDragDestInfo *info,
228 GtkDragDestCallback callback);
229 static void gtk_drag_proxy_begin (GtkWidget *widget,
230 GtkDragDestInfo *dest_info,
232 static void gtk_drag_dest_realized (GtkWidget *widget);
233 static void gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
234 GtkWidget *previous_toplevel);
235 static void gtk_drag_dest_site_destroy (gpointer data);
236 static void gtk_drag_dest_leave (GtkWidget *widget,
237 GdkDragContext *context,
239 static gboolean gtk_drag_dest_motion (GtkWidget *widget,
240 GdkDragContext *context,
244 static gboolean gtk_drag_dest_drop (GtkWidget *widget,
245 GdkDragContext *context,
250 static GtkDragDestInfo * gtk_drag_get_dest_info (GdkDragContext *context,
252 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
254 static void gtk_drag_clear_source_info (GdkDragContext *context);
256 static void gtk_drag_source_check_selection (GtkDragSourceInfo *info,
259 static void gtk_drag_source_release_selections (GtkDragSourceInfo *info,
261 static void gtk_drag_drop (GtkDragSourceInfo *info,
263 static void gtk_drag_drop_finished (GtkDragSourceInfo *info,
264 GtkDragResult result,
266 static void gtk_drag_cancel (GtkDragSourceInfo *info,
267 GtkDragResult result,
270 static gboolean gtk_drag_source_event_cb (GtkWidget *widget,
273 static void gtk_drag_source_site_destroy (gpointer data);
274 static void gtk_drag_selection_get (GtkWidget *widget,
275 GtkSelectionData *selection_data,
279 static gboolean gtk_drag_anim_timeout (gpointer data);
280 static void gtk_drag_remove_icon (GtkDragSourceInfo *info);
281 static void gtk_drag_source_info_destroy (GtkDragSourceInfo *info);
282 static void gtk_drag_add_update_idle (GtkDragSourceInfo *info);
284 static void gtk_drag_update (GtkDragSourceInfo *info,
289 static gboolean gtk_drag_motion_cb (GtkWidget *widget,
290 GdkEventMotion *event,
292 static gboolean gtk_drag_key_cb (GtkWidget *widget,
295 static gboolean gtk_drag_grab_broken_event_cb (GtkWidget *widget,
296 GdkEventGrabBroken *event,
298 static void gtk_drag_grab_notify_cb (GtkWidget *widget,
299 gboolean was_grabbed,
301 static gboolean gtk_drag_button_release_cb (GtkWidget *widget,
302 GdkEventButton *event,
304 static gboolean gtk_drag_abort_timeout (gpointer data);
306 static void set_icon_stock_pixbuf (GdkDragContext *context,
307 const gchar *stock_id,
311 gboolean force_window);
313 /************************
314 * Cursor and Icon data *
315 ************************/
318 GdkDragAction action;
324 { GDK_ACTION_DEFAULT, NULL },
325 { GDK_ACTION_ASK, "dnd-ask", dnd_cursor_ask, NULL, NULL },
326 { GDK_ACTION_COPY, "dnd-copy", dnd_cursor_copy, NULL, NULL },
327 { GDK_ACTION_MOVE, "dnd-move", dnd_cursor_move, NULL, NULL },
328 { GDK_ACTION_LINK, "dnd-link", dnd_cursor_link, NULL, NULL },
329 { 0 , "dnd-none", dnd_cursor_none, NULL, NULL },
332 /*********************
333 * Utility functions *
334 *********************/
337 set_can_change_screen (GtkWidget *widget,
338 gboolean can_change_screen)
340 can_change_screen = can_change_screen != FALSE;
342 g_object_set_data (G_OBJECT (widget), I_("gtk-dnd-can-change-screen"),
343 GUINT_TO_POINTER (can_change_screen));
347 get_can_change_screen (GtkWidget *widget)
349 return g_object_get_data (G_OBJECT (widget), "gtk-dnd-can-change-screen") != NULL;
354 gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen)
357 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
358 "gtk-dnd-ipc-widgets");
362 GSList *tmp = drag_widgets;
363 result = drag_widgets->data;
364 drag_widgets = drag_widgets->next;
365 g_object_set_data (G_OBJECT (screen),
366 I_("gtk-dnd-ipc-widgets"),
368 g_slist_free_1 (tmp);
372 result = gtk_window_new (GTK_WINDOW_POPUP);
373 gtk_window_set_screen (GTK_WINDOW (result), screen);
374 gtk_window_resize (GTK_WINDOW (result), 1, 1);
375 gtk_window_move (GTK_WINDOW (result), -100, -100);
376 gtk_widget_show (result);
383 gtk_drag_get_ipc_widget (GtkWidget *widget)
388 result = gtk_drag_get_ipc_widget_for_screen (gtk_widget_get_screen (widget));
390 toplevel = gtk_widget_get_toplevel (widget);
392 if (GTK_IS_WINDOW (toplevel))
394 if (gtk_window_has_group (GTK_WINDOW (toplevel)))
395 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
396 GTK_WINDOW (result));
402 #if defined (GDK_WINDOWING_X11)
405 * We want to handle a handful of keys during DND, e.g. Escape to abort.
406 * Grabbing the keyboard has the unfortunate side-effect of preventing
407 * useful things such as using Alt-Tab to cycle between windows or
408 * switching workspaces. Therefore, we just grab the few keys we are
409 * interested in. Note that we need to put the grabs on the root window
410 * in order for them to still work when the focus is moved to another
413 * GDK needs a little help to successfully deliver root key events...
416 static GdkFilterReturn
417 root_key_filter (GdkXEvent *xevent,
421 XEvent *ev = (XEvent *) xevent;
423 if ((ev->type == KeyPress || ev->type == KeyRelease) &&
424 ev->xkey.root == ev->xkey.window)
425 ev->xkey.window = (Window)data;
427 return GDK_FILTER_CONTINUE;
435 static GrabKey grab_keys[] = {
444 { XK_Down, Mod1Mask },
446 { XK_Left, Mod1Mask },
448 { XK_Right, Mod1Mask },
450 { XK_KP_Up, Mod1Mask },
452 { XK_KP_Down, Mod1Mask },
454 { XK_KP_Left, Mod1Mask },
456 { XK_KP_Right, Mod1Mask }
460 grab_dnd_keys (GtkWidget *widget,
465 GdkWindow *window, *root;
469 XIGrabModifiers mods;
472 unsigned char mask[(XI_LASTEVENT + 7)/8];
475 deviceid = gdk_x11_device_get_id (device);
477 if (GDK_IS_X11_DEVICE_MANAGER_XI2 (gdk_display_get_device_manager (gtk_widget_get_display (widget))))
483 window = gtk_widget_get_window (widget);
484 if (!GDK_IS_X11_WINDOW (window))
486 gdk_device_grab (device,
487 gtk_widget_get_window (widget),
488 GDK_OWNERSHIP_APPLICATION, FALSE,
489 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
495 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
497 gdk_error_trap_push ();
499 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
501 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
502 if (keycode == NoSymbol)
508 memset (mask, 0, sizeof (mask));
509 XISetMask (mask, XI_KeyPress);
510 XISetMask (mask, XI_KeyRelease);
512 evmask.deviceid = deviceid;
513 evmask.mask_len = sizeof (mask);
517 mods.modifiers = grab_keys[i].modifiers;
519 XIGrabKeycode (GDK_WINDOW_XDISPLAY (window),
522 GDK_WINDOW_XID (root),
532 XGrabKey (GDK_WINDOW_XDISPLAY (window),
533 keycode, grab_keys[i].modifiers,
534 GDK_WINDOW_XID (root),
541 gdk_error_trap_pop_ignored ();
543 gdk_window_add_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
547 ungrab_dnd_keys (GtkWidget *widget,
552 GdkWindow *window, *root;
555 XIGrabModifiers mods;
560 deviceid = gdk_x11_device_get_id (device);
561 if (GDK_IS_X11_DEVICE_MANAGER_XI2 (gdk_display_get_device_manager (gtk_widget_get_display (widget))))
567 window = gtk_widget_get_window (widget);
568 if (!GDK_IS_X11_WINDOW (window))
570 gdk_device_ungrab (device, time);
574 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
576 gdk_window_remove_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
578 gdk_error_trap_push ();
580 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
582 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
583 if (keycode == NoSymbol)
590 mods.modifiers = grab_keys[i].modifiers;
592 XIUngrabKeycode (GDK_WINDOW_XDISPLAY (window),
595 GDK_WINDOW_XID (root),
601 XUngrabKey (GDK_WINDOW_XDISPLAY (window),
602 keycode, grab_keys[i].modifiers,
603 GDK_WINDOW_XID (root));
607 gdk_error_trap_pop_ignored ();
610 #else /* !GDK_WINDOWING_X11 */
613 grab_dnd_keys (GtkWidget *widget,
617 gdk_device_grab (device,
618 gtk_widget_get_window (widget),
619 GDK_OWNERSHIP_APPLICATION, FALSE,
620 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
625 ungrab_dnd_keys (GtkWidget *widget,
629 gdk_device_ungrab (device, time);
632 #endif /* GDK_WINDOWING_X11 */
635 /***************************************************************
636 * gtk_drag_release_ipc_widget:
637 * Releases widget retrieved with gtk_drag_get_ipc_widget ()
639 * widget: the widget to release.
641 ***************************************************************/
644 gtk_drag_release_ipc_widget (GtkWidget *widget)
646 GtkWindow *window = GTK_WINDOW (widget);
647 GdkScreen *screen = gtk_widget_get_screen (widget);
648 GdkDragContext *context = g_object_get_data (G_OBJECT (widget), "drag-context");
649 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
650 "gtk-dnd-ipc-widgets");
651 GdkDevice *pointer, *keyboard;
655 pointer = gdk_drag_context_get_device (context);
656 keyboard = gdk_device_get_associated_device (pointer);
659 ungrab_dnd_keys (widget, keyboard, GDK_CURRENT_TIME);
662 if (gtk_window_has_group (window))
663 gtk_window_group_remove_window (gtk_window_get_group (window),
665 drag_widgets = g_slist_prepend (drag_widgets, widget);
666 g_object_set_data (G_OBJECT (screen),
667 I_("gtk-dnd-ipc-widgets"),
672 gtk_drag_get_event_time (GdkEvent *event)
674 guint32 tm = GDK_CURRENT_TIME;
679 case GDK_MOTION_NOTIFY:
680 tm = event->motion.time; break;
681 case GDK_BUTTON_PRESS:
682 case GDK_2BUTTON_PRESS:
683 case GDK_3BUTTON_PRESS:
684 case GDK_BUTTON_RELEASE:
685 tm = event->button.time; break;
687 case GDK_KEY_RELEASE:
688 tm = event->key.time; break;
689 case GDK_ENTER_NOTIFY:
690 case GDK_LEAVE_NOTIFY:
691 tm = event->crossing.time; break;
692 case GDK_PROPERTY_NOTIFY:
693 tm = event->property.time; break;
694 case GDK_SELECTION_CLEAR:
695 case GDK_SELECTION_REQUEST:
696 case GDK_SELECTION_NOTIFY:
697 tm = event->selection.time; break;
698 case GDK_PROXIMITY_IN:
699 case GDK_PROXIMITY_OUT:
700 tm = event->proximity.time; break;
701 default: /* use current time */
709 gtk_drag_get_event_actions (GdkEvent *event,
711 GdkDragAction actions,
712 GdkDragAction *suggested_action,
713 GdkDragAction *possible_actions)
715 *suggested_action = 0;
716 *possible_actions = 0;
720 GdkModifierType state = 0;
724 case GDK_MOTION_NOTIFY:
725 state = event->motion.state;
727 case GDK_BUTTON_PRESS:
728 case GDK_2BUTTON_PRESS:
729 case GDK_3BUTTON_PRESS:
730 case GDK_BUTTON_RELEASE:
731 state = event->button.state;
734 case GDK_KEY_RELEASE:
735 state = event->key.state;
737 case GDK_ENTER_NOTIFY:
738 case GDK_LEAVE_NOTIFY:
739 state = event->crossing.state;
745 if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
747 *suggested_action = GDK_ACTION_ASK;
748 *possible_actions = actions;
750 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
752 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
754 if (actions & GDK_ACTION_LINK)
756 *suggested_action = GDK_ACTION_LINK;
757 *possible_actions = GDK_ACTION_LINK;
760 else if (state & GDK_CONTROL_MASK)
762 if (actions & GDK_ACTION_COPY)
764 *suggested_action = GDK_ACTION_COPY;
765 *possible_actions = GDK_ACTION_COPY;
771 if (actions & GDK_ACTION_MOVE)
773 *suggested_action = GDK_ACTION_MOVE;
774 *possible_actions = GDK_ACTION_MOVE;
781 *possible_actions = actions;
783 if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
784 *suggested_action = GDK_ACTION_ASK;
785 else if (actions & GDK_ACTION_COPY)
786 *suggested_action = GDK_ACTION_COPY;
787 else if (actions & GDK_ACTION_MOVE)
788 *suggested_action = GDK_ACTION_MOVE;
789 else if (actions & GDK_ACTION_LINK)
790 *suggested_action = GDK_ACTION_LINK;
795 *possible_actions = actions;
797 if (actions & GDK_ACTION_COPY)
798 *suggested_action = GDK_ACTION_COPY;
799 else if (actions & GDK_ACTION_MOVE)
800 *suggested_action = GDK_ACTION_MOVE;
801 else if (actions & GDK_ACTION_LINK)
802 *suggested_action = GDK_ACTION_LINK;
807 gtk_drag_can_use_rgba_cursor (GdkDisplay *display,
811 guint max_width, max_height;
813 if (!gdk_display_supports_cursor_color (display))
816 if (!gdk_display_supports_cursor_alpha (display))
819 gdk_display_get_maximal_cursor_size (display,
822 if (width > max_width || height > max_height)
824 /* can't use rgba cursor (too large) */
832 gtk_drag_get_cursor (GdkDisplay *display,
833 GdkDragAction action,
834 GtkDragSourceInfo *info)
838 /* reconstruct the cursors for each new drag (thus !info),
839 * to catch cursor theme changes
843 for (i = 0 ; i < G_N_ELEMENTS (drag_cursors) - 1; i++)
844 if (drag_cursors[i].cursor != NULL)
846 g_object_unref (drag_cursors[i].cursor);
847 drag_cursors[i].cursor = NULL;
851 for (i = 0 ; i < G_N_ELEMENTS (drag_cursors) - 1; i++)
852 if (drag_cursors[i].action == action)
855 if (drag_cursors[i].pixbuf == NULL)
856 drag_cursors[i].pixbuf =
857 gdk_pixbuf_new_from_inline (-1, drag_cursors[i].data, FALSE, NULL);
859 if (drag_cursors[i].cursor != NULL)
861 if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
863 g_object_unref (drag_cursors[i].cursor);
864 drag_cursors[i].cursor = NULL;
868 if (drag_cursors[i].cursor == NULL)
869 drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
871 if (drag_cursors[i].cursor == NULL)
872 drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);
874 if (info && info->icon_pixbuf)
876 gint cursor_width, cursor_height;
877 gint icon_width, icon_height;
879 GdkPixbuf *cursor_pixbuf, *pixbuf;
881 gint icon_x, icon_y, ref_x, ref_y;
883 if (info->drag_cursors[i] != NULL)
885 if (display == gdk_cursor_get_display (info->drag_cursors[i]))
886 return info->drag_cursors[i];
888 g_object_unref (info->drag_cursors[i]);
889 info->drag_cursors[i] = NULL;
892 icon_x = info->hot_x;
893 icon_y = info->hot_y;
894 icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
895 icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);
898 cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
900 cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
903 if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
904 hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
906 if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
907 hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
910 /* The code below is an attempt to let cursor themes
911 * determine the attachment of the icon to enable things
912 * like the following:
920 * It does not work since Xcursor doesn't allow to attach
921 * any additional information to cursors in a retrievable
922 * way (there are comments, but no way to get at them
923 * short of searching for the actual cursor file).
924 * If this code ever gets used, the icon_window placement
925 * must be changed to recognize these placement options
926 * as well. Note that this code ignores info->hot_x/y.
928 for (j = 0; j < 10; j++)
933 GtkAnchorType icon_anchor;
935 g_snprintf (key, 32, "comment%d", j);
936 opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
937 if (opt && g_str_has_prefix ("icon-attach:", opt))
939 toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
940 if (g_strv_length (toks) != 3)
945 icon_anchor = atoi (toks[0]);
946 icon_x = atoi (toks[1]);
947 icon_y = atoi (toks[2]);
951 case GTK_ANCHOR_NORTH:
952 case GTK_ANCHOR_CENTER:
953 case GTK_ANCHOR_SOUTH:
954 icon_x += icon_width / 2;
956 case GTK_ANCHOR_NORTH_EAST:
957 case GTK_ANCHOR_EAST:
958 case GTK_ANCHOR_SOUTH_EAST:
959 icon_x += icon_width;
966 case GTK_ANCHOR_WEST:
967 case GTK_ANCHOR_CENTER:
968 case GTK_ANCHOR_EAST:
969 icon_y += icon_height / 2;
971 case GTK_ANCHOR_SOUTH_WEST:
972 case GTK_ANCHOR_SOUTH:
973 case GTK_ANCHOR_SOUTH_EAST:
974 icon_x += icon_height;
986 cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
987 cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
989 ref_x = MAX (hot_x, icon_x);
990 ref_y = MAX (hot_y, icon_y);
991 width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
992 height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
994 if (gtk_drag_can_use_rgba_cursor (display, width, height))
996 /* Composite cursor and icon so that both hotspots
997 * end up at (ref_x, ref_y)
999 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
1002 gdk_pixbuf_fill (pixbuf, 0xff000000);
1004 gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
1005 ref_x - icon_x, ref_y - icon_y,
1006 icon_width, icon_height,
1007 ref_x - icon_x, ref_y - icon_y,
1009 GDK_INTERP_BILINEAR, 255);
1011 gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
1012 ref_x - hot_x, ref_y - hot_y,
1013 cursor_width, cursor_height,
1014 ref_x - hot_x, ref_y - hot_y,
1016 GDK_INTERP_BILINEAR, 255);
1018 info->drag_cursors[i] =
1019 gdk_cursor_new_from_pixbuf (display, pixbuf, ref_x, ref_y);
1021 g_object_unref (pixbuf);
1024 g_object_unref (cursor_pixbuf);
1026 if (info->drag_cursors[i] != NULL)
1027 return info->drag_cursors[i];
1030 return drag_cursors[i].cursor;
1034 gtk_drag_update_cursor (GtkDragSourceInfo *info)
1039 if (!info->have_grab)
1042 for (i = 0 ; i < G_N_ELEMENTS (drag_cursors) - 1; i++)
1043 if (info->cursor == drag_cursors[i].cursor ||
1044 info->cursor == info->drag_cursors[i])
1047 if (i == G_N_ELEMENTS (drag_cursors))
1050 cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor),
1051 drag_cursors[i].action, info);
1053 if (cursor != info->cursor)
1057 pointer = gdk_drag_context_get_device (info->context);
1058 gdk_device_grab (pointer,
1059 gtk_widget_get_window (info->ipc_widget),
1060 GDK_OWNERSHIP_APPLICATION, FALSE,
1061 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
1062 cursor, info->grab_time);
1063 info->cursor = cursor;
1067 /********************
1068 * Destination side *
1069 ********************/
1072 * gtk_drag_get_data: (method)
1073 * @widget: the widget that will receive the
1074 * #GtkWidget::drag-data-received signal.
1075 * @context: the drag context
1076 * @target: the target (form of the data) to retrieve.
1077 * @time_: a timestamp for retrieving the data. This will
1078 * generally be the time received in a #GtkWidget::drag-motion"
1079 * or #GtkWidget::drag-drop" signal.
1081 * Gets the data associated with a drag. When the data
1082 * is received or the retrieval fails, GTK+ will emit a
1083 * #GtkWidget::drag-data-received signal. Failure of the retrieval
1084 * is indicated by the length field of the @selection_data
1085 * signal parameter being negative. However, when gtk_drag_get_data()
1086 * is called implicitely because the %GTK_DEST_DEFAULT_DROP was set,
1087 * then the widget will not receive notification of failed
1091 gtk_drag_get_data (GtkWidget *widget,
1092 GdkDragContext *context,
1096 GtkWidget *selection_widget;
1098 g_return_if_fail (GTK_IS_WIDGET (widget));
1099 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1101 selection_widget = gtk_drag_get_ipc_widget (widget);
1103 g_object_ref (context);
1104 g_object_ref (widget);
1106 g_signal_connect (selection_widget, "selection-received",
1107 G_CALLBACK (gtk_drag_selection_received), widget);
1109 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1111 gtk_selection_convert (selection_widget,
1112 gdk_drag_get_selection (context),
1119 * gtk_drag_get_source_widget: (method)
1120 * @context: a (destination side) drag context
1122 * Determines the source widget for a drag.
1124 * Return value: (transfer none): if the drag is occurring
1125 * within a single application, a pointer to the source widget.
1129 gtk_drag_get_source_widget (GdkDragContext *context)
1133 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
1135 tmp_list = source_widgets;
1138 GtkWidget *ipc_widget = tmp_list->data;
1140 if (gtk_widget_get_window (ipc_widget) == gdk_drag_context_get_source_window (context))
1142 GtkDragSourceInfo *info;
1143 info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
1145 return info ? info->widget : NULL;
1148 tmp_list = tmp_list->next;
1155 * gtk_drag_finish: (method)
1156 * @context: the drag context.
1157 * @success: a flag indicating whether the drop was successful
1158 * @del: a flag indicating whether the source should delete the
1159 * original data. (This should be %TRUE for a move)
1160 * @time_: the timestamp from the #GtkWidget::drag-drop signal.
1162 * Informs the drag source that the drop is finished, and
1163 * that the data of the drag will no longer be required.
1166 gtk_drag_finish (GdkDragContext *context,
1171 GdkAtom target = GDK_NONE;
1173 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1177 target = gdk_atom_intern_static_string ("DELETE");
1179 else if (gdk_drag_context_get_protocol (context) == GDK_DRAG_PROTO_MOTIF)
1181 target = gdk_atom_intern_static_string (success ?
1182 "XmTRANSFER_SUCCESS" :
1183 "XmTRANSFER_FAILURE");
1186 if (target != GDK_NONE)
1188 GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_window_get_screen (gdk_drag_context_get_source_window (context)));
1190 g_object_ref (context);
1192 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1193 g_signal_connect (selection_widget, "selection-received",
1194 G_CALLBACK (gtk_drag_selection_received),
1197 gtk_selection_convert (selection_widget,
1198 gdk_drag_get_selection (context),
1203 if (!(success && del))
1204 gdk_drop_finish (context, success, time);
1207 /*************************************************************
1208 * gtk_drag_highlight_draw:
1209 * Callback for expose_event for highlighted widgets.
1215 *************************************************************/
1218 gtk_drag_highlight_draw (GtkWidget *widget,
1222 int width = gtk_widget_get_allocated_width (widget);
1223 int height = gtk_widget_get_allocated_height (widget);
1224 GtkStyleContext *context;
1226 context = gtk_widget_get_style_context (widget);
1228 gtk_style_context_save (context);
1229 gtk_style_context_add_class (context, GTK_STYLE_CLASS_DND);
1231 gtk_render_frame (context, cr, 0, 0, width, height);
1233 gtk_style_context_restore (context);
1235 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
1236 cairo_set_line_width (cr, 1.0);
1237 cairo_rectangle (cr,
1239 width - 1, height - 1);
1246 * gtk_drag_highlight: (method)
1247 * @widget: a widget to highlight
1249 * Draws a highlight around a widget. This will attach
1250 * handlers to #GtkWidget::draw, so the highlight
1251 * will continue to be displayed until gtk_drag_unhighlight()
1255 gtk_drag_highlight (GtkWidget *widget)
1257 g_return_if_fail (GTK_IS_WIDGET (widget));
1259 g_signal_connect_after (widget, "draw",
1260 G_CALLBACK (gtk_drag_highlight_draw),
1263 gtk_widget_queue_draw (widget);
1267 * gtk_drag_unhighlight: (method)
1268 * @widget: a widget to remove the highlight from.
1270 * Removes a highlight set by gtk_drag_highlight() from
1274 gtk_drag_unhighlight (GtkWidget *widget)
1276 g_return_if_fail (GTK_IS_WIDGET (widget));
1278 g_signal_handlers_disconnect_by_func (widget,
1279 gtk_drag_highlight_draw,
1282 gtk_widget_queue_draw (widget);
1286 gtk_drag_dest_set_internal (GtkWidget *widget,
1287 GtkDragDestSite *site)
1289 GtkDragDestSite *old_site;
1291 g_return_if_fail (widget != NULL);
1293 /* HACK, do this in the destroy */
1294 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1297 g_signal_handlers_disconnect_by_func (widget,
1298 gtk_drag_dest_realized,
1300 g_signal_handlers_disconnect_by_func (widget,
1301 gtk_drag_dest_hierarchy_changed,
1304 site->track_motion = old_site->track_motion;
1307 if (gtk_widget_get_realized (widget))
1308 gtk_drag_dest_realized (widget);
1310 g_signal_connect (widget, "realize",
1311 G_CALLBACK (gtk_drag_dest_realized), site);
1312 g_signal_connect (widget, "hierarchy-changed",
1313 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1315 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
1316 site, gtk_drag_dest_site_destroy);
1320 * gtk_drag_dest_set: (method)
1321 * @widget: a #GtkWidget
1322 * @flags: which types of default drag behavior to use
1323 * @targets: (allow-none) (array length=n_targets): a pointer to an array of #GtkTargetEntry<!-- -->s
1324 * indicating the drop types that this @widget will accept, or %NULL.
1325 * Later you can access the list with gtk_drag_dest_get_target_list()
1326 * and gtk_drag_dest_find_target().
1327 * @n_targets: the number of entries in @targets
1328 * @actions: a bitmask of possible actions for a drop onto this @widget.
1330 * Sets a widget as a potential drop destination, and adds default behaviors.
1332 * The default behaviors listed in @flags have an effect similar
1333 * to installing default handlers for the widget's drag-and-drop signals
1334 * (#GtkWidget::drag-motion, #GtkWidget::drag-drop, ...). They all exist
1335 * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
1336 * sufficient to connect to the widget's #GtkWidget::drag-data-received
1337 * signal to get primitive, but consistent drag-and-drop support.
1339 * Things become more complicated when you try to preview the dragged data,
1340 * as described in the documentation for #GtkWidget::drag-motion. The default
1341 * behaviors described by @flags make some assumptions, that can conflict
1342 * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
1343 * invokations of gdk_drag_status() in the context of #GtkWidget::drag-motion,
1344 * and invokations of gtk_drag_finish() in #GtkWidget::drag-data-received.
1345 * Especially the later is dramatic, when your own #GtkWidget::drag-motion
1346 * handler calls gtk_drag_get_data() to inspect the dragged data.
1348 * There's no way to set a default action here, you can use the
1349 * #GtkWidget::drag-motion callback for that. Here's an example which selects
1350 * the action to use depending on whether the control key is pressed or not:
1353 * drag_motion (GtkWidget *widget,
1354 * GdkDragContext *context,
1359 * GdkModifierType mask;
1361 * gdk_window_get_pointer (gtk_widget_get_window (widget),
1362 * NULL, NULL, &mask);
1363 * if (mask & GDK_CONTROL_MASK)
1364 * gdk_drag_status (context, GDK_ACTION_COPY, time);
1366 * gdk_drag_status (context, GDK_ACTION_MOVE, time);
1371 gtk_drag_dest_set (GtkWidget *widget,
1372 GtkDestDefaults flags,
1373 const GtkTargetEntry *targets,
1375 GdkDragAction actions)
1377 GtkDragDestSite *site;
1379 g_return_if_fail (GTK_IS_WIDGET (widget));
1381 site = g_slice_new0 (GtkDragDestSite);
1383 site->flags = flags;
1384 site->have_drag = FALSE;
1386 site->target_list = gtk_target_list_new (targets, n_targets);
1388 site->target_list = NULL;
1389 site->actions = actions;
1390 site->do_proxy = FALSE;
1391 site->proxy_window = NULL;
1392 site->track_motion = FALSE;
1394 gtk_drag_dest_set_internal (widget, site);
1398 * gtk_drag_dest_set_proxy: (method)
1399 * @widget: a #GtkWidget
1400 * @proxy_window: the window to which to forward drag events
1401 * @protocol: the drag protocol which the @proxy_window accepts
1402 * (You can use gdk_drag_get_protocol() to determine this)
1403 * @use_coordinates: If %TRUE, send the same coordinates to the
1404 * destination, because it is an embedded
1407 * Sets this widget as a proxy for drops to another window.
1410 gtk_drag_dest_set_proxy (GtkWidget *widget,
1411 GdkWindow *proxy_window,
1412 GdkDragProtocol protocol,
1413 gboolean use_coordinates)
1415 GtkDragDestSite *site;
1417 g_return_if_fail (GTK_IS_WIDGET (widget));
1418 g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1420 site = g_slice_new (GtkDragDestSite);
1423 site->have_drag = FALSE;
1424 site->target_list = NULL;
1426 site->proxy_window = proxy_window;
1428 g_object_ref (proxy_window);
1429 site->do_proxy = TRUE;
1430 site->proxy_protocol = protocol;
1431 site->proxy_coords = use_coordinates;
1432 site->track_motion = FALSE;
1434 gtk_drag_dest_set_internal (widget, site);
1438 * gtk_drag_dest_unset: (method)
1439 * @widget: a #GtkWidget
1441 * Clears information about a drop destination set with
1442 * gtk_drag_dest_set(). The widget will no longer receive
1443 * notification of drags.
1446 gtk_drag_dest_unset (GtkWidget *widget)
1448 GtkDragDestSite *old_site;
1450 g_return_if_fail (GTK_IS_WIDGET (widget));
1452 old_site = g_object_get_data (G_OBJECT (widget),
1456 g_signal_handlers_disconnect_by_func (widget,
1457 gtk_drag_dest_realized,
1459 g_signal_handlers_disconnect_by_func (widget,
1460 gtk_drag_dest_hierarchy_changed,
1464 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1468 * gtk_drag_dest_get_target_list: (method)
1469 * @widget: a #GtkWidget
1471 * Returns the list of targets this widget can accept from
1474 * Return value: (transfer none): the #GtkTargetList, or %NULL if none
1477 gtk_drag_dest_get_target_list (GtkWidget *widget)
1479 GtkDragDestSite *site;
1481 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1483 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1485 return site ? site->target_list : NULL;
1489 * gtk_drag_dest_set_target_list: (method)
1490 * @widget: a #GtkWidget that's a drag destination
1491 * @target_list: (allow-none): list of droppable targets, or %NULL for none
1493 * Sets the target types that this widget can accept from drag-and-drop.
1494 * The widget must first be made into a drag destination with
1495 * gtk_drag_dest_set().
1498 gtk_drag_dest_set_target_list (GtkWidget *widget,
1499 GtkTargetList *target_list)
1501 GtkDragDestSite *site;
1503 g_return_if_fail (GTK_IS_WIDGET (widget));
1505 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1509 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1510 "to make the widget into a drag destination");
1515 gtk_target_list_ref (target_list);
1517 if (site->target_list)
1518 gtk_target_list_unref (site->target_list);
1520 site->target_list = target_list;
1524 * gtk_drag_dest_add_text_targets: (method)
1525 * @widget: a #GtkWidget that's a drag destination
1527 * Add the text targets supported by #GtkSelection to
1528 * the target list of the drag destination. The targets
1529 * are added with @info = 0. If you need another value,
1530 * use gtk_target_list_add_text_targets() and
1531 * gtk_drag_dest_set_target_list().
1536 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1538 GtkTargetList *target_list;
1540 target_list = gtk_drag_dest_get_target_list (widget);
1542 gtk_target_list_ref (target_list);
1544 target_list = gtk_target_list_new (NULL, 0);
1545 gtk_target_list_add_text_targets (target_list, 0);
1546 gtk_drag_dest_set_target_list (widget, target_list);
1547 gtk_target_list_unref (target_list);
1551 * gtk_drag_dest_add_image_targets: (method)
1552 * @widget: a #GtkWidget that's a drag destination
1554 * Add the image targets supported by #GtkSelection to
1555 * the target list of the drag destination. The targets
1556 * are added with @info = 0. If you need another value,
1557 * use gtk_target_list_add_image_targets() and
1558 * gtk_drag_dest_set_target_list().
1563 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1565 GtkTargetList *target_list;
1567 target_list = gtk_drag_dest_get_target_list (widget);
1569 gtk_target_list_ref (target_list);
1571 target_list = gtk_target_list_new (NULL, 0);
1572 gtk_target_list_add_image_targets (target_list, 0, FALSE);
1573 gtk_drag_dest_set_target_list (widget, target_list);
1574 gtk_target_list_unref (target_list);
1578 * gtk_drag_dest_add_uri_targets: (method)
1579 * @widget: a #GtkWidget that's a drag destination
1581 * Add the URI targets supported by #GtkSelection to
1582 * the target list of the drag destination. The targets
1583 * are added with @info = 0. If you need another value,
1584 * use gtk_target_list_add_uri_targets() and
1585 * gtk_drag_dest_set_target_list().
1590 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1592 GtkTargetList *target_list;
1594 target_list = gtk_drag_dest_get_target_list (widget);
1596 gtk_target_list_ref (target_list);
1598 target_list = gtk_target_list_new (NULL, 0);
1599 gtk_target_list_add_uri_targets (target_list, 0);
1600 gtk_drag_dest_set_target_list (widget, target_list);
1601 gtk_target_list_unref (target_list);
1605 * gtk_drag_dest_set_track_motion: (method)
1606 * @widget: a #GtkWidget that's a drag destination
1607 * @track_motion: whether to accept all targets
1609 * Tells the widget to emit #GtkWidget::drag-motion and
1610 * #GtkWidget::drag-leave events regardless of the targets and the
1611 * %GTK_DEST_DEFAULT_MOTION flag.
1613 * This may be used when a widget wants to do generic
1614 * actions regardless of the targets that the source offers.
1619 gtk_drag_dest_set_track_motion (GtkWidget *widget,
1620 gboolean track_motion)
1622 GtkDragDestSite *site;
1624 g_return_if_fail (GTK_IS_WIDGET (widget));
1626 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1628 g_return_if_fail (site != NULL);
1630 site->track_motion = track_motion != FALSE;
1634 * gtk_drag_dest_get_track_motion: (method)
1635 * @widget: a #GtkWidget that's a drag destination
1637 * Returns whether the widget has been configured to always
1638 * emit #GtkWidget::drag-motion signals.
1640 * Return Value: %TRUE if the widget always emits
1641 * #GtkWidget::drag-motion events
1646 gtk_drag_dest_get_track_motion (GtkWidget *widget)
1648 GtkDragDestSite *site;
1650 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1652 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1655 return site->track_motion;
1660 /*************************************************************
1661 * _gtk_drag_dest_handle_event:
1662 * Called from widget event handling code on Drag events
1666 * toplevel: Toplevel widget that received the event
1669 *************************************************************/
1672 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1675 GtkDragDestInfo *info;
1676 GdkDragContext *context;
1678 g_return_if_fail (toplevel != NULL);
1679 g_return_if_fail (event != NULL);
1681 context = event->dnd.context;
1683 info = gtk_drag_get_dest_info (context, TRUE);
1685 /* Find the widget for the event */
1686 switch (event->type)
1688 case GDK_DRAG_ENTER:
1691 case GDK_DRAG_LEAVE:
1694 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1695 info->widget = NULL;
1699 case GDK_DRAG_MOTION:
1700 case GDK_DROP_START:
1706 if (event->type == GDK_DROP_START)
1708 info->dropped = TRUE;
1709 /* We send a leave here so that the widget unhighlights
1714 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1715 info->widget = NULL;
1719 window = gtk_widget_get_window (toplevel);
1721 #ifdef GDK_WINDOWING_X11
1722 /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1724 * Currently gdk_window_get_position doesn't provide reliable
1725 * information for embedded windows, so we call the much more
1726 * expensive gdk_window_get_origin().
1728 if (GTK_IS_PLUG (toplevel))
1729 gdk_window_get_origin (window, &tx, &ty);
1731 #endif /* GDK_WINDOWING_X11 */
1732 gdk_window_get_position (window, &tx, &ty);
1734 found = gtk_drag_find_widget (toplevel,
1737 event->dnd.x_root - tx,
1738 event->dnd.y_root - ty,
1740 (event->type == GDK_DRAG_MOTION) ?
1741 gtk_drag_dest_motion :
1742 gtk_drag_dest_drop);
1744 if (info->widget && !found)
1746 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1747 info->widget = NULL;
1752 if (event->type == GDK_DRAG_MOTION)
1755 gdk_drag_status (context, 0, event->dnd.time);
1757 else if (event->type == GDK_DROP_START && !info->proxy_source)
1759 gdk_drop_reply (context, found, event->dnd.time);
1760 if ((gdk_drag_context_get_protocol (context) == GDK_DRAG_PROTO_MOTIF) && !found)
1761 gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1767 g_assert_not_reached ();
1772 * gtk_drag_dest_find_target: (method)
1773 * @widget: drag destination widget
1774 * @context: drag context
1775 * @target_list: (allow-none): list of droppable targets, or %NULL to use
1776 * gtk_drag_dest_get_target_list (@widget).
1778 * Looks for a match between the supported targets of @context and the
1779 * @dest_target_list, returning the first matching target, otherwise
1780 * returning %GDK_NONE. @dest_target_list should usually be the return
1781 * value from gtk_drag_dest_get_target_list(), but some widgets may
1782 * have different valid targets for different parts of the widget; in
1783 * that case, they will have to implement a drag_motion handler that
1784 * passes the correct target list to this function.
1786 * Return value: (transfer none): first target that the source offers
1787 * and the dest can accept, or %GDK_NONE
1790 gtk_drag_dest_find_target (GtkWidget *widget,
1791 GdkDragContext *context,
1792 GtkTargetList *target_list)
1795 GList *tmp_source = NULL;
1796 GtkWidget *source_widget;
1798 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1799 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1802 source_widget = gtk_drag_get_source_widget (context);
1804 if (target_list == NULL)
1805 target_list = gtk_drag_dest_get_target_list (widget);
1807 if (target_list == NULL)
1810 tmp_target = target_list->list;
1813 GtkTargetPair *pair = tmp_target->data;
1814 tmp_source = gdk_drag_context_list_targets (context);
1817 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1819 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1820 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)) &&
1821 (!(pair->flags & GTK_TARGET_OTHER_APP) || !source_widget) &&
1822 (!(pair->flags & GTK_TARGET_OTHER_WIDGET) || (source_widget != widget)))
1823 return pair->target;
1827 tmp_source = tmp_source->next;
1829 tmp_target = tmp_target->next;
1836 gtk_drag_selection_received (GtkWidget *widget,
1837 GtkSelectionData *selection_data,
1841 GdkDragContext *context;
1842 GtkDragDestInfo *info;
1843 GtkWidget *drop_widget;
1848 context = g_object_get_data (G_OBJECT (widget), "drag-context");
1849 info = gtk_drag_get_dest_info (context, FALSE);
1851 if (info->proxy_data &&
1852 gtk_selection_data_get_target (info->proxy_data) == gtk_selection_data_get_target (selection_data))
1854 gtk_selection_data_set (info->proxy_data,
1855 gtk_selection_data_get_data_type (selection_data),
1856 gtk_selection_data_get_format (selection_data),
1857 gtk_selection_data_get_data (selection_data),
1858 gtk_selection_data_get_length (selection_data));
1863 target = gtk_selection_data_get_target (selection_data);
1864 if (target == gdk_atom_intern_static_string ("DELETE"))
1866 gtk_drag_finish (context, TRUE, FALSE, time);
1868 else if ((target == gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS")) ||
1869 (target == gdk_atom_intern_static_string ("XmTRANSFER_FAILURE")))
1875 GtkDragDestSite *site;
1877 site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1879 if (site && site->target_list)
1883 if (gtk_target_list_find (site->target_list,
1887 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1888 gtk_selection_data_get_length (selection_data) >= 0)
1889 g_signal_emit_by_name (drop_widget,
1890 "drag-data-received",
1891 context, info->drop_x, info->drop_y,
1898 g_signal_emit_by_name (drop_widget,
1899 "drag-data-received",
1900 context, info->drop_x, info->drop_y,
1905 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1908 gtk_drag_finish (context,
1909 (gtk_selection_data_get_length (selection_data) >= 0),
1910 (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
1914 g_object_unref (drop_widget);
1917 g_signal_handlers_disconnect_by_func (widget,
1918 gtk_drag_selection_received,
1921 g_object_set_data (G_OBJECT (widget), I_("drag-context"), NULL);
1922 g_object_unref (context);
1924 gtk_drag_release_ipc_widget (widget);
1927 /*************************************************************
1928 * gtk_drag_find_widget:
1929 * Function used to locate widgets for
1930 * DRAG_MOTION and DROP_START events.
1931 *************************************************************/
1934 gtk_drag_find_widget (GtkWidget *widget,
1935 GdkDragContext *context,
1936 GtkDragDestInfo *info,
1940 GtkDragDestCallback callback)
1942 if (!gtk_widget_get_mapped (widget) ||
1943 !gtk_widget_get_sensitive (widget))
1946 /* Get the widget at the pointer coordinates and travel up
1947 * the widget hierarchy from there.
1949 widget = _gtk_widget_find_at_coords (gtk_widget_get_window (widget),
1957 GList *hierarchy = NULL;
1958 gboolean found = FALSE;
1960 if (!gtk_widget_get_mapped (widget) ||
1961 !gtk_widget_get_sensitive (widget))
1964 /* need to reference the entire hierarchy temporarily in case the
1965 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
1967 for (parent = widget;
1969 parent = gtk_widget_get_parent (parent))
1971 hierarchy = g_list_prepend (hierarchy, g_object_ref (parent));
1974 /* If the current widget is registered as a drop site, check to
1975 * emit "drag-motion" to check if we are actually in a drop
1978 if (g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1980 found = callback (widget, context, x, y, time);
1982 /* If so, send a "drag-leave" to the last widget */
1985 if (info->widget && info->widget != widget)
1987 gtk_drag_dest_leave (info->widget, context, time);
1990 info->widget = widget;
1996 /* Get the parent before unreffing the hierarchy because
1997 * invoking the callback might have destroyed the widget
1999 parent = gtk_widget_get_parent (widget);
2001 /* The parent might be going away when unreffing the
2002 * hierarchy, so also protect againt that
2005 g_object_add_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
2008 g_list_foreach (hierarchy, (GFunc) g_object_unref, NULL);
2009 g_list_free (hierarchy);
2015 g_object_remove_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
2019 if (!gtk_widget_translate_coordinates (widget, parent, x, y, &x, &y))
2029 gtk_drag_proxy_begin (GtkWidget *widget,
2030 GtkDragDestInfo *dest_info,
2033 GtkDragSourceInfo *source_info;
2035 GdkDragContext *context;
2036 GtkWidget *ipc_widget;
2038 if (dest_info->proxy_source)
2040 gdk_drag_abort (dest_info->proxy_source->context, time);
2041 gtk_drag_source_info_destroy (dest_info->proxy_source);
2042 dest_info->proxy_source = NULL;
2045 ipc_widget = gtk_drag_get_ipc_widget (widget);
2046 context = gdk_drag_begin (gtk_widget_get_window (ipc_widget),
2047 gdk_drag_context_list_targets (dest_info->context));
2049 source_info = gtk_drag_get_source_info (context, TRUE);
2051 source_info->ipc_widget = ipc_widget;
2052 source_info->widget = g_object_ref (widget);
2054 source_info->target_list = gtk_target_list_new (NULL, 0);
2055 tmp_list = gdk_drag_context_list_targets (dest_info->context);
2058 gtk_target_list_add (source_info->target_list,
2059 GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
2060 tmp_list = tmp_list->next;
2063 source_info->proxy_dest = dest_info;
2065 g_signal_connect (ipc_widget,
2067 G_CALLBACK (gtk_drag_selection_get),
2070 dest_info->proxy_source = source_info;
2074 gtk_drag_dest_info_destroy (gpointer data)
2076 g_slice_free (GtkDragDestInfo, data);
2079 static GtkDragDestInfo *
2080 gtk_drag_get_dest_info (GdkDragContext *context,
2083 GtkDragDestInfo *info;
2084 static GQuark info_quark = 0;
2086 info_quark = g_quark_from_static_string ("gtk-dest-info");
2088 info = g_object_get_qdata (G_OBJECT (context), info_quark);
2089 if (!info && create)
2091 info = g_slice_new0 (GtkDragDestInfo);
2092 info->context = context;
2093 g_object_set_qdata_full (G_OBJECT (context), info_quark,
2094 info, gtk_drag_dest_info_destroy);
2100 static GQuark dest_info_quark = 0;
2102 static GtkDragSourceInfo *
2103 gtk_drag_get_source_info (GdkDragContext *context,
2106 GtkDragSourceInfo *info;
2107 if (!dest_info_quark)
2108 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
2110 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
2111 if (!info && create)
2113 info = g_new0 (GtkDragSourceInfo, 1);
2114 info->context = context;
2115 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
2122 gtk_drag_clear_source_info (GdkDragContext *context)
2124 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
2128 gtk_drag_dest_realized (GtkWidget *widget)
2130 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2132 if (gtk_widget_is_toplevel (toplevel))
2133 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2137 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
2138 GtkWidget *previous_toplevel)
2140 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2142 if (gtk_widget_is_toplevel (toplevel) && gtk_widget_get_realized (toplevel))
2143 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2147 gtk_drag_dest_site_destroy (gpointer data)
2149 GtkDragDestSite *site = data;
2151 if (site->proxy_window)
2152 g_object_unref (site->proxy_window);
2154 if (site->target_list)
2155 gtk_target_list_unref (site->target_list);
2157 g_slice_free (GtkDragDestSite, site);
2161 * Default drag handlers
2164 gtk_drag_dest_leave (GtkWidget *widget,
2165 GdkDragContext *context,
2168 GtkDragDestSite *site;
2170 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2171 g_return_if_fail (site != NULL);
2175 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2177 if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
2179 gdk_drag_abort (info->proxy_source->context, time);
2180 gtk_drag_source_info_destroy (info->proxy_source);
2181 info->proxy_source = NULL;
2188 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
2189 gtk_drag_unhighlight (widget);
2191 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
2193 g_signal_emit_by_name (widget, "drag-leave", context, time);
2195 site->have_drag = FALSE;
2200 gtk_drag_dest_motion (GtkWidget *widget,
2201 GdkDragContext *context,
2206 GtkDragDestSite *site;
2207 GdkDragAction action = 0;
2210 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2211 g_return_val_if_fail (site != NULL, FALSE);
2216 GdkEvent *current_event;
2217 GdkWindow *dest_window;
2218 GdkDragProtocol proto;
2220 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2222 if (!info->proxy_source || info->proxy_source->widget != widget)
2223 gtk_drag_proxy_begin (widget, info, time);
2225 current_event = gtk_get_current_event ();
2227 if (site->proxy_window)
2229 dest_window = site->proxy_window;
2230 proto = site->proxy_protocol;
2234 gdk_drag_find_window_for_screen (info->proxy_source->context,
2236 gdk_window_get_screen (current_event->dnd.window),
2237 current_event->dnd.x_root,
2238 current_event->dnd.y_root,
2239 &dest_window, &proto);
2242 gdk_drag_motion (info->proxy_source->context,
2244 current_event->dnd.x_root,
2245 current_event->dnd.y_root,
2246 gdk_drag_context_get_suggested_action (context),
2247 gdk_drag_context_get_actions (context),
2250 if (!site->proxy_window && dest_window)
2251 g_object_unref (dest_window);
2253 selection = gdk_drag_get_selection (info->proxy_source->context);
2255 selection != gdk_drag_get_selection (info->context))
2256 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2258 gdk_event_free (current_event);
2263 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
2265 if (gdk_drag_context_get_suggested_action (context) & site->actions)
2266 action = gdk_drag_context_get_suggested_action (context);
2271 for (i = 0; i < 8; i++)
2273 if ((site->actions & (1 << i)) &&
2274 (gdk_drag_context_get_actions (context) & (1 << i)))
2282 if (action && gtk_drag_dest_find_target (widget, context, NULL))
2284 if (!site->have_drag)
2286 site->have_drag = TRUE;
2287 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
2288 gtk_drag_highlight (widget);
2291 gdk_drag_status (context, action, time);
2295 gdk_drag_status (context, 0, time);
2296 if (!site->track_motion)
2301 g_signal_emit_by_name (widget, "drag-motion",
2302 context, x, y, time, &retval);
2304 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
2308 gtk_drag_dest_drop (GtkWidget *widget,
2309 GdkDragContext *context,
2314 GtkDragDestSite *site;
2315 GtkDragDestInfo *info;
2317 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2318 g_return_val_if_fail (site != NULL, FALSE);
2320 info = gtk_drag_get_dest_info (context, FALSE);
2321 g_return_val_if_fail (info != NULL, FALSE);
2328 if (info->proxy_source ||
2329 (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_ROOTWIN))
2331 gtk_drag_drop (info->proxy_source, time);
2335 /* We need to synthesize a motion event, wait for a status,
2336 * and, if we get a good one, do a drop.
2339 GdkEvent *current_event;
2341 GdkWindow *dest_window;
2342 GdkDragProtocol proto;
2344 gtk_drag_proxy_begin (widget, info, time);
2345 info->proxy_drop_wait = TRUE;
2346 info->proxy_drop_time = time;
2348 current_event = gtk_get_current_event ();
2350 if (site->proxy_window)
2352 dest_window = site->proxy_window;
2353 proto = site->proxy_protocol;
2357 gdk_drag_find_window_for_screen (info->proxy_source->context,
2359 gdk_window_get_screen (current_event->dnd.window),
2360 current_event->dnd.x_root,
2361 current_event->dnd.y_root,
2362 &dest_window, &proto);
2365 gdk_drag_motion (info->proxy_source->context,
2367 current_event->dnd.x_root,
2368 current_event->dnd.y_root,
2369 gdk_drag_context_get_suggested_action (context),
2370 gdk_drag_context_get_actions (context),
2373 if (!site->proxy_window && dest_window)
2374 g_object_unref (dest_window);
2376 selection = gdk_drag_get_selection (info->proxy_source->context);
2378 selection != gdk_drag_get_selection (info->context))
2379 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2381 gdk_event_free (current_event);
2390 if (site->flags & GTK_DEST_DEFAULT_DROP)
2392 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2394 if (target == GDK_NONE)
2396 gtk_drag_finish (context, FALSE, FALSE, time);
2400 gtk_drag_get_data (widget, context, target, time);
2403 g_signal_emit_by_name (widget, "drag-drop",
2404 context, x, y, time, &retval);
2406 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2414 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2415 * so that we can set the icon from the source site information
2417 static GdkDragContext *
2418 gtk_drag_begin_internal (GtkWidget *widget,
2419 GtkDragSourceSite *site,
2420 GtkTargetList *target_list,
2421 GdkDragAction actions,
2425 GtkDragSourceInfo *info;
2426 GList *targets = NULL;
2428 guint32 time = GDK_CURRENT_TIME;
2429 GdkDragAction possible_actions, suggested_action;
2430 GdkDragContext *context;
2431 GtkWidget *ipc_widget;
2433 GdkDevice *pointer, *keyboard;
2434 GdkWindow *ipc_window;
2436 pointer = keyboard = NULL;
2437 ipc_widget = gtk_drag_get_ipc_widget (widget);
2439 gtk_drag_get_event_actions (event, button, actions,
2440 &suggested_action, &possible_actions);
2442 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2448 time = gdk_event_get_time (event);
2449 if (time == GDK_CURRENT_TIME)
2450 time = gtk_get_current_event_time ();
2452 pointer = gdk_event_get_device (event);
2454 if (gdk_device_get_source (pointer) == GDK_SOURCE_KEYBOARD)
2457 pointer = gdk_device_get_associated_device (keyboard);
2460 keyboard = gdk_device_get_associated_device (pointer);
2464 GdkDeviceManager *device_manager;
2466 device_manager = gdk_display_get_device_manager (gtk_widget_get_display (widget));
2467 pointer = gdk_device_manager_get_client_pointer (device_manager);
2468 keyboard = gdk_device_get_associated_device (pointer);
2474 ipc_window = gtk_widget_get_window (ipc_widget);
2476 if (gdk_device_grab (pointer, ipc_window,
2477 GDK_OWNERSHIP_APPLICATION, FALSE,
2478 GDK_POINTER_MOTION_MASK |
2479 GDK_BUTTON_RELEASE_MASK,
2480 cursor, time) != GDK_GRAB_SUCCESS)
2482 gtk_drag_release_ipc_widget (ipc_widget);
2487 grab_dnd_keys (ipc_widget, keyboard, time);
2489 /* We use a GTK grab here to override any grabs that the widget
2490 * we are dragging from might have held
2492 gtk_device_grab_add (ipc_widget, pointer, FALSE);
2494 tmp_list = g_list_last (target_list->list);
2497 GtkTargetPair *pair = tmp_list->data;
2498 targets = g_list_prepend (targets,
2499 GINT_TO_POINTER (pair->target));
2500 tmp_list = tmp_list->prev;
2503 source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2505 context = gdk_drag_begin (ipc_window, targets);
2506 gdk_drag_context_set_device (context, pointer);
2507 g_list_free (targets);
2509 info = gtk_drag_get_source_info (context, TRUE);
2511 info->ipc_widget = ipc_widget;
2512 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), info);
2514 info->widget = g_object_ref (widget);
2516 info->button = button;
2517 info->cursor = cursor;
2518 info->target_list = target_list;
2519 gtk_target_list_ref (target_list);
2521 info->possible_actions = actions;
2523 info->status = GTK_DRAG_STATUS_DRAG;
2524 info->last_event = NULL;
2525 info->selections = NULL;
2526 info->icon_window = NULL;
2527 info->destroy_icon = FALSE;
2529 /* Set cur_x, cur_y here so if the "drag-begin" signal shows
2530 * the drag icon, it will be in the right place
2532 if (event && event->type == GDK_MOTION_NOTIFY)
2534 info->cur_screen = gtk_widget_get_screen (widget);
2535 info->cur_x = event->motion.x_root;
2536 info->cur_y = event->motion.y_root;
2540 gdk_device_get_position (pointer, &info->cur_screen, &info->cur_x, &info->cur_y);
2543 g_signal_emit_by_name (widget, "drag-begin", info->context);
2545 /* Ensure that we have an icon before we start the drag; the
2546 * application may have set one in ::drag_begin, or it may
2549 if (!info->icon_window && !info->icon_pixbuf)
2551 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2552 gtk_drag_set_icon_default (context);
2554 switch (site->icon_type)
2556 case GTK_IMAGE_PIXBUF:
2557 gtk_drag_set_icon_pixbuf (context,
2558 site->icon_data.pixbuf.pixbuf,
2561 case GTK_IMAGE_STOCK:
2562 gtk_drag_set_icon_stock (context,
2563 site->icon_data.stock.stock_id,
2566 case GTK_IMAGE_ICON_NAME:
2567 gtk_drag_set_icon_name (context,
2568 site->icon_data.name.icon_name,
2571 case GTK_IMAGE_GICON:
2572 gtk_drag_set_icon_gicon (context,
2573 site->icon_data.gicon.icon,
2576 case GTK_IMAGE_EMPTY:
2578 g_assert_not_reached();
2583 /* We need to composite the icon into the cursor, if we are
2584 * not using an icon window.
2586 if (info->icon_pixbuf)
2588 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2592 if (cursor != info->cursor)
2594 gdk_device_grab (pointer, gtk_widget_get_window (widget),
2595 GDK_OWNERSHIP_APPLICATION, FALSE,
2596 GDK_POINTER_MOTION_MASK |
2597 GDK_BUTTON_RELEASE_MASK,
2599 info->cursor = cursor;
2603 if (event && event->type == GDK_MOTION_NOTIFY)
2604 gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2606 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, event);
2608 info->start_x = info->cur_x;
2609 info->start_y = info->cur_y;
2611 g_signal_connect (info->ipc_widget, "grab-broken-event",
2612 G_CALLBACK (gtk_drag_grab_broken_event_cb), info);
2613 g_signal_connect (info->ipc_widget, "grab-notify",
2614 G_CALLBACK (gtk_drag_grab_notify_cb), info);
2615 g_signal_connect (info->ipc_widget, "button-release-event",
2616 G_CALLBACK (gtk_drag_button_release_cb), info);
2617 g_signal_connect (info->ipc_widget, "motion-notify-event",
2618 G_CALLBACK (gtk_drag_motion_cb), info);
2619 g_signal_connect (info->ipc_widget, "key-press-event",
2620 G_CALLBACK (gtk_drag_key_cb), info);
2621 g_signal_connect (info->ipc_widget, "key-release-event",
2622 G_CALLBACK (gtk_drag_key_cb), info);
2623 g_signal_connect (info->ipc_widget, "selection-get",
2624 G_CALLBACK (gtk_drag_selection_get), info);
2626 info->have_grab = TRUE;
2627 info->grab_time = time;
2629 return info->context;
2633 * gtk_drag_begin: (method)
2634 * @widget: the source widget.
2635 * @targets: The targets (data formats) in which the
2636 * source can provide the data.
2637 * @actions: A bitmask of the allowed drag actions for this drag.
2638 * @button: The button the user clicked to start the drag.
2639 * @event: The event that triggered the start of the drag.
2641 * Initiates a drag on the source side. The function
2642 * only needs to be used when the application is
2643 * starting drags itself, and is not needed when
2644 * gtk_drag_source_set() is used.
2646 * The @event is used to retrieve the timestamp that will be used internally to
2647 * grab the pointer. If @event is #NULL, then GDK_CURRENT_TIME will be used.
2648 * However, you should try to pass a real event in all cases, since that can be
2649 * used by GTK+ to get information about the start position of the drag, for
2650 * example if the @event is a %GDK_MOTION_NOTIFY.
2652 * Generally there are three cases when you want to start a drag by hand by
2653 * calling this function:
2655 * 1. During a #GtkWidget::button-press-event handler, if you want to start a drag
2656 * immediately when the user presses the mouse button. Pass the @event
2657 * that you have in your #GtkWidget::button-press-event handler.
2659 * 2. During a #GtkWidget::motion-notify-event handler, if you want to start a drag
2660 * when the mouse moves past a certain threshold distance after a button-press.
2661 * Pass the @event that you have in your #GtkWidget::motion-notify-event handler.
2663 * 3. During a timeout handler, if you want to start a drag after the mouse
2664 * button is held down for some time. Try to save the last event that you got
2665 * from the mouse, using gdk_event_copy(), and pass it to this function
2666 * (remember to free the event with gdk_event_free() when you are done).
2667 * If you can really not pass a real event, pass #NULL instead.
2669 * Return value: (transfer none): the context for this drag.
2672 gtk_drag_begin (GtkWidget *widget,
2673 GtkTargetList *targets,
2674 GdkDragAction actions,
2678 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2679 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
2680 g_return_val_if_fail (targets != NULL, NULL);
2682 return gtk_drag_begin_internal (widget, NULL, targets,
2683 actions, button, event);
2687 * gtk_drag_source_set: (method)
2688 * @widget: a #GtkWidget
2689 * @start_button_mask: the bitmask of buttons that can start the drag
2690 * @targets: (allow-none) (array length=n_targets): the table of targets that the drag will support,
2692 * @n_targets: the number of items in @targets
2693 * @actions: the bitmask of possible actions for a drag from this widget
2695 * Sets up a widget so that GTK+ will start a drag operation when the user
2696 * clicks and drags on the widget. The widget must have a window.
2699 gtk_drag_source_set (GtkWidget *widget,
2700 GdkModifierType start_button_mask,
2701 const GtkTargetEntry *targets,
2703 GdkDragAction actions)
2705 GtkDragSourceSite *site;
2707 g_return_if_fail (GTK_IS_WIDGET (widget));
2709 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2711 gtk_widget_add_events (widget,
2712 gtk_widget_get_events (widget) |
2713 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2714 GDK_BUTTON_MOTION_MASK);
2718 if (site->target_list)
2719 gtk_target_list_unref (site->target_list);
2723 site = g_slice_new0 (GtkDragSourceSite);
2725 site->icon_type = GTK_IMAGE_EMPTY;
2727 g_signal_connect (widget, "button-press-event",
2728 G_CALLBACK (gtk_drag_source_event_cb),
2730 g_signal_connect (widget, "button-release-event",
2731 G_CALLBACK (gtk_drag_source_event_cb),
2733 g_signal_connect (widget, "motion-notify-event",
2734 G_CALLBACK (gtk_drag_source_event_cb),
2737 g_object_set_data_full (G_OBJECT (widget),
2738 I_("gtk-site-data"),
2739 site, gtk_drag_source_site_destroy);
2742 site->start_button_mask = start_button_mask;
2744 site->target_list = gtk_target_list_new (targets, n_targets);
2746 site->actions = actions;
2750 * gtk_drag_source_unset: (method)
2751 * @widget: a #GtkWidget
2753 * Undoes the effects of gtk_drag_source_set().
2756 gtk_drag_source_unset (GtkWidget *widget)
2758 GtkDragSourceSite *site;
2760 g_return_if_fail (GTK_IS_WIDGET (widget));
2762 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2766 g_signal_handlers_disconnect_by_func (widget,
2767 gtk_drag_source_event_cb,
2769 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
2774 * gtk_drag_source_get_target_list: (method)
2775 * @widget: a #GtkWidget
2777 * Gets the list of targets this widget can provide for
2780 * Return value: (transfer none): the #GtkTargetList, or %NULL if none
2785 gtk_drag_source_get_target_list (GtkWidget *widget)
2787 GtkDragSourceSite *site;
2789 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2791 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2793 return site ? site->target_list : NULL;
2797 * gtk_drag_source_set_target_list: (method)
2798 * @widget: a #GtkWidget that's a drag source
2799 * @target_list: (allow-none): list of draggable targets, or %NULL for none
2801 * Changes the target types that this widget offers for drag-and-drop.
2802 * The widget must first be made into a drag source with
2803 * gtk_drag_source_set().
2808 gtk_drag_source_set_target_list (GtkWidget *widget,
2809 GtkTargetList *target_list)
2811 GtkDragSourceSite *site;
2813 g_return_if_fail (GTK_IS_WIDGET (widget));
2815 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2818 g_warning ("gtk_drag_source_set_target_list() requires the widget "
2819 "to already be a drag source.");
2824 gtk_target_list_ref (target_list);
2826 if (site->target_list)
2827 gtk_target_list_unref (site->target_list);
2829 site->target_list = target_list;
2833 * gtk_drag_source_add_text_targets: (method)
2834 * @widget: a #GtkWidget that's is a drag source
2836 * Add the text targets supported by #GtkSelection to
2837 * the target list of the drag source. The targets
2838 * are added with @info = 0. If you need another value,
2839 * use gtk_target_list_add_text_targets() and
2840 * gtk_drag_source_set_target_list().
2845 gtk_drag_source_add_text_targets (GtkWidget *widget)
2847 GtkTargetList *target_list;
2849 target_list = gtk_drag_source_get_target_list (widget);
2851 gtk_target_list_ref (target_list);
2853 target_list = gtk_target_list_new (NULL, 0);
2854 gtk_target_list_add_text_targets (target_list, 0);
2855 gtk_drag_source_set_target_list (widget, target_list);
2856 gtk_target_list_unref (target_list);
2860 * gtk_drag_source_add_image_targets: (method)
2861 * @widget: a #GtkWidget that's is a drag source
2863 * Add the writable image targets supported by #GtkSelection to
2864 * the target list of the drag source. The targets
2865 * are added with @info = 0. If you need another value,
2866 * use gtk_target_list_add_image_targets() and
2867 * gtk_drag_source_set_target_list().
2872 gtk_drag_source_add_image_targets (GtkWidget *widget)
2874 GtkTargetList *target_list;
2876 target_list = gtk_drag_source_get_target_list (widget);
2878 gtk_target_list_ref (target_list);
2880 target_list = gtk_target_list_new (NULL, 0);
2881 gtk_target_list_add_image_targets (target_list, 0, TRUE);
2882 gtk_drag_source_set_target_list (widget, target_list);
2883 gtk_target_list_unref (target_list);
2887 * gtk_drag_source_add_uri_targets: (method)
2888 * @widget: a #GtkWidget that's is a drag source
2890 * Add the URI targets supported by #GtkSelection to
2891 * the target list of the drag source. The targets
2892 * are added with @info = 0. If you need another value,
2893 * use gtk_target_list_add_uri_targets() and
2894 * gtk_drag_source_set_target_list().
2899 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2901 GtkTargetList *target_list;
2903 target_list = gtk_drag_source_get_target_list (widget);
2905 gtk_target_list_ref (target_list);
2907 target_list = gtk_target_list_new (NULL, 0);
2908 gtk_target_list_add_uri_targets (target_list, 0);
2909 gtk_drag_source_set_target_list (widget, target_list);
2910 gtk_target_list_unref (target_list);
2914 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2916 switch (site->icon_type)
2918 case GTK_IMAGE_EMPTY:
2920 case GTK_IMAGE_PIXBUF:
2921 g_object_unref (site->icon_data.pixbuf.pixbuf);
2923 case GTK_IMAGE_STOCK:
2924 g_free (site->icon_data.stock.stock_id);
2926 case GTK_IMAGE_ICON_NAME:
2927 g_free (site->icon_data.name.icon_name);
2929 case GTK_IMAGE_GICON:
2930 _gtk_image_gicon_data_clear (&(site->icon_data.gicon));
2933 g_assert_not_reached();
2936 site->icon_type = GTK_IMAGE_EMPTY;
2940 * gtk_drag_source_set_icon_pixbuf: (method)
2941 * @widget: a #GtkWidget
2942 * @pixbuf: the #GdkPixbuf for the drag icon
2944 * Sets the icon that will be used for drags from a particular widget
2945 * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
2946 * release it when it is no longer needed.
2949 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
2952 GtkDragSourceSite *site;
2954 g_return_if_fail (GTK_IS_WIDGET (widget));
2955 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2957 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2958 g_return_if_fail (site != NULL);
2959 g_object_ref (pixbuf);
2961 gtk_drag_source_unset_icon (site);
2963 site->icon_type = GTK_IMAGE_PIXBUF;
2964 site->icon_data.pixbuf.pixbuf = pixbuf;
2968 * gtk_drag_source_set_icon_stock: (method)
2969 * @widget: a #GtkWidget
2970 * @stock_id: the ID of the stock icon to use
2972 * Sets the icon that will be used for drags from a particular source
2976 gtk_drag_source_set_icon_stock (GtkWidget *widget,
2977 const gchar *stock_id)
2979 GtkDragSourceSite *site;
2981 g_return_if_fail (GTK_IS_WIDGET (widget));
2982 g_return_if_fail (stock_id != NULL);
2984 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2985 g_return_if_fail (site != NULL);
2987 gtk_drag_source_unset_icon (site);
2989 site->icon_type = GTK_IMAGE_STOCK;
2990 site->icon_data.stock.stock_id = g_strdup (stock_id);
2994 * gtk_drag_source_set_icon_name: (method)
2995 * @widget: a #GtkWidget
2996 * @icon_name: name of icon to use
2998 * Sets the icon that will be used for drags from a particular source
2999 * to a themed icon. See the docs for #GtkIconTheme for more details.
3004 gtk_drag_source_set_icon_name (GtkWidget *widget,
3005 const gchar *icon_name)
3007 GtkDragSourceSite *site;
3009 g_return_if_fail (GTK_IS_WIDGET (widget));
3010 g_return_if_fail (icon_name != NULL);
3012 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
3013 g_return_if_fail (site != NULL);
3015 gtk_drag_source_unset_icon (site);
3017 site->icon_type = GTK_IMAGE_ICON_NAME;
3018 site->icon_data.name.icon_name = g_strdup (icon_name);
3022 * gtk_drag_source_set_icon_gicon: (method)
3023 * @widget: a #GtkWidget
3026 * Sets the icon that will be used for drags from a particular source
3027 * to @icon. See the docs for #GtkIconTheme for more details.
3032 gtk_drag_source_set_icon_gicon (GtkWidget *widget,
3035 GtkDragSourceSite *site;
3037 g_return_if_fail (GTK_IS_WIDGET (widget));
3038 g_return_if_fail (icon != NULL);
3040 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
3041 g_return_if_fail (site != NULL);
3043 gtk_drag_source_unset_icon (site);
3045 site->icon_type = GTK_IMAGE_GICON;
3046 site->icon_data.gicon.icon = g_object_ref (icon);
3050 gtk_drag_get_icon (GtkDragSourceInfo *info,
3051 GtkWidget **icon_window,
3055 if (get_can_change_screen (info->icon_window))
3056 gtk_window_set_screen (GTK_WINDOW (info->icon_window),
3059 if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
3061 if (!info->fallback_icon)
3063 gint save_hot_x, save_hot_y;
3064 gboolean save_destroy_icon;
3065 GtkWidget *save_icon_window;
3067 /* HACK to get the appropriate icon
3069 save_icon_window = info->icon_window;
3070 save_hot_x = info->hot_x;
3071 save_hot_y = info->hot_x;
3072 save_destroy_icon = info->destroy_icon;
3074 info->icon_window = NULL;
3075 set_icon_stock_pixbuf (info->context,
3076 GTK_STOCK_DND, NULL, -2, -2, TRUE);
3077 info->fallback_icon = info->icon_window;
3079 info->icon_window = save_icon_window;
3080 info->hot_x = save_hot_x;
3081 info->hot_y = save_hot_y;
3082 info->destroy_icon = save_destroy_icon;
3085 gtk_widget_hide (info->icon_window);
3087 *icon_window = info->fallback_icon;
3088 gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
3095 if (info->fallback_icon)
3096 gtk_widget_hide (info->fallback_icon);
3098 *icon_window = info->icon_window;
3099 *hot_x = info->hot_x;
3100 *hot_y = info->hot_y;
3105 gtk_drag_update_icon (GtkDragSourceInfo *info)
3107 if (info->icon_window)
3109 GtkWidget *icon_window;
3112 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
3114 gtk_window_move (GTK_WINDOW (icon_window),
3115 info->cur_x - hot_x,
3116 info->cur_y - hot_y);
3118 if (gtk_widget_get_visible (icon_window))
3119 gdk_window_raise (gtk_widget_get_window (icon_window));
3121 gtk_widget_show (icon_window);
3126 gtk_drag_set_icon_window (GdkDragContext *context,
3130 gboolean destroy_on_release)
3132 GtkDragSourceInfo *info;
3134 info = gtk_drag_get_source_info (context, FALSE);
3137 if (destroy_on_release)
3138 gtk_widget_destroy (widget);
3142 gtk_drag_remove_icon (info);
3145 g_object_ref (widget);
3147 info->icon_window = widget;
3148 info->hot_x = hot_x;
3149 info->hot_y = hot_y;
3150 info->destroy_icon = destroy_on_release;
3152 if (widget && info->icon_pixbuf)
3154 g_object_unref (info->icon_pixbuf);
3155 info->icon_pixbuf = NULL;
3158 gtk_drag_update_cursor (info);
3159 gtk_drag_update_icon (info);
3163 * gtk_drag_set_icon_widget: (method)
3164 * @context: the context for a drag. (This must be called
3165 with a context for the source side of a drag)
3166 * @widget: a toplevel window to use as an icon.
3167 * @hot_x: the X offset within @widget of the hotspot.
3168 * @hot_y: the Y offset within @widget of the hotspot.
3170 * Changes the icon for a widget to a given widget. GTK+
3171 * will not destroy the icon, so if you don't want
3172 * it to persist, you should connect to the "drag-end"
3173 * signal and destroy it yourself.
3176 gtk_drag_set_icon_widget (GdkDragContext *context,
3181 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3182 g_return_if_fail (GTK_IS_WIDGET (widget));
3184 gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
3188 icon_window_realize (GtkWidget *window,
3191 cairo_surface_t *surface;
3192 cairo_pattern_t *pattern;
3195 surface = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3196 CAIRO_CONTENT_COLOR,
3197 gdk_pixbuf_get_width (pixbuf),
3198 gdk_pixbuf_get_height (pixbuf));
3200 cr = cairo_create (surface);
3201 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3202 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3204 cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3206 cairo_pop_group_to_source (cr);
3210 pattern = cairo_pattern_create_for_surface (surface);
3211 gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3212 cairo_pattern_destroy (pattern);
3214 cairo_surface_destroy (surface);
3216 if (gdk_pixbuf_get_has_alpha (pixbuf))
3218 cairo_region_t *region;
3220 surface = cairo_image_surface_create (CAIRO_FORMAT_A1,
3221 gdk_pixbuf_get_width (pixbuf),
3222 gdk_pixbuf_get_height (pixbuf));
3224 cr = cairo_create (surface);
3225 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3229 region = gdk_cairo_region_create_from_surface (surface);
3230 gtk_widget_shape_combine_region (window, region);
3231 cairo_region_destroy (region);
3233 cairo_surface_destroy (surface);
3238 set_icon_stock_pixbuf (GdkDragContext *context,
3239 const gchar *stock_id,
3243 gboolean force_window)
3248 GdkDisplay *display;
3250 g_return_if_fail (context != NULL);
3251 g_return_if_fail (pixbuf != NULL || stock_id != NULL);
3252 g_return_if_fail (pixbuf == NULL || stock_id == NULL);
3254 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3256 window = gtk_window_new (GTK_WINDOW_POPUP);
3257 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3258 gtk_window_set_screen (GTK_WINDOW (window), screen);
3259 set_can_change_screen (window, TRUE);
3261 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3262 gtk_widget_set_app_paintable (window, TRUE);
3266 pixbuf = gtk_widget_render_icon_pixbuf (window, stock_id,
3271 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
3272 gtk_widget_destroy (window);
3278 g_object_ref (pixbuf);
3280 display = gdk_window_get_display (gdk_drag_context_get_source_window (context));
3281 width = gdk_pixbuf_get_width (pixbuf);
3282 height = gdk_pixbuf_get_height (pixbuf);
3284 if (!force_window &&
3285 gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
3287 GtkDragSourceInfo *info;
3289 gtk_widget_destroy (window);
3291 info = gtk_drag_get_source_info (context, FALSE);
3293 if (info->icon_pixbuf)
3294 g_object_unref (info->icon_pixbuf);
3295 info->icon_pixbuf = pixbuf;
3297 gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
3301 gtk_widget_set_size_request (window, width, height);
3303 g_signal_connect_closure (window, "realize",
3304 g_cclosure_new (G_CALLBACK (icon_window_realize),
3306 (GClosureNotify)g_object_unref),
3309 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3314 * gtk_drag_set_icon_pixbuf: (method)
3315 * @context: the context for a drag. (This must be called
3316 * with a context for the source side of a drag)
3317 * @pixbuf: the #GdkPixbuf to use as the drag icon.
3318 * @hot_x: the X offset within @widget of the hotspot.
3319 * @hot_y: the Y offset within @widget of the hotspot.
3321 * Sets @pixbuf as the icon for a given drag.
3324 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
3329 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3330 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3332 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3336 * gtk_drag_set_icon_stock: (method)
3337 * @context: the context for a drag. (This must be called
3338 * with a context for the source side of a drag)
3339 * @stock_id: the ID of the stock icon to use for the drag.
3340 * @hot_x: the X offset within the icon of the hotspot.
3341 * @hot_y: the Y offset within the icon of the hotspot.
3343 * Sets the icon for a given drag from a stock ID.
3346 gtk_drag_set_icon_stock (GdkDragContext *context,
3347 const gchar *stock_id,
3351 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3352 g_return_if_fail (stock_id != NULL);
3354 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
3357 /* XXX: This function is in gdk, too. Should it be in Cairo? */
3359 _gtk_cairo_surface_extents (cairo_surface_t *surface,
3360 GdkRectangle *extents)
3362 double x1, x2, y1, y2;
3365 g_return_val_if_fail (surface != NULL, FALSE);
3366 g_return_val_if_fail (extents != NULL, FALSE);
3368 cr = cairo_create (surface);
3369 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
3378 if (x1 < G_MININT || x1 > G_MAXINT ||
3379 y1 < G_MININT || y1 > G_MAXINT ||
3380 x2 > G_MAXINT || y2 > G_MAXINT)
3382 extents->x = extents->y = extents->width = extents->height = 0;
3388 extents->width = x2;
3389 extents->height = y2;
3395 * gtk_drag_set_icon_surface: (method)
3396 * @context: the context for a drag. (This must be called
3397 * with a context for the source side of a drag)
3398 * @surface: the surface to use as icon
3400 * Sets @surface as the icon for a given drag. GTK+ retains
3401 * references for the arguments, and will release them when
3402 * they are no longer needed.
3404 * To position the surface relative to the mouse, use
3405 * cairo_surface_set_device_offset() on @surface. The mouse
3406 * cursor will be positioned at the (0,0) coordinate of the
3410 gtk_drag_set_icon_surface (GdkDragContext *context,
3411 cairo_surface_t *surface)
3415 GdkRectangle extents;
3416 cairo_pattern_t *pattern;
3418 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3419 g_return_if_fail (surface != NULL);
3421 _gtk_cairo_surface_extents (surface, &extents);
3424 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3426 window = gtk_window_new (GTK_WINDOW_POPUP);
3427 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3428 gtk_window_set_screen (GTK_WINDOW (window), screen);
3429 set_can_change_screen (window, TRUE);
3431 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3432 gtk_widget_set_app_paintable (window, TRUE);
3434 gtk_widget_set_size_request (window, extents.width, extents.height);
3435 gtk_widget_realize (window);
3437 if (cairo_surface_get_content (surface) != CAIRO_CONTENT_COLOR)
3439 cairo_surface_t *saturated;
3440 cairo_region_t *region;
3443 region = gdk_cairo_region_create_from_surface (surface);
3444 cairo_region_translate (region, -extents.x, -extents.y);
3446 gtk_widget_shape_combine_region (window, region);
3447 cairo_region_destroy (region);
3449 /* Need to saturate the colors, so it doesn't look like semi-transparent
3450 * pixels were painted on black. */
3451 saturated = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3452 CAIRO_CONTENT_COLOR,
3456 cr = cairo_create (saturated);
3457 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3458 cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
3460 cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3462 cairo_pop_group_to_source (cr);
3466 pattern = cairo_pattern_create_for_surface (saturated);
3468 cairo_surface_destroy (saturated);
3472 cairo_matrix_t matrix;
3474 pattern = cairo_pattern_create_for_surface (surface);
3475 cairo_matrix_init_translate (&matrix, extents.x, extents.y);
3476 cairo_pattern_set_matrix (pattern, &matrix);
3479 gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3481 gtk_drag_set_icon_window (context, window, extents.x, extents.y, TRUE);
3485 * gtk_drag_set_icon_name: (method)
3486 * @context: the context for a drag. (This must be called
3487 * with a context for the source side of a drag)
3488 * @icon_name: name of icon to use
3489 * @hot_x: the X offset of the hotspot within the icon
3490 * @hot_y: the Y offset of the hotspot within the icon
3492 * Sets the icon for a given drag from a named themed icon. See
3493 * the docs for #GtkIconTheme for more details. Note that the
3494 * size of the icon depends on the icon theme (the icon is
3495 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
3496 * @hot_x and @hot_y have to be used with care.
3501 gtk_drag_set_icon_name (GdkDragContext *context,
3502 const gchar *icon_name,
3508 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3509 g_return_if_fail (icon_name != NULL);
3511 icon = g_themed_icon_new (icon_name);
3512 gtk_drag_set_icon_gicon (context, icon, hot_x, hot_y);
3513 g_object_unref (icon);
3517 * gtk_drag_set_icon_gicon: (method)
3518 * @context: the context for a drag. (This must be called
3519 * with a context for the source side of a drag)
3521 * @hot_x: the X offset of the hotspot within the icon
3522 * @hot_y: the Y offset of the hotspot within the icon
3524 * Sets the icon for a given drag from the given @icon. See the
3525 * documentation for gtk_drag_set_icon_name() for more details about
3526 * using icons in drag and drop.
3531 gtk_drag_set_icon_gicon (GdkDragContext *context,
3537 GtkSettings *settings;
3538 GtkIconTheme *icon_theme;
3539 GtkIconInfo *icon_info;
3541 gint width, height, icon_size;
3543 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3544 g_return_if_fail (icon != NULL);
3546 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3547 g_return_if_fail (screen != NULL);
3549 settings = gtk_settings_get_for_screen (screen);
3550 if (gtk_icon_size_lookup_for_settings (settings,
3553 icon_size = MAX (width, height);
3555 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
3557 icon_theme = gtk_icon_theme_get_for_screen (screen);
3559 icon_info = gtk_icon_theme_lookup_by_gicon (icon_theme, icon, icon_size, 0);
3560 if (icon_info != NULL)
3562 pixbuf = gtk_icon_info_load_icon (icon_info, NULL);
3568 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3571 char *str = g_icon_to_string (icon);
3572 g_warning ("Cannot load drag icon from GIcon '%s'", str);
3578 * gtk_drag_set_icon_default: (method)
3579 * @context: the context for a drag. (This must be called
3580 with a context for the source side of a drag)
3582 * Sets the icon for a particular drag to the default
3586 gtk_drag_set_icon_default (GdkDragContext *context)
3588 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3590 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
3593 /*************************************************************
3594 * _gtk_drag_source_handle_event:
3595 * Called from widget event handling code on Drag events
3599 * toplevel: Toplevel widget that received the event
3602 *************************************************************/
3605 _gtk_drag_source_handle_event (GtkWidget *widget,
3608 GtkDragSourceInfo *info;
3609 GdkDragContext *context;
3611 g_return_if_fail (widget != NULL);
3612 g_return_if_fail (event != NULL);
3614 context = event->dnd.context;
3615 info = gtk_drag_get_source_info (context, FALSE);
3619 switch (event->type)
3621 case GDK_DRAG_STATUS:
3625 if (info->proxy_dest)
3627 if (!event->dnd.send_event)
3629 if (info->proxy_dest->proxy_drop_wait)
3631 gboolean result = gdk_drag_context_get_selected_action (context) != 0;
3633 /* Aha - we can finally pass the MOTIF DROP on... */
3634 gdk_drop_reply (info->proxy_dest->context, result, info->proxy_dest->proxy_drop_time);
3636 gdk_drag_drop (info->context, info->proxy_dest->proxy_drop_time);
3638 gtk_drag_finish (info->proxy_dest->context, FALSE, FALSE, info->proxy_dest->proxy_drop_time);
3642 gdk_drag_status (info->proxy_dest->context,
3643 gdk_drag_context_get_selected_action (event->dnd.context),
3648 else if (info->have_grab)
3650 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
3651 gdk_drag_context_get_selected_action (event->dnd.context),
3653 if (info->cursor != cursor)
3657 pointer = gdk_drag_context_get_device (context);
3658 gdk_device_grab (pointer, gtk_widget_get_window (widget),
3659 GDK_OWNERSHIP_APPLICATION, FALSE,
3660 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3661 cursor, info->grab_time);
3662 info->cursor = cursor;
3665 gtk_drag_add_update_idle (info);
3670 case GDK_DROP_FINISHED:
3671 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, event->dnd.time);
3674 g_assert_not_reached ();
3678 /*************************************************************
3679 * gtk_drag_source_check_selection:
3680 * Check if we've set up handlers/claimed the selection
3681 * for a given drag. If not, add them.
3685 *************************************************************/
3688 gtk_drag_source_check_selection (GtkDragSourceInfo *info,
3694 tmp_list = info->selections;
3697 if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3699 tmp_list = tmp_list->next;
3702 gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3706 info->selections = g_list_prepend (info->selections,
3707 GUINT_TO_POINTER (selection));
3709 tmp_list = info->target_list->list;
3712 GtkTargetPair *pair = tmp_list->data;
3714 gtk_selection_add_target (info->ipc_widget,
3718 tmp_list = tmp_list->next;
3721 if (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_MOTIF)
3723 gtk_selection_add_target (info->ipc_widget,
3725 gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS"),
3726 TARGET_MOTIF_SUCCESS);
3727 gtk_selection_add_target (info->ipc_widget,
3729 gdk_atom_intern_static_string ("XmTRANSFER_FAILURE"),
3730 TARGET_MOTIF_FAILURE);
3733 gtk_selection_add_target (info->ipc_widget,
3735 gdk_atom_intern_static_string ("DELETE"),
3739 /*************************************************************
3740 * gtk_drag_drop_finished:
3741 * Clean up from the drag, and display snapback, if necessary.
3747 *************************************************************/
3750 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3751 GtkDragResult result,
3756 success = (result == GTK_DRAG_RESULT_SUCCESS);
3757 gtk_drag_source_release_selections (info, time);
3759 if (info->proxy_dest)
3761 /* The time from the event isn't reliable for Xdnd drags */
3762 gtk_drag_finish (info->proxy_dest->context, success, FALSE,
3763 info->proxy_dest->proxy_drop_time);
3764 gtk_drag_source_info_destroy (info);
3769 g_signal_emit_by_name (info->widget, "drag-failed",
3770 info->context, result, &success);
3774 gtk_drag_source_info_destroy (info);
3778 GtkDragAnim *anim = g_slice_new0 (GtkDragAnim);
3782 anim->n_steps = MAX (info->cur_x - info->start_x,
3783 info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3784 anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3786 info->cur_screen = gtk_widget_get_screen (info->widget);
3788 if (!info->icon_window)
3789 set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf,
3792 gtk_drag_update_icon (info);
3794 /* Mark the context as dead, so if the destination decides
3795 * to respond really late, we still are OK.
3797 gtk_drag_clear_source_info (info->context);
3798 gdk_threads_add_timeout (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3804 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3807 GdkDisplay *display = gtk_widget_get_display (info->widget);
3808 GList *tmp_list = info->selections;
3812 GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3813 if (gdk_selection_owner_get_for_display (display, selection) == gtk_widget_get_window (info->ipc_widget))
3814 gtk_selection_owner_set_for_display (display, NULL, selection, time);
3816 tmp_list = tmp_list->next;
3819 g_list_free (info->selections);
3820 info->selections = NULL;
3823 /*************************************************************
3825 * Send a drop event.
3829 *************************************************************/
3832 gtk_drag_drop (GtkDragSourceInfo *info,
3835 if (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_ROOTWIN)
3837 GtkSelectionData selection_data;
3839 /* GTK+ traditionally has used application/x-rootwin-drop, but the
3840 * XDND spec specifies x-rootwindow-drop.
3842 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3843 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3845 tmp_list = info->target_list->list;
3848 GtkTargetPair *pair = tmp_list->data;
3850 if (pair->target == target1 || pair->target == target2)
3852 selection_data.selection = GDK_NONE;
3853 selection_data.target = pair->target;
3854 selection_data.data = NULL;
3855 selection_data.length = -1;
3857 g_signal_emit_by_name (info->widget, "drag-data-get",
3858 info->context, &selection_data,
3862 /* FIXME: Should we check for length >= 0 here? */
3863 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3866 tmp_list = tmp_list->next;
3868 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3872 if (info->icon_window)
3873 gtk_widget_hide (info->icon_window);
3875 gdk_drag_drop (info->context, time);
3876 info->drop_timeout = gdk_threads_add_timeout (DROP_ABORT_TIME,
3877 gtk_drag_abort_timeout,
3883 * Source side callbacks.
3887 gtk_drag_source_event_cb (GtkWidget *widget,
3891 GtkDragSourceSite *site;
3892 gboolean retval = FALSE;
3893 site = (GtkDragSourceSite *)data;
3895 switch (event->type)
3897 case GDK_BUTTON_PRESS:
3898 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3900 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3901 site->x = event->button.x;
3902 site->y = event->button.y;
3906 case GDK_BUTTON_RELEASE:
3907 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3908 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3911 case GDK_MOTION_NOTIFY:
3912 if (site->state & event->motion.state & site->start_button_mask)
3914 /* FIXME: This is really broken and can leave us
3920 if (site->state & event->motion.state &
3921 GDK_BUTTON1_MASK << (i - 1))
3925 if (gtk_drag_check_threshold (widget, site->x, site->y,
3926 event->motion.x, event->motion.y))
3929 gtk_drag_begin_internal (widget, site, site->target_list,
3938 default: /* hit for 2/3BUTTON_PRESS */
3946 gtk_drag_source_site_destroy (gpointer data)
3948 GtkDragSourceSite *site = data;
3950 if (site->target_list)
3951 gtk_target_list_unref (site->target_list);
3953 gtk_drag_source_unset_icon (site);
3954 g_slice_free (GtkDragSourceSite, site);
3958 gtk_drag_selection_get (GtkWidget *widget,
3959 GtkSelectionData *selection_data,
3964 GtkDragSourceInfo *info = data;
3965 static GdkAtom null_atom = GDK_NONE;
3969 null_atom = gdk_atom_intern_static_string ("NULL");
3974 g_signal_emit_by_name (info->widget,
3977 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3979 case TARGET_MOTIF_SUCCESS:
3980 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3981 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3983 case TARGET_MOTIF_FAILURE:
3984 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3985 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3988 if (info->proxy_dest)
3990 /* This is sort of dangerous and needs to be thought
3993 info->proxy_dest->proxy_data = selection_data;
3994 gtk_drag_get_data (info->widget,
3995 info->proxy_dest->context,
3996 gtk_selection_data_get_target (selection_data),
3999 info->proxy_dest->proxy_data = NULL;
4003 if (gtk_target_list_find (info->target_list,
4004 gtk_selection_data_get_target (selection_data),
4007 g_signal_emit_by_name (info->widget, "drag-data-get",
4019 gtk_drag_anim_timeout (gpointer data)
4022 GtkDragSourceInfo *info;
4029 if (anim->step == anim->n_steps)
4031 gtk_drag_source_info_destroy (anim->info);
4032 g_slice_free (GtkDragAnim, anim);
4038 x = (info->start_x * (anim->step + 1) +
4039 info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
4040 y = (info->start_y * (anim->step + 1) +
4041 info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
4042 if (info->icon_window)
4044 GtkWidget *icon_window;
4047 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
4048 gtk_window_move (GTK_WINDOW (icon_window),
4062 gtk_drag_remove_icon (GtkDragSourceInfo *info)
4064 if (info->icon_window)
4066 gtk_widget_hide (info->icon_window);
4067 if (info->destroy_icon)
4068 gtk_widget_destroy (info->icon_window);
4070 if (info->fallback_icon)
4072 gtk_widget_destroy (info->fallback_icon);
4073 info->fallback_icon = NULL;
4076 g_object_unref (info->icon_window);
4077 info->icon_window = NULL;
4082 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
4086 for (i = 0; i < G_N_ELEMENTS (drag_cursors); i++)
4088 if (info->drag_cursors[i] != NULL)
4090 g_object_unref (info->drag_cursors[i]);
4091 info->drag_cursors[i] = NULL;
4095 gtk_drag_remove_icon (info);
4097 if (info->icon_pixbuf)
4099 g_object_unref (info->icon_pixbuf);
4100 info->icon_pixbuf = NULL;
4103 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4104 gtk_drag_grab_broken_event_cb,
4106 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4107 gtk_drag_grab_notify_cb,
4109 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4110 gtk_drag_button_release_cb,
4112 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4115 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4118 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4119 gtk_drag_selection_get,
4122 if (!info->proxy_dest)
4123 g_signal_emit_by_name (info->widget, "drag-end", info->context);
4126 g_object_unref (info->widget);
4128 gtk_selection_remove_all (info->ipc_widget);
4129 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), NULL);
4130 source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
4131 gtk_drag_release_ipc_widget (info->ipc_widget);
4133 gtk_target_list_unref (info->target_list);
4135 gtk_drag_clear_source_info (info->context);
4136 g_object_unref (info->context);
4138 if (info->drop_timeout)
4139 g_source_remove (info->drop_timeout);
4141 if (info->update_idle)
4142 g_source_remove (info->update_idle);
4148 gtk_drag_update_idle (gpointer data)
4150 GtkDragSourceInfo *info = data;
4151 GdkWindow *dest_window;
4152 GdkDragProtocol protocol;
4155 GdkDragAction action;
4156 GdkDragAction possible_actions;
4159 info->update_idle = 0;
4161 if (info->last_event)
4163 time = gtk_drag_get_event_time (info->last_event);
4164 gtk_drag_get_event_actions (info->last_event,
4166 info->possible_actions,
4167 &action, &possible_actions);
4168 gtk_drag_update_icon (info);
4169 gdk_drag_find_window_for_screen (info->context,
4170 info->icon_window ? gtk_widget_get_window (info->icon_window) : NULL,
4171 info->cur_screen, info->cur_x, info->cur_y,
4172 &dest_window, &protocol);
4174 if (!gdk_drag_motion (info->context, dest_window, protocol,
4175 info->cur_x, info->cur_y, action,
4179 gdk_event_free ((GdkEvent *)info->last_event);
4180 info->last_event = NULL;
4184 g_object_unref (dest_window);
4186 selection = gdk_drag_get_selection (info->context);
4188 gtk_drag_source_check_selection (info, selection, time);
4196 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
4198 /* We use an idle lower than GDK_PRIORITY_REDRAW so that exposes
4199 * from the last move can catch up before we move again.
4201 if (!info->update_idle)
4202 info->update_idle = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW + 5,
4203 gtk_drag_update_idle,
4210 * @info: DragSourceInfo for the drag
4211 * @screen: new screen
4212 * @x_root: new X position
4213 * @y_root: new y position
4214 * @event: event received requiring update
4216 * Updates the status of the drag; called when the
4217 * cursor moves or the modifier changes
4220 gtk_drag_update (GtkDragSourceInfo *info,
4226 info->cur_screen = screen;
4227 info->cur_x = x_root;
4228 info->cur_y = y_root;
4229 if (info->last_event)
4231 gdk_event_free ((GdkEvent *)info->last_event);
4232 info->last_event = NULL;
4235 info->last_event = gdk_event_copy ((GdkEvent *)event);
4237 gtk_drag_add_update_idle (info);
4240 /*************************************************************
4242 * Called when the user finishes to drag, either by
4243 * releasing the mouse, or by pressing Esc.
4245 * info: Source info for the drag
4246 * time: Timestamp for ending the drag
4248 *************************************************************/
4251 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
4253 GtkWidget *source_widget = info->widget;
4254 GdkDevice *pointer, *keyboard;
4256 pointer = gdk_drag_context_get_device (info->context);
4257 keyboard = gdk_device_get_associated_device (pointer);
4259 /* Prevent ungrab before grab (see bug 623865) */
4260 if (info->grab_time == GDK_CURRENT_TIME)
4261 time = GDK_CURRENT_TIME;
4263 if (info->update_idle)
4265 g_source_remove (info->update_idle);
4266 info->update_idle = 0;
4269 if (info->last_event)
4271 gdk_event_free (info->last_event);
4272 info->last_event = NULL;
4275 info->have_grab = FALSE;
4277 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4278 gtk_drag_grab_broken_event_cb,
4280 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4281 gtk_drag_grab_notify_cb,
4283 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4284 gtk_drag_button_release_cb,
4286 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4289 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4293 gdk_device_ungrab (pointer, time);
4294 ungrab_dnd_keys (info->ipc_widget, keyboard, time);
4295 gtk_device_grab_remove (info->ipc_widget, pointer);
4297 if (gtk_widget_get_realized (source_widget))
4299 GdkEvent *send_event;
4301 /* Send on a release pair to the original widget to convince it
4302 * to release its grab. We need to call gtk_propagate_event()
4303 * here, instead of gtk_widget_event() because widget like
4304 * GtkList may expect propagation.
4307 send_event = gdk_event_new (GDK_BUTTON_RELEASE);
4308 send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
4309 send_event->button.send_event = TRUE;
4310 send_event->button.time = time;
4311 send_event->button.x = 0;
4312 send_event->button.y = 0;
4313 send_event->button.axes = NULL;
4314 send_event->button.state = 0;
4315 send_event->button.button = info->button;
4316 send_event->button.device = pointer;
4317 send_event->button.x_root = 0;
4318 send_event->button.y_root = 0;
4320 gtk_propagate_event (source_widget, send_event);
4321 gdk_event_free (send_event);
4325 /*************************************************************
4327 * Called on cancellation of a drag, either by the user
4328 * or programmatically.
4330 * info: Source info for the drag
4331 * time: Timestamp for ending the drag
4333 *************************************************************/
4336 gtk_drag_cancel (GtkDragSourceInfo *info, GtkDragResult result, guint32 time)
4338 gtk_drag_end (info, time);
4339 gdk_drag_abort (info->context, time);
4340 gtk_drag_drop_finished (info, result, time);
4343 /*************************************************************
4344 * gtk_drag_motion_cb:
4345 * "motion-notify-event" callback during drag.
4349 *************************************************************/
4352 gtk_drag_motion_cb (GtkWidget *widget,
4353 GdkEventMotion *event,
4356 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4358 gint x_root, y_root;
4362 gdk_device_get_position (event->device, &screen, &x_root, &y_root);
4363 event->x_root = x_root;
4364 event->y_root = y_root;
4367 screen = gdk_event_get_screen ((GdkEvent *)event);
4369 x_root = (gint)(event->x_root + 0.5);
4370 y_root = (gint)(event->y_root + 0.5);
4371 gtk_drag_update (info, screen, x_root, y_root, (GdkEvent *) event);
4376 /*************************************************************
4378 * "key-press/release-event" callback during drag.
4382 *************************************************************/
4385 #define SMALL_STEP 1
4388 gtk_drag_key_cb (GtkWidget *widget,
4392 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4393 GdkModifierType state;
4394 GdkWindow *root_window;
4399 state = event->state & gtk_accelerator_get_default_mod_mask ();
4400 pointer = gdk_device_get_associated_device (gdk_event_get_device ((GdkEvent *) event));
4402 if (event->type == GDK_KEY_PRESS)
4404 switch (event->keyval)
4406 case GDK_KEY_Escape:
4407 gtk_drag_cancel (info, GTK_DRAG_RESULT_USER_CANCELLED, event->time);
4411 case GDK_KEY_Return:
4412 case GDK_KEY_ISO_Enter:
4413 case GDK_KEY_KP_Enter:
4414 case GDK_KEY_KP_Space:
4415 if ((gdk_drag_context_get_selected_action (info->context) != 0) &&
4416 (gdk_drag_context_get_dest_window (info->context) != NULL))
4418 gtk_drag_end (info, event->time);
4419 gtk_drag_drop (info, event->time);
4423 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4430 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4434 case GDK_KEY_KP_Down:
4435 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4439 case GDK_KEY_KP_Left:
4440 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4444 case GDK_KEY_KP_Right:
4445 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4450 /* Now send a "motion" so that the modifier state is updated */
4452 /* The state is not yet updated in the event, so we need
4453 * to query it here. We could use XGetModifierMapping, but
4454 * that would be overkill.
4456 root_window = gtk_widget_get_root_window (widget);
4457 gdk_window_get_device_position (root_window, pointer, NULL, NULL, &state);
4458 event->state = state;
4460 if (dx != 0 || dy != 0)
4464 gdk_device_warp (pointer,
4465 gtk_widget_get_screen (widget),
4466 info->cur_x, info->cur_y);
4469 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
4475 gtk_drag_grab_broken_event_cb (GtkWidget *widget,
4476 GdkEventGrabBroken *event,
4479 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4481 /* Don't cancel if we break the implicit grab from the initial button_press.
4482 * Also, don't cancel if we re-grab on the widget or on our IPC window, for
4483 * example, when changing the drag cursor.
4486 || event->grab_window == gtk_widget_get_window (info->widget)
4487 || event->grab_window == gtk_widget_get_window (info->ipc_widget))
4490 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4495 gtk_drag_grab_notify_cb (GtkWidget *widget,
4496 gboolean was_grabbed,
4499 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4502 pointer = gdk_drag_context_get_device (info->context);
4504 if (gtk_widget_device_is_shadowed (widget, pointer))
4506 /* We have to block callbacks to avoid recursion here, because
4507 gtk_drag_cancel calls gtk_grab_remove (via gtk_drag_end) */
4508 g_signal_handlers_block_by_func (widget, gtk_drag_grab_notify_cb, data);
4509 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4510 g_signal_handlers_unblock_by_func (widget, gtk_drag_grab_notify_cb, data);
4515 /*************************************************************
4516 * gtk_drag_button_release_cb:
4517 * "button-release-event" callback during drag.
4521 *************************************************************/
4524 gtk_drag_button_release_cb (GtkWidget *widget,
4525 GdkEventButton *event,
4528 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4530 if (event->button != info->button)
4533 if ((gdk_drag_context_get_selected_action (info->context) != 0) &&
4534 (gdk_drag_context_get_dest_window (info->context) != NULL))
4536 gtk_drag_end (info, event->time);
4537 gtk_drag_drop (info, event->time);
4541 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4548 gtk_drag_abort_timeout (gpointer data)
4550 GtkDragSourceInfo *info = data;
4551 guint32 time = GDK_CURRENT_TIME;
4553 if (info->proxy_dest)
4554 time = info->proxy_dest->proxy_drop_time;
4556 info->drop_timeout = 0;
4557 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_TIMEOUT_EXPIRED, time);
4563 * gtk_drag_check_threshold: (method)
4564 * @widget: a #GtkWidget
4565 * @start_x: X coordinate of start of drag
4566 * @start_y: Y coordinate of start of drag
4567 * @current_x: current X coordinate
4568 * @current_y: current Y coordinate
4570 * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
4571 * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
4572 * should trigger the beginning of a drag-and-drop operation.
4574 * Return Value: %TRUE if the drag threshold has been passed.
4577 gtk_drag_check_threshold (GtkWidget *widget,
4583 gint drag_threshold;
4585 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4587 g_object_get (gtk_widget_get_settings (widget),
4588 "gtk-dnd-drag-threshold", &drag_threshold,
4591 return (ABS (current_x - start_x) > drag_threshold ||
4592 ABS (current_y - start_y) > drag_threshold);