1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1999 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include <X11/Xatom.h>
32 #include "gdk.h" /* For gdk_flush() */
36 #include "gdkproperty.h"
37 #include "gdkprivate-x11.h"
38 #include "gdkinternals.h"
39 #include "gdkscreen-x11.h"
40 #include "gdkdisplay-x11.h"
43 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
47 GDK_DRAG_STATUS_MOTION_WAIT,
48 GDK_DRAG_STATUS_ACTION_WAIT,
54 gint x, y, width, height;
60 GHashTable *child_hash;
65 /* Structure that holds information about a drag in progress.
66 * this is used on both source and destination sides.
68 struct _GdkDragContextPrivateX11 {
69 GdkDragContext context;
74 guint16 last_x; /* Coordinates from last event */
76 GdkDragAction old_action; /* The last action we sent to the source */
77 GdkDragAction old_actions; /* The last actions we sent to the source */
78 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
80 Window dest_xid; /* The last window we looked up */
81 Window drop_xid; /* The (non-proxied) window that is receiving drops */
82 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
83 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
84 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
85 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
86 guint drag_status : 4; /* current status of drag */
88 guint drop_failed : 1; /* Whether the drop was unsuccessful */
89 guint version; /* Xdnd protocol version */
91 GSList *window_caches;
94 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
96 /* Forward declarations */
98 static void gdk_window_cache_destroy (GdkWindowCache *cache);
100 static void motif_read_target_table (GdkDisplay *display);
102 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
106 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
109 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
112 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
115 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
118 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
121 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
125 static void xdnd_manage_source_filter (GdkDragContext *context,
127 gboolean add_filter);
129 static void gdk_drag_context_finalize (GObject *object);
131 static GList *contexts;
133 static const struct {
134 const char *atom_name;
137 { "XdndEnter", xdnd_enter_filter },
138 { "XdndLeave", xdnd_leave_filter },
139 { "XdndPosition", xdnd_position_filter },
140 { "XdndStatus", xdnd_status_filter },
141 { "XdndFinished", xdnd_finished_filter },
142 { "XdndDrop", xdnd_drop_filter },
145 G_DEFINE_TYPE (GdkDragContext, gdk_drag_context, G_TYPE_OBJECT)
148 gdk_drag_context_init (GdkDragContext *dragcontext)
150 GdkDragContextPrivateX11 *private;
152 private = G_TYPE_INSTANCE_GET_PRIVATE (dragcontext,
153 GDK_TYPE_DRAG_CONTEXT,
154 GdkDragContextPrivateX11);
156 dragcontext->windowing_data = private;
158 contexts = g_list_prepend (contexts, dragcontext);
162 gdk_drag_context_class_init (GdkDragContextClass *klass)
164 GObjectClass *object_class = G_OBJECT_CLASS (klass);
166 object_class->finalize = gdk_drag_context_finalize;
168 g_type_class_add_private (object_class, sizeof (GdkDragContextPrivateX11));
172 gdk_drag_context_finalize (GObject *object)
174 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
175 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
178 g_list_free (context->targets);
180 if (context->source_window)
182 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
184 xdnd_manage_source_filter (context, context->source_window, FALSE);
186 g_object_unref (context->source_window);
189 if (context->dest_window)
190 g_object_unref (context->dest_window);
192 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
193 gdk_window_cache_destroy (tmp_list->data);
194 g_slist_free (private->window_caches);
196 contexts = g_list_remove (contexts, context);
198 G_OBJECT_CLASS (gdk_drag_context_parent_class)->finalize (object);
204 * gdk_drag_context_new:
206 * Creates a new #GdkDragContext.
208 * Return value: the newly created #GdkDragContext.
211 gdk_drag_context_new (void)
213 return g_object_new (GDK_TYPE_DRAG_CONTEXT, NULL);
217 * gdk_drag_context_ref:
218 * @context: a #GdkDragContext.
220 * Deprecated function; use g_object_ref() instead.
223 gdk_drag_context_ref (GdkDragContext *context)
225 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
227 g_object_ref (context);
231 * gdk_drag_context_unref:
232 * @context: a #GdkDragContext.
234 * Deprecated function; use g_object_unref() instead.
237 gdk_drag_context_unref (GdkDragContext *context)
239 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
241 g_object_unref (context);
244 static GdkDragContext *
245 gdk_drag_context_find (GdkDisplay *display,
250 GList *tmp_list = contexts;
251 GdkDragContext *context;
252 GdkDragContextPrivateX11 *private;
253 Window context_dest_xid;
257 context = (GdkDragContext *)tmp_list->data;
258 private = PRIVATE_DATA (context);
260 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
261 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
264 context_dest_xid = context->dest_window ?
267 GDK_DRAWABLE_XID (context->dest_window)) :
270 if ((!context->is_source == !is_source) &&
271 ((source_xid == None) || (context->source_window &&
272 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
273 ((dest_xid == None) || (context_dest_xid == dest_xid)))
276 tmp_list = tmp_list->next;
283 precache_target_list (GdkDragContext *context)
285 if (context->targets)
287 GPtrArray *targets = g_ptr_array_new ();
291 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
292 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
294 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
295 (const gchar **)targets->pdata,
298 for (i =0; i < targets->len; i++)
299 g_free (targets->pdata[i]);
301 g_ptr_array_free (targets, TRUE);
305 /* Utility functions */
308 gdk_window_cache_add (GdkWindowCache *cache,
310 gint x, gint y, gint width, gint height,
313 GdkCacheChild *child = g_new (GdkCacheChild, 1);
318 child->width = width;
319 child->height = height;
320 child->mapped = mapped;
322 cache->children = g_list_prepend (cache->children, child);
323 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
327 static GdkFilterReturn
328 gdk_window_cache_filter (GdkXEvent *xev,
332 XEvent *xevent = (XEvent *)xev;
333 GdkWindowCache *cache = data;
335 switch (xevent->type)
337 case CirculateNotify:
339 case ConfigureNotify:
341 XConfigureEvent *xce = &xevent->xconfigure;
344 node = g_hash_table_lookup (cache->child_hash,
345 GUINT_TO_POINTER (xce->window));
348 GdkCacheChild *child = node->data;
351 child->width = xce->width;
352 child->height = xce->height;
353 if (xce->above == None && (node->next))
355 GList *last = g_list_last (cache->children);
356 cache->children = g_list_remove_link (cache->children, node);
363 GList *above_node = g_hash_table_lookup (cache->child_hash,
364 GUINT_TO_POINTER (xce->above));
365 if (above_node && node->next != above_node)
367 /* Put the window above (before in the list) above_node
369 cache->children = g_list_remove_link (cache->children, node);
370 node->prev = above_node->prev;
372 node->prev->next = node;
374 cache->children = node;
375 node->next = above_node;
376 above_node->prev = node;
384 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
386 if (!g_hash_table_lookup (cache->child_hash,
387 GUINT_TO_POINTER (xcwe->window)))
388 gdk_window_cache_add (cache, xcwe->window,
389 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
395 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
398 node = g_hash_table_lookup (cache->child_hash,
399 GUINT_TO_POINTER (xdwe->window));
402 g_hash_table_remove (cache->child_hash,
403 GUINT_TO_POINTER (xdwe->window));
404 cache->children = g_list_remove_link (cache->children, node);
406 g_list_free_1 (node);
412 XMapEvent *xme = &xevent->xmap;
415 node = g_hash_table_lookup (cache->child_hash,
416 GUINT_TO_POINTER (xme->window));
419 GdkCacheChild *child = node->data;
420 child->mapped = TRUE;
428 XMapEvent *xume = &xevent->xmap;
431 node = g_hash_table_lookup (cache->child_hash,
432 GUINT_TO_POINTER (xume->window));
435 GdkCacheChild *child = node->data;
436 child->mapped = FALSE;
441 return GDK_FILTER_CONTINUE;
443 return GDK_FILTER_REMOVE;
446 static GdkWindowCache *
447 gdk_window_cache_new (GdkScreen *screen)
449 XWindowAttributes xwa;
450 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
451 GdkWindow *root_window = gdk_screen_get_root_window (screen);
452 GdkChildInfoX11 *children;
455 GdkWindowCache *result = g_new (GdkWindowCache, 1);
457 result->children = NULL;
458 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
459 result->screen = screen;
461 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
462 result->old_event_mask = xwa.your_event_mask;
464 if (G_UNLIKELY (!GDK_DISPLAY_X11 (GDK_SCREEN_X11 (screen)->display)->trusted_client))
466 GList *toplevel_windows, *list;
468 gint x, y, width, height;
470 toplevel_windows = gdk_screen_get_toplevel_windows (screen);
471 for (list = toplevel_windows; list; list = list->next) {
472 window = GDK_WINDOW (list->data);
473 gdk_window_get_geometry (window, &x, &y, &width, &height, NULL);
474 gdk_window_cache_add (result, GDK_WINDOW_XID (window),
476 gdk_window_is_visible (window));
478 g_list_free (toplevel_windows);
482 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
483 result->old_event_mask | SubstructureNotifyMask);
484 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
486 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
487 GDK_WINDOW_XWINDOW (root_window),
489 &children, &nchildren))
492 for (i = 0; i < nchildren ; i++)
494 gdk_window_cache_add (result, children[i].window,
495 children[i].x, children[i].y, children[i].width, children[i].height,
496 children[i].is_mapped);
505 gdk_window_cache_destroy (GdkWindowCache *cache)
507 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
509 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
510 GDK_WINDOW_XWINDOW (root_window),
511 cache->old_event_mask);
512 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
514 g_list_foreach (cache->children, (GFunc)g_free, NULL);
515 g_list_free (cache->children);
516 g_hash_table_destroy (cache->child_hash);
522 get_client_window_at_coords_recurse (GdkDisplay *display,
524 gboolean is_toplevel,
528 GdkChildInfoX11 *children;
529 unsigned int nchildren;
531 gboolean found_child = FALSE;
532 GdkChildInfoX11 child = { 0, };
533 gboolean has_wm_state = FALSE;
535 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
536 is_toplevel? &has_wm_state : NULL,
537 &children, &nchildren))
547 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
549 GdkChildInfoX11 *cur_child = &children[i];
551 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
552 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
553 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
566 if (child.has_wm_state)
569 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
576 get_client_window_at_coords (GdkWindowCache *cache,
582 Window retval = None;
584 gdk_error_trap_push ();
586 tmp_list = cache->children;
588 while (tmp_list && !retval)
590 GdkCacheChild *child = tmp_list->data;
592 if ((child->xid != ignore) && (child->mapped))
594 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
595 (y_root >= child->y) && (y_root < child->y + child->height))
597 retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
606 tmp_list = tmp_list->next;
609 gdk_error_trap_pop ();
614 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
617 /*************************************************************
618 ***************************** MOTIF *************************
619 *************************************************************/
621 /* values used in the message type for Motif DND */
634 /* Values used to specify type of protocol to use */
638 XmDRAG_PREFER_PREREGISTER,
640 XmDRAG_PREFER_DYNAMIC,
642 XmDRAG_PREFER_RECEIVER
645 /* Operation codes */
653 /* Drop site status */
655 XmNO_DROP_SITE = 0x01,
656 XmDROP_SITE_INVALID = 0x02,
657 XmDROP_SITE_VALID = 0x03
660 /* completion status */
668 /* Byte swapping routines. The motif specification leaves it
669 * up to us to save a few bytes in the client messages
671 static gchar local_byte_order = '\0';
673 #ifdef G_ENABLE_DEBUG
675 print_target_list (GList *targets)
679 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
680 g_message ("\t%s", name);
682 targets = targets->next;
685 #endif /* G_ENABLE_DEBUG */
688 init_byte_order (void)
690 guint32 myint = 0x01020304;
691 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
695 card16_to_host (guint16 x, gchar byte_order) {
696 if (byte_order == local_byte_order)
699 return (x << 8) | (x >> 8);
703 card32_to_host (guint32 x, gchar byte_order) {
704 if (byte_order == local_byte_order)
707 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
710 /* Motif packs together fields of varying length into the
711 * client message. We can't rely on accessing these
712 * through data.s[], data.l[], etc, because on some architectures
713 * (i.e., Alpha) these won't be valid for format == 8.
716 #define MOTIF_XCLIENT_BYTE(xevent,i) \
717 (xevent)->xclient.data.b[i]
718 #define MOTIF_XCLIENT_SHORT(xevent,i) \
719 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
720 #define MOTIF_XCLIENT_LONG(xevent,i) \
721 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
723 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
724 #define MOTIF_UNPACK_SHORT(xevent,i) \
725 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
726 #define MOTIF_UNPACK_LONG(xevent,i) \
727 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
729 /***** Dest side ***********/
731 /* Property placed on source windows */
732 typedef struct _MotifDragInitiatorInfo {
734 guint8 protocol_version;
735 guint16 targets_index;
736 guint32 selection_atom;
737 } MotifDragInitiatorInfo;
739 /* Header for target table on the drag window */
740 typedef struct _MotifTargetTableHeader {
742 guchar protocol_version;
745 } MotifTargetTableHeader;
747 /* Property placed on target windows */
748 typedef struct _MotifDragReceiverInfo {
750 guint8 protocol_version;
751 guint8 protocol_style;
753 guint32 proxy_window;
754 guint16 num_drop_sites;
757 } MotifDragReceiverInfo;
759 /* Target table handling */
761 static GdkFilterReturn
762 motif_drag_window_filter (GdkXEvent *xevent,
766 XEvent *xev = (XEvent *)xevent;
767 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
768 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
770 switch (xev->xany.type)
773 display_x11->motif_drag_window = None;
774 display_x11->motif_drag_gdk_window = NULL;
777 if (display_x11->motif_target_lists &&
778 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
779 motif_read_target_table (display);
782 return GDK_FILTER_REMOVE;
786 motif_lookup_drag_window (GdkDisplay *display,
787 Display *lookup_xdisplay)
789 Window retval = None;
790 gulong bytes_after, nitems;
795 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
796 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
798 XA_WINDOW, &type, &format, &nitems, &bytes_after,
801 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
803 retval = *(Window *)data;
805 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
814 /* Finds the window where global Motif drag information is stored.
815 * If it doesn't exist and 'create' is TRUE, create one.
818 motif_find_drag_window (GdkDisplay *display,
821 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
823 if (!display_x11->motif_drag_window)
825 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
826 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
828 if (!display_x11->motif_drag_window && create)
830 /* Create a persistant window. (Copied from LessTif) */
832 Display *persistant_xdisplay;
833 XSetWindowAttributes attr;
834 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
835 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
837 XGrabServer (persistant_xdisplay);
839 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
841 if (!display_x11->motif_drag_window)
843 attr.override_redirect = True;
844 attr.event_mask = PropertyChangeMask;
846 display_x11->motif_drag_window =
847 XCreateWindow (persistant_xdisplay,
848 RootWindow (persistant_xdisplay, 0),
849 -100, -100, 10, 10, 0, 0,
850 InputOnly, (Visual *)CopyFromParent,
851 (CWOverrideRedirect | CWEventMask), &attr);
854 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
856 XChangeProperty (persistant_xdisplay,
857 RootWindow (persistant_xdisplay, 0),
858 motif_drag_window_atom, XA_WINDOW,
860 (guchar *)&motif_drag_window_atom, 1);
863 XUngrabServer (persistant_xdisplay);
864 XCloseDisplay (persistant_xdisplay);
867 /* There is a miniscule race condition here if the drag window
868 * gets destroyed exactly now.
870 if (display_x11->motif_drag_window)
872 display_x11->motif_drag_gdk_window =
873 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
874 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
875 motif_drag_window_filter,
880 return display_x11->motif_drag_window;
884 motif_read_target_table (GdkDisplay *display)
886 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
887 gulong bytes_after, nitems;
892 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
894 if (display_x11->motif_target_lists)
896 for (i=0; i<display_x11->motif_n_target_lists; i++)
897 g_list_free (display_x11->motif_target_lists[i]);
899 g_free (display_x11->motif_target_lists);
900 display_x11->motif_target_lists = NULL;
901 display_x11->motif_n_target_lists = 0;
904 if (motif_find_drag_window (display, FALSE))
907 MotifTargetTableHeader *header = NULL;
908 guchar *target_bytes = NULL;
910 gboolean success = FALSE;
912 gdk_error_trap_push ();
913 XGetWindowProperty (display_x11->xdisplay,
914 display_x11->motif_drag_window,
915 motif_drag_targets_atom,
916 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
917 motif_drag_targets_atom,
918 &type, &format, &nitems, &bytes_after,
921 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
924 header = (MotifTargetTableHeader *)data;
926 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
927 header->total_size = card32_to_host (header->total_size, header->byte_order);
929 gdk_error_trap_push ();
930 XGetWindowProperty (display_x11->xdisplay,
931 display_x11->motif_drag_window,
932 motif_drag_targets_atom,
933 (sizeof(MotifTargetTableHeader)+3)/4,
934 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
936 motif_drag_targets_atom, &type, &format, &nitems,
937 &bytes_after, &target_bytes);
939 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
940 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
943 display_x11->motif_n_target_lists = header->n_lists;
944 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
947 for (i=0; i<header->n_lists; i++)
952 if (p + sizeof(guint16) - target_bytes > nitems)
955 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
957 /* We need to make a copy of the targets, since it may
960 targets = g_new (guint32, n_targets);
961 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
963 p += sizeof(guint16) + n_targets * sizeof(guint32);
964 if (p - target_bytes > nitems)
967 for (j=0; j<n_targets; j++)
968 display_x11->motif_target_lists[i] =
969 g_list_prepend (display_x11->motif_target_lists[i],
970 GUINT_TO_POINTER (card32_to_host (targets[j],
971 header->byte_order)));
973 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
983 XFree (target_bytes);
987 if (display_x11->motif_target_lists)
989 g_free (display_x11->motif_target_lists);
990 display_x11->motif_target_lists = NULL;
991 display_x11->motif_n_target_lists = 0;
993 g_warning ("Error reading Motif target table\n");
999 targets_sort_func (gconstpointer a, gconstpointer b)
1001 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1002 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1005 /* Check if given (sorted) list is in the targets table */
1007 motif_target_table_check (GdkDisplay *display,
1010 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1011 GList *tmp_list1, *tmp_list2;
1014 for (i=0; i<display_x11->motif_n_target_lists; i++)
1016 tmp_list1 = display_x11->motif_target_lists[i];
1019 while (tmp_list1 && tmp_list2)
1021 if (tmp_list1->data != tmp_list2->data)
1024 tmp_list1 = tmp_list1->next;
1025 tmp_list2 = tmp_list2->next;
1027 if (!tmp_list1 && !tmp_list2) /* Found it */
1035 motif_add_to_target_table (GdkDisplay *display,
1036 GList *targets) /* targets is list of GdkAtom */
1038 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1039 GList *sorted = NULL;
1044 /* make a sorted copy of the list */
1048 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1049 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1050 targets = targets->next;
1053 /* First check if it is there already */
1055 if (display_x11->motif_target_lists)
1056 index = motif_target_table_check (display, sorted);
1058 /* We need to grab the server while doing this, to ensure
1064 /* We need to make sure that it exists _before_ we grab the
1065 * server, since we can't open a new connection after we
1068 motif_find_drag_window (display, TRUE);
1070 gdk_x11_display_grab (display);
1071 motif_read_target_table (display);
1073 /* Check again, in case it was added in the meantime */
1075 if (display_x11->motif_target_lists)
1076 index = motif_target_table_check (display, sorted);
1080 guint32 total_size = 0;
1084 MotifTargetTableHeader *header;
1086 if (!display_x11->motif_target_lists)
1088 display_x11->motif_target_lists = g_new (GList *, 1);
1089 display_x11->motif_n_target_lists = 1;
1093 display_x11->motif_n_target_lists++;
1094 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1095 sizeof(GList *) * display_x11->motif_n_target_lists);
1097 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1099 index = display_x11->motif_n_target_lists - 1;
1101 total_size = sizeof (MotifTargetTableHeader);
1102 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1103 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1105 data = g_malloc (total_size);
1107 header = (MotifTargetTableHeader *)data;
1108 p = data + sizeof(MotifTargetTableHeader);
1110 header->byte_order = local_byte_order;
1111 header->protocol_version = 0;
1112 header->n_lists = display_x11->motif_n_target_lists;
1113 header->total_size = total_size;
1115 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1117 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1118 guint32 *targets = g_new (guint32, n_targets);
1119 guint32 *p32 = targets;
1121 tmp_list = display_x11->motif_target_lists[i];
1124 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1126 tmp_list = tmp_list->next;
1131 p += sizeof(guint16);
1133 memcpy (p, targets, n_targets * sizeof(guint32));
1136 p += sizeof(guint32) * n_targets;
1140 XChangeProperty (display_x11->xdisplay,
1141 display_x11->motif_drag_window,
1142 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1143 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1147 gdk_x11_display_ungrab (display);
1150 g_list_free (sorted);
1154 /* Translate flags */
1157 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1159 guint recommended_op = flags & 0x000f;
1160 guint possible_ops = (flags & 0x0f0) >> 4;
1162 switch (recommended_op)
1165 context->suggested_action = GDK_ACTION_MOVE;
1168 context->suggested_action = GDK_ACTION_COPY;
1171 context->suggested_action = GDK_ACTION_LINK;
1174 context->suggested_action = GDK_ACTION_COPY;
1178 context->actions = 0;
1179 if (possible_ops & XmDROP_MOVE)
1180 context->actions |= GDK_ACTION_MOVE;
1181 if (possible_ops & XmDROP_COPY)
1182 context->actions |= GDK_ACTION_COPY;
1183 if (possible_ops & XmDROP_LINK)
1184 context->actions |= GDK_ACTION_LINK;
1188 motif_dnd_get_flags (GdkDragContext *context)
1192 switch (context->suggested_action)
1194 case GDK_ACTION_MOVE:
1195 flags = XmDROP_MOVE;
1197 case GDK_ACTION_COPY:
1198 flags = XmDROP_COPY;
1200 case GDK_ACTION_LINK:
1201 flags = XmDROP_LINK;
1204 flags = XmDROP_NOOP;
1208 if (context->actions & GDK_ACTION_MOVE)
1209 flags |= XmDROP_MOVE << 8;
1210 if (context->actions & GDK_ACTION_COPY)
1211 flags |= XmDROP_COPY << 8;
1212 if (context->actions & GDK_ACTION_LINK)
1213 flags |= XmDROP_LINK << 8;
1221 motif_set_targets (GdkDragContext *context)
1223 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1224 MotifDragInitiatorInfo info;
1226 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1228 info.byte_order = local_byte_order;
1229 info.protocol_version = 0;
1231 info.targets_index = motif_add_to_target_table (display, context->targets);
1236 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1238 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1239 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1243 info.selection_atom = private->motif_selection;
1245 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1246 GDK_DRAWABLE_XID (context->source_window),
1247 private->motif_selection,
1248 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1250 (guchar *)&info, sizeof (info));
1252 private->motif_targets_set = 1;
1256 motif_check_dest (GdkDisplay *display,
1259 gboolean retval = FALSE;
1261 MotifDragReceiverInfo *info;
1264 unsigned long nitems, after;
1265 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1267 gdk_error_trap_push ();
1268 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1269 motif_drag_receiver_info_atom,
1270 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1271 &type, &format, &nitems, &after,
1274 if (gdk_error_trap_pop() == 0)
1278 info = (MotifDragReceiverInfo *)data;
1280 if ((format == 8) && (nitems == sizeof(*info)))
1282 if ((info->protocol_version == 0) &&
1283 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1284 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1285 (info->protocol_style == XmDRAG_DYNAMIC)))
1291 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1298 return retval ? win : None;
1302 motif_send_enter (GdkDragContext *context,
1305 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1306 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1309 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1310 return; /* Motif Dnd requires getting properties on the root window */
1312 xev.xclient.type = ClientMessage;
1313 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1314 xev.xclient.format = 8;
1315 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1317 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1318 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1319 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1320 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1321 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1323 if (!private->motif_targets_set)
1324 motif_set_targets (context);
1326 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1327 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1329 if (!_gdk_send_xevent (display,
1330 GDK_DRAWABLE_XID (context->dest_window),
1333 g_message ("Send event to %lx failed",
1334 GDK_DRAWABLE_XID (context->dest_window)));
1338 motif_send_leave (GdkDragContext *context,
1341 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1344 xev.xclient.type = ClientMessage;
1345 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1346 xev.xclient.format = 8;
1347 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1349 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1350 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1351 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1352 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1353 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1354 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1355 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1357 if (!_gdk_send_xevent (display,
1358 GDK_DRAWABLE_XID (context->dest_window),
1361 g_message ("Send event to %lx failed",
1362 GDK_DRAWABLE_XID (context->dest_window)));
1366 motif_send_motion (GdkDragContext *context,
1369 GdkDragAction action,
1372 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1373 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1377 xev.xclient.type = ClientMessage;
1378 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1379 xev.xclient.format = 8;
1380 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1382 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1383 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1384 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1385 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1386 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1388 if ((context->suggested_action != private->old_action) ||
1389 (context->actions != private->old_actions))
1391 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1393 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1398 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1400 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1401 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1403 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1407 if (!_gdk_send_xevent (display,
1408 GDK_DRAWABLE_XID (context->dest_window),
1411 g_message ("Send event to %lx failed",
1412 GDK_DRAWABLE_XID (context->dest_window)));
1418 motif_send_drop (GdkDragContext *context, guint32 time)
1420 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1421 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1424 xev.xclient.type = ClientMessage;
1425 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1426 xev.xclient.format = 8;
1427 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1429 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1430 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1431 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1432 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1434 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1435 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1437 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1438 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1440 if (!_gdk_send_xevent (display,
1441 GDK_DRAWABLE_XID (context->dest_window),
1444 g_message ("Send event to %lx failed",
1445 GDK_DRAWABLE_XID (context->dest_window)));
1451 motif_read_initiator_info (GdkDisplay *display,
1452 Window source_window,
1463 MotifDragInitiatorInfo *initiator_info;
1465 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1467 gdk_error_trap_push ();
1468 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1469 0, sizeof(*initiator_info), FALSE,
1470 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1471 &type, &format, &nitems, &bytes_after,
1474 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1476 g_warning ("Error reading initiator info\n");
1480 initiator_info = (MotifDragInitiatorInfo *)data;
1482 motif_read_target_table (display);
1484 initiator_info->targets_index =
1485 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1486 initiator_info->selection_atom =
1487 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1489 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1491 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1492 XFree (initiator_info);
1496 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1501 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1502 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1503 tmp_list = tmp_list->prev;
1506 #ifdef G_ENABLE_DEBUG
1507 if (_gdk_debug_flags & GDK_DEBUG_DND)
1508 print_target_list (*targets);
1509 #endif /* G_ENABLE_DEBUG */
1511 *selection = initiator_info->selection_atom;
1513 XFree (initiator_info);
1518 static GdkDragContext *
1519 motif_drag_context_new (GdkWindow *dest_window,
1521 guint32 source_window,
1524 GdkDragContext *new_context;
1525 GdkDragContextPrivateX11 *private;
1526 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1527 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1529 /* FIXME, current_dest_drag really shouldn't be NULL'd
1530 * if we error below.
1532 if (display_x11->current_dest_drag != NULL)
1534 if (timestamp >= display_x11->current_dest_drag->start_time)
1536 g_object_unref (display_x11->current_dest_drag);
1537 display_x11->current_dest_drag = NULL;
1543 new_context = gdk_drag_context_new ();
1544 private = PRIVATE_DATA (new_context);
1546 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1547 new_context->is_source = FALSE;
1549 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1550 if (new_context->source_window)
1551 g_object_ref (new_context->source_window);
1554 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1555 if (!new_context->source_window)
1557 g_object_unref (new_context);
1562 new_context->dest_window = dest_window;
1563 g_object_ref (dest_window);
1564 new_context->start_time = timestamp;
1566 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1569 &new_context->targets,
1570 &private->motif_selection))
1572 g_object_unref (new_context);
1580 * The MOTIF drag protocol has no real provisions for distinguishing
1581 * multiple simultaneous drops. If the sources grab the pointer
1582 * when doing drags, that shouldn't happen, in any case. If it
1583 * does, we can't do much except hope for the best.
1586 static GdkFilterReturn
1587 motif_top_level_enter (GdkEvent *event,
1590 guint32 source_window,
1593 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1594 GdkDragContext *new_context;
1596 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1597 flags, timestamp, source_window, atom));
1599 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1601 return GDK_FILTER_REMOVE;
1603 event->dnd.type = GDK_DRAG_ENTER;
1604 event->dnd.context = new_context;
1605 g_object_ref (new_context);
1607 display_x11->current_dest_drag = new_context;
1609 return GDK_FILTER_TRANSLATE;
1612 static GdkFilterReturn
1613 motif_top_level_leave (GdkEvent *event,
1617 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1619 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1622 if ((display_x11->current_dest_drag != NULL) &&
1623 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1624 (timestamp >= display_x11->current_dest_drag->start_time))
1626 event->dnd.type = GDK_DRAG_LEAVE;
1627 /* Pass ownership of context to the event */
1628 event->dnd.context = display_x11->current_dest_drag;
1630 display_x11->current_dest_drag = NULL;
1632 return GDK_FILTER_TRANSLATE;
1635 return GDK_FILTER_REMOVE;
1638 static GdkFilterReturn
1639 motif_motion (GdkEvent *event,
1645 GdkDragContextPrivateX11 *private;
1646 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1648 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1649 flags, timestamp, x_root, y_root));
1651 if ((display_x11->current_dest_drag != NULL) &&
1652 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1653 (timestamp >= display_x11->current_dest_drag->start_time))
1655 private = PRIVATE_DATA (display_x11->current_dest_drag);
1657 event->dnd.type = GDK_DRAG_MOTION;
1658 event->dnd.context = display_x11->current_dest_drag;
1659 g_object_ref (display_x11->current_dest_drag);
1661 event->dnd.time = timestamp;
1663 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1665 event->dnd.x_root = x_root;
1666 event->dnd.y_root = y_root;
1668 private->last_x = x_root;
1669 private->last_y = y_root;
1671 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1673 return GDK_FILTER_TRANSLATE;
1676 return GDK_FILTER_REMOVE;
1679 static GdkFilterReturn
1680 motif_operation_changed (GdkEvent *event,
1684 GdkDragContextPrivateX11 *private;
1685 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1686 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1689 if ((display_x11->current_dest_drag != NULL) &&
1690 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1691 (timestamp >= display_x11->current_dest_drag->start_time))
1693 event->dnd.type = GDK_DRAG_MOTION;
1694 event->dnd.send_event = FALSE;
1695 event->dnd.context = display_x11->current_dest_drag;
1696 g_object_ref (display_x11->current_dest_drag);
1698 event->dnd.time = timestamp;
1699 private = PRIVATE_DATA (display_x11->current_dest_drag);
1701 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1703 event->dnd.x_root = private->last_x;
1704 event->dnd.y_root = private->last_y;
1706 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1708 return GDK_FILTER_TRANSLATE;
1711 return GDK_FILTER_REMOVE;
1714 static GdkFilterReturn
1715 motif_drop_start (GdkEvent *event,
1718 guint32 source_window,
1723 GdkDragContext *new_context;
1724 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1726 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1727 flags, timestamp, x_root, y_root, source_window, atom));
1729 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1731 return GDK_FILTER_REMOVE;
1733 motif_dnd_translate_flags (new_context, flags);
1735 event->dnd.type = GDK_DROP_START;
1736 event->dnd.context = new_context;
1737 event->dnd.time = timestamp;
1738 event->dnd.x_root = x_root;
1739 event->dnd.y_root = y_root;
1741 gdk_x11_window_set_user_time (event->any.window, timestamp);
1743 g_object_ref (new_context);
1744 display_x11->current_dest_drag = new_context;
1746 return GDK_FILTER_TRANSLATE;
1749 static GdkFilterReturn
1750 motif_drag_status (GdkEvent *event,
1754 GdkDragContext *context;
1755 GdkDisplay *display;
1758 g_message ("Motif status message: flags %x", flags));
1760 display = gdk_drawable_get_display (event->any.window);
1762 return GDK_FILTER_REMOVE;
1764 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1768 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1769 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1770 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1771 private->drag_status = GDK_DRAG_STATUS_DRAG;
1773 event->dnd.type = GDK_DRAG_STATUS;
1774 event->dnd.send_event = FALSE;
1775 event->dnd.context = context;
1776 g_object_ref (context);
1778 event->dnd.time = timestamp;
1780 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1782 switch (flags & 0x000f)
1785 context->action = 0;
1788 context->action = GDK_ACTION_MOVE;
1791 context->action = GDK_ACTION_COPY;
1794 context->action = GDK_ACTION_LINK;
1799 context->action = 0;
1801 return GDK_FILTER_TRANSLATE;
1803 return GDK_FILTER_REMOVE;
1806 static GdkFilterReturn
1807 motif_dnd_filter (GdkXEvent *xev,
1811 XEvent *xevent = (XEvent *)xev;
1816 guint32 source_window;
1818 gint16 x_root, y_root;
1821 if (!event->any.window ||
1822 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1823 return GDK_FILTER_CONTINUE; /* Not for us */
1825 /* First read some fields common to all Motif DND messages */
1827 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1828 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1829 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1831 is_reply = ((reason & 0x80) != 0);
1833 switch (reason & 0x7f)
1835 case XmTOP_LEVEL_ENTER:
1836 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1837 atom = MOTIF_UNPACK_LONG (xevent, 3);
1838 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1839 case XmTOP_LEVEL_LEAVE:
1840 return motif_top_level_leave (event, flags, timestamp);
1843 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1844 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1847 return motif_motion (event, flags, timestamp, x_root, y_root);
1849 return motif_drag_status (event, flags, timestamp);
1851 case XmDROP_SITE_ENTER:
1852 return motif_drag_status (event, flags, timestamp);
1854 case XmDROP_SITE_LEAVE:
1855 return motif_drag_status (event,
1856 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1859 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1860 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1861 atom = MOTIF_UNPACK_LONG (xevent, 3);
1862 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1865 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1868 case XmOPERATION_CHANGED:
1870 return motif_operation_changed (event, flags, timestamp);
1872 return motif_drag_status (event, flags, timestamp);
1875 /* To the best of my knowledge, these next two messages are
1876 * not part of the protocol, though they are defined in
1880 case XmDRAG_DROP_FINISH:
1884 return GDK_FILTER_REMOVE;
1887 /*************************************************************
1888 ***************************** XDND **************************
1889 *************************************************************/
1891 /* Utility functions */
1896 GdkDragAction action;
1897 } xdnd_actions_table[] = {
1898 { "XdndActionCopy", None, GDK_ACTION_COPY },
1899 { "XdndActionMove", None, GDK_ACTION_MOVE },
1900 { "XdndActionLink", None, GDK_ACTION_LINK },
1901 { "XdndActionAsk", None, GDK_ACTION_ASK },
1902 { "XdndActionPrivate", None, GDK_ACTION_COPY },
1905 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1906 static gboolean xdnd_actions_initialized = FALSE;
1909 xdnd_initialize_actions (void)
1913 xdnd_actions_initialized = TRUE;
1914 for (i=0; i < xdnd_n_actions; i++)
1915 xdnd_actions_table[i].atom = gdk_atom_intern_static_string (xdnd_actions_table[i].name);
1918 static GdkDragAction
1919 xdnd_action_from_atom (GdkDisplay *display,
1928 atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
1930 if (!xdnd_actions_initialized)
1931 xdnd_initialize_actions();
1933 for (i=0; i<xdnd_n_actions; i++)
1934 if (atom == xdnd_actions_table[i].atom)
1935 return xdnd_actions_table[i].action;
1941 xdnd_action_to_atom (GdkDisplay *display,
1942 GdkDragAction action)
1946 if (!xdnd_actions_initialized)
1947 xdnd_initialize_actions();
1949 for (i=0; i<xdnd_n_actions; i++)
1950 if (action == xdnd_actions_table[i].action)
1951 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
1958 static GdkFilterReturn
1959 xdnd_status_filter (GdkXEvent *xev,
1963 GdkDisplay *display;
1964 XEvent *xevent = (XEvent *)xev;
1965 guint32 dest_window = xevent->xclient.data.l[0];
1966 guint32 flags = xevent->xclient.data.l[1];
1967 Atom action = xevent->xclient.data.l[4];
1968 GdkDragContext *context;
1970 if (!event->any.window ||
1971 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1972 return GDK_FILTER_CONTINUE; /* Not for us */
1975 g_message ("XdndStatus: dest_window: %#x action: %ld",
1976 dest_window, action));
1978 display = gdk_drawable_get_display (event->any.window);
1979 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1983 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1984 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1985 private->drag_status = GDK_DRAG_STATUS_DRAG;
1987 event->dnd.send_event = FALSE;
1988 event->dnd.type = GDK_DRAG_STATUS;
1989 event->dnd.context = context;
1990 g_object_ref (context);
1992 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1993 if (!(action != 0) != !(flags & 1))
1996 g_warning ("Received status event with flags not corresponding to action!\n"));
2000 context->action = xdnd_action_from_atom (display, action);
2002 return GDK_FILTER_TRANSLATE;
2005 return GDK_FILTER_REMOVE;
2008 static GdkFilterReturn
2009 xdnd_finished_filter (GdkXEvent *xev,
2013 GdkDisplay *display;
2014 XEvent *xevent = (XEvent *)xev;
2015 guint32 dest_window = xevent->xclient.data.l[0];
2016 GdkDragContext *context;
2017 GdkDragContextPrivateX11 *private;
2019 if (!event->any.window ||
2020 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2021 return GDK_FILTER_CONTINUE; /* Not for us */
2024 g_message ("XdndFinished: dest_window: %#x", dest_window));
2026 display = gdk_drawable_get_display (event->any.window);
2027 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2031 private = PRIVATE_DATA (context);
2032 if (private->version == 5)
2033 private->drop_failed = xevent->xclient.data.l[1] == 0;
2035 event->dnd.type = GDK_DROP_FINISHED;
2036 event->dnd.context = context;
2037 g_object_ref (context);
2039 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
2041 return GDK_FILTER_TRANSLATE;
2044 return GDK_FILTER_REMOVE;
2048 xdnd_set_targets (GdkDragContext *context)
2050 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2052 GList *tmp_list = context->targets;
2054 gint n_atoms = g_list_length (context->targets);
2055 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2057 atomlist = g_new (Atom, n_atoms);
2061 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2062 tmp_list = tmp_list->next;
2066 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2067 GDK_DRAWABLE_XID (context->source_window),
2068 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2069 XA_ATOM, 32, PropModeReplace,
2070 (guchar *)atomlist, n_atoms);
2074 private->xdnd_targets_set = 1;
2078 xdnd_set_actions (GdkDragContext *context)
2080 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2085 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2087 if (!xdnd_actions_initialized)
2088 xdnd_initialize_actions();
2090 actions = context->actions;
2092 for (i=0; i<xdnd_n_actions; i++)
2094 if (actions & xdnd_actions_table[i].action)
2096 actions &= ~xdnd_actions_table[i].action;
2101 atomlist = g_new (Atom, n_atoms);
2103 actions = context->actions;
2105 for (i=0; i<xdnd_n_actions; i++)
2107 if (actions & xdnd_actions_table[i].action)
2109 actions &= ~xdnd_actions_table[i].action;
2110 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2115 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2116 GDK_DRAWABLE_XID (context->source_window),
2117 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2118 XA_ATOM, 32, PropModeReplace,
2119 (guchar *)atomlist, n_atoms);
2123 private->xdnd_actions_set = TRUE;
2124 private->xdnd_actions = context->actions;
2128 send_client_message_async_cb (Window window,
2132 GdkDragContext *context = data;
2134 g_message ("Got async callback for #%lx, success = %d",
2137 /* On failure, we immediately continue with the protocol
2138 * so we don't end up blocking for a timeout
2141 context->dest_window &&
2142 window == GDK_WINDOW_XID (context->dest_window))
2144 GdkEvent temp_event;
2145 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2147 g_object_unref (context->dest_window);
2148 context->dest_window = NULL;
2149 context->action = 0;
2151 private->drag_status = GDK_DRAG_STATUS_DRAG;
2153 temp_event.dnd.type = GDK_DRAG_STATUS;
2154 temp_event.dnd.window = context->source_window;
2155 temp_event.dnd.send_event = TRUE;
2156 temp_event.dnd.context = context;
2157 temp_event.dnd.time = GDK_CURRENT_TIME;
2159 gdk_event_put (&temp_event);
2162 g_object_unref (context);
2167 gdk_drag_context_get_display (GdkDragContext *context)
2169 if (context->source_window)
2170 return GDK_DRAWABLE_DISPLAY (context->source_window);
2171 else if (context->dest_window)
2172 return GDK_DRAWABLE_DISPLAY (context->dest_window);
2174 g_assert_not_reached ();
2179 send_client_message_async (GdkDragContext *context,
2183 XClientMessageEvent *event_send)
2185 GdkDisplay *display = gdk_drag_context_get_display (context);
2187 g_object_ref (context);
2189 _gdk_x11_send_client_message_async (display, window,
2190 propagate, event_mask, event_send,
2191 send_client_message_async_cb, context);
2195 xdnd_send_xevent (GdkDragContext *context,
2200 GdkDisplay *display = gdk_drag_context_get_display (context);
2204 g_assert (event_send->xany.type == ClientMessage);
2206 /* We short-circuit messages to ourselves */
2207 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2211 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2213 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2214 event_send->xclient.message_type)
2216 GdkEvent temp_event;
2217 temp_event.any.window = window;
2219 if ((*xdnd_filters[i].func) (event_send, &temp_event, NULL) == GDK_FILTER_TRANSLATE)
2221 gdk_event_put (&temp_event);
2222 g_object_unref (temp_event.dnd.context);
2230 xwindow = GDK_WINDOW_XWINDOW (window);
2232 if (_gdk_x11_display_is_root_window (display, xwindow))
2233 event_mask = ButtonPressMask;
2237 send_client_message_async (context, xwindow, propagate, event_mask,
2238 &event_send->xclient);
2244 xdnd_send_enter (GdkDragContext *context)
2247 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2248 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2250 xev.xclient.type = ClientMessage;
2251 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2252 xev.xclient.format = 32;
2253 xev.xclient.window = private->drop_xid ?
2255 GDK_DRAWABLE_XID (context->dest_window);
2256 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2257 xev.xclient.data.l[1] = (private->version << 24); /* version */
2258 xev.xclient.data.l[2] = 0;
2259 xev.xclient.data.l[3] = 0;
2260 xev.xclient.data.l[4] = 0;
2263 g_message ("Sending enter source window %#lx XDND protocol version %d\n",
2264 GDK_DRAWABLE_XID (context->source_window), private->version));
2265 if (g_list_length (context->targets) > 3)
2267 if (!private->xdnd_targets_set)
2268 xdnd_set_targets (context);
2269 xev.xclient.data.l[1] |= 1;
2273 GList *tmp_list = context->targets;
2278 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2279 GDK_POINTER_TO_ATOM (tmp_list->data));
2280 tmp_list = tmp_list->next;
2285 if (!xdnd_send_xevent (context, context->dest_window,
2289 g_message ("Send event to %lx failed",
2290 GDK_DRAWABLE_XID (context->dest_window)));
2291 g_object_unref (context->dest_window);
2292 context->dest_window = NULL;
2297 xdnd_send_leave (GdkDragContext *context)
2299 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2302 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2304 xev.xclient.type = ClientMessage;
2305 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2306 xev.xclient.format = 32;
2307 xev.xclient.window = private->drop_xid ?
2309 GDK_DRAWABLE_XID (context->dest_window);
2310 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2311 xev.xclient.data.l[1] = 0;
2312 xev.xclient.data.l[2] = 0;
2313 xev.xclient.data.l[3] = 0;
2314 xev.xclient.data.l[4] = 0;
2316 if (!xdnd_send_xevent (context, context->dest_window,
2320 g_message ("Send event to %lx failed",
2321 GDK_DRAWABLE_XID (context->dest_window)));
2322 g_object_unref (context->dest_window);
2323 context->dest_window = NULL;
2328 xdnd_send_drop (GdkDragContext *context, guint32 time)
2330 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2331 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2334 xev.xclient.type = ClientMessage;
2335 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2336 xev.xclient.format = 32;
2337 xev.xclient.window = private->drop_xid ?
2339 GDK_DRAWABLE_XID (context->dest_window);
2340 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2341 xev.xclient.data.l[1] = 0;
2342 xev.xclient.data.l[2] = time;
2343 xev.xclient.data.l[3] = 0;
2344 xev.xclient.data.l[4] = 0;
2346 if (!xdnd_send_xevent (context, context->dest_window,
2350 g_message ("Send event to %lx failed",
2351 GDK_DRAWABLE_XID (context->dest_window)));
2352 g_object_unref (context->dest_window);
2353 context->dest_window = NULL;
2358 xdnd_send_motion (GdkDragContext *context,
2361 GdkDragAction action,
2364 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2365 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2368 xev.xclient.type = ClientMessage;
2369 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2370 xev.xclient.format = 32;
2371 xev.xclient.window = private->drop_xid ?
2373 GDK_DRAWABLE_XID (context->dest_window);
2374 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2375 xev.xclient.data.l[1] = 0;
2376 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2377 xev.xclient.data.l[3] = time;
2378 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2380 if (!xdnd_send_xevent (context, context->dest_window,
2384 g_message ("Send event to %lx failed",
2385 GDK_DRAWABLE_XID (context->dest_window)));
2386 g_object_unref (context->dest_window);
2387 context->dest_window = NULL;
2389 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2393 xdnd_check_dest (GdkDisplay *display,
2395 guint *xdnd_version)
2397 gboolean retval = FALSE;
2400 unsigned long nitems, after;
2405 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2406 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2410 gdk_error_trap_push ();
2412 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2414 1, False, AnyPropertyType,
2415 &type, &format, &nitems, &after,
2420 proxy_data = (Window *)data;
2422 if ((format == 32) && (nitems == 1))
2424 proxy = *proxy_data;
2428 g_warning ("Invalid XdndProxy "
2429 "property on window %ld\n", win));
2434 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2436 1, False, AnyPropertyType,
2437 &type, &format, &nitems, &after,
2438 &data) == Success) &&
2441 version = (Atom *)data;
2443 if ((format == 32) && (nitems == 1))
2448 *xdnd_version = *version;
2452 g_warning ("Invalid XdndAware "
2453 "property on window %ld\n", win));
2459 gdk_error_trap_pop ();
2461 return retval ? (proxy ? proxy : win) : None;
2467 xdnd_read_actions (GdkDragContext *context)
2469 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2472 gulong nitems, after;
2478 PRIVATE_DATA (context)->xdnd_have_actions = FALSE;
2480 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2482 /* Get the XdndActionList, if set */
2484 gdk_error_trap_push ();
2486 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2487 GDK_DRAWABLE_XID (context->source_window),
2488 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2490 False, XA_ATOM, &type, &format, &nitems,
2491 &after, &data) == Success &&
2494 atoms = (Atom *)data;
2496 context->actions = 0;
2498 for (i=0; i<nitems; i++)
2499 context->actions |= xdnd_action_from_atom (display, atoms[i]);
2501 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2503 #ifdef G_ENABLE_DEBUG
2504 if (_gdk_debug_flags & GDK_DEBUG_DND)
2506 GString *action_str = g_string_new (NULL);
2507 if (context->actions & GDK_ACTION_MOVE)
2508 g_string_append(action_str, "MOVE ");
2509 if (context->actions & GDK_ACTION_COPY)
2510 g_string_append(action_str, "COPY ");
2511 if (context->actions & GDK_ACTION_LINK)
2512 g_string_append(action_str, "LINK ");
2513 if (context->actions & GDK_ACTION_ASK)
2514 g_string_append(action_str, "ASK ");
2516 g_message("Xdnd actions = %s", action_str->str);
2517 g_string_free (action_str, TRUE);
2519 #endif /* G_ENABLE_DEBUG */
2526 gdk_error_trap_pop ();
2532 GdkDragContext *source_context;
2534 source_context = gdk_drag_context_find (display, TRUE,
2535 GDK_DRAWABLE_XID (context->source_window),
2536 GDK_DRAWABLE_XID (context->dest_window));
2540 context->actions = source_context->actions;
2541 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2546 /* We have to make sure that the XdndActionList we keep internally
2547 * is up to date with the XdndActionList on the source window
2548 * because we get no notification, because Xdnd wasn't meant
2549 * to continually send actions. So we select on PropertyChangeMask
2550 * and add this filter.
2552 static GdkFilterReturn
2553 xdnd_source_window_filter (GdkXEvent *xev,
2557 XEvent *xevent = (XEvent *)xev;
2558 GdkDragContext *context = cb_data;
2559 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2561 if ((xevent->xany.type == PropertyNotify) &&
2562 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2564 xdnd_read_actions (context);
2566 return GDK_FILTER_REMOVE;
2569 return GDK_FILTER_CONTINUE;
2573 xdnd_manage_source_filter (GdkDragContext *context,
2575 gboolean add_filter)
2577 if (!GDK_WINDOW_DESTROYED (window) &&
2578 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2580 gdk_error_trap_push ();
2584 gdk_window_set_events (window,
2585 gdk_window_get_events (window) |
2586 GDK_PROPERTY_CHANGE_MASK);
2587 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2591 gdk_window_remove_filter (window,
2592 xdnd_source_window_filter,
2594 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2595 * but we might want it for other reasons. (Like
2596 * INCR selection transactions).
2600 gdk_display_sync (gdk_drawable_get_display (window));
2601 gdk_error_trap_pop ();
2606 base_precache_atoms (GdkDisplay *display)
2608 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2610 if (!display_x11->base_dnd_atoms_precached)
2612 static const char *const precache_atoms[] = {
2613 "ENLIGHTENMENT_DESKTOP",
2617 "_MOTIF_DRAG_RECEIVER_INFO"
2620 _gdk_x11_precache_atoms (display,
2621 precache_atoms, G_N_ELEMENTS (precache_atoms));
2623 display_x11->base_dnd_atoms_precached = TRUE;
2628 xdnd_precache_atoms (GdkDisplay *display)
2630 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2632 if (!display_x11->xdnd_atoms_precached)
2634 static const char *const precache_atoms[] = {
2640 "XdndActionPrivate",
2651 _gdk_x11_precache_atoms (display,
2652 precache_atoms, G_N_ELEMENTS (precache_atoms));
2654 display_x11->xdnd_atoms_precached = TRUE;
2658 static GdkFilterReturn
2659 xdnd_enter_filter (GdkXEvent *xev,
2663 GdkDisplay *display;
2664 GdkDisplayX11 *display_x11;
2665 XEvent *xevent = (XEvent *)xev;
2666 GdkDragContext *new_context;
2671 gulong nitems, after;
2675 guint32 source_window;
2679 if (!event->any.window ||
2680 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2681 return GDK_FILTER_CONTINUE; /* Not for us */
2683 source_window = xevent->xclient.data.l[0];
2684 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2685 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2687 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2688 display_x11 = GDK_DISPLAY_X11 (display);
2690 xdnd_precache_atoms (display);
2693 g_message ("XdndEnter: source_window: %#x, version: %#x",
2694 source_window, version));
2698 /* Old source ignore */
2699 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2700 return GDK_FILTER_REMOVE;
2703 if (display_x11->current_dest_drag != NULL)
2705 g_object_unref (display_x11->current_dest_drag);
2706 display_x11->current_dest_drag = NULL;
2709 new_context = gdk_drag_context_new ();
2710 new_context->protocol = GDK_DRAG_PROTO_XDND;
2711 PRIVATE_DATA(new_context)->version = version;
2713 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2714 if (new_context->source_window)
2715 g_object_ref (new_context->source_window);
2718 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2719 if (!new_context->source_window)
2721 g_object_unref (new_context);
2722 return GDK_FILTER_REMOVE;
2725 new_context->dest_window = event->any.window;
2726 g_object_ref (new_context->dest_window);
2728 new_context->targets = NULL;
2731 gdk_error_trap_push ();
2732 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2734 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2736 False, XA_ATOM, &type, &format, &nitems,
2739 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2741 g_object_unref (new_context);
2746 return GDK_FILTER_REMOVE;
2749 atoms = (Atom *)data;
2751 for (i=0; i<nitems; i++)
2752 new_context->targets =
2753 g_list_append (new_context->targets,
2754 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2762 if (xevent->xclient.data.l[2+i])
2763 new_context->targets =
2764 g_list_append (new_context->targets,
2765 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2766 xevent->xclient.data.l[2+i])));
2769 #ifdef G_ENABLE_DEBUG
2770 if (_gdk_debug_flags & GDK_DEBUG_DND)
2771 print_target_list (new_context->targets);
2772 #endif /* G_ENABLE_DEBUG */
2774 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2775 xdnd_read_actions (new_context);
2777 event->dnd.type = GDK_DRAG_ENTER;
2778 event->dnd.context = new_context;
2779 g_object_ref (new_context);
2781 display_x11->current_dest_drag = new_context;
2783 return GDK_FILTER_TRANSLATE;
2786 static GdkFilterReturn
2787 xdnd_leave_filter (GdkXEvent *xev,
2791 XEvent *xevent = (XEvent *)xev;
2792 guint32 source_window = xevent->xclient.data.l[0];
2793 GdkDisplay *display;
2794 GdkDisplayX11 *display_x11;
2796 if (!event->any.window ||
2797 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2798 return GDK_FILTER_CONTINUE; /* Not for us */
2801 g_message ("XdndLeave: source_window: %#x",
2804 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2805 display_x11 = GDK_DISPLAY_X11 (display);
2807 xdnd_precache_atoms (display);
2809 if ((display_x11->current_dest_drag != NULL) &&
2810 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2811 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2813 event->dnd.type = GDK_DRAG_LEAVE;
2814 /* Pass ownership of context to the event */
2815 event->dnd.context = display_x11->current_dest_drag;
2817 display_x11->current_dest_drag = NULL;
2819 return GDK_FILTER_TRANSLATE;
2822 return GDK_FILTER_REMOVE;
2825 static GdkFilterReturn
2826 xdnd_position_filter (GdkXEvent *xev,
2830 XEvent *xevent = (XEvent *)xev;
2831 guint32 source_window = xevent->xclient.data.l[0];
2832 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2833 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2834 guint32 time = xevent->xclient.data.l[3];
2835 Atom action = xevent->xclient.data.l[4];
2837 GdkDisplay *display;
2838 GdkDisplayX11 *display_x11;
2840 if (!event->any.window ||
2841 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2842 return GDK_FILTER_CONTINUE; /* Not for us */
2845 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2846 source_window, x_root, y_root, time, action));
2848 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2849 display_x11 = GDK_DISPLAY_X11 (display);
2851 xdnd_precache_atoms (display);
2853 if ((display_x11->current_dest_drag != NULL) &&
2854 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2855 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2857 event->dnd.type = GDK_DRAG_MOTION;
2858 event->dnd.context = display_x11->current_dest_drag;
2859 g_object_ref (display_x11->current_dest_drag);
2861 event->dnd.time = time;
2863 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2865 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2866 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2868 event->dnd.x_root = x_root;
2869 event->dnd.y_root = y_root;
2871 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2872 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
2874 return GDK_FILTER_TRANSLATE;
2877 return GDK_FILTER_REMOVE;
2880 static GdkFilterReturn
2881 xdnd_drop_filter (GdkXEvent *xev,
2885 XEvent *xevent = (XEvent *)xev;
2886 guint32 source_window = xevent->xclient.data.l[0];
2887 guint32 time = xevent->xclient.data.l[2];
2888 GdkDisplay *display;
2889 GdkDisplayX11 *display_x11;
2891 if (!event->any.window ||
2892 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2893 return GDK_FILTER_CONTINUE; /* Not for us */
2896 g_message ("XdndDrop: source_window: %#x time: %d",
2897 source_window, time));
2899 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2900 display_x11 = GDK_DISPLAY_X11 (display);
2902 xdnd_precache_atoms (display);
2904 if ((display_x11->current_dest_drag != NULL) &&
2905 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2906 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2908 GdkDragContextPrivateX11 *private;
2909 private = PRIVATE_DATA (display_x11->current_dest_drag);
2911 event->dnd.type = GDK_DROP_START;
2913 event->dnd.context = display_x11->current_dest_drag;
2914 g_object_ref (display_x11->current_dest_drag);
2916 event->dnd.time = time;
2917 event->dnd.x_root = private->last_x;
2918 event->dnd.y_root = private->last_y;
2920 gdk_x11_window_set_user_time (event->any.window, time);
2922 return GDK_FILTER_TRANSLATE;
2925 return GDK_FILTER_REMOVE;
2928 /*************************************************************
2929 ************************** Public API ***********************
2930 *************************************************************/
2932 _gdk_dnd_init (GdkDisplay *display)
2937 gdk_display_add_client_message_filter (
2939 gdk_atom_intern_static_string ("_MOTIF_DRAG_AND_DROP_MESSAGE"),
2940 motif_dnd_filter, NULL);
2942 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2944 gdk_display_add_client_message_filter (
2946 gdk_atom_intern_static_string (xdnd_filters[i].atom_name),
2947 xdnd_filters[i].func, NULL);
2954 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2956 if (context->dest_window)
2958 switch (context->protocol)
2960 case GDK_DRAG_PROTO_MOTIF:
2961 motif_send_leave (context, time);
2963 case GDK_DRAG_PROTO_XDND:
2964 xdnd_send_leave (context);
2966 case GDK_DRAG_PROTO_ROOTWIN:
2967 case GDK_DRAG_PROTO_NONE:
2972 g_object_unref (context->dest_window);
2973 context->dest_window = NULL;
2979 * @window: the source window for this drag.
2980 * @targets: the list of offered targets.
2982 * Starts a drag and creates a new drag context for it.
2984 * This function is called by the drag source.
2986 * Return value: a newly created #GdkDragContext.
2989 gdk_drag_begin (GdkWindow *window,
2992 GdkDragContext *new_context;
2994 g_return_val_if_fail (window != NULL, NULL);
2995 g_return_val_if_fail (GDK_WINDOW_IS_X11 (window), NULL);
2997 new_context = gdk_drag_context_new ();
2998 new_context->is_source = TRUE;
2999 new_context->source_window = window;
3000 g_object_ref (window);
3002 new_context->targets = g_list_copy (targets);
3003 precache_target_list (new_context);
3005 new_context->actions = 0;
3010 static GdkNativeWindow
3011 _gdk_drag_get_protocol_for_display (GdkDisplay *display,
3012 GdkNativeWindow xid,
3013 GdkDragProtocol *protocol,
3018 GdkNativeWindow retval;
3019 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
3021 base_precache_atoms (display);
3023 /* Check for a local drag
3025 window = gdk_window_lookup_for_display (display, xid);
3027 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
3029 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3031 *protocol = GDK_DRAG_PROTO_XDND;
3033 xdnd_precache_atoms (display);
3034 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3037 else if (_gdk_x11_display_is_root_window (display, (Window) xid))
3039 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3040 GDK_NOTE (DND, g_message ("Entering root window\n"));
3044 else if ((retval = xdnd_check_dest (display, xid, version)))
3046 *protocol = GDK_DRAG_PROTO_XDND;
3047 xdnd_precache_atoms (display);
3048 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3051 else if ((retval = motif_check_dest (display, xid)))
3053 *protocol = GDK_DRAG_PROTO_MOTIF;
3054 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3059 /* Check if this is a root window */
3061 gboolean rootwin = FALSE;
3064 unsigned long nitems, after;
3065 unsigned char *data;
3067 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3070 gdk_error_trap_push ();
3074 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3075 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
3076 0, 0, False, AnyPropertyType,
3077 &type, &format, &nitems, &after, &data) == Success &&
3085 /* Until I find out what window manager the next one is for,
3086 * I'm leaving it commented out. It's supported in the
3087 * xscreensaver sources, though.
3092 if (XGetWindowProperty (gdk_display, win,
3093 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
3094 0, 0, False, AnyPropertyType,
3095 &type, &format, &nitems, &data) &&
3104 gdk_error_trap_pop ();
3108 GDK_NOTE (DND, g_message ("Entering root window\n"));
3109 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3114 *protocol = GDK_DRAG_PROTO_NONE;
3116 return 0; /* a.k.a. None */
3120 * gdk_drag_get_protocol_for_display:
3121 * @display: the #GdkDisplay where the destination window resides
3122 * @xid: the windowing system id of the destination window.
3123 * @protocol: location where the supported DND protocol is returned.
3124 * @returns: the windowing system id of the window where the drop should happen. This
3125 * may be @xid or the id of a proxy window, or zero if @xid doesn't
3126 * support Drag and Drop.
3128 * Finds out the DND protocol supported by a window.
3133 gdk_drag_get_protocol_for_display (GdkDisplay *display,
3134 GdkNativeWindow xid,
3135 GdkDragProtocol *protocol)
3137 return _gdk_drag_get_protocol_for_display (display, xid, protocol, NULL);
3140 static GdkWindowCache *
3141 drag_context_find_window_cache (GdkDragContext *context,
3144 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3146 GdkWindowCache *cache;
3148 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
3150 cache = tmp_list->data;
3151 if (cache->screen == screen)
3155 cache = gdk_window_cache_new (screen);
3156 private->window_caches = g_slist_prepend (private->window_caches, cache);
3162 * gdk_drag_find_window_for_screen:
3163 * @context: a #GdkDragContext
3164 * @drag_window: a window which may be at the pointer position, but
3165 * should be ignored, since it is put up by the drag source as an icon.
3166 * @screen: the screen where the destination window is sought.
3167 * @x_root: the x position of the pointer in root coordinates.
3168 * @y_root: the y position of the pointer in root coordinates.
3169 * @dest_window: location to store the destination window in.
3170 * @protocol: location to store the DND protocol in.
3172 * Finds the destination window and DND protocol to use at the
3173 * given pointer position.
3175 * This function is called by the drag source to obtain the
3176 * @dest_window and @protocol parameters for gdk_drag_motion().
3181 gdk_drag_find_window_for_screen (GdkDragContext *context,
3182 GdkWindow *drag_window,
3186 GdkWindow **dest_window,
3187 GdkDragProtocol *protocol)
3189 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3190 GdkWindowCache *window_cache;
3191 GdkDisplay *display;
3194 g_return_if_fail (context != NULL);
3196 display = GDK_WINDOW_DISPLAY (context->source_window);
3198 window_cache = drag_context_find_window_cache (context, screen);
3200 dest = get_client_window_at_coords (window_cache,
3201 drag_window && GDK_WINDOW_IS_X11 (drag_window) ?
3202 GDK_DRAWABLE_XID (drag_window) : None,
3205 if (private->dest_xid != dest)
3208 private->dest_xid = dest;
3210 /* Check if new destination accepts drags, and which protocol */
3212 /* There is some ugliness here. We actually need to pass
3213 * _three_ pieces of information to drag_motion - dest_window,
3214 * protocol, and the XID of the unproxied window. The first
3215 * two are passed explicitely, the third implicitly through
3216 * protocol->dest_xid.
3218 if ((recipient = _gdk_drag_get_protocol_for_display (display, dest,
3219 protocol, &private->version)))
3221 *dest_window = gdk_window_lookup_for_display (display, recipient);
3223 g_object_ref (*dest_window);
3225 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3228 *dest_window = NULL;
3232 *dest_window = context->dest_window;
3234 g_object_ref (*dest_window);
3235 *protocol = context->protocol;
3241 * @context: a #GdkDragContext.
3242 * @dest_window: the new destination window, obtained by
3243 * gdk_drag_find_window().
3244 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3245 * @x_root: the x position of the pointer in root coordinates.
3246 * @y_root: the y position of the pointer in root coordinates.
3247 * @suggested_action: the suggested action.
3248 * @possible_actions: the possible actions.
3249 * @time_: the timestamp for this operation.
3251 * Updates the drag context when the pointer moves or the
3252 * set of actions changes.
3254 * This function is called by the drag source.
3256 * Return value: FIXME
3259 gdk_drag_motion (GdkDragContext *context,
3260 GdkWindow *dest_window,
3261 GdkDragProtocol protocol,
3264 GdkDragAction suggested_action,
3265 GdkDragAction possible_actions,
3268 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3270 g_return_val_if_fail (context != NULL, FALSE);
3271 g_return_val_if_fail (dest_window == NULL || GDK_WINDOW_IS_X11 (dest_window), FALSE);
3273 private->old_actions = context->actions;
3274 context->actions = possible_actions;
3276 if (private->old_actions != possible_actions)
3277 private->xdnd_actions_set = FALSE;
3279 if (protocol == GDK_DRAG_PROTO_XDND && private->version == 0)
3281 /* This ugly hack is necessary since GTK+ doesn't know about
3282 * the XDND protocol version, and in particular doesn't know
3283 * that gdk_drag_find_window_for_screen() has the side-effect
3284 * of setting private->version, and therefore sometimes call
3285 * gdk_drag_motion() without a prior call to
3286 * gdk_drag_find_window_for_screen(). This happens, e.g.
3287 * when GTK+ is proxying DND events to embedded windows.
3291 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3293 xdnd_check_dest (display,
3294 GDK_DRAWABLE_XID (dest_window),
3299 /* When we have a Xdnd target, make sure our XdndActionList
3300 * matches the current actions;
3302 if (protocol == GDK_DRAG_PROTO_XDND && !private->xdnd_actions_set)
3306 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3307 xdnd_set_actions (context);
3308 else if (context->dest_window == dest_window)
3310 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3311 GdkDragContext *dest_context;
3313 dest_context = gdk_drag_context_find (display, FALSE,
3314 GDK_DRAWABLE_XID (context->source_window),
3315 GDK_DRAWABLE_XID (dest_window));
3319 dest_context->actions = context->actions;
3320 PRIVATE_DATA (dest_context)->xdnd_have_actions = TRUE;
3326 if (context->dest_window != dest_window)
3328 GdkEvent temp_event;
3330 /* Send a leave to the last destination */
3331 gdk_drag_do_leave (context, time);
3332 private->drag_status = GDK_DRAG_STATUS_DRAG;
3334 /* Check if new destination accepts drags, and which protocol */
3338 context->dest_window = dest_window;
3339 private->drop_xid = private->dest_xid;
3340 g_object_ref (context->dest_window);
3341 context->protocol = protocol;
3345 case GDK_DRAG_PROTO_MOTIF:
3346 motif_send_enter (context, time);
3349 case GDK_DRAG_PROTO_XDND:
3350 xdnd_send_enter (context);
3353 case GDK_DRAG_PROTO_ROOTWIN:
3354 case GDK_DRAG_PROTO_NONE:
3358 private->old_action = suggested_action;
3359 context->suggested_action = suggested_action;
3360 private->old_actions = possible_actions;
3364 context->dest_window = NULL;
3365 private->drop_xid = None;
3366 context->action = 0;
3369 /* Push a status event, to let the client know that
3373 temp_event.dnd.type = GDK_DRAG_STATUS;
3374 temp_event.dnd.window = context->source_window;
3375 /* We use this to signal a synthetic status. Perhaps
3376 * we should use an extra field...
3378 temp_event.dnd.send_event = TRUE;
3380 temp_event.dnd.context = context;
3381 temp_event.dnd.time = time;
3383 gdk_event_put (&temp_event);
3387 private->old_action = context->suggested_action;
3388 context->suggested_action = suggested_action;
3391 /* Send a drag-motion event */
3393 private->last_x = x_root;
3394 private->last_y = y_root;
3396 if (context->dest_window)
3398 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3400 switch (context->protocol)
3402 case GDK_DRAG_PROTO_MOTIF:
3403 motif_send_motion (context, x_root, y_root, suggested_action, time);
3406 case GDK_DRAG_PROTO_XDND:
3407 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3410 case GDK_DRAG_PROTO_ROOTWIN:
3412 GdkEvent temp_event;
3413 /* GTK+ traditionally has used application/x-rootwin-drop,
3414 * but the XDND spec specifies x-rootwindow-drop.
3416 GdkAtom target1 = gdk_atom_intern_static_string ("application/x-rootwindow-drop");
3417 GdkAtom target2 = gdk_atom_intern_static_string ("application/x-rootwin-drop");
3419 if (g_list_find (context->targets,
3420 GDK_ATOM_TO_POINTER (target1)) ||
3421 g_list_find (context->targets,
3422 GDK_ATOM_TO_POINTER (target2)))
3423 context->action = context->suggested_action;
3425 context->action = 0;
3427 temp_event.dnd.type = GDK_DRAG_STATUS;
3428 temp_event.dnd.window = context->source_window;
3429 temp_event.dnd.send_event = FALSE;
3430 temp_event.dnd.context = context;
3431 temp_event.dnd.time = time;
3433 gdk_event_put (&temp_event);
3436 case GDK_DRAG_PROTO_NONE:
3437 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3452 * @context: a #GdkDragContext.
3453 * @time_: the timestamp for this operation.
3455 * Drops on the current destination.
3457 * This function is called by the drag source.
3460 gdk_drag_drop (GdkDragContext *context,
3463 g_return_if_fail (context != NULL);
3465 if (context->dest_window)
3467 switch (context->protocol)
3469 case GDK_DRAG_PROTO_MOTIF:
3470 motif_send_leave (context, time);
3471 motif_send_drop (context, time);
3474 case GDK_DRAG_PROTO_XDND:
3475 xdnd_send_drop (context, time);
3478 case GDK_DRAG_PROTO_ROOTWIN:
3479 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3481 case GDK_DRAG_PROTO_NONE:
3482 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3492 * @context: a #GdkDragContext.
3493 * @time_: the timestamp for this operation.
3495 * Aborts a drag without dropping.
3497 * This function is called by the drag source.
3500 gdk_drag_abort (GdkDragContext *context,
3503 g_return_if_fail (context != NULL);
3505 gdk_drag_do_leave (context, time);
3508 /* Destination side */
3512 * @context: a #GdkDragContext.
3513 * @action: the selected action which will be taken when a drop happens,
3514 * or 0 to indicate that a drop will not be accepted.
3515 * @time_: the timestamp for this operation.
3517 * Selects one of the actions offered by the drag source.
3519 * This function is called by the drag destination in response to
3520 * gdk_drag_motion() called by the drag source.
3523 gdk_drag_status (GdkDragContext *context,
3524 GdkDragAction action,
3527 GdkDragContextPrivateX11 *private;
3529 GdkDisplay *display;
3531 g_return_if_fail (context != NULL);
3533 private = PRIVATE_DATA (context);
3534 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3536 context->action = action;
3538 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3540 gboolean need_coords = FALSE;
3542 xev.xclient.type = ClientMessage;
3543 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3544 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3545 xev.xclient.format = 8;
3546 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3548 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3550 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3554 if ((action != 0) != (private->old_action != 0))
3558 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3562 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3566 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3571 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3575 case GDK_ACTION_MOVE:
3576 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3578 case GDK_ACTION_COPY:
3579 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3581 case GDK_ACTION_LINK:
3582 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3585 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3590 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3592 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3594 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3598 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3599 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3602 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3604 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3605 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3607 if (!_gdk_send_xevent (display,
3608 GDK_DRAWABLE_XID (context->source_window),
3611 g_message ("Send event to %lx failed",
3612 GDK_DRAWABLE_XID (context->source_window)));
3614 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3616 xev.xclient.type = ClientMessage;
3617 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3618 xev.xclient.format = 32;
3619 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3621 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3622 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3623 xev.xclient.data.l[2] = 0;
3624 xev.xclient.data.l[3] = 0;
3625 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3627 if (!xdnd_send_xevent (context, context->source_window,
3630 g_message ("Send event to %lx failed",
3631 GDK_DRAWABLE_XID (context->source_window)));
3634 private->old_action = action;
3639 * @context: a #GdkDragContext.
3640 * @ok: %TRUE if the drop is accepted.
3641 * @time_: the timestamp for this operation.
3643 * Accepts or rejects a drop.
3645 * This function is called by the drag destination in response
3646 * to a drop initiated by the drag source.
3649 gdk_drop_reply (GdkDragContext *context,
3653 GdkDragContextPrivateX11 *private;
3655 g_return_if_fail (context != NULL);
3657 private = PRIVATE_DATA (context);
3659 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3661 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3664 xev.xclient.type = ClientMessage;
3665 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3666 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3667 xev.xclient.format = 8;
3669 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3670 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3672 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3673 (XmDROP_SITE_VALID << 4) |
3674 (XmDROP_NOOP << 8) |
3677 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3678 (XmNO_DROP_SITE << 4) |
3679 (XmDROP_NOOP << 8) |
3680 (XmDROP_CANCEL << 12);
3681 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3682 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3683 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3684 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3685 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3687 _gdk_send_xevent (display,
3688 GDK_DRAWABLE_XID (context->source_window),
3695 * @context: a #GtkDragContext.
3696 * @success: %TRUE if the data was successfully received.
3697 * @time_: the timestamp for this operation.
3699 * Ends the drag operation after a drop.
3701 * This function is called by the drag destination.
3704 gdk_drop_finish (GdkDragContext *context,
3708 g_return_if_fail (context != NULL);
3710 if (context->protocol == GDK_DRAG_PROTO_XDND)
3712 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3715 xev.xclient.type = ClientMessage;
3716 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3717 xev.xclient.format = 32;
3718 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3720 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3723 xev.xclient.data.l[1] = 1;
3724 xev.xclient.data.l[2] = xdnd_action_to_atom (display,
3729 xev.xclient.data.l[1] = 0;
3730 xev.xclient.data.l[2] = None;
3732 xev.xclient.data.l[3] = 0;
3733 xev.xclient.data.l[4] = 0;
3735 if (!xdnd_send_xevent (context, context->source_window,
3738 g_message ("Send event to %lx failed",
3739 GDK_DRAWABLE_XID (context->source_window)));
3745 gdk_window_register_dnd (GdkWindow *window)
3747 static const gulong xdnd_version = 5;
3748 MotifDragReceiverInfo info;
3749 Atom motif_drag_receiver_info_atom;
3750 GdkDisplay *display = gdk_drawable_get_display (window);
3752 g_return_if_fail (window != NULL);
3754 base_precache_atoms (display);
3756 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3759 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3761 /* Set Motif drag receiver information property */
3763 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3764 "_MOTIF_DRAG_RECEIVER_INFO");
3765 /* initialize to zero to avoid writing uninitialized data to socket */
3766 memset(&info, 0, sizeof(info));
3767 info.byte_order = local_byte_order;
3768 info.protocol_version = 0;
3769 info.protocol_style = XmDRAG_DYNAMIC;
3770 info.proxy_window = None;
3771 info.num_drop_sites = 0;
3772 info.total_size = sizeof(info);
3774 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3775 motif_drag_receiver_info_atom,
3776 motif_drag_receiver_info_atom,
3783 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3784 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3785 GDK_DRAWABLE_XID (window),
3786 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3787 XA_ATOM, 32, PropModeReplace,
3788 (guchar *)&xdnd_version, 1);
3792 * gdk_drag_get_selection:
3793 * @context: a #GdkDragContext.
3795 * Returns the selection atom for the current source window.
3797 * Return value: the selection atom.
3800 gdk_drag_get_selection (GdkDragContext *context)
3802 g_return_val_if_fail (context != NULL, GDK_NONE);
3804 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3805 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3806 (PRIVATE_DATA (context))->motif_selection);
3807 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3808 return gdk_atom_intern_static_string ("XdndSelection");
3814 * gdk_drag_drop_succeeded:
3815 * @context: a #GdkDragContext
3817 * Returns whether the dropped data has been successfully
3818 * transferred. This function is intended to be used while
3819 * handling a %GDK_DROP_FINISHED event, its return value is
3820 * meaningless at other times.
3822 * Return value: %TRUE if the drop was successful.
3827 gdk_drag_drop_succeeded (GdkDragContext *context)
3829 GdkDragContextPrivateX11 *private;
3831 g_return_val_if_fail (context != NULL, FALSE);
3833 private = PRIVATE_DATA (context);
3835 return !private->drop_failed;
3838 #define __GDK_DND_X11_C__
3839 #include "gdkaliasdef.c"