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/.
29 #include "gdkx11dnd.h"
30 #include "gdkdndprivate.h"
33 #include "gdkinternals.h"
35 #include "gdkproperty.h"
36 #include "gdkprivate-x11.h"
37 #include "gdkscreen-x11.h"
38 #include "gdkdisplay-x11.h"
41 #include <X11/Xutil.h>
42 #include <X11/Xatom.h>
43 #include <X11/extensions/shape.h>
44 #ifdef HAVE_XCOMPOSITE
45 #include <X11/extensions/Xcomposite.h>
52 GDK_DRAG_STATUS_MOTION_WAIT,
53 GDK_DRAG_STATUS_ACTION_WAIT,
59 gint x, y, width, height;
61 gboolean shape_selected;
63 cairo_region_t *shape;
68 GHashTable *child_hash;
75 struct _GdkX11DragContext
77 GdkDragContext context;
82 guint16 last_x; /* Coordinates from last event */
84 GdkDragAction old_action; /* The last action we sent to the source */
85 GdkDragAction old_actions; /* The last actions we sent to the source */
86 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
88 Window dest_xid; /* The last window we looked up */
89 Window drop_xid; /* The (non-proxied) window that is receiving drops */
90 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
91 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
92 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
93 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
94 guint drag_status : 4; /* current status of drag */
96 guint drop_failed : 1; /* Whether the drop was unsuccessful */
97 guint version; /* Xdnd protocol version */
99 GSList *window_caches;
102 struct _GdkX11DragContextClass
104 GdkDragContextClass parent_class;
107 /* Forward declarations */
109 static GdkWindowCache *gdk_window_cache_get (GdkScreen *screen);
110 static GdkWindowCache *gdk_window_cache_ref (GdkWindowCache *cache);
111 static void gdk_window_cache_unref (GdkWindowCache *cache);
113 static void motif_read_target_table (GdkDisplay *display);
115 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
119 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
122 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
125 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
128 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
131 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
134 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
138 static void xdnd_manage_source_filter (GdkDragContext *context,
140 gboolean add_filter);
142 static GList *contexts;
143 static GSList *window_caches;
145 static const struct {
146 const char *atom_name;
149 { "XdndEnter", xdnd_enter_filter },
150 { "XdndLeave", xdnd_leave_filter },
151 { "XdndPosition", xdnd_position_filter },
152 { "XdndStatus", xdnd_status_filter },
153 { "XdndFinished", xdnd_finished_filter },
154 { "XdndDrop", xdnd_drop_filter },
158 G_DEFINE_TYPE (GdkX11DragContext, gdk_x11_drag_context, GDK_TYPE_DRAG_CONTEXT)
161 gdk_x11_drag_context_init (GdkX11DragContext *context)
163 contexts = g_list_prepend (contexts, context);
166 static void gdk_x11_drag_context_finalize (GObject *object);
167 static GdkWindow * gdk_x11_drag_context_find_window (GdkDragContext *context,
168 GdkWindow *drag_window,
172 GdkDragProtocol *protocol);
173 static gboolean gdk_x11_drag_context_drag_motion (GdkDragContext *context,
174 GdkWindow *dest_window,
175 GdkDragProtocol protocol,
178 GdkDragAction suggested_action,
179 GdkDragAction possible_actions,
181 static void gdk_x11_drag_context_drag_status (GdkDragContext *context,
182 GdkDragAction action,
184 static void gdk_x11_drag_context_drag_abort (GdkDragContext *context,
186 static void gdk_x11_drag_context_drag_drop (GdkDragContext *context,
188 static void gdk_x11_drag_context_drop_reply (GdkDragContext *context,
191 static void gdk_x11_drag_context_drop_finish (GdkDragContext *context,
194 static gboolean gdk_x11_drag_context_drop_status (GdkDragContext *context);
195 static GdkAtom gdk_x11_drag_context_get_selection (GdkDragContext *context);
198 gdk_x11_drag_context_class_init (GdkX11DragContextClass *klass)
200 GObjectClass *object_class = G_OBJECT_CLASS (klass);
201 GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
203 object_class->finalize = gdk_x11_drag_context_finalize;
205 context_class->find_window = gdk_x11_drag_context_find_window;
206 context_class->drag_status = gdk_x11_drag_context_drag_status;
207 context_class->drag_motion = gdk_x11_drag_context_drag_motion;
208 context_class->drag_abort = gdk_x11_drag_context_drag_abort;
209 context_class->drag_drop = gdk_x11_drag_context_drag_drop;
210 context_class->drop_reply = gdk_x11_drag_context_drop_reply;
211 context_class->drop_finish = gdk_x11_drag_context_drop_finish;
212 context_class->drop_status = gdk_x11_drag_context_drop_status;
213 context_class->get_selection = gdk_x11_drag_context_get_selection;
217 gdk_x11_drag_context_finalize (GObject *object)
219 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
220 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (object);
222 if (context->source_window)
224 if ((context->protocol == GDK_DRAG_PROTO_XDND) && !context->is_source)
225 xdnd_manage_source_filter (context, context->source_window, FALSE);
228 g_slist_free_full (x11_context->window_caches, (GDestroyNotify)gdk_window_cache_unref);
229 x11_context->window_caches = NULL;
231 contexts = g_list_remove (contexts, context);
233 G_OBJECT_CLASS (gdk_x11_drag_context_parent_class)->finalize (object);
238 static GdkDragContext *
239 gdk_drag_context_find (GdkDisplay *display,
244 GList *tmp_list = contexts;
245 GdkDragContext *context;
246 GdkX11DragContext *context_x11;
247 Window context_dest_xid;
251 context = (GdkDragContext *)tmp_list->data;
252 context_x11 = (GdkX11DragContext *)context;
254 if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
255 (context->dest_window && gdk_window_get_display (context->dest_window) != display))
258 context_dest_xid = context->dest_window
259 ? (context_x11->drop_xid
260 ? context_x11->drop_xid
261 : GDK_WINDOW_XID (context->dest_window))
264 if ((!context->is_source == !is_source) &&
265 ((source_xid == None) || (context->source_window &&
266 (GDK_WINDOW_XID (context->source_window) == source_xid))) &&
267 ((dest_xid == None) || (context_dest_xid == dest_xid)))
270 tmp_list = tmp_list->next;
277 precache_target_list (GdkDragContext *context)
279 if (context->targets)
281 GPtrArray *targets = g_ptr_array_new ();
285 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
286 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
288 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
289 (const gchar **)targets->pdata,
292 for (i =0; i < targets->len; i++)
293 g_free (targets->pdata[i]);
295 g_ptr_array_free (targets, TRUE);
299 /* Utility functions */
302 free_cache_child (GdkCacheChild *child,
306 cairo_region_destroy (child->shape);
308 if (child->shape_selected && display)
310 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
312 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
319 gdk_window_cache_add (GdkWindowCache *cache,
327 GdkCacheChild *child = g_new (GdkCacheChild, 1);
332 child->width = width;
333 child->height = height;
334 child->mapped = mapped;
335 child->shape_selected = FALSE;
336 child->shape_valid = FALSE;
339 cache->children = g_list_prepend (cache->children, child);
340 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
344 static GdkFilterReturn
345 gdk_window_cache_shape_filter (GdkXEvent *xev,
349 XEvent *xevent = (XEvent *)xev;
350 GdkWindowCache *cache = data;
352 GdkX11Display *display = GDK_X11_DISPLAY (gdk_screen_get_display (cache->screen));
354 if (display->have_shapes &&
355 xevent->type == display->shape_event_base + ShapeNotify)
357 XShapeEvent *xse = (XShapeEvent*)xevent;
360 node = g_hash_table_lookup (cache->child_hash,
361 GUINT_TO_POINTER (xse->window));
364 GdkCacheChild *child = node->data;
365 child->shape_valid = FALSE;
368 cairo_region_destroy (child->shape);
373 return GDK_FILTER_REMOVE;
376 return GDK_FILTER_CONTINUE;
379 static GdkFilterReturn
380 gdk_window_cache_filter (GdkXEvent *xev,
384 XEvent *xevent = (XEvent *)xev;
385 GdkWindowCache *cache = data;
387 switch (xevent->type)
389 case CirculateNotify:
391 case ConfigureNotify:
393 XConfigureEvent *xce = &xevent->xconfigure;
396 node = g_hash_table_lookup (cache->child_hash,
397 GUINT_TO_POINTER (xce->window));
400 GdkCacheChild *child = node->data;
403 child->width = xce->width;
404 child->height = xce->height;
405 if (xce->above == None && (node->next))
407 GList *last = g_list_last (cache->children);
408 cache->children = g_list_remove_link (cache->children, node);
415 GList *above_node = g_hash_table_lookup (cache->child_hash,
416 GUINT_TO_POINTER (xce->above));
417 if (above_node && node->next != above_node)
419 /* Put the window above (before in the list) above_node */
420 cache->children = g_list_remove_link (cache->children, node);
421 node->prev = above_node->prev;
423 node->prev->next = node;
425 cache->children = node;
426 node->next = above_node;
427 above_node->prev = node;
435 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
437 if (!g_hash_table_lookup (cache->child_hash,
438 GUINT_TO_POINTER (xcwe->window)))
439 gdk_window_cache_add (cache, xcwe->window,
440 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
446 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
449 node = g_hash_table_lookup (cache->child_hash,
450 GUINT_TO_POINTER (xdwe->window));
453 GdkCacheChild *child = node->data;
455 g_hash_table_remove (cache->child_hash,
456 GUINT_TO_POINTER (xdwe->window));
457 cache->children = g_list_remove_link (cache->children, node);
458 /* window is destroyed, no need to disable ShapeNotify */
459 free_cache_child (child, NULL);
460 g_list_free_1 (node);
466 XMapEvent *xme = &xevent->xmap;
469 node = g_hash_table_lookup (cache->child_hash,
470 GUINT_TO_POINTER (xme->window));
473 GdkCacheChild *child = node->data;
474 child->mapped = TRUE;
482 XMapEvent *xume = &xevent->xmap;
485 node = g_hash_table_lookup (cache->child_hash,
486 GUINT_TO_POINTER (xume->window));
489 GdkCacheChild *child = node->data;
490 child->mapped = FALSE;
495 return GDK_FILTER_CONTINUE;
497 return GDK_FILTER_REMOVE;
500 static GdkWindowCache *
501 gdk_window_cache_new (GdkScreen *screen)
503 XWindowAttributes xwa;
504 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
505 GdkWindow *root_window = gdk_screen_get_root_window (screen);
506 GdkChildInfoX11 *children;
508 #ifdef HAVE_XCOMPOSITE
512 GdkWindowCache *result = g_new (GdkWindowCache, 1);
514 result->children = NULL;
515 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
516 result->screen = screen;
517 result->ref_count = 1;
519 XGetWindowAttributes (xdisplay, GDK_WINDOW_XID (root_window), &xwa);
520 result->old_event_mask = xwa.your_event_mask;
522 if (G_UNLIKELY (!GDK_X11_DISPLAY (GDK_X11_SCREEN (screen)->display)->trusted_client))
524 GList *toplevel_windows, *list;
526 gint x, y, width, height;
528 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
529 for (list = toplevel_windows; list; list = list->next)
531 window = GDK_WINDOW (list->data);
532 gdk_window_get_geometry (window, &x, &y, &width, &height);
533 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
535 gdk_window_is_visible (window));
537 g_list_free (toplevel_windows);
541 XSelectInput (xdisplay, GDK_WINDOW_XID (root_window),
542 result->old_event_mask | SubstructureNotifyMask);
543 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
544 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
546 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
547 GDK_WINDOW_XID (root_window),
549 &children, &nchildren))
552 for (i = 0; i < nchildren ; i++)
554 gdk_window_cache_add (result, children[i].window,
555 children[i].x, children[i].y, children[i].width, children[i].height,
556 children[i].is_mapped);
561 #ifdef HAVE_XCOMPOSITE
563 * Add the composite overlay window to the cache, as this can be a reasonable
564 * Xdnd proxy as well.
565 * This is only done when the screen is composited in order to avoid mapping
566 * the COW. We assume that the CM is using the COW (which is true for pretty
567 * much any CM currently in use).
569 if (gdk_screen_is_composited (screen))
571 cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
572 gdk_window_cache_add (result, cow, 0, 0, gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
573 XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
581 gdk_window_cache_destroy (GdkWindowCache *cache)
583 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
586 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
587 GDK_WINDOW_XID (root_window),
588 cache->old_event_mask);
589 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
590 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
592 display = gdk_screen_get_display (cache->screen);
594 gdk_x11_display_error_trap_push (display);
595 g_list_foreach (cache->children, (GFunc)free_cache_child, display);
596 gdk_x11_display_error_trap_pop_ignored (display);
598 g_list_free (cache->children);
599 g_hash_table_destroy (cache->child_hash);
604 static GdkWindowCache *
605 gdk_window_cache_ref (GdkWindowCache *cache)
607 cache->ref_count += 1;
613 gdk_window_cache_unref (GdkWindowCache *cache)
615 g_assert (cache->ref_count > 0);
617 cache->ref_count -= 1;
619 if (cache->ref_count == 0)
621 window_caches = g_slist_remove (window_caches, cache);
622 gdk_window_cache_destroy (cache);
627 gdk_window_cache_get (GdkScreen *screen)
630 GdkWindowCache *cache;
632 for (list = window_caches; list; list = list->next)
635 if (cache->screen == screen)
636 return gdk_window_cache_ref (cache);
639 cache = gdk_window_cache_new (screen);
641 window_caches = g_slist_prepend (window_caches, cache);
647 is_pointer_within_shape (GdkDisplay *display,
648 GdkCacheChild *child,
652 if (!child->shape_selected)
654 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
656 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
657 child->shape_selected = TRUE;
659 if (!child->shape_valid)
661 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
662 cairo_region_t *input_shape;
664 child->shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
665 child->xid, ShapeBounding);
667 input_shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
668 child->xid, ShapeInput);
669 if (child->shape && input_shape)
671 cairo_region_intersect (child->shape, input_shape);
672 cairo_region_destroy (input_shape);
674 else if (input_shape)
676 child->shape = input_shape;
680 child->shape_valid = TRUE;
683 return child->shape == NULL ||
684 cairo_region_contains_point (child->shape, x_pos, y_pos);
688 get_client_window_at_coords_recurse (GdkDisplay *display,
690 gboolean is_toplevel,
694 GdkChildInfoX11 *children;
695 unsigned int nchildren;
697 gboolean found_child = FALSE;
698 GdkChildInfoX11 child = { 0, };
699 gboolean has_wm_state = FALSE;
701 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
702 is_toplevel? &has_wm_state : NULL,
703 &children, &nchildren))
713 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
715 GdkChildInfoX11 *cur_child = &children[i];
717 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
718 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
719 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
732 if (child.has_wm_state)
735 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
742 get_client_window_at_coords (GdkWindowCache *cache,
748 Window retval = None;
751 display = gdk_screen_get_display (cache->screen);
753 gdk_x11_display_error_trap_push (display);
755 tmp_list = cache->children;
757 while (tmp_list && !retval)
759 GdkCacheChild *child = tmp_list->data;
761 if ((child->xid != ignore) && (child->mapped))
763 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
764 (y_root >= child->y) && (y_root < child->y + child->height))
766 if (!is_pointer_within_shape (display, child,
770 tmp_list = tmp_list->next;
774 retval = get_client_window_at_coords_recurse (display,
782 tmp_list = tmp_list->next;
785 gdk_x11_display_error_trap_pop_ignored (display);
790 return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
793 /*************************************************************
794 ***************************** MOTIF *************************
795 *************************************************************/
797 /* values used in the message type for Motif DND */
810 /* Values used to specify type of protocol to use */
814 XmDRAG_PREFER_PREREGISTER,
816 XmDRAG_PREFER_DYNAMIC,
818 XmDRAG_PREFER_RECEIVER
821 /* Operation codes */
829 /* Drop site status */
831 XmNO_DROP_SITE = 0x01,
832 XmDROP_SITE_INVALID = 0x02,
833 XmDROP_SITE_VALID = 0x03
836 /* completion status */
844 /* Byte swapping routines. The motif specification leaves it
845 * up to us to save a few bytes in the client messages
847 static gchar local_byte_order = '\0';
849 #ifdef G_ENABLE_DEBUG
851 print_target_list (GList *targets)
855 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
856 g_message ("\t%s", name);
858 targets = targets->next;
861 #endif /* G_ENABLE_DEBUG */
864 init_byte_order (void)
866 guint32 myint = 0x01020304;
867 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
871 card16_to_host (guint16 x, gchar byte_order)
873 if (byte_order == local_byte_order)
876 return (x << 8) | (x >> 8);
880 card32_to_host (guint32 x, gchar byte_order)
882 if (byte_order == local_byte_order)
885 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
888 /* Motif packs together fields of varying length into the
889 * client message. We can't rely on accessing these
890 * through data.s[], data.l[], etc, because on some architectures
891 * (i.e., Alpha) these won't be valid for format == 8.
894 #define MOTIF_XCLIENT_BYTE(xevent,i) \
895 (xevent)->xclient.data.b[i]
896 #define MOTIF_XCLIENT_SHORT(xevent,i) \
897 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
898 #define MOTIF_XCLIENT_LONG(xevent,i) \
899 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
901 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
902 #define MOTIF_UNPACK_SHORT(xevent,i) \
903 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
904 #define MOTIF_UNPACK_LONG(xevent,i) \
905 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
907 /***** Dest side ***********/
909 /* Property placed on source windows */
910 typedef struct _MotifDragInitiatorInfo
913 guint8 protocol_version;
914 guint16 targets_index;
915 guint32 selection_atom;
916 } MotifDragInitiatorInfo;
918 /* Header for target table on the drag window */
919 typedef struct _MotifTargetTableHeader
922 guchar protocol_version;
925 } MotifTargetTableHeader;
927 /* Property placed on target windows */
928 typedef struct _MotifDragReceiverInfo
931 guint8 protocol_version;
932 guint8 protocol_style;
934 guint32 proxy_window;
935 guint16 num_drop_sites;
938 } MotifDragReceiverInfo;
940 /* Target table handling */
942 static GdkFilterReturn
943 motif_drag_window_filter (GdkXEvent *xevent,
947 XEvent *xev = (XEvent *)xevent;
948 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
949 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
951 switch (xev->xany.type)
954 display_x11->motif_drag_window = None;
955 display_x11->motif_drag_gdk_window = NULL;
958 if (display_x11->motif_target_lists &&
959 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
960 motif_read_target_table (display);
963 return GDK_FILTER_REMOVE;
967 motif_lookup_drag_window (GdkDisplay *display,
968 Display *lookup_xdisplay)
970 Window retval = None;
971 gulong bytes_after, nitems;
976 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
977 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
979 XA_WINDOW, &type, &format, &nitems, &bytes_after,
982 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
984 retval = *(Window *)data;
986 g_message ("Found drag window %#lx\n", GDK_X11_DISPLAY (display)->motif_drag_window));
995 /* Finds the window where global Motif drag information is stored.
996 * If it doesn't exist and 'create' is TRUE, create one.
999 motif_find_drag_window (GdkDisplay *display,
1002 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1004 if (!display_x11->motif_drag_window)
1006 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
1007 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
1009 if (!display_x11->motif_drag_window && create)
1011 /* Create a persistant window. (Copied from LessTif) */
1012 Display *persistant_xdisplay;
1013 XSetWindowAttributes attr;
1014 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
1015 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
1017 XGrabServer (persistant_xdisplay);
1019 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
1021 if (!display_x11->motif_drag_window)
1023 attr.override_redirect = True;
1024 attr.event_mask = PropertyChangeMask;
1026 display_x11->motif_drag_window =
1027 XCreateWindow (persistant_xdisplay,
1028 RootWindow (persistant_xdisplay, 0),
1029 -100, -100, 10, 10, 0, 0,
1030 InputOnly, (Visual *)CopyFromParent,
1031 (CWOverrideRedirect | CWEventMask), &attr);
1034 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
1036 XChangeProperty (persistant_xdisplay,
1037 RootWindow (persistant_xdisplay, 0),
1038 motif_drag_window_atom, XA_WINDOW,
1039 32, PropModeReplace,
1040 (guchar *)&motif_drag_window_atom, 1);
1042 XUngrabServer (persistant_xdisplay);
1043 XCloseDisplay (persistant_xdisplay);
1046 /* There is a miniscule race condition here if the drag window
1047 * gets destroyed exactly now.
1049 if (display_x11->motif_drag_window)
1051 display_x11->motif_drag_gdk_window =
1052 gdk_x11_window_foreign_new_for_display (display, display_x11->motif_drag_window);
1053 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
1054 motif_drag_window_filter,
1059 return display_x11->motif_drag_window;
1063 motif_read_target_table (GdkDisplay *display)
1065 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1066 gulong bytes_after, nitems;
1071 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1073 if (display_x11->motif_target_lists)
1075 for (i=0; i<display_x11->motif_n_target_lists; i++)
1076 g_list_free (display_x11->motif_target_lists[i]);
1078 g_free (display_x11->motif_target_lists);
1079 display_x11->motif_target_lists = NULL;
1080 display_x11->motif_n_target_lists = 0;
1083 if (motif_find_drag_window (display, FALSE))
1086 MotifTargetTableHeader *header = NULL;
1087 guchar *target_bytes = NULL;
1089 gboolean success = FALSE;
1091 gdk_x11_display_error_trap_push (display);
1092 XGetWindowProperty (display_x11->xdisplay,
1093 display_x11->motif_drag_window,
1094 motif_drag_targets_atom,
1095 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1096 motif_drag_targets_atom,
1097 &type, &format, &nitems, &bytes_after,
1100 if (gdk_x11_display_error_trap_pop (display) ||
1101 (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1104 header = (MotifTargetTableHeader *)data;
1106 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1107 header->total_size = card32_to_host (header->total_size, header->byte_order);
1109 gdk_x11_display_error_trap_push (display);
1110 XGetWindowProperty (display_x11->xdisplay,
1111 display_x11->motif_drag_window,
1112 motif_drag_targets_atom,
1113 (sizeof(MotifTargetTableHeader)+3)/4,
1114 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1116 motif_drag_targets_atom, &type, &format, &nitems,
1117 &bytes_after, &target_bytes);
1119 if (gdk_x11_display_error_trap_pop (display) ||
1120 (format != 8) || (bytes_after != 0) ||
1121 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1124 display_x11->motif_n_target_lists = header->n_lists;
1125 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1128 for (i=0; i<header->n_lists; i++)
1133 if (p + sizeof(guint16) - target_bytes > nitems)
1136 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1138 /* We need to make a copy of the targets, since it may
1141 targets = g_new (guint32, n_targets);
1142 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1144 p += sizeof(guint16) + n_targets * sizeof(guint32);
1145 if (p - target_bytes > nitems)
1148 for (j=0; j<n_targets; j++)
1149 display_x11->motif_target_lists[i] =
1150 g_list_prepend (display_x11->motif_target_lists[i],
1151 GUINT_TO_POINTER (card32_to_host (targets[j],
1152 header->byte_order)));
1154 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1164 XFree (target_bytes);
1168 if (display_x11->motif_target_lists)
1170 g_free (display_x11->motif_target_lists);
1171 display_x11->motif_target_lists = NULL;
1172 display_x11->motif_n_target_lists = 0;
1174 g_warning ("Error reading Motif target table\n");
1180 targets_sort_func (gconstpointer a, gconstpointer b)
1182 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1183 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1186 /* Check if given (sorted) list is in the targets table */
1188 motif_target_table_check (GdkDisplay *display,
1191 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1192 GList *tmp_list1, *tmp_list2;
1195 for (i=0; i<display_x11->motif_n_target_lists; i++)
1197 tmp_list1 = display_x11->motif_target_lists[i];
1200 while (tmp_list1 && tmp_list2)
1202 if (tmp_list1->data != tmp_list2->data)
1205 tmp_list1 = tmp_list1->next;
1206 tmp_list2 = tmp_list2->next;
1208 if (!tmp_list1 && !tmp_list2) /* Found it */
1216 motif_add_to_target_table (GdkDisplay *display,
1217 GList *targets) /* targets is list of GdkAtom */
1219 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1220 GList *sorted = NULL;
1225 /* make a sorted copy of the list */
1228 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1229 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1230 targets = targets->next;
1233 /* First check if it is there already */
1235 if (display_x11->motif_target_lists)
1236 index = motif_target_table_check (display, sorted);
1238 /* We need to grab the server while doing this, to ensure
1244 /* We need to make sure that it exists _before_ we grab the
1245 * server, since we can't open a new connection after we
1248 motif_find_drag_window (display, TRUE);
1250 gdk_x11_display_grab (display);
1251 motif_read_target_table (display);
1253 /* Check again, in case it was added in the meantime */
1254 if (display_x11->motif_target_lists)
1255 index = motif_target_table_check (display, sorted);
1259 guint32 total_size = 0;
1263 MotifTargetTableHeader *header;
1265 if (!display_x11->motif_target_lists)
1267 display_x11->motif_target_lists = g_new (GList *, 1);
1268 display_x11->motif_n_target_lists = 1;
1272 display_x11->motif_n_target_lists++;
1273 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1274 sizeof(GList *) * display_x11->motif_n_target_lists);
1276 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1278 index = display_x11->motif_n_target_lists - 1;
1280 total_size = sizeof (MotifTargetTableHeader);
1281 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1282 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1284 data = g_malloc (total_size);
1286 header = (MotifTargetTableHeader *)data;
1287 p = data + sizeof(MotifTargetTableHeader);
1289 header->byte_order = local_byte_order;
1290 header->protocol_version = 0;
1291 header->n_lists = display_x11->motif_n_target_lists;
1292 header->total_size = total_size;
1294 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1296 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1297 guint32 *targets = g_new (guint32, n_targets);
1298 guint32 *p32 = targets;
1300 tmp_list = display_x11->motif_target_lists[i];
1303 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1305 tmp_list = tmp_list->next;
1310 p += sizeof(guint16);
1312 memcpy (p, targets, n_targets * sizeof(guint32));
1315 p += sizeof(guint32) * n_targets;
1319 XChangeProperty (display_x11->xdisplay,
1320 display_x11->motif_drag_window,
1321 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1322 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1326 gdk_x11_display_ungrab (display);
1329 g_list_free (sorted);
1333 /* Translate flags */
1336 motif_dnd_translate_flags (GdkX11DragContext *context_x11,
1339 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1340 guint recommended_op = flags & 0x000f;
1341 guint possible_ops = (flags & 0x0f0) >> 4;
1343 switch (recommended_op)
1346 context->suggested_action = GDK_ACTION_MOVE;
1349 context->suggested_action = GDK_ACTION_COPY;
1352 context->suggested_action = GDK_ACTION_LINK;
1355 context->suggested_action = GDK_ACTION_COPY;
1359 context->actions = 0;
1360 if (possible_ops & XmDROP_MOVE)
1361 context->actions |= GDK_ACTION_MOVE;
1362 if (possible_ops & XmDROP_COPY)
1363 context->actions |= GDK_ACTION_COPY;
1364 if (possible_ops & XmDROP_LINK)
1365 context->actions |= GDK_ACTION_LINK;
1369 motif_dnd_get_flags (GdkDragContext *context)
1373 switch (context->suggested_action)
1375 case GDK_ACTION_MOVE:
1376 flags = XmDROP_MOVE;
1378 case GDK_ACTION_COPY:
1379 flags = XmDROP_COPY;
1381 case GDK_ACTION_LINK:
1382 flags = XmDROP_LINK;
1385 flags = XmDROP_NOOP;
1389 if (context->actions & GDK_ACTION_MOVE)
1390 flags |= XmDROP_MOVE << 8;
1391 if (context->actions & GDK_ACTION_COPY)
1392 flags |= XmDROP_COPY << 8;
1393 if (context->actions & GDK_ACTION_LINK)
1394 flags |= XmDROP_LINK << 8;
1402 motif_set_targets (GdkX11DragContext *context_x11)
1404 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1405 MotifDragInitiatorInfo info;
1407 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1409 info.byte_order = local_byte_order;
1410 info.protocol_version = 0;
1411 info.targets_index = motif_add_to_target_table (display, context->targets);
1416 g_snprintf (buf, 20, "_GDK_SELECTION_%d", i);
1418 context_x11->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1419 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), context_x11->motif_selection))
1423 info.selection_atom = context_x11->motif_selection;
1425 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1426 GDK_WINDOW_XID (context->source_window),
1427 context_x11->motif_selection,
1428 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1430 (guchar *)&info, sizeof (info));
1432 context_x11->motif_targets_set = 1;
1436 motif_check_dest (GdkDisplay *display,
1439 gboolean retval = FALSE;
1441 MotifDragReceiverInfo *info;
1444 unsigned long nitems, after;
1445 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1447 gdk_x11_display_error_trap_push (display);
1448 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1449 motif_drag_receiver_info_atom,
1450 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1451 &type, &format, &nitems, &after,
1454 if (gdk_x11_display_error_trap_pop (display) == 0)
1458 info = (MotifDragReceiverInfo *)data;
1460 if ((format == 8) && (nitems == sizeof(*info)))
1462 if ((info->protocol_version == 0) &&
1463 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1464 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1465 (info->protocol_style == XmDRAG_DYNAMIC)))
1471 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1478 return retval ? win : None;
1482 motif_send_enter (GdkX11DragContext *context_x11,
1485 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1486 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1489 if (!G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
1490 return; /* Motif Dnd requires getting properties on the root window */
1492 xev.xclient.type = ClientMessage;
1493 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1494 xev.xclient.format = 8;
1495 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1497 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1498 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1499 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1500 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1501 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XID (context->source_window);
1503 if (!context_x11->motif_targets_set)
1504 motif_set_targets (context_x11);
1506 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1507 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1509 if (!_gdk_x11_display_send_xevent (display,
1510 GDK_WINDOW_XID (context->dest_window),
1513 g_message ("Send event to %lx failed",
1514 GDK_WINDOW_XID (context->dest_window)));
1518 motif_send_leave (GdkX11DragContext *context_x11,
1521 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1522 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1525 xev.xclient.type = ClientMessage;
1526 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1527 xev.xclient.format = 8;
1528 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1530 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1531 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1532 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1533 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1534 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1535 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1536 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1538 if (!_gdk_x11_display_send_xevent (display,
1539 GDK_WINDOW_XID (context->dest_window),
1542 g_message ("Send event to %lx failed",
1543 GDK_WINDOW_XID (context->dest_window)));
1547 motif_send_motion (GdkX11DragContext *context_x11,
1550 GdkDragAction action,
1553 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1554 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1558 xev.xclient.type = ClientMessage;
1559 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1560 xev.xclient.format = 8;
1561 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1563 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1564 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1565 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1566 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1567 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1569 if ((context->suggested_action != context_x11->old_action) ||
1570 (context->actions != context_x11->old_actions))
1572 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1574 /* context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1579 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1581 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1582 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1584 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1588 if (!_gdk_x11_display_send_xevent (display,
1589 GDK_WINDOW_XID (context->dest_window),
1592 g_message ("Send event to %lx failed",
1593 GDK_WINDOW_XID (context->dest_window)));
1599 motif_send_drop (GdkX11DragContext *context_x11,
1602 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1603 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1606 xev.xclient.type = ClientMessage;
1607 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1608 xev.xclient.format = 8;
1609 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1611 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1612 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1613 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1614 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1616 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
1617 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
1619 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1620 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XID (context->source_window);
1622 if (!_gdk_x11_display_send_xevent (display,
1623 GDK_WINDOW_XID (context->dest_window),
1626 g_message ("Send event to %lx failed",
1627 GDK_WINDOW_XID (context->dest_window)));
1633 motif_read_initiator_info (GdkDisplay *display,
1634 Window source_window,
1645 MotifDragInitiatorInfo *initiator_info;
1647 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1649 gdk_x11_display_error_trap_push (display);
1650 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1651 0, sizeof(*initiator_info), FALSE,
1652 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1653 &type, &format, &nitems, &bytes_after,
1656 if (gdk_x11_display_error_trap_pop (display) ||
1657 (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) ||
1660 g_warning ("Error reading initiator info\n");
1664 initiator_info = (MotifDragInitiatorInfo *)data;
1666 motif_read_target_table (display);
1668 initiator_info->targets_index =
1669 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1670 initiator_info->selection_atom =
1671 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1673 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1675 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1676 XFree (initiator_info);
1680 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1685 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1686 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1687 tmp_list = tmp_list->prev;
1690 #ifdef G_ENABLE_DEBUG
1691 if (_gdk_debug_flags & GDK_DEBUG_DND)
1692 print_target_list (*targets);
1693 #endif /* G_ENABLE_DEBUG */
1695 *selection = initiator_info->selection_atom;
1697 XFree (initiator_info);
1702 static GdkDragContext *
1703 motif_drag_context_new (GdkWindow *dest_window,
1705 guint32 source_window,
1708 GdkX11DragContext *context_x11;
1709 GdkDragContext *context;
1710 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
1711 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1713 /* FIXME, current_dest_drag really shouldn't be NULL'd
1714 * if we error below.
1716 if (display_x11->current_dest_drag != NULL)
1718 if (timestamp >= display_x11->current_dest_drag->start_time)
1720 g_object_unref (display_x11->current_dest_drag);
1721 display_x11->current_dest_drag = NULL;
1727 context_x11 = g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
1728 context = GDK_DRAG_CONTEXT (context_x11);
1730 context->protocol = GDK_DRAG_PROTO_MOTIF;
1731 context->is_source = FALSE;
1733 context->source_window = gdk_x11_window_lookup_for_display (display, source_window);
1734 if (context->source_window)
1735 g_object_ref (context->source_window);
1738 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
1739 if (!context->source_window)
1741 g_object_unref (context_x11);
1746 context->dest_window = dest_window;
1747 g_object_ref (dest_window);
1748 context->start_time = timestamp;
1750 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1754 &context_x11->motif_selection))
1756 g_object_unref (context_x11);
1764 * The MOTIF drag protocol has no real provisions for distinguishing
1765 * multiple simultaneous drops. If the sources grab the pointer
1766 * when doing drags, that shouldn't happen, in any case. If it
1767 * does, we can't do much except hope for the best.
1770 static GdkFilterReturn
1771 motif_top_level_enter (GdkEvent *event,
1774 guint32 source_window,
1777 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1778 GdkDragContext *new_context;
1780 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1781 flags, timestamp, source_window, atom));
1783 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1785 return GDK_FILTER_REMOVE;
1787 event->dnd.type = GDK_DRAG_ENTER;
1788 event->dnd.context = new_context;
1789 g_object_ref (new_context);
1791 display_x11->current_dest_drag = new_context;
1793 return GDK_FILTER_TRANSLATE;
1796 static GdkFilterReturn
1797 motif_top_level_leave (GdkEvent *event,
1801 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1803 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1806 if ((display_x11->current_dest_drag != NULL) &&
1807 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1808 (timestamp >= display_x11->current_dest_drag->start_time))
1810 event->dnd.type = GDK_DRAG_LEAVE;
1811 /* Pass ownership of context to the event */
1812 event->dnd.context = display_x11->current_dest_drag;
1814 display_x11->current_dest_drag = NULL;
1816 return GDK_FILTER_TRANSLATE;
1819 return GDK_FILTER_REMOVE;
1822 static GdkFilterReturn
1823 motif_motion (GdkEvent *event,
1829 GdkX11DragContext *context_x11;
1830 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1832 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1833 flags, timestamp, x_root, y_root));
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 context_x11 = GDK_X11_DRAG_CONTEXT (display_x11->current_dest_drag);
1841 event->dnd.type = GDK_DRAG_MOTION;
1842 event->dnd.context = display_x11->current_dest_drag;
1843 g_object_ref (display_x11->current_dest_drag);
1845 event->dnd.time = timestamp;
1847 motif_dnd_translate_flags (context_x11, flags);
1849 event->dnd.x_root = x_root;
1850 event->dnd.y_root = y_root;
1852 context_x11->last_x = x_root;
1853 context_x11->last_y = y_root;
1855 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1857 return GDK_FILTER_TRANSLATE;
1860 return GDK_FILTER_REMOVE;
1863 static GdkFilterReturn
1864 motif_operation_changed (GdkEvent *event,
1868 GdkX11DragContext *context_x11;
1869 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1870 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1873 if ((display_x11->current_dest_drag != NULL) &&
1874 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1875 (timestamp >= display_x11->current_dest_drag->start_time))
1877 event->dnd.type = GDK_DRAG_MOTION;
1878 event->dnd.send_event = FALSE;
1879 event->dnd.context = display_x11->current_dest_drag;
1880 g_object_ref (display_x11->current_dest_drag);
1882 event->dnd.time = timestamp;
1883 context_x11 = GDK_X11_DRAG_CONTEXT (display_x11->current_dest_drag);
1885 motif_dnd_translate_flags (context_x11, flags);
1887 event->dnd.x_root = context_x11->last_x;
1888 event->dnd.y_root = context_x11->last_y;
1890 context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1892 return GDK_FILTER_TRANSLATE;
1895 return GDK_FILTER_REMOVE;
1898 static GdkFilterReturn
1899 motif_drop_start (GdkEvent *event,
1902 guint32 source_window,
1907 GdkDragContext *new_context;
1908 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1910 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1911 flags, timestamp, x_root, y_root, source_window, atom));
1913 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1915 return GDK_FILTER_REMOVE;
1917 motif_dnd_translate_flags (GDK_X11_DRAG_CONTEXT (new_context), flags);
1919 event->dnd.type = GDK_DROP_START;
1920 event->dnd.context = new_context;
1921 event->dnd.time = timestamp;
1922 event->dnd.x_root = x_root;
1923 event->dnd.y_root = y_root;
1925 gdk_x11_window_set_user_time (event->any.window, timestamp);
1927 g_object_ref (new_context);
1928 display_x11->current_dest_drag = new_context;
1930 return GDK_FILTER_TRANSLATE;
1933 static GdkFilterReturn
1934 motif_drag_status (GdkEvent *event,
1938 GdkDragContext *context;
1939 GdkDisplay *display;
1942 g_message ("Motif status message: flags %x", flags));
1944 display = gdk_window_get_display (event->any.window);
1946 return GDK_FILTER_REMOVE;
1948 context = gdk_drag_context_find (display, TRUE, GDK_WINDOW_XID (event->any.window), None);
1952 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
1953 if ((context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1954 (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1955 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
1957 event->dnd.type = GDK_DRAG_STATUS;
1958 event->dnd.send_event = FALSE;
1959 event->dnd.context = context;
1960 g_object_ref (context);
1962 event->dnd.time = timestamp;
1964 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1966 switch (flags & 0x000f)
1969 context->action = 0;
1972 context->action = GDK_ACTION_MOVE;
1975 context->action = GDK_ACTION_COPY;
1978 context->action = GDK_ACTION_LINK;
1983 context->action = 0;
1985 return GDK_FILTER_TRANSLATE;
1987 return GDK_FILTER_REMOVE;
1990 static GdkFilterReturn
1991 motif_dnd_filter (GdkXEvent *xev,
1995 XEvent *xevent = (XEvent *)xev;
2000 guint32 source_window;
2002 gint16 x_root, y_root;
2005 if (!event->any.window ||
2006 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2007 return GDK_FILTER_CONTINUE; /* Not for us */
2009 /* First read some fields common to all Motif DND messages */
2010 reason = MOTIF_UNPACK_BYTE (xevent, 0);
2011 flags = MOTIF_UNPACK_SHORT (xevent, 1);
2012 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
2014 is_reply = ((reason & 0x80) != 0);
2016 switch (reason & 0x7f)
2018 case XmTOP_LEVEL_ENTER:
2019 source_window = MOTIF_UNPACK_LONG (xevent, 2);
2020 atom = MOTIF_UNPACK_LONG (xevent, 3);
2021 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
2022 case XmTOP_LEVEL_LEAVE:
2023 return motif_top_level_leave (event, flags, timestamp);
2026 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2027 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2030 return motif_motion (event, flags, timestamp, x_root, y_root);
2032 return motif_drag_status (event, flags, timestamp);
2034 case XmDROP_SITE_ENTER:
2035 return motif_drag_status (event, flags, timestamp);
2037 case XmDROP_SITE_LEAVE:
2038 return motif_drag_status (event,
2039 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
2042 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2043 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2044 atom = MOTIF_UNPACK_LONG (xevent, 3);
2045 source_window = MOTIF_UNPACK_LONG (xevent, 4);
2048 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
2051 case XmOPERATION_CHANGED:
2053 return motif_operation_changed (event, flags, timestamp);
2055 return motif_drag_status (event, flags, timestamp);
2058 /* To the best of my knowledge, these next two messages are
2059 * not part of the protocol, though they are defined in
2063 case XmDRAG_DROP_FINISH:
2067 return GDK_FILTER_REMOVE;
2070 /*************************************************************
2071 ***************************** XDND **************************
2072 *************************************************************/
2074 /* Utility functions */
2079 GdkDragAction action;
2080 } xdnd_actions_table[] = {
2081 { "XdndActionCopy", None, GDK_ACTION_COPY },
2082 { "XdndActionMove", None, GDK_ACTION_MOVE },
2083 { "XdndActionLink", None, GDK_ACTION_LINK },
2084 { "XdndActionAsk", None, GDK_ACTION_ASK },
2085 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2088 static const gint xdnd_n_actions = G_N_ELEMENTS (xdnd_actions_table);
2089 static gboolean xdnd_actions_initialized = FALSE;
2092 xdnd_initialize_actions (void)
2096 xdnd_actions_initialized = TRUE;
2097 for (i = 0; i < xdnd_n_actions; i++)
2098 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2101 static GdkDragAction
2102 xdnd_action_from_atom (GdkDisplay *display,
2111 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2113 if (!xdnd_actions_initialized)
2114 xdnd_initialize_actions();
2116 for (i = 0; i < xdnd_n_actions; i++)
2117 if (atom == xdnd_actions_table[i].atom)
2118 return xdnd_actions_table[i].action;
2124 xdnd_action_to_atom (GdkDisplay *display,
2125 GdkDragAction action)
2129 if (!xdnd_actions_initialized)
2130 xdnd_initialize_actions();
2132 for (i = 0; i < xdnd_n_actions; i++)
2133 if (action == xdnd_actions_table[i].action)
2134 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2141 static GdkFilterReturn
2142 xdnd_status_filter (GdkXEvent *xev,
2146 GdkDisplay *display;
2147 XEvent *xevent = (XEvent *)xev;
2148 guint32 dest_window = xevent->xclient.data.l[0];
2149 guint32 flags = xevent->xclient.data.l[1];
2150 Atom action = xevent->xclient.data.l[4];
2151 GdkDragContext *context;
2153 if (!event->any.window ||
2154 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2155 return GDK_FILTER_CONTINUE; /* Not for us */
2158 g_message ("XdndStatus: dest_window: %#x action: %ld",
2159 dest_window, action));
2161 display = gdk_window_get_display (event->any.window);
2162 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2166 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
2167 if (context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2168 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2170 event->dnd.send_event = FALSE;
2171 event->dnd.type = GDK_DRAG_STATUS;
2172 event->dnd.context = context;
2173 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2174 g_object_ref (context);
2176 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2177 if (!(action != 0) != !(flags & 1))
2180 g_warning ("Received status event with flags not corresponding to action!\n"));
2184 context->action = xdnd_action_from_atom (display, action);
2186 return GDK_FILTER_TRANSLATE;
2189 return GDK_FILTER_REMOVE;
2192 static GdkFilterReturn
2193 xdnd_finished_filter (GdkXEvent *xev,
2197 GdkDisplay *display;
2198 XEvent *xevent = (XEvent *)xev;
2199 guint32 dest_window = xevent->xclient.data.l[0];
2200 GdkDragContext *context;
2201 GdkX11DragContext *context_x11;
2203 if (!event->any.window ||
2204 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2205 return GDK_FILTER_CONTINUE; /* Not for us */
2208 g_message ("XdndFinished: dest_window: %#x", dest_window));
2210 display = gdk_window_get_display (event->any.window);
2211 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2215 context_x11 = GDK_X11_DRAG_CONTEXT (context);
2216 if (context_x11->version == 5)
2217 context_x11->drop_failed = xevent->xclient.data.l[1] == 0;
2219 event->dnd.type = GDK_DROP_FINISHED;
2220 event->dnd.context = context;
2221 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2222 g_object_ref (context);
2224 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2226 return GDK_FILTER_TRANSLATE;
2229 return GDK_FILTER_REMOVE;
2233 xdnd_set_targets (GdkX11DragContext *context_x11)
2235 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2237 GList *tmp_list = context->targets;
2239 gint n_atoms = g_list_length (context->targets);
2240 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2242 atomlist = g_new (Atom, n_atoms);
2246 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2247 tmp_list = tmp_list->next;
2251 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2252 GDK_WINDOW_XID (context->source_window),
2253 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2254 XA_ATOM, 32, PropModeReplace,
2255 (guchar *)atomlist, n_atoms);
2259 context_x11->xdnd_targets_set = 1;
2263 xdnd_set_actions (GdkX11DragContext *context_x11)
2265 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2270 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2272 if (!xdnd_actions_initialized)
2273 xdnd_initialize_actions();
2275 actions = context->actions;
2277 for (i = 0; i < xdnd_n_actions; i++)
2279 if (actions & xdnd_actions_table[i].action)
2281 actions &= ~xdnd_actions_table[i].action;
2286 atomlist = g_new (Atom, n_atoms);
2288 actions = context->actions;
2290 for (i = 0; i < xdnd_n_actions; i++)
2292 if (actions & xdnd_actions_table[i].action)
2294 actions &= ~xdnd_actions_table[i].action;
2295 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2300 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2301 GDK_WINDOW_XID (context->source_window),
2302 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2303 XA_ATOM, 32, PropModeReplace,
2304 (guchar *)atomlist, n_atoms);
2308 context_x11->xdnd_actions_set = TRUE;
2309 context_x11->xdnd_actions = context->actions;
2313 send_client_message_async_cb (Window window,
2317 GdkDragContext *context = data;
2319 g_message ("Got async callback for #%lx, success = %d",
2322 /* On failure, we immediately continue with the protocol
2323 * so we don't end up blocking for a timeout
2326 context->dest_window &&
2327 window == GDK_WINDOW_XID (context->dest_window))
2329 GdkEvent *temp_event;
2330 GdkX11DragContext *context_x11 = data;
2332 g_object_unref (context->dest_window);
2333 context->dest_window = NULL;
2334 context->action = 0;
2336 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2338 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2339 temp_event->dnd.window = g_object_ref (context->source_window);
2340 temp_event->dnd.send_event = TRUE;
2341 temp_event->dnd.context = g_object_ref (context);
2342 temp_event->dnd.time = GDK_CURRENT_TIME;
2343 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2345 gdk_event_put (temp_event);
2347 gdk_event_free (temp_event);
2350 g_object_unref (context);
2355 gdk_drag_context_get_display (GdkDragContext *context)
2357 if (context->source_window)
2358 return GDK_WINDOW_DISPLAY (context->source_window);
2359 else if (context->dest_window)
2360 return GDK_WINDOW_DISPLAY (context->dest_window);
2362 g_assert_not_reached ();
2367 send_client_message_async (GdkDragContext *context,
2371 XClientMessageEvent *event_send)
2373 GdkDisplay *display = gdk_drag_context_get_display (context);
2375 g_object_ref (context);
2377 _gdk_x11_send_client_message_async (display, window,
2378 propagate, event_mask, event_send,
2379 send_client_message_async_cb, context);
2383 xdnd_send_xevent (GdkX11DragContext *context_x11,
2388 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2389 GdkDisplay *display = gdk_drag_context_get_display (context);
2393 g_assert (event_send->xany.type == ClientMessage);
2395 /* We short-circuit messages to ourselves */
2396 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2400 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2402 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2403 event_send->xclient.message_type)
2405 GdkEvent *temp_event;
2407 temp_event = gdk_event_new (GDK_NOTHING);
2408 temp_event->any.window = g_object_ref (window);
2410 if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
2412 gdk_event_put (temp_event);
2413 gdk_event_free (temp_event);
2421 xwindow = GDK_WINDOW_XID (window);
2423 if (_gdk_x11_display_is_root_window (display, xwindow))
2424 event_mask = ButtonPressMask;
2428 send_client_message_async (context, xwindow, propagate, event_mask,
2429 &event_send->xclient);
2435 xdnd_send_enter (GdkX11DragContext *context_x11)
2437 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2438 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->dest_window);
2441 xev.xclient.type = ClientMessage;
2442 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2443 xev.xclient.format = 32;
2444 xev.xclient.window = context_x11->drop_xid
2445 ? context_x11->drop_xid
2446 : GDK_WINDOW_XID (context->dest_window);
2447 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2448 xev.xclient.data.l[1] = (context_x11->version << 24); /* version */
2449 xev.xclient.data.l[2] = 0;
2450 xev.xclient.data.l[3] = 0;
2451 xev.xclient.data.l[4] = 0;
2454 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2455 GDK_WINDOW_XID (context->source_window), context_x11->version));
2456 if (g_list_length (context->targets) > 3)
2458 if (!context_x11->xdnd_targets_set)
2459 xdnd_set_targets (context_x11);
2460 xev.xclient.data.l[1] |= 1;
2464 GList *tmp_list = context->targets;
2469 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2470 GDK_POINTER_TO_ATOM (tmp_list->data));
2471 tmp_list = tmp_list->next;
2476 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2479 g_message ("Send event to %lx failed",
2480 GDK_WINDOW_XID (context->dest_window)));
2481 g_object_unref (context->dest_window);
2482 context->dest_window = NULL;
2487 xdnd_send_leave (GdkX11DragContext *context_x11)
2489 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2490 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2493 xev.xclient.type = ClientMessage;
2494 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2495 xev.xclient.format = 32;
2496 xev.xclient.window = context_x11->drop_xid
2497 ? context_x11->drop_xid
2498 : GDK_WINDOW_XID (context->dest_window);
2499 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2500 xev.xclient.data.l[1] = 0;
2501 xev.xclient.data.l[2] = 0;
2502 xev.xclient.data.l[3] = 0;
2503 xev.xclient.data.l[4] = 0;
2505 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2508 g_message ("Send event to %lx failed",
2509 GDK_WINDOW_XID (context->dest_window)));
2510 g_object_unref (context->dest_window);
2511 context->dest_window = NULL;
2516 xdnd_send_drop (GdkX11DragContext *context_x11,
2519 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2520 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2523 xev.xclient.type = ClientMessage;
2524 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2525 xev.xclient.format = 32;
2526 xev.xclient.window = context_x11->drop_xid
2527 ? context_x11->drop_xid
2528 : GDK_WINDOW_XID (context->dest_window);
2529 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2530 xev.xclient.data.l[1] = 0;
2531 xev.xclient.data.l[2] = time;
2532 xev.xclient.data.l[3] = 0;
2533 xev.xclient.data.l[4] = 0;
2535 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2538 g_message ("Send event to %lx failed",
2539 GDK_WINDOW_XID (context->dest_window)));
2540 g_object_unref (context->dest_window);
2541 context->dest_window = NULL;
2546 xdnd_send_motion (GdkX11DragContext *context_x11,
2549 GdkDragAction action,
2552 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2553 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2556 xev.xclient.type = ClientMessage;
2557 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2558 xev.xclient.format = 32;
2559 xev.xclient.window = context_x11->drop_xid
2560 ? context_x11->drop_xid
2561 : GDK_WINDOW_XID (context->dest_window);
2562 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2563 xev.xclient.data.l[1] = 0;
2564 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2565 xev.xclient.data.l[3] = time;
2566 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2568 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2571 g_message ("Send event to %lx failed",
2572 GDK_WINDOW_XID (context->dest_window)));
2573 g_object_unref (context->dest_window);
2574 context->dest_window = NULL;
2576 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2580 xdnd_check_dest (GdkDisplay *display,
2582 guint *xdnd_version)
2584 gboolean retval = FALSE;
2587 unsigned long nitems, after;
2592 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2593 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2597 gdk_x11_display_error_trap_push (display);
2598 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2600 1, False, AnyPropertyType,
2601 &type, &format, &nitems, &after,
2606 proxy_data = (Window *)data;
2608 if ((format == 32) && (nitems == 1))
2610 proxy = *proxy_data;
2614 g_warning ("Invalid XdndProxy "
2615 "property on window %ld\n", win));
2620 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2622 1, False, AnyPropertyType,
2623 &type, &format, &nitems, &after,
2624 &data) == Success) &&
2627 version = (Atom *)data;
2629 if ((format == 32) && (nitems == 1))
2634 *xdnd_version = *version;
2638 g_warning ("Invalid XdndAware "
2639 "property on window %ld\n", win));
2645 gdk_x11_display_error_trap_pop_ignored (display);
2647 return retval ? (proxy ? proxy : win) : None;
2653 xdnd_read_actions (GdkX11DragContext *context_x11)
2655 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2656 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2659 gulong nitems, after;
2664 context_x11->xdnd_have_actions = FALSE;
2666 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2668 /* Get the XdndActionList, if set */
2670 gdk_x11_display_error_trap_push (display);
2671 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2672 GDK_WINDOW_XID (context->source_window),
2673 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2675 False, XA_ATOM, &type, &format, &nitems,
2676 &after, &data) == Success &&
2679 atoms = (Atom *)data;
2681 context->actions = 0;
2683 for (i = 0; i < nitems; i++)
2684 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2686 context_x11->xdnd_have_actions = TRUE;
2688 #ifdef G_ENABLE_DEBUG
2689 if (_gdk_debug_flags & GDK_DEBUG_DND)
2691 GString *action_str = g_string_new (NULL);
2692 if (context->actions & GDK_ACTION_MOVE)
2693 g_string_append(action_str, "MOVE ");
2694 if (context->actions & GDK_ACTION_COPY)
2695 g_string_append(action_str, "COPY ");
2696 if (context->actions & GDK_ACTION_LINK)
2697 g_string_append(action_str, "LINK ");
2698 if (context->actions & GDK_ACTION_ASK)
2699 g_string_append(action_str, "ASK ");
2701 g_message("Xdnd actions = %s", action_str->str);
2702 g_string_free (action_str, TRUE);
2704 #endif /* G_ENABLE_DEBUG */
2711 gdk_x11_display_error_trap_pop_ignored (display);
2717 GdkDragContext *source_context;
2719 source_context = gdk_drag_context_find (display, TRUE,
2720 GDK_WINDOW_XID (context->source_window),
2721 GDK_WINDOW_XID (context->dest_window));
2725 context->actions = source_context->actions;
2726 context_x11->xdnd_have_actions = TRUE;
2731 /* We have to make sure that the XdndActionList we keep internally
2732 * is up to date with the XdndActionList on the source window
2733 * because we get no notification, because Xdnd wasn't meant
2734 * to continually send actions. So we select on PropertyChangeMask
2735 * and add this filter.
2737 static GdkFilterReturn
2738 xdnd_source_window_filter (GdkXEvent *xev,
2742 XEvent *xevent = (XEvent *)xev;
2743 GdkX11DragContext *context_x11 = cb_data;
2744 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2746 if ((xevent->xany.type == PropertyNotify) &&
2747 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2749 xdnd_read_actions (context_x11);
2751 return GDK_FILTER_REMOVE;
2754 return GDK_FILTER_CONTINUE;
2758 xdnd_manage_source_filter (GdkDragContext *context,
2760 gboolean add_filter)
2762 if (!GDK_WINDOW_DESTROYED (window) &&
2763 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2765 gdk_x11_display_error_trap_push (GDK_WINDOW_DISPLAY (window));
2769 gdk_window_set_events (window,
2770 gdk_window_get_events (window) |
2771 GDK_PROPERTY_CHANGE_MASK);
2772 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2776 gdk_window_remove_filter (window,
2777 xdnd_source_window_filter,
2779 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2780 * but we might want it for other reasons. (Like
2781 * INCR selection transactions).
2785 gdk_x11_display_error_trap_pop_ignored (GDK_WINDOW_DISPLAY (window));
2790 base_precache_atoms (GdkDisplay *display)
2792 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2794 if (!display_x11->base_dnd_atoms_precached)
2796 static const char *const precache_atoms[] = {
2797 "ENLIGHTENMENT_DESKTOP",
2801 "_MOTIF_DRAG_RECEIVER_INFO"
2804 _gdk_x11_precache_atoms (display,
2805 precache_atoms, G_N_ELEMENTS (precache_atoms));
2807 display_x11->base_dnd_atoms_precached = TRUE;
2812 xdnd_precache_atoms (GdkDisplay *display)
2814 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2816 if (!display_x11->xdnd_atoms_precached)
2818 static const gchar *const precache_atoms[] = {
2824 "XdndActionPrivate",
2835 _gdk_x11_precache_atoms (display,
2836 precache_atoms, G_N_ELEMENTS (precache_atoms));
2838 display_x11->xdnd_atoms_precached = TRUE;
2842 static GdkFilterReturn
2843 xdnd_enter_filter (GdkXEvent *xev,
2847 GdkDeviceManager *device_manager;
2848 GdkDisplay *display;
2849 GdkX11Display *display_x11;
2850 XEvent *xevent = (XEvent *)xev;
2851 GdkDragContext *context;
2852 GdkX11DragContext *context_x11;
2856 gulong nitems, after;
2859 guint32 source_window;
2863 if (!event->any.window ||
2864 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2865 return GDK_FILTER_CONTINUE; /* Not for us */
2867 source_window = xevent->xclient.data.l[0];
2868 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2869 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2871 display = GDK_WINDOW_DISPLAY (event->any.window);
2872 display_x11 = GDK_X11_DISPLAY (display);
2874 xdnd_precache_atoms (display);
2877 g_message ("XdndEnter: source_window: %#x, version: %#x",
2878 source_window, version));
2882 /* Old source ignore */
2883 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2884 return GDK_FILTER_REMOVE;
2887 if (display_x11->current_dest_drag != NULL)
2889 g_object_unref (display_x11->current_dest_drag);
2890 display_x11->current_dest_drag = NULL;
2893 context_x11 = (GdkX11DragContext *)g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
2894 context = (GdkDragContext *)context_x11;
2896 context->protocol = GDK_DRAG_PROTO_XDND;
2897 context_x11->version = version;
2899 /* FIXME: Should extend DnD protocol to have device info */
2900 device_manager = gdk_display_get_device_manager (display);
2901 gdk_drag_context_set_device (context, gdk_device_manager_get_client_pointer (device_manager));
2903 context->source_window = gdk_x11_window_lookup_for_display (display, source_window);
2904 if (context->source_window)
2905 g_object_ref (context->source_window);
2908 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
2909 if (!context->source_window)
2911 g_object_unref (context);
2912 return GDK_FILTER_REMOVE;
2915 context->dest_window = event->any.window;
2916 g_object_ref (context->dest_window);
2918 context->targets = NULL;
2921 gdk_x11_display_error_trap_push (display);
2922 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2924 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2926 False, XA_ATOM, &type, &format, &nitems,
2929 if (gdk_x11_display_error_trap_pop (display) || (format != 32) || (type != XA_ATOM))
2931 g_object_unref (context);
2936 return GDK_FILTER_REMOVE;
2939 atoms = (Atom *)data;
2941 for (i = 0; i < nitems; i++)
2943 g_list_append (context->targets,
2944 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2951 for (i = 0; i < 3; i++)
2952 if (xevent->xclient.data.l[2 + i])
2954 g_list_append (context->targets,
2955 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2956 xevent->xclient.data.l[2 + i])));
2959 #ifdef G_ENABLE_DEBUG
2960 if (_gdk_debug_flags & GDK_DEBUG_DND)
2961 print_target_list (context->targets);
2962 #endif /* G_ENABLE_DEBUG */
2964 xdnd_manage_source_filter (context, context->source_window, TRUE);
2965 xdnd_read_actions (context_x11);
2967 event->dnd.type = GDK_DRAG_ENTER;
2968 event->dnd.context = context;
2969 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2970 g_object_ref (context);
2972 display_x11->current_dest_drag = context;
2974 return GDK_FILTER_TRANSLATE;
2977 static GdkFilterReturn
2978 xdnd_leave_filter (GdkXEvent *xev,
2982 XEvent *xevent = (XEvent *)xev;
2983 guint32 source_window = xevent->xclient.data.l[0];
2984 GdkDisplay *display;
2985 GdkX11Display *display_x11;
2987 if (!event->any.window ||
2988 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2989 return GDK_FILTER_CONTINUE; /* Not for us */
2992 g_message ("XdndLeave: source_window: %#x",
2995 display = GDK_WINDOW_DISPLAY (event->any.window);
2996 display_x11 = GDK_X11_DISPLAY (display);
2998 xdnd_precache_atoms (display);
3000 if ((display_x11->current_dest_drag != NULL) &&
3001 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3002 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
3004 event->dnd.type = GDK_DRAG_LEAVE;
3005 /* Pass ownership of context to the event */
3006 event->dnd.context = display_x11->current_dest_drag;
3007 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
3009 display_x11->current_dest_drag = NULL;
3011 return GDK_FILTER_TRANSLATE;
3014 return GDK_FILTER_REMOVE;
3017 static GdkFilterReturn
3018 xdnd_position_filter (GdkXEvent *xev,
3022 XEvent *xevent = (XEvent *)xev;
3023 guint32 source_window = xevent->xclient.data.l[0];
3024 gint16 x_root = xevent->xclient.data.l[2] >> 16;
3025 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
3026 guint32 time = xevent->xclient.data.l[3];
3027 Atom action = xevent->xclient.data.l[4];
3028 GdkDisplay *display;
3029 GdkX11Display *display_x11;
3030 GdkDragContext *context;
3031 GdkX11DragContext *context_x11;
3033 if (!event->any.window ||
3034 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3035 return GDK_FILTER_CONTINUE; /* Not for us */
3038 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
3039 source_window, x_root, y_root, time, action));
3041 display = GDK_WINDOW_DISPLAY (event->any.window);
3042 display_x11 = GDK_X11_DISPLAY (display);
3044 xdnd_precache_atoms (display);
3046 context = display_x11->current_dest_drag;
3048 if ((context != NULL) &&
3049 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3050 (GDK_WINDOW_XID (context->source_window) == source_window))
3052 context_x11 = GDK_X11_DRAG_CONTEXT (context);
3054 event->dnd.type = GDK_DRAG_MOTION;
3055 event->dnd.context = context;
3056 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3057 g_object_ref (context);
3059 event->dnd.time = time;
3061 context->suggested_action = xdnd_action_from_atom (display, action);
3063 if (!context_x11->xdnd_have_actions)
3064 context->actions = context->suggested_action;
3066 event->dnd.x_root = x_root;
3067 event->dnd.y_root = y_root;
3069 context_x11->last_x = x_root;
3070 context_x11->last_y = y_root;
3072 return GDK_FILTER_TRANSLATE;
3075 return GDK_FILTER_REMOVE;
3078 static GdkFilterReturn
3079 xdnd_drop_filter (GdkXEvent *xev,
3083 XEvent *xevent = (XEvent *)xev;
3084 guint32 source_window = xevent->xclient.data.l[0];
3085 guint32 time = xevent->xclient.data.l[2];
3086 GdkDisplay *display;
3087 GdkX11Display *display_x11;
3088 GdkDragContext *context;
3089 GdkX11DragContext *context_x11;
3091 if (!event->any.window ||
3092 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3093 return GDK_FILTER_CONTINUE; /* Not for us */
3096 g_message ("XdndDrop: source_window: %#x time: %d",
3097 source_window, time));
3099 display = GDK_WINDOW_DISPLAY (event->any.window);
3100 display_x11 = GDK_X11_DISPLAY (display);
3102 xdnd_precache_atoms (display);
3104 context = display_x11->current_dest_drag;
3106 if ((context != NULL) &&
3107 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3108 (GDK_WINDOW_XID (context->source_window) == source_window))
3110 context_x11 = GDK_X11_DRAG_CONTEXT (context);
3111 event->dnd.type = GDK_DROP_START;
3113 event->dnd.context = context;
3114 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3115 g_object_ref (context);
3117 event->dnd.time = time;
3118 event->dnd.x_root = context_x11->last_x;
3119 event->dnd.y_root = context_x11->last_y;
3121 gdk_x11_window_set_user_time (event->any.window, time);
3123 return GDK_FILTER_TRANSLATE;
3126 return GDK_FILTER_REMOVE;
3130 _gdk_x11_display_init_dnd (GdkDisplay *display)
3135 gdk_display_add_client_message_filter (
3137 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
3138 motif_dnd_filter, NULL);
3140 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3142 gdk_display_add_client_message_filter (
3144 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
3145 xdnd_filters[i].func, NULL);
3152 gdk_drag_do_leave (GdkX11DragContext *context_x11,
3155 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
3157 if (context->dest_window)
3159 switch (context->protocol)
3161 case GDK_DRAG_PROTO_MOTIF:
3162 motif_send_leave (context_x11, time);
3164 case GDK_DRAG_PROTO_XDND:
3165 xdnd_send_leave (context_x11);
3167 case GDK_DRAG_PROTO_ROOTWIN:
3168 case GDK_DRAG_PROTO_NONE:
3173 g_object_unref (context->dest_window);
3174 context->dest_window = NULL;
3179 _gdk_x11_window_drag_begin (GdkWindow *window,
3183 GdkDragContext *context;
3185 context = (GdkDragContext *) g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
3187 context->is_source = TRUE;
3188 context->source_window = window;
3189 g_object_ref (window);
3191 context->targets = g_list_copy (targets);
3192 precache_target_list (context);
3194 context->actions = 0;
3196 gdk_drag_context_set_device (context, device);
3202 _gdk_x11_display_get_drag_protocol (GdkDisplay *display,
3203 GdkNativeWindow xid,
3204 GdkDragProtocol *protocol,
3209 GdkNativeWindow retval;
3211 base_precache_atoms (display);
3213 /* Check for a local drag */
3214 window = gdk_x11_window_lookup_for_display (display, xid);
3215 if (window && gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3217 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3219 *protocol = GDK_DRAG_PROTO_XDND;
3221 xdnd_precache_atoms (display);
3222 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3225 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3227 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3228 GDK_NOTE (DND, g_message ("Entering root window\n"));
3232 else if ((retval = xdnd_check_dest (display, xid, version)))
3234 *protocol = GDK_DRAG_PROTO_XDND;
3235 xdnd_precache_atoms (display);
3236 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3239 else if ((retval = motif_check_dest (display, xid)))
3241 *protocol = GDK_DRAG_PROTO_MOTIF;
3242 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3247 /* Check if this is a root window */
3248 gboolean rootwin = FALSE;
3250 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3255 GDK_NOTE (DND, g_message ("Entering root window\n"));
3256 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3261 *protocol = GDK_DRAG_PROTO_NONE;
3263 return 0; /* a.k.a. None */
3266 static GdkWindowCache *
3267 drag_context_find_window_cache (GdkX11DragContext *context_x11,
3271 GdkWindowCache *cache;
3273 for (list = context_x11->window_caches; list; list = list->next)
3276 if (cache->screen == screen)
3280 cache = gdk_window_cache_get (screen);
3281 context_x11->window_caches = g_slist_prepend (context_x11->window_caches, cache);
3287 gdk_x11_drag_context_find_window (GdkDragContext *context,
3288 GdkWindow *drag_window,
3292 GdkDragProtocol *protocol)
3294 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3295 GdkWindowCache *window_cache;
3296 GdkDisplay *display;
3298 GdkWindow *dest_window;
3300 display = GDK_WINDOW_DISPLAY (context->source_window);
3302 window_cache = drag_context_find_window_cache (context_x11, screen);
3304 dest = get_client_window_at_coords (window_cache,
3305 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3306 GDK_WINDOW_XID (drag_window) : None,
3309 if (context_x11->dest_xid != dest)
3312 context_x11->dest_xid = dest;
3314 /* Check if new destination accepts drags, and which protocol */
3316 /* There is some ugliness here. We actually need to pass
3317 * _three_ pieces of information to drag_motion - dest_window,
3318 * protocol, and the XID of the unproxied window. The first
3319 * two are passed explicitely, the third implicitly through
3320 * protocol->dest_xid.
3322 recipient = _gdk_x11_display_get_drag_protocol (display,
3325 &context_x11->version);
3327 if (recipient != None)
3329 dest_window = gdk_x11_window_lookup_for_display (display, recipient);
3331 g_object_ref (dest_window);
3333 dest_window = gdk_x11_window_foreign_new_for_display (display, recipient);
3340 dest_window = context->dest_window;
3342 g_object_ref (dest_window);
3343 *protocol = context->protocol;
3350 gdk_x11_drag_context_drag_motion (GdkDragContext *context,
3351 GdkWindow *dest_window,
3352 GdkDragProtocol protocol,
3355 GdkDragAction suggested_action,
3356 GdkDragAction possible_actions,
3359 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3361 context_x11->old_actions = context->actions;
3362 context->actions = possible_actions;
3364 if (context_x11->old_actions != possible_actions)
3365 context_x11->xdnd_actions_set = FALSE;
3367 if (protocol == GDK_DRAG_PROTO_XDND && context_x11->version == 0)
3369 /* This ugly hack is necessary since GTK+ doesn't know about
3370 * the XDND protocol version, and in particular doesn't know
3371 * that gdk_drag_find_window_for_screen() has the side-effect
3372 * of setting context_x11->version, and therefore sometimes call
3373 * gdk_drag_motion() without a prior call to
3374 * gdk_drag_find_window_for_screen(). This happens, e.g.
3375 * when GTK+ is proxying DND events to embedded windows.
3379 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3381 xdnd_check_dest (display,
3382 GDK_WINDOW_XID (dest_window),
3383 &context_x11->version);
3387 /* When we have a Xdnd target, make sure our XdndActionList
3388 * matches the current actions;
3390 if (protocol == GDK_DRAG_PROTO_XDND && !context_x11->xdnd_actions_set)
3394 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3395 xdnd_set_actions (context_x11);
3396 else if (context->dest_window == dest_window)
3398 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3399 GdkDragContext *dest_context;
3401 dest_context = gdk_drag_context_find (display, FALSE,
3402 GDK_WINDOW_XID (context->source_window),
3403 GDK_WINDOW_XID (dest_window));
3407 dest_context->actions = context->actions;
3408 GDK_X11_DRAG_CONTEXT (dest_context)->xdnd_have_actions = TRUE;
3414 if (context->dest_window != dest_window)
3416 GdkEvent *temp_event;
3418 /* Send a leave to the last destination */
3419 gdk_drag_do_leave (context_x11, time);
3420 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
3422 /* Check if new destination accepts drags, and which protocol */
3426 context->dest_window = dest_window;
3427 context_x11->drop_xid = context_x11->dest_xid;
3428 g_object_ref (context->dest_window);
3429 context->protocol = protocol;
3433 case GDK_DRAG_PROTO_MOTIF:
3434 motif_send_enter (context_x11, time);
3437 case GDK_DRAG_PROTO_XDND:
3438 xdnd_send_enter (context_x11);
3441 case GDK_DRAG_PROTO_ROOTWIN:
3442 case GDK_DRAG_PROTO_NONE:
3446 context_x11->old_action = suggested_action;
3447 context->suggested_action = suggested_action;
3448 context_x11->old_actions = possible_actions;
3452 context->dest_window = NULL;
3453 context_x11->drop_xid = None;
3454 context->action = 0;
3457 /* Push a status event, to let the client know that
3460 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3461 temp_event->dnd.window = g_object_ref (context->source_window);
3462 /* We use this to signal a synthetic status. Perhaps
3463 * we should use an extra field...
3465 temp_event->dnd.send_event = TRUE;
3467 temp_event->dnd.context = g_object_ref (context);
3468 temp_event->dnd.time = time;
3469 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3471 gdk_event_put (temp_event);
3472 gdk_event_free (temp_event);
3476 context_x11->old_action = context->suggested_action;
3477 context->suggested_action = suggested_action;
3480 /* Send a drag-motion event */
3482 context_x11->last_x = x_root;
3483 context_x11->last_y = y_root;
3485 if (context->dest_window)
3487 if (context_x11->drag_status == GDK_DRAG_STATUS_DRAG)
3489 switch (context->protocol)
3491 case GDK_DRAG_PROTO_MOTIF:
3492 motif_send_motion (context_x11, x_root, y_root, suggested_action, time);
3495 case GDK_DRAG_PROTO_XDND:
3496 xdnd_send_motion (context_x11, x_root, y_root, suggested_action, time);
3499 case GDK_DRAG_PROTO_ROOTWIN:
3501 GdkEvent *temp_event;
3502 /* GTK+ traditionally has used application/x-rootwin-drop,
3503 * but the XDND spec specifies x-rootwindow-drop.
3505 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3506 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3508 if (g_list_find (context->targets,
3509 GDK_ATOM_TO_POINTER (target1)) ||
3510 g_list_find (context->targets,
3511 GDK_ATOM_TO_POINTER (target2)))
3512 context->action = context->suggested_action;
3514 context->action = 0;
3516 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3517 temp_event->dnd.window = g_object_ref (context->source_window);
3518 temp_event->dnd.send_event = FALSE;
3519 temp_event->dnd.context = g_object_ref (context);
3520 temp_event->dnd.time = time;
3521 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3523 gdk_event_put (temp_event);
3524 gdk_event_free (temp_event);
3527 case GDK_DRAG_PROTO_NONE:
3528 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3542 gdk_x11_drag_context_drag_abort (GdkDragContext *context,
3545 gdk_drag_do_leave (GDK_X11_DRAG_CONTEXT (context), time);
3549 gdk_x11_drag_context_drag_drop (GdkDragContext *context,
3552 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3554 if (context->dest_window)
3556 switch (context->protocol)
3558 case GDK_DRAG_PROTO_MOTIF:
3559 motif_send_leave (context_x11, time);
3560 motif_send_drop (context_x11, time);
3563 case GDK_DRAG_PROTO_XDND:
3564 xdnd_send_drop (context_x11, time);
3567 case GDK_DRAG_PROTO_ROOTWIN:
3568 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3570 case GDK_DRAG_PROTO_NONE:
3571 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3579 /* Destination side */
3582 gdk_x11_drag_context_drag_status (GdkDragContext *context,
3583 GdkDragAction action,
3586 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3588 GdkDisplay *display;
3590 display = GDK_WINDOW_DISPLAY (context->source_window);
3592 context->action = action;
3594 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3596 gboolean need_coords = FALSE;
3598 xev.xclient.type = ClientMessage;
3599 xev.xclient.message_type =
3600 gdk_x11_get_xatom_by_name_for_display (display,
3601 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3602 xev.xclient.format = 8;
3603 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3605 if (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3607 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3611 if ((action != 0) != (context_x11->old_action != 0))
3615 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3620 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3625 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3630 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3634 case GDK_ACTION_MOVE:
3635 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3637 case GDK_ACTION_COPY:
3638 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3640 case GDK_ACTION_LINK:
3641 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3644 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3649 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3651 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3653 MOTIF_XCLIENT_LONG (&xev, 1) = time_;
3657 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
3658 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
3661 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3663 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3664 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3666 if (!_gdk_x11_display_send_xevent (display,
3667 GDK_WINDOW_XID (context->source_window),
3670 g_message ("Send event to %lx failed",
3671 GDK_WINDOW_XID (context->source_window)));
3673 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3675 xev.xclient.type = ClientMessage;
3676 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3677 xev.xclient.format = 32;
3678 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3680 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3681 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3682 xev.xclient.data.l[2] = 0;
3683 xev.xclient.data.l[3] = 0;
3684 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3685 if (!xdnd_send_xevent (context_x11, context->source_window, FALSE, &xev))
3687 g_message ("Send event to %lx failed",
3688 GDK_WINDOW_XID (context->source_window)));
3691 context_x11->old_action = action;
3695 gdk_x11_drag_context_drop_reply (GdkDragContext *context,
3699 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3701 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3703 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3706 xev.xclient.type = ClientMessage;
3707 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3708 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3709 xev.xclient.format = 8;
3711 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3712 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3714 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3715 (XmDROP_SITE_VALID << 4) |
3716 (XmDROP_NOOP << 8) |
3719 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3720 (XmNO_DROP_SITE << 4) |
3721 (XmDROP_NOOP << 8) |
3722 (XmDROP_CANCEL << 12);
3723 MOTIF_XCLIENT_SHORT (&xev, 2) = context_x11->last_x;
3724 MOTIF_XCLIENT_SHORT (&xev, 3) = context_x11->last_y;
3725 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3726 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3727 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3729 _gdk_x11_display_send_xevent (display,
3730 GDK_WINDOW_XID (context->source_window),
3736 gdk_x11_drag_context_drop_finish (GdkDragContext *context,
3740 if (context->protocol == GDK_DRAG_PROTO_XDND)
3742 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3745 xev.xclient.type = ClientMessage;
3746 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3747 xev.xclient.format = 32;
3748 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3750 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3753 xev.xclient.data.l[1] = 1;
3754 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3759 xev.xclient.data.l[1] = 0;
3760 xev.xclient.data.l[2] = None;
3762 xev.xclient.data.l[3] = 0;
3763 xev.xclient.data.l[4] = 0;
3765 if (!xdnd_send_xevent (GDK_X11_DRAG_CONTEXT (context), context->source_window, FALSE, &xev))
3767 g_message ("Send event to %lx failed",
3768 GDK_WINDOW_XID (context->source_window)));
3773 _gdk_x11_window_register_dnd (GdkWindow *window)
3775 static const gulong xdnd_version = 5;
3776 MotifDragReceiverInfo info;
3777 Atom motif_drag_receiver_info_atom;
3778 GdkDisplay *display = gdk_window_get_display (window);
3780 g_return_if_fail (window != NULL);
3782 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
3785 base_precache_atoms (display);
3787 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3790 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3792 /* Set Motif drag receiver information property */
3793 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3794 "_MOTIF_DRAG_RECEIVER_INFO");
3795 /* initialize to zero to avoid writing uninitialized data to socket */
3796 memset(&info, 0, sizeof(info));
3797 info.byte_order = local_byte_order;
3798 info.protocol_version = 0;
3799 info.protocol_style = XmDRAG_DYNAMIC;
3800 info.proxy_window = None;
3801 info.num_drop_sites = 0;
3802 info.total_size = sizeof(info);
3804 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3805 GDK_WINDOW_XID (window),
3806 motif_drag_receiver_info_atom,
3807 motif_drag_receiver_info_atom,
3814 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3815 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3816 GDK_WINDOW_XID (window),
3817 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3818 XA_ATOM, 32, PropModeReplace,
3819 (guchar *)&xdnd_version, 1);
3823 gdk_x11_drag_context_get_selection (GdkDragContext *context)
3825 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3826 return gdk_x11_xatom_to_atom_for_display (GDK_WINDOW_DISPLAY (context->source_window),
3827 (GDK_X11_DRAG_CONTEXT (context))->motif_selection);
3828 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3829 return gdk_atom_intern_static_string ("XdndSelection");
3835 gdk_x11_drag_context_drop_status (GdkDragContext *context)
3837 return ! GDK_X11_DRAG_CONTEXT (context)->drop_failed;