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"
48 static GSList *source_widgets = NULL;
50 typedef struct _GtkDragSourceSite GtkDragSourceSite;
51 typedef struct _GtkDragSourceInfo GtkDragSourceInfo;
52 typedef struct _GtkDragDestSite GtkDragDestSite;
53 typedef struct _GtkDragDestInfo GtkDragDestInfo;
54 typedef struct _GtkDragAnim GtkDragAnim;
55 typedef struct _GtkDragFindData GtkDragFindData;
65 struct _GtkDragSourceSite
67 GdkModifierType start_button_mask;
68 GtkTargetList *target_list; /* Targets for drag data */
69 GdkDragAction actions; /* Possible actions */
72 GtkImageType icon_type;
75 GtkImagePixmapData pixmap;
76 GtkImagePixbufData pixbuf;
77 GtkImageStockData stock;
78 GtkImageIconNameData name;
82 GdkColormap *colormap; /* Colormap for drag icon */
84 /* Stored button press information to detect drag beginning */
89 struct _GtkDragSourceInfo
92 GtkTargetList *target_list; /* Targets for drag data */
93 GdkDragAction possible_actions; /* Actions allowed by source */
94 GdkDragContext *context; /* drag context */
95 GtkWidget *icon_window; /* Window for drag */
96 GtkWidget *fallback_icon; /* Window for drag used on other screens */
97 GtkWidget *ipc_widget; /* GtkInvisible for grab, message passing */
98 GdkCursor *cursor; /* Cursor for drag */
99 gint hot_x, hot_y; /* Hot spot for drag */
100 gint button; /* mouse button starting drag */
102 GtkDragStatus status; /* drag status */
103 GdkEvent *last_event; /* pending event */
105 gint start_x, start_y; /* Initial position */
106 gint cur_x, cur_y; /* Current Position */
107 GdkScreen *cur_screen; /* Current screen for pointer */
109 guint32 grab_time; /* timestamp for initial grab */
110 GList *selections; /* selections we've claimed */
112 GtkDragDestInfo *proxy_dest; /* Set if this is a proxy drag */
114 guint update_idle; /* Idle function to update the drag */
115 guint drop_timeout; /* Timeout for aborting drop */
116 guint destroy_icon : 1; /* If true, destroy icon_window
118 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), I_("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 I_("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 I_("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_WEST;
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),
752 drag_cursors[i].action, info);
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), I_("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_static_string ("DELETE");
873 else if (context->protocol == GDK_DRAG_PROTO_MOTIF)
875 target = gdk_atom_intern_static_string (success ?
876 "XmTRANSFER_SUCCESS" :
877 "XmTRANSFER_FAILURE");
880 if (target != GDK_NONE)
882 GtkWidget *selection_widget = gtk_drag_get_ipc_widget (gdk_drawable_get_screen (context->source_window));
884 g_object_ref (context);
886 g_object_set_data (G_OBJECT (selection_widget), I_("drag-context"), context);
887 g_signal_connect (selection_widget, "selection_received",
888 G_CALLBACK (gtk_drag_selection_received),
891 gtk_selection_convert (selection_widget,
892 gdk_drag_get_selection (context),
897 if (!(success && del))
898 gdk_drop_finish (context, success, time);
901 /*************************************************************
902 * gtk_drag_highlight_expose:
903 * Callback for expose_event for highlighted widgets.
909 *************************************************************/
912 gtk_drag_highlight_expose (GtkWidget *widget,
913 GdkEventExpose *event,
916 gint x, y, width, height;
918 if (GTK_WIDGET_DRAWABLE (widget))
922 if (GTK_WIDGET_NO_WINDOW (widget))
924 x = widget->allocation.x;
925 y = widget->allocation.y;
926 width = widget->allocation.width;
927 height = widget->allocation.height;
933 gdk_drawable_get_size (widget->window, &width, &height);
936 gtk_paint_shadow (widget->style, widget->window,
937 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
939 x, y, width, height);
941 cr = gdk_cairo_create (widget->window);
942 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
943 cairo_set_line_width (cr, 1.0);
946 width - 1, height - 1);
954 /*************************************************************
955 * gtk_drag_highlight:
956 * Highlight the given widget in the default manner.
960 *************************************************************/
963 gtk_drag_highlight (GtkWidget *widget)
965 g_return_if_fail (GTK_IS_WIDGET (widget));
967 g_signal_connect_after (widget, "expose_event",
968 G_CALLBACK (gtk_drag_highlight_expose),
971 gtk_widget_queue_draw (widget);
974 /*************************************************************
975 * gtk_drag_unhighlight:
976 * Refresh the given widget to remove the highlight.
980 *************************************************************/
983 gtk_drag_unhighlight (GtkWidget *widget)
985 g_return_if_fail (GTK_IS_WIDGET (widget));
987 g_signal_handlers_disconnect_by_func (widget,
988 gtk_drag_highlight_expose,
991 gtk_widget_queue_draw (widget);
995 gtk_drag_dest_set_internal (GtkWidget *widget,
996 GtkDragDestSite *site)
998 GtkDragDestSite *old_site;
1000 g_return_if_fail (widget != NULL);
1002 /* HACK, do this in the destroy */
1003 old_site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1006 g_signal_handlers_disconnect_by_func (widget,
1007 gtk_drag_dest_realized,
1009 g_signal_handlers_disconnect_by_func (widget,
1010 gtk_drag_dest_hierarchy_changed,
1014 if (GTK_WIDGET_REALIZED (widget))
1015 gtk_drag_dest_realized (widget);
1017 g_signal_connect (widget, "realize",
1018 G_CALLBACK (gtk_drag_dest_realized), site);
1019 g_signal_connect (widget, "hierarchy_changed",
1020 G_CALLBACK (gtk_drag_dest_hierarchy_changed), site);
1022 g_object_set_data_full (G_OBJECT (widget), I_("gtk-drag-dest"),
1023 site, gtk_drag_dest_site_destroy);
1027 /*************************************************************
1028 * gtk_drag_dest_set:
1029 * Register a drop site, and possibly add default behaviors.
1032 * flags: Which types of default drag behavior to use
1033 * targets: Table of targets that can be accepted
1034 * n_targets: Number of of entries in targets
1037 *************************************************************/
1040 gtk_drag_dest_set (GtkWidget *widget,
1041 GtkDestDefaults flags,
1042 const GtkTargetEntry *targets,
1044 GdkDragAction actions)
1046 GtkDragDestSite *site;
1048 g_return_if_fail (GTK_IS_WIDGET (widget));
1050 site = g_new (GtkDragDestSite, 1);
1052 site->flags = flags;
1053 site->have_drag = FALSE;
1055 site->target_list = gtk_target_list_new (targets, n_targets);
1057 site->target_list = NULL;
1058 site->actions = actions;
1059 site->do_proxy = FALSE;
1060 site->proxy_window = NULL;
1062 gtk_drag_dest_set_internal (widget, site);
1065 /*************************************************************
1066 * gtk_drag_dest_set_proxy:
1067 * Set up this widget to proxy drags elsewhere.
1070 * proxy_window: window to which forward drag events
1071 * protocol: Drag protocol which the dest widget accepts
1072 * use_coordinates: If true, send the same coordinates to the
1073 * destination, because it is a embedded
1076 *************************************************************/
1079 gtk_drag_dest_set_proxy (GtkWidget *widget,
1080 GdkWindow *proxy_window,
1081 GdkDragProtocol protocol,
1082 gboolean use_coordinates)
1084 GtkDragDestSite *site;
1086 g_return_if_fail (GTK_IS_WIDGET (widget));
1087 g_return_if_fail (!proxy_window || GDK_IS_WINDOW (proxy_window));
1089 site = g_new (GtkDragDestSite, 1);
1092 site->have_drag = FALSE;
1093 site->target_list = NULL;
1095 site->proxy_window = proxy_window;
1097 g_object_ref (proxy_window);
1098 site->do_proxy = TRUE;
1099 site->proxy_protocol = protocol;
1100 site->proxy_coords = use_coordinates;
1102 gtk_drag_dest_set_internal (widget, site);
1105 /*************************************************************
1106 * gtk_drag_dest_unset
1107 * Unregister this widget as a drag target.
1111 *************************************************************/
1114 gtk_drag_dest_unset (GtkWidget *widget)
1116 g_return_if_fail (GTK_IS_WIDGET (widget));
1118 g_object_set_data (G_OBJECT (widget), I_("gtk-drag-dest"), NULL);
1122 * gtk_drag_dest_get_target_list:
1123 * @widget: a #GtkWidget
1125 * Returns the list of targets this widget can accept from
1128 * Return value: the #GtkTargetList, or %NULL if none
1131 gtk_drag_dest_get_target_list (GtkWidget *widget)
1133 GtkDragDestSite *site;
1135 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1137 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1139 return site ? site->target_list : NULL;
1143 * gtk_drag_dest_set_target_list:
1144 * @widget: a #GtkWidget that's a drag destination
1145 * @target_list: list of droppable targets, or %NULL for none
1147 * Sets the target types that this widget can accept from drag-and-drop.
1148 * The widget must first be made into a drag destination with
1149 * gtk_drag_dest_set().
1152 gtk_drag_dest_set_target_list (GtkWidget *widget,
1153 GtkTargetList *target_list)
1155 GtkDragDestSite *site;
1157 g_return_if_fail (GTK_IS_WIDGET (widget));
1159 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1163 g_warning ("Can't set a target list on a widget until you've called gtk_drag_dest_set() "
1164 "to make the widget into a drag destination");
1169 gtk_target_list_ref (target_list);
1171 if (site->target_list)
1172 gtk_target_list_unref (site->target_list);
1174 site->target_list = target_list;
1178 * gtk_drag_dest_add_text_targets:
1179 * @widget: a #GtkWidget that's a drag destination
1181 * Add the text targets supported by #GtkSelection to
1182 * the target list of the drag destination. The targets
1183 * are added with @info = 0. If you need another value,
1184 * use gtk_target_list_add_text_targets() and
1185 * gtk_drag_dest_set_target_list().
1190 gtk_drag_dest_add_text_targets (GtkWidget *widget)
1192 GtkTargetList *target_list;
1194 target_list = gtk_drag_dest_get_target_list (widget);
1196 gtk_target_list_ref (target_list);
1198 target_list = gtk_target_list_new (NULL, 0);
1199 gtk_target_list_add_text_targets (target_list, 0);
1200 gtk_drag_dest_set_target_list (widget, target_list);
1201 gtk_target_list_unref (target_list);
1205 * gtk_drag_dest_add_image_targets:
1206 * @widget: a #GtkWidget that's a drag destination
1208 * Add the image targets supported by #GtkSelection to
1209 * the target list of the drag destination. The targets
1210 * are added with @info = 0. If you need another value,
1211 * use gtk_target_list_add_image_targets() and
1212 * gtk_drag_dest_set_target_list().
1217 gtk_drag_dest_add_image_targets (GtkWidget *widget)
1219 GtkTargetList *target_list;
1221 target_list = gtk_drag_dest_get_target_list (widget);
1223 gtk_target_list_ref (target_list);
1225 target_list = gtk_target_list_new (NULL, 0);
1226 gtk_target_list_add_image_targets (target_list, 0, FALSE);
1227 gtk_drag_dest_set_target_list (widget, target_list);
1228 gtk_target_list_unref (target_list);
1232 * gtk_drag_dest_add_uri_targets:
1233 * @widget: a #GtkWidget that's a drag destination
1235 * Add the URI targets supported by #GtkSelection to
1236 * the target list of the drag destination. The targets
1237 * are added with @info = 0. If you need another value,
1238 * use gtk_target_list_add_uri_targets() and
1239 * gtk_drag_dest_set_target_list().
1244 gtk_drag_dest_add_uri_targets (GtkWidget *widget)
1246 GtkTargetList *target_list;
1248 target_list = gtk_drag_dest_get_target_list (widget);
1250 gtk_target_list_ref (target_list);
1252 target_list = gtk_target_list_new (NULL, 0);
1253 gtk_target_list_add_uri_targets (target_list, 0);
1254 gtk_drag_dest_set_target_list (widget, target_list);
1255 gtk_target_list_unref (target_list);
1258 /*************************************************************
1259 * _gtk_drag_dest_handle_event:
1260 * Called from widget event handling code on Drag events
1264 * toplevel: Toplevel widget that received the event
1267 *************************************************************/
1270 _gtk_drag_dest_handle_event (GtkWidget *toplevel,
1273 GtkDragDestInfo *info;
1274 GdkDragContext *context;
1276 g_return_if_fail (toplevel != NULL);
1277 g_return_if_fail (event != NULL);
1279 context = event->dnd.context;
1281 info = gtk_drag_get_dest_info (context, TRUE);
1283 /* Find the widget for the event */
1284 switch (event->type)
1286 case GDK_DRAG_ENTER:
1289 case GDK_DRAG_LEAVE:
1292 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1293 info->widget = NULL;
1297 case GDK_DRAG_MOTION:
1298 case GDK_DROP_START:
1300 GtkDragFindData data;
1303 if (event->type == GDK_DROP_START)
1305 info->dropped = TRUE;
1306 /* We send a leave here so that the widget unhighlights
1311 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1312 info->widget = NULL;
1316 #ifdef GDK_WINDOWING_X11
1317 /* Hackaround for: http://bugzilla.gnome.org/show_bug.cgi?id=136112
1319 * Currently gdk_window_get_position doesn't provide reliable
1320 * information for embedded windows, so we call the much more
1321 * expensive gdk_window_get_origin().
1323 if (GTK_IS_PLUG (toplevel))
1324 gdk_window_get_origin (toplevel->window, &tx, &ty);
1326 #endif /* GDK_WINDOWING_X11 */
1327 gdk_window_get_position (toplevel->window, &tx, &ty);
1329 data.x = event->dnd.x_root - tx;
1330 data.y = event->dnd.y_root - ty;
1331 data.context = context;
1334 data.toplevel = TRUE;
1335 data.callback = (event->type == GDK_DRAG_MOTION) ?
1336 gtk_drag_dest_motion : gtk_drag_dest_drop;
1337 data.time = event->dnd.time;
1339 gtk_drag_find_widget (toplevel, &data);
1341 if (info->widget && !data.found)
1343 gtk_drag_dest_leave (info->widget, context, event->dnd.time);
1344 info->widget = NULL;
1349 if (event->type == GDK_DRAG_MOTION)
1352 gdk_drag_status (context, 0, event->dnd.time);
1354 else if (event->type == GDK_DROP_START && !info->proxy_source)
1356 gdk_drop_reply (context, data.found, event->dnd.time);
1357 if ((context->protocol == GDK_DRAG_PROTO_MOTIF) && !data.found)
1358 gtk_drag_finish (context, FALSE, FALSE, event->dnd.time);
1364 g_assert_not_reached ();
1369 * gtk_drag_dest_find_target:
1370 * @widget: drag destination widget
1371 * @context: drag context
1372 * @target_list: list of droppable targets, or %NULL to use
1373 * gtk_drag_dest_get_target_list (@widget).
1375 * Looks for a match between @context->targets and the
1376 * @dest_target_list, returning the first matching target, otherwise
1377 * returning %GDK_NONE. @dest_target_list should usually be the return
1378 * value from gtk_drag_dest_get_target_list(), but some widgets may
1379 * have different valid targets for different parts of the widget; in
1380 * that case, they will have to implement a drag_motion handler that
1381 * passes the correct target list to this function.
1383 * Return value: first target that the source offers and the dest can accept, or %GDK_NONE
1386 gtk_drag_dest_find_target (GtkWidget *widget,
1387 GdkDragContext *context,
1388 GtkTargetList *target_list)
1391 GList *tmp_source = NULL;
1392 GtkWidget *source_widget;
1394 g_return_val_if_fail (GTK_IS_WIDGET (widget), GDK_NONE);
1395 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), GDK_NONE);
1396 g_return_val_if_fail (!context->is_source, GDK_NONE);
1399 source_widget = gtk_drag_get_source_widget (context);
1401 if (target_list == NULL)
1402 target_list = gtk_drag_dest_get_target_list (widget);
1404 if (target_list == NULL)
1407 tmp_target = target_list->list;
1410 GtkTargetPair *pair = tmp_target->data;
1411 tmp_source = context->targets;
1414 if (tmp_source->data == GUINT_TO_POINTER (pair->target))
1416 if ((!(pair->flags & GTK_TARGET_SAME_APP) || source_widget) &&
1417 (!(pair->flags & GTK_TARGET_SAME_WIDGET) || (source_widget == widget)))
1418 return pair->target;
1422 tmp_source = tmp_source->next;
1424 tmp_target = tmp_target->next;
1431 gtk_drag_selection_received (GtkWidget *widget,
1432 GtkSelectionData *selection_data,
1436 GdkDragContext *context;
1437 GtkDragDestInfo *info;
1438 GtkWidget *drop_widget;
1442 context = g_object_get_data (G_OBJECT (widget), "drag-context");
1443 info = gtk_drag_get_dest_info (context, FALSE);
1445 if (info->proxy_data &&
1446 info->proxy_data->target == selection_data->target)
1448 gtk_selection_data_set (info->proxy_data,
1449 selection_data->type,
1450 selection_data->format,
1451 selection_data->data,
1452 selection_data->length);
1457 if (selection_data->target == gdk_atom_intern_static_string ("DELETE"))
1459 gtk_drag_finish (context, TRUE, FALSE, time);
1461 else if ((selection_data->target == gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS")) ||
1462 (selection_data->target == gdk_atom_intern_static_string ("XmTRANSFER_FAILURE")))
1468 GtkDragDestSite *site;
1470 site = g_object_get_data (G_OBJECT (drop_widget), "gtk-drag-dest");
1472 if (site && site->target_list)
1476 if (gtk_target_list_find (site->target_list,
1477 selection_data->target,
1480 if (!(site->flags & GTK_DEST_DEFAULT_DROP) ||
1481 selection_data->length >= 0)
1482 g_signal_emit_by_name (drop_widget,
1483 "drag_data_received",
1484 context, info->drop_x, info->drop_y,
1491 g_signal_emit_by_name (drop_widget,
1492 "drag_data_received",
1493 context, info->drop_x, info->drop_y,
1498 if (site && site->flags & GTK_DEST_DEFAULT_DROP)
1501 gtk_drag_finish (context,
1502 (selection_data->length >= 0),
1503 (context->action == GDK_ACTION_MOVE),
1507 g_object_unref (drop_widget);
1510 g_signal_handlers_disconnect_by_func (widget,
1511 gtk_drag_selection_received,
1514 g_object_set_data (G_OBJECT (widget), I_("drag-context"), NULL);
1515 g_object_unref (context);
1517 gtk_drag_release_ipc_widget (widget);
1521 prepend_and_ref_widget (GtkWidget *widget,
1524 GSList **slist_p = data;
1526 *slist_p = g_slist_prepend (*slist_p, g_object_ref (widget));
1529 /*************************************************************
1530 * gtk_drag_find_widget:
1531 * Recursive callback used to locate widgets for
1532 * DRAG_MOTION and DROP_START events.
1536 *************************************************************/
1539 gtk_drag_find_widget (GtkWidget *widget,
1540 GtkDragFindData *data)
1542 GtkAllocation new_allocation;
1543 gint allocation_to_window_x = 0;
1544 gint allocation_to_window_y = 0;
1548 if (data->found || !GTK_WIDGET_MAPPED (widget) || !GTK_WIDGET_SENSITIVE (widget))
1551 /* Note that in the following code, we only count the
1552 * position as being inside a WINDOW widget if it is inside
1553 * widget->window; points that are outside of widget->window
1554 * but within the allocation are not counted. This is consistent
1555 * with the way we highlight drag targets.
1557 * data->x,y are relative to widget->parent->window (if
1558 * widget is not a toplevel, widget->window otherwise).
1559 * We compute the allocation of widget in the same coordinates,
1560 * clipping to widget->window, and all intermediate
1561 * windows. If data->x,y is inside that, then we translate
1562 * our coordinates to be relative to widget->window and
1565 new_allocation = widget->allocation;
1570 GdkWindow *window = widget->window;
1572 /* Compute the offset from allocation-relative to
1573 * window-relative coordinates.
1575 allocation_to_window_x = widget->allocation.x;
1576 allocation_to_window_y = widget->allocation.y;
1578 if (!GTK_WIDGET_NO_WINDOW (widget))
1580 /* The allocation is relative to the parent window for
1581 * window widgets, not to widget->window.
1583 gdk_window_get_position (window, &tx, &ty);
1585 allocation_to_window_x -= tx;
1586 allocation_to_window_y -= ty;
1589 new_allocation.x = 0 + allocation_to_window_x;
1590 new_allocation.y = 0 + allocation_to_window_y;
1592 while (window && window != widget->parent->window)
1594 GdkRectangle window_rect = { 0, 0, 0, 0 };
1596 gdk_drawable_get_size (window, &window_rect.width, &window_rect.height);
1598 gdk_rectangle_intersect (&new_allocation, &window_rect, &new_allocation);
1600 gdk_window_get_position (window, &tx, &ty);
1601 new_allocation.x += tx;
1603 new_allocation.y += ty;
1606 window = gdk_window_get_parent (window);
1609 if (!window) /* Window and widget heirarchies didn't match. */
1613 if (data->toplevel ||
1614 ((data->x >= new_allocation.x) && (data->y >= new_allocation.y) &&
1615 (data->x < new_allocation.x + new_allocation.width) &&
1616 (data->y < new_allocation.y + new_allocation.height)))
1618 /* First, check if the drag is in a valid drop site in
1619 * one of our children
1621 if (GTK_IS_CONTAINER (widget))
1623 GtkDragFindData new_data = *data;
1624 GSList *children = NULL;
1627 new_data.x -= x_offset;
1628 new_data.y -= y_offset;
1629 new_data.found = FALSE;
1630 new_data.toplevel = FALSE;
1632 /* need to reference children temporarily in case the
1633 * ::drag_motion/::drag_drop callbacks change the widget heirarchy.
1635 gtk_container_forall (GTK_CONTAINER (widget), prepend_and_ref_widget, &children);
1636 for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
1638 if (!new_data.found && GTK_WIDGET_DRAWABLE (tmp_list->data))
1639 gtk_drag_find_widget (tmp_list->data, &new_data);
1640 g_object_unref (tmp_list->data);
1642 g_slist_free (children);
1644 data->found = new_data.found;
1647 /* If not, and this widget is registered as a drop site, check to
1648 * emit "drag_motion" to check if we are actually in
1652 g_object_get_data (G_OBJECT (widget), "gtk-drag-dest"))
1654 data->found = data->callback (widget,
1656 data->x - x_offset - allocation_to_window_x,
1657 data->y - y_offset - allocation_to_window_y,
1659 /* If so, send a "drag_leave" to the last widget */
1662 if (data->info->widget && data->info->widget != widget)
1664 gtk_drag_dest_leave (data->info->widget, data->context, data->time);
1666 data->info->widget = widget;
1673 gtk_drag_proxy_begin (GtkWidget *widget,
1674 GtkDragDestInfo *dest_info,
1677 GtkDragSourceInfo *source_info;
1679 GdkDragContext *context;
1680 GtkWidget *ipc_widget;
1682 if (dest_info->proxy_source)
1684 gdk_drag_abort (dest_info->proxy_source->context, time);
1685 gtk_drag_source_info_destroy (dest_info->proxy_source);
1686 dest_info->proxy_source = NULL;
1689 ipc_widget = gtk_drag_get_ipc_widget (gtk_widget_get_screen (widget));
1690 context = gdk_drag_begin (ipc_widget->window,
1691 dest_info->context->targets);
1693 source_info = gtk_drag_get_source_info (context, TRUE);
1695 source_info->ipc_widget = ipc_widget;
1696 source_info->widget = gtk_widget_ref (widget);
1698 source_info->target_list = gtk_target_list_new (NULL, 0);
1699 tmp_list = dest_info->context->targets;
1702 gtk_target_list_add (source_info->target_list,
1703 GDK_POINTER_TO_ATOM (tmp_list->data), 0, 0);
1704 tmp_list = tmp_list->next;
1707 source_info->proxy_dest = dest_info;
1709 g_signal_connect (ipc_widget,
1711 G_CALLBACK (gtk_drag_selection_get),
1714 dest_info->proxy_source = source_info;
1718 gtk_drag_dest_info_destroy (gpointer data)
1720 GtkDragDestInfo *info = data;
1725 static GtkDragDestInfo *
1726 gtk_drag_get_dest_info (GdkDragContext *context,
1729 GtkDragDestInfo *info;
1730 static GQuark info_quark = 0;
1732 info_quark = g_quark_from_static_string ("gtk-dest-info");
1734 info = g_object_get_qdata (G_OBJECT (context), info_quark);
1735 if (!info && create)
1737 info = g_new (GtkDragDestInfo, 1);
1738 info->widget = NULL;
1739 info->context = context;
1740 info->proxy_source = NULL;
1741 info->proxy_data = NULL;
1742 info->dropped = FALSE;
1743 info->proxy_drop_wait = FALSE;
1744 g_object_set_qdata_full (G_OBJECT (context), info_quark,
1745 info, gtk_drag_dest_info_destroy);
1751 static GQuark dest_info_quark = 0;
1753 static GtkDragSourceInfo *
1754 gtk_drag_get_source_info (GdkDragContext *context,
1757 GtkDragSourceInfo *info;
1758 if (!dest_info_quark)
1759 dest_info_quark = g_quark_from_static_string ("gtk-source-info");
1761 info = g_object_get_qdata (G_OBJECT (context), dest_info_quark);
1762 if (!info && create)
1764 info = g_new0 (GtkDragSourceInfo, 1);
1765 info->context = context;
1766 g_object_set_qdata (G_OBJECT (context), dest_info_quark, info);
1773 gtk_drag_clear_source_info (GdkDragContext *context)
1775 g_object_set_qdata (G_OBJECT (context), dest_info_quark, NULL);
1779 gtk_drag_dest_realized (GtkWidget *widget)
1781 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1783 if (GTK_WIDGET_TOPLEVEL (toplevel))
1784 gdk_window_register_dnd (toplevel->window);
1788 gtk_drag_dest_hierarchy_changed (GtkWidget *widget,
1789 GtkWidget *previous_toplevel)
1791 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
1793 if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_WIDGET_REALIZED (toplevel))
1794 gdk_window_register_dnd (toplevel->window);
1798 gtk_drag_dest_site_destroy (gpointer data)
1800 GtkDragDestSite *site = data;
1802 if (site->proxy_window)
1803 g_object_unref (site->proxy_window);
1805 if (site->target_list)
1806 gtk_target_list_unref (site->target_list);
1812 * Default drag handlers
1815 gtk_drag_dest_leave (GtkWidget *widget,
1816 GdkDragContext *context,
1819 GtkDragDestSite *site;
1821 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1822 g_return_if_fail (site != NULL);
1826 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
1828 if (info->proxy_source && info->proxy_source->widget == widget && !info->dropped)
1830 gdk_drag_abort (info->proxy_source->context, time);
1831 gtk_drag_source_info_destroy (info->proxy_source);
1832 info->proxy_source = NULL;
1839 if ((site->flags & GTK_DEST_DEFAULT_HIGHLIGHT) && site->have_drag)
1840 gtk_drag_unhighlight (widget);
1842 if (!(site->flags & GTK_DEST_DEFAULT_MOTION) || site->have_drag)
1843 g_signal_emit_by_name (widget, "drag_leave",
1846 site->have_drag = FALSE;
1851 gtk_drag_dest_motion (GtkWidget *widget,
1852 GdkDragContext *context,
1857 GtkDragDestSite *site;
1858 GdkDragAction action = 0;
1861 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1862 g_return_val_if_fail (site != NULL, FALSE);
1867 GdkEvent *current_event;
1868 GdkWindow *dest_window;
1869 GdkDragProtocol proto;
1871 GtkDragDestInfo *info = gtk_drag_get_dest_info (context, FALSE);
1873 if (!info->proxy_source || info->proxy_source->widget != widget)
1874 gtk_drag_proxy_begin (widget, info, time);
1876 current_event = gtk_get_current_event ();
1878 if (site->proxy_window)
1880 dest_window = site->proxy_window;
1881 proto = site->proxy_protocol;
1885 gdk_drag_find_window_for_screen (info->proxy_source->context,
1887 gdk_drawable_get_screen (current_event->dnd.window),
1888 current_event->dnd.x_root,
1889 current_event->dnd.y_root,
1890 &dest_window, &proto);
1893 gdk_drag_motion (info->proxy_source->context,
1895 current_event->dnd.x_root,
1896 current_event->dnd.y_root,
1897 context->suggested_action,
1898 context->actions, time);
1900 if (!site->proxy_window && dest_window)
1901 g_object_unref (dest_window);
1903 selection = gdk_drag_get_selection (info->proxy_source->context);
1905 selection != gdk_drag_get_selection (info->context))
1906 gtk_drag_source_check_selection (info->proxy_source, selection, time);
1908 gdk_event_free (current_event);
1913 if (site->flags & GTK_DEST_DEFAULT_MOTION)
1915 if (context->suggested_action & site->actions)
1916 action = context->suggested_action;
1923 if ((site->actions & (1 << i)) &&
1924 (context->actions & (1 << i)))
1932 if (action && gtk_drag_dest_find_target (widget, context, NULL))
1934 if (!site->have_drag)
1936 site->have_drag = TRUE;
1937 if (site->flags & GTK_DEST_DEFAULT_HIGHLIGHT)
1938 gtk_drag_highlight (widget);
1941 gdk_drag_status (context, action, time);
1945 gdk_drag_status (context, 0, time);
1950 g_signal_emit_by_name (widget, "drag_motion",
1951 context, x, y, time, &retval);
1953 return (site->flags & GTK_DEST_DEFAULT_MOTION) ? TRUE : retval;
1957 gtk_drag_dest_drop (GtkWidget *widget,
1958 GdkDragContext *context,
1963 GtkDragDestSite *site;
1964 GtkDragDestInfo *info;
1966 site = g_object_get_data (G_OBJECT (widget), "gtk-drag-dest");
1967 g_return_val_if_fail (site != NULL, FALSE);
1969 info = gtk_drag_get_dest_info (context, FALSE);
1970 g_return_val_if_fail (info != NULL, FALSE);
1977 if (info->proxy_source ||
1978 (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN))
1980 gtk_drag_drop (info->proxy_source, time);
1984 /* We need to synthesize a motion event, wait for a status,
1985 * and, if we get a good one, do a drop.
1988 GdkEvent *current_event;
1990 GdkWindow *dest_window;
1991 GdkDragProtocol proto;
1993 gtk_drag_proxy_begin (widget, info, time);
1994 info->proxy_drop_wait = TRUE;
1995 info->proxy_drop_time = time;
1997 current_event = gtk_get_current_event ();
1999 if (site->proxy_window)
2001 dest_window = site->proxy_window;
2002 proto = site->proxy_protocol;
2006 gdk_drag_find_window_for_screen (info->proxy_source->context,
2008 gdk_drawable_get_screen (current_event->dnd.window),
2009 current_event->dnd.x_root,
2010 current_event->dnd.y_root,
2011 &dest_window, &proto);
2014 gdk_drag_motion (info->proxy_source->context,
2016 current_event->dnd.x_root,
2017 current_event->dnd.y_root,
2018 context->suggested_action,
2019 context->actions, time);
2021 if (!site->proxy_window && dest_window)
2022 g_object_unref (dest_window);
2024 selection = gdk_drag_get_selection (info->proxy_source->context);
2026 selection != gdk_drag_get_selection (info->context))
2027 gtk_drag_source_check_selection (info->proxy_source, selection, time);
2029 gdk_event_free (current_event);
2038 if (site->flags & GTK_DEST_DEFAULT_DROP)
2040 GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
2042 if (target == GDK_NONE)
2044 gtk_drag_finish (context, FALSE, FALSE, time);
2048 gtk_drag_get_data (widget, context, target, time);
2051 g_signal_emit_by_name (widget, "drag_drop",
2052 context, x, y, time, &retval);
2054 return (site->flags & GTK_DEST_DEFAULT_DROP) ? TRUE : retval;
2062 /* Like GtkDragBegin, but also takes a GtkDragSourceSite,
2063 * so that we can set the icon from the source site information
2065 static GdkDragContext *
2066 gtk_drag_begin_internal (GtkWidget *widget,
2067 GtkDragSourceSite *site,
2068 GtkTargetList *target_list,
2069 GdkDragAction actions,
2073 GtkDragSourceInfo *info;
2074 GList *targets = NULL;
2076 guint32 time = GDK_CURRENT_TIME;
2077 GdkDragAction possible_actions, suggested_action;
2078 GdkDragContext *context;
2079 GtkWidget *ipc_widget;
2082 ipc_widget = gtk_drag_get_ipc_widget (gtk_widget_get_screen (widget));
2084 gtk_drag_get_event_actions (event, button, actions,
2085 &suggested_action, &possible_actions);
2087 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2092 time = gdk_event_get_time (event);
2094 if (gdk_pointer_grab (ipc_widget->window, FALSE,
2095 GDK_POINTER_MOTION_MASK |
2096 GDK_BUTTON_RELEASE_MASK, NULL,
2097 cursor, time) != GDK_GRAB_SUCCESS)
2099 gtk_drag_release_ipc_widget (ipc_widget);
2103 gdk_keyboard_grab (ipc_widget->window, FALSE, time);
2105 /* We use a GTK grab here to override any grabs that the widget
2106 * we are dragging from might have held
2108 gtk_grab_add (ipc_widget);
2110 tmp_list = g_list_last (target_list->list);
2113 GtkTargetPair *pair = tmp_list->data;
2114 targets = g_list_prepend (targets,
2115 GINT_TO_POINTER (pair->target));
2116 tmp_list = tmp_list->prev;
2119 source_widgets = g_slist_prepend (source_widgets, ipc_widget);
2121 context = gdk_drag_begin (ipc_widget->window, targets);
2122 g_list_free (targets);
2124 info = gtk_drag_get_source_info (context, TRUE);
2126 info->ipc_widget = ipc_widget;
2127 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), info);
2129 info->widget = gtk_widget_ref (widget);
2131 info->button = button;
2132 info->cursor = cursor;
2133 info->target_list = target_list;
2134 gtk_target_list_ref (target_list);
2136 info->possible_actions = actions;
2138 info->status = GTK_DRAG_STATUS_DRAG;
2139 info->last_event = NULL;
2140 info->selections = NULL;
2141 info->icon_window = NULL;
2142 info->destroy_icon = FALSE;
2144 /* Set cur_x, cur_y here so if the "drag_begin" signal shows
2145 * the drag icon, it will be in the right place
2147 if (event && event->type == GDK_MOTION_NOTIFY)
2149 info->cur_screen = gtk_widget_get_screen (widget);
2150 info->cur_x = event->motion.x_root;
2151 info->cur_y = event->motion.y_root;
2155 gdk_display_get_pointer (gtk_widget_get_display (widget),
2156 &info->cur_screen, &info->cur_x, &info->cur_y, NULL);
2159 g_signal_emit_by_name (widget, "drag_begin", info->context);
2161 /* Ensure that we have an icon before we start the drag; the
2162 * application may have set one in ::drag_begin, or it may
2165 if (!info->icon_window && !info->icon_pixbuf)
2167 if (!site || site->icon_type == GTK_IMAGE_EMPTY)
2168 gtk_drag_set_icon_default (context);
2170 switch (site->icon_type)
2172 case GTK_IMAGE_PIXMAP:
2173 gtk_drag_set_icon_pixmap (context,
2175 site->icon_data.pixmap.pixmap,
2179 case GTK_IMAGE_PIXBUF:
2180 gtk_drag_set_icon_pixbuf (context,
2181 site->icon_data.pixbuf.pixbuf,
2184 case GTK_IMAGE_STOCK:
2185 gtk_drag_set_icon_stock (context,
2186 site->icon_data.stock.stock_id,
2189 case GTK_IMAGE_ICON_NAME:
2190 gtk_drag_set_icon_name (context,
2191 site->icon_data.name.icon_name,
2194 case GTK_IMAGE_EMPTY:
2196 g_assert_not_reached();
2201 /* We need to composite the icon into the cursor, if we are
2202 * not using an icon window.
2204 if (info->icon_pixbuf)
2206 cursor = gtk_drag_get_cursor (gtk_widget_get_display (widget),
2210 if (cursor != info->cursor)
2212 gdk_pointer_grab (widget->window, FALSE,
2213 GDK_POINTER_MOTION_MASK |
2214 GDK_BUTTON_RELEASE_MASK,
2217 info->cursor = cursor;
2221 if (event && event->type == GDK_MOTION_NOTIFY)
2222 gtk_drag_motion_cb (info->ipc_widget, (GdkEventMotion *)event, info);
2224 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, event);
2226 info->start_x = info->cur_x;
2227 info->start_y = info->cur_y;
2229 g_signal_connect (info->ipc_widget, "button_release_event",
2230 G_CALLBACK (gtk_drag_button_release_cb), info);
2231 g_signal_connect (info->ipc_widget, "motion_notify_event",
2232 G_CALLBACK (gtk_drag_motion_cb), info);
2233 g_signal_connect (info->ipc_widget, "key_press_event",
2234 G_CALLBACK (gtk_drag_key_cb), info);
2235 g_signal_connect (info->ipc_widget, "key_release_event",
2236 G_CALLBACK (gtk_drag_key_cb), info);
2237 g_signal_connect (info->ipc_widget, "selection_get",
2238 G_CALLBACK (gtk_drag_selection_get), info);
2240 info->have_grab = TRUE;
2241 info->grab_time = time;
2243 return info->context;
2248 * @widget: the source widget.
2249 * @targets: The targets (data formats) in which the
2250 * source can provide the data.
2251 * @actions: A bitmask of the allowed drag actions for this drag.
2252 * @button: The button the user clicked to start the drag.
2253 * @event: The event that triggered the start of the drag.
2255 * Initiates a drag on the source side. The function
2256 * only needs to be used when the application is
2257 * starting drags itself, and is not needed when
2258 * gtk_drag_source_set() is used.
2260 * Return value: the context for this drag.
2263 gtk_drag_begin (GtkWidget *widget,
2264 GtkTargetList *targets,
2265 GdkDragAction actions,
2269 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2270 g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), NULL);
2271 g_return_val_if_fail (targets != NULL, NULL);
2273 return gtk_drag_begin_internal (widget, NULL, targets,
2274 actions, button, event);
2277 /*************************************************************
2278 * gtk_drag_source_set:
2279 * Register a drop site, and possibly add default behaviors.
2282 * start_button_mask: Mask of allowed buttons to start drag
2283 * targets: Table of targets for this source
2285 * actions: Actions allowed for this source
2287 *************************************************************/
2290 gtk_drag_source_set (GtkWidget *widget,
2291 GdkModifierType start_button_mask,
2292 const GtkTargetEntry *targets,
2294 GdkDragAction actions)
2296 GtkDragSourceSite *site;
2298 g_return_if_fail (GTK_IS_WIDGET (widget));
2300 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2302 gtk_widget_add_events (widget,
2303 gtk_widget_get_events (widget) |
2304 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2305 GDK_BUTTON_MOTION_MASK);
2309 if (site->target_list)
2310 gtk_target_list_unref (site->target_list);
2314 site = g_new0 (GtkDragSourceSite, 1);
2316 site->icon_type = GTK_IMAGE_EMPTY;
2318 g_signal_connect (widget, "button_press_event",
2319 G_CALLBACK (gtk_drag_source_event_cb),
2321 g_signal_connect (widget, "button_release_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),
2329 I_("gtk-site-data"),
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_object_set_data (G_OBJECT (widget), I_("gtk-site-data"), NULL);
2367 * gtk_drag_source_get_target_list:
2368 * @widget: a #GtkWidget
2370 * Gets the list of targets this widget can provide for
2373 * Return value: the #GtkTargetList, or %NULL if none
2378 gtk_drag_source_get_target_list (GtkWidget *widget)
2380 GtkDragSourceSite *site;
2382 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2384 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2386 return site ? site->target_list : NULL;
2390 * gtk_drag_source_set_target_list:
2391 * @widget: a #GtkWidget that's a drag source
2392 * @target_list: list of draggable targets, or %NULL for none
2394 * Changes the target types that this widget offers for drag-and-drop.
2395 * The widget must first be made into a drag source with
2396 * gtk_drag_source_set().
2401 gtk_drag_source_set_target_list (GtkWidget *widget,
2402 GtkTargetList *target_list)
2404 GtkDragSourceSite *site;
2406 g_return_if_fail (GTK_IS_WIDGET (widget));
2408 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2411 g_warning ("gtk_drag_source_set_target_list() requires the widget "
2412 "to already be a drag source.");
2417 gtk_target_list_ref (target_list);
2419 if (site->target_list)
2420 gtk_target_list_unref (site->target_list);
2422 site->target_list = target_list;
2426 * gtk_drag_source_add_text_targets:
2427 * @widget: a #GtkWidget that's is a drag source
2429 * Add the text targets supported by #GtkSelection to
2430 * the target list of the drag source. The targets
2431 * are added with @info = 0. If you need another value,
2432 * use gtk_target_list_add_text_targets() and
2433 * gtk_drag_source_set_target_list().
2438 gtk_drag_source_add_text_targets (GtkWidget *widget)
2440 GtkTargetList *target_list;
2442 target_list = gtk_drag_source_get_target_list (widget);
2444 gtk_target_list_ref (target_list);
2446 target_list = gtk_target_list_new (NULL, 0);
2447 gtk_target_list_add_text_targets (target_list, 0);
2448 gtk_drag_source_set_target_list (widget, target_list);
2449 gtk_target_list_unref (target_list);
2453 * gtk_drag_source_add_image_targets:
2454 * @widget: a #GtkWidget that's is a drag source
2456 * Add the writable image targets supported by #GtkSelection to
2457 * the target list of the drag source. The targets
2458 * are added with @info = 0. If you need another value,
2459 * use gtk_target_list_add_image_targets() and
2460 * gtk_drag_source_set_target_list().
2465 gtk_drag_source_add_image_targets (GtkWidget *widget)
2467 GtkTargetList *target_list;
2469 target_list = gtk_drag_source_get_target_list (widget);
2471 gtk_target_list_ref (target_list);
2473 target_list = gtk_target_list_new (NULL, 0);
2474 gtk_target_list_add_image_targets (target_list, 0, TRUE);
2475 gtk_drag_source_set_target_list (widget, target_list);
2476 gtk_target_list_unref (target_list);
2480 * gtk_drag_source_add_uri_targets:
2481 * @widget: a #GtkWidget that's is a drag source
2483 * Add the URI targets supported by #GtkSelection to
2484 * the target list of the drag source. The targets
2485 * are added with @info = 0. If you need another value,
2486 * use gtk_target_list_add_uri_targets() and
2487 * gtk_drag_source_set_target_list().
2492 gtk_drag_source_add_uri_targets (GtkWidget *widget)
2494 GtkTargetList *target_list;
2496 target_list = gtk_drag_source_get_target_list (widget);
2498 gtk_target_list_ref (target_list);
2500 target_list = gtk_target_list_new (NULL, 0);
2501 gtk_target_list_add_uri_targets (target_list, 0);
2502 gtk_drag_source_set_target_list (widget, target_list);
2503 gtk_target_list_unref (target_list);
2507 gtk_drag_source_unset_icon (GtkDragSourceSite *site)
2509 switch (site->icon_type)
2511 case GTK_IMAGE_EMPTY:
2513 case GTK_IMAGE_PIXMAP:
2514 if (site->icon_data.pixmap.pixmap)
2515 g_object_unref (site->icon_data.pixmap.pixmap);
2516 if (site->icon_mask)
2517 g_object_unref (site->icon_mask);
2519 case GTK_IMAGE_PIXBUF:
2520 g_object_unref (site->icon_data.pixbuf.pixbuf);
2522 case GTK_IMAGE_STOCK:
2523 g_free (site->icon_data.stock.stock_id);
2525 case GTK_IMAGE_ICON_NAME:
2526 g_free (site->icon_data.name.icon_name);
2529 g_assert_not_reached();
2532 site->icon_type = GTK_IMAGE_EMPTY;
2535 g_object_unref (site->colormap);
2536 site->colormap = NULL;
2540 * gtk_drag_source_set_icon:
2541 * @widget: a #GtkWidget
2542 * @colormap: the colormap of the icon
2543 * @pixmap: the image data for the icon
2544 * @mask: the transparency mask for an image.
2546 * Sets the icon that will be used for drags from a particular widget
2547 * from a pixmap/mask. GTK+ retains references for the arguments, and
2548 * will release them when they are no longer needed.
2549 * Use gtk_drag_source_set_icon_pixbuf() instead.
2552 gtk_drag_source_set_icon (GtkWidget *widget,
2553 GdkColormap *colormap,
2557 GtkDragSourceSite *site;
2559 g_return_if_fail (GTK_IS_WIDGET (widget));
2560 g_return_if_fail (GDK_IS_COLORMAP (colormap));
2561 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
2562 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
2564 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2565 g_return_if_fail (site != NULL);
2567 g_object_ref (colormap);
2568 g_object_ref (pixmap);
2570 g_object_ref (mask);
2572 gtk_drag_source_unset_icon (site);
2574 site->icon_type = GTK_IMAGE_PIXMAP;
2576 site->icon_data.pixmap.pixmap = pixmap;
2577 site->icon_mask = mask;
2578 site->colormap = colormap;
2582 * gtk_drag_source_set_icon_pixbuf:
2583 * @widget: a #GtkWidget
2584 * @pixbuf: the #GdkPixbuf for the drag icon
2586 * Sets the icon that will be used for drags from a particular widget
2587 * from a #GdkPixbuf. GTK+ retains a reference for @pixbuf and will
2588 * release it when it is no longer needed.
2591 gtk_drag_source_set_icon_pixbuf (GtkWidget *widget,
2594 GtkDragSourceSite *site;
2596 g_return_if_fail (GTK_IS_WIDGET (widget));
2597 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2599 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2600 g_return_if_fail (site != NULL);
2601 g_object_ref (pixbuf);
2603 gtk_drag_source_unset_icon (site);
2605 site->icon_type = GTK_IMAGE_PIXBUF;
2606 site->icon_data.pixbuf.pixbuf = pixbuf;
2610 * gtk_drag_source_set_icon_stock:
2611 * @widget: a #GtkWidget
2612 * @stock_id: the ID of the stock icon to use
2614 * Sets the icon that will be used for drags from a particular source
2618 gtk_drag_source_set_icon_stock (GtkWidget *widget,
2619 const gchar *stock_id)
2621 GtkDragSourceSite *site;
2623 g_return_if_fail (GTK_IS_WIDGET (widget));
2624 g_return_if_fail (stock_id != NULL);
2626 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2627 g_return_if_fail (site != NULL);
2629 gtk_drag_source_unset_icon (site);
2631 site->icon_type = GTK_IMAGE_STOCK;
2632 site->icon_data.stock.stock_id = g_strdup (stock_id);
2636 * gtk_drag_source_set_icon_name:
2637 * @widget: a #GtkWidget
2638 * @icon_name: name of icon to use
2640 * Sets the icon that will be used for drags from a particular source
2641 * to a themed icon. See the docs for #GtkIconTheme for more details.
2646 gtk_drag_source_set_icon_name (GtkWidget *widget,
2647 const gchar *icon_name)
2649 GtkDragSourceSite *site;
2651 g_return_if_fail (GTK_IS_WIDGET (widget));
2652 g_return_if_fail (icon_name != NULL);
2654 site = g_object_get_data (G_OBJECT (widget), "gtk-site-data");
2655 g_return_if_fail (site != NULL);
2657 gtk_drag_source_unset_icon (site);
2659 site->icon_type = GTK_IMAGE_ICON_NAME;
2660 site->icon_data.name.icon_name = g_strdup (icon_name);
2664 gtk_drag_get_icon (GtkDragSourceInfo *info,
2665 GtkWidget **icon_window,
2669 if (get_can_change_screen (info->icon_window))
2670 gtk_window_set_screen (GTK_WINDOW (info->icon_window),
2673 if (gtk_widget_get_screen (info->icon_window) != info->cur_screen)
2675 if (!info->fallback_icon)
2677 gint save_hot_x, save_hot_y;
2678 gboolean save_destroy_icon;
2679 GtkWidget *save_icon_window;
2681 /* HACK to get the appropriate icon
2683 save_icon_window = info->icon_window;
2684 save_hot_x = info->hot_x;
2685 save_hot_y = info->hot_x;
2686 save_destroy_icon = info->destroy_icon;
2688 info->icon_window = NULL;
2689 gtk_drag_set_icon_default (info->context);
2690 info->fallback_icon = info->icon_window;
2692 info->icon_window = save_icon_window;
2693 info->hot_x = save_hot_x;
2694 info->hot_y = save_hot_y;
2695 info->destroy_icon = save_destroy_icon;
2698 gtk_widget_hide (info->icon_window);
2700 *icon_window = info->fallback_icon;
2701 gtk_window_set_screen (GTK_WINDOW (*icon_window), info->cur_screen);
2703 if (!default_icon_pixmap)
2710 *hot_x = default_icon_hot_x;
2711 *hot_y = default_icon_hot_y;
2716 if (info->fallback_icon)
2717 gtk_widget_hide (info->fallback_icon);
2719 *icon_window = info->icon_window;
2720 *hot_x = info->hot_x;
2721 *hot_y = info->hot_y;
2726 gtk_drag_update_icon (GtkDragSourceInfo *info)
2728 if (info->icon_window)
2730 GtkWidget *icon_window;
2733 gtk_drag_get_icon (info, &icon_window, &hot_x, &hot_y);
2735 gtk_window_move (GTK_WINDOW (icon_window),
2736 info->cur_x - hot_x,
2737 info->cur_y - hot_y);
2739 if (GTK_WIDGET_VISIBLE (icon_window))
2740 gdk_window_raise (icon_window->window);
2742 gtk_widget_show (icon_window);
2747 gtk_drag_set_icon_window (GdkDragContext *context,
2751 gboolean destroy_on_release)
2753 GtkDragSourceInfo *info;
2755 info = gtk_drag_get_source_info (context, FALSE);
2756 gtk_drag_remove_icon (info);
2759 gtk_widget_ref (widget);
2761 info->icon_window = widget;
2762 info->hot_x = hot_x;
2763 info->hot_y = hot_y;
2764 info->destroy_icon = destroy_on_release;
2766 if (widget && info->icon_pixbuf)
2768 g_object_unref (info->icon_pixbuf);
2769 info->icon_pixbuf = NULL;
2772 gtk_drag_update_cursor (info);
2773 gtk_drag_update_icon (info);
2777 * gtk_drag_set_icon_widget:
2778 * @context: the context for a drag. (This must be called
2779 with a context for the source side of a drag)
2780 * @widget: a toplevel window to use as an icon.
2781 * @hot_x: the X offset within @widget of the hotspot.
2782 * @hot_y: the Y offset within @widget of the hotspot.
2784 * Changes the icon for a widget to a given widget. GTK+
2785 * will not destroy the icon, so if you don't want
2786 * it to persist, you should connect to the "drag_end"
2787 * signal and destroy it yourself.
2790 gtk_drag_set_icon_widget (GdkDragContext *context,
2795 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
2796 g_return_if_fail (context->is_source);
2797 g_return_if_fail (GTK_IS_WIDGET (widget));
2799 gtk_drag_set_icon_window (context, widget, hot_x, hot_y, FALSE);
2803 icon_window_realize (GtkWidget *window,
2809 gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
2810 gtk_widget_get_colormap (window),
2811 &pixmap, &mask, 128);
2813 gdk_window_set_back_pixmap (window->window, pixmap, FALSE);
2814 g_object_unref (pixmap);
2818 gtk_widget_shape_combine_mask (window, mask, 0, 0);
2819 g_object_unref (mask);
2824 set_icon_stock_pixbuf (GdkDragContext *context,
2825 const gchar *stock_id,
2829 gboolean force_window)
2834 GdkDisplay *display;
2836 g_return_if_fail (context != NULL);
2837 g_return_if_fail (pixbuf != NULL || stock_id != NULL);
2838 g_return_if_fail (pixbuf == NULL || stock_id == NULL);
2840 screen = gdk_drawable_get_screen (context->source_window);
2842 /* Push a NULL colormap to guard against gtk_widget_push_colormap() */
2843 gtk_widget_push_colormap (NULL);
2844 window = gtk_window_new (GTK_WINDOW_POPUP);
2845 gtk_window_set_screen (GTK_WINDOW (window), screen);
2846 set_can_change_screen (window, TRUE);
2847 gtk_widget_pop_colormap ();
2849 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
2850 gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
2854 pixbuf = gtk_widget_render_icon (window, stock_id,
2855 GTK_ICON_SIZE_DND, NULL);
2859 g_warning ("Cannot load drag icon from stock_id %s", stock_id);
2860 gtk_widget_destroy (window);
2866 g_object_ref (pixbuf);
2868 display = gdk_drawable_get_display (context->source_window);
2869 width = gdk_pixbuf_get_width (pixbuf);
2870 height = gdk_pixbuf_get_height (pixbuf);
2872 if (!force_window &&
2873 gtk_drag_can_use_rgba_cursor (display, width + 2, height + 2))
2875 GtkDragSourceInfo *info;
2877 gtk_widget_destroy (window);
2879 info = gtk_drag_get_source_info (context, FALSE);
2881 if (info->icon_pixbuf)
2882 g_object_unref (info->icon_pixbuf);
2883 info->icon_pixbuf = pixbuf;
2885 gtk_drag_set_icon_window (context, NULL, hot_x, hot_y, TRUE);
2889 gtk_widget_set_size_request (window, width, height);
2891 g_signal_connect_closure (window, "realize",
2892 g_cclosure_new (G_CALLBACK (icon_window_realize),
2894 (GClosureNotify)g_object_unref),
2897 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
2902 * gtk_drag_set_icon_pixbuf:
2903 * @context: the context for a drag. (This must be called
2904 * with a context for the source side of a drag)
2905 * @pixbuf: the #GdkPixbuf to use as the drag icon.
2906 * @hot_x: the X offset within @widget of the hotspot.
2907 * @hot_y: the Y offset within @widget of the hotspot.
2909 * Sets @pixbuf as the icon for a given drag.
2912 gtk_drag_set_icon_pixbuf (GdkDragContext *context,
2917 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
2918 g_return_if_fail (context->is_source);
2919 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2921 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
2925 * gtk_drag_set_icon_stock:
2926 * @context: the context for a drag. (This must be called
2927 * with a context for the source side of a drag)
2928 * @stock_id: the ID of the stock icon to use for the drag.
2929 * @hot_x: the X offset within the icon of the hotspot.
2930 * @hot_y: the Y offset within the icon of the hotspot.
2932 * Sets the icon for a given drag from a stock ID.
2935 gtk_drag_set_icon_stock (GdkDragContext *context,
2936 const gchar *stock_id,
2940 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
2941 g_return_if_fail (context->is_source);
2942 g_return_if_fail (stock_id != NULL);
2944 set_icon_stock_pixbuf (context, stock_id, NULL, hot_x, hot_y, FALSE);
2948 * gtk_drag_set_icon_pixmap:
2949 * @context: the context for a drag. (This must be called
2950 * with a context for the source side of a drag)
2951 * @colormap: the colormap of the icon
2952 * @pixmap: the image data for the icon
2953 * @mask: the transparency mask for the icon
2954 * @hot_x: the X offset within @pixmap of the hotspot.
2955 * @hot_y: the Y offset within @pixmap of the hotspot.
2957 * Sets @pixmap as the icon for a given drag. GTK+ retains
2958 * references for the arguments, and will release them when
2959 * they are no longer needed. In general, gtk_drag_set_icon_pixbuf()
2960 * will be more convenient to use.
2963 gtk_drag_set_icon_pixmap (GdkDragContext *context,
2964 GdkColormap *colormap,
2974 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
2975 g_return_if_fail (context->is_source);
2976 g_return_if_fail (GDK_IS_COLORMAP (colormap));
2977 g_return_if_fail (GDK_IS_PIXMAP (pixmap));
2978 g_return_if_fail (!mask || GDK_IS_PIXMAP (mask));
2980 screen = gdk_colormap_get_screen (colormap);
2982 g_return_if_fail (gdk_drawable_get_screen (pixmap) == screen);
2983 g_return_if_fail (!mask || gdk_drawable_get_screen (mask) == screen);
2985 gdk_drawable_get_size (pixmap, &width, &height);
2987 gtk_widget_push_colormap (colormap);
2989 window = gtk_window_new (GTK_WINDOW_POPUP);
2990 gtk_window_set_screen (GTK_WINDOW (window), screen);
2991 set_can_change_screen (window, FALSE);
2992 gtk_widget_set_events (window, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
2993 gtk_widget_set_app_paintable (GTK_WIDGET (window), TRUE);
2995 gtk_widget_pop_colormap ();
2997 gtk_widget_set_size_request (window, width, height);
2998 gtk_widget_realize (window);
3000 gdk_window_set_back_pixmap (window->window, pixmap, FALSE);
3003 gtk_widget_shape_combine_mask (window, mask, 0, 0);
3005 gtk_drag_set_icon_window (context, window, hot_x, hot_y, TRUE);
3009 * gtk_drag_set_icon_name:
3010 * @context: the context for a drag. (This must be called
3011 * with a context for the source side of a drag)
3012 * @icon_name: name of icon to use
3013 * @hot_x: the X offset of the hotspot within the icon
3014 * @hot_y: the Y offset of the hotspot within the icon
3016 * Sets the icon for a given drag from a named themed icon. See
3017 * the docs for #GtkIconTheme for more details. Note that the
3018 * size of the icon depends on the icon theme (the icon is
3019 * loaded at the symbolic size #GTK_ICON_SIZE_DND), thus
3020 * @hot_x and @hot_y have to be used with care.
3025 gtk_drag_set_icon_name (GdkDragContext *context,
3026 const gchar *icon_name,
3031 GtkSettings *settings;
3032 GtkIconTheme *icon_theme;
3034 gint width, height, icon_size;
3036 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
3037 g_return_if_fail (context->is_source);
3038 g_return_if_fail (icon_name != NULL);
3040 screen = gdk_drawable_get_screen (context->source_window);
3041 g_return_if_fail (screen != NULL);
3043 settings = gtk_settings_get_for_screen (screen);
3044 if (gtk_icon_size_lookup_for_settings (settings,
3047 icon_size = MAX (width, height);
3049 icon_size = 32; /* default value for GTK_ICON_SIZE_DND */
3051 icon_theme = gtk_icon_theme_get_for_screen (screen);
3053 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
3054 icon_size, 0, NULL);
3056 set_icon_stock_pixbuf (context, NULL, pixbuf, hot_x, hot_y, FALSE);
3058 g_warning ("Cannot load drag icon from icon name %s", icon_name);
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 gtk_drag_add_update_idle (info);
3207 case GDK_DROP_FINISHED:
3208 gtk_drag_drop_finished (info, TRUE, event->dnd.time);
3211 g_assert_not_reached ();
3215 /*************************************************************
3216 * gtk_drag_source_check_selection:
3217 * Check if we've set up handlers/claimed the selection
3218 * for a given drag. If not, add them.
3222 *************************************************************/
3225 gtk_drag_source_check_selection (GtkDragSourceInfo *info,
3231 tmp_list = info->selections;
3234 if (GDK_POINTER_TO_ATOM (tmp_list->data) == selection)
3236 tmp_list = tmp_list->next;
3239 gtk_selection_owner_set_for_display (gtk_widget_get_display (info->widget),
3243 info->selections = g_list_prepend (info->selections,
3244 GUINT_TO_POINTER (selection));
3246 tmp_list = info->target_list->list;
3249 GtkTargetPair *pair = tmp_list->data;
3251 gtk_selection_add_target (info->ipc_widget,
3255 tmp_list = tmp_list->next;
3258 if (info->context->protocol == GDK_DRAG_PROTO_MOTIF)
3260 gtk_selection_add_target (info->ipc_widget,
3262 gdk_atom_intern_static_string ("XmTRANSFER_SUCCESS"),
3263 TARGET_MOTIF_SUCCESS);
3264 gtk_selection_add_target (info->ipc_widget,
3266 gdk_atom_intern_static_string ("XmTRANSFER_FAILURE"),
3267 TARGET_MOTIF_FAILURE);
3270 gtk_selection_add_target (info->ipc_widget,
3272 gdk_atom_intern_static_string ("DELETE"),
3276 /*************************************************************
3277 * gtk_drag_drop_finished:
3278 * Clean up from the drag, and display snapback, if necessary.
3284 *************************************************************/
3287 gtk_drag_drop_finished (GtkDragSourceInfo *info,
3291 gtk_drag_source_release_selections (info, time);
3293 if (info->proxy_dest)
3295 /* The time from the event isn't reliable for Xdnd drags */
3296 gtk_drag_finish (info->proxy_dest->context, success, FALSE,
3297 info->proxy_dest->proxy_drop_time);
3298 gtk_drag_source_info_destroy (info);
3304 gtk_drag_source_info_destroy (info);
3308 GtkDragAnim *anim = g_new (GtkDragAnim, 1);
3312 anim->n_steps = MAX (info->cur_x - info->start_x,
3313 info->cur_y - info->start_y) / ANIM_STEP_LENGTH;
3314 anim->n_steps = CLAMP (anim->n_steps, ANIM_MIN_STEPS, ANIM_MAX_STEPS);
3316 info->cur_screen = gtk_widget_get_screen (info->widget);
3318 if (!info->icon_window)
3319 set_icon_stock_pixbuf (info->context, NULL, info->icon_pixbuf,
3322 gtk_drag_update_icon (info);
3324 /* Mark the context as dead, so if the destination decides
3325 * to respond really late, we still are OK.
3327 gtk_drag_clear_source_info (info->context);
3328 g_timeout_add (ANIM_STEP_TIME, gtk_drag_anim_timeout, anim);
3334 gtk_drag_source_release_selections (GtkDragSourceInfo *info,
3337 GdkDisplay *display = gtk_widget_get_display (info->widget);
3338 GList *tmp_list = info->selections;
3342 GdkAtom selection = GDK_POINTER_TO_ATOM (tmp_list->data);
3343 if (gdk_selection_owner_get_for_display (display, selection) == info->ipc_widget->window)
3344 gtk_selection_owner_set_for_display (display, NULL, selection, time);
3346 tmp_list = tmp_list->next;
3349 g_list_free (info->selections);
3350 info->selections = NULL;
3353 /*************************************************************
3355 * Send a drop event.
3359 *************************************************************/
3362 gtk_drag_drop (GtkDragSourceInfo *info,
3365 if (info->context->protocol == GDK_DRAG_PROTO_ROOTWIN)
3367 GtkSelectionData selection_data;
3369 /* GTK+ traditionally has used application/x-rootwin-drop, but the
3370 * XDND spec specifies x-rootwindow-drop.
3372 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3373 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3375 tmp_list = info->target_list->list;
3378 GtkTargetPair *pair = tmp_list->data;
3380 if (pair->target == target1 || pair->target == target2)
3382 selection_data.selection = GDK_NONE;
3383 selection_data.target = pair->target;
3384 selection_data.data = NULL;
3385 selection_data.length = -1;
3387 g_signal_emit_by_name (info->widget, "drag_data_get",
3388 info->context, &selection_data,
3392 /* FIXME: Should we check for length >= 0 here? */
3393 gtk_drag_drop_finished (info, TRUE, time);
3396 tmp_list = tmp_list->next;
3398 gtk_drag_drop_finished (info, FALSE, time);
3402 if (info->icon_window)
3403 gtk_widget_hide (info->icon_window);
3405 gdk_drag_drop (info->context, time);
3406 info->drop_timeout = g_timeout_add (DROP_ABORT_TIME,
3407 gtk_drag_abort_timeout,
3413 * Source side callbacks.
3417 gtk_drag_source_event_cb (GtkWidget *widget,
3421 GtkDragSourceSite *site;
3422 gboolean retval = FALSE;
3423 site = (GtkDragSourceSite *)data;
3425 switch (event->type)
3427 case GDK_BUTTON_PRESS:
3428 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3430 site->state |= (GDK_BUTTON1_MASK << (event->button.button - 1));
3431 site->x = event->button.x;
3432 site->y = event->button.y;
3436 case GDK_BUTTON_RELEASE:
3437 if ((GDK_BUTTON1_MASK << (event->button.button - 1)) & site->start_button_mask)
3438 site->state &= ~(GDK_BUTTON1_MASK << (event->button.button - 1));
3441 case GDK_MOTION_NOTIFY:
3442 if (site->state & event->motion.state & site->start_button_mask)
3444 /* FIXME: This is really broken and can leave us
3450 if (site->state & event->motion.state &
3451 GDK_BUTTON1_MASK << (i - 1))
3455 if (gtk_drag_check_threshold (widget, site->x, site->y,
3456 event->motion.x, event->motion.y))
3459 gtk_drag_begin_internal (widget, site, site->target_list,
3468 default: /* hit for 2/3BUTTON_PRESS */
3476 gtk_drag_source_site_destroy (gpointer data)
3478 GtkDragSourceSite *site = data;
3480 if (site->target_list)
3481 gtk_target_list_unref (site->target_list);
3483 gtk_drag_source_unset_icon (site);
3488 gtk_drag_selection_get (GtkWidget *widget,
3489 GtkSelectionData *selection_data,
3494 GtkDragSourceInfo *info = data;
3495 static GdkAtom null_atom = GDK_NONE;
3499 null_atom = gdk_atom_intern_static_string ("NULL");
3504 g_signal_emit_by_name (info->widget,
3507 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3509 case TARGET_MOTIF_SUCCESS:
3510 gtk_drag_drop_finished (info, TRUE, time);
3511 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3513 case TARGET_MOTIF_FAILURE:
3514 gtk_drag_drop_finished (info, FALSE, time);
3515 gtk_selection_data_set (selection_data, null_atom, 8, NULL, 0);
3518 if (info->proxy_dest)
3520 /* This is sort of dangerous and needs to be thought
3523 info->proxy_dest->proxy_data = selection_data;
3524 gtk_drag_get_data (info->widget,
3525 info->proxy_dest->context,
3526 selection_data->target,
3529 info->proxy_dest->proxy_data = NULL;
3533 if (gtk_target_list_find (info->target_list,
3534 selection_data->target,
3537 g_signal_emit_by_name (info->widget, "drag_data_get",
3549 gtk_drag_anim_timeout (gpointer data)
3551 GtkDragAnim *anim = data;
3555 GDK_THREADS_ENTER ();
3557 if (anim->step == anim->n_steps)
3559 gtk_drag_source_info_destroy (anim->info);
3566 x = (anim->info->start_x * (anim->step + 1) +
3567 anim->info->cur_x * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3568 y = (anim->info->start_y * (anim->step + 1) +
3569 anim->info->cur_y * (anim->n_steps - anim->step - 1)) / anim->n_steps;
3570 if (anim->info->icon_window)
3572 GtkWidget *icon_window;
3575 gtk_drag_get_icon (anim->info, &icon_window, &hot_x, &hot_y);
3576 gtk_window_move (GTK_WINDOW (icon_window),
3586 GDK_THREADS_LEAVE ();
3592 gtk_drag_remove_icon (GtkDragSourceInfo *info)
3594 if (info->icon_window)
3596 gtk_widget_hide (info->icon_window);
3597 if (info->destroy_icon)
3598 gtk_widget_destroy (info->icon_window);
3600 if (info->fallback_icon)
3602 gtk_widget_destroy (info->fallback_icon);
3603 info->fallback_icon = NULL;
3606 g_object_unref (info->icon_window);
3607 info->icon_window = NULL;
3612 gtk_drag_source_info_destroy (GtkDragSourceInfo *info)
3616 for (i = 0; i < n_drag_cursors; i++)
3618 if (info->drag_cursors[i] != NULL)
3620 gdk_cursor_unref (info->drag_cursors[i]);
3621 info->drag_cursors[i] = NULL;
3625 gtk_drag_remove_icon (info);
3627 if (info->icon_pixbuf)
3628 g_object_unref (info->icon_pixbuf);
3630 if (!info->proxy_dest)
3631 g_signal_emit_by_name (info->widget, "drag_end",
3635 g_object_unref (info->widget);
3638 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3639 gtk_drag_button_release_cb,
3641 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3644 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3647 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3648 gtk_drag_selection_get,
3651 gtk_selection_remove_all (info->ipc_widget);
3652 g_object_set_data (G_OBJECT (info->ipc_widget), I_("gtk-info"), NULL);
3653 source_widgets = g_slist_remove (source_widgets, info->ipc_widget);
3654 gtk_drag_release_ipc_widget (info->ipc_widget);
3656 gtk_target_list_unref (info->target_list);
3658 gtk_drag_clear_source_info (info->context);
3659 g_object_unref (info->context);
3661 if (info->drop_timeout)
3662 g_source_remove (info->drop_timeout);
3668 gtk_drag_update_idle (gpointer data)
3670 GtkDragSourceInfo *info = data;
3671 GdkWindow *dest_window;
3672 GdkDragProtocol protocol;
3675 GdkDragAction action;
3676 GdkDragAction possible_actions;
3679 GDK_THREADS_ENTER ();
3681 info->update_idle = 0;
3683 if (info->last_event)
3685 time = gtk_drag_get_event_time (info->last_event);
3686 gtk_drag_get_event_actions (info->last_event,
3688 info->possible_actions,
3689 &action, &possible_actions);
3690 gtk_drag_update_icon (info);
3691 gdk_drag_find_window_for_screen (info->context,
3692 info->icon_window ? info->icon_window->window : NULL,
3693 info->cur_screen, info->cur_x, info->cur_y,
3694 &dest_window, &protocol);
3696 if (!gdk_drag_motion (info->context, dest_window, protocol,
3697 info->cur_x, info->cur_y, action,
3701 gdk_event_free ((GdkEvent *)info->last_event);
3702 info->last_event = NULL;
3706 g_object_unref (dest_window);
3708 selection = gdk_drag_get_selection (info->context);
3710 gtk_drag_source_check_selection (info, selection, time);
3714 GDK_THREADS_LEAVE ();
3720 gtk_drag_add_update_idle (GtkDragSourceInfo *info)
3722 /* We use an idle lowerthan GDK_PRIORITY_REDRAW so that exposes
3723 * from the last move can catch up before we move again.
3725 if (!info->update_idle)
3726 info->update_idle = g_idle_add_full (GDK_PRIORITY_REDRAW + 5,
3727 gtk_drag_update_idle,
3734 * @info: DragSourceInfo for the drag
3735 * @screen: new screen
3736 * @x_root: new X position
3737 * @y_root: new y position
3738 * @event: event received requiring update
3740 * Updates the status of the drag; called when the
3741 * cursor moves or the modifier changes
3744 gtk_drag_update (GtkDragSourceInfo *info,
3750 info->cur_screen = screen;
3751 info->cur_x = x_root;
3752 info->cur_y = y_root;
3753 if (info->last_event)
3755 gdk_event_free ((GdkEvent *)info->last_event);
3756 info->last_event = NULL;
3759 info->last_event = gdk_event_copy ((GdkEvent *)event);
3761 gtk_drag_add_update_idle (info);
3764 /*************************************************************
3766 * Called when the user finishes to drag, either by
3767 * releasing the mouse, or by pressing Esc.
3769 * info: Source info for the drag
3770 * time: Timestamp for ending the drag
3772 *************************************************************/
3775 gtk_drag_end (GtkDragSourceInfo *info, guint32 time)
3777 GdkEvent *send_event;
3778 GtkWidget *source_widget = info->widget;
3779 GdkDisplay *display = gtk_widget_get_display (source_widget);
3781 if (info->update_idle)
3783 g_source_remove (info->update_idle);
3784 info->update_idle = 0;
3787 if (info->last_event)
3789 gdk_event_free (info->last_event);
3790 info->last_event = NULL;
3793 info->have_grab = FALSE;
3795 gdk_display_pointer_ungrab (display, time);
3796 gdk_display_keyboard_ungrab (display, time);
3797 gtk_grab_remove (info->ipc_widget);
3799 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3800 gtk_drag_button_release_cb,
3802 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3805 g_signal_handlers_disconnect_by_func (info->ipc_widget,
3809 /* Send on a release pair to the original
3810 * widget to convince it to release its grab. We need to
3811 * call gtk_propagate_event() here, instead of
3812 * gtk_widget_event() because widget like GtkList may
3813 * expect propagation.
3816 send_event = gdk_event_new (GDK_BUTTON_RELEASE);
3817 send_event->button.window = g_object_ref (gtk_widget_get_root_window (source_widget));
3818 send_event->button.send_event = TRUE;
3819 send_event->button.time = time;
3820 send_event->button.x = 0;
3821 send_event->button.y = 0;
3822 send_event->button.axes = NULL;
3823 send_event->button.state = 0;
3824 send_event->button.button = info->button;
3825 send_event->button.device = gdk_display_get_core_pointer (display);
3826 send_event->button.x_root = 0;
3827 send_event->button.y_root = 0;
3829 gtk_propagate_event (source_widget, send_event);
3830 gdk_event_free (send_event);
3833 /*************************************************************
3835 * Called on cancellation of a drag, either by the user
3836 * or programmatically.
3838 * info: Source info for the drag
3839 * time: Timestamp for ending the drag
3841 *************************************************************/
3844 gtk_drag_cancel (GtkDragSourceInfo *info, guint32 time)
3846 gtk_drag_end (info, time);
3847 gdk_drag_abort (info->context, time);
3848 gtk_drag_drop_finished (info, FALSE, time);
3851 /*************************************************************
3852 * gtk_drag_motion_cb:
3853 * "motion_notify_event" callback during drag.
3857 *************************************************************/
3860 gtk_drag_motion_cb (GtkWidget *widget,
3861 GdkEventMotion *event,
3864 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
3866 gint x_root, y_root;
3870 GdkDisplay *display = gtk_widget_get_display (widget);
3872 gdk_display_get_pointer (display, &screen, &x_root, &y_root, NULL);
3873 event->x_root = x_root;
3874 event->y_root = y_root;
3877 screen = gdk_event_get_screen ((GdkEvent *)event);
3879 gtk_drag_update (info, screen, event->x_root, event->y_root, (GdkEvent *)event);
3884 /*************************************************************
3886 * "key_press/release_event" callback during drag.
3890 *************************************************************/
3893 #define SMALL_STEP 1
3896 gtk_drag_key_cb (GtkWidget *widget,
3900 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
3901 GdkModifierType state;
3902 GdkWindow *root_window;
3906 state = event->state & gtk_accelerator_get_default_mod_mask ();
3908 if (event->type == GDK_KEY_PRESS)
3910 switch (event->keyval)
3913 gtk_drag_cancel (info, event->time);
3920 gtk_drag_end (info, event->time);
3921 gtk_drag_drop (info, event->time);
3926 dy = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
3931 dy = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
3936 dx = (state & GDK_MOD1_MASK) ? -BIG_STEP : -SMALL_STEP;
3941 dx = (state & GDK_MOD1_MASK) ? BIG_STEP : SMALL_STEP;
3947 /* Now send a "motion" so that the modifier state is updated */
3949 /* The state is not yet updated in the event, so we need
3950 * to query it here. We could use XGetModifierMapping, but
3951 * that would be overkill.
3953 root_window = gtk_widget_get_root_window (widget);
3954 gdk_window_get_pointer (root_window, NULL, NULL, &state);
3955 event->state = state;
3957 if (dx != 0 || dy != 0)
3961 gdk_display_warp_pointer (gtk_widget_get_display (widget),
3962 gtk_widget_get_screen (widget),
3963 info->cur_x, info->cur_y);
3966 gtk_drag_update (info, info->cur_screen, info->cur_x, info->cur_y, (GdkEvent *)event);
3971 /*************************************************************
3972 * gtk_drag_button_release_cb:
3973 * "button_release_event" callback during drag.
3977 *************************************************************/
3980 gtk_drag_button_release_cb (GtkWidget *widget,
3981 GdkEventButton *event,
3984 GtkDragSourceInfo *info = (GtkDragSourceInfo *)data;
3986 if (event->button != info->button)
3989 if ((info->context->action != 0) && (info->context->dest_window != NULL))
3991 gtk_drag_end (info, event->time);
3992 gtk_drag_drop (info, event->time);
3996 gtk_drag_cancel (info, event->time);
4003 gtk_drag_abort_timeout (gpointer data)
4005 GtkDragSourceInfo *info = data;
4006 guint32 time = GDK_CURRENT_TIME;
4008 GDK_THREADS_ENTER ();
4010 if (info->proxy_dest)
4011 time = info->proxy_dest->proxy_drop_time;
4013 info->drop_timeout = 0;
4014 gtk_drag_drop_finished (info, FALSE, time);
4016 GDK_THREADS_LEAVE ();
4022 * gtk_drag_check_threshold:
4023 * @widget: a #GtkWidget
4024 * @start_x: X coordinate of start of drag
4025 * @start_y: Y coordinate of start of drag
4026 * @current_x: current X coordinate
4027 * @current_y: current Y coordinate
4029 * Checks to see if a mouse drag starting at (@start_x, @start_y) and ending
4030 * at (@current_x, @current_y) has passed the GTK+ drag threshold, and thus
4031 * should trigger the beginning of a drag-and-drop operation.
4033 * Return Value: %TRUE if the drag threshold has been passed.
4036 gtk_drag_check_threshold (GtkWidget *widget,
4042 gint drag_threshold;
4044 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4046 g_object_get (gtk_widget_get_settings (widget),
4047 "gtk-dnd-drag-threshold", &drag_threshold,
4050 return (ABS (current_x - start_x) > drag_threshold ||
4051 ABS (current_y - start_y) > drag_threshold);
4054 #define __GTK_DND_C__
4055 #include "gtkaliasdef.c"