1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdkx11dnd.h"
30 #include "gdkdndprivate.h"
33 #include "gdkinternals.h"
35 #include "gdkproperty.h"
36 #include "gdkprivate-x11.h"
37 #include "gdkscreen-x11.h"
38 #include "gdkdisplay-x11.h"
41 #include <X11/Xutil.h>
42 #include <X11/Xatom.h>
43 #include <X11/extensions/shape.h>
44 #ifdef HAVE_XCOMPOSITE
45 #include <X11/extensions/Xcomposite.h>
52 GDK_DRAG_STATUS_MOTION_WAIT,
53 GDK_DRAG_STATUS_ACTION_WAIT,
59 gint x, y, width, height;
61 gboolean shape_selected;
63 cairo_region_t *shape;
68 GHashTable *child_hash;
75 struct _GdkX11DragContext
77 GdkDragContext context;
82 guint16 last_x; /* Coordinates from last event */
84 GdkDragAction old_action; /* The last action we sent to the source */
85 GdkDragAction old_actions; /* The last actions we sent to the source */
86 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
88 Window dest_xid; /* The last window we looked up */
89 Window drop_xid; /* The (non-proxied) window that is receiving drops */
90 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
91 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
92 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
93 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
94 guint drag_status : 4; /* current status of drag */
96 guint drop_failed : 1; /* Whether the drop was unsuccessful */
97 guint version; /* Xdnd protocol version */
99 GSList *window_caches;
102 struct _GdkX11DragContextClass
104 GdkDragContextClass parent_class;
107 /* Forward declarations */
109 static GdkWindowCache *gdk_window_cache_get (GdkScreen *screen);
110 static GdkWindowCache *gdk_window_cache_ref (GdkWindowCache *cache);
111 static void gdk_window_cache_unref (GdkWindowCache *cache);
113 static void motif_read_target_table (GdkDisplay *display);
115 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
119 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
122 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
125 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
128 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
131 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
134 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
138 static void xdnd_manage_source_filter (GdkDragContext *context,
140 gboolean add_filter);
142 static GList *contexts;
143 static GSList *window_caches;
145 static const struct {
146 const char *atom_name;
149 { "_MOTIF_DRAG_AND_DROP_MESSAGE", motif_dnd_filter },
151 { "XdndEnter", xdnd_enter_filter },
152 { "XdndLeave", xdnd_leave_filter },
153 { "XdndPosition", xdnd_position_filter },
154 { "XdndStatus", xdnd_status_filter },
155 { "XdndFinished", xdnd_finished_filter },
156 { "XdndDrop", xdnd_drop_filter },
160 G_DEFINE_TYPE (GdkX11DragContext, gdk_x11_drag_context, GDK_TYPE_DRAG_CONTEXT)
163 gdk_x11_drag_context_init (GdkX11DragContext *context)
165 contexts = g_list_prepend (contexts, context);
168 static void gdk_x11_drag_context_finalize (GObject *object);
169 static GdkWindow * gdk_x11_drag_context_find_window (GdkDragContext *context,
170 GdkWindow *drag_window,
174 GdkDragProtocol *protocol);
175 static gboolean gdk_x11_drag_context_drag_motion (GdkDragContext *context,
176 GdkWindow *dest_window,
177 GdkDragProtocol protocol,
180 GdkDragAction suggested_action,
181 GdkDragAction possible_actions,
183 static void gdk_x11_drag_context_drag_status (GdkDragContext *context,
184 GdkDragAction action,
186 static void gdk_x11_drag_context_drag_abort (GdkDragContext *context,
188 static void gdk_x11_drag_context_drag_drop (GdkDragContext *context,
190 static void gdk_x11_drag_context_drop_reply (GdkDragContext *context,
193 static void gdk_x11_drag_context_drop_finish (GdkDragContext *context,
196 static gboolean gdk_x11_drag_context_drop_status (GdkDragContext *context);
197 static GdkAtom gdk_x11_drag_context_get_selection (GdkDragContext *context);
200 gdk_x11_drag_context_class_init (GdkX11DragContextClass *klass)
202 GObjectClass *object_class = G_OBJECT_CLASS (klass);
203 GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
205 object_class->finalize = gdk_x11_drag_context_finalize;
207 context_class->find_window = gdk_x11_drag_context_find_window;
208 context_class->drag_status = gdk_x11_drag_context_drag_status;
209 context_class->drag_motion = gdk_x11_drag_context_drag_motion;
210 context_class->drag_abort = gdk_x11_drag_context_drag_abort;
211 context_class->drag_drop = gdk_x11_drag_context_drag_drop;
212 context_class->drop_reply = gdk_x11_drag_context_drop_reply;
213 context_class->drop_finish = gdk_x11_drag_context_drop_finish;
214 context_class->drop_status = gdk_x11_drag_context_drop_status;
215 context_class->get_selection = gdk_x11_drag_context_get_selection;
219 gdk_x11_drag_context_finalize (GObject *object)
221 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
222 GdkX11DragContext *x11_context = GDK_X11_DRAG_CONTEXT (object);
224 if (context->source_window)
226 if ((context->protocol == GDK_DRAG_PROTO_XDND) && !context->is_source)
227 xdnd_manage_source_filter (context, context->source_window, FALSE);
230 g_slist_free_full (x11_context->window_caches, (GDestroyNotify)gdk_window_cache_unref);
231 x11_context->window_caches = NULL;
233 contexts = g_list_remove (contexts, context);
235 G_OBJECT_CLASS (gdk_x11_drag_context_parent_class)->finalize (object);
240 static GdkDragContext *
241 gdk_drag_context_find (GdkDisplay *display,
246 GList *tmp_list = contexts;
247 GdkDragContext *context;
248 GdkX11DragContext *context_x11;
249 Window context_dest_xid;
253 context = (GdkDragContext *)tmp_list->data;
254 context_x11 = (GdkX11DragContext *)context;
256 if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
257 (context->dest_window && gdk_window_get_display (context->dest_window) != display))
260 context_dest_xid = context->dest_window
261 ? (context_x11->drop_xid
262 ? context_x11->drop_xid
263 : GDK_WINDOW_XID (context->dest_window))
266 if ((!context->is_source == !is_source) &&
267 ((source_xid == None) || (context->source_window &&
268 (GDK_WINDOW_XID (context->source_window) == source_xid))) &&
269 ((dest_xid == None) || (context_dest_xid == dest_xid)))
272 tmp_list = tmp_list->next;
279 precache_target_list (GdkDragContext *context)
281 if (context->targets)
283 GPtrArray *targets = g_ptr_array_new ();
287 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
288 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
290 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
291 (const gchar **)targets->pdata,
294 for (i =0; i < targets->len; i++)
295 g_free (targets->pdata[i]);
297 g_ptr_array_free (targets, TRUE);
301 /* Utility functions */
304 free_cache_child (GdkCacheChild *child,
308 cairo_region_destroy (child->shape);
310 if (child->shape_selected && display)
312 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
314 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
321 gdk_window_cache_add (GdkWindowCache *cache,
329 GdkCacheChild *child = g_new (GdkCacheChild, 1);
334 child->width = width;
335 child->height = height;
336 child->mapped = mapped;
337 child->shape_selected = FALSE;
338 child->shape_valid = FALSE;
341 cache->children = g_list_prepend (cache->children, child);
342 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
346 static GdkFilterReturn
347 gdk_window_cache_shape_filter (GdkXEvent *xev,
351 XEvent *xevent = (XEvent *)xev;
352 GdkWindowCache *cache = data;
354 GdkX11Display *display = GDK_X11_DISPLAY (gdk_screen_get_display (cache->screen));
356 if (display->have_shapes &&
357 xevent->type == display->shape_event_base + ShapeNotify)
359 XShapeEvent *xse = (XShapeEvent*)xevent;
362 node = g_hash_table_lookup (cache->child_hash,
363 GUINT_TO_POINTER (xse->window));
366 GdkCacheChild *child = node->data;
367 child->shape_valid = FALSE;
370 cairo_region_destroy (child->shape);
375 return GDK_FILTER_REMOVE;
378 return GDK_FILTER_CONTINUE;
381 static GdkFilterReturn
382 gdk_window_cache_filter (GdkXEvent *xev,
386 XEvent *xevent = (XEvent *)xev;
387 GdkWindowCache *cache = data;
389 switch (xevent->type)
391 case CirculateNotify:
393 case ConfigureNotify:
395 XConfigureEvent *xce = &xevent->xconfigure;
398 node = g_hash_table_lookup (cache->child_hash,
399 GUINT_TO_POINTER (xce->window));
402 GdkCacheChild *child = node->data;
405 child->width = xce->width;
406 child->height = xce->height;
407 if (xce->above == None && (node->next))
409 GList *last = g_list_last (cache->children);
410 cache->children = g_list_remove_link (cache->children, node);
417 GList *above_node = g_hash_table_lookup (cache->child_hash,
418 GUINT_TO_POINTER (xce->above));
419 if (above_node && node->next != above_node)
421 /* Put the window above (before in the list) above_node */
422 cache->children = g_list_remove_link (cache->children, node);
423 node->prev = above_node->prev;
425 node->prev->next = node;
427 cache->children = node;
428 node->next = above_node;
429 above_node->prev = node;
437 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
439 if (!g_hash_table_lookup (cache->child_hash,
440 GUINT_TO_POINTER (xcwe->window)))
441 gdk_window_cache_add (cache, xcwe->window,
442 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
448 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
451 node = g_hash_table_lookup (cache->child_hash,
452 GUINT_TO_POINTER (xdwe->window));
455 GdkCacheChild *child = node->data;
457 g_hash_table_remove (cache->child_hash,
458 GUINT_TO_POINTER (xdwe->window));
459 cache->children = g_list_remove_link (cache->children, node);
460 /* window is destroyed, no need to disable ShapeNotify */
461 free_cache_child (child, NULL);
462 g_list_free_1 (node);
468 XMapEvent *xme = &xevent->xmap;
471 node = g_hash_table_lookup (cache->child_hash,
472 GUINT_TO_POINTER (xme->window));
475 GdkCacheChild *child = node->data;
476 child->mapped = TRUE;
484 XMapEvent *xume = &xevent->xmap;
487 node = g_hash_table_lookup (cache->child_hash,
488 GUINT_TO_POINTER (xume->window));
491 GdkCacheChild *child = node->data;
492 child->mapped = FALSE;
497 return GDK_FILTER_CONTINUE;
499 return GDK_FILTER_REMOVE;
502 static GdkWindowCache *
503 gdk_window_cache_new (GdkScreen *screen)
505 XWindowAttributes xwa;
506 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
507 GdkWindow *root_window = gdk_screen_get_root_window (screen);
508 GdkChildInfoX11 *children;
510 #ifdef HAVE_XCOMPOSITE
514 GdkWindowCache *result = g_new (GdkWindowCache, 1);
516 result->children = NULL;
517 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
518 result->screen = screen;
519 result->ref_count = 1;
521 XGetWindowAttributes (xdisplay, GDK_WINDOW_XID (root_window), &xwa);
522 result->old_event_mask = xwa.your_event_mask;
524 if (G_UNLIKELY (!GDK_X11_DISPLAY (GDK_X11_SCREEN (screen)->display)->trusted_client))
526 GList *toplevel_windows, *list;
528 gint x, y, width, height;
530 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
531 for (list = toplevel_windows; list; list = list->next)
533 window = GDK_WINDOW (list->data);
534 gdk_window_get_geometry (window, &x, &y, &width, &height);
535 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
537 gdk_window_is_visible (window));
539 g_list_free (toplevel_windows);
543 XSelectInput (xdisplay, GDK_WINDOW_XID (root_window),
544 result->old_event_mask | SubstructureNotifyMask);
545 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
546 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
548 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
549 GDK_WINDOW_XID (root_window),
551 &children, &nchildren))
554 for (i = 0; i < nchildren ; i++)
556 gdk_window_cache_add (result, children[i].window,
557 children[i].x, children[i].y, children[i].width, children[i].height,
558 children[i].is_mapped);
563 #ifdef HAVE_XCOMPOSITE
565 * Add the composite overlay window to the cache, as this can be a reasonable
566 * Xdnd proxy as well.
567 * This is only done when the screen is composited in order to avoid mapping
568 * the COW. We assume that the CM is using the COW (which is true for pretty
569 * much any CM currently in use).
571 if (gdk_screen_is_composited (screen))
573 cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
574 gdk_window_cache_add (result, cow, 0, 0, gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
575 XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
583 gdk_window_cache_destroy (GdkWindowCache *cache)
585 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
588 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
589 GDK_WINDOW_XID (root_window),
590 cache->old_event_mask);
591 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
592 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
594 display = gdk_screen_get_display (cache->screen);
596 gdk_x11_display_error_trap_push (display);
597 g_list_foreach (cache->children, (GFunc)free_cache_child, display);
598 gdk_x11_display_error_trap_pop_ignored (display);
600 g_list_free (cache->children);
601 g_hash_table_destroy (cache->child_hash);
606 static GdkWindowCache *
607 gdk_window_cache_ref (GdkWindowCache *cache)
609 cache->ref_count += 1;
615 gdk_window_cache_unref (GdkWindowCache *cache)
617 g_assert (cache->ref_count > 0);
619 cache->ref_count -= 1;
621 if (cache->ref_count == 0)
623 window_caches = g_slist_remove (window_caches, cache);
624 gdk_window_cache_destroy (cache);
629 gdk_window_cache_get (GdkScreen *screen)
632 GdkWindowCache *cache;
634 for (list = window_caches; list; list = list->next)
637 if (cache->screen == screen)
638 return gdk_window_cache_ref (cache);
641 cache = gdk_window_cache_new (screen);
643 window_caches = g_slist_prepend (window_caches, cache);
649 is_pointer_within_shape (GdkDisplay *display,
650 GdkCacheChild *child,
654 if (!child->shape_selected)
656 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
658 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
659 child->shape_selected = TRUE;
661 if (!child->shape_valid)
663 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
664 cairo_region_t *input_shape;
666 child->shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
667 child->xid, ShapeBounding);
669 input_shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
670 child->xid, ShapeInput);
671 if (child->shape && input_shape)
673 cairo_region_intersect (child->shape, input_shape);
674 cairo_region_destroy (input_shape);
676 else if (input_shape)
678 child->shape = input_shape;
682 child->shape_valid = TRUE;
685 return child->shape == NULL ||
686 cairo_region_contains_point (child->shape, x_pos, y_pos);
690 get_client_window_at_coords_recurse (GdkDisplay *display,
692 gboolean is_toplevel,
696 GdkChildInfoX11 *children;
697 unsigned int nchildren;
699 gboolean found_child = FALSE;
700 GdkChildInfoX11 child = { 0, };
701 gboolean has_wm_state = FALSE;
703 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
704 is_toplevel? &has_wm_state : NULL,
705 &children, &nchildren))
715 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
717 GdkChildInfoX11 *cur_child = &children[i];
719 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
720 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
721 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
734 if (child.has_wm_state)
737 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
744 get_client_window_at_coords (GdkWindowCache *cache,
750 Window retval = None;
753 display = gdk_screen_get_display (cache->screen);
755 gdk_x11_display_error_trap_push (display);
757 tmp_list = cache->children;
759 while (tmp_list && !retval)
761 GdkCacheChild *child = tmp_list->data;
763 if ((child->xid != ignore) && (child->mapped))
765 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
766 (y_root >= child->y) && (y_root < child->y + child->height))
768 if (!is_pointer_within_shape (display, child,
772 tmp_list = tmp_list->next;
776 retval = get_client_window_at_coords_recurse (display,
784 tmp_list = tmp_list->next;
787 gdk_x11_display_error_trap_pop_ignored (display);
792 return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
795 /*************************************************************
796 ***************************** MOTIF *************************
797 *************************************************************/
799 /* values used in the message type for Motif DND */
812 /* Values used to specify type of protocol to use */
816 XmDRAG_PREFER_PREREGISTER,
818 XmDRAG_PREFER_DYNAMIC,
820 XmDRAG_PREFER_RECEIVER
823 /* Operation codes */
831 /* Drop site status */
833 XmNO_DROP_SITE = 0x01,
834 XmDROP_SITE_INVALID = 0x02,
835 XmDROP_SITE_VALID = 0x03
838 /* completion status */
846 /* Byte swapping routines. The motif specification leaves it
847 * up to us to save a few bytes in the client messages
849 #if G_BYTE_ORDER == G_BIG_ENDIAN
850 static gchar local_byte_order = 'B';
852 static gchar local_byte_order = 'l';
855 #ifdef G_ENABLE_DEBUG
857 print_target_list (GList *targets)
861 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
862 g_message ("\t%s", name);
864 targets = targets->next;
867 #endif /* G_ENABLE_DEBUG */
870 card16_to_host (guint16 x, gchar byte_order)
872 if (byte_order == local_byte_order)
875 return (x << 8) | (x >> 8);
879 card32_to_host (guint32 x, gchar byte_order)
881 if (byte_order == local_byte_order)
884 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
887 /* Motif packs together fields of varying length into the
888 * client message. We can't rely on accessing these
889 * through data.s[], data.l[], etc, because on some architectures
890 * (i.e., Alpha) these won't be valid for format == 8.
893 #define MOTIF_XCLIENT_BYTE(xevent,i) \
894 (xevent)->xclient.data.b[i]
895 #define MOTIF_XCLIENT_SHORT(xevent,i) \
896 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
897 #define MOTIF_XCLIENT_LONG(xevent,i) \
898 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
900 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
901 #define MOTIF_UNPACK_SHORT(xevent,i) \
902 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
903 #define MOTIF_UNPACK_LONG(xevent,i) \
904 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
906 /***** Dest side ***********/
908 /* Property placed on source windows */
909 typedef struct _MotifDragInitiatorInfo
912 guint8 protocol_version;
913 guint16 targets_index;
914 guint32 selection_atom;
915 } MotifDragInitiatorInfo;
917 /* Header for target table on the drag window */
918 typedef struct _MotifTargetTableHeader
921 guchar protocol_version;
924 } MotifTargetTableHeader;
926 /* Property placed on target windows */
927 typedef struct _MotifDragReceiverInfo
930 guint8 protocol_version;
931 guint8 protocol_style;
933 guint32 proxy_window;
934 guint16 num_drop_sites;
937 } MotifDragReceiverInfo;
939 /* Target table handling */
941 static GdkFilterReturn
942 motif_drag_window_filter (GdkXEvent *xevent,
946 XEvent *xev = (XEvent *)xevent;
947 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
948 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
950 switch (xev->xany.type)
953 display_x11->motif_drag_window = None;
954 display_x11->motif_drag_gdk_window = NULL;
957 if (display_x11->motif_target_lists &&
958 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
959 motif_read_target_table (display);
962 return GDK_FILTER_REMOVE;
966 motif_lookup_drag_window (GdkDisplay *display,
967 Display *lookup_xdisplay)
969 Window retval = None;
970 gulong bytes_after, nitems;
975 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
976 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
978 XA_WINDOW, &type, &format, &nitems, &bytes_after,
981 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
983 retval = *(Window *)data;
985 g_message ("Found drag window %#lx\n", GDK_X11_DISPLAY (display)->motif_drag_window));
994 /* Finds the window where global Motif drag information is stored.
995 * If it doesn't exist and 'create' is TRUE, create one.
998 motif_find_drag_window (GdkDisplay *display,
1001 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1003 if (!display_x11->motif_drag_window)
1005 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
1006 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
1008 if (!display_x11->motif_drag_window && create)
1010 /* Create a persistant window. (Copied from LessTif) */
1011 Display *persistant_xdisplay;
1012 XSetWindowAttributes attr;
1013 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
1014 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
1016 XGrabServer (persistant_xdisplay);
1018 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
1020 if (!display_x11->motif_drag_window)
1022 attr.override_redirect = True;
1023 attr.event_mask = PropertyChangeMask;
1025 display_x11->motif_drag_window =
1026 XCreateWindow (persistant_xdisplay,
1027 RootWindow (persistant_xdisplay, 0),
1028 -100, -100, 10, 10, 0, 0,
1029 InputOnly, (Visual *)CopyFromParent,
1030 (CWOverrideRedirect | CWEventMask), &attr);
1033 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
1035 XChangeProperty (persistant_xdisplay,
1036 RootWindow (persistant_xdisplay, 0),
1037 motif_drag_window_atom, XA_WINDOW,
1038 32, PropModeReplace,
1039 (guchar *)&motif_drag_window_atom, 1);
1041 XUngrabServer (persistant_xdisplay);
1042 XCloseDisplay (persistant_xdisplay);
1045 /* There is a miniscule race condition here if the drag window
1046 * gets destroyed exactly now.
1048 if (display_x11->motif_drag_window)
1050 display_x11->motif_drag_gdk_window =
1051 gdk_x11_window_foreign_new_for_display (display, display_x11->motif_drag_window);
1052 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
1053 motif_drag_window_filter,
1058 return display_x11->motif_drag_window;
1062 motif_read_target_table (GdkDisplay *display)
1064 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1065 gulong bytes_after, nitems;
1070 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1072 if (display_x11->motif_target_lists)
1074 for (i=0; i<display_x11->motif_n_target_lists; i++)
1075 g_list_free (display_x11->motif_target_lists[i]);
1077 g_free (display_x11->motif_target_lists);
1078 display_x11->motif_target_lists = NULL;
1079 display_x11->motif_n_target_lists = 0;
1082 if (motif_find_drag_window (display, FALSE))
1085 MotifTargetTableHeader *header = NULL;
1086 guchar *target_bytes = NULL;
1088 gboolean success = FALSE;
1090 gdk_x11_display_error_trap_push (display);
1091 XGetWindowProperty (display_x11->xdisplay,
1092 display_x11->motif_drag_window,
1093 motif_drag_targets_atom,
1094 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1095 motif_drag_targets_atom,
1096 &type, &format, &nitems, &bytes_after,
1099 if (gdk_x11_display_error_trap_pop (display) ||
1100 (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1103 header = (MotifTargetTableHeader *)data;
1105 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1106 header->total_size = card32_to_host (header->total_size, header->byte_order);
1108 gdk_x11_display_error_trap_push (display);
1109 XGetWindowProperty (display_x11->xdisplay,
1110 display_x11->motif_drag_window,
1111 motif_drag_targets_atom,
1112 (sizeof(MotifTargetTableHeader)+3)/4,
1113 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1115 motif_drag_targets_atom, &type, &format, &nitems,
1116 &bytes_after, &target_bytes);
1118 if (gdk_x11_display_error_trap_pop (display) ||
1119 (format != 8) || (bytes_after != 0) ||
1120 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1123 display_x11->motif_n_target_lists = header->n_lists;
1124 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1127 for (i=0; i<header->n_lists; i++)
1132 if (p + sizeof(guint16) - target_bytes > nitems)
1135 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1137 /* We need to make a copy of the targets, since it may
1140 targets = g_new (guint32, n_targets);
1141 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1143 p += sizeof(guint16) + n_targets * sizeof(guint32);
1144 if (p - target_bytes > nitems)
1147 for (j=0; j<n_targets; j++)
1148 display_x11->motif_target_lists[i] =
1149 g_list_prepend (display_x11->motif_target_lists[i],
1150 GUINT_TO_POINTER (card32_to_host (targets[j],
1151 header->byte_order)));
1153 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1163 XFree (target_bytes);
1167 if (display_x11->motif_target_lists)
1169 g_free (display_x11->motif_target_lists);
1170 display_x11->motif_target_lists = NULL;
1171 display_x11->motif_n_target_lists = 0;
1173 g_warning ("Error reading Motif target table\n");
1179 targets_sort_func (gconstpointer a, gconstpointer b)
1181 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1182 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1185 /* Check if given (sorted) list is in the targets table */
1187 motif_target_table_check (GdkDisplay *display,
1190 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1191 GList *tmp_list1, *tmp_list2;
1194 for (i=0; i<display_x11->motif_n_target_lists; i++)
1196 tmp_list1 = display_x11->motif_target_lists[i];
1199 while (tmp_list1 && tmp_list2)
1201 if (tmp_list1->data != tmp_list2->data)
1204 tmp_list1 = tmp_list1->next;
1205 tmp_list2 = tmp_list2->next;
1207 if (!tmp_list1 && !tmp_list2) /* Found it */
1215 motif_add_to_target_table (GdkDisplay *display,
1216 GList *targets) /* targets is list of GdkAtom */
1218 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1219 GList *sorted = NULL;
1224 /* make a sorted copy of the list */
1227 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1228 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1229 targets = targets->next;
1232 /* First check if it is there already */
1234 if (display_x11->motif_target_lists)
1235 index = motif_target_table_check (display, sorted);
1237 /* We need to grab the server while doing this, to ensure
1243 /* We need to make sure that it exists _before_ we grab the
1244 * server, since we can't open a new connection after we
1247 motif_find_drag_window (display, TRUE);
1249 gdk_x11_display_grab (display);
1250 motif_read_target_table (display);
1252 /* Check again, in case it was added in the meantime */
1253 if (display_x11->motif_target_lists)
1254 index = motif_target_table_check (display, sorted);
1258 guint32 total_size = 0;
1262 MotifTargetTableHeader *header;
1264 if (!display_x11->motif_target_lists)
1266 display_x11->motif_target_lists = g_new (GList *, 1);
1267 display_x11->motif_n_target_lists = 1;
1271 display_x11->motif_n_target_lists++;
1272 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1273 sizeof(GList *) * display_x11->motif_n_target_lists);
1275 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1277 index = display_x11->motif_n_target_lists - 1;
1279 total_size = sizeof (MotifTargetTableHeader);
1280 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1281 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1283 data = g_malloc (total_size);
1285 header = (MotifTargetTableHeader *)data;
1286 p = data + sizeof(MotifTargetTableHeader);
1288 header->byte_order = local_byte_order;
1289 header->protocol_version = 0;
1290 header->n_lists = display_x11->motif_n_target_lists;
1291 header->total_size = total_size;
1293 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1295 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1296 guint32 *targets = g_new (guint32, n_targets);
1297 guint32 *p32 = targets;
1299 tmp_list = display_x11->motif_target_lists[i];
1302 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1304 tmp_list = tmp_list->next;
1309 p += sizeof(guint16);
1311 memcpy (p, targets, n_targets * sizeof(guint32));
1314 p += sizeof(guint32) * n_targets;
1318 XChangeProperty (display_x11->xdisplay,
1319 display_x11->motif_drag_window,
1320 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1321 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1325 gdk_x11_display_ungrab (display);
1328 g_list_free (sorted);
1332 /* Translate flags */
1335 motif_dnd_translate_flags (GdkX11DragContext *context_x11,
1338 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1339 guint recommended_op = flags & 0x000f;
1340 guint possible_ops = (flags & 0x0f0) >> 4;
1342 switch (recommended_op)
1345 context->suggested_action = GDK_ACTION_MOVE;
1348 context->suggested_action = GDK_ACTION_COPY;
1351 context->suggested_action = GDK_ACTION_LINK;
1354 context->suggested_action = GDK_ACTION_COPY;
1358 context->actions = 0;
1359 if (possible_ops & XmDROP_MOVE)
1360 context->actions |= GDK_ACTION_MOVE;
1361 if (possible_ops & XmDROP_COPY)
1362 context->actions |= GDK_ACTION_COPY;
1363 if (possible_ops & XmDROP_LINK)
1364 context->actions |= GDK_ACTION_LINK;
1368 motif_dnd_get_flags (GdkDragContext *context)
1372 switch (context->suggested_action)
1374 case GDK_ACTION_MOVE:
1375 flags = XmDROP_MOVE;
1377 case GDK_ACTION_COPY:
1378 flags = XmDROP_COPY;
1380 case GDK_ACTION_LINK:
1381 flags = XmDROP_LINK;
1384 flags = XmDROP_NOOP;
1388 if (context->actions & GDK_ACTION_MOVE)
1389 flags |= XmDROP_MOVE << 8;
1390 if (context->actions & GDK_ACTION_COPY)
1391 flags |= XmDROP_COPY << 8;
1392 if (context->actions & GDK_ACTION_LINK)
1393 flags |= XmDROP_LINK << 8;
1401 motif_set_targets (GdkX11DragContext *context_x11)
1403 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1404 MotifDragInitiatorInfo info;
1406 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1408 info.byte_order = local_byte_order;
1409 info.protocol_version = 0;
1410 info.targets_index = motif_add_to_target_table (display, context->targets);
1415 g_snprintf (buf, 20, "_GDK_SELECTION_%d", i);
1417 context_x11->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1418 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), context_x11->motif_selection))
1422 info.selection_atom = context_x11->motif_selection;
1424 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1425 GDK_WINDOW_XID (context->source_window),
1426 context_x11->motif_selection,
1427 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1429 (guchar *)&info, sizeof (info));
1431 context_x11->motif_targets_set = 1;
1435 motif_check_dest (GdkDisplay *display,
1438 gboolean retval = FALSE;
1440 MotifDragReceiverInfo *info;
1443 unsigned long nitems, after;
1444 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1446 gdk_x11_display_error_trap_push (display);
1447 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1448 motif_drag_receiver_info_atom,
1449 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1450 &type, &format, &nitems, &after,
1453 if (gdk_x11_display_error_trap_pop (display) == 0)
1457 info = (MotifDragReceiverInfo *)data;
1459 if ((format == 8) && (nitems == sizeof(*info)))
1461 if ((info->protocol_version == 0) &&
1462 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1463 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1464 (info->protocol_style == XmDRAG_DYNAMIC)))
1470 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1477 return retval ? win : None;
1481 motif_send_enter (GdkX11DragContext *context_x11,
1484 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1485 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1488 if (!G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
1489 return; /* Motif Dnd requires getting properties on the root window */
1491 xev.xclient.type = ClientMessage;
1492 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1493 xev.xclient.format = 8;
1494 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1496 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1497 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1498 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1499 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1500 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XID (context->source_window);
1502 if (!context_x11->motif_targets_set)
1503 motif_set_targets (context_x11);
1505 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1506 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1508 if (!_gdk_x11_display_send_xevent (display,
1509 GDK_WINDOW_XID (context->dest_window),
1512 g_message ("Send event to %lx failed",
1513 GDK_WINDOW_XID (context->dest_window)));
1517 motif_send_leave (GdkX11DragContext *context_x11,
1520 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1521 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1524 xev.xclient.type = ClientMessage;
1525 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1526 xev.xclient.format = 8;
1527 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1529 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1530 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1531 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1532 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1533 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1534 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1535 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1537 if (!_gdk_x11_display_send_xevent (display,
1538 GDK_WINDOW_XID (context->dest_window),
1541 g_message ("Send event to %lx failed",
1542 GDK_WINDOW_XID (context->dest_window)));
1546 motif_send_motion (GdkX11DragContext *context_x11,
1549 GdkDragAction action,
1552 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1553 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1557 xev.xclient.type = ClientMessage;
1558 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1559 xev.xclient.format = 8;
1560 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1562 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1563 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1564 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1565 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1566 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1568 if ((context->suggested_action != context_x11->old_action) ||
1569 (context->actions != context_x11->old_actions))
1571 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1573 /* context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1578 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1580 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1581 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1583 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1587 if (!_gdk_x11_display_send_xevent (display,
1588 GDK_WINDOW_XID (context->dest_window),
1591 g_message ("Send event to %lx failed",
1592 GDK_WINDOW_XID (context->dest_window)));
1598 motif_send_drop (GdkX11DragContext *context_x11,
1601 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1602 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1605 xev.xclient.type = ClientMessage;
1606 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1607 xev.xclient.format = 8;
1608 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1610 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1611 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1612 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1613 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1615 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
1616 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
1618 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1619 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XID (context->source_window);
1621 if (!_gdk_x11_display_send_xevent (display,
1622 GDK_WINDOW_XID (context->dest_window),
1625 g_message ("Send event to %lx failed",
1626 GDK_WINDOW_XID (context->dest_window)));
1632 motif_read_initiator_info (GdkDisplay *display,
1633 Window source_window,
1644 MotifDragInitiatorInfo *initiator_info;
1646 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1648 gdk_x11_display_error_trap_push (display);
1649 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1650 0, sizeof(*initiator_info), FALSE,
1651 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1652 &type, &format, &nitems, &bytes_after,
1655 if (gdk_x11_display_error_trap_pop (display) ||
1656 (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) ||
1659 g_warning ("Error reading initiator info\n");
1663 initiator_info = (MotifDragInitiatorInfo *)data;
1665 motif_read_target_table (display);
1667 initiator_info->targets_index =
1668 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1669 initiator_info->selection_atom =
1670 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1672 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1674 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1675 XFree (initiator_info);
1679 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1684 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1685 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1686 tmp_list = tmp_list->prev;
1689 #ifdef G_ENABLE_DEBUG
1690 if (_gdk_debug_flags & GDK_DEBUG_DND)
1691 print_target_list (*targets);
1692 #endif /* G_ENABLE_DEBUG */
1694 *selection = initiator_info->selection_atom;
1696 XFree (initiator_info);
1701 static GdkDragContext *
1702 motif_drag_context_new (GdkWindow *dest_window,
1704 guint32 source_window,
1707 GdkX11DragContext *context_x11;
1708 GdkDragContext *context;
1709 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
1710 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1712 /* FIXME, current_dest_drag really shouldn't be NULL'd
1713 * if we error below.
1715 if (display_x11->current_dest_drag != NULL)
1717 if (timestamp >= display_x11->current_dest_drag->start_time)
1719 g_object_unref (display_x11->current_dest_drag);
1720 display_x11->current_dest_drag = NULL;
1726 context_x11 = g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
1727 context = GDK_DRAG_CONTEXT (context_x11);
1729 context->protocol = GDK_DRAG_PROTO_MOTIF;
1730 context->is_source = FALSE;
1732 context->source_window = gdk_x11_window_lookup_for_display (display, source_window);
1733 if (context->source_window)
1734 g_object_ref (context->source_window);
1737 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
1738 if (!context->source_window)
1740 g_object_unref (context_x11);
1745 context->dest_window = dest_window;
1746 g_object_ref (dest_window);
1747 context->start_time = timestamp;
1749 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1753 &context_x11->motif_selection))
1755 g_object_unref (context_x11);
1763 * The MOTIF drag protocol has no real provisions for distinguishing
1764 * multiple simultaneous drops. If the sources grab the pointer
1765 * when doing drags, that shouldn't happen, in any case. If it
1766 * does, we can't do much except hope for the best.
1769 static GdkFilterReturn
1770 motif_top_level_enter (GdkEvent *event,
1773 guint32 source_window,
1776 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1777 GdkDragContext *new_context;
1779 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1780 flags, timestamp, source_window, atom));
1782 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1784 return GDK_FILTER_REMOVE;
1786 event->dnd.type = GDK_DRAG_ENTER;
1787 event->dnd.context = new_context;
1788 g_object_ref (new_context);
1790 display_x11->current_dest_drag = new_context;
1792 return GDK_FILTER_TRANSLATE;
1795 static GdkFilterReturn
1796 motif_top_level_leave (GdkEvent *event,
1800 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1802 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1805 if ((display_x11->current_dest_drag != NULL) &&
1806 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1807 (timestamp >= display_x11->current_dest_drag->start_time))
1809 event->dnd.type = GDK_DRAG_LEAVE;
1810 /* Pass ownership of context to the event */
1811 event->dnd.context = display_x11->current_dest_drag;
1813 display_x11->current_dest_drag = NULL;
1815 return GDK_FILTER_TRANSLATE;
1818 return GDK_FILTER_REMOVE;
1821 static GdkFilterReturn
1822 motif_motion (GdkEvent *event,
1828 GdkX11DragContext *context_x11;
1829 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1831 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1832 flags, timestamp, x_root, y_root));
1834 if ((display_x11->current_dest_drag != NULL) &&
1835 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1836 (timestamp >= display_x11->current_dest_drag->start_time))
1838 context_x11 = GDK_X11_DRAG_CONTEXT (display_x11->current_dest_drag);
1840 event->dnd.type = GDK_DRAG_MOTION;
1841 event->dnd.context = display_x11->current_dest_drag;
1842 g_object_ref (display_x11->current_dest_drag);
1844 event->dnd.time = timestamp;
1846 motif_dnd_translate_flags (context_x11, flags);
1848 event->dnd.x_root = x_root;
1849 event->dnd.y_root = y_root;
1851 context_x11->last_x = x_root;
1852 context_x11->last_y = y_root;
1854 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1856 return GDK_FILTER_TRANSLATE;
1859 return GDK_FILTER_REMOVE;
1862 static GdkFilterReturn
1863 motif_operation_changed (GdkEvent *event,
1867 GdkX11DragContext *context_x11;
1868 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1869 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1872 if ((display_x11->current_dest_drag != NULL) &&
1873 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1874 (timestamp >= display_x11->current_dest_drag->start_time))
1876 event->dnd.type = GDK_DRAG_MOTION;
1877 event->dnd.send_event = FALSE;
1878 event->dnd.context = display_x11->current_dest_drag;
1879 g_object_ref (display_x11->current_dest_drag);
1881 event->dnd.time = timestamp;
1882 context_x11 = GDK_X11_DRAG_CONTEXT (display_x11->current_dest_drag);
1884 motif_dnd_translate_flags (context_x11, flags);
1886 event->dnd.x_root = context_x11->last_x;
1887 event->dnd.y_root = context_x11->last_y;
1889 context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1891 return GDK_FILTER_TRANSLATE;
1894 return GDK_FILTER_REMOVE;
1897 static GdkFilterReturn
1898 motif_drop_start (GdkEvent *event,
1901 guint32 source_window,
1906 GdkDragContext *new_context;
1907 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1909 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1910 flags, timestamp, x_root, y_root, source_window, atom));
1912 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1914 return GDK_FILTER_REMOVE;
1916 motif_dnd_translate_flags (GDK_X11_DRAG_CONTEXT (new_context), flags);
1918 event->dnd.type = GDK_DROP_START;
1919 event->dnd.context = new_context;
1920 event->dnd.time = timestamp;
1921 event->dnd.x_root = x_root;
1922 event->dnd.y_root = y_root;
1924 gdk_x11_window_set_user_time (event->any.window, timestamp);
1926 g_object_ref (new_context);
1927 display_x11->current_dest_drag = new_context;
1929 return GDK_FILTER_TRANSLATE;
1932 static GdkFilterReturn
1933 motif_drag_status (GdkEvent *event,
1937 GdkDragContext *context;
1938 GdkDisplay *display;
1941 g_message ("Motif status message: flags %x", flags));
1943 display = gdk_window_get_display (event->any.window);
1945 return GDK_FILTER_REMOVE;
1947 context = gdk_drag_context_find (display, TRUE, GDK_WINDOW_XID (event->any.window), None);
1951 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
1952 if ((context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1953 (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1954 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
1956 event->dnd.type = GDK_DRAG_STATUS;
1957 event->dnd.send_event = FALSE;
1958 event->dnd.context = context;
1959 g_object_ref (context);
1961 event->dnd.time = timestamp;
1963 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1965 switch (flags & 0x000f)
1968 context->action = 0;
1971 context->action = GDK_ACTION_MOVE;
1974 context->action = GDK_ACTION_COPY;
1977 context->action = GDK_ACTION_LINK;
1982 context->action = 0;
1984 return GDK_FILTER_TRANSLATE;
1986 return GDK_FILTER_REMOVE;
1989 static GdkFilterReturn
1990 motif_dnd_filter (GdkXEvent *xev,
1994 XEvent *xevent = (XEvent *)xev;
1999 guint32 source_window;
2001 gint16 x_root, y_root;
2004 if (!event->any.window ||
2005 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2006 return GDK_FILTER_CONTINUE; /* Not for us */
2008 /* First read some fields common to all Motif DND messages */
2009 reason = MOTIF_UNPACK_BYTE (xevent, 0);
2010 flags = MOTIF_UNPACK_SHORT (xevent, 1);
2011 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
2013 is_reply = ((reason & 0x80) != 0);
2015 switch (reason & 0x7f)
2017 case XmTOP_LEVEL_ENTER:
2018 source_window = MOTIF_UNPACK_LONG (xevent, 2);
2019 atom = MOTIF_UNPACK_LONG (xevent, 3);
2020 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
2021 case XmTOP_LEVEL_LEAVE:
2022 return motif_top_level_leave (event, flags, timestamp);
2025 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2026 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2029 return motif_motion (event, flags, timestamp, x_root, y_root);
2031 return motif_drag_status (event, flags, timestamp);
2033 case XmDROP_SITE_ENTER:
2034 return motif_drag_status (event, flags, timestamp);
2036 case XmDROP_SITE_LEAVE:
2037 return motif_drag_status (event,
2038 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
2041 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2042 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2043 atom = MOTIF_UNPACK_LONG (xevent, 3);
2044 source_window = MOTIF_UNPACK_LONG (xevent, 4);
2047 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
2050 case XmOPERATION_CHANGED:
2052 return motif_operation_changed (event, flags, timestamp);
2054 return motif_drag_status (event, flags, timestamp);
2057 /* To the best of my knowledge, these next two messages are
2058 * not part of the protocol, though they are defined in
2062 case XmDRAG_DROP_FINISH:
2066 return GDK_FILTER_REMOVE;
2069 /*************************************************************
2070 ***************************** XDND **************************
2071 *************************************************************/
2073 /* Utility functions */
2078 GdkDragAction action;
2079 } xdnd_actions_table[] = {
2080 { "XdndActionCopy", None, GDK_ACTION_COPY },
2081 { "XdndActionMove", None, GDK_ACTION_MOVE },
2082 { "XdndActionLink", None, GDK_ACTION_LINK },
2083 { "XdndActionAsk", None, GDK_ACTION_ASK },
2084 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2087 static const gint xdnd_n_actions = G_N_ELEMENTS (xdnd_actions_table);
2088 static gboolean xdnd_actions_initialized = FALSE;
2091 xdnd_initialize_actions (void)
2095 xdnd_actions_initialized = TRUE;
2096 for (i = 0; i < xdnd_n_actions; i++)
2097 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2100 static GdkDragAction
2101 xdnd_action_from_atom (GdkDisplay *display,
2110 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2112 if (!xdnd_actions_initialized)
2113 xdnd_initialize_actions();
2115 for (i = 0; i < xdnd_n_actions; i++)
2116 if (atom == xdnd_actions_table[i].atom)
2117 return xdnd_actions_table[i].action;
2123 xdnd_action_to_atom (GdkDisplay *display,
2124 GdkDragAction action)
2128 if (!xdnd_actions_initialized)
2129 xdnd_initialize_actions();
2131 for (i = 0; i < xdnd_n_actions; i++)
2132 if (action == xdnd_actions_table[i].action)
2133 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2140 static GdkFilterReturn
2141 xdnd_status_filter (GdkXEvent *xev,
2145 GdkDisplay *display;
2146 XEvent *xevent = (XEvent *)xev;
2147 guint32 dest_window = xevent->xclient.data.l[0];
2148 guint32 flags = xevent->xclient.data.l[1];
2149 Atom action = xevent->xclient.data.l[4];
2150 GdkDragContext *context;
2152 if (!event->any.window ||
2153 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2154 return GDK_FILTER_CONTINUE; /* Not for us */
2157 g_message ("XdndStatus: dest_window: %#x action: %ld",
2158 dest_window, action));
2160 display = gdk_window_get_display (event->any.window);
2161 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2165 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
2166 if (context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2167 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2169 event->dnd.send_event = FALSE;
2170 event->dnd.type = GDK_DRAG_STATUS;
2171 event->dnd.context = context;
2172 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2173 g_object_ref (context);
2175 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2176 if (!(action != 0) != !(flags & 1))
2179 g_warning ("Received status event with flags not corresponding to action!\n"));
2183 context->action = xdnd_action_from_atom (display, action);
2185 return GDK_FILTER_TRANSLATE;
2188 return GDK_FILTER_REMOVE;
2191 static GdkFilterReturn
2192 xdnd_finished_filter (GdkXEvent *xev,
2196 GdkDisplay *display;
2197 XEvent *xevent = (XEvent *)xev;
2198 guint32 dest_window = xevent->xclient.data.l[0];
2199 GdkDragContext *context;
2200 GdkX11DragContext *context_x11;
2202 if (!event->any.window ||
2203 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2204 return GDK_FILTER_CONTINUE; /* Not for us */
2207 g_message ("XdndFinished: dest_window: %#x", dest_window));
2209 display = gdk_window_get_display (event->any.window);
2210 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2214 context_x11 = GDK_X11_DRAG_CONTEXT (context);
2215 if (context_x11->version == 5)
2216 context_x11->drop_failed = xevent->xclient.data.l[1] == 0;
2218 event->dnd.type = GDK_DROP_FINISHED;
2219 event->dnd.context = context;
2220 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2221 g_object_ref (context);
2223 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2225 return GDK_FILTER_TRANSLATE;
2228 return GDK_FILTER_REMOVE;
2232 xdnd_set_targets (GdkX11DragContext *context_x11)
2234 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2236 GList *tmp_list = context->targets;
2238 gint n_atoms = g_list_length (context->targets);
2239 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2241 atomlist = g_new (Atom, n_atoms);
2245 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2246 tmp_list = tmp_list->next;
2250 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2251 GDK_WINDOW_XID (context->source_window),
2252 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2253 XA_ATOM, 32, PropModeReplace,
2254 (guchar *)atomlist, n_atoms);
2258 context_x11->xdnd_targets_set = 1;
2262 xdnd_set_actions (GdkX11DragContext *context_x11)
2264 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2269 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2271 if (!xdnd_actions_initialized)
2272 xdnd_initialize_actions();
2274 actions = context->actions;
2276 for (i = 0; i < xdnd_n_actions; i++)
2278 if (actions & xdnd_actions_table[i].action)
2280 actions &= ~xdnd_actions_table[i].action;
2285 atomlist = g_new (Atom, n_atoms);
2287 actions = context->actions;
2289 for (i = 0; i < xdnd_n_actions; i++)
2291 if (actions & xdnd_actions_table[i].action)
2293 actions &= ~xdnd_actions_table[i].action;
2294 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2299 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2300 GDK_WINDOW_XID (context->source_window),
2301 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2302 XA_ATOM, 32, PropModeReplace,
2303 (guchar *)atomlist, n_atoms);
2307 context_x11->xdnd_actions_set = TRUE;
2308 context_x11->xdnd_actions = context->actions;
2312 send_client_message_async_cb (Window window,
2316 GdkDragContext *context = data;
2318 g_message ("Got async callback for #%lx, success = %d",
2321 /* On failure, we immediately continue with the protocol
2322 * so we don't end up blocking for a timeout
2325 context->dest_window &&
2326 window == GDK_WINDOW_XID (context->dest_window))
2328 GdkEvent *temp_event;
2329 GdkX11DragContext *context_x11 = data;
2331 g_object_unref (context->dest_window);
2332 context->dest_window = NULL;
2333 context->action = 0;
2335 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2337 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2338 temp_event->dnd.window = g_object_ref (context->source_window);
2339 temp_event->dnd.send_event = TRUE;
2340 temp_event->dnd.context = g_object_ref (context);
2341 temp_event->dnd.time = GDK_CURRENT_TIME;
2342 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2344 gdk_event_put (temp_event);
2346 gdk_event_free (temp_event);
2349 g_object_unref (context);
2354 gdk_drag_context_get_display (GdkDragContext *context)
2356 if (context->source_window)
2357 return GDK_WINDOW_DISPLAY (context->source_window);
2358 else if (context->dest_window)
2359 return GDK_WINDOW_DISPLAY (context->dest_window);
2361 g_assert_not_reached ();
2366 send_client_message_async (GdkDragContext *context,
2370 XClientMessageEvent *event_send)
2372 GdkDisplay *display = gdk_drag_context_get_display (context);
2374 g_object_ref (context);
2376 _gdk_x11_send_client_message_async (display, window,
2377 propagate, event_mask, event_send,
2378 send_client_message_async_cb, context);
2382 xdnd_send_xevent (GdkX11DragContext *context_x11,
2387 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2388 GdkDisplay *display = gdk_drag_context_get_display (context);
2392 g_assert (event_send->xany.type == ClientMessage);
2394 /* We short-circuit messages to ourselves */
2395 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2399 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2401 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2402 event_send->xclient.message_type)
2404 GdkEvent *temp_event;
2406 temp_event = gdk_event_new (GDK_NOTHING);
2407 temp_event->any.window = g_object_ref (window);
2409 if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
2411 gdk_event_put (temp_event);
2412 gdk_event_free (temp_event);
2420 xwindow = GDK_WINDOW_XID (window);
2422 if (_gdk_x11_display_is_root_window (display, xwindow))
2423 event_mask = ButtonPressMask;
2427 send_client_message_async (context, xwindow, propagate, event_mask,
2428 &event_send->xclient);
2434 xdnd_send_enter (GdkX11DragContext *context_x11)
2436 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2437 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->dest_window);
2440 xev.xclient.type = ClientMessage;
2441 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2442 xev.xclient.format = 32;
2443 xev.xclient.window = context_x11->drop_xid
2444 ? context_x11->drop_xid
2445 : GDK_WINDOW_XID (context->dest_window);
2446 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2447 xev.xclient.data.l[1] = (context_x11->version << 24); /* version */
2448 xev.xclient.data.l[2] = 0;
2449 xev.xclient.data.l[3] = 0;
2450 xev.xclient.data.l[4] = 0;
2453 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2454 GDK_WINDOW_XID (context->source_window), context_x11->version));
2455 if (g_list_length (context->targets) > 3)
2457 if (!context_x11->xdnd_targets_set)
2458 xdnd_set_targets (context_x11);
2459 xev.xclient.data.l[1] |= 1;
2463 GList *tmp_list = context->targets;
2468 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2469 GDK_POINTER_TO_ATOM (tmp_list->data));
2470 tmp_list = tmp_list->next;
2475 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2478 g_message ("Send event to %lx failed",
2479 GDK_WINDOW_XID (context->dest_window)));
2480 g_object_unref (context->dest_window);
2481 context->dest_window = NULL;
2486 xdnd_send_leave (GdkX11DragContext *context_x11)
2488 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2489 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2492 xev.xclient.type = ClientMessage;
2493 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2494 xev.xclient.format = 32;
2495 xev.xclient.window = context_x11->drop_xid
2496 ? context_x11->drop_xid
2497 : GDK_WINDOW_XID (context->dest_window);
2498 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2499 xev.xclient.data.l[1] = 0;
2500 xev.xclient.data.l[2] = 0;
2501 xev.xclient.data.l[3] = 0;
2502 xev.xclient.data.l[4] = 0;
2504 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2507 g_message ("Send event to %lx failed",
2508 GDK_WINDOW_XID (context->dest_window)));
2509 g_object_unref (context->dest_window);
2510 context->dest_window = NULL;
2515 xdnd_send_drop (GdkX11DragContext *context_x11,
2518 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2519 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2522 xev.xclient.type = ClientMessage;
2523 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2524 xev.xclient.format = 32;
2525 xev.xclient.window = context_x11->drop_xid
2526 ? context_x11->drop_xid
2527 : GDK_WINDOW_XID (context->dest_window);
2528 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2529 xev.xclient.data.l[1] = 0;
2530 xev.xclient.data.l[2] = time;
2531 xev.xclient.data.l[3] = 0;
2532 xev.xclient.data.l[4] = 0;
2534 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2537 g_message ("Send event to %lx failed",
2538 GDK_WINDOW_XID (context->dest_window)));
2539 g_object_unref (context->dest_window);
2540 context->dest_window = NULL;
2545 xdnd_send_motion (GdkX11DragContext *context_x11,
2548 GdkDragAction action,
2551 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2552 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2555 xev.xclient.type = ClientMessage;
2556 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2557 xev.xclient.format = 32;
2558 xev.xclient.window = context_x11->drop_xid
2559 ? context_x11->drop_xid
2560 : GDK_WINDOW_XID (context->dest_window);
2561 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2562 xev.xclient.data.l[1] = 0;
2563 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2564 xev.xclient.data.l[3] = time;
2565 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2567 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2570 g_message ("Send event to %lx failed",
2571 GDK_WINDOW_XID (context->dest_window)));
2572 g_object_unref (context->dest_window);
2573 context->dest_window = NULL;
2575 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2579 xdnd_check_dest (GdkDisplay *display,
2581 guint *xdnd_version)
2583 gboolean retval = FALSE;
2586 unsigned long nitems, after;
2591 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2592 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2596 gdk_x11_display_error_trap_push (display);
2597 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2599 1, False, AnyPropertyType,
2600 &type, &format, &nitems, &after,
2605 proxy_data = (Window *)data;
2607 if ((format == 32) && (nitems == 1))
2609 proxy = *proxy_data;
2613 g_warning ("Invalid XdndProxy "
2614 "property on window %ld\n", win));
2619 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2621 1, False, AnyPropertyType,
2622 &type, &format, &nitems, &after,
2623 &data) == Success) &&
2626 version = (Atom *)data;
2628 if ((format == 32) && (nitems == 1))
2633 *xdnd_version = *version;
2637 g_warning ("Invalid XdndAware "
2638 "property on window %ld\n", win));
2644 gdk_x11_display_error_trap_pop_ignored (display);
2646 return retval ? (proxy ? proxy : win) : None;
2652 xdnd_read_actions (GdkX11DragContext *context_x11)
2654 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2655 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2658 gulong nitems, after;
2663 context_x11->xdnd_have_actions = FALSE;
2665 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2667 /* Get the XdndActionList, if set */
2669 gdk_x11_display_error_trap_push (display);
2670 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2671 GDK_WINDOW_XID (context->source_window),
2672 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2674 False, XA_ATOM, &type, &format, &nitems,
2675 &after, &data) == Success &&
2678 atoms = (Atom *)data;
2680 context->actions = 0;
2682 for (i = 0; i < nitems; i++)
2683 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2685 context_x11->xdnd_have_actions = TRUE;
2687 #ifdef G_ENABLE_DEBUG
2688 if (_gdk_debug_flags & GDK_DEBUG_DND)
2690 GString *action_str = g_string_new (NULL);
2691 if (context->actions & GDK_ACTION_MOVE)
2692 g_string_append(action_str, "MOVE ");
2693 if (context->actions & GDK_ACTION_COPY)
2694 g_string_append(action_str, "COPY ");
2695 if (context->actions & GDK_ACTION_LINK)
2696 g_string_append(action_str, "LINK ");
2697 if (context->actions & GDK_ACTION_ASK)
2698 g_string_append(action_str, "ASK ");
2700 g_message("Xdnd actions = %s", action_str->str);
2701 g_string_free (action_str, TRUE);
2703 #endif /* G_ENABLE_DEBUG */
2710 gdk_x11_display_error_trap_pop_ignored (display);
2716 GdkDragContext *source_context;
2718 source_context = gdk_drag_context_find (display, TRUE,
2719 GDK_WINDOW_XID (context->source_window),
2720 GDK_WINDOW_XID (context->dest_window));
2724 context->actions = source_context->actions;
2725 context_x11->xdnd_have_actions = TRUE;
2730 /* We have to make sure that the XdndActionList we keep internally
2731 * is up to date with the XdndActionList on the source window
2732 * because we get no notification, because Xdnd wasn't meant
2733 * to continually send actions. So we select on PropertyChangeMask
2734 * and add this filter.
2736 static GdkFilterReturn
2737 xdnd_source_window_filter (GdkXEvent *xev,
2741 XEvent *xevent = (XEvent *)xev;
2742 GdkX11DragContext *context_x11 = cb_data;
2743 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2745 if ((xevent->xany.type == PropertyNotify) &&
2746 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2748 xdnd_read_actions (context_x11);
2750 return GDK_FILTER_REMOVE;
2753 return GDK_FILTER_CONTINUE;
2757 xdnd_manage_source_filter (GdkDragContext *context,
2759 gboolean add_filter)
2761 if (!GDK_WINDOW_DESTROYED (window) &&
2762 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2764 gdk_x11_display_error_trap_push (GDK_WINDOW_DISPLAY (window));
2768 gdk_window_set_events (window,
2769 gdk_window_get_events (window) |
2770 GDK_PROPERTY_CHANGE_MASK);
2771 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2775 gdk_window_remove_filter (window,
2776 xdnd_source_window_filter,
2778 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2779 * but we might want it for other reasons. (Like
2780 * INCR selection transactions).
2784 gdk_x11_display_error_trap_pop_ignored (GDK_WINDOW_DISPLAY (window));
2789 base_precache_atoms (GdkDisplay *display)
2791 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2793 if (!display_x11->base_dnd_atoms_precached)
2795 static const char *const precache_atoms[] = {
2796 "ENLIGHTENMENT_DESKTOP",
2800 "_MOTIF_DRAG_RECEIVER_INFO"
2803 _gdk_x11_precache_atoms (display,
2804 precache_atoms, G_N_ELEMENTS (precache_atoms));
2806 display_x11->base_dnd_atoms_precached = TRUE;
2811 xdnd_precache_atoms (GdkDisplay *display)
2813 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2815 if (!display_x11->xdnd_atoms_precached)
2817 static const gchar *const precache_atoms[] = {
2823 "XdndActionPrivate",
2834 _gdk_x11_precache_atoms (display,
2835 precache_atoms, G_N_ELEMENTS (precache_atoms));
2837 display_x11->xdnd_atoms_precached = TRUE;
2841 static GdkFilterReturn
2842 xdnd_enter_filter (GdkXEvent *xev,
2846 GdkDeviceManager *device_manager;
2847 GdkDisplay *display;
2848 GdkX11Display *display_x11;
2849 XEvent *xevent = (XEvent *)xev;
2850 GdkDragContext *context;
2851 GdkX11DragContext *context_x11;
2855 gulong nitems, after;
2858 guint32 source_window;
2862 if (!event->any.window ||
2863 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2864 return GDK_FILTER_CONTINUE; /* Not for us */
2866 source_window = xevent->xclient.data.l[0];
2867 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2868 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2870 display = GDK_WINDOW_DISPLAY (event->any.window);
2871 display_x11 = GDK_X11_DISPLAY (display);
2873 xdnd_precache_atoms (display);
2876 g_message ("XdndEnter: source_window: %#x, version: %#x",
2877 source_window, version));
2881 /* Old source ignore */
2882 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2883 return GDK_FILTER_REMOVE;
2886 if (display_x11->current_dest_drag != NULL)
2888 g_object_unref (display_x11->current_dest_drag);
2889 display_x11->current_dest_drag = NULL;
2892 context_x11 = (GdkX11DragContext *)g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
2893 context = (GdkDragContext *)context_x11;
2895 context->protocol = GDK_DRAG_PROTO_XDND;
2896 context_x11->version = version;
2898 /* FIXME: Should extend DnD protocol to have device info */
2899 device_manager = gdk_display_get_device_manager (display);
2900 gdk_drag_context_set_device (context, gdk_device_manager_get_client_pointer (device_manager));
2902 context->source_window = gdk_x11_window_lookup_for_display (display, source_window);
2903 if (context->source_window)
2904 g_object_ref (context->source_window);
2907 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
2908 if (!context->source_window)
2910 g_object_unref (context);
2911 return GDK_FILTER_REMOVE;
2914 context->dest_window = event->any.window;
2915 g_object_ref (context->dest_window);
2917 context->targets = NULL;
2920 gdk_x11_display_error_trap_push (display);
2921 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2923 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2925 False, XA_ATOM, &type, &format, &nitems,
2928 if (gdk_x11_display_error_trap_pop (display) || (format != 32) || (type != XA_ATOM))
2930 g_object_unref (context);
2935 return GDK_FILTER_REMOVE;
2938 atoms = (Atom *)data;
2940 for (i = 0; i < nitems; i++)
2942 g_list_append (context->targets,
2943 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2950 for (i = 0; i < 3; i++)
2951 if (xevent->xclient.data.l[2 + i])
2953 g_list_append (context->targets,
2954 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2955 xevent->xclient.data.l[2 + i])));
2958 #ifdef G_ENABLE_DEBUG
2959 if (_gdk_debug_flags & GDK_DEBUG_DND)
2960 print_target_list (context->targets);
2961 #endif /* G_ENABLE_DEBUG */
2963 xdnd_manage_source_filter (context, context->source_window, TRUE);
2964 xdnd_read_actions (context_x11);
2966 event->dnd.type = GDK_DRAG_ENTER;
2967 event->dnd.context = context;
2968 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2969 g_object_ref (context);
2971 display_x11->current_dest_drag = context;
2973 return GDK_FILTER_TRANSLATE;
2976 static GdkFilterReturn
2977 xdnd_leave_filter (GdkXEvent *xev,
2981 XEvent *xevent = (XEvent *)xev;
2982 guint32 source_window = xevent->xclient.data.l[0];
2983 GdkDisplay *display;
2984 GdkX11Display *display_x11;
2986 if (!event->any.window ||
2987 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2988 return GDK_FILTER_CONTINUE; /* Not for us */
2991 g_message ("XdndLeave: source_window: %#x",
2994 display = GDK_WINDOW_DISPLAY (event->any.window);
2995 display_x11 = GDK_X11_DISPLAY (display);
2997 xdnd_precache_atoms (display);
2999 if ((display_x11->current_dest_drag != NULL) &&
3000 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3001 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
3003 event->dnd.type = GDK_DRAG_LEAVE;
3004 /* Pass ownership of context to the event */
3005 event->dnd.context = display_x11->current_dest_drag;
3006 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
3008 display_x11->current_dest_drag = NULL;
3010 return GDK_FILTER_TRANSLATE;
3013 return GDK_FILTER_REMOVE;
3016 static GdkFilterReturn
3017 xdnd_position_filter (GdkXEvent *xev,
3021 XEvent *xevent = (XEvent *)xev;
3022 guint32 source_window = xevent->xclient.data.l[0];
3023 gint16 x_root = xevent->xclient.data.l[2] >> 16;
3024 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
3025 guint32 time = xevent->xclient.data.l[3];
3026 Atom action = xevent->xclient.data.l[4];
3027 GdkDisplay *display;
3028 GdkX11Display *display_x11;
3029 GdkDragContext *context;
3030 GdkX11DragContext *context_x11;
3032 if (!event->any.window ||
3033 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3034 return GDK_FILTER_CONTINUE; /* Not for us */
3037 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
3038 source_window, x_root, y_root, time, action));
3040 display = GDK_WINDOW_DISPLAY (event->any.window);
3041 display_x11 = GDK_X11_DISPLAY (display);
3043 xdnd_precache_atoms (display);
3045 context = display_x11->current_dest_drag;
3047 if ((context != NULL) &&
3048 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3049 (GDK_WINDOW_XID (context->source_window) == source_window))
3051 context_x11 = GDK_X11_DRAG_CONTEXT (context);
3053 event->dnd.type = GDK_DRAG_MOTION;
3054 event->dnd.context = context;
3055 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3056 g_object_ref (context);
3058 event->dnd.time = time;
3060 context->suggested_action = xdnd_action_from_atom (display, action);
3062 if (!context_x11->xdnd_have_actions)
3063 context->actions = context->suggested_action;
3065 event->dnd.x_root = x_root;
3066 event->dnd.y_root = y_root;
3068 context_x11->last_x = x_root;
3069 context_x11->last_y = y_root;
3071 return GDK_FILTER_TRANSLATE;
3074 return GDK_FILTER_REMOVE;
3077 static GdkFilterReturn
3078 xdnd_drop_filter (GdkXEvent *xev,
3082 XEvent *xevent = (XEvent *)xev;
3083 guint32 source_window = xevent->xclient.data.l[0];
3084 guint32 time = xevent->xclient.data.l[2];
3085 GdkDisplay *display;
3086 GdkX11Display *display_x11;
3087 GdkDragContext *context;
3088 GdkX11DragContext *context_x11;
3090 if (!event->any.window ||
3091 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3092 return GDK_FILTER_CONTINUE; /* Not for us */
3095 g_message ("XdndDrop: source_window: %#x time: %d",
3096 source_window, time));
3098 display = GDK_WINDOW_DISPLAY (event->any.window);
3099 display_x11 = GDK_X11_DISPLAY (display);
3101 xdnd_precache_atoms (display);
3103 context = display_x11->current_dest_drag;
3105 if ((context != NULL) &&
3106 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3107 (GDK_WINDOW_XID (context->source_window) == source_window))
3109 context_x11 = GDK_X11_DRAG_CONTEXT (context);
3110 event->dnd.type = GDK_DROP_START;
3112 event->dnd.context = context;
3113 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3114 g_object_ref (context);
3116 event->dnd.time = time;
3117 event->dnd.x_root = context_x11->last_x;
3118 event->dnd.y_root = context_x11->last_y;
3120 gdk_x11_window_set_user_time (event->any.window, time);
3122 return GDK_FILTER_TRANSLATE;
3125 return GDK_FILTER_REMOVE;
3129 _gdk_x11_display_init_dnd (GdkDisplay *display)
3133 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3135 gdk_display_add_client_message_filter (
3137 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
3138 xdnd_filters[i].func, NULL);
3145 gdk_drag_do_leave (GdkX11DragContext *context_x11,
3148 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
3150 if (context->dest_window)
3152 switch (context->protocol)
3154 case GDK_DRAG_PROTO_MOTIF:
3155 motif_send_leave (context_x11, time);
3157 case GDK_DRAG_PROTO_XDND:
3158 xdnd_send_leave (context_x11);
3160 case GDK_DRAG_PROTO_ROOTWIN:
3161 case GDK_DRAG_PROTO_NONE:
3166 g_object_unref (context->dest_window);
3167 context->dest_window = NULL;
3172 _gdk_x11_window_drag_begin (GdkWindow *window,
3176 GdkDragContext *context;
3178 context = (GdkDragContext *) g_object_new (GDK_TYPE_X11_DRAG_CONTEXT, NULL);
3180 context->is_source = TRUE;
3181 context->source_window = window;
3182 g_object_ref (window);
3184 context->targets = g_list_copy (targets);
3185 precache_target_list (context);
3187 context->actions = 0;
3189 gdk_drag_context_set_device (context, device);
3195 _gdk_x11_display_get_drag_protocol (GdkDisplay *display,
3196 GdkNativeWindow xid,
3197 GdkDragProtocol *protocol,
3202 GdkNativeWindow retval;
3204 base_precache_atoms (display);
3206 /* Check for a local drag */
3207 window = gdk_x11_window_lookup_for_display (display, xid);
3208 if (window && gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3210 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3212 *protocol = GDK_DRAG_PROTO_XDND;
3214 xdnd_precache_atoms (display);
3215 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3218 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3220 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3221 GDK_NOTE (DND, g_message ("Entering root window\n"));
3225 else if ((retval = xdnd_check_dest (display, xid, version)))
3227 *protocol = GDK_DRAG_PROTO_XDND;
3228 xdnd_precache_atoms (display);
3229 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3232 else if ((retval = motif_check_dest (display, xid)))
3234 *protocol = GDK_DRAG_PROTO_MOTIF;
3235 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3240 /* Check if this is a root window */
3241 gboolean rootwin = FALSE;
3243 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3248 GDK_NOTE (DND, g_message ("Entering root window\n"));
3249 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3254 *protocol = GDK_DRAG_PROTO_NONE;
3256 return 0; /* a.k.a. None */
3259 static GdkWindowCache *
3260 drag_context_find_window_cache (GdkX11DragContext *context_x11,
3264 GdkWindowCache *cache;
3266 for (list = context_x11->window_caches; list; list = list->next)
3269 if (cache->screen == screen)
3273 cache = gdk_window_cache_get (screen);
3274 context_x11->window_caches = g_slist_prepend (context_x11->window_caches, cache);
3280 gdk_x11_drag_context_find_window (GdkDragContext *context,
3281 GdkWindow *drag_window,
3285 GdkDragProtocol *protocol)
3287 GdkX11DragContext *context_x11 = GDK_X11_DRAG_CONTEXT (context);
3288 GdkWindowCache *window_cache;
3289 GdkDisplay *display;
3291 GdkWindow *dest_window;
3293 display = GDK_WINDOW_DISPLAY (context->source_window);
3295 window_cache = drag_context_find_window_cache (context_x11, screen);
3297 dest = get_client_window_at_coords (window_cache,
3298 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3299 GDK_WINDOW_XID (drag_window) : None,
3302 if (context_x11->dest_xid != dest)
3305 context_x11->dest_xid = dest;
3307 /* Check if new destination accepts drags, and which protocol */
3309 /* There is some ugliness here. We actually need to pass
3310 * _three_ pieces of information to drag_motion - dest_window,
3311 * protocol, and the XID of the unproxied window. The first
3312 * two are passed explicitely, the third implicitly through
3313 * protocol->dest_xid.
3315 recipient = _gdk_x11_display_get_drag_protocol (display,
3318 &context_x11->version);
3320 if (recipient != None)
3322 dest_window = gdk_x11_window_lookup_for_display (display, recipient);
3324 g_object_ref (dest_window);
3326 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;