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;
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,
104 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
107 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
110 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
113 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
116 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
120 static void xdnd_manage_source_filter (GdkDragContext *context,
122 gboolean add_filter);
124 static void gdk_drag_context_init (GdkDragContext *dragcontext);
125 static void gdk_drag_context_class_init (GdkDragContextClass *klass);
126 static void gdk_drag_context_finalize (GObject *object);
128 static gpointer parent_class = NULL;
129 static GList *contexts;
131 const static struct {
132 const char *atom_name;
135 { "XdndEnter", xdnd_enter_filter },
136 { "XdndLeave", xdnd_leave_filter },
137 { "XdndPosition", xdnd_position_filter },
138 { "XdndStatus", xdnd_status_filter },
139 { "XdndFinished", xdnd_finished_filter },
140 { "XdndDrop", xdnd_drop_filter },
144 gdk_drag_context_get_type (void)
146 static GType object_type = 0;
150 static const GTypeInfo object_info =
152 sizeof (GdkDragContextClass),
153 (GBaseInitFunc) NULL,
154 (GBaseFinalizeFunc) NULL,
155 (GClassInitFunc) gdk_drag_context_class_init,
156 NULL, /* class_finalize */
157 NULL, /* class_data */
158 sizeof (GdkDragContext),
160 (GInstanceInitFunc) gdk_drag_context_init,
163 object_type = g_type_register_static (G_TYPE_OBJECT,
172 gdk_drag_context_init (GdkDragContext *dragcontext)
174 GdkDragContextPrivateX11 *private;
176 private = g_new0 (GdkDragContextPrivateX11, 1);
178 dragcontext->windowing_data = private;
180 contexts = g_list_prepend (contexts, dragcontext);
184 gdk_drag_context_class_init (GdkDragContextClass *klass)
186 GObjectClass *object_class = G_OBJECT_CLASS (klass);
188 parent_class = g_type_class_peek_parent (klass);
190 object_class->finalize = gdk_drag_context_finalize;
194 gdk_drag_context_finalize (GObject *object)
196 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
197 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
200 g_list_free (context->targets);
202 if (context->source_window)
204 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
206 xdnd_manage_source_filter (context, context->source_window, FALSE);
208 g_object_unref (context->source_window);
211 if (context->dest_window)
212 g_object_unref (context->dest_window);
214 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
215 gdk_window_cache_destroy (tmp_list->data);
216 g_slist_free (private->window_caches);
218 contexts = g_list_remove (contexts, context);
222 G_OBJECT_CLASS (parent_class)->finalize (object);
228 * gdk_drag_context_new:
230 * Creates a new #GdkDragContext.
232 * Return value: the newly created #GdkDragContext.
235 gdk_drag_context_new (void)
237 return g_object_new (gdk_drag_context_get_type (), NULL);
241 * gdk_drag_context_ref:
242 * @context: a #GdkDragContext.
244 * Deprecated function; use g_object_ref() instead.
247 gdk_drag_context_ref (GdkDragContext *context)
249 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
251 g_object_ref (context);
255 * gdk_drag_context_unref:
256 * @context: a #GdkDragContext.
258 * Deprecated function; use g_object_unref() instead.
261 gdk_drag_context_unref (GdkDragContext *context)
263 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
265 g_object_unref (context);
268 static GdkDragContext *
269 gdk_drag_context_find (GdkDisplay *display,
274 GList *tmp_list = contexts;
275 GdkDragContext *context;
276 GdkDragContextPrivateX11 *private;
277 Window context_dest_xid;
281 context = (GdkDragContext *)tmp_list->data;
282 private = PRIVATE_DATA (context);
284 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
285 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
288 context_dest_xid = context->dest_window ?
291 GDK_DRAWABLE_XID (context->dest_window)) :
294 if ((!context->is_source == !is_source) &&
295 ((source_xid == None) || (context->source_window &&
296 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
297 ((dest_xid == None) || (context_dest_xid == dest_xid)))
300 tmp_list = tmp_list->next;
307 precache_target_list (GdkDragContext *context)
309 if (context->targets)
311 GPtrArray *targets = g_ptr_array_new ();
315 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
316 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
318 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
319 (const gchar **)targets->pdata,
322 for (i =0; i < targets->len; i++)
323 g_free (targets->pdata[i]);
325 g_ptr_array_free (targets, TRUE);
329 /* Utility functions */
332 gdk_window_cache_add (GdkWindowCache *cache,
334 gint x, gint y, gint width, gint height,
337 GdkCacheChild *child = g_new (GdkCacheChild, 1);
342 child->width = width;
343 child->height = height;
344 child->mapped = mapped;
346 cache->children = g_list_prepend (cache->children, child);
347 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
351 static GdkFilterReturn
352 gdk_window_cache_filter (GdkXEvent *xev,
356 XEvent *xevent = (XEvent *)xev;
357 GdkWindowCache *cache = data;
359 switch (xevent->type)
361 case CirculateNotify:
363 case ConfigureNotify:
365 XConfigureEvent *xce = &xevent->xconfigure;
368 node = g_hash_table_lookup (cache->child_hash,
369 GUINT_TO_POINTER (xce->window));
372 GdkCacheChild *child = node->data;
375 child->width = xce->width;
376 child->height = xce->height;
377 if (xce->above == None && (node->next))
379 GList *last = g_list_last (cache->children);
380 cache->children = g_list_remove_link (cache->children, node);
387 GList *above_node = g_hash_table_lookup (cache->child_hash,
388 GUINT_TO_POINTER (xce->above));
389 if (above_node && node->next != above_node)
391 /* Put the window above (before in the list) above_node
393 cache->children = g_list_remove_link (cache->children, node);
394 node->prev = above_node->prev;
396 node->prev->next = node;
398 cache->children = node;
399 node->next = above_node;
400 above_node->prev = node;
408 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
410 if (!g_hash_table_lookup (cache->child_hash,
411 GUINT_TO_POINTER (xcwe->window)))
412 gdk_window_cache_add (cache, xcwe->window,
413 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
419 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
422 node = g_hash_table_lookup (cache->child_hash,
423 GUINT_TO_POINTER (xdwe->window));
426 g_hash_table_remove (cache->child_hash,
427 GUINT_TO_POINTER (xdwe->window));
428 cache->children = g_list_remove_link (cache->children, node);
430 g_list_free_1 (node);
436 XMapEvent *xme = &xevent->xmap;
439 node = g_hash_table_lookup (cache->child_hash,
440 GUINT_TO_POINTER (xme->window));
443 GdkCacheChild *child = node->data;
444 child->mapped = TRUE;
452 XMapEvent *xume = &xevent->xmap;
455 node = g_hash_table_lookup (cache->child_hash,
456 GUINT_TO_POINTER (xume->window));
459 GdkCacheChild *child = node->data;
460 child->mapped = FALSE;
465 return GDK_FILTER_CONTINUE;
467 return GDK_FILTER_REMOVE;
470 static GdkWindowCache *
471 gdk_window_cache_new (GdkScreen *screen)
473 XWindowAttributes xwa;
474 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
475 GdkWindow *root_window = gdk_screen_get_root_window (screen);
476 GdkChildInfoX11 *children;
479 GdkWindowCache *result = g_new (GdkWindowCache, 1);
481 result->children = NULL;
482 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
483 result->screen = screen;
485 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
486 result->old_event_mask = xwa.your_event_mask;
487 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
488 result->old_event_mask | SubstructureNotifyMask);
489 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
491 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
492 GDK_WINDOW_XWINDOW (root_window),
494 &children, &nchildren))
497 for (i = 0; i < nchildren ; i++)
499 gdk_window_cache_add (result, children[i].window,
500 children[i].x, children[i].y, children[i].width, children[i].height,
501 children[i].is_mapped);
510 gdk_window_cache_destroy (GdkWindowCache *cache)
512 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
514 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
515 GDK_WINDOW_XWINDOW (root_window),
516 cache->old_event_mask);
517 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
519 g_list_foreach (cache->children, (GFunc)g_free, NULL);
520 g_list_free (cache->children);
521 g_hash_table_destroy (cache->child_hash);
527 get_client_window_at_coords_recurse (GdkDisplay *display,
529 gboolean is_toplevel,
533 GdkChildInfoX11 *children;
534 unsigned int nchildren;
536 gboolean found_child = FALSE;
537 GdkChildInfoX11 child;
538 gboolean has_wm_state = FALSE;
540 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
541 is_toplevel? &has_wm_state : NULL,
542 &children, &nchildren))
548 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
550 GdkChildInfoX11 *cur_child = &children[i];
552 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
553 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
554 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
567 if (child.has_wm_state)
570 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
577 get_client_window_at_coords (GdkWindowCache *cache,
583 Window retval = None;
585 gdk_error_trap_push ();
587 tmp_list = cache->children;
589 while (tmp_list && !retval)
591 GdkCacheChild *child = tmp_list->data;
593 if ((child->xid != ignore) && (child->mapped))
595 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
596 (y_root >= child->y) && (y_root < child->y + child->height))
598 retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
607 tmp_list = tmp_list->next;
610 gdk_error_trap_pop ();
615 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
618 /*************************************************************
619 ***************************** MOTIF *************************
620 *************************************************************/
622 /* values used in the message type for Motif DND */
635 /* Values used to specify type of protocol to use */
639 XmDRAG_PREFER_PREREGISTER,
641 XmDRAG_PREFER_DYNAMIC,
643 XmDRAG_PREFER_RECEIVER
646 /* Operation codes */
654 /* Drop site status */
656 XmNO_DROP_SITE = 0x01,
657 XmDROP_SITE_INVALID = 0x02,
658 XmDROP_SITE_VALID = 0x03
661 /* completion status */
669 /* Byte swapping routines. The motif specification leaves it
670 * up to us to save a few bytes in the client messages
672 static gchar local_byte_order = '\0';
674 #ifdef G_ENABLE_DEBUG
676 print_target_list (GList *targets)
680 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
681 g_message ("\t%s", name);
683 targets = targets->next;
686 #endif /* G_ENABLE_DEBUG */
689 init_byte_order (void)
691 guint32 myint = 0x01020304;
692 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
696 card16_to_host (guint16 x, gchar byte_order) {
697 if (byte_order == local_byte_order)
700 return (x << 8) | (x >> 8);
704 card32_to_host (guint32 x, gchar byte_order) {
705 if (byte_order == local_byte_order)
708 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
711 /* Motif packs together fields of varying length into the
712 * client message. We can't rely on accessing these
713 * through data.s[], data.l[], etc, because on some architectures
714 * (i.e., Alpha) these won't be valid for format == 8.
717 #define MOTIF_XCLIENT_BYTE(xevent,i) \
718 (xevent)->xclient.data.b[i]
719 #define MOTIF_XCLIENT_SHORT(xevent,i) \
720 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
721 #define MOTIF_XCLIENT_LONG(xevent,i) \
722 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
724 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
725 #define MOTIF_UNPACK_SHORT(xevent,i) \
726 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
727 #define MOTIF_UNPACK_LONG(xevent,i) \
728 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
730 /***** Dest side ***********/
732 /* Property placed on source windows */
733 typedef struct _MotifDragInitiatorInfo {
735 guint8 protocol_version;
736 guint16 targets_index;
737 guint32 selection_atom;
738 } MotifDragInitiatorInfo;
740 /* Header for target table on the drag window */
741 typedef struct _MotifTargetTableHeader {
743 guchar protocol_version;
746 } MotifTargetTableHeader;
748 /* Property placed on target windows */
749 typedef struct _MotifDragReceiverInfo {
751 guint8 protocol_version;
752 guint8 protocol_style;
754 guint32 proxy_window;
755 guint16 num_drop_sites;
758 } MotifDragReceiverInfo;
760 /* Target table handling */
762 static GdkFilterReturn
763 motif_drag_window_filter (GdkXEvent *xevent,
767 XEvent *xev = (XEvent *)xevent;
768 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
769 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
771 switch (xev->xany.type)
774 display_x11->motif_drag_window = None;
775 display_x11->motif_drag_gdk_window = NULL;
778 if (display_x11->motif_target_lists &&
779 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
780 motif_read_target_table (display);
783 return GDK_FILTER_REMOVE;
787 motif_lookup_drag_window (GdkDisplay *display,
788 Display *lookup_xdisplay)
790 Window retval = None;
791 gulong bytes_after, nitems;
796 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
797 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
799 XA_WINDOW, &type, &format, &nitems, &bytes_after,
802 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
804 retval = *(Window *)data;
806 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
815 /* Finds the window where global Motif drag information is stored.
816 * If it doesn't exist and 'create' is TRUE, create one.
819 motif_find_drag_window (GdkDisplay *display,
822 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
824 if (!display_x11->motif_drag_window)
826 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
827 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
829 if (!display_x11->motif_drag_window && create)
831 /* Create a persistant window. (Copied from LessTif) */
833 Display *persistant_xdisplay;
834 XSetWindowAttributes attr;
835 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
836 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
838 XGrabServer (persistant_xdisplay);
840 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
842 if (!display_x11->motif_drag_window)
844 attr.override_redirect = True;
845 attr.event_mask = PropertyChangeMask;
847 display_x11->motif_drag_window =
848 XCreateWindow (persistant_xdisplay,
849 RootWindow (persistant_xdisplay, 0),
850 -100, -100, 10, 10, 0, 0,
851 InputOnly, CopyFromParent,
852 (CWOverrideRedirect | CWEventMask), &attr);
855 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
857 XChangeProperty (persistant_xdisplay,
858 RootWindow (persistant_xdisplay, 0),
859 motif_drag_window_atom, XA_WINDOW,
861 (guchar *)&motif_drag_window_atom, 1);
864 XUngrabServer (persistant_xdisplay);
865 XCloseDisplay (persistant_xdisplay);
868 /* There is a miniscule race condition here if the drag window
869 * gets destroyed exactly now.
871 if (display_x11->motif_drag_window)
873 display_x11->motif_drag_gdk_window =
874 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
875 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
876 motif_drag_window_filter,
881 return display_x11->motif_drag_window;
885 motif_read_target_table (GdkDisplay *display)
887 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
888 gulong bytes_after, nitems;
893 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
895 if (display_x11->motif_target_lists)
897 for (i=0; i<display_x11->motif_n_target_lists; i++)
898 g_list_free (display_x11->motif_target_lists[i]);
900 g_free (display_x11->motif_target_lists);
901 display_x11->motif_target_lists = NULL;
902 display_x11->motif_n_target_lists = 0;
905 if (motif_find_drag_window (display, FALSE))
907 MotifTargetTableHeader *header = NULL;
908 guchar *target_bytes = NULL;
910 gboolean success = FALSE;
912 gdk_error_trap_push ();
913 XGetWindowProperty (display_x11->xdisplay,
914 display_x11->motif_drag_window,
915 motif_drag_targets_atom,
916 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
917 motif_drag_targets_atom,
918 &type, &format, &nitems, &bytes_after,
921 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
924 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
925 header->total_size = card32_to_host (header->total_size, header->byte_order);
927 gdk_error_trap_push ();
928 XGetWindowProperty (display_x11->xdisplay,
929 display_x11->motif_drag_window,
930 motif_drag_targets_atom,
931 (sizeof(MotifTargetTableHeader)+3)/4,
932 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
934 motif_drag_targets_atom, &type, &format, &nitems,
935 &bytes_after, &target_bytes);
937 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
938 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
941 display_x11->motif_n_target_lists = header->n_lists;
942 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
945 for (i=0; i<header->n_lists; i++)
950 if (p + sizeof(guint16) - target_bytes > nitems)
953 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
955 /* We need to make a copy of the targets, since it may
958 targets = g_new (guint32, n_targets);
959 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
961 p += sizeof(guint16) + n_targets * sizeof(guint32);
962 if (p - target_bytes > nitems)
965 for (j=0; j<n_targets; j++)
966 display_x11->motif_target_lists[i] =
967 g_list_prepend (display_x11->motif_target_lists[i],
968 GUINT_TO_POINTER (card32_to_host (targets[j],
969 header->byte_order)));
971 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
981 XFree (target_bytes);
985 if (display_x11->motif_target_lists)
987 g_free (display_x11->motif_target_lists);
988 display_x11->motif_target_lists = NULL;
989 display_x11->motif_n_target_lists = 0;
991 g_warning ("Error reading Motif target table\n");
997 targets_sort_func (gconstpointer a, gconstpointer b)
999 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1000 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1003 /* Check if given (sorted) list is in the targets table */
1005 motif_target_table_check (GdkDisplay *display,
1008 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1009 GList *tmp_list1, *tmp_list2;
1012 for (i=0; i<display_x11->motif_n_target_lists; i++)
1014 tmp_list1 = display_x11->motif_target_lists[i];
1017 while (tmp_list1 && tmp_list2)
1019 if (tmp_list1->data != tmp_list2->data)
1022 tmp_list1 = tmp_list1->next;
1023 tmp_list2 = tmp_list2->next;
1025 if (!tmp_list1 && !tmp_list2) /* Found it */
1033 motif_add_to_target_table (GdkDisplay *display,
1034 GList *targets) /* targets is list of GdkAtom */
1036 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1037 GList *sorted = NULL;
1042 /* make a sorted copy of the list */
1046 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1047 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1048 targets = targets->next;
1051 /* First check if it is there already */
1053 if (display_x11->motif_target_lists)
1054 index = motif_target_table_check (display, sorted);
1056 /* We need to grab the server while doing this, to ensure
1062 /* We need to make sure that it exists _before_ we grab the
1063 * server, since we can't open a new connection after we
1066 motif_find_drag_window (display, TRUE);
1068 gdk_x11_display_grab (display);
1069 motif_read_target_table (display);
1071 /* Check again, in case it was added in the meantime */
1073 if (display_x11->motif_target_lists)
1074 index = motif_target_table_check (display, sorted);
1078 guint32 total_size = 0;
1082 MotifTargetTableHeader *header;
1084 if (!display_x11->motif_target_lists)
1086 display_x11->motif_target_lists = g_new (GList *, 1);
1087 display_x11->motif_n_target_lists = 1;
1091 display_x11->motif_n_target_lists++;
1092 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1093 sizeof(GList *) * display_x11->motif_n_target_lists);
1095 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1097 index = display_x11->motif_n_target_lists - 1;
1099 total_size = sizeof (MotifTargetTableHeader);
1100 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1101 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1103 data = g_malloc (total_size);
1105 header = (MotifTargetTableHeader *)data;
1106 p = data + sizeof(MotifTargetTableHeader);
1108 header->byte_order = local_byte_order;
1109 header->protocol_version = 0;
1110 header->n_lists = display_x11->motif_n_target_lists;
1111 header->total_size = total_size;
1113 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1115 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1116 guint32 *targets = g_new (guint32, n_targets);
1117 guint32 *p32 = targets;
1119 tmp_list = display_x11->motif_target_lists[i];
1122 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1124 tmp_list = tmp_list->next;
1129 p += sizeof(guint16);
1131 memcpy (p, targets, n_targets * sizeof(guint32));
1134 p += sizeof(guint32) * n_targets;
1138 XChangeProperty (display_x11->xdisplay,
1139 display_x11->motif_drag_window,
1140 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1141 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1145 gdk_x11_display_ungrab (display);
1148 g_list_free (sorted);
1152 /* Translate flags */
1155 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1157 guint recommended_op = flags & 0x000f;
1158 guint possible_ops = (flags & 0x0f0) >> 4;
1160 switch (recommended_op)
1163 context->suggested_action = GDK_ACTION_MOVE;
1166 context->suggested_action = GDK_ACTION_COPY;
1169 context->suggested_action = GDK_ACTION_LINK;
1172 context->suggested_action = GDK_ACTION_COPY;
1176 context->actions = 0;
1177 if (possible_ops & XmDROP_MOVE)
1178 context->actions |= GDK_ACTION_MOVE;
1179 if (possible_ops & XmDROP_COPY)
1180 context->actions |= GDK_ACTION_COPY;
1181 if (possible_ops & XmDROP_LINK)
1182 context->actions |= GDK_ACTION_LINK;
1186 motif_dnd_get_flags (GdkDragContext *context)
1190 switch (context->suggested_action)
1192 case GDK_ACTION_MOVE:
1193 flags = XmDROP_MOVE;
1195 case GDK_ACTION_COPY:
1196 flags = XmDROP_COPY;
1198 case GDK_ACTION_LINK:
1199 flags = XmDROP_LINK;
1202 flags = XmDROP_NOOP;
1206 if (context->actions & GDK_ACTION_MOVE)
1207 flags |= XmDROP_MOVE << 8;
1208 if (context->actions & GDK_ACTION_COPY)
1209 flags |= XmDROP_COPY << 8;
1210 if (context->actions & GDK_ACTION_LINK)
1211 flags |= XmDROP_LINK << 8;
1219 motif_set_targets (GdkDragContext *context)
1221 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1222 MotifDragInitiatorInfo info;
1224 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1226 info.byte_order = local_byte_order;
1227 info.protocol_version = 0;
1229 info.targets_index = motif_add_to_target_table (display, context->targets);
1234 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1236 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1237 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1241 info.selection_atom = private->motif_selection;
1243 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1244 GDK_DRAWABLE_XID (context->source_window),
1245 private->motif_selection,
1246 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1248 (guchar *)&info, sizeof (info));
1250 private->motif_targets_set = 1;
1254 motif_check_dest (GdkDisplay *display,
1257 gboolean retval = FALSE;
1258 MotifDragReceiverInfo *info;
1261 unsigned long nitems, after;
1262 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1264 gdk_error_trap_push ();
1265 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1266 motif_drag_receiver_info_atom,
1267 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1268 &type, &format, &nitems, &after,
1271 if (gdk_error_trap_pop() == 0)
1275 if ((format == 8) && (nitems == sizeof(*info)))
1277 if ((info->protocol_version == 0) &&
1278 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1279 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1280 (info->protocol_style == XmDRAG_DYNAMIC)))
1286 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1293 return retval ? win : None;
1297 motif_send_enter (GdkDragContext *context,
1300 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1301 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1304 xev.xclient.type = ClientMessage;
1305 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1306 xev.xclient.format = 8;
1307 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1309 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1310 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1311 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1312 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1313 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1315 if (!private->motif_targets_set)
1316 motif_set_targets (context);
1318 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1319 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1321 if (!_gdk_send_xevent (display,
1322 GDK_DRAWABLE_XID (context->dest_window),
1325 g_message ("Send event to %lx failed",
1326 GDK_DRAWABLE_XID (context->dest_window)));
1330 motif_send_leave (GdkDragContext *context,
1333 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1336 xev.xclient.type = ClientMessage;
1337 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1338 xev.xclient.format = 8;
1339 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1341 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1342 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1343 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1344 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1345 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1346 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1347 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1349 if (!_gdk_send_xevent (display,
1350 GDK_DRAWABLE_XID (context->dest_window),
1353 g_message ("Send event to %lx failed",
1354 GDK_DRAWABLE_XID (context->dest_window)));
1358 motif_send_motion (GdkDragContext *context,
1361 GdkDragAction action,
1364 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1365 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1369 xev.xclient.type = ClientMessage;
1370 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1371 xev.xclient.format = 8;
1372 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1374 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1375 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1376 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1377 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1378 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1380 if ((context->suggested_action != private->old_action) ||
1381 (context->actions != private->old_actions))
1383 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1385 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1390 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1392 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1393 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1395 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1399 if (!_gdk_send_xevent (display,
1400 GDK_DRAWABLE_XID (context->dest_window),
1403 g_message ("Send event to %lx failed",
1404 GDK_DRAWABLE_XID (context->dest_window)));
1410 motif_send_drop (GdkDragContext *context, guint32 time)
1412 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1413 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1416 xev.xclient.type = ClientMessage;
1417 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1418 xev.xclient.format = 8;
1419 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1421 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1422 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1423 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1424 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1426 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1427 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1429 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1430 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1432 if (!_gdk_send_xevent (display,
1433 GDK_DRAWABLE_XID (context->dest_window),
1436 g_message ("Send event to %lx failed",
1437 GDK_DRAWABLE_XID (context->dest_window)));
1443 motif_read_initiator_info (GdkDisplay *display,
1444 Window source_window,
1454 MotifDragInitiatorInfo *initiator_info;
1456 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1458 gdk_error_trap_push ();
1459 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1460 0, sizeof(*initiator_info), FALSE,
1461 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1462 &type, &format, &nitems, &bytes_after,
1463 (guchar **)&initiator_info);
1465 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1467 g_warning ("Error reading initiator info\n");
1471 motif_read_target_table (display);
1473 initiator_info->targets_index =
1474 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1475 initiator_info->selection_atom =
1476 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1478 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1480 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1481 XFree (initiator_info);
1485 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1490 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1491 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1492 tmp_list = tmp_list->prev;
1495 #ifdef G_ENABLE_DEBUG
1496 if (_gdk_debug_flags & GDK_DEBUG_DND)
1497 print_target_list (*targets);
1498 #endif /* G_ENABLE_DEBUG */
1500 *selection = initiator_info->selection_atom;
1502 XFree (initiator_info);
1507 static GdkDragContext *
1508 motif_drag_context_new (GdkWindow *dest_window,
1510 guint32 source_window,
1513 GdkDragContext *new_context;
1514 GdkDragContextPrivateX11 *private;
1515 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1516 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1518 /* FIXME, current_dest_drag really shouldn't be NULL'd
1519 * if we error below.
1521 if (display_x11->current_dest_drag != NULL)
1523 if (timestamp >= display_x11->current_dest_drag->start_time)
1525 g_object_unref (display_x11->current_dest_drag);
1526 display_x11->current_dest_drag = NULL;
1532 new_context = gdk_drag_context_new ();
1533 private = PRIVATE_DATA (new_context);
1535 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1536 new_context->is_source = FALSE;
1538 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1539 if (new_context->source_window)
1540 g_object_ref (new_context->source_window);
1543 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1544 if (!new_context->source_window)
1546 g_object_unref (new_context);
1551 new_context->dest_window = dest_window;
1552 g_object_ref (dest_window);
1553 new_context->start_time = timestamp;
1555 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1558 &new_context->targets,
1559 &private->motif_selection))
1561 g_object_unref (new_context);
1569 * The MOTIF drag protocol has no real provisions for distinguishing
1570 * multiple simultaneous drops. If the sources grab the pointer
1571 * when doing drags, that shouldn't happen, in any case. If it
1572 * does, we can't do much except hope for the best.
1575 static GdkFilterReturn
1576 motif_top_level_enter (GdkEvent *event,
1579 guint32 source_window,
1582 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1583 GdkDragContext *new_context;
1585 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1586 flags, timestamp, source_window, atom));
1588 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1590 return GDK_FILTER_REMOVE;
1592 event->dnd.type = GDK_DRAG_ENTER;
1593 event->dnd.context = new_context;
1594 g_object_ref (new_context);
1596 display_x11->current_dest_drag = new_context;
1598 return GDK_FILTER_TRANSLATE;
1601 static GdkFilterReturn
1602 motif_top_level_leave (GdkEvent *event,
1606 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1608 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1611 if ((display_x11->current_dest_drag != NULL) &&
1612 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1613 (timestamp >= display_x11->current_dest_drag->start_time))
1615 event->dnd.type = GDK_DRAG_LEAVE;
1616 /* Pass ownership of context to the event */
1617 event->dnd.context = display_x11->current_dest_drag;
1619 display_x11->current_dest_drag = NULL;
1621 return GDK_FILTER_TRANSLATE;
1624 return GDK_FILTER_REMOVE;
1627 static GdkFilterReturn
1628 motif_motion (GdkEvent *event,
1634 GdkDragContextPrivateX11 *private;
1635 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1637 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1638 flags, timestamp, x_root, y_root));
1640 if ((display_x11->current_dest_drag != NULL) &&
1641 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1642 (timestamp >= display_x11->current_dest_drag->start_time))
1644 private = PRIVATE_DATA (display_x11->current_dest_drag);
1646 event->dnd.type = GDK_DRAG_MOTION;
1647 event->dnd.context = display_x11->current_dest_drag;
1648 g_object_ref (display_x11->current_dest_drag);
1650 event->dnd.time = timestamp;
1652 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1654 event->dnd.x_root = x_root;
1655 event->dnd.y_root = y_root;
1657 private->last_x = x_root;
1658 private->last_y = y_root;
1660 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1662 return GDK_FILTER_TRANSLATE;
1665 return GDK_FILTER_REMOVE;
1668 static GdkFilterReturn
1669 motif_operation_changed (GdkEvent *event,
1673 GdkDragContextPrivateX11 *private;
1674 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1675 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1678 if ((display_x11->current_dest_drag != NULL) &&
1679 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1680 (timestamp >= display_x11->current_dest_drag->start_time))
1682 event->dnd.type = GDK_DRAG_MOTION;
1683 event->dnd.send_event = FALSE;
1684 event->dnd.context = display_x11->current_dest_drag;
1685 g_object_ref (display_x11->current_dest_drag);
1687 event->dnd.time = timestamp;
1688 private = PRIVATE_DATA (display_x11->current_dest_drag);
1690 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1692 event->dnd.x_root = private->last_x;
1693 event->dnd.y_root = private->last_y;
1695 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1697 return GDK_FILTER_TRANSLATE;
1700 return GDK_FILTER_REMOVE;
1703 static GdkFilterReturn
1704 motif_drop_start (GdkEvent *event,
1707 guint32 source_window,
1712 GdkDragContext *new_context;
1713 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1715 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1716 flags, timestamp, x_root, y_root, source_window, atom));
1718 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1720 return GDK_FILTER_REMOVE;
1722 motif_dnd_translate_flags (new_context, flags);
1724 event->dnd.type = GDK_DROP_START;
1725 event->dnd.context = new_context;
1726 event->dnd.time = timestamp;
1727 event->dnd.x_root = x_root;
1728 event->dnd.y_root = y_root;
1730 g_object_ref (new_context);
1731 display_x11->current_dest_drag = new_context;
1733 return GDK_FILTER_TRANSLATE;
1736 static GdkFilterReturn
1737 motif_drag_status (GdkEvent *event,
1741 GdkDragContext *context;
1742 GdkDisplay *display;
1745 g_message ("Motif status message: flags %x", flags));
1747 display = gdk_drawable_get_display (event->any.window);
1749 return GDK_FILTER_REMOVE;
1751 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1755 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1756 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1757 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1758 private->drag_status = GDK_DRAG_STATUS_DRAG;
1760 event->dnd.type = GDK_DRAG_STATUS;
1761 event->dnd.send_event = FALSE;
1762 event->dnd.context = context;
1763 g_object_ref (context);
1765 event->dnd.time = timestamp;
1767 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1769 switch (flags & 0x000f)
1772 context->action = 0;
1775 context->action = GDK_ACTION_MOVE;
1778 context->action = GDK_ACTION_COPY;
1781 context->action = GDK_ACTION_LINK;
1786 context->action = 0;
1788 return GDK_FILTER_TRANSLATE;
1790 return GDK_FILTER_REMOVE;
1793 static GdkFilterReturn
1794 motif_dnd_filter (GdkXEvent *xev,
1798 XEvent *xevent = (XEvent *)xev;
1803 guint32 source_window;
1805 gint16 x_root, y_root;
1808 if (!event->any.window ||
1809 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1810 return GDK_FILTER_CONTINUE; /* Not for us */
1812 /* First read some fields common to all Motif DND messages */
1814 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1815 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1816 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1818 is_reply = ((reason & 0x80) != 0);
1820 switch (reason & 0x7f)
1822 case XmTOP_LEVEL_ENTER:
1823 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1824 atom = MOTIF_UNPACK_LONG (xevent, 3);
1825 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1826 case XmTOP_LEVEL_LEAVE:
1827 return motif_top_level_leave (event, flags, timestamp);
1830 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1831 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1834 return motif_motion (event, flags, timestamp, x_root, y_root);
1836 return motif_drag_status (event, flags, timestamp);
1838 case XmDROP_SITE_ENTER:
1839 return motif_drag_status (event, flags, timestamp);
1841 case XmDROP_SITE_LEAVE:
1842 return motif_drag_status (event,
1843 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1846 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1847 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1848 atom = MOTIF_UNPACK_LONG (xevent, 3);
1849 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1852 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1855 case XmOPERATION_CHANGED:
1857 return motif_operation_changed (event, flags, timestamp);
1859 return motif_drag_status (event, flags, timestamp);
1862 /* To the best of my knowledge, these next two messages are
1863 * not part of the protocol, though they are defined in
1867 case XmDRAG_DROP_FINISH:
1871 return GDK_FILTER_REMOVE;
1874 /*************************************************************
1875 ***************************** XDND **************************
1876 *************************************************************/
1878 /* Utility functions */
1883 GdkDragAction action;
1884 } xdnd_actions_table[] = {
1885 { "XdndActionCopy", None, GDK_ACTION_COPY },
1886 { "XdndActionMove", None, GDK_ACTION_MOVE },
1887 { "XdndActionLink", None, GDK_ACTION_LINK },
1888 { "XdndActionAsk", None, GDK_ACTION_ASK },
1889 { "XdndActionPrivate", None, GDK_ACTION_COPY },
1892 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1893 static gboolean xdnd_actions_initialized = FALSE;
1896 xdnd_initialize_actions (void)
1900 xdnd_actions_initialized = TRUE;
1901 for (i=0; i < xdnd_n_actions; i++)
1902 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1905 static GdkDragAction
1906 xdnd_action_from_atom (GdkDisplay *display,
1909 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
1912 if (!xdnd_actions_initialized)
1913 xdnd_initialize_actions();
1915 for (i=0; i<xdnd_n_actions; i++)
1916 if (atom == xdnd_actions_table[i].atom)
1917 return xdnd_actions_table[i].action;
1923 xdnd_action_to_atom (GdkDisplay *display,
1924 GdkDragAction action)
1928 if (!xdnd_actions_initialized)
1929 xdnd_initialize_actions();
1931 for (i=0; i<xdnd_n_actions; i++)
1932 if (action == xdnd_actions_table[i].action)
1933 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
1940 static GdkFilterReturn
1941 xdnd_status_filter (GdkXEvent *xev,
1945 GdkDisplay *display;
1946 XEvent *xevent = (XEvent *)xev;
1947 guint32 dest_window = xevent->xclient.data.l[0];
1948 guint32 flags = xevent->xclient.data.l[1];
1949 Atom action = xevent->xclient.data.l[4];
1950 GdkDragContext *context;
1952 if (!event->any.window ||
1953 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1954 return GDK_FILTER_CONTINUE; /* Not for us */
1957 g_message ("XdndStatus: dest_window: %#x action: %ld",
1958 dest_window, action));
1960 display = gdk_drawable_get_display (event->any.window);
1961 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1965 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1966 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1967 private->drag_status = GDK_DRAG_STATUS_DRAG;
1969 event->dnd.send_event = FALSE;
1970 event->dnd.type = GDK_DRAG_STATUS;
1971 event->dnd.context = context;
1972 g_object_ref (context);
1974 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1975 if (!(action != 0) != !(flags & 1))
1978 g_warning ("Received status event with flags not corresponding to action!\n"));
1982 context->action = xdnd_action_from_atom (display, action);
1984 return GDK_FILTER_TRANSLATE;
1987 return GDK_FILTER_REMOVE;
1990 static GdkFilterReturn
1991 xdnd_finished_filter (GdkXEvent *xev,
1995 GdkDisplay *display;
1996 XEvent *xevent = (XEvent *)xev;
1997 guint32 dest_window = xevent->xclient.data.l[0];
1998 GdkDragContext *context;
2000 if (!event->any.window ||
2001 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2002 return GDK_FILTER_CONTINUE; /* Not for us */
2005 g_message ("XdndFinished: dest_window: %#x", dest_window));
2007 display = gdk_drawable_get_display (event->any.window);
2008 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2012 event->dnd.type = GDK_DROP_FINISHED;
2013 event->dnd.context = context;
2014 g_object_ref (context);
2016 return GDK_FILTER_TRANSLATE;
2019 return GDK_FILTER_REMOVE;
2023 xdnd_set_targets (GdkDragContext *context)
2025 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2027 GList *tmp_list = context->targets;
2029 gint n_atoms = g_list_length (context->targets);
2030 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2032 atomlist = g_new (Atom, n_atoms);
2036 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2037 tmp_list = tmp_list->next;
2041 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2042 GDK_DRAWABLE_XID (context->source_window),
2043 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2044 XA_ATOM, 32, PropModeReplace,
2045 (guchar *)atomlist, n_atoms);
2049 private->xdnd_targets_set = 1;
2053 xdnd_set_actions (GdkDragContext *context)
2055 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2060 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2062 if (!xdnd_actions_initialized)
2063 xdnd_initialize_actions();
2065 actions = context->actions;
2067 for (i=0; i<xdnd_n_actions; i++)
2069 if (actions & xdnd_actions_table[i].action)
2071 actions &= ~xdnd_actions_table[i].action;
2076 atomlist = g_new (Atom, n_atoms);
2078 actions = context->actions;
2080 for (i=0; i<xdnd_n_actions; i++)
2082 if (actions & xdnd_actions_table[i].action)
2084 actions &= ~xdnd_actions_table[i].action;
2085 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2090 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2091 GDK_DRAWABLE_XID (context->source_window),
2092 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2093 XA_ATOM, 32, PropModeReplace,
2094 (guchar *)atomlist, n_atoms);
2098 private->xdnd_actions_set = TRUE;
2099 private->xdnd_actions = context->actions;
2103 send_xevent_async_cb (Window window,
2107 GdkDragContext *context = data;
2109 g_message ("Got async callback for #%lx, success = %d",
2112 /* On failure, we immediately continue with the protocol
2113 * so we don't end up blocking for a timeout
2116 context->dest_window &&
2117 window == GDK_WINDOW_XID (context->dest_window))
2119 GdkEvent temp_event;
2120 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2122 g_object_unref (context->dest_window);
2123 context->dest_window = NULL;
2124 context->action = 0;
2126 private->drag_status = GDK_DRAG_STATUS_DRAG;
2128 temp_event.dnd.type = GDK_DRAG_STATUS;
2129 temp_event.dnd.window = context->source_window;
2130 temp_event.dnd.send_event = TRUE;
2131 temp_event.dnd.context = context;
2132 temp_event.dnd.time = GDK_CURRENT_TIME;
2134 gdk_event_put (&temp_event);
2137 g_object_unref (context);
2142 gdk_drag_context_get_display (GdkDragContext *context)
2144 if (context->source_window)
2145 return GDK_DRAWABLE_DISPLAY (context->source_window);
2146 else if (context->dest_window)
2147 return GDK_DRAWABLE_DISPLAY (context->dest_window);
2149 g_assert_not_reached ();
2154 send_xevent_async (GdkDragContext *context,
2160 GdkDisplay *display = gdk_drag_context_get_display (context);
2162 g_object_ref (context);
2164 _gdk_x11_send_xevent_async (display, window,
2165 propagate, event_mask, event_send,
2166 send_xevent_async_cb, context);
2170 xdnd_send_xevent (GdkDragContext *context,
2175 GdkDisplay *display = gdk_drag_context_get_display (context);
2179 /* We short-circuit messages to ourselves */
2180 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN &&
2181 event_send->xany.type == ClientMessage)
2185 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2187 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2188 event_send->xclient.message_type)
2190 GdkEvent temp_event;
2191 temp_event.any.window = window;
2193 if ((*xdnd_filters[i].func) (event_send, &temp_event, NULL) == GDK_FILTER_TRANSLATE)
2194 gdk_event_put (&temp_event);
2201 xwindow = GDK_WINDOW_XWINDOW (window);
2203 if (_gdk_x11_display_is_root_window (display, xwindow))
2204 event_mask = ButtonPressMask;
2208 send_xevent_async (context, xwindow, propagate, event_mask, event_send);
2214 xdnd_send_enter (GdkDragContext *context)
2217 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2218 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2220 xev.xclient.type = ClientMessage;
2221 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
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] = (3 << 24); /* version */
2228 xev.xclient.data.l[2] = 0;
2229 xev.xclient.data.l[3] = 0;
2230 xev.xclient.data.l[4] = 0;
2232 if (g_list_length (context->targets) > 3)
2234 if (!private->xdnd_targets_set)
2235 xdnd_set_targets (context);
2236 xev.xclient.data.l[1] |= 1;
2240 GList *tmp_list = context->targets;
2245 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2246 GDK_POINTER_TO_ATOM (tmp_list->data));
2247 tmp_list = tmp_list->next;
2252 if (!xdnd_send_xevent (context, context->dest_window,
2256 g_message ("Send event to %lx failed",
2257 GDK_DRAWABLE_XID (context->dest_window)));
2258 g_object_unref (context->dest_window);
2259 context->dest_window = NULL;
2264 xdnd_send_leave (GdkDragContext *context)
2266 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2269 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2271 xev.xclient.type = ClientMessage;
2272 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2273 xev.xclient.format = 32;
2274 xev.xclient.window = private->drop_xid ?
2276 GDK_DRAWABLE_XID (context->dest_window);
2277 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2278 xev.xclient.data.l[1] = 0;
2279 xev.xclient.data.l[2] = 0;
2280 xev.xclient.data.l[3] = 0;
2281 xev.xclient.data.l[4] = 0;
2283 if (!xdnd_send_xevent (context, context->dest_window,
2287 g_message ("Send event to %lx failed",
2288 GDK_DRAWABLE_XID (context->dest_window)));
2289 g_object_unref (context->dest_window);
2290 context->dest_window = NULL;
2295 xdnd_send_drop (GdkDragContext *context, guint32 time)
2297 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2298 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2301 xev.xclient.type = ClientMessage;
2302 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2303 xev.xclient.format = 32;
2304 xev.xclient.window = private->drop_xid ?
2306 GDK_DRAWABLE_XID (context->dest_window);
2307 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2308 xev.xclient.data.l[1] = 0;
2309 xev.xclient.data.l[2] = time;
2310 xev.xclient.data.l[3] = 0;
2311 xev.xclient.data.l[4] = 0;
2313 if (!xdnd_send_xevent (context, context->dest_window,
2317 g_message ("Send event to %lx failed",
2318 GDK_DRAWABLE_XID (context->dest_window)));
2319 g_object_unref (context->dest_window);
2320 context->dest_window = NULL;
2325 xdnd_send_motion (GdkDragContext *context,
2328 GdkDragAction action,
2331 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2332 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2335 xev.xclient.type = ClientMessage;
2336 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2337 xev.xclient.format = 32;
2338 xev.xclient.window = private->drop_xid ?
2340 GDK_DRAWABLE_XID (context->dest_window);
2341 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2342 xev.xclient.data.l[1] = 0;
2343 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2344 xev.xclient.data.l[3] = time;
2345 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2347 if (!xdnd_send_xevent (context, context->dest_window,
2351 g_message ("Send event to %lx failed",
2352 GDK_DRAWABLE_XID (context->dest_window)));
2353 g_object_unref (context->dest_window);
2354 context->dest_window = NULL;
2356 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2360 xdnd_check_dest (GdkDisplay *display,
2363 gboolean retval = FALSE;
2366 unsigned long nitems, after;
2370 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2371 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2375 gdk_error_trap_push ();
2377 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2379 1, False, AnyPropertyType,
2380 &type, &format, &nitems, &after,
2381 (guchar **)&proxy_data) == Success)
2385 if ((format == 32) && (nitems == 1))
2387 proxy = *proxy_data;
2391 g_warning ("Invalid XdndOwner "
2392 "property on window %ld\n", win));
2397 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2399 1, False, AnyPropertyType,
2400 &type, &format, &nitems, &after,
2401 (guchar **)&version) == Success) &&
2404 if ((format == 32) && (nitems == 1))
2411 g_warning ("Invalid XdndAware property on window %ld\n", win));
2417 gdk_error_trap_pop ();
2419 return retval ? (proxy ? proxy : win) : None;
2425 xdnd_read_actions (GdkDragContext *context)
2427 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2430 gulong nitems, after;
2435 PRIVATE_DATA (context)->xdnd_have_actions = FALSE;
2437 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2439 /* Get the XdndActionList, if set */
2441 gdk_error_trap_push ();
2443 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2444 GDK_DRAWABLE_XID (context->source_window),
2445 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2447 False, XA_ATOM, &type, &format, &nitems,
2448 &after, (guchar **)&data) == Success &&
2451 context->actions = 0;
2453 for (i=0; i<nitems; i++)
2454 context->actions |= xdnd_action_from_atom (display, data[i]);
2456 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2458 #ifdef G_ENABLE_DEBUG
2459 if (_gdk_debug_flags & GDK_DEBUG_DND)
2461 GString *action_str = g_string_new (NULL);
2462 if (context->actions & GDK_ACTION_MOVE)
2463 g_string_append(action_str, "MOVE ");
2464 if (context->actions & GDK_ACTION_COPY)
2465 g_string_append(action_str, "COPY ");
2466 if (context->actions & GDK_ACTION_LINK)
2467 g_string_append(action_str, "LINK ");
2468 if (context->actions & GDK_ACTION_ASK)
2469 g_string_append(action_str, "ASK ");
2471 g_message("Xdnd actions = %s", action_str->str);
2472 g_string_free (action_str, TRUE);
2474 #endif /* G_ENABLE_DEBUG */
2479 gdk_error_trap_pop ();
2485 GdkDragContext *source_context;
2487 source_context = gdk_drag_context_find (display, TRUE,
2488 GDK_DRAWABLE_XID (context->source_window),
2489 GDK_DRAWABLE_XID (context->dest_window));
2493 context->actions = source_context->actions;
2494 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2499 /* We have to make sure that the XdndActionList we keep internally
2500 * is up to date with the XdndActionList on the source window
2501 * because we get no notification, because Xdnd wasn't meant
2502 * to continually send actions. So we select on PropertyChangeMask
2503 * and add this filter.
2505 static GdkFilterReturn
2506 xdnd_source_window_filter (GdkXEvent *xev,
2510 XEvent *xevent = (XEvent *)xev;
2511 GdkDragContext *context = cb_data;
2512 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2514 if ((xevent->xany.type == PropertyNotify) &&
2515 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2517 xdnd_read_actions (context);
2519 return GDK_FILTER_REMOVE;
2522 return GDK_FILTER_CONTINUE;
2526 xdnd_manage_source_filter (GdkDragContext *context,
2528 gboolean add_filter)
2530 if (!GDK_WINDOW_DESTROYED (window) &&
2531 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2533 gdk_error_trap_push ();
2537 gdk_window_set_events (window,
2538 gdk_window_get_events (window) |
2539 GDK_PROPERTY_CHANGE_MASK);
2540 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2544 gdk_window_remove_filter (window,
2545 xdnd_source_window_filter,
2547 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2548 * but we might want it for other reasons. (Like
2549 * INCR selection transactions).
2553 gdk_display_sync (gdk_drawable_get_display (window));
2554 gdk_error_trap_pop ();
2559 base_precache_atoms (GdkDisplay *display)
2561 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2563 if (!display_x11->base_dnd_atoms_precached)
2565 static const char *const precache_atoms[] = {
2566 "ENLIGHTENMENT_DESKTOP",
2570 "_MOTIF_DRAG_RECEIVER_INFO"
2573 _gdk_x11_precache_atoms (display,
2574 precache_atoms, G_N_ELEMENTS (precache_atoms));
2576 display_x11->base_dnd_atoms_precached = TRUE;
2581 xdnd_precache_atoms (GdkDisplay *display)
2583 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2585 if (!display_x11->xdnd_atoms_precached)
2587 static const char *const precache_atoms[] = {
2593 "XdndActionPrivate",
2604 _gdk_x11_precache_atoms (display,
2605 precache_atoms, G_N_ELEMENTS (precache_atoms));
2607 display_x11->xdnd_atoms_precached = TRUE;
2611 static GdkFilterReturn
2612 xdnd_enter_filter (GdkXEvent *xev,
2616 GdkDisplay *display;
2617 GdkDisplayX11 *display_x11;
2618 XEvent *xevent = (XEvent *)xev;
2619 GdkDragContext *new_context;
2624 gulong nitems, after;
2627 guint32 source_window;
2631 if (!event->any.window ||
2632 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2633 return GDK_FILTER_CONTINUE; /* Not for us */
2635 source_window = xevent->xclient.data.l[0];
2636 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2637 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2639 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2640 display_x11 = GDK_DISPLAY_X11 (display);
2642 xdnd_precache_atoms (display);
2645 g_message ("XdndEnter: source_window: %#x, version: %#x",
2646 source_window, version));
2650 /* Old source ignore */
2651 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2652 return GDK_FILTER_REMOVE;
2655 if (display_x11->current_dest_drag != NULL)
2657 g_object_unref (display_x11->current_dest_drag);
2658 display_x11->current_dest_drag = NULL;
2661 new_context = gdk_drag_context_new ();
2662 new_context->protocol = GDK_DRAG_PROTO_XDND;
2663 new_context->is_source = FALSE;
2665 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2666 if (new_context->source_window)
2667 g_object_ref (new_context->source_window);
2670 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2671 if (!new_context->source_window)
2673 g_object_unref (new_context);
2674 return GDK_FILTER_REMOVE;
2677 new_context->dest_window = event->any.window;
2678 g_object_ref (new_context->dest_window);
2680 new_context->targets = NULL;
2683 gdk_error_trap_push ();
2684 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2686 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2688 False, XA_ATOM, &type, &format, &nitems,
2689 &after, (guchar **)&data);
2691 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2693 g_object_unref (new_context);
2694 return GDK_FILTER_REMOVE;
2697 for (i=0; i<nitems; i++)
2698 new_context->targets =
2699 g_list_append (new_context->targets,
2700 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2708 if (xevent->xclient.data.l[2+i])
2709 new_context->targets =
2710 g_list_append (new_context->targets,
2711 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2712 xevent->xclient.data.l[2+i])));
2715 #ifdef G_ENABLE_DEBUG
2716 if (_gdk_debug_flags & GDK_DEBUG_DND)
2717 print_target_list (new_context->targets);
2718 #endif /* G_ENABLE_DEBUG */
2720 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2721 xdnd_read_actions (new_context);
2723 event->dnd.type = GDK_DRAG_ENTER;
2724 event->dnd.context = new_context;
2725 g_object_ref (new_context);
2727 display_x11->current_dest_drag = new_context;
2729 return GDK_FILTER_TRANSLATE;
2732 static GdkFilterReturn
2733 xdnd_leave_filter (GdkXEvent *xev,
2737 XEvent *xevent = (XEvent *)xev;
2738 guint32 source_window = xevent->xclient.data.l[0];
2739 GdkDisplay *display;
2740 GdkDisplayX11 *display_x11;
2742 if (!event->any.window ||
2743 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2744 return GDK_FILTER_CONTINUE; /* Not for us */
2747 g_message ("XdndLeave: source_window: %#x",
2750 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2751 display_x11 = GDK_DISPLAY_X11 (display);
2753 xdnd_precache_atoms (display);
2755 if ((display_x11->current_dest_drag != NULL) &&
2756 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2757 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2759 event->dnd.type = GDK_DRAG_LEAVE;
2760 /* Pass ownership of context to the event */
2761 event->dnd.context = display_x11->current_dest_drag;
2763 display_x11->current_dest_drag = NULL;
2765 return GDK_FILTER_TRANSLATE;
2768 return GDK_FILTER_REMOVE;
2771 static GdkFilterReturn
2772 xdnd_position_filter (GdkXEvent *xev,
2776 XEvent *xevent = (XEvent *)xev;
2777 guint32 source_window = xevent->xclient.data.l[0];
2778 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2779 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2780 guint32 time = xevent->xclient.data.l[3];
2781 Atom action = xevent->xclient.data.l[4];
2783 GdkDisplay *display;
2784 GdkDisplayX11 *display_x11;
2786 if (!event->any.window ||
2787 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2788 return GDK_FILTER_CONTINUE; /* Not for us */
2791 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2792 source_window, x_root, y_root, time, action));
2794 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2795 display_x11 = GDK_DISPLAY_X11 (display);
2797 xdnd_precache_atoms (display);
2799 if ((display_x11->current_dest_drag != NULL) &&
2800 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2801 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2803 event->dnd.type = GDK_DRAG_MOTION;
2804 event->dnd.context = display_x11->current_dest_drag;
2805 g_object_ref (display_x11->current_dest_drag);
2807 event->dnd.time = time;
2809 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2811 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2812 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2814 event->dnd.x_root = x_root;
2815 event->dnd.y_root = y_root;
2817 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2818 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
2820 return GDK_FILTER_TRANSLATE;
2823 return GDK_FILTER_REMOVE;
2826 static GdkFilterReturn
2827 xdnd_drop_filter (GdkXEvent *xev,
2831 XEvent *xevent = (XEvent *)xev;
2832 guint32 source_window = xevent->xclient.data.l[0];
2833 guint32 time = xevent->xclient.data.l[2];
2834 GdkDisplay *display;
2835 GdkDisplayX11 *display_x11;
2837 if (!event->any.window ||
2838 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2839 return GDK_FILTER_CONTINUE; /* Not for us */
2842 g_message ("XdndDrop: source_window: %#x time: %d",
2843 source_window, time));
2845 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2846 display_x11 = GDK_DISPLAY_X11 (display);
2848 xdnd_precache_atoms (display);
2850 if ((display_x11->current_dest_drag != NULL) &&
2851 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2852 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2854 GdkDragContextPrivateX11 *private;
2855 private = PRIVATE_DATA (display_x11->current_dest_drag);
2857 event->dnd.type = GDK_DROP_START;
2859 event->dnd.context = display_x11->current_dest_drag;
2860 g_object_ref (display_x11->current_dest_drag);
2862 event->dnd.time = time;
2863 event->dnd.x_root = private->last_x;
2864 event->dnd.y_root = private->last_y;
2866 return GDK_FILTER_TRANSLATE;
2869 return GDK_FILTER_REMOVE;
2872 /*************************************************************
2873 ************************** Public API ***********************
2874 *************************************************************/
2876 _gdk_dnd_init (GdkDisplay *display)
2881 gdk_display_add_client_message_filter (
2883 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2884 motif_dnd_filter, NULL);
2886 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2888 gdk_display_add_client_message_filter (
2890 gdk_atom_intern (xdnd_filters[i].atom_name, FALSE),
2891 xdnd_filters[i].func, NULL);
2898 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2900 if (context->dest_window)
2902 switch (context->protocol)
2904 case GDK_DRAG_PROTO_MOTIF:
2905 motif_send_leave (context, time);
2907 case GDK_DRAG_PROTO_XDND:
2908 xdnd_send_leave (context);
2910 case GDK_DRAG_PROTO_ROOTWIN:
2911 case GDK_DRAG_PROTO_NONE:
2916 g_object_unref (context->dest_window);
2917 context->dest_window = NULL;
2923 * @window: the source window for this drag.
2924 * @targets: the list of offered targets.
2926 * Starts a drag and creates a new drag context for it.
2928 * This function is called by the drag source.
2930 * Return value: a newly created #GdkDragContext.
2933 gdk_drag_begin (GdkWindow *window,
2936 GdkDragContext *new_context;
2938 g_return_val_if_fail (window != NULL, NULL);
2940 new_context = gdk_drag_context_new ();
2941 new_context->is_source = TRUE;
2942 new_context->source_window = window;
2943 g_object_ref (window);
2945 new_context->targets = g_list_copy (targets);
2946 precache_target_list (new_context);
2948 new_context->actions = 0;
2954 * gdk_drag_get_protocol_for_display:
2955 * @display: the #GdkDisplay where the destination window resides
2956 * @xid: the X id of the destination window.
2957 * @protocol: location where the supported DND protocol is returned.
2958 * @returns: the X id of the window where the drop should happen. This
2959 * may be @xid or the X id of a proxy window, or None if @xid doesn't
2960 * support Drag and Drop.
2962 * Finds out the DND protocol supported by a window.
2967 gdk_drag_get_protocol_for_display (GdkDisplay *display,
2969 GdkDragProtocol *protocol)
2973 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
2975 base_precache_atoms (display);
2977 /* Check for a local drag
2979 window = gdk_window_lookup_for_display (display, xid);
2981 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2983 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
2985 *protocol = GDK_DRAG_PROTO_XDND;
2986 xdnd_precache_atoms (display);
2987 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
2994 if ((retval = xdnd_check_dest (display, xid)))
2996 *protocol = GDK_DRAG_PROTO_XDND;
2997 xdnd_precache_atoms (display);
2998 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3001 else if ((retval = motif_check_dest (display, xid)))
3003 *protocol = GDK_DRAG_PROTO_MOTIF;
3004 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3009 /* Check if this is a root window */
3011 gboolean rootwin = FALSE;
3014 unsigned long nitems, after;
3015 unsigned char *data;
3017 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3020 gdk_error_trap_push ();
3024 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3025 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
3026 0, 0, False, AnyPropertyType,
3027 &type, &format, &nitems, &after, &data) == Success &&
3035 /* Until I find out what window manager the next one is for,
3036 * I'm leaving it commented out. It's supported in the
3037 * xscreensaver sources, though.
3042 if (XGetWindowProperty (gdk_display, win,
3043 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
3044 0, 0, False, AnyPropertyType,
3045 &type, &format, &nitems, &data) &&
3051 gdk_error_trap_pop ();
3055 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3060 *protocol = GDK_DRAG_PROTO_NONE;
3064 static GdkWindowCache *
3065 drag_context_find_window_cache (GdkDragContext *context,
3068 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3070 GdkWindowCache *cache;
3072 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
3074 cache = tmp_list->data;
3075 if (cache->screen == screen)
3079 cache = gdk_window_cache_new (screen);
3080 private->window_caches = g_slist_prepend (private->window_caches, cache);
3086 * gdk_drag_find_window_for_screen:
3087 * @context: a #GdkDragContext
3088 * @drag_window: a window which may be at the pointer position, but
3089 * should be ignored, since it is put up by the drag source as an icon.
3090 * @screen: the screen where the destination window is sought.
3091 * @x_root: the x position of the pointer in root coordinates.
3092 * @y_root: the y position of the pointer in root coordinates.
3093 * @dest_window: location to store the destination window in.
3094 * @protocol: location to store the DND protocol in.
3096 * Finds the destination window and DND protocol to use at the
3097 * given pointer position.
3099 * This function is called by the drag source to obtain the
3100 * @dest_window and @protocol parameters for gdk_drag_motion().
3105 gdk_drag_find_window_for_screen (GdkDragContext *context,
3106 GdkWindow *drag_window,
3110 GdkWindow **dest_window,
3111 GdkDragProtocol *protocol)
3113 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3114 GdkWindowCache *window_cache;
3115 GdkDisplay *display;
3118 g_return_if_fail (context != NULL);
3120 display = GDK_WINDOW_DISPLAY (context->source_window);
3122 window_cache = drag_context_find_window_cache (context, screen);
3124 dest = get_client_window_at_coords (window_cache,
3126 GDK_DRAWABLE_XID (drag_window) : None,
3129 if (private->dest_xid != dest)
3132 private->dest_xid = dest;
3134 /* Check if new destination accepts drags, and which protocol */
3136 /* There is some ugliness here. We actually need to pass
3137 * _three_ pieces of information to drag_motion - dest_window,
3138 * protocol, and the XID of the unproxied window. The first
3139 * two are passed explicitely, the third implicitly through
3140 * protocol->dest_xid.
3142 if ((recipient = gdk_drag_get_protocol_for_display (display, dest, protocol)))
3144 *dest_window = gdk_window_lookup_for_display (display, recipient);
3146 g_object_ref (*dest_window);
3148 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3151 *dest_window = NULL;
3155 *dest_window = context->dest_window;
3157 g_object_ref (*dest_window);
3158 *protocol = context->protocol;
3164 * @context: a #GdkDragContext.
3165 * @dest_window: the new destination window, obtained by
3166 * gdk_drag_find_window().
3167 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3168 * @x_root: the x position of the pointer in root coordinates.
3169 * @y_root: the y position of the pointer in root coordinates.
3170 * @suggested_action: the suggested action.
3171 * @possible_actions: the possible actions.
3172 * @time_: the timestamp for this operation.
3174 * Updates the drag context when the pointer moves or the
3175 * set of actions changes.
3177 * This function is called by the drag source.
3179 * Return value: FIXME
3182 gdk_drag_motion (GdkDragContext *context,
3183 GdkWindow *dest_window,
3184 GdkDragProtocol protocol,
3187 GdkDragAction suggested_action,
3188 GdkDragAction possible_actions,
3191 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3193 g_return_val_if_fail (context != NULL, FALSE);
3195 private->old_actions = context->actions;
3196 context->actions = possible_actions;
3198 if (private->old_actions != possible_actions)
3199 private->xdnd_actions_set = FALSE;
3201 /* When we have a Xdnd target, make sure our XdndActionList
3202 * matches the current actions;
3204 if (protocol == GDK_DRAG_PROTO_XDND && !private->xdnd_actions_set)
3208 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3209 xdnd_set_actions (context);
3210 else if (context->dest_window == dest_window)
3212 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3213 GdkDragContext *dest_context;
3215 dest_context = gdk_drag_context_find (display, FALSE,
3216 GDK_DRAWABLE_XID (context->source_window),
3217 GDK_DRAWABLE_XID (dest_window));
3221 dest_context->actions = context->actions;
3222 PRIVATE_DATA (dest_context)->xdnd_have_actions = TRUE;
3228 if (context->dest_window != dest_window)
3230 GdkEvent temp_event;
3232 /* Send a leave to the last destination */
3233 gdk_drag_do_leave (context, time);
3234 private->drag_status = GDK_DRAG_STATUS_DRAG;
3236 /* Check if new destination accepts drags, and which protocol */
3240 context->dest_window = dest_window;
3241 private->drop_xid = private->dest_xid;
3242 g_object_ref (context->dest_window);
3243 context->protocol = protocol;
3247 case GDK_DRAG_PROTO_MOTIF:
3248 motif_send_enter (context, time);
3251 case GDK_DRAG_PROTO_XDND:
3252 xdnd_send_enter (context);
3255 case GDK_DRAG_PROTO_ROOTWIN:
3256 case GDK_DRAG_PROTO_NONE:
3260 private->old_action = suggested_action;
3261 context->suggested_action = suggested_action;
3262 private->old_actions = possible_actions;
3266 context->dest_window = NULL;
3267 private->drop_xid = None;
3268 context->action = 0;
3271 /* Push a status event, to let the client know that
3275 temp_event.dnd.type = GDK_DRAG_STATUS;
3276 temp_event.dnd.window = context->source_window;
3277 /* We use this to signal a synthetic status. Perhaps
3278 * we should use an extra field...
3280 temp_event.dnd.send_event = TRUE;
3282 temp_event.dnd.context = context;
3283 temp_event.dnd.time = time;
3285 gdk_event_put (&temp_event);
3289 private->old_action = context->suggested_action;
3290 context->suggested_action = suggested_action;
3293 /* Send a drag-motion event */
3295 private->last_x = x_root;
3296 private->last_y = y_root;
3298 if (context->dest_window)
3300 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3302 switch (context->protocol)
3304 case GDK_DRAG_PROTO_MOTIF:
3305 motif_send_motion (context, x_root, y_root, suggested_action, time);
3308 case GDK_DRAG_PROTO_XDND:
3309 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3312 case GDK_DRAG_PROTO_ROOTWIN:
3314 GdkEvent temp_event;
3315 /* GTK+ traditionally has used application/x-rootwin-drop,
3316 * but the XDND spec specifies x-rootwindow-drop.
3318 GdkAtom target1 = gdk_atom_intern ("application/x-rootwindow-drop", FALSE);
3319 GdkAtom target2 = gdk_atom_intern ("application/x-rootwin-drop", FALSE);
3321 if (g_list_find (context->targets,
3322 GDK_ATOM_TO_POINTER (target1)) ||
3323 g_list_find (context->targets,
3324 GDK_ATOM_TO_POINTER (target2)))
3325 context->action = context->suggested_action;
3327 context->action = 0;
3329 temp_event.dnd.type = GDK_DRAG_STATUS;
3330 temp_event.dnd.window = context->source_window;
3331 temp_event.dnd.send_event = FALSE;
3332 temp_event.dnd.context = context;
3333 temp_event.dnd.time = time;
3335 gdk_event_put (&temp_event);
3338 case GDK_DRAG_PROTO_NONE:
3339 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3354 * @context: a #GdkDragContext.
3355 * @time_: the timestamp for this operation.
3357 * Drops on the current destination.
3359 * This function is called by the drag source.
3362 gdk_drag_drop (GdkDragContext *context,
3365 g_return_if_fail (context != NULL);
3367 if (context->dest_window)
3369 switch (context->protocol)
3371 case GDK_DRAG_PROTO_MOTIF:
3372 motif_send_leave (context, time);
3373 motif_send_drop (context, time);
3376 case GDK_DRAG_PROTO_XDND:
3377 xdnd_send_drop (context, time);
3380 case GDK_DRAG_PROTO_ROOTWIN:
3381 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3383 case GDK_DRAG_PROTO_NONE:
3384 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3394 * @context: a #GdkDragContext.
3395 * @time_: the timestamp for this operation.
3397 * Aborts a drag without dropping.
3399 * This function is called by the drag source.
3402 gdk_drag_abort (GdkDragContext *context,
3405 g_return_if_fail (context != NULL);
3407 gdk_drag_do_leave (context, time);
3410 /* Destination side */
3414 * @context: a #GdkDragContext.
3415 * @action: the selected action which will be taken when a drop happens,
3416 * or 0 to indicate that a drop will not be accepted.
3417 * @time_: the timestamp for this operation.
3419 * Selects one of the actions offered by the drag source.
3421 * This function is called by the drag destination in response to
3422 * gdk_drag_motion() called by the drag source.
3425 gdk_drag_status (GdkDragContext *context,
3426 GdkDragAction action,
3429 GdkDragContextPrivateX11 *private;
3431 GdkDisplay *display;
3433 g_return_if_fail (context != NULL);
3435 private = PRIVATE_DATA (context);
3436 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3438 context->action = action;
3440 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3442 gboolean need_coords = FALSE;
3444 xev.xclient.type = ClientMessage;
3445 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3446 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3447 xev.xclient.format = 8;
3448 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3450 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3452 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3456 if ((action != 0) != (private->old_action != 0))
3460 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3464 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3468 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3473 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3477 case GDK_ACTION_MOVE:
3478 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3480 case GDK_ACTION_COPY:
3481 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3483 case GDK_ACTION_LINK:
3484 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3487 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3492 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3494 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3496 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3500 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3501 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3504 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3506 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3507 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3509 if (!_gdk_send_xevent (display,
3510 GDK_DRAWABLE_XID (context->source_window),
3513 g_message ("Send event to %lx failed",
3514 GDK_DRAWABLE_XID (context->source_window)));
3516 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3518 xev.xclient.type = ClientMessage;
3519 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3520 xev.xclient.format = 32;
3521 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3523 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3524 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3525 xev.xclient.data.l[2] = 0;
3526 xev.xclient.data.l[3] = 0;
3527 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3529 if (!xdnd_send_xevent (context, context->source_window,
3532 g_message ("Send event to %lx failed",
3533 GDK_DRAWABLE_XID (context->source_window)));
3536 private->old_action = action;
3541 * @context: a #GdkDragContext.
3542 * @ok: %TRUE if the drop is accepted.
3543 * @time_: the timestamp for this operation.
3545 * Accepts or rejects a drop.
3547 * This function is called by the drag destination in response
3548 * to a drop initiated by the drag source.
3551 gdk_drop_reply (GdkDragContext *context,
3555 GdkDragContextPrivateX11 *private;
3557 g_return_if_fail (context != NULL);
3559 private = PRIVATE_DATA (context);
3561 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3563 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3566 xev.xclient.type = ClientMessage;
3567 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3568 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3569 xev.xclient.format = 8;
3571 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3572 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3574 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3575 (XmDROP_SITE_VALID << 4) |
3576 (XmDROP_NOOP << 8) |
3579 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3580 (XmNO_DROP_SITE << 4) |
3581 (XmDROP_NOOP << 8) |
3582 (XmDROP_CANCEL << 12);
3583 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3584 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3585 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3586 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3587 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3589 _gdk_send_xevent (display,
3590 GDK_DRAWABLE_XID (context->source_window),
3597 * @context: a #GtkDragContext.
3598 * @success: %TRUE if the data was successfully received.
3599 * @time_: the timestamp for this operation.
3601 * Ends the drag operation after a drop.
3603 * This function is called by the drag destination.
3606 gdk_drop_finish (GdkDragContext *context,
3610 g_return_if_fail (context != NULL);
3612 if (context->protocol == GDK_DRAG_PROTO_XDND)
3614 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3617 xev.xclient.type = ClientMessage;
3618 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3619 xev.xclient.format = 32;
3620 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3622 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3623 xev.xclient.data.l[1] = 0;
3624 xev.xclient.data.l[2] = 0;
3625 xev.xclient.data.l[3] = 0;
3626 xev.xclient.data.l[4] = 0;
3628 if (!xdnd_send_xevent (context, context->source_window,
3631 g_message ("Send event to %lx failed",
3632 GDK_DRAWABLE_XID (context->source_window)));
3638 gdk_window_register_dnd (GdkWindow *window)
3640 static gulong xdnd_version = 3;
3641 MotifDragReceiverInfo info;
3642 Atom motif_drag_receiver_info_atom;
3643 GdkDisplay *display = gdk_drawable_get_display (window);
3645 g_return_if_fail (window != NULL);
3647 base_precache_atoms (display);
3649 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3652 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3654 /* Set Motif drag receiver information property */
3656 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3657 "_MOTIF_DRAG_RECEIVER_INFO");
3658 info.byte_order = local_byte_order;
3659 info.protocol_version = 0;
3660 info.protocol_style = XmDRAG_DYNAMIC;
3661 info.proxy_window = None;
3662 info.num_drop_sites = 0;
3663 info.total_size = sizeof(info);
3665 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3666 motif_drag_receiver_info_atom,
3667 motif_drag_receiver_info_atom,
3674 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3675 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3676 GDK_DRAWABLE_XID (window),
3677 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3678 XA_ATOM, 32, PropModeReplace,
3679 (guchar *)&xdnd_version, 1);
3683 * gdk_drag_get_selection:
3684 * @context: a #GdkDragContext.
3686 * Returns the selection atom for the current source window.
3688 * Return value: the selection atom.
3691 gdk_drag_get_selection (GdkDragContext *context)
3693 g_return_val_if_fail (context != NULL, GDK_NONE);
3695 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3696 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3697 (PRIVATE_DATA (context))->motif_selection);
3698 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3699 return gdk_atom_intern ("XdndSelection", FALSE);