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 "gdkdndprivate.h"
32 #include "gdkinternals.h"
34 #include "gdkproperty.h"
35 #include "gdkprivate-x11.h"
36 #include "gdkscreen-x11.h"
37 #include "gdkdisplay-x11.h"
40 #include <X11/Xutil.h>
41 #include <X11/Xatom.h>
42 #include <X11/extensions/shape.h>
43 #include <X11/extensions/Xcomposite.h>
49 GDK_DRAG_STATUS_MOTION_WAIT,
50 GDK_DRAG_STATUS_ACTION_WAIT,
56 gint x, y, width, height;
58 gboolean shape_selected;
60 cairo_region_t *shape;
65 GHashTable *child_hash;
70 #define GDK_TYPE_DRAG_CONTEXT_X11 (gdk_drag_context_x11_get_type ())
71 #define GDK_DRAG_CONTEXT_X11(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_DRAG_CONTEXT_X11, GdkDragContextX11))
73 typedef struct _GdkDragContextX11 GdkDragContextX11;
74 typedef struct _GdkDragContextClass GdkDragContextX11Class;
76 struct _GdkDragContextX11 {
77 GdkDragContext context;
82 guint16 last_x; /* Coordinates from last event */
84 GdkDragAction old_action; /* The last action we sent to the source */
85 GdkDragAction old_actions; /* The last actions we sent to the source */
86 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
88 Window dest_xid; /* The last window we looked up */
89 Window drop_xid; /* The (non-proxied) window that is receiving drops */
90 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
91 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
92 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
93 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
94 guint drag_status : 4; /* current status of drag */
96 guint drop_failed : 1; /* Whether the drop was unsuccessful */
97 guint version; /* Xdnd protocol version */
99 GSList *window_caches;
102 /* Forward declarations */
104 static void gdk_window_cache_destroy (GdkWindowCache *cache);
106 static void motif_read_target_table (GdkDisplay *display);
108 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
112 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
115 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
118 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
121 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
124 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
127 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
131 static void xdnd_manage_source_filter (GdkDragContext *context,
133 gboolean add_filter);
135 static GList *contexts;
137 static const struct {
138 const char *atom_name;
141 { "XdndEnter", xdnd_enter_filter },
142 { "XdndLeave", xdnd_leave_filter },
143 { "XdndPosition", xdnd_position_filter },
144 { "XdndStatus", xdnd_status_filter },
145 { "XdndFinished", xdnd_finished_filter },
146 { "XdndDrop", xdnd_drop_filter },
150 G_DEFINE_TYPE (GdkDragContextX11, gdk_drag_context_x11, GDK_TYPE_DRAG_CONTEXT)
153 gdk_drag_context_x11_init (GdkDragContextX11 *context)
155 contexts = g_list_prepend (contexts, context);
158 static void gdk_drag_context_x11_finalize (GObject *object);
159 static GdkWindow * gdk_drag_context_x11_find_window (GdkDragContext *context,
160 GdkWindow *drag_window,
164 GdkDragProtocol *protocol);
165 static gboolean gdk_drag_context_x11_drag_motion (GdkDragContext *context,
166 GdkWindow *dest_window,
167 GdkDragProtocol protocol,
170 GdkDragAction suggested_action,
171 GdkDragAction possible_actions,
173 static void gdk_drag_context_x11_drag_status (GdkDragContext *context,
174 GdkDragAction action,
176 static void gdk_drag_context_x11_drag_abort (GdkDragContext *context,
178 static void gdk_drag_context_x11_drag_drop (GdkDragContext *context,
180 static void gdk_drag_context_x11_drop_reply (GdkDragContext *context,
183 static void gdk_drag_context_x11_drop_finish (GdkDragContext *context,
186 static gboolean gdk_drag_context_x11_drop_status (GdkDragContext *context);
187 static GdkAtom gdk_drag_context_x11_get_selection (GdkDragContext *context);
190 gdk_drag_context_x11_class_init (GdkDragContextX11Class *klass)
192 GObjectClass *object_class = G_OBJECT_CLASS (klass);
193 GdkDragContextClass *context_class = GDK_DRAG_CONTEXT_CLASS (klass);
195 object_class->finalize = gdk_drag_context_x11_finalize;
197 context_class->find_window = gdk_drag_context_x11_find_window;
198 context_class->drag_status = gdk_drag_context_x11_drag_status;
199 context_class->drag_motion = gdk_drag_context_x11_drag_motion;
200 context_class->drag_abort = gdk_drag_context_x11_drag_abort;
201 context_class->drag_drop = gdk_drag_context_x11_drag_drop;
202 context_class->drop_reply = gdk_drag_context_x11_drop_reply;
203 context_class->drop_finish = gdk_drag_context_x11_drop_finish;
204 context_class->drop_status = gdk_drag_context_x11_drop_status;
205 context_class->get_selection = gdk_drag_context_x11_get_selection;
209 gdk_drag_context_x11_finalize (GObject *object)
211 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (object);
212 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
214 if (context->source_window)
216 if ((context->protocol == GDK_DRAG_PROTO_XDND) && !context->is_source)
217 xdnd_manage_source_filter (context, context->source_window, FALSE);
220 g_slist_free_full (context_x11->window_caches, (GDestroyNotify)gdk_window_cache_destroy);
222 contexts = g_list_remove (contexts, context);
224 G_OBJECT_CLASS (gdk_drag_context_x11_parent_class)->finalize (object);
229 static GdkDragContext *
230 gdk_drag_context_find (GdkDisplay *display,
235 GList *tmp_list = contexts;
236 GdkDragContext *context;
237 GdkDragContextX11 *context_x11;
238 Window context_dest_xid;
242 context = (GdkDragContext *)tmp_list->data;
243 context_x11 = (GdkDragContextX11 *)context;
245 if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
246 (context->dest_window && gdk_window_get_display (context->dest_window) != display))
249 context_dest_xid = context->dest_window
250 ? (context_x11->drop_xid
251 ? context_x11->drop_xid
252 : GDK_WINDOW_XID (context->dest_window))
255 if ((!context->is_source == !is_source) &&
256 ((source_xid == None) || (context->source_window &&
257 (GDK_WINDOW_XID (context->source_window) == source_xid))) &&
258 ((dest_xid == None) || (context_dest_xid == dest_xid)))
261 tmp_list = tmp_list->next;
268 precache_target_list (GdkDragContext *context)
270 if (context->targets)
272 GPtrArray *targets = g_ptr_array_new ();
276 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
277 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
279 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
280 (const gchar **)targets->pdata,
283 for (i =0; i < targets->len; i++)
284 g_free (targets->pdata[i]);
286 g_ptr_array_free (targets, TRUE);
290 /* Utility functions */
293 free_cache_child (GdkCacheChild *child,
297 cairo_region_destroy (child->shape);
299 if (child->shape_selected && display)
301 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
303 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
310 gdk_window_cache_add (GdkWindowCache *cache,
312 gint x, gint y, gint width, gint height,
315 GdkCacheChild *child = g_new (GdkCacheChild, 1);
320 child->width = width;
321 child->height = height;
322 child->mapped = mapped;
323 child->shape_selected = FALSE;
324 child->shape_valid = FALSE;
327 cache->children = g_list_prepend (cache->children, child);
328 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
332 static GdkFilterReturn
333 gdk_window_cache_shape_filter (GdkXEvent *xev,
337 XEvent *xevent = (XEvent *)xev;
338 GdkWindowCache *cache = data;
340 GdkX11Display *display = GDK_X11_DISPLAY (gdk_screen_get_display (cache->screen));
342 if (display->have_shapes &&
343 xevent->type == display->shape_event_base + ShapeNotify)
345 XShapeEvent *xse = (XShapeEvent*)xevent;
348 node = g_hash_table_lookup (cache->child_hash,
349 GUINT_TO_POINTER (xse->window));
352 GdkCacheChild *child = node->data;
353 child->shape_valid = FALSE;
356 cairo_region_destroy (child->shape);
361 return GDK_FILTER_REMOVE;
364 return GDK_FILTER_CONTINUE;
367 static GdkFilterReturn
368 gdk_window_cache_filter (GdkXEvent *xev,
372 XEvent *xevent = (XEvent *)xev;
373 GdkWindowCache *cache = data;
375 switch (xevent->type)
377 case CirculateNotify:
379 case ConfigureNotify:
381 XConfigureEvent *xce = &xevent->xconfigure;
384 node = g_hash_table_lookup (cache->child_hash,
385 GUINT_TO_POINTER (xce->window));
388 GdkCacheChild *child = node->data;
391 child->width = xce->width;
392 child->height = xce->height;
393 if (xce->above == None && (node->next))
395 GList *last = g_list_last (cache->children);
396 cache->children = g_list_remove_link (cache->children, node);
403 GList *above_node = g_hash_table_lookup (cache->child_hash,
404 GUINT_TO_POINTER (xce->above));
405 if (above_node && node->next != above_node)
407 /* Put the window above (before in the list) above_node
409 cache->children = g_list_remove_link (cache->children, node);
410 node->prev = above_node->prev;
412 node->prev->next = node;
414 cache->children = node;
415 node->next = above_node;
416 above_node->prev = node;
424 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
426 if (!g_hash_table_lookup (cache->child_hash,
427 GUINT_TO_POINTER (xcwe->window)))
428 gdk_window_cache_add (cache, xcwe->window,
429 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
435 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
438 node = g_hash_table_lookup (cache->child_hash,
439 GUINT_TO_POINTER (xdwe->window));
442 GdkCacheChild *child = node->data;
444 g_hash_table_remove (cache->child_hash,
445 GUINT_TO_POINTER (xdwe->window));
446 cache->children = g_list_remove_link (cache->children, node);
447 /* window is destroyed, no need to disable ShapeNotify */
448 free_cache_child (child, NULL);
449 g_list_free_1 (node);
455 XMapEvent *xme = &xevent->xmap;
458 node = g_hash_table_lookup (cache->child_hash,
459 GUINT_TO_POINTER (xme->window));
462 GdkCacheChild *child = node->data;
463 child->mapped = TRUE;
471 XMapEvent *xume = &xevent->xmap;
474 node = g_hash_table_lookup (cache->child_hash,
475 GUINT_TO_POINTER (xume->window));
478 GdkCacheChild *child = node->data;
479 child->mapped = FALSE;
484 return GDK_FILTER_CONTINUE;
486 return GDK_FILTER_REMOVE;
489 static GdkWindowCache *
490 gdk_window_cache_new (GdkScreen *screen)
492 XWindowAttributes xwa;
493 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
494 GdkWindow *root_window = gdk_screen_get_root_window (screen);
495 GdkChildInfoX11 *children;
499 GdkWindowCache *result = g_new (GdkWindowCache, 1);
501 result->children = NULL;
502 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
503 result->screen = screen;
505 XGetWindowAttributes (xdisplay, GDK_WINDOW_XID (root_window), &xwa);
506 result->old_event_mask = xwa.your_event_mask;
508 if (G_UNLIKELY (!GDK_X11_DISPLAY (GDK_SCREEN_X11 (screen)->display)->trusted_client))
510 GList *toplevel_windows, *list;
512 gint x, y, width, height;
514 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
515 for (list = toplevel_windows; list; list = list->next) {
516 window = GDK_WINDOW (list->data);
517 gdk_window_get_geometry (window, &x, &y, &width, &height);
518 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
520 gdk_window_is_visible (window));
522 g_list_free (toplevel_windows);
526 XSelectInput (xdisplay, GDK_WINDOW_XID (root_window),
527 result->old_event_mask | SubstructureNotifyMask);
528 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
529 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
531 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
532 GDK_WINDOW_XID (root_window),
534 &children, &nchildren))
537 for (i = 0; i < nchildren ; i++)
539 gdk_window_cache_add (result, children[i].window,
540 children[i].x, children[i].y, children[i].width, children[i].height,
541 children[i].is_mapped);
546 #ifdef HAVE_XCOMPOSITE
548 * Add the composite overlay window to the cache, as this can be a reasonable
549 * Xdnd proxy as well.
550 * This is only done when the screen is composited in order to avoid mapping
551 * the COW. We assume that the CM is using the COW (which is true for pretty
552 * much any CM currently in use).
554 if (gdk_screen_is_composited (screen))
556 cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
557 gdk_window_cache_add (result, cow, 0, 0, gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
558 XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
566 gdk_window_cache_destroy (GdkWindowCache *cache)
568 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
571 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
572 GDK_WINDOW_XID (root_window),
573 cache->old_event_mask);
574 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
575 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
577 display = gdk_screen_get_display (cache->screen);
579 gdk_x11_display_error_trap_push (display);
580 g_list_foreach (cache->children, (GFunc)free_cache_child, display);
581 gdk_x11_display_error_trap_pop_ignored (display);
583 g_list_free (cache->children);
584 g_hash_table_destroy (cache->child_hash);
590 is_pointer_within_shape (GdkDisplay *display,
591 GdkCacheChild *child,
595 if (!child->shape_selected)
597 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
599 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
600 child->shape_selected = TRUE;
602 if (!child->shape_valid)
604 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
605 cairo_region_t *input_shape;
607 child->shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
608 child->xid, ShapeBounding);
610 input_shape = _gdk_x11_xwindow_get_shape (display_x11->xdisplay,
611 child->xid, ShapeInput);
612 if (child->shape && input_shape)
614 cairo_region_intersect (child->shape, input_shape);
615 cairo_region_destroy (input_shape);
617 else if (input_shape)
619 child->shape = input_shape;
623 child->shape_valid = TRUE;
626 return child->shape == NULL ||
627 cairo_region_contains_point (child->shape, x_pos, y_pos);
631 get_client_window_at_coords_recurse (GdkDisplay *display,
633 gboolean is_toplevel,
637 GdkChildInfoX11 *children;
638 unsigned int nchildren;
640 gboolean found_child = FALSE;
641 GdkChildInfoX11 child = { 0, };
642 gboolean has_wm_state = FALSE;
644 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
645 is_toplevel? &has_wm_state : NULL,
646 &children, &nchildren))
656 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
658 GdkChildInfoX11 *cur_child = &children[i];
660 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
661 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
662 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
675 if (child.has_wm_state)
678 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
685 get_client_window_at_coords (GdkWindowCache *cache,
691 Window retval = None;
694 display = gdk_screen_get_display (cache->screen);
696 gdk_x11_display_error_trap_push (display);
698 tmp_list = cache->children;
700 while (tmp_list && !retval)
702 GdkCacheChild *child = tmp_list->data;
704 if ((child->xid != ignore) && (child->mapped))
706 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
707 (y_root >= child->y) && (y_root < child->y + child->height))
709 if (!is_pointer_within_shape (display, child,
713 tmp_list = tmp_list->next;
717 retval = get_client_window_at_coords_recurse (display,
725 tmp_list = tmp_list->next;
728 gdk_x11_display_error_trap_pop_ignored (display);
733 return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
736 /*************************************************************
737 ***************************** MOTIF *************************
738 *************************************************************/
740 /* values used in the message type for Motif DND */
753 /* Values used to specify type of protocol to use */
757 XmDRAG_PREFER_PREREGISTER,
759 XmDRAG_PREFER_DYNAMIC,
761 XmDRAG_PREFER_RECEIVER
764 /* Operation codes */
772 /* Drop site status */
774 XmNO_DROP_SITE = 0x01,
775 XmDROP_SITE_INVALID = 0x02,
776 XmDROP_SITE_VALID = 0x03
779 /* completion status */
787 /* Byte swapping routines. The motif specification leaves it
788 * up to us to save a few bytes in the client messages
790 static gchar local_byte_order = '\0';
792 #ifdef G_ENABLE_DEBUG
794 print_target_list (GList *targets)
798 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
799 g_message ("\t%s", name);
801 targets = targets->next;
804 #endif /* G_ENABLE_DEBUG */
807 init_byte_order (void)
809 guint32 myint = 0x01020304;
810 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
814 card16_to_host (guint16 x, gchar byte_order) {
815 if (byte_order == local_byte_order)
818 return (x << 8) | (x >> 8);
822 card32_to_host (guint32 x, gchar byte_order) {
823 if (byte_order == local_byte_order)
826 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
829 /* Motif packs together fields of varying length into the
830 * client message. We can't rely on accessing these
831 * through data.s[], data.l[], etc, because on some architectures
832 * (i.e., Alpha) these won't be valid for format == 8.
835 #define MOTIF_XCLIENT_BYTE(xevent,i) \
836 (xevent)->xclient.data.b[i]
837 #define MOTIF_XCLIENT_SHORT(xevent,i) \
838 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
839 #define MOTIF_XCLIENT_LONG(xevent,i) \
840 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
842 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
843 #define MOTIF_UNPACK_SHORT(xevent,i) \
844 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
845 #define MOTIF_UNPACK_LONG(xevent,i) \
846 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
848 /***** Dest side ***********/
850 /* Property placed on source windows */
851 typedef struct _MotifDragInitiatorInfo {
853 guint8 protocol_version;
854 guint16 targets_index;
855 guint32 selection_atom;
856 } MotifDragInitiatorInfo;
858 /* Header for target table on the drag window */
859 typedef struct _MotifTargetTableHeader {
861 guchar protocol_version;
864 } MotifTargetTableHeader;
866 /* Property placed on target windows */
867 typedef struct _MotifDragReceiverInfo {
869 guint8 protocol_version;
870 guint8 protocol_style;
872 guint32 proxy_window;
873 guint16 num_drop_sites;
876 } MotifDragReceiverInfo;
878 /* Target table handling */
880 static GdkFilterReturn
881 motif_drag_window_filter (GdkXEvent *xevent,
885 XEvent *xev = (XEvent *)xevent;
886 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
887 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
889 switch (xev->xany.type)
892 display_x11->motif_drag_window = None;
893 display_x11->motif_drag_gdk_window = NULL;
896 if (display_x11->motif_target_lists &&
897 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
898 motif_read_target_table (display);
901 return GDK_FILTER_REMOVE;
905 motif_lookup_drag_window (GdkDisplay *display,
906 Display *lookup_xdisplay)
908 Window retval = None;
909 gulong bytes_after, nitems;
914 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
915 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
917 XA_WINDOW, &type, &format, &nitems, &bytes_after,
920 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
922 retval = *(Window *)data;
924 g_message ("Found drag window %#lx\n", GDK_X11_DISPLAY (display)->motif_drag_window));
933 /* Finds the window where global Motif drag information is stored.
934 * If it doesn't exist and 'create' is TRUE, create one.
937 motif_find_drag_window (GdkDisplay *display,
940 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
942 if (!display_x11->motif_drag_window)
944 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
945 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
947 if (!display_x11->motif_drag_window && create)
949 /* Create a persistant window. (Copied from LessTif) */
951 Display *persistant_xdisplay;
952 XSetWindowAttributes attr;
953 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
954 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
956 XGrabServer (persistant_xdisplay);
958 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
960 if (!display_x11->motif_drag_window)
962 attr.override_redirect = True;
963 attr.event_mask = PropertyChangeMask;
965 display_x11->motif_drag_window =
966 XCreateWindow (persistant_xdisplay,
967 RootWindow (persistant_xdisplay, 0),
968 -100, -100, 10, 10, 0, 0,
969 InputOnly, (Visual *)CopyFromParent,
970 (CWOverrideRedirect | CWEventMask), &attr);
973 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
975 XChangeProperty (persistant_xdisplay,
976 RootWindow (persistant_xdisplay, 0),
977 motif_drag_window_atom, XA_WINDOW,
979 (guchar *)&motif_drag_window_atom, 1);
982 XUngrabServer (persistant_xdisplay);
983 XCloseDisplay (persistant_xdisplay);
986 /* There is a miniscule race condition here if the drag window
987 * gets destroyed exactly now.
989 if (display_x11->motif_drag_window)
991 display_x11->motif_drag_gdk_window =
992 gdk_x11_window_foreign_new_for_display (display, display_x11->motif_drag_window);
993 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
994 motif_drag_window_filter,
999 return display_x11->motif_drag_window;
1003 motif_read_target_table (GdkDisplay *display)
1005 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1006 gulong bytes_after, nitems;
1011 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1013 if (display_x11->motif_target_lists)
1015 for (i=0; i<display_x11->motif_n_target_lists; i++)
1016 g_list_free (display_x11->motif_target_lists[i]);
1018 g_free (display_x11->motif_target_lists);
1019 display_x11->motif_target_lists = NULL;
1020 display_x11->motif_n_target_lists = 0;
1023 if (motif_find_drag_window (display, FALSE))
1026 MotifTargetTableHeader *header = NULL;
1027 guchar *target_bytes = NULL;
1029 gboolean success = FALSE;
1031 gdk_x11_display_error_trap_push (display);
1032 XGetWindowProperty (display_x11->xdisplay,
1033 display_x11->motif_drag_window,
1034 motif_drag_targets_atom,
1035 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1036 motif_drag_targets_atom,
1037 &type, &format, &nitems, &bytes_after,
1040 if (gdk_x11_display_error_trap_pop (display) ||
1041 (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1044 header = (MotifTargetTableHeader *)data;
1046 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1047 header->total_size = card32_to_host (header->total_size, header->byte_order);
1049 gdk_x11_display_error_trap_push (display);
1050 XGetWindowProperty (display_x11->xdisplay,
1051 display_x11->motif_drag_window,
1052 motif_drag_targets_atom,
1053 (sizeof(MotifTargetTableHeader)+3)/4,
1054 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1056 motif_drag_targets_atom, &type, &format, &nitems,
1057 &bytes_after, &target_bytes);
1059 if (gdk_x11_display_error_trap_pop (display) ||
1060 (format != 8) || (bytes_after != 0) ||
1061 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1064 display_x11->motif_n_target_lists = header->n_lists;
1065 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1068 for (i=0; i<header->n_lists; i++)
1073 if (p + sizeof(guint16) - target_bytes > nitems)
1076 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1078 /* We need to make a copy of the targets, since it may
1081 targets = g_new (guint32, n_targets);
1082 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1084 p += sizeof(guint16) + n_targets * sizeof(guint32);
1085 if (p - target_bytes > nitems)
1088 for (j=0; j<n_targets; j++)
1089 display_x11->motif_target_lists[i] =
1090 g_list_prepend (display_x11->motif_target_lists[i],
1091 GUINT_TO_POINTER (card32_to_host (targets[j],
1092 header->byte_order)));
1094 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1104 XFree (target_bytes);
1108 if (display_x11->motif_target_lists)
1110 g_free (display_x11->motif_target_lists);
1111 display_x11->motif_target_lists = NULL;
1112 display_x11->motif_n_target_lists = 0;
1114 g_warning ("Error reading Motif target table\n");
1120 targets_sort_func (gconstpointer a, gconstpointer b)
1122 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1123 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1126 /* Check if given (sorted) list is in the targets table */
1128 motif_target_table_check (GdkDisplay *display,
1131 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1132 GList *tmp_list1, *tmp_list2;
1135 for (i=0; i<display_x11->motif_n_target_lists; i++)
1137 tmp_list1 = display_x11->motif_target_lists[i];
1140 while (tmp_list1 && tmp_list2)
1142 if (tmp_list1->data != tmp_list2->data)
1145 tmp_list1 = tmp_list1->next;
1146 tmp_list2 = tmp_list2->next;
1148 if (!tmp_list1 && !tmp_list2) /* Found it */
1156 motif_add_to_target_table (GdkDisplay *display,
1157 GList *targets) /* targets is list of GdkAtom */
1159 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1160 GList *sorted = NULL;
1165 /* make a sorted copy of the list */
1169 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1170 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1171 targets = targets->next;
1174 /* First check if it is there already */
1176 if (display_x11->motif_target_lists)
1177 index = motif_target_table_check (display, sorted);
1179 /* We need to grab the server while doing this, to ensure
1185 /* We need to make sure that it exists _before_ we grab the
1186 * server, since we can't open a new connection after we
1189 motif_find_drag_window (display, TRUE);
1191 gdk_x11_display_grab (display);
1192 motif_read_target_table (display);
1194 /* Check again, in case it was added in the meantime */
1196 if (display_x11->motif_target_lists)
1197 index = motif_target_table_check (display, sorted);
1201 guint32 total_size = 0;
1205 MotifTargetTableHeader *header;
1207 if (!display_x11->motif_target_lists)
1209 display_x11->motif_target_lists = g_new (GList *, 1);
1210 display_x11->motif_n_target_lists = 1;
1214 display_x11->motif_n_target_lists++;
1215 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1216 sizeof(GList *) * display_x11->motif_n_target_lists);
1218 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1220 index = display_x11->motif_n_target_lists - 1;
1222 total_size = sizeof (MotifTargetTableHeader);
1223 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1224 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1226 data = g_malloc (total_size);
1228 header = (MotifTargetTableHeader *)data;
1229 p = data + sizeof(MotifTargetTableHeader);
1231 header->byte_order = local_byte_order;
1232 header->protocol_version = 0;
1233 header->n_lists = display_x11->motif_n_target_lists;
1234 header->total_size = total_size;
1236 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1238 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1239 guint32 *targets = g_new (guint32, n_targets);
1240 guint32 *p32 = targets;
1242 tmp_list = display_x11->motif_target_lists[i];
1245 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1247 tmp_list = tmp_list->next;
1252 p += sizeof(guint16);
1254 memcpy (p, targets, n_targets * sizeof(guint32));
1257 p += sizeof(guint32) * n_targets;
1261 XChangeProperty (display_x11->xdisplay,
1262 display_x11->motif_drag_window,
1263 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1264 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1268 gdk_x11_display_ungrab (display);
1271 g_list_free (sorted);
1275 /* Translate flags */
1278 motif_dnd_translate_flags (GdkDragContextX11 *context_x11,
1281 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1282 guint recommended_op = flags & 0x000f;
1283 guint possible_ops = (flags & 0x0f0) >> 4;
1285 switch (recommended_op)
1288 context->suggested_action = GDK_ACTION_MOVE;
1291 context->suggested_action = GDK_ACTION_COPY;
1294 context->suggested_action = GDK_ACTION_LINK;
1297 context->suggested_action = GDK_ACTION_COPY;
1301 context->actions = 0;
1302 if (possible_ops & XmDROP_MOVE)
1303 context->actions |= GDK_ACTION_MOVE;
1304 if (possible_ops & XmDROP_COPY)
1305 context->actions |= GDK_ACTION_COPY;
1306 if (possible_ops & XmDROP_LINK)
1307 context->actions |= GDK_ACTION_LINK;
1311 motif_dnd_get_flags (GdkDragContext *context)
1315 switch (context->suggested_action)
1317 case GDK_ACTION_MOVE:
1318 flags = XmDROP_MOVE;
1320 case GDK_ACTION_COPY:
1321 flags = XmDROP_COPY;
1323 case GDK_ACTION_LINK:
1324 flags = XmDROP_LINK;
1327 flags = XmDROP_NOOP;
1331 if (context->actions & GDK_ACTION_MOVE)
1332 flags |= XmDROP_MOVE << 8;
1333 if (context->actions & GDK_ACTION_COPY)
1334 flags |= XmDROP_COPY << 8;
1335 if (context->actions & GDK_ACTION_LINK)
1336 flags |= XmDROP_LINK << 8;
1344 motif_set_targets (GdkDragContextX11 *context_x11)
1346 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1347 MotifDragInitiatorInfo info;
1349 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1351 info.byte_order = local_byte_order;
1352 info.protocol_version = 0;
1353 info.targets_index = motif_add_to_target_table (display, context->targets);
1358 g_snprintf (buf, 20, "_GDK_SELECTION_%d", i);
1360 context_x11->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1361 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), context_x11->motif_selection))
1365 info.selection_atom = context_x11->motif_selection;
1367 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1368 GDK_WINDOW_XID (context->source_window),
1369 context_x11->motif_selection,
1370 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1372 (guchar *)&info, sizeof (info));
1374 context_x11->motif_targets_set = 1;
1378 motif_check_dest (GdkDisplay *display,
1381 gboolean retval = FALSE;
1383 MotifDragReceiverInfo *info;
1386 unsigned long nitems, after;
1387 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1389 gdk_x11_display_error_trap_push (display);
1390 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1391 motif_drag_receiver_info_atom,
1392 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1393 &type, &format, &nitems, &after,
1396 if (gdk_x11_display_error_trap_pop (display) == 0)
1400 info = (MotifDragReceiverInfo *)data;
1402 if ((format == 8) && (nitems == sizeof(*info)))
1404 if ((info->protocol_version == 0) &&
1405 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1406 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1407 (info->protocol_style == XmDRAG_DYNAMIC)))
1413 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1420 return retval ? win : None;
1424 motif_send_enter (GdkDragContextX11 *context_x11,
1427 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1428 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1431 if (!G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
1432 return; /* Motif Dnd requires getting properties on the root window */
1434 xev.xclient.type = ClientMessage;
1435 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1436 xev.xclient.format = 8;
1437 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1439 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1440 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1441 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1442 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1443 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XID (context->source_window);
1445 if (!context_x11->motif_targets_set)
1446 motif_set_targets (context_x11);
1448 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1449 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1451 if (!_gdk_x11_display_send_xevent (display,
1452 GDK_WINDOW_XID (context->dest_window),
1455 g_message ("Send event to %lx failed",
1456 GDK_WINDOW_XID (context->dest_window)));
1460 motif_send_leave (GdkDragContextX11 *context_x11,
1463 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1464 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1467 xev.xclient.type = ClientMessage;
1468 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1469 xev.xclient.format = 8;
1470 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1472 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1473 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1474 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1475 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1476 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1477 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1478 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1480 if (!_gdk_x11_display_send_xevent (display,
1481 GDK_WINDOW_XID (context->dest_window),
1484 g_message ("Send event to %lx failed",
1485 GDK_WINDOW_XID (context->dest_window)));
1489 motif_send_motion (GdkDragContextX11 *context_x11,
1492 GdkDragAction action,
1495 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1496 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1500 xev.xclient.type = ClientMessage;
1501 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1502 xev.xclient.format = 8;
1503 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1505 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1506 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1507 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1508 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1509 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1511 if ((context->suggested_action != context_x11->old_action) ||
1512 (context->actions != context_x11->old_actions))
1514 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1516 /* context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1521 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1523 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1524 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1526 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1530 if (!_gdk_x11_display_send_xevent (display,
1531 GDK_WINDOW_XID (context->dest_window),
1534 g_message ("Send event to %lx failed",
1535 GDK_WINDOW_XID (context->dest_window)));
1541 motif_send_drop (GdkDragContextX11 *context_x11,
1544 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
1545 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1548 xev.xclient.type = ClientMessage;
1549 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1550 xev.xclient.format = 8;
1551 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1553 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1554 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1555 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1556 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1558 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
1559 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
1561 MOTIF_XCLIENT_LONG (&xev, 3) = context_x11->motif_selection;
1562 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XID (context->source_window);
1564 if (!_gdk_x11_display_send_xevent (display,
1565 GDK_WINDOW_XID (context->dest_window),
1568 g_message ("Send event to %lx failed",
1569 GDK_WINDOW_XID (context->dest_window)));
1575 motif_read_initiator_info (GdkDisplay *display,
1576 Window source_window,
1587 MotifDragInitiatorInfo *initiator_info;
1589 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1591 gdk_x11_display_error_trap_push (display);
1592 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1593 0, sizeof(*initiator_info), FALSE,
1594 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1595 &type, &format, &nitems, &bytes_after,
1598 if (gdk_x11_display_error_trap_pop (display) ||
1599 (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) ||
1602 g_warning ("Error reading initiator info\n");
1606 initiator_info = (MotifDragInitiatorInfo *)data;
1608 motif_read_target_table (display);
1610 initiator_info->targets_index =
1611 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1612 initiator_info->selection_atom =
1613 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1615 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1617 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1618 XFree (initiator_info);
1622 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1627 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1628 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1629 tmp_list = tmp_list->prev;
1632 #ifdef G_ENABLE_DEBUG
1633 if (_gdk_debug_flags & GDK_DEBUG_DND)
1634 print_target_list (*targets);
1635 #endif /* G_ENABLE_DEBUG */
1637 *selection = initiator_info->selection_atom;
1639 XFree (initiator_info);
1644 static GdkDragContext *
1645 motif_drag_context_new (GdkWindow *dest_window,
1647 guint32 source_window,
1650 GdkDragContextX11 *context_x11;
1651 GdkDragContext *context;
1652 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
1653 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1655 /* FIXME, current_dest_drag really shouldn't be NULL'd
1656 * if we error below.
1658 if (display_x11->current_dest_drag != NULL)
1660 if (timestamp >= display_x11->current_dest_drag->start_time)
1662 g_object_unref (display_x11->current_dest_drag);
1663 display_x11->current_dest_drag = NULL;
1669 context_x11 = g_object_new (GDK_TYPE_DRAG_CONTEXT_X11, NULL);
1670 context = GDK_DRAG_CONTEXT (context_x11);
1672 context->protocol = GDK_DRAG_PROTO_MOTIF;
1673 context->is_source = FALSE;
1675 context->source_window = gdk_x11_window_lookup_for_display (display, source_window);
1676 if (context->source_window)
1677 g_object_ref (context->source_window);
1680 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
1681 if (!context->source_window)
1683 g_object_unref (context_x11);
1688 context->dest_window = dest_window;
1689 g_object_ref (dest_window);
1690 context->start_time = timestamp;
1692 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1696 &context_x11->motif_selection))
1698 g_object_unref (context_x11);
1706 * The MOTIF drag protocol has no real provisions for distinguishing
1707 * multiple simultaneous drops. If the sources grab the pointer
1708 * when doing drags, that shouldn't happen, in any case. If it
1709 * does, we can't do much except hope for the best.
1712 static GdkFilterReturn
1713 motif_top_level_enter (GdkEvent *event,
1716 guint32 source_window,
1719 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1720 GdkDragContext *new_context;
1722 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1723 flags, timestamp, source_window, atom));
1725 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1727 return GDK_FILTER_REMOVE;
1729 event->dnd.type = GDK_DRAG_ENTER;
1730 event->dnd.context = new_context;
1731 g_object_ref (new_context);
1733 display_x11->current_dest_drag = new_context;
1735 return GDK_FILTER_TRANSLATE;
1738 static GdkFilterReturn
1739 motif_top_level_leave (GdkEvent *event,
1743 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1745 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1748 if ((display_x11->current_dest_drag != NULL) &&
1749 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1750 (timestamp >= display_x11->current_dest_drag->start_time))
1752 event->dnd.type = GDK_DRAG_LEAVE;
1753 /* Pass ownership of context to the event */
1754 event->dnd.context = display_x11->current_dest_drag;
1756 display_x11->current_dest_drag = NULL;
1758 return GDK_FILTER_TRANSLATE;
1761 return GDK_FILTER_REMOVE;
1764 static GdkFilterReturn
1765 motif_motion (GdkEvent *event,
1771 GdkDragContextX11 *context_x11;
1772 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1774 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1775 flags, timestamp, x_root, y_root));
1777 if ((display_x11->current_dest_drag != NULL) &&
1778 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1779 (timestamp >= display_x11->current_dest_drag->start_time))
1781 context_x11 = GDK_DRAG_CONTEXT_X11 (display_x11->current_dest_drag);
1783 event->dnd.type = GDK_DRAG_MOTION;
1784 event->dnd.context = display_x11->current_dest_drag;
1785 g_object_ref (display_x11->current_dest_drag);
1787 event->dnd.time = timestamp;
1789 motif_dnd_translate_flags (context_x11, flags);
1791 event->dnd.x_root = x_root;
1792 event->dnd.y_root = y_root;
1794 context_x11->last_x = x_root;
1795 context_x11->last_y = y_root;
1797 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1799 return GDK_FILTER_TRANSLATE;
1802 return GDK_FILTER_REMOVE;
1805 static GdkFilterReturn
1806 motif_operation_changed (GdkEvent *event,
1810 GdkDragContextX11 *context_x11;
1811 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1812 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1815 if ((display_x11->current_dest_drag != NULL) &&
1816 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1817 (timestamp >= display_x11->current_dest_drag->start_time))
1819 event->dnd.type = GDK_DRAG_MOTION;
1820 event->dnd.send_event = FALSE;
1821 event->dnd.context = display_x11->current_dest_drag;
1822 g_object_ref (display_x11->current_dest_drag);
1824 event->dnd.time = timestamp;
1825 context_x11 = GDK_DRAG_CONTEXT_X11 (display_x11->current_dest_drag);
1827 motif_dnd_translate_flags (context_x11, flags);
1829 event->dnd.x_root = context_x11->last_x;
1830 event->dnd.y_root = context_x11->last_y;
1832 context_x11->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1834 return GDK_FILTER_TRANSLATE;
1837 return GDK_FILTER_REMOVE;
1840 static GdkFilterReturn
1841 motif_drop_start (GdkEvent *event,
1844 guint32 source_window,
1849 GdkDragContext *new_context;
1850 GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_WINDOW_DISPLAY (event->any.window));
1852 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1853 flags, timestamp, x_root, y_root, source_window, atom));
1855 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1857 return GDK_FILTER_REMOVE;
1859 motif_dnd_translate_flags (GDK_DRAG_CONTEXT_X11 (new_context), flags);
1861 event->dnd.type = GDK_DROP_START;
1862 event->dnd.context = new_context;
1863 event->dnd.time = timestamp;
1864 event->dnd.x_root = x_root;
1865 event->dnd.y_root = y_root;
1867 gdk_x11_window_set_user_time (event->any.window, timestamp);
1869 g_object_ref (new_context);
1870 display_x11->current_dest_drag = new_context;
1872 return GDK_FILTER_TRANSLATE;
1875 static GdkFilterReturn
1876 motif_drag_status (GdkEvent *event,
1880 GdkDragContext *context;
1881 GdkDisplay *display;
1884 g_message ("Motif status message: flags %x", flags));
1886 display = gdk_window_get_display (event->any.window);
1888 return GDK_FILTER_REMOVE;
1890 context = gdk_drag_context_find (display, TRUE, GDK_WINDOW_XID (event->any.window), None);
1894 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
1895 if ((context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1896 (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1897 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
1899 event->dnd.type = GDK_DRAG_STATUS;
1900 event->dnd.send_event = FALSE;
1901 event->dnd.context = context;
1902 g_object_ref (context);
1904 event->dnd.time = timestamp;
1906 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1908 switch (flags & 0x000f)
1911 context->action = 0;
1914 context->action = GDK_ACTION_MOVE;
1917 context->action = GDK_ACTION_COPY;
1920 context->action = GDK_ACTION_LINK;
1925 context->action = 0;
1927 return GDK_FILTER_TRANSLATE;
1929 return GDK_FILTER_REMOVE;
1932 static GdkFilterReturn
1933 motif_dnd_filter (GdkXEvent *xev,
1937 XEvent *xevent = (XEvent *)xev;
1942 guint32 source_window;
1944 gint16 x_root, y_root;
1947 if (!event->any.window ||
1948 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1949 return GDK_FILTER_CONTINUE; /* Not for us */
1951 /* First read some fields common to all Motif DND messages */
1953 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1954 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1955 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1957 is_reply = ((reason & 0x80) != 0);
1959 switch (reason & 0x7f)
1961 case XmTOP_LEVEL_ENTER:
1962 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1963 atom = MOTIF_UNPACK_LONG (xevent, 3);
1964 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1965 case XmTOP_LEVEL_LEAVE:
1966 return motif_top_level_leave (event, flags, timestamp);
1969 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1970 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1973 return motif_motion (event, flags, timestamp, x_root, y_root);
1975 return motif_drag_status (event, flags, timestamp);
1977 case XmDROP_SITE_ENTER:
1978 return motif_drag_status (event, flags, timestamp);
1980 case XmDROP_SITE_LEAVE:
1981 return motif_drag_status (event,
1982 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1985 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1986 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1987 atom = MOTIF_UNPACK_LONG (xevent, 3);
1988 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1991 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1994 case XmOPERATION_CHANGED:
1996 return motif_operation_changed (event, flags, timestamp);
1998 return motif_drag_status (event, flags, timestamp);
2001 /* To the best of my knowledge, these next two messages are
2002 * not part of the protocol, though they are defined in
2006 case XmDRAG_DROP_FINISH:
2010 return GDK_FILTER_REMOVE;
2013 /*************************************************************
2014 ***************************** XDND **************************
2015 *************************************************************/
2017 /* Utility functions */
2022 GdkDragAction action;
2023 } xdnd_actions_table[] = {
2024 { "XdndActionCopy", None, GDK_ACTION_COPY },
2025 { "XdndActionMove", None, GDK_ACTION_MOVE },
2026 { "XdndActionLink", None, GDK_ACTION_LINK },
2027 { "XdndActionAsk", None, GDK_ACTION_ASK },
2028 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2031 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
2032 static gboolean xdnd_actions_initialized = FALSE;
2035 xdnd_initialize_actions (void)
2039 xdnd_actions_initialized = TRUE;
2040 for (i = 0; i < xdnd_n_actions; i++)
2041 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2044 static GdkDragAction
2045 xdnd_action_from_atom (GdkDisplay *display,
2054 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2056 if (!xdnd_actions_initialized)
2057 xdnd_initialize_actions();
2059 for (i = 0; i < xdnd_n_actions; i++)
2060 if (atom == xdnd_actions_table[i].atom)
2061 return xdnd_actions_table[i].action;
2067 xdnd_action_to_atom (GdkDisplay *display,
2068 GdkDragAction action)
2072 if (!xdnd_actions_initialized)
2073 xdnd_initialize_actions();
2075 for (i = 0; i < xdnd_n_actions; i++)
2076 if (action == xdnd_actions_table[i].action)
2077 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2084 static GdkFilterReturn
2085 xdnd_status_filter (GdkXEvent *xev,
2089 GdkDisplay *display;
2090 XEvent *xevent = (XEvent *)xev;
2091 guint32 dest_window = xevent->xclient.data.l[0];
2092 guint32 flags = xevent->xclient.data.l[1];
2093 Atom action = xevent->xclient.data.l[4];
2094 GdkDragContext *context;
2096 if (!event->any.window ||
2097 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2098 return GDK_FILTER_CONTINUE; /* Not for us */
2101 g_message ("XdndStatus: dest_window: %#x action: %ld",
2102 dest_window, action));
2104 display = gdk_window_get_display (event->any.window);
2105 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2109 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
2110 if (context_x11->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2111 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2113 event->dnd.send_event = FALSE;
2114 event->dnd.type = GDK_DRAG_STATUS;
2115 event->dnd.context = context;
2116 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2117 g_object_ref (context);
2119 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2120 if (!(action != 0) != !(flags & 1))
2123 g_warning ("Received status event with flags not corresponding to action!\n"));
2127 context->action = xdnd_action_from_atom (display, action);
2129 return GDK_FILTER_TRANSLATE;
2132 return GDK_FILTER_REMOVE;
2135 static GdkFilterReturn
2136 xdnd_finished_filter (GdkXEvent *xev,
2140 GdkDisplay *display;
2141 XEvent *xevent = (XEvent *)xev;
2142 guint32 dest_window = xevent->xclient.data.l[0];
2143 GdkDragContext *context;
2144 GdkDragContextX11 *context_x11;
2146 if (!event->any.window ||
2147 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2148 return GDK_FILTER_CONTINUE; /* Not for us */
2151 g_message ("XdndFinished: dest_window: %#x", dest_window));
2153 display = gdk_window_get_display (event->any.window);
2154 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2158 context_x11 = GDK_DRAG_CONTEXT_X11 (context);
2159 if (context_x11->version == 5)
2160 context_x11->drop_failed = xevent->xclient.data.l[1] == 0;
2162 event->dnd.type = GDK_DROP_FINISHED;
2163 event->dnd.context = context;
2164 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2165 g_object_ref (context);
2167 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2169 return GDK_FILTER_TRANSLATE;
2172 return GDK_FILTER_REMOVE;
2176 xdnd_set_targets (GdkDragContextX11 *context_x11)
2178 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2180 GList *tmp_list = context->targets;
2182 gint n_atoms = g_list_length (context->targets);
2183 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2185 atomlist = g_new (Atom, n_atoms);
2189 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2190 tmp_list = tmp_list->next;
2194 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2195 GDK_WINDOW_XID (context->source_window),
2196 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2197 XA_ATOM, 32, PropModeReplace,
2198 (guchar *)atomlist, n_atoms);
2202 context_x11->xdnd_targets_set = 1;
2206 xdnd_set_actions (GdkDragContextX11 *context_x11)
2208 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2213 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2215 if (!xdnd_actions_initialized)
2216 xdnd_initialize_actions();
2218 actions = context->actions;
2220 for (i = 0; i < xdnd_n_actions; i++)
2222 if (actions & xdnd_actions_table[i].action)
2224 actions &= ~xdnd_actions_table[i].action;
2229 atomlist = g_new (Atom, n_atoms);
2231 actions = context->actions;
2233 for (i = 0; i < xdnd_n_actions; i++)
2235 if (actions & xdnd_actions_table[i].action)
2237 actions &= ~xdnd_actions_table[i].action;
2238 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2243 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2244 GDK_WINDOW_XID (context->source_window),
2245 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2246 XA_ATOM, 32, PropModeReplace,
2247 (guchar *)atomlist, n_atoms);
2251 context_x11->xdnd_actions_set = TRUE;
2252 context_x11->xdnd_actions = context->actions;
2256 send_client_message_async_cb (Window window,
2260 GdkDragContext *context = data;
2262 g_message ("Got async callback for #%lx, success = %d",
2265 /* On failure, we immediately continue with the protocol
2266 * so we don't end up blocking for a timeout
2269 context->dest_window &&
2270 window == GDK_WINDOW_XID (context->dest_window))
2272 GdkEvent *temp_event;
2273 GdkDragContextX11 *context_x11 = data;
2275 g_object_unref (context->dest_window);
2276 context->dest_window = NULL;
2277 context->action = 0;
2279 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
2281 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2282 temp_event->dnd.window = g_object_ref (context->source_window);
2283 temp_event->dnd.send_event = TRUE;
2284 temp_event->dnd.context = g_object_ref (context);
2285 temp_event->dnd.time = GDK_CURRENT_TIME;
2286 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2288 gdk_event_put (temp_event);
2290 gdk_event_free (temp_event);
2293 g_object_unref (context);
2298 gdk_drag_context_get_display (GdkDragContext *context)
2300 if (context->source_window)
2301 return GDK_WINDOW_DISPLAY (context->source_window);
2302 else if (context->dest_window)
2303 return GDK_WINDOW_DISPLAY (context->dest_window);
2305 g_assert_not_reached ();
2310 send_client_message_async (GdkDragContext *context,
2314 XClientMessageEvent *event_send)
2316 GdkDisplay *display = gdk_drag_context_get_display (context);
2318 g_object_ref (context);
2320 _gdk_x11_send_client_message_async (display, window,
2321 propagate, event_mask, event_send,
2322 send_client_message_async_cb, context);
2326 xdnd_send_xevent (GdkDragContextX11 *context_x11,
2331 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2332 GdkDisplay *display = gdk_drag_context_get_display (context);
2336 g_assert (event_send->xany.type == ClientMessage);
2338 /* We short-circuit messages to ourselves */
2339 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2343 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2345 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2346 event_send->xclient.message_type)
2348 GdkEvent *temp_event;
2350 temp_event = gdk_event_new (GDK_NOTHING);
2351 temp_event->any.window = g_object_ref (window);
2353 if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
2355 gdk_event_put (temp_event);
2356 gdk_event_free (temp_event);
2364 xwindow = GDK_WINDOW_XID (window);
2366 if (_gdk_x11_display_is_root_window (display, xwindow))
2367 event_mask = ButtonPressMask;
2371 send_client_message_async (context, xwindow, propagate, event_mask,
2372 &event_send->xclient);
2378 xdnd_send_enter (GdkDragContextX11 *context_x11)
2380 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2381 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->dest_window);
2384 xev.xclient.type = ClientMessage;
2385 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2386 xev.xclient.format = 32;
2387 xev.xclient.window = context_x11->drop_xid
2388 ? context_x11->drop_xid
2389 : GDK_WINDOW_XID (context->dest_window);
2390 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2391 xev.xclient.data.l[1] = (context_x11->version << 24); /* version */
2392 xev.xclient.data.l[2] = 0;
2393 xev.xclient.data.l[3] = 0;
2394 xev.xclient.data.l[4] = 0;
2397 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2398 GDK_WINDOW_XID (context->source_window), context_x11->version));
2399 if (g_list_length (context->targets) > 3)
2401 if (!context_x11->xdnd_targets_set)
2402 xdnd_set_targets (context_x11);
2403 xev.xclient.data.l[1] |= 1;
2407 GList *tmp_list = context->targets;
2412 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2413 GDK_POINTER_TO_ATOM (tmp_list->data));
2414 tmp_list = tmp_list->next;
2419 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2422 g_message ("Send event to %lx failed",
2423 GDK_WINDOW_XID (context->dest_window)));
2424 g_object_unref (context->dest_window);
2425 context->dest_window = NULL;
2430 xdnd_send_leave (GdkDragContextX11 *context_x11)
2432 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2433 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2436 xev.xclient.type = ClientMessage;
2437 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2438 xev.xclient.format = 32;
2439 xev.xclient.window = context_x11->drop_xid
2440 ? context_x11->drop_xid
2441 : GDK_WINDOW_XID (context->dest_window);
2442 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2443 xev.xclient.data.l[1] = 0;
2444 xev.xclient.data.l[2] = 0;
2445 xev.xclient.data.l[3] = 0;
2446 xev.xclient.data.l[4] = 0;
2448 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2451 g_message ("Send event to %lx failed",
2452 GDK_WINDOW_XID (context->dest_window)));
2453 g_object_unref (context->dest_window);
2454 context->dest_window = NULL;
2459 xdnd_send_drop (GdkDragContextX11 *context_x11,
2462 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2463 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2466 xev.xclient.type = ClientMessage;
2467 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2468 xev.xclient.format = 32;
2469 xev.xclient.window = context_x11->drop_xid
2470 ? context_x11->drop_xid
2471 : GDK_WINDOW_XID (context->dest_window);
2472 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2473 xev.xclient.data.l[1] = 0;
2474 xev.xclient.data.l[2] = time;
2475 xev.xclient.data.l[3] = 0;
2476 xev.xclient.data.l[4] = 0;
2478 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2481 g_message ("Send event to %lx failed",
2482 GDK_WINDOW_XID (context->dest_window)));
2483 g_object_unref (context->dest_window);
2484 context->dest_window = NULL;
2489 xdnd_send_motion (GdkDragContextX11 *context_x11,
2492 GdkDragAction action,
2495 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2496 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2499 xev.xclient.type = ClientMessage;
2500 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2501 xev.xclient.format = 32;
2502 xev.xclient.window = context_x11->drop_xid
2503 ? context_x11->drop_xid
2504 : GDK_WINDOW_XID (context->dest_window);
2505 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2506 xev.xclient.data.l[1] = 0;
2507 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2508 xev.xclient.data.l[3] = time;
2509 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2511 if (!xdnd_send_xevent (context_x11, context->dest_window, FALSE, &xev))
2514 g_message ("Send event to %lx failed",
2515 GDK_WINDOW_XID (context->dest_window)));
2516 g_object_unref (context->dest_window);
2517 context->dest_window = NULL;
2519 context_x11->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2523 xdnd_check_dest (GdkDisplay *display,
2525 guint *xdnd_version)
2527 gboolean retval = FALSE;
2530 unsigned long nitems, after;
2535 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2536 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2540 gdk_x11_display_error_trap_push (display);
2541 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2543 1, False, AnyPropertyType,
2544 &type, &format, &nitems, &after,
2549 proxy_data = (Window *)data;
2551 if ((format == 32) && (nitems == 1))
2553 proxy = *proxy_data;
2557 g_warning ("Invalid XdndProxy "
2558 "property on window %ld\n", win));
2563 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2565 1, False, AnyPropertyType,
2566 &type, &format, &nitems, &after,
2567 &data) == Success) &&
2570 version = (Atom *)data;
2572 if ((format == 32) && (nitems == 1))
2577 *xdnd_version = *version;
2581 g_warning ("Invalid XdndAware "
2582 "property on window %ld\n", win));
2588 gdk_x11_display_error_trap_pop_ignored (display);
2590 return retval ? (proxy ? proxy : win) : None;
2596 xdnd_read_actions (GdkDragContextX11 *context_x11)
2598 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
2599 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2602 gulong nitems, after;
2607 context_x11->xdnd_have_actions = FALSE;
2609 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2611 /* Get the XdndActionList, if set */
2613 gdk_x11_display_error_trap_push (display);
2614 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2615 GDK_WINDOW_XID (context->source_window),
2616 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2618 False, XA_ATOM, &type, &format, &nitems,
2619 &after, &data) == Success &&
2622 atoms = (Atom *)data;
2624 context->actions = 0;
2626 for (i = 0; i < nitems; i++)
2627 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2629 context_x11->xdnd_have_actions = TRUE;
2631 #ifdef G_ENABLE_DEBUG
2632 if (_gdk_debug_flags & GDK_DEBUG_DND)
2634 GString *action_str = g_string_new (NULL);
2635 if (context->actions & GDK_ACTION_MOVE)
2636 g_string_append(action_str, "MOVE ");
2637 if (context->actions & GDK_ACTION_COPY)
2638 g_string_append(action_str, "COPY ");
2639 if (context->actions & GDK_ACTION_LINK)
2640 g_string_append(action_str, "LINK ");
2641 if (context->actions & GDK_ACTION_ASK)
2642 g_string_append(action_str, "ASK ");
2644 g_message("Xdnd actions = %s", action_str->str);
2645 g_string_free (action_str, TRUE);
2647 #endif /* G_ENABLE_DEBUG */
2654 gdk_x11_display_error_trap_pop_ignored (display);
2660 GdkDragContext *source_context;
2662 source_context = gdk_drag_context_find (display, TRUE,
2663 GDK_WINDOW_XID (context->source_window),
2664 GDK_WINDOW_XID (context->dest_window));
2668 context->actions = source_context->actions;
2669 context_x11->xdnd_have_actions = TRUE;
2674 /* We have to make sure that the XdndActionList we keep internally
2675 * is up to date with the XdndActionList on the source window
2676 * because we get no notification, because Xdnd wasn't meant
2677 * to continually send actions. So we select on PropertyChangeMask
2678 * and add this filter.
2680 static GdkFilterReturn
2681 xdnd_source_window_filter (GdkXEvent *xev,
2685 XEvent *xevent = (XEvent *)xev;
2686 GdkDragContextX11 *context_x11 = cb_data;
2687 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2689 if ((xevent->xany.type == PropertyNotify) &&
2690 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2692 xdnd_read_actions (context_x11);
2694 return GDK_FILTER_REMOVE;
2697 return GDK_FILTER_CONTINUE;
2701 xdnd_manage_source_filter (GdkDragContext *context,
2703 gboolean add_filter)
2705 if (!GDK_WINDOW_DESTROYED (window) &&
2706 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2708 gdk_x11_display_error_trap_push (GDK_WINDOW_DISPLAY (window));
2712 gdk_window_set_events (window,
2713 gdk_window_get_events (window) |
2714 GDK_PROPERTY_CHANGE_MASK);
2715 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2719 gdk_window_remove_filter (window,
2720 xdnd_source_window_filter,
2722 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2723 * but we might want it for other reasons. (Like
2724 * INCR selection transactions).
2728 gdk_x11_display_error_trap_pop_ignored (GDK_WINDOW_DISPLAY (window));
2733 base_precache_atoms (GdkDisplay *display)
2735 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2737 if (!display_x11->base_dnd_atoms_precached)
2739 static const char *const precache_atoms[] = {
2740 "ENLIGHTENMENT_DESKTOP",
2744 "_MOTIF_DRAG_RECEIVER_INFO"
2747 _gdk_x11_precache_atoms (display,
2748 precache_atoms, G_N_ELEMENTS (precache_atoms));
2750 display_x11->base_dnd_atoms_precached = TRUE;
2755 xdnd_precache_atoms (GdkDisplay *display)
2757 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
2759 if (!display_x11->xdnd_atoms_precached)
2761 static const char *const precache_atoms[] = {
2767 "XdndActionPrivate",
2778 _gdk_x11_precache_atoms (display,
2779 precache_atoms, G_N_ELEMENTS (precache_atoms));
2781 display_x11->xdnd_atoms_precached = TRUE;
2785 static GdkFilterReturn
2786 xdnd_enter_filter (GdkXEvent *xev,
2790 GdkDeviceManager *device_manager;
2791 GdkDisplay *display;
2792 GdkX11Display *display_x11;
2793 XEvent *xevent = (XEvent *)xev;
2794 GdkDragContext *context;
2795 GdkDragContextX11 *context_x11;
2800 gulong nitems, after;
2804 guint32 source_window;
2808 if (!event->any.window ||
2809 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2810 return GDK_FILTER_CONTINUE; /* Not for us */
2812 source_window = xevent->xclient.data.l[0];
2813 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2814 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2816 display = GDK_WINDOW_DISPLAY (event->any.window);
2817 display_x11 = GDK_X11_DISPLAY (display);
2819 xdnd_precache_atoms (display);
2822 g_message ("XdndEnter: source_window: %#x, version: %#x",
2823 source_window, version));
2827 /* Old source ignore */
2828 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2829 return GDK_FILTER_REMOVE;
2832 if (display_x11->current_dest_drag != NULL)
2834 g_object_unref (display_x11->current_dest_drag);
2835 display_x11->current_dest_drag = NULL;
2838 context_x11 = (GdkDragContextX11 *)g_object_new (GDK_TYPE_DRAG_CONTEXT_X11, NULL);
2839 context = (GdkDragContext *)context_x11;
2841 context->protocol = GDK_DRAG_PROTO_XDND;
2842 context_x11->version = version;
2844 /* FIXME: Should extend DnD protocol to have device info */
2845 device_manager = gdk_display_get_device_manager (display);
2846 gdk_drag_context_set_device (context, gdk_device_manager_get_client_pointer (device_manager));
2848 context->source_window = gdk_x11_window_lookup_for_display (display, source_window);
2849 if (context->source_window)
2850 g_object_ref (context->source_window);
2853 context->source_window = gdk_x11_window_foreign_new_for_display (display, source_window);
2854 if (!context->source_window)
2856 g_object_unref (context);
2857 return GDK_FILTER_REMOVE;
2860 context->dest_window = event->any.window;
2861 g_object_ref (context->dest_window);
2863 context->targets = NULL;
2866 gdk_x11_display_error_trap_push (display);
2867 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2869 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2871 False, XA_ATOM, &type, &format, &nitems,
2874 if (gdk_x11_display_error_trap_pop (display) || (format != 32) || (type != XA_ATOM))
2876 g_object_unref (context);
2881 return GDK_FILTER_REMOVE;
2884 atoms = (Atom *)data;
2886 for (i = 0; i < nitems; i++)
2888 g_list_append (context->targets,
2889 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2896 for (i = 0; i < 3; i++)
2897 if (xevent->xclient.data.l[2 + i])
2899 g_list_append (context->targets,
2900 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2901 xevent->xclient.data.l[2 + i])));
2904 #ifdef G_ENABLE_DEBUG
2905 if (_gdk_debug_flags & GDK_DEBUG_DND)
2906 print_target_list (context->targets);
2907 #endif /* G_ENABLE_DEBUG */
2909 xdnd_manage_source_filter (context, context->source_window, TRUE);
2910 xdnd_read_actions (context_x11);
2912 event->dnd.type = GDK_DRAG_ENTER;
2913 event->dnd.context = context;
2914 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2915 g_object_ref (context);
2917 display_x11->current_dest_drag = context;
2919 return GDK_FILTER_TRANSLATE;
2922 static GdkFilterReturn
2923 xdnd_leave_filter (GdkXEvent *xev,
2927 XEvent *xevent = (XEvent *)xev;
2928 guint32 source_window = xevent->xclient.data.l[0];
2929 GdkDisplay *display;
2930 GdkX11Display *display_x11;
2932 if (!event->any.window ||
2933 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2934 return GDK_FILTER_CONTINUE; /* Not for us */
2937 g_message ("XdndLeave: source_window: %#x",
2940 display = GDK_WINDOW_DISPLAY (event->any.window);
2941 display_x11 = GDK_X11_DISPLAY (display);
2943 xdnd_precache_atoms (display);
2945 if ((display_x11->current_dest_drag != NULL) &&
2946 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2947 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
2949 event->dnd.type = GDK_DRAG_LEAVE;
2950 /* Pass ownership of context to the event */
2951 event->dnd.context = display_x11->current_dest_drag;
2952 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
2954 display_x11->current_dest_drag = NULL;
2956 return GDK_FILTER_TRANSLATE;
2959 return GDK_FILTER_REMOVE;
2962 static GdkFilterReturn
2963 xdnd_position_filter (GdkXEvent *xev,
2967 XEvent *xevent = (XEvent *)xev;
2968 guint32 source_window = xevent->xclient.data.l[0];
2969 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2970 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2971 guint32 time = xevent->xclient.data.l[3];
2972 Atom action = xevent->xclient.data.l[4];
2974 GdkDisplay *display;
2975 GdkX11Display *display_x11;
2976 GdkDragContext *context;
2977 GdkDragContextX11 *context_x11;
2979 if (!event->any.window ||
2980 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2981 return GDK_FILTER_CONTINUE; /* Not for us */
2984 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2985 source_window, x_root, y_root, time, action));
2987 display = GDK_WINDOW_DISPLAY (event->any.window);
2988 display_x11 = GDK_X11_DISPLAY (display);
2990 xdnd_precache_atoms (display);
2992 context = display_x11->current_dest_drag;
2994 if ((context != NULL) &&
2995 (context->protocol == GDK_DRAG_PROTO_XDND) &&
2996 (GDK_WINDOW_XID (context->source_window) == source_window))
2998 context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3000 event->dnd.type = GDK_DRAG_MOTION;
3001 event->dnd.context = context;
3002 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3003 g_object_ref (context);
3005 event->dnd.time = time;
3007 context->suggested_action = xdnd_action_from_atom (display, action);
3009 if (!context_x11->xdnd_have_actions)
3010 context->actions = context->suggested_action;
3012 event->dnd.x_root = x_root;
3013 event->dnd.y_root = y_root;
3015 context_x11->last_x = x_root;
3016 context_x11->last_y = y_root;
3018 return GDK_FILTER_TRANSLATE;
3021 return GDK_FILTER_REMOVE;
3024 static GdkFilterReturn
3025 xdnd_drop_filter (GdkXEvent *xev,
3029 XEvent *xevent = (XEvent *)xev;
3030 guint32 source_window = xevent->xclient.data.l[0];
3031 guint32 time = xevent->xclient.data.l[2];
3032 GdkDisplay *display;
3033 GdkX11Display *display_x11;
3034 GdkDragContext *context;
3035 GdkDragContextX11 *context_x11;
3037 if (!event->any.window ||
3038 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3039 return GDK_FILTER_CONTINUE; /* Not for us */
3042 g_message ("XdndDrop: source_window: %#x time: %d",
3043 source_window, time));
3045 display = GDK_WINDOW_DISPLAY (event->any.window);
3046 display_x11 = GDK_X11_DISPLAY (display);
3048 xdnd_precache_atoms (display);
3050 context = display_x11->current_dest_drag;
3052 if ((context != NULL) &&
3053 (context->protocol == GDK_DRAG_PROTO_XDND) &&
3054 (GDK_WINDOW_XID (context->source_window) == source_window))
3056 context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3057 event->dnd.type = GDK_DROP_START;
3059 event->dnd.context = context;
3060 gdk_event_set_device (event, gdk_drag_context_get_device (context));
3061 g_object_ref (context);
3063 event->dnd.time = time;
3064 event->dnd.x_root = context_x11->last_x;
3065 event->dnd.y_root = context_x11->last_y;
3067 gdk_x11_window_set_user_time (event->any.window, time);
3069 return GDK_FILTER_TRANSLATE;
3072 return GDK_FILTER_REMOVE;
3076 _gdk_x11_display_init_dnd (GdkDisplay *display)
3081 gdk_display_add_client_message_filter (
3083 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
3084 motif_dnd_filter, NULL);
3086 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3088 gdk_display_add_client_message_filter (
3090 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
3091 xdnd_filters[i].func, NULL);
3098 gdk_drag_do_leave (GdkDragContextX11 *context_x11,
3101 GdkDragContext *context = GDK_DRAG_CONTEXT (context_x11);
3103 if (context->dest_window)
3105 switch (context->protocol)
3107 case GDK_DRAG_PROTO_MOTIF:
3108 motif_send_leave (context_x11, time);
3110 case GDK_DRAG_PROTO_XDND:
3111 xdnd_send_leave (context_x11);
3113 case GDK_DRAG_PROTO_ROOTWIN:
3114 case GDK_DRAG_PROTO_NONE:
3119 g_object_unref (context->dest_window);
3120 context->dest_window = NULL;
3125 _gdk_x11_window_drag_begin (GdkWindow *window,
3129 GdkDragContext *context;
3131 context = (GdkDragContext *)g_object_new (GDK_TYPE_DRAG_CONTEXT_X11, NULL);
3133 context->is_source = TRUE;
3134 context->source_window = window;
3135 g_object_ref (window);
3137 context->targets = g_list_copy (targets);
3138 precache_target_list (context);
3140 context->actions = 0;
3142 gdk_drag_context_set_device (context, device);
3148 _gdk_x11_display_get_drag_protocol (GdkDisplay *display,
3149 GdkNativeWindow xid,
3150 GdkDragProtocol *protocol,
3155 GdkNativeWindow retval;
3157 base_precache_atoms (display);
3159 /* Check for a local drag */
3160 window = gdk_x11_window_lookup_for_display (display, xid);
3161 if (window && gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3163 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3165 *protocol = GDK_DRAG_PROTO_XDND;
3167 xdnd_precache_atoms (display);
3168 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3171 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3173 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3174 GDK_NOTE (DND, g_message ("Entering root window\n"));
3178 else if ((retval = xdnd_check_dest (display, xid, version)))
3180 *protocol = GDK_DRAG_PROTO_XDND;
3181 xdnd_precache_atoms (display);
3182 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3185 else if ((retval = motif_check_dest (display, xid)))
3187 *protocol = GDK_DRAG_PROTO_MOTIF;
3188 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3193 /* Check if this is a root window */
3194 gboolean rootwin = FALSE;
3196 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3201 GDK_NOTE (DND, g_message ("Entering root window\n"));
3202 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3207 *protocol = GDK_DRAG_PROTO_NONE;
3209 return 0; /* a.k.a. None */
3212 static GdkWindowCache *
3213 drag_context_find_window_cache (GdkDragContextX11 *context_x11,
3217 GdkWindowCache *cache;
3219 for (list = context_x11->window_caches; list; list = list->next)
3222 if (cache->screen == screen)
3226 cache = gdk_window_cache_new (screen);
3227 context_x11->window_caches = g_slist_prepend (context_x11->window_caches, cache);
3233 gdk_drag_context_x11_find_window (GdkDragContext *context,
3234 GdkWindow *drag_window,
3238 GdkDragProtocol *protocol)
3240 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3241 GdkWindowCache *window_cache;
3242 GdkDisplay *display;
3244 GdkWindow *dest_window;
3246 display = GDK_WINDOW_DISPLAY (context->source_window);
3248 window_cache = drag_context_find_window_cache (context_x11, screen);
3250 dest = get_client_window_at_coords (window_cache,
3251 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3252 GDK_WINDOW_XID (drag_window) : None,
3255 if (context_x11->dest_xid != dest)
3258 context_x11->dest_xid = dest;
3260 /* Check if new destination accepts drags, and which protocol */
3262 /* There is some ugliness here. We actually need to pass
3263 * _three_ pieces of information to drag_motion - dest_window,
3264 * protocol, and the XID of the unproxied window. The first
3265 * two are passed explicitely, the third implicitly through
3266 * protocol->dest_xid.
3268 recipient = _gdk_x11_display_get_drag_protocol (display,
3271 &context_x11->version);
3273 if (recipient != None)
3275 dest_window = gdk_x11_window_lookup_for_display (display, recipient);
3277 g_object_ref (dest_window);
3279 dest_window = gdk_x11_window_foreign_new_for_display (display, recipient);
3286 dest_window = context->dest_window;
3288 g_object_ref (dest_window);
3289 *protocol = context->protocol;
3296 gdk_drag_context_x11_drag_motion (GdkDragContext *context,
3297 GdkWindow *dest_window,
3298 GdkDragProtocol protocol,
3301 GdkDragAction suggested_action,
3302 GdkDragAction possible_actions,
3305 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3307 context_x11->old_actions = context->actions;
3308 context->actions = possible_actions;
3310 if (context_x11->old_actions != possible_actions)
3311 context_x11->xdnd_actions_set = FALSE;
3313 if (protocol == GDK_DRAG_PROTO_XDND && context_x11->version == 0)
3315 /* This ugly hack is necessary since GTK+ doesn't know about
3316 * the XDND protocol version, and in particular doesn't know
3317 * that gdk_drag_find_window_for_screen() has the side-effect
3318 * of setting context_x11->version, and therefore sometimes call
3319 * gdk_drag_motion() without a prior call to
3320 * gdk_drag_find_window_for_screen(). This happens, e.g.
3321 * when GTK+ is proxying DND events to embedded windows.
3325 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3327 xdnd_check_dest (display,
3328 GDK_WINDOW_XID (dest_window),
3329 &context_x11->version);
3333 /* When we have a Xdnd target, make sure our XdndActionList
3334 * matches the current actions;
3336 if (protocol == GDK_DRAG_PROTO_XDND && !context_x11->xdnd_actions_set)
3340 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3341 xdnd_set_actions (context_x11);
3342 else if (context->dest_window == dest_window)
3344 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3345 GdkDragContext *dest_context;
3347 dest_context = gdk_drag_context_find (display, FALSE,
3348 GDK_WINDOW_XID (context->source_window),
3349 GDK_WINDOW_XID (dest_window));
3353 dest_context->actions = context->actions;
3354 GDK_DRAG_CONTEXT_X11 (dest_context)->xdnd_have_actions = TRUE;
3360 if (context->dest_window != dest_window)
3362 GdkEvent *temp_event;
3364 /* Send a leave to the last destination */
3365 gdk_drag_do_leave (context_x11, time);
3366 context_x11->drag_status = GDK_DRAG_STATUS_DRAG;
3368 /* Check if new destination accepts drags, and which protocol */
3372 context->dest_window = dest_window;
3373 context_x11->drop_xid = context_x11->dest_xid;
3374 g_object_ref (context->dest_window);
3375 context->protocol = protocol;
3379 case GDK_DRAG_PROTO_MOTIF:
3380 motif_send_enter (context_x11, time);
3383 case GDK_DRAG_PROTO_XDND:
3384 xdnd_send_enter (context_x11);
3387 case GDK_DRAG_PROTO_ROOTWIN:
3388 case GDK_DRAG_PROTO_NONE:
3392 context_x11->old_action = suggested_action;
3393 context->suggested_action = suggested_action;
3394 context_x11->old_actions = possible_actions;
3398 context->dest_window = NULL;
3399 context_x11->drop_xid = None;
3400 context->action = 0;
3403 /* Push a status event, to let the client know that
3406 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3407 temp_event->dnd.window = g_object_ref (context->source_window);
3408 /* We use this to signal a synthetic status. Perhaps
3409 * we should use an extra field...
3411 temp_event->dnd.send_event = TRUE;
3413 temp_event->dnd.context = g_object_ref (context);
3414 temp_event->dnd.time = time;
3415 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3417 gdk_event_put (temp_event);
3418 gdk_event_free (temp_event);
3422 context_x11->old_action = context->suggested_action;
3423 context->suggested_action = suggested_action;
3426 /* Send a drag-motion event */
3428 context_x11->last_x = x_root;
3429 context_x11->last_y = y_root;
3431 if (context->dest_window)
3433 if (context_x11->drag_status == GDK_DRAG_STATUS_DRAG)
3435 switch (context->protocol)
3437 case GDK_DRAG_PROTO_MOTIF:
3438 motif_send_motion (context_x11, x_root, y_root, suggested_action, time);
3441 case GDK_DRAG_PROTO_XDND:
3442 xdnd_send_motion (context_x11, x_root, y_root, suggested_action, time);
3445 case GDK_DRAG_PROTO_ROOTWIN:
3447 GdkEvent *temp_event;
3448 /* GTK+ traditionally has used application/x-rootwin-drop,
3449 * but the XDND spec specifies x-rootwindow-drop.
3451 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3452 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3454 if (g_list_find (context->targets,
3455 GDK_ATOM_TO_POINTER (target1)) ||
3456 g_list_find (context->targets,
3457 GDK_ATOM_TO_POINTER (target2)))
3458 context->action = context->suggested_action;
3460 context->action = 0;
3462 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3463 temp_event->dnd.window = g_object_ref (context->source_window);
3464 temp_event->dnd.send_event = FALSE;
3465 temp_event->dnd.context = g_object_ref (context);
3466 temp_event->dnd.time = time;
3467 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3469 gdk_event_put (temp_event);
3470 gdk_event_free (temp_event);
3473 case GDK_DRAG_PROTO_NONE:
3474 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3488 gdk_drag_context_x11_drag_abort (GdkDragContext *context,
3491 gdk_drag_do_leave (GDK_DRAG_CONTEXT_X11 (context), time);
3495 gdk_drag_context_x11_drag_drop (GdkDragContext *context,
3498 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3500 if (context->dest_window)
3502 switch (context->protocol)
3504 case GDK_DRAG_PROTO_MOTIF:
3505 motif_send_leave (context_x11, time);
3506 motif_send_drop (context_x11, time);
3509 case GDK_DRAG_PROTO_XDND:
3510 xdnd_send_drop (context_x11, time);
3513 case GDK_DRAG_PROTO_ROOTWIN:
3514 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3516 case GDK_DRAG_PROTO_NONE:
3517 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3525 /* Destination side */
3528 gdk_drag_context_x11_drag_status (GdkDragContext *context,
3529 GdkDragAction action,
3532 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3534 GdkDisplay *display;
3536 display = GDK_WINDOW_DISPLAY (context->source_window);
3538 context->action = action;
3540 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3542 gboolean need_coords = FALSE;
3544 xev.xclient.type = ClientMessage;
3545 xev.xclient.message_type =
3546 gdk_x11_get_xatom_by_name_for_display (display,
3547 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3548 xev.xclient.format = 8;
3549 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3551 if (context_x11->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3553 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3557 if ((action != 0) != (context_x11->old_action != 0))
3561 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3566 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3571 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3576 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3580 case GDK_ACTION_MOVE:
3581 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3583 case GDK_ACTION_COPY:
3584 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3586 case GDK_ACTION_LINK:
3587 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3590 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3595 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3597 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3599 MOTIF_XCLIENT_LONG (&xev, 1) = time_;
3603 MOTIF_XCLIENT_SHORT (&xev, 4) = context_x11->last_x;
3604 MOTIF_XCLIENT_SHORT (&xev, 5) = context_x11->last_y;
3607 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3609 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3610 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3612 if (!_gdk_x11_display_send_xevent (display,
3613 GDK_WINDOW_XID (context->source_window),
3616 g_message ("Send event to %lx failed",
3617 GDK_WINDOW_XID (context->source_window)));
3619 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3621 xev.xclient.type = ClientMessage;
3622 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3623 xev.xclient.format = 32;
3624 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3626 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3627 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3628 xev.xclient.data.l[2] = 0;
3629 xev.xclient.data.l[3] = 0;
3630 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3631 if (!xdnd_send_xevent (context_x11, context->source_window, FALSE, &xev))
3633 g_message ("Send event to %lx failed",
3634 GDK_WINDOW_XID (context->source_window)));
3637 context_x11->old_action = action;
3641 gdk_drag_context_x11_drop_reply (GdkDragContext *context,
3645 GdkDragContextX11 *context_x11 = GDK_DRAG_CONTEXT_X11 (context);
3647 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3649 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3652 xev.xclient.type = ClientMessage;
3653 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3654 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3655 xev.xclient.format = 8;
3657 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3658 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3660 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3661 (XmDROP_SITE_VALID << 4) |
3662 (XmDROP_NOOP << 8) |
3665 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3666 (XmNO_DROP_SITE << 4) |
3667 (XmDROP_NOOP << 8) |
3668 (XmDROP_CANCEL << 12);
3669 MOTIF_XCLIENT_SHORT (&xev, 2) = context_x11->last_x;
3670 MOTIF_XCLIENT_SHORT (&xev, 3) = context_x11->last_y;
3671 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3672 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3673 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3675 _gdk_x11_display_send_xevent (display,
3676 GDK_WINDOW_XID (context->source_window),
3682 gdk_drag_context_x11_drop_finish (GdkDragContext *context,
3686 if (context->protocol == GDK_DRAG_PROTO_XDND)
3688 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3691 xev.xclient.type = ClientMessage;
3692 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3693 xev.xclient.format = 32;
3694 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3696 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3699 xev.xclient.data.l[1] = 1;
3700 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3705 xev.xclient.data.l[1] = 0;
3706 xev.xclient.data.l[2] = None;
3708 xev.xclient.data.l[3] = 0;
3709 xev.xclient.data.l[4] = 0;
3711 if (!xdnd_send_xevent (GDK_DRAG_CONTEXT_X11 (context), context->source_window, FALSE, &xev))
3713 g_message ("Send event to %lx failed",
3714 GDK_WINDOW_XID (context->source_window)));
3719 _gdk_x11_window_register_dnd (GdkWindow *window)
3721 static const gulong xdnd_version = 5;
3722 MotifDragReceiverInfo info;
3723 Atom motif_drag_receiver_info_atom;
3724 GdkDisplay *display = gdk_window_get_display (window);
3726 g_return_if_fail (window != NULL);
3728 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
3731 base_precache_atoms (display);
3733 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3736 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3738 /* Set Motif drag receiver information property */
3740 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3741 "_MOTIF_DRAG_RECEIVER_INFO");
3742 /* initialize to zero to avoid writing uninitialized data to socket */
3743 memset(&info, 0, sizeof(info));
3744 info.byte_order = local_byte_order;
3745 info.protocol_version = 0;
3746 info.protocol_style = XmDRAG_DYNAMIC;
3747 info.proxy_window = None;
3748 info.num_drop_sites = 0;
3749 info.total_size = sizeof(info);
3751 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
3752 motif_drag_receiver_info_atom,
3753 motif_drag_receiver_info_atom,
3760 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3761 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3762 GDK_WINDOW_XID (window),
3763 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3764 XA_ATOM, 32, PropModeReplace,
3765 (guchar *)&xdnd_version, 1);
3769 gdk_drag_context_x11_get_selection (GdkDragContext *context)
3771 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3772 return gdk_x11_xatom_to_atom_for_display (GDK_WINDOW_DISPLAY (context->source_window),
3773 (GDK_DRAG_CONTEXT_X11 (context))->motif_selection);
3774 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3775 return gdk_atom_intern_static_string ("XdndSelection");
3781 gdk_drag_context_x11_drop_status (GdkDragContext *context)
3783 return ! GDK_DRAG_CONTEXT_X11 (context)->drop_failed;