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;
666 if (gdk_display_supports_shapes (display))
667 child->shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
668 child->xid, ShapeBounding);
671 if (gdk_display_supports_input_shapes (display))
672 input_shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
673 child->xid, ShapeInput);
675 if (child->shape && input_shape)
677 cairo_region_intersect (child->shape, input_shape);
678 cairo_region_destroy (input_shape);
680 else if (input_shape)
682 child->shape = input_shape;
686 child->shape_valid = TRUE;
689 return child->shape == NULL ||
690 cairo_region_contains_point (child->shape, x_pos, y_pos);
694 get_client_window_at_coords_recurse (GdkDisplay *display,
696 gboolean is_toplevel,
700 GdkChildInfoX11 *children;
701 unsigned int nchildren;
703 gboolean found_child = FALSE;
704 GdkChildInfoX11 child = { 0, };
705 gboolean has_wm_state = FALSE;
707 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
708 is_toplevel? &has_wm_state : NULL,
709 &children, &nchildren))
719 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
721 GdkChildInfoX11 *cur_child = &children[i];
723 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
724 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
725 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
738 if (child.has_wm_state)
741 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
748 get_client_window_at_coords (GdkWindowCache *cache,
754 Window retval = None;
757 display = gdk_screen_get_display (cache->screen);
759 gdk_x11_display_error_trap_push (display);
761 tmp_list = cache->children;
763 while (tmp_list && !retval)
765 GdkCacheChild *child = tmp_list->data;
767 if ((child->xid != ignore) && (child->mapped))
769 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
770 (y_root >= child->y) && (y_root < child->y + child->height))
772 if (!is_pointer_within_shape (display, child,
776 tmp_list = tmp_list->next;
780 retval = get_client_window_at_coords_recurse (display,
788 tmp_list = tmp_list->next;
791 gdk_x11_display_error_trap_pop_ignored (display);
796 return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
799 /*************************************************************
800 ***************************** MOTIF *************************
801 *************************************************************/
803 /* values used in the message type for Motif DND */
816 /* Values used to specify type of protocol to use */
820 XmDRAG_PREFER_PREREGISTER,
822 XmDRAG_PREFER_DYNAMIC,
824 XmDRAG_PREFER_RECEIVER
827 /* Operation codes */
835 /* Drop site status */
837 XmNO_DROP_SITE = 0x01,
838 XmDROP_SITE_INVALID = 0x02,
839 XmDROP_SITE_VALID = 0x03
842 /* completion status */
850 /* Byte swapping routines. The motif specification leaves it
851 * up to us to save a few bytes in the client messages
853 #if G_BYTE_ORDER == G_BIG_ENDIAN
854 static gchar local_byte_order = 'B';
856 static gchar local_byte_order = 'l';
859 #ifdef G_ENABLE_DEBUG
861 print_target_list (GList *targets)
865 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
866 g_message ("\t%s", name);
868 targets = targets->next;
871 #endif /* G_ENABLE_DEBUG */
874 card16_to_host (guint16 x, gchar byte_order)
876 if (byte_order == local_byte_order)
879 return (x << 8) | (x >> 8);
883 card32_to_host (guint32 x, gchar byte_order)
885 if (byte_order == local_byte_order)
888 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
891 /* Motif packs together fields of varying length into the
892 * client message. We can't rely on accessing these
893 * through data.s[], data.l[], etc, because on some architectures
894 * (i.e., Alpha) these won't be valid for format == 8.
897 #define MOTIF_XCLIENT_BYTE(xevent,i) \
898 (xevent)->xclient.data.b[i]
899 #define MOTIF_XCLIENT_SHORT(xevent,i) \
900 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
901 #define MOTIF_XCLIENT_LONG(xevent,i) \
902 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
904 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
905 #define MOTIF_UNPACK_SHORT(xevent,i) \
906 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
907 #define MOTIF_UNPACK_LONG(xevent,i) \
908 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
910 /***** Dest side ***********/
912 /* Property placed on source windows */
913 typedef struct _MotifDragInitiatorInfo
916 guint8 protocol_version;
917 guint16 targets_index;
918 guint32 selection_atom;
919 } MotifDragInitiatorInfo;
921 /* Header for target table on the drag window */
922 typedef struct _MotifTargetTableHeader
925 guchar protocol_version;
928 } MotifTargetTableHeader;
930 /* Property placed on target windows */
931 typedef struct _MotifDragReceiverInfo
934 guint8 protocol_version;
935 guint8 protocol_style;
937 guint32 proxy_window;
938 guint16 num_drop_sites;
941 } MotifDragReceiverInfo;
943 /* Target table handling */
945 static GdkFilterReturn
946 motif_drag_window_filter (GdkXEvent *xevent,
950 XEvent *xev = (XEvent *)xevent;
951 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
952 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
954 switch (xev->xany.type)
957 display_x11->motif_drag_window = None;
958 display_x11->motif_drag_gdk_window = NULL;
961 if (display_x11->motif_target_lists &&
962 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
963 motif_read_target_table (display);
966 return GDK_FILTER_REMOVE;
970 motif_lookup_drag_window (GdkDisplay *display,
971 Display *lookup_xdisplay)
973 Window retval = None;
974 gulong bytes_after, nitems;
979 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
980 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
982 XA_WINDOW, &type, &format, &nitems, &bytes_after,
985 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
987 retval = *(Window *)data;
989 g_message ("Found drag window %#lx\n", GDK_X11_DISPLAY (display)->motif_drag_window));
998 /* Finds the window where global Motif drag information is stored.
999 * If it doesn't exist and 'create' is TRUE, create one.
1002 motif_find_drag_window (GdkDisplay *display,
1005 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1007 if (!display_x11->motif_drag_window)
1009 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
1010 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
1012 if (!display_x11->motif_drag_window && create)
1014 /* Create a persistant window. (Copied from LessTif) */
1015 Display *persistant_xdisplay;
1016 XSetWindowAttributes attr;
1017 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
1018 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
1020 XGrabServer (persistant_xdisplay);
1022 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
1024 if (!display_x11->motif_drag_window)
1026 attr.override_redirect = True;
1027 attr.event_mask = PropertyChangeMask;
1029 display_x11->motif_drag_window =
1030 XCreateWindow (persistant_xdisplay,
1031 RootWindow (persistant_xdisplay, 0),
1032 -100, -100, 10, 10, 0, 0,
1033 InputOnly, (Visual *)CopyFromParent,
1034 (CWOverrideRedirect | CWEventMask), &attr);
1037 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
1039 XChangeProperty (persistant_xdisplay,
1040 RootWindow (persistant_xdisplay, 0),
1041 motif_drag_window_atom, XA_WINDOW,
1042 32, PropModeReplace,
1043 (guchar *)&motif_drag_window_atom, 1);
1045 XUngrabServer (persistant_xdisplay);
1046 XCloseDisplay (persistant_xdisplay);
1049 /* There is a miniscule race condition here if the drag window
1050 * gets destroyed exactly now.
1052 if (display_x11->motif_drag_window)
1054 display_x11->motif_drag_gdk_window =
1055 gdk_x11_window_foreign_new_for_display (display, display_x11->motif_drag_window);
1056 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
1057 motif_drag_window_filter,
1062 return display_x11->motif_drag_window;
1066 motif_read_target_table (GdkDisplay *display)
1068 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1069 gulong bytes_after, nitems;
1074 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1076 if (display_x11->motif_target_lists)
1078 for (i=0; i<display_x11->motif_n_target_lists; i++)
1079 g_list_free (display_x11->motif_target_lists[i]);
1081 g_free (display_x11->motif_target_lists);
1082 display_x11->motif_target_lists = NULL;
1083 display_x11->motif_n_target_lists = 0;
1086 if (motif_find_drag_window (display, FALSE))
1089 MotifTargetTableHeader *header = NULL;
1090 guchar *target_bytes = NULL;
1092 gboolean success = FALSE;
1094 gdk_x11_display_error_trap_push (display);
1095 XGetWindowProperty (display_x11->xdisplay,
1096 display_x11->motif_drag_window,
1097 motif_drag_targets_atom,
1098 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1099 motif_drag_targets_atom,
1100 &type, &format, &nitems, &bytes_after,
1103 if (gdk_x11_display_error_trap_pop (display) ||
1104 (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1107 header = (MotifTargetTableHeader *)data;
1109 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1110 header->total_size = card32_to_host (header->total_size, header->byte_order);
1112 gdk_x11_display_error_trap_push (display);
1113 XGetWindowProperty (display_x11->xdisplay,
1114 display_x11->motif_drag_window,
1115 motif_drag_targets_atom,
1116 (sizeof(MotifTargetTableHeader)+3)/4,
1117 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1119 motif_drag_targets_atom, &type, &format, &nitems,
1120 &bytes_after, &target_bytes);
1122 if (gdk_x11_display_error_trap_pop (display) ||
1123 (format != 8) || (bytes_after != 0) ||
1124 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1127 display_x11->motif_n_target_lists = header->n_lists;
1128 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1131 for (i=0; i<header->n_lists; i++)
1136 if (p + sizeof(guint16) - target_bytes > nitems)
1139 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1141 /* We need to make a copy of the targets, since it may
1144 targets = g_new (guint32, n_targets);
1145 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1147 p += sizeof(guint16) + n_targets * sizeof(guint32);
1148 if (p - target_bytes > nitems)
1151 for (j=0; j<n_targets; j++)
1152 display_x11->motif_target_lists[i] =
1153 g_list_prepend (display_x11->motif_target_lists[i],
1154 GUINT_TO_POINTER (card32_to_host (targets[j],
1155 header->byte_order)));
1157 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1167 XFree (target_bytes);
1171 if (display_x11->motif_target_lists)
1173 g_free (display_x11->motif_target_lists);
1174 display_x11->motif_target_lists = NULL;
1175 display_x11->motif_n_target_lists = 0;
1177 g_warning ("Error reading Motif target table\n");
1183 targets_sort_func (gconstpointer a, gconstpointer b)
1185 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1186 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1189 /* Check if given (sorted) list is in the targets table */
1191 motif_target_table_check (GdkDisplay *display,
1194 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1195 GList *tmp_list1, *tmp_list2;
1198 for (i=0; i<display_x11->motif_n_target_lists; i++)
1200 tmp_list1 = display_x11->motif_target_lists[i];
1203 while (tmp_list1 && tmp_list2)
1205 if (tmp_list1->data != tmp_list2->data)
1208 tmp_list1 = tmp_list1->next;
1209 tmp_list2 = tmp_list2->next;
1211 if (!tmp_list1 && !tmp_list2) /* Found it */
1219 motif_add_to_target_table (GdkDisplay *display,
1220 GList *targets) /* targets is list of GdkAtom */
1222 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1223 GList *sorted = NULL;
1228 /* make a sorted copy of the list */
1231 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1232 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1233 targets = targets->next;
1236 /* First check if it is there already */
1238 if (display_x11->motif_target_lists)
1239 index = motif_target_table_check (display, sorted);
1241 /* We need to grab the server while doing this, to ensure
1247 /* We need to make sure that it exists _before_ we grab the
1248 * server, since we can't open a new connection after we
1251 motif_find_drag_window (display, TRUE);
1253 gdk_x11_display_grab (display);
1254 motif_read_target_table (display);
1256 /* Check again, in case it was added in the meantime */
1257 if (display_x11->motif_target_lists)
1258 index = motif_target_table_check (display, sorted);
1262 guint32 total_size = 0;
1266 MotifTargetTableHeader *header;
1268 if (!display_x11->motif_target_lists)
1270 display_x11->motif_target_lists = g_new (GList *, 1);
1271 display_x11->motif_n_target_lists = 1;
1275 display_x11->motif_n_target_lists++;
1276 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1277 sizeof(GList *) * display_x11->motif_n_target_lists);
1279 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1281 index = display_x11->motif_n_target_lists - 1;
1283 total_size = sizeof (MotifTargetTableHeader);
1284 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1285 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1287 data = g_malloc (total_size);
1289 header = (MotifTargetTableHeader *)data;
1290 p = data + sizeof(MotifTargetTableHeader);
1292 header->byte_order = local_byte_order;
1293 header->protocol_version = 0;
1294 header->n_lists = display_x11->motif_n_target_lists;
1295 header->total_size = total_size;
1297 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1299 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1300 guint32 *targets = g_new (guint32, n_targets);
1301 guint32 *p32 = targets;
1303 tmp_list = display_x11->motif_target_lists[i];
1306 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1308 tmp_list = tmp_list->next;
1313 p += sizeof(guint16);
1315 memcpy (p, targets, n_targets * sizeof(guint32));
1318 p += sizeof(guint32) * n_targets;
1322 XChangeProperty (display_x11->xdisplay,
1323 display_x11->motif_drag_window,
1324 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1325 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1329 gdk_x11_display_ungrab (display);
1332 g_list_free (sorted);
1336 /* Translate flags */
1339 motif_dnd_translate_flags (GdkX11DragContext *context_x11,
1342 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1343 guint recommended_op = flags & 0x000f;
1344 guint possible_ops = (flags & 0x0f0) >> 4;
1346 switch (recommended_op)
1349 context->suggested_action = GDK_ACTION_MOVE;
1352 context->suggested_action = GDK_ACTION_COPY;
1355 context->suggested_action = GDK_ACTION_LINK;
1358 context->suggested_action = GDK_ACTION_COPY;
1362 context->actions = 0;
1363 if (possible_ops & XmDROP_MOVE)
1364 context->actions |= GDK_ACTION_MOVE;
1365 if (possible_ops & XmDROP_COPY)
1366 context->actions |= GDK_ACTION_COPY;
1367 if (possible_ops & XmDROP_LINK)
1368 context->actions |= GDK_ACTION_LINK;
1372 motif_dnd_get_flags (GdkDragContext *context)
1376 switch (context->suggested_action)
1378 case GDK_ACTION_MOVE:
1379 flags = XmDROP_MOVE;
1381 case GDK_ACTION_COPY:
1382 flags = XmDROP_COPY;
1384 case GDK_ACTION_LINK:
1385 flags = XmDROP_LINK;
1388 flags = XmDROP_NOOP;
1392 if (context->actions & GDK_ACTION_MOVE)
1393 flags |= XmDROP_MOVE << 8;
1394 if (context->actions & GDK_ACTION_COPY)
1395 flags |= XmDROP_COPY << 8;
1396 if (context->actions & GDK_ACTION_LINK)
1397 flags |= XmDROP_LINK << 8;
1405 motif_set_targets (GdkX11DragContext *context_x11)
1407 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1408 MotifDragInitiatorInfo info;
1410 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1412 info.byte_order = local_byte_order;
1413 info.protocol_version = 0;
1414 info.targets_index = motif_add_to_target_table (display, context->targets);
1419 g_snprintf (buf, 20, "_GDK_SELECTION_%d", i);
1421 context_x11->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1422 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), context_x11->motif_selection))
1426 info.selection_atom = context_x11->motif_selection;
1428 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1429 GDK_WINDOW_XID (context->source_window),
1430 context_x11->motif_selection,
1431 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1433 (guchar *)&info, sizeof (info));
1435 context_x11->motif_targets_set = 1;
1439 motif_check_dest (GdkDisplay *display,
1442 gboolean retval = FALSE;
1444 MotifDragReceiverInfo *info;
1447 unsigned long nitems, after;
1448 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1450 gdk_x11_display_error_trap_push (display);
1451 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1452 motif_drag_receiver_info_atom,
1453 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1454 &type, &format, &nitems, &after,
1457 if (gdk_x11_display_error_trap_pop (display) == 0)
1461 info = (MotifDragReceiverInfo *)data;
1463 if ((format == 8) && (nitems == sizeof(*info)))
1465 if ((info->protocol_version == 0) &&
1466 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1467 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1468 (info->protocol_style == XmDRAG_DYNAMIC)))
1474 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1481 return retval ? win : None;
1485 motif_send_enter (GdkX11DragContext *context_x11,
1488 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1489 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1492 if (!G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
1493 return; /* Motif Dnd requires getting properties on the root window */
1495 xev.xclient.type = ClientMessage;
1496 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1497 xev.xclient.format = 8;
1498 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1500 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1501 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1502 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1503 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1504 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XID (context->source_window);
1506 if (!context_x11->motif_targets_set)
1507 motif_set_targets (context_x11);
1509 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1510 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1512 if (!_gdk_x11_display_send_xevent (display,
1513 GDK_WINDOW_XID (context->dest_window),
1516 g_message ("Send event to %lx failed",
1517 GDK_WINDOW_XID (context->dest_window)));
1521 motif_send_leave (GdkX11DragContext *context_x11,
1524 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1525 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1528 xev.xclient.type = ClientMessage;
1529 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1530 xev.xclient.format = 8;
1531 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1533 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1534 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1535 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1536 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1537 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1538 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1539 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1541 if (!_gdk_x11_display_send_xevent (display,
1542 GDK_WINDOW_XID (context->dest_window),
1545 g_message ("Send event to %lx failed",
1546 GDK_WINDOW_XID (context->dest_window)));
1550 motif_send_motion (GdkX11DragContext *context_x11,
1553 GdkDragAction action,
1556 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1557 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1561 xev.xclient.type = ClientMessage;
1562 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1563 xev.xclient.format = 8;
1564 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1566 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1567 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1568 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1569 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1570 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1572 if ((context->suggested_action != context_x11->old_action) ||
1573 (context->actions != context_x11->old_actions))
1575 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1577 /* context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1582 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1584 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1585 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1587 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1591 if (!_gdk_x11_display_send_xevent (display,
1592 GDK_WINDOW_XID (context->dest_window),
1595 g_message ("Send event to %lx failed",
1596 GDK_WINDOW_XID (context->dest_window)));
1602 motif_send_drop (GdkX11DragContext *context_x11,
1605 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1606 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1609 xev.xclient.type = ClientMessage;
1610 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1611 xev.xclient.format = 8;
1612 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1614 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1615 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1616 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1617 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1619 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
1620 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
1622 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1623 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XID (context->source_window);
1625 if (!_gdk_x11_display_send_xevent (display,
1626 GDK_WINDOW_XID (context->dest_window),
1629 g_message ("Send event to %lx failed",
1630 GDK_WINDOW_XID (context->dest_window)));
1636 motif_read_initiator_info (GdkDisplay *display,
1637 Window source_window,
1648 MotifDragInitiatorInfo *initiator_info;
1650 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1652 gdk_x11_display_error_trap_push (display);
1653 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1654 0, sizeof(*initiator_info), FALSE,
1655 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1656 &type, &format, &nitems, &bytes_after,
1659 if (gdk_x11_display_error_trap_pop (display) ||
1660 (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) ||
1663 g_warning ("Error reading initiator info\n");
1667 initiator_info = (MotifDragInitiatorInfo *)data;
1669 motif_read_target_table (display);
1671 initiator_info->targets_index =
1672 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1673 initiator_info->selection_atom =
1674 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1676 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1678 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1679 XFree (initiator_info);
1683 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1688 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1689 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1690 tmp_list = tmp_list->prev;
1693 #ifdef G_ENABLE_DEBUG
1694 if (_gdk_debug_flags & GDK_DEBUG_DND)
1695 print_target_list (*targets);
1696 #endif /* G_ENABLE_DEBUG */
1698 *selection = initiator_info->selection_atom;
1700 XFree (initiator_info);
1705 static GdkDragContext *
1706 motif_drag_context_new (GdkWindow *dest_window,
1708 guint32 source_window,
1711 GdkX11DragContext *context_x11;
1712 GdkDragContext *context;
1713 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
1714 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1716 /* FIXME, current_dest_drag really shouldn't be NULL'd
1717 * if we error below.
1719 if (display_x11->current_dest_drag != NULL)
1721 if (timestamp >= display_x11->current_dest_drag->start_time)
1723 g_object_unref (display_x11->current_dest_drag);
1724 display_x11->current_dest_drag = NULL;
1730 context_x11 = g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
1731 context = GDK_DRAG_CONTEXT (context_x11);
1733 context->protocol = GDK_DRAG_PROTO_MOTIF;
1734 context->is_source = FALSE;
1736 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
1737 if (!context->source_window)
1739 g_object_unref (context_x11);
1743 context->dest_window = dest_window;
1744 g_object_ref (dest_window);
1745 context->start_time = timestamp;
1747 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1751 &context_x11->motif_selection))
1753 g_object_unref (context_x11);
1761 * The MOTIF drag protocol has no real provisions for distinguishing
1762 * multiple simultaneous drops. If the sources grab the pointer
1763 * when doing drags, that shouldn't happen, in any case. If it
1764 * does, we can't do much except hope for the best.
1767 static GdkFilterReturn
1768 motif_top_level_enter (GdkEvent *event,
1771 guint32 source_window,
1774 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1775 GdkDragContext *new_context;
1777 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1778 flags, timestamp, source_window, atom));
1780 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1782 return GDK_FILTER_REMOVE;
1784 event->dnd.type = GDK_DRAG_ENTER;
1785 event->dnd.context = new_context;
1786 g_object_ref (new_context);
1788 display_x11->current_dest_drag = new_context;
1790 return GDK_FILTER_TRANSLATE;
1793 static GdkFilterReturn
1794 motif_top_level_leave (GdkEvent *event,
1798 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1800 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1803 if ((display_x11->current_dest_drag != NULL) &&
1804 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1805 (timestamp >= display_x11->current_dest_drag->start_time))
1807 event->dnd.type = GDK_DRAG_LEAVE;
1808 /* Pass ownership of context to the event */
1809 event->dnd.context = display_x11->current_dest_drag;
1811 display_x11->current_dest_drag = NULL;
1813 return GDK_FILTER_TRANSLATE;
1816 return GDK_FILTER_REMOVE;
1819 static GdkFilterReturn
1820 motif_motion (GdkEvent *event,
1826 GdkX11DragContext *context_x11;
1827 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1829 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1830 flags, timestamp, x_root, y_root));
1832 if ((display_x11->current_dest_drag != NULL) &&
1833 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1834 (timestamp >= display_x11->current_dest_drag->start_time))
1836 context_x11 = GDK_X11_DRAG_CONTEXT (display_x11->current_dest_drag);
1838 event->dnd.type = GDK_DRAG_MOTION;
1839 event->dnd.context = display_x11->current_dest_drag;
1840 g_object_ref (display_x11->current_dest_drag);
1842 event->dnd.time = timestamp;
1844 motif_dnd_translate_flags (context_x11, flags);
1846 event->dnd.x_root = x_root;
1847 event->dnd.y_root = y_root;
1849 context_x11->last_x = x_root;
1850 context_x11->last_y = y_root;
1852 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1854 return GDK_FILTER_TRANSLATE;
1857 return GDK_FILTER_REMOVE;
1860 static GdkFilterReturn
1861 motif_operation_changed (GdkEvent *event,
1865 GdkX11DragContext *context_x11;
1866 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1867 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1870 if ((display_x11->current_dest_drag != NULL) &&
1871 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1872 (timestamp >= display_x11->current_dest_drag->start_time))
1874 event->dnd.type = GDK_DRAG_MOTION;
1875 event->dnd.send_event = FALSE;
1876 event->dnd.context = display_x11->current_dest_drag;
1877 g_object_ref (display_x11->current_dest_drag);
1879 event->dnd.time = timestamp;
1880 context_x11 = GDK_X11_DRAG_CONTEXT (display_x11->current_dest_drag);
1882 motif_dnd_translate_flags (context_x11, flags);
1884 event->dnd.x_root = context_x11->last_x;
1885 event->dnd.y_root = context_x11->last_y;
1887 context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1889 return GDK_FILTER_TRANSLATE;
1892 return GDK_FILTER_REMOVE;
1895 static GdkFilterReturn
1896 motif_drop_start (GdkEvent *event,
1899 guint32 source_window,
1904 GdkDragContext *new_context;
1905 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1907 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1908 flags, timestamp, x_root, y_root, source_window, atom));
1910 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1912 return GDK_FILTER_REMOVE;
1914 motif_dnd_translate_flags (GDK_X11_DRAG_CONTEXT (new_context), flags);
1916 event->dnd.type = GDK_DROP_START;
1917 event->dnd.context = new_context;
1918 event->dnd.time = timestamp;
1919 event->dnd.x_root = x_root;
1920 event->dnd.y_root = y_root;
1922 gdk_x11_window_set_user_time (event->any.window, timestamp);
1924 g_object_ref (new_context);
1925 display_x11->current_dest_drag = new_context;
1927 return GDK_FILTER_TRANSLATE;
1930 static GdkFilterReturn
1931 motif_drag_status (GdkEvent *event,
1935 GdkDragContext *context;
1936 GdkDisplay *display;
1939 g_message ("Motif status message: flags %x", flags));
1941 display = gdk_window_get_display (event->any.window);
1943 return GDK_FILTER_REMOVE;
1945 context = gdk_drag_context_find (display, TRUE, GDK_WINDOW_XID (event->any.window), None);
1949 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
1950 if ((context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1951 (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1952 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
1954 event->dnd.type = GDK_DRAG_STATUS;
1955 event->dnd.send_event = FALSE;
1956 event->dnd.context = context;
1957 g_object_ref (context);
1959 event->dnd.time = timestamp;
1961 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1963 switch (flags & 0x000f)
1966 context->action = 0;
1969 context->action = GDK_ACTION_MOVE;
1972 context->action = GDK_ACTION_COPY;
1975 context->action = GDK_ACTION_LINK;
1980 context->action = 0;
1982 return GDK_FILTER_TRANSLATE;
1984 return GDK_FILTER_REMOVE;
1987 static GdkFilterReturn
1988 motif_dnd_filter (GdkXEvent *xev,
1992 XEvent *xevent = (XEvent *)xev;
1997 guint32 source_window;
1999 gint16 x_root, y_root;
2002 if (!event->any.window ||
2003 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2004 return GDK_FILTER_CONTINUE; /* Not for us */
2006 /* First read some fields common to all Motif DND messages */
2007 reason = MOTIF_UNPACK_BYTE (xevent, 0);
2008 flags = MOTIF_UNPACK_SHORT (xevent, 1);
2009 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
2011 is_reply = ((reason & 0x80) != 0);
2013 switch (reason & 0x7f)
2015 case XmTOP_LEVEL_ENTER:
2016 source_window = MOTIF_UNPACK_LONG (xevent, 2);
2017 atom = MOTIF_UNPACK_LONG (xevent, 3);
2018 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
2019 case XmTOP_LEVEL_LEAVE:
2020 return motif_top_level_leave (event, flags, timestamp);
2023 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2024 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2027 return motif_motion (event, flags, timestamp, x_root, y_root);
2029 return motif_drag_status (event, flags, timestamp);
2031 case XmDROP_SITE_ENTER:
2032 return motif_drag_status (event, flags, timestamp);
2034 case XmDROP_SITE_LEAVE:
2035 return motif_drag_status (event,
2036 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
2039 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2040 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2041 atom = MOTIF_UNPACK_LONG (xevent, 3);
2042 source_window = MOTIF_UNPACK_LONG (xevent, 4);
2045 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
2048 case XmOPERATION_CHANGED:
2050 return motif_operation_changed (event, flags, timestamp);
2052 return motif_drag_status (event, flags, timestamp);
2055 /* To the best of my knowledge, these next two messages are
2056 * not part of the protocol, though they are defined in
2060 case XmDRAG_DROP_FINISH:
2064 return GDK_FILTER_REMOVE;
2067 /*************************************************************
2068 ***************************** XDND **************************
2069 *************************************************************/
2071 /* Utility functions */
2076 GdkDragAction action;
2077 } xdnd_actions_table[] = {
2078 { "XdndActionCopy", None, GDK_ACTION_COPY },
2079 { "XdndActionMove", None, GDK_ACTION_MOVE },
2080 { "XdndActionLink", None, GDK_ACTION_LINK },
2081 { "XdndActionAsk", None, GDK_ACTION_ASK },
2082 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2085 static const gint xdnd_n_actions = G_N_ELEMENTS (xdnd_actions_table);
2086 static gboolean xdnd_actions_initialized = FALSE;
2089 xdnd_initialize_actions (void)
2093 xdnd_actions_initialized = TRUE;
2094 for (i = 0; i < xdnd_n_actions; i++)
2095 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2098 static GdkDragAction
2099 xdnd_action_from_atom (GdkDisplay *display,
2108 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2110 if (!xdnd_actions_initialized)
2111 xdnd_initialize_actions();
2113 for (i = 0; i < xdnd_n_actions; i++)
2114 if (atom == xdnd_actions_table[i].atom)
2115 return xdnd_actions_table[i].action;
2121 xdnd_action_to_atom (GdkDisplay *display,
2122 GdkDragAction action)
2126 if (!xdnd_actions_initialized)
2127 xdnd_initialize_actions();
2129 for (i = 0; i < xdnd_n_actions; i++)
2130 if (action == xdnd_actions_table[i].action)
2131 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2138 static GdkFilterReturn
2139 xdnd_status_filter (GdkXEvent *xev,
2143 GdkDisplay *display;
2144 XEvent *xevent = (XEvent *)xev;
2145 guint32 dest_window = xevent->xclient.data.l[0];
2146 guint32 flags = xevent->xclient.data.l[1];
2147 Atom action = xevent->xclient.data.l[4];
2148 GdkDragContext *context;
2150 if (!event->any.window ||
2151 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2152 return GDK_FILTER_CONTINUE; /* Not for us */
2155 g_message ("XdndStatus: dest_window: %#x action: %ld",
2156 dest_window, action));
2158 display = gdk_window_get_display (event->any.window);
2159 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2163 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
2164 if (context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2165 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2167 event->dnd.send_event = FALSE;
2168 event->dnd.type = GDK_DRAG_STATUS;
2169 event->dnd.context = context;
2170 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2171 g_object_ref (context);
2173 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2174 if (!(action != 0) != !(flags & 1))
2177 g_warning ("Received status event with flags not corresponding to action!\n"));
2181 context->action = xdnd_action_from_atom (display, action);
2183 return GDK_FILTER_TRANSLATE;
2186 return GDK_FILTER_REMOVE;
2189 static GdkFilterReturn
2190 xdnd_finished_filter (GdkXEvent *xev,
2194 GdkDisplay *display;
2195 XEvent *xevent = (XEvent *)xev;
2196 guint32 dest_window = xevent->xclient.data.l[0];
2197 GdkDragContext *context;
2198 GdkX11DragContext *context_x11;
2200 if (!event->any.window ||
2201 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2202 return GDK_FILTER_CONTINUE; /* Not for us */
2205 g_message ("XdndFinished: dest_window: %#x", dest_window));
2207 display = gdk_window_get_display (event->any.window);
2208 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2212 context_x11 = GDK_X11_DRAG_CONTEXT (context);
2213 if (context_x11->version == 5)
2214 context_x11->drop_failed = xevent->xclient.data.l[1] == 0;
2216 event->dnd.type = GDK_DROP_FINISHED;
2217 event->dnd.context = context;
2218 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2219 g_object_ref (context);
2221 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2223 return GDK_FILTER_TRANSLATE;
2226 return GDK_FILTER_REMOVE;
2230 xdnd_set_targets (GdkX11DragContext *context_x11)
2232 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2234 GList *tmp_list = context->targets;
2236 gint n_atoms = g_list_length (context->targets);
2237 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2239 atomlist = g_new (Atom, n_atoms);
2243 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2244 tmp_list = tmp_list->next;
2248 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2249 GDK_WINDOW_XID (context->source_window),
2250 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2251 XA_ATOM, 32, PropModeReplace,
2252 (guchar *)atomlist, n_atoms);
2256 context_x11->xdnd_targets_set = 1;
2260 xdnd_set_actions (GdkX11DragContext *context_x11)
2262 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2267 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2269 if (!xdnd_actions_initialized)
2270 xdnd_initialize_actions();
2272 actions = context->actions;
2274 for (i = 0; i < xdnd_n_actions; i++)
2276 if (actions & xdnd_actions_table[i].action)
2278 actions &= ~xdnd_actions_table[i].action;
2283 atomlist = g_new (Atom, n_atoms);
2285 actions = context->actions;
2287 for (i = 0; i < xdnd_n_actions; i++)
2289 if (actions & xdnd_actions_table[i].action)
2291 actions &= ~xdnd_actions_table[i].action;
2292 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2297 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2298 GDK_WINDOW_XID (context->source_window),
2299 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2300 XA_ATOM, 32, PropModeReplace,
2301 (guchar *)atomlist, n_atoms);
2305 context_x11->xdnd_actions_set = TRUE;
2306 context_x11->xdnd_actions = context->actions;
2310 send_client_message_async_cb (Window window,
2314 GdkDragContext *context = data;
2316 g_message ("Got async callback for #%lx, success = %d",
2319 /* On failure, we immediately continue with the protocol
2320 * so we don't end up blocking for a timeout
2323 context->dest_window &&
2324 window == GDK_WINDOW_XID (context->dest_window))
2326 GdkEvent *temp_event;
2327 GdkX11DragContext *context_x11 = data;
2329 g_object_unref (context->dest_window);
2330 context->dest_window = NULL;
2331 context->action = 0;
2333 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2335 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2336 temp_event->dnd.window = g_object_ref (context->source_window);
2337 temp_event->dnd.send_event = TRUE;
2338 temp_event->dnd.context = g_object_ref (context);
2339 temp_event->dnd.time = GDK_CURRENT_TIME;
2340 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2342 gdk_event_put (temp_event);
2344 gdk_event_free (temp_event);
2347 g_object_unref (context);
2352 gdk_drag_context_get_display (GdkDragContext *context)
2354 if (context->source_window)
2355 return GDK_WINDOW_DISPLAY (context->source_window);
2356 else if (context->dest_window)
2357 return GDK_WINDOW_DISPLAY (context->dest_window);
2359 g_assert_not_reached ();
2364 send_client_message_async (GdkDragContext *context,
2368 XClientMessageEvent *event_send)
2370 GdkDisplay *display = gdk_drag_context_get_display (context);
2372 g_object_ref (context);
2374 _gdk_x11_send_client_message_async (display, window,
2375 propagate, event_mask, event_send,
2376 send_client_message_async_cb, context);
2380 xdnd_send_xevent (GdkX11DragContext *context_x11,
2385 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2386 GdkDisplay *display = gdk_drag_context_get_display (context);
2390 g_assert (event_send->xany.type == ClientMessage);
2392 /* We short-circuit messages to ourselves */
2393 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2397 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2399 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2400 event_send->xclient.message_type)
2402 GdkEvent *temp_event;
2404 temp_event = gdk_event_new (GDK_NOTHING);
2405 temp_event->any.window = g_object_ref (window);
2407 if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
2409 gdk_event_put (temp_event);
2410 gdk_event_free (temp_event);
2418 xwindow = GDK_WINDOW_XID (window);
2420 if (_gdk_x11_display_is_root_window (display, xwindow))
2421 event_mask = ButtonPressMask;
2425 send_client_message_async (context, xwindow, propagate, event_mask,
2426 &event_send->xclient);
2432 xdnd_send_enter (GdkX11DragContext *context_x11)
2434 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2435 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->dest_window);
2438 xev.xclient.type = ClientMessage;
2439 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2440 xev.xclient.format = 32;
2441 xev.xclient.window = context_x11->drop_xid
2442 ? context_x11->drop_xid
2443 : GDK_WINDOW_XID (context->dest_window);
2444 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2445 xev.xclient.data.l[1] = (context_x11->version << 24); /* version */
2446 xev.xclient.data.l[2] = 0;
2447 xev.xclient.data.l[3] = 0;
2448 xev.xclient.data.l[4] = 0;
2451 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2452 GDK_WINDOW_XID (context->source_window), context_x11->version));
2453 if (g_list_length (context->targets) > 3)
2455 if (!context_x11->xdnd_targets_set)
2456 xdnd_set_targets (context_x11);
2457 xev.xclient.data.l[1] |= 1;
2461 GList *tmp_list = context->targets;
2466 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2467 GDK_POINTER_TO_ATOM (tmp_list->data));
2468 tmp_list = tmp_list->next;
2473 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2476 g_message ("Send event to %lx failed",
2477 GDK_WINDOW_XID (context->dest_window)));
2478 g_object_unref (context->dest_window);
2479 context->dest_window = NULL;
2484 xdnd_send_leave (GdkX11DragContext *context_x11)
2486 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2487 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2490 xev.xclient.type = ClientMessage;
2491 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2492 xev.xclient.format = 32;
2493 xev.xclient.window = context_x11->drop_xid
2494 ? context_x11->drop_xid
2495 : GDK_WINDOW_XID (context->dest_window);
2496 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2497 xev.xclient.data.l[1] = 0;
2498 xev.xclient.data.l[2] = 0;
2499 xev.xclient.data.l[3] = 0;
2500 xev.xclient.data.l[4] = 0;
2502 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2505 g_message ("Send event to %lx failed",
2506 GDK_WINDOW_XID (context->dest_window)));
2507 g_object_unref (context->dest_window);
2508 context->dest_window = NULL;
2513 xdnd_send_drop (GdkX11DragContext *context_x11,
2516 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2517 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2520 xev.xclient.type = ClientMessage;
2521 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2522 xev.xclient.format = 32;
2523 xev.xclient.window = context_x11->drop_xid
2524 ? context_x11->drop_xid
2525 : GDK_WINDOW_XID (context->dest_window);
2526 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2527 xev.xclient.data.l[1] = 0;
2528 xev.xclient.data.l[2] = time;
2529 xev.xclient.data.l[3] = 0;
2530 xev.xclient.data.l[4] = 0;
2532 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2535 g_message ("Send event to %lx failed",
2536 GDK_WINDOW_XID (context->dest_window)));
2537 g_object_unref (context->dest_window);
2538 context->dest_window = NULL;
2543 xdnd_send_motion (GdkX11DragContext *context_x11,
2546 GdkDragAction action,
2549 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2550 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2553 xev.xclient.type = ClientMessage;
2554 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2555 xev.xclient.format = 32;
2556 xev.xclient.window = context_x11->drop_xid
2557 ? context_x11->drop_xid
2558 : GDK_WINDOW_XID (context->dest_window);
2559 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2560 xev.xclient.data.l[1] = 0;
2561 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2562 xev.xclient.data.l[3] = time;
2563 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2565 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2568 g_message ("Send event to %lx failed",
2569 GDK_WINDOW_XID (context->dest_window)));
2570 g_object_unref (context->dest_window);
2571 context->dest_window = NULL;
2573 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2577 xdnd_check_dest (GdkDisplay *display,
2579 guint *xdnd_version)
2581 gboolean retval = FALSE;
2584 unsigned long nitems, after;
2589 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2590 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2594 gdk_x11_display_error_trap_push (display);
2595 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2597 1, False, AnyPropertyType,
2598 &type, &format, &nitems, &after,
2603 proxy_data = (Window *)data;
2605 if ((format == 32) && (nitems == 1))
2607 proxy = *proxy_data;
2611 g_warning ("Invalid XdndProxy "
2612 "property on window %ld\n", win));
2617 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2619 1, False, AnyPropertyType,
2620 &type, &format, &nitems, &after,
2621 &data) == Success) &&
2624 version = (Atom *)data;
2626 if ((format == 32) && (nitems == 1))
2631 *xdnd_version = *version;
2635 g_warning ("Invalid XdndAware "
2636 "property on window %ld\n", win));
2642 gdk_x11_display_error_trap_pop_ignored (display);
2644 return retval ? (proxy ? proxy : win) : None;
2650 xdnd_read_actions (GdkX11DragContext *context_x11)
2652 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2653 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2656 gulong nitems, after;
2661 context_x11->xdnd_have_actions = FALSE;
2663 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2665 /* Get the XdndActionList, if set */
2667 gdk_x11_display_error_trap_push (display);
2668 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2669 GDK_WINDOW_XID (context->source_window),
2670 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2672 False, XA_ATOM, &type, &format, &nitems,
2673 &after, &data) == Success &&
2676 atoms = (Atom *)data;
2678 context->actions = 0;
2680 for (i = 0; i < nitems; i++)
2681 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2683 context_x11->xdnd_have_actions = TRUE;
2685 #ifdef G_ENABLE_DEBUG
2686 if (_gdk_debug_flags & GDK_DEBUG_DND)
2688 GString *action_str = g_string_new (NULL);
2689 if (context->actions & GDK_ACTION_MOVE)
2690 g_string_append(action_str, "MOVE ");
2691 if (context->actions & GDK_ACTION_COPY)
2692 g_string_append(action_str, "COPY ");
2693 if (context->actions & GDK_ACTION_LINK)
2694 g_string_append(action_str, "LINK ");
2695 if (context->actions & GDK_ACTION_ASK)
2696 g_string_append(action_str, "ASK ");
2698 g_message("Xdnd actions = %s", action_str->str);
2699 g_string_free (action_str, TRUE);
2701 #endif /* G_ENABLE_DEBUG */
2708 gdk_x11_display_error_trap_pop_ignored (display);
2714 GdkDragContext *source_context;
2716 source_context = gdk_drag_context_find (display, TRUE,
2717 GDK_WINDOW_XID (context->source_window),
2718 GDK_WINDOW_XID (context->dest_window));
2722 context->actions = source_context->actions;
2723 context_x11->xdnd_have_actions = TRUE;
2728 /* We have to make sure that the XdndActionList we keep internally
2729 * is up to date with the XdndActionList on the source window
2730 * because we get no notification, because Xdnd wasn't meant
2731 * to continually send actions. So we select on PropertyChangeMask
2732 * and add this filter.
2734 static GdkFilterReturn
2735 xdnd_source_window_filter (GdkXEvent *xev,
2739 XEvent *xevent = (XEvent *)xev;
2740 GdkX11DragContext *context_x11 = cb_data;
2741 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2743 if ((xevent->xany.type == PropertyNotify) &&
2744 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2746 xdnd_read_actions (context_x11);
2748 return GDK_FILTER_REMOVE;
2751 return GDK_FILTER_CONTINUE;
2755 xdnd_manage_source_filter (GdkDragContext *context,
2757 gboolean add_filter)
2759 if (!GDK_WINDOW_DESTROYED (window) &&
2760 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2762 gdk_x11_display_error_trap_push (GDK_WINDOW_DISPLAY (window));
2766 gdk_window_set_events (window,
2767 gdk_window_get_events (window) |
2768 GDK_PROPERTY_CHANGE_MASK);
2769 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2773 gdk_window_remove_filter (window,
2774 xdnd_source_window_filter,
2776 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2777 * but we might want it for other reasons. (Like
2778 * INCR selection transactions).
2782 gdk_x11_display_error_trap_pop_ignored (GDK_WINDOW_DISPLAY (window));
2787 base_precache_atoms (GdkDisplay *display)
2789 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2791 if (!display_x11->base_dnd_atoms_precached)
2793 static const char *const precache_atoms[] = {
2794 "ENLIGHTENMENT_DESKTOP",
2798 "_MOTIF_DRAG_RECEIVER_INFO"
2801 _gdk_x11_precache_atoms (display,
2802 precache_atoms, G_N_ELEMENTS (precache_atoms));
2804 display_x11->base_dnd_atoms_precached = TRUE;
2809 xdnd_precache_atoms (GdkDisplay *display)
2811 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2813 if (!display_x11->xdnd_atoms_precached)
2815 static const gchar *const precache_atoms[] = {
2821 "XdndActionPrivate",
2832 _gdk_x11_precache_atoms (display,
2833 precache_atoms, G_N_ELEMENTS (precache_atoms));
2835 display_x11->xdnd_atoms_precached = TRUE;
2839 static GdkFilterReturn
2840 xdnd_enter_filter (GdkXEvent *xev,
2844 GdkDeviceManager *device_manager;
2845 GdkDisplay *display;
2846 GdkX11Display *display_x11;
2847 XEvent *xevent = (XEvent *)xev;
2848 GdkDragContext *context;
2849 GdkX11DragContext *context_x11;
2853 gulong nitems, after;
2856 guint32 source_window;
2860 if (!event->any.window ||
2861 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2862 return GDK_FILTER_CONTINUE; /* Not for us */
2864 source_window = xevent->xclient.data.l[0];
2865 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2866 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2868 display = GDK_WINDOW_DISPLAY (event->any.window);
2869 display_x11 = GDK_X11_DISPLAY (display);
2871 xdnd_precache_atoms (display);
2874 g_message ("XdndEnter: source_window: %#x, version: %#x",
2875 source_window, version));
2879 /* Old source ignore */
2880 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2881 return GDK_FILTER_REMOVE;
2884 if (display_x11->current_dest_drag != NULL)
2886 g_object_unref (display_x11->current_dest_drag);
2887 display_x11->current_dest_drag = NULL;
2890 context_x11 = (GdkX11DragContext *)g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
2891 context = (GdkDragContext *)context_x11;
2893 context->protocol = GDK_DRAG_PROTO_XDND;
2894 context_x11->version = version;
2896 /* FIXME: Should extend DnD protocol to have device info */
2897 device_manager = gdk_display_get_device_manager (display);
2898 gdk_drag_context_set_device (context, gdk_device_manager_get_client_pointer (device_manager));
2900 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
2901 if (!context->source_window)
2903 g_object_unref (context);
2904 return GDK_FILTER_REMOVE;
2906 context->dest_window = event->any.window;
2907 g_object_ref (context->dest_window);
2909 context->targets = NULL;
2912 gdk_x11_display_error_trap_push (display);
2913 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2915 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2917 False, XA_ATOM, &type, &format, &nitems,
2920 if (gdk_x11_display_error_trap_pop (display) || (format != 32) || (type != XA_ATOM))
2922 g_object_unref (context);
2927 return GDK_FILTER_REMOVE;
2930 atoms = (Atom *)data;
2932 for (i = 0; i < nitems; i++)
2934 g_list_append (context->targets,
2935 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2942 for (i = 0; i < 3; i++)
2943 if (xevent->xclient.data.l[2 + i])
2945 g_list_append (context->targets,
2946 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2947 xevent->xclient.data.l[2 + i])));
2950 #ifdef G_ENABLE_DEBUG
2951 if (_gdk_debug_flags & GDK_DEBUG_DND)
2952 print_target_list (context->targets);
2953 #endif /* G_ENABLE_DEBUG */
2955 xdnd_manage_source_filter (context, context->source_window, TRUE);
2956 xdnd_read_actions (context_x11);
2958 event->dnd.type = GDK_DRAG_ENTER;
2959 event->dnd.context = context;
2960 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2961 g_object_ref (context);
2963 display_x11->current_dest_drag = context;
2965 return GDK_FILTER_TRANSLATE;
2968 static GdkFilterReturn
2969 xdnd_leave_filter (GdkXEvent *xev,
2973 XEvent *xevent = (XEvent *)xev;
2974 guint32 source_window = xevent->xclient.data.l[0];
2975 GdkDisplay *display;
2976 GdkX11Display *display_x11;
2978 if (!event->any.window ||
2979 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2980 return GDK_FILTER_CONTINUE; /* Not for us */
2983 g_message ("XdndLeave: source_window: %#x",
2986 display = GDK_WINDOW_DISPLAY (event->any.window);
2987 display_x11 = GDK_X11_DISPLAY (display);
2989 xdnd_precache_atoms (display);
2991 if ((display_x11->current_dest_drag != NULL) &&
2992 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2993 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
2995 event->dnd.type = GDK_DRAG_LEAVE;
2996 /* Pass ownership of context to the event */
2997 event->dnd.context = display_x11->current_dest_drag;
2998 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
3000 display_x11->current_dest_drag = NULL;
3002 return GDK_FILTER_TRANSLATE;
3005 return GDK_FILTER_REMOVE;
3008 static GdkFilterReturn
3009 xdnd_position_filter (GdkXEvent *xev,
3013 XEvent *xevent = (XEvent *)xev;
3014 guint32 source_window = xevent->xclient.data.l[0];
3015 gint16 x_root = xevent->xclient.data.l[2] >> 16;
3016 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
3017 guint32 time = xevent->xclient.data.l[3];
3018 Atom action = xevent->xclient.data.l[4];
3019 GdkDisplay *display;
3020 GdkX11Display *display_x11;
3021 GdkDragContext *context;
3022 GdkX11DragContext *context_x11;
3024 if (!event->any.window ||
3025 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3026 return GDK_FILTER_CONTINUE; /* Not for us */
3029 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
3030 source_window, x_root, y_root, time, action));
3032 display = GDK_WINDOW_DISPLAY (event->any.window);
3033 display_x11 = GDK_X11_DISPLAY (display);
3035 xdnd_precache_atoms (display);
3037 context = display_x11->current_dest_drag;
3039 if ((context != NULL) &&
3040 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3041 (GDK_WINDOW_XID (context->source_window) == source_window))
3043 context_x11 = GDK_X11_DRAG_CONTEXT (context);
3045 event->dnd.type = GDK_DRAG_MOTION;
3046 event->dnd.context = context;
3047 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3048 g_object_ref (context);
3050 event->dnd.time = time;
3052 context->suggested_action = xdnd_action_from_atom (display, action);
3054 if (!context_x11->xdnd_have_actions)
3055 context->actions = context->suggested_action;
3057 event->dnd.x_root = x_root;
3058 event->dnd.y_root = y_root;
3060 context_x11->last_x = x_root;
3061 context_x11->last_y = y_root;
3063 return GDK_FILTER_TRANSLATE;
3066 return GDK_FILTER_REMOVE;
3069 static GdkFilterReturn
3070 xdnd_drop_filter (GdkXEvent *xev,
3074 XEvent *xevent = (XEvent *)xev;
3075 guint32 source_window = xevent->xclient.data.l[0];
3076 guint32 time = xevent->xclient.data.l[2];
3077 GdkDisplay *display;
3078 GdkX11Display *display_x11;
3079 GdkDragContext *context;
3080 GdkX11DragContext *context_x11;
3082 if (!event->any.window ||
3083 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3084 return GDK_FILTER_CONTINUE; /* Not for us */
3087 g_message ("XdndDrop: source_window: %#x time: %d",
3088 source_window, time));
3090 display = GDK_WINDOW_DISPLAY (event->any.window);
3091 display_x11 = GDK_X11_DISPLAY (display);
3093 xdnd_precache_atoms (display);
3095 context = display_x11->current_dest_drag;
3097 if ((context != NULL) &&
3098 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3099 (GDK_WINDOW_XID (context->source_window) == source_window))
3101 context_x11 = GDK_X11_DRAG_CONTEXT (context);
3102 event->dnd.type = GDK_DROP_START;
3104 event->dnd.context = context;
3105 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3106 g_object_ref (context);
3108 event->dnd.time = time;
3109 event->dnd.x_root = context_x11->last_x;
3110 event->dnd.y_root = context_x11->last_y;
3112 gdk_x11_window_set_user_time (event->any.window, time);
3114 return GDK_FILTER_TRANSLATE;
3117 return GDK_FILTER_REMOVE;
3121 _gdk_x11_dnd_filter (GdkXEvent *xev,
3125 XEvent *xevent = (XEvent *) xev;
3126 GdkDisplay *display;
3129 if (!GDK_IS_X11_WINDOW (event->any.window))
3130 return GDK_FILTER_CONTINUE;
3132 if (xevent->type != ClientMessage)
3133 return GDK_FILTER_CONTINUE;
3135 display = GDK_WINDOW_DISPLAY (event->any.window);
3137 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3139 if (xevent->xclient.message_type != gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name))
3142 return xdnd_filters[i].func (xev, event, data);
3145 return GDK_FILTER_CONTINUE;
3151 gdk_drag_do_leave (GdkX11DragContext *context_x11,
3154 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
3156 if (context->dest_window)
3158 switch (context->protocol)
3160 case GDK_DRAG_PROTO_MOTIF:
3161 motif_send_leave (context_x11, time);
3163 case GDK_DRAG_PROTO_XDND:
3164 xdnd_send_leave (context_x11);
3166 case GDK_DRAG_PROTO_ROOTWIN:
3167 case GDK_DRAG_PROTO_NONE:
3172 g_object_unref (context->dest_window);
3173 context->dest_window = NULL;
3178 _gdk_x11_window_drag_begin (GdkWindow *window,
3182 GdkDragContext *context;
3184 context = (GdkDragContext *) g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
3186 context->is_source = TRUE;
3187 context->source_window = window;
3188 g_object_ref (window);
3190 context->targets = g_list_copy (targets);
3191 precache_target_list (context);
3193 context->actions = 0;
3195 gdk_drag_context_set_device (context, device);
3201 _gdk_x11_display_get_drag_protocol (GdkDisplay *display,
3203 GdkDragProtocol *protocol,
3210 base_precache_atoms (display);
3212 /* Check for a local drag */
3213 window = gdk_x11_window_lookup_for_display (display, xid);
3214 if (window && gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3216 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3218 *protocol = GDK_DRAG_PROTO_XDND;
3220 xdnd_precache_atoms (display);
3221 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", (guint) xid));
3224 else if (_gdk_x11_display_is_root_window (display, xid))
3226 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3227 GDK_NOTE (DND, g_message ("Entering root window\n"));
3231 else if ((retval = xdnd_check_dest (display, xid, version)))
3233 *protocol = GDK_DRAG_PROTO_XDND;
3234 xdnd_precache_atoms (display);
3235 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", (guint) xid));
3238 else if ((retval = motif_check_dest (display, xid)))
3240 *protocol = GDK_DRAG_PROTO_MOTIF;
3241 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", (guint) xid));
3246 /* Check if this is a root window */
3247 gboolean rootwin = FALSE;
3249 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3254 GDK_NOTE (DND, g_message ("Entering root window\n"));
3255 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3260 *protocol = GDK_DRAG_PROTO_NONE;
3262 return 0; /* a.k.a. None */
3265 static GdkWindowCache *
3266 drag_context_find_window_cache (GdkX11DragContext *context_x11,
3270 GdkWindowCache *cache;
3272 for (list = context_x11->window_caches; list; list = list->next)
3275 if (cache->screen == screen)
3279 cache = gdk_window_cache_get (screen);
3280 context_x11->window_caches = g_slist_prepend (context_x11->window_caches, cache);
3286 gdk_x11_drag_context_find_window (GdkDragContext *context,
3287 GdkWindow *drag_window,
3291 GdkDragProtocol *protocol)
3293 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3294 GdkWindowCache *window_cache;
3295 GdkDisplay *display;
3297 GdkWindow *dest_window;
3299 display = GDK_WINDOW_DISPLAY (context->source_window);
3301 window_cache = drag_context_find_window_cache (context_x11, screen);
3303 dest = get_client_window_at_coords (window_cache,
3304 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3305 GDK_WINDOW_XID (drag_window) : None,
3308 if (context_x11->dest_xid != dest)
3311 context_x11->dest_xid = dest;
3313 /* Check if new destination accepts drags, and which protocol */
3315 /* There is some ugliness here. We actually need to pass
3316 * _three_ pieces of information to drag_motion - dest_window,
3317 * protocol, and the XID of the unproxied window. The first
3318 * two are passed explicitely, the third implicitly through
3319 * protocol->dest_xid.
3321 recipient = _gdk_x11_display_get_drag_protocol (display,
3324 &context_x11->version);
3326 if (recipient != None)
3327 dest_window = gdk_x11_window_foreign_new_for_display (display, recipient);
3333 dest_window = context->dest_window;
3335 g_object_ref (dest_window);
3336 *protocol = context->protocol;
3343 gdk_x11_drag_context_drag_motion (GdkDragContext *context,
3344 GdkWindow *dest_window,
3345 GdkDragProtocol protocol,
3348 GdkDragAction suggested_action,
3349 GdkDragAction possible_actions,
3352 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3354 context_x11->old_actions = context->actions;
3355 context->actions = possible_actions;
3357 if (context_x11->old_actions != possible_actions)
3358 context_x11->xdnd_actions_set = FALSE;
3360 if (protocol == GDK_DRAG_PROTO_XDND && context_x11->version == 0)
3362 /* This ugly hack is necessary since GTK+ doesn't know about
3363 * the XDND protocol version, and in particular doesn't know
3364 * that gdk_drag_find_window_for_screen() has the side-effect
3365 * of setting context_x11->version, and therefore sometimes call
3366 * gdk_drag_motion() without a prior call to
3367 * gdk_drag_find_window_for_screen(). This happens, e.g.
3368 * when GTK+ is proxying DND events to embedded windows.
3372 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3374 xdnd_check_dest (display,
3375 GDK_WINDOW_XID (dest_window),
3376 &context_x11->version);
3380 /* When we have a Xdnd target, make sure our XdndActionList
3381 * matches the current actions;
3383 if (protocol == GDK_DRAG_PROTO_XDND && !context_x11->xdnd_actions_set)
3387 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3388 xdnd_set_actions (context_x11);
3389 else if (context->dest_window == dest_window)
3391 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3392 GdkDragContext *dest_context;
3394 dest_context = gdk_drag_context_find (display, FALSE,
3395 GDK_WINDOW_XID (context->source_window),
3396 GDK_WINDOW_XID (dest_window));
3400 dest_context->actions = context->actions;
3401 GDK_X11_DRAG_CONTEXT (dest_context)->xdnd_have_actions = TRUE;
3407 if (context->dest_window != dest_window)
3409 GdkEvent *temp_event;
3411 /* Send a leave to the last destination */
3412 gdk_drag_do_leave (context_x11, time);
3413 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
3415 /* Check if new destination accepts drags, and which protocol */
3419 context->dest_window = dest_window;
3420 context_x11->drop_xid = context_x11->dest_xid;
3421 g_object_ref (context->dest_window);
3422 context->protocol = protocol;
3426 case GDK_DRAG_PROTO_MOTIF:
3427 motif_send_enter (context_x11, time);
3430 case GDK_DRAG_PROTO_XDND:
3431 xdnd_send_enter (context_x11);
3434 case GDK_DRAG_PROTO_ROOTWIN:
3435 case GDK_DRAG_PROTO_NONE:
3439 context_x11->old_action = suggested_action;
3440 context->suggested_action = suggested_action;
3441 context_x11->old_actions = possible_actions;
3445 context->dest_window = NULL;
3446 context_x11->drop_xid = None;
3447 context->action = 0;
3450 /* Push a status event, to let the client know that
3453 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3454 temp_event->dnd.window = g_object_ref (context->source_window);
3455 /* We use this to signal a synthetic status. Perhaps
3456 * we should use an extra field...
3458 temp_event->dnd.send_event = TRUE;
3460 temp_event->dnd.context = g_object_ref (context);
3461 temp_event->dnd.time = time;
3462 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3464 gdk_event_put (temp_event);
3465 gdk_event_free (temp_event);
3469 context_x11->old_action = context->suggested_action;
3470 context->suggested_action = suggested_action;
3473 /* Send a drag-motion event */
3475 context_x11->last_x = x_root;
3476 context_x11->last_y = y_root;
3478 if (context->dest_window)
3480 if (context_x11->drag_status == GDK_DRAG_STATUS_DRAG)
3482 switch (context->protocol)
3484 case GDK_DRAG_PROTO_MOTIF:
3485 motif_send_motion (context_x11, x_root, y_root, suggested_action, time);
3488 case GDK_DRAG_PROTO_XDND:
3489 xdnd_send_motion (context_x11, x_root, y_root, suggested_action, time);
3492 case GDK_DRAG_PROTO_ROOTWIN:
3494 GdkEvent *temp_event;
3495 /* GTK+ traditionally has used application/x-rootwin-drop,
3496 * but the XDND spec specifies x-rootwindow-drop.
3498 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3499 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3501 if (g_list_find (context->targets,
3502 GDK_ATOM_TO_POINTER (target1)) ||
3503 g_list_find (context->targets,
3504 GDK_ATOM_TO_POINTER (target2)))
3505 context->action = context->suggested_action;
3507 context->action = 0;
3509 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3510 temp_event->dnd.window = g_object_ref (context->source_window);
3511 temp_event->dnd.send_event = FALSE;
3512 temp_event->dnd.context = g_object_ref (context);
3513 temp_event->dnd.time = time;
3514 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3516 gdk_event_put (temp_event);
3517 gdk_event_free (temp_event);
3520 case GDK_DRAG_PROTO_NONE:
3521 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3535 gdk_x11_drag_context_drag_abort (GdkDragContext *context,
3538 gdk_drag_do_leave (GDK_X11_DRAG_CONTEXT (context), time);
3542 gdk_x11_drag_context_drag_drop (GdkDragContext *context,
3545 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3547 if (context->dest_window)
3549 switch (context->protocol)
3551 case GDK_DRAG_PROTO_MOTIF:
3552 motif_send_leave (context_x11, time);
3553 motif_send_drop (context_x11, time);
3556 case GDK_DRAG_PROTO_XDND:
3557 xdnd_send_drop (context_x11, time);
3560 case GDK_DRAG_PROTO_ROOTWIN:
3561 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3563 case GDK_DRAG_PROTO_NONE:
3564 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3572 /* Destination side */
3575 gdk_x11_drag_context_drag_status (GdkDragContext *context,
3576 GdkDragAction action,
3579 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3581 GdkDisplay *display;
3583 display = GDK_WINDOW_DISPLAY (context->source_window);
3585 context->action = action;
3587 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3589 gboolean need_coords = FALSE;
3591 xev.xclient.type = ClientMessage;
3592 xev.xclient.message_type =
3593 gdk_x11_get_xatom_by_name_for_display (display,
3594 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3595 xev.xclient.format = 8;
3596 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3598 if (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3600 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3604 if ((action != 0) != (context_x11->old_action != 0))
3608 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3613 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3618 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3623 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3627 case GDK_ACTION_MOVE:
3628 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3630 case GDK_ACTION_COPY:
3631 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3633 case GDK_ACTION_LINK:
3634 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3637 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3642 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3644 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3646 MOTIF_XCLIENT_LONG (&xev, 1) = time_;
3650 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
3651 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
3654 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3656 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3657 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3659 if (!_gdk_x11_display_send_xevent (display,
3660 GDK_WINDOW_XID (context->source_window),
3663 g_message ("Send event to %lx failed",
3664 GDK_WINDOW_XID (context->source_window)));
3666 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3668 xev.xclient.type = ClientMessage;
3669 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3670 xev.xclient.format = 32;
3671 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3673 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3674 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3675 xev.xclient.data.l[2] = 0;
3676 xev.xclient.data.l[3] = 0;
3677 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3678 if (!xdnd_send_xevent (context_x11, context->source_window, FALSE, &xev))
3680 g_message ("Send event to %lx failed",
3681 GDK_WINDOW_XID (context->source_window)));
3684 context_x11->old_action = action;
3688 gdk_x11_drag_context_drop_reply (GdkDragContext *context,
3692 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3694 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3696 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3699 xev.xclient.type = ClientMessage;
3700 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3701 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3702 xev.xclient.format = 8;
3704 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3705 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3707 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3708 (XmDROP_SITE_VALID << 4) |
3709 (XmDROP_NOOP << 8) |
3712 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3713 (XmNO_DROP_SITE << 4) |
3714 (XmDROP_NOOP << 8) |
3715 (XmDROP_CANCEL << 12);
3716 MOTIF_XCLIENT_SHORT (&xev, 2) = context_x11->last_x;
3717 MOTIF_XCLIENT_SHORT (&xev, 3) = context_x11->last_y;
3718 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3719 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3720 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3722 _gdk_x11_display_send_xevent (display,
3723 GDK_WINDOW_XID (context->source_window),
3729 gdk_x11_drag_context_drop_finish (GdkDragContext *context,
3733 if (context->protocol == GDK_DRAG_PROTO_XDND)
3735 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3738 xev.xclient.type = ClientMessage;
3739 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3740 xev.xclient.format = 32;
3741 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3743 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3746 xev.xclient.data.l[1] = 1;
3747 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3752 xev.xclient.data.l[1] = 0;
3753 xev.xclient.data.l[2] = None;
3755 xev.xclient.data.l[3] = 0;
3756 xev.xclient.data.l[4] = 0;
3758 if (!xdnd_send_xevent (GDK_X11_DRAG_CONTEXT (context), context->source_window, FALSE, &xev))
3760 g_message ("Send event to %lx failed",
3761 GDK_WINDOW_XID (context->source_window)));
3766 _gdk_x11_window_register_dnd (GdkWindow *window)
3768 static const gulong xdnd_version = 5;
3769 MotifDragReceiverInfo info;
3770 Atom motif_drag_receiver_info_atom;
3771 GdkDisplay *display = gdk_window_get_display (window);
3773 g_return_if_fail (window != NULL);
3775 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
3778 base_precache_atoms (display);
3780 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3783 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3785 /* Set Motif drag receiver information property */
3786 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3787 "_MOTIF_DRAG_RECEIVER_INFO");
3788 /* initialize to zero to avoid writing uninitialized data to socket */
3789 memset(&info, 0, sizeof(info));
3790 info.byte_order = local_byte_order;
3791 info.protocol_version = 0;
3792 info.protocol_style = XmDRAG_DYNAMIC;
3793 info.proxy_window = None;
3794 info.num_drop_sites = 0;
3795 info.total_size = sizeof(info);
3797 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3798 GDK_WINDOW_XID (window),
3799 motif_drag_receiver_info_atom,
3800 motif_drag_receiver_info_atom,
3807 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3808 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3809 GDK_WINDOW_XID (window),
3810 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3811 XA_ATOM, 32, PropModeReplace,
3812 (guchar *)&xdnd_version, 1);
3816 gdk_x11_drag_context_get_selection (GdkDragContext *context)
3818 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3819 return gdk_x11_xatom_to_atom_for_display (GDK_WINDOW_DISPLAY (context->source_window),
3820 (GDK_X11_DRAG_CONTEXT (context))->motif_selection);
3821 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3822 return gdk_atom_intern_static_string ("XdndSelection");
3828 gdk_x11_drag_context_drop_status (GdkDragContext *context)
3830 return ! GDK_X11_DRAG_CONTEXT (context)->drop_failed;