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/.
28 #include <X11/Xatom.h>
31 #include "gdk.h" /* For gdk_flush() */
34 #include "gdkproperty.h"
35 #include "gdkprivate-x11.h"
36 #include "gdkinternals.h"
37 #include "gdkscreen-x11.h"
38 #include "gdkdisplay-x11.h"
40 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
44 GDK_DRAG_STATUS_MOTION_WAIT,
45 GDK_DRAG_STATUS_ACTION_WAIT,
51 gint x, y, width, height;
57 GHashTable *child_hash;
62 /* Structure that holds information about a drag in progress.
63 * this is used on both source and destination sides.
65 struct _GdkDragContextPrivateX11 {
66 GdkDragContext context;
72 guint16 last_x; /* Coordinates from last event */
74 GdkDragAction old_action; /* The last action we sent to the source */
75 GdkDragAction old_actions; /* The last actions we sent to the source */
76 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
78 Window dest_xid; /* The last window we looked up */
79 Window drop_xid; /* The (non-proxied) window that is receiving drops */
80 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
81 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
82 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
83 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
84 guint drag_status : 4; /* current status of drag */
86 GdkWindowCache *window_cache;
89 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
91 /* Forward declarations */
93 static void gdk_window_cache_destroy (GdkWindowCache *cache);
95 static void motif_read_target_table (GdkDisplay *display);
97 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
101 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
105 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
109 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
113 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
117 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
121 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
125 static void xdnd_manage_source_filter (GdkDragContext *context,
127 gboolean add_filter);
129 static void gdk_drag_context_init (GdkDragContext *dragcontext);
130 static void gdk_drag_context_class_init (GdkDragContextClass *klass);
131 static void gdk_drag_context_finalize (GObject *object);
133 static gpointer parent_class = NULL;
134 static GList *contexts;
137 gdk_drag_context_get_type (void)
139 static GType object_type = 0;
143 static const GTypeInfo object_info =
145 sizeof (GdkDragContextClass),
146 (GBaseInitFunc) NULL,
147 (GBaseFinalizeFunc) NULL,
148 (GClassInitFunc) gdk_drag_context_class_init,
149 NULL, /* class_finalize */
150 NULL, /* class_data */
151 sizeof (GdkDragContext),
153 (GInstanceInitFunc) gdk_drag_context_init,
156 object_type = g_type_register_static (G_TYPE_OBJECT,
165 gdk_drag_context_init (GdkDragContext *dragcontext)
167 GdkDragContextPrivateX11 *private;
169 private = g_new0 (GdkDragContextPrivateX11, 1);
171 dragcontext->windowing_data = private;
173 contexts = g_list_prepend (contexts, dragcontext);
177 gdk_drag_context_class_init (GdkDragContextClass *klass)
179 GObjectClass *object_class = G_OBJECT_CLASS (klass);
181 parent_class = g_type_class_peek_parent (klass);
183 object_class->finalize = gdk_drag_context_finalize;
187 gdk_drag_context_finalize (GObject *object)
189 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
190 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
192 g_list_free (context->targets);
194 if (context->source_window)
196 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
198 xdnd_manage_source_filter (context, context->source_window, FALSE);
200 gdk_window_unref (context->source_window);
203 if (context->dest_window)
204 gdk_window_unref (context->dest_window);
206 if (private->window_cache)
207 gdk_window_cache_destroy (private->window_cache);
209 contexts = g_list_remove (contexts, context);
213 G_OBJECT_CLASS (parent_class)->finalize (object);
219 gdk_drag_context_new (void)
221 return g_object_new (gdk_drag_context_get_type (), NULL);
225 gdk_drag_context_ref (GdkDragContext *context)
227 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
229 g_object_ref (G_OBJECT (context));
233 gdk_drag_context_unref (GdkDragContext *context)
235 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
237 g_object_unref (G_OBJECT (context));
240 static GdkDragContext *
241 gdk_drag_context_find (GdkDisplay *display,
246 GList *tmp_list = contexts;
247 GdkDragContext *context;
248 GdkDragContextPrivateX11 *private;
249 Window context_dest_xid;
253 context = (GdkDragContext *)tmp_list->data;
254 private = PRIVATE_DATA (context);
256 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
257 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
260 context_dest_xid = context->dest_window ?
263 GDK_DRAWABLE_XID (context->dest_window)) :
266 if ((!context->is_source == !is_source) &&
267 ((source_xid == None) || (context->source_window &&
268 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
269 ((dest_xid == None) || (context_dest_xid == dest_xid)))
272 tmp_list = tmp_list->next;
278 /* Utility functions */
281 gdk_window_cache_add (GdkWindowCache *cache,
283 gint x, gint y, gint width, gint height,
286 GdkCacheChild *child = g_new (GdkCacheChild, 1);
291 child->width = width;
292 child->height = height;
293 child->mapped = mapped;
295 cache->children = g_list_prepend (cache->children, child);
296 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
300 static GdkFilterReturn
301 gdk_window_cache_filter (GdkXEvent *xev,
305 XEvent *xevent = (XEvent *)xev;
306 GdkWindowCache *cache = data;
308 switch (xevent->type)
310 case CirculateNotify:
312 case ConfigureNotify:
314 XConfigureEvent *xce = &xevent->xconfigure;
317 node = g_hash_table_lookup (cache->child_hash,
318 GUINT_TO_POINTER (xce->window));
321 GdkCacheChild *child = node->data;
324 child->width = xce->width;
325 child->height = xce->height;
326 if (xce->above == None && (node->next))
328 GList *last = g_list_last (cache->children);
329 cache->children = g_list_remove_link (cache->children, node);
336 GList *above_node = g_hash_table_lookup (cache->child_hash,
337 GUINT_TO_POINTER (xce->above));
338 if (above_node && node->prev != above_node)
340 /* Put the window above (before in the list) above_node
342 cache->children = g_list_remove_link (cache->children, node);
343 node->prev = above_node->prev;
345 node->prev->next = node;
347 cache->children = node;
348 node->next = above_node;
349 above_node->prev = node;
357 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
359 if (!g_hash_table_lookup (cache->child_hash,
360 GUINT_TO_POINTER (xcwe->window)))
361 gdk_window_cache_add (cache, xcwe->window,
362 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
368 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
371 node = g_hash_table_lookup (cache->child_hash,
372 GUINT_TO_POINTER (xdwe->window));
375 g_hash_table_remove (cache->child_hash,
376 GUINT_TO_POINTER (xdwe->window));
377 cache->children = g_list_remove_link (cache->children, node);
379 g_list_free_1 (node);
385 XMapEvent *xme = &xevent->xmap;
388 node = g_hash_table_lookup (cache->child_hash,
389 GUINT_TO_POINTER (xme->window));
392 GdkCacheChild *child = node->data;
393 child->mapped = TRUE;
401 XMapEvent *xume = &xevent->xmap;
404 node = g_hash_table_lookup (cache->child_hash,
405 GUINT_TO_POINTER (xume->window));
408 GdkCacheChild *child = node->data;
409 child->mapped = FALSE;
414 return GDK_FILTER_CONTINUE;
416 return GDK_FILTER_REMOVE;
419 static GdkWindowCache *
420 gdk_window_cache_new (GdkScreen *screen)
422 XWindowAttributes xwa;
423 Window root, parent, *children;
424 unsigned int nchildren;
426 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
427 GdkWindow *root_window = gdk_screen_get_root_window (screen);
429 GdkWindowCache *result = g_new (GdkWindowCache, 1);
431 result->children = NULL;
432 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
433 result->screen = screen;
435 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
436 result->old_event_mask = xwa.your_event_mask;
437 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
438 result->old_event_mask | SubstructureNotifyMask);
439 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
441 gdk_error_trap_push ();
443 if (!XQueryTree(xdisplay, GDK_WINDOW_XWINDOW (root_window),
444 &root, &parent, &children, &nchildren))
446 gdk_error_trap_pop ();
450 for (i = 0; i < nchildren ; i++)
452 if (XGetWindowAttributes (xdisplay, children[i], &xwa))
453 gdk_window_cache_add (result, children[i],
454 xwa.x, xwa.y, xwa.width, xwa.height,
455 xwa.map_state != IsUnmapped);
459 gdk_error_trap_pop ();
465 gdk_window_cache_destroy (GdkWindowCache *cache)
467 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
469 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
470 GDK_WINDOW_XWINDOW (root_window),
471 cache->old_event_mask);
472 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
474 g_list_foreach (cache->children, (GFunc)g_free, NULL);
475 g_list_free (cache->children);
476 g_hash_table_destroy (cache->child_hash);
482 get_client_window_at_coords_recurse (GdkDisplay *display,
487 Window root, tmp_parent, *children;
488 unsigned int nchildren;
493 unsigned long nitems, after;
496 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(display), win,
497 gdk_x11_get_xatom_by_name_for_display (display, "WM_STATE"),
498 0, 0, False, AnyPropertyType,
499 &type, &format, &nitems, &after, &data) != Success)
509 /* This is beautiful! Damn Enlightenment and click-to-focus */
510 if (!XTranslateCoordinates (gdk_display, _gdk_root_window, win,
511 x_root, y_root, &dest_x, &dest_y, &child))
515 if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), win,
516 &root, &tmp_parent, &children, &nchildren))
519 for (i = nchildren - 1; (i >= 0) && (child == None); i--)
521 XWindowAttributes xwa;
523 if (XGetWindowAttributes (GDK_DISPLAY_XDISPLAY (display),
526 if ((xwa.map_state == IsViewable) && (xwa.class == InputOutput) &&
527 (x >= xwa.x) && (x < xwa.x + (gint)xwa.width) &&
528 (y >= xwa.y) && (y < xwa.y + (gint)xwa.height))
541 return get_client_window_at_coords_recurse (display, child, x, y);
547 get_client_window_at_coords (GdkWindowCache *cache,
553 Window retval = None;
555 gdk_error_trap_push ();
557 tmp_list = cache->children;
559 while (tmp_list && !retval)
561 GdkCacheChild *child = tmp_list->data;
563 if ((child->xid != ignore) && (child->mapped))
565 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
566 (y_root >= child->y) && (y_root < child->y + child->height))
568 retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
577 tmp_list = tmp_list->next;
580 gdk_error_trap_pop ();
585 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
588 /*************************************************************
589 ***************************** MOTIF *************************
590 *************************************************************/
592 /* values used in the message type for Motif DND */
605 /* Values used to specify type of protocol to use */
609 XmDRAG_PREFER_PREREGISTER,
611 XmDRAG_PREFER_DYNAMIC,
613 XmDRAG_PREFER_RECEIVER
616 /* Operation codes */
624 /* Drop site status */
626 XmNO_DROP_SITE = 0x01,
627 XmDROP_SITE_INVALID = 0x02,
628 XmDROP_SITE_VALID = 0x03
631 /* completion status */
639 /* Byte swapping routines. The motif specification leaves it
640 * up to us to save a few bytes in the client messages
642 static gchar local_byte_order = '\0';
644 #ifdef G_ENABLE_DEBUG
646 print_target_list (GList *targets)
650 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
651 g_message ("\t%s", name);
653 targets = targets->next;
656 #endif /* G_ENABLE_DEBUG */
659 init_byte_order (void)
661 guint32 myint = 0x01020304;
662 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
666 card16_to_host (guint16 x, gchar byte_order) {
667 if (byte_order == local_byte_order)
670 return (x << 8) | (x >> 8);
674 card32_to_host (guint32 x, gchar byte_order) {
675 if (byte_order == local_byte_order)
678 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
681 /* Motif packs together fields of varying length into the
682 * client message. We can't rely on accessing these
683 * through data.s[], data.l[], etc, because on some architectures
684 * (i.e., Alpha) these won't be valid for format == 8.
687 #define MOTIF_XCLIENT_BYTE(xevent,i) \
688 (xevent)->xclient.data.b[i]
689 #define MOTIF_XCLIENT_SHORT(xevent,i) \
690 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
691 #define MOTIF_XCLIENT_LONG(xevent,i) \
692 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
694 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
695 #define MOTIF_UNPACK_SHORT(xevent,i) \
696 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
697 #define MOTIF_UNPACK_LONG(xevent,i) \
698 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
700 /***** Dest side ***********/
702 /* Property placed on source windows */
703 typedef struct _MotifDragInitiatorInfo {
705 guint8 protocol_version;
706 guint16 targets_index;
707 guint32 selection_atom;
708 } MotifDragInitiatorInfo;
710 /* Header for target table on the drag window */
711 typedef struct _MotifTargetTableHeader {
713 guchar protocol_version;
716 } MotifTargetTableHeader;
718 /* Property placed on target windows */
719 typedef struct _MotifDragReceiverInfo {
721 guint8 protocol_version;
722 guint8 protocol_style;
724 guint32 proxy_window;
725 guint16 num_drop_sites;
728 } MotifDragReceiverInfo;
730 /* Target table handling */
732 static GdkFilterReturn
733 motif_drag_window_filter (GdkXEvent *xevent,
737 XEvent *xev = (XEvent *)xevent;
738 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
739 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
741 switch (xev->xany.type)
744 display_x11->motif_drag_window = None;
745 display_x11->motif_drag_gdk_window = NULL;
748 if (display_x11->motif_target_lists &&
749 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
750 motif_read_target_table (display);
753 return GDK_FILTER_REMOVE;
757 motif_lookup_drag_window (GdkDisplay *display,
758 Display *lookup_xdisplay)
760 Window retval = None;
761 gulong bytes_after, nitems;
766 XGetWindowProperty (lookup_xdisplay, DefaultRootWindow (lookup_xdisplay),
767 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
769 XA_WINDOW, &type, &format, &nitems, &bytes_after,
772 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
774 retval = *(Window *)data;
776 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
785 /* Finds the window where global Motif drag information is stored.
786 * If it doesn't exist and 'create' is TRUE, create one.
789 motif_find_drag_window (GdkDisplay *display,
792 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
794 if (!display_x11->motif_drag_window)
796 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
797 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
799 if (!display_x11->motif_drag_window && create)
801 /* Create a persistant window. (Copied from LessTif) */
803 Display *persistant_xdisplay;
804 XSetWindowAttributes attr;
805 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
806 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
808 XGrabServer (persistant_xdisplay);
810 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
812 if (!display_x11->motif_drag_window)
814 attr.override_redirect = True;
815 attr.event_mask = PropertyChangeMask;
817 display_x11->motif_drag_window =
818 XCreateWindow (persistant_xdisplay,
819 DefaultRootWindow (persistant_xdisplay),
820 -100, -100, 10, 10, 0, 0,
821 InputOnly, CopyFromParent,
822 (CWOverrideRedirect | CWEventMask), &attr);
825 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
827 XChangeProperty (persistant_xdisplay,
828 DefaultRootWindow (persistant_xdisplay),
829 motif_drag_window_atom, XA_WINDOW,
831 (guchar *)&motif_drag_window_atom, 1);
834 XUngrabServer (persistant_xdisplay);
835 XCloseDisplay (persistant_xdisplay);
838 /* There is a miniscule race condition here if the drag window
839 * gets destroyed exactly now.
841 if (display_x11->motif_drag_window)
843 display_x11->motif_drag_gdk_window =
844 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
845 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
846 motif_drag_window_filter,
851 return display_x11->motif_drag_window;
855 motif_read_target_table (GdkDisplay *display)
857 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
858 gulong bytes_after, nitems;
863 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
865 if (display_x11->motif_target_lists)
867 for (i=0; i<display_x11->motif_n_target_lists; i++)
868 g_list_free (display_x11->motif_target_lists[i]);
870 g_free (display_x11->motif_target_lists);
871 display_x11->motif_target_lists = NULL;
872 display_x11->motif_n_target_lists = 0;
875 if (motif_find_drag_window (display, FALSE))
877 MotifTargetTableHeader *header = NULL;
878 guchar *target_bytes = NULL;
880 gboolean success = FALSE;
882 gdk_error_trap_push ();
883 XGetWindowProperty (display_x11->xdisplay,
884 display_x11->motif_drag_window,
885 motif_drag_targets_atom,
886 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
887 motif_drag_targets_atom,
888 &type, &format, &nitems, &bytes_after,
891 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
894 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
895 header->total_size = card32_to_host (header->total_size, header->byte_order);
897 gdk_error_trap_push ();
898 XGetWindowProperty (display_x11->xdisplay,
899 display_x11->motif_drag_window,
900 motif_drag_targets_atom,
901 (sizeof(MotifTargetTableHeader)+3)/4,
902 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
904 motif_drag_targets_atom, &type, &format, &nitems,
905 &bytes_after, &target_bytes);
907 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
908 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
911 display_x11->motif_n_target_lists = header->n_lists;
912 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
915 for (i=0; i<header->n_lists; i++)
920 if (p + sizeof(guint16) - target_bytes > nitems)
923 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
925 /* We need to make a copy of the targets, since it may
928 targets = g_new (guint32, n_targets);
929 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
931 p += sizeof(guint16) + n_targets * sizeof(guint32);
932 if (p - target_bytes > nitems)
935 for (j=0; j<n_targets; j++)
936 display_x11->motif_target_lists[i] =
937 g_list_prepend (display_x11->motif_target_lists[i],
938 GUINT_TO_POINTER (card32_to_host (targets[j],
939 header->byte_order)));
941 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
951 XFree (target_bytes);
955 if (display_x11->motif_target_lists)
957 g_free (display_x11->motif_target_lists);
958 display_x11->motif_target_lists = NULL;
959 display_x11->motif_n_target_lists = 0;
961 g_warning ("Error reading Motif target table\n");
967 targets_sort_func (gconstpointer a, gconstpointer b)
969 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
970 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
973 /* Check if given (sorted) list is in the targets table */
975 motif_target_table_check (GdkDisplay *display,
978 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
979 GList *tmp_list1, *tmp_list2;
982 for (i=0; i<display_x11->motif_n_target_lists; i++)
984 tmp_list1 = display_x11->motif_target_lists[i];
987 while (tmp_list1 && tmp_list2)
989 if (tmp_list1->data != tmp_list2->data)
992 tmp_list1 = tmp_list1->next;
993 tmp_list2 = tmp_list2->next;
995 if (!tmp_list1 && !tmp_list2) /* Found it */
1003 motif_add_to_target_table (GdkDisplay *display,
1004 GList *targets) /* targets is list of GdkAtom */
1006 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1007 GList *sorted = NULL;
1012 /* make a sorted copy of the list */
1016 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1017 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1018 targets = targets->next;
1021 /* First check if it is their already */
1023 if (display_x11->motif_target_lists)
1024 index = motif_target_table_check (display, sorted);
1026 /* We need to grab the server while doing this, to ensure
1032 /* We need to make sure that it exists _before_ we grab the
1033 * server, since we can't open a new connection after we
1036 motif_find_drag_window (display, TRUE);
1038 gdk_x11_display_grab (display);
1039 motif_read_target_table (display);
1041 /* Check again, in case it was added in the meantime */
1043 if (display_x11->motif_target_lists)
1044 index = motif_target_table_check (display, sorted);
1048 guint32 total_size = 0;
1052 MotifTargetTableHeader *header;
1054 if (!display_x11->motif_target_lists)
1056 display_x11->motif_target_lists = g_new (GList *, 1);
1057 display_x11->motif_n_target_lists = 1;
1061 display_x11->motif_n_target_lists++;
1062 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1063 sizeof(GList *) * display_x11->motif_n_target_lists);
1065 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1067 index = display_x11->motif_n_target_lists - 1;
1069 total_size = sizeof (MotifTargetTableHeader);
1070 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1071 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1073 data = g_malloc (total_size);
1075 header = (MotifTargetTableHeader *)data;
1076 p = data + sizeof(MotifTargetTableHeader);
1078 header->byte_order = local_byte_order;
1079 header->protocol_version = 0;
1080 header->n_lists = display_x11->motif_n_target_lists;
1081 header->total_size = total_size;
1083 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1085 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1086 guint32 *targets = g_new (guint32, n_targets);
1087 guint32 *p32 = targets;
1089 tmp_list = display_x11->motif_target_lists[i];
1092 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1094 tmp_list = tmp_list->next;
1099 p += sizeof(guint16);
1101 memcpy (p, targets, n_targets * sizeof(guint32));
1104 p += sizeof(guint32) * n_targets;
1108 XChangeProperty (display_x11->xdisplay,
1109 display_x11->motif_drag_window,
1110 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1111 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1115 gdk_x11_display_ungrab (display);
1118 g_list_free (sorted);
1122 /* Translate flags */
1125 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1127 guint recommended_op = flags & 0x000f;
1128 guint possible_ops = (flags & 0x0f0) >> 4;
1130 switch (recommended_op)
1133 context->suggested_action = GDK_ACTION_MOVE;
1136 context->suggested_action = GDK_ACTION_COPY;
1139 context->suggested_action = GDK_ACTION_LINK;
1142 context->suggested_action = GDK_ACTION_COPY;
1146 context->actions = 0;
1147 if (possible_ops & XmDROP_MOVE)
1148 context->actions |= GDK_ACTION_MOVE;
1149 if (possible_ops & XmDROP_COPY)
1150 context->actions |= GDK_ACTION_COPY;
1151 if (possible_ops & XmDROP_LINK)
1152 context->actions |= GDK_ACTION_LINK;
1156 motif_dnd_get_flags (GdkDragContext *context)
1160 switch (context->suggested_action)
1162 case GDK_ACTION_MOVE:
1163 flags = XmDROP_MOVE;
1165 case GDK_ACTION_COPY:
1166 flags = XmDROP_COPY;
1168 case GDK_ACTION_LINK:
1169 flags = XmDROP_LINK;
1172 flags = XmDROP_NOOP;
1176 if (context->actions & GDK_ACTION_MOVE)
1177 flags |= XmDROP_MOVE << 8;
1178 if (context->actions & GDK_ACTION_COPY)
1179 flags |= XmDROP_COPY << 8;
1180 if (context->actions & GDK_ACTION_LINK)
1181 flags |= XmDROP_LINK << 8;
1189 motif_set_targets (GdkDragContext *context)
1191 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1192 MotifDragInitiatorInfo info;
1194 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1196 info.byte_order = local_byte_order;
1197 info.protocol_version = 0;
1199 info.targets_index = motif_add_to_target_table (display, context->targets);
1204 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1206 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1207 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1211 info.selection_atom = private->motif_selection;
1213 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1214 GDK_DRAWABLE_XID (context->source_window),
1215 private->motif_selection,
1216 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1218 (guchar *)&info, sizeof (info));
1220 private->motif_targets_set = 1;
1224 motif_check_dest (GdkDisplay *display,
1227 gboolean retval = FALSE;
1228 MotifDragReceiverInfo *info;
1231 unsigned long nitems, after;
1232 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1234 gdk_error_trap_push ();
1235 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1236 motif_drag_receiver_info_atom,
1237 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1238 &type, &format, &nitems, &after,
1241 if (gdk_error_trap_pop() == 0)
1245 if ((format == 8) && (nitems == sizeof(*info)))
1247 if ((info->protocol_version == 0) &&
1248 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1249 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1250 (info->protocol_style == XmDRAG_DYNAMIC)))
1256 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1263 return retval ? win : None;
1267 motif_send_enter (GdkDragContext *context,
1270 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1271 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1274 xev.xclient.type = ClientMessage;
1275 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1276 xev.xclient.format = 8;
1277 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1279 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1280 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1281 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1282 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1283 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1285 if (!private->motif_targets_set)
1286 motif_set_targets (context);
1288 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1290 if (!_gdk_send_xevent (display,
1291 GDK_DRAWABLE_XID (context->dest_window),
1294 g_message ("Send event to %lx failed",
1295 GDK_DRAWABLE_XID (context->dest_window)));
1299 motif_send_leave (GdkDragContext *context,
1302 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1305 xev.xclient.type = ClientMessage;
1306 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1307 xev.xclient.format = 8;
1308 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1310 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1311 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1312 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1313 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1314 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1315 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1317 if (!_gdk_send_xevent (display,
1318 GDK_DRAWABLE_XID (context->dest_window),
1321 g_message ("Send event to %lx failed",
1322 GDK_DRAWABLE_XID (context->dest_window)));
1326 motif_send_motion (GdkDragContext *context,
1329 GdkDragAction action,
1332 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1333 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1337 xev.xclient.type = ClientMessage;
1338 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1339 xev.xclient.format = 8;
1340 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1342 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1343 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1344 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1346 if ((context->suggested_action != private->old_action) ||
1347 (context->actions != private->old_actions))
1349 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1351 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1356 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1358 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1359 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1361 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1365 if (!_gdk_send_xevent (display,
1366 GDK_DRAWABLE_XID (context->dest_window),
1369 g_message ("Send event to %lx failed",
1370 GDK_DRAWABLE_XID (context->dest_window)));
1376 motif_send_drop (GdkDragContext *context, guint32 time)
1378 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1379 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1382 xev.xclient.type = ClientMessage;
1383 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1384 xev.xclient.format = 8;
1385 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1387 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1388 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1389 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1390 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1392 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1393 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1395 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1396 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1398 if (!_gdk_send_xevent (display,
1399 GDK_DRAWABLE_XID (context->dest_window),
1402 g_message ("Send event to %lx failed",
1403 GDK_DRAWABLE_XID (context->dest_window)));
1409 motif_read_initiator_info (GdkDisplay *display,
1410 Window source_window,
1420 MotifDragInitiatorInfo *initiator_info;
1422 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1424 gdk_error_trap_push ();
1425 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1426 0, sizeof(*initiator_info), FALSE,
1427 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1428 &type, &format, &nitems, &bytes_after,
1429 (guchar **)&initiator_info);
1431 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1433 g_warning ("Error reading initiator info\n");
1437 motif_read_target_table (display);
1439 initiator_info->targets_index =
1440 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1441 initiator_info->selection_atom =
1442 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1444 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1446 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1447 XFree (initiator_info);
1448 return GDK_FILTER_REMOVE;
1451 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1456 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1457 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1458 tmp_list = tmp_list->prev;
1461 #ifdef G_ENABLE_DEBUG
1462 if (_gdk_debug_flags & GDK_DEBUG_DND)
1463 print_target_list (*targets);
1464 #endif /* G_ENABLE_DEBUG */
1466 *selection = initiator_info->selection_atom;
1468 XFree (initiator_info);
1473 static GdkDragContext *
1474 motif_drag_context_new (GdkWindow *dest_window,
1476 guint32 source_window,
1479 GdkDragContext *new_context;
1480 GdkDragContextPrivateX11 *private;
1481 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1482 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1484 /* FIXME, current_dest_drag really shouldn't be NULL'd
1485 * if we error below.
1487 if (display_x11->current_dest_drag != NULL)
1489 if (timestamp >= display_x11->current_dest_drag->start_time)
1491 gdk_drag_context_unref (display_x11->current_dest_drag);
1492 display_x11->current_dest_drag = NULL;
1498 new_context = gdk_drag_context_new ();
1499 private = PRIVATE_DATA (new_context);
1501 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1502 new_context->is_source = FALSE;
1504 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1505 if (new_context->source_window)
1506 gdk_window_ref (new_context->source_window);
1509 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1510 if (!new_context->source_window)
1512 gdk_drag_context_unref (new_context);
1517 new_context->dest_window = dest_window;
1518 gdk_window_ref (dest_window);
1519 new_context->start_time = timestamp;
1521 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1524 &new_context->targets,
1525 &private->motif_selection))
1527 gdk_drag_context_unref (new_context);
1535 * The MOTIF drag protocol has no real provisions for distinguishing
1536 * multiple simultaneous drops. If the sources grab the pointer
1537 * when doing drags, that shouldn't happen, in any case. If it
1538 * does, we can't do much except hope for the best.
1541 static GdkFilterReturn
1542 motif_top_level_enter (GdkEvent *event,
1545 guint32 source_window,
1548 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1549 GdkDragContext *new_context;
1551 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1552 flags, timestamp, source_window, atom));
1554 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1556 return GDK_FILTER_REMOVE;
1558 event->dnd.type = GDK_DRAG_ENTER;
1559 event->dnd.context = new_context;
1560 gdk_drag_context_ref (new_context);
1562 display_x11->current_dest_drag = new_context;
1564 return GDK_FILTER_TRANSLATE;
1567 static GdkFilterReturn
1568 motif_top_level_leave (GdkEvent *event,
1572 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1574 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1577 if ((display_x11->current_dest_drag != NULL) &&
1578 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1579 (timestamp >= display_x11->current_dest_drag->start_time))
1581 event->dnd.type = GDK_DRAG_LEAVE;
1582 /* Pass ownership of context to the event */
1583 event->dnd.context = display_x11->current_dest_drag;
1585 display_x11->current_dest_drag = NULL;
1587 return GDK_FILTER_TRANSLATE;
1590 return GDK_FILTER_REMOVE;
1593 static GdkFilterReturn
1594 motif_motion (GdkEvent *event,
1600 GdkDragContextPrivateX11 *private;
1601 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1603 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1604 flags, timestamp, x_root, y_root));
1606 if ((display_x11->current_dest_drag != NULL) &&
1607 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1608 (timestamp >= display_x11->current_dest_drag->start_time))
1610 private = PRIVATE_DATA (display_x11->current_dest_drag);
1612 event->dnd.type = GDK_DRAG_MOTION;
1613 event->dnd.context = display_x11->current_dest_drag;
1614 gdk_drag_context_ref (display_x11->current_dest_drag);
1616 event->dnd.time = timestamp;
1618 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1620 event->dnd.x_root = x_root;
1621 event->dnd.y_root = y_root;
1623 private->last_x = x_root;
1624 private->last_y = y_root;
1626 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1628 return GDK_FILTER_TRANSLATE;
1631 return GDK_FILTER_REMOVE;
1634 static GdkFilterReturn
1635 motif_operation_changed (GdkEvent *event,
1639 GdkDragContextPrivateX11 *private;
1640 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1641 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1644 if ((display_x11->current_dest_drag != NULL) &&
1645 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1646 (timestamp >= display_x11->current_dest_drag->start_time))
1648 event->dnd.type = GDK_DRAG_MOTION;
1649 event->dnd.send_event = FALSE;
1650 event->dnd.context = display_x11->current_dest_drag;
1651 gdk_drag_context_ref (display_x11->current_dest_drag);
1653 event->dnd.time = timestamp;
1654 private = PRIVATE_DATA (display_x11->current_dest_drag);
1656 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1658 event->dnd.x_root = private->last_x;
1659 event->dnd.y_root = private->last_y;
1661 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1663 return GDK_FILTER_TRANSLATE;
1666 return GDK_FILTER_REMOVE;
1669 static GdkFilterReturn
1670 motif_drop_start (GdkEvent *event,
1673 guint32 source_window,
1678 GdkDragContext *new_context;
1679 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1681 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1682 flags, timestamp, x_root, y_root, source_window, atom));
1684 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1686 return GDK_FILTER_REMOVE;
1688 motif_dnd_translate_flags (new_context, flags);
1690 event->dnd.type = GDK_DROP_START;
1691 event->dnd.context = new_context;
1692 event->dnd.time = timestamp;
1693 event->dnd.x_root = x_root;
1694 event->dnd.y_root = y_root;
1696 gdk_drag_context_ref (new_context);
1697 display_x11->current_dest_drag = new_context;
1699 return GDK_FILTER_TRANSLATE;
1702 static GdkFilterReturn
1703 motif_drag_status (GdkEvent *event,
1707 GdkDragContext *context;
1708 GdkDisplay *display;
1711 g_message ("Motif status message: flags %x", flags));
1713 display = gdk_drawable_get_display (event->any.window);
1715 return GDK_FILTER_REMOVE;
1717 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1721 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1722 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1723 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1724 private->drag_status = GDK_DRAG_STATUS_DRAG;
1726 event->dnd.type = GDK_DRAG_STATUS;
1727 event->dnd.send_event = FALSE;
1728 event->dnd.context = context;
1729 gdk_drag_context_ref (context);
1731 event->dnd.time = timestamp;
1733 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1735 switch (flags & 0x000f)
1738 context->action = 0;
1741 context->action = GDK_ACTION_MOVE;
1744 context->action = GDK_ACTION_COPY;
1747 context->action = GDK_ACTION_LINK;
1752 context->action = 0;
1754 return GDK_FILTER_TRANSLATE;
1756 return GDK_FILTER_REMOVE;
1759 static GdkFilterReturn
1760 motif_dnd_filter (GdkXEvent *xev,
1764 XEvent *xevent = (XEvent *)xev;
1769 guint32 source_window;
1771 gint16 x_root, y_root;
1774 if (!event->any.window ||
1775 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1776 return GDK_FILTER_CONTINUE; /* Not for us */
1778 /* First read some fields common to all Motif DND messages */
1780 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1781 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1782 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1784 is_reply = ((reason & 0x80) != 0);
1786 switch (reason & 0x7f)
1788 case XmTOP_LEVEL_ENTER:
1789 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1790 atom = MOTIF_UNPACK_LONG (xevent, 3);
1791 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1792 case XmTOP_LEVEL_LEAVE:
1793 return motif_top_level_leave (event, flags, timestamp);
1796 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1797 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1800 return motif_motion (event, flags, timestamp, x_root, y_root);
1802 return motif_drag_status (event, flags, timestamp);
1804 case XmDROP_SITE_ENTER:
1805 return motif_drag_status (event, flags, timestamp);
1807 case XmDROP_SITE_LEAVE:
1808 return motif_drag_status (event,
1809 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1812 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1813 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1814 atom = MOTIF_UNPACK_LONG (xevent, 3);
1815 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1818 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1821 case XmOPERATION_CHANGED:
1823 return motif_operation_changed (event, flags, timestamp);
1825 return motif_drag_status (event, flags, timestamp);
1828 /* To the best of my knowledge, these next two messages are
1829 * not part of the protocol, though they are defined in
1833 case XmDRAG_DROP_FINISH:
1837 return GDK_FILTER_REMOVE;
1840 /*************************************************************
1841 ***************************** XDND **************************
1842 *************************************************************/
1844 /* Utility functions */
1849 GdkDragAction action;
1850 } xdnd_actions_table[] = {
1851 { "XdndActionCopy", None, GDK_ACTION_COPY },
1852 { "XdndActionMove", None, GDK_ACTION_MOVE },
1853 { "XdndActionLink", None, GDK_ACTION_LINK },
1854 { "XdndActionAsk", None, GDK_ACTION_ASK },
1855 { "XdndActionPrivate", None, GDK_ACTION_COPY },
1858 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1859 static gboolean xdnd_actions_initialized = FALSE;
1862 xdnd_initialize_actions (void)
1866 xdnd_actions_initialized = TRUE;
1867 for (i=0; i < xdnd_n_actions; i++)
1868 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1871 static GdkDragAction
1872 xdnd_action_from_atom (GdkDisplay *display,
1875 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
1878 if (!xdnd_actions_initialized)
1879 xdnd_initialize_actions();
1881 for (i=0; i<xdnd_n_actions; i++)
1882 if (atom == xdnd_actions_table[i].atom)
1883 return xdnd_actions_table[i].action;
1889 xdnd_action_to_atom (GdkDisplay *display,
1890 GdkDragAction action)
1894 if (!xdnd_actions_initialized)
1895 xdnd_initialize_actions();
1897 for (i=0; i<xdnd_n_actions; i++)
1898 if (action == xdnd_actions_table[i].action)
1899 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
1906 static GdkFilterReturn
1907 xdnd_status_filter (GdkXEvent *xev,
1911 GdkDisplay *display;
1912 XEvent *xevent = (XEvent *)xev;
1913 guint32 dest_window = xevent->xclient.data.l[0];
1914 guint32 flags = xevent->xclient.data.l[1];
1915 Atom action = xevent->xclient.data.l[4];
1916 GdkDragContext *context;
1918 if (!event->any.window ||
1919 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1920 return GDK_FILTER_CONTINUE; /* Not for us */
1923 g_message ("XdndStatus: dest_window: %#x action: %ld",
1924 dest_window, action));
1926 display = gdk_drawable_get_display (event->any.window);
1927 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1931 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1932 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1933 private->drag_status = GDK_DRAG_STATUS_DRAG;
1935 event->dnd.send_event = FALSE;
1936 event->dnd.type = GDK_DRAG_STATUS;
1937 event->dnd.context = context;
1938 gdk_drag_context_ref (context);
1940 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1941 if (!(action != 0) != !(flags & 1))
1944 g_warning ("Received status event with flags not corresponding to action!\n"));
1948 context->action = xdnd_action_from_atom (display, action);
1950 return GDK_FILTER_TRANSLATE;
1953 return GDK_FILTER_REMOVE;
1956 static GdkFilterReturn
1957 xdnd_finished_filter (GdkXEvent *xev,
1961 GdkDisplay *display;
1962 XEvent *xevent = (XEvent *)xev;
1963 guint32 dest_window = xevent->xclient.data.l[0];
1964 GdkDragContext *context;
1966 if (!event->any.window ||
1967 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1968 return GDK_FILTER_CONTINUE; /* Not for us */
1971 g_message ("XdndFinished: dest_window: %#x", dest_window));
1973 display = gdk_drawable_get_display (event->any.window);
1974 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1978 event->dnd.type = GDK_DROP_FINISHED;
1979 event->dnd.context = context;
1980 gdk_drag_context_ref (context);
1982 return GDK_FILTER_TRANSLATE;
1985 return GDK_FILTER_REMOVE;
1989 xdnd_set_targets (GdkDragContext *context)
1991 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1993 GList *tmp_list = context->targets;
1995 gint n_atoms = g_list_length (context->targets);
1996 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1998 atomlist = g_new (Atom, n_atoms);
2002 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2003 tmp_list = tmp_list->next;
2007 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2008 GDK_DRAWABLE_XID (context->source_window),
2009 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2010 XA_ATOM, 32, PropModeReplace,
2011 (guchar *)atomlist, n_atoms);
2015 private->xdnd_targets_set = 1;
2019 xdnd_set_actions (GdkDragContext *context)
2021 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2026 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2028 if (!xdnd_actions_initialized)
2029 xdnd_initialize_actions();
2031 actions = context->actions;
2033 for (i=0; i<xdnd_n_actions; i++)
2035 if (actions & xdnd_actions_table[i].action)
2037 actions &= ~xdnd_actions_table[i].action;
2042 atomlist = g_new (Atom, n_atoms);
2044 actions = context->actions;
2046 for (i=0; i<xdnd_n_actions; i++)
2048 if (actions & xdnd_actions_table[i].action)
2050 actions &= ~xdnd_actions_table[i].action;
2051 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2056 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2057 GDK_DRAWABLE_XID (context->source_window),
2058 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2059 XA_ATOM, 32, PropModeReplace,
2060 (guchar *)atomlist, n_atoms);
2064 private->xdnd_actions_set = 1;
2065 private->xdnd_actions = context->actions;
2068 /*************************************************************
2070 * Like gdk_send_event, but if the target is the root
2071 * window, sets an event mask of ButtonPressMask, otherwise
2072 * an event mask of 0.
2076 *************************************************************/
2079 xdnd_send_xevent (GdkDisplay *display,
2084 if (_gdk_x11_display_is_root_window (display, window))
2085 return _gdk_send_xevent (display, window, propagate, ButtonPressMask, event_send);
2087 return _gdk_send_xevent (display, window, propagate, 0, event_send);
2091 xdnd_send_enter (GdkDragContext *context)
2094 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2095 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2097 xev.xclient.type = ClientMessage;
2098 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2099 xev.xclient.format = 32;
2100 xev.xclient.window = private->drop_xid ?
2102 GDK_DRAWABLE_XID (context->dest_window);
2103 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2104 xev.xclient.data.l[1] = (3 << 24); /* version */
2105 xev.xclient.data.l[2] = 0;
2106 xev.xclient.data.l[3] = 0;
2107 xev.xclient.data.l[4] = 0;
2109 if (!private->xdnd_selection)
2110 private->xdnd_selection = gdk_x11_get_xatom_by_name_for_display (display, "XdndSelection");
2112 if (g_list_length (context->targets) > 3)
2114 if (!private->xdnd_targets_set)
2115 xdnd_set_targets (context);
2116 xev.xclient.data.l[1] |= 1;
2120 GList *tmp_list = context->targets;
2125 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2126 GDK_POINTER_TO_ATOM (tmp_list->data));
2127 tmp_list = tmp_list->next;
2132 if (!xdnd_send_xevent (display,
2133 GDK_DRAWABLE_XID (context->dest_window),
2137 g_message ("Send event to %lx failed",
2138 GDK_DRAWABLE_XID (context->dest_window)));
2139 gdk_window_unref (context->dest_window);
2140 context->dest_window = NULL;
2145 xdnd_send_leave (GdkDragContext *context)
2147 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2150 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2152 xev.xclient.type = ClientMessage;
2153 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2154 xev.xclient.format = 32;
2155 xev.xclient.window = private->drop_xid ?
2157 GDK_DRAWABLE_XID (context->dest_window);
2158 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2159 xev.xclient.data.l[1] = 0;
2160 xev.xclient.data.l[2] = 0;
2161 xev.xclient.data.l[3] = 0;
2162 xev.xclient.data.l[4] = 0;
2164 if (!xdnd_send_xevent (display,
2165 GDK_DRAWABLE_XID (context->dest_window),
2169 g_message ("Send event to %lx failed",
2170 GDK_DRAWABLE_XID (context->dest_window)));
2171 gdk_window_unref (context->dest_window);
2172 context->dest_window = NULL;
2177 xdnd_send_drop (GdkDragContext *context, guint32 time)
2179 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2180 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2183 xev.xclient.type = ClientMessage;
2184 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2185 xev.xclient.format = 32;
2186 xev.xclient.window = private->drop_xid ?
2188 GDK_DRAWABLE_XID (context->dest_window);
2189 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2190 xev.xclient.data.l[1] = 0;
2191 xev.xclient.data.l[2] = time;
2192 xev.xclient.data.l[3] = 0;
2193 xev.xclient.data.l[4] = 0;
2195 if (!xdnd_send_xevent (display,
2196 GDK_DRAWABLE_XID (context->dest_window),
2200 g_message ("Send event to %lx failed",
2201 GDK_DRAWABLE_XID (context->dest_window)));
2202 gdk_window_unref (context->dest_window);
2203 context->dest_window = NULL;
2208 xdnd_send_motion (GdkDragContext *context,
2211 GdkDragAction action,
2214 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2215 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2218 xev.xclient.type = ClientMessage;
2219 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2220 xev.xclient.format = 32;
2221 xev.xclient.window = private->drop_xid ?
2223 GDK_DRAWABLE_XID (context->dest_window);
2224 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2225 xev.xclient.data.l[1] = 0;
2226 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2227 xev.xclient.data.l[3] = time;
2228 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2230 if (!xdnd_send_xevent (display,
2231 GDK_DRAWABLE_XID (context->dest_window),
2235 g_message ("Send event to %lx failed",
2236 GDK_DRAWABLE_XID (context->dest_window)));
2237 gdk_window_unref (context->dest_window);
2238 context->dest_window = NULL;
2240 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2244 xdnd_check_dest (GdkDisplay *display,
2247 gboolean retval = FALSE;
2250 unsigned long nitems, after;
2254 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2255 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2259 gdk_error_trap_push ();
2261 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2263 1, False, AnyPropertyType,
2264 &type, &format, &nitems, &after,
2265 (guchar **)&proxy_data) == Success)
2269 if ((format == 32) && (nitems == 1))
2271 proxy = *proxy_data;
2275 g_warning ("Invalid XdndOwner "
2276 "property on window %ld\n", win));
2281 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2283 1, False, AnyPropertyType,
2284 &type, &format, &nitems, &after,
2285 (guchar **)&version) == Success) &&
2288 if ((format == 32) && (nitems == 1))
2295 g_warning ("Invalid XdndAware property on window %ld\n", win));
2301 gdk_error_trap_pop ();
2303 return retval ? (proxy ? proxy : win) : None;
2309 xdnd_read_actions (GdkDragContext *context)
2311 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2314 gulong nitems, after;
2319 /* Get the XdndActionList, if set */
2321 gdk_error_trap_push ();
2323 if (XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2324 GDK_DRAWABLE_XID (context->source_window),
2325 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2327 False, XA_ATOM, &type, &format, &nitems,
2328 &after, (guchar **)&data) == Success &&
2331 context->actions = 0;
2333 for (i=0; i<nitems; i++)
2334 context->actions |= xdnd_action_from_atom (display, data[i]);
2336 (PRIVATE_DATA (context))->xdnd_have_actions = TRUE;
2338 #ifdef G_ENABLE_DEBUG
2339 if (_gdk_debug_flags & GDK_DEBUG_DND)
2341 GString *action_str = g_string_new (NULL);
2342 if (context->actions & GDK_ACTION_MOVE)
2343 g_string_append(action_str, "MOVE ");
2344 if (context->actions & GDK_ACTION_COPY)
2345 g_string_append(action_str, "COPY ");
2346 if (context->actions & GDK_ACTION_LINK)
2347 g_string_append(action_str, "LINK ");
2348 if (context->actions & GDK_ACTION_ASK)
2349 g_string_append(action_str, "ASK ");
2351 g_message("Xdnd actions = %s", action_str->str);
2352 g_string_free (action_str, TRUE);
2354 #endif /* G_ENABLE_DEBUG */
2359 gdk_error_trap_pop ();
2362 /* We have to make sure that the XdndActionList we keep internally
2363 * is up to date with the XdndActionList on the source window
2364 * because we get no notification, because Xdnd wasn't meant
2365 * to continually send actions. So we select on PropertyChangeMask
2366 * and add this filter.
2368 static GdkFilterReturn
2369 xdnd_source_window_filter (GdkXEvent *xev,
2373 XEvent *xevent = (XEvent *)xev;
2374 GdkDragContext *context = cb_data;
2375 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2377 if ((xevent->xany.type == PropertyNotify) &&
2378 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2380 xdnd_read_actions (context);
2382 return GDK_FILTER_REMOVE;
2385 return GDK_FILTER_CONTINUE;
2389 xdnd_manage_source_filter (GdkDragContext *context,
2391 gboolean add_filter)
2393 gdk_error_trap_push ();
2395 if (!GDK_WINDOW_DESTROYED (window))
2399 gdk_window_set_events (window,
2400 gdk_window_get_events (window) |
2401 GDK_PROPERTY_CHANGE_MASK);
2402 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2407 gdk_window_remove_filter (window,
2408 xdnd_source_window_filter,
2410 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2411 * but we might want it for other reasons. (Like
2412 * INCR selection transactions).
2417 gdk_display_sync (gdk_drawable_get_display (window));
2418 gdk_error_trap_pop ();
2421 static GdkFilterReturn
2422 xdnd_enter_filter (GdkXEvent *xev,
2426 GdkDisplay *display;
2427 GdkDisplayX11 *display_x11;
2428 XEvent *xevent = (XEvent *)xev;
2429 GdkDragContext *new_context;
2434 gulong nitems, after;
2437 guint32 source_window;
2441 if (!event->any.window ||
2442 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2443 return GDK_FILTER_CONTINUE; /* Not for us */
2445 source_window = xevent->xclient.data.l[0];
2446 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2447 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2449 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2450 display_x11 = GDK_DISPLAY_X11 (display);
2453 g_message ("XdndEnter: source_window: %#x, version: %#x",
2454 source_window, version));
2458 /* Old source ignore */
2459 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2460 return GDK_FILTER_REMOVE;
2463 if (display_x11->current_dest_drag != NULL)
2465 gdk_drag_context_unref (display_x11->current_dest_drag);
2466 display_x11->current_dest_drag = NULL;
2469 new_context = gdk_drag_context_new ();
2470 new_context->protocol = GDK_DRAG_PROTO_XDND;
2471 new_context->is_source = FALSE;
2473 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2474 if (new_context->source_window)
2475 gdk_window_ref (new_context->source_window);
2478 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2479 if (!new_context->source_window)
2481 gdk_drag_context_unref (new_context);
2482 return GDK_FILTER_REMOVE;
2485 new_context->dest_window = event->any.window;
2486 gdk_window_ref (new_context->dest_window);
2488 new_context->targets = NULL;
2491 gdk_error_trap_push ();
2492 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2494 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2496 False, XA_ATOM, &type, &format, &nitems,
2497 &after, (guchar **)&data);
2499 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2501 gdk_drag_context_unref (new_context);
2502 return GDK_FILTER_REMOVE;
2505 for (i=0; i<nitems; i++)
2506 new_context->targets =
2507 g_list_append (new_context->targets,
2508 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2516 if (xevent->xclient.data.l[2+i])
2517 new_context->targets =
2518 g_list_append (new_context->targets,
2519 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2520 xevent->xclient.data.l[2+i])));
2523 #ifdef G_ENABLE_DEBUG
2524 if (_gdk_debug_flags & GDK_DEBUG_DND)
2525 print_target_list (new_context->targets);
2526 #endif /* G_ENABLE_DEBUG */
2528 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2529 xdnd_read_actions (new_context);
2531 event->dnd.type = GDK_DRAG_ENTER;
2532 event->dnd.context = new_context;
2533 gdk_drag_context_ref (new_context);
2535 display_x11->current_dest_drag = new_context;
2537 (PRIVATE_DATA (new_context))->xdnd_selection =
2538 gdk_x11_get_xatom_by_name_for_display (display, "XdndSelection");
2539 return GDK_FILTER_TRANSLATE;
2542 static GdkFilterReturn
2543 xdnd_leave_filter (GdkXEvent *xev,
2547 XEvent *xevent = (XEvent *)xev;
2548 guint32 source_window = xevent->xclient.data.l[0];
2549 GdkDisplayX11 *display_x11;
2551 if (!event->any.window ||
2552 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2553 return GDK_FILTER_CONTINUE; /* Not for us */
2556 g_message ("XdndLeave: source_window: %#x",
2559 display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
2561 if ((display_x11->current_dest_drag != NULL) &&
2562 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2563 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2565 event->dnd.type = GDK_DRAG_LEAVE;
2566 /* Pass ownership of context to the event */
2567 event->dnd.context = display_x11->current_dest_drag;
2569 display_x11->current_dest_drag = NULL;
2571 return GDK_FILTER_TRANSLATE;
2574 return GDK_FILTER_REMOVE;
2577 static GdkFilterReturn
2578 xdnd_position_filter (GdkXEvent *xev,
2582 XEvent *xevent = (XEvent *)xev;
2583 guint32 source_window = xevent->xclient.data.l[0];
2584 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2585 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2586 guint32 time = xevent->xclient.data.l[3];
2587 Atom action = xevent->xclient.data.l[4];
2589 GdkDisplay *display;
2590 GdkDisplayX11 *display_x11;
2592 if (!event->any.window ||
2593 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2594 return GDK_FILTER_CONTINUE; /* Not for us */
2597 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2598 source_window, x_root, y_root, time, action));
2600 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2601 display_x11 = GDK_DISPLAY_X11 (display);
2603 if ((display_x11->current_dest_drag != NULL) &&
2604 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2605 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2607 event->dnd.type = GDK_DRAG_MOTION;
2608 event->dnd.context = display_x11->current_dest_drag;
2609 gdk_drag_context_ref (display_x11->current_dest_drag);
2611 event->dnd.time = time;
2613 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2615 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2616 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2618 event->dnd.x_root = x_root;
2619 event->dnd.y_root = y_root;
2621 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2622 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
2624 return GDK_FILTER_TRANSLATE;
2627 return GDK_FILTER_REMOVE;
2630 static GdkFilterReturn
2631 xdnd_drop_filter (GdkXEvent *xev,
2635 XEvent *xevent = (XEvent *)xev;
2636 guint32 source_window = xevent->xclient.data.l[0];
2637 guint32 time = xevent->xclient.data.l[2];
2638 GdkDisplayX11 *display_x11;
2640 if (!event->any.window ||
2641 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2642 return GDK_FILTER_CONTINUE; /* Not for us */
2645 g_message ("XdndDrop: source_window: %#x time: %d",
2646 source_window, time));
2648 display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
2650 if ((display_x11->current_dest_drag != NULL) &&
2651 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2652 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2654 GdkDragContextPrivateX11 *private;
2655 private = PRIVATE_DATA (display_x11->current_dest_drag);
2657 event->dnd.type = GDK_DROP_START;
2659 event->dnd.context = display_x11->current_dest_drag;
2660 gdk_drag_context_ref (display_x11->current_dest_drag);
2662 event->dnd.time = time;
2663 event->dnd.x_root = private->last_x;
2664 event->dnd.y_root = private->last_y;
2666 return GDK_FILTER_TRANSLATE;
2669 return GDK_FILTER_REMOVE;
2672 /*************************************************************
2673 ************************** Public API ***********************
2674 *************************************************************/
2676 _gdk_dnd_init (GdkDisplay *display)
2680 gdk_display_add_client_message_filter (
2682 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2683 motif_dnd_filter, NULL);
2684 gdk_display_add_client_message_filter (
2686 gdk_atom_intern ("XdndEnter", FALSE),
2687 xdnd_enter_filter, NULL);
2688 gdk_display_add_client_message_filter (
2690 gdk_atom_intern ("XdndLeave", FALSE),
2691 xdnd_leave_filter, NULL);
2692 gdk_display_add_client_message_filter (
2694 gdk_atom_intern ("XdndPosition", FALSE),
2695 xdnd_position_filter, NULL);
2696 gdk_display_add_client_message_filter (
2698 gdk_atom_intern ("XdndStatus", FALSE),
2699 xdnd_status_filter, NULL);
2700 gdk_display_add_client_message_filter (
2702 gdk_atom_intern ("XdndFinished", FALSE),
2703 xdnd_finished_filter, NULL);
2704 gdk_display_add_client_message_filter (
2706 gdk_atom_intern ("XdndDrop", FALSE),
2707 xdnd_drop_filter, NULL);
2713 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2715 if (context->dest_window)
2717 switch (context->protocol)
2719 case GDK_DRAG_PROTO_MOTIF:
2720 motif_send_leave (context, time);
2722 case GDK_DRAG_PROTO_XDND:
2723 xdnd_send_leave (context);
2725 case GDK_DRAG_PROTO_ROOTWIN:
2726 case GDK_DRAG_PROTO_NONE:
2731 gdk_window_unref (context->dest_window);
2732 context->dest_window = NULL;
2737 gdk_drag_begin (GdkWindow *window,
2740 GdkDragContext *new_context;
2742 g_return_val_if_fail (window != NULL, NULL);
2744 new_context = gdk_drag_context_new ();
2745 new_context->is_source = TRUE;
2746 new_context->source_window = window;
2747 gdk_window_ref (window);
2749 new_context->targets = g_list_copy (targets);
2750 new_context->actions = 0;
2756 * gdk_drag_get_protocol_for_display:
2757 * @display: the #GdkDisplay where the destination window resides
2758 * @xid: the X id of the destination window.
2759 * @protocol: location where the supported DND protocol is returned.
2760 * @returns: the X id of the window where the drop should happen. This
2761 * may be @xid or the X id of a proxy window, or None if @xid doesn't
2762 * support Drag and Drop.
2764 * Finds out the DND protocol supported by a window.
2767 gdk_drag_get_protocol_for_display (GdkDisplay *display,
2769 GdkDragProtocol *protocol)
2772 g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
2774 if ((retval = xdnd_check_dest (display, xid)))
2776 *protocol = GDK_DRAG_PROTO_XDND;
2777 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2780 else if ((retval = motif_check_dest (display, xid)))
2782 *protocol = GDK_DRAG_PROTO_MOTIF;
2783 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2788 /* Check if this is a root window */
2790 gboolean rootwin = FALSE;
2793 unsigned long nitems, after;
2794 unsigned char *data;
2796 if (_gdk_x11_display_is_root_window (display, (Window) xid))
2799 gdk_error_trap_push ();
2803 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
2804 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
2805 0, 0, False, AnyPropertyType,
2806 &type, &format, &nitems, &after, &data) == Success &&
2814 /* Until I find out what window manager the next one is for,
2815 * I'm leaving it commented out. It's supported in the
2816 * xscreensaver sources, though.
2821 if (XGetWindowProperty (gdk_display, win,
2822 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
2823 0, 0, False, AnyPropertyType,
2824 &type, &format, &nitems, &data) &&
2830 gdk_error_trap_pop ();
2834 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2839 *protocol = GDK_DRAG_PROTO_NONE;
2844 gdk_drag_get_protocol (guint32 xid,
2845 GdkDragProtocol *protocol)
2847 return gdk_drag_get_protocol_for_display (gdk_get_default_display (), xid, protocol);
2851 gdk_drag_find_window (GdkDragContext *context,
2852 GdkWindow *drag_window,
2855 GdkWindow **dest_window,
2856 GdkDragProtocol *protocol)
2858 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2859 GdkDisplay *display;
2862 g_return_if_fail (context != NULL);
2864 display = GDK_WINDOW_DISPLAY (context->source_window);
2866 if (!private->window_cache)
2867 private->window_cache = gdk_window_cache_new (gdk_drawable_get_screen (context->source_window));
2869 dest = get_client_window_at_coords (private->window_cache,
2871 GDK_DRAWABLE_XID (drag_window) : None,
2874 if (private->dest_xid != dest)
2877 private->dest_xid = dest;
2879 /* Check if new destination accepts drags, and which protocol */
2881 /* There is some ugliness here. We actually need to pass
2882 * _three_ pieces of information to drag_motion - dest_window,
2883 * protocol, and the XID of the unproxied window. The first
2884 * two are passed explicitely, the third implicitly through
2885 * protocol->dest_xid.
2887 if ((recipient = gdk_drag_get_protocol_for_display (display, dest, protocol)))
2889 *dest_window = gdk_window_lookup_for_display (display, recipient);
2891 gdk_window_ref (*dest_window);
2893 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
2896 *dest_window = NULL;
2900 *dest_window = context->dest_window;
2902 gdk_window_ref (*dest_window);
2903 *protocol = context->protocol;
2908 gdk_drag_motion (GdkDragContext *context,
2909 GdkWindow *dest_window,
2910 GdkDragProtocol protocol,
2913 GdkDragAction suggested_action,
2914 GdkDragAction possible_actions,
2917 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2919 g_return_val_if_fail (context != NULL, FALSE);
2921 /* When we have a Xdnd target, make sure our XdndActionList
2922 * matches the current actions;
2924 private->old_actions = context->actions;
2925 context->actions = possible_actions;
2927 if ((protocol == GDK_DRAG_PROTO_XDND) &&
2928 (!private->xdnd_actions_set ||
2929 private->xdnd_actions != possible_actions))
2930 xdnd_set_actions (context);
2932 if (context->dest_window != dest_window)
2934 GdkEvent temp_event;
2936 /* Send a leave to the last destination */
2937 gdk_drag_do_leave (context, time);
2938 private->drag_status = GDK_DRAG_STATUS_DRAG;
2940 /* Check if new destination accepts drags, and which protocol */
2944 context->dest_window = dest_window;
2945 private->drop_xid = private->dest_xid;
2946 gdk_window_ref (context->dest_window);
2947 context->protocol = protocol;
2951 case GDK_DRAG_PROTO_MOTIF:
2952 motif_send_enter (context, time);
2955 case GDK_DRAG_PROTO_XDND:
2956 xdnd_send_enter (context);
2959 case GDK_DRAG_PROTO_ROOTWIN:
2960 case GDK_DRAG_PROTO_NONE:
2964 private->old_action = suggested_action;
2965 context->suggested_action = suggested_action;
2966 private->old_actions = possible_actions;
2970 context->dest_window = NULL;
2971 private->drop_xid = None;
2972 context->action = 0;
2975 /* Push a status event, to let the client know that
2979 temp_event.dnd.type = GDK_DRAG_STATUS;
2980 temp_event.dnd.window = context->source_window;
2981 /* We use this to signal a synthetic status. Perhaps
2982 * we should use an extra field...
2984 temp_event.dnd.send_event = TRUE;
2986 temp_event.dnd.context = context;
2987 temp_event.dnd.time = time;
2989 gdk_event_put (&temp_event);
2993 private->old_action = context->suggested_action;
2994 context->suggested_action = suggested_action;
2997 /* Send a drag-motion event */
2999 private->last_x = x_root;
3000 private->last_y = y_root;
3002 if (context->dest_window)
3004 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3006 switch (context->protocol)
3008 case GDK_DRAG_PROTO_MOTIF:
3009 motif_send_motion (context, x_root, y_root, suggested_action, time);
3012 case GDK_DRAG_PROTO_XDND:
3013 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3016 case GDK_DRAG_PROTO_ROOTWIN:
3018 GdkEvent temp_event;
3020 if (g_list_find (context->targets,
3021 GDK_ATOM_TO_POINTER (gdk_atom_intern ("application/x-rootwin-drop", FALSE))))
3022 context->action = context->suggested_action;
3024 context->action = 0;
3026 temp_event.dnd.type = GDK_DRAG_STATUS;
3027 temp_event.dnd.window = context->source_window;
3028 temp_event.dnd.send_event = FALSE;
3029 temp_event.dnd.context = context;
3030 temp_event.dnd.time = time;
3032 gdk_event_put (&temp_event);
3035 case GDK_DRAG_PROTO_NONE:
3036 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3050 gdk_drag_drop (GdkDragContext *context,
3053 g_return_if_fail (context != NULL);
3055 if (context->dest_window)
3057 switch (context->protocol)
3059 case GDK_DRAG_PROTO_MOTIF:
3060 motif_send_leave (context, time);
3061 motif_send_drop (context, time);
3064 case GDK_DRAG_PROTO_XDND:
3065 xdnd_send_drop (context, time);
3068 case GDK_DRAG_PROTO_ROOTWIN:
3069 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3071 case GDK_DRAG_PROTO_NONE:
3072 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3081 gdk_drag_abort (GdkDragContext *context,
3084 g_return_if_fail (context != NULL);
3086 gdk_drag_do_leave (context, time);
3089 /* Destination side */
3092 gdk_drag_status (GdkDragContext *context,
3093 GdkDragAction action,
3096 GdkDragContextPrivateX11 *private;
3098 GdkDisplay *display;
3100 g_return_if_fail (context != NULL);
3102 private = PRIVATE_DATA (context);
3103 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3105 context->action = action;
3107 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3109 xev.xclient.type = ClientMessage;
3110 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3111 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3112 xev.xclient.format = 8;
3113 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3115 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3117 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3121 if ((action != 0) != (private->old_action != 0))
3124 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3126 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3129 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3132 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3136 case GDK_ACTION_MOVE:
3137 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3139 case GDK_ACTION_COPY:
3140 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3142 case GDK_ACTION_LINK:
3143 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3146 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3151 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3153 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3155 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3156 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3157 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3159 if (!_gdk_send_xevent (display,
3160 GDK_DRAWABLE_XID (context->source_window),
3163 g_message ("Send event to %lx failed",
3164 GDK_DRAWABLE_XID (context->source_window)));
3166 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3168 xev.xclient.type = ClientMessage;
3169 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3170 xev.xclient.format = 32;
3171 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3173 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3174 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3175 xev.xclient.data.l[2] = 0;
3176 xev.xclient.data.l[3] = 0;
3177 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3179 if (!xdnd_send_xevent (display,
3180 GDK_DRAWABLE_XID (context->source_window),
3183 g_message ("Send event to %lx failed",
3184 GDK_DRAWABLE_XID (context->source_window)));
3187 private->old_action = action;
3191 gdk_drop_reply (GdkDragContext *context,
3195 GdkDragContextPrivateX11 *private;
3197 g_return_if_fail (context != NULL);
3199 private = PRIVATE_DATA (context);
3201 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3203 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3206 xev.xclient.type = ClientMessage;
3207 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3208 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3209 xev.xclient.format = 8;
3211 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3212 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3214 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3215 (XmDROP_SITE_VALID << 4) |
3216 (XmDROP_NOOP << 8) |
3219 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3220 (XmNO_DROP_SITE << 4) |
3221 (XmDROP_NOOP << 8) |
3222 (XmDROP_CANCEL << 12);
3223 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3224 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3226 _gdk_send_xevent (display,
3227 GDK_DRAWABLE_XID (context->source_window),
3233 gdk_drop_finish (GdkDragContext *context,
3237 g_return_if_fail (context != NULL);
3239 if (context->protocol == GDK_DRAG_PROTO_XDND)
3241 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3244 xev.xclient.type = ClientMessage;
3245 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3246 xev.xclient.format = 32;
3247 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3249 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3250 xev.xclient.data.l[1] = 0;
3251 xev.xclient.data.l[2] = 0;
3252 xev.xclient.data.l[3] = 0;
3253 xev.xclient.data.l[4] = 0;
3255 if (!xdnd_send_xevent (display,
3256 GDK_DRAWABLE_XID (context->source_window),
3259 g_message ("Send event to %lx failed",
3260 GDK_DRAWABLE_XID (context->source_window)));
3266 gdk_window_register_dnd (GdkWindow *window)
3268 static gulong xdnd_version = 3;
3269 MotifDragReceiverInfo info;
3270 Atom motif_drag_receiver_info_atom;
3271 GdkDisplay *display = gdk_drawable_get_display (window);
3273 g_return_if_fail (window != NULL);
3275 if (GPOINTER_TO_INT (gdk_drawable_get_data (window, "gdk-dnd-registered")))
3278 gdk_drawable_set_data (window, "gdk-dnd-registered", GINT_TO_POINTER(TRUE), NULL);
3280 /* Set Motif drag receiver information property */
3282 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3283 "_MOTIF_DRAG_RECEIVER_INFO");
3284 info.byte_order = local_byte_order;
3285 info.protocol_version = 0;
3286 info.protocol_style = XmDRAG_DYNAMIC;
3287 info.proxy_window = None;
3288 info.num_drop_sites = 0;
3289 info.total_size = sizeof(info);
3291 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3292 motif_drag_receiver_info_atom,
3293 motif_drag_receiver_info_atom,
3300 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3301 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3302 GDK_DRAWABLE_XID (window),
3303 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3304 XA_ATOM, 32, PropModeReplace,
3305 (guchar *)&xdnd_version, 1);
3308 /*************************************************************
3309 * gdk_drag_get_selection:
3310 * Returns the selection atom for the current source window
3314 *************************************************************/
3317 gdk_drag_get_selection (GdkDragContext *context)
3319 g_return_val_if_fail (context != NULL, GDK_NONE);
3321 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3322 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3323 (PRIVATE_DATA (context))->motif_selection);
3324 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3325 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3326 (PRIVATE_DATA (context))->xdnd_selection);