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 static const 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.
222 * Deprecated: 2.2: Use g_object_ref() instead.
225 gdk_drag_context_ref (GdkDragContext *context)
227 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
229 g_object_ref (context);
233 * gdk_drag_context_unref:
234 * @context: a #GdkDragContext.
236 * Deprecated function; use g_object_unref() instead.
238 * Deprecated: 2.2: Use g_object_unref() instead.
241 gdk_drag_context_unref (GdkDragContext *context)
243 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
245 g_object_unref (context);
248 static GdkDragContext *
249 gdk_drag_context_find (GdkDisplay *display,
254 GList *tmp_list = contexts;
255 GdkDragContext *context;
256 GdkDragContextPrivateX11 *private;
257 Window context_dest_xid;
261 context = (GdkDragContext *)tmp_list->data;
262 private = PRIVATE_DATA (context);
264 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
265 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
268 context_dest_xid = context->dest_window ?
271 GDK_DRAWABLE_XID (context->dest_window)) :
274 if ((!context->is_source == !is_source) &&
275 ((source_xid == None) || (context->source_window &&
276 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
277 ((dest_xid == None) || (context_dest_xid == dest_xid)))
280 tmp_list = tmp_list->next;
287 precache_target_list (GdkDragContext *context)
289 if (context->targets)
291 GPtrArray *targets = g_ptr_array_new ();
295 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
296 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
298 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
299 (const gchar **)targets->pdata,
302 for (i =0; i < targets->len; i++)
303 g_free (targets->pdata[i]);
305 g_ptr_array_free (targets, TRUE);
309 /* Utility functions */
312 gdk_window_cache_add (GdkWindowCache *cache,
314 gint x, gint y, gint width, gint height,
317 GdkCacheChild *child = g_new (GdkCacheChild, 1);
322 child->width = width;
323 child->height = height;
324 child->mapped = mapped;
326 cache->children = g_list_prepend (cache->children, child);
327 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
331 static GdkFilterReturn
332 gdk_window_cache_filter (GdkXEvent *xev,
336 XEvent *xevent = (XEvent *)xev;
337 GdkWindowCache *cache = data;
339 switch (xevent->type)
341 case CirculateNotify:
343 case ConfigureNotify:
345 XConfigureEvent *xce = &xevent->xconfigure;
348 node = g_hash_table_lookup (cache->child_hash,
349 GUINT_TO_POINTER (xce->window));
352 GdkCacheChild *child = node->data;
355 child->width = xce->width;
356 child->height = xce->height;
357 if (xce->above == None && (node->next))
359 GList *last = g_list_last (cache->children);
360 cache->children = g_list_remove_link (cache->children, node);
367 GList *above_node = g_hash_table_lookup (cache->child_hash,
368 GUINT_TO_POINTER (xce->above));
369 if (above_node && node->next != above_node)
371 /* Put the window above (before in the list) above_node
373 cache->children = g_list_remove_link (cache->children, node);
374 node->prev = above_node->prev;
376 node->prev->next = node;
378 cache->children = node;
379 node->next = above_node;
380 above_node->prev = node;
388 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
390 if (!g_hash_table_lookup (cache->child_hash,
391 GUINT_TO_POINTER (xcwe->window)))
392 gdk_window_cache_add (cache, xcwe->window,
393 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
399 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
402 node = g_hash_table_lookup (cache->child_hash,
403 GUINT_TO_POINTER (xdwe->window));
406 g_hash_table_remove (cache->child_hash,
407 GUINT_TO_POINTER (xdwe->window));
408 cache->children = g_list_remove_link (cache->children, node);
410 g_list_free_1 (node);
416 XMapEvent *xme = &xevent->xmap;
419 node = g_hash_table_lookup (cache->child_hash,
420 GUINT_TO_POINTER (xme->window));
423 GdkCacheChild *child = node->data;
424 child->mapped = TRUE;
432 XMapEvent *xume = &xevent->xmap;
435 node = g_hash_table_lookup (cache->child_hash,
436 GUINT_TO_POINTER (xume->window));
439 GdkCacheChild *child = node->data;
440 child->mapped = FALSE;
445 return GDK_FILTER_CONTINUE;
447 return GDK_FILTER_REMOVE;
450 static GdkWindowCache *
451 gdk_window_cache_new (GdkScreen *screen)
453 XWindowAttributes xwa;
454 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
455 GdkWindow *root_window = gdk_screen_get_root_window (screen);
456 GdkChildInfoX11 *children;
459 GdkWindowCache *result = g_new (GdkWindowCache, 1);
461 result->children = NULL;
462 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
463 result->screen = screen;
465 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
466 result->old_event_mask = xwa.your_event_mask;
468 if (G_UNLIKELY (!GDK_DISPLAY_X11 (GDK_SCREEN_X11 (screen)->display)->trusted_client))
470 GList *toplevel_windows, *list;
472 gint x, y, width, height;
474 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
475 for (list = toplevel_windows; list; list = list->next) {
476 window = GDK_WINDOW (list->data);
477 gdk_window_get_geometry (window, &x, &y, &width, &height, NULL);
478 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
480 gdk_window_is_visible (window));
482 g_list_free (toplevel_windows);
486 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
487 result->old_event_mask | SubstructureNotifyMask);
488 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
490 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
491 GDK_WINDOW_XWINDOW (root_window),
493 &children, &nchildren))
496 for (i = 0; i < nchildren ; i++)
498 gdk_window_cache_add (result, children[i].window,
499 children[i].x, children[i].y, children[i].width, children[i].height,
500 children[i].is_mapped);
509 gdk_window_cache_destroy (GdkWindowCache *cache)
511 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
513 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
514 GDK_WINDOW_XWINDOW (root_window),
515 cache->old_event_mask);
516 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
518 g_list_foreach (cache->children, (GFunc)g_free, NULL);
519 g_list_free (cache->children);
520 g_hash_table_destroy (cache->child_hash);
526 get_client_window_at_coords_recurse (GdkDisplay *display,
528 gboolean is_toplevel,
532 GdkChildInfoX11 *children;
533 unsigned int nchildren;
535 gboolean found_child = FALSE;
536 GdkChildInfoX11 child = { 0, };
537 gboolean has_wm_state = FALSE;
539 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
540 is_toplevel? &has_wm_state : NULL,
541 &children, &nchildren))
551 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
553 GdkChildInfoX11 *cur_child = &children[i];
555 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
556 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
557 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
570 if (child.has_wm_state)
573 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
580 get_client_window_at_coords (GdkWindowCache *cache,
586 Window retval = None;
588 gdk_error_trap_push ();
590 tmp_list = cache->children;
592 while (tmp_list && !retval)
594 GdkCacheChild *child = tmp_list->data;
596 if ((child->xid != ignore) && (child->mapped))
598 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
599 (y_root >= child->y) && (y_root < child->y + child->height))
601 retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
610 tmp_list = tmp_list->next;
613 gdk_error_trap_pop ();
618 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
621 /*************************************************************
622 ***************************** MOTIF *************************
623 *************************************************************/
625 /* values used in the message type for Motif DND */
638 /* Values used to specify type of protocol to use */
642 XmDRAG_PREFER_PREREGISTER,
644 XmDRAG_PREFER_DYNAMIC,
646 XmDRAG_PREFER_RECEIVER
649 /* Operation codes */
657 /* Drop site status */
659 XmNO_DROP_SITE = 0x01,
660 XmDROP_SITE_INVALID = 0x02,
661 XmDROP_SITE_VALID = 0x03
664 /* completion status */
672 /* Byte swapping routines. The motif specification leaves it
673 * up to us to save a few bytes in the client messages
675 static gchar local_byte_order = '\0';
677 #ifdef G_ENABLE_DEBUG
679 print_target_list (GList *targets)
683 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
684 g_message ("\t%s", name);
686 targets = targets->next;
689 #endif /* G_ENABLE_DEBUG */
692 init_byte_order (void)
694 guint32 myint = 0x01020304;
695 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
699 card16_to_host (guint16 x, gchar byte_order) {
700 if (byte_order == local_byte_order)
703 return (x << 8) | (x >> 8);
707 card32_to_host (guint32 x, gchar byte_order) {
708 if (byte_order == local_byte_order)
711 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
714 /* Motif packs together fields of varying length into the
715 * client message. We can't rely on accessing these
716 * through data.s[], data.l[], etc, because on some architectures
717 * (i.e., Alpha) these won't be valid for format == 8.
720 #define MOTIF_XCLIENT_BYTE(xevent,i) \
721 (xevent)->xclient.data.b[i]
722 #define MOTIF_XCLIENT_SHORT(xevent,i) \
723 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
724 #define MOTIF_XCLIENT_LONG(xevent,i) \
725 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
727 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
728 #define MOTIF_UNPACK_SHORT(xevent,i) \
729 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
730 #define MOTIF_UNPACK_LONG(xevent,i) \
731 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
733 /***** Dest side ***********/
735 /* Property placed on source windows */
736 typedef struct _MotifDragInitiatorInfo {
738 guint8 protocol_version;
739 guint16 targets_index;
740 guint32 selection_atom;
741 } MotifDragInitiatorInfo;
743 /* Header for target table on the drag window */
744 typedef struct _MotifTargetTableHeader {
746 guchar protocol_version;
749 } MotifTargetTableHeader;
751 /* Property placed on target windows */
752 typedef struct _MotifDragReceiverInfo {
754 guint8 protocol_version;
755 guint8 protocol_style;
757 guint32 proxy_window;
758 guint16 num_drop_sites;
761 } MotifDragReceiverInfo;
763 /* Target table handling */
765 static GdkFilterReturn
766 motif_drag_window_filter (GdkXEvent *xevent,
770 XEvent *xev = (XEvent *)xevent;
771 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
772 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
774 switch (xev->xany.type)
777 display_x11->motif_drag_window = None;
778 display_x11->motif_drag_gdk_window = NULL;
781 if (display_x11->motif_target_lists &&
782 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
783 motif_read_target_table (display);
786 return GDK_FILTER_REMOVE;
790 motif_lookup_drag_window (GdkDisplay *display,
791 Display *lookup_xdisplay)
793 Window retval = None;
794 gulong bytes_after, nitems;
799 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
800 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
802 XA_WINDOW, &type, &format, &nitems, &bytes_after,
805 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
807 retval = *(Window *)data;
809 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
818 /* Finds the window where global Motif drag information is stored.
819 * If it doesn't exist and 'create' is TRUE, create one.
822 motif_find_drag_window (GdkDisplay *display,
825 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
827 if (!display_x11->motif_drag_window)
829 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
830 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
832 if (!display_x11->motif_drag_window && create)
834 /* Create a persistant window. (Copied from LessTif) */
836 Display *persistant_xdisplay;
837 XSetWindowAttributes attr;
838 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
839 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
841 XGrabServer (persistant_xdisplay);
843 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
845 if (!display_x11->motif_drag_window)
847 attr.override_redirect = True;
848 attr.event_mask = PropertyChangeMask;
850 display_x11->motif_drag_window =
851 XCreateWindow (persistant_xdisplay,
852 RootWindow (persistant_xdisplay, 0),
853 -100, -100, 10, 10, 0, 0,
854 InputOnly, (Visual *)CopyFromParent,
855 (CWOverrideRedirect | CWEventMask), &attr);
858 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
860 XChangeProperty (persistant_xdisplay,
861 RootWindow (persistant_xdisplay, 0),
862 motif_drag_window_atom, XA_WINDOW,
864 (guchar *)&motif_drag_window_atom, 1);
867 XUngrabServer (persistant_xdisplay);
868 XCloseDisplay (persistant_xdisplay);
871 /* There is a miniscule race condition here if the drag window
872 * gets destroyed exactly now.
874 if (display_x11->motif_drag_window)
876 display_x11->motif_drag_gdk_window =
877 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
878 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
879 motif_drag_window_filter,
884 return display_x11->motif_drag_window;
888 motif_read_target_table (GdkDisplay *display)
890 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
891 gulong bytes_after, nitems;
896 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
898 if (display_x11->motif_target_lists)
900 for (i=0; i<display_x11->motif_n_target_lists; i++)
901 g_list_free (display_x11->motif_target_lists[i]);
903 g_free (display_x11->motif_target_lists);
904 display_x11->motif_target_lists = NULL;
905 display_x11->motif_n_target_lists = 0;
908 if (motif_find_drag_window (display, FALSE))
911 MotifTargetTableHeader *header = NULL;
912 guchar *target_bytes = NULL;
914 gboolean success = FALSE;
916 gdk_error_trap_push ();
917 XGetWindowProperty (display_x11->xdisplay,
918 display_x11->motif_drag_window,
919 motif_drag_targets_atom,
920 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
921 motif_drag_targets_atom,
922 &type, &format, &nitems, &bytes_after,
925 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
928 header = (MotifTargetTableHeader *)data;
930 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
931 header->total_size = card32_to_host (header->total_size, header->byte_order);
933 gdk_error_trap_push ();
934 XGetWindowProperty (display_x11->xdisplay,
935 display_x11->motif_drag_window,
936 motif_drag_targets_atom,
937 (sizeof(MotifTargetTableHeader)+3)/4,
938 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
940 motif_drag_targets_atom, &type, &format, &nitems,
941 &bytes_after, &target_bytes);
943 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
944 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
947 display_x11->motif_n_target_lists = header->n_lists;
948 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
951 for (i=0; i<header->n_lists; i++)
956 if (p + sizeof(guint16) - target_bytes > nitems)
959 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
961 /* We need to make a copy of the targets, since it may
964 targets = g_new (guint32, n_targets);
965 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
967 p += sizeof(guint16) + n_targets * sizeof(guint32);
968 if (p - target_bytes > nitems)
971 for (j=0; j<n_targets; j++)
972 display_x11->motif_target_lists[i] =
973 g_list_prepend (display_x11->motif_target_lists[i],
974 GUINT_TO_POINTER (card32_to_host (targets[j],
975 header->byte_order)));
977 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
987 XFree (target_bytes);
991 if (display_x11->motif_target_lists)
993 g_free (display_x11->motif_target_lists);
994 display_x11->motif_target_lists = NULL;
995 display_x11->motif_n_target_lists = 0;
997 g_warning ("Error reading Motif target table\n");
1003 targets_sort_func (gconstpointer a, gconstpointer b)
1005 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1006 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1009 /* Check if given (sorted) list is in the targets table */
1011 motif_target_table_check (GdkDisplay *display,
1014 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1015 GList *tmp_list1, *tmp_list2;
1018 for (i=0; i<display_x11->motif_n_target_lists; i++)
1020 tmp_list1 = display_x11->motif_target_lists[i];
1023 while (tmp_list1 && tmp_list2)
1025 if (tmp_list1->data != tmp_list2->data)
1028 tmp_list1 = tmp_list1->next;
1029 tmp_list2 = tmp_list2->next;
1031 if (!tmp_list1 && !tmp_list2) /* Found it */
1039 motif_add_to_target_table (GdkDisplay *display,
1040 GList *targets) /* targets is list of GdkAtom */
1042 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1043 GList *sorted = NULL;
1048 /* make a sorted copy of the list */
1052 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1053 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1054 targets = targets->next;
1057 /* First check if it is there already */
1059 if (display_x11->motif_target_lists)
1060 index = motif_target_table_check (display, sorted);
1062 /* We need to grab the server while doing this, to ensure
1068 /* We need to make sure that it exists _before_ we grab the
1069 * server, since we can't open a new connection after we
1072 motif_find_drag_window (display, TRUE);
1074 gdk_x11_display_grab (display);
1075 motif_read_target_table (display);
1077 /* Check again, in case it was added in the meantime */
1079 if (display_x11->motif_target_lists)
1080 index = motif_target_table_check (display, sorted);
1084 guint32 total_size = 0;
1088 MotifTargetTableHeader *header;
1090 if (!display_x11->motif_target_lists)
1092 display_x11->motif_target_lists = g_new (GList *, 1);
1093 display_x11->motif_n_target_lists = 1;
1097 display_x11->motif_n_target_lists++;
1098 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1099 sizeof(GList *) * display_x11->motif_n_target_lists);
1101 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1103 index = display_x11->motif_n_target_lists - 1;
1105 total_size = sizeof (MotifTargetTableHeader);
1106 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1107 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1109 data = g_malloc (total_size);
1111 header = (MotifTargetTableHeader *)data;
1112 p = data + sizeof(MotifTargetTableHeader);
1114 header->byte_order = local_byte_order;
1115 header->protocol_version = 0;
1116 header->n_lists = display_x11->motif_n_target_lists;
1117 header->total_size = total_size;
1119 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1121 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1122 guint32 *targets = g_new (guint32, n_targets);
1123 guint32 *p32 = targets;
1125 tmp_list = display_x11->motif_target_lists[i];
1128 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1130 tmp_list = tmp_list->next;
1135 p += sizeof(guint16);
1137 memcpy (p, targets, n_targets * sizeof(guint32));
1140 p += sizeof(guint32) * n_targets;
1144 XChangeProperty (display_x11->xdisplay,
1145 display_x11->motif_drag_window,
1146 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1147 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1151 gdk_x11_display_ungrab (display);
1154 g_list_free (sorted);
1158 /* Translate flags */
1161 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1163 guint recommended_op = flags & 0x000f;
1164 guint possible_ops = (flags & 0x0f0) >> 4;
1166 switch (recommended_op)
1169 context->suggested_action = GDK_ACTION_MOVE;
1172 context->suggested_action = GDK_ACTION_COPY;
1175 context->suggested_action = GDK_ACTION_LINK;
1178 context->suggested_action = GDK_ACTION_COPY;
1182 context->actions = 0;
1183 if (possible_ops & XmDROP_MOVE)
1184 context->actions |= GDK_ACTION_MOVE;
1185 if (possible_ops & XmDROP_COPY)
1186 context->actions |= GDK_ACTION_COPY;
1187 if (possible_ops & XmDROP_LINK)
1188 context->actions |= GDK_ACTION_LINK;
1192 motif_dnd_get_flags (GdkDragContext *context)
1196 switch (context->suggested_action)
1198 case GDK_ACTION_MOVE:
1199 flags = XmDROP_MOVE;
1201 case GDK_ACTION_COPY:
1202 flags = XmDROP_COPY;
1204 case GDK_ACTION_LINK:
1205 flags = XmDROP_LINK;
1208 flags = XmDROP_NOOP;
1212 if (context->actions & GDK_ACTION_MOVE)
1213 flags |= XmDROP_MOVE << 8;
1214 if (context->actions & GDK_ACTION_COPY)
1215 flags |= XmDROP_COPY << 8;
1216 if (context->actions & GDK_ACTION_LINK)
1217 flags |= XmDROP_LINK << 8;
1225 motif_set_targets (GdkDragContext *context)
1227 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1228 MotifDragInitiatorInfo info;
1230 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1232 info.byte_order = local_byte_order;
1233 info.protocol_version = 0;
1235 info.targets_index = motif_add_to_target_table (display, context->targets);
1240 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1242 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1243 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1247 info.selection_atom = private->motif_selection;
1249 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1250 GDK_DRAWABLE_XID (context->source_window),
1251 private->motif_selection,
1252 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1254 (guchar *)&info, sizeof (info));
1256 private->motif_targets_set = 1;
1260 motif_check_dest (GdkDisplay *display,
1263 gboolean retval = FALSE;
1265 MotifDragReceiverInfo *info;
1268 unsigned long nitems, after;
1269 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1271 gdk_error_trap_push ();
1272 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1273 motif_drag_receiver_info_atom,
1274 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1275 &type, &format, &nitems, &after,
1278 if (gdk_error_trap_pop() == 0)
1282 info = (MotifDragReceiverInfo *)data;
1284 if ((format == 8) && (nitems == sizeof(*info)))
1286 if ((info->protocol_version == 0) &&
1287 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1288 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1289 (info->protocol_style == XmDRAG_DYNAMIC)))
1295 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1302 return retval ? win : None;
1306 motif_send_enter (GdkDragContext *context,
1309 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1310 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1313 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1314 return; /* Motif Dnd requires getting properties on the root window */
1316 xev.xclient.type = ClientMessage;
1317 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1318 xev.xclient.format = 8;
1319 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1321 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1322 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1323 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1324 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1325 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1327 if (!private->motif_targets_set)
1328 motif_set_targets (context);
1330 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1331 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1333 if (!_gdk_send_xevent (display,
1334 GDK_DRAWABLE_XID (context->dest_window),
1337 g_message ("Send event to %lx failed",
1338 GDK_DRAWABLE_XID (context->dest_window)));
1342 motif_send_leave (GdkDragContext *context,
1345 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1348 xev.xclient.type = ClientMessage;
1349 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1350 xev.xclient.format = 8;
1351 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1353 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1354 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1355 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1356 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1357 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1358 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1359 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1361 if (!_gdk_send_xevent (display,
1362 GDK_DRAWABLE_XID (context->dest_window),
1365 g_message ("Send event to %lx failed",
1366 GDK_DRAWABLE_XID (context->dest_window)));
1370 motif_send_motion (GdkDragContext *context,
1373 GdkDragAction action,
1376 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1377 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1381 xev.xclient.type = ClientMessage;
1382 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1383 xev.xclient.format = 8;
1384 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1386 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1387 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1388 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1389 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1390 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1392 if ((context->suggested_action != private->old_action) ||
1393 (context->actions != private->old_actions))
1395 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1397 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1402 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1404 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1405 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1407 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1411 if (!_gdk_send_xevent (display,
1412 GDK_DRAWABLE_XID (context->dest_window),
1415 g_message ("Send event to %lx failed",
1416 GDK_DRAWABLE_XID (context->dest_window)));
1422 motif_send_drop (GdkDragContext *context, guint32 time)
1424 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1425 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1428 xev.xclient.type = ClientMessage;
1429 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1430 xev.xclient.format = 8;
1431 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1433 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1434 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1435 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1436 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1438 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1439 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1441 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1442 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1444 if (!_gdk_send_xevent (display,
1445 GDK_DRAWABLE_XID (context->dest_window),
1448 g_message ("Send event to %lx failed",
1449 GDK_DRAWABLE_XID (context->dest_window)));
1455 motif_read_initiator_info (GdkDisplay *display,
1456 Window source_window,
1467 MotifDragInitiatorInfo *initiator_info;
1469 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1471 gdk_error_trap_push ();
1472 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1473 0, sizeof(*initiator_info), FALSE,
1474 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1475 &type, &format, &nitems, &bytes_after,
1478 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1480 g_warning ("Error reading initiator info\n");
1484 initiator_info = (MotifDragInitiatorInfo *)data;
1486 motif_read_target_table (display);
1488 initiator_info->targets_index =
1489 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1490 initiator_info->selection_atom =
1491 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1493 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1495 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1496 XFree (initiator_info);
1500 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1505 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1506 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1507 tmp_list = tmp_list->prev;
1510 #ifdef G_ENABLE_DEBUG
1511 if (_gdk_debug_flags & GDK_DEBUG_DND)
1512 print_target_list (*targets);
1513 #endif /* G_ENABLE_DEBUG */
1515 *selection = initiator_info->selection_atom;
1517 XFree (initiator_info);
1522 static GdkDragContext *
1523 motif_drag_context_new (GdkWindow *dest_window,
1525 guint32 source_window,
1528 GdkDragContext *new_context;
1529 GdkDragContextPrivateX11 *private;
1530 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1531 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1533 /* FIXME, current_dest_drag really shouldn't be NULL'd
1534 * if we error below.
1536 if (display_x11->current_dest_drag != NULL)
1538 if (timestamp >= display_x11->current_dest_drag->start_time)
1540 g_object_unref (display_x11->current_dest_drag);
1541 display_x11->current_dest_drag = NULL;
1547 new_context = gdk_drag_context_new ();
1548 private = PRIVATE_DATA (new_context);
1550 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1551 new_context->is_source = FALSE;
1553 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1554 if (new_context->source_window)
1555 g_object_ref (new_context->source_window);
1558 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1559 if (!new_context->source_window)
1561 g_object_unref (new_context);
1566 new_context->dest_window = dest_window;
1567 g_object_ref (dest_window);
1568 new_context->start_time = timestamp;
1570 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1573 &new_context->targets,
1574 &private->motif_selection))
1576 g_object_unref (new_context);
1584 * The MOTIF drag protocol has no real provisions for distinguishing
1585 * multiple simultaneous drops. If the sources grab the pointer
1586 * when doing drags, that shouldn't happen, in any case. If it
1587 * does, we can't do much except hope for the best.
1590 static GdkFilterReturn
1591 motif_top_level_enter (GdkEvent *event,
1594 guint32 source_window,
1597 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1598 GdkDragContext *new_context;
1600 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1601 flags, timestamp, source_window, atom));
1603 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1605 return GDK_FILTER_REMOVE;
1607 event->dnd.type = GDK_DRAG_ENTER;
1608 event->dnd.context = new_context;
1609 g_object_ref (new_context);
1611 display_x11->current_dest_drag = new_context;
1613 return GDK_FILTER_TRANSLATE;
1616 static GdkFilterReturn
1617 motif_top_level_leave (GdkEvent *event,
1621 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1623 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1626 if ((display_x11->current_dest_drag != NULL) &&
1627 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1628 (timestamp >= display_x11->current_dest_drag->start_time))
1630 event->dnd.type = GDK_DRAG_LEAVE;
1631 /* Pass ownership of context to the event */
1632 event->dnd.context = display_x11->current_dest_drag;
1634 display_x11->current_dest_drag = NULL;
1636 return GDK_FILTER_TRANSLATE;
1639 return GDK_FILTER_REMOVE;
1642 static GdkFilterReturn
1643 motif_motion (GdkEvent *event,
1649 GdkDragContextPrivateX11 *private;
1650 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1652 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1653 flags, timestamp, x_root, y_root));
1655 if ((display_x11->current_dest_drag != NULL) &&
1656 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1657 (timestamp >= display_x11->current_dest_drag->start_time))
1659 private = PRIVATE_DATA (display_x11->current_dest_drag);
1661 event->dnd.type = GDK_DRAG_MOTION;
1662 event->dnd.context = display_x11->current_dest_drag;
1663 g_object_ref (display_x11->current_dest_drag);
1665 event->dnd.time = timestamp;
1667 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1669 event->dnd.x_root = x_root;
1670 event->dnd.y_root = y_root;
1672 private->last_x = x_root;
1673 private->last_y = y_root;
1675 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1677 return GDK_FILTER_TRANSLATE;
1680 return GDK_FILTER_REMOVE;
1683 static GdkFilterReturn
1684 motif_operation_changed (GdkEvent *event,
1688 GdkDragContextPrivateX11 *private;
1689 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1690 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1693 if ((display_x11->current_dest_drag != NULL) &&
1694 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1695 (timestamp >= display_x11->current_dest_drag->start_time))
1697 event->dnd.type = GDK_DRAG_MOTION;
1698 event->dnd.send_event = FALSE;
1699 event->dnd.context = display_x11->current_dest_drag;
1700 g_object_ref (display_x11->current_dest_drag);
1702 event->dnd.time = timestamp;
1703 private = PRIVATE_DATA (display_x11->current_dest_drag);
1705 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1707 event->dnd.x_root = private->last_x;
1708 event->dnd.y_root = private->last_y;
1710 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1712 return GDK_FILTER_TRANSLATE;
1715 return GDK_FILTER_REMOVE;
1718 static GdkFilterReturn
1719 motif_drop_start (GdkEvent *event,
1722 guint32 source_window,
1727 GdkDragContext *new_context;
1728 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1730 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1731 flags, timestamp, x_root, y_root, source_window, atom));
1733 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1735 return GDK_FILTER_REMOVE;
1737 motif_dnd_translate_flags (new_context, flags);
1739 event->dnd.type = GDK_DROP_START;
1740 event->dnd.context = new_context;
1741 event->dnd.time = timestamp;
1742 event->dnd.x_root = x_root;
1743 event->dnd.y_root = y_root;
1745 gdk_x11_window_set_user_time (event->any.window, timestamp);
1747 g_object_ref (new_context);
1748 display_x11->current_dest_drag = new_context;
1750 return GDK_FILTER_TRANSLATE;
1753 static GdkFilterReturn
1754 motif_drag_status (GdkEvent *event,
1758 GdkDragContext *context;
1759 GdkDisplay *display;
1762 g_message ("Motif status message: flags %x", flags));
1764 display = gdk_drawable_get_display (event->any.window);
1766 return GDK_FILTER_REMOVE;
1768 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1772 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1773 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1774 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1775 private->drag_status = GDK_DRAG_STATUS_DRAG;
1777 event->dnd.type = GDK_DRAG_STATUS;
1778 event->dnd.send_event = FALSE;
1779 event->dnd.context = context;
1780 g_object_ref (context);
1782 event->dnd.time = timestamp;
1784 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1786 switch (flags & 0x000f)
1789 context->action = 0;
1792 context->action = GDK_ACTION_MOVE;
1795 context->action = GDK_ACTION_COPY;
1798 context->action = GDK_ACTION_LINK;
1803 context->action = 0;
1805 return GDK_FILTER_TRANSLATE;
1807 return GDK_FILTER_REMOVE;
1810 static GdkFilterReturn
1811 motif_dnd_filter (GdkXEvent *xev,
1815 XEvent *xevent = (XEvent *)xev;
1820 guint32 source_window;
1822 gint16 x_root, y_root;
1825 if (!event->any.window ||
1826 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1827 return GDK_FILTER_CONTINUE; /* Not for us */
1829 /* First read some fields common to all Motif DND messages */
1831 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1832 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1833 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1835 is_reply = ((reason & 0x80) != 0);
1837 switch (reason & 0x7f)
1839 case XmTOP_LEVEL_ENTER:
1840 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1841 atom = MOTIF_UNPACK_LONG (xevent, 3);
1842 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1843 case XmTOP_LEVEL_LEAVE:
1844 return motif_top_level_leave (event, flags, timestamp);
1847 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1848 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1851 return motif_motion (event, flags, timestamp, x_root, y_root);
1853 return motif_drag_status (event, flags, timestamp);
1855 case XmDROP_SITE_ENTER:
1856 return motif_drag_status (event, flags, timestamp);
1858 case XmDROP_SITE_LEAVE:
1859 return motif_drag_status (event,
1860 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1863 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1864 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1865 atom = MOTIF_UNPACK_LONG (xevent, 3);
1866 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1869 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1872 case XmOPERATION_CHANGED:
1874 return motif_operation_changed (event, flags, timestamp);
1876 return motif_drag_status (event, flags, timestamp);
1879 /* To the best of my knowledge, these next two messages are
1880 * not part of the protocol, though they are defined in
1884 case XmDRAG_DROP_FINISH:
1888 return GDK_FILTER_REMOVE;
1891 /*************************************************************
1892 ***************************** XDND **************************
1893 *************************************************************/
1895 /* Utility functions */
1900 GdkDragAction action;
1901 } xdnd_actions_table[] = {
1902 { "XdndActionCopy", None, GDK_ACTION_COPY },
1903 { "XdndActionMove", None, GDK_ACTION_MOVE },
1904 { "XdndActionLink", None, GDK_ACTION_LINK },
1905 { "XdndActionAsk", None, GDK_ACTION_ASK },
1906 { "XdndActionPrivate", None, GDK_ACTION_COPY },
1909 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1910 static gboolean xdnd_actions_initialized = FALSE;
1913 xdnd_initialize_actions (void)
1917 xdnd_actions_initialized = TRUE;
1918 for (i=0; i < xdnd_n_actions; i++)
1919 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
1922 static GdkDragAction
1923 xdnd_action_from_atom (GdkDisplay *display,
1932 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
1934 if (!xdnd_actions_initialized)
1935 xdnd_initialize_actions();
1937 for (i=0; i<xdnd_n_actions; i++)
1938 if (atom == xdnd_actions_table[i].atom)
1939 return xdnd_actions_table[i].action;
1945 xdnd_action_to_atom (GdkDisplay *display,
1946 GdkDragAction action)
1950 if (!xdnd_actions_initialized)
1951 xdnd_initialize_actions();
1953 for (i=0; i<xdnd_n_actions; i++)
1954 if (action == xdnd_actions_table[i].action)
1955 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
1962 static GdkFilterReturn
1963 xdnd_status_filter (GdkXEvent *xev,
1967 GdkDisplay *display;
1968 XEvent *xevent = (XEvent *)xev;
1969 guint32 dest_window = xevent->xclient.data.l[0];
1970 guint32 flags = xevent->xclient.data.l[1];
1971 Atom action = xevent->xclient.data.l[4];
1972 GdkDragContext *context;
1974 if (!event->any.window ||
1975 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1976 return GDK_FILTER_CONTINUE; /* Not for us */
1979 g_message ("XdndStatus: dest_window: %#x action: %ld",
1980 dest_window, action));
1982 display = gdk_drawable_get_display (event->any.window);
1983 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1987 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1988 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1989 private->drag_status = GDK_DRAG_STATUS_DRAG;
1991 event->dnd.send_event = FALSE;
1992 event->dnd.type = GDK_DRAG_STATUS;
1993 event->dnd.context = context;
1994 g_object_ref (context);
1996 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1997 if (!(action != 0) != !(flags & 1))
2000 g_warning ("Received status event with flags not corresponding to action!\n"));
2004 context->action = xdnd_action_from_atom (display, action);
2006 return GDK_FILTER_TRANSLATE;
2009 return GDK_FILTER_REMOVE;
2012 static GdkFilterReturn
2013 xdnd_finished_filter (GdkXEvent *xev,
2017 GdkDisplay *display;
2018 XEvent *xevent = (XEvent *)xev;
2019 guint32 dest_window = xevent->xclient.data.l[0];
2020 GdkDragContext *context;
2021 GdkDragContextPrivateX11 *private;
2023 if (!event->any.window ||
2024 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2025 return GDK_FILTER_CONTINUE; /* Not for us */
2028 g_message ("XdndFinished: dest_window: %#x", dest_window));
2030 display = gdk_drawable_get_display (event->any.window);
2031 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2035 private = PRIVATE_DATA (context);
2036 if (private->version == 5)
2037 private->drop_failed = xevent->xclient.data.l[1] == 0;
2039 event->dnd.type = GDK_DROP_FINISHED;
2040 event->dnd.context = context;
2041 g_object_ref (context);
2043 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2045 return GDK_FILTER_TRANSLATE;
2048 return GDK_FILTER_REMOVE;
2052 xdnd_set_targets (GdkDragContext *context)
2054 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2056 GList *tmp_list = context->targets;
2058 gint n_atoms = g_list_length (context->targets);
2059 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2061 atomlist = g_new (Atom, n_atoms);
2065 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2066 tmp_list = tmp_list->next;
2070 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2071 GDK_DRAWABLE_XID (context->source_window),
2072 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2073 XA_ATOM, 32, PropModeReplace,
2074 (guchar *)atomlist, n_atoms);
2078 private->xdnd_targets_set = 1;
2082 xdnd_set_actions (GdkDragContext *context)
2084 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2089 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2091 if (!xdnd_actions_initialized)
2092 xdnd_initialize_actions();
2094 actions = context->actions;
2096 for (i=0; i<xdnd_n_actions; i++)
2098 if (actions & xdnd_actions_table[i].action)
2100 actions &= ~xdnd_actions_table[i].action;
2105 atomlist = g_new (Atom, n_atoms);
2107 actions = context->actions;
2109 for (i=0; i<xdnd_n_actions; i++)
2111 if (actions & xdnd_actions_table[i].action)
2113 actions &= ~xdnd_actions_table[i].action;
2114 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2119 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2120 GDK_DRAWABLE_XID (context->source_window),
2121 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2122 XA_ATOM, 32, PropModeReplace,
2123 (guchar *)atomlist, n_atoms);
2127 private->xdnd_actions_set = TRUE;
2128 private->xdnd_actions = context->actions;
2132 send_client_message_async_cb (Window window,
2136 GdkDragContext *context = data;
2138 g_message ("Got async callback for #%lx, success = %d",
2141 /* On failure, we immediately continue with the protocol
2142 * so we don't end up blocking for a timeout
2145 context->dest_window &&
2146 window == GDK_WINDOW_XID (context->dest_window))
2148 GdkEvent temp_event;
2149 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2151 g_object_unref (context->dest_window);
2152 context->dest_window = NULL;
2153 context->action = 0;
2155 private->drag_status = GDK_DRAG_STATUS_DRAG;
2157 temp_event.dnd.type = GDK_DRAG_STATUS;
2158 temp_event.dnd.window = context->source_window;
2159 temp_event.dnd.send_event = TRUE;
2160 temp_event.dnd.context = context;
2161 temp_event.dnd.time = GDK_CURRENT_TIME;
2163 gdk_event_put (&temp_event);
2166 g_object_unref (context);
2171 gdk_drag_context_get_display (GdkDragContext *context)
2173 if (context->source_window)
2174 return GDK_DRAWABLE_DISPLAY (context->source_window);
2175 else if (context->dest_window)
2176 return GDK_DRAWABLE_DISPLAY (context->dest_window);
2178 g_assert_not_reached ();
2183 send_client_message_async (GdkDragContext *context,
2187 XClientMessageEvent *event_send)
2189 GdkDisplay *display = gdk_drag_context_get_display (context);
2191 g_object_ref (context);
2193 _gdk_x11_send_client_message_async (display, window,
2194 propagate, event_mask, event_send,
2195 send_client_message_async_cb, context);
2199 xdnd_send_xevent (GdkDragContext *context,
2204 GdkDisplay *display = gdk_drag_context_get_display (context);
2208 g_assert (event_send->xany.type == ClientMessage);
2210 /* We short-circuit messages to ourselves */
2211 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2215 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2217 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2218 event_send->xclient.message_type)
2220 GdkEvent temp_event;
2221 temp_event.any.window = window;
2223 if ((*xdnd_filters[i].func) (event_send, &temp_event, NULL) == GDK_FILTER_TRANSLATE)
2225 gdk_event_put (&temp_event);
2226 g_object_unref (temp_event.dnd.context);
2234 xwindow = GDK_WINDOW_XWINDOW (window);
2236 if (_gdk_x11_display_is_root_window (display, xwindow))
2237 event_mask = ButtonPressMask;
2241 send_client_message_async (context, xwindow, propagate, event_mask,
2242 &event_send->xclient);
2248 xdnd_send_enter (GdkDragContext *context)
2251 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2252 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2254 xev.xclient.type = ClientMessage;
2255 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2256 xev.xclient.format = 32;
2257 xev.xclient.window = private->drop_xid ?
2259 GDK_DRAWABLE_XID (context->dest_window);
2260 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2261 xev.xclient.data.l[1] = (private->version << 24); /* version */
2262 xev.xclient.data.l[2] = 0;
2263 xev.xclient.data.l[3] = 0;
2264 xev.xclient.data.l[4] = 0;
2267 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2268 GDK_DRAWABLE_XID (context->source_window), private->version));
2269 if (g_list_length (context->targets) > 3)
2271 if (!private->xdnd_targets_set)
2272 xdnd_set_targets (context);
2273 xev.xclient.data.l[1] |= 1;
2277 GList *tmp_list = context->targets;
2282 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2283 GDK_POINTER_TO_ATOM (tmp_list->data));
2284 tmp_list = tmp_list->next;
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_leave (GdkDragContext *context)
2303 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2306 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2308 xev.xclient.type = ClientMessage;
2309 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2310 xev.xclient.format = 32;
2311 xev.xclient.window = private->drop_xid ?
2313 GDK_DRAWABLE_XID (context->dest_window);
2314 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2315 xev.xclient.data.l[1] = 0;
2316 xev.xclient.data.l[2] = 0;
2317 xev.xclient.data.l[3] = 0;
2318 xev.xclient.data.l[4] = 0;
2320 if (!xdnd_send_xevent (context, context->dest_window,
2324 g_message ("Send event to %lx failed",
2325 GDK_DRAWABLE_XID (context->dest_window)));
2326 g_object_unref (context->dest_window);
2327 context->dest_window = NULL;
2332 xdnd_send_drop (GdkDragContext *context, guint32 time)
2334 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2335 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2338 xev.xclient.type = ClientMessage;
2339 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2340 xev.xclient.format = 32;
2341 xev.xclient.window = private->drop_xid ?
2343 GDK_DRAWABLE_XID (context->dest_window);
2344 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2345 xev.xclient.data.l[1] = 0;
2346 xev.xclient.data.l[2] = time;
2347 xev.xclient.data.l[3] = 0;
2348 xev.xclient.data.l[4] = 0;
2350 if (!xdnd_send_xevent (context, context->dest_window,
2354 g_message ("Send event to %lx failed",
2355 GDK_DRAWABLE_XID (context->dest_window)));
2356 g_object_unref (context->dest_window);
2357 context->dest_window = NULL;
2362 xdnd_send_motion (GdkDragContext *context,
2365 GdkDragAction action,
2368 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2369 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2372 xev.xclient.type = ClientMessage;
2373 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2374 xev.xclient.format = 32;
2375 xev.xclient.window = private->drop_xid ?
2377 GDK_DRAWABLE_XID (context->dest_window);
2378 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2379 xev.xclient.data.l[1] = 0;
2380 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2381 xev.xclient.data.l[3] = time;
2382 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2384 if (!xdnd_send_xevent (context, context->dest_window,
2388 g_message ("Send event to %lx failed",
2389 GDK_DRAWABLE_XID (context->dest_window)));
2390 g_object_unref (context->dest_window);
2391 context->dest_window = NULL;
2393 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2397 xdnd_check_dest (GdkDisplay *display,
2399 guint *xdnd_version)
2401 gboolean retval = FALSE;
2404 unsigned long nitems, after;
2409 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2410 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2414 gdk_error_trap_push ();
2416 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2418 1, False, AnyPropertyType,
2419 &type, &format, &nitems, &after,
2424 proxy_data = (Window *)data;
2426 if ((format == 32) && (nitems == 1))
2428 proxy = *proxy_data;
2432 g_warning ("Invalid XdndProxy "
2433 "property on window %ld\n", win));
2438 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2440 1, False, AnyPropertyType,
2441 &type, &format, &nitems, &after,
2442 &data) == Success) &&
2445 version = (Atom *)data;
2447 if ((format == 32) && (nitems == 1))
2452 *xdnd_version = *version;
2456 g_warning ("Invalid XdndAware "
2457 "property on window %ld\n", win));
2463 gdk_error_trap_pop ();
2465 return retval ? (proxy ? proxy : win) : None;
2471 xdnd_read_actions (GdkDragContext *context)
2473 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2476 gulong nitems, after;
2482 PRIVATE_DATA (context)->xdnd_have_actions = FALSE;
2484 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2486 /* Get the XdndActionList, if set */
2488 gdk_error_trap_push ();
2490 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2491 GDK_DRAWABLE_XID (context->source_window),
2492 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2494 False, XA_ATOM, &type, &format, &nitems,
2495 &after, &data) == Success &&
2498 atoms = (Atom *)data;
2500 context->actions = 0;
2502 for (i=0; i<nitems; i++)
2503 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2505 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2507 #ifdef G_ENABLE_DEBUG
2508 if (_gdk_debug_flags & GDK_DEBUG_DND)
2510 GString *action_str = g_string_new (NULL);
2511 if (context->actions & GDK_ACTION_MOVE)
2512 g_string_append(action_str, "MOVE ");
2513 if (context->actions & GDK_ACTION_COPY)
2514 g_string_append(action_str, "COPY ");
2515 if (context->actions & GDK_ACTION_LINK)
2516 g_string_append(action_str, "LINK ");
2517 if (context->actions & GDK_ACTION_ASK)
2518 g_string_append(action_str, "ASK ");
2520 g_message("Xdnd actions = %s", action_str->str);
2521 g_string_free (action_str, TRUE);
2523 #endif /* G_ENABLE_DEBUG */
2530 gdk_error_trap_pop ();
2536 GdkDragContext *source_context;
2538 source_context = gdk_drag_context_find (display, TRUE,
2539 GDK_DRAWABLE_XID (context->source_window),
2540 GDK_DRAWABLE_XID (context->dest_window));
2544 context->actions = source_context->actions;
2545 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2550 /* We have to make sure that the XdndActionList we keep internally
2551 * is up to date with the XdndActionList on the source window
2552 * because we get no notification, because Xdnd wasn't meant
2553 * to continually send actions. So we select on PropertyChangeMask
2554 * and add this filter.
2556 static GdkFilterReturn
2557 xdnd_source_window_filter (GdkXEvent *xev,
2561 XEvent *xevent = (XEvent *)xev;
2562 GdkDragContext *context = cb_data;
2563 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2565 if ((xevent->xany.type == PropertyNotify) &&
2566 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2568 xdnd_read_actions (context);
2570 return GDK_FILTER_REMOVE;
2573 return GDK_FILTER_CONTINUE;
2577 xdnd_manage_source_filter (GdkDragContext *context,
2579 gboolean add_filter)
2581 if (!GDK_WINDOW_DESTROYED (window) &&
2582 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2584 gdk_error_trap_push ();
2588 gdk_window_set_events (window,
2589 gdk_window_get_events (window) |
2590 GDK_PROPERTY_CHANGE_MASK);
2591 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2595 gdk_window_remove_filter (window,
2596 xdnd_source_window_filter,
2598 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2599 * but we might want it for other reasons. (Like
2600 * INCR selection transactions).
2604 gdk_display_sync (gdk_drawable_get_display (window));
2605 gdk_error_trap_pop ();
2610 base_precache_atoms (GdkDisplay *display)
2612 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2614 if (!display_x11->base_dnd_atoms_precached)
2616 static const char *const precache_atoms[] = {
2617 "ENLIGHTENMENT_DESKTOP",
2621 "_MOTIF_DRAG_RECEIVER_INFO"
2624 _gdk_x11_precache_atoms (display,
2625 precache_atoms, G_N_ELEMENTS (precache_atoms));
2627 display_x11->base_dnd_atoms_precached = TRUE;
2632 xdnd_precache_atoms (GdkDisplay *display)
2634 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2636 if (!display_x11->xdnd_atoms_precached)
2638 static const char *const precache_atoms[] = {
2644 "XdndActionPrivate",
2655 _gdk_x11_precache_atoms (display,
2656 precache_atoms, G_N_ELEMENTS (precache_atoms));
2658 display_x11->xdnd_atoms_precached = TRUE;
2662 static GdkFilterReturn
2663 xdnd_enter_filter (GdkXEvent *xev,
2667 GdkDisplay *display;
2668 GdkDisplayX11 *display_x11;
2669 XEvent *xevent = (XEvent *)xev;
2670 GdkDragContext *new_context;
2675 gulong nitems, after;
2679 guint32 source_window;
2683 if (!event->any.window ||
2684 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2685 return GDK_FILTER_CONTINUE; /* Not for us */
2687 source_window = xevent->xclient.data.l[0];
2688 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2689 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2691 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2692 display_x11 = GDK_DISPLAY_X11 (display);
2694 xdnd_precache_atoms (display);
2697 g_message ("XdndEnter: source_window: %#x, version: %#x",
2698 source_window, version));
2702 /* Old source ignore */
2703 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2704 return GDK_FILTER_REMOVE;
2707 if (display_x11->current_dest_drag != NULL)
2709 g_object_unref (display_x11->current_dest_drag);
2710 display_x11->current_dest_drag = NULL;
2713 new_context = gdk_drag_context_new ();
2714 new_context->protocol = GDK_DRAG_PROTO_XDND;
2715 PRIVATE_DATA(new_context)->version = version;
2717 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2718 if (new_context->source_window)
2719 g_object_ref (new_context->source_window);
2722 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2723 if (!new_context->source_window)
2725 g_object_unref (new_context);
2726 return GDK_FILTER_REMOVE;
2729 new_context->dest_window = event->any.window;
2730 g_object_ref (new_context->dest_window);
2732 new_context->targets = NULL;
2735 gdk_error_trap_push ();
2736 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2738 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2740 False, XA_ATOM, &type, &format, &nitems,
2743 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2745 g_object_unref (new_context);
2750 return GDK_FILTER_REMOVE;
2753 atoms = (Atom *)data;
2755 for (i=0; i<nitems; i++)
2756 new_context->targets =
2757 g_list_append (new_context->targets,
2758 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2766 if (xevent->xclient.data.l[2+i])
2767 new_context->targets =
2768 g_list_append (new_context->targets,
2769 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2770 xevent->xclient.data.l[2+i])));
2773 #ifdef G_ENABLE_DEBUG
2774 if (_gdk_debug_flags & GDK_DEBUG_DND)
2775 print_target_list (new_context->targets);
2776 #endif /* G_ENABLE_DEBUG */
2778 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2779 xdnd_read_actions (new_context);
2781 event->dnd.type = GDK_DRAG_ENTER;
2782 event->dnd.context = new_context;
2783 g_object_ref (new_context);
2785 display_x11->current_dest_drag = new_context;
2787 return GDK_FILTER_TRANSLATE;
2790 static GdkFilterReturn
2791 xdnd_leave_filter (GdkXEvent *xev,
2795 XEvent *xevent = (XEvent *)xev;
2796 guint32 source_window = xevent->xclient.data.l[0];
2797 GdkDisplay *display;
2798 GdkDisplayX11 *display_x11;
2800 if (!event->any.window ||
2801 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2802 return GDK_FILTER_CONTINUE; /* Not for us */
2805 g_message ("XdndLeave: source_window: %#x",
2808 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2809 display_x11 = GDK_DISPLAY_X11 (display);
2811 xdnd_precache_atoms (display);
2813 if ((display_x11->current_dest_drag != NULL) &&
2814 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2815 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2817 event->dnd.type = GDK_DRAG_LEAVE;
2818 /* Pass ownership of context to the event */
2819 event->dnd.context = display_x11->current_dest_drag;
2821 display_x11->current_dest_drag = NULL;
2823 return GDK_FILTER_TRANSLATE;
2826 return GDK_FILTER_REMOVE;
2829 static GdkFilterReturn
2830 xdnd_position_filter (GdkXEvent *xev,
2834 XEvent *xevent = (XEvent *)xev;
2835 guint32 source_window = xevent->xclient.data.l[0];
2836 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2837 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2838 guint32 time = xevent->xclient.data.l[3];
2839 Atom action = xevent->xclient.data.l[4];
2841 GdkDisplay *display;
2842 GdkDisplayX11 *display_x11;
2844 if (!event->any.window ||
2845 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2846 return GDK_FILTER_CONTINUE; /* Not for us */
2849 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2850 source_window, x_root, y_root, time, action));
2852 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2853 display_x11 = GDK_DISPLAY_X11 (display);
2855 xdnd_precache_atoms (display);
2857 if ((display_x11->current_dest_drag != NULL) &&
2858 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2859 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2861 event->dnd.type = GDK_DRAG_MOTION;
2862 event->dnd.context = display_x11->current_dest_drag;
2863 g_object_ref (display_x11->current_dest_drag);
2865 event->dnd.time = time;
2867 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2869 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2870 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2872 event->dnd.x_root = x_root;
2873 event->dnd.y_root = y_root;
2875 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2876 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
2878 return GDK_FILTER_TRANSLATE;
2881 return GDK_FILTER_REMOVE;
2884 static GdkFilterReturn
2885 xdnd_drop_filter (GdkXEvent *xev,
2889 XEvent *xevent = (XEvent *)xev;
2890 guint32 source_window = xevent->xclient.data.l[0];
2891 guint32 time = xevent->xclient.data.l[2];
2892 GdkDisplay *display;
2893 GdkDisplayX11 *display_x11;
2895 if (!event->any.window ||
2896 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2897 return GDK_FILTER_CONTINUE; /* Not for us */
2900 g_message ("XdndDrop: source_window: %#x time: %d",
2901 source_window, time));
2903 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2904 display_x11 = GDK_DISPLAY_X11 (display);
2906 xdnd_precache_atoms (display);
2908 if ((display_x11->current_dest_drag != NULL) &&
2909 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2910 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2912 GdkDragContextPrivateX11 *private;
2913 private = PRIVATE_DATA (display_x11->current_dest_drag);
2915 event->dnd.type = GDK_DROP_START;
2917 event->dnd.context = display_x11->current_dest_drag;
2918 g_object_ref (display_x11->current_dest_drag);
2920 event->dnd.time = time;
2921 event->dnd.x_root = private->last_x;
2922 event->dnd.y_root = private->last_y;
2924 gdk_x11_window_set_user_time (event->any.window, time);
2926 return GDK_FILTER_TRANSLATE;
2929 return GDK_FILTER_REMOVE;
2932 /*************************************************************
2933 ************************** Public API ***********************
2934 *************************************************************/
2936 _gdk_dnd_init (GdkDisplay *display)
2941 gdk_display_add_client_message_filter (
2943 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
2944 motif_dnd_filter, NULL);
2946 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2948 gdk_display_add_client_message_filter (
2950 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
2951 xdnd_filters[i].func, NULL);
2958 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2960 if (context->dest_window)
2962 switch (context->protocol)
2964 case GDK_DRAG_PROTO_MOTIF:
2965 motif_send_leave (context, time);
2967 case GDK_DRAG_PROTO_XDND:
2968 xdnd_send_leave (context);
2970 case GDK_DRAG_PROTO_ROOTWIN:
2971 case GDK_DRAG_PROTO_NONE:
2976 g_object_unref (context->dest_window);
2977 context->dest_window = NULL;
2983 * @window: the source window for this drag.
2984 * @targets: the list of offered targets.
2986 * Starts a drag and creates a new drag context for it.
2988 * This function is called by the drag source.
2990 * Return value: a newly created #GdkDragContext.
2993 gdk_drag_begin (GdkWindow *window,
2996 GdkDragContext *new_context;
2998 g_return_val_if_fail (window != NULL, NULL);
2999 g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), NULL);
3001 new_context = gdk_drag_context_new ();
3002 new_context->is_source = TRUE;
3003 new_context->source_window = window;
3004 g_object_ref (window);
3006 new_context->targets = g_list_copy (targets);
3007 precache_target_list (new_context);
3009 new_context->actions = 0;
3014 static GdkNativeWindow
3015 _gdk_drag_get_protocol_for_display (GdkDisplay *display,
3016 GdkNativeWindow xid,
3017 GdkDragProtocol *protocol,
3022 GdkNativeWindow retval;
3023 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
3025 base_precache_atoms (display);
3027 /* Check for a local drag
3029 window = gdk_window_lookup_for_display (display, xid);
3031 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3033 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3035 *protocol = GDK_DRAG_PROTO_XDND;
3037 xdnd_precache_atoms (display);
3038 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3041 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3043 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3044 GDK_NOTE (DND, g_message ("Entering root window\n"));
3048 else if ((retval = xdnd_check_dest (display, xid, version)))
3050 *protocol = GDK_DRAG_PROTO_XDND;
3051 xdnd_precache_atoms (display);
3052 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3055 else if ((retval = motif_check_dest (display, xid)))
3057 *protocol = GDK_DRAG_PROTO_MOTIF;
3058 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3063 /* Check if this is a root window */
3065 gboolean rootwin = FALSE;
3068 unsigned long nitems, after;
3069 unsigned char *data;
3071 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3074 gdk_error_trap_push ();
3078 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3079 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
3080 0, 0, False, AnyPropertyType,
3081 &type, &format, &nitems, &after, &data) == Success &&
3089 /* Until I find out what window manager the next one is for,
3090 * I'm leaving it commented out. It's supported in the
3091 * xscreensaver sources, though.
3096 if (XGetWindowProperty (gdk_display, win,
3097 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
3098 0, 0, False, AnyPropertyType,
3099 &type, &format, &nitems, &data) &&
3108 gdk_error_trap_pop ();
3112 GDK_NOTE (DND, g_message ("Entering root window\n"));
3113 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3118 *protocol = GDK_DRAG_PROTO_NONE;
3120 return 0; /* a.k.a. None */
3124 * gdk_drag_get_protocol_for_display:
3125 * @display: the #GdkDisplay where the destination window resides
3126 * @xid: the windowing system id of the destination window.
3127 * @protocol: location where the supported DND protocol is returned.
3128 * @returns: the windowing system id of the window where the drop should happen. This
3129 * may be @xid or the id of a proxy window, or zero if @xid doesn't
3130 * support Drag and Drop.
3132 * Finds out the DND protocol supported by a window.
3137 gdk_drag_get_protocol_for_display (GdkDisplay *display,
3138 GdkNativeWindow xid,
3139 GdkDragProtocol *protocol)
3141 return _gdk_drag_get_protocol_for_display (display, xid, protocol, NULL);
3144 static GdkWindowCache *
3145 drag_context_find_window_cache (GdkDragContext *context,
3148 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3150 GdkWindowCache *cache;
3152 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
3154 cache = tmp_list->data;
3155 if (cache->screen == screen)
3159 cache = gdk_window_cache_new (screen);
3160 private->window_caches = g_slist_prepend (private->window_caches, cache);
3166 * gdk_drag_find_window_for_screen:
3167 * @context: a #GdkDragContext
3168 * @drag_window: a window which may be at the pointer position, but
3169 * should be ignored, since it is put up by the drag source as an icon.
3170 * @screen: the screen where the destination window is sought.
3171 * @x_root: the x position of the pointer in root coordinates.
3172 * @y_root: the y position of the pointer in root coordinates.
3173 * @dest_window: (out): location to store the destination window in.
3174 * @protocol: (out): location to store the DND protocol in.
3176 * Finds the destination window and DND protocol to use at the
3177 * given pointer position.
3179 * This function is called by the drag source to obtain the
3180 * @dest_window and @protocol parameters for gdk_drag_motion().
3185 gdk_drag_find_window_for_screen (GdkDragContext *context,
3186 GdkWindow *drag_window,
3190 GdkWindow **dest_window,
3191 GdkDragProtocol *protocol)
3193 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3194 GdkWindowCache *window_cache;
3195 GdkDisplay *display;
3198 g_return_if_fail (context != NULL);
3200 display = GDK_WINDOW_DISPLAY (context->source_window);
3202 window_cache = drag_context_find_window_cache (context, screen);
3204 dest = get_client_window_at_coords (window_cache,
3205 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3206 GDK_DRAWABLE_XID (drag_window) : None,
3209 if (private->dest_xid != dest)
3212 private->dest_xid = dest;
3214 /* Check if new destination accepts drags, and which protocol */
3216 /* There is some ugliness here. We actually need to pass
3217 * _three_ pieces of information to drag_motion - dest_window,
3218 * protocol, and the XID of the unproxied window. The first
3219 * two are passed explicitely, the third implicitly through
3220 * protocol->dest_xid.
3222 if ((recipient = _gdk_drag_get_protocol_for_display (display, dest,
3223 protocol, &private->version)))
3225 *dest_window = gdk_window_lookup_for_display (display, recipient);
3227 g_object_ref (*dest_window);
3229 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3232 *dest_window = NULL;
3236 *dest_window = context->dest_window;
3238 g_object_ref (*dest_window);
3239 *protocol = context->protocol;
3245 * @context: a #GdkDragContext.
3246 * @dest_window: the new destination window, obtained by
3247 * gdk_drag_find_window().
3248 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3249 * @x_root: the x position of the pointer in root coordinates.
3250 * @y_root: the y position of the pointer in root coordinates.
3251 * @suggested_action: the suggested action.
3252 * @possible_actions: the possible actions.
3253 * @time_: the timestamp for this operation.
3255 * Updates the drag context when the pointer moves or the
3256 * set of actions changes.
3258 * This function is called by the drag source.
3260 * Return value: FIXME
3263 gdk_drag_motion (GdkDragContext *context,
3264 GdkWindow *dest_window,
3265 GdkDragProtocol protocol,
3268 GdkDragAction suggested_action,
3269 GdkDragAction possible_actions,
3272 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3274 g_return_val_if_fail (context != NULL, FALSE);
3275 g_return_val_if_fail (dest_window == NULL || GDK_WINDOW_IS_X11 (dest_window), FALSE);
3277 private->old_actions = context->actions;
3278 context->actions = possible_actions;
3280 if (private->old_actions != possible_actions)
3281 private->xdnd_actions_set = FALSE;
3283 if (protocol == GDK_DRAG_PROTO_XDND && private->version == 0)
3285 /* This ugly hack is necessary since GTK+ doesn't know about
3286 * the XDND protocol version, and in particular doesn't know
3287 * that gdk_drag_find_window_for_screen() has the side-effect
3288 * of setting private->version, and therefore sometimes call
3289 * gdk_drag_motion() without a prior call to
3290 * gdk_drag_find_window_for_screen(). This happens, e.g.
3291 * when GTK+ is proxying DND events to embedded windows.
3295 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3297 xdnd_check_dest (display,
3298 GDK_DRAWABLE_XID (dest_window),
3303 /* When we have a Xdnd target, make sure our XdndActionList
3304 * matches the current actions;
3306 if (protocol == GDK_DRAG_PROTO_XDND && !private->xdnd_actions_set)
3310 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3311 xdnd_set_actions (context);
3312 else if (context->dest_window == dest_window)
3314 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3315 GdkDragContext *dest_context;
3317 dest_context = gdk_drag_context_find (display, FALSE,
3318 GDK_DRAWABLE_XID (context->source_window),
3319 GDK_DRAWABLE_XID (dest_window));
3323 dest_context->actions = context->actions;
3324 PRIVATE_DATA (dest_context)->xdnd_have_actions = TRUE;
3330 if (context->dest_window != dest_window)
3332 GdkEvent temp_event;
3334 /* Send a leave to the last destination */
3335 gdk_drag_do_leave (context, time);
3336 private->drag_status = GDK_DRAG_STATUS_DRAG;
3338 /* Check if new destination accepts drags, and which protocol */
3342 context->dest_window = dest_window;
3343 private->drop_xid = private->dest_xid;
3344 g_object_ref (context->dest_window);
3345 context->protocol = protocol;
3349 case GDK_DRAG_PROTO_MOTIF:
3350 motif_send_enter (context, time);
3353 case GDK_DRAG_PROTO_XDND:
3354 xdnd_send_enter (context);
3357 case GDK_DRAG_PROTO_ROOTWIN:
3358 case GDK_DRAG_PROTO_NONE:
3362 private->old_action = suggested_action;
3363 context->suggested_action = suggested_action;
3364 private->old_actions = possible_actions;
3368 context->dest_window = NULL;
3369 private->drop_xid = None;
3370 context->action = 0;
3373 /* Push a status event, to let the client know that
3377 temp_event.dnd.type = GDK_DRAG_STATUS;
3378 temp_event.dnd.window = context->source_window;
3379 /* We use this to signal a synthetic status. Perhaps
3380 * we should use an extra field...
3382 temp_event.dnd.send_event = TRUE;
3384 temp_event.dnd.context = context;
3385 temp_event.dnd.time = time;
3387 gdk_event_put (&temp_event);
3391 private->old_action = context->suggested_action;
3392 context->suggested_action = suggested_action;
3395 /* Send a drag-motion event */
3397 private->last_x = x_root;
3398 private->last_y = y_root;
3400 if (context->dest_window)
3402 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3404 switch (context->protocol)
3406 case GDK_DRAG_PROTO_MOTIF:
3407 motif_send_motion (context, x_root, y_root, suggested_action, time);
3410 case GDK_DRAG_PROTO_XDND:
3411 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3414 case GDK_DRAG_PROTO_ROOTWIN:
3416 GdkEvent temp_event;
3417 /* GTK+ traditionally has used application/x-rootwin-drop,
3418 * but the XDND spec specifies x-rootwindow-drop.
3420 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3421 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3423 if (g_list_find (context->targets,
3424 GDK_ATOM_TO_POINTER (target1)) ||
3425 g_list_find (context->targets,
3426 GDK_ATOM_TO_POINTER (target2)))
3427 context->action = context->suggested_action;
3429 context->action = 0;
3431 temp_event.dnd.type = GDK_DRAG_STATUS;
3432 temp_event.dnd.window = context->source_window;
3433 temp_event.dnd.send_event = FALSE;
3434 temp_event.dnd.context = context;
3435 temp_event.dnd.time = time;
3437 gdk_event_put (&temp_event);
3440 case GDK_DRAG_PROTO_NONE:
3441 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3456 * @context: a #GdkDragContext.
3457 * @time_: the timestamp for this operation.
3459 * Drops on the current destination.
3461 * This function is called by the drag source.
3464 gdk_drag_drop (GdkDragContext *context,
3467 g_return_if_fail (context != NULL);
3469 if (context->dest_window)
3471 switch (context->protocol)
3473 case GDK_DRAG_PROTO_MOTIF:
3474 motif_send_leave (context, time);
3475 motif_send_drop (context, time);
3478 case GDK_DRAG_PROTO_XDND:
3479 xdnd_send_drop (context, time);
3482 case GDK_DRAG_PROTO_ROOTWIN:
3483 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3485 case GDK_DRAG_PROTO_NONE:
3486 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3496 * @context: a #GdkDragContext.
3497 * @time_: the timestamp for this operation.
3499 * Aborts a drag without dropping.
3501 * This function is called by the drag source.
3504 gdk_drag_abort (GdkDragContext *context,
3507 g_return_if_fail (context != NULL);
3509 gdk_drag_do_leave (context, time);
3512 /* Destination side */
3516 * @context: a #GdkDragContext.
3517 * @action: the selected action which will be taken when a drop happens,
3518 * or 0 to indicate that a drop will not be accepted.
3519 * @time_: the timestamp for this operation.
3521 * Selects one of the actions offered by the drag source.
3523 * This function is called by the drag destination in response to
3524 * gdk_drag_motion() called by the drag source.
3527 gdk_drag_status (GdkDragContext *context,
3528 GdkDragAction action,
3531 GdkDragContextPrivateX11 *private;
3533 GdkDisplay *display;
3535 g_return_if_fail (context != NULL);
3537 private = PRIVATE_DATA (context);
3538 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3540 context->action = action;
3542 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3544 gboolean need_coords = FALSE;
3546 xev.xclient.type = ClientMessage;
3547 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3548 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3549 xev.xclient.format = 8;
3550 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3552 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3554 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3558 if ((action != 0) != (private->old_action != 0))
3562 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3566 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3570 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3575 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3579 case GDK_ACTION_MOVE:
3580 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3582 case GDK_ACTION_COPY:
3583 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3585 case GDK_ACTION_LINK:
3586 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3589 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3594 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3596 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3598 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3602 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3603 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3606 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3608 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3609 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3611 if (!_gdk_send_xevent (display,
3612 GDK_DRAWABLE_XID (context->source_window),
3615 g_message ("Send event to %lx failed",
3616 GDK_DRAWABLE_XID (context->source_window)));
3618 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3620 xev.xclient.type = ClientMessage;
3621 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3622 xev.xclient.format = 32;
3623 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3625 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3626 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3627 xev.xclient.data.l[2] = 0;
3628 xev.xclient.data.l[3] = 0;
3629 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3631 if (!xdnd_send_xevent (context, context->source_window,
3634 g_message ("Send event to %lx failed",
3635 GDK_DRAWABLE_XID (context->source_window)));
3638 private->old_action = action;
3643 * @context: a #GdkDragContext.
3644 * @ok: %TRUE if the drop is accepted.
3645 * @time_: the timestamp for this operation.
3647 * Accepts or rejects a drop.
3649 * This function is called by the drag destination in response
3650 * to a drop initiated by the drag source.
3653 gdk_drop_reply (GdkDragContext *context,
3657 GdkDragContextPrivateX11 *private;
3659 g_return_if_fail (context != NULL);
3661 private = PRIVATE_DATA (context);
3663 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3665 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3668 xev.xclient.type = ClientMessage;
3669 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3670 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3671 xev.xclient.format = 8;
3673 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3674 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3676 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3677 (XmDROP_SITE_VALID << 4) |
3678 (XmDROP_NOOP << 8) |
3681 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3682 (XmNO_DROP_SITE << 4) |
3683 (XmDROP_NOOP << 8) |
3684 (XmDROP_CANCEL << 12);
3685 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3686 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3687 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3688 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3689 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3691 _gdk_send_xevent (display,
3692 GDK_DRAWABLE_XID (context->source_window),
3699 * @context: a #GtkDragContext.
3700 * @success: %TRUE if the data was successfully received.
3701 * @time_: the timestamp for this operation.
3703 * Ends the drag operation after a drop.
3705 * This function is called by the drag destination.
3708 gdk_drop_finish (GdkDragContext *context,
3712 g_return_if_fail (context != NULL);
3714 if (context->protocol == GDK_DRAG_PROTO_XDND)
3716 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3719 xev.xclient.type = ClientMessage;
3720 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3721 xev.xclient.format = 32;
3722 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3724 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3727 xev.xclient.data.l[1] = 1;
3728 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3733 xev.xclient.data.l[1] = 0;
3734 xev.xclient.data.l[2] = None;
3736 xev.xclient.data.l[3] = 0;
3737 xev.xclient.data.l[4] = 0;
3739 if (!xdnd_send_xevent (context, context->source_window,
3742 g_message ("Send event to %lx failed",
3743 GDK_DRAWABLE_XID (context->source_window)));
3749 gdk_window_register_dnd (GdkWindow *window)
3751 static const gulong xdnd_version = 5;
3752 MotifDragReceiverInfo info;
3753 Atom motif_drag_receiver_info_atom;
3754 GdkDisplay *display = gdk_drawable_get_display (window);
3756 g_return_if_fail (window != NULL);
3758 base_precache_atoms (display);
3760 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3763 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3765 /* Set Motif drag receiver information property */
3767 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3768 "_MOTIF_DRAG_RECEIVER_INFO");
3769 /* initialize to zero to avoid writing uninitialized data to socket */
3770 memset(&info, 0, sizeof(info));
3771 info.byte_order = local_byte_order;
3772 info.protocol_version = 0;
3773 info.protocol_style = XmDRAG_DYNAMIC;
3774 info.proxy_window = None;
3775 info.num_drop_sites = 0;
3776 info.total_size = sizeof(info);
3778 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3779 motif_drag_receiver_info_atom,
3780 motif_drag_receiver_info_atom,
3787 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3788 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3789 GDK_DRAWABLE_XID (window),
3790 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3791 XA_ATOM, 32, PropModeReplace,
3792 (guchar *)&xdnd_version, 1);
3796 * gdk_drag_get_selection:
3797 * @context: a #GdkDragContext.
3799 * Returns the selection atom for the current source window.
3801 * Return value: the selection atom.
3804 gdk_drag_get_selection (GdkDragContext *context)
3806 g_return_val_if_fail (context != NULL, GDK_NONE);
3808 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3809 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3810 (PRIVATE_DATA (context))->motif_selection);
3811 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3812 return gdk_atom_intern_static_string ("XdndSelection");
3818 * gdk_drag_drop_succeeded:
3819 * @context: a #GdkDragContext
3821 * Returns whether the dropped data has been successfully
3822 * transferred. This function is intended to be used while
3823 * handling a %GDK_DROP_FINISHED event, its return value is
3824 * meaningless at other times.
3826 * Return value: %TRUE if the drop was successful.
3831 gdk_drag_drop_succeeded (GdkDragContext *context)
3833 GdkDragContextPrivateX11 *private;
3835 g_return_val_if_fail (context != NULL, FALSE);
3837 private = PRIVATE_DATA (context);
3839 return !private->drop_failed;
3842 #define __GDK_DND_X11_C__
3843 #include "gdkaliasdef.c"