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() */
33 #include "gdkproperty.h"
34 #include "gdkinternals.h"
35 #include "gdkprivate-x11.h"
37 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
41 GDK_DRAG_STATUS_MOTION_WAIT,
42 GDK_DRAG_STATUS_ACTION_WAIT,
48 gint x, y, width, height;
54 GHashTable *child_hash;
58 /* Structure that holds information about a drag in progress.
59 * this is used on both source and destination sides.
61 struct _GdkDragContextPrivateX11 {
62 GdkDragContext context;
64 GdkAtom motif_selection;
65 GdkAtom xdnd_selection;
68 guint16 last_x; /* Coordinates from last event */
70 GdkDragAction old_action; /* The last action we sent to the source */
71 GdkDragAction old_actions; /* The last actions we sent to the source */
72 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
74 Window dest_xid; /* The last window we looked up */
75 Window drop_xid; /* The (non-proxied) window that is receiving drops */
76 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
77 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
78 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
79 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
80 guint drag_status : 4; /* current status of drag */
82 GdkWindowCache *window_cache;
85 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
87 GdkDragContext *current_dest_drag = NULL;
89 /* Forward declarations */
91 static void gdk_window_cache_destroy (GdkWindowCache *cache);
93 static void motif_read_target_table (void);
94 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
98 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
102 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
106 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
110 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
114 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
118 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
122 static void xdnd_manage_source_filter (GdkDragContext *context,
124 gboolean add_filter);
126 static void gdk_drag_context_init (GdkDragContext *dragcontext);
127 static void gdk_drag_context_class_init (GdkDragContextClass *klass);
128 static void gdk_drag_context_finalize (GObject *object);
130 static gpointer parent_class = NULL;
131 static GList *contexts;
134 gdk_drag_context_get_type (void)
136 static GType object_type = 0;
140 static const GTypeInfo object_info =
142 sizeof (GdkDragContextClass),
143 (GBaseInitFunc) NULL,
144 (GBaseFinalizeFunc) NULL,
145 (GClassInitFunc) gdk_drag_context_class_init,
146 NULL, /* class_finalize */
147 NULL, /* class_data */
148 sizeof (GdkDragContext),
150 (GInstanceInitFunc) gdk_drag_context_init,
153 object_type = g_type_register_static (G_TYPE_OBJECT,
162 gdk_drag_context_init (GdkDragContext *dragcontext)
164 GdkDragContextPrivateX11 *private;
166 private = g_new0 (GdkDragContextPrivateX11, 1);
168 dragcontext->windowing_data = private;
170 contexts = g_list_prepend (contexts, dragcontext);
174 gdk_drag_context_class_init (GdkDragContextClass *klass)
176 GObjectClass *object_class = G_OBJECT_CLASS (klass);
178 parent_class = g_type_class_peek_parent (klass);
180 object_class->finalize = gdk_drag_context_finalize;
184 gdk_drag_context_finalize (GObject *object)
186 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
187 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
189 g_list_free (context->targets);
191 if (context->source_window)
193 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
195 xdnd_manage_source_filter (context, context->source_window, FALSE);
197 gdk_window_unref (context->source_window);
200 if (context->dest_window)
201 gdk_window_unref (context->dest_window);
203 if (private->window_cache)
204 gdk_window_cache_destroy (private->window_cache);
206 contexts = g_list_remove (contexts, context);
210 G_OBJECT_CLASS (parent_class)->finalize (object);
216 gdk_drag_context_new (void)
218 return g_object_new (gdk_drag_context_get_type (), NULL);
222 gdk_drag_context_ref (GdkDragContext *context)
224 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
226 g_object_ref (G_OBJECT (context));
230 gdk_drag_context_unref (GdkDragContext *context)
232 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
234 g_object_unref (G_OBJECT (context));
237 static GdkDragContext *
238 gdk_drag_context_find (gboolean is_source,
242 GList *tmp_list = contexts;
243 GdkDragContext *context;
244 GdkDragContextPrivateX11 *private;
245 Window context_dest_xid;
249 context = (GdkDragContext *)tmp_list->data;
250 private = PRIVATE_DATA (context);
252 context_dest_xid = context->dest_window ?
255 GDK_DRAWABLE_XID (context->dest_window)) :
258 if ((!context->is_source == !is_source) &&
259 ((source_xid == None) || (context->source_window &&
260 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
261 ((dest_xid == None) || (context_dest_xid == dest_xid)))
264 tmp_list = tmp_list->next;
270 /* Utility functions */
273 gdk_window_cache_add (GdkWindowCache *cache,
275 gint x, gint y, gint width, gint height,
278 GdkCacheChild *child = g_new (GdkCacheChild, 1);
283 child->width = width;
284 child->height = height;
285 child->mapped = mapped;
287 cache->children = g_list_prepend (cache->children, child);
288 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
292 static GdkFilterReturn
293 gdk_window_cache_filter (GdkXEvent *xev,
297 XEvent *xevent = (XEvent *)xev;
298 GdkWindowCache *cache = data;
300 switch (xevent->type)
302 case CirculateNotify:
304 case ConfigureNotify:
306 XConfigureEvent *xce = &xevent->xconfigure;
309 node = g_hash_table_lookup (cache->child_hash,
310 GUINT_TO_POINTER (xce->window));
313 GdkCacheChild *child = node->data;
316 child->width = xce->width;
317 child->height = xce->height;
318 if (xce->above == None && (node->next))
320 GList *last = g_list_last (cache->children);
321 cache->children = g_list_remove_link (cache->children, node);
328 GList *above_node = g_hash_table_lookup (cache->child_hash,
329 GUINT_TO_POINTER (xce->above));
330 if (above_node && node->prev != above_node)
332 cache->children = g_list_remove_link (cache->children, node);
333 node->next = above_node->next;
335 node->next->prev = node;
336 node->prev = above_node;
337 above_node->next = node;
345 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
347 if (!g_hash_table_lookup (cache->child_hash,
348 GUINT_TO_POINTER (xcwe->window)))
349 gdk_window_cache_add (cache, xcwe->window,
350 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
356 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
359 node = g_hash_table_lookup (cache->child_hash,
360 GUINT_TO_POINTER (xdwe->window));
363 g_hash_table_remove (cache->child_hash,
364 GUINT_TO_POINTER (xdwe->window));
365 cache->children = g_list_remove_link (cache->children, node);
367 g_list_free_1 (node);
373 XMapEvent *xme = &xevent->xmap;
376 node = g_hash_table_lookup (cache->child_hash,
377 GUINT_TO_POINTER (xme->window));
380 GdkCacheChild *child = node->data;
381 child->mapped = TRUE;
389 XMapEvent *xume = &xevent->xmap;
392 node = g_hash_table_lookup (cache->child_hash,
393 GUINT_TO_POINTER (xume->window));
396 GdkCacheChild *child = node->data;
397 child->mapped = FALSE;
402 return GDK_FILTER_CONTINUE;
404 return GDK_FILTER_REMOVE;
407 static GdkWindowCache *
408 gdk_window_cache_new (void)
410 XWindowAttributes xwa;
411 Window root, parent, *children;
412 unsigned int nchildren;
415 gint old_warnings = gdk_error_warnings;
417 GdkWindowCache *result = g_new (GdkWindowCache, 1);
419 result->children = NULL;
420 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
422 XGetWindowAttributes (gdk_display, gdk_root_window, &xwa);
423 result->old_event_mask = xwa.your_event_mask;
424 XSelectInput (gdk_display, gdk_root_window,
425 result->old_event_mask | SubstructureNotifyMask);
426 gdk_window_add_filter (gdk_parent_root,
427 gdk_window_cache_filter, result);
430 gdk_error_warnings = 0;
432 if (XQueryTree(gdk_display, gdk_root_window,
433 &root, &parent, &children, &nchildren) == 0)
436 for (i = 0; i < nchildren ; i++)
438 XGetWindowAttributes (gdk_display, children[i], &xwa);
440 gdk_window_cache_add (result, children[i],
441 xwa.x, xwa.y, xwa.width, xwa.height,
442 xwa.map_state != IsUnmapped);
448 gdk_window_cache_add (result, children[i],
449 xwa.x, xwa.y, xwa.width, xwa.height,
450 (xwa.map_state != IsUnmapped));
456 gdk_error_warnings = old_warnings;
462 gdk_window_cache_destroy (GdkWindowCache *cache)
464 XSelectInput (gdk_display, gdk_root_window, cache->old_event_mask);
465 gdk_window_remove_filter (gdk_parent_root,
466 gdk_window_cache_filter, cache);
468 g_list_foreach (cache->children, (GFunc)g_free, NULL);
469 g_list_free (cache->children);
470 g_hash_table_destroy (cache->child_hash);
476 get_client_window_at_coords_recurse (Window win,
480 Window root, tmp_parent, *children;
481 unsigned int nchildren;
486 unsigned long nitems, after;
489 static Atom wm_state_atom = None;
492 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
494 XGetWindowProperty (gdk_display, win,
495 wm_state_atom, 0, 0, False, AnyPropertyType,
496 &type, &format, &nitems, &after, &data);
512 /* This is beautiful! Damn Enlightenment and click-to-focus */
513 XTranslateCoordinates (gdk_display, gdk_root_window, win,
514 x_root, y_root, &dest_x, &dest_y, &child);
524 if (XQueryTree(gdk_display, win,
525 &root, &tmp_parent, &children, &nchildren) == 0)
530 for (i = nchildren - 1; (i >= 0) && (child == None); i--)
532 XWindowAttributes xwa;
534 XGetWindowAttributes (gdk_display, children[i], &xwa);
538 else if ((xwa.map_state == IsViewable) && (xwa.class == InputOutput) &&
539 (x >= xwa.x) && (x < xwa.x + (gint)xwa.width) &&
540 (y >= xwa.y) && (y < xwa.y + (gint)xwa.height))
555 return get_client_window_at_coords_recurse (child, x, y);
561 get_client_window_at_coords (GdkWindowCache *cache,
567 Window retval = None;
569 gint old_warnings = gdk_error_warnings;
572 gdk_error_warnings = 0;
574 tmp_list = cache->children;
576 while (tmp_list && !retval)
578 GdkCacheChild *child = tmp_list->data;
580 if ((child->xid != ignore) && (child->mapped))
582 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
583 (y_root >= child->y) && (y_root < child->y + child->height))
585 retval = get_client_window_at_coords_recurse (child->xid,
593 tmp_list = tmp_list->next;
596 gdk_error_warnings = old_warnings;
600 return gdk_root_window;
605 get_client_window_at_coords_recurse (Window win,
612 unsigned long nitems, after;
616 static Atom wm_state_atom = None;
619 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
621 XGetWindowProperty (gdk_display, win,
622 wm_state_atom, 0, 0, False, AnyPropertyType,
623 &type, &format, &nitems, &after, &data);
638 XTranslateCoordinates (gdk_display, gdk_root_window, win,
639 x_root, y_root, &dest_x, &dest_y, &child);
649 return get_client_window_at_coords_recurse (child, x_root, y_root);
655 get_client_window_at_coords (Window ignore,
659 Window root, parent, *children;
660 unsigned int nchildren;
662 Window retval = None;
664 gint old_warnings = gdk_error_warnings;
667 gdk_error_warnings = 0;
669 if (XQueryTree(gdk_display, gdk_root_window,
670 &root, &parent, &children, &nchildren) == 0)
673 for (i = nchildren - 1; (i >= 0) && (retval == None); i--)
675 if (children[i] != ignore)
677 XWindowAttributes xwa;
679 XGetWindowAttributes (gdk_display, children[i], &xwa);
683 else if ((xwa.map_state == IsViewable) &&
684 (x_root >= xwa.x) && (x_root < xwa.x + (gint)xwa.width) &&
685 (y_root >= xwa.y) && (y_root < xwa.y + (gint)xwa.height))
687 retval = get_client_window_at_coords_recurse (children[i],
690 retval = children[i];
697 gdk_error_warnings = old_warnings;
701 return gdk_root_window;
705 /*************************************************************
706 ***************************** MOTIF *************************
707 *************************************************************/
709 /* values used in the message type for Motif DND */
722 /* Values used to specify type of protocol to use */
726 XmDRAG_PREFER_PREREGISTER,
728 XmDRAG_PREFER_DYNAMIC,
730 XmDRAG_PREFER_RECEIVER
733 /* Operation codes */
741 /* Drop site status */
743 XmNO_DROP_SITE = 0x01,
744 XmDROP_SITE_INVALID = 0x02,
745 XmDROP_SITE_VALID = 0x03
748 /* completion status */
756 /* Static data for MOTIF DND */
757 static GList **motif_target_lists = NULL;
758 static gint motif_n_target_lists = -1;
760 /* Byte swapping routines. The motif specification leaves it
761 * up to us to save a few bytes in the client messages
763 static gchar local_byte_order = '\0';
765 #ifdef G_ENABLE_DEBUG
767 print_target_list (GList *targets)
771 gchar *name = gdk_atom_name (GPOINTER_TO_INT (targets->data));
772 g_message ("\t%s", name);
774 targets = targets->next;
777 #endif /* G_ENABLE_DEBUG */
780 init_byte_order (void)
782 guint32 myint = 0x01020304;
783 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
787 card16_to_host (guint16 x, gchar byte_order) {
788 if (byte_order == local_byte_order)
791 return (x << 8) | (x >> 8);
795 card32_to_host (guint32 x, gchar byte_order) {
796 if (byte_order == local_byte_order)
799 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
802 /* Motif packs together fields of varying length into the
803 * client message. We can't rely on accessing these
804 * through data.s[], data.l[], etc, because on some architectures
805 * (i.e., Alpha) these won't be valid for format == 8.
808 #define MOTIF_XCLIENT_BYTE(xevent,i) \
809 (xevent)->xclient.data.b[i]
810 #define MOTIF_XCLIENT_SHORT(xevent,i) \
811 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
812 #define MOTIF_XCLIENT_LONG(xevent,i) \
813 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
815 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
816 #define MOTIF_UNPACK_SHORT(xevent,i) \
817 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
818 #define MOTIF_UNPACK_LONG(xevent,i) \
819 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
821 /***** Dest side ***********/
823 /* Property placed on source windows */
824 typedef struct _MotifDragInitiatorInfo {
826 guint8 protocol_version;
827 guint16 targets_index;
828 guint32 selection_atom;
829 } MotifDragInitiatorInfo;
831 /* Header for target table on the drag window */
832 typedef struct _MotifTargetTableHeader {
834 guchar protocol_version;
837 } MotifTargetTableHeader;
839 /* Property placed on target windows */
840 typedef struct _MotifDragReceiverInfo {
842 guint8 protocol_version;
843 guint8 protocol_style;
845 guint32 proxy_window;
846 guint16 num_drop_sites;
849 } MotifDragReceiverInfo;
851 static Window motif_drag_window = None;
852 static GdkWindow *motif_drag_gdk_window = NULL;
854 static GdkAtom motif_drag_targets_atom = GDK_NONE;
855 static GdkAtom motif_drag_receiver_info_atom = GDK_NONE;
857 /* Target table handling */
860 motif_drag_window_filter (GdkXEvent *xevent,
864 XEvent *xev = (XEvent *)xevent;
866 switch (xev->xany.type)
869 motif_drag_window = None;
870 motif_drag_gdk_window = NULL;
873 if (motif_target_lists &&
874 motif_drag_targets_atom &&
875 (xev->xproperty.atom == motif_drag_targets_atom))
876 motif_read_target_table();
879 return GDK_FILTER_REMOVE;
882 static Atom motif_drag_window_atom = GDK_NONE;
885 motif_lookup_drag_window (Display *display)
887 Window retval = None;
888 gulong bytes_after, nitems;
893 XGetWindowProperty (gdk_display, gdk_root_window, motif_drag_window_atom,
895 XA_WINDOW, &type, &format, &nitems, &bytes_after,
898 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
900 retval = *(Window *)data;
902 g_message ("Found drag window %#lx\n", motif_drag_window));
911 /* Finds the window where global Motif drag information is stored.
912 * If it doesn't exist and 'create' is TRUE, create one.
915 motif_find_drag_window (gboolean create)
917 if (!motif_drag_window)
919 if (!motif_drag_window_atom)
920 motif_drag_window_atom = gdk_atom_intern ("_MOTIF_DRAG_WINDOW", TRUE);
922 motif_drag_window = motif_lookup_drag_window (gdk_display);
924 if (!motif_drag_window && create)
926 /* Create a persistant window. (Copied from LessTif) */
929 XSetWindowAttributes attr;
930 display = XOpenDisplay (gdk_display_name);
931 XSetCloseDownMode (display, RetainPermanent);
933 XGrabServer (display);
935 motif_drag_window = motif_lookup_drag_window (display);
937 if (!motif_drag_window)
939 attr.override_redirect = True;
940 attr.event_mask = PropertyChangeMask;
943 XCreateWindow(display, DefaultRootWindow(display),
944 -100, -100, 10, 10, 0, 0,
945 InputOnly, CopyFromParent,
946 (CWOverrideRedirect | CWEventMask), &attr);
949 g_message ("Created drag window %#lx\n", motif_drag_window));
951 XChangeProperty (display, gdk_root_window,
952 motif_drag_window_atom, XA_WINDOW,
954 (guchar *)&motif_drag_window_atom, 1);
957 XUngrabServer (display);
958 XCloseDisplay (display);
961 /* There is a miniscule race condition here if the drag window
962 * gets destroyed exactly now.
964 if (motif_drag_window)
966 motif_drag_gdk_window = gdk_window_foreign_new (motif_drag_window);
967 gdk_window_add_filter (motif_drag_gdk_window,
968 motif_drag_window_filter,
973 return motif_drag_window;
977 motif_read_target_table (void)
979 gulong bytes_after, nitems;
984 if (!motif_drag_targets_atom)
985 motif_drag_targets_atom = gdk_atom_intern ("_MOTIF_DRAG_TARGETS", FALSE);
987 if (motif_target_lists)
989 for (i=0; i<motif_n_target_lists; i++)
990 g_list_free (motif_target_lists[i]);
992 g_free (motif_target_lists);
993 motif_target_lists = NULL;
994 motif_n_target_lists = 0;
997 if (motif_find_drag_window (FALSE))
999 MotifTargetTableHeader *header = NULL;
1000 guchar *target_bytes = NULL;
1002 gboolean success = FALSE;
1004 gdk_error_trap_push ();
1005 XGetWindowProperty (gdk_display, motif_drag_window,
1006 motif_drag_targets_atom,
1007 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1008 motif_drag_targets_atom,
1009 &type, &format, &nitems, &bytes_after,
1010 (guchar **)&header);
1012 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1015 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1016 header->total_size = card32_to_host (header->total_size, header->byte_order);
1018 gdk_error_trap_push ();
1019 XGetWindowProperty (gdk_display, motif_drag_window, motif_drag_targets_atom,
1020 (sizeof(MotifTargetTableHeader)+3)/4,
1021 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1023 motif_drag_targets_atom, &type, &format, &nitems,
1024 &bytes_after, &target_bytes);
1026 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
1027 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1030 motif_n_target_lists = header->n_lists;
1031 motif_target_lists = g_new0 (GList *, motif_n_target_lists);
1034 for (i=0; i<header->n_lists; i++)
1039 if (p + sizeof(guint16) - target_bytes > nitems)
1042 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1044 /* We need to make a copy of the targets, since it may
1047 targets = g_new (guint32, n_targets);
1048 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1050 p += sizeof(guint16) + n_targets * sizeof(guint32);
1051 if (p - target_bytes > nitems)
1054 for (j=0; j<n_targets; j++)
1055 motif_target_lists[i] =
1056 g_list_prepend (motif_target_lists[i],
1057 GUINT_TO_POINTER (card32_to_host (targets[j],
1058 header->byte_order)));
1060 motif_target_lists[i] = g_list_reverse (motif_target_lists[i]);
1070 XFree (target_bytes);
1074 if (motif_target_lists)
1076 g_free (motif_target_lists);
1077 motif_target_lists = NULL;
1078 motif_n_target_lists = 0;
1080 g_warning ("Error reading Motif target table\n");
1086 targets_sort_func (gconstpointer a, gconstpointer b)
1088 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1089 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1092 /* Check if given (sorted) list is in the targets table */
1094 motif_target_table_check (GList *sorted)
1096 GList *tmp_list1, *tmp_list2;
1099 for (i=0; i<motif_n_target_lists; i++)
1101 tmp_list1 = motif_target_lists[i];
1104 while (tmp_list1 && tmp_list2)
1106 if (tmp_list1->data != tmp_list2->data)
1109 tmp_list1 = tmp_list1->next;
1110 tmp_list2 = tmp_list2->next;
1112 if (!tmp_list1 && !tmp_list2) /* Found it */
1120 motif_add_to_target_table (GList *targets)
1122 GList *sorted = NULL;
1127 /* make a sorted copy of the list */
1131 sorted = g_list_insert_sorted (sorted, targets->data, targets_sort_func);
1132 targets = targets->next;
1135 /* First check if it is their already */
1137 if (motif_target_lists)
1138 index = motif_target_table_check (sorted);
1140 /* We need to grab the server while doing this, to ensure
1146 /* We need to make sure that it exists _before_ we grab the
1147 * server, since we can't open a new connection after we
1150 motif_find_drag_window (TRUE);
1152 XGrabServer(gdk_display);
1153 motif_read_target_table();
1155 /* Check again, in case it was added in the meantime */
1157 if (motif_target_lists)
1158 index = motif_target_table_check (sorted);
1162 guint32 total_size = 0;
1166 MotifTargetTableHeader *header;
1168 if (!motif_target_lists)
1170 motif_target_lists = g_new (GList *, 1);
1171 motif_n_target_lists = 1;
1175 motif_n_target_lists++;
1176 motif_target_lists = g_realloc (motif_target_lists,
1177 sizeof(GList *) * motif_n_target_lists);
1179 motif_target_lists[motif_n_target_lists - 1] = sorted;
1181 index = motif_n_target_lists - 1;
1183 total_size = sizeof (MotifTargetTableHeader);
1184 for (i = 0; i < motif_n_target_lists ; i++)
1185 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (motif_target_lists[i]);
1187 data = g_malloc (total_size);
1189 header = (MotifTargetTableHeader *)data;
1190 p = data + sizeof(MotifTargetTableHeader);
1192 header->byte_order = local_byte_order;
1193 header->protocol_version = 0;
1194 header->n_lists = motif_n_target_lists;
1195 header->total_size = total_size;
1197 for (i = 0; i < motif_n_target_lists ; i++)
1199 guint16 n_targets = g_list_length (motif_target_lists[i]);
1200 guint32 *targets = g_new (guint32, n_targets);
1201 guint32 *p32 = targets;
1203 tmp_list = motif_target_lists[i];
1206 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1208 tmp_list = tmp_list->next;
1213 p += sizeof(guint16);
1215 memcpy (p, targets, n_targets * sizeof(guint32));
1218 p += sizeof(guint32) * n_targets;
1222 XChangeProperty (gdk_display, motif_drag_window,
1223 motif_drag_targets_atom,
1224 motif_drag_targets_atom,
1228 XUngrabServer(gdk_display);
1231 g_list_free (sorted);
1235 /* Translate flags */
1238 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1240 guint recommended_op = flags & 0x000f;
1241 guint possible_ops = (flags & 0x0f0) >> 4;
1243 switch (recommended_op)
1246 context->suggested_action = GDK_ACTION_MOVE;
1249 context->suggested_action = GDK_ACTION_COPY;
1252 context->suggested_action = GDK_ACTION_LINK;
1255 context->suggested_action = GDK_ACTION_COPY;
1259 context->actions = 0;
1260 if (possible_ops & XmDROP_MOVE)
1261 context->actions |= GDK_ACTION_MOVE;
1262 if (possible_ops & XmDROP_COPY)
1263 context->actions |= GDK_ACTION_COPY;
1264 if (possible_ops & XmDROP_LINK)
1265 context->actions |= GDK_ACTION_LINK;
1269 motif_dnd_get_flags (GdkDragContext *context)
1273 switch (context->suggested_action)
1275 case GDK_ACTION_MOVE:
1276 flags = XmDROP_MOVE;
1278 case GDK_ACTION_COPY:
1279 flags = XmDROP_COPY;
1281 case GDK_ACTION_LINK:
1282 flags = XmDROP_LINK;
1285 flags = XmDROP_NOOP;
1289 if (context->actions & GDK_ACTION_MOVE)
1290 flags |= XmDROP_MOVE << 8;
1291 if (context->actions & GDK_ACTION_COPY)
1292 flags |= XmDROP_COPY << 8;
1293 if (context->actions & GDK_ACTION_LINK)
1294 flags |= XmDROP_LINK << 8;
1302 motif_set_targets (GdkDragContext *context)
1304 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1305 MotifDragInitiatorInfo info;
1307 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1309 if (!motif_drag_initiator_info)
1310 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1312 info.byte_order = local_byte_order;
1313 info.protocol_version = 0;
1315 info.targets_index = motif_add_to_target_table (context->targets);
1320 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1322 private->motif_selection = gdk_atom_intern (buf, FALSE);
1323 if (!XGetSelectionOwner (gdk_display, private->motif_selection))
1327 info.selection_atom = private->motif_selection;
1329 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1330 GDK_DRAWABLE_XID (context->source_window),
1331 private->motif_selection,
1332 motif_drag_initiator_info, 8, PropModeReplace,
1333 (guchar *)&info, sizeof (info));
1335 private->motif_targets_set = 1;
1339 motif_check_dest (Window win)
1341 gboolean retval = FALSE;
1342 MotifDragReceiverInfo *info;
1345 unsigned long nitems, after;
1347 if (!motif_drag_receiver_info_atom)
1348 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
1350 gdk_error_trap_push ();
1351 XGetWindowProperty (gdk_display, win,
1352 motif_drag_receiver_info_atom,
1353 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1354 &type, &format, &nitems, &after,
1357 if (gdk_error_trap_pop() == 0)
1361 if ((format == 8) && (nitems == sizeof(*info)))
1363 if ((info->protocol_version == 0) &&
1364 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1365 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1366 (info->protocol_style == XmDRAG_DYNAMIC)))
1372 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1379 return retval ? win : GDK_NONE;
1384 motif_send_enter (GdkDragContext *context,
1388 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1390 xev.xclient.type = ClientMessage;
1391 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1392 xev.xclient.format = 8;
1393 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1395 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1396 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1397 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1398 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1399 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1401 if (!private->motif_targets_set)
1402 motif_set_targets (context);
1404 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1406 if (!gdk_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
1409 g_message ("Send event to %lx failed",
1410 GDK_DRAWABLE_XID (context->dest_window)));
1414 motif_send_leave (GdkDragContext *context,
1419 xev.xclient.type = ClientMessage;
1420 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1421 xev.xclient.format = 8;
1422 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1424 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1425 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1426 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1427 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1428 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1429 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1431 if (!gdk_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
1434 g_message ("Send event to %lx failed",
1435 GDK_DRAWABLE_XID (context->dest_window)));
1439 motif_send_motion (GdkDragContext *context,
1442 GdkDragAction action,
1447 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1449 xev.xclient.type = ClientMessage;
1450 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1451 xev.xclient.format = 8;
1452 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1454 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1455 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1456 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1458 if ((context->suggested_action != private->old_action) ||
1459 (context->actions != private->old_actions))
1461 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1463 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1468 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1470 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1471 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1473 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1477 if (!gdk_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
1480 g_message ("Send event to %lx failed",
1481 GDK_DRAWABLE_XID (context->dest_window)));
1487 motif_send_drop (GdkDragContext *context, guint32 time)
1490 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1492 xev.xclient.type = ClientMessage;
1493 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1494 xev.xclient.format = 8;
1495 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1497 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1498 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1499 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1500 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1502 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1503 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1505 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1506 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1508 if (!gdk_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
1511 g_message ("Send event to %lx failed",
1512 GDK_DRAWABLE_XID (context->dest_window)));
1518 motif_read_initiator_info (Window source_window,
1524 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1530 MotifDragInitiatorInfo *initiator_info;
1532 if (!motif_drag_initiator_info)
1533 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1535 gdk_error_trap_push ();
1536 XGetWindowProperty (gdk_display, source_window, atom,
1537 0, sizeof(*initiator_info), FALSE,
1538 motif_drag_initiator_info,
1539 &type, &format, &nitems, &bytes_after,
1540 (guchar **)&initiator_info);
1542 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1544 g_warning ("Error reading initiator info\n");
1548 motif_read_target_table ();
1550 initiator_info->targets_index =
1551 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1552 initiator_info->selection_atom =
1553 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1555 if (initiator_info->targets_index >= motif_n_target_lists)
1557 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1558 XFree (initiator_info);
1559 return GDK_FILTER_REMOVE;
1562 tmp_list = g_list_last (motif_target_lists[initiator_info->targets_index]);
1567 *targets = g_list_prepend (*targets,
1569 tmp_list = tmp_list->prev;
1572 #ifdef G_ENABLE_DEBUG
1573 if (gdk_debug_flags & GDK_DEBUG_DND)
1574 print_target_list (*targets);
1575 #endif /* G_ENABLE_DEBUG */
1577 *selection = initiator_info->selection_atom;
1579 XFree (initiator_info);
1584 static GdkDragContext *
1585 motif_drag_context_new (GdkWindow *dest_window,
1587 guint32 source_window,
1590 GdkDragContext *new_context;
1591 GdkDragContextPrivateX11 *private;
1593 /* FIXME, current_dest_drag really shouldn't be NULL'd
1594 * if we error below.
1596 if (current_dest_drag != NULL)
1598 if (timestamp >= current_dest_drag->start_time)
1600 gdk_drag_context_unref (current_dest_drag);
1601 current_dest_drag = NULL;
1607 new_context = gdk_drag_context_new ();
1608 private = PRIVATE_DATA (new_context);
1610 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1611 new_context->is_source = FALSE;
1613 new_context->source_window = gdk_window_lookup (source_window);
1614 if (new_context->source_window)
1615 gdk_window_ref (new_context->source_window);
1618 new_context->source_window = gdk_window_foreign_new (source_window);
1619 if (!new_context->source_window)
1621 gdk_drag_context_unref (new_context);
1626 new_context->dest_window = dest_window;
1627 gdk_window_ref (dest_window);
1628 new_context->start_time = timestamp;
1630 if (!motif_read_initiator_info(source_window,
1632 &new_context->targets,
1633 &private->motif_selection))
1635 gdk_drag_context_unref (new_context);
1643 * The MOTIF drag protocol has no real provisions for distinguishing
1644 * multiple simultaneous drops. If the sources grab the pointer
1645 * when doing drags, that shouldn't happen, in any case. If it
1646 * does, we can't do much except hope for the best.
1649 static GdkFilterReturn
1650 motif_top_level_enter (GdkEvent *event,
1653 guint32 source_window,
1656 GdkDragContext *new_context;
1658 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1659 flags, timestamp, source_window, atom));
1661 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1663 return GDK_FILTER_REMOVE;
1665 event->dnd.type = GDK_DRAG_ENTER;
1666 event->dnd.context = new_context;
1667 gdk_drag_context_ref (new_context);
1669 current_dest_drag = new_context;
1671 return GDK_FILTER_TRANSLATE;
1675 motif_top_level_leave (GdkEvent *event,
1679 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1682 if ((current_dest_drag != NULL) &&
1683 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1684 (timestamp >= current_dest_drag->start_time))
1686 event->dnd.type = GDK_DRAG_LEAVE;
1687 /* Pass ownership of context to the event */
1688 event->dnd.context = current_dest_drag;
1690 current_dest_drag = NULL;
1692 return GDK_FILTER_TRANSLATE;
1695 return GDK_FILTER_REMOVE;
1699 motif_motion (GdkEvent *event,
1705 GdkDragContextPrivateX11 *private;
1707 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1708 flags, timestamp, x_root, y_root));
1710 if ((current_dest_drag != NULL) &&
1711 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1712 (timestamp >= current_dest_drag->start_time))
1714 private = PRIVATE_DATA (current_dest_drag);
1716 event->dnd.type = GDK_DRAG_MOTION;
1717 event->dnd.context = current_dest_drag;
1718 gdk_drag_context_ref (current_dest_drag);
1720 event->dnd.time = timestamp;
1722 motif_dnd_translate_flags (current_dest_drag, flags);
1724 event->dnd.x_root = x_root;
1725 event->dnd.y_root = y_root;
1727 private->last_x = x_root;
1728 private->last_y = y_root;
1730 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1732 return GDK_FILTER_TRANSLATE;
1735 return GDK_FILTER_REMOVE;
1739 motif_operation_changed (GdkEvent *event,
1743 GdkDragContextPrivateX11 *private;
1745 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1748 if ((current_dest_drag != NULL) &&
1749 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1750 (timestamp >= current_dest_drag->start_time))
1752 event->dnd.type = GDK_DRAG_MOTION;
1753 event->dnd.send_event = FALSE;
1754 event->dnd.context = current_dest_drag;
1755 gdk_drag_context_ref (current_dest_drag);
1757 event->dnd.time = timestamp;
1758 private = PRIVATE_DATA (current_dest_drag);
1760 motif_dnd_translate_flags (current_dest_drag, flags);
1762 event->dnd.x_root = private->last_x;
1763 event->dnd.y_root = private->last_y;
1765 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1767 return GDK_FILTER_TRANSLATE;
1770 return GDK_FILTER_REMOVE;
1774 motif_drop_start (GdkEvent *event,
1777 guint32 source_window,
1782 GdkDragContext *new_context;
1785 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1786 flags, timestamp, x_root, y_root, source_window, atom));
1788 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1790 return GDK_FILTER_REMOVE;
1792 motif_dnd_translate_flags (new_context, flags);
1794 event->dnd.type = GDK_DROP_START;
1795 event->dnd.context = new_context;
1796 event->dnd.time = timestamp;
1797 event->dnd.x_root = x_root;
1798 event->dnd.y_root = y_root;
1800 gdk_drag_context_ref (new_context);
1801 current_dest_drag = new_context;
1803 return GDK_FILTER_TRANSLATE;
1807 motif_drag_status (GdkEvent *event,
1811 GdkDragContext *context;
1814 g_message ("Motif status message: flags %x", flags));
1816 context = gdk_drag_context_find (TRUE,
1817 GDK_DRAWABLE_XID (event->any.window),
1822 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1823 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1824 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1825 private->drag_status = GDK_DRAG_STATUS_DRAG;
1827 event->dnd.type = GDK_DRAG_STATUS;
1828 event->dnd.send_event = FALSE;
1829 event->dnd.context = context;
1830 gdk_drag_context_ref (context);
1832 event->dnd.time = timestamp;
1834 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1836 switch (flags & 0x000f)
1839 context->action = 0;
1842 context->action = GDK_ACTION_MOVE;
1845 context->action = GDK_ACTION_COPY;
1848 context->action = GDK_ACTION_LINK;
1853 context->action = 0;
1855 return GDK_FILTER_TRANSLATE;
1857 return GDK_FILTER_REMOVE;
1861 motif_dnd_filter (GdkXEvent *xev,
1865 XEvent *xevent = (XEvent *)xev;
1870 guint32 source_window;
1872 gint16 x_root, y_root;
1875 /* First read some fields common to all Motif DND messages */
1877 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1878 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1879 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1881 is_reply = ((reason & 0x80) != 0);
1883 switch (reason & 0x7f)
1885 case XmTOP_LEVEL_ENTER:
1886 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1887 atom = MOTIF_UNPACK_LONG (xevent, 3);
1888 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1889 case XmTOP_LEVEL_LEAVE:
1890 return motif_top_level_leave (event, flags, timestamp);
1893 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1894 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1897 return motif_motion (event, flags, timestamp, x_root, y_root);
1899 return motif_drag_status (event, flags, timestamp);
1901 case XmDROP_SITE_ENTER:
1902 return motif_drag_status (event, flags, timestamp);
1904 case XmDROP_SITE_LEAVE:
1905 return motif_drag_status (event,
1906 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1909 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1910 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1911 atom = MOTIF_UNPACK_LONG (xevent, 3);
1912 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1915 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1918 case XmOPERATION_CHANGED:
1920 return motif_operation_changed (event, flags, timestamp);
1922 return motif_drag_status (event, flags, timestamp);
1925 /* To the best of my knowledge, these next two messages are
1926 * not part of the protocol, though they are defined in
1930 case XmDRAG_DROP_FINISH:
1934 return GDK_FILTER_REMOVE;
1937 /*************************************************************
1938 ***************************** XDND **************************
1939 *************************************************************/
1941 /* Utility functions */
1946 GdkDragAction action;
1947 } xdnd_actions_table[] = {
1948 { "XdndActionCopy", GDK_NONE, GDK_ACTION_COPY },
1949 { "XdndActionMove", GDK_NONE, GDK_ACTION_MOVE },
1950 { "XdndActionLink", GDK_NONE, GDK_ACTION_LINK },
1951 { "XdndActionAsk", GDK_NONE, GDK_ACTION_ASK },
1952 { "XdndActionPrivate", GDK_NONE, GDK_ACTION_COPY },
1955 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1956 static gboolean xdnd_actions_initialized = FALSE;
1959 xdnd_initialize_actions (void)
1963 xdnd_actions_initialized = TRUE;
1964 for (i=0; i < xdnd_n_actions; i++)
1965 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1968 static GdkDragAction
1969 xdnd_action_from_atom (GdkAtom atom)
1973 if (!xdnd_actions_initialized)
1974 xdnd_initialize_actions();
1976 for (i=0; i<xdnd_n_actions; i++)
1977 if (atom == xdnd_actions_table[i].atom)
1978 return xdnd_actions_table[i].action;
1984 xdnd_action_to_atom (GdkDragAction action)
1988 if (!xdnd_actions_initialized)
1989 xdnd_initialize_actions();
1991 for (i=0; i<xdnd_n_actions; i++)
1992 if (action == xdnd_actions_table[i].action)
1993 return xdnd_actions_table[i].atom;
1998 static GdkAtom xdnd_aware_atom = GDK_NONE;
2002 static GdkFilterReturn
2003 xdnd_status_filter (GdkXEvent *xev,
2007 XEvent *xevent = (XEvent *)xev;
2008 guint32 dest_window = xevent->xclient.data.l[0];
2009 guint32 flags = xevent->xclient.data.l[1];
2010 GdkAtom action = xevent->xclient.data.l[4];
2011 GdkDragContext *context;
2014 g_message ("XdndStatus: dest_window: %#x action: %ld",
2015 dest_window, action));
2018 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
2021 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2022 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2023 private->drag_status = GDK_DRAG_STATUS_DRAG;
2025 event->dnd.send_event = FALSE;
2026 event->dnd.type = GDK_DRAG_STATUS;
2027 event->dnd.context = context;
2028 gdk_drag_context_ref (context);
2030 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2031 if (!(action != 0) != !(flags & 1))
2034 g_warning ("Received status event with flags not corresponding to action!\n"));
2038 context->action = xdnd_action_from_atom (action);
2040 return GDK_FILTER_TRANSLATE;
2043 return GDK_FILTER_REMOVE;
2046 static GdkFilterReturn
2047 xdnd_finished_filter (GdkXEvent *xev,
2051 XEvent *xevent = (XEvent *)xev;
2052 guint32 dest_window = xevent->xclient.data.l[0];
2053 GdkDragContext *context;
2056 g_message ("XdndFinished: dest_window: %#x", dest_window));
2058 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
2061 event->dnd.type = GDK_DROP_FINISHED;
2062 event->dnd.context = context;
2063 gdk_drag_context_ref (context);
2065 return GDK_FILTER_TRANSLATE;
2068 return GDK_FILTER_REMOVE;
2072 xdnd_set_targets (GdkDragContext *context)
2074 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2076 GList *tmp_list = context->targets;
2078 gint n_atoms = g_list_length (context->targets);
2080 atomlist = g_new (GdkAtom, n_atoms);
2084 atomlist[i] = GPOINTER_TO_INT (tmp_list->data);
2085 tmp_list = tmp_list->next;
2089 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2090 GDK_DRAWABLE_XID (context->source_window),
2091 gdk_atom_intern ("XdndTypeList", FALSE),
2092 XA_ATOM, 32, PropModeReplace,
2093 (guchar *)atomlist, n_atoms);
2097 private->xdnd_targets_set = 1;
2101 xdnd_set_actions (GdkDragContext *context)
2103 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2109 if (!xdnd_actions_initialized)
2110 xdnd_initialize_actions();
2112 actions = context->actions;
2114 for (i=0; i<xdnd_n_actions; i++)
2116 if (actions & xdnd_actions_table[i].action)
2118 actions &= ~xdnd_actions_table[i].action;
2123 atomlist = g_new (GdkAtom, n_atoms);
2125 actions = context->actions;
2127 for (i=0; i<xdnd_n_actions; i++)
2129 if (actions & xdnd_actions_table[i].action)
2131 actions &= ~xdnd_actions_table[i].action;
2132 atomlist[n_atoms] = xdnd_actions_table[i].atom;
2137 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2138 GDK_DRAWABLE_XID (context->source_window),
2139 gdk_atom_intern ("XdndActionList", FALSE),
2140 XA_ATOM, 32, PropModeReplace,
2141 (guchar *)atomlist, n_atoms);
2145 private->xdnd_actions_set = 1;
2146 private->xdnd_actions = context->actions;
2149 /*************************************************************
2151 * Like gdk_send_event, but if the target is the root
2152 * window, sets an event mask of ButtonPressMask, otherwise
2153 * an event mask of 0.
2157 *************************************************************/
2160 xdnd_send_xevent (Window window, gboolean propagate,
2163 if (window == gdk_root_window)
2164 return gdk_send_xevent (window, propagate, ButtonPressMask, event_send);
2166 return gdk_send_xevent (window, propagate, 0, event_send);
2170 xdnd_send_enter (GdkDragContext *context)
2173 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2175 xev.xclient.type = ClientMessage;
2176 xev.xclient.message_type = gdk_atom_intern ("XdndEnter", FALSE);
2177 xev.xclient.format = 32;
2178 xev.xclient.window = private->drop_xid ?
2180 GDK_DRAWABLE_XID (context->dest_window);
2181 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2182 xev.xclient.data.l[1] = (3 << 24); /* version */
2183 xev.xclient.data.l[2] = 0;
2184 xev.xclient.data.l[3] = 0;
2185 xev.xclient.data.l[4] = 0;
2187 if (!private->xdnd_selection)
2188 private->xdnd_selection = gdk_atom_intern ("XdndSelection", FALSE);
2190 if (g_list_length (context->targets) > 3)
2192 if (!private->xdnd_targets_set)
2193 xdnd_set_targets (context);
2194 xev.xclient.data.l[1] |= 1;
2198 GList *tmp_list = context->targets;
2203 xev.xclient.data.l[i] = GPOINTER_TO_INT (tmp_list->data);
2204 tmp_list = tmp_list->next;
2209 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
2213 g_message ("Send event to %lx failed",
2214 GDK_DRAWABLE_XID (context->dest_window)));
2215 gdk_window_unref (context->dest_window);
2216 context->dest_window = NULL;
2221 xdnd_send_leave (GdkDragContext *context)
2225 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2227 xev.xclient.type = ClientMessage;
2228 xev.xclient.message_type = gdk_atom_intern ("XdndLeave", FALSE);
2229 xev.xclient.format = 32;
2230 xev.xclient.window = private->drop_xid ?
2232 GDK_DRAWABLE_XID (context->dest_window);
2233 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2234 xev.xclient.data.l[1] = 0;
2235 xev.xclient.data.l[2] = 0;
2236 xev.xclient.data.l[3] = 0;
2237 xev.xclient.data.l[4] = 0;
2239 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
2243 g_message ("Send event to %lx failed",
2244 GDK_DRAWABLE_XID (context->dest_window)));
2245 gdk_window_unref (context->dest_window);
2246 context->dest_window = NULL;
2251 xdnd_send_drop (GdkDragContext *context, guint32 time)
2253 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2256 xev.xclient.type = ClientMessage;
2257 xev.xclient.message_type = gdk_atom_intern ("XdndDrop", FALSE);
2258 xev.xclient.format = 32;
2259 xev.xclient.window = private->drop_xid ?
2261 GDK_DRAWABLE_XID (context->dest_window);
2262 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2263 xev.xclient.data.l[1] = 0;
2264 xev.xclient.data.l[2] = time;
2265 xev.xclient.data.l[3] = 0;
2266 xev.xclient.data.l[4] = 0;
2268 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
2272 g_message ("Send event to %lx failed",
2273 GDK_DRAWABLE_XID (context->dest_window)));
2274 gdk_window_unref (context->dest_window);
2275 context->dest_window = NULL;
2280 xdnd_send_motion (GdkDragContext *context,
2283 GdkDragAction action,
2286 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2289 xev.xclient.type = ClientMessage;
2290 xev.xclient.message_type = gdk_atom_intern ("XdndPosition", FALSE);
2291 xev.xclient.format = 32;
2292 xev.xclient.window = private->drop_xid ?
2294 GDK_DRAWABLE_XID (context->dest_window);
2295 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2296 xev.xclient.data.l[1] = 0;
2297 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2298 xev.xclient.data.l[3] = time;
2299 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
2301 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
2305 g_message ("Send event to %lx failed",
2306 GDK_DRAWABLE_XID (context->dest_window)));
2307 gdk_window_unref (context->dest_window);
2308 context->dest_window = NULL;
2310 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2314 xdnd_check_dest (Window win)
2316 gboolean retval = FALSE;
2319 unsigned long nitems, after;
2323 static GdkAtom xdnd_proxy_atom = GDK_NONE;
2325 gint old_warnings = gdk_error_warnings;
2327 if (!xdnd_proxy_atom)
2328 xdnd_proxy_atom = gdk_atom_intern ("XdndProxy", FALSE);
2330 if (!xdnd_aware_atom)
2331 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
2336 gdk_error_warnings = 0;
2338 XGetWindowProperty (gdk_display, win,
2340 1, False, AnyPropertyType,
2341 &type, &format, &nitems, &after,
2342 (guchar **)&proxy_data);
2344 if (!gdk_error_code)
2348 if ((format == 32) && (nitems == 1))
2350 proxy = *proxy_data;
2354 g_warning ("Invalid XdndOwner property on window %ld\n", win));
2359 XGetWindowProperty (gdk_display, proxy ? proxy : win,
2361 1, False, AnyPropertyType,
2362 &type, &format, &nitems, &after,
2363 (guchar **)&version);
2365 if (!gdk_error_code && type != None)
2367 if ((format == 32) && (nitems == 1))
2374 g_warning ("Invalid XdndAware property on window %ld\n", win));
2381 gdk_error_warnings = old_warnings;
2384 return retval ? (proxy ? proxy : win) : GDK_NONE;
2390 xdnd_read_actions (GdkDragContext *context)
2394 gulong nitems, after;
2399 gint old_warnings = gdk_error_warnings;
2402 gdk_error_warnings = 0;
2404 /* Get the XdndActionList, if set */
2406 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2407 GDK_DRAWABLE_XID (context->source_window),
2408 gdk_atom_intern ("XdndActionList", FALSE), 0, 65536,
2409 False, XA_ATOM, &type, &format, &nitems,
2410 &after, (guchar **)&data);
2412 if (!gdk_error_code && (format == 32) && (type == XA_ATOM))
2414 context->actions = 0;
2416 for (i=0; i<nitems; i++)
2417 context->actions |= xdnd_action_from_atom (data[i]);
2419 (PRIVATE_DATA (context))->xdnd_have_actions = TRUE;
2421 #ifdef G_ENABLE_DEBUG
2422 if (gdk_debug_flags & GDK_DEBUG_DND)
2424 GString *action_str = g_string_new (NULL);
2425 if (context->actions & GDK_ACTION_MOVE)
2426 g_string_append(action_str, "MOVE ");
2427 if (context->actions & GDK_ACTION_COPY)
2428 g_string_append(action_str, "COPY ");
2429 if (context->actions & GDK_ACTION_LINK)
2430 g_string_append(action_str, "LINK ");
2431 if (context->actions & GDK_ACTION_ASK)
2432 g_string_append(action_str, "ASK ");
2434 g_message("Xdnd actions = %s", action_str->str);
2435 g_string_free (action_str, TRUE);
2437 #endif /* G_ENABLE_DEBUG */
2442 gdk_error_warnings = old_warnings;
2446 /* We have to make sure that the XdndActionList we keep internally
2447 * is up to date with the XdndActionList on the source window
2448 * because we get no notification, because Xdnd wasn't meant
2449 * to continually send actions. So we select on PropertyChangeMask
2450 * and add this filter.
2452 static GdkFilterReturn
2453 xdnd_source_window_filter (GdkXEvent *xev,
2457 XEvent *xevent = (XEvent *)xev;
2458 GdkDragContext *context = cb_data;
2460 if ((xevent->xany.type == PropertyNotify) &&
2461 (xevent->xproperty.atom == gdk_atom_intern ("XdndActionList", FALSE)))
2463 xdnd_read_actions (context);
2465 return GDK_FILTER_REMOVE;
2468 return GDK_FILTER_CONTINUE;
2472 xdnd_manage_source_filter (GdkDragContext *context,
2474 gboolean add_filter)
2476 gint old_warnings = 0; /* quiet gcc */
2478 gdk_error_trap_push ();
2480 if (!GDK_WINDOW_DESTROYED (window))
2484 gdk_window_set_events (window,
2485 gdk_window_get_events (window) |
2486 GDK_PROPERTY_CHANGE_MASK);
2487 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2492 gdk_window_remove_filter (window,
2493 xdnd_source_window_filter,
2495 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2496 * but we might want it for other reasons. (Like
2497 * INCR selection transactions).
2503 gdk_error_trap_pop ();
2506 static GdkFilterReturn
2507 xdnd_enter_filter (GdkXEvent *xev,
2511 XEvent *xevent = (XEvent *)xev;
2512 GdkDragContext *new_context;
2517 gulong nitems, after;
2520 guint32 source_window = xevent->xclient.data.l[0];
2521 gboolean get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2522 gint version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2525 g_message ("XdndEnter: source_window: %#x, version: %#x",
2526 source_window, version));
2530 /* Old source ignore */
2531 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2532 return GDK_FILTER_REMOVE;
2535 if (current_dest_drag != NULL)
2537 gdk_drag_context_unref (current_dest_drag);
2538 current_dest_drag = NULL;
2541 new_context = gdk_drag_context_new ();
2542 new_context->protocol = GDK_DRAG_PROTO_XDND;
2543 new_context->is_source = FALSE;
2545 new_context->source_window = gdk_window_lookup (source_window);
2546 if (new_context->source_window)
2547 gdk_window_ref (new_context->source_window);
2550 new_context->source_window = gdk_window_foreign_new (source_window);
2551 if (!new_context->source_window)
2553 gdk_drag_context_unref (new_context);
2554 return GDK_FILTER_REMOVE;
2557 new_context->dest_window = event->any.window;
2558 gdk_window_ref (new_context->dest_window);
2560 new_context->targets = NULL;
2563 gdk_error_trap_push ();
2564 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2566 gdk_atom_intern ("XdndTypeList", FALSE), 0, 65536,
2567 False, XA_ATOM, &type, &format, &nitems,
2568 &after, (guchar **)&data);
2570 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2572 gdk_drag_context_unref (new_context);
2573 return GDK_FILTER_REMOVE;
2576 for (i=0; i<nitems; i++)
2577 new_context->targets = g_list_append (new_context->targets,
2578 GUINT_TO_POINTER (data[i]));
2585 if (xevent->xclient.data.l[2+i])
2586 new_context->targets = g_list_append (new_context->targets,
2587 GUINT_TO_POINTER (xevent->xclient.data.l[2+i]));
2590 #ifdef G_ENABLE_DEBUG
2591 if (gdk_debug_flags & GDK_DEBUG_DND)
2592 print_target_list (new_context->targets);
2593 #endif /* G_ENABLE_DEBUG */
2595 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2596 xdnd_read_actions (new_context);
2598 event->dnd.type = GDK_DRAG_ENTER;
2599 event->dnd.context = new_context;
2600 gdk_drag_context_ref (new_context);
2602 current_dest_drag = new_context;
2603 (PRIVATE_DATA (new_context))->xdnd_selection =
2604 gdk_atom_intern ("XdndSelection", FALSE);
2606 return GDK_FILTER_TRANSLATE;
2609 static GdkFilterReturn
2610 xdnd_leave_filter (GdkXEvent *xev,
2614 XEvent *xevent = (XEvent *)xev;
2615 guint32 source_window = xevent->xclient.data.l[0];
2618 g_message ("XdndLeave: source_window: %#x",
2621 if ((current_dest_drag != NULL) &&
2622 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2623 (GDK_DRAWABLE_XID (current_dest_drag->source_window) == source_window))
2625 event->dnd.type = GDK_DRAG_LEAVE;
2626 /* Pass ownership of context to the event */
2627 event->dnd.context = current_dest_drag;
2629 current_dest_drag = NULL;
2631 return GDK_FILTER_TRANSLATE;
2634 return GDK_FILTER_REMOVE;
2637 static GdkFilterReturn
2638 xdnd_position_filter (GdkXEvent *xev,
2642 XEvent *xevent = (XEvent *)xev;
2643 guint32 source_window = xevent->xclient.data.l[0];
2644 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2645 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2646 guint32 time = xevent->xclient.data.l[3];
2647 GdkAtom action = xevent->xclient.data.l[4];
2650 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2651 source_window, x_root, y_root, time, action));
2654 if ((current_dest_drag != NULL) &&
2655 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2656 (GDK_DRAWABLE_XID (current_dest_drag->source_window) == source_window))
2658 event->dnd.type = GDK_DRAG_MOTION;
2659 event->dnd.context = current_dest_drag;
2660 gdk_drag_context_ref (current_dest_drag);
2662 event->dnd.time = time;
2664 current_dest_drag->suggested_action = xdnd_action_from_atom (action);
2665 if (!(PRIVATE_DATA (current_dest_drag))->xdnd_have_actions)
2666 current_dest_drag->actions = current_dest_drag->suggested_action;
2668 event->dnd.x_root = x_root;
2669 event->dnd.y_root = y_root;
2671 (PRIVATE_DATA (current_dest_drag))->last_x = x_root;
2672 (PRIVATE_DATA (current_dest_drag))->last_y = y_root;
2674 return GDK_FILTER_TRANSLATE;
2677 return GDK_FILTER_REMOVE;
2680 static GdkFilterReturn
2681 xdnd_drop_filter (GdkXEvent *xev,
2685 XEvent *xevent = (XEvent *)xev;
2686 guint32 source_window = xevent->xclient.data.l[0];
2687 guint32 time = xevent->xclient.data.l[2];
2690 g_message ("XdndDrop: source_window: %#x time: %d",
2691 source_window, time));
2693 if ((current_dest_drag != NULL) &&
2694 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2695 (GDK_DRAWABLE_XID (current_dest_drag->source_window) == source_window))
2697 GdkDragContextPrivateX11 *private;
2698 private = PRIVATE_DATA (current_dest_drag);
2700 event->dnd.type = GDK_DROP_START;
2702 event->dnd.context = current_dest_drag;
2703 gdk_drag_context_ref (current_dest_drag);
2705 event->dnd.time = time;
2706 event->dnd.x_root = private->last_x;
2707 event->dnd.y_root = private->last_y;
2709 return GDK_FILTER_TRANSLATE;
2712 return GDK_FILTER_REMOVE;
2715 /*************************************************************
2716 ************************** Public API ***********************
2717 *************************************************************/
2724 gdk_add_client_message_filter (
2725 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2726 motif_dnd_filter, NULL);
2727 gdk_add_client_message_filter (
2728 gdk_atom_intern ("XdndEnter", FALSE),
2729 xdnd_enter_filter, NULL);
2730 gdk_add_client_message_filter (
2731 gdk_atom_intern ("XdndLeave", FALSE),
2732 xdnd_leave_filter, NULL);
2733 gdk_add_client_message_filter (
2734 gdk_atom_intern ("XdndPosition", FALSE),
2735 xdnd_position_filter, NULL);
2736 gdk_add_client_message_filter (
2737 gdk_atom_intern ("XdndStatus", FALSE),
2738 xdnd_status_filter, NULL);
2739 gdk_add_client_message_filter (
2740 gdk_atom_intern ("XdndFinished", FALSE),
2741 xdnd_finished_filter, NULL);
2742 gdk_add_client_message_filter (
2743 gdk_atom_intern ("XdndDrop", FALSE),
2744 xdnd_drop_filter, NULL);
2750 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2752 if (context->dest_window)
2754 switch (context->protocol)
2756 case GDK_DRAG_PROTO_MOTIF:
2757 motif_send_leave (context, time);
2759 case GDK_DRAG_PROTO_XDND:
2760 xdnd_send_leave (context);
2762 case GDK_DRAG_PROTO_ROOTWIN:
2763 case GDK_DRAG_PROTO_NONE:
2768 gdk_window_unref (context->dest_window);
2769 context->dest_window = NULL;
2774 gdk_drag_begin (GdkWindow *window,
2778 GdkDragContext *new_context;
2780 g_return_val_if_fail (window != NULL, NULL);
2782 new_context = gdk_drag_context_new ();
2783 new_context->is_source = TRUE;
2784 new_context->source_window = window;
2785 gdk_window_ref (window);
2787 tmp_list = g_list_last (targets);
2788 new_context->targets = NULL;
2791 new_context->targets = g_list_prepend (new_context->targets,
2793 tmp_list = tmp_list->prev;
2796 new_context->actions = 0;
2802 gdk_drag_get_protocol (guint32 xid,
2803 GdkDragProtocol *protocol)
2807 if ((retval = xdnd_check_dest (xid)))
2809 *protocol = GDK_DRAG_PROTO_XDND;
2810 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2813 else if ((retval = motif_check_dest (xid)))
2815 *protocol = GDK_DRAG_PROTO_MOTIF;
2816 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2821 /* Check if this is a root window */
2823 gboolean rootwin = FALSE;
2824 gint old_warnings = gdk_error_warnings;
2827 unsigned long nitems, after;
2828 unsigned char *data;
2830 if (xid == gdk_root_window)
2833 gdk_error_warnings = 0;
2839 XGetWindowProperty (gdk_display, xid,
2840 gdk_atom_intern ("ENLIGHTENMENT_DESKTOP", FALSE),
2841 0, 0, False, AnyPropertyType,
2842 &type, &format, &nitems, &after, &data);
2843 if ((gdk_error_code == 0) && type != None)
2850 /* Until I find out what window manager the next one is for,
2851 * I'm leaving it commented out. It's supported in the
2852 * xscreensaver sources, though.
2859 XGetWindowProperty (gdk_display, win,
2860 gdk_atom_intern ("__SWM_VROOT", FALSE),
2861 0, 0, False, AnyPropertyType,
2862 &type, &format, &nitems, &data);
2863 if ((gdk_error_code == 0) && type != None)
2868 gdk_error_warnings = old_warnings;
2872 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2877 *protocol = GDK_DRAG_PROTO_NONE;
2882 gdk_drag_find_window (GdkDragContext *context,
2883 GdkWindow *drag_window,
2886 GdkWindow **dest_window,
2887 GdkDragProtocol *protocol)
2889 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2892 g_return_if_fail (context != NULL);
2894 if (!private->window_cache)
2895 private->window_cache = gdk_window_cache_new();
2897 dest = get_client_window_at_coords (private->window_cache,
2899 GDK_DRAWABLE_XID (drag_window) : GDK_NONE,
2902 if (private->dest_xid != dest)
2905 private->dest_xid = dest;
2907 /* Check if new destination accepts drags, and which protocol */
2909 /* There is some ugliness here. We actually need to pass
2910 * _three_ pieces of information to drag_motion - dest_window,
2911 * protocol, and the XID of the unproxied window. The first
2912 * two are passed explicitely, the third implicitly through
2913 * protocol->dest_xid.
2915 if ((recipient = gdk_drag_get_protocol (dest, protocol)))
2917 *dest_window = gdk_window_lookup (recipient);
2919 gdk_window_ref (*dest_window);
2921 *dest_window = gdk_window_foreign_new (recipient);
2924 *dest_window = NULL;
2928 *dest_window = context->dest_window;
2930 gdk_window_ref (*dest_window);
2931 *protocol = context->protocol;
2936 gdk_drag_motion (GdkDragContext *context,
2937 GdkWindow *dest_window,
2938 GdkDragProtocol protocol,
2941 GdkDragAction suggested_action,
2942 GdkDragAction possible_actions,
2945 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2947 g_return_val_if_fail (context != NULL, FALSE);
2949 /* When we have a Xdnd target, make sure our XdndActionList
2950 * matches the current actions;
2952 private->old_actions = context->actions;
2953 context->actions = possible_actions;
2955 if ((protocol == GDK_DRAG_PROTO_XDND) &&
2956 (!private->xdnd_actions_set ||
2957 private->xdnd_actions != possible_actions))
2958 xdnd_set_actions (context);
2960 if (context->dest_window != dest_window)
2962 GdkEvent temp_event;
2964 /* Send a leave to the last destination */
2965 gdk_drag_do_leave (context, time);
2966 private->drag_status = GDK_DRAG_STATUS_DRAG;
2968 /* Check if new destination accepts drags, and which protocol */
2972 context->dest_window = dest_window;
2973 private->drop_xid = private->dest_xid;
2974 gdk_window_ref (context->dest_window);
2975 context->protocol = protocol;
2979 case GDK_DRAG_PROTO_MOTIF:
2980 motif_send_enter (context, time);
2983 case GDK_DRAG_PROTO_XDND:
2984 xdnd_send_enter (context);
2987 case GDK_DRAG_PROTO_ROOTWIN:
2988 case GDK_DRAG_PROTO_NONE:
2992 private->old_action = suggested_action;
2993 context->suggested_action = suggested_action;
2994 private->old_actions = possible_actions;
2998 context->dest_window = NULL;
2999 private->drop_xid = None;
3000 context->action = 0;
3003 /* Push a status event, to let the client know that
3007 temp_event.dnd.type = GDK_DRAG_STATUS;
3008 temp_event.dnd.window = context->source_window;
3009 /* We use this to signal a synthetic status. Perhaps
3010 * we should use an extra field...
3012 temp_event.dnd.send_event = TRUE;
3014 temp_event.dnd.context = context;
3015 temp_event.dnd.time = time;
3017 gdk_event_put (&temp_event);
3021 private->old_action = context->suggested_action;
3022 context->suggested_action = suggested_action;
3025 /* Send a drag-motion event */
3027 private->last_x = x_root;
3028 private->last_y = y_root;
3030 if (context->dest_window)
3032 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3034 switch (context->protocol)
3036 case GDK_DRAG_PROTO_MOTIF:
3037 motif_send_motion (context, x_root, y_root, suggested_action, time);
3040 case GDK_DRAG_PROTO_XDND:
3041 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3044 case GDK_DRAG_PROTO_ROOTWIN:
3046 GdkEvent temp_event;
3048 if (g_list_find (context->targets,
3049 GUINT_TO_POINTER (gdk_atom_intern ("application/x-rootwin-drop", FALSE))))
3050 context->action = context->suggested_action;
3052 context->action = 0;
3054 temp_event.dnd.type = GDK_DRAG_STATUS;
3055 temp_event.dnd.window = context->source_window;
3056 temp_event.dnd.send_event = FALSE;
3057 temp_event.dnd.context = context;
3058 temp_event.dnd.time = time;
3060 gdk_event_put (&temp_event);
3063 case GDK_DRAG_PROTO_NONE:
3064 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3078 gdk_drag_drop (GdkDragContext *context,
3081 g_return_if_fail (context != NULL);
3083 if (context->dest_window)
3085 switch (context->protocol)
3087 case GDK_DRAG_PROTO_MOTIF:
3088 motif_send_leave (context, time);
3089 motif_send_drop (context, time);
3092 case GDK_DRAG_PROTO_XDND:
3093 xdnd_send_drop (context, time);
3096 case GDK_DRAG_PROTO_ROOTWIN:
3097 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3099 case GDK_DRAG_PROTO_NONE:
3100 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3109 gdk_drag_abort (GdkDragContext *context,
3112 g_return_if_fail (context != NULL);
3114 gdk_drag_do_leave (context, time);
3117 /* Destination side */
3120 gdk_drag_status (GdkDragContext *context,
3121 GdkDragAction action,
3124 GdkDragContextPrivateX11 *private;
3127 g_return_if_fail (context != NULL);
3129 private = PRIVATE_DATA (context);
3131 context->action = action;
3133 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3135 xev.xclient.type = ClientMessage;
3136 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
3137 xev.xclient.format = 8;
3138 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3140 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3142 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3146 if ((action != 0) != (private->old_action != 0))
3149 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3151 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3154 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3157 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3161 case GDK_ACTION_MOVE:
3162 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3164 case GDK_ACTION_COPY:
3165 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3167 case GDK_ACTION_LINK:
3168 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3171 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3176 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3178 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3180 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3181 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3182 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3184 if (!gdk_send_xevent (GDK_DRAWABLE_XID (context->source_window),
3187 g_message ("Send event to %lx failed",
3188 GDK_DRAWABLE_XID (context->source_window)));
3190 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3192 xev.xclient.type = ClientMessage;
3193 xev.xclient.message_type = gdk_atom_intern ("XdndStatus", FALSE);
3194 xev.xclient.format = 32;
3195 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3197 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3198 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3199 xev.xclient.data.l[2] = 0;
3200 xev.xclient.data.l[3] = 0;
3201 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
3203 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->source_window),
3206 g_message ("Send event to %lx failed",
3207 GDK_DRAWABLE_XID (context->source_window)));
3210 private->old_action = action;
3214 gdk_drop_reply (GdkDragContext *context,
3218 GdkDragContextPrivateX11 *private;
3220 g_return_if_fail (context != NULL);
3222 private = PRIVATE_DATA (context);
3224 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3228 xev.xclient.type = ClientMessage;
3229 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
3230 xev.xclient.format = 8;
3232 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3233 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3235 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3236 (XmDROP_SITE_VALID << 4) |
3237 (XmDROP_NOOP << 8) |
3240 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3241 (XmNO_DROP_SITE << 4) |
3242 (XmDROP_NOOP << 8) |
3243 (XmDROP_CANCEL << 12);
3244 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3245 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3247 gdk_send_xevent (GDK_DRAWABLE_XID (context->source_window),
3253 gdk_drop_finish (GdkDragContext *context,
3257 g_return_if_fail (context != NULL);
3259 if (context->protocol == GDK_DRAG_PROTO_XDND)
3263 xev.xclient.type = ClientMessage;
3264 xev.xclient.message_type = gdk_atom_intern ("XdndFinished", FALSE);
3265 xev.xclient.format = 32;
3266 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3268 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3269 xev.xclient.data.l[1] = 0;
3270 xev.xclient.data.l[2] = 0;
3271 xev.xclient.data.l[3] = 0;
3272 xev.xclient.data.l[4] = 0;
3274 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->source_window),
3277 g_message ("Send event to %lx failed",
3278 GDK_DRAWABLE_XID (context->source_window)));
3284 gdk_window_register_dnd (GdkWindow *window)
3286 static gulong xdnd_version = 3;
3287 MotifDragReceiverInfo info;
3289 g_return_if_fail (window != NULL);
3291 if (GPOINTER_TO_INT (gdk_drawable_get_data (window, "gdk-dnd-registered")))
3294 gdk_drawable_set_data (window, "gdk-dnd-registered", GINT_TO_POINTER(TRUE), NULL);
3296 /* Set Motif drag receiver information property */
3298 if (!motif_drag_receiver_info_atom)
3299 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
3301 info.byte_order = local_byte_order;
3302 info.protocol_version = 0;
3303 info.protocol_style = XmDRAG_DYNAMIC;
3304 info.proxy_window = GDK_NONE;
3305 info.num_drop_sites = 0;
3306 info.total_size = sizeof(info);
3308 XChangeProperty (gdk_display, GDK_DRAWABLE_XID (window),
3309 motif_drag_receiver_info_atom,
3310 motif_drag_receiver_info_atom,
3317 if (!xdnd_aware_atom)
3318 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
3320 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3321 XChangeProperty (GDK_DRAWABLE_XDISPLAY (window),
3322 GDK_DRAWABLE_XID (window),
3323 xdnd_aware_atom, XA_ATOM,
3324 32, PropModeReplace,
3325 (guchar *)&xdnd_version, 1);
3328 /*************************************************************
3329 * gdk_drag_get_selection:
3330 * Returns the selection atom for the current source window
3334 *************************************************************/
3337 gdk_drag_get_selection (GdkDragContext *context)
3339 g_return_val_if_fail (context != NULL, GDK_NONE);
3341 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3342 return (PRIVATE_DATA (context))->motif_selection;
3343 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3344 return (PRIVATE_DATA (context))->xdnd_selection;