1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
20 #include <X11/Xatom.h>
23 #include "gdk/gdkprivate.h"
26 typedef struct _GdkDragContextPrivate GdkDragContextPrivate;
30 GDK_DRAG_STATUS_MOTION_WAIT,
31 GDK_DRAG_STATUS_ACTION_WAIT,
37 gint x, y, width, height;
43 GHashTable *child_hash;
47 /* Structure that holds information about a drag in progress.
48 * this is used on both source and destination sides.
50 struct _GdkDragContextPrivate {
51 GdkDragContext context;
53 GdkAtom motif_selection;
54 GdkAtom xdnd_selection;
57 guint16 last_x; /* Coordinates from last event */
59 GdkDragAction old_action; /* The last action we sent to the source */
62 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
63 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
64 guint drag_status : 4; /* current status of drag */
66 GdkWindowCache *window_cache;
69 GdkDragContext *current_dest_drag = NULL;
71 /* Forward declarations */
73 static void gdk_window_cache_destroy (GdkWindowCache *cache);
75 static void motif_read_target_table (void);
76 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
80 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
84 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
88 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
92 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
96 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
100 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
106 static GList *contexts;
109 gdk_drag_context_new (void)
111 GdkDragContextPrivate *result;
113 result = g_new0 (GdkDragContextPrivate, 1);
115 result->ref_count = 1;
117 contexts = g_list_prepend (contexts, result);
119 return (GdkDragContext *)result;
123 gdk_drag_context_ref (GdkDragContext *context)
125 ((GdkDragContextPrivate *)context)->ref_count++;
129 gdk_drag_context_unref (GdkDragContext *context)
131 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
132 private->ref_count--;
134 if (private->ref_count == 0)
136 g_dataset_destroy (private);
138 g_list_free (context->targets);
140 if (context->source_window)
141 gdk_window_unref (context->source_window);
143 if (context->dest_window)
144 gdk_window_unref (context->dest_window);
146 if (private->window_cache)
147 gdk_window_cache_destroy (private->window_cache);
149 contexts = g_list_remove (contexts, private);
154 static GdkDragContext *
155 gdk_drag_context_find (gboolean is_source,
159 GList *tmp_list = contexts;
163 GdkDragContext *context = (GdkDragContext *)tmp_list->data;
165 if ((!context->is_source == !is_source) &&
166 ((source_xid == None) || (context->source_window &&
167 (GDK_WINDOW_XWINDOW (context->source_window) == source_xid))) &&
168 ((dest_xid == None) || (context->dest_window &&
169 (GDK_WINDOW_XWINDOW (context->dest_window) == dest_xid))))
172 tmp_list = tmp_list->next;
178 /* Utility functions */
181 gdk_window_cache_add (GdkWindowCache *cache,
183 gint x, gint y, gint width, gint height,
186 GdkCacheChild *child = g_new (GdkCacheChild, 1);
191 child->width = width;
192 child->height = height;
193 child->mapped = mapped;
195 cache->children = g_list_prepend (cache->children, child);
196 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
200 static GdkFilterReturn
201 gdk_window_cache_filter (GdkXEvent *xev,
205 XEvent *xevent = (XEvent *)xev;
206 GdkWindowCache *cache = data;
208 switch (xevent->type)
210 case CirculateNotify:
212 case ConfigureNotify:
214 XConfigureEvent *xce = &xevent->xconfigure;
217 node = g_hash_table_lookup (cache->child_hash,
218 GUINT_TO_POINTER (xce->window));
221 GdkCacheChild *child = node->data;
224 child->width = xce->width;
225 child->height = xce->height;
226 if (xce->above == None && (node->next))
228 GList *last = g_list_last (cache->children);
229 cache->children = g_list_remove_link (cache->children, node);
236 GList *above_node = g_hash_table_lookup (cache->child_hash,
237 GUINT_TO_POINTER (xce->above));
238 if (above_node && node->prev != above_node)
240 cache->children = g_list_remove_link (cache->children, node);
241 node->next = above_node->next;
242 node->prev = above_node;
243 above_node->next = node;
251 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
252 if (!g_hash_table_lookup (cache->child_hash,
253 GUINT_TO_POINTER (xcwe->window)))
254 gdk_window_cache_add (cache, xcwe->window,
255 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
261 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
264 node = g_hash_table_lookup (cache->child_hash,
265 GUINT_TO_POINTER (xdwe->window));
268 g_hash_table_remove (cache->child_hash,
269 GUINT_TO_POINTER (xdwe->window));
270 cache->children = g_list_remove_link (cache->children, node);
272 g_list_free_1 (node);
278 XMapEvent *xme = &xevent->xmap;
281 node = g_hash_table_lookup (cache->child_hash,
282 GUINT_TO_POINTER (xme->window));
285 GdkCacheChild *child = node->data;
286 child->mapped = TRUE;
294 XMapEvent *xume = &xevent->xmap;
297 node = g_hash_table_lookup (cache->child_hash,
298 GUINT_TO_POINTER (xume->window));
301 GdkCacheChild *child = node->data;
302 child->mapped = FALSE;
307 return GDK_FILTER_CONTINUE;
309 return GDK_FILTER_REMOVE;
312 static GdkWindowCache *
313 gdk_window_cache_new (void)
315 XWindowAttributes xwa;
316 Window root, parent, *children;
317 unsigned int nchildren;
320 gint old_warnings = gdk_error_warnings;
322 GdkWindowCache *result = g_new (GdkWindowCache, 1);
324 result->children = NULL;
325 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
327 XGetWindowAttributes (gdk_display, gdk_root_window, &xwa);
328 result->old_event_mask = xwa.your_event_mask;
329 XSelectInput (gdk_display, gdk_root_window,
330 result->old_event_mask | SubstructureNotifyMask);
331 gdk_window_add_filter ((GdkWindow *)&gdk_root_parent,
332 gdk_window_cache_filter, result);
335 gdk_error_warnings = 0;
337 if (XQueryTree(gdk_display, gdk_root_window,
338 &root, &parent, &children, &nchildren) == 0)
341 for (i = 0; i < nchildren ; i++)
343 XGetWindowAttributes (gdk_display, children[i], &xwa);
345 gdk_window_cache_add (result, children[i],
346 xwa.x, xwa.y, xwa.width, xwa.height,
347 xwa.map_state != IsUnmapped);
349 if (gdk_error_code != 0)
353 gdk_window_cache_add (result, children[i],
354 xwa.x, xwa.y, xwa.width, xwa.height,
355 (xwa.map_state != IsUnmapped));
361 gdk_error_warnings = old_warnings;
367 gdk_window_cache_destroy (GdkWindowCache *cache)
369 XSelectInput (gdk_display, gdk_root_window, cache->old_event_mask);
370 gdk_window_remove_filter ((GdkWindow *)&gdk_root_parent,
371 gdk_window_cache_filter, cache);
373 g_list_foreach (cache->children, (GFunc)g_free, NULL);
374 g_list_free (cache->children);
375 g_hash_table_destroy (cache->child_hash);
379 get_client_window_at_coords_recurse (Window win,
386 unsigned long nitems, after;
390 static Atom wm_state_atom = None;
393 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
395 XGetWindowProperty (gdk_display, win,
396 wm_state_atom, 0, 0, False, AnyPropertyType,
397 &type, &format, &nitems, &after, &data);
399 if (gdk_error_code != 0)
411 XTranslateCoordinates (gdk_display, gdk_root_window, win,
412 x_root, y_root, &dest_x, &dest_y, &child);
414 if (gdk_error_code != 0)
421 return get_client_window_at_coords_recurse (child, x_root, y_root);
427 get_client_window_at_coords (GdkWindowCache *cache,
433 Window retval = None;
435 gint old_warnings = gdk_error_warnings;
438 gdk_error_warnings = 0;
440 tmp_list = cache->children;
442 while (tmp_list && !retval)
444 GdkCacheChild *child = tmp_list->data;
446 if ((child->xid != ignore) && (child->mapped))
448 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
449 (y_root >= child->y) && (y_root < child->y + child->height))
451 retval = get_client_window_at_coords_recurse (child->xid,
458 tmp_list = tmp_list->next;
461 gdk_error_warnings = old_warnings;
465 return gdk_root_window;
470 get_client_window_at_coords_recurse (Window win,
477 unsigned long nitems, after;
481 static Atom wm_state_atom = None;
484 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
486 XGetWindowProperty (gdk_display, win,
487 wm_state_atom, 0, 0, False, AnyPropertyType,
488 &type, &format, &nitems, &after, &data);
490 if (gdk_error_code != 0)
502 XTranslateCoordinates (gdk_display, gdk_root_window, win,
503 x_root, y_root, &dest_x, &dest_y, &child);
505 if (gdk_error_code != 0)
512 return get_client_window_at_coords_recurse (child, x_root, y_root);
518 get_client_window_at_coords (Window ignore,
522 Window root, parent, *children;
523 unsigned int nchildren;
525 Window retval = None;
527 gint old_warnings = gdk_error_warnings;
530 gdk_error_warnings = 0;
532 if (XQueryTree(gdk_display, gdk_root_window,
533 &root, &parent, &children, &nchildren) == 0)
536 for (i = nchildren - 1; (i >= 0) && (retval == None); i--)
538 if (children[i] != ignore)
540 XWindowAttributes xwa;
542 XGetWindowAttributes (gdk_display, children[i], &xwa);
544 if (gdk_error_code != 0)
546 else if ((xwa.map_state == IsViewable) &&
547 (x_root >= xwa.x) && (x_root < xwa.x + (gint)xwa.width) &&
548 (y_root >= xwa.y) && (y_root < xwa.y + (gint)xwa.height))
550 retval = get_client_window_at_coords_recurse (children[i],
553 retval = children[i];
560 gdk_error_warnings = old_warnings;
564 return gdk_root_window;
568 /*************************************************************
569 ***************************** MOTIF *************************
570 *************************************************************/
572 /* values used in the message type for Motif DND */
585 /* Values used to specify type of protocol to use */
589 XmDRAG_PREFER_PREREGISTER,
591 XmDRAG_PREFER_DYNAMIC,
593 XmDRAG_PREFER_RECEIVER
596 /* Operation codes */
604 /* Drop site status */
606 XmNO_DROP_SITE = 0x01,
607 XmDROP_SITE_INVALID = 0x02,
608 XmDROP_SITE_VALID = 0x03
611 /* completion status */
619 /* Static data for MOTIF DND */
620 static GList **motif_target_lists = NULL;
621 static gint motif_n_target_lists = -1;
623 /* Byte swapping routines. The motif specification leaves it
624 * up to us to save a few bytes in the client messages
626 static gchar local_byte_order = '\0';
628 #ifdef G_ENABLE_DEBUG
630 print_target_list (GList *targets)
634 gchar *name = gdk_atom_name (GPOINTER_TO_INT (targets->data));
635 g_message ("\t%s", name);
637 targets = targets->next;
640 #endif /* G_ENABLE_DEBUG */
643 init_byte_order (void)
645 guint32 myint = 0x01020304;
646 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
650 card16_to_host (guint16 x, gchar byte_order) {
651 if (byte_order == local_byte_order)
654 return (x << 8) | (x >> 8);
658 card32_to_host (guint32 x, gchar byte_order) {
659 if (byte_order == local_byte_order)
662 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
666 /***** Dest side ***********/
668 /* Property placed on source windows */
669 typedef struct _MotifDragInitiatorInfo {
671 guint8 protocol_version;
672 guint16 targets_index;
673 guint32 selection_atom;
674 } MotifDragInitiatorInfo;
676 /* Header for target table on the drag window */
677 typedef struct _MotifTargetTableHeader {
679 guchar protocol_version;
682 } MotifTargetTableHeader;
684 /* Property placed on target windows */
685 typedef struct _MotifDragReceiverInfo {
687 guint8 protocol_version;
688 guint8 protocol_style;
690 guint32 proxy_window;
691 guint16 num_drop_sites;
694 } MotifDragReceiverInfo;
696 static Window motif_drag_window = None;
697 static GdkWindow *motif_drag_gdk_window = NULL;
699 static GdkAtom motif_drag_targets_atom = GDK_NONE;
700 static GdkAtom motif_drag_receiver_info_atom = GDK_NONE;
702 /* Target table handling */
705 motif_drag_window_filter (GdkXEvent *xevent,
709 XEvent *xev = (XEvent *)xevent;
711 switch (xev->xany.type)
714 motif_drag_window = None;
715 motif_drag_gdk_window = NULL;
718 if (motif_target_lists &&
719 motif_drag_targets_atom &&
720 (xev->xproperty.atom == motif_drag_targets_atom))
721 motif_read_target_table();
724 return GDK_FILTER_REMOVE;
727 static GdkAtom motif_drag_window_atom = GDK_NONE;
730 motif_lookup_drag_window (Display *display)
732 Window retval = None;
733 gulong bytes_after, nitems;
738 XGetWindowProperty (gdk_display, gdk_root_window, motif_drag_window_atom,
739 0, sizeof(Window)/4, FALSE,
740 XA_WINDOW, &type, &format, &nitems, &bytes_after,
743 if ((format == 8*sizeof(Window)) && (nitems == 1) && (bytes_after == 0))
745 retval = *(Window *)data;
747 g_message ("Found drag window %#lx\n", motif_drag_window));
756 /* Finds the window where global Motif drag information is stored.
757 * If it doesn't exist and 'create' is TRUE, create one.
760 motif_find_drag_window (gboolean create)
762 if (!motif_drag_window)
764 if (!motif_drag_window_atom)
765 motif_drag_window_atom = gdk_atom_intern ("_MOTIF_DRAG_WINDOW", TRUE);
767 motif_drag_window = motif_lookup_drag_window (gdk_display);
769 if (!motif_drag_window && create)
771 /* Create a persistant window. (Copied from LessTif) */
774 XSetWindowAttributes attr;
775 display = XOpenDisplay (NULL);
776 XSetCloseDownMode (display, RetainPermanent);
778 XGrabServer (display);
780 motif_drag_window = motif_lookup_drag_window (display);
782 if (!motif_drag_window)
784 attr.override_redirect = True;
785 attr.event_mask = PropertyChangeMask;
788 XCreateWindow(display, DefaultRootWindow(display),
789 -100, -100, 10, 10, 0, 0,
790 InputOnly, CopyFromParent,
791 (CWOverrideRedirect | CWEventMask), &attr);
794 g_message ("Created drag window %#lx\n", motif_drag_window));
796 XChangeProperty (display, gdk_root_window,
797 motif_drag_window_atom, XA_WINDOW,
799 (guchar *)&motif_drag_window_atom, 1);
802 XUngrabServer (display);
803 XCloseDisplay (display);
806 /* There is a miniscule race condition here if the drag window
807 * gets destroyed exactly now.
809 if (motif_drag_window)
811 motif_drag_gdk_window = gdk_window_foreign_new (motif_drag_window);
812 gdk_window_add_filter (motif_drag_gdk_window,
813 motif_drag_window_filter,
818 return motif_drag_window;
822 motif_read_target_table (void)
824 gulong bytes_after, nitems;
829 if (!motif_drag_targets_atom)
830 motif_drag_targets_atom = gdk_atom_intern ("_MOTIF_DRAG_TARGETS", FALSE);
832 if (motif_target_lists)
834 for (i=0; i<motif_n_target_lists; i++)
835 g_list_free (motif_target_lists[i]);
837 g_free (motif_target_lists);
838 motif_target_lists = NULL;
839 motif_n_target_lists = 0;
842 if (motif_find_drag_window (FALSE))
844 MotifTargetTableHeader *header = NULL;
845 guchar *target_bytes = NULL;
847 gboolean success = FALSE;
849 XGetWindowProperty (gdk_display, motif_drag_window,
850 motif_drag_targets_atom,
851 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
852 motif_drag_targets_atom,
853 &type, &format, &nitems, &bytes_after,
856 if ((format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
859 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
860 header->total_size = card32_to_host (header->total_size, header->byte_order);
862 XGetWindowProperty (gdk_display, motif_drag_window, motif_drag_targets_atom,
863 (sizeof(MotifTargetTableHeader)+3)/4,
864 (header->total_size - sizeof(MotifTargetTableHeader) + 3)/4,
866 motif_drag_targets_atom, &type, &format, &nitems,
867 &bytes_after, &target_bytes);
869 if ((format != 8) || (bytes_after != 0) ||
870 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
873 motif_n_target_lists = header->n_lists;
874 motif_target_lists = g_new0 (GList *, motif_n_target_lists);
877 for (i=0; i<header->n_lists; i++)
882 if (p + sizeof(guint16) - target_bytes > nitems)
885 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
886 targets = (guint32 *)(p + sizeof(guint16));
888 p += sizeof(guint16) + n_targets * sizeof(guint32);
889 if (p - target_bytes > nitems)
892 for (j=0; j<n_targets; j++)
893 motif_target_lists[i] =
894 g_list_prepend (motif_target_lists[i],
895 GUINT_TO_POINTER (card32_to_host (targets[j],
896 header->byte_order)));
898 motif_target_lists[i] = g_list_reverse (motif_target_lists[i]);
908 XFree (target_bytes);
912 if (motif_target_lists)
914 g_free (motif_target_lists);
915 motif_target_lists = NULL;
916 motif_n_target_lists = 0;
918 g_warning ("Error reading Motif target table\n");
924 targets_sort_func (gconstpointer a, gconstpointer b)
926 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
927 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
930 /* Check if given (sorted) list is in the targets table */
932 motif_target_table_check (GList *sorted)
934 GList *tmp_list1, *tmp_list2;
937 for (i=0; i<motif_n_target_lists; i++)
939 tmp_list1 = motif_target_lists[i];
942 while (tmp_list1 && tmp_list2)
944 if (tmp_list1->data != tmp_list2->data)
947 tmp_list1 = tmp_list1->next;
948 tmp_list2 = tmp_list2->next;
950 if (!tmp_list1 && !tmp_list2) /* Found it */
958 motif_add_to_target_table (GList *targets)
960 GList *sorted = NULL;
965 /* make a sorted copy of the list */
969 sorted = g_list_insert_sorted (sorted, targets->data, targets_sort_func);
970 targets = targets->next;
973 /* First check if it is their already */
975 if (motif_target_lists)
976 index = motif_target_table_check (sorted);
978 /* We need to grab the server while doing this, to ensure
984 /* We need to make sure that it exists _before_ we grab the
985 * server, since we can't open a new connection after we
988 motif_find_drag_window (TRUE);
990 XGrabServer(gdk_display);
991 motif_read_target_table();
993 /* Check again, in case it was added in the meantime */
995 if (motif_target_lists)
996 index = motif_target_table_check (sorted);
1000 guint32 total_size = 0;
1005 MotifTargetTableHeader *header;
1007 if (!motif_target_lists)
1009 motif_target_lists = g_new (GList *, 1);
1010 motif_n_target_lists = 1;
1014 motif_n_target_lists++;
1015 motif_target_lists = g_realloc (motif_target_lists,
1016 sizeof(GList *) * motif_n_target_lists);
1018 motif_target_lists[motif_n_target_lists - 1] = sorted;
1020 index = motif_n_target_lists - 1;
1022 total_size = sizeof (MotifTargetTableHeader);
1023 for (i = 0; i < motif_n_target_lists ; i++)
1024 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (motif_target_lists[i]);
1026 data = g_malloc (total_size);
1028 header = (MotifTargetTableHeader *)data;
1029 p = data + sizeof(MotifTargetTableHeader);
1031 header->byte_order = local_byte_order;
1032 header->protocol_version = 0;
1033 header->n_lists = motif_n_target_lists;
1034 header->total_size = total_size;
1036 for (i = 0; i < motif_n_target_lists ; i++)
1041 p += sizeof(guint16);
1043 tmp_list = motif_target_lists[i];
1046 *p32++ = GPOINTER_TO_UINT (tmp_list->data);
1047 tmp_list = tmp_list->next;
1054 XChangeProperty (gdk_display, motif_drag_window,
1055 motif_drag_targets_atom,
1056 motif_drag_targets_atom,
1060 XUngrabServer(gdk_display);
1063 g_list_free (sorted);
1067 /* Translate flags */
1070 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1072 guint recommended_op = flags & 0x000f;
1073 guint possible_ops = (flags & 0x0f0) >> 4;
1075 switch (recommended_op)
1078 context->suggested_action = GDK_ACTION_MOVE;
1081 context->suggested_action = GDK_ACTION_COPY;
1084 context->suggested_action = GDK_ACTION_LINK;
1087 context->suggested_action = GDK_ACTION_COPY;
1091 context->actions = 0;
1092 if (possible_ops & XmDROP_MOVE)
1093 context->actions |= GDK_ACTION_MOVE;
1094 if (possible_ops & XmDROP_COPY)
1095 context->actions |= GDK_ACTION_COPY;
1096 if (possible_ops & XmDROP_LINK)
1097 context->actions |= GDK_ACTION_LINK;
1101 motif_dnd_get_flags (GdkDragContext *context)
1105 switch (context->suggested_action)
1107 case GDK_ACTION_MOVE:
1108 flags = XmDROP_MOVE;
1110 case GDK_ACTION_COPY:
1111 flags = XmDROP_COPY;
1113 case GDK_ACTION_LINK:
1114 flags = XmDROP_LINK;
1117 flags = XmDROP_NOOP;
1121 if (context->actions & GDK_ACTION_MOVE)
1122 flags |= XmDROP_MOVE << 8;
1123 if (context->actions & GDK_ACTION_COPY)
1124 flags |= XmDROP_COPY << 8;
1125 if (context->actions & GDK_ACTION_LINK)
1126 flags |= XmDROP_LINK << 8;
1134 motif_set_targets (GdkDragContext *context)
1136 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1137 MotifDragInitiatorInfo info;
1139 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1141 if (!motif_drag_initiator_info)
1142 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1144 info.byte_order = local_byte_order;
1145 info.protocol_version = 0;
1147 info.targets_index = motif_add_to_target_table (context->targets);
1152 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1154 private->motif_selection = gdk_atom_intern (buf, FALSE);
1155 if (!XGetSelectionOwner (gdk_display, private->motif_selection))
1159 info.selection_atom = private->motif_selection;
1161 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1162 GDK_WINDOW_XWINDOW (context->source_window),
1163 private->motif_selection,
1164 motif_drag_initiator_info, 8, PropModeReplace,
1165 (guchar *)&info, sizeof (info));
1167 private->motif_targets_set = 1;
1171 motif_check_dest (Window win)
1173 gboolean retval = FALSE;
1174 MotifDragReceiverInfo *info;
1177 unsigned long nitems, after;
1179 if (!motif_drag_receiver_info_atom)
1180 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
1182 XGetWindowProperty (gdk_display, win,
1183 motif_drag_receiver_info_atom,
1184 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1185 &type, &format, &nitems, &after,
1190 if ((format == 8) && (nitems == sizeof(*info)))
1192 if ((info->protocol_version == 0) &&
1193 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1194 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1195 (info->protocol_style == XmDRAG_DYNAMIC)))
1201 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1212 motif_send_enter (GdkDragContext *context,
1216 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1218 xev.xclient.type = ClientMessage;
1219 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1220 xev.xclient.format = 8;
1221 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1223 xev.xclient.data.b[0] = XmTOP_LEVEL_ENTER;
1224 xev.xclient.data.b[1] = local_byte_order;
1225 xev.xclient.data.s[1] = 0;
1226 xev.xclient.data.l[1] = time;
1227 xev.xclient.data.l[2] = GDK_WINDOW_XWINDOW (context->source_window);
1229 if (!private->motif_targets_set)
1230 motif_set_targets (context);
1232 xev.xclient.data.l[3] = private->motif_selection;
1234 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1237 g_message ("Send event to %lx failed",
1238 GDK_WINDOW_XWINDOW (context->dest_window)));
1242 motif_send_leave (GdkDragContext *context,
1247 xev.xclient.type = ClientMessage;
1248 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1249 xev.xclient.format = 8;
1250 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1252 xev.xclient.data.b[0] = XmTOP_LEVEL_LEAVE;
1253 xev.xclient.data.b[1] = local_byte_order;
1254 xev.xclient.data.s[1] = 0;
1255 xev.xclient.data.l[1] = time;
1256 xev.xclient.data.l[2] = GDK_WINDOW_XWINDOW (context->source_window);
1257 xev.xclient.data.l[3] = 0;
1259 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1262 g_message ("Send event to %lx failed",
1263 GDK_WINDOW_XWINDOW (context->dest_window)));
1267 motif_send_motion (GdkDragContext *context,
1270 GdkDragAction action,
1275 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1277 xev.xclient.type = ClientMessage;
1278 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1279 xev.xclient.format = 8;
1280 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1282 xev.xclient.data.b[1] = local_byte_order;
1283 xev.xclient.data.s[1] = motif_dnd_get_flags (context);
1284 xev.xclient.data.l[1] = time;
1286 if (context->suggested_action != private->old_action)
1288 xev.xclient.data.b[0] = XmOPERATION_CHANGED;
1290 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1295 xev.xclient.data.b[0] = XmDRAG_MOTION;
1297 xev.xclient.data.s[4] = x_root;
1298 xev.xclient.data.s[5] = y_root;
1300 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1304 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1307 g_message ("Send event to %lx failed",
1308 GDK_WINDOW_XWINDOW (context->dest_window)));
1314 motif_send_drop (GdkDragContext *context, guint32 time)
1317 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1319 xev.xclient.type = ClientMessage;
1320 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1321 xev.xclient.format = 8;
1322 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1324 xev.xclient.data.b[0] = XmDROP_START;
1325 xev.xclient.data.b[1] = local_byte_order;
1326 xev.xclient.data.s[1] = motif_dnd_get_flags (context);
1327 xev.xclient.data.l[1] = time;
1329 xev.xclient.data.s[4] = private->last_x;
1330 xev.xclient.data.s[5] = private->last_y;
1332 xev.xclient.data.l[3] = private->motif_selection;
1333 xev.xclient.data.l[4] = GDK_WINDOW_XWINDOW (context->source_window);
1335 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1338 g_message ("Send event to %lx failed",
1339 GDK_WINDOW_XWINDOW (context->dest_window)));
1345 motif_read_initiator_info (Window source_window,
1351 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1357 MotifDragInitiatorInfo *initiator_info;
1359 if (!motif_drag_initiator_info)
1360 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1362 XGetWindowProperty (gdk_display, source_window, atom,
1363 0, sizeof(*initiator_info), FALSE,
1364 motif_drag_initiator_info,
1365 &type, &format, &nitems, &bytes_after,
1366 (guchar **)&initiator_info);
1368 if ((format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1370 g_warning ("Error reading initiator info\n");
1374 motif_read_target_table ();
1376 if (initiator_info->targets_index >= motif_n_target_lists)
1378 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1379 XFree (initiator_info);
1380 return GDK_FILTER_REMOVE;
1383 tmp_list = g_list_last (motif_target_lists[initiator_info->targets_index]);
1388 *targets = g_list_prepend (*targets,
1390 tmp_list = tmp_list->prev;
1393 #ifdef G_ENABLE_DEBUG
1394 if (gdk_debug_flags & GDK_DEBUG_DND)
1395 print_target_list (*targets);
1396 #endif /* G_ENABLE_DEBUG */
1398 *selection = initiator_info->selection_atom;
1400 XFree (initiator_info);
1405 static GdkDragContext *
1406 motif_drag_context_new (GdkWindow *dest_window,
1408 guint32 source_window,
1411 GdkDragContext *new_context;
1412 GdkDragContextPrivate *private;
1414 /* FIXME, current_dest_drag really shouldn't be NULL'd
1415 * if we error below.
1417 if (current_dest_drag != NULL)
1419 if (timestamp >= current_dest_drag->start_time)
1421 gdk_drag_context_unref (current_dest_drag);
1422 current_dest_drag = NULL;
1428 new_context = gdk_drag_context_new ();
1429 private = (GdkDragContextPrivate *)new_context;
1431 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1432 new_context->is_source = FALSE;
1434 new_context->source_window = gdk_window_lookup (source_window);
1435 if (new_context->source_window)
1436 gdk_window_ref (new_context->source_window);
1439 new_context->source_window = gdk_window_foreign_new (source_window);
1440 if (!new_context->source_window)
1442 gdk_drag_context_unref (new_context);
1447 new_context->dest_window = dest_window;
1448 gdk_window_ref (dest_window);
1449 new_context->start_time = timestamp;
1451 if (!motif_read_initiator_info(source_window,
1453 &new_context->targets,
1454 &private->motif_selection))
1456 gdk_drag_context_unref (new_context);
1464 * The MOTIF drag protocol has no real provisions for distinguishing
1465 * multiple simultaneous drops. If the sources grab the pointer
1466 * when doing drags, that shouldn't happen, in any case. If it
1467 * does, we can't do much except hope for the best.
1470 static GdkFilterReturn
1471 motif_top_level_enter (GdkEvent *event,
1474 guint32 source_window,
1477 GdkDragContext *new_context;
1479 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1480 flags, timestamp, source_window, atom));
1482 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1484 return GDK_FILTER_REMOVE;
1486 event->dnd.type = GDK_DRAG_ENTER;
1487 event->dnd.context = new_context;
1488 gdk_drag_context_ref (new_context);
1490 current_dest_drag = new_context;
1492 return GDK_FILTER_TRANSLATE;
1496 motif_top_level_leave (GdkEvent *event,
1500 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1503 if ((current_dest_drag != NULL) &&
1504 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1505 (timestamp >= current_dest_drag->start_time))
1507 event->dnd.type = GDK_DRAG_LEAVE;
1508 /* Pass ownership of context to the event */
1509 event->dnd.context = current_dest_drag;
1511 current_dest_drag = NULL;
1513 return GDK_FILTER_TRANSLATE;
1516 return GDK_FILTER_REMOVE;
1520 motif_motion (GdkEvent *event,
1526 GdkDragContextPrivate *private;
1528 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1529 flags, timestamp, x_root, y_root));
1531 if ((current_dest_drag != NULL) &&
1532 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1533 (timestamp >= current_dest_drag->start_time))
1535 private = (GdkDragContextPrivate *)current_dest_drag;
1537 event->dnd.type = GDK_DRAG_MOTION;
1538 event->dnd.context = current_dest_drag;
1539 gdk_drag_context_ref (current_dest_drag);
1541 event->dnd.time = timestamp;
1543 motif_dnd_translate_flags (current_dest_drag, flags);
1545 event->dnd.x_root = x_root;
1546 event->dnd.y_root = y_root;
1548 private->last_x = x_root;
1549 private->last_y = y_root;
1551 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1553 return GDK_FILTER_TRANSLATE;
1556 return GDK_FILTER_REMOVE;
1560 motif_operation_changed (GdkEvent *event,
1564 GdkDragContextPrivate *private;
1566 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1569 if ((current_dest_drag != NULL) &&
1570 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1571 (timestamp >= current_dest_drag->start_time))
1573 event->dnd.type = GDK_DRAG_MOTION;
1574 event->dnd.send_event = FALSE;
1575 event->dnd.context = current_dest_drag;
1576 gdk_drag_context_ref (current_dest_drag);
1578 event->dnd.time = timestamp;
1579 private = (GdkDragContextPrivate *)current_dest_drag;
1581 motif_dnd_translate_flags (current_dest_drag, flags);
1583 event->dnd.x_root = private->last_x;
1584 event->dnd.y_root = private->last_y;
1586 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1588 return GDK_FILTER_TRANSLATE;
1591 return GDK_FILTER_REMOVE;
1595 motif_drop_start (GdkEvent *event,
1598 guint32 source_window,
1603 GdkDragContext *new_context;
1606 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1607 flags, timestamp, x_root, y_root, source_window, atom));
1609 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1611 return GDK_FILTER_REMOVE;
1613 motif_dnd_translate_flags (new_context, flags);
1615 event->dnd.type = GDK_DROP_START;
1616 event->dnd.context = new_context;
1617 event->dnd.time = timestamp;
1618 event->dnd.x_root = x_root;
1619 event->dnd.y_root = y_root;
1621 gdk_drag_context_ref (new_context);
1622 current_dest_drag = new_context;
1624 return GDK_FILTER_TRANSLATE;
1628 motif_drag_status (GdkEvent *event,
1632 GdkDragContext *context;
1635 g_message ("Motif status message: flags %x", flags));
1637 context = gdk_drag_context_find (TRUE,
1638 GDK_WINDOW_XWINDOW (event->any.window),
1643 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1644 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1645 private->drag_status = GDK_DRAG_STATUS_DRAG;
1647 event->dnd.type = GDK_DRAG_STATUS;
1648 event->dnd.send_event = FALSE;
1649 event->dnd.context = context;
1650 gdk_drag_context_ref (context);
1652 event->dnd.time = timestamp;
1654 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1656 switch (flags & 0x000f)
1659 context->action = 0;
1662 context->action = GDK_ACTION_MOVE;
1665 context->action = GDK_ACTION_COPY;
1668 context->action = GDK_ACTION_LINK;
1673 context->action = 0;
1675 return GDK_FILTER_TRANSLATE;
1677 return GDK_FILTER_REMOVE;
1681 motif_dnd_filter (GdkXEvent *xev,
1685 XEvent *xevent = (XEvent *)xev;
1686 XClientMessageEvent *xce = &xevent->xclient;
1692 guint32 source_window;
1694 gint16 x_root, y_root;
1697 /* First read some fields common to all Motif DND messages */
1699 reason = xce->data.b[0];
1700 byte_order = xce->data.b[1];
1701 flags = card16_to_host (xce->data.s[1], byte_order);
1702 timestamp = card32_to_host (xce->data.l[1], byte_order);
1704 is_reply = ((reason & 0x80) != 0);
1706 switch (reason & 0x7f)
1708 case XmTOP_LEVEL_ENTER:
1709 source_window = card32_to_host (xce->data.l[2], byte_order);
1710 atom = card32_to_host (xce->data.l[3], byte_order);
1711 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1712 case XmTOP_LEVEL_LEAVE:
1713 return motif_top_level_leave (event, flags, timestamp);
1716 x_root = card16_to_host (xce->data.s[4], byte_order);
1717 y_root = card16_to_host (xce->data.s[5], byte_order);
1720 return motif_motion (event, flags, timestamp, x_root, y_root);
1722 return motif_drag_status (event, flags, timestamp);
1724 case XmDROP_SITE_ENTER:
1725 return motif_drag_status (event, flags, timestamp);
1727 case XmDROP_SITE_LEAVE:
1728 return motif_drag_status (event,
1729 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1732 x_root = card16_to_host (xce->data.s[4], byte_order);
1733 y_root = card16_to_host (xce->data.s[5], byte_order);
1734 atom = card32_to_host (xce->data.l[3], byte_order);
1735 source_window = card32_to_host (xce->data.l[4], byte_order);
1738 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1741 case XmOPERATION_CHANGED:
1743 return motif_operation_changed (event, flags, timestamp);
1745 return motif_drag_status (event, flags, timestamp);
1748 /* To the best of my knowledge, these next two messages are
1749 * not part of the protocol, though they are defined in
1753 case XmDRAG_DROP_FINISH:
1757 return GDK_FILTER_REMOVE;
1760 /*************************************************************
1761 ***************************** XDND **************************
1762 *************************************************************/
1764 /* Utility functions */
1769 GdkDragAction action;
1770 } xdnd_actions_table[] = {
1771 { "XdndActionCopy", GDK_NONE, GDK_ACTION_COPY },
1772 { "XdndActionMove", GDK_NONE, GDK_ACTION_MOVE },
1773 { "XdndActionLink", GDK_NONE, GDK_ACTION_LINK },
1774 { "XdndActionAsk", GDK_NONE, GDK_ACTION_ASK },
1775 { "XdndActionPrivate", GDK_NONE, GDK_ACTION_COPY },
1778 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1779 static gboolean xdnd_actions_initialized = FALSE;
1782 xdnd_initialize_actions (void)
1786 xdnd_actions_initialized = TRUE;
1787 for (i=0; i < xdnd_n_actions; i++)
1788 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1791 static GdkDragAction
1792 xdnd_action_from_atom (GdkAtom atom)
1796 if (!xdnd_actions_initialized)
1797 xdnd_initialize_actions();
1799 for (i=0; i<xdnd_n_actions; i++)
1800 if (atom == xdnd_actions_table[i].atom)
1801 return xdnd_actions_table[i].action;
1807 xdnd_action_to_atom (GdkDragAction action)
1811 if (!xdnd_actions_initialized)
1812 xdnd_initialize_actions();
1814 for (i=0; i<xdnd_n_actions; i++)
1815 if (action == xdnd_actions_table[i].action)
1816 return xdnd_actions_table[i].atom;
1821 static GdkAtom xdnd_aware_atom = GDK_NONE;
1825 static GdkFilterReturn
1826 xdnd_status_filter (GdkXEvent *xev,
1830 XEvent *xevent = (XEvent *)xev;
1831 guint32 dest_window = xevent->xclient.data.l[0];
1832 guint32 flags = xevent->xclient.data.l[1];
1833 GdkAtom action = xevent->xclient.data.l[4];
1834 GdkDragContext *context;
1837 g_message ("XdndStatus: dest_window: %#x action: %ld",
1838 dest_window, action));
1841 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
1844 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1845 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1846 private->drag_status = GDK_DRAG_STATUS_DRAG;
1848 event->dnd.send_event = FALSE;
1849 event->dnd.type = GDK_DRAG_STATUS;
1850 event->dnd.context = context;
1851 gdk_drag_context_ref (context);
1853 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1854 if (!(action != 0) != !(flags & 1))
1857 g_warning ("Received status event with flags not corresponding to action!\n"));
1861 context->action = xdnd_action_from_atom (action);
1863 return GDK_FILTER_TRANSLATE;
1866 return GDK_FILTER_REMOVE;
1869 static GdkFilterReturn
1870 xdnd_finished_filter (GdkXEvent *xev,
1874 XEvent *xevent = (XEvent *)xev;
1875 guint32 dest_window = xevent->xclient.data.l[0];
1876 GdkDragContext *context;
1879 g_message ("XdndFinished: dest_window: %#x", dest_window));
1881 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
1884 event->dnd.type = GDK_DROP_FINISHED;
1885 event->dnd.context = context;
1886 gdk_drag_context_ref (context);
1888 return GDK_FILTER_TRANSLATE;
1891 return GDK_FILTER_REMOVE;
1895 xdnd_set_targets (GdkDragContext *context)
1897 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1899 GList *tmp_list = context->targets;
1901 gint n_targets = g_list_length (context->targets);
1903 typelist = g_new (GdkAtom, n_targets);
1907 typelist[i] = GPOINTER_TO_INT (tmp_list->data);
1908 tmp_list = tmp_list->next;
1912 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1913 GDK_WINDOW_XWINDOW (context->source_window),
1914 gdk_atom_intern ("XdndTypeList", FALSE),
1915 XA_ATOM, 32, PropModeReplace,
1916 (guchar *)typelist, n_targets);
1918 private->xdnd_targets_set = 1;
1922 xdnd_send_enter (GdkDragContext *context)
1925 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1927 xev.xclient.type = ClientMessage;
1928 xev.xclient.message_type = gdk_atom_intern ("XdndEnter", FALSE);
1929 xev.xclient.format = 32;
1930 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1931 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
1932 xev.xclient.data.l[1] = (3 << 24); /* version */
1933 xev.xclient.data.l[2] = 0;
1934 xev.xclient.data.l[3] = 0;
1935 xev.xclient.data.l[4] = 0;
1937 if (!private->xdnd_selection)
1938 private->xdnd_selection = gdk_atom_intern ("XdndSelection", FALSE);
1940 if (g_list_length (context->targets) > 3)
1942 if (!private->xdnd_targets_set)
1943 xdnd_set_targets (context);
1944 xev.xclient.data.l[1] |= 1;
1948 GList *tmp_list = context->targets;
1953 xev.xclient.data.l[i] = GPOINTER_TO_INT (tmp_list->data);
1954 tmp_list = tmp_list->next;
1959 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1963 g_message ("Send event to %lx failed",
1964 GDK_WINDOW_XWINDOW (context->dest_window)));
1965 gdk_window_unref (context->dest_window);
1966 context->dest_window = NULL;
1971 xdnd_send_leave (GdkDragContext *context)
1975 xev.xclient.type = ClientMessage;
1976 xev.xclient.message_type = gdk_atom_intern ("XdndLeave", FALSE);
1977 xev.xclient.format = 32;
1978 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1979 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
1980 xev.xclient.data.l[1] = 0;
1981 xev.xclient.data.l[2] = 0;
1982 xev.xclient.data.l[3] = 0;
1983 xev.xclient.data.l[4] = 0;
1985 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1989 g_message ("Send event to %lx failed",
1990 GDK_WINDOW_XWINDOW (context->dest_window)));
1991 gdk_window_unref (context->dest_window);
1992 context->dest_window = NULL;
1997 xdnd_send_drop (GdkDragContext *context, guint32 time)
2001 xev.xclient.type = ClientMessage;
2002 xev.xclient.message_type = gdk_atom_intern ("XdndDrop", FALSE);
2003 xev.xclient.format = 32;
2004 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2005 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2006 xev.xclient.data.l[1] = 0;
2007 xev.xclient.data.l[2] = time;
2008 xev.xclient.data.l[3] = 0;
2009 xev.xclient.data.l[4] = 0;
2011 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2015 g_message ("Send event to %lx failed",
2016 GDK_WINDOW_XWINDOW (context->dest_window)));
2017 gdk_window_unref (context->dest_window);
2018 context->dest_window = NULL;
2023 xdnd_send_motion (GdkDragContext *context,
2026 GdkDragAction action,
2029 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2032 xev.xclient.type = ClientMessage;
2033 xev.xclient.message_type = gdk_atom_intern ("XdndPosition", FALSE);
2034 xev.xclient.format = 32;
2035 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2036 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2037 xev.xclient.data.l[1] = 0;
2038 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2039 xev.xclient.data.l[3] = time;
2040 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
2042 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2046 g_message ("Send event to %lx failed",
2047 GDK_WINDOW_XWINDOW (context->dest_window)));
2048 gdk_window_unref (context->dest_window);
2049 context->dest_window = NULL;
2051 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2055 xdnd_check_dest (Window win)
2057 gboolean retval = FALSE;
2060 unsigned long nitems, after;
2063 if (!xdnd_aware_atom)
2064 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
2066 XGetWindowProperty (gdk_display, win,
2068 1, False, AnyPropertyType,
2069 &type, &format, &nitems, &after,
2070 (guchar **)&version);
2074 if ((format == 32) && (nitems == 1))
2081 g_warning ("Invalid XdndAware property on window %ld\n", win));
2091 static GdkFilterReturn
2092 xdnd_enter_filter (GdkXEvent *xev,
2096 XEvent *xevent = (XEvent *)xev;
2097 GdkDragContext *new_context;
2100 guint32 source_window = xevent->xclient.data.l[0];
2101 gboolean get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2102 gint version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2105 g_message ("XdndEnter: source_window: %#x, version: %#x",
2106 source_window, version));
2110 /* Old source ignore */
2111 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2112 return GDK_FILTER_REMOVE;
2115 if (current_dest_drag != NULL)
2117 gdk_drag_context_unref (current_dest_drag);
2118 current_dest_drag = NULL;
2121 new_context = gdk_drag_context_new ();
2122 new_context->protocol = GDK_DRAG_PROTO_XDND;
2123 new_context->is_source = FALSE;
2125 new_context->source_window = gdk_window_lookup (source_window);
2126 if (new_context->source_window)
2127 gdk_window_ref (new_context->source_window);
2130 new_context->source_window = gdk_window_foreign_new (source_window);
2131 if (!new_context->source_window)
2133 gdk_drag_context_unref (new_context);
2134 return GDK_FILTER_REMOVE;
2137 new_context->dest_window = event->any.window;
2138 gdk_window_ref (new_context->dest_window);
2140 new_context->targets = NULL;
2145 gulong nitems, after;
2148 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2150 gdk_atom_intern ("XdndTypeList", FALSE), 0, 65536,
2151 False, XA_ATOM, &type, &format, &nitems,
2152 &after, (guchar **)&data);
2154 if ((format != 32) || (type != XA_ATOM))
2156 gdk_drag_context_unref (new_context);
2157 return GDK_FILTER_REMOVE;
2160 for (i=0; i<nitems; i++)
2161 new_context->targets = g_list_append (new_context->targets,
2162 GUINT_TO_POINTER (data[i]));
2169 if (xevent->xclient.data.l[2+i])
2170 new_context->targets = g_list_append (new_context->targets,
2171 GUINT_TO_POINTER (xevent->xclient.data.l[2+i]));
2174 #ifdef G_ENABLE_DEBUG
2175 if (gdk_debug_flags & GDK_DEBUG_DND)
2176 print_target_list (new_context->targets);
2177 #endif /* G_ENABLE_DEBUG */
2179 event->dnd.type = GDK_DRAG_ENTER;
2180 event->dnd.context = new_context;
2181 gdk_drag_context_ref (new_context);
2183 current_dest_drag = new_context;
2184 ((GdkDragContextPrivate *)new_context)->xdnd_selection =
2185 gdk_atom_intern ("XdndSelection", FALSE);
2187 return GDK_FILTER_TRANSLATE;
2190 static GdkFilterReturn
2191 xdnd_leave_filter (GdkXEvent *xev,
2195 XEvent *xevent = (XEvent *)xev;
2196 guint32 source_window = xevent->xclient.data.l[0];
2199 g_message ("XdndLeave: source_window: %#x",
2202 if ((current_dest_drag != NULL) &&
2203 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2204 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2206 event->dnd.type = GDK_DRAG_LEAVE;
2207 /* Pass ownership of context to the event */
2208 event->dnd.context = current_dest_drag;
2210 current_dest_drag = NULL;
2212 return GDK_FILTER_TRANSLATE;
2215 return GDK_FILTER_REMOVE;
2218 static GdkFilterReturn
2219 xdnd_position_filter (GdkXEvent *xev,
2223 XEvent *xevent = (XEvent *)xev;
2224 guint32 source_window = xevent->xclient.data.l[0];
2225 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2226 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2227 guint32 time = xevent->xclient.data.l[3];
2228 GdkAtom action = xevent->xclient.data.l[4];
2231 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2232 source_window, x_root, y_root, time, action));
2235 if ((current_dest_drag != NULL) &&
2236 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2237 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2239 event->dnd.type = GDK_DRAG_MOTION;
2240 event->dnd.context = current_dest_drag;
2241 gdk_drag_context_ref (current_dest_drag);
2243 event->dnd.time = time;
2245 current_dest_drag->suggested_action = xdnd_action_from_atom (action);
2247 event->dnd.x_root = x_root;
2248 event->dnd.y_root = y_root;
2250 ((GdkDragContextPrivate *)current_dest_drag)->last_x = x_root;
2251 ((GdkDragContextPrivate *)current_dest_drag)->last_y = y_root;
2253 return GDK_FILTER_TRANSLATE;
2256 return GDK_FILTER_REMOVE;
2259 static GdkFilterReturn
2260 xdnd_drop_filter (GdkXEvent *xev,
2264 XEvent *xevent = (XEvent *)xev;
2265 guint32 source_window = xevent->xclient.data.l[0];
2266 guint32 time = xevent->xclient.data.l[2];
2269 g_message ("XdndDrop: source_window: %#x time: %d",
2270 source_window, time));
2272 if ((current_dest_drag != NULL) &&
2273 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2274 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2276 GdkDragContextPrivate *private;
2277 private = (GdkDragContextPrivate *)current_dest_drag;
2279 event->dnd.type = GDK_DROP_START;
2281 event->dnd.context = current_dest_drag;
2282 gdk_drag_context_ref (current_dest_drag);
2284 event->dnd.time = time;
2285 event->dnd.x_root = private->last_x;
2286 event->dnd.y_root = private->last_y;
2288 return GDK_FILTER_TRANSLATE;
2291 return GDK_FILTER_REMOVE;
2294 /*************************************************************
2295 ************************** Public API ***********************
2296 *************************************************************/
2303 gdk_add_client_message_filter (
2304 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2305 motif_dnd_filter, NULL);
2306 gdk_add_client_message_filter (
2307 gdk_atom_intern ("XdndEnter", FALSE),
2308 xdnd_enter_filter, NULL);
2309 gdk_add_client_message_filter (
2310 gdk_atom_intern ("XdndLeave", FALSE),
2311 xdnd_leave_filter, NULL);
2312 gdk_add_client_message_filter (
2313 gdk_atom_intern ("XdndPosition", FALSE),
2314 xdnd_position_filter, NULL);
2315 gdk_add_client_message_filter (
2316 gdk_atom_intern ("XdndStatus", FALSE),
2317 xdnd_status_filter, NULL);
2318 gdk_add_client_message_filter (
2319 gdk_atom_intern ("XdndFinished", FALSE),
2320 xdnd_finished_filter, NULL);
2321 gdk_add_client_message_filter (
2322 gdk_atom_intern ("XdndDrop", FALSE),
2323 xdnd_drop_filter, NULL);
2329 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2331 if (context->dest_window)
2333 switch (context->protocol)
2335 case GDK_DRAG_PROTO_MOTIF:
2336 motif_send_leave (context, time);
2338 case GDK_DRAG_PROTO_XDND:
2339 xdnd_send_leave (context);
2341 case GDK_DRAG_PROTO_ROOTWIN:
2345 gdk_window_unref (context->dest_window);
2346 context->dest_window = NULL;
2351 gdk_drag_begin (GdkWindow *window,
2353 GdkDragAction actions)
2356 GdkDragContext *new_context;
2358 g_return_val_if_fail (window != NULL, NULL);
2360 new_context = gdk_drag_context_new ();
2361 new_context->is_source = TRUE;
2362 new_context->source_window = window;
2363 gdk_window_ref (window);
2365 tmp_list = g_list_last (targets);
2366 new_context->targets = NULL;
2369 new_context->targets = g_list_prepend (new_context->targets,
2371 tmp_list = tmp_list->prev;
2374 new_context->actions = actions;
2380 gdk_drag_get_protocol (guint32 xid,
2381 GdkDragProtocol *protocol)
2383 if (xdnd_check_dest (xid))
2385 *protocol = GDK_DRAG_PROTO_XDND;
2386 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2389 else if (motif_check_dest (xid))
2391 *protocol = GDK_DRAG_PROTO_MOTIF;
2392 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2397 /* Check if this is a root window */
2399 gboolean rootwin = FALSE;
2400 gint old_warnings = gdk_error_warnings;
2403 unsigned long nitems, after;
2404 unsigned char *data;
2406 if (xid == gdk_root_window)
2413 XGetWindowProperty (gdk_display, xid,
2414 gdk_atom_intern ("ENLIGHTENMENT_DESKTOP", FALSE),
2415 0, 0, False, AnyPropertyType,
2416 &type, &format, &nitems, &after, &data);
2417 if ((gdk_error_code == 0) && type != None)
2424 /* Until I find out what window manager the next one is for,
2425 * I'm leaving it commented out. It's supported in the
2426 * xscreensaver sources, though.
2433 XGetWindowProperty (gdk_display, win,
2434 gdk_atom_intern ("__SWM_VROOT", FALSE),
2435 0, 0, False, AnyPropertyType,
2436 &type, &format, &nitems, &data);
2437 if ((gdk_error_code == 0) && type != None)
2442 gdk_error_warnings = old_warnings;
2446 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2455 gdk_drag_find_window (GdkDragContext *context,
2456 GdkWindow *drag_window,
2459 GdkWindow **dest_window,
2460 GdkDragProtocol *protocol)
2462 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2465 if (!private->window_cache)
2466 private->window_cache = gdk_window_cache_new();
2468 dest = get_client_window_at_coords (private->window_cache,
2470 GDK_WINDOW_XWINDOW (drag_window) : GDK_NONE,
2473 if (private->dest_xid != dest)
2475 private->dest_xid = dest;
2477 /* Check if new destination accepts drags, and which protocol */
2479 if (gdk_drag_get_protocol (dest, protocol))
2481 *dest_window = gdk_window_lookup (dest);
2483 gdk_window_ref (*dest_window);
2485 *dest_window = gdk_window_foreign_new (dest);
2488 *dest_window = NULL;
2492 *dest_window = context->dest_window;
2494 gdk_window_ref (*dest_window);
2495 *protocol = context->protocol;
2500 gdk_drag_motion (GdkDragContext *context,
2501 GdkWindow *dest_window,
2502 GdkDragProtocol protocol,
2505 GdkDragAction action,
2508 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2510 if (context->dest_window != dest_window)
2512 GdkEvent temp_event;
2514 /* Send a leave to the last destination */
2515 gdk_drag_do_leave (context, time);
2516 private->drag_status = GDK_DRAG_STATUS_DRAG;
2518 /* Check if new destination accepts drags, and which protocol */
2522 context->dest_window = dest_window;
2523 gdk_window_ref (context->dest_window);
2524 context->protocol = protocol;
2528 case GDK_DRAG_PROTO_MOTIF:
2529 motif_send_enter (context, time);
2532 case GDK_DRAG_PROTO_XDND:
2533 xdnd_send_enter (context);
2536 case GDK_DRAG_PROTO_ROOTWIN:
2539 private->old_action = action;
2540 context->suggested_action = action;
2544 context->dest_window = NULL;
2545 context->action = 0;
2548 /* Push a status event, to let the client know that
2552 temp_event.dnd.type = GDK_DRAG_STATUS;
2553 temp_event.dnd.window = context->source_window;
2554 /* We use this to signal a synthetic status. Perhaps
2555 * we should use an extra field...
2557 temp_event.dnd.send_event = TRUE;
2559 temp_event.dnd.context = context;
2560 temp_event.dnd.time = time;
2562 gdk_event_put (&temp_event);
2566 private->old_action = context->suggested_action;
2567 context->suggested_action = action;
2570 /* Send a drag-motion event */
2572 private->last_x = x_root;
2573 private->last_y = y_root;
2575 if (context->dest_window)
2577 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
2579 switch (context->protocol)
2581 case GDK_DRAG_PROTO_MOTIF:
2582 motif_send_motion (context, x_root, y_root, action, time);
2585 case GDK_DRAG_PROTO_XDND:
2586 xdnd_send_motion (context, x_root, y_root, action, time);
2589 case GDK_DRAG_PROTO_ROOTWIN:
2591 GdkEvent temp_event;
2593 if (g_list_find (context->targets,
2594 GUINT_TO_POINTER (gdk_atom_intern ("application/x-rootwin-drop", FALSE))))
2595 context->action = context->suggested_action;
2597 context->action = 0;
2599 temp_event.dnd.type = GDK_DRAG_STATUS;
2600 temp_event.dnd.window = context->source_window;
2601 temp_event.dnd.send_event = FALSE;
2602 temp_event.dnd.context = context;
2603 temp_event.dnd.time = time;
2605 gdk_event_put (&temp_event);
2618 gdk_drag_drop (GdkDragContext *context,
2621 if (context->dest_window)
2623 switch (context->protocol)
2625 case GDK_DRAG_PROTO_MOTIF:
2626 motif_send_leave (context, time);
2627 motif_send_drop (context, time);
2630 case GDK_DRAG_PROTO_XDND:
2631 xdnd_send_drop (context, time);
2634 case GDK_DRAG_PROTO_ROOTWIN:
2635 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
2642 gdk_drag_abort (GdkDragContext *context,
2645 gdk_drag_do_leave (context, time);
2648 /* Destination side */
2651 gdk_drag_status (GdkDragContext *context,
2652 GdkDragAction action,
2655 GdkDragContextPrivate *private;
2658 g_return_if_fail (context != 0);
2660 private = (GdkDragContextPrivate *)context;
2662 context->action = action;
2664 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
2666 xev.xclient.type = ClientMessage;
2667 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
2668 xev.xclient.format = 8;
2669 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
2671 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
2673 xev.xclient.data.b[0] = XmOPERATION_CHANGED | 0x80;
2677 if ((action != 0) != (private->old_action != 0))
2680 xev.xclient.data.b[0] = XmDROP_SITE_ENTER | 0x80;
2682 xev.xclient.data.b[0] = XmDROP_SITE_LEAVE | 0x80;
2685 xev.xclient.data.b[0] = XmDRAG_MOTION | 0x80;
2688 xev.xclient.data.b[1] = local_byte_order;
2692 case GDK_ACTION_MOVE:
2693 xev.xclient.data.s[1] = XmDROP_MOVE;
2695 case GDK_ACTION_COPY:
2696 xev.xclient.data.s[1] = XmDROP_COPY;
2698 case GDK_ACTION_LINK:
2699 xev.xclient.data.s[1] = XmDROP_LINK;
2702 xev.xclient.data.s[1] = XmDROP_NOOP;
2707 xev.xclient.data.s[1] |= (XmDROP_SITE_VALID << 4);
2709 xev.xclient.data.s[1] |= (XmNO_DROP_SITE << 4);
2711 xev.xclient.data.l[1] = time;
2712 xev.xclient.data.s[4] = private->last_x;
2713 xev.xclient.data.s[5] = private->last_y;
2715 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
2718 g_message ("Send event to %lx failed",
2719 GDK_WINDOW_XWINDOW (context->source_window)));
2721 else if (context->protocol == GDK_DRAG_PROTO_XDND)
2723 xev.xclient.type = ClientMessage;
2724 xev.xclient.message_type = gdk_atom_intern ("XdndStatus", FALSE);
2725 xev.xclient.format = 32;
2726 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
2728 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->dest_window);
2729 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
2730 xev.xclient.data.l[2] = 0;
2731 xev.xclient.data.l[3] = 0;
2732 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
2734 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
2737 g_message ("Send event to %lx failed",
2738 GDK_WINDOW_XWINDOW (context->source_window)));
2741 private->old_action = action;
2745 gdk_drop_reply (GdkDragContext *context,
2749 GdkDragContextPrivate *private;
2751 g_return_if_fail (context != 0);
2753 private = (GdkDragContextPrivate *)context;
2755 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
2759 xev.xclient.type = ClientMessage;
2760 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
2761 xev.xclient.format = 8;
2763 xev.xclient.data.b[0] = XmDROP_START | 0x80;
2764 xev.xclient.data.b[1] = local_byte_order;
2766 xev.xclient.data.s[2] = XmDROP_COPY |
2767 (XmDROP_SITE_VALID << 4) |
2768 (XmDROP_NOOP << 8) |
2771 xev.xclient.data.s[2] = XmDROP_NOOP |
2772 (XmNO_DROP_SITE << 4) |
2773 (XmDROP_NOOP << 8) |
2774 (XmDROP_CANCEL << 12);
2775 xev.xclient.data.s[2] = private->last_x;
2776 xev.xclient.data.s[3] = private->last_y;
2778 gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
2784 gdk_drop_finish (GdkDragContext *context,
2788 if (context->protocol == GDK_DRAG_PROTO_XDND)
2792 xev.xclient.type = ClientMessage;
2793 xev.xclient.message_type = gdk_atom_intern ("XdndFinished", FALSE);
2794 xev.xclient.format = 32;
2795 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
2797 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->dest_window);
2798 xev.xclient.data.l[1] = 0;
2799 xev.xclient.data.l[2] = 0;
2800 xev.xclient.data.l[3] = 0;
2801 xev.xclient.data.l[4] = 0;
2803 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
2806 g_message ("Send event to %lx failed",
2807 GDK_WINDOW_XWINDOW (context->source_window)));
2813 gdk_window_register_dnd (GdkWindow *window)
2815 static guint32 xdnd_version = 3;
2817 MotifDragReceiverInfo info;
2819 /* Set Motif drag receiver information property */
2821 if (!motif_drag_receiver_info_atom)
2822 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
2824 info.byte_order = local_byte_order;
2825 info.protocol_version = 0;
2826 info.protocol_style = XmDRAG_DYNAMIC;
2827 info.proxy_window = GDK_NONE;
2828 info.num_drop_sites = 0;
2829 info.total_size = sizeof(info);
2831 XChangeProperty (gdk_display, GDK_WINDOW_XWINDOW (window),
2832 motif_drag_receiver_info_atom,
2833 motif_drag_receiver_info_atom,
2840 if (!xdnd_aware_atom)
2841 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
2843 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
2844 XChangeProperty (GDK_WINDOW_XDISPLAY (window),
2845 GDK_WINDOW_XWINDOW (window),
2846 xdnd_aware_atom, XA_ATOM,
2847 32, PropModeReplace,
2848 (guchar *)&xdnd_version,
2849 sizeof (xdnd_version));
2852 /*************************************************************
2853 * gdk_drag_get_selection:
2854 * Returns the selection atom for the current source window
2858 *************************************************************/
2861 gdk_drag_get_selection (GdkDragContext *context)
2863 g_return_val_if_fail (context != NULL, GDK_NONE);
2865 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
2866 return ((GdkDragContextPrivate *)context)->motif_selection;
2867 else if (context->protocol == GDK_DRAG_PROTO_XDND)
2868 return ((GdkDragContextPrivate *)context)->xdnd_selection;