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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gdkx11dnd.h"
28 #include "gdkdndprivate.h"
31 #include "gdkinternals.h"
33 #include "gdkproperty.h"
34 #include "gdkprivate-x11.h"
35 #include "gdkscreen-x11.h"
36 #include "gdkdisplay-x11.h"
39 #include <X11/Xutil.h>
40 #include <X11/Xatom.h>
41 #include <X11/extensions/shape.h>
42 #ifdef HAVE_XCOMPOSITE
43 #include <X11/extensions/Xcomposite.h>
50 GDK_DRAG_STATUS_MOTION_WAIT,
51 GDK_DRAG_STATUS_ACTION_WAIT,
57 gint x, y, width, height;
59 gboolean shape_selected;
61 cairo_region_t *shape;
66 GHashTable *child_hash;
73 struct _GdkX11DragContext
75 GdkDragContext context;
80 guint16 last_x; /* Coordinates from last event */
82 GdkDragAction old_action; /* The last action we sent to the source */
83 GdkDragAction old_actions; /* The last actions we sent to the source */
84 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
85 guint version; /* Xdnd protocol version */
87 GSList *window_caches;
89 Window dest_xid; /* The last window we looked up */
90 Window drop_xid; /* The (non-proxied) window that is receiving drops */
91 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
92 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
93 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
94 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
95 guint drag_status : 4; /* current status of drag */
96 guint drop_failed : 1; /* Whether the drop was unsuccessful */
99 struct _GdkX11DragContextClass
101 GdkDragContextClass parent_class;
104 /* Forward declarations */
106 static GdkWindowCache *gdk_window_cache_get (GdkScreen *screen);
107 static GdkWindowCache *gdk_window_cache_ref (GdkWindowCache *cache);
108 static void gdk_window_cache_unref (GdkWindowCache *cache);
110 static void motif_read_target_table (GdkDisplay *display);
112 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
116 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
119 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
122 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
125 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
128 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
131 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
135 static void xdnd_manage_source_filter (GdkDragContext *context,
137 gboolean add_filter);
139 static GList *contexts;
140 static GSList *window_caches;
142 static const struct {
143 const char *atom_name;
146 { "_MOTIF_DRAG_AND_DROP_MESSAGE", motif_dnd_filter },
148 { "XdndEnter", xdnd_enter_filter },
149 { "XdndLeave", xdnd_leave_filter },
150 { "XdndPosition", xdnd_position_filter },
151 { "XdndStatus", xdnd_status_filter },
152 { "XdndFinished", xdnd_finished_filter },
153 { "XdndDrop", xdnd_drop_filter },
157 G_DEFINE_TYPE (GdkX11DragContext, gdk_x11_drag_context, GDK_TYPE_DRAG_CONTEXT)
160 gdk_x11_drag_context_init (GdkX11DragContext *context)
162 contexts = g_list_prepend (contexts, context);
165 static void gdk_x11_drag_context_finalize (GObject *object);
166 static GdkWindow * gdk_x11_drag_context_find_window (GdkDragContext *context,
167 GdkWindow *drag_window,
171 GdkDragProtocol *protocol);
172 static gboolean gdk_x11_drag_context_drag_motion (GdkDragContext *context,
173 GdkWindow *dest_window,
174 GdkDragProtocol protocol,
177 GdkDragAction suggested_action,
178 GdkDragAction possible_actions,
180 static void gdk_x11_drag_context_drag_status (GdkDragContext *context,
181 GdkDragAction action,
183 static void gdk_x11_drag_context_drag_abort (GdkDragContext *context,
185 static void gdk_x11_drag_context_drag_drop (GdkDragContext *context,
187 static void gdk_x11_drag_context_drop_reply (GdkDragContext *context,
190 static void gdk_x11_drag_context_drop_finish (GdkDragContext *context,
193 static gboolean gdk_x11_drag_context_drop_status (GdkDragContext *context);
194 static GdkAtom gdk_x11_drag_context_get_selection (GdkDragContext *context);
197 gdk_x11_drag_context_class_init (GdkX11DragContextClass *klass)
199 GObjectClass *object_class = G_OBJECT_CLASS (klass);
200 GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
202 object_class->finalize = gdk_x11_drag_context_finalize;
204 context_class->find_window = gdk_x11_drag_context_find_window;
205 context_class->drag_status = gdk_x11_drag_context_drag_status;
206 context_class->drag_motion = gdk_x11_drag_context_drag_motion;
207 context_class->drag_abort = gdk_x11_drag_context_drag_abort;
208 context_class->drag_drop = gdk_x11_drag_context_drag_drop;
209 context_class->drop_reply = gdk_x11_drag_context_drop_reply;
210 context_class->drop_finish = gdk_x11_drag_context_drop_finish;
211 context_class->drop_status = gdk_x11_drag_context_drop_status;
212 context_class->get_selection = gdk_x11_drag_context_get_selection;
216 gdk_x11_drag_context_finalize (GObject *object)
218 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
219 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (object);
221 if (context->source_window)
223 if ((context->protocol == GDK_DRAG_PROTO_XDND) && !context->is_source)
224 xdnd_manage_source_filter (context, context->source_window, FALSE);
227 g_slist_free_full (x11_context->window_caches, (GDestroyNotify)gdk_window_cache_unref);
228 x11_context->window_caches = NULL;
230 contexts = g_list_remove (contexts, context);
232 G_OBJECT_CLASS (gdk_x11_drag_context_parent_class)->finalize (object);
237 static GdkDragContext *
238 gdk_drag_context_find (GdkDisplay *display,
243 GList *tmp_list = contexts;
244 GdkDragContext *context;
245 GdkX11DragContext *context_x11;
246 Window context_dest_xid;
250 context = (GdkDragContext *)tmp_list->data;
251 context_x11 = (GdkX11DragContext *)context;
253 if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
254 (context->dest_window && gdk_window_get_display (context->dest_window) != display))
257 context_dest_xid = context->dest_window
258 ? (context_x11->drop_xid
259 ? context_x11->drop_xid
260 : GDK_WINDOW_XID (context->dest_window))
263 if ((!context->is_source == !is_source) &&
264 ((source_xid == None) || (context->source_window &&
265 (GDK_WINDOW_XID (context->source_window) == source_xid))) &&
266 ((dest_xid == None) || (context_dest_xid == dest_xid)))
269 tmp_list = tmp_list->next;
276 precache_target_list (GdkDragContext *context)
278 if (context->targets)
280 GPtrArray *targets = g_ptr_array_new ();
284 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
285 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
287 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
288 (const gchar **)targets->pdata,
291 for (i =0; i < targets->len; i++)
292 g_free (targets->pdata[i]);
294 g_ptr_array_free (targets, TRUE);
298 /* Utility functions */
301 free_cache_child (GdkCacheChild *child,
305 cairo_region_destroy (child->shape);
307 if (child->shape_selected && display)
309 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
311 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
318 gdk_window_cache_add (GdkWindowCache *cache,
326 GdkCacheChild *child = g_new (GdkCacheChild, 1);
331 child->width = width;
332 child->height = height;
333 child->mapped = mapped;
334 child->shape_selected = FALSE;
335 child->shape_valid = FALSE;
338 cache->children = g_list_prepend (cache->children, child);
339 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
343 static GdkFilterReturn
344 gdk_window_cache_shape_filter (GdkXEvent *xev,
348 XEvent *xevent = (XEvent *)xev;
349 GdkWindowCache *cache = data;
351 GdkX11Display *display = GDK_X11_DISPLAY (gdk_screen_get_display (cache->screen));
353 if (display->have_shapes &&
354 xevent->type == display->shape_event_base + ShapeNotify)
356 XShapeEvent *xse = (XShapeEvent*)xevent;
359 node = g_hash_table_lookup (cache->child_hash,
360 GUINT_TO_POINTER (xse->window));
363 GdkCacheChild *child = node->data;
364 child->shape_valid = FALSE;
367 cairo_region_destroy (child->shape);
372 return GDK_FILTER_REMOVE;
375 return GDK_FILTER_CONTINUE;
378 static GdkFilterReturn
379 gdk_window_cache_filter (GdkXEvent *xev,
383 XEvent *xevent = (XEvent *)xev;
384 GdkWindowCache *cache = data;
386 switch (xevent->type)
388 case CirculateNotify:
390 case ConfigureNotify:
392 XConfigureEvent *xce = &xevent->xconfigure;
395 node = g_hash_table_lookup (cache->child_hash,
396 GUINT_TO_POINTER (xce->window));
399 GdkCacheChild *child = node->data;
402 child->width = xce->width;
403 child->height = xce->height;
404 if (xce->above == None && (node->next))
406 GList *last = g_list_last (cache->children);
407 cache->children = g_list_remove_link (cache->children, node);
414 GList *above_node = g_hash_table_lookup (cache->child_hash,
415 GUINT_TO_POINTER (xce->above));
416 if (above_node && node->next != above_node)
418 /* Put the window above (before in the list) above_node */
419 cache->children = g_list_remove_link (cache->children, node);
420 node->prev = above_node->prev;
422 node->prev->next = node;
424 cache->children = node;
425 node->next = above_node;
426 above_node->prev = node;
434 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
436 if (!g_hash_table_lookup (cache->child_hash,
437 GUINT_TO_POINTER (xcwe->window)))
438 gdk_window_cache_add (cache, xcwe->window,
439 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
445 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
448 node = g_hash_table_lookup (cache->child_hash,
449 GUINT_TO_POINTER (xdwe->window));
452 GdkCacheChild *child = node->data;
454 g_hash_table_remove (cache->child_hash,
455 GUINT_TO_POINTER (xdwe->window));
456 cache->children = g_list_remove_link (cache->children, node);
457 /* window is destroyed, no need to disable ShapeNotify */
458 free_cache_child (child, NULL);
459 g_list_free_1 (node);
465 XMapEvent *xme = &xevent->xmap;
468 node = g_hash_table_lookup (cache->child_hash,
469 GUINT_TO_POINTER (xme->window));
472 GdkCacheChild *child = node->data;
473 child->mapped = TRUE;
481 XMapEvent *xume = &xevent->xmap;
484 node = g_hash_table_lookup (cache->child_hash,
485 GUINT_TO_POINTER (xume->window));
488 GdkCacheChild *child = node->data;
489 child->mapped = FALSE;
494 return GDK_FILTER_CONTINUE;
496 return GDK_FILTER_REMOVE;
499 static GdkWindowCache *
500 gdk_window_cache_new (GdkScreen *screen)
502 XWindowAttributes xwa;
503 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
504 GdkWindow *root_window = gdk_screen_get_root_window (screen);
505 GdkChildInfoX11 *children;
507 #ifdef HAVE_XCOMPOSITE
511 GdkWindowCache *result = g_new (GdkWindowCache, 1);
513 result->children = NULL;
514 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
515 result->screen = screen;
516 result->ref_count = 1;
518 XGetWindowAttributes (xdisplay, GDK_WINDOW_XID (root_window), &xwa);
519 result->old_event_mask = xwa.your_event_mask;
521 if (G_UNLIKELY (!GDK_X11_DISPLAY (GDK_X11_SCREEN (screen)->display)->trusted_client))
523 GList *toplevel_windows, *list;
525 gint x, y, width, height;
527 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
528 for (list = toplevel_windows; list; list = list->next)
530 window = GDK_WINDOW (list->data);
531 gdk_window_get_geometry (window, &x, &y, &width, &height);
532 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
534 gdk_window_is_visible (window));
536 g_list_free (toplevel_windows);
540 XSelectInput (xdisplay, GDK_WINDOW_XID (root_window),
541 result->old_event_mask | SubstructureNotifyMask);
542 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
543 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
545 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
546 GDK_WINDOW_XID (root_window),
548 &children, &nchildren))
551 for (i = 0; i < nchildren ; i++)
553 gdk_window_cache_add (result, children[i].window,
554 children[i].x, children[i].y, children[i].width, children[i].height,
555 children[i].is_mapped);
560 #ifdef HAVE_XCOMPOSITE
562 * Add the composite overlay window to the cache, as this can be a reasonable
563 * Xdnd proxy as well.
564 * This is only done when the screen is composited in order to avoid mapping
565 * the COW. We assume that the CM is using the COW (which is true for pretty
566 * much any CM currently in use).
568 if (gdk_screen_is_composited (screen))
570 cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
571 gdk_window_cache_add (result, cow, 0, 0, gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
572 XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
580 gdk_window_cache_destroy (GdkWindowCache *cache)
582 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
585 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
586 GDK_WINDOW_XID (root_window),
587 cache->old_event_mask);
588 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
589 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
591 display = gdk_screen_get_display (cache->screen);
593 gdk_x11_display_error_trap_push (display);
594 g_list_foreach (cache->children, (GFunc)free_cache_child, display);
595 gdk_x11_display_error_trap_pop_ignored (display);
597 g_list_free (cache->children);
598 g_hash_table_destroy (cache->child_hash);
603 static GdkWindowCache *
604 gdk_window_cache_ref (GdkWindowCache *cache)
606 cache->ref_count += 1;
612 gdk_window_cache_unref (GdkWindowCache *cache)
614 g_assert (cache->ref_count > 0);
616 cache->ref_count -= 1;
618 if (cache->ref_count == 0)
620 window_caches = g_slist_remove (window_caches, cache);
621 gdk_window_cache_destroy (cache);
626 gdk_window_cache_get (GdkScreen *screen)
629 GdkWindowCache *cache;
631 for (list = window_caches; list; list = list->next)
634 if (cache->screen == screen)
635 return gdk_window_cache_ref (cache);
638 cache = gdk_window_cache_new (screen);
640 window_caches = g_slist_prepend (window_caches, cache);
646 is_pointer_within_shape (GdkDisplay *display,
647 GdkCacheChild *child,
651 if (!child->shape_selected)
653 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
655 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
656 child->shape_selected = TRUE;
658 if (!child->shape_valid)
660 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
661 cairo_region_t *input_shape;
664 if (gdk_display_supports_shapes (display))
665 child->shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
666 child->xid, ShapeBounding);
669 if (gdk_display_supports_input_shapes (display))
670 input_shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
671 child->xid, ShapeInput);
673 if (child->shape && input_shape)
675 cairo_region_intersect (child->shape, input_shape);
676 cairo_region_destroy (input_shape);
678 else if (input_shape)
680 child->shape = input_shape;
684 child->shape_valid = TRUE;
687 return child->shape == NULL ||
688 cairo_region_contains_point (child->shape, x_pos, y_pos);
692 get_client_window_at_coords_recurse (GdkDisplay *display,
694 gboolean is_toplevel,
698 GdkChildInfoX11 *children;
699 unsigned int nchildren;
701 gboolean found_child = FALSE;
702 GdkChildInfoX11 child = { 0, };
703 gboolean has_wm_state = FALSE;
705 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
706 is_toplevel? &has_wm_state : NULL,
707 &children, &nchildren))
717 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
719 GdkChildInfoX11 *cur_child = &children[i];
721 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
722 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
723 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
736 if (child.has_wm_state)
739 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
746 get_client_window_at_coords (GdkWindowCache *cache,
752 Window retval = None;
755 display = gdk_screen_get_display (cache->screen);
757 gdk_x11_display_error_trap_push (display);
759 tmp_list = cache->children;
761 while (tmp_list && !retval)
763 GdkCacheChild *child = tmp_list->data;
765 if ((child->xid != ignore) && (child->mapped))
767 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
768 (y_root >= child->y) && (y_root < child->y + child->height))
770 if (!is_pointer_within_shape (display, child,
774 tmp_list = tmp_list->next;
778 retval = get_client_window_at_coords_recurse (display,
786 tmp_list = tmp_list->next;
789 gdk_x11_display_error_trap_pop_ignored (display);
794 return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
797 /*************************************************************
798 ***************************** MOTIF *************************
799 *************************************************************/
801 /* values used in the message type for Motif DND */
814 /* Values used to specify type of protocol to use */
818 XmDRAG_PREFER_PREREGISTER,
820 XmDRAG_PREFER_DYNAMIC,
822 XmDRAG_PREFER_RECEIVER
825 /* Operation codes */
833 /* Drop site status */
835 XmNO_DROP_SITE = 0x01,
836 XmDROP_SITE_INVALID = 0x02,
837 XmDROP_SITE_VALID = 0x03
840 /* completion status */
848 /* Byte swapping routines. The motif specification leaves it
849 * up to us to save a few bytes in the client messages
851 #if G_BYTE_ORDER == G_BIG_ENDIAN
852 static gchar local_byte_order = 'B';
854 static gchar local_byte_order = 'l';
857 #ifdef G_ENABLE_DEBUG
859 print_target_list (GList *targets)
863 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
864 g_message ("\t%s", name);
866 targets = targets->next;
869 #endif /* G_ENABLE_DEBUG */
872 card16_to_host (guint16 x, gchar byte_order)
874 if (byte_order == local_byte_order)
877 return (x << 8) | (x >> 8);
881 card32_to_host (guint32 x, gchar byte_order)
883 if (byte_order == local_byte_order)
886 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
889 /* Motif packs together fields of varying length into the
890 * client message. We can't rely on accessing these
891 * through data.s[], data.l[], etc, because on some architectures
892 * (i.e., Alpha) these won't be valid for format == 8.
895 #define MOTIF_XCLIENT_BYTE(xevent,i) \
896 (xevent)->xclient.data.b[i]
897 #define MOTIF_XCLIENT_SHORT(xevent,i) \
898 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
899 #define MOTIF_XCLIENT_LONG(xevent,i) \
900 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
902 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
903 #define MOTIF_UNPACK_SHORT(xevent,i) \
904 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
905 #define MOTIF_UNPACK_LONG(xevent,i) \
906 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
908 /***** Dest side ***********/
910 /* Property placed on source windows */
911 typedef struct _MotifDragInitiatorInfo
914 guint8 protocol_version;
915 guint16 targets_index;
916 guint32 selection_atom;
917 } MotifDragInitiatorInfo;
919 /* Header for target table on the drag window */
920 typedef struct _MotifTargetTableHeader
923 guchar protocol_version;
926 } MotifTargetTableHeader;
928 /* Property placed on target windows */
929 typedef struct _MotifDragReceiverInfo
932 guint8 protocol_version;
933 guint8 protocol_style;
935 guint32 proxy_window;
936 guint16 num_drop_sites;
939 } MotifDragReceiverInfo;
941 /* Target table handling */
943 static GdkFilterReturn
944 motif_drag_window_filter (GdkXEvent *xevent,
948 XEvent *xev = (XEvent *)xevent;
949 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
950 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
952 switch (xev->xany.type)
955 display_x11->motif_drag_window = None;
956 display_x11->motif_drag_gdk_window = NULL;
959 if (display_x11->motif_target_lists &&
960 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
961 motif_read_target_table (display);
964 return GDK_FILTER_REMOVE;
968 motif_lookup_drag_window (GdkDisplay *display,
969 Display *lookup_xdisplay)
971 Window retval = None;
972 gulong bytes_after, nitems;
977 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
978 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
980 XA_WINDOW, &type, &format, &nitems, &bytes_after,
983 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
985 retval = *(Window *)data;
987 g_message ("Found drag window %#lx\n", GDK_X11_DISPLAY (display)->motif_drag_window));
996 /* Finds the window where global Motif drag information is stored.
997 * If it doesn't exist and 'create' is TRUE, create one.
1000 motif_find_drag_window (GdkDisplay *display,
1003 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1005 if (!display_x11->motif_drag_window)
1007 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
1008 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
1010 if (!display_x11->motif_drag_window && create)
1012 /* Create a persistant window. (Copied from LessTif) */
1013 Display *persistant_xdisplay;
1014 XSetWindowAttributes attr;
1015 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
1016 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
1018 XGrabServer (persistant_xdisplay);
1020 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
1022 if (!display_x11->motif_drag_window)
1024 attr.override_redirect = True;
1025 attr.event_mask = PropertyChangeMask;
1027 display_x11->motif_drag_window =
1028 XCreateWindow (persistant_xdisplay,
1029 RootWindow (persistant_xdisplay, 0),
1030 -100, -100, 10, 10, 0, 0,
1031 InputOnly, (Visual *)CopyFromParent,
1032 (CWOverrideRedirect | CWEventMask), &attr);
1035 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
1037 XChangeProperty (persistant_xdisplay,
1038 RootWindow (persistant_xdisplay, 0),
1039 motif_drag_window_atom, XA_WINDOW,
1040 32, PropModeReplace,
1041 (guchar *)&motif_drag_window_atom, 1);
1043 XUngrabServer (persistant_xdisplay);
1044 XCloseDisplay (persistant_xdisplay);
1047 /* There is a miniscule race condition here if the drag window
1048 * gets destroyed exactly now.
1050 if (display_x11->motif_drag_window)
1052 display_x11->motif_drag_gdk_window =
1053 gdk_x11_window_foreign_new_for_display (display, display_x11->motif_drag_window);
1054 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
1055 motif_drag_window_filter,
1060 return display_x11->motif_drag_window;
1064 motif_read_target_table (GdkDisplay *display)
1066 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1067 gulong bytes_after, nitems;
1072 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1074 if (display_x11->motif_target_lists)
1076 for (i=0; i<display_x11->motif_n_target_lists; i++)
1077 g_list_free (display_x11->motif_target_lists[i]);
1079 g_free (display_x11->motif_target_lists);
1080 display_x11->motif_target_lists = NULL;
1081 display_x11->motif_n_target_lists = 0;
1084 if (motif_find_drag_window (display, FALSE))
1087 MotifTargetTableHeader *header = NULL;
1088 guchar *target_bytes = NULL;
1090 gboolean success = FALSE;
1092 gdk_x11_display_error_trap_push (display);
1093 XGetWindowProperty (display_x11->xdisplay,
1094 display_x11->motif_drag_window,
1095 motif_drag_targets_atom,
1096 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1097 motif_drag_targets_atom,
1098 &type, &format, &nitems, &bytes_after,
1101 if (gdk_x11_display_error_trap_pop (display) ||
1102 (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1105 header = (MotifTargetTableHeader *)data;
1107 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1108 header->total_size = card32_to_host (header->total_size, header->byte_order);
1110 gdk_x11_display_error_trap_push (display);
1111 XGetWindowProperty (display_x11->xdisplay,
1112 display_x11->motif_drag_window,
1113 motif_drag_targets_atom,
1114 (sizeof(MotifTargetTableHeader)+3)/4,
1115 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1117 motif_drag_targets_atom, &type, &format, &nitems,
1118 &bytes_after, &target_bytes);
1120 if (gdk_x11_display_error_trap_pop (display) ||
1121 (format != 8) || (bytes_after != 0) ||
1122 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1125 display_x11->motif_n_target_lists = header->n_lists;
1126 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1129 for (i=0; i<header->n_lists; i++)
1134 if (p + sizeof(guint16) - target_bytes > nitems)
1137 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1139 /* We need to make a copy of the targets, since it may
1142 targets = g_new (guint32, n_targets);
1143 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1145 p += sizeof(guint16) + n_targets * sizeof(guint32);
1146 if (p - target_bytes > nitems)
1149 for (j=0; j<n_targets; j++)
1150 display_x11->motif_target_lists[i] =
1151 g_list_prepend (display_x11->motif_target_lists[i],
1152 GUINT_TO_POINTER (card32_to_host (targets[j],
1153 header->byte_order)));
1155 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1165 XFree (target_bytes);
1169 if (display_x11->motif_target_lists)
1171 g_free (display_x11->motif_target_lists);
1172 display_x11->motif_target_lists = NULL;
1173 display_x11->motif_n_target_lists = 0;
1175 g_warning ("Error reading Motif target table\n");
1181 targets_sort_func (gconstpointer a, gconstpointer b)
1183 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1184 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1187 /* Check if given (sorted) list is in the targets table */
1189 motif_target_table_check (GdkDisplay *display,
1192 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1193 GList *tmp_list1, *tmp_list2;
1196 for (i=0; i<display_x11->motif_n_target_lists; i++)
1198 tmp_list1 = display_x11->motif_target_lists[i];
1201 while (tmp_list1 && tmp_list2)
1203 if (tmp_list1->data != tmp_list2->data)
1206 tmp_list1 = tmp_list1->next;
1207 tmp_list2 = tmp_list2->next;
1209 if (!tmp_list1 && !tmp_list2) /* Found it */
1217 motif_add_to_target_table (GdkDisplay *display,
1218 GList *targets) /* targets is list of GdkAtom */
1220 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1221 GList *sorted = NULL;
1226 /* make a sorted copy of the list */
1229 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1230 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1231 targets = targets->next;
1234 /* First check if it is there already */
1236 if (display_x11->motif_target_lists)
1237 index = motif_target_table_check (display, sorted);
1239 /* We need to grab the server while doing this, to ensure
1245 /* We need to make sure that it exists _before_ we grab the
1246 * server, since we can't open a new connection after we
1249 motif_find_drag_window (display, TRUE);
1251 gdk_x11_display_grab (display);
1252 motif_read_target_table (display);
1254 /* Check again, in case it was added in the meantime */
1255 if (display_x11->motif_target_lists)
1256 index = motif_target_table_check (display, sorted);
1260 guint32 total_size = 0;
1264 MotifTargetTableHeader *header;
1266 if (!display_x11->motif_target_lists)
1268 display_x11->motif_target_lists = g_new (GList *, 1);
1269 display_x11->motif_n_target_lists = 1;
1273 display_x11->motif_n_target_lists++;
1274 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1275 sizeof(GList *) * display_x11->motif_n_target_lists);
1277 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1279 index = display_x11->motif_n_target_lists - 1;
1281 total_size = sizeof (MotifTargetTableHeader);
1282 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1283 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1285 data = g_malloc (total_size);
1287 header = (MotifTargetTableHeader *)data;
1288 p = data + sizeof(MotifTargetTableHeader);
1290 header->byte_order = local_byte_order;
1291 header->protocol_version = 0;
1292 header->n_lists = display_x11->motif_n_target_lists;
1293 header->total_size = total_size;
1295 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1297 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1298 guint32 *targets = g_new (guint32, n_targets);
1299 guint32 *p32 = targets;
1301 tmp_list = display_x11->motif_target_lists[i];
1304 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1306 tmp_list = tmp_list->next;
1311 p += sizeof(guint16);
1313 memcpy (p, targets, n_targets * sizeof(guint32));
1316 p += sizeof(guint32) * n_targets;
1320 XChangeProperty (display_x11->xdisplay,
1321 display_x11->motif_drag_window,
1322 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1323 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1327 gdk_x11_display_ungrab (display);
1330 g_list_free (sorted);
1334 /* Translate flags */
1337 motif_dnd_translate_flags (GdkX11DragContext *context_x11,
1340 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1341 guint recommended_op = flags & 0x000f;
1342 guint possible_ops = (flags & 0x0f0) >> 4;
1344 switch (recommended_op)
1347 context->suggested_action = GDK_ACTION_MOVE;
1350 context->suggested_action = GDK_ACTION_COPY;
1353 context->suggested_action = GDK_ACTION_LINK;
1356 context->suggested_action = GDK_ACTION_COPY;
1360 context->actions = 0;
1361 if (possible_ops & XmDROP_MOVE)
1362 context->actions |= GDK_ACTION_MOVE;
1363 if (possible_ops & XmDROP_COPY)
1364 context->actions |= GDK_ACTION_COPY;
1365 if (possible_ops & XmDROP_LINK)
1366 context->actions |= GDK_ACTION_LINK;
1370 motif_dnd_get_flags (GdkDragContext *context)
1374 switch (context->suggested_action)
1376 case GDK_ACTION_MOVE:
1377 flags = XmDROP_MOVE;
1379 case GDK_ACTION_COPY:
1380 flags = XmDROP_COPY;
1382 case GDK_ACTION_LINK:
1383 flags = XmDROP_LINK;
1386 flags = XmDROP_NOOP;
1390 if (context->actions & GDK_ACTION_MOVE)
1391 flags |= XmDROP_MOVE << 8;
1392 if (context->actions & GDK_ACTION_COPY)
1393 flags |= XmDROP_COPY << 8;
1394 if (context->actions & GDK_ACTION_LINK)
1395 flags |= XmDROP_LINK << 8;
1403 motif_set_targets (GdkX11DragContext *context_x11)
1405 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1406 MotifDragInitiatorInfo info;
1408 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1410 info.byte_order = local_byte_order;
1411 info.protocol_version = 0;
1412 info.targets_index = motif_add_to_target_table (display, context->targets);
1417 g_snprintf (buf, 20, "_GDK_SELECTION_%d", i);
1419 context_x11->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1420 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), context_x11->motif_selection))
1424 info.selection_atom = context_x11->motif_selection;
1426 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1427 GDK_WINDOW_XID (context->source_window),
1428 context_x11->motif_selection,
1429 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1431 (guchar *)&info, sizeof (info));
1433 context_x11->motif_targets_set = 1;
1437 motif_check_dest (GdkDisplay *display,
1440 gboolean retval = FALSE;
1442 MotifDragReceiverInfo *info;
1445 unsigned long nitems, after;
1446 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1448 gdk_x11_display_error_trap_push (display);
1449 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1450 motif_drag_receiver_info_atom,
1451 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1452 &type, &format, &nitems, &after,
1455 if (gdk_x11_display_error_trap_pop (display) == 0)
1459 info = (MotifDragReceiverInfo *)data;
1461 if ((format == 8) && (nitems == sizeof(*info)))
1463 if ((info->protocol_version == 0) &&
1464 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1465 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1466 (info->protocol_style == XmDRAG_DYNAMIC)))
1472 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1479 return retval ? win : None;
1483 motif_send_enter (GdkX11DragContext *context_x11,
1486 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1487 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1490 if (!G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
1491 return; /* Motif Dnd requires getting properties on the root window */
1493 xev.xclient.type = ClientMessage;
1494 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1495 xev.xclient.format = 8;
1496 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1498 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1499 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1500 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1501 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1502 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XID (context->source_window);
1504 if (!context_x11->motif_targets_set)
1505 motif_set_targets (context_x11);
1507 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1508 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1510 if (!_gdk_x11_display_send_xevent (display,
1511 GDK_WINDOW_XID (context->dest_window),
1514 g_message ("Send event to %lx failed",
1515 GDK_WINDOW_XID (context->dest_window)));
1519 motif_send_leave (GdkX11DragContext *context_x11,
1522 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1523 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1526 xev.xclient.type = ClientMessage;
1527 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1528 xev.xclient.format = 8;
1529 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1531 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1532 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1533 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1534 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1535 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1536 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1537 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1539 if (!_gdk_x11_display_send_xevent (display,
1540 GDK_WINDOW_XID (context->dest_window),
1543 g_message ("Send event to %lx failed",
1544 GDK_WINDOW_XID (context->dest_window)));
1548 motif_send_motion (GdkX11DragContext *context_x11,
1551 GdkDragAction action,
1554 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1555 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1559 xev.xclient.type = ClientMessage;
1560 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1561 xev.xclient.format = 8;
1562 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1564 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1565 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1566 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1567 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1568 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1570 if ((context->suggested_action != context_x11->old_action) ||
1571 (context->actions != context_x11->old_actions))
1573 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1575 /* context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1580 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1582 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1583 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1585 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1589 if (!_gdk_x11_display_send_xevent (display,
1590 GDK_WINDOW_XID (context->dest_window),
1593 g_message ("Send event to %lx failed",
1594 GDK_WINDOW_XID (context->dest_window)));
1600 motif_send_drop (GdkX11DragContext *context_x11,
1603 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1604 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1607 xev.xclient.type = ClientMessage;
1608 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1609 xev.xclient.format = 8;
1610 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1612 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1613 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1614 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1615 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1617 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
1618 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
1620 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1621 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XID (context->source_window);
1623 if (!_gdk_x11_display_send_xevent (display,
1624 GDK_WINDOW_XID (context->dest_window),
1627 g_message ("Send event to %lx failed",
1628 GDK_WINDOW_XID (context->dest_window)));
1634 motif_read_initiator_info (GdkDisplay *display,
1635 Window source_window,
1646 MotifDragInitiatorInfo *initiator_info;
1648 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1650 gdk_x11_display_error_trap_push (display);
1651 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1652 0, sizeof(*initiator_info), FALSE,
1653 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1654 &type, &format, &nitems, &bytes_after,
1657 if (gdk_x11_display_error_trap_pop (display) ||
1658 (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) ||
1661 g_warning ("Error reading initiator info\n");
1665 initiator_info = (MotifDragInitiatorInfo *)data;
1667 motif_read_target_table (display);
1669 initiator_info->targets_index =
1670 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1671 initiator_info->selection_atom =
1672 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1674 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1676 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1677 XFree (initiator_info);
1681 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1686 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1687 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1688 tmp_list = tmp_list->prev;
1691 #ifdef G_ENABLE_DEBUG
1692 if (_gdk_debug_flags & GDK_DEBUG_DND)
1693 print_target_list (*targets);
1694 #endif /* G_ENABLE_DEBUG */
1696 *selection = initiator_info->selection_atom;
1698 XFree (initiator_info);
1703 static GdkDragContext *
1704 motif_drag_context_new (GdkWindow *dest_window,
1706 guint32 source_window,
1709 GdkX11DragContext *context_x11;
1710 GdkDragContext *context;
1711 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
1712 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1714 /* FIXME, current_dest_drag really shouldn't be NULL'd
1715 * if we error below.
1717 if (display_x11->current_dest_drag != NULL)
1719 if (timestamp >= display_x11->current_dest_drag->start_time)
1721 g_object_unref (display_x11->current_dest_drag);
1722 display_x11->current_dest_drag = NULL;
1728 context_x11 = g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
1729 context = GDK_DRAG_CONTEXT (context_x11);
1731 context->protocol = GDK_DRAG_PROTO_MOTIF;
1732 context->is_source = FALSE;
1734 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
1735 if (!context->source_window)
1737 g_object_unref (context_x11);
1741 context->dest_window = dest_window;
1742 g_object_ref (dest_window);
1743 context->start_time = timestamp;
1745 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1749 &context_x11->motif_selection))
1751 g_object_unref (context_x11);
1759 * The MOTIF drag protocol has no real provisions for distinguishing
1760 * multiple simultaneous drops. If the sources grab the pointer
1761 * when doing drags, that shouldn't happen, in any case. If it
1762 * does, we can't do much except hope for the best.
1765 static GdkFilterReturn
1766 motif_top_level_enter (GdkEvent *event,
1769 guint32 source_window,
1772 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1773 GdkDragContext *new_context;
1775 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1776 flags, timestamp, source_window, atom));
1778 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1780 return GDK_FILTER_REMOVE;
1782 event->dnd.type = GDK_DRAG_ENTER;
1783 event->dnd.context = new_context;
1784 g_object_ref (new_context);
1786 display_x11->current_dest_drag = new_context;
1788 return GDK_FILTER_TRANSLATE;
1791 static GdkFilterReturn
1792 motif_top_level_leave (GdkEvent *event,
1796 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1798 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1801 if ((display_x11->current_dest_drag != NULL) &&
1802 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1803 (timestamp >= display_x11->current_dest_drag->start_time))
1805 event->dnd.type = GDK_DRAG_LEAVE;
1806 /* Pass ownership of context to the event */
1807 event->dnd.context = display_x11->current_dest_drag;
1809 display_x11->current_dest_drag = NULL;
1811 return GDK_FILTER_TRANSLATE;
1814 return GDK_FILTER_REMOVE;
1817 static GdkFilterReturn
1818 motif_motion (GdkEvent *event,
1824 GdkX11DragContext *context_x11;
1825 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1827 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1828 flags, timestamp, x_root, y_root));
1830 if ((display_x11->current_dest_drag != NULL) &&
1831 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1832 (timestamp >= display_x11->current_dest_drag->start_time))
1834 context_x11 = GDK_X11_DRAG_CONTEXT (display_x11->current_dest_drag);
1836 event->dnd.type = GDK_DRAG_MOTION;
1837 event->dnd.context = display_x11->current_dest_drag;
1838 g_object_ref (display_x11->current_dest_drag);
1840 event->dnd.time = timestamp;
1842 motif_dnd_translate_flags (context_x11, flags);
1844 event->dnd.x_root = x_root;
1845 event->dnd.y_root = y_root;
1847 context_x11->last_x = x_root;
1848 context_x11->last_y = y_root;
1850 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1852 return GDK_FILTER_TRANSLATE;
1855 return GDK_FILTER_REMOVE;
1858 static GdkFilterReturn
1859 motif_operation_changed (GdkEvent *event,
1863 GdkX11DragContext *context_x11;
1864 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1865 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1868 if ((display_x11->current_dest_drag != NULL) &&
1869 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1870 (timestamp >= display_x11->current_dest_drag->start_time))
1872 event->dnd.type = GDK_DRAG_MOTION;
1873 event->dnd.send_event = FALSE;
1874 event->dnd.context = display_x11->current_dest_drag;
1875 g_object_ref (display_x11->current_dest_drag);
1877 event->dnd.time = timestamp;
1878 context_x11 = GDK_X11_DRAG_CONTEXT (display_x11->current_dest_drag);
1880 motif_dnd_translate_flags (context_x11, flags);
1882 event->dnd.x_root = context_x11->last_x;
1883 event->dnd.y_root = context_x11->last_y;
1885 context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1887 return GDK_FILTER_TRANSLATE;
1890 return GDK_FILTER_REMOVE;
1893 static GdkFilterReturn
1894 motif_drop_start (GdkEvent *event,
1897 guint32 source_window,
1902 GdkDragContext *new_context;
1903 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1905 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1906 flags, timestamp, x_root, y_root, source_window, atom));
1908 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1910 return GDK_FILTER_REMOVE;
1912 motif_dnd_translate_flags (GDK_X11_DRAG_CONTEXT (new_context), flags);
1914 event->dnd.type = GDK_DROP_START;
1915 event->dnd.context = new_context;
1916 event->dnd.time = timestamp;
1917 event->dnd.x_root = x_root;
1918 event->dnd.y_root = y_root;
1920 gdk_x11_window_set_user_time (event->any.window, timestamp);
1922 g_object_ref (new_context);
1923 display_x11->current_dest_drag = new_context;
1925 return GDK_FILTER_TRANSLATE;
1928 static GdkFilterReturn
1929 motif_drag_status (GdkEvent *event,
1933 GdkDragContext *context;
1934 GdkDisplay *display;
1937 g_message ("Motif status message: flags %x", flags));
1939 display = gdk_window_get_display (event->any.window);
1941 return GDK_FILTER_REMOVE;
1943 context = gdk_drag_context_find (display, TRUE, GDK_WINDOW_XID (event->any.window), None);
1947 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
1948 if ((context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1949 (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1950 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
1952 event->dnd.type = GDK_DRAG_STATUS;
1953 event->dnd.send_event = FALSE;
1954 event->dnd.context = context;
1955 g_object_ref (context);
1957 event->dnd.time = timestamp;
1959 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1961 switch (flags & 0x000f)
1964 context->action = 0;
1967 context->action = GDK_ACTION_MOVE;
1970 context->action = GDK_ACTION_COPY;
1973 context->action = GDK_ACTION_LINK;
1978 context->action = 0;
1980 return GDK_FILTER_TRANSLATE;
1982 return GDK_FILTER_REMOVE;
1985 static GdkFilterReturn
1986 motif_dnd_filter (GdkXEvent *xev,
1990 XEvent *xevent = (XEvent *)xev;
1995 guint32 source_window;
1997 gint16 x_root, y_root;
2000 if (!event->any.window ||
2001 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2002 return GDK_FILTER_CONTINUE; /* Not for us */
2004 /* First read some fields common to all Motif DND messages */
2005 reason = MOTIF_UNPACK_BYTE (xevent, 0);
2006 flags = MOTIF_UNPACK_SHORT (xevent, 1);
2007 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
2009 is_reply = ((reason & 0x80) != 0);
2011 switch (reason & 0x7f)
2013 case XmTOP_LEVEL_ENTER:
2014 source_window = MOTIF_UNPACK_LONG (xevent, 2);
2015 atom = MOTIF_UNPACK_LONG (xevent, 3);
2016 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
2017 case XmTOP_LEVEL_LEAVE:
2018 return motif_top_level_leave (event, flags, timestamp);
2021 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2022 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2025 return motif_motion (event, flags, timestamp, x_root, y_root);
2027 return motif_drag_status (event, flags, timestamp);
2029 case XmDROP_SITE_ENTER:
2030 return motif_drag_status (event, flags, timestamp);
2032 case XmDROP_SITE_LEAVE:
2033 return motif_drag_status (event,
2034 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
2037 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2038 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2039 atom = MOTIF_UNPACK_LONG (xevent, 3);
2040 source_window = MOTIF_UNPACK_LONG (xevent, 4);
2043 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
2046 case XmOPERATION_CHANGED:
2048 return motif_operation_changed (event, flags, timestamp);
2050 return motif_drag_status (event, flags, timestamp);
2053 /* To the best of my knowledge, these next two messages are
2054 * not part of the protocol, though they are defined in
2058 case XmDRAG_DROP_FINISH:
2062 return GDK_FILTER_REMOVE;
2065 /*************************************************************
2066 ***************************** XDND **************************
2067 *************************************************************/
2069 /* Utility functions */
2074 GdkDragAction action;
2075 } xdnd_actions_table[] = {
2076 { "XdndActionCopy", None, GDK_ACTION_COPY },
2077 { "XdndActionMove", None, GDK_ACTION_MOVE },
2078 { "XdndActionLink", None, GDK_ACTION_LINK },
2079 { "XdndActionAsk", None, GDK_ACTION_ASK },
2080 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2083 static const gint xdnd_n_actions = G_N_ELEMENTS (xdnd_actions_table);
2084 static gboolean xdnd_actions_initialized = FALSE;
2087 xdnd_initialize_actions (void)
2091 xdnd_actions_initialized = TRUE;
2092 for (i = 0; i < xdnd_n_actions; i++)
2093 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2096 static GdkDragAction
2097 xdnd_action_from_atom (GdkDisplay *display,
2106 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2108 if (!xdnd_actions_initialized)
2109 xdnd_initialize_actions();
2111 for (i = 0; i < xdnd_n_actions; i++)
2112 if (atom == xdnd_actions_table[i].atom)
2113 return xdnd_actions_table[i].action;
2119 xdnd_action_to_atom (GdkDisplay *display,
2120 GdkDragAction action)
2124 if (!xdnd_actions_initialized)
2125 xdnd_initialize_actions();
2127 for (i = 0; i < xdnd_n_actions; i++)
2128 if (action == xdnd_actions_table[i].action)
2129 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2136 static GdkFilterReturn
2137 xdnd_status_filter (GdkXEvent *xev,
2141 GdkDisplay *display;
2142 XEvent *xevent = (XEvent *)xev;
2143 guint32 dest_window = xevent->xclient.data.l[0];
2144 guint32 flags = xevent->xclient.data.l[1];
2145 Atom action = xevent->xclient.data.l[4];
2146 GdkDragContext *context;
2148 if (!event->any.window ||
2149 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2150 return GDK_FILTER_CONTINUE; /* Not for us */
2153 g_message ("XdndStatus: dest_window: %#x action: %ld",
2154 dest_window, action));
2156 display = gdk_window_get_display (event->any.window);
2157 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2161 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
2162 if (context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2163 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2165 event->dnd.send_event = FALSE;
2166 event->dnd.type = GDK_DRAG_STATUS;
2167 event->dnd.context = context;
2168 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2169 g_object_ref (context);
2171 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2172 if (!(action != 0) != !(flags & 1))
2175 g_warning ("Received status event with flags not corresponding to action!\n"));
2179 context->action = xdnd_action_from_atom (display, action);
2181 return GDK_FILTER_TRANSLATE;
2184 return GDK_FILTER_REMOVE;
2187 static GdkFilterReturn
2188 xdnd_finished_filter (GdkXEvent *xev,
2192 GdkDisplay *display;
2193 XEvent *xevent = (XEvent *)xev;
2194 guint32 dest_window = xevent->xclient.data.l[0];
2195 GdkDragContext *context;
2196 GdkX11DragContext *context_x11;
2198 if (!event->any.window ||
2199 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2200 return GDK_FILTER_CONTINUE; /* Not for us */
2203 g_message ("XdndFinished: dest_window: %#x", dest_window));
2205 display = gdk_window_get_display (event->any.window);
2206 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2210 context_x11 = GDK_X11_DRAG_CONTEXT (context);
2211 if (context_x11->version == 5)
2212 context_x11->drop_failed = xevent->xclient.data.l[1] == 0;
2214 event->dnd.type = GDK_DROP_FINISHED;
2215 event->dnd.context = context;
2216 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2217 g_object_ref (context);
2219 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2221 return GDK_FILTER_TRANSLATE;
2224 return GDK_FILTER_REMOVE;
2228 xdnd_set_targets (GdkX11DragContext *context_x11)
2230 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2232 GList *tmp_list = context->targets;
2234 gint n_atoms = g_list_length (context->targets);
2235 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2237 atomlist = g_new (Atom, n_atoms);
2241 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2242 tmp_list = tmp_list->next;
2246 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2247 GDK_WINDOW_XID (context->source_window),
2248 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2249 XA_ATOM, 32, PropModeReplace,
2250 (guchar *)atomlist, n_atoms);
2254 context_x11->xdnd_targets_set = 1;
2258 xdnd_set_actions (GdkX11DragContext *context_x11)
2260 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2265 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2267 if (!xdnd_actions_initialized)
2268 xdnd_initialize_actions();
2270 actions = context->actions;
2272 for (i = 0; i < xdnd_n_actions; i++)
2274 if (actions & xdnd_actions_table[i].action)
2276 actions &= ~xdnd_actions_table[i].action;
2281 atomlist = g_new (Atom, n_atoms);
2283 actions = context->actions;
2285 for (i = 0; i < xdnd_n_actions; i++)
2287 if (actions & xdnd_actions_table[i].action)
2289 actions &= ~xdnd_actions_table[i].action;
2290 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2295 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2296 GDK_WINDOW_XID (context->source_window),
2297 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2298 XA_ATOM, 32, PropModeReplace,
2299 (guchar *)atomlist, n_atoms);
2303 context_x11->xdnd_actions_set = TRUE;
2304 context_x11->xdnd_actions = context->actions;
2308 send_client_message_async_cb (Window window,
2312 GdkDragContext *context = data;
2314 g_message ("Got async callback for #%lx, success = %d",
2317 /* On failure, we immediately continue with the protocol
2318 * so we don't end up blocking for a timeout
2321 context->dest_window &&
2322 window == GDK_WINDOW_XID (context->dest_window))
2324 GdkEvent *temp_event;
2325 GdkX11DragContext *context_x11 = data;
2327 g_object_unref (context->dest_window);
2328 context->dest_window = NULL;
2329 context->action = 0;
2331 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2333 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2334 temp_event->dnd.window = g_object_ref (context->source_window);
2335 temp_event->dnd.send_event = TRUE;
2336 temp_event->dnd.context = g_object_ref (context);
2337 temp_event->dnd.time = GDK_CURRENT_TIME;
2338 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2340 gdk_event_put (temp_event);
2342 gdk_event_free (temp_event);
2345 g_object_unref (context);
2350 gdk_drag_context_get_display (GdkDragContext *context)
2352 if (context->source_window)
2353 return GDK_WINDOW_DISPLAY (context->source_window);
2354 else if (context->dest_window)
2355 return GDK_WINDOW_DISPLAY (context->dest_window);
2357 g_assert_not_reached ();
2362 send_client_message_async (GdkDragContext *context,
2366 XClientMessageEvent *event_send)
2368 GdkDisplay *display = gdk_drag_context_get_display (context);
2370 g_object_ref (context);
2372 _gdk_x11_send_client_message_async (display, window,
2373 propagate, event_mask, event_send,
2374 send_client_message_async_cb, context);
2378 xdnd_send_xevent (GdkX11DragContext *context_x11,
2383 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2384 GdkDisplay *display = gdk_drag_context_get_display (context);
2388 g_assert (event_send->xany.type == ClientMessage);
2390 /* We short-circuit messages to ourselves */
2391 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2395 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2397 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2398 event_send->xclient.message_type)
2400 GdkEvent *temp_event;
2402 temp_event = gdk_event_new (GDK_NOTHING);
2403 temp_event->any.window = g_object_ref (window);
2405 if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
2407 gdk_event_put (temp_event);
2408 gdk_event_free (temp_event);
2416 xwindow = GDK_WINDOW_XID (window);
2418 if (_gdk_x11_display_is_root_window (display, xwindow))
2419 event_mask = ButtonPressMask;
2423 send_client_message_async (context, xwindow, propagate, event_mask,
2424 &event_send->xclient);
2430 xdnd_send_enter (GdkX11DragContext *context_x11)
2432 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2433 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->dest_window);
2436 xev.xclient.type = ClientMessage;
2437 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2438 xev.xclient.format = 32;
2439 xev.xclient.window = context_x11->drop_xid
2440 ? context_x11->drop_xid
2441 : GDK_WINDOW_XID (context->dest_window);
2442 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2443 xev.xclient.data.l[1] = (context_x11->version << 24); /* version */
2444 xev.xclient.data.l[2] = 0;
2445 xev.xclient.data.l[3] = 0;
2446 xev.xclient.data.l[4] = 0;
2449 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2450 GDK_WINDOW_XID (context->source_window), context_x11->version));
2451 if (g_list_length (context->targets) > 3)
2453 if (!context_x11->xdnd_targets_set)
2454 xdnd_set_targets (context_x11);
2455 xev.xclient.data.l[1] |= 1;
2459 GList *tmp_list = context->targets;
2464 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2465 GDK_POINTER_TO_ATOM (tmp_list->data));
2466 tmp_list = tmp_list->next;
2471 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2474 g_message ("Send event to %lx failed",
2475 GDK_WINDOW_XID (context->dest_window)));
2476 g_object_unref (context->dest_window);
2477 context->dest_window = NULL;
2482 xdnd_send_leave (GdkX11DragContext *context_x11)
2484 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2485 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2488 xev.xclient.type = ClientMessage;
2489 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2490 xev.xclient.format = 32;
2491 xev.xclient.window = context_x11->drop_xid
2492 ? context_x11->drop_xid
2493 : GDK_WINDOW_XID (context->dest_window);
2494 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2495 xev.xclient.data.l[1] = 0;
2496 xev.xclient.data.l[2] = 0;
2497 xev.xclient.data.l[3] = 0;
2498 xev.xclient.data.l[4] = 0;
2500 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2503 g_message ("Send event to %lx failed",
2504 GDK_WINDOW_XID (context->dest_window)));
2505 g_object_unref (context->dest_window);
2506 context->dest_window = NULL;
2511 xdnd_send_drop (GdkX11DragContext *context_x11,
2514 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2515 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2518 xev.xclient.type = ClientMessage;
2519 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2520 xev.xclient.format = 32;
2521 xev.xclient.window = context_x11->drop_xid
2522 ? context_x11->drop_xid
2523 : GDK_WINDOW_XID (context->dest_window);
2524 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2525 xev.xclient.data.l[1] = 0;
2526 xev.xclient.data.l[2] = time;
2527 xev.xclient.data.l[3] = 0;
2528 xev.xclient.data.l[4] = 0;
2530 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2533 g_message ("Send event to %lx failed",
2534 GDK_WINDOW_XID (context->dest_window)));
2535 g_object_unref (context->dest_window);
2536 context->dest_window = NULL;
2541 xdnd_send_motion (GdkX11DragContext *context_x11,
2544 GdkDragAction action,
2547 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2548 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2551 xev.xclient.type = ClientMessage;
2552 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2553 xev.xclient.format = 32;
2554 xev.xclient.window = context_x11->drop_xid
2555 ? context_x11->drop_xid
2556 : GDK_WINDOW_XID (context->dest_window);
2557 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2558 xev.xclient.data.l[1] = 0;
2559 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2560 xev.xclient.data.l[3] = time;
2561 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2563 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2566 g_message ("Send event to %lx failed",
2567 GDK_WINDOW_XID (context->dest_window)));
2568 g_object_unref (context->dest_window);
2569 context->dest_window = NULL;
2571 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2575 xdnd_check_dest (GdkDisplay *display,
2577 guint *xdnd_version)
2579 gboolean retval = FALSE;
2582 unsigned long nitems, after;
2587 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2588 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2592 gdk_x11_display_error_trap_push (display);
2593 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2595 1, False, AnyPropertyType,
2596 &type, &format, &nitems, &after,
2601 proxy_data = (Window *)data;
2603 if ((format == 32) && (nitems == 1))
2605 proxy = *proxy_data;
2609 g_warning ("Invalid XdndProxy "
2610 "property on window %ld\n", win));
2615 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2617 1, False, AnyPropertyType,
2618 &type, &format, &nitems, &after,
2619 &data) == Success) &&
2622 version = (Atom *)data;
2624 if ((format == 32) && (nitems == 1))
2629 *xdnd_version = *version;
2633 g_warning ("Invalid XdndAware "
2634 "property on window %ld\n", win));
2640 gdk_x11_display_error_trap_pop_ignored (display);
2642 return retval ? (proxy ? proxy : win) : None;
2648 xdnd_read_actions (GdkX11DragContext *context_x11)
2650 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2651 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2654 gulong nitems, after;
2659 context_x11->xdnd_have_actions = FALSE;
2661 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2663 /* Get the XdndActionList, if set */
2665 gdk_x11_display_error_trap_push (display);
2666 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2667 GDK_WINDOW_XID (context->source_window),
2668 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2670 False, XA_ATOM, &type, &format, &nitems,
2671 &after, &data) == Success &&
2674 atoms = (Atom *)data;
2676 context->actions = 0;
2678 for (i = 0; i < nitems; i++)
2679 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2681 context_x11->xdnd_have_actions = TRUE;
2683 #ifdef G_ENABLE_DEBUG
2684 if (_gdk_debug_flags & GDK_DEBUG_DND)
2686 GString *action_str = g_string_new (NULL);
2687 if (context->actions & GDK_ACTION_MOVE)
2688 g_string_append(action_str, "MOVE ");
2689 if (context->actions & GDK_ACTION_COPY)
2690 g_string_append(action_str, "COPY ");
2691 if (context->actions & GDK_ACTION_LINK)
2692 g_string_append(action_str, "LINK ");
2693 if (context->actions & GDK_ACTION_ASK)
2694 g_string_append(action_str, "ASK ");
2696 g_message("Xdnd actions = %s", action_str->str);
2697 g_string_free (action_str, TRUE);
2699 #endif /* G_ENABLE_DEBUG */
2706 gdk_x11_display_error_trap_pop_ignored (display);
2712 GdkDragContext *source_context;
2714 source_context = gdk_drag_context_find (display, TRUE,
2715 GDK_WINDOW_XID (context->source_window),
2716 GDK_WINDOW_XID (context->dest_window));
2720 context->actions = source_context->actions;
2721 context_x11->xdnd_have_actions = TRUE;
2726 /* We have to make sure that the XdndActionList we keep internally
2727 * is up to date with the XdndActionList on the source window
2728 * because we get no notification, because Xdnd wasn't meant
2729 * to continually send actions. So we select on PropertyChangeMask
2730 * and add this filter.
2732 static GdkFilterReturn
2733 xdnd_source_window_filter (GdkXEvent *xev,
2737 XEvent *xevent = (XEvent *)xev;
2738 GdkX11DragContext *context_x11 = cb_data;
2739 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2741 if ((xevent->xany.type == PropertyNotify) &&
2742 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2744 xdnd_read_actions (context_x11);
2746 return GDK_FILTER_REMOVE;
2749 return GDK_FILTER_CONTINUE;
2753 xdnd_manage_source_filter (GdkDragContext *context,
2755 gboolean add_filter)
2757 if (!GDK_WINDOW_DESTROYED (window) &&
2758 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2760 gdk_x11_display_error_trap_push (GDK_WINDOW_DISPLAY (window));
2764 gdk_window_set_events (window,
2765 gdk_window_get_events (window) |
2766 GDK_PROPERTY_CHANGE_MASK);
2767 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2771 gdk_window_remove_filter (window,
2772 xdnd_source_window_filter,
2774 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2775 * but we might want it for other reasons. (Like
2776 * INCR selection transactions).
2780 gdk_x11_display_error_trap_pop_ignored (GDK_WINDOW_DISPLAY (window));
2785 base_precache_atoms (GdkDisplay *display)
2787 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2789 if (!display_x11->base_dnd_atoms_precached)
2791 static const char *const precache_atoms[] = {
2792 "ENLIGHTENMENT_DESKTOP",
2796 "_MOTIF_DRAG_RECEIVER_INFO"
2799 _gdk_x11_precache_atoms (display,
2800 precache_atoms, G_N_ELEMENTS (precache_atoms));
2802 display_x11->base_dnd_atoms_precached = TRUE;
2807 xdnd_precache_atoms (GdkDisplay *display)
2809 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2811 if (!display_x11->xdnd_atoms_precached)
2813 static const gchar *const precache_atoms[] = {
2819 "XdndActionPrivate",
2830 _gdk_x11_precache_atoms (display,
2831 precache_atoms, G_N_ELEMENTS (precache_atoms));
2833 display_x11->xdnd_atoms_precached = TRUE;
2837 static GdkFilterReturn
2838 xdnd_enter_filter (GdkXEvent *xev,
2842 GdkDeviceManager *device_manager;
2843 GdkDisplay *display;
2844 GdkX11Display *display_x11;
2845 XEvent *xevent = (XEvent *)xev;
2846 GdkDragContext *context;
2847 GdkX11DragContext *context_x11;
2851 gulong nitems, after;
2854 guint32 source_window;
2858 if (!event->any.window ||
2859 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2860 return GDK_FILTER_CONTINUE; /* Not for us */
2862 source_window = xevent->xclient.data.l[0];
2863 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2864 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2866 display = GDK_WINDOW_DISPLAY (event->any.window);
2867 display_x11 = GDK_X11_DISPLAY (display);
2869 xdnd_precache_atoms (display);
2872 g_message ("XdndEnter: source_window: %#x, version: %#x",
2873 source_window, version));
2877 /* Old source ignore */
2878 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2879 return GDK_FILTER_REMOVE;
2882 if (display_x11->current_dest_drag != NULL)
2884 g_object_unref (display_x11->current_dest_drag);
2885 display_x11->current_dest_drag = NULL;
2888 context_x11 = (GdkX11DragContext *)g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
2889 context = (GdkDragContext *)context_x11;
2891 context->protocol = GDK_DRAG_PROTO_XDND;
2892 context_x11->version = version;
2894 /* FIXME: Should extend DnD protocol to have device info */
2895 device_manager = gdk_display_get_device_manager (display);
2896 gdk_drag_context_set_device (context, gdk_device_manager_get_client_pointer (device_manager));
2898 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
2899 if (!context->source_window)
2901 g_object_unref (context);
2902 return GDK_FILTER_REMOVE;
2904 context->dest_window = event->any.window;
2905 g_object_ref (context->dest_window);
2907 context->targets = NULL;
2910 gdk_x11_display_error_trap_push (display);
2911 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2913 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2915 False, XA_ATOM, &type, &format, &nitems,
2918 if (gdk_x11_display_error_trap_pop (display) || (format != 32) || (type != XA_ATOM))
2920 g_object_unref (context);
2925 return GDK_FILTER_REMOVE;
2928 atoms = (Atom *)data;
2930 for (i = 0; i < nitems; i++)
2932 g_list_append (context->targets,
2933 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2940 for (i = 0; i < 3; i++)
2941 if (xevent->xclient.data.l[2 + i])
2943 g_list_append (context->targets,
2944 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2945 xevent->xclient.data.l[2 + i])));
2948 #ifdef G_ENABLE_DEBUG
2949 if (_gdk_debug_flags & GDK_DEBUG_DND)
2950 print_target_list (context->targets);
2951 #endif /* G_ENABLE_DEBUG */
2953 xdnd_manage_source_filter (context, context->source_window, TRUE);
2954 xdnd_read_actions (context_x11);
2956 event->dnd.type = GDK_DRAG_ENTER;
2957 event->dnd.context = context;
2958 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2959 g_object_ref (context);
2961 display_x11->current_dest_drag = context;
2963 return GDK_FILTER_TRANSLATE;
2966 static GdkFilterReturn
2967 xdnd_leave_filter (GdkXEvent *xev,
2971 XEvent *xevent = (XEvent *)xev;
2972 guint32 source_window = xevent->xclient.data.l[0];
2973 GdkDisplay *display;
2974 GdkX11Display *display_x11;
2976 if (!event->any.window ||
2977 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2978 return GDK_FILTER_CONTINUE; /* Not for us */
2981 g_message ("XdndLeave: source_window: %#x",
2984 display = GDK_WINDOW_DISPLAY (event->any.window);
2985 display_x11 = GDK_X11_DISPLAY (display);
2987 xdnd_precache_atoms (display);
2989 if ((display_x11->current_dest_drag != NULL) &&
2990 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2991 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
2993 event->dnd.type = GDK_DRAG_LEAVE;
2994 /* Pass ownership of context to the event */
2995 event->dnd.context = display_x11->current_dest_drag;
2996 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
2998 display_x11->current_dest_drag = NULL;
3000 return GDK_FILTER_TRANSLATE;
3003 return GDK_FILTER_REMOVE;
3006 static GdkFilterReturn
3007 xdnd_position_filter (GdkXEvent *xev,
3011 XEvent *xevent = (XEvent *)xev;
3012 guint32 source_window = xevent->xclient.data.l[0];
3013 gint16 x_root = xevent->xclient.data.l[2] >> 16;
3014 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
3015 guint32 time = xevent->xclient.data.l[3];
3016 Atom action = xevent->xclient.data.l[4];
3017 GdkDisplay *display;
3018 GdkX11Display *display_x11;
3019 GdkDragContext *context;
3020 GdkX11DragContext *context_x11;
3022 if (!event->any.window ||
3023 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3024 return GDK_FILTER_CONTINUE; /* Not for us */
3027 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
3028 source_window, x_root, y_root, time, action));
3030 display = GDK_WINDOW_DISPLAY (event->any.window);
3031 display_x11 = GDK_X11_DISPLAY (display);
3033 xdnd_precache_atoms (display);
3035 context = display_x11->current_dest_drag;
3037 if ((context != NULL) &&
3038 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3039 (GDK_WINDOW_XID (context->source_window) == source_window))
3041 context_x11 = GDK_X11_DRAG_CONTEXT (context);
3043 event->dnd.type = GDK_DRAG_MOTION;
3044 event->dnd.context = context;
3045 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3046 g_object_ref (context);
3048 event->dnd.time = time;
3050 context->suggested_action = xdnd_action_from_atom (display, action);
3052 if (!context_x11->xdnd_have_actions)
3053 context->actions = context->suggested_action;
3055 event->dnd.x_root = x_root;
3056 event->dnd.y_root = y_root;
3058 context_x11->last_x = x_root;
3059 context_x11->last_y = y_root;
3061 return GDK_FILTER_TRANSLATE;
3064 return GDK_FILTER_REMOVE;
3067 static GdkFilterReturn
3068 xdnd_drop_filter (GdkXEvent *xev,
3072 XEvent *xevent = (XEvent *)xev;
3073 guint32 source_window = xevent->xclient.data.l[0];
3074 guint32 time = xevent->xclient.data.l[2];
3075 GdkDisplay *display;
3076 GdkX11Display *display_x11;
3077 GdkDragContext *context;
3078 GdkX11DragContext *context_x11;
3080 if (!event->any.window ||
3081 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3082 return GDK_FILTER_CONTINUE; /* Not for us */
3085 g_message ("XdndDrop: source_window: %#x time: %d",
3086 source_window, time));
3088 display = GDK_WINDOW_DISPLAY (event->any.window);
3089 display_x11 = GDK_X11_DISPLAY (display);
3091 xdnd_precache_atoms (display);
3093 context = display_x11->current_dest_drag;
3095 if ((context != NULL) &&
3096 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3097 (GDK_WINDOW_XID (context->source_window) == source_window))
3099 context_x11 = GDK_X11_DRAG_CONTEXT (context);
3100 event->dnd.type = GDK_DROP_START;
3102 event->dnd.context = context;
3103 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3104 g_object_ref (context);
3106 event->dnd.time = time;
3107 event->dnd.x_root = context_x11->last_x;
3108 event->dnd.y_root = context_x11->last_y;
3110 gdk_x11_window_set_user_time (event->any.window, time);
3112 return GDK_FILTER_TRANSLATE;
3115 return GDK_FILTER_REMOVE;
3119 _gdk_x11_dnd_filter (GdkXEvent *xev,
3123 XEvent *xevent = (XEvent *) xev;
3124 GdkDisplay *display;
3127 if (!GDK_IS_X11_WINDOW (event->any.window))
3128 return GDK_FILTER_CONTINUE;
3130 if (xevent->type != ClientMessage)
3131 return GDK_FILTER_CONTINUE;
3133 display = GDK_WINDOW_DISPLAY (event->any.window);
3135 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3137 if (xevent->xclient.message_type != gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name))
3140 return xdnd_filters[i].func (xev, event, data);
3143 return GDK_FILTER_CONTINUE;
3149 gdk_drag_do_leave (GdkX11DragContext *context_x11,
3152 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
3154 if (context->dest_window)
3156 switch (context->protocol)
3158 case GDK_DRAG_PROTO_MOTIF:
3159 motif_send_leave (context_x11, time);
3161 case GDK_DRAG_PROTO_XDND:
3162 xdnd_send_leave (context_x11);
3164 case GDK_DRAG_PROTO_ROOTWIN:
3165 case GDK_DRAG_PROTO_NONE:
3170 g_object_unref (context->dest_window);
3171 context->dest_window = NULL;
3176 _gdk_x11_window_drag_begin (GdkWindow *window,
3180 GdkDragContext *context;
3182 context = (GdkDragContext *) g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
3184 context->is_source = TRUE;
3185 context->source_window = window;
3186 g_object_ref (window);
3188 context->targets = g_list_copy (targets);
3189 precache_target_list (context);
3191 context->actions = 0;
3193 gdk_drag_context_set_device (context, device);
3199 _gdk_x11_display_get_drag_protocol (GdkDisplay *display,
3201 GdkDragProtocol *protocol,
3208 base_precache_atoms (display);
3210 /* Check for a local drag */
3211 window = gdk_x11_window_lookup_for_display (display, xid);
3212 if (window && gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3214 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3216 *protocol = GDK_DRAG_PROTO_XDND;
3218 xdnd_precache_atoms (display);
3219 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", (guint) xid));
3222 else if (_gdk_x11_display_is_root_window (display, xid))
3224 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3225 GDK_NOTE (DND, g_message ("Entering root window\n"));
3229 else if ((retval = xdnd_check_dest (display, xid, version)))
3231 *protocol = GDK_DRAG_PROTO_XDND;
3232 xdnd_precache_atoms (display);
3233 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", (guint) xid));
3236 else if ((retval = motif_check_dest (display, xid)))
3238 *protocol = GDK_DRAG_PROTO_MOTIF;
3239 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", (guint) xid));
3244 /* Check if this is a root window */
3245 gboolean rootwin = FALSE;
3247 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3252 GDK_NOTE (DND, g_message ("Entering root window\n"));
3253 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3258 *protocol = GDK_DRAG_PROTO_NONE;
3260 return 0; /* a.k.a. None */
3263 static GdkWindowCache *
3264 drag_context_find_window_cache (GdkX11DragContext *context_x11,
3268 GdkWindowCache *cache;
3270 for (list = context_x11->window_caches; list; list = list->next)
3273 if (cache->screen == screen)
3277 cache = gdk_window_cache_get (screen);
3278 context_x11->window_caches = g_slist_prepend (context_x11->window_caches, cache);
3284 gdk_x11_drag_context_find_window (GdkDragContext *context,
3285 GdkWindow *drag_window,
3289 GdkDragProtocol *protocol)
3291 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3292 GdkWindowCache *window_cache;
3293 GdkDisplay *display;
3295 GdkWindow *dest_window;
3297 display = GDK_WINDOW_DISPLAY (context->source_window);
3299 window_cache = drag_context_find_window_cache (context_x11, screen);
3301 dest = get_client_window_at_coords (window_cache,
3302 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3303 GDK_WINDOW_XID (drag_window) : None,
3306 if (context_x11->dest_xid != dest)
3309 context_x11->dest_xid = dest;
3311 /* Check if new destination accepts drags, and which protocol */
3313 /* There is some ugliness here. We actually need to pass
3314 * _three_ pieces of information to drag_motion - dest_window,
3315 * protocol, and the XID of the unproxied window. The first
3316 * two are passed explicitely, the third implicitly through
3317 * protocol->dest_xid.
3319 recipient = _gdk_x11_display_get_drag_protocol (display,
3322 &context_x11->version);
3324 if (recipient != None)
3325 dest_window = gdk_x11_window_foreign_new_for_display (display, recipient);
3331 dest_window = context->dest_window;
3333 g_object_ref (dest_window);
3334 *protocol = context->protocol;
3341 gdk_x11_drag_context_drag_motion (GdkDragContext *context,
3342 GdkWindow *dest_window,
3343 GdkDragProtocol protocol,
3346 GdkDragAction suggested_action,
3347 GdkDragAction possible_actions,
3350 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3352 context_x11->old_actions = context->actions;
3353 context->actions = possible_actions;
3355 if (context_x11->old_actions != possible_actions)
3356 context_x11->xdnd_actions_set = FALSE;
3358 if (protocol == GDK_DRAG_PROTO_XDND && context_x11->version == 0)
3360 /* This ugly hack is necessary since GTK+ doesn't know about
3361 * the XDND protocol version, and in particular doesn't know
3362 * that gdk_drag_find_window_for_screen() has the side-effect
3363 * of setting context_x11->version, and therefore sometimes call
3364 * gdk_drag_motion() without a prior call to
3365 * gdk_drag_find_window_for_screen(). This happens, e.g.
3366 * when GTK+ is proxying DND events to embedded windows.
3370 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3372 xdnd_check_dest (display,
3373 GDK_WINDOW_XID (dest_window),
3374 &context_x11->version);
3378 /* When we have a Xdnd target, make sure our XdndActionList
3379 * matches the current actions;
3381 if (protocol == GDK_DRAG_PROTO_XDND && !context_x11->xdnd_actions_set)
3385 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3386 xdnd_set_actions (context_x11);
3387 else if (context->dest_window == dest_window)
3389 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3390 GdkDragContext *dest_context;
3392 dest_context = gdk_drag_context_find (display, FALSE,
3393 GDK_WINDOW_XID (context->source_window),
3394 GDK_WINDOW_XID (dest_window));
3398 dest_context->actions = context->actions;
3399 GDK_X11_DRAG_CONTEXT (dest_context)->xdnd_have_actions = TRUE;
3405 if (context->dest_window != dest_window)
3407 GdkEvent *temp_event;
3409 /* Send a leave to the last destination */
3410 gdk_drag_do_leave (context_x11, time);
3411 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
3413 /* Check if new destination accepts drags, and which protocol */
3417 context->dest_window = dest_window;
3418 context_x11->drop_xid = context_x11->dest_xid;
3419 g_object_ref (context->dest_window);
3420 context->protocol = protocol;
3424 case GDK_DRAG_PROTO_MOTIF:
3425 motif_send_enter (context_x11, time);
3428 case GDK_DRAG_PROTO_XDND:
3429 xdnd_send_enter (context_x11);
3432 case GDK_DRAG_PROTO_ROOTWIN:
3433 case GDK_DRAG_PROTO_NONE:
3437 context_x11->old_action = suggested_action;
3438 context->suggested_action = suggested_action;
3439 context_x11->old_actions = possible_actions;
3443 context->dest_window = NULL;
3444 context_x11->drop_xid = None;
3445 context->action = 0;
3448 /* Push a status event, to let the client know that
3451 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3452 temp_event->dnd.window = g_object_ref (context->source_window);
3453 /* We use this to signal a synthetic status. Perhaps
3454 * we should use an extra field...
3456 temp_event->dnd.send_event = TRUE;
3458 temp_event->dnd.context = g_object_ref (context);
3459 temp_event->dnd.time = time;
3460 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3462 gdk_event_put (temp_event);
3463 gdk_event_free (temp_event);
3467 context_x11->old_action = context->suggested_action;
3468 context->suggested_action = suggested_action;
3471 /* Send a drag-motion event */
3473 context_x11->last_x = x_root;
3474 context_x11->last_y = y_root;
3476 if (context->dest_window)
3478 if (context_x11->drag_status == GDK_DRAG_STATUS_DRAG)
3480 switch (context->protocol)
3482 case GDK_DRAG_PROTO_MOTIF:
3483 motif_send_motion (context_x11, x_root, y_root, suggested_action, time);
3486 case GDK_DRAG_PROTO_XDND:
3487 xdnd_send_motion (context_x11, x_root, y_root, suggested_action, time);
3490 case GDK_DRAG_PROTO_ROOTWIN:
3492 GdkEvent *temp_event;
3493 /* GTK+ traditionally has used application/x-rootwin-drop,
3494 * but the XDND spec specifies x-rootwindow-drop.
3496 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3497 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3499 if (g_list_find (context->targets,
3500 GDK_ATOM_TO_POINTER (target1)) ||
3501 g_list_find (context->targets,
3502 GDK_ATOM_TO_POINTER (target2)))
3503 context->action = context->suggested_action;
3505 context->action = 0;
3507 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3508 temp_event->dnd.window = g_object_ref (context->source_window);
3509 temp_event->dnd.send_event = FALSE;
3510 temp_event->dnd.context = g_object_ref (context);
3511 temp_event->dnd.time = time;
3512 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3514 gdk_event_put (temp_event);
3515 gdk_event_free (temp_event);
3518 case GDK_DRAG_PROTO_NONE:
3519 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3533 gdk_x11_drag_context_drag_abort (GdkDragContext *context,
3536 gdk_drag_do_leave (GDK_X11_DRAG_CONTEXT (context), time);
3540 gdk_x11_drag_context_drag_drop (GdkDragContext *context,
3543 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3545 if (context->dest_window)
3547 switch (context->protocol)
3549 case GDK_DRAG_PROTO_MOTIF:
3550 motif_send_leave (context_x11, time);
3551 motif_send_drop (context_x11, time);
3554 case GDK_DRAG_PROTO_XDND:
3555 xdnd_send_drop (context_x11, time);
3558 case GDK_DRAG_PROTO_ROOTWIN:
3559 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3561 case GDK_DRAG_PROTO_NONE:
3562 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3570 /* Destination side */
3573 gdk_x11_drag_context_drag_status (GdkDragContext *context,
3574 GdkDragAction action,
3577 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3579 GdkDisplay *display;
3581 display = GDK_WINDOW_DISPLAY (context->source_window);
3583 context->action = action;
3585 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3587 gboolean need_coords = FALSE;
3589 xev.xclient.type = ClientMessage;
3590 xev.xclient.message_type =
3591 gdk_x11_get_xatom_by_name_for_display (display,
3592 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3593 xev.xclient.format = 8;
3594 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3596 if (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3598 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3602 if ((action != 0) != (context_x11->old_action != 0))
3606 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3611 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3616 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3621 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3625 case GDK_ACTION_MOVE:
3626 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3628 case GDK_ACTION_COPY:
3629 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3631 case GDK_ACTION_LINK:
3632 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3635 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3640 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3642 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3644 MOTIF_XCLIENT_LONG (&xev, 1) = time_;
3648 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
3649 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
3652 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3654 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3655 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3657 if (!_gdk_x11_display_send_xevent (display,
3658 GDK_WINDOW_XID (context->source_window),
3661 g_message ("Send event to %lx failed",
3662 GDK_WINDOW_XID (context->source_window)));
3664 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3666 xev.xclient.type = ClientMessage;
3667 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3668 xev.xclient.format = 32;
3669 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3671 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3672 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3673 xev.xclient.data.l[2] = 0;
3674 xev.xclient.data.l[3] = 0;
3675 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3676 if (!xdnd_send_xevent (context_x11, context->source_window, FALSE, &xev))
3678 g_message ("Send event to %lx failed",
3679 GDK_WINDOW_XID (context->source_window)));
3682 context_x11->old_action = action;
3686 gdk_x11_drag_context_drop_reply (GdkDragContext *context,
3690 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3692 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3694 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3697 xev.xclient.type = ClientMessage;
3698 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3699 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3700 xev.xclient.format = 8;
3702 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3703 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3705 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3706 (XmDROP_SITE_VALID << 4) |
3707 (XmDROP_NOOP << 8) |
3710 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3711 (XmNO_DROP_SITE << 4) |
3712 (XmDROP_NOOP << 8) |
3713 (XmDROP_CANCEL << 12);
3714 MOTIF_XCLIENT_SHORT (&xev, 2) = context_x11->last_x;
3715 MOTIF_XCLIENT_SHORT (&xev, 3) = context_x11->last_y;
3716 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3717 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3718 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3720 _gdk_x11_display_send_xevent (display,
3721 GDK_WINDOW_XID (context->source_window),
3727 gdk_x11_drag_context_drop_finish (GdkDragContext *context,
3731 if (context->protocol == GDK_DRAG_PROTO_XDND)
3733 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3736 xev.xclient.type = ClientMessage;
3737 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3738 xev.xclient.format = 32;
3739 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3741 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3744 xev.xclient.data.l[1] = 1;
3745 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3750 xev.xclient.data.l[1] = 0;
3751 xev.xclient.data.l[2] = None;
3753 xev.xclient.data.l[3] = 0;
3754 xev.xclient.data.l[4] = 0;
3756 if (!xdnd_send_xevent (GDK_X11_DRAG_CONTEXT (context), context->source_window, FALSE, &xev))
3758 g_message ("Send event to %lx failed",
3759 GDK_WINDOW_XID (context->source_window)));
3764 _gdk_x11_window_register_dnd (GdkWindow *window)
3766 static const gulong xdnd_version = 5;
3767 MotifDragReceiverInfo info;
3768 Atom motif_drag_receiver_info_atom;
3769 GdkDisplay *display = gdk_window_get_display (window);
3771 g_return_if_fail (window != NULL);
3773 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
3776 base_precache_atoms (display);
3778 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3781 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3783 /* Set Motif drag receiver information property */
3784 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3785 "_MOTIF_DRAG_RECEIVER_INFO");
3786 /* initialize to zero to avoid writing uninitialized data to socket */
3787 memset(&info, 0, sizeof(info));
3788 info.byte_order = local_byte_order;
3789 info.protocol_version = 0;
3790 info.protocol_style = XmDRAG_DYNAMIC;
3791 info.proxy_window = None;
3792 info.num_drop_sites = 0;
3793 info.total_size = sizeof(info);
3795 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3796 GDK_WINDOW_XID (window),
3797 motif_drag_receiver_info_atom,
3798 motif_drag_receiver_info_atom,
3805 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3806 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3807 GDK_WINDOW_XID (window),
3808 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3809 XA_ATOM, 32, PropModeReplace,
3810 (guchar *)&xdnd_version, 1);
3814 gdk_x11_drag_context_get_selection (GdkDragContext *context)
3816 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3817 return gdk_x11_xatom_to_atom_for_display (GDK_WINDOW_DISPLAY (context->source_window),
3818 (GDK_X11_DRAG_CONTEXT (context))->motif_selection);
3819 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3820 return gdk_atom_intern_static_string ("XdndSelection");
3826 gdk_x11_drag_context_drop_status (GdkDragContext *context)
3828 return ! GDK_X11_DRAG_CONTEXT (context)->drop_failed;