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/.
29 #include <X11/Xatom.h>
32 #include "gdk.h" /* For gdk_flush() */
36 #include "gdkproperty.h"
37 #include "gdkprivate-x11.h"
38 #include "gdkinternals.h"
39 #include "gdkscreen-x11.h"
40 #include "gdkdisplay-x11.h"
43 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
47 GDK_DRAG_STATUS_MOTION_WAIT,
48 GDK_DRAG_STATUS_ACTION_WAIT,
54 gint x, y, width, height;
60 GHashTable *child_hash;
65 /* Structure that holds information about a drag in progress.
66 * this is used on both source and destination sides.
68 struct _GdkDragContextPrivateX11 {
69 GdkDragContext context;
74 guint16 last_x; /* Coordinates from last event */
76 GdkDragAction old_action; /* The last action we sent to the source */
77 GdkDragAction old_actions; /* The last actions we sent to the source */
78 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
80 Window dest_xid; /* The last window we looked up */
81 Window drop_xid; /* The (non-proxied) window that is receiving drops */
82 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
83 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
84 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
85 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
86 guint drag_status : 4; /* current status of drag */
88 guint drop_failed : 1; /* Whether the drop was unsuccessful */
89 guint version; /* Xdnd protocol version */
91 GSList *window_caches;
94 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
96 /* Forward declarations */
98 static void gdk_window_cache_destroy (GdkWindowCache *cache);
100 static void motif_read_target_table (GdkDisplay *display);
102 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
106 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
109 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
112 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
115 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
118 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
121 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
125 static void xdnd_manage_source_filter (GdkDragContext *context,
127 gboolean add_filter);
129 static void gdk_drag_context_finalize (GObject *object);
131 static GList *contexts;
133 const static struct {
134 const char *atom_name;
137 { "XdndEnter", xdnd_enter_filter },
138 { "XdndLeave", xdnd_leave_filter },
139 { "XdndPosition", xdnd_position_filter },
140 { "XdndStatus", xdnd_status_filter },
141 { "XdndFinished", xdnd_finished_filter },
142 { "XdndDrop", xdnd_drop_filter },
145 G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT);
148 gdk_drag_context_init (GdkDragContext *dragcontext)
150 GdkDragContextPrivateX11 *private;
152 private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext,
153 GDK_TYPE_DRAG_CONTEXT,
154 GdkDragContextPrivateX11);
156 dragcontext->windowing_data = private;
158 contexts = g_list_prepend (contexts, dragcontext);
162 gdk_drag_context_class_init (GdkDragContextClass *klass)
164 GObjectClass *object_class = G_OBJECT_CLASS (klass);
166 object_class->finalize = gdk_drag_context_finalize;
168 g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateX11));
172 gdk_drag_context_finalize (GObject *object)
174 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
175 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
178 g_list_free (context->targets);
180 if (context->source_window)
182 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
184 xdnd_manage_source_filter (context, context->source_window, FALSE);
186 g_object_unref (context->source_window);
189 if (context->dest_window)
190 g_object_unref (context->dest_window);
192 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
193 gdk_window_cache_destroy (tmp_list->data);
194 g_slist_free (private->window_caches);
196 contexts = g_list_remove (contexts, context);
198 G_OBJECT_CLASS (gdk_drag_context_parent_class)->finalize (object);
204 * gdk_drag_context_new:
206 * Creates a new #GdkDragContext.
208 * Return value: the newly created #GdkDragContext.
211 gdk_drag_context_new (void)
213 return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
217 * gdk_drag_context_ref:
218 * @context: a #GdkDragContext.
220 * Deprecated function; use g_object_ref() instead.
223 gdk_drag_context_ref (GdkDragContext *context)
225 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
227 g_object_ref (context);
231 * gdk_drag_context_unref:
232 * @context: a #GdkDragContext.
234 * Deprecated function; use g_object_unref() instead.
237 gdk_drag_context_unref (GdkDragContext *context)
239 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
241 g_object_unref (context);
244 static GdkDragContext *
245 gdk_drag_context_find (GdkDisplay *display,
250 GList *tmp_list = contexts;
251 GdkDragContext *context;
252 GdkDragContextPrivateX11 *private;
253 Window context_dest_xid;
257 context = (GdkDragContext *)tmp_list->data;
258 private = PRIVATE_DATA (context);
260 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
261 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
264 context_dest_xid = context->dest_window ?
267 GDK_DRAWABLE_XID (context->dest_window)) :
270 if ((!context->is_source == !is_source) &&
271 ((source_xid == None) || (context->source_window &&
272 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
273 ((dest_xid == None) || (context_dest_xid == dest_xid)))
276 tmp_list = tmp_list->next;
283 precache_target_list (GdkDragContext *context)
285 if (context->targets)
287 GPtrArray *targets = g_ptr_array_new ();
291 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
292 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
294 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
295 (const gchar **)targets->pdata,
298 for (i =0; i < targets->len; i++)
299 g_free (targets->pdata[i]);
301 g_ptr_array_free (targets, TRUE);
305 /* Utility functions */
308 gdk_window_cache_add (GdkWindowCache *cache,
310 gint x, gint y, gint width, gint height,
313 GdkCacheChild *child = g_new (GdkCacheChild, 1);
318 child->width = width;
319 child->height = height;
320 child->mapped = mapped;
322 cache->children = g_list_prepend (cache->children, child);
323 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
327 static GdkFilterReturn
328 gdk_window_cache_filter (GdkXEvent *xev,
332 XEvent *xevent = (XEvent *)xev;
333 GdkWindowCache *cache = data;
335 switch (xevent->type)
337 case CirculateNotify:
339 case ConfigureNotify:
341 XConfigureEvent *xce = &xevent->xconfigure;
344 node = g_hash_table_lookup (cache->child_hash,
345 GUINT_TO_POINTER (xce->window));
348 GdkCacheChild *child = node->data;
351 child->width = xce->width;
352 child->height = xce->height;
353 if (xce->above == None && (node->next))
355 GList *last = g_list_last (cache->children);
356 cache->children = g_list_remove_link (cache->children, node);
363 GList *above_node = g_hash_table_lookup (cache->child_hash,
364 GUINT_TO_POINTER (xce->above));
365 if (above_node && node->next != above_node)
367 /* Put the window above (before in the list) above_node
369 cache->children = g_list_remove_link (cache->children, node);
370 node->prev = above_node->prev;
372 node->prev->next = node;
374 cache->children = node;
375 node->next = above_node;
376 above_node->prev = node;
384 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
386 if (!g_hash_table_lookup (cache->child_hash,
387 GUINT_TO_POINTER (xcwe->window)))
388 gdk_window_cache_add (cache, xcwe->window,
389 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
395 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
398 node = g_hash_table_lookup (cache->child_hash,
399 GUINT_TO_POINTER (xdwe->window));
402 g_hash_table_remove (cache->child_hash,
403 GUINT_TO_POINTER (xdwe->window));
404 cache->children = g_list_remove_link (cache->children, node);
406 g_list_free_1 (node);
412 XMapEvent *xme = &xevent->xmap;
415 node = g_hash_table_lookup (cache->child_hash,
416 GUINT_TO_POINTER (xme->window));
419 GdkCacheChild *child = node->data;
420 child->mapped = TRUE;
428 XMapEvent *xume = &xevent->xmap;
431 node = g_hash_table_lookup (cache->child_hash,
432 GUINT_TO_POINTER (xume->window));
435 GdkCacheChild *child = node->data;
436 child->mapped = FALSE;
441 return GDK_FILTER_CONTINUE;
443 return GDK_FILTER_REMOVE;
446 static GdkWindowCache *
447 gdk_window_cache_new (GdkScreen *screen)
449 XWindowAttributes xwa;
450 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
451 GdkWindow *root_window = gdk_screen_get_root_window (screen);
452 GdkChildInfoX11 *children;
455 GdkWindowCache *result = g_new (GdkWindowCache, 1);
457 result->children = NULL;
458 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
459 result->screen = screen;
461 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
462 result->old_event_mask = xwa.your_event_mask;
463 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
464 result->old_event_mask | SubstructureNotifyMask);
465 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
467 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
468 GDK_WINDOW_XWINDOW (root_window),
470 &children, &nchildren))
473 for (i = 0; i < nchildren ; i++)
475 gdk_window_cache_add (result, children[i].window,
476 children[i].x, children[i].y, children[i].width, children[i].height,
477 children[i].is_mapped);
486 gdk_window_cache_destroy (GdkWindowCache *cache)
488 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
490 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
491 GDK_WINDOW_XWINDOW (root_window),
492 cache->old_event_mask);
493 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
495 g_list_foreach (cache->children, (GFunc)g_free, NULL);
496 g_list_free (cache->children);
497 g_hash_table_destroy (cache->child_hash);
503 get_client_window_at_coords_recurse (GdkDisplay *display,
505 gboolean is_toplevel,
509 GdkChildInfoX11 *children;
510 unsigned int nchildren;
512 gboolean found_child = FALSE;
513 GdkChildInfoX11 child = { 0, };
514 gboolean has_wm_state = FALSE;
516 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
517 is_toplevel? &has_wm_state : NULL,
518 &children, &nchildren))
528 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
530 GdkChildInfoX11 *cur_child = &children[i];
532 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
533 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
534 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
547 if (child.has_wm_state)
550 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
557 get_client_window_at_coords (GdkWindowCache *cache,
563 Window retval = None;
565 gdk_error_trap_push ();
567 tmp_list = cache->children;
569 while (tmp_list && !retval)
571 GdkCacheChild *child = tmp_list->data;
573 if ((child->xid != ignore) && (child->mapped))
575 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
576 (y_root >= child->y) && (y_root < child->y + child->height))
578 retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
587 tmp_list = tmp_list->next;
590 gdk_error_trap_pop ();
595 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
598 /*************************************************************
599 ***************************** MOTIF *************************
600 *************************************************************/
602 /* values used in the message type for Motif DND */
615 /* Values used to specify type of protocol to use */
619 XmDRAG_PREFER_PREREGISTER,
621 XmDRAG_PREFER_DYNAMIC,
623 XmDRAG_PREFER_RECEIVER
626 /* Operation codes */
634 /* Drop site status */
636 XmNO_DROP_SITE = 0x01,
637 XmDROP_SITE_INVALID = 0x02,
638 XmDROP_SITE_VALID = 0x03
641 /* completion status */
649 /* Byte swapping routines. The motif specification leaves it
650 * up to us to save a few bytes in the client messages
652 static gchar local_byte_order = '\0';
654 #ifdef G_ENABLE_DEBUG
656 print_target_list (GList *targets)
660 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
661 g_message ("\t%s", name);
663 targets = targets->next;
666 #endif /* G_ENABLE_DEBUG */
669 init_byte_order (void)
671 guint32 myint = 0x01020304;
672 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
676 card16_to_host (guint16 x, gchar byte_order) {
677 if (byte_order == local_byte_order)
680 return (x << 8) | (x >> 8);
684 card32_to_host (guint32 x, gchar byte_order) {
685 if (byte_order == local_byte_order)
688 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
691 /* Motif packs together fields of varying length into the
692 * client message. We can't rely on accessing these
693 * through data.s[], data.l[], etc, because on some architectures
694 * (i.e., Alpha) these won't be valid for format == 8.
697 #define MOTIF_XCLIENT_BYTE(xevent,i) \
698 (xevent)->xclient.data.b[i]
699 #define MOTIF_XCLIENT_SHORT(xevent,i) \
700 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
701 #define MOTIF_XCLIENT_LONG(xevent,i) \
702 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
704 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
705 #define MOTIF_UNPACK_SHORT(xevent,i) \
706 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
707 #define MOTIF_UNPACK_LONG(xevent,i) \
708 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
710 /***** Dest side ***********/
712 /* Property placed on source windows */
713 typedef struct _MotifDragInitiatorInfo {
715 guint8 protocol_version;
716 guint16 targets_index;
717 guint32 selection_atom;
718 } MotifDragInitiatorInfo;
720 /* Header for target table on the drag window */
721 typedef struct _MotifTargetTableHeader {
723 guchar protocol_version;
726 } MotifTargetTableHeader;
728 /* Property placed on target windows */
729 typedef struct _MotifDragReceiverInfo {
731 guint8 protocol_version;
732 guint8 protocol_style;
734 guint32 proxy_window;
735 guint16 num_drop_sites;
738 } MotifDragReceiverInfo;
740 /* Target table handling */
742 static GdkFilterReturn
743 motif_drag_window_filter (GdkXEvent *xevent,
747 XEvent *xev = (XEvent *)xevent;
748 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
749 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
751 switch (xev->xany.type)
754 display_x11->motif_drag_window = None;
755 display_x11->motif_drag_gdk_window = NULL;
758 if (display_x11->motif_target_lists &&
759 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
760 motif_read_target_table (display);
763 return GDK_FILTER_REMOVE;
767 motif_lookup_drag_window (GdkDisplay *display,
768 Display *lookup_xdisplay)
770 Window retval = None;
771 gulong bytes_after, nitems;
776 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
777 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
779 XA_WINDOW, &type, &format, &nitems, &bytes_after,
782 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
784 retval = *(Window *)data;
786 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
795 /* Finds the window where global Motif drag information is stored.
796 * If it doesn't exist and 'create' is TRUE, create one.
799 motif_find_drag_window (GdkDisplay *display,
802 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
804 if (!display_x11->motif_drag_window)
806 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
807 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
809 if (!display_x11->motif_drag_window && create)
811 /* Create a persistant window. (Copied from LessTif) */
813 Display *persistant_xdisplay;
814 XSetWindowAttributes attr;
815 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
816 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
818 XGrabServer (persistant_xdisplay);
820 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
822 if (!display_x11->motif_drag_window)
824 attr.override_redirect = True;
825 attr.event_mask = PropertyChangeMask;
827 display_x11->motif_drag_window =
828 XCreateWindow (persistant_xdisplay,
829 RootWindow (persistant_xdisplay, 0),
830 -100, -100, 10, 10, 0, 0,
831 InputOnly, (Visual *)CopyFromParent,
832 (CWOverrideRedirect | CWEventMask), &attr);
835 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
837 XChangeProperty (persistant_xdisplay,
838 RootWindow (persistant_xdisplay, 0),
839 motif_drag_window_atom, XA_WINDOW,
841 (guchar *)&motif_drag_window_atom, 1);
844 XUngrabServer (persistant_xdisplay);
845 XCloseDisplay (persistant_xdisplay);
848 /* There is a miniscule race condition here if the drag window
849 * gets destroyed exactly now.
851 if (display_x11->motif_drag_window)
853 display_x11->motif_drag_gdk_window =
854 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
855 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
856 motif_drag_window_filter,
861 return display_x11->motif_drag_window;
865 motif_read_target_table (GdkDisplay *display)
867 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
868 gulong bytes_after, nitems;
873 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
875 if (display_x11->motif_target_lists)
877 for (i=0; i<display_x11->motif_n_target_lists; i++)
878 g_list_free (display_x11->motif_target_lists[i]);
880 g_free (display_x11->motif_target_lists);
881 display_x11->motif_target_lists = NULL;
882 display_x11->motif_n_target_lists = 0;
885 if (motif_find_drag_window (display, FALSE))
888 MotifTargetTableHeader *header = NULL;
889 guchar *target_bytes = NULL;
891 gboolean success = FALSE;
893 gdk_error_trap_push ();
894 XGetWindowProperty (display_x11->xdisplay,
895 display_x11->motif_drag_window,
896 motif_drag_targets_atom,
897 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
898 motif_drag_targets_atom,
899 &type, &format, &nitems, &bytes_after,
902 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
905 header = (MotifTargetTableHeader *)data;
907 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
908 header->total_size = card32_to_host (header->total_size, header->byte_order);
910 gdk_error_trap_push ();
911 XGetWindowProperty (display_x11->xdisplay,
912 display_x11->motif_drag_window,
913 motif_drag_targets_atom,
914 (sizeof(MotifTargetTableHeader)+3)/4,
915 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
917 motif_drag_targets_atom, &type, &format, &nitems,
918 &bytes_after, &target_bytes);
920 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
921 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
924 display_x11->motif_n_target_lists = header->n_lists;
925 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
928 for (i=0; i<header->n_lists; i++)
933 if (p + sizeof(guint16) - target_bytes > nitems)
936 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
938 /* We need to make a copy of the targets, since it may
941 targets = g_new (guint32, n_targets);
942 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
944 p += sizeof(guint16) + n_targets * sizeof(guint32);
945 if (p - target_bytes > nitems)
948 for (j=0; j<n_targets; j++)
949 display_x11->motif_target_lists[i] =
950 g_list_prepend (display_x11->motif_target_lists[i],
951 GUINT_TO_POINTER (card32_to_host (targets[j],
952 header->byte_order)));
954 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
964 XFree (target_bytes);
968 if (display_x11->motif_target_lists)
970 g_free (display_x11->motif_target_lists);
971 display_x11->motif_target_lists = NULL;
972 display_x11->motif_n_target_lists = 0;
974 g_warning ("Error reading Motif target table\n");
980 targets_sort_func (gconstpointer a, gconstpointer b)
982 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
983 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
986 /* Check if given (sorted) list is in the targets table */
988 motif_target_table_check (GdkDisplay *display,
991 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
992 GList *tmp_list1, *tmp_list2;
995 for (i=0; i<display_x11->motif_n_target_lists; i++)
997 tmp_list1 = display_x11->motif_target_lists[i];
1000 while (tmp_list1 && tmp_list2)
1002 if (tmp_list1->data != tmp_list2->data)
1005 tmp_list1 = tmp_list1->next;
1006 tmp_list2 = tmp_list2->next;
1008 if (!tmp_list1 && !tmp_list2) /* Found it */
1016 motif_add_to_target_table (GdkDisplay *display,
1017 GList *targets) /* targets is list of GdkAtom */
1019 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1020 GList *sorted = NULL;
1025 /* make a sorted copy of the list */
1029 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1030 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1031 targets = targets->next;
1034 /* First check if it is there already */
1036 if (display_x11->motif_target_lists)
1037 index = motif_target_table_check (display, sorted);
1039 /* We need to grab the server while doing this, to ensure
1045 /* We need to make sure that it exists _before_ we grab the
1046 * server, since we can't open a new connection after we
1049 motif_find_drag_window (display, TRUE);
1051 gdk_x11_display_grab (display);
1052 motif_read_target_table (display);
1054 /* Check again, in case it was added in the meantime */
1056 if (display_x11->motif_target_lists)
1057 index = motif_target_table_check (display, sorted);
1061 guint32 total_size = 0;
1065 MotifTargetTableHeader *header;
1067 if (!display_x11->motif_target_lists)
1069 display_x11->motif_target_lists = g_new (GList *, 1);
1070 display_x11->motif_n_target_lists = 1;
1074 display_x11->motif_n_target_lists++;
1075 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1076 sizeof(GList *) * display_x11->motif_n_target_lists);
1078 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1080 index = display_x11->motif_n_target_lists - 1;
1082 total_size = sizeof (MotifTargetTableHeader);
1083 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1084 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1086 data = g_malloc (total_size);
1088 header = (MotifTargetTableHeader *)data;
1089 p = data + sizeof(MotifTargetTableHeader);
1091 header->byte_order = local_byte_order;
1092 header->protocol_version = 0;
1093 header->n_lists = display_x11->motif_n_target_lists;
1094 header->total_size = total_size;
1096 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1098 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1099 guint32 *targets = g_new (guint32, n_targets);
1100 guint32 *p32 = targets;
1102 tmp_list = display_x11->motif_target_lists[i];
1105 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1107 tmp_list = tmp_list->next;
1112 p += sizeof(guint16);
1114 memcpy (p, targets, n_targets * sizeof(guint32));
1117 p += sizeof(guint32) * n_targets;
1121 XChangeProperty (display_x11->xdisplay,
1122 display_x11->motif_drag_window,
1123 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1124 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1128 gdk_x11_display_ungrab (display);
1131 g_list_free (sorted);
1135 /* Translate flags */
1138 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1140 guint recommended_op = flags & 0x000f;
1141 guint possible_ops = (flags & 0x0f0) >> 4;
1143 switch (recommended_op)
1146 context->suggested_action = GDK_ACTION_MOVE;
1149 context->suggested_action = GDK_ACTION_COPY;
1152 context->suggested_action = GDK_ACTION_LINK;
1155 context->suggested_action = GDK_ACTION_COPY;
1159 context->actions = 0;
1160 if (possible_ops & XmDROP_MOVE)
1161 context->actions |= GDK_ACTION_MOVE;
1162 if (possible_ops & XmDROP_COPY)
1163 context->actions |= GDK_ACTION_COPY;
1164 if (possible_ops & XmDROP_LINK)
1165 context->actions |= GDK_ACTION_LINK;
1169 motif_dnd_get_flags (GdkDragContext *context)
1173 switch (context->suggested_action)
1175 case GDK_ACTION_MOVE:
1176 flags = XmDROP_MOVE;
1178 case GDK_ACTION_COPY:
1179 flags = XmDROP_COPY;
1181 case GDK_ACTION_LINK:
1182 flags = XmDROP_LINK;
1185 flags = XmDROP_NOOP;
1189 if (context->actions & GDK_ACTION_MOVE)
1190 flags |= XmDROP_MOVE << 8;
1191 if (context->actions & GDK_ACTION_COPY)
1192 flags |= XmDROP_COPY << 8;
1193 if (context->actions & GDK_ACTION_LINK)
1194 flags |= XmDROP_LINK << 8;
1202 motif_set_targets (GdkDragContext *context)
1204 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1205 MotifDragInitiatorInfo info;
1207 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1209 info.byte_order = local_byte_order;
1210 info.protocol_version = 0;
1212 info.targets_index = motif_add_to_target_table (display, context->targets);
1217 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1219 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1220 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1224 info.selection_atom = private->motif_selection;
1226 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1227 GDK_DRAWABLE_XID (context->source_window),
1228 private->motif_selection,
1229 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1231 (guchar *)&info, sizeof (info));
1233 private->motif_targets_set = 1;
1237 motif_check_dest (GdkDisplay *display,
1240 gboolean retval = FALSE;
1242 MotifDragReceiverInfo *info;
1245 unsigned long nitems, after;
1246 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1248 gdk_error_trap_push ();
1249 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1250 motif_drag_receiver_info_atom,
1251 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1252 &type, &format, &nitems, &after,
1255 if (gdk_error_trap_pop() == 0)
1259 info = (MotifDragReceiverInfo *)data;
1261 if ((format == 8) && (nitems == sizeof(*info)))
1263 if ((info->protocol_version == 0) &&
1264 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1265 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1266 (info->protocol_style == XmDRAG_DYNAMIC)))
1272 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1279 return retval ? win : None;
1283 motif_send_enter (GdkDragContext *context,
1286 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1287 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1290 xev.xclient.type = ClientMessage;
1291 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1292 xev.xclient.format = 8;
1293 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1295 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1296 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1297 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1298 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1299 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1301 if (!private->motif_targets_set)
1302 motif_set_targets (context);
1304 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1305 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1307 if (!_gdk_send_xevent (display,
1308 GDK_DRAWABLE_XID (context->dest_window),
1311 g_message ("Send event to %lx failed",
1312 GDK_DRAWABLE_XID (context->dest_window)));
1316 motif_send_leave (GdkDragContext *context,
1319 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1322 xev.xclient.type = ClientMessage;
1323 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1324 xev.xclient.format = 8;
1325 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1327 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1328 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1329 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1330 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1331 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1332 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1333 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1335 if (!_gdk_send_xevent (display,
1336 GDK_DRAWABLE_XID (context->dest_window),
1339 g_message ("Send event to %lx failed",
1340 GDK_DRAWABLE_XID (context->dest_window)));
1344 motif_send_motion (GdkDragContext *context,
1347 GdkDragAction action,
1350 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1351 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1355 xev.xclient.type = ClientMessage;
1356 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1357 xev.xclient.format = 8;
1358 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1360 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1361 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1362 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1363 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1364 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1366 if ((context->suggested_action != private->old_action) ||
1367 (context->actions != private->old_actions))
1369 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1371 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1376 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1378 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1379 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1381 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1385 if (!_gdk_send_xevent (display,
1386 GDK_DRAWABLE_XID (context->dest_window),
1389 g_message ("Send event to %lx failed",
1390 GDK_DRAWABLE_XID (context->dest_window)));
1396 motif_send_drop (GdkDragContext *context, guint32 time)
1398 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1399 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1402 xev.xclient.type = ClientMessage;
1403 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1404 xev.xclient.format = 8;
1405 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1407 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1408 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1409 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1410 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1412 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1413 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1415 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1416 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1418 if (!_gdk_send_xevent (display,
1419 GDK_DRAWABLE_XID (context->dest_window),
1422 g_message ("Send event to %lx failed",
1423 GDK_DRAWABLE_XID (context->dest_window)));
1429 motif_read_initiator_info (GdkDisplay *display,
1430 Window source_window,
1441 MotifDragInitiatorInfo *initiator_info;
1443 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1445 gdk_error_trap_push ();
1446 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1447 0, sizeof(*initiator_info), FALSE,
1448 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1449 &type, &format, &nitems, &bytes_after,
1452 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1454 g_warning ("Error reading initiator info\n");
1458 initiator_info = (MotifDragInitiatorInfo *)data;
1460 motif_read_target_table (display);
1462 initiator_info->targets_index =
1463 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1464 initiator_info->selection_atom =
1465 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1467 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1469 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1470 XFree (initiator_info);
1474 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1479 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1480 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1481 tmp_list = tmp_list->prev;
1484 #ifdef G_ENABLE_DEBUG
1485 if (_gdk_debug_flags & GDK_DEBUG_DND)
1486 print_target_list (*targets);
1487 #endif /* G_ENABLE_DEBUG */
1489 *selection = initiator_info->selection_atom;
1491 XFree (initiator_info);
1496 static GdkDragContext *
1497 motif_drag_context_new (GdkWindow *dest_window,
1499 guint32 source_window,
1502 GdkDragContext *new_context;
1503 GdkDragContextPrivateX11 *private;
1504 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1505 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1507 /* FIXME, current_dest_drag really shouldn't be NULL'd
1508 * if we error below.
1510 if (display_x11->current_dest_drag != NULL)
1512 if (timestamp >= display_x11->current_dest_drag->start_time)
1514 g_object_unref (display_x11->current_dest_drag);
1515 display_x11->current_dest_drag = NULL;
1521 new_context = gdk_drag_context_new ();
1522 private = PRIVATE_DATA (new_context);
1524 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1525 new_context->is_source = FALSE;
1527 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1528 if (new_context->source_window)
1529 g_object_ref (new_context->source_window);
1532 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1533 if (!new_context->source_window)
1535 g_object_unref (new_context);
1540 new_context->dest_window = dest_window;
1541 g_object_ref (dest_window);
1542 new_context->start_time = timestamp;
1544 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1547 &new_context->targets,
1548 &private->motif_selection))
1550 g_object_unref (new_context);
1558 * The MOTIF drag protocol has no real provisions for distinguishing
1559 * multiple simultaneous drops. If the sources grab the pointer
1560 * when doing drags, that shouldn't happen, in any case. If it
1561 * does, we can't do much except hope for the best.
1564 static GdkFilterReturn
1565 motif_top_level_enter (GdkEvent *event,
1568 guint32 source_window,
1571 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1572 GdkDragContext *new_context;
1574 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1575 flags, timestamp, source_window, atom));
1577 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1579 return GDK_FILTER_REMOVE;
1581 event->dnd.type = GDK_DRAG_ENTER;
1582 event->dnd.context = new_context;
1583 g_object_ref (new_context);
1585 display_x11->current_dest_drag = new_context;
1587 return GDK_FILTER_TRANSLATE;
1590 static GdkFilterReturn
1591 motif_top_level_leave (GdkEvent *event,
1595 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1597 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1600 if ((display_x11->current_dest_drag != NULL) &&
1601 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1602 (timestamp >= display_x11->current_dest_drag->start_time))
1604 event->dnd.type = GDK_DRAG_LEAVE;
1605 /* Pass ownership of context to the event */
1606 event->dnd.context = display_x11->current_dest_drag;
1608 display_x11->current_dest_drag = NULL;
1610 return GDK_FILTER_TRANSLATE;
1613 return GDK_FILTER_REMOVE;
1616 static GdkFilterReturn
1617 motif_motion (GdkEvent *event,
1623 GdkDragContextPrivateX11 *private;
1624 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1626 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1627 flags, timestamp, x_root, y_root));
1629 if ((display_x11->current_dest_drag != NULL) &&
1630 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1631 (timestamp >= display_x11->current_dest_drag->start_time))
1633 private = PRIVATE_DATA (display_x11->current_dest_drag);
1635 event->dnd.type = GDK_DRAG_MOTION;
1636 event->dnd.context = display_x11->current_dest_drag;
1637 g_object_ref (display_x11->current_dest_drag);
1639 event->dnd.time = timestamp;
1641 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1643 event->dnd.x_root = x_root;
1644 event->dnd.y_root = y_root;
1646 private->last_x = x_root;
1647 private->last_y = y_root;
1649 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1651 return GDK_FILTER_TRANSLATE;
1654 return GDK_FILTER_REMOVE;
1657 static GdkFilterReturn
1658 motif_operation_changed (GdkEvent *event,
1662 GdkDragContextPrivateX11 *private;
1663 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1664 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1667 if ((display_x11->current_dest_drag != NULL) &&
1668 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1669 (timestamp >= display_x11->current_dest_drag->start_time))
1671 event->dnd.type = GDK_DRAG_MOTION;
1672 event->dnd.send_event = FALSE;
1673 event->dnd.context = display_x11->current_dest_drag;
1674 g_object_ref (display_x11->current_dest_drag);
1676 event->dnd.time = timestamp;
1677 private = PRIVATE_DATA (display_x11->current_dest_drag);
1679 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1681 event->dnd.x_root = private->last_x;
1682 event->dnd.y_root = private->last_y;
1684 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1686 return GDK_FILTER_TRANSLATE;
1689 return GDK_FILTER_REMOVE;
1692 static GdkFilterReturn
1693 motif_drop_start (GdkEvent *event,
1696 guint32 source_window,
1701 GdkDragContext *new_context;
1702 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1704 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1705 flags, timestamp, x_root, y_root, source_window, atom));
1707 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1709 return GDK_FILTER_REMOVE;
1711 motif_dnd_translate_flags (new_context, flags);
1713 event->dnd.type = GDK_DROP_START;
1714 event->dnd.context = new_context;
1715 event->dnd.time = timestamp;
1716 event->dnd.x_root = x_root;
1717 event->dnd.y_root = y_root;
1719 gdk_x11_window_set_user_time (event->any.window, timestamp);
1721 g_object_ref (new_context);
1722 display_x11->current_dest_drag = new_context;
1724 return GDK_FILTER_TRANSLATE;
1727 static GdkFilterReturn
1728 motif_drag_status (GdkEvent *event,
1732 GdkDragContext *context;
1733 GdkDisplay *display;
1736 g_message ("Motif status message: flags %x", flags));
1738 display = gdk_drawable_get_display (event->any.window);
1740 return GDK_FILTER_REMOVE;
1742 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1746 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1747 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1748 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1749 private->drag_status = GDK_DRAG_STATUS_DRAG;
1751 event->dnd.type = GDK_DRAG_STATUS;
1752 event->dnd.send_event = FALSE;
1753 event->dnd.context = context;
1754 g_object_ref (context);
1756 event->dnd.time = timestamp;
1758 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1760 switch (flags & 0x000f)
1763 context->action = 0;
1766 context->action = GDK_ACTION_MOVE;
1769 context->action = GDK_ACTION_COPY;
1772 context->action = GDK_ACTION_LINK;
1777 context->action = 0;
1779 return GDK_FILTER_TRANSLATE;
1781 return GDK_FILTER_REMOVE;
1784 static GdkFilterReturn
1785 motif_dnd_filter (GdkXEvent *xev,
1789 XEvent *xevent = (XEvent *)xev;
1794 guint32 source_window;
1796 gint16 x_root, y_root;
1799 if (!event->any.window ||
1800 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1801 return GDK_FILTER_CONTINUE; /* Not for us */
1803 /* First read some fields common to all Motif DND messages */
1805 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1806 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1807 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1809 is_reply = ((reason & 0x80) != 0);
1811 switch (reason & 0x7f)
1813 case XmTOP_LEVEL_ENTER:
1814 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1815 atom = MOTIF_UNPACK_LONG (xevent, 3);
1816 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1817 case XmTOP_LEVEL_LEAVE:
1818 return motif_top_level_leave (event, flags, timestamp);
1821 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1822 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1825 return motif_motion (event, flags, timestamp, x_root, y_root);
1827 return motif_drag_status (event, flags, timestamp);
1829 case XmDROP_SITE_ENTER:
1830 return motif_drag_status (event, flags, timestamp);
1832 case XmDROP_SITE_LEAVE:
1833 return motif_drag_status (event,
1834 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1837 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1838 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1839 atom = MOTIF_UNPACK_LONG (xevent, 3);
1840 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1843 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1846 case XmOPERATION_CHANGED:
1848 return motif_operation_changed (event, flags, timestamp);
1850 return motif_drag_status (event, flags, timestamp);
1853 /* To the best of my knowledge, these next two messages are
1854 * not part of the protocol, though they are defined in
1858 case XmDRAG_DROP_FINISH:
1862 return GDK_FILTER_REMOVE;
1865 /*************************************************************
1866 ***************************** XDND **************************
1867 *************************************************************/
1869 /* Utility functions */
1874 GdkDragAction action;
1875 } xdnd_actions_table[] = {
1876 { "XdndActionCopy", None, GDK_ACTION_COPY },
1877 { "XdndActionMove", None, GDK_ACTION_MOVE },
1878 { "XdndActionLink", None, GDK_ACTION_LINK },
1879 { "XdndActionAsk", None, GDK_ACTION_ASK },
1880 { "XdndActionPrivate", None, GDK_ACTION_COPY },
1883 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1884 static gboolean xdnd_actions_initialized = FALSE;
1887 xdnd_initialize_actions (void)
1891 xdnd_actions_initialized = TRUE;
1892 for (i=0; i < xdnd_n_actions; i++)
1893 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
1896 static GdkDragAction
1897 xdnd_action_from_atom (GdkDisplay *display,
1900 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
1903 if (!xdnd_actions_initialized)
1904 xdnd_initialize_actions();
1906 for (i=0; i<xdnd_n_actions; i++)
1907 if (atom == xdnd_actions_table[i].atom)
1908 return xdnd_actions_table[i].action;
1914 xdnd_action_to_atom (GdkDisplay *display,
1915 GdkDragAction action)
1919 if (!xdnd_actions_initialized)
1920 xdnd_initialize_actions();
1922 for (i=0; i<xdnd_n_actions; i++)
1923 if (action == xdnd_actions_table[i].action)
1924 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
1931 static GdkFilterReturn
1932 xdnd_status_filter (GdkXEvent *xev,
1936 GdkDisplay *display;
1937 XEvent *xevent = (XEvent *)xev;
1938 guint32 dest_window = xevent->xclient.data.l[0];
1939 guint32 flags = xevent->xclient.data.l[1];
1940 Atom action = xevent->xclient.data.l[4];
1941 GdkDragContext *context;
1943 if (!event->any.window ||
1944 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1945 return GDK_FILTER_CONTINUE; /* Not for us */
1948 g_message ("XdndStatus: dest_window: %#x action: %ld",
1949 dest_window, action));
1951 display = gdk_drawable_get_display (event->any.window);
1952 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1956 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1957 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1958 private->drag_status = GDK_DRAG_STATUS_DRAG;
1960 event->dnd.send_event = FALSE;
1961 event->dnd.type = GDK_DRAG_STATUS;
1962 event->dnd.context = context;
1963 g_object_ref (context);
1965 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1966 if (!(action != 0) != !(flags & 1))
1969 g_warning ("Received status event with flags not corresponding to action!\n"));
1973 context->action = xdnd_action_from_atom (display, action);
1975 return GDK_FILTER_TRANSLATE;
1978 return GDK_FILTER_REMOVE;
1981 static GdkFilterReturn
1982 xdnd_finished_filter (GdkXEvent *xev,
1986 GdkDisplay *display;
1987 XEvent *xevent = (XEvent *)xev;
1988 guint32 dest_window = xevent->xclient.data.l[0];
1989 GdkDragContext *context;
1990 GdkDragContextPrivateX11 *private;
1992 if (!event->any.window ||
1993 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1994 return GDK_FILTER_CONTINUE; /* Not for us */
1997 g_message ("XdndFinished: dest_window: %#x", dest_window));
1999 display = gdk_drawable_get_display (event->any.window);
2000 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2004 private = PRIVATE_DATA (context);
2005 if (private->version == 5)
2006 private->drop_failed = xevent->xclient.data.l[1] == 0;
2008 event->dnd.type = GDK_DROP_FINISHED;
2009 event->dnd.context = context;
2010 g_object_ref (context);
2012 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2014 return GDK_FILTER_TRANSLATE;
2017 return GDK_FILTER_REMOVE;
2021 xdnd_set_targets (GdkDragContext *context)
2023 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2025 GList *tmp_list = context->targets;
2027 gint n_atoms = g_list_length (context->targets);
2028 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2030 atomlist = g_new (Atom, n_atoms);
2034 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2035 tmp_list = tmp_list->next;
2039 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2040 GDK_DRAWABLE_XID (context->source_window),
2041 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2042 XA_ATOM, 32, PropModeReplace,
2043 (guchar *)atomlist, n_atoms);
2047 private->xdnd_targets_set = 1;
2051 xdnd_set_actions (GdkDragContext *context)
2053 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2058 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2060 if (!xdnd_actions_initialized)
2061 xdnd_initialize_actions();
2063 actions = context->actions;
2065 for (i=0; i<xdnd_n_actions; i++)
2067 if (actions & xdnd_actions_table[i].action)
2069 actions &= ~xdnd_actions_table[i].action;
2074 atomlist = g_new (Atom, n_atoms);
2076 actions = context->actions;
2078 for (i=0; i<xdnd_n_actions; i++)
2080 if (actions & xdnd_actions_table[i].action)
2082 actions &= ~xdnd_actions_table[i].action;
2083 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2088 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2089 GDK_DRAWABLE_XID (context->source_window),
2090 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2091 XA_ATOM, 32, PropModeReplace,
2092 (guchar *)atomlist, n_atoms);
2096 private->xdnd_actions_set = TRUE;
2097 private->xdnd_actions = context->actions;
2101 send_client_message_async_cb (Window window,
2105 GdkDragContext *context = data;
2107 g_message ("Got async callback for #%lx, success = %d",
2110 /* On failure, we immediately continue with the protocol
2111 * so we don't end up blocking for a timeout
2114 context->dest_window &&
2115 window == GDK_WINDOW_XID (context->dest_window))
2117 GdkEvent temp_event;
2118 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2120 g_object_unref (context->dest_window);
2121 context->dest_window = NULL;
2122 context->action = 0;
2124 private->drag_status = GDK_DRAG_STATUS_DRAG;
2126 temp_event.dnd.type = GDK_DRAG_STATUS;
2127 temp_event.dnd.window = context->source_window;
2128 temp_event.dnd.send_event = TRUE;
2129 temp_event.dnd.context = context;
2130 temp_event.dnd.time = GDK_CURRENT_TIME;
2132 gdk_event_put (&temp_event);
2135 g_object_unref (context);
2140 gdk_drag_context_get_display (GdkDragContext *context)
2142 if (context->source_window)
2143 return GDK_DRAWABLE_DISPLAY (context->source_window);
2144 else if (context->dest_window)
2145 return GDK_DRAWABLE_DISPLAY (context->dest_window);
2147 g_assert_not_reached ();
2152 send_client_message_async (GdkDragContext *context,
2156 XClientMessageEvent *event_send)
2158 GdkDisplay *display = gdk_drag_context_get_display (context);
2160 g_object_ref (context);
2162 _gdk_x11_send_client_message_async (display, window,
2163 propagate, event_mask, event_send,
2164 send_client_message_async_cb, context);
2168 xdnd_send_xevent (GdkDragContext *context,
2173 GdkDisplay *display = gdk_drag_context_get_display (context);
2177 g_assert (event_send->xany.type == ClientMessage);
2179 /* We short-circuit messages to ourselves */
2180 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2184 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2186 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2187 event_send->xclient.message_type)
2189 GdkEvent temp_event;
2190 temp_event.any.window = window;
2192 if ((*xdnd_filters[i].func) (event_send, &temp_event, NULL) == GDK_FILTER_TRANSLATE)
2194 gdk_event_put (&temp_event);
2195 g_object_unref (temp_event.dnd.context);
2203 xwindow = GDK_WINDOW_XWINDOW (window);
2205 if (_gdk_x11_display_is_root_window (display, xwindow))
2206 event_mask = ButtonPressMask;
2210 send_client_message_async (context, xwindow, propagate, event_mask,
2211 &event_send->xclient);
2217 xdnd_send_enter (GdkDragContext *context)
2220 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2221 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2223 xev.xclient.type = ClientMessage;
2224 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2225 xev.xclient.format = 32;
2226 xev.xclient.window = private->drop_xid ?
2228 GDK_DRAWABLE_XID (context->dest_window);
2229 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2230 xev.xclient.data.l[1] = (private->version << 24); /* version */
2231 xev.xclient.data.l[2] = 0;
2232 xev.xclient.data.l[3] = 0;
2233 xev.xclient.data.l[4] = 0;
2236 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2237 GDK_DRAWABLE_XID (context->source_window), private->version));
2238 if (g_list_length (context->targets) > 3)
2240 if (!private->xdnd_targets_set)
2241 xdnd_set_targets (context);
2242 xev.xclient.data.l[1] |= 1;
2246 GList *tmp_list = context->targets;
2251 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2252 GDK_POINTER_TO_ATOM (tmp_list->data));
2253 tmp_list = tmp_list->next;
2258 if (!xdnd_send_xevent (context, context->dest_window,
2262 g_message ("Send event to %lx failed",
2263 GDK_DRAWABLE_XID (context->dest_window)));
2264 g_object_unref (context->dest_window);
2265 context->dest_window = NULL;
2270 xdnd_send_leave (GdkDragContext *context)
2272 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2275 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2277 xev.xclient.type = ClientMessage;
2278 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2279 xev.xclient.format = 32;
2280 xev.xclient.window = private->drop_xid ?
2282 GDK_DRAWABLE_XID (context->dest_window);
2283 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2284 xev.xclient.data.l[1] = 0;
2285 xev.xclient.data.l[2] = 0;
2286 xev.xclient.data.l[3] = 0;
2287 xev.xclient.data.l[4] = 0;
2289 if (!xdnd_send_xevent (context, context->dest_window,
2293 g_message ("Send event to %lx failed",
2294 GDK_DRAWABLE_XID (context->dest_window)));
2295 g_object_unref (context->dest_window);
2296 context->dest_window = NULL;
2301 xdnd_send_drop (GdkDragContext *context, guint32 time)
2303 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2304 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2307 xev.xclient.type = ClientMessage;
2308 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2309 xev.xclient.format = 32;
2310 xev.xclient.window = private->drop_xid ?
2312 GDK_DRAWABLE_XID (context->dest_window);
2313 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2314 xev.xclient.data.l[1] = 0;
2315 xev.xclient.data.l[2] = time;
2316 xev.xclient.data.l[3] = 0;
2317 xev.xclient.data.l[4] = 0;
2319 if (!xdnd_send_xevent (context, context->dest_window,
2323 g_message ("Send event to %lx failed",
2324 GDK_DRAWABLE_XID (context->dest_window)));
2325 g_object_unref (context->dest_window);
2326 context->dest_window = NULL;
2331 xdnd_send_motion (GdkDragContext *context,
2334 GdkDragAction action,
2337 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2338 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2341 xev.xclient.type = ClientMessage;
2342 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2343 xev.xclient.format = 32;
2344 xev.xclient.window = private->drop_xid ?
2346 GDK_DRAWABLE_XID (context->dest_window);
2347 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2348 xev.xclient.data.l[1] = 0;
2349 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2350 xev.xclient.data.l[3] = time;
2351 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2353 if (!xdnd_send_xevent (context, context->dest_window,
2357 g_message ("Send event to %lx failed",
2358 GDK_DRAWABLE_XID (context->dest_window)));
2359 g_object_unref (context->dest_window);
2360 context->dest_window = NULL;
2362 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2366 xdnd_check_dest (GdkDisplay *display,
2368 guint *xdnd_version)
2370 gboolean retval = FALSE;
2373 unsigned long nitems, after;
2378 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2379 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2383 gdk_error_trap_push ();
2385 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2387 1, False, AnyPropertyType,
2388 &type, &format, &nitems, &after,
2393 proxy_data = (Window *)data;
2395 if ((format == 32) && (nitems == 1))
2397 proxy = *proxy_data;
2401 g_warning ("Invalid XdndProxy "
2402 "property on window %ld\n", win));
2407 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2409 1, False, AnyPropertyType,
2410 &type, &format, &nitems, &after,
2411 &data) == Success) &&
2414 version = (Atom *)data;
2416 if ((format == 32) && (nitems == 1))
2421 *xdnd_version = *version;
2425 g_warning ("Invalid XdndAware "
2426 "property on window %ld\n", win));
2432 gdk_error_trap_pop ();
2434 return retval ? (proxy ? proxy : win) : None;
2440 xdnd_read_actions (GdkDragContext *context)
2442 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2445 gulong nitems, after;
2451 PRIVATE_DATA (context)->xdnd_have_actions = FALSE;
2453 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2455 /* Get the XdndActionList, if set */
2457 gdk_error_trap_push ();
2459 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2460 GDK_DRAWABLE_XID (context->source_window),
2461 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2463 False, XA_ATOM, &type, &format, &nitems,
2464 &after, &data) == Success &&
2467 atoms = (Atom *)data;
2469 context->actions = 0;
2471 for (i=0; i<nitems; i++)
2472 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2474 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2476 #ifdef G_ENABLE_DEBUG
2477 if (_gdk_debug_flags & GDK_DEBUG_DND)
2479 GString *action_str = g_string_new (NULL);
2480 if (context->actions & GDK_ACTION_MOVE)
2481 g_string_append(action_str, "MOVE ");
2482 if (context->actions & GDK_ACTION_COPY)
2483 g_string_append(action_str, "COPY ");
2484 if (context->actions & GDK_ACTION_LINK)
2485 g_string_append(action_str, "LINK ");
2486 if (context->actions & GDK_ACTION_ASK)
2487 g_string_append(action_str, "ASK ");
2489 g_message("Xdnd actions = %s", action_str->str);
2490 g_string_free (action_str, TRUE);
2492 #endif /* G_ENABLE_DEBUG */
2499 gdk_error_trap_pop ();
2505 GdkDragContext *source_context;
2507 source_context = gdk_drag_context_find (display, TRUE,
2508 GDK_DRAWABLE_XID (context->source_window),
2509 GDK_DRAWABLE_XID (context->dest_window));
2513 context->actions = source_context->actions;
2514 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2519 /* We have to make sure that the XdndActionList we keep internally
2520 * is up to date with the XdndActionList on the source window
2521 * because we get no notification, because Xdnd wasn't meant
2522 * to continually send actions. So we select on PropertyChangeMask
2523 * and add this filter.
2525 static GdkFilterReturn
2526 xdnd_source_window_filter (GdkXEvent *xev,
2530 XEvent *xevent = (XEvent *)xev;
2531 GdkDragContext *context = cb_data;
2532 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2534 if ((xevent->xany.type == PropertyNotify) &&
2535 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2537 xdnd_read_actions (context);
2539 return GDK_FILTER_REMOVE;
2542 return GDK_FILTER_CONTINUE;
2546 xdnd_manage_source_filter (GdkDragContext *context,
2548 gboolean add_filter)
2550 if (!GDK_WINDOW_DESTROYED (window) &&
2551 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2553 gdk_error_trap_push ();
2557 gdk_window_set_events (window,
2558 gdk_window_get_events (window) |
2559 GDK_PROPERTY_CHANGE_MASK);
2560 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2564 gdk_window_remove_filter (window,
2565 xdnd_source_window_filter,
2567 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2568 * but we might want it for other reasons. (Like
2569 * INCR selection transactions).
2573 gdk_display_sync (gdk_drawable_get_display (window));
2574 gdk_error_trap_pop ();
2579 base_precache_atoms (GdkDisplay *display)
2581 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2583 if (!display_x11->base_dnd_atoms_precached)
2585 static const char *const precache_atoms[] = {
2586 "ENLIGHTENMENT_DESKTOP",
2590 "_MOTIF_DRAG_RECEIVER_INFO"
2593 _gdk_x11_precache_atoms (display,
2594 precache_atoms, G_N_ELEMENTS (precache_atoms));
2596 display_x11->base_dnd_atoms_precached = TRUE;
2601 xdnd_precache_atoms (GdkDisplay *display)
2603 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2605 if (!display_x11->xdnd_atoms_precached)
2607 static const char *const precache_atoms[] = {
2613 "XdndActionPrivate",
2624 _gdk_x11_precache_atoms (display,
2625 precache_atoms, G_N_ELEMENTS (precache_atoms));
2627 display_x11->xdnd_atoms_precached = TRUE;
2631 static GdkFilterReturn
2632 xdnd_enter_filter (GdkXEvent *xev,
2636 GdkDisplay *display;
2637 GdkDisplayX11 *display_x11;
2638 XEvent *xevent = (XEvent *)xev;
2639 GdkDragContext *new_context;
2644 gulong nitems, after;
2648 guint32 source_window;
2652 if (!event->any.window ||
2653 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2654 return GDK_FILTER_CONTINUE; /* Not for us */
2656 source_window = xevent->xclient.data.l[0];
2657 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2658 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2660 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2661 display_x11 = GDK_DISPLAY_X11 (display);
2663 xdnd_precache_atoms (display);
2666 g_message ("XdndEnter: source_window: %#x, version: %#x",
2667 source_window, version));
2671 /* Old source ignore */
2672 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2673 return GDK_FILTER_REMOVE;
2676 if (display_x11->current_dest_drag != NULL)
2678 g_object_unref (display_x11->current_dest_drag);
2679 display_x11->current_dest_drag = NULL;
2682 new_context = gdk_drag_context_new ();
2683 new_context->protocol = GDK_DRAG_PROTO_XDND;
2684 PRIVATE_DATA(new_context)->version = version;
2686 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2687 if (new_context->source_window)
2688 g_object_ref (new_context->source_window);
2691 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2692 if (!new_context->source_window)
2694 g_object_unref (new_context);
2695 return GDK_FILTER_REMOVE;
2698 new_context->dest_window = event->any.window;
2699 g_object_ref (new_context->dest_window);
2701 new_context->targets = NULL;
2704 gdk_error_trap_push ();
2705 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2707 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2709 False, XA_ATOM, &type, &format, &nitems,
2712 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2714 g_object_unref (new_context);
2719 return GDK_FILTER_REMOVE;
2722 atoms = (Atom *)data;
2724 for (i=0; i<nitems; i++)
2725 new_context->targets =
2726 g_list_append (new_context->targets,
2727 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2735 if (xevent->xclient.data.l[2+i])
2736 new_context->targets =
2737 g_list_append (new_context->targets,
2738 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2739 xevent->xclient.data.l[2+i])));
2742 #ifdef G_ENABLE_DEBUG
2743 if (_gdk_debug_flags & GDK_DEBUG_DND)
2744 print_target_list (new_context->targets);
2745 #endif /* G_ENABLE_DEBUG */
2747 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2748 xdnd_read_actions (new_context);
2750 event->dnd.type = GDK_DRAG_ENTER;
2751 event->dnd.context = new_context;
2752 g_object_ref (new_context);
2754 display_x11->current_dest_drag = new_context;
2756 return GDK_FILTER_TRANSLATE;
2759 static GdkFilterReturn
2760 xdnd_leave_filter (GdkXEvent *xev,
2764 XEvent *xevent = (XEvent *)xev;
2765 guint32 source_window = xevent->xclient.data.l[0];
2766 GdkDisplay *display;
2767 GdkDisplayX11 *display_x11;
2769 if (!event->any.window ||
2770 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2771 return GDK_FILTER_CONTINUE; /* Not for us */
2774 g_message ("XdndLeave: source_window: %#x",
2777 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2778 display_x11 = GDK_DISPLAY_X11 (display);
2780 xdnd_precache_atoms (display);
2782 if ((display_x11->current_dest_drag != NULL) &&
2783 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2784 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2786 event->dnd.type = GDK_DRAG_LEAVE;
2787 /* Pass ownership of context to the event */
2788 event->dnd.context = display_x11->current_dest_drag;
2790 display_x11->current_dest_drag = NULL;
2792 return GDK_FILTER_TRANSLATE;
2795 return GDK_FILTER_REMOVE;
2798 static GdkFilterReturn
2799 xdnd_position_filter (GdkXEvent *xev,
2803 XEvent *xevent = (XEvent *)xev;
2804 guint32 source_window = xevent->xclient.data.l[0];
2805 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2806 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2807 guint32 time = xevent->xclient.data.l[3];
2808 Atom action = xevent->xclient.data.l[4];
2810 GdkDisplay *display;
2811 GdkDisplayX11 *display_x11;
2813 if (!event->any.window ||
2814 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2815 return GDK_FILTER_CONTINUE; /* Not for us */
2818 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2819 source_window, x_root, y_root, time, action));
2821 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2822 display_x11 = GDK_DISPLAY_X11 (display);
2824 xdnd_precache_atoms (display);
2826 if ((display_x11->current_dest_drag != NULL) &&
2827 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2828 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2830 event->dnd.type = GDK_DRAG_MOTION;
2831 event->dnd.context = display_x11->current_dest_drag;
2832 g_object_ref (display_x11->current_dest_drag);
2834 event->dnd.time = time;
2836 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2838 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2839 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2841 event->dnd.x_root = x_root;
2842 event->dnd.y_root = y_root;
2844 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2845 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
2847 return GDK_FILTER_TRANSLATE;
2850 return GDK_FILTER_REMOVE;
2853 static GdkFilterReturn
2854 xdnd_drop_filter (GdkXEvent *xev,
2858 XEvent *xevent = (XEvent *)xev;
2859 guint32 source_window = xevent->xclient.data.l[0];
2860 guint32 time = xevent->xclient.data.l[2];
2861 GdkDisplay *display;
2862 GdkDisplayX11 *display_x11;
2864 if (!event->any.window ||
2865 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2866 return GDK_FILTER_CONTINUE; /* Not for us */
2869 g_message ("XdndDrop: source_window: %#x time: %d",
2870 source_window, time));
2872 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2873 display_x11 = GDK_DISPLAY_X11 (display);
2875 xdnd_precache_atoms (display);
2877 if ((display_x11->current_dest_drag != NULL) &&
2878 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2879 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2881 GdkDragContextPrivateX11 *private;
2882 private = PRIVATE_DATA (display_x11->current_dest_drag);
2884 event->dnd.type = GDK_DROP_START;
2886 event->dnd.context = display_x11->current_dest_drag;
2887 g_object_ref (display_x11->current_dest_drag);
2889 event->dnd.time = time;
2890 event->dnd.x_root = private->last_x;
2891 event->dnd.y_root = private->last_y;
2893 gdk_x11_window_set_user_time (event->any.window, time);
2895 return GDK_FILTER_TRANSLATE;
2898 return GDK_FILTER_REMOVE;
2901 /*************************************************************
2902 ************************** Public API ***********************
2903 *************************************************************/
2905 _gdk_dnd_init (GdkDisplay *display)
2910 gdk_display_add_client_message_filter (
2912 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
2913 motif_dnd_filter, NULL);
2915 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2917 gdk_display_add_client_message_filter (
2919 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
2920 xdnd_filters[i].func, NULL);
2927 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2929 if (context->dest_window)
2931 switch (context->protocol)
2933 case GDK_DRAG_PROTO_MOTIF:
2934 motif_send_leave (context, time);
2936 case GDK_DRAG_PROTO_XDND:
2937 xdnd_send_leave (context);
2939 case GDK_DRAG_PROTO_ROOTWIN:
2940 case GDK_DRAG_PROTO_NONE:
2945 g_object_unref (context->dest_window);
2946 context->dest_window = NULL;
2952 * @window: the source window for this drag.
2953 * @targets: the list of offered targets.
2955 * Starts a drag and creates a new drag context for it.
2957 * This function is called by the drag source.
2959 * Return value: a newly created #GdkDragContext.
2962 gdk_drag_begin (GdkWindow *window,
2965 GdkDragContext *new_context;
2967 g_return_val_if_fail (window != NULL, NULL);
2969 new_context = gdk_drag_context_new ();
2970 new_context->is_source = TRUE;
2971 new_context->source_window = window;
2972 g_object_ref (window);
2974 new_context->targets = g_list_copy (targets);
2975 precache_target_list (new_context);
2977 new_context->actions = 0;
2983 _gdk_drag_get_protocol_for_display (GdkDisplay *display,
2985 GdkDragProtocol *protocol,
2991 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
2993 base_precache_atoms (display);
2995 /* Check for a local drag
2997 window = gdk_window_lookup_for_display (display, xid);
2999 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3001 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3003 *protocol = GDK_DRAG_PROTO_XDND;
3005 xdnd_precache_atoms (display);
3006 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3009 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3011 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3012 GDK_NOTE (DND, g_message ("Entering root window\n"));
3019 if ((retval = xdnd_check_dest (display, xid, version)))
3021 *protocol = GDK_DRAG_PROTO_XDND;
3022 xdnd_precache_atoms (display);
3023 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3026 else if ((retval = motif_check_dest (display, xid)))
3028 *protocol = GDK_DRAG_PROTO_MOTIF;
3029 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3034 /* Check if this is a root window */
3036 gboolean rootwin = FALSE;
3039 unsigned long nitems, after;
3040 unsigned char *data;
3042 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3045 gdk_error_trap_push ();
3049 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3050 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
3051 0, 0, False, AnyPropertyType,
3052 &type, &format, &nitems, &after, &data) == Success &&
3060 /* Until I find out what window manager the next one is for,
3061 * I'm leaving it commented out. It's supported in the
3062 * xscreensaver sources, though.
3067 if (XGetWindowProperty (gdk_display, win,
3068 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
3069 0, 0, False, AnyPropertyType,
3070 &type, &format, &nitems, &data) &&
3079 gdk_error_trap_pop ();
3083 GDK_NOTE (DND, g_message ("Entering root window\n"));
3084 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3089 *protocol = GDK_DRAG_PROTO_NONE;
3094 * gdk_drag_get_protocol_for_display:
3095 * @display: the #GdkDisplay where the destination window resides
3096 * @xid: the X id of the destination window.
3097 * @protocol: location where the supported DND protocol is returned.
3098 * @returns: the X id of the window where the drop should happen. This
3099 * may be @xid or the X id of a proxy window, or None if @xid doesn't
3100 * support Drag and Drop.
3102 * Finds out the DND protocol supported by a window.
3107 gdk_drag_get_protocol_for_display (GdkDisplay *display,
3109 GdkDragProtocol *protocol)
3111 return _gdk_drag_get_protocol_for_display (display, xid, protocol, NULL);
3114 static GdkWindowCache *
3115 drag_context_find_window_cache (GdkDragContext *context,
3118 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3120 GdkWindowCache *cache;
3122 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
3124 cache = tmp_list->data;
3125 if (cache->screen == screen)
3129 cache = gdk_window_cache_new (screen);
3130 private->window_caches = g_slist_prepend (private->window_caches, cache);
3136 * gdk_drag_find_window_for_screen:
3137 * @context: a #GdkDragContext
3138 * @drag_window: a window which may be at the pointer position, but
3139 * should be ignored, since it is put up by the drag source as an icon.
3140 * @screen: the screen where the destination window is sought.
3141 * @x_root: the x position of the pointer in root coordinates.
3142 * @y_root: the y position of the pointer in root coordinates.
3143 * @dest_window: location to store the destination window in.
3144 * @protocol: location to store the DND protocol in.
3146 * Finds the destination window and DND protocol to use at the
3147 * given pointer position.
3149 * This function is called by the drag source to obtain the
3150 * @dest_window and @protocol parameters for gdk_drag_motion().
3155 gdk_drag_find_window_for_screen (GdkDragContext *context,
3156 GdkWindow *drag_window,
3160 GdkWindow **dest_window,
3161 GdkDragProtocol *protocol)
3163 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3164 GdkWindowCache *window_cache;
3165 GdkDisplay *display;
3168 g_return_if_fail (context != NULL);
3170 display = GDK_WINDOW_DISPLAY (context->source_window);
3172 window_cache = drag_context_find_window_cache (context, screen);
3174 dest = get_client_window_at_coords (window_cache,
3176 GDK_DRAWABLE_XID (drag_window) : None,
3179 if (private->dest_xid != dest)
3182 private->dest_xid = dest;
3184 /* Check if new destination accepts drags, and which protocol */
3186 /* There is some ugliness here. We actually need to pass
3187 * _three_ pieces of information to drag_motion - dest_window,
3188 * protocol, and the XID of the unproxied window. The first
3189 * two are passed explicitely, the third implicitly through
3190 * protocol->dest_xid.
3192 if ((recipient = _gdk_drag_get_protocol_for_display (display, dest,
3193 protocol, &private->version)))
3195 *dest_window = gdk_window_lookup_for_display (display, recipient);
3197 g_object_ref (*dest_window);
3199 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3202 *dest_window = NULL;
3206 *dest_window = context->dest_window;
3208 g_object_ref (*dest_window);
3209 *protocol = context->protocol;
3215 * @context: a #GdkDragContext.
3216 * @dest_window: the new destination window, obtained by
3217 * gdk_drag_find_window().
3218 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3219 * @x_root: the x position of the pointer in root coordinates.
3220 * @y_root: the y position of the pointer in root coordinates.
3221 * @suggested_action: the suggested action.
3222 * @possible_actions: the possible actions.
3223 * @time_: the timestamp for this operation.
3225 * Updates the drag context when the pointer moves or the
3226 * set of actions changes.
3228 * This function is called by the drag source.
3230 * Return value: FIXME
3233 gdk_drag_motion (GdkDragContext *context,
3234 GdkWindow *dest_window,
3235 GdkDragProtocol protocol,
3238 GdkDragAction suggested_action,
3239 GdkDragAction possible_actions,
3242 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3244 g_return_val_if_fail (context != NULL, FALSE);
3246 private->old_actions = context->actions;
3247 context->actions = possible_actions;
3249 if (private->old_actions != possible_actions)
3250 private->xdnd_actions_set = FALSE;
3252 if (protocol == GDK_DRAG_PROTO_XDND && private->version == 0)
3254 /* This ugly hack is necessary since GTK+ doesn't know about
3255 * the XDND protocol version, and in particular doesn't know
3256 * that gdk_drag_find_window_for_screen() has the side-effect
3257 * of setting private->version, and therefore sometimes call
3258 * gdk_drag_motion() without a prior call to
3259 * gdk_drag_find_window_for_screen(). This happens, e.g.
3260 * when GTK+ is proxying DND events to embedded windows.
3264 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3266 xdnd_check_dest (display,
3267 GDK_DRAWABLE_XID (dest_window),
3272 /* When we have a Xdnd target, make sure our XdndActionList
3273 * matches the current actions;
3275 if (protocol == GDK_DRAG_PROTO_XDND && !private->xdnd_actions_set)
3279 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3280 xdnd_set_actions (context);
3281 else if (context->dest_window == dest_window)
3283 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3284 GdkDragContext *dest_context;
3286 dest_context = gdk_drag_context_find (display, FALSE,
3287 GDK_DRAWABLE_XID (context->source_window),
3288 GDK_DRAWABLE_XID (dest_window));
3292 dest_context->actions = context->actions;
3293 PRIVATE_DATA (dest_context)->xdnd_have_actions = TRUE;
3299 if (context->dest_window != dest_window)
3301 GdkEvent temp_event;
3303 /* Send a leave to the last destination */
3304 gdk_drag_do_leave (context, time);
3305 private->drag_status = GDK_DRAG_STATUS_DRAG;
3307 /* Check if new destination accepts drags, and which protocol */
3311 context->dest_window = dest_window;
3312 private->drop_xid = private->dest_xid;
3313 g_object_ref (context->dest_window);
3314 context->protocol = protocol;
3318 case GDK_DRAG_PROTO_MOTIF:
3319 motif_send_enter (context, time);
3322 case GDK_DRAG_PROTO_XDND:
3323 xdnd_send_enter (context);
3326 case GDK_DRAG_PROTO_ROOTWIN:
3327 case GDK_DRAG_PROTO_NONE:
3331 private->old_action = suggested_action;
3332 context->suggested_action = suggested_action;
3333 private->old_actions = possible_actions;
3337 context->dest_window = NULL;
3338 private->drop_xid = None;
3339 context->action = 0;
3342 /* Push a status event, to let the client know that
3346 temp_event.dnd.type = GDK_DRAG_STATUS;
3347 temp_event.dnd.window = context->source_window;
3348 /* We use this to signal a synthetic status. Perhaps
3349 * we should use an extra field...
3351 temp_event.dnd.send_event = TRUE;
3353 temp_event.dnd.context = context;
3354 temp_event.dnd.time = time;
3356 gdk_event_put (&temp_event);
3360 private->old_action = context->suggested_action;
3361 context->suggested_action = suggested_action;
3364 /* Send a drag-motion event */
3366 private->last_x = x_root;
3367 private->last_y = y_root;
3369 if (context->dest_window)
3371 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3373 switch (context->protocol)
3375 case GDK_DRAG_PROTO_MOTIF:
3376 motif_send_motion (context, x_root, y_root, suggested_action, time);
3379 case GDK_DRAG_PROTO_XDND:
3380 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3383 case GDK_DRAG_PROTO_ROOTWIN:
3385 GdkEvent temp_event;
3386 /* GTK+ traditionally has used application/x-rootwin-drop,
3387 * but the XDND spec specifies x-rootwindow-drop.
3389 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3390 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3392 if (g_list_find (context->targets,
3393 GDK_ATOM_TO_POINTER (target1)) ||
3394 g_list_find (context->targets,
3395 GDK_ATOM_TO_POINTER (target2)))
3396 context->action = context->suggested_action;
3398 context->action = 0;
3400 temp_event.dnd.type = GDK_DRAG_STATUS;
3401 temp_event.dnd.window = context->source_window;
3402 temp_event.dnd.send_event = FALSE;
3403 temp_event.dnd.context = context;
3404 temp_event.dnd.time = time;
3406 gdk_event_put (&temp_event);
3409 case GDK_DRAG_PROTO_NONE:
3410 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3425 * @context: a #GdkDragContext.
3426 * @time_: the timestamp for this operation.
3428 * Drops on the current destination.
3430 * This function is called by the drag source.
3433 gdk_drag_drop (GdkDragContext *context,
3436 g_return_if_fail (context != NULL);
3438 if (context->dest_window)
3440 switch (context->protocol)
3442 case GDK_DRAG_PROTO_MOTIF:
3443 motif_send_leave (context, time);
3444 motif_send_drop (context, time);
3447 case GDK_DRAG_PROTO_XDND:
3448 xdnd_send_drop (context, time);
3451 case GDK_DRAG_PROTO_ROOTWIN:
3452 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3454 case GDK_DRAG_PROTO_NONE:
3455 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3465 * @context: a #GdkDragContext.
3466 * @time_: the timestamp for this operation.
3468 * Aborts a drag without dropping.
3470 * This function is called by the drag source.
3473 gdk_drag_abort (GdkDragContext *context,
3476 g_return_if_fail (context != NULL);
3478 gdk_drag_do_leave (context, time);
3481 /* Destination side */
3485 * @context: a #GdkDragContext.
3486 * @action: the selected action which will be taken when a drop happens,
3487 * or 0 to indicate that a drop will not be accepted.
3488 * @time_: the timestamp for this operation.
3490 * Selects one of the actions offered by the drag source.
3492 * This function is called by the drag destination in response to
3493 * gdk_drag_motion() called by the drag source.
3496 gdk_drag_status (GdkDragContext *context,
3497 GdkDragAction action,
3500 GdkDragContextPrivateX11 *private;
3502 GdkDisplay *display;
3504 g_return_if_fail (context != NULL);
3506 private = PRIVATE_DATA (context);
3507 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3509 context->action = action;
3511 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3513 gboolean need_coords = FALSE;
3515 xev.xclient.type = ClientMessage;
3516 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3517 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3518 xev.xclient.format = 8;
3519 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3521 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3523 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3527 if ((action != 0) != (private->old_action != 0))
3531 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3535 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3539 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3544 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3548 case GDK_ACTION_MOVE:
3549 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3551 case GDK_ACTION_COPY:
3552 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3554 case GDK_ACTION_LINK:
3555 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3558 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3563 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3565 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3567 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3571 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3572 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3575 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3577 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3578 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3580 if (!_gdk_send_xevent (display,
3581 GDK_DRAWABLE_XID (context->source_window),
3584 g_message ("Send event to %lx failed",
3585 GDK_DRAWABLE_XID (context->source_window)));
3587 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3589 xev.xclient.type = ClientMessage;
3590 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3591 xev.xclient.format = 32;
3592 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3594 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3595 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3596 xev.xclient.data.l[2] = 0;
3597 xev.xclient.data.l[3] = 0;
3598 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3600 if (!xdnd_send_xevent (context, context->source_window,
3603 g_message ("Send event to %lx failed",
3604 GDK_DRAWABLE_XID (context->source_window)));
3607 private->old_action = action;
3612 * @context: a #GdkDragContext.
3613 * @ok: %TRUE if the drop is accepted.
3614 * @time_: the timestamp for this operation.
3616 * Accepts or rejects a drop.
3618 * This function is called by the drag destination in response
3619 * to a drop initiated by the drag source.
3622 gdk_drop_reply (GdkDragContext *context,
3626 GdkDragContextPrivateX11 *private;
3628 g_return_if_fail (context != NULL);
3630 private = PRIVATE_DATA (context);
3632 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3634 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3637 xev.xclient.type = ClientMessage;
3638 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3639 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3640 xev.xclient.format = 8;
3642 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3643 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3645 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3646 (XmDROP_SITE_VALID << 4) |
3647 (XmDROP_NOOP << 8) |
3650 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3651 (XmNO_DROP_SITE << 4) |
3652 (XmDROP_NOOP << 8) |
3653 (XmDROP_CANCEL << 12);
3654 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3655 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3656 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3657 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3658 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3660 _gdk_send_xevent (display,
3661 GDK_DRAWABLE_XID (context->source_window),
3668 * @context: a #GtkDragContext.
3669 * @success: %TRUE if the data was successfully received.
3670 * @time_: the timestamp for this operation.
3672 * Ends the drag operation after a drop.
3674 * This function is called by the drag destination.
3677 gdk_drop_finish (GdkDragContext *context,
3681 g_return_if_fail (context != NULL);
3683 if (context->protocol == GDK_DRAG_PROTO_XDND)
3685 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3688 xev.xclient.type = ClientMessage;
3689 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3690 xev.xclient.format = 32;
3691 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3693 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3696 xev.xclient.data.l[1] = 1;
3697 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3702 xev.xclient.data.l[1] = 0;
3703 xev.xclient.data.l[2] = None;
3705 xev.xclient.data.l[3] = 0;
3706 xev.xclient.data.l[4] = 0;
3708 if (!xdnd_send_xevent (context, context->source_window,
3711 g_message ("Send event to %lx failed",
3712 GDK_DRAWABLE_XID (context->source_window)));
3718 gdk_window_register_dnd (GdkWindow *window)
3720 static const gulong xdnd_version = 5;
3721 MotifDragReceiverInfo info;
3722 Atom motif_drag_receiver_info_atom;
3723 GdkDisplay *display = gdk_drawable_get_display (window);
3725 g_return_if_fail (window != NULL);
3727 base_precache_atoms (display);
3729 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3732 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3734 /* Set Motif drag receiver information property */
3736 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3737 "_MOTIF_DRAG_RECEIVER_INFO");
3738 info.byte_order = local_byte_order;
3739 info.protocol_version = 0;
3740 info.protocol_style = XmDRAG_DYNAMIC;
3741 info.proxy_window = None;
3742 info.num_drop_sites = 0;
3743 info.total_size = sizeof(info);
3745 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3746 motif_drag_receiver_info_atom,
3747 motif_drag_receiver_info_atom,
3754 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3755 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3756 GDK_DRAWABLE_XID (window),
3757 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3758 XA_ATOM, 32, PropModeReplace,
3759 (guchar *)&xdnd_version, 1);
3763 * gdk_drag_get_selection:
3764 * @context: a #GdkDragContext.
3766 * Returns the selection atom for the current source window.
3768 * Return value: the selection atom.
3771 gdk_drag_get_selection (GdkDragContext *context)
3773 g_return_val_if_fail (context != NULL, GDK_NONE);
3775 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3776 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3777 (PRIVATE_DATA (context))->motif_selection);
3778 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3779 return gdk_atom_intern_static_string ("XdndSelection");
3785 * gdk_drag_drop_succeeded:
3786 * @context: a #GdkDragContext
3788 * Returns wether the dropped data has been successfully
3789 * transferred. This function is intended to be used while
3790 * handling a %GDK_DROP_FINISHED event, its return value is
3791 * meaningless at other times.
3793 * Return value: %TRUE if the drop was successful.
3798 gdk_drag_drop_succeeded (GdkDragContext *context)
3800 GdkDragContextPrivateX11 *private;
3802 g_return_val_if_fail (context != NULL, FALSE);
3804 private = PRIVATE_DATA (context);
3806 return !private->drop_failed;
3809 #define __GDK_DND_X11_C__
3810 #include "gdkaliasdef.c"