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 "gdkdndprivate.h"
34 #include "gdkproperty.h"
35 #include "gdkprivate-x11.h"
36 #include "gdkinternals.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 #include <X11/extensions/Xcomposite.h>
50 GDK_DRAG_STATUS_MOTION_WAIT,
51 GDK_DRAG_STATUS_ACTION_WAIT,
57 gint x, y, width, height;
59 gboolean shape_selected;
61 cairo_region_t *shape;
66 GHashTable *child_hash;
71 #define GDK_TYPE_DRAG_CONTEXT_X11 (gdk_drag_context_x11_get_type ())
72 #define GDK_DRAG_CONTEXT_X11(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_DRAG_CONTEXT_X11, GdkDragContextX11))
74 typedef struct _GdkDragContextX11 GdkDragContextX11;
75 typedef struct _GdkDragContextClass GdkDragContextX11Class;
77 struct _GdkDragContextX11 {
78 GdkDragContext context;
83 guint16 last_x; /* Coordinates from last event */
85 GdkDragAction old_action; /* The last action we sent to the source */
86 GdkDragAction old_actions; /* The last actions we sent to the source */
87 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
89 Window dest_xid; /* The last window we looked up */
90 Window drop_xid; /* The (non-proxied) window that is receiving drops */
91 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
92 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
93 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
94 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
95 guint drag_status : 4; /* current status of drag */
97 guint drop_failed : 1; /* Whether the drop was unsuccessful */
98 guint version; /* Xdnd protocol version */
100 GSList *window_caches;
103 /* Forward declarations */
105 static void gdk_window_cache_destroy (GdkWindowCache *cache);
107 static void motif_read_target_table (GdkDisplay *display);
109 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
113 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
116 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
119 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
122 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
125 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
128 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
132 static void xdnd_manage_source_filter (GdkDragContext *context,
134 gboolean add_filter);
136 static GList *contexts;
138 static const struct {
139 const char *atom_name;
142 { "XdndEnter", xdnd_enter_filter },
143 { "XdndLeave", xdnd_leave_filter },
144 { "XdndPosition", xdnd_position_filter },
145 { "XdndStatus", xdnd_status_filter },
146 { "XdndFinished", xdnd_finished_filter },
147 { "XdndDrop", xdnd_drop_filter },
151 G_DEFINE_TYPE (GdkDragContextX11, gdk_drag_context_x11, GDK_TYPE_DRAG_CONTEXT)
154 gdk_drag_context_x11_init (GdkDragContextX11 *context)
156 contexts = g_list_prepend (contexts, context);
159 static void gdk_drag_context_x11_finalize (GObject *object);
160 static GdkWindow * gdk_drag_context_x11_find_window (GdkDragContext *context,
161 GdkWindow *drag_window,
165 GdkDragProtocol *protocol);
166 static gboolean gdk_drag_context_x11_drag_motion (GdkDragContext *context,
167 GdkWindow *dest_window,
168 GdkDragProtocol protocol,
171 GdkDragAction suggested_action,
172 GdkDragAction possible_actions,
174 static void gdk_drag_context_x11_drag_status (GdkDragContext *context,
175 GdkDragAction action,
177 static void gdk_drag_context_x11_drag_abort (GdkDragContext *context,
179 static void gdk_drag_context_x11_drag_drop (GdkDragContext *context,
181 static void gdk_drag_context_x11_drop_reply (GdkDragContext *context,
184 static void gdk_drag_context_x11_drop_finish (GdkDragContext *context,
187 static gboolean gdk_drag_context_x11_drop_status (GdkDragContext *context);
188 static GdkAtom gdk_drag_context_x11_get_selection (GdkDragContext *context);
191 gdk_drag_context_x11_class_init (GdkDragContextX11Class *klass)
193 GObjectClass *object_class = G_OBJECT_CLASS (klass);
194 GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
196 object_class->finalize = gdk_drag_context_x11_finalize;
198 context_class->find_window = gdk_drag_context_x11_find_window;
199 context_class->drag_status = gdk_drag_context_x11_drag_status;
200 context_class->drag_motion = gdk_drag_context_x11_drag_motion;
201 context_class->drag_abort = gdk_drag_context_x11_drag_abort;
202 context_class->drag_drop = gdk_drag_context_x11_drag_drop;
203 context_class->drop_reply = gdk_drag_context_x11_drop_reply;
204 context_class->drop_finish = gdk_drag_context_x11_drop_finish;
205 context_class->drop_status = gdk_drag_context_x11_drop_status;
206 context_class->get_selection = gdk_drag_context_x11_get_selection;
210 gdk_drag_context_x11_finalize (GObject *object)
212 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (object);
213 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
215 if (context->source_window)
217 if ((context->protocol == GDK_DRAG_PROTO_XDND) && !context->is_source)
218 xdnd_manage_source_filter (context, context->source_window, FALSE);
221 g_slist_free_full (context_x11->window_caches, (GDestroyNotify)gdk_window_cache_destroy);
223 contexts = g_list_remove (contexts, context);
225 G_OBJECT_CLASS (gdk_drag_context_x11_parent_class)->finalize (object);
230 static GdkDragContext *
231 gdk_drag_context_find (GdkDisplay *display,
236 GList *tmp_list = contexts;
237 GdkDragContext *context;
238 GdkDragContextX11 *context_x11;
239 Window context_dest_xid;
243 context = (GdkDragContext *)tmp_list->data;
244 context_x11 = (GdkDragContextX11 *)context;
246 if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
247 (context->dest_window && gdk_window_get_display (context->dest_window) != display))
250 context_dest_xid = context->dest_window
251 ? (context_x11->drop_xid
252 ? context_x11->drop_xid
253 : GDK_WINDOW_XID (context->dest_window))
256 if ((!context->is_source == !is_source) &&
257 ((source_xid == None) || (context->source_window &&
258 (GDK_WINDOW_XID (context->source_window) == source_xid))) &&
259 ((dest_xid == None) || (context_dest_xid == dest_xid)))
262 tmp_list = tmp_list->next;
269 precache_target_list (GdkDragContext *context)
271 if (context->targets)
273 GPtrArray *targets = g_ptr_array_new ();
277 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
278 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
280 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
281 (const gchar **)targets->pdata,
284 for (i =0; i < targets->len; i++)
285 g_free (targets->pdata[i]);
287 g_ptr_array_free (targets, TRUE);
291 /* Utility functions */
294 free_cache_child (GdkCacheChild *child,
298 cairo_region_destroy (child->shape);
300 if (child->shape_selected && display)
302 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
304 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
311 gdk_window_cache_add (GdkWindowCache *cache,
313 gint x, gint y, gint width, gint height,
316 GdkCacheChild *child = g_new (GdkCacheChild, 1);
321 child->width = width;
322 child->height = height;
323 child->mapped = mapped;
324 child->shape_selected = FALSE;
325 child->shape_valid = FALSE;
328 cache->children = g_list_prepend (cache->children, child);
329 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
333 static GdkFilterReturn
334 gdk_window_cache_shape_filter (GdkXEvent *xev,
338 XEvent *xevent = (XEvent *)xev;
339 GdkWindowCache *cache = data;
341 GdkDisplayX11 *display = GDK_DISPLAY_X11 (gdk_screen_get_display (cache->screen));
343 if (display->have_shapes &&
344 xevent->type == display->shape_event_base + ShapeNotify)
346 XShapeEvent *xse = (XShapeEvent*)xevent;
349 node = g_hash_table_lookup (cache->child_hash,
350 GUINT_TO_POINTER (xse->window));
353 GdkCacheChild *child = node->data;
354 child->shape_valid = FALSE;
357 cairo_region_destroy (child->shape);
362 return GDK_FILTER_REMOVE;
365 return GDK_FILTER_CONTINUE;
368 static GdkFilterReturn
369 gdk_window_cache_filter (GdkXEvent *xev,
373 XEvent *xevent = (XEvent *)xev;
374 GdkWindowCache *cache = data;
376 switch (xevent->type)
378 case CirculateNotify:
380 case ConfigureNotify:
382 XConfigureEvent *xce = &xevent->xconfigure;
385 node = g_hash_table_lookup (cache->child_hash,
386 GUINT_TO_POINTER (xce->window));
389 GdkCacheChild *child = node->data;
392 child->width = xce->width;
393 child->height = xce->height;
394 if (xce->above == None && (node->next))
396 GList *last = g_list_last (cache->children);
397 cache->children = g_list_remove_link (cache->children, node);
404 GList *above_node = g_hash_table_lookup (cache->child_hash,
405 GUINT_TO_POINTER (xce->above));
406 if (above_node && node->next != above_node)
408 /* Put the window above (before in the list) above_node
410 cache->children = g_list_remove_link (cache->children, node);
411 node->prev = above_node->prev;
413 node->prev->next = node;
415 cache->children = node;
416 node->next = above_node;
417 above_node->prev = node;
425 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
427 if (!g_hash_table_lookup (cache->child_hash,
428 GUINT_TO_POINTER (xcwe->window)))
429 gdk_window_cache_add (cache, xcwe->window,
430 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
436 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
439 node = g_hash_table_lookup (cache->child_hash,
440 GUINT_TO_POINTER (xdwe->window));
443 GdkCacheChild *child = node->data;
445 g_hash_table_remove (cache->child_hash,
446 GUINT_TO_POINTER (xdwe->window));
447 cache->children = g_list_remove_link (cache->children, node);
448 /* window is destroyed, no need to disable ShapeNotify */
449 free_cache_child (child, NULL);
450 g_list_free_1 (node);
456 XMapEvent *xme = &xevent->xmap;
459 node = g_hash_table_lookup (cache->child_hash,
460 GUINT_TO_POINTER (xme->window));
463 GdkCacheChild *child = node->data;
464 child->mapped = TRUE;
472 XMapEvent *xume = &xevent->xmap;
475 node = g_hash_table_lookup (cache->child_hash,
476 GUINT_TO_POINTER (xume->window));
479 GdkCacheChild *child = node->data;
480 child->mapped = FALSE;
485 return GDK_FILTER_CONTINUE;
487 return GDK_FILTER_REMOVE;
490 static GdkWindowCache *
491 gdk_window_cache_new (GdkScreen *screen)
493 XWindowAttributes xwa;
494 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
495 GdkWindow *root_window = gdk_screen_get_root_window (screen);
496 GdkChildInfoX11 *children;
500 GdkWindowCache *result = g_new (GdkWindowCache, 1);
502 result->children = NULL;
503 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
504 result->screen = screen;
506 XGetWindowAttributes (xdisplay, GDK_WINDOW_XID (root_window), &xwa);
507 result->old_event_mask = xwa.your_event_mask;
509 if (G_UNLIKELY (!GDK_DISPLAY_X11 (GDK_SCREEN_X11 (screen)->display)->trusted_client))
511 GList *toplevel_windows, *list;
513 gint x, y, width, height;
515 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
516 for (list = toplevel_windows; list; list = list->next) {
517 window = GDK_WINDOW (list->data);
518 gdk_window_get_geometry (window, &x, &y, &width, &height);
519 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
521 gdk_window_is_visible (window));
523 g_list_free (toplevel_windows);
527 XSelectInput (xdisplay, GDK_WINDOW_XID (root_window),
528 result->old_event_mask | SubstructureNotifyMask);
529 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
530 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
532 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
533 GDK_WINDOW_XID (root_window),
535 &children, &nchildren))
538 for (i = 0; i < nchildren ; i++)
540 gdk_window_cache_add (result, children[i].window,
541 children[i].x, children[i].y, children[i].width, children[i].height,
542 children[i].is_mapped);
547 #ifdef HAVE_XCOMPOSITE
549 * Add the composite overlay window to the cache, as this can be a reasonable
550 * Xdnd proxy as well.
551 * This is only done when the screen is composited in order to avoid mapping
552 * the COW. We assume that the CM is using the COW (which is true for pretty
553 * much any CM currently in use).
555 if (gdk_screen_is_composited (screen))
557 cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
558 gdk_window_cache_add (result, cow, 0, 0, gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
559 XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
567 gdk_window_cache_destroy (GdkWindowCache *cache)
569 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
572 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
573 GDK_WINDOW_XID (root_window),
574 cache->old_event_mask);
575 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
576 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
578 display = gdk_screen_get_display (cache->screen);
580 gdk_x11_display_error_trap_push (display);
581 g_list_foreach (cache->children, (GFunc)free_cache_child, display);
582 gdk_x11_display_error_trap_pop_ignored (display);
584 g_list_free (cache->children);
585 g_hash_table_destroy (cache->child_hash);
591 is_pointer_within_shape (GdkDisplay *display,
592 GdkCacheChild *child,
596 if (!child->shape_selected)
598 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
600 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
601 child->shape_selected = TRUE;
603 if (!child->shape_valid)
605 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
606 cairo_region_t *input_shape;
608 child->shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
609 child->xid, ShapeBounding);
611 input_shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
612 child->xid, ShapeInput);
613 if (child->shape && input_shape)
615 cairo_region_intersect (child->shape, input_shape);
616 cairo_region_destroy (input_shape);
618 else if (input_shape)
620 child->shape = input_shape;
624 child->shape_valid = TRUE;
627 return child->shape == NULL ||
628 cairo_region_contains_point (child->shape, x_pos, y_pos);
632 get_client_window_at_coords_recurse (GdkDisplay *display,
634 gboolean is_toplevel,
638 GdkChildInfoX11 *children;
639 unsigned int nchildren;
641 gboolean found_child = FALSE;
642 GdkChildInfoX11 child = { 0, };
643 gboolean has_wm_state = FALSE;
645 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
646 is_toplevel? &has_wm_state : NULL,
647 &children, &nchildren))
657 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
659 GdkChildInfoX11 *cur_child = &children[i];
661 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
662 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
663 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
676 if (child.has_wm_state)
679 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
686 get_client_window_at_coords (GdkWindowCache *cache,
692 Window retval = None;
695 display = gdk_screen_get_display (cache->screen);
697 gdk_x11_display_error_trap_push (display);
699 tmp_list = cache->children;
701 while (tmp_list && !retval)
703 GdkCacheChild *child = tmp_list->data;
705 if ((child->xid != ignore) && (child->mapped))
707 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
708 (y_root >= child->y) && (y_root < child->y + child->height))
710 if (!is_pointer_within_shape (display, child,
714 tmp_list = tmp_list->next;
718 retval = get_client_window_at_coords_recurse (display,
726 tmp_list = tmp_list->next;
729 gdk_x11_display_error_trap_pop_ignored (display);
734 return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
737 /*************************************************************
738 ***************************** MOTIF *************************
739 *************************************************************/
741 /* values used in the message type for Motif DND */
754 /* Values used to specify type of protocol to use */
758 XmDRAG_PREFER_PREREGISTER,
760 XmDRAG_PREFER_DYNAMIC,
762 XmDRAG_PREFER_RECEIVER
765 /* Operation codes */
773 /* Drop site status */
775 XmNO_DROP_SITE = 0x01,
776 XmDROP_SITE_INVALID = 0x02,
777 XmDROP_SITE_VALID = 0x03
780 /* completion status */
788 /* Byte swapping routines. The motif specification leaves it
789 * up to us to save a few bytes in the client messages
791 static gchar local_byte_order = '\0';
793 #ifdef G_ENABLE_DEBUG
795 print_target_list (GList *targets)
799 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
800 g_message ("\t%s", name);
802 targets = targets->next;
805 #endif /* G_ENABLE_DEBUG */
808 init_byte_order (void)
810 guint32 myint = 0x01020304;
811 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
815 card16_to_host (guint16 x, gchar byte_order) {
816 if (byte_order == local_byte_order)
819 return (x << 8) | (x >> 8);
823 card32_to_host (guint32 x, gchar byte_order) {
824 if (byte_order == local_byte_order)
827 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
830 /* Motif packs together fields of varying length into the
831 * client message. We can't rely on accessing these
832 * through data.s[], data.l[], etc, because on some architectures
833 * (i.e., Alpha) these won't be valid for format == 8.
836 #define MOTIF_XCLIENT_BYTE(xevent,i) \
837 (xevent)->xclient.data.b[i]
838 #define MOTIF_XCLIENT_SHORT(xevent,i) \
839 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
840 #define MOTIF_XCLIENT_LONG(xevent,i) \
841 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
843 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
844 #define MOTIF_UNPACK_SHORT(xevent,i) \
845 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
846 #define MOTIF_UNPACK_LONG(xevent,i) \
847 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
849 /***** Dest side ***********/
851 /* Property placed on source windows */
852 typedef struct _MotifDragInitiatorInfo {
854 guint8 protocol_version;
855 guint16 targets_index;
856 guint32 selection_atom;
857 } MotifDragInitiatorInfo;
859 /* Header for target table on the drag window */
860 typedef struct _MotifTargetTableHeader {
862 guchar protocol_version;
865 } MotifTargetTableHeader;
867 /* Property placed on target windows */
868 typedef struct _MotifDragReceiverInfo {
870 guint8 protocol_version;
871 guint8 protocol_style;
873 guint32 proxy_window;
874 guint16 num_drop_sites;
877 } MotifDragReceiverInfo;
879 /* Target table handling */
881 static GdkFilterReturn
882 motif_drag_window_filter (GdkXEvent *xevent,
886 XEvent *xev = (XEvent *)xevent;
887 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
888 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
890 switch (xev->xany.type)
893 display_x11->motif_drag_window = None;
894 display_x11->motif_drag_gdk_window = NULL;
897 if (display_x11->motif_target_lists &&
898 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
899 motif_read_target_table (display);
902 return GDK_FILTER_REMOVE;
906 motif_lookup_drag_window (GdkDisplay *display,
907 Display *lookup_xdisplay)
909 Window retval = None;
910 gulong bytes_after, nitems;
915 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
916 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
918 XA_WINDOW, &type, &format, &nitems, &bytes_after,
921 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
923 retval = *(Window *)data;
925 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
934 /* Finds the window where global Motif drag information is stored.
935 * If it doesn't exist and 'create' is TRUE, create one.
938 motif_find_drag_window (GdkDisplay *display,
941 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
943 if (!display_x11->motif_drag_window)
945 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
946 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
948 if (!display_x11->motif_drag_window && create)
950 /* Create a persistant window. (Copied from LessTif) */
952 Display *persistant_xdisplay;
953 XSetWindowAttributes attr;
954 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
955 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
957 XGrabServer (persistant_xdisplay);
959 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
961 if (!display_x11->motif_drag_window)
963 attr.override_redirect = True;
964 attr.event_mask = PropertyChangeMask;
966 display_x11->motif_drag_window =
967 XCreateWindow (persistant_xdisplay,
968 RootWindow (persistant_xdisplay, 0),
969 -100, -100, 10, 10, 0, 0,
970 InputOnly, (Visual *)CopyFromParent,
971 (CWOverrideRedirect | CWEventMask), &attr);
974 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
976 XChangeProperty (persistant_xdisplay,
977 RootWindow (persistant_xdisplay, 0),
978 motif_drag_window_atom, XA_WINDOW,
980 (guchar *)&motif_drag_window_atom, 1);
983 XUngrabServer (persistant_xdisplay);
984 XCloseDisplay (persistant_xdisplay);
987 /* There is a miniscule race condition here if the drag window
988 * gets destroyed exactly now.
990 if (display_x11->motif_drag_window)
992 display_x11->motif_drag_gdk_window =
993 gdk_x11_window_foreign_new_for_display (display, display_x11->motif_drag_window);
994 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
995 motif_drag_window_filter,
1000 return display_x11->motif_drag_window;
1004 motif_read_target_table (GdkDisplay *display)
1006 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1007 gulong bytes_after, nitems;
1012 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1014 if (display_x11->motif_target_lists)
1016 for (i=0; i<display_x11->motif_n_target_lists; i++)
1017 g_list_free (display_x11->motif_target_lists[i]);
1019 g_free (display_x11->motif_target_lists);
1020 display_x11->motif_target_lists = NULL;
1021 display_x11->motif_n_target_lists = 0;
1024 if (motif_find_drag_window (display, FALSE))
1027 MotifTargetTableHeader *header = NULL;
1028 guchar *target_bytes = NULL;
1030 gboolean success = FALSE;
1032 gdk_x11_display_error_trap_push (display);
1033 XGetWindowProperty (display_x11->xdisplay,
1034 display_x11->motif_drag_window,
1035 motif_drag_targets_atom,
1036 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1037 motif_drag_targets_atom,
1038 &type, &format, &nitems, &bytes_after,
1041 if (gdk_x11_display_error_trap_pop (display) ||
1042 (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1045 header = (MotifTargetTableHeader *)data;
1047 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1048 header->total_size = card32_to_host (header->total_size, header->byte_order);
1050 gdk_x11_display_error_trap_push (display);
1051 XGetWindowProperty (display_x11->xdisplay,
1052 display_x11->motif_drag_window,
1053 motif_drag_targets_atom,
1054 (sizeof(MotifTargetTableHeader)+3)/4,
1055 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1057 motif_drag_targets_atom, &type, &format, &nitems,
1058 &bytes_after, &target_bytes);
1060 if (gdk_x11_display_error_trap_pop (display) ||
1061 (format != 8) || (bytes_after != 0) ||
1062 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1065 display_x11->motif_n_target_lists = header->n_lists;
1066 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1069 for (i=0; i<header->n_lists; i++)
1074 if (p + sizeof(guint16) - target_bytes > nitems)
1077 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1079 /* We need to make a copy of the targets, since it may
1082 targets = g_new (guint32, n_targets);
1083 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1085 p += sizeof(guint16) + n_targets * sizeof(guint32);
1086 if (p - target_bytes > nitems)
1089 for (j=0; j<n_targets; j++)
1090 display_x11->motif_target_lists[i] =
1091 g_list_prepend (display_x11->motif_target_lists[i],
1092 GUINT_TO_POINTER (card32_to_host (targets[j],
1093 header->byte_order)));
1095 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1105 XFree (target_bytes);
1109 if (display_x11->motif_target_lists)
1111 g_free (display_x11->motif_target_lists);
1112 display_x11->motif_target_lists = NULL;
1113 display_x11->motif_n_target_lists = 0;
1115 g_warning ("Error reading Motif target table\n");
1121 targets_sort_func (gconstpointer a, gconstpointer b)
1123 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1124 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1127 /* Check if given (sorted) list is in the targets table */
1129 motif_target_table_check (GdkDisplay *display,
1132 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1133 GList *tmp_list1, *tmp_list2;
1136 for (i=0; i<display_x11->motif_n_target_lists; i++)
1138 tmp_list1 = display_x11->motif_target_lists[i];
1141 while (tmp_list1 && tmp_list2)
1143 if (tmp_list1->data != tmp_list2->data)
1146 tmp_list1 = tmp_list1->next;
1147 tmp_list2 = tmp_list2->next;
1149 if (!tmp_list1 && !tmp_list2) /* Found it */
1157 motif_add_to_target_table (GdkDisplay *display,
1158 GList *targets) /* targets is list of GdkAtom */
1160 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1161 GList *sorted = NULL;
1166 /* make a sorted copy of the list */
1170 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1171 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1172 targets = targets->next;
1175 /* First check if it is there already */
1177 if (display_x11->motif_target_lists)
1178 index = motif_target_table_check (display, sorted);
1180 /* We need to grab the server while doing this, to ensure
1186 /* We need to make sure that it exists _before_ we grab the
1187 * server, since we can't open a new connection after we
1190 motif_find_drag_window (display, TRUE);
1192 gdk_x11_display_grab (display);
1193 motif_read_target_table (display);
1195 /* Check again, in case it was added in the meantime */
1197 if (display_x11->motif_target_lists)
1198 index = motif_target_table_check (display, sorted);
1202 guint32 total_size = 0;
1206 MotifTargetTableHeader *header;
1208 if (!display_x11->motif_target_lists)
1210 display_x11->motif_target_lists = g_new (GList *, 1);
1211 display_x11->motif_n_target_lists = 1;
1215 display_x11->motif_n_target_lists++;
1216 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1217 sizeof(GList *) * display_x11->motif_n_target_lists);
1219 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1221 index = display_x11->motif_n_target_lists - 1;
1223 total_size = sizeof (MotifTargetTableHeader);
1224 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1225 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1227 data = g_malloc (total_size);
1229 header = (MotifTargetTableHeader *)data;
1230 p = data + sizeof(MotifTargetTableHeader);
1232 header->byte_order = local_byte_order;
1233 header->protocol_version = 0;
1234 header->n_lists = display_x11->motif_n_target_lists;
1235 header->total_size = total_size;
1237 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1239 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1240 guint32 *targets = g_new (guint32, n_targets);
1241 guint32 *p32 = targets;
1243 tmp_list = display_x11->motif_target_lists[i];
1246 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1248 tmp_list = tmp_list->next;
1253 p += sizeof(guint16);
1255 memcpy (p, targets, n_targets * sizeof(guint32));
1258 p += sizeof(guint32) * n_targets;
1262 XChangeProperty (display_x11->xdisplay,
1263 display_x11->motif_drag_window,
1264 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1265 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1269 gdk_x11_display_ungrab (display);
1272 g_list_free (sorted);
1276 /* Translate flags */
1279 motif_dnd_translate_flags (GdkDragContextX11 *context_x11,
1282 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1283 guint recommended_op = flags & 0x000f;
1284 guint possible_ops = (flags & 0x0f0) >> 4;
1286 switch (recommended_op)
1289 context->suggested_action = GDK_ACTION_MOVE;
1292 context->suggested_action = GDK_ACTION_COPY;
1295 context->suggested_action = GDK_ACTION_LINK;
1298 context->suggested_action = GDK_ACTION_COPY;
1302 context->actions = 0;
1303 if (possible_ops & XmDROP_MOVE)
1304 context->actions |= GDK_ACTION_MOVE;
1305 if (possible_ops & XmDROP_COPY)
1306 context->actions |= GDK_ACTION_COPY;
1307 if (possible_ops & XmDROP_LINK)
1308 context->actions |= GDK_ACTION_LINK;
1312 motif_dnd_get_flags (GdkDragContext *context)
1316 switch (context->suggested_action)
1318 case GDK_ACTION_MOVE:
1319 flags = XmDROP_MOVE;
1321 case GDK_ACTION_COPY:
1322 flags = XmDROP_COPY;
1324 case GDK_ACTION_LINK:
1325 flags = XmDROP_LINK;
1328 flags = XmDROP_NOOP;
1332 if (context->actions & GDK_ACTION_MOVE)
1333 flags |= XmDROP_MOVE << 8;
1334 if (context->actions & GDK_ACTION_COPY)
1335 flags |= XmDROP_COPY << 8;
1336 if (context->actions & GDK_ACTION_LINK)
1337 flags |= XmDROP_LINK << 8;
1345 motif_set_targets (GdkDragContextX11 *context_x11)
1347 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1348 MotifDragInitiatorInfo info;
1350 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1352 info.byte_order = local_byte_order;
1353 info.protocol_version = 0;
1354 info.targets_index = motif_add_to_target_table (display, context->targets);
1359 g_snprintf (buf, 20, "_GDK_SELECTION_%d", i);
1361 context_x11->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1362 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), context_x11->motif_selection))
1366 info.selection_atom = context_x11->motif_selection;
1368 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1369 GDK_WINDOW_XID (context->source_window),
1370 context_x11->motif_selection,
1371 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1373 (guchar *)&info, sizeof (info));
1375 context_x11->motif_targets_set = 1;
1379 motif_check_dest (GdkDisplay *display,
1382 gboolean retval = FALSE;
1384 MotifDragReceiverInfo *info;
1387 unsigned long nitems, after;
1388 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1390 gdk_x11_display_error_trap_push (display);
1391 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1392 motif_drag_receiver_info_atom,
1393 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1394 &type, &format, &nitems, &after,
1397 if (gdk_x11_display_error_trap_pop (display) == 0)
1401 info = (MotifDragReceiverInfo *)data;
1403 if ((format == 8) && (nitems == sizeof(*info)))
1405 if ((info->protocol_version == 0) &&
1406 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1407 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1408 (info->protocol_style == XmDRAG_DYNAMIC)))
1414 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1421 return retval ? win : None;
1425 motif_send_enter (GdkDragContextX11 *context_x11,
1428 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1429 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1432 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1433 return; /* Motif Dnd requires getting properties on the root window */
1435 xev.xclient.type = ClientMessage;
1436 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1437 xev.xclient.format = 8;
1438 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1440 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1441 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1442 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1443 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1444 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XID (context->source_window);
1446 if (!context_x11->motif_targets_set)
1447 motif_set_targets (context_x11);
1449 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1450 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1452 if (!_gdk_send_xevent (display,
1453 GDK_WINDOW_XID (context->dest_window),
1456 g_message ("Send event to %lx failed",
1457 GDK_WINDOW_XID (context->dest_window)));
1461 motif_send_leave (GdkDragContextX11 *context_x11,
1464 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1465 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1468 xev.xclient.type = ClientMessage;
1469 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1470 xev.xclient.format = 8;
1471 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1473 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1474 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1475 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1476 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1477 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1478 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1479 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1481 if (!_gdk_send_xevent (display,
1482 GDK_WINDOW_XID (context->dest_window),
1485 g_message ("Send event to %lx failed",
1486 GDK_WINDOW_XID (context->dest_window)));
1490 motif_send_motion (GdkDragContextX11 *context_x11,
1493 GdkDragAction action,
1496 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1497 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1501 xev.xclient.type = ClientMessage;
1502 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1503 xev.xclient.format = 8;
1504 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1506 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1507 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1508 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1509 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1510 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1512 if ((context->suggested_action != context_x11->old_action) ||
1513 (context->actions != context_x11->old_actions))
1515 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1517 /* context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1522 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1524 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1525 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1527 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1531 if (!_gdk_send_xevent (display,
1532 GDK_WINDOW_XID (context->dest_window),
1535 g_message ("Send event to %lx failed",
1536 GDK_WINDOW_XID (context->dest_window)));
1542 motif_send_drop (GdkDragContextX11 *context_x11,
1545 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1546 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1549 xev.xclient.type = ClientMessage;
1550 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1551 xev.xclient.format = 8;
1552 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1554 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1555 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1556 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1557 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1559 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
1560 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
1562 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1563 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XID (context->source_window);
1565 if (!_gdk_send_xevent (display,
1566 GDK_WINDOW_XID (context->dest_window),
1569 g_message ("Send event to %lx failed",
1570 GDK_WINDOW_XID (context->dest_window)));
1576 motif_read_initiator_info (GdkDisplay *display,
1577 Window source_window,
1588 MotifDragInitiatorInfo *initiator_info;
1590 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1592 gdk_x11_display_error_trap_push (display);
1593 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1594 0, sizeof(*initiator_info), FALSE,
1595 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1596 &type, &format, &nitems, &bytes_after,
1599 if (gdk_x11_display_error_trap_pop (display) ||
1600 (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) ||
1603 g_warning ("Error reading initiator info\n");
1607 initiator_info = (MotifDragInitiatorInfo *)data;
1609 motif_read_target_table (display);
1611 initiator_info->targets_index =
1612 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1613 initiator_info->selection_atom =
1614 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1616 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1618 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1619 XFree (initiator_info);
1623 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1628 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1629 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1630 tmp_list = tmp_list->prev;
1633 #ifdef G_ENABLE_DEBUG
1634 if (_gdk_debug_flags & GDK_DEBUG_DND)
1635 print_target_list (*targets);
1636 #endif /* G_ENABLE_DEBUG */
1638 *selection = initiator_info->selection_atom;
1640 XFree (initiator_info);
1645 static GdkDragContext *
1646 motif_drag_context_new (GdkWindow *dest_window,
1648 guint32 source_window,
1651 GdkDragContextX11 *context_x11;
1652 GdkDragContext *context;
1653 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
1654 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1656 /* FIXME, current_dest_drag really shouldn't be NULL'd
1657 * if we error below.
1659 if (display_x11->current_dest_drag != NULL)
1661 if (timestamp >= display_x11->current_dest_drag->start_time)
1663 g_object_unref (display_x11->current_dest_drag);
1664 display_x11->current_dest_drag = NULL;
1670 context_x11 = g_object_new (GDK_TYPE_DRAG_CONTEXT_X11, NULL);
1671 context = GDK_DRAG_CONTEXT (context_x11);
1673 context->protocol = GDK_DRAG_PROTO_MOTIF;
1674 context->is_source = FALSE;
1676 context->source_window = gdk_x11_window_lookup_for_display (display, source_window);
1677 if (context->source_window)
1678 g_object_ref (context->source_window);
1681 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
1682 if (!context->source_window)
1684 g_object_unref (context_x11);
1689 context->dest_window = dest_window;
1690 g_object_ref (dest_window);
1691 context->start_time = timestamp;
1693 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1697 &context_x11->motif_selection))
1699 g_object_unref (context_x11);
1707 * The MOTIF drag protocol has no real provisions for distinguishing
1708 * multiple simultaneous drops. If the sources grab the pointer
1709 * when doing drags, that shouldn't happen, in any case. If it
1710 * does, we can't do much except hope for the best.
1713 static GdkFilterReturn
1714 motif_top_level_enter (GdkEvent *event,
1717 guint32 source_window,
1720 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
1721 GdkDragContext *new_context;
1723 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1724 flags, timestamp, source_window, atom));
1726 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1728 return GDK_FILTER_REMOVE;
1730 event->dnd.type = GDK_DRAG_ENTER;
1731 event->dnd.context = new_context;
1732 g_object_ref (new_context);
1734 display_x11->current_dest_drag = new_context;
1736 return GDK_FILTER_TRANSLATE;
1739 static GdkFilterReturn
1740 motif_top_level_leave (GdkEvent *event,
1744 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
1746 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1749 if ((display_x11->current_dest_drag != NULL) &&
1750 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1751 (timestamp >= display_x11->current_dest_drag->start_time))
1753 event->dnd.type = GDK_DRAG_LEAVE;
1754 /* Pass ownership of context to the event */
1755 event->dnd.context = display_x11->current_dest_drag;
1757 display_x11->current_dest_drag = NULL;
1759 return GDK_FILTER_TRANSLATE;
1762 return GDK_FILTER_REMOVE;
1765 static GdkFilterReturn
1766 motif_motion (GdkEvent *event,
1772 GdkDragContextX11 *context_x11;
1773 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
1775 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1776 flags, timestamp, x_root, y_root));
1778 if ((display_x11->current_dest_drag != NULL) &&
1779 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1780 (timestamp >= display_x11->current_dest_drag->start_time))
1782 context_x11 = GDK_DRAG_CONTEXT_X11 (display_x11->current_dest_drag);
1784 event->dnd.type = GDK_DRAG_MOTION;
1785 event->dnd.context = display_x11->current_dest_drag;
1786 g_object_ref (display_x11->current_dest_drag);
1788 event->dnd.time = timestamp;
1790 motif_dnd_translate_flags (context_x11, flags);
1792 event->dnd.x_root = x_root;
1793 event->dnd.y_root = y_root;
1795 context_x11->last_x = x_root;
1796 context_x11->last_y = y_root;
1798 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1800 return GDK_FILTER_TRANSLATE;
1803 return GDK_FILTER_REMOVE;
1806 static GdkFilterReturn
1807 motif_operation_changed (GdkEvent *event,
1811 GdkDragContextX11 *context_x11;
1812 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
1813 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1816 if ((display_x11->current_dest_drag != NULL) &&
1817 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1818 (timestamp >= display_x11->current_dest_drag->start_time))
1820 event->dnd.type = GDK_DRAG_MOTION;
1821 event->dnd.send_event = FALSE;
1822 event->dnd.context = display_x11->current_dest_drag;
1823 g_object_ref (display_x11->current_dest_drag);
1825 event->dnd.time = timestamp;
1826 context_x11 = GDK_DRAG_CONTEXT_X11 (display_x11->current_dest_drag);
1828 motif_dnd_translate_flags (context_x11, flags);
1830 event->dnd.x_root = context_x11->last_x;
1831 event->dnd.y_root = context_x11->last_y;
1833 context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1835 return GDK_FILTER_TRANSLATE;
1838 return GDK_FILTER_REMOVE;
1841 static GdkFilterReturn
1842 motif_drop_start (GdkEvent *event,
1845 guint32 source_window,
1850 GdkDragContext *new_context;
1851 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
1853 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1854 flags, timestamp, x_root, y_root, source_window, atom));
1856 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1858 return GDK_FILTER_REMOVE;
1860 motif_dnd_translate_flags (GDK_DRAG_CONTEXT_X11 (new_context), flags);
1862 event->dnd.type = GDK_DROP_START;
1863 event->dnd.context = new_context;
1864 event->dnd.time = timestamp;
1865 event->dnd.x_root = x_root;
1866 event->dnd.y_root = y_root;
1868 gdk_x11_window_set_user_time (event->any.window, timestamp);
1870 g_object_ref (new_context);
1871 display_x11->current_dest_drag = new_context;
1873 return GDK_FILTER_TRANSLATE;
1876 static GdkFilterReturn
1877 motif_drag_status (GdkEvent *event,
1881 GdkDragContext *context;
1882 GdkDisplay *display;
1885 g_message ("Motif status message: flags %x", flags));
1887 display = gdk_window_get_display (event->any.window);
1889 return GDK_FILTER_REMOVE;
1891 context = gdk_drag_context_find (display, TRUE, GDK_WINDOW_XID (event->any.window), None);
1895 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
1896 if ((context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1897 (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1898 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
1900 event->dnd.type = GDK_DRAG_STATUS;
1901 event->dnd.send_event = FALSE;
1902 event->dnd.context = context;
1903 g_object_ref (context);
1905 event->dnd.time = timestamp;
1907 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1909 switch (flags & 0x000f)
1912 context->action = 0;
1915 context->action = GDK_ACTION_MOVE;
1918 context->action = GDK_ACTION_COPY;
1921 context->action = GDK_ACTION_LINK;
1926 context->action = 0;
1928 return GDK_FILTER_TRANSLATE;
1930 return GDK_FILTER_REMOVE;
1933 static GdkFilterReturn
1934 motif_dnd_filter (GdkXEvent *xev,
1938 XEvent *xevent = (XEvent *)xev;
1943 guint32 source_window;
1945 gint16 x_root, y_root;
1948 if (!event->any.window ||
1949 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1950 return GDK_FILTER_CONTINUE; /* Not for us */
1952 /* First read some fields common to all Motif DND messages */
1954 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1955 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1956 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1958 is_reply = ((reason & 0x80) != 0);
1960 switch (reason & 0x7f)
1962 case XmTOP_LEVEL_ENTER:
1963 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1964 atom = MOTIF_UNPACK_LONG (xevent, 3);
1965 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1966 case XmTOP_LEVEL_LEAVE:
1967 return motif_top_level_leave (event, flags, timestamp);
1970 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1971 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1974 return motif_motion (event, flags, timestamp, x_root, y_root);
1976 return motif_drag_status (event, flags, timestamp);
1978 case XmDROP_SITE_ENTER:
1979 return motif_drag_status (event, flags, timestamp);
1981 case XmDROP_SITE_LEAVE:
1982 return motif_drag_status (event,
1983 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1986 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1987 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1988 atom = MOTIF_UNPACK_LONG (xevent, 3);
1989 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1992 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1995 case XmOPERATION_CHANGED:
1997 return motif_operation_changed (event, flags, timestamp);
1999 return motif_drag_status (event, flags, timestamp);
2002 /* To the best of my knowledge, these next two messages are
2003 * not part of the protocol, though they are defined in
2007 case XmDRAG_DROP_FINISH:
2011 return GDK_FILTER_REMOVE;
2014 /*************************************************************
2015 ***************************** XDND **************************
2016 *************************************************************/
2018 /* Utility functions */
2023 GdkDragAction action;
2024 } xdnd_actions_table[] = {
2025 { "XdndActionCopy", None, GDK_ACTION_COPY },
2026 { "XdndActionMove", None, GDK_ACTION_MOVE },
2027 { "XdndActionLink", None, GDK_ACTION_LINK },
2028 { "XdndActionAsk", None, GDK_ACTION_ASK },
2029 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2032 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
2033 static gboolean xdnd_actions_initialized = FALSE;
2036 xdnd_initialize_actions (void)
2040 xdnd_actions_initialized = TRUE;
2041 for (i = 0; i < xdnd_n_actions; i++)
2042 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2045 static GdkDragAction
2046 xdnd_action_from_atom (GdkDisplay *display,
2055 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2057 if (!xdnd_actions_initialized)
2058 xdnd_initialize_actions();
2060 for (i = 0; i < xdnd_n_actions; i++)
2061 if (atom == xdnd_actions_table[i].atom)
2062 return xdnd_actions_table[i].action;
2068 xdnd_action_to_atom (GdkDisplay *display,
2069 GdkDragAction action)
2073 if (!xdnd_actions_initialized)
2074 xdnd_initialize_actions();
2076 for (i = 0; i < xdnd_n_actions; i++)
2077 if (action == xdnd_actions_table[i].action)
2078 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2085 static GdkFilterReturn
2086 xdnd_status_filter (GdkXEvent *xev,
2090 GdkDisplay *display;
2091 XEvent *xevent = (XEvent *)xev;
2092 guint32 dest_window = xevent->xclient.data.l[0];
2093 guint32 flags = xevent->xclient.data.l[1];
2094 Atom action = xevent->xclient.data.l[4];
2095 GdkDragContext *context;
2097 if (!event->any.window ||
2098 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2099 return GDK_FILTER_CONTINUE; /* Not for us */
2102 g_message ("XdndStatus: dest_window: %#x action: %ld",
2103 dest_window, action));
2105 display = gdk_window_get_display (event->any.window);
2106 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2110 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
2111 if (context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2112 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2114 event->dnd.send_event = FALSE;
2115 event->dnd.type = GDK_DRAG_STATUS;
2116 event->dnd.context = context;
2117 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2118 g_object_ref (context);
2120 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2121 if (!(action != 0) != !(flags & 1))
2124 g_warning ("Received status event with flags not corresponding to action!\n"));
2128 context->action = xdnd_action_from_atom (display, action);
2130 return GDK_FILTER_TRANSLATE;
2133 return GDK_FILTER_REMOVE;
2136 static GdkFilterReturn
2137 xdnd_finished_filter (GdkXEvent *xev,
2141 GdkDisplay *display;
2142 XEvent *xevent = (XEvent *)xev;
2143 guint32 dest_window = xevent->xclient.data.l[0];
2144 GdkDragContext *context;
2145 GdkDragContextX11 *context_x11;
2147 if (!event->any.window ||
2148 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2149 return GDK_FILTER_CONTINUE; /* Not for us */
2152 g_message ("XdndFinished: dest_window: %#x", dest_window));
2154 display = gdk_window_get_display (event->any.window);
2155 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2159 context_x11 = GDK_DRAG_CONTEXT_X11 (context);
2160 if (context_x11->version == 5)
2161 context_x11->drop_failed = xevent->xclient.data.l[1] == 0;
2163 event->dnd.type = GDK_DROP_FINISHED;
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? */
2170 return GDK_FILTER_TRANSLATE;
2173 return GDK_FILTER_REMOVE;
2177 xdnd_set_targets (GdkDragContextX11 *context_x11)
2179 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2181 GList *tmp_list = context->targets;
2183 gint n_atoms = g_list_length (context->targets);
2184 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2186 atomlist = g_new (Atom, n_atoms);
2190 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2191 tmp_list = tmp_list->next;
2195 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2196 GDK_WINDOW_XID (context->source_window),
2197 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2198 XA_ATOM, 32, PropModeReplace,
2199 (guchar *)atomlist, n_atoms);
2203 context_x11->xdnd_targets_set = 1;
2207 xdnd_set_actions (GdkDragContextX11 *context_x11)
2209 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2214 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2216 if (!xdnd_actions_initialized)
2217 xdnd_initialize_actions();
2219 actions = context->actions;
2221 for (i = 0; i < xdnd_n_actions; i++)
2223 if (actions & xdnd_actions_table[i].action)
2225 actions &= ~xdnd_actions_table[i].action;
2230 atomlist = g_new (Atom, n_atoms);
2232 actions = context->actions;
2234 for (i = 0; i < xdnd_n_actions; i++)
2236 if (actions & xdnd_actions_table[i].action)
2238 actions &= ~xdnd_actions_table[i].action;
2239 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2244 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2245 GDK_WINDOW_XID (context->source_window),
2246 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2247 XA_ATOM, 32, PropModeReplace,
2248 (guchar *)atomlist, n_atoms);
2252 context_x11->xdnd_actions_set = TRUE;
2253 context_x11->xdnd_actions = context->actions;
2257 send_client_message_async_cb (Window window,
2261 GdkDragContext *context = data;
2263 g_message ("Got async callback for #%lx, success = %d",
2266 /* On failure, we immediately continue with the protocol
2267 * so we don't end up blocking for a timeout
2270 context->dest_window &&
2271 window == GDK_WINDOW_XID (context->dest_window))
2273 GdkEvent *temp_event;
2274 GdkDragContextX11 *context_x11 = data;
2276 g_object_unref (context->dest_window);
2277 context->dest_window = NULL;
2278 context->action = 0;
2280 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2282 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2283 temp_event->dnd.window = g_object_ref (context->source_window);
2284 temp_event->dnd.send_event = TRUE;
2285 temp_event->dnd.context = g_object_ref (context);
2286 temp_event->dnd.time = GDK_CURRENT_TIME;
2287 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2289 gdk_event_put (temp_event);
2291 gdk_event_free (temp_event);
2294 g_object_unref (context);
2299 gdk_drag_context_get_display (GdkDragContext *context)
2301 if (context->source_window)
2302 return GDK_WINDOW_DISPLAY (context->source_window);
2303 else if (context->dest_window)
2304 return GDK_WINDOW_DISPLAY (context->dest_window);
2306 g_assert_not_reached ();
2311 send_client_message_async (GdkDragContext *context,
2315 XClientMessageEvent *event_send)
2317 GdkDisplay *display = gdk_drag_context_get_display (context);
2319 g_object_ref (context);
2321 _gdk_x11_send_client_message_async (display, window,
2322 propagate, event_mask, event_send,
2323 send_client_message_async_cb, context);
2327 xdnd_send_xevent (GdkDragContextX11 *context_x11,
2332 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2333 GdkDisplay *display = gdk_drag_context_get_display (context);
2337 g_assert (event_send->xany.type == ClientMessage);
2339 /* We short-circuit messages to ourselves */
2340 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2344 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2346 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2347 event_send->xclient.message_type)
2349 GdkEvent *temp_event;
2351 temp_event = gdk_event_new (GDK_NOTHING);
2352 temp_event->any.window = g_object_ref (window);
2354 if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
2356 gdk_event_put (temp_event);
2357 gdk_event_free (temp_event);
2365 xwindow = GDK_WINDOW_XID (window);
2367 if (_gdk_x11_display_is_root_window (display, xwindow))
2368 event_mask = ButtonPressMask;
2372 send_client_message_async (context, xwindow, propagate, event_mask,
2373 &event_send->xclient);
2379 xdnd_send_enter (GdkDragContextX11 *context_x11)
2381 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2382 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->dest_window);
2385 xev.xclient.type = ClientMessage;
2386 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2387 xev.xclient.format = 32;
2388 xev.xclient.window = context_x11->drop_xid
2389 ? context_x11->drop_xid
2390 : GDK_WINDOW_XID (context->dest_window);
2391 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2392 xev.xclient.data.l[1] = (context_x11->version << 24); /* version */
2393 xev.xclient.data.l[2] = 0;
2394 xev.xclient.data.l[3] = 0;
2395 xev.xclient.data.l[4] = 0;
2398 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2399 GDK_WINDOW_XID (context->source_window), context_x11->version));
2400 if (g_list_length (context->targets) > 3)
2402 if (!context_x11->xdnd_targets_set)
2403 xdnd_set_targets (context_x11);
2404 xev.xclient.data.l[1] |= 1;
2408 GList *tmp_list = context->targets;
2413 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2414 GDK_POINTER_TO_ATOM (tmp_list->data));
2415 tmp_list = tmp_list->next;
2420 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2423 g_message ("Send event to %lx failed",
2424 GDK_WINDOW_XID (context->dest_window)));
2425 g_object_unref (context->dest_window);
2426 context->dest_window = NULL;
2431 xdnd_send_leave (GdkDragContextX11 *context_x11)
2433 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2434 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2437 xev.xclient.type = ClientMessage;
2438 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2439 xev.xclient.format = 32;
2440 xev.xclient.window = context_x11->drop_xid
2441 ? context_x11->drop_xid
2442 : GDK_WINDOW_XID (context->dest_window);
2443 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2444 xev.xclient.data.l[1] = 0;
2445 xev.xclient.data.l[2] = 0;
2446 xev.xclient.data.l[3] = 0;
2447 xev.xclient.data.l[4] = 0;
2449 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2452 g_message ("Send event to %lx failed",
2453 GDK_WINDOW_XID (context->dest_window)));
2454 g_object_unref (context->dest_window);
2455 context->dest_window = NULL;
2460 xdnd_send_drop (GdkDragContextX11 *context_x11,
2463 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2464 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2467 xev.xclient.type = ClientMessage;
2468 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2469 xev.xclient.format = 32;
2470 xev.xclient.window = context_x11->drop_xid
2471 ? context_x11->drop_xid
2472 : GDK_WINDOW_XID (context->dest_window);
2473 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2474 xev.xclient.data.l[1] = 0;
2475 xev.xclient.data.l[2] = time;
2476 xev.xclient.data.l[3] = 0;
2477 xev.xclient.data.l[4] = 0;
2479 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2482 g_message ("Send event to %lx failed",
2483 GDK_WINDOW_XID (context->dest_window)));
2484 g_object_unref (context->dest_window);
2485 context->dest_window = NULL;
2490 xdnd_send_motion (GdkDragContextX11 *context_x11,
2493 GdkDragAction action,
2496 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2497 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2500 xev.xclient.type = ClientMessage;
2501 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2502 xev.xclient.format = 32;
2503 xev.xclient.window = context_x11->drop_xid
2504 ? context_x11->drop_xid
2505 : GDK_WINDOW_XID (context->dest_window);
2506 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2507 xev.xclient.data.l[1] = 0;
2508 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2509 xev.xclient.data.l[3] = time;
2510 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2512 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2515 g_message ("Send event to %lx failed",
2516 GDK_WINDOW_XID (context->dest_window)));
2517 g_object_unref (context->dest_window);
2518 context->dest_window = NULL;
2520 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2524 xdnd_check_dest (GdkDisplay *display,
2526 guint *xdnd_version)
2528 gboolean retval = FALSE;
2531 unsigned long nitems, after;
2536 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2537 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2541 gdk_x11_display_error_trap_push (display);
2542 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2544 1, False, AnyPropertyType,
2545 &type, &format, &nitems, &after,
2550 proxy_data = (Window *)data;
2552 if ((format == 32) && (nitems == 1))
2554 proxy = *proxy_data;
2558 g_warning ("Invalid XdndProxy "
2559 "property on window %ld\n", win));
2564 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2566 1, False, AnyPropertyType,
2567 &type, &format, &nitems, &after,
2568 &data) == Success) &&
2571 version = (Atom *)data;
2573 if ((format == 32) && (nitems == 1))
2578 *xdnd_version = *version;
2582 g_warning ("Invalid XdndAware "
2583 "property on window %ld\n", win));
2589 gdk_x11_display_error_trap_pop_ignored (display);
2591 return retval ? (proxy ? proxy : win) : None;
2597 xdnd_read_actions (GdkDragContextX11 *context_x11)
2599 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2600 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2603 gulong nitems, after;
2608 context_x11->xdnd_have_actions = FALSE;
2610 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2612 /* Get the XdndActionList, if set */
2614 gdk_x11_display_error_trap_push (display);
2615 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2616 GDK_WINDOW_XID (context->source_window),
2617 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2619 False, XA_ATOM, &type, &format, &nitems,
2620 &after, &data) == Success &&
2623 atoms = (Atom *)data;
2625 context->actions = 0;
2627 for (i = 0; i < nitems; i++)
2628 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2630 context_x11->xdnd_have_actions = TRUE;
2632 #ifdef G_ENABLE_DEBUG
2633 if (_gdk_debug_flags & GDK_DEBUG_DND)
2635 GString *action_str = g_string_new (NULL);
2636 if (context->actions & GDK_ACTION_MOVE)
2637 g_string_append(action_str, "MOVE ");
2638 if (context->actions & GDK_ACTION_COPY)
2639 g_string_append(action_str, "COPY ");
2640 if (context->actions & GDK_ACTION_LINK)
2641 g_string_append(action_str, "LINK ");
2642 if (context->actions & GDK_ACTION_ASK)
2643 g_string_append(action_str, "ASK ");
2645 g_message("Xdnd actions = %s", action_str->str);
2646 g_string_free (action_str, TRUE);
2648 #endif /* G_ENABLE_DEBUG */
2655 gdk_x11_display_error_trap_pop_ignored (display);
2661 GdkDragContext *source_context;
2663 source_context = gdk_drag_context_find (display, TRUE,
2664 GDK_WINDOW_XID (context->source_window),
2665 GDK_WINDOW_XID (context->dest_window));
2669 context->actions = source_context->actions;
2670 context_x11->xdnd_have_actions = TRUE;
2675 /* We have to make sure that the XdndActionList we keep internally
2676 * is up to date with the XdndActionList on the source window
2677 * because we get no notification, because Xdnd wasn't meant
2678 * to continually send actions. So we select on PropertyChangeMask
2679 * and add this filter.
2681 static GdkFilterReturn
2682 xdnd_source_window_filter (GdkXEvent *xev,
2686 XEvent *xevent = (XEvent *)xev;
2687 GdkDragContextX11 *context_x11 = cb_data;
2688 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2690 if ((xevent->xany.type == PropertyNotify) &&
2691 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2693 xdnd_read_actions (context_x11);
2695 return GDK_FILTER_REMOVE;
2698 return GDK_FILTER_CONTINUE;
2702 xdnd_manage_source_filter (GdkDragContext *context,
2704 gboolean add_filter)
2706 if (!GDK_WINDOW_DESTROYED (window) &&
2707 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2709 gdk_x11_display_error_trap_push (GDK_WINDOW_DISPLAY (window));
2713 gdk_window_set_events (window,
2714 gdk_window_get_events (window) |
2715 GDK_PROPERTY_CHANGE_MASK);
2716 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2720 gdk_window_remove_filter (window,
2721 xdnd_source_window_filter,
2723 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2724 * but we might want it for other reasons. (Like
2725 * INCR selection transactions).
2729 gdk_x11_display_error_trap_pop_ignored (GDK_WINDOW_DISPLAY (window));
2734 base_precache_atoms (GdkDisplay *display)
2736 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2738 if (!display_x11->base_dnd_atoms_precached)
2740 static const char *const precache_atoms[] = {
2741 "ENLIGHTENMENT_DESKTOP",
2745 "_MOTIF_DRAG_RECEIVER_INFO"
2748 _gdk_x11_precache_atoms (display,
2749 precache_atoms, G_N_ELEMENTS (precache_atoms));
2751 display_x11->base_dnd_atoms_precached = TRUE;
2756 xdnd_precache_atoms (GdkDisplay *display)
2758 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2760 if (!display_x11->xdnd_atoms_precached)
2762 static const char *const precache_atoms[] = {
2768 "XdndActionPrivate",
2779 _gdk_x11_precache_atoms (display,
2780 precache_atoms, G_N_ELEMENTS (precache_atoms));
2782 display_x11->xdnd_atoms_precached = TRUE;
2786 static GdkFilterReturn
2787 xdnd_enter_filter (GdkXEvent *xev,
2791 GdkDeviceManager *device_manager;
2792 GdkDisplay *display;
2793 GdkDisplayX11 *display_x11;
2794 XEvent *xevent = (XEvent *)xev;
2795 GdkDragContext *context;
2796 GdkDragContextX11 *context_x11;
2801 gulong nitems, after;
2805 guint32 source_window;
2809 if (!event->any.window ||
2810 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2811 return GDK_FILTER_CONTINUE; /* Not for us */
2813 source_window = xevent->xclient.data.l[0];
2814 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2815 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2817 display = GDK_WINDOW_DISPLAY (event->any.window);
2818 display_x11 = GDK_DISPLAY_X11 (display);
2820 xdnd_precache_atoms (display);
2823 g_message ("XdndEnter: source_window: %#x, version: %#x",
2824 source_window, version));
2828 /* Old source ignore */
2829 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2830 return GDK_FILTER_REMOVE;
2833 if (display_x11->current_dest_drag != NULL)
2835 g_object_unref (display_x11->current_dest_drag);
2836 display_x11->current_dest_drag = NULL;
2839 context_x11 = (GdkDragContextX11 *)g_object_new (GDK_TYPE_DRAG_CONTEXT_X11, NULL);
2840 context = (GdkDragContext *)context_x11;
2842 context->protocol = GDK_DRAG_PROTO_XDND;
2843 context_x11->version = version;
2845 /* FIXME: Should extend DnD protocol to have device info */
2846 device_manager = gdk_display_get_device_manager (display);
2847 gdk_drag_context_set_device (context, gdk_device_manager_get_client_pointer (device_manager));
2849 context->source_window = gdk_x11_window_lookup_for_display (display, source_window);
2850 if (context->source_window)
2851 g_object_ref (context->source_window);
2854 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
2855 if (!context->source_window)
2857 g_object_unref (context);
2858 return GDK_FILTER_REMOVE;
2861 context->dest_window = event->any.window;
2862 g_object_ref (context->dest_window);
2864 context->targets = NULL;
2867 gdk_x11_display_error_trap_push (display);
2868 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2870 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2872 False, XA_ATOM, &type, &format, &nitems,
2875 if (gdk_x11_display_error_trap_pop (display) || (format != 32) || (type != XA_ATOM))
2877 g_object_unref (context);
2882 return GDK_FILTER_REMOVE;
2885 atoms = (Atom *)data;
2887 for (i = 0; i < nitems; i++)
2889 g_list_append (context->targets,
2890 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2897 for (i = 0; i < 3; i++)
2898 if (xevent->xclient.data.l[2 + i])
2900 g_list_append (context->targets,
2901 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2902 xevent->xclient.data.l[2 + i])));
2905 #ifdef G_ENABLE_DEBUG
2906 if (_gdk_debug_flags & GDK_DEBUG_DND)
2907 print_target_list (context->targets);
2908 #endif /* G_ENABLE_DEBUG */
2910 xdnd_manage_source_filter (context, context->source_window, TRUE);
2911 xdnd_read_actions (context_x11);
2913 event->dnd.type = GDK_DRAG_ENTER;
2914 event->dnd.context = context;
2915 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2916 g_object_ref (context);
2918 display_x11->current_dest_drag = context;
2920 return GDK_FILTER_TRANSLATE;
2923 static GdkFilterReturn
2924 xdnd_leave_filter (GdkXEvent *xev,
2928 XEvent *xevent = (XEvent *)xev;
2929 guint32 source_window = xevent->xclient.data.l[0];
2930 GdkDisplay *display;
2931 GdkDisplayX11 *display_x11;
2933 if (!event->any.window ||
2934 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2935 return GDK_FILTER_CONTINUE; /* Not for us */
2938 g_message ("XdndLeave: source_window: %#x",
2941 display = GDK_WINDOW_DISPLAY (event->any.window);
2942 display_x11 = GDK_DISPLAY_X11 (display);
2944 xdnd_precache_atoms (display);
2946 if ((display_x11->current_dest_drag != NULL) &&
2947 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2948 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
2950 event->dnd.type = GDK_DRAG_LEAVE;
2951 /* Pass ownership of context to the event */
2952 event->dnd.context = display_x11->current_dest_drag;
2953 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
2955 display_x11->current_dest_drag = NULL;
2957 return GDK_FILTER_TRANSLATE;
2960 return GDK_FILTER_REMOVE;
2963 static GdkFilterReturn
2964 xdnd_position_filter (GdkXEvent *xev,
2968 XEvent *xevent = (XEvent *)xev;
2969 guint32 source_window = xevent->xclient.data.l[0];
2970 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2971 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2972 guint32 time = xevent->xclient.data.l[3];
2973 Atom action = xevent->xclient.data.l[4];
2975 GdkDisplay *display;
2976 GdkDisplayX11 *display_x11;
2977 GdkDragContext *context;
2978 GdkDragContextX11 *context_x11;
2980 if (!event->any.window ||
2981 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2982 return GDK_FILTER_CONTINUE; /* Not for us */
2985 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2986 source_window, x_root, y_root, time, action));
2988 display = GDK_WINDOW_DISPLAY (event->any.window);
2989 display_x11 = GDK_DISPLAY_X11 (display);
2991 xdnd_precache_atoms (display);
2993 context = display_x11->current_dest_drag;
2995 if ((context != NULL) &&
2996 (context->protocol == GDK_DRAG_PROTO_XDND) &&
2997 (GDK_WINDOW_XID (context->source_window) == source_window))
2999 context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3001 event->dnd.type = GDK_DRAG_MOTION;
3002 event->dnd.context = context;
3003 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3004 g_object_ref (context);
3006 event->dnd.time = time;
3008 context->suggested_action = xdnd_action_from_atom (display, action);
3010 if (!context_x11->xdnd_have_actions)
3011 context->actions = context->suggested_action;
3013 event->dnd.x_root = x_root;
3014 event->dnd.y_root = y_root;
3016 context_x11->last_x = x_root;
3017 context_x11->last_y = y_root;
3019 return GDK_FILTER_TRANSLATE;
3022 return GDK_FILTER_REMOVE;
3025 static GdkFilterReturn
3026 xdnd_drop_filter (GdkXEvent *xev,
3030 XEvent *xevent = (XEvent *)xev;
3031 guint32 source_window = xevent->xclient.data.l[0];
3032 guint32 time = xevent->xclient.data.l[2];
3033 GdkDisplay *display;
3034 GdkDisplayX11 *display_x11;
3035 GdkDragContext *context;
3036 GdkDragContextX11 *context_x11;
3038 if (!event->any.window ||
3039 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3040 return GDK_FILTER_CONTINUE; /* Not for us */
3043 g_message ("XdndDrop: source_window: %#x time: %d",
3044 source_window, time));
3046 display = GDK_WINDOW_DISPLAY (event->any.window);
3047 display_x11 = GDK_DISPLAY_X11 (display);
3049 xdnd_precache_atoms (display);
3051 context = display_x11->current_dest_drag;
3053 if ((context != NULL) &&
3054 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3055 (GDK_WINDOW_XID (context->source_window) == source_window))
3057 context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3058 event->dnd.type = GDK_DROP_START;
3060 event->dnd.context = context;
3061 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3062 g_object_ref (context);
3064 event->dnd.time = time;
3065 event->dnd.x_root = context_x11->last_x;
3066 event->dnd.y_root = context_x11->last_y;
3068 gdk_x11_window_set_user_time (event->any.window, time);
3070 return GDK_FILTER_TRANSLATE;
3073 return GDK_FILTER_REMOVE;
3076 /*************************************************************
3077 ************************** Public API ***********************
3078 *************************************************************/
3080 _gdk_x11_dnd_init (GdkDisplay *display)
3085 gdk_display_add_client_message_filter (
3087 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
3088 motif_dnd_filter, NULL);
3090 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3092 gdk_display_add_client_message_filter (
3094 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
3095 xdnd_filters[i].func, NULL);
3102 gdk_drag_do_leave (GdkDragContextX11 *context_x11,
3105 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
3107 if (context->dest_window)
3109 switch (context->protocol)
3111 case GDK_DRAG_PROTO_MOTIF:
3112 motif_send_leave (context_x11, time);
3114 case GDK_DRAG_PROTO_XDND:
3115 xdnd_send_leave (context_x11);
3117 case GDK_DRAG_PROTO_ROOTWIN:
3118 case GDK_DRAG_PROTO_NONE:
3123 g_object_unref (context->dest_window);
3124 context->dest_window = NULL;
3129 _gdk_x11_window_drag_begin (GdkWindow *window,
3133 GdkDragContext *context;
3135 context = (GdkDragContext *)g_object_new (GDK_TYPE_DRAG_CONTEXT_X11, NULL);
3137 context->is_source = TRUE;
3138 context->source_window = window;
3139 g_object_ref (window);
3141 context->targets = g_list_copy (targets);
3142 precache_target_list (context);
3144 context->actions = 0;
3146 gdk_drag_context_set_device (context, device);
3152 _gdk_x11_display_get_drag_protocol (GdkDisplay *display,
3153 GdkNativeWindow xid,
3154 GdkDragProtocol *protocol,
3159 GdkNativeWindow retval;
3161 base_precache_atoms (display);
3163 /* Check for a local drag */
3164 window = gdk_x11_window_lookup_for_display (display, xid);
3165 if (window && gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3167 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3169 *protocol = GDK_DRAG_PROTO_XDND;
3171 xdnd_precache_atoms (display);
3172 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3175 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3177 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3178 GDK_NOTE (DND, g_message ("Entering root window\n"));
3182 else if ((retval = xdnd_check_dest (display, xid, version)))
3184 *protocol = GDK_DRAG_PROTO_XDND;
3185 xdnd_precache_atoms (display);
3186 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3189 else if ((retval = motif_check_dest (display, xid)))
3191 *protocol = GDK_DRAG_PROTO_MOTIF;
3192 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3197 /* Check if this is a root window */
3198 gboolean rootwin = FALSE;
3200 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3205 GDK_NOTE (DND, g_message ("Entering root window\n"));
3206 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3211 *protocol = GDK_DRAG_PROTO_NONE;
3213 return 0; /* a.k.a. None */
3216 static GdkWindowCache *
3217 drag_context_find_window_cache (GdkDragContextX11 *context_x11,
3221 GdkWindowCache *cache;
3223 for (list = context_x11->window_caches; list; list = list->next)
3226 if (cache->screen == screen)
3230 cache = gdk_window_cache_new (screen);
3231 context_x11->window_caches = g_slist_prepend (context_x11->window_caches, cache);
3237 gdk_drag_context_x11_find_window (GdkDragContext *context,
3238 GdkWindow *drag_window,
3242 GdkDragProtocol *protocol)
3244 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3245 GdkWindowCache *window_cache;
3246 GdkDisplay *display;
3248 GdkWindow *dest_window;
3250 display = GDK_WINDOW_DISPLAY (context->source_window);
3252 window_cache = drag_context_find_window_cache (context_x11, screen);
3254 dest = get_client_window_at_coords (window_cache,
3255 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3256 GDK_WINDOW_XID (drag_window) : None,
3259 if (context_x11->dest_xid != dest)
3262 context_x11->dest_xid = dest;
3264 /* Check if new destination accepts drags, and which protocol */
3266 /* There is some ugliness here. We actually need to pass
3267 * _three_ pieces of information to drag_motion - dest_window,
3268 * protocol, and the XID of the unproxied window. The first
3269 * two are passed explicitely, the third implicitly through
3270 * protocol->dest_xid.
3272 recipient = _gdk_x11_display_get_drag_protocol (display,
3275 &context_x11->version);
3277 if (recipient != None)
3279 dest_window = gdk_x11_window_lookup_for_display (display, recipient);
3281 g_object_ref (dest_window);
3283 dest_window = gdk_x11_window_foreign_new_for_display (display, recipient);
3290 dest_window = context->dest_window;
3292 g_object_ref (dest_window);
3293 *protocol = context->protocol;
3300 gdk_drag_context_x11_drag_motion (GdkDragContext *context,
3301 GdkWindow *dest_window,
3302 GdkDragProtocol protocol,
3305 GdkDragAction suggested_action,
3306 GdkDragAction possible_actions,
3309 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3311 context_x11->old_actions = context->actions;
3312 context->actions = possible_actions;
3314 if (context_x11->old_actions != possible_actions)
3315 context_x11->xdnd_actions_set = FALSE;
3317 if (protocol == GDK_DRAG_PROTO_XDND && context_x11->version == 0)
3319 /* This ugly hack is necessary since GTK+ doesn't know about
3320 * the XDND protocol version, and in particular doesn't know
3321 * that gdk_drag_find_window_for_screen() has the side-effect
3322 * of setting context_x11->version, and therefore sometimes call
3323 * gdk_drag_motion() without a prior call to
3324 * gdk_drag_find_window_for_screen(). This happens, e.g.
3325 * when GTK+ is proxying DND events to embedded windows.
3329 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3331 xdnd_check_dest (display,
3332 GDK_WINDOW_XID (dest_window),
3333 &context_x11->version);
3337 /* When we have a Xdnd target, make sure our XdndActionList
3338 * matches the current actions;
3340 if (protocol == GDK_DRAG_PROTO_XDND && !context_x11->xdnd_actions_set)
3344 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3345 xdnd_set_actions (context_x11);
3346 else if (context->dest_window == dest_window)
3348 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3349 GdkDragContext *dest_context;
3351 dest_context = gdk_drag_context_find (display, FALSE,
3352 GDK_WINDOW_XID (context->source_window),
3353 GDK_WINDOW_XID (dest_window));
3357 dest_context->actions = context->actions;
3358 GDK_DRAG_CONTEXT_X11 (dest_context)->xdnd_have_actions = TRUE;
3364 if (context->dest_window != dest_window)
3366 GdkEvent *temp_event;
3368 /* Send a leave to the last destination */
3369 gdk_drag_do_leave (context_x11, time);
3370 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
3372 /* Check if new destination accepts drags, and which protocol */
3376 context->dest_window = dest_window;
3377 context_x11->drop_xid = context_x11->dest_xid;
3378 g_object_ref (context->dest_window);
3379 context->protocol = protocol;
3383 case GDK_DRAG_PROTO_MOTIF:
3384 motif_send_enter (context_x11, time);
3387 case GDK_DRAG_PROTO_XDND:
3388 xdnd_send_enter (context_x11);
3391 case GDK_DRAG_PROTO_ROOTWIN:
3392 case GDK_DRAG_PROTO_NONE:
3396 context_x11->old_action = suggested_action;
3397 context->suggested_action = suggested_action;
3398 context_x11->old_actions = possible_actions;
3402 context->dest_window = NULL;
3403 context_x11->drop_xid = None;
3404 context->action = 0;
3407 /* Push a status event, to let the client know that
3410 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3411 temp_event->dnd.window = g_object_ref (context->source_window);
3412 /* We use this to signal a synthetic status. Perhaps
3413 * we should use an extra field...
3415 temp_event->dnd.send_event = TRUE;
3417 temp_event->dnd.context = g_object_ref (context);
3418 temp_event->dnd.time = time;
3419 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3421 gdk_event_put (temp_event);
3422 gdk_event_free (temp_event);
3426 context_x11->old_action = context->suggested_action;
3427 context->suggested_action = suggested_action;
3430 /* Send a drag-motion event */
3432 context_x11->last_x = x_root;
3433 context_x11->last_y = y_root;
3435 if (context->dest_window)
3437 if (context_x11->drag_status == GDK_DRAG_STATUS_DRAG)
3439 switch (context->protocol)
3441 case GDK_DRAG_PROTO_MOTIF:
3442 motif_send_motion (context_x11, x_root, y_root, suggested_action, time);
3445 case GDK_DRAG_PROTO_XDND:
3446 xdnd_send_motion (context_x11, x_root, y_root, suggested_action, time);
3449 case GDK_DRAG_PROTO_ROOTWIN:
3451 GdkEvent *temp_event;
3452 /* GTK+ traditionally has used application/x-rootwin-drop,
3453 * but the XDND spec specifies x-rootwindow-drop.
3455 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3456 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3458 if (g_list_find (context->targets,
3459 GDK_ATOM_TO_POINTER (target1)) ||
3460 g_list_find (context->targets,
3461 GDK_ATOM_TO_POINTER (target2)))
3462 context->action = context->suggested_action;
3464 context->action = 0;
3466 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3467 temp_event->dnd.window = g_object_ref (context->source_window);
3468 temp_event->dnd.send_event = FALSE;
3469 temp_event->dnd.context = g_object_ref (context);
3470 temp_event->dnd.time = time;
3471 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3473 gdk_event_put (temp_event);
3474 gdk_event_free (temp_event);
3477 case GDK_DRAG_PROTO_NONE:
3478 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3492 gdk_drag_context_x11_drag_abort (GdkDragContext *context,
3495 gdk_drag_do_leave (GDK_DRAG_CONTEXT_X11 (context), time);
3499 gdk_drag_context_x11_drag_drop (GdkDragContext *context,
3502 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3504 if (context->dest_window)
3506 switch (context->protocol)
3508 case GDK_DRAG_PROTO_MOTIF:
3509 motif_send_leave (context_x11, time);
3510 motif_send_drop (context_x11, time);
3513 case GDK_DRAG_PROTO_XDND:
3514 xdnd_send_drop (context_x11, time);
3517 case GDK_DRAG_PROTO_ROOTWIN:
3518 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3520 case GDK_DRAG_PROTO_NONE:
3521 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3529 /* Destination side */
3532 gdk_drag_context_x11_drag_status (GdkDragContext *context,
3533 GdkDragAction action,
3536 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3538 GdkDisplay *display;
3540 display = GDK_WINDOW_DISPLAY (context->source_window);
3542 context->action = action;
3544 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3546 gboolean need_coords = FALSE;
3548 xev.xclient.type = ClientMessage;
3549 xev.xclient.message_type =
3550 gdk_x11_get_xatom_by_name_for_display (display,
3551 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3552 xev.xclient.format = 8;
3553 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3555 if (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3557 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3561 if ((action != 0) != (context_x11->old_action != 0))
3565 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3570 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3575 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3580 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3584 case GDK_ACTION_MOVE:
3585 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3587 case GDK_ACTION_COPY:
3588 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3590 case GDK_ACTION_LINK:
3591 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3594 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3599 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3601 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3603 MOTIF_XCLIENT_LONG (&xev, 1) = time_;
3607 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
3608 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
3611 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3613 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3614 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3616 if (!_gdk_send_xevent (display,
3617 GDK_WINDOW_XID (context->source_window),
3620 g_message ("Send event to %lx failed",
3621 GDK_WINDOW_XID (context->source_window)));
3623 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3625 xev.xclient.type = ClientMessage;
3626 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3627 xev.xclient.format = 32;
3628 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3630 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3631 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3632 xev.xclient.data.l[2] = 0;
3633 xev.xclient.data.l[3] = 0;
3634 xev.xclient.data.l[4] = 0;
3635 if (!xdnd_send_xevent (context_x11, context->source_window, FALSE, &xev))
3637 g_message ("Send event to %lx failed",
3638 GDK_WINDOW_XID (context->source_window)));
3641 context_x11->old_action = action;
3645 gdk_drag_context_x11_drop_reply (GdkDragContext *context,
3649 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3651 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3653 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3656 xev.xclient.type = ClientMessage;
3657 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3658 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3659 xev.xclient.format = 8;
3661 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3662 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3664 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3665 (XmDROP_SITE_VALID << 4) |
3666 (XmDROP_NOOP << 8) |
3669 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3670 (XmNO_DROP_SITE << 4) |
3671 (XmDROP_NOOP << 8) |
3672 (XmDROP_CANCEL << 12);
3673 MOTIF_XCLIENT_SHORT (&xev, 2) = context_x11->last_x;
3674 MOTIF_XCLIENT_SHORT (&xev, 3) = context_x11->last_y;
3675 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3676 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3677 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3679 _gdk_send_xevent (display,
3680 GDK_WINDOW_XID (context->source_window),
3686 gdk_drag_context_x11_drop_finish (GdkDragContext *context,
3690 if (context->protocol == GDK_DRAG_PROTO_XDND)
3692 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3695 xev.xclient.type = ClientMessage;
3696 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3697 xev.xclient.format = 32;
3698 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3700 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3703 xev.xclient.data.l[1] = 1;
3704 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3709 xev.xclient.data.l[1] = 0;
3710 xev.xclient.data.l[2] = None;
3712 xev.xclient.data.l[3] = 0;
3713 xev.xclient.data.l[4] = 0;
3715 if (!xdnd_send_xevent (GDK_DRAG_CONTEXT_X11 (context), context->source_window, FALSE, &xev))
3717 g_message ("Send event to %lx failed",
3718 GDK_WINDOW_XID (context->source_window)));
3723 _gdk_x11_window_register_dnd (GdkWindow *window)
3725 static const gulong xdnd_version = 5;
3726 MotifDragReceiverInfo info;
3727 Atom motif_drag_receiver_info_atom;
3728 GdkDisplay *display = gdk_window_get_display (window);
3730 g_return_if_fail (window != NULL);
3732 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
3735 base_precache_atoms (display);
3737 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3740 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3742 /* Set Motif drag receiver information property */
3744 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3745 "_MOTIF_DRAG_RECEIVER_INFO");
3746 /* initialize to zero to avoid writing uninitialized data to socket */
3747 memset(&info, 0, sizeof(info));
3748 info.byte_order = local_byte_order;
3749 info.protocol_version = 0;
3750 info.protocol_style = XmDRAG_DYNAMIC;
3751 info.proxy_window = None;
3752 info.num_drop_sites = 0;
3753 info.total_size = sizeof(info);
3755 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
3756 motif_drag_receiver_info_atom,
3757 motif_drag_receiver_info_atom,
3764 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3765 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3766 GDK_WINDOW_XID (window),
3767 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3768 XA_ATOM, 32, PropModeReplace,
3769 (guchar *)&xdnd_version, 1);
3773 gdk_drag_context_x11_get_selection (GdkDragContext *context)
3775 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3776 return gdk_x11_xatom_to_atom_for_display (GDK_WINDOW_DISPLAY (context->source_window),
3777 (GDK_DRAG_CONTEXT_X11 (context))->motif_selection);
3778 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3779 return gdk_atom_intern_static_string ("XdndSelection");
3785 gdk_drag_context_x11_drop_status (GdkDragContext *context)
3787 return ! GDK_DRAG_CONTEXT_X11 (context)->drop_failed;