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 GSList *window_caches;
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);
193 g_list_free (context->targets);
195 if (context->source_window)
197 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
199 xdnd_manage_source_filter (context, context->source_window, FALSE);
201 g_object_unref (context->source_window);
204 if (context->dest_window)
205 g_object_unref (context->dest_window);
207 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
208 gdk_window_cache_destroy (tmp_list->data);
209 g_slist_free (private->window_caches);
211 contexts = g_list_remove (contexts, context);
215 G_OBJECT_CLASS (parent_class)->finalize (object);
221 gdk_drag_context_new (void)
223 return g_object_new (gdk_drag_context_get_type (), NULL);
227 gdk_drag_context_ref (GdkDragContext *context)
229 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
231 g_object_ref (context);
235 gdk_drag_context_unref (GdkDragContext *context)
237 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
239 g_object_unref (context);
242 static GdkDragContext *
243 gdk_drag_context_find (GdkDisplay *display,
248 GList *tmp_list = contexts;
249 GdkDragContext *context;
250 GdkDragContextPrivateX11 *private;
251 Window context_dest_xid;
255 context = (GdkDragContext *)tmp_list->data;
256 private = PRIVATE_DATA (context);
258 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
259 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
262 context_dest_xid = context->dest_window ?
265 GDK_DRAWABLE_XID (context->dest_window)) :
268 if ((!context->is_source == !is_source) &&
269 ((source_xid == None) || (context->source_window &&
270 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
271 ((dest_xid == None) || (context_dest_xid == dest_xid)))
274 tmp_list = tmp_list->next;
280 /* Utility functions */
283 gdk_window_cache_add (GdkWindowCache *cache,
285 gint x, gint y, gint width, gint height,
288 GdkCacheChild *child = g_new (GdkCacheChild, 1);
293 child->width = width;
294 child->height = height;
295 child->mapped = mapped;
297 cache->children = g_list_prepend (cache->children, child);
298 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
302 static GdkFilterReturn
303 gdk_window_cache_filter (GdkXEvent *xev,
307 XEvent *xevent = (XEvent *)xev;
308 GdkWindowCache *cache = data;
310 switch (xevent->type)
312 case CirculateNotify:
314 case ConfigureNotify:
316 XConfigureEvent *xce = &xevent->xconfigure;
319 node = g_hash_table_lookup (cache->child_hash,
320 GUINT_TO_POINTER (xce->window));
323 GdkCacheChild *child = node->data;
326 child->width = xce->width;
327 child->height = xce->height;
328 if (xce->above == None && (node->next))
330 GList *last = g_list_last (cache->children);
331 cache->children = g_list_remove_link (cache->children, node);
338 GList *above_node = g_hash_table_lookup (cache->child_hash,
339 GUINT_TO_POINTER (xce->above));
340 if (above_node && node->prev != above_node)
342 /* Put the window above (before in the list) above_node
344 cache->children = g_list_remove_link (cache->children, node);
345 node->prev = above_node->prev;
347 node->prev->next = node;
349 cache->children = node;
350 node->next = above_node;
351 above_node->prev = node;
359 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
361 if (!g_hash_table_lookup (cache->child_hash,
362 GUINT_TO_POINTER (xcwe->window)))
363 gdk_window_cache_add (cache, xcwe->window,
364 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
370 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
373 node = g_hash_table_lookup (cache->child_hash,
374 GUINT_TO_POINTER (xdwe->window));
377 g_hash_table_remove (cache->child_hash,
378 GUINT_TO_POINTER (xdwe->window));
379 cache->children = g_list_remove_link (cache->children, node);
381 g_list_free_1 (node);
387 XMapEvent *xme = &xevent->xmap;
390 node = g_hash_table_lookup (cache->child_hash,
391 GUINT_TO_POINTER (xme->window));
394 GdkCacheChild *child = node->data;
395 child->mapped = TRUE;
403 XMapEvent *xume = &xevent->xmap;
406 node = g_hash_table_lookup (cache->child_hash,
407 GUINT_TO_POINTER (xume->window));
410 GdkCacheChild *child = node->data;
411 child->mapped = FALSE;
416 return GDK_FILTER_CONTINUE;
418 return GDK_FILTER_REMOVE;
421 static GdkWindowCache *
422 gdk_window_cache_new (GdkScreen *screen)
424 XWindowAttributes xwa;
425 Window root, parent, *children;
426 unsigned int nchildren;
428 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
429 GdkWindow *root_window = gdk_screen_get_root_window (screen);
431 GdkWindowCache *result = g_new (GdkWindowCache, 1);
433 result->children = NULL;
434 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
435 result->screen = screen;
437 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
438 result->old_event_mask = xwa.your_event_mask;
439 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
440 result->old_event_mask | SubstructureNotifyMask);
441 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
443 gdk_error_trap_push ();
445 if (!XQueryTree(xdisplay, GDK_WINDOW_XWINDOW (root_window),
446 &root, &parent, &children, &nchildren))
448 gdk_error_trap_pop ();
452 for (i = 0; i < nchildren ; i++)
454 if (XGetWindowAttributes (xdisplay, children[i], &xwa))
455 gdk_window_cache_add (result, children[i],
456 xwa.x, xwa.y, xwa.width, xwa.height,
457 xwa.map_state != IsUnmapped);
461 gdk_error_trap_pop ();
467 gdk_window_cache_destroy (GdkWindowCache *cache)
469 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
471 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
472 GDK_WINDOW_XWINDOW (root_window),
473 cache->old_event_mask);
474 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
476 g_list_foreach (cache->children, (GFunc)g_free, NULL);
477 g_list_free (cache->children);
478 g_hash_table_destroy (cache->child_hash);
484 get_client_window_at_coords_recurse (GdkDisplay *display,
489 Window root, tmp_parent, *children;
490 unsigned int nchildren;
495 unsigned long nitems, after;
498 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(display), win,
499 gdk_x11_get_xatom_by_name_for_display (display, "WM_STATE"),
500 0, 0, False, AnyPropertyType,
501 &type, &format, &nitems, &after, &data) != Success)
511 /* This is beautiful! Damn Enlightenment and click-to-focus */
512 if (!XTranslateCoordinates (gdk_display, _gdk_root_window, win,
513 x_root, y_root, &dest_x, &dest_y, &child))
517 if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), win,
518 &root, &tmp_parent, &children, &nchildren))
521 for (i = nchildren - 1; (i >= 0) && (child == None); i--)
523 XWindowAttributes xwa;
525 if (XGetWindowAttributes (GDK_DISPLAY_XDISPLAY (display),
528 if ((xwa.map_state == IsViewable) && (xwa.class == InputOutput) &&
529 (x >= xwa.x) && (x < xwa.x + (gint)xwa.width) &&
530 (y >= xwa.y) && (y < xwa.y + (gint)xwa.height))
543 return get_client_window_at_coords_recurse (display, child, x, y);
549 get_client_window_at_coords (GdkWindowCache *cache,
555 Window retval = None;
557 gdk_error_trap_push ();
559 tmp_list = cache->children;
561 while (tmp_list && !retval)
563 GdkCacheChild *child = tmp_list->data;
565 if ((child->xid != ignore) && (child->mapped))
567 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
568 (y_root >= child->y) && (y_root < child->y + child->height))
570 retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
579 tmp_list = tmp_list->next;
582 gdk_error_trap_pop ();
587 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
590 /*************************************************************
591 ***************************** MOTIF *************************
592 *************************************************************/
594 /* values used in the message type for Motif DND */
607 /* Values used to specify type of protocol to use */
611 XmDRAG_PREFER_PREREGISTER,
613 XmDRAG_PREFER_DYNAMIC,
615 XmDRAG_PREFER_RECEIVER
618 /* Operation codes */
626 /* Drop site status */
628 XmNO_DROP_SITE = 0x01,
629 XmDROP_SITE_INVALID = 0x02,
630 XmDROP_SITE_VALID = 0x03
633 /* completion status */
641 /* Byte swapping routines. The motif specification leaves it
642 * up to us to save a few bytes in the client messages
644 static gchar local_byte_order = '\0';
646 #ifdef G_ENABLE_DEBUG
648 print_target_list (GList *targets)
652 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
653 g_message ("\t%s", name);
655 targets = targets->next;
658 #endif /* G_ENABLE_DEBUG */
661 init_byte_order (void)
663 guint32 myint = 0x01020304;
664 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
668 card16_to_host (guint16 x, gchar byte_order) {
669 if (byte_order == local_byte_order)
672 return (x << 8) | (x >> 8);
676 card32_to_host (guint32 x, gchar byte_order) {
677 if (byte_order == local_byte_order)
680 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
683 /* Motif packs together fields of varying length into the
684 * client message. We can't rely on accessing these
685 * through data.s[], data.l[], etc, because on some architectures
686 * (i.e., Alpha) these won't be valid for format == 8.
689 #define MOTIF_XCLIENT_BYTE(xevent,i) \
690 (xevent)->xclient.data.b[i]
691 #define MOTIF_XCLIENT_SHORT(xevent,i) \
692 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
693 #define MOTIF_XCLIENT_LONG(xevent,i) \
694 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
696 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
697 #define MOTIF_UNPACK_SHORT(xevent,i) \
698 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
699 #define MOTIF_UNPACK_LONG(xevent,i) \
700 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
702 /***** Dest side ***********/
704 /* Property placed on source windows */
705 typedef struct _MotifDragInitiatorInfo {
707 guint8 protocol_version;
708 guint16 targets_index;
709 guint32 selection_atom;
710 } MotifDragInitiatorInfo;
712 /* Header for target table on the drag window */
713 typedef struct _MotifTargetTableHeader {
715 guchar protocol_version;
718 } MotifTargetTableHeader;
720 /* Property placed on target windows */
721 typedef struct _MotifDragReceiverInfo {
723 guint8 protocol_version;
724 guint8 protocol_style;
726 guint32 proxy_window;
727 guint16 num_drop_sites;
730 } MotifDragReceiverInfo;
732 /* Target table handling */
734 static GdkFilterReturn
735 motif_drag_window_filter (GdkXEvent *xevent,
739 XEvent *xev = (XEvent *)xevent;
740 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
741 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
743 switch (xev->xany.type)
746 display_x11->motif_drag_window = None;
747 display_x11->motif_drag_gdk_window = NULL;
750 if (display_x11->motif_target_lists &&
751 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
752 motif_read_target_table (display);
755 return GDK_FILTER_REMOVE;
759 motif_lookup_drag_window (GdkDisplay *display,
760 Display *lookup_xdisplay)
762 Window retval = None;
763 gulong bytes_after, nitems;
768 XGetWindowProperty (lookup_xdisplay, DefaultRootWindow (lookup_xdisplay),
769 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
771 XA_WINDOW, &type, &format, &nitems, &bytes_after,
774 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
776 retval = *(Window *)data;
778 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
787 /* Finds the window where global Motif drag information is stored.
788 * If it doesn't exist and 'create' is TRUE, create one.
791 motif_find_drag_window (GdkDisplay *display,
794 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
796 if (!display_x11->motif_drag_window)
798 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
799 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
801 if (!display_x11->motif_drag_window && create)
803 /* Create a persistant window. (Copied from LessTif) */
805 Display *persistant_xdisplay;
806 XSetWindowAttributes attr;
807 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
808 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
810 XGrabServer (persistant_xdisplay);
812 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
814 if (!display_x11->motif_drag_window)
816 attr.override_redirect = True;
817 attr.event_mask = PropertyChangeMask;
819 display_x11->motif_drag_window =
820 XCreateWindow (persistant_xdisplay,
821 DefaultRootWindow (persistant_xdisplay),
822 -100, -100, 10, 10, 0, 0,
823 InputOnly, CopyFromParent,
824 (CWOverrideRedirect | CWEventMask), &attr);
827 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
829 XChangeProperty (persistant_xdisplay,
830 DefaultRootWindow (persistant_xdisplay),
831 motif_drag_window_atom, XA_WINDOW,
833 (guchar *)&motif_drag_window_atom, 1);
836 XUngrabServer (persistant_xdisplay);
837 XCloseDisplay (persistant_xdisplay);
840 /* There is a miniscule race condition here if the drag window
841 * gets destroyed exactly now.
843 if (display_x11->motif_drag_window)
845 display_x11->motif_drag_gdk_window =
846 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
847 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
848 motif_drag_window_filter,
853 return display_x11->motif_drag_window;
857 motif_read_target_table (GdkDisplay *display)
859 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
860 gulong bytes_after, nitems;
865 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
867 if (display_x11->motif_target_lists)
869 for (i=0; i<display_x11->motif_n_target_lists; i++)
870 g_list_free (display_x11->motif_target_lists[i]);
872 g_free (display_x11->motif_target_lists);
873 display_x11->motif_target_lists = NULL;
874 display_x11->motif_n_target_lists = 0;
877 if (motif_find_drag_window (display, FALSE))
879 MotifTargetTableHeader *header = NULL;
880 guchar *target_bytes = NULL;
882 gboolean success = FALSE;
884 gdk_error_trap_push ();
885 XGetWindowProperty (display_x11->xdisplay,
886 display_x11->motif_drag_window,
887 motif_drag_targets_atom,
888 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
889 motif_drag_targets_atom,
890 &type, &format, &nitems, &bytes_after,
893 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
896 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
897 header->total_size = card32_to_host (header->total_size, header->byte_order);
899 gdk_error_trap_push ();
900 XGetWindowProperty (display_x11->xdisplay,
901 display_x11->motif_drag_window,
902 motif_drag_targets_atom,
903 (sizeof(MotifTargetTableHeader)+3)/4,
904 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
906 motif_drag_targets_atom, &type, &format, &nitems,
907 &bytes_after, &target_bytes);
909 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
910 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
913 display_x11->motif_n_target_lists = header->n_lists;
914 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
917 for (i=0; i<header->n_lists; i++)
922 if (p + sizeof(guint16) - target_bytes > nitems)
925 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
927 /* We need to make a copy of the targets, since it may
930 targets = g_new (guint32, n_targets);
931 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
933 p += sizeof(guint16) + n_targets * sizeof(guint32);
934 if (p - target_bytes > nitems)
937 for (j=0; j<n_targets; j++)
938 display_x11->motif_target_lists[i] =
939 g_list_prepend (display_x11->motif_target_lists[i],
940 GUINT_TO_POINTER (card32_to_host (targets[j],
941 header->byte_order)));
943 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
953 XFree (target_bytes);
957 if (display_x11->motif_target_lists)
959 g_free (display_x11->motif_target_lists);
960 display_x11->motif_target_lists = NULL;
961 display_x11->motif_n_target_lists = 0;
963 g_warning ("Error reading Motif target table\n");
969 targets_sort_func (gconstpointer a, gconstpointer b)
971 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
972 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
975 /* Check if given (sorted) list is in the targets table */
977 motif_target_table_check (GdkDisplay *display,
980 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
981 GList *tmp_list1, *tmp_list2;
984 for (i=0; i<display_x11->motif_n_target_lists; i++)
986 tmp_list1 = display_x11->motif_target_lists[i];
989 while (tmp_list1 && tmp_list2)
991 if (tmp_list1->data != tmp_list2->data)
994 tmp_list1 = tmp_list1->next;
995 tmp_list2 = tmp_list2->next;
997 if (!tmp_list1 && !tmp_list2) /* Found it */
1005 motif_add_to_target_table (GdkDisplay *display,
1006 GList *targets) /* targets is list of GdkAtom */
1008 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1009 GList *sorted = NULL;
1014 /* make a sorted copy of the list */
1018 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1019 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1020 targets = targets->next;
1023 /* First check if it is their already */
1025 if (display_x11->motif_target_lists)
1026 index = motif_target_table_check (display, sorted);
1028 /* We need to grab the server while doing this, to ensure
1034 /* We need to make sure that it exists _before_ we grab the
1035 * server, since we can't open a new connection after we
1038 motif_find_drag_window (display, TRUE);
1040 gdk_x11_display_grab (display);
1041 motif_read_target_table (display);
1043 /* Check again, in case it was added in the meantime */
1045 if (display_x11->motif_target_lists)
1046 index = motif_target_table_check (display, sorted);
1050 guint32 total_size = 0;
1054 MotifTargetTableHeader *header;
1056 if (!display_x11->motif_target_lists)
1058 display_x11->motif_target_lists = g_new (GList *, 1);
1059 display_x11->motif_n_target_lists = 1;
1063 display_x11->motif_n_target_lists++;
1064 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1065 sizeof(GList *) * display_x11->motif_n_target_lists);
1067 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1069 index = display_x11->motif_n_target_lists - 1;
1071 total_size = sizeof (MotifTargetTableHeader);
1072 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1073 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1075 data = g_malloc (total_size);
1077 header = (MotifTargetTableHeader *)data;
1078 p = data + sizeof(MotifTargetTableHeader);
1080 header->byte_order = local_byte_order;
1081 header->protocol_version = 0;
1082 header->n_lists = display_x11->motif_n_target_lists;
1083 header->total_size = total_size;
1085 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1087 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1088 guint32 *targets = g_new (guint32, n_targets);
1089 guint32 *p32 = targets;
1091 tmp_list = display_x11->motif_target_lists[i];
1094 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1096 tmp_list = tmp_list->next;
1101 p += sizeof(guint16);
1103 memcpy (p, targets, n_targets * sizeof(guint32));
1106 p += sizeof(guint32) * n_targets;
1110 XChangeProperty (display_x11->xdisplay,
1111 display_x11->motif_drag_window,
1112 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1113 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1117 gdk_x11_display_ungrab (display);
1120 g_list_free (sorted);
1124 /* Translate flags */
1127 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1129 guint recommended_op = flags & 0x000f;
1130 guint possible_ops = (flags & 0x0f0) >> 4;
1132 switch (recommended_op)
1135 context->suggested_action = GDK_ACTION_MOVE;
1138 context->suggested_action = GDK_ACTION_COPY;
1141 context->suggested_action = GDK_ACTION_LINK;
1144 context->suggested_action = GDK_ACTION_COPY;
1148 context->actions = 0;
1149 if (possible_ops & XmDROP_MOVE)
1150 context->actions |= GDK_ACTION_MOVE;
1151 if (possible_ops & XmDROP_COPY)
1152 context->actions |= GDK_ACTION_COPY;
1153 if (possible_ops & XmDROP_LINK)
1154 context->actions |= GDK_ACTION_LINK;
1158 motif_dnd_get_flags (GdkDragContext *context)
1162 switch (context->suggested_action)
1164 case GDK_ACTION_MOVE:
1165 flags = XmDROP_MOVE;
1167 case GDK_ACTION_COPY:
1168 flags = XmDROP_COPY;
1170 case GDK_ACTION_LINK:
1171 flags = XmDROP_LINK;
1174 flags = XmDROP_NOOP;
1178 if (context->actions & GDK_ACTION_MOVE)
1179 flags |= XmDROP_MOVE << 8;
1180 if (context->actions & GDK_ACTION_COPY)
1181 flags |= XmDROP_COPY << 8;
1182 if (context->actions & GDK_ACTION_LINK)
1183 flags |= XmDROP_LINK << 8;
1191 motif_set_targets (GdkDragContext *context)
1193 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1194 MotifDragInitiatorInfo info;
1196 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1198 info.byte_order = local_byte_order;
1199 info.protocol_version = 0;
1201 info.targets_index = motif_add_to_target_table (display, context->targets);
1206 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1208 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1209 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1213 info.selection_atom = private->motif_selection;
1215 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1216 GDK_DRAWABLE_XID (context->source_window),
1217 private->motif_selection,
1218 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1220 (guchar *)&info, sizeof (info));
1222 private->motif_targets_set = 1;
1226 motif_check_dest (GdkDisplay *display,
1229 gboolean retval = FALSE;
1230 MotifDragReceiverInfo *info;
1233 unsigned long nitems, after;
1234 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1236 gdk_error_trap_push ();
1237 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1238 motif_drag_receiver_info_atom,
1239 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1240 &type, &format, &nitems, &after,
1243 if (gdk_error_trap_pop() == 0)
1247 if ((format == 8) && (nitems == sizeof(*info)))
1249 if ((info->protocol_version == 0) &&
1250 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1251 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1252 (info->protocol_style == XmDRAG_DYNAMIC)))
1258 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1265 return retval ? win : None;
1269 motif_send_enter (GdkDragContext *context,
1272 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1273 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1276 xev.xclient.type = ClientMessage;
1277 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1278 xev.xclient.format = 8;
1279 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1281 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1282 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1283 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1284 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1285 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1287 if (!private->motif_targets_set)
1288 motif_set_targets (context);
1290 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1292 if (!_gdk_send_xevent (display,
1293 GDK_DRAWABLE_XID (context->dest_window),
1296 g_message ("Send event to %lx failed",
1297 GDK_DRAWABLE_XID (context->dest_window)));
1301 motif_send_leave (GdkDragContext *context,
1304 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1307 xev.xclient.type = ClientMessage;
1308 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1309 xev.xclient.format = 8;
1310 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1312 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1313 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1314 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1315 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1316 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1317 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1319 if (!_gdk_send_xevent (display,
1320 GDK_DRAWABLE_XID (context->dest_window),
1323 g_message ("Send event to %lx failed",
1324 GDK_DRAWABLE_XID (context->dest_window)));
1328 motif_send_motion (GdkDragContext *context,
1331 GdkDragAction action,
1334 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1335 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1339 xev.xclient.type = ClientMessage;
1340 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1341 xev.xclient.format = 8;
1342 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1344 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1345 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1346 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1348 if ((context->suggested_action != private->old_action) ||
1349 (context->actions != private->old_actions))
1351 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1353 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1358 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1360 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1361 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1363 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1367 if (!_gdk_send_xevent (display,
1368 GDK_DRAWABLE_XID (context->dest_window),
1371 g_message ("Send event to %lx failed",
1372 GDK_DRAWABLE_XID (context->dest_window)));
1378 motif_send_drop (GdkDragContext *context, guint32 time)
1380 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1381 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1384 xev.xclient.type = ClientMessage;
1385 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1386 xev.xclient.format = 8;
1387 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1389 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1390 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1391 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1392 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1394 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1395 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1397 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1398 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1400 if (!_gdk_send_xevent (display,
1401 GDK_DRAWABLE_XID (context->dest_window),
1404 g_message ("Send event to %lx failed",
1405 GDK_DRAWABLE_XID (context->dest_window)));
1411 motif_read_initiator_info (GdkDisplay *display,
1412 Window source_window,
1422 MotifDragInitiatorInfo *initiator_info;
1424 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1426 gdk_error_trap_push ();
1427 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1428 0, sizeof(*initiator_info), FALSE,
1429 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1430 &type, &format, &nitems, &bytes_after,
1431 (guchar **)&initiator_info);
1433 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1435 g_warning ("Error reading initiator info\n");
1439 motif_read_target_table (display);
1441 initiator_info->targets_index =
1442 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1443 initiator_info->selection_atom =
1444 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1446 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1448 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1449 XFree (initiator_info);
1450 return GDK_FILTER_REMOVE;
1453 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1458 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1459 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1460 tmp_list = tmp_list->prev;
1463 #ifdef G_ENABLE_DEBUG
1464 if (_gdk_debug_flags & GDK_DEBUG_DND)
1465 print_target_list (*targets);
1466 #endif /* G_ENABLE_DEBUG */
1468 *selection = initiator_info->selection_atom;
1470 XFree (initiator_info);
1475 static GdkDragContext *
1476 motif_drag_context_new (GdkWindow *dest_window,
1478 guint32 source_window,
1481 GdkDragContext *new_context;
1482 GdkDragContextPrivateX11 *private;
1483 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1484 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1486 /* FIXME, current_dest_drag really shouldn't be NULL'd
1487 * if we error below.
1489 if (display_x11->current_dest_drag != NULL)
1491 if (timestamp >= display_x11->current_dest_drag->start_time)
1493 g_object_unref (display_x11->current_dest_drag);
1494 display_x11->current_dest_drag = NULL;
1500 new_context = gdk_drag_context_new ();
1501 private = PRIVATE_DATA (new_context);
1503 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1504 new_context->is_source = FALSE;
1506 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1507 if (new_context->source_window)
1508 g_object_ref (new_context->source_window);
1511 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1512 if (!new_context->source_window)
1514 g_object_unref (new_context);
1519 new_context->dest_window = dest_window;
1520 g_object_ref (dest_window);
1521 new_context->start_time = timestamp;
1523 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1526 &new_context->targets,
1527 &private->motif_selection))
1529 g_object_unref (new_context);
1537 * The MOTIF drag protocol has no real provisions for distinguishing
1538 * multiple simultaneous drops. If the sources grab the pointer
1539 * when doing drags, that shouldn't happen, in any case. If it
1540 * does, we can't do much except hope for the best.
1543 static GdkFilterReturn
1544 motif_top_level_enter (GdkEvent *event,
1547 guint32 source_window,
1550 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1551 GdkDragContext *new_context;
1553 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1554 flags, timestamp, source_window, atom));
1556 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1558 return GDK_FILTER_REMOVE;
1560 event->dnd.type = GDK_DRAG_ENTER;
1561 event->dnd.context = new_context;
1562 g_object_ref (new_context);
1564 display_x11->current_dest_drag = new_context;
1566 return GDK_FILTER_TRANSLATE;
1569 static GdkFilterReturn
1570 motif_top_level_leave (GdkEvent *event,
1574 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1576 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1579 if ((display_x11->current_dest_drag != NULL) &&
1580 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1581 (timestamp >= display_x11->current_dest_drag->start_time))
1583 event->dnd.type = GDK_DRAG_LEAVE;
1584 /* Pass ownership of context to the event */
1585 event->dnd.context = display_x11->current_dest_drag;
1587 display_x11->current_dest_drag = NULL;
1589 return GDK_FILTER_TRANSLATE;
1592 return GDK_FILTER_REMOVE;
1595 static GdkFilterReturn
1596 motif_motion (GdkEvent *event,
1602 GdkDragContextPrivateX11 *private;
1603 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1605 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1606 flags, timestamp, x_root, y_root));
1608 if ((display_x11->current_dest_drag != NULL) &&
1609 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1610 (timestamp >= display_x11->current_dest_drag->start_time))
1612 private = PRIVATE_DATA (display_x11->current_dest_drag);
1614 event->dnd.type = GDK_DRAG_MOTION;
1615 event->dnd.context = display_x11->current_dest_drag;
1616 g_object_ref (display_x11->current_dest_drag);
1618 event->dnd.time = timestamp;
1620 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1622 event->dnd.x_root = x_root;
1623 event->dnd.y_root = y_root;
1625 private->last_x = x_root;
1626 private->last_y = y_root;
1628 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1630 return GDK_FILTER_TRANSLATE;
1633 return GDK_FILTER_REMOVE;
1636 static GdkFilterReturn
1637 motif_operation_changed (GdkEvent *event,
1641 GdkDragContextPrivateX11 *private;
1642 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1643 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1646 if ((display_x11->current_dest_drag != NULL) &&
1647 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1648 (timestamp >= display_x11->current_dest_drag->start_time))
1650 event->dnd.type = GDK_DRAG_MOTION;
1651 event->dnd.send_event = FALSE;
1652 event->dnd.context = display_x11->current_dest_drag;
1653 g_object_ref (display_x11->current_dest_drag);
1655 event->dnd.time = timestamp;
1656 private = PRIVATE_DATA (display_x11->current_dest_drag);
1658 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1660 event->dnd.x_root = private->last_x;
1661 event->dnd.y_root = private->last_y;
1663 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1665 return GDK_FILTER_TRANSLATE;
1668 return GDK_FILTER_REMOVE;
1671 static GdkFilterReturn
1672 motif_drop_start (GdkEvent *event,
1675 guint32 source_window,
1680 GdkDragContext *new_context;
1681 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1683 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1684 flags, timestamp, x_root, y_root, source_window, atom));
1686 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1688 return GDK_FILTER_REMOVE;
1690 motif_dnd_translate_flags (new_context, flags);
1692 event->dnd.type = GDK_DROP_START;
1693 event->dnd.context = new_context;
1694 event->dnd.time = timestamp;
1695 event->dnd.x_root = x_root;
1696 event->dnd.y_root = y_root;
1698 g_object_ref (new_context);
1699 display_x11->current_dest_drag = new_context;
1701 return GDK_FILTER_TRANSLATE;
1704 static GdkFilterReturn
1705 motif_drag_status (GdkEvent *event,
1709 GdkDragContext *context;
1710 GdkDisplay *display;
1713 g_message ("Motif status message: flags %x", flags));
1715 display = gdk_drawable_get_display (event->any.window);
1717 return GDK_FILTER_REMOVE;
1719 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1723 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1724 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1725 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1726 private->drag_status = GDK_DRAG_STATUS_DRAG;
1728 event->dnd.type = GDK_DRAG_STATUS;
1729 event->dnd.send_event = FALSE;
1730 event->dnd.context = context;
1731 g_object_ref (context);
1733 event->dnd.time = timestamp;
1735 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1737 switch (flags & 0x000f)
1740 context->action = 0;
1743 context->action = GDK_ACTION_MOVE;
1746 context->action = GDK_ACTION_COPY;
1749 context->action = GDK_ACTION_LINK;
1754 context->action = 0;
1756 return GDK_FILTER_TRANSLATE;
1758 return GDK_FILTER_REMOVE;
1761 static GdkFilterReturn
1762 motif_dnd_filter (GdkXEvent *xev,
1766 XEvent *xevent = (XEvent *)xev;
1771 guint32 source_window;
1773 gint16 x_root, y_root;
1776 if (!event->any.window ||
1777 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1778 return GDK_FILTER_CONTINUE; /* Not for us */
1780 /* First read some fields common to all Motif DND messages */
1782 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1783 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1784 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1786 is_reply = ((reason & 0x80) != 0);
1788 switch (reason & 0x7f)
1790 case XmTOP_LEVEL_ENTER:
1791 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1792 atom = MOTIF_UNPACK_LONG (xevent, 3);
1793 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1794 case XmTOP_LEVEL_LEAVE:
1795 return motif_top_level_leave (event, flags, timestamp);
1798 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1799 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1802 return motif_motion (event, flags, timestamp, x_root, y_root);
1804 return motif_drag_status (event, flags, timestamp);
1806 case XmDROP_SITE_ENTER:
1807 return motif_drag_status (event, flags, timestamp);
1809 case XmDROP_SITE_LEAVE:
1810 return motif_drag_status (event,
1811 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1814 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1815 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1816 atom = MOTIF_UNPACK_LONG (xevent, 3);
1817 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1820 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1823 case XmOPERATION_CHANGED:
1825 return motif_operation_changed (event, flags, timestamp);
1827 return motif_drag_status (event, flags, timestamp);
1830 /* To the best of my knowledge, these next two messages are
1831 * not part of the protocol, though they are defined in
1835 case XmDRAG_DROP_FINISH:
1839 return GDK_FILTER_REMOVE;
1842 /*************************************************************
1843 ***************************** XDND **************************
1844 *************************************************************/
1846 /* Utility functions */
1851 GdkDragAction action;
1852 } xdnd_actions_table[] = {
1853 { "XdndActionCopy", None, GDK_ACTION_COPY },
1854 { "XdndActionMove", None, GDK_ACTION_MOVE },
1855 { "XdndActionLink", None, GDK_ACTION_LINK },
1856 { "XdndActionAsk", None, GDK_ACTION_ASK },
1857 { "XdndActionPrivate", None, GDK_ACTION_COPY },
1860 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1861 static gboolean xdnd_actions_initialized = FALSE;
1864 xdnd_initialize_actions (void)
1868 xdnd_actions_initialized = TRUE;
1869 for (i=0; i < xdnd_n_actions; i++)
1870 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1873 static GdkDragAction
1874 xdnd_action_from_atom (GdkDisplay *display,
1877 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
1880 if (!xdnd_actions_initialized)
1881 xdnd_initialize_actions();
1883 for (i=0; i<xdnd_n_actions; i++)
1884 if (atom == xdnd_actions_table[i].atom)
1885 return xdnd_actions_table[i].action;
1891 xdnd_action_to_atom (GdkDisplay *display,
1892 GdkDragAction action)
1896 if (!xdnd_actions_initialized)
1897 xdnd_initialize_actions();
1899 for (i=0; i<xdnd_n_actions; i++)
1900 if (action == xdnd_actions_table[i].action)
1901 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
1908 static GdkFilterReturn
1909 xdnd_status_filter (GdkXEvent *xev,
1913 GdkDisplay *display;
1914 XEvent *xevent = (XEvent *)xev;
1915 guint32 dest_window = xevent->xclient.data.l[0];
1916 guint32 flags = xevent->xclient.data.l[1];
1917 Atom action = xevent->xclient.data.l[4];
1918 GdkDragContext *context;
1920 if (!event->any.window ||
1921 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1922 return GDK_FILTER_CONTINUE; /* Not for us */
1925 g_message ("XdndStatus: dest_window: %#x action: %ld",
1926 dest_window, action));
1928 display = gdk_drawable_get_display (event->any.window);
1929 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1933 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1934 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1935 private->drag_status = GDK_DRAG_STATUS_DRAG;
1937 event->dnd.send_event = FALSE;
1938 event->dnd.type = GDK_DRAG_STATUS;
1939 event->dnd.context = context;
1940 g_object_ref (context);
1942 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1943 if (!(action != 0) != !(flags & 1))
1946 g_warning ("Received status event with flags not corresponding to action!\n"));
1950 context->action = xdnd_action_from_atom (display, action);
1952 return GDK_FILTER_TRANSLATE;
1955 return GDK_FILTER_REMOVE;
1958 static GdkFilterReturn
1959 xdnd_finished_filter (GdkXEvent *xev,
1963 GdkDisplay *display;
1964 XEvent *xevent = (XEvent *)xev;
1965 guint32 dest_window = xevent->xclient.data.l[0];
1966 GdkDragContext *context;
1968 if (!event->any.window ||
1969 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1970 return GDK_FILTER_CONTINUE; /* Not for us */
1973 g_message ("XdndFinished: dest_window: %#x", dest_window));
1975 display = gdk_drawable_get_display (event->any.window);
1976 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1980 event->dnd.type = GDK_DROP_FINISHED;
1981 event->dnd.context = context;
1982 g_object_ref (context);
1984 return GDK_FILTER_TRANSLATE;
1987 return GDK_FILTER_REMOVE;
1991 xdnd_set_targets (GdkDragContext *context)
1993 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1995 GList *tmp_list = context->targets;
1997 gint n_atoms = g_list_length (context->targets);
1998 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2000 atomlist = g_new (Atom, n_atoms);
2004 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2005 tmp_list = tmp_list->next;
2009 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2010 GDK_DRAWABLE_XID (context->source_window),
2011 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2012 XA_ATOM, 32, PropModeReplace,
2013 (guchar *)atomlist, n_atoms);
2017 private->xdnd_targets_set = 1;
2021 xdnd_set_actions (GdkDragContext *context)
2023 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2028 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2030 if (!xdnd_actions_initialized)
2031 xdnd_initialize_actions();
2033 actions = context->actions;
2035 for (i=0; i<xdnd_n_actions; i++)
2037 if (actions & xdnd_actions_table[i].action)
2039 actions &= ~xdnd_actions_table[i].action;
2044 atomlist = g_new (Atom, n_atoms);
2046 actions = context->actions;
2048 for (i=0; i<xdnd_n_actions; i++)
2050 if (actions & xdnd_actions_table[i].action)
2052 actions &= ~xdnd_actions_table[i].action;
2053 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2058 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2059 GDK_DRAWABLE_XID (context->source_window),
2060 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2061 XA_ATOM, 32, PropModeReplace,
2062 (guchar *)atomlist, n_atoms);
2066 private->xdnd_actions_set = 1;
2067 private->xdnd_actions = context->actions;
2070 /*************************************************************
2072 * Like gdk_send_event, but if the target is the root
2073 * window, sets an event mask of ButtonPressMask, otherwise
2074 * an event mask of 0.
2078 *************************************************************/
2081 xdnd_send_xevent (GdkDisplay *display,
2086 if (_gdk_x11_display_is_root_window (display, window))
2087 return _gdk_send_xevent (display, window, propagate, ButtonPressMask, event_send);
2089 return _gdk_send_xevent (display, window, propagate, 0, event_send);
2093 xdnd_send_enter (GdkDragContext *context)
2096 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2097 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2099 xev.xclient.type = ClientMessage;
2100 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2101 xev.xclient.format = 32;
2102 xev.xclient.window = private->drop_xid ?
2104 GDK_DRAWABLE_XID (context->dest_window);
2105 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2106 xev.xclient.data.l[1] = (3 << 24); /* version */
2107 xev.xclient.data.l[2] = 0;
2108 xev.xclient.data.l[3] = 0;
2109 xev.xclient.data.l[4] = 0;
2111 if (!private->xdnd_selection)
2112 private->xdnd_selection = gdk_x11_get_xatom_by_name_for_display (display, "XdndSelection");
2114 if (g_list_length (context->targets) > 3)
2116 if (!private->xdnd_targets_set)
2117 xdnd_set_targets (context);
2118 xev.xclient.data.l[1] |= 1;
2122 GList *tmp_list = context->targets;
2127 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2128 GDK_POINTER_TO_ATOM (tmp_list->data));
2129 tmp_list = tmp_list->next;
2134 if (!xdnd_send_xevent (display,
2135 GDK_DRAWABLE_XID (context->dest_window),
2139 g_message ("Send event to %lx failed",
2140 GDK_DRAWABLE_XID (context->dest_window)));
2141 g_object_unref (context->dest_window);
2142 context->dest_window = NULL;
2147 xdnd_send_leave (GdkDragContext *context)
2149 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2152 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2154 xev.xclient.type = ClientMessage;
2155 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2156 xev.xclient.format = 32;
2157 xev.xclient.window = private->drop_xid ?
2159 GDK_DRAWABLE_XID (context->dest_window);
2160 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2161 xev.xclient.data.l[1] = 0;
2162 xev.xclient.data.l[2] = 0;
2163 xev.xclient.data.l[3] = 0;
2164 xev.xclient.data.l[4] = 0;
2166 if (!xdnd_send_xevent (display,
2167 GDK_DRAWABLE_XID (context->dest_window),
2171 g_message ("Send event to %lx failed",
2172 GDK_DRAWABLE_XID (context->dest_window)));
2173 g_object_unref (context->dest_window);
2174 context->dest_window = NULL;
2179 xdnd_send_drop (GdkDragContext *context, guint32 time)
2181 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2182 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2185 xev.xclient.type = ClientMessage;
2186 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2187 xev.xclient.format = 32;
2188 xev.xclient.window = private->drop_xid ?
2190 GDK_DRAWABLE_XID (context->dest_window);
2191 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2192 xev.xclient.data.l[1] = 0;
2193 xev.xclient.data.l[2] = time;
2194 xev.xclient.data.l[3] = 0;
2195 xev.xclient.data.l[4] = 0;
2197 if (!xdnd_send_xevent (display,
2198 GDK_DRAWABLE_XID (context->dest_window),
2202 g_message ("Send event to %lx failed",
2203 GDK_DRAWABLE_XID (context->dest_window)));
2204 g_object_unref (context->dest_window);
2205 context->dest_window = NULL;
2210 xdnd_send_motion (GdkDragContext *context,
2213 GdkDragAction action,
2216 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2217 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2220 xev.xclient.type = ClientMessage;
2221 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2222 xev.xclient.format = 32;
2223 xev.xclient.window = private->drop_xid ?
2225 GDK_DRAWABLE_XID (context->dest_window);
2226 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2227 xev.xclient.data.l[1] = 0;
2228 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2229 xev.xclient.data.l[3] = time;
2230 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2232 if (!xdnd_send_xevent (display,
2233 GDK_DRAWABLE_XID (context->dest_window),
2237 g_message ("Send event to %lx failed",
2238 GDK_DRAWABLE_XID (context->dest_window)));
2239 g_object_unref (context->dest_window);
2240 context->dest_window = NULL;
2242 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2246 xdnd_check_dest (GdkDisplay *display,
2249 gboolean retval = FALSE;
2252 unsigned long nitems, after;
2256 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2257 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2261 gdk_error_trap_push ();
2263 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2265 1, False, AnyPropertyType,
2266 &type, &format, &nitems, &after,
2267 (guchar **)&proxy_data) == Success)
2271 if ((format == 32) && (nitems == 1))
2273 proxy = *proxy_data;
2277 g_warning ("Invalid XdndOwner "
2278 "property on window %ld\n", win));
2283 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2285 1, False, AnyPropertyType,
2286 &type, &format, &nitems, &after,
2287 (guchar **)&version) == Success) &&
2290 if ((format == 32) && (nitems == 1))
2297 g_warning ("Invalid XdndAware property on window %ld\n", win));
2303 gdk_error_trap_pop ();
2305 return retval ? (proxy ? proxy : win) : None;
2311 xdnd_read_actions (GdkDragContext *context)
2313 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2316 gulong nitems, after;
2321 /* Get the XdndActionList, if set */
2323 gdk_error_trap_push ();
2325 if (XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2326 GDK_DRAWABLE_XID (context->source_window),
2327 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2329 False, XA_ATOM, &type, &format, &nitems,
2330 &after, (guchar **)&data) == Success &&
2333 context->actions = 0;
2335 for (i=0; i<nitems; i++)
2336 context->actions |= xdnd_action_from_atom (display, data[i]);
2338 (PRIVATE_DATA (context))->xdnd_have_actions = TRUE;
2340 #ifdef G_ENABLE_DEBUG
2341 if (_gdk_debug_flags & GDK_DEBUG_DND)
2343 GString *action_str = g_string_new (NULL);
2344 if (context->actions & GDK_ACTION_MOVE)
2345 g_string_append(action_str, "MOVE ");
2346 if (context->actions & GDK_ACTION_COPY)
2347 g_string_append(action_str, "COPY ");
2348 if (context->actions & GDK_ACTION_LINK)
2349 g_string_append(action_str, "LINK ");
2350 if (context->actions & GDK_ACTION_ASK)
2351 g_string_append(action_str, "ASK ");
2353 g_message("Xdnd actions = %s", action_str->str);
2354 g_string_free (action_str, TRUE);
2356 #endif /* G_ENABLE_DEBUG */
2361 gdk_error_trap_pop ();
2364 /* We have to make sure that the XdndActionList we keep internally
2365 * is up to date with the XdndActionList on the source window
2366 * because we get no notification, because Xdnd wasn't meant
2367 * to continually send actions. So we select on PropertyChangeMask
2368 * and add this filter.
2370 static GdkFilterReturn
2371 xdnd_source_window_filter (GdkXEvent *xev,
2375 XEvent *xevent = (XEvent *)xev;
2376 GdkDragContext *context = cb_data;
2377 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2379 if ((xevent->xany.type == PropertyNotify) &&
2380 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2382 xdnd_read_actions (context);
2384 return GDK_FILTER_REMOVE;
2387 return GDK_FILTER_CONTINUE;
2391 xdnd_manage_source_filter (GdkDragContext *context,
2393 gboolean add_filter)
2395 gdk_error_trap_push ();
2397 if (!GDK_WINDOW_DESTROYED (window))
2401 gdk_window_set_events (window,
2402 gdk_window_get_events (window) |
2403 GDK_PROPERTY_CHANGE_MASK);
2404 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2409 gdk_window_remove_filter (window,
2410 xdnd_source_window_filter,
2412 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2413 * but we might want it for other reasons. (Like
2414 * INCR selection transactions).
2419 gdk_display_sync (gdk_drawable_get_display (window));
2420 gdk_error_trap_pop ();
2423 static GdkFilterReturn
2424 xdnd_enter_filter (GdkXEvent *xev,
2428 GdkDisplay *display;
2429 GdkDisplayX11 *display_x11;
2430 XEvent *xevent = (XEvent *)xev;
2431 GdkDragContext *new_context;
2436 gulong nitems, after;
2439 guint32 source_window;
2443 if (!event->any.window ||
2444 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2445 return GDK_FILTER_CONTINUE; /* Not for us */
2447 source_window = xevent->xclient.data.l[0];
2448 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2449 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2451 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2452 display_x11 = GDK_DISPLAY_X11 (display);
2455 g_message ("XdndEnter: source_window: %#x, version: %#x",
2456 source_window, version));
2460 /* Old source ignore */
2461 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2462 return GDK_FILTER_REMOVE;
2465 if (display_x11->current_dest_drag != NULL)
2467 g_object_unref (display_x11->current_dest_drag);
2468 display_x11->current_dest_drag = NULL;
2471 new_context = gdk_drag_context_new ();
2472 new_context->protocol = GDK_DRAG_PROTO_XDND;
2473 new_context->is_source = FALSE;
2475 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2476 if (new_context->source_window)
2477 g_object_ref (new_context->source_window);
2480 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2481 if (!new_context->source_window)
2483 g_object_unref (new_context);
2484 return GDK_FILTER_REMOVE;
2487 new_context->dest_window = event->any.window;
2488 g_object_ref (new_context->dest_window);
2490 new_context->targets = NULL;
2493 gdk_error_trap_push ();
2494 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2496 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2498 False, XA_ATOM, &type, &format, &nitems,
2499 &after, (guchar **)&data);
2501 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2503 g_object_unref (new_context);
2504 return GDK_FILTER_REMOVE;
2507 for (i=0; i<nitems; i++)
2508 new_context->targets =
2509 g_list_append (new_context->targets,
2510 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2518 if (xevent->xclient.data.l[2+i])
2519 new_context->targets =
2520 g_list_append (new_context->targets,
2521 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2522 xevent->xclient.data.l[2+i])));
2525 #ifdef G_ENABLE_DEBUG
2526 if (_gdk_debug_flags & GDK_DEBUG_DND)
2527 print_target_list (new_context->targets);
2528 #endif /* G_ENABLE_DEBUG */
2530 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2531 xdnd_read_actions (new_context);
2533 event->dnd.type = GDK_DRAG_ENTER;
2534 event->dnd.context = new_context;
2535 g_object_ref (new_context);
2537 display_x11->current_dest_drag = new_context;
2539 (PRIVATE_DATA (new_context))->xdnd_selection =
2540 gdk_x11_get_xatom_by_name_for_display (display, "XdndSelection");
2541 return GDK_FILTER_TRANSLATE;
2544 static GdkFilterReturn
2545 xdnd_leave_filter (GdkXEvent *xev,
2549 XEvent *xevent = (XEvent *)xev;
2550 guint32 source_window = xevent->xclient.data.l[0];
2551 GdkDisplayX11 *display_x11;
2553 if (!event->any.window ||
2554 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2555 return GDK_FILTER_CONTINUE; /* Not for us */
2558 g_message ("XdndLeave: source_window: %#x",
2561 display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
2563 if ((display_x11->current_dest_drag != NULL) &&
2564 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2565 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2567 event->dnd.type = GDK_DRAG_LEAVE;
2568 /* Pass ownership of context to the event */
2569 event->dnd.context = display_x11->current_dest_drag;
2571 display_x11->current_dest_drag = NULL;
2573 return GDK_FILTER_TRANSLATE;
2576 return GDK_FILTER_REMOVE;
2579 static GdkFilterReturn
2580 xdnd_position_filter (GdkXEvent *xev,
2584 XEvent *xevent = (XEvent *)xev;
2585 guint32 source_window = xevent->xclient.data.l[0];
2586 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2587 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2588 guint32 time = xevent->xclient.data.l[3];
2589 Atom action = xevent->xclient.data.l[4];
2591 GdkDisplay *display;
2592 GdkDisplayX11 *display_x11;
2594 if (!event->any.window ||
2595 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2596 return GDK_FILTER_CONTINUE; /* Not for us */
2599 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2600 source_window, x_root, y_root, time, action));
2602 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2603 display_x11 = GDK_DISPLAY_X11 (display);
2605 if ((display_x11->current_dest_drag != NULL) &&
2606 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2607 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2609 event->dnd.type = GDK_DRAG_MOTION;
2610 event->dnd.context = display_x11->current_dest_drag;
2611 g_object_ref (display_x11->current_dest_drag);
2613 event->dnd.time = time;
2615 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2617 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2618 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2620 event->dnd.x_root = x_root;
2621 event->dnd.y_root = y_root;
2623 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2624 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
2626 return GDK_FILTER_TRANSLATE;
2629 return GDK_FILTER_REMOVE;
2632 static GdkFilterReturn
2633 xdnd_drop_filter (GdkXEvent *xev,
2637 XEvent *xevent = (XEvent *)xev;
2638 guint32 source_window = xevent->xclient.data.l[0];
2639 guint32 time = xevent->xclient.data.l[2];
2640 GdkDisplayX11 *display_x11;
2642 if (!event->any.window ||
2643 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2644 return GDK_FILTER_CONTINUE; /* Not for us */
2647 g_message ("XdndDrop: source_window: %#x time: %d",
2648 source_window, time));
2650 display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
2652 if ((display_x11->current_dest_drag != NULL) &&
2653 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2654 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2656 GdkDragContextPrivateX11 *private;
2657 private = PRIVATE_DATA (display_x11->current_dest_drag);
2659 event->dnd.type = GDK_DROP_START;
2661 event->dnd.context = display_x11->current_dest_drag;
2662 g_object_ref (display_x11->current_dest_drag);
2664 event->dnd.time = time;
2665 event->dnd.x_root = private->last_x;
2666 event->dnd.y_root = private->last_y;
2668 return GDK_FILTER_TRANSLATE;
2671 return GDK_FILTER_REMOVE;
2674 /*************************************************************
2675 ************************** Public API ***********************
2676 *************************************************************/
2678 _gdk_dnd_init (GdkDisplay *display)
2682 gdk_display_add_client_message_filter (
2684 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2685 motif_dnd_filter, NULL);
2686 gdk_display_add_client_message_filter (
2688 gdk_atom_intern ("XdndEnter", FALSE),
2689 xdnd_enter_filter, NULL);
2690 gdk_display_add_client_message_filter (
2692 gdk_atom_intern ("XdndLeave", FALSE),
2693 xdnd_leave_filter, NULL);
2694 gdk_display_add_client_message_filter (
2696 gdk_atom_intern ("XdndPosition", FALSE),
2697 xdnd_position_filter, NULL);
2698 gdk_display_add_client_message_filter (
2700 gdk_atom_intern ("XdndStatus", FALSE),
2701 xdnd_status_filter, NULL);
2702 gdk_display_add_client_message_filter (
2704 gdk_atom_intern ("XdndFinished", FALSE),
2705 xdnd_finished_filter, NULL);
2706 gdk_display_add_client_message_filter (
2708 gdk_atom_intern ("XdndDrop", FALSE),
2709 xdnd_drop_filter, NULL);
2715 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2717 if (context->dest_window)
2719 switch (context->protocol)
2721 case GDK_DRAG_PROTO_MOTIF:
2722 motif_send_leave (context, time);
2724 case GDK_DRAG_PROTO_XDND:
2725 xdnd_send_leave (context);
2727 case GDK_DRAG_PROTO_ROOTWIN:
2728 case GDK_DRAG_PROTO_NONE:
2733 g_object_unref (context->dest_window);
2734 context->dest_window = NULL;
2739 gdk_drag_begin (GdkWindow *window,
2742 GdkDragContext *new_context;
2744 g_return_val_if_fail (window != NULL, NULL);
2746 new_context = gdk_drag_context_new ();
2747 new_context->is_source = TRUE;
2748 new_context->source_window = window;
2749 g_object_ref (window);
2751 new_context->targets = g_list_copy (targets);
2752 new_context->actions = 0;
2758 * gdk_drag_get_protocol_for_display:
2759 * @display: the #GdkDisplay where the destination window resides
2760 * @xid: the X id of the destination window.
2761 * @protocol: location where the supported DND protocol is returned.
2762 * @returns: the X id of the window where the drop should happen. This
2763 * may be @xid or the X id of a proxy window, or None if @xid doesn't
2764 * support Drag and Drop.
2766 * Finds out the DND protocol supported by a window.
2769 gdk_drag_get_protocol_for_display (GdkDisplay *display,
2771 GdkDragProtocol *protocol)
2774 g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
2776 if ((retval = xdnd_check_dest (display, xid)))
2778 *protocol = GDK_DRAG_PROTO_XDND;
2779 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2782 else if ((retval = motif_check_dest (display, xid)))
2784 *protocol = GDK_DRAG_PROTO_MOTIF;
2785 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2790 /* Check if this is a root window */
2792 gboolean rootwin = FALSE;
2795 unsigned long nitems, after;
2796 unsigned char *data;
2798 if (_gdk_x11_display_is_root_window (display, (Window) xid))
2801 gdk_error_trap_push ();
2805 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
2806 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
2807 0, 0, False, AnyPropertyType,
2808 &type, &format, &nitems, &after, &data) == Success &&
2816 /* Until I find out what window manager the next one is for,
2817 * I'm leaving it commented out. It's supported in the
2818 * xscreensaver sources, though.
2823 if (XGetWindowProperty (gdk_display, win,
2824 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
2825 0, 0, False, AnyPropertyType,
2826 &type, &format, &nitems, &data) &&
2832 gdk_error_trap_pop ();
2836 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2841 *protocol = GDK_DRAG_PROTO_NONE;
2845 static GdkWindowCache *
2846 drag_context_find_window_cache (GdkDragContext *context,
2849 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2851 GdkWindowCache *cache;
2853 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
2855 cache = tmp_list->data;
2856 if (cache->screen == screen)
2860 cache = gdk_window_cache_new (screen);
2861 private->window_caches = g_slist_prepend (private->window_caches, cache);
2867 gdk_drag_find_window_for_screen (GdkDragContext *context,
2868 GdkWindow *drag_window,
2872 GdkWindow **dest_window,
2873 GdkDragProtocol *protocol)
2875 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2876 GdkWindowCache *window_cache;
2877 GdkDisplay *display;
2880 g_return_if_fail (context != NULL);
2882 display = GDK_WINDOW_DISPLAY (context->source_window);
2884 window_cache = drag_context_find_window_cache (context, screen);
2886 dest = get_client_window_at_coords (window_cache,
2888 GDK_DRAWABLE_XID (drag_window) : None,
2891 if (private->dest_xid != dest)
2894 private->dest_xid = dest;
2896 /* Check if new destination accepts drags, and which protocol */
2898 /* There is some ugliness here. We actually need to pass
2899 * _three_ pieces of information to drag_motion - dest_window,
2900 * protocol, and the XID of the unproxied window. The first
2901 * two are passed explicitely, the third implicitly through
2902 * protocol->dest_xid.
2904 if ((recipient = gdk_drag_get_protocol_for_display (display, dest, protocol)))
2906 *dest_window = gdk_window_lookup_for_display (display, recipient);
2908 g_object_ref (*dest_window);
2910 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
2913 *dest_window = NULL;
2917 *dest_window = context->dest_window;
2919 g_object_ref (*dest_window);
2920 *protocol = context->protocol;
2925 gdk_drag_motion (GdkDragContext *context,
2926 GdkWindow *dest_window,
2927 GdkDragProtocol protocol,
2930 GdkDragAction suggested_action,
2931 GdkDragAction possible_actions,
2934 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2936 g_return_val_if_fail (context != NULL, FALSE);
2938 /* When we have a Xdnd target, make sure our XdndActionList
2939 * matches the current actions;
2941 private->old_actions = context->actions;
2942 context->actions = possible_actions;
2944 if ((protocol == GDK_DRAG_PROTO_XDND) &&
2945 (!private->xdnd_actions_set ||
2946 private->xdnd_actions != possible_actions))
2947 xdnd_set_actions (context);
2949 if (context->dest_window != dest_window)
2951 GdkEvent temp_event;
2953 /* Send a leave to the last destination */
2954 gdk_drag_do_leave (context, time);
2955 private->drag_status = GDK_DRAG_STATUS_DRAG;
2957 /* Check if new destination accepts drags, and which protocol */
2961 context->dest_window = dest_window;
2962 private->drop_xid = private->dest_xid;
2963 g_object_ref (context->dest_window);
2964 context->protocol = protocol;
2968 case GDK_DRAG_PROTO_MOTIF:
2969 motif_send_enter (context, time);
2972 case GDK_DRAG_PROTO_XDND:
2973 xdnd_send_enter (context);
2976 case GDK_DRAG_PROTO_ROOTWIN:
2977 case GDK_DRAG_PROTO_NONE:
2981 private->old_action = suggested_action;
2982 context->suggested_action = suggested_action;
2983 private->old_actions = possible_actions;
2987 context->dest_window = NULL;
2988 private->drop_xid = None;
2989 context->action = 0;
2992 /* Push a status event, to let the client know that
2996 temp_event.dnd.type = GDK_DRAG_STATUS;
2997 temp_event.dnd.window = context->source_window;
2998 /* We use this to signal a synthetic status. Perhaps
2999 * we should use an extra field...
3001 temp_event.dnd.send_event = TRUE;
3003 temp_event.dnd.context = context;
3004 temp_event.dnd.time = time;
3006 gdk_event_put (&temp_event);
3010 private->old_action = context->suggested_action;
3011 context->suggested_action = suggested_action;
3014 /* Send a drag-motion event */
3016 private->last_x = x_root;
3017 private->last_y = y_root;
3019 if (context->dest_window)
3021 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3023 switch (context->protocol)
3025 case GDK_DRAG_PROTO_MOTIF:
3026 motif_send_motion (context, x_root, y_root, suggested_action, time);
3029 case GDK_DRAG_PROTO_XDND:
3030 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3033 case GDK_DRAG_PROTO_ROOTWIN:
3035 GdkEvent temp_event;
3037 if (g_list_find (context->targets,
3038 GDK_ATOM_TO_POINTER (gdk_atom_intern ("application/x-rootwin-drop", FALSE))))
3039 context->action = context->suggested_action;
3041 context->action = 0;
3043 temp_event.dnd.type = GDK_DRAG_STATUS;
3044 temp_event.dnd.window = context->source_window;
3045 temp_event.dnd.send_event = FALSE;
3046 temp_event.dnd.context = context;
3047 temp_event.dnd.time = time;
3049 gdk_event_put (&temp_event);
3052 case GDK_DRAG_PROTO_NONE:
3053 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3067 gdk_drag_drop (GdkDragContext *context,
3070 g_return_if_fail (context != NULL);
3072 if (context->dest_window)
3074 switch (context->protocol)
3076 case GDK_DRAG_PROTO_MOTIF:
3077 motif_send_leave (context, time);
3078 motif_send_drop (context, time);
3081 case GDK_DRAG_PROTO_XDND:
3082 xdnd_send_drop (context, time);
3085 case GDK_DRAG_PROTO_ROOTWIN:
3086 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3088 case GDK_DRAG_PROTO_NONE:
3089 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3098 gdk_drag_abort (GdkDragContext *context,
3101 g_return_if_fail (context != NULL);
3103 gdk_drag_do_leave (context, time);
3106 /* Destination side */
3109 gdk_drag_status (GdkDragContext *context,
3110 GdkDragAction action,
3113 GdkDragContextPrivateX11 *private;
3115 GdkDisplay *display;
3117 g_return_if_fail (context != NULL);
3119 private = PRIVATE_DATA (context);
3120 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3122 context->action = action;
3124 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3126 xev.xclient.type = ClientMessage;
3127 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3128 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3129 xev.xclient.format = 8;
3130 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3132 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3134 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3138 if ((action != 0) != (private->old_action != 0))
3141 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3143 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3146 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3149 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3153 case GDK_ACTION_MOVE:
3154 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3156 case GDK_ACTION_COPY:
3157 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3159 case GDK_ACTION_LINK:
3160 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3163 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3168 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3170 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3172 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3173 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3174 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3176 if (!_gdk_send_xevent (display,
3177 GDK_DRAWABLE_XID (context->source_window),
3180 g_message ("Send event to %lx failed",
3181 GDK_DRAWABLE_XID (context->source_window)));
3183 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3185 xev.xclient.type = ClientMessage;
3186 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3187 xev.xclient.format = 32;
3188 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3190 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3191 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3192 xev.xclient.data.l[2] = 0;
3193 xev.xclient.data.l[3] = 0;
3194 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3196 if (!xdnd_send_xevent (display,
3197 GDK_DRAWABLE_XID (context->source_window),
3200 g_message ("Send event to %lx failed",
3201 GDK_DRAWABLE_XID (context->source_window)));
3204 private->old_action = action;
3208 gdk_drop_reply (GdkDragContext *context,
3212 GdkDragContextPrivateX11 *private;
3214 g_return_if_fail (context != NULL);
3216 private = PRIVATE_DATA (context);
3218 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3220 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3223 xev.xclient.type = ClientMessage;
3224 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3225 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3226 xev.xclient.format = 8;
3228 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3229 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3231 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3232 (XmDROP_SITE_VALID << 4) |
3233 (XmDROP_NOOP << 8) |
3236 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3237 (XmNO_DROP_SITE << 4) |
3238 (XmDROP_NOOP << 8) |
3239 (XmDROP_CANCEL << 12);
3240 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3241 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3243 _gdk_send_xevent (display,
3244 GDK_DRAWABLE_XID (context->source_window),
3250 gdk_drop_finish (GdkDragContext *context,
3254 g_return_if_fail (context != NULL);
3256 if (context->protocol == GDK_DRAG_PROTO_XDND)
3258 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3261 xev.xclient.type = ClientMessage;
3262 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3263 xev.xclient.format = 32;
3264 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3266 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3267 xev.xclient.data.l[1] = 0;
3268 xev.xclient.data.l[2] = 0;
3269 xev.xclient.data.l[3] = 0;
3270 xev.xclient.data.l[4] = 0;
3272 if (!xdnd_send_xevent (display,
3273 GDK_DRAWABLE_XID (context->source_window),
3276 g_message ("Send event to %lx failed",
3277 GDK_DRAWABLE_XID (context->source_window)));
3283 gdk_window_register_dnd (GdkWindow *window)
3285 static gulong xdnd_version = 3;
3286 MotifDragReceiverInfo info;
3287 Atom motif_drag_receiver_info_atom;
3288 GdkDisplay *display = gdk_drawable_get_display (window);
3290 g_return_if_fail (window != NULL);
3292 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered")))
3295 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3297 /* Set Motif drag receiver information property */
3299 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3300 "_MOTIF_DRAG_RECEIVER_INFO");
3301 info.byte_order = local_byte_order;
3302 info.protocol_version = 0;
3303 info.protocol_style = XmDRAG_DYNAMIC;
3304 info.proxy_window = None;
3305 info.num_drop_sites = 0;
3306 info.total_size = sizeof(info);
3308 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3309 motif_drag_receiver_info_atom,
3310 motif_drag_receiver_info_atom,
3317 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3318 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3319 GDK_DRAWABLE_XID (window),
3320 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3321 XA_ATOM, 32, PropModeReplace,
3322 (guchar *)&xdnd_version, 1);
3325 /*************************************************************
3326 * gdk_drag_get_selection:
3327 * Returns the selection atom for the current source window
3331 *************************************************************/
3334 gdk_drag_get_selection (GdkDragContext *context)
3336 g_return_val_if_fail (context != NULL, GDK_NONE);
3338 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3339 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3340 (PRIVATE_DATA (context))->motif_selection);
3341 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3342 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3343 (PRIVATE_DATA (context))->xdnd_selection);