1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
33 #include "gdkconfig.h"
35 #include "gdk/gdkkeysyms.h"
37 #ifdef GDK_WINDOWING_X11
39 #include <X11/keysym.h>
40 #include "gdk/x11/gdkx.h"
44 #include "gtkiconfactory.h"
45 #include "gtkicontheme.h"
47 #include "gtkinvisible.h"
51 #include "gtktooltip.h"
52 #include "gtkwindow.h"
54 #include "gtkdndcursors.h"
55 #include "gtkselectionprivate.h"
57 static GSList *source_widgets = NULL;
59 typedef struct _GtkDragSourceSite GtkDragSourceSite;
60 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
61 typedef struct _GtkDragDestSite GtkDragDestSite;
62 typedef struct _GtkDragDestInfo GtkDragDestInfo;
63 typedef struct _GtkDragAnim GtkDragAnim;
73 struct _GtkDragSourceSite
75 GdkModifierType start_button_mask;
76 GtkTargetList *target_list; /* Targets for drag data */
77 GdkDragAction actions; /* Possible actions */
80 GtkImageType icon_type;
83 GtkImagePixbufData pixbuf;
84 GtkImageStockData stock;
85 GtkImageIconNameData name;
88 /* Stored button press information to detect drag beginning */
93 struct _GtkDragSourceInfo
96 GtkTargetList *target_list; /* Targets for drag data */
97 GdkDragAction possible_actions; /* Actions allowed by source */
98 GdkDragContext *context; /* drag context */
99 GtkWidget *icon_window; /* Window for drag */
100 GtkWidget *fallback_icon; /* Window for drag used on other screens */
101 GtkWidget *ipc_widget; /* GtkInvisible for grab, message passing */
102 GdkCursor *cursor; /* Cursor for drag */
103 gint hot_x, hot_y; /* Hot spot for drag */
104 gint button; /* mouse button starting drag */
106 GtkDragStatus status; /* drag status */
107 GdkEvent *last_event; /* pending event */
109 gint start_x, start_y; /* Initial position */
110 gint cur_x, cur_y; /* Current Position */
111 GdkScreen *cur_screen; /* Current screen for pointer */
113 guint32 grab_time; /* timestamp for initial grab */
114 GList *selections; /* selections we've claimed */
116 GtkDragDestInfo *proxy_dest; /* Set if this is a proxy drag */
118 guint update_idle; /* Idle function to update the drag */
119 guint drop_timeout; /* Timeout for aborting drop */
120 guint destroy_icon : 1; /* If true, destroy icon_window */
121 guint have_grab : 1; /* Do we still have the pointer grab */
122 GdkPixbuf *icon_pixbuf;
123 GdkCursor *drag_cursors[6];
126 struct _GtkDragDestSite
128 GtkDestDefaults flags;
129 GtkTargetList *target_list;
130 GdkDragAction actions;
131 GdkWindow *proxy_window;
132 GdkDragProtocol proxy_protocol;
134 guint proxy_coords : 1;
136 guint track_motion : 1;
139 struct _GtkDragDestInfo
141 GtkWidget *widget; /* Widget in which drag is in */
142 GdkDragContext *context; /* Drag context */
143 GtkDragSourceInfo *proxy_source; /* Set if this is a proxy drag */
144 GtkSelectionData *proxy_data; /* Set while retrieving proxied data */
145 guint32 proxy_drop_time; /* Timestamp for proxied drop */
146 guint proxy_drop_wait : 1; /* Set if we are waiting for a
147 * status reply before sending
150 guint dropped : 1; /* Set after we receive a drop */
151 gint drop_x, drop_y; /* Position of drop */
154 #define DROP_ABORT_TIME 300000
156 #define ANIM_STEP_TIME 50
157 #define ANIM_STEP_LENGTH 50
158 #define ANIM_MIN_STEPS 5
159 #define ANIM_MAX_STEPS 10
163 GtkDragSourceInfo *info;
168 typedef gboolean (* GtkDragDestCallback) (GtkWidget *widget,
169 GdkDragContext *context,
174 /* Enumeration for some targets we handle internally */
177 TARGET_MOTIF_SUCCESS = 0x40000000,
178 TARGET_MOTIF_FAILURE,
182 /* Forward declarations */
183 static void gtk_drag_get_event_actions (GdkEvent *event,
185 GdkDragAction actions,
186 GdkDragAction *suggested_action,
187 GdkDragAction *possible_actions);
188 static GdkCursor * gtk_drag_get_cursor (GdkDisplay *display,
189 GdkDragAction action,
190 GtkDragSourceInfo *info);
191 static void gtk_drag_update_cursor (GtkDragSourceInfo *info);
192 static GtkWidget *gtk_drag_get_ipc_widget (GtkWidget *widget);
193 static GtkWidget *gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen);
194 static void gtk_drag_release_ipc_widget (GtkWidget *widget);
196 static void gtk_drag_selection_received (GtkWidget *widget,
197 GtkSelectionData *selection_data,
200 static gboolean gtk_drag_find_widget (GtkWidget *widget,
201 GdkDragContext *context,
202 GtkDragDestInfo *info,
206 GtkDragDestCallback callback);
207 static void gtk_drag_proxy_begin (GtkWidget *widget,
208 GtkDragDestInfo *dest_info,
210 static void gtk_drag_dest_realized (GtkWidget *widget);
211 static void gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
212 GtkWidget *previous_toplevel);
213 static void gtk_drag_dest_site_destroy (gpointer data);
214 static void gtk_drag_dest_leave (GtkWidget *widget,
215 GdkDragContext *context,
217 static gboolean gtk_drag_dest_motion (GtkWidget *widget,
218 GdkDragContext *context,
222 static gboolean gtk_drag_dest_drop (GtkWidget *widget,
223 GdkDragContext *context,
228 static GtkDragDestInfo * gtk_drag_get_dest_info (GdkDragContext *context,
230 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
232 static void gtk_drag_clear_source_info (GdkDragContext *context);
234 static void gtk_drag_source_check_selection (GtkDragSourceInfo *info,
237 static void gtk_drag_source_release_selections (GtkDragSourceInfo *info,
239 static void gtk_drag_drop (GtkDragSourceInfo *info,
241 static void gtk_drag_drop_finished (GtkDragSourceInfo *info,
242 GtkDragResult result,
244 static void gtk_drag_cancel (GtkDragSourceInfo *info,
245 GtkDragResult result,
248 static gboolean gtk_drag_source_event_cb (GtkWidget *widget,
251 static void gtk_drag_source_site_destroy (gpointer data);
252 static void gtk_drag_selection_get (GtkWidget *widget,
253 GtkSelectionData *selection_data,
257 static gboolean gtk_drag_anim_timeout (gpointer data);
258 static void gtk_drag_remove_icon (GtkDragSourceInfo *info);
259 static void gtk_drag_source_info_destroy (GtkDragSourceInfo *info);
260 static void gtk_drag_add_update_idle (GtkDragSourceInfo *info);
262 static void gtk_drag_update (GtkDragSourceInfo *info,
267 static gboolean gtk_drag_motion_cb (GtkWidget *widget,
268 GdkEventMotion *event,
270 static gboolean gtk_drag_key_cb (GtkWidget *widget,
273 static gboolean gtk_drag_grab_broken_event_cb (GtkWidget *widget,
274 GdkEventGrabBroken *event,
276 static void gtk_drag_grab_notify_cb (GtkWidget *widget,
277 gboolean was_grabbed,
279 static gboolean gtk_drag_button_release_cb (GtkWidget *widget,
280 GdkEventButton *event,
282 static gboolean gtk_drag_abort_timeout (gpointer data);
284 static void set_icon_stock_pixbuf (GdkDragContext *context,
285 const gchar *stock_id,
289 gboolean force_window);
291 /************************
292 * Cursor and Icon data *
293 ************************/
296 GdkDragAction action;
302 { GDK_ACTION_DEFAULT, NULL },
303 { GDK_ACTION_ASK, "dnd-ask", dnd_cursor_ask, NULL, NULL },
304 { GDK_ACTION_COPY, "dnd-copy", dnd_cursor_copy, NULL, NULL },
305 { GDK_ACTION_MOVE, "dnd-move", dnd_cursor_move, NULL, NULL },
306 { GDK_ACTION_LINK, "dnd-link", dnd_cursor_link, NULL, NULL },
307 { 0 , "dnd-none", dnd_cursor_none, NULL, NULL },
310 static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
312 /*********************
313 * Utility functions *
314 *********************/
317 set_can_change_screen (GtkWidget *widget,
318 gboolean can_change_screen)
320 can_change_screen = can_change_screen != FALSE;
322 g_object_set_data (G_OBJECT (widget), I_("gtk-dnd-can-change-screen"),
323 GUINT_TO_POINTER (can_change_screen));
327 get_can_change_screen (GtkWidget *widget)
329 return g_object_get_data (G_OBJECT (widget), "gtk-dnd-can-change-screen") != NULL;
334 gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen)
337 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
338 "gtk-dnd-ipc-widgets");
342 GSList *tmp = drag_widgets;
343 result = drag_widgets->data;
344 drag_widgets = drag_widgets->next;
345 g_object_set_data (G_OBJECT (screen),
346 I_("gtk-dnd-ipc-widgets"),
348 g_slist_free_1 (tmp);
352 result = gtk_window_new (GTK_WINDOW_POPUP);
353 gtk_window_set_screen (GTK_WINDOW (result), screen);
354 gtk_window_resize (GTK_WINDOW (result), 1, 1);
355 gtk_window_move (GTK_WINDOW (result), -100, -100);
356 gtk_widget_show (result);
363 gtk_drag_get_ipc_widget (GtkWidget *widget)
368 result = gtk_drag_get_ipc_widget_for_screen (gtk_widget_get_screen (widget));
370 toplevel = gtk_widget_get_toplevel (widget);
372 if (GTK_IS_WINDOW (toplevel))
374 if (gtk_window_has_group (GTK_WINDOW (toplevel)))
375 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
376 GTK_WINDOW (result));
382 /* FIXME: modifying the XEvent window as in root_key_filter() isn't
383 * going to work with XGE/XI2, since the actual event to handle would
384 * be allocated/freed before GDK gets to translate the event.
385 * Active grabs on the keyboard are used instead at the moment...
387 #if defined (GDK_WINDOWING_X11) && !defined (XINPUT_2)
390 * We want to handle a handful of keys during DND, e.g. Escape to abort.
391 * Grabbing the keyboard has the unfortunate side-effect of preventing
392 * useful things such as using Alt-Tab to cycle between windows or
393 * switching workspaces. Therefore, we just grab the few keys we are
394 * interested in. Note that we need to put the grabs on the root window
395 * in order for them to still work when the focus is moved to another
398 * GDK needs a little help to successfully deliver root key events...
401 static GdkFilterReturn
402 root_key_filter (GdkXEvent *xevent,
406 XEvent *ev = (XEvent *) xevent;
408 if ((ev->type == KeyPress || ev->type == KeyRelease) &&
409 ev->xkey.root == ev->xkey.window)
410 ev->xkey.window = (Window)data;
412 return GDK_FILTER_CONTINUE;
420 static GrabKey grab_keys[] = {
429 { XK_Down, Mod1Mask },
431 { XK_Left, Mod1Mask },
433 { XK_Right, Mod1Mask },
435 { XK_KP_Up, Mod1Mask },
437 { XK_KP_Down, Mod1Mask },
439 { XK_KP_Left, Mod1Mask },
441 { XK_KP_Right, Mod1Mask }
445 grab_dnd_keys (GtkWidget *widget,
450 GdkWindow *window, *root;
453 window = gtk_widget_get_window (widget);
454 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
456 gdk_error_trap_push ();
458 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
460 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
461 XGrabKey (GDK_WINDOW_XDISPLAY (window),
462 keycode, grab_keys[i].modifiers,
463 GDK_WINDOW_XID (root),
470 gdk_error_trap_pop ();
472 gdk_window_add_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
476 ungrab_dnd_keys (GtkWidget *widget,
481 GdkWindow *window, *root;
484 window = gtk_widget_get_window (widget);
485 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
487 gdk_window_remove_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
489 gdk_error_trap_push ();
491 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
493 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
494 XUngrabKey (GDK_WINDOW_XDISPLAY (window),
495 keycode, grab_keys[i].modifiers,
496 GDK_WINDOW_XID (root));
500 gdk_error_trap_pop ();
503 #else /* GDK_WINDOWING_X11 && !XINPUT_2 */
506 grab_dnd_keys (GtkWidget *widget,
510 gdk_device_grab (device,
511 gtk_widget_get_window (widget),
512 GDK_OWNERSHIP_APPLICATION, FALSE,
513 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
518 ungrab_dnd_keys (GtkWidget *widget,
522 gdk_device_ungrab (device, time);
525 #endif /* GDK_WINDOWING_X11 */
528 /***************************************************************
529 * gtk_drag_release_ipc_widget:
530 * Releases widget retrieved with gtk_drag_get_ipc_widget ()
532 * widget: the widget to release.
534 ***************************************************************/
537 gtk_drag_release_ipc_widget (GtkWidget *widget)
539 GtkWindow *window = GTK_WINDOW (widget);
540 GdkScreen *screen = gtk_widget_get_screen (widget);
541 GdkDragContext *context = g_object_get_data (G_OBJECT (widget), "drag-context");
542 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
543 "gtk-dnd-ipc-widgets");
544 GdkDevice *pointer, *keyboard;
548 pointer = gdk_drag_context_get_device (context);
549 keyboard = gdk_device_get_associated_device (pointer);
552 ungrab_dnd_keys (widget, keyboard, GDK_CURRENT_TIME);
555 if (gtk_window_has_group (window))
556 gtk_window_group_remove_window (gtk_window_get_group (window),
558 drag_widgets = g_slist_prepend (drag_widgets, widget);
559 g_object_set_data (G_OBJECT (screen),
560 I_("gtk-dnd-ipc-widgets"),
565 gtk_drag_get_event_time (GdkEvent *event)
567 guint32 tm = GDK_CURRENT_TIME;
572 case GDK_MOTION_NOTIFY:
573 tm = event->motion.time; break;
574 case GDK_BUTTON_PRESS:
575 case GDK_2BUTTON_PRESS:
576 case GDK_3BUTTON_PRESS:
577 case GDK_BUTTON_RELEASE:
578 tm = event->button.time; break;
580 case GDK_KEY_RELEASE:
581 tm = event->key.time; break;
582 case GDK_ENTER_NOTIFY:
583 case GDK_LEAVE_NOTIFY:
584 tm = event->crossing.time; break;
585 case GDK_PROPERTY_NOTIFY:
586 tm = event->property.time; break;
587 case GDK_SELECTION_CLEAR:
588 case GDK_SELECTION_REQUEST:
589 case GDK_SELECTION_NOTIFY:
590 tm = event->selection.time; break;
591 case GDK_PROXIMITY_IN:
592 case GDK_PROXIMITY_OUT:
593 tm = event->proximity.time; break;
594 default: /* use current time */
602 gtk_drag_get_event_actions (GdkEvent *event,
604 GdkDragAction actions,
605 GdkDragAction *suggested_action,
606 GdkDragAction *possible_actions)
608 *suggested_action = 0;
609 *possible_actions = 0;
613 GdkModifierType state = 0;
617 case GDK_MOTION_NOTIFY:
618 state = event->motion.state;
620 case GDK_BUTTON_PRESS:
621 case GDK_2BUTTON_PRESS:
622 case GDK_3BUTTON_PRESS:
623 case GDK_BUTTON_RELEASE:
624 state = event->button.state;
627 case GDK_KEY_RELEASE:
628 state = event->key.state;
630 case GDK_ENTER_NOTIFY:
631 case GDK_LEAVE_NOTIFY:
632 state = event->crossing.state;
638 if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
640 *suggested_action = GDK_ACTION_ASK;
641 *possible_actions = actions;
643 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
645 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
647 if (actions & GDK_ACTION_LINK)
649 *suggested_action = GDK_ACTION_LINK;
650 *possible_actions = GDK_ACTION_LINK;
653 else if (state & GDK_CONTROL_MASK)
655 if (actions & GDK_ACTION_COPY)
657 *suggested_action = GDK_ACTION_COPY;
658 *possible_actions = GDK_ACTION_COPY;
664 if (actions & GDK_ACTION_MOVE)
666 *suggested_action = GDK_ACTION_MOVE;
667 *possible_actions = GDK_ACTION_MOVE;
674 *possible_actions = actions;
676 if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
677 *suggested_action = GDK_ACTION_ASK;
678 else if (actions & GDK_ACTION_COPY)
679 *suggested_action = GDK_ACTION_COPY;
680 else if (actions & GDK_ACTION_MOVE)
681 *suggested_action = GDK_ACTION_MOVE;
682 else if (actions & GDK_ACTION_LINK)
683 *suggested_action = GDK_ACTION_LINK;
688 *possible_actions = actions;
690 if (actions & GDK_ACTION_COPY)
691 *suggested_action = GDK_ACTION_COPY;
692 else if (actions & GDK_ACTION_MOVE)
693 *suggested_action = GDK_ACTION_MOVE;
694 else if (actions & GDK_ACTION_LINK)
695 *suggested_action = GDK_ACTION_LINK;
700 gtk_drag_can_use_rgba_cursor (GdkDisplay *display,
704 guint max_width, max_height;
706 if (!gdk_display_supports_cursor_color (display))
709 if (!gdk_display_supports_cursor_alpha (display))
712 gdk_display_get_maximal_cursor_size (display,
715 if (width > max_width || height > max_height)
717 /* can't use rgba cursor (too large) */
725 gtk_drag_get_cursor (GdkDisplay *display,
726 GdkDragAction action,
727 GtkDragSourceInfo *info)
731 /* reconstruct the cursors for each new drag (thus !info),
732 * to catch cursor theme changes
736 for (i = 0 ; i < n_drag_cursors - 1; i++)
737 if (drag_cursors[i].cursor != NULL)
739 gdk_cursor_unref (drag_cursors[i].cursor);
740 drag_cursors[i].cursor = NULL;
744 for (i = 0 ; i < n_drag_cursors - 1; i++)
745 if (drag_cursors[i].action == action)
748 if (drag_cursors[i].pixbuf == NULL)
749 drag_cursors[i].pixbuf =
750 gdk_pixbuf_new_from_inline (-1, drag_cursors[i].data, FALSE, NULL);
752 if (drag_cursors[i].cursor != NULL)
754 if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
756 gdk_cursor_unref (drag_cursors[i].cursor);
757 drag_cursors[i].cursor = NULL;
761 if (drag_cursors[i].cursor == NULL)
762 drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
764 if (drag_cursors[i].cursor == NULL)
765 drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);
767 if (info && info->icon_pixbuf)
769 gint cursor_width, cursor_height;
770 gint icon_width, icon_height;
772 GdkPixbuf *cursor_pixbuf, *pixbuf;
774 gint icon_x, icon_y, ref_x, ref_y;
776 if (info->drag_cursors[i] != NULL)
778 if (display == gdk_cursor_get_display (info->drag_cursors[i]))
779 return info->drag_cursors[i];
781 gdk_cursor_unref (info->drag_cursors[i]);
782 info->drag_cursors[i] = NULL;
785 icon_x = info->hot_x;
786 icon_y = info->hot_y;
787 icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
788 icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);
791 cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
793 cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
796 if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
797 hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
799 if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
800 hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
803 /* The code below is an attempt to let cursor themes
804 * determine the attachment of the icon to enable things
805 * like the following:
813 * It does not work since Xcursor doesn't allow to attach
814 * any additional information to cursors in a retrievable
815 * way (there are comments, but no way to get at them
816 * short of searching for the actual cursor file).
817 * If this code ever gets used, the icon_window placement
818 * must be changed to recognize these placement options
819 * as well. Note that this code ignores info->hot_x/y.
821 for (j = 0; j < 10; j++)
826 GtkAnchorType icon_anchor;
828 g_snprintf (key, 32, "comment%d", j);
829 opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
830 if (opt && g_str_has_prefix ("icon-attach:", opt))
832 toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
833 if (g_strv_length (toks) != 3)
838 icon_anchor = atoi (toks[0]);
839 icon_x = atoi (toks[1]);
840 icon_y = atoi (toks[2]);
844 case GTK_ANCHOR_NORTH:
845 case GTK_ANCHOR_CENTER:
846 case GTK_ANCHOR_SOUTH:
847 icon_x += icon_width / 2;
849 case GTK_ANCHOR_NORTH_EAST:
850 case GTK_ANCHOR_EAST:
851 case GTK_ANCHOR_SOUTH_EAST:
852 icon_x += icon_width;
859 case GTK_ANCHOR_WEST:
860 case GTK_ANCHOR_CENTER:
861 case GTK_ANCHOR_EAST:
862 icon_y += icon_height / 2;
864 case GTK_ANCHOR_SOUTH_WEST:
865 case GTK_ANCHOR_SOUTH:
866 case GTK_ANCHOR_SOUTH_EAST:
867 icon_x += icon_height;
879 cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
880 cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
882 ref_x = MAX (hot_x, icon_x);
883 ref_y = MAX (hot_y, icon_y);
884 width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
885 height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
887 if (gtk_drag_can_use_rgba_cursor (display, width, height))
889 /* Composite cursor and icon so that both hotspots
890 * end up at (ref_x, ref_y)
892 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
895 gdk_pixbuf_fill (pixbuf, 0xff000000);
897 gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
898 ref_x - icon_x, ref_y - icon_y,
899 icon_width, icon_height,
900 ref_x - icon_x, ref_y - icon_y,
902 GDK_INTERP_BILINEAR, 255);
904 gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
905 ref_x - hot_x, ref_y - hot_y,
906 cursor_width, cursor_height,
907 ref_x - hot_x, ref_y - hot_y,
909 GDK_INTERP_BILINEAR, 255);
911 info->drag_cursors[i] =
912 gdk_cursor_new_from_pixbuf (display, pixbuf, ref_x, ref_y);
914 g_object_unref (pixbuf);
917 g_object_unref (cursor_pixbuf);
919 if (info->drag_cursors[i] != NULL)
920 return info->drag_cursors[i];
923 return drag_cursors[i].cursor;
927 gtk_drag_update_cursor (GtkDragSourceInfo *info)
932 if (!info->have_grab)
935 for (i = 0 ; i < n_drag_cursors - 1; i++)
936 if (info->cursor == drag_cursors[i].cursor ||
937 info->cursor == info->drag_cursors[i])
940 if (i == n_drag_cursors)
943 cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor),
944 drag_cursors[i].action, info);
946 if (cursor != info->cursor)
950 pointer = gdk_drag_context_get_device (info->context);
951 gdk_device_grab (pointer,
952 gtk_widget_get_window (info->ipc_widget),
953 GDK_OWNERSHIP_APPLICATION, FALSE,
954 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
955 cursor, info->grab_time);
956 info->cursor = cursor;
960 /********************
962 ********************/
964 /*************************************************************
966 * Get the data for a drag or drop
968 * context - drag context
969 * target - format to retrieve the data in.
970 * time - timestamp of triggering event.
973 *************************************************************/
976 gtk_drag_get_data (GtkWidget *widget,
977 GdkDragContext *context,
981 GtkWidget *selection_widget;
983 g_return_if_fail (GTK_IS_WIDGET (widget));
984 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
986 selection_widget = gtk_drag_get_ipc_widget (widget);
988 g_object_ref (context);
989 g_object_ref (widget);
991 g_signal_connect (selection_widget, "selection-received",
992 G_CALLBACK (gtk_drag_selection_received), widget);
994 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
996 gtk_selection_convert (selection_widget,
997 gdk_drag_get_selection (context),
1004 * gtk_drag_get_source_widget:
1005 * @context: a (destination side) drag context
1007 * Determines the source widget for a drag.
1009 * Return value: (transfer none): if the drag is occurring
1010 * within a single application, a pointer to the source widget.
1014 gtk_drag_get_source_widget (GdkDragContext *context)
1018 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
1020 tmp_list = source_widgets;
1023 GtkWidget *ipc_widget = tmp_list->data;
1025 if (gtk_widget_get_window (ipc_widget) == gdk_drag_context_get_source_window (context))
1027 GtkDragSourceInfo *info;
1028 info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
1030 return info ? info->widget : NULL;
1033 tmp_list = tmp_list->next;
1039 /*************************************************************
1041 * Notify the drag source that the transfer of data
1044 * context: The drag context for this drag
1045 * success: Was the data successfully transferred?
1046 * time: The timestamp to use when notifying the destination.
1048 *************************************************************/
1051 gtk_drag_finish (GdkDragContext *context,
1056 GdkAtom target = GDK_NONE;
1058 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1062 target = gdk_atom_intern_static_string ("DELETE");
1064 else if (gdk_drag_context_get_protocol (context) == GDK_DRAG_PROTO_MOTIF)
1066 target = gdk_atom_intern_static_string (success ?
1067 "XmTRANSFER_SUCCESS" :
1068 "XmTRANSFER_FAILURE");
1071 if (target != GDK_NONE)
1073 GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_window_get_screen (gdk_drag_context_get_source_window (context)));
1075 g_object_ref (context);
1077 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1078 g_signal_connect (selection_widget, "selection-received",
1079 G_CALLBACK (gtk_drag_selection_received),
1082 gtk_selection_convert (selection_widget,
1083 gdk_drag_get_selection (context),
1088 if (!(success && del))
1089 gdk_drop_finish (context, success, time);
1092 /*************************************************************
1093 * gtk_drag_highlight_draw:
1094 * Callback for expose_event for highlighted widgets.
1100 *************************************************************/
1103 gtk_drag_highlight_draw (GtkWidget *widget,
1107 int width = gtk_widget_get_allocated_width (widget);
1108 int height = gtk_widget_get_allocated_height (widget);
1110 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
1111 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1113 0, 0, width, height);
1115 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
1116 cairo_set_line_width (cr, 1.0);
1117 cairo_rectangle (cr,
1119 width - 1, height - 1);
1125 /*************************************************************
1126 * gtk_drag_highlight:
1127 * Highlight the given widget in the default manner.
1131 *************************************************************/
1134 gtk_drag_highlight (GtkWidget *widget)
1136 g_return_if_fail (GTK_IS_WIDGET (widget));
1138 g_signal_connect_after (widget, "draw",
1139 G_CALLBACK (gtk_drag_highlight_draw),
1142 gtk_widget_queue_draw (widget);
1145 /*************************************************************
1146 * gtk_drag_unhighlight:
1147 * Refresh the given widget to remove the highlight.
1151 *************************************************************/
1154 gtk_drag_unhighlight (GtkWidget *widget)
1156 g_return_if_fail (GTK_IS_WIDGET (widget));
1158 g_signal_handlers_disconnect_by_func (widget,
1159 gtk_drag_highlight_draw,
1162 gtk_widget_queue_draw (widget);
1166 gtk_drag_dest_set_internal (GtkWidget *widget,
1167 GtkDragDestSite *site)
1169 GtkDragDestSite *old_site;
1171 g_return_if_fail (widget != NULL);
1173 /* HACK, do this in the destroy */
1174 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1177 g_signal_handlers_disconnect_by_func (widget,
1178 gtk_drag_dest_realized,
1180 g_signal_handlers_disconnect_by_func (widget,
1181 gtk_drag_dest_hierarchy_changed,
1184 site->track_motion = old_site->track_motion;
1187 if (gtk_widget_get_realized (widget))
1188 gtk_drag_dest_realized (widget);
1190 g_signal_connect (widget, "realize",
1191 G_CALLBACK (gtk_drag_dest_realized), site);
1192 g_signal_connect (widget, "hierarchy-changed",
1193 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1195 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
1196 site, gtk_drag_dest_site_destroy);
1200 * gtk_drag_dest_set:
1201 * @widget: a #GtkWidget
1202 * @flags: which types of default drag behavior to use
1203 * @targets: (allow-none) (array length=n_targets): a pointer to an array of #GtkTargetEntry<!-- -->s
1204 * indicating the drop types that this @widget will accept, or %NULL.
1205 * Later you can access the list with gtk_drag_dest_get_target_list()
1206 * and gtk_drag_dest_find_target().
1207 * @n_targets: the number of entries in @targets
1208 * @actions: a bitmask of possible actions for a drop onto this @widget.
1210 * Sets a widget as a potential drop destination, and adds default behaviors.
1212 * The default behaviors listed in @flags have an effect similar
1213 * to installing default handlers for the widget's drag-and-drop signals
1214 * (#GtkWidget:drag-motion, #GtkWidget:drag-drop, ...). They all exist
1215 * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
1216 * sufficient to connect to the widget's #GtkWidget::drag-data-received
1217 * signal to get primitive, but consistent drag-and-drop support.
1219 * Things become more complicated when you try to preview the dragged data,
1220 * as described in the documentation for #GtkWidget:drag-motion. The default
1221 * behaviors described by @flags make some assumptions, that can conflict
1222 * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
1223 * invokations of gdk_drag_status() in the context of #GtkWidget:drag-motion,
1224 * and invokations of gtk_drag_finish() in #GtkWidget:drag-data-received.
1225 * Especially the later is dramatic, when your own #GtkWidget:drag-motion
1226 * handler calls gtk_drag_get_data() to inspect the dragged data.
1228 * There's no way to set a default action here, you can use the
1229 * #GtkWidget:drag-motion callback for that. Here's an example which selects
1230 * the action to use depending on whether the control key is pressed or not:
1233 * drag_motion (GtkWidget *widget,
1234 * GdkDragContext *context,
1239 * GdkModifierType mask;
1241 * gdk_window_get_pointer (gtk_widget_get_window (widget),
1242 * NULL, NULL, &mask);
1243 * if (mask & GDK_CONTROL_MASK)
1244 * gdk_drag_status (context, GDK_ACTION_COPY, time);
1246 * gdk_drag_status (context, GDK_ACTION_MOVE, time);
1251 gtk_drag_dest_set (GtkWidget *widget,
1252 GtkDestDefaults flags,
1253 const GtkTargetEntry *targets,
1255 GdkDragAction actions)
1257 GtkDragDestSite *site;
1259 g_return_if_fail (GTK_IS_WIDGET (widget));
1261 site = g_slice_new0 (GtkDragDestSite);
1263 site->flags = flags;
1264 site->have_drag = FALSE;
1266 site->target_list = gtk_target_list_new (targets, n_targets);
1268 site->target_list = NULL;
1269 site->actions = actions;
1270 site->do_proxy = FALSE;
1271 site->proxy_window = NULL;
1272 site->track_motion = FALSE;
1274 gtk_drag_dest_set_internal (widget, site);
1277 /*************************************************************
1278 * gtk_drag_dest_set_proxy:
1279 * Set up this widget to proxy drags elsewhere.
1282 * proxy_window: window to which forward drag events
1283 * protocol: Drag protocol which the dest widget accepts
1284 * use_coordinates: If true, send the same coordinates to the
1285 * destination, because it is a embedded
1288 *************************************************************/
1291 gtk_drag_dest_set_proxy (GtkWidget *widget,
1292 GdkWindow *proxy_window,
1293 GdkDragProtocol protocol,
1294 gboolean use_coordinates)
1296 GtkDragDestSite *site;
1298 g_return_if_fail (GTK_IS_WIDGET (widget));
1299 g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1301 site = g_slice_new (GtkDragDestSite);
1304 site->have_drag = FALSE;
1305 site->target_list = NULL;
1307 site->proxy_window = proxy_window;
1309 g_object_ref (proxy_window);
1310 site->do_proxy = TRUE;
1311 site->proxy_protocol = protocol;
1312 site->proxy_coords = use_coordinates;
1313 site->track_motion = FALSE;
1315 gtk_drag_dest_set_internal (widget, site);
1318 /*************************************************************
1319 * gtk_drag_dest_unset
1320 * Unregister this widget as a drag target.
1324 *************************************************************/
1327 gtk_drag_dest_unset (GtkWidget *widget)
1329 GtkDragDestSite *old_site;
1331 g_return_if_fail (GTK_IS_WIDGET (widget));
1333 old_site = g_object_get_data (G_OBJECT (widget),
1337 g_signal_handlers_disconnect_by_func (widget,
1338 gtk_drag_dest_realized,
1340 g_signal_handlers_disconnect_by_func (widget,
1341 gtk_drag_dest_hierarchy_changed,
1345 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1349 * gtk_drag_dest_get_target_list:
1350 * @widget: a #GtkWidget
1352 * Returns the list of targets this widget can accept from
1355 * Return value: the #GtkTargetList, or %NULL if none
1358 gtk_drag_dest_get_target_list (GtkWidget *widget)
1360 GtkDragDestSite *site;
1362 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1364 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1366 return site ? site->target_list : NULL;
1370 * gtk_drag_dest_set_target_list:
1371 * @widget: a #GtkWidget that's a drag destination
1372 * @target_list: (allow-none): list of droppable targets, or %NULL for none
1374 * Sets the target types that this widget can accept from drag-and-drop.
1375 * The widget must first be made into a drag destination with
1376 * gtk_drag_dest_set().
1379 gtk_drag_dest_set_target_list (GtkWidget *widget,
1380 GtkTargetList *target_list)
1382 GtkDragDestSite *site;
1384 g_return_if_fail (GTK_IS_WIDGET (widget));
1386 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1390 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1391 "to make the widget into a drag destination");
1396 gtk_target_list_ref (target_list);
1398 if (site->target_list)
1399 gtk_target_list_unref (site->target_list);
1401 site->target_list = target_list;
1405 * gtk_drag_dest_add_text_targets:
1406 * @widget: a #GtkWidget that's a drag destination
1408 * Add the text targets supported by #GtkSelection to
1409 * the target list of the drag destination. The targets
1410 * are added with @info = 0. If you need another value,
1411 * use gtk_target_list_add_text_targets() and
1412 * gtk_drag_dest_set_target_list().
1417 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1419 GtkTargetList *target_list;
1421 target_list = gtk_drag_dest_get_target_list (widget);
1423 gtk_target_list_ref (target_list);
1425 target_list = gtk_target_list_new (NULL, 0);
1426 gtk_target_list_add_text_targets (target_list, 0);
1427 gtk_drag_dest_set_target_list (widget, target_list);
1428 gtk_target_list_unref (target_list);
1432 * gtk_drag_dest_add_image_targets:
1433 * @widget: a #GtkWidget that's a drag destination
1435 * Add the image targets supported by #GtkSelection to
1436 * the target list of the drag destination. The targets
1437 * are added with @info = 0. If you need another value,
1438 * use gtk_target_list_add_image_targets() and
1439 * gtk_drag_dest_set_target_list().
1444 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1446 GtkTargetList *target_list;
1448 target_list = gtk_drag_dest_get_target_list (widget);
1450 gtk_target_list_ref (target_list);
1452 target_list = gtk_target_list_new (NULL, 0);
1453 gtk_target_list_add_image_targets (target_list, 0, FALSE);
1454 gtk_drag_dest_set_target_list (widget, target_list);
1455 gtk_target_list_unref (target_list);
1459 * gtk_drag_dest_add_uri_targets:
1460 * @widget: a #GtkWidget that's a drag destination
1462 * Add the URI targets supported by #GtkSelection to
1463 * the target list of the drag destination. The targets
1464 * are added with @info = 0. If you need another value,
1465 * use gtk_target_list_add_uri_targets() and
1466 * gtk_drag_dest_set_target_list().
1471 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1473 GtkTargetList *target_list;
1475 target_list = gtk_drag_dest_get_target_list (widget);
1477 gtk_target_list_ref (target_list);
1479 target_list = gtk_target_list_new (NULL, 0);
1480 gtk_target_list_add_uri_targets (target_list, 0);
1481 gtk_drag_dest_set_target_list (widget, target_list);
1482 gtk_target_list_unref (target_list);
1486 * gtk_drag_dest_set_track_motion:
1487 * @widget: a #GtkWidget that's a drag destination
1488 * @track_motion: whether to accept all targets
1490 * Tells the widget to emit ::drag-motion and ::drag-leave
1491 * events regardless of the targets and the %GTK_DEST_DEFAULT_MOTION
1494 * This may be used when a widget wants to do generic
1495 * actions regardless of the targets that the source offers.
1500 gtk_drag_dest_set_track_motion (GtkWidget *widget,
1501 gboolean track_motion)
1503 GtkDragDestSite *site;
1505 g_return_if_fail (GTK_IS_WIDGET (widget));
1507 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1509 g_return_if_fail (site != NULL);
1511 site->track_motion = track_motion != FALSE;
1515 * gtk_drag_dest_get_track_motion:
1516 * @widget: a #GtkWidget that's a drag destination
1518 * Returns whether the widget has been configured to always
1519 * emit ::drag-motion signals.
1521 * Return Value: %TRUE if the widget always emits ::drag-motion events
1526 gtk_drag_dest_get_track_motion (GtkWidget *widget)
1528 GtkDragDestSite *site;
1530 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1532 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1535 return site->track_motion;
1540 /*************************************************************
1541 * _gtk_drag_dest_handle_event:
1542 * Called from widget event handling code on Drag events
1546 * toplevel: Toplevel widget that received the event
1549 *************************************************************/
1552 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1555 GtkDragDestInfo *info;
1556 GdkDragContext *context;
1558 g_return_if_fail (toplevel != NULL);
1559 g_return_if_fail (event != NULL);
1561 context = event->dnd.context;
1563 info = gtk_drag_get_dest_info (context, TRUE);
1565 /* Find the widget for the event */
1566 switch (event->type)
1568 case GDK_DRAG_ENTER:
1571 case GDK_DRAG_LEAVE:
1574 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1575 info->widget = NULL;
1579 case GDK_DRAG_MOTION:
1580 case GDK_DROP_START:
1586 if (event->type == GDK_DROP_START)
1588 info->dropped = TRUE;
1589 /* We send a leave here so that the widget unhighlights
1594 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1595 info->widget = NULL;
1599 window = gtk_widget_get_window (toplevel);
1601 #ifdef GDK_WINDOWING_X11
1602 /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1604 * Currently gdk_window_get_position doesn't provide reliable
1605 * information for embedded windows, so we call the much more
1606 * expensive gdk_window_get_origin().
1608 if (GTK_IS_PLUG (toplevel))
1609 gdk_window_get_origin (window, &tx, &ty);
1611 #endif /* GDK_WINDOWING_X11 */
1612 gdk_window_get_position (window, &tx, &ty);
1614 found = gtk_drag_find_widget (toplevel,
1617 event->dnd.x_root - tx,
1618 event->dnd.y_root - ty,
1620 (event->type == GDK_DRAG_MOTION) ?
1621 gtk_drag_dest_motion :
1622 gtk_drag_dest_drop);
1624 if (info->widget && !found)
1626 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1627 info->widget = NULL;
1632 if (event->type == GDK_DRAG_MOTION)
1635 gdk_drag_status (context, 0, event->dnd.time);
1637 else if (event->type == GDK_DROP_START && !info->proxy_source)
1639 gdk_drop_reply (context, found, event->dnd.time);
1640 if ((gdk_drag_context_get_protocol (context) == GDK_DRAG_PROTO_MOTIF) && !found)
1641 gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1647 g_assert_not_reached ();
1652 * gtk_drag_dest_find_target:
1653 * @widget: drag destination widget
1654 * @context: drag context
1655 * @target_list: (allow-none): list of droppable targets, or %NULL to use
1656 * gtk_drag_dest_get_target_list (@widget).
1658 * Looks for a match between the supported targets of @context and the
1659 * @dest_target_list, returning the first matching target, otherwise
1660 * returning %GDK_NONE. @dest_target_list should usually be the return
1661 * value from gtk_drag_dest_get_target_list(), but some widgets may
1662 * have different valid targets for different parts of the widget; in
1663 * that case, they will have to implement a drag_motion handler that
1664 * passes the correct target list to this function.
1666 * Return value: first target that the source offers and the dest can
1667 * accept, or %GDK_NONE
1670 gtk_drag_dest_find_target (GtkWidget *widget,
1671 GdkDragContext *context,
1672 GtkTargetList *target_list)
1675 GList *tmp_source = NULL;
1676 GtkWidget *source_widget;
1678 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1679 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1682 source_widget = gtk_drag_get_source_widget (context);
1684 if (target_list == NULL)
1685 target_list = gtk_drag_dest_get_target_list (widget);
1687 if (target_list == NULL)
1690 tmp_target = target_list->list;
1693 GtkTargetPair *pair = tmp_target->data;
1694 tmp_source = gdk_drag_context_list_targets (context);
1697 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1699 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1700 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)) &&
1701 (!(pair->flags & GTK_TARGET_OTHER_APP) || !source_widget) &&
1702 (!(pair->flags & GTK_TARGET_OTHER_WIDGET) || (source_widget != widget)))
1703 return pair->target;
1707 tmp_source = tmp_source->next;
1709 tmp_target = tmp_target->next;
1716 gtk_drag_selection_received (GtkWidget *widget,
1717 GtkSelectionData *selection_data,
1721 GdkDragContext *context;
1722 GtkDragDestInfo *info;
1723 GtkWidget *drop_widget;
1728 context = g_object_get_data (G_OBJECT (widget), "drag-context");
1729 info = gtk_drag_get_dest_info (context, FALSE);
1731 if (info->proxy_data &&
1732 gtk_selection_data_get_target (info->proxy_data) == gtk_selection_data_get_target (selection_data))
1734 gtk_selection_data_set (info->proxy_data,
1735 gtk_selection_data_get_data_type (selection_data),
1736 gtk_selection_data_get_format (selection_data),
1737 gtk_selection_data_get_data (selection_data),
1738 gtk_selection_data_get_length (selection_data));
1743 target = gtk_selection_data_get_target (selection_data);
1744 if (target == gdk_atom_intern_static_string ("DELETE"))
1746 gtk_drag_finish (context, TRUE, FALSE, time);
1748 else if ((target == gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS")) ||
1749 (target == gdk_atom_intern_static_string ("XmTRANSFER_FAILURE")))
1755 GtkDragDestSite *site;
1757 site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1759 if (site && site->target_list)
1763 if (gtk_target_list_find (site->target_list,
1767 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1768 gtk_selection_data_get_length (selection_data) >= 0)
1769 g_signal_emit_by_name (drop_widget,
1770 "drag-data-received",
1771 context, info->drop_x, info->drop_y,
1778 g_signal_emit_by_name (drop_widget,
1779 "drag-data-received",
1780 context, info->drop_x, info->drop_y,
1785 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1788 gtk_drag_finish (context,
1789 (gtk_selection_data_get_length (selection_data) >= 0),
1790 (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
1794 g_object_unref (drop_widget);
1797 g_signal_handlers_disconnect_by_func (widget,
1798 gtk_drag_selection_received,
1801 g_object_set_data (G_OBJECT (widget), I_("drag-context"), NULL);
1802 g_object_unref (context);
1804 gtk_drag_release_ipc_widget (widget);
1807 /*************************************************************
1808 * gtk_drag_find_widget:
1809 * Function used to locate widgets for
1810 * DRAG_MOTION and DROP_START events.
1811 *************************************************************/
1814 gtk_drag_find_widget (GtkWidget *widget,
1815 GdkDragContext *context,
1816 GtkDragDestInfo *info,
1820 GtkDragDestCallback callback)
1822 if (!gtk_widget_get_mapped (widget) ||
1823 !gtk_widget_get_sensitive (widget))
1826 /* Get the widget at the pointer coordinates and travel up
1827 * the widget hierarchy from there.
1829 widget = _gtk_widget_find_at_coords (gtk_widget_get_window (widget),
1837 GList *hierarchy = NULL;
1838 gboolean found = FALSE;
1840 if (!gtk_widget_get_mapped (widget) ||
1841 !gtk_widget_get_sensitive (widget))
1844 /* need to reference the entire hierarchy temporarily in case the
1845 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
1847 for (parent = widget;
1849 parent = gtk_widget_get_parent (parent))
1851 hierarchy = g_list_prepend (hierarchy, g_object_ref (parent));
1854 /* If the current widget is registered as a drop site, check to
1855 * emit "drag-motion" to check if we are actually in a drop
1858 if (g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1860 found = callback (widget, context, x, y, time);
1862 /* If so, send a "drag-leave" to the last widget */
1865 if (info->widget && info->widget != widget)
1867 gtk_drag_dest_leave (info->widget, context, time);
1870 info->widget = widget;
1876 /* Get the parent before unreffing the hierarchy because
1877 * invoking the callback might have destroyed the widget
1879 parent = gtk_widget_get_parent (widget);
1881 /* The parent might be going away when unreffing the
1882 * hierarchy, so also protect againt that
1885 g_object_add_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1888 g_list_foreach (hierarchy, (GFunc) g_object_unref, NULL);
1889 g_list_free (hierarchy);
1895 g_object_remove_weak_pointer (G_OBJECT (parent), (gpointer *) &parent);
1899 if (!gtk_widget_translate_coordinates (widget, parent, x, y, &x, &y))
1909 gtk_drag_proxy_begin (GtkWidget *widget,
1910 GtkDragDestInfo *dest_info,
1913 GtkDragSourceInfo *source_info;
1915 GdkDragContext *context;
1916 GtkWidget *ipc_widget;
1918 if (dest_info->proxy_source)
1920 gdk_drag_abort (dest_info->proxy_source->context, time);
1921 gtk_drag_source_info_destroy (dest_info->proxy_source);
1922 dest_info->proxy_source = NULL;
1925 ipc_widget = gtk_drag_get_ipc_widget (widget);
1926 context = gdk_drag_begin (gtk_widget_get_window (ipc_widget),
1927 gdk_drag_context_list_targets (dest_info->context));
1929 source_info = gtk_drag_get_source_info (context, TRUE);
1931 source_info->ipc_widget = ipc_widget;
1932 source_info->widget = g_object_ref (widget);
1934 source_info->target_list = gtk_target_list_new (NULL, 0);
1935 tmp_list = gdk_drag_context_list_targets (dest_info->context);
1938 gtk_target_list_add (source_info->target_list,
1939 GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
1940 tmp_list = tmp_list->next;
1943 source_info->proxy_dest = dest_info;
1945 g_signal_connect (ipc_widget,
1947 G_CALLBACK (gtk_drag_selection_get),
1950 dest_info->proxy_source = source_info;
1954 gtk_drag_dest_info_destroy (gpointer data)
1956 g_slice_free (GtkDragDestInfo, data);
1959 static GtkDragDestInfo *
1960 gtk_drag_get_dest_info (GdkDragContext *context,
1963 GtkDragDestInfo *info;
1964 static GQuark info_quark = 0;
1966 info_quark = g_quark_from_static_string ("gtk-dest-info");
1968 info = g_object_get_qdata (G_OBJECT (context), info_quark);
1969 if (!info && create)
1971 info = g_slice_new0 (GtkDragDestInfo);
1972 info->context = context;
1973 g_object_set_qdata_full (G_OBJECT (context), info_quark,
1974 info, gtk_drag_dest_info_destroy);
1980 static GQuark dest_info_quark = 0;
1982 static GtkDragSourceInfo *
1983 gtk_drag_get_source_info (GdkDragContext *context,
1986 GtkDragSourceInfo *info;
1987 if (!dest_info_quark)
1988 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
1990 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
1991 if (!info && create)
1993 info = g_new0 (GtkDragSourceInfo, 1);
1994 info->context = context;
1995 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
2002 gtk_drag_clear_source_info (GdkDragContext *context)
2004 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
2008 gtk_drag_dest_realized (GtkWidget *widget)
2010 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2012 if (gtk_widget_is_toplevel (toplevel))
2013 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2017 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
2018 GtkWidget *previous_toplevel)
2020 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2022 if (gtk_widget_is_toplevel (toplevel) && gtk_widget_get_realized (toplevel))
2023 gdk_window_register_dnd (gtk_widget_get_window (toplevel));
2027 gtk_drag_dest_site_destroy (gpointer data)
2029 GtkDragDestSite *site = data;
2031 if (site->proxy_window)
2032 g_object_unref (site->proxy_window);
2034 if (site->target_list)
2035 gtk_target_list_unref (site->target_list);
2037 g_slice_free (GtkDragDestSite, site);
2041 * Default drag handlers
2044 gtk_drag_dest_leave (GtkWidget *widget,
2045 GdkDragContext *context,
2048 GtkDragDestSite *site;
2050 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2051 g_return_if_fail (site != NULL);
2055 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2057 if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
2059 gdk_drag_abort (info->proxy_source->context, time);
2060 gtk_drag_source_info_destroy (info->proxy_source);
2061 info->proxy_source = NULL;
2068 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
2069 gtk_drag_unhighlight (widget);
2071 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
2073 g_signal_emit_by_name (widget, "drag-leave", context, time);
2075 site->have_drag = FALSE;
2080 gtk_drag_dest_motion (GtkWidget *widget,
2081 GdkDragContext *context,
2086 GtkDragDestSite *site;
2087 GdkDragAction action = 0;
2090 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2091 g_return_val_if_fail (site != NULL, FALSE);
2096 GdkEvent *current_event;
2097 GdkWindow *dest_window;
2098 GdkDragProtocol proto;
2100 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2102 if (!info->proxy_source || info->proxy_source->widget != widget)
2103 gtk_drag_proxy_begin (widget, info, time);
2105 current_event = gtk_get_current_event ();
2107 if (site->proxy_window)
2109 dest_window = site->proxy_window;
2110 proto = site->proxy_protocol;
2114 gdk_drag_find_window_for_screen (info->proxy_source->context,
2116 gdk_window_get_screen (current_event->dnd.window),
2117 current_event->dnd.x_root,
2118 current_event->dnd.y_root,
2119 &dest_window, &proto);
2122 gdk_drag_motion (info->proxy_source->context,
2124 current_event->dnd.x_root,
2125 current_event->dnd.y_root,
2126 gdk_drag_context_get_suggested_action (context),
2127 gdk_drag_context_get_actions (context),
2130 if (!site->proxy_window && dest_window)
2131 g_object_unref (dest_window);
2133 selection = gdk_drag_get_selection (info->proxy_source->context);
2135 selection != gdk_drag_get_selection (info->context))
2136 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2138 gdk_event_free (current_event);
2143 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
2145 if (gdk_drag_context_get_suggested_action (context) & site->actions)
2146 action = gdk_drag_context_get_suggested_action (context);
2151 for (i = 0; i < 8; i++)
2153 if ((site->actions & (1 << i)) &&
2154 (gdk_drag_context_get_actions (context) & (1 << i)))
2162 if (action && gtk_drag_dest_find_target (widget, context, NULL))
2164 if (!site->have_drag)
2166 site->have_drag = TRUE;
2167 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
2168 gtk_drag_highlight (widget);
2171 gdk_drag_status (context, action, time);
2175 gdk_drag_status (context, 0, time);
2176 if (!site->track_motion)
2181 g_signal_emit_by_name (widget, "drag-motion",
2182 context, x, y, time, &retval);
2184 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
2188 gtk_drag_dest_drop (GtkWidget *widget,
2189 GdkDragContext *context,
2194 GtkDragDestSite *site;
2195 GtkDragDestInfo *info;
2197 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2198 g_return_val_if_fail (site != NULL, FALSE);
2200 info = gtk_drag_get_dest_info (context, FALSE);
2201 g_return_val_if_fail (info != NULL, FALSE);
2208 if (info->proxy_source ||
2209 (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_ROOTWIN))
2211 gtk_drag_drop (info->proxy_source, time);
2215 /* We need to synthesize a motion event, wait for a status,
2216 * and, if we get a good one, do a drop.
2219 GdkEvent *current_event;
2221 GdkWindow *dest_window;
2222 GdkDragProtocol proto;
2224 gtk_drag_proxy_begin (widget, info, time);
2225 info->proxy_drop_wait = TRUE;
2226 info->proxy_drop_time = time;
2228 current_event = gtk_get_current_event ();
2230 if (site->proxy_window)
2232 dest_window = site->proxy_window;
2233 proto = site->proxy_protocol;
2237 gdk_drag_find_window_for_screen (info->proxy_source->context,
2239 gdk_window_get_screen (current_event->dnd.window),
2240 current_event->dnd.x_root,
2241 current_event->dnd.y_root,
2242 &dest_window, &proto);
2245 gdk_drag_motion (info->proxy_source->context,
2247 current_event->dnd.x_root,
2248 current_event->dnd.y_root,
2249 gdk_drag_context_get_suggested_action (context),
2250 gdk_drag_context_get_actions (context),
2253 if (!site->proxy_window && dest_window)
2254 g_object_unref (dest_window);
2256 selection = gdk_drag_get_selection (info->proxy_source->context);
2258 selection != gdk_drag_get_selection (info->context))
2259 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2261 gdk_event_free (current_event);
2270 if (site->flags & GTK_DEST_DEFAULT_DROP)
2272 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2274 if (target == GDK_NONE)
2276 gtk_drag_finish (context, FALSE, FALSE, time);
2280 gtk_drag_get_data (widget, context, target, time);
2283 g_signal_emit_by_name (widget, "drag-drop",
2284 context, x, y, time, &retval);
2286 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2294 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2295 * so that we can set the icon from the source site information
2297 static GdkDragContext *
2298 gtk_drag_begin_internal (GtkWidget *widget,
2299 GtkDragSourceSite *site,
2300 GtkTargetList *target_list,
2301 GdkDragAction actions,
2305 GtkDragSourceInfo *info;
2306 GList *targets = NULL;
2308 guint32 time = GDK_CURRENT_TIME;
2309 GdkDragAction possible_actions, suggested_action;
2310 GdkDragContext *context;
2311 GtkWidget *ipc_widget;
2313 GdkDevice *pointer, *keyboard;
2314 GdkWindow *ipc_window;
2316 pointer = keyboard = NULL;
2317 ipc_widget = gtk_drag_get_ipc_widget (widget);
2319 gtk_drag_get_event_actions (event, button, actions,
2320 &suggested_action, &possible_actions);
2322 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2328 time = gdk_event_get_time (event);
2329 if (time == GDK_CURRENT_TIME)
2330 time = gtk_get_current_event_time ();
2332 pointer = gdk_event_get_device (event);
2334 if (gdk_device_get_source (pointer) == GDK_SOURCE_KEYBOARD)
2337 pointer = gdk_device_get_associated_device (keyboard);
2340 keyboard = gdk_device_get_associated_device (pointer);
2344 GdkDeviceManager *device_manager;
2346 device_manager = gdk_display_get_device_manager (gtk_widget_get_display (widget));
2347 pointer = gdk_device_manager_get_client_pointer (device_manager);
2348 keyboard = gdk_device_get_associated_device (pointer);
2354 ipc_window = gtk_widget_get_window (ipc_widget);
2356 if (gdk_device_grab (pointer, ipc_window,
2357 GDK_OWNERSHIP_APPLICATION, FALSE,
2358 GDK_POINTER_MOTION_MASK |
2359 GDK_BUTTON_RELEASE_MASK,
2360 cursor, time) != GDK_GRAB_SUCCESS)
2362 gtk_drag_release_ipc_widget (ipc_widget);
2367 grab_dnd_keys (ipc_widget, keyboard, time);
2369 /* We use a GTK grab here to override any grabs that the widget
2370 * we are dragging from might have held
2372 gtk_device_grab_add (ipc_widget, pointer, FALSE);
2374 tmp_list = g_list_last (target_list->list);
2377 GtkTargetPair *pair = tmp_list->data;
2378 targets = g_list_prepend (targets,
2379 GINT_TO_POINTER (pair->target));
2380 tmp_list = tmp_list->prev;
2383 source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2385 context = gdk_drag_begin (ipc_window, targets);
2386 gdk_drag_context_set_device (context, pointer);
2387 g_list_free (targets);
2389 info = gtk_drag_get_source_info (context, TRUE);
2391 info->ipc_widget = ipc_widget;
2392 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), info);
2394 info->widget = g_object_ref (widget);
2396 info->button = button;
2397 info->cursor = cursor;
2398 info->target_list = target_list;
2399 gtk_target_list_ref (target_list);
2401 info->possible_actions = actions;
2403 info->status = GTK_DRAG_STATUS_DRAG;
2404 info->last_event = NULL;
2405 info->selections = NULL;
2406 info->icon_window = NULL;
2407 info->destroy_icon = FALSE;
2409 /* Set cur_x, cur_y here so if the "drag-begin" signal shows
2410 * the drag icon, it will be in the right place
2412 if (event && event->type == GDK_MOTION_NOTIFY)
2414 info->cur_screen = gtk_widget_get_screen (widget);
2415 info->cur_x = event->motion.x_root;
2416 info->cur_y = event->motion.y_root;
2420 gdk_display_get_device_state (gtk_widget_get_display (widget), pointer,
2421 &info->cur_screen, &info->cur_x, &info->cur_y, NULL);
2424 g_signal_emit_by_name (widget, "drag-begin", info->context);
2426 /* Ensure that we have an icon before we start the drag; the
2427 * application may have set one in ::drag_begin, or it may
2430 if (!info->icon_window && !info->icon_pixbuf)
2432 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2433 gtk_drag_set_icon_default (context);
2435 switch (site->icon_type)
2437 case GTK_IMAGE_PIXBUF:
2438 gtk_drag_set_icon_pixbuf (context,
2439 site->icon_data.pixbuf.pixbuf,
2442 case GTK_IMAGE_STOCK:
2443 gtk_drag_set_icon_stock (context,
2444 site->icon_data.stock.stock_id,
2447 case GTK_IMAGE_ICON_NAME:
2448 gtk_drag_set_icon_name (context,
2449 site->icon_data.name.icon_name,
2452 case GTK_IMAGE_EMPTY:
2454 g_assert_not_reached();
2459 /* We need to composite the icon into the cursor, if we are
2460 * not using an icon window.
2462 if (info->icon_pixbuf)
2464 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2468 if (cursor != info->cursor)
2470 gdk_device_grab (pointer, gtk_widget_get_window (widget),
2471 GDK_OWNERSHIP_APPLICATION, FALSE,
2472 GDK_POINTER_MOTION_MASK |
2473 GDK_BUTTON_RELEASE_MASK,
2475 info->cursor = cursor;
2479 if (event && event->type == GDK_MOTION_NOTIFY)
2480 gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2482 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, event);
2484 info->start_x = info->cur_x;
2485 info->start_y = info->cur_y;
2487 g_signal_connect (info->ipc_widget, "grab-broken-event",
2488 G_CALLBACK (gtk_drag_grab_broken_event_cb), info);
2489 g_signal_connect (info->ipc_widget, "grab-notify",
2490 G_CALLBACK (gtk_drag_grab_notify_cb), info);
2491 g_signal_connect (info->ipc_widget, "button-release-event",
2492 G_CALLBACK (gtk_drag_button_release_cb), info);
2493 g_signal_connect (info->ipc_widget, "motion-notify-event",
2494 G_CALLBACK (gtk_drag_motion_cb), info);
2495 g_signal_connect (info->ipc_widget, "key-press-event",
2496 G_CALLBACK (gtk_drag_key_cb), info);
2497 g_signal_connect (info->ipc_widget, "key-release-event",
2498 G_CALLBACK (gtk_drag_key_cb), info);
2499 g_signal_connect (info->ipc_widget, "selection-get",
2500 G_CALLBACK (gtk_drag_selection_get), info);
2502 info->have_grab = TRUE;
2503 info->grab_time = time;
2505 return info->context;
2510 * @widget: the source widget.
2511 * @targets: The targets (data formats) in which the
2512 * source can provide the data.
2513 * @actions: A bitmask of the allowed drag actions for this drag.
2514 * @button: The button the user clicked to start the drag.
2515 * @event: The event that triggered the start of the drag.
2517 * Initiates a drag on the source side. The function
2518 * only needs to be used when the application is
2519 * starting drags itself, and is not needed when
2520 * gtk_drag_source_set() is used.
2522 * The @event is used to retrieve the timestamp that will be used internally to
2523 * grab the pointer. If @event is #NULL, then GDK_CURRENT_TIME will be used.
2524 * However, you should try to pass a real event in all cases, since that can be
2525 * used by GTK+ to get information about the start position of the drag, for
2526 * example if the @event is a GDK_MOTION_NOTIFY.
2528 * Generally there are three cases when you want to start a drag by hand by
2529 * calling this function:
2531 * 1. During a button-press-event handler, if you want to start a drag
2532 * immediately when the user presses the mouse button. Pass the @event
2533 * that you have in your button-press-event handler.
2535 * 2. During a motion-notify-event handler, if you want to start a drag
2536 * when the mouse moves past a certain threshold distance after a button-press.
2537 * Pass the @event that you have in your motion-notify-event handler.
2539 * 3. During a timeout handler, if you want to start a drag after the mouse
2540 * button is held down for some time. Try to save the last event that you got
2541 * from the mouse, using gdk_event_copy(), and pass it to this function
2542 * (remember to free the event with gdk_event_free() when you are done).
2543 * If you can really not pass a real event, pass #NULL instead.
2545 * Return value: the context for this drag.
2548 gtk_drag_begin (GtkWidget *widget,
2549 GtkTargetList *targets,
2550 GdkDragAction actions,
2554 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2555 g_return_val_if_fail (gtk_widget_get_realized (widget), NULL);
2556 g_return_val_if_fail (targets != NULL, NULL);
2558 return gtk_drag_begin_internal (widget, NULL, targets,
2559 actions, button, event);
2563 * gtk_drag_source_set:
2564 * @widget: a #GtkWidget
2565 * @start_button_mask: the bitmask of buttons that can start the drag
2566 * @targets: (allow-none) (array length=n_targets): the table of targets that the drag will support,
2568 * @n_targets: the number of items in @targets
2569 * @actions: the bitmask of possible actions for a drag from this widget
2571 * Sets up a widget so that GTK+ will start a drag operation when the user
2572 * clicks and drags on the widget. The widget must have a window.
2575 gtk_drag_source_set (GtkWidget *widget,
2576 GdkModifierType start_button_mask,
2577 const GtkTargetEntry *targets,
2579 GdkDragAction actions)
2581 GtkDragSourceSite *site;
2583 g_return_if_fail (GTK_IS_WIDGET (widget));
2585 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2587 gtk_widget_add_events (widget,
2588 gtk_widget_get_events (widget) |
2589 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2590 GDK_BUTTON_MOTION_MASK);
2594 if (site->target_list)
2595 gtk_target_list_unref (site->target_list);
2599 site = g_slice_new0 (GtkDragSourceSite);
2601 site->icon_type = GTK_IMAGE_EMPTY;
2603 g_signal_connect (widget, "button-press-event",
2604 G_CALLBACK (gtk_drag_source_event_cb),
2606 g_signal_connect (widget, "button-release-event",
2607 G_CALLBACK (gtk_drag_source_event_cb),
2609 g_signal_connect (widget, "motion-notify-event",
2610 G_CALLBACK (gtk_drag_source_event_cb),
2613 g_object_set_data_full (G_OBJECT (widget),
2614 I_("gtk-site-data"),
2615 site, gtk_drag_source_site_destroy);
2618 site->start_button_mask = start_button_mask;
2620 site->target_list = gtk_target_list_new (targets, n_targets);
2622 site->actions = actions;
2625 /*************************************************************
2626 * gtk_drag_source_unset
2627 * Unregister this widget as a drag source.
2631 *************************************************************/
2634 gtk_drag_source_unset (GtkWidget *widget)
2636 GtkDragSourceSite *site;
2638 g_return_if_fail (GTK_IS_WIDGET (widget));
2640 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2644 g_signal_handlers_disconnect_by_func (widget,
2645 gtk_drag_source_event_cb,
2647 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
2652 * gtk_drag_source_get_target_list:
2653 * @widget: a #GtkWidget
2655 * Gets the list of targets this widget can provide for
2658 * Return value: the #GtkTargetList, or %NULL if none
2663 gtk_drag_source_get_target_list (GtkWidget *widget)
2665 GtkDragSourceSite *site;
2667 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2669 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2671 return site ? site->target_list : NULL;
2675 * gtk_drag_source_set_target_list:
2676 * @widget: a #GtkWidget that's a drag source
2677 * @target_list: (allow-none): list of draggable targets, or %NULL for none
2679 * Changes the target types that this widget offers for drag-and-drop.
2680 * The widget must first be made into a drag source with
2681 * gtk_drag_source_set().
2686 gtk_drag_source_set_target_list (GtkWidget *widget,
2687 GtkTargetList *target_list)
2689 GtkDragSourceSite *site;
2691 g_return_if_fail (GTK_IS_WIDGET (widget));
2693 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2696 g_warning ("gtk_drag_source_set_target_list() requires the widget "
2697 "to already be a drag source.");
2702 gtk_target_list_ref (target_list);
2704 if (site->target_list)
2705 gtk_target_list_unref (site->target_list);
2707 site->target_list = target_list;
2711 * gtk_drag_source_add_text_targets:
2712 * @widget: a #GtkWidget that's is a drag source
2714 * Add the text targets supported by #GtkSelection to
2715 * the target list of the drag source. The targets
2716 * are added with @info = 0. If you need another value,
2717 * use gtk_target_list_add_text_targets() and
2718 * gtk_drag_source_set_target_list().
2723 gtk_drag_source_add_text_targets (GtkWidget *widget)
2725 GtkTargetList *target_list;
2727 target_list = gtk_drag_source_get_target_list (widget);
2729 gtk_target_list_ref (target_list);
2731 target_list = gtk_target_list_new (NULL, 0);
2732 gtk_target_list_add_text_targets (target_list, 0);
2733 gtk_drag_source_set_target_list (widget, target_list);
2734 gtk_target_list_unref (target_list);
2738 * gtk_drag_source_add_image_targets:
2739 * @widget: a #GtkWidget that's is a drag source
2741 * Add the writable image targets supported by #GtkSelection to
2742 * the target list of the drag source. The targets
2743 * are added with @info = 0. If you need another value,
2744 * use gtk_target_list_add_image_targets() and
2745 * gtk_drag_source_set_target_list().
2750 gtk_drag_source_add_image_targets (GtkWidget *widget)
2752 GtkTargetList *target_list;
2754 target_list = gtk_drag_source_get_target_list (widget);
2756 gtk_target_list_ref (target_list);
2758 target_list = gtk_target_list_new (NULL, 0);
2759 gtk_target_list_add_image_targets (target_list, 0, TRUE);
2760 gtk_drag_source_set_target_list (widget, target_list);
2761 gtk_target_list_unref (target_list);
2765 * gtk_drag_source_add_uri_targets:
2766 * @widget: a #GtkWidget that's is a drag source
2768 * Add the URI targets supported by #GtkSelection to
2769 * the target list of the drag source. The targets
2770 * are added with @info = 0. If you need another value,
2771 * use gtk_target_list_add_uri_targets() and
2772 * gtk_drag_source_set_target_list().
2777 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2779 GtkTargetList *target_list;
2781 target_list = gtk_drag_source_get_target_list (widget);
2783 gtk_target_list_ref (target_list);
2785 target_list = gtk_target_list_new (NULL, 0);
2786 gtk_target_list_add_uri_targets (target_list, 0);
2787 gtk_drag_source_set_target_list (widget, target_list);
2788 gtk_target_list_unref (target_list);
2792 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2794 switch (site->icon_type)
2796 case GTK_IMAGE_EMPTY:
2798 case GTK_IMAGE_PIXBUF:
2799 g_object_unref (site->icon_data.pixbuf.pixbuf);
2801 case GTK_IMAGE_STOCK:
2802 g_free (site->icon_data.stock.stock_id);
2804 case GTK_IMAGE_ICON_NAME:
2805 g_free (site->icon_data.name.icon_name);
2808 g_assert_not_reached();
2811 site->icon_type = GTK_IMAGE_EMPTY;
2815 * gtk_drag_source_set_icon_pixbuf:
2816 * @widget: a #GtkWidget
2817 * @pixbuf: the #GdkPixbuf for the drag icon
2819 * Sets the icon that will be used for drags from a particular widget
2820 * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
2821 * release it when it is no longer needed.
2824 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
2827 GtkDragSourceSite *site;
2829 g_return_if_fail (GTK_IS_WIDGET (widget));
2830 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2832 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2833 g_return_if_fail (site != NULL);
2834 g_object_ref (pixbuf);
2836 gtk_drag_source_unset_icon (site);
2838 site->icon_type = GTK_IMAGE_PIXBUF;
2839 site->icon_data.pixbuf.pixbuf = pixbuf;
2843 * gtk_drag_source_set_icon_stock:
2844 * @widget: a #GtkWidget
2845 * @stock_id: the ID of the stock icon to use
2847 * Sets the icon that will be used for drags from a particular source
2851 gtk_drag_source_set_icon_stock (GtkWidget *widget,
2852 const gchar *stock_id)
2854 GtkDragSourceSite *site;
2856 g_return_if_fail (GTK_IS_WIDGET (widget));
2857 g_return_if_fail (stock_id != NULL);
2859 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2860 g_return_if_fail (site != NULL);
2862 gtk_drag_source_unset_icon (site);
2864 site->icon_type = GTK_IMAGE_STOCK;
2865 site->icon_data.stock.stock_id = g_strdup (stock_id);
2869 * gtk_drag_source_set_icon_name:
2870 * @widget: a #GtkWidget
2871 * @icon_name: name of icon to use
2873 * Sets the icon that will be used for drags from a particular source
2874 * to a themed icon. See the docs for #GtkIconTheme for more details.
2879 gtk_drag_source_set_icon_name (GtkWidget *widget,
2880 const gchar *icon_name)
2882 GtkDragSourceSite *site;
2884 g_return_if_fail (GTK_IS_WIDGET (widget));
2885 g_return_if_fail (icon_name != NULL);
2887 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2888 g_return_if_fail (site != NULL);
2890 gtk_drag_source_unset_icon (site);
2892 site->icon_type = GTK_IMAGE_ICON_NAME;
2893 site->icon_data.name.icon_name = g_strdup (icon_name);
2897 gtk_drag_get_icon (GtkDragSourceInfo *info,
2898 GtkWidget **icon_window,
2902 if (get_can_change_screen (info->icon_window))
2903 gtk_window_set_screen (GTK_WINDOW (info->icon_window),
2906 if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
2908 if (!info->fallback_icon)
2910 gint save_hot_x, save_hot_y;
2911 gboolean save_destroy_icon;
2912 GtkWidget *save_icon_window;
2914 /* HACK to get the appropriate icon
2916 save_icon_window = info->icon_window;
2917 save_hot_x = info->hot_x;
2918 save_hot_y = info->hot_x;
2919 save_destroy_icon = info->destroy_icon;
2921 info->icon_window = NULL;
2922 set_icon_stock_pixbuf (info->context,
2923 GTK_STOCK_DND, NULL, -2, -2, TRUE);
2924 info->fallback_icon = info->icon_window;
2926 info->icon_window = save_icon_window;
2927 info->hot_x = save_hot_x;
2928 info->hot_y = save_hot_y;
2929 info->destroy_icon = save_destroy_icon;
2932 gtk_widget_hide (info->icon_window);
2934 *icon_window = info->fallback_icon;
2935 gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
2942 if (info->fallback_icon)
2943 gtk_widget_hide (info->fallback_icon);
2945 *icon_window = info->icon_window;
2946 *hot_x = info->hot_x;
2947 *hot_y = info->hot_y;
2952 gtk_drag_update_icon (GtkDragSourceInfo *info)
2954 if (info->icon_window)
2956 GtkWidget *icon_window;
2959 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
2961 gtk_window_move (GTK_WINDOW (icon_window),
2962 info->cur_x - hot_x,
2963 info->cur_y - hot_y);
2965 if (gtk_widget_get_visible (icon_window))
2966 gdk_window_raise (gtk_widget_get_window (icon_window));
2968 gtk_widget_show (icon_window);
2973 gtk_drag_set_icon_window (GdkDragContext *context,
2977 gboolean destroy_on_release)
2979 GtkDragSourceInfo *info;
2981 info = gtk_drag_get_source_info (context, FALSE);
2984 if (destroy_on_release)
2985 gtk_widget_destroy (widget);
2989 gtk_drag_remove_icon (info);
2992 g_object_ref (widget);
2994 info->icon_window = widget;
2995 info->hot_x = hot_x;
2996 info->hot_y = hot_y;
2997 info->destroy_icon = destroy_on_release;
2999 if (widget && info->icon_pixbuf)
3001 g_object_unref (info->icon_pixbuf);
3002 info->icon_pixbuf = NULL;
3005 gtk_drag_update_cursor (info);
3006 gtk_drag_update_icon (info);
3010 * gtk_drag_set_icon_widget:
3011 * @context: the context for a drag. (This must be called
3012 with a context for the source side of a drag)
3013 * @widget: a toplevel window to use as an icon.
3014 * @hot_x: the X offset within @widget of the hotspot.
3015 * @hot_y: the Y offset within @widget of the hotspot.
3017 * Changes the icon for a widget to a given widget. GTK+
3018 * will not destroy the icon, so if you don't want
3019 * it to persist, you should connect to the "drag-end"
3020 * signal and destroy it yourself.
3023 gtk_drag_set_icon_widget (GdkDragContext *context,
3028 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3029 g_return_if_fail (GTK_IS_WIDGET (widget));
3031 gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
3035 icon_window_realize (GtkWidget *window,
3038 cairo_surface_t *surface;
3039 cairo_pattern_t *pattern;
3042 surface = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3043 CAIRO_CONTENT_COLOR,
3044 gdk_pixbuf_get_width (pixbuf),
3045 gdk_pixbuf_get_height (pixbuf));
3047 cr = cairo_create (surface);
3048 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3049 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3051 cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3053 cairo_pop_group_to_source (cr);
3057 pattern = cairo_pattern_create_for_surface (surface);
3058 gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3059 cairo_pattern_destroy (pattern);
3061 cairo_surface_destroy (surface);
3063 if (gdk_pixbuf_get_has_alpha (pixbuf))
3065 cairo_region_t *region;
3067 surface = cairo_image_surface_create (CAIRO_FORMAT_A1,
3068 gdk_pixbuf_get_width (pixbuf),
3069 gdk_pixbuf_get_height (pixbuf));
3071 cr = cairo_create (surface);
3072 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
3076 region = gdk_cairo_region_create_from_surface (surface);
3077 gtk_widget_shape_combine_region (window, region);
3078 cairo_region_destroy (region);
3080 cairo_surface_destroy (surface);
3085 set_icon_stock_pixbuf (GdkDragContext *context,
3086 const gchar *stock_id,
3090 gboolean force_window)
3095 GdkDisplay *display;
3097 g_return_if_fail (context != NULL);
3098 g_return_if_fail (pixbuf != NULL || stock_id != NULL);
3099 g_return_if_fail (pixbuf == NULL || stock_id == NULL);
3101 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3103 window = gtk_window_new (GTK_WINDOW_POPUP);
3104 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3105 gtk_window_set_screen (GTK_WINDOW (window), screen);
3106 set_can_change_screen (window, TRUE);
3108 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3109 gtk_widget_set_app_paintable (window, TRUE);
3113 pixbuf = gtk_widget_render_icon_pixbuf (window, stock_id,
3118 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
3119 gtk_widget_destroy (window);
3125 g_object_ref (pixbuf);
3127 display = gdk_window_get_display (gdk_drag_context_get_source_window (context));
3128 width = gdk_pixbuf_get_width (pixbuf);
3129 height = gdk_pixbuf_get_height (pixbuf);
3131 if (!force_window &&
3132 gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
3134 GtkDragSourceInfo *info;
3136 gtk_widget_destroy (window);
3138 info = gtk_drag_get_source_info (context, FALSE);
3140 if (info->icon_pixbuf)
3141 g_object_unref (info->icon_pixbuf);
3142 info->icon_pixbuf = pixbuf;
3144 gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
3148 gtk_widget_set_size_request (window, width, height);
3150 g_signal_connect_closure (window, "realize",
3151 g_cclosure_new (G_CALLBACK (icon_window_realize),
3153 (GClosureNotify)g_object_unref),
3156 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3161 * gtk_drag_set_icon_pixbuf:
3162 * @context: the context for a drag. (This must be called
3163 * with a context for the source side of a drag)
3164 * @pixbuf: the #GdkPixbuf to use as the drag icon.
3165 * @hot_x: the X offset within @widget of the hotspot.
3166 * @hot_y: the Y offset within @widget of the hotspot.
3168 * Sets @pixbuf as the icon for a given drag.
3171 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
3176 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3177 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3179 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3183 * gtk_drag_set_icon_stock:
3184 * @context: the context for a drag. (This must be called
3185 * with a context for the source side of a drag)
3186 * @stock_id: the ID of the stock icon to use for the drag.
3187 * @hot_x: the X offset within the icon of the hotspot.
3188 * @hot_y: the Y offset within the icon of the hotspot.
3190 * Sets the icon for a given drag from a stock ID.
3193 gtk_drag_set_icon_stock (GdkDragContext *context,
3194 const gchar *stock_id,
3198 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3199 g_return_if_fail (stock_id != NULL);
3201 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
3204 /* XXX: This function is in gdk, too. Should it be in Cairo? */
3206 _gtk_cairo_surface_extents (cairo_surface_t *surface,
3207 GdkRectangle *extents)
3209 double x1, x2, y1, y2;
3212 g_return_val_if_fail (surface != NULL, FALSE);
3213 g_return_val_if_fail (extents != NULL, FALSE);
3215 cr = cairo_create (surface);
3216 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
3225 if (x1 < G_MININT || x1 > G_MAXINT ||
3226 y1 < G_MININT || y1 > G_MAXINT ||
3227 x2 > G_MAXINT || y2 > G_MAXINT)
3229 extents->x = extents->y = extents->width = extents->height = 0;
3235 extents->width = x2;
3236 extents->height = y2;
3242 * gtk_drag_set_icon_surface:
3243 * @context: the context for a drag. (This must be called
3244 * with a context for the source side of a drag)
3245 * @surface: the surface to use as icon
3247 * Sets @surface as the icon for a given drag. GTK+ retains
3248 * references for the arguments, and will release them when
3249 * they are no longer needed.
3251 * To position the surface relative to the mouse, use
3252 * cairo_surface_set_device_offset() on @surface. The mouse
3253 * cursor will be positioned at the (0,0) coordinate of the
3257 gtk_drag_set_icon_surface (GdkDragContext *context,
3258 cairo_surface_t *surface)
3262 GdkRectangle extents;
3263 cairo_pattern_t *pattern;
3265 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3266 g_return_if_fail (surface != NULL);
3268 _gtk_cairo_surface_extents (surface, &extents);
3271 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3273 window = gtk_window_new (GTK_WINDOW_POPUP);
3274 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3275 gtk_window_set_screen (GTK_WINDOW (window), screen);
3276 set_can_change_screen (window, TRUE);
3278 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3279 gtk_widget_set_app_paintable (window, TRUE);
3281 gtk_widget_set_size_request (window, extents.width, extents.height);
3282 gtk_widget_realize (window);
3284 if (cairo_surface_get_content (surface) != CAIRO_CONTENT_COLOR)
3286 cairo_surface_t *saturated;
3287 cairo_region_t *region;
3290 region = gdk_cairo_region_create_from_surface (surface);
3291 cairo_region_translate (region, -extents.x, -extents.y);
3293 gtk_widget_shape_combine_region (window, region);
3294 cairo_region_destroy (region);
3296 /* Need to saturate the colors, so it doesn't look like semi-transparent
3297 * pixels were painted on black. */
3298 saturated = gdk_window_create_similar_surface (gtk_widget_get_window (window),
3299 CAIRO_CONTENT_COLOR,
3303 cr = cairo_create (saturated);
3304 cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR_ALPHA);
3305 cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
3307 cairo_set_operator (cr, CAIRO_OPERATOR_SATURATE);
3309 cairo_pop_group_to_source (cr);
3313 pattern = cairo_pattern_create_for_surface (saturated);
3315 cairo_surface_destroy (saturated);
3319 cairo_matrix_t matrix;
3321 pattern = cairo_pattern_create_for_surface (surface);
3322 cairo_matrix_init_translate (&matrix, extents.x, extents.y);
3323 cairo_pattern_set_matrix (pattern, &matrix);
3326 gdk_window_set_background_pattern (gtk_widget_get_window (window), pattern);
3328 gtk_drag_set_icon_window (context, window, extents.x, extents.y, TRUE);
3332 * gtk_drag_set_icon_name:
3333 * @context: the context for a drag. (This must be called
3334 * with a context for the source side of a drag)
3335 * @icon_name: name of icon to use
3336 * @hot_x: the X offset of the hotspot within the icon
3337 * @hot_y: the Y offset of the hotspot within the icon
3339 * Sets the icon for a given drag from a named themed icon. See
3340 * the docs for #GtkIconTheme for more details. Note that the
3341 * size of the icon depends on the icon theme (the icon is
3342 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
3343 * @hot_x and @hot_y have to be used with care.
3348 gtk_drag_set_icon_name (GdkDragContext *context,
3349 const gchar *icon_name,
3354 GtkSettings *settings;
3355 GtkIconTheme *icon_theme;
3357 gint width, height, icon_size;
3359 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3360 g_return_if_fail (icon_name != NULL);
3362 screen = gdk_window_get_screen (gdk_drag_context_get_source_window (context));
3363 g_return_if_fail (screen != NULL);
3365 settings = gtk_settings_get_for_screen (screen);
3366 if (gtk_icon_size_lookup_for_settings (settings,
3369 icon_size = MAX (width, height);
3371 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
3373 icon_theme = gtk_icon_theme_get_for_screen (screen);
3375 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
3376 icon_size, 0, NULL);
3378 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3380 g_warning ("Cannot load drag icon from icon name %s", icon_name);
3384 * gtk_drag_set_icon_default:
3385 * @context: the context for a drag. (This must be called
3386 with a context for the source side of a drag)
3388 * Sets the icon for a particular drag to the default
3392 gtk_drag_set_icon_default (GdkDragContext *context)
3394 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3396 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
3399 /*************************************************************
3400 * _gtk_drag_source_handle_event:
3401 * Called from widget event handling code on Drag events
3405 * toplevel: Toplevel widget that received the event
3408 *************************************************************/
3411 _gtk_drag_source_handle_event (GtkWidget *widget,
3414 GtkDragSourceInfo *info;
3415 GdkDragContext *context;
3417 g_return_if_fail (widget != NULL);
3418 g_return_if_fail (event != NULL);
3420 context = event->dnd.context;
3421 info = gtk_drag_get_source_info (context, FALSE);
3425 switch (event->type)
3427 case GDK_DRAG_STATUS:
3431 if (info->proxy_dest)
3433 if (!event->dnd.send_event)
3435 if (info->proxy_dest->proxy_drop_wait)
3437 gboolean result = gdk_drag_context_get_selected_action (context) != 0;
3439 /* Aha - we can finally pass the MOTIF DROP on... */
3440 gdk_drop_reply (info->proxy_dest->context, result, info->proxy_dest->proxy_drop_time);
3442 gdk_drag_drop (info->context, info->proxy_dest->proxy_drop_time);
3444 gtk_drag_finish (info->proxy_dest->context, FALSE, FALSE, info->proxy_dest->proxy_drop_time);
3448 gdk_drag_status (info->proxy_dest->context,
3449 gdk_drag_context_get_selected_action (event->dnd.context),
3454 else if (info->have_grab)
3456 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
3457 gdk_drag_context_get_selected_action (event->dnd.context),
3459 if (info->cursor != cursor)
3463 pointer = gdk_drag_context_get_device (context);
3464 gdk_device_grab (pointer, gtk_widget_get_window (widget),
3465 GDK_OWNERSHIP_APPLICATION, FALSE,
3466 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
3467 cursor, info->grab_time);
3468 info->cursor = cursor;
3471 gtk_drag_add_update_idle (info);
3476 case GDK_DROP_FINISHED:
3477 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, event->dnd.time);
3480 g_assert_not_reached ();
3484 /*************************************************************
3485 * gtk_drag_source_check_selection:
3486 * Check if we've set up handlers/claimed the selection
3487 * for a given drag. If not, add them.
3491 *************************************************************/
3494 gtk_drag_source_check_selection (GtkDragSourceInfo *info,
3500 tmp_list = info->selections;
3503 if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3505 tmp_list = tmp_list->next;
3508 gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3512 info->selections = g_list_prepend (info->selections,
3513 GUINT_TO_POINTER (selection));
3515 tmp_list = info->target_list->list;
3518 GtkTargetPair *pair = tmp_list->data;
3520 gtk_selection_add_target (info->ipc_widget,
3524 tmp_list = tmp_list->next;
3527 if (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_MOTIF)
3529 gtk_selection_add_target (info->ipc_widget,
3531 gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS"),
3532 TARGET_MOTIF_SUCCESS);
3533 gtk_selection_add_target (info->ipc_widget,
3535 gdk_atom_intern_static_string ("XmTRANSFER_FAILURE"),
3536 TARGET_MOTIF_FAILURE);
3539 gtk_selection_add_target (info->ipc_widget,
3541 gdk_atom_intern_static_string ("DELETE"),
3545 /*************************************************************
3546 * gtk_drag_drop_finished:
3547 * Clean up from the drag, and display snapback, if necessary.
3553 *************************************************************/
3556 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3557 GtkDragResult result,
3562 success = (result == GTK_DRAG_RESULT_SUCCESS);
3563 gtk_drag_source_release_selections (info, time);
3565 if (info->proxy_dest)
3567 /* The time from the event isn't reliable for Xdnd drags */
3568 gtk_drag_finish (info->proxy_dest->context, success, FALSE,
3569 info->proxy_dest->proxy_drop_time);
3570 gtk_drag_source_info_destroy (info);
3575 g_signal_emit_by_name (info->widget, "drag-failed",
3576 info->context, result, &success);
3580 gtk_drag_source_info_destroy (info);
3584 GtkDragAnim *anim = g_slice_new0 (GtkDragAnim);
3588 anim->n_steps = MAX (info->cur_x - info->start_x,
3589 info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3590 anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3592 info->cur_screen = gtk_widget_get_screen (info->widget);
3594 if (!info->icon_window)
3595 set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf,
3598 gtk_drag_update_icon (info);
3600 /* Mark the context as dead, so if the destination decides
3601 * to respond really late, we still are OK.
3603 gtk_drag_clear_source_info (info->context);
3604 gdk_threads_add_timeout (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3610 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3613 GdkDisplay *display = gtk_widget_get_display (info->widget);
3614 GList *tmp_list = info->selections;
3618 GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3619 if (gdk_selection_owner_get_for_display (display, selection) == gtk_widget_get_window (info->ipc_widget))
3620 gtk_selection_owner_set_for_display (display, NULL, selection, time);
3622 tmp_list = tmp_list->next;
3625 g_list_free (info->selections);
3626 info->selections = NULL;
3629 /*************************************************************
3631 * Send a drop event.
3635 *************************************************************/
3638 gtk_drag_drop (GtkDragSourceInfo *info,
3641 if (gdk_drag_context_get_protocol (info->context) == GDK_DRAG_PROTO_ROOTWIN)
3643 GtkSelectionData selection_data;
3645 /* GTK+ traditionally has used application/x-rootwin-drop, but the
3646 * XDND spec specifies x-rootwindow-drop.
3648 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3649 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3651 tmp_list = info->target_list->list;
3654 GtkTargetPair *pair = tmp_list->data;
3656 if (pair->target == target1 || pair->target == target2)
3658 selection_data.selection = GDK_NONE;
3659 selection_data.target = pair->target;
3660 selection_data.data = NULL;
3661 selection_data.length = -1;
3663 g_signal_emit_by_name (info->widget, "drag-data-get",
3664 info->context, &selection_data,
3668 /* FIXME: Should we check for length >= 0 here? */
3669 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3672 tmp_list = tmp_list->next;
3674 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3678 if (info->icon_window)
3679 gtk_widget_hide (info->icon_window);
3681 gdk_drag_drop (info->context, time);
3682 info->drop_timeout = gdk_threads_add_timeout (DROP_ABORT_TIME,
3683 gtk_drag_abort_timeout,
3689 * Source side callbacks.
3693 gtk_drag_source_event_cb (GtkWidget *widget,
3697 GtkDragSourceSite *site;
3698 gboolean retval = FALSE;
3699 site = (GtkDragSourceSite *)data;
3701 switch (event->type)
3703 case GDK_BUTTON_PRESS:
3704 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3706 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3707 site->x = event->button.x;
3708 site->y = event->button.y;
3712 case GDK_BUTTON_RELEASE:
3713 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3714 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3717 case GDK_MOTION_NOTIFY:
3718 if (site->state & event->motion.state & site->start_button_mask)
3720 /* FIXME: This is really broken and can leave us
3726 if (site->state & event->motion.state &
3727 GDK_BUTTON1_MASK << (i - 1))
3731 if (gtk_drag_check_threshold (widget, site->x, site->y,
3732 event->motion.x, event->motion.y))
3735 gtk_drag_begin_internal (widget, site, site->target_list,
3744 default: /* hit for 2/3BUTTON_PRESS */
3752 gtk_drag_source_site_destroy (gpointer data)
3754 GtkDragSourceSite *site = data;
3756 if (site->target_list)
3757 gtk_target_list_unref (site->target_list);
3759 gtk_drag_source_unset_icon (site);
3760 g_slice_free (GtkDragSourceSite, site);
3764 gtk_drag_selection_get (GtkWidget *widget,
3765 GtkSelectionData *selection_data,
3770 GtkDragSourceInfo *info = data;
3771 static GdkAtom null_atom = GDK_NONE;
3775 null_atom = gdk_atom_intern_static_string ("NULL");
3780 g_signal_emit_by_name (info->widget,
3783 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3785 case TARGET_MOTIF_SUCCESS:
3786 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3787 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3789 case TARGET_MOTIF_FAILURE:
3790 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3791 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3794 if (info->proxy_dest)
3796 /* This is sort of dangerous and needs to be thought
3799 info->proxy_dest->proxy_data = selection_data;
3800 gtk_drag_get_data (info->widget,
3801 info->proxy_dest->context,
3802 gtk_selection_data_get_target (selection_data),
3805 info->proxy_dest->proxy_data = NULL;
3809 if (gtk_target_list_find (info->target_list,
3810 gtk_selection_data_get_target (selection_data),
3813 g_signal_emit_by_name (info->widget, "drag-data-get",
3825 gtk_drag_anim_timeout (gpointer data)
3828 GtkDragSourceInfo *info;
3835 if (anim->step == anim->n_steps)
3837 gtk_drag_source_info_destroy (anim->info);
3838 g_slice_free (GtkDragAnim, anim);
3844 x = (info->start_x * (anim->step + 1) +
3845 info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3846 y = (info->start_y * (anim->step + 1) +
3847 info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3848 if (info->icon_window)
3850 GtkWidget *icon_window;
3853 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
3854 gtk_window_move (GTK_WINDOW (icon_window),
3868 gtk_drag_remove_icon (GtkDragSourceInfo *info)
3870 if (info->icon_window)
3872 gtk_widget_hide (info->icon_window);
3873 if (info->destroy_icon)
3874 gtk_widget_destroy (info->icon_window);
3876 if (info->fallback_icon)
3878 gtk_widget_destroy (info->fallback_icon);
3879 info->fallback_icon = NULL;
3882 g_object_unref (info->icon_window);
3883 info->icon_window = NULL;
3888 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
3892 for (i = 0; i < n_drag_cursors; i++)
3894 if (info->drag_cursors[i] != NULL)
3896 gdk_cursor_unref (info->drag_cursors[i]);
3897 info->drag_cursors[i] = NULL;
3901 gtk_drag_remove_icon (info);
3903 if (info->icon_pixbuf)
3905 g_object_unref (info->icon_pixbuf);
3906 info->icon_pixbuf = NULL;
3909 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3910 gtk_drag_grab_broken_event_cb,
3912 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3913 gtk_drag_grab_notify_cb,
3915 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3916 gtk_drag_button_release_cb,
3918 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3921 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3924 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3925 gtk_drag_selection_get,
3928 if (!info->proxy_dest)
3929 g_signal_emit_by_name (info->widget, "drag-end", info->context);
3932 g_object_unref (info->widget);
3934 gtk_selection_remove_all (info->ipc_widget);
3935 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), NULL);
3936 source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
3937 gtk_drag_release_ipc_widget (info->ipc_widget);
3939 gtk_target_list_unref (info->target_list);
3941 gtk_drag_clear_source_info (info->context);
3942 g_object_unref (info->context);
3944 if (info->drop_timeout)
3945 g_source_remove (info->drop_timeout);
3947 if (info->update_idle)
3948 g_source_remove (info->update_idle);
3954 gtk_drag_update_idle (gpointer data)
3956 GtkDragSourceInfo *info = data;
3957 GdkWindow *dest_window;
3958 GdkDragProtocol protocol;
3961 GdkDragAction action;
3962 GdkDragAction possible_actions;
3965 info->update_idle = 0;
3967 if (info->last_event)
3969 time = gtk_drag_get_event_time (info->last_event);
3970 gtk_drag_get_event_actions (info->last_event,
3972 info->possible_actions,
3973 &action, &possible_actions);
3974 gtk_drag_update_icon (info);
3975 gdk_drag_find_window_for_screen (info->context,
3976 info->icon_window ? gtk_widget_get_window (info->icon_window) : NULL,
3977 info->cur_screen, info->cur_x, info->cur_y,
3978 &dest_window, &protocol);
3980 if (!gdk_drag_motion (info->context, dest_window, protocol,
3981 info->cur_x, info->cur_y, action,
3985 gdk_event_free ((GdkEvent *)info->last_event);
3986 info->last_event = NULL;
3990 g_object_unref (dest_window);
3992 selection = gdk_drag_get_selection (info->context);
3994 gtk_drag_source_check_selection (info, selection, time);
4002 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
4004 /* We use an idle lower than GDK_PRIORITY_REDRAW so that exposes
4005 * from the last move can catch up before we move again.
4007 if (!info->update_idle)
4008 info->update_idle = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW + 5,
4009 gtk_drag_update_idle,
4016 * @info: DragSourceInfo for the drag
4017 * @screen: new screen
4018 * @x_root: new X position
4019 * @y_root: new y position
4020 * @event: event received requiring update
4022 * Updates the status of the drag; called when the
4023 * cursor moves or the modifier changes
4026 gtk_drag_update (GtkDragSourceInfo *info,
4032 info->cur_screen = screen;
4033 info->cur_x = x_root;
4034 info->cur_y = y_root;
4035 if (info->last_event)
4037 gdk_event_free ((GdkEvent *)info->last_event);
4038 info->last_event = NULL;
4041 info->last_event = gdk_event_copy ((GdkEvent *)event);
4043 gtk_drag_add_update_idle (info);
4046 /*************************************************************
4048 * Called when the user finishes to drag, either by
4049 * releasing the mouse, or by pressing Esc.
4051 * info: Source info for the drag
4052 * time: Timestamp for ending the drag
4054 *************************************************************/
4057 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
4059 GdkEvent *send_event;
4060 GtkWidget *source_widget = info->widget;
4061 GdkDevice *pointer, *keyboard;
4063 pointer = gdk_drag_context_get_device (info->context);
4064 keyboard = gdk_device_get_associated_device (pointer);
4066 /* Prevent ungrab before grab (see bug 623865) */
4067 if (info->grab_time == GDK_CURRENT_TIME)
4068 time = GDK_CURRENT_TIME;
4070 if (info->update_idle)
4072 g_source_remove (info->update_idle);
4073 info->update_idle = 0;
4076 if (info->last_event)
4078 gdk_event_free (info->last_event);
4079 info->last_event = NULL;
4082 info->have_grab = FALSE;
4084 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4085 gtk_drag_grab_broken_event_cb,
4087 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4088 gtk_drag_grab_notify_cb,
4090 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4091 gtk_drag_button_release_cb,
4093 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4096 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4100 gdk_device_ungrab (pointer, time);
4101 ungrab_dnd_keys (info->ipc_widget, keyboard, time);
4102 gtk_device_grab_remove (info->ipc_widget, pointer);
4104 /* Send on a release pair to the original
4105 * widget to convince it to release its grab. We need to
4106 * call gtk_propagate_event() here, instead of
4107 * gtk_widget_event() because widget like GtkList may
4108 * expect propagation.
4111 send_event = gdk_event_new (GDK_BUTTON_RELEASE);
4112 send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
4113 send_event->button.send_event = TRUE;
4114 send_event->button.time = time;
4115 send_event->button.x = 0;
4116 send_event->button.y = 0;
4117 send_event->button.axes = NULL;
4118 send_event->button.state = 0;
4119 send_event->button.button = info->button;
4120 send_event->button.device = pointer;
4121 send_event->button.x_root = 0;
4122 send_event->button.y_root = 0;
4124 gtk_propagate_event (source_widget, send_event);
4125 gdk_event_free (send_event);
4128 /*************************************************************
4130 * Called on cancellation of a drag, either by the user
4131 * or programmatically.
4133 * info: Source info for the drag
4134 * time: Timestamp for ending the drag
4136 *************************************************************/
4139 gtk_drag_cancel (GtkDragSourceInfo *info, GtkDragResult result, guint32 time)
4141 gtk_drag_end (info, time);
4142 gdk_drag_abort (info->context, time);
4143 gtk_drag_drop_finished (info, result, time);
4146 /*************************************************************
4147 * gtk_drag_motion_cb:
4148 * "motion-notify-event" callback during drag.
4152 *************************************************************/
4155 gtk_drag_motion_cb (GtkWidget *widget,
4156 GdkEventMotion *event,
4159 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4161 gint x_root, y_root;
4165 GdkDisplay *display = gtk_widget_get_display (widget);
4167 gdk_display_get_device_state (display, event->device,
4168 &screen, &x_root, &y_root, NULL);
4169 event->x_root = x_root;
4170 event->y_root = y_root;
4173 screen = gdk_event_get_screen ((GdkEvent *)event);
4175 gtk_drag_update (info, screen, event->x_root, event->y_root, (GdkEvent *) event);
4180 /*************************************************************
4182 * "key-press/release-event" callback during drag.
4186 *************************************************************/
4189 #define SMALL_STEP 1
4192 gtk_drag_key_cb (GtkWidget *widget,
4196 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4197 GdkModifierType state;
4198 GdkWindow *root_window;
4203 state = event->state & gtk_accelerator_get_default_mod_mask ();
4204 pointer = gdk_device_get_associated_device (gdk_event_get_device ((GdkEvent *) event));
4206 if (event->type == GDK_KEY_PRESS)
4208 switch (event->keyval)
4210 case GDK_KEY_Escape:
4211 gtk_drag_cancel (info, GTK_DRAG_RESULT_USER_CANCELLED, event->time);
4215 case GDK_KEY_Return:
4216 case GDK_KEY_ISO_Enter:
4217 case GDK_KEY_KP_Enter:
4218 case GDK_KEY_KP_Space:
4219 gtk_drag_end (info, event->time);
4220 gtk_drag_drop (info, event->time);
4225 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4229 case GDK_KEY_KP_Down:
4230 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4234 case GDK_KEY_KP_Left:
4235 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4239 case GDK_KEY_KP_Right:
4240 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4246 /* Now send a "motion" so that the modifier state is updated */
4248 /* The state is not yet updated in the event, so we need
4249 * to query it here. We could use XGetModifierMapping, but
4250 * that would be overkill.
4252 root_window = gtk_widget_get_root_window (widget);
4253 gdk_window_get_device_position (root_window, pointer, NULL, NULL, &state);
4254 event->state = state;
4256 if (dx != 0 || dy != 0)
4260 gdk_display_warp_device (gtk_widget_get_display (widget), pointer,
4261 gtk_widget_get_screen (widget),
4262 info->cur_x, info->cur_y);
4265 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
4271 gtk_drag_grab_broken_event_cb (GtkWidget *widget,
4272 GdkEventGrabBroken *event,
4275 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4277 /* Don't cancel if we break the implicit grab from the initial button_press.
4278 * Also, don't cancel if we re-grab on the widget or on our IPC window, for
4279 * example, when changing the drag cursor.
4282 || event->grab_window == gtk_widget_get_window (info->widget)
4283 || event->grab_window == gtk_widget_get_window (info->ipc_widget))
4286 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4291 gtk_drag_grab_notify_cb (GtkWidget *widget,
4292 gboolean was_grabbed,
4295 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4298 pointer = gdk_drag_context_get_device (info->context);
4300 if (gtk_widget_device_is_shadowed (widget, pointer))
4302 /* We have to block callbacks to avoid recursion here, because
4303 gtk_drag_cancel calls gtk_grab_remove (via gtk_drag_end) */
4304 g_signal_handlers_block_by_func (widget, gtk_drag_grab_notify_cb, data);
4305 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4306 g_signal_handlers_unblock_by_func (widget, gtk_drag_grab_notify_cb, data);
4311 /*************************************************************
4312 * gtk_drag_button_release_cb:
4313 * "button-release-event" callback during drag.
4317 *************************************************************/
4320 gtk_drag_button_release_cb (GtkWidget *widget,
4321 GdkEventButton *event,
4324 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4326 if (event->button != info->button)
4329 if ((gdk_drag_context_get_selected_action (info->context) != 0) &&
4330 (gdk_drag_context_get_dest_window (info->context) != NULL))
4332 gtk_drag_end (info, event->time);
4333 gtk_drag_drop (info, event->time);
4337 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4344 gtk_drag_abort_timeout (gpointer data)
4346 GtkDragSourceInfo *info = data;
4347 guint32 time = GDK_CURRENT_TIME;
4349 if (info->proxy_dest)
4350 time = info->proxy_dest->proxy_drop_time;
4352 info->drop_timeout = 0;
4353 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_TIMEOUT_EXPIRED, time);
4359 * gtk_drag_check_threshold:
4360 * @widget: a #GtkWidget
4361 * @start_x: X coordinate of start of drag
4362 * @start_y: Y coordinate of start of drag
4363 * @current_x: current X coordinate
4364 * @current_y: current Y coordinate
4366 * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
4367 * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
4368 * should trigger the beginning of a drag-and-drop operation.
4370 * Return Value: %TRUE if the drag threshold has been passed.
4373 gtk_drag_check_threshold (GtkWidget *widget,
4379 gint drag_threshold;
4381 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4383 g_object_get (gtk_widget_get_settings (widget),
4384 "gtk-dnd-drag-threshold", &drag_threshold,
4387 return (ABS (current_x - start_x) > drag_threshold ||
4388 ABS (current_y - start_y) > drag_threshold);