1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gdkconfig.h"
34 #include "gdk/gdkkeysyms.h"
36 #ifdef GDK_WINDOWING_X11
38 #include <X11/keysym.h>
39 #include "gdk/x11/gdkx.h"
43 #include "gtkiconfactory.h"
44 #include "gtkicontheme.h"
46 #include "gtkinvisible.h"
50 #include "gtkwindow.h"
52 #include "gtkdndcursors.h"
55 static GSList *source_widgets = NULL;
57 typedef struct _GtkDragSourceSite GtkDragSourceSite;
58 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
59 typedef struct _GtkDragDestSite GtkDragDestSite;
60 typedef struct _GtkDragDestInfo GtkDragDestInfo;
61 typedef struct _GtkDragAnim GtkDragAnim;
62 typedef struct _GtkDragFindData GtkDragFindData;
72 struct _GtkDragSourceSite
74 GdkModifierType start_button_mask;
75 GtkTargetList *target_list; /* Targets for drag data */
76 GdkDragAction actions; /* Possible actions */
79 GtkImageType icon_type;
82 GtkImagePixmapData pixmap;
83 GtkImagePixbufData pixbuf;
84 GtkImageStockData stock;
85 GtkImageIconNameData name;
89 GdkColormap *colormap; /* Colormap for drag icon */
91 /* Stored button press information to detect drag beginning */
96 struct _GtkDragSourceInfo
99 GtkTargetList *target_list; /* Targets for drag data */
100 GdkDragAction possible_actions; /* Actions allowed by source */
101 GdkDragContext *context; /* drag context */
102 GtkWidget *icon_window; /* Window for drag */
103 GtkWidget *fallback_icon; /* Window for drag used on other screens */
104 GtkWidget *ipc_widget; /* GtkInvisible for grab, message passing */
105 GdkCursor *cursor; /* Cursor for drag */
106 gint hot_x, hot_y; /* Hot spot for drag */
107 gint button; /* mouse button starting drag */
109 GtkDragStatus status; /* drag status */
110 GdkEvent *last_event; /* pending event */
112 gint start_x, start_y; /* Initial position */
113 gint cur_x, cur_y; /* Current Position */
114 GdkScreen *cur_screen; /* Current screen for pointer */
116 guint32 grab_time; /* timestamp for initial grab */
117 GList *selections; /* selections we've claimed */
119 GtkDragDestInfo *proxy_dest; /* Set if this is a proxy drag */
121 guint update_idle; /* Idle function to update the drag */
122 guint drop_timeout; /* Timeout for aborting drop */
123 guint destroy_icon : 1; /* If true, destroy icon_window
125 guint have_grab : 1; /* Do we still have the pointer grab
127 GdkPixbuf *icon_pixbuf;
128 GdkCursor *drag_cursors[6];
131 struct _GtkDragDestSite
133 GtkDestDefaults flags;
134 GtkTargetList *target_list;
135 GdkDragAction actions;
136 GdkWindow *proxy_window;
137 GdkDragProtocol proxy_protocol;
139 guint proxy_coords : 1;
141 guint track_motion : 1;
144 struct _GtkDragDestInfo
146 GtkWidget *widget; /* Widget in which drag is in */
147 GdkDragContext *context; /* Drag context */
148 GtkDragSourceInfo *proxy_source; /* Set if this is a proxy drag */
149 GtkSelectionData *proxy_data; /* Set while retrieving proxied data */
150 guint dropped : 1; /* Set after we receive a drop */
151 guint32 proxy_drop_time; /* Timestamp for proxied drop */
152 guint proxy_drop_wait : 1; /* Set if we are waiting for a
153 * status reply before sending
156 gint drop_x, drop_y; /* Position of drop */
159 #define DROP_ABORT_TIME 300000
161 #define ANIM_STEP_TIME 50
162 #define ANIM_STEP_LENGTH 50
163 #define ANIM_MIN_STEPS 5
164 #define ANIM_MAX_STEPS 10
168 GtkDragSourceInfo *info;
173 struct _GtkDragFindData
177 GdkDragContext *context;
178 GtkDragDestInfo *info;
181 gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
182 gint x, gint y, guint32 time);
186 /* Enumeration for some targets we handle internally */
189 TARGET_MOTIF_SUCCESS = 0x40000000,
190 TARGET_MOTIF_FAILURE,
196 static GdkPixmap *default_icon_pixmap = NULL;
197 static GdkPixmap *default_icon_mask = NULL;
198 static GdkColormap *default_icon_colormap = NULL;
199 static gint default_icon_hot_x;
200 static gint default_icon_hot_y;
202 /* Forward declarations */
203 static void gtk_drag_get_event_actions (GdkEvent *event,
205 GdkDragAction actions,
206 GdkDragAction *suggested_action,
207 GdkDragAction *possible_actions);
208 static GdkCursor * gtk_drag_get_cursor (GdkDisplay *display,
209 GdkDragAction action,
210 GtkDragSourceInfo *info);
211 static void gtk_drag_update_cursor (GtkDragSourceInfo *info);
212 static GtkWidget *gtk_drag_get_ipc_widget (GtkWidget *widget);
213 static GtkWidget *gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen);
214 static void gtk_drag_release_ipc_widget (GtkWidget *widget);
216 static gboolean gtk_drag_highlight_expose (GtkWidget *widget,
217 GdkEventExpose *event,
220 static void gtk_drag_selection_received (GtkWidget *widget,
221 GtkSelectionData *selection_data,
224 static void gtk_drag_find_widget (GtkWidget *widget,
225 GtkDragFindData *data);
226 static void gtk_drag_proxy_begin (GtkWidget *widget,
227 GtkDragDestInfo *dest_info,
229 static void gtk_drag_dest_realized (GtkWidget *widget);
230 static void gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
231 GtkWidget *previous_toplevel);
232 static void gtk_drag_dest_site_destroy (gpointer data);
233 static void gtk_drag_dest_leave (GtkWidget *widget,
234 GdkDragContext *context,
236 static gboolean gtk_drag_dest_motion (GtkWidget *widget,
237 GdkDragContext *context,
241 static gboolean gtk_drag_dest_drop (GtkWidget *widget,
242 GdkDragContext *context,
247 static GtkDragDestInfo * gtk_drag_get_dest_info (GdkDragContext *context,
249 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
251 static void gtk_drag_clear_source_info (GdkDragContext *context);
253 static void gtk_drag_source_check_selection (GtkDragSourceInfo *info,
256 static void gtk_drag_source_release_selections (GtkDragSourceInfo *info,
258 static void gtk_drag_drop (GtkDragSourceInfo *info,
260 static void gtk_drag_drop_finished (GtkDragSourceInfo *info,
261 GtkDragResult result,
263 static void gtk_drag_cancel (GtkDragSourceInfo *info,
264 GtkDragResult result,
267 static gboolean gtk_drag_source_event_cb (GtkWidget *widget,
270 static void gtk_drag_source_site_destroy (gpointer data);
271 static void gtk_drag_selection_get (GtkWidget *widget,
272 GtkSelectionData *selection_data,
276 static gboolean gtk_drag_anim_timeout (gpointer data);
277 static void gtk_drag_remove_icon (GtkDragSourceInfo *info);
278 static void gtk_drag_source_info_destroy (GtkDragSourceInfo *info);
279 static void gtk_drag_add_update_idle (GtkDragSourceInfo *info);
281 static void gtk_drag_update (GtkDragSourceInfo *info,
286 static gboolean gtk_drag_motion_cb (GtkWidget *widget,
287 GdkEventMotion *event,
289 static gboolean gtk_drag_key_cb (GtkWidget *widget,
292 static gboolean gtk_drag_grab_broken_event_cb (GtkWidget *widget,
293 GdkEventGrabBroken *event,
295 static void gtk_drag_grab_notify_cb (GtkWidget *widget,
296 gboolean was_grabbed,
298 static gboolean gtk_drag_button_release_cb (GtkWidget *widget,
299 GdkEventButton *event,
301 static gboolean gtk_drag_abort_timeout (gpointer data);
303 static void set_icon_stock_pixbuf (GdkDragContext *context,
304 const gchar *stock_id,
308 gboolean force_window);
310 /************************
311 * Cursor and Icon data *
312 ************************/
315 GdkDragAction action;
321 { GDK_ACTION_DEFAULT, NULL },
322 { GDK_ACTION_ASK, "dnd-ask", dnd_cursor_ask, NULL, NULL },
323 { GDK_ACTION_COPY, "dnd-copy", dnd_cursor_copy, NULL, NULL },
324 { GDK_ACTION_MOVE, "dnd-move", dnd_cursor_move, NULL, NULL },
325 { GDK_ACTION_LINK, "dnd-link", dnd_cursor_link, NULL, NULL },
326 { 0 , "dnd-none", dnd_cursor_none, NULL, NULL },
329 static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
331 /*********************
332 * Utility functions *
333 *********************/
336 set_can_change_screen (GtkWidget *widget,
337 gboolean can_change_screen)
339 can_change_screen = can_change_screen != FALSE;
341 g_object_set_data (G_OBJECT (widget), I_("gtk-dnd-can-change-screen"),
342 GUINT_TO_POINTER (can_change_screen));
346 get_can_change_screen (GtkWidget *widget)
348 return g_object_get_data (G_OBJECT (widget), "gtk-dnd-can-change-screen") != NULL;
353 gtk_drag_get_ipc_widget_for_screen (GdkScreen *screen)
356 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
357 "gtk-dnd-ipc-widgets");
361 GSList *tmp = drag_widgets;
362 result = drag_widgets->data;
363 drag_widgets = drag_widgets->next;
364 g_object_set_data (G_OBJECT (screen),
365 I_("gtk-dnd-ipc-widgets"),
367 g_slist_free_1 (tmp);
371 result = gtk_window_new (GTK_WINDOW_POPUP);
372 gtk_window_set_screen (GTK_WINDOW (result), screen);
373 gtk_window_resize (GTK_WINDOW (result), 1, 1);
374 gtk_window_move (GTK_WINDOW (result), -100, -100);
375 gtk_widget_show (result);
382 gtk_drag_get_ipc_widget (GtkWidget *widget)
387 result = gtk_drag_get_ipc_widget_for_screen (gtk_widget_get_screen (widget));
389 toplevel = gtk_widget_get_toplevel (widget);
391 if (GTK_IS_WINDOW (toplevel))
393 if (GTK_WINDOW (toplevel)->group)
394 gtk_window_group_add_window (GTK_WINDOW (toplevel)->group,
395 GTK_WINDOW (result));
402 #ifdef GDK_WINDOWING_X11
405 * We want to handle a handful of keys during DND, e.g. Escape to abort.
406 * Grabbing the keyboard has the unfortunate side-effect of preventing
407 * useful things such as using Alt-Tab to cycle between windows or
408 * switching workspaces. Therefore, we just grab the few keys we are
409 * interested in. Note that we need to put the grabs on the root window
410 * in order for them to still work when the focus is moved to another
413 * GDK needs a little help to successfully deliver root key events...
416 static GdkFilterReturn
417 root_key_filter (GdkXEvent *xevent,
421 XEvent *ev = (XEvent *)xevent;
423 if ((ev->type == KeyPress || ev->type == KeyRelease) &&
424 ev->xkey.root == ev->xkey.window)
425 ev->xkey.window = (Window)data;
427 return GDK_FILTER_CONTINUE;
435 static GrabKey grab_keys[] = {
444 { XK_Down, Mod1Mask },
446 { XK_Left, Mod1Mask },
448 { XK_Right, Mod1Mask },
450 { XK_KP_Up, Mod1Mask },
452 { XK_KP_Down, Mod1Mask },
454 { XK_KP_Left, Mod1Mask },
456 { XK_KP_Right, Mod1Mask }
460 grab_dnd_keys (GtkWidget *widget,
464 GdkWindow *window, *root;
467 window = widget->window;
468 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
470 gdk_error_trap_push ();
472 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
474 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
475 XGrabKey (GDK_WINDOW_XDISPLAY (window),
476 keycode, grab_keys[i].modifiers,
477 GDK_WINDOW_XID (root),
484 gdk_error_trap_pop ();
486 gdk_window_add_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
490 ungrab_dnd_keys (GtkWidget *widget,
494 GdkWindow *window, *root;
497 window = widget->window;
498 root = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
500 gdk_window_remove_filter (NULL, root_key_filter, (gpointer) GDK_WINDOW_XID (window));
502 gdk_error_trap_push ();
504 for (i = 0; i < G_N_ELEMENTS (grab_keys); ++i)
506 keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (window), grab_keys[i].keysym);
507 XUngrabKey (GDK_WINDOW_XDISPLAY (window),
508 keycode, grab_keys[i].modifiers,
509 GDK_WINDOW_XID (root));
513 gdk_error_trap_pop ();
519 grab_dnd_keys (GtkWidget *widget,
522 gdk_keyboard_grab (widget->window, FALSE, time);
526 ungrab_dnd_keys (GtkWidget *widget,
529 gdk_display_keyboard_ungrab (gtk_widget_get_display (widget), time);
535 /***************************************************************
536 * gtk_drag_release_ipc_widget:
537 * Releases widget retrieved with gtk_drag_get_ipc_widget ()
539 * widget: the widget to release.
541 ***************************************************************/
544 gtk_drag_release_ipc_widget (GtkWidget *widget)
546 GtkWindow *window = GTK_WINDOW (widget);
547 GdkScreen *screen = gtk_widget_get_screen (widget);
548 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
549 "gtk-dnd-ipc-widgets");
550 ungrab_dnd_keys (widget, GDK_CURRENT_TIME);
552 gtk_window_group_remove_window (window->group, window);
553 drag_widgets = g_slist_prepend (drag_widgets, widget);
554 g_object_set_data (G_OBJECT (screen),
555 I_("gtk-dnd-ipc-widgets"),
560 gtk_drag_get_event_time (GdkEvent *event)
562 guint32 tm = GDK_CURRENT_TIME;
567 case GDK_MOTION_NOTIFY:
568 tm = event->motion.time; break;
569 case GDK_BUTTON_PRESS:
570 case GDK_2BUTTON_PRESS:
571 case GDK_3BUTTON_PRESS:
572 case GDK_BUTTON_RELEASE:
573 tm = event->button.time; break;
575 case GDK_KEY_RELEASE:
576 tm = event->key.time; break;
577 case GDK_ENTER_NOTIFY:
578 case GDK_LEAVE_NOTIFY:
579 tm = event->crossing.time; break;
580 case GDK_PROPERTY_NOTIFY:
581 tm = event->property.time; break;
582 case GDK_SELECTION_CLEAR:
583 case GDK_SELECTION_REQUEST:
584 case GDK_SELECTION_NOTIFY:
585 tm = event->selection.time; break;
586 case GDK_PROXIMITY_IN:
587 case GDK_PROXIMITY_OUT:
588 tm = event->proximity.time; break;
589 default: /* use current time */
597 gtk_drag_get_event_actions (GdkEvent *event,
599 GdkDragAction actions,
600 GdkDragAction *suggested_action,
601 GdkDragAction *possible_actions)
603 *suggested_action = 0;
604 *possible_actions = 0;
608 GdkModifierType state = 0;
612 case GDK_MOTION_NOTIFY:
613 state = event->motion.state;
615 case GDK_BUTTON_PRESS:
616 case GDK_2BUTTON_PRESS:
617 case GDK_3BUTTON_PRESS:
618 case GDK_BUTTON_RELEASE:
619 state = event->button.state;
622 case GDK_KEY_RELEASE:
623 state = event->key.state;
625 case GDK_ENTER_NOTIFY:
626 case GDK_LEAVE_NOTIFY:
627 state = event->crossing.state;
633 if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
635 *suggested_action = GDK_ACTION_ASK;
636 *possible_actions = actions;
638 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
640 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
642 if (actions & GDK_ACTION_LINK)
644 *suggested_action = GDK_ACTION_LINK;
645 *possible_actions = GDK_ACTION_LINK;
648 else if (state & GDK_CONTROL_MASK)
650 if (actions & GDK_ACTION_COPY)
652 *suggested_action = GDK_ACTION_COPY;
653 *possible_actions = GDK_ACTION_COPY;
659 if (actions & GDK_ACTION_MOVE)
661 *suggested_action = GDK_ACTION_MOVE;
662 *possible_actions = GDK_ACTION_MOVE;
669 *possible_actions = actions;
671 if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
672 *suggested_action = GDK_ACTION_ASK;
673 else if (actions & GDK_ACTION_COPY)
674 *suggested_action = GDK_ACTION_COPY;
675 else if (actions & GDK_ACTION_MOVE)
676 *suggested_action = GDK_ACTION_MOVE;
677 else if (actions & GDK_ACTION_LINK)
678 *suggested_action = GDK_ACTION_LINK;
683 *possible_actions = actions;
685 if (actions & GDK_ACTION_COPY)
686 *suggested_action = GDK_ACTION_COPY;
687 else if (actions & GDK_ACTION_MOVE)
688 *suggested_action = GDK_ACTION_MOVE;
689 else if (actions & GDK_ACTION_LINK)
690 *suggested_action = GDK_ACTION_LINK;
695 gtk_drag_can_use_rgba_cursor (GdkDisplay *display,
699 guint max_width, max_height;
701 if (!gdk_display_supports_cursor_color (display))
704 if (!gdk_display_supports_cursor_alpha (display))
707 gdk_display_get_maximal_cursor_size (display,
710 if (width > max_width || height > max_height)
712 /* can't use rgba cursor (too large) */
720 gtk_drag_get_cursor (GdkDisplay *display,
721 GdkDragAction action,
722 GtkDragSourceInfo *info)
726 /* reconstruct the cursors for each new drag (thus !info),
727 * to catch cursor theme changes
731 for (i = 0 ; i < n_drag_cursors - 1; i++)
732 if (drag_cursors[i].cursor != NULL)
734 gdk_cursor_unref (drag_cursors[i].cursor);
735 drag_cursors[i].cursor = NULL;
739 for (i = 0 ; i < n_drag_cursors - 1; i++)
740 if (drag_cursors[i].action == action)
743 if (drag_cursors[i].pixbuf == NULL)
744 drag_cursors[i].pixbuf =
745 gdk_pixbuf_new_from_inline (-1, drag_cursors[i].data, FALSE, NULL);
747 if (drag_cursors[i].cursor != NULL)
749 if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
751 gdk_cursor_unref (drag_cursors[i].cursor);
752 drag_cursors[i].cursor = NULL;
756 if (drag_cursors[i].cursor == NULL)
757 drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
759 if (drag_cursors[i].cursor == NULL)
760 drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);
762 if (info && info->icon_pixbuf)
764 gint cursor_width, cursor_height;
765 gint icon_width, icon_height;
767 GdkPixbuf *cursor_pixbuf, *pixbuf;
769 gint icon_x, icon_y, ref_x, ref_y;
771 if (info->drag_cursors[i] != NULL)
773 if (display == gdk_cursor_get_display (info->drag_cursors[i]))
774 return info->drag_cursors[i];
776 gdk_cursor_unref (info->drag_cursors[i]);
777 info->drag_cursors[i] = NULL;
780 icon_x = info->hot_x;
781 icon_y = info->hot_y;
782 icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
783 icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);
786 cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
788 cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
791 if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
792 hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
794 if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
795 hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
798 /* The code below is an attempt to let cursor themes
799 * determine the attachment of the icon to enable things
800 * like the following:
808 * It does not work since Xcursor doesn't allow to attach
809 * any additional information to cursors in a retrievable
810 * way (there are comments, but no way to get at them
811 * short of searching for the actual cursor file).
812 * If this code ever gets used, the icon_window placement
813 * must be changed to recognize these placement options
814 * as well. Note that this code ignores info->hot_x/y.
816 for (j = 0; j < 10; j++)
821 GtkAnchorType icon_anchor;
823 g_snprintf (key, 32, "comment%d", j);
824 opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
825 if (opt && g_str_has_prefix ("icon-attach:", opt))
827 toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
828 if (g_strv_length (toks) != 3)
833 icon_anchor = atoi (toks[0]);
834 icon_x = atoi (toks[1]);
835 icon_y = atoi (toks[2]);
839 case GTK_ANCHOR_NORTH:
840 case GTK_ANCHOR_CENTER:
841 case GTK_ANCHOR_SOUTH:
842 icon_x += icon_width / 2;
844 case GTK_ANCHOR_NORTH_EAST:
845 case GTK_ANCHOR_EAST:
846 case GTK_ANCHOR_SOUTH_EAST:
847 icon_x += icon_width;
854 case GTK_ANCHOR_WEST:
855 case GTK_ANCHOR_CENTER:
856 case GTK_ANCHOR_EAST:
857 icon_y += icon_height / 2;
859 case GTK_ANCHOR_SOUTH_WEST:
860 case GTK_ANCHOR_SOUTH:
861 case GTK_ANCHOR_SOUTH_EAST:
862 icon_x += icon_height;
874 cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
875 cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
877 ref_x = MAX (hot_x, icon_x);
878 ref_y = MAX (hot_y, icon_y);
879 width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
880 height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
882 if (gtk_drag_can_use_rgba_cursor (display, width, height))
884 /* Composite cursor and icon so that both hotspots
885 * end up at (ref_x, ref_y)
887 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
890 gdk_pixbuf_fill (pixbuf, 0xff000000);
892 gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
893 ref_x - icon_x, ref_y - icon_y,
894 icon_width, icon_height,
895 ref_x - icon_x, ref_y - icon_y,
897 GDK_INTERP_BILINEAR, 255);
899 gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
900 ref_x - hot_x, ref_y - hot_y,
901 cursor_width, cursor_height,
902 ref_x - hot_x, ref_y - hot_y,
904 GDK_INTERP_BILINEAR, 255);
906 info->drag_cursors[i] =
907 gdk_cursor_new_from_pixbuf (display, pixbuf, ref_x, ref_y);
909 g_object_unref (pixbuf);
912 g_object_unref (cursor_pixbuf);
914 if (info->drag_cursors[i] != NULL)
915 return info->drag_cursors[i];
918 return drag_cursors[i].cursor;
922 gtk_drag_update_cursor (GtkDragSourceInfo *info)
927 if (!info->have_grab)
930 for (i = 0 ; i < n_drag_cursors - 1; i++)
931 if (info->cursor == drag_cursors[i].cursor ||
932 info->cursor == info->drag_cursors[i])
935 if (i == n_drag_cursors)
938 cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor),
939 drag_cursors[i].action, info);
941 if (cursor != info->cursor)
943 gdk_pointer_grab (info->ipc_widget->window, FALSE,
944 GDK_POINTER_MOTION_MASK |
945 GDK_BUTTON_RELEASE_MASK,
947 cursor, info->grab_time);
948 info->cursor = cursor;
952 /********************
954 ********************/
956 /*************************************************************
958 * Get the data for a drag or drop
960 * context - drag context
961 * target - format to retrieve the data in.
962 * time - timestamp of triggering event.
965 *************************************************************/
968 gtk_drag_get_data (GtkWidget *widget,
969 GdkDragContext *context,
973 GtkWidget *selection_widget;
975 g_return_if_fail (GTK_IS_WIDGET (widget));
976 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
977 g_return_if_fail (!context->is_source);
979 selection_widget = gtk_drag_get_ipc_widget (widget);
981 g_object_ref (context);
982 g_object_ref (widget);
984 g_signal_connect (selection_widget, "selection-received",
985 G_CALLBACK (gtk_drag_selection_received), widget);
987 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
989 gtk_selection_convert (selection_widget,
990 gdk_drag_get_selection (context),
996 /*************************************************************
997 * gtk_drag_get_source_widget:
998 * Get the widget the was the source of this drag, if
999 * the drag originated from this application.
1001 * context: The drag context for this drag
1003 * The source widget, or NULL if the drag originated from
1004 * a different application.
1005 *************************************************************/
1008 gtk_drag_get_source_widget (GdkDragContext *context)
1012 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
1013 g_return_val_if_fail (!context->is_source, NULL);
1015 tmp_list = source_widgets;
1018 GtkWidget *ipc_widget = tmp_list->data;
1020 if (ipc_widget->window == context->source_window)
1022 GtkDragSourceInfo *info;
1023 info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
1025 return info ? info->widget : NULL;
1028 tmp_list = tmp_list->next;
1034 /*************************************************************
1036 * Notify the drag source that the transfer of data
1039 * context: The drag context for this drag
1040 * success: Was the data successfully transferred?
1041 * time: The timestamp to use when notifying the destination.
1043 *************************************************************/
1046 gtk_drag_finish (GdkDragContext *context,
1051 GdkAtom target = GDK_NONE;
1053 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
1054 g_return_if_fail (!context->is_source);
1058 target = gdk_atom_intern_static_string ("DELETE");
1060 else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
1062 target = gdk_atom_intern_static_string (success ?
1063 "XmTRANSFER_SUCCESS" :
1064 "XmTRANSFER_FAILURE");
1067 if (target != GDK_NONE)
1069 GtkWidget *selection_widget = gtk_drag_get_ipc_widget_for_screen (gdk_drawable_get_screen (context->source_window));
1071 g_object_ref (context);
1073 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
1074 g_signal_connect (selection_widget, "selection-received",
1075 G_CALLBACK (gtk_drag_selection_received),
1078 gtk_selection_convert (selection_widget,
1079 gdk_drag_get_selection (context),
1084 if (!(success && del))
1085 gdk_drop_finish (context, success, time);
1088 /*************************************************************
1089 * gtk_drag_highlight_expose:
1090 * Callback for expose_event for highlighted widgets.
1096 *************************************************************/
1099 gtk_drag_highlight_expose (GtkWidget *widget,
1100 GdkEventExpose *event,
1103 gint x, y, width, height;
1105 if (gtk_widget_is_drawable (widget))
1109 if (!gtk_widget_get_has_window (widget))
1111 x = widget->allocation.x;
1112 y = widget->allocation.y;
1113 width = widget->allocation.width;
1114 height = widget->allocation.height;
1120 gdk_drawable_get_size (widget->window, &width, &height);
1123 gtk_paint_shadow (widget->style, widget->window,
1124 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1125 &event->area, widget, "dnd",
1126 x, y, width, height);
1128 cr = gdk_cairo_create (widget->window);
1129 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
1130 cairo_set_line_width (cr, 1.0);
1131 cairo_rectangle (cr,
1133 width - 1, height - 1);
1141 /*************************************************************
1142 * gtk_drag_highlight:
1143 * Highlight the given widget in the default manner.
1147 *************************************************************/
1150 gtk_drag_highlight (GtkWidget *widget)
1152 g_return_if_fail (GTK_IS_WIDGET (widget));
1154 g_signal_connect_after (widget, "expose-event",
1155 G_CALLBACK (gtk_drag_highlight_expose),
1158 gtk_widget_queue_draw (widget);
1161 /*************************************************************
1162 * gtk_drag_unhighlight:
1163 * Refresh the given widget to remove the highlight.
1167 *************************************************************/
1170 gtk_drag_unhighlight (GtkWidget *widget)
1172 g_return_if_fail (GTK_IS_WIDGET (widget));
1174 g_signal_handlers_disconnect_by_func (widget,
1175 gtk_drag_highlight_expose,
1178 gtk_widget_queue_draw (widget);
1182 gtk_drag_dest_set_internal (GtkWidget *widget,
1183 GtkDragDestSite *site)
1185 GtkDragDestSite *old_site;
1187 g_return_if_fail (widget != NULL);
1189 /* HACK, do this in the destroy */
1190 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1193 g_signal_handlers_disconnect_by_func (widget,
1194 gtk_drag_dest_realized,
1196 g_signal_handlers_disconnect_by_func (widget,
1197 gtk_drag_dest_hierarchy_changed,
1200 site->track_motion = old_site->track_motion;
1203 if (GTK_WIDGET_REALIZED (widget))
1204 gtk_drag_dest_realized (widget);
1206 g_signal_connect (widget, "realize",
1207 G_CALLBACK (gtk_drag_dest_realized), site);
1208 g_signal_connect (widget, "hierarchy-changed",
1209 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1211 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
1212 site, gtk_drag_dest_site_destroy);
1216 * gtk_drag_dest_set:
1217 * @widget: a #GtkWidget
1218 * @flags: which types of default drag behavior to use
1219 * @targets: a pointer to an array of #GtkTargetEntry<!-- -->s indicating
1220 * the drop types that this @widget will accept. Later you can access the list
1221 * with gtk_drag_dest_get_target_list() and gtk_drag_dest_find_target().
1222 * @n_targets: the number of entries in @targets.
1223 * @actions: a bitmask of possible actions for a drop onto this @widget.
1225 * Sets a widget as a potential drop destination, and adds default behaviors.
1227 * The default behaviors listed in @flags have an effect similar
1228 * to installing default handlers for the widget's drag-and-drop signals
1229 * (#GtkWidget:drag-motion, #GtkWidget:drag-drop, ...). They all exist
1230 * for convenience. When passing #GTK_DEST_DEFAULT_ALL for instance it is
1231 * sufficient to connect to the widget's #GtkWidget::drag-data-received
1232 * signal to get primitive, but consistent drag-and-drop support.
1234 * Things become more complicated when you try to preview the dragged data,
1235 * as described in the documentation for #GtkWidget:drag-motion. The default
1236 * behaviors described by @flags make some assumptions, that can conflict
1237 * with your own signal handlers. For instance #GTK_DEST_DEFAULT_DROP causes
1238 * invokations of gdk_drag_status() in the context of #GtkWidget:drag-motion,
1239 * and invokations of gtk_drag_finish() in #GtkWidget:drag-data-received.
1240 * Especially the later is dramatic, when your own #GtkWidget:drag-motion
1241 * handler calls gtk_drag_get_data() to inspect the dragged data.
1243 * There's no way to set a default action here, you can use the
1244 * #GtkWidget:drag-motion callback for that. Here's an example which selects
1245 * the action to use depending on whether the control key is pressed or not:
1248 * drag_motion (GtkWidget *widget,
1249 * GdkDragContext *context,
1254 * GdkModifierType mask;
1256 * gdk_window_get_pointer (gtk_widget_get_window (widget),
1257 * NULL, NULL, &mask);
1258 * if (mask & GDK_CONTROL_MASK)
1259 * gdk_drag_status (context, GDK_ACTION_COPY, time);
1261 * gdk_drag_status (context, GDK_ACTION_MOVE, time);
1266 gtk_drag_dest_set (GtkWidget *widget,
1267 GtkDestDefaults flags,
1268 const GtkTargetEntry *targets,
1270 GdkDragAction actions)
1272 GtkDragDestSite *site;
1274 g_return_if_fail (GTK_IS_WIDGET (widget));
1276 site = g_new (GtkDragDestSite, 1);
1278 site->flags = flags;
1279 site->have_drag = FALSE;
1281 site->target_list = gtk_target_list_new (targets, n_targets);
1283 site->target_list = NULL;
1284 site->actions = actions;
1285 site->do_proxy = FALSE;
1286 site->proxy_window = NULL;
1287 site->track_motion = FALSE;
1289 gtk_drag_dest_set_internal (widget, site);
1292 /*************************************************************
1293 * gtk_drag_dest_set_proxy:
1294 * Set up this widget to proxy drags elsewhere.
1297 * proxy_window: window to which forward drag events
1298 * protocol: Drag protocol which the dest widget accepts
1299 * use_coordinates: If true, send the same coordinates to the
1300 * destination, because it is a embedded
1303 *************************************************************/
1306 gtk_drag_dest_set_proxy (GtkWidget *widget,
1307 GdkWindow *proxy_window,
1308 GdkDragProtocol protocol,
1309 gboolean use_coordinates)
1311 GtkDragDestSite *site;
1313 g_return_if_fail (GTK_IS_WIDGET (widget));
1314 g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1316 site = g_new (GtkDragDestSite, 1);
1319 site->have_drag = FALSE;
1320 site->target_list = NULL;
1322 site->proxy_window = proxy_window;
1324 g_object_ref (proxy_window);
1325 site->do_proxy = TRUE;
1326 site->proxy_protocol = protocol;
1327 site->proxy_coords = use_coordinates;
1328 site->track_motion = FALSE;
1330 gtk_drag_dest_set_internal (widget, site);
1333 /*************************************************************
1334 * gtk_drag_dest_unset
1335 * Unregister this widget as a drag target.
1339 *************************************************************/
1342 gtk_drag_dest_unset (GtkWidget *widget)
1344 GtkDragDestSite *old_site;
1346 g_return_if_fail (GTK_IS_WIDGET (widget));
1348 old_site = g_object_get_data (G_OBJECT (widget),
1352 g_signal_handlers_disconnect_by_func (widget,
1353 gtk_drag_dest_realized,
1355 g_signal_handlers_disconnect_by_func (widget,
1356 gtk_drag_dest_hierarchy_changed,
1360 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1364 * gtk_drag_dest_get_target_list:
1365 * @widget: a #GtkWidget
1367 * Returns the list of targets this widget can accept from
1370 * Return value: the #GtkTargetList, or %NULL if none
1373 gtk_drag_dest_get_target_list (GtkWidget *widget)
1375 GtkDragDestSite *site;
1377 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1379 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1381 return site ? site->target_list : NULL;
1385 * gtk_drag_dest_set_target_list:
1386 * @widget: a #GtkWidget that's a drag destination
1387 * @target_list: (allow-none): list of droppable targets, or %NULL for none
1389 * Sets the target types that this widget can accept from drag-and-drop.
1390 * The widget must first be made into a drag destination with
1391 * gtk_drag_dest_set().
1394 gtk_drag_dest_set_target_list (GtkWidget *widget,
1395 GtkTargetList *target_list)
1397 GtkDragDestSite *site;
1399 g_return_if_fail (GTK_IS_WIDGET (widget));
1401 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1405 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1406 "to make the widget into a drag destination");
1411 gtk_target_list_ref (target_list);
1413 if (site->target_list)
1414 gtk_target_list_unref (site->target_list);
1416 site->target_list = target_list;
1420 * gtk_drag_dest_add_text_targets:
1421 * @widget: a #GtkWidget that's a drag destination
1423 * Add the text targets supported by #GtkSelection to
1424 * the target list of the drag destination. The targets
1425 * are added with @info = 0. If you need another value,
1426 * use gtk_target_list_add_text_targets() and
1427 * gtk_drag_dest_set_target_list().
1432 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1434 GtkTargetList *target_list;
1436 target_list = gtk_drag_dest_get_target_list (widget);
1438 gtk_target_list_ref (target_list);
1440 target_list = gtk_target_list_new (NULL, 0);
1441 gtk_target_list_add_text_targets (target_list, 0);
1442 gtk_drag_dest_set_target_list (widget, target_list);
1443 gtk_target_list_unref (target_list);
1447 * gtk_drag_dest_add_image_targets:
1448 * @widget: a #GtkWidget that's a drag destination
1450 * Add the image targets supported by #GtkSelection to
1451 * the target list of the drag destination. The targets
1452 * are added with @info = 0. If you need another value,
1453 * use gtk_target_list_add_image_targets() and
1454 * gtk_drag_dest_set_target_list().
1459 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1461 GtkTargetList *target_list;
1463 target_list = gtk_drag_dest_get_target_list (widget);
1465 gtk_target_list_ref (target_list);
1467 target_list = gtk_target_list_new (NULL, 0);
1468 gtk_target_list_add_image_targets (target_list, 0, FALSE);
1469 gtk_drag_dest_set_target_list (widget, target_list);
1470 gtk_target_list_unref (target_list);
1474 * gtk_drag_dest_add_uri_targets:
1475 * @widget: a #GtkWidget that's a drag destination
1477 * Add the URI targets supported by #GtkSelection to
1478 * the target list of the drag destination. The targets
1479 * are added with @info = 0. If you need another value,
1480 * use gtk_target_list_add_uri_targets() and
1481 * gtk_drag_dest_set_target_list().
1486 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1488 GtkTargetList *target_list;
1490 target_list = gtk_drag_dest_get_target_list (widget);
1492 gtk_target_list_ref (target_list);
1494 target_list = gtk_target_list_new (NULL, 0);
1495 gtk_target_list_add_uri_targets (target_list, 0);
1496 gtk_drag_dest_set_target_list (widget, target_list);
1497 gtk_target_list_unref (target_list);
1501 * gtk_drag_dest_set_track_motion:
1502 * @widget: a #GtkWidget that's a drag destination
1503 * @track_motion: whether to accept all targets
1505 * Tells the widget to emit ::drag-motion and ::drag-leave
1506 * events regardless of the targets and the %GTK_DEST_DEFAULT_MOTION
1509 * This may be used when a widget wants to do generic
1510 * actions regardless of the targets that the source offers.
1515 gtk_drag_dest_set_track_motion (GtkWidget *widget,
1516 gboolean track_motion)
1518 GtkDragDestSite *site;
1520 g_return_if_fail (GTK_IS_WIDGET (widget));
1522 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1524 g_return_if_fail (site != NULL);
1526 site->track_motion = track_motion != FALSE;
1530 * gtk_drag_dest_get_track_motion:
1531 * @widget: a #GtkWidget that's a drag destination
1533 * Returns whether the widget has been configured to always
1534 * emit ::drag-motion signals.
1536 * Return Value: %TRUE if the widget always emits ::drag-motion events
1541 gtk_drag_dest_get_track_motion (GtkWidget *widget)
1543 GtkDragDestSite *site;
1545 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1547 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1550 return site->track_motion;
1555 /*************************************************************
1556 * _gtk_drag_dest_handle_event:
1557 * Called from widget event handling code on Drag events
1561 * toplevel: Toplevel widget that received the event
1564 *************************************************************/
1567 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1570 GtkDragDestInfo *info;
1571 GdkDragContext *context;
1573 g_return_if_fail (toplevel != NULL);
1574 g_return_if_fail (event != NULL);
1576 context = event->dnd.context;
1578 info = gtk_drag_get_dest_info (context, TRUE);
1580 /* Find the widget for the event */
1581 switch (event->type)
1583 case GDK_DRAG_ENTER:
1586 case GDK_DRAG_LEAVE:
1589 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1590 info->widget = NULL;
1594 case GDK_DRAG_MOTION:
1595 case GDK_DROP_START:
1597 GtkDragFindData data;
1600 if (event->type == GDK_DROP_START)
1602 info->dropped = TRUE;
1603 /* We send a leave here so that the widget unhighlights
1608 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1609 info->widget = NULL;
1613 #ifdef GDK_WINDOWING_X11
1614 /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1616 * Currently gdk_window_get_position doesn't provide reliable
1617 * information for embedded windows, so we call the much more
1618 * expensive gdk_window_get_origin().
1620 if (GTK_IS_PLUG (toplevel))
1621 gdk_window_get_origin (toplevel->window, &tx, &ty);
1623 #endif /* GDK_WINDOWING_X11 */
1624 gdk_window_get_position (toplevel->window, &tx, &ty);
1626 data.x = event->dnd.x_root - tx;
1627 data.y = event->dnd.y_root - ty;
1628 data.context = context;
1631 data.toplevel = TRUE;
1632 data.callback = (event->type == GDK_DRAG_MOTION) ?
1633 gtk_drag_dest_motion : gtk_drag_dest_drop;
1634 data.time = event->dnd.time;
1636 gtk_drag_find_widget (toplevel, &data);
1638 if (info->widget && !data.found)
1640 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1641 info->widget = NULL;
1646 if (event->type == GDK_DRAG_MOTION)
1649 gdk_drag_status (context, 0, event->dnd.time);
1651 else if (event->type == GDK_DROP_START && !info->proxy_source)
1653 gdk_drop_reply (context, data.found, event->dnd.time);
1654 if ((context->protocol == GDK_DRAG_PROTO_MOTIF) && !data.found)
1655 gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1661 g_assert_not_reached ();
1666 * gtk_drag_dest_find_target:
1667 * @widget: drag destination widget
1668 * @context: drag context
1669 * @target_list: (allow-none): list of droppable targets, or %NULL to use
1670 * gtk_drag_dest_get_target_list (@widget).
1672 * Looks for a match between @context->targets and the
1673 * @dest_target_list, returning the first matching target, otherwise
1674 * returning %GDK_NONE. @dest_target_list should usually be the return
1675 * value from gtk_drag_dest_get_target_list(), but some widgets may
1676 * have different valid targets for different parts of the widget; in
1677 * that case, they will have to implement a drag_motion handler that
1678 * passes the correct target list to this function.
1680 * Return value: first target that the source offers and the dest can accept, or %GDK_NONE
1683 gtk_drag_dest_find_target (GtkWidget *widget,
1684 GdkDragContext *context,
1685 GtkTargetList *target_list)
1688 GList *tmp_source = NULL;
1689 GtkWidget *source_widget;
1691 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1692 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1693 g_return_val_if_fail (!context->is_source, GDK_NONE);
1696 source_widget = gtk_drag_get_source_widget (context);
1698 if (target_list == NULL)
1699 target_list = gtk_drag_dest_get_target_list (widget);
1701 if (target_list == NULL)
1704 tmp_target = target_list->list;
1707 GtkTargetPair *pair = tmp_target->data;
1708 tmp_source = context->targets;
1711 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1713 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1714 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)) &&
1715 (!(pair->flags & GTK_TARGET_OTHER_APP) || !source_widget) &&
1716 (!(pair->flags & GTK_TARGET_OTHER_WIDGET) || (source_widget != widget)))
1717 return pair->target;
1721 tmp_source = tmp_source->next;
1723 tmp_target = tmp_target->next;
1730 gtk_drag_selection_received (GtkWidget *widget,
1731 GtkSelectionData *selection_data,
1735 GdkDragContext *context;
1736 GtkDragDestInfo *info;
1737 GtkWidget *drop_widget;
1741 context = g_object_get_data (G_OBJECT (widget), "drag-context");
1742 info = gtk_drag_get_dest_info (context, FALSE);
1744 if (info->proxy_data &&
1745 info->proxy_data->target == selection_data->target)
1747 gtk_selection_data_set (info->proxy_data,
1748 selection_data->type,
1749 selection_data->format,
1750 selection_data->data,
1751 selection_data->length);
1756 if (selection_data->target == gdk_atom_intern_static_string ("DELETE"))
1758 gtk_drag_finish (context, TRUE, FALSE, time);
1760 else if ((selection_data->target == gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS")) ||
1761 (selection_data->target == gdk_atom_intern_static_string ("XmTRANSFER_FAILURE")))
1767 GtkDragDestSite *site;
1769 site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1771 if (site && site->target_list)
1775 if (gtk_target_list_find (site->target_list,
1776 selection_data->target,
1779 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1780 selection_data->length >= 0)
1781 g_signal_emit_by_name (drop_widget,
1782 "drag-data-received",
1783 context, info->drop_x, info->drop_y,
1790 g_signal_emit_by_name (drop_widget,
1791 "drag-data-received",
1792 context, info->drop_x, info->drop_y,
1797 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1800 gtk_drag_finish (context,
1801 (selection_data->length >= 0),
1802 (context->action == GDK_ACTION_MOVE),
1806 g_object_unref (drop_widget);
1809 g_signal_handlers_disconnect_by_func (widget,
1810 gtk_drag_selection_received,
1813 g_object_set_data (G_OBJECT (widget), I_("drag-context"), NULL);
1814 g_object_unref (context);
1816 gtk_drag_release_ipc_widget (widget);
1820 prepend_and_ref_widget (GtkWidget *widget,
1823 GSList **slist_p = data;
1825 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
1828 /*************************************************************
1829 * gtk_drag_find_widget:
1830 * Recursive callback used to locate widgets for
1831 * DRAG_MOTION and DROP_START events.
1835 *************************************************************/
1838 gtk_drag_find_widget (GtkWidget *widget,
1839 GtkDragFindData *data)
1841 GtkAllocation new_allocation;
1842 gint allocation_to_window_x = 0;
1843 gint allocation_to_window_y = 0;
1847 if (data->found || !GTK_WIDGET_MAPPED (widget) || !GTK_WIDGET_SENSITIVE (widget))
1850 /* Note that in the following code, we only count the
1851 * position as being inside a WINDOW widget if it is inside
1852 * widget->window; points that are outside of widget->window
1853 * but within the allocation are not counted. This is consistent
1854 * with the way we highlight drag targets.
1856 * data->x,y are relative to widget->parent->window (if
1857 * widget is not a toplevel, widget->window otherwise).
1858 * We compute the allocation of widget in the same coordinates,
1859 * clipping to widget->window, and all intermediate
1860 * windows. If data->x,y is inside that, then we translate
1861 * our coordinates to be relative to widget->window and
1864 new_allocation = widget->allocation;
1869 GdkWindow *window = widget->window;
1871 /* Compute the offset from allocation-relative to
1872 * window-relative coordinates.
1874 allocation_to_window_x = widget->allocation.x;
1875 allocation_to_window_y = widget->allocation.y;
1877 if (gtk_widget_get_has_window (widget))
1879 /* The allocation is relative to the parent window for
1880 * window widgets, not to widget->window.
1882 gdk_window_get_position (window, &tx, &ty);
1884 allocation_to_window_x -= tx;
1885 allocation_to_window_y -= ty;
1888 new_allocation.x = 0 + allocation_to_window_x;
1889 new_allocation.y = 0 + allocation_to_window_y;
1891 while (window && window != widget->parent->window)
1893 GdkRectangle window_rect = { 0, 0, 0, 0 };
1895 gdk_drawable_get_size (window, &window_rect.width, &window_rect.height);
1897 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
1899 gdk_window_get_position (window, &tx, &ty);
1900 new_allocation.x += tx;
1902 new_allocation.y += ty;
1905 window = gdk_window_get_parent (window);
1908 if (!window) /* Window and widget heirarchies didn't match. */
1912 if (data->toplevel ||
1913 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
1914 (data->x < new_allocation.x + new_allocation.width) &&
1915 (data->y < new_allocation.y + new_allocation.height)))
1917 /* First, check if the drag is in a valid drop site in
1918 * one of our children
1920 if (GTK_IS_CONTAINER (widget))
1922 GtkDragFindData new_data = *data;
1923 GSList *children = NULL;
1926 new_data.x -= x_offset;
1927 new_data.y -= y_offset;
1928 new_data.found = FALSE;
1929 new_data.toplevel = FALSE;
1931 /* need to reference children temporarily in case the
1932 * ::drag-motion/::drag-drop callbacks change the widget hierarchy.
1934 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
1935 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
1937 if (!new_data.found && gtk_widget_is_drawable (tmp_list->data))
1938 gtk_drag_find_widget (tmp_list->data, &new_data);
1939 g_object_unref (tmp_list->data);
1941 g_slist_free (children);
1943 data->found = new_data.found;
1946 /* If not, and this widget is registered as a drop site, check to
1947 * emit "drag-motion" to check if we are actually in
1951 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1953 data->found = data->callback (widget,
1955 data->x - x_offset - allocation_to_window_x,
1956 data->y - y_offset - allocation_to_window_y,
1958 /* If so, send a "drag-leave" to the last widget */
1961 if (data->info->widget && data->info->widget != widget)
1963 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
1965 data->info->widget = widget;
1972 gtk_drag_proxy_begin (GtkWidget *widget,
1973 GtkDragDestInfo *dest_info,
1976 GtkDragSourceInfo *source_info;
1978 GdkDragContext *context;
1979 GtkWidget *ipc_widget;
1981 if (dest_info->proxy_source)
1983 gdk_drag_abort (dest_info->proxy_source->context, time);
1984 gtk_drag_source_info_destroy (dest_info->proxy_source);
1985 dest_info->proxy_source = NULL;
1988 ipc_widget = gtk_drag_get_ipc_widget (widget);
1989 context = gdk_drag_begin (ipc_widget->window,
1990 dest_info->context->targets);
1992 source_info = gtk_drag_get_source_info (context, TRUE);
1994 source_info->ipc_widget = ipc_widget;
1995 source_info->widget = g_object_ref (widget);
1997 source_info->target_list = gtk_target_list_new (NULL, 0);
1998 tmp_list = dest_info->context->targets;
2001 gtk_target_list_add (source_info->target_list,
2002 GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
2003 tmp_list = tmp_list->next;
2006 source_info->proxy_dest = dest_info;
2008 g_signal_connect (ipc_widget,
2010 G_CALLBACK (gtk_drag_selection_get),
2013 dest_info->proxy_source = source_info;
2017 gtk_drag_dest_info_destroy (gpointer data)
2019 GtkDragDestInfo *info = data;
2024 static GtkDragDestInfo *
2025 gtk_drag_get_dest_info (GdkDragContext *context,
2028 GtkDragDestInfo *info;
2029 static GQuark info_quark = 0;
2031 info_quark = g_quark_from_static_string ("gtk-dest-info");
2033 info = g_object_get_qdata (G_OBJECT (context), info_quark);
2034 if (!info && create)
2036 info = g_new (GtkDragDestInfo, 1);
2037 info->widget = NULL;
2038 info->context = context;
2039 info->proxy_source = NULL;
2040 info->proxy_data = NULL;
2041 info->dropped = FALSE;
2042 info->proxy_drop_wait = FALSE;
2043 g_object_set_qdata_full (G_OBJECT (context), info_quark,
2044 info, gtk_drag_dest_info_destroy);
2050 static GQuark dest_info_quark = 0;
2052 static GtkDragSourceInfo *
2053 gtk_drag_get_source_info (GdkDragContext *context,
2056 GtkDragSourceInfo *info;
2057 if (!dest_info_quark)
2058 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
2060 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
2061 if (!info && create)
2063 info = g_new0 (GtkDragSourceInfo, 1);
2064 info->context = context;
2065 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
2072 gtk_drag_clear_source_info (GdkDragContext *context)
2074 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
2078 gtk_drag_dest_realized (GtkWidget *widget)
2080 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2082 if (gtk_widget_is_toplevel (toplevel))
2083 gdk_window_register_dnd (toplevel->window);
2087 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
2088 GtkWidget *previous_toplevel)
2090 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
2092 if (gtk_widget_is_toplevel (toplevel) && GTK_WIDGET_REALIZED (toplevel))
2093 gdk_window_register_dnd (toplevel->window);
2097 gtk_drag_dest_site_destroy (gpointer data)
2099 GtkDragDestSite *site = data;
2101 if (site->proxy_window)
2102 g_object_unref (site->proxy_window);
2104 if (site->target_list)
2105 gtk_target_list_unref (site->target_list);
2111 * Default drag handlers
2114 gtk_drag_dest_leave (GtkWidget *widget,
2115 GdkDragContext *context,
2118 GtkDragDestSite *site;
2120 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2121 g_return_if_fail (site != NULL);
2125 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2127 if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
2129 gdk_drag_abort (info->proxy_source->context, time);
2130 gtk_drag_source_info_destroy (info->proxy_source);
2131 info->proxy_source = NULL;
2138 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
2139 gtk_drag_unhighlight (widget);
2141 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag ||
2143 g_signal_emit_by_name (widget, "drag-leave", context, time);
2145 site->have_drag = FALSE;
2150 gtk_drag_dest_motion (GtkWidget *widget,
2151 GdkDragContext *context,
2156 GtkDragDestSite *site;
2157 GdkDragAction action = 0;
2160 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2161 g_return_val_if_fail (site != NULL, FALSE);
2166 GdkEvent *current_event;
2167 GdkWindow *dest_window;
2168 GdkDragProtocol proto;
2170 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
2172 if (!info->proxy_source || info->proxy_source->widget != widget)
2173 gtk_drag_proxy_begin (widget, info, time);
2175 current_event = gtk_get_current_event ();
2177 if (site->proxy_window)
2179 dest_window = site->proxy_window;
2180 proto = site->proxy_protocol;
2184 gdk_drag_find_window_for_screen (info->proxy_source->context,
2186 gdk_drawable_get_screen (current_event->dnd.window),
2187 current_event->dnd.x_root,
2188 current_event->dnd.y_root,
2189 &dest_window, &proto);
2192 gdk_drag_motion (info->proxy_source->context,
2194 current_event->dnd.x_root,
2195 current_event->dnd.y_root,
2196 context->suggested_action,
2197 context->actions, time);
2199 if (!site->proxy_window && dest_window)
2200 g_object_unref (dest_window);
2202 selection = gdk_drag_get_selection (info->proxy_source->context);
2204 selection != gdk_drag_get_selection (info->context))
2205 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2207 gdk_event_free (current_event);
2212 if (site->track_motion || site->flags & GTK_DEST_DEFAULT_MOTION)
2214 if (context->suggested_action & site->actions)
2215 action = context->suggested_action;
2220 for (i = 0; i < 8; i++)
2222 if ((site->actions & (1 << i)) &&
2223 (context->actions & (1 << i)))
2231 if (action && gtk_drag_dest_find_target (widget, context, NULL))
2233 if (!site->have_drag)
2235 site->have_drag = TRUE;
2236 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
2237 gtk_drag_highlight (widget);
2240 gdk_drag_status (context, action, time);
2244 gdk_drag_status (context, 0, time);
2245 if (!site->track_motion)
2250 g_signal_emit_by_name (widget, "drag-motion",
2251 context, x, y, time, &retval);
2253 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
2257 gtk_drag_dest_drop (GtkWidget *widget,
2258 GdkDragContext *context,
2263 GtkDragDestSite *site;
2264 GtkDragDestInfo *info;
2266 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
2267 g_return_val_if_fail (site != NULL, FALSE);
2269 info = gtk_drag_get_dest_info (context, FALSE);
2270 g_return_val_if_fail (info != NULL, FALSE);
2277 if (info->proxy_source ||
2278 (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN))
2280 gtk_drag_drop (info->proxy_source, time);
2284 /* We need to synthesize a motion event, wait for a status,
2285 * and, if we get a good one, do a drop.
2288 GdkEvent *current_event;
2290 GdkWindow *dest_window;
2291 GdkDragProtocol proto;
2293 gtk_drag_proxy_begin (widget, info, time);
2294 info->proxy_drop_wait = TRUE;
2295 info->proxy_drop_time = time;
2297 current_event = gtk_get_current_event ();
2299 if (site->proxy_window)
2301 dest_window = site->proxy_window;
2302 proto = site->proxy_protocol;
2306 gdk_drag_find_window_for_screen (info->proxy_source->context,
2308 gdk_drawable_get_screen (current_event->dnd.window),
2309 current_event->dnd.x_root,
2310 current_event->dnd.y_root,
2311 &dest_window, &proto);
2314 gdk_drag_motion (info->proxy_source->context,
2316 current_event->dnd.x_root,
2317 current_event->dnd.y_root,
2318 context->suggested_action,
2319 context->actions, time);
2321 if (!site->proxy_window && dest_window)
2322 g_object_unref (dest_window);
2324 selection = gdk_drag_get_selection (info->proxy_source->context);
2326 selection != gdk_drag_get_selection (info->context))
2327 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2329 gdk_event_free (current_event);
2338 if (site->flags & GTK_DEST_DEFAULT_DROP)
2340 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2342 if (target == GDK_NONE)
2344 gtk_drag_finish (context, FALSE, FALSE, time);
2348 gtk_drag_get_data (widget, context, target, time);
2351 g_signal_emit_by_name (widget, "drag-drop",
2352 context, x, y, time, &retval);
2354 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2362 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2363 * so that we can set the icon from the source site information
2365 static GdkDragContext *
2366 gtk_drag_begin_internal (GtkWidget *widget,
2367 GtkDragSourceSite *site,
2368 GtkTargetList *target_list,
2369 GdkDragAction actions,
2373 GtkDragSourceInfo *info;
2374 GList *targets = NULL;
2376 guint32 time = GDK_CURRENT_TIME;
2377 GdkDragAction possible_actions, suggested_action;
2378 GdkDragContext *context;
2379 GtkWidget *ipc_widget;
2382 ipc_widget = gtk_drag_get_ipc_widget (widget);
2384 gtk_drag_get_event_actions (event, button, actions,
2385 &suggested_action, &possible_actions);
2387 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2392 time = gdk_event_get_time (event);
2394 if (gdk_pointer_grab (ipc_widget->window, FALSE,
2395 GDK_POINTER_MOTION_MASK |
2396 GDK_BUTTON_RELEASE_MASK, NULL,
2397 cursor, time) != GDK_GRAB_SUCCESS)
2399 gtk_drag_release_ipc_widget (ipc_widget);
2403 grab_dnd_keys (ipc_widget, time);
2405 /* We use a GTK grab here to override any grabs that the widget
2406 * we are dragging from might have held
2408 gtk_grab_add (ipc_widget);
2410 tmp_list = g_list_last (target_list->list);
2413 GtkTargetPair *pair = tmp_list->data;
2414 targets = g_list_prepend (targets,
2415 GINT_TO_POINTER (pair->target));
2416 tmp_list = tmp_list->prev;
2419 source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2421 context = gdk_drag_begin (ipc_widget->window, targets);
2422 g_list_free (targets);
2424 info = gtk_drag_get_source_info (context, TRUE);
2426 info->ipc_widget = ipc_widget;
2427 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), info);
2429 info->widget = g_object_ref (widget);
2431 info->button = button;
2432 info->cursor = cursor;
2433 info->target_list = target_list;
2434 gtk_target_list_ref (target_list);
2436 info->possible_actions = actions;
2438 info->status = GTK_DRAG_STATUS_DRAG;
2439 info->last_event = NULL;
2440 info->selections = NULL;
2441 info->icon_window = NULL;
2442 info->destroy_icon = FALSE;
2444 /* Set cur_x, cur_y here so if the "drag-begin" signal shows
2445 * the drag icon, it will be in the right place
2447 if (event && event->type == GDK_MOTION_NOTIFY)
2449 info->cur_screen = gtk_widget_get_screen (widget);
2450 info->cur_x = event->motion.x_root;
2451 info->cur_y = event->motion.y_root;
2455 gdk_display_get_pointer (gtk_widget_get_display (widget),
2456 &info->cur_screen, &info->cur_x, &info->cur_y, NULL);
2459 g_signal_emit_by_name (widget, "drag-begin", info->context);
2461 /* Ensure that we have an icon before we start the drag; the
2462 * application may have set one in ::drag_begin, or it may
2465 if (!info->icon_window && !info->icon_pixbuf)
2467 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2468 gtk_drag_set_icon_default (context);
2470 switch (site->icon_type)
2472 case GTK_IMAGE_PIXMAP:
2473 gtk_drag_set_icon_pixmap (context,
2475 site->icon_data.pixmap.pixmap,
2479 case GTK_IMAGE_PIXBUF:
2480 gtk_drag_set_icon_pixbuf (context,
2481 site->icon_data.pixbuf.pixbuf,
2484 case GTK_IMAGE_STOCK:
2485 gtk_drag_set_icon_stock (context,
2486 site->icon_data.stock.stock_id,
2489 case GTK_IMAGE_ICON_NAME:
2490 gtk_drag_set_icon_name (context,
2491 site->icon_data.name.icon_name,
2494 case GTK_IMAGE_EMPTY:
2496 g_assert_not_reached();
2501 /* We need to composite the icon into the cursor, if we are
2502 * not using an icon window.
2504 if (info->icon_pixbuf)
2506 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2510 if (cursor != info->cursor)
2512 gdk_pointer_grab (widget->window, FALSE,
2513 GDK_POINTER_MOTION_MASK |
2514 GDK_BUTTON_RELEASE_MASK,
2517 info->cursor = cursor;
2521 if (event && event->type == GDK_MOTION_NOTIFY)
2522 gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2524 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, event);
2526 info->start_x = info->cur_x;
2527 info->start_y = info->cur_y;
2529 g_signal_connect (info->ipc_widget, "grab-broken-event",
2530 G_CALLBACK (gtk_drag_grab_broken_event_cb), info);
2531 g_signal_connect (info->ipc_widget, "grab-notify",
2532 G_CALLBACK (gtk_drag_grab_notify_cb), info);
2533 g_signal_connect (info->ipc_widget, "button-release-event",
2534 G_CALLBACK (gtk_drag_button_release_cb), info);
2535 g_signal_connect (info->ipc_widget, "motion-notify-event",
2536 G_CALLBACK (gtk_drag_motion_cb), info);
2537 g_signal_connect (info->ipc_widget, "key-press-event",
2538 G_CALLBACK (gtk_drag_key_cb), info);
2539 g_signal_connect (info->ipc_widget, "key-release-event",
2540 G_CALLBACK (gtk_drag_key_cb), info);
2541 g_signal_connect (info->ipc_widget, "selection-get",
2542 G_CALLBACK (gtk_drag_selection_get), info);
2544 info->have_grab = TRUE;
2545 info->grab_time = time;
2547 return info->context;
2552 * @widget: the source widget.
2553 * @targets: The targets (data formats) in which the
2554 * source can provide the data.
2555 * @actions: A bitmask of the allowed drag actions for this drag.
2556 * @button: The button the user clicked to start the drag.
2557 * @event: The event that triggered the start of the drag.
2559 * Initiates a drag on the source side. The function
2560 * only needs to be used when the application is
2561 * starting drags itself, and is not needed when
2562 * gtk_drag_source_set() is used.
2564 * Return value: the context for this drag.
2567 gtk_drag_begin (GtkWidget *widget,
2568 GtkTargetList *targets,
2569 GdkDragAction actions,
2573 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2574 g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), NULL);
2575 g_return_val_if_fail (targets != NULL, NULL);
2577 return gtk_drag_begin_internal (widget, NULL, targets,
2578 actions, button, event);
2581 /*************************************************************
2582 * gtk_drag_source_set:
2583 * Register a drop site, and possibly add default behaviors.
2586 * start_button_mask: Mask of allowed buttons to start drag
2587 * targets: Table of targets for this source
2589 * actions: Actions allowed for this source
2591 *************************************************************/
2594 gtk_drag_source_set (GtkWidget *widget,
2595 GdkModifierType start_button_mask,
2596 const GtkTargetEntry *targets,
2598 GdkDragAction actions)
2600 GtkDragSourceSite *site;
2602 g_return_if_fail (GTK_IS_WIDGET (widget));
2604 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2606 gtk_widget_add_events (widget,
2607 gtk_widget_get_events (widget) |
2608 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2609 GDK_BUTTON_MOTION_MASK);
2613 if (site->target_list)
2614 gtk_target_list_unref (site->target_list);
2618 site = g_new0 (GtkDragSourceSite, 1);
2620 site->icon_type = GTK_IMAGE_EMPTY;
2622 g_signal_connect (widget, "button-press-event",
2623 G_CALLBACK (gtk_drag_source_event_cb),
2625 g_signal_connect (widget, "button-release-event",
2626 G_CALLBACK (gtk_drag_source_event_cb),
2628 g_signal_connect (widget, "motion-notify-event",
2629 G_CALLBACK (gtk_drag_source_event_cb),
2632 g_object_set_data_full (G_OBJECT (widget),
2633 I_("gtk-site-data"),
2634 site, gtk_drag_source_site_destroy);
2637 site->start_button_mask = start_button_mask;
2639 site->target_list = gtk_target_list_new (targets, n_targets);
2641 site->actions = actions;
2644 /*************************************************************
2645 * gtk_drag_source_unset
2646 * Unregister this widget as a drag source.
2650 *************************************************************/
2653 gtk_drag_source_unset (GtkWidget *widget)
2655 GtkDragSourceSite *site;
2657 g_return_if_fail (GTK_IS_WIDGET (widget));
2659 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2663 g_signal_handlers_disconnect_by_func (widget,
2664 gtk_drag_source_event_cb,
2666 g_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
2671 * gtk_drag_source_get_target_list:
2672 * @widget: a #GtkWidget
2674 * Gets the list of targets this widget can provide for
2677 * Return value: the #GtkTargetList, or %NULL if none
2682 gtk_drag_source_get_target_list (GtkWidget *widget)
2684 GtkDragSourceSite *site;
2686 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2688 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2690 return site ? site->target_list : NULL;
2694 * gtk_drag_source_set_target_list:
2695 * @widget: a #GtkWidget that's a drag source
2696 * @target_list: (allow-none): list of draggable targets, or %NULL for none
2698 * Changes the target types that this widget offers for drag-and-drop.
2699 * The widget must first be made into a drag source with
2700 * gtk_drag_source_set().
2705 gtk_drag_source_set_target_list (GtkWidget *widget,
2706 GtkTargetList *target_list)
2708 GtkDragSourceSite *site;
2710 g_return_if_fail (GTK_IS_WIDGET (widget));
2712 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2715 g_warning ("gtk_drag_source_set_target_list() requires the widget "
2716 "to already be a drag source.");
2721 gtk_target_list_ref (target_list);
2723 if (site->target_list)
2724 gtk_target_list_unref (site->target_list);
2726 site->target_list = target_list;
2730 * gtk_drag_source_add_text_targets:
2731 * @widget: a #GtkWidget that's is a drag source
2733 * Add the text targets supported by #GtkSelection to
2734 * the target list of the drag source. The targets
2735 * are added with @info = 0. If you need another value,
2736 * use gtk_target_list_add_text_targets() and
2737 * gtk_drag_source_set_target_list().
2742 gtk_drag_source_add_text_targets (GtkWidget *widget)
2744 GtkTargetList *target_list;
2746 target_list = gtk_drag_source_get_target_list (widget);
2748 gtk_target_list_ref (target_list);
2750 target_list = gtk_target_list_new (NULL, 0);
2751 gtk_target_list_add_text_targets (target_list, 0);
2752 gtk_drag_source_set_target_list (widget, target_list);
2753 gtk_target_list_unref (target_list);
2757 * gtk_drag_source_add_image_targets:
2758 * @widget: a #GtkWidget that's is a drag source
2760 * Add the writable image targets supported by #GtkSelection to
2761 * the target list of the drag source. The targets
2762 * are added with @info = 0. If you need another value,
2763 * use gtk_target_list_add_image_targets() and
2764 * gtk_drag_source_set_target_list().
2769 gtk_drag_source_add_image_targets (GtkWidget *widget)
2771 GtkTargetList *target_list;
2773 target_list = gtk_drag_source_get_target_list (widget);
2775 gtk_target_list_ref (target_list);
2777 target_list = gtk_target_list_new (NULL, 0);
2778 gtk_target_list_add_image_targets (target_list, 0, TRUE);
2779 gtk_drag_source_set_target_list (widget, target_list);
2780 gtk_target_list_unref (target_list);
2784 * gtk_drag_source_add_uri_targets:
2785 * @widget: a #GtkWidget that's is a drag source
2787 * Add the URI targets supported by #GtkSelection to
2788 * the target list of the drag source. The targets
2789 * are added with @info = 0. If you need another value,
2790 * use gtk_target_list_add_uri_targets() and
2791 * gtk_drag_source_set_target_list().
2796 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2798 GtkTargetList *target_list;
2800 target_list = gtk_drag_source_get_target_list (widget);
2802 gtk_target_list_ref (target_list);
2804 target_list = gtk_target_list_new (NULL, 0);
2805 gtk_target_list_add_uri_targets (target_list, 0);
2806 gtk_drag_source_set_target_list (widget, target_list);
2807 gtk_target_list_unref (target_list);
2811 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2813 switch (site->icon_type)
2815 case GTK_IMAGE_EMPTY:
2817 case GTK_IMAGE_PIXMAP:
2818 if (site->icon_data.pixmap.pixmap)
2819 g_object_unref (site->icon_data.pixmap.pixmap);
2820 if (site->icon_mask)
2821 g_object_unref (site->icon_mask);
2823 case GTK_IMAGE_PIXBUF:
2824 g_object_unref (site->icon_data.pixbuf.pixbuf);
2826 case GTK_IMAGE_STOCK:
2827 g_free (site->icon_data.stock.stock_id);
2829 case GTK_IMAGE_ICON_NAME:
2830 g_free (site->icon_data.name.icon_name);
2833 g_assert_not_reached();
2836 site->icon_type = GTK_IMAGE_EMPTY;
2839 g_object_unref (site->colormap);
2840 site->colormap = NULL;
2844 * gtk_drag_source_set_icon:
2845 * @widget: a #GtkWidget
2846 * @colormap: the colormap of the icon
2847 * @pixmap: the image data for the icon
2848 * @mask: (allow-none): the transparency mask for an image.
2850 * Sets the icon that will be used for drags from a particular widget
2851 * from a pixmap/mask. GTK+ retains references for the arguments, and
2852 * will release them when they are no longer needed.
2853 * Use gtk_drag_source_set_icon_pixbuf() instead.
2856 gtk_drag_source_set_icon (GtkWidget *widget,
2857 GdkColormap *colormap,
2861 GtkDragSourceSite *site;
2863 g_return_if_fail (GTK_IS_WIDGET (widget));
2864 g_return_if_fail (GDK_IS_COLORMAP (colormap));
2865 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
2866 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
2868 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2869 g_return_if_fail (site != NULL);
2871 g_object_ref (colormap);
2872 g_object_ref (pixmap);
2874 g_object_ref (mask);
2876 gtk_drag_source_unset_icon (site);
2878 site->icon_type = GTK_IMAGE_PIXMAP;
2880 site->icon_data.pixmap.pixmap = pixmap;
2881 site->icon_mask = mask;
2882 site->colormap = colormap;
2886 * gtk_drag_source_set_icon_pixbuf:
2887 * @widget: a #GtkWidget
2888 * @pixbuf: the #GdkPixbuf for the drag icon
2890 * Sets the icon that will be used for drags from a particular widget
2891 * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
2892 * release it when it is no longer needed.
2895 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
2898 GtkDragSourceSite *site;
2900 g_return_if_fail (GTK_IS_WIDGET (widget));
2901 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2903 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2904 g_return_if_fail (site != NULL);
2905 g_object_ref (pixbuf);
2907 gtk_drag_source_unset_icon (site);
2909 site->icon_type = GTK_IMAGE_PIXBUF;
2910 site->icon_data.pixbuf.pixbuf = pixbuf;
2914 * gtk_drag_source_set_icon_stock:
2915 * @widget: a #GtkWidget
2916 * @stock_id: the ID of the stock icon to use
2918 * Sets the icon that will be used for drags from a particular source
2922 gtk_drag_source_set_icon_stock (GtkWidget *widget,
2923 const gchar *stock_id)
2925 GtkDragSourceSite *site;
2927 g_return_if_fail (GTK_IS_WIDGET (widget));
2928 g_return_if_fail (stock_id != NULL);
2930 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2931 g_return_if_fail (site != NULL);
2933 gtk_drag_source_unset_icon (site);
2935 site->icon_type = GTK_IMAGE_STOCK;
2936 site->icon_data.stock.stock_id = g_strdup (stock_id);
2940 * gtk_drag_source_set_icon_name:
2941 * @widget: a #GtkWidget
2942 * @icon_name: name of icon to use
2944 * Sets the icon that will be used for drags from a particular source
2945 * to a themed icon. See the docs for #GtkIconTheme for more details.
2950 gtk_drag_source_set_icon_name (GtkWidget *widget,
2951 const gchar *icon_name)
2953 GtkDragSourceSite *site;
2955 g_return_if_fail (GTK_IS_WIDGET (widget));
2956 g_return_if_fail (icon_name != NULL);
2958 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2959 g_return_if_fail (site != NULL);
2961 gtk_drag_source_unset_icon (site);
2963 site->icon_type = GTK_IMAGE_ICON_NAME;
2964 site->icon_data.name.icon_name = g_strdup (icon_name);
2968 gtk_drag_get_icon (GtkDragSourceInfo *info,
2969 GtkWidget **icon_window,
2973 if (get_can_change_screen (info->icon_window))
2974 gtk_window_set_screen (GTK_WINDOW (info->icon_window),
2977 if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
2979 if (!info->fallback_icon)
2981 gint save_hot_x, save_hot_y;
2982 gboolean save_destroy_icon;
2983 GtkWidget *save_icon_window;
2985 /* HACK to get the appropriate icon
2987 save_icon_window = info->icon_window;
2988 save_hot_x = info->hot_x;
2989 save_hot_y = info->hot_x;
2990 save_destroy_icon = info->destroy_icon;
2992 info->icon_window = NULL;
2993 if (!default_icon_pixmap)
2994 set_icon_stock_pixbuf (info->context,
2995 GTK_STOCK_DND, NULL, -2, -2, TRUE);
2997 gtk_drag_set_icon_pixmap (info->context,
2998 default_icon_colormap,
2999 default_icon_pixmap,
3002 default_icon_hot_y);
3003 info->fallback_icon = info->icon_window;
3005 info->icon_window = save_icon_window;
3006 info->hot_x = save_hot_x;
3007 info->hot_y = save_hot_y;
3008 info->destroy_icon = save_destroy_icon;
3011 gtk_widget_hide (info->icon_window);
3013 *icon_window = info->fallback_icon;
3014 gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
3016 if (!default_icon_pixmap)
3023 *hot_x = default_icon_hot_x;
3024 *hot_y = default_icon_hot_y;
3029 if (info->fallback_icon)
3030 gtk_widget_hide (info->fallback_icon);
3032 *icon_window = info->icon_window;
3033 *hot_x = info->hot_x;
3034 *hot_y = info->hot_y;
3039 gtk_drag_update_icon (GtkDragSourceInfo *info)
3041 if (info->icon_window)
3043 GtkWidget *icon_window;
3046 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
3048 gtk_window_move (GTK_WINDOW (icon_window),
3049 info->cur_x - hot_x,
3050 info->cur_y - hot_y);
3052 if (GTK_WIDGET_VISIBLE (icon_window))
3053 gdk_window_raise (icon_window->window);
3055 gtk_widget_show (icon_window);
3060 gtk_drag_set_icon_window (GdkDragContext *context,
3064 gboolean destroy_on_release)
3066 GtkDragSourceInfo *info;
3068 info = gtk_drag_get_source_info (context, FALSE);
3071 if (destroy_on_release)
3072 gtk_widget_destroy (widget);
3076 gtk_drag_remove_icon (info);
3079 g_object_ref (widget);
3081 info->icon_window = widget;
3082 info->hot_x = hot_x;
3083 info->hot_y = hot_y;
3084 info->destroy_icon = destroy_on_release;
3086 if (widget && info->icon_pixbuf)
3088 g_object_unref (info->icon_pixbuf);
3089 info->icon_pixbuf = NULL;
3092 gtk_drag_update_cursor (info);
3093 gtk_drag_update_icon (info);
3097 * gtk_drag_set_icon_widget:
3098 * @context: the context for a drag. (This must be called
3099 with a context for the source side of a drag)
3100 * @widget: a toplevel window to use as an icon.
3101 * @hot_x: the X offset within @widget of the hotspot.
3102 * @hot_y: the Y offset within @widget of the hotspot.
3104 * Changes the icon for a widget to a given widget. GTK+
3105 * will not destroy the icon, so if you don't want
3106 * it to persist, you should connect to the "drag-end"
3107 * signal and destroy it yourself.
3110 gtk_drag_set_icon_widget (GdkDragContext *context,
3115 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3116 g_return_if_fail (context->is_source);
3117 g_return_if_fail (GTK_IS_WIDGET (widget));
3119 gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
3123 icon_window_realize (GtkWidget *window,
3129 gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
3130 gtk_widget_get_colormap (window),
3131 &pixmap, &mask, 128);
3133 gdk_window_set_back_pixmap (window->window, pixmap, FALSE);
3134 g_object_unref (pixmap);
3138 gtk_widget_shape_combine_mask (window, mask, 0, 0);
3139 g_object_unref (mask);
3144 set_icon_stock_pixbuf (GdkDragContext *context,
3145 const gchar *stock_id,
3149 gboolean force_window)
3154 GdkDisplay *display;
3156 g_return_if_fail (context != NULL);
3157 g_return_if_fail (pixbuf != NULL || stock_id != NULL);
3158 g_return_if_fail (pixbuf == NULL || stock_id == NULL);
3160 screen = gdk_drawable_get_screen (context->source_window);
3162 /* Push a NULL colormap to guard against gtk_widget_push_colormap() */
3163 gtk_widget_push_colormap (NULL);
3164 window = gtk_window_new (GTK_WINDOW_POPUP);
3165 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3166 gtk_window_set_screen (GTK_WINDOW (window), screen);
3167 set_can_change_screen (window, TRUE);
3168 gtk_widget_pop_colormap ();
3170 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3171 gtk_widget_set_app_paintable (window, TRUE);
3175 pixbuf = gtk_widget_render_icon (window, stock_id,
3176 GTK_ICON_SIZE_DND, NULL);
3180 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
3181 gtk_widget_destroy (window);
3187 g_object_ref (pixbuf);
3189 display = gdk_drawable_get_display (context->source_window);
3190 width = gdk_pixbuf_get_width (pixbuf);
3191 height = gdk_pixbuf_get_height (pixbuf);
3193 if (!force_window &&
3194 gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
3196 GtkDragSourceInfo *info;
3198 gtk_widget_destroy (window);
3200 info = gtk_drag_get_source_info (context, FALSE);
3202 if (info->icon_pixbuf)
3203 g_object_unref (info->icon_pixbuf);
3204 info->icon_pixbuf = pixbuf;
3206 gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
3210 gtk_widget_set_size_request (window, width, height);
3212 g_signal_connect_closure (window, "realize",
3213 g_cclosure_new (G_CALLBACK (icon_window_realize),
3215 (GClosureNotify)g_object_unref),
3218 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3223 * gtk_drag_set_icon_pixbuf:
3224 * @context: the context for a drag. (This must be called
3225 * with a context for the source side of a drag)
3226 * @pixbuf: the #GdkPixbuf to use as the drag icon.
3227 * @hot_x: the X offset within @widget of the hotspot.
3228 * @hot_y: the Y offset within @widget of the hotspot.
3230 * Sets @pixbuf as the icon for a given drag.
3233 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
3238 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3239 g_return_if_fail (context->is_source);
3240 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3242 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3246 * gtk_drag_set_icon_stock:
3247 * @context: the context for a drag. (This must be called
3248 * with a context for the source side of a drag)
3249 * @stock_id: the ID of the stock icon to use for the drag.
3250 * @hot_x: the X offset within the icon of the hotspot.
3251 * @hot_y: the Y offset within the icon of the hotspot.
3253 * Sets the icon for a given drag from a stock ID.
3256 gtk_drag_set_icon_stock (GdkDragContext *context,
3257 const gchar *stock_id,
3261 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3262 g_return_if_fail (context->is_source);
3263 g_return_if_fail (stock_id != NULL);
3265 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
3269 * gtk_drag_set_icon_pixmap:
3270 * @context: the context for a drag. (This must be called
3271 * with a context for the source side of a drag)
3272 * @colormap: the colormap of the icon
3273 * @pixmap: the image data for the icon
3274 * @mask: (allow-none): the transparency mask for the icon or %NULL for none.
3275 * @hot_x: the X offset within @pixmap of the hotspot.
3276 * @hot_y: the Y offset within @pixmap of the hotspot.
3278 * Sets @pixmap as the icon for a given drag. GTK+ retains
3279 * references for the arguments, and will release them when
3280 * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
3281 * will be more convenient to use.
3284 gtk_drag_set_icon_pixmap (GdkDragContext *context,
3285 GdkColormap *colormap,
3295 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3296 g_return_if_fail (context->is_source);
3297 g_return_if_fail (GDK_IS_COLORMAP (colormap));
3298 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
3299 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
3301 screen = gdk_colormap_get_screen (colormap);
3303 g_return_if_fail (gdk_drawable_get_screen (pixmap) == screen);
3304 g_return_if_fail (!mask || gdk_drawable_get_screen (mask) == screen);
3306 gdk_drawable_get_size (pixmap, &width, &height);
3308 gtk_widget_push_colormap (colormap);
3310 window = gtk_window_new (GTK_WINDOW_POPUP);
3311 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DND);
3312 gtk_window_set_screen (GTK_WINDOW (window), screen);
3313 set_can_change_screen (window, FALSE);
3314 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
3315 gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
3317 gtk_widget_pop_colormap ();
3319 gtk_widget_set_size_request (window, width, height);
3320 gtk_widget_realize (window);
3322 gdk_window_set_back_pixmap (window->window, pixmap, FALSE);
3325 gtk_widget_shape_combine_mask (window, mask, 0, 0);
3327 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3331 * gtk_drag_set_icon_name:
3332 * @context: the context for a drag. (This must be called
3333 * with a context for the source side of a drag)
3334 * @icon_name: name of icon to use
3335 * @hot_x: the X offset of the hotspot within the icon
3336 * @hot_y: the Y offset of the hotspot within the icon
3338 * Sets the icon for a given drag from a named themed icon. See
3339 * the docs for #GtkIconTheme for more details. Note that the
3340 * size of the icon depends on the icon theme (the icon is
3341 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
3342 * @hot_x and @hot_y have to be used with care.
3347 gtk_drag_set_icon_name (GdkDragContext *context,
3348 const gchar *icon_name,
3353 GtkSettings *settings;
3354 GtkIconTheme *icon_theme;
3356 gint width, height, icon_size;
3358 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3359 g_return_if_fail (context->is_source);
3360 g_return_if_fail (icon_name != NULL);
3362 screen = gdk_drawable_get_screen (context->source_window);
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));
3395 g_return_if_fail (context->is_source);
3397 if (!default_icon_pixmap)
3398 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
3400 gtk_drag_set_icon_pixmap (context,
3401 default_icon_colormap,
3402 default_icon_pixmap,
3405 default_icon_hot_y);
3409 * gtk_drag_set_default_icon:
3410 * @colormap: the colormap of the icon
3411 * @pixmap: the image data for the icon
3412 * @mask: the transparency mask for an image.
3413 * @hot_x: The X offset within @widget of the hotspot.
3414 * @hot_y: The Y offset within @widget of the hotspot.
3416 * Changes the default drag icon. GTK+ retains references for the
3417 * arguments, and will release them when they are no longer needed.
3419 * Deprecated: Change the default drag icon via the stock system by
3420 * changing the stock pixbuf for #GTK_STOCK_DND instead.
3423 gtk_drag_set_default_icon (GdkColormap *colormap,
3429 g_return_if_fail (GDK_IS_COLORMAP (colormap));
3430 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
3431 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
3433 if (default_icon_colormap)
3434 g_object_unref (default_icon_colormap);
3435 if (default_icon_pixmap)
3436 g_object_unref (default_icon_pixmap);
3437 if (default_icon_mask)
3438 g_object_unref (default_icon_mask);
3440 default_icon_colormap = colormap;
3441 g_object_ref (colormap);
3443 default_icon_pixmap = pixmap;
3444 g_object_ref (pixmap);
3446 default_icon_mask = mask;
3448 g_object_ref (mask);
3450 default_icon_hot_x = hot_x;
3451 default_icon_hot_y = hot_y;
3455 /*************************************************************
3456 * _gtk_drag_source_handle_event:
3457 * Called from widget event handling code on Drag events
3461 * toplevel: Toplevel widget that received the event
3464 *************************************************************/
3467 _gtk_drag_source_handle_event (GtkWidget *widget,
3470 GtkDragSourceInfo *info;
3471 GdkDragContext *context;
3473 g_return_if_fail (widget != NULL);
3474 g_return_if_fail (event != NULL);
3476 context = event->dnd.context;
3477 info = gtk_drag_get_source_info (context, FALSE);
3481 switch (event->type)
3483 case GDK_DRAG_STATUS:
3487 if (info->proxy_dest)
3489 if (!event->dnd.send_event)
3491 if (info->proxy_dest->proxy_drop_wait)
3493 gboolean result = context->action != 0;
3495 /* Aha - we can finally pass the MOTIF DROP on... */
3496 gdk_drop_reply (info->proxy_dest->context, result, info->proxy_dest->proxy_drop_time);
3498 gdk_drag_drop (info->context, info->proxy_dest->proxy_drop_time);
3500 gtk_drag_finish (info->proxy_dest->context, FALSE, FALSE, info->proxy_dest->proxy_drop_time);
3504 gdk_drag_status (info->proxy_dest->context,
3505 event->dnd.context->action,
3510 else if (info->have_grab)
3512 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
3513 event->dnd.context->action,
3515 if (info->cursor != cursor)
3517 gdk_pointer_grab (widget->window, FALSE,
3518 GDK_POINTER_MOTION_MASK |
3519 GDK_BUTTON_RELEASE_MASK,
3521 cursor, info->grab_time);
3522 info->cursor = cursor;
3525 gtk_drag_add_update_idle (info);
3530 case GDK_DROP_FINISHED:
3531 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, event->dnd.time);
3534 g_assert_not_reached ();
3538 /*************************************************************
3539 * gtk_drag_source_check_selection:
3540 * Check if we've set up handlers/claimed the selection
3541 * for a given drag. If not, add them.
3545 *************************************************************/
3548 gtk_drag_source_check_selection (GtkDragSourceInfo *info,
3554 tmp_list = info->selections;
3557 if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3559 tmp_list = tmp_list->next;
3562 gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3566 info->selections = g_list_prepend (info->selections,
3567 GUINT_TO_POINTER (selection));
3569 tmp_list = info->target_list->list;
3572 GtkTargetPair *pair = tmp_list->data;
3574 gtk_selection_add_target (info->ipc_widget,
3578 tmp_list = tmp_list->next;
3581 if (info->context->protocol == GDK_DRAG_PROTO_MOTIF)
3583 gtk_selection_add_target (info->ipc_widget,
3585 gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS"),
3586 TARGET_MOTIF_SUCCESS);
3587 gtk_selection_add_target (info->ipc_widget,
3589 gdk_atom_intern_static_string ("XmTRANSFER_FAILURE"),
3590 TARGET_MOTIF_FAILURE);
3593 gtk_selection_add_target (info->ipc_widget,
3595 gdk_atom_intern_static_string ("DELETE"),
3599 /*************************************************************
3600 * gtk_drag_drop_finished:
3601 * Clean up from the drag, and display snapback, if necessary.
3607 *************************************************************/
3610 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3611 GtkDragResult result,
3616 success = (result == GTK_DRAG_RESULT_SUCCESS);
3617 gtk_drag_source_release_selections (info, time);
3619 if (info->proxy_dest)
3621 /* The time from the event isn't reliable for Xdnd drags */
3622 gtk_drag_finish (info->proxy_dest->context, success, FALSE,
3623 info->proxy_dest->proxy_drop_time);
3624 gtk_drag_source_info_destroy (info);
3629 g_signal_emit_by_name (info->widget, "drag-failed",
3630 info->context, result, &success);
3634 gtk_drag_source_info_destroy (info);
3638 GtkDragAnim *anim = g_new (GtkDragAnim, 1);
3642 anim->n_steps = MAX (info->cur_x - info->start_x,
3643 info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3644 anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3646 info->cur_screen = gtk_widget_get_screen (info->widget);
3648 if (!info->icon_window)
3649 set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf,
3652 gtk_drag_update_icon (info);
3654 /* Mark the context as dead, so if the destination decides
3655 * to respond really late, we still are OK.
3657 gtk_drag_clear_source_info (info->context);
3658 gdk_threads_add_timeout (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3664 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3667 GdkDisplay *display = gtk_widget_get_display (info->widget);
3668 GList *tmp_list = info->selections;
3672 GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3673 if (gdk_selection_owner_get_for_display (display, selection) == info->ipc_widget->window)
3674 gtk_selection_owner_set_for_display (display, NULL, selection, time);
3676 tmp_list = tmp_list->next;
3679 g_list_free (info->selections);
3680 info->selections = NULL;
3683 /*************************************************************
3685 * Send a drop event.
3689 *************************************************************/
3692 gtk_drag_drop (GtkDragSourceInfo *info,
3695 if (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN)
3697 GtkSelectionData selection_data;
3699 /* GTK+ traditionally has used application/x-rootwin-drop, but the
3700 * XDND spec specifies x-rootwindow-drop.
3702 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3703 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3705 tmp_list = info->target_list->list;
3708 GtkTargetPair *pair = tmp_list->data;
3710 if (pair->target == target1 || pair->target == target2)
3712 selection_data.selection = GDK_NONE;
3713 selection_data.target = pair->target;
3714 selection_data.data = NULL;
3715 selection_data.length = -1;
3717 g_signal_emit_by_name (info->widget, "drag-data-get",
3718 info->context, &selection_data,
3722 /* FIXME: Should we check for length >= 0 here? */
3723 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3726 tmp_list = tmp_list->next;
3728 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3732 if (info->icon_window)
3733 gtk_widget_hide (info->icon_window);
3735 gdk_drag_drop (info->context, time);
3736 info->drop_timeout = gdk_threads_add_timeout (DROP_ABORT_TIME,
3737 gtk_drag_abort_timeout,
3743 * Source side callbacks.
3747 gtk_drag_source_event_cb (GtkWidget *widget,
3751 GtkDragSourceSite *site;
3752 gboolean retval = FALSE;
3753 site = (GtkDragSourceSite *)data;
3755 switch (event->type)
3757 case GDK_BUTTON_PRESS:
3758 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3760 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3761 site->x = event->button.x;
3762 site->y = event->button.y;
3766 case GDK_BUTTON_RELEASE:
3767 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3768 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3771 case GDK_MOTION_NOTIFY:
3772 if (site->state & event->motion.state & site->start_button_mask)
3774 /* FIXME: This is really broken and can leave us
3780 if (site->state & event->motion.state &
3781 GDK_BUTTON1_MASK << (i - 1))
3785 if (gtk_drag_check_threshold (widget, site->x, site->y,
3786 event->motion.x, event->motion.y))
3789 gtk_drag_begin_internal (widget, site, site->target_list,
3798 default: /* hit for 2/3BUTTON_PRESS */
3806 gtk_drag_source_site_destroy (gpointer data)
3808 GtkDragSourceSite *site = data;
3810 if (site->target_list)
3811 gtk_target_list_unref (site->target_list);
3813 gtk_drag_source_unset_icon (site);
3818 gtk_drag_selection_get (GtkWidget *widget,
3819 GtkSelectionData *selection_data,
3824 GtkDragSourceInfo *info = data;
3825 static GdkAtom null_atom = GDK_NONE;
3829 null_atom = gdk_atom_intern_static_string ("NULL");
3834 g_signal_emit_by_name (info->widget,
3837 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3839 case TARGET_MOTIF_SUCCESS:
3840 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_SUCCESS, time);
3841 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3843 case TARGET_MOTIF_FAILURE:
3844 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_NO_TARGET, time);
3845 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3848 if (info->proxy_dest)
3850 /* This is sort of dangerous and needs to be thought
3853 info->proxy_dest->proxy_data = selection_data;
3854 gtk_drag_get_data (info->widget,
3855 info->proxy_dest->context,
3856 selection_data->target,
3859 info->proxy_dest->proxy_data = NULL;
3863 if (gtk_target_list_find (info->target_list,
3864 selection_data->target,
3867 g_signal_emit_by_name (info->widget, "drag-data-get",
3879 gtk_drag_anim_timeout (gpointer data)
3881 GtkDragAnim *anim = data;
3885 if (anim->step == anim->n_steps)
3887 gtk_drag_source_info_destroy (anim->info);
3894 x = (anim->info->start_x * (anim->step + 1) +
3895 anim->info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3896 y = (anim->info->start_y * (anim->step + 1) +
3897 anim->info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3898 if (anim->info->icon_window)
3900 GtkWidget *icon_window;
3903 gtk_drag_get_icon (anim->info, &icon_window, &hot_x, &hot_y);
3904 gtk_window_move (GTK_WINDOW (icon_window),
3918 gtk_drag_remove_icon (GtkDragSourceInfo *info)
3920 if (info->icon_window)
3922 gtk_widget_hide (info->icon_window);
3923 if (info->destroy_icon)
3924 gtk_widget_destroy (info->icon_window);
3926 if (info->fallback_icon)
3928 gtk_widget_destroy (info->fallback_icon);
3929 info->fallback_icon = NULL;
3932 g_object_unref (info->icon_window);
3933 info->icon_window = NULL;
3938 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
3942 for (i = 0; i < n_drag_cursors; i++)
3944 if (info->drag_cursors[i] != NULL)
3946 gdk_cursor_unref (info->drag_cursors[i]);
3947 info->drag_cursors[i] = NULL;
3951 gtk_drag_remove_icon (info);
3953 if (info->icon_pixbuf)
3955 g_object_unref (info->icon_pixbuf);
3956 info->icon_pixbuf = NULL;
3959 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3960 gtk_drag_grab_broken_event_cb,
3962 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3963 gtk_drag_grab_notify_cb,
3965 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3966 gtk_drag_button_release_cb,
3968 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3971 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3974 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3975 gtk_drag_selection_get,
3978 if (!info->proxy_dest)
3979 g_signal_emit_by_name (info->widget, "drag-end", info->context);
3982 g_object_unref (info->widget);
3984 gtk_selection_remove_all (info->ipc_widget);
3985 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), NULL);
3986 source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
3987 gtk_drag_release_ipc_widget (info->ipc_widget);
3989 gtk_target_list_unref (info->target_list);
3991 gtk_drag_clear_source_info (info->context);
3992 g_object_unref (info->context);
3994 if (info->drop_timeout)
3995 g_source_remove (info->drop_timeout);
3997 if (info->update_idle)
3998 g_source_remove (info->update_idle);
4004 gtk_drag_update_idle (gpointer data)
4006 GtkDragSourceInfo *info = data;
4007 GdkWindow *dest_window;
4008 GdkDragProtocol protocol;
4011 GdkDragAction action;
4012 GdkDragAction possible_actions;
4015 info->update_idle = 0;
4017 if (info->last_event)
4019 time = gtk_drag_get_event_time (info->last_event);
4020 gtk_drag_get_event_actions (info->last_event,
4022 info->possible_actions,
4023 &action, &possible_actions);
4024 gtk_drag_update_icon (info);
4025 gdk_drag_find_window_for_screen (info->context,
4026 info->icon_window ? info->icon_window->window : NULL,
4027 info->cur_screen, info->cur_x, info->cur_y,
4028 &dest_window, &protocol);
4030 if (!gdk_drag_motion (info->context, dest_window, protocol,
4031 info->cur_x, info->cur_y, action,
4035 gdk_event_free ((GdkEvent *)info->last_event);
4036 info->last_event = NULL;
4040 g_object_unref (dest_window);
4042 selection = gdk_drag_get_selection (info->context);
4044 gtk_drag_source_check_selection (info, selection, time);
4052 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
4054 /* We use an idle lower than GDK_PRIORITY_REDRAW so that exposes
4055 * from the last move can catch up before we move again.
4057 if (!info->update_idle)
4058 info->update_idle = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW + 5,
4059 gtk_drag_update_idle,
4066 * @info: DragSourceInfo for the drag
4067 * @screen: new screen
4068 * @x_root: new X position
4069 * @y_root: new y position
4070 * @event: event received requiring update
4072 * Updates the status of the drag; called when the
4073 * cursor moves or the modifier changes
4076 gtk_drag_update (GtkDragSourceInfo *info,
4082 info->cur_screen = screen;
4083 info->cur_x = x_root;
4084 info->cur_y = y_root;
4085 if (info->last_event)
4087 gdk_event_free ((GdkEvent *)info->last_event);
4088 info->last_event = NULL;
4091 info->last_event = gdk_event_copy ((GdkEvent *)event);
4093 gtk_drag_add_update_idle (info);
4096 /*************************************************************
4098 * Called when the user finishes to drag, either by
4099 * releasing the mouse, or by pressing Esc.
4101 * info: Source info for the drag
4102 * time: Timestamp for ending the drag
4104 *************************************************************/
4107 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
4109 GdkEvent *send_event;
4110 GtkWidget *source_widget = info->widget;
4111 GdkDisplay *display = gtk_widget_get_display (source_widget);
4113 if (info->update_idle)
4115 g_source_remove (info->update_idle);
4116 info->update_idle = 0;
4119 if (info->last_event)
4121 gdk_event_free (info->last_event);
4122 info->last_event = NULL;
4125 info->have_grab = FALSE;
4127 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4128 gtk_drag_grab_broken_event_cb,
4130 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4131 gtk_drag_grab_notify_cb,
4133 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4134 gtk_drag_button_release_cb,
4136 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4139 g_signal_handlers_disconnect_by_func (info->ipc_widget,
4143 gdk_display_pointer_ungrab (display, time);
4144 ungrab_dnd_keys (info->ipc_widget, time);
4145 gtk_grab_remove (info->ipc_widget);
4147 /* Send on a release pair to the original
4148 * widget to convince it to release its grab. We need to
4149 * call gtk_propagate_event() here, instead of
4150 * gtk_widget_event() because widget like GtkList may
4151 * expect propagation.
4154 send_event = gdk_event_new (GDK_BUTTON_RELEASE);
4155 send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
4156 send_event->button.send_event = TRUE;
4157 send_event->button.time = time;
4158 send_event->button.x = 0;
4159 send_event->button.y = 0;
4160 send_event->button.axes = NULL;
4161 send_event->button.state = 0;
4162 send_event->button.button = info->button;
4163 send_event->button.device = gdk_display_get_core_pointer (display);
4164 send_event->button.x_root = 0;
4165 send_event->button.y_root = 0;
4167 gtk_propagate_event (source_widget, send_event);
4168 gdk_event_free (send_event);
4171 /*************************************************************
4173 * Called on cancellation of a drag, either by the user
4174 * or programmatically.
4176 * info: Source info for the drag
4177 * time: Timestamp for ending the drag
4179 *************************************************************/
4182 gtk_drag_cancel (GtkDragSourceInfo *info, GtkDragResult result, guint32 time)
4184 gtk_drag_end (info, time);
4185 gdk_drag_abort (info->context, time);
4186 gtk_drag_drop_finished (info, result, time);
4189 /*************************************************************
4190 * gtk_drag_motion_cb:
4191 * "motion-notify-event" callback during drag.
4195 *************************************************************/
4198 gtk_drag_motion_cb (GtkWidget *widget,
4199 GdkEventMotion *event,
4202 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4204 gint x_root, y_root;
4208 GdkDisplay *display = gtk_widget_get_display (widget);
4210 gdk_display_get_pointer (display, &screen, &x_root, &y_root, NULL);
4211 event->x_root = x_root;
4212 event->y_root = y_root;
4215 screen = gdk_event_get_screen ((GdkEvent *)event);
4217 gtk_drag_update (info, screen, event->x_root, event->y_root, (GdkEvent *)event);
4222 /*************************************************************
4224 * "key-press/release-event" callback during drag.
4228 *************************************************************/
4231 #define SMALL_STEP 1
4234 gtk_drag_key_cb (GtkWidget *widget,
4238 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4239 GdkModifierType state;
4240 GdkWindow *root_window;
4244 state = event->state & gtk_accelerator_get_default_mod_mask ();
4246 if (event->type == GDK_KEY_PRESS)
4248 switch (event->keyval)
4251 gtk_drag_cancel (info, GTK_DRAG_RESULT_USER_CANCELLED, event->time);
4259 gtk_drag_end (info, event->time);
4260 gtk_drag_drop (info, event->time);
4265 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4270 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4275 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
4280 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
4286 /* Now send a "motion" so that the modifier state is updated */
4288 /* The state is not yet updated in the event, so we need
4289 * to query it here. We could use XGetModifierMapping, but
4290 * that would be overkill.
4292 root_window = gtk_widget_get_root_window (widget);
4293 gdk_window_get_pointer (root_window, NULL, NULL, &state);
4294 event->state = state;
4296 if (dx != 0 || dy != 0)
4300 gdk_display_warp_pointer (gtk_widget_get_display (widget),
4301 gtk_widget_get_screen (widget),
4302 info->cur_x, info->cur_y);
4305 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
4311 gtk_drag_grab_broken_event_cb (GtkWidget *widget,
4312 GdkEventGrabBroken *event,
4315 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4317 /* Don't cancel if we break the implicit grab from the initial button_press.
4318 * Also, don't cancel if we re-grab on the widget or on our IPC window, for
4319 * example, when changing the drag cursor.
4322 || event->grab_window == info->widget->window
4323 || event->grab_window == info->ipc_widget->window)
4326 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4331 gtk_drag_grab_notify_cb (GtkWidget *widget,
4332 gboolean was_grabbed,
4335 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4339 /* We have to block callbacks to avoid recursion here, because
4340 gtk_drag_cancel calls gtk_grab_remove (via gtk_drag_end) */
4341 g_signal_handlers_block_by_func (widget, gtk_drag_grab_notify_cb, data);
4342 gtk_drag_cancel (info, GTK_DRAG_RESULT_GRAB_BROKEN, gtk_get_current_event_time ());
4343 g_signal_handlers_unblock_by_func (widget, gtk_drag_grab_notify_cb, data);
4348 /*************************************************************
4349 * gtk_drag_button_release_cb:
4350 * "button-release-event" callback during drag.
4354 *************************************************************/
4357 gtk_drag_button_release_cb (GtkWidget *widget,
4358 GdkEventButton *event,
4361 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
4363 if (event->button != info->button)
4366 if ((info->context->action != 0) && (info->context->dest_window != NULL))
4368 gtk_drag_end (info, event->time);
4369 gtk_drag_drop (info, event->time);
4373 gtk_drag_cancel (info, GTK_DRAG_RESULT_NO_TARGET, event->time);
4380 gtk_drag_abort_timeout (gpointer data)
4382 GtkDragSourceInfo *info = data;
4383 guint32 time = GDK_CURRENT_TIME;
4385 if (info->proxy_dest)
4386 time = info->proxy_dest->proxy_drop_time;
4388 info->drop_timeout = 0;
4389 gtk_drag_drop_finished (info, GTK_DRAG_RESULT_TIMEOUT_EXPIRED, time);
4395 * gtk_drag_check_threshold:
4396 * @widget: a #GtkWidget
4397 * @start_x: X coordinate of start of drag
4398 * @start_y: Y coordinate of start of drag
4399 * @current_x: current X coordinate
4400 * @current_y: current Y coordinate
4402 * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
4403 * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
4404 * should trigger the beginning of a drag-and-drop operation.
4406 * Return Value: %TRUE if the drag threshold has been passed.
4409 gtk_drag_check_threshold (GtkWidget *widget,
4415 gint drag_threshold;
4417 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4419 g_object_get (gtk_widget_get_settings (widget),
4420 "gtk-dnd-drag-threshold", &drag_threshold,
4423 return (ABS (current_x - start_x) > drag_threshold ||
4424 ABS (current_y - start_y) > drag_threshold);
4427 #define __GTK_DND_C__
4428 #include "gtkaliasdef.c"