1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
35 #ifdef GDK_WINDOWING_X11
37 #include <X11/keysym.h>
38 #include "gdk/x11/gdkx.h"
42 #include "gtkiconfactory.h"
43 #include "gtkicontheme.h"
44 #include "gtkimageprivate.h"
45 #include "gtkinvisible.h"
49 #include "gtktooltip.h"
50 #include "gtkwindow.h"
52 #include "gtkdndcursors.h"
53 #include "gtkselectionprivate.h"
58 * @Short_description: Functions for controlling drag and drop handling
59 * @Title: Drag and Drop
61 * GTK+ has a rich set of functions for doing inter-process
62 * communication via the drag-and-drop metaphor. GTK+
63 * can do drag-and-drop (DND) via multiple protocols.
64 * The currently supported protocols are the Xdnd and
67 * As well as the functions listed here, applications
68 * may need to use some facilities provided for
69 * <link linkend="gtk-Selections">Selections</link>.
70 * Also, the Drag and Drop API makes use of signals
71 * in the #GtkWidget class.
75 static GSList *source_widgets = NULL;
77 typedef struct _GtkDragSourceSite GtkDragSourceSite;
78 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
79 typedef struct _GtkDragDestSite GtkDragDestSite;
80 typedef struct _GtkDragDestInfo GtkDragDestInfo;
81 typedef struct _GtkDragAnim GtkDragAnim;
91 struct _GtkDragSourceSite
93 GdkModifierType start_button_mask;
94 GtkTargetList *target_list; /* Targets for drag data */
95 GdkDragAction actions; /* Possible actions */
98 GtkImageType icon_type;
101 GtkImagePixbufData pixbuf;
102 GtkImageStockData stock;
103 GtkImageIconNameData name;
104 GtkImageGIconData gicon;
107 /* Stored button press information to detect drag beginning */
112 struct _GtkDragSourceInfo
115 GtkTargetList *target_list; /* Targets for drag data */
116 GdkDragAction possible_actions; /* Actions allowed by source */
117 GdkDragContext *context; /* drag context */
118 GtkWidget *icon_window; /* Window for drag */
119 GtkWidget *fallback_icon; /* Window for drag used on other screens */
120 GtkWidget *ipc_widget; /* GtkInvisible for grab, message passing */
121 GdkCursor *cursor; /* Cursor for drag */
122 gint hot_x, hot_y; /* Hot spot for drag */
123 gint button; /* mouse button starting drag */
125 GtkDragStatus status; /* drag status */
126 GdkEvent *last_event; /* pending event */
128 gint start_x, start_y; /* Initial position */
129 gint cur_x, cur_y; /* Current Position */
130 GdkScreen *cur_screen; /* Current screen for pointer */
132 guint32 grab_time; /* timestamp for initial grab */
133 GList *selections; /* selections we've claimed */
135 GtkDragDestInfo *proxy_dest; /* Set if this is a proxy drag */
137 guint update_idle; /* Idle function to update the drag */
138 guint drop_timeout; /* Timeout for aborting drop */
139 guint destroy_icon : 1; /* If true, destroy icon_window */
140 guint have_grab : 1; /* Do we still have the pointer grab */
141 GdkPixbuf *icon_pixbuf;
142 GdkCursor *drag_cursors[6];
145 struct _GtkDragDestSite
147 GtkDestDefaults flags;
148 GtkTargetList *target_list;
149 GdkDragAction actions;
150 GdkWindow *proxy_window;
151 GdkDragProtocol proxy_protocol;
153 guint proxy_coords : 1;
155 guint track_motion : 1;
158 struct _GtkDragDestInfo
160 GtkWidget *widget; /* Widget in which drag is in */
161 GdkDragContext *context; /* Drag context */
162 GtkDragSourceInfo *proxy_source; /* Set if this is a proxy drag */
163 GtkSelectionData *proxy_data; /* Set while retrieving proxied data */
164 guint32 proxy_drop_time; /* Timestamp for proxied drop */
165 guint proxy_drop_wait : 1; /* Set if we are waiting for a
166 * status reply before sending
169 guint dropped : 1; /* Set after we receive a drop */
170 gint drop_x, drop_y; /* Position of drop */
173 #define DROP_ABORT_TIME 300000
175 #define ANIM_STEP_TIME 50
176 #define ANIM_STEP_LENGTH 50
177 #define ANIM_MIN_STEPS 5
178 #define ANIM_MAX_STEPS 10
182 GtkDragSourceInfo *info;
187 typedef gboolean (* GtkDragDestCallback) (GtkWidget *widget,
188 GdkDragContext *context,
193 /* Enumeration for some targets we handle internally */
196 TARGET_MOTIF_SUCCESS = 0x40000000,
197 TARGET_MOTIF_FAILURE,
201 /* Forward declarations */
202 static void gtk_drag_get_event_actions (GdkEvent *event,
204 GdkDragAction actions,
205 GdkDragAction *suggested_action,
206 GdkDragAction *possible_actions);
207 static GdkCursor * gtk_drag_get_cursor (GdkDisplay *display,
208 GdkDragAction action,
209 GtkDragSourceInfo *info);
210 static void gtk_drag_update_cursor (GtkDragSourceInfo *info);
211 static GtkWidget *gtk_drag_get_ipc_widget (GtkWidget *widget);
212 static GtkWidget *gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen);
213 static void gtk_drag_release_ipc_widget (GtkWidget *widget);
215 static void gtk_drag_selection_received (GtkWidget *widget,
216 GtkSelectionData *selection_data,
219 static gboolean gtk_drag_find_widget (GtkWidget *widget,
220 GdkDragContext *context,
221 GtkDragDestInfo *info,
225 GtkDragDestCallback callback);
226 static void gtk_drag_proxy_begin (GtkWidget *widget,
227 GtkDragDestInfo *dest_info,
229 static void gtk_drag_dest_realized (GtkWidget *widget);
230 static void gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
231 GtkWidget *previous_toplevel);
232 static void gtk_drag_dest_site_destroy (gpointer data);
233 static void gtk_drag_dest_leave (GtkWidget *widget,
234 GdkDragContext *context,
236 static gboolean gtk_drag_dest_motion (GtkWidget *widget,
237 GdkDragContext *context,
241 static gboolean gtk_drag_dest_drop (GtkWidget *widget,
242 GdkDragContext *context,
247 static GtkDragDestInfo * gtk_drag_get_dest_info (GdkDragContext *context,
249 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
251 static void gtk_drag_clear_source_info (GdkDragContext *context);
253 static void gtk_drag_source_check_selection (GtkDragSourceInfo *info,
256 static void gtk_drag_source_release_selections (GtkDragSourceInfo *info,
258 static void gtk_drag_drop (GtkDragSourceInfo *info,
260 static void gtk_drag_drop_finished (GtkDragSourceInfo *info,
261 GtkDragResult result,
263 static void gtk_drag_cancel (GtkDragSourceInfo *info,
264 GtkDragResult result,
267 static gboolean gtk_drag_source_event_cb (GtkWidget *widget,
270 static void gtk_drag_source_site_destroy (gpointer data);
271 static void gtk_drag_selection_get (GtkWidget *widget,
272 GtkSelectionData *selection_data,
276 static gboolean gtk_drag_anim_timeout (gpointer data);
277 static void gtk_drag_remove_icon (GtkDragSourceInfo *info);
278 static void gtk_drag_source_info_destroy (GtkDragSourceInfo *info);
279 static void gtk_drag_add_update_idle (GtkDragSourceInfo *info);
281 static void gtk_drag_update (GtkDragSourceInfo *info,
286 static gboolean gtk_drag_motion_cb (GtkWidget *widget,
287 GdkEventMotion *event,
289 static gboolean gtk_drag_key_cb (GtkWidget *widget,
292 static gboolean gtk_drag_grab_broken_event_cb (GtkWidget *widget,
293 GdkEventGrabBroken *event,
295 static void gtk_drag_grab_notify_cb (GtkWidget *widget,
296 gboolean was_grabbed,
298 static gboolean gtk_drag_button_release_cb (GtkWidget *widget,
299 GdkEventButton *event,
301 static gboolean gtk_drag_abort_timeout (gpointer data);
303 static void set_icon_stock_pixbuf (GdkDragContext *context,
304 const gchar *stock_id,
308 gboolean force_window);
310 /************************
311 * Cursor and Icon data *
312 ************************/
315 GdkDragAction action;
321 { GDK_ACTION_DEFAULT, NULL },
322 { GDK_ACTION_ASK, "dnd-ask", dnd_cursor_ask, NULL, NULL },
323 { GDK_ACTION_COPY, "dnd-copy", dnd_cursor_copy, NULL, NULL },
324 { GDK_ACTION_MOVE, "dnd-move", dnd_cursor_move, NULL, NULL },
325 { GDK_ACTION_LINK, "dnd-link", dnd_cursor_link, NULL, NULL },
326 { 0 , "dnd-none", dnd_cursor_none, NULL, NULL },
329 /*********************
330 * Utility functions *
331 *********************/
334 set_can_change_screen (GtkWidget *widget,
335 gboolean can_change_screen)
337 can_change_screen = can_change_screen != FALSE;
339 g_object_set_data (G_OBJECT (widget), I_("gtk-dnd-can-change-screen"),
340 GUINT_TO_POINTER (can_change_screen));
344 get_can_change_screen (GtkWidget *widget)
346 return g_object_get_data (G_OBJECT (widget), "gtk-dnd-can-change-screen") != NULL;
351 gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen)
354 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
355 "gtk-dnd-ipc-widgets");
359 GSList *tmp = drag_widgets;
360 result = drag_widgets->data;
361 drag_widgets = drag_widgets->next;
362 g_object_set_data (G_OBJECT (screen),
363 I_("gtk-dnd-ipc-widgets"),
365 g_slist_free_1 (tmp);
369 result = gtk_window_new (GTK_WINDOW_POPUP);
370 gtk_window_set_screen (GTK_WINDOW (result), screen);
371 gtk_window_resize (GTK_WINDOW (result), 1, 1);
372 gtk_window_move (GTK_WINDOW (result), -100, -100);
373 gtk_widget_show (result);
380 gtk_drag_get_ipc_widget (GtkWidget *widget)
385 result = gtk_drag_get_ipc_widget_for_screen (gtk_widget_get_screen (widget));
387 toplevel = gtk_widget_get_toplevel (widget);
389 if (GTK_IS_WINDOW (toplevel))
391 if (gtk_window_has_group (GTK_WINDOW (toplevel)))
392 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
393 GTK_WINDOW (result));
399 /* FIXME: modifying the XEvent window as in root_key_filter() isn't
400 * going to work with XGE/XI2, since the actual event to handle would
401 * be allocated/freed before GDK gets to translate the event.
402 * Active grabs on the keyboard are used instead at the moment...
404 #if defined (GDK_WINDOWING_X11) && !defined (XINPUT_2)
407 * We want to handle a handful of keys during DND, e.g. Escape to abort.
408 * Grabbing the keyboard has the unfortunate side-effect of preventing
409 * useful things such as using Alt-Tab to cycle between windows or
410 * switching workspaces. Therefore, we just grab the few keys we are
411 * interested in. Note that we need to put the grabs on the root window
412 * in order for them to still work when the focus is moved to another
415 * GDK needs a little help to successfully deliver root key events...
418 static GdkFilterReturn
419 root_key_filter (GdkXEvent *xevent,
423 XEvent *ev = (XEvent *) xevent;
425 if ((ev->type == KeyPress || ev->type == KeyRelease) &&
426 ev->xkey.root == ev->xkey.window)
427 ev->xkey.window = (Window)data;
429 return GDK_FILTER_CONTINUE;
437 static GrabKey grab_keys[] = {
446 { XK_Down, Mod1Mask },
448 { XK_Left, Mod1Mask },
450 { XK_Right, Mod1Mask },
452 { XK_KP_Up, Mod1Mask },
454 { XK_KP_Down, Mod1Mask },
456 { XK_KP_Left, Mod1Mask },
458 { XK_KP_Right, Mod1Mask }
462 grab_dnd_keys (GtkWidget *widget,
467 GdkWindow *window, *root;
470 window = gtk_widget_get_window (widget);
471 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
473 gdk_error_trap_push ();
475 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
477 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
478 if (keycode == NoSymbol)
480 XGrabKey (GDK_WINDOW_XDISPLAY (window),
481 keycode, grab_keys[i].modifiers,
482 GDK_WINDOW_XID (root),
489 gdk_error_trap_pop_ignored ();
491 gdk_window_add_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
495 ungrab_dnd_keys (GtkWidget *widget,
500 GdkWindow *window, *root;
503 window = gtk_widget_get_window (widget);
504 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
506 gdk_window_remove_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
508 gdk_error_trap_push ();
510 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
512 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
513 if (keycode == NoSymbol)
515 XUngrabKey (GDK_WINDOW_XDISPLAY (window),
516 keycode, grab_keys[i].modifiers,
517 GDK_WINDOW_XID (root));
521 gdk_error_trap_pop_ignored ();
524 #else /* GDK_WINDOWING_X11 && !XINPUT_2 */
527 grab_dnd_keys (GtkWidget *widget,
531 gdk_device_grab (device,
532 gtk_widget_get_window (widget),
533 GDK_OWNERSHIP_APPLICATION, FALSE,
534 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
539 ungrab_dnd_keys (GtkWidget *widget,
543 gdk_device_ungrab (device, time);
546 #endif /* GDK_WINDOWING_X11 */
549 /***************************************************************
550 * gtk_drag_release_ipc_widget:
551 * Releases widget retrieved with gtk_drag_get_ipc_widget ()
553 * widget: the widget to release.
555 ***************************************************************/
558 gtk_drag_release_ipc_widget (GtkWidget *widget)
560 GtkWindow *window = GTK_WINDOW (widget);
561 GdkScreen *screen = gtk_widget_get_screen (widget);
562 GdkDragContext *context = g_object_get_data (G_OBJECT (widget), "drag-context");
563 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
564 "gtk-dnd-ipc-widgets");
565 GdkDevice *pointer, *keyboard;
569 pointer = gdk_drag_context_get_device (context);
570 keyboard = gdk_device_get_associated_device (pointer);
573 ungrab_dnd_keys (widget, keyboard, GDK_CURRENT_TIME);
576 if (gtk_window_has_group (window))
577 gtk_window_group_remove_window (gtk_window_get_group (window),
579 drag_widgets = g_slist_prepend (drag_widgets, widget);
580 g_object_set_data (G_OBJECT (screen),
581 I_("gtk-dnd-ipc-widgets"),
586 gtk_drag_get_event_time (GdkEvent *event)
588 guint32 tm = GDK_CURRENT_TIME;
593 case GDK_MOTION_NOTIFY:
594 tm = event->motion.time; break;
595 case GDK_BUTTON_PRESS:
596 case GDK_2BUTTON_PRESS:
597 case GDK_3BUTTON_PRESS:
598 case GDK_BUTTON_RELEASE:
599 tm = event->button.time; break;
601 case GDK_KEY_RELEASE:
602 tm = event->key.time; break;
603 case GDK_ENTER_NOTIFY:
604 case GDK_LEAVE_NOTIFY:
605 tm = event->crossing.time; break;
606 case GDK_PROPERTY_NOTIFY:
607 tm = event->property.time; break;
608 case GDK_SELECTION_CLEAR:
609 case GDK_SELECTION_REQUEST:
610 case GDK_SELECTION_NOTIFY:
611 tm = event->selection.time; break;
612 case GDK_PROXIMITY_IN:
613 case GDK_PROXIMITY_OUT:
614 tm = event->proximity.time; break;
615 default: /* use current time */
623 gtk_drag_get_event_actions (GdkEvent *event,
625 GdkDragAction actions,
626 GdkDragAction *suggested_action,
627 GdkDragAction *possible_actions)
629 *suggested_action = 0;
630 *possible_actions = 0;
634 GdkModifierType state = 0;
638 case GDK_MOTION_NOTIFY:
639 state = event->motion.state;
641 case GDK_BUTTON_PRESS:
642 case GDK_2BUTTON_PRESS:
643 case GDK_3BUTTON_PRESS:
644 case GDK_BUTTON_RELEASE:
645 state = event->button.state;
648 case GDK_KEY_RELEASE:
649 state = event->key.state;
651 case GDK_ENTER_NOTIFY:
652 case GDK_LEAVE_NOTIFY:
653 state = event->crossing.state;
659 if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
661 *suggested_action = GDK_ACTION_ASK;
662 *possible_actions = actions;
664 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
666 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
668 if (actions & GDK_ACTION_LINK)
670 *suggested_action = GDK_ACTION_LINK;
671 *possible_actions = GDK_ACTION_LINK;
674 else if (state & GDK_CONTROL_MASK)
676 if (actions & GDK_ACTION_COPY)
678 *suggested_action = GDK_ACTION_COPY;
679 *possible_actions = GDK_ACTION_COPY;
685 if (actions & GDK_ACTION_MOVE)
687 *suggested_action = GDK_ACTION_MOVE;
688 *possible_actions = GDK_ACTION_MOVE;
695 *possible_actions = actions;
697 if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
698 *suggested_action = GDK_ACTION_ASK;
699 else if (actions & GDK_ACTION_COPY)
700 *suggested_action = GDK_ACTION_COPY;
701 else if (actions & GDK_ACTION_MOVE)
702 *suggested_action = GDK_ACTION_MOVE;
703 else if (actions & GDK_ACTION_LINK)
704 *suggested_action = GDK_ACTION_LINK;
709 *possible_actions = actions;
711 if (actions & GDK_ACTION_COPY)
712 *suggested_action = GDK_ACTION_COPY;
713 else if (actions & GDK_ACTION_MOVE)
714 *suggested_action = GDK_ACTION_MOVE;
715 else if (actions & GDK_ACTION_LINK)
716 *suggested_action = GDK_ACTION_LINK;
721 gtk_drag_can_use_rgba_cursor (GdkDisplay *display,
725 guint max_width, max_height;
727 if (!gdk_display_supports_cursor_color (display))
730 if (!gdk_display_supports_cursor_alpha (display))
733 gdk_display_get_maximal_cursor_size (display,
736 if (width > max_width || height > max_height)
738 /* can't use rgba cursor (too large) */
746 gtk_drag_get_cursor (GdkDisplay *display,
747 GdkDragAction action,
748 GtkDragSourceInfo *info)
752 /* reconstruct the cursors for each new drag (thus !info),
753 * to catch cursor theme changes
757 for (i = 0 ; i < G_N_ELEMENTS (drag_cursors) - 1; i++)
758 if (drag_cursors[i].cursor != NULL)
760 g_object_unref (drag_cursors[i].cursor);
761 drag_cursors[i].cursor = NULL;
765 for (i = 0 ; i < G_N_ELEMENTS (drag_cursors) - 1; i++)
766 if (drag_cursors[i].action == action)
769 if (drag_cursors[i].pixbuf == NULL)
770 drag_cursors[i].pixbuf =
771 gdk_pixbuf_new_from_inline (-1, drag_cursors[i].data, FALSE, NULL);
773 if (drag_cursors[i].cursor != NULL)
775 if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
777 g_object_unref (drag_cursors[i].cursor);
778 drag_cursors[i].cursor = NULL;
782 if (drag_cursors[i].cursor == NULL)
783 drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
785 if (drag_cursors[i].cursor == NULL)
786 drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);
788 if (info && info->icon_pixbuf)
790 gint cursor_width, cursor_height;
791 gint icon_width, icon_height;
793 GdkPixbuf *cursor_pixbuf, *pixbuf;
795 gint icon_x, icon_y, ref_x, ref_y;
797 if (info->drag_cursors[i] != NULL)
799 if (display == gdk_cursor_get_display (info->drag_cursors[i]))
800 return info->drag_cursors[i];
802 g_object_unref (info->drag_cursors[i]);
803 info->drag_cursors[i] = NULL;
806 icon_x = info->hot_x;
807 icon_y = info->hot_y;
808 icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
809 icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);
812 cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
814 cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
817 if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
818 hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
820 if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
821 hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
824 /* The code below is an attempt to let cursor themes
825 * determine the attachment of the icon to enable things
826 * like the following:
834 * It does not work since Xcursor doesn't allow to attach
835 * any additional information to cursors in a retrievable
836 * way (there are comments, but no way to get at them
837 * short of searching for the actual cursor file).
838 * If this code ever gets used, the icon_window placement
839 * must be changed to recognize these placement options
840 * as well. Note that this code ignores info->hot_x/y.
842 for (j = 0; j < 10; j++)
847 GtkAnchorType icon_anchor;
849 g_snprintf (key, 32, "comment%d", j);
850 opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
851 if (opt && g_str_has_prefix ("icon-attach:", opt))
853 toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
854 if (g_strv_length (toks) != 3)
859 icon_anchor = atoi (toks[0]);
860 icon_x = atoi (toks[1]);
861 icon_y = atoi (toks[2]);
865 case GTK_ANCHOR_NORTH:
866 case GTK_ANCHOR_CENTER:
867 case GTK_ANCHOR_SOUTH:
868 icon_x += icon_width / 2;
870 case GTK_ANCHOR_NORTH_EAST:
871 case GTK_ANCHOR_EAST:
872 case GTK_ANCHOR_SOUTH_EAST:
873 icon_x += icon_width;
880 case GTK_ANCHOR_WEST:
881 case GTK_ANCHOR_CENTER:
882 case GTK_ANCHOR_EAST:
883 icon_y += icon_height / 2;
885 case GTK_ANCHOR_SOUTH_WEST:
886 case GTK_ANCHOR_SOUTH:
887 case GTK_ANCHOR_SOUTH_EAST:
888 icon_x += icon_height;
900 cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
901 cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
903 ref_x = MAX (hot_x, icon_x);
904 ref_y = MAX (hot_y, icon_y);
905 width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
906 height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
908 if (gtk_drag_can_use_rgba_cursor (display, width, height))
910 /* Composite cursor and icon so that both hotspots
911 * end up at (ref_x, ref_y)
913 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
916 gdk_pixbuf_fill (pixbuf, 0xff000000);
918 gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
919 ref_x - icon_x, ref_y - icon_y,
920 icon_width, icon_height,
921 ref_x - icon_x, ref_y - icon_y,
923 GDK_INTERP_BILINEAR, 255);
925 gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
926 ref_x - hot_x, ref_y - hot_y,
927 cursor_width, cursor_height,
928 ref_x - hot_x, ref_y - hot_y,
930 GDK_INTERP_BILINEAR, 255);
932 info->drag_cursors[i] =
933 gdk_cursor_new_from_pixbuf (display, pixbuf, ref_x, ref_y);
935 g_object_unref (pixbuf);
938 g_object_unref (cursor_pixbuf);
940 if (info->drag_cursors[i] != NULL)
941 return info->drag_cursors[i];
944 return drag_cursors[i].cursor;
948 gtk_drag_update_cursor (GtkDragSourceInfo *info)
953 if (!info->have_grab)
956 for (i = 0 ; i < G_N_ELEMENTS (drag_cursors) - 1; i++)
957 if (info->cursor == drag_cursors[i].cursor ||
958 info->cursor == info->drag_cursors[i])
961 if (i == G_N_ELEMENTS (drag_cursors))
964 cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor),
965 drag_cursors[i].action, info);
967 if (cursor != info->cursor)
971 pointer = gdk_drag_context_get_device (info->context);
972 gdk_device_grab (pointer,
973 gtk_widget_get_window (info->ipc_widget),
974 GDK_OWNERSHIP_APPLICATION, FALSE,
975 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
976 cursor, info->grab_time);
977 info->cursor = cursor;
981 /********************
983 ********************/
986 * gtk_drag_get_data: (method)
987 * @widget: the widget that will receive the
988 * #GtkWidget::drag-data-received signal.
989 * @context: the drag context
990 * @target: the target (form of the data) to retrieve.
991 * @time_: a timestamp for retrieving the data. This will
992 * generally be the time received in a #GtkWidget::drag-motion"
993 * or #GtkWidget::drag-drop" signal.
995 * Gets the data associated with a drag. When the data
996 * is received or the retrieval fails, GTK+ will emit a
997 * #GtkWidget::drag-data-received signal. Failure of the retrieval
998 * is indicated by the length field of the @selection_data
999 * signal parameter being negative. However, when gtk_drag_get_data()
1000 * is called implicitely because the %GTK_DEST_DEFAULT_DROP was set,
1001 * then the widget will not receive notification of failed
1005 gtk_drag_get_data (GtkWidget *widget,
1006 GdkDragContext *context,
1010 GtkWidget *selection_widget;
1012 g_return_if_fail (GTK_IS_WIDGET (widget));
1013 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1015 selection_widget = gtk_drag_get_ipc_widget (widget);
1017 g_object_ref (context);
1018 g_object_ref (widget);
1020 g_signal_connect (selection_widget, "selection-received",
1021 G_CALLBACK (gtk_drag_selection_received), widget);
1023 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1025 gtk_selection_convert (selection_widget,
1026 gdk_drag_get_selection (context),
1033 * gtk_drag_get_source_widget: (method)
1034 * @context: a (destination side) drag context
1036 * Determines the source widget for a drag.
1038 * Return value: (transfer none): if the drag is occurring
1039 * within a single application, a pointer to the source widget.
1043 gtk_drag_get_source_widget (GdkDragContext *context)
1047 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
1049 tmp_list = source_widgets;
1052 GtkWidget *ipc_widget = tmp_list->data;
1054 if (gtk_widget_get_window (ipc_widget) == gdk_drag_context_get_source_window (context))
1056 GtkDragSourceInfo *info;
1057 info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
1059 return info ? info->widget : NULL;
1062 tmp_list = tmp_list->next;
1069 * gtk_drag_finish: (method)
1070 * @context: the drag context.
1071 * @success: a flag indicating whether the drop was successful
1072 * @del: a flag indicating whether the source should delete the
1073 * original data. (This should be %TRUE for a move)
1074 * @time_: the timestamp from the #GtkWidget::drag-drop signal.
1076 * Informs the drag source that the drop is finished, and
1077 * that the data of the drag will no longer be required.
1080 gtk_drag_finish (GdkDragContext *context,
1085 GdkAtom target = GDK_NONE;
1087 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1091 target = gdk_atom_intern_static_string ("DELETE");
1093 else if (gdk_drag_context_get_protocol (context) == GDK_DRAG_PROTO_MOTIF)
1095 target = gdk_atom_intern_static_string (success ?
1096 "XmTRANSFER_SUCCESS" :
1097 "XmTRANSFER_FAILURE");
1100 if (target != GDK_NONE)
1102 GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_window_get_screen (gdk_drag_context_get_source_window (context)));
1104 g_object_ref (context);
1106 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1107 g_signal_connect (selection_widget, "selection-received",
1108 G_CALLBACK (gtk_drag_selection_received),
1111 gtk_selection_convert (selection_widget,
1112 gdk_drag_get_selection (context),
1117 if (!(success && del))
1118 gdk_drop_finish (context, success, time);
1121 /*************************************************************
1122 * gtk_drag_highlight_draw:
1123 * Callback for expose_event for highlighted widgets.
1129 *************************************************************/
1132 gtk_drag_highlight_draw (GtkWidget *widget,
1136 int width = gtk_widget_get_allocated_width (widget);
1137 int height = gtk_widget_get_allocated_height (widget);
1138 GtkStyleContext *context;
1140 context = gtk_widget_get_style_context (widget);
1142 gtk_style_context_save (context);
1143 gtk_style_context_add_class (context, GTK_STYLE_CLASS_DND);
1145 gtk_render_frame (context, cr, 0, 0, width, height);
1147 gtk_style_context_restore (context);
1149 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
1150 cairo_set_line_width (cr, 1.0);
1151 cairo_rectangle (cr,
1153 width - 1, height - 1);
1160 * gtk_drag_highlight: (method)
1161 * @widget: a widget to highlight
1163 * Draws a highlight around a widget. This will attach
1164 * handlers to #GtkWidget::draw, so the highlight
1165 * will continue to be displayed until gtk_drag_unhighlight()
1169 gtk_drag_highlight (GtkWidget *widget)
1171 g_return_if_fail (GTK_IS_WIDGET (widget));
1173 g_signal_connect_after (widget, "draw",
1174 G_CALLBACK (gtk_drag_highlight_draw),
1177 gtk_widget_queue_draw (widget);
1181 * gtk_drag_unhighlight: (method)
1182 * @widget: a widget to remove the highlight from.
1184 * Removes a highlight set by gtk_drag_highlight() from
1188 gtk_drag_unhighlight (GtkWidget *widget)
1190 g_return_if_fail (GTK_IS_WIDGET (widget));
1192 g_signal_handlers_disconnect_by_func (widget,
1193 gtk_drag_highlight_draw,
1196 gtk_widget_queue_draw (widget);
1200 gtk_drag_dest_set_internal (GtkWidget *widget,
1201 GtkDragDestSite *site)
1203 GtkDragDestSite *old_site;
1205 g_return_if_fail (widget != NULL);
1207 /* HACK, do this in the destroy */
1208 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1211 g_signal_handlers_disconnect_by_func (widget,
1212 gtk_drag_dest_realized,
1214 g_signal_handlers_disconnect_by_func (widget,
1215 gtk_drag_dest_hierarchy_changed,
1218 site->track_motion = old_site->track_motion;
1221 if (gtk_widget_get_realized (widget))
1222 gtk_drag_dest_realized (widget);
1224 g_signal_connect (widget, "realize",
1225 G_CALLBACK (gtk_drag_dest_realized), site);
1226 g_signal_connect (widget, "hierarchy-changed",
1227 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1229 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
1230 site, gtk_drag_dest_site_destroy);
1234 * gtk_drag_dest_set: (method)
1235 * @widget: a #GtkWidget
1236 * @flags: which types of default drag behavior to use
1237 * @targets: (allow-none) (array length=n_targets): a pointer to an array of #GtkTargetEntry<!-- -->s
1238 * indicating the drop types that this @widget will accept, or %NULL.
1239 * Later you can access the list with gtk_drag_dest_get_target_list()
1240 * and gtk_drag_dest_find_target().
1241 * @n_targets: the number of entries in @targets
1242 * @actions: a bitmask of possible actions for a drop onto this @widget.
1244 * Sets a widget as a potential drop destination, and adds default behaviors.
1246 * The default behaviors listed in @flags have an effect similar
1247 * to installing default handlers for the widget's drag-and-drop signals
1248 * (#GtkWidget::drag-motion, #GtkWidget::drag-drop, ...). They all exist
1249 * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
1250 * sufficient to connect to the widget's #GtkWidget::drag-data-received
1251 * signal to get primitive, but consistent drag-and-drop support.
1253 * Things become more complicated when you try to preview the dragged data,
1254 * as described in the documentation for #GtkWidget::drag-motion. The default
1255 * behaviors described by @flags make some assumptions, that can conflict
1256 * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
1257 * invokations of gdk_drag_status() in the context of #GtkWidget::drag-motion,
1258 * and invokations of gtk_drag_finish() in #GtkWidget::drag-data-received.
1259 * Especially the later is dramatic, when your own #GtkWidget::drag-motion
1260 * handler calls gtk_drag_get_data() to inspect the dragged data.
1262 * There's no way to set a default action here, you can use the
1263 * #GtkWidget::drag-motion callback for that. Here's an example which selects
1264 * the action to use depending on whether the control key is pressed or not:
1267 * drag_motion (GtkWidget *widget,
1268 * GdkDragContext *context,
1273 * GdkModifierType mask;
1275 * gdk_window_get_pointer (gtk_widget_get_window (widget),
1276 * NULL, NULL, &mask);
1277 * if (mask & GDK_CONTROL_MASK)
1278 * gdk_drag_status (context, GDK_ACTION_COPY, time);
1280 * gdk_drag_status (context, GDK_ACTION_MOVE, time);
1285 gtk_drag_dest_set (GtkWidget *widget,
1286 GtkDestDefaults flags,
1287 const GtkTargetEntry *targets,
1289 GdkDragAction actions)
1291 GtkDragDestSite *site;
1293 g_return_if_fail (GTK_IS_WIDGET (widget));
1295 site = g_slice_new0 (GtkDragDestSite);
1297 site->flags = flags;
1298 site->have_drag = FALSE;
1300 site->target_list = gtk_target_list_new (targets, n_targets);
1302 site->target_list = NULL;
1303 site->actions = actions;
1304 site->do_proxy = FALSE;
1305 site->proxy_window = NULL;
1306 site->track_motion = FALSE;
1308 gtk_drag_dest_set_internal (widget, site);
1312 * gtk_drag_dest_set_proxy: (method)
1313 * @widget: a #GtkWidget
1314 * @proxy_window: the window to which to forward drag events
1315 * @protocol: the drag protocol which the @proxy_window accepts
1316 * (You can use gdk_drag_get_protocol() to determine this)
1317 * @use_coordinates: If %TRUE, send the same coordinates to the
1318 * destination, because it is an embedded
1321 * Sets this widget as a proxy for drops to another window.
1324 gtk_drag_dest_set_proxy (GtkWidget *widget,
1325 GdkWindow *proxy_window,
1326 GdkDragProtocol protocol,
1327 gboolean use_coordinates)
1329 GtkDragDestSite *site;
1331 g_return_if_fail (GTK_IS_WIDGET (widget));
1332 g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1334 site = g_slice_new (GtkDragDestSite);
1337 site->have_drag = FALSE;
1338 site->target_list = NULL;
1340 site->proxy_window = proxy_window;
1342 g_object_ref (proxy_window);
1343 site->do_proxy = TRUE;
1344 site->proxy_protocol = protocol;
1345 site->proxy_coords = use_coordinates;
1346 site->track_motion = FALSE;
1348 gtk_drag_dest_set_internal (widget, site);
1352 * gtk_drag_dest_unset: (method)
1353 * @widget: a #GtkWidget
1355 * Clears information about a drop destination set with
1356 * gtk_drag_dest_set(). The widget will no longer receive
1357 * notification of drags.
1360 gtk_drag_dest_unset (GtkWidget *widget)
1362 GtkDragDestSite *old_site;
1364 g_return_if_fail (GTK_IS_WIDGET (widget));
1366 old_site = g_object_get_data (G_OBJECT (widget),
1370 g_signal_handlers_disconnect_by_func (widget,
1371 gtk_drag_dest_realized,
1373 g_signal_handlers_disconnect_by_func (widget,
1374 gtk_drag_dest_hierarchy_changed,
1378 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1382 * gtk_drag_dest_get_target_list: (method)
1383 * @widget: a #GtkWidget
1385 * Returns the list of targets this widget can accept from
1388 * Return value: (transfer none): the #GtkTargetList, or %NULL if none
1391 gtk_drag_dest_get_target_list (GtkWidget *widget)
1393 GtkDragDestSite *site;
1395 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1397 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1399 return site ? site->target_list : NULL;
1403 * gtk_drag_dest_set_target_list: (method)
1404 * @widget: a #GtkWidget that's a drag destination
1405 * @target_list: (allow-none): list of droppable targets, or %NULL for none
1407 * Sets the target types that this widget can accept from drag-and-drop.
1408 * The widget must first be made into a drag destination with
1409 * gtk_drag_dest_set().
1412 gtk_drag_dest_set_target_list (GtkWidget *widget,
1413 GtkTargetList *target_list)
1415 GtkDragDestSite *site;
1417 g_return_if_fail (GTK_IS_WIDGET (widget));
1419 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1423 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1424 "to make the widget into a drag destination");
1429 gtk_target_list_ref (target_list);
1431 if (site->target_list)
1432 gtk_target_list_unref (site->target_list);
1434 site->target_list = target_list;
1438 * gtk_drag_dest_add_text_targets: (method)
1439 * @widget: a #GtkWidget that's a drag destination
1441 * Add the text targets supported by #GtkSelection to
1442 * the target list of the drag destination. The targets
1443 * are added with @info = 0. If you need another value,
1444 * use gtk_target_list_add_text_targets() and
1445 * gtk_drag_dest_set_target_list().
1450 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1452 GtkTargetList *target_list;
1454 target_list = gtk_drag_dest_get_target_list (widget);
1456 gtk_target_list_ref (target_list);
1458 target_list = gtk_target_list_new (NULL, 0);
1459 gtk_target_list_add_text_targets (target_list, 0);
1460 gtk_drag_dest_set_target_list (widget, target_list);
1461 gtk_target_list_unref (target_list);
1465 * gtk_drag_dest_add_image_targets: (method)
1466 * @widget: a #GtkWidget that's a drag destination
1468 * Add the image targets supported by #GtkSelection to
1469 * the target list of the drag destination. The targets
1470 * are added with @info = 0. If you need another value,
1471 * use gtk_target_list_add_image_targets() and
1472 * gtk_drag_dest_set_target_list().
1477 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1479 GtkTargetList *target_list;
1481 target_list = gtk_drag_dest_get_target_list (widget);
1483 gtk_target_list_ref (target_list);
1485 target_list = gtk_target_list_new (NULL, 0);
1486 gtk_target_list_add_image_targets (target_list, 0, FALSE);
1487 gtk_drag_dest_set_target_list (widget, target_list);
1488 gtk_target_list_unref (target_list);
1492 * gtk_drag_dest_add_uri_targets: (method)
1493 * @widget: a #GtkWidget that's a drag destination
1495 * Add the URI targets supported by #GtkSelection to
1496 * the target list of the drag destination. The targets
1497 * are added with @info = 0. If you need another value,
1498 * use gtk_target_list_add_uri_targets() and
1499 * gtk_drag_dest_set_target_list().
1504 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1506 GtkTargetList *target_list;
1508 target_list = gtk_drag_dest_get_target_list (widget);
1510 gtk_target_list_ref (target_list);
1512 target_list = gtk_target_list_new (NULL, 0);
1513 gtk_target_list_add_uri_targets (target_list, 0);
1514 gtk_drag_dest_set_target_list (widget, target_list);
1515 gtk_target_list_unref (target_list);
1519 * gtk_drag_dest_set_track_motion: (method)
1520 * @widget: a #GtkWidget that's a drag destination
1521 * @track_motion: whether to accept all targets
1523 * Tells the widget to emit #GtkWidget::drag-motion and
1524 * #GtkWidget::drag-leave events regardless of the targets and the
1525 * %GTK_DEST_DEFAULT_MOTION flag.
1527 * This may be used when a widget wants to do generic
1528 * actions regardless of the targets that the source offers.
1533 gtk_drag_dest_set_track_motion (GtkWidget *widget,
1534 gboolean track_motion)
1536 GtkDragDestSite *site;
1538 g_return_if_fail (GTK_IS_WIDGET (widget));
1540 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1542 g_return_if_fail (site != NULL);
1544 site->track_motion = track_motion != FALSE;
1548 * gtk_drag_dest_get_track_motion: (method)
1549 * @widget: a #GtkWidget that's a drag destination
1551 * Returns whether the widget has been configured to always
1552 * emit #GtkWidget::drag-motion signals.
1554 * Return Value: %TRUE if the widget always emits
1555 * #GtkWidget::drag-motion events
1560 gtk_drag_dest_get_track_motion (GtkWidget *widget)
1562 GtkDragDestSite *site;
1564 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1566 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1569 return site->track_motion;
1574 /*************************************************************
1575 * _gtk_drag_dest_handle_event:
1576 * Called from widget event handling code on Drag events
1580 * toplevel: Toplevel widget that received the event
1583 *************************************************************/
1586 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1589 GtkDragDestInfo *info;
1590 GdkDragContext *context;
1592 g_return_if_fail (toplevel != NULL);
1593 g_return_if_fail (event != NULL);
1595 context = event->dnd.context;
1597 info = gtk_drag_get_dest_info (context, TRUE);
1599 /* Find the widget for the event */
1600 switch (event->type)
1602 case GDK_DRAG_ENTER:
1605 case GDK_DRAG_LEAVE:
1608 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1609 info->widget = NULL;
1613 case GDK_DRAG_MOTION:
1614 case GDK_DROP_START:
1620 if (event->type == GDK_DROP_START)
1622 info->dropped = TRUE;
1623 /* We send a leave here so that the widget unhighlights
1628 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1629 info->widget = NULL;
1633 window = gtk_widget_get_window (toplevel);
1635 #ifdef GDK_WINDOWING_X11
1636 /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1638 * Currently gdk_window_get_position doesn't provide reliable
1639 * information for embedded windows, so we call the much more
1640 * expensive gdk_window_get_origin().
1642 if (GTK_IS_PLUG (toplevel))
1643 gdk_window_get_origin (window, &tx, &ty);
1645 #endif /* GDK_WINDOWING_X11 */
1646 gdk_window_get_position (window, &tx, &ty);
1648 found = gtk_drag_find_widget (toplevel,
1651 event->dnd.x_root - tx,
1652 event->dnd.y_root - ty,
1654 (event->type == GDK_DRAG_MOTION) ?
1655 gtk_drag_dest_motion :
1656 gtk_drag_dest_drop);
1658 if (info->widget && !found)
1660 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1661 info->widget = NULL;
1666 if (event->type == GDK_DRAG_MOTION)
1669 gdk_drag_status (context, 0, event->dnd.time);
1671 else if (event->type == GDK_DROP_START && !info->proxy_source)
1673 gdk_drop_reply (context, found, event->dnd.time);
1674 if ((gdk_drag_context_get_protocol (context) == GDK_DRAG_PROTO_MOTIF) && !found)
1675 gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1681 g_assert_not_reached ();
1686 * gtk_drag_dest_find_target: (method)
1687 * @widget: drag destination widget
1688 * @context: drag context
1689 * @target_list: (allow-none): list of droppable targets, or %NULL to use
1690 * gtk_drag_dest_get_target_list (@widget).
1692 * Looks for a match between the supported targets of @context and the
1693 * @dest_target_list, returning the first matching target, otherwise
1694 * returning %GDK_NONE. @dest_target_list should usually be the return
1695 * value from gtk_drag_dest_get_target_list(), but some widgets may
1696 * have different valid targets for different parts of the widget; in
1697 * that case, they will have to implement a drag_motion handler that
1698 * passes the correct target list to this function.
1700 * Return value: (transfer none): first target that the source offers
1701 * and the dest can accept, or %GDK_NONE
1704 gtk_drag_dest_find_target (GtkWidget *widget,
1705 GdkDragContext *context,
1706 GtkTargetList *target_list)
1709 GList *tmp_source = NULL;
1710 GtkWidget *source_widget;
1712 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1713 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1716 source_widget = gtk_drag_get_source_widget (context);
1718 if (target_list == NULL)
1719 target_list = gtk_drag_dest_get_target_list (widget);
1721 if (target_list == NULL)
1724 tmp_target = target_list->list;
1727 GtkTargetPair *pair = tmp_target->data;
1728 tmp_source = gdk_drag_context_list_targets (context);
1731 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1733 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1734 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)) &&
1735 (!(pair->flags & GTK_TARGET_OTHER_APP) || !source_widget) &&
1736 (!(pair->flags & GTK_TARGET_OTHER_WIDGET) || (source_widget != widget)))
1737 return pair->target;
1741 tmp_source = tmp_source->next;
1743 tmp_target = tmp_target->next;
1750 gtk_drag_selection_received (GtkWidget *widget,
1751 GtkSelectionData *selection_data,
1755 GdkDragContext *context;
1756 GtkDragDestInfo *info;
1757 GtkWidget *drop_widget;
1762 context = g_object_get_data (G_OBJECT (widget), "drag-context");
1763 info = gtk_drag_get_dest_info (context, FALSE);
1765 if (info->proxy_data &&
1766 gtk_selection_data_get_target (info->proxy_data) == gtk_selection_data_get_target (selection_data))
1768 gtk_selection_data_set (info->proxy_data,
1769 gtk_selection_data_get_data_type (selection_data),
1770 gtk_selection_data_get_format (selection_data),
1771 gtk_selection_data_get_data (selection_data),
1772 gtk_selection_data_get_length (selection_data));
1777 target = gtk_selection_data_get_target (selection_data);
1778 if (target == gdk_atom_intern_static_string ("DELETE"))
1780 gtk_drag_finish (context, TRUE, FALSE, time);
1782 else if ((target == gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS")) ||
1783 (target == gdk_atom_intern_static_string ("XmTRANSFER_FAILURE")))
1789 GtkDragDestSite *site;
1791 site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1793 if (site && site->target_list)
1797 if (gtk_target_list_find (site->target_list,
1801 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1802 gtk_selection_data_get_length (selection_data) >= 0)
1803 g_signal_emit_by_name (drop_widget,
1804 "drag-data-received",
1805 context, info->drop_x, info->drop_y,
1812 g_signal_emit_by_name (drop_widget,
1813 "drag-data-received",
1814 context, info->drop_x, info->drop_y,
1819 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1822 gtk_drag_finish (context,
1823 (gtk_selection_data_get_length (selection_data) >= 0),
1824 (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
1828 g_object_unref (drop_widget);
1831 g_signal_handlers_disconnect_by_func (widget,
1832 gtk_drag_selection_received,
1835 g_object_set_data (G_OBJECT (widget), I_("drag-context"), NULL);
1836 g_object_unref (context);
1838 gtk_drag_release_ipc_widget (widget);
1841 /*************************************************************
1842 * gtk_drag_find_widget:
1843 * Function used to locate widgets for
1844 * DRAG_MOTION and DROP_START events.
1845 *************************************************************/
1848 gtk_drag_find_widget (GtkWidget *widget,
1849 GdkDragContext *context,
1850 GtkDragDestInfo *info,
1854 GtkDragDestCallback callback)
1856 if (!gtk_widget_get_mapped (widget) ||
1857 !gtk_widget_get_sensitive (widget))
1860 /* Get the widget at the pointer coordinates and travel up
1861 * the widget hierarchy from there.
1863 widget = _gtk_widget_find_at_coords (gtk_widget_get_window (widget),
1871 GList *hierarchy = NULL;
1872 gboolean found = FALSE;
1874 if (!gtk_widget_get_mapped (widget) ||
1875 !gtk_widget_get_sensitive (widget))
1878 /* need to reference the entire hierarchy temporarily in case the
1879 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
1881 for (parent = widget;
1883 parent = gtk_widget_get_parent (parent))
1885 hierarchy = g_list_prepend (hierarchy, g_object_ref (parent));
1888 /* If the current widget is registered as a drop site, check to
1889 * emit "drag-motion" to check if we are actually in a drop
1892 if (g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1894 found = callback (widget, context, x, y, time);
1896 /* If so, send a "drag-leave" to the last widget */
1899 if (info->widget && info->widget != widget)
1901 gtk_drag_dest_leave (info->widget, context, time);
1904 info->widget = widget;
1910 /* Get the parent before unreffing the hierarchy because
1911 * invoking the callback might have destroyed the widget
1913 parent = gtk_widget_get_parent (widget);
1915 /* The parent might be going away when unreffing the
1916 * hierarchy, so also protect againt that
1919 g_object_add_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1922 g_list_foreach (hierarchy, (GFunc) g_object_unref, NULL);
1923 g_list_free (hierarchy);
1929 g_object_remove_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1933 if (!gtk_widget_translate_coordinates (widget, parent, x, y, &x, &y))
1943 gtk_drag_proxy_begin (GtkWidget *widget,
1944 GtkDragDestInfo *dest_info,
1947 GtkDragSourceInfo *source_info;
1949 GdkDragContext *context;
1950 GtkWidget *ipc_widget;
1952 if (dest_info->proxy_source)
1954 gdk_drag_abort (dest_info->proxy_source->context, time);
1955 gtk_drag_source_info_destroy (dest_info->proxy_source);
1956 dest_info->proxy_source = NULL;
1959 ipc_widget = gtk_drag_get_ipc_widget (widget);
1960 context = gdk_drag_begin (gtk_widget_get_window (ipc_widget),
1961 gdk_drag_context_list_targets (dest_info->context));
1963 source_info = gtk_drag_get_source_info (context, TRUE);
1965 source_info->ipc_widget = ipc_widget;
1966 source_info->widget = g_object_ref (widget);
1968 source_info->target_list = gtk_target_list_new (NULL, 0);
1969 tmp_list = gdk_drag_context_list_targets (dest_info->context);
1972 gtk_target_list_add (source_info->target_list,
1973 GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
1974 tmp_list = tmp_list->next;
1977 source_info->proxy_dest = dest_info;
1979 g_signal_connect (ipc_widget,
1981 G_CALLBACK (gtk_drag_selection_get),
1984 dest_info->proxy_source = source_info;
1988 gtk_drag_dest_info_destroy (gpointer data)
1990 g_slice_free (GtkDragDestInfo, data);
1993 static GtkDragDestInfo *
1994 gtk_drag_get_dest_info (GdkDragContext *context,
1997 GtkDragDestInfo *info;
1998 static GQuark info_quark = 0;
2000 info_quark = g_quark_from_static_string ("gtk-dest-info");
2002 info = g_object_get_qdata (G_OBJECT (context), info_quark);
2003 if (!info && create)
2005 info = g_slice_new0 (GtkDragDestInfo);
2006 info->context = context;
2007 g_object_set_qdata_full (G_OBJECT (context), info_quark,
2008 info, gtk_drag_dest_info_destroy);
2014 static GQuark dest_info_quark = 0;
2016 static GtkDragSourceInfo *
2017 gtk_drag_get_source_info (GdkDragContext *context,
2020 GtkDragSourceInfo *info;
2021 if (!dest_info_quark)
2022 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
2024 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
2025 if (!info && create)
2027 info = g_new0 (GtkDragSourceInfo, 1);
2028 info->context = context;
2029 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
2036 gtk_drag_clear_source_info (GdkDragContext *context)
2038 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
2042 gtk_drag_dest_realized (GtkWidget *widget)
2044 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2046 if (gtk_widget_is_toplevel (toplevel))
2047 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2051 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
2052 GtkWidget *previous_toplevel)
2054 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2056 if (gtk_widget_is_toplevel (toplevel) && gtk_widget_get_realized (toplevel))
2057 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2061 gtk_drag_dest_site_destroy (gpointer data)
2063 GtkDragDestSite *site = data;
2065 if (site->proxy_window)
2066 g_object_unref (site->proxy_window);
2068 if (site->target_list)
2069 gtk_target_list_unref (site->target_list);
2071 g_slice_free (GtkDragDestSite, site);
2075 * Default drag handlers
2078 gtk_drag_dest_leave (GtkWidget *widget,
2079 GdkDragContext *context,
2082 GtkDragDestSite *site;
2084 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2085 g_return_if_fail (site != NULL);
2089 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2091 if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
2093 gdk_drag_abort (info->proxy_source->context, time);
2094 gtk_drag_source_info_destroy (info->proxy_source);
2095 info->proxy_source = NULL;
2102 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
2103 gtk_drag_unhighlight (widget);
2105 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
2107 g_signal_emit_by_name (widget, "drag-leave", context, time);
2109 site->have_drag = FALSE;
2114 gtk_drag_dest_motion (GtkWidget *widget,
2115 GdkDragContext *context,
2120 GtkDragDestSite *site;
2121 GdkDragAction action = 0;
2124 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2125 g_return_val_if_fail (site != NULL, FALSE);
2130 GdkEvent *current_event;
2131 GdkWindow *dest_window;
2132 GdkDragProtocol proto;
2134 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2136 if (!info->proxy_source || info->proxy_source->widget != widget)
2137 gtk_drag_proxy_begin (widget, info, time);
2139 current_event = gtk_get_current_event ();
2141 if (site->proxy_window)
2143 dest_window = site->proxy_window;
2144 proto = site->proxy_protocol;
2148 gdk_drag_find_window_for_screen (info->proxy_source->context,
2150 gdk_window_get_screen (current_event->dnd.window),
2151 current_event->dnd.x_root,
2152 current_event->dnd.y_root,
2153 &dest_window, &proto);
2156 gdk_drag_motion (info->proxy_source->context,
2158 current_event->dnd.x_root,
2159 current_event->dnd.y_root,
2160 gdk_drag_context_get_suggested_action (context),
2161 gdk_drag_context_get_actions (context),
2164 if (!site->proxy_window && dest_window)
2165 g_object_unref (dest_window);
2167 selection = gdk_drag_get_selection (info->proxy_source->context);
2169 selection != gdk_drag_get_selection (info->context))
2170 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2172 gdk_event_free (current_event);
2177 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
2179 if (gdk_drag_context_get_suggested_action (context) & site->actions)
2180 action = gdk_drag_context_get_suggested_action (context);
2185 for (i = 0; i < 8; i++)
2187 if ((site->actions & (1 << i)) &&
2188 (gdk_drag_context_get_actions (context) & (1 << i)))
2196 if (action && gtk_drag_dest_find_target (widget, context, NULL))
2198 if (!site->have_drag)
2200 site->have_drag = TRUE;
2201 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
2202 gtk_drag_highlight (widget);
2205 gdk_drag_status (context, action, time);
2209 gdk_drag_status (context, 0, time);
2210 if (!site->track_motion)
2215 g_signal_emit_by_name (widget, "drag-motion",
2216 context, x, y, time, &retval);
2218 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
2222 gtk_drag_dest_drop (GtkWidget *widget,
2223 GdkDragContext *context,
2228 GtkDragDestSite *site;
2229 GtkDragDestInfo *info;
2231 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2232 g_return_val_if_fail (site != NULL, FALSE);
2234 info = gtk_drag_get_dest_info (context, FALSE);
2235 g_return_val_if_fail (info != NULL, FALSE);
2242 if (info->proxy_source ||
2243 (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_ROOTWIN))
2245 gtk_drag_drop (info->proxy_source, time);
2249 /* We need to synthesize a motion event, wait for a status,
2250 * and, if we get a good one, do a drop.
2253 GdkEvent *current_event;
2255 GdkWindow *dest_window;
2256 GdkDragProtocol proto;
2258 gtk_drag_proxy_begin (widget, info, time);
2259 info->proxy_drop_wait = TRUE;
2260 info->proxy_drop_time = time;
2262 current_event = gtk_get_current_event ();
2264 if (site->proxy_window)
2266 dest_window = site->proxy_window;
2267 proto = site->proxy_protocol;
2271 gdk_drag_find_window_for_screen (info->proxy_source->context,
2273 gdk_window_get_screen (current_event->dnd.window),
2274 current_event->dnd.x_root,
2275 current_event->dnd.y_root,
2276 &dest_window, &proto);
2279 gdk_drag_motion (info->proxy_source->context,
2281 current_event->dnd.x_root,
2282 current_event->dnd.y_root,
2283 gdk_drag_context_get_suggested_action (context),
2284 gdk_drag_context_get_actions (context),
2287 if (!site->proxy_window && dest_window)
2288 g_object_unref (dest_window);
2290 selection = gdk_drag_get_selection (info->proxy_source->context);
2292 selection != gdk_drag_get_selection (info->context))
2293 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2295 gdk_event_free (current_event);
2304 if (site->flags & GTK_DEST_DEFAULT_DROP)
2306 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2308 if (target == GDK_NONE)
2310 gtk_drag_finish (context, FALSE, FALSE, time);
2314 gtk_drag_get_data (widget, context, target, time);
2317 g_signal_emit_by_name (widget, "drag-drop",
2318 context, x, y, time, &retval);
2320 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2328 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2329 * so that we can set the icon from the source site information
2331 static GdkDragContext *
2332 gtk_drag_begin_internal (GtkWidget *widget,
2333 GtkDragSourceSite *site,
2334 GtkTargetList *target_list,
2335 GdkDragAction actions,
2339 GtkDragSourceInfo *info;
2340 GList *targets = NULL;
2342 guint32 time = GDK_CURRENT_TIME;
2343 GdkDragAction possible_actions, suggested_action;
2344 GdkDragContext *context;
2345 GtkWidget *ipc_widget;
2347 GdkDevice *pointer, *keyboard;
2348 GdkWindow *ipc_window;
2350 pointer = keyboard = NULL;
2351 ipc_widget = gtk_drag_get_ipc_widget (widget);
2353 gtk_drag_get_event_actions (event, button, actions,
2354 &suggested_action, &possible_actions);
2356 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2362 time = gdk_event_get_time (event);
2363 if (time == GDK_CURRENT_TIME)
2364 time = gtk_get_current_event_time ();
2366 pointer = gdk_event_get_device (event);
2368 if (gdk_device_get_source (pointer) == GDK_SOURCE_KEYBOARD)
2371 pointer = gdk_device_get_associated_device (keyboard);
2374 keyboard = gdk_device_get_associated_device (pointer);
2378 GdkDeviceManager *device_manager;
2380 device_manager = gdk_display_get_device_manager (gtk_widget_get_display (widget));
2381 pointer = gdk_device_manager_get_client_pointer (device_manager);
2382 keyboard = gdk_device_get_associated_device (pointer);
2388 ipc_window = gtk_widget_get_window (ipc_widget);
2390 if (gdk_device_grab (pointer, ipc_window,
2391 GDK_OWNERSHIP_APPLICATION, FALSE,
2392 GDK_POINTER_MOTION_MASK |
2393 GDK_BUTTON_RELEASE_MASK,
2394 cursor, time) != GDK_GRAB_SUCCESS)
2396 gtk_drag_release_ipc_widget (ipc_widget);
2401 grab_dnd_keys (ipc_widget, keyboard, time);
2403 /* We use a GTK grab here to override any grabs that the widget
2404 * we are dragging from might have held
2406 gtk_device_grab_add (ipc_widget, pointer, FALSE);
2408 tmp_list = g_list_last (target_list->list);
2411 GtkTargetPair *pair = tmp_list->data;
2412 targets = g_list_prepend (targets,
2413 GINT_TO_POINTER (pair->target));
2414 tmp_list = tmp_list->prev;
2417 source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2419 context = gdk_drag_begin (ipc_window, targets);
2420 gdk_drag_context_set_device (context, pointer);
2421 g_list_free (targets);
2423 info = gtk_drag_get_source_info (context, TRUE);
2425 info->ipc_widget = ipc_widget;
2426 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), info);
2428 info->widget = g_object_ref (widget);
2430 info->button = button;
2431 info->cursor = cursor;
2432 info->target_list = target_list;
2433 gtk_target_list_ref (target_list);
2435 info->possible_actions = actions;
2437 info->status = GTK_DRAG_STATUS_DRAG;
2438 info->last_event = NULL;
2439 info->selections = NULL;
2440 info->icon_window = NULL;
2441 info->destroy_icon = FALSE;
2443 /* Set cur_x, cur_y here so if the "drag-begin" signal shows
2444 * the drag icon, it will be in the right place
2446 if (event && event->type == GDK_MOTION_NOTIFY)
2448 info->cur_screen = gtk_widget_get_screen (widget);
2449 info->cur_x = event->motion.x_root;
2450 info->cur_y = event->motion.y_root;
2454 gdk_device_get_position (pointer, &info->cur_screen, &info->cur_x, &info->cur_y);
2457 g_signal_emit_by_name (widget, "drag-begin", info->context);
2459 /* Ensure that we have an icon before we start the drag; the
2460 * application may have set one in ::drag_begin, or it may
2463 if (!info->icon_window && !info->icon_pixbuf)
2465 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2466 gtk_drag_set_icon_default (context);
2468 switch (site->icon_type)
2470 case GTK_IMAGE_PIXBUF:
2471 gtk_drag_set_icon_pixbuf (context,
2472 site->icon_data.pixbuf.pixbuf,
2475 case GTK_IMAGE_STOCK:
2476 gtk_drag_set_icon_stock (context,
2477 site->icon_data.stock.stock_id,
2480 case GTK_IMAGE_ICON_NAME:
2481 gtk_drag_set_icon_name (context,
2482 site->icon_data.name.icon_name,
2485 case GTK_IMAGE_GICON:
2486 gtk_drag_set_icon_gicon (context,
2487 site->icon_data.gicon.icon,
2490 case GTK_IMAGE_EMPTY:
2492 g_assert_not_reached();
2497 /* We need to composite the icon into the cursor, if we are
2498 * not using an icon window.
2500 if (info->icon_pixbuf)
2502 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2506 if (cursor != info->cursor)
2508 gdk_device_grab (pointer, gtk_widget_get_window (widget),
2509 GDK_OWNERSHIP_APPLICATION, FALSE,
2510 GDK_POINTER_MOTION_MASK |
2511 GDK_BUTTON_RELEASE_MASK,
2513 info->cursor = cursor;
2517 if (event && event->type == GDK_MOTION_NOTIFY)
2518 gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2520 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, event);
2522 info->start_x = info->cur_x;
2523 info->start_y = info->cur_y;
2525 g_signal_connect (info->ipc_widget, "grab-broken-event",
2526 G_CALLBACK (gtk_drag_grab_broken_event_cb), info);
2527 g_signal_connect (info->ipc_widget, "grab-notify",
2528 G_CALLBACK (gtk_drag_grab_notify_cb), info);
2529 g_signal_connect (info->ipc_widget, "button-release-event",
2530 G_CALLBACK (gtk_drag_button_release_cb), info);
2531 g_signal_connect (info->ipc_widget, "motion-notify-event",
2532 G_CALLBACK (gtk_drag_motion_cb), info);
2533 g_signal_connect (info->ipc_widget, "key-press-event",
2534 G_CALLBACK (gtk_drag_key_cb), info);
2535 g_signal_connect (info->ipc_widget, "key-release-event",
2536 G_CALLBACK (gtk_drag_key_cb), info);
2537 g_signal_connect (info->ipc_widget, "selection-get",
2538 G_CALLBACK (gtk_drag_selection_get), info);
2540 info->have_grab = TRUE;
2541 info->grab_time = time;
2543 return info->context;
2547 * gtk_drag_begin: (method)
2548 * @widget: the source widget.
2549 * @targets: The targets (data formats) in which the
2550 * source can provide the data.
2551 * @actions: A bitmask of the allowed drag actions for this drag.
2552 * @button: The button the user clicked to start the drag.
2553 * @event: The event that triggered the start of the drag.
2555 * Initiates a drag on the source side. The function
2556 * only needs to be used when the application is
2557 * starting drags itself, and is not needed when
2558 * gtk_drag_source_set() is used.
2560 * The @event is used to retrieve the timestamp that will be used internally to
2561 * grab the pointer. If @event is #NULL, then GDK_CURRENT_TIME will be used.
2562 * However, you should try to pass a real event in all cases, since that can be
2563 * used by GTK+ to get information about the start position of the drag, for
2564 * example if the @event is a %GDK_MOTION_NOTIFY.
2566 * Generally there are three cases when you want to start a drag by hand by
2567 * calling this function:
2569 * 1. During a #GtkWidget::button-press-event handler, if you want to start a drag
2570 * immediately when the user presses the mouse button. Pass the @event
2571 * that you have in your #GtkWidget::button-press-event handler.
2573 * 2. During a #GtkWidget::motion-notify-event handler, if you want to start a drag
2574 * when the mouse moves past a certain threshold distance after a button-press.
2575 * Pass the @event that you have in your #GtkWidget::motion-notify-event handler.
2577 * 3. During a timeout handler, if you want to start a drag after the mouse
2578 * button is held down for some time. Try to save the last event that you got
2579 * from the mouse, using gdk_event_copy(), and pass it to this function
2580 * (remember to free the event with gdk_event_free() when you are done).
2581 * If you can really not pass a real event, pass #NULL instead.
2583 * Return value: (transfer none): the context for this drag.
2586 gtk_drag_begin (GtkWidget *widget,
2587 GtkTargetList *targets,
2588 GdkDragAction actions,
2592 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2593 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
2594 g_return_val_if_fail (targets != NULL, NULL);
2596 return gtk_drag_begin_internal (widget, NULL, targets,
2597 actions, button, event);
2601 * gtk_drag_source_set: (method)
2602 * @widget: a #GtkWidget
2603 * @start_button_mask: the bitmask of buttons that can start the drag
2604 * @targets: (allow-none) (array length=n_targets): the table of targets that the drag will support,
2606 * @n_targets: the number of items in @targets
2607 * @actions: the bitmask of possible actions for a drag from this widget
2609 * Sets up a widget so that GTK+ will start a drag operation when the user
2610 * clicks and drags on the widget. The widget must have a window.
2613 gtk_drag_source_set (GtkWidget *widget,
2614 GdkModifierType start_button_mask,
2615 const GtkTargetEntry *targets,
2617 GdkDragAction actions)
2619 GtkDragSourceSite *site;
2621 g_return_if_fail (GTK_IS_WIDGET (widget));
2623 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2625 gtk_widget_add_events (widget,
2626 gtk_widget_get_events (widget) |
2627 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2628 GDK_BUTTON_MOTION_MASK);
2632 if (site->target_list)
2633 gtk_target_list_unref (site->target_list);
2637 site = g_slice_new0 (GtkDragSourceSite);
2639 site->icon_type = GTK_IMAGE_EMPTY;
2641 g_signal_connect (widget, "button-press-event",
2642 G_CALLBACK (gtk_drag_source_event_cb),
2644 g_signal_connect (widget, "button-release-event",
2645 G_CALLBACK (gtk_drag_source_event_cb),
2647 g_signal_connect (widget, "motion-notify-event",
2648 G_CALLBACK (gtk_drag_source_event_cb),
2651 g_object_set_data_full (G_OBJECT (widget),
2652 I_("gtk-site-data"),
2653 site, gtk_drag_source_site_destroy);
2656 site->start_button_mask = start_button_mask;
2658 site->target_list = gtk_target_list_new (targets, n_targets);
2660 site->actions = actions;
2664 * gtk_drag_source_unset: (method)
2665 * @widget: a #GtkWidget
2667 * Undoes the effects of gtk_drag_source_set().
2670 gtk_drag_source_unset (GtkWidget *widget)
2672 GtkDragSourceSite *site;
2674 g_return_if_fail (GTK_IS_WIDGET (widget));
2676 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2680 g_signal_handlers_disconnect_by_func (widget,
2681 gtk_drag_source_event_cb,
2683 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
2688 * gtk_drag_source_get_target_list: (method)
2689 * @widget: a #GtkWidget
2691 * Gets the list of targets this widget can provide for
2694 * Return value: (transfer none): the #GtkTargetList, or %NULL if none
2699 gtk_drag_source_get_target_list (GtkWidget *widget)
2701 GtkDragSourceSite *site;
2703 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2705 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2707 return site ? site->target_list : NULL;
2711 * gtk_drag_source_set_target_list: (method)
2712 * @widget: a #GtkWidget that's a drag source
2713 * @target_list: (allow-none): list of draggable targets, or %NULL for none
2715 * Changes the target types that this widget offers for drag-and-drop.
2716 * The widget must first be made into a drag source with
2717 * gtk_drag_source_set().
2722 gtk_drag_source_set_target_list (GtkWidget *widget,
2723 GtkTargetList *target_list)
2725 GtkDragSourceSite *site;
2727 g_return_if_fail (GTK_IS_WIDGET (widget));
2729 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2732 g_warning ("gtk_drag_source_set_target_list() requires the widget "
2733 "to already be a drag source.");
2738 gtk_target_list_ref (target_list);
2740 if (site->target_list)
2741 gtk_target_list_unref (site->target_list);
2743 site->target_list = target_list;
2747 * gtk_drag_source_add_text_targets: (method)
2748 * @widget: a #GtkWidget that's is a drag source
2750 * Add the text targets supported by #GtkSelection to
2751 * the target list of the drag source. The targets
2752 * are added with @info = 0. If you need another value,
2753 * use gtk_target_list_add_text_targets() and
2754 * gtk_drag_source_set_target_list().
2759 gtk_drag_source_add_text_targets (GtkWidget *widget)
2761 GtkTargetList *target_list;
2763 target_list = gtk_drag_source_get_target_list (widget);
2765 gtk_target_list_ref (target_list);
2767 target_list = gtk_target_list_new (NULL, 0);
2768 gtk_target_list_add_text_targets (target_list, 0);
2769 gtk_drag_source_set_target_list (widget, target_list);
2770 gtk_target_list_unref (target_list);
2774 * gtk_drag_source_add_image_targets: (method)
2775 * @widget: a #GtkWidget that's is a drag source
2777 * Add the writable image targets supported by #GtkSelection to
2778 * the target list of the drag source. The targets
2779 * are added with @info = 0. If you need another value,
2780 * use gtk_target_list_add_image_targets() and
2781 * gtk_drag_source_set_target_list().
2786 gtk_drag_source_add_image_targets (GtkWidget *widget)
2788 GtkTargetList *target_list;
2790 target_list = gtk_drag_source_get_target_list (widget);
2792 gtk_target_list_ref (target_list);
2794 target_list = gtk_target_list_new (NULL, 0);
2795 gtk_target_list_add_image_targets (target_list, 0, TRUE);
2796 gtk_drag_source_set_target_list (widget, target_list);
2797 gtk_target_list_unref (target_list);
2801 * gtk_drag_source_add_uri_targets: (method)
2802 * @widget: a #GtkWidget that's is a drag source
2804 * Add the URI targets supported by #GtkSelection to
2805 * the target list of the drag source. The targets
2806 * are added with @info = 0. If you need another value,
2807 * use gtk_target_list_add_uri_targets() and
2808 * gtk_drag_source_set_target_list().
2813 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2815 GtkTargetList *target_list;
2817 target_list = gtk_drag_source_get_target_list (widget);
2819 gtk_target_list_ref (target_list);
2821 target_list = gtk_target_list_new (NULL, 0);
2822 gtk_target_list_add_uri_targets (target_list, 0);
2823 gtk_drag_source_set_target_list (widget, target_list);
2824 gtk_target_list_unref (target_list);
2828 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2830 switch (site->icon_type)
2832 case GTK_IMAGE_EMPTY:
2834 case GTK_IMAGE_PIXBUF:
2835 g_object_unref (site->icon_data.pixbuf.pixbuf);
2837 case GTK_IMAGE_STOCK:
2838 g_free (site->icon_data.stock.stock_id);
2840 case GTK_IMAGE_ICON_NAME:
2841 g_free (site->icon_data.name.icon_name);
2843 case GTK_IMAGE_GICON:
2844 _gtk_image_gicon_data_clear (&(site->icon_data.gicon));
2847 g_assert_not_reached();
2850 site->icon_type = GTK_IMAGE_EMPTY;
2854 * gtk_drag_source_set_icon_pixbuf: (method)
2855 * @widget: a #GtkWidget
2856 * @pixbuf: the #GdkPixbuf for the drag icon
2858 * Sets the icon that will be used for drags from a particular widget
2859 * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
2860 * release it when it is no longer needed.
2863 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
2866 GtkDragSourceSite *site;
2868 g_return_if_fail (GTK_IS_WIDGET (widget));
2869 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2871 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2872 g_return_if_fail (site != NULL);
2873 g_object_ref (pixbuf);
2875 gtk_drag_source_unset_icon (site);
2877 site->icon_type = GTK_IMAGE_PIXBUF;
2878 site->icon_data.pixbuf.pixbuf = pixbuf;
2882 * gtk_drag_source_set_icon_stock: (method)
2883 * @widget: a #GtkWidget
2884 * @stock_id: the ID of the stock icon to use
2886 * Sets the icon that will be used for drags from a particular source
2890 gtk_drag_source_set_icon_stock (GtkWidget *widget,
2891 const gchar *stock_id)
2893 GtkDragSourceSite *site;
2895 g_return_if_fail (GTK_IS_WIDGET (widget));
2896 g_return_if_fail (stock_id != NULL);
2898 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2899 g_return_if_fail (site != NULL);
2901 gtk_drag_source_unset_icon (site);
2903 site->icon_type = GTK_IMAGE_STOCK;
2904 site->icon_data.stock.stock_id = g_strdup (stock_id);
2908 * gtk_drag_source_set_icon_name: (method)
2909 * @widget: a #GtkWidget
2910 * @icon_name: name of icon to use
2912 * Sets the icon that will be used for drags from a particular source
2913 * to a themed icon. See the docs for #GtkIconTheme for more details.
2918 gtk_drag_source_set_icon_name (GtkWidget *widget,
2919 const gchar *icon_name)
2921 GtkDragSourceSite *site;
2923 g_return_if_fail (GTK_IS_WIDGET (widget));
2924 g_return_if_fail (icon_name != NULL);
2926 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2927 g_return_if_fail (site != NULL);
2929 gtk_drag_source_unset_icon (site);
2931 site->icon_type = GTK_IMAGE_ICON_NAME;
2932 site->icon_data.name.icon_name = g_strdup (icon_name);
2936 * gtk_drag_source_set_icon_gicon: (method)
2937 * @widget: a #GtkWidget
2940 * Sets the icon that will be used for drags from a particular source
2941 * to @icon. See the docs for #GtkIconTheme for more details.
2946 gtk_drag_source_set_icon_gicon (GtkWidget *widget,
2949 GtkDragSourceSite *site;
2951 g_return_if_fail (GTK_IS_WIDGET (widget));
2952 g_return_if_fail (icon != NULL);
2954 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2955 g_return_if_fail (site != NULL);
2957 gtk_drag_source_unset_icon (site);
2959 site->icon_type = GTK_IMAGE_GICON;
2960 site->icon_data.gicon.icon = g_object_ref (icon);
2964 gtk_drag_get_icon (GtkDragSourceInfo *info,
2965 GtkWidget **icon_window,
2969 if (get_can_change_screen (info->icon_window))
2970 gtk_window_set_screen (GTK_WINDOW (info->icon_window),
2973 if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
2975 if (!info->fallback_icon)
2977 gint save_hot_x, save_hot_y;
2978 gboolean save_destroy_icon;
2979 GtkWidget *save_icon_window;
2981 /* HACK to get the appropriate icon
2983 save_icon_window = info->icon_window;
2984 save_hot_x = info->hot_x;
2985 save_hot_y = info->hot_x;
2986 save_destroy_icon = info->destroy_icon;
2988 info->icon_window = NULL;
2989 set_icon_stock_pixbuf (info->context,
2990 GTK_STOCK_DND, NULL, -2, -2, TRUE);
2991 info->fallback_icon = info->icon_window;
2993 info->icon_window = save_icon_window;
2994 info->hot_x = save_hot_x;
2995 info->hot_y = save_hot_y;
2996 info->destroy_icon = save_destroy_icon;
2999 gtk_widget_hide (info->icon_window);
3001 *icon_window = info->fallback_icon;
3002 gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
3009 if (info->fallback_icon)
3010 gtk_widget_hide (info->fallback_icon);
3012 *icon_window = info->icon_window;
3013 *hot_x = info->hot_x;
3014 *hot_y = info->hot_y;
3019 gtk_drag_update_icon (GtkDragSourceInfo *info)
3021 if (info->icon_window)
3023 GtkWidget *icon_window;
3026 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
3028 gtk_window_move (GTK_WINDOW (icon_window),
3029 info->cur_x - hot_x,
3030 info->cur_y - hot_y);
3032 if (gtk_widget_get_visible (icon_window))
3033 gdk_window_raise (gtk_widget_get_window (icon_window));
3035 gtk_widget_show (icon_window);
3040 gtk_drag_set_icon_window (GdkDragContext *context,
3044 gboolean destroy_on_release)
3046 GtkDragSourceInfo *info;
3048 info = gtk_drag_get_source_info (context, FALSE);
3051 if (destroy_on_release)
3052 gtk_widget_destroy (widget);
3056 gtk_drag_remove_icon (info);
3059 g_object_ref (widget);
3061 info->icon_window = widget;
3062 info->hot_x = hot_x;
3063 info->hot_y = hot_y;
3064 info->destroy_icon = destroy_on_release;
3066 if (widget && info->icon_pixbuf)
3068 g_object_unref (info->icon_pixbuf);
3069 info->icon_pixbuf = NULL;
3072 gtk_drag_update_cursor (info);
3073 gtk_drag_update_icon (info);
3077 * gtk_drag_set_icon_widget: (method)
3078 * @context: the context for a drag. (This must be called
3079 with a context for the source side of a drag)
3080 * @widget: a toplevel window to use as an icon.
3081 * @hot_x: the X offset within @widget of the hotspot.
3082 * @hot_y: the Y offset within @widget of the hotspot.
3084 * Changes the icon for a widget to a given widget. GTK+
3085 * will not destroy the icon, so if you don't want
3086 * it to persist, you should connect to the "drag-end"
3087 * signal and destroy it yourself.
3090 gtk_drag_set_icon_widget (GdkDragContext *context,
3095 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3096 g_return_if_fail (GTK_IS_WIDGET (widget));
3098 gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
3102 icon_window_realize (GtkWidget *window,
3105 cairo_surface_t *surface;
3106 cairo_pattern_t *pattern;
3109 surface = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3110 CAIRO_CONTENT_COLOR,
3111 gdk_pixbuf_get_width (pixbuf),
3112 gdk_pixbuf_get_height (pixbuf));
3114 cr = cairo_create (surface);
3115 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3116 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3118 cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3120 cairo_pop_group_to_source (cr);
3124 pattern = cairo_pattern_create_for_surface (surface);
3125 gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3126 cairo_pattern_destroy (pattern);
3128 cairo_surface_destroy (surface);
3130 if (gdk_pixbuf_get_has_alpha (pixbuf))
3132 cairo_region_t *region;
3134 surface = cairo_image_surface_create (CAIRO_FORMAT_A1,
3135 gdk_pixbuf_get_width (pixbuf),
3136 gdk_pixbuf_get_height (pixbuf));
3138 cr = cairo_create (surface);
3139 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3143 region = gdk_cairo_region_create_from_surface (surface);
3144 gtk_widget_shape_combine_region (window, region);
3145 cairo_region_destroy (region);
3147 cairo_surface_destroy (surface);
3152 set_icon_stock_pixbuf (GdkDragContext *context,
3153 const gchar *stock_id,
3157 gboolean force_window)
3162 GdkDisplay *display;
3164 g_return_if_fail (context != NULL);
3165 g_return_if_fail (pixbuf != NULL || stock_id != NULL);
3166 g_return_if_fail (pixbuf == NULL || stock_id == NULL);
3168 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3170 window = gtk_window_new (GTK_WINDOW_POPUP);
3171 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3172 gtk_window_set_screen (GTK_WINDOW (window), screen);
3173 set_can_change_screen (window, TRUE);
3175 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3176 gtk_widget_set_app_paintable (window, TRUE);
3180 pixbuf = gtk_widget_render_icon_pixbuf (window, stock_id,
3185 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
3186 gtk_widget_destroy (window);
3192 g_object_ref (pixbuf);
3194 display = gdk_window_get_display (gdk_drag_context_get_source_window (context));
3195 width = gdk_pixbuf_get_width (pixbuf);
3196 height = gdk_pixbuf_get_height (pixbuf);
3198 if (!force_window &&
3199 gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
3201 GtkDragSourceInfo *info;
3203 gtk_widget_destroy (window);
3205 info = gtk_drag_get_source_info (context, FALSE);
3207 if (info->icon_pixbuf)
3208 g_object_unref (info->icon_pixbuf);
3209 info->icon_pixbuf = pixbuf;
3211 gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
3215 gtk_widget_set_size_request (window, width, height);
3217 g_signal_connect_closure (window, "realize",
3218 g_cclosure_new (G_CALLBACK (icon_window_realize),
3220 (GClosureNotify)g_object_unref),
3223 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3228 * gtk_drag_set_icon_pixbuf: (method)
3229 * @context: the context for a drag. (This must be called
3230 * with a context for the source side of a drag)
3231 * @pixbuf: the #GdkPixbuf to use as the drag icon.
3232 * @hot_x: the X offset within @widget of the hotspot.
3233 * @hot_y: the Y offset within @widget of the hotspot.
3235 * Sets @pixbuf as the icon for a given drag.
3238 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
3243 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3244 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3246 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3250 * gtk_drag_set_icon_stock: (method)
3251 * @context: the context for a drag. (This must be called
3252 * with a context for the source side of a drag)
3253 * @stock_id: the ID of the stock icon to use for the drag.
3254 * @hot_x: the X offset within the icon of the hotspot.
3255 * @hot_y: the Y offset within the icon of the hotspot.
3257 * Sets the icon for a given drag from a stock ID.
3260 gtk_drag_set_icon_stock (GdkDragContext *context,
3261 const gchar *stock_id,
3265 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3266 g_return_if_fail (stock_id != NULL);
3268 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
3271 /* XXX: This function is in gdk, too. Should it be in Cairo? */
3273 _gtk_cairo_surface_extents (cairo_surface_t *surface,
3274 GdkRectangle *extents)
3276 double x1, x2, y1, y2;
3279 g_return_val_if_fail (surface != NULL, FALSE);
3280 g_return_val_if_fail (extents != NULL, FALSE);
3282 cr = cairo_create (surface);
3283 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
3292 if (x1 < G_MININT || x1 > G_MAXINT ||
3293 y1 < G_MININT || y1 > G_MAXINT ||
3294 x2 > G_MAXINT || y2 > G_MAXINT)
3296 extents->x = extents->y = extents->width = extents->height = 0;
3302 extents->width = x2;
3303 extents->height = y2;
3309 * gtk_drag_set_icon_surface: (method)
3310 * @context: the context for a drag. (This must be called
3311 * with a context for the source side of a drag)
3312 * @surface: the surface to use as icon
3314 * Sets @surface as the icon for a given drag. GTK+ retains
3315 * references for the arguments, and will release them when
3316 * they are no longer needed.
3318 * To position the surface relative to the mouse, use
3319 * cairo_surface_set_device_offset() on @surface. The mouse
3320 * cursor will be positioned at the (0,0) coordinate of the
3324 gtk_drag_set_icon_surface (GdkDragContext *context,
3325 cairo_surface_t *surface)
3329 GdkRectangle extents;
3330 cairo_pattern_t *pattern;
3332 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3333 g_return_if_fail (surface != NULL);
3335 _gtk_cairo_surface_extents (surface, &extents);
3338 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3340 window = gtk_window_new (GTK_WINDOW_POPUP);
3341 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3342 gtk_window_set_screen (GTK_WINDOW (window), screen);
3343 set_can_change_screen (window, TRUE);
3345 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3346 gtk_widget_set_app_paintable (window, TRUE);
3348 gtk_widget_set_size_request (window, extents.width, extents.height);
3349 gtk_widget_realize (window);
3351 if (cairo_surface_get_content (surface) != CAIRO_CONTENT_COLOR)
3353 cairo_surface_t *saturated;
3354 cairo_region_t *region;
3357 region = gdk_cairo_region_create_from_surface (surface);
3358 cairo_region_translate (region, -extents.x, -extents.y);
3360 gtk_widget_shape_combine_region (window, region);
3361 cairo_region_destroy (region);
3363 /* Need to saturate the colors, so it doesn't look like semi-transparent
3364 * pixels were painted on black. */
3365 saturated = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3366 CAIRO_CONTENT_COLOR,
3370 cr = cairo_create (saturated);
3371 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3372 cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
3374 cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3376 cairo_pop_group_to_source (cr);
3380 pattern = cairo_pattern_create_for_surface (saturated);
3382 cairo_surface_destroy (saturated);
3386 cairo_matrix_t matrix;
3388 pattern = cairo_pattern_create_for_surface (surface);
3389 cairo_matrix_init_translate (&matrix, extents.x, extents.y);
3390 cairo_pattern_set_matrix (pattern, &matrix);
3393 gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3395 gtk_drag_set_icon_window (context, window, extents.x, extents.y, TRUE);
3399 * gtk_drag_set_icon_name: (method)
3400 * @context: the context for a drag. (This must be called
3401 * with a context for the source side of a drag)
3402 * @icon_name: name of icon to use
3403 * @hot_x: the X offset of the hotspot within the icon
3404 * @hot_y: the Y offset of the hotspot within the icon
3406 * Sets the icon for a given drag from a named themed icon. See
3407 * the docs for #GtkIconTheme for more details. Note that the
3408 * size of the icon depends on the icon theme (the icon is
3409 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
3410 * @hot_x and @hot_y have to be used with care.
3415 gtk_drag_set_icon_name (GdkDragContext *context,
3416 const gchar *icon_name,
3422 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3423 g_return_if_fail (icon_name != NULL);
3425 icon = g_themed_icon_new (icon_name);
3426 gtk_drag_set_icon_gicon (context, icon, hot_x, hot_y);
3427 g_object_unref (icon);
3431 * gtk_drag_set_icon_gicon: (method)
3432 * @context: the context for a drag. (This must be called
3433 * with a context for the source side of a drag)
3435 * @hot_x: the X offset of the hotspot within the icon
3436 * @hot_y: the Y offset of the hotspot within the icon
3438 * Sets the icon for a given drag from the given @icon. See the
3439 * documentation for gtk_drag_set_icon_name() for more details about
3440 * using icons in drag and drop.
3445 gtk_drag_set_icon_gicon (GdkDragContext *context,
3451 GtkSettings *settings;
3452 GtkIconTheme *icon_theme;
3453 GtkIconInfo *icon_info;
3455 gint width, height, icon_size;
3457 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3458 g_return_if_fail (icon != NULL);
3460 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3461 g_return_if_fail (screen != NULL);
3463 settings = gtk_settings_get_for_screen (screen);
3464 if (gtk_icon_size_lookup_for_settings (settings,
3467 icon_size = MAX (width, height);
3469 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
3471 icon_theme = gtk_icon_theme_get_for_screen (screen);
3473 icon_info = gtk_icon_theme_lookup_by_gicon (icon_theme, icon, icon_size, 0);
3474 if (icon_info != NULL)
3476 pixbuf = gtk_icon_info_load_icon (icon_info, NULL);
3482 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3485 char *str = g_icon_to_string (icon);
3486 g_warning ("Cannot load drag icon from GIcon '%s'", str);
3492 * gtk_drag_set_icon_default: (method)
3493 * @context: the context for a drag. (This must be called
3494 with a context for the source side of a drag)
3496 * Sets the icon for a particular drag to the default
3500 gtk_drag_set_icon_default (GdkDragContext *context)
3502 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3504 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
3507 /*************************************************************
3508 * _gtk_drag_source_handle_event:
3509 * Called from widget event handling code on Drag events
3513 * toplevel: Toplevel widget that received the event
3516 *************************************************************/
3519 _gtk_drag_source_handle_event (GtkWidget *widget,
3522 GtkDragSourceInfo *info;
3523 GdkDragContext *context;
3525 g_return_if_fail (widget != NULL);
3526 g_return_if_fail (event != NULL);
3528 context = event->dnd.context;
3529 info = gtk_drag_get_source_info (context, FALSE);
3533 switch (event->type)
3535 case GDK_DRAG_STATUS:
3539 if (info->proxy_dest)
3541 if (!event->dnd.send_event)
3543 if (info->proxy_dest->proxy_drop_wait)
3545 gboolean result = gdk_drag_context_get_selected_action (context) != 0;
3547 /* Aha - we can finally pass the MOTIF DROP on... */
3548 gdk_drop_reply (info->proxy_dest->context, result, info->proxy_dest->proxy_drop_time);
3550 gdk_drag_drop (info->context, info->proxy_dest->proxy_drop_time);
3552 gtk_drag_finish (info->proxy_dest->context, FALSE, FALSE, info->proxy_dest->proxy_drop_time);
3556 gdk_drag_status (info->proxy_dest->context,
3557 gdk_drag_context_get_selected_action (event->dnd.context),
3562 else if (info->have_grab)
3564 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
3565 gdk_drag_context_get_selected_action (event->dnd.context),
3567 if (info->cursor != cursor)
3571 pointer = gdk_drag_context_get_device (context);
3572 gdk_device_grab (pointer, gtk_widget_get_window (widget),
3573 GDK_OWNERSHIP_APPLICATION, FALSE,
3574 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3575 cursor, info->grab_time);
3576 info->cursor = cursor;
3579 gtk_drag_add_update_idle (info);
3584 case GDK_DROP_FINISHED:
3585 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, event->dnd.time);
3588 g_assert_not_reached ();
3592 /*************************************************************
3593 * gtk_drag_source_check_selection:
3594 * Check if we've set up handlers/claimed the selection
3595 * for a given drag. If not, add them.
3599 *************************************************************/
3602 gtk_drag_source_check_selection (GtkDragSourceInfo *info,
3608 tmp_list = info->selections;
3611 if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3613 tmp_list = tmp_list->next;
3616 gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3620 info->selections = g_list_prepend (info->selections,
3621 GUINT_TO_POINTER (selection));
3623 tmp_list = info->target_list->list;
3626 GtkTargetPair *pair = tmp_list->data;
3628 gtk_selection_add_target (info->ipc_widget,
3632 tmp_list = tmp_list->next;
3635 if (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_MOTIF)
3637 gtk_selection_add_target (info->ipc_widget,
3639 gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS"),
3640 TARGET_MOTIF_SUCCESS);
3641 gtk_selection_add_target (info->ipc_widget,
3643 gdk_atom_intern_static_string ("XmTRANSFER_FAILURE"),
3644 TARGET_MOTIF_FAILURE);
3647 gtk_selection_add_target (info->ipc_widget,
3649 gdk_atom_intern_static_string ("DELETE"),
3653 /*************************************************************
3654 * gtk_drag_drop_finished:
3655 * Clean up from the drag, and display snapback, if necessary.
3661 *************************************************************/
3664 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3665 GtkDragResult result,
3670 success = (result == GTK_DRAG_RESULT_SUCCESS);
3671 gtk_drag_source_release_selections (info, time);
3673 if (info->proxy_dest)
3675 /* The time from the event isn't reliable for Xdnd drags */
3676 gtk_drag_finish (info->proxy_dest->context, success, FALSE,
3677 info->proxy_dest->proxy_drop_time);
3678 gtk_drag_source_info_destroy (info);
3683 g_signal_emit_by_name (info->widget, "drag-failed",
3684 info->context, result, &success);
3688 gtk_drag_source_info_destroy (info);
3692 GtkDragAnim *anim = g_slice_new0 (GtkDragAnim);
3696 anim->n_steps = MAX (info->cur_x - info->start_x,
3697 info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3698 anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3700 info->cur_screen = gtk_widget_get_screen (info->widget);
3702 if (!info->icon_window)
3703 set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf,
3706 gtk_drag_update_icon (info);
3708 /* Mark the context as dead, so if the destination decides
3709 * to respond really late, we still are OK.
3711 gtk_drag_clear_source_info (info->context);
3712 gdk_threads_add_timeout (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3718 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3721 GdkDisplay *display = gtk_widget_get_display (info->widget);
3722 GList *tmp_list = info->selections;
3726 GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3727 if (gdk_selection_owner_get_for_display (display, selection) == gtk_widget_get_window (info->ipc_widget))
3728 gtk_selection_owner_set_for_display (display, NULL, selection, time);
3730 tmp_list = tmp_list->next;
3733 g_list_free (info->selections);
3734 info->selections = NULL;
3737 /*************************************************************
3739 * Send a drop event.
3743 *************************************************************/
3746 gtk_drag_drop (GtkDragSourceInfo *info,
3749 if (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_ROOTWIN)
3751 GtkSelectionData selection_data;
3753 /* GTK+ traditionally has used application/x-rootwin-drop, but the
3754 * XDND spec specifies x-rootwindow-drop.
3756 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3757 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3759 tmp_list = info->target_list->list;
3762 GtkTargetPair *pair = tmp_list->data;
3764 if (pair->target == target1 || pair->target == target2)
3766 selection_data.selection = GDK_NONE;
3767 selection_data.target = pair->target;
3768 selection_data.data = NULL;
3769 selection_data.length = -1;
3771 g_signal_emit_by_name (info->widget, "drag-data-get",
3772 info->context, &selection_data,
3776 /* FIXME: Should we check for length >= 0 here? */
3777 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3780 tmp_list = tmp_list->next;
3782 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3786 if (info->icon_window)
3787 gtk_widget_hide (info->icon_window);
3789 gdk_drag_drop (info->context, time);
3790 info->drop_timeout = gdk_threads_add_timeout (DROP_ABORT_TIME,
3791 gtk_drag_abort_timeout,
3797 * Source side callbacks.
3801 gtk_drag_source_event_cb (GtkWidget *widget,
3805 GtkDragSourceSite *site;
3806 gboolean retval = FALSE;
3807 site = (GtkDragSourceSite *)data;
3809 switch (event->type)
3811 case GDK_BUTTON_PRESS:
3812 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3814 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3815 site->x = event->button.x;
3816 site->y = event->button.y;
3820 case GDK_BUTTON_RELEASE:
3821 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3822 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3825 case GDK_MOTION_NOTIFY:
3826 if (site->state & event->motion.state & site->start_button_mask)
3828 /* FIXME: This is really broken and can leave us
3834 if (site->state & event->motion.state &
3835 GDK_BUTTON1_MASK << (i - 1))
3839 if (gtk_drag_check_threshold (widget, site->x, site->y,
3840 event->motion.x, event->motion.y))
3843 gtk_drag_begin_internal (widget, site, site->target_list,
3852 default: /* hit for 2/3BUTTON_PRESS */
3860 gtk_drag_source_site_destroy (gpointer data)
3862 GtkDragSourceSite *site = data;
3864 if (site->target_list)
3865 gtk_target_list_unref (site->target_list);
3867 gtk_drag_source_unset_icon (site);
3868 g_slice_free (GtkDragSourceSite, site);
3872 gtk_drag_selection_get (GtkWidget *widget,
3873 GtkSelectionData *selection_data,
3878 GtkDragSourceInfo *info = data;
3879 static GdkAtom null_atom = GDK_NONE;
3883 null_atom = gdk_atom_intern_static_string ("NULL");
3888 g_signal_emit_by_name (info->widget,
3891 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3893 case TARGET_MOTIF_SUCCESS:
3894 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3895 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3897 case TARGET_MOTIF_FAILURE:
3898 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3899 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3902 if (info->proxy_dest)
3904 /* This is sort of dangerous and needs to be thought
3907 info->proxy_dest->proxy_data = selection_data;
3908 gtk_drag_get_data (info->widget,
3909 info->proxy_dest->context,
3910 gtk_selection_data_get_target (selection_data),
3913 info->proxy_dest->proxy_data = NULL;
3917 if (gtk_target_list_find (info->target_list,
3918 gtk_selection_data_get_target (selection_data),
3921 g_signal_emit_by_name (info->widget, "drag-data-get",
3933 gtk_drag_anim_timeout (gpointer data)
3936 GtkDragSourceInfo *info;
3943 if (anim->step == anim->n_steps)
3945 gtk_drag_source_info_destroy (anim->info);
3946 g_slice_free (GtkDragAnim, anim);
3952 x = (info->start_x * (anim->step + 1) +
3953 info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3954 y = (info->start_y * (anim->step + 1) +
3955 info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3956 if (info->icon_window)
3958 GtkWidget *icon_window;
3961 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
3962 gtk_window_move (GTK_WINDOW (icon_window),
3976 gtk_drag_remove_icon (GtkDragSourceInfo *info)
3978 if (info->icon_window)
3980 gtk_widget_hide (info->icon_window);
3981 if (info->destroy_icon)
3982 gtk_widget_destroy (info->icon_window);
3984 if (info->fallback_icon)
3986 gtk_widget_destroy (info->fallback_icon);
3987 info->fallback_icon = NULL;
3990 g_object_unref (info->icon_window);
3991 info->icon_window = NULL;
3996 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
4000 for (i = 0; i < G_N_ELEMENTS (drag_cursors); i++)
4002 if (info->drag_cursors[i] != NULL)
4004 g_object_unref (info->drag_cursors[i]);
4005 info->drag_cursors[i] = NULL;
4009 gtk_drag_remove_icon (info);
4011 if (info->icon_pixbuf)
4013 g_object_unref (info->icon_pixbuf);
4014 info->icon_pixbuf = NULL;
4017 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4018 gtk_drag_grab_broken_event_cb,
4020 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4021 gtk_drag_grab_notify_cb,
4023 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4024 gtk_drag_button_release_cb,
4026 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4029 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4032 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4033 gtk_drag_selection_get,
4036 if (!info->proxy_dest)
4037 g_signal_emit_by_name (info->widget, "drag-end", info->context);
4040 g_object_unref (info->widget);
4042 gtk_selection_remove_all (info->ipc_widget);
4043 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), NULL);
4044 source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
4045 gtk_drag_release_ipc_widget (info->ipc_widget);
4047 gtk_target_list_unref (info->target_list);
4049 gtk_drag_clear_source_info (info->context);
4050 g_object_unref (info->context);
4052 if (info->drop_timeout)
4053 g_source_remove (info->drop_timeout);
4055 if (info->update_idle)
4056 g_source_remove (info->update_idle);
4062 gtk_drag_update_idle (gpointer data)
4064 GtkDragSourceInfo *info = data;
4065 GdkWindow *dest_window;
4066 GdkDragProtocol protocol;
4069 GdkDragAction action;
4070 GdkDragAction possible_actions;
4073 info->update_idle = 0;
4075 if (info->last_event)
4077 time = gtk_drag_get_event_time (info->last_event);
4078 gtk_drag_get_event_actions (info->last_event,
4080 info->possible_actions,
4081 &action, &possible_actions);
4082 gtk_drag_update_icon (info);
4083 gdk_drag_find_window_for_screen (info->context,
4084 info->icon_window ? gtk_widget_get_window (info->icon_window) : NULL,
4085 info->cur_screen, info->cur_x, info->cur_y,
4086 &dest_window, &protocol);
4088 if (!gdk_drag_motion (info->context, dest_window, protocol,
4089 info->cur_x, info->cur_y, action,
4093 gdk_event_free ((GdkEvent *)info->last_event);
4094 info->last_event = NULL;
4098 g_object_unref (dest_window);
4100 selection = gdk_drag_get_selection (info->context);
4102 gtk_drag_source_check_selection (info, selection, time);
4110 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
4112 /* We use an idle lower than GDK_PRIORITY_REDRAW so that exposes
4113 * from the last move can catch up before we move again.
4115 if (!info->update_idle)
4116 info->update_idle = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW + 5,
4117 gtk_drag_update_idle,
4124 * @info: DragSourceInfo for the drag
4125 * @screen: new screen
4126 * @x_root: new X position
4127 * @y_root: new y position
4128 * @event: event received requiring update
4130 * Updates the status of the drag; called when the
4131 * cursor moves or the modifier changes
4134 gtk_drag_update (GtkDragSourceInfo *info,
4140 info->cur_screen = screen;
4141 info->cur_x = x_root;
4142 info->cur_y = y_root;
4143 if (info->last_event)
4145 gdk_event_free ((GdkEvent *)info->last_event);
4146 info->last_event = NULL;
4149 info->last_event = gdk_event_copy ((GdkEvent *)event);
4151 gtk_drag_add_update_idle (info);
4154 /*************************************************************
4156 * Called when the user finishes to drag, either by
4157 * releasing the mouse, or by pressing Esc.
4159 * info: Source info for the drag
4160 * time: Timestamp for ending the drag
4162 *************************************************************/
4165 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
4167 GtkWidget *source_widget = info->widget;
4168 GdkDevice *pointer, *keyboard;
4170 pointer = gdk_drag_context_get_device (info->context);
4171 keyboard = gdk_device_get_associated_device (pointer);
4173 /* Prevent ungrab before grab (see bug 623865) */
4174 if (info->grab_time == GDK_CURRENT_TIME)
4175 time = GDK_CURRENT_TIME;
4177 if (info->update_idle)
4179 g_source_remove (info->update_idle);
4180 info->update_idle = 0;
4183 if (info->last_event)
4185 gdk_event_free (info->last_event);
4186 info->last_event = NULL;
4189 info->have_grab = FALSE;
4191 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4192 gtk_drag_grab_broken_event_cb,
4194 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4195 gtk_drag_grab_notify_cb,
4197 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4198 gtk_drag_button_release_cb,
4200 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4203 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4207 gdk_device_ungrab (pointer, time);
4208 ungrab_dnd_keys (info->ipc_widget, keyboard, time);
4209 gtk_device_grab_remove (info->ipc_widget, pointer);
4211 if (gtk_widget_get_realized (source_widget))
4213 GdkEvent *send_event;
4215 /* Send on a release pair to the original widget to convince it
4216 * to release its grab. We need to call gtk_propagate_event()
4217 * here, instead of gtk_widget_event() because widget like
4218 * GtkList may expect propagation.
4221 send_event = gdk_event_new (GDK_BUTTON_RELEASE);
4222 send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
4223 send_event->button.send_event = TRUE;
4224 send_event->button.time = time;
4225 send_event->button.x = 0;
4226 send_event->button.y = 0;
4227 send_event->button.axes = NULL;
4228 send_event->button.state = 0;
4229 send_event->button.button = info->button;
4230 send_event->button.device = pointer;
4231 send_event->button.x_root = 0;
4232 send_event->button.y_root = 0;
4234 gtk_propagate_event (source_widget, send_event);
4235 gdk_event_free (send_event);
4239 /*************************************************************
4241 * Called on cancellation of a drag, either by the user
4242 * or programmatically.
4244 * info: Source info for the drag
4245 * time: Timestamp for ending the drag
4247 *************************************************************/
4250 gtk_drag_cancel (GtkDragSourceInfo *info, GtkDragResult result, guint32 time)
4252 gtk_drag_end (info, time);
4253 gdk_drag_abort (info->context, time);
4254 gtk_drag_drop_finished (info, result, time);
4257 /*************************************************************
4258 * gtk_drag_motion_cb:
4259 * "motion-notify-event" callback during drag.
4263 *************************************************************/
4266 gtk_drag_motion_cb (GtkWidget *widget,
4267 GdkEventMotion *event,
4270 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4272 gint x_root, y_root;
4276 gdk_device_get_position (event->device, &screen, &x_root, &y_root);
4277 event->x_root = x_root;
4278 event->y_root = y_root;
4281 screen = gdk_event_get_screen ((GdkEvent *)event);
4283 x_root = (gint)(event->x_root + 0.5);
4284 y_root = (gint)(event->y_root + 0.5);
4285 gtk_drag_update (info, screen, x_root, y_root, (GdkEvent *) event);
4290 /*************************************************************
4292 * "key-press/release-event" callback during drag.
4296 *************************************************************/
4299 #define SMALL_STEP 1
4302 gtk_drag_key_cb (GtkWidget *widget,
4306 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4307 GdkModifierType state;
4308 GdkWindow *root_window;
4313 state = event->state & gtk_accelerator_get_default_mod_mask ();
4314 pointer = gdk_device_get_associated_device (gdk_event_get_device ((GdkEvent *) event));
4316 if (event->type == GDK_KEY_PRESS)
4318 switch (event->keyval)
4320 case GDK_KEY_Escape:
4321 gtk_drag_cancel (info, GTK_DRAG_RESULT_USER_CANCELLED, event->time);
4325 case GDK_KEY_Return:
4326 case GDK_KEY_ISO_Enter:
4327 case GDK_KEY_KP_Enter:
4328 case GDK_KEY_KP_Space:
4329 if ((gdk_drag_context_get_selected_action (info->context) != 0) &&
4330 (gdk_drag_context_get_dest_window (info->context) != NULL))
4332 gtk_drag_end (info, event->time);
4333 gtk_drag_drop (info, event->time);
4337 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4344 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4348 case GDK_KEY_KP_Down:
4349 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4353 case GDK_KEY_KP_Left:
4354 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4358 case GDK_KEY_KP_Right:
4359 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4364 /* Now send a "motion" so that the modifier state is updated */
4366 /* The state is not yet updated in the event, so we need
4367 * to query it here. We could use XGetModifierMapping, but
4368 * that would be overkill.
4370 root_window = gtk_widget_get_root_window (widget);
4371 gdk_window_get_device_position (root_window, pointer, NULL, NULL, &state);
4372 event->state = state;
4374 if (dx != 0 || dy != 0)
4378 gdk_device_warp (pointer,
4379 gtk_widget_get_screen (widget),
4380 info->cur_x, info->cur_y);
4383 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
4389 gtk_drag_grab_broken_event_cb (GtkWidget *widget,
4390 GdkEventGrabBroken *event,
4393 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4395 /* Don't cancel if we break the implicit grab from the initial button_press.
4396 * Also, don't cancel if we re-grab on the widget or on our IPC window, for
4397 * example, when changing the drag cursor.
4400 || event->grab_window == gtk_widget_get_window (info->widget)
4401 || event->grab_window == gtk_widget_get_window (info->ipc_widget))
4404 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4409 gtk_drag_grab_notify_cb (GtkWidget *widget,
4410 gboolean was_grabbed,
4413 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4416 pointer = gdk_drag_context_get_device (info->context);
4418 if (gtk_widget_device_is_shadowed (widget, pointer))
4420 /* We have to block callbacks to avoid recursion here, because
4421 gtk_drag_cancel calls gtk_grab_remove (via gtk_drag_end) */
4422 g_signal_handlers_block_by_func (widget, gtk_drag_grab_notify_cb, data);
4423 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4424 g_signal_handlers_unblock_by_func (widget, gtk_drag_grab_notify_cb, data);
4429 /*************************************************************
4430 * gtk_drag_button_release_cb:
4431 * "button-release-event" callback during drag.
4435 *************************************************************/
4438 gtk_drag_button_release_cb (GtkWidget *widget,
4439 GdkEventButton *event,
4442 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4444 if (event->button != info->button)
4447 if ((gdk_drag_context_get_selected_action (info->context) != 0) &&
4448 (gdk_drag_context_get_dest_window (info->context) != NULL))
4450 gtk_drag_end (info, event->time);
4451 gtk_drag_drop (info, event->time);
4455 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4462 gtk_drag_abort_timeout (gpointer data)
4464 GtkDragSourceInfo *info = data;
4465 guint32 time = GDK_CURRENT_TIME;
4467 if (info->proxy_dest)
4468 time = info->proxy_dest->proxy_drop_time;
4470 info->drop_timeout = 0;
4471 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_TIMEOUT_EXPIRED, time);
4477 * gtk_drag_check_threshold: (method)
4478 * @widget: a #GtkWidget
4479 * @start_x: X coordinate of start of drag
4480 * @start_y: Y coordinate of start of drag
4481 * @current_x: current X coordinate
4482 * @current_y: current Y coordinate
4484 * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
4485 * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
4486 * should trigger the beginning of a drag-and-drop operation.
4488 * Return Value: %TRUE if the drag threshold has been passed.
4491 gtk_drag_check_threshold (GtkWidget *widget,
4497 gint drag_threshold;
4499 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4501 g_object_get (gtk_widget_get_settings (widget),
4502 "gtk-dnd-drag-threshold", &drag_threshold,
4505 return (ABS (current_x - start_x) > drag_threshold ||
4506 ABS (current_y - start_y) > drag_threshold);