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;
346 node->next = above_node;
347 above_node->prev = node;
355 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
357 if (!g_hash_table_lookup (cache->child_hash,
358 GUINT_TO_POINTER (xcwe->window)))
359 gdk_window_cache_add (cache, xcwe->window,
360 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
366 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
369 node = g_hash_table_lookup (cache->child_hash,
370 GUINT_TO_POINTER (xdwe->window));
373 g_hash_table_remove (cache->child_hash,
374 GUINT_TO_POINTER (xdwe->window));
375 cache->children = g_list_remove_link (cache->children, node);
377 g_list_free_1 (node);
383 XMapEvent *xme = &xevent->xmap;
386 node = g_hash_table_lookup (cache->child_hash,
387 GUINT_TO_POINTER (xme->window));
390 GdkCacheChild *child = node->data;
391 child->mapped = TRUE;
399 XMapEvent *xume = &xevent->xmap;
402 node = g_hash_table_lookup (cache->child_hash,
403 GUINT_TO_POINTER (xume->window));
406 GdkCacheChild *child = node->data;
407 child->mapped = FALSE;
412 return GDK_FILTER_CONTINUE;
414 return GDK_FILTER_REMOVE;
417 static GdkWindowCache *
418 gdk_window_cache_new (GdkScreen *screen)
420 XWindowAttributes xwa;
421 Window root, parent, *children;
422 unsigned int nchildren;
424 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
425 GdkWindow *root_window = gdk_screen_get_root_window (screen);
427 GdkWindowCache *result = g_new (GdkWindowCache, 1);
429 result->children = NULL;
430 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
431 result->screen = screen;
433 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
434 result->old_event_mask = xwa.your_event_mask;
435 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
436 result->old_event_mask | SubstructureNotifyMask);
437 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
439 gdk_error_trap_push ();
441 if (!XQueryTree(xdisplay, GDK_WINDOW_XWINDOW (root_window),
442 &root, &parent, &children, &nchildren))
444 gdk_error_trap_pop ();
448 for (i = 0; i < nchildren ; i++)
450 if (XGetWindowAttributes (xdisplay, children[i], &xwa))
451 gdk_window_cache_add (result, children[i],
452 xwa.x, xwa.y, xwa.width, xwa.height,
453 xwa.map_state != IsUnmapped);
457 gdk_error_trap_pop ();
463 gdk_window_cache_destroy (GdkWindowCache *cache)
465 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
467 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
468 GDK_WINDOW_XWINDOW (root_window),
469 cache->old_event_mask);
470 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
472 g_list_foreach (cache->children, (GFunc)g_free, NULL);
473 g_list_free (cache->children);
474 g_hash_table_destroy (cache->child_hash);
480 get_client_window_at_coords_recurse (GdkDisplay *display,
485 Window root, tmp_parent, *children;
486 unsigned int nchildren;
491 unsigned long nitems, after;
494 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(display), win,
495 gdk_x11_get_xatom_by_name_for_display (display, "WM_STATE"),
496 0, 0, False, AnyPropertyType,
497 &type, &format, &nitems, &after, &data) != Success)
507 /* This is beautiful! Damn Enlightenment and click-to-focus */
508 if (!XTranslateCoordinates (gdk_display, _gdk_root_window, win,
509 x_root, y_root, &dest_x, &dest_y, &child))
513 if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), win,
514 &root, &tmp_parent, &children, &nchildren))
517 for (i = nchildren - 1; (i >= 0) && (child == None); i--)
519 XWindowAttributes xwa;
521 if (XGetWindowAttributes (GDK_DISPLAY_XDISPLAY (display),
524 if ((xwa.map_state == IsViewable) && (xwa.class == InputOutput) &&
525 (x >= xwa.x) && (x < xwa.x + (gint)xwa.width) &&
526 (y >= xwa.y) && (y < xwa.y + (gint)xwa.height))
539 return get_client_window_at_coords_recurse (display, child, x, y);
545 get_client_window_at_coords (GdkWindowCache *cache,
551 Window retval = None;
553 gdk_error_trap_push ();
555 tmp_list = cache->children;
557 while (tmp_list && !retval)
559 GdkCacheChild *child = tmp_list->data;
561 if ((child->xid != ignore) && (child->mapped))
563 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
564 (y_root >= child->y) && (y_root < child->y + child->height))
566 retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
575 tmp_list = tmp_list->next;
578 gdk_error_trap_pop ();
583 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
586 /*************************************************************
587 ***************************** MOTIF *************************
588 *************************************************************/
590 /* values used in the message type for Motif DND */
603 /* Values used to specify type of protocol to use */
607 XmDRAG_PREFER_PREREGISTER,
609 XmDRAG_PREFER_DYNAMIC,
611 XmDRAG_PREFER_RECEIVER
614 /* Operation codes */
622 /* Drop site status */
624 XmNO_DROP_SITE = 0x01,
625 XmDROP_SITE_INVALID = 0x02,
626 XmDROP_SITE_VALID = 0x03
629 /* completion status */
637 /* Byte swapping routines. The motif specification leaves it
638 * up to us to save a few bytes in the client messages
640 static gchar local_byte_order = '\0';
642 #ifdef G_ENABLE_DEBUG
644 print_target_list (GList *targets)
648 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
649 g_message ("\t%s", name);
651 targets = targets->next;
654 #endif /* G_ENABLE_DEBUG */
657 init_byte_order (void)
659 guint32 myint = 0x01020304;
660 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
664 card16_to_host (guint16 x, gchar byte_order) {
665 if (byte_order == local_byte_order)
668 return (x << 8) | (x >> 8);
672 card32_to_host (guint32 x, gchar byte_order) {
673 if (byte_order == local_byte_order)
676 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
679 /* Motif packs together fields of varying length into the
680 * client message. We can't rely on accessing these
681 * through data.s[], data.l[], etc, because on some architectures
682 * (i.e., Alpha) these won't be valid for format == 8.
685 #define MOTIF_XCLIENT_BYTE(xevent,i) \
686 (xevent)->xclient.data.b[i]
687 #define MOTIF_XCLIENT_SHORT(xevent,i) \
688 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
689 #define MOTIF_XCLIENT_LONG(xevent,i) \
690 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
692 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
693 #define MOTIF_UNPACK_SHORT(xevent,i) \
694 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
695 #define MOTIF_UNPACK_LONG(xevent,i) \
696 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
698 /***** Dest side ***********/
700 /* Property placed on source windows */
701 typedef struct _MotifDragInitiatorInfo {
703 guint8 protocol_version;
704 guint16 targets_index;
705 guint32 selection_atom;
706 } MotifDragInitiatorInfo;
708 /* Header for target table on the drag window */
709 typedef struct _MotifTargetTableHeader {
711 guchar protocol_version;
714 } MotifTargetTableHeader;
716 /* Property placed on target windows */
717 typedef struct _MotifDragReceiverInfo {
719 guint8 protocol_version;
720 guint8 protocol_style;
722 guint32 proxy_window;
723 guint16 num_drop_sites;
726 } MotifDragReceiverInfo;
728 /* Target table handling */
730 static GdkFilterReturn
731 motif_drag_window_filter (GdkXEvent *xevent,
735 XEvent *xev = (XEvent *)xevent;
736 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
737 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
739 switch (xev->xany.type)
742 display_x11->motif_drag_window = None;
743 display_x11->motif_drag_gdk_window = NULL;
746 if (display_x11->motif_target_lists &&
747 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
748 motif_read_target_table (display);
751 return GDK_FILTER_REMOVE;
755 motif_lookup_drag_window (GdkDisplay *display,
756 Display *lookup_xdisplay)
758 Window retval = None;
759 gulong bytes_after, nitems;
764 XGetWindowProperty (lookup_xdisplay, DefaultRootWindow (lookup_xdisplay),
765 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
767 XA_WINDOW, &type, &format, &nitems, &bytes_after,
770 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
772 retval = *(Window *)data;
774 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
783 /* Finds the window where global Motif drag information is stored.
784 * If it doesn't exist and 'create' is TRUE, create one.
787 motif_find_drag_window (GdkDisplay *display,
790 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
792 if (!display_x11->motif_drag_window)
794 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
795 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
797 if (!display_x11->motif_drag_window && create)
799 /* Create a persistant window. (Copied from LessTif) */
801 Display *persistant_xdisplay;
802 XSetWindowAttributes attr;
803 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
804 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
806 XGrabServer (persistant_xdisplay);
808 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
810 if (!display_x11->motif_drag_window)
812 attr.override_redirect = True;
813 attr.event_mask = PropertyChangeMask;
815 display_x11->motif_drag_window =
816 XCreateWindow (persistant_xdisplay,
817 DefaultRootWindow (persistant_xdisplay),
818 -100, -100, 10, 10, 0, 0,
819 InputOnly, CopyFromParent,
820 (CWOverrideRedirect | CWEventMask), &attr);
823 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
825 XChangeProperty (persistant_xdisplay,
826 DefaultRootWindow (persistant_xdisplay),
827 motif_drag_window_atom, XA_WINDOW,
829 (guchar *)&motif_drag_window_atom, 1);
832 XUngrabServer (persistant_xdisplay);
833 XCloseDisplay (persistant_xdisplay);
836 /* There is a miniscule race condition here if the drag window
837 * gets destroyed exactly now.
839 if (display_x11->motif_drag_window)
841 display_x11->motif_drag_gdk_window =
842 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
843 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
844 motif_drag_window_filter,
849 return display_x11->motif_drag_window;
853 motif_read_target_table (GdkDisplay *display)
855 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
856 gulong bytes_after, nitems;
861 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
863 if (display_x11->motif_target_lists)
865 for (i=0; i<display_x11->motif_n_target_lists; i++)
866 g_list_free (display_x11->motif_target_lists[i]);
868 g_free (display_x11->motif_target_lists);
869 display_x11->motif_target_lists = NULL;
870 display_x11->motif_n_target_lists = 0;
873 if (motif_find_drag_window (display, FALSE))
875 MotifTargetTableHeader *header = NULL;
876 guchar *target_bytes = NULL;
878 gboolean success = FALSE;
880 gdk_error_trap_push ();
881 XGetWindowProperty (display_x11->xdisplay,
882 display_x11->motif_drag_window,
883 motif_drag_targets_atom,
884 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
885 motif_drag_targets_atom,
886 &type, &format, &nitems, &bytes_after,
889 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
892 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
893 header->total_size = card32_to_host (header->total_size, header->byte_order);
895 gdk_error_trap_push ();
896 XGetWindowProperty (display_x11->xdisplay,
897 display_x11->motif_drag_window,
898 motif_drag_targets_atom,
899 (sizeof(MotifTargetTableHeader)+3)/4,
900 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
902 motif_drag_targets_atom, &type, &format, &nitems,
903 &bytes_after, &target_bytes);
905 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
906 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
909 display_x11->motif_n_target_lists = header->n_lists;
910 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
913 for (i=0; i<header->n_lists; i++)
918 if (p + sizeof(guint16) - target_bytes > nitems)
921 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
923 /* We need to make a copy of the targets, since it may
926 targets = g_new (guint32, n_targets);
927 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
929 p += sizeof(guint16) + n_targets * sizeof(guint32);
930 if (p - target_bytes > nitems)
933 for (j=0; j<n_targets; j++)
934 display_x11->motif_target_lists[i] =
935 g_list_prepend (display_x11->motif_target_lists[i],
936 GUINT_TO_POINTER (card32_to_host (targets[j],
937 header->byte_order)));
939 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
949 XFree (target_bytes);
953 if (display_x11->motif_target_lists)
955 g_free (display_x11->motif_target_lists);
956 display_x11->motif_target_lists = NULL;
957 display_x11->motif_n_target_lists = 0;
959 g_warning ("Error reading Motif target table\n");
965 targets_sort_func (gconstpointer a, gconstpointer b)
967 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
968 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
971 /* Check if given (sorted) list is in the targets table */
973 motif_target_table_check (GdkDisplay *display,
976 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
977 GList *tmp_list1, *tmp_list2;
980 for (i=0; i<display_x11->motif_n_target_lists; i++)
982 tmp_list1 = display_x11->motif_target_lists[i];
985 while (tmp_list1 && tmp_list2)
987 if (tmp_list1->data != tmp_list2->data)
990 tmp_list1 = tmp_list1->next;
991 tmp_list2 = tmp_list2->next;
993 if (!tmp_list1 && !tmp_list2) /* Found it */
1001 motif_add_to_target_table (GdkDisplay *display,
1002 GList *targets) /* targets is list of GdkAtom */
1004 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1005 GList *sorted = NULL;
1010 /* make a sorted copy of the list */
1014 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1015 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1016 targets = targets->next;
1019 /* First check if it is their already */
1021 if (display_x11->motif_target_lists)
1022 index = motif_target_table_check (display, sorted);
1024 /* We need to grab the server while doing this, to ensure
1030 /* We need to make sure that it exists _before_ we grab the
1031 * server, since we can't open a new connection after we
1034 motif_find_drag_window (display, TRUE);
1036 gdk_x11_display_grab (display);
1037 motif_read_target_table (display);
1039 /* Check again, in case it was added in the meantime */
1041 if (display_x11->motif_target_lists)
1042 index = motif_target_table_check (display, sorted);
1046 guint32 total_size = 0;
1050 MotifTargetTableHeader *header;
1052 if (!display_x11->motif_target_lists)
1054 display_x11->motif_target_lists = g_new (GList *, 1);
1055 display_x11->motif_n_target_lists = 1;
1059 display_x11->motif_n_target_lists++;
1060 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1061 sizeof(GList *) * display_x11->motif_n_target_lists);
1063 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1065 index = display_x11->motif_n_target_lists - 1;
1067 total_size = sizeof (MotifTargetTableHeader);
1068 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1069 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1071 data = g_malloc (total_size);
1073 header = (MotifTargetTableHeader *)data;
1074 p = data + sizeof(MotifTargetTableHeader);
1076 header->byte_order = local_byte_order;
1077 header->protocol_version = 0;
1078 header->n_lists = display_x11->motif_n_target_lists;
1079 header->total_size = total_size;
1081 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1083 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1084 guint32 *targets = g_new (guint32, n_targets);
1085 guint32 *p32 = targets;
1087 tmp_list = display_x11->motif_target_lists[i];
1090 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1092 tmp_list = tmp_list->next;
1097 p += sizeof(guint16);
1099 memcpy (p, targets, n_targets * sizeof(guint32));
1102 p += sizeof(guint32) * n_targets;
1106 XChangeProperty (display_x11->xdisplay,
1107 display_x11->motif_drag_window,
1108 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1109 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1113 gdk_x11_display_ungrab (display);
1116 g_list_free (sorted);
1120 /* Translate flags */
1123 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1125 guint recommended_op = flags & 0x000f;
1126 guint possible_ops = (flags & 0x0f0) >> 4;
1128 switch (recommended_op)
1131 context->suggested_action = GDK_ACTION_MOVE;
1134 context->suggested_action = GDK_ACTION_COPY;
1137 context->suggested_action = GDK_ACTION_LINK;
1140 context->suggested_action = GDK_ACTION_COPY;
1144 context->actions = 0;
1145 if (possible_ops & XmDROP_MOVE)
1146 context->actions |= GDK_ACTION_MOVE;
1147 if (possible_ops & XmDROP_COPY)
1148 context->actions |= GDK_ACTION_COPY;
1149 if (possible_ops & XmDROP_LINK)
1150 context->actions |= GDK_ACTION_LINK;
1154 motif_dnd_get_flags (GdkDragContext *context)
1158 switch (context->suggested_action)
1160 case GDK_ACTION_MOVE:
1161 flags = XmDROP_MOVE;
1163 case GDK_ACTION_COPY:
1164 flags = XmDROP_COPY;
1166 case GDK_ACTION_LINK:
1167 flags = XmDROP_LINK;
1170 flags = XmDROP_NOOP;
1174 if (context->actions & GDK_ACTION_MOVE)
1175 flags |= XmDROP_MOVE << 8;
1176 if (context->actions & GDK_ACTION_COPY)
1177 flags |= XmDROP_COPY << 8;
1178 if (context->actions & GDK_ACTION_LINK)
1179 flags |= XmDROP_LINK << 8;
1187 motif_set_targets (GdkDragContext *context)
1189 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1190 MotifDragInitiatorInfo info;
1192 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1194 info.byte_order = local_byte_order;
1195 info.protocol_version = 0;
1197 info.targets_index = motif_add_to_target_table (display, context->targets);
1202 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1204 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1205 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1209 info.selection_atom = private->motif_selection;
1211 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1212 GDK_DRAWABLE_XID (context->source_window),
1213 private->motif_selection,
1214 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1216 (guchar *)&info, sizeof (info));
1218 private->motif_targets_set = 1;
1222 motif_check_dest (GdkDisplay *display,
1225 gboolean retval = FALSE;
1226 MotifDragReceiverInfo *info;
1229 unsigned long nitems, after;
1230 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1232 gdk_error_trap_push ();
1233 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1234 motif_drag_receiver_info_atom,
1235 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1236 &type, &format, &nitems, &after,
1239 if (gdk_error_trap_pop() == 0)
1243 if ((format == 8) && (nitems == sizeof(*info)))
1245 if ((info->protocol_version == 0) &&
1246 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1247 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1248 (info->protocol_style == XmDRAG_DYNAMIC)))
1254 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1261 return retval ? win : None;
1265 motif_send_enter (GdkDragContext *context,
1268 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1269 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1272 xev.xclient.type = ClientMessage;
1273 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1274 xev.xclient.format = 8;
1275 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1277 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1278 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1279 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1280 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1281 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1283 if (!private->motif_targets_set)
1284 motif_set_targets (context);
1286 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1288 if (!_gdk_send_xevent (display,
1289 GDK_DRAWABLE_XID (context->dest_window),
1292 g_message ("Send event to %lx failed",
1293 GDK_DRAWABLE_XID (context->dest_window)));
1297 motif_send_leave (GdkDragContext *context,
1300 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1303 xev.xclient.type = ClientMessage;
1304 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1305 xev.xclient.format = 8;
1306 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1308 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1309 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1310 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1311 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1312 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1313 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1315 if (!_gdk_send_xevent (display,
1316 GDK_DRAWABLE_XID (context->dest_window),
1319 g_message ("Send event to %lx failed",
1320 GDK_DRAWABLE_XID (context->dest_window)));
1324 motif_send_motion (GdkDragContext *context,
1327 GdkDragAction action,
1330 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1331 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1335 xev.xclient.type = ClientMessage;
1336 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1337 xev.xclient.format = 8;
1338 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1340 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1341 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1342 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1344 if ((context->suggested_action != private->old_action) ||
1345 (context->actions != private->old_actions))
1347 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1349 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1354 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1356 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1357 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1359 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1363 if (!_gdk_send_xevent (display,
1364 GDK_DRAWABLE_XID (context->dest_window),
1367 g_message ("Send event to %lx failed",
1368 GDK_DRAWABLE_XID (context->dest_window)));
1374 motif_send_drop (GdkDragContext *context, guint32 time)
1376 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1377 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1380 xev.xclient.type = ClientMessage;
1381 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1382 xev.xclient.format = 8;
1383 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1385 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1386 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1387 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1388 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1390 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1391 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1393 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1394 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1396 if (!_gdk_send_xevent (display,
1397 GDK_DRAWABLE_XID (context->dest_window),
1400 g_message ("Send event to %lx failed",
1401 GDK_DRAWABLE_XID (context->dest_window)));
1407 motif_read_initiator_info (GdkDisplay *display,
1408 Window source_window,
1418 MotifDragInitiatorInfo *initiator_info;
1420 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1422 gdk_error_trap_push ();
1423 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1424 0, sizeof(*initiator_info), FALSE,
1425 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1426 &type, &format, &nitems, &bytes_after,
1427 (guchar **)&initiator_info);
1429 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1431 g_warning ("Error reading initiator info\n");
1435 motif_read_target_table (display);
1437 initiator_info->targets_index =
1438 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1439 initiator_info->selection_atom =
1440 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1442 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1444 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1445 XFree (initiator_info);
1446 return GDK_FILTER_REMOVE;
1449 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1454 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1455 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1456 tmp_list = tmp_list->prev;
1459 #ifdef G_ENABLE_DEBUG
1460 if (_gdk_debug_flags & GDK_DEBUG_DND)
1461 print_target_list (*targets);
1462 #endif /* G_ENABLE_DEBUG */
1464 *selection = initiator_info->selection_atom;
1466 XFree (initiator_info);
1471 static GdkDragContext *
1472 motif_drag_context_new (GdkWindow *dest_window,
1474 guint32 source_window,
1477 GdkDragContext *new_context;
1478 GdkDragContextPrivateX11 *private;
1479 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1480 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1482 /* FIXME, current_dest_drag really shouldn't be NULL'd
1483 * if we error below.
1485 if (display_x11->current_dest_drag != NULL)
1487 if (timestamp >= display_x11->current_dest_drag->start_time)
1489 gdk_drag_context_unref (display_x11->current_dest_drag);
1490 display_x11->current_dest_drag = NULL;
1496 new_context = gdk_drag_context_new ();
1497 private = PRIVATE_DATA (new_context);
1499 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1500 new_context->is_source = FALSE;
1502 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1503 if (new_context->source_window)
1504 gdk_window_ref (new_context->source_window);
1507 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1508 if (!new_context->source_window)
1510 gdk_drag_context_unref (new_context);
1515 new_context->dest_window = dest_window;
1516 gdk_window_ref (dest_window);
1517 new_context->start_time = timestamp;
1519 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1522 &new_context->targets,
1523 &private->motif_selection))
1525 gdk_drag_context_unref (new_context);
1533 * The MOTIF drag protocol has no real provisions for distinguishing
1534 * multiple simultaneous drops. If the sources grab the pointer
1535 * when doing drags, that shouldn't happen, in any case. If it
1536 * does, we can't do much except hope for the best.
1539 static GdkFilterReturn
1540 motif_top_level_enter (GdkEvent *event,
1543 guint32 source_window,
1546 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1547 GdkDragContext *new_context;
1549 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1550 flags, timestamp, source_window, atom));
1552 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1554 return GDK_FILTER_REMOVE;
1556 event->dnd.type = GDK_DRAG_ENTER;
1557 event->dnd.context = new_context;
1558 gdk_drag_context_ref (new_context);
1560 display_x11->current_dest_drag = new_context;
1562 return GDK_FILTER_TRANSLATE;
1565 static GdkFilterReturn
1566 motif_top_level_leave (GdkEvent *event,
1570 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1572 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1575 if ((display_x11->current_dest_drag != NULL) &&
1576 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1577 (timestamp >= display_x11->current_dest_drag->start_time))
1579 event->dnd.type = GDK_DRAG_LEAVE;
1580 /* Pass ownership of context to the event */
1581 event->dnd.context = display_x11->current_dest_drag;
1583 display_x11->current_dest_drag = NULL;
1585 return GDK_FILTER_TRANSLATE;
1588 return GDK_FILTER_REMOVE;
1591 static GdkFilterReturn
1592 motif_motion (GdkEvent *event,
1598 GdkDragContextPrivateX11 *private;
1599 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1601 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1602 flags, timestamp, x_root, y_root));
1604 if ((display_x11->current_dest_drag != NULL) &&
1605 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1606 (timestamp >= display_x11->current_dest_drag->start_time))
1608 private = PRIVATE_DATA (display_x11->current_dest_drag);
1610 event->dnd.type = GDK_DRAG_MOTION;
1611 event->dnd.context = display_x11->current_dest_drag;
1612 gdk_drag_context_ref (display_x11->current_dest_drag);
1614 event->dnd.time = timestamp;
1616 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1618 event->dnd.x_root = x_root;
1619 event->dnd.y_root = y_root;
1621 private->last_x = x_root;
1622 private->last_y = y_root;
1624 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1626 return GDK_FILTER_TRANSLATE;
1629 return GDK_FILTER_REMOVE;
1632 static GdkFilterReturn
1633 motif_operation_changed (GdkEvent *event,
1637 GdkDragContextPrivateX11 *private;
1638 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1639 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1642 if ((display_x11->current_dest_drag != NULL) &&
1643 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1644 (timestamp >= display_x11->current_dest_drag->start_time))
1646 event->dnd.type = GDK_DRAG_MOTION;
1647 event->dnd.send_event = FALSE;
1648 event->dnd.context = display_x11->current_dest_drag;
1649 gdk_drag_context_ref (display_x11->current_dest_drag);
1651 event->dnd.time = timestamp;
1652 private = PRIVATE_DATA (display_x11->current_dest_drag);
1654 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1656 event->dnd.x_root = private->last_x;
1657 event->dnd.y_root = private->last_y;
1659 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1661 return GDK_FILTER_TRANSLATE;
1664 return GDK_FILTER_REMOVE;
1667 static GdkFilterReturn
1668 motif_drop_start (GdkEvent *event,
1671 guint32 source_window,
1676 GdkDragContext *new_context;
1677 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1679 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1680 flags, timestamp, x_root, y_root, source_window, atom));
1682 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1684 return GDK_FILTER_REMOVE;
1686 motif_dnd_translate_flags (new_context, flags);
1688 event->dnd.type = GDK_DROP_START;
1689 event->dnd.context = new_context;
1690 event->dnd.time = timestamp;
1691 event->dnd.x_root = x_root;
1692 event->dnd.y_root = y_root;
1694 gdk_drag_context_ref (new_context);
1695 display_x11->current_dest_drag = new_context;
1697 return GDK_FILTER_TRANSLATE;
1700 static GdkFilterReturn
1701 motif_drag_status (GdkEvent *event,
1705 GdkDragContext *context;
1706 GdkDisplay *display;
1709 g_message ("Motif status message: flags %x", flags));
1711 display = gdk_drawable_get_display (event->any.window);
1713 return GDK_FILTER_REMOVE;
1715 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1719 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1720 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1721 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1722 private->drag_status = GDK_DRAG_STATUS_DRAG;
1724 event->dnd.type = GDK_DRAG_STATUS;
1725 event->dnd.send_event = FALSE;
1726 event->dnd.context = context;
1727 gdk_drag_context_ref (context);
1729 event->dnd.time = timestamp;
1731 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1733 switch (flags & 0x000f)
1736 context->action = 0;
1739 context->action = GDK_ACTION_MOVE;
1742 context->action = GDK_ACTION_COPY;
1745 context->action = GDK_ACTION_LINK;
1750 context->action = 0;
1752 return GDK_FILTER_TRANSLATE;
1754 return GDK_FILTER_REMOVE;
1757 static GdkFilterReturn
1758 motif_dnd_filter (GdkXEvent *xev,
1762 XEvent *xevent = (XEvent *)xev;
1767 guint32 source_window;
1769 gint16 x_root, y_root;
1772 /* First read some fields common to all Motif DND messages */
1774 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1775 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1776 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1778 is_reply = ((reason & 0x80) != 0);
1780 switch (reason & 0x7f)
1782 case XmTOP_LEVEL_ENTER:
1783 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1784 atom = MOTIF_UNPACK_LONG (xevent, 3);
1785 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1786 case XmTOP_LEVEL_LEAVE:
1787 return motif_top_level_leave (event, flags, timestamp);
1790 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1791 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1794 return motif_motion (event, flags, timestamp, x_root, y_root);
1796 return motif_drag_status (event, flags, timestamp);
1798 case XmDROP_SITE_ENTER:
1799 return motif_drag_status (event, flags, timestamp);
1801 case XmDROP_SITE_LEAVE:
1802 return motif_drag_status (event,
1803 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1806 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1807 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1808 atom = MOTIF_UNPACK_LONG (xevent, 3);
1809 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1812 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1815 case XmOPERATION_CHANGED:
1817 return motif_operation_changed (event, flags, timestamp);
1819 return motif_drag_status (event, flags, timestamp);
1822 /* To the best of my knowledge, these next two messages are
1823 * not part of the protocol, though they are defined in
1827 case XmDRAG_DROP_FINISH:
1831 return GDK_FILTER_REMOVE;
1834 /*************************************************************
1835 ***************************** XDND **************************
1836 *************************************************************/
1838 /* Utility functions */
1843 GdkDragAction action;
1844 } xdnd_actions_table[] = {
1845 { "XdndActionCopy", None, GDK_ACTION_COPY },
1846 { "XdndActionMove", None, GDK_ACTION_MOVE },
1847 { "XdndActionLink", None, GDK_ACTION_LINK },
1848 { "XdndActionAsk", None, GDK_ACTION_ASK },
1849 { "XdndActionPrivate", None, GDK_ACTION_COPY },
1852 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1853 static gboolean xdnd_actions_initialized = FALSE;
1856 xdnd_initialize_actions (void)
1860 xdnd_actions_initialized = TRUE;
1861 for (i=0; i < xdnd_n_actions; i++)
1862 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1865 static GdkDragAction
1866 xdnd_action_from_atom (GdkDisplay *display,
1869 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
1872 if (!xdnd_actions_initialized)
1873 xdnd_initialize_actions();
1875 for (i=0; i<xdnd_n_actions; i++)
1876 if (atom == xdnd_actions_table[i].atom)
1877 return xdnd_actions_table[i].action;
1883 xdnd_action_to_atom (GdkDisplay *display,
1884 GdkDragAction action)
1888 if (!xdnd_actions_initialized)
1889 xdnd_initialize_actions();
1891 for (i=0; i<xdnd_n_actions; i++)
1892 if (action == xdnd_actions_table[i].action)
1893 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
1900 static GdkFilterReturn
1901 xdnd_status_filter (GdkXEvent *xev,
1905 GdkDisplay *display;
1906 XEvent *xevent = (XEvent *)xev;
1907 guint32 dest_window = xevent->xclient.data.l[0];
1908 guint32 flags = xevent->xclient.data.l[1];
1909 Atom action = xevent->xclient.data.l[4];
1910 GdkDragContext *context;
1913 g_message ("XdndStatus: dest_window: %#x action: %ld",
1914 dest_window, action));
1916 display = gdk_drawable_get_display (event->any.window);
1918 return GDK_FILTER_REMOVE;
1920 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1924 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1925 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1926 private->drag_status = GDK_DRAG_STATUS_DRAG;
1928 event->dnd.send_event = FALSE;
1929 event->dnd.type = GDK_DRAG_STATUS;
1930 event->dnd.context = context;
1931 gdk_drag_context_ref (context);
1933 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1934 if (!(action != 0) != !(flags & 1))
1937 g_warning ("Received status event with flags not corresponding to action!\n"));
1941 context->action = xdnd_action_from_atom (display, action);
1943 return GDK_FILTER_TRANSLATE;
1946 return GDK_FILTER_REMOVE;
1949 static GdkFilterReturn
1950 xdnd_finished_filter (GdkXEvent *xev,
1954 GdkDisplay *display;
1955 XEvent *xevent = (XEvent *)xev;
1956 guint32 dest_window = xevent->xclient.data.l[0];
1957 GdkDragContext *context;
1960 g_message ("XdndFinished: dest_window: %#x", dest_window));
1962 display = gdk_drawable_get_display (event->any.window);
1964 return GDK_FILTER_REMOVE;
1966 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1970 event->dnd.type = GDK_DROP_FINISHED;
1971 event->dnd.context = context;
1972 gdk_drag_context_ref (context);
1974 return GDK_FILTER_TRANSLATE;
1977 return GDK_FILTER_REMOVE;
1981 xdnd_set_targets (GdkDragContext *context)
1983 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1985 GList *tmp_list = context->targets;
1987 gint n_atoms = g_list_length (context->targets);
1988 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1990 atomlist = g_new (Atom, n_atoms);
1994 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
1995 tmp_list = tmp_list->next;
1999 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2000 GDK_DRAWABLE_XID (context->source_window),
2001 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2002 XA_ATOM, 32, PropModeReplace,
2003 (guchar *)atomlist, n_atoms);
2007 private->xdnd_targets_set = 1;
2011 xdnd_set_actions (GdkDragContext *context)
2013 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2018 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2020 if (!xdnd_actions_initialized)
2021 xdnd_initialize_actions();
2023 actions = context->actions;
2025 for (i=0; i<xdnd_n_actions; i++)
2027 if (actions & xdnd_actions_table[i].action)
2029 actions &= ~xdnd_actions_table[i].action;
2034 atomlist = g_new (Atom, n_atoms);
2036 actions = context->actions;
2038 for (i=0; i<xdnd_n_actions; i++)
2040 if (actions & xdnd_actions_table[i].action)
2042 actions &= ~xdnd_actions_table[i].action;
2043 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2048 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2049 GDK_DRAWABLE_XID (context->source_window),
2050 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2051 XA_ATOM, 32, PropModeReplace,
2052 (guchar *)atomlist, n_atoms);
2056 private->xdnd_actions_set = 1;
2057 private->xdnd_actions = context->actions;
2060 /*************************************************************
2062 * Like gdk_send_event, but if the target is the root
2063 * window, sets an event mask of ButtonPressMask, otherwise
2064 * an event mask of 0.
2068 *************************************************************/
2071 xdnd_send_xevent (GdkDisplay *display,
2076 if (_gdk_x11_display_is_root_window (display, window))
2077 return _gdk_send_xevent (display, window, propagate, ButtonPressMask, event_send);
2079 return _gdk_send_xevent (display, window, propagate, 0, event_send);
2083 xdnd_send_enter (GdkDragContext *context)
2086 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2087 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2089 xev.xclient.type = ClientMessage;
2090 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2091 xev.xclient.format = 32;
2092 xev.xclient.window = private->drop_xid ?
2094 GDK_DRAWABLE_XID (context->dest_window);
2095 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2096 xev.xclient.data.l[1] = (3 << 24); /* version */
2097 xev.xclient.data.l[2] = 0;
2098 xev.xclient.data.l[3] = 0;
2099 xev.xclient.data.l[4] = 0;
2101 if (!private->xdnd_selection)
2102 private->xdnd_selection = gdk_x11_get_xatom_by_name_for_display (display, "XdndSelection");
2104 if (g_list_length (context->targets) > 3)
2106 if (!private->xdnd_targets_set)
2107 xdnd_set_targets (context);
2108 xev.xclient.data.l[1] |= 1;
2112 GList *tmp_list = context->targets;
2117 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2118 GDK_POINTER_TO_ATOM (tmp_list->data));
2119 tmp_list = tmp_list->next;
2124 if (!xdnd_send_xevent (display,
2125 GDK_DRAWABLE_XID (context->dest_window),
2129 g_message ("Send event to %lx failed",
2130 GDK_DRAWABLE_XID (context->dest_window)));
2131 gdk_window_unref (context->dest_window);
2132 context->dest_window = NULL;
2137 xdnd_send_leave (GdkDragContext *context)
2139 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2142 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2144 xev.xclient.type = ClientMessage;
2145 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2146 xev.xclient.format = 32;
2147 xev.xclient.window = private->drop_xid ?
2149 GDK_DRAWABLE_XID (context->dest_window);
2150 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2151 xev.xclient.data.l[1] = 0;
2152 xev.xclient.data.l[2] = 0;
2153 xev.xclient.data.l[3] = 0;
2154 xev.xclient.data.l[4] = 0;
2156 if (!xdnd_send_xevent (display,
2157 GDK_DRAWABLE_XID (context->dest_window),
2161 g_message ("Send event to %lx failed",
2162 GDK_DRAWABLE_XID (context->dest_window)));
2163 gdk_window_unref (context->dest_window);
2164 context->dest_window = NULL;
2169 xdnd_send_drop (GdkDragContext *context, guint32 time)
2171 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2172 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2175 xev.xclient.type = ClientMessage;
2176 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2177 xev.xclient.format = 32;
2178 xev.xclient.window = private->drop_xid ?
2180 GDK_DRAWABLE_XID (context->dest_window);
2181 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2182 xev.xclient.data.l[1] = 0;
2183 xev.xclient.data.l[2] = time;
2184 xev.xclient.data.l[3] = 0;
2185 xev.xclient.data.l[4] = 0;
2187 if (!xdnd_send_xevent (display,
2188 GDK_DRAWABLE_XID (context->dest_window),
2192 g_message ("Send event to %lx failed",
2193 GDK_DRAWABLE_XID (context->dest_window)));
2194 gdk_window_unref (context->dest_window);
2195 context->dest_window = NULL;
2200 xdnd_send_motion (GdkDragContext *context,
2203 GdkDragAction action,
2206 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2207 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2210 xev.xclient.type = ClientMessage;
2211 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2212 xev.xclient.format = 32;
2213 xev.xclient.window = private->drop_xid ?
2215 GDK_DRAWABLE_XID (context->dest_window);
2216 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2217 xev.xclient.data.l[1] = 0;
2218 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2219 xev.xclient.data.l[3] = time;
2220 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2222 if (!xdnd_send_xevent (display,
2223 GDK_DRAWABLE_XID (context->dest_window),
2227 g_message ("Send event to %lx failed",
2228 GDK_DRAWABLE_XID (context->dest_window)));
2229 gdk_window_unref (context->dest_window);
2230 context->dest_window = NULL;
2232 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2236 xdnd_check_dest (GdkDisplay *display,
2239 gboolean retval = FALSE;
2242 unsigned long nitems, after;
2246 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2247 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2251 gdk_error_trap_push ();
2253 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2255 1, False, AnyPropertyType,
2256 &type, &format, &nitems, &after,
2257 (guchar **)&proxy_data) == Success)
2261 if ((format == 32) && (nitems == 1))
2263 proxy = *proxy_data;
2267 g_warning ("Invalid XdndOwner "
2268 "property on window %ld\n", win));
2273 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2275 1, False, AnyPropertyType,
2276 &type, &format, &nitems, &after,
2277 (guchar **)&version) == Success) &&
2280 if ((format == 32) && (nitems == 1))
2287 g_warning ("Invalid XdndAware property on window %ld\n", win));
2293 gdk_error_trap_pop ();
2295 return retval ? (proxy ? proxy : win) : None;
2301 xdnd_read_actions (GdkDragContext *context)
2303 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2306 gulong nitems, after;
2311 /* Get the XdndActionList, if set */
2313 gdk_error_trap_push ();
2315 if (XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2316 GDK_DRAWABLE_XID (context->source_window),
2317 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2319 False, XA_ATOM, &type, &format, &nitems,
2320 &after, (guchar **)&data) == Success &&
2323 context->actions = 0;
2325 for (i=0; i<nitems; i++)
2326 context->actions |= xdnd_action_from_atom (display, data[i]);
2328 (PRIVATE_DATA (context))->xdnd_have_actions = TRUE;
2330 #ifdef G_ENABLE_DEBUG
2331 if (_gdk_debug_flags & GDK_DEBUG_DND)
2333 GString *action_str = g_string_new (NULL);
2334 if (context->actions & GDK_ACTION_MOVE)
2335 g_string_append(action_str, "MOVE ");
2336 if (context->actions & GDK_ACTION_COPY)
2337 g_string_append(action_str, "COPY ");
2338 if (context->actions & GDK_ACTION_LINK)
2339 g_string_append(action_str, "LINK ");
2340 if (context->actions & GDK_ACTION_ASK)
2341 g_string_append(action_str, "ASK ");
2343 g_message("Xdnd actions = %s", action_str->str);
2344 g_string_free (action_str, TRUE);
2346 #endif /* G_ENABLE_DEBUG */
2351 gdk_error_trap_pop ();
2354 /* We have to make sure that the XdndActionList we keep internally
2355 * is up to date with the XdndActionList on the source window
2356 * because we get no notification, because Xdnd wasn't meant
2357 * to continually send actions. So we select on PropertyChangeMask
2358 * and add this filter.
2360 static GdkFilterReturn
2361 xdnd_source_window_filter (GdkXEvent *xev,
2365 XEvent *xevent = (XEvent *)xev;
2366 GdkDragContext *context = cb_data;
2367 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2369 if ((xevent->xany.type == PropertyNotify) &&
2370 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2372 xdnd_read_actions (context);
2374 return GDK_FILTER_REMOVE;
2377 return GDK_FILTER_CONTINUE;
2381 xdnd_manage_source_filter (GdkDragContext *context,
2383 gboolean add_filter)
2385 gdk_error_trap_push ();
2387 if (!GDK_WINDOW_DESTROYED (window))
2391 gdk_window_set_events (window,
2392 gdk_window_get_events (window) |
2393 GDK_PROPERTY_CHANGE_MASK);
2394 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2399 gdk_window_remove_filter (window,
2400 xdnd_source_window_filter,
2402 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2403 * but we might want it for other reasons. (Like
2404 * INCR selection transactions).
2409 gdk_display_sync (gdk_drawable_get_display (window));
2410 gdk_error_trap_pop ();
2413 static GdkFilterReturn
2414 xdnd_enter_filter (GdkXEvent *xev,
2418 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (event->any.window);
2419 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2420 XEvent *xevent = (XEvent *)xev;
2421 GdkDragContext *new_context;
2426 gulong nitems, after;
2429 guint32 source_window = xevent->xclient.data.l[0];
2430 gboolean get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2431 gint version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2434 g_message ("XdndEnter: source_window: %#x, version: %#x",
2435 source_window, version));
2439 /* Old source ignore */
2440 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2441 return GDK_FILTER_REMOVE;
2444 if (display_x11->current_dest_drag != NULL)
2446 gdk_drag_context_unref (display_x11->current_dest_drag);
2447 display_x11->current_dest_drag = NULL;
2450 new_context = gdk_drag_context_new ();
2451 new_context->protocol = GDK_DRAG_PROTO_XDND;
2452 new_context->is_source = FALSE;
2454 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2455 if (new_context->source_window)
2456 gdk_window_ref (new_context->source_window);
2459 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2460 if (!new_context->source_window)
2462 gdk_drag_context_unref (new_context);
2463 return GDK_FILTER_REMOVE;
2466 new_context->dest_window = event->any.window;
2467 gdk_window_ref (new_context->dest_window);
2469 new_context->targets = NULL;
2472 gdk_error_trap_push ();
2473 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2475 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2477 False, XA_ATOM, &type, &format, &nitems,
2478 &after, (guchar **)&data);
2480 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2482 gdk_drag_context_unref (new_context);
2483 return GDK_FILTER_REMOVE;
2486 for (i=0; i<nitems; i++)
2487 new_context->targets =
2488 g_list_append (new_context->targets,
2489 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2497 if (xevent->xclient.data.l[2+i])
2498 new_context->targets =
2499 g_list_append (new_context->targets,
2500 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2501 xevent->xclient.data.l[2+i])));
2504 #ifdef G_ENABLE_DEBUG
2505 if (_gdk_debug_flags & GDK_DEBUG_DND)
2506 print_target_list (new_context->targets);
2507 #endif /* G_ENABLE_DEBUG */
2509 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2510 xdnd_read_actions (new_context);
2512 event->dnd.type = GDK_DRAG_ENTER;
2513 event->dnd.context = new_context;
2514 gdk_drag_context_ref (new_context);
2516 display_x11->current_dest_drag = new_context;
2518 (PRIVATE_DATA (new_context))->xdnd_selection =
2519 gdk_x11_get_xatom_by_name_for_display (display, "XdndSelection");
2520 return GDK_FILTER_TRANSLATE;
2523 static GdkFilterReturn
2524 xdnd_leave_filter (GdkXEvent *xev,
2528 XEvent *xevent = (XEvent *)xev;
2529 guint32 source_window = xevent->xclient.data.l[0];
2530 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
2533 g_message ("XdndLeave: source_window: %#x",
2536 if ((display_x11->current_dest_drag != NULL) &&
2537 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2538 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2540 event->dnd.type = GDK_DRAG_LEAVE;
2541 /* Pass ownership of context to the event */
2542 event->dnd.context = display_x11->current_dest_drag;
2544 display_x11->current_dest_drag = NULL;
2546 return GDK_FILTER_TRANSLATE;
2549 return GDK_FILTER_REMOVE;
2552 static GdkFilterReturn
2553 xdnd_position_filter (GdkXEvent *xev,
2557 XEvent *xevent = (XEvent *)xev;
2558 guint32 source_window = xevent->xclient.data.l[0];
2559 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2560 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2561 guint32 time = xevent->xclient.data.l[3];
2562 Atom action = xevent->xclient.data.l[4];
2564 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (event->any.window);
2565 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2568 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2569 source_window, x_root, y_root, time, action));
2572 if ((display_x11->current_dest_drag != NULL) &&
2573 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2574 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2576 event->dnd.type = GDK_DRAG_MOTION;
2577 event->dnd.context = display_x11->current_dest_drag;
2578 gdk_drag_context_ref (display_x11->current_dest_drag);
2580 event->dnd.time = time;
2582 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2584 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2585 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2587 event->dnd.x_root = x_root;
2588 event->dnd.y_root = y_root;
2590 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2591 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
2593 return GDK_FILTER_TRANSLATE;
2596 return GDK_FILTER_REMOVE;
2599 static GdkFilterReturn
2600 xdnd_drop_filter (GdkXEvent *xev,
2604 XEvent *xevent = (XEvent *)xev;
2605 guint32 source_window = xevent->xclient.data.l[0];
2606 guint32 time = xevent->xclient.data.l[2];
2607 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
2610 g_message ("XdndDrop: source_window: %#x time: %d",
2611 source_window, time));
2613 if ((display_x11->current_dest_drag != NULL) &&
2614 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2615 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2617 GdkDragContextPrivateX11 *private;
2618 private = PRIVATE_DATA (display_x11->current_dest_drag);
2620 event->dnd.type = GDK_DROP_START;
2622 event->dnd.context = display_x11->current_dest_drag;
2623 gdk_drag_context_ref (display_x11->current_dest_drag);
2625 event->dnd.time = time;
2626 event->dnd.x_root = private->last_x;
2627 event->dnd.y_root = private->last_y;
2629 return GDK_FILTER_TRANSLATE;
2632 return GDK_FILTER_REMOVE;
2635 /*************************************************************
2636 ************************** Public API ***********************
2637 *************************************************************/
2639 _gdk_dnd_init (GdkDisplay *display)
2643 gdk_display_add_client_message_filter (
2645 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2646 motif_dnd_filter, NULL);
2647 gdk_display_add_client_message_filter (
2649 gdk_atom_intern ("XdndEnter", FALSE),
2650 xdnd_enter_filter, NULL);
2651 gdk_display_add_client_message_filter (
2653 gdk_atom_intern ("XdndLeave", FALSE),
2654 xdnd_leave_filter, NULL);
2655 gdk_display_add_client_message_filter (
2657 gdk_atom_intern ("XdndPosition", FALSE),
2658 xdnd_position_filter, NULL);
2659 gdk_display_add_client_message_filter (
2661 gdk_atom_intern ("XdndStatus", FALSE),
2662 xdnd_status_filter, NULL);
2663 gdk_display_add_client_message_filter (
2665 gdk_atom_intern ("XdndFinished", FALSE),
2666 xdnd_finished_filter, NULL);
2667 gdk_display_add_client_message_filter (
2669 gdk_atom_intern ("XdndDrop", FALSE),
2670 xdnd_drop_filter, NULL);
2676 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2678 if (context->dest_window)
2680 switch (context->protocol)
2682 case GDK_DRAG_PROTO_MOTIF:
2683 motif_send_leave (context, time);
2685 case GDK_DRAG_PROTO_XDND:
2686 xdnd_send_leave (context);
2688 case GDK_DRAG_PROTO_ROOTWIN:
2689 case GDK_DRAG_PROTO_NONE:
2694 gdk_window_unref (context->dest_window);
2695 context->dest_window = NULL;
2700 gdk_drag_begin (GdkWindow *window,
2703 GdkDragContext *new_context;
2705 g_return_val_if_fail (window != NULL, NULL);
2707 new_context = gdk_drag_context_new ();
2708 new_context->is_source = TRUE;
2709 new_context->source_window = window;
2710 gdk_window_ref (window);
2712 new_context->targets = g_list_copy (targets);
2713 new_context->actions = 0;
2719 gdk_drag_get_protocol_for_display (GdkDisplay *display,
2721 GdkDragProtocol *protocol)
2724 g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
2726 if ((retval = xdnd_check_dest (display, xid)))
2728 *protocol = GDK_DRAG_PROTO_XDND;
2729 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2732 else if ((retval = motif_check_dest (display, xid)))
2734 *protocol = GDK_DRAG_PROTO_MOTIF;
2735 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2740 /* Check if this is a root window */
2742 gboolean rootwin = FALSE;
2745 unsigned long nitems, after;
2746 unsigned char *data;
2748 if (_gdk_x11_display_is_root_window (display, (Window) xid))
2751 gdk_error_trap_push ();
2755 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
2756 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
2757 0, 0, False, AnyPropertyType,
2758 &type, &format, &nitems, &after, &data) == Success &&
2766 /* Until I find out what window manager the next one is for,
2767 * I'm leaving it commented out. It's supported in the
2768 * xscreensaver sources, though.
2773 if (XGetWindowProperty (gdk_display, win,
2774 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
2775 0, 0, False, AnyPropertyType,
2776 &type, &format, &nitems, &data) &&
2782 gdk_error_trap_pop ();
2786 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2791 *protocol = GDK_DRAG_PROTO_NONE;
2796 gdk_drag_get_protocol (guint32 xid,
2797 GdkDragProtocol *protocol)
2799 return gdk_drag_get_protocol_for_display (gdk_get_default_display (), xid, protocol);
2803 gdk_drag_find_window (GdkDragContext *context,
2804 GdkWindow *drag_window,
2807 GdkWindow **dest_window,
2808 GdkDragProtocol *protocol)
2810 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2811 GdkDisplay *display;
2814 g_return_if_fail (context != NULL);
2816 display = GDK_WINDOW_DISPLAY (context->source_window);
2818 if (!private->window_cache)
2819 private->window_cache = gdk_window_cache_new (gdk_drawable_get_screen (context->source_window));
2821 dest = get_client_window_at_coords (private->window_cache,
2823 GDK_DRAWABLE_XID (drag_window) : None,
2826 if (private->dest_xid != dest)
2829 private->dest_xid = dest;
2831 /* Check if new destination accepts drags, and which protocol */
2833 /* There is some ugliness here. We actually need to pass
2834 * _three_ pieces of information to drag_motion - dest_window,
2835 * protocol, and the XID of the unproxied window. The first
2836 * two are passed explicitely, the third implicitly through
2837 * protocol->dest_xid.
2839 if ((recipient = gdk_drag_get_protocol_for_display (display, dest, protocol)))
2841 *dest_window = gdk_window_lookup_for_display (display, recipient);
2843 gdk_window_ref (*dest_window);
2845 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
2848 *dest_window = NULL;
2852 *dest_window = context->dest_window;
2854 gdk_window_ref (*dest_window);
2855 *protocol = context->protocol;
2860 gdk_drag_motion (GdkDragContext *context,
2861 GdkWindow *dest_window,
2862 GdkDragProtocol protocol,
2865 GdkDragAction suggested_action,
2866 GdkDragAction possible_actions,
2869 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2871 g_return_val_if_fail (context != NULL, FALSE);
2873 /* When we have a Xdnd target, make sure our XdndActionList
2874 * matches the current actions;
2876 private->old_actions = context->actions;
2877 context->actions = possible_actions;
2879 if ((protocol == GDK_DRAG_PROTO_XDND) &&
2880 (!private->xdnd_actions_set ||
2881 private->xdnd_actions != possible_actions))
2882 xdnd_set_actions (context);
2884 if (context->dest_window != dest_window)
2886 GdkEvent temp_event;
2888 /* Send a leave to the last destination */
2889 gdk_drag_do_leave (context, time);
2890 private->drag_status = GDK_DRAG_STATUS_DRAG;
2892 /* Check if new destination accepts drags, and which protocol */
2896 context->dest_window = dest_window;
2897 private->drop_xid = private->dest_xid;
2898 gdk_window_ref (context->dest_window);
2899 context->protocol = protocol;
2903 case GDK_DRAG_PROTO_MOTIF:
2904 motif_send_enter (context, time);
2907 case GDK_DRAG_PROTO_XDND:
2908 xdnd_send_enter (context);
2911 case GDK_DRAG_PROTO_ROOTWIN:
2912 case GDK_DRAG_PROTO_NONE:
2916 private->old_action = suggested_action;
2917 context->suggested_action = suggested_action;
2918 private->old_actions = possible_actions;
2922 context->dest_window = NULL;
2923 private->drop_xid = None;
2924 context->action = 0;
2927 /* Push a status event, to let the client know that
2931 temp_event.dnd.type = GDK_DRAG_STATUS;
2932 temp_event.dnd.window = context->source_window;
2933 /* We use this to signal a synthetic status. Perhaps
2934 * we should use an extra field...
2936 temp_event.dnd.send_event = TRUE;
2938 temp_event.dnd.context = context;
2939 temp_event.dnd.time = time;
2941 gdk_event_put (&temp_event);
2945 private->old_action = context->suggested_action;
2946 context->suggested_action = suggested_action;
2949 /* Send a drag-motion event */
2951 private->last_x = x_root;
2952 private->last_y = y_root;
2954 if (context->dest_window)
2956 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
2958 switch (context->protocol)
2960 case GDK_DRAG_PROTO_MOTIF:
2961 motif_send_motion (context, x_root, y_root, suggested_action, time);
2964 case GDK_DRAG_PROTO_XDND:
2965 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
2968 case GDK_DRAG_PROTO_ROOTWIN:
2970 GdkEvent temp_event;
2972 if (g_list_find (context->targets,
2973 GDK_ATOM_TO_POINTER (gdk_atom_intern ("application/x-rootwin-drop", FALSE))))
2974 context->action = context->suggested_action;
2976 context->action = 0;
2978 temp_event.dnd.type = GDK_DRAG_STATUS;
2979 temp_event.dnd.window = context->source_window;
2980 temp_event.dnd.send_event = FALSE;
2981 temp_event.dnd.context = context;
2982 temp_event.dnd.time = time;
2984 gdk_event_put (&temp_event);
2987 case GDK_DRAG_PROTO_NONE:
2988 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3002 gdk_drag_drop (GdkDragContext *context,
3005 g_return_if_fail (context != NULL);
3007 if (context->dest_window)
3009 switch (context->protocol)
3011 case GDK_DRAG_PROTO_MOTIF:
3012 motif_send_leave (context, time);
3013 motif_send_drop (context, time);
3016 case GDK_DRAG_PROTO_XDND:
3017 xdnd_send_drop (context, time);
3020 case GDK_DRAG_PROTO_ROOTWIN:
3021 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3023 case GDK_DRAG_PROTO_NONE:
3024 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3033 gdk_drag_abort (GdkDragContext *context,
3036 g_return_if_fail (context != NULL);
3038 gdk_drag_do_leave (context, time);
3041 /* Destination side */
3044 gdk_drag_status (GdkDragContext *context,
3045 GdkDragAction action,
3048 GdkDragContextPrivateX11 *private;
3050 GdkDisplay *display;
3052 g_return_if_fail (context != NULL);
3054 private = PRIVATE_DATA (context);
3055 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3057 context->action = action;
3059 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3061 xev.xclient.type = ClientMessage;
3062 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3063 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3064 xev.xclient.format = 8;
3065 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3067 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3069 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3073 if ((action != 0) != (private->old_action != 0))
3076 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3078 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3081 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3084 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3088 case GDK_ACTION_MOVE:
3089 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3091 case GDK_ACTION_COPY:
3092 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3094 case GDK_ACTION_LINK:
3095 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3098 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3103 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3105 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3107 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3108 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3109 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3111 if (!_gdk_send_xevent (display,
3112 GDK_DRAWABLE_XID (context->source_window),
3115 g_message ("Send event to %lx failed",
3116 GDK_DRAWABLE_XID (context->source_window)));
3118 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3120 xev.xclient.type = ClientMessage;
3121 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3122 xev.xclient.format = 32;
3123 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3125 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3126 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3127 xev.xclient.data.l[2] = 0;
3128 xev.xclient.data.l[3] = 0;
3129 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3131 if (!xdnd_send_xevent (display,
3132 GDK_DRAWABLE_XID (context->source_window),
3135 g_message ("Send event to %lx failed",
3136 GDK_DRAWABLE_XID (context->source_window)));
3139 private->old_action = action;
3143 gdk_drop_reply (GdkDragContext *context,
3147 GdkDragContextPrivateX11 *private;
3149 g_return_if_fail (context != NULL);
3151 private = PRIVATE_DATA (context);
3153 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3155 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3158 xev.xclient.type = ClientMessage;
3159 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3160 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3161 xev.xclient.format = 8;
3163 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3164 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3166 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3167 (XmDROP_SITE_VALID << 4) |
3168 (XmDROP_NOOP << 8) |
3171 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3172 (XmNO_DROP_SITE << 4) |
3173 (XmDROP_NOOP << 8) |
3174 (XmDROP_CANCEL << 12);
3175 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3176 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3178 _gdk_send_xevent (display,
3179 GDK_DRAWABLE_XID (context->source_window),
3185 gdk_drop_finish (GdkDragContext *context,
3189 g_return_if_fail (context != NULL);
3191 if (context->protocol == GDK_DRAG_PROTO_XDND)
3193 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3196 xev.xclient.type = ClientMessage;
3197 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3198 xev.xclient.format = 32;
3199 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3201 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3202 xev.xclient.data.l[1] = 0;
3203 xev.xclient.data.l[2] = 0;
3204 xev.xclient.data.l[3] = 0;
3205 xev.xclient.data.l[4] = 0;
3207 if (!xdnd_send_xevent (display,
3208 GDK_DRAWABLE_XID (context->source_window),
3211 g_message ("Send event to %lx failed",
3212 GDK_DRAWABLE_XID (context->source_window)));
3218 gdk_window_register_dnd (GdkWindow *window)
3220 static gulong xdnd_version = 3;
3221 MotifDragReceiverInfo info;
3222 Atom motif_drag_receiver_info_atom;
3223 GdkDisplay *display = gdk_drawable_get_display (window);
3225 g_return_if_fail (window != NULL);
3227 if (GPOINTER_TO_INT (gdk_drawable_get_data (window, "gdk-dnd-registered")))
3230 gdk_drawable_set_data (window, "gdk-dnd-registered", GINT_TO_POINTER(TRUE), NULL);
3232 /* Set Motif drag receiver information property */
3234 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3235 "_MOTIF_DRAG_RECEIVER_INFO");
3236 info.byte_order = local_byte_order;
3237 info.protocol_version = 0;
3238 info.protocol_style = XmDRAG_DYNAMIC;
3239 info.proxy_window = None;
3240 info.num_drop_sites = 0;
3241 info.total_size = sizeof(info);
3243 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3244 motif_drag_receiver_info_atom,
3245 motif_drag_receiver_info_atom,
3252 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3253 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3254 GDK_DRAWABLE_XID (window),
3255 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3256 XA_ATOM, 32, PropModeReplace,
3257 (guchar *)&xdnd_version, 1);
3260 /*************************************************************
3261 * gdk_drag_get_selection:
3262 * Returns the selection atom for the current source window
3266 *************************************************************/
3269 gdk_drag_get_selection (GdkDragContext *context)
3271 g_return_val_if_fail (context != NULL, GDK_NONE);
3273 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3274 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3275 (PRIVATE_DATA (context))->motif_selection);
3276 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3277 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3278 (PRIVATE_DATA (context))->xdnd_selection);