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() */
35 #include "gdkproperty.h"
36 #include "gdkprivate-x11.h"
37 #include "gdkinternals.h"
38 #include "gdkscreen-x11.h"
39 #include "gdkdisplay-x11.h"
41 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
45 GDK_DRAG_STATUS_MOTION_WAIT,
46 GDK_DRAG_STATUS_ACTION_WAIT,
52 gint x, y, width, height;
58 GHashTable *child_hash;
63 /* Structure that holds information about a drag in progress.
64 * this is used on both source and destination sides.
66 struct _GdkDragContextPrivateX11 {
67 GdkDragContext context;
73 guint16 last_x; /* Coordinates from last event */
75 GdkDragAction old_action; /* The last action we sent to the source */
76 GdkDragAction old_actions; /* The last actions we sent to the source */
77 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
79 Window dest_xid; /* The last window we looked up */
80 Window drop_xid; /* The (non-proxied) window that is receiving drops */
81 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
82 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
83 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
84 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
85 guint drag_status : 4; /* current status of drag */
87 GSList *window_caches;
90 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
92 /* Forward declarations */
94 static void gdk_window_cache_destroy (GdkWindowCache *cache);
96 static void motif_read_target_table (GdkDisplay *display);
98 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
102 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
105 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
108 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
111 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
114 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
117 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
121 static void xdnd_manage_source_filter (GdkDragContext *context,
123 gboolean add_filter);
125 static void gdk_drag_context_init (GdkDragContext *dragcontext);
126 static void gdk_drag_context_class_init (GdkDragContextClass *klass);
127 static void gdk_drag_context_finalize (GObject *object);
129 static gpointer parent_class = NULL;
130 static GList *contexts;
132 const static struct {
133 const char *atom_name;
136 { "XdndEnter", xdnd_enter_filter },
137 { "XdndLeave", xdnd_leave_filter },
138 { "XdndPosition", xdnd_position_filter },
139 { "XdndStatus", xdnd_status_filter },
140 { "XdndFinished", xdnd_finished_filter },
141 { "XdndDrop", xdnd_drop_filter },
145 gdk_drag_context_get_type (void)
147 static GType object_type = 0;
151 static const GTypeInfo object_info =
153 sizeof (GdkDragContextClass),
154 (GBaseInitFunc) NULL,
155 (GBaseFinalizeFunc) NULL,
156 (GClassInitFunc) gdk_drag_context_class_init,
157 NULL, /* class_finalize */
158 NULL, /* class_data */
159 sizeof (GdkDragContext),
161 (GInstanceInitFunc) gdk_drag_context_init,
164 object_type = g_type_register_static (G_TYPE_OBJECT,
173 gdk_drag_context_init (GdkDragContext *dragcontext)
175 GdkDragContextPrivateX11 *private;
177 private = g_new0 (GdkDragContextPrivateX11, 1);
179 dragcontext->windowing_data = private;
181 contexts = g_list_prepend (contexts, dragcontext);
185 gdk_drag_context_class_init (GdkDragContextClass *klass)
187 GObjectClass *object_class = G_OBJECT_CLASS (klass);
189 parent_class = g_type_class_peek_parent (klass);
191 object_class->finalize = gdk_drag_context_finalize;
195 gdk_drag_context_finalize (GObject *object)
197 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
198 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
201 g_list_free (context->targets);
203 if (context->source_window)
205 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
207 xdnd_manage_source_filter (context, context->source_window, FALSE);
209 g_object_unref (context->source_window);
212 if (context->dest_window)
213 g_object_unref (context->dest_window);
215 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
216 gdk_window_cache_destroy (tmp_list->data);
217 g_slist_free (private->window_caches);
219 contexts = g_list_remove (contexts, context);
223 G_OBJECT_CLASS (parent_class)->finalize (object);
229 * gdk_drag_context_new:
231 * Creates a new #GdkDragContext.
233 * Return value: the newly created #GdkDragContext.
236 gdk_drag_context_new (void)
238 return g_object_new (gdk_drag_context_get_type (), NULL);
242 * gdk_drag_context_ref:
243 * @context: a #GdkDragContext.
245 * Deprecated function; use g_object_ref() instead.
248 gdk_drag_context_ref (GdkDragContext *context)
250 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
252 g_object_ref (context);
256 * gdk_drag_context_unref:
257 * @context: a #GdkDragContext.
259 * Deprecated function; use g_object_unref() instead.
262 gdk_drag_context_unref (GdkDragContext *context)
264 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
266 g_object_unref (context);
269 static GdkDragContext *
270 gdk_drag_context_find (GdkDisplay *display,
275 GList *tmp_list = contexts;
276 GdkDragContext *context;
277 GdkDragContextPrivateX11 *private;
278 Window context_dest_xid;
282 context = (GdkDragContext *)tmp_list->data;
283 private = PRIVATE_DATA (context);
285 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
286 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
289 context_dest_xid = context->dest_window ?
292 GDK_DRAWABLE_XID (context->dest_window)) :
295 if ((!context->is_source == !is_source) &&
296 ((source_xid == None) || (context->source_window &&
297 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
298 ((dest_xid == None) || (context_dest_xid == dest_xid)))
301 tmp_list = tmp_list->next;
307 /* Utility functions */
310 gdk_window_cache_add (GdkWindowCache *cache,
312 gint x, gint y, gint width, gint height,
315 GdkCacheChild *child = g_new (GdkCacheChild, 1);
320 child->width = width;
321 child->height = height;
322 child->mapped = mapped;
324 cache->children = g_list_prepend (cache->children, child);
325 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
329 static GdkFilterReturn
330 gdk_window_cache_filter (GdkXEvent *xev,
334 XEvent *xevent = (XEvent *)xev;
335 GdkWindowCache *cache = data;
337 switch (xevent->type)
339 case CirculateNotify:
341 case ConfigureNotify:
343 XConfigureEvent *xce = &xevent->xconfigure;
346 node = g_hash_table_lookup (cache->child_hash,
347 GUINT_TO_POINTER (xce->window));
350 GdkCacheChild *child = node->data;
353 child->width = xce->width;
354 child->height = xce->height;
355 if (xce->above == None && (node->next))
357 GList *last = g_list_last (cache->children);
358 cache->children = g_list_remove_link (cache->children, node);
365 GList *above_node = g_hash_table_lookup (cache->child_hash,
366 GUINT_TO_POINTER (xce->above));
367 if (above_node && node->next != above_node)
369 /* Put the window above (before in the list) above_node
371 cache->children = g_list_remove_link (cache->children, node);
372 node->prev = above_node->prev;
374 node->prev->next = node;
376 cache->children = node;
377 node->next = above_node;
378 above_node->prev = node;
386 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
388 if (!g_hash_table_lookup (cache->child_hash,
389 GUINT_TO_POINTER (xcwe->window)))
390 gdk_window_cache_add (cache, xcwe->window,
391 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
397 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
400 node = g_hash_table_lookup (cache->child_hash,
401 GUINT_TO_POINTER (xdwe->window));
404 g_hash_table_remove (cache->child_hash,
405 GUINT_TO_POINTER (xdwe->window));
406 cache->children = g_list_remove_link (cache->children, node);
408 g_list_free_1 (node);
414 XMapEvent *xme = &xevent->xmap;
417 node = g_hash_table_lookup (cache->child_hash,
418 GUINT_TO_POINTER (xme->window));
421 GdkCacheChild *child = node->data;
422 child->mapped = TRUE;
430 XMapEvent *xume = &xevent->xmap;
433 node = g_hash_table_lookup (cache->child_hash,
434 GUINT_TO_POINTER (xume->window));
437 GdkCacheChild *child = node->data;
438 child->mapped = FALSE;
443 return GDK_FILTER_CONTINUE;
445 return GDK_FILTER_REMOVE;
448 static GdkWindowCache *
449 gdk_window_cache_new (GdkScreen *screen)
451 XWindowAttributes xwa;
452 Window root, parent, *children;
453 unsigned int nchildren;
455 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
456 GdkWindow *root_window = gdk_screen_get_root_window (screen);
458 GdkWindowCache *result = g_new (GdkWindowCache, 1);
460 result->children = NULL;
461 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
462 result->screen = screen;
464 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
465 result->old_event_mask = xwa.your_event_mask;
466 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
467 result->old_event_mask | SubstructureNotifyMask);
468 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
470 gdk_error_trap_push ();
472 if (!XQueryTree(xdisplay, GDK_WINDOW_XWINDOW (root_window),
473 &root, &parent, &children, &nchildren))
475 gdk_error_trap_pop ();
479 for (i = 0; i < nchildren ; i++)
481 if (XGetWindowAttributes (xdisplay, children[i], &xwa))
482 gdk_window_cache_add (result, children[i],
483 xwa.x, xwa.y, xwa.width, xwa.height,
484 xwa.map_state != IsUnmapped);
488 gdk_error_trap_pop ();
494 gdk_window_cache_destroy (GdkWindowCache *cache)
496 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
498 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
499 GDK_WINDOW_XWINDOW (root_window),
500 cache->old_event_mask);
501 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
503 g_list_foreach (cache->children, (GFunc)g_free, NULL);
504 g_list_free (cache->children);
505 g_hash_table_destroy (cache->child_hash);
511 get_client_window_at_coords_recurse (GdkDisplay *display,
516 Window root, tmp_parent, *children;
517 unsigned int nchildren;
522 unsigned long nitems, after;
525 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(display), win,
526 gdk_x11_get_xatom_by_name_for_display (display, "WM_STATE"),
527 0, 0, False, AnyPropertyType,
528 &type, &format, &nitems, &after, &data) != Success)
538 /* This is beautiful! Damn Enlightenment and click-to-focus */
539 if (!XTranslateCoordinates (gdk_display, _gdk_root_window, win,
540 x_root, y_root, &dest_x, &dest_y, &child))
544 if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), win,
545 &root, &tmp_parent, &children, &nchildren))
548 for (i = nchildren - 1; (i >= 0) && (child == None); i--)
550 XWindowAttributes xwa;
552 if (XGetWindowAttributes (GDK_DISPLAY_XDISPLAY (display),
555 if ((xwa.map_state == IsViewable) && (xwa.class == InputOutput) &&
556 (x >= xwa.x) && (x < xwa.x + (gint)xwa.width) &&
557 (y >= xwa.y) && (y < xwa.y + (gint)xwa.height))
570 return get_client_window_at_coords_recurse (display, child, x, y);
576 get_client_window_at_coords (GdkWindowCache *cache,
582 Window retval = None;
584 gdk_error_trap_push ();
586 tmp_list = cache->children;
588 while (tmp_list && !retval)
590 GdkCacheChild *child = tmp_list->data;
592 if ((child->xid != ignore) && (child->mapped))
594 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
595 (y_root >= child->y) && (y_root < child->y + child->height))
597 retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
606 tmp_list = tmp_list->next;
609 gdk_error_trap_pop ();
614 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
617 /*************************************************************
618 ***************************** MOTIF *************************
619 *************************************************************/
621 /* values used in the message type for Motif DND */
634 /* Values used to specify type of protocol to use */
638 XmDRAG_PREFER_PREREGISTER,
640 XmDRAG_PREFER_DYNAMIC,
642 XmDRAG_PREFER_RECEIVER
645 /* Operation codes */
653 /* Drop site status */
655 XmNO_DROP_SITE = 0x01,
656 XmDROP_SITE_INVALID = 0x02,
657 XmDROP_SITE_VALID = 0x03
660 /* completion status */
668 /* Byte swapping routines. The motif specification leaves it
669 * up to us to save a few bytes in the client messages
671 static gchar local_byte_order = '\0';
673 #ifdef G_ENABLE_DEBUG
675 print_target_list (GList *targets)
679 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
680 g_message ("\t%s", name);
682 targets = targets->next;
685 #endif /* G_ENABLE_DEBUG */
688 init_byte_order (void)
690 guint32 myint = 0x01020304;
691 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
695 card16_to_host (guint16 x, gchar byte_order) {
696 if (byte_order == local_byte_order)
699 return (x << 8) | (x >> 8);
703 card32_to_host (guint32 x, gchar byte_order) {
704 if (byte_order == local_byte_order)
707 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
710 /* Motif packs together fields of varying length into the
711 * client message. We can't rely on accessing these
712 * through data.s[], data.l[], etc, because on some architectures
713 * (i.e., Alpha) these won't be valid for format == 8.
716 #define MOTIF_XCLIENT_BYTE(xevent,i) \
717 (xevent)->xclient.data.b[i]
718 #define MOTIF_XCLIENT_SHORT(xevent,i) \
719 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
720 #define MOTIF_XCLIENT_LONG(xevent,i) \
721 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
723 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
724 #define MOTIF_UNPACK_SHORT(xevent,i) \
725 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
726 #define MOTIF_UNPACK_LONG(xevent,i) \
727 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
729 /***** Dest side ***********/
731 /* Property placed on source windows */
732 typedef struct _MotifDragInitiatorInfo {
734 guint8 protocol_version;
735 guint16 targets_index;
736 guint32 selection_atom;
737 } MotifDragInitiatorInfo;
739 /* Header for target table on the drag window */
740 typedef struct _MotifTargetTableHeader {
742 guchar protocol_version;
745 } MotifTargetTableHeader;
747 /* Property placed on target windows */
748 typedef struct _MotifDragReceiverInfo {
750 guint8 protocol_version;
751 guint8 protocol_style;
753 guint32 proxy_window;
754 guint16 num_drop_sites;
757 } MotifDragReceiverInfo;
759 /* Target table handling */
761 static GdkFilterReturn
762 motif_drag_window_filter (GdkXEvent *xevent,
766 XEvent *xev = (XEvent *)xevent;
767 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
768 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
770 switch (xev->xany.type)
773 display_x11->motif_drag_window = None;
774 display_x11->motif_drag_gdk_window = NULL;
777 if (display_x11->motif_target_lists &&
778 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
779 motif_read_target_table (display);
782 return GDK_FILTER_REMOVE;
786 motif_lookup_drag_window (GdkDisplay *display,
787 Display *lookup_xdisplay)
789 Window retval = None;
790 gulong bytes_after, nitems;
795 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
796 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
798 XA_WINDOW, &type, &format, &nitems, &bytes_after,
801 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
803 retval = *(Window *)data;
805 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
814 /* Finds the window where global Motif drag information is stored.
815 * If it doesn't exist and 'create' is TRUE, create one.
818 motif_find_drag_window (GdkDisplay *display,
821 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
823 if (!display_x11->motif_drag_window)
825 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
826 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
828 if (!display_x11->motif_drag_window && create)
830 /* Create a persistant window. (Copied from LessTif) */
832 Display *persistant_xdisplay;
833 XSetWindowAttributes attr;
834 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
835 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
837 XGrabServer (persistant_xdisplay);
839 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
841 if (!display_x11->motif_drag_window)
843 attr.override_redirect = True;
844 attr.event_mask = PropertyChangeMask;
846 display_x11->motif_drag_window =
847 XCreateWindow (persistant_xdisplay,
848 RootWindow (persistant_xdisplay, 0),
849 -100, -100, 10, 10, 0, 0,
850 InputOnly, CopyFromParent,
851 (CWOverrideRedirect | CWEventMask), &attr);
854 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
856 XChangeProperty (persistant_xdisplay,
857 RootWindow (persistant_xdisplay, 0),
858 motif_drag_window_atom, XA_WINDOW,
860 (guchar *)&motif_drag_window_atom, 1);
863 XUngrabServer (persistant_xdisplay);
864 XCloseDisplay (persistant_xdisplay);
867 /* There is a miniscule race condition here if the drag window
868 * gets destroyed exactly now.
870 if (display_x11->motif_drag_window)
872 display_x11->motif_drag_gdk_window =
873 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
874 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
875 motif_drag_window_filter,
880 return display_x11->motif_drag_window;
884 motif_read_target_table (GdkDisplay *display)
886 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
887 gulong bytes_after, nitems;
892 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
894 if (display_x11->motif_target_lists)
896 for (i=0; i<display_x11->motif_n_target_lists; i++)
897 g_list_free (display_x11->motif_target_lists[i]);
899 g_free (display_x11->motif_target_lists);
900 display_x11->motif_target_lists = NULL;
901 display_x11->motif_n_target_lists = 0;
904 if (motif_find_drag_window (display, FALSE))
906 MotifTargetTableHeader *header = NULL;
907 guchar *target_bytes = NULL;
909 gboolean success = FALSE;
911 gdk_error_trap_push ();
912 XGetWindowProperty (display_x11->xdisplay,
913 display_x11->motif_drag_window,
914 motif_drag_targets_atom,
915 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
916 motif_drag_targets_atom,
917 &type, &format, &nitems, &bytes_after,
920 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
923 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
924 header->total_size = card32_to_host (header->total_size, header->byte_order);
926 gdk_error_trap_push ();
927 XGetWindowProperty (display_x11->xdisplay,
928 display_x11->motif_drag_window,
929 motif_drag_targets_atom,
930 (sizeof(MotifTargetTableHeader)+3)/4,
931 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
933 motif_drag_targets_atom, &type, &format, &nitems,
934 &bytes_after, &target_bytes);
936 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
937 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
940 display_x11->motif_n_target_lists = header->n_lists;
941 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
944 for (i=0; i<header->n_lists; i++)
949 if (p + sizeof(guint16) - target_bytes > nitems)
952 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
954 /* We need to make a copy of the targets, since it may
957 targets = g_new (guint32, n_targets);
958 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
960 p += sizeof(guint16) + n_targets * sizeof(guint32);
961 if (p - target_bytes > nitems)
964 for (j=0; j<n_targets; j++)
965 display_x11->motif_target_lists[i] =
966 g_list_prepend (display_x11->motif_target_lists[i],
967 GUINT_TO_POINTER (card32_to_host (targets[j],
968 header->byte_order)));
970 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
980 XFree (target_bytes);
984 if (display_x11->motif_target_lists)
986 g_free (display_x11->motif_target_lists);
987 display_x11->motif_target_lists = NULL;
988 display_x11->motif_n_target_lists = 0;
990 g_warning ("Error reading Motif target table\n");
996 targets_sort_func (gconstpointer a, gconstpointer b)
998 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
999 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1002 /* Check if given (sorted) list is in the targets table */
1004 motif_target_table_check (GdkDisplay *display,
1007 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1008 GList *tmp_list1, *tmp_list2;
1011 for (i=0; i<display_x11->motif_n_target_lists; i++)
1013 tmp_list1 = display_x11->motif_target_lists[i];
1016 while (tmp_list1 && tmp_list2)
1018 if (tmp_list1->data != tmp_list2->data)
1021 tmp_list1 = tmp_list1->next;
1022 tmp_list2 = tmp_list2->next;
1024 if (!tmp_list1 && !tmp_list2) /* Found it */
1032 motif_add_to_target_table (GdkDisplay *display,
1033 GList *targets) /* targets is list of GdkAtom */
1035 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1036 GList *sorted = NULL;
1041 /* make a sorted copy of the list */
1045 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1046 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1047 targets = targets->next;
1050 /* First check if it is there already */
1052 if (display_x11->motif_target_lists)
1053 index = motif_target_table_check (display, sorted);
1055 /* We need to grab the server while doing this, to ensure
1061 /* We need to make sure that it exists _before_ we grab the
1062 * server, since we can't open a new connection after we
1065 motif_find_drag_window (display, TRUE);
1067 gdk_x11_display_grab (display);
1068 motif_read_target_table (display);
1070 /* Check again, in case it was added in the meantime */
1072 if (display_x11->motif_target_lists)
1073 index = motif_target_table_check (display, sorted);
1077 guint32 total_size = 0;
1081 MotifTargetTableHeader *header;
1083 if (!display_x11->motif_target_lists)
1085 display_x11->motif_target_lists = g_new (GList *, 1);
1086 display_x11->motif_n_target_lists = 1;
1090 display_x11->motif_n_target_lists++;
1091 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1092 sizeof(GList *) * display_x11->motif_n_target_lists);
1094 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1096 index = display_x11->motif_n_target_lists - 1;
1098 total_size = sizeof (MotifTargetTableHeader);
1099 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1100 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1102 data = g_malloc (total_size);
1104 header = (MotifTargetTableHeader *)data;
1105 p = data + sizeof(MotifTargetTableHeader);
1107 header->byte_order = local_byte_order;
1108 header->protocol_version = 0;
1109 header->n_lists = display_x11->motif_n_target_lists;
1110 header->total_size = total_size;
1112 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1114 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1115 guint32 *targets = g_new (guint32, n_targets);
1116 guint32 *p32 = targets;
1118 tmp_list = display_x11->motif_target_lists[i];
1121 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1123 tmp_list = tmp_list->next;
1128 p += sizeof(guint16);
1130 memcpy (p, targets, n_targets * sizeof(guint32));
1133 p += sizeof(guint32) * n_targets;
1137 XChangeProperty (display_x11->xdisplay,
1138 display_x11->motif_drag_window,
1139 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1140 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1144 gdk_x11_display_ungrab (display);
1147 g_list_free (sorted);
1151 /* Translate flags */
1154 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1156 guint recommended_op = flags & 0x000f;
1157 guint possible_ops = (flags & 0x0f0) >> 4;
1159 switch (recommended_op)
1162 context->suggested_action = GDK_ACTION_MOVE;
1165 context->suggested_action = GDK_ACTION_COPY;
1168 context->suggested_action = GDK_ACTION_LINK;
1171 context->suggested_action = GDK_ACTION_COPY;
1175 context->actions = 0;
1176 if (possible_ops & XmDROP_MOVE)
1177 context->actions |= GDK_ACTION_MOVE;
1178 if (possible_ops & XmDROP_COPY)
1179 context->actions |= GDK_ACTION_COPY;
1180 if (possible_ops & XmDROP_LINK)
1181 context->actions |= GDK_ACTION_LINK;
1185 motif_dnd_get_flags (GdkDragContext *context)
1189 switch (context->suggested_action)
1191 case GDK_ACTION_MOVE:
1192 flags = XmDROP_MOVE;
1194 case GDK_ACTION_COPY:
1195 flags = XmDROP_COPY;
1197 case GDK_ACTION_LINK:
1198 flags = XmDROP_LINK;
1201 flags = XmDROP_NOOP;
1205 if (context->actions & GDK_ACTION_MOVE)
1206 flags |= XmDROP_MOVE << 8;
1207 if (context->actions & GDK_ACTION_COPY)
1208 flags |= XmDROP_COPY << 8;
1209 if (context->actions & GDK_ACTION_LINK)
1210 flags |= XmDROP_LINK << 8;
1218 motif_set_targets (GdkDragContext *context)
1220 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1221 MotifDragInitiatorInfo info;
1223 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1225 info.byte_order = local_byte_order;
1226 info.protocol_version = 0;
1228 info.targets_index = motif_add_to_target_table (display, context->targets);
1233 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1235 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1236 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1240 info.selection_atom = private->motif_selection;
1242 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1243 GDK_DRAWABLE_XID (context->source_window),
1244 private->motif_selection,
1245 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1247 (guchar *)&info, sizeof (info));
1249 private->motif_targets_set = 1;
1253 motif_check_dest (GdkDisplay *display,
1256 gboolean retval = FALSE;
1257 MotifDragReceiverInfo *info;
1260 unsigned long nitems, after;
1261 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1263 gdk_error_trap_push ();
1264 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1265 motif_drag_receiver_info_atom,
1266 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1267 &type, &format, &nitems, &after,
1270 if (gdk_error_trap_pop() == 0)
1274 if ((format == 8) && (nitems == sizeof(*info)))
1276 if ((info->protocol_version == 0) &&
1277 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1278 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1279 (info->protocol_style == XmDRAG_DYNAMIC)))
1285 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1292 return retval ? win : None;
1296 motif_send_enter (GdkDragContext *context,
1299 GdkDragContextPrivateX11 *private = PRIVATE_DATA (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_ENTER;
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);
1314 if (!private->motif_targets_set)
1315 motif_set_targets (context);
1317 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1318 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1320 if (!_gdk_send_xevent (display,
1321 GDK_DRAWABLE_XID (context->dest_window),
1324 g_message ("Send event to %lx failed",
1325 GDK_DRAWABLE_XID (context->dest_window)));
1329 motif_send_leave (GdkDragContext *context,
1332 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, 0) = XmTOP_LEVEL_LEAVE;
1341 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1342 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1343 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1344 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1345 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1346 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1348 if (!_gdk_send_xevent (display,
1349 GDK_DRAWABLE_XID (context->dest_window),
1352 g_message ("Send event to %lx failed",
1353 GDK_DRAWABLE_XID (context->dest_window)));
1357 motif_send_motion (GdkDragContext *context,
1360 GdkDragAction action,
1363 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1364 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1368 xev.xclient.type = ClientMessage;
1369 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1370 xev.xclient.format = 8;
1371 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1373 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1374 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1375 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1376 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1377 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1379 if ((context->suggested_action != private->old_action) ||
1380 (context->actions != private->old_actions))
1382 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1384 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1389 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1391 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1392 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1394 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1398 if (!_gdk_send_xevent (display,
1399 GDK_DRAWABLE_XID (context->dest_window),
1402 g_message ("Send event to %lx failed",
1403 GDK_DRAWABLE_XID (context->dest_window)));
1409 motif_send_drop (GdkDragContext *context, guint32 time)
1411 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1412 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1415 xev.xclient.type = ClientMessage;
1416 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1417 xev.xclient.format = 8;
1418 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1420 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1421 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1422 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1423 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1425 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1426 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1428 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1429 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1431 if (!_gdk_send_xevent (display,
1432 GDK_DRAWABLE_XID (context->dest_window),
1435 g_message ("Send event to %lx failed",
1436 GDK_DRAWABLE_XID (context->dest_window)));
1442 motif_read_initiator_info (GdkDisplay *display,
1443 Window source_window,
1453 MotifDragInitiatorInfo *initiator_info;
1455 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1457 gdk_error_trap_push ();
1458 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1459 0, sizeof(*initiator_info), FALSE,
1460 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1461 &type, &format, &nitems, &bytes_after,
1462 (guchar **)&initiator_info);
1464 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1466 g_warning ("Error reading initiator info\n");
1470 motif_read_target_table (display);
1472 initiator_info->targets_index =
1473 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1474 initiator_info->selection_atom =
1475 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1477 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1479 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1480 XFree (initiator_info);
1484 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1489 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1490 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1491 tmp_list = tmp_list->prev;
1494 #ifdef G_ENABLE_DEBUG
1495 if (_gdk_debug_flags & GDK_DEBUG_DND)
1496 print_target_list (*targets);
1497 #endif /* G_ENABLE_DEBUG */
1499 *selection = initiator_info->selection_atom;
1501 XFree (initiator_info);
1506 static GdkDragContext *
1507 motif_drag_context_new (GdkWindow *dest_window,
1509 guint32 source_window,
1512 GdkDragContext *new_context;
1513 GdkDragContextPrivateX11 *private;
1514 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1515 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1517 /* FIXME, current_dest_drag really shouldn't be NULL'd
1518 * if we error below.
1520 if (display_x11->current_dest_drag != NULL)
1522 if (timestamp >= display_x11->current_dest_drag->start_time)
1524 g_object_unref (display_x11->current_dest_drag);
1525 display_x11->current_dest_drag = NULL;
1531 new_context = gdk_drag_context_new ();
1532 private = PRIVATE_DATA (new_context);
1534 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1535 new_context->is_source = FALSE;
1537 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1538 if (new_context->source_window)
1539 g_object_ref (new_context->source_window);
1542 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1543 if (!new_context->source_window)
1545 g_object_unref (new_context);
1550 new_context->dest_window = dest_window;
1551 g_object_ref (dest_window);
1552 new_context->start_time = timestamp;
1554 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1557 &new_context->targets,
1558 &private->motif_selection))
1560 g_object_unref (new_context);
1568 * The MOTIF drag protocol has no real provisions for distinguishing
1569 * multiple simultaneous drops. If the sources grab the pointer
1570 * when doing drags, that shouldn't happen, in any case. If it
1571 * does, we can't do much except hope for the best.
1574 static GdkFilterReturn
1575 motif_top_level_enter (GdkEvent *event,
1578 guint32 source_window,
1581 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1582 GdkDragContext *new_context;
1584 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1585 flags, timestamp, source_window, atom));
1587 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1589 return GDK_FILTER_REMOVE;
1591 event->dnd.type = GDK_DRAG_ENTER;
1592 event->dnd.context = new_context;
1593 g_object_ref (new_context);
1595 display_x11->current_dest_drag = new_context;
1597 return GDK_FILTER_TRANSLATE;
1600 static GdkFilterReturn
1601 motif_top_level_leave (GdkEvent *event,
1605 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1607 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1610 if ((display_x11->current_dest_drag != NULL) &&
1611 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1612 (timestamp >= display_x11->current_dest_drag->start_time))
1614 event->dnd.type = GDK_DRAG_LEAVE;
1615 /* Pass ownership of context to the event */
1616 event->dnd.context = display_x11->current_dest_drag;
1618 display_x11->current_dest_drag = NULL;
1620 return GDK_FILTER_TRANSLATE;
1623 return GDK_FILTER_REMOVE;
1626 static GdkFilterReturn
1627 motif_motion (GdkEvent *event,
1633 GdkDragContextPrivateX11 *private;
1634 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1636 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1637 flags, timestamp, x_root, y_root));
1639 if ((display_x11->current_dest_drag != NULL) &&
1640 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1641 (timestamp >= display_x11->current_dest_drag->start_time))
1643 private = PRIVATE_DATA (display_x11->current_dest_drag);
1645 event->dnd.type = GDK_DRAG_MOTION;
1646 event->dnd.context = display_x11->current_dest_drag;
1647 g_object_ref (display_x11->current_dest_drag);
1649 event->dnd.time = timestamp;
1651 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1653 event->dnd.x_root = x_root;
1654 event->dnd.y_root = y_root;
1656 private->last_x = x_root;
1657 private->last_y = y_root;
1659 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1661 return GDK_FILTER_TRANSLATE;
1664 return GDK_FILTER_REMOVE;
1667 static GdkFilterReturn
1668 motif_operation_changed (GdkEvent *event,
1672 GdkDragContextPrivateX11 *private;
1673 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1674 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1677 if ((display_x11->current_dest_drag != NULL) &&
1678 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1679 (timestamp >= display_x11->current_dest_drag->start_time))
1681 event->dnd.type = GDK_DRAG_MOTION;
1682 event->dnd.send_event = FALSE;
1683 event->dnd.context = display_x11->current_dest_drag;
1684 g_object_ref (display_x11->current_dest_drag);
1686 event->dnd.time = timestamp;
1687 private = PRIVATE_DATA (display_x11->current_dest_drag);
1689 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1691 event->dnd.x_root = private->last_x;
1692 event->dnd.y_root = private->last_y;
1694 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1696 return GDK_FILTER_TRANSLATE;
1699 return GDK_FILTER_REMOVE;
1702 static GdkFilterReturn
1703 motif_drop_start (GdkEvent *event,
1706 guint32 source_window,
1711 GdkDragContext *new_context;
1712 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1714 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1715 flags, timestamp, x_root, y_root, source_window, atom));
1717 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1719 return GDK_FILTER_REMOVE;
1721 motif_dnd_translate_flags (new_context, flags);
1723 event->dnd.type = GDK_DROP_START;
1724 event->dnd.context = new_context;
1725 event->dnd.time = timestamp;
1726 event->dnd.x_root = x_root;
1727 event->dnd.y_root = y_root;
1729 g_object_ref (new_context);
1730 display_x11->current_dest_drag = new_context;
1732 return GDK_FILTER_TRANSLATE;
1735 static GdkFilterReturn
1736 motif_drag_status (GdkEvent *event,
1740 GdkDragContext *context;
1741 GdkDisplay *display;
1744 g_message ("Motif status message: flags %x", flags));
1746 display = gdk_drawable_get_display (event->any.window);
1748 return GDK_FILTER_REMOVE;
1750 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1754 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1755 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1756 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1757 private->drag_status = GDK_DRAG_STATUS_DRAG;
1759 event->dnd.type = GDK_DRAG_STATUS;
1760 event->dnd.send_event = FALSE;
1761 event->dnd.context = context;
1762 g_object_ref (context);
1764 event->dnd.time = timestamp;
1766 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1768 switch (flags & 0x000f)
1771 context->action = 0;
1774 context->action = GDK_ACTION_MOVE;
1777 context->action = GDK_ACTION_COPY;
1780 context->action = GDK_ACTION_LINK;
1785 context->action = 0;
1787 return GDK_FILTER_TRANSLATE;
1789 return GDK_FILTER_REMOVE;
1792 static GdkFilterReturn
1793 motif_dnd_filter (GdkXEvent *xev,
1797 XEvent *xevent = (XEvent *)xev;
1802 guint32 source_window;
1804 gint16 x_root, y_root;
1807 if (!event->any.window ||
1808 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1809 return GDK_FILTER_CONTINUE; /* Not for us */
1811 /* First read some fields common to all Motif DND messages */
1813 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1814 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1815 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1817 is_reply = ((reason & 0x80) != 0);
1819 switch (reason & 0x7f)
1821 case XmTOP_LEVEL_ENTER:
1822 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1823 atom = MOTIF_UNPACK_LONG (xevent, 3);
1824 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1825 case XmTOP_LEVEL_LEAVE:
1826 return motif_top_level_leave (event, flags, timestamp);
1829 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1830 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1833 return motif_motion (event, flags, timestamp, x_root, y_root);
1835 return motif_drag_status (event, flags, timestamp);
1837 case XmDROP_SITE_ENTER:
1838 return motif_drag_status (event, flags, timestamp);
1840 case XmDROP_SITE_LEAVE:
1841 return motif_drag_status (event,
1842 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1845 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1846 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1847 atom = MOTIF_UNPACK_LONG (xevent, 3);
1848 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1851 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1854 case XmOPERATION_CHANGED:
1856 return motif_operation_changed (event, flags, timestamp);
1858 return motif_drag_status (event, flags, timestamp);
1861 /* To the best of my knowledge, these next two messages are
1862 * not part of the protocol, though they are defined in
1866 case XmDRAG_DROP_FINISH:
1870 return GDK_FILTER_REMOVE;
1873 /*************************************************************
1874 ***************************** XDND **************************
1875 *************************************************************/
1877 /* Utility functions */
1882 GdkDragAction action;
1883 } xdnd_actions_table[] = {
1884 { "XdndActionCopy", None, GDK_ACTION_COPY },
1885 { "XdndActionMove", None, GDK_ACTION_MOVE },
1886 { "XdndActionLink", None, GDK_ACTION_LINK },
1887 { "XdndActionAsk", None, GDK_ACTION_ASK },
1888 { "XdndActionPrivate", None, GDK_ACTION_COPY },
1891 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1892 static gboolean xdnd_actions_initialized = FALSE;
1895 xdnd_initialize_actions (void)
1899 xdnd_actions_initialized = TRUE;
1900 for (i=0; i < xdnd_n_actions; i++)
1901 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1904 static GdkDragAction
1905 xdnd_action_from_atom (GdkDisplay *display,
1908 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
1911 if (!xdnd_actions_initialized)
1912 xdnd_initialize_actions();
1914 for (i=0; i<xdnd_n_actions; i++)
1915 if (atom == xdnd_actions_table[i].atom)
1916 return xdnd_actions_table[i].action;
1922 xdnd_action_to_atom (GdkDisplay *display,
1923 GdkDragAction action)
1927 if (!xdnd_actions_initialized)
1928 xdnd_initialize_actions();
1930 for (i=0; i<xdnd_n_actions; i++)
1931 if (action == xdnd_actions_table[i].action)
1932 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
1939 static GdkFilterReturn
1940 xdnd_status_filter (GdkXEvent *xev,
1944 GdkDisplay *display;
1945 XEvent *xevent = (XEvent *)xev;
1946 guint32 dest_window = xevent->xclient.data.l[0];
1947 guint32 flags = xevent->xclient.data.l[1];
1948 Atom action = xevent->xclient.data.l[4];
1949 GdkDragContext *context;
1951 if (!event->any.window ||
1952 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1953 return GDK_FILTER_CONTINUE; /* Not for us */
1956 g_message ("XdndStatus: dest_window: %#x action: %ld",
1957 dest_window, action));
1959 display = gdk_drawable_get_display (event->any.window);
1960 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1964 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1965 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1966 private->drag_status = GDK_DRAG_STATUS_DRAG;
1968 event->dnd.send_event = FALSE;
1969 event->dnd.type = GDK_DRAG_STATUS;
1970 event->dnd.context = context;
1971 g_object_ref (context);
1973 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1974 if (!(action != 0) != !(flags & 1))
1977 g_warning ("Received status event with flags not corresponding to action!\n"));
1981 context->action = xdnd_action_from_atom (display, action);
1983 return GDK_FILTER_TRANSLATE;
1986 return GDK_FILTER_REMOVE;
1989 static GdkFilterReturn
1990 xdnd_finished_filter (GdkXEvent *xev,
1994 GdkDisplay *display;
1995 XEvent *xevent = (XEvent *)xev;
1996 guint32 dest_window = xevent->xclient.data.l[0];
1997 GdkDragContext *context;
1999 if (!event->any.window ||
2000 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2001 return GDK_FILTER_CONTINUE; /* Not for us */
2004 g_message ("XdndFinished: dest_window: %#x", dest_window));
2006 display = gdk_drawable_get_display (event->any.window);
2007 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2011 event->dnd.type = GDK_DROP_FINISHED;
2012 event->dnd.context = context;
2013 g_object_ref (context);
2015 return GDK_FILTER_TRANSLATE;
2018 return GDK_FILTER_REMOVE;
2022 xdnd_set_targets (GdkDragContext *context)
2024 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2026 GList *tmp_list = context->targets;
2028 gint n_atoms = g_list_length (context->targets);
2029 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2031 atomlist = g_new (Atom, n_atoms);
2035 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2036 tmp_list = tmp_list->next;
2040 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2041 GDK_DRAWABLE_XID (context->source_window),
2042 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2043 XA_ATOM, 32, PropModeReplace,
2044 (guchar *)atomlist, n_atoms);
2048 private->xdnd_targets_set = 1;
2052 xdnd_set_actions (GdkDragContext *context)
2054 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2059 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2061 if (!xdnd_actions_initialized)
2062 xdnd_initialize_actions();
2064 actions = context->actions;
2066 for (i=0; i<xdnd_n_actions; i++)
2068 if (actions & xdnd_actions_table[i].action)
2070 actions &= ~xdnd_actions_table[i].action;
2075 atomlist = g_new (Atom, n_atoms);
2077 actions = context->actions;
2079 for (i=0; i<xdnd_n_actions; i++)
2081 if (actions & xdnd_actions_table[i].action)
2083 actions &= ~xdnd_actions_table[i].action;
2084 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2089 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2090 GDK_DRAWABLE_XID (context->source_window),
2091 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2092 XA_ATOM, 32, PropModeReplace,
2093 (guchar *)atomlist, n_atoms);
2097 private->xdnd_actions_set = 1;
2098 private->xdnd_actions = context->actions;
2102 send_xevent_async_cb (Window window,
2106 GdkDragContext *context = data;
2108 g_message ("Got async callback for #%lx, success = %d",
2111 /* On failure, we immediately continue with the protocol
2112 * so we don't end up blocking for a timeout
2115 context->dest_window &&
2116 window == GDK_WINDOW_XID (context->dest_window))
2118 GdkEvent temp_event;
2119 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2121 g_object_unref (context->dest_window);
2122 context->dest_window = NULL;
2123 context->action = 0;
2125 private->drag_status = GDK_DRAG_STATUS_DRAG;
2127 temp_event.dnd.type = GDK_DRAG_STATUS;
2128 temp_event.dnd.window = context->source_window;
2129 temp_event.dnd.send_event = TRUE;
2130 temp_event.dnd.context = context;
2131 temp_event.dnd.time = GDK_CURRENT_TIME;
2133 gdk_event_put (&temp_event);
2136 g_object_unref (context);
2141 gdk_drag_context_get_display (GdkDragContext *context)
2143 if (context->source_window)
2144 return GDK_DRAWABLE_DISPLAY (context->source_window);
2145 else if (context->dest_window)
2146 return GDK_DRAWABLE_DISPLAY (context->dest_window);
2148 g_assert_not_reached ();
2153 send_xevent_async (GdkDragContext *context,
2159 GdkDisplay *display = gdk_drag_context_get_display (context);
2161 g_object_ref (context);
2163 _gdk_send_xevent_async (display, window, propagate, event_mask, event_send,
2164 send_xevent_async_cb, context);
2168 xdnd_send_xevent (GdkDragContext *context,
2173 GdkDisplay *display = gdk_drag_context_get_display (context);
2177 /* We short-circuit messages to ourselves */
2178 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN &&
2179 event_send->xany.type == ClientMessage)
2183 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2185 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2186 event_send->xclient.message_type)
2188 GdkEvent temp_event;
2189 temp_event.any.window = window;
2191 if ((*xdnd_filters[i].func) (event_send, &temp_event, NULL) == GDK_FILTER_TRANSLATE)
2192 gdk_event_put (&temp_event);
2199 xwindow = GDK_WINDOW_XWINDOW (window);
2201 if (_gdk_x11_display_is_root_window (display, xwindow))
2202 event_mask = ButtonPressMask;
2206 send_xevent_async (context, xwindow, propagate, event_mask, event_send);
2212 xdnd_send_enter (GdkDragContext *context)
2215 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2216 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2218 xev.xclient.type = ClientMessage;
2219 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2220 xev.xclient.format = 32;
2221 xev.xclient.window = private->drop_xid ?
2223 GDK_DRAWABLE_XID (context->dest_window);
2224 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2225 xev.xclient.data.l[1] = (3 << 24); /* version */
2226 xev.xclient.data.l[2] = 0;
2227 xev.xclient.data.l[3] = 0;
2228 xev.xclient.data.l[4] = 0;
2230 if (!private->xdnd_selection)
2231 private->xdnd_selection = gdk_x11_get_xatom_by_name_for_display (display, "XdndSelection");
2233 if (g_list_length (context->targets) > 3)
2235 if (!private->xdnd_targets_set)
2236 xdnd_set_targets (context);
2237 xev.xclient.data.l[1] |= 1;
2241 GList *tmp_list = context->targets;
2246 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2247 GDK_POINTER_TO_ATOM (tmp_list->data));
2248 tmp_list = tmp_list->next;
2253 if (!xdnd_send_xevent (context, context->dest_window,
2257 g_message ("Send event to %lx failed",
2258 GDK_DRAWABLE_XID (context->dest_window)));
2259 g_object_unref (context->dest_window);
2260 context->dest_window = NULL;
2265 xdnd_send_leave (GdkDragContext *context)
2267 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2270 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2272 xev.xclient.type = ClientMessage;
2273 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2274 xev.xclient.format = 32;
2275 xev.xclient.window = private->drop_xid ?
2277 GDK_DRAWABLE_XID (context->dest_window);
2278 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2279 xev.xclient.data.l[1] = 0;
2280 xev.xclient.data.l[2] = 0;
2281 xev.xclient.data.l[3] = 0;
2282 xev.xclient.data.l[4] = 0;
2284 if (!xdnd_send_xevent (context, context->dest_window,
2288 g_message ("Send event to %lx failed",
2289 GDK_DRAWABLE_XID (context->dest_window)));
2290 g_object_unref (context->dest_window);
2291 context->dest_window = NULL;
2296 xdnd_send_drop (GdkDragContext *context, guint32 time)
2298 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2299 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2302 xev.xclient.type = ClientMessage;
2303 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2304 xev.xclient.format = 32;
2305 xev.xclient.window = private->drop_xid ?
2307 GDK_DRAWABLE_XID (context->dest_window);
2308 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2309 xev.xclient.data.l[1] = 0;
2310 xev.xclient.data.l[2] = time;
2311 xev.xclient.data.l[3] = 0;
2312 xev.xclient.data.l[4] = 0;
2314 if (!xdnd_send_xevent (context, context->dest_window,
2318 g_message ("Send event to %lx failed",
2319 GDK_DRAWABLE_XID (context->dest_window)));
2320 g_object_unref (context->dest_window);
2321 context->dest_window = NULL;
2326 xdnd_send_motion (GdkDragContext *context,
2329 GdkDragAction action,
2332 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2333 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2336 xev.xclient.type = ClientMessage;
2337 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2338 xev.xclient.format = 32;
2339 xev.xclient.window = private->drop_xid ?
2341 GDK_DRAWABLE_XID (context->dest_window);
2342 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2343 xev.xclient.data.l[1] = 0;
2344 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2345 xev.xclient.data.l[3] = time;
2346 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2348 if (!xdnd_send_xevent (context, context->dest_window,
2352 g_message ("Send event to %lx failed",
2353 GDK_DRAWABLE_XID (context->dest_window)));
2354 g_object_unref (context->dest_window);
2355 context->dest_window = NULL;
2357 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2361 xdnd_check_dest (GdkDisplay *display,
2364 gboolean retval = FALSE;
2367 unsigned long nitems, after;
2371 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2372 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2376 gdk_error_trap_push ();
2378 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2380 1, False, AnyPropertyType,
2381 &type, &format, &nitems, &after,
2382 (guchar **)&proxy_data) == Success)
2386 if ((format == 32) && (nitems == 1))
2388 proxy = *proxy_data;
2392 g_warning ("Invalid XdndOwner "
2393 "property on window %ld\n", win));
2398 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2400 1, False, AnyPropertyType,
2401 &type, &format, &nitems, &after,
2402 (guchar **)&version) == Success) &&
2405 if ((format == 32) && (nitems == 1))
2412 g_warning ("Invalid XdndAware property on window %ld\n", win));
2418 gdk_error_trap_pop ();
2420 return retval ? (proxy ? proxy : win) : None;
2426 xdnd_read_actions (GdkDragContext *context)
2428 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2431 gulong nitems, after;
2436 /* Get the XdndActionList, if set */
2438 gdk_error_trap_push ();
2440 if (XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2441 GDK_DRAWABLE_XID (context->source_window),
2442 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2444 False, XA_ATOM, &type, &format, &nitems,
2445 &after, (guchar **)&data) == Success &&
2448 context->actions = 0;
2450 for (i=0; i<nitems; i++)
2451 context->actions |= xdnd_action_from_atom (display, data[i]);
2453 (PRIVATE_DATA (context))->xdnd_have_actions = TRUE;
2455 #ifdef G_ENABLE_DEBUG
2456 if (_gdk_debug_flags & GDK_DEBUG_DND)
2458 GString *action_str = g_string_new (NULL);
2459 if (context->actions & GDK_ACTION_MOVE)
2460 g_string_append(action_str, "MOVE ");
2461 if (context->actions & GDK_ACTION_COPY)
2462 g_string_append(action_str, "COPY ");
2463 if (context->actions & GDK_ACTION_LINK)
2464 g_string_append(action_str, "LINK ");
2465 if (context->actions & GDK_ACTION_ASK)
2466 g_string_append(action_str, "ASK ");
2468 g_message("Xdnd actions = %s", action_str->str);
2469 g_string_free (action_str, TRUE);
2471 #endif /* G_ENABLE_DEBUG */
2476 gdk_error_trap_pop ();
2479 /* We have to make sure that the XdndActionList we keep internally
2480 * is up to date with the XdndActionList on the source window
2481 * because we get no notification, because Xdnd wasn't meant
2482 * to continually send actions. So we select on PropertyChangeMask
2483 * and add this filter.
2485 static GdkFilterReturn
2486 xdnd_source_window_filter (GdkXEvent *xev,
2490 XEvent *xevent = (XEvent *)xev;
2491 GdkDragContext *context = cb_data;
2492 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2494 if ((xevent->xany.type == PropertyNotify) &&
2495 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2497 xdnd_read_actions (context);
2499 return GDK_FILTER_REMOVE;
2502 return GDK_FILTER_CONTINUE;
2506 xdnd_manage_source_filter (GdkDragContext *context,
2508 gboolean add_filter)
2510 gdk_error_trap_push ();
2512 if (!GDK_WINDOW_DESTROYED (window))
2516 gdk_window_set_events (window,
2517 gdk_window_get_events (window) |
2518 GDK_PROPERTY_CHANGE_MASK);
2519 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2524 gdk_window_remove_filter (window,
2525 xdnd_source_window_filter,
2527 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2528 * but we might want it for other reasons. (Like
2529 * INCR selection transactions).
2534 gdk_display_sync (gdk_drawable_get_display (window));
2535 gdk_error_trap_pop ();
2538 static GdkFilterReturn
2539 xdnd_enter_filter (GdkXEvent *xev,
2543 GdkDisplay *display;
2544 GdkDisplayX11 *display_x11;
2545 XEvent *xevent = (XEvent *)xev;
2546 GdkDragContext *new_context;
2551 gulong nitems, after;
2554 guint32 source_window;
2558 if (!event->any.window ||
2559 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2560 return GDK_FILTER_CONTINUE; /* Not for us */
2562 source_window = xevent->xclient.data.l[0];
2563 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2564 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2566 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2567 display_x11 = GDK_DISPLAY_X11 (display);
2570 g_message ("XdndEnter: source_window: %#x, version: %#x",
2571 source_window, version));
2575 /* Old source ignore */
2576 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2577 return GDK_FILTER_REMOVE;
2580 if (display_x11->current_dest_drag != NULL)
2582 g_object_unref (display_x11->current_dest_drag);
2583 display_x11->current_dest_drag = NULL;
2586 new_context = gdk_drag_context_new ();
2587 new_context->protocol = GDK_DRAG_PROTO_XDND;
2588 new_context->is_source = FALSE;
2590 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2591 if (new_context->source_window)
2592 g_object_ref (new_context->source_window);
2595 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2596 if (!new_context->source_window)
2598 g_object_unref (new_context);
2599 return GDK_FILTER_REMOVE;
2602 new_context->dest_window = event->any.window;
2603 g_object_ref (new_context->dest_window);
2605 new_context->targets = NULL;
2608 gdk_error_trap_push ();
2609 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2611 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2613 False, XA_ATOM, &type, &format, &nitems,
2614 &after, (guchar **)&data);
2616 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2618 g_object_unref (new_context);
2619 return GDK_FILTER_REMOVE;
2622 for (i=0; i<nitems; i++)
2623 new_context->targets =
2624 g_list_append (new_context->targets,
2625 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2633 if (xevent->xclient.data.l[2+i])
2634 new_context->targets =
2635 g_list_append (new_context->targets,
2636 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2637 xevent->xclient.data.l[2+i])));
2640 #ifdef G_ENABLE_DEBUG
2641 if (_gdk_debug_flags & GDK_DEBUG_DND)
2642 print_target_list (new_context->targets);
2643 #endif /* G_ENABLE_DEBUG */
2645 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2646 xdnd_read_actions (new_context);
2648 event->dnd.type = GDK_DRAG_ENTER;
2649 event->dnd.context = new_context;
2650 g_object_ref (new_context);
2652 display_x11->current_dest_drag = new_context;
2654 (PRIVATE_DATA (new_context))->xdnd_selection =
2655 gdk_x11_get_xatom_by_name_for_display (display, "XdndSelection");
2656 return GDK_FILTER_TRANSLATE;
2659 static GdkFilterReturn
2660 xdnd_leave_filter (GdkXEvent *xev,
2664 XEvent *xevent = (XEvent *)xev;
2665 guint32 source_window = xevent->xclient.data.l[0];
2666 GdkDisplayX11 *display_x11;
2668 if (!event->any.window ||
2669 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2670 return GDK_FILTER_CONTINUE; /* Not for us */
2673 g_message ("XdndLeave: source_window: %#x",
2676 display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
2678 if ((display_x11->current_dest_drag != NULL) &&
2679 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2680 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2682 event->dnd.type = GDK_DRAG_LEAVE;
2683 /* Pass ownership of context to the event */
2684 event->dnd.context = display_x11->current_dest_drag;
2686 display_x11->current_dest_drag = NULL;
2688 return GDK_FILTER_TRANSLATE;
2691 return GDK_FILTER_REMOVE;
2694 static GdkFilterReturn
2695 xdnd_position_filter (GdkXEvent *xev,
2699 XEvent *xevent = (XEvent *)xev;
2700 guint32 source_window = xevent->xclient.data.l[0];
2701 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2702 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2703 guint32 time = xevent->xclient.data.l[3];
2704 Atom action = xevent->xclient.data.l[4];
2706 GdkDisplay *display;
2707 GdkDisplayX11 *display_x11;
2709 if (!event->any.window ||
2710 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2711 return GDK_FILTER_CONTINUE; /* Not for us */
2714 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2715 source_window, x_root, y_root, time, action));
2717 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2718 display_x11 = GDK_DISPLAY_X11 (display);
2720 if ((display_x11->current_dest_drag != NULL) &&
2721 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2722 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2724 event->dnd.type = GDK_DRAG_MOTION;
2725 event->dnd.context = display_x11->current_dest_drag;
2726 g_object_ref (display_x11->current_dest_drag);
2728 event->dnd.time = time;
2730 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2732 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2733 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2735 event->dnd.x_root = x_root;
2736 event->dnd.y_root = y_root;
2738 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2739 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
2741 return GDK_FILTER_TRANSLATE;
2744 return GDK_FILTER_REMOVE;
2747 static GdkFilterReturn
2748 xdnd_drop_filter (GdkXEvent *xev,
2752 XEvent *xevent = (XEvent *)xev;
2753 guint32 source_window = xevent->xclient.data.l[0];
2754 guint32 time = xevent->xclient.data.l[2];
2755 GdkDisplayX11 *display_x11;
2757 if (!event->any.window ||
2758 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2759 return GDK_FILTER_CONTINUE; /* Not for us */
2762 g_message ("XdndDrop: source_window: %#x time: %d",
2763 source_window, time));
2765 display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
2767 if ((display_x11->current_dest_drag != NULL) &&
2768 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2769 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2771 GdkDragContextPrivateX11 *private;
2772 private = PRIVATE_DATA (display_x11->current_dest_drag);
2774 event->dnd.type = GDK_DROP_START;
2776 event->dnd.context = display_x11->current_dest_drag;
2777 g_object_ref (display_x11->current_dest_drag);
2779 event->dnd.time = time;
2780 event->dnd.x_root = private->last_x;
2781 event->dnd.y_root = private->last_y;
2783 return GDK_FILTER_TRANSLATE;
2786 return GDK_FILTER_REMOVE;
2789 /*************************************************************
2790 ************************** Public API ***********************
2791 *************************************************************/
2793 _gdk_dnd_init (GdkDisplay *display)
2798 gdk_display_add_client_message_filter (
2800 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2801 motif_dnd_filter, NULL);
2803 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2805 gdk_display_add_client_message_filter (
2807 gdk_atom_intern (xdnd_filters[i].atom_name, FALSE),
2808 xdnd_filters[i].func, NULL);
2815 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2817 if (context->dest_window)
2819 switch (context->protocol)
2821 case GDK_DRAG_PROTO_MOTIF:
2822 motif_send_leave (context, time);
2824 case GDK_DRAG_PROTO_XDND:
2825 xdnd_send_leave (context);
2827 case GDK_DRAG_PROTO_ROOTWIN:
2828 case GDK_DRAG_PROTO_NONE:
2833 g_object_unref (context->dest_window);
2834 context->dest_window = NULL;
2840 * @window: the source window for this drag.
2841 * @targets: the list of offered targets.
2843 * Starts a drag and creates a new drag context for it.
2845 * This function is called by the drag source.
2847 * Return value: a newly created #GdkDragContext.
2850 gdk_drag_begin (GdkWindow *window,
2853 GdkDragContext *new_context;
2855 g_return_val_if_fail (window != NULL, NULL);
2857 new_context = gdk_drag_context_new ();
2858 new_context->is_source = TRUE;
2859 new_context->source_window = window;
2860 g_object_ref (window);
2862 new_context->targets = g_list_copy (targets);
2863 new_context->actions = 0;
2869 * gdk_drag_get_protocol_for_display:
2870 * @display: the #GdkDisplay where the destination window resides
2871 * @xid: the X id of the destination window.
2872 * @protocol: location where the supported DND protocol is returned.
2873 * @returns: the X id of the window where the drop should happen. This
2874 * may be @xid or the X id of a proxy window, or None if @xid doesn't
2875 * support Drag and Drop.
2877 * Finds out the DND protocol supported by a window.
2882 gdk_drag_get_protocol_for_display (GdkDisplay *display,
2884 GdkDragProtocol *protocol)
2887 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
2889 if ((retval = xdnd_check_dest (display, xid)))
2891 *protocol = GDK_DRAG_PROTO_XDND;
2892 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2895 else if ((retval = motif_check_dest (display, xid)))
2897 *protocol = GDK_DRAG_PROTO_MOTIF;
2898 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2903 /* Check if this is a root window */
2905 gboolean rootwin = FALSE;
2908 unsigned long nitems, after;
2909 unsigned char *data;
2911 if (_gdk_x11_display_is_root_window (display, (Window) xid))
2914 gdk_error_trap_push ();
2918 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
2919 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
2920 0, 0, False, AnyPropertyType,
2921 &type, &format, &nitems, &after, &data) == Success &&
2929 /* Until I find out what window manager the next one is for,
2930 * I'm leaving it commented out. It's supported in the
2931 * xscreensaver sources, though.
2936 if (XGetWindowProperty (gdk_display, win,
2937 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
2938 0, 0, False, AnyPropertyType,
2939 &type, &format, &nitems, &data) &&
2945 gdk_error_trap_pop ();
2949 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2954 *protocol = GDK_DRAG_PROTO_NONE;
2958 static GdkWindowCache *
2959 drag_context_find_window_cache (GdkDragContext *context,
2962 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2964 GdkWindowCache *cache;
2966 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
2968 cache = tmp_list->data;
2969 if (cache->screen == screen)
2973 cache = gdk_window_cache_new (screen);
2974 private->window_caches = g_slist_prepend (private->window_caches, cache);
2980 * gdk_drag_find_window_for_screen:
2981 * @context: a #GdkDragContext
2982 * @drag_window: a window which may be at the pointer position, but
2983 * should be ignored, since it is put up by the drag source as an icon.
2984 * @screen: the screen where the destination window is sought.
2985 * @x_root: the x position of the pointer in root coordinates.
2986 * @y_root: the y position of the pointer in root coordinates.
2987 * @dest_window: location to store the destination window in.
2988 * @protocol: location to store the DND protocol in.
2990 * Finds the destination window and DND protocol to use at the
2991 * given pointer position.
2993 * This function is called by the drag source to obtain the
2994 * @dest_window and @protocol parameters for gdk_drag_motion().
2999 gdk_drag_find_window_for_screen (GdkDragContext *context,
3000 GdkWindow *drag_window,
3004 GdkWindow **dest_window,
3005 GdkDragProtocol *protocol)
3007 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3008 GdkWindowCache *window_cache;
3009 GdkDisplay *display;
3012 g_return_if_fail (context != NULL);
3014 display = GDK_WINDOW_DISPLAY (context->source_window);
3016 window_cache = drag_context_find_window_cache (context, screen);
3018 dest = get_client_window_at_coords (window_cache,
3020 GDK_DRAWABLE_XID (drag_window) : None,
3023 if (private->dest_xid != dest)
3026 private->dest_xid = dest;
3028 /* Check if new destination accepts drags, and which protocol */
3030 /* There is some ugliness here. We actually need to pass
3031 * _three_ pieces of information to drag_motion - dest_window,
3032 * protocol, and the XID of the unproxied window. The first
3033 * two are passed explicitely, the third implicitly through
3034 * protocol->dest_xid.
3036 if ((recipient = gdk_drag_get_protocol_for_display (display, dest, protocol)))
3038 *dest_window = gdk_window_lookup_for_display (display, recipient);
3040 g_object_ref (*dest_window);
3042 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3045 *dest_window = NULL;
3049 *dest_window = context->dest_window;
3051 g_object_ref (*dest_window);
3052 *protocol = context->protocol;
3058 * @context: a #GdkDragContext.
3059 * @dest_window: the new destination window, obtained by
3060 * gdk_drag_find_window().
3061 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3062 * @x_root: the x position of the pointer in root coordinates.
3063 * @y_root: the y position of the pointer in root coordinates.
3064 * @suggested_action: the suggested action.
3065 * @possible_actions: the possible actions.
3066 * @time_: the timestamp for this operation.
3068 * Updates the drag context when the pointer moves or the
3069 * set of actions changes.
3071 * This function is called by the drag source.
3073 * Return value: FIXME
3076 gdk_drag_motion (GdkDragContext *context,
3077 GdkWindow *dest_window,
3078 GdkDragProtocol protocol,
3081 GdkDragAction suggested_action,
3082 GdkDragAction possible_actions,
3085 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3087 g_return_val_if_fail (context != NULL, FALSE);
3089 /* When we have a Xdnd target, make sure our XdndActionList
3090 * matches the current actions;
3092 private->old_actions = context->actions;
3093 context->actions = possible_actions;
3095 if ((protocol == GDK_DRAG_PROTO_XDND) &&
3096 (!private->xdnd_actions_set ||
3097 private->xdnd_actions != possible_actions))
3098 xdnd_set_actions (context);
3100 if (context->dest_window != dest_window)
3102 GdkEvent temp_event;
3104 /* Send a leave to the last destination */
3105 gdk_drag_do_leave (context, time);
3106 private->drag_status = GDK_DRAG_STATUS_DRAG;
3108 /* Check if new destination accepts drags, and which protocol */
3112 context->dest_window = dest_window;
3113 private->drop_xid = private->dest_xid;
3114 g_object_ref (context->dest_window);
3115 context->protocol = protocol;
3119 case GDK_DRAG_PROTO_MOTIF:
3120 motif_send_enter (context, time);
3123 case GDK_DRAG_PROTO_XDND:
3124 xdnd_send_enter (context);
3127 case GDK_DRAG_PROTO_ROOTWIN:
3128 case GDK_DRAG_PROTO_NONE:
3132 private->old_action = suggested_action;
3133 context->suggested_action = suggested_action;
3134 private->old_actions = possible_actions;
3138 context->dest_window = NULL;
3139 private->drop_xid = None;
3140 context->action = 0;
3143 /* Push a status event, to let the client know that
3147 temp_event.dnd.type = GDK_DRAG_STATUS;
3148 temp_event.dnd.window = context->source_window;
3149 /* We use this to signal a synthetic status. Perhaps
3150 * we should use an extra field...
3152 temp_event.dnd.send_event = TRUE;
3154 temp_event.dnd.context = context;
3155 temp_event.dnd.time = time;
3157 gdk_event_put (&temp_event);
3161 private->old_action = context->suggested_action;
3162 context->suggested_action = suggested_action;
3165 /* Send a drag-motion event */
3167 private->last_x = x_root;
3168 private->last_y = y_root;
3170 if (context->dest_window)
3172 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3174 switch (context->protocol)
3176 case GDK_DRAG_PROTO_MOTIF:
3177 motif_send_motion (context, x_root, y_root, suggested_action, time);
3180 case GDK_DRAG_PROTO_XDND:
3181 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3184 case GDK_DRAG_PROTO_ROOTWIN:
3186 GdkEvent temp_event;
3187 /* GTK+ traditionally has used application/x-rootwin-drop,
3188 * but the XDND spec specifies x-rootwindow-drop.
3190 GdkAtom target1 = gdk_atom_intern ("application/x-rootwindow-drop", FALSE);
3191 GdkAtom target2 = gdk_atom_intern ("application/x-rootwin-drop", FALSE);
3193 if (g_list_find (context->targets,
3194 GDK_ATOM_TO_POINTER (target1)) ||
3195 g_list_find (context->targets,
3196 GDK_ATOM_TO_POINTER (target2)))
3197 context->action = context->suggested_action;
3199 context->action = 0;
3201 temp_event.dnd.type = GDK_DRAG_STATUS;
3202 temp_event.dnd.window = context->source_window;
3203 temp_event.dnd.send_event = FALSE;
3204 temp_event.dnd.context = context;
3205 temp_event.dnd.time = time;
3207 gdk_event_put (&temp_event);
3210 case GDK_DRAG_PROTO_NONE:
3211 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3226 * @context: a #GdkDragContext.
3227 * @time_: the timestamp for this operation.
3229 * Drops on the current destination.
3231 * This function is called by the drag source.
3234 gdk_drag_drop (GdkDragContext *context,
3237 g_return_if_fail (context != NULL);
3239 if (context->dest_window)
3241 switch (context->protocol)
3243 case GDK_DRAG_PROTO_MOTIF:
3244 motif_send_leave (context, time);
3245 motif_send_drop (context, time);
3248 case GDK_DRAG_PROTO_XDND:
3249 xdnd_send_drop (context, time);
3252 case GDK_DRAG_PROTO_ROOTWIN:
3253 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3255 case GDK_DRAG_PROTO_NONE:
3256 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3266 * @context: a #GdkDragContext.
3267 * @time_: the timestamp for this operation.
3269 * Aborts a drag without dropping.
3271 * This function is called by the drag source.
3274 gdk_drag_abort (GdkDragContext *context,
3277 g_return_if_fail (context != NULL);
3279 gdk_drag_do_leave (context, time);
3282 /* Destination side */
3286 * @context: a #GdkDragContext.
3287 * @action: the selected action which will be taken when a drop happens,
3288 * or 0 to indicate that a drop will not be accepted.
3289 * @time_: the timestamp for this operation.
3291 * Selects one of the actions offered by the drag source.
3293 * This function is called by the drag destination in response to
3294 * gdk_drag_motion() called by the drag source.
3297 gdk_drag_status (GdkDragContext *context,
3298 GdkDragAction action,
3301 GdkDragContextPrivateX11 *private;
3303 GdkDisplay *display;
3305 g_return_if_fail (context != NULL);
3307 private = PRIVATE_DATA (context);
3308 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3310 context->action = action;
3312 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3314 gboolean need_coords = FALSE;
3316 xev.xclient.type = ClientMessage;
3317 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3318 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3319 xev.xclient.format = 8;
3320 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3322 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3324 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3328 if ((action != 0) != (private->old_action != 0))
3332 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3336 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3340 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3345 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3349 case GDK_ACTION_MOVE:
3350 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3352 case GDK_ACTION_COPY:
3353 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3355 case GDK_ACTION_LINK:
3356 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3359 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3364 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3366 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3368 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3372 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3373 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3376 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3378 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3379 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3381 if (!_gdk_send_xevent (display,
3382 GDK_DRAWABLE_XID (context->source_window),
3385 g_message ("Send event to %lx failed",
3386 GDK_DRAWABLE_XID (context->source_window)));
3388 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3390 xev.xclient.type = ClientMessage;
3391 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3392 xev.xclient.format = 32;
3393 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3395 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3396 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3397 xev.xclient.data.l[2] = 0;
3398 xev.xclient.data.l[3] = 0;
3399 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3401 if (!xdnd_send_xevent (context, context->source_window,
3404 g_message ("Send event to %lx failed",
3405 GDK_DRAWABLE_XID (context->source_window)));
3408 private->old_action = action;
3413 * @context: a #GdkDragContext.
3414 * @ok: %TRUE if the drop is accepted.
3415 * @time_: the timestamp for this operation.
3417 * Accepts or rejects a drop.
3419 * This function is called by the drag destination in response
3420 * to a drop initiated by the drag source.
3423 gdk_drop_reply (GdkDragContext *context,
3427 GdkDragContextPrivateX11 *private;
3429 g_return_if_fail (context != NULL);
3431 private = PRIVATE_DATA (context);
3433 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3435 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3438 xev.xclient.type = ClientMessage;
3439 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3440 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3441 xev.xclient.format = 8;
3443 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3444 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3446 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3447 (XmDROP_SITE_VALID << 4) |
3448 (XmDROP_NOOP << 8) |
3451 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3452 (XmNO_DROP_SITE << 4) |
3453 (XmDROP_NOOP << 8) |
3454 (XmDROP_CANCEL << 12);
3455 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3456 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3457 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3458 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3459 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3461 _gdk_send_xevent (display,
3462 GDK_DRAWABLE_XID (context->source_window),
3469 * @context: a #GtkDragContext.
3470 * @success: %TRUE if the data was successfully received.
3471 * @time_: the timestamp for this operation.
3473 * Ends the drag operation after a drop.
3475 * This function is called by the drag destination.
3478 gdk_drop_finish (GdkDragContext *context,
3482 g_return_if_fail (context != NULL);
3484 if (context->protocol == GDK_DRAG_PROTO_XDND)
3486 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3489 xev.xclient.type = ClientMessage;
3490 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3491 xev.xclient.format = 32;
3492 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3494 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3495 xev.xclient.data.l[1] = 0;
3496 xev.xclient.data.l[2] = 0;
3497 xev.xclient.data.l[3] = 0;
3498 xev.xclient.data.l[4] = 0;
3500 if (!xdnd_send_xevent (context, context->source_window,
3503 g_message ("Send event to %lx failed",
3504 GDK_DRAWABLE_XID (context->source_window)));
3510 gdk_window_register_dnd (GdkWindow *window)
3512 static gulong xdnd_version = 3;
3513 MotifDragReceiverInfo info;
3514 Atom motif_drag_receiver_info_atom;
3515 GdkDisplay *display = gdk_drawable_get_display (window);
3517 g_return_if_fail (window != NULL);
3519 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered")))
3522 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3524 /* Set Motif drag receiver information property */
3526 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3527 "_MOTIF_DRAG_RECEIVER_INFO");
3528 info.byte_order = local_byte_order;
3529 info.protocol_version = 0;
3530 info.protocol_style = XmDRAG_DYNAMIC;
3531 info.proxy_window = None;
3532 info.num_drop_sites = 0;
3533 info.total_size = sizeof(info);
3535 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3536 motif_drag_receiver_info_atom,
3537 motif_drag_receiver_info_atom,
3544 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3545 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3546 GDK_DRAWABLE_XID (window),
3547 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3548 XA_ATOM, 32, PropModeReplace,
3549 (guchar *)&xdnd_version, 1);
3553 * gdk_drag_get_selection:
3554 * @context: a #GdkDragContext.
3556 * Returns the selection atom for the current source window.
3558 * Return value: the selection atom.
3561 gdk_drag_get_selection (GdkDragContext *context)
3563 g_return_val_if_fail (context != NULL, GDK_NONE);
3565 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3566 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3567 (PRIVATE_DATA (context))->motif_selection);
3568 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3569 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3570 (PRIVATE_DATA (context))->xdnd_selection);