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/.
34 #include "gdkproperty.h"
35 #include "gdkprivate-x11.h"
36 #include "gdkinternals.h"
37 #include "gdkscreen-x11.h"
38 #include "gdkdisplay-x11.h"
41 #include <X11/Xutil.h>
42 #include <X11/Xatom.h>
43 #include <X11/extensions/shape.h>
44 #include <X11/extensions/Xcomposite.h>
48 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
52 GDK_DRAG_STATUS_MOTION_WAIT,
53 GDK_DRAG_STATUS_ACTION_WAIT,
59 gint x, y, width, height;
61 gboolean shape_selected;
63 cairo_region_t *shape;
68 GHashTable *child_hash;
73 /* Structure that holds information about a drag in progress.
74 * this is used on both source and destination sides.
76 struct _GdkDragContextPrivateX11 {
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;
103 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
105 /* Forward declarations */
107 static void gdk_window_cache_destroy (GdkWindowCache *cache);
109 static void motif_read_target_table (GdkDisplay *display);
111 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
115 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
118 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
121 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
124 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
127 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
130 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
134 static void xdnd_manage_source_filter (GdkDragContext *context,
136 gboolean add_filter);
138 static void gdk_drag_context_finalize (GObject *object);
140 static GList *contexts;
142 static const struct {
143 const char *atom_name;
146 { "XdndEnter", xdnd_enter_filter },
147 { "XdndLeave", xdnd_leave_filter },
148 { "XdndPosition", xdnd_position_filter },
149 { "XdndStatus", xdnd_status_filter },
150 { "XdndFinished", xdnd_finished_filter },
151 { "XdndDrop", xdnd_drop_filter },
154 G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
157 gdk_drag_context_init (GdkDragContext *dragcontext)
159 GdkDragContextPrivateX11 *private;
161 private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext,
162 GDK_TYPE_DRAG_CONTEXT,
163 GdkDragContextPrivateX11);
165 dragcontext->windowing_data = private;
167 contexts = g_list_prepend (contexts, dragcontext);
171 gdk_drag_context_class_init (GdkDragContextClass *klass)
173 GObjectClass *object_class = G_OBJECT_CLASS (klass);
175 object_class->finalize = gdk_drag_context_finalize;
177 g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateX11));
181 gdk_drag_context_finalize (GObject *object)
183 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
184 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
187 g_list_free (context->targets);
189 if (context->source_window)
191 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
193 xdnd_manage_source_filter (context, context->source_window, FALSE);
195 g_object_unref (context->source_window);
198 if (context->dest_window)
199 g_object_unref (context->dest_window);
201 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
202 gdk_window_cache_destroy (tmp_list->data);
203 g_slist_free (private->window_caches);
205 contexts = g_list_remove (contexts, context);
207 G_OBJECT_CLASS (gdk_drag_context_parent_class)->finalize (object);
213 * gdk_drag_context_new:
215 * Creates a new #GdkDragContext.
217 * Return value: the newly created #GdkDragContext.
220 gdk_drag_context_new (void)
222 return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
226 * gdk_drag_context_set_device:
227 * @context: a #GdkDragContext
228 * @device: a #GdkDevice
230 * Associates a #GdkDevice to @context, so all Drag and Drop events
231 * for @context are emitted as if they came from this device.
234 gdk_drag_context_set_device (GdkDragContext *context,
237 GdkDragContextPrivateX11 *private;
239 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
240 g_return_if_fail (GDK_IS_DEVICE (device));
242 private = PRIVATE_DATA (context);
246 g_object_unref (private->device);
247 private->device = NULL;
251 private->device = g_object_ref (device);
255 * gdk_drag_context_get_device:
256 * @context: a #GdkDragContext
258 * Returns the #GdkDevice associated to the drag context.
260 * Returns: (transfer none): The #GdkDevice associated to @context.
263 gdk_drag_context_get_device (GdkDragContext *context)
265 GdkDragContextPrivateX11 *private;
267 g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
269 private = PRIVATE_DATA (context);
271 return private->device;
274 static GdkDragContext *
275 gdk_drag_context_find (GdkDisplay *display,
280 GList *tmp_list = contexts;
281 GdkDragContext *context;
282 GdkDragContextPrivateX11 *private;
283 Window context_dest_xid;
287 context = (GdkDragContext *)tmp_list->data;
288 private = PRIVATE_DATA (context);
290 if ((context->source_window && gdk_window_get_display (context->source_window) != display) ||
291 (context->dest_window && gdk_window_get_display (context->dest_window) != display))
294 context_dest_xid = context->dest_window ?
297 GDK_WINDOW_XID (context->dest_window)) :
300 if ((!context->is_source == !is_source) &&
301 ((source_xid == None) || (context->source_window &&
302 (GDK_WINDOW_XID (context->source_window) == source_xid))) &&
303 ((dest_xid == None) || (context_dest_xid == dest_xid)))
306 tmp_list = tmp_list->next;
313 precache_target_list (GdkDragContext *context)
315 if (context->targets)
317 GPtrArray *targets = g_ptr_array_new ();
321 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
322 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
324 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
325 (const gchar **)targets->pdata,
328 for (i =0; i < targets->len; i++)
329 g_free (targets->pdata[i]);
331 g_ptr_array_free (targets, TRUE);
335 /* Utility functions */
338 free_cache_child (GdkCacheChild *child,
342 cairo_region_destroy (child->shape);
344 if (child->shape_selected && display)
346 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
348 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
355 gdk_window_cache_add (GdkWindowCache *cache,
357 gint x, gint y, gint width, gint height,
360 GdkCacheChild *child = g_new (GdkCacheChild, 1);
365 child->width = width;
366 child->height = height;
367 child->mapped = mapped;
368 child->shape_selected = FALSE;
369 child->shape_valid = FALSE;
372 cache->children = g_list_prepend (cache->children, child);
373 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
377 static GdkFilterReturn
378 gdk_window_cache_shape_filter (GdkXEvent *xev,
382 XEvent *xevent = (XEvent *)xev;
383 GdkWindowCache *cache = data;
385 GdkDisplayX11 *display = GDK_DISPLAY_X11 (gdk_screen_get_display (cache->screen));
387 if (display->have_shapes &&
388 xevent->type == display->shape_event_base + ShapeNotify)
390 XShapeEvent *xse = (XShapeEvent*)xevent;
393 node = g_hash_table_lookup (cache->child_hash,
394 GUINT_TO_POINTER (xse->window));
397 GdkCacheChild *child = node->data;
398 child->shape_valid = FALSE;
401 cairo_region_destroy (child->shape);
406 return GDK_FILTER_REMOVE;
409 return GDK_FILTER_CONTINUE;
412 static GdkFilterReturn
413 gdk_window_cache_filter (GdkXEvent *xev,
417 XEvent *xevent = (XEvent *)xev;
418 GdkWindowCache *cache = data;
420 switch (xevent->type)
422 case CirculateNotify:
424 case ConfigureNotify:
426 XConfigureEvent *xce = &xevent->xconfigure;
429 node = g_hash_table_lookup (cache->child_hash,
430 GUINT_TO_POINTER (xce->window));
433 GdkCacheChild *child = node->data;
436 child->width = xce->width;
437 child->height = xce->height;
438 if (xce->above == None && (node->next))
440 GList *last = g_list_last (cache->children);
441 cache->children = g_list_remove_link (cache->children, node);
448 GList *above_node = g_hash_table_lookup (cache->child_hash,
449 GUINT_TO_POINTER (xce->above));
450 if (above_node && node->next != above_node)
452 /* Put the window above (before in the list) above_node
454 cache->children = g_list_remove_link (cache->children, node);
455 node->prev = above_node->prev;
457 node->prev->next = node;
459 cache->children = node;
460 node->next = above_node;
461 above_node->prev = node;
469 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
471 if (!g_hash_table_lookup (cache->child_hash,
472 GUINT_TO_POINTER (xcwe->window)))
473 gdk_window_cache_add (cache, xcwe->window,
474 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
480 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
483 node = g_hash_table_lookup (cache->child_hash,
484 GUINT_TO_POINTER (xdwe->window));
487 GdkCacheChild *child = node->data;
489 g_hash_table_remove (cache->child_hash,
490 GUINT_TO_POINTER (xdwe->window));
491 cache->children = g_list_remove_link (cache->children, node);
492 /* window is destroyed, no need to disable ShapeNotify */
493 free_cache_child (child, NULL);
494 g_list_free_1 (node);
500 XMapEvent *xme = &xevent->xmap;
503 node = g_hash_table_lookup (cache->child_hash,
504 GUINT_TO_POINTER (xme->window));
507 GdkCacheChild *child = node->data;
508 child->mapped = TRUE;
516 XMapEvent *xume = &xevent->xmap;
519 node = g_hash_table_lookup (cache->child_hash,
520 GUINT_TO_POINTER (xume->window));
523 GdkCacheChild *child = node->data;
524 child->mapped = FALSE;
529 return GDK_FILTER_CONTINUE;
531 return GDK_FILTER_REMOVE;
534 static GdkWindowCache *
535 gdk_window_cache_new (GdkScreen *screen)
537 XWindowAttributes xwa;
538 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
539 GdkWindow *root_window = gdk_screen_get_root_window (screen);
540 GdkChildInfoX11 *children;
544 GdkWindowCache *result = g_new (GdkWindowCache, 1);
546 result->children = NULL;
547 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
548 result->screen = screen;
550 XGetWindowAttributes (xdisplay, GDK_WINDOW_XID (root_window), &xwa);
551 result->old_event_mask = xwa.your_event_mask;
553 if (G_UNLIKELY (!GDK_DISPLAY_X11 (GDK_SCREEN_X11 (screen)->display)->trusted_client))
555 GList *toplevel_windows, *list;
557 gint x, y, width, height;
559 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
560 for (list = toplevel_windows; list; list = list->next) {
561 window = GDK_WINDOW (list->data);
562 gdk_window_get_geometry (window, &x, &y, &width, &height, NULL);
563 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
565 gdk_window_is_visible (window));
567 g_list_free (toplevel_windows);
571 XSelectInput (xdisplay, GDK_WINDOW_XID (root_window),
572 result->old_event_mask | SubstructureNotifyMask);
573 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
574 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
576 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
577 GDK_WINDOW_XID (root_window),
579 &children, &nchildren))
582 for (i = 0; i < nchildren ; i++)
584 gdk_window_cache_add (result, children[i].window,
585 children[i].x, children[i].y, children[i].width, children[i].height,
586 children[i].is_mapped);
591 #ifdef HAVE_XCOMPOSITE
593 * Add the composite overlay window to the cache, as this can be a reasonable
594 * Xdnd proxy as well.
595 * This is only done when the screen is composited in order to avoid mapping
596 * the COW. We assume that the CM is using the COW (which is true for pretty
597 * much any CM currently in use).
599 if (gdk_screen_is_composited (screen))
601 cow = XCompositeGetOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
602 gdk_window_cache_add (result, cow, 0, 0, gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
603 XCompositeReleaseOverlayWindow (xdisplay, GDK_WINDOW_XID (root_window));
611 gdk_window_cache_destroy (GdkWindowCache *cache)
613 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
615 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
616 GDK_WINDOW_XID (root_window),
617 cache->old_event_mask);
618 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
619 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
621 gdk_error_trap_push ();
623 g_list_foreach (cache->children, (GFunc)free_cache_child,
624 gdk_screen_get_display (cache->screen));
626 gdk_error_trap_pop_ignored ();
628 g_list_free (cache->children);
629 g_hash_table_destroy (cache->child_hash);
635 is_pointer_within_shape (GdkDisplay *display,
636 GdkCacheChild *child,
640 if (!child->shape_selected)
642 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
644 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
645 child->shape_selected = TRUE;
647 if (!child->shape_valid)
649 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
650 cairo_region_t *input_shape;
652 child->shape = _xwindow_get_shape (display_x11->xdisplay,
653 child->xid, ShapeBounding);
655 input_shape = _xwindow_get_shape (display_x11->xdisplay,
656 child->xid, ShapeInput);
657 if (child->shape && input_shape)
659 cairo_region_intersect (child->shape, input_shape);
660 cairo_region_destroy (input_shape);
662 else if (input_shape)
664 child->shape = input_shape;
668 child->shape_valid = TRUE;
671 return child->shape == NULL ||
672 cairo_region_contains_point (child->shape, x_pos, y_pos);
676 get_client_window_at_coords_recurse (GdkDisplay *display,
678 gboolean is_toplevel,
682 GdkChildInfoX11 *children;
683 unsigned int nchildren;
685 gboolean found_child = FALSE;
686 GdkChildInfoX11 child = { 0, };
687 gboolean has_wm_state = FALSE;
689 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
690 is_toplevel? &has_wm_state : NULL,
691 &children, &nchildren))
701 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
703 GdkChildInfoX11 *cur_child = &children[i];
705 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
706 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
707 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
720 if (child.has_wm_state)
723 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
730 get_client_window_at_coords (GdkWindowCache *cache,
736 Window retval = None;
738 gdk_error_trap_push ();
740 tmp_list = cache->children;
742 while (tmp_list && !retval)
744 GdkCacheChild *child = tmp_list->data;
746 if ((child->xid != ignore) && (child->mapped))
748 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
749 (y_root >= child->y) && (y_root < child->y + child->height))
751 GdkDisplay *display = gdk_screen_get_display (cache->screen);
753 if (!is_pointer_within_shape (display, child,
757 tmp_list = tmp_list->next;
761 retval = get_client_window_at_coords_recurse (display,
769 tmp_list = tmp_list->next;
772 gdk_error_trap_pop_ignored ();
777 return GDK_WINDOW_XID (gdk_screen_get_root_window (cache->screen));
780 /*************************************************************
781 ***************************** MOTIF *************************
782 *************************************************************/
784 /* values used in the message type for Motif DND */
797 /* Values used to specify type of protocol to use */
801 XmDRAG_PREFER_PREREGISTER,
803 XmDRAG_PREFER_DYNAMIC,
805 XmDRAG_PREFER_RECEIVER
808 /* Operation codes */
816 /* Drop site status */
818 XmNO_DROP_SITE = 0x01,
819 XmDROP_SITE_INVALID = 0x02,
820 XmDROP_SITE_VALID = 0x03
823 /* completion status */
831 /* Byte swapping routines. The motif specification leaves it
832 * up to us to save a few bytes in the client messages
834 static gchar local_byte_order = '\0';
836 #ifdef G_ENABLE_DEBUG
838 print_target_list (GList *targets)
842 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
843 g_message ("\t%s", name);
845 targets = targets->next;
848 #endif /* G_ENABLE_DEBUG */
851 init_byte_order (void)
853 guint32 myint = 0x01020304;
854 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
858 card16_to_host (guint16 x, gchar byte_order) {
859 if (byte_order == local_byte_order)
862 return (x << 8) | (x >> 8);
866 card32_to_host (guint32 x, gchar byte_order) {
867 if (byte_order == local_byte_order)
870 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
873 /* Motif packs together fields of varying length into the
874 * client message. We can't rely on accessing these
875 * through data.s[], data.l[], etc, because on some architectures
876 * (i.e., Alpha) these won't be valid for format == 8.
879 #define MOTIF_XCLIENT_BYTE(xevent,i) \
880 (xevent)->xclient.data.b[i]
881 #define MOTIF_XCLIENT_SHORT(xevent,i) \
882 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
883 #define MOTIF_XCLIENT_LONG(xevent,i) \
884 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
886 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
887 #define MOTIF_UNPACK_SHORT(xevent,i) \
888 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
889 #define MOTIF_UNPACK_LONG(xevent,i) \
890 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
892 /***** Dest side ***********/
894 /* Property placed on source windows */
895 typedef struct _MotifDragInitiatorInfo {
897 guint8 protocol_version;
898 guint16 targets_index;
899 guint32 selection_atom;
900 } MotifDragInitiatorInfo;
902 /* Header for target table on the drag window */
903 typedef struct _MotifTargetTableHeader {
905 guchar protocol_version;
908 } MotifTargetTableHeader;
910 /* Property placed on target windows */
911 typedef struct _MotifDragReceiverInfo {
913 guint8 protocol_version;
914 guint8 protocol_style;
916 guint32 proxy_window;
917 guint16 num_drop_sites;
920 } MotifDragReceiverInfo;
922 /* Target table handling */
924 static GdkFilterReturn
925 motif_drag_window_filter (GdkXEvent *xevent,
929 XEvent *xev = (XEvent *)xevent;
930 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
931 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
933 switch (xev->xany.type)
936 display_x11->motif_drag_window = None;
937 display_x11->motif_drag_gdk_window = NULL;
940 if (display_x11->motif_target_lists &&
941 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
942 motif_read_target_table (display);
945 return GDK_FILTER_REMOVE;
949 motif_lookup_drag_window (GdkDisplay *display,
950 Display *lookup_xdisplay)
952 Window retval = None;
953 gulong bytes_after, nitems;
958 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
959 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
961 XA_WINDOW, &type, &format, &nitems, &bytes_after,
964 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
966 retval = *(Window *)data;
968 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
977 /* Finds the window where global Motif drag information is stored.
978 * If it doesn't exist and 'create' is TRUE, create one.
981 motif_find_drag_window (GdkDisplay *display,
984 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
986 if (!display_x11->motif_drag_window)
988 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
989 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
991 if (!display_x11->motif_drag_window && create)
993 /* Create a persistant window. (Copied from LessTif) */
995 Display *persistant_xdisplay;
996 XSetWindowAttributes attr;
997 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
998 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
1000 XGrabServer (persistant_xdisplay);
1002 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
1004 if (!display_x11->motif_drag_window)
1006 attr.override_redirect = True;
1007 attr.event_mask = PropertyChangeMask;
1009 display_x11->motif_drag_window =
1010 XCreateWindow (persistant_xdisplay,
1011 RootWindow (persistant_xdisplay, 0),
1012 -100, -100, 10, 10, 0, 0,
1013 InputOnly, (Visual *)CopyFromParent,
1014 (CWOverrideRedirect | CWEventMask), &attr);
1017 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
1019 XChangeProperty (persistant_xdisplay,
1020 RootWindow (persistant_xdisplay, 0),
1021 motif_drag_window_atom, XA_WINDOW,
1022 32, PropModeReplace,
1023 (guchar *)&motif_drag_window_atom, 1);
1026 XUngrabServer (persistant_xdisplay);
1027 XCloseDisplay (persistant_xdisplay);
1030 /* There is a miniscule race condition here if the drag window
1031 * gets destroyed exactly now.
1033 if (display_x11->motif_drag_window)
1035 display_x11->motif_drag_gdk_window =
1036 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
1037 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
1038 motif_drag_window_filter,
1043 return display_x11->motif_drag_window;
1047 motif_read_target_table (GdkDisplay *display)
1049 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1050 gulong bytes_after, nitems;
1055 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1057 if (display_x11->motif_target_lists)
1059 for (i=0; i<display_x11->motif_n_target_lists; i++)
1060 g_list_free (display_x11->motif_target_lists[i]);
1062 g_free (display_x11->motif_target_lists);
1063 display_x11->motif_target_lists = NULL;
1064 display_x11->motif_n_target_lists = 0;
1067 if (motif_find_drag_window (display, FALSE))
1070 MotifTargetTableHeader *header = NULL;
1071 guchar *target_bytes = NULL;
1073 gboolean success = FALSE;
1075 gdk_error_trap_push ();
1076 XGetWindowProperty (display_x11->xdisplay,
1077 display_x11->motif_drag_window,
1078 motif_drag_targets_atom,
1079 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1080 motif_drag_targets_atom,
1081 &type, &format, &nitems, &bytes_after,
1084 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1087 header = (MotifTargetTableHeader *)data;
1089 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1090 header->total_size = card32_to_host (header->total_size, header->byte_order);
1092 gdk_error_trap_push ();
1093 XGetWindowProperty (display_x11->xdisplay,
1094 display_x11->motif_drag_window,
1095 motif_drag_targets_atom,
1096 (sizeof(MotifTargetTableHeader)+3)/4,
1097 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1099 motif_drag_targets_atom, &type, &format, &nitems,
1100 &bytes_after, &target_bytes);
1102 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
1103 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1106 display_x11->motif_n_target_lists = header->n_lists;
1107 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1110 for (i=0; i<header->n_lists; i++)
1115 if (p + sizeof(guint16) - target_bytes > nitems)
1118 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1120 /* We need to make a copy of the targets, since it may
1123 targets = g_new (guint32, n_targets);
1124 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1126 p += sizeof(guint16) + n_targets * sizeof(guint32);
1127 if (p - target_bytes > nitems)
1130 for (j=0; j<n_targets; j++)
1131 display_x11->motif_target_lists[i] =
1132 g_list_prepend (display_x11->motif_target_lists[i],
1133 GUINT_TO_POINTER (card32_to_host (targets[j],
1134 header->byte_order)));
1136 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1146 XFree (target_bytes);
1150 if (display_x11->motif_target_lists)
1152 g_free (display_x11->motif_target_lists);
1153 display_x11->motif_target_lists = NULL;
1154 display_x11->motif_n_target_lists = 0;
1156 g_warning ("Error reading Motif target table\n");
1162 targets_sort_func (gconstpointer a, gconstpointer b)
1164 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1165 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1168 /* Check if given (sorted) list is in the targets table */
1170 motif_target_table_check (GdkDisplay *display,
1173 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1174 GList *tmp_list1, *tmp_list2;
1177 for (i=0; i<display_x11->motif_n_target_lists; i++)
1179 tmp_list1 = display_x11->motif_target_lists[i];
1182 while (tmp_list1 && tmp_list2)
1184 if (tmp_list1->data != tmp_list2->data)
1187 tmp_list1 = tmp_list1->next;
1188 tmp_list2 = tmp_list2->next;
1190 if (!tmp_list1 && !tmp_list2) /* Found it */
1198 motif_add_to_target_table (GdkDisplay *display,
1199 GList *targets) /* targets is list of GdkAtom */
1201 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1202 GList *sorted = NULL;
1207 /* make a sorted copy of the list */
1211 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1212 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1213 targets = targets->next;
1216 /* First check if it is there already */
1218 if (display_x11->motif_target_lists)
1219 index = motif_target_table_check (display, sorted);
1221 /* We need to grab the server while doing this, to ensure
1227 /* We need to make sure that it exists _before_ we grab the
1228 * server, since we can't open a new connection after we
1231 motif_find_drag_window (display, TRUE);
1233 gdk_x11_display_grab (display);
1234 motif_read_target_table (display);
1236 /* Check again, in case it was added in the meantime */
1238 if (display_x11->motif_target_lists)
1239 index = motif_target_table_check (display, sorted);
1243 guint32 total_size = 0;
1247 MotifTargetTableHeader *header;
1249 if (!display_x11->motif_target_lists)
1251 display_x11->motif_target_lists = g_new (GList *, 1);
1252 display_x11->motif_n_target_lists = 1;
1256 display_x11->motif_n_target_lists++;
1257 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1258 sizeof(GList *) * display_x11->motif_n_target_lists);
1260 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1262 index = display_x11->motif_n_target_lists - 1;
1264 total_size = sizeof (MotifTargetTableHeader);
1265 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1266 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1268 data = g_malloc (total_size);
1270 header = (MotifTargetTableHeader *)data;
1271 p = data + sizeof(MotifTargetTableHeader);
1273 header->byte_order = local_byte_order;
1274 header->protocol_version = 0;
1275 header->n_lists = display_x11->motif_n_target_lists;
1276 header->total_size = total_size;
1278 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1280 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1281 guint32 *targets = g_new (guint32, n_targets);
1282 guint32 *p32 = targets;
1284 tmp_list = display_x11->motif_target_lists[i];
1287 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1289 tmp_list = tmp_list->next;
1294 p += sizeof(guint16);
1296 memcpy (p, targets, n_targets * sizeof(guint32));
1299 p += sizeof(guint32) * n_targets;
1303 XChangeProperty (display_x11->xdisplay,
1304 display_x11->motif_drag_window,
1305 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1306 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1310 gdk_x11_display_ungrab (display);
1313 g_list_free (sorted);
1317 /* Translate flags */
1320 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1322 guint recommended_op = flags & 0x000f;
1323 guint possible_ops = (flags & 0x0f0) >> 4;
1325 switch (recommended_op)
1328 context->suggested_action = GDK_ACTION_MOVE;
1331 context->suggested_action = GDK_ACTION_COPY;
1334 context->suggested_action = GDK_ACTION_LINK;
1337 context->suggested_action = GDK_ACTION_COPY;
1341 context->actions = 0;
1342 if (possible_ops & XmDROP_MOVE)
1343 context->actions |= GDK_ACTION_MOVE;
1344 if (possible_ops & XmDROP_COPY)
1345 context->actions |= GDK_ACTION_COPY;
1346 if (possible_ops & XmDROP_LINK)
1347 context->actions |= GDK_ACTION_LINK;
1351 motif_dnd_get_flags (GdkDragContext *context)
1355 switch (context->suggested_action)
1357 case GDK_ACTION_MOVE:
1358 flags = XmDROP_MOVE;
1360 case GDK_ACTION_COPY:
1361 flags = XmDROP_COPY;
1363 case GDK_ACTION_LINK:
1364 flags = XmDROP_LINK;
1367 flags = XmDROP_NOOP;
1371 if (context->actions & GDK_ACTION_MOVE)
1372 flags |= XmDROP_MOVE << 8;
1373 if (context->actions & GDK_ACTION_COPY)
1374 flags |= XmDROP_COPY << 8;
1375 if (context->actions & GDK_ACTION_LINK)
1376 flags |= XmDROP_LINK << 8;
1384 motif_set_targets (GdkDragContext *context)
1386 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1387 MotifDragInitiatorInfo info;
1389 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1391 info.byte_order = local_byte_order;
1392 info.protocol_version = 0;
1394 info.targets_index = motif_add_to_target_table (display, context->targets);
1399 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1401 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1402 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1406 info.selection_atom = private->motif_selection;
1408 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1409 GDK_WINDOW_XID (context->source_window),
1410 private->motif_selection,
1411 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1413 (guchar *)&info, sizeof (info));
1415 private->motif_targets_set = 1;
1419 motif_check_dest (GdkDisplay *display,
1422 gboolean retval = FALSE;
1424 MotifDragReceiverInfo *info;
1427 unsigned long nitems, after;
1428 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1430 gdk_error_trap_push ();
1431 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1432 motif_drag_receiver_info_atom,
1433 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1434 &type, &format, &nitems, &after,
1437 if (gdk_error_trap_pop() == 0)
1441 info = (MotifDragReceiverInfo *)data;
1443 if ((format == 8) && (nitems == sizeof(*info)))
1445 if ((info->protocol_version == 0) &&
1446 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1447 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1448 (info->protocol_style == XmDRAG_DYNAMIC)))
1454 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1461 return retval ? win : None;
1465 motif_send_enter (GdkDragContext *context,
1468 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1469 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1472 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1473 return; /* Motif Dnd requires getting properties on the root window */
1475 xev.xclient.type = ClientMessage;
1476 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1477 xev.xclient.format = 8;
1478 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1480 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1481 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1482 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1483 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1484 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XID (context->source_window);
1486 if (!private->motif_targets_set)
1487 motif_set_targets (context);
1489 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1490 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1492 if (!_gdk_send_xevent (display,
1493 GDK_WINDOW_XID (context->dest_window),
1496 g_message ("Send event to %lx failed",
1497 GDK_WINDOW_XID (context->dest_window)));
1501 motif_send_leave (GdkDragContext *context,
1504 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1507 xev.xclient.type = ClientMessage;
1508 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1509 xev.xclient.format = 8;
1510 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1512 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1513 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1514 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1515 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1516 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1517 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1518 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1520 if (!_gdk_send_xevent (display,
1521 GDK_WINDOW_XID (context->dest_window),
1524 g_message ("Send event to %lx failed",
1525 GDK_WINDOW_XID (context->dest_window)));
1529 motif_send_motion (GdkDragContext *context,
1532 GdkDragAction action,
1535 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1536 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1540 xev.xclient.type = ClientMessage;
1541 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1542 xev.xclient.format = 8;
1543 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1545 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1546 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1547 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1548 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1549 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1551 if ((context->suggested_action != private->old_action) ||
1552 (context->actions != private->old_actions))
1554 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1556 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1561 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1563 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1564 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1566 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1570 if (!_gdk_send_xevent (display,
1571 GDK_WINDOW_XID (context->dest_window),
1574 g_message ("Send event to %lx failed",
1575 GDK_WINDOW_XID (context->dest_window)));
1581 motif_send_drop (GdkDragContext *context, guint32 time)
1583 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1584 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
1587 xev.xclient.type = ClientMessage;
1588 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1589 xev.xclient.format = 8;
1590 xev.xclient.window = GDK_WINDOW_XID (context->dest_window);
1592 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1593 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1594 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1595 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1597 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1598 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1600 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1601 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XID (context->source_window);
1603 if (!_gdk_send_xevent (display,
1604 GDK_WINDOW_XID (context->dest_window),
1607 g_message ("Send event to %lx failed",
1608 GDK_WINDOW_XID (context->dest_window)));
1614 motif_read_initiator_info (GdkDisplay *display,
1615 Window source_window,
1626 MotifDragInitiatorInfo *initiator_info;
1628 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1630 gdk_error_trap_push ();
1631 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1632 0, sizeof(*initiator_info), FALSE,
1633 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1634 &type, &format, &nitems, &bytes_after,
1637 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1639 g_warning ("Error reading initiator info\n");
1643 initiator_info = (MotifDragInitiatorInfo *)data;
1645 motif_read_target_table (display);
1647 initiator_info->targets_index =
1648 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1649 initiator_info->selection_atom =
1650 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1652 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1654 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1655 XFree (initiator_info);
1659 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1664 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1665 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1666 tmp_list = tmp_list->prev;
1669 #ifdef G_ENABLE_DEBUG
1670 if (_gdk_debug_flags & GDK_DEBUG_DND)
1671 print_target_list (*targets);
1672 #endif /* G_ENABLE_DEBUG */
1674 *selection = initiator_info->selection_atom;
1676 XFree (initiator_info);
1681 static GdkDragContext *
1682 motif_drag_context_new (GdkWindow *dest_window,
1684 guint32 source_window,
1687 GdkDragContext *new_context;
1688 GdkDragContextPrivateX11 *private;
1689 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
1690 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1692 /* FIXME, current_dest_drag really shouldn't be NULL'd
1693 * if we error below.
1695 if (display_x11->current_dest_drag != NULL)
1697 if (timestamp >= display_x11->current_dest_drag->start_time)
1699 g_object_unref (display_x11->current_dest_drag);
1700 display_x11->current_dest_drag = NULL;
1706 new_context = gdk_drag_context_new ();
1707 private = PRIVATE_DATA (new_context);
1709 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1710 new_context->is_source = FALSE;
1712 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1713 if (new_context->source_window)
1714 g_object_ref (new_context->source_window);
1717 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1718 if (!new_context->source_window)
1720 g_object_unref (new_context);
1725 new_context->dest_window = dest_window;
1726 g_object_ref (dest_window);
1727 new_context->start_time = timestamp;
1729 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1732 &new_context->targets,
1733 &private->motif_selection))
1735 g_object_unref (new_context);
1743 * The MOTIF drag protocol has no real provisions for distinguishing
1744 * multiple simultaneous drops. If the sources grab the pointer
1745 * when doing drags, that shouldn't happen, in any case. If it
1746 * does, we can't do much except hope for the best.
1749 static GdkFilterReturn
1750 motif_top_level_enter (GdkEvent *event,
1753 guint32 source_window,
1756 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
1757 GdkDragContext *new_context;
1759 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1760 flags, timestamp, source_window, atom));
1762 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1764 return GDK_FILTER_REMOVE;
1766 event->dnd.type = GDK_DRAG_ENTER;
1767 event->dnd.context = new_context;
1768 g_object_ref (new_context);
1770 display_x11->current_dest_drag = new_context;
1772 return GDK_FILTER_TRANSLATE;
1775 static GdkFilterReturn
1776 motif_top_level_leave (GdkEvent *event,
1780 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
1782 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1785 if ((display_x11->current_dest_drag != NULL) &&
1786 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1787 (timestamp >= display_x11->current_dest_drag->start_time))
1789 event->dnd.type = GDK_DRAG_LEAVE;
1790 /* Pass ownership of context to the event */
1791 event->dnd.context = display_x11->current_dest_drag;
1793 display_x11->current_dest_drag = NULL;
1795 return GDK_FILTER_TRANSLATE;
1798 return GDK_FILTER_REMOVE;
1801 static GdkFilterReturn
1802 motif_motion (GdkEvent *event,
1808 GdkDragContextPrivateX11 *private;
1809 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
1811 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1812 flags, timestamp, x_root, y_root));
1814 if ((display_x11->current_dest_drag != NULL) &&
1815 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1816 (timestamp >= display_x11->current_dest_drag->start_time))
1818 private = PRIVATE_DATA (display_x11->current_dest_drag);
1820 event->dnd.type = GDK_DRAG_MOTION;
1821 event->dnd.context = display_x11->current_dest_drag;
1822 g_object_ref (display_x11->current_dest_drag);
1824 event->dnd.time = timestamp;
1826 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1828 event->dnd.x_root = x_root;
1829 event->dnd.y_root = y_root;
1831 private->last_x = x_root;
1832 private->last_y = y_root;
1834 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1836 return GDK_FILTER_TRANSLATE;
1839 return GDK_FILTER_REMOVE;
1842 static GdkFilterReturn
1843 motif_operation_changed (GdkEvent *event,
1847 GdkDragContextPrivateX11 *private;
1848 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
1849 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1852 if ((display_x11->current_dest_drag != NULL) &&
1853 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1854 (timestamp >= display_x11->current_dest_drag->start_time))
1856 event->dnd.type = GDK_DRAG_MOTION;
1857 event->dnd.send_event = FALSE;
1858 event->dnd.context = display_x11->current_dest_drag;
1859 g_object_ref (display_x11->current_dest_drag);
1861 event->dnd.time = timestamp;
1862 private = PRIVATE_DATA (display_x11->current_dest_drag);
1864 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1866 event->dnd.x_root = private->last_x;
1867 event->dnd.y_root = private->last_y;
1869 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1871 return GDK_FILTER_TRANSLATE;
1874 return GDK_FILTER_REMOVE;
1877 static GdkFilterReturn
1878 motif_drop_start (GdkEvent *event,
1881 guint32 source_window,
1886 GdkDragContext *new_context;
1887 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_WINDOW_DISPLAY (event->any.window));
1889 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1890 flags, timestamp, x_root, y_root, source_window, atom));
1892 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1894 return GDK_FILTER_REMOVE;
1896 motif_dnd_translate_flags (new_context, flags);
1898 event->dnd.type = GDK_DROP_START;
1899 event->dnd.context = new_context;
1900 event->dnd.time = timestamp;
1901 event->dnd.x_root = x_root;
1902 event->dnd.y_root = y_root;
1904 gdk_x11_window_set_user_time (event->any.window, timestamp);
1906 g_object_ref (new_context);
1907 display_x11->current_dest_drag = new_context;
1909 return GDK_FILTER_TRANSLATE;
1912 static GdkFilterReturn
1913 motif_drag_status (GdkEvent *event,
1917 GdkDragContext *context;
1918 GdkDisplay *display;
1921 g_message ("Motif status message: flags %x", flags));
1923 display = gdk_window_get_display (event->any.window);
1925 return GDK_FILTER_REMOVE;
1927 context = gdk_drag_context_find (display, TRUE, GDK_WINDOW_XID (event->any.window), None);
1931 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1932 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1933 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1934 private->drag_status = GDK_DRAG_STATUS_DRAG;
1936 event->dnd.type = GDK_DRAG_STATUS;
1937 event->dnd.send_event = FALSE;
1938 event->dnd.context = context;
1939 g_object_ref (context);
1941 event->dnd.time = timestamp;
1943 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1945 switch (flags & 0x000f)
1948 context->action = 0;
1951 context->action = GDK_ACTION_MOVE;
1954 context->action = GDK_ACTION_COPY;
1957 context->action = GDK_ACTION_LINK;
1962 context->action = 0;
1964 return GDK_FILTER_TRANSLATE;
1966 return GDK_FILTER_REMOVE;
1969 static GdkFilterReturn
1970 motif_dnd_filter (GdkXEvent *xev,
1974 XEvent *xevent = (XEvent *)xev;
1979 guint32 source_window;
1981 gint16 x_root, y_root;
1984 if (!event->any.window ||
1985 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1986 return GDK_FILTER_CONTINUE; /* Not for us */
1988 /* First read some fields common to all Motif DND messages */
1990 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1991 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1992 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1994 is_reply = ((reason & 0x80) != 0);
1996 switch (reason & 0x7f)
1998 case XmTOP_LEVEL_ENTER:
1999 source_window = MOTIF_UNPACK_LONG (xevent, 2);
2000 atom = MOTIF_UNPACK_LONG (xevent, 3);
2001 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
2002 case XmTOP_LEVEL_LEAVE:
2003 return motif_top_level_leave (event, flags, timestamp);
2006 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2007 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2010 return motif_motion (event, flags, timestamp, x_root, y_root);
2012 return motif_drag_status (event, flags, timestamp);
2014 case XmDROP_SITE_ENTER:
2015 return motif_drag_status (event, flags, timestamp);
2017 case XmDROP_SITE_LEAVE:
2018 return motif_drag_status (event,
2019 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
2022 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
2023 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
2024 atom = MOTIF_UNPACK_LONG (xevent, 3);
2025 source_window = MOTIF_UNPACK_LONG (xevent, 4);
2028 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
2031 case XmOPERATION_CHANGED:
2033 return motif_operation_changed (event, flags, timestamp);
2035 return motif_drag_status (event, flags, timestamp);
2038 /* To the best of my knowledge, these next two messages are
2039 * not part of the protocol, though they are defined in
2043 case XmDRAG_DROP_FINISH:
2047 return GDK_FILTER_REMOVE;
2050 /*************************************************************
2051 ***************************** XDND **************************
2052 *************************************************************/
2054 /* Utility functions */
2059 GdkDragAction action;
2060 } xdnd_actions_table[] = {
2061 { "XdndActionCopy", None, GDK_ACTION_COPY },
2062 { "XdndActionMove", None, GDK_ACTION_MOVE },
2063 { "XdndActionLink", None, GDK_ACTION_LINK },
2064 { "XdndActionAsk", None, GDK_ACTION_ASK },
2065 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2068 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
2069 static gboolean xdnd_actions_initialized = FALSE;
2072 xdnd_initialize_actions (void)
2076 xdnd_actions_initialized = TRUE;
2077 for (i=0; i < xdnd_n_actions; i++)
2078 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2081 static GdkDragAction
2082 xdnd_action_from_atom (GdkDisplay *display,
2091 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2093 if (!xdnd_actions_initialized)
2094 xdnd_initialize_actions();
2096 for (i=0; i<xdnd_n_actions; i++)
2097 if (atom == xdnd_actions_table[i].atom)
2098 return xdnd_actions_table[i].action;
2104 xdnd_action_to_atom (GdkDisplay *display,
2105 GdkDragAction action)
2109 if (!xdnd_actions_initialized)
2110 xdnd_initialize_actions();
2112 for (i=0; i<xdnd_n_actions; i++)
2113 if (action == xdnd_actions_table[i].action)
2114 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2121 static GdkFilterReturn
2122 xdnd_status_filter (GdkXEvent *xev,
2126 GdkDisplay *display;
2127 XEvent *xevent = (XEvent *)xev;
2128 guint32 dest_window = xevent->xclient.data.l[0];
2129 guint32 flags = xevent->xclient.data.l[1];
2130 Atom action = xevent->xclient.data.l[4];
2131 GdkDragContext *context;
2133 if (!event->any.window ||
2134 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2135 return GDK_FILTER_CONTINUE; /* Not for us */
2138 g_message ("XdndStatus: dest_window: %#x action: %ld",
2139 dest_window, action));
2141 display = gdk_window_get_display (event->any.window);
2142 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2146 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2147 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2148 private->drag_status = GDK_DRAG_STATUS_DRAG;
2150 event->dnd.send_event = FALSE;
2151 event->dnd.type = GDK_DRAG_STATUS;
2152 event->dnd.context = context;
2153 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2154 g_object_ref (context);
2156 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2157 if (!(action != 0) != !(flags & 1))
2160 g_warning ("Received status event with flags not corresponding to action!\n"));
2164 context->action = xdnd_action_from_atom (display, action);
2166 return GDK_FILTER_TRANSLATE;
2169 return GDK_FILTER_REMOVE;
2172 static GdkFilterReturn
2173 xdnd_finished_filter (GdkXEvent *xev,
2177 GdkDisplay *display;
2178 XEvent *xevent = (XEvent *)xev;
2179 guint32 dest_window = xevent->xclient.data.l[0];
2180 GdkDragContext *context;
2181 GdkDragContextPrivateX11 *private;
2183 if (!event->any.window ||
2184 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2185 return GDK_FILTER_CONTINUE; /* Not for us */
2188 g_message ("XdndFinished: dest_window: %#x", dest_window));
2190 display = gdk_window_get_display (event->any.window);
2191 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2195 private = PRIVATE_DATA (context);
2196 if (private->version == 5)
2197 private->drop_failed = xevent->xclient.data.l[1] == 0;
2199 event->dnd.type = GDK_DROP_FINISHED;
2200 event->dnd.context = context;
2201 gdk_event_set_device (event, gdk_drag_context_get_device (context));
2202 g_object_ref (context);
2204 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2206 return GDK_FILTER_TRANSLATE;
2209 return GDK_FILTER_REMOVE;
2213 xdnd_set_targets (GdkDragContext *context)
2215 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2217 GList *tmp_list = context->targets;
2219 gint n_atoms = g_list_length (context->targets);
2220 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2222 atomlist = g_new (Atom, n_atoms);
2226 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2227 tmp_list = tmp_list->next;
2231 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2232 GDK_WINDOW_XID (context->source_window),
2233 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2234 XA_ATOM, 32, PropModeReplace,
2235 (guchar *)atomlist, n_atoms);
2239 private->xdnd_targets_set = 1;
2243 xdnd_set_actions (GdkDragContext *context)
2245 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2250 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2252 if (!xdnd_actions_initialized)
2253 xdnd_initialize_actions();
2255 actions = context->actions;
2257 for (i=0; i<xdnd_n_actions; i++)
2259 if (actions & xdnd_actions_table[i].action)
2261 actions &= ~xdnd_actions_table[i].action;
2266 atomlist = g_new (Atom, n_atoms);
2268 actions = context->actions;
2270 for (i=0; i<xdnd_n_actions; i++)
2272 if (actions & xdnd_actions_table[i].action)
2274 actions &= ~xdnd_actions_table[i].action;
2275 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2280 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2281 GDK_WINDOW_XID (context->source_window),
2282 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2283 XA_ATOM, 32, PropModeReplace,
2284 (guchar *)atomlist, n_atoms);
2288 private->xdnd_actions_set = TRUE;
2289 private->xdnd_actions = context->actions;
2293 send_client_message_async_cb (Window window,
2297 GdkDragContext *context = data;
2299 g_message ("Got async callback for #%lx, success = %d",
2302 /* On failure, we immediately continue with the protocol
2303 * so we don't end up blocking for a timeout
2306 context->dest_window &&
2307 window == GDK_WINDOW_XID (context->dest_window))
2309 GdkEvent *temp_event;
2310 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2312 g_object_unref (context->dest_window);
2313 context->dest_window = NULL;
2314 context->action = 0;
2316 private->drag_status = GDK_DRAG_STATUS_DRAG;
2318 temp_event = gdk_event_new (GDK_DRAG_STATUS);
2319 temp_event->dnd.window = g_object_ref (context->source_window);
2320 temp_event->dnd.send_event = TRUE;
2321 temp_event->dnd.context = g_object_ref (context);
2322 temp_event->dnd.time = GDK_CURRENT_TIME;
2323 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
2325 gdk_event_put (temp_event);
2327 gdk_event_free (temp_event);
2330 g_object_unref (context);
2335 gdk_drag_context_get_display (GdkDragContext *context)
2337 if (context->source_window)
2338 return GDK_WINDOW_DISPLAY (context->source_window);
2339 else if (context->dest_window)
2340 return GDK_WINDOW_DISPLAY (context->dest_window);
2342 g_assert_not_reached ();
2347 send_client_message_async (GdkDragContext *context,
2351 XClientMessageEvent *event_send)
2353 GdkDisplay *display = gdk_drag_context_get_display (context);
2355 g_object_ref (context);
2357 _gdk_x11_send_client_message_async (display, window,
2358 propagate, event_mask, event_send,
2359 send_client_message_async_cb, context);
2363 xdnd_send_xevent (GdkDragContext *context,
2368 GdkDisplay *display = gdk_drag_context_get_display (context);
2372 g_assert (event_send->xany.type == ClientMessage);
2374 /* We short-circuit messages to ourselves */
2375 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2379 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2381 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2382 event_send->xclient.message_type)
2384 GdkEvent *temp_event;
2386 temp_event = gdk_event_new (GDK_NOTHING);
2387 temp_event->any.window = g_object_ref (window);
2389 if ((*xdnd_filters[i].func) (event_send, temp_event, NULL) == GDK_FILTER_TRANSLATE)
2391 gdk_event_put (temp_event);
2392 gdk_event_free (temp_event);
2400 xwindow = GDK_WINDOW_XID (window);
2402 if (_gdk_x11_display_is_root_window (display, xwindow))
2403 event_mask = ButtonPressMask;
2407 send_client_message_async (context, xwindow, propagate, event_mask,
2408 &event_send->xclient);
2414 xdnd_send_enter (GdkDragContext *context)
2417 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2418 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->dest_window);
2420 xev.xclient.type = ClientMessage;
2421 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2422 xev.xclient.format = 32;
2423 xev.xclient.window = private->drop_xid ?
2425 GDK_WINDOW_XID (context->dest_window);
2426 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2427 xev.xclient.data.l[1] = (private->version << 24); /* version */
2428 xev.xclient.data.l[2] = 0;
2429 xev.xclient.data.l[3] = 0;
2430 xev.xclient.data.l[4] = 0;
2433 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2434 GDK_WINDOW_XID (context->source_window), private->version));
2435 if (g_list_length (context->targets) > 3)
2437 if (!private->xdnd_targets_set)
2438 xdnd_set_targets (context);
2439 xev.xclient.data.l[1] |= 1;
2443 GList *tmp_list = context->targets;
2448 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2449 GDK_POINTER_TO_ATOM (tmp_list->data));
2450 tmp_list = tmp_list->next;
2455 if (!xdnd_send_xevent (context, context->dest_window,
2459 g_message ("Send event to %lx failed",
2460 GDK_WINDOW_XID (context->dest_window)));
2461 g_object_unref (context->dest_window);
2462 context->dest_window = NULL;
2467 xdnd_send_leave (GdkDragContext *context)
2469 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2472 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2474 xev.xclient.type = ClientMessage;
2475 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2476 xev.xclient.format = 32;
2477 xev.xclient.window = private->drop_xid ?
2479 GDK_WINDOW_XID (context->dest_window);
2480 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2481 xev.xclient.data.l[1] = 0;
2482 xev.xclient.data.l[2] = 0;
2483 xev.xclient.data.l[3] = 0;
2484 xev.xclient.data.l[4] = 0;
2486 if (!xdnd_send_xevent (context, context->dest_window,
2490 g_message ("Send event to %lx failed",
2491 GDK_WINDOW_XID (context->dest_window)));
2492 g_object_unref (context->dest_window);
2493 context->dest_window = NULL;
2498 xdnd_send_drop (GdkDragContext *context, guint32 time)
2500 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2501 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2504 xev.xclient.type = ClientMessage;
2505 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2506 xev.xclient.format = 32;
2507 xev.xclient.window = private->drop_xid ?
2509 GDK_WINDOW_XID (context->dest_window);
2510 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2511 xev.xclient.data.l[1] = 0;
2512 xev.xclient.data.l[2] = time;
2513 xev.xclient.data.l[3] = 0;
2514 xev.xclient.data.l[4] = 0;
2516 if (!xdnd_send_xevent (context, context->dest_window,
2520 g_message ("Send event to %lx failed",
2521 GDK_WINDOW_XID (context->dest_window)));
2522 g_object_unref (context->dest_window);
2523 context->dest_window = NULL;
2528 xdnd_send_motion (GdkDragContext *context,
2531 GdkDragAction action,
2534 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2535 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2538 xev.xclient.type = ClientMessage;
2539 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2540 xev.xclient.format = 32;
2541 xev.xclient.window = private->drop_xid ?
2543 GDK_WINDOW_XID (context->dest_window);
2544 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->source_window);
2545 xev.xclient.data.l[1] = 0;
2546 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2547 xev.xclient.data.l[3] = time;
2548 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2550 if (!xdnd_send_xevent (context, context->dest_window,
2554 g_message ("Send event to %lx failed",
2555 GDK_WINDOW_XID (context->dest_window)));
2556 g_object_unref (context->dest_window);
2557 context->dest_window = NULL;
2559 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2563 xdnd_check_dest (GdkDisplay *display,
2565 guint *xdnd_version)
2567 gboolean retval = FALSE;
2570 unsigned long nitems, after;
2575 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2576 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2580 gdk_error_trap_push ();
2582 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2584 1, False, AnyPropertyType,
2585 &type, &format, &nitems, &after,
2590 proxy_data = (Window *)data;
2592 if ((format == 32) && (nitems == 1))
2594 proxy = *proxy_data;
2598 g_warning ("Invalid XdndProxy "
2599 "property on window %ld\n", win));
2604 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2606 1, False, AnyPropertyType,
2607 &type, &format, &nitems, &after,
2608 &data) == Success) &&
2611 version = (Atom *)data;
2613 if ((format == 32) && (nitems == 1))
2618 *xdnd_version = *version;
2622 g_warning ("Invalid XdndAware "
2623 "property on window %ld\n", win));
2629 gdk_error_trap_pop_ignored ();
2631 return retval ? (proxy ? proxy : win) : None;
2637 xdnd_read_actions (GdkDragContext *context)
2639 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2642 gulong nitems, after;
2648 PRIVATE_DATA (context)->xdnd_have_actions = FALSE;
2650 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2652 /* Get the XdndActionList, if set */
2654 gdk_error_trap_push ();
2656 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2657 GDK_WINDOW_XID (context->source_window),
2658 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2660 False, XA_ATOM, &type, &format, &nitems,
2661 &after, &data) == Success &&
2664 atoms = (Atom *)data;
2666 context->actions = 0;
2668 for (i=0; i<nitems; i++)
2669 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2671 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2673 #ifdef G_ENABLE_DEBUG
2674 if (_gdk_debug_flags & GDK_DEBUG_DND)
2676 GString *action_str = g_string_new (NULL);
2677 if (context->actions & GDK_ACTION_MOVE)
2678 g_string_append(action_str, "MOVE ");
2679 if (context->actions & GDK_ACTION_COPY)
2680 g_string_append(action_str, "COPY ");
2681 if (context->actions & GDK_ACTION_LINK)
2682 g_string_append(action_str, "LINK ");
2683 if (context->actions & GDK_ACTION_ASK)
2684 g_string_append(action_str, "ASK ");
2686 g_message("Xdnd actions = %s", action_str->str);
2687 g_string_free (action_str, TRUE);
2689 #endif /* G_ENABLE_DEBUG */
2696 gdk_error_trap_pop_ignored ();
2702 GdkDragContext *source_context;
2704 source_context = gdk_drag_context_find (display, TRUE,
2705 GDK_WINDOW_XID (context->source_window),
2706 GDK_WINDOW_XID (context->dest_window));
2710 context->actions = source_context->actions;
2711 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2716 /* We have to make sure that the XdndActionList we keep internally
2717 * is up to date with the XdndActionList on the source window
2718 * because we get no notification, because Xdnd wasn't meant
2719 * to continually send actions. So we select on PropertyChangeMask
2720 * and add this filter.
2722 static GdkFilterReturn
2723 xdnd_source_window_filter (GdkXEvent *xev,
2727 XEvent *xevent = (XEvent *)xev;
2728 GdkDragContext *context = cb_data;
2729 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2731 if ((xevent->xany.type == PropertyNotify) &&
2732 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2734 xdnd_read_actions (context);
2736 return GDK_FILTER_REMOVE;
2739 return GDK_FILTER_CONTINUE;
2743 xdnd_manage_source_filter (GdkDragContext *context,
2745 gboolean add_filter)
2747 if (!GDK_WINDOW_DESTROYED (window) &&
2748 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2750 gdk_error_trap_push ();
2754 gdk_window_set_events (window,
2755 gdk_window_get_events (window) |
2756 GDK_PROPERTY_CHANGE_MASK);
2757 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2761 gdk_window_remove_filter (window,
2762 xdnd_source_window_filter,
2764 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2765 * but we might want it for other reasons. (Like
2766 * INCR selection transactions).
2770 gdk_error_trap_pop_ignored ();
2775 base_precache_atoms (GdkDisplay *display)
2777 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2779 if (!display_x11->base_dnd_atoms_precached)
2781 static const char *const precache_atoms[] = {
2782 "ENLIGHTENMENT_DESKTOP",
2786 "_MOTIF_DRAG_RECEIVER_INFO"
2789 _gdk_x11_precache_atoms (display,
2790 precache_atoms, G_N_ELEMENTS (precache_atoms));
2792 display_x11->base_dnd_atoms_precached = TRUE;
2797 xdnd_precache_atoms (GdkDisplay *display)
2799 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2801 if (!display_x11->xdnd_atoms_precached)
2803 static const char *const precache_atoms[] = {
2809 "XdndActionPrivate",
2820 _gdk_x11_precache_atoms (display,
2821 precache_atoms, G_N_ELEMENTS (precache_atoms));
2823 display_x11->xdnd_atoms_precached = TRUE;
2827 static GdkFilterReturn
2828 xdnd_enter_filter (GdkXEvent *xev,
2832 GdkDeviceManager *device_manager;
2833 GdkDisplay *display;
2834 GdkDisplayX11 *display_x11;
2835 XEvent *xevent = (XEvent *)xev;
2836 GdkDragContext *new_context;
2841 gulong nitems, after;
2845 guint32 source_window;
2849 if (!event->any.window ||
2850 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2851 return GDK_FILTER_CONTINUE; /* Not for us */
2853 source_window = xevent->xclient.data.l[0];
2854 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2855 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2857 display = GDK_WINDOW_DISPLAY (event->any.window);
2858 display_x11 = GDK_DISPLAY_X11 (display);
2860 xdnd_precache_atoms (display);
2863 g_message ("XdndEnter: source_window: %#x, version: %#x",
2864 source_window, version));
2868 /* Old source ignore */
2869 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2870 return GDK_FILTER_REMOVE;
2873 if (display_x11->current_dest_drag != NULL)
2875 g_object_unref (display_x11->current_dest_drag);
2876 display_x11->current_dest_drag = NULL;
2879 new_context = gdk_drag_context_new ();
2880 new_context->protocol = GDK_DRAG_PROTO_XDND;
2881 PRIVATE_DATA(new_context)->version = version;
2883 /* FIXME: Should extend DnD protocol to have device info */
2884 device_manager = gdk_display_get_device_manager (display);
2885 gdk_drag_context_set_device (new_context, gdk_device_manager_get_client_pointer (device_manager));
2887 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2888 if (new_context->source_window)
2889 g_object_ref (new_context->source_window);
2892 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2893 if (!new_context->source_window)
2895 g_object_unref (new_context);
2896 return GDK_FILTER_REMOVE;
2899 new_context->dest_window = event->any.window;
2900 g_object_ref (new_context->dest_window);
2902 new_context->targets = NULL;
2905 gdk_error_trap_push ();
2906 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2908 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2910 False, XA_ATOM, &type, &format, &nitems,
2913 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2915 g_object_unref (new_context);
2920 return GDK_FILTER_REMOVE;
2923 atoms = (Atom *)data;
2925 for (i=0; i<nitems; i++)
2926 new_context->targets =
2927 g_list_append (new_context->targets,
2928 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2936 if (xevent->xclient.data.l[2+i])
2937 new_context->targets =
2938 g_list_append (new_context->targets,
2939 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2940 xevent->xclient.data.l[2+i])));
2943 #ifdef G_ENABLE_DEBUG
2944 if (_gdk_debug_flags & GDK_DEBUG_DND)
2945 print_target_list (new_context->targets);
2946 #endif /* G_ENABLE_DEBUG */
2948 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2949 xdnd_read_actions (new_context);
2951 event->dnd.type = GDK_DRAG_ENTER;
2952 event->dnd.context = new_context;
2953 gdk_event_set_device (event, gdk_drag_context_get_device (new_context));
2954 g_object_ref (new_context);
2956 display_x11->current_dest_drag = new_context;
2958 return GDK_FILTER_TRANSLATE;
2961 static GdkFilterReturn
2962 xdnd_leave_filter (GdkXEvent *xev,
2966 XEvent *xevent = (XEvent *)xev;
2967 guint32 source_window = xevent->xclient.data.l[0];
2968 GdkDisplay *display;
2969 GdkDisplayX11 *display_x11;
2971 if (!event->any.window ||
2972 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2973 return GDK_FILTER_CONTINUE; /* Not for us */
2976 g_message ("XdndLeave: source_window: %#x",
2979 display = GDK_WINDOW_DISPLAY (event->any.window);
2980 display_x11 = GDK_DISPLAY_X11 (display);
2982 xdnd_precache_atoms (display);
2984 if ((display_x11->current_dest_drag != NULL) &&
2985 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2986 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
2988 event->dnd.type = GDK_DRAG_LEAVE;
2989 /* Pass ownership of context to the event */
2990 event->dnd.context = display_x11->current_dest_drag;
2991 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
2993 display_x11->current_dest_drag = NULL;
2995 return GDK_FILTER_TRANSLATE;
2998 return GDK_FILTER_REMOVE;
3001 static GdkFilterReturn
3002 xdnd_position_filter (GdkXEvent *xev,
3006 XEvent *xevent = (XEvent *)xev;
3007 guint32 source_window = xevent->xclient.data.l[0];
3008 gint16 x_root = xevent->xclient.data.l[2] >> 16;
3009 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
3010 guint32 time = xevent->xclient.data.l[3];
3011 Atom action = xevent->xclient.data.l[4];
3013 GdkDisplay *display;
3014 GdkDisplayX11 *display_x11;
3016 if (!event->any.window ||
3017 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3018 return GDK_FILTER_CONTINUE; /* Not for us */
3021 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
3022 source_window, x_root, y_root, time, action));
3024 display = GDK_WINDOW_DISPLAY (event->any.window);
3025 display_x11 = GDK_DISPLAY_X11 (display);
3027 xdnd_precache_atoms (display);
3029 if ((display_x11->current_dest_drag != NULL) &&
3030 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3031 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
3033 event->dnd.type = GDK_DRAG_MOTION;
3034 event->dnd.context = display_x11->current_dest_drag;
3035 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
3036 g_object_ref (display_x11->current_dest_drag);
3038 event->dnd.time = time;
3040 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
3042 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
3043 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
3045 event->dnd.x_root = x_root;
3046 event->dnd.y_root = y_root;
3048 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
3049 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
3051 return GDK_FILTER_TRANSLATE;
3054 return GDK_FILTER_REMOVE;
3057 static GdkFilterReturn
3058 xdnd_drop_filter (GdkXEvent *xev,
3062 XEvent *xevent = (XEvent *)xev;
3063 guint32 source_window = xevent->xclient.data.l[0];
3064 guint32 time = xevent->xclient.data.l[2];
3065 GdkDisplay *display;
3066 GdkDisplayX11 *display_x11;
3068 if (!event->any.window ||
3069 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3070 return GDK_FILTER_CONTINUE; /* Not for us */
3073 g_message ("XdndDrop: source_window: %#x time: %d",
3074 source_window, time));
3076 display = GDK_WINDOW_DISPLAY (event->any.window);
3077 display_x11 = GDK_DISPLAY_X11 (display);
3079 xdnd_precache_atoms (display);
3081 if ((display_x11->current_dest_drag != NULL) &&
3082 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3083 (GDK_WINDOW_XID (display_x11->current_dest_drag->source_window) == source_window))
3085 GdkDragContextPrivateX11 *private;
3086 private = PRIVATE_DATA (display_x11->current_dest_drag);
3088 event->dnd.type = GDK_DROP_START;
3090 event->dnd.context = display_x11->current_dest_drag;
3091 gdk_event_set_device (event, gdk_drag_context_get_device (event->dnd.context));
3092 g_object_ref (display_x11->current_dest_drag);
3094 event->dnd.time = time;
3095 event->dnd.x_root = private->last_x;
3096 event->dnd.y_root = private->last_y;
3098 gdk_x11_window_set_user_time (event->any.window, time);
3100 return GDK_FILTER_TRANSLATE;
3103 return GDK_FILTER_REMOVE;
3106 /*************************************************************
3107 ************************** Public API ***********************
3108 *************************************************************/
3110 _gdk_dnd_init (GdkDisplay *display)
3115 gdk_display_add_client_message_filter (
3117 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
3118 motif_dnd_filter, NULL);
3120 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3122 gdk_display_add_client_message_filter (
3124 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
3125 xdnd_filters[i].func, NULL);
3132 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
3134 if (context->dest_window)
3136 switch (context->protocol)
3138 case GDK_DRAG_PROTO_MOTIF:
3139 motif_send_leave (context, time);
3141 case GDK_DRAG_PROTO_XDND:
3142 xdnd_send_leave (context);
3144 case GDK_DRAG_PROTO_ROOTWIN:
3145 case GDK_DRAG_PROTO_NONE:
3150 g_object_unref (context->dest_window);
3151 context->dest_window = NULL;
3157 * @window: the source window for this drag.
3158 * @targets: (transfer none) (element-type GdkAtom): the offered targets,
3159 * as list of #GdkAtom<!-- -->s
3161 * Starts a drag and creates a new drag context for it.
3163 * This function is called by the drag source.
3165 * Return value: (transfer full): a newly created #GdkDragContext.
3168 gdk_drag_begin (GdkWindow *window,
3171 GdkDragContext *new_context;
3172 GdkDisplay *display;
3174 GdkDeviceManager *device_manager;
3176 g_return_val_if_fail (window != NULL, NULL);
3177 g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), NULL);
3179 new_context = gdk_drag_context_new ();
3180 new_context->is_source = TRUE;
3181 new_context->source_window = window;
3182 g_object_ref (window);
3184 new_context->targets = g_list_copy (targets);
3185 precache_target_list (new_context);
3187 new_context->actions = 0;
3189 display = gdk_window_get_display (window);
3190 device_manager = gdk_display_get_device_manager (display);
3191 device = gdk_device_manager_get_client_pointer (device_manager);
3192 gdk_drag_context_set_device (new_context, device);
3197 static GdkNativeWindow
3198 _gdk_drag_get_protocol_for_display (GdkDisplay *display,
3199 GdkNativeWindow xid,
3200 GdkDragProtocol *protocol,
3205 GdkNativeWindow retval;
3206 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
3208 base_precache_atoms (display);
3210 /* Check for a local drag
3212 window = gdk_window_lookup_for_display (display, xid);
3214 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3216 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3218 *protocol = GDK_DRAG_PROTO_XDND;
3220 xdnd_precache_atoms (display);
3221 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3224 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3226 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3227 GDK_NOTE (DND, g_message ("Entering root window\n"));
3231 else if ((retval = xdnd_check_dest (display, xid, version)))
3233 *protocol = GDK_DRAG_PROTO_XDND;
3234 xdnd_precache_atoms (display);
3235 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3238 else if ((retval = motif_check_dest (display, xid)))
3240 *protocol = GDK_DRAG_PROTO_MOTIF;
3241 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3246 /* Check if this is a root window */
3248 gboolean rootwin = FALSE;
3251 unsigned long nitems, after;
3252 unsigned char *data;
3254 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3257 gdk_error_trap_push ();
3261 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3262 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
3263 0, 0, False, AnyPropertyType,
3264 &type, &format, &nitems, &after, &data) == Success &&
3272 /* Until I find out what window manager the next one is for,
3273 * I'm leaving it commented out. It's supported in the
3274 * xscreensaver sources, though.
3279 if (XGetWindowProperty (gdk_display, win,
3280 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
3281 0, 0, False, AnyPropertyType,
3282 &type, &format, &nitems, &data) &&
3291 gdk_error_trap_pop_ignored ();
3295 GDK_NOTE (DND, g_message ("Entering root window\n"));
3296 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3301 *protocol = GDK_DRAG_PROTO_NONE;
3303 return 0; /* a.k.a. None */
3307 * gdk_drag_get_protocol_for_display:
3308 * @display: the #GdkDisplay where the destination window resides
3309 * @xid: the windowing system id of the destination window.
3310 * @protocol: location where the supported DND protocol is returned.
3311 * @returns: the windowing system id of the window where the drop should happen. This
3312 * may be @xid or the id of a proxy window, or zero if @xid doesn't
3313 * support Drag and Drop.
3315 * Finds out the DND protocol supported by a window.
3320 gdk_drag_get_protocol_for_display (GdkDisplay *display,
3321 GdkNativeWindow xid,
3322 GdkDragProtocol *protocol)
3324 return _gdk_drag_get_protocol_for_display (display, xid, protocol, NULL);
3327 static GdkWindowCache *
3328 drag_context_find_window_cache (GdkDragContext *context,
3331 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3333 GdkWindowCache *cache;
3335 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
3337 cache = tmp_list->data;
3338 if (cache->screen == screen)
3342 cache = gdk_window_cache_new (screen);
3343 private->window_caches = g_slist_prepend (private->window_caches, cache);
3349 * gdk_drag_find_window_for_screen:
3350 * @context: a #GdkDragContext
3351 * @drag_window: a window which may be at the pointer position, but
3352 * should be ignored, since it is put up by the drag source as an icon.
3353 * @screen: the screen where the destination window is sought.
3354 * @x_root: the x position of the pointer in root coordinates.
3355 * @y_root: the y position of the pointer in root coordinates.
3356 * @dest_window: (out): location to store the destination window in.
3357 * @protocol: (out): location to store the DND protocol in.
3359 * Finds the destination window and DND protocol to use at the
3360 * given pointer position.
3362 * This function is called by the drag source to obtain the
3363 * @dest_window and @protocol parameters for gdk_drag_motion().
3368 gdk_drag_find_window_for_screen (GdkDragContext *context,
3369 GdkWindow *drag_window,
3373 GdkWindow **dest_window,
3374 GdkDragProtocol *protocol)
3376 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3377 GdkWindowCache *window_cache;
3378 GdkDisplay *display;
3381 g_return_if_fail (context != NULL);
3383 display = GDK_WINDOW_DISPLAY (context->source_window);
3385 window_cache = drag_context_find_window_cache (context, screen);
3387 dest = get_client_window_at_coords (window_cache,
3388 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3389 GDK_WINDOW_XID (drag_window) : None,
3392 if (private->dest_xid != dest)
3395 private->dest_xid = dest;
3397 /* Check if new destination accepts drags, and which protocol */
3399 /* There is some ugliness here. We actually need to pass
3400 * _three_ pieces of information to drag_motion - dest_window,
3401 * protocol, and the XID of the unproxied window. The first
3402 * two are passed explicitely, the third implicitly through
3403 * protocol->dest_xid.
3405 if ((recipient = _gdk_drag_get_protocol_for_display (display, dest,
3406 protocol, &private->version)))
3408 *dest_window = gdk_window_lookup_for_display (display, recipient);
3410 g_object_ref (*dest_window);
3412 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3415 *dest_window = NULL;
3419 *dest_window = context->dest_window;
3421 g_object_ref (*dest_window);
3422 *protocol = context->protocol;
3428 * @context: a #GdkDragContext.
3429 * @dest_window: the new destination window, obtained by
3430 * gdk_drag_find_window().
3431 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3432 * @x_root: the x position of the pointer in root coordinates.
3433 * @y_root: the y position of the pointer in root coordinates.
3434 * @suggested_action: the suggested action.
3435 * @possible_actions: the possible actions.
3436 * @time_: the timestamp for this operation.
3438 * Updates the drag context when the pointer moves or the
3439 * set of actions changes.
3441 * This function is called by the drag source.
3443 * Return value: FIXME
3446 gdk_drag_motion (GdkDragContext *context,
3447 GdkWindow *dest_window,
3448 GdkDragProtocol protocol,
3451 GdkDragAction suggested_action,
3452 GdkDragAction possible_actions,
3455 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3457 g_return_val_if_fail (context != NULL, FALSE);
3458 g_return_val_if_fail (dest_window == NULL || GDK_WINDOW_IS_X11 (dest_window), FALSE);
3460 private->old_actions = context->actions;
3461 context->actions = possible_actions;
3463 if (private->old_actions != possible_actions)
3464 private->xdnd_actions_set = FALSE;
3466 if (protocol == GDK_DRAG_PROTO_XDND && private->version == 0)
3468 /* This ugly hack is necessary since GTK+ doesn't know about
3469 * the XDND protocol version, and in particular doesn't know
3470 * that gdk_drag_find_window_for_screen() has the side-effect
3471 * of setting private->version, and therefore sometimes call
3472 * gdk_drag_motion() without a prior call to
3473 * gdk_drag_find_window_for_screen(). This happens, e.g.
3474 * when GTK+ is proxying DND events to embedded windows.
3478 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3480 xdnd_check_dest (display,
3481 GDK_WINDOW_XID (dest_window),
3486 /* When we have a Xdnd target, make sure our XdndActionList
3487 * matches the current actions;
3489 if (protocol == GDK_DRAG_PROTO_XDND && !private->xdnd_actions_set)
3493 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3494 xdnd_set_actions (context);
3495 else if (context->dest_window == dest_window)
3497 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3498 GdkDragContext *dest_context;
3500 dest_context = gdk_drag_context_find (display, FALSE,
3501 GDK_WINDOW_XID (context->source_window),
3502 GDK_WINDOW_XID (dest_window));
3506 dest_context->actions = context->actions;
3507 PRIVATE_DATA (dest_context)->xdnd_have_actions = TRUE;
3513 if (context->dest_window != dest_window)
3515 GdkEvent *temp_event;
3517 /* Send a leave to the last destination */
3518 gdk_drag_do_leave (context, time);
3519 private->drag_status = GDK_DRAG_STATUS_DRAG;
3521 /* Check if new destination accepts drags, and which protocol */
3525 context->dest_window = dest_window;
3526 private->drop_xid = private->dest_xid;
3527 g_object_ref (context->dest_window);
3528 context->protocol = protocol;
3532 case GDK_DRAG_PROTO_MOTIF:
3533 motif_send_enter (context, time);
3536 case GDK_DRAG_PROTO_XDND:
3537 xdnd_send_enter (context);
3540 case GDK_DRAG_PROTO_ROOTWIN:
3541 case GDK_DRAG_PROTO_NONE:
3545 private->old_action = suggested_action;
3546 context->suggested_action = suggested_action;
3547 private->old_actions = possible_actions;
3551 context->dest_window = NULL;
3552 private->drop_xid = None;
3553 context->action = 0;
3556 /* Push a status event, to let the client know that
3559 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3560 temp_event->dnd.window = g_object_ref (context->source_window);
3561 /* We use this to signal a synthetic status. Perhaps
3562 * we should use an extra field...
3564 temp_event->dnd.send_event = TRUE;
3566 temp_event->dnd.context = g_object_ref (context);
3567 temp_event->dnd.time = time;
3568 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3570 gdk_event_put (temp_event);
3571 gdk_event_free (temp_event);
3575 private->old_action = context->suggested_action;
3576 context->suggested_action = suggested_action;
3579 /* Send a drag-motion event */
3581 private->last_x = x_root;
3582 private->last_y = y_root;
3584 if (context->dest_window)
3586 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3588 switch (context->protocol)
3590 case GDK_DRAG_PROTO_MOTIF:
3591 motif_send_motion (context, x_root, y_root, suggested_action, time);
3594 case GDK_DRAG_PROTO_XDND:
3595 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3598 case GDK_DRAG_PROTO_ROOTWIN:
3600 GdkEvent *temp_event;
3601 /* GTK+ traditionally has used application/x-rootwin-drop,
3602 * but the XDND spec specifies x-rootwindow-drop.
3604 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3605 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3607 if (g_list_find (context->targets,
3608 GDK_ATOM_TO_POINTER (target1)) ||
3609 g_list_find (context->targets,
3610 GDK_ATOM_TO_POINTER (target2)))
3611 context->action = context->suggested_action;
3613 context->action = 0;
3615 temp_event = gdk_event_new (GDK_DRAG_STATUS);
3616 temp_event->dnd.window = g_object_ref (context->source_window);
3617 temp_event->dnd.send_event = FALSE;
3618 temp_event->dnd.context = g_object_ref (context);
3619 temp_event->dnd.time = time;
3620 gdk_event_set_device (temp_event, gdk_drag_context_get_device (context));
3622 gdk_event_put (temp_event);
3623 gdk_event_free (temp_event);
3626 case GDK_DRAG_PROTO_NONE:
3627 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3642 * @context: a #GdkDragContext.
3643 * @time_: the timestamp for this operation.
3645 * Drops on the current destination.
3647 * This function is called by the drag source.
3650 gdk_drag_drop (GdkDragContext *context,
3653 g_return_if_fail (context != NULL);
3655 if (context->dest_window)
3657 switch (context->protocol)
3659 case GDK_DRAG_PROTO_MOTIF:
3660 motif_send_leave (context, time);
3661 motif_send_drop (context, time);
3664 case GDK_DRAG_PROTO_XDND:
3665 xdnd_send_drop (context, time);
3668 case GDK_DRAG_PROTO_ROOTWIN:
3669 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3671 case GDK_DRAG_PROTO_NONE:
3672 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3682 * @context: a #GdkDragContext.
3683 * @time_: the timestamp for this operation.
3685 * Aborts a drag without dropping.
3687 * This function is called by the drag source.
3690 gdk_drag_abort (GdkDragContext *context,
3693 g_return_if_fail (context != NULL);
3695 gdk_drag_do_leave (context, time);
3698 /* Destination side */
3702 * @context: a #GdkDragContext.
3703 * @action: the selected action which will be taken when a drop happens,
3704 * or 0 to indicate that a drop will not be accepted.
3705 * @time_: the timestamp for this operation.
3707 * Selects one of the actions offered by the drag source.
3709 * This function is called by the drag destination in response to
3710 * gdk_drag_motion() called by the drag source.
3713 gdk_drag_status (GdkDragContext *context,
3714 GdkDragAction action,
3717 GdkDragContextPrivateX11 *private;
3719 GdkDisplay *display;
3721 g_return_if_fail (context != NULL);
3723 private = PRIVATE_DATA (context);
3724 display = GDK_WINDOW_DISPLAY (context->source_window);
3726 context->action = action;
3728 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3730 gboolean need_coords = FALSE;
3732 xev.xclient.type = ClientMessage;
3733 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3734 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3735 xev.xclient.format = 8;
3736 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3738 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3740 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3744 if ((action != 0) != (private->old_action != 0))
3748 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3752 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3756 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3761 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3765 case GDK_ACTION_MOVE:
3766 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3768 case GDK_ACTION_COPY:
3769 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3771 case GDK_ACTION_LINK:
3772 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3775 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3780 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3782 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3784 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3788 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3789 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3792 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3794 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3795 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3797 if (!_gdk_send_xevent (display,
3798 GDK_WINDOW_XID (context->source_window),
3801 g_message ("Send event to %lx failed",
3802 GDK_WINDOW_XID (context->source_window)));
3804 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3806 xev.xclient.type = ClientMessage;
3807 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3808 xev.xclient.format = 32;
3809 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3811 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3812 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3813 xev.xclient.data.l[2] = 0;
3814 xev.xclient.data.l[3] = 0;
3815 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3817 if (!xdnd_send_xevent (context, context->source_window,
3820 g_message ("Send event to %lx failed",
3821 GDK_WINDOW_XID (context->source_window)));
3824 private->old_action = action;
3829 * @context: a #GdkDragContext.
3830 * @ok: %TRUE if the drop is accepted.
3831 * @time_: the timestamp for this operation.
3833 * Accepts or rejects a drop.
3835 * This function is called by the drag destination in response
3836 * to a drop initiated by the drag source.
3839 gdk_drop_reply (GdkDragContext *context,
3843 GdkDragContextPrivateX11 *private;
3845 g_return_if_fail (context != NULL);
3847 private = PRIVATE_DATA (context);
3849 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3851 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3854 xev.xclient.type = ClientMessage;
3855 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3856 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3857 xev.xclient.format = 8;
3859 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3860 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3862 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3863 (XmDROP_SITE_VALID << 4) |
3864 (XmDROP_NOOP << 8) |
3867 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3868 (XmNO_DROP_SITE << 4) |
3869 (XmDROP_NOOP << 8) |
3870 (XmDROP_CANCEL << 12);
3871 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3872 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3873 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3874 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3875 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3877 _gdk_send_xevent (display,
3878 GDK_WINDOW_XID (context->source_window),
3885 * @context: a #GtkDragContext.
3886 * @success: %TRUE if the data was successfully received.
3887 * @time_: the timestamp for this operation.
3889 * Ends the drag operation after a drop.
3891 * This function is called by the drag destination.
3894 gdk_drop_finish (GdkDragContext *context,
3898 g_return_if_fail (context != NULL);
3900 if (context->protocol == GDK_DRAG_PROTO_XDND)
3902 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
3905 xev.xclient.type = ClientMessage;
3906 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3907 xev.xclient.format = 32;
3908 xev.xclient.window = GDK_WINDOW_XID (context->source_window);
3910 xev.xclient.data.l[0] = GDK_WINDOW_XID (context->dest_window);
3913 xev.xclient.data.l[1] = 1;
3914 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3919 xev.xclient.data.l[1] = 0;
3920 xev.xclient.data.l[2] = None;
3922 xev.xclient.data.l[3] = 0;
3923 xev.xclient.data.l[4] = 0;
3925 if (!xdnd_send_xevent (context, context->source_window,
3928 g_message ("Send event to %lx failed",
3929 GDK_WINDOW_XID (context->source_window)));
3935 * gdk_window_register_dnd:
3936 * @window: a #GdkWindow.
3938 * Registers a window as a potential drop destination.
3941 gdk_window_register_dnd (GdkWindow *window)
3943 static const gulong xdnd_version = 5;
3944 MotifDragReceiverInfo info;
3945 Atom motif_drag_receiver_info_atom;
3946 GdkDisplay *display = gdk_window_get_display (window);
3948 g_return_if_fail (window != NULL);
3950 if (gdk_window_get_window_type (window) == GDK_WINDOW_OFFSCREEN)
3953 base_precache_atoms (display);
3955 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3958 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3960 /* Set Motif drag receiver information property */
3962 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3963 "_MOTIF_DRAG_RECEIVER_INFO");
3964 /* initialize to zero to avoid writing uninitialized data to socket */
3965 memset(&info, 0, sizeof(info));
3966 info.byte_order = local_byte_order;
3967 info.protocol_version = 0;
3968 info.protocol_style = XmDRAG_DYNAMIC;
3969 info.proxy_window = None;
3970 info.num_drop_sites = 0;
3971 info.total_size = sizeof(info);
3973 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
3974 motif_drag_receiver_info_atom,
3975 motif_drag_receiver_info_atom,
3982 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3983 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3984 GDK_WINDOW_XID (window),
3985 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3986 XA_ATOM, 32, PropModeReplace,
3987 (guchar *)&xdnd_version, 1);
3991 * gdk_drag_get_selection:
3992 * @context: a #GdkDragContext.
3994 * Returns the selection atom for the current source window.
3996 * Return value: the selection atom.
3999 gdk_drag_get_selection (GdkDragContext *context)
4001 g_return_val_if_fail (context != NULL, GDK_NONE);
4003 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
4004 return gdk_x11_xatom_to_atom_for_display (GDK_WINDOW_DISPLAY (context->source_window),
4005 (PRIVATE_DATA (context))->motif_selection);
4006 else if (context->protocol == GDK_DRAG_PROTO_XDND)
4007 return gdk_atom_intern_static_string ("XdndSelection");
4013 * gdk_drag_drop_succeeded:
4014 * @context: a #GdkDragContext
4016 * Returns whether the dropped data has been successfully
4017 * transferred. This function is intended to be used while
4018 * handling a %GDK_DROP_FINISHED event, its return value is
4019 * meaningless at other times.
4021 * Return value: %TRUE if the drop was successful.
4026 gdk_drag_drop_succeeded (GdkDragContext *context)
4028 GdkDragContextPrivateX11 *private;
4030 g_return_val_if_fail (context != NULL, FALSE);
4032 private = PRIVATE_DATA (context);
4034 return !private->drop_failed;