1 /* GDK - The GIMP Drawing Kit
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/.
30 #include <X11/Xutil.h>
31 #include <X11/Xatom.h>
32 #include <X11/extensions/shape.h>
36 #include "gdk.h" /* For gdk_flush() */
40 #include "gdkproperty.h"
41 #include "gdkprivate-x11.h"
42 #include "gdkinternals.h"
43 #include "gdkscreen-x11.h"
44 #include "gdkdisplay-x11.h"
47 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
51 GDK_DRAG_STATUS_MOTION_WAIT,
52 GDK_DRAG_STATUS_ACTION_WAIT,
58 gint x, y, width, height;
60 gboolean shape_selected;
67 GHashTable *child_hash;
72 /* Structure that holds information about a drag in progress.
73 * this is used on both source and destination sides.
75 struct _GdkDragContextPrivateX11 {
76 GdkDragContext context;
81 guint16 last_x; /* Coordinates from last event */
83 GdkDragAction old_action; /* The last action we sent to the source */
84 GdkDragAction old_actions; /* The last actions we sent to the source */
85 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
87 Window dest_xid; /* The last window we looked up */
88 Window drop_xid; /* The (non-proxied) window that is receiving drops */
89 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
90 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
91 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
92 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
93 guint drag_status : 4; /* current status of drag */
95 guint drop_failed : 1; /* Whether the drop was unsuccessful */
96 guint version; /* Xdnd protocol version */
98 GSList *window_caches;
102 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
104 /* Forward declarations */
106 static void gdk_window_cache_destroy (GdkWindowCache *cache);
108 static void motif_read_target_table (GdkDisplay *display);
110 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
114 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
117 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
120 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
123 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
126 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
129 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
133 static void xdnd_manage_source_filter (GdkDragContext *context,
135 gboolean add_filter);
137 static void gdk_drag_context_finalize (GObject *object);
139 static GList *contexts;
141 static const struct {
142 const char *atom_name;
145 { "XdndEnter", xdnd_enter_filter },
146 { "XdndLeave", xdnd_leave_filter },
147 { "XdndPosition", xdnd_position_filter },
148 { "XdndStatus", xdnd_status_filter },
149 { "XdndFinished", xdnd_finished_filter },
150 { "XdndDrop", xdnd_drop_filter },
153 G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
156 gdk_drag_context_init (GdkDragContext *dragcontext)
158 GdkDragContextPrivateX11 *private;
160 private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext,
161 GDK_TYPE_DRAG_CONTEXT,
162 GdkDragContextPrivateX11);
164 dragcontext->windowing_data = private;
166 contexts = g_list_prepend (contexts, dragcontext);
170 gdk_drag_context_class_init (GdkDragContextClass *klass)
172 GObjectClass *object_class = G_OBJECT_CLASS (klass);
174 object_class->finalize = gdk_drag_context_finalize;
176 g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateX11));
180 gdk_drag_context_finalize (GObject *object)
182 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
183 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
186 g_list_free (context->targets);
188 if (context->source_window)
190 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
192 xdnd_manage_source_filter (context, context->source_window, FALSE);
194 g_object_unref (context->source_window);
197 if (context->dest_window)
198 g_object_unref (context->dest_window);
200 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
201 gdk_window_cache_destroy (tmp_list->data);
202 g_slist_free (private->window_caches);
204 contexts = g_list_remove (contexts, context);
206 G_OBJECT_CLASS (gdk_drag_context_parent_class)->finalize (object);
212 * gdk_drag_context_new:
214 * Creates a new #GdkDragContext.
216 * Return value: the newly created #GdkDragContext.
219 gdk_drag_context_new (void)
221 return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
225 * gdk_drag_context_set_device:
226 * @context: a #GdkDragContext
227 * @device: a #GdkDevice
229 * Associates a #GdkDevice to @context, so all Drag and Drop events
230 * for @context are emitted as if they came from this device.
233 gdk_drag_context_set_device (GdkDragContext *context,
236 GdkDragContextPrivateX11 *private;
238 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
239 g_return_if_fail (GDK_IS_DEVICE (device));
241 private = PRIVATE_DATA (context);
245 g_object_unref (private->device);
246 private->device = NULL;
250 private->device = g_object_ref (device);
254 * gdk_drag_context_get_device:
255 * @context: a #GdkDragContext
257 * Returns the #GdkDevice associated to the drag context.
259 * Returns: The #GdkDevice associated to @context.
262 gdk_drag_context_get_device (GdkDragContext *context)
264 GdkDragContextPrivateX11 *private;
266 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
268 private = PRIVATE_DATA (context);
270 return private->device;
273 static GdkDragContext *
274 gdk_drag_context_find (GdkDisplay *display,
279 GList *tmp_list = contexts;
280 GdkDragContext *context;
281 GdkDragContextPrivateX11 *private;
282 Window context_dest_xid;
286 context = (GdkDragContext *)tmp_list->data;
287 private = PRIVATE_DATA (context);
289 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
290 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
293 context_dest_xid = context->dest_window ?
296 GDK_DRAWABLE_XID (context->dest_window)) :
299 if ((!context->is_source == !is_source) &&
300 ((source_xid == None) || (context->source_window &&
301 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
302 ((dest_xid == None) || (context_dest_xid == dest_xid)))
305 tmp_list = tmp_list->next;
312 precache_target_list (GdkDragContext *context)
314 if (context->targets)
316 GPtrArray *targets = g_ptr_array_new ();
320 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
321 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
323 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
324 (const gchar **)targets->pdata,
327 for (i =0; i < targets->len; i++)
328 g_free (targets->pdata[i]);
330 g_ptr_array_free (targets, TRUE);
334 /* Utility functions */
337 free_cache_child (GdkCacheChild *child,
341 gdk_region_destroy (child->shape);
343 if (child->shape_selected && display)
345 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
347 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
354 gdk_window_cache_add (GdkWindowCache *cache,
356 gint x, gint y, gint width, gint height,
359 GdkCacheChild *child = g_new (GdkCacheChild, 1);
364 child->width = width;
365 child->height = height;
366 child->mapped = mapped;
367 child->shape_selected = FALSE;
368 child->shape_valid = FALSE;
371 cache->children = g_list_prepend (cache->children, child);
372 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
376 static GdkFilterReturn
377 gdk_window_cache_shape_filter (GdkXEvent *xev,
381 XEvent *xevent = (XEvent *)xev;
382 GdkWindowCache *cache = data;
384 GdkDisplayX11 *display = GDK_DISPLAY_X11 (gdk_screen_get_display (cache->screen));
386 if (display->have_shapes &&
387 xevent->type == display->shape_event_base + ShapeNotify)
389 XShapeEvent *xse = (XShapeEvent*)xevent;
392 node = g_hash_table_lookup (cache->child_hash,
393 GUINT_TO_POINTER (xse->window));
396 GdkCacheChild *child = node->data;
397 child->shape_valid = FALSE;
400 gdk_region_destroy (child->shape);
405 return GDK_FILTER_REMOVE;
408 return GDK_FILTER_CONTINUE;
411 static GdkFilterReturn
412 gdk_window_cache_filter (GdkXEvent *xev,
416 XEvent *xevent = (XEvent *)xev;
417 GdkWindowCache *cache = data;
419 switch (xevent->type)
421 case CirculateNotify:
423 case ConfigureNotify:
425 XConfigureEvent *xce = &xevent->xconfigure;
428 node = g_hash_table_lookup (cache->child_hash,
429 GUINT_TO_POINTER (xce->window));
432 GdkCacheChild *child = node->data;
435 child->width = xce->width;
436 child->height = xce->height;
437 if (xce->above == None && (node->next))
439 GList *last = g_list_last (cache->children);
440 cache->children = g_list_remove_link (cache->children, node);
447 GList *above_node = g_hash_table_lookup (cache->child_hash,
448 GUINT_TO_POINTER (xce->above));
449 if (above_node && node->next != above_node)
451 /* Put the window above (before in the list) above_node
453 cache->children = g_list_remove_link (cache->children, node);
454 node->prev = above_node->prev;
456 node->prev->next = node;
458 cache->children = node;
459 node->next = above_node;
460 above_node->prev = node;
468 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
470 if (!g_hash_table_lookup (cache->child_hash,
471 GUINT_TO_POINTER (xcwe->window)))
472 gdk_window_cache_add (cache, xcwe->window,
473 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
479 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
482 node = g_hash_table_lookup (cache->child_hash,
483 GUINT_TO_POINTER (xdwe->window));
486 GdkCacheChild *child = node->data;
488 g_hash_table_remove (cache->child_hash,
489 GUINT_TO_POINTER (xdwe->window));
490 cache->children = g_list_remove_link (cache->children, node);
491 /* window is destroyed, no need to disable ShapeNotify */
492 free_cache_child (child, NULL);
493 g_list_free_1 (node);
499 XMapEvent *xme = &xevent->xmap;
502 node = g_hash_table_lookup (cache->child_hash,
503 GUINT_TO_POINTER (xme->window));
506 GdkCacheChild *child = node->data;
507 child->mapped = TRUE;
515 XMapEvent *xume = &xevent->xmap;
518 node = g_hash_table_lookup (cache->child_hash,
519 GUINT_TO_POINTER (xume->window));
522 GdkCacheChild *child = node->data;
523 child->mapped = FALSE;
528 return GDK_FILTER_CONTINUE;
530 return GDK_FILTER_REMOVE;
533 static GdkWindowCache *
534 gdk_window_cache_new (GdkScreen *screen)
536 XWindowAttributes xwa;
537 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
538 GdkWindow *root_window = gdk_screen_get_root_window (screen);
539 GdkChildInfoX11 *children;
542 GdkWindowCache *result = g_new (GdkWindowCache, 1);
544 result->children = NULL;
545 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
546 result->screen = screen;
548 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
549 result->old_event_mask = xwa.your_event_mask;
551 if (G_UNLIKELY (!GDK_DISPLAY_X11 (GDK_SCREEN_X11 (screen)->display)->trusted_client))
553 GList *toplevel_windows, *list;
555 gint x, y, width, height;
557 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
558 for (list = toplevel_windows; list; list = list->next) {
559 window = GDK_WINDOW (list->data);
560 gdk_window_get_geometry (window, &x, &y, &width, &height, NULL);
561 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
563 gdk_window_is_visible (window));
565 g_list_free (toplevel_windows);
569 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
570 result->old_event_mask | SubstructureNotifyMask);
571 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
572 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
574 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
575 GDK_WINDOW_XWINDOW (root_window),
577 &children, &nchildren))
580 for (i = 0; i < nchildren ; i++)
582 gdk_window_cache_add (result, children[i].window,
583 children[i].x, children[i].y, children[i].width, children[i].height,
584 children[i].is_mapped);
593 gdk_window_cache_destroy (GdkWindowCache *cache)
595 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
597 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
598 GDK_WINDOW_XWINDOW (root_window),
599 cache->old_event_mask);
600 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
601 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
603 gdk_error_trap_push ();
605 g_list_foreach (cache->children, (GFunc)free_cache_child,
606 gdk_screen_get_display (cache->screen));
609 gdk_error_trap_pop ();
611 g_list_free (cache->children);
612 g_hash_table_destroy (cache->child_hash);
618 is_pointer_within_shape (GdkDisplay *display,
619 GdkCacheChild *child,
623 if (!child->shape_selected)
625 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
627 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
628 child->shape_selected = TRUE;
630 if (!child->shape_valid)
632 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
633 GdkRegion *input_shape;
635 child->shape = _xwindow_get_shape (display_x11->xdisplay,
636 child->xid, ShapeBounding);
638 input_shape = _xwindow_get_shape (display_x11->xdisplay,
639 child->xid, ShapeInput);
640 if (child->shape && input_shape)
642 gdk_region_intersect (child->shape, input_shape);
643 gdk_region_destroy (input_shape);
645 else if (input_shape)
647 child->shape = input_shape;
651 child->shape_valid = TRUE;
654 return child->shape == NULL ||
655 gdk_region_point_in (child->shape, x_pos, y_pos);
659 get_client_window_at_coords_recurse (GdkDisplay *display,
661 gboolean is_toplevel,
665 GdkChildInfoX11 *children;
666 unsigned int nchildren;
668 gboolean found_child = FALSE;
669 GdkChildInfoX11 child = { 0, };
670 gboolean has_wm_state = FALSE;
672 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
673 is_toplevel? &has_wm_state : NULL,
674 &children, &nchildren))
684 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
686 GdkChildInfoX11 *cur_child = &children[i];
688 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
689 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
690 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
703 if (child.has_wm_state)
706 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
713 get_client_window_at_coords (GdkWindowCache *cache,
719 Window retval = None;
721 gdk_error_trap_push ();
723 tmp_list = cache->children;
725 while (tmp_list && !retval)
727 GdkCacheChild *child = tmp_list->data;
729 if ((child->xid != ignore) && (child->mapped))
731 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
732 (y_root >= child->y) && (y_root < child->y + child->height))
734 GdkDisplay *display = gdk_screen_get_display (cache->screen);
736 if (!is_pointer_within_shape (display, child,
740 tmp_list = tmp_list->next;
744 retval = get_client_window_at_coords_recurse (display,
752 tmp_list = tmp_list->next;
755 gdk_error_trap_pop ();
760 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
763 /*************************************************************
764 ***************************** MOTIF *************************
765 *************************************************************/
767 /* values used in the message type for Motif DND */
780 /* Values used to specify type of protocol to use */
784 XmDRAG_PREFER_PREREGISTER,
786 XmDRAG_PREFER_DYNAMIC,
788 XmDRAG_PREFER_RECEIVER
791 /* Operation codes */
799 /* Drop site status */
801 XmNO_DROP_SITE = 0x01,
802 XmDROP_SITE_INVALID = 0x02,
803 XmDROP_SITE_VALID = 0x03
806 /* completion status */
814 /* Byte swapping routines. The motif specification leaves it
815 * up to us to save a few bytes in the client messages
817 static gchar local_byte_order = '\0';
819 #ifdef G_ENABLE_DEBUG
821 print_target_list (GList *targets)
825 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
826 g_message ("\t%s", name);
828 targets = targets->next;
831 #endif /* G_ENABLE_DEBUG */
834 init_byte_order (void)
836 guint32 myint = 0x01020304;
837 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
841 card16_to_host (guint16 x, gchar byte_order) {
842 if (byte_order == local_byte_order)
845 return (x << 8) | (x >> 8);
849 card32_to_host (guint32 x, gchar byte_order) {
850 if (byte_order == local_byte_order)
853 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
856 /* Motif packs together fields of varying length into the
857 * client message. We can't rely on accessing these
858 * through data.s[], data.l[], etc, because on some architectures
859 * (i.e., Alpha) these won't be valid for format == 8.
862 #define MOTIF_XCLIENT_BYTE(xevent,i) \
863 (xevent)->xclient.data.b[i]
864 #define MOTIF_XCLIENT_SHORT(xevent,i) \
865 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
866 #define MOTIF_XCLIENT_LONG(xevent,i) \
867 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
869 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
870 #define MOTIF_UNPACK_SHORT(xevent,i) \
871 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
872 #define MOTIF_UNPACK_LONG(xevent,i) \
873 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
875 /***** Dest side ***********/
877 /* Property placed on source windows */
878 typedef struct _MotifDragInitiatorInfo {
880 guint8 protocol_version;
881 guint16 targets_index;
882 guint32 selection_atom;
883 } MotifDragInitiatorInfo;
885 /* Header for target table on the drag window */
886 typedef struct _MotifTargetTableHeader {
888 guchar protocol_version;
891 } MotifTargetTableHeader;
893 /* Property placed on target windows */
894 typedef struct _MotifDragReceiverInfo {
896 guint8 protocol_version;
897 guint8 protocol_style;
899 guint32 proxy_window;
900 guint16 num_drop_sites;
903 } MotifDragReceiverInfo;
905 /* Target table handling */
907 static GdkFilterReturn
908 motif_drag_window_filter (GdkXEvent *xevent,
912 XEvent *xev = (XEvent *)xevent;
913 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
914 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
916 switch (xev->xany.type)
919 display_x11->motif_drag_window = None;
920 display_x11->motif_drag_gdk_window = NULL;
923 if (display_x11->motif_target_lists &&
924 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
925 motif_read_target_table (display);
928 return GDK_FILTER_REMOVE;
932 motif_lookup_drag_window (GdkDisplay *display,
933 Display *lookup_xdisplay)
935 Window retval = None;
936 gulong bytes_after, nitems;
941 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
942 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
944 XA_WINDOW, &type, &format, &nitems, &bytes_after,
947 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
949 retval = *(Window *)data;
951 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
960 /* Finds the window where global Motif drag information is stored.
961 * If it doesn't exist and 'create' is TRUE, create one.
964 motif_find_drag_window (GdkDisplay *display,
967 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
969 if (!display_x11->motif_drag_window)
971 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
972 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
974 if (!display_x11->motif_drag_window && create)
976 /* Create a persistant window. (Copied from LessTif) */
978 Display *persistant_xdisplay;
979 XSetWindowAttributes attr;
980 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
981 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
983 XGrabServer (persistant_xdisplay);
985 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
987 if (!display_x11->motif_drag_window)
989 attr.override_redirect = True;
990 attr.event_mask = PropertyChangeMask;
992 display_x11->motif_drag_window =
993 XCreateWindow (persistant_xdisplay,
994 RootWindow (persistant_xdisplay, 0),
995 -100, -100, 10, 10, 0, 0,
996 InputOnly, (Visual *)CopyFromParent,
997 (CWOverrideRedirect | CWEventMask), &attr);
1000 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
1002 XChangeProperty (persistant_xdisplay,
1003 RootWindow (persistant_xdisplay, 0),
1004 motif_drag_window_atom, XA_WINDOW,
1005 32, PropModeReplace,
1006 (guchar *)&motif_drag_window_atom, 1);
1009 XUngrabServer (persistant_xdisplay);
1010 XCloseDisplay (persistant_xdisplay);
1013 /* There is a miniscule race condition here if the drag window
1014 * gets destroyed exactly now.
1016 if (display_x11->motif_drag_window)
1018 display_x11->motif_drag_gdk_window =
1019 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
1020 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
1021 motif_drag_window_filter,
1026 return display_x11->motif_drag_window;
1030 motif_read_target_table (GdkDisplay *display)
1032 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1033 gulong bytes_after, nitems;
1038 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1040 if (display_x11->motif_target_lists)
1042 for (i=0; i<display_x11->motif_n_target_lists; i++)
1043 g_list_free (display_x11->motif_target_lists[i]);
1045 g_free (display_x11->motif_target_lists);
1046 display_x11->motif_target_lists = NULL;
1047 display_x11->motif_n_target_lists = 0;
1050 if (motif_find_drag_window (display, FALSE))
1053 MotifTargetTableHeader *header = NULL;
1054 guchar *target_bytes = NULL;
1056 gboolean success = FALSE;
1058 gdk_error_trap_push ();
1059 XGetWindowProperty (display_x11->xdisplay,
1060 display_x11->motif_drag_window,
1061 motif_drag_targets_atom,
1062 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1063 motif_drag_targets_atom,
1064 &type, &format, &nitems, &bytes_after,
1067 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1070 header = (MotifTargetTableHeader *)data;
1072 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1073 header->total_size = card32_to_host (header->total_size, header->byte_order);
1075 gdk_error_trap_push ();
1076 XGetWindowProperty (display_x11->xdisplay,
1077 display_x11->motif_drag_window,
1078 motif_drag_targets_atom,
1079 (sizeof(MotifTargetTableHeader)+3)/4,
1080 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1082 motif_drag_targets_atom, &type, &format, &nitems,
1083 &bytes_after, &target_bytes);
1085 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
1086 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1089 display_x11->motif_n_target_lists = header->n_lists;
1090 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1093 for (i=0; i<header->n_lists; i++)
1098 if (p + sizeof(guint16) - target_bytes > nitems)
1101 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1103 /* We need to make a copy of the targets, since it may
1106 targets = g_new (guint32, n_targets);
1107 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1109 p += sizeof(guint16) + n_targets * sizeof(guint32);
1110 if (p - target_bytes > nitems)
1113 for (j=0; j<n_targets; j++)
1114 display_x11->motif_target_lists[i] =
1115 g_list_prepend (display_x11->motif_target_lists[i],
1116 GUINT_TO_POINTER (card32_to_host (targets[j],
1117 header->byte_order)));
1119 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1129 XFree (target_bytes);
1133 if (display_x11->motif_target_lists)
1135 g_free (display_x11->motif_target_lists);
1136 display_x11->motif_target_lists = NULL;
1137 display_x11->motif_n_target_lists = 0;
1139 g_warning ("Error reading Motif target table\n");
1145 targets_sort_func (gconstpointer a, gconstpointer b)
1147 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1148 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1151 /* Check if given (sorted) list is in the targets table */
1153 motif_target_table_check (GdkDisplay *display,
1156 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1157 GList *tmp_list1, *tmp_list2;
1160 for (i=0; i<display_x11->motif_n_target_lists; i++)
1162 tmp_list1 = display_x11->motif_target_lists[i];
1165 while (tmp_list1 && tmp_list2)
1167 if (tmp_list1->data != tmp_list2->data)
1170 tmp_list1 = tmp_list1->next;
1171 tmp_list2 = tmp_list2->next;
1173 if (!tmp_list1 && !tmp_list2) /* Found it */
1181 motif_add_to_target_table (GdkDisplay *display,
1182 GList *targets) /* targets is list of GdkAtom */
1184 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1185 GList *sorted = NULL;
1190 /* make a sorted copy of the list */
1194 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1195 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1196 targets = targets->next;
1199 /* First check if it is there already */
1201 if (display_x11->motif_target_lists)
1202 index = motif_target_table_check (display, sorted);
1204 /* We need to grab the server while doing this, to ensure
1210 /* We need to make sure that it exists _before_ we grab the
1211 * server, since we can't open a new connection after we
1214 motif_find_drag_window (display, TRUE);
1216 gdk_x11_display_grab (display);
1217 motif_read_target_table (display);
1219 /* Check again, in case it was added in the meantime */
1221 if (display_x11->motif_target_lists)
1222 index = motif_target_table_check (display, sorted);
1226 guint32 total_size = 0;
1230 MotifTargetTableHeader *header;
1232 if (!display_x11->motif_target_lists)
1234 display_x11->motif_target_lists = g_new (GList *, 1);
1235 display_x11->motif_n_target_lists = 1;
1239 display_x11->motif_n_target_lists++;
1240 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1241 sizeof(GList *) * display_x11->motif_n_target_lists);
1243 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1245 index = display_x11->motif_n_target_lists - 1;
1247 total_size = sizeof (MotifTargetTableHeader);
1248 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1249 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1251 data = g_malloc (total_size);
1253 header = (MotifTargetTableHeader *)data;
1254 p = data + sizeof(MotifTargetTableHeader);
1256 header->byte_order = local_byte_order;
1257 header->protocol_version = 0;
1258 header->n_lists = display_x11->motif_n_target_lists;
1259 header->total_size = total_size;
1261 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1263 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1264 guint32 *targets = g_new (guint32, n_targets);
1265 guint32 *p32 = targets;
1267 tmp_list = display_x11->motif_target_lists[i];
1270 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1272 tmp_list = tmp_list->next;
1277 p += sizeof(guint16);
1279 memcpy (p, targets, n_targets * sizeof(guint32));
1282 p += sizeof(guint32) * n_targets;
1286 XChangeProperty (display_x11->xdisplay,
1287 display_x11->motif_drag_window,
1288 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1289 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1293 gdk_x11_display_ungrab (display);
1296 g_list_free (sorted);
1300 /* Translate flags */
1303 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1305 guint recommended_op = flags & 0x000f;
1306 guint possible_ops = (flags & 0x0f0) >> 4;
1308 switch (recommended_op)
1311 context->suggested_action = GDK_ACTION_MOVE;
1314 context->suggested_action = GDK_ACTION_COPY;
1317 context->suggested_action = GDK_ACTION_LINK;
1320 context->suggested_action = GDK_ACTION_COPY;
1324 context->actions = 0;
1325 if (possible_ops & XmDROP_MOVE)
1326 context->actions |= GDK_ACTION_MOVE;
1327 if (possible_ops & XmDROP_COPY)
1328 context->actions |= GDK_ACTION_COPY;
1329 if (possible_ops & XmDROP_LINK)
1330 context->actions |= GDK_ACTION_LINK;
1334 motif_dnd_get_flags (GdkDragContext *context)
1338 switch (context->suggested_action)
1340 case GDK_ACTION_MOVE:
1341 flags = XmDROP_MOVE;
1343 case GDK_ACTION_COPY:
1344 flags = XmDROP_COPY;
1346 case GDK_ACTION_LINK:
1347 flags = XmDROP_LINK;
1350 flags = XmDROP_NOOP;
1354 if (context->actions & GDK_ACTION_MOVE)
1355 flags |= XmDROP_MOVE << 8;
1356 if (context->actions & GDK_ACTION_COPY)
1357 flags |= XmDROP_COPY << 8;
1358 if (context->actions & GDK_ACTION_LINK)
1359 flags |= XmDROP_LINK << 8;
1367 motif_set_targets (GdkDragContext *context)
1369 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1370 MotifDragInitiatorInfo info;
1372 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1374 info.byte_order = local_byte_order;
1375 info.protocol_version = 0;
1377 info.targets_index = motif_add_to_target_table (display, context->targets);
1382 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1384 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1385 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1389 info.selection_atom = private->motif_selection;
1391 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1392 GDK_DRAWABLE_XID (context->source_window),
1393 private->motif_selection,
1394 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1396 (guchar *)&info, sizeof (info));
1398 private->motif_targets_set = 1;
1402 motif_check_dest (GdkDisplay *display,
1405 gboolean retval = FALSE;
1407 MotifDragReceiverInfo *info;
1410 unsigned long nitems, after;
1411 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1413 gdk_error_trap_push ();
1414 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1415 motif_drag_receiver_info_atom,
1416 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1417 &type, &format, &nitems, &after,
1420 if (gdk_error_trap_pop() == 0)
1424 info = (MotifDragReceiverInfo *)data;
1426 if ((format == 8) && (nitems == sizeof(*info)))
1428 if ((info->protocol_version == 0) &&
1429 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1430 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1431 (info->protocol_style == XmDRAG_DYNAMIC)))
1437 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1444 return retval ? win : None;
1448 motif_send_enter (GdkDragContext *context,
1451 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1452 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1455 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1456 return; /* Motif Dnd requires getting properties on the root window */
1458 xev.xclient.type = ClientMessage;
1459 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1460 xev.xclient.format = 8;
1461 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1463 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1464 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1465 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1466 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1467 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1469 if (!private->motif_targets_set)
1470 motif_set_targets (context);
1472 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1473 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1475 if (!_gdk_send_xevent (display,
1476 GDK_DRAWABLE_XID (context->dest_window),
1479 g_message ("Send event to %lx failed",
1480 GDK_DRAWABLE_XID (context->dest_window)));
1484 motif_send_leave (GdkDragContext *context,
1487 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1490 xev.xclient.type = ClientMessage;
1491 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1492 xev.xclient.format = 8;
1493 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1495 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1496 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1497 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1498 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1499 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1500 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1501 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1503 if (!_gdk_send_xevent (display,
1504 GDK_DRAWABLE_XID (context->dest_window),
1507 g_message ("Send event to %lx failed",
1508 GDK_DRAWABLE_XID (context->dest_window)));
1512 motif_send_motion (GdkDragContext *context,
1515 GdkDragAction action,
1518 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1519 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1523 xev.xclient.type = ClientMessage;
1524 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1525 xev.xclient.format = 8;
1526 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1528 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1529 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1530 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1531 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1532 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1534 if ((context->suggested_action != private->old_action) ||
1535 (context->actions != private->old_actions))
1537 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1539 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1544 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1546 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1547 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1549 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1553 if (!_gdk_send_xevent (display,
1554 GDK_DRAWABLE_XID (context->dest_window),
1557 g_message ("Send event to %lx failed",
1558 GDK_DRAWABLE_XID (context->dest_window)));
1564 motif_send_drop (GdkDragContext *context, guint32 time)
1566 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1567 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1570 xev.xclient.type = ClientMessage;
1571 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1572 xev.xclient.format = 8;
1573 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1575 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1576 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1577 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1578 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1580 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1581 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1583 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1584 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1586 if (!_gdk_send_xevent (display,
1587 GDK_DRAWABLE_XID (context->dest_window),
1590 g_message ("Send event to %lx failed",
1591 GDK_DRAWABLE_XID (context->dest_window)));
1597 motif_read_initiator_info (GdkDisplay *display,
1598 Window source_window,
1609 MotifDragInitiatorInfo *initiator_info;
1611 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1613 gdk_error_trap_push ();
1614 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1615 0, sizeof(*initiator_info), FALSE,
1616 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1617 &type, &format, &nitems, &bytes_after,
1620 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1622 g_warning ("Error reading initiator info\n");
1626 initiator_info = (MotifDragInitiatorInfo *)data;
1628 motif_read_target_table (display);
1630 initiator_info->targets_index =
1631 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1632 initiator_info->selection_atom =
1633 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1635 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1637 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1638 XFree (initiator_info);
1642 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1647 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1648 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1649 tmp_list = tmp_list->prev;
1652 #ifdef G_ENABLE_DEBUG
1653 if (_gdk_debug_flags & GDK_DEBUG_DND)
1654 print_target_list (*targets);
1655 #endif /* G_ENABLE_DEBUG */
1657 *selection = initiator_info->selection_atom;
1659 XFree (initiator_info);
1664 static GdkDragContext *
1665 motif_drag_context_new (GdkWindow *dest_window,
1667 guint32 source_window,
1670 GdkDragContext *new_context;
1671 GdkDragContextPrivateX11 *private;
1672 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1673 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1675 /* FIXME, current_dest_drag really shouldn't be NULL'd
1676 * if we error below.
1678 if (display_x11->current_dest_drag != NULL)
1680 if (timestamp >= display_x11->current_dest_drag->start_time)
1682 g_object_unref (display_x11->current_dest_drag);
1683 display_x11->current_dest_drag = NULL;
1689 new_context = gdk_drag_context_new ();
1690 private = PRIVATE_DATA (new_context);
1692 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1693 new_context->is_source = FALSE;
1695 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1696 if (new_context->source_window)
1697 g_object_ref (new_context->source_window);
1700 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1701 if (!new_context->source_window)
1703 g_object_unref (new_context);
1708 new_context->dest_window = dest_window;
1709 g_object_ref (dest_window);
1710 new_context->start_time = timestamp;
1712 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1715 &new_context->targets,
1716 &private->motif_selection))
1718 g_object_unref (new_context);
1726 * The MOTIF drag protocol has no real provisions for distinguishing
1727 * multiple simultaneous drops. If the sources grab the pointer
1728 * when doing drags, that shouldn't happen, in any case. If it
1729 * does, we can't do much except hope for the best.
1732 static GdkFilterReturn
1733 motif_top_level_enter (GdkEvent *event,
1736 guint32 source_window,
1739 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1740 GdkDragContext *new_context;
1742 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1743 flags, timestamp, source_window, atom));
1745 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1747 return GDK_FILTER_REMOVE;
1749 event->dnd.type = GDK_DRAG_ENTER;
1750 event->dnd.context = new_context;
1751 g_object_ref (new_context);
1753 display_x11->current_dest_drag = new_context;
1755 return GDK_FILTER_TRANSLATE;
1758 static GdkFilterReturn
1759 motif_top_level_leave (GdkEvent *event,
1763 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1765 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1768 if ((display_x11->current_dest_drag != NULL) &&
1769 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1770 (timestamp >= display_x11->current_dest_drag->start_time))
1772 event->dnd.type = GDK_DRAG_LEAVE;
1773 /* Pass ownership of context to the event */
1774 event->dnd.context = display_x11->current_dest_drag;
1776 display_x11->current_dest_drag = NULL;
1778 return GDK_FILTER_TRANSLATE;
1781 return GDK_FILTER_REMOVE;
1784 static GdkFilterReturn
1785 motif_motion (GdkEvent *event,
1791 GdkDragContextPrivateX11 *private;
1792 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1794 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1795 flags, timestamp, x_root, y_root));
1797 if ((display_x11->current_dest_drag != NULL) &&
1798 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1799 (timestamp >= display_x11->current_dest_drag->start_time))
1801 private = PRIVATE_DATA (display_x11->current_dest_drag);
1803 event->dnd.type = GDK_DRAG_MOTION;
1804 event->dnd.context = display_x11->current_dest_drag;
1805 g_object_ref (display_x11->current_dest_drag);
1807 event->dnd.time = timestamp;
1809 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1811 event->dnd.x_root = x_root;
1812 event->dnd.y_root = y_root;
1814 private->last_x = x_root;
1815 private->last_y = y_root;
1817 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1819 return GDK_FILTER_TRANSLATE;
1822 return GDK_FILTER_REMOVE;
1825 static GdkFilterReturn
1826 motif_operation_changed (GdkEvent *event,
1830 GdkDragContextPrivateX11 *private;
1831 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1832 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1835 if ((display_x11->current_dest_drag != NULL) &&
1836 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1837 (timestamp >= display_x11->current_dest_drag->start_time))
1839 event->dnd.type = GDK_DRAG_MOTION;
1840 event->dnd.send_event = FALSE;
1841 event->dnd.context = display_x11->current_dest_drag;
1842 g_object_ref (display_x11->current_dest_drag);
1844 event->dnd.time = timestamp;
1845 private = PRIVATE_DATA (display_x11->current_dest_drag);
1847 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1849 event->dnd.x_root = private->last_x;
1850 event->dnd.y_root = private->last_y;
1852 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1854 return GDK_FILTER_TRANSLATE;
1857 return GDK_FILTER_REMOVE;
1860 static GdkFilterReturn
1861 motif_drop_start (GdkEvent *event,
1864 guint32 source_window,
1869 GdkDragContext *new_context;
1870 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1872 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1873 flags, timestamp, x_root, y_root, source_window, atom));
1875 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1877 return GDK_FILTER_REMOVE;
1879 motif_dnd_translate_flags (new_context, flags);
1881 event->dnd.type = GDK_DROP_START;
1882 event->dnd.context = new_context;
1883 event->dnd.time = timestamp;
1884 event->dnd.x_root = x_root;
1885 event->dnd.y_root = y_root;
1887 gdk_x11_window_set_user_time (event->any.window, timestamp);
1889 g_object_ref (new_context);
1890 display_x11->current_dest_drag = new_context;
1892 return GDK_FILTER_TRANSLATE;
1895 static GdkFilterReturn
1896 motif_drag_status (GdkEvent *event,
1900 GdkDragContext *context;
1901 GdkDisplay *display;
1904 g_message ("Motif status message: flags %x", flags));
1906 display = gdk_drawable_get_display (event->any.window);
1908 return GDK_FILTER_REMOVE;
1910 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1914 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1915 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1916 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1917 private->drag_status = GDK_DRAG_STATUS_DRAG;
1919 event->dnd.type = GDK_DRAG_STATUS;
1920 event->dnd.send_event = FALSE;
1921 event->dnd.context = context;
1922 g_object_ref (context);
1924 event->dnd.time = timestamp;
1926 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1928 switch (flags & 0x000f)
1931 context->action = 0;
1934 context->action = GDK_ACTION_MOVE;
1937 context->action = GDK_ACTION_COPY;
1940 context->action = GDK_ACTION_LINK;
1945 context->action = 0;
1947 return GDK_FILTER_TRANSLATE;
1949 return GDK_FILTER_REMOVE;
1952 static GdkFilterReturn
1953 motif_dnd_filter (GdkXEvent *xev,
1957 XEvent *xevent = (XEvent *)xev;
1962 guint32 source_window;
1964 gint16 x_root, y_root;
1967 if (!event->any.window ||
1968 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1969 return GDK_FILTER_CONTINUE; /* Not for us */
1971 /* First read some fields common to all Motif DND messages */
1973 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1974 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1975 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1977 is_reply = ((reason & 0x80) != 0);
1979 switch (reason & 0x7f)
1981 case XmTOP_LEVEL_ENTER:
1982 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1983 atom = MOTIF_UNPACK_LONG (xevent, 3);
1984 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1985 case XmTOP_LEVEL_LEAVE:
1986 return motif_top_level_leave (event, flags, timestamp);
1989 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1990 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1993 return motif_motion (event, flags, timestamp, x_root, y_root);
1995 return motif_drag_status (event, flags, timestamp);
1997 case XmDROP_SITE_ENTER:
1998 return motif_drag_status (event, flags, timestamp);
2000 case XmDROP_SITE_LEAVE:
2001 return motif_drag_status (event,
2002 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
2005 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2006 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2007 atom = MOTIF_UNPACK_LONG (xevent, 3);
2008 source_window = MOTIF_UNPACK_LONG (xevent, 4);
2011 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
2014 case XmOPERATION_CHANGED:
2016 return motif_operation_changed (event, flags, timestamp);
2018 return motif_drag_status (event, flags, timestamp);
2021 /* To the best of my knowledge, these next two messages are
2022 * not part of the protocol, though they are defined in
2026 case XmDRAG_DROP_FINISH:
2030 return GDK_FILTER_REMOVE;
2033 /*************************************************************
2034 ***************************** XDND **************************
2035 *************************************************************/
2037 /* Utility functions */
2042 GdkDragAction action;
2043 } xdnd_actions_table[] = {
2044 { "XdndActionCopy", None, GDK_ACTION_COPY },
2045 { "XdndActionMove", None, GDK_ACTION_MOVE },
2046 { "XdndActionLink", None, GDK_ACTION_LINK },
2047 { "XdndActionAsk", None, GDK_ACTION_ASK },
2048 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2051 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
2052 static gboolean xdnd_actions_initialized = FALSE;
2055 xdnd_initialize_actions (void)
2059 xdnd_actions_initialized = TRUE;
2060 for (i=0; i < xdnd_n_actions; i++)
2061 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2064 static GdkDragAction
2065 xdnd_action_from_atom (GdkDisplay *display,
2074 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2076 if (!xdnd_actions_initialized)
2077 xdnd_initialize_actions();
2079 for (i=0; i<xdnd_n_actions; i++)
2080 if (atom == xdnd_actions_table[i].atom)
2081 return xdnd_actions_table[i].action;
2087 xdnd_action_to_atom (GdkDisplay *display,
2088 GdkDragAction action)
2092 if (!xdnd_actions_initialized)
2093 xdnd_initialize_actions();
2095 for (i=0; i<xdnd_n_actions; i++)
2096 if (action == xdnd_actions_table[i].action)
2097 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2104 static GdkFilterReturn
2105 xdnd_status_filter (GdkXEvent *xev,
2109 GdkDisplay *display;
2110 XEvent *xevent = (XEvent *)xev;
2111 guint32 dest_window = xevent->xclient.data.l[0];
2112 guint32 flags = xevent->xclient.data.l[1];
2113 Atom action = xevent->xclient.data.l[4];
2114 GdkDragContext *context;
2116 if (!event->any.window ||
2117 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2118 return GDK_FILTER_CONTINUE; /* Not for us */
2121 g_message ("XdndStatus: dest_window: %#x action: %ld",
2122 dest_window, action));
2124 display = gdk_drawable_get_display (event->any.window);
2125 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2129 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2130 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2131 private->drag_status = GDK_DRAG_STATUS_DRAG;
2133 event->dnd.send_event = FALSE;
2134 event->dnd.type = GDK_DRAG_STATUS;
2135 event->dnd.context = context;
2136 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2137 g_object_ref (context);
2139 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2140 if (!(action != 0) != !(flags & 1))
2143 g_warning ("Received status event with flags not corresponding to action!\n"));
2147 context->action = xdnd_action_from_atom (display, action);
2149 return GDK_FILTER_TRANSLATE;
2152 return GDK_FILTER_REMOVE;
2155 static GdkFilterReturn
2156 xdnd_finished_filter (GdkXEvent *xev,
2160 GdkDisplay *display;
2161 XEvent *xevent = (XEvent *)xev;
2162 guint32 dest_window = xevent->xclient.data.l[0];
2163 GdkDragContext *context;
2164 GdkDragContextPrivateX11 *private;
2166 if (!event->any.window ||
2167 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2168 return GDK_FILTER_CONTINUE; /* Not for us */
2171 g_message ("XdndFinished: dest_window: %#x", dest_window));
2173 display = gdk_drawable_get_display (event->any.window);
2174 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2178 private = PRIVATE_DATA (context);
2179 if (private->version == 5)
2180 private->drop_failed = xevent->xclient.data.l[1] == 0;
2182 event->dnd.type = GDK_DROP_FINISHED;
2183 event->dnd.context = context;
2184 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2185 g_object_ref (context);
2187 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2189 return GDK_FILTER_TRANSLATE;
2192 return GDK_FILTER_REMOVE;
2196 xdnd_set_targets (GdkDragContext *context)
2198 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2200 GList *tmp_list = context->targets;
2202 gint n_atoms = g_list_length (context->targets);
2203 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2205 atomlist = g_new (Atom, n_atoms);
2209 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2210 tmp_list = tmp_list->next;
2214 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2215 GDK_DRAWABLE_XID (context->source_window),
2216 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2217 XA_ATOM, 32, PropModeReplace,
2218 (guchar *)atomlist, n_atoms);
2222 private->xdnd_targets_set = 1;
2226 xdnd_set_actions (GdkDragContext *context)
2228 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2233 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2235 if (!xdnd_actions_initialized)
2236 xdnd_initialize_actions();
2238 actions = context->actions;
2240 for (i=0; i<xdnd_n_actions; i++)
2242 if (actions & xdnd_actions_table[i].action)
2244 actions &= ~xdnd_actions_table[i].action;
2249 atomlist = g_new (Atom, n_atoms);
2251 actions = context->actions;
2253 for (i=0; i<xdnd_n_actions; i++)
2255 if (actions & xdnd_actions_table[i].action)
2257 actions &= ~xdnd_actions_table[i].action;
2258 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2263 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2264 GDK_DRAWABLE_XID (context->source_window),
2265 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2266 XA_ATOM, 32, PropModeReplace,
2267 (guchar *)atomlist, n_atoms);
2271 private->xdnd_actions_set = TRUE;
2272 private->xdnd_actions = context->actions;
2276 send_client_message_async_cb (Window window,
2280 GdkDragContext *context = data;
2282 g_message ("Got async callback for #%lx, success = %d",
2285 /* On failure, we immediately continue with the protocol
2286 * so we don't end up blocking for a timeout
2289 context->dest_window &&
2290 window == GDK_WINDOW_XID (context->dest_window))
2292 GdkEvent *temp_event;
2293 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2295 g_object_unref (context->dest_window);
2296 context->dest_window = NULL;
2297 context->action = 0;
2299 private->drag_status = GDK_DRAG_STATUS_DRAG;
2301 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2302 temp_event->dnd.window = g_object_ref (context->source_window);
2303 temp_event->dnd.send_event = TRUE;
2304 temp_event->dnd.context = g_object_ref (context);
2305 temp_event->dnd.time = GDK_CURRENT_TIME;
2306 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2308 gdk_event_put (temp_event);
2310 gdk_event_free (temp_event);
2313 g_object_unref (context);
2318 gdk_drag_context_get_display (GdkDragContext *context)
2320 if (context->source_window)
2321 return GDK_DRAWABLE_DISPLAY (context->source_window);
2322 else if (context->dest_window)
2323 return GDK_DRAWABLE_DISPLAY (context->dest_window);
2325 g_assert_not_reached ();
2330 send_client_message_async (GdkDragContext *context,
2334 XClientMessageEvent *event_send)
2336 GdkDisplay *display = gdk_drag_context_get_display (context);
2338 g_object_ref (context);
2340 _gdk_x11_send_client_message_async (display, window,
2341 propagate, event_mask, event_send,
2342 send_client_message_async_cb, context);
2346 xdnd_send_xevent (GdkDragContext *context,
2351 GdkDisplay *display = gdk_drag_context_get_display (context);
2355 g_assert (event_send->xany.type == ClientMessage);
2357 /* We short-circuit messages to ourselves */
2358 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2362 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2364 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2365 event_send->xclient.message_type)
2367 GdkEvent *temp_event;
2369 temp_event = gdk_event_new (GDK_NOTHING);
2370 temp_event->any.window = g_object_ref (window);
2372 if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
2374 gdk_event_put (temp_event);
2375 gdk_event_free (temp_event);
2383 xwindow = GDK_WINDOW_XWINDOW (window);
2385 if (_gdk_x11_display_is_root_window (display, xwindow))
2386 event_mask = ButtonPressMask;
2390 send_client_message_async (context, xwindow, propagate, event_mask,
2391 &event_send->xclient);
2397 xdnd_send_enter (GdkDragContext *context)
2400 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2401 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2403 xev.xclient.type = ClientMessage;
2404 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2405 xev.xclient.format = 32;
2406 xev.xclient.window = private->drop_xid ?
2408 GDK_DRAWABLE_XID (context->dest_window);
2409 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2410 xev.xclient.data.l[1] = (private->version << 24); /* version */
2411 xev.xclient.data.l[2] = 0;
2412 xev.xclient.data.l[3] = 0;
2413 xev.xclient.data.l[4] = 0;
2416 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2417 GDK_DRAWABLE_XID (context->source_window), private->version));
2418 if (g_list_length (context->targets) > 3)
2420 if (!private->xdnd_targets_set)
2421 xdnd_set_targets (context);
2422 xev.xclient.data.l[1] |= 1;
2426 GList *tmp_list = context->targets;
2431 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2432 GDK_POINTER_TO_ATOM (tmp_list->data));
2433 tmp_list = tmp_list->next;
2438 if (!xdnd_send_xevent (context, context->dest_window,
2442 g_message ("Send event to %lx failed",
2443 GDK_DRAWABLE_XID (context->dest_window)));
2444 g_object_unref (context->dest_window);
2445 context->dest_window = NULL;
2450 xdnd_send_leave (GdkDragContext *context)
2452 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2455 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2457 xev.xclient.type = ClientMessage;
2458 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2459 xev.xclient.format = 32;
2460 xev.xclient.window = private->drop_xid ?
2462 GDK_DRAWABLE_XID (context->dest_window);
2463 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2464 xev.xclient.data.l[1] = 0;
2465 xev.xclient.data.l[2] = 0;
2466 xev.xclient.data.l[3] = 0;
2467 xev.xclient.data.l[4] = 0;
2469 if (!xdnd_send_xevent (context, context->dest_window,
2473 g_message ("Send event to %lx failed",
2474 GDK_DRAWABLE_XID (context->dest_window)));
2475 g_object_unref (context->dest_window);
2476 context->dest_window = NULL;
2481 xdnd_send_drop (GdkDragContext *context, guint32 time)
2483 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2484 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2487 xev.xclient.type = ClientMessage;
2488 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2489 xev.xclient.format = 32;
2490 xev.xclient.window = private->drop_xid ?
2492 GDK_DRAWABLE_XID (context->dest_window);
2493 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2494 xev.xclient.data.l[1] = 0;
2495 xev.xclient.data.l[2] = time;
2496 xev.xclient.data.l[3] = 0;
2497 xev.xclient.data.l[4] = 0;
2499 if (!xdnd_send_xevent (context, context->dest_window,
2503 g_message ("Send event to %lx failed",
2504 GDK_DRAWABLE_XID (context->dest_window)));
2505 g_object_unref (context->dest_window);
2506 context->dest_window = NULL;
2511 xdnd_send_motion (GdkDragContext *context,
2514 GdkDragAction action,
2517 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2518 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2521 xev.xclient.type = ClientMessage;
2522 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2523 xev.xclient.format = 32;
2524 xev.xclient.window = private->drop_xid ?
2526 GDK_DRAWABLE_XID (context->dest_window);
2527 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2528 xev.xclient.data.l[1] = 0;
2529 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2530 xev.xclient.data.l[3] = time;
2531 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2533 if (!xdnd_send_xevent (context, context->dest_window,
2537 g_message ("Send event to %lx failed",
2538 GDK_DRAWABLE_XID (context->dest_window)));
2539 g_object_unref (context->dest_window);
2540 context->dest_window = NULL;
2542 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2546 xdnd_check_dest (GdkDisplay *display,
2548 guint *xdnd_version)
2550 gboolean retval = FALSE;
2553 unsigned long nitems, after;
2558 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2559 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2563 gdk_error_trap_push ();
2565 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2567 1, False, AnyPropertyType,
2568 &type, &format, &nitems, &after,
2573 proxy_data = (Window *)data;
2575 if ((format == 32) && (nitems == 1))
2577 proxy = *proxy_data;
2581 g_warning ("Invalid XdndProxy "
2582 "property on window %ld\n", win));
2587 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2589 1, False, AnyPropertyType,
2590 &type, &format, &nitems, &after,
2591 &data) == Success) &&
2594 version = (Atom *)data;
2596 if ((format == 32) && (nitems == 1))
2601 *xdnd_version = *version;
2605 g_warning ("Invalid XdndAware "
2606 "property on window %ld\n", win));
2612 gdk_error_trap_pop ();
2614 return retval ? (proxy ? proxy : win) : None;
2620 xdnd_read_actions (GdkDragContext *context)
2622 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2625 gulong nitems, after;
2631 PRIVATE_DATA (context)->xdnd_have_actions = FALSE;
2633 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2635 /* Get the XdndActionList, if set */
2637 gdk_error_trap_push ();
2639 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2640 GDK_DRAWABLE_XID (context->source_window),
2641 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2643 False, XA_ATOM, &type, &format, &nitems,
2644 &after, &data) == Success &&
2647 atoms = (Atom *)data;
2649 context->actions = 0;
2651 for (i=0; i<nitems; i++)
2652 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2654 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2656 #ifdef G_ENABLE_DEBUG
2657 if (_gdk_debug_flags & GDK_DEBUG_DND)
2659 GString *action_str = g_string_new (NULL);
2660 if (context->actions & GDK_ACTION_MOVE)
2661 g_string_append(action_str, "MOVE ");
2662 if (context->actions & GDK_ACTION_COPY)
2663 g_string_append(action_str, "COPY ");
2664 if (context->actions & GDK_ACTION_LINK)
2665 g_string_append(action_str, "LINK ");
2666 if (context->actions & GDK_ACTION_ASK)
2667 g_string_append(action_str, "ASK ");
2669 g_message("Xdnd actions = %s", action_str->str);
2670 g_string_free (action_str, TRUE);
2672 #endif /* G_ENABLE_DEBUG */
2679 gdk_error_trap_pop ();
2685 GdkDragContext *source_context;
2687 source_context = gdk_drag_context_find (display, TRUE,
2688 GDK_DRAWABLE_XID (context->source_window),
2689 GDK_DRAWABLE_XID (context->dest_window));
2693 context->actions = source_context->actions;
2694 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2699 /* We have to make sure that the XdndActionList we keep internally
2700 * is up to date with the XdndActionList on the source window
2701 * because we get no notification, because Xdnd wasn't meant
2702 * to continually send actions. So we select on PropertyChangeMask
2703 * and add this filter.
2705 static GdkFilterReturn
2706 xdnd_source_window_filter (GdkXEvent *xev,
2710 XEvent *xevent = (XEvent *)xev;
2711 GdkDragContext *context = cb_data;
2712 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2714 if ((xevent->xany.type == PropertyNotify) &&
2715 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2717 xdnd_read_actions (context);
2719 return GDK_FILTER_REMOVE;
2722 return GDK_FILTER_CONTINUE;
2726 xdnd_manage_source_filter (GdkDragContext *context,
2728 gboolean add_filter)
2730 if (!GDK_WINDOW_DESTROYED (window) &&
2731 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2733 gdk_error_trap_push ();
2737 gdk_window_set_events (window,
2738 gdk_window_get_events (window) |
2739 GDK_PROPERTY_CHANGE_MASK);
2740 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2744 gdk_window_remove_filter (window,
2745 xdnd_source_window_filter,
2747 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2748 * but we might want it for other reasons. (Like
2749 * INCR selection transactions).
2753 gdk_display_sync (gdk_drawable_get_display (window));
2754 gdk_error_trap_pop ();
2759 base_precache_atoms (GdkDisplay *display)
2761 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2763 if (!display_x11->base_dnd_atoms_precached)
2765 static const char *const precache_atoms[] = {
2766 "ENLIGHTENMENT_DESKTOP",
2770 "_MOTIF_DRAG_RECEIVER_INFO"
2773 _gdk_x11_precache_atoms (display,
2774 precache_atoms, G_N_ELEMENTS (precache_atoms));
2776 display_x11->base_dnd_atoms_precached = TRUE;
2781 xdnd_precache_atoms (GdkDisplay *display)
2783 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2785 if (!display_x11->xdnd_atoms_precached)
2787 static const char *const precache_atoms[] = {
2793 "XdndActionPrivate",
2804 _gdk_x11_precache_atoms (display,
2805 precache_atoms, G_N_ELEMENTS (precache_atoms));
2807 display_x11->xdnd_atoms_precached = TRUE;
2811 static GdkFilterReturn
2812 xdnd_enter_filter (GdkXEvent *xev,
2816 GdkDisplay *display;
2817 GdkDisplayX11 *display_x11;
2818 XEvent *xevent = (XEvent *)xev;
2819 GdkDragContext *new_context;
2824 gulong nitems, after;
2828 guint32 source_window;
2832 if (!event->any.window ||
2833 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2834 return GDK_FILTER_CONTINUE; /* Not for us */
2836 source_window = xevent->xclient.data.l[0];
2837 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2838 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2840 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2841 display_x11 = GDK_DISPLAY_X11 (display);
2843 xdnd_precache_atoms (display);
2846 g_message ("XdndEnter: source_window: %#x, version: %#x",
2847 source_window, version));
2851 /* Old source ignore */
2852 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2853 return GDK_FILTER_REMOVE;
2856 if (display_x11->current_dest_drag != NULL)
2858 g_object_unref (display_x11->current_dest_drag);
2859 display_x11->current_dest_drag = NULL;
2862 new_context = gdk_drag_context_new ();
2863 new_context->protocol = GDK_DRAG_PROTO_XDND;
2864 PRIVATE_DATA(new_context)->version = version;
2866 /* FIXME: Should extend DnD protocol to have device info */
2867 gdk_drag_context_set_device (new_context, gdk_display_get_core_pointer (display));
2869 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2870 if (new_context->source_window)
2871 g_object_ref (new_context->source_window);
2874 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2875 if (!new_context->source_window)
2877 g_object_unref (new_context);
2878 return GDK_FILTER_REMOVE;
2881 new_context->dest_window = event->any.window;
2882 g_object_ref (new_context->dest_window);
2884 new_context->targets = NULL;
2887 gdk_error_trap_push ();
2888 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2890 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2892 False, XA_ATOM, &type, &format, &nitems,
2895 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2897 g_object_unref (new_context);
2902 return GDK_FILTER_REMOVE;
2905 atoms = (Atom *)data;
2907 for (i=0; i<nitems; i++)
2908 new_context->targets =
2909 g_list_append (new_context->targets,
2910 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2918 if (xevent->xclient.data.l[2+i])
2919 new_context->targets =
2920 g_list_append (new_context->targets,
2921 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2922 xevent->xclient.data.l[2+i])));
2925 #ifdef G_ENABLE_DEBUG
2926 if (_gdk_debug_flags & GDK_DEBUG_DND)
2927 print_target_list (new_context->targets);
2928 #endif /* G_ENABLE_DEBUG */
2930 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2931 xdnd_read_actions (new_context);
2933 event->dnd.type = GDK_DRAG_ENTER;
2934 event->dnd.context = new_context;
2935 gdk_event_set_device (event, gdk_drag_context_get_device (new_context));
2936 g_object_ref (new_context);
2938 display_x11->current_dest_drag = new_context;
2940 return GDK_FILTER_TRANSLATE;
2943 static GdkFilterReturn
2944 xdnd_leave_filter (GdkXEvent *xev,
2948 XEvent *xevent = (XEvent *)xev;
2949 guint32 source_window = xevent->xclient.data.l[0];
2950 GdkDisplay *display;
2951 GdkDisplayX11 *display_x11;
2953 if (!event->any.window ||
2954 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2955 return GDK_FILTER_CONTINUE; /* Not for us */
2958 g_message ("XdndLeave: source_window: %#x",
2961 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2962 display_x11 = GDK_DISPLAY_X11 (display);
2964 xdnd_precache_atoms (display);
2966 if ((display_x11->current_dest_drag != NULL) &&
2967 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2968 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2970 event->dnd.type = GDK_DRAG_LEAVE;
2971 /* Pass ownership of context to the event */
2972 event->dnd.context = display_x11->current_dest_drag;
2973 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
2975 display_x11->current_dest_drag = NULL;
2977 return GDK_FILTER_TRANSLATE;
2980 return GDK_FILTER_REMOVE;
2983 static GdkFilterReturn
2984 xdnd_position_filter (GdkXEvent *xev,
2988 XEvent *xevent = (XEvent *)xev;
2989 guint32 source_window = xevent->xclient.data.l[0];
2990 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2991 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2992 guint32 time = xevent->xclient.data.l[3];
2993 Atom action = xevent->xclient.data.l[4];
2995 GdkDisplay *display;
2996 GdkDisplayX11 *display_x11;
2998 if (!event->any.window ||
2999 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3000 return GDK_FILTER_CONTINUE; /* Not for us */
3003 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
3004 source_window, x_root, y_root, time, action));
3006 display = GDK_DRAWABLE_DISPLAY (event->any.window);
3007 display_x11 = GDK_DISPLAY_X11 (display);
3009 xdnd_precache_atoms (display);
3011 if ((display_x11->current_dest_drag != NULL) &&
3012 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3013 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
3015 event->dnd.type = GDK_DRAG_MOTION;
3016 event->dnd.context = display_x11->current_dest_drag;
3017 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
3018 g_object_ref (display_x11->current_dest_drag);
3020 event->dnd.time = time;
3022 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
3024 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
3025 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
3027 event->dnd.x_root = x_root;
3028 event->dnd.y_root = y_root;
3030 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
3031 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
3033 return GDK_FILTER_TRANSLATE;
3036 return GDK_FILTER_REMOVE;
3039 static GdkFilterReturn
3040 xdnd_drop_filter (GdkXEvent *xev,
3044 XEvent *xevent = (XEvent *)xev;
3045 guint32 source_window = xevent->xclient.data.l[0];
3046 guint32 time = xevent->xclient.data.l[2];
3047 GdkDisplay *display;
3048 GdkDisplayX11 *display_x11;
3050 if (!event->any.window ||
3051 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3052 return GDK_FILTER_CONTINUE; /* Not for us */
3055 g_message ("XdndDrop: source_window: %#x time: %d",
3056 source_window, time));
3058 display = GDK_DRAWABLE_DISPLAY (event->any.window);
3059 display_x11 = GDK_DISPLAY_X11 (display);
3061 xdnd_precache_atoms (display);
3063 if ((display_x11->current_dest_drag != NULL) &&
3064 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3065 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
3067 GdkDragContextPrivateX11 *private;
3068 private = PRIVATE_DATA (display_x11->current_dest_drag);
3070 event->dnd.type = GDK_DROP_START;
3072 event->dnd.context = display_x11->current_dest_drag;
3073 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
3074 g_object_ref (display_x11->current_dest_drag);
3076 event->dnd.time = time;
3077 event->dnd.x_root = private->last_x;
3078 event->dnd.y_root = private->last_y;
3080 gdk_x11_window_set_user_time (event->any.window, time);
3082 return GDK_FILTER_TRANSLATE;
3085 return GDK_FILTER_REMOVE;
3088 /*************************************************************
3089 ************************** Public API ***********************
3090 *************************************************************/
3092 _gdk_dnd_init (GdkDisplay *display)
3097 gdk_display_add_client_message_filter (
3099 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
3100 motif_dnd_filter, NULL);
3102 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3104 gdk_display_add_client_message_filter (
3106 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
3107 xdnd_filters[i].func, NULL);
3114 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
3116 if (context->dest_window)
3118 switch (context->protocol)
3120 case GDK_DRAG_PROTO_MOTIF:
3121 motif_send_leave (context, time);
3123 case GDK_DRAG_PROTO_XDND:
3124 xdnd_send_leave (context);
3126 case GDK_DRAG_PROTO_ROOTWIN:
3127 case GDK_DRAG_PROTO_NONE:
3132 g_object_unref (context->dest_window);
3133 context->dest_window = NULL;
3139 * @window: the source window for this drag.
3140 * @targets: the offered targets, as list of #GdkAtom<!-- -->s
3142 * Starts a drag and creates a new drag context for it.
3144 * This function is called by the drag source.
3146 * Return value: a newly created #GdkDragContext.
3149 gdk_drag_begin (GdkWindow *window,
3152 GdkDragContext *new_context;
3153 GdkDisplay *display;
3156 g_return_val_if_fail (window != NULL, NULL);
3157 g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), NULL);
3159 new_context = gdk_drag_context_new ();
3160 new_context->is_source = TRUE;
3161 new_context->source_window = window;
3162 g_object_ref (window);
3164 new_context->targets = g_list_copy (targets);
3165 precache_target_list (new_context);
3167 new_context->actions = 0;
3169 display = gdk_drawable_get_display (GDK_DRAWABLE (window));
3170 device = gdk_display_get_core_pointer (display);
3171 gdk_drag_context_set_device (new_context, device);
3176 static GdkNativeWindow
3177 _gdk_drag_get_protocol_for_display (GdkDisplay *display,
3178 GdkNativeWindow xid,
3179 GdkDragProtocol *protocol,
3184 GdkNativeWindow retval;
3185 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
3187 base_precache_atoms (display);
3189 /* Check for a local drag
3191 window = gdk_window_lookup_for_display (display, xid);
3193 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3195 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3197 *protocol = GDK_DRAG_PROTO_XDND;
3199 xdnd_precache_atoms (display);
3200 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3203 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3205 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3206 GDK_NOTE (DND, g_message ("Entering root window\n"));
3210 else if ((retval = xdnd_check_dest (display, xid, version)))
3212 *protocol = GDK_DRAG_PROTO_XDND;
3213 xdnd_precache_atoms (display);
3214 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3217 else if ((retval = motif_check_dest (display, xid)))
3219 *protocol = GDK_DRAG_PROTO_MOTIF;
3220 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3225 /* Check if this is a root window */
3227 gboolean rootwin = FALSE;
3230 unsigned long nitems, after;
3231 unsigned char *data;
3233 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3236 gdk_error_trap_push ();
3240 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3241 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
3242 0, 0, False, AnyPropertyType,
3243 &type, &format, &nitems, &after, &data) == Success &&
3251 /* Until I find out what window manager the next one is for,
3252 * I'm leaving it commented out. It's supported in the
3253 * xscreensaver sources, though.
3258 if (XGetWindowProperty (gdk_display, win,
3259 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
3260 0, 0, False, AnyPropertyType,
3261 &type, &format, &nitems, &data) &&
3270 gdk_error_trap_pop ();
3274 GDK_NOTE (DND, g_message ("Entering root window\n"));
3275 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3280 *protocol = GDK_DRAG_PROTO_NONE;
3282 return 0; /* a.k.a. None */
3286 * gdk_drag_get_protocol_for_display:
3287 * @display: the #GdkDisplay where the destination window resides
3288 * @xid: the windowing system id of the destination window.
3289 * @protocol: location where the supported DND protocol is returned.
3290 * @returns: the windowing system id of the window where the drop should happen. This
3291 * may be @xid or the id of a proxy window, or zero if @xid doesn't
3292 * support Drag and Drop.
3294 * Finds out the DND protocol supported by a window.
3299 gdk_drag_get_protocol_for_display (GdkDisplay *display,
3300 GdkNativeWindow xid,
3301 GdkDragProtocol *protocol)
3303 return _gdk_drag_get_protocol_for_display (display, xid, protocol, NULL);
3306 static GdkWindowCache *
3307 drag_context_find_window_cache (GdkDragContext *context,
3310 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3312 GdkWindowCache *cache;
3314 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
3316 cache = tmp_list->data;
3317 if (cache->screen == screen)
3321 cache = gdk_window_cache_new (screen);
3322 private->window_caches = g_slist_prepend (private->window_caches, cache);
3328 * gdk_drag_find_window_for_screen:
3329 * @context: a #GdkDragContext
3330 * @drag_window: a window which may be at the pointer position, but
3331 * should be ignored, since it is put up by the drag source as an icon.
3332 * @screen: the screen where the destination window is sought.
3333 * @x_root: the x position of the pointer in root coordinates.
3334 * @y_root: the y position of the pointer in root coordinates.
3335 * @dest_window: (out): location to store the destination window in.
3336 * @protocol: (out): location to store the DND protocol in.
3338 * Finds the destination window and DND protocol to use at the
3339 * given pointer position.
3341 * This function is called by the drag source to obtain the
3342 * @dest_window and @protocol parameters for gdk_drag_motion().
3347 gdk_drag_find_window_for_screen (GdkDragContext *context,
3348 GdkWindow *drag_window,
3352 GdkWindow **dest_window,
3353 GdkDragProtocol *protocol)
3355 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3356 GdkWindowCache *window_cache;
3357 GdkDisplay *display;
3360 g_return_if_fail (context != NULL);
3362 display = GDK_WINDOW_DISPLAY (context->source_window);
3364 window_cache = drag_context_find_window_cache (context, screen);
3366 dest = get_client_window_at_coords (window_cache,
3367 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3368 GDK_DRAWABLE_XID (drag_window) : None,
3371 if (private->dest_xid != dest)
3374 private->dest_xid = dest;
3376 /* Check if new destination accepts drags, and which protocol */
3378 /* There is some ugliness here. We actually need to pass
3379 * _three_ pieces of information to drag_motion - dest_window,
3380 * protocol, and the XID of the unproxied window. The first
3381 * two are passed explicitely, the third implicitly through
3382 * protocol->dest_xid.
3384 if ((recipient = _gdk_drag_get_protocol_for_display (display, dest,
3385 protocol, &private->version)))
3387 *dest_window = gdk_window_lookup_for_display (display, recipient);
3389 g_object_ref (*dest_window);
3391 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3394 *dest_window = NULL;
3398 *dest_window = context->dest_window;
3400 g_object_ref (*dest_window);
3401 *protocol = context->protocol;
3407 * @context: a #GdkDragContext.
3408 * @dest_window: the new destination window, obtained by
3409 * gdk_drag_find_window().
3410 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3411 * @x_root: the x position of the pointer in root coordinates.
3412 * @y_root: the y position of the pointer in root coordinates.
3413 * @suggested_action: the suggested action.
3414 * @possible_actions: the possible actions.
3415 * @time_: the timestamp for this operation.
3417 * Updates the drag context when the pointer moves or the
3418 * set of actions changes.
3420 * This function is called by the drag source.
3422 * Return value: FIXME
3425 gdk_drag_motion (GdkDragContext *context,
3426 GdkWindow *dest_window,
3427 GdkDragProtocol protocol,
3430 GdkDragAction suggested_action,
3431 GdkDragAction possible_actions,
3434 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3436 g_return_val_if_fail (context != NULL, FALSE);
3437 g_return_val_if_fail (dest_window == NULL || GDK_WINDOW_IS_X11 (dest_window), FALSE);
3439 private->old_actions = context->actions;
3440 context->actions = possible_actions;
3442 if (private->old_actions != possible_actions)
3443 private->xdnd_actions_set = FALSE;
3445 if (protocol == GDK_DRAG_PROTO_XDND && private->version == 0)
3447 /* This ugly hack is necessary since GTK+ doesn't know about
3448 * the XDND protocol version, and in particular doesn't know
3449 * that gdk_drag_find_window_for_screen() has the side-effect
3450 * of setting private->version, and therefore sometimes call
3451 * gdk_drag_motion() without a prior call to
3452 * gdk_drag_find_window_for_screen(). This happens, e.g.
3453 * when GTK+ is proxying DND events to embedded windows.
3457 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3459 xdnd_check_dest (display,
3460 GDK_DRAWABLE_XID (dest_window),
3465 /* When we have a Xdnd target, make sure our XdndActionList
3466 * matches the current actions;
3468 if (protocol == GDK_DRAG_PROTO_XDND && !private->xdnd_actions_set)
3472 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3473 xdnd_set_actions (context);
3474 else if (context->dest_window == dest_window)
3476 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3477 GdkDragContext *dest_context;
3479 dest_context = gdk_drag_context_find (display, FALSE,
3480 GDK_DRAWABLE_XID (context->source_window),
3481 GDK_DRAWABLE_XID (dest_window));
3485 dest_context->actions = context->actions;
3486 PRIVATE_DATA (dest_context)->xdnd_have_actions = TRUE;
3492 if (context->dest_window != dest_window)
3494 GdkEvent *temp_event;
3496 /* Send a leave to the last destination */
3497 gdk_drag_do_leave (context, time);
3498 private->drag_status = GDK_DRAG_STATUS_DRAG;
3500 /* Check if new destination accepts drags, and which protocol */
3504 context->dest_window = dest_window;
3505 private->drop_xid = private->dest_xid;
3506 g_object_ref (context->dest_window);
3507 context->protocol = protocol;
3511 case GDK_DRAG_PROTO_MOTIF:
3512 motif_send_enter (context, time);
3515 case GDK_DRAG_PROTO_XDND:
3516 xdnd_send_enter (context);
3519 case GDK_DRAG_PROTO_ROOTWIN:
3520 case GDK_DRAG_PROTO_NONE:
3524 private->old_action = suggested_action;
3525 context->suggested_action = suggested_action;
3526 private->old_actions = possible_actions;
3530 context->dest_window = NULL;
3531 private->drop_xid = None;
3532 context->action = 0;
3535 /* Push a status event, to let the client know that
3538 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3539 temp_event->dnd.window = g_object_ref (context->source_window);
3540 /* We use this to signal a synthetic status. Perhaps
3541 * we should use an extra field...
3543 temp_event->dnd.send_event = TRUE;
3545 temp_event->dnd.context = g_object_ref (context);
3546 temp_event->dnd.time = time;
3547 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3549 gdk_event_put (temp_event);
3550 gdk_event_free (temp_event);
3554 private->old_action = context->suggested_action;
3555 context->suggested_action = suggested_action;
3558 /* Send a drag-motion event */
3560 private->last_x = x_root;
3561 private->last_y = y_root;
3563 if (context->dest_window)
3565 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3567 switch (context->protocol)
3569 case GDK_DRAG_PROTO_MOTIF:
3570 motif_send_motion (context, x_root, y_root, suggested_action, time);
3573 case GDK_DRAG_PROTO_XDND:
3574 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3577 case GDK_DRAG_PROTO_ROOTWIN:
3579 GdkEvent *temp_event;
3580 /* GTK+ traditionally has used application/x-rootwin-drop,
3581 * but the XDND spec specifies x-rootwindow-drop.
3583 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3584 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3586 if (g_list_find (context->targets,
3587 GDK_ATOM_TO_POINTER (target1)) ||
3588 g_list_find (context->targets,
3589 GDK_ATOM_TO_POINTER (target2)))
3590 context->action = context->suggested_action;
3592 context->action = 0;
3594 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3595 temp_event->dnd.window = g_object_ref (context->source_window);
3596 temp_event->dnd.send_event = FALSE;
3597 temp_event->dnd.context = g_object_ref (context);
3598 temp_event->dnd.time = time;
3599 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3601 gdk_event_put (temp_event);
3602 gdk_event_free (temp_event);
3605 case GDK_DRAG_PROTO_NONE:
3606 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3621 * @context: a #GdkDragContext.
3622 * @time_: the timestamp for this operation.
3624 * Drops on the current destination.
3626 * This function is called by the drag source.
3629 gdk_drag_drop (GdkDragContext *context,
3632 g_return_if_fail (context != NULL);
3634 if (context->dest_window)
3636 switch (context->protocol)
3638 case GDK_DRAG_PROTO_MOTIF:
3639 motif_send_leave (context, time);
3640 motif_send_drop (context, time);
3643 case GDK_DRAG_PROTO_XDND:
3644 xdnd_send_drop (context, time);
3647 case GDK_DRAG_PROTO_ROOTWIN:
3648 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3650 case GDK_DRAG_PROTO_NONE:
3651 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3661 * @context: a #GdkDragContext.
3662 * @time_: the timestamp for this operation.
3664 * Aborts a drag without dropping.
3666 * This function is called by the drag source.
3669 gdk_drag_abort (GdkDragContext *context,
3672 g_return_if_fail (context != NULL);
3674 gdk_drag_do_leave (context, time);
3677 /* Destination side */
3681 * @context: a #GdkDragContext.
3682 * @action: the selected action which will be taken when a drop happens,
3683 * or 0 to indicate that a drop will not be accepted.
3684 * @time_: the timestamp for this operation.
3686 * Selects one of the actions offered by the drag source.
3688 * This function is called by the drag destination in response to
3689 * gdk_drag_motion() called by the drag source.
3692 gdk_drag_status (GdkDragContext *context,
3693 GdkDragAction action,
3696 GdkDragContextPrivateX11 *private;
3698 GdkDisplay *display;
3700 g_return_if_fail (context != NULL);
3702 private = PRIVATE_DATA (context);
3703 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3705 context->action = action;
3707 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3709 gboolean need_coords = FALSE;
3711 xev.xclient.type = ClientMessage;
3712 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3713 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3714 xev.xclient.format = 8;
3715 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3717 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3719 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3723 if ((action != 0) != (private->old_action != 0))
3727 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3731 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3735 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3740 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3744 case GDK_ACTION_MOVE:
3745 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3747 case GDK_ACTION_COPY:
3748 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3750 case GDK_ACTION_LINK:
3751 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3754 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3759 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3761 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3763 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3767 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3768 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3771 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3773 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3774 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3776 if (!_gdk_send_xevent (display,
3777 GDK_DRAWABLE_XID (context->source_window),
3780 g_message ("Send event to %lx failed",
3781 GDK_DRAWABLE_XID (context->source_window)));
3783 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3785 xev.xclient.type = ClientMessage;
3786 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3787 xev.xclient.format = 32;
3788 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3790 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3791 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3792 xev.xclient.data.l[2] = 0;
3793 xev.xclient.data.l[3] = 0;
3794 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3796 if (!xdnd_send_xevent (context, context->source_window,
3799 g_message ("Send event to %lx failed",
3800 GDK_DRAWABLE_XID (context->source_window)));
3803 private->old_action = action;
3808 * @context: a #GdkDragContext.
3809 * @ok: %TRUE if the drop is accepted.
3810 * @time_: the timestamp for this operation.
3812 * Accepts or rejects a drop.
3814 * This function is called by the drag destination in response
3815 * to a drop initiated by the drag source.
3818 gdk_drop_reply (GdkDragContext *context,
3822 GdkDragContextPrivateX11 *private;
3824 g_return_if_fail (context != NULL);
3826 private = PRIVATE_DATA (context);
3828 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3830 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3833 xev.xclient.type = ClientMessage;
3834 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3835 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3836 xev.xclient.format = 8;
3838 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3839 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3841 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3842 (XmDROP_SITE_VALID << 4) |
3843 (XmDROP_NOOP << 8) |
3846 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3847 (XmNO_DROP_SITE << 4) |
3848 (XmDROP_NOOP << 8) |
3849 (XmDROP_CANCEL << 12);
3850 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3851 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3852 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3853 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3854 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3856 _gdk_send_xevent (display,
3857 GDK_DRAWABLE_XID (context->source_window),
3864 * @context: a #GtkDragContext.
3865 * @success: %TRUE if the data was successfully received.
3866 * @time_: the timestamp for this operation.
3868 * Ends the drag operation after a drop.
3870 * This function is called by the drag destination.
3873 gdk_drop_finish (GdkDragContext *context,
3877 g_return_if_fail (context != NULL);
3879 if (context->protocol == GDK_DRAG_PROTO_XDND)
3881 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3884 xev.xclient.type = ClientMessage;
3885 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3886 xev.xclient.format = 32;
3887 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3889 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3892 xev.xclient.data.l[1] = 1;
3893 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3898 xev.xclient.data.l[1] = 0;
3899 xev.xclient.data.l[2] = None;
3901 xev.xclient.data.l[3] = 0;
3902 xev.xclient.data.l[4] = 0;
3904 if (!xdnd_send_xevent (context, context->source_window,
3907 g_message ("Send event to %lx failed",
3908 GDK_DRAWABLE_XID (context->source_window)));
3914 * gdk_window_register_dnd:
3915 * @window: a #GdkWindow.
3917 * Registers a window as a potential drop destination.
3920 gdk_window_register_dnd (GdkWindow *window)
3922 static const gulong xdnd_version = 5;
3923 MotifDragReceiverInfo info;
3924 Atom motif_drag_receiver_info_atom;
3925 GdkDisplay *display = gdk_drawable_get_display (window);
3927 g_return_if_fail (window != NULL);
3929 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
3932 base_precache_atoms (display);
3934 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3937 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3939 /* Set Motif drag receiver information property */
3941 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3942 "_MOTIF_DRAG_RECEIVER_INFO");
3943 /* initialize to zero to avoid writing uninitialized data to socket */
3944 memset(&info, 0, sizeof(info));
3945 info.byte_order = local_byte_order;
3946 info.protocol_version = 0;
3947 info.protocol_style = XmDRAG_DYNAMIC;
3948 info.proxy_window = None;
3949 info.num_drop_sites = 0;
3950 info.total_size = sizeof(info);
3952 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3953 motif_drag_receiver_info_atom,
3954 motif_drag_receiver_info_atom,
3961 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3962 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3963 GDK_DRAWABLE_XID (window),
3964 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3965 XA_ATOM, 32, PropModeReplace,
3966 (guchar *)&xdnd_version, 1);
3970 * gdk_drag_get_selection:
3971 * @context: a #GdkDragContext.
3973 * Returns the selection atom for the current source window.
3975 * Return value: the selection atom.
3978 gdk_drag_get_selection (GdkDragContext *context)
3980 g_return_val_if_fail (context != NULL, GDK_NONE);
3982 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3983 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3984 (PRIVATE_DATA (context))->motif_selection);
3985 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3986 return gdk_atom_intern_static_string ("XdndSelection");
3992 * gdk_drag_drop_succeeded:
3993 * @context: a #GdkDragContext
3995 * Returns whether the dropped data has been successfully
3996 * transferred. This function is intended to be used while
3997 * handling a %GDK_DROP_FINISHED event, its return value is
3998 * meaningless at other times.
4000 * Return value: %TRUE if the drop was successful.
4005 gdk_drag_drop_succeeded (GdkDragContext *context)
4007 GdkDragContextPrivateX11 *private;
4009 g_return_val_if_fail (context != NULL, FALSE);
4011 private = PRIVATE_DATA (context);
4013 return !private->drop_failed;
4016 #define __GDK_DND_X11_C__
4017 #include "gdkaliasdef.c"