1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gdkconfig.h"
34 #include "gdk/gdkkeysyms.h"
36 #ifdef GDK_WINDOWING_X11
38 #include <X11/keysym.h>
39 #include "gdk/x11/gdkx.h"
43 #include "gtkiconfactory.h"
44 #include "gtkicontheme.h"
46 #include "gtkinvisible.h"
50 #include "gtktooltip.h"
51 #include "gtkwindow.h"
53 #include "gtkdndcursors.h"
55 static GSList *source_widgets = NULL;
57 typedef struct _GtkDragSourceSite GtkDragSourceSite;
58 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
59 typedef struct _GtkDragDestSite GtkDragDestSite;
60 typedef struct _GtkDragDestInfo GtkDragDestInfo;
61 typedef struct _GtkDragAnim GtkDragAnim;
71 struct _GtkDragSourceSite
73 GdkModifierType start_button_mask;
74 GtkTargetList *target_list; /* Targets for drag data */
75 GdkDragAction actions; /* Possible actions */
78 GtkImageType icon_type;
81 GtkImagePixmapData pixmap;
82 GtkImagePixbufData pixbuf;
83 GtkImageStockData stock;
84 GtkImageIconNameData name;
88 GdkColormap *colormap; /* Colormap for drag icon */
90 /* Stored button press information to detect drag beginning */
95 struct _GtkDragSourceInfo
98 GtkTargetList *target_list; /* Targets for drag data */
99 GdkDragAction possible_actions; /* Actions allowed by source */
100 GdkDragContext *context; /* drag context */
101 GtkWidget *icon_window; /* Window for drag */
102 GtkWidget *fallback_icon; /* Window for drag used on other screens */
103 GtkWidget *ipc_widget; /* GtkInvisible for grab, message passing */
104 GdkCursor *cursor; /* Cursor for drag */
105 gint hot_x, hot_y; /* Hot spot for drag */
106 gint button; /* mouse button starting drag */
108 GtkDragStatus status; /* drag status */
109 GdkEvent *last_event; /* pending event */
111 gint start_x, start_y; /* Initial position */
112 gint cur_x, cur_y; /* Current Position */
113 GdkScreen *cur_screen; /* Current screen for pointer */
115 guint32 grab_time; /* timestamp for initial grab */
116 GList *selections; /* selections we've claimed */
118 GtkDragDestInfo *proxy_dest; /* Set if this is a proxy drag */
120 guint update_idle; /* Idle function to update the drag */
121 guint drop_timeout; /* Timeout for aborting drop */
122 guint destroy_icon : 1; /* If true, destroy icon_window
124 guint have_grab : 1; /* Do we still have the pointer grab
126 GdkPixbuf *icon_pixbuf;
127 GdkCursor *drag_cursors[6];
130 struct _GtkDragDestSite
132 GtkDestDefaults flags;
133 GtkTargetList *target_list;
134 GdkDragAction actions;
135 GdkWindow *proxy_window;
136 GdkDragProtocol proxy_protocol;
138 guint proxy_coords : 1;
140 guint track_motion : 1;
143 struct _GtkDragDestInfo
145 GtkWidget *widget; /* Widget in which drag is in */
146 GdkDragContext *context; /* Drag context */
147 GtkDragSourceInfo *proxy_source; /* Set if this is a proxy drag */
148 GtkSelectionData *proxy_data; /* Set while retrieving proxied data */
149 guint dropped : 1; /* Set after we receive a drop */
150 guint32 proxy_drop_time; /* Timestamp for proxied drop */
151 guint proxy_drop_wait : 1; /* Set if we are waiting for a
152 * status reply before sending
155 gint drop_x, drop_y; /* Position of drop */
158 #define DROP_ABORT_TIME 300000
160 #define ANIM_STEP_TIME 50
161 #define ANIM_STEP_LENGTH 50
162 #define ANIM_MIN_STEPS 5
163 #define ANIM_MAX_STEPS 10
167 GtkDragSourceInfo *info;
172 typedef gboolean (* GtkDragDestCallback) (GtkWidget *widget,
173 GdkDragContext *context,
178 /* Enumeration for some targets we handle internally */
181 TARGET_MOTIF_SUCCESS = 0x40000000,
182 TARGET_MOTIF_FAILURE,
186 /* Forward declarations */
187 static void gtk_drag_get_event_actions (GdkEvent *event,
189 GdkDragAction actions,
190 GdkDragAction *suggested_action,
191 GdkDragAction *possible_actions);
192 static GdkCursor * gtk_drag_get_cursor (GdkDisplay *display,
193 GdkDragAction action,
194 GtkDragSourceInfo *info);
195 static void gtk_drag_update_cursor (GtkDragSourceInfo *info);
196 static GtkWidget *gtk_drag_get_ipc_widget (GtkWidget *widget);
197 static GtkWidget *gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen);
198 static void gtk_drag_release_ipc_widget (GtkWidget *widget);
200 static gboolean gtk_drag_highlight_expose (GtkWidget *widget,
201 GdkEventExpose *event,
204 static void gtk_drag_selection_received (GtkWidget *widget,
205 GtkSelectionData *selection_data,
208 static gboolean gtk_drag_find_widget (GtkWidget *widget,
209 GdkDragContext *context,
210 GtkDragDestInfo *info,
214 GtkDragDestCallback callback);
215 static void gtk_drag_proxy_begin (GtkWidget *widget,
216 GtkDragDestInfo *dest_info,
218 static void gtk_drag_dest_realized (GtkWidget *widget);
219 static void gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
220 GtkWidget *previous_toplevel);
221 static void gtk_drag_dest_site_destroy (gpointer data);
222 static void gtk_drag_dest_leave (GtkWidget *widget,
223 GdkDragContext *context,
225 static gboolean gtk_drag_dest_motion (GtkWidget *widget,
226 GdkDragContext *context,
230 static gboolean gtk_drag_dest_drop (GtkWidget *widget,
231 GdkDragContext *context,
236 static GtkDragDestInfo * gtk_drag_get_dest_info (GdkDragContext *context,
238 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
240 static void gtk_drag_clear_source_info (GdkDragContext *context);
242 static void gtk_drag_source_check_selection (GtkDragSourceInfo *info,
245 static void gtk_drag_source_release_selections (GtkDragSourceInfo *info,
247 static void gtk_drag_drop (GtkDragSourceInfo *info,
249 static void gtk_drag_drop_finished (GtkDragSourceInfo *info,
250 GtkDragResult result,
252 static void gtk_drag_cancel (GtkDragSourceInfo *info,
253 GtkDragResult result,
256 static gboolean gtk_drag_source_event_cb (GtkWidget *widget,
259 static void gtk_drag_source_site_destroy (gpointer data);
260 static void gtk_drag_selection_get (GtkWidget *widget,
261 GtkSelectionData *selection_data,
265 static gboolean gtk_drag_anim_timeout (gpointer data);
266 static void gtk_drag_remove_icon (GtkDragSourceInfo *info);
267 static void gtk_drag_source_info_destroy (GtkDragSourceInfo *info);
268 static void gtk_drag_add_update_idle (GtkDragSourceInfo *info);
270 static void gtk_drag_update (GtkDragSourceInfo *info,
275 static gboolean gtk_drag_motion_cb (GtkWidget *widget,
276 GdkEventMotion *event,
278 static gboolean gtk_drag_key_cb (GtkWidget *widget,
281 static gboolean gtk_drag_grab_broken_event_cb (GtkWidget *widget,
282 GdkEventGrabBroken *event,
284 static void gtk_drag_grab_notify_cb (GtkWidget *widget,
285 gboolean was_grabbed,
287 static gboolean gtk_drag_button_release_cb (GtkWidget *widget,
288 GdkEventButton *event,
290 static gboolean gtk_drag_abort_timeout (gpointer data);
292 static void set_icon_stock_pixbuf (GdkDragContext *context,
293 const gchar *stock_id,
297 gboolean force_window);
299 /************************
300 * Cursor and Icon data *
301 ************************/
304 GdkDragAction action;
310 { GDK_ACTION_DEFAULT, NULL },
311 { GDK_ACTION_ASK, "dnd-ask", dnd_cursor_ask, NULL, NULL },
312 { GDK_ACTION_COPY, "dnd-copy", dnd_cursor_copy, NULL, NULL },
313 { GDK_ACTION_MOVE, "dnd-move", dnd_cursor_move, NULL, NULL },
314 { GDK_ACTION_LINK, "dnd-link", dnd_cursor_link, NULL, NULL },
315 { 0 , "dnd-none", dnd_cursor_none, NULL, NULL },
318 static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
320 /*********************
321 * Utility functions *
322 *********************/
325 set_can_change_screen (GtkWidget *widget,
326 gboolean can_change_screen)
328 can_change_screen = can_change_screen != FALSE;
330 g_object_set_data (G_OBJECT (widget), I_("gtk-dnd-can-change-screen"),
331 GUINT_TO_POINTER (can_change_screen));
335 get_can_change_screen (GtkWidget *widget)
337 return g_object_get_data (G_OBJECT (widget), "gtk-dnd-can-change-screen") != NULL;
342 gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen)
345 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
346 "gtk-dnd-ipc-widgets");
350 GSList *tmp = drag_widgets;
351 result = drag_widgets->data;
352 drag_widgets = drag_widgets->next;
353 g_object_set_data (G_OBJECT (screen),
354 I_("gtk-dnd-ipc-widgets"),
356 g_slist_free_1 (tmp);
360 result = gtk_window_new (GTK_WINDOW_POPUP);
361 gtk_window_set_screen (GTK_WINDOW (result), screen);
362 gtk_window_resize (GTK_WINDOW (result), 1, 1);
363 gtk_window_move (GTK_WINDOW (result), -100, -100);
364 gtk_widget_show (result);
371 gtk_drag_get_ipc_widget (GtkWidget *widget)
376 result = gtk_drag_get_ipc_widget_for_screen (gtk_widget_get_screen (widget));
378 toplevel = gtk_widget_get_toplevel (widget);
380 if (GTK_IS_WINDOW (toplevel))
382 if (gtk_window_has_group (GTK_WINDOW (toplevel)))
383 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
384 GTK_WINDOW (result));
390 /* FIXME: modifying the XEvent window as in root_key_filter() isn't
391 * going to work with XGE/XI2, since the actual event to handle would
392 * be allocated/freed before GDK gets to translate the event.
393 * Active grabs on the keyboard are used instead at the moment...
395 #if defined (GDK_WINDOWING_X11) && !defined (XINPUT_2)
398 * We want to handle a handful of keys during DND, e.g. Escape to abort.
399 * Grabbing the keyboard has the unfortunate side-effect of preventing
400 * useful things such as using Alt-Tab to cycle between windows or
401 * switching workspaces. Therefore, we just grab the few keys we are
402 * interested in. Note that we need to put the grabs on the root window
403 * in order for them to still work when the focus is moved to another
406 * GDK needs a little help to successfully deliver root key events...
409 static GdkFilterReturn
410 root_key_filter (GdkXEvent *xevent,
414 XEvent *ev = (XEvent *) xevent;
416 if ((ev->type == KeyPress || ev->type == KeyRelease) &&
417 ev->xkey.root == ev->xkey.window)
418 ev->xkey.window = (Window)data;
420 return GDK_FILTER_CONTINUE;
428 static GrabKey grab_keys[] = {
437 { XK_Down, Mod1Mask },
439 { XK_Left, Mod1Mask },
441 { XK_Right, Mod1Mask },
443 { XK_KP_Up, Mod1Mask },
445 { XK_KP_Down, Mod1Mask },
447 { XK_KP_Left, Mod1Mask },
449 { XK_KP_Right, Mod1Mask }
453 grab_dnd_keys (GtkWidget *widget,
458 GdkWindow *window, *root;
461 window = gtk_widget_get_window (widget);
462 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
464 gdk_error_trap_push ();
466 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
468 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
469 XGrabKey (GDK_WINDOW_XDISPLAY (window),
470 keycode, grab_keys[i].modifiers,
471 GDK_WINDOW_XID (root),
478 gdk_error_trap_pop ();
480 gdk_window_add_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
484 ungrab_dnd_keys (GtkWidget *widget,
489 GdkWindow *window, *root;
492 window = gtk_widget_get_window (widget);
493 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
495 gdk_window_remove_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
497 gdk_error_trap_push ();
499 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
501 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
502 XUngrabKey (GDK_WINDOW_XDISPLAY (window),
503 keycode, grab_keys[i].modifiers,
504 GDK_WINDOW_XID (root));
508 gdk_error_trap_pop ();
511 #else /* GDK_WINDOWING_X11 && !XINPUT_2 */
514 grab_dnd_keys (GtkWidget *widget,
518 gdk_device_grab (device,
519 gtk_widget_get_window (widget),
520 GDK_OWNERSHIP_APPLICATION, FALSE,
521 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
526 ungrab_dnd_keys (GtkWidget *widget,
530 gdk_device_ungrab (device, time);
533 #endif /* GDK_WINDOWING_X11 */
536 /***************************************************************
537 * gtk_drag_release_ipc_widget:
538 * Releases widget retrieved with gtk_drag_get_ipc_widget ()
540 * widget: the widget to release.
542 ***************************************************************/
545 gtk_drag_release_ipc_widget (GtkWidget *widget)
547 GtkWindow *window = GTK_WINDOW (widget);
548 GdkScreen *screen = gtk_widget_get_screen (widget);
549 GdkDragContext *context = g_object_get_data (G_OBJECT (widget), "drag-context");
550 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
551 "gtk-dnd-ipc-widgets");
552 GdkDevice *pointer, *keyboard;
556 pointer = gdk_drag_context_get_device (context);
557 keyboard = gdk_device_get_associated_device (pointer);
560 ungrab_dnd_keys (widget, keyboard, GDK_CURRENT_TIME);
563 if (gtk_window_has_group (window))
564 gtk_window_group_remove_window (gtk_window_get_group (window),
566 drag_widgets = g_slist_prepend (drag_widgets, widget);
567 g_object_set_data (G_OBJECT (screen),
568 I_("gtk-dnd-ipc-widgets"),
573 gtk_drag_get_event_time (GdkEvent *event)
575 guint32 tm = GDK_CURRENT_TIME;
580 case GDK_MOTION_NOTIFY:
581 tm = event->motion.time; break;
582 case GDK_BUTTON_PRESS:
583 case GDK_2BUTTON_PRESS:
584 case GDK_3BUTTON_PRESS:
585 case GDK_BUTTON_RELEASE:
586 tm = event->button.time; break;
588 case GDK_KEY_RELEASE:
589 tm = event->key.time; break;
590 case GDK_ENTER_NOTIFY:
591 case GDK_LEAVE_NOTIFY:
592 tm = event->crossing.time; break;
593 case GDK_PROPERTY_NOTIFY:
594 tm = event->property.time; break;
595 case GDK_SELECTION_CLEAR:
596 case GDK_SELECTION_REQUEST:
597 case GDK_SELECTION_NOTIFY:
598 tm = event->selection.time; break;
599 case GDK_PROXIMITY_IN:
600 case GDK_PROXIMITY_OUT:
601 tm = event->proximity.time; break;
602 default: /* use current time */
610 gtk_drag_get_event_actions (GdkEvent *event,
612 GdkDragAction actions,
613 GdkDragAction *suggested_action,
614 GdkDragAction *possible_actions)
616 *suggested_action = 0;
617 *possible_actions = 0;
621 GdkModifierType state = 0;
625 case GDK_MOTION_NOTIFY:
626 state = event->motion.state;
628 case GDK_BUTTON_PRESS:
629 case GDK_2BUTTON_PRESS:
630 case GDK_3BUTTON_PRESS:
631 case GDK_BUTTON_RELEASE:
632 state = event->button.state;
635 case GDK_KEY_RELEASE:
636 state = event->key.state;
638 case GDK_ENTER_NOTIFY:
639 case GDK_LEAVE_NOTIFY:
640 state = event->crossing.state;
646 if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
648 *suggested_action = GDK_ACTION_ASK;
649 *possible_actions = actions;
651 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
653 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
655 if (actions & GDK_ACTION_LINK)
657 *suggested_action = GDK_ACTION_LINK;
658 *possible_actions = GDK_ACTION_LINK;
661 else if (state & GDK_CONTROL_MASK)
663 if (actions & GDK_ACTION_COPY)
665 *suggested_action = GDK_ACTION_COPY;
666 *possible_actions = GDK_ACTION_COPY;
672 if (actions & GDK_ACTION_MOVE)
674 *suggested_action = GDK_ACTION_MOVE;
675 *possible_actions = GDK_ACTION_MOVE;
682 *possible_actions = actions;
684 if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
685 *suggested_action = GDK_ACTION_ASK;
686 else if (actions & GDK_ACTION_COPY)
687 *suggested_action = GDK_ACTION_COPY;
688 else if (actions & GDK_ACTION_MOVE)
689 *suggested_action = GDK_ACTION_MOVE;
690 else if (actions & GDK_ACTION_LINK)
691 *suggested_action = GDK_ACTION_LINK;
696 *possible_actions = actions;
698 if (actions & GDK_ACTION_COPY)
699 *suggested_action = GDK_ACTION_COPY;
700 else if (actions & GDK_ACTION_MOVE)
701 *suggested_action = GDK_ACTION_MOVE;
702 else if (actions & GDK_ACTION_LINK)
703 *suggested_action = GDK_ACTION_LINK;
708 gtk_drag_can_use_rgba_cursor (GdkDisplay *display,
712 guint max_width, max_height;
714 if (!gdk_display_supports_cursor_color (display))
717 if (!gdk_display_supports_cursor_alpha (display))
720 gdk_display_get_maximal_cursor_size (display,
723 if (width > max_width || height > max_height)
725 /* can't use rgba cursor (too large) */
733 gtk_drag_get_cursor (GdkDisplay *display,
734 GdkDragAction action,
735 GtkDragSourceInfo *info)
739 /* reconstruct the cursors for each new drag (thus !info),
740 * to catch cursor theme changes
744 for (i = 0 ; i < n_drag_cursors - 1; i++)
745 if (drag_cursors[i].cursor != NULL)
747 gdk_cursor_unref (drag_cursors[i].cursor);
748 drag_cursors[i].cursor = NULL;
752 for (i = 0 ; i < n_drag_cursors - 1; i++)
753 if (drag_cursors[i].action == action)
756 if (drag_cursors[i].pixbuf == NULL)
757 drag_cursors[i].pixbuf =
758 gdk_pixbuf_new_from_inline (-1, drag_cursors[i].data, FALSE, NULL);
760 if (drag_cursors[i].cursor != NULL)
762 if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
764 gdk_cursor_unref (drag_cursors[i].cursor);
765 drag_cursors[i].cursor = NULL;
769 if (drag_cursors[i].cursor == NULL)
770 drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
772 if (drag_cursors[i].cursor == NULL)
773 drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);
775 if (info && info->icon_pixbuf)
777 gint cursor_width, cursor_height;
778 gint icon_width, icon_height;
780 GdkPixbuf *cursor_pixbuf, *pixbuf;
782 gint icon_x, icon_y, ref_x, ref_y;
784 if (info->drag_cursors[i] != NULL)
786 if (display == gdk_cursor_get_display (info->drag_cursors[i]))
787 return info->drag_cursors[i];
789 gdk_cursor_unref (info->drag_cursors[i]);
790 info->drag_cursors[i] = NULL;
793 icon_x = info->hot_x;
794 icon_y = info->hot_y;
795 icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
796 icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);
799 cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
801 cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
804 if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
805 hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
807 if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
808 hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
811 /* The code below is an attempt to let cursor themes
812 * determine the attachment of the icon to enable things
813 * like the following:
821 * It does not work since Xcursor doesn't allow to attach
822 * any additional information to cursors in a retrievable
823 * way (there are comments, but no way to get at them
824 * short of searching for the actual cursor file).
825 * If this code ever gets used, the icon_window placement
826 * must be changed to recognize these placement options
827 * as well. Note that this code ignores info->hot_x/y.
829 for (j = 0; j < 10; j++)
834 GtkAnchorType icon_anchor;
836 g_snprintf (key, 32, "comment%d", j);
837 opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
838 if (opt && g_str_has_prefix ("icon-attach:", opt))
840 toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
841 if (g_strv_length (toks) != 3)
846 icon_anchor = atoi (toks[0]);
847 icon_x = atoi (toks[1]);
848 icon_y = atoi (toks[2]);
852 case GTK_ANCHOR_NORTH:
853 case GTK_ANCHOR_CENTER:
854 case GTK_ANCHOR_SOUTH:
855 icon_x += icon_width / 2;
857 case GTK_ANCHOR_NORTH_EAST:
858 case GTK_ANCHOR_EAST:
859 case GTK_ANCHOR_SOUTH_EAST:
860 icon_x += icon_width;
867 case GTK_ANCHOR_WEST:
868 case GTK_ANCHOR_CENTER:
869 case GTK_ANCHOR_EAST:
870 icon_y += icon_height / 2;
872 case GTK_ANCHOR_SOUTH_WEST:
873 case GTK_ANCHOR_SOUTH:
874 case GTK_ANCHOR_SOUTH_EAST:
875 icon_x += icon_height;
887 cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
888 cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
890 ref_x = MAX (hot_x, icon_x);
891 ref_y = MAX (hot_y, icon_y);
892 width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
893 height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
895 if (gtk_drag_can_use_rgba_cursor (display, width, height))
897 /* Composite cursor and icon so that both hotspots
898 * end up at (ref_x, ref_y)
900 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
903 gdk_pixbuf_fill (pixbuf, 0xff000000);
905 gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
906 ref_x - icon_x, ref_y - icon_y,
907 icon_width, icon_height,
908 ref_x - icon_x, ref_y - icon_y,
910 GDK_INTERP_BILINEAR, 255);
912 gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
913 ref_x - hot_x, ref_y - hot_y,
914 cursor_width, cursor_height,
915 ref_x - hot_x, ref_y - hot_y,
917 GDK_INTERP_BILINEAR, 255);
919 info->drag_cursors[i] =
920 gdk_cursor_new_from_pixbuf (display, pixbuf, ref_x, ref_y);
922 g_object_unref (pixbuf);
925 g_object_unref (cursor_pixbuf);
927 if (info->drag_cursors[i] != NULL)
928 return info->drag_cursors[i];
931 return drag_cursors[i].cursor;
935 gtk_drag_update_cursor (GtkDragSourceInfo *info)
940 if (!info->have_grab)
943 for (i = 0 ; i < n_drag_cursors - 1; i++)
944 if (info->cursor == drag_cursors[i].cursor ||
945 info->cursor == info->drag_cursors[i])
948 if (i == n_drag_cursors)
951 cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor),
952 drag_cursors[i].action, info);
954 if (cursor != info->cursor)
958 pointer = gdk_drag_context_get_device (info->context);
959 gdk_device_grab (pointer,
960 gtk_widget_get_window (info->ipc_widget),
961 GDK_OWNERSHIP_APPLICATION, FALSE,
962 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
963 cursor, info->grab_time);
964 info->cursor = cursor;
968 /********************
970 ********************/
972 /*************************************************************
974 * Get the data for a drag or drop
976 * context - drag context
977 * target - format to retrieve the data in.
978 * time - timestamp of triggering event.
981 *************************************************************/
984 gtk_drag_get_data (GtkWidget *widget,
985 GdkDragContext *context,
989 GtkWidget *selection_widget;
991 g_return_if_fail (GTK_IS_WIDGET (widget));
992 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
993 g_return_if_fail (!context->is_source);
995 selection_widget = gtk_drag_get_ipc_widget (widget);
997 g_object_ref (context);
998 g_object_ref (widget);
1000 g_signal_connect (selection_widget, "selection-received",
1001 G_CALLBACK (gtk_drag_selection_received), widget);
1003 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1005 gtk_selection_convert (selection_widget,
1006 gdk_drag_get_selection (context),
1012 /*************************************************************
1013 * gtk_drag_get_source_widget:
1014 * Get the widget the was the source of this drag, if
1015 * the drag originated from this application.
1017 * context: The drag context for this drag
1019 * The source widget, or NULL if the drag originated from
1020 * a different application.
1021 *************************************************************/
1024 gtk_drag_get_source_widget (GdkDragContext *context)
1028 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
1029 g_return_val_if_fail (!context->is_source, NULL);
1031 tmp_list = source_widgets;
1034 GtkWidget *ipc_widget = tmp_list->data;
1036 if (gtk_widget_get_window (ipc_widget) == context->source_window)
1038 GtkDragSourceInfo *info;
1039 info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
1041 return info ? info->widget : NULL;
1044 tmp_list = tmp_list->next;
1050 /*************************************************************
1052 * Notify the drag source that the transfer of data
1055 * context: The drag context for this drag
1056 * success: Was the data successfully transferred?
1057 * time: The timestamp to use when notifying the destination.
1059 *************************************************************/
1062 gtk_drag_finish (GdkDragContext *context,
1067 GdkAtom target = GDK_NONE;
1069 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1070 g_return_if_fail (!context->is_source);
1074 target = gdk_atom_intern_static_string ("DELETE");
1076 else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
1078 target = gdk_atom_intern_static_string (success ?
1079 "XmTRANSFER_SUCCESS" :
1080 "XmTRANSFER_FAILURE");
1083 if (target != GDK_NONE)
1085 GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_drawable_get_screen (context->source_window));
1087 g_object_ref (context);
1089 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1090 g_signal_connect (selection_widget, "selection-received",
1091 G_CALLBACK (gtk_drag_selection_received),
1094 gtk_selection_convert (selection_widget,
1095 gdk_drag_get_selection (context),
1100 if (!(success && del))
1101 gdk_drop_finish (context, success, time);
1104 /*************************************************************
1105 * gtk_drag_highlight_expose:
1106 * Callback for expose_event for highlighted widgets.
1112 *************************************************************/
1115 gtk_drag_highlight_expose (GtkWidget *widget,
1116 GdkEventExpose *event,
1119 gint x, y, width, height;
1121 if (gtk_widget_is_drawable (widget))
1126 window = gtk_widget_get_window (widget);
1128 if (!gtk_widget_get_has_window (widget))
1130 GtkAllocation allocation;
1132 gtk_widget_get_allocation (widget, &allocation);
1135 width = allocation.width;
1136 height = allocation.height;
1142 gdk_drawable_get_size (window, &width, &height);
1145 gtk_paint_shadow (gtk_widget_get_style (widget), window,
1146 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1147 &event->area, widget, "dnd",
1148 x, y, width, height);
1150 cr = gdk_cairo_create (window);
1151 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
1152 cairo_set_line_width (cr, 1.0);
1153 cairo_rectangle (cr,
1155 width - 1, height - 1);
1163 /*************************************************************
1164 * gtk_drag_highlight:
1165 * Highlight the given widget in the default manner.
1169 *************************************************************/
1172 gtk_drag_highlight (GtkWidget *widget)
1174 g_return_if_fail (GTK_IS_WIDGET (widget));
1176 g_signal_connect_after (widget, "expose-event",
1177 G_CALLBACK (gtk_drag_highlight_expose),
1180 gtk_widget_queue_draw (widget);
1183 /*************************************************************
1184 * gtk_drag_unhighlight:
1185 * Refresh the given widget to remove the highlight.
1189 *************************************************************/
1192 gtk_drag_unhighlight (GtkWidget *widget)
1194 g_return_if_fail (GTK_IS_WIDGET (widget));
1196 g_signal_handlers_disconnect_by_func (widget,
1197 gtk_drag_highlight_expose,
1200 gtk_widget_queue_draw (widget);
1204 gtk_drag_dest_set_internal (GtkWidget *widget,
1205 GtkDragDestSite *site)
1207 GtkDragDestSite *old_site;
1209 g_return_if_fail (widget != NULL);
1211 /* HACK, do this in the destroy */
1212 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1215 g_signal_handlers_disconnect_by_func (widget,
1216 gtk_drag_dest_realized,
1218 g_signal_handlers_disconnect_by_func (widget,
1219 gtk_drag_dest_hierarchy_changed,
1222 site->track_motion = old_site->track_motion;
1225 if (gtk_widget_get_realized (widget))
1226 gtk_drag_dest_realized (widget);
1228 g_signal_connect (widget, "realize",
1229 G_CALLBACK (gtk_drag_dest_realized), site);
1230 g_signal_connect (widget, "hierarchy-changed",
1231 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1233 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
1234 site, gtk_drag_dest_site_destroy);
1238 * gtk_drag_dest_set:
1239 * @widget: a #GtkWidget
1240 * @flags: which types of default drag behavior to use
1241 * @targets: (allow-none) (array length=n_targets): a pointer to an array of #GtkTargetEntry<!-- -->s
1242 * indicating the drop types that this @widget will accept, or %NULL.
1243 * Later you can access the list with gtk_drag_dest_get_target_list()
1244 * and gtk_drag_dest_find_target().
1245 * @n_targets: the number of entries in @targets
1246 * @actions: a bitmask of possible actions for a drop onto this @widget.
1248 * Sets a widget as a potential drop destination, and adds default behaviors.
1250 * The default behaviors listed in @flags have an effect similar
1251 * to installing default handlers for the widget's drag-and-drop signals
1252 * (#GtkWidget:drag-motion, #GtkWidget:drag-drop, ...). They all exist
1253 * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
1254 * sufficient to connect to the widget's #GtkWidget::drag-data-received
1255 * signal to get primitive, but consistent drag-and-drop support.
1257 * Things become more complicated when you try to preview the dragged data,
1258 * as described in the documentation for #GtkWidget:drag-motion. The default
1259 * behaviors described by @flags make some assumptions, that can conflict
1260 * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
1261 * invokations of gdk_drag_status() in the context of #GtkWidget:drag-motion,
1262 * and invokations of gtk_drag_finish() in #GtkWidget:drag-data-received.
1263 * Especially the later is dramatic, when your own #GtkWidget:drag-motion
1264 * handler calls gtk_drag_get_data() to inspect the dragged data.
1266 * There's no way to set a default action here, you can use the
1267 * #GtkWidget:drag-motion callback for that. Here's an example which selects
1268 * the action to use depending on whether the control key is pressed or not:
1271 * drag_motion (GtkWidget *widget,
1272 * GdkDragContext *context,
1277 * GdkModifierType mask;
1279 * gdk_window_get_pointer (gtk_widget_get_window (widget),
1280 * NULL, NULL, &mask);
1281 * if (mask & GDK_CONTROL_MASK)
1282 * gdk_drag_status (context, GDK_ACTION_COPY, time);
1284 * gdk_drag_status (context, GDK_ACTION_MOVE, time);
1289 gtk_drag_dest_set (GtkWidget *widget,
1290 GtkDestDefaults flags,
1291 const GtkTargetEntry *targets,
1293 GdkDragAction actions)
1295 GtkDragDestSite *site;
1297 g_return_if_fail (GTK_IS_WIDGET (widget));
1299 site = g_new (GtkDragDestSite, 1);
1301 site->flags = flags;
1302 site->have_drag = FALSE;
1304 site->target_list = gtk_target_list_new (targets, n_targets);
1306 site->target_list = NULL;
1307 site->actions = actions;
1308 site->do_proxy = FALSE;
1309 site->proxy_window = NULL;
1310 site->track_motion = FALSE;
1312 gtk_drag_dest_set_internal (widget, site);
1315 /*************************************************************
1316 * gtk_drag_dest_set_proxy:
1317 * Set up this widget to proxy drags elsewhere.
1320 * proxy_window: window to which forward drag events
1321 * protocol: Drag protocol which the dest widget accepts
1322 * use_coordinates: If true, send the same coordinates to the
1323 * destination, because it is a embedded
1326 *************************************************************/
1329 gtk_drag_dest_set_proxy (GtkWidget *widget,
1330 GdkWindow *proxy_window,
1331 GdkDragProtocol protocol,
1332 gboolean use_coordinates)
1334 GtkDragDestSite *site;
1336 g_return_if_fail (GTK_IS_WIDGET (widget));
1337 g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1339 site = g_new (GtkDragDestSite, 1);
1342 site->have_drag = FALSE;
1343 site->target_list = NULL;
1345 site->proxy_window = proxy_window;
1347 g_object_ref (proxy_window);
1348 site->do_proxy = TRUE;
1349 site->proxy_protocol = protocol;
1350 site->proxy_coords = use_coordinates;
1351 site->track_motion = FALSE;
1353 gtk_drag_dest_set_internal (widget, site);
1356 /*************************************************************
1357 * gtk_drag_dest_unset
1358 * Unregister this widget as a drag target.
1362 *************************************************************/
1365 gtk_drag_dest_unset (GtkWidget *widget)
1367 GtkDragDestSite *old_site;
1369 g_return_if_fail (GTK_IS_WIDGET (widget));
1371 old_site = g_object_get_data (G_OBJECT (widget),
1375 g_signal_handlers_disconnect_by_func (widget,
1376 gtk_drag_dest_realized,
1378 g_signal_handlers_disconnect_by_func (widget,
1379 gtk_drag_dest_hierarchy_changed,
1383 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1387 * gtk_drag_dest_get_target_list:
1388 * @widget: a #GtkWidget
1390 * Returns the list of targets this widget can accept from
1393 * Return value: the #GtkTargetList, or %NULL if none
1396 gtk_drag_dest_get_target_list (GtkWidget *widget)
1398 GtkDragDestSite *site;
1400 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1402 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1404 return site ? site->target_list : NULL;
1408 * gtk_drag_dest_set_target_list:
1409 * @widget: a #GtkWidget that's a drag destination
1410 * @target_list: (allow-none): list of droppable targets, or %NULL for none
1412 * Sets the target types that this widget can accept from drag-and-drop.
1413 * The widget must first be made into a drag destination with
1414 * gtk_drag_dest_set().
1417 gtk_drag_dest_set_target_list (GtkWidget *widget,
1418 GtkTargetList *target_list)
1420 GtkDragDestSite *site;
1422 g_return_if_fail (GTK_IS_WIDGET (widget));
1424 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1428 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1429 "to make the widget into a drag destination");
1434 gtk_target_list_ref (target_list);
1436 if (site->target_list)
1437 gtk_target_list_unref (site->target_list);
1439 site->target_list = target_list;
1443 * gtk_drag_dest_add_text_targets:
1444 * @widget: a #GtkWidget that's a drag destination
1446 * Add the text targets supported by #GtkSelection to
1447 * the target list of the drag destination. The targets
1448 * are added with @info = 0. If you need another value,
1449 * use gtk_target_list_add_text_targets() and
1450 * gtk_drag_dest_set_target_list().
1455 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1457 GtkTargetList *target_list;
1459 target_list = gtk_drag_dest_get_target_list (widget);
1461 gtk_target_list_ref (target_list);
1463 target_list = gtk_target_list_new (NULL, 0);
1464 gtk_target_list_add_text_targets (target_list, 0);
1465 gtk_drag_dest_set_target_list (widget, target_list);
1466 gtk_target_list_unref (target_list);
1470 * gtk_drag_dest_add_image_targets:
1471 * @widget: a #GtkWidget that's a drag destination
1473 * Add the image targets supported by #GtkSelection to
1474 * the target list of the drag destination. The targets
1475 * are added with @info = 0. If you need another value,
1476 * use gtk_target_list_add_image_targets() and
1477 * gtk_drag_dest_set_target_list().
1482 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1484 GtkTargetList *target_list;
1486 target_list = gtk_drag_dest_get_target_list (widget);
1488 gtk_target_list_ref (target_list);
1490 target_list = gtk_target_list_new (NULL, 0);
1491 gtk_target_list_add_image_targets (target_list, 0, FALSE);
1492 gtk_drag_dest_set_target_list (widget, target_list);
1493 gtk_target_list_unref (target_list);
1497 * gtk_drag_dest_add_uri_targets:
1498 * @widget: a #GtkWidget that's a drag destination
1500 * Add the URI targets supported by #GtkSelection to
1501 * the target list of the drag destination. The targets
1502 * are added with @info = 0. If you need another value,
1503 * use gtk_target_list_add_uri_targets() and
1504 * gtk_drag_dest_set_target_list().
1509 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1511 GtkTargetList *target_list;
1513 target_list = gtk_drag_dest_get_target_list (widget);
1515 gtk_target_list_ref (target_list);
1517 target_list = gtk_target_list_new (NULL, 0);
1518 gtk_target_list_add_uri_targets (target_list, 0);
1519 gtk_drag_dest_set_target_list (widget, target_list);
1520 gtk_target_list_unref (target_list);
1524 * gtk_drag_dest_set_track_motion:
1525 * @widget: a #GtkWidget that's a drag destination
1526 * @track_motion: whether to accept all targets
1528 * Tells the widget to emit ::drag-motion and ::drag-leave
1529 * events regardless of the targets and the %GTK_DEST_DEFAULT_MOTION
1532 * This may be used when a widget wants to do generic
1533 * actions regardless of the targets that the source offers.
1538 gtk_drag_dest_set_track_motion (GtkWidget *widget,
1539 gboolean track_motion)
1541 GtkDragDestSite *site;
1543 g_return_if_fail (GTK_IS_WIDGET (widget));
1545 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1547 g_return_if_fail (site != NULL);
1549 site->track_motion = track_motion != FALSE;
1553 * gtk_drag_dest_get_track_motion:
1554 * @widget: a #GtkWidget that's a drag destination
1556 * Returns whether the widget has been configured to always
1557 * emit ::drag-motion signals.
1559 * Return Value: %TRUE if the widget always emits ::drag-motion events
1564 gtk_drag_dest_get_track_motion (GtkWidget *widget)
1566 GtkDragDestSite *site;
1568 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1570 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1573 return site->track_motion;
1578 /*************************************************************
1579 * _gtk_drag_dest_handle_event:
1580 * Called from widget event handling code on Drag events
1584 * toplevel: Toplevel widget that received the event
1587 *************************************************************/
1590 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1593 GtkDragDestInfo *info;
1594 GdkDragContext *context;
1596 g_return_if_fail (toplevel != NULL);
1597 g_return_if_fail (event != NULL);
1599 context = event->dnd.context;
1601 info = gtk_drag_get_dest_info (context, TRUE);
1603 /* Find the widget for the event */
1604 switch (event->type)
1606 case GDK_DRAG_ENTER:
1609 case GDK_DRAG_LEAVE:
1612 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1613 info->widget = NULL;
1617 case GDK_DRAG_MOTION:
1618 case GDK_DROP_START:
1624 if (event->type == GDK_DROP_START)
1626 info->dropped = TRUE;
1627 /* We send a leave here so that the widget unhighlights
1632 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1633 info->widget = NULL;
1637 window = gtk_widget_get_window (toplevel);
1639 #ifdef GDK_WINDOWING_X11
1640 /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1642 * Currently gdk_window_get_position doesn't provide reliable
1643 * information for embedded windows, so we call the much more
1644 * expensive gdk_window_get_origin().
1646 if (GTK_IS_PLUG (toplevel))
1647 gdk_window_get_origin (window, &tx, &ty);
1649 #endif /* GDK_WINDOWING_X11 */
1650 gdk_window_get_position (window, &tx, &ty);
1652 found = gtk_drag_find_widget (toplevel,
1655 event->dnd.x_root - tx,
1656 event->dnd.y_root - ty,
1658 (event->type == GDK_DRAG_MOTION) ?
1659 gtk_drag_dest_motion :
1660 gtk_drag_dest_drop);
1662 if (info->widget && !found)
1664 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1665 info->widget = NULL;
1670 if (event->type == GDK_DRAG_MOTION)
1673 gdk_drag_status (context, 0, event->dnd.time);
1675 else if (event->type == GDK_DROP_START && !info->proxy_source)
1677 gdk_drop_reply (context, found, event->dnd.time);
1678 if ((context->protocol == GDK_DRAG_PROTO_MOTIF) && !found)
1679 gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1685 g_assert_not_reached ();
1690 * gtk_drag_dest_find_target:
1691 * @widget: drag destination widget
1692 * @context: drag context
1693 * @target_list: (allow-none): list of droppable targets, or %NULL to use
1694 * gtk_drag_dest_get_target_list (@widget).
1696 * Looks for a match between @context->targets and the
1697 * @dest_target_list, returning the first matching target, otherwise
1698 * returning %GDK_NONE. @dest_target_list should usually be the return
1699 * value from gtk_drag_dest_get_target_list(), but some widgets may
1700 * have different valid targets for different parts of the widget; in
1701 * that case, they will have to implement a drag_motion handler that
1702 * passes the correct target list to this function.
1704 * Return value: first target that the source offers and the dest can accept, or %GDK_NONE
1707 gtk_drag_dest_find_target (GtkWidget *widget,
1708 GdkDragContext *context,
1709 GtkTargetList *target_list)
1712 GList *tmp_source = NULL;
1713 GtkWidget *source_widget;
1715 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1716 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1717 g_return_val_if_fail (!context->is_source, GDK_NONE);
1720 source_widget = gtk_drag_get_source_widget (context);
1722 if (target_list == NULL)
1723 target_list = gtk_drag_dest_get_target_list (widget);
1725 if (target_list == NULL)
1728 tmp_target = target_list->list;
1731 GtkTargetPair *pair = tmp_target->data;
1732 tmp_source = context->targets;
1735 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1737 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1738 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)) &&
1739 (!(pair->flags & GTK_TARGET_OTHER_APP) || !source_widget) &&
1740 (!(pair->flags & GTK_TARGET_OTHER_WIDGET) || (source_widget != widget)))
1741 return pair->target;
1745 tmp_source = tmp_source->next;
1747 tmp_target = tmp_target->next;
1754 gtk_drag_selection_received (GtkWidget *widget,
1755 GtkSelectionData *selection_data,
1759 GdkDragContext *context;
1760 GtkDragDestInfo *info;
1761 GtkWidget *drop_widget;
1765 context = g_object_get_data (G_OBJECT (widget), "drag-context");
1766 info = gtk_drag_get_dest_info (context, FALSE);
1768 if (info->proxy_data &&
1769 info->proxy_data->target == selection_data->target)
1771 gtk_selection_data_set (info->proxy_data,
1772 selection_data->type,
1773 selection_data->format,
1774 selection_data->data,
1775 selection_data->length);
1780 if (selection_data->target == gdk_atom_intern_static_string ("DELETE"))
1782 gtk_drag_finish (context, TRUE, FALSE, time);
1784 else if ((selection_data->target == gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS")) ||
1785 (selection_data->target == gdk_atom_intern_static_string ("XmTRANSFER_FAILURE")))
1791 GtkDragDestSite *site;
1793 site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1795 if (site && site->target_list)
1799 if (gtk_target_list_find (site->target_list,
1800 selection_data->target,
1803 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1804 selection_data->length >= 0)
1805 g_signal_emit_by_name (drop_widget,
1806 "drag-data-received",
1807 context, info->drop_x, info->drop_y,
1814 g_signal_emit_by_name (drop_widget,
1815 "drag-data-received",
1816 context, info->drop_x, info->drop_y,
1821 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1824 gtk_drag_finish (context,
1825 (selection_data->length >= 0),
1826 (context->action == GDK_ACTION_MOVE),
1830 g_object_unref (drop_widget);
1833 g_signal_handlers_disconnect_by_func (widget,
1834 gtk_drag_selection_received,
1837 g_object_set_data (G_OBJECT (widget), I_("drag-context"), NULL);
1838 g_object_unref (context);
1840 gtk_drag_release_ipc_widget (widget);
1843 /*************************************************************
1844 * gtk_drag_find_widget:
1845 * Function used to locate widgets for
1846 * DRAG_MOTION and DROP_START events.
1847 *************************************************************/
1850 gtk_drag_find_widget (GtkWidget *widget,
1851 GdkDragContext *context,
1852 GtkDragDestInfo *info,
1856 GtkDragDestCallback callback)
1858 if (!gtk_widget_get_mapped (widget) ||
1859 !gtk_widget_get_sensitive (widget))
1862 /* Get the widget at the pointer coordinates and travel up
1863 * the widget hierarchy from there.
1865 widget = _gtk_widget_find_at_coords (gtk_widget_get_window (widget),
1873 GList *hierarchy = NULL;
1874 gboolean found = FALSE;
1876 if (!gtk_widget_get_mapped (widget) ||
1877 !gtk_widget_get_sensitive (widget))
1880 /* need to reference the entire hierarchy temporarily in case the
1881 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
1883 for (parent = widget;
1885 parent = gtk_widget_get_parent (parent))
1887 hierarchy = g_list_prepend (hierarchy, g_object_ref (parent));
1890 /* If the current widget is registered as a drop site, check to
1891 * emit "drag-motion" to check if we are actually in a drop
1894 if (g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1896 found = callback (widget, context, x, y, time);
1898 /* If so, send a "drag-leave" to the last widget */
1901 if (info->widget && info->widget != widget)
1903 gtk_drag_dest_leave (info->widget, context, time);
1906 info->widget = widget;
1912 /* Get the parent before unreffing the hierarchy because
1913 * invoking the callback might have destroyed the widget
1915 parent = gtk_widget_get_parent (widget);
1917 /* The parent might be going away when unreffing the
1918 * hierarchy, so also protect againt that
1921 g_object_add_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1924 g_list_foreach (hierarchy, (GFunc) g_object_unref, NULL);
1925 g_list_free (hierarchy);
1931 g_object_remove_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1935 if (!gtk_widget_translate_coordinates (widget, parent, x, y, &x, &y))
1945 gtk_drag_proxy_begin (GtkWidget *widget,
1946 GtkDragDestInfo *dest_info,
1949 GtkDragSourceInfo *source_info;
1951 GdkDragContext *context;
1952 GtkWidget *ipc_widget;
1954 if (dest_info->proxy_source)
1956 gdk_drag_abort (dest_info->proxy_source->context, time);
1957 gtk_drag_source_info_destroy (dest_info->proxy_source);
1958 dest_info->proxy_source = NULL;
1961 ipc_widget = gtk_drag_get_ipc_widget (widget);
1962 context = gdk_drag_begin (gtk_widget_get_window (ipc_widget),
1963 dest_info->context->targets);
1965 source_info = gtk_drag_get_source_info (context, TRUE);
1967 source_info->ipc_widget = ipc_widget;
1968 source_info->widget = g_object_ref (widget);
1970 source_info->target_list = gtk_target_list_new (NULL, 0);
1971 tmp_list = dest_info->context->targets;
1974 gtk_target_list_add (source_info->target_list,
1975 GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
1976 tmp_list = tmp_list->next;
1979 source_info->proxy_dest = dest_info;
1981 g_signal_connect (ipc_widget,
1983 G_CALLBACK (gtk_drag_selection_get),
1986 dest_info->proxy_source = source_info;
1990 gtk_drag_dest_info_destroy (gpointer data)
1992 GtkDragDestInfo *info = data;
1997 static GtkDragDestInfo *
1998 gtk_drag_get_dest_info (GdkDragContext *context,
2001 GtkDragDestInfo *info;
2002 static GQuark info_quark = 0;
2004 info_quark = g_quark_from_static_string ("gtk-dest-info");
2006 info = g_object_get_qdata (G_OBJECT (context), info_quark);
2007 if (!info && create)
2009 info = g_new (GtkDragDestInfo, 1);
2010 info->widget = NULL;
2011 info->context = context;
2012 info->proxy_source = NULL;
2013 info->proxy_data = NULL;
2014 info->dropped = FALSE;
2015 info->proxy_drop_wait = FALSE;
2016 g_object_set_qdata_full (G_OBJECT (context), info_quark,
2017 info, gtk_drag_dest_info_destroy);
2023 static GQuark dest_info_quark = 0;
2025 static GtkDragSourceInfo *
2026 gtk_drag_get_source_info (GdkDragContext *context,
2029 GtkDragSourceInfo *info;
2030 if (!dest_info_quark)
2031 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
2033 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
2034 if (!info && create)
2036 info = g_new0 (GtkDragSourceInfo, 1);
2037 info->context = context;
2038 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
2045 gtk_drag_clear_source_info (GdkDragContext *context)
2047 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
2051 gtk_drag_dest_realized (GtkWidget *widget)
2053 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2055 if (gtk_widget_is_toplevel (toplevel))
2056 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2060 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
2061 GtkWidget *previous_toplevel)
2063 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2065 if (gtk_widget_is_toplevel (toplevel) && gtk_widget_get_realized (toplevel))
2066 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2070 gtk_drag_dest_site_destroy (gpointer data)
2072 GtkDragDestSite *site = data;
2074 if (site->proxy_window)
2075 g_object_unref (site->proxy_window);
2077 if (site->target_list)
2078 gtk_target_list_unref (site->target_list);
2084 * Default drag handlers
2087 gtk_drag_dest_leave (GtkWidget *widget,
2088 GdkDragContext *context,
2091 GtkDragDestSite *site;
2093 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2094 g_return_if_fail (site != NULL);
2098 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2100 if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
2102 gdk_drag_abort (info->proxy_source->context, time);
2103 gtk_drag_source_info_destroy (info->proxy_source);
2104 info->proxy_source = NULL;
2111 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
2112 gtk_drag_unhighlight (widget);
2114 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
2116 g_signal_emit_by_name (widget, "drag-leave", context, time);
2118 site->have_drag = FALSE;
2123 gtk_drag_dest_motion (GtkWidget *widget,
2124 GdkDragContext *context,
2129 GtkDragDestSite *site;
2130 GdkDragAction action = 0;
2133 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2134 g_return_val_if_fail (site != NULL, FALSE);
2139 GdkEvent *current_event;
2140 GdkWindow *dest_window;
2141 GdkDragProtocol proto;
2143 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2145 if (!info->proxy_source || info->proxy_source->widget != widget)
2146 gtk_drag_proxy_begin (widget, info, time);
2148 current_event = gtk_get_current_event ();
2150 if (site->proxy_window)
2152 dest_window = site->proxy_window;
2153 proto = site->proxy_protocol;
2157 gdk_drag_find_window_for_screen (info->proxy_source->context,
2159 gdk_drawable_get_screen (current_event->dnd.window),
2160 current_event->dnd.x_root,
2161 current_event->dnd.y_root,
2162 &dest_window, &proto);
2165 gdk_drag_motion (info->proxy_source->context,
2167 current_event->dnd.x_root,
2168 current_event->dnd.y_root,
2169 context->suggested_action,
2170 context->actions, time);
2172 if (!site->proxy_window && dest_window)
2173 g_object_unref (dest_window);
2175 selection = gdk_drag_get_selection (info->proxy_source->context);
2177 selection != gdk_drag_get_selection (info->context))
2178 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2180 gdk_event_free (current_event);
2185 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
2187 if (context->suggested_action & site->actions)
2188 action = context->suggested_action;
2193 for (i = 0; i < 8; i++)
2195 if ((site->actions & (1 << i)) &&
2196 (context->actions & (1 << i)))
2204 if (action && gtk_drag_dest_find_target (widget, context, NULL))
2206 if (!site->have_drag)
2208 site->have_drag = TRUE;
2209 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
2210 gtk_drag_highlight (widget);
2213 gdk_drag_status (context, action, time);
2217 gdk_drag_status (context, 0, time);
2218 if (!site->track_motion)
2223 g_signal_emit_by_name (widget, "drag-motion",
2224 context, x, y, time, &retval);
2226 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
2230 gtk_drag_dest_drop (GtkWidget *widget,
2231 GdkDragContext *context,
2236 GtkDragDestSite *site;
2237 GtkDragDestInfo *info;
2239 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2240 g_return_val_if_fail (site != NULL, FALSE);
2242 info = gtk_drag_get_dest_info (context, FALSE);
2243 g_return_val_if_fail (info != NULL, FALSE);
2250 if (info->proxy_source ||
2251 (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN))
2253 gtk_drag_drop (info->proxy_source, time);
2257 /* We need to synthesize a motion event, wait for a status,
2258 * and, if we get a good one, do a drop.
2261 GdkEvent *current_event;
2263 GdkWindow *dest_window;
2264 GdkDragProtocol proto;
2266 gtk_drag_proxy_begin (widget, info, time);
2267 info->proxy_drop_wait = TRUE;
2268 info->proxy_drop_time = time;
2270 current_event = gtk_get_current_event ();
2272 if (site->proxy_window)
2274 dest_window = site->proxy_window;
2275 proto = site->proxy_protocol;
2279 gdk_drag_find_window_for_screen (info->proxy_source->context,
2281 gdk_drawable_get_screen (current_event->dnd.window),
2282 current_event->dnd.x_root,
2283 current_event->dnd.y_root,
2284 &dest_window, &proto);
2287 gdk_drag_motion (info->proxy_source->context,
2289 current_event->dnd.x_root,
2290 current_event->dnd.y_root,
2291 context->suggested_action,
2292 context->actions, time);
2294 if (!site->proxy_window && dest_window)
2295 g_object_unref (dest_window);
2297 selection = gdk_drag_get_selection (info->proxy_source->context);
2299 selection != gdk_drag_get_selection (info->context))
2300 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2302 gdk_event_free (current_event);
2311 if (site->flags & GTK_DEST_DEFAULT_DROP)
2313 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2315 if (target == GDK_NONE)
2317 gtk_drag_finish (context, FALSE, FALSE, time);
2321 gtk_drag_get_data (widget, context, target, time);
2324 g_signal_emit_by_name (widget, "drag-drop",
2325 context, x, y, time, &retval);
2327 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2335 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2336 * so that we can set the icon from the source site information
2338 static GdkDragContext *
2339 gtk_drag_begin_internal (GtkWidget *widget,
2340 GtkDragSourceSite *site,
2341 GtkTargetList *target_list,
2342 GdkDragAction actions,
2346 GtkDragSourceInfo *info;
2347 GList *targets = NULL;
2349 guint32 time = GDK_CURRENT_TIME;
2350 GdkDragAction possible_actions, suggested_action;
2351 GdkDragContext *context;
2352 GtkWidget *ipc_widget;
2354 GdkDevice *pointer, *keyboard;
2355 GdkWindow *ipc_window;
2357 pointer = keyboard = NULL;
2358 ipc_widget = gtk_drag_get_ipc_widget (widget);
2360 gtk_drag_get_event_actions (event, button, actions,
2361 &suggested_action, &possible_actions);
2363 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2369 time = gdk_event_get_time (event);
2370 if (time == GDK_CURRENT_TIME)
2371 time = gtk_get_current_event_time ();
2373 pointer = gdk_event_get_device (event);
2375 if (pointer->source == GDK_SOURCE_KEYBOARD)
2378 pointer = gdk_device_get_associated_device (keyboard);
2381 keyboard = gdk_device_get_associated_device (pointer);
2385 GdkDeviceManager *device_manager;
2387 device_manager = gdk_display_get_device_manager (gtk_widget_get_display (widget));
2388 pointer = gdk_device_manager_get_client_pointer (device_manager);
2389 keyboard = gdk_device_get_associated_device (pointer);
2395 ipc_window = gtk_widget_get_window (ipc_widget);
2397 if (gdk_device_grab (pointer, ipc_window,
2398 GDK_OWNERSHIP_APPLICATION, FALSE,
2399 GDK_POINTER_MOTION_MASK |
2400 GDK_BUTTON_RELEASE_MASK,
2401 cursor, time) != GDK_GRAB_SUCCESS)
2403 gtk_drag_release_ipc_widget (ipc_widget);
2408 grab_dnd_keys (ipc_widget, keyboard, time);
2410 /* We use a GTK grab here to override any grabs that the widget
2411 * we are dragging from might have held
2413 gtk_device_grab_add (ipc_widget, pointer, FALSE);
2415 tmp_list = g_list_last (target_list->list);
2418 GtkTargetPair *pair = tmp_list->data;
2419 targets = g_list_prepend (targets,
2420 GINT_TO_POINTER (pair->target));
2421 tmp_list = tmp_list->prev;
2424 source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2426 context = gdk_drag_begin (ipc_window, targets);
2427 gdk_drag_context_set_device (context, pointer);
2428 g_list_free (targets);
2430 info = gtk_drag_get_source_info (context, TRUE);
2432 info->ipc_widget = ipc_widget;
2433 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), info);
2435 info->widget = g_object_ref (widget);
2437 info->button = button;
2438 info->cursor = cursor;
2439 info->target_list = target_list;
2440 gtk_target_list_ref (target_list);
2442 info->possible_actions = actions;
2444 info->status = GTK_DRAG_STATUS_DRAG;
2445 info->last_event = NULL;
2446 info->selections = NULL;
2447 info->icon_window = NULL;
2448 info->destroy_icon = FALSE;
2450 /* Set cur_x, cur_y here so if the "drag-begin" signal shows
2451 * the drag icon, it will be in the right place
2453 if (event && event->type == GDK_MOTION_NOTIFY)
2455 info->cur_screen = gtk_widget_get_screen (widget);
2456 info->cur_x = event->motion.x_root;
2457 info->cur_y = event->motion.y_root;
2461 gdk_display_get_device_state (gtk_widget_get_display (widget), pointer,
2462 &info->cur_screen, &info->cur_x, &info->cur_y, NULL);
2465 g_signal_emit_by_name (widget, "drag-begin", info->context);
2467 /* Ensure that we have an icon before we start the drag; the
2468 * application may have set one in ::drag_begin, or it may
2471 if (!info->icon_window && !info->icon_pixbuf)
2473 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2474 gtk_drag_set_icon_default (context);
2476 switch (site->icon_type)
2478 case GTK_IMAGE_PIXMAP:
2479 gtk_drag_set_icon_pixmap (context,
2481 site->icon_data.pixmap.pixmap,
2485 case GTK_IMAGE_PIXBUF:
2486 gtk_drag_set_icon_pixbuf (context,
2487 site->icon_data.pixbuf.pixbuf,
2490 case GTK_IMAGE_STOCK:
2491 gtk_drag_set_icon_stock (context,
2492 site->icon_data.stock.stock_id,
2495 case GTK_IMAGE_ICON_NAME:
2496 gtk_drag_set_icon_name (context,
2497 site->icon_data.name.icon_name,
2500 case GTK_IMAGE_EMPTY:
2502 g_assert_not_reached();
2507 /* We need to composite the icon into the cursor, if we are
2508 * not using an icon window.
2510 if (info->icon_pixbuf)
2512 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2516 if (cursor != info->cursor)
2518 gdk_device_grab (pointer, gtk_widget_get_window (widget),
2519 GDK_OWNERSHIP_APPLICATION, FALSE,
2520 GDK_POINTER_MOTION_MASK |
2521 GDK_BUTTON_RELEASE_MASK,
2523 info->cursor = cursor;
2527 if (event && event->type == GDK_MOTION_NOTIFY)
2528 gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2530 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, event);
2532 info->start_x = info->cur_x;
2533 info->start_y = info->cur_y;
2535 g_signal_connect (info->ipc_widget, "grab-broken-event",
2536 G_CALLBACK (gtk_drag_grab_broken_event_cb), info);
2537 g_signal_connect (info->ipc_widget, "grab-notify",
2538 G_CALLBACK (gtk_drag_grab_notify_cb), info);
2539 g_signal_connect (info->ipc_widget, "button-release-event",
2540 G_CALLBACK (gtk_drag_button_release_cb), info);
2541 g_signal_connect (info->ipc_widget, "motion-notify-event",
2542 G_CALLBACK (gtk_drag_motion_cb), info);
2543 g_signal_connect (info->ipc_widget, "key-press-event",
2544 G_CALLBACK (gtk_drag_key_cb), info);
2545 g_signal_connect (info->ipc_widget, "key-release-event",
2546 G_CALLBACK (gtk_drag_key_cb), info);
2547 g_signal_connect (info->ipc_widget, "selection-get",
2548 G_CALLBACK (gtk_drag_selection_get), info);
2550 info->have_grab = TRUE;
2551 info->grab_time = time;
2553 return info->context;
2558 * @widget: the source widget.
2559 * @targets: The targets (data formats) in which the
2560 * source can provide the data.
2561 * @actions: A bitmask of the allowed drag actions for this drag.
2562 * @button: The button the user clicked to start the drag.
2563 * @event: The event that triggered the start of the drag.
2565 * Initiates a drag on the source side. The function
2566 * only needs to be used when the application is
2567 * starting drags itself, and is not needed when
2568 * gtk_drag_source_set() is used.
2570 * The @event is used to retrieve the timestamp that will be used internally to
2571 * grab the pointer. If @event is #NULL, then GDK_CURRENT_TIME will be used.
2572 * However, you should try to pass a real event in all cases, since that can be
2573 * used by GTK+ to get information about the start position of the drag, for
2574 * example if the @event is a GDK_MOTION_NOTIFY.
2576 * Generally there are three cases when you want to start a drag by hand by calling
2579 * 1. During a button-press-event handler, if you want to start a drag immediately
2580 * when the user presses the mouse button. Pass the @event that you have in your
2581 * button-press-event handler.
2583 * 2. During a motion-notify-event handler, if you want to start a drag when the mouse
2584 * moves past a certain threshold distance after a button-press. Pass the @event that you
2585 * have in your motion-notify-event handler.
2587 * 3. During a timeout handler, if you want to start a drag after the mouse
2588 * button is held down for some time. Try to save the last event that you got
2589 * from the mouse, using gdk_event_copy(), and pass it to this function
2590 * (remember to free the event with gdk_event_free() when you are done). If you
2591 * can really not pass a real event, pass #NULL instead.
2593 * Return value: the context for this drag.
2596 gtk_drag_begin (GtkWidget *widget,
2597 GtkTargetList *targets,
2598 GdkDragAction actions,
2602 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2603 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
2604 g_return_val_if_fail (targets != NULL, NULL);
2606 return gtk_drag_begin_internal (widget, NULL, targets,
2607 actions, button, event);
2611 * gtk_drag_source_set:
2612 * @widget: a #GtkWidget
2613 * @start_button_mask: the bitmask of buttons that can start the drag
2614 * @targets: (allow-none) (array length=n_targets): the table of targets that the drag will support,
2616 * @n_targets: the number of items in @targets
2617 * @actions: the bitmask of possible actions for a drag from this widget
2619 * Sets up a widget so that GTK+ will start a drag operation when the user
2620 * clicks and drags on the widget. The widget must have a window.
2623 gtk_drag_source_set (GtkWidget *widget,
2624 GdkModifierType start_button_mask,
2625 const GtkTargetEntry *targets,
2627 GdkDragAction actions)
2629 GtkDragSourceSite *site;
2631 g_return_if_fail (GTK_IS_WIDGET (widget));
2633 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2635 gtk_widget_add_events (widget,
2636 gtk_widget_get_events (widget) |
2637 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2638 GDK_BUTTON_MOTION_MASK);
2642 if (site->target_list)
2643 gtk_target_list_unref (site->target_list);
2647 site = g_new0 (GtkDragSourceSite, 1);
2649 site->icon_type = GTK_IMAGE_EMPTY;
2651 g_signal_connect (widget, "button-press-event",
2652 G_CALLBACK (gtk_drag_source_event_cb),
2654 g_signal_connect (widget, "button-release-event",
2655 G_CALLBACK (gtk_drag_source_event_cb),
2657 g_signal_connect (widget, "motion-notify-event",
2658 G_CALLBACK (gtk_drag_source_event_cb),
2661 g_object_set_data_full (G_OBJECT (widget),
2662 I_("gtk-site-data"),
2663 site, gtk_drag_source_site_destroy);
2666 site->start_button_mask = start_button_mask;
2668 site->target_list = gtk_target_list_new (targets, n_targets);
2670 site->actions = actions;
2673 /*************************************************************
2674 * gtk_drag_source_unset
2675 * Unregister this widget as a drag source.
2679 *************************************************************/
2682 gtk_drag_source_unset (GtkWidget *widget)
2684 GtkDragSourceSite *site;
2686 g_return_if_fail (GTK_IS_WIDGET (widget));
2688 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2692 g_signal_handlers_disconnect_by_func (widget,
2693 gtk_drag_source_event_cb,
2695 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
2700 * gtk_drag_source_get_target_list:
2701 * @widget: a #GtkWidget
2703 * Gets the list of targets this widget can provide for
2706 * Return value: the #GtkTargetList, or %NULL if none
2711 gtk_drag_source_get_target_list (GtkWidget *widget)
2713 GtkDragSourceSite *site;
2715 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2717 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2719 return site ? site->target_list : NULL;
2723 * gtk_drag_source_set_target_list:
2724 * @widget: a #GtkWidget that's a drag source
2725 * @target_list: (allow-none): list of draggable targets, or %NULL for none
2727 * Changes the target types that this widget offers for drag-and-drop.
2728 * The widget must first be made into a drag source with
2729 * gtk_drag_source_set().
2734 gtk_drag_source_set_target_list (GtkWidget *widget,
2735 GtkTargetList *target_list)
2737 GtkDragSourceSite *site;
2739 g_return_if_fail (GTK_IS_WIDGET (widget));
2741 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2744 g_warning ("gtk_drag_source_set_target_list() requires the widget "
2745 "to already be a drag source.");
2750 gtk_target_list_ref (target_list);
2752 if (site->target_list)
2753 gtk_target_list_unref (site->target_list);
2755 site->target_list = target_list;
2759 * gtk_drag_source_add_text_targets:
2760 * @widget: a #GtkWidget that's is a drag source
2762 * Add the text targets supported by #GtkSelection to
2763 * the target list of the drag source. The targets
2764 * are added with @info = 0. If you need another value,
2765 * use gtk_target_list_add_text_targets() and
2766 * gtk_drag_source_set_target_list().
2771 gtk_drag_source_add_text_targets (GtkWidget *widget)
2773 GtkTargetList *target_list;
2775 target_list = gtk_drag_source_get_target_list (widget);
2777 gtk_target_list_ref (target_list);
2779 target_list = gtk_target_list_new (NULL, 0);
2780 gtk_target_list_add_text_targets (target_list, 0);
2781 gtk_drag_source_set_target_list (widget, target_list);
2782 gtk_target_list_unref (target_list);
2786 * gtk_drag_source_add_image_targets:
2787 * @widget: a #GtkWidget that's is a drag source
2789 * Add the writable image targets supported by #GtkSelection to
2790 * the target list of the drag source. The targets
2791 * are added with @info = 0. If you need another value,
2792 * use gtk_target_list_add_image_targets() and
2793 * gtk_drag_source_set_target_list().
2798 gtk_drag_source_add_image_targets (GtkWidget *widget)
2800 GtkTargetList *target_list;
2802 target_list = gtk_drag_source_get_target_list (widget);
2804 gtk_target_list_ref (target_list);
2806 target_list = gtk_target_list_new (NULL, 0);
2807 gtk_target_list_add_image_targets (target_list, 0, TRUE);
2808 gtk_drag_source_set_target_list (widget, target_list);
2809 gtk_target_list_unref (target_list);
2813 * gtk_drag_source_add_uri_targets:
2814 * @widget: a #GtkWidget that's is a drag source
2816 * Add the URI targets supported by #GtkSelection to
2817 * the target list of the drag source. The targets
2818 * are added with @info = 0. If you need another value,
2819 * use gtk_target_list_add_uri_targets() and
2820 * gtk_drag_source_set_target_list().
2825 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2827 GtkTargetList *target_list;
2829 target_list = gtk_drag_source_get_target_list (widget);
2831 gtk_target_list_ref (target_list);
2833 target_list = gtk_target_list_new (NULL, 0);
2834 gtk_target_list_add_uri_targets (target_list, 0);
2835 gtk_drag_source_set_target_list (widget, target_list);
2836 gtk_target_list_unref (target_list);
2840 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2842 switch (site->icon_type)
2844 case GTK_IMAGE_EMPTY:
2846 case GTK_IMAGE_PIXMAP:
2847 if (site->icon_data.pixmap.pixmap)
2848 g_object_unref (site->icon_data.pixmap.pixmap);
2849 if (site->icon_mask)
2850 g_object_unref (site->icon_mask);
2852 case GTK_IMAGE_PIXBUF:
2853 g_object_unref (site->icon_data.pixbuf.pixbuf);
2855 case GTK_IMAGE_STOCK:
2856 g_free (site->icon_data.stock.stock_id);
2858 case GTK_IMAGE_ICON_NAME:
2859 g_free (site->icon_data.name.icon_name);
2862 g_assert_not_reached();
2865 site->icon_type = GTK_IMAGE_EMPTY;
2868 g_object_unref (site->colormap);
2869 site->colormap = NULL;
2873 * gtk_drag_source_set_icon:
2874 * @widget: a #GtkWidget
2875 * @colormap: the colormap of the icon
2876 * @pixmap: the image data for the icon
2877 * @mask: (allow-none): the transparency mask for an image.
2879 * Sets the icon that will be used for drags from a particular widget
2880 * from a pixmap/mask. GTK+ retains references for the arguments, and
2881 * will release them when they are no longer needed.
2882 * Use gtk_drag_source_set_icon_pixbuf() instead.
2885 gtk_drag_source_set_icon (GtkWidget *widget,
2886 GdkColormap *colormap,
2890 GtkDragSourceSite *site;
2892 g_return_if_fail (GTK_IS_WIDGET (widget));
2893 g_return_if_fail (GDK_IS_COLORMAP (colormap));
2894 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
2895 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
2897 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2898 g_return_if_fail (site != NULL);
2900 g_object_ref (colormap);
2901 g_object_ref (pixmap);
2903 g_object_ref (mask);
2905 gtk_drag_source_unset_icon (site);
2907 site->icon_type = GTK_IMAGE_PIXMAP;
2909 site->icon_data.pixmap.pixmap = pixmap;
2910 site->icon_mask = mask;
2911 site->colormap = colormap;
2915 * gtk_drag_source_set_icon_pixbuf:
2916 * @widget: a #GtkWidget
2917 * @pixbuf: the #GdkPixbuf for the drag icon
2919 * Sets the icon that will be used for drags from a particular widget
2920 * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
2921 * release it when it is no longer needed.
2924 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
2927 GtkDragSourceSite *site;
2929 g_return_if_fail (GTK_IS_WIDGET (widget));
2930 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2932 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2933 g_return_if_fail (site != NULL);
2934 g_object_ref (pixbuf);
2936 gtk_drag_source_unset_icon (site);
2938 site->icon_type = GTK_IMAGE_PIXBUF;
2939 site->icon_data.pixbuf.pixbuf = pixbuf;
2943 * gtk_drag_source_set_icon_stock:
2944 * @widget: a #GtkWidget
2945 * @stock_id: the ID of the stock icon to use
2947 * Sets the icon that will be used for drags from a particular source
2951 gtk_drag_source_set_icon_stock (GtkWidget *widget,
2952 const gchar *stock_id)
2954 GtkDragSourceSite *site;
2956 g_return_if_fail (GTK_IS_WIDGET (widget));
2957 g_return_if_fail (stock_id != NULL);
2959 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2960 g_return_if_fail (site != NULL);
2962 gtk_drag_source_unset_icon (site);
2964 site->icon_type = GTK_IMAGE_STOCK;
2965 site->icon_data.stock.stock_id = g_strdup (stock_id);
2969 * gtk_drag_source_set_icon_name:
2970 * @widget: a #GtkWidget
2971 * @icon_name: name of icon to use
2973 * Sets the icon that will be used for drags from a particular source
2974 * to a themed icon. See the docs for #GtkIconTheme for more details.
2979 gtk_drag_source_set_icon_name (GtkWidget *widget,
2980 const gchar *icon_name)
2982 GtkDragSourceSite *site;
2984 g_return_if_fail (GTK_IS_WIDGET (widget));
2985 g_return_if_fail (icon_name != NULL);
2987 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2988 g_return_if_fail (site != NULL);
2990 gtk_drag_source_unset_icon (site);
2992 site->icon_type = GTK_IMAGE_ICON_NAME;
2993 site->icon_data.name.icon_name = g_strdup (icon_name);
2997 gtk_drag_get_icon (GtkDragSourceInfo *info,
2998 GtkWidget **icon_window,
3002 if (get_can_change_screen (info->icon_window))
3003 gtk_window_set_screen (GTK_WINDOW (info->icon_window),
3006 if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
3008 if (!info->fallback_icon)
3010 gint save_hot_x, save_hot_y;
3011 gboolean save_destroy_icon;
3012 GtkWidget *save_icon_window;
3014 /* HACK to get the appropriate icon
3016 save_icon_window = info->icon_window;
3017 save_hot_x = info->hot_x;
3018 save_hot_y = info->hot_x;
3019 save_destroy_icon = info->destroy_icon;
3021 info->icon_window = NULL;
3022 set_icon_stock_pixbuf (info->context,
3023 GTK_STOCK_DND, NULL, -2, -2, TRUE);
3024 info->fallback_icon = info->icon_window;
3026 info->icon_window = save_icon_window;
3027 info->hot_x = save_hot_x;
3028 info->hot_y = save_hot_y;
3029 info->destroy_icon = save_destroy_icon;
3032 gtk_widget_hide (info->icon_window);
3034 *icon_window = info->fallback_icon;
3035 gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
3042 if (info->fallback_icon)
3043 gtk_widget_hide (info->fallback_icon);
3045 *icon_window = info->icon_window;
3046 *hot_x = info->hot_x;
3047 *hot_y = info->hot_y;
3052 gtk_drag_update_icon (GtkDragSourceInfo *info)
3054 if (info->icon_window)
3056 GtkWidget *icon_window;
3059 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
3061 gtk_window_move (GTK_WINDOW (icon_window),
3062 info->cur_x - hot_x,
3063 info->cur_y - hot_y);
3065 if (gtk_widget_get_visible (icon_window))
3066 gdk_window_raise (gtk_widget_get_window (icon_window));
3068 gtk_widget_show (icon_window);
3073 gtk_drag_set_icon_window (GdkDragContext *context,
3077 gboolean destroy_on_release)
3079 GtkDragSourceInfo *info;
3081 info = gtk_drag_get_source_info (context, FALSE);
3084 if (destroy_on_release)
3085 gtk_widget_destroy (widget);
3089 gtk_drag_remove_icon (info);
3092 g_object_ref (widget);
3094 info->icon_window = widget;
3095 info->hot_x = hot_x;
3096 info->hot_y = hot_y;
3097 info->destroy_icon = destroy_on_release;
3099 if (widget && info->icon_pixbuf)
3101 g_object_unref (info->icon_pixbuf);
3102 info->icon_pixbuf = NULL;
3105 gtk_drag_update_cursor (info);
3106 gtk_drag_update_icon (info);
3110 * gtk_drag_set_icon_widget:
3111 * @context: the context for a drag. (This must be called
3112 with a context for the source side of a drag)
3113 * @widget: a toplevel window to use as an icon.
3114 * @hot_x: the X offset within @widget of the hotspot.
3115 * @hot_y: the Y offset within @widget of the hotspot.
3117 * Changes the icon for a widget to a given widget. GTK+
3118 * will not destroy the icon, so if you don't want
3119 * it to persist, you should connect to the "drag-end"
3120 * signal and destroy it yourself.
3123 gtk_drag_set_icon_widget (GdkDragContext *context,
3128 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3129 g_return_if_fail (context->is_source);
3130 g_return_if_fail (GTK_IS_WIDGET (widget));
3132 gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
3136 icon_window_realize (GtkWidget *window,
3142 gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
3143 gtk_widget_get_colormap (window),
3144 &pixmap, &mask, 128);
3146 gdk_window_set_back_pixmap (gtk_widget_get_window (window),
3148 g_object_unref (pixmap);
3152 gtk_widget_shape_combine_mask (window, mask, 0, 0);
3153 g_object_unref (mask);
3158 set_icon_stock_pixbuf (GdkDragContext *context,
3159 const gchar *stock_id,
3163 gboolean force_window)
3168 GdkDisplay *display;
3170 g_return_if_fail (context != NULL);
3171 g_return_if_fail (pixbuf != NULL || stock_id != NULL);
3172 g_return_if_fail (pixbuf == NULL || stock_id == NULL);
3174 screen = gdk_drawable_get_screen (context->source_window);
3176 /* Push a NULL colormap to guard against gtk_widget_push_colormap() */
3177 gtk_widget_push_colormap (NULL);
3178 window = gtk_window_new (GTK_WINDOW_POPUP);
3179 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3180 gtk_window_set_screen (GTK_WINDOW (window), screen);
3181 set_can_change_screen (window, TRUE);
3182 gtk_widget_pop_colormap ();
3184 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3185 gtk_widget_set_app_paintable (window, TRUE);
3189 pixbuf = gtk_widget_render_icon (window, stock_id,
3190 GTK_ICON_SIZE_DND, NULL);
3194 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
3195 gtk_widget_destroy (window);
3201 g_object_ref (pixbuf);
3203 display = gdk_drawable_get_display (context->source_window);
3204 width = gdk_pixbuf_get_width (pixbuf);
3205 height = gdk_pixbuf_get_height (pixbuf);
3207 if (!force_window &&
3208 gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
3210 GtkDragSourceInfo *info;
3212 gtk_widget_destroy (window);
3214 info = gtk_drag_get_source_info (context, FALSE);
3216 if (info->icon_pixbuf)
3217 g_object_unref (info->icon_pixbuf);
3218 info->icon_pixbuf = pixbuf;
3220 gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
3224 gtk_widget_set_size_request (window, width, height);
3226 g_signal_connect_closure (window, "realize",
3227 g_cclosure_new (G_CALLBACK (icon_window_realize),
3229 (GClosureNotify)g_object_unref),
3232 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3237 * gtk_drag_set_icon_pixbuf:
3238 * @context: the context for a drag. (This must be called
3239 * with a context for the source side of a drag)
3240 * @pixbuf: the #GdkPixbuf to use as the drag icon.
3241 * @hot_x: the X offset within @widget of the hotspot.
3242 * @hot_y: the Y offset within @widget of the hotspot.
3244 * Sets @pixbuf as the icon for a given drag.
3247 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
3252 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3253 g_return_if_fail (context->is_source);
3254 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3256 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3260 * gtk_drag_set_icon_stock:
3261 * @context: the context for a drag. (This must be called
3262 * with a context for the source side of a drag)
3263 * @stock_id: the ID of the stock icon to use for the drag.
3264 * @hot_x: the X offset within the icon of the hotspot.
3265 * @hot_y: the Y offset within the icon of the hotspot.
3267 * Sets the icon for a given drag from a stock ID.
3270 gtk_drag_set_icon_stock (GdkDragContext *context,
3271 const gchar *stock_id,
3275 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3276 g_return_if_fail (context->is_source);
3277 g_return_if_fail (stock_id != NULL);
3279 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
3283 * gtk_drag_set_icon_pixmap:
3284 * @context: the context for a drag. (This must be called
3285 * with a context for the source side of a drag)
3286 * @colormap: the colormap of the icon
3287 * @pixmap: the image data for the icon
3288 * @mask: (allow-none): the transparency mask for the icon or %NULL for none.
3289 * @hot_x: the X offset within @pixmap of the hotspot.
3290 * @hot_y: the Y offset within @pixmap of the hotspot.
3292 * Sets @pixmap as the icon for a given drag. GTK+ retains
3293 * references for the arguments, and will release them when
3294 * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
3295 * will be more convenient to use.
3298 gtk_drag_set_icon_pixmap (GdkDragContext *context,
3299 GdkColormap *colormap,
3309 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3310 g_return_if_fail (context->is_source);
3311 g_return_if_fail (GDK_IS_COLORMAP (colormap));
3312 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
3313 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
3315 screen = gdk_colormap_get_screen (colormap);
3317 g_return_if_fail (gdk_drawable_get_screen (pixmap) == screen);
3318 g_return_if_fail (!mask || gdk_drawable_get_screen (mask) == screen);
3320 gdk_drawable_get_size (pixmap, &width, &height);
3322 gtk_widget_push_colormap (colormap);
3324 window = gtk_window_new (GTK_WINDOW_POPUP);
3325 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3326 gtk_window_set_screen (GTK_WINDOW (window), screen);
3327 set_can_change_screen (window, FALSE);
3328 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3329 gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
3331 gtk_widget_pop_colormap ();
3333 gtk_widget_set_size_request (window, width, height);
3334 gtk_widget_realize (window);
3336 gdk_window_set_back_pixmap (gtk_widget_get_window (window),
3340 gtk_widget_shape_combine_mask (window, mask, 0, 0);
3342 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3346 * gtk_drag_set_icon_name:
3347 * @context: the context for a drag. (This must be called
3348 * with a context for the source side of a drag)
3349 * @icon_name: name of icon to use
3350 * @hot_x: the X offset of the hotspot within the icon
3351 * @hot_y: the Y offset of the hotspot within the icon
3353 * Sets the icon for a given drag from a named themed icon. See
3354 * the docs for #GtkIconTheme for more details. Note that the
3355 * size of the icon depends on the icon theme (the icon is
3356 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
3357 * @hot_x and @hot_y have to be used with care.
3362 gtk_drag_set_icon_name (GdkDragContext *context,
3363 const gchar *icon_name,
3368 GtkSettings *settings;
3369 GtkIconTheme *icon_theme;
3371 gint width, height, icon_size;
3373 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3374 g_return_if_fail (context->is_source);
3375 g_return_if_fail (icon_name != NULL);
3377 screen = gdk_drawable_get_screen (context->source_window);
3378 g_return_if_fail (screen != NULL);
3380 settings = gtk_settings_get_for_screen (screen);
3381 if (gtk_icon_size_lookup_for_settings (settings,
3384 icon_size = MAX (width, height);
3386 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
3388 icon_theme = gtk_icon_theme_get_for_screen (screen);
3390 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
3391 icon_size, 0, NULL);
3393 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3395 g_warning ("Cannot load drag icon from icon name %s", icon_name);
3399 * gtk_drag_set_icon_default:
3400 * @context: the context for a drag. (This must be called
3401 with a context for the source side of a drag)
3403 * Sets the icon for a particular drag to the default
3407 gtk_drag_set_icon_default (GdkDragContext *context)
3409 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3410 g_return_if_fail (context->is_source);
3412 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
3415 /*************************************************************
3416 * _gtk_drag_source_handle_event:
3417 * Called from widget event handling code on Drag events
3421 * toplevel: Toplevel widget that received the event
3424 *************************************************************/
3427 _gtk_drag_source_handle_event (GtkWidget *widget,
3430 GtkDragSourceInfo *info;
3431 GdkDragContext *context;
3433 g_return_if_fail (widget != NULL);
3434 g_return_if_fail (event != NULL);
3436 context = event->dnd.context;
3437 info = gtk_drag_get_source_info (context, FALSE);
3441 switch (event->type)
3443 case GDK_DRAG_STATUS:
3447 if (info->proxy_dest)
3449 if (!event->dnd.send_event)
3451 if (info->proxy_dest->proxy_drop_wait)
3453 gboolean result = context->action != 0;
3455 /* Aha - we can finally pass the MOTIF DROP on... */
3456 gdk_drop_reply (info->proxy_dest->context, result, info->proxy_dest->proxy_drop_time);
3458 gdk_drag_drop (info->context, info->proxy_dest->proxy_drop_time);
3460 gtk_drag_finish (info->proxy_dest->context, FALSE, FALSE, info->proxy_dest->proxy_drop_time);
3464 gdk_drag_status (info->proxy_dest->context,
3465 event->dnd.context->action,
3470 else if (info->have_grab)
3472 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
3473 event->dnd.context->action,
3475 if (info->cursor != cursor)
3479 pointer = gdk_drag_context_get_device (context);
3480 gdk_device_grab (pointer, gtk_widget_get_window (widget),
3481 GDK_OWNERSHIP_APPLICATION, FALSE,
3482 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3483 cursor, info->grab_time);
3484 info->cursor = cursor;
3487 gtk_drag_add_update_idle (info);
3492 case GDK_DROP_FINISHED:
3493 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, event->dnd.time);
3496 g_assert_not_reached ();
3500 /*************************************************************
3501 * gtk_drag_source_check_selection:
3502 * Check if we've set up handlers/claimed the selection
3503 * for a given drag. If not, add them.
3507 *************************************************************/
3510 gtk_drag_source_check_selection (GtkDragSourceInfo *info,
3516 tmp_list = info->selections;
3519 if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3521 tmp_list = tmp_list->next;
3524 gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3528 info->selections = g_list_prepend (info->selections,
3529 GUINT_TO_POINTER (selection));
3531 tmp_list = info->target_list->list;
3534 GtkTargetPair *pair = tmp_list->data;
3536 gtk_selection_add_target (info->ipc_widget,
3540 tmp_list = tmp_list->next;
3543 if (info->context->protocol == GDK_DRAG_PROTO_MOTIF)
3545 gtk_selection_add_target (info->ipc_widget,
3547 gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS"),
3548 TARGET_MOTIF_SUCCESS);
3549 gtk_selection_add_target (info->ipc_widget,
3551 gdk_atom_intern_static_string ("XmTRANSFER_FAILURE"),
3552 TARGET_MOTIF_FAILURE);
3555 gtk_selection_add_target (info->ipc_widget,
3557 gdk_atom_intern_static_string ("DELETE"),
3561 /*************************************************************
3562 * gtk_drag_drop_finished:
3563 * Clean up from the drag, and display snapback, if necessary.
3569 *************************************************************/
3572 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3573 GtkDragResult result,
3578 success = (result == GTK_DRAG_RESULT_SUCCESS);
3579 gtk_drag_source_release_selections (info, time);
3581 if (info->proxy_dest)
3583 /* The time from the event isn't reliable for Xdnd drags */
3584 gtk_drag_finish (info->proxy_dest->context, success, FALSE,
3585 info->proxy_dest->proxy_drop_time);
3586 gtk_drag_source_info_destroy (info);
3591 g_signal_emit_by_name (info->widget, "drag-failed",
3592 info->context, result, &success);
3596 gtk_drag_source_info_destroy (info);
3600 GtkDragAnim *anim = g_new (GtkDragAnim, 1);
3604 anim->n_steps = MAX (info->cur_x - info->start_x,
3605 info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3606 anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3608 info->cur_screen = gtk_widget_get_screen (info->widget);
3610 if (!info->icon_window)
3611 set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf,
3614 gtk_drag_update_icon (info);
3616 /* Mark the context as dead, so if the destination decides
3617 * to respond really late, we still are OK.
3619 gtk_drag_clear_source_info (info->context);
3620 gdk_threads_add_timeout (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3626 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3629 GdkDisplay *display = gtk_widget_get_display (info->widget);
3630 GList *tmp_list = info->selections;
3634 GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3635 if (gdk_selection_owner_get_for_display (display, selection) == gtk_widget_get_window (info->ipc_widget))
3636 gtk_selection_owner_set_for_display (display, NULL, selection, time);
3638 tmp_list = tmp_list->next;
3641 g_list_free (info->selections);
3642 info->selections = NULL;
3645 /*************************************************************
3647 * Send a drop event.
3651 *************************************************************/
3654 gtk_drag_drop (GtkDragSourceInfo *info,
3657 if (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN)
3659 GtkSelectionData selection_data;
3661 /* GTK+ traditionally has used application/x-rootwin-drop, but the
3662 * XDND spec specifies x-rootwindow-drop.
3664 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3665 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3667 tmp_list = info->target_list->list;
3670 GtkTargetPair *pair = tmp_list->data;
3672 if (pair->target == target1 || pair->target == target2)
3674 selection_data.selection = GDK_NONE;
3675 selection_data.target = pair->target;
3676 selection_data.data = NULL;
3677 selection_data.length = -1;
3679 g_signal_emit_by_name (info->widget, "drag-data-get",
3680 info->context, &selection_data,
3684 /* FIXME: Should we check for length >= 0 here? */
3685 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3688 tmp_list = tmp_list->next;
3690 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3694 if (info->icon_window)
3695 gtk_widget_hide (info->icon_window);
3697 gdk_drag_drop (info->context, time);
3698 info->drop_timeout = gdk_threads_add_timeout (DROP_ABORT_TIME,
3699 gtk_drag_abort_timeout,
3705 * Source side callbacks.
3709 gtk_drag_source_event_cb (GtkWidget *widget,
3713 GtkDragSourceSite *site;
3714 gboolean retval = FALSE;
3715 site = (GtkDragSourceSite *)data;
3717 switch (event->type)
3719 case GDK_BUTTON_PRESS:
3720 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3722 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3723 site->x = event->button.x;
3724 site->y = event->button.y;
3728 case GDK_BUTTON_RELEASE:
3729 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3730 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3733 case GDK_MOTION_NOTIFY:
3734 if (site->state & event->motion.state & site->start_button_mask)
3736 /* FIXME: This is really broken and can leave us
3742 if (site->state & event->motion.state &
3743 GDK_BUTTON1_MASK << (i - 1))
3747 if (gtk_drag_check_threshold (widget, site->x, site->y,
3748 event->motion.x, event->motion.y))
3751 gtk_drag_begin_internal (widget, site, site->target_list,
3760 default: /* hit for 2/3BUTTON_PRESS */
3768 gtk_drag_source_site_destroy (gpointer data)
3770 GtkDragSourceSite *site = data;
3772 if (site->target_list)
3773 gtk_target_list_unref (site->target_list);
3775 gtk_drag_source_unset_icon (site);
3780 gtk_drag_selection_get (GtkWidget *widget,
3781 GtkSelectionData *selection_data,
3786 GtkDragSourceInfo *info = data;
3787 static GdkAtom null_atom = GDK_NONE;
3791 null_atom = gdk_atom_intern_static_string ("NULL");
3796 g_signal_emit_by_name (info->widget,
3799 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3801 case TARGET_MOTIF_SUCCESS:
3802 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3803 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3805 case TARGET_MOTIF_FAILURE:
3806 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3807 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3810 if (info->proxy_dest)
3812 /* This is sort of dangerous and needs to be thought
3815 info->proxy_dest->proxy_data = selection_data;
3816 gtk_drag_get_data (info->widget,
3817 info->proxy_dest->context,
3818 selection_data->target,
3821 info->proxy_dest->proxy_data = NULL;
3825 if (gtk_target_list_find (info->target_list,
3826 selection_data->target,
3829 g_signal_emit_by_name (info->widget, "drag-data-get",
3841 gtk_drag_anim_timeout (gpointer data)
3843 GtkDragAnim *anim = data;
3847 if (anim->step == anim->n_steps)
3849 gtk_drag_source_info_destroy (anim->info);
3856 x = (anim->info->start_x * (anim->step + 1) +
3857 anim->info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3858 y = (anim->info->start_y * (anim->step + 1) +
3859 anim->info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3860 if (anim->info->icon_window)
3862 GtkWidget *icon_window;
3865 gtk_drag_get_icon (anim->info, &icon_window, &hot_x, &hot_y);
3866 gtk_window_move (GTK_WINDOW (icon_window),
3880 gtk_drag_remove_icon (GtkDragSourceInfo *info)
3882 if (info->icon_window)
3884 gtk_widget_hide (info->icon_window);
3885 if (info->destroy_icon)
3886 gtk_widget_destroy (info->icon_window);
3888 if (info->fallback_icon)
3890 gtk_widget_destroy (info->fallback_icon);
3891 info->fallback_icon = NULL;
3894 g_object_unref (info->icon_window);
3895 info->icon_window = NULL;
3900 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
3904 for (i = 0; i < n_drag_cursors; i++)
3906 if (info->drag_cursors[i] != NULL)
3908 gdk_cursor_unref (info->drag_cursors[i]);
3909 info->drag_cursors[i] = NULL;
3913 gtk_drag_remove_icon (info);
3915 if (info->icon_pixbuf)
3917 g_object_unref (info->icon_pixbuf);
3918 info->icon_pixbuf = NULL;
3921 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3922 gtk_drag_grab_broken_event_cb,
3924 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3925 gtk_drag_grab_notify_cb,
3927 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3928 gtk_drag_button_release_cb,
3930 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3933 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3936 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3937 gtk_drag_selection_get,
3940 if (!info->proxy_dest)
3941 g_signal_emit_by_name (info->widget, "drag-end", info->context);
3944 g_object_unref (info->widget);
3946 gtk_selection_remove_all (info->ipc_widget);
3947 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), NULL);
3948 source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
3949 gtk_drag_release_ipc_widget (info->ipc_widget);
3951 gtk_target_list_unref (info->target_list);
3953 gtk_drag_clear_source_info (info->context);
3954 g_object_unref (info->context);
3956 if (info->drop_timeout)
3957 g_source_remove (info->drop_timeout);
3959 if (info->update_idle)
3960 g_source_remove (info->update_idle);
3966 gtk_drag_update_idle (gpointer data)
3968 GtkDragSourceInfo *info = data;
3969 GdkWindow *dest_window;
3970 GdkDragProtocol protocol;
3973 GdkDragAction action;
3974 GdkDragAction possible_actions;
3977 info->update_idle = 0;
3979 if (info->last_event)
3981 time = gtk_drag_get_event_time (info->last_event);
3982 gtk_drag_get_event_actions (info->last_event,
3984 info->possible_actions,
3985 &action, &possible_actions);
3986 gtk_drag_update_icon (info);
3987 gdk_drag_find_window_for_screen (info->context,
3988 info->icon_window ? gtk_widget_get_window (info->icon_window) : NULL,
3989 info->cur_screen, info->cur_x, info->cur_y,
3990 &dest_window, &protocol);
3992 if (!gdk_drag_motion (info->context, dest_window, protocol,
3993 info->cur_x, info->cur_y, action,
3997 gdk_event_free ((GdkEvent *)info->last_event);
3998 info->last_event = NULL;
4002 g_object_unref (dest_window);
4004 selection = gdk_drag_get_selection (info->context);
4006 gtk_drag_source_check_selection (info, selection, time);
4014 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
4016 /* We use an idle lower than GDK_PRIORITY_REDRAW so that exposes
4017 * from the last move can catch up before we move again.
4019 if (!info->update_idle)
4020 info->update_idle = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW + 5,
4021 gtk_drag_update_idle,
4028 * @info: DragSourceInfo for the drag
4029 * @screen: new screen
4030 * @x_root: new X position
4031 * @y_root: new y position
4032 * @event: event received requiring update
4034 * Updates the status of the drag; called when the
4035 * cursor moves or the modifier changes
4038 gtk_drag_update (GtkDragSourceInfo *info,
4044 info->cur_screen = screen;
4045 info->cur_x = x_root;
4046 info->cur_y = y_root;
4047 if (info->last_event)
4049 gdk_event_free ((GdkEvent *)info->last_event);
4050 info->last_event = NULL;
4053 info->last_event = gdk_event_copy ((GdkEvent *)event);
4055 gtk_drag_add_update_idle (info);
4058 /*************************************************************
4060 * Called when the user finishes to drag, either by
4061 * releasing the mouse, or by pressing Esc.
4063 * info: Source info for the drag
4064 * time: Timestamp for ending the drag
4066 *************************************************************/
4069 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
4071 GdkEvent *send_event;
4072 GtkWidget *source_widget = info->widget;
4073 GdkDevice *pointer, *keyboard;
4075 pointer = gdk_drag_context_get_device (info->context);
4076 keyboard = gdk_device_get_associated_device (pointer);
4078 /* Prevent ungrab before grab (see bug 623865) */
4079 if (info->grab_time == GDK_CURRENT_TIME)
4080 time = GDK_CURRENT_TIME;
4082 if (info->update_idle)
4084 g_source_remove (info->update_idle);
4085 info->update_idle = 0;
4088 if (info->last_event)
4090 gdk_event_free (info->last_event);
4091 info->last_event = NULL;
4094 info->have_grab = FALSE;
4096 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4097 gtk_drag_grab_broken_event_cb,
4099 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4100 gtk_drag_grab_notify_cb,
4102 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4103 gtk_drag_button_release_cb,
4105 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4108 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4112 gdk_device_ungrab (pointer, time);
4113 ungrab_dnd_keys (info->ipc_widget, keyboard, time);
4114 gtk_device_grab_remove (info->ipc_widget, pointer);
4116 /* Send on a release pair to the original
4117 * widget to convince it to release its grab. We need to
4118 * call gtk_propagate_event() here, instead of
4119 * gtk_widget_event() because widget like GtkList may
4120 * expect propagation.
4123 send_event = gdk_event_new (GDK_BUTTON_RELEASE);
4124 send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
4125 send_event->button.send_event = TRUE;
4126 send_event->button.time = time;
4127 send_event->button.x = 0;
4128 send_event->button.y = 0;
4129 send_event->button.axes = NULL;
4130 send_event->button.state = 0;
4131 send_event->button.button = info->button;
4132 send_event->button.device = pointer;
4133 send_event->button.x_root = 0;
4134 send_event->button.y_root = 0;
4136 gtk_propagate_event (source_widget, send_event);
4137 gdk_event_free (send_event);
4140 /*************************************************************
4142 * Called on cancellation of a drag, either by the user
4143 * or programmatically.
4145 * info: Source info for the drag
4146 * time: Timestamp for ending the drag
4148 *************************************************************/
4151 gtk_drag_cancel (GtkDragSourceInfo *info, GtkDragResult result, guint32 time)
4153 gtk_drag_end (info, time);
4154 gdk_drag_abort (info->context, time);
4155 gtk_drag_drop_finished (info, result, time);
4158 /*************************************************************
4159 * gtk_drag_motion_cb:
4160 * "motion-notify-event" callback during drag.
4164 *************************************************************/
4167 gtk_drag_motion_cb (GtkWidget *widget,
4168 GdkEventMotion *event,
4171 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4173 gint x_root, y_root;
4177 GdkDisplay *display = gtk_widget_get_display (widget);
4179 gdk_display_get_device_state (display, event->device,
4180 &screen, &x_root, &y_root, NULL);
4181 event->x_root = x_root;
4182 event->y_root = y_root;
4185 screen = gdk_event_get_screen ((GdkEvent *)event);
4187 gtk_drag_update (info, screen, event->x_root, event->y_root, (GdkEvent *) event);
4192 /*************************************************************
4194 * "key-press/release-event" callback during drag.
4198 *************************************************************/
4201 #define SMALL_STEP 1
4204 gtk_drag_key_cb (GtkWidget *widget,
4208 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4209 GdkModifierType state;
4210 GdkWindow *root_window;
4215 state = event->state & gtk_accelerator_get_default_mod_mask ();
4216 pointer = gdk_device_get_associated_device (gdk_event_get_device ((GdkEvent *) event));
4218 if (event->type == GDK_KEY_PRESS)
4220 switch (event->keyval)
4222 case GDK_KEY_Escape:
4223 gtk_drag_cancel (info, GTK_DRAG_RESULT_USER_CANCELLED, event->time);
4227 case GDK_KEY_Return:
4228 case GDK_KEY_ISO_Enter:
4229 case GDK_KEY_KP_Enter:
4230 case GDK_KEY_KP_Space:
4231 gtk_drag_end (info, event->time);
4232 gtk_drag_drop (info, event->time);
4237 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4241 case GDK_KEY_KP_Down:
4242 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4246 case GDK_KEY_KP_Left:
4247 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4251 case GDK_KEY_KP_Right:
4252 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4258 /* Now send a "motion" so that the modifier state is updated */
4260 /* The state is not yet updated in the event, so we need
4261 * to query it here. We could use XGetModifierMapping, but
4262 * that would be overkill.
4264 root_window = gtk_widget_get_root_window (widget);
4265 gdk_window_get_device_position (root_window, pointer, NULL, NULL, &state);
4266 event->state = state;
4268 if (dx != 0 || dy != 0)
4272 gdk_display_warp_device (gtk_widget_get_display (widget), pointer,
4273 gtk_widget_get_screen (widget),
4274 info->cur_x, info->cur_y);
4277 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
4283 gtk_drag_grab_broken_event_cb (GtkWidget *widget,
4284 GdkEventGrabBroken *event,
4287 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4289 /* Don't cancel if we break the implicit grab from the initial button_press.
4290 * Also, don't cancel if we re-grab on the widget or on our IPC window, for
4291 * example, when changing the drag cursor.
4294 || event->grab_window == gtk_widget_get_window (info->widget)
4295 || event->grab_window == gtk_widget_get_window (info->ipc_widget))
4298 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4303 gtk_drag_grab_notify_cb (GtkWidget *widget,
4304 gboolean was_grabbed,
4307 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4310 pointer = gdk_drag_context_get_device (info->context);
4312 if (gtk_widget_device_is_shadowed (widget, pointer))
4314 /* We have to block callbacks to avoid recursion here, because
4315 gtk_drag_cancel calls gtk_grab_remove (via gtk_drag_end) */
4316 g_signal_handlers_block_by_func (widget, gtk_drag_grab_notify_cb, data);
4317 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4318 g_signal_handlers_unblock_by_func (widget, gtk_drag_grab_notify_cb, data);
4323 /*************************************************************
4324 * gtk_drag_button_release_cb:
4325 * "button-release-event" callback during drag.
4329 *************************************************************/
4332 gtk_drag_button_release_cb (GtkWidget *widget,
4333 GdkEventButton *event,
4336 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4338 if (event->button != info->button)
4341 if ((info->context->action != 0) && (info->context->dest_window != NULL))
4343 gtk_drag_end (info, event->time);
4344 gtk_drag_drop (info, event->time);
4348 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4355 gtk_drag_abort_timeout (gpointer data)
4357 GtkDragSourceInfo *info = data;
4358 guint32 time = GDK_CURRENT_TIME;
4360 if (info->proxy_dest)
4361 time = info->proxy_dest->proxy_drop_time;
4363 info->drop_timeout = 0;
4364 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_TIMEOUT_EXPIRED, time);
4370 * gtk_drag_check_threshold:
4371 * @widget: a #GtkWidget
4372 * @start_x: X coordinate of start of drag
4373 * @start_y: Y coordinate of start of drag
4374 * @current_x: current X coordinate
4375 * @current_y: current Y coordinate
4377 * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
4378 * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
4379 * should trigger the beginning of a drag-and-drop operation.
4381 * Return Value: %TRUE if the drag threshold has been passed.
4384 gtk_drag_check_threshold (GtkWidget *widget,
4390 gint drag_threshold;
4392 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4394 g_object_get (gtk_widget_get_settings (widget),
4395 "gtk-dnd-drag-threshold", &drag_threshold,
4398 return (ABS (current_x - start_x) > drag_threshold ||
4399 ABS (current_y - start_y) > drag_threshold);