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 */
87 guint version; /* Xdnd protocol version */
89 GSList *window_caches;
91 Window dest_xid; /* The last window we looked up */
92 Window drop_xid; /* The (non-proxied) window that is receiving drops */
93 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
94 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
95 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
96 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
97 guint drag_status : 4; /* current status of drag */
98 guint drop_failed : 1; /* Whether the drop was unsuccessful */
101 struct _GdkX11DragContextClass
103 GdkDragContextClass parent_class;
106 /* Forward declarations */
108 static GdkWindowCache *gdk_window_cache_get (GdkScreen *screen);
109 static GdkWindowCache *gdk_window_cache_ref (GdkWindowCache *cache);
110 static void gdk_window_cache_unref (GdkWindowCache *cache);
112 static void motif_read_target_table (GdkDisplay *display);
114 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
118 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
121 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
124 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
127 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
130 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
133 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
137 static void xdnd_manage_source_filter (GdkDragContext *context,
139 gboolean add_filter);
141 static GList *contexts;
142 static GSList *window_caches;
144 static const struct {
145 const char *atom_name;
148 { "_MOTIF_DRAG_AND_DROP_MESSAGE", motif_dnd_filter },
150 { "XdndEnter", xdnd_enter_filter },
151 { "XdndLeave", xdnd_leave_filter },
152 { "XdndPosition", xdnd_position_filter },
153 { "XdndStatus", xdnd_status_filter },
154 { "XdndFinished", xdnd_finished_filter },
155 { "XdndDrop", xdnd_drop_filter },
159 G_DEFINE_TYPE (GdkX11DragContext, gdk_x11_drag_context, GDK_TYPE_DRAG_CONTEXT)
162 gdk_x11_drag_context_init (GdkX11DragContext *context)
164 contexts = g_list_prepend (contexts, context);
167 static void gdk_x11_drag_context_finalize (GObject *object);
168 static GdkWindow * gdk_x11_drag_context_find_window (GdkDragContext *context,
169 GdkWindow *drag_window,
173 GdkDragProtocol *protocol);
174 static gboolean gdk_x11_drag_context_drag_motion (GdkDragContext *context,
175 GdkWindow *dest_window,
176 GdkDragProtocol protocol,
179 GdkDragAction suggested_action,
180 GdkDragAction possible_actions,
182 static void gdk_x11_drag_context_drag_status (GdkDragContext *context,
183 GdkDragAction action,
185 static void gdk_x11_drag_context_drag_abort (GdkDragContext *context,
187 static void gdk_x11_drag_context_drag_drop (GdkDragContext *context,
189 static void gdk_x11_drag_context_drop_reply (GdkDragContext *context,
192 static void gdk_x11_drag_context_drop_finish (GdkDragContext *context,
195 static gboolean gdk_x11_drag_context_drop_status (GdkDragContext *context);
196 static GdkAtom gdk_x11_drag_context_get_selection (GdkDragContext *context);
199 gdk_x11_drag_context_class_init (GdkX11DragContextClass *klass)
201 GObjectClass *object_class = G_OBJECT_CLASS (klass);
202 GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
204 object_class->finalize = gdk_x11_drag_context_finalize;
206 context_class->find_window = gdk_x11_drag_context_find_window;
207 context_class->drag_status = gdk_x11_drag_context_drag_status;
208 context_class->drag_motion = gdk_x11_drag_context_drag_motion;
209 context_class->drag_abort = gdk_x11_drag_context_drag_abort;
210 context_class->drag_drop = gdk_x11_drag_context_drag_drop;
211 context_class->drop_reply = gdk_x11_drag_context_drop_reply;
212 context_class->drop_finish = gdk_x11_drag_context_drop_finish;
213 context_class->drop_status = gdk_x11_drag_context_drop_status;
214 context_class->get_selection = gdk_x11_drag_context_get_selection;
218 gdk_x11_drag_context_finalize (GObject *object)
220 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
221 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (object);
223 if (context->source_window)
225 if ((context->protocol == GDK_DRAG_PROTO_XDND) && !context->is_source)
226 xdnd_manage_source_filter (context, context->source_window, FALSE);
229 g_slist_free_full (x11_context->window_caches, (GDestroyNotify)gdk_window_cache_unref);
230 x11_context->window_caches = NULL;
232 contexts = g_list_remove (contexts, context);
234 G_OBJECT_CLASS (gdk_x11_drag_context_parent_class)->finalize (object);
239 static GdkDragContext *
240 gdk_drag_context_find (GdkDisplay *display,
245 GList *tmp_list = contexts;
246 GdkDragContext *context;
247 GdkX11DragContext *context_x11;
248 Window context_dest_xid;
252 context = (GdkDragContext *)tmp_list->data;
253 context_x11 = (GdkX11DragContext *)context;
255 if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
256 (context->dest_window && gdk_window_get_display (context->dest_window) != display))
259 context_dest_xid = context->dest_window
260 ? (context_x11->drop_xid
261 ? context_x11->drop_xid
262 : GDK_WINDOW_XID (context->dest_window))
265 if ((!context->is_source == !is_source) &&
266 ((source_xid == None) || (context->source_window &&
267 (GDK_WINDOW_XID (context->source_window) == source_xid))) &&
268 ((dest_xid == None) || (context_dest_xid == dest_xid)))
271 tmp_list = tmp_list->next;
278 precache_target_list (GdkDragContext *context)
280 if (context->targets)
282 GPtrArray *targets = g_ptr_array_new ();
286 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
287 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
289 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
290 (const gchar **)targets->pdata,
293 for (i =0; i < targets->len; i++)
294 g_free (targets->pdata[i]);
296 g_ptr_array_free (targets, TRUE);
300 /* Utility functions */
303 free_cache_child (GdkCacheChild *child,
307 cairo_region_destroy (child->shape);
309 if (child->shape_selected && display)
311 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
313 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
320 gdk_window_cache_add (GdkWindowCache *cache,
328 GdkCacheChild *child = g_new (GdkCacheChild, 1);
333 child->width = width;
334 child->height = height;
335 child->mapped = mapped;
336 child->shape_selected = FALSE;
337 child->shape_valid = FALSE;
340 cache->children = g_list_prepend (cache->children, child);
341 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
345 static GdkFilterReturn
346 gdk_window_cache_shape_filter (GdkXEvent *xev,
350 XEvent *xevent = (XEvent *)xev;
351 GdkWindowCache *cache = data;
353 GdkX11Display *display = GDK_X11_DISPLAY (gdk_screen_get_display (cache->screen));
355 if (display->have_shapes &&
356 xevent->type == display->shape_event_base + ShapeNotify)
358 XShapeEvent *xse = (XShapeEvent*)xevent;
361 node = g_hash_table_lookup (cache->child_hash,
362 GUINT_TO_POINTER (xse->window));
365 GdkCacheChild *child = node->data;
366 child->shape_valid = FALSE;
369 cairo_region_destroy (child->shape);
374 return GDK_FILTER_REMOVE;
377 return GDK_FILTER_CONTINUE;
380 static GdkFilterReturn
381 gdk_window_cache_filter (GdkXEvent *xev,
385 XEvent *xevent = (XEvent *)xev;
386 GdkWindowCache *cache = data;
388 switch (xevent->type)
390 case CirculateNotify:
392 case ConfigureNotify:
394 XConfigureEvent *xce = &xevent->xconfigure;
397 node = g_hash_table_lookup (cache->child_hash,
398 GUINT_TO_POINTER (xce->window));
401 GdkCacheChild *child = node->data;
404 child->width = xce->width;
405 child->height = xce->height;
406 if (xce->above == None && (node->next))
408 GList *last = g_list_last (cache->children);
409 cache->children = g_list_remove_link (cache->children, node);
416 GList *above_node = g_hash_table_lookup (cache->child_hash,
417 GUINT_TO_POINTER (xce->above));
418 if (above_node && node->next != above_node)
420 /* Put the window above (before in the list) above_node */
421 cache->children = g_list_remove_link (cache->children, node);
422 node->prev = above_node->prev;
424 node->prev->next = node;
426 cache->children = node;
427 node->next = above_node;
428 above_node->prev = node;
436 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
438 if (!g_hash_table_lookup (cache->child_hash,
439 GUINT_TO_POINTER (xcwe->window)))
440 gdk_window_cache_add (cache, xcwe->window,
441 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
447 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
450 node = g_hash_table_lookup (cache->child_hash,
451 GUINT_TO_POINTER (xdwe->window));
454 GdkCacheChild *child = node->data;
456 g_hash_table_remove (cache->child_hash,
457 GUINT_TO_POINTER (xdwe->window));
458 cache->children = g_list_remove_link (cache->children, node);
459 /* window is destroyed, no need to disable ShapeNotify */
460 free_cache_child (child, NULL);
461 g_list_free_1 (node);
467 XMapEvent *xme = &xevent->xmap;
470 node = g_hash_table_lookup (cache->child_hash,
471 GUINT_TO_POINTER (xme->window));
474 GdkCacheChild *child = node->data;
475 child->mapped = TRUE;
483 XMapEvent *xume = &xevent->xmap;
486 node = g_hash_table_lookup (cache->child_hash,
487 GUINT_TO_POINTER (xume->window));
490 GdkCacheChild *child = node->data;
491 child->mapped = FALSE;
496 return GDK_FILTER_CONTINUE;
498 return GDK_FILTER_REMOVE;
501 static GdkWindowCache *
502 gdk_window_cache_new (GdkScreen *screen)
504 XWindowAttributes xwa;
505 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
506 GdkWindow *root_window = gdk_screen_get_root_window (screen);
507 GdkChildInfoX11 *children;
509 #ifdef HAVE_XCOMPOSITE
513 GdkWindowCache *result = g_new (GdkWindowCache, 1);
515 result->children = NULL;
516 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
517 result->screen = screen;
518 result->ref_count = 1;
520 XGetWindowAttributes (xdisplay, GDK_WINDOW_XID (root_window), &xwa);
521 result->old_event_mask = xwa.your_event_mask;
523 if (G_UNLIKELY (!GDK_X11_DISPLAY (GDK_X11_SCREEN (screen)->display)->trusted_client))
525 GList *toplevel_windows, *list;
527 gint x, y, width, height;
529 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
530 for (list = toplevel_windows; list; list = list->next)
532 window = GDK_WINDOW (list->data);
533 gdk_window_get_geometry (window, &x, &y, &width, &height);
534 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
536 gdk_window_is_visible (window));
538 g_list_free (toplevel_windows);
542 XSelectInput (xdisplay, GDK_WINDOW_XID (root_window),
543 result->old_event_mask | SubstructureNotifyMask);
544 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
545 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
547 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
548 GDK_WINDOW_XID (root_window),
550 &children, &nchildren))
553 for (i = 0; i < nchildren ; i++)
555 gdk_window_cache_add (result, children[i].window,
556 children[i].x, children[i].y, children[i].width, children[i].height,
557 children[i].is_mapped);
562 #ifdef HAVE_XCOMPOSITE
564 * Add the composite overlay window to the cache, as this can be a reasonable
565 * Xdnd proxy as well.
566 * This is only done when the screen is composited in order to avoid mapping
567 * the COW. We assume that the CM is using the COW (which is true for pretty
568 * much any CM currently in use).
570 if (gdk_screen_is_composited (screen))
572 cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
573 gdk_window_cache_add (result, cow, 0, 0, gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
574 XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
582 gdk_window_cache_destroy (GdkWindowCache *cache)
584 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
587 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
588 GDK_WINDOW_XID (root_window),
589 cache->old_event_mask);
590 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
591 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
593 display = gdk_screen_get_display (cache->screen);
595 gdk_x11_display_error_trap_push (display);
596 g_list_foreach (cache->children, (GFunc)free_cache_child, display);
597 gdk_x11_display_error_trap_pop_ignored (display);
599 g_list_free (cache->children);
600 g_hash_table_destroy (cache->child_hash);
605 static GdkWindowCache *
606 gdk_window_cache_ref (GdkWindowCache *cache)
608 cache->ref_count += 1;
614 gdk_window_cache_unref (GdkWindowCache *cache)
616 g_assert (cache->ref_count > 0);
618 cache->ref_count -= 1;
620 if (cache->ref_count == 0)
622 window_caches = g_slist_remove (window_caches, cache);
623 gdk_window_cache_destroy (cache);
628 gdk_window_cache_get (GdkScreen *screen)
631 GdkWindowCache *cache;
633 for (list = window_caches; list; list = list->next)
636 if (cache->screen == screen)
637 return gdk_window_cache_ref (cache);
640 cache = gdk_window_cache_new (screen);
642 window_caches = g_slist_prepend (window_caches, cache);
648 is_pointer_within_shape (GdkDisplay *display,
649 GdkCacheChild *child,
653 if (!child->shape_selected)
655 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
657 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
658 child->shape_selected = TRUE;
660 if (!child->shape_valid)
662 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
663 cairo_region_t *input_shape;
665 child->shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
666 child->xid, ShapeBounding);
668 input_shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
669 child->xid, ShapeInput);
670 if (child->shape && input_shape)
672 cairo_region_intersect (child->shape, input_shape);
673 cairo_region_destroy (input_shape);
675 else if (input_shape)
677 child->shape = input_shape;
681 child->shape_valid = TRUE;
684 return child->shape == NULL ||
685 cairo_region_contains_point (child->shape, x_pos, y_pos);
689 get_client_window_at_coords_recurse (GdkDisplay *display,
691 gboolean is_toplevel,
695 GdkChildInfoX11 *children;
696 unsigned int nchildren;
698 gboolean found_child = FALSE;
699 GdkChildInfoX11 child = { 0, };
700 gboolean has_wm_state = FALSE;
702 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
703 is_toplevel? &has_wm_state : NULL,
704 &children, &nchildren))
714 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
716 GdkChildInfoX11 *cur_child = &children[i];
718 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
719 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
720 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
733 if (child.has_wm_state)
736 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
743 get_client_window_at_coords (GdkWindowCache *cache,
749 Window retval = None;
752 display = gdk_screen_get_display (cache->screen);
754 gdk_x11_display_error_trap_push (display);
756 tmp_list = cache->children;
758 while (tmp_list && !retval)
760 GdkCacheChild *child = tmp_list->data;
762 if ((child->xid != ignore) && (child->mapped))
764 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
765 (y_root >= child->y) && (y_root < child->y + child->height))
767 if (!is_pointer_within_shape (display, child,
771 tmp_list = tmp_list->next;
775 retval = get_client_window_at_coords_recurse (display,
783 tmp_list = tmp_list->next;
786 gdk_x11_display_error_trap_pop_ignored (display);
791 return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
794 /*************************************************************
795 ***************************** MOTIF *************************
796 *************************************************************/
798 /* values used in the message type for Motif DND */
811 /* Values used to specify type of protocol to use */
815 XmDRAG_PREFER_PREREGISTER,
817 XmDRAG_PREFER_DYNAMIC,
819 XmDRAG_PREFER_RECEIVER
822 /* Operation codes */
830 /* Drop site status */
832 XmNO_DROP_SITE = 0x01,
833 XmDROP_SITE_INVALID = 0x02,
834 XmDROP_SITE_VALID = 0x03
837 /* completion status */
845 /* Byte swapping routines. The motif specification leaves it
846 * up to us to save a few bytes in the client messages
848 #if G_BYTE_ORDER == G_BIG_ENDIAN
849 static gchar local_byte_order = 'B';
851 static gchar local_byte_order = 'l';
854 #ifdef G_ENABLE_DEBUG
856 print_target_list (GList *targets)
860 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
861 g_message ("\t%s", name);
863 targets = targets->next;
866 #endif /* G_ENABLE_DEBUG */
869 card16_to_host (guint16 x, gchar byte_order)
871 if (byte_order == local_byte_order)
874 return (x << 8) | (x >> 8);
878 card32_to_host (guint32 x, gchar byte_order)
880 if (byte_order == local_byte_order)
883 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
886 /* Motif packs together fields of varying length into the
887 * client message. We can't rely on accessing these
888 * through data.s[], data.l[], etc, because on some architectures
889 * (i.e., Alpha) these won't be valid for format == 8.
892 #define MOTIF_XCLIENT_BYTE(xevent,i) \
893 (xevent)->xclient.data.b[i]
894 #define MOTIF_XCLIENT_SHORT(xevent,i) \
895 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
896 #define MOTIF_XCLIENT_LONG(xevent,i) \
897 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
899 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
900 #define MOTIF_UNPACK_SHORT(xevent,i) \
901 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
902 #define MOTIF_UNPACK_LONG(xevent,i) \
903 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
905 /***** Dest side ***********/
907 /* Property placed on source windows */
908 typedef struct _MotifDragInitiatorInfo
911 guint8 protocol_version;
912 guint16 targets_index;
913 guint32 selection_atom;
914 } MotifDragInitiatorInfo;
916 /* Header for target table on the drag window */
917 typedef struct _MotifTargetTableHeader
920 guchar protocol_version;
923 } MotifTargetTableHeader;
925 /* Property placed on target windows */
926 typedef struct _MotifDragReceiverInfo
929 guint8 protocol_version;
930 guint8 protocol_style;
932 guint32 proxy_window;
933 guint16 num_drop_sites;
936 } MotifDragReceiverInfo;
938 /* Target table handling */
940 static GdkFilterReturn
941 motif_drag_window_filter (GdkXEvent *xevent,
945 XEvent *xev = (XEvent *)xevent;
946 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
947 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
949 switch (xev->xany.type)
952 display_x11->motif_drag_window = None;
953 display_x11->motif_drag_gdk_window = NULL;
956 if (display_x11->motif_target_lists &&
957 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
958 motif_read_target_table (display);
961 return GDK_FILTER_REMOVE;
965 motif_lookup_drag_window (GdkDisplay *display,
966 Display *lookup_xdisplay)
968 Window retval = None;
969 gulong bytes_after, nitems;
974 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
975 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
977 XA_WINDOW, &type, &format, &nitems, &bytes_after,
980 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
982 retval = *(Window *)data;
984 g_message ("Found drag window %#lx\n", GDK_X11_DISPLAY (display)->motif_drag_window));
993 /* Finds the window where global Motif drag information is stored.
994 * If it doesn't exist and 'create' is TRUE, create one.
997 motif_find_drag_window (GdkDisplay *display,
1000 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1002 if (!display_x11->motif_drag_window)
1004 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
1005 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
1007 if (!display_x11->motif_drag_window && create)
1009 /* Create a persistant window. (Copied from LessTif) */
1010 Display *persistant_xdisplay;
1011 XSetWindowAttributes attr;
1012 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
1013 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
1015 XGrabServer (persistant_xdisplay);
1017 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
1019 if (!display_x11->motif_drag_window)
1021 attr.override_redirect = True;
1022 attr.event_mask = PropertyChangeMask;
1024 display_x11->motif_drag_window =
1025 XCreateWindow (persistant_xdisplay,
1026 RootWindow (persistant_xdisplay, 0),
1027 -100, -100, 10, 10, 0, 0,
1028 InputOnly, (Visual *)CopyFromParent,
1029 (CWOverrideRedirect | CWEventMask), &attr);
1032 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
1034 XChangeProperty (persistant_xdisplay,
1035 RootWindow (persistant_xdisplay, 0),
1036 motif_drag_window_atom, XA_WINDOW,
1037 32, PropModeReplace,
1038 (guchar *)&motif_drag_window_atom, 1);
1040 XUngrabServer (persistant_xdisplay);
1041 XCloseDisplay (persistant_xdisplay);
1044 /* There is a miniscule race condition here if the drag window
1045 * gets destroyed exactly now.
1047 if (display_x11->motif_drag_window)
1049 display_x11->motif_drag_gdk_window =
1050 gdk_x11_window_foreign_new_for_display (display, display_x11->motif_drag_window);
1051 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
1052 motif_drag_window_filter,
1057 return display_x11->motif_drag_window;
1061 motif_read_target_table (GdkDisplay *display)
1063 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1064 gulong bytes_after, nitems;
1069 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1071 if (display_x11->motif_target_lists)
1073 for (i=0; i<display_x11->motif_n_target_lists; i++)
1074 g_list_free (display_x11->motif_target_lists[i]);
1076 g_free (display_x11->motif_target_lists);
1077 display_x11->motif_target_lists = NULL;
1078 display_x11->motif_n_target_lists = 0;
1081 if (motif_find_drag_window (display, FALSE))
1084 MotifTargetTableHeader *header = NULL;
1085 guchar *target_bytes = NULL;
1087 gboolean success = FALSE;
1089 gdk_x11_display_error_trap_push (display);
1090 XGetWindowProperty (display_x11->xdisplay,
1091 display_x11->motif_drag_window,
1092 motif_drag_targets_atom,
1093 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1094 motif_drag_targets_atom,
1095 &type, &format, &nitems, &bytes_after,
1098 if (gdk_x11_display_error_trap_pop (display) ||
1099 (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1102 header = (MotifTargetTableHeader *)data;
1104 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1105 header->total_size = card32_to_host (header->total_size, header->byte_order);
1107 gdk_x11_display_error_trap_push (display);
1108 XGetWindowProperty (display_x11->xdisplay,
1109 display_x11->motif_drag_window,
1110 motif_drag_targets_atom,
1111 (sizeof(MotifTargetTableHeader)+3)/4,
1112 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1114 motif_drag_targets_atom, &type, &format, &nitems,
1115 &bytes_after, &target_bytes);
1117 if (gdk_x11_display_error_trap_pop (display) ||
1118 (format != 8) || (bytes_after != 0) ||
1119 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1122 display_x11->motif_n_target_lists = header->n_lists;
1123 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1126 for (i=0; i<header->n_lists; i++)
1131 if (p + sizeof(guint16) - target_bytes > nitems)
1134 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1136 /* We need to make a copy of the targets, since it may
1139 targets = g_new (guint32, n_targets);
1140 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1142 p += sizeof(guint16) + n_targets * sizeof(guint32);
1143 if (p - target_bytes > nitems)
1146 for (j=0; j<n_targets; j++)
1147 display_x11->motif_target_lists[i] =
1148 g_list_prepend (display_x11->motif_target_lists[i],
1149 GUINT_TO_POINTER (card32_to_host (targets[j],
1150 header->byte_order)));
1152 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1162 XFree (target_bytes);
1166 if (display_x11->motif_target_lists)
1168 g_free (display_x11->motif_target_lists);
1169 display_x11->motif_target_lists = NULL;
1170 display_x11->motif_n_target_lists = 0;
1172 g_warning ("Error reading Motif target table\n");
1178 targets_sort_func (gconstpointer a, gconstpointer b)
1180 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1181 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1184 /* Check if given (sorted) list is in the targets table */
1186 motif_target_table_check (GdkDisplay *display,
1189 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1190 GList *tmp_list1, *tmp_list2;
1193 for (i=0; i<display_x11->motif_n_target_lists; i++)
1195 tmp_list1 = display_x11->motif_target_lists[i];
1198 while (tmp_list1 && tmp_list2)
1200 if (tmp_list1->data != tmp_list2->data)
1203 tmp_list1 = tmp_list1->next;
1204 tmp_list2 = tmp_list2->next;
1206 if (!tmp_list1 && !tmp_list2) /* Found it */
1214 motif_add_to_target_table (GdkDisplay *display,
1215 GList *targets) /* targets is list of GdkAtom */
1217 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1218 GList *sorted = NULL;
1223 /* make a sorted copy of the list */
1226 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1227 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1228 targets = targets->next;
1231 /* First check if it is there already */
1233 if (display_x11->motif_target_lists)
1234 index = motif_target_table_check (display, sorted);
1236 /* We need to grab the server while doing this, to ensure
1242 /* We need to make sure that it exists _before_ we grab the
1243 * server, since we can't open a new connection after we
1246 motif_find_drag_window (display, TRUE);
1248 gdk_x11_display_grab (display);
1249 motif_read_target_table (display);
1251 /* Check again, in case it was added in the meantime */
1252 if (display_x11->motif_target_lists)
1253 index = motif_target_table_check (display, sorted);
1257 guint32 total_size = 0;
1261 MotifTargetTableHeader *header;
1263 if (!display_x11->motif_target_lists)
1265 display_x11->motif_target_lists = g_new (GList *, 1);
1266 display_x11->motif_n_target_lists = 1;
1270 display_x11->motif_n_target_lists++;
1271 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1272 sizeof(GList *) * display_x11->motif_n_target_lists);
1274 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1276 index = display_x11->motif_n_target_lists - 1;
1278 total_size = sizeof (MotifTargetTableHeader);
1279 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1280 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1282 data = g_malloc (total_size);
1284 header = (MotifTargetTableHeader *)data;
1285 p = data + sizeof(MotifTargetTableHeader);
1287 header->byte_order = local_byte_order;
1288 header->protocol_version = 0;
1289 header->n_lists = display_x11->motif_n_target_lists;
1290 header->total_size = total_size;
1292 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1294 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1295 guint32 *targets = g_new (guint32, n_targets);
1296 guint32 *p32 = targets;
1298 tmp_list = display_x11->motif_target_lists[i];
1301 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1303 tmp_list = tmp_list->next;
1308 p += sizeof(guint16);
1310 memcpy (p, targets, n_targets * sizeof(guint32));
1313 p += sizeof(guint32) * n_targets;
1317 XChangeProperty (display_x11->xdisplay,
1318 display_x11->motif_drag_window,
1319 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1320 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1324 gdk_x11_display_ungrab (display);
1327 g_list_free (sorted);
1331 /* Translate flags */
1334 motif_dnd_translate_flags (GdkX11DragContext *context_x11,
1337 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1338 guint recommended_op = flags & 0x000f;
1339 guint possible_ops = (flags & 0x0f0) >> 4;
1341 switch (recommended_op)
1344 context->suggested_action = GDK_ACTION_MOVE;
1347 context->suggested_action = GDK_ACTION_COPY;
1350 context->suggested_action = GDK_ACTION_LINK;
1353 context->suggested_action = GDK_ACTION_COPY;
1357 context->actions = 0;
1358 if (possible_ops & XmDROP_MOVE)
1359 context->actions |= GDK_ACTION_MOVE;
1360 if (possible_ops & XmDROP_COPY)
1361 context->actions |= GDK_ACTION_COPY;
1362 if (possible_ops & XmDROP_LINK)
1363 context->actions |= GDK_ACTION_LINK;
1367 motif_dnd_get_flags (GdkDragContext *context)
1371 switch (context->suggested_action)
1373 case GDK_ACTION_MOVE:
1374 flags = XmDROP_MOVE;
1376 case GDK_ACTION_COPY:
1377 flags = XmDROP_COPY;
1379 case GDK_ACTION_LINK:
1380 flags = XmDROP_LINK;
1383 flags = XmDROP_NOOP;
1387 if (context->actions & GDK_ACTION_MOVE)
1388 flags |= XmDROP_MOVE << 8;
1389 if (context->actions & GDK_ACTION_COPY)
1390 flags |= XmDROP_COPY << 8;
1391 if (context->actions & GDK_ACTION_LINK)
1392 flags |= XmDROP_LINK << 8;
1400 motif_set_targets (GdkX11DragContext *context_x11)
1402 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1403 MotifDragInitiatorInfo info;
1405 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1407 info.byte_order = local_byte_order;
1408 info.protocol_version = 0;
1409 info.targets_index = motif_add_to_target_table (display, context->targets);
1414 g_snprintf (buf, 20, "_GDK_SELECTION_%d", i);
1416 context_x11->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1417 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), context_x11->motif_selection))
1421 info.selection_atom = context_x11->motif_selection;
1423 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1424 GDK_WINDOW_XID (context->source_window),
1425 context_x11->motif_selection,
1426 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1428 (guchar *)&info, sizeof (info));
1430 context_x11->motif_targets_set = 1;
1434 motif_check_dest (GdkDisplay *display,
1437 gboolean retval = FALSE;
1439 MotifDragReceiverInfo *info;
1442 unsigned long nitems, after;
1443 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1445 gdk_x11_display_error_trap_push (display);
1446 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1447 motif_drag_receiver_info_atom,
1448 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1449 &type, &format, &nitems, &after,
1452 if (gdk_x11_display_error_trap_pop (display) == 0)
1456 info = (MotifDragReceiverInfo *)data;
1458 if ((format == 8) && (nitems == sizeof(*info)))
1460 if ((info->protocol_version == 0) &&
1461 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1462 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1463 (info->protocol_style == XmDRAG_DYNAMIC)))
1469 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1476 return retval ? win : None;
1480 motif_send_enter (GdkX11DragContext *context_x11,
1483 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1484 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1487 if (!G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
1488 return; /* Motif Dnd requires getting properties on the root window */
1490 xev.xclient.type = ClientMessage;
1491 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1492 xev.xclient.format = 8;
1493 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1495 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1496 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1497 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1498 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1499 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XID (context->source_window);
1501 if (!context_x11->motif_targets_set)
1502 motif_set_targets (context_x11);
1504 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1505 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1507 if (!_gdk_x11_display_send_xevent (display,
1508 GDK_WINDOW_XID (context->dest_window),
1511 g_message ("Send event to %lx failed",
1512 GDK_WINDOW_XID (context->dest_window)));
1516 motif_send_leave (GdkX11DragContext *context_x11,
1519 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1520 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1523 xev.xclient.type = ClientMessage;
1524 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1525 xev.xclient.format = 8;
1526 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1528 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1529 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1530 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1531 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1532 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1533 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1534 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1536 if (!_gdk_x11_display_send_xevent (display,
1537 GDK_WINDOW_XID (context->dest_window),
1540 g_message ("Send event to %lx failed",
1541 GDK_WINDOW_XID (context->dest_window)));
1545 motif_send_motion (GdkX11DragContext *context_x11,
1548 GdkDragAction action,
1551 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1552 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1556 xev.xclient.type = ClientMessage;
1557 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1558 xev.xclient.format = 8;
1559 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1561 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1562 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1563 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1564 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1565 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1567 if ((context->suggested_action != context_x11->old_action) ||
1568 (context->actions != context_x11->old_actions))
1570 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1572 /* context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1577 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1579 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1580 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1582 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1586 if (!_gdk_x11_display_send_xevent (display,
1587 GDK_WINDOW_XID (context->dest_window),
1590 g_message ("Send event to %lx failed",
1591 GDK_WINDOW_XID (context->dest_window)));
1597 motif_send_drop (GdkX11DragContext *context_x11,
1600 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1601 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1604 xev.xclient.type = ClientMessage;
1605 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1606 xev.xclient.format = 8;
1607 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1609 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1610 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1611 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1612 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1614 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
1615 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
1617 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1618 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XID (context->source_window);
1620 if (!_gdk_x11_display_send_xevent (display,
1621 GDK_WINDOW_XID (context->dest_window),
1624 g_message ("Send event to %lx failed",
1625 GDK_WINDOW_XID (context->dest_window)));
1631 motif_read_initiator_info (GdkDisplay *display,
1632 Window source_window,
1643 MotifDragInitiatorInfo *initiator_info;
1645 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1647 gdk_x11_display_error_trap_push (display);
1648 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1649 0, sizeof(*initiator_info), FALSE,
1650 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1651 &type, &format, &nitems, &bytes_after,
1654 if (gdk_x11_display_error_trap_pop (display) ||
1655 (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) ||
1658 g_warning ("Error reading initiator info\n");
1662 initiator_info = (MotifDragInitiatorInfo *)data;
1664 motif_read_target_table (display);
1666 initiator_info->targets_index =
1667 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1668 initiator_info->selection_atom =
1669 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1671 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1673 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1674 XFree (initiator_info);
1678 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1683 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1684 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1685 tmp_list = tmp_list->prev;
1688 #ifdef G_ENABLE_DEBUG
1689 if (_gdk_debug_flags & GDK_DEBUG_DND)
1690 print_target_list (*targets);
1691 #endif /* G_ENABLE_DEBUG */
1693 *selection = initiator_info->selection_atom;
1695 XFree (initiator_info);
1700 static GdkDragContext *
1701 motif_drag_context_new (GdkWindow *dest_window,
1703 guint32 source_window,
1706 GdkX11DragContext *context_x11;
1707 GdkDragContext *context;
1708 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
1709 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1711 /* FIXME, current_dest_drag really shouldn't be NULL'd
1712 * if we error below.
1714 if (display_x11->current_dest_drag != NULL)
1716 if (timestamp >= display_x11->current_dest_drag->start_time)
1718 g_object_unref (display_x11->current_dest_drag);
1719 display_x11->current_dest_drag = NULL;
1725 context_x11 = g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
1726 context = GDK_DRAG_CONTEXT (context_x11);
1728 context->protocol = GDK_DRAG_PROTO_MOTIF;
1729 context->is_source = FALSE;
1731 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
1732 if (!context->source_window)
1734 g_object_unref (context_x11);
1738 context->dest_window = dest_window;
1739 g_object_ref (dest_window);
1740 context->start_time = timestamp;
1742 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1746 &context_x11->motif_selection))
1748 g_object_unref (context_x11);
1756 * The MOTIF drag protocol has no real provisions for distinguishing
1757 * multiple simultaneous drops. If the sources grab the pointer
1758 * when doing drags, that shouldn't happen, in any case. If it
1759 * does, we can't do much except hope for the best.
1762 static GdkFilterReturn
1763 motif_top_level_enter (GdkEvent *event,
1766 guint32 source_window,
1769 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1770 GdkDragContext *new_context;
1772 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1773 flags, timestamp, source_window, atom));
1775 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1777 return GDK_FILTER_REMOVE;
1779 event->dnd.type = GDK_DRAG_ENTER;
1780 event->dnd.context = new_context;
1781 g_object_ref (new_context);
1783 display_x11->current_dest_drag = new_context;
1785 return GDK_FILTER_TRANSLATE;
1788 static GdkFilterReturn
1789 motif_top_level_leave (GdkEvent *event,
1793 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1795 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1798 if ((display_x11->current_dest_drag != NULL) &&
1799 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1800 (timestamp >= display_x11->current_dest_drag->start_time))
1802 event->dnd.type = GDK_DRAG_LEAVE;
1803 /* Pass ownership of context to the event */
1804 event->dnd.context = display_x11->current_dest_drag;
1806 display_x11->current_dest_drag = NULL;
1808 return GDK_FILTER_TRANSLATE;
1811 return GDK_FILTER_REMOVE;
1814 static GdkFilterReturn
1815 motif_motion (GdkEvent *event,
1821 GdkX11DragContext *context_x11;
1822 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1824 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1825 flags, timestamp, x_root, y_root));
1827 if ((display_x11->current_dest_drag != NULL) &&
1828 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1829 (timestamp >= display_x11->current_dest_drag->start_time))
1831 context_x11 = GDK_X11_DRAG_CONTEXT (display_x11->current_dest_drag);
1833 event->dnd.type = GDK_DRAG_MOTION;
1834 event->dnd.context = display_x11->current_dest_drag;
1835 g_object_ref (display_x11->current_dest_drag);
1837 event->dnd.time = timestamp;
1839 motif_dnd_translate_flags (context_x11, flags);
1841 event->dnd.x_root = x_root;
1842 event->dnd.y_root = y_root;
1844 context_x11->last_x = x_root;
1845 context_x11->last_y = y_root;
1847 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1849 return GDK_FILTER_TRANSLATE;
1852 return GDK_FILTER_REMOVE;
1855 static GdkFilterReturn
1856 motif_operation_changed (GdkEvent *event,
1860 GdkX11DragContext *context_x11;
1861 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1862 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1865 if ((display_x11->current_dest_drag != NULL) &&
1866 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1867 (timestamp >= display_x11->current_dest_drag->start_time))
1869 event->dnd.type = GDK_DRAG_MOTION;
1870 event->dnd.send_event = FALSE;
1871 event->dnd.context = display_x11->current_dest_drag;
1872 g_object_ref (display_x11->current_dest_drag);
1874 event->dnd.time = timestamp;
1875 context_x11 = GDK_X11_DRAG_CONTEXT (display_x11->current_dest_drag);
1877 motif_dnd_translate_flags (context_x11, flags);
1879 event->dnd.x_root = context_x11->last_x;
1880 event->dnd.y_root = context_x11->last_y;
1882 context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1884 return GDK_FILTER_TRANSLATE;
1887 return GDK_FILTER_REMOVE;
1890 static GdkFilterReturn
1891 motif_drop_start (GdkEvent *event,
1894 guint32 source_window,
1899 GdkDragContext *new_context;
1900 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1902 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1903 flags, timestamp, x_root, y_root, source_window, atom));
1905 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1907 return GDK_FILTER_REMOVE;
1909 motif_dnd_translate_flags (GDK_X11_DRAG_CONTEXT (new_context), flags);
1911 event->dnd.type = GDK_DROP_START;
1912 event->dnd.context = new_context;
1913 event->dnd.time = timestamp;
1914 event->dnd.x_root = x_root;
1915 event->dnd.y_root = y_root;
1917 gdk_x11_window_set_user_time (event->any.window, timestamp);
1919 g_object_ref (new_context);
1920 display_x11->current_dest_drag = new_context;
1922 return GDK_FILTER_TRANSLATE;
1925 static GdkFilterReturn
1926 motif_drag_status (GdkEvent *event,
1930 GdkDragContext *context;
1931 GdkDisplay *display;
1934 g_message ("Motif status message: flags %x", flags));
1936 display = gdk_window_get_display (event->any.window);
1938 return GDK_FILTER_REMOVE;
1940 context = gdk_drag_context_find (display, TRUE, GDK_WINDOW_XID (event->any.window), None);
1944 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
1945 if ((context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1946 (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1947 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
1949 event->dnd.type = GDK_DRAG_STATUS;
1950 event->dnd.send_event = FALSE;
1951 event->dnd.context = context;
1952 g_object_ref (context);
1954 event->dnd.time = timestamp;
1956 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1958 switch (flags & 0x000f)
1961 context->action = 0;
1964 context->action = GDK_ACTION_MOVE;
1967 context->action = GDK_ACTION_COPY;
1970 context->action = GDK_ACTION_LINK;
1975 context->action = 0;
1977 return GDK_FILTER_TRANSLATE;
1979 return GDK_FILTER_REMOVE;
1982 static GdkFilterReturn
1983 motif_dnd_filter (GdkXEvent *xev,
1987 XEvent *xevent = (XEvent *)xev;
1992 guint32 source_window;
1994 gint16 x_root, y_root;
1997 if (!event->any.window ||
1998 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1999 return GDK_FILTER_CONTINUE; /* Not for us */
2001 /* First read some fields common to all Motif DND messages */
2002 reason = MOTIF_UNPACK_BYTE (xevent, 0);
2003 flags = MOTIF_UNPACK_SHORT (xevent, 1);
2004 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
2006 is_reply = ((reason & 0x80) != 0);
2008 switch (reason & 0x7f)
2010 case XmTOP_LEVEL_ENTER:
2011 source_window = MOTIF_UNPACK_LONG (xevent, 2);
2012 atom = MOTIF_UNPACK_LONG (xevent, 3);
2013 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
2014 case XmTOP_LEVEL_LEAVE:
2015 return motif_top_level_leave (event, flags, timestamp);
2018 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2019 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2022 return motif_motion (event, flags, timestamp, x_root, y_root);
2024 return motif_drag_status (event, flags, timestamp);
2026 case XmDROP_SITE_ENTER:
2027 return motif_drag_status (event, flags, timestamp);
2029 case XmDROP_SITE_LEAVE:
2030 return motif_drag_status (event,
2031 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
2034 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2035 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2036 atom = MOTIF_UNPACK_LONG (xevent, 3);
2037 source_window = MOTIF_UNPACK_LONG (xevent, 4);
2040 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
2043 case XmOPERATION_CHANGED:
2045 return motif_operation_changed (event, flags, timestamp);
2047 return motif_drag_status (event, flags, timestamp);
2050 /* To the best of my knowledge, these next two messages are
2051 * not part of the protocol, though they are defined in
2055 case XmDRAG_DROP_FINISH:
2059 return GDK_FILTER_REMOVE;
2062 /*************************************************************
2063 ***************************** XDND **************************
2064 *************************************************************/
2066 /* Utility functions */
2071 GdkDragAction action;
2072 } xdnd_actions_table[] = {
2073 { "XdndActionCopy", None, GDK_ACTION_COPY },
2074 { "XdndActionMove", None, GDK_ACTION_MOVE },
2075 { "XdndActionLink", None, GDK_ACTION_LINK },
2076 { "XdndActionAsk", None, GDK_ACTION_ASK },
2077 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2080 static const gint xdnd_n_actions = G_N_ELEMENTS (xdnd_actions_table);
2081 static gboolean xdnd_actions_initialized = FALSE;
2084 xdnd_initialize_actions (void)
2088 xdnd_actions_initialized = TRUE;
2089 for (i = 0; i < xdnd_n_actions; i++)
2090 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2093 static GdkDragAction
2094 xdnd_action_from_atom (GdkDisplay *display,
2103 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2105 if (!xdnd_actions_initialized)
2106 xdnd_initialize_actions();
2108 for (i = 0; i < xdnd_n_actions; i++)
2109 if (atom == xdnd_actions_table[i].atom)
2110 return xdnd_actions_table[i].action;
2116 xdnd_action_to_atom (GdkDisplay *display,
2117 GdkDragAction action)
2121 if (!xdnd_actions_initialized)
2122 xdnd_initialize_actions();
2124 for (i = 0; i < xdnd_n_actions; i++)
2125 if (action == xdnd_actions_table[i].action)
2126 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2133 static GdkFilterReturn
2134 xdnd_status_filter (GdkXEvent *xev,
2138 GdkDisplay *display;
2139 XEvent *xevent = (XEvent *)xev;
2140 guint32 dest_window = xevent->xclient.data.l[0];
2141 guint32 flags = xevent->xclient.data.l[1];
2142 Atom action = xevent->xclient.data.l[4];
2143 GdkDragContext *context;
2145 if (!event->any.window ||
2146 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2147 return GDK_FILTER_CONTINUE; /* Not for us */
2150 g_message ("XdndStatus: dest_window: %#x action: %ld",
2151 dest_window, action));
2153 display = gdk_window_get_display (event->any.window);
2154 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2158 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
2159 if (context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2160 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2162 event->dnd.send_event = FALSE;
2163 event->dnd.type = GDK_DRAG_STATUS;
2164 event->dnd.context = context;
2165 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2166 g_object_ref (context);
2168 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2169 if (!(action != 0) != !(flags & 1))
2172 g_warning ("Received status event with flags not corresponding to action!\n"));
2176 context->action = xdnd_action_from_atom (display, action);
2178 return GDK_FILTER_TRANSLATE;
2181 return GDK_FILTER_REMOVE;
2184 static GdkFilterReturn
2185 xdnd_finished_filter (GdkXEvent *xev,
2189 GdkDisplay *display;
2190 XEvent *xevent = (XEvent *)xev;
2191 guint32 dest_window = xevent->xclient.data.l[0];
2192 GdkDragContext *context;
2193 GdkX11DragContext *context_x11;
2195 if (!event->any.window ||
2196 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2197 return GDK_FILTER_CONTINUE; /* Not for us */
2200 g_message ("XdndFinished: dest_window: %#x", dest_window));
2202 display = gdk_window_get_display (event->any.window);
2203 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2207 context_x11 = GDK_X11_DRAG_CONTEXT (context);
2208 if (context_x11->version == 5)
2209 context_x11->drop_failed = xevent->xclient.data.l[1] == 0;
2211 event->dnd.type = GDK_DROP_FINISHED;
2212 event->dnd.context = context;
2213 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2214 g_object_ref (context);
2216 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2218 return GDK_FILTER_TRANSLATE;
2221 return GDK_FILTER_REMOVE;
2225 xdnd_set_targets (GdkX11DragContext *context_x11)
2227 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2229 GList *tmp_list = context->targets;
2231 gint n_atoms = g_list_length (context->targets);
2232 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2234 atomlist = g_new (Atom, n_atoms);
2238 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2239 tmp_list = tmp_list->next;
2243 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2244 GDK_WINDOW_XID (context->source_window),
2245 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2246 XA_ATOM, 32, PropModeReplace,
2247 (guchar *)atomlist, n_atoms);
2251 context_x11->xdnd_targets_set = 1;
2255 xdnd_set_actions (GdkX11DragContext *context_x11)
2257 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2262 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2264 if (!xdnd_actions_initialized)
2265 xdnd_initialize_actions();
2267 actions = context->actions;
2269 for (i = 0; i < xdnd_n_actions; i++)
2271 if (actions & xdnd_actions_table[i].action)
2273 actions &= ~xdnd_actions_table[i].action;
2278 atomlist = g_new (Atom, n_atoms);
2280 actions = context->actions;
2282 for (i = 0; i < xdnd_n_actions; i++)
2284 if (actions & xdnd_actions_table[i].action)
2286 actions &= ~xdnd_actions_table[i].action;
2287 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2292 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2293 GDK_WINDOW_XID (context->source_window),
2294 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2295 XA_ATOM, 32, PropModeReplace,
2296 (guchar *)atomlist, n_atoms);
2300 context_x11->xdnd_actions_set = TRUE;
2301 context_x11->xdnd_actions = context->actions;
2305 send_client_message_async_cb (Window window,
2309 GdkDragContext *context = data;
2311 g_message ("Got async callback for #%lx, success = %d",
2314 /* On failure, we immediately continue with the protocol
2315 * so we don't end up blocking for a timeout
2318 context->dest_window &&
2319 window == GDK_WINDOW_XID (context->dest_window))
2321 GdkEvent *temp_event;
2322 GdkX11DragContext *context_x11 = data;
2324 g_object_unref (context->dest_window);
2325 context->dest_window = NULL;
2326 context->action = 0;
2328 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2330 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2331 temp_event->dnd.window = g_object_ref (context->source_window);
2332 temp_event->dnd.send_event = TRUE;
2333 temp_event->dnd.context = g_object_ref (context);
2334 temp_event->dnd.time = GDK_CURRENT_TIME;
2335 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2337 gdk_event_put (temp_event);
2339 gdk_event_free (temp_event);
2342 g_object_unref (context);
2347 gdk_drag_context_get_display (GdkDragContext *context)
2349 if (context->source_window)
2350 return GDK_WINDOW_DISPLAY (context->source_window);
2351 else if (context->dest_window)
2352 return GDK_WINDOW_DISPLAY (context->dest_window);
2354 g_assert_not_reached ();
2359 send_client_message_async (GdkDragContext *context,
2363 XClientMessageEvent *event_send)
2365 GdkDisplay *display = gdk_drag_context_get_display (context);
2367 g_object_ref (context);
2369 _gdk_x11_send_client_message_async (display, window,
2370 propagate, event_mask, event_send,
2371 send_client_message_async_cb, context);
2375 xdnd_send_xevent (GdkX11DragContext *context_x11,
2380 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2381 GdkDisplay *display = gdk_drag_context_get_display (context);
2385 g_assert (event_send->xany.type == ClientMessage);
2387 /* We short-circuit messages to ourselves */
2388 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2392 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2394 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2395 event_send->xclient.message_type)
2397 GdkEvent *temp_event;
2399 temp_event = gdk_event_new (GDK_NOTHING);
2400 temp_event->any.window = g_object_ref (window);
2402 if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
2404 gdk_event_put (temp_event);
2405 gdk_event_free (temp_event);
2413 xwindow = GDK_WINDOW_XID (window);
2415 if (_gdk_x11_display_is_root_window (display, xwindow))
2416 event_mask = ButtonPressMask;
2420 send_client_message_async (context, xwindow, propagate, event_mask,
2421 &event_send->xclient);
2427 xdnd_send_enter (GdkX11DragContext *context_x11)
2429 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2430 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->dest_window);
2433 xev.xclient.type = ClientMessage;
2434 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2435 xev.xclient.format = 32;
2436 xev.xclient.window = context_x11->drop_xid
2437 ? context_x11->drop_xid
2438 : GDK_WINDOW_XID (context->dest_window);
2439 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2440 xev.xclient.data.l[1] = (context_x11->version << 24); /* version */
2441 xev.xclient.data.l[2] = 0;
2442 xev.xclient.data.l[3] = 0;
2443 xev.xclient.data.l[4] = 0;
2446 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2447 GDK_WINDOW_XID (context->source_window), context_x11->version));
2448 if (g_list_length (context->targets) > 3)
2450 if (!context_x11->xdnd_targets_set)
2451 xdnd_set_targets (context_x11);
2452 xev.xclient.data.l[1] |= 1;
2456 GList *tmp_list = context->targets;
2461 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2462 GDK_POINTER_TO_ATOM (tmp_list->data));
2463 tmp_list = tmp_list->next;
2468 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2471 g_message ("Send event to %lx failed",
2472 GDK_WINDOW_XID (context->dest_window)));
2473 g_object_unref (context->dest_window);
2474 context->dest_window = NULL;
2479 xdnd_send_leave (GdkX11DragContext *context_x11)
2481 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2482 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2485 xev.xclient.type = ClientMessage;
2486 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2487 xev.xclient.format = 32;
2488 xev.xclient.window = context_x11->drop_xid
2489 ? context_x11->drop_xid
2490 : GDK_WINDOW_XID (context->dest_window);
2491 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2492 xev.xclient.data.l[1] = 0;
2493 xev.xclient.data.l[2] = 0;
2494 xev.xclient.data.l[3] = 0;
2495 xev.xclient.data.l[4] = 0;
2497 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2500 g_message ("Send event to %lx failed",
2501 GDK_WINDOW_XID (context->dest_window)));
2502 g_object_unref (context->dest_window);
2503 context->dest_window = NULL;
2508 xdnd_send_drop (GdkX11DragContext *context_x11,
2511 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2512 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2515 xev.xclient.type = ClientMessage;
2516 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2517 xev.xclient.format = 32;
2518 xev.xclient.window = context_x11->drop_xid
2519 ? context_x11->drop_xid
2520 : GDK_WINDOW_XID (context->dest_window);
2521 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2522 xev.xclient.data.l[1] = 0;
2523 xev.xclient.data.l[2] = time;
2524 xev.xclient.data.l[3] = 0;
2525 xev.xclient.data.l[4] = 0;
2527 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2530 g_message ("Send event to %lx failed",
2531 GDK_WINDOW_XID (context->dest_window)));
2532 g_object_unref (context->dest_window);
2533 context->dest_window = NULL;
2538 xdnd_send_motion (GdkX11DragContext *context_x11,
2541 GdkDragAction action,
2544 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2545 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2548 xev.xclient.type = ClientMessage;
2549 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2550 xev.xclient.format = 32;
2551 xev.xclient.window = context_x11->drop_xid
2552 ? context_x11->drop_xid
2553 : GDK_WINDOW_XID (context->dest_window);
2554 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2555 xev.xclient.data.l[1] = 0;
2556 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2557 xev.xclient.data.l[3] = time;
2558 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2560 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2563 g_message ("Send event to %lx failed",
2564 GDK_WINDOW_XID (context->dest_window)));
2565 g_object_unref (context->dest_window);
2566 context->dest_window = NULL;
2568 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2572 xdnd_check_dest (GdkDisplay *display,
2574 guint *xdnd_version)
2576 gboolean retval = FALSE;
2579 unsigned long nitems, after;
2584 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2585 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2589 gdk_x11_display_error_trap_push (display);
2590 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2592 1, False, AnyPropertyType,
2593 &type, &format, &nitems, &after,
2598 proxy_data = (Window *)data;
2600 if ((format == 32) && (nitems == 1))
2602 proxy = *proxy_data;
2606 g_warning ("Invalid XdndProxy "
2607 "property on window %ld\n", win));
2612 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2614 1, False, AnyPropertyType,
2615 &type, &format, &nitems, &after,
2616 &data) == Success) &&
2619 version = (Atom *)data;
2621 if ((format == 32) && (nitems == 1))
2626 *xdnd_version = *version;
2630 g_warning ("Invalid XdndAware "
2631 "property on window %ld\n", win));
2637 gdk_x11_display_error_trap_pop_ignored (display);
2639 return retval ? (proxy ? proxy : win) : None;
2645 xdnd_read_actions (GdkX11DragContext *context_x11)
2647 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2648 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2651 gulong nitems, after;
2656 context_x11->xdnd_have_actions = FALSE;
2658 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2660 /* Get the XdndActionList, if set */
2662 gdk_x11_display_error_trap_push (display);
2663 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2664 GDK_WINDOW_XID (context->source_window),
2665 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2667 False, XA_ATOM, &type, &format, &nitems,
2668 &after, &data) == Success &&
2671 atoms = (Atom *)data;
2673 context->actions = 0;
2675 for (i = 0; i < nitems; i++)
2676 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2678 context_x11->xdnd_have_actions = TRUE;
2680 #ifdef G_ENABLE_DEBUG
2681 if (_gdk_debug_flags & GDK_DEBUG_DND)
2683 GString *action_str = g_string_new (NULL);
2684 if (context->actions & GDK_ACTION_MOVE)
2685 g_string_append(action_str, "MOVE ");
2686 if (context->actions & GDK_ACTION_COPY)
2687 g_string_append(action_str, "COPY ");
2688 if (context->actions & GDK_ACTION_LINK)
2689 g_string_append(action_str, "LINK ");
2690 if (context->actions & GDK_ACTION_ASK)
2691 g_string_append(action_str, "ASK ");
2693 g_message("Xdnd actions = %s", action_str->str);
2694 g_string_free (action_str, TRUE);
2696 #endif /* G_ENABLE_DEBUG */
2703 gdk_x11_display_error_trap_pop_ignored (display);
2709 GdkDragContext *source_context;
2711 source_context = gdk_drag_context_find (display, TRUE,
2712 GDK_WINDOW_XID (context->source_window),
2713 GDK_WINDOW_XID (context->dest_window));
2717 context->actions = source_context->actions;
2718 context_x11->xdnd_have_actions = TRUE;
2723 /* We have to make sure that the XdndActionList we keep internally
2724 * is up to date with the XdndActionList on the source window
2725 * because we get no notification, because Xdnd wasn't meant
2726 * to continually send actions. So we select on PropertyChangeMask
2727 * and add this filter.
2729 static GdkFilterReturn
2730 xdnd_source_window_filter (GdkXEvent *xev,
2734 XEvent *xevent = (XEvent *)xev;
2735 GdkX11DragContext *context_x11 = cb_data;
2736 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2738 if ((xevent->xany.type == PropertyNotify) &&
2739 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2741 xdnd_read_actions (context_x11);
2743 return GDK_FILTER_REMOVE;
2746 return GDK_FILTER_CONTINUE;
2750 xdnd_manage_source_filter (GdkDragContext *context,
2752 gboolean add_filter)
2754 if (!GDK_WINDOW_DESTROYED (window) &&
2755 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2757 gdk_x11_display_error_trap_push (GDK_WINDOW_DISPLAY (window));
2761 gdk_window_set_events (window,
2762 gdk_window_get_events (window) |
2763 GDK_PROPERTY_CHANGE_MASK);
2764 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2768 gdk_window_remove_filter (window,
2769 xdnd_source_window_filter,
2771 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2772 * but we might want it for other reasons. (Like
2773 * INCR selection transactions).
2777 gdk_x11_display_error_trap_pop_ignored (GDK_WINDOW_DISPLAY (window));
2782 base_precache_atoms (GdkDisplay *display)
2784 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2786 if (!display_x11->base_dnd_atoms_precached)
2788 static const char *const precache_atoms[] = {
2789 "ENLIGHTENMENT_DESKTOP",
2793 "_MOTIF_DRAG_RECEIVER_INFO"
2796 _gdk_x11_precache_atoms (display,
2797 precache_atoms, G_N_ELEMENTS (precache_atoms));
2799 display_x11->base_dnd_atoms_precached = TRUE;
2804 xdnd_precache_atoms (GdkDisplay *display)
2806 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2808 if (!display_x11->xdnd_atoms_precached)
2810 static const gchar *const precache_atoms[] = {
2816 "XdndActionPrivate",
2827 _gdk_x11_precache_atoms (display,
2828 precache_atoms, G_N_ELEMENTS (precache_atoms));
2830 display_x11->xdnd_atoms_precached = TRUE;
2834 static GdkFilterReturn
2835 xdnd_enter_filter (GdkXEvent *xev,
2839 GdkDeviceManager *device_manager;
2840 GdkDisplay *display;
2841 GdkX11Display *display_x11;
2842 XEvent *xevent = (XEvent *)xev;
2843 GdkDragContext *context;
2844 GdkX11DragContext *context_x11;
2848 gulong nitems, after;
2851 guint32 source_window;
2855 if (!event->any.window ||
2856 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2857 return GDK_FILTER_CONTINUE; /* Not for us */
2859 source_window = xevent->xclient.data.l[0];
2860 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2861 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2863 display = GDK_WINDOW_DISPLAY (event->any.window);
2864 display_x11 = GDK_X11_DISPLAY (display);
2866 xdnd_precache_atoms (display);
2869 g_message ("XdndEnter: source_window: %#x, version: %#x",
2870 source_window, version));
2874 /* Old source ignore */
2875 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2876 return GDK_FILTER_REMOVE;
2879 if (display_x11->current_dest_drag != NULL)
2881 g_object_unref (display_x11->current_dest_drag);
2882 display_x11->current_dest_drag = NULL;
2885 context_x11 = (GdkX11DragContext *)g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
2886 context = (GdkDragContext *)context_x11;
2888 context->protocol = GDK_DRAG_PROTO_XDND;
2889 context_x11->version = version;
2891 /* FIXME: Should extend DnD protocol to have device info */
2892 device_manager = gdk_display_get_device_manager (display);
2893 gdk_drag_context_set_device (context, gdk_device_manager_get_client_pointer (device_manager));
2895 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
2896 if (!context->source_window)
2898 g_object_unref (context);
2899 return GDK_FILTER_REMOVE;
2901 context->dest_window = event->any.window;
2902 g_object_ref (context->dest_window);
2904 context->targets = NULL;
2907 gdk_x11_display_error_trap_push (display);
2908 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2910 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2912 False, XA_ATOM, &type, &format, &nitems,
2915 if (gdk_x11_display_error_trap_pop (display) || (format != 32) || (type != XA_ATOM))
2917 g_object_unref (context);
2922 return GDK_FILTER_REMOVE;
2925 atoms = (Atom *)data;
2927 for (i = 0; i < nitems; i++)
2929 g_list_append (context->targets,
2930 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2937 for (i = 0; i < 3; i++)
2938 if (xevent->xclient.data.l[2 + i])
2940 g_list_append (context->targets,
2941 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2942 xevent->xclient.data.l[2 + i])));
2945 #ifdef G_ENABLE_DEBUG
2946 if (_gdk_debug_flags & GDK_DEBUG_DND)
2947 print_target_list (context->targets);
2948 #endif /* G_ENABLE_DEBUG */
2950 xdnd_manage_source_filter (context, context->source_window, TRUE);
2951 xdnd_read_actions (context_x11);
2953 event->dnd.type = GDK_DRAG_ENTER;
2954 event->dnd.context = context;
2955 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2956 g_object_ref (context);
2958 display_x11->current_dest_drag = context;
2960 return GDK_FILTER_TRANSLATE;
2963 static GdkFilterReturn
2964 xdnd_leave_filter (GdkXEvent *xev,
2968 XEvent *xevent = (XEvent *)xev;
2969 guint32 source_window = xevent->xclient.data.l[0];
2970 GdkDisplay *display;
2971 GdkX11Display *display_x11;
2973 if (!event->any.window ||
2974 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2975 return GDK_FILTER_CONTINUE; /* Not for us */
2978 g_message ("XdndLeave: source_window: %#x",
2981 display = GDK_WINDOW_DISPLAY (event->any.window);
2982 display_x11 = GDK_X11_DISPLAY (display);
2984 xdnd_precache_atoms (display);
2986 if ((display_x11->current_dest_drag != NULL) &&
2987 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2988 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
2990 event->dnd.type = GDK_DRAG_LEAVE;
2991 /* Pass ownership of context to the event */
2992 event->dnd.context = display_x11->current_dest_drag;
2993 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
2995 display_x11->current_dest_drag = NULL;
2997 return GDK_FILTER_TRANSLATE;
3000 return GDK_FILTER_REMOVE;
3003 static GdkFilterReturn
3004 xdnd_position_filter (GdkXEvent *xev,
3008 XEvent *xevent = (XEvent *)xev;
3009 guint32 source_window = xevent->xclient.data.l[0];
3010 gint16 x_root = xevent->xclient.data.l[2] >> 16;
3011 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
3012 guint32 time = xevent->xclient.data.l[3];
3013 Atom action = xevent->xclient.data.l[4];
3014 GdkDisplay *display;
3015 GdkX11Display *display_x11;
3016 GdkDragContext *context;
3017 GdkX11DragContext *context_x11;
3019 if (!event->any.window ||
3020 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3021 return GDK_FILTER_CONTINUE; /* Not for us */
3024 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
3025 source_window, x_root, y_root, time, action));
3027 display = GDK_WINDOW_DISPLAY (event->any.window);
3028 display_x11 = GDK_X11_DISPLAY (display);
3030 xdnd_precache_atoms (display);
3032 context = display_x11->current_dest_drag;
3034 if ((context != NULL) &&
3035 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3036 (GDK_WINDOW_XID (context->source_window) == source_window))
3038 context_x11 = GDK_X11_DRAG_CONTEXT (context);
3040 event->dnd.type = GDK_DRAG_MOTION;
3041 event->dnd.context = context;
3042 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3043 g_object_ref (context);
3045 event->dnd.time = time;
3047 context->suggested_action = xdnd_action_from_atom (display, action);
3049 if (!context_x11->xdnd_have_actions)
3050 context->actions = context->suggested_action;
3052 event->dnd.x_root = x_root;
3053 event->dnd.y_root = y_root;
3055 context_x11->last_x = x_root;
3056 context_x11->last_y = y_root;
3058 return GDK_FILTER_TRANSLATE;
3061 return GDK_FILTER_REMOVE;
3064 static GdkFilterReturn
3065 xdnd_drop_filter (GdkXEvent *xev,
3069 XEvent *xevent = (XEvent *)xev;
3070 guint32 source_window = xevent->xclient.data.l[0];
3071 guint32 time = xevent->xclient.data.l[2];
3072 GdkDisplay *display;
3073 GdkX11Display *display_x11;
3074 GdkDragContext *context;
3075 GdkX11DragContext *context_x11;
3077 if (!event->any.window ||
3078 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3079 return GDK_FILTER_CONTINUE; /* Not for us */
3082 g_message ("XdndDrop: source_window: %#x time: %d",
3083 source_window, time));
3085 display = GDK_WINDOW_DISPLAY (event->any.window);
3086 display_x11 = GDK_X11_DISPLAY (display);
3088 xdnd_precache_atoms (display);
3090 context = display_x11->current_dest_drag;
3092 if ((context != NULL) &&
3093 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3094 (GDK_WINDOW_XID (context->source_window) == source_window))
3096 context_x11 = GDK_X11_DRAG_CONTEXT (context);
3097 event->dnd.type = GDK_DROP_START;
3099 event->dnd.context = context;
3100 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3101 g_object_ref (context);
3103 event->dnd.time = time;
3104 event->dnd.x_root = context_x11->last_x;
3105 event->dnd.y_root = context_x11->last_y;
3107 gdk_x11_window_set_user_time (event->any.window, time);
3109 return GDK_FILTER_TRANSLATE;
3112 return GDK_FILTER_REMOVE;
3116 _gdk_x11_dnd_filter (GdkXEvent *xev,
3120 XEvent *xevent = (XEvent *) xev;
3121 GdkDisplay *display;
3124 if (!GDK_IS_X11_WINDOW (event->any.window))
3125 return GDK_FILTER_CONTINUE;
3127 if (xevent->type != ClientMessage)
3128 return GDK_FILTER_CONTINUE;
3130 display = GDK_WINDOW_DISPLAY (event->any.window);
3132 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3134 if (xevent->xclient.message_type != gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name))
3137 return xdnd_filters[i].func (xev, event, data);
3140 return GDK_FILTER_CONTINUE;
3146 gdk_drag_do_leave (GdkX11DragContext *context_x11,
3149 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
3151 if (context->dest_window)
3153 switch (context->protocol)
3155 case GDK_DRAG_PROTO_MOTIF:
3156 motif_send_leave (context_x11, time);
3158 case GDK_DRAG_PROTO_XDND:
3159 xdnd_send_leave (context_x11);
3161 case GDK_DRAG_PROTO_ROOTWIN:
3162 case GDK_DRAG_PROTO_NONE:
3167 g_object_unref (context->dest_window);
3168 context->dest_window = NULL;
3173 _gdk_x11_window_drag_begin (GdkWindow *window,
3177 GdkDragContext *context;
3179 context = (GdkDragContext *) g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
3181 context->is_source = TRUE;
3182 context->source_window = window;
3183 g_object_ref (window);
3185 context->targets = g_list_copy (targets);
3186 precache_target_list (context);
3188 context->actions = 0;
3190 gdk_drag_context_set_device (context, device);
3196 _gdk_x11_display_get_drag_protocol (GdkDisplay *display,
3198 GdkDragProtocol *protocol,
3205 base_precache_atoms (display);
3207 /* Check for a local drag */
3208 window = gdk_x11_window_lookup_for_display (display, xid);
3209 if (window && gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3211 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3213 *protocol = GDK_DRAG_PROTO_XDND;
3215 xdnd_precache_atoms (display);
3216 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", (guint) xid));
3219 else if (_gdk_x11_display_is_root_window (display, xid))
3221 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3222 GDK_NOTE (DND, g_message ("Entering root window\n"));
3226 else if ((retval = xdnd_check_dest (display, xid, version)))
3228 *protocol = GDK_DRAG_PROTO_XDND;
3229 xdnd_precache_atoms (display);
3230 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", (guint) xid));
3233 else if ((retval = motif_check_dest (display, xid)))
3235 *protocol = GDK_DRAG_PROTO_MOTIF;
3236 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", (guint) xid));
3241 /* Check if this is a root window */
3242 gboolean rootwin = FALSE;
3244 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3249 GDK_NOTE (DND, g_message ("Entering root window\n"));
3250 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3255 *protocol = GDK_DRAG_PROTO_NONE;
3257 return 0; /* a.k.a. None */
3260 static GdkWindowCache *
3261 drag_context_find_window_cache (GdkX11DragContext *context_x11,
3265 GdkWindowCache *cache;
3267 for (list = context_x11->window_caches; list; list = list->next)
3270 if (cache->screen == screen)
3274 cache = gdk_window_cache_get (screen);
3275 context_x11->window_caches = g_slist_prepend (context_x11->window_caches, cache);
3281 gdk_x11_drag_context_find_window (GdkDragContext *context,
3282 GdkWindow *drag_window,
3286 GdkDragProtocol *protocol)
3288 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3289 GdkWindowCache *window_cache;
3290 GdkDisplay *display;
3292 GdkWindow *dest_window;
3294 display = GDK_WINDOW_DISPLAY (context->source_window);
3296 window_cache = drag_context_find_window_cache (context_x11, screen);
3298 dest = get_client_window_at_coords (window_cache,
3299 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3300 GDK_WINDOW_XID (drag_window) : None,
3303 if (context_x11->dest_xid != dest)
3306 context_x11->dest_xid = dest;
3308 /* Check if new destination accepts drags, and which protocol */
3310 /* There is some ugliness here. We actually need to pass
3311 * _three_ pieces of information to drag_motion - dest_window,
3312 * protocol, and the XID of the unproxied window. The first
3313 * two are passed explicitely, the third implicitly through
3314 * protocol->dest_xid.
3316 recipient = _gdk_x11_display_get_drag_protocol (display,
3319 &context_x11->version);
3321 if (recipient != None)
3322 dest_window = gdk_x11_window_foreign_new_for_display (display, recipient);
3328 dest_window = context->dest_window;
3330 g_object_ref (dest_window);
3331 *protocol = context->protocol;
3338 gdk_x11_drag_context_drag_motion (GdkDragContext *context,
3339 GdkWindow *dest_window,
3340 GdkDragProtocol protocol,
3343 GdkDragAction suggested_action,
3344 GdkDragAction possible_actions,
3347 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3349 context_x11->old_actions = context->actions;
3350 context->actions = possible_actions;
3352 if (context_x11->old_actions != possible_actions)
3353 context_x11->xdnd_actions_set = FALSE;
3355 if (protocol == GDK_DRAG_PROTO_XDND && context_x11->version == 0)
3357 /* This ugly hack is necessary since GTK+ doesn't know about
3358 * the XDND protocol version, and in particular doesn't know
3359 * that gdk_drag_find_window_for_screen() has the side-effect
3360 * of setting context_x11->version, and therefore sometimes call
3361 * gdk_drag_motion() without a prior call to
3362 * gdk_drag_find_window_for_screen(). This happens, e.g.
3363 * when GTK+ is proxying DND events to embedded windows.
3367 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3369 xdnd_check_dest (display,
3370 GDK_WINDOW_XID (dest_window),
3371 &context_x11->version);
3375 /* When we have a Xdnd target, make sure our XdndActionList
3376 * matches the current actions;
3378 if (protocol == GDK_DRAG_PROTO_XDND && !context_x11->xdnd_actions_set)
3382 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3383 xdnd_set_actions (context_x11);
3384 else if (context->dest_window == dest_window)
3386 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3387 GdkDragContext *dest_context;
3389 dest_context = gdk_drag_context_find (display, FALSE,
3390 GDK_WINDOW_XID (context->source_window),
3391 GDK_WINDOW_XID (dest_window));
3395 dest_context->actions = context->actions;
3396 GDK_X11_DRAG_CONTEXT (dest_context)->xdnd_have_actions = TRUE;
3402 if (context->dest_window != dest_window)
3404 GdkEvent *temp_event;
3406 /* Send a leave to the last destination */
3407 gdk_drag_do_leave (context_x11, time);
3408 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
3410 /* Check if new destination accepts drags, and which protocol */
3414 context->dest_window = dest_window;
3415 context_x11->drop_xid = context_x11->dest_xid;
3416 g_object_ref (context->dest_window);
3417 context->protocol = protocol;
3421 case GDK_DRAG_PROTO_MOTIF:
3422 motif_send_enter (context_x11, time);
3425 case GDK_DRAG_PROTO_XDND:
3426 xdnd_send_enter (context_x11);
3429 case GDK_DRAG_PROTO_ROOTWIN:
3430 case GDK_DRAG_PROTO_NONE:
3434 context_x11->old_action = suggested_action;
3435 context->suggested_action = suggested_action;
3436 context_x11->old_actions = possible_actions;
3440 context->dest_window = NULL;
3441 context_x11->drop_xid = None;
3442 context->action = 0;
3445 /* Push a status event, to let the client know that
3448 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3449 temp_event->dnd.window = g_object_ref (context->source_window);
3450 /* We use this to signal a synthetic status. Perhaps
3451 * we should use an extra field...
3453 temp_event->dnd.send_event = TRUE;
3455 temp_event->dnd.context = g_object_ref (context);
3456 temp_event->dnd.time = time;
3457 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3459 gdk_event_put (temp_event);
3460 gdk_event_free (temp_event);
3464 context_x11->old_action = context->suggested_action;
3465 context->suggested_action = suggested_action;
3468 /* Send a drag-motion event */
3470 context_x11->last_x = x_root;
3471 context_x11->last_y = y_root;
3473 if (context->dest_window)
3475 if (context_x11->drag_status == GDK_DRAG_STATUS_DRAG)
3477 switch (context->protocol)
3479 case GDK_DRAG_PROTO_MOTIF:
3480 motif_send_motion (context_x11, x_root, y_root, suggested_action, time);
3483 case GDK_DRAG_PROTO_XDND:
3484 xdnd_send_motion (context_x11, x_root, y_root, suggested_action, time);
3487 case GDK_DRAG_PROTO_ROOTWIN:
3489 GdkEvent *temp_event;
3490 /* GTK+ traditionally has used application/x-rootwin-drop,
3491 * but the XDND spec specifies x-rootwindow-drop.
3493 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3494 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3496 if (g_list_find (context->targets,
3497 GDK_ATOM_TO_POINTER (target1)) ||
3498 g_list_find (context->targets,
3499 GDK_ATOM_TO_POINTER (target2)))
3500 context->action = context->suggested_action;
3502 context->action = 0;
3504 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3505 temp_event->dnd.window = g_object_ref (context->source_window);
3506 temp_event->dnd.send_event = FALSE;
3507 temp_event->dnd.context = g_object_ref (context);
3508 temp_event->dnd.time = time;
3509 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3511 gdk_event_put (temp_event);
3512 gdk_event_free (temp_event);
3515 case GDK_DRAG_PROTO_NONE:
3516 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3530 gdk_x11_drag_context_drag_abort (GdkDragContext *context,
3533 gdk_drag_do_leave (GDK_X11_DRAG_CONTEXT (context), time);
3537 gdk_x11_drag_context_drag_drop (GdkDragContext *context,
3540 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3542 if (context->dest_window)
3544 switch (context->protocol)
3546 case GDK_DRAG_PROTO_MOTIF:
3547 motif_send_leave (context_x11, time);
3548 motif_send_drop (context_x11, time);
3551 case GDK_DRAG_PROTO_XDND:
3552 xdnd_send_drop (context_x11, time);
3555 case GDK_DRAG_PROTO_ROOTWIN:
3556 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3558 case GDK_DRAG_PROTO_NONE:
3559 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3567 /* Destination side */
3570 gdk_x11_drag_context_drag_status (GdkDragContext *context,
3571 GdkDragAction action,
3574 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3576 GdkDisplay *display;
3578 display = GDK_WINDOW_DISPLAY (context->source_window);
3580 context->action = action;
3582 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3584 gboolean need_coords = FALSE;
3586 xev.xclient.type = ClientMessage;
3587 xev.xclient.message_type =
3588 gdk_x11_get_xatom_by_name_for_display (display,
3589 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3590 xev.xclient.format = 8;
3591 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3593 if (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3595 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3599 if ((action != 0) != (context_x11->old_action != 0))
3603 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3608 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3613 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3618 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3622 case GDK_ACTION_MOVE:
3623 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3625 case GDK_ACTION_COPY:
3626 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3628 case GDK_ACTION_LINK:
3629 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3632 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3637 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3639 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3641 MOTIF_XCLIENT_LONG (&xev, 1) = time_;
3645 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
3646 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
3649 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3651 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3652 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3654 if (!_gdk_x11_display_send_xevent (display,
3655 GDK_WINDOW_XID (context->source_window),
3658 g_message ("Send event to %lx failed",
3659 GDK_WINDOW_XID (context->source_window)));
3661 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3663 xev.xclient.type = ClientMessage;
3664 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3665 xev.xclient.format = 32;
3666 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3668 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3669 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3670 xev.xclient.data.l[2] = 0;
3671 xev.xclient.data.l[3] = 0;
3672 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3673 if (!xdnd_send_xevent (context_x11, context->source_window, FALSE, &xev))
3675 g_message ("Send event to %lx failed",
3676 GDK_WINDOW_XID (context->source_window)));
3679 context_x11->old_action = action;
3683 gdk_x11_drag_context_drop_reply (GdkDragContext *context,
3687 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3689 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3691 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3694 xev.xclient.type = ClientMessage;
3695 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3696 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3697 xev.xclient.format = 8;
3699 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3700 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3702 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3703 (XmDROP_SITE_VALID << 4) |
3704 (XmDROP_NOOP << 8) |
3707 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3708 (XmNO_DROP_SITE << 4) |
3709 (XmDROP_NOOP << 8) |
3710 (XmDROP_CANCEL << 12);
3711 MOTIF_XCLIENT_SHORT (&xev, 2) = context_x11->last_x;
3712 MOTIF_XCLIENT_SHORT (&xev, 3) = context_x11->last_y;
3713 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3714 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3715 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3717 _gdk_x11_display_send_xevent (display,
3718 GDK_WINDOW_XID (context->source_window),
3724 gdk_x11_drag_context_drop_finish (GdkDragContext *context,
3728 if (context->protocol == GDK_DRAG_PROTO_XDND)
3730 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3733 xev.xclient.type = ClientMessage;
3734 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3735 xev.xclient.format = 32;
3736 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3738 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3741 xev.xclient.data.l[1] = 1;
3742 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3747 xev.xclient.data.l[1] = 0;
3748 xev.xclient.data.l[2] = None;
3750 xev.xclient.data.l[3] = 0;
3751 xev.xclient.data.l[4] = 0;
3753 if (!xdnd_send_xevent (GDK_X11_DRAG_CONTEXT (context), context->source_window, FALSE, &xev))
3755 g_message ("Send event to %lx failed",
3756 GDK_WINDOW_XID (context->source_window)));
3761 _gdk_x11_window_register_dnd (GdkWindow *window)
3763 static const gulong xdnd_version = 5;
3764 MotifDragReceiverInfo info;
3765 Atom motif_drag_receiver_info_atom;
3766 GdkDisplay *display = gdk_window_get_display (window);
3768 g_return_if_fail (window != NULL);
3770 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
3773 base_precache_atoms (display);
3775 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3778 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3780 /* Set Motif drag receiver information property */
3781 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3782 "_MOTIF_DRAG_RECEIVER_INFO");
3783 /* initialize to zero to avoid writing uninitialized data to socket */
3784 memset(&info, 0, sizeof(info));
3785 info.byte_order = local_byte_order;
3786 info.protocol_version = 0;
3787 info.protocol_style = XmDRAG_DYNAMIC;
3788 info.proxy_window = None;
3789 info.num_drop_sites = 0;
3790 info.total_size = sizeof(info);
3792 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3793 GDK_WINDOW_XID (window),
3794 motif_drag_receiver_info_atom,
3795 motif_drag_receiver_info_atom,
3802 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3803 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3804 GDK_WINDOW_XID (window),
3805 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3806 XA_ATOM, 32, PropModeReplace,
3807 (guchar *)&xdnd_version, 1);
3811 gdk_x11_drag_context_get_selection (GdkDragContext *context)
3813 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3814 return gdk_x11_xatom_to_atom_for_display (GDK_WINDOW_DISPLAY (context->source_window),
3815 (GDK_X11_DRAG_CONTEXT (context))->motif_selection);
3816 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3817 return gdk_atom_intern_static_string ("XdndSelection");
3823 gdk_x11_drag_context_drop_status (GdkDragContext *context)
3825 return ! GDK_X11_DRAG_CONTEXT (context)->drop_failed;