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"
42 typedef struct _GdkDragContextPrivateX11 GdkDragContextPrivateX11;
46 GDK_DRAG_STATUS_MOTION_WAIT,
47 GDK_DRAG_STATUS_ACTION_WAIT,
53 gint x, y, width, height;
59 GHashTable *child_hash;
64 /* Structure that holds information about a drag in progress.
65 * this is used on both source and destination sides.
67 struct _GdkDragContextPrivateX11 {
68 GdkDragContext context;
73 guint16 last_x; /* Coordinates from last event */
75 GdkDragAction old_action; /* The last action we sent to the source */
76 GdkDragAction old_actions; /* The last actions we sent to the source */
77 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
79 Window dest_xid; /* The last window we looked up */
80 Window drop_xid; /* The (non-proxied) window that is receiving drops */
81 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
82 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
83 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
84 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
85 guint drag_status : 4; /* current status of drag */
87 GSList *window_caches;
90 #define PRIVATE_DATA(context) ((GdkDragContextPrivateX11 *) GDK_DRAG_CONTEXT (context)->windowing_data)
92 /* Forward declarations */
94 static void gdk_window_cache_destroy (GdkWindowCache *cache);
96 static void motif_read_target_table (GdkDisplay *display);
98 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
102 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
105 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
108 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
111 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
114 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
117 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
121 static void xdnd_manage_source_filter (GdkDragContext *context,
123 gboolean add_filter);
125 static void gdk_drag_context_init (GdkDragContext *dragcontext);
126 static void gdk_drag_context_class_init (GdkDragContextClass *klass);
127 static void gdk_drag_context_finalize (GObject *object);
129 static gpointer parent_class = NULL;
130 static GList *contexts;
132 const static struct {
133 const char *atom_name;
136 { "XdndEnter", xdnd_enter_filter },
137 { "XdndLeave", xdnd_leave_filter },
138 { "XdndPosition", xdnd_position_filter },
139 { "XdndStatus", xdnd_status_filter },
140 { "XdndFinished", xdnd_finished_filter },
141 { "XdndDrop", xdnd_drop_filter },
145 gdk_drag_context_get_type (void)
147 static GType object_type = 0;
151 static const GTypeInfo object_info =
153 sizeof (GdkDragContextClass),
154 (GBaseInitFunc) NULL,
155 (GBaseFinalizeFunc) NULL,
156 (GClassInitFunc) gdk_drag_context_class_init,
157 NULL, /* class_finalize */
158 NULL, /* class_data */
159 sizeof (GdkDragContext),
161 (GInstanceInitFunc) gdk_drag_context_init,
164 object_type = g_type_register_static (G_TYPE_OBJECT,
173 gdk_drag_context_init (GdkDragContext *dragcontext)
175 GdkDragContextPrivateX11 *private;
177 private = g_new0 (GdkDragContextPrivateX11, 1);
179 dragcontext->windowing_data = private;
181 contexts = g_list_prepend (contexts, dragcontext);
185 gdk_drag_context_class_init (GdkDragContextClass *klass)
187 GObjectClass *object_class = G_OBJECT_CLASS (klass);
189 parent_class = g_type_class_peek_parent (klass);
191 object_class->finalize = gdk_drag_context_finalize;
195 gdk_drag_context_finalize (GObject *object)
197 GdkDragContext *context = GDK_DRAG_CONTEXT (object);
198 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
201 g_list_free (context->targets);
203 if (context->source_window)
205 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
207 xdnd_manage_source_filter (context, context->source_window, FALSE);
209 g_object_unref (context->source_window);
212 if (context->dest_window)
213 g_object_unref (context->dest_window);
215 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
216 gdk_window_cache_destroy (tmp_list->data);
217 g_slist_free (private->window_caches);
219 contexts = g_list_remove (contexts, context);
223 G_OBJECT_CLASS (parent_class)->finalize (object);
229 * gdk_drag_context_new:
231 * Creates a new #GdkDragContext.
233 * Return value: the newly created #GdkDragContext.
236 gdk_drag_context_new (void)
238 return g_object_new (gdk_drag_context_get_type (), NULL);
242 * gdk_drag_context_ref:
243 * @context: a #GdkDragContext.
245 * Deprecated function; use g_object_ref() instead.
248 gdk_drag_context_ref (GdkDragContext *context)
250 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
252 g_object_ref (context);
256 * gdk_drag_context_unref:
257 * @context: a #GdkDragContext.
259 * Deprecated function; use g_object_unref() instead.
262 gdk_drag_context_unref (GdkDragContext *context)
264 g_return_if_fail (GDK_IS_DRAG_CONTEXT (context));
266 g_object_unref (context);
269 static GdkDragContext *
270 gdk_drag_context_find (GdkDisplay *display,
275 GList *tmp_list = contexts;
276 GdkDragContext *context;
277 GdkDragContextPrivateX11 *private;
278 Window context_dest_xid;
282 context = (GdkDragContext *)tmp_list->data;
283 private = PRIVATE_DATA (context);
285 if ((context->source_window && gdk_drawable_get_display (context->source_window) != display) ||
286 (context->dest_window && gdk_drawable_get_display (context->dest_window) != display))
289 context_dest_xid = context->dest_window ?
292 GDK_DRAWABLE_XID (context->dest_window)) :
295 if ((!context->is_source == !is_source) &&
296 ((source_xid == None) || (context->source_window &&
297 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
298 ((dest_xid == None) || (context_dest_xid == dest_xid)))
301 tmp_list = tmp_list->next;
308 precache_target_list (GdkDragContext *context)
310 if (context->targets)
312 GPtrArray *targets = g_ptr_array_new ();
316 for (tmp_list = context->targets; tmp_list; tmp_list = tmp_list->next)
317 g_ptr_array_add (targets, gdk_atom_name (GDK_POINTER_TO_ATOM (tmp_list->data)));
319 _gdk_x11_precache_atoms (GDK_WINDOW_DISPLAY (context->source_window),
320 (const gchar **)targets->pdata,
323 for (i =0; i < targets->len; i++)
324 g_free (targets->pdata[i]);
326 g_ptr_array_free (targets, TRUE);
330 /* Utility functions */
333 gdk_window_cache_add (GdkWindowCache *cache,
335 gint x, gint y, gint width, gint height,
338 GdkCacheChild *child = g_new (GdkCacheChild, 1);
343 child->width = width;
344 child->height = height;
345 child->mapped = mapped;
347 cache->children = g_list_prepend (cache->children, child);
348 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
352 static GdkFilterReturn
353 gdk_window_cache_filter (GdkXEvent *xev,
357 XEvent *xevent = (XEvent *)xev;
358 GdkWindowCache *cache = data;
360 switch (xevent->type)
362 case CirculateNotify:
364 case ConfigureNotify:
366 XConfigureEvent *xce = &xevent->xconfigure;
369 node = g_hash_table_lookup (cache->child_hash,
370 GUINT_TO_POINTER (xce->window));
373 GdkCacheChild *child = node->data;
376 child->width = xce->width;
377 child->height = xce->height;
378 if (xce->above == None && (node->next))
380 GList *last = g_list_last (cache->children);
381 cache->children = g_list_remove_link (cache->children, node);
388 GList *above_node = g_hash_table_lookup (cache->child_hash,
389 GUINT_TO_POINTER (xce->above));
390 if (above_node && node->next != above_node)
392 /* Put the window above (before in the list) above_node
394 cache->children = g_list_remove_link (cache->children, node);
395 node->prev = above_node->prev;
397 node->prev->next = node;
399 cache->children = node;
400 node->next = above_node;
401 above_node->prev = node;
409 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
411 if (!g_hash_table_lookup (cache->child_hash,
412 GUINT_TO_POINTER (xcwe->window)))
413 gdk_window_cache_add (cache, xcwe->window,
414 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
420 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
423 node = g_hash_table_lookup (cache->child_hash,
424 GUINT_TO_POINTER (xdwe->window));
427 g_hash_table_remove (cache->child_hash,
428 GUINT_TO_POINTER (xdwe->window));
429 cache->children = g_list_remove_link (cache->children, node);
431 g_list_free_1 (node);
437 XMapEvent *xme = &xevent->xmap;
440 node = g_hash_table_lookup (cache->child_hash,
441 GUINT_TO_POINTER (xme->window));
444 GdkCacheChild *child = node->data;
445 child->mapped = TRUE;
453 XMapEvent *xume = &xevent->xmap;
456 node = g_hash_table_lookup (cache->child_hash,
457 GUINT_TO_POINTER (xume->window));
460 GdkCacheChild *child = node->data;
461 child->mapped = FALSE;
466 return GDK_FILTER_CONTINUE;
468 return GDK_FILTER_REMOVE;
471 static GdkWindowCache *
472 gdk_window_cache_new (GdkScreen *screen)
474 XWindowAttributes xwa;
475 Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
476 GdkWindow *root_window = gdk_screen_get_root_window (screen);
477 GdkChildInfoX11 *children;
480 GdkWindowCache *result = g_new (GdkWindowCache, 1);
482 result->children = NULL;
483 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
484 result->screen = screen;
486 XGetWindowAttributes (xdisplay, GDK_WINDOW_XWINDOW (root_window), &xwa);
487 result->old_event_mask = xwa.your_event_mask;
488 XSelectInput (xdisplay, GDK_WINDOW_XWINDOW (root_window),
489 result->old_event_mask | SubstructureNotifyMask);
490 gdk_window_add_filter (root_window, gdk_window_cache_filter, result);
492 if (!_gdk_x11_get_window_child_info (gdk_screen_get_display (screen),
493 GDK_WINDOW_XWINDOW (root_window),
495 &children, &nchildren))
498 for (i = 0; i < nchildren ; i++)
500 gdk_window_cache_add (result, children[i].window,
501 children[i].x, children[i].y, children[i].width, children[i].height,
502 children[i].is_mapped);
511 gdk_window_cache_destroy (GdkWindowCache *cache)
513 GdkWindow *root_window = gdk_screen_get_root_window (cache->screen);
515 XSelectInput (GDK_WINDOW_XDISPLAY (root_window),
516 GDK_WINDOW_XWINDOW (root_window),
517 cache->old_event_mask);
518 gdk_window_remove_filter (root_window, gdk_window_cache_filter, cache);
520 g_list_foreach (cache->children, (GFunc)g_free, NULL);
521 g_list_free (cache->children);
522 g_hash_table_destroy (cache->child_hash);
528 get_client_window_at_coords_recurse (GdkDisplay *display,
530 gboolean is_toplevel,
534 GdkChildInfoX11 *children;
535 unsigned int nchildren;
537 gboolean found_child = FALSE;
538 GdkChildInfoX11 child;
539 gboolean has_wm_state = FALSE;
541 if (!_gdk_x11_get_window_child_info (display, win, TRUE,
542 is_toplevel? &has_wm_state : NULL,
543 &children, &nchildren))
549 for (i = nchildren - 1; (i >= 0) && !found_child; i--)
551 GdkChildInfoX11 *cur_child = &children[i];
553 if ((cur_child->is_mapped) && (cur_child->window_class == InputOutput) &&
554 (x >= cur_child->x) && (x < cur_child->x + cur_child->width) &&
555 (y >= cur_child->y) && (y < cur_child->y + cur_child->height))
568 if (child.has_wm_state)
571 return get_client_window_at_coords_recurse (display, child.window, FALSE, x, y);
578 get_client_window_at_coords (GdkWindowCache *cache,
584 Window retval = None;
586 gdk_error_trap_push ();
588 tmp_list = cache->children;
590 while (tmp_list && !retval)
592 GdkCacheChild *child = tmp_list->data;
594 if ((child->xid != ignore) && (child->mapped))
596 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
597 (y_root >= child->y) && (y_root < child->y + child->height))
599 retval = get_client_window_at_coords_recurse (gdk_screen_get_display (cache->screen),
608 tmp_list = tmp_list->next;
611 gdk_error_trap_pop ();
616 return GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (cache->screen));
619 /*************************************************************
620 ***************************** MOTIF *************************
621 *************************************************************/
623 /* values used in the message type for Motif DND */
636 /* Values used to specify type of protocol to use */
640 XmDRAG_PREFER_PREREGISTER,
642 XmDRAG_PREFER_DYNAMIC,
644 XmDRAG_PREFER_RECEIVER
647 /* Operation codes */
655 /* Drop site status */
657 XmNO_DROP_SITE = 0x01,
658 XmDROP_SITE_INVALID = 0x02,
659 XmDROP_SITE_VALID = 0x03
662 /* completion status */
670 /* Byte swapping routines. The motif specification leaves it
671 * up to us to save a few bytes in the client messages
673 static gchar local_byte_order = '\0';
675 #ifdef G_ENABLE_DEBUG
677 print_target_list (GList *targets)
681 gchar *name = gdk_atom_name (GDK_POINTER_TO_ATOM (targets->data));
682 g_message ("\t%s", name);
684 targets = targets->next;
687 #endif /* G_ENABLE_DEBUG */
690 init_byte_order (void)
692 guint32 myint = 0x01020304;
693 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
697 card16_to_host (guint16 x, gchar byte_order) {
698 if (byte_order == local_byte_order)
701 return (x << 8) | (x >> 8);
705 card32_to_host (guint32 x, gchar byte_order) {
706 if (byte_order == local_byte_order)
709 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
712 /* Motif packs together fields of varying length into the
713 * client message. We can't rely on accessing these
714 * through data.s[], data.l[], etc, because on some architectures
715 * (i.e., Alpha) these won't be valid for format == 8.
718 #define MOTIF_XCLIENT_BYTE(xevent,i) \
719 (xevent)->xclient.data.b[i]
720 #define MOTIF_XCLIENT_SHORT(xevent,i) \
721 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
722 #define MOTIF_XCLIENT_LONG(xevent,i) \
723 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
725 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
726 #define MOTIF_UNPACK_SHORT(xevent,i) \
727 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
728 #define MOTIF_UNPACK_LONG(xevent,i) \
729 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
731 /***** Dest side ***********/
733 /* Property placed on source windows */
734 typedef struct _MotifDragInitiatorInfo {
736 guint8 protocol_version;
737 guint16 targets_index;
738 guint32 selection_atom;
739 } MotifDragInitiatorInfo;
741 /* Header for target table on the drag window */
742 typedef struct _MotifTargetTableHeader {
744 guchar protocol_version;
747 } MotifTargetTableHeader;
749 /* Property placed on target windows */
750 typedef struct _MotifDragReceiverInfo {
752 guint8 protocol_version;
753 guint8 protocol_style;
755 guint32 proxy_window;
756 guint16 num_drop_sites;
759 } MotifDragReceiverInfo;
761 /* Target table handling */
763 static GdkFilterReturn
764 motif_drag_window_filter (GdkXEvent *xevent,
768 XEvent *xev = (XEvent *)xevent;
769 GdkDisplay *display = GDK_WINDOW_DISPLAY (event->any.window);
770 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
772 switch (xev->xany.type)
775 display_x11->motif_drag_window = None;
776 display_x11->motif_drag_gdk_window = NULL;
779 if (display_x11->motif_target_lists &&
780 (xev->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS")))
781 motif_read_target_table (display);
784 return GDK_FILTER_REMOVE;
788 motif_lookup_drag_window (GdkDisplay *display,
789 Display *lookup_xdisplay)
791 Window retval = None;
792 gulong bytes_after, nitems;
797 XGetWindowProperty (lookup_xdisplay, RootWindow (lookup_xdisplay, 0),
798 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW"),
800 XA_WINDOW, &type, &format, &nitems, &bytes_after,
803 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
805 retval = *(Window *)data;
807 g_message ("Found drag window %#lx\n", GDK_DISPLAY_X11 (display)->motif_drag_window));
816 /* Finds the window where global Motif drag information is stored.
817 * If it doesn't exist and 'create' is TRUE, create one.
820 motif_find_drag_window (GdkDisplay *display,
823 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
825 if (!display_x11->motif_drag_window)
827 Atom motif_drag_window_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_WINDOW");
828 display_x11->motif_drag_window = motif_lookup_drag_window (display, display_x11->xdisplay);
830 if (!display_x11->motif_drag_window && create)
832 /* Create a persistant window. (Copied from LessTif) */
834 Display *persistant_xdisplay;
835 XSetWindowAttributes attr;
836 persistant_xdisplay = XOpenDisplay (gdk_display_get_name (display));
837 XSetCloseDownMode (persistant_xdisplay, RetainPermanent);
839 XGrabServer (persistant_xdisplay);
841 display_x11->motif_drag_window = motif_lookup_drag_window (display, persistant_xdisplay);
843 if (!display_x11->motif_drag_window)
845 attr.override_redirect = True;
846 attr.event_mask = PropertyChangeMask;
848 display_x11->motif_drag_window =
849 XCreateWindow (persistant_xdisplay,
850 RootWindow (persistant_xdisplay, 0),
851 -100, -100, 10, 10, 0, 0,
852 InputOnly, CopyFromParent,
853 (CWOverrideRedirect | CWEventMask), &attr);
856 g_message ("Created drag window %#lx\n", display_x11->motif_drag_window));
858 XChangeProperty (persistant_xdisplay,
859 RootWindow (persistant_xdisplay, 0),
860 motif_drag_window_atom, XA_WINDOW,
862 (guchar *)&motif_drag_window_atom, 1);
865 XUngrabServer (persistant_xdisplay);
866 XCloseDisplay (persistant_xdisplay);
869 /* There is a miniscule race condition here if the drag window
870 * gets destroyed exactly now.
872 if (display_x11->motif_drag_window)
874 display_x11->motif_drag_gdk_window =
875 gdk_window_foreign_new_for_display (display, display_x11->motif_drag_window);
876 gdk_window_add_filter (display_x11->motif_drag_gdk_window,
877 motif_drag_window_filter,
882 return display_x11->motif_drag_window;
886 motif_read_target_table (GdkDisplay *display)
888 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
889 gulong bytes_after, nitems;
894 Atom motif_drag_targets_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS");
896 if (display_x11->motif_target_lists)
898 for (i=0; i<display_x11->motif_n_target_lists; i++)
899 g_list_free (display_x11->motif_target_lists[i]);
901 g_free (display_x11->motif_target_lists);
902 display_x11->motif_target_lists = NULL;
903 display_x11->motif_n_target_lists = 0;
906 if (motif_find_drag_window (display, FALSE))
908 MotifTargetTableHeader *header = NULL;
909 guchar *target_bytes = NULL;
911 gboolean success = FALSE;
913 gdk_error_trap_push ();
914 XGetWindowProperty (display_x11->xdisplay,
915 display_x11->motif_drag_window,
916 motif_drag_targets_atom,
917 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
918 motif_drag_targets_atom,
919 &type, &format, &nitems, &bytes_after,
922 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
925 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
926 header->total_size = card32_to_host (header->total_size, header->byte_order);
928 gdk_error_trap_push ();
929 XGetWindowProperty (display_x11->xdisplay,
930 display_x11->motif_drag_window,
931 motif_drag_targets_atom,
932 (sizeof(MotifTargetTableHeader)+3)/4,
933 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
935 motif_drag_targets_atom, &type, &format, &nitems,
936 &bytes_after, &target_bytes);
938 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
939 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
942 display_x11->motif_n_target_lists = header->n_lists;
943 display_x11->motif_target_lists = g_new0 (GList *, display_x11->motif_n_target_lists);
946 for (i=0; i<header->n_lists; i++)
951 if (p + sizeof(guint16) - target_bytes > nitems)
954 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
956 /* We need to make a copy of the targets, since it may
959 targets = g_new (guint32, n_targets);
960 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
962 p += sizeof(guint16) + n_targets * sizeof(guint32);
963 if (p - target_bytes > nitems)
966 for (j=0; j<n_targets; j++)
967 display_x11->motif_target_lists[i] =
968 g_list_prepend (display_x11->motif_target_lists[i],
969 GUINT_TO_POINTER (card32_to_host (targets[j],
970 header->byte_order)));
972 display_x11->motif_target_lists[i] = g_list_reverse (display_x11->motif_target_lists[i]);
982 XFree (target_bytes);
986 if (display_x11->motif_target_lists)
988 g_free (display_x11->motif_target_lists);
989 display_x11->motif_target_lists = NULL;
990 display_x11->motif_n_target_lists = 0;
992 g_warning ("Error reading Motif target table\n");
998 targets_sort_func (gconstpointer a, gconstpointer b)
1000 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1001 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1004 /* Check if given (sorted) list is in the targets table */
1006 motif_target_table_check (GdkDisplay *display,
1009 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1010 GList *tmp_list1, *tmp_list2;
1013 for (i=0; i<display_x11->motif_n_target_lists; i++)
1015 tmp_list1 = display_x11->motif_target_lists[i];
1018 while (tmp_list1 && tmp_list2)
1020 if (tmp_list1->data != tmp_list2->data)
1023 tmp_list1 = tmp_list1->next;
1024 tmp_list2 = tmp_list2->next;
1026 if (!tmp_list1 && !tmp_list2) /* Found it */
1034 motif_add_to_target_table (GdkDisplay *display,
1035 GList *targets) /* targets is list of GdkAtom */
1037 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1038 GList *sorted = NULL;
1043 /* make a sorted copy of the list */
1047 Atom xatom = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (targets->data));
1048 sorted = g_list_insert_sorted (sorted, GUINT_TO_POINTER (xatom), targets_sort_func);
1049 targets = targets->next;
1052 /* First check if it is there already */
1054 if (display_x11->motif_target_lists)
1055 index = motif_target_table_check (display, sorted);
1057 /* We need to grab the server while doing this, to ensure
1063 /* We need to make sure that it exists _before_ we grab the
1064 * server, since we can't open a new connection after we
1067 motif_find_drag_window (display, TRUE);
1069 gdk_x11_display_grab (display);
1070 motif_read_target_table (display);
1072 /* Check again, in case it was added in the meantime */
1074 if (display_x11->motif_target_lists)
1075 index = motif_target_table_check (display, sorted);
1079 guint32 total_size = 0;
1083 MotifTargetTableHeader *header;
1085 if (!display_x11->motif_target_lists)
1087 display_x11->motif_target_lists = g_new (GList *, 1);
1088 display_x11->motif_n_target_lists = 1;
1092 display_x11->motif_n_target_lists++;
1093 display_x11->motif_target_lists = g_realloc (display_x11->motif_target_lists,
1094 sizeof(GList *) * display_x11->motif_n_target_lists);
1096 display_x11->motif_target_lists[display_x11->motif_n_target_lists - 1] = sorted;
1098 index = display_x11->motif_n_target_lists - 1;
1100 total_size = sizeof (MotifTargetTableHeader);
1101 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1102 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (display_x11->motif_target_lists[i]);
1104 data = g_malloc (total_size);
1106 header = (MotifTargetTableHeader *)data;
1107 p = data + sizeof(MotifTargetTableHeader);
1109 header->byte_order = local_byte_order;
1110 header->protocol_version = 0;
1111 header->n_lists = display_x11->motif_n_target_lists;
1112 header->total_size = total_size;
1114 for (i = 0; i < display_x11->motif_n_target_lists ; i++)
1116 guint16 n_targets = g_list_length (display_x11->motif_target_lists[i]);
1117 guint32 *targets = g_new (guint32, n_targets);
1118 guint32 *p32 = targets;
1120 tmp_list = display_x11->motif_target_lists[i];
1123 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1125 tmp_list = tmp_list->next;
1130 p += sizeof(guint16);
1132 memcpy (p, targets, n_targets * sizeof(guint32));
1135 p += sizeof(guint32) * n_targets;
1139 XChangeProperty (display_x11->xdisplay,
1140 display_x11->motif_drag_window,
1141 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1142 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_TARGETS"),
1146 gdk_x11_display_ungrab (display);
1149 g_list_free (sorted);
1153 /* Translate flags */
1156 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1158 guint recommended_op = flags & 0x000f;
1159 guint possible_ops = (flags & 0x0f0) >> 4;
1161 switch (recommended_op)
1164 context->suggested_action = GDK_ACTION_MOVE;
1167 context->suggested_action = GDK_ACTION_COPY;
1170 context->suggested_action = GDK_ACTION_LINK;
1173 context->suggested_action = GDK_ACTION_COPY;
1177 context->actions = 0;
1178 if (possible_ops & XmDROP_MOVE)
1179 context->actions |= GDK_ACTION_MOVE;
1180 if (possible_ops & XmDROP_COPY)
1181 context->actions |= GDK_ACTION_COPY;
1182 if (possible_ops & XmDROP_LINK)
1183 context->actions |= GDK_ACTION_LINK;
1187 motif_dnd_get_flags (GdkDragContext *context)
1191 switch (context->suggested_action)
1193 case GDK_ACTION_MOVE:
1194 flags = XmDROP_MOVE;
1196 case GDK_ACTION_COPY:
1197 flags = XmDROP_COPY;
1199 case GDK_ACTION_LINK:
1200 flags = XmDROP_LINK;
1203 flags = XmDROP_NOOP;
1207 if (context->actions & GDK_ACTION_MOVE)
1208 flags |= XmDROP_MOVE << 8;
1209 if (context->actions & GDK_ACTION_COPY)
1210 flags |= XmDROP_COPY << 8;
1211 if (context->actions & GDK_ACTION_LINK)
1212 flags |= XmDROP_LINK << 8;
1220 motif_set_targets (GdkDragContext *context)
1222 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1223 MotifDragInitiatorInfo info;
1225 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1227 info.byte_order = local_byte_order;
1228 info.protocol_version = 0;
1230 info.targets_index = motif_add_to_target_table (display, context->targets);
1235 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1237 private->motif_selection = gdk_x11_get_xatom_by_name_for_display (display, buf);
1238 if (!XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), private->motif_selection))
1242 info.selection_atom = private->motif_selection;
1244 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1245 GDK_DRAWABLE_XID (context->source_window),
1246 private->motif_selection,
1247 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1249 (guchar *)&info, sizeof (info));
1251 private->motif_targets_set = 1;
1255 motif_check_dest (GdkDisplay *display,
1258 gboolean retval = FALSE;
1259 MotifDragReceiverInfo *info;
1262 unsigned long nitems, after;
1263 Atom motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO");
1265 gdk_error_trap_push ();
1266 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
1267 motif_drag_receiver_info_atom,
1268 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1269 &type, &format, &nitems, &after,
1272 if (gdk_error_trap_pop() == 0)
1276 if ((format == 8) && (nitems == sizeof(*info)))
1278 if ((info->protocol_version == 0) &&
1279 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1280 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1281 (info->protocol_style == XmDRAG_DYNAMIC)))
1287 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1294 return retval ? win : None;
1298 motif_send_enter (GdkDragContext *context,
1301 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1302 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1305 xev.xclient.type = ClientMessage;
1306 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1307 xev.xclient.format = 8;
1308 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1310 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1311 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1312 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1313 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1314 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1316 if (!private->motif_targets_set)
1317 motif_set_targets (context);
1319 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1320 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1322 if (!_gdk_send_xevent (display,
1323 GDK_DRAWABLE_XID (context->dest_window),
1326 g_message ("Send event to %lx failed",
1327 GDK_DRAWABLE_XID (context->dest_window)));
1331 motif_send_leave (GdkDragContext *context,
1334 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1337 xev.xclient.type = ClientMessage;
1338 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1339 xev.xclient.format = 8;
1340 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1342 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1343 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1344 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1345 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1346 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
1347 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1348 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1350 if (!_gdk_send_xevent (display,
1351 GDK_DRAWABLE_XID (context->dest_window),
1354 g_message ("Send event to %lx failed",
1355 GDK_DRAWABLE_XID (context->dest_window)));
1359 motif_send_motion (GdkDragContext *context,
1362 GdkDragAction action,
1365 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1366 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1370 xev.xclient.type = ClientMessage;
1371 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1372 xev.xclient.format = 8;
1373 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1375 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1376 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1377 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1378 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1379 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
1381 if ((context->suggested_action != private->old_action) ||
1382 (context->actions != private->old_actions))
1384 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1386 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1391 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1393 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1394 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1396 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1400 if (!_gdk_send_xevent (display,
1401 GDK_DRAWABLE_XID (context->dest_window),
1404 g_message ("Send event to %lx failed",
1405 GDK_DRAWABLE_XID (context->dest_window)));
1411 motif_send_drop (GdkDragContext *context, guint32 time)
1413 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1414 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
1417 xev.xclient.type = ClientMessage;
1418 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_AND_DROP_MESSAGE");
1419 xev.xclient.format = 8;
1420 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1422 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1423 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1424 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1425 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1427 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1428 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1430 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1431 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1433 if (!_gdk_send_xevent (display,
1434 GDK_DRAWABLE_XID (context->dest_window),
1437 g_message ("Send event to %lx failed",
1438 GDK_DRAWABLE_XID (context->dest_window)));
1444 motif_read_initiator_info (GdkDisplay *display,
1445 Window source_window,
1455 MotifDragInitiatorInfo *initiator_info;
1457 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1459 gdk_error_trap_push ();
1460 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), source_window, atom,
1461 0, sizeof(*initiator_info), FALSE,
1462 gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_INITIATOR_INFO"),
1463 &type, &format, &nitems, &bytes_after,
1464 (guchar **)&initiator_info);
1466 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1468 g_warning ("Error reading initiator info\n");
1472 motif_read_target_table (display);
1474 initiator_info->targets_index =
1475 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1476 initiator_info->selection_atom =
1477 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1479 if (initiator_info->targets_index >= display_x11->motif_n_target_lists)
1481 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1482 XFree (initiator_info);
1486 tmp_list = g_list_last (display_x11->motif_target_lists[initiator_info->targets_index]);
1491 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, GPOINTER_TO_UINT (tmp_list->data));
1492 *targets = g_list_prepend (*targets, GDK_ATOM_TO_POINTER (atom));
1493 tmp_list = tmp_list->prev;
1496 #ifdef G_ENABLE_DEBUG
1497 if (_gdk_debug_flags & GDK_DEBUG_DND)
1498 print_target_list (*targets);
1499 #endif /* G_ENABLE_DEBUG */
1501 *selection = initiator_info->selection_atom;
1503 XFree (initiator_info);
1508 static GdkDragContext *
1509 motif_drag_context_new (GdkWindow *dest_window,
1511 guint32 source_window,
1514 GdkDragContext *new_context;
1515 GdkDragContextPrivateX11 *private;
1516 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (dest_window);
1517 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1519 /* FIXME, current_dest_drag really shouldn't be NULL'd
1520 * if we error below.
1522 if (display_x11->current_dest_drag != NULL)
1524 if (timestamp >= display_x11->current_dest_drag->start_time)
1526 g_object_unref (display_x11->current_dest_drag);
1527 display_x11->current_dest_drag = NULL;
1533 new_context = gdk_drag_context_new ();
1534 private = PRIVATE_DATA (new_context);
1536 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1537 new_context->is_source = FALSE;
1539 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
1540 if (new_context->source_window)
1541 g_object_ref (new_context->source_window);
1544 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
1545 if (!new_context->source_window)
1547 g_object_unref (new_context);
1552 new_context->dest_window = dest_window;
1553 g_object_ref (dest_window);
1554 new_context->start_time = timestamp;
1556 if (!motif_read_initiator_info (GDK_WINDOW_DISPLAY (dest_window),
1559 &new_context->targets,
1560 &private->motif_selection))
1562 g_object_unref (new_context);
1570 * The MOTIF drag protocol has no real provisions for distinguishing
1571 * multiple simultaneous drops. If the sources grab the pointer
1572 * when doing drags, that shouldn't happen, in any case. If it
1573 * does, we can't do much except hope for the best.
1576 static GdkFilterReturn
1577 motif_top_level_enter (GdkEvent *event,
1580 guint32 source_window,
1583 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1584 GdkDragContext *new_context;
1586 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1587 flags, timestamp, source_window, atom));
1589 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1591 return GDK_FILTER_REMOVE;
1593 event->dnd.type = GDK_DRAG_ENTER;
1594 event->dnd.context = new_context;
1595 g_object_ref (new_context);
1597 display_x11->current_dest_drag = new_context;
1599 return GDK_FILTER_TRANSLATE;
1602 static GdkFilterReturn
1603 motif_top_level_leave (GdkEvent *event,
1607 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1609 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1612 if ((display_x11->current_dest_drag != NULL) &&
1613 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1614 (timestamp >= display_x11->current_dest_drag->start_time))
1616 event->dnd.type = GDK_DRAG_LEAVE;
1617 /* Pass ownership of context to the event */
1618 event->dnd.context = display_x11->current_dest_drag;
1620 display_x11->current_dest_drag = NULL;
1622 return GDK_FILTER_TRANSLATE;
1625 return GDK_FILTER_REMOVE;
1628 static GdkFilterReturn
1629 motif_motion (GdkEvent *event,
1635 GdkDragContextPrivateX11 *private;
1636 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1638 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1639 flags, timestamp, x_root, y_root));
1641 if ((display_x11->current_dest_drag != NULL) &&
1642 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1643 (timestamp >= display_x11->current_dest_drag->start_time))
1645 private = PRIVATE_DATA (display_x11->current_dest_drag);
1647 event->dnd.type = GDK_DRAG_MOTION;
1648 event->dnd.context = display_x11->current_dest_drag;
1649 g_object_ref (display_x11->current_dest_drag);
1651 event->dnd.time = timestamp;
1653 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1655 event->dnd.x_root = x_root;
1656 event->dnd.y_root = y_root;
1658 private->last_x = x_root;
1659 private->last_y = y_root;
1661 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1663 return GDK_FILTER_TRANSLATE;
1666 return GDK_FILTER_REMOVE;
1669 static GdkFilterReturn
1670 motif_operation_changed (GdkEvent *event,
1674 GdkDragContextPrivateX11 *private;
1675 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1676 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1679 if ((display_x11->current_dest_drag != NULL) &&
1680 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1681 (timestamp >= display_x11->current_dest_drag->start_time))
1683 event->dnd.type = GDK_DRAG_MOTION;
1684 event->dnd.send_event = FALSE;
1685 event->dnd.context = display_x11->current_dest_drag;
1686 g_object_ref (display_x11->current_dest_drag);
1688 event->dnd.time = timestamp;
1689 private = PRIVATE_DATA (display_x11->current_dest_drag);
1691 motif_dnd_translate_flags (display_x11->current_dest_drag, flags);
1693 event->dnd.x_root = private->last_x;
1694 event->dnd.y_root = private->last_y;
1696 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1698 return GDK_FILTER_TRANSLATE;
1701 return GDK_FILTER_REMOVE;
1704 static GdkFilterReturn
1705 motif_drop_start (GdkEvent *event,
1708 guint32 source_window,
1713 GdkDragContext *new_context;
1714 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_DRAWABLE_DISPLAY (event->any.window));
1716 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1717 flags, timestamp, x_root, y_root, source_window, atom));
1719 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1721 return GDK_FILTER_REMOVE;
1723 motif_dnd_translate_flags (new_context, flags);
1725 event->dnd.type = GDK_DROP_START;
1726 event->dnd.context = new_context;
1727 event->dnd.time = timestamp;
1728 event->dnd.x_root = x_root;
1729 event->dnd.y_root = y_root;
1731 g_object_ref (new_context);
1732 display_x11->current_dest_drag = new_context;
1734 return GDK_FILTER_TRANSLATE;
1737 static GdkFilterReturn
1738 motif_drag_status (GdkEvent *event,
1742 GdkDragContext *context;
1743 GdkDisplay *display;
1746 g_message ("Motif status message: flags %x", flags));
1748 display = gdk_drawable_get_display (event->any.window);
1750 return GDK_FILTER_REMOVE;
1752 context = gdk_drag_context_find (display, TRUE, GDK_DRAWABLE_XID (event->any.window), None);
1756 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1757 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1758 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1759 private->drag_status = GDK_DRAG_STATUS_DRAG;
1761 event->dnd.type = GDK_DRAG_STATUS;
1762 event->dnd.send_event = FALSE;
1763 event->dnd.context = context;
1764 g_object_ref (context);
1766 event->dnd.time = timestamp;
1768 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1770 switch (flags & 0x000f)
1773 context->action = 0;
1776 context->action = GDK_ACTION_MOVE;
1779 context->action = GDK_ACTION_COPY;
1782 context->action = GDK_ACTION_LINK;
1787 context->action = 0;
1789 return GDK_FILTER_TRANSLATE;
1791 return GDK_FILTER_REMOVE;
1794 static GdkFilterReturn
1795 motif_dnd_filter (GdkXEvent *xev,
1799 XEvent *xevent = (XEvent *)xev;
1804 guint32 source_window;
1806 gint16 x_root, y_root;
1809 if (!event->any.window ||
1810 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1811 return GDK_FILTER_CONTINUE; /* Not for us */
1813 /* First read some fields common to all Motif DND messages */
1815 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1816 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1817 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1819 is_reply = ((reason & 0x80) != 0);
1821 switch (reason & 0x7f)
1823 case XmTOP_LEVEL_ENTER:
1824 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1825 atom = MOTIF_UNPACK_LONG (xevent, 3);
1826 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1827 case XmTOP_LEVEL_LEAVE:
1828 return motif_top_level_leave (event, flags, timestamp);
1831 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1832 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1835 return motif_motion (event, flags, timestamp, x_root, y_root);
1837 return motif_drag_status (event, flags, timestamp);
1839 case XmDROP_SITE_ENTER:
1840 return motif_drag_status (event, flags, timestamp);
1842 case XmDROP_SITE_LEAVE:
1843 return motif_drag_status (event,
1844 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1847 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1848 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1849 atom = MOTIF_UNPACK_LONG (xevent, 3);
1850 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1853 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1856 case XmOPERATION_CHANGED:
1858 return motif_operation_changed (event, flags, timestamp);
1860 return motif_drag_status (event, flags, timestamp);
1863 /* To the best of my knowledge, these next two messages are
1864 * not part of the protocol, though they are defined in
1868 case XmDRAG_DROP_FINISH:
1872 return GDK_FILTER_REMOVE;
1875 /*************************************************************
1876 ***************************** XDND **************************
1877 *************************************************************/
1879 /* Utility functions */
1884 GdkDragAction action;
1885 } xdnd_actions_table[] = {
1886 { "XdndActionCopy", None, GDK_ACTION_COPY },
1887 { "XdndActionMove", None, GDK_ACTION_MOVE },
1888 { "XdndActionLink", None, GDK_ACTION_LINK },
1889 { "XdndActionAsk", None, GDK_ACTION_ASK },
1890 { "XdndActionPrivate", None, GDK_ACTION_COPY },
1893 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1894 static gboolean xdnd_actions_initialized = FALSE;
1897 xdnd_initialize_actions (void)
1901 xdnd_actions_initialized = TRUE;
1902 for (i=0; i < xdnd_n_actions; i++)
1903 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1906 static GdkDragAction
1907 xdnd_action_from_atom (GdkDisplay *display,
1910 GdkAtom atom = gdk_x11_xatom_to_atom_for_display (display, xatom);
1913 if (!xdnd_actions_initialized)
1914 xdnd_initialize_actions();
1916 for (i=0; i<xdnd_n_actions; i++)
1917 if (atom == xdnd_actions_table[i].atom)
1918 return xdnd_actions_table[i].action;
1924 xdnd_action_to_atom (GdkDisplay *display,
1925 GdkDragAction action)
1929 if (!xdnd_actions_initialized)
1930 xdnd_initialize_actions();
1932 for (i=0; i<xdnd_n_actions; i++)
1933 if (action == xdnd_actions_table[i].action)
1934 return gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
1941 static GdkFilterReturn
1942 xdnd_status_filter (GdkXEvent *xev,
1946 GdkDisplay *display;
1947 XEvent *xevent = (XEvent *)xev;
1948 guint32 dest_window = xevent->xclient.data.l[0];
1949 guint32 flags = xevent->xclient.data.l[1];
1950 Atom action = xevent->xclient.data.l[4];
1951 GdkDragContext *context;
1953 if (!event->any.window ||
1954 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
1955 return GDK_FILTER_CONTINUE; /* Not for us */
1958 g_message ("XdndStatus: dest_window: %#x action: %ld",
1959 dest_window, action));
1961 display = gdk_drawable_get_display (event->any.window);
1962 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
1966 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
1967 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1968 private->drag_status = GDK_DRAG_STATUS_DRAG;
1970 event->dnd.send_event = FALSE;
1971 event->dnd.type = GDK_DRAG_STATUS;
1972 event->dnd.context = context;
1973 g_object_ref (context);
1975 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1976 if (!(action != 0) != !(flags & 1))
1979 g_warning ("Received status event with flags not corresponding to action!\n"));
1983 context->action = xdnd_action_from_atom (display, action);
1985 return GDK_FILTER_TRANSLATE;
1988 return GDK_FILTER_REMOVE;
1991 static GdkFilterReturn
1992 xdnd_finished_filter (GdkXEvent *xev,
1996 GdkDisplay *display;
1997 XEvent *xevent = (XEvent *)xev;
1998 guint32 dest_window = xevent->xclient.data.l[0];
1999 GdkDragContext *context;
2001 if (!event->any.window ||
2002 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2003 return GDK_FILTER_CONTINUE; /* Not for us */
2006 g_message ("XdndFinished: dest_window: %#x", dest_window));
2008 display = gdk_drawable_get_display (event->any.window);
2009 context = gdk_drag_context_find (display, TRUE, xevent->xclient.window, dest_window);
2013 event->dnd.type = GDK_DROP_FINISHED;
2014 event->dnd.context = context;
2015 g_object_ref (context);
2017 return GDK_FILTER_TRANSLATE;
2020 return GDK_FILTER_REMOVE;
2024 xdnd_set_targets (GdkDragContext *context)
2026 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2028 GList *tmp_list = context->targets;
2030 gint n_atoms = g_list_length (context->targets);
2031 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2033 atomlist = g_new (Atom, n_atoms);
2037 atomlist[i] = gdk_x11_atom_to_xatom_for_display (display, GDK_POINTER_TO_ATOM (tmp_list->data));
2038 tmp_list = tmp_list->next;
2042 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2043 GDK_DRAWABLE_XID (context->source_window),
2044 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2045 XA_ATOM, 32, PropModeReplace,
2046 (guchar *)atomlist, n_atoms);
2050 private->xdnd_targets_set = 1;
2054 xdnd_set_actions (GdkDragContext *context)
2056 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2061 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2063 if (!xdnd_actions_initialized)
2064 xdnd_initialize_actions();
2066 actions = context->actions;
2068 for (i=0; i<xdnd_n_actions; i++)
2070 if (actions & xdnd_actions_table[i].action)
2072 actions &= ~xdnd_actions_table[i].action;
2077 atomlist = g_new (Atom, n_atoms);
2079 actions = context->actions;
2081 for (i=0; i<xdnd_n_actions; i++)
2083 if (actions & xdnd_actions_table[i].action)
2085 actions &= ~xdnd_actions_table[i].action;
2086 atomlist[n_atoms] = gdk_x11_atom_to_xatom_for_display (display, xdnd_actions_table[i].atom);
2091 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2092 GDK_DRAWABLE_XID (context->source_window),
2093 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2094 XA_ATOM, 32, PropModeReplace,
2095 (guchar *)atomlist, n_atoms);
2099 private->xdnd_actions_set = TRUE;
2100 private->xdnd_actions = context->actions;
2104 send_client_message_async_cb (Window window,
2108 GdkDragContext *context = data;
2110 g_message ("Got async callback for #%lx, success = %d",
2113 /* On failure, we immediately continue with the protocol
2114 * so we don't end up blocking for a timeout
2117 context->dest_window &&
2118 window == GDK_WINDOW_XID (context->dest_window))
2120 GdkEvent temp_event;
2121 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2123 g_object_unref (context->dest_window);
2124 context->dest_window = NULL;
2125 context->action = 0;
2127 private->drag_status = GDK_DRAG_STATUS_DRAG;
2129 temp_event.dnd.type = GDK_DRAG_STATUS;
2130 temp_event.dnd.window = context->source_window;
2131 temp_event.dnd.send_event = TRUE;
2132 temp_event.dnd.context = context;
2133 temp_event.dnd.time = GDK_CURRENT_TIME;
2135 gdk_event_put (&temp_event);
2138 g_object_unref (context);
2143 gdk_drag_context_get_display (GdkDragContext *context)
2145 if (context->source_window)
2146 return GDK_DRAWABLE_DISPLAY (context->source_window);
2147 else if (context->dest_window)
2148 return GDK_DRAWABLE_DISPLAY (context->dest_window);
2150 g_assert_not_reached ();
2155 send_client_message_async (GdkDragContext *context,
2159 XClientMessageEvent *event_send)
2161 GdkDisplay *display = gdk_drag_context_get_display (context);
2163 g_object_ref (context);
2165 _gdk_x11_send_client_message_async (display, window,
2166 propagate, event_mask, event_send,
2167 send_client_message_async_cb, context);
2171 xdnd_send_xevent (GdkDragContext *context,
2176 GdkDisplay *display = gdk_drag_context_get_display (context);
2180 g_assert (event_send->xany.type == ClientMessage);
2182 /* We short-circuit messages to ourselves */
2183 if (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2187 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2189 if (gdk_x11_get_xatom_by_name_for_display (display, xdnd_filters[i].atom_name) ==
2190 event_send->xclient.message_type)
2192 GdkEvent temp_event;
2193 temp_event.any.window = window;
2195 if ((*xdnd_filters[i].func) (event_send, &temp_event, NULL) == GDK_FILTER_TRANSLATE)
2197 gdk_event_put (&temp_event);
2198 g_object_unref (temp_event.dnd.context);
2206 xwindow = GDK_WINDOW_XWINDOW (window);
2208 if (_gdk_x11_display_is_root_window (display, xwindow))
2209 event_mask = ButtonPressMask;
2213 send_client_message_async (context, xwindow, propagate, event_mask,
2214 &event_send->xclient);
2220 xdnd_send_enter (GdkDragContext *context)
2223 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2224 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->dest_window);
2226 xev.xclient.type = ClientMessage;
2227 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndEnter");
2228 xev.xclient.format = 32;
2229 xev.xclient.window = private->drop_xid ?
2231 GDK_DRAWABLE_XID (context->dest_window);
2232 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2233 xev.xclient.data.l[1] = (3 << 24); /* version */
2234 xev.xclient.data.l[2] = 0;
2235 xev.xclient.data.l[3] = 0;
2236 xev.xclient.data.l[4] = 0;
2238 if (g_list_length (context->targets) > 3)
2240 if (!private->xdnd_targets_set)
2241 xdnd_set_targets (context);
2242 xev.xclient.data.l[1] |= 1;
2246 GList *tmp_list = context->targets;
2251 xev.xclient.data.l[i] = gdk_x11_atom_to_xatom_for_display (display,
2252 GDK_POINTER_TO_ATOM (tmp_list->data));
2253 tmp_list = tmp_list->next;
2258 if (!xdnd_send_xevent (context, context->dest_window,
2262 g_message ("Send event to %lx failed",
2263 GDK_DRAWABLE_XID (context->dest_window)));
2264 g_object_unref (context->dest_window);
2265 context->dest_window = NULL;
2270 xdnd_send_leave (GdkDragContext *context)
2272 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2275 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2277 xev.xclient.type = ClientMessage;
2278 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndLeave");
2279 xev.xclient.format = 32;
2280 xev.xclient.window = private->drop_xid ?
2282 GDK_DRAWABLE_XID (context->dest_window);
2283 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2284 xev.xclient.data.l[1] = 0;
2285 xev.xclient.data.l[2] = 0;
2286 xev.xclient.data.l[3] = 0;
2287 xev.xclient.data.l[4] = 0;
2289 if (!xdnd_send_xevent (context, context->dest_window,
2293 g_message ("Send event to %lx failed",
2294 GDK_DRAWABLE_XID (context->dest_window)));
2295 g_object_unref (context->dest_window);
2296 context->dest_window = NULL;
2301 xdnd_send_drop (GdkDragContext *context, guint32 time)
2303 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2304 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2307 xev.xclient.type = ClientMessage;
2308 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndDrop");
2309 xev.xclient.format = 32;
2310 xev.xclient.window = private->drop_xid ?
2312 GDK_DRAWABLE_XID (context->dest_window);
2313 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2314 xev.xclient.data.l[1] = 0;
2315 xev.xclient.data.l[2] = time;
2316 xev.xclient.data.l[3] = 0;
2317 xev.xclient.data.l[4] = 0;
2319 if (!xdnd_send_xevent (context, context->dest_window,
2323 g_message ("Send event to %lx failed",
2324 GDK_DRAWABLE_XID (context->dest_window)));
2325 g_object_unref (context->dest_window);
2326 context->dest_window = NULL;
2331 xdnd_send_motion (GdkDragContext *context,
2334 GdkDragAction action,
2337 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
2338 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
2341 xev.xclient.type = ClientMessage;
2342 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndPosition");
2343 xev.xclient.format = 32;
2344 xev.xclient.window = private->drop_xid ?
2346 GDK_DRAWABLE_XID (context->dest_window);
2347 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2348 xev.xclient.data.l[1] = 0;
2349 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2350 xev.xclient.data.l[3] = time;
2351 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
2353 if (!xdnd_send_xevent (context, context->dest_window,
2357 g_message ("Send event to %lx failed",
2358 GDK_DRAWABLE_XID (context->dest_window)));
2359 g_object_unref (context->dest_window);
2360 context->dest_window = NULL;
2362 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2366 xdnd_check_dest (GdkDisplay *display,
2369 gboolean retval = FALSE;
2372 unsigned long nitems, after;
2376 Atom xdnd_proxy_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndProxy");
2377 Atom xdnd_aware_atom = gdk_x11_get_xatom_by_name_for_display (display, "XdndAware");
2381 gdk_error_trap_push ();
2383 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), win,
2385 1, False, AnyPropertyType,
2386 &type, &format, &nitems, &after,
2387 (guchar **)&proxy_data) == Success)
2391 if ((format == 32) && (nitems == 1))
2393 proxy = *proxy_data;
2397 g_warning ("Invalid XdndOwner "
2398 "property on window %ld\n", win));
2403 if ((XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), proxy ? proxy : win,
2405 1, False, AnyPropertyType,
2406 &type, &format, &nitems, &after,
2407 (guchar **)&version) == Success) &&
2410 if ((format == 32) && (nitems == 1))
2417 g_warning ("Invalid XdndAware property on window %ld\n", win));
2423 gdk_error_trap_pop ();
2425 return retval ? (proxy ? proxy : win) : None;
2431 xdnd_read_actions (GdkDragContext *context)
2433 GdkDisplay *display = GDK_WINDOW_DISPLAY (context->source_window);
2436 gulong nitems, after;
2441 PRIVATE_DATA (context)->xdnd_have_actions = FALSE;
2443 if (gdk_window_get_window_type (context->source_window) == GDK_WINDOW_FOREIGN)
2445 /* Get the XdndActionList, if set */
2447 gdk_error_trap_push ();
2449 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
2450 GDK_DRAWABLE_XID (context->source_window),
2451 gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList"),
2453 False, XA_ATOM, &type, &format, &nitems,
2454 &after, (guchar **)&data) == Success &&
2457 context->actions = 0;
2459 for (i=0; i<nitems; i++)
2460 context->actions |= xdnd_action_from_atom (display, data[i]);
2462 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2464 #ifdef G_ENABLE_DEBUG
2465 if (_gdk_debug_flags & GDK_DEBUG_DND)
2467 GString *action_str = g_string_new (NULL);
2468 if (context->actions & GDK_ACTION_MOVE)
2469 g_string_append(action_str, "MOVE ");
2470 if (context->actions & GDK_ACTION_COPY)
2471 g_string_append(action_str, "COPY ");
2472 if (context->actions & GDK_ACTION_LINK)
2473 g_string_append(action_str, "LINK ");
2474 if (context->actions & GDK_ACTION_ASK)
2475 g_string_append(action_str, "ASK ");
2477 g_message("Xdnd actions = %s", action_str->str);
2478 g_string_free (action_str, TRUE);
2480 #endif /* G_ENABLE_DEBUG */
2485 gdk_error_trap_pop ();
2491 GdkDragContext *source_context;
2493 source_context = gdk_drag_context_find (display, TRUE,
2494 GDK_DRAWABLE_XID (context->source_window),
2495 GDK_DRAWABLE_XID (context->dest_window));
2499 context->actions = source_context->actions;
2500 PRIVATE_DATA (context)->xdnd_have_actions = TRUE;
2505 /* We have to make sure that the XdndActionList we keep internally
2506 * is up to date with the XdndActionList on the source window
2507 * because we get no notification, because Xdnd wasn't meant
2508 * to continually send actions. So we select on PropertyChangeMask
2509 * and add this filter.
2511 static GdkFilterReturn
2512 xdnd_source_window_filter (GdkXEvent *xev,
2516 XEvent *xevent = (XEvent *)xev;
2517 GdkDragContext *context = cb_data;
2518 GdkDisplay *display = GDK_WINDOW_DISPLAY(event->any.window);
2520 if ((xevent->xany.type == PropertyNotify) &&
2521 (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndActionList")))
2523 xdnd_read_actions (context);
2525 return GDK_FILTER_REMOVE;
2528 return GDK_FILTER_CONTINUE;
2532 xdnd_manage_source_filter (GdkDragContext *context,
2534 gboolean add_filter)
2536 if (!GDK_WINDOW_DESTROYED (window) &&
2537 gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN)
2539 gdk_error_trap_push ();
2543 gdk_window_set_events (window,
2544 gdk_window_get_events (window) |
2545 GDK_PROPERTY_CHANGE_MASK);
2546 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2550 gdk_window_remove_filter (window,
2551 xdnd_source_window_filter,
2553 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2554 * but we might want it for other reasons. (Like
2555 * INCR selection transactions).
2559 gdk_display_sync (gdk_drawable_get_display (window));
2560 gdk_error_trap_pop ();
2565 base_precache_atoms (GdkDisplay *display)
2567 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2569 if (!display_x11->base_dnd_atoms_precached)
2571 static const char *const precache_atoms[] = {
2572 "ENLIGHTENMENT_DESKTOP",
2576 "_MOTIF_DRAG_RECEIVER_INFO"
2579 _gdk_x11_precache_atoms (display,
2580 precache_atoms, G_N_ELEMENTS (precache_atoms));
2582 display_x11->base_dnd_atoms_precached = TRUE;
2587 xdnd_precache_atoms (GdkDisplay *display)
2589 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
2591 if (!display_x11->xdnd_atoms_precached)
2593 static const char *const precache_atoms[] = {
2599 "XdndActionPrivate",
2610 _gdk_x11_precache_atoms (display,
2611 precache_atoms, G_N_ELEMENTS (precache_atoms));
2613 display_x11->xdnd_atoms_precached = TRUE;
2617 static GdkFilterReturn
2618 xdnd_enter_filter (GdkXEvent *xev,
2622 GdkDisplay *display;
2623 GdkDisplayX11 *display_x11;
2624 XEvent *xevent = (XEvent *)xev;
2625 GdkDragContext *new_context;
2630 gulong nitems, after;
2633 guint32 source_window;
2637 if (!event->any.window ||
2638 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2639 return GDK_FILTER_CONTINUE; /* Not for us */
2641 source_window = xevent->xclient.data.l[0];
2642 get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2643 version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2645 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2646 display_x11 = GDK_DISPLAY_X11 (display);
2648 xdnd_precache_atoms (display);
2651 g_message ("XdndEnter: source_window: %#x, version: %#x",
2652 source_window, version));
2656 /* Old source ignore */
2657 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2658 return GDK_FILTER_REMOVE;
2661 if (display_x11->current_dest_drag != NULL)
2663 g_object_unref (display_x11->current_dest_drag);
2664 display_x11->current_dest_drag = NULL;
2667 new_context = gdk_drag_context_new ();
2668 new_context->protocol = GDK_DRAG_PROTO_XDND;
2669 new_context->is_source = FALSE;
2671 new_context->source_window = gdk_window_lookup_for_display (display, source_window);
2672 if (new_context->source_window)
2673 g_object_ref (new_context->source_window);
2676 new_context->source_window = gdk_window_foreign_new_for_display (display, source_window);
2677 if (!new_context->source_window)
2679 g_object_unref (new_context);
2680 return GDK_FILTER_REMOVE;
2683 new_context->dest_window = event->any.window;
2684 g_object_ref (new_context->dest_window);
2686 new_context->targets = NULL;
2689 gdk_error_trap_push ();
2690 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2692 gdk_x11_get_xatom_by_name_for_display (display, "XdndTypeList"),
2694 False, XA_ATOM, &type, &format, &nitems,
2695 &after, (guchar **)&data);
2697 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2699 g_object_unref (new_context);
2700 return GDK_FILTER_REMOVE;
2703 for (i=0; i<nitems; i++)
2704 new_context->targets =
2705 g_list_append (new_context->targets,
2706 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2714 if (xevent->xclient.data.l[2+i])
2715 new_context->targets =
2716 g_list_append (new_context->targets,
2717 GDK_ATOM_TO_POINTER (gdk_x11_xatom_to_atom_for_display (display,
2718 xevent->xclient.data.l[2+i])));
2721 #ifdef G_ENABLE_DEBUG
2722 if (_gdk_debug_flags & GDK_DEBUG_DND)
2723 print_target_list (new_context->targets);
2724 #endif /* G_ENABLE_DEBUG */
2726 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2727 xdnd_read_actions (new_context);
2729 event->dnd.type = GDK_DRAG_ENTER;
2730 event->dnd.context = new_context;
2731 g_object_ref (new_context);
2733 display_x11->current_dest_drag = new_context;
2735 return GDK_FILTER_TRANSLATE;
2738 static GdkFilterReturn
2739 xdnd_leave_filter (GdkXEvent *xev,
2743 XEvent *xevent = (XEvent *)xev;
2744 guint32 source_window = xevent->xclient.data.l[0];
2745 GdkDisplay *display;
2746 GdkDisplayX11 *display_x11;
2748 if (!event->any.window ||
2749 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2750 return GDK_FILTER_CONTINUE; /* Not for us */
2753 g_message ("XdndLeave: source_window: %#x",
2756 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2757 display_x11 = GDK_DISPLAY_X11 (display);
2759 xdnd_precache_atoms (display);
2761 if ((display_x11->current_dest_drag != NULL) &&
2762 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2763 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2765 event->dnd.type = GDK_DRAG_LEAVE;
2766 /* Pass ownership of context to the event */
2767 event->dnd.context = display_x11->current_dest_drag;
2769 display_x11->current_dest_drag = NULL;
2771 return GDK_FILTER_TRANSLATE;
2774 return GDK_FILTER_REMOVE;
2777 static GdkFilterReturn
2778 xdnd_position_filter (GdkXEvent *xev,
2782 XEvent *xevent = (XEvent *)xev;
2783 guint32 source_window = xevent->xclient.data.l[0];
2784 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2785 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2786 guint32 time = xevent->xclient.data.l[3];
2787 Atom action = xevent->xclient.data.l[4];
2789 GdkDisplay *display;
2790 GdkDisplayX11 *display_x11;
2792 if (!event->any.window ||
2793 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2794 return GDK_FILTER_CONTINUE; /* Not for us */
2797 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2798 source_window, x_root, y_root, time, action));
2800 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2801 display_x11 = GDK_DISPLAY_X11 (display);
2803 xdnd_precache_atoms (display);
2805 if ((display_x11->current_dest_drag != NULL) &&
2806 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2807 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2809 event->dnd.type = GDK_DRAG_MOTION;
2810 event->dnd.context = display_x11->current_dest_drag;
2811 g_object_ref (display_x11->current_dest_drag);
2813 event->dnd.time = time;
2815 display_x11->current_dest_drag->suggested_action = xdnd_action_from_atom (display, action);
2817 if (!(PRIVATE_DATA (display_x11->current_dest_drag))->xdnd_have_actions)
2818 display_x11->current_dest_drag->actions = display_x11->current_dest_drag->suggested_action;
2820 event->dnd.x_root = x_root;
2821 event->dnd.y_root = y_root;
2823 (PRIVATE_DATA (display_x11->current_dest_drag))->last_x = x_root;
2824 (PRIVATE_DATA (display_x11->current_dest_drag))->last_y = y_root;
2826 return GDK_FILTER_TRANSLATE;
2829 return GDK_FILTER_REMOVE;
2832 static GdkFilterReturn
2833 xdnd_drop_filter (GdkXEvent *xev,
2837 XEvent *xevent = (XEvent *)xev;
2838 guint32 source_window = xevent->xclient.data.l[0];
2839 guint32 time = xevent->xclient.data.l[2];
2840 GdkDisplay *display;
2841 GdkDisplayX11 *display_x11;
2843 if (!event->any.window ||
2844 gdk_window_get_window_type (event->any.window) == GDK_WINDOW_FOREIGN)
2845 return GDK_FILTER_CONTINUE; /* Not for us */
2848 g_message ("XdndDrop: source_window: %#x time: %d",
2849 source_window, time));
2851 display = GDK_DRAWABLE_DISPLAY (event->any.window);
2852 display_x11 = GDK_DISPLAY_X11 (display);
2854 xdnd_precache_atoms (display);
2856 if ((display_x11->current_dest_drag != NULL) &&
2857 (display_x11->current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2858 (GDK_DRAWABLE_XID (display_x11->current_dest_drag->source_window) == source_window))
2860 GdkDragContextPrivateX11 *private;
2861 private = PRIVATE_DATA (display_x11->current_dest_drag);
2863 event->dnd.type = GDK_DROP_START;
2865 event->dnd.context = display_x11->current_dest_drag;
2866 g_object_ref (display_x11->current_dest_drag);
2868 event->dnd.time = time;
2869 event->dnd.x_root = private->last_x;
2870 event->dnd.y_root = private->last_y;
2872 return GDK_FILTER_TRANSLATE;
2875 return GDK_FILTER_REMOVE;
2878 /*************************************************************
2879 ************************** Public API ***********************
2880 *************************************************************/
2882 _gdk_dnd_init (GdkDisplay *display)
2887 gdk_display_add_client_message_filter (
2889 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2890 motif_dnd_filter, NULL);
2892 for (i = 0; i < G_N_ELEMENTS (xdnd_filters); i++)
2894 gdk_display_add_client_message_filter (
2896 gdk_atom_intern (xdnd_filters[i].atom_name, FALSE),
2897 xdnd_filters[i].func, NULL);
2904 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2906 if (context->dest_window)
2908 switch (context->protocol)
2910 case GDK_DRAG_PROTO_MOTIF:
2911 motif_send_leave (context, time);
2913 case GDK_DRAG_PROTO_XDND:
2914 xdnd_send_leave (context);
2916 case GDK_DRAG_PROTO_ROOTWIN:
2917 case GDK_DRAG_PROTO_NONE:
2922 g_object_unref (context->dest_window);
2923 context->dest_window = NULL;
2929 * @window: the source window for this drag.
2930 * @targets: the list of offered targets.
2932 * Starts a drag and creates a new drag context for it.
2934 * This function is called by the drag source.
2936 * Return value: a newly created #GdkDragContext.
2939 gdk_drag_begin (GdkWindow *window,
2942 GdkDragContext *new_context;
2944 g_return_val_if_fail (window != NULL, NULL);
2946 new_context = gdk_drag_context_new ();
2947 new_context->is_source = TRUE;
2948 new_context->source_window = window;
2949 g_object_ref (window);
2951 new_context->targets = g_list_copy (targets);
2952 precache_target_list (new_context);
2954 new_context->actions = 0;
2960 * gdk_drag_get_protocol_for_display:
2961 * @display: the #GdkDisplay where the destination window resides
2962 * @xid: the X id of the destination window.
2963 * @protocol: location where the supported DND protocol is returned.
2964 * @returns: the X id of the window where the drop should happen. This
2965 * may be @xid or the X id of a proxy window, or None if @xid doesn't
2966 * support Drag and Drop.
2968 * Finds out the DND protocol supported by a window.
2973 gdk_drag_get_protocol_for_display (GdkDisplay *display,
2975 GdkDragProtocol *protocol)
2979 g_return_val_if_fail (GDK_IS_DISPLAY (display), None);
2981 base_precache_atoms (display);
2983 /* Check for a local drag
2985 window = gdk_window_lookup_for_display (display, xid);
2987 gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN)
2989 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
2991 *protocol = GDK_DRAG_PROTO_XDND;
2992 xdnd_precache_atoms (display);
2993 GDK_NOTE (DND, g_message ("Entering local Xdnd window %#x\n", xid));
3000 if ((retval = xdnd_check_dest (display, xid)))
3002 *protocol = GDK_DRAG_PROTO_XDND;
3003 xdnd_precache_atoms (display);
3004 GDK_NOTE (DND, g_message ("Entering Xdnd window %#x\n", xid));
3007 else if ((retval = motif_check_dest (display, xid)))
3009 *protocol = GDK_DRAG_PROTO_MOTIF;
3010 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
3015 /* Check if this is a root window */
3017 gboolean rootwin = FALSE;
3020 unsigned long nitems, after;
3021 unsigned char *data;
3023 if (_gdk_x11_display_is_root_window (display, (Window) xid))
3026 gdk_error_trap_push ();
3030 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
3031 gdk_x11_get_xatom_by_name_for_display (display, "ENLIGHTENMENT_DESKTOP"),
3032 0, 0, False, AnyPropertyType,
3033 &type, &format, &nitems, &after, &data) == Success &&
3041 /* Until I find out what window manager the next one is for,
3042 * I'm leaving it commented out. It's supported in the
3043 * xscreensaver sources, though.
3048 if (XGetWindowProperty (gdk_display, win,
3049 gdk_x11_get_xatom_by_name ("__SWM_VROOT"),
3050 0, 0, False, AnyPropertyType,
3051 &type, &format, &nitems, &data) &&
3057 gdk_error_trap_pop ();
3061 *protocol = GDK_DRAG_PROTO_ROOTWIN;
3066 *protocol = GDK_DRAG_PROTO_NONE;
3070 static GdkWindowCache *
3071 drag_context_find_window_cache (GdkDragContext *context,
3074 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3076 GdkWindowCache *cache;
3078 for (tmp_list = private->window_caches; tmp_list; tmp_list = tmp_list->next)
3080 cache = tmp_list->data;
3081 if (cache->screen == screen)
3085 cache = gdk_window_cache_new (screen);
3086 private->window_caches = g_slist_prepend (private->window_caches, cache);
3092 * gdk_drag_find_window_for_screen:
3093 * @context: a #GdkDragContext
3094 * @drag_window: a window which may be at the pointer position, but
3095 * should be ignored, since it is put up by the drag source as an icon.
3096 * @screen: the screen where the destination window is sought.
3097 * @x_root: the x position of the pointer in root coordinates.
3098 * @y_root: the y position of the pointer in root coordinates.
3099 * @dest_window: location to store the destination window in.
3100 * @protocol: location to store the DND protocol in.
3102 * Finds the destination window and DND protocol to use at the
3103 * given pointer position.
3105 * This function is called by the drag source to obtain the
3106 * @dest_window and @protocol parameters for gdk_drag_motion().
3111 gdk_drag_find_window_for_screen (GdkDragContext *context,
3112 GdkWindow *drag_window,
3116 GdkWindow **dest_window,
3117 GdkDragProtocol *protocol)
3119 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3120 GdkWindowCache *window_cache;
3121 GdkDisplay *display;
3124 g_return_if_fail (context != NULL);
3126 display = GDK_WINDOW_DISPLAY (context->source_window);
3128 window_cache = drag_context_find_window_cache (context, screen);
3130 dest = get_client_window_at_coords (window_cache,
3132 GDK_DRAWABLE_XID (drag_window) : None,
3135 if (private->dest_xid != dest)
3138 private->dest_xid = dest;
3140 /* Check if new destination accepts drags, and which protocol */
3142 /* There is some ugliness here. We actually need to pass
3143 * _three_ pieces of information to drag_motion - dest_window,
3144 * protocol, and the XID of the unproxied window. The first
3145 * two are passed explicitely, the third implicitly through
3146 * protocol->dest_xid.
3148 if ((recipient = gdk_drag_get_protocol_for_display (display, dest, protocol)))
3150 *dest_window = gdk_window_lookup_for_display (display, recipient);
3152 g_object_ref (*dest_window);
3154 *dest_window = gdk_window_foreign_new_for_display (display, recipient);
3157 *dest_window = NULL;
3161 *dest_window = context->dest_window;
3163 g_object_ref (*dest_window);
3164 *protocol = context->protocol;
3170 * @context: a #GdkDragContext.
3171 * @dest_window: the new destination window, obtained by
3172 * gdk_drag_find_window().
3173 * @protocol: the DND protocol in use, obtained by gdk_drag_find_window().
3174 * @x_root: the x position of the pointer in root coordinates.
3175 * @y_root: the y position of the pointer in root coordinates.
3176 * @suggested_action: the suggested action.
3177 * @possible_actions: the possible actions.
3178 * @time_: the timestamp for this operation.
3180 * Updates the drag context when the pointer moves or the
3181 * set of actions changes.
3183 * This function is called by the drag source.
3185 * Return value: FIXME
3188 gdk_drag_motion (GdkDragContext *context,
3189 GdkWindow *dest_window,
3190 GdkDragProtocol protocol,
3193 GdkDragAction suggested_action,
3194 GdkDragAction possible_actions,
3197 GdkDragContextPrivateX11 *private = PRIVATE_DATA (context);
3199 g_return_val_if_fail (context != NULL, FALSE);
3201 private->old_actions = context->actions;
3202 context->actions = possible_actions;
3204 if (private->old_actions != possible_actions)
3205 private->xdnd_actions_set = FALSE;
3207 /* When we have a Xdnd target, make sure our XdndActionList
3208 * matches the current actions;
3210 if (protocol == GDK_DRAG_PROTO_XDND && !private->xdnd_actions_set)
3214 if (gdk_window_get_window_type (dest_window) == GDK_WINDOW_FOREIGN)
3215 xdnd_set_actions (context);
3216 else if (context->dest_window == dest_window)
3218 GdkDisplay *display = GDK_WINDOW_DISPLAY (dest_window);
3219 GdkDragContext *dest_context;
3221 dest_context = gdk_drag_context_find (display, FALSE,
3222 GDK_DRAWABLE_XID (context->source_window),
3223 GDK_DRAWABLE_XID (dest_window));
3227 dest_context->actions = context->actions;
3228 PRIVATE_DATA (dest_context)->xdnd_have_actions = TRUE;
3234 if (context->dest_window != dest_window)
3236 GdkEvent temp_event;
3238 /* Send a leave to the last destination */
3239 gdk_drag_do_leave (context, time);
3240 private->drag_status = GDK_DRAG_STATUS_DRAG;
3242 /* Check if new destination accepts drags, and which protocol */
3246 context->dest_window = dest_window;
3247 private->drop_xid = private->dest_xid;
3248 g_object_ref (context->dest_window);
3249 context->protocol = protocol;
3253 case GDK_DRAG_PROTO_MOTIF:
3254 motif_send_enter (context, time);
3257 case GDK_DRAG_PROTO_XDND:
3258 xdnd_send_enter (context);
3261 case GDK_DRAG_PROTO_ROOTWIN:
3262 case GDK_DRAG_PROTO_NONE:
3266 private->old_action = suggested_action;
3267 context->suggested_action = suggested_action;
3268 private->old_actions = possible_actions;
3272 context->dest_window = NULL;
3273 private->drop_xid = None;
3274 context->action = 0;
3277 /* Push a status event, to let the client know that
3281 temp_event.dnd.type = GDK_DRAG_STATUS;
3282 temp_event.dnd.window = context->source_window;
3283 /* We use this to signal a synthetic status. Perhaps
3284 * we should use an extra field...
3286 temp_event.dnd.send_event = TRUE;
3288 temp_event.dnd.context = context;
3289 temp_event.dnd.time = time;
3291 gdk_event_put (&temp_event);
3295 private->old_action = context->suggested_action;
3296 context->suggested_action = suggested_action;
3299 /* Send a drag-motion event */
3301 private->last_x = x_root;
3302 private->last_y = y_root;
3304 if (context->dest_window)
3306 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
3308 switch (context->protocol)
3310 case GDK_DRAG_PROTO_MOTIF:
3311 motif_send_motion (context, x_root, y_root, suggested_action, time);
3314 case GDK_DRAG_PROTO_XDND:
3315 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3318 case GDK_DRAG_PROTO_ROOTWIN:
3320 GdkEvent temp_event;
3321 /* GTK+ traditionally has used application/x-rootwin-drop,
3322 * but the XDND spec specifies x-rootwindow-drop.
3324 GdkAtom target1 = gdk_atom_intern ("application/x-rootwindow-drop", FALSE);
3325 GdkAtom target2 = gdk_atom_intern ("application/x-rootwin-drop", FALSE);
3327 if (g_list_find (context->targets,
3328 GDK_ATOM_TO_POINTER (target1)) ||
3329 g_list_find (context->targets,
3330 GDK_ATOM_TO_POINTER (target2)))
3331 context->action = context->suggested_action;
3333 context->action = 0;
3335 temp_event.dnd.type = GDK_DRAG_STATUS;
3336 temp_event.dnd.window = context->source_window;
3337 temp_event.dnd.send_event = FALSE;
3338 temp_event.dnd.context = context;
3339 temp_event.dnd.time = time;
3341 gdk_event_put (&temp_event);
3344 case GDK_DRAG_PROTO_NONE:
3345 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3360 * @context: a #GdkDragContext.
3361 * @time_: the timestamp for this operation.
3363 * Drops on the current destination.
3365 * This function is called by the drag source.
3368 gdk_drag_drop (GdkDragContext *context,
3371 g_return_if_fail (context != NULL);
3373 if (context->dest_window)
3375 switch (context->protocol)
3377 case GDK_DRAG_PROTO_MOTIF:
3378 motif_send_leave (context, time);
3379 motif_send_drop (context, time);
3382 case GDK_DRAG_PROTO_XDND:
3383 xdnd_send_drop (context, time);
3386 case GDK_DRAG_PROTO_ROOTWIN:
3387 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3389 case GDK_DRAG_PROTO_NONE:
3390 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3400 * @context: a #GdkDragContext.
3401 * @time_: the timestamp for this operation.
3403 * Aborts a drag without dropping.
3405 * This function is called by the drag source.
3408 gdk_drag_abort (GdkDragContext *context,
3411 g_return_if_fail (context != NULL);
3413 gdk_drag_do_leave (context, time);
3416 /* Destination side */
3420 * @context: a #GdkDragContext.
3421 * @action: the selected action which will be taken when a drop happens,
3422 * or 0 to indicate that a drop will not be accepted.
3423 * @time_: the timestamp for this operation.
3425 * Selects one of the actions offered by the drag source.
3427 * This function is called by the drag destination in response to
3428 * gdk_drag_motion() called by the drag source.
3431 gdk_drag_status (GdkDragContext *context,
3432 GdkDragAction action,
3435 GdkDragContextPrivateX11 *private;
3437 GdkDisplay *display;
3439 g_return_if_fail (context != NULL);
3441 private = PRIVATE_DATA (context);
3442 display = GDK_DRAWABLE_DISPLAY (context->source_window);
3444 context->action = action;
3446 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3448 gboolean need_coords = FALSE;
3450 xev.xclient.type = ClientMessage;
3451 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3452 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3453 xev.xclient.format = 8;
3454 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3456 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3458 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3462 if ((action != 0) != (private->old_action != 0))
3466 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3470 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3474 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3479 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3483 case GDK_ACTION_MOVE:
3484 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3486 case GDK_ACTION_COPY:
3487 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3489 case GDK_ACTION_LINK:
3490 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3493 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3498 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3500 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3502 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3506 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3507 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3510 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3512 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3513 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3515 if (!_gdk_send_xevent (display,
3516 GDK_DRAWABLE_XID (context->source_window),
3519 g_message ("Send event to %lx failed",
3520 GDK_DRAWABLE_XID (context->source_window)));
3522 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3524 xev.xclient.type = ClientMessage;
3525 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndStatus");
3526 xev.xclient.format = 32;
3527 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3529 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3530 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3531 xev.xclient.data.l[2] = 0;
3532 xev.xclient.data.l[3] = 0;
3533 xev.xclient.data.l[4] = xdnd_action_to_atom (display, action);
3535 if (!xdnd_send_xevent (context, context->source_window,
3538 g_message ("Send event to %lx failed",
3539 GDK_DRAWABLE_XID (context->source_window)));
3542 private->old_action = action;
3547 * @context: a #GdkDragContext.
3548 * @ok: %TRUE if the drop is accepted.
3549 * @time_: the timestamp for this operation.
3551 * Accepts or rejects a drop.
3553 * This function is called by the drag destination in response
3554 * to a drop initiated by the drag source.
3557 gdk_drop_reply (GdkDragContext *context,
3561 GdkDragContextPrivateX11 *private;
3563 g_return_if_fail (context != NULL);
3565 private = PRIVATE_DATA (context);
3567 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3569 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3572 xev.xclient.type = ClientMessage;
3573 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display,
3574 "_MOTIF_DRAG_AND_DROP_MESSAGE");
3575 xev.xclient.format = 8;
3577 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3578 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3580 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3581 (XmDROP_SITE_VALID << 4) |
3582 (XmDROP_NOOP << 8) |
3585 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3586 (XmNO_DROP_SITE << 4) |
3587 (XmDROP_NOOP << 8) |
3588 (XmDROP_CANCEL << 12);
3589 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3590 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3591 MOTIF_XCLIENT_LONG (&xev, 2) = 0;
3592 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
3593 MOTIF_XCLIENT_LONG (&xev, 4) = 0;
3595 _gdk_send_xevent (display,
3596 GDK_DRAWABLE_XID (context->source_window),
3603 * @context: a #GtkDragContext.
3604 * @success: %TRUE if the data was successfully received.
3605 * @time_: the timestamp for this operation.
3607 * Ends the drag operation after a drop.
3609 * This function is called by the drag destination.
3612 gdk_drop_finish (GdkDragContext *context,
3616 g_return_if_fail (context != NULL);
3618 if (context->protocol == GDK_DRAG_PROTO_XDND)
3620 GdkDisplay *display = GDK_DRAWABLE_DISPLAY (context->source_window);
3623 xev.xclient.type = ClientMessage;
3624 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "XdndFinished");
3625 xev.xclient.format = 32;
3626 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3628 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3629 xev.xclient.data.l[1] = 0;
3630 xev.xclient.data.l[2] = 0;
3631 xev.xclient.data.l[3] = 0;
3632 xev.xclient.data.l[4] = 0;
3634 if (!xdnd_send_xevent (context, context->source_window,
3637 g_message ("Send event to %lx failed",
3638 GDK_DRAWABLE_XID (context->source_window)));
3644 gdk_window_register_dnd (GdkWindow *window)
3646 static gulong xdnd_version = 3;
3647 MotifDragReceiverInfo info;
3648 Atom motif_drag_receiver_info_atom;
3649 GdkDisplay *display = gdk_drawable_get_display (window);
3651 g_return_if_fail (window != NULL);
3653 base_precache_atoms (display);
3655 if (g_object_get_data (G_OBJECT (window), "gdk-dnd-registered") != NULL)
3658 g_object_set_data (G_OBJECT (window), "gdk-dnd-registered", GINT_TO_POINTER (TRUE));
3660 /* Set Motif drag receiver information property */
3662 motif_drag_receiver_info_atom = gdk_x11_get_xatom_by_name_for_display (display,
3663 "_MOTIF_DRAG_RECEIVER_INFO");
3664 info.byte_order = local_byte_order;
3665 info.protocol_version = 0;
3666 info.protocol_style = XmDRAG_DYNAMIC;
3667 info.proxy_window = None;
3668 info.num_drop_sites = 0;
3669 info.total_size = sizeof(info);
3671 XChangeProperty (GDK_DISPLAY_XDISPLAY (display), GDK_DRAWABLE_XID (window),
3672 motif_drag_receiver_info_atom,
3673 motif_drag_receiver_info_atom,
3680 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3681 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
3682 GDK_DRAWABLE_XID (window),
3683 gdk_x11_get_xatom_by_name_for_display (display, "XdndAware"),
3684 XA_ATOM, 32, PropModeReplace,
3685 (guchar *)&xdnd_version, 1);
3689 * gdk_drag_get_selection:
3690 * @context: a #GdkDragContext.
3692 * Returns the selection atom for the current source window.
3694 * Return value: the selection atom.
3697 gdk_drag_get_selection (GdkDragContext *context)
3699 g_return_val_if_fail (context != NULL, GDK_NONE);
3701 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3702 return gdk_x11_xatom_to_atom_for_display (GDK_DRAWABLE_DISPLAY (context->source_window),
3703 (PRIVATE_DATA (context))->motif_selection);
3704 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3705 return gdk_atom_intern ("XdndSelection", FALSE);