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"
37 #include "gtkiconfactory.h"
38 #include "gtkicontheme.h"
40 #include "gtkinvisible.h"
44 #include "gtkwindow.h"
47 static GSList *source_widgets = NULL;
49 typedef struct _GtkDragSourceSite GtkDragSourceSite;
50 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
51 typedef struct _GtkDragDestSite GtkDragDestSite;
52 typedef struct _GtkDragDestInfo GtkDragDestInfo;
53 typedef struct _GtkDragAnim GtkDragAnim;
54 typedef struct _GtkDragFindData GtkDragFindData;
64 struct _GtkDragSourceSite
66 GdkModifierType start_button_mask;
67 GtkTargetList *target_list; /* Targets for drag data */
68 GdkDragAction actions; /* Possible actions */
71 GtkImageType icon_type;
74 GtkImagePixmapData pixmap;
75 GtkImagePixbufData pixbuf;
76 GtkImageStockData stock;
77 GtkImageIconNameData name;
81 GdkColormap *colormap; /* Colormap for drag icon */
83 /* Stored button press information to detect drag beginning */
88 struct _GtkDragSourceInfo
91 GtkTargetList *target_list; /* Targets for drag data */
92 GdkDragAction possible_actions; /* Actions allowed by source */
93 GdkDragContext *context; /* drag context */
94 GtkWidget *icon_window; /* Window for drag */
95 GtkWidget *fallback_icon; /* Window for drag used on other screens */
96 GtkWidget *ipc_widget; /* GtkInvisible for grab, message passing */
97 GdkCursor *cursor; /* Cursor for drag */
98 gint hot_x, hot_y; /* Hot spot for drag */
99 gint button; /* mouse button starting drag */
101 GtkDragStatus status; /* drag status */
102 GdkEvent *last_event; /* pending event */
104 gint start_x, start_y; /* Initial position */
105 gint cur_x, cur_y; /* Current Position */
106 GdkScreen *cur_screen; /* Current screen for pointer */
108 guint32 grab_time; /* timestamp for initial grab */
109 GList *selections; /* selections we've claimed */
111 GtkDragDestInfo *proxy_dest; /* Set if this is a proxy drag */
113 guint update_idle; /* Idle function to update the drag */
114 guint drop_timeout; /* Timeout for aborting drop */
115 guint destroy_icon : 1; /* If true, destroy icon_window
117 guint have_grab : 1; /* Do we still have the pointer grab
120 GdkPixbuf *icon_pixbuf;
121 GdkCursor *drag_cursors[6];
124 struct _GtkDragDestSite
126 GtkDestDefaults flags;
127 GtkTargetList *target_list;
128 GdkDragAction actions;
129 GdkWindow *proxy_window;
130 GdkDragProtocol proxy_protocol;
132 guint proxy_coords : 1;
136 struct _GtkDragDestInfo
138 GtkWidget *widget; /* Widget in which drag is in */
139 GdkDragContext *context; /* Drag context */
140 GtkDragSourceInfo *proxy_source; /* Set if this is a proxy drag */
141 GtkSelectionData *proxy_data; /* Set while retrieving proxied data */
142 guint dropped : 1; /* Set after we receive a drop */
143 guint32 proxy_drop_time; /* Timestamp for proxied drop */
144 guint proxy_drop_wait : 1; /* Set if we are waiting for a
145 * status reply before sending
148 gint drop_x, drop_y; /* Position of drop */
151 #define DROP_ABORT_TIME 300000
153 #define ANIM_STEP_TIME 50
154 #define ANIM_STEP_LENGTH 50
155 #define ANIM_MIN_STEPS 5
156 #define ANIM_MAX_STEPS 10
160 GtkDragSourceInfo *info;
165 struct _GtkDragFindData
169 GdkDragContext *context;
170 GtkDragDestInfo *info;
173 gboolean (*callback) (GtkWidget *widget, GdkDragContext *context,
174 gint x, gint y, guint32 time);
178 /* Enumeration for some targets we handle internally */
181 TARGET_MOTIF_SUCCESS = 0x40000000,
182 TARGET_MOTIF_FAILURE,
188 static GdkPixmap *default_icon_pixmap = NULL;
189 static GdkPixmap *default_icon_mask = NULL;
190 static GdkColormap *default_icon_colormap = NULL;
191 static gint default_icon_hot_x;
192 static gint default_icon_hot_y;
194 /* Forward declarations */
195 static void gtk_drag_get_event_actions (GdkEvent *event,
197 GdkDragAction actions,
198 GdkDragAction *suggested_action,
199 GdkDragAction *possible_actions);
200 static GdkCursor * gtk_drag_get_cursor (GdkDisplay *display,
201 GdkDragAction action,
202 GtkDragSourceInfo *info);
203 static void gtk_drag_update_cursor (GtkDragSourceInfo *info);
204 static GtkWidget *gtk_drag_get_ipc_widget (GdkScreen *screen);
205 static void gtk_drag_release_ipc_widget (GtkWidget *widget);
207 static gboolean gtk_drag_highlight_expose (GtkWidget *widget,
208 GdkEventExpose *event,
211 static void gtk_drag_selection_received (GtkWidget *widget,
212 GtkSelectionData *selection_data,
215 static void gtk_drag_find_widget (GtkWidget *widget,
216 GtkDragFindData *data);
217 static void gtk_drag_proxy_begin (GtkWidget *widget,
218 GtkDragDestInfo *dest_info,
220 static void gtk_drag_dest_realized (GtkWidget *widget);
221 static void gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
222 GtkWidget *previous_toplevel);
223 static void gtk_drag_dest_site_destroy (gpointer data);
224 static void gtk_drag_dest_leave (GtkWidget *widget,
225 GdkDragContext *context,
227 static gboolean gtk_drag_dest_motion (GtkWidget *widget,
228 GdkDragContext *context,
232 static gboolean gtk_drag_dest_drop (GtkWidget *widget,
233 GdkDragContext *context,
238 static GtkDragDestInfo * gtk_drag_get_dest_info (GdkDragContext *context,
240 static GtkDragSourceInfo *gtk_drag_get_source_info (GdkDragContext *context,
242 static void gtk_drag_clear_source_info (GdkDragContext *context);
244 static void gtk_drag_source_check_selection (GtkDragSourceInfo *info,
247 static void gtk_drag_source_release_selections (GtkDragSourceInfo *info,
249 static void gtk_drag_drop (GtkDragSourceInfo *info,
251 static void gtk_drag_drop_finished (GtkDragSourceInfo *info,
254 static void gtk_drag_cancel (GtkDragSourceInfo *info,
257 static gint gtk_drag_source_event_cb (GtkWidget *widget,
260 static void gtk_drag_source_site_destroy (gpointer data);
261 static void gtk_drag_selection_get (GtkWidget *widget,
262 GtkSelectionData *selection_data,
266 static gint gtk_drag_anim_timeout (gpointer data);
267 static void gtk_drag_remove_icon (GtkDragSourceInfo *info);
268 static void gtk_drag_source_info_destroy (GtkDragSourceInfo *info);
269 static void gtk_drag_add_update_idle (GtkDragSourceInfo *info);
271 static void gtk_drag_update (GtkDragSourceInfo *info,
276 static gint gtk_drag_motion_cb (GtkWidget *widget,
277 GdkEventMotion *event,
279 static gint gtk_drag_key_cb (GtkWidget *widget,
282 static gint gtk_drag_button_release_cb (GtkWidget *widget,
283 GdkEventButton *event,
285 static gint gtk_drag_abort_timeout (gpointer data);
287 /************************
288 * Cursor and Icon data *
289 ************************/
291 #include "gtkdndcursors.h"
294 GdkDragAction action;
300 { GDK_ACTION_DEFAULT, NULL },
301 { GDK_ACTION_ASK, "dnd-ask", dnd_cursor_ask, NULL, NULL },
302 { GDK_ACTION_COPY, "dnd-copy", dnd_cursor_copy, NULL, NULL },
303 { GDK_ACTION_MOVE, "dnd-move", dnd_cursor_move, NULL, NULL },
304 { GDK_ACTION_LINK, "dnd-link", dnd_cursor_link, NULL, NULL },
305 { 0 , "dnd-none", dnd_cursor_none, NULL, NULL },
308 static const gint n_drag_cursors = sizeof (drag_cursors) / sizeof (drag_cursors[0]);
310 /*********************
311 * Utility functions *
312 *********************/
315 set_can_change_screen (GtkWidget *widget,
316 gboolean can_change_screen)
318 can_change_screen = can_change_screen != FALSE;
320 g_object_set_data (G_OBJECT (widget), "gtk-dnd-can-change-screen",
321 GUINT_TO_POINTER (can_change_screen));
325 get_can_change_screen (GtkWidget *widget)
327 return g_object_get_data (G_OBJECT (widget), "gtk-dnd-can-change-screen") != NULL;
331 /*************************************************************
332 * gtk_drag_get_ipc_widget:
333 * Return a invisible, off-screen, override-redirect
338 *************************************************************/
341 gtk_drag_get_ipc_widget (GdkScreen *screen)
344 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
345 "gtk-dnd-ipc-widgets");
349 GSList *tmp = drag_widgets;
350 result = drag_widgets->data;
351 drag_widgets = drag_widgets->next;
352 g_object_set_data (G_OBJECT (screen),
353 "gtk-dnd-ipc-widgets",
355 g_slist_free_1 (tmp);
359 result = gtk_invisible_new_for_screen (screen);
360 gtk_widget_show (result);
366 /***************************************************************
367 * gtk_drag_release_ipc_widget:
368 * Releases widget retrieved with gtk_drag_get_ipc_widget ()
370 * widget: the widget to release.
372 ***************************************************************/
375 gtk_drag_release_ipc_widget (GtkWidget *widget)
377 GdkScreen *screen = gtk_widget_get_screen (widget);
378 GSList *drag_widgets = g_object_get_data (G_OBJECT (screen),
379 "gtk-dnd-ipc-widgets");
380 drag_widgets = g_slist_prepend (drag_widgets, widget);
381 g_object_set_data (G_OBJECT (screen),
382 "gtk-dnd-ipc-widgets",
387 gtk_drag_get_event_time (GdkEvent *event)
389 guint32 tm = GDK_CURRENT_TIME;
394 case GDK_MOTION_NOTIFY:
395 tm = event->motion.time; break;
396 case GDK_BUTTON_PRESS:
397 case GDK_2BUTTON_PRESS:
398 case GDK_3BUTTON_PRESS:
399 case GDK_BUTTON_RELEASE:
400 tm = event->button.time; break;
402 case GDK_KEY_RELEASE:
403 tm = event->key.time; break;
404 case GDK_ENTER_NOTIFY:
405 case GDK_LEAVE_NOTIFY:
406 tm = event->crossing.time; break;
407 case GDK_PROPERTY_NOTIFY:
408 tm = event->property.time; break;
409 case GDK_SELECTION_CLEAR:
410 case GDK_SELECTION_REQUEST:
411 case GDK_SELECTION_NOTIFY:
412 tm = event->selection.time; break;
413 case GDK_PROXIMITY_IN:
414 case GDK_PROXIMITY_OUT:
415 tm = event->proximity.time; break;
416 default: /* use current time */
424 gtk_drag_get_event_actions (GdkEvent *event,
426 GdkDragAction actions,
427 GdkDragAction *suggested_action,
428 GdkDragAction *possible_actions)
430 *suggested_action = 0;
431 *possible_actions = 0;
435 GdkModifierType state = 0;
439 case GDK_MOTION_NOTIFY:
440 state = event->motion.state;
442 case GDK_BUTTON_PRESS:
443 case GDK_2BUTTON_PRESS:
444 case GDK_3BUTTON_PRESS:
445 case GDK_BUTTON_RELEASE:
446 state = event->button.state;
449 case GDK_KEY_RELEASE:
450 state = event->key.state;
452 case GDK_ENTER_NOTIFY:
453 case GDK_LEAVE_NOTIFY:
454 state = event->crossing.state;
460 if ((button == 2 || button == 3) && (actions & GDK_ACTION_ASK))
462 *suggested_action = GDK_ACTION_ASK;
463 *possible_actions = actions;
465 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
467 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
469 if (actions & GDK_ACTION_LINK)
471 *suggested_action = GDK_ACTION_LINK;
472 *possible_actions = GDK_ACTION_LINK;
475 else if (state & GDK_CONTROL_MASK)
477 if (actions & GDK_ACTION_COPY)
479 *suggested_action = GDK_ACTION_COPY;
480 *possible_actions = GDK_ACTION_COPY;
486 if (actions & GDK_ACTION_MOVE)
488 *suggested_action = GDK_ACTION_MOVE;
489 *possible_actions = GDK_ACTION_MOVE;
496 *possible_actions = actions;
498 if ((state & (GDK_MOD1_MASK)) && (actions & GDK_ACTION_ASK))
499 *suggested_action = GDK_ACTION_ASK;
500 else if (actions & GDK_ACTION_COPY)
501 *suggested_action = GDK_ACTION_COPY;
502 else if (actions & GDK_ACTION_MOVE)
503 *suggested_action = GDK_ACTION_MOVE;
504 else if (actions & GDK_ACTION_LINK)
505 *suggested_action = GDK_ACTION_LINK;
510 *possible_actions = actions;
512 if (actions & GDK_ACTION_COPY)
513 *suggested_action = GDK_ACTION_COPY;
514 else if (actions & GDK_ACTION_MOVE)
515 *suggested_action = GDK_ACTION_MOVE;
516 else if (actions & GDK_ACTION_LINK)
517 *suggested_action = GDK_ACTION_LINK;
522 gtk_drag_can_use_rgba_cursor (GdkDisplay *display,
526 guint max_width, max_height;
528 if (!gdk_display_supports_cursor_color (display))
531 if (!gdk_display_supports_cursor_alpha (display))
534 gdk_display_get_maximal_cursor_size (display,
537 if (width > max_width || height > max_height)
539 /* can't use rgba cursor (too large) */
547 gtk_drag_get_cursor (GdkDisplay *display,
548 GdkDragAction action,
549 GtkDragSourceInfo *info)
553 /* reconstruct the cursors for each new drag (thus !info),
554 * to catch cursor theme changes
558 for (i = 0 ; i < n_drag_cursors - 1; i++)
559 if (drag_cursors[i].cursor != NULL)
561 gdk_cursor_unref (drag_cursors[i].cursor);
562 drag_cursors[i].cursor = NULL;
566 for (i = 0 ; i < n_drag_cursors - 1; i++)
567 if (drag_cursors[i].action == action)
570 if (drag_cursors[i].pixbuf == NULL)
571 drag_cursors[i].pixbuf =
572 gdk_pixbuf_new_from_inline (-1, drag_cursors[i].data, FALSE, NULL);
574 if (drag_cursors[i].cursor != NULL)
576 if (display != gdk_cursor_get_display (drag_cursors[i].cursor))
578 gdk_cursor_unref (drag_cursors[i].cursor);
579 drag_cursors[i].cursor = NULL;
583 if (drag_cursors[i].cursor == NULL)
584 drag_cursors[i].cursor = gdk_cursor_new_from_name (display, drag_cursors[i].name);
586 if (drag_cursors[i].cursor == NULL)
587 drag_cursors[i].cursor = gdk_cursor_new_from_pixbuf (display, drag_cursors[i].pixbuf, 0, 0);
589 if (info && info->icon_pixbuf)
591 gint cursor_width, cursor_height;
592 gint icon_width, icon_height;
594 GdkPixbuf *cursor_pixbuf, *pixbuf;
595 GtkAnchorType icon_anchor;
596 gint hot_x = 0, hot_y = 0;
597 gint icon_x, icon_y, ref_x, ref_y;
600 if (info->drag_cursors[i] != NULL)
602 if (display == gdk_cursor_get_display (info->drag_cursors[i]))
603 return info->drag_cursors[i];
605 gdk_cursor_unref (info->drag_cursors[i]);
606 info->drag_cursors[i] = NULL;
609 cursor_pixbuf = gdk_cursor_get_image (drag_cursors[i].cursor);
612 cursor_pixbuf = g_object_ref (drag_cursors[i].pixbuf);
613 icon_anchor = GTK_ANCHOR_NORTH;
614 icon_x = icon_y = -2;
618 if (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"))
619 hot_x = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"));
621 if (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"))
622 hot_y = atoi (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"));
625 for (j = 0; j < 10; j++)
631 g_snprintf (key, 32, "comment%d", j);
632 opt = gdk_pixbuf_get_option (cursor_pixbuf, key);
633 if (opt && g_str_has_prefix ("icon-attach:", opt))
635 toks = g_strsplit (opt + strlen ("icon-attach:"), "'", -1);
636 if (g_strv_length (toks) != 3)
641 icon_anchor = atoi (toks[0]);
642 icon_x = atoi (toks[1]);
643 icon_y = atoi (toks[2]);
652 icon_anchor = GTK_ANCHOR_NORTH_WEST;
653 icon_x = (gint) -2 * gdk_pixbuf_get_width (cursor_pixbuf) / 16.0;
654 icon_y = (gint) -2 * gdk_pixbuf_get_height (cursor_pixbuf) / 16.0;
658 icon_width = gdk_pixbuf_get_width (info->icon_pixbuf);
659 icon_height = gdk_pixbuf_get_height (info->icon_pixbuf);
660 cursor_width = gdk_pixbuf_get_width (cursor_pixbuf);
661 cursor_height = gdk_pixbuf_get_height (cursor_pixbuf);
665 case GTK_ANCHOR_NORTH:
666 case GTK_ANCHOR_CENTER:
667 case GTK_ANCHOR_SOUTH:
668 icon_x += icon_width / 2;
670 case GTK_ANCHOR_NORTH_EAST:
671 case GTK_ANCHOR_EAST:
672 case GTK_ANCHOR_SOUTH_EAST:
673 icon_x += icon_width;
680 case GTK_ANCHOR_WEST:
681 case GTK_ANCHOR_CENTER:
682 case GTK_ANCHOR_EAST:
683 icon_y += icon_height / 2;
685 case GTK_ANCHOR_SOUTH_WEST:
686 case GTK_ANCHOR_SOUTH:
687 case GTK_ANCHOR_SOUTH_EAST:
688 icon_x += icon_height;
693 ref_x = MAX (hot_x, icon_x);
694 ref_y = MAX (hot_y, icon_y);
695 width = ref_x + MAX (cursor_width - hot_x, icon_width - icon_x);
696 height = ref_y + MAX (cursor_height - hot_y, icon_height - icon_y);
698 if (gtk_drag_can_use_rgba_cursor (display, width, height))
700 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
703 gdk_pixbuf_fill (pixbuf, 0xff000000);
705 gdk_pixbuf_composite (info->icon_pixbuf, pixbuf,
706 ref_x - icon_x, ref_y - icon_y,
707 icon_width, icon_height,
708 ref_x - icon_x, ref_y - icon_y,
710 GDK_INTERP_BILINEAR, 255);
712 gdk_pixbuf_composite (cursor_pixbuf, pixbuf,
713 ref_x - hot_x, ref_y - hot_y,
714 cursor_width, cursor_height,
715 ref_x - hot_x, ref_y - hot_y,
717 GDK_INTERP_BILINEAR, 255);
719 info->drag_cursors[i] =
720 gdk_cursor_new_from_pixbuf (display, pixbuf, 0, 0);
722 g_object_unref (pixbuf);
725 g_object_unref (cursor_pixbuf);
727 if (info->drag_cursors[i] != NULL)
728 return info->drag_cursors[i];
731 return drag_cursors[i].cursor;
735 gtk_drag_update_cursor (GtkDragSourceInfo *info)
740 if (!info->have_grab)
743 for (i = 0 ; i < n_drag_cursors - 1; i++)
744 if (info->cursor == drag_cursors[i].cursor ||
745 info->cursor == info->drag_cursors[i])
748 if (i == n_drag_cursors)
751 cursor = gtk_drag_get_cursor (gdk_cursor_get_display (info->cursor),
754 if (cursor != info->cursor)
756 gdk_pointer_grab (info->ipc_widget->window, FALSE,
757 GDK_POINTER_MOTION_MASK |
758 GDK_BUTTON_RELEASE_MASK,
760 cursor, info->grab_time);
761 info->cursor = cursor;
765 /********************
767 ********************/
769 /*************************************************************
771 * Get the data for a drag or drop
773 * context - drag context
774 * target - format to retrieve the data in.
775 * time - timestamp of triggering event.
778 *************************************************************/
781 gtk_drag_get_data (GtkWidget *widget,
782 GdkDragContext *context,
786 GtkWidget *selection_widget;
788 g_return_if_fail (GTK_IS_WIDGET (widget));
789 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
790 g_return_if_fail (!context->is_source);
792 selection_widget = gtk_drag_get_ipc_widget (gtk_widget_get_screen (widget));
794 g_object_ref (context);
795 g_object_ref (widget);
797 g_signal_connect (selection_widget, "selection_received",
798 G_CALLBACK (gtk_drag_selection_received), widget);
800 g_object_set_data (G_OBJECT (selection_widget), "drag-context", context);
802 gtk_selection_convert (selection_widget,
803 gdk_drag_get_selection (context),
809 /*************************************************************
810 * gtk_drag_get_source_widget:
811 * Get the widget the was the source of this drag, if
812 * the drag originated from this application.
814 * context: The drag context for this drag
816 * The source widget, or NULL if the drag originated from
817 * a different application.
818 *************************************************************/
821 gtk_drag_get_source_widget (GdkDragContext *context)
825 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
826 g_return_val_if_fail (!context->is_source, NULL);
828 tmp_list = source_widgets;
831 GtkWidget *ipc_widget = tmp_list->data;
833 if (ipc_widget->window == context->source_window)
835 GtkDragSourceInfo *info;
836 info = g_object_get_data (G_OBJECT (ipc_widget), "gtk-info");
838 return info ? info->widget : NULL;
841 tmp_list = tmp_list->next;
847 /*************************************************************
849 * Notify the drag source that the transfer of data
852 * context: The drag context for this drag
853 * success: Was the data successfully transferred?
854 * time: The timestamp to use when notifying the destination.
856 *************************************************************/
859 gtk_drag_finish (GdkDragContext *context,
864 GdkAtom target = GDK_NONE;
866 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
867 g_return_if_fail (!context->is_source);
871 target = gdk_atom_intern ("DELETE", FALSE);
873 else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
875 target = gdk_atom_intern (success ?
876 "XmTRANSFER_SUCCESS" :
877 "XmTRANSFER_FAILURE",
881 if (target != GDK_NONE)
883 GtkWidget *selection_widget = gtk_drag_get_ipc_widget (gdk_drawable_get_screen (context->source_window));
885 g_object_ref (context);
887 g_object_set_data (G_OBJECT (selection_widget), "drag-context", context);
888 g_signal_connect (selection_widget, "selection_received",
889 G_CALLBACK (gtk_drag_selection_received),
892 gtk_selection_convert (selection_widget,
893 gdk_drag_get_selection (context),
898 if (!(success && del))
899 gdk_drop_finish (context, success, time);
902 /*************************************************************
903 * gtk_drag_highlight_expose:
904 * Callback for expose_event for highlighted widgets.
910 *************************************************************/
913 gtk_drag_highlight_expose (GtkWidget *widget,
914 GdkEventExpose *event,
917 gint x, y, width, height;
919 if (GTK_WIDGET_DRAWABLE (widget))
923 if (GTK_WIDGET_NO_WINDOW (widget))
925 x = widget->allocation.x;
926 y = widget->allocation.y;
927 width = widget->allocation.width;
928 height = widget->allocation.height;
934 gdk_drawable_get_size (widget->window, &width, &height);
937 gtk_paint_shadow (widget->style, widget->window,
938 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
940 x, y, width, height);
942 cr = gdk_cairo_create (widget->window);
943 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
944 cairo_set_line_width (cr, 1.0);
947 width - 1, height - 1);
955 /*************************************************************
956 * gtk_drag_highlight:
957 * Highlight the given widget in the default manner.
961 *************************************************************/
964 gtk_drag_highlight (GtkWidget *widget)
966 g_return_if_fail (GTK_IS_WIDGET (widget));
968 g_signal_connect_after (widget, "expose_event",
969 G_CALLBACK (gtk_drag_highlight_expose),
972 gtk_widget_queue_draw (widget);
975 /*************************************************************
976 * gtk_drag_unhighlight:
977 * Refresh the given widget to remove the highlight.
981 *************************************************************/
984 gtk_drag_unhighlight (GtkWidget *widget)
986 g_return_if_fail (GTK_IS_WIDGET (widget));
988 g_signal_handlers_disconnect_by_func (widget,
989 gtk_drag_highlight_expose,
992 gtk_widget_queue_draw (widget);
996 gtk_drag_dest_set_internal (GtkWidget *widget,
997 GtkDragDestSite *site)
999 GtkDragDestSite *old_site;
1001 g_return_if_fail (widget != NULL);
1003 /* HACK, do this in the destroy */
1004 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1007 g_signal_handlers_disconnect_by_func (widget,
1008 gtk_drag_dest_realized,
1010 g_signal_handlers_disconnect_by_func (widget,
1011 gtk_drag_dest_hierarchy_changed,
1015 if (GTK_WIDGET_REALIZED (widget))
1016 gtk_drag_dest_realized (widget);
1018 g_signal_connect (widget, "realize",
1019 G_CALLBACK (gtk_drag_dest_realized), site);
1020 g_signal_connect (widget, "hierarchy_changed",
1021 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1023 g_object_set_data_full (G_OBJECT (widget), "gtk-drag-dest",
1024 site, gtk_drag_dest_site_destroy);
1028 /*************************************************************
1029 * gtk_drag_dest_set:
1030 * Register a drop site, and possibly add default behaviors.
1033 * flags: Which types of default drag behavior to use
1034 * targets: Table of targets that can be accepted
1035 * n_targets: Number of of entries in targets
1038 *************************************************************/
1041 gtk_drag_dest_set (GtkWidget *widget,
1042 GtkDestDefaults flags,
1043 const GtkTargetEntry *targets,
1045 GdkDragAction actions)
1047 GtkDragDestSite *site;
1049 g_return_if_fail (GTK_IS_WIDGET (widget));
1051 site = g_new (GtkDragDestSite, 1);
1053 site->flags = flags;
1054 site->have_drag = FALSE;
1056 site->target_list = gtk_target_list_new (targets, n_targets);
1058 site->target_list = NULL;
1059 site->actions = actions;
1060 site->do_proxy = FALSE;
1061 site->proxy_window = NULL;
1063 gtk_drag_dest_set_internal (widget, site);
1066 /*************************************************************
1067 * gtk_drag_dest_set_proxy:
1068 * Set up this widget to proxy drags elsewhere.
1071 * proxy_window: window to which forward drag events
1072 * protocol: Drag protocol which the dest widget accepts
1073 * use_coordinates: If true, send the same coordinates to the
1074 * destination, because it is a embedded
1077 *************************************************************/
1080 gtk_drag_dest_set_proxy (GtkWidget *widget,
1081 GdkWindow *proxy_window,
1082 GdkDragProtocol protocol,
1083 gboolean use_coordinates)
1085 GtkDragDestSite *site;
1087 g_return_if_fail (GTK_IS_WIDGET (widget));
1088 g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1090 site = g_new (GtkDragDestSite, 1);
1093 site->have_drag = FALSE;
1094 site->target_list = NULL;
1096 site->proxy_window = proxy_window;
1098 g_object_ref (proxy_window);
1099 site->do_proxy = TRUE;
1100 site->proxy_protocol = protocol;
1101 site->proxy_coords = use_coordinates;
1103 gtk_drag_dest_set_internal (widget, site);
1106 /*************************************************************
1107 * gtk_drag_dest_unset
1108 * Unregister this widget as a drag target.
1112 *************************************************************/
1115 gtk_drag_dest_unset (GtkWidget *widget)
1117 g_return_if_fail (GTK_IS_WIDGET (widget));
1119 g_object_set_data (G_OBJECT (widget), "gtk-drag-dest", NULL);
1123 * gtk_drag_dest_get_target_list:
1124 * @widget: a #GtkWidget
1126 * Returns the list of targets this widget can accept from
1129 * Return value: the #GtkTargetList, or %NULL if none
1132 gtk_drag_dest_get_target_list (GtkWidget *widget)
1134 GtkDragDestSite *site;
1136 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1138 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1140 return site ? site->target_list : NULL;
1144 * gtk_drag_dest_set_target_list:
1145 * @widget: a #GtkWidget that's a drag destination
1146 * @target_list: list of droppable targets, or %NULL for none
1148 * Sets the target types that this widget can accept from drag-and-drop.
1149 * The widget must first be made into a drag destination with
1150 * gtk_drag_dest_set().
1153 gtk_drag_dest_set_target_list (GtkWidget *widget,
1154 GtkTargetList *target_list)
1156 GtkDragDestSite *site;
1158 g_return_if_fail (GTK_IS_WIDGET (widget));
1160 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1164 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1165 "to make the widget into a drag destination");
1170 gtk_target_list_ref (target_list);
1172 if (site->target_list)
1173 gtk_target_list_unref (site->target_list);
1175 site->target_list = target_list;
1179 * gtk_drag_dest_add_text_targets:
1180 * @widget: a #GtkWidget that's a drag destination
1182 * Add the text targets supported by #GtkSelection to
1183 * the target list of the drag destination. The targets
1184 * are added with @info = 0. If you need another value,
1185 * use gtk_target_list_add_text_targets() and
1186 * gtk_drag_dest_set_target_list().
1191 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1193 GtkTargetList *target_list;
1195 target_list = gtk_drag_dest_get_target_list (widget);
1197 gtk_target_list_ref (target_list);
1199 target_list = gtk_target_list_new (NULL, 0);
1200 gtk_target_list_add_text_targets (target_list, 0);
1201 gtk_drag_dest_set_target_list (widget, target_list);
1202 gtk_target_list_unref (target_list);
1206 * gtk_drag_dest_add_image_targets:
1207 * @widget: a #GtkWidget that's a drag destination
1209 * Add the image targets supported by #GtkSelection to
1210 * the target list of the drag destination. The targets
1211 * are added with @info = 0. If you need another value,
1212 * use gtk_target_list_add_image_targets() and
1213 * gtk_drag_dest_set_target_list().
1218 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1220 GtkTargetList *target_list;
1222 target_list = gtk_drag_dest_get_target_list (widget);
1224 gtk_target_list_ref (target_list);
1226 target_list = gtk_target_list_new (NULL, 0);
1227 gtk_target_list_add_image_targets (target_list, 0, FALSE);
1228 gtk_drag_dest_set_target_list (widget, target_list);
1229 gtk_target_list_unref (target_list);
1233 * gtk_drag_dest_add_uri_targets:
1234 * @widget: a #GtkWidget that's a drag destination
1236 * Add the URI targets supported by #GtkSelection to
1237 * the target list of the drag destination. The targets
1238 * are added with @info = 0. If you need another value,
1239 * use gtk_target_list_add_uri_targets() and
1240 * gtk_drag_dest_set_target_list().
1245 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1247 GtkTargetList *target_list;
1249 target_list = gtk_drag_dest_get_target_list (widget);
1251 gtk_target_list_ref (target_list);
1253 target_list = gtk_target_list_new (NULL, 0);
1254 gtk_target_list_add_uri_targets (target_list, 0);
1255 gtk_drag_dest_set_target_list (widget, target_list);
1256 gtk_target_list_unref (target_list);
1259 /*************************************************************
1260 * _gtk_drag_dest_handle_event:
1261 * Called from widget event handling code on Drag events
1265 * toplevel: Toplevel widget that received the event
1268 *************************************************************/
1271 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1274 GtkDragDestInfo *info;
1275 GdkDragContext *context;
1277 g_return_if_fail (toplevel != NULL);
1278 g_return_if_fail (event != NULL);
1280 context = event->dnd.context;
1282 info = gtk_drag_get_dest_info (context, TRUE);
1284 /* Find the widget for the event */
1285 switch (event->type)
1287 case GDK_DRAG_ENTER:
1290 case GDK_DRAG_LEAVE:
1293 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1294 info->widget = NULL;
1298 case GDK_DRAG_MOTION:
1299 case GDK_DROP_START:
1301 GtkDragFindData data;
1304 if (event->type == GDK_DROP_START)
1306 info->dropped = TRUE;
1307 /* We send a leave here so that the widget unhighlights
1312 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1313 info->widget = NULL;
1317 #ifdef GDK_WINDOWING_X11
1318 /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1320 * Currently gdk_window_get_position doesn't provide reliable
1321 * information for embedded windows, so we call the much more
1322 * expensive gdk_window_get_origin().
1324 if (GTK_IS_PLUG (toplevel))
1325 gdk_window_get_origin (toplevel->window, &tx, &ty);
1327 #endif /* GDK_WINDOWING_X11 */
1328 gdk_window_get_position (toplevel->window, &tx, &ty);
1330 data.x = event->dnd.x_root - tx;
1331 data.y = event->dnd.y_root - ty;
1332 data.context = context;
1335 data.toplevel = TRUE;
1336 data.callback = (event->type == GDK_DRAG_MOTION) ?
1337 gtk_drag_dest_motion : gtk_drag_dest_drop;
1338 data.time = event->dnd.time;
1340 gtk_drag_find_widget (toplevel, &data);
1342 if (info->widget && !data.found)
1344 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1345 info->widget = NULL;
1350 if (event->type == GDK_DRAG_MOTION)
1353 gdk_drag_status (context, 0, event->dnd.time);
1355 else if (event->type == GDK_DROP_START && !info->proxy_source)
1357 gdk_drop_reply (context, data.found, event->dnd.time);
1358 if ((context->protocol == GDK_DRAG_PROTO_MOTIF) && !data.found)
1359 gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1365 g_assert_not_reached ();
1370 * gtk_drag_dest_find_target:
1371 * @widget: drag destination widget
1372 * @context: drag context
1373 * @target_list: list of droppable targets, or %NULL to use
1374 * gtk_drag_dest_get_target_list (@widget).
1376 * Looks for a match between @context->targets and the
1377 * @dest_target_list, returning the first matching target, otherwise
1378 * returning %GDK_NONE. @dest_target_list should usually be the return
1379 * value from gtk_drag_dest_get_target_list(), but some widgets may
1380 * have different valid targets for different parts of the widget; in
1381 * that case, they will have to implement a drag_motion handler that
1382 * passes the correct target list to this function.
1384 * Return value: first target that the source offers and the dest can accept, or %GDK_NONE
1387 gtk_drag_dest_find_target (GtkWidget *widget,
1388 GdkDragContext *context,
1389 GtkTargetList *target_list)
1392 GList *tmp_source = NULL;
1393 GtkWidget *source_widget;
1395 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1396 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1397 g_return_val_if_fail (!context->is_source, GDK_NONE);
1400 source_widget = gtk_drag_get_source_widget (context);
1402 if (target_list == NULL)
1403 target_list = gtk_drag_dest_get_target_list (widget);
1405 if (target_list == NULL)
1408 tmp_target = target_list->list;
1411 GtkTargetPair *pair = tmp_target->data;
1412 tmp_source = context->targets;
1415 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1417 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1418 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
1419 return pair->target;
1423 tmp_source = tmp_source->next;
1425 tmp_target = tmp_target->next;
1432 gtk_drag_selection_received (GtkWidget *widget,
1433 GtkSelectionData *selection_data,
1437 GdkDragContext *context;
1438 GtkDragDestInfo *info;
1439 GtkWidget *drop_widget;
1443 context = g_object_get_data (G_OBJECT (widget), "drag-context");
1444 info = gtk_drag_get_dest_info (context, FALSE);
1446 if (info->proxy_data &&
1447 info->proxy_data->target == selection_data->target)
1449 gtk_selection_data_set (info->proxy_data,
1450 selection_data->type,
1451 selection_data->format,
1452 selection_data->data,
1453 selection_data->length);
1458 if (selection_data->target == gdk_atom_intern ("DELETE", FALSE))
1460 gtk_drag_finish (context, TRUE, FALSE, time);
1462 else if ((selection_data->target == gdk_atom_intern ("XmTRANSFER_SUCCESS", FALSE)) ||
1463 (selection_data->target == gdk_atom_intern ("XmTRANSFER_FAILURE", FALSE)))
1469 GtkDragDestSite *site;
1471 site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1473 if (site && site->target_list)
1477 if (gtk_target_list_find (site->target_list,
1478 selection_data->target,
1481 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1482 selection_data->length >= 0)
1483 g_signal_emit_by_name (drop_widget,
1484 "drag_data_received",
1485 context, info->drop_x, info->drop_y,
1492 g_signal_emit_by_name (drop_widget,
1493 "drag_data_received",
1494 context, info->drop_x, info->drop_y,
1499 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1502 gtk_drag_finish (context,
1503 (selection_data->length >= 0),
1504 (context->action == GDK_ACTION_MOVE),
1508 g_object_unref (drop_widget);
1511 g_signal_handlers_disconnect_by_func (widget,
1512 gtk_drag_selection_received,
1515 g_object_set_data (G_OBJECT (widget), "drag-context", NULL);
1516 g_object_unref (context);
1518 gtk_drag_release_ipc_widget (widget);
1522 prepend_and_ref_widget (GtkWidget *widget,
1525 GSList **slist_p = data;
1527 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
1530 /*************************************************************
1531 * gtk_drag_find_widget:
1532 * Recursive callback used to locate widgets for
1533 * DRAG_MOTION and DROP_START events.
1537 *************************************************************/
1540 gtk_drag_find_widget (GtkWidget *widget,
1541 GtkDragFindData *data)
1543 GtkAllocation new_allocation;
1544 gint allocation_to_window_x = 0;
1545 gint allocation_to_window_y = 0;
1549 if (data->found || !GTK_WIDGET_MAPPED (widget) || !GTK_WIDGET_SENSITIVE (widget))
1552 /* Note that in the following code, we only count the
1553 * position as being inside a WINDOW widget if it is inside
1554 * widget->window; points that are outside of widget->window
1555 * but within the allocation are not counted. This is consistent
1556 * with the way we highlight drag targets.
1558 * data->x,y are relative to widget->parent->window (if
1559 * widget is not a toplevel, widget->window otherwise).
1560 * We compute the allocation of widget in the same coordinates,
1561 * clipping to widget->window, and all intermediate
1562 * windows. If data->x,y is inside that, then we translate
1563 * our coordinates to be relative to widget->window and
1566 new_allocation = widget->allocation;
1571 GdkWindow *window = widget->window;
1573 /* Compute the offset from allocation-relative to
1574 * window-relative coordinates.
1576 allocation_to_window_x = widget->allocation.x;
1577 allocation_to_window_y = widget->allocation.y;
1579 if (!GTK_WIDGET_NO_WINDOW (widget))
1581 /* The allocation is relative to the parent window for
1582 * window widgets, not to widget->window.
1584 gdk_window_get_position (window, &tx, &ty);
1586 allocation_to_window_x -= tx;
1587 allocation_to_window_y -= ty;
1590 new_allocation.x = 0 + allocation_to_window_x;
1591 new_allocation.y = 0 + allocation_to_window_y;
1593 while (window && window != widget->parent->window)
1595 GdkRectangle window_rect = { 0, 0, 0, 0 };
1597 gdk_drawable_get_size (window, &window_rect.width, &window_rect.height);
1599 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
1601 gdk_window_get_position (window, &tx, &ty);
1602 new_allocation.x += tx;
1604 new_allocation.y += ty;
1607 window = gdk_window_get_parent (window);
1610 if (!window) /* Window and widget heirarchies didn't match. */
1614 if (data->toplevel ||
1615 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
1616 (data->x < new_allocation.x + new_allocation.width) &&
1617 (data->y < new_allocation.y + new_allocation.height)))
1619 /* First, check if the drag is in a valid drop site in
1620 * one of our children
1622 if (GTK_IS_CONTAINER (widget))
1624 GtkDragFindData new_data = *data;
1625 GSList *children = NULL;
1628 new_data.x -= x_offset;
1629 new_data.y -= y_offset;
1630 new_data.found = FALSE;
1631 new_data.toplevel = FALSE;
1633 /* need to reference children temporarily in case the
1634 * ::drag_motion/::drag_drop callbacks change the widget heirarchy.
1636 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
1637 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
1639 if (!new_data.found && GTK_WIDGET_DRAWABLE (tmp_list->data))
1640 gtk_drag_find_widget (tmp_list->data, &new_data);
1641 g_object_unref (tmp_list->data);
1643 g_slist_free (children);
1645 data->found = new_data.found;
1648 /* If not, and this widget is registered as a drop site, check to
1649 * emit "drag_motion" to check if we are actually in
1653 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1655 data->found = data->callback (widget,
1657 data->x - x_offset - allocation_to_window_x,
1658 data->y - y_offset - allocation_to_window_y,
1660 /* If so, send a "drag_leave" to the last widget */
1663 if (data->info->widget && data->info->widget != widget)
1665 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
1667 data->info->widget = widget;
1674 gtk_drag_proxy_begin (GtkWidget *widget,
1675 GtkDragDestInfo *dest_info,
1678 GtkDragSourceInfo *source_info;
1680 GdkDragContext *context;
1681 GtkWidget *ipc_widget;
1683 if (dest_info->proxy_source)
1685 gdk_drag_abort (dest_info->proxy_source->context, time);
1686 gtk_drag_source_info_destroy (dest_info->proxy_source);
1687 dest_info->proxy_source = NULL;
1690 ipc_widget = gtk_drag_get_ipc_widget (gtk_widget_get_screen (widget));
1691 context = gdk_drag_begin (ipc_widget->window,
1692 dest_info->context->targets);
1694 source_info = gtk_drag_get_source_info (context, TRUE);
1696 source_info->ipc_widget = ipc_widget;
1697 source_info->widget = gtk_widget_ref (widget);
1699 source_info->target_list = gtk_target_list_new (NULL, 0);
1700 tmp_list = dest_info->context->targets;
1703 gtk_target_list_add (source_info->target_list,
1704 GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
1705 tmp_list = tmp_list->next;
1708 source_info->proxy_dest = dest_info;
1710 g_signal_connect (ipc_widget,
1712 G_CALLBACK (gtk_drag_selection_get),
1715 dest_info->proxy_source = source_info;
1719 gtk_drag_dest_info_destroy (gpointer data)
1721 GtkDragDestInfo *info = data;
1726 static GtkDragDestInfo *
1727 gtk_drag_get_dest_info (GdkDragContext *context,
1730 GtkDragDestInfo *info;
1731 static GQuark info_quark = 0;
1733 info_quark = g_quark_from_static_string ("gtk-dest-info");
1735 info = g_object_get_qdata (G_OBJECT (context), info_quark);
1736 if (!info && create)
1738 info = g_new (GtkDragDestInfo, 1);
1739 info->widget = NULL;
1740 info->context = context;
1741 info->proxy_source = NULL;
1742 info->proxy_data = NULL;
1743 info->dropped = FALSE;
1744 info->proxy_drop_wait = FALSE;
1745 g_object_set_qdata_full (G_OBJECT (context), info_quark,
1746 info, gtk_drag_dest_info_destroy);
1752 static GQuark dest_info_quark = 0;
1754 static GtkDragSourceInfo *
1755 gtk_drag_get_source_info (GdkDragContext *context,
1758 GtkDragSourceInfo *info;
1759 if (!dest_info_quark)
1760 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
1762 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
1763 if (!info && create)
1765 info = g_new0 (GtkDragSourceInfo, 1);
1766 info->context = context;
1767 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
1774 gtk_drag_clear_source_info (GdkDragContext *context)
1776 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
1780 gtk_drag_dest_realized (GtkWidget *widget)
1782 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1784 if (GTK_WIDGET_TOPLEVEL (toplevel))
1785 gdk_window_register_dnd (toplevel->window);
1789 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
1790 GtkWidget *previous_toplevel)
1792 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1794 if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_WIDGET_REALIZED (toplevel))
1795 gdk_window_register_dnd (toplevel->window);
1799 gtk_drag_dest_site_destroy (gpointer data)
1801 GtkDragDestSite *site = data;
1803 if (site->proxy_window)
1804 g_object_unref (site->proxy_window);
1806 if (site->target_list)
1807 gtk_target_list_unref (site->target_list);
1813 * Default drag handlers
1816 gtk_drag_dest_leave (GtkWidget *widget,
1817 GdkDragContext *context,
1820 GtkDragDestSite *site;
1822 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1823 g_return_if_fail (site != NULL);
1827 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
1829 if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
1831 gdk_drag_abort (info->proxy_source->context, time);
1832 gtk_drag_source_info_destroy (info->proxy_source);
1833 info->proxy_source = NULL;
1840 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
1841 gtk_drag_unhighlight (widget);
1843 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag)
1844 g_signal_emit_by_name (widget, "drag_leave",
1847 site->have_drag = FALSE;
1852 gtk_drag_dest_motion (GtkWidget *widget,
1853 GdkDragContext *context,
1858 GtkDragDestSite *site;
1859 GdkDragAction action = 0;
1862 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1863 g_return_val_if_fail (site != NULL, FALSE);
1868 GdkEvent *current_event;
1869 GdkWindow *dest_window;
1870 GdkDragProtocol proto;
1872 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
1874 if (!info->proxy_source || info->proxy_source->widget != widget)
1875 gtk_drag_proxy_begin (widget, info, time);
1877 current_event = gtk_get_current_event ();
1879 if (site->proxy_window)
1881 dest_window = site->proxy_window;
1882 proto = site->proxy_protocol;
1886 gdk_drag_find_window_for_screen (info->proxy_source->context,
1888 gdk_drawable_get_screen (current_event->dnd.window),
1889 current_event->dnd.x_root,
1890 current_event->dnd.y_root,
1891 &dest_window, &proto);
1894 gdk_drag_motion (info->proxy_source->context,
1896 current_event->dnd.x_root,
1897 current_event->dnd.y_root,
1898 context->suggested_action,
1899 context->actions, time);
1901 if (!site->proxy_window && dest_window)
1902 g_object_unref (dest_window);
1904 selection = gdk_drag_get_selection (info->proxy_source->context);
1906 selection != gdk_drag_get_selection (info->context))
1907 gtk_drag_source_check_selection (info->proxy_source, selection, time);
1909 gdk_event_free (current_event);
1914 if (site->flags & GTK_DEST_DEFAULT_MOTION)
1916 if (context->suggested_action & site->actions)
1917 action = context->suggested_action;
1924 if ((site->actions & (1 << i)) &&
1925 (context->actions & (1 << i)))
1933 if (action && gtk_drag_dest_find_target (widget, context, NULL))
1935 if (!site->have_drag)
1937 site->have_drag = TRUE;
1938 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
1939 gtk_drag_highlight (widget);
1942 gdk_drag_status (context, action, time);
1946 gdk_drag_status (context, 0, time);
1951 g_signal_emit_by_name (widget, "drag_motion",
1952 context, x, y, time, &retval);
1954 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
1958 gtk_drag_dest_drop (GtkWidget *widget,
1959 GdkDragContext *context,
1964 GtkDragDestSite *site;
1965 GtkDragDestInfo *info;
1967 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1968 g_return_val_if_fail (site != NULL, FALSE);
1970 info = gtk_drag_get_dest_info (context, FALSE);
1971 g_return_val_if_fail (info != NULL, FALSE);
1978 if (info->proxy_source ||
1979 (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN))
1981 gtk_drag_drop (info->proxy_source, time);
1985 /* We need to synthesize a motion event, wait for a status,
1986 * and, if we get a good one, do a drop.
1989 GdkEvent *current_event;
1991 GdkWindow *dest_window;
1992 GdkDragProtocol proto;
1994 gtk_drag_proxy_begin (widget, info, time);
1995 info->proxy_drop_wait = TRUE;
1996 info->proxy_drop_time = time;
1998 current_event = gtk_get_current_event ();
2000 if (site->proxy_window)
2002 dest_window = site->proxy_window;
2003 proto = site->proxy_protocol;
2007 gdk_drag_find_window_for_screen (info->proxy_source->context,
2009 gdk_drawable_get_screen (current_event->dnd.window),
2010 current_event->dnd.x_root,
2011 current_event->dnd.y_root,
2012 &dest_window, &proto);
2015 gdk_drag_motion (info->proxy_source->context,
2017 current_event->dnd.x_root,
2018 current_event->dnd.y_root,
2019 context->suggested_action,
2020 context->actions, time);
2022 if (!site->proxy_window && dest_window)
2023 g_object_unref (dest_window);
2025 selection = gdk_drag_get_selection (info->proxy_source->context);
2027 selection != gdk_drag_get_selection (info->context))
2028 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2030 gdk_event_free (current_event);
2039 if (site->flags & GTK_DEST_DEFAULT_DROP)
2041 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2043 if (target == GDK_NONE)
2045 gtk_drag_finish (context, FALSE, FALSE, time);
2049 gtk_drag_get_data (widget, context, target, time);
2052 g_signal_emit_by_name (widget, "drag_drop",
2053 context, x, y, time, &retval);
2055 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2063 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2064 * so that we can set the icon from the source site information
2066 static GdkDragContext *
2067 gtk_drag_begin_internal (GtkWidget *widget,
2068 GtkDragSourceSite *site,
2069 GtkTargetList *target_list,
2070 GdkDragAction actions,
2074 GtkDragSourceInfo *info;
2075 GList *targets = NULL;
2077 guint32 time = GDK_CURRENT_TIME;
2078 GdkDragAction possible_actions, suggested_action;
2079 GdkDragContext *context;
2080 GtkWidget *ipc_widget;
2083 ipc_widget = gtk_drag_get_ipc_widget (gtk_widget_get_screen (widget));
2085 gtk_drag_get_event_actions (event, button, actions,
2086 &suggested_action, &possible_actions);
2088 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2093 time = gdk_event_get_time (event);
2095 if (gdk_pointer_grab (ipc_widget->window, FALSE,
2096 GDK_POINTER_MOTION_MASK |
2097 GDK_BUTTON_RELEASE_MASK, NULL,
2100 gtk_drag_release_ipc_widget (ipc_widget);
2104 if (gdk_keyboard_grab (ipc_widget->window, FALSE, time) != 0)
2106 gtk_drag_release_ipc_widget (ipc_widget);
2110 /* We use a GTK grab here to override any grabs that the widget
2111 * we are dragging from might have held
2113 gtk_grab_add (ipc_widget);
2115 tmp_list = g_list_last (target_list->list);
2118 GtkTargetPair *pair = tmp_list->data;
2119 targets = g_list_prepend (targets,
2120 GINT_TO_POINTER (pair->target));
2121 tmp_list = tmp_list->prev;
2124 source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2126 context = gdk_drag_begin (ipc_widget->window, targets);
2127 g_list_free (targets);
2129 info = gtk_drag_get_source_info (context, TRUE);
2131 info->ipc_widget = ipc_widget;
2132 g_object_set_data (G_OBJECT (info->ipc_widget), "gtk-info", info);
2134 info->widget = gtk_widget_ref (widget);
2136 info->button = button;
2137 info->cursor = cursor;
2138 info->target_list = target_list;
2139 gtk_target_list_ref (target_list);
2141 info->possible_actions = actions;
2143 info->status = GTK_DRAG_STATUS_DRAG;
2144 info->last_event = NULL;
2145 info->selections = NULL;
2146 info->icon_window = NULL;
2147 info->destroy_icon = FALSE;
2149 /* Set cur_x, cur_y here so if the "drag_begin" signal shows
2150 * the drag icon, it will be in the right place
2152 if (event && event->type == GDK_MOTION_NOTIFY)
2154 info->cur_screen = gtk_widget_get_screen (widget);
2155 info->cur_x = event->motion.x_root;
2156 info->cur_y = event->motion.y_root;
2160 gdk_display_get_pointer (gtk_widget_get_display (widget),
2161 &info->cur_screen, &info->cur_x, &info->cur_y, NULL);
2164 g_signal_emit_by_name (widget, "drag_begin", info->context);
2166 /* Ensure that we have an icon before we start the drag; the
2167 * application may have set one in ::drag_begin, or it may
2170 if (!info->icon_window && !info->icon_pixbuf)
2172 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2173 gtk_drag_set_icon_default (context);
2175 switch (site->icon_type)
2177 case GTK_IMAGE_PIXMAP:
2178 gtk_drag_set_icon_pixmap (context,
2180 site->icon_data.pixmap.pixmap,
2184 case GTK_IMAGE_PIXBUF:
2185 gtk_drag_set_icon_pixbuf (context,
2186 site->icon_data.pixbuf.pixbuf,
2189 case GTK_IMAGE_STOCK:
2190 gtk_drag_set_icon_stock (context,
2191 site->icon_data.stock.stock_id,
2194 case GTK_IMAGE_ICON_NAME:
2195 gtk_drag_set_icon_name (context,
2196 site->icon_data.name.icon_name,
2199 case GTK_IMAGE_EMPTY:
2201 g_assert_not_reached();
2206 /* We need to composite the icon into the cursor, if we are
2207 * not using an icon window.
2209 if (info->icon_pixbuf)
2211 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2215 if (cursor != info->cursor)
2217 gdk_pointer_grab (widget->window, FALSE,
2218 GDK_POINTER_MOTION_MASK |
2219 GDK_BUTTON_RELEASE_MASK,
2222 info->cursor = cursor;
2226 if (event && event->type == GDK_MOTION_NOTIFY)
2227 gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2229 info->start_x = info->cur_x;
2230 info->start_y = info->cur_y;
2232 g_signal_connect (info->ipc_widget, "button_release_event",
2233 G_CALLBACK (gtk_drag_button_release_cb), info);
2234 g_signal_connect (info->ipc_widget, "motion_notify_event",
2235 G_CALLBACK (gtk_drag_motion_cb), info);
2236 g_signal_connect (info->ipc_widget, "key_press_event",
2237 G_CALLBACK (gtk_drag_key_cb), info);
2238 g_signal_connect (info->ipc_widget, "key_release_event",
2239 G_CALLBACK (gtk_drag_key_cb), info);
2240 g_signal_connect (info->ipc_widget, "selection_get",
2241 G_CALLBACK (gtk_drag_selection_get), info);
2243 info->have_grab = TRUE;
2244 info->grab_time = time;
2246 return info->context;
2251 * @widget: the source widget.
2252 * @targets: The targets (data formats) in which the
2253 * source can provide the data.
2254 * @actions: A bitmask of the allowed drag actions for this drag.
2255 * @button: The button the user clicked to start the drag.
2256 * @event: The event that triggered the start of the drag.
2258 * Initiates a drag on the source side. The function
2259 * only needs to be used when the application is
2260 * starting drags itself, and is not needed when
2261 * gtk_drag_source_set() is used.
2263 * Return value: the context for this drag.
2266 gtk_drag_begin (GtkWidget *widget,
2267 GtkTargetList *targets,
2268 GdkDragAction actions,
2272 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2273 g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), NULL);
2274 g_return_val_if_fail (targets != NULL, NULL);
2276 return gtk_drag_begin_internal (widget, NULL, targets,
2277 actions, button, event);
2280 /*************************************************************
2281 * gtk_drag_source_set:
2282 * Register a drop site, and possibly add default behaviors.
2285 * start_button_mask: Mask of allowed buttons to start drag
2286 * targets: Table of targets for this source
2288 * actions: Actions allowed for this source
2290 *************************************************************/
2293 gtk_drag_source_set (GtkWidget *widget,
2294 GdkModifierType start_button_mask,
2295 const GtkTargetEntry *targets,
2297 GdkDragAction actions)
2299 GtkDragSourceSite *site;
2301 g_return_if_fail (GTK_IS_WIDGET (widget));
2303 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2305 gtk_widget_add_events (widget,
2306 gtk_widget_get_events (widget) |
2307 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2308 GDK_BUTTON_MOTION_MASK);
2312 if (site->target_list)
2313 gtk_target_list_unref (site->target_list);
2317 site = g_new0 (GtkDragSourceSite, 1);
2319 site->icon_type = GTK_IMAGE_EMPTY;
2321 g_signal_connect (widget, "button_press_event",
2322 G_CALLBACK (gtk_drag_source_event_cb),
2324 g_signal_connect (widget, "motion_notify_event",
2325 G_CALLBACK (gtk_drag_source_event_cb),
2328 g_object_set_data_full (G_OBJECT (widget),
2330 site, gtk_drag_source_site_destroy);
2333 site->start_button_mask = start_button_mask;
2335 site->target_list = gtk_target_list_new (targets, n_targets);
2337 site->actions = actions;
2340 /*************************************************************
2341 * gtk_drag_source_unset
2342 * Unregister this widget as a drag source.
2346 *************************************************************/
2349 gtk_drag_source_unset (GtkWidget *widget)
2351 GtkDragSourceSite *site;
2353 g_return_if_fail (GTK_IS_WIDGET (widget));
2355 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2359 g_signal_handlers_disconnect_by_func (widget,
2360 gtk_drag_source_event_cb,
2362 g_signal_handlers_disconnect_by_func (widget,
2363 gtk_drag_source_event_cb,
2365 g_object_set_data (G_OBJECT (widget), "gtk-site-data", NULL);
2370 * gtk_drag_source_get_target_list:
2371 * @widget: a #GtkWidget
2373 * Gets the list of targets this widget can provide for
2376 * Return value: the #GtkTargetList, or %NULL if none
2381 gtk_drag_source_get_target_list (GtkWidget *widget)
2383 GtkDragSourceSite *site;
2385 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2387 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2389 return site ? site->target_list : NULL;
2393 * gtk_drag_source_set_target_list:
2394 * @widget: a #GtkWidget that's a drag source
2395 * @target_list: list of draggable targets, or %NULL for none
2397 * Changes the target types that this widget offers for drag-and-drop.
2398 * The widget must first be made into a drag source with
2399 * gtk_drag_source_set().
2404 gtk_drag_source_set_target_list (GtkWidget *widget,
2405 GtkTargetList *target_list)
2407 GtkDragSourceSite *site;
2409 g_return_if_fail (GTK_IS_WIDGET (widget));
2411 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2414 g_warning ("gtk_drag_source_set_target_list() requires the widget "
2415 "to already be a drag source.");
2420 gtk_target_list_ref (target_list);
2422 if (site->target_list)
2423 gtk_target_list_unref (site->target_list);
2425 site->target_list = target_list;
2429 * gtk_drag_source_add_text_targets:
2430 * @widget: a #GtkWidget that's is a drag source
2432 * Add the text targets supported by #GtkSelection to
2433 * the target list of the drag source. The targets
2434 * are added with @info = 0. If you need another value,
2435 * use gtk_target_list_add_text_targets() and
2436 * gtk_drag_source_set_target_list().
2441 gtk_drag_source_add_text_targets (GtkWidget *widget)
2443 GtkTargetList *target_list;
2445 target_list = gtk_drag_source_get_target_list (widget);
2447 gtk_target_list_ref (target_list);
2449 target_list = gtk_target_list_new (NULL, 0);
2450 gtk_target_list_add_text_targets (target_list, 0);
2451 gtk_drag_source_set_target_list (widget, target_list);
2452 gtk_target_list_unref (target_list);
2456 * gtk_drag_source_add_image_targets:
2457 * @widget: a #GtkWidget that's is a drag source
2459 * Add the writable image targets supported by #GtkSelection to
2460 * the target list of the drag source. The targets
2461 * are added with @info = 0. If you need another value,
2462 * use gtk_target_list_add_image_targets() and
2463 * gtk_drag_source_set_target_list().
2468 gtk_drag_source_add_image_targets (GtkWidget *widget)
2470 GtkTargetList *target_list;
2472 target_list = gtk_drag_source_get_target_list (widget);
2474 gtk_target_list_ref (target_list);
2476 target_list = gtk_target_list_new (NULL, 0);
2477 gtk_target_list_add_image_targets (target_list, 0, TRUE);
2478 gtk_drag_source_set_target_list (widget, target_list);
2479 gtk_target_list_unref (target_list);
2483 * gtk_drag_source_add_uri_targets:
2484 * @widget: a #GtkWidget that's is a drag source
2486 * Add the URI targets supported by #GtkSelection to
2487 * the target list of the drag source. The targets
2488 * are added with @info = 0. If you need another value,
2489 * use gtk_target_list_add_uri_targets() and
2490 * gtk_drag_source_set_target_list().
2495 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2497 GtkTargetList *target_list;
2499 target_list = gtk_drag_source_get_target_list (widget);
2501 gtk_target_list_ref (target_list);
2503 target_list = gtk_target_list_new (NULL, 0);
2504 gtk_target_list_add_uri_targets (target_list, 0);
2505 gtk_drag_source_set_target_list (widget, target_list);
2506 gtk_target_list_unref (target_list);
2510 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2512 switch (site->icon_type)
2514 case GTK_IMAGE_EMPTY:
2516 case GTK_IMAGE_PIXMAP:
2517 if (site->icon_data.pixmap.pixmap)
2518 g_object_unref (site->icon_data.pixmap.pixmap);
2519 if (site->icon_mask)
2520 g_object_unref (site->icon_mask);
2522 case GTK_IMAGE_PIXBUF:
2523 g_object_unref (site->icon_data.pixbuf.pixbuf);
2525 case GTK_IMAGE_STOCK:
2526 g_free (site->icon_data.stock.stock_id);
2528 case GTK_IMAGE_ICON_NAME:
2529 g_free (site->icon_data.name.icon_name);
2532 g_assert_not_reached();
2535 site->icon_type = GTK_IMAGE_EMPTY;
2538 g_object_unref (site->colormap);
2539 site->colormap = NULL;
2543 * gtk_drag_source_set_icon:
2544 * @widget: a #GtkWidget
2545 * @colormap: the colormap of the icon
2546 * @pixmap: the image data for the icon
2547 * @mask: the transparency mask for an image.
2549 * Sets the icon that will be used for drags from a particular widget
2550 * from a pixmap/mask. GTK+ retains references for the arguments, and
2551 * will release them when they are no longer needed.
2552 * Use gtk_drag_source_set_icon_pixbuf() instead.
2555 gtk_drag_source_set_icon (GtkWidget *widget,
2556 GdkColormap *colormap,
2560 GtkDragSourceSite *site;
2562 g_return_if_fail (GTK_IS_WIDGET (widget));
2563 g_return_if_fail (GDK_IS_COLORMAP (colormap));
2564 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
2565 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
2567 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2568 g_return_if_fail (site != NULL);
2570 g_object_ref (colormap);
2571 g_object_ref (pixmap);
2573 g_object_ref (mask);
2575 gtk_drag_source_unset_icon (site);
2577 site->icon_type = GTK_IMAGE_PIXMAP;
2579 site->icon_data.pixmap.pixmap = pixmap;
2580 site->icon_mask = mask;
2581 site->colormap = colormap;
2585 * gtk_drag_source_set_icon_pixbuf:
2586 * @widget: a #GtkWidget
2587 * @pixbuf: the #GdkPixbuf for the drag icon
2589 * Sets the icon that will be used for drags from a particular widget
2590 * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
2591 * release it when it is no longer needed.
2594 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
2597 GtkDragSourceSite *site;
2599 g_return_if_fail (GTK_IS_WIDGET (widget));
2600 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2602 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2603 g_return_if_fail (site != NULL);
2604 g_object_ref (pixbuf);
2606 gtk_drag_source_unset_icon (site);
2608 site->icon_type = GTK_IMAGE_PIXBUF;
2609 site->icon_data.pixbuf.pixbuf = pixbuf;
2613 * gtk_drag_source_set_icon_stock:
2614 * @widget: a #GtkWidget
2615 * @stock_id: the ID of the stock icon to use
2617 * Sets the icon that will be used for drags from a particular source
2621 gtk_drag_source_set_icon_stock (GtkWidget *widget,
2622 const gchar *stock_id)
2624 GtkDragSourceSite *site;
2626 g_return_if_fail (GTK_IS_WIDGET (widget));
2627 g_return_if_fail (stock_id != NULL);
2629 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2630 g_return_if_fail (site != NULL);
2632 gtk_drag_source_unset_icon (site);
2634 site->icon_type = GTK_IMAGE_STOCK;
2635 site->icon_data.stock.stock_id = g_strdup (stock_id);
2639 * gtk_drag_source_set_icon_name:
2640 * @widget: a #GtkWidget
2641 * @icon_name: name of icon to use
2643 * Sets the icon that will be used for drags from a particular source
2644 * to a themed icon. See the docs for #GtkIconTheme for more details.
2649 gtk_drag_source_set_icon_name (GtkWidget *widget,
2650 const gchar *icon_name)
2652 GtkDragSourceSite *site;
2654 g_return_if_fail (GTK_IS_WIDGET (widget));
2655 g_return_if_fail (icon_name != NULL);
2657 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2658 g_return_if_fail (site != NULL);
2660 gtk_drag_source_unset_icon (site);
2662 site->icon_type = GTK_IMAGE_ICON_NAME;
2663 site->icon_data.name.icon_name = g_strdup (icon_name);
2667 gtk_drag_get_icon (GtkDragSourceInfo *info,
2668 GtkWidget **icon_window,
2672 if (get_can_change_screen (info->icon_window))
2673 gtk_window_set_screen (GTK_WINDOW (info->icon_window),
2676 if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
2678 if (!info->fallback_icon)
2680 gint save_hot_x, save_hot_y;
2681 gboolean save_destroy_icon;
2682 GtkWidget *save_icon_window;
2684 /* HACK to get the appropriate icon
2686 save_icon_window = info->icon_window;
2687 save_hot_x = info->hot_x;
2688 save_hot_y = info->hot_x;
2689 save_destroy_icon = info->destroy_icon;
2691 info->icon_window = NULL;
2692 gtk_drag_set_icon_default (info->context);
2693 info->fallback_icon = info->icon_window;
2695 info->icon_window = save_icon_window;
2696 info->hot_x = save_hot_x;
2697 info->hot_y = save_hot_y;
2698 info->destroy_icon = save_destroy_icon;
2701 gtk_widget_hide (info->icon_window);
2703 *icon_window = info->fallback_icon;
2704 gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
2706 if (!default_icon_pixmap)
2713 *hot_x = default_icon_hot_x;
2714 *hot_y = default_icon_hot_y;
2719 if (info->fallback_icon)
2720 gtk_widget_hide (info->fallback_icon);
2722 *icon_window = info->icon_window;
2723 *hot_x = info->hot_x;
2724 *hot_y = info->hot_y;
2729 gtk_drag_update_icon (GtkDragSourceInfo *info)
2731 if (info->icon_window)
2733 GtkWidget *icon_window;
2736 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
2738 gtk_window_move (GTK_WINDOW (icon_window),
2739 info->cur_x - hot_x,
2740 info->cur_y - hot_y);
2742 if (GTK_WIDGET_VISIBLE (icon_window))
2743 gdk_window_raise (icon_window->window);
2745 gtk_widget_show (icon_window);
2750 gtk_drag_set_icon_window (GdkDragContext *context,
2754 gboolean destroy_on_release)
2756 GtkDragSourceInfo *info;
2758 info = gtk_drag_get_source_info (context, FALSE);
2759 gtk_drag_remove_icon (info);
2762 gtk_widget_ref (widget);
2764 info->icon_window = widget;
2765 info->hot_x = hot_x;
2766 info->hot_y = hot_y;
2767 info->destroy_icon = destroy_on_release;
2769 if (widget && info->icon_pixbuf)
2771 g_object_unref (info->icon_pixbuf);
2772 info->icon_pixbuf = NULL;
2775 gtk_drag_update_cursor (info);
2776 gtk_drag_update_icon (info);
2780 * gtk_drag_set_icon_widget:
2781 * @context: the context for a drag. (This must be called
2782 with a context for the source side of a drag)
2783 * @widget: a toplevel window to use as an icon.
2784 * @hot_x: the X offset within @widget of the hotspot.
2785 * @hot_y: the Y offset within @widget of the hotspot.
2787 * Changes the icon for a widget to a given widget. GTK+
2788 * will not destroy the icon, so if you don't want
2789 * it to persist, you should connect to the "drag_end"
2790 * signal and destroy it yourself.
2793 gtk_drag_set_icon_widget (GdkDragContext *context,
2798 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
2799 g_return_if_fail (context->is_source);
2800 g_return_if_fail (GTK_IS_WIDGET (widget));
2802 gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
2806 icon_window_realize (GtkWidget *window,
2812 gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
2813 gtk_widget_get_colormap (window),
2814 &pixmap, &mask, 128);
2816 gdk_window_set_back_pixmap (window->window, pixmap, FALSE);
2817 g_object_unref (pixmap);
2821 gtk_widget_shape_combine_mask (window, mask, 0, 0);
2822 g_object_unref (mask);
2827 set_icon_stock_pixbuf (GdkDragContext *context,
2828 const gchar *stock_id,
2832 gboolean force_window)
2837 GdkDisplay *display;
2839 g_return_if_fail (context != NULL);
2840 g_return_if_fail (pixbuf != NULL || stock_id != NULL);
2841 g_return_if_fail (pixbuf == NULL || stock_id == NULL);
2843 screen = gdk_drawable_get_screen (context->source_window);
2845 /* Push a NULL colormap to guard against gtk_widget_push_colormap() */
2846 gtk_widget_push_colormap (NULL);
2847 window = gtk_window_new (GTK_WINDOW_POPUP);
2848 gtk_window_set_screen (GTK_WINDOW (window), screen);
2849 set_can_change_screen (window, TRUE);
2850 gtk_widget_pop_colormap ();
2852 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
2853 gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
2857 pixbuf = gtk_widget_render_icon (window, stock_id,
2858 GTK_ICON_SIZE_DND, NULL);
2862 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
2863 gtk_widget_destroy (window);
2869 g_object_ref (pixbuf);
2871 display = gdk_drawable_get_display (context->source_window);
2872 width = gdk_pixbuf_get_width (pixbuf);
2873 height = gdk_pixbuf_get_width (pixbuf);
2875 if (!force_window &&
2876 gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
2878 GtkDragSourceInfo *info;
2880 gtk_widget_destroy (window);
2882 info = gtk_drag_get_source_info (context, FALSE);
2884 if (info->icon_pixbuf)
2885 g_object_unref (info->icon_pixbuf);
2886 info->icon_pixbuf = pixbuf;
2888 gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
2892 gtk_widget_set_size_request (window, width, height);
2894 g_signal_connect_closure (window, "realize",
2895 g_cclosure_new (G_CALLBACK (icon_window_realize),
2897 (GClosureNotify)g_object_unref),
2900 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
2905 * gtk_drag_set_icon_pixbuf:
2906 * @context: the context for a drag. (This must be called
2907 * with a context for the source side of a drag)
2908 * @pixbuf: the #GdkPixbuf to use as the drag icon.
2909 * @hot_x: the X offset within @widget of the hotspot.
2910 * @hot_y: the Y offset within @widget of the hotspot.
2912 * Sets @pixbuf as the icon for a given drag.
2915 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
2920 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
2921 g_return_if_fail (context->is_source);
2922 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2924 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
2928 * gtk_drag_set_icon_stock:
2929 * @context: the context for a drag. (This must be called
2930 * with a context for the source side of a drag)
2931 * @stock_id: the ID of the stock icon to use for the drag.
2932 * @hot_x: the X offset within the icon of the hotspot.
2933 * @hot_y: the Y offset within the icon of the hotspot.
2935 * Sets the icon for a given drag from a stock ID.
2938 gtk_drag_set_icon_stock (GdkDragContext *context,
2939 const gchar *stock_id,
2943 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
2944 g_return_if_fail (context->is_source);
2945 g_return_if_fail (stock_id != NULL);
2947 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
2951 * gtk_drag_set_icon_pixmap:
2952 * @context: the context for a drag. (This must be called
2953 * with a context for the source side of a drag)
2954 * @colormap: the colormap of the icon
2955 * @pixmap: the image data for the icon
2956 * @mask: the transparency mask for the icon
2957 * @hot_x: the X offset within @pixmap of the hotspot.
2958 * @hot_y: the Y offset within @pixmap of the hotspot.
2960 * Sets @pixmap as the icon for a given drag. GTK+ retains
2961 * references for the arguments, and will release them when
2962 * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
2963 * will be more convenient to use.
2966 gtk_drag_set_icon_pixmap (GdkDragContext *context,
2967 GdkColormap *colormap,
2977 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
2978 g_return_if_fail (context->is_source);
2979 g_return_if_fail (GDK_IS_COLORMAP (colormap));
2980 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
2981 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
2983 screen = gdk_colormap_get_screen (colormap);
2985 g_return_if_fail (gdk_drawable_get_screen (pixmap) == screen);
2986 g_return_if_fail (!mask || gdk_drawable_get_screen (mask) == screen);
2988 gdk_drawable_get_size (pixmap, &width, &height);
2990 gtk_widget_push_colormap (colormap);
2992 window = gtk_window_new (GTK_WINDOW_POPUP);
2993 gtk_window_set_screen (GTK_WINDOW (window), screen);
2994 set_can_change_screen (window, FALSE);
2995 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
2996 gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
2998 gtk_widget_pop_colormap ();
3000 gtk_widget_set_size_request (window, width, height);
3001 gtk_widget_realize (window);
3003 gdk_window_set_back_pixmap (window->window, pixmap, FALSE);
3006 gtk_widget_shape_combine_mask (window, mask, 0, 0);
3008 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3012 * gtk_drag_set_icon_name:
3013 * @context: the context for a drag. (This must be called
3014 * with a context for the source side of a drag)
3015 * @icon_name: name of icon to use
3016 * @hot_x: the X offset of the hotspot within the icon
3017 * @hot_y: the Y offset of the hotspot within the icon
3019 * Sets the icon for the window from a named themed icon. See
3020 * the docs for #GtkIconTheme for more details. Note that the
3021 * size of the icon depends on the icon theme (the icon is
3022 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
3023 * @hot_x and @hot_y have to be used with care.
3028 gtk_drag_set_icon_name (GdkDragContext *context,
3029 const gchar *icon_name,
3034 GtkSettings *settings;
3035 GtkIconTheme *icon_theme;
3037 gint width, height, icon_size;
3039 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3040 g_return_if_fail (context->is_source);
3041 g_return_if_fail (icon_name != NULL);
3043 screen = gdk_drawable_get_screen (context->source_window);
3044 g_return_if_fail (screen != NULL);
3046 settings = gtk_settings_get_for_screen (screen);
3047 if (gtk_icon_size_lookup_for_settings (settings,
3050 icon_size = MAX (width, height);
3052 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
3054 icon_theme = gtk_icon_theme_get_for_screen (screen);
3056 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
3057 icon_size, 0, NULL);
3058 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3062 * gtk_drag_set_icon_default:
3063 * @context: the context for a drag. (This must be called
3064 with a context for the source side of a drag)
3066 * Sets the icon for a particular drag to the default
3070 gtk_drag_set_icon_default (GdkDragContext *context)
3072 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3073 g_return_if_fail (context->is_source);
3075 if (!default_icon_pixmap)
3076 gtk_drag_set_icon_stock (context, GTK_STOCK_DND, -2, -2);
3078 gtk_drag_set_icon_pixmap (context,
3079 default_icon_colormap,
3080 default_icon_pixmap,
3083 default_icon_hot_y);
3087 * gtk_drag_set_default_icon:
3088 * @colormap: the colormap of the icon
3089 * @pixmap: the image data for the icon
3090 * @mask: the transparency mask for an image.
3091 * @hot_x: The X offset within @widget of the hotspot.
3092 * @hot_y: The Y offset within @widget of the hotspot.
3094 * Changes the default drag icon. GTK+ retains references for the
3095 * arguments, and will release them when they are no longer needed.
3096 * This function is obsolete. The default icon should now be changed
3097 * via the stock system by changing the stock pixbuf for #GTK_STOCK_DND.
3100 gtk_drag_set_default_icon (GdkColormap *colormap,
3106 g_return_if_fail (GDK_IS_COLORMAP (colormap));
3107 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
3108 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
3110 if (default_icon_colormap)
3111 g_object_unref (default_icon_colormap);
3112 if (default_icon_pixmap)
3113 g_object_unref (default_icon_pixmap);
3114 if (default_icon_mask)
3115 g_object_unref (default_icon_mask);
3117 default_icon_colormap = colormap;
3118 g_object_ref (colormap);
3120 default_icon_pixmap = pixmap;
3121 g_object_ref (pixmap);
3123 default_icon_mask = mask;
3125 g_object_ref (mask);
3127 default_icon_hot_x = hot_x;
3128 default_icon_hot_y = hot_y;
3132 /*************************************************************
3133 * _gtk_drag_source_handle_event:
3134 * Called from widget event handling code on Drag events
3138 * toplevel: Toplevel widget that received the event
3141 *************************************************************/
3144 _gtk_drag_source_handle_event (GtkWidget *widget,
3147 GtkDragSourceInfo *info;
3148 GdkDragContext *context;
3150 g_return_if_fail (widget != NULL);
3151 g_return_if_fail (event != NULL);
3153 context = event->dnd.context;
3154 info = gtk_drag_get_source_info (context, FALSE);
3158 switch (event->type)
3160 case GDK_DRAG_STATUS:
3164 if (info->proxy_dest)
3166 if (!event->dnd.send_event)
3168 if (info->proxy_dest->proxy_drop_wait)
3170 gboolean result = context->action != 0;
3172 /* Aha - we can finally pass the MOTIF DROP on... */
3173 gdk_drop_reply (info->proxy_dest->context, result, info->proxy_dest->proxy_drop_time);
3175 gdk_drag_drop (info->context, info->proxy_dest->proxy_drop_time);
3177 gtk_drag_finish (info->proxy_dest->context, FALSE, FALSE, info->proxy_dest->proxy_drop_time);
3181 gdk_drag_status (info->proxy_dest->context,
3182 event->dnd.context->action,
3187 else if (info->have_grab)
3189 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
3190 event->dnd.context->action,
3192 if (info->cursor != cursor)
3194 gdk_pointer_grab (widget->window, FALSE,
3195 GDK_POINTER_MOTION_MASK |
3196 GDK_BUTTON_RELEASE_MASK,
3198 cursor, info->grab_time);
3199 info->cursor = cursor;
3202 if (info->last_event)
3203 gtk_drag_add_update_idle (info);
3208 case GDK_DROP_FINISHED:
3209 gtk_drag_drop_finished (info, TRUE, event->dnd.time);
3212 g_assert_not_reached ();
3216 /*************************************************************
3217 * gtk_drag_source_check_selection:
3218 * Check if we've set up handlers/claimed the selection
3219 * for a given drag. If not, add them.
3223 *************************************************************/
3226 gtk_drag_source_check_selection (GtkDragSourceInfo *info,
3232 tmp_list = info->selections;
3235 if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3237 tmp_list = tmp_list->next;
3240 gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3244 info->selections = g_list_prepend (info->selections,
3245 GUINT_TO_POINTER (selection));
3247 tmp_list = info->target_list->list;
3250 GtkTargetPair *pair = tmp_list->data;
3252 gtk_selection_add_target (info->ipc_widget,
3256 tmp_list = tmp_list->next;
3259 if (info->context->protocol == GDK_DRAG_PROTO_MOTIF)
3261 gtk_selection_add_target (info->ipc_widget,
3263 gdk_atom_intern ("XmTRANSFER_SUCCESS", FALSE),
3264 TARGET_MOTIF_SUCCESS);
3265 gtk_selection_add_target (info->ipc_widget,
3267 gdk_atom_intern ("XmTRANSFER_FAILURE", FALSE),
3268 TARGET_MOTIF_FAILURE);
3271 gtk_selection_add_target (info->ipc_widget,
3273 gdk_atom_intern ("DELETE", FALSE),
3277 /*************************************************************
3278 * gtk_drag_drop_finished:
3279 * Clean up from the drag, and display snapback, if necessary.
3285 *************************************************************/
3288 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3292 gtk_drag_source_release_selections (info, time);
3294 if (info->proxy_dest)
3296 /* The time from the event isn't reliable for Xdnd drags */
3297 gtk_drag_finish (info->proxy_dest->context, success, FALSE,
3298 info->proxy_dest->proxy_drop_time);
3299 gtk_drag_source_info_destroy (info);
3305 gtk_drag_source_info_destroy (info);
3309 GtkDragAnim *anim = g_new (GtkDragAnim, 1);
3313 anim->n_steps = MAX (info->cur_x - info->start_x,
3314 info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3315 anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3317 info->cur_screen = gtk_widget_get_screen (info->widget);
3319 if (!info->icon_window)
3320 set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf,
3323 gtk_drag_update_icon (info);
3325 /* Mark the context as dead, so if the destination decides
3326 * to respond really late, we still are OK.
3328 gtk_drag_clear_source_info (info->context);
3329 g_timeout_add (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3335 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3338 GdkDisplay *display = gtk_widget_get_display (info->widget);
3339 GList *tmp_list = info->selections;
3343 GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3344 if (gdk_selection_owner_get_for_display (display, selection) == info->ipc_widget->window)
3345 gtk_selection_owner_set_for_display (display, NULL, selection, time);
3347 tmp_list = tmp_list->next;
3350 g_list_free (info->selections);
3351 info->selections = NULL;
3354 /*************************************************************
3356 * Send a drop event.
3360 *************************************************************/
3363 gtk_drag_drop (GtkDragSourceInfo *info,
3366 if (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN)
3368 GtkSelectionData selection_data;
3370 /* GTK+ traditionally has used application/x-rootwin-drop, but the
3371 * XDND spec specifies x-rootwindow-drop.
3373 GdkAtom target1 = gdk_atom_intern ("application/x-rootwindow-drop", FALSE);
3374 GdkAtom target2 = gdk_atom_intern ("application/x-rootwin-drop", FALSE);
3376 tmp_list = info->target_list->list;
3379 GtkTargetPair *pair = tmp_list->data;
3381 if (pair->target == target1 || pair->target == target2)
3383 selection_data.selection = GDK_NONE;
3384 selection_data.target = pair->target;
3385 selection_data.data = NULL;
3386 selection_data.length = -1;
3388 g_signal_emit_by_name (info->widget, "drag_data_get",
3389 info->context, &selection_data,
3393 /* FIXME: Should we check for length >= 0 here? */
3394 gtk_drag_drop_finished (info, TRUE, time);
3397 tmp_list = tmp_list->next;
3399 gtk_drag_drop_finished (info, FALSE, time);
3403 if (info->icon_window)
3404 gtk_widget_hide (info->icon_window);
3406 gdk_drag_drop (info->context, time);
3407 info->drop_timeout = g_timeout_add (DROP_ABORT_TIME,
3408 gtk_drag_abort_timeout,
3414 * Source side callbacks.
3418 gtk_drag_source_event_cb (GtkWidget *widget,
3422 GtkDragSourceSite *site;
3423 gboolean retval = FALSE;
3424 site = (GtkDragSourceSite *)data;
3426 switch (event->type)
3428 case GDK_BUTTON_PRESS:
3429 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3431 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3432 site->x = event->button.x;
3433 site->y = event->button.y;
3437 case GDK_BUTTON_RELEASE:
3438 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3439 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3442 case GDK_MOTION_NOTIFY:
3443 if (site->state & event->motion.state & site->start_button_mask)
3445 /* FIXME: This is really broken and can leave us
3451 if (site->state & event->motion.state &
3452 GDK_BUTTON1_MASK << (i - 1))
3456 if (gtk_drag_check_threshold (widget, site->x, site->y,
3457 event->motion.x, event->motion.y))
3459 GdkDragContext *context;
3462 context = gtk_drag_begin_internal (widget, site, site->target_list,
3471 default: /* hit for 2/3BUTTON_PRESS */
3479 gtk_drag_source_site_destroy (gpointer data)
3481 GtkDragSourceSite *site = data;
3483 if (site->target_list)
3484 gtk_target_list_unref (site->target_list);
3486 gtk_drag_source_unset_icon (site);
3491 gtk_drag_selection_get (GtkWidget *widget,
3492 GtkSelectionData *selection_data,
3497 GtkDragSourceInfo *info = data;
3498 static GdkAtom null_atom = GDK_NONE;
3502 null_atom = gdk_atom_intern ("NULL", FALSE);
3507 g_signal_emit_by_name (info->widget,
3510 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3512 case TARGET_MOTIF_SUCCESS:
3513 gtk_drag_drop_finished (info, TRUE, time);
3514 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3516 case TARGET_MOTIF_FAILURE:
3517 gtk_drag_drop_finished (info, FALSE, time);
3518 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3521 if (info->proxy_dest)
3523 /* This is sort of dangerous and needs to be thought
3526 info->proxy_dest->proxy_data = selection_data;
3527 gtk_drag_get_data (info->widget,
3528 info->proxy_dest->context,
3529 selection_data->target,
3532 info->proxy_dest->proxy_data = NULL;
3536 if (gtk_target_list_find (info->target_list,
3537 selection_data->target,
3540 g_signal_emit_by_name (info->widget, "drag_data_get",
3552 gtk_drag_anim_timeout (gpointer data)
3554 GtkDragAnim *anim = data;
3558 GDK_THREADS_ENTER ();
3560 if (anim->step == anim->n_steps)
3562 gtk_drag_source_info_destroy (anim->info);
3569 x = (anim->info->start_x * (anim->step + 1) +
3570 anim->info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3571 y = (anim->info->start_y * (anim->step + 1) +
3572 anim->info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3573 if (anim->info->icon_window)
3575 GtkWidget *icon_window;
3578 gtk_drag_get_icon (anim->info, &icon_window, &hot_x, &hot_y);
3579 gtk_window_move (GTK_WINDOW (icon_window),
3589 GDK_THREADS_LEAVE ();
3595 gtk_drag_remove_icon (GtkDragSourceInfo *info)
3597 if (info->icon_window)
3599 gtk_widget_hide (info->icon_window);
3600 if (info->destroy_icon)
3601 gtk_widget_destroy (info->icon_window);
3603 if (info->fallback_icon)
3605 gtk_widget_destroy (info->fallback_icon);
3606 info->fallback_icon = NULL;
3609 g_object_unref (info->icon_window);
3610 info->icon_window = NULL;
3615 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
3619 for (i = 0; i < n_drag_cursors; i++)
3621 if (info->drag_cursors[i] != NULL)
3623 gdk_cursor_unref (info->drag_cursors[i]);
3624 info->drag_cursors[i] = NULL;
3628 gtk_drag_remove_icon (info);
3630 if (info->icon_pixbuf)
3631 g_object_unref (info->icon_pixbuf);
3633 if (!info->proxy_dest)
3634 g_signal_emit_by_name (info->widget, "drag_end",
3638 g_object_unref (info->widget);
3641 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3642 gtk_drag_button_release_cb,
3644 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3647 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3650 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3651 gtk_drag_selection_get,
3654 gtk_selection_remove_all (info->ipc_widget);
3655 g_object_set_data (G_OBJECT (info->ipc_widget), "gtk-info", NULL);
3656 source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
3657 gtk_drag_release_ipc_widget (info->ipc_widget);
3659 gtk_target_list_unref (info->target_list);
3661 gtk_drag_clear_source_info (info->context);
3662 g_object_unref (info->context);
3664 if (info->drop_timeout)
3665 g_source_remove (info->drop_timeout);
3671 gtk_drag_update_idle (gpointer data)
3673 GtkDragSourceInfo *info = data;
3674 GdkWindow *dest_window;
3675 GdkDragProtocol protocol;
3678 GdkDragAction action;
3679 GdkDragAction possible_actions;
3682 GDK_THREADS_ENTER ();
3684 info->update_idle = 0;
3686 time = gtk_drag_get_event_time (info->last_event);
3687 gtk_drag_get_event_actions (info->last_event,
3689 info->possible_actions,
3690 &action, &possible_actions);
3691 gtk_drag_update_icon (info);
3692 gdk_drag_find_window_for_screen (info->context,
3693 info->icon_window ? info->icon_window->window : NULL,
3694 info->cur_screen, info->cur_x, info->cur_y,
3695 &dest_window, &protocol);
3697 if (!gdk_drag_motion (info->context, dest_window, protocol,
3698 info->cur_x, info->cur_y, action,
3702 gdk_event_free ((GdkEvent *)info->last_event);
3703 info->last_event = NULL;
3707 g_object_unref (dest_window);
3709 selection = gdk_drag_get_selection (info->context);
3711 gtk_drag_source_check_selection (info, selection, time);
3713 GDK_THREADS_LEAVE ();
3719 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
3721 /* We use an idle lowerthan GDK_PRIORITY_REDRAW so that exposes
3722 * from the last move can catch up before we move again.
3724 if (!info->update_idle)
3725 info->update_idle = g_idle_add_full (GDK_PRIORITY_REDRAW + 5,
3726 gtk_drag_update_idle,
3733 * @info: DragSourceInfo for the drag
3734 * @screen: new screen
3735 * @x_root: new X position
3736 * @y_root: new y position
3737 * @event: event received requiring update
3739 * Updates the status of the drag; called when the
3740 * cursor moves or the modifier changes
3743 gtk_drag_update (GtkDragSourceInfo *info,
3749 info->cur_screen = screen;
3750 info->cur_x = x_root;
3751 info->cur_y = y_root;
3752 if (info->last_event)
3753 gdk_event_free ((GdkEvent *)info->last_event);
3754 info->last_event = gdk_event_copy ((GdkEvent *)event);
3756 gtk_drag_add_update_idle (info);
3759 /*************************************************************
3761 * Called when the user finishes to drag, either by
3762 * releasing the mouse, or by pressing Esc.
3764 * info: Source info for the drag
3765 * time: Timestamp for ending the drag
3767 *************************************************************/
3770 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
3772 GdkEvent *send_event;
3773 GtkWidget *source_widget = info->widget;
3774 GdkDisplay *display = gtk_widget_get_display (source_widget);
3776 if (info->update_idle)
3778 g_source_remove (info->update_idle);
3779 info->update_idle = 0;
3782 if (info->last_event)
3784 gdk_event_free (info->last_event);
3785 info->last_event = NULL;
3788 info->have_grab = FALSE;
3790 gdk_display_pointer_ungrab (display, time);
3791 gdk_display_keyboard_ungrab (display, time);
3792 gtk_grab_remove (info->ipc_widget);
3794 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3795 gtk_drag_button_release_cb,
3797 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3800 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3804 /* Send on a release pair to the original
3805 * widget to convince it to release its grab. We need to
3806 * call gtk_propagate_event() here, instead of
3807 * gtk_widget_event() because widget like GtkList may
3808 * expect propagation.
3811 send_event = gdk_event_new (GDK_BUTTON_RELEASE);
3812 send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
3813 send_event->button.send_event = TRUE;
3814 send_event->button.time = time;
3815 send_event->button.x = 0;
3816 send_event->button.y = 0;
3817 send_event->button.axes = NULL;
3818 send_event->button.state = 0;
3819 send_event->button.button = info->button;
3820 send_event->button.device = gdk_display_get_core_pointer (display);
3821 send_event->button.x_root = 0;
3822 send_event->button.y_root = 0;
3824 gtk_propagate_event (source_widget, send_event);
3825 gdk_event_free (send_event);
3828 /*************************************************************
3830 * Called on cancellation of a drag, either by the user
3831 * or programmatically.
3833 * info: Source info for the drag
3834 * time: Timestamp for ending the drag
3836 *************************************************************/
3839 gtk_drag_cancel (GtkDragSourceInfo *info, guint32 time)
3841 gtk_drag_end (info, time);
3842 gdk_drag_abort (info->context, time);
3843 gtk_drag_drop_finished (info, FALSE, time);
3846 /*************************************************************
3847 * gtk_drag_motion_cb:
3848 * "motion_notify_event" callback during drag.
3852 *************************************************************/
3855 gtk_drag_motion_cb (GtkWidget *widget,
3856 GdkEventMotion *event,
3859 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
3861 gint x_root, y_root;
3865 GdkDisplay *display = gtk_widget_get_display (widget);
3867 gdk_display_get_pointer (display, &screen, &x_root, &y_root, NULL);
3868 event->x_root = x_root;
3869 event->y_root = y_root;
3872 screen = gdk_event_get_screen ((GdkEvent *)event);
3874 gtk_drag_update (info, screen, event->x_root, event->y_root, (GdkEvent *)event);
3879 /*************************************************************
3881 * "key_press/release_event" callback during drag.
3885 *************************************************************/
3888 gtk_drag_key_cb (GtkWidget *widget,
3892 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
3893 GdkModifierType state;
3894 GdkWindow *root_window;
3896 if (event->type == GDK_KEY_PRESS)
3898 if (event->keyval == GDK_Escape)
3900 gtk_drag_cancel (info, event->time);
3906 /* Now send a "motion" so that the modifier state is updated */
3908 /* The state is not yet updated in the event, so we need
3909 * to query it here. We could use XGetModifierMapping, but
3910 * that would be overkill.
3912 root_window = gtk_widget_get_root_window (widget);
3913 gdk_window_get_pointer (root_window, NULL, NULL, &state);
3915 event->state = state;
3916 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
3921 /*************************************************************
3922 * gtk_drag_button_release_cb:
3923 * "button_release_event" callback during drag.
3927 *************************************************************/
3930 gtk_drag_button_release_cb (GtkWidget *widget,
3931 GdkEventButton *event,
3934 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
3936 if (event->button != info->button)
3939 if ((info->context->action != 0) && (info->context->dest_window != NULL))
3941 gtk_drag_end (info, event->time);
3942 gtk_drag_drop (info, event->time);
3946 gtk_drag_cancel (info, event->time);
3953 gtk_drag_abort_timeout (gpointer data)
3955 GtkDragSourceInfo *info = data;
3956 guint32 time = GDK_CURRENT_TIME;
3958 GDK_THREADS_ENTER ();
3960 if (info->proxy_dest)
3961 time = info->proxy_dest->proxy_drop_time;
3963 info->drop_timeout = 0;
3964 gtk_drag_drop_finished (info, FALSE, time);
3966 GDK_THREADS_LEAVE ();
3972 * gtk_drag_check_threshold:
3973 * @widget: a #GtkWidget
3974 * @start_x: X coordinate of start of drag
3975 * @start_y: Y coordinate of start of drag
3976 * @current_x: current X coordinate
3977 * @current_y: current Y coordinate
3979 * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
3980 * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
3981 * should trigger the beginning of a drag-and-drop operation.
3983 * Return Value: %TRUE if the drag threshold has been passed.
3986 gtk_drag_check_threshold (GtkWidget *widget,
3992 gint drag_threshold;
3994 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
3996 g_object_get (gtk_widget_get_settings (widget),
3997 "gtk-dnd-drag-threshold", &drag_threshold,
4000 return (ABS (current_x - start_x) > drag_threshold ||
4001 ABS (current_y - start_y) > drag_threshold);
4004 #define __GTK_DND_C__
4005 #include "gtkaliasdef.c"