1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include <X11/Xatom.h>
30 #include <X11/extensions/shape.h>
34 #include "gdk.h" /* For gdk_flush() */
38 #include "gdkproperty.h"
39 #include "gdkprivate-x11.h"
40 #include "gdkinternals.h"
41 #include "gdkscreen-x11.h"
42 #include "gdkdisplay-x11.h"
45 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
49 GDK_DRAG_STATUS_MOTION_WAIT,
50 GDK_DRAG_STATUS_ACTION_WAIT,
56 gint x, y, width, height;
58 gboolean shape_selected;
65 GHashTable *child_hash;
70 /* Structure that holds information about a drag in progress.
71 * this is used on both source and destination sides.
73 struct _GdkDragContextPrivateX11 {
74 GdkDragContext context;
79 guint16 last_x; /* Coordinates from last event */
81 GdkDragAction old_action; /* The last action we sent to the source */
82 GdkDragAction old_actions; /* The last actions we sent to the source */
83 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
85 Window dest_xid; /* The last window we looked up */
86 Window drop_xid; /* The (non-proxied) window that is receiving drops */
87 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
88 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
89 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
90 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
91 guint drag_status : 4; /* current status of drag */
93 guint drop_failed : 1; /* Whether the drop was unsuccessful */
94 guint version; /* Xdnd protocol version */
96 GSList *window_caches;
99 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
101 /* Forward declarations */
103 static void gdk_window_cache_destroy (GdkWindowCache *cache);
105 static void motif_read_target_table (GdkDisplay *display);
107 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
111 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
114 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
117 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
120 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
123 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
126 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
130 static void xdnd_manage_source_filter (GdkDragContext *context,
132 gboolean add_filter);
134 static void gdk_drag_context_finalize (GObject *object);
136 static GList *contexts;
138 static const struct {
139 const char *atom_name;
142 { "XdndEnter", xdnd_enter_filter },
143 { "XdndLeave", xdnd_leave_filter },
144 { "XdndPosition", xdnd_position_filter },
145 { "XdndStatus", xdnd_status_filter },
146 { "XdndFinished", xdnd_finished_filter },
147 { "XdndDrop", xdnd_drop_filter },
150 G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
153 gdk_drag_context_init (GdkDragContext *dragcontext)
155 GdkDragContextPrivateX11 *private;
157 private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext,
158 GDK_TYPE_DRAG_CONTEXT,
159 GdkDragContextPrivateX11);
161 dragcontext->windowing_data = private;
163 contexts = g_list_prepend (contexts, dragcontext);
167 gdk_drag_context_class_init (GdkDragContextClass *klass)
169 GObjectClass *object_class = G_OBJECT_CLASS (klass);
171 object_class->finalize = gdk_drag_context_finalize;
173 g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateX11));
177 gdk_drag_context_finalize (GObject *object)
179 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
180 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
183 g_list_free (context->targets);
185 if (context->source_window)
187 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
189 xdnd_manage_source_filter (context, context->source_window, FALSE);
191 g_object_unref (context->source_window);
194 if (context->dest_window)
195 g_object_unref (context->dest_window);
197 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
198 gdk_window_cache_destroy (tmp_list->data);
199 g_slist_free (private->window_caches);
201 contexts = g_list_remove (contexts, context);
203 G_OBJECT_CLASS (gdk_drag_context_parent_class)->finalize (object);
209 * gdk_drag_context_new:
211 * Creates a new #GdkDragContext.
213 * Return value: the newly created #GdkDragContext.
216 gdk_drag_context_new (void)
218 return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
222 * gdk_drag_context_ref:
223 * @context: a #GdkDragContext.
225 * Deprecated function; use g_object_ref() instead.
227 * Deprecated: 2.2: Use g_object_ref() instead.
230 gdk_drag_context_ref (GdkDragContext *context)
232 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
234 g_object_ref (context);
238 * gdk_drag_context_unref:
239 * @context: a #GdkDragContext.
241 * Deprecated function; use g_object_unref() instead.
243 * Deprecated: 2.2: Use g_object_unref() instead.
246 gdk_drag_context_unref (GdkDragContext *context)
248 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
250 g_object_unref (context);
253 static GdkDragContext *
254 gdk_drag_context_find (GdkDisplay *display,
259 GList *tmp_list = contexts;
260 GdkDragContext *context;
261 GdkDragContextPrivateX11 *private;
262 Window context_dest_xid;
266 context = (GdkDragContext *)tmp_list->data;
267 private = PRIVATE_DATA (context);
269 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
270 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
273 context_dest_xid = context->dest_window ?
276 GDK_DRAWABLE_XID (context->dest_window)) :
279 if ((!context->is_source == !is_source) &&
280 ((source_xid == None) || (context->source_window &&
281 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
282 ((dest_xid == None) || (context_dest_xid == dest_xid)))
285 tmp_list = tmp_list->next;
292 precache_target_list (GdkDragContext *context)
294 if (context->targets)
296 GPtrArray *targets = g_ptr_array_new ();
300 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
301 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
303 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
304 (const gchar **)targets->pdata,
307 for (i =0; i < targets->len; i++)
308 g_free (targets->pdata[i]);
310 g_ptr_array_free (targets, TRUE);
314 /* Utility functions */
317 free_cache_child (GdkCacheChild *child,
321 gdk_region_destroy (child->shape);
323 if (child->shape_selected && display)
325 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
327 XShapeSelectInput (display_x11->xdisplay, child->xid, 0);
334 gdk_window_cache_add (GdkWindowCache *cache,
336 gint x, gint y, gint width, gint height,
339 GdkCacheChild *child = g_new (GdkCacheChild, 1);
344 child->width = width;
345 child->height = height;
346 child->mapped = mapped;
347 child->shape_selected = FALSE;
348 child->shape_valid = FALSE;
351 cache->children = g_list_prepend (cache->children, child);
352 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
356 static GdkFilterReturn
357 gdk_window_cache_shape_filter (GdkXEvent *xev,
361 XEvent *xevent = (XEvent *)xev;
362 GdkWindowCache *cache = data;
364 GdkDisplayX11 *display = GDK_DISPLAY_X11 (gdk_screen_get_display (cache->screen));
366 if (display->have_shapes &&
367 xevent->type == display->shape_event_base + ShapeNotify)
369 XShapeEvent *xse = (XShapeEvent*)xevent;
372 node = g_hash_table_lookup (cache->child_hash,
373 GUINT_TO_POINTER (xse->window));
376 GdkCacheChild *child = node->data;
377 child->shape_valid = FALSE;
380 gdk_region_destroy (child->shape);
385 return GDK_FILTER_REMOVE;
388 return GDK_FILTER_CONTINUE;
391 static GdkFilterReturn
392 gdk_window_cache_filter (GdkXEvent *xev,
396 XEvent *xevent = (XEvent *)xev;
397 GdkWindowCache *cache = data;
399 switch (xevent->type)
401 case CirculateNotify:
403 case ConfigureNotify:
405 XConfigureEvent *xce = &xevent->xconfigure;
408 node = g_hash_table_lookup (cache->child_hash,
409 GUINT_TO_POINTER (xce->window));
412 GdkCacheChild *child = node->data;
415 child->width = xce->width;
416 child->height = xce->height;
417 if (xce->above == None && (node->next))
419 GList *last = g_list_last (cache->children);
420 cache->children = g_list_remove_link (cache->children, node);
427 GList *above_node = g_hash_table_lookup (cache->child_hash,
428 GUINT_TO_POINTER (xce->above));
429 if (above_node && node->next != above_node)
431 /* Put the window above (before in the list) above_node
433 cache->children = g_list_remove_link (cache->children, node);
434 node->prev = above_node->prev;
436 node->prev->next = node;
438 cache->children = node;
439 node->next = above_node;
440 above_node->prev = node;
448 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
450 if (!g_hash_table_lookup (cache->child_hash,
451 GUINT_TO_POINTER (xcwe->window)))
452 gdk_window_cache_add (cache, xcwe->window,
453 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
459 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
462 node = g_hash_table_lookup (cache->child_hash,
463 GUINT_TO_POINTER (xdwe->window));
466 GdkCacheChild *child = node->data;
468 g_hash_table_remove (cache->child_hash,
469 GUINT_TO_POINTER (xdwe->window));
470 cache->children = g_list_remove_link (cache->children, node);
471 /* window is destroyed, no need to disable ShapeNotify */
472 free_cache_child (child, NULL);
473 g_list_free_1 (node);
479 XMapEvent *xme = &xevent->xmap;
482 node = g_hash_table_lookup (cache->child_hash,
483 GUINT_TO_POINTER (xme->window));
486 GdkCacheChild *child = node->data;
487 child->mapped = TRUE;
495 XMapEvent *xume = &xevent->xmap;
498 node = g_hash_table_lookup (cache->child_hash,
499 GUINT_TO_POINTER (xume->window));
502 GdkCacheChild *child = node->data;
503 child->mapped = FALSE;
508 return GDK_FILTER_CONTINUE;
510 return GDK_FILTER_REMOVE;
513 static GdkWindowCache *
514 gdk_window_cache_new (GdkScreen *screen)
516 XWindowAttributes xwa;
517 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
518 GdkWindow *root_window = gdk_screen_get_root_window (screen);
519 GdkChildInfoX11 *children;
522 GdkWindowCache *result = g_new (GdkWindowCache, 1);
524 result->children = NULL;
525 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
526 result->screen = screen;
528 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
529 result->old_event_mask = xwa.your_event_mask;
531 if (G_UNLIKELY (!GDK_DISPLAY_X11 (GDK_SCREEN_X11 (screen)->display)->trusted_client))
533 GList *toplevel_windows, *list;
535 gint x, y, width, height;
537 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
538 for (list = toplevel_windows; list; list = list->next) {
539 window = GDK_WINDOW (list->data);
540 gdk_window_get_geometry (window, &x, &y, &width, &height, NULL);
541 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
543 gdk_window_is_visible (window));
545 g_list_free (toplevel_windows);
549 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
550 result->old_event_mask | SubstructureNotifyMask);
551 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
552 gdk_window_add_filter (NULL, gdk_window_cache_shape_filter, result);
554 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
555 GDK_WINDOW_XWINDOW (root_window),
557 &children, &nchildren))
560 for (i = 0; i < nchildren ; i++)
562 gdk_window_cache_add (result, children[i].window,
563 children[i].x, children[i].y, children[i].width, children[i].height,
564 children[i].is_mapped);
573 gdk_window_cache_destroy (GdkWindowCache *cache)
575 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
577 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
578 GDK_WINDOW_XWINDOW (root_window),
579 cache->old_event_mask);
580 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
581 gdk_window_remove_filter (NULL, gdk_window_cache_shape_filter, cache);
583 gdk_error_trap_push ();
585 g_list_foreach (cache->children, (GFunc)free_cache_child,
586 gdk_screen_get_display (cache->screen));
589 gdk_error_trap_pop ();
591 g_list_free (cache->children);
592 g_hash_table_destroy (cache->child_hash);
598 is_pointer_within_shape (GdkDisplay *display,
599 GdkCacheChild *child,
603 if (!child->shape_selected)
605 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
607 XShapeSelectInput (display_x11->xdisplay, child->xid, ShapeNotifyMask);
608 child->shape_selected = TRUE;
610 if (!child->shape_valid)
612 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
613 GdkRegion *input_shape;
615 child->shape = _xwindow_get_shape (display_x11->xdisplay,
616 child->xid, ShapeBounding);
618 input_shape = _xwindow_get_shape (display_x11->xdisplay,
619 child->xid, ShapeInput);
620 if (child->shape && input_shape)
622 gdk_region_intersect (child->shape, input_shape);
623 gdk_region_destroy (input_shape);
625 else if (input_shape)
627 child->shape = input_shape;
631 child->shape_valid = TRUE;
634 return child->shape == NULL ||
635 gdk_region_point_in (child->shape, x_pos, y_pos);
639 get_client_window_at_coords_recurse (GdkDisplay *display,
641 gboolean is_toplevel,
645 GdkChildInfoX11 *children;
646 unsigned int nchildren;
648 gboolean found_child = FALSE;
649 GdkChildInfoX11 child = { 0, };
650 gboolean has_wm_state = FALSE;
652 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
653 is_toplevel? &has_wm_state : NULL,
654 &children, &nchildren))
664 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
666 GdkChildInfoX11 *cur_child = &children[i];
668 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
669 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
670 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
683 if (child.has_wm_state)
686 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
693 get_client_window_at_coords (GdkWindowCache *cache,
699 Window retval = None;
701 gdk_error_trap_push ();
703 tmp_list = cache->children;
705 while (tmp_list && !retval)
707 GdkCacheChild *child = tmp_list->data;
709 if ((child->xid != ignore) && (child->mapped))
711 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
712 (y_root >= child->y) && (y_root < child->y + child->height))
714 GdkDisplay *display = gdk_screen_get_display (cache->screen);
716 if (!is_pointer_within_shape (display, child,
720 tmp_list = tmp_list->next;
724 retval = get_client_window_at_coords_recurse (display,
732 tmp_list = tmp_list->next;
735 gdk_error_trap_pop ();
740 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
743 /*************************************************************
744 ***************************** MOTIF *************************
745 *************************************************************/
747 /* values used in the message type for Motif DND */
760 /* Values used to specify type of protocol to use */
764 XmDRAG_PREFER_PREREGISTER,
766 XmDRAG_PREFER_DYNAMIC,
768 XmDRAG_PREFER_RECEIVER
771 /* Operation codes */
779 /* Drop site status */
781 XmNO_DROP_SITE = 0x01,
782 XmDROP_SITE_INVALID = 0x02,
783 XmDROP_SITE_VALID = 0x03
786 /* completion status */
794 /* Byte swapping routines. The motif specification leaves it
795 * up to us to save a few bytes in the client messages
797 static gchar local_byte_order = '\0';
799 #ifdef G_ENABLE_DEBUG
801 print_target_list (GList *targets)
805 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
806 g_message ("\t%s", name);
808 targets = targets->next;
811 #endif /* G_ENABLE_DEBUG */
814 init_byte_order (void)
816 guint32 myint = 0x01020304;
817 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
821 card16_to_host (guint16 x, gchar byte_order) {
822 if (byte_order == local_byte_order)
825 return (x << 8) | (x >> 8);
829 card32_to_host (guint32 x, gchar byte_order) {
830 if (byte_order == local_byte_order)
833 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
836 /* Motif packs together fields of varying length into the
837 * client message. We can't rely on accessing these
838 * through data.s[], data.l[], etc, because on some architectures
839 * (i.e., Alpha) these won't be valid for format == 8.
842 #define MOTIF_XCLIENT_BYTE(xevent,i) \
843 (xevent)->xclient.data.b[i]
844 #define MOTIF_XCLIENT_SHORT(xevent,i) \
845 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
846 #define MOTIF_XCLIENT_LONG(xevent,i) \
847 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
849 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
850 #define MOTIF_UNPACK_SHORT(xevent,i) \
851 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
852 #define MOTIF_UNPACK_LONG(xevent,i) \
853 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
855 /***** Dest side ***********/
857 /* Property placed on source windows */
858 typedef struct _MotifDragInitiatorInfo {
860 guint8 protocol_version;
861 guint16 targets_index;
862 guint32 selection_atom;
863 } MotifDragInitiatorInfo;
865 /* Header for target table on the drag window */
866 typedef struct _MotifTargetTableHeader {
868 guchar protocol_version;
871 } MotifTargetTableHeader;
873 /* Property placed on target windows */
874 typedef struct _MotifDragReceiverInfo {
876 guint8 protocol_version;
877 guint8 protocol_style;
879 guint32 proxy_window;
880 guint16 num_drop_sites;
883 } MotifDragReceiverInfo;
885 /* Target table handling */
887 static GdkFilterReturn
888 motif_drag_window_filter (GdkXEvent *xevent,
892 XEvent *xev = (XEvent *)xevent;
893 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
894 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
896 switch (xev->xany.type)
899 display_x11->motif_drag_window = None;
900 display_x11->motif_drag_gdk_window = NULL;
903 if (display_x11->motif_target_lists &&
904 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
905 motif_read_target_table (display);
908 return GDK_FILTER_REMOVE;
912 motif_lookup_drag_window (GdkDisplay *display,
913 Display *lookup_xdisplay)
915 Window retval = None;
916 gulong bytes_after, nitems;
921 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
922 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
924 XA_WINDOW, &type, &format, &nitems, &bytes_after,
927 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
929 retval = *(Window *)data;
931 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
940 /* Finds the window where global Motif drag information is stored.
941 * If it doesn't exist and 'create' is TRUE, create one.
944 motif_find_drag_window (GdkDisplay *display,
947 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
949 if (!display_x11->motif_drag_window)
951 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
952 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
954 if (!display_x11->motif_drag_window && create)
956 /* Create a persistant window. (Copied from LessTif) */
958 Display *persistant_xdisplay;
959 XSetWindowAttributes attr;
960 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
961 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
963 XGrabServer (persistant_xdisplay);
965 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
967 if (!display_x11->motif_drag_window)
969 attr.override_redirect = True;
970 attr.event_mask = PropertyChangeMask;
972 display_x11->motif_drag_window =
973 XCreateWindow (persistant_xdisplay,
974 RootWindow (persistant_xdisplay, 0),
975 -100, -100, 10, 10, 0, 0,
976 InputOnly, (Visual *)CopyFromParent,
977 (CWOverrideRedirect | CWEventMask), &attr);
980 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
982 XChangeProperty (persistant_xdisplay,
983 RootWindow (persistant_xdisplay, 0),
984 motif_drag_window_atom, XA_WINDOW,
986 (guchar *)&motif_drag_window_atom, 1);
989 XUngrabServer (persistant_xdisplay);
990 XCloseDisplay (persistant_xdisplay);
993 /* There is a miniscule race condition here if the drag window
994 * gets destroyed exactly now.
996 if (display_x11->motif_drag_window)
998 display_x11->motif_drag_gdk_window =
999 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
1000 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
1001 motif_drag_window_filter,
1006 return display_x11->motif_drag_window;
1010 motif_read_target_table (GdkDisplay *display)
1012 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1013 gulong bytes_after, nitems;
1018 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
1020 if (display_x11->motif_target_lists)
1022 for (i=0; i<display_x11->motif_n_target_lists; i++)
1023 g_list_free (display_x11->motif_target_lists[i]);
1025 g_free (display_x11->motif_target_lists);
1026 display_x11->motif_target_lists = NULL;
1027 display_x11->motif_n_target_lists = 0;
1030 if (motif_find_drag_window (display, FALSE))
1033 MotifTargetTableHeader *header = NULL;
1034 guchar *target_bytes = NULL;
1036 gboolean success = FALSE;
1038 gdk_error_trap_push ();
1039 XGetWindowProperty (display_x11->xdisplay,
1040 display_x11->motif_drag_window,
1041 motif_drag_targets_atom,
1042 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
1043 motif_drag_targets_atom,
1044 &type, &format, &nitems, &bytes_after,
1047 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
1050 header = (MotifTargetTableHeader *)data;
1052 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
1053 header->total_size = card32_to_host (header->total_size, header->byte_order);
1055 gdk_error_trap_push ();
1056 XGetWindowProperty (display_x11->xdisplay,
1057 display_x11->motif_drag_window,
1058 motif_drag_targets_atom,
1059 (sizeof(MotifTargetTableHeader)+3)/4,
1060 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
1062 motif_drag_targets_atom, &type, &format, &nitems,
1063 &bytes_after, &target_bytes);
1065 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
1066 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
1069 display_x11->motif_n_target_lists = header->n_lists;
1070 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
1073 for (i=0; i<header->n_lists; i++)
1078 if (p + sizeof(guint16) - target_bytes > nitems)
1081 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
1083 /* We need to make a copy of the targets, since it may
1086 targets = g_new (guint32, n_targets);
1087 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
1089 p += sizeof(guint16) + n_targets * sizeof(guint32);
1090 if (p - target_bytes > nitems)
1093 for (j=0; j<n_targets; j++)
1094 display_x11->motif_target_lists[i] =
1095 g_list_prepend (display_x11->motif_target_lists[i],
1096 GUINT_TO_POINTER (card32_to_host (targets[j],
1097 header->byte_order)));
1099 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
1109 XFree (target_bytes);
1113 if (display_x11->motif_target_lists)
1115 g_free (display_x11->motif_target_lists);
1116 display_x11->motif_target_lists = NULL;
1117 display_x11->motif_n_target_lists = 0;
1119 g_warning ("Error reading Motif target table\n");
1125 targets_sort_func (gconstpointer a, gconstpointer b)
1127 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1128 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1131 /* Check if given (sorted) list is in the targets table */
1133 motif_target_table_check (GdkDisplay *display,
1136 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1137 GList *tmp_list1, *tmp_list2;
1140 for (i=0; i<display_x11->motif_n_target_lists; i++)
1142 tmp_list1 = display_x11->motif_target_lists[i];
1145 while (tmp_list1 && tmp_list2)
1147 if (tmp_list1->data != tmp_list2->data)
1150 tmp_list1 = tmp_list1->next;
1151 tmp_list2 = tmp_list2->next;
1153 if (!tmp_list1 && !tmp_list2) /* Found it */
1161 motif_add_to_target_table (GdkDisplay *display,
1162 GList *targets) /* targets is list of GdkAtom */
1164 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1165 GList *sorted = NULL;
1170 /* make a sorted copy of the list */
1174 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1175 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1176 targets = targets->next;
1179 /* First check if it is there already */
1181 if (display_x11->motif_target_lists)
1182 index = motif_target_table_check (display, sorted);
1184 /* We need to grab the server while doing this, to ensure
1190 /* We need to make sure that it exists _before_ we grab the
1191 * server, since we can't open a new connection after we
1194 motif_find_drag_window (display, TRUE);
1196 gdk_x11_display_grab (display);
1197 motif_read_target_table (display);
1199 /* Check again, in case it was added in the meantime */
1201 if (display_x11->motif_target_lists)
1202 index = motif_target_table_check (display, sorted);
1206 guint32 total_size = 0;
1210 MotifTargetTableHeader *header;
1212 if (!display_x11->motif_target_lists)
1214 display_x11->motif_target_lists = g_new (GList *, 1);
1215 display_x11->motif_n_target_lists = 1;
1219 display_x11->motif_n_target_lists++;
1220 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1221 sizeof(GList *) * display_x11->motif_n_target_lists);
1223 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1225 index = display_x11->motif_n_target_lists - 1;
1227 total_size = sizeof (MotifTargetTableHeader);
1228 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1229 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1231 data = g_malloc (total_size);
1233 header = (MotifTargetTableHeader *)data;
1234 p = data + sizeof(MotifTargetTableHeader);
1236 header->byte_order = local_byte_order;
1237 header->protocol_version = 0;
1238 header->n_lists = display_x11->motif_n_target_lists;
1239 header->total_size = total_size;
1241 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1243 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1244 guint32 *targets = g_new (guint32, n_targets);
1245 guint32 *p32 = targets;
1247 tmp_list = display_x11->motif_target_lists[i];
1250 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1252 tmp_list = tmp_list->next;
1257 p += sizeof(guint16);
1259 memcpy (p, targets, n_targets * sizeof(guint32));
1262 p += sizeof(guint32) * n_targets;
1266 XChangeProperty (display_x11->xdisplay,
1267 display_x11->motif_drag_window,
1268 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1269 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1273 gdk_x11_display_ungrab (display);
1276 g_list_free (sorted);
1280 /* Translate flags */
1283 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1285 guint recommended_op = flags & 0x000f;
1286 guint possible_ops = (flags & 0x0f0) >> 4;
1288 switch (recommended_op)
1291 context->suggested_action = GDK_ACTION_MOVE;
1294 context->suggested_action = GDK_ACTION_COPY;
1297 context->suggested_action = GDK_ACTION_LINK;
1300 context->suggested_action = GDK_ACTION_COPY;
1304 context->actions = 0;
1305 if (possible_ops & XmDROP_MOVE)
1306 context->actions |= GDK_ACTION_MOVE;
1307 if (possible_ops & XmDROP_COPY)
1308 context->actions |= GDK_ACTION_COPY;
1309 if (possible_ops & XmDROP_LINK)
1310 context->actions |= GDK_ACTION_LINK;
1314 motif_dnd_get_flags (GdkDragContext *context)
1318 switch (context->suggested_action)
1320 case GDK_ACTION_MOVE:
1321 flags = XmDROP_MOVE;
1323 case GDK_ACTION_COPY:
1324 flags = XmDROP_COPY;
1326 case GDK_ACTION_LINK:
1327 flags = XmDROP_LINK;
1330 flags = XmDROP_NOOP;
1334 if (context->actions & GDK_ACTION_MOVE)
1335 flags |= XmDROP_MOVE << 8;
1336 if (context->actions & GDK_ACTION_COPY)
1337 flags |= XmDROP_COPY << 8;
1338 if (context->actions & GDK_ACTION_LINK)
1339 flags |= XmDROP_LINK << 8;
1347 motif_set_targets (GdkDragContext *context)
1349 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1350 MotifDragInitiatorInfo info;
1352 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1354 info.byte_order = local_byte_order;
1355 info.protocol_version = 0;
1357 info.targets_index = motif_add_to_target_table (display, context->targets);
1362 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1364 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1365 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1369 info.selection_atom = private->motif_selection;
1371 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1372 GDK_DRAWABLE_XID (context->source_window),
1373 private->motif_selection,
1374 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1376 (guchar *)&info, sizeof (info));
1378 private->motif_targets_set = 1;
1382 motif_check_dest (GdkDisplay *display,
1385 gboolean retval = FALSE;
1387 MotifDragReceiverInfo *info;
1390 unsigned long nitems, after;
1391 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1393 gdk_error_trap_push ();
1394 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1395 motif_drag_receiver_info_atom,
1396 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1397 &type, &format, &nitems, &after,
1400 if (gdk_error_trap_pop() == 0)
1404 info = (MotifDragReceiverInfo *)data;
1406 if ((format == 8) && (nitems == sizeof(*info)))
1408 if ((info->protocol_version == 0) &&
1409 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1410 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1411 (info->protocol_style == XmDRAG_DYNAMIC)))
1417 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1424 return retval ? win : None;
1428 motif_send_enter (GdkDragContext *context,
1431 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1432 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1435 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1436 return; /* Motif Dnd requires getting properties on the root window */
1438 xev.xclient.type = ClientMessage;
1439 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1440 xev.xclient.format = 8;
1441 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1443 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1444 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1445 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1446 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1447 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1449 if (!private->motif_targets_set)
1450 motif_set_targets (context);
1452 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1453 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1455 if (!_gdk_send_xevent (display,
1456 GDK_DRAWABLE_XID (context->dest_window),
1459 g_message ("Send event to %lx failed",
1460 GDK_DRAWABLE_XID (context->dest_window)));
1464 motif_send_leave (GdkDragContext *context,
1467 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1470 xev.xclient.type = ClientMessage;
1471 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1472 xev.xclient.format = 8;
1473 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1475 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1476 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1477 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1478 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1479 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1480 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1481 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1483 if (!_gdk_send_xevent (display,
1484 GDK_DRAWABLE_XID (context->dest_window),
1487 g_message ("Send event to %lx failed",
1488 GDK_DRAWABLE_XID (context->dest_window)));
1492 motif_send_motion (GdkDragContext *context,
1495 GdkDragAction action,
1498 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1499 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1503 xev.xclient.type = ClientMessage;
1504 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1505 xev.xclient.format = 8;
1506 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1508 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1509 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1510 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1511 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1512 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1514 if ((context->suggested_action != private->old_action) ||
1515 (context->actions != private->old_actions))
1517 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1519 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1524 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1526 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1527 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1529 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1533 if (!_gdk_send_xevent (display,
1534 GDK_DRAWABLE_XID (context->dest_window),
1537 g_message ("Send event to %lx failed",
1538 GDK_DRAWABLE_XID (context->dest_window)));
1544 motif_send_drop (GdkDragContext *context, guint32 time)
1546 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1547 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1550 xev.xclient.type = ClientMessage;
1551 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1552 xev.xclient.format = 8;
1553 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1555 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1556 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1557 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1558 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1560 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1561 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1563 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1564 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1566 if (!_gdk_send_xevent (display,
1567 GDK_DRAWABLE_XID (context->dest_window),
1570 g_message ("Send event to %lx failed",
1571 GDK_DRAWABLE_XID (context->dest_window)));
1577 motif_read_initiator_info (GdkDisplay *display,
1578 Window source_window,
1589 MotifDragInitiatorInfo *initiator_info;
1591 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1593 gdk_error_trap_push ();
1594 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1595 0, sizeof(*initiator_info), FALSE,
1596 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1597 &type, &format, &nitems, &bytes_after,
1600 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1602 g_warning ("Error reading initiator info\n");
1606 initiator_info = (MotifDragInitiatorInfo *)data;
1608 motif_read_target_table (display);
1610 initiator_info->targets_index =
1611 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1612 initiator_info->selection_atom =
1613 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1615 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1617 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1618 XFree (initiator_info);
1622 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1627 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1628 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1629 tmp_list = tmp_list->prev;
1632 #ifdef G_ENABLE_DEBUG
1633 if (_gdk_debug_flags & GDK_DEBUG_DND)
1634 print_target_list (*targets);
1635 #endif /* G_ENABLE_DEBUG */
1637 *selection = initiator_info->selection_atom;
1639 XFree (initiator_info);
1644 static GdkDragContext *
1645 motif_drag_context_new (GdkWindow *dest_window,
1647 guint32 source_window,
1650 GdkDragContext *new_context;
1651 GdkDragContextPrivateX11 *private;
1652 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1653 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1655 /* FIXME, current_dest_drag really shouldn't be NULL'd
1656 * if we error below.
1658 if (display_x11->current_dest_drag != NULL)
1660 if (timestamp >= display_x11->current_dest_drag->start_time)
1662 g_object_unref (display_x11->current_dest_drag);
1663 display_x11->current_dest_drag = NULL;
1669 new_context = gdk_drag_context_new ();
1670 private = PRIVATE_DATA (new_context);
1672 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1673 new_context->is_source = FALSE;
1675 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1676 if (new_context->source_window)
1677 g_object_ref (new_context->source_window);
1680 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1681 if (!new_context->source_window)
1683 g_object_unref (new_context);
1688 new_context->dest_window = dest_window;
1689 g_object_ref (dest_window);
1690 new_context->start_time = timestamp;
1692 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1695 &new_context->targets,
1696 &private->motif_selection))
1698 g_object_unref (new_context);
1706 * The MOTIF drag protocol has no real provisions for distinguishing
1707 * multiple simultaneous drops. If the sources grab the pointer
1708 * when doing drags, that shouldn't happen, in any case. If it
1709 * does, we can't do much except hope for the best.
1712 static GdkFilterReturn
1713 motif_top_level_enter (GdkEvent *event,
1716 guint32 source_window,
1719 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1720 GdkDragContext *new_context;
1722 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1723 flags, timestamp, source_window, atom));
1725 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1727 return GDK_FILTER_REMOVE;
1729 event->dnd.type = GDK_DRAG_ENTER;
1730 event->dnd.context = new_context;
1731 g_object_ref (new_context);
1733 display_x11->current_dest_drag = new_context;
1735 return GDK_FILTER_TRANSLATE;
1738 static GdkFilterReturn
1739 motif_top_level_leave (GdkEvent *event,
1743 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1745 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1748 if ((display_x11->current_dest_drag != NULL) &&
1749 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1750 (timestamp >= display_x11->current_dest_drag->start_time))
1752 event->dnd.type = GDK_DRAG_LEAVE;
1753 /* Pass ownership of context to the event */
1754 event->dnd.context = display_x11->current_dest_drag;
1756 display_x11->current_dest_drag = NULL;
1758 return GDK_FILTER_TRANSLATE;
1761 return GDK_FILTER_REMOVE;
1764 static GdkFilterReturn
1765 motif_motion (GdkEvent *event,
1771 GdkDragContextPrivateX11 *private;
1772 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1774 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1775 flags, timestamp, x_root, y_root));
1777 if ((display_x11->current_dest_drag != NULL) &&
1778 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1779 (timestamp >= display_x11->current_dest_drag->start_time))
1781 private = PRIVATE_DATA (display_x11->current_dest_drag);
1783 event->dnd.type = GDK_DRAG_MOTION;
1784 event->dnd.context = display_x11->current_dest_drag;
1785 g_object_ref (display_x11->current_dest_drag);
1787 event->dnd.time = timestamp;
1789 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1791 event->dnd.x_root = x_root;
1792 event->dnd.y_root = y_root;
1794 private->last_x = x_root;
1795 private->last_y = y_root;
1797 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1799 return GDK_FILTER_TRANSLATE;
1802 return GDK_FILTER_REMOVE;
1805 static GdkFilterReturn
1806 motif_operation_changed (GdkEvent *event,
1810 GdkDragContextPrivateX11 *private;
1811 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1812 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1815 if ((display_x11->current_dest_drag != NULL) &&
1816 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1817 (timestamp >= display_x11->current_dest_drag->start_time))
1819 event->dnd.type = GDK_DRAG_MOTION;
1820 event->dnd.send_event = FALSE;
1821 event->dnd.context = display_x11->current_dest_drag;
1822 g_object_ref (display_x11->current_dest_drag);
1824 event->dnd.time = timestamp;
1825 private = PRIVATE_DATA (display_x11->current_dest_drag);
1827 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1829 event->dnd.x_root = private->last_x;
1830 event->dnd.y_root = private->last_y;
1832 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1834 return GDK_FILTER_TRANSLATE;
1837 return GDK_FILTER_REMOVE;
1840 static GdkFilterReturn
1841 motif_drop_start (GdkEvent *event,
1844 guint32 source_window,
1849 GdkDragContext *new_context;
1850 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1852 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1853 flags, timestamp, x_root, y_root, source_window, atom));
1855 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1857 return GDK_FILTER_REMOVE;
1859 motif_dnd_translate_flags (new_context, flags);
1861 event->dnd.type = GDK_DROP_START;
1862 event->dnd.context = new_context;
1863 event->dnd.time = timestamp;
1864 event->dnd.x_root = x_root;
1865 event->dnd.y_root = y_root;
1867 gdk_x11_window_set_user_time (event->any.window, timestamp);
1869 g_object_ref (new_context);
1870 display_x11->current_dest_drag = new_context;
1872 return GDK_FILTER_TRANSLATE;
1875 static GdkFilterReturn
1876 motif_drag_status (GdkEvent *event,
1880 GdkDragContext *context;
1881 GdkDisplay *display;
1884 g_message ("Motif status message: flags %x", flags));
1886 display = gdk_drawable_get_display (event->any.window);
1888 return GDK_FILTER_REMOVE;
1890 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1894 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1895 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1896 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1897 private->drag_status = GDK_DRAG_STATUS_DRAG;
1899 event->dnd.type = GDK_DRAG_STATUS;
1900 event->dnd.send_event = FALSE;
1901 event->dnd.context = context;
1902 g_object_ref (context);
1904 event->dnd.time = timestamp;
1906 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1908 switch (flags & 0x000f)
1911 context->action = 0;
1914 context->action = GDK_ACTION_MOVE;
1917 context->action = GDK_ACTION_COPY;
1920 context->action = GDK_ACTION_LINK;
1925 context->action = 0;
1927 return GDK_FILTER_TRANSLATE;
1929 return GDK_FILTER_REMOVE;
1932 static GdkFilterReturn
1933 motif_dnd_filter (GdkXEvent *xev,
1937 XEvent *xevent = (XEvent *)xev;
1942 guint32 source_window;
1944 gint16 x_root, y_root;
1947 if (!event->any.window ||
1948 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1949 return GDK_FILTER_CONTINUE; /* Not for us */
1951 /* First read some fields common to all Motif DND messages */
1953 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1954 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1955 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1957 is_reply = ((reason & 0x80) != 0);
1959 switch (reason & 0x7f)
1961 case XmTOP_LEVEL_ENTER:
1962 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1963 atom = MOTIF_UNPACK_LONG (xevent, 3);
1964 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1965 case XmTOP_LEVEL_LEAVE:
1966 return motif_top_level_leave (event, flags, timestamp);
1969 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1970 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1973 return motif_motion (event, flags, timestamp, x_root, y_root);
1975 return motif_drag_status (event, flags, timestamp);
1977 case XmDROP_SITE_ENTER:
1978 return motif_drag_status (event, flags, timestamp);
1980 case XmDROP_SITE_LEAVE:
1981 return motif_drag_status (event,
1982 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1985 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1986 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1987 atom = MOTIF_UNPACK_LONG (xevent, 3);
1988 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1991 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1994 case XmOPERATION_CHANGED:
1996 return motif_operation_changed (event, flags, timestamp);
1998 return motif_drag_status (event, flags, timestamp);
2001 /* To the best of my knowledge, these next two messages are
2002 * not part of the protocol, though they are defined in
2006 case XmDRAG_DROP_FINISH:
2010 return GDK_FILTER_REMOVE;
2013 /*************************************************************
2014 ***************************** XDND **************************
2015 *************************************************************/
2017 /* Utility functions */
2022 GdkDragAction action;
2023 } xdnd_actions_table[] = {
2024 { "XdndActionCopy", None, GDK_ACTION_COPY },
2025 { "XdndActionMove", None, GDK_ACTION_MOVE },
2026 { "XdndActionLink", None, GDK_ACTION_LINK },
2027 { "XdndActionAsk", None, GDK_ACTION_ASK },
2028 { "XdndActionPrivate", None, GDK_ACTION_COPY },
2031 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
2032 static gboolean xdnd_actions_initialized = FALSE;
2035 xdnd_initialize_actions (void)
2039 xdnd_actions_initialized = TRUE;
2040 for (i=0; i < xdnd_n_actions; i++)
2041 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
2044 static GdkDragAction
2045 xdnd_action_from_atom (GdkDisplay *display,
2054 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
2056 if (!xdnd_actions_initialized)
2057 xdnd_initialize_actions();
2059 for (i=0; i<xdnd_n_actions; i++)
2060 if (atom == xdnd_actions_table[i].atom)
2061 return xdnd_actions_table[i].action;
2067 xdnd_action_to_atom (GdkDisplay *display,
2068 GdkDragAction action)
2072 if (!xdnd_actions_initialized)
2073 xdnd_initialize_actions();
2075 for (i=0; i<xdnd_n_actions; i++)
2076 if (action == xdnd_actions_table[i].action)
2077 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2084 static GdkFilterReturn
2085 xdnd_status_filter (GdkXEvent *xev,
2089 GdkDisplay *display;
2090 XEvent *xevent = (XEvent *)xev;
2091 guint32 dest_window = xevent->xclient.data.l[0];
2092 guint32 flags = xevent->xclient.data.l[1];
2093 Atom action = xevent->xclient.data.l[4];
2094 GdkDragContext *context;
2096 if (!event->any.window ||
2097 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2098 return GDK_FILTER_CONTINUE; /* Not for us */
2101 g_message ("XdndStatus: dest_window: %#x action: %ld",
2102 dest_window, action));
2104 display = gdk_drawable_get_display (event->any.window);
2105 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2109 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2110 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
2111 private->drag_status = GDK_DRAG_STATUS_DRAG;
2113 event->dnd.send_event = FALSE;
2114 event->dnd.type = GDK_DRAG_STATUS;
2115 event->dnd.context = context;
2116 g_object_ref (context);
2118 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2119 if (!(action != 0) != !(flags & 1))
2122 g_warning ("Received status event with flags not corresponding to action!\n"));
2126 context->action = xdnd_action_from_atom (display, action);
2128 return GDK_FILTER_TRANSLATE;
2131 return GDK_FILTER_REMOVE;
2134 static GdkFilterReturn
2135 xdnd_finished_filter (GdkXEvent *xev,
2139 GdkDisplay *display;
2140 XEvent *xevent = (XEvent *)xev;
2141 guint32 dest_window = xevent->xclient.data.l[0];
2142 GdkDragContext *context;
2143 GdkDragContextPrivateX11 *private;
2145 if (!event->any.window ||
2146 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2147 return GDK_FILTER_CONTINUE; /* Not for us */
2150 g_message ("XdndFinished: dest_window: %#x", dest_window));
2152 display = gdk_drawable_get_display (event->any.window);
2153 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2157 private = PRIVATE_DATA (context);
2158 if (private->version == 5)
2159 private->drop_failed = xevent->xclient.data.l[1] == 0;
2161 event->dnd.type = GDK_DROP_FINISHED;
2162 event->dnd.context = context;
2163 g_object_ref (context);
2165 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2167 return GDK_FILTER_TRANSLATE;
2170 return GDK_FILTER_REMOVE;
2174 xdnd_set_targets (GdkDragContext *context)
2176 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2178 GList *tmp_list = context->targets;
2180 gint n_atoms = g_list_length (context->targets);
2181 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2183 atomlist = g_new (Atom, n_atoms);
2187 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2188 tmp_list = tmp_list->next;
2192 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2193 GDK_DRAWABLE_XID (context->source_window),
2194 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2195 XA_ATOM, 32, PropModeReplace,
2196 (guchar *)atomlist, n_atoms);
2200 private->xdnd_targets_set = 1;
2204 xdnd_set_actions (GdkDragContext *context)
2206 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2211 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2213 if (!xdnd_actions_initialized)
2214 xdnd_initialize_actions();
2216 actions = context->actions;
2218 for (i=0; i<xdnd_n_actions; i++)
2220 if (actions & xdnd_actions_table[i].action)
2222 actions &= ~xdnd_actions_table[i].action;
2227 atomlist = g_new (Atom, n_atoms);
2229 actions = context->actions;
2231 for (i=0; i<xdnd_n_actions; i++)
2233 if (actions & xdnd_actions_table[i].action)
2235 actions &= ~xdnd_actions_table[i].action;
2236 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2241 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2242 GDK_DRAWABLE_XID (context->source_window),
2243 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2244 XA_ATOM, 32, PropModeReplace,
2245 (guchar *)atomlist, n_atoms);
2249 private->xdnd_actions_set = TRUE;
2250 private->xdnd_actions = context->actions;
2254 send_client_message_async_cb (Window window,
2258 GdkDragContext *context = data;
2260 g_message ("Got async callback for #%lx, success = %d",
2263 /* On failure, we immediately continue with the protocol
2264 * so we don't end up blocking for a timeout
2267 context->dest_window &&
2268 window == GDK_WINDOW_XID (context->dest_window))
2270 GdkEvent temp_event;
2271 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2273 g_object_unref (context->dest_window);
2274 context->dest_window = NULL;
2275 context->action = 0;
2277 private->drag_status = GDK_DRAG_STATUS_DRAG;
2279 temp_event.dnd.type = GDK_DRAG_STATUS;
2280 temp_event.dnd.window = context->source_window;
2281 temp_event.dnd.send_event = TRUE;
2282 temp_event.dnd.context = context;
2283 temp_event.dnd.time = GDK_CURRENT_TIME;
2285 gdk_event_put (&temp_event);
2288 g_object_unref (context);
2293 gdk_drag_context_get_display (GdkDragContext *context)
2295 if (context->source_window)
2296 return GDK_DRAWABLE_DISPLAY (context->source_window);
2297 else if (context->dest_window)
2298 return GDK_DRAWABLE_DISPLAY (context->dest_window);
2300 g_assert_not_reached ();
2305 send_client_message_async (GdkDragContext *context,
2309 XClientMessageEvent *event_send)
2311 GdkDisplay *display = gdk_drag_context_get_display (context);
2313 g_object_ref (context);
2315 _gdk_x11_send_client_message_async (display, window,
2316 propagate, event_mask, event_send,
2317 send_client_message_async_cb, context);
2321 xdnd_send_xevent (GdkDragContext *context,
2326 GdkDisplay *display = gdk_drag_context_get_display (context);
2330 g_assert (event_send->xany.type == ClientMessage);
2332 /* We short-circuit messages to ourselves */
2333 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2337 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2339 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2340 event_send->xclient.message_type)
2342 GdkEvent temp_event;
2343 temp_event.any.window = window;
2345 if ((*xdnd_filters[i].func) (event_send, &temp_event, NULL) == GDK_FILTER_TRANSLATE)
2347 gdk_event_put (&temp_event);
2348 g_object_unref (temp_event.dnd.context);
2356 xwindow = GDK_WINDOW_XWINDOW (window);
2358 if (_gdk_x11_display_is_root_window (display, xwindow))
2359 event_mask = ButtonPressMask;
2363 send_client_message_async (context, xwindow, propagate, event_mask,
2364 &event_send->xclient);
2370 xdnd_send_enter (GdkDragContext *context)
2373 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2374 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2376 xev.xclient.type = ClientMessage;
2377 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2378 xev.xclient.format = 32;
2379 xev.xclient.window = private->drop_xid ?
2381 GDK_DRAWABLE_XID (context->dest_window);
2382 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2383 xev.xclient.data.l[1] = (private->version << 24); /* version */
2384 xev.xclient.data.l[2] = 0;
2385 xev.xclient.data.l[3] = 0;
2386 xev.xclient.data.l[4] = 0;
2389 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2390 GDK_DRAWABLE_XID (context->source_window), private->version));
2391 if (g_list_length (context->targets) > 3)
2393 if (!private->xdnd_targets_set)
2394 xdnd_set_targets (context);
2395 xev.xclient.data.l[1] |= 1;
2399 GList *tmp_list = context->targets;
2404 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2405 GDK_POINTER_TO_ATOM (tmp_list->data));
2406 tmp_list = tmp_list->next;
2411 if (!xdnd_send_xevent (context, context->dest_window,
2415 g_message ("Send event to %lx failed",
2416 GDK_DRAWABLE_XID (context->dest_window)));
2417 g_object_unref (context->dest_window);
2418 context->dest_window = NULL;
2423 xdnd_send_leave (GdkDragContext *context)
2425 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2428 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2430 xev.xclient.type = ClientMessage;
2431 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
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] = 0;
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_drop (GdkDragContext *context, guint32 time)
2456 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2457 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2460 xev.xclient.type = ClientMessage;
2461 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2462 xev.xclient.format = 32;
2463 xev.xclient.window = private->drop_xid ?
2465 GDK_DRAWABLE_XID (context->dest_window);
2466 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2467 xev.xclient.data.l[1] = 0;
2468 xev.xclient.data.l[2] = time;
2469 xev.xclient.data.l[3] = 0;
2470 xev.xclient.data.l[4] = 0;
2472 if (!xdnd_send_xevent (context, context->dest_window,
2476 g_message ("Send event to %lx failed",
2477 GDK_DRAWABLE_XID (context->dest_window)));
2478 g_object_unref (context->dest_window);
2479 context->dest_window = NULL;
2484 xdnd_send_motion (GdkDragContext *context,
2487 GdkDragAction action,
2490 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2491 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2494 xev.xclient.type = ClientMessage;
2495 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2496 xev.xclient.format = 32;
2497 xev.xclient.window = private->drop_xid ?
2499 GDK_DRAWABLE_XID (context->dest_window);
2500 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2501 xev.xclient.data.l[1] = 0;
2502 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2503 xev.xclient.data.l[3] = time;
2504 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2506 if (!xdnd_send_xevent (context, context->dest_window,
2510 g_message ("Send event to %lx failed",
2511 GDK_DRAWABLE_XID (context->dest_window)));
2512 g_object_unref (context->dest_window);
2513 context->dest_window = NULL;
2515 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2519 xdnd_check_dest (GdkDisplay *display,
2521 guint *xdnd_version)
2523 gboolean retval = FALSE;
2526 unsigned long nitems, after;
2531 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2532 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2536 gdk_error_trap_push ();
2538 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2540 1, False, AnyPropertyType,
2541 &type, &format, &nitems, &after,
2546 proxy_data = (Window *)data;
2548 if ((format == 32) && (nitems == 1))
2550 proxy = *proxy_data;
2554 g_warning ("Invalid XdndProxy "
2555 "property on window %ld\n", win));
2560 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2562 1, False, AnyPropertyType,
2563 &type, &format, &nitems, &after,
2564 &data) == Success) &&
2567 version = (Atom *)data;
2569 if ((format == 32) && (nitems == 1))
2574 *xdnd_version = *version;
2578 g_warning ("Invalid XdndAware "
2579 "property on window %ld\n", win));
2585 gdk_error_trap_pop ();
2587 return retval ? (proxy ? proxy : win) : None;
2593 xdnd_read_actions (GdkDragContext *context)
2595 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2598 gulong nitems, after;
2604 PRIVATE_DATA (context)->xdnd_have_actions = FALSE;
2606 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2608 /* Get the XdndActionList, if set */
2610 gdk_error_trap_push ();
2612 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2613 GDK_DRAWABLE_XID (context->source_window),
2614 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2616 False, XA_ATOM, &type, &format, &nitems,
2617 &after, &data) == Success &&
2620 atoms = (Atom *)data;
2622 context->actions = 0;
2624 for (i=0; i<nitems; i++)
2625 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2627 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2629 #ifdef G_ENABLE_DEBUG
2630 if (_gdk_debug_flags & GDK_DEBUG_DND)
2632 GString *action_str = g_string_new (NULL);
2633 if (context->actions & GDK_ACTION_MOVE)
2634 g_string_append(action_str, "MOVE ");
2635 if (context->actions & GDK_ACTION_COPY)
2636 g_string_append(action_str, "COPY ");
2637 if (context->actions & GDK_ACTION_LINK)
2638 g_string_append(action_str, "LINK ");
2639 if (context->actions & GDK_ACTION_ASK)
2640 g_string_append(action_str, "ASK ");
2642 g_message("Xdnd actions = %s", action_str->str);
2643 g_string_free (action_str, TRUE);
2645 #endif /* G_ENABLE_DEBUG */
2652 gdk_error_trap_pop ();
2658 GdkDragContext *source_context;
2660 source_context = gdk_drag_context_find (display, TRUE,
2661 GDK_DRAWABLE_XID (context->source_window),
2662 GDK_DRAWABLE_XID (context->dest_window));
2666 context->actions = source_context->actions;
2667 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2672 /* We have to make sure that the XdndActionList we keep internally
2673 * is up to date with the XdndActionList on the source window
2674 * because we get no notification, because Xdnd wasn't meant
2675 * to continually send actions. So we select on PropertyChangeMask
2676 * and add this filter.
2678 static GdkFilterReturn
2679 xdnd_source_window_filter (GdkXEvent *xev,
2683 XEvent *xevent = (XEvent *)xev;
2684 GdkDragContext *context = cb_data;
2685 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2687 if ((xevent->xany.type == PropertyNotify) &&
2688 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2690 xdnd_read_actions (context);
2692 return GDK_FILTER_REMOVE;
2695 return GDK_FILTER_CONTINUE;
2699 xdnd_manage_source_filter (GdkDragContext *context,
2701 gboolean add_filter)
2703 if (!GDK_WINDOW_DESTROYED (window) &&
2704 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2706 gdk_error_trap_push ();
2710 gdk_window_set_events (window,
2711 gdk_window_get_events (window) |
2712 GDK_PROPERTY_CHANGE_MASK);
2713 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2717 gdk_window_remove_filter (window,
2718 xdnd_source_window_filter,
2720 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2721 * but we might want it for other reasons. (Like
2722 * INCR selection transactions).
2726 gdk_display_sync (gdk_drawable_get_display (window));
2727 gdk_error_trap_pop ();
2732 base_precache_atoms (GdkDisplay *display)
2734 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2736 if (!display_x11->base_dnd_atoms_precached)
2738 static const char *const precache_atoms[] = {
2739 "ENLIGHTENMENT_DESKTOP",
2743 "_MOTIF_DRAG_RECEIVER_INFO"
2746 _gdk_x11_precache_atoms (display,
2747 precache_atoms, G_N_ELEMENTS (precache_atoms));
2749 display_x11->base_dnd_atoms_precached = TRUE;
2754 xdnd_precache_atoms (GdkDisplay *display)
2756 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2758 if (!display_x11->xdnd_atoms_precached)
2760 static const char *const precache_atoms[] = {
2766 "XdndActionPrivate",
2777 _gdk_x11_precache_atoms (display,
2778 precache_atoms, G_N_ELEMENTS (precache_atoms));
2780 display_x11->xdnd_atoms_precached = TRUE;
2784 static GdkFilterReturn
2785 xdnd_enter_filter (GdkXEvent *xev,
2789 GdkDisplay *display;
2790 GdkDisplayX11 *display_x11;
2791 XEvent *xevent = (XEvent *)xev;
2792 GdkDragContext *new_context;
2797 gulong nitems, after;
2801 guint32 source_window;
2805 if (!event->any.window ||
2806 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2807 return GDK_FILTER_CONTINUE; /* Not for us */
2809 source_window = xevent->xclient.data.l[0];
2810 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2811 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2813 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2814 display_x11 = GDK_DISPLAY_X11 (display);
2816 xdnd_precache_atoms (display);
2819 g_message ("XdndEnter: source_window: %#x, version: %#x",
2820 source_window, version));
2824 /* Old source ignore */
2825 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2826 return GDK_FILTER_REMOVE;
2829 if (display_x11->current_dest_drag != NULL)
2831 g_object_unref (display_x11->current_dest_drag);
2832 display_x11->current_dest_drag = NULL;
2835 new_context = gdk_drag_context_new ();
2836 new_context->protocol = GDK_DRAG_PROTO_XDND;
2837 PRIVATE_DATA(new_context)->version = version;
2839 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2840 if (new_context->source_window)
2841 g_object_ref (new_context->source_window);
2844 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2845 if (!new_context->source_window)
2847 g_object_unref (new_context);
2848 return GDK_FILTER_REMOVE;
2851 new_context->dest_window = event->any.window;
2852 g_object_ref (new_context->dest_window);
2854 new_context->targets = NULL;
2857 gdk_error_trap_push ();
2858 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2860 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2862 False, XA_ATOM, &type, &format, &nitems,
2865 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2867 g_object_unref (new_context);
2872 return GDK_FILTER_REMOVE;
2875 atoms = (Atom *)data;
2877 for (i=0; i<nitems; i++)
2878 new_context->targets =
2879 g_list_append (new_context->targets,
2880 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2888 if (xevent->xclient.data.l[2+i])
2889 new_context->targets =
2890 g_list_append (new_context->targets,
2891 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2892 xevent->xclient.data.l[2+i])));
2895 #ifdef G_ENABLE_DEBUG
2896 if (_gdk_debug_flags & GDK_DEBUG_DND)
2897 print_target_list (new_context->targets);
2898 #endif /* G_ENABLE_DEBUG */
2900 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2901 xdnd_read_actions (new_context);
2903 event->dnd.type = GDK_DRAG_ENTER;
2904 event->dnd.context = new_context;
2905 g_object_ref (new_context);
2907 display_x11->current_dest_drag = new_context;
2909 return GDK_FILTER_TRANSLATE;
2912 static GdkFilterReturn
2913 xdnd_leave_filter (GdkXEvent *xev,
2917 XEvent *xevent = (XEvent *)xev;
2918 guint32 source_window = xevent->xclient.data.l[0];
2919 GdkDisplay *display;
2920 GdkDisplayX11 *display_x11;
2922 if (!event->any.window ||
2923 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2924 return GDK_FILTER_CONTINUE; /* Not for us */
2927 g_message ("XdndLeave: source_window: %#x",
2930 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2931 display_x11 = GDK_DISPLAY_X11 (display);
2933 xdnd_precache_atoms (display);
2935 if ((display_x11->current_dest_drag != NULL) &&
2936 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2937 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2939 event->dnd.type = GDK_DRAG_LEAVE;
2940 /* Pass ownership of context to the event */
2941 event->dnd.context = display_x11->current_dest_drag;
2943 display_x11->current_dest_drag = NULL;
2945 return GDK_FILTER_TRANSLATE;
2948 return GDK_FILTER_REMOVE;
2951 static GdkFilterReturn
2952 xdnd_position_filter (GdkXEvent *xev,
2956 XEvent *xevent = (XEvent *)xev;
2957 guint32 source_window = xevent->xclient.data.l[0];
2958 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2959 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2960 guint32 time = xevent->xclient.data.l[3];
2961 Atom action = xevent->xclient.data.l[4];
2963 GdkDisplay *display;
2964 GdkDisplayX11 *display_x11;
2966 if (!event->any.window ||
2967 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2968 return GDK_FILTER_CONTINUE; /* Not for us */
2971 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2972 source_window, x_root, y_root, time, action));
2974 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2975 display_x11 = GDK_DISPLAY_X11 (display);
2977 xdnd_precache_atoms (display);
2979 if ((display_x11->current_dest_drag != NULL) &&
2980 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2981 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2983 event->dnd.type = GDK_DRAG_MOTION;
2984 event->dnd.context = display_x11->current_dest_drag;
2985 g_object_ref (display_x11->current_dest_drag);
2987 event->dnd.time = time;
2989 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2991 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2992 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2994 event->dnd.x_root = x_root;
2995 event->dnd.y_root = y_root;
2997 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2998 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
3000 return GDK_FILTER_TRANSLATE;
3003 return GDK_FILTER_REMOVE;
3006 static GdkFilterReturn
3007 xdnd_drop_filter (GdkXEvent *xev,
3011 XEvent *xevent = (XEvent *)xev;
3012 guint32 source_window = xevent->xclient.data.l[0];
3013 guint32 time = xevent->xclient.data.l[2];
3014 GdkDisplay *display;
3015 GdkDisplayX11 *display_x11;
3017 if (!event->any.window ||
3018 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
3019 return GDK_FILTER_CONTINUE; /* Not for us */
3022 g_message ("XdndDrop: source_window: %#x time: %d",
3023 source_window, time));
3025 display = GDK_DRAWABLE_DISPLAY (event->any.window);
3026 display_x11 = GDK_DISPLAY_X11 (display);
3028 xdnd_precache_atoms (display);
3030 if ((display_x11->current_dest_drag != NULL) &&
3031 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
3032 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
3034 GdkDragContextPrivateX11 *private;
3035 private = PRIVATE_DATA (display_x11->current_dest_drag);
3037 event->dnd.type = GDK_DROP_START;
3039 event->dnd.context = display_x11->current_dest_drag;
3040 g_object_ref (display_x11->current_dest_drag);
3042 event->dnd.time = time;
3043 event->dnd.x_root = private->last_x;
3044 event->dnd.y_root = private->last_y;
3046 gdk_x11_window_set_user_time (event->any.window, time);
3048 return GDK_FILTER_TRANSLATE;
3051 return GDK_FILTER_REMOVE;
3054 /*************************************************************
3055 ************************** Public API ***********************
3056 *************************************************************/
3058 _gdk_dnd_init (GdkDisplay *display)
3063 gdk_display_add_client_message_filter (
3065 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
3066 motif_dnd_filter, NULL);
3068 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
3070 gdk_display_add_client_message_filter (
3072 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
3073 xdnd_filters[i].func, NULL);
3080 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
3082 if (context->dest_window)
3084 switch (context->protocol)
3086 case GDK_DRAG_PROTO_MOTIF:
3087 motif_send_leave (context, time);
3089 case GDK_DRAG_PROTO_XDND:
3090 xdnd_send_leave (context);
3092 case GDK_DRAG_PROTO_ROOTWIN:
3093 case GDK_DRAG_PROTO_NONE:
3098 g_object_unref (context->dest_window);
3099 context->dest_window = NULL;
3105 * @window: the source window for this drag.
3106 * @targets: the list of offered targets.
3108 * Starts a drag and creates a new drag context for it.
3110 * This function is called by the drag source.
3112 * Return value: a newly created #GdkDragContext.
3115 gdk_drag_begin (GdkWindow *window,
3118 GdkDragContext *new_context;
3120 g_return_val_if_fail (window != NULL, NULL);
3121 g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), NULL);
3123 new_context = gdk_drag_context_new ();
3124 new_context->is_source = TRUE;
3125 new_context->source_window = window;
3126 g_object_ref (window);
3128 new_context->targets = g_list_copy (targets);
3129 precache_target_list (new_context);
3131 new_context->actions = 0;
3136 static GdkNativeWindow
3137 _gdk_drag_get_protocol_for_display (GdkDisplay *display,
3138 GdkNativeWindow xid,
3139 GdkDragProtocol *protocol,
3144 GdkNativeWindow retval;
3145 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
3147 base_precache_atoms (display);
3149 /* Check for a local drag
3151 window = gdk_window_lookup_for_display (display, xid);
3153 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3155 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3157 *protocol = GDK_DRAG_PROTO_XDND;
3159 xdnd_precache_atoms (display);
3160 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3163 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3165 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3166 GDK_NOTE (DND, g_message ("Entering root window\n"));
3170 else if ((retval = xdnd_check_dest (display, xid, version)))
3172 *protocol = GDK_DRAG_PROTO_XDND;
3173 xdnd_precache_atoms (display);
3174 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3177 else if ((retval = motif_check_dest (display, xid)))
3179 *protocol = GDK_DRAG_PROTO_MOTIF;
3180 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3185 /* Check if this is a root window */
3187 gboolean rootwin = FALSE;
3190 unsigned long nitems, after;
3191 unsigned char *data;
3193 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3196 gdk_error_trap_push ();
3200 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3201 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
3202 0, 0, False, AnyPropertyType,
3203 &type, &format, &nitems, &after, &data) == Success &&
3211 /* Until I find out what window manager the next one is for,
3212 * I'm leaving it commented out. It's supported in the
3213 * xscreensaver sources, though.
3218 if (XGetWindowProperty (gdk_display, win,
3219 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
3220 0, 0, False, AnyPropertyType,
3221 &type, &format, &nitems, &data) &&
3230 gdk_error_trap_pop ();
3234 GDK_NOTE (DND, g_message ("Entering root window\n"));
3235 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3240 *protocol = GDK_DRAG_PROTO_NONE;
3242 return 0; /* a.k.a. None */
3246 * gdk_drag_get_protocol_for_display:
3247 * @display: the #GdkDisplay where the destination window resides
3248 * @xid: the windowing system id of the destination window.
3249 * @protocol: location where the supported DND protocol is returned.
3250 * @returns: the windowing system id of the window where the drop should happen. This
3251 * may be @xid or the id of a proxy window, or zero if @xid doesn't
3252 * support Drag and Drop.
3254 * Finds out the DND protocol supported by a window.
3259 gdk_drag_get_protocol_for_display (GdkDisplay *display,
3260 GdkNativeWindow xid,
3261 GdkDragProtocol *protocol)
3263 return _gdk_drag_get_protocol_for_display (display, xid, protocol, NULL);
3266 static GdkWindowCache *
3267 drag_context_find_window_cache (GdkDragContext *context,
3270 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3272 GdkWindowCache *cache;
3274 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
3276 cache = tmp_list->data;
3277 if (cache->screen == screen)
3281 cache = gdk_window_cache_new (screen);
3282 private->window_caches = g_slist_prepend (private->window_caches, cache);
3288 * gdk_drag_find_window_for_screen:
3289 * @context: a #GdkDragContext
3290 * @drag_window: a window which may be at the pointer position, but
3291 * should be ignored, since it is put up by the drag source as an icon.
3292 * @screen: the screen where the destination window is sought.
3293 * @x_root: the x position of the pointer in root coordinates.
3294 * @y_root: the y position of the pointer in root coordinates.
3295 * @dest_window: (out): location to store the destination window in.
3296 * @protocol: (out): location to store the DND protocol in.
3298 * Finds the destination window and DND protocol to use at the
3299 * given pointer position.
3301 * This function is called by the drag source to obtain the
3302 * @dest_window and @protocol parameters for gdk_drag_motion().
3307 gdk_drag_find_window_for_screen (GdkDragContext *context,
3308 GdkWindow *drag_window,
3312 GdkWindow **dest_window,
3313 GdkDragProtocol *protocol)
3315 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3316 GdkWindowCache *window_cache;
3317 GdkDisplay *display;
3320 g_return_if_fail (context != NULL);
3322 display = GDK_WINDOW_DISPLAY (context->source_window);
3324 window_cache = drag_context_find_window_cache (context, screen);
3326 dest = get_client_window_at_coords (window_cache,
3327 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3328 GDK_DRAWABLE_XID (drag_window) : None,
3331 if (private->dest_xid != dest)
3334 private->dest_xid = dest;
3336 /* Check if new destination accepts drags, and which protocol */
3338 /* There is some ugliness here. We actually need to pass
3339 * _three_ pieces of information to drag_motion - dest_window,
3340 * protocol, and the XID of the unproxied window. The first
3341 * two are passed explicitely, the third implicitly through
3342 * protocol->dest_xid.
3344 if ((recipient = _gdk_drag_get_protocol_for_display (display, dest,
3345 protocol, &private->version)))
3347 *dest_window = gdk_window_lookup_for_display (display, recipient);
3349 g_object_ref (*dest_window);
3351 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3354 *dest_window = NULL;
3358 *dest_window = context->dest_window;
3360 g_object_ref (*dest_window);
3361 *protocol = context->protocol;
3367 * @context: a #GdkDragContext.
3368 * @dest_window: the new destination window, obtained by
3369 * gdk_drag_find_window().
3370 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3371 * @x_root: the x position of the pointer in root coordinates.
3372 * @y_root: the y position of the pointer in root coordinates.
3373 * @suggested_action: the suggested action.
3374 * @possible_actions: the possible actions.
3375 * @time_: the timestamp for this operation.
3377 * Updates the drag context when the pointer moves or the
3378 * set of actions changes.
3380 * This function is called by the drag source.
3382 * Return value: FIXME
3385 gdk_drag_motion (GdkDragContext *context,
3386 GdkWindow *dest_window,
3387 GdkDragProtocol protocol,
3390 GdkDragAction suggested_action,
3391 GdkDragAction possible_actions,
3394 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3396 g_return_val_if_fail (context != NULL, FALSE);
3397 g_return_val_if_fail (dest_window == NULL || GDK_WINDOW_IS_X11 (dest_window), FALSE);
3399 private->old_actions = context->actions;
3400 context->actions = possible_actions;
3402 if (private->old_actions != possible_actions)
3403 private->xdnd_actions_set = FALSE;
3405 if (protocol == GDK_DRAG_PROTO_XDND && private->version == 0)
3407 /* This ugly hack is necessary since GTK+ doesn't know about
3408 * the XDND protocol version, and in particular doesn't know
3409 * that gdk_drag_find_window_for_screen() has the side-effect
3410 * of setting private->version, and therefore sometimes call
3411 * gdk_drag_motion() without a prior call to
3412 * gdk_drag_find_window_for_screen(). This happens, e.g.
3413 * when GTK+ is proxying DND events to embedded windows.
3417 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3419 xdnd_check_dest (display,
3420 GDK_DRAWABLE_XID (dest_window),
3425 /* When we have a Xdnd target, make sure our XdndActionList
3426 * matches the current actions;
3428 if (protocol == GDK_DRAG_PROTO_XDND && !private->xdnd_actions_set)
3432 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3433 xdnd_set_actions (context);
3434 else if (context->dest_window == dest_window)
3436 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3437 GdkDragContext *dest_context;
3439 dest_context = gdk_drag_context_find (display, FALSE,
3440 GDK_DRAWABLE_XID (context->source_window),
3441 GDK_DRAWABLE_XID (dest_window));
3445 dest_context->actions = context->actions;
3446 PRIVATE_DATA (dest_context)->xdnd_have_actions = TRUE;
3452 if (context->dest_window != dest_window)
3454 GdkEvent temp_event;
3456 /* Send a leave to the last destination */
3457 gdk_drag_do_leave (context, time);
3458 private->drag_status = GDK_DRAG_STATUS_DRAG;
3460 /* Check if new destination accepts drags, and which protocol */
3464 context->dest_window = dest_window;
3465 private->drop_xid = private->dest_xid;
3466 g_object_ref (context->dest_window);
3467 context->protocol = protocol;
3471 case GDK_DRAG_PROTO_MOTIF:
3472 motif_send_enter (context, time);
3475 case GDK_DRAG_PROTO_XDND:
3476 xdnd_send_enter (context);
3479 case GDK_DRAG_PROTO_ROOTWIN:
3480 case GDK_DRAG_PROTO_NONE:
3484 private->old_action = suggested_action;
3485 context->suggested_action = suggested_action;
3486 private->old_actions = possible_actions;
3490 context->dest_window = NULL;
3491 private->drop_xid = None;
3492 context->action = 0;
3495 /* Push a status event, to let the client know that
3499 temp_event.dnd.type = GDK_DRAG_STATUS;
3500 temp_event.dnd.window = context->source_window;
3501 /* We use this to signal a synthetic status. Perhaps
3502 * we should use an extra field...
3504 temp_event.dnd.send_event = TRUE;
3506 temp_event.dnd.context = context;
3507 temp_event.dnd.time = time;
3509 gdk_event_put (&temp_event);
3513 private->old_action = context->suggested_action;
3514 context->suggested_action = suggested_action;
3517 /* Send a drag-motion event */
3519 private->last_x = x_root;
3520 private->last_y = y_root;
3522 if (context->dest_window)
3524 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3526 switch (context->protocol)
3528 case GDK_DRAG_PROTO_MOTIF:
3529 motif_send_motion (context, x_root, y_root, suggested_action, time);
3532 case GDK_DRAG_PROTO_XDND:
3533 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3536 case GDK_DRAG_PROTO_ROOTWIN:
3538 GdkEvent temp_event;
3539 /* GTK+ traditionally has used application/x-rootwin-drop,
3540 * but the XDND spec specifies x-rootwindow-drop.
3542 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3543 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3545 if (g_list_find (context->targets,
3546 GDK_ATOM_TO_POINTER (target1)) ||
3547 g_list_find (context->targets,
3548 GDK_ATOM_TO_POINTER (target2)))
3549 context->action = context->suggested_action;
3551 context->action = 0;
3553 temp_event.dnd.type = GDK_DRAG_STATUS;
3554 temp_event.dnd.window = context->source_window;
3555 temp_event.dnd.send_event = FALSE;
3556 temp_event.dnd.context = context;
3557 temp_event.dnd.time = time;
3559 gdk_event_put (&temp_event);
3562 case GDK_DRAG_PROTO_NONE:
3563 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3578 * @context: a #GdkDragContext.
3579 * @time_: the timestamp for this operation.
3581 * Drops on the current destination.
3583 * This function is called by the drag source.
3586 gdk_drag_drop (GdkDragContext *context,
3589 g_return_if_fail (context != NULL);
3591 if (context->dest_window)
3593 switch (context->protocol)
3595 case GDK_DRAG_PROTO_MOTIF:
3596 motif_send_leave (context, time);
3597 motif_send_drop (context, time);
3600 case GDK_DRAG_PROTO_XDND:
3601 xdnd_send_drop (context, time);
3604 case GDK_DRAG_PROTO_ROOTWIN:
3605 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3607 case GDK_DRAG_PROTO_NONE:
3608 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3618 * @context: a #GdkDragContext.
3619 * @time_: the timestamp for this operation.
3621 * Aborts a drag without dropping.
3623 * This function is called by the drag source.
3626 gdk_drag_abort (GdkDragContext *context,
3629 g_return_if_fail (context != NULL);
3631 gdk_drag_do_leave (context, time);
3634 /* Destination side */
3638 * @context: a #GdkDragContext.
3639 * @action: the selected action which will be taken when a drop happens,
3640 * or 0 to indicate that a drop will not be accepted.
3641 * @time_: the timestamp for this operation.
3643 * Selects one of the actions offered by the drag source.
3645 * This function is called by the drag destination in response to
3646 * gdk_drag_motion() called by the drag source.
3649 gdk_drag_status (GdkDragContext *context,
3650 GdkDragAction action,
3653 GdkDragContextPrivateX11 *private;
3655 GdkDisplay *display;
3657 g_return_if_fail (context != NULL);
3659 private = PRIVATE_DATA (context);
3660 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3662 context->action = action;
3664 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3666 gboolean need_coords = FALSE;
3668 xev.xclient.type = ClientMessage;
3669 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3670 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3671 xev.xclient.format = 8;
3672 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3674 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3676 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3680 if ((action != 0) != (private->old_action != 0))
3684 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3688 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3692 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3697 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3701 case GDK_ACTION_MOVE:
3702 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3704 case GDK_ACTION_COPY:
3705 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3707 case GDK_ACTION_LINK:
3708 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3711 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3716 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3718 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3720 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3724 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3725 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3728 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3730 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3731 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3733 if (!_gdk_send_xevent (display,
3734 GDK_DRAWABLE_XID (context->source_window),
3737 g_message ("Send event to %lx failed",
3738 GDK_DRAWABLE_XID (context->source_window)));
3740 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3742 xev.xclient.type = ClientMessage;
3743 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3744 xev.xclient.format = 32;
3745 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3747 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3748 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3749 xev.xclient.data.l[2] = 0;
3750 xev.xclient.data.l[3] = 0;
3751 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3753 if (!xdnd_send_xevent (context, context->source_window,
3756 g_message ("Send event to %lx failed",
3757 GDK_DRAWABLE_XID (context->source_window)));
3760 private->old_action = action;
3765 * @context: a #GdkDragContext.
3766 * @ok: %TRUE if the drop is accepted.
3767 * @time_: the timestamp for this operation.
3769 * Accepts or rejects a drop.
3771 * This function is called by the drag destination in response
3772 * to a drop initiated by the drag source.
3775 gdk_drop_reply (GdkDragContext *context,
3779 GdkDragContextPrivateX11 *private;
3781 g_return_if_fail (context != NULL);
3783 private = PRIVATE_DATA (context);
3785 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3787 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3790 xev.xclient.type = ClientMessage;
3791 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3792 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3793 xev.xclient.format = 8;
3795 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3796 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3798 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3799 (XmDROP_SITE_VALID << 4) |
3800 (XmDROP_NOOP << 8) |
3803 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3804 (XmNO_DROP_SITE << 4) |
3805 (XmDROP_NOOP << 8) |
3806 (XmDROP_CANCEL << 12);
3807 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3808 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3809 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3810 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3811 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3813 _gdk_send_xevent (display,
3814 GDK_DRAWABLE_XID (context->source_window),
3821 * @context: a #GtkDragContext.
3822 * @success: %TRUE if the data was successfully received.
3823 * @time_: the timestamp for this operation.
3825 * Ends the drag operation after a drop.
3827 * This function is called by the drag destination.
3830 gdk_drop_finish (GdkDragContext *context,
3834 g_return_if_fail (context != NULL);
3836 if (context->protocol == GDK_DRAG_PROTO_XDND)
3838 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3841 xev.xclient.type = ClientMessage;
3842 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3843 xev.xclient.format = 32;
3844 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3846 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3849 xev.xclient.data.l[1] = 1;
3850 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3855 xev.xclient.data.l[1] = 0;
3856 xev.xclient.data.l[2] = None;
3858 xev.xclient.data.l[3] = 0;
3859 xev.xclient.data.l[4] = 0;
3861 if (!xdnd_send_xevent (context, context->source_window,
3864 g_message ("Send event to %lx failed",
3865 GDK_DRAWABLE_XID (context->source_window)));
3871 gdk_window_register_dnd (GdkWindow *window)
3873 static const gulong xdnd_version = 5;
3874 MotifDragReceiverInfo info;
3875 Atom motif_drag_receiver_info_atom;
3876 GdkDisplay *display = gdk_drawable_get_display (window);
3878 g_return_if_fail (window != NULL);
3880 base_precache_atoms (display);
3882 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3885 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3887 /* Set Motif drag receiver information property */
3889 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3890 "_MOTIF_DRAG_RECEIVER_INFO");
3891 /* initialize to zero to avoid writing uninitialized data to socket */
3892 memset(&info, 0, sizeof(info));
3893 info.byte_order = local_byte_order;
3894 info.protocol_version = 0;
3895 info.protocol_style = XmDRAG_DYNAMIC;
3896 info.proxy_window = None;
3897 info.num_drop_sites = 0;
3898 info.total_size = sizeof(info);
3900 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3901 motif_drag_receiver_info_atom,
3902 motif_drag_receiver_info_atom,
3909 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3910 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3911 GDK_DRAWABLE_XID (window),
3912 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3913 XA_ATOM, 32, PropModeReplace,
3914 (guchar *)&xdnd_version, 1);
3918 * gdk_drag_get_selection:
3919 * @context: a #GdkDragContext.
3921 * Returns the selection atom for the current source window.
3923 * Return value: the selection atom.
3926 gdk_drag_get_selection (GdkDragContext *context)
3928 g_return_val_if_fail (context != NULL, GDK_NONE);
3930 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3931 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3932 (PRIVATE_DATA (context))->motif_selection);
3933 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3934 return gdk_atom_intern_static_string ("XdndSelection");
3940 * gdk_drag_drop_succeeded:
3941 * @context: a #GdkDragContext
3943 * Returns whether the dropped data has been successfully
3944 * transferred. This function is intended to be used while
3945 * handling a %GDK_DROP_FINISHED event, its return value is
3946 * meaningless at other times.
3948 * Return value: %TRUE if the drop was successful.
3953 gdk_drag_drop_succeeded (GdkDragContext *context)
3955 GdkDragContextPrivateX11 *private;
3957 g_return_val_if_fail (context != NULL, FALSE);
3959 private = PRIVATE_DATA (context);
3961 return !private->drop_failed;
3964 #define __GDK_DND_X11_C__
3965 #include "gdkaliasdef.c"