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),
1013 * gtk_drag_get_source_widget:
1014 * @context: a (destination side) drag context
1016 * Determines the source widget for a drag.
1018 * Return value: (transfer none): if the drag is occurring
1019 * within a single application, a pointer to the source widget.
1023 gtk_drag_get_source_widget (GdkDragContext *context)
1027 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
1028 g_return_val_if_fail (!context->is_source, NULL);
1030 tmp_list = source_widgets;
1033 GtkWidget *ipc_widget = tmp_list->data;
1035 if (gtk_widget_get_window (ipc_widget) == context->source_window)
1037 GtkDragSourceInfo *info;
1038 info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
1040 return info ? info->widget : NULL;
1043 tmp_list = tmp_list->next;
1049 /*************************************************************
1051 * Notify the drag source that the transfer of data
1054 * context: The drag context for this drag
1055 * success: Was the data successfully transferred?
1056 * time: The timestamp to use when notifying the destination.
1058 *************************************************************/
1061 gtk_drag_finish (GdkDragContext *context,
1066 GdkAtom target = GDK_NONE;
1068 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1069 g_return_if_fail (!context->is_source);
1073 target = gdk_atom_intern_static_string ("DELETE");
1075 else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
1077 target = gdk_atom_intern_static_string (success ?
1078 "XmTRANSFER_SUCCESS" :
1079 "XmTRANSFER_FAILURE");
1082 if (target != GDK_NONE)
1084 GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_drawable_get_screen (context->source_window));
1086 g_object_ref (context);
1088 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1089 g_signal_connect (selection_widget, "selection-received",
1090 G_CALLBACK (gtk_drag_selection_received),
1093 gtk_selection_convert (selection_widget,
1094 gdk_drag_get_selection (context),
1099 if (!(success && del))
1100 gdk_drop_finish (context, success, time);
1103 /*************************************************************
1104 * gtk_drag_highlight_expose:
1105 * Callback for expose_event for highlighted widgets.
1111 *************************************************************/
1114 gtk_drag_highlight_expose (GtkWidget *widget,
1115 GdkEventExpose *event,
1118 gint x, y, width, height;
1120 if (gtk_widget_is_drawable (widget))
1125 window = gtk_widget_get_window (widget);
1127 if (!gtk_widget_get_has_window (widget))
1129 GtkAllocation allocation;
1131 gtk_widget_get_allocation (widget, &allocation);
1134 width = allocation.width;
1135 height = allocation.height;
1141 gdk_drawable_get_size (window, &width, &height);
1144 gtk_paint_shadow (gtk_widget_get_style (widget), window,
1145 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1146 &event->area, widget, "dnd",
1147 x, y, width, height);
1149 cr = gdk_cairo_create (window);
1150 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
1151 cairo_set_line_width (cr, 1.0);
1152 cairo_rectangle (cr,
1154 width - 1, height - 1);
1162 /*************************************************************
1163 * gtk_drag_highlight:
1164 * Highlight the given widget in the default manner.
1168 *************************************************************/
1171 gtk_drag_highlight (GtkWidget *widget)
1173 g_return_if_fail (GTK_IS_WIDGET (widget));
1175 g_signal_connect_after (widget, "expose-event",
1176 G_CALLBACK (gtk_drag_highlight_expose),
1179 gtk_widget_queue_draw (widget);
1182 /*************************************************************
1183 * gtk_drag_unhighlight:
1184 * Refresh the given widget to remove the highlight.
1188 *************************************************************/
1191 gtk_drag_unhighlight (GtkWidget *widget)
1193 g_return_if_fail (GTK_IS_WIDGET (widget));
1195 g_signal_handlers_disconnect_by_func (widget,
1196 gtk_drag_highlight_expose,
1199 gtk_widget_queue_draw (widget);
1203 gtk_drag_dest_set_internal (GtkWidget *widget,
1204 GtkDragDestSite *site)
1206 GtkDragDestSite *old_site;
1208 g_return_if_fail (widget != NULL);
1210 /* HACK, do this in the destroy */
1211 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1214 g_signal_handlers_disconnect_by_func (widget,
1215 gtk_drag_dest_realized,
1217 g_signal_handlers_disconnect_by_func (widget,
1218 gtk_drag_dest_hierarchy_changed,
1221 site->track_motion = old_site->track_motion;
1224 if (gtk_widget_get_realized (widget))
1225 gtk_drag_dest_realized (widget);
1227 g_signal_connect (widget, "realize",
1228 G_CALLBACK (gtk_drag_dest_realized), site);
1229 g_signal_connect (widget, "hierarchy-changed",
1230 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1232 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
1233 site, gtk_drag_dest_site_destroy);
1237 * gtk_drag_dest_set:
1238 * @widget: a #GtkWidget
1239 * @flags: which types of default drag behavior to use
1240 * @targets: (allow-none) (array length=n_targets): a pointer to an array of #GtkTargetEntry<!-- -->s
1241 * indicating the drop types that this @widget will accept, or %NULL.
1242 * Later you can access the list with gtk_drag_dest_get_target_list()
1243 * and gtk_drag_dest_find_target().
1244 * @n_targets: the number of entries in @targets
1245 * @actions: a bitmask of possible actions for a drop onto this @widget.
1247 * Sets a widget as a potential drop destination, and adds default behaviors.
1249 * The default behaviors listed in @flags have an effect similar
1250 * to installing default handlers for the widget's drag-and-drop signals
1251 * (#GtkWidget:drag-motion, #GtkWidget:drag-drop, ...). They all exist
1252 * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
1253 * sufficient to connect to the widget's #GtkWidget::drag-data-received
1254 * signal to get primitive, but consistent drag-and-drop support.
1256 * Things become more complicated when you try to preview the dragged data,
1257 * as described in the documentation for #GtkWidget:drag-motion. The default
1258 * behaviors described by @flags make some assumptions, that can conflict
1259 * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
1260 * invokations of gdk_drag_status() in the context of #GtkWidget:drag-motion,
1261 * and invokations of gtk_drag_finish() in #GtkWidget:drag-data-received.
1262 * Especially the later is dramatic, when your own #GtkWidget:drag-motion
1263 * handler calls gtk_drag_get_data() to inspect the dragged data.
1265 * There's no way to set a default action here, you can use the
1266 * #GtkWidget:drag-motion callback for that. Here's an example which selects
1267 * the action to use depending on whether the control key is pressed or not:
1270 * drag_motion (GtkWidget *widget,
1271 * GdkDragContext *context,
1276 * GdkModifierType mask;
1278 * gdk_window_get_pointer (gtk_widget_get_window (widget),
1279 * NULL, NULL, &mask);
1280 * if (mask & GDK_CONTROL_MASK)
1281 * gdk_drag_status (context, GDK_ACTION_COPY, time);
1283 * gdk_drag_status (context, GDK_ACTION_MOVE, time);
1288 gtk_drag_dest_set (GtkWidget *widget,
1289 GtkDestDefaults flags,
1290 const GtkTargetEntry *targets,
1292 GdkDragAction actions)
1294 GtkDragDestSite *site;
1296 g_return_if_fail (GTK_IS_WIDGET (widget));
1298 site = g_new (GtkDragDestSite, 1);
1300 site->flags = flags;
1301 site->have_drag = FALSE;
1303 site->target_list = gtk_target_list_new (targets, n_targets);
1305 site->target_list = NULL;
1306 site->actions = actions;
1307 site->do_proxy = FALSE;
1308 site->proxy_window = NULL;
1309 site->track_motion = FALSE;
1311 gtk_drag_dest_set_internal (widget, site);
1314 /*************************************************************
1315 * gtk_drag_dest_set_proxy:
1316 * Set up this widget to proxy drags elsewhere.
1319 * proxy_window: window to which forward drag events
1320 * protocol: Drag protocol which the dest widget accepts
1321 * use_coordinates: If true, send the same coordinates to the
1322 * destination, because it is a embedded
1325 *************************************************************/
1328 gtk_drag_dest_set_proxy (GtkWidget *widget,
1329 GdkWindow *proxy_window,
1330 GdkDragProtocol protocol,
1331 gboolean use_coordinates)
1333 GtkDragDestSite *site;
1335 g_return_if_fail (GTK_IS_WIDGET (widget));
1336 g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1338 site = g_new (GtkDragDestSite, 1);
1341 site->have_drag = FALSE;
1342 site->target_list = NULL;
1344 site->proxy_window = proxy_window;
1346 g_object_ref (proxy_window);
1347 site->do_proxy = TRUE;
1348 site->proxy_protocol = protocol;
1349 site->proxy_coords = use_coordinates;
1350 site->track_motion = FALSE;
1352 gtk_drag_dest_set_internal (widget, site);
1355 /*************************************************************
1356 * gtk_drag_dest_unset
1357 * Unregister this widget as a drag target.
1361 *************************************************************/
1364 gtk_drag_dest_unset (GtkWidget *widget)
1366 GtkDragDestSite *old_site;
1368 g_return_if_fail (GTK_IS_WIDGET (widget));
1370 old_site = g_object_get_data (G_OBJECT (widget),
1374 g_signal_handlers_disconnect_by_func (widget,
1375 gtk_drag_dest_realized,
1377 g_signal_handlers_disconnect_by_func (widget,
1378 gtk_drag_dest_hierarchy_changed,
1382 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1386 * gtk_drag_dest_get_target_list:
1387 * @widget: a #GtkWidget
1389 * Returns the list of targets this widget can accept from
1392 * Return value: the #GtkTargetList, or %NULL if none
1395 gtk_drag_dest_get_target_list (GtkWidget *widget)
1397 GtkDragDestSite *site;
1399 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1401 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1403 return site ? site->target_list : NULL;
1407 * gtk_drag_dest_set_target_list:
1408 * @widget: a #GtkWidget that's a drag destination
1409 * @target_list: (allow-none): list of droppable targets, or %NULL for none
1411 * Sets the target types that this widget can accept from drag-and-drop.
1412 * The widget must first be made into a drag destination with
1413 * gtk_drag_dest_set().
1416 gtk_drag_dest_set_target_list (GtkWidget *widget,
1417 GtkTargetList *target_list)
1419 GtkDragDestSite *site;
1421 g_return_if_fail (GTK_IS_WIDGET (widget));
1423 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1427 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1428 "to make the widget into a drag destination");
1433 gtk_target_list_ref (target_list);
1435 if (site->target_list)
1436 gtk_target_list_unref (site->target_list);
1438 site->target_list = target_list;
1442 * gtk_drag_dest_add_text_targets:
1443 * @widget: a #GtkWidget that's a drag destination
1445 * Add the text targets supported by #GtkSelection to
1446 * the target list of the drag destination. The targets
1447 * are added with @info = 0. If you need another value,
1448 * use gtk_target_list_add_text_targets() and
1449 * gtk_drag_dest_set_target_list().
1454 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1456 GtkTargetList *target_list;
1458 target_list = gtk_drag_dest_get_target_list (widget);
1460 gtk_target_list_ref (target_list);
1462 target_list = gtk_target_list_new (NULL, 0);
1463 gtk_target_list_add_text_targets (target_list, 0);
1464 gtk_drag_dest_set_target_list (widget, target_list);
1465 gtk_target_list_unref (target_list);
1469 * gtk_drag_dest_add_image_targets:
1470 * @widget: a #GtkWidget that's a drag destination
1472 * Add the image targets supported by #GtkSelection to
1473 * the target list of the drag destination. The targets
1474 * are added with @info = 0. If you need another value,
1475 * use gtk_target_list_add_image_targets() and
1476 * gtk_drag_dest_set_target_list().
1481 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1483 GtkTargetList *target_list;
1485 target_list = gtk_drag_dest_get_target_list (widget);
1487 gtk_target_list_ref (target_list);
1489 target_list = gtk_target_list_new (NULL, 0);
1490 gtk_target_list_add_image_targets (target_list, 0, FALSE);
1491 gtk_drag_dest_set_target_list (widget, target_list);
1492 gtk_target_list_unref (target_list);
1496 * gtk_drag_dest_add_uri_targets:
1497 * @widget: a #GtkWidget that's a drag destination
1499 * Add the URI targets supported by #GtkSelection to
1500 * the target list of the drag destination. The targets
1501 * are added with @info = 0. If you need another value,
1502 * use gtk_target_list_add_uri_targets() and
1503 * gtk_drag_dest_set_target_list().
1508 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1510 GtkTargetList *target_list;
1512 target_list = gtk_drag_dest_get_target_list (widget);
1514 gtk_target_list_ref (target_list);
1516 target_list = gtk_target_list_new (NULL, 0);
1517 gtk_target_list_add_uri_targets (target_list, 0);
1518 gtk_drag_dest_set_target_list (widget, target_list);
1519 gtk_target_list_unref (target_list);
1523 * gtk_drag_dest_set_track_motion:
1524 * @widget: a #GtkWidget that's a drag destination
1525 * @track_motion: whether to accept all targets
1527 * Tells the widget to emit ::drag-motion and ::drag-leave
1528 * events regardless of the targets and the %GTK_DEST_DEFAULT_MOTION
1531 * This may be used when a widget wants to do generic
1532 * actions regardless of the targets that the source offers.
1537 gtk_drag_dest_set_track_motion (GtkWidget *widget,
1538 gboolean track_motion)
1540 GtkDragDestSite *site;
1542 g_return_if_fail (GTK_IS_WIDGET (widget));
1544 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1546 g_return_if_fail (site != NULL);
1548 site->track_motion = track_motion != FALSE;
1552 * gtk_drag_dest_get_track_motion:
1553 * @widget: a #GtkWidget that's a drag destination
1555 * Returns whether the widget has been configured to always
1556 * emit ::drag-motion signals.
1558 * Return Value: %TRUE if the widget always emits ::drag-motion events
1563 gtk_drag_dest_get_track_motion (GtkWidget *widget)
1565 GtkDragDestSite *site;
1567 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1569 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1572 return site->track_motion;
1577 /*************************************************************
1578 * _gtk_drag_dest_handle_event:
1579 * Called from widget event handling code on Drag events
1583 * toplevel: Toplevel widget that received the event
1586 *************************************************************/
1589 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1592 GtkDragDestInfo *info;
1593 GdkDragContext *context;
1595 g_return_if_fail (toplevel != NULL);
1596 g_return_if_fail (event != NULL);
1598 context = event->dnd.context;
1600 info = gtk_drag_get_dest_info (context, TRUE);
1602 /* Find the widget for the event */
1603 switch (event->type)
1605 case GDK_DRAG_ENTER:
1608 case GDK_DRAG_LEAVE:
1611 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1612 info->widget = NULL;
1616 case GDK_DRAG_MOTION:
1617 case GDK_DROP_START:
1623 if (event->type == GDK_DROP_START)
1625 info->dropped = TRUE;
1626 /* We send a leave here so that the widget unhighlights
1631 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1632 info->widget = NULL;
1636 window = gtk_widget_get_window (toplevel);
1638 #ifdef GDK_WINDOWING_X11
1639 /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1641 * Currently gdk_window_get_position doesn't provide reliable
1642 * information for embedded windows, so we call the much more
1643 * expensive gdk_window_get_origin().
1645 if (GTK_IS_PLUG (toplevel))
1646 gdk_window_get_origin (window, &tx, &ty);
1648 #endif /* GDK_WINDOWING_X11 */
1649 gdk_window_get_position (window, &tx, &ty);
1651 found = gtk_drag_find_widget (toplevel,
1654 event->dnd.x_root - tx,
1655 event->dnd.y_root - ty,
1657 (event->type == GDK_DRAG_MOTION) ?
1658 gtk_drag_dest_motion :
1659 gtk_drag_dest_drop);
1661 if (info->widget && !found)
1663 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1664 info->widget = NULL;
1669 if (event->type == GDK_DRAG_MOTION)
1672 gdk_drag_status (context, 0, event->dnd.time);
1674 else if (event->type == GDK_DROP_START && !info->proxy_source)
1676 gdk_drop_reply (context, found, event->dnd.time);
1677 if ((context->protocol == GDK_DRAG_PROTO_MOTIF) && !found)
1678 gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1684 g_assert_not_reached ();
1689 * gtk_drag_dest_find_target:
1690 * @widget: drag destination widget
1691 * @context: drag context
1692 * @target_list: (allow-none): list of droppable targets, or %NULL to use
1693 * gtk_drag_dest_get_target_list (@widget).
1695 * Looks for a match between @context->targets and the
1696 * @dest_target_list, returning the first matching target, otherwise
1697 * returning %GDK_NONE. @dest_target_list should usually be the return
1698 * value from gtk_drag_dest_get_target_list(), but some widgets may
1699 * have different valid targets for different parts of the widget; in
1700 * that case, they will have to implement a drag_motion handler that
1701 * passes the correct target list to this function.
1703 * Return value: first target that the source offers and the dest can accept, or %GDK_NONE
1706 gtk_drag_dest_find_target (GtkWidget *widget,
1707 GdkDragContext *context,
1708 GtkTargetList *target_list)
1711 GList *tmp_source = NULL;
1712 GtkWidget *source_widget;
1714 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1715 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1716 g_return_val_if_fail (!context->is_source, GDK_NONE);
1719 source_widget = gtk_drag_get_source_widget (context);
1721 if (target_list == NULL)
1722 target_list = gtk_drag_dest_get_target_list (widget);
1724 if (target_list == NULL)
1727 tmp_target = target_list->list;
1730 GtkTargetPair *pair = tmp_target->data;
1731 tmp_source = context->targets;
1734 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1736 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1737 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)) &&
1738 (!(pair->flags & GTK_TARGET_OTHER_APP) || !source_widget) &&
1739 (!(pair->flags & GTK_TARGET_OTHER_WIDGET) || (source_widget != widget)))
1740 return pair->target;
1744 tmp_source = tmp_source->next;
1746 tmp_target = tmp_target->next;
1753 gtk_drag_selection_received (GtkWidget *widget,
1754 GtkSelectionData *selection_data,
1758 GdkDragContext *context;
1759 GtkDragDestInfo *info;
1760 GtkWidget *drop_widget;
1764 context = g_object_get_data (G_OBJECT (widget), "drag-context");
1765 info = gtk_drag_get_dest_info (context, FALSE);
1767 if (info->proxy_data &&
1768 info->proxy_data->target == selection_data->target)
1770 gtk_selection_data_set (info->proxy_data,
1771 selection_data->type,
1772 selection_data->format,
1773 selection_data->data,
1774 selection_data->length);
1779 if (selection_data->target == gdk_atom_intern_static_string ("DELETE"))
1781 gtk_drag_finish (context, TRUE, FALSE, time);
1783 else if ((selection_data->target == gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS")) ||
1784 (selection_data->target == gdk_atom_intern_static_string ("XmTRANSFER_FAILURE")))
1790 GtkDragDestSite *site;
1792 site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1794 if (site && site->target_list)
1798 if (gtk_target_list_find (site->target_list,
1799 selection_data->target,
1802 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1803 selection_data->length >= 0)
1804 g_signal_emit_by_name (drop_widget,
1805 "drag-data-received",
1806 context, info->drop_x, info->drop_y,
1813 g_signal_emit_by_name (drop_widget,
1814 "drag-data-received",
1815 context, info->drop_x, info->drop_y,
1820 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1823 gtk_drag_finish (context,
1824 (selection_data->length >= 0),
1825 (context->action == GDK_ACTION_MOVE),
1829 g_object_unref (drop_widget);
1832 g_signal_handlers_disconnect_by_func (widget,
1833 gtk_drag_selection_received,
1836 g_object_set_data (G_OBJECT (widget), I_("drag-context"), NULL);
1837 g_object_unref (context);
1839 gtk_drag_release_ipc_widget (widget);
1842 /*************************************************************
1843 * gtk_drag_find_widget:
1844 * Function used to locate widgets for
1845 * DRAG_MOTION and DROP_START events.
1846 *************************************************************/
1849 gtk_drag_find_widget (GtkWidget *widget,
1850 GdkDragContext *context,
1851 GtkDragDestInfo *info,
1855 GtkDragDestCallback callback)
1857 if (!gtk_widget_get_mapped (widget) ||
1858 !gtk_widget_get_sensitive (widget))
1861 /* Get the widget at the pointer coordinates and travel up
1862 * the widget hierarchy from there.
1864 widget = _gtk_widget_find_at_coords (gtk_widget_get_window (widget),
1872 GList *hierarchy = NULL;
1873 gboolean found = FALSE;
1875 if (!gtk_widget_get_mapped (widget) ||
1876 !gtk_widget_get_sensitive (widget))
1879 /* need to reference the entire hierarchy temporarily in case the
1880 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
1882 for (parent = widget;
1884 parent = gtk_widget_get_parent (parent))
1886 hierarchy = g_list_prepend (hierarchy, g_object_ref (parent));
1889 /* If the current widget is registered as a drop site, check to
1890 * emit "drag-motion" to check if we are actually in a drop
1893 if (g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1895 found = callback (widget, context, x, y, time);
1897 /* If so, send a "drag-leave" to the last widget */
1900 if (info->widget && info->widget != widget)
1902 gtk_drag_dest_leave (info->widget, context, time);
1905 info->widget = widget;
1911 /* Get the parent before unreffing the hierarchy because
1912 * invoking the callback might have destroyed the widget
1914 parent = gtk_widget_get_parent (widget);
1916 /* The parent might be going away when unreffing the
1917 * hierarchy, so also protect againt that
1920 g_object_add_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1923 g_list_foreach (hierarchy, (GFunc) g_object_unref, NULL);
1924 g_list_free (hierarchy);
1930 g_object_remove_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1934 if (!gtk_widget_translate_coordinates (widget, parent, x, y, &x, &y))
1944 gtk_drag_proxy_begin (GtkWidget *widget,
1945 GtkDragDestInfo *dest_info,
1948 GtkDragSourceInfo *source_info;
1950 GdkDragContext *context;
1951 GtkWidget *ipc_widget;
1953 if (dest_info->proxy_source)
1955 gdk_drag_abort (dest_info->proxy_source->context, time);
1956 gtk_drag_source_info_destroy (dest_info->proxy_source);
1957 dest_info->proxy_source = NULL;
1960 ipc_widget = gtk_drag_get_ipc_widget (widget);
1961 context = gdk_drag_begin (gtk_widget_get_window (ipc_widget),
1962 dest_info->context->targets);
1964 source_info = gtk_drag_get_source_info (context, TRUE);
1966 source_info->ipc_widget = ipc_widget;
1967 source_info->widget = g_object_ref (widget);
1969 source_info->target_list = gtk_target_list_new (NULL, 0);
1970 tmp_list = dest_info->context->targets;
1973 gtk_target_list_add (source_info->target_list,
1974 GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
1975 tmp_list = tmp_list->next;
1978 source_info->proxy_dest = dest_info;
1980 g_signal_connect (ipc_widget,
1982 G_CALLBACK (gtk_drag_selection_get),
1985 dest_info->proxy_source = source_info;
1989 gtk_drag_dest_info_destroy (gpointer data)
1991 GtkDragDestInfo *info = data;
1996 static GtkDragDestInfo *
1997 gtk_drag_get_dest_info (GdkDragContext *context,
2000 GtkDragDestInfo *info;
2001 static GQuark info_quark = 0;
2003 info_quark = g_quark_from_static_string ("gtk-dest-info");
2005 info = g_object_get_qdata (G_OBJECT (context), info_quark);
2006 if (!info && create)
2008 info = g_new (GtkDragDestInfo, 1);
2009 info->widget = NULL;
2010 info->context = context;
2011 info->proxy_source = NULL;
2012 info->proxy_data = NULL;
2013 info->dropped = FALSE;
2014 info->proxy_drop_wait = FALSE;
2015 g_object_set_qdata_full (G_OBJECT (context), info_quark,
2016 info, gtk_drag_dest_info_destroy);
2022 static GQuark dest_info_quark = 0;
2024 static GtkDragSourceInfo *
2025 gtk_drag_get_source_info (GdkDragContext *context,
2028 GtkDragSourceInfo *info;
2029 if (!dest_info_quark)
2030 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
2032 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
2033 if (!info && create)
2035 info = g_new0 (GtkDragSourceInfo, 1);
2036 info->context = context;
2037 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
2044 gtk_drag_clear_source_info (GdkDragContext *context)
2046 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
2050 gtk_drag_dest_realized (GtkWidget *widget)
2052 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2054 if (gtk_widget_is_toplevel (toplevel))
2055 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2059 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
2060 GtkWidget *previous_toplevel)
2062 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2064 if (gtk_widget_is_toplevel (toplevel) && gtk_widget_get_realized (toplevel))
2065 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2069 gtk_drag_dest_site_destroy (gpointer data)
2071 GtkDragDestSite *site = data;
2073 if (site->proxy_window)
2074 g_object_unref (site->proxy_window);
2076 if (site->target_list)
2077 gtk_target_list_unref (site->target_list);
2083 * Default drag handlers
2086 gtk_drag_dest_leave (GtkWidget *widget,
2087 GdkDragContext *context,
2090 GtkDragDestSite *site;
2092 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2093 g_return_if_fail (site != NULL);
2097 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2099 if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
2101 gdk_drag_abort (info->proxy_source->context, time);
2102 gtk_drag_source_info_destroy (info->proxy_source);
2103 info->proxy_source = NULL;
2110 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
2111 gtk_drag_unhighlight (widget);
2113 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
2115 g_signal_emit_by_name (widget, "drag-leave", context, time);
2117 site->have_drag = FALSE;
2122 gtk_drag_dest_motion (GtkWidget *widget,
2123 GdkDragContext *context,
2128 GtkDragDestSite *site;
2129 GdkDragAction action = 0;
2132 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2133 g_return_val_if_fail (site != NULL, FALSE);
2138 GdkEvent *current_event;
2139 GdkWindow *dest_window;
2140 GdkDragProtocol proto;
2142 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2144 if (!info->proxy_source || info->proxy_source->widget != widget)
2145 gtk_drag_proxy_begin (widget, info, time);
2147 current_event = gtk_get_current_event ();
2149 if (site->proxy_window)
2151 dest_window = site->proxy_window;
2152 proto = site->proxy_protocol;
2156 gdk_drag_find_window_for_screen (info->proxy_source->context,
2158 gdk_drawable_get_screen (current_event->dnd.window),
2159 current_event->dnd.x_root,
2160 current_event->dnd.y_root,
2161 &dest_window, &proto);
2164 gdk_drag_motion (info->proxy_source->context,
2166 current_event->dnd.x_root,
2167 current_event->dnd.y_root,
2168 context->suggested_action,
2169 context->actions, time);
2171 if (!site->proxy_window && dest_window)
2172 g_object_unref (dest_window);
2174 selection = gdk_drag_get_selection (info->proxy_source->context);
2176 selection != gdk_drag_get_selection (info->context))
2177 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2179 gdk_event_free (current_event);
2184 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
2186 if (context->suggested_action & site->actions)
2187 action = context->suggested_action;
2192 for (i = 0; i < 8; i++)
2194 if ((site->actions & (1 << i)) &&
2195 (context->actions & (1 << i)))
2203 if (action && gtk_drag_dest_find_target (widget, context, NULL))
2205 if (!site->have_drag)
2207 site->have_drag = TRUE;
2208 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
2209 gtk_drag_highlight (widget);
2212 gdk_drag_status (context, action, time);
2216 gdk_drag_status (context, 0, time);
2217 if (!site->track_motion)
2222 g_signal_emit_by_name (widget, "drag-motion",
2223 context, x, y, time, &retval);
2225 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
2229 gtk_drag_dest_drop (GtkWidget *widget,
2230 GdkDragContext *context,
2235 GtkDragDestSite *site;
2236 GtkDragDestInfo *info;
2238 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2239 g_return_val_if_fail (site != NULL, FALSE);
2241 info = gtk_drag_get_dest_info (context, FALSE);
2242 g_return_val_if_fail (info != NULL, FALSE);
2249 if (info->proxy_source ||
2250 (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN))
2252 gtk_drag_drop (info->proxy_source, time);
2256 /* We need to synthesize a motion event, wait for a status,
2257 * and, if we get a good one, do a drop.
2260 GdkEvent *current_event;
2262 GdkWindow *dest_window;
2263 GdkDragProtocol proto;
2265 gtk_drag_proxy_begin (widget, info, time);
2266 info->proxy_drop_wait = TRUE;
2267 info->proxy_drop_time = time;
2269 current_event = gtk_get_current_event ();
2271 if (site->proxy_window)
2273 dest_window = site->proxy_window;
2274 proto = site->proxy_protocol;
2278 gdk_drag_find_window_for_screen (info->proxy_source->context,
2280 gdk_drawable_get_screen (current_event->dnd.window),
2281 current_event->dnd.x_root,
2282 current_event->dnd.y_root,
2283 &dest_window, &proto);
2286 gdk_drag_motion (info->proxy_source->context,
2288 current_event->dnd.x_root,
2289 current_event->dnd.y_root,
2290 context->suggested_action,
2291 context->actions, time);
2293 if (!site->proxy_window && dest_window)
2294 g_object_unref (dest_window);
2296 selection = gdk_drag_get_selection (info->proxy_source->context);
2298 selection != gdk_drag_get_selection (info->context))
2299 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2301 gdk_event_free (current_event);
2310 if (site->flags & GTK_DEST_DEFAULT_DROP)
2312 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2314 if (target == GDK_NONE)
2316 gtk_drag_finish (context, FALSE, FALSE, time);
2320 gtk_drag_get_data (widget, context, target, time);
2323 g_signal_emit_by_name (widget, "drag-drop",
2324 context, x, y, time, &retval);
2326 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2334 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2335 * so that we can set the icon from the source site information
2337 static GdkDragContext *
2338 gtk_drag_begin_internal (GtkWidget *widget,
2339 GtkDragSourceSite *site,
2340 GtkTargetList *target_list,
2341 GdkDragAction actions,
2345 GtkDragSourceInfo *info;
2346 GList *targets = NULL;
2348 guint32 time = GDK_CURRENT_TIME;
2349 GdkDragAction possible_actions, suggested_action;
2350 GdkDragContext *context;
2351 GtkWidget *ipc_widget;
2353 GdkDevice *pointer, *keyboard;
2354 GdkWindow *ipc_window;
2356 pointer = keyboard = NULL;
2357 ipc_widget = gtk_drag_get_ipc_widget (widget);
2359 gtk_drag_get_event_actions (event, button, actions,
2360 &suggested_action, &possible_actions);
2362 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2368 time = gdk_event_get_time (event);
2369 if (time == GDK_CURRENT_TIME)
2370 time = gtk_get_current_event_time ();
2372 pointer = gdk_event_get_device (event);
2374 if (pointer->source == GDK_SOURCE_KEYBOARD)
2377 pointer = gdk_device_get_associated_device (keyboard);
2380 keyboard = gdk_device_get_associated_device (pointer);
2384 GdkDeviceManager *device_manager;
2386 device_manager = gdk_display_get_device_manager (gtk_widget_get_display (widget));
2387 pointer = gdk_device_manager_get_client_pointer (device_manager);
2388 keyboard = gdk_device_get_associated_device (pointer);
2394 ipc_window = gtk_widget_get_window (ipc_widget);
2396 if (gdk_device_grab (pointer, ipc_window,
2397 GDK_OWNERSHIP_APPLICATION, FALSE,
2398 GDK_POINTER_MOTION_MASK |
2399 GDK_BUTTON_RELEASE_MASK,
2400 cursor, time) != GDK_GRAB_SUCCESS)
2402 gtk_drag_release_ipc_widget (ipc_widget);
2407 grab_dnd_keys (ipc_widget, keyboard, time);
2409 /* We use a GTK grab here to override any grabs that the widget
2410 * we are dragging from might have held
2412 gtk_device_grab_add (ipc_widget, pointer, FALSE);
2414 tmp_list = g_list_last (target_list->list);
2417 GtkTargetPair *pair = tmp_list->data;
2418 targets = g_list_prepend (targets,
2419 GINT_TO_POINTER (pair->target));
2420 tmp_list = tmp_list->prev;
2423 source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2425 context = gdk_drag_begin (ipc_window, targets);
2426 gdk_drag_context_set_device (context, pointer);
2427 g_list_free (targets);
2429 info = gtk_drag_get_source_info (context, TRUE);
2431 info->ipc_widget = ipc_widget;
2432 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), info);
2434 info->widget = g_object_ref (widget);
2436 info->button = button;
2437 info->cursor = cursor;
2438 info->target_list = target_list;
2439 gtk_target_list_ref (target_list);
2441 info->possible_actions = actions;
2443 info->status = GTK_DRAG_STATUS_DRAG;
2444 info->last_event = NULL;
2445 info->selections = NULL;
2446 info->icon_window = NULL;
2447 info->destroy_icon = FALSE;
2449 /* Set cur_x, cur_y here so if the "drag-begin" signal shows
2450 * the drag icon, it will be in the right place
2452 if (event && event->type == GDK_MOTION_NOTIFY)
2454 info->cur_screen = gtk_widget_get_screen (widget);
2455 info->cur_x = event->motion.x_root;
2456 info->cur_y = event->motion.y_root;
2460 gdk_display_get_device_state (gtk_widget_get_display (widget), pointer,
2461 &info->cur_screen, &info->cur_x, &info->cur_y, NULL);
2464 g_signal_emit_by_name (widget, "drag-begin", info->context);
2466 /* Ensure that we have an icon before we start the drag; the
2467 * application may have set one in ::drag_begin, or it may
2470 if (!info->icon_window && !info->icon_pixbuf)
2472 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2473 gtk_drag_set_icon_default (context);
2475 switch (site->icon_type)
2477 case GTK_IMAGE_PIXMAP:
2478 gtk_drag_set_icon_pixmap (context,
2480 site->icon_data.pixmap.pixmap,
2484 case GTK_IMAGE_PIXBUF:
2485 gtk_drag_set_icon_pixbuf (context,
2486 site->icon_data.pixbuf.pixbuf,
2489 case GTK_IMAGE_STOCK:
2490 gtk_drag_set_icon_stock (context,
2491 site->icon_data.stock.stock_id,
2494 case GTK_IMAGE_ICON_NAME:
2495 gtk_drag_set_icon_name (context,
2496 site->icon_data.name.icon_name,
2499 case GTK_IMAGE_EMPTY:
2501 g_assert_not_reached();
2506 /* We need to composite the icon into the cursor, if we are
2507 * not using an icon window.
2509 if (info->icon_pixbuf)
2511 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2515 if (cursor != info->cursor)
2517 gdk_device_grab (pointer, gtk_widget_get_window (widget),
2518 GDK_OWNERSHIP_APPLICATION, FALSE,
2519 GDK_POINTER_MOTION_MASK |
2520 GDK_BUTTON_RELEASE_MASK,
2522 info->cursor = cursor;
2526 if (event && event->type == GDK_MOTION_NOTIFY)
2527 gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2529 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, event);
2531 info->start_x = info->cur_x;
2532 info->start_y = info->cur_y;
2534 g_signal_connect (info->ipc_widget, "grab-broken-event",
2535 G_CALLBACK (gtk_drag_grab_broken_event_cb), info);
2536 g_signal_connect (info->ipc_widget, "grab-notify",
2537 G_CALLBACK (gtk_drag_grab_notify_cb), info);
2538 g_signal_connect (info->ipc_widget, "button-release-event",
2539 G_CALLBACK (gtk_drag_button_release_cb), info);
2540 g_signal_connect (info->ipc_widget, "motion-notify-event",
2541 G_CALLBACK (gtk_drag_motion_cb), info);
2542 g_signal_connect (info->ipc_widget, "key-press-event",
2543 G_CALLBACK (gtk_drag_key_cb), info);
2544 g_signal_connect (info->ipc_widget, "key-release-event",
2545 G_CALLBACK (gtk_drag_key_cb), info);
2546 g_signal_connect (info->ipc_widget, "selection-get",
2547 G_CALLBACK (gtk_drag_selection_get), info);
2549 info->have_grab = TRUE;
2550 info->grab_time = time;
2552 return info->context;
2557 * @widget: the source widget.
2558 * @targets: The targets (data formats) in which the
2559 * source can provide the data.
2560 * @actions: A bitmask of the allowed drag actions for this drag.
2561 * @button: The button the user clicked to start the drag.
2562 * @event: The event that triggered the start of the drag.
2564 * Initiates a drag on the source side. The function
2565 * only needs to be used when the application is
2566 * starting drags itself, and is not needed when
2567 * gtk_drag_source_set() is used.
2569 * The @event is used to retrieve the timestamp that will be used internally to
2570 * grab the pointer. If @event is #NULL, then GDK_CURRENT_TIME will be used.
2571 * However, you should try to pass a real event in all cases, since that can be
2572 * used by GTK+ to get information about the start position of the drag, for
2573 * example if the @event is a GDK_MOTION_NOTIFY.
2575 * Generally there are three cases when you want to start a drag by hand by
2576 * calling this function:
2578 * 1. During a button-press-event handler, if you want to start a drag
2579 * immediately when the user presses the mouse button. Pass the @event
2580 * that you have in your button-press-event handler.
2582 * 2. During a motion-notify-event handler, if you want to start a drag
2583 * when the mouse moves past a certain threshold distance after a button-press.
2584 * Pass the @event that you have in your motion-notify-event handler.
2586 * 3. During a timeout handler, if you want to start a drag after the mouse
2587 * button is held down for some time. Try to save the last event that you got
2588 * from the mouse, using gdk_event_copy(), and pass it to this function
2589 * (remember to free the event with gdk_event_free() when you are done).
2590 * If you can really not pass a real event, pass #NULL instead.
2592 * Return value: the context for this drag.
2595 gtk_drag_begin (GtkWidget *widget,
2596 GtkTargetList *targets,
2597 GdkDragAction actions,
2601 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2602 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
2603 g_return_val_if_fail (targets != NULL, NULL);
2605 return gtk_drag_begin_internal (widget, NULL, targets,
2606 actions, button, event);
2610 * gtk_drag_source_set:
2611 * @widget: a #GtkWidget
2612 * @start_button_mask: the bitmask of buttons that can start the drag
2613 * @targets: (allow-none) (array length=n_targets): the table of targets that the drag will support,
2615 * @n_targets: the number of items in @targets
2616 * @actions: the bitmask of possible actions for a drag from this widget
2618 * Sets up a widget so that GTK+ will start a drag operation when the user
2619 * clicks and drags on the widget. The widget must have a window.
2622 gtk_drag_source_set (GtkWidget *widget,
2623 GdkModifierType start_button_mask,
2624 const GtkTargetEntry *targets,
2626 GdkDragAction actions)
2628 GtkDragSourceSite *site;
2630 g_return_if_fail (GTK_IS_WIDGET (widget));
2632 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2634 gtk_widget_add_events (widget,
2635 gtk_widget_get_events (widget) |
2636 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2637 GDK_BUTTON_MOTION_MASK);
2641 if (site->target_list)
2642 gtk_target_list_unref (site->target_list);
2646 site = g_new0 (GtkDragSourceSite, 1);
2648 site->icon_type = GTK_IMAGE_EMPTY;
2650 g_signal_connect (widget, "button-press-event",
2651 G_CALLBACK (gtk_drag_source_event_cb),
2653 g_signal_connect (widget, "button-release-event",
2654 G_CALLBACK (gtk_drag_source_event_cb),
2656 g_signal_connect (widget, "motion-notify-event",
2657 G_CALLBACK (gtk_drag_source_event_cb),
2660 g_object_set_data_full (G_OBJECT (widget),
2661 I_("gtk-site-data"),
2662 site, gtk_drag_source_site_destroy);
2665 site->start_button_mask = start_button_mask;
2667 site->target_list = gtk_target_list_new (targets, n_targets);
2669 site->actions = actions;
2672 /*************************************************************
2673 * gtk_drag_source_unset
2674 * Unregister this widget as a drag source.
2678 *************************************************************/
2681 gtk_drag_source_unset (GtkWidget *widget)
2683 GtkDragSourceSite *site;
2685 g_return_if_fail (GTK_IS_WIDGET (widget));
2687 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2691 g_signal_handlers_disconnect_by_func (widget,
2692 gtk_drag_source_event_cb,
2694 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
2699 * gtk_drag_source_get_target_list:
2700 * @widget: a #GtkWidget
2702 * Gets the list of targets this widget can provide for
2705 * Return value: the #GtkTargetList, or %NULL if none
2710 gtk_drag_source_get_target_list (GtkWidget *widget)
2712 GtkDragSourceSite *site;
2714 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2716 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2718 return site ? site->target_list : NULL;
2722 * gtk_drag_source_set_target_list:
2723 * @widget: a #GtkWidget that's a drag source
2724 * @target_list: (allow-none): list of draggable targets, or %NULL for none
2726 * Changes the target types that this widget offers for drag-and-drop.
2727 * The widget must first be made into a drag source with
2728 * gtk_drag_source_set().
2733 gtk_drag_source_set_target_list (GtkWidget *widget,
2734 GtkTargetList *target_list)
2736 GtkDragSourceSite *site;
2738 g_return_if_fail (GTK_IS_WIDGET (widget));
2740 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2743 g_warning ("gtk_drag_source_set_target_list() requires the widget "
2744 "to already be a drag source.");
2749 gtk_target_list_ref (target_list);
2751 if (site->target_list)
2752 gtk_target_list_unref (site->target_list);
2754 site->target_list = target_list;
2758 * gtk_drag_source_add_text_targets:
2759 * @widget: a #GtkWidget that's is a drag source
2761 * Add the text targets supported by #GtkSelection to
2762 * the target list of the drag source. The targets
2763 * are added with @info = 0. If you need another value,
2764 * use gtk_target_list_add_text_targets() and
2765 * gtk_drag_source_set_target_list().
2770 gtk_drag_source_add_text_targets (GtkWidget *widget)
2772 GtkTargetList *target_list;
2774 target_list = gtk_drag_source_get_target_list (widget);
2776 gtk_target_list_ref (target_list);
2778 target_list = gtk_target_list_new (NULL, 0);
2779 gtk_target_list_add_text_targets (target_list, 0);
2780 gtk_drag_source_set_target_list (widget, target_list);
2781 gtk_target_list_unref (target_list);
2785 * gtk_drag_source_add_image_targets:
2786 * @widget: a #GtkWidget that's is a drag source
2788 * Add the writable image targets supported by #GtkSelection to
2789 * the target list of the drag source. The targets
2790 * are added with @info = 0. If you need another value,
2791 * use gtk_target_list_add_image_targets() and
2792 * gtk_drag_source_set_target_list().
2797 gtk_drag_source_add_image_targets (GtkWidget *widget)
2799 GtkTargetList *target_list;
2801 target_list = gtk_drag_source_get_target_list (widget);
2803 gtk_target_list_ref (target_list);
2805 target_list = gtk_target_list_new (NULL, 0);
2806 gtk_target_list_add_image_targets (target_list, 0, TRUE);
2807 gtk_drag_source_set_target_list (widget, target_list);
2808 gtk_target_list_unref (target_list);
2812 * gtk_drag_source_add_uri_targets:
2813 * @widget: a #GtkWidget that's is a drag source
2815 * Add the URI targets supported by #GtkSelection to
2816 * the target list of the drag source. The targets
2817 * are added with @info = 0. If you need another value,
2818 * use gtk_target_list_add_uri_targets() and
2819 * gtk_drag_source_set_target_list().
2824 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2826 GtkTargetList *target_list;
2828 target_list = gtk_drag_source_get_target_list (widget);
2830 gtk_target_list_ref (target_list);
2832 target_list = gtk_target_list_new (NULL, 0);
2833 gtk_target_list_add_uri_targets (target_list, 0);
2834 gtk_drag_source_set_target_list (widget, target_list);
2835 gtk_target_list_unref (target_list);
2839 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2841 switch (site->icon_type)
2843 case GTK_IMAGE_EMPTY:
2845 case GTK_IMAGE_PIXMAP:
2846 if (site->icon_data.pixmap.pixmap)
2847 g_object_unref (site->icon_data.pixmap.pixmap);
2848 if (site->icon_mask)
2849 g_object_unref (site->icon_mask);
2851 case GTK_IMAGE_PIXBUF:
2852 g_object_unref (site->icon_data.pixbuf.pixbuf);
2854 case GTK_IMAGE_STOCK:
2855 g_free (site->icon_data.stock.stock_id);
2857 case GTK_IMAGE_ICON_NAME:
2858 g_free (site->icon_data.name.icon_name);
2861 g_assert_not_reached();
2864 site->icon_type = GTK_IMAGE_EMPTY;
2867 g_object_unref (site->colormap);
2868 site->colormap = NULL;
2872 * gtk_drag_source_set_icon:
2873 * @widget: a #GtkWidget
2874 * @colormap: the colormap of the icon
2875 * @pixmap: the image data for the icon
2876 * @mask: (allow-none): the transparency mask for an image.
2878 * Sets the icon that will be used for drags from a particular widget
2879 * from a pixmap/mask. GTK+ retains references for the arguments, and
2880 * will release them when they are no longer needed.
2881 * Use gtk_drag_source_set_icon_pixbuf() instead.
2884 gtk_drag_source_set_icon (GtkWidget *widget,
2885 GdkColormap *colormap,
2889 GtkDragSourceSite *site;
2891 g_return_if_fail (GTK_IS_WIDGET (widget));
2892 g_return_if_fail (GDK_IS_COLORMAP (colormap));
2893 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
2894 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
2896 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2897 g_return_if_fail (site != NULL);
2899 g_object_ref (colormap);
2900 g_object_ref (pixmap);
2902 g_object_ref (mask);
2904 gtk_drag_source_unset_icon (site);
2906 site->icon_type = GTK_IMAGE_PIXMAP;
2908 site->icon_data.pixmap.pixmap = pixmap;
2909 site->icon_mask = mask;
2910 site->colormap = colormap;
2914 * gtk_drag_source_set_icon_pixbuf:
2915 * @widget: a #GtkWidget
2916 * @pixbuf: the #GdkPixbuf for the drag icon
2918 * Sets the icon that will be used for drags from a particular widget
2919 * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
2920 * release it when it is no longer needed.
2923 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
2926 GtkDragSourceSite *site;
2928 g_return_if_fail (GTK_IS_WIDGET (widget));
2929 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2931 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2932 g_return_if_fail (site != NULL);
2933 g_object_ref (pixbuf);
2935 gtk_drag_source_unset_icon (site);
2937 site->icon_type = GTK_IMAGE_PIXBUF;
2938 site->icon_data.pixbuf.pixbuf = pixbuf;
2942 * gtk_drag_source_set_icon_stock:
2943 * @widget: a #GtkWidget
2944 * @stock_id: the ID of the stock icon to use
2946 * Sets the icon that will be used for drags from a particular source
2950 gtk_drag_source_set_icon_stock (GtkWidget *widget,
2951 const gchar *stock_id)
2953 GtkDragSourceSite *site;
2955 g_return_if_fail (GTK_IS_WIDGET (widget));
2956 g_return_if_fail (stock_id != NULL);
2958 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2959 g_return_if_fail (site != NULL);
2961 gtk_drag_source_unset_icon (site);
2963 site->icon_type = GTK_IMAGE_STOCK;
2964 site->icon_data.stock.stock_id = g_strdup (stock_id);
2968 * gtk_drag_source_set_icon_name:
2969 * @widget: a #GtkWidget
2970 * @icon_name: name of icon to use
2972 * Sets the icon that will be used for drags from a particular source
2973 * to a themed icon. See the docs for #GtkIconTheme for more details.
2978 gtk_drag_source_set_icon_name (GtkWidget *widget,
2979 const gchar *icon_name)
2981 GtkDragSourceSite *site;
2983 g_return_if_fail (GTK_IS_WIDGET (widget));
2984 g_return_if_fail (icon_name != NULL);
2986 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2987 g_return_if_fail (site != NULL);
2989 gtk_drag_source_unset_icon (site);
2991 site->icon_type = GTK_IMAGE_ICON_NAME;
2992 site->icon_data.name.icon_name = g_strdup (icon_name);
2996 gtk_drag_get_icon (GtkDragSourceInfo *info,
2997 GtkWidget **icon_window,
3001 if (get_can_change_screen (info->icon_window))
3002 gtk_window_set_screen (GTK_WINDOW (info->icon_window),
3005 if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
3007 if (!info->fallback_icon)
3009 gint save_hot_x, save_hot_y;
3010 gboolean save_destroy_icon;
3011 GtkWidget *save_icon_window;
3013 /* HACK to get the appropriate icon
3015 save_icon_window = info->icon_window;
3016 save_hot_x = info->hot_x;
3017 save_hot_y = info->hot_x;
3018 save_destroy_icon = info->destroy_icon;
3020 info->icon_window = NULL;
3021 set_icon_stock_pixbuf (info->context,
3022 GTK_STOCK_DND, NULL, -2, -2, TRUE);
3023 info->fallback_icon = info->icon_window;
3025 info->icon_window = save_icon_window;
3026 info->hot_x = save_hot_x;
3027 info->hot_y = save_hot_y;
3028 info->destroy_icon = save_destroy_icon;
3031 gtk_widget_hide (info->icon_window);
3033 *icon_window = info->fallback_icon;
3034 gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
3041 if (info->fallback_icon)
3042 gtk_widget_hide (info->fallback_icon);
3044 *icon_window = info->icon_window;
3045 *hot_x = info->hot_x;
3046 *hot_y = info->hot_y;
3051 gtk_drag_update_icon (GtkDragSourceInfo *info)
3053 if (info->icon_window)
3055 GtkWidget *icon_window;
3058 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
3060 gtk_window_move (GTK_WINDOW (icon_window),
3061 info->cur_x - hot_x,
3062 info->cur_y - hot_y);
3064 if (gtk_widget_get_visible (icon_window))
3065 gdk_window_raise (gtk_widget_get_window (icon_window));
3067 gtk_widget_show (icon_window);
3072 gtk_drag_set_icon_window (GdkDragContext *context,
3076 gboolean destroy_on_release)
3078 GtkDragSourceInfo *info;
3080 info = gtk_drag_get_source_info (context, FALSE);
3083 if (destroy_on_release)
3084 gtk_widget_destroy (widget);
3088 gtk_drag_remove_icon (info);
3091 g_object_ref (widget);
3093 info->icon_window = widget;
3094 info->hot_x = hot_x;
3095 info->hot_y = hot_y;
3096 info->destroy_icon = destroy_on_release;
3098 if (widget && info->icon_pixbuf)
3100 g_object_unref (info->icon_pixbuf);
3101 info->icon_pixbuf = NULL;
3104 gtk_drag_update_cursor (info);
3105 gtk_drag_update_icon (info);
3109 * gtk_drag_set_icon_widget:
3110 * @context: the context for a drag. (This must be called
3111 with a context for the source side of a drag)
3112 * @widget: a toplevel window to use as an icon.
3113 * @hot_x: the X offset within @widget of the hotspot.
3114 * @hot_y: the Y offset within @widget of the hotspot.
3116 * Changes the icon for a widget to a given widget. GTK+
3117 * will not destroy the icon, so if you don't want
3118 * it to persist, you should connect to the "drag-end"
3119 * signal and destroy it yourself.
3122 gtk_drag_set_icon_widget (GdkDragContext *context,
3127 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3128 g_return_if_fail (context->is_source);
3129 g_return_if_fail (GTK_IS_WIDGET (widget));
3131 gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
3135 icon_window_realize (GtkWidget *window,
3141 gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
3142 gtk_widget_get_colormap (window),
3143 &pixmap, &mask, 128);
3145 gdk_window_set_back_pixmap (gtk_widget_get_window (window),
3147 g_object_unref (pixmap);
3151 gtk_widget_shape_combine_mask (window, mask, 0, 0);
3152 g_object_unref (mask);
3157 set_icon_stock_pixbuf (GdkDragContext *context,
3158 const gchar *stock_id,
3162 gboolean force_window)
3167 GdkDisplay *display;
3169 g_return_if_fail (context != NULL);
3170 g_return_if_fail (pixbuf != NULL || stock_id != NULL);
3171 g_return_if_fail (pixbuf == NULL || stock_id == NULL);
3173 screen = gdk_drawable_get_screen (context->source_window);
3175 /* Push a NULL colormap to guard against gtk_widget_push_colormap() */
3176 gtk_widget_push_colormap (NULL);
3177 window = gtk_window_new (GTK_WINDOW_POPUP);
3178 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3179 gtk_window_set_screen (GTK_WINDOW (window), screen);
3180 set_can_change_screen (window, TRUE);
3181 gtk_widget_pop_colormap ();
3183 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3184 gtk_widget_set_app_paintable (window, TRUE);
3188 pixbuf = gtk_widget_render_icon (window, stock_id,
3189 GTK_ICON_SIZE_DND, NULL);
3193 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
3194 gtk_widget_destroy (window);
3200 g_object_ref (pixbuf);
3202 display = gdk_drawable_get_display (context->source_window);
3203 width = gdk_pixbuf_get_width (pixbuf);
3204 height = gdk_pixbuf_get_height (pixbuf);
3206 if (!force_window &&
3207 gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
3209 GtkDragSourceInfo *info;
3211 gtk_widget_destroy (window);
3213 info = gtk_drag_get_source_info (context, FALSE);
3215 if (info->icon_pixbuf)
3216 g_object_unref (info->icon_pixbuf);
3217 info->icon_pixbuf = pixbuf;
3219 gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
3223 gtk_widget_set_size_request (window, width, height);
3225 g_signal_connect_closure (window, "realize",
3226 g_cclosure_new (G_CALLBACK (icon_window_realize),
3228 (GClosureNotify)g_object_unref),
3231 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3236 * gtk_drag_set_icon_pixbuf:
3237 * @context: the context for a drag. (This must be called
3238 * with a context for the source side of a drag)
3239 * @pixbuf: the #GdkPixbuf to use as the drag icon.
3240 * @hot_x: the X offset within @widget of the hotspot.
3241 * @hot_y: the Y offset within @widget of the hotspot.
3243 * Sets @pixbuf as the icon for a given drag.
3246 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
3251 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3252 g_return_if_fail (context->is_source);
3253 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3255 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3259 * gtk_drag_set_icon_stock:
3260 * @context: the context for a drag. (This must be called
3261 * with a context for the source side of a drag)
3262 * @stock_id: the ID of the stock icon to use for the drag.
3263 * @hot_x: the X offset within the icon of the hotspot.
3264 * @hot_y: the Y offset within the icon of the hotspot.
3266 * Sets the icon for a given drag from a stock ID.
3269 gtk_drag_set_icon_stock (GdkDragContext *context,
3270 const gchar *stock_id,
3274 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3275 g_return_if_fail (context->is_source);
3276 g_return_if_fail (stock_id != NULL);
3278 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
3282 * gtk_drag_set_icon_pixmap:
3283 * @context: the context for a drag. (This must be called
3284 * with a context for the source side of a drag)
3285 * @colormap: the colormap of the icon
3286 * @pixmap: the image data for the icon
3287 * @mask: (allow-none): the transparency mask for the icon or %NULL for none.
3288 * @hot_x: the X offset within @pixmap of the hotspot.
3289 * @hot_y: the Y offset within @pixmap of the hotspot.
3291 * Sets @pixmap as the icon for a given drag. GTK+ retains
3292 * references for the arguments, and will release them when
3293 * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
3294 * will be more convenient to use.
3297 gtk_drag_set_icon_pixmap (GdkDragContext *context,
3298 GdkColormap *colormap,
3308 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3309 g_return_if_fail (context->is_source);
3310 g_return_if_fail (GDK_IS_COLORMAP (colormap));
3311 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
3312 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
3314 screen = gdk_colormap_get_screen (colormap);
3316 g_return_if_fail (gdk_drawable_get_screen (pixmap) == screen);
3317 g_return_if_fail (!mask || gdk_drawable_get_screen (mask) == screen);
3319 gdk_drawable_get_size (pixmap, &width, &height);
3321 gtk_widget_push_colormap (colormap);
3323 window = gtk_window_new (GTK_WINDOW_POPUP);
3324 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3325 gtk_window_set_screen (GTK_WINDOW (window), screen);
3326 set_can_change_screen (window, FALSE);
3327 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3328 gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
3330 gtk_widget_pop_colormap ();
3332 gtk_widget_set_size_request (window, width, height);
3333 gtk_widget_realize (window);
3335 gdk_window_set_back_pixmap (gtk_widget_get_window (window),
3339 gtk_widget_shape_combine_mask (window, mask, 0, 0);
3341 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3345 * gtk_drag_set_icon_name:
3346 * @context: the context for a drag. (This must be called
3347 * with a context for the source side of a drag)
3348 * @icon_name: name of icon to use
3349 * @hot_x: the X offset of the hotspot within the icon
3350 * @hot_y: the Y offset of the hotspot within the icon
3352 * Sets the icon for a given drag from a named themed icon. See
3353 * the docs for #GtkIconTheme for more details. Note that the
3354 * size of the icon depends on the icon theme (the icon is
3355 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
3356 * @hot_x and @hot_y have to be used with care.
3361 gtk_drag_set_icon_name (GdkDragContext *context,
3362 const gchar *icon_name,
3367 GtkSettings *settings;
3368 GtkIconTheme *icon_theme;
3370 gint width, height, icon_size;
3372 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3373 g_return_if_fail (context->is_source);
3374 g_return_if_fail (icon_name != NULL);
3376 screen = gdk_drawable_get_screen (context->source_window);
3377 g_return_if_fail (screen != NULL);
3379 settings = gtk_settings_get_for_screen (screen);
3380 if (gtk_icon_size_lookup_for_settings (settings,
3383 icon_size = MAX (width, height);
3385 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
3387 icon_theme = gtk_icon_theme_get_for_screen (screen);
3389 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
3390 icon_size, 0, NULL);
3392 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3394 g_warning ("Cannot load drag icon from icon name %s", icon_name);
3398 * gtk_drag_set_icon_default:
3399 * @context: the context for a drag. (This must be called
3400 with a context for the source side of a drag)
3402 * Sets the icon for a particular drag to the default
3406 gtk_drag_set_icon_default (GdkDragContext *context)
3408 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3409 g_return_if_fail (context->is_source);
3411 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
3414 /*************************************************************
3415 * _gtk_drag_source_handle_event:
3416 * Called from widget event handling code on Drag events
3420 * toplevel: Toplevel widget that received the event
3423 *************************************************************/
3426 _gtk_drag_source_handle_event (GtkWidget *widget,
3429 GtkDragSourceInfo *info;
3430 GdkDragContext *context;
3432 g_return_if_fail (widget != NULL);
3433 g_return_if_fail (event != NULL);
3435 context = event->dnd.context;
3436 info = gtk_drag_get_source_info (context, FALSE);
3440 switch (event->type)
3442 case GDK_DRAG_STATUS:
3446 if (info->proxy_dest)
3448 if (!event->dnd.send_event)
3450 if (info->proxy_dest->proxy_drop_wait)
3452 gboolean result = context->action != 0;
3454 /* Aha - we can finally pass the MOTIF DROP on... */
3455 gdk_drop_reply (info->proxy_dest->context, result, info->proxy_dest->proxy_drop_time);
3457 gdk_drag_drop (info->context, info->proxy_dest->proxy_drop_time);
3459 gtk_drag_finish (info->proxy_dest->context, FALSE, FALSE, info->proxy_dest->proxy_drop_time);
3463 gdk_drag_status (info->proxy_dest->context,
3464 event->dnd.context->action,
3469 else if (info->have_grab)
3471 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
3472 event->dnd.context->action,
3474 if (info->cursor != cursor)
3478 pointer = gdk_drag_context_get_device (context);
3479 gdk_device_grab (pointer, gtk_widget_get_window (widget),
3480 GDK_OWNERSHIP_APPLICATION, FALSE,
3481 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3482 cursor, info->grab_time);
3483 info->cursor = cursor;
3486 gtk_drag_add_update_idle (info);
3491 case GDK_DROP_FINISHED:
3492 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, event->dnd.time);
3495 g_assert_not_reached ();
3499 /*************************************************************
3500 * gtk_drag_source_check_selection:
3501 * Check if we've set up handlers/claimed the selection
3502 * for a given drag. If not, add them.
3506 *************************************************************/
3509 gtk_drag_source_check_selection (GtkDragSourceInfo *info,
3515 tmp_list = info->selections;
3518 if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3520 tmp_list = tmp_list->next;
3523 gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3527 info->selections = g_list_prepend (info->selections,
3528 GUINT_TO_POINTER (selection));
3530 tmp_list = info->target_list->list;
3533 GtkTargetPair *pair = tmp_list->data;
3535 gtk_selection_add_target (info->ipc_widget,
3539 tmp_list = tmp_list->next;
3542 if (info->context->protocol == GDK_DRAG_PROTO_MOTIF)
3544 gtk_selection_add_target (info->ipc_widget,
3546 gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS"),
3547 TARGET_MOTIF_SUCCESS);
3548 gtk_selection_add_target (info->ipc_widget,
3550 gdk_atom_intern_static_string ("XmTRANSFER_FAILURE"),
3551 TARGET_MOTIF_FAILURE);
3554 gtk_selection_add_target (info->ipc_widget,
3556 gdk_atom_intern_static_string ("DELETE"),
3560 /*************************************************************
3561 * gtk_drag_drop_finished:
3562 * Clean up from the drag, and display snapback, if necessary.
3568 *************************************************************/
3571 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3572 GtkDragResult result,
3577 success = (result == GTK_DRAG_RESULT_SUCCESS);
3578 gtk_drag_source_release_selections (info, time);
3580 if (info->proxy_dest)
3582 /* The time from the event isn't reliable for Xdnd drags */
3583 gtk_drag_finish (info->proxy_dest->context, success, FALSE,
3584 info->proxy_dest->proxy_drop_time);
3585 gtk_drag_source_info_destroy (info);
3590 g_signal_emit_by_name (info->widget, "drag-failed",
3591 info->context, result, &success);
3595 gtk_drag_source_info_destroy (info);
3599 GtkDragAnim *anim = g_new (GtkDragAnim, 1);
3603 anim->n_steps = MAX (info->cur_x - info->start_x,
3604 info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3605 anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3607 info->cur_screen = gtk_widget_get_screen (info->widget);
3609 if (!info->icon_window)
3610 set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf,
3613 gtk_drag_update_icon (info);
3615 /* Mark the context as dead, so if the destination decides
3616 * to respond really late, we still are OK.
3618 gtk_drag_clear_source_info (info->context);
3619 gdk_threads_add_timeout (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3625 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3628 GdkDisplay *display = gtk_widget_get_display (info->widget);
3629 GList *tmp_list = info->selections;
3633 GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3634 if (gdk_selection_owner_get_for_display (display, selection) == gtk_widget_get_window (info->ipc_widget))
3635 gtk_selection_owner_set_for_display (display, NULL, selection, time);
3637 tmp_list = tmp_list->next;
3640 g_list_free (info->selections);
3641 info->selections = NULL;
3644 /*************************************************************
3646 * Send a drop event.
3650 *************************************************************/
3653 gtk_drag_drop (GtkDragSourceInfo *info,
3656 if (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN)
3658 GtkSelectionData selection_data;
3660 /* GTK+ traditionally has used application/x-rootwin-drop, but the
3661 * XDND spec specifies x-rootwindow-drop.
3663 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3664 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3666 tmp_list = info->target_list->list;
3669 GtkTargetPair *pair = tmp_list->data;
3671 if (pair->target == target1 || pair->target == target2)
3673 selection_data.selection = GDK_NONE;
3674 selection_data.target = pair->target;
3675 selection_data.data = NULL;
3676 selection_data.length = -1;
3678 g_signal_emit_by_name (info->widget, "drag-data-get",
3679 info->context, &selection_data,
3683 /* FIXME: Should we check for length >= 0 here? */
3684 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3687 tmp_list = tmp_list->next;
3689 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3693 if (info->icon_window)
3694 gtk_widget_hide (info->icon_window);
3696 gdk_drag_drop (info->context, time);
3697 info->drop_timeout = gdk_threads_add_timeout (DROP_ABORT_TIME,
3698 gtk_drag_abort_timeout,
3704 * Source side callbacks.
3708 gtk_drag_source_event_cb (GtkWidget *widget,
3712 GtkDragSourceSite *site;
3713 gboolean retval = FALSE;
3714 site = (GtkDragSourceSite *)data;
3716 switch (event->type)
3718 case GDK_BUTTON_PRESS:
3719 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3721 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3722 site->x = event->button.x;
3723 site->y = event->button.y;
3727 case GDK_BUTTON_RELEASE:
3728 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3729 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3732 case GDK_MOTION_NOTIFY:
3733 if (site->state & event->motion.state & site->start_button_mask)
3735 /* FIXME: This is really broken and can leave us
3741 if (site->state & event->motion.state &
3742 GDK_BUTTON1_MASK << (i - 1))
3746 if (gtk_drag_check_threshold (widget, site->x, site->y,
3747 event->motion.x, event->motion.y))
3750 gtk_drag_begin_internal (widget, site, site->target_list,
3759 default: /* hit for 2/3BUTTON_PRESS */
3767 gtk_drag_source_site_destroy (gpointer data)
3769 GtkDragSourceSite *site = data;
3771 if (site->target_list)
3772 gtk_target_list_unref (site->target_list);
3774 gtk_drag_source_unset_icon (site);
3779 gtk_drag_selection_get (GtkWidget *widget,
3780 GtkSelectionData *selection_data,
3785 GtkDragSourceInfo *info = data;
3786 static GdkAtom null_atom = GDK_NONE;
3790 null_atom = gdk_atom_intern_static_string ("NULL");
3795 g_signal_emit_by_name (info->widget,
3798 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3800 case TARGET_MOTIF_SUCCESS:
3801 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3802 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3804 case TARGET_MOTIF_FAILURE:
3805 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3806 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3809 if (info->proxy_dest)
3811 /* This is sort of dangerous and needs to be thought
3814 info->proxy_dest->proxy_data = selection_data;
3815 gtk_drag_get_data (info->widget,
3816 info->proxy_dest->context,
3817 selection_data->target,
3820 info->proxy_dest->proxy_data = NULL;
3824 if (gtk_target_list_find (info->target_list,
3825 selection_data->target,
3828 g_signal_emit_by_name (info->widget, "drag-data-get",
3840 gtk_drag_anim_timeout (gpointer data)
3842 GtkDragAnim *anim = data;
3846 if (anim->step == anim->n_steps)
3848 gtk_drag_source_info_destroy (anim->info);
3855 x = (anim->info->start_x * (anim->step + 1) +
3856 anim->info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3857 y = (anim->info->start_y * (anim->step + 1) +
3858 anim->info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3859 if (anim->info->icon_window)
3861 GtkWidget *icon_window;
3864 gtk_drag_get_icon (anim->info, &icon_window, &hot_x, &hot_y);
3865 gtk_window_move (GTK_WINDOW (icon_window),
3879 gtk_drag_remove_icon (GtkDragSourceInfo *info)
3881 if (info->icon_window)
3883 gtk_widget_hide (info->icon_window);
3884 if (info->destroy_icon)
3885 gtk_widget_destroy (info->icon_window);
3887 if (info->fallback_icon)
3889 gtk_widget_destroy (info->fallback_icon);
3890 info->fallback_icon = NULL;
3893 g_object_unref (info->icon_window);
3894 info->icon_window = NULL;
3899 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
3903 for (i = 0; i < n_drag_cursors; i++)
3905 if (info->drag_cursors[i] != NULL)
3907 gdk_cursor_unref (info->drag_cursors[i]);
3908 info->drag_cursors[i] = NULL;
3912 gtk_drag_remove_icon (info);
3914 if (info->icon_pixbuf)
3916 g_object_unref (info->icon_pixbuf);
3917 info->icon_pixbuf = NULL;
3920 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3921 gtk_drag_grab_broken_event_cb,
3923 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3924 gtk_drag_grab_notify_cb,
3926 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3927 gtk_drag_button_release_cb,
3929 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3932 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3935 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3936 gtk_drag_selection_get,
3939 if (!info->proxy_dest)
3940 g_signal_emit_by_name (info->widget, "drag-end", info->context);
3943 g_object_unref (info->widget);
3945 gtk_selection_remove_all (info->ipc_widget);
3946 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), NULL);
3947 source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
3948 gtk_drag_release_ipc_widget (info->ipc_widget);
3950 gtk_target_list_unref (info->target_list);
3952 gtk_drag_clear_source_info (info->context);
3953 g_object_unref (info->context);
3955 if (info->drop_timeout)
3956 g_source_remove (info->drop_timeout);
3958 if (info->update_idle)
3959 g_source_remove (info->update_idle);
3965 gtk_drag_update_idle (gpointer data)
3967 GtkDragSourceInfo *info = data;
3968 GdkWindow *dest_window;
3969 GdkDragProtocol protocol;
3972 GdkDragAction action;
3973 GdkDragAction possible_actions;
3976 info->update_idle = 0;
3978 if (info->last_event)
3980 time = gtk_drag_get_event_time (info->last_event);
3981 gtk_drag_get_event_actions (info->last_event,
3983 info->possible_actions,
3984 &action, &possible_actions);
3985 gtk_drag_update_icon (info);
3986 gdk_drag_find_window_for_screen (info->context,
3987 info->icon_window ? gtk_widget_get_window (info->icon_window) : NULL,
3988 info->cur_screen, info->cur_x, info->cur_y,
3989 &dest_window, &protocol);
3991 if (!gdk_drag_motion (info->context, dest_window, protocol,
3992 info->cur_x, info->cur_y, action,
3996 gdk_event_free ((GdkEvent *)info->last_event);
3997 info->last_event = NULL;
4001 g_object_unref (dest_window);
4003 selection = gdk_drag_get_selection (info->context);
4005 gtk_drag_source_check_selection (info, selection, time);
4013 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
4015 /* We use an idle lower than GDK_PRIORITY_REDRAW so that exposes
4016 * from the last move can catch up before we move again.
4018 if (!info->update_idle)
4019 info->update_idle = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW + 5,
4020 gtk_drag_update_idle,
4027 * @info: DragSourceInfo for the drag
4028 * @screen: new screen
4029 * @x_root: new X position
4030 * @y_root: new y position
4031 * @event: event received requiring update
4033 * Updates the status of the drag; called when the
4034 * cursor moves or the modifier changes
4037 gtk_drag_update (GtkDragSourceInfo *info,
4043 info->cur_screen = screen;
4044 info->cur_x = x_root;
4045 info->cur_y = y_root;
4046 if (info->last_event)
4048 gdk_event_free ((GdkEvent *)info->last_event);
4049 info->last_event = NULL;
4052 info->last_event = gdk_event_copy ((GdkEvent *)event);
4054 gtk_drag_add_update_idle (info);
4057 /*************************************************************
4059 * Called when the user finishes to drag, either by
4060 * releasing the mouse, or by pressing Esc.
4062 * info: Source info for the drag
4063 * time: Timestamp for ending the drag
4065 *************************************************************/
4068 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
4070 GdkEvent *send_event;
4071 GtkWidget *source_widget = info->widget;
4072 GdkDevice *pointer, *keyboard;
4074 pointer = gdk_drag_context_get_device (info->context);
4075 keyboard = gdk_device_get_associated_device (pointer);
4077 /* Prevent ungrab before grab (see bug 623865) */
4078 if (info->grab_time == GDK_CURRENT_TIME)
4079 time = GDK_CURRENT_TIME;
4081 if (info->update_idle)
4083 g_source_remove (info->update_idle);
4084 info->update_idle = 0;
4087 if (info->last_event)
4089 gdk_event_free (info->last_event);
4090 info->last_event = NULL;
4093 info->have_grab = FALSE;
4095 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4096 gtk_drag_grab_broken_event_cb,
4098 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4099 gtk_drag_grab_notify_cb,
4101 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4102 gtk_drag_button_release_cb,
4104 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4107 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4111 gdk_device_ungrab (pointer, time);
4112 ungrab_dnd_keys (info->ipc_widget, keyboard, time);
4113 gtk_device_grab_remove (info->ipc_widget, pointer);
4115 /* Send on a release pair to the original
4116 * widget to convince it to release its grab. We need to
4117 * call gtk_propagate_event() here, instead of
4118 * gtk_widget_event() because widget like GtkList may
4119 * expect propagation.
4122 send_event = gdk_event_new (GDK_BUTTON_RELEASE);
4123 send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
4124 send_event->button.send_event = TRUE;
4125 send_event->button.time = time;
4126 send_event->button.x = 0;
4127 send_event->button.y = 0;
4128 send_event->button.axes = NULL;
4129 send_event->button.state = 0;
4130 send_event->button.button = info->button;
4131 send_event->button.device = pointer;
4132 send_event->button.x_root = 0;
4133 send_event->button.y_root = 0;
4135 gtk_propagate_event (source_widget, send_event);
4136 gdk_event_free (send_event);
4139 /*************************************************************
4141 * Called on cancellation of a drag, either by the user
4142 * or programmatically.
4144 * info: Source info for the drag
4145 * time: Timestamp for ending the drag
4147 *************************************************************/
4150 gtk_drag_cancel (GtkDragSourceInfo *info, GtkDragResult result, guint32 time)
4152 gtk_drag_end (info, time);
4153 gdk_drag_abort (info->context, time);
4154 gtk_drag_drop_finished (info, result, time);
4157 /*************************************************************
4158 * gtk_drag_motion_cb:
4159 * "motion-notify-event" callback during drag.
4163 *************************************************************/
4166 gtk_drag_motion_cb (GtkWidget *widget,
4167 GdkEventMotion *event,
4170 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4172 gint x_root, y_root;
4176 GdkDisplay *display = gtk_widget_get_display (widget);
4178 gdk_display_get_device_state (display, event->device,
4179 &screen, &x_root, &y_root, NULL);
4180 event->x_root = x_root;
4181 event->y_root = y_root;
4184 screen = gdk_event_get_screen ((GdkEvent *)event);
4186 gtk_drag_update (info, screen, event->x_root, event->y_root, (GdkEvent *) event);
4191 /*************************************************************
4193 * "key-press/release-event" callback during drag.
4197 *************************************************************/
4200 #define SMALL_STEP 1
4203 gtk_drag_key_cb (GtkWidget *widget,
4207 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4208 GdkModifierType state;
4209 GdkWindow *root_window;
4214 state = event->state & gtk_accelerator_get_default_mod_mask ();
4215 pointer = gdk_device_get_associated_device (gdk_event_get_device ((GdkEvent *) event));
4217 if (event->type == GDK_KEY_PRESS)
4219 switch (event->keyval)
4221 case GDK_KEY_Escape:
4222 gtk_drag_cancel (info, GTK_DRAG_RESULT_USER_CANCELLED, event->time);
4226 case GDK_KEY_Return:
4227 case GDK_KEY_ISO_Enter:
4228 case GDK_KEY_KP_Enter:
4229 case GDK_KEY_KP_Space:
4230 gtk_drag_end (info, event->time);
4231 gtk_drag_drop (info, event->time);
4236 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4240 case GDK_KEY_KP_Down:
4241 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4245 case GDK_KEY_KP_Left:
4246 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4250 case GDK_KEY_KP_Right:
4251 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4257 /* Now send a "motion" so that the modifier state is updated */
4259 /* The state is not yet updated in the event, so we need
4260 * to query it here. We could use XGetModifierMapping, but
4261 * that would be overkill.
4263 root_window = gtk_widget_get_root_window (widget);
4264 gdk_window_get_device_position (root_window, pointer, NULL, NULL, &state);
4265 event->state = state;
4267 if (dx != 0 || dy != 0)
4271 gdk_display_warp_device (gtk_widget_get_display (widget), pointer,
4272 gtk_widget_get_screen (widget),
4273 info->cur_x, info->cur_y);
4276 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
4282 gtk_drag_grab_broken_event_cb (GtkWidget *widget,
4283 GdkEventGrabBroken *event,
4286 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4288 /* Don't cancel if we break the implicit grab from the initial button_press.
4289 * Also, don't cancel if we re-grab on the widget or on our IPC window, for
4290 * example, when changing the drag cursor.
4293 || event->grab_window == gtk_widget_get_window (info->widget)
4294 || event->grab_window == gtk_widget_get_window (info->ipc_widget))
4297 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4302 gtk_drag_grab_notify_cb (GtkWidget *widget,
4303 gboolean was_grabbed,
4306 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4309 pointer = gdk_drag_context_get_device (info->context);
4311 if (gtk_widget_device_is_shadowed (widget, pointer))
4313 /* We have to block callbacks to avoid recursion here, because
4314 gtk_drag_cancel calls gtk_grab_remove (via gtk_drag_end) */
4315 g_signal_handlers_block_by_func (widget, gtk_drag_grab_notify_cb, data);
4316 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4317 g_signal_handlers_unblock_by_func (widget, gtk_drag_grab_notify_cb, data);
4322 /*************************************************************
4323 * gtk_drag_button_release_cb:
4324 * "button-release-event" callback during drag.
4328 *************************************************************/
4331 gtk_drag_button_release_cb (GtkWidget *widget,
4332 GdkEventButton *event,
4335 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4337 if (event->button != info->button)
4340 if ((info->context->action != 0) && (info->context->dest_window != NULL))
4342 gtk_drag_end (info, event->time);
4343 gtk_drag_drop (info, event->time);
4347 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4354 gtk_drag_abort_timeout (gpointer data)
4356 GtkDragSourceInfo *info = data;
4357 guint32 time = GDK_CURRENT_TIME;
4359 if (info->proxy_dest)
4360 time = info->proxy_dest->proxy_drop_time;
4362 info->drop_timeout = 0;
4363 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_TIMEOUT_EXPIRED, time);
4369 * gtk_drag_check_threshold:
4370 * @widget: a #GtkWidget
4371 * @start_x: X coordinate of start of drag
4372 * @start_y: Y coordinate of start of drag
4373 * @current_x: current X coordinate
4374 * @current_y: current Y coordinate
4376 * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
4377 * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
4378 * should trigger the beginning of a drag-and-drop operation.
4380 * Return Value: %TRUE if the drag threshold has been passed.
4383 gtk_drag_check_threshold (GtkWidget *widget,
4389 gint drag_threshold;
4391 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4393 g_object_get (gtk_widget_get_settings (widget),
4394 "gtk-dnd-drag-threshold", &drag_threshold,
4397 return (ABS (current_x - start_x) > drag_threshold ||
4398 ABS (current_y - start_y) > drag_threshold);