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/.
33 #include "gdkconfig.h"
35 #include "gdk/gdkkeysyms.h"
37 #ifdef GDK_WINDOWING_X11
39 #include <X11/keysym.h>
40 #include "gdk/x11/gdkx.h"
44 #include "gtkiconfactory.h"
45 #include "gtkicontheme.h"
47 #include "gtkinvisible.h"
51 #include "gtktooltip.h"
52 #include "gtkwindow.h"
54 #include "gtkdndcursors.h"
56 static GSList *source_widgets = NULL;
58 typedef struct _GtkDragSourceSite GtkDragSourceSite;
59 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
60 typedef struct _GtkDragDestSite GtkDragDestSite;
61 typedef struct _GtkDragDestInfo GtkDragDestInfo;
62 typedef struct _GtkDragAnim GtkDragAnim;
72 struct _GtkDragSourceSite
74 GdkModifierType start_button_mask;
75 GtkTargetList *target_list; /* Targets for drag data */
76 GdkDragAction actions; /* Possible actions */
79 GtkImageType icon_type;
82 GtkImagePixbufData pixbuf;
83 GtkImageStockData stock;
84 GtkImageIconNameData name;
87 /* Stored button press information to detect drag beginning */
92 struct _GtkDragSourceInfo
95 GtkTargetList *target_list; /* Targets for drag data */
96 GdkDragAction possible_actions; /* Actions allowed by source */
97 GdkDragContext *context; /* drag context */
98 GtkWidget *icon_window; /* Window for drag */
99 GtkWidget *fallback_icon; /* Window for drag used on other screens */
100 GtkWidget *ipc_widget; /* GtkInvisible for grab, message passing */
101 GdkCursor *cursor; /* Cursor for drag */
102 gint hot_x, hot_y; /* Hot spot for drag */
103 gint button; /* mouse button starting drag */
105 GtkDragStatus status; /* drag status */
106 GdkEvent *last_event; /* pending event */
108 gint start_x, start_y; /* Initial position */
109 gint cur_x, cur_y; /* Current Position */
110 GdkScreen *cur_screen; /* Current screen for pointer */
112 guint32 grab_time; /* timestamp for initial grab */
113 GList *selections; /* selections we've claimed */
115 GtkDragDestInfo *proxy_dest; /* Set if this is a proxy drag */
117 guint update_idle; /* Idle function to update the drag */
118 guint drop_timeout; /* Timeout for aborting drop */
119 guint destroy_icon : 1; /* If true, destroy icon_window */
120 guint have_grab : 1; /* Do we still have the pointer grab */
121 GdkPixbuf *icon_pixbuf;
122 GdkCursor *drag_cursors[6];
125 struct _GtkDragDestSite
127 GtkDestDefaults flags;
128 GtkTargetList *target_list;
129 GdkDragAction actions;
130 GdkWindow *proxy_window;
131 GdkDragProtocol proxy_protocol;
133 guint proxy_coords : 1;
135 guint track_motion : 1;
138 struct _GtkDragDestInfo
140 GtkWidget *widget; /* Widget in which drag is in */
141 GdkDragContext *context; /* Drag context */
142 GtkDragSourceInfo *proxy_source; /* Set if this is a proxy drag */
143 GtkSelectionData *proxy_data; /* Set while retrieving proxied data */
144 guint32 proxy_drop_time; /* Timestamp for proxied drop */
145 guint proxy_drop_wait : 1; /* Set if we are waiting for a
146 * status reply before sending
149 guint dropped : 1; /* Set after we receive a drop */
150 gint drop_x, drop_y; /* Position of drop */
153 #define DROP_ABORT_TIME 300000
155 #define ANIM_STEP_TIME 50
156 #define ANIM_STEP_LENGTH 50
157 #define ANIM_MIN_STEPS 5
158 #define ANIM_MAX_STEPS 10
162 GtkDragSourceInfo *info;
167 typedef gboolean (* GtkDragDestCallback) (GtkWidget *widget,
168 GdkDragContext *context,
173 /* Enumeration for some targets we handle internally */
176 TARGET_MOTIF_SUCCESS = 0x40000000,
177 TARGET_MOTIF_FAILURE,
181 /* Forward declarations */
182 static void gtk_drag_get_event_actions (GdkEvent *event,
184 GdkDragAction actions,
185 GdkDragAction *suggested_action,
186 GdkDragAction *possible_actions);
187 static GdkCursor * gtk_drag_get_cursor (GdkDisplay *display,
188 GdkDragAction action,
189 GtkDragSourceInfo *info);
190 static void gtk_drag_update_cursor (GtkDragSourceInfo *info);
191 static GtkWidget *gtk_drag_get_ipc_widget (GtkWidget *widget);
192 static GtkWidget *gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen);
193 static void gtk_drag_release_ipc_widget (GtkWidget *widget);
195 static void gtk_drag_selection_received (GtkWidget *widget,
196 GtkSelectionData *selection_data,
199 static gboolean gtk_drag_find_widget (GtkWidget *widget,
200 GdkDragContext *context,
201 GtkDragDestInfo *info,
205 GtkDragDestCallback callback);
206 static void gtk_drag_proxy_begin (GtkWidget *widget,
207 GtkDragDestInfo *dest_info,
209 static void gtk_drag_dest_realized (GtkWidget *widget);
210 static void gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
211 GtkWidget *previous_toplevel);
212 static void gtk_drag_dest_site_destroy (gpointer data);
213 static void gtk_drag_dest_leave (GtkWidget *widget,
214 GdkDragContext *context,
216 static gboolean gtk_drag_dest_motion (GtkWidget *widget,
217 GdkDragContext *context,
221 static gboolean gtk_drag_dest_drop (GtkWidget *widget,
222 GdkDragContext *context,
227 static GtkDragDestInfo * gtk_drag_get_dest_info (GdkDragContext *context,
229 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
231 static void gtk_drag_clear_source_info (GdkDragContext *context);
233 static void gtk_drag_source_check_selection (GtkDragSourceInfo *info,
236 static void gtk_drag_source_release_selections (GtkDragSourceInfo *info,
238 static void gtk_drag_drop (GtkDragSourceInfo *info,
240 static void gtk_drag_drop_finished (GtkDragSourceInfo *info,
241 GtkDragResult result,
243 static void gtk_drag_cancel (GtkDragSourceInfo *info,
244 GtkDragResult result,
247 static gboolean gtk_drag_source_event_cb (GtkWidget *widget,
250 static void gtk_drag_source_site_destroy (gpointer data);
251 static void gtk_drag_selection_get (GtkWidget *widget,
252 GtkSelectionData *selection_data,
256 static gboolean gtk_drag_anim_timeout (gpointer data);
257 static void gtk_drag_remove_icon (GtkDragSourceInfo *info);
258 static void gtk_drag_source_info_destroy (GtkDragSourceInfo *info);
259 static void gtk_drag_add_update_idle (GtkDragSourceInfo *info);
261 static void gtk_drag_update (GtkDragSourceInfo *info,
266 static gboolean gtk_drag_motion_cb (GtkWidget *widget,
267 GdkEventMotion *event,
269 static gboolean gtk_drag_key_cb (GtkWidget *widget,
272 static gboolean gtk_drag_grab_broken_event_cb (GtkWidget *widget,
273 GdkEventGrabBroken *event,
275 static void gtk_drag_grab_notify_cb (GtkWidget *widget,
276 gboolean was_grabbed,
278 static gboolean gtk_drag_button_release_cb (GtkWidget *widget,
279 GdkEventButton *event,
281 static gboolean gtk_drag_abort_timeout (gpointer data);
283 static void set_icon_stock_pixbuf (GdkDragContext *context,
284 const gchar *stock_id,
288 gboolean force_window);
290 /************************
291 * Cursor and Icon data *
292 ************************/
295 GdkDragAction action;
301 { GDK_ACTION_DEFAULT, NULL },
302 { GDK_ACTION_ASK, "dnd-ask", dnd_cursor_ask, NULL, NULL },
303 { GDK_ACTION_COPY, "dnd-copy", dnd_cursor_copy, NULL, NULL },
304 { GDK_ACTION_MOVE, "dnd-move", dnd_cursor_move, NULL, NULL },
305 { GDK_ACTION_LINK, "dnd-link", dnd_cursor_link, NULL, NULL },
306 { 0 , "dnd-none", dnd_cursor_none, NULL, NULL },
309 static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
311 /*********************
312 * Utility functions *
313 *********************/
316 set_can_change_screen (GtkWidget *widget,
317 gboolean can_change_screen)
319 can_change_screen = can_change_screen != FALSE;
321 g_object_set_data (G_OBJECT (widget), I_("gtk-dnd-can-change-screen"),
322 GUINT_TO_POINTER (can_change_screen));
326 get_can_change_screen (GtkWidget *widget)
328 return g_object_get_data (G_OBJECT (widget), "gtk-dnd-can-change-screen") != NULL;
333 gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen)
336 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
337 "gtk-dnd-ipc-widgets");
341 GSList *tmp = drag_widgets;
342 result = drag_widgets->data;
343 drag_widgets = drag_widgets->next;
344 g_object_set_data (G_OBJECT (screen),
345 I_("gtk-dnd-ipc-widgets"),
347 g_slist_free_1 (tmp);
351 result = gtk_window_new (GTK_WINDOW_POPUP);
352 gtk_window_set_screen (GTK_WINDOW (result), screen);
353 gtk_window_resize (GTK_WINDOW (result), 1, 1);
354 gtk_window_move (GTK_WINDOW (result), -100, -100);
355 gtk_widget_show (result);
362 gtk_drag_get_ipc_widget (GtkWidget *widget)
367 result = gtk_drag_get_ipc_widget_for_screen (gtk_widget_get_screen (widget));
369 toplevel = gtk_widget_get_toplevel (widget);
371 if (GTK_IS_WINDOW (toplevel))
373 if (gtk_window_has_group (GTK_WINDOW (toplevel)))
374 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
375 GTK_WINDOW (result));
381 /* FIXME: modifying the XEvent window as in root_key_filter() isn't
382 * going to work with XGE/XI2, since the actual event to handle would
383 * be allocated/freed before GDK gets to translate the event.
384 * Active grabs on the keyboard are used instead at the moment...
386 #if defined (GDK_WINDOWING_X11) && !defined (XINPUT_2)
389 * We want to handle a handful of keys during DND, e.g. Escape to abort.
390 * Grabbing the keyboard has the unfortunate side-effect of preventing
391 * useful things such as using Alt-Tab to cycle between windows or
392 * switching workspaces. Therefore, we just grab the few keys we are
393 * interested in. Note that we need to put the grabs on the root window
394 * in order for them to still work when the focus is moved to another
397 * GDK needs a little help to successfully deliver root key events...
400 static GdkFilterReturn
401 root_key_filter (GdkXEvent *xevent,
405 XEvent *ev = (XEvent *) xevent;
407 if ((ev->type == KeyPress || ev->type == KeyRelease) &&
408 ev->xkey.root == ev->xkey.window)
409 ev->xkey.window = (Window)data;
411 return GDK_FILTER_CONTINUE;
419 static GrabKey grab_keys[] = {
428 { XK_Down, Mod1Mask },
430 { XK_Left, Mod1Mask },
432 { XK_Right, Mod1Mask },
434 { XK_KP_Up, Mod1Mask },
436 { XK_KP_Down, Mod1Mask },
438 { XK_KP_Left, Mod1Mask },
440 { XK_KP_Right, Mod1Mask }
444 grab_dnd_keys (GtkWidget *widget,
449 GdkWindow *window, *root;
452 window = gtk_widget_get_window (widget);
453 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
455 gdk_error_trap_push ();
457 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
459 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
460 XGrabKey (GDK_WINDOW_XDISPLAY (window),
461 keycode, grab_keys[i].modifiers,
462 GDK_WINDOW_XID (root),
469 gdk_error_trap_pop ();
471 gdk_window_add_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
475 ungrab_dnd_keys (GtkWidget *widget,
480 GdkWindow *window, *root;
483 window = gtk_widget_get_window (widget);
484 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
486 gdk_window_remove_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
488 gdk_error_trap_push ();
490 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
492 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
493 XUngrabKey (GDK_WINDOW_XDISPLAY (window),
494 keycode, grab_keys[i].modifiers,
495 GDK_WINDOW_XID (root));
499 gdk_error_trap_pop ();
502 #else /* GDK_WINDOWING_X11 && !XINPUT_2 */
505 grab_dnd_keys (GtkWidget *widget,
509 gdk_device_grab (device,
510 gtk_widget_get_window (widget),
511 GDK_OWNERSHIP_APPLICATION, FALSE,
512 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
517 ungrab_dnd_keys (GtkWidget *widget,
521 gdk_device_ungrab (device, time);
524 #endif /* GDK_WINDOWING_X11 */
527 /***************************************************************
528 * gtk_drag_release_ipc_widget:
529 * Releases widget retrieved with gtk_drag_get_ipc_widget ()
531 * widget: the widget to release.
533 ***************************************************************/
536 gtk_drag_release_ipc_widget (GtkWidget *widget)
538 GtkWindow *window = GTK_WINDOW (widget);
539 GdkScreen *screen = gtk_widget_get_screen (widget);
540 GdkDragContext *context = g_object_get_data (G_OBJECT (widget), "drag-context");
541 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
542 "gtk-dnd-ipc-widgets");
543 GdkDevice *pointer, *keyboard;
547 pointer = gdk_drag_context_get_device (context);
548 keyboard = gdk_device_get_associated_device (pointer);
551 ungrab_dnd_keys (widget, keyboard, GDK_CURRENT_TIME);
554 if (gtk_window_has_group (window))
555 gtk_window_group_remove_window (gtk_window_get_group (window),
557 drag_widgets = g_slist_prepend (drag_widgets, widget);
558 g_object_set_data (G_OBJECT (screen),
559 I_("gtk-dnd-ipc-widgets"),
564 gtk_drag_get_event_time (GdkEvent *event)
566 guint32 tm = GDK_CURRENT_TIME;
571 case GDK_MOTION_NOTIFY:
572 tm = event->motion.time; break;
573 case GDK_BUTTON_PRESS:
574 case GDK_2BUTTON_PRESS:
575 case GDK_3BUTTON_PRESS:
576 case GDK_BUTTON_RELEASE:
577 tm = event->button.time; break;
579 case GDK_KEY_RELEASE:
580 tm = event->key.time; break;
581 case GDK_ENTER_NOTIFY:
582 case GDK_LEAVE_NOTIFY:
583 tm = event->crossing.time; break;
584 case GDK_PROPERTY_NOTIFY:
585 tm = event->property.time; break;
586 case GDK_SELECTION_CLEAR:
587 case GDK_SELECTION_REQUEST:
588 case GDK_SELECTION_NOTIFY:
589 tm = event->selection.time; break;
590 case GDK_PROXIMITY_IN:
591 case GDK_PROXIMITY_OUT:
592 tm = event->proximity.time; break;
593 default: /* use current time */
601 gtk_drag_get_event_actions (GdkEvent *event,
603 GdkDragAction actions,
604 GdkDragAction *suggested_action,
605 GdkDragAction *possible_actions)
607 *suggested_action = 0;
608 *possible_actions = 0;
612 GdkModifierType state = 0;
616 case GDK_MOTION_NOTIFY:
617 state = event->motion.state;
619 case GDK_BUTTON_PRESS:
620 case GDK_2BUTTON_PRESS:
621 case GDK_3BUTTON_PRESS:
622 case GDK_BUTTON_RELEASE:
623 state = event->button.state;
626 case GDK_KEY_RELEASE:
627 state = event->key.state;
629 case GDK_ENTER_NOTIFY:
630 case GDK_LEAVE_NOTIFY:
631 state = event->crossing.state;
637 if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
639 *suggested_action = GDK_ACTION_ASK;
640 *possible_actions = actions;
642 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
644 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
646 if (actions & GDK_ACTION_LINK)
648 *suggested_action = GDK_ACTION_LINK;
649 *possible_actions = GDK_ACTION_LINK;
652 else if (state & GDK_CONTROL_MASK)
654 if (actions & GDK_ACTION_COPY)
656 *suggested_action = GDK_ACTION_COPY;
657 *possible_actions = GDK_ACTION_COPY;
663 if (actions & GDK_ACTION_MOVE)
665 *suggested_action = GDK_ACTION_MOVE;
666 *possible_actions = GDK_ACTION_MOVE;
673 *possible_actions = actions;
675 if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
676 *suggested_action = GDK_ACTION_ASK;
677 else if (actions & GDK_ACTION_COPY)
678 *suggested_action = GDK_ACTION_COPY;
679 else if (actions & GDK_ACTION_MOVE)
680 *suggested_action = GDK_ACTION_MOVE;
681 else if (actions & GDK_ACTION_LINK)
682 *suggested_action = GDK_ACTION_LINK;
687 *possible_actions = actions;
689 if (actions & GDK_ACTION_COPY)
690 *suggested_action = GDK_ACTION_COPY;
691 else if (actions & GDK_ACTION_MOVE)
692 *suggested_action = GDK_ACTION_MOVE;
693 else if (actions & GDK_ACTION_LINK)
694 *suggested_action = GDK_ACTION_LINK;
699 gtk_drag_can_use_rgba_cursor (GdkDisplay *display,
703 guint max_width, max_height;
705 if (!gdk_display_supports_cursor_color (display))
708 if (!gdk_display_supports_cursor_alpha (display))
711 gdk_display_get_maximal_cursor_size (display,
714 if (width > max_width || height > max_height)
716 /* can't use rgba cursor (too large) */
724 gtk_drag_get_cursor (GdkDisplay *display,
725 GdkDragAction action,
726 GtkDragSourceInfo *info)
730 /* reconstruct the cursors for each new drag (thus !info),
731 * to catch cursor theme changes
735 for (i = 0 ; i < n_drag_cursors - 1; i++)
736 if (drag_cursors[i].cursor != NULL)
738 gdk_cursor_unref (drag_cursors[i].cursor);
739 drag_cursors[i].cursor = NULL;
743 for (i = 0 ; i < n_drag_cursors - 1; i++)
744 if (drag_cursors[i].action == action)
747 if (drag_cursors[i].pixbuf == NULL)
748 drag_cursors[i].pixbuf =
749 gdk_pixbuf_new_from_inline (-1, drag_cursors[i].data, FALSE, NULL);
751 if (drag_cursors[i].cursor != NULL)
753 if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
755 gdk_cursor_unref (drag_cursors[i].cursor);
756 drag_cursors[i].cursor = NULL;
760 if (drag_cursors[i].cursor == NULL)
761 drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
763 if (drag_cursors[i].cursor == NULL)
764 drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);
766 if (info && info->icon_pixbuf)
768 gint cursor_width, cursor_height;
769 gint icon_width, icon_height;
771 GdkPixbuf *cursor_pixbuf, *pixbuf;
773 gint icon_x, icon_y, ref_x, ref_y;
775 if (info->drag_cursors[i] != NULL)
777 if (display == gdk_cursor_get_display (info->drag_cursors[i]))
778 return info->drag_cursors[i];
780 gdk_cursor_unref (info->drag_cursors[i]);
781 info->drag_cursors[i] = NULL;
784 icon_x = info->hot_x;
785 icon_y = info->hot_y;
786 icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
787 icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);
790 cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
792 cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
795 if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
796 hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
798 if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
799 hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
802 /* The code below is an attempt to let cursor themes
803 * determine the attachment of the icon to enable things
804 * like the following:
812 * It does not work since Xcursor doesn't allow to attach
813 * any additional information to cursors in a retrievable
814 * way (there are comments, but no way to get at them
815 * short of searching for the actual cursor file).
816 * If this code ever gets used, the icon_window placement
817 * must be changed to recognize these placement options
818 * as well. Note that this code ignores info->hot_x/y.
820 for (j = 0; j < 10; j++)
825 GtkAnchorType icon_anchor;
827 g_snprintf (key, 32, "comment%d", j);
828 opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
829 if (opt && g_str_has_prefix ("icon-attach:", opt))
831 toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
832 if (g_strv_length (toks) != 3)
837 icon_anchor = atoi (toks[0]);
838 icon_x = atoi (toks[1]);
839 icon_y = atoi (toks[2]);
843 case GTK_ANCHOR_NORTH:
844 case GTK_ANCHOR_CENTER:
845 case GTK_ANCHOR_SOUTH:
846 icon_x += icon_width / 2;
848 case GTK_ANCHOR_NORTH_EAST:
849 case GTK_ANCHOR_EAST:
850 case GTK_ANCHOR_SOUTH_EAST:
851 icon_x += icon_width;
858 case GTK_ANCHOR_WEST:
859 case GTK_ANCHOR_CENTER:
860 case GTK_ANCHOR_EAST:
861 icon_y += icon_height / 2;
863 case GTK_ANCHOR_SOUTH_WEST:
864 case GTK_ANCHOR_SOUTH:
865 case GTK_ANCHOR_SOUTH_EAST:
866 icon_x += icon_height;
878 cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
879 cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
881 ref_x = MAX (hot_x, icon_x);
882 ref_y = MAX (hot_y, icon_y);
883 width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
884 height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
886 if (gtk_drag_can_use_rgba_cursor (display, width, height))
888 /* Composite cursor and icon so that both hotspots
889 * end up at (ref_x, ref_y)
891 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
894 gdk_pixbuf_fill (pixbuf, 0xff000000);
896 gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
897 ref_x - icon_x, ref_y - icon_y,
898 icon_width, icon_height,
899 ref_x - icon_x, ref_y - icon_y,
901 GDK_INTERP_BILINEAR, 255);
903 gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
904 ref_x - hot_x, ref_y - hot_y,
905 cursor_width, cursor_height,
906 ref_x - hot_x, ref_y - hot_y,
908 GDK_INTERP_BILINEAR, 255);
910 info->drag_cursors[i] =
911 gdk_cursor_new_from_pixbuf (display, pixbuf, ref_x, ref_y);
913 g_object_unref (pixbuf);
916 g_object_unref (cursor_pixbuf);
918 if (info->drag_cursors[i] != NULL)
919 return info->drag_cursors[i];
922 return drag_cursors[i].cursor;
926 gtk_drag_update_cursor (GtkDragSourceInfo *info)
931 if (!info->have_grab)
934 for (i = 0 ; i < n_drag_cursors - 1; i++)
935 if (info->cursor == drag_cursors[i].cursor ||
936 info->cursor == info->drag_cursors[i])
939 if (i == n_drag_cursors)
942 cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor),
943 drag_cursors[i].action, info);
945 if (cursor != info->cursor)
949 pointer = gdk_drag_context_get_device (info->context);
950 gdk_device_grab (pointer,
951 gtk_widget_get_window (info->ipc_widget),
952 GDK_OWNERSHIP_APPLICATION, FALSE,
953 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
954 cursor, info->grab_time);
955 info->cursor = cursor;
959 /********************
961 ********************/
963 /*************************************************************
965 * Get the data for a drag or drop
967 * context - drag context
968 * target - format to retrieve the data in.
969 * time - timestamp of triggering event.
972 *************************************************************/
975 gtk_drag_get_data (GtkWidget *widget,
976 GdkDragContext *context,
980 GtkWidget *selection_widget;
982 g_return_if_fail (GTK_IS_WIDGET (widget));
983 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
984 g_return_if_fail (!context->is_source);
986 selection_widget = gtk_drag_get_ipc_widget (widget);
988 g_object_ref (context);
989 g_object_ref (widget);
991 g_signal_connect (selection_widget, "selection-received",
992 G_CALLBACK (gtk_drag_selection_received), widget);
994 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
996 gtk_selection_convert (selection_widget,
997 gdk_drag_get_selection (context),
1004 * gtk_drag_get_source_widget:
1005 * @context: a (destination side) drag context
1007 * Determines the source widget for a drag.
1009 * Return value: (transfer none): if the drag is occurring
1010 * within a single application, a pointer to the source widget.
1014 gtk_drag_get_source_widget (GdkDragContext *context)
1018 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
1019 g_return_val_if_fail (!context->is_source, NULL);
1021 tmp_list = source_widgets;
1024 GtkWidget *ipc_widget = tmp_list->data;
1026 if (gtk_widget_get_window (ipc_widget) == context->source_window)
1028 GtkDragSourceInfo *info;
1029 info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
1031 return info ? info->widget : NULL;
1034 tmp_list = tmp_list->next;
1040 /*************************************************************
1042 * Notify the drag source that the transfer of data
1045 * context: The drag context for this drag
1046 * success: Was the data successfully transferred?
1047 * time: The timestamp to use when notifying the destination.
1049 *************************************************************/
1052 gtk_drag_finish (GdkDragContext *context,
1057 GdkAtom target = GDK_NONE;
1059 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1060 g_return_if_fail (!context->is_source);
1064 target = gdk_atom_intern_static_string ("DELETE");
1066 else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
1068 target = gdk_atom_intern_static_string (success ?
1069 "XmTRANSFER_SUCCESS" :
1070 "XmTRANSFER_FAILURE");
1073 if (target != GDK_NONE)
1075 GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_window_get_screen (context->source_window));
1077 g_object_ref (context);
1079 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1080 g_signal_connect (selection_widget, "selection-received",
1081 G_CALLBACK (gtk_drag_selection_received),
1084 gtk_selection_convert (selection_widget,
1085 gdk_drag_get_selection (context),
1090 if (!(success && del))
1091 gdk_drop_finish (context, success, time);
1094 /*************************************************************
1095 * gtk_drag_highlight_draw:
1096 * Callback for expose_event for highlighted widgets.
1102 *************************************************************/
1105 gtk_drag_highlight_draw (GtkWidget *widget,
1109 int width = gtk_widget_get_allocated_width (widget);
1110 int height = gtk_widget_get_allocated_height (widget);
1112 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
1113 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1115 0, 0, width, height);
1117 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
1118 cairo_set_line_width (cr, 1.0);
1119 cairo_rectangle (cr,
1121 width - 1, height - 1);
1127 /*************************************************************
1128 * gtk_drag_highlight:
1129 * Highlight the given widget in the default manner.
1133 *************************************************************/
1136 gtk_drag_highlight (GtkWidget *widget)
1138 g_return_if_fail (GTK_IS_WIDGET (widget));
1140 g_signal_connect_after (widget, "draw",
1141 G_CALLBACK (gtk_drag_highlight_draw),
1144 gtk_widget_queue_draw (widget);
1147 /*************************************************************
1148 * gtk_drag_unhighlight:
1149 * Refresh the given widget to remove the highlight.
1153 *************************************************************/
1156 gtk_drag_unhighlight (GtkWidget *widget)
1158 g_return_if_fail (GTK_IS_WIDGET (widget));
1160 g_signal_handlers_disconnect_by_func (widget,
1161 gtk_drag_highlight_draw,
1164 gtk_widget_queue_draw (widget);
1168 gtk_drag_dest_set_internal (GtkWidget *widget,
1169 GtkDragDestSite *site)
1171 GtkDragDestSite *old_site;
1173 g_return_if_fail (widget != NULL);
1175 /* HACK, do this in the destroy */
1176 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1179 g_signal_handlers_disconnect_by_func (widget,
1180 gtk_drag_dest_realized,
1182 g_signal_handlers_disconnect_by_func (widget,
1183 gtk_drag_dest_hierarchy_changed,
1186 site->track_motion = old_site->track_motion;
1189 if (gtk_widget_get_realized (widget))
1190 gtk_drag_dest_realized (widget);
1192 g_signal_connect (widget, "realize",
1193 G_CALLBACK (gtk_drag_dest_realized), site);
1194 g_signal_connect (widget, "hierarchy-changed",
1195 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1197 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
1198 site, gtk_drag_dest_site_destroy);
1202 * gtk_drag_dest_set:
1203 * @widget: a #GtkWidget
1204 * @flags: which types of default drag behavior to use
1205 * @targets: (allow-none) (array length=n_targets): a pointer to an array of #GtkTargetEntry<!-- -->s
1206 * indicating the drop types that this @widget will accept, or %NULL.
1207 * Later you can access the list with gtk_drag_dest_get_target_list()
1208 * and gtk_drag_dest_find_target().
1209 * @n_targets: the number of entries in @targets
1210 * @actions: a bitmask of possible actions for a drop onto this @widget.
1212 * Sets a widget as a potential drop destination, and adds default behaviors.
1214 * The default behaviors listed in @flags have an effect similar
1215 * to installing default handlers for the widget's drag-and-drop signals
1216 * (#GtkWidget:drag-motion, #GtkWidget:drag-drop, ...). They all exist
1217 * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
1218 * sufficient to connect to the widget's #GtkWidget::drag-data-received
1219 * signal to get primitive, but consistent drag-and-drop support.
1221 * Things become more complicated when you try to preview the dragged data,
1222 * as described in the documentation for #GtkWidget:drag-motion. The default
1223 * behaviors described by @flags make some assumptions, that can conflict
1224 * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
1225 * invokations of gdk_drag_status() in the context of #GtkWidget:drag-motion,
1226 * and invokations of gtk_drag_finish() in #GtkWidget:drag-data-received.
1227 * Especially the later is dramatic, when your own #GtkWidget:drag-motion
1228 * handler calls gtk_drag_get_data() to inspect the dragged data.
1230 * There's no way to set a default action here, you can use the
1231 * #GtkWidget:drag-motion callback for that. Here's an example which selects
1232 * the action to use depending on whether the control key is pressed or not:
1235 * drag_motion (GtkWidget *widget,
1236 * GdkDragContext *context,
1241 * GdkModifierType mask;
1243 * gdk_window_get_pointer (gtk_widget_get_window (widget),
1244 * NULL, NULL, &mask);
1245 * if (mask & GDK_CONTROL_MASK)
1246 * gdk_drag_status (context, GDK_ACTION_COPY, time);
1248 * gdk_drag_status (context, GDK_ACTION_MOVE, time);
1253 gtk_drag_dest_set (GtkWidget *widget,
1254 GtkDestDefaults flags,
1255 const GtkTargetEntry *targets,
1257 GdkDragAction actions)
1259 GtkDragDestSite *site;
1261 g_return_if_fail (GTK_IS_WIDGET (widget));
1263 site = g_slice_new0 (GtkDragDestSite);
1265 site->flags = flags;
1266 site->have_drag = FALSE;
1268 site->target_list = gtk_target_list_new (targets, n_targets);
1270 site->target_list = NULL;
1271 site->actions = actions;
1272 site->do_proxy = FALSE;
1273 site->proxy_window = NULL;
1274 site->track_motion = FALSE;
1276 gtk_drag_dest_set_internal (widget, site);
1279 /*************************************************************
1280 * gtk_drag_dest_set_proxy:
1281 * Set up this widget to proxy drags elsewhere.
1284 * proxy_window: window to which forward drag events
1285 * protocol: Drag protocol which the dest widget accepts
1286 * use_coordinates: If true, send the same coordinates to the
1287 * destination, because it is a embedded
1290 *************************************************************/
1293 gtk_drag_dest_set_proxy (GtkWidget *widget,
1294 GdkWindow *proxy_window,
1295 GdkDragProtocol protocol,
1296 gboolean use_coordinates)
1298 GtkDragDestSite *site;
1300 g_return_if_fail (GTK_IS_WIDGET (widget));
1301 g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1303 site = g_new (GtkDragDestSite, 1);
1306 site->have_drag = FALSE;
1307 site->target_list = NULL;
1309 site->proxy_window = proxy_window;
1311 g_object_ref (proxy_window);
1312 site->do_proxy = TRUE;
1313 site->proxy_protocol = protocol;
1314 site->proxy_coords = use_coordinates;
1315 site->track_motion = FALSE;
1317 gtk_drag_dest_set_internal (widget, site);
1320 /*************************************************************
1321 * gtk_drag_dest_unset
1322 * Unregister this widget as a drag target.
1326 *************************************************************/
1329 gtk_drag_dest_unset (GtkWidget *widget)
1331 GtkDragDestSite *old_site;
1333 g_return_if_fail (GTK_IS_WIDGET (widget));
1335 old_site = g_object_get_data (G_OBJECT (widget),
1339 g_signal_handlers_disconnect_by_func (widget,
1340 gtk_drag_dest_realized,
1342 g_signal_handlers_disconnect_by_func (widget,
1343 gtk_drag_dest_hierarchy_changed,
1347 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1351 * gtk_drag_dest_get_target_list:
1352 * @widget: a #GtkWidget
1354 * Returns the list of targets this widget can accept from
1357 * Return value: the #GtkTargetList, or %NULL if none
1360 gtk_drag_dest_get_target_list (GtkWidget *widget)
1362 GtkDragDestSite *site;
1364 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1366 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1368 return site ? site->target_list : NULL;
1372 * gtk_drag_dest_set_target_list:
1373 * @widget: a #GtkWidget that's a drag destination
1374 * @target_list: (allow-none): list of droppable targets, or %NULL for none
1376 * Sets the target types that this widget can accept from drag-and-drop.
1377 * The widget must first be made into a drag destination with
1378 * gtk_drag_dest_set().
1381 gtk_drag_dest_set_target_list (GtkWidget *widget,
1382 GtkTargetList *target_list)
1384 GtkDragDestSite *site;
1386 g_return_if_fail (GTK_IS_WIDGET (widget));
1388 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1392 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1393 "to make the widget into a drag destination");
1398 gtk_target_list_ref (target_list);
1400 if (site->target_list)
1401 gtk_target_list_unref (site->target_list);
1403 site->target_list = target_list;
1407 * gtk_drag_dest_add_text_targets:
1408 * @widget: a #GtkWidget that's a drag destination
1410 * Add the text targets supported by #GtkSelection to
1411 * the target list of the drag destination. The targets
1412 * are added with @info = 0. If you need another value,
1413 * use gtk_target_list_add_text_targets() and
1414 * gtk_drag_dest_set_target_list().
1419 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1421 GtkTargetList *target_list;
1423 target_list = gtk_drag_dest_get_target_list (widget);
1425 gtk_target_list_ref (target_list);
1427 target_list = gtk_target_list_new (NULL, 0);
1428 gtk_target_list_add_text_targets (target_list, 0);
1429 gtk_drag_dest_set_target_list (widget, target_list);
1430 gtk_target_list_unref (target_list);
1434 * gtk_drag_dest_add_image_targets:
1435 * @widget: a #GtkWidget that's a drag destination
1437 * Add the image targets supported by #GtkSelection to
1438 * the target list of the drag destination. The targets
1439 * are added with @info = 0. If you need another value,
1440 * use gtk_target_list_add_image_targets() and
1441 * gtk_drag_dest_set_target_list().
1446 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1448 GtkTargetList *target_list;
1450 target_list = gtk_drag_dest_get_target_list (widget);
1452 gtk_target_list_ref (target_list);
1454 target_list = gtk_target_list_new (NULL, 0);
1455 gtk_target_list_add_image_targets (target_list, 0, FALSE);
1456 gtk_drag_dest_set_target_list (widget, target_list);
1457 gtk_target_list_unref (target_list);
1461 * gtk_drag_dest_add_uri_targets:
1462 * @widget: a #GtkWidget that's a drag destination
1464 * Add the URI targets supported by #GtkSelection to
1465 * the target list of the drag destination. The targets
1466 * are added with @info = 0. If you need another value,
1467 * use gtk_target_list_add_uri_targets() and
1468 * gtk_drag_dest_set_target_list().
1473 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1475 GtkTargetList *target_list;
1477 target_list = gtk_drag_dest_get_target_list (widget);
1479 gtk_target_list_ref (target_list);
1481 target_list = gtk_target_list_new (NULL, 0);
1482 gtk_target_list_add_uri_targets (target_list, 0);
1483 gtk_drag_dest_set_target_list (widget, target_list);
1484 gtk_target_list_unref (target_list);
1488 * gtk_drag_dest_set_track_motion:
1489 * @widget: a #GtkWidget that's a drag destination
1490 * @track_motion: whether to accept all targets
1492 * Tells the widget to emit ::drag-motion and ::drag-leave
1493 * events regardless of the targets and the %GTK_DEST_DEFAULT_MOTION
1496 * This may be used when a widget wants to do generic
1497 * actions regardless of the targets that the source offers.
1502 gtk_drag_dest_set_track_motion (GtkWidget *widget,
1503 gboolean track_motion)
1505 GtkDragDestSite *site;
1507 g_return_if_fail (GTK_IS_WIDGET (widget));
1509 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1511 g_return_if_fail (site != NULL);
1513 site->track_motion = track_motion != FALSE;
1517 * gtk_drag_dest_get_track_motion:
1518 * @widget: a #GtkWidget that's a drag destination
1520 * Returns whether the widget has been configured to always
1521 * emit ::drag-motion signals.
1523 * Return Value: %TRUE if the widget always emits ::drag-motion events
1528 gtk_drag_dest_get_track_motion (GtkWidget *widget)
1530 GtkDragDestSite *site;
1532 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1534 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1537 return site->track_motion;
1542 /*************************************************************
1543 * _gtk_drag_dest_handle_event:
1544 * Called from widget event handling code on Drag events
1548 * toplevel: Toplevel widget that received the event
1551 *************************************************************/
1554 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1557 GtkDragDestInfo *info;
1558 GdkDragContext *context;
1560 g_return_if_fail (toplevel != NULL);
1561 g_return_if_fail (event != NULL);
1563 context = event->dnd.context;
1565 info = gtk_drag_get_dest_info (context, TRUE);
1567 /* Find the widget for the event */
1568 switch (event->type)
1570 case GDK_DRAG_ENTER:
1573 case GDK_DRAG_LEAVE:
1576 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1577 info->widget = NULL;
1581 case GDK_DRAG_MOTION:
1582 case GDK_DROP_START:
1588 if (event->type == GDK_DROP_START)
1590 info->dropped = TRUE;
1591 /* We send a leave here so that the widget unhighlights
1596 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1597 info->widget = NULL;
1601 window = gtk_widget_get_window (toplevel);
1603 #ifdef GDK_WINDOWING_X11
1604 /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1606 * Currently gdk_window_get_position doesn't provide reliable
1607 * information for embedded windows, so we call the much more
1608 * expensive gdk_window_get_origin().
1610 if (GTK_IS_PLUG (toplevel))
1611 gdk_window_get_origin (window, &tx, &ty);
1613 #endif /* GDK_WINDOWING_X11 */
1614 gdk_window_get_position (window, &tx, &ty);
1616 found = gtk_drag_find_widget (toplevel,
1619 event->dnd.x_root - tx,
1620 event->dnd.y_root - ty,
1622 (event->type == GDK_DRAG_MOTION) ?
1623 gtk_drag_dest_motion :
1624 gtk_drag_dest_drop);
1626 if (info->widget && !found)
1628 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1629 info->widget = NULL;
1634 if (event->type == GDK_DRAG_MOTION)
1637 gdk_drag_status (context, 0, event->dnd.time);
1639 else if (event->type == GDK_DROP_START && !info->proxy_source)
1641 gdk_drop_reply (context, found, event->dnd.time);
1642 if ((context->protocol == GDK_DRAG_PROTO_MOTIF) && !found)
1643 gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1649 g_assert_not_reached ();
1654 * gtk_drag_dest_find_target:
1655 * @widget: drag destination widget
1656 * @context: drag context
1657 * @target_list: (allow-none): list of droppable targets, or %NULL to use
1658 * gtk_drag_dest_get_target_list (@widget).
1660 * Looks for a match between @context->targets and the
1661 * @dest_target_list, returning the first matching target, otherwise
1662 * returning %GDK_NONE. @dest_target_list should usually be the return
1663 * value from gtk_drag_dest_get_target_list(), but some widgets may
1664 * have different valid targets for different parts of the widget; in
1665 * that case, they will have to implement a drag_motion handler that
1666 * passes the correct target list to this function.
1668 * Return value: first target that the source offers and the dest can accept, or %GDK_NONE
1671 gtk_drag_dest_find_target (GtkWidget *widget,
1672 GdkDragContext *context,
1673 GtkTargetList *target_list)
1676 GList *tmp_source = NULL;
1677 GtkWidget *source_widget;
1679 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1680 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1681 g_return_val_if_fail (!context->is_source, GDK_NONE);
1684 source_widget = gtk_drag_get_source_widget (context);
1686 if (target_list == NULL)
1687 target_list = gtk_drag_dest_get_target_list (widget);
1689 if (target_list == NULL)
1692 tmp_target = target_list->list;
1695 GtkTargetPair *pair = tmp_target->data;
1696 tmp_source = context->targets;
1699 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1701 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1702 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)) &&
1703 (!(pair->flags & GTK_TARGET_OTHER_APP) || !source_widget) &&
1704 (!(pair->flags & GTK_TARGET_OTHER_WIDGET) || (source_widget != widget)))
1705 return pair->target;
1709 tmp_source = tmp_source->next;
1711 tmp_target = tmp_target->next;
1718 gtk_drag_selection_received (GtkWidget *widget,
1719 GtkSelectionData *selection_data,
1723 GdkDragContext *context;
1724 GtkDragDestInfo *info;
1725 GtkWidget *drop_widget;
1729 context = g_object_get_data (G_OBJECT (widget), "drag-context");
1730 info = gtk_drag_get_dest_info (context, FALSE);
1732 if (info->proxy_data &&
1733 info->proxy_data->target == selection_data->target)
1735 gtk_selection_data_set (info->proxy_data,
1736 selection_data->type,
1737 selection_data->format,
1738 selection_data->data,
1739 selection_data->length);
1744 if (selection_data->target == gdk_atom_intern_static_string ("DELETE"))
1746 gtk_drag_finish (context, TRUE, FALSE, time);
1748 else if ((selection_data->target == gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS")) ||
1749 (selection_data->target == gdk_atom_intern_static_string ("XmTRANSFER_FAILURE")))
1755 GtkDragDestSite *site;
1757 site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1759 if (site && site->target_list)
1763 if (gtk_target_list_find (site->target_list,
1764 selection_data->target,
1767 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1768 selection_data->length >= 0)
1769 g_signal_emit_by_name (drop_widget,
1770 "drag-data-received",
1771 context, info->drop_x, info->drop_y,
1778 g_signal_emit_by_name (drop_widget,
1779 "drag-data-received",
1780 context, info->drop_x, info->drop_y,
1785 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1788 gtk_drag_finish (context,
1789 (selection_data->length >= 0),
1790 (context->action == GDK_ACTION_MOVE),
1794 g_object_unref (drop_widget);
1797 g_signal_handlers_disconnect_by_func (widget,
1798 gtk_drag_selection_received,
1801 g_object_set_data (G_OBJECT (widget), I_("drag-context"), NULL);
1802 g_object_unref (context);
1804 gtk_drag_release_ipc_widget (widget);
1807 /*************************************************************
1808 * gtk_drag_find_widget:
1809 * Function used to locate widgets for
1810 * DRAG_MOTION and DROP_START events.
1811 *************************************************************/
1814 gtk_drag_find_widget (GtkWidget *widget,
1815 GdkDragContext *context,
1816 GtkDragDestInfo *info,
1820 GtkDragDestCallback callback)
1822 if (!gtk_widget_get_mapped (widget) ||
1823 !gtk_widget_get_sensitive (widget))
1826 /* Get the widget at the pointer coordinates and travel up
1827 * the widget hierarchy from there.
1829 widget = _gtk_widget_find_at_coords (gtk_widget_get_window (widget),
1837 GList *hierarchy = NULL;
1838 gboolean found = FALSE;
1840 if (!gtk_widget_get_mapped (widget) ||
1841 !gtk_widget_get_sensitive (widget))
1844 /* need to reference the entire hierarchy temporarily in case the
1845 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
1847 for (parent = widget;
1849 parent = gtk_widget_get_parent (parent))
1851 hierarchy = g_list_prepend (hierarchy, g_object_ref (parent));
1854 /* If the current widget is registered as a drop site, check to
1855 * emit "drag-motion" to check if we are actually in a drop
1858 if (g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1860 found = callback (widget, context, x, y, time);
1862 /* If so, send a "drag-leave" to the last widget */
1865 if (info->widget && info->widget != widget)
1867 gtk_drag_dest_leave (info->widget, context, time);
1870 info->widget = widget;
1876 /* Get the parent before unreffing the hierarchy because
1877 * invoking the callback might have destroyed the widget
1879 parent = gtk_widget_get_parent (widget);
1881 /* The parent might be going away when unreffing the
1882 * hierarchy, so also protect againt that
1885 g_object_add_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1888 g_list_foreach (hierarchy, (GFunc) g_object_unref, NULL);
1889 g_list_free (hierarchy);
1895 g_object_remove_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1899 if (!gtk_widget_translate_coordinates (widget, parent, x, y, &x, &y))
1909 gtk_drag_proxy_begin (GtkWidget *widget,
1910 GtkDragDestInfo *dest_info,
1913 GtkDragSourceInfo *source_info;
1915 GdkDragContext *context;
1916 GtkWidget *ipc_widget;
1918 if (dest_info->proxy_source)
1920 gdk_drag_abort (dest_info->proxy_source->context, time);
1921 gtk_drag_source_info_destroy (dest_info->proxy_source);
1922 dest_info->proxy_source = NULL;
1925 ipc_widget = gtk_drag_get_ipc_widget (widget);
1926 context = gdk_drag_begin (gtk_widget_get_window (ipc_widget),
1927 dest_info->context->targets);
1929 source_info = gtk_drag_get_source_info (context, TRUE);
1931 source_info->ipc_widget = ipc_widget;
1932 source_info->widget = g_object_ref (widget);
1934 source_info->target_list = gtk_target_list_new (NULL, 0);
1935 tmp_list = dest_info->context->targets;
1938 gtk_target_list_add (source_info->target_list,
1939 GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
1940 tmp_list = tmp_list->next;
1943 source_info->proxy_dest = dest_info;
1945 g_signal_connect (ipc_widget,
1947 G_CALLBACK (gtk_drag_selection_get),
1950 dest_info->proxy_source = source_info;
1954 gtk_drag_dest_info_destroy (gpointer data)
1956 g_slice_free (GtkDragDestInfo, data);
1959 static GtkDragDestInfo *
1960 gtk_drag_get_dest_info (GdkDragContext *context,
1963 GtkDragDestInfo *info;
1964 static GQuark info_quark = 0;
1966 info_quark = g_quark_from_static_string ("gtk-dest-info");
1968 info = g_object_get_qdata (G_OBJECT (context), info_quark);
1969 if (!info && create)
1971 info = g_slice_new0 (GtkDragDestInfo);
1972 info->context = context;
1973 g_object_set_qdata_full (G_OBJECT (context), info_quark,
1974 info, gtk_drag_dest_info_destroy);
1980 static GQuark dest_info_quark = 0;
1982 static GtkDragSourceInfo *
1983 gtk_drag_get_source_info (GdkDragContext *context,
1986 GtkDragSourceInfo *info;
1987 if (!dest_info_quark)
1988 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
1990 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
1991 if (!info && create)
1993 info = g_new0 (GtkDragSourceInfo, 1);
1994 info->context = context;
1995 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
2002 gtk_drag_clear_source_info (GdkDragContext *context)
2004 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
2008 gtk_drag_dest_realized (GtkWidget *widget)
2010 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2012 if (gtk_widget_is_toplevel (toplevel))
2013 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2017 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
2018 GtkWidget *previous_toplevel)
2020 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2022 if (gtk_widget_is_toplevel (toplevel) && gtk_widget_get_realized (toplevel))
2023 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2027 gtk_drag_dest_site_destroy (gpointer data)
2029 GtkDragDestSite *site = data;
2031 if (site->proxy_window)
2032 g_object_unref (site->proxy_window);
2034 if (site->target_list)
2035 gtk_target_list_unref (site->target_list);
2037 g_slice_free (GtkDragDestSite, site);
2041 * Default drag handlers
2044 gtk_drag_dest_leave (GtkWidget *widget,
2045 GdkDragContext *context,
2048 GtkDragDestSite *site;
2050 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2051 g_return_if_fail (site != NULL);
2055 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2057 if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
2059 gdk_drag_abort (info->proxy_source->context, time);
2060 gtk_drag_source_info_destroy (info->proxy_source);
2061 info->proxy_source = NULL;
2068 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
2069 gtk_drag_unhighlight (widget);
2071 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
2073 g_signal_emit_by_name (widget, "drag-leave", context, time);
2075 site->have_drag = FALSE;
2080 gtk_drag_dest_motion (GtkWidget *widget,
2081 GdkDragContext *context,
2086 GtkDragDestSite *site;
2087 GdkDragAction action = 0;
2090 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2091 g_return_val_if_fail (site != NULL, FALSE);
2096 GdkEvent *current_event;
2097 GdkWindow *dest_window;
2098 GdkDragProtocol proto;
2100 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2102 if (!info->proxy_source || info->proxy_source->widget != widget)
2103 gtk_drag_proxy_begin (widget, info, time);
2105 current_event = gtk_get_current_event ();
2107 if (site->proxy_window)
2109 dest_window = site->proxy_window;
2110 proto = site->proxy_protocol;
2114 gdk_drag_find_window_for_screen (info->proxy_source->context,
2116 gdk_window_get_screen (current_event->dnd.window),
2117 current_event->dnd.x_root,
2118 current_event->dnd.y_root,
2119 &dest_window, &proto);
2122 gdk_drag_motion (info->proxy_source->context,
2124 current_event->dnd.x_root,
2125 current_event->dnd.y_root,
2126 context->suggested_action,
2127 context->actions, time);
2129 if (!site->proxy_window && dest_window)
2130 g_object_unref (dest_window);
2132 selection = gdk_drag_get_selection (info->proxy_source->context);
2134 selection != gdk_drag_get_selection (info->context))
2135 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2137 gdk_event_free (current_event);
2142 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
2144 if (context->suggested_action & site->actions)
2145 action = context->suggested_action;
2150 for (i = 0; i < 8; i++)
2152 if ((site->actions & (1 << i)) &&
2153 (context->actions & (1 << i)))
2161 if (action && gtk_drag_dest_find_target (widget, context, NULL))
2163 if (!site->have_drag)
2165 site->have_drag = TRUE;
2166 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
2167 gtk_drag_highlight (widget);
2170 gdk_drag_status (context, action, time);
2174 gdk_drag_status (context, 0, time);
2175 if (!site->track_motion)
2180 g_signal_emit_by_name (widget, "drag-motion",
2181 context, x, y, time, &retval);
2183 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
2187 gtk_drag_dest_drop (GtkWidget *widget,
2188 GdkDragContext *context,
2193 GtkDragDestSite *site;
2194 GtkDragDestInfo *info;
2196 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2197 g_return_val_if_fail (site != NULL, FALSE);
2199 info = gtk_drag_get_dest_info (context, FALSE);
2200 g_return_val_if_fail (info != NULL, FALSE);
2207 if (info->proxy_source ||
2208 (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN))
2210 gtk_drag_drop (info->proxy_source, time);
2214 /* We need to synthesize a motion event, wait for a status,
2215 * and, if we get a good one, do a drop.
2218 GdkEvent *current_event;
2220 GdkWindow *dest_window;
2221 GdkDragProtocol proto;
2223 gtk_drag_proxy_begin (widget, info, time);
2224 info->proxy_drop_wait = TRUE;
2225 info->proxy_drop_time = time;
2227 current_event = gtk_get_current_event ();
2229 if (site->proxy_window)
2231 dest_window = site->proxy_window;
2232 proto = site->proxy_protocol;
2236 gdk_drag_find_window_for_screen (info->proxy_source->context,
2238 gdk_window_get_screen (current_event->dnd.window),
2239 current_event->dnd.x_root,
2240 current_event->dnd.y_root,
2241 &dest_window, &proto);
2244 gdk_drag_motion (info->proxy_source->context,
2246 current_event->dnd.x_root,
2247 current_event->dnd.y_root,
2248 context->suggested_action,
2249 context->actions, time);
2251 if (!site->proxy_window && dest_window)
2252 g_object_unref (dest_window);
2254 selection = gdk_drag_get_selection (info->proxy_source->context);
2256 selection != gdk_drag_get_selection (info->context))
2257 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2259 gdk_event_free (current_event);
2268 if (site->flags & GTK_DEST_DEFAULT_DROP)
2270 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2272 if (target == GDK_NONE)
2274 gtk_drag_finish (context, FALSE, FALSE, time);
2278 gtk_drag_get_data (widget, context, target, time);
2281 g_signal_emit_by_name (widget, "drag-drop",
2282 context, x, y, time, &retval);
2284 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2292 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2293 * so that we can set the icon from the source site information
2295 static GdkDragContext *
2296 gtk_drag_begin_internal (GtkWidget *widget,
2297 GtkDragSourceSite *site,
2298 GtkTargetList *target_list,
2299 GdkDragAction actions,
2303 GtkDragSourceInfo *info;
2304 GList *targets = NULL;
2306 guint32 time = GDK_CURRENT_TIME;
2307 GdkDragAction possible_actions, suggested_action;
2308 GdkDragContext *context;
2309 GtkWidget *ipc_widget;
2311 GdkDevice *pointer, *keyboard;
2312 GdkWindow *ipc_window;
2314 pointer = keyboard = NULL;
2315 ipc_widget = gtk_drag_get_ipc_widget (widget);
2317 gtk_drag_get_event_actions (event, button, actions,
2318 &suggested_action, &possible_actions);
2320 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2326 time = gdk_event_get_time (event);
2327 if (time == GDK_CURRENT_TIME)
2328 time = gtk_get_current_event_time ();
2330 pointer = gdk_event_get_device (event);
2332 if (pointer->source == GDK_SOURCE_KEYBOARD)
2335 pointer = gdk_device_get_associated_device (keyboard);
2338 keyboard = gdk_device_get_associated_device (pointer);
2342 GdkDeviceManager *device_manager;
2344 device_manager = gdk_display_get_device_manager (gtk_widget_get_display (widget));
2345 pointer = gdk_device_manager_get_client_pointer (device_manager);
2346 keyboard = gdk_device_get_associated_device (pointer);
2352 ipc_window = gtk_widget_get_window (ipc_widget);
2354 if (gdk_device_grab (pointer, ipc_window,
2355 GDK_OWNERSHIP_APPLICATION, FALSE,
2356 GDK_POINTER_MOTION_MASK |
2357 GDK_BUTTON_RELEASE_MASK,
2358 cursor, time) != GDK_GRAB_SUCCESS)
2360 gtk_drag_release_ipc_widget (ipc_widget);
2365 grab_dnd_keys (ipc_widget, keyboard, time);
2367 /* We use a GTK grab here to override any grabs that the widget
2368 * we are dragging from might have held
2370 gtk_device_grab_add (ipc_widget, pointer, FALSE);
2372 tmp_list = g_list_last (target_list->list);
2375 GtkTargetPair *pair = tmp_list->data;
2376 targets = g_list_prepend (targets,
2377 GINT_TO_POINTER (pair->target));
2378 tmp_list = tmp_list->prev;
2381 source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2383 context = gdk_drag_begin (ipc_window, targets);
2384 gdk_drag_context_set_device (context, pointer);
2385 g_list_free (targets);
2387 info = gtk_drag_get_source_info (context, TRUE);
2389 info->ipc_widget = ipc_widget;
2390 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), info);
2392 info->widget = g_object_ref (widget);
2394 info->button = button;
2395 info->cursor = cursor;
2396 info->target_list = target_list;
2397 gtk_target_list_ref (target_list);
2399 info->possible_actions = actions;
2401 info->status = GTK_DRAG_STATUS_DRAG;
2402 info->last_event = NULL;
2403 info->selections = NULL;
2404 info->icon_window = NULL;
2405 info->destroy_icon = FALSE;
2407 /* Set cur_x, cur_y here so if the "drag-begin" signal shows
2408 * the drag icon, it will be in the right place
2410 if (event && event->type == GDK_MOTION_NOTIFY)
2412 info->cur_screen = gtk_widget_get_screen (widget);
2413 info->cur_x = event->motion.x_root;
2414 info->cur_y = event->motion.y_root;
2418 gdk_display_get_device_state (gtk_widget_get_display (widget), pointer,
2419 &info->cur_screen, &info->cur_x, &info->cur_y, NULL);
2422 g_signal_emit_by_name (widget, "drag-begin", info->context);
2424 /* Ensure that we have an icon before we start the drag; the
2425 * application may have set one in ::drag_begin, or it may
2428 if (!info->icon_window && !info->icon_pixbuf)
2430 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2431 gtk_drag_set_icon_default (context);
2433 switch (site->icon_type)
2435 case GTK_IMAGE_PIXBUF:
2436 gtk_drag_set_icon_pixbuf (context,
2437 site->icon_data.pixbuf.pixbuf,
2440 case GTK_IMAGE_STOCK:
2441 gtk_drag_set_icon_stock (context,
2442 site->icon_data.stock.stock_id,
2445 case GTK_IMAGE_ICON_NAME:
2446 gtk_drag_set_icon_name (context,
2447 site->icon_data.name.icon_name,
2450 case GTK_IMAGE_EMPTY:
2452 g_assert_not_reached();
2457 /* We need to composite the icon into the cursor, if we are
2458 * not using an icon window.
2460 if (info->icon_pixbuf)
2462 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2466 if (cursor != info->cursor)
2468 gdk_device_grab (pointer, gtk_widget_get_window (widget),
2469 GDK_OWNERSHIP_APPLICATION, FALSE,
2470 GDK_POINTER_MOTION_MASK |
2471 GDK_BUTTON_RELEASE_MASK,
2473 info->cursor = cursor;
2477 if (event && event->type == GDK_MOTION_NOTIFY)
2478 gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2480 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, event);
2482 info->start_x = info->cur_x;
2483 info->start_y = info->cur_y;
2485 g_signal_connect (info->ipc_widget, "grab-broken-event",
2486 G_CALLBACK (gtk_drag_grab_broken_event_cb), info);
2487 g_signal_connect (info->ipc_widget, "grab-notify",
2488 G_CALLBACK (gtk_drag_grab_notify_cb), info);
2489 g_signal_connect (info->ipc_widget, "button-release-event",
2490 G_CALLBACK (gtk_drag_button_release_cb), info);
2491 g_signal_connect (info->ipc_widget, "motion-notify-event",
2492 G_CALLBACK (gtk_drag_motion_cb), info);
2493 g_signal_connect (info->ipc_widget, "key-press-event",
2494 G_CALLBACK (gtk_drag_key_cb), info);
2495 g_signal_connect (info->ipc_widget, "key-release-event",
2496 G_CALLBACK (gtk_drag_key_cb), info);
2497 g_signal_connect (info->ipc_widget, "selection-get",
2498 G_CALLBACK (gtk_drag_selection_get), info);
2500 info->have_grab = TRUE;
2501 info->grab_time = time;
2503 return info->context;
2508 * @widget: the source widget.
2509 * @targets: The targets (data formats) in which the
2510 * source can provide the data.
2511 * @actions: A bitmask of the allowed drag actions for this drag.
2512 * @button: The button the user clicked to start the drag.
2513 * @event: The event that triggered the start of the drag.
2515 * Initiates a drag on the source side. The function
2516 * only needs to be used when the application is
2517 * starting drags itself, and is not needed when
2518 * gtk_drag_source_set() is used.
2520 * The @event is used to retrieve the timestamp that will be used internally to
2521 * grab the pointer. If @event is #NULL, then GDK_CURRENT_TIME will be used.
2522 * However, you should try to pass a real event in all cases, since that can be
2523 * used by GTK+ to get information about the start position of the drag, for
2524 * example if the @event is a GDK_MOTION_NOTIFY.
2526 * Generally there are three cases when you want to start a drag by hand by
2527 * calling this function:
2529 * 1. During a button-press-event handler, if you want to start a drag
2530 * immediately when the user presses the mouse button. Pass the @event
2531 * that you have in your button-press-event handler.
2533 * 2. During a motion-notify-event handler, if you want to start a drag
2534 * when the mouse moves past a certain threshold distance after a button-press.
2535 * Pass the @event that you have in your motion-notify-event handler.
2537 * 3. During a timeout handler, if you want to start a drag after the mouse
2538 * button is held down for some time. Try to save the last event that you got
2539 * from the mouse, using gdk_event_copy(), and pass it to this function
2540 * (remember to free the event with gdk_event_free() when you are done).
2541 * If you can really not pass a real event, pass #NULL instead.
2543 * Return value: the context for this drag.
2546 gtk_drag_begin (GtkWidget *widget,
2547 GtkTargetList *targets,
2548 GdkDragAction actions,
2552 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2553 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
2554 g_return_val_if_fail (targets != NULL, NULL);
2556 return gtk_drag_begin_internal (widget, NULL, targets,
2557 actions, button, event);
2561 * gtk_drag_source_set:
2562 * @widget: a #GtkWidget
2563 * @start_button_mask: the bitmask of buttons that can start the drag
2564 * @targets: (allow-none) (array length=n_targets): the table of targets that the drag will support,
2566 * @n_targets: the number of items in @targets
2567 * @actions: the bitmask of possible actions for a drag from this widget
2569 * Sets up a widget so that GTK+ will start a drag operation when the user
2570 * clicks and drags on the widget. The widget must have a window.
2573 gtk_drag_source_set (GtkWidget *widget,
2574 GdkModifierType start_button_mask,
2575 const GtkTargetEntry *targets,
2577 GdkDragAction actions)
2579 GtkDragSourceSite *site;
2581 g_return_if_fail (GTK_IS_WIDGET (widget));
2583 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2585 gtk_widget_add_events (widget,
2586 gtk_widget_get_events (widget) |
2587 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2588 GDK_BUTTON_MOTION_MASK);
2592 if (site->target_list)
2593 gtk_target_list_unref (site->target_list);
2597 site = g_slice_new0 (GtkDragSourceSite);
2599 site->icon_type = GTK_IMAGE_EMPTY;
2601 g_signal_connect (widget, "button-press-event",
2602 G_CALLBACK (gtk_drag_source_event_cb),
2604 g_signal_connect (widget, "button-release-event",
2605 G_CALLBACK (gtk_drag_source_event_cb),
2607 g_signal_connect (widget, "motion-notify-event",
2608 G_CALLBACK (gtk_drag_source_event_cb),
2611 g_object_set_data_full (G_OBJECT (widget),
2612 I_("gtk-site-data"),
2613 site, gtk_drag_source_site_destroy);
2616 site->start_button_mask = start_button_mask;
2618 site->target_list = gtk_target_list_new (targets, n_targets);
2620 site->actions = actions;
2623 /*************************************************************
2624 * gtk_drag_source_unset
2625 * Unregister this widget as a drag source.
2629 *************************************************************/
2632 gtk_drag_source_unset (GtkWidget *widget)
2634 GtkDragSourceSite *site;
2636 g_return_if_fail (GTK_IS_WIDGET (widget));
2638 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2642 g_signal_handlers_disconnect_by_func (widget,
2643 gtk_drag_source_event_cb,
2645 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
2650 * gtk_drag_source_get_target_list:
2651 * @widget: a #GtkWidget
2653 * Gets the list of targets this widget can provide for
2656 * Return value: the #GtkTargetList, or %NULL if none
2661 gtk_drag_source_get_target_list (GtkWidget *widget)
2663 GtkDragSourceSite *site;
2665 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2667 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2669 return site ? site->target_list : NULL;
2673 * gtk_drag_source_set_target_list:
2674 * @widget: a #GtkWidget that's a drag source
2675 * @target_list: (allow-none): list of draggable targets, or %NULL for none
2677 * Changes the target types that this widget offers for drag-and-drop.
2678 * The widget must first be made into a drag source with
2679 * gtk_drag_source_set().
2684 gtk_drag_source_set_target_list (GtkWidget *widget,
2685 GtkTargetList *target_list)
2687 GtkDragSourceSite *site;
2689 g_return_if_fail (GTK_IS_WIDGET (widget));
2691 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2694 g_warning ("gtk_drag_source_set_target_list() requires the widget "
2695 "to already be a drag source.");
2700 gtk_target_list_ref (target_list);
2702 if (site->target_list)
2703 gtk_target_list_unref (site->target_list);
2705 site->target_list = target_list;
2709 * gtk_drag_source_add_text_targets:
2710 * @widget: a #GtkWidget that's is a drag source
2712 * Add the text targets supported by #GtkSelection to
2713 * the target list of the drag source. The targets
2714 * are added with @info = 0. If you need another value,
2715 * use gtk_target_list_add_text_targets() and
2716 * gtk_drag_source_set_target_list().
2721 gtk_drag_source_add_text_targets (GtkWidget *widget)
2723 GtkTargetList *target_list;
2725 target_list = gtk_drag_source_get_target_list (widget);
2727 gtk_target_list_ref (target_list);
2729 target_list = gtk_target_list_new (NULL, 0);
2730 gtk_target_list_add_text_targets (target_list, 0);
2731 gtk_drag_source_set_target_list (widget, target_list);
2732 gtk_target_list_unref (target_list);
2736 * gtk_drag_source_add_image_targets:
2737 * @widget: a #GtkWidget that's is a drag source
2739 * Add the writable image targets supported by #GtkSelection to
2740 * the target list of the drag source. The targets
2741 * are added with @info = 0. If you need another value,
2742 * use gtk_target_list_add_image_targets() and
2743 * gtk_drag_source_set_target_list().
2748 gtk_drag_source_add_image_targets (GtkWidget *widget)
2750 GtkTargetList *target_list;
2752 target_list = gtk_drag_source_get_target_list (widget);
2754 gtk_target_list_ref (target_list);
2756 target_list = gtk_target_list_new (NULL, 0);
2757 gtk_target_list_add_image_targets (target_list, 0, TRUE);
2758 gtk_drag_source_set_target_list (widget, target_list);
2759 gtk_target_list_unref (target_list);
2763 * gtk_drag_source_add_uri_targets:
2764 * @widget: a #GtkWidget that's is a drag source
2766 * Add the URI targets supported by #GtkSelection to
2767 * the target list of the drag source. The targets
2768 * are added with @info = 0. If you need another value,
2769 * use gtk_target_list_add_uri_targets() and
2770 * gtk_drag_source_set_target_list().
2775 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2777 GtkTargetList *target_list;
2779 target_list = gtk_drag_source_get_target_list (widget);
2781 gtk_target_list_ref (target_list);
2783 target_list = gtk_target_list_new (NULL, 0);
2784 gtk_target_list_add_uri_targets (target_list, 0);
2785 gtk_drag_source_set_target_list (widget, target_list);
2786 gtk_target_list_unref (target_list);
2790 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2792 switch (site->icon_type)
2794 case GTK_IMAGE_EMPTY:
2796 case GTK_IMAGE_PIXBUF:
2797 g_object_unref (site->icon_data.pixbuf.pixbuf);
2799 case GTK_IMAGE_STOCK:
2800 g_free (site->icon_data.stock.stock_id);
2802 case GTK_IMAGE_ICON_NAME:
2803 g_free (site->icon_data.name.icon_name);
2806 g_assert_not_reached();
2809 site->icon_type = GTK_IMAGE_EMPTY;
2813 * gtk_drag_source_set_icon_pixbuf:
2814 * @widget: a #GtkWidget
2815 * @pixbuf: the #GdkPixbuf for the drag icon
2817 * Sets the icon that will be used for drags from a particular widget
2818 * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
2819 * release it when it is no longer needed.
2822 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
2825 GtkDragSourceSite *site;
2827 g_return_if_fail (GTK_IS_WIDGET (widget));
2828 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2830 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2831 g_return_if_fail (site != NULL);
2832 g_object_ref (pixbuf);
2834 gtk_drag_source_unset_icon (site);
2836 site->icon_type = GTK_IMAGE_PIXBUF;
2837 site->icon_data.pixbuf.pixbuf = pixbuf;
2841 * gtk_drag_source_set_icon_stock:
2842 * @widget: a #GtkWidget
2843 * @stock_id: the ID of the stock icon to use
2845 * Sets the icon that will be used for drags from a particular source
2849 gtk_drag_source_set_icon_stock (GtkWidget *widget,
2850 const gchar *stock_id)
2852 GtkDragSourceSite *site;
2854 g_return_if_fail (GTK_IS_WIDGET (widget));
2855 g_return_if_fail (stock_id != NULL);
2857 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2858 g_return_if_fail (site != NULL);
2860 gtk_drag_source_unset_icon (site);
2862 site->icon_type = GTK_IMAGE_STOCK;
2863 site->icon_data.stock.stock_id = g_strdup (stock_id);
2867 * gtk_drag_source_set_icon_name:
2868 * @widget: a #GtkWidget
2869 * @icon_name: name of icon to use
2871 * Sets the icon that will be used for drags from a particular source
2872 * to a themed icon. See the docs for #GtkIconTheme for more details.
2877 gtk_drag_source_set_icon_name (GtkWidget *widget,
2878 const gchar *icon_name)
2880 GtkDragSourceSite *site;
2882 g_return_if_fail (GTK_IS_WIDGET (widget));
2883 g_return_if_fail (icon_name != NULL);
2885 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2886 g_return_if_fail (site != NULL);
2888 gtk_drag_source_unset_icon (site);
2890 site->icon_type = GTK_IMAGE_ICON_NAME;
2891 site->icon_data.name.icon_name = g_strdup (icon_name);
2895 gtk_drag_get_icon (GtkDragSourceInfo *info,
2896 GtkWidget **icon_window,
2900 if (get_can_change_screen (info->icon_window))
2901 gtk_window_set_screen (GTK_WINDOW (info->icon_window),
2904 if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
2906 if (!info->fallback_icon)
2908 gint save_hot_x, save_hot_y;
2909 gboolean save_destroy_icon;
2910 GtkWidget *save_icon_window;
2912 /* HACK to get the appropriate icon
2914 save_icon_window = info->icon_window;
2915 save_hot_x = info->hot_x;
2916 save_hot_y = info->hot_x;
2917 save_destroy_icon = info->destroy_icon;
2919 info->icon_window = NULL;
2920 set_icon_stock_pixbuf (info->context,
2921 GTK_STOCK_DND, NULL, -2, -2, TRUE);
2922 info->fallback_icon = info->icon_window;
2924 info->icon_window = save_icon_window;
2925 info->hot_x = save_hot_x;
2926 info->hot_y = save_hot_y;
2927 info->destroy_icon = save_destroy_icon;
2930 gtk_widget_hide (info->icon_window);
2932 *icon_window = info->fallback_icon;
2933 gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
2940 if (info->fallback_icon)
2941 gtk_widget_hide (info->fallback_icon);
2943 *icon_window = info->icon_window;
2944 *hot_x = info->hot_x;
2945 *hot_y = info->hot_y;
2950 gtk_drag_update_icon (GtkDragSourceInfo *info)
2952 if (info->icon_window)
2954 GtkWidget *icon_window;
2957 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
2959 gtk_window_move (GTK_WINDOW (icon_window),
2960 info->cur_x - hot_x,
2961 info->cur_y - hot_y);
2963 if (gtk_widget_get_visible (icon_window))
2964 gdk_window_raise (gtk_widget_get_window (icon_window));
2966 gtk_widget_show (icon_window);
2971 gtk_drag_set_icon_window (GdkDragContext *context,
2975 gboolean destroy_on_release)
2977 GtkDragSourceInfo *info;
2979 info = gtk_drag_get_source_info (context, FALSE);
2982 if (destroy_on_release)
2983 gtk_widget_destroy (widget);
2987 gtk_drag_remove_icon (info);
2990 g_object_ref (widget);
2992 info->icon_window = widget;
2993 info->hot_x = hot_x;
2994 info->hot_y = hot_y;
2995 info->destroy_icon = destroy_on_release;
2997 if (widget && info->icon_pixbuf)
2999 g_object_unref (info->icon_pixbuf);
3000 info->icon_pixbuf = NULL;
3003 gtk_drag_update_cursor (info);
3004 gtk_drag_update_icon (info);
3008 * gtk_drag_set_icon_widget:
3009 * @context: the context for a drag. (This must be called
3010 with a context for the source side of a drag)
3011 * @widget: a toplevel window to use as an icon.
3012 * @hot_x: the X offset within @widget of the hotspot.
3013 * @hot_y: the Y offset within @widget of the hotspot.
3015 * Changes the icon for a widget to a given widget. GTK+
3016 * will not destroy the icon, so if you don't want
3017 * it to persist, you should connect to the "drag-end"
3018 * signal and destroy it yourself.
3021 gtk_drag_set_icon_widget (GdkDragContext *context,
3026 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3027 g_return_if_fail (context->is_source);
3028 g_return_if_fail (GTK_IS_WIDGET (widget));
3030 gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
3034 icon_window_realize (GtkWidget *window,
3037 cairo_surface_t *surface;
3038 cairo_pattern_t *pattern;
3041 surface = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3042 CAIRO_CONTENT_COLOR,
3043 gdk_pixbuf_get_width (pixbuf),
3044 gdk_pixbuf_get_height (pixbuf));
3046 cr = cairo_create (surface);
3047 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3048 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3050 cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3052 cairo_pop_group_to_source (cr);
3056 pattern = cairo_pattern_create_for_surface (surface);
3057 gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3058 cairo_pattern_destroy (pattern);
3060 cairo_surface_destroy (surface);
3062 if (gdk_pixbuf_get_has_alpha (pixbuf))
3064 cairo_region_t *region;
3066 surface = cairo_image_surface_create (CAIRO_FORMAT_A1,
3067 gdk_pixbuf_get_width (pixbuf),
3068 gdk_pixbuf_get_height (pixbuf));
3070 cr = cairo_create (surface);
3071 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3075 region = gdk_cairo_region_create_from_surface (surface);
3076 gtk_widget_shape_combine_region (window, region);
3077 cairo_region_destroy (region);
3079 cairo_surface_destroy (surface);
3084 set_icon_stock_pixbuf (GdkDragContext *context,
3085 const gchar *stock_id,
3089 gboolean force_window)
3094 GdkDisplay *display;
3096 g_return_if_fail (context != NULL);
3097 g_return_if_fail (pixbuf != NULL || stock_id != NULL);
3098 g_return_if_fail (pixbuf == NULL || stock_id == NULL);
3100 screen = gdk_window_get_screen (context->source_window);
3102 window = gtk_window_new (GTK_WINDOW_POPUP);
3103 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3104 gtk_window_set_screen (GTK_WINDOW (window), screen);
3105 set_can_change_screen (window, TRUE);
3107 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3108 gtk_widget_set_app_paintable (window, TRUE);
3112 pixbuf = gtk_widget_render_icon (window, stock_id,
3113 GTK_ICON_SIZE_DND, NULL);
3117 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
3118 gtk_widget_destroy (window);
3124 g_object_ref (pixbuf);
3126 display = gdk_window_get_display (context->source_window);
3127 width = gdk_pixbuf_get_width (pixbuf);
3128 height = gdk_pixbuf_get_height (pixbuf);
3130 if (!force_window &&
3131 gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
3133 GtkDragSourceInfo *info;
3135 gtk_widget_destroy (window);
3137 info = gtk_drag_get_source_info (context, FALSE);
3139 if (info->icon_pixbuf)
3140 g_object_unref (info->icon_pixbuf);
3141 info->icon_pixbuf = pixbuf;
3143 gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
3147 gtk_widget_set_size_request (window, width, height);
3149 g_signal_connect_closure (window, "realize",
3150 g_cclosure_new (G_CALLBACK (icon_window_realize),
3152 (GClosureNotify)g_object_unref),
3155 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3160 * gtk_drag_set_icon_pixbuf:
3161 * @context: the context for a drag. (This must be called
3162 * with a context for the source side of a drag)
3163 * @pixbuf: the #GdkPixbuf to use as the drag icon.
3164 * @hot_x: the X offset within @widget of the hotspot.
3165 * @hot_y: the Y offset within @widget of the hotspot.
3167 * Sets @pixbuf as the icon for a given drag.
3170 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
3175 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3176 g_return_if_fail (context->is_source);
3177 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3179 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3183 * gtk_drag_set_icon_stock:
3184 * @context: the context for a drag. (This must be called
3185 * with a context for the source side of a drag)
3186 * @stock_id: the ID of the stock icon to use for the drag.
3187 * @hot_x: the X offset within the icon of the hotspot.
3188 * @hot_y: the Y offset within the icon of the hotspot.
3190 * Sets the icon for a given drag from a stock ID.
3193 gtk_drag_set_icon_stock (GdkDragContext *context,
3194 const gchar *stock_id,
3198 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3199 g_return_if_fail (context->is_source);
3200 g_return_if_fail (stock_id != NULL);
3202 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
3205 /* XXX: This function is in gdk, too. Should it be in Cairo? */
3207 _gtk_cairo_surface_extents (cairo_surface_t *surface,
3208 GdkRectangle *extents)
3210 double x1, x2, y1, y2;
3213 g_return_val_if_fail (surface != NULL, FALSE);
3214 g_return_val_if_fail (extents != NULL, FALSE);
3216 cr = cairo_create (surface);
3217 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
3226 if (x1 < G_MININT || x1 > G_MAXINT ||
3227 y1 < G_MININT || y1 > G_MAXINT ||
3228 x2 > G_MAXINT || y2 > G_MAXINT)
3230 extents->x = extents->y = extents->width = extents->height = 0;
3236 extents->width = x2;
3237 extents->height = y2;
3243 * gtk_drag_set_icon_surface:
3244 * @context: the context for a drag. (This must be called
3245 * with a context for the source side of a drag)
3246 * @surface: the surface to use as icon
3248 * Sets @surface as the icon for a given drag. GTK+ retains
3249 * references for the arguments, and will release them when
3250 * they are no longer needed.
3252 * To position the surface relative to the mouse, use
3253 * cairo_surface_set_device_offset() on @surface. The mouse
3254 * cursor will be positioned at the (0,0) coordinate of the
3258 gtk_drag_set_icon_surface (GdkDragContext *context,
3259 cairo_surface_t *surface)
3263 GdkRectangle extents;
3264 cairo_pattern_t *pattern;
3266 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3267 g_return_if_fail (context->is_source);
3268 g_return_if_fail (surface != NULL);
3270 _gtk_cairo_surface_extents (surface, &extents);
3273 screen = gdk_window_get_screen (context->source_window);
3275 window = gtk_window_new (GTK_WINDOW_POPUP);
3276 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3277 gtk_window_set_screen (GTK_WINDOW (window), screen);
3278 set_can_change_screen (window, TRUE);
3280 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3281 gtk_widget_set_app_paintable (window, TRUE);
3283 gtk_widget_set_size_request (window, extents.width, extents.height);
3284 gtk_widget_realize (window);
3286 if (cairo_surface_get_content (surface) != CAIRO_CONTENT_COLOR)
3288 cairo_surface_t *saturated;
3289 cairo_region_t *region;
3292 region = gdk_cairo_region_create_from_surface (surface);
3293 cairo_region_translate (region, -extents.x, -extents.y);
3295 gtk_widget_shape_combine_region (window, region);
3296 cairo_region_destroy (region);
3298 /* Need to saturate the colors, so it doesn't look like semi-transparent
3299 * pixels were painted on black. */
3300 saturated = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3301 CAIRO_CONTENT_COLOR,
3305 cr = cairo_create (saturated);
3306 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3307 cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
3309 cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3311 cairo_pop_group_to_source (cr);
3315 pattern = cairo_pattern_create_for_surface (saturated);
3317 cairo_surface_destroy (saturated);
3321 cairo_matrix_t matrix;
3323 pattern = cairo_pattern_create_for_surface (surface);
3324 cairo_matrix_init_translate (&matrix, extents.x, extents.y);
3325 cairo_pattern_set_matrix (pattern, &matrix);
3328 gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3330 gtk_drag_set_icon_window (context, window, extents.x, extents.y, TRUE);
3334 * gtk_drag_set_icon_name:
3335 * @context: the context for a drag. (This must be called
3336 * with a context for the source side of a drag)
3337 * @icon_name: name of icon to use
3338 * @hot_x: the X offset of the hotspot within the icon
3339 * @hot_y: the Y offset of the hotspot within the icon
3341 * Sets the icon for a given drag from a named themed icon. See
3342 * the docs for #GtkIconTheme for more details. Note that the
3343 * size of the icon depends on the icon theme (the icon is
3344 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
3345 * @hot_x and @hot_y have to be used with care.
3350 gtk_drag_set_icon_name (GdkDragContext *context,
3351 const gchar *icon_name,
3356 GtkSettings *settings;
3357 GtkIconTheme *icon_theme;
3359 gint width, height, icon_size;
3361 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3362 g_return_if_fail (context->is_source);
3363 g_return_if_fail (icon_name != NULL);
3365 screen = gdk_window_get_screen (context->source_window);
3366 g_return_if_fail (screen != NULL);
3368 settings = gtk_settings_get_for_screen (screen);
3369 if (gtk_icon_size_lookup_for_settings (settings,
3372 icon_size = MAX (width, height);
3374 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
3376 icon_theme = gtk_icon_theme_get_for_screen (screen);
3378 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
3379 icon_size, 0, NULL);
3381 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3383 g_warning ("Cannot load drag icon from icon name %s", icon_name);
3387 * gtk_drag_set_icon_default:
3388 * @context: the context for a drag. (This must be called
3389 with a context for the source side of a drag)
3391 * Sets the icon for a particular drag to the default
3395 gtk_drag_set_icon_default (GdkDragContext *context)
3397 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3398 g_return_if_fail (context->is_source);
3400 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
3403 /*************************************************************
3404 * _gtk_drag_source_handle_event:
3405 * Called from widget event handling code on Drag events
3409 * toplevel: Toplevel widget that received the event
3412 *************************************************************/
3415 _gtk_drag_source_handle_event (GtkWidget *widget,
3418 GtkDragSourceInfo *info;
3419 GdkDragContext *context;
3421 g_return_if_fail (widget != NULL);
3422 g_return_if_fail (event != NULL);
3424 context = event->dnd.context;
3425 info = gtk_drag_get_source_info (context, FALSE);
3429 switch (event->type)
3431 case GDK_DRAG_STATUS:
3435 if (info->proxy_dest)
3437 if (!event->dnd.send_event)
3439 if (info->proxy_dest->proxy_drop_wait)
3441 gboolean result = context->action != 0;
3443 /* Aha - we can finally pass the MOTIF DROP on... */
3444 gdk_drop_reply (info->proxy_dest->context, result, info->proxy_dest->proxy_drop_time);
3446 gdk_drag_drop (info->context, info->proxy_dest->proxy_drop_time);
3448 gtk_drag_finish (info->proxy_dest->context, FALSE, FALSE, info->proxy_dest->proxy_drop_time);
3452 gdk_drag_status (info->proxy_dest->context,
3453 event->dnd.context->action,
3458 else if (info->have_grab)
3460 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
3461 event->dnd.context->action,
3463 if (info->cursor != cursor)
3467 pointer = gdk_drag_context_get_device (context);
3468 gdk_device_grab (pointer, gtk_widget_get_window (widget),
3469 GDK_OWNERSHIP_APPLICATION, FALSE,
3470 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3471 cursor, info->grab_time);
3472 info->cursor = cursor;
3475 gtk_drag_add_update_idle (info);
3480 case GDK_DROP_FINISHED:
3481 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, event->dnd.time);
3484 g_assert_not_reached ();
3488 /*************************************************************
3489 * gtk_drag_source_check_selection:
3490 * Check if we've set up handlers/claimed the selection
3491 * for a given drag. If not, add them.
3495 *************************************************************/
3498 gtk_drag_source_check_selection (GtkDragSourceInfo *info,
3504 tmp_list = info->selections;
3507 if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3509 tmp_list = tmp_list->next;
3512 gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3516 info->selections = g_list_prepend (info->selections,
3517 GUINT_TO_POINTER (selection));
3519 tmp_list = info->target_list->list;
3522 GtkTargetPair *pair = tmp_list->data;
3524 gtk_selection_add_target (info->ipc_widget,
3528 tmp_list = tmp_list->next;
3531 if (info->context->protocol == GDK_DRAG_PROTO_MOTIF)
3533 gtk_selection_add_target (info->ipc_widget,
3535 gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS"),
3536 TARGET_MOTIF_SUCCESS);
3537 gtk_selection_add_target (info->ipc_widget,
3539 gdk_atom_intern_static_string ("XmTRANSFER_FAILURE"),
3540 TARGET_MOTIF_FAILURE);
3543 gtk_selection_add_target (info->ipc_widget,
3545 gdk_atom_intern_static_string ("DELETE"),
3549 /*************************************************************
3550 * gtk_drag_drop_finished:
3551 * Clean up from the drag, and display snapback, if necessary.
3557 *************************************************************/
3560 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3561 GtkDragResult result,
3566 success = (result == GTK_DRAG_RESULT_SUCCESS);
3567 gtk_drag_source_release_selections (info, time);
3569 if (info->proxy_dest)
3571 /* The time from the event isn't reliable for Xdnd drags */
3572 gtk_drag_finish (info->proxy_dest->context, success, FALSE,
3573 info->proxy_dest->proxy_drop_time);
3574 gtk_drag_source_info_destroy (info);
3579 g_signal_emit_by_name (info->widget, "drag-failed",
3580 info->context, result, &success);
3584 gtk_drag_source_info_destroy (info);
3588 GtkDragAnim *anim = g_slice_new0 (GtkDragAnim);
3592 anim->n_steps = MAX (info->cur_x - info->start_x,
3593 info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3594 anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3596 info->cur_screen = gtk_widget_get_screen (info->widget);
3598 if (!info->icon_window)
3599 set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf,
3602 gtk_drag_update_icon (info);
3604 /* Mark the context as dead, so if the destination decides
3605 * to respond really late, we still are OK.
3607 gtk_drag_clear_source_info (info->context);
3608 gdk_threads_add_timeout (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3614 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3617 GdkDisplay *display = gtk_widget_get_display (info->widget);
3618 GList *tmp_list = info->selections;
3622 GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3623 if (gdk_selection_owner_get_for_display (display, selection) == gtk_widget_get_window (info->ipc_widget))
3624 gtk_selection_owner_set_for_display (display, NULL, selection, time);
3626 tmp_list = tmp_list->next;
3629 g_list_free (info->selections);
3630 info->selections = NULL;
3633 /*************************************************************
3635 * Send a drop event.
3639 *************************************************************/
3642 gtk_drag_drop (GtkDragSourceInfo *info,
3645 if (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN)
3647 GtkSelectionData selection_data;
3649 /* GTK+ traditionally has used application/x-rootwin-drop, but the
3650 * XDND spec specifies x-rootwindow-drop.
3652 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3653 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3655 tmp_list = info->target_list->list;
3658 GtkTargetPair *pair = tmp_list->data;
3660 if (pair->target == target1 || pair->target == target2)
3662 selection_data.selection = GDK_NONE;
3663 selection_data.target = pair->target;
3664 selection_data.data = NULL;
3665 selection_data.length = -1;
3667 g_signal_emit_by_name (info->widget, "drag-data-get",
3668 info->context, &selection_data,
3672 /* FIXME: Should we check for length >= 0 here? */
3673 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3676 tmp_list = tmp_list->next;
3678 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3682 if (info->icon_window)
3683 gtk_widget_hide (info->icon_window);
3685 gdk_drag_drop (info->context, time);
3686 info->drop_timeout = gdk_threads_add_timeout (DROP_ABORT_TIME,
3687 gtk_drag_abort_timeout,
3693 * Source side callbacks.
3697 gtk_drag_source_event_cb (GtkWidget *widget,
3701 GtkDragSourceSite *site;
3702 gboolean retval = FALSE;
3703 site = (GtkDragSourceSite *)data;
3705 switch (event->type)
3707 case GDK_BUTTON_PRESS:
3708 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3710 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3711 site->x = event->button.x;
3712 site->y = event->button.y;
3716 case GDK_BUTTON_RELEASE:
3717 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3718 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3721 case GDK_MOTION_NOTIFY:
3722 if (site->state & event->motion.state & site->start_button_mask)
3724 /* FIXME: This is really broken and can leave us
3730 if (site->state & event->motion.state &
3731 GDK_BUTTON1_MASK << (i - 1))
3735 if (gtk_drag_check_threshold (widget, site->x, site->y,
3736 event->motion.x, event->motion.y))
3739 gtk_drag_begin_internal (widget, site, site->target_list,
3748 default: /* hit for 2/3BUTTON_PRESS */
3756 gtk_drag_source_site_destroy (gpointer data)
3758 GtkDragSourceSite *site = data;
3760 if (site->target_list)
3761 gtk_target_list_unref (site->target_list);
3763 gtk_drag_source_unset_icon (site);
3764 g_slice_free (GtkDragSourceSite, site);
3768 gtk_drag_selection_get (GtkWidget *widget,
3769 GtkSelectionData *selection_data,
3774 GtkDragSourceInfo *info = data;
3775 static GdkAtom null_atom = GDK_NONE;
3779 null_atom = gdk_atom_intern_static_string ("NULL");
3784 g_signal_emit_by_name (info->widget,
3787 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3789 case TARGET_MOTIF_SUCCESS:
3790 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3791 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3793 case TARGET_MOTIF_FAILURE:
3794 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3795 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3798 if (info->proxy_dest)
3800 /* This is sort of dangerous and needs to be thought
3803 info->proxy_dest->proxy_data = selection_data;
3804 gtk_drag_get_data (info->widget,
3805 info->proxy_dest->context,
3806 selection_data->target,
3809 info->proxy_dest->proxy_data = NULL;
3813 if (gtk_target_list_find (info->target_list,
3814 selection_data->target,
3817 g_signal_emit_by_name (info->widget, "drag-data-get",
3829 gtk_drag_anim_timeout (gpointer data)
3832 GtkDragSourceInfo *info;
3839 if (anim->step == anim->n_steps)
3841 gtk_drag_source_info_destroy (anim->info);
3842 g_slice_free (GtkDragAnim, anim);
3848 x = (info->start_x * (anim->step + 1) +
3849 info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3850 y = (info->start_y * (anim->step + 1) +
3851 info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3852 if (info->icon_window)
3854 GtkWidget *icon_window;
3857 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
3858 gtk_window_move (GTK_WINDOW (icon_window),
3872 gtk_drag_remove_icon (GtkDragSourceInfo *info)
3874 if (info->icon_window)
3876 gtk_widget_hide (info->icon_window);
3877 if (info->destroy_icon)
3878 gtk_widget_destroy (info->icon_window);
3880 if (info->fallback_icon)
3882 gtk_widget_destroy (info->fallback_icon);
3883 info->fallback_icon = NULL;
3886 g_object_unref (info->icon_window);
3887 info->icon_window = NULL;
3892 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
3896 for (i = 0; i < n_drag_cursors; i++)
3898 if (info->drag_cursors[i] != NULL)
3900 gdk_cursor_unref (info->drag_cursors[i]);
3901 info->drag_cursors[i] = NULL;
3905 gtk_drag_remove_icon (info);
3907 if (info->icon_pixbuf)
3909 g_object_unref (info->icon_pixbuf);
3910 info->icon_pixbuf = NULL;
3913 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3914 gtk_drag_grab_broken_event_cb,
3916 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3917 gtk_drag_grab_notify_cb,
3919 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3920 gtk_drag_button_release_cb,
3922 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3925 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3928 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3929 gtk_drag_selection_get,
3932 if (!info->proxy_dest)
3933 g_signal_emit_by_name (info->widget, "drag-end", info->context);
3936 g_object_unref (info->widget);
3938 gtk_selection_remove_all (info->ipc_widget);
3939 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), NULL);
3940 source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
3941 gtk_drag_release_ipc_widget (info->ipc_widget);
3943 gtk_target_list_unref (info->target_list);
3945 gtk_drag_clear_source_info (info->context);
3946 g_object_unref (info->context);
3948 if (info->drop_timeout)
3949 g_source_remove (info->drop_timeout);
3951 if (info->update_idle)
3952 g_source_remove (info->update_idle);
3958 gtk_drag_update_idle (gpointer data)
3960 GtkDragSourceInfo *info = data;
3961 GdkWindow *dest_window;
3962 GdkDragProtocol protocol;
3965 GdkDragAction action;
3966 GdkDragAction possible_actions;
3969 info->update_idle = 0;
3971 if (info->last_event)
3973 time = gtk_drag_get_event_time (info->last_event);
3974 gtk_drag_get_event_actions (info->last_event,
3976 info->possible_actions,
3977 &action, &possible_actions);
3978 gtk_drag_update_icon (info);
3979 gdk_drag_find_window_for_screen (info->context,
3980 info->icon_window ? gtk_widget_get_window (info->icon_window) : NULL,
3981 info->cur_screen, info->cur_x, info->cur_y,
3982 &dest_window, &protocol);
3984 if (!gdk_drag_motion (info->context, dest_window, protocol,
3985 info->cur_x, info->cur_y, action,
3989 gdk_event_free ((GdkEvent *)info->last_event);
3990 info->last_event = NULL;
3994 g_object_unref (dest_window);
3996 selection = gdk_drag_get_selection (info->context);
3998 gtk_drag_source_check_selection (info, selection, time);
4006 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
4008 /* We use an idle lower than GDK_PRIORITY_REDRAW so that exposes
4009 * from the last move can catch up before we move again.
4011 if (!info->update_idle)
4012 info->update_idle = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW + 5,
4013 gtk_drag_update_idle,
4020 * @info: DragSourceInfo for the drag
4021 * @screen: new screen
4022 * @x_root: new X position
4023 * @y_root: new y position
4024 * @event: event received requiring update
4026 * Updates the status of the drag; called when the
4027 * cursor moves or the modifier changes
4030 gtk_drag_update (GtkDragSourceInfo *info,
4036 info->cur_screen = screen;
4037 info->cur_x = x_root;
4038 info->cur_y = y_root;
4039 if (info->last_event)
4041 gdk_event_free ((GdkEvent *)info->last_event);
4042 info->last_event = NULL;
4045 info->last_event = gdk_event_copy ((GdkEvent *)event);
4047 gtk_drag_add_update_idle (info);
4050 /*************************************************************
4052 * Called when the user finishes to drag, either by
4053 * releasing the mouse, or by pressing Esc.
4055 * info: Source info for the drag
4056 * time: Timestamp for ending the drag
4058 *************************************************************/
4061 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
4063 GdkEvent *send_event;
4064 GtkWidget *source_widget = info->widget;
4065 GdkDevice *pointer, *keyboard;
4067 pointer = gdk_drag_context_get_device (info->context);
4068 keyboard = gdk_device_get_associated_device (pointer);
4070 /* Prevent ungrab before grab (see bug 623865) */
4071 if (info->grab_time == GDK_CURRENT_TIME)
4072 time = GDK_CURRENT_TIME;
4074 if (info->update_idle)
4076 g_source_remove (info->update_idle);
4077 info->update_idle = 0;
4080 if (info->last_event)
4082 gdk_event_free (info->last_event);
4083 info->last_event = NULL;
4086 info->have_grab = FALSE;
4088 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4089 gtk_drag_grab_broken_event_cb,
4091 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4092 gtk_drag_grab_notify_cb,
4094 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4095 gtk_drag_button_release_cb,
4097 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4100 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4104 gdk_device_ungrab (pointer, time);
4105 ungrab_dnd_keys (info->ipc_widget, keyboard, time);
4106 gtk_device_grab_remove (info->ipc_widget, pointer);
4108 /* Send on a release pair to the original
4109 * widget to convince it to release its grab. We need to
4110 * call gtk_propagate_event() here, instead of
4111 * gtk_widget_event() because widget like GtkList may
4112 * expect propagation.
4115 send_event = gdk_event_new (GDK_BUTTON_RELEASE);
4116 send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
4117 send_event->button.send_event = TRUE;
4118 send_event->button.time = time;
4119 send_event->button.x = 0;
4120 send_event->button.y = 0;
4121 send_event->button.axes = NULL;
4122 send_event->button.state = 0;
4123 send_event->button.button = info->button;
4124 send_event->button.device = pointer;
4125 send_event->button.x_root = 0;
4126 send_event->button.y_root = 0;
4128 gtk_propagate_event (source_widget, send_event);
4129 gdk_event_free (send_event);
4132 /*************************************************************
4134 * Called on cancellation of a drag, either by the user
4135 * or programmatically.
4137 * info: Source info for the drag
4138 * time: Timestamp for ending the drag
4140 *************************************************************/
4143 gtk_drag_cancel (GtkDragSourceInfo *info, GtkDragResult result, guint32 time)
4145 gtk_drag_end (info, time);
4146 gdk_drag_abort (info->context, time);
4147 gtk_drag_drop_finished (info, result, time);
4150 /*************************************************************
4151 * gtk_drag_motion_cb:
4152 * "motion-notify-event" callback during drag.
4156 *************************************************************/
4159 gtk_drag_motion_cb (GtkWidget *widget,
4160 GdkEventMotion *event,
4163 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4165 gint x_root, y_root;
4169 GdkDisplay *display = gtk_widget_get_display (widget);
4171 gdk_display_get_device_state (display, event->device,
4172 &screen, &x_root, &y_root, NULL);
4173 event->x_root = x_root;
4174 event->y_root = y_root;
4177 screen = gdk_event_get_screen ((GdkEvent *)event);
4179 gtk_drag_update (info, screen, event->x_root, event->y_root, (GdkEvent *) event);
4184 /*************************************************************
4186 * "key-press/release-event" callback during drag.
4190 *************************************************************/
4193 #define SMALL_STEP 1
4196 gtk_drag_key_cb (GtkWidget *widget,
4200 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4201 GdkModifierType state;
4202 GdkWindow *root_window;
4207 state = event->state & gtk_accelerator_get_default_mod_mask ();
4208 pointer = gdk_device_get_associated_device (gdk_event_get_device ((GdkEvent *) event));
4210 if (event->type == GDK_KEY_PRESS)
4212 switch (event->keyval)
4214 case GDK_KEY_Escape:
4215 gtk_drag_cancel (info, GTK_DRAG_RESULT_USER_CANCELLED, event->time);
4219 case GDK_KEY_Return:
4220 case GDK_KEY_ISO_Enter:
4221 case GDK_KEY_KP_Enter:
4222 case GDK_KEY_KP_Space:
4223 gtk_drag_end (info, event->time);
4224 gtk_drag_drop (info, event->time);
4229 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4233 case GDK_KEY_KP_Down:
4234 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4238 case GDK_KEY_KP_Left:
4239 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4243 case GDK_KEY_KP_Right:
4244 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4250 /* Now send a "motion" so that the modifier state is updated */
4252 /* The state is not yet updated in the event, so we need
4253 * to query it here. We could use XGetModifierMapping, but
4254 * that would be overkill.
4256 root_window = gtk_widget_get_root_window (widget);
4257 gdk_window_get_device_position (root_window, pointer, NULL, NULL, &state);
4258 event->state = state;
4260 if (dx != 0 || dy != 0)
4264 gdk_display_warp_device (gtk_widget_get_display (widget), pointer,
4265 gtk_widget_get_screen (widget),
4266 info->cur_x, info->cur_y);
4269 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
4275 gtk_drag_grab_broken_event_cb (GtkWidget *widget,
4276 GdkEventGrabBroken *event,
4279 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4281 /* Don't cancel if we break the implicit grab from the initial button_press.
4282 * Also, don't cancel if we re-grab on the widget or on our IPC window, for
4283 * example, when changing the drag cursor.
4286 || event->grab_window == gtk_widget_get_window (info->widget)
4287 || event->grab_window == gtk_widget_get_window (info->ipc_widget))
4290 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4295 gtk_drag_grab_notify_cb (GtkWidget *widget,
4296 gboolean was_grabbed,
4299 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4302 pointer = gdk_drag_context_get_device (info->context);
4304 if (gtk_widget_device_is_shadowed (widget, pointer))
4306 /* We have to block callbacks to avoid recursion here, because
4307 gtk_drag_cancel calls gtk_grab_remove (via gtk_drag_end) */
4308 g_signal_handlers_block_by_func (widget, gtk_drag_grab_notify_cb, data);
4309 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4310 g_signal_handlers_unblock_by_func (widget, gtk_drag_grab_notify_cb, data);
4315 /*************************************************************
4316 * gtk_drag_button_release_cb:
4317 * "button-release-event" callback during drag.
4321 *************************************************************/
4324 gtk_drag_button_release_cb (GtkWidget *widget,
4325 GdkEventButton *event,
4328 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4330 if (event->button != info->button)
4333 if ((info->context->action != 0) && (info->context->dest_window != NULL))
4335 gtk_drag_end (info, event->time);
4336 gtk_drag_drop (info, event->time);
4340 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4347 gtk_drag_abort_timeout (gpointer data)
4349 GtkDragSourceInfo *info = data;
4350 guint32 time = GDK_CURRENT_TIME;
4352 if (info->proxy_dest)
4353 time = info->proxy_dest->proxy_drop_time;
4355 info->drop_timeout = 0;
4356 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_TIMEOUT_EXPIRED, time);
4362 * gtk_drag_check_threshold:
4363 * @widget: a #GtkWidget
4364 * @start_x: X coordinate of start of drag
4365 * @start_y: Y coordinate of start of drag
4366 * @current_x: current X coordinate
4367 * @current_y: current Y coordinate
4369 * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
4370 * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
4371 * should trigger the beginning of a drag-and-drop operation.
4373 * Return Value: %TRUE if the drag threshold has been passed.
4376 gtk_drag_check_threshold (GtkWidget *widget,
4382 gint drag_threshold;
4384 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4386 g_object_get (gtk_widget_get_settings (widget),
4387 "gtk-dnd-drag-threshold", &drag_threshold,
4390 return (ABS (current_x - start_x) > drag_threshold ||
4391 ABS (current_y - start_y) > drag_threshold);