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 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.
21 * Modified by the GTK+ Team and others 1997-1999. 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/.
28 #include <X11/Xatom.h>
31 #include "gdk.h" /* For gdk_flush() */
33 #include "gdkproperty.h"
34 #include "gdkprivate.h"
37 typedef struct _GdkDragContextPrivate GdkDragContextPrivate;
41 GDK_DRAG_STATUS_MOTION_WAIT,
42 GDK_DRAG_STATUS_ACTION_WAIT,
48 gint x, y, width, height;
54 GHashTable *child_hash;
58 /* Structure that holds information about a drag in progress.
59 * this is used on both source and destination sides.
61 struct _GdkDragContextPrivate {
62 GdkDragContext context;
64 GdkAtom motif_selection;
65 GdkAtom xdnd_selection;
68 guint16 last_x; /* Coordinates from last event */
70 GdkDragAction old_action; /* The last action we sent to the source */
71 GdkDragAction old_actions; /* The last actions we sent to the source */
72 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
74 Window dest_xid; /* The last window we looked up */
75 Window drop_xid; /* The (non-proxied) window that is receiving drops */
76 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
77 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
78 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
79 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
80 guint drag_status : 4; /* current status of drag */
82 GdkWindowCache *window_cache;
85 GdkDragContext *current_dest_drag = NULL;
87 /* Forward declarations */
89 static void gdk_window_cache_destroy (GdkWindowCache *cache);
91 static void motif_read_target_table (void);
92 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
96 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
100 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
104 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
108 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
112 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
116 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
120 static void xdnd_manage_source_filter (GdkDragContext *context,
122 gboolean add_filter);
126 static GList *contexts;
129 gdk_drag_context_new (void)
131 GdkDragContextPrivate *result;
133 result = g_new0 (GdkDragContextPrivate, 1);
135 result->ref_count = 1;
137 contexts = g_list_prepend (contexts, result);
139 return (GdkDragContext *)result;
143 gdk_drag_context_ref (GdkDragContext *context)
145 g_return_if_fail (context != NULL);
147 ((GdkDragContextPrivate *)context)->ref_count++;
151 gdk_drag_context_unref (GdkDragContext *context)
153 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
155 g_return_if_fail (context != NULL);
156 g_return_if_fail (private->ref_count > 0);
158 private->ref_count--;
160 if (private->ref_count == 0)
162 g_dataset_destroy (private);
164 g_list_free (context->targets);
166 if (context->source_window)
168 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
170 xdnd_manage_source_filter (context, context->source_window, FALSE);
172 gdk_window_unref (context->source_window);
175 if (context->dest_window)
176 gdk_window_unref (context->dest_window);
178 if (private->window_cache)
179 gdk_window_cache_destroy (private->window_cache);
181 contexts = g_list_remove (contexts, private);
186 static GdkDragContext *
187 gdk_drag_context_find (gboolean is_source,
191 GList *tmp_list = contexts;
192 GdkDragContext *context;
193 GdkDragContextPrivate *private;
194 Window context_dest_xid;
198 context = (GdkDragContext *)tmp_list->data;
199 private = (GdkDragContextPrivate *)context;
201 context_dest_xid = context->dest_window ?
204 GDK_DRAWABLE_XID (context->dest_window)) :
207 if ((!context->is_source == !is_source) &&
208 ((source_xid == None) || (context->source_window &&
209 (GDK_DRAWABLE_XID (context->source_window) == source_xid))) &&
210 ((dest_xid == None) || (context_dest_xid == dest_xid)))
213 tmp_list = tmp_list->next;
219 /* Utility functions */
222 gdk_window_cache_add (GdkWindowCache *cache,
224 gint x, gint y, gint width, gint height,
227 GdkCacheChild *child = g_new (GdkCacheChild, 1);
232 child->width = width;
233 child->height = height;
234 child->mapped = mapped;
236 cache->children = g_list_prepend (cache->children, child);
237 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
241 static GdkFilterReturn
242 gdk_window_cache_filter (GdkXEvent *xev,
246 XEvent *xevent = (XEvent *)xev;
247 GdkWindowCache *cache = data;
249 switch (xevent->type)
251 case CirculateNotify:
253 case ConfigureNotify:
255 XConfigureEvent *xce = &xevent->xconfigure;
258 node = g_hash_table_lookup (cache->child_hash,
259 GUINT_TO_POINTER (xce->window));
262 GdkCacheChild *child = node->data;
265 child->width = xce->width;
266 child->height = xce->height;
267 if (xce->above == None && (node->next))
269 GList *last = g_list_last (cache->children);
270 cache->children = g_list_remove_link (cache->children, node);
277 GList *above_node = g_hash_table_lookup (cache->child_hash,
278 GUINT_TO_POINTER (xce->above));
279 if (above_node && node->prev != above_node)
281 cache->children = g_list_remove_link (cache->children, node);
282 node->next = above_node->next;
284 node->next->prev = node;
285 node->prev = above_node;
286 above_node->next = node;
294 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
296 if (!g_hash_table_lookup (cache->child_hash,
297 GUINT_TO_POINTER (xcwe->window)))
298 gdk_window_cache_add (cache, xcwe->window,
299 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
305 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
308 node = g_hash_table_lookup (cache->child_hash,
309 GUINT_TO_POINTER (xdwe->window));
312 g_hash_table_remove (cache->child_hash,
313 GUINT_TO_POINTER (xdwe->window));
314 cache->children = g_list_remove_link (cache->children, node);
316 g_list_free_1 (node);
322 XMapEvent *xme = &xevent->xmap;
325 node = g_hash_table_lookup (cache->child_hash,
326 GUINT_TO_POINTER (xme->window));
329 GdkCacheChild *child = node->data;
330 child->mapped = TRUE;
338 XMapEvent *xume = &xevent->xmap;
341 node = g_hash_table_lookup (cache->child_hash,
342 GUINT_TO_POINTER (xume->window));
345 GdkCacheChild *child = node->data;
346 child->mapped = FALSE;
351 return GDK_FILTER_CONTINUE;
353 return GDK_FILTER_REMOVE;
356 static GdkWindowCache *
357 gdk_window_cache_new (void)
359 XWindowAttributes xwa;
360 Window root, parent, *children;
361 unsigned int nchildren;
364 gint old_warnings = gdk_error_warnings;
366 GdkWindowCache *result = g_new (GdkWindowCache, 1);
368 result->children = NULL;
369 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
371 XGetWindowAttributes (gdk_display, gdk_root_window, &xwa);
372 result->old_event_mask = xwa.your_event_mask;
373 XSelectInput (gdk_display, gdk_root_window,
374 result->old_event_mask | SubstructureNotifyMask);
375 gdk_window_add_filter (gdk_parent_root,
376 gdk_window_cache_filter, result);
379 gdk_error_warnings = 0;
381 if (XQueryTree(gdk_display, gdk_root_window,
382 &root, &parent, &children, &nchildren) == 0)
385 for (i = 0; i < nchildren ; i++)
387 XGetWindowAttributes (gdk_display, children[i], &xwa);
389 gdk_window_cache_add (result, children[i],
390 xwa.x, xwa.y, xwa.width, xwa.height,
391 xwa.map_state != IsUnmapped);
397 gdk_window_cache_add (result, children[i],
398 xwa.x, xwa.y, xwa.width, xwa.height,
399 (xwa.map_state != IsUnmapped));
405 gdk_error_warnings = old_warnings;
411 gdk_window_cache_destroy (GdkWindowCache *cache)
413 XSelectInput (gdk_display, gdk_root_window, cache->old_event_mask);
414 gdk_window_remove_filter (gdk_parent_root,
415 gdk_window_cache_filter, cache);
417 g_list_foreach (cache->children, (GFunc)g_free, NULL);
418 g_list_free (cache->children);
419 g_hash_table_destroy (cache->child_hash);
425 get_client_window_at_coords_recurse (Window win,
429 Window root, tmp_parent, *children;
430 unsigned int nchildren;
435 unsigned long nitems, after;
438 static Atom wm_state_atom = None;
441 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
443 XGetWindowProperty (gdk_display, win,
444 wm_state_atom, 0, 0, False, AnyPropertyType,
445 &type, &format, &nitems, &after, &data);
461 /* This is beautiful! Damn Enlightenment and click-to-focus */
462 XTranslateCoordinates (gdk_display, gdk_root_window, win,
463 x_root, y_root, &dest_x, &dest_y, &child);
473 if (XQueryTree(gdk_display, win,
474 &root, &tmp_parent, &children, &nchildren) == 0)
479 for (i = nchildren - 1; (i >= 0) && (child == None); i--)
481 XWindowAttributes xwa;
483 XGetWindowAttributes (gdk_display, children[i], &xwa);
487 else if ((xwa.map_state == IsViewable) && (xwa.class == InputOutput) &&
488 (x >= xwa.x) && (x < xwa.x + (gint)xwa.width) &&
489 (y >= xwa.y) && (y < xwa.y + (gint)xwa.height))
504 return get_client_window_at_coords_recurse (child, x, y);
510 get_client_window_at_coords (GdkWindowCache *cache,
516 Window retval = None;
518 gint old_warnings = gdk_error_warnings;
521 gdk_error_warnings = 0;
523 tmp_list = cache->children;
525 while (tmp_list && !retval)
527 GdkCacheChild *child = tmp_list->data;
529 if ((child->xid != ignore) && (child->mapped))
531 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
532 (y_root >= child->y) && (y_root < child->y + child->height))
534 retval = get_client_window_at_coords_recurse (child->xid,
542 tmp_list = tmp_list->next;
545 gdk_error_warnings = old_warnings;
549 return gdk_root_window;
554 get_client_window_at_coords_recurse (Window win,
561 unsigned long nitems, after;
565 static Atom wm_state_atom = None;
568 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
570 XGetWindowProperty (gdk_display, win,
571 wm_state_atom, 0, 0, False, AnyPropertyType,
572 &type, &format, &nitems, &after, &data);
587 XTranslateCoordinates (gdk_display, gdk_root_window, win,
588 x_root, y_root, &dest_x, &dest_y, &child);
598 return get_client_window_at_coords_recurse (child, x_root, y_root);
604 get_client_window_at_coords (Window ignore,
608 Window root, parent, *children;
609 unsigned int nchildren;
611 Window retval = None;
613 gint old_warnings = gdk_error_warnings;
616 gdk_error_warnings = 0;
618 if (XQueryTree(gdk_display, gdk_root_window,
619 &root, &parent, &children, &nchildren) == 0)
622 for (i = nchildren - 1; (i >= 0) && (retval == None); i--)
624 if (children[i] != ignore)
626 XWindowAttributes xwa;
628 XGetWindowAttributes (gdk_display, children[i], &xwa);
632 else if ((xwa.map_state == IsViewable) &&
633 (x_root >= xwa.x) && (x_root < xwa.x + (gint)xwa.width) &&
634 (y_root >= xwa.y) && (y_root < xwa.y + (gint)xwa.height))
636 retval = get_client_window_at_coords_recurse (children[i],
639 retval = children[i];
646 gdk_error_warnings = old_warnings;
650 return gdk_root_window;
654 /*************************************************************
655 ***************************** MOTIF *************************
656 *************************************************************/
658 /* values used in the message type for Motif DND */
671 /* Values used to specify type of protocol to use */
675 XmDRAG_PREFER_PREREGISTER,
677 XmDRAG_PREFER_DYNAMIC,
679 XmDRAG_PREFER_RECEIVER
682 /* Operation codes */
690 /* Drop site status */
692 XmNO_DROP_SITE = 0x01,
693 XmDROP_SITE_INVALID = 0x02,
694 XmDROP_SITE_VALID = 0x03
697 /* completion status */
705 /* Static data for MOTIF DND */
706 static GList **motif_target_lists = NULL;
707 static gint motif_n_target_lists = -1;
709 /* Byte swapping routines. The motif specification leaves it
710 * up to us to save a few bytes in the client messages
712 static gchar local_byte_order = '\0';
714 #ifdef G_ENABLE_DEBUG
716 print_target_list (GList *targets)
720 gchar *name = gdk_atom_name (GPOINTER_TO_INT (targets->data));
721 g_message ("\t%s", name);
723 targets = targets->next;
726 #endif /* G_ENABLE_DEBUG */
729 init_byte_order (void)
731 guint32 myint = 0x01020304;
732 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
736 card16_to_host (guint16 x, gchar byte_order) {
737 if (byte_order == local_byte_order)
740 return (x << 8) | (x >> 8);
744 card32_to_host (guint32 x, gchar byte_order) {
745 if (byte_order == local_byte_order)
748 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
751 /* Motif packs together fields of varying length into the
752 * client message. We can't rely on accessing these
753 * through data.s[], data.l[], etc, because on some architectures
754 * (i.e., Alpha) these won't be valid for format == 8.
757 #define MOTIF_XCLIENT_BYTE(xevent,i) \
758 (xevent)->xclient.data.b[i]
759 #define MOTIF_XCLIENT_SHORT(xevent,i) \
760 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
761 #define MOTIF_XCLIENT_LONG(xevent,i) \
762 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
764 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
765 #define MOTIF_UNPACK_SHORT(xevent,i) \
766 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
767 #define MOTIF_UNPACK_LONG(xevent,i) \
768 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
770 /***** Dest side ***********/
772 /* Property placed on source windows */
773 typedef struct _MotifDragInitiatorInfo {
775 guint8 protocol_version;
776 guint16 targets_index;
777 guint32 selection_atom;
778 } MotifDragInitiatorInfo;
780 /* Header for target table on the drag window */
781 typedef struct _MotifTargetTableHeader {
783 guchar protocol_version;
786 } MotifTargetTableHeader;
788 /* Property placed on target windows */
789 typedef struct _MotifDragReceiverInfo {
791 guint8 protocol_version;
792 guint8 protocol_style;
794 guint32 proxy_window;
795 guint16 num_drop_sites;
798 } MotifDragReceiverInfo;
800 static Window motif_drag_window = None;
801 static GdkWindow *motif_drag_gdk_window = NULL;
803 static GdkAtom motif_drag_targets_atom = GDK_NONE;
804 static GdkAtom motif_drag_receiver_info_atom = GDK_NONE;
806 /* Target table handling */
809 motif_drag_window_filter (GdkXEvent *xevent,
813 XEvent *xev = (XEvent *)xevent;
815 switch (xev->xany.type)
818 motif_drag_window = None;
819 motif_drag_gdk_window = NULL;
822 if (motif_target_lists &&
823 motif_drag_targets_atom &&
824 (xev->xproperty.atom == motif_drag_targets_atom))
825 motif_read_target_table();
828 return GDK_FILTER_REMOVE;
831 static Atom motif_drag_window_atom = GDK_NONE;
834 motif_lookup_drag_window (Display *display)
836 Window retval = None;
837 gulong bytes_after, nitems;
842 XGetWindowProperty (gdk_display, gdk_root_window, motif_drag_window_atom,
844 XA_WINDOW, &type, &format, &nitems, &bytes_after,
847 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
849 retval = *(Window *)data;
851 g_message ("Found drag window %#lx\n", motif_drag_window));
860 /* Finds the window where global Motif drag information is stored.
861 * If it doesn't exist and 'create' is TRUE, create one.
864 motif_find_drag_window (gboolean create)
866 if (!motif_drag_window)
868 if (!motif_drag_window_atom)
869 motif_drag_window_atom = gdk_atom_intern ("_MOTIF_DRAG_WINDOW", TRUE);
871 motif_drag_window = motif_lookup_drag_window (gdk_display);
873 if (!motif_drag_window && create)
875 /* Create a persistant window. (Copied from LessTif) */
878 XSetWindowAttributes attr;
879 display = XOpenDisplay (gdk_display_name);
880 XSetCloseDownMode (display, RetainPermanent);
882 XGrabServer (display);
884 motif_drag_window = motif_lookup_drag_window (display);
886 if (!motif_drag_window)
888 attr.override_redirect = True;
889 attr.event_mask = PropertyChangeMask;
892 XCreateWindow(display, DefaultRootWindow(display),
893 -100, -100, 10, 10, 0, 0,
894 InputOnly, CopyFromParent,
895 (CWOverrideRedirect | CWEventMask), &attr);
898 g_message ("Created drag window %#lx\n", motif_drag_window));
900 XChangeProperty (display, gdk_root_window,
901 motif_drag_window_atom, XA_WINDOW,
903 (guchar *)&motif_drag_window_atom, 1);
906 XUngrabServer (display);
907 XCloseDisplay (display);
910 /* There is a miniscule race condition here if the drag window
911 * gets destroyed exactly now.
913 if (motif_drag_window)
915 motif_drag_gdk_window = gdk_window_foreign_new (motif_drag_window);
916 gdk_window_add_filter (motif_drag_gdk_window,
917 motif_drag_window_filter,
922 return motif_drag_window;
926 motif_read_target_table (void)
928 gulong bytes_after, nitems;
933 if (!motif_drag_targets_atom)
934 motif_drag_targets_atom = gdk_atom_intern ("_MOTIF_DRAG_TARGETS", FALSE);
936 if (motif_target_lists)
938 for (i=0; i<motif_n_target_lists; i++)
939 g_list_free (motif_target_lists[i]);
941 g_free (motif_target_lists);
942 motif_target_lists = NULL;
943 motif_n_target_lists = 0;
946 if (motif_find_drag_window (FALSE))
948 MotifTargetTableHeader *header = NULL;
949 guchar *target_bytes = NULL;
951 gboolean success = FALSE;
953 gdk_error_trap_push ();
954 XGetWindowProperty (gdk_display, motif_drag_window,
955 motif_drag_targets_atom,
956 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
957 motif_drag_targets_atom,
958 &type, &format, &nitems, &bytes_after,
961 if (gdk_error_trap_pop () || (format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
964 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
965 header->total_size = card32_to_host (header->total_size, header->byte_order);
967 gdk_error_trap_push ();
968 XGetWindowProperty (gdk_display, motif_drag_window, motif_drag_targets_atom,
969 (sizeof(MotifTargetTableHeader)+3)/4,
970 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
972 motif_drag_targets_atom, &type, &format, &nitems,
973 &bytes_after, &target_bytes);
975 if (gdk_error_trap_pop () || (format != 8) || (bytes_after != 0) ||
976 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
979 motif_n_target_lists = header->n_lists;
980 motif_target_lists = g_new0 (GList *, motif_n_target_lists);
983 for (i=0; i<header->n_lists; i++)
988 if (p + sizeof(guint16) - target_bytes > nitems)
991 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
993 /* We need to make a copy of the targets, since it may
996 targets = g_new (guint32, n_targets);
997 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
999 p += sizeof(guint16) + n_targets * sizeof(guint32);
1000 if (p - target_bytes > nitems)
1003 for (j=0; j<n_targets; j++)
1004 motif_target_lists[i] =
1005 g_list_prepend (motif_target_lists[i],
1006 GUINT_TO_POINTER (card32_to_host (targets[j],
1007 header->byte_order)));
1009 motif_target_lists[i] = g_list_reverse (motif_target_lists[i]);
1019 XFree (target_bytes);
1023 if (motif_target_lists)
1025 g_free (motif_target_lists);
1026 motif_target_lists = NULL;
1027 motif_n_target_lists = 0;
1029 g_warning ("Error reading Motif target table\n");
1035 targets_sort_func (gconstpointer a, gconstpointer b)
1037 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1038 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1041 /* Check if given (sorted) list is in the targets table */
1043 motif_target_table_check (GList *sorted)
1045 GList *tmp_list1, *tmp_list2;
1048 for (i=0; i<motif_n_target_lists; i++)
1050 tmp_list1 = motif_target_lists[i];
1053 while (tmp_list1 && tmp_list2)
1055 if (tmp_list1->data != tmp_list2->data)
1058 tmp_list1 = tmp_list1->next;
1059 tmp_list2 = tmp_list2->next;
1061 if (!tmp_list1 && !tmp_list2) /* Found it */
1069 motif_add_to_target_table (GList *targets)
1071 GList *sorted = NULL;
1076 /* make a sorted copy of the list */
1080 sorted = g_list_insert_sorted (sorted, targets->data, targets_sort_func);
1081 targets = targets->next;
1084 /* First check if it is their already */
1086 if (motif_target_lists)
1087 index = motif_target_table_check (sorted);
1089 /* We need to grab the server while doing this, to ensure
1095 /* We need to make sure that it exists _before_ we grab the
1096 * server, since we can't open a new connection after we
1099 motif_find_drag_window (TRUE);
1101 XGrabServer(gdk_display);
1102 motif_read_target_table();
1104 /* Check again, in case it was added in the meantime */
1106 if (motif_target_lists)
1107 index = motif_target_table_check (sorted);
1111 guint32 total_size = 0;
1115 MotifTargetTableHeader *header;
1117 if (!motif_target_lists)
1119 motif_target_lists = g_new (GList *, 1);
1120 motif_n_target_lists = 1;
1124 motif_n_target_lists++;
1125 motif_target_lists = g_realloc (motif_target_lists,
1126 sizeof(GList *) * motif_n_target_lists);
1128 motif_target_lists[motif_n_target_lists - 1] = sorted;
1130 index = motif_n_target_lists - 1;
1132 total_size = sizeof (MotifTargetTableHeader);
1133 for (i = 0; i < motif_n_target_lists ; i++)
1134 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (motif_target_lists[i]);
1136 data = g_malloc (total_size);
1138 header = (MotifTargetTableHeader *)data;
1139 p = data + sizeof(MotifTargetTableHeader);
1141 header->byte_order = local_byte_order;
1142 header->protocol_version = 0;
1143 header->n_lists = motif_n_target_lists;
1144 header->total_size = total_size;
1146 for (i = 0; i < motif_n_target_lists ; i++)
1148 guint16 n_targets = g_list_length (motif_target_lists[i]);
1149 guint32 *targets = g_new (guint32, n_targets);
1150 guint32 *p32 = targets;
1152 tmp_list = motif_target_lists[i];
1155 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1157 tmp_list = tmp_list->next;
1162 p += sizeof(guint16);
1164 memcpy (p, targets, n_targets * sizeof(guint32));
1167 p += sizeof(guint32) * n_targets;
1171 XChangeProperty (gdk_display, motif_drag_window,
1172 motif_drag_targets_atom,
1173 motif_drag_targets_atom,
1177 XUngrabServer(gdk_display);
1180 g_list_free (sorted);
1184 /* Translate flags */
1187 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1189 guint recommended_op = flags & 0x000f;
1190 guint possible_ops = (flags & 0x0f0) >> 4;
1192 switch (recommended_op)
1195 context->suggested_action = GDK_ACTION_MOVE;
1198 context->suggested_action = GDK_ACTION_COPY;
1201 context->suggested_action = GDK_ACTION_LINK;
1204 context->suggested_action = GDK_ACTION_COPY;
1208 context->actions = 0;
1209 if (possible_ops & XmDROP_MOVE)
1210 context->actions |= GDK_ACTION_MOVE;
1211 if (possible_ops & XmDROP_COPY)
1212 context->actions |= GDK_ACTION_COPY;
1213 if (possible_ops & XmDROP_LINK)
1214 context->actions |= GDK_ACTION_LINK;
1218 motif_dnd_get_flags (GdkDragContext *context)
1222 switch (context->suggested_action)
1224 case GDK_ACTION_MOVE:
1225 flags = XmDROP_MOVE;
1227 case GDK_ACTION_COPY:
1228 flags = XmDROP_COPY;
1230 case GDK_ACTION_LINK:
1231 flags = XmDROP_LINK;
1234 flags = XmDROP_NOOP;
1238 if (context->actions & GDK_ACTION_MOVE)
1239 flags |= XmDROP_MOVE << 8;
1240 if (context->actions & GDK_ACTION_COPY)
1241 flags |= XmDROP_COPY << 8;
1242 if (context->actions & GDK_ACTION_LINK)
1243 flags |= XmDROP_LINK << 8;
1251 motif_set_targets (GdkDragContext *context)
1253 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1254 MotifDragInitiatorInfo info;
1256 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1258 if (!motif_drag_initiator_info)
1259 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1261 info.byte_order = local_byte_order;
1262 info.protocol_version = 0;
1264 info.targets_index = motif_add_to_target_table (context->targets);
1269 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1271 private->motif_selection = gdk_atom_intern (buf, FALSE);
1272 if (!XGetSelectionOwner (gdk_display, private->motif_selection))
1276 info.selection_atom = private->motif_selection;
1278 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
1279 GDK_DRAWABLE_XID (context->source_window),
1280 private->motif_selection,
1281 motif_drag_initiator_info, 8, PropModeReplace,
1282 (guchar *)&info, sizeof (info));
1284 private->motif_targets_set = 1;
1288 motif_check_dest (Window win)
1290 gboolean retval = FALSE;
1291 MotifDragReceiverInfo *info;
1294 unsigned long nitems, after;
1296 if (!motif_drag_receiver_info_atom)
1297 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
1299 gdk_error_trap_push ();
1300 XGetWindowProperty (gdk_display, win,
1301 motif_drag_receiver_info_atom,
1302 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1303 &type, &format, &nitems, &after,
1306 if (gdk_error_trap_pop() == 0)
1310 if ((format == 8) && (nitems == sizeof(*info)))
1312 if ((info->protocol_version == 0) &&
1313 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1314 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1315 (info->protocol_style == XmDRAG_DYNAMIC)))
1321 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1328 return retval ? win : GDK_NONE;
1333 motif_send_enter (GdkDragContext *context,
1337 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1339 xev.xclient.type = ClientMessage;
1340 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1341 xev.xclient.format = 8;
1342 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1344 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1345 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1346 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1347 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1348 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1350 if (!private->motif_targets_set)
1351 motif_set_targets (context);
1353 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1355 if (!gdk_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
1358 g_message ("Send event to %lx failed",
1359 GDK_DRAWABLE_XID (context->dest_window)));
1363 motif_send_leave (GdkDragContext *context,
1368 xev.xclient.type = ClientMessage;
1369 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1370 xev.xclient.format = 8;
1371 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1373 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1374 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1375 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1376 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1377 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_DRAWABLE_XID (context->source_window);
1378 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1380 if (!gdk_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
1383 g_message ("Send event to %lx failed",
1384 GDK_DRAWABLE_XID (context->dest_window)));
1388 motif_send_motion (GdkDragContext *context,
1391 GdkDragAction action,
1396 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1398 xev.xclient.type = ClientMessage;
1399 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1400 xev.xclient.format = 8;
1401 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1403 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1404 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1405 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1407 if ((context->suggested_action != private->old_action) ||
1408 (context->actions != private->old_actions))
1410 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1412 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1417 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1419 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1420 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1422 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1426 if (!gdk_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
1429 g_message ("Send event to %lx failed",
1430 GDK_DRAWABLE_XID (context->dest_window)));
1436 motif_send_drop (GdkDragContext *context, guint32 time)
1439 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1441 xev.xclient.type = ClientMessage;
1442 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1443 xev.xclient.format = 8;
1444 xev.xclient.window = GDK_DRAWABLE_XID (context->dest_window);
1446 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1447 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1448 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1449 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1451 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1452 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1454 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1455 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_DRAWABLE_XID (context->source_window);
1457 if (!gdk_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
1460 g_message ("Send event to %lx failed",
1461 GDK_DRAWABLE_XID (context->dest_window)));
1467 motif_read_initiator_info (Window source_window,
1473 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1479 MotifDragInitiatorInfo *initiator_info;
1481 if (!motif_drag_initiator_info)
1482 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1484 gdk_error_trap_push ();
1485 XGetWindowProperty (gdk_display, source_window, atom,
1486 0, sizeof(*initiator_info), FALSE,
1487 motif_drag_initiator_info,
1488 &type, &format, &nitems, &bytes_after,
1489 (guchar **)&initiator_info);
1491 if (gdk_error_trap_pop () || (format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1493 g_warning ("Error reading initiator info\n");
1497 motif_read_target_table ();
1499 initiator_info->targets_index =
1500 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1501 initiator_info->selection_atom =
1502 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1504 if (initiator_info->targets_index >= motif_n_target_lists)
1506 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1507 XFree (initiator_info);
1508 return GDK_FILTER_REMOVE;
1511 tmp_list = g_list_last (motif_target_lists[initiator_info->targets_index]);
1516 *targets = g_list_prepend (*targets,
1518 tmp_list = tmp_list->prev;
1521 #ifdef G_ENABLE_DEBUG
1522 if (gdk_debug_flags & GDK_DEBUG_DND)
1523 print_target_list (*targets);
1524 #endif /* G_ENABLE_DEBUG */
1526 *selection = initiator_info->selection_atom;
1528 XFree (initiator_info);
1533 static GdkDragContext *
1534 motif_drag_context_new (GdkWindow *dest_window,
1536 guint32 source_window,
1539 GdkDragContext *new_context;
1540 GdkDragContextPrivate *private;
1542 /* FIXME, current_dest_drag really shouldn't be NULL'd
1543 * if we error below.
1545 if (current_dest_drag != NULL)
1547 if (timestamp >= current_dest_drag->start_time)
1549 gdk_drag_context_unref (current_dest_drag);
1550 current_dest_drag = NULL;
1556 new_context = gdk_drag_context_new ();
1557 private = (GdkDragContextPrivate *)new_context;
1559 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1560 new_context->is_source = FALSE;
1562 new_context->source_window = gdk_window_lookup (source_window);
1563 if (new_context->source_window)
1564 gdk_window_ref (new_context->source_window);
1567 new_context->source_window = gdk_window_foreign_new (source_window);
1568 if (!new_context->source_window)
1570 gdk_drag_context_unref (new_context);
1575 new_context->dest_window = dest_window;
1576 gdk_window_ref (dest_window);
1577 new_context->start_time = timestamp;
1579 if (!motif_read_initiator_info(source_window,
1581 &new_context->targets,
1582 &private->motif_selection))
1584 gdk_drag_context_unref (new_context);
1592 * The MOTIF drag protocol has no real provisions for distinguishing
1593 * multiple simultaneous drops. If the sources grab the pointer
1594 * when doing drags, that shouldn't happen, in any case. If it
1595 * does, we can't do much except hope for the best.
1598 static GdkFilterReturn
1599 motif_top_level_enter (GdkEvent *event,
1602 guint32 source_window,
1605 GdkDragContext *new_context;
1607 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1608 flags, timestamp, source_window, atom));
1610 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1612 return GDK_FILTER_REMOVE;
1614 event->dnd.type = GDK_DRAG_ENTER;
1615 event->dnd.context = new_context;
1616 gdk_drag_context_ref (new_context);
1618 current_dest_drag = new_context;
1620 return GDK_FILTER_TRANSLATE;
1624 motif_top_level_leave (GdkEvent *event,
1628 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1631 if ((current_dest_drag != NULL) &&
1632 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1633 (timestamp >= current_dest_drag->start_time))
1635 event->dnd.type = GDK_DRAG_LEAVE;
1636 /* Pass ownership of context to the event */
1637 event->dnd.context = current_dest_drag;
1639 current_dest_drag = NULL;
1641 return GDK_FILTER_TRANSLATE;
1644 return GDK_FILTER_REMOVE;
1648 motif_motion (GdkEvent *event,
1654 GdkDragContextPrivate *private;
1656 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1657 flags, timestamp, x_root, y_root));
1659 if ((current_dest_drag != NULL) &&
1660 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1661 (timestamp >= current_dest_drag->start_time))
1663 private = (GdkDragContextPrivate *)current_dest_drag;
1665 event->dnd.type = GDK_DRAG_MOTION;
1666 event->dnd.context = current_dest_drag;
1667 gdk_drag_context_ref (current_dest_drag);
1669 event->dnd.time = timestamp;
1671 motif_dnd_translate_flags (current_dest_drag, flags);
1673 event->dnd.x_root = x_root;
1674 event->dnd.y_root = y_root;
1676 private->last_x = x_root;
1677 private->last_y = y_root;
1679 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1681 return GDK_FILTER_TRANSLATE;
1684 return GDK_FILTER_REMOVE;
1688 motif_operation_changed (GdkEvent *event,
1692 GdkDragContextPrivate *private;
1694 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1697 if ((current_dest_drag != NULL) &&
1698 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1699 (timestamp >= current_dest_drag->start_time))
1701 event->dnd.type = GDK_DRAG_MOTION;
1702 event->dnd.send_event = FALSE;
1703 event->dnd.context = current_dest_drag;
1704 gdk_drag_context_ref (current_dest_drag);
1706 event->dnd.time = timestamp;
1707 private = (GdkDragContextPrivate *)current_dest_drag;
1709 motif_dnd_translate_flags (current_dest_drag, flags);
1711 event->dnd.x_root = private->last_x;
1712 event->dnd.y_root = private->last_y;
1714 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1716 return GDK_FILTER_TRANSLATE;
1719 return GDK_FILTER_REMOVE;
1723 motif_drop_start (GdkEvent *event,
1726 guint32 source_window,
1731 GdkDragContext *new_context;
1734 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1735 flags, timestamp, x_root, y_root, source_window, atom));
1737 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1739 return GDK_FILTER_REMOVE;
1741 motif_dnd_translate_flags (new_context, flags);
1743 event->dnd.type = GDK_DROP_START;
1744 event->dnd.context = new_context;
1745 event->dnd.time = timestamp;
1746 event->dnd.x_root = x_root;
1747 event->dnd.y_root = y_root;
1749 gdk_drag_context_ref (new_context);
1750 current_dest_drag = new_context;
1752 return GDK_FILTER_TRANSLATE;
1756 motif_drag_status (GdkEvent *event,
1760 GdkDragContext *context;
1763 g_message ("Motif status message: flags %x", flags));
1765 context = gdk_drag_context_find (TRUE,
1766 GDK_DRAWABLE_XID (event->any.window),
1771 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1772 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1773 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1774 private->drag_status = GDK_DRAG_STATUS_DRAG;
1776 event->dnd.type = GDK_DRAG_STATUS;
1777 event->dnd.send_event = FALSE;
1778 event->dnd.context = context;
1779 gdk_drag_context_ref (context);
1781 event->dnd.time = timestamp;
1783 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1785 switch (flags & 0x000f)
1788 context->action = 0;
1791 context->action = GDK_ACTION_MOVE;
1794 context->action = GDK_ACTION_COPY;
1797 context->action = GDK_ACTION_LINK;
1802 context->action = 0;
1804 return GDK_FILTER_TRANSLATE;
1806 return GDK_FILTER_REMOVE;
1810 motif_dnd_filter (GdkXEvent *xev,
1814 XEvent *xevent = (XEvent *)xev;
1819 guint32 source_window;
1821 gint16 x_root, y_root;
1824 /* First read some fields common to all Motif DND messages */
1826 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1827 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1828 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1830 is_reply = ((reason & 0x80) != 0);
1832 switch (reason & 0x7f)
1834 case XmTOP_LEVEL_ENTER:
1835 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1836 atom = MOTIF_UNPACK_LONG (xevent, 3);
1837 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1838 case XmTOP_LEVEL_LEAVE:
1839 return motif_top_level_leave (event, flags, timestamp);
1842 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1843 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1846 return motif_motion (event, flags, timestamp, x_root, y_root);
1848 return motif_drag_status (event, flags, timestamp);
1850 case XmDROP_SITE_ENTER:
1851 return motif_drag_status (event, flags, timestamp);
1853 case XmDROP_SITE_LEAVE:
1854 return motif_drag_status (event,
1855 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1858 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1859 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1860 atom = MOTIF_UNPACK_LONG (xevent, 3);
1861 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1864 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1867 case XmOPERATION_CHANGED:
1869 return motif_operation_changed (event, flags, timestamp);
1871 return motif_drag_status (event, flags, timestamp);
1874 /* To the best of my knowledge, these next two messages are
1875 * not part of the protocol, though they are defined in
1879 case XmDRAG_DROP_FINISH:
1883 return GDK_FILTER_REMOVE;
1886 /*************************************************************
1887 ***************************** XDND **************************
1888 *************************************************************/
1890 /* Utility functions */
1895 GdkDragAction action;
1896 } xdnd_actions_table[] = {
1897 { "XdndActionCopy", GDK_NONE, GDK_ACTION_COPY },
1898 { "XdndActionMove", GDK_NONE, GDK_ACTION_MOVE },
1899 { "XdndActionLink", GDK_NONE, GDK_ACTION_LINK },
1900 { "XdndActionAsk", GDK_NONE, GDK_ACTION_ASK },
1901 { "XdndActionPrivate", GDK_NONE, GDK_ACTION_COPY },
1904 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1905 static gboolean xdnd_actions_initialized = FALSE;
1908 xdnd_initialize_actions (void)
1912 xdnd_actions_initialized = TRUE;
1913 for (i=0; i < xdnd_n_actions; i++)
1914 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1917 static GdkDragAction
1918 xdnd_action_from_atom (GdkAtom atom)
1922 if (!xdnd_actions_initialized)
1923 xdnd_initialize_actions();
1925 for (i=0; i<xdnd_n_actions; i++)
1926 if (atom == xdnd_actions_table[i].atom)
1927 return xdnd_actions_table[i].action;
1933 xdnd_action_to_atom (GdkDragAction action)
1937 if (!xdnd_actions_initialized)
1938 xdnd_initialize_actions();
1940 for (i=0; i<xdnd_n_actions; i++)
1941 if (action == xdnd_actions_table[i].action)
1942 return xdnd_actions_table[i].atom;
1947 static GdkAtom xdnd_aware_atom = GDK_NONE;
1951 static GdkFilterReturn
1952 xdnd_status_filter (GdkXEvent *xev,
1956 XEvent *xevent = (XEvent *)xev;
1957 guint32 dest_window = xevent->xclient.data.l[0];
1958 guint32 flags = xevent->xclient.data.l[1];
1959 GdkAtom action = xevent->xclient.data.l[4];
1960 GdkDragContext *context;
1963 g_message ("XdndStatus: dest_window: %#x action: %ld",
1964 dest_window, action));
1967 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
1970 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1971 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1972 private->drag_status = GDK_DRAG_STATUS_DRAG;
1974 event->dnd.send_event = FALSE;
1975 event->dnd.type = GDK_DRAG_STATUS;
1976 event->dnd.context = context;
1977 gdk_drag_context_ref (context);
1979 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1980 if (!(action != 0) != !(flags & 1))
1983 g_warning ("Received status event with flags not corresponding to action!\n"));
1987 context->action = xdnd_action_from_atom (action);
1989 return GDK_FILTER_TRANSLATE;
1992 return GDK_FILTER_REMOVE;
1995 static GdkFilterReturn
1996 xdnd_finished_filter (GdkXEvent *xev,
2000 XEvent *xevent = (XEvent *)xev;
2001 guint32 dest_window = xevent->xclient.data.l[0];
2002 GdkDragContext *context;
2005 g_message ("XdndFinished: dest_window: %#x", dest_window));
2007 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
2010 event->dnd.type = GDK_DROP_FINISHED;
2011 event->dnd.context = context;
2012 gdk_drag_context_ref (context);
2014 return GDK_FILTER_TRANSLATE;
2017 return GDK_FILTER_REMOVE;
2021 xdnd_set_targets (GdkDragContext *context)
2023 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2025 GList *tmp_list = context->targets;
2027 gint n_atoms = g_list_length (context->targets);
2029 atomlist = g_new (GdkAtom, n_atoms);
2033 atomlist[i] = GPOINTER_TO_INT (tmp_list->data);
2034 tmp_list = tmp_list->next;
2038 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2039 GDK_DRAWABLE_XID (context->source_window),
2040 gdk_atom_intern ("XdndTypeList", FALSE),
2041 XA_ATOM, 32, PropModeReplace,
2042 (guchar *)atomlist, n_atoms);
2046 private->xdnd_targets_set = 1;
2050 xdnd_set_actions (GdkDragContext *context)
2052 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2058 if (!xdnd_actions_initialized)
2059 xdnd_initialize_actions();
2061 actions = context->actions;
2063 for (i=0; i<xdnd_n_actions; i++)
2065 if (actions & xdnd_actions_table[i].action)
2067 actions &= ~xdnd_actions_table[i].action;
2072 atomlist = g_new (GdkAtom, n_atoms);
2074 actions = context->actions;
2076 for (i=0; i<xdnd_n_actions; i++)
2078 if (actions & xdnd_actions_table[i].action)
2080 actions &= ~xdnd_actions_table[i].action;
2081 atomlist[n_atoms] = xdnd_actions_table[i].atom;
2086 XChangeProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2087 GDK_DRAWABLE_XID (context->source_window),
2088 gdk_atom_intern ("XdndActionList", FALSE),
2089 XA_ATOM, 32, PropModeReplace,
2090 (guchar *)atomlist, n_atoms);
2094 private->xdnd_actions_set = 1;
2095 private->xdnd_actions = context->actions;
2098 /*************************************************************
2100 * Like gdk_send_event, but if the target is the root
2101 * window, sets an event mask of ButtonPressMask, otherwise
2102 * an event mask of 0.
2106 *************************************************************/
2109 xdnd_send_xevent (Window window, gboolean propagate,
2112 if (window == gdk_root_window)
2113 return gdk_send_xevent (window, propagate, ButtonPressMask, event_send);
2115 return gdk_send_xevent (window, propagate, 0, event_send);
2119 xdnd_send_enter (GdkDragContext *context)
2122 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2124 xev.xclient.type = ClientMessage;
2125 xev.xclient.message_type = gdk_atom_intern ("XdndEnter", FALSE);
2126 xev.xclient.format = 32;
2127 xev.xclient.window = private->drop_xid ?
2129 GDK_DRAWABLE_XID (context->dest_window);
2130 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2131 xev.xclient.data.l[1] = (3 << 24); /* version */
2132 xev.xclient.data.l[2] = 0;
2133 xev.xclient.data.l[3] = 0;
2134 xev.xclient.data.l[4] = 0;
2136 if (!private->xdnd_selection)
2137 private->xdnd_selection = gdk_atom_intern ("XdndSelection", FALSE);
2139 if (g_list_length (context->targets) > 3)
2141 if (!private->xdnd_targets_set)
2142 xdnd_set_targets (context);
2143 xev.xclient.data.l[1] |= 1;
2147 GList *tmp_list = context->targets;
2152 xev.xclient.data.l[i] = GPOINTER_TO_INT (tmp_list->data);
2153 tmp_list = tmp_list->next;
2158 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
2162 g_message ("Send event to %lx failed",
2163 GDK_DRAWABLE_XID (context->dest_window)));
2164 gdk_window_unref (context->dest_window);
2165 context->dest_window = NULL;
2170 xdnd_send_leave (GdkDragContext *context)
2174 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2176 xev.xclient.type = ClientMessage;
2177 xev.xclient.message_type = gdk_atom_intern ("XdndLeave", FALSE);
2178 xev.xclient.format = 32;
2179 xev.xclient.window = private->drop_xid ?
2181 GDK_DRAWABLE_XID (context->dest_window);
2182 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2183 xev.xclient.data.l[1] = 0;
2184 xev.xclient.data.l[2] = 0;
2185 xev.xclient.data.l[3] = 0;
2186 xev.xclient.data.l[4] = 0;
2188 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
2192 g_message ("Send event to %lx failed",
2193 GDK_DRAWABLE_XID (context->dest_window)));
2194 gdk_window_unref (context->dest_window);
2195 context->dest_window = NULL;
2200 xdnd_send_drop (GdkDragContext *context, guint32 time)
2202 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2205 xev.xclient.type = ClientMessage;
2206 xev.xclient.message_type = gdk_atom_intern ("XdndDrop", FALSE);
2207 xev.xclient.format = 32;
2208 xev.xclient.window = private->drop_xid ?
2210 GDK_DRAWABLE_XID (context->dest_window);
2211 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2212 xev.xclient.data.l[1] = 0;
2213 xev.xclient.data.l[2] = time;
2214 xev.xclient.data.l[3] = 0;
2215 xev.xclient.data.l[4] = 0;
2217 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
2221 g_message ("Send event to %lx failed",
2222 GDK_DRAWABLE_XID (context->dest_window)));
2223 gdk_window_unref (context->dest_window);
2224 context->dest_window = NULL;
2229 xdnd_send_motion (GdkDragContext *context,
2232 GdkDragAction action,
2235 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2238 xev.xclient.type = ClientMessage;
2239 xev.xclient.message_type = gdk_atom_intern ("XdndPosition", FALSE);
2240 xev.xclient.format = 32;
2241 xev.xclient.window = private->drop_xid ?
2243 GDK_DRAWABLE_XID (context->dest_window);
2244 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->source_window);
2245 xev.xclient.data.l[1] = 0;
2246 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2247 xev.xclient.data.l[3] = time;
2248 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
2250 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->dest_window),
2254 g_message ("Send event to %lx failed",
2255 GDK_DRAWABLE_XID (context->dest_window)));
2256 gdk_window_unref (context->dest_window);
2257 context->dest_window = NULL;
2259 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2263 xdnd_check_dest (Window win)
2265 gboolean retval = FALSE;
2268 unsigned long nitems, after;
2272 static GdkAtom xdnd_proxy_atom = GDK_NONE;
2274 gint old_warnings = gdk_error_warnings;
2276 if (!xdnd_proxy_atom)
2277 xdnd_proxy_atom = gdk_atom_intern ("XdndProxy", FALSE);
2279 if (!xdnd_aware_atom)
2280 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
2285 gdk_error_warnings = 0;
2287 XGetWindowProperty (gdk_display, win,
2289 1, False, AnyPropertyType,
2290 &type, &format, &nitems, &after,
2291 (guchar **)&proxy_data);
2293 if (!gdk_error_code)
2297 if ((format == 32) && (nitems == 1))
2299 proxy = *proxy_data;
2303 g_warning ("Invalid XdndOwner property on window %ld\n", win));
2308 XGetWindowProperty (gdk_display, proxy ? proxy : win,
2310 1, False, AnyPropertyType,
2311 &type, &format, &nitems, &after,
2312 (guchar **)&version);
2314 if (!gdk_error_code && type != None)
2316 if ((format == 32) && (nitems == 1))
2323 g_warning ("Invalid XdndAware property on window %ld\n", win));
2330 gdk_error_warnings = old_warnings;
2333 return retval ? (proxy ? proxy : win) : GDK_NONE;
2339 xdnd_read_actions (GdkDragContext *context)
2343 gulong nitems, after;
2348 gint old_warnings = gdk_error_warnings;
2351 gdk_error_warnings = 0;
2353 /* Get the XdndActionList, if set */
2355 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (context->source_window),
2356 GDK_DRAWABLE_XID (context->source_window),
2357 gdk_atom_intern ("XdndActionList", FALSE), 0, 65536,
2358 False, XA_ATOM, &type, &format, &nitems,
2359 &after, (guchar **)&data);
2361 if (!gdk_error_code && (format == 32) && (type == XA_ATOM))
2363 context->actions = 0;
2365 for (i=0; i<nitems; i++)
2366 context->actions |= xdnd_action_from_atom (data[i]);
2368 ((GdkDragContextPrivate *)context)->xdnd_have_actions = TRUE;
2370 #ifdef G_ENABLE_DEBUG
2371 if (gdk_debug_flags & GDK_DEBUG_DND)
2373 GString *action_str = g_string_new (NULL);
2374 if (context->actions & GDK_ACTION_MOVE)
2375 g_string_append(action_str, "MOVE ");
2376 if (context->actions & GDK_ACTION_COPY)
2377 g_string_append(action_str, "COPY ");
2378 if (context->actions & GDK_ACTION_LINK)
2379 g_string_append(action_str, "LINK ");
2380 if (context->actions & GDK_ACTION_ASK)
2381 g_string_append(action_str, "ASK ");
2383 g_message("Xdnd actions = %s", action_str->str);
2384 g_string_free (action_str, TRUE);
2386 #endif /* G_ENABLE_DEBUG */
2391 gdk_error_warnings = old_warnings;
2395 /* We have to make sure that the XdndActionList we keep internally
2396 * is up to date with the XdndActionList on the source window
2397 * because we get no notification, because Xdnd wasn't meant
2398 * to continually send actions. So we select on PropertyChangeMask
2399 * and add this filter.
2401 static GdkFilterReturn
2402 xdnd_source_window_filter (GdkXEvent *xev,
2406 XEvent *xevent = (XEvent *)xev;
2407 GdkDragContext *context = cb_data;
2409 if ((xevent->xany.type == PropertyNotify) &&
2410 (xevent->xproperty.atom == gdk_atom_intern ("XdndActionList", FALSE)))
2412 xdnd_read_actions (context);
2414 return GDK_FILTER_REMOVE;
2417 return GDK_FILTER_CONTINUE;
2421 xdnd_manage_source_filter (GdkDragContext *context,
2423 gboolean add_filter)
2425 gint old_warnings = 0; /* quiet gcc */
2427 gboolean is_foreign = GDK_DRAWABLE_TYPE (window);
2431 old_warnings = gdk_error_warnings;
2432 gdk_error_warnings = 0;
2435 if (!GDK_DRAWABLE_DESTROYED (window))
2439 gdk_window_set_events (window,
2440 gdk_window_get_events (window) |
2441 GDK_PROPERTY_CHANGE_MASK);
2442 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2447 gdk_window_remove_filter (window,
2448 xdnd_source_window_filter,
2450 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2451 * but we might want it for other reasons. (Like
2452 * INCR selection transactions).
2460 gdk_error_warnings = old_warnings;
2464 static GdkFilterReturn
2465 xdnd_enter_filter (GdkXEvent *xev,
2469 XEvent *xevent = (XEvent *)xev;
2470 GdkDragContext *new_context;
2475 gulong nitems, after;
2478 guint32 source_window = xevent->xclient.data.l[0];
2479 gboolean get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2480 gint version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2483 g_message ("XdndEnter: source_window: %#x, version: %#x",
2484 source_window, version));
2488 /* Old source ignore */
2489 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2490 return GDK_FILTER_REMOVE;
2493 if (current_dest_drag != NULL)
2495 gdk_drag_context_unref (current_dest_drag);
2496 current_dest_drag = NULL;
2499 new_context = gdk_drag_context_new ();
2500 new_context->protocol = GDK_DRAG_PROTO_XDND;
2501 new_context->is_source = FALSE;
2503 new_context->source_window = gdk_window_lookup (source_window);
2504 if (new_context->source_window)
2505 gdk_window_ref (new_context->source_window);
2508 new_context->source_window = gdk_window_foreign_new (source_window);
2509 if (!new_context->source_window)
2511 gdk_drag_context_unref (new_context);
2512 return GDK_FILTER_REMOVE;
2515 new_context->dest_window = event->any.window;
2516 gdk_window_ref (new_context->dest_window);
2518 new_context->targets = NULL;
2521 gdk_error_trap_push ();
2522 XGetWindowProperty (GDK_DRAWABLE_XDISPLAY (event->any.window),
2524 gdk_atom_intern ("XdndTypeList", FALSE), 0, 65536,
2525 False, XA_ATOM, &type, &format, &nitems,
2526 &after, (guchar **)&data);
2528 if (gdk_error_trap_pop () || (format != 32) || (type != XA_ATOM))
2530 gdk_drag_context_unref (new_context);
2531 return GDK_FILTER_REMOVE;
2534 for (i=0; i<nitems; i++)
2535 new_context->targets = g_list_append (new_context->targets,
2536 GUINT_TO_POINTER (data[i]));
2543 if (xevent->xclient.data.l[2+i])
2544 new_context->targets = g_list_append (new_context->targets,
2545 GUINT_TO_POINTER (xevent->xclient.data.l[2+i]));
2548 #ifdef G_ENABLE_DEBUG
2549 if (gdk_debug_flags & GDK_DEBUG_DND)
2550 print_target_list (new_context->targets);
2551 #endif /* G_ENABLE_DEBUG */
2553 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2554 xdnd_read_actions (new_context);
2556 event->dnd.type = GDK_DRAG_ENTER;
2557 event->dnd.context = new_context;
2558 gdk_drag_context_ref (new_context);
2560 current_dest_drag = new_context;
2561 ((GdkDragContextPrivate *)new_context)->xdnd_selection =
2562 gdk_atom_intern ("XdndSelection", FALSE);
2564 return GDK_FILTER_TRANSLATE;
2567 static GdkFilterReturn
2568 xdnd_leave_filter (GdkXEvent *xev,
2572 XEvent *xevent = (XEvent *)xev;
2573 guint32 source_window = xevent->xclient.data.l[0];
2576 g_message ("XdndLeave: source_window: %#x",
2579 if ((current_dest_drag != NULL) &&
2580 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2581 (GDK_DRAWABLE_XID (current_dest_drag->source_window) == source_window))
2583 event->dnd.type = GDK_DRAG_LEAVE;
2584 /* Pass ownership of context to the event */
2585 event->dnd.context = current_dest_drag;
2587 current_dest_drag = NULL;
2589 return GDK_FILTER_TRANSLATE;
2592 return GDK_FILTER_REMOVE;
2595 static GdkFilterReturn
2596 xdnd_position_filter (GdkXEvent *xev,
2600 XEvent *xevent = (XEvent *)xev;
2601 guint32 source_window = xevent->xclient.data.l[0];
2602 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2603 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2604 guint32 time = xevent->xclient.data.l[3];
2605 GdkAtom action = xevent->xclient.data.l[4];
2608 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2609 source_window, x_root, y_root, time, action));
2612 if ((current_dest_drag != NULL) &&
2613 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2614 (GDK_DRAWABLE_XID (current_dest_drag->source_window) == source_window))
2616 event->dnd.type = GDK_DRAG_MOTION;
2617 event->dnd.context = current_dest_drag;
2618 gdk_drag_context_ref (current_dest_drag);
2620 event->dnd.time = time;
2622 current_dest_drag->suggested_action = xdnd_action_from_atom (action);
2623 if (!((GdkDragContextPrivate *)current_dest_drag)->xdnd_have_actions)
2624 current_dest_drag->actions = current_dest_drag->suggested_action;
2626 event->dnd.x_root = x_root;
2627 event->dnd.y_root = y_root;
2629 ((GdkDragContextPrivate *)current_dest_drag)->last_x = x_root;
2630 ((GdkDragContextPrivate *)current_dest_drag)->last_y = y_root;
2632 return GDK_FILTER_TRANSLATE;
2635 return GDK_FILTER_REMOVE;
2638 static GdkFilterReturn
2639 xdnd_drop_filter (GdkXEvent *xev,
2643 XEvent *xevent = (XEvent *)xev;
2644 guint32 source_window = xevent->xclient.data.l[0];
2645 guint32 time = xevent->xclient.data.l[2];
2648 g_message ("XdndDrop: source_window: %#x time: %d",
2649 source_window, time));
2651 if ((current_dest_drag != NULL) &&
2652 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2653 (GDK_DRAWABLE_XID (current_dest_drag->source_window) == source_window))
2655 GdkDragContextPrivate *private;
2656 private = (GdkDragContextPrivate *)current_dest_drag;
2658 event->dnd.type = GDK_DROP_START;
2660 event->dnd.context = current_dest_drag;
2661 gdk_drag_context_ref (current_dest_drag);
2663 event->dnd.time = time;
2664 event->dnd.x_root = private->last_x;
2665 event->dnd.y_root = private->last_y;
2667 return GDK_FILTER_TRANSLATE;
2670 return GDK_FILTER_REMOVE;
2673 /*************************************************************
2674 ************************** Public API ***********************
2675 *************************************************************/
2682 gdk_add_client_message_filter (
2683 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2684 motif_dnd_filter, NULL);
2685 gdk_add_client_message_filter (
2686 gdk_atom_intern ("XdndEnter", FALSE),
2687 xdnd_enter_filter, NULL);
2688 gdk_add_client_message_filter (
2689 gdk_atom_intern ("XdndLeave", FALSE),
2690 xdnd_leave_filter, NULL);
2691 gdk_add_client_message_filter (
2692 gdk_atom_intern ("XdndPosition", FALSE),
2693 xdnd_position_filter, NULL);
2694 gdk_add_client_message_filter (
2695 gdk_atom_intern ("XdndStatus", FALSE),
2696 xdnd_status_filter, NULL);
2697 gdk_add_client_message_filter (
2698 gdk_atom_intern ("XdndFinished", FALSE),
2699 xdnd_finished_filter, NULL);
2700 gdk_add_client_message_filter (
2701 gdk_atom_intern ("XdndDrop", FALSE),
2702 xdnd_drop_filter, NULL);
2708 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2710 if (context->dest_window)
2712 switch (context->protocol)
2714 case GDK_DRAG_PROTO_MOTIF:
2715 motif_send_leave (context, time);
2717 case GDK_DRAG_PROTO_XDND:
2718 xdnd_send_leave (context);
2720 case GDK_DRAG_PROTO_ROOTWIN:
2721 case GDK_DRAG_PROTO_NONE:
2725 gdk_window_unref (context->dest_window);
2726 context->dest_window = NULL;
2731 gdk_drag_begin (GdkWindow *window,
2735 GdkDragContext *new_context;
2737 g_return_val_if_fail (window != NULL, NULL);
2739 new_context = gdk_drag_context_new ();
2740 new_context->is_source = TRUE;
2741 new_context->source_window = window;
2742 gdk_window_ref (window);
2744 tmp_list = g_list_last (targets);
2745 new_context->targets = NULL;
2748 new_context->targets = g_list_prepend (new_context->targets,
2750 tmp_list = tmp_list->prev;
2753 new_context->actions = 0;
2759 gdk_drag_get_protocol (guint32 xid,
2760 GdkDragProtocol *protocol)
2764 if ((retval = xdnd_check_dest (xid)))
2766 *protocol = GDK_DRAG_PROTO_XDND;
2767 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2770 else if ((retval = motif_check_dest (xid)))
2772 *protocol = GDK_DRAG_PROTO_MOTIF;
2773 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2778 /* Check if this is a root window */
2780 gboolean rootwin = FALSE;
2781 gint old_warnings = gdk_error_warnings;
2784 unsigned long nitems, after;
2785 unsigned char *data;
2787 if (xid == gdk_root_window)
2790 gdk_error_warnings = 0;
2796 XGetWindowProperty (gdk_display, xid,
2797 gdk_atom_intern ("ENLIGHTENMENT_DESKTOP", FALSE),
2798 0, 0, False, AnyPropertyType,
2799 &type, &format, &nitems, &after, &data);
2800 if ((gdk_error_code == 0) && type != None)
2807 /* Until I find out what window manager the next one is for,
2808 * I'm leaving it commented out. It's supported in the
2809 * xscreensaver sources, though.
2816 XGetWindowProperty (gdk_display, win,
2817 gdk_atom_intern ("__SWM_VROOT", FALSE),
2818 0, 0, False, AnyPropertyType,
2819 &type, &format, &nitems, &data);
2820 if ((gdk_error_code == 0) && type != None)
2825 gdk_error_warnings = old_warnings;
2829 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2834 *protocol = GDK_DRAG_PROTO_NONE;
2839 gdk_drag_find_window (GdkDragContext *context,
2840 GdkWindow *drag_window,
2843 GdkWindow **dest_window,
2844 GdkDragProtocol *protocol)
2846 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2849 g_return_if_fail (context != NULL);
2851 if (!private->window_cache)
2852 private->window_cache = gdk_window_cache_new();
2854 dest = get_client_window_at_coords (private->window_cache,
2856 GDK_DRAWABLE_XID (drag_window) : GDK_NONE,
2859 if (private->dest_xid != dest)
2862 private->dest_xid = dest;
2864 /* Check if new destination accepts drags, and which protocol */
2866 /* There is some ugliness here. We actually need to pass
2867 * _three_ pieces of information to drag_motion - dest_window,
2868 * protocol, and the XID of the unproxied window. The first
2869 * two are passed explicitely, the third implicitly through
2870 * protocol->dest_xid.
2872 if ((recipient = gdk_drag_get_protocol (dest, protocol)))
2874 *dest_window = gdk_window_lookup (recipient);
2876 gdk_window_ref (*dest_window);
2878 *dest_window = gdk_window_foreign_new (recipient);
2881 *dest_window = NULL;
2885 *dest_window = context->dest_window;
2887 gdk_window_ref (*dest_window);
2888 *protocol = context->protocol;
2893 gdk_drag_motion (GdkDragContext *context,
2894 GdkWindow *dest_window,
2895 GdkDragProtocol protocol,
2898 GdkDragAction suggested_action,
2899 GdkDragAction possible_actions,
2902 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2904 g_return_val_if_fail (context != NULL, FALSE);
2906 /* When we have a Xdnd target, make sure our XdndActionList
2907 * matches the current actions;
2909 private->old_actions = context->actions;
2910 context->actions = possible_actions;
2912 if ((protocol == GDK_DRAG_PROTO_XDND) &&
2913 (!private->xdnd_actions_set ||
2914 private->xdnd_actions != possible_actions))
2915 xdnd_set_actions (context);
2917 if (context->dest_window != dest_window)
2919 GdkEvent temp_event;
2921 /* Send a leave to the last destination */
2922 gdk_drag_do_leave (context, time);
2923 private->drag_status = GDK_DRAG_STATUS_DRAG;
2925 /* Check if new destination accepts drags, and which protocol */
2929 context->dest_window = dest_window;
2930 private->drop_xid = private->dest_xid;
2931 gdk_window_ref (context->dest_window);
2932 context->protocol = protocol;
2936 case GDK_DRAG_PROTO_MOTIF:
2937 motif_send_enter (context, time);
2940 case GDK_DRAG_PROTO_XDND:
2941 xdnd_send_enter (context);
2944 case GDK_DRAG_PROTO_ROOTWIN:
2945 case GDK_DRAG_PROTO_NONE:
2948 private->old_action = suggested_action;
2949 context->suggested_action = suggested_action;
2950 private->old_actions = possible_actions;
2954 context->dest_window = NULL;
2955 private->drop_xid = None;
2956 context->action = 0;
2959 /* Push a status event, to let the client know that
2963 temp_event.dnd.type = GDK_DRAG_STATUS;
2964 temp_event.dnd.window = context->source_window;
2965 /* We use this to signal a synthetic status. Perhaps
2966 * we should use an extra field...
2968 temp_event.dnd.send_event = TRUE;
2970 temp_event.dnd.context = context;
2971 temp_event.dnd.time = time;
2973 gdk_event_put (&temp_event);
2977 private->old_action = context->suggested_action;
2978 context->suggested_action = suggested_action;
2981 /* Send a drag-motion event */
2983 private->last_x = x_root;
2984 private->last_y = y_root;
2986 if (context->dest_window)
2988 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
2990 switch (context->protocol)
2992 case GDK_DRAG_PROTO_MOTIF:
2993 motif_send_motion (context, x_root, y_root, suggested_action, time);
2996 case GDK_DRAG_PROTO_XDND:
2997 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
3000 case GDK_DRAG_PROTO_ROOTWIN:
3002 GdkEvent temp_event;
3004 if (g_list_find (context->targets,
3005 GUINT_TO_POINTER (gdk_atom_intern ("application/x-rootwin-drop", FALSE))))
3006 context->action = context->suggested_action;
3008 context->action = 0;
3010 temp_event.dnd.type = GDK_DRAG_STATUS;
3011 temp_event.dnd.window = context->source_window;
3012 temp_event.dnd.send_event = FALSE;
3013 temp_event.dnd.context = context;
3014 temp_event.dnd.time = time;
3016 gdk_event_put (&temp_event);
3019 case GDK_DRAG_PROTO_NONE:
3020 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3032 gdk_drag_drop (GdkDragContext *context,
3035 g_return_if_fail (context != NULL);
3037 if (context->dest_window)
3039 switch (context->protocol)
3041 case GDK_DRAG_PROTO_MOTIF:
3042 motif_send_leave (context, time);
3043 motif_send_drop (context, time);
3046 case GDK_DRAG_PROTO_XDND:
3047 xdnd_send_drop (context, time);
3050 case GDK_DRAG_PROTO_ROOTWIN:
3051 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3053 case GDK_DRAG_PROTO_NONE:
3054 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3061 gdk_drag_abort (GdkDragContext *context,
3064 g_return_if_fail (context != NULL);
3066 gdk_drag_do_leave (context, time);
3069 /* Destination side */
3072 gdk_drag_status (GdkDragContext *context,
3073 GdkDragAction action,
3076 GdkDragContextPrivate *private;
3079 g_return_if_fail (context != NULL);
3081 private = (GdkDragContextPrivate *)context;
3083 context->action = action;
3085 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3087 xev.xclient.type = ClientMessage;
3088 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
3089 xev.xclient.format = 8;
3090 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3092 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3094 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3098 if ((action != 0) != (private->old_action != 0))
3101 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3103 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3106 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3109 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3113 case GDK_ACTION_MOVE:
3114 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3116 case GDK_ACTION_COPY:
3117 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3119 case GDK_ACTION_LINK:
3120 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3123 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3128 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3130 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3132 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3133 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3134 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3136 if (!gdk_send_xevent (GDK_DRAWABLE_XID (context->source_window),
3139 g_message ("Send event to %lx failed",
3140 GDK_DRAWABLE_XID (context->source_window)));
3142 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3144 xev.xclient.type = ClientMessage;
3145 xev.xclient.message_type = gdk_atom_intern ("XdndStatus", FALSE);
3146 xev.xclient.format = 32;
3147 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3149 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3150 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3151 xev.xclient.data.l[2] = 0;
3152 xev.xclient.data.l[3] = 0;
3153 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
3155 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->source_window),
3158 g_message ("Send event to %lx failed",
3159 GDK_DRAWABLE_XID (context->source_window)));
3162 private->old_action = action;
3166 gdk_drop_reply (GdkDragContext *context,
3170 GdkDragContextPrivate *private;
3172 g_return_if_fail (context != NULL);
3174 private = (GdkDragContextPrivate *)context;
3176 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3180 xev.xclient.type = ClientMessage;
3181 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
3182 xev.xclient.format = 8;
3184 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3185 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3187 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3188 (XmDROP_SITE_VALID << 4) |
3189 (XmDROP_NOOP << 8) |
3192 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3193 (XmNO_DROP_SITE << 4) |
3194 (XmDROP_NOOP << 8) |
3195 (XmDROP_CANCEL << 12);
3196 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3197 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3199 gdk_send_xevent (GDK_DRAWABLE_XID (context->source_window),
3205 gdk_drop_finish (GdkDragContext *context,
3209 g_return_if_fail (context != NULL);
3211 if (context->protocol == GDK_DRAG_PROTO_XDND)
3215 xev.xclient.type = ClientMessage;
3216 xev.xclient.message_type = gdk_atom_intern ("XdndFinished", FALSE);
3217 xev.xclient.format = 32;
3218 xev.xclient.window = GDK_DRAWABLE_XID (context->source_window);
3220 xev.xclient.data.l[0] = GDK_DRAWABLE_XID (context->dest_window);
3221 xev.xclient.data.l[1] = 0;
3222 xev.xclient.data.l[2] = 0;
3223 xev.xclient.data.l[3] = 0;
3224 xev.xclient.data.l[4] = 0;
3226 if (!xdnd_send_xevent (GDK_DRAWABLE_XID (context->source_window),
3229 g_message ("Send event to %lx failed",
3230 GDK_DRAWABLE_XID (context->source_window)));
3236 gdk_window_register_dnd (GdkWindow *window)
3238 static gulong xdnd_version = 3;
3239 MotifDragReceiverInfo info;
3241 g_return_if_fail (window != NULL);
3243 /* Set Motif drag receiver information property */
3245 if (!motif_drag_receiver_info_atom)
3246 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
3248 info.byte_order = local_byte_order;
3249 info.protocol_version = 0;
3250 info.protocol_style = XmDRAG_DYNAMIC;
3251 info.proxy_window = GDK_NONE;
3252 info.num_drop_sites = 0;
3253 info.total_size = sizeof(info);
3255 XChangeProperty (gdk_display, GDK_DRAWABLE_XID (window),
3256 motif_drag_receiver_info_atom,
3257 motif_drag_receiver_info_atom,
3264 if (!xdnd_aware_atom)
3265 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
3267 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3268 XChangeProperty (GDK_DRAWABLE_XDISPLAY (window),
3269 GDK_DRAWABLE_XID (window),
3270 xdnd_aware_atom, XA_ATOM,
3271 32, PropModeReplace,
3272 (guchar *)&xdnd_version, 1);
3275 /*************************************************************
3276 * gdk_drag_get_selection:
3277 * Returns the selection atom for the current source window
3281 *************************************************************/
3284 gdk_drag_get_selection (GdkDragContext *context)
3286 g_return_val_if_fail (context != NULL, GDK_NONE);
3288 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3289 return ((GdkDragContextPrivate *)context)->motif_selection;
3290 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3291 return ((GdkDragContextPrivate *)context)->xdnd_selection;