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/.
30 #include <X11/Xutil.h>
31 #include <X11/Xatom.h>
32 #include <X11/extensions/shape.h>
36 #include "gdk.h" /* For gdk_flush() */
40 #include "gdkproperty.h"
41 #include "gdkprivate-x11.h"
42 #include "gdkinternals.h"
43 #include "gdkscreen-x11.h"
44 #include "gdkdisplay-x11.h"
47 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
51 GDK_DRAG_STATUS_MOTION_WAIT,
52 GDK_DRAG_STATUS_ACTION_WAIT,
58 gint x, y, width, height;
60 gboolean shape_selected;
67 GHashTable *child_hash;
72 /* Structure that holds information about a drag in progress.
73 * this is used on both source and destination sides.
75 struct _GdkDragContextPrivateX11 {
76 GdkDragContext context;
81 guint16 last_x; /* Coordinates from last event */
83 GdkDragAction old_action; /* The last action we sent to the source */
84 GdkDragAction old_actions; /* The last actions we sent to the source */
85 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
87 Window dest_xid; /* The last window we looked up */
88 Window drop_xid; /* The (non-proxied) window that is receiving drops */
89 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
90 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
91 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
92 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
93 guint drag_status : 4; /* current status of drag */
95 guint drop_failed : 1; /* Whether the drop was unsuccessful */
96 guint version; /* Xdnd protocol version */
98 GSList *window_caches;
101 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
103 /* Forward declarations */
105 static void gdk_window_cache_destroy (GdkWindowCache *cache);
107 static void motif_read_target_table (GdkDisplay *display);
109 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
113 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
116 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
119 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
122 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
125 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
128 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
132 static void xdnd_manage_source_filter (GdkDragContext *context,
134 gboolean add_filter);
136 static void gdk_drag_context_finalize (GObject *object);
138 static GList *contexts;
140 static const struct {
141 const char *atom_name;
144 { "XdndEnter", xdnd_enter_filter },
145 { "XdndLeave", xdnd_leave_filter },
146 { "XdndPosition", xdnd_position_filter },
147 { "XdndStatus", xdnd_status_filter },
148 { "XdndFinished", xdnd_finished_filter },
149 { "XdndDrop", xdnd_drop_filter },
152 G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
155 gdk_drag_context_init (GdkDragContext *dragcontext)
157 GdkDragContextPrivateX11 *private;
159 private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext,
160 GDK_TYPE_DRAG_CONTEXT,
161 GdkDragContextPrivateX11);
163 dragcontext->windowing_data = private;
165 contexts = g_list_prepend (contexts, dragcontext);
169 gdk_drag_context_class_init (GdkDragContextClass *klass)
171 GObjectClass *object_class = G_OBJECT_CLASS (klass);
173 object_class->finalize = gdk_drag_context_finalize;
175 g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateX11));
179 gdk_drag_context_finalize (GObject *object)
181 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
182 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
185 g_list_free (context->targets);
187 if (context->source_window)
189 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
191 xdnd_manage_source_filter (context, context->source_window, FALSE);
193 g_object_unref (context->source_window);
196 if (context->dest_window)
197 g_object_unref (context->dest_window);
199 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
200 gdk_window_cache_destroy (tmp_list->data);
201 g_slist_free (private->window_caches);
203 contexts = g_list_remove (contexts, context);
205 G_OBJECT_CLASS (gdk_drag_context_parent_class)->finalize (object);
211 * gdk_drag_context_new:
213 * Creates a new #GdkDragContext.
215 * Return value: the newly created #GdkDragContext.
218 gdk_drag_context_new (void)
220 return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
223 static GdkDragContext *
224 gdk_drag_context_find (GdkDisplay *display,
229 GList *tmp_list = contexts;
230 GdkDragContext *context;
231 GdkDragContextPrivateX11 *private;
232 Window context_dest_xid;
236 context = (GdkDragContext *)tmp_list->data;
237 private = PRIVATE_DATA (context);
239 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
240 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
243 context_dest_xid = context->dest_window ?
246 GDK_DRAWABLE_XID (context->dest_window)) :
249 if ((!context->is_source == !is_source) &&
250 ((source_xid == None) || (context->source_window &&
251 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
252 ((dest_xid == None) || (context_dest_xid == dest_xid)))
255 tmp_list = tmp_list->next;
262 precache_target_list (GdkDragContext *context)
264 if (context->targets)
266 GPtrArray *targets = g_ptr_array_new ();
270 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
271 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
273 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
274 (const gchar **)targets->pdata,
277 for (i =0; i < targets->len; i++)
278 g_free (targets->pdata[i]);
280 g_ptr_array_free (targets, TRUE);
284 /* Utility functions */
287 free_cache_child (GdkCacheChild *child,
291 gdk_region_destroy (child->shape);
293 if (child->shape_selected && display)
295 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
297 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
304 gdk_window_cache_add (GdkWindowCache *cache,
306 gint x, gint y, gint width, gint height,
309 GdkCacheChild *child = g_new (GdkCacheChild, 1);
314 child->width = width;
315 child->height = height;
316 child->mapped = mapped;
317 child->shape_selected = FALSE;
318 child->shape_valid = FALSE;
321 cache->children = g_list_prepend (cache->children, child);
322 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
326 static GdkFilterReturn
327 gdk_window_cache_shape_filter (GdkXEvent *xev,
331 XEvent *xevent = (XEvent *)xev;
332 GdkWindowCache *cache = data;
334 GdkDisplayX11 *display = GDK_DISPLAY_X11 (gdk_screen_get_display (cache->screen));
336 if (display->have_shapes &&
337 xevent->type == display->shape_event_base + ShapeNotify)
339 XShapeEvent *xse = (XShapeEvent*)xevent;
342 node = g_hash_table_lookup (cache->child_hash,
343 GUINT_TO_POINTER (xse->window));
346 GdkCacheChild *child = node->data;
347 child->shape_valid = FALSE;
350 gdk_region_destroy (child->shape);
355 return GDK_FILTER_REMOVE;
358 return GDK_FILTER_CONTINUE;
361 static GdkFilterReturn
362 gdk_window_cache_filter (GdkXEvent *xev,
366 XEvent *xevent = (XEvent *)xev;
367 GdkWindowCache *cache = data;
369 switch (xevent->type)
371 case CirculateNotify:
373 case ConfigureNotify:
375 XConfigureEvent *xce = &xevent->xconfigure;
378 node = g_hash_table_lookup (cache->child_hash,
379 GUINT_TO_POINTER (xce->window));
382 GdkCacheChild *child = node->data;
385 child->width = xce->width;
386 child->height = xce->height;
387 if (xce->above == None && (node->next))
389 GList *last = g_list_last (cache->children);
390 cache->children = g_list_remove_link (cache->children, node);
397 GList *above_node = g_hash_table_lookup (cache->child_hash,
398 GUINT_TO_POINTER (xce->above));
399 if (above_node && node->next != above_node)
401 /* Put the window above (before in the list) above_node
403 cache->children = g_list_remove_link (cache->children, node);
404 node->prev = above_node->prev;
406 node->prev->next = node;
408 cache->children = node;
409 node->next = above_node;
410 above_node->prev = node;
418 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
420 if (!g_hash_table_lookup (cache->child_hash,
421 GUINT_TO_POINTER (xcwe->window)))
422 gdk_window_cache_add (cache, xcwe->window,
423 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
429 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
432 node = g_hash_table_lookup (cache->child_hash,
433 GUINT_TO_POINTER (xdwe->window));
436 GdkCacheChild *child = node->data;
438 g_hash_table_remove (cache->child_hash,
439 GUINT_TO_POINTER (xdwe->window));
440 cache->children = g_list_remove_link (cache->children, node);
441 /* window is destroyed, no need to disable ShapeNotify */
442 free_cache_child (child, NULL);
443 g_list_free_1 (node);
449 XMapEvent *xme = &xevent->xmap;
452 node = g_hash_table_lookup (cache->child_hash,
453 GUINT_TO_POINTER (xme->window));
456 GdkCacheChild *child = node->data;
457 child->mapped = TRUE;
465 XMapEvent *xume = &xevent->xmap;
468 node = g_hash_table_lookup (cache->child_hash,
469 GUINT_TO_POINTER (xume->window));
472 GdkCacheChild *child = node->data;
473 child->mapped = FALSE;
478 return GDK_FILTER_CONTINUE;
480 return GDK_FILTER_REMOVE;
483 static GdkWindowCache *
484 gdk_window_cache_new (GdkScreen *screen)
486 XWindowAttributes xwa;
487 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
488 GdkWindow *root_window = gdk_screen_get_root_window (screen);
489 GdkChildInfoX11 *children;
492 GdkWindowCache *result = g_new (GdkWindowCache, 1);
494 result->children = NULL;
495 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
496 result->screen = screen;
498 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
499 result->old_event_mask = xwa.your_event_mask;
501 if (G_UNLIKELY (!GDK_DISPLAY_X11 (GDK_SCREEN_X11 (screen)->display)->trusted_client))
503 GList *toplevel_windows, *list;
505 gint x, y, width, height;
507 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
508 for (list = toplevel_windows; list; list = list->next) {
509 window = GDK_WINDOW (list->data);
510 gdk_window_get_geometry (window, &x, &y, &width, &height, NULL);
511 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
513 gdk_window_is_visible (window));
515 g_list_free (toplevel_windows);
519 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
520 result->old_event_mask | SubstructureNotifyMask);
521 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
522 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
524 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
525 GDK_WINDOW_XWINDOW (root_window),
527 &children, &nchildren))
530 for (i = 0; i < nchildren ; i++)
532 gdk_window_cache_add (result, children[i].window,
533 children[i].x, children[i].y, children[i].width, children[i].height,
534 children[i].is_mapped);
543 gdk_window_cache_destroy (GdkWindowCache *cache)
545 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
547 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
548 GDK_WINDOW_XWINDOW (root_window),
549 cache->old_event_mask);
550 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
551 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
553 gdk_error_trap_push ();
555 g_list_foreach (cache->children, (GFunc)free_cache_child,
556 gdk_screen_get_display (cache->screen));
559 gdk_error_trap_pop ();
561 g_list_free (cache->children);
562 g_hash_table_destroy (cache->child_hash);
568 is_pointer_within_shape (GdkDisplay *display,
569 GdkCacheChild *child,
573 if (!child->shape_selected)
575 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
577 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
578 child->shape_selected = TRUE;
580 if (!child->shape_valid)
582 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
583 GdkRegion *input_shape;
585 child->shape = _xwindow_get_shape (display_x11->xdisplay,
586 child->xid, ShapeBounding);
588 input_shape = _xwindow_get_shape (display_x11->xdisplay,
589 child->xid, ShapeInput);
590 if (child->shape && input_shape)
592 gdk_region_intersect (child->shape, input_shape);
593 gdk_region_destroy (input_shape);
595 else if (input_shape)
597 child->shape = input_shape;
601 child->shape_valid = TRUE;
604 return child->shape == NULL ||
605 gdk_region_point_in (child->shape, x_pos, y_pos);
609 get_client_window_at_coords_recurse (GdkDisplay *display,
611 gboolean is_toplevel,
615 GdkChildInfoX11 *children;
616 unsigned int nchildren;
618 gboolean found_child = FALSE;
619 GdkChildInfoX11 child = { 0, };
620 gboolean has_wm_state = FALSE;
622 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
623 is_toplevel? &has_wm_state : NULL,
624 &children, &nchildren))
634 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
636 GdkChildInfoX11 *cur_child = &children[i];
638 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
639 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
640 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
653 if (child.has_wm_state)
656 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
663 get_client_window_at_coords (GdkWindowCache *cache,
669 Window retval = None;
671 gdk_error_trap_push ();
673 tmp_list = cache->children;
675 while (tmp_list && !retval)
677 GdkCacheChild *child = tmp_list->data;
679 if ((child->xid != ignore) && (child->mapped))
681 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
682 (y_root >= child->y) && (y_root < child->y + child->height))
684 GdkDisplay *display = gdk_screen_get_display (cache->screen);
686 if (!is_pointer_within_shape (display, child,
690 tmp_list = tmp_list->next;
694 retval = get_client_window_at_coords_recurse (display,
702 tmp_list = tmp_list->next;
705 gdk_error_trap_pop ();
710 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
713 /*************************************************************
714 ***************************** MOTIF *************************
715 *************************************************************/
717 /* values used in the message type for Motif DND */
730 /* Values used to specify type of protocol to use */
734 XmDRAG_PREFER_PREREGISTER,
736 XmDRAG_PREFER_DYNAMIC,
738 XmDRAG_PREFER_RECEIVER
741 /* Operation codes */
749 /* Drop site status */
751 XmNO_DROP_SITE = 0x01,
752 XmDROP_SITE_INVALID = 0x02,
753 XmDROP_SITE_VALID = 0x03
756 /* completion status */
764 /* Byte swapping routines. The motif specification leaves it
765 * up to us to save a few bytes in the client messages
767 static gchar local_byte_order = '\0';
769 #ifdef G_ENABLE_DEBUG
771 print_target_list (GList *targets)
775 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
776 g_message ("\t%s", name);
778 targets = targets->next;
781 #endif /* G_ENABLE_DEBUG */
784 init_byte_order (void)
786 guint32 myint = 0x01020304;
787 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
791 card16_to_host (guint16 x, gchar byte_order) {
792 if (byte_order == local_byte_order)
795 return (x << 8) | (x >> 8);
799 card32_to_host (guint32 x, gchar byte_order) {
800 if (byte_order == local_byte_order)
803 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
806 /* Motif packs together fields of varying length into the
807 * client message. We can't rely on accessing these
808 * through data.s[], data.l[], etc, because on some architectures
809 * (i.e., Alpha) these won't be valid for format == 8.
812 #define MOTIF_XCLIENT_BYTE(xevent,i) \
813 (xevent)->xclient.data.b[i]
814 #define MOTIF_XCLIENT_SHORT(xevent,i) \
815 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
816 #define MOTIF_XCLIENT_LONG(xevent,i) \
817 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
819 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
820 #define MOTIF_UNPACK_SHORT(xevent,i) \
821 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
822 #define MOTIF_UNPACK_LONG(xevent,i) \
823 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
825 /***** Dest side ***********/
827 /* Property placed on source windows */
828 typedef struct _MotifDragInitiatorInfo {
830 guint8 protocol_version;
831 guint16 targets_index;
832 guint32 selection_atom;
833 } MotifDragInitiatorInfo;
835 /* Header for target table on the drag window */
836 typedef struct _MotifTargetTableHeader {
838 guchar protocol_version;
841 } MotifTargetTableHeader;
843 /* Property placed on target windows */
844 typedef struct _MotifDragReceiverInfo {
846 guint8 protocol_version;
847 guint8 protocol_style;
849 guint32 proxy_window;
850 guint16 num_drop_sites;
853 } MotifDragReceiverInfo;
855 /* Target table handling */
857 static GdkFilterReturn
858 motif_drag_window_filter (GdkXEvent *xevent,
862 XEvent *xev = (XEvent *)xevent;
863 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
864 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
866 switch (xev->xany.type)
869 display_x11->motif_drag_window = None;
870 display_x11->motif_drag_gdk_window = NULL;
873 if (display_x11->motif_target_lists &&
874 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
875 motif_read_target_table (display);
878 return GDK_FILTER_REMOVE;
882 motif_lookup_drag_window (GdkDisplay *display,
883 Display *lookup_xdisplay)
885 Window retval = None;
886 gulong bytes_after, nitems;
891 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
892 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
894 XA_WINDOW, &type, &format, &nitems, &bytes_after,
897 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
899 retval = *(Window *)data;
901 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
910 /* Finds the window where global Motif drag information is stored.
911 * If it doesn't exist and 'create' is TRUE, create one.
914 motif_find_drag_window (GdkDisplay *display,
917 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
919 if (!display_x11->motif_drag_window)
921 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
922 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
924 if (!display_x11->motif_drag_window && create)
926 /* Create a persistant window. (Copied from LessTif) */
928 Display *persistant_xdisplay;
929 XSetWindowAttributes attr;
930 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
931 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
933 XGrabServer (persistant_xdisplay);
935 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
937 if (!display_x11->motif_drag_window)
939 attr.override_redirect = True;
940 attr.event_mask = PropertyChangeMask;
942 display_x11->motif_drag_window =
943 XCreateWindow (persistant_xdisplay,
944 RootWindow (persistant_xdisplay, 0),
945 -100, -100, 10, 10, 0, 0,
946 InputOnly, (Visual *)CopyFromParent,
947 (CWOverrideRedirect | CWEventMask), &attr);
950 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
952 XChangeProperty (persistant_xdisplay,
953 RootWindow (persistant_xdisplay, 0),
954 motif_drag_window_atom, XA_WINDOW,
956 (guchar *)&motif_drag_window_atom, 1);
959 XUngrabServer (persistant_xdisplay);
960 XCloseDisplay (persistant_xdisplay);
963 /* There is a miniscule race condition here if the drag window
964 * gets destroyed exactly now.
966 if (display_x11->motif_drag_window)
968 display_x11->motif_drag_gdk_window =
969 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
970 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
971 motif_drag_window_filter,
976 return display_x11->motif_drag_window;
980 motif_read_target_table (GdkDisplay *display)
982 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
983 gulong bytes_after, nitems;
988 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
990 if (display_x11->motif_target_lists)
992 for (i=0; i<display_x11->motif_n_target_lists; i++)
993 g_list_free (display_x11->motif_target_lists[i]);
995 g_free (display_x11->motif_target_lists);
996 display_x11->motif_target_lists = NULL;
997 display_x11->motif_n_target_lists = 0;
1000 if (motif_find_drag_window (display, FALSE))
1003 MotifTargetTableHeader *header = NULL;
1004 guchar *target_bytes = NULL;
1006 gboolean success = FALSE;
1008 gdk_error_trap_push ();
1009 XGetWindowProperty (display_x11->xdisplay,
1010 display_x11->motif_drag_window,
1011 motif_drag_targets_atom,
1012 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1013 motif_drag_targets_atom,
1014 &type, &format, &nitems, &bytes_after,
1017 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1020 header = (MotifTargetTableHeader *)data;
1022 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1023 header->total_size = card32_to_host (header->total_size, header->byte_order);
1025 gdk_error_trap_push ();
1026 XGetWindowProperty (display_x11->xdisplay,
1027 display_x11->motif_drag_window,
1028 motif_drag_targets_atom,
1029 (sizeof(MotifTargetTableHeader)+3)/4,
1030 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1032 motif_drag_targets_atom, &type, &format, &nitems,
1033 &bytes_after, &target_bytes);
1035 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
1036 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1039 display_x11->motif_n_target_lists = header->n_lists;
1040 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1043 for (i=0; i<header->n_lists; i++)
1048 if (p + sizeof(guint16) - target_bytes > nitems)
1051 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1053 /* We need to make a copy of the targets, since it may
1056 targets = g_new (guint32, n_targets);
1057 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1059 p += sizeof(guint16) + n_targets * sizeof(guint32);
1060 if (p - target_bytes > nitems)
1063 for (j=0; j<n_targets; j++)
1064 display_x11->motif_target_lists[i] =
1065 g_list_prepend (display_x11->motif_target_lists[i],
1066 GUINT_TO_POINTER (card32_to_host (targets[j],
1067 header->byte_order)));
1069 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1079 XFree (target_bytes);
1083 if (display_x11->motif_target_lists)
1085 g_free (display_x11->motif_target_lists);
1086 display_x11->motif_target_lists = NULL;
1087 display_x11->motif_n_target_lists = 0;
1089 g_warning ("Error reading Motif target table\n");
1095 targets_sort_func (gconstpointer a, gconstpointer b)
1097 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1098 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1101 /* Check if given (sorted) list is in the targets table */
1103 motif_target_table_check (GdkDisplay *display,
1106 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1107 GList *tmp_list1, *tmp_list2;
1110 for (i=0; i<display_x11->motif_n_target_lists; i++)
1112 tmp_list1 = display_x11->motif_target_lists[i];
1115 while (tmp_list1 && tmp_list2)
1117 if (tmp_list1->data != tmp_list2->data)
1120 tmp_list1 = tmp_list1->next;
1121 tmp_list2 = tmp_list2->next;
1123 if (!tmp_list1 && !tmp_list2) /* Found it */
1131 motif_add_to_target_table (GdkDisplay *display,
1132 GList *targets) /* targets is list of GdkAtom */
1134 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1135 GList *sorted = NULL;
1140 /* make a sorted copy of the list */
1144 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1145 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1146 targets = targets->next;
1149 /* First check if it is there already */
1151 if (display_x11->motif_target_lists)
1152 index = motif_target_table_check (display, sorted);
1154 /* We need to grab the server while doing this, to ensure
1160 /* We need to make sure that it exists _before_ we grab the
1161 * server, since we can't open a new connection after we
1164 motif_find_drag_window (display, TRUE);
1166 gdk_x11_display_grab (display);
1167 motif_read_target_table (display);
1169 /* Check again, in case it was added in the meantime */
1171 if (display_x11->motif_target_lists)
1172 index = motif_target_table_check (display, sorted);
1176 guint32 total_size = 0;
1180 MotifTargetTableHeader *header;
1182 if (!display_x11->motif_target_lists)
1184 display_x11->motif_target_lists = g_new (GList *, 1);
1185 display_x11->motif_n_target_lists = 1;
1189 display_x11->motif_n_target_lists++;
1190 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1191 sizeof(GList *) * display_x11->motif_n_target_lists);
1193 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1195 index = display_x11->motif_n_target_lists - 1;
1197 total_size = sizeof (MotifTargetTableHeader);
1198 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1199 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1201 data = g_malloc (total_size);
1203 header = (MotifTargetTableHeader *)data;
1204 p = data + sizeof(MotifTargetTableHeader);
1206 header->byte_order = local_byte_order;
1207 header->protocol_version = 0;
1208 header->n_lists = display_x11->motif_n_target_lists;
1209 header->total_size = total_size;
1211 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1213 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1214 guint32 *targets = g_new (guint32, n_targets);
1215 guint32 *p32 = targets;
1217 tmp_list = display_x11->motif_target_lists[i];
1220 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1222 tmp_list = tmp_list->next;
1227 p += sizeof(guint16);
1229 memcpy (p, targets, n_targets * sizeof(guint32));
1232 p += sizeof(guint32) * n_targets;
1236 XChangeProperty (display_x11->xdisplay,
1237 display_x11->motif_drag_window,
1238 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1239 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1243 gdk_x11_display_ungrab (display);
1246 g_list_free (sorted);
1250 /* Translate flags */
1253 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1255 guint recommended_op = flags & 0x000f;
1256 guint possible_ops = (flags & 0x0f0) >> 4;
1258 switch (recommended_op)
1261 context->suggested_action = GDK_ACTION_MOVE;
1264 context->suggested_action = GDK_ACTION_COPY;
1267 context->suggested_action = GDK_ACTION_LINK;
1270 context->suggested_action = GDK_ACTION_COPY;
1274 context->actions = 0;
1275 if (possible_ops & XmDROP_MOVE)
1276 context->actions |= GDK_ACTION_MOVE;
1277 if (possible_ops & XmDROP_COPY)
1278 context->actions |= GDK_ACTION_COPY;
1279 if (possible_ops & XmDROP_LINK)
1280 context->actions |= GDK_ACTION_LINK;
1284 motif_dnd_get_flags (GdkDragContext *context)
1288 switch (context->suggested_action)
1290 case GDK_ACTION_MOVE:
1291 flags = XmDROP_MOVE;
1293 case GDK_ACTION_COPY:
1294 flags = XmDROP_COPY;
1296 case GDK_ACTION_LINK:
1297 flags = XmDROP_LINK;
1300 flags = XmDROP_NOOP;
1304 if (context->actions & GDK_ACTION_MOVE)
1305 flags |= XmDROP_MOVE << 8;
1306 if (context->actions & GDK_ACTION_COPY)
1307 flags |= XmDROP_COPY << 8;
1308 if (context->actions & GDK_ACTION_LINK)
1309 flags |= XmDROP_LINK << 8;
1317 motif_set_targets (GdkDragContext *context)
1319 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1320 MotifDragInitiatorInfo info;
1322 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1324 info.byte_order = local_byte_order;
1325 info.protocol_version = 0;
1327 info.targets_index = motif_add_to_target_table (display, context->targets);
1332 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1334 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1335 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1339 info.selection_atom = private->motif_selection;
1341 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1342 GDK_DRAWABLE_XID (context->source_window),
1343 private->motif_selection,
1344 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1346 (guchar *)&info, sizeof (info));
1348 private->motif_targets_set = 1;
1352 motif_check_dest (GdkDisplay *display,
1355 gboolean retval = FALSE;
1357 MotifDragReceiverInfo *info;
1360 unsigned long nitems, after;
1361 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1363 gdk_error_trap_push ();
1364 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1365 motif_drag_receiver_info_atom,
1366 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1367 &type, &format, &nitems, &after,
1370 if (gdk_error_trap_pop() == 0)
1374 info = (MotifDragReceiverInfo *)data;
1376 if ((format == 8) && (nitems == sizeof(*info)))
1378 if ((info->protocol_version == 0) &&
1379 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1380 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1381 (info->protocol_style == XmDRAG_DYNAMIC)))
1387 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1394 return retval ? win : None;
1398 motif_send_enter (GdkDragContext *context,
1401 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1402 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1405 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1406 return; /* Motif Dnd requires getting properties on the root window */
1408 xev.xclient.type = ClientMessage;
1409 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1410 xev.xclient.format = 8;
1411 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1413 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1414 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1415 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1416 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1417 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1419 if (!private->motif_targets_set)
1420 motif_set_targets (context);
1422 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1423 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1425 if (!_gdk_send_xevent (display,
1426 GDK_DRAWABLE_XID (context->dest_window),
1429 g_message ("Send event to %lx failed",
1430 GDK_DRAWABLE_XID (context->dest_window)));
1434 motif_send_leave (GdkDragContext *context,
1437 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1440 xev.xclient.type = ClientMessage;
1441 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1442 xev.xclient.format = 8;
1443 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1445 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1446 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1447 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1448 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1449 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1450 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1451 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1453 if (!_gdk_send_xevent (display,
1454 GDK_DRAWABLE_XID (context->dest_window),
1457 g_message ("Send event to %lx failed",
1458 GDK_DRAWABLE_XID (context->dest_window)));
1462 motif_send_motion (GdkDragContext *context,
1465 GdkDragAction action,
1468 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1469 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1473 xev.xclient.type = ClientMessage;
1474 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1475 xev.xclient.format = 8;
1476 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1478 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1479 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1480 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1481 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1482 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1484 if ((context->suggested_action != private->old_action) ||
1485 (context->actions != private->old_actions))
1487 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1489 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1494 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1496 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1497 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1499 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1503 if (!_gdk_send_xevent (display,
1504 GDK_DRAWABLE_XID (context->dest_window),
1507 g_message ("Send event to %lx failed",
1508 GDK_DRAWABLE_XID (context->dest_window)));
1514 motif_send_drop (GdkDragContext *context, guint32 time)
1516 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1517 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1520 xev.xclient.type = ClientMessage;
1521 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1522 xev.xclient.format = 8;
1523 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1525 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1526 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1527 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1528 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1530 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1531 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1533 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1534 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1536 if (!_gdk_send_xevent (display,
1537 GDK_DRAWABLE_XID (context->dest_window),
1540 g_message ("Send event to %lx failed",
1541 GDK_DRAWABLE_XID (context->dest_window)));
1547 motif_read_initiator_info (GdkDisplay *display,
1548 Window source_window,
1559 MotifDragInitiatorInfo *initiator_info;
1561 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1563 gdk_error_trap_push ();
1564 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1565 0, sizeof(*initiator_info), FALSE,
1566 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1567 &type, &format, &nitems, &bytes_after,
1570 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1572 g_warning ("Error reading initiator info\n");
1576 initiator_info = (MotifDragInitiatorInfo *)data;
1578 motif_read_target_table (display);
1580 initiator_info->targets_index =
1581 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1582 initiator_info->selection_atom =
1583 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1585 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1587 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1588 XFree (initiator_info);
1592 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1597 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1598 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1599 tmp_list = tmp_list->prev;
1602 #ifdef G_ENABLE_DEBUG
1603 if (_gdk_debug_flags & GDK_DEBUG_DND)
1604 print_target_list (*targets);
1605 #endif /* G_ENABLE_DEBUG */
1607 *selection = initiator_info->selection_atom;
1609 XFree (initiator_info);
1614 static GdkDragContext *
1615 motif_drag_context_new (GdkWindow *dest_window,
1617 guint32 source_window,
1620 GdkDragContext *new_context;
1621 GdkDragContextPrivateX11 *private;
1622 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1623 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1625 /* FIXME, current_dest_drag really shouldn't be NULL'd
1626 * if we error below.
1628 if (display_x11->current_dest_drag != NULL)
1630 if (timestamp >= display_x11->current_dest_drag->start_time)
1632 g_object_unref (display_x11->current_dest_drag);
1633 display_x11->current_dest_drag = NULL;
1639 new_context = gdk_drag_context_new ();
1640 private = PRIVATE_DATA (new_context);
1642 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1643 new_context->is_source = FALSE;
1645 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1646 if (new_context->source_window)
1647 g_object_ref (new_context->source_window);
1650 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1651 if (!new_context->source_window)
1653 g_object_unref (new_context);
1658 new_context->dest_window = dest_window;
1659 g_object_ref (dest_window);
1660 new_context->start_time = timestamp;
1662 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1665 &new_context->targets,
1666 &private->motif_selection))
1668 g_object_unref (new_context);
1676 * The MOTIF drag protocol has no real provisions for distinguishing
1677 * multiple simultaneous drops. If the sources grab the pointer
1678 * when doing drags, that shouldn't happen, in any case. If it
1679 * does, we can't do much except hope for the best.
1682 static GdkFilterReturn
1683 motif_top_level_enter (GdkEvent *event,
1686 guint32 source_window,
1689 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1690 GdkDragContext *new_context;
1692 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1693 flags, timestamp, source_window, atom));
1695 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1697 return GDK_FILTER_REMOVE;
1699 event->dnd.type = GDK_DRAG_ENTER;
1700 event->dnd.context = new_context;
1701 g_object_ref (new_context);
1703 display_x11->current_dest_drag = new_context;
1705 return GDK_FILTER_TRANSLATE;
1708 static GdkFilterReturn
1709 motif_top_level_leave (GdkEvent *event,
1713 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1715 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1718 if ((display_x11->current_dest_drag != NULL) &&
1719 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1720 (timestamp >= display_x11->current_dest_drag->start_time))
1722 event->dnd.type = GDK_DRAG_LEAVE;
1723 /* Pass ownership of context to the event */
1724 event->dnd.context = display_x11->current_dest_drag;
1726 display_x11->current_dest_drag = NULL;
1728 return GDK_FILTER_TRANSLATE;
1731 return GDK_FILTER_REMOVE;
1734 static GdkFilterReturn
1735 motif_motion (GdkEvent *event,
1741 GdkDragContextPrivateX11 *private;
1742 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1744 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1745 flags, timestamp, x_root, y_root));
1747 if ((display_x11->current_dest_drag != NULL) &&
1748 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1749 (timestamp >= display_x11->current_dest_drag->start_time))
1751 private = PRIVATE_DATA (display_x11->current_dest_drag);
1753 event->dnd.type = GDK_DRAG_MOTION;
1754 event->dnd.context = display_x11->current_dest_drag;
1755 g_object_ref (display_x11->current_dest_drag);
1757 event->dnd.time = timestamp;
1759 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1761 event->dnd.x_root = x_root;
1762 event->dnd.y_root = y_root;
1764 private->last_x = x_root;
1765 private->last_y = y_root;
1767 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1769 return GDK_FILTER_TRANSLATE;
1772 return GDK_FILTER_REMOVE;
1775 static GdkFilterReturn
1776 motif_operation_changed (GdkEvent *event,
1780 GdkDragContextPrivateX11 *private;
1781 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1782 GDK_NOTE(DND, g_message ("Motif DND operation changed: 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_MOTION;
1790 event->dnd.send_event = FALSE;
1791 event->dnd.context = display_x11->current_dest_drag;
1792 g_object_ref (display_x11->current_dest_drag);
1794 event->dnd.time = timestamp;
1795 private = PRIVATE_DATA (display_x11->current_dest_drag);
1797 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1799 event->dnd.x_root = private->last_x;
1800 event->dnd.y_root = private->last_y;
1802 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1804 return GDK_FILTER_TRANSLATE;
1807 return GDK_FILTER_REMOVE;
1810 static GdkFilterReturn
1811 motif_drop_start (GdkEvent *event,
1814 guint32 source_window,
1819 GdkDragContext *new_context;
1820 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1822 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1823 flags, timestamp, x_root, y_root, source_window, atom));
1825 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1827 return GDK_FILTER_REMOVE;
1829 motif_dnd_translate_flags (new_context, flags);
1831 event->dnd.type = GDK_DROP_START;
1832 event->dnd.context = new_context;
1833 event->dnd.time = timestamp;
1834 event->dnd.x_root = x_root;
1835 event->dnd.y_root = y_root;
1837 gdk_x11_window_set_user_time (event->any.window, timestamp);
1839 g_object_ref (new_context);
1840 display_x11->current_dest_drag = new_context;
1842 return GDK_FILTER_TRANSLATE;
1845 static GdkFilterReturn
1846 motif_drag_status (GdkEvent *event,
1850 GdkDragContext *context;
1851 GdkDisplay *display;
1854 g_message ("Motif status message: flags %x", flags));
1856 display = gdk_drawable_get_display (event->any.window);
1858 return GDK_FILTER_REMOVE;
1860 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1864 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1865 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1866 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1867 private->drag_status = GDK_DRAG_STATUS_DRAG;
1869 event->dnd.type = GDK_DRAG_STATUS;
1870 event->dnd.send_event = FALSE;
1871 event->dnd.context = context;
1872 g_object_ref (context);
1874 event->dnd.time = timestamp;
1876 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1878 switch (flags & 0x000f)
1881 context->action = 0;
1884 context->action = GDK_ACTION_MOVE;
1887 context->action = GDK_ACTION_COPY;
1890 context->action = GDK_ACTION_LINK;
1895 context->action = 0;
1897 return GDK_FILTER_TRANSLATE;
1899 return GDK_FILTER_REMOVE;
1902 static GdkFilterReturn
1903 motif_dnd_filter (GdkXEvent *xev,
1907 XEvent *xevent = (XEvent *)xev;
1912 guint32 source_window;
1914 gint16 x_root, y_root;
1917 if (!event->any.window ||
1918 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1919 return GDK_FILTER_CONTINUE; /* Not for us */
1921 /* First read some fields common to all Motif DND messages */
1923 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1924 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1925 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1927 is_reply = ((reason & 0x80) != 0);
1929 switch (reason & 0x7f)
1931 case XmTOP_LEVEL_ENTER:
1932 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1933 atom = MOTIF_UNPACK_LONG (xevent, 3);
1934 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1935 case XmTOP_LEVEL_LEAVE:
1936 return motif_top_level_leave (event, flags, timestamp);
1939 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1940 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1943 return motif_motion (event, flags, timestamp, x_root, y_root);
1945 return motif_drag_status (event, flags, timestamp);
1947 case XmDROP_SITE_ENTER:
1948 return motif_drag_status (event, flags, timestamp);
1950 case XmDROP_SITE_LEAVE:
1951 return motif_drag_status (event,
1952 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1955 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1956 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1957 atom = MOTIF_UNPACK_LONG (xevent, 3);
1958 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1961 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1964 case XmOPERATION_CHANGED:
1966 return motif_operation_changed (event, flags, timestamp);
1968 return motif_drag_status (event, flags, timestamp);
1971 /* To the best of my knowledge, these next two messages are
1972 * not part of the protocol, though they are defined in
1976 case XmDRAG_DROP_FINISH:
1980 return GDK_FILTER_REMOVE;
1983 /*************************************************************
1984 ***************************** XDND **************************
1985 *************************************************************/
1987 /* Utility functions */
1992 GdkDragAction action;
1993 } xdnd_actions_table[] = {
1994 { "XdndActionCopy", None, GDK_ACTION_COPY },
1995 { "XdndActionMove", None, GDK_ACTION_MOVE },
1996 { "XdndActionLink", None, GDK_ACTION_LINK },
1997 { "XdndActionAsk", None, GDK_ACTION_ASK },
1998 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2001 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
2002 static gboolean xdnd_actions_initialized = FALSE;
2005 xdnd_initialize_actions (void)
2009 xdnd_actions_initialized = TRUE;
2010 for (i=0; i < xdnd_n_actions; i++)
2011 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2014 static GdkDragAction
2015 xdnd_action_from_atom (GdkDisplay *display,
2024 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2026 if (!xdnd_actions_initialized)
2027 xdnd_initialize_actions();
2029 for (i=0; i<xdnd_n_actions; i++)
2030 if (atom == xdnd_actions_table[i].atom)
2031 return xdnd_actions_table[i].action;
2037 xdnd_action_to_atom (GdkDisplay *display,
2038 GdkDragAction action)
2042 if (!xdnd_actions_initialized)
2043 xdnd_initialize_actions();
2045 for (i=0; i<xdnd_n_actions; i++)
2046 if (action == xdnd_actions_table[i].action)
2047 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2054 static GdkFilterReturn
2055 xdnd_status_filter (GdkXEvent *xev,
2059 GdkDisplay *display;
2060 XEvent *xevent = (XEvent *)xev;
2061 guint32 dest_window = xevent->xclient.data.l[0];
2062 guint32 flags = xevent->xclient.data.l[1];
2063 Atom action = xevent->xclient.data.l[4];
2064 GdkDragContext *context;
2066 if (!event->any.window ||
2067 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2068 return GDK_FILTER_CONTINUE; /* Not for us */
2071 g_message ("XdndStatus: dest_window: %#x action: %ld",
2072 dest_window, action));
2074 display = gdk_drawable_get_display (event->any.window);
2075 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2079 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2080 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2081 private->drag_status = GDK_DRAG_STATUS_DRAG;
2083 event->dnd.send_event = FALSE;
2084 event->dnd.type = GDK_DRAG_STATUS;
2085 event->dnd.context = context;
2086 g_object_ref (context);
2088 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2089 if (!(action != 0) != !(flags & 1))
2092 g_warning ("Received status event with flags not corresponding to action!\n"));
2096 context->action = xdnd_action_from_atom (display, action);
2098 return GDK_FILTER_TRANSLATE;
2101 return GDK_FILTER_REMOVE;
2104 static GdkFilterReturn
2105 xdnd_finished_filter (GdkXEvent *xev,
2109 GdkDisplay *display;
2110 XEvent *xevent = (XEvent *)xev;
2111 guint32 dest_window = xevent->xclient.data.l[0];
2112 GdkDragContext *context;
2113 GdkDragContextPrivateX11 *private;
2115 if (!event->any.window ||
2116 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2117 return GDK_FILTER_CONTINUE; /* Not for us */
2120 g_message ("XdndFinished: dest_window: %#x", dest_window));
2122 display = gdk_drawable_get_display (event->any.window);
2123 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2127 private = PRIVATE_DATA (context);
2128 if (private->version == 5)
2129 private->drop_failed = xevent->xclient.data.l[1] == 0;
2131 event->dnd.type = GDK_DROP_FINISHED;
2132 event->dnd.context = context;
2133 g_object_ref (context);
2135 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2137 return GDK_FILTER_TRANSLATE;
2140 return GDK_FILTER_REMOVE;
2144 xdnd_set_targets (GdkDragContext *context)
2146 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2148 GList *tmp_list = context->targets;
2150 gint n_atoms = g_list_length (context->targets);
2151 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2153 atomlist = g_new (Atom, n_atoms);
2157 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2158 tmp_list = tmp_list->next;
2162 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2163 GDK_DRAWABLE_XID (context->source_window),
2164 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2165 XA_ATOM, 32, PropModeReplace,
2166 (guchar *)atomlist, n_atoms);
2170 private->xdnd_targets_set = 1;
2174 xdnd_set_actions (GdkDragContext *context)
2176 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2181 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2183 if (!xdnd_actions_initialized)
2184 xdnd_initialize_actions();
2186 actions = context->actions;
2188 for (i=0; i<xdnd_n_actions; i++)
2190 if (actions & xdnd_actions_table[i].action)
2192 actions &= ~xdnd_actions_table[i].action;
2197 atomlist = g_new (Atom, n_atoms);
2199 actions = context->actions;
2201 for (i=0; i<xdnd_n_actions; i++)
2203 if (actions & xdnd_actions_table[i].action)
2205 actions &= ~xdnd_actions_table[i].action;
2206 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2211 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2212 GDK_DRAWABLE_XID (context->source_window),
2213 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2214 XA_ATOM, 32, PropModeReplace,
2215 (guchar *)atomlist, n_atoms);
2219 private->xdnd_actions_set = TRUE;
2220 private->xdnd_actions = context->actions;
2224 send_client_message_async_cb (Window window,
2228 GdkDragContext *context = data;
2230 g_message ("Got async callback for #%lx, success = %d",
2233 /* On failure, we immediately continue with the protocol
2234 * so we don't end up blocking for a timeout
2237 context->dest_window &&
2238 window == GDK_WINDOW_XID (context->dest_window))
2240 GdkEvent temp_event;
2241 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2243 g_object_unref (context->dest_window);
2244 context->dest_window = NULL;
2245 context->action = 0;
2247 private->drag_status = GDK_DRAG_STATUS_DRAG;
2249 temp_event.dnd.type = GDK_DRAG_STATUS;
2250 temp_event.dnd.window = context->source_window;
2251 temp_event.dnd.send_event = TRUE;
2252 temp_event.dnd.context = context;
2253 temp_event.dnd.time = GDK_CURRENT_TIME;
2255 gdk_event_put (&temp_event);
2258 g_object_unref (context);
2263 gdk_drag_context_get_display (GdkDragContext *context)
2265 if (context->source_window)
2266 return GDK_DRAWABLE_DISPLAY (context->source_window);
2267 else if (context->dest_window)
2268 return GDK_DRAWABLE_DISPLAY (context->dest_window);
2270 g_assert_not_reached ();
2275 send_client_message_async (GdkDragContext *context,
2279 XClientMessageEvent *event_send)
2281 GdkDisplay *display = gdk_drag_context_get_display (context);
2283 g_object_ref (context);
2285 _gdk_x11_send_client_message_async (display, window,
2286 propagate, event_mask, event_send,
2287 send_client_message_async_cb, context);
2291 xdnd_send_xevent (GdkDragContext *context,
2296 GdkDisplay *display = gdk_drag_context_get_display (context);
2300 g_assert (event_send->xany.type == ClientMessage);
2302 /* We short-circuit messages to ourselves */
2303 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2307 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2309 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2310 event_send->xclient.message_type)
2312 GdkEvent temp_event;
2313 temp_event.any.window = window;
2315 if ((*xdnd_filters[i].func) (event_send, &temp_event, NULL) == GDK_FILTER_TRANSLATE)
2317 gdk_event_put (&temp_event);
2318 g_object_unref (temp_event.dnd.context);
2326 xwindow = GDK_WINDOW_XWINDOW (window);
2328 if (_gdk_x11_display_is_root_window (display, xwindow))
2329 event_mask = ButtonPressMask;
2333 send_client_message_async (context, xwindow, propagate, event_mask,
2334 &event_send->xclient);
2340 xdnd_send_enter (GdkDragContext *context)
2343 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2344 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2346 xev.xclient.type = ClientMessage;
2347 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2348 xev.xclient.format = 32;
2349 xev.xclient.window = private->drop_xid ?
2351 GDK_DRAWABLE_XID (context->dest_window);
2352 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2353 xev.xclient.data.l[1] = (private->version << 24); /* version */
2354 xev.xclient.data.l[2] = 0;
2355 xev.xclient.data.l[3] = 0;
2356 xev.xclient.data.l[4] = 0;
2359 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2360 GDK_DRAWABLE_XID (context->source_window), private->version));
2361 if (g_list_length (context->targets) > 3)
2363 if (!private->xdnd_targets_set)
2364 xdnd_set_targets (context);
2365 xev.xclient.data.l[1] |= 1;
2369 GList *tmp_list = context->targets;
2374 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2375 GDK_POINTER_TO_ATOM (tmp_list->data));
2376 tmp_list = tmp_list->next;
2381 if (!xdnd_send_xevent (context, context->dest_window,
2385 g_message ("Send event to %lx failed",
2386 GDK_DRAWABLE_XID (context->dest_window)));
2387 g_object_unref (context->dest_window);
2388 context->dest_window = NULL;
2393 xdnd_send_leave (GdkDragContext *context)
2395 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2398 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2400 xev.xclient.type = ClientMessage;
2401 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2402 xev.xclient.format = 32;
2403 xev.xclient.window = private->drop_xid ?
2405 GDK_DRAWABLE_XID (context->dest_window);
2406 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2407 xev.xclient.data.l[1] = 0;
2408 xev.xclient.data.l[2] = 0;
2409 xev.xclient.data.l[3] = 0;
2410 xev.xclient.data.l[4] = 0;
2412 if (!xdnd_send_xevent (context, context->dest_window,
2416 g_message ("Send event to %lx failed",
2417 GDK_DRAWABLE_XID (context->dest_window)));
2418 g_object_unref (context->dest_window);
2419 context->dest_window = NULL;
2424 xdnd_send_drop (GdkDragContext *context, guint32 time)
2426 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2427 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2430 xev.xclient.type = ClientMessage;
2431 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2432 xev.xclient.format = 32;
2433 xev.xclient.window = private->drop_xid ?
2435 GDK_DRAWABLE_XID (context->dest_window);
2436 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2437 xev.xclient.data.l[1] = 0;
2438 xev.xclient.data.l[2] = time;
2439 xev.xclient.data.l[3] = 0;
2440 xev.xclient.data.l[4] = 0;
2442 if (!xdnd_send_xevent (context, context->dest_window,
2446 g_message ("Send event to %lx failed",
2447 GDK_DRAWABLE_XID (context->dest_window)));
2448 g_object_unref (context->dest_window);
2449 context->dest_window = NULL;
2454 xdnd_send_motion (GdkDragContext *context,
2457 GdkDragAction action,
2460 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2461 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2464 xev.xclient.type = ClientMessage;
2465 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2466 xev.xclient.format = 32;
2467 xev.xclient.window = private->drop_xid ?
2469 GDK_DRAWABLE_XID (context->dest_window);
2470 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2471 xev.xclient.data.l[1] = 0;
2472 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2473 xev.xclient.data.l[3] = time;
2474 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2476 if (!xdnd_send_xevent (context, context->dest_window,
2480 g_message ("Send event to %lx failed",
2481 GDK_DRAWABLE_XID (context->dest_window)));
2482 g_object_unref (context->dest_window);
2483 context->dest_window = NULL;
2485 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2489 xdnd_check_dest (GdkDisplay *display,
2491 guint *xdnd_version)
2493 gboolean retval = FALSE;
2496 unsigned long nitems, after;
2501 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2502 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2506 gdk_error_trap_push ();
2508 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2510 1, False, AnyPropertyType,
2511 &type, &format, &nitems, &after,
2516 proxy_data = (Window *)data;
2518 if ((format == 32) && (nitems == 1))
2520 proxy = *proxy_data;
2524 g_warning ("Invalid XdndProxy "
2525 "property on window %ld\n", win));
2530 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2532 1, False, AnyPropertyType,
2533 &type, &format, &nitems, &after,
2534 &data) == Success) &&
2537 version = (Atom *)data;
2539 if ((format == 32) && (nitems == 1))
2544 *xdnd_version = *version;
2548 g_warning ("Invalid XdndAware "
2549 "property on window %ld\n", win));
2555 gdk_error_trap_pop ();
2557 return retval ? (proxy ? proxy : win) : None;
2563 xdnd_read_actions (GdkDragContext *context)
2565 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2568 gulong nitems, after;
2574 PRIVATE_DATA (context)->xdnd_have_actions = FALSE;
2576 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2578 /* Get the XdndActionList, if set */
2580 gdk_error_trap_push ();
2582 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2583 GDK_DRAWABLE_XID (context->source_window),
2584 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2586 False, XA_ATOM, &type, &format, &nitems,
2587 &after, &data) == Success &&
2590 atoms = (Atom *)data;
2592 context->actions = 0;
2594 for (i=0; i<nitems; i++)
2595 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2597 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2599 #ifdef G_ENABLE_DEBUG
2600 if (_gdk_debug_flags & GDK_DEBUG_DND)
2602 GString *action_str = g_string_new (NULL);
2603 if (context->actions & GDK_ACTION_MOVE)
2604 g_string_append(action_str, "MOVE ");
2605 if (context->actions & GDK_ACTION_COPY)
2606 g_string_append(action_str, "COPY ");
2607 if (context->actions & GDK_ACTION_LINK)
2608 g_string_append(action_str, "LINK ");
2609 if (context->actions & GDK_ACTION_ASK)
2610 g_string_append(action_str, "ASK ");
2612 g_message("Xdnd actions = %s", action_str->str);
2613 g_string_free (action_str, TRUE);
2615 #endif /* G_ENABLE_DEBUG */
2622 gdk_error_trap_pop ();
2628 GdkDragContext *source_context;
2630 source_context = gdk_drag_context_find (display, TRUE,
2631 GDK_DRAWABLE_XID (context->source_window),
2632 GDK_DRAWABLE_XID (context->dest_window));
2636 context->actions = source_context->actions;
2637 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2642 /* We have to make sure that the XdndActionList we keep internally
2643 * is up to date with the XdndActionList on the source window
2644 * because we get no notification, because Xdnd wasn't meant
2645 * to continually send actions. So we select on PropertyChangeMask
2646 * and add this filter.
2648 static GdkFilterReturn
2649 xdnd_source_window_filter (GdkXEvent *xev,
2653 XEvent *xevent = (XEvent *)xev;
2654 GdkDragContext *context = cb_data;
2655 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2657 if ((xevent->xany.type == PropertyNotify) &&
2658 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2660 xdnd_read_actions (context);
2662 return GDK_FILTER_REMOVE;
2665 return GDK_FILTER_CONTINUE;
2669 xdnd_manage_source_filter (GdkDragContext *context,
2671 gboolean add_filter)
2673 if (!GDK_WINDOW_DESTROYED (window) &&
2674 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2676 gdk_error_trap_push ();
2680 gdk_window_set_events (window,
2681 gdk_window_get_events (window) |
2682 GDK_PROPERTY_CHANGE_MASK);
2683 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2687 gdk_window_remove_filter (window,
2688 xdnd_source_window_filter,
2690 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2691 * but we might want it for other reasons. (Like
2692 * INCR selection transactions).
2696 gdk_display_sync (gdk_drawable_get_display (window));
2697 gdk_error_trap_pop ();
2702 base_precache_atoms (GdkDisplay *display)
2704 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2706 if (!display_x11->base_dnd_atoms_precached)
2708 static const char *const precache_atoms[] = {
2709 "ENLIGHTENMENT_DESKTOP",
2713 "_MOTIF_DRAG_RECEIVER_INFO"
2716 _gdk_x11_precache_atoms (display,
2717 precache_atoms, G_N_ELEMENTS (precache_atoms));
2719 display_x11->base_dnd_atoms_precached = TRUE;
2724 xdnd_precache_atoms (GdkDisplay *display)
2726 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2728 if (!display_x11->xdnd_atoms_precached)
2730 static const char *const precache_atoms[] = {
2736 "XdndActionPrivate",
2747 _gdk_x11_precache_atoms (display,
2748 precache_atoms, G_N_ELEMENTS (precache_atoms));
2750 display_x11->xdnd_atoms_precached = TRUE;
2754 static GdkFilterReturn
2755 xdnd_enter_filter (GdkXEvent *xev,
2759 GdkDisplay *display;
2760 GdkDisplayX11 *display_x11;
2761 XEvent *xevent = (XEvent *)xev;
2762 GdkDragContext *new_context;
2767 gulong nitems, after;
2771 guint32 source_window;
2775 if (!event->any.window ||
2776 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2777 return GDK_FILTER_CONTINUE; /* Not for us */
2779 source_window = xevent->xclient.data.l[0];
2780 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2781 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2783 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2784 display_x11 = GDK_DISPLAY_X11 (display);
2786 xdnd_precache_atoms (display);
2789 g_message ("XdndEnter: source_window: %#x, version: %#x",
2790 source_window, version));
2794 /* Old source ignore */
2795 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2796 return GDK_FILTER_REMOVE;
2799 if (display_x11->current_dest_drag != NULL)
2801 g_object_unref (display_x11->current_dest_drag);
2802 display_x11->current_dest_drag = NULL;
2805 new_context = gdk_drag_context_new ();
2806 new_context->protocol = GDK_DRAG_PROTO_XDND;
2807 PRIVATE_DATA(new_context)->version = version;
2809 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2810 if (new_context->source_window)
2811 g_object_ref (new_context->source_window);
2814 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2815 if (!new_context->source_window)
2817 g_object_unref (new_context);
2818 return GDK_FILTER_REMOVE;
2821 new_context->dest_window = event->any.window;
2822 g_object_ref (new_context->dest_window);
2824 new_context->targets = NULL;
2827 gdk_error_trap_push ();
2828 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2830 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2832 False, XA_ATOM, &type, &format, &nitems,
2835 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2837 g_object_unref (new_context);
2842 return GDK_FILTER_REMOVE;
2845 atoms = (Atom *)data;
2847 for (i=0; i<nitems; i++)
2848 new_context->targets =
2849 g_list_append (new_context->targets,
2850 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2858 if (xevent->xclient.data.l[2+i])
2859 new_context->targets =
2860 g_list_append (new_context->targets,
2861 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2862 xevent->xclient.data.l[2+i])));
2865 #ifdef G_ENABLE_DEBUG
2866 if (_gdk_debug_flags & GDK_DEBUG_DND)
2867 print_target_list (new_context->targets);
2868 #endif /* G_ENABLE_DEBUG */
2870 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2871 xdnd_read_actions (new_context);
2873 event->dnd.type = GDK_DRAG_ENTER;
2874 event->dnd.context = new_context;
2875 g_object_ref (new_context);
2877 display_x11->current_dest_drag = new_context;
2879 return GDK_FILTER_TRANSLATE;
2882 static GdkFilterReturn
2883 xdnd_leave_filter (GdkXEvent *xev,
2887 XEvent *xevent = (XEvent *)xev;
2888 guint32 source_window = xevent->xclient.data.l[0];
2889 GdkDisplay *display;
2890 GdkDisplayX11 *display_x11;
2892 if (!event->any.window ||
2893 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2894 return GDK_FILTER_CONTINUE; /* Not for us */
2897 g_message ("XdndLeave: source_window: %#x",
2900 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2901 display_x11 = GDK_DISPLAY_X11 (display);
2903 xdnd_precache_atoms (display);
2905 if ((display_x11->current_dest_drag != NULL) &&
2906 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2907 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2909 event->dnd.type = GDK_DRAG_LEAVE;
2910 /* Pass ownership of context to the event */
2911 event->dnd.context = display_x11->current_dest_drag;
2913 display_x11->current_dest_drag = NULL;
2915 return GDK_FILTER_TRANSLATE;
2918 return GDK_FILTER_REMOVE;
2921 static GdkFilterReturn
2922 xdnd_position_filter (GdkXEvent *xev,
2926 XEvent *xevent = (XEvent *)xev;
2927 guint32 source_window = xevent->xclient.data.l[0];
2928 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2929 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2930 guint32 time = xevent->xclient.data.l[3];
2931 Atom action = xevent->xclient.data.l[4];
2933 GdkDisplay *display;
2934 GdkDisplayX11 *display_x11;
2936 if (!event->any.window ||
2937 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2938 return GDK_FILTER_CONTINUE; /* Not for us */
2941 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2942 source_window, x_root, y_root, time, action));
2944 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2945 display_x11 = GDK_DISPLAY_X11 (display);
2947 xdnd_precache_atoms (display);
2949 if ((display_x11->current_dest_drag != NULL) &&
2950 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2951 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2953 event->dnd.type = GDK_DRAG_MOTION;
2954 event->dnd.context = display_x11->current_dest_drag;
2955 g_object_ref (display_x11->current_dest_drag);
2957 event->dnd.time = time;
2959 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2961 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2962 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2964 event->dnd.x_root = x_root;
2965 event->dnd.y_root = y_root;
2967 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2968 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
2970 return GDK_FILTER_TRANSLATE;
2973 return GDK_FILTER_REMOVE;
2976 static GdkFilterReturn
2977 xdnd_drop_filter (GdkXEvent *xev,
2981 XEvent *xevent = (XEvent *)xev;
2982 guint32 source_window = xevent->xclient.data.l[0];
2983 guint32 time = xevent->xclient.data.l[2];
2984 GdkDisplay *display;
2985 GdkDisplayX11 *display_x11;
2987 if (!event->any.window ||
2988 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2989 return GDK_FILTER_CONTINUE; /* Not for us */
2992 g_message ("XdndDrop: source_window: %#x time: %d",
2993 source_window, time));
2995 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2996 display_x11 = GDK_DISPLAY_X11 (display);
2998 xdnd_precache_atoms (display);
3000 if ((display_x11->current_dest_drag != NULL) &&
3001 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3002 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
3004 GdkDragContextPrivateX11 *private;
3005 private = PRIVATE_DATA (display_x11->current_dest_drag);
3007 event->dnd.type = GDK_DROP_START;
3009 event->dnd.context = display_x11->current_dest_drag;
3010 g_object_ref (display_x11->current_dest_drag);
3012 event->dnd.time = time;
3013 event->dnd.x_root = private->last_x;
3014 event->dnd.y_root = private->last_y;
3016 gdk_x11_window_set_user_time (event->any.window, time);
3018 return GDK_FILTER_TRANSLATE;
3021 return GDK_FILTER_REMOVE;
3024 /*************************************************************
3025 ************************** Public API ***********************
3026 *************************************************************/
3028 _gdk_dnd_init (GdkDisplay *display)
3033 gdk_display_add_client_message_filter (
3035 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
3036 motif_dnd_filter, NULL);
3038 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3040 gdk_display_add_client_message_filter (
3042 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
3043 xdnd_filters[i].func, NULL);
3050 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
3052 if (context->dest_window)
3054 switch (context->protocol)
3056 case GDK_DRAG_PROTO_MOTIF:
3057 motif_send_leave (context, time);
3059 case GDK_DRAG_PROTO_XDND:
3060 xdnd_send_leave (context);
3062 case GDK_DRAG_PROTO_ROOTWIN:
3063 case GDK_DRAG_PROTO_NONE:
3068 g_object_unref (context->dest_window);
3069 context->dest_window = NULL;
3075 * @window: the source window for this drag.
3076 * @targets: the offered targets, as list of #GdkAtom<!-- -->s
3078 * Starts a drag and creates a new drag context for it.
3080 * This function is called by the drag source.
3082 * Return value: a newly created #GdkDragContext.
3085 gdk_drag_begin (GdkWindow *window,
3088 GdkDragContext *new_context;
3090 g_return_val_if_fail (window != NULL, NULL);
3091 g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), NULL);
3093 new_context = gdk_drag_context_new ();
3094 new_context->is_source = TRUE;
3095 new_context->source_window = window;
3096 g_object_ref (window);
3098 new_context->targets = g_list_copy (targets);
3099 precache_target_list (new_context);
3101 new_context->actions = 0;
3106 static GdkNativeWindow
3107 _gdk_drag_get_protocol_for_display (GdkDisplay *display,
3108 GdkNativeWindow xid,
3109 GdkDragProtocol *protocol,
3114 GdkNativeWindow retval;
3115 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
3117 base_precache_atoms (display);
3119 /* Check for a local drag
3121 window = gdk_window_lookup_for_display (display, xid);
3123 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3125 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3127 *protocol = GDK_DRAG_PROTO_XDND;
3129 xdnd_precache_atoms (display);
3130 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3133 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3135 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3136 GDK_NOTE (DND, g_message ("Entering root window\n"));
3140 else if ((retval = xdnd_check_dest (display, xid, version)))
3142 *protocol = GDK_DRAG_PROTO_XDND;
3143 xdnd_precache_atoms (display);
3144 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3147 else if ((retval = motif_check_dest (display, xid)))
3149 *protocol = GDK_DRAG_PROTO_MOTIF;
3150 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3155 /* Check if this is a root window */
3157 gboolean rootwin = FALSE;
3160 unsigned long nitems, after;
3161 unsigned char *data;
3163 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3166 gdk_error_trap_push ();
3170 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3171 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
3172 0, 0, False, AnyPropertyType,
3173 &type, &format, &nitems, &after, &data) == Success &&
3181 /* Until I find out what window manager the next one is for,
3182 * I'm leaving it commented out. It's supported in the
3183 * xscreensaver sources, though.
3188 if (XGetWindowProperty (gdk_display, win,
3189 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
3190 0, 0, False, AnyPropertyType,
3191 &type, &format, &nitems, &data) &&
3200 gdk_error_trap_pop ();
3204 GDK_NOTE (DND, g_message ("Entering root window\n"));
3205 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3210 *protocol = GDK_DRAG_PROTO_NONE;
3212 return 0; /* a.k.a. None */
3216 * gdk_drag_get_protocol_for_display:
3217 * @display: the #GdkDisplay where the destination window resides
3218 * @xid: the windowing system id of the destination window.
3219 * @protocol: location where the supported DND protocol is returned.
3220 * @returns: the windowing system id of the window where the drop should happen. This
3221 * may be @xid or the id of a proxy window, or zero if @xid doesn't
3222 * support Drag and Drop.
3224 * Finds out the DND protocol supported by a window.
3229 gdk_drag_get_protocol_for_display (GdkDisplay *display,
3230 GdkNativeWindow xid,
3231 GdkDragProtocol *protocol)
3233 return _gdk_drag_get_protocol_for_display (display, xid, protocol, NULL);
3236 static GdkWindowCache *
3237 drag_context_find_window_cache (GdkDragContext *context,
3240 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3242 GdkWindowCache *cache;
3244 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
3246 cache = tmp_list->data;
3247 if (cache->screen == screen)
3251 cache = gdk_window_cache_new (screen);
3252 private->window_caches = g_slist_prepend (private->window_caches, cache);
3258 * gdk_drag_find_window_for_screen:
3259 * @context: a #GdkDragContext
3260 * @drag_window: a window which may be at the pointer position, but
3261 * should be ignored, since it is put up by the drag source as an icon.
3262 * @screen: the screen where the destination window is sought.
3263 * @x_root: the x position of the pointer in root coordinates.
3264 * @y_root: the y position of the pointer in root coordinates.
3265 * @dest_window: (out): location to store the destination window in.
3266 * @protocol: (out): location to store the DND protocol in.
3268 * Finds the destination window and DND protocol to use at the
3269 * given pointer position.
3271 * This function is called by the drag source to obtain the
3272 * @dest_window and @protocol parameters for gdk_drag_motion().
3277 gdk_drag_find_window_for_screen (GdkDragContext *context,
3278 GdkWindow *drag_window,
3282 GdkWindow **dest_window,
3283 GdkDragProtocol *protocol)
3285 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3286 GdkWindowCache *window_cache;
3287 GdkDisplay *display;
3290 g_return_if_fail (context != NULL);
3292 display = GDK_WINDOW_DISPLAY (context->source_window);
3294 window_cache = drag_context_find_window_cache (context, screen);
3296 dest = get_client_window_at_coords (window_cache,
3297 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3298 GDK_DRAWABLE_XID (drag_window) : None,
3301 if (private->dest_xid != dest)
3304 private->dest_xid = dest;
3306 /* Check if new destination accepts drags, and which protocol */
3308 /* There is some ugliness here. We actually need to pass
3309 * _three_ pieces of information to drag_motion - dest_window,
3310 * protocol, and the XID of the unproxied window. The first
3311 * two are passed explicitely, the third implicitly through
3312 * protocol->dest_xid.
3314 if ((recipient = _gdk_drag_get_protocol_for_display (display, dest,
3315 protocol, &private->version)))
3317 *dest_window = gdk_window_lookup_for_display (display, recipient);
3319 g_object_ref (*dest_window);
3321 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3324 *dest_window = NULL;
3328 *dest_window = context->dest_window;
3330 g_object_ref (*dest_window);
3331 *protocol = context->protocol;
3337 * @context: a #GdkDragContext.
3338 * @dest_window: the new destination window, obtained by
3339 * gdk_drag_find_window().
3340 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3341 * @x_root: the x position of the pointer in root coordinates.
3342 * @y_root: the y position of the pointer in root coordinates.
3343 * @suggested_action: the suggested action.
3344 * @possible_actions: the possible actions.
3345 * @time_: the timestamp for this operation.
3347 * Updates the drag context when the pointer moves or the
3348 * set of actions changes.
3350 * This function is called by the drag source.
3352 * Return value: FIXME
3355 gdk_drag_motion (GdkDragContext *context,
3356 GdkWindow *dest_window,
3357 GdkDragProtocol protocol,
3360 GdkDragAction suggested_action,
3361 GdkDragAction possible_actions,
3364 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3366 g_return_val_if_fail (context != NULL, FALSE);
3367 g_return_val_if_fail (dest_window == NULL || GDK_WINDOW_IS_X11 (dest_window), FALSE);
3369 private->old_actions = context->actions;
3370 context->actions = possible_actions;
3372 if (private->old_actions != possible_actions)
3373 private->xdnd_actions_set = FALSE;
3375 if (protocol == GDK_DRAG_PROTO_XDND && private->version == 0)
3377 /* This ugly hack is necessary since GTK+ doesn't know about
3378 * the XDND protocol version, and in particular doesn't know
3379 * that gdk_drag_find_window_for_screen() has the side-effect
3380 * of setting private->version, and therefore sometimes call
3381 * gdk_drag_motion() without a prior call to
3382 * gdk_drag_find_window_for_screen(). This happens, e.g.
3383 * when GTK+ is proxying DND events to embedded windows.
3387 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3389 xdnd_check_dest (display,
3390 GDK_DRAWABLE_XID (dest_window),
3395 /* When we have a Xdnd target, make sure our XdndActionList
3396 * matches the current actions;
3398 if (protocol == GDK_DRAG_PROTO_XDND && !private->xdnd_actions_set)
3402 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3403 xdnd_set_actions (context);
3404 else if (context->dest_window == dest_window)
3406 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3407 GdkDragContext *dest_context;
3409 dest_context = gdk_drag_context_find (display, FALSE,
3410 GDK_DRAWABLE_XID (context->source_window),
3411 GDK_DRAWABLE_XID (dest_window));
3415 dest_context->actions = context->actions;
3416 PRIVATE_DATA (dest_context)->xdnd_have_actions = TRUE;
3422 if (context->dest_window != dest_window)
3424 GdkEvent temp_event;
3426 /* Send a leave to the last destination */
3427 gdk_drag_do_leave (context, time);
3428 private->drag_status = GDK_DRAG_STATUS_DRAG;
3430 /* Check if new destination accepts drags, and which protocol */
3434 context->dest_window = dest_window;
3435 private->drop_xid = private->dest_xid;
3436 g_object_ref (context->dest_window);
3437 context->protocol = protocol;
3441 case GDK_DRAG_PROTO_MOTIF:
3442 motif_send_enter (context, time);
3445 case GDK_DRAG_PROTO_XDND:
3446 xdnd_send_enter (context);
3449 case GDK_DRAG_PROTO_ROOTWIN:
3450 case GDK_DRAG_PROTO_NONE:
3454 private->old_action = suggested_action;
3455 context->suggested_action = suggested_action;
3456 private->old_actions = possible_actions;
3460 context->dest_window = NULL;
3461 private->drop_xid = None;
3462 context->action = 0;
3465 /* Push a status event, to let the client know that
3469 temp_event.dnd.type = GDK_DRAG_STATUS;
3470 temp_event.dnd.window = context->source_window;
3471 /* We use this to signal a synthetic status. Perhaps
3472 * we should use an extra field...
3474 temp_event.dnd.send_event = TRUE;
3476 temp_event.dnd.context = context;
3477 temp_event.dnd.time = time;
3479 gdk_event_put (&temp_event);
3483 private->old_action = context->suggested_action;
3484 context->suggested_action = suggested_action;
3487 /* Send a drag-motion event */
3489 private->last_x = x_root;
3490 private->last_y = y_root;
3492 if (context->dest_window)
3494 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3496 switch (context->protocol)
3498 case GDK_DRAG_PROTO_MOTIF:
3499 motif_send_motion (context, x_root, y_root, suggested_action, time);
3502 case GDK_DRAG_PROTO_XDND:
3503 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3506 case GDK_DRAG_PROTO_ROOTWIN:
3508 GdkEvent temp_event;
3509 /* GTK+ traditionally has used application/x-rootwin-drop,
3510 * but the XDND spec specifies x-rootwindow-drop.
3512 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3513 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3515 if (g_list_find (context->targets,
3516 GDK_ATOM_TO_POINTER (target1)) ||
3517 g_list_find (context->targets,
3518 GDK_ATOM_TO_POINTER (target2)))
3519 context->action = context->suggested_action;
3521 context->action = 0;
3523 temp_event.dnd.type = GDK_DRAG_STATUS;
3524 temp_event.dnd.window = context->source_window;
3525 temp_event.dnd.send_event = FALSE;
3526 temp_event.dnd.context = context;
3527 temp_event.dnd.time = time;
3529 gdk_event_put (&temp_event);
3532 case GDK_DRAG_PROTO_NONE:
3533 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3548 * @context: a #GdkDragContext.
3549 * @time_: the timestamp for this operation.
3551 * Drops on the current destination.
3553 * This function is called by the drag source.
3556 gdk_drag_drop (GdkDragContext *context,
3559 g_return_if_fail (context != NULL);
3561 if (context->dest_window)
3563 switch (context->protocol)
3565 case GDK_DRAG_PROTO_MOTIF:
3566 motif_send_leave (context, time);
3567 motif_send_drop (context, time);
3570 case GDK_DRAG_PROTO_XDND:
3571 xdnd_send_drop (context, time);
3574 case GDK_DRAG_PROTO_ROOTWIN:
3575 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3577 case GDK_DRAG_PROTO_NONE:
3578 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3588 * @context: a #GdkDragContext.
3589 * @time_: the timestamp for this operation.
3591 * Aborts a drag without dropping.
3593 * This function is called by the drag source.
3596 gdk_drag_abort (GdkDragContext *context,
3599 g_return_if_fail (context != NULL);
3601 gdk_drag_do_leave (context, time);
3604 /* Destination side */
3608 * @context: a #GdkDragContext.
3609 * @action: the selected action which will be taken when a drop happens,
3610 * or 0 to indicate that a drop will not be accepted.
3611 * @time_: the timestamp for this operation.
3613 * Selects one of the actions offered by the drag source.
3615 * This function is called by the drag destination in response to
3616 * gdk_drag_motion() called by the drag source.
3619 gdk_drag_status (GdkDragContext *context,
3620 GdkDragAction action,
3623 GdkDragContextPrivateX11 *private;
3625 GdkDisplay *display;
3627 g_return_if_fail (context != NULL);
3629 private = PRIVATE_DATA (context);
3630 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3632 context->action = action;
3634 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3636 gboolean need_coords = FALSE;
3638 xev.xclient.type = ClientMessage;
3639 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3640 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3641 xev.xclient.format = 8;
3642 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3644 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3646 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3650 if ((action != 0) != (private->old_action != 0))
3654 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3658 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3662 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3667 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3671 case GDK_ACTION_MOVE:
3672 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3674 case GDK_ACTION_COPY:
3675 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3677 case GDK_ACTION_LINK:
3678 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3681 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3686 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3688 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3690 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3694 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3695 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3698 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3700 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3701 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3703 if (!_gdk_send_xevent (display,
3704 GDK_DRAWABLE_XID (context->source_window),
3707 g_message ("Send event to %lx failed",
3708 GDK_DRAWABLE_XID (context->source_window)));
3710 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3712 xev.xclient.type = ClientMessage;
3713 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3714 xev.xclient.format = 32;
3715 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3717 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3718 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3719 xev.xclient.data.l[2] = 0;
3720 xev.xclient.data.l[3] = 0;
3721 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3723 if (!xdnd_send_xevent (context, context->source_window,
3726 g_message ("Send event to %lx failed",
3727 GDK_DRAWABLE_XID (context->source_window)));
3730 private->old_action = action;
3735 * @context: a #GdkDragContext.
3736 * @ok: %TRUE if the drop is accepted.
3737 * @time_: the timestamp for this operation.
3739 * Accepts or rejects a drop.
3741 * This function is called by the drag destination in response
3742 * to a drop initiated by the drag source.
3745 gdk_drop_reply (GdkDragContext *context,
3749 GdkDragContextPrivateX11 *private;
3751 g_return_if_fail (context != NULL);
3753 private = PRIVATE_DATA (context);
3755 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3757 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3760 xev.xclient.type = ClientMessage;
3761 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3762 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3763 xev.xclient.format = 8;
3765 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3766 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3768 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3769 (XmDROP_SITE_VALID << 4) |
3770 (XmDROP_NOOP << 8) |
3773 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3774 (XmNO_DROP_SITE << 4) |
3775 (XmDROP_NOOP << 8) |
3776 (XmDROP_CANCEL << 12);
3777 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3778 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3779 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3780 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3781 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3783 _gdk_send_xevent (display,
3784 GDK_DRAWABLE_XID (context->source_window),
3791 * @context: a #GtkDragContext.
3792 * @success: %TRUE if the data was successfully received.
3793 * @time_: the timestamp for this operation.
3795 * Ends the drag operation after a drop.
3797 * This function is called by the drag destination.
3800 gdk_drop_finish (GdkDragContext *context,
3804 g_return_if_fail (context != NULL);
3806 if (context->protocol == GDK_DRAG_PROTO_XDND)
3808 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3811 xev.xclient.type = ClientMessage;
3812 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3813 xev.xclient.format = 32;
3814 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3816 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3819 xev.xclient.data.l[1] = 1;
3820 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3825 xev.xclient.data.l[1] = 0;
3826 xev.xclient.data.l[2] = None;
3828 xev.xclient.data.l[3] = 0;
3829 xev.xclient.data.l[4] = 0;
3831 if (!xdnd_send_xevent (context, context->source_window,
3834 g_message ("Send event to %lx failed",
3835 GDK_DRAWABLE_XID (context->source_window)));
3841 gdk_window_register_dnd (GdkWindow *window)
3843 static const gulong xdnd_version = 5;
3844 MotifDragReceiverInfo info;
3845 Atom motif_drag_receiver_info_atom;
3846 GdkDisplay *display = gdk_drawable_get_display (window);
3848 g_return_if_fail (window != NULL);
3850 base_precache_atoms (display);
3852 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3855 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3857 /* Set Motif drag receiver information property */
3859 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3860 "_MOTIF_DRAG_RECEIVER_INFO");
3861 /* initialize to zero to avoid writing uninitialized data to socket */
3862 memset(&info, 0, sizeof(info));
3863 info.byte_order = local_byte_order;
3864 info.protocol_version = 0;
3865 info.protocol_style = XmDRAG_DYNAMIC;
3866 info.proxy_window = None;
3867 info.num_drop_sites = 0;
3868 info.total_size = sizeof(info);
3870 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3871 motif_drag_receiver_info_atom,
3872 motif_drag_receiver_info_atom,
3879 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3880 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3881 GDK_DRAWABLE_XID (window),
3882 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3883 XA_ATOM, 32, PropModeReplace,
3884 (guchar *)&xdnd_version, 1);
3888 * gdk_drag_get_selection:
3889 * @context: a #GdkDragContext.
3891 * Returns the selection atom for the current source window.
3893 * Return value: the selection atom.
3896 gdk_drag_get_selection (GdkDragContext *context)
3898 g_return_val_if_fail (context != NULL, GDK_NONE);
3900 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3901 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3902 (PRIVATE_DATA (context))->motif_selection);
3903 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3904 return gdk_atom_intern_static_string ("XdndSelection");
3910 * gdk_drag_drop_succeeded:
3911 * @context: a #GdkDragContext
3913 * Returns whether the dropped data has been successfully
3914 * transferred. This function is intended to be used while
3915 * handling a %GDK_DROP_FINISHED event, its return value is
3916 * meaningless at other times.
3918 * Return value: %TRUE if the drop was successful.
3923 gdk_drag_drop_succeeded (GdkDragContext *context)
3925 GdkDragContextPrivateX11 *private;
3927 g_return_val_if_fail (context != NULL, FALSE);
3929 private = PRIVATE_DATA (context);
3931 return !private->drop_failed;
3934 #define __GDK_DND_X11_C__
3935 #include "gdkaliasdef.c"