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_WINDOW_XWINDOW (context->dest_window)) :
207 if ((!context->is_source == !is_source) &&
208 ((source_xid == None) || (context->source_window &&
209 (GDK_WINDOW_XWINDOW (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 ((GdkWindow *)&gdk_root_parent,
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 ((GdkWindow *)&gdk_root_parent,
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 XGetWindowProperty (gdk_display, motif_drag_window,
954 motif_drag_targets_atom,
955 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
956 motif_drag_targets_atom,
957 &type, &format, &nitems, &bytes_after,
960 if ((format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
963 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
964 header->total_size = card32_to_host (header->total_size, header->byte_order);
966 XGetWindowProperty (gdk_display, motif_drag_window, motif_drag_targets_atom,
967 (sizeof(MotifTargetTableHeader)+3)/4,
968 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
970 motif_drag_targets_atom, &type, &format, &nitems,
971 &bytes_after, &target_bytes);
973 if ((format != 8) || (bytes_after != 0) ||
974 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
977 motif_n_target_lists = header->n_lists;
978 motif_target_lists = g_new0 (GList *, motif_n_target_lists);
981 for (i=0; i<header->n_lists; i++)
986 if (p + sizeof(guint16) - target_bytes > nitems)
989 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
991 /* We need to make a copy of the targets, since it may
994 targets = g_new (guint32, n_targets);
995 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
997 p += sizeof(guint16) + n_targets * sizeof(guint32);
998 if (p - target_bytes > nitems)
1001 for (j=0; j<n_targets; j++)
1002 motif_target_lists[i] =
1003 g_list_prepend (motif_target_lists[i],
1004 GUINT_TO_POINTER (card32_to_host (targets[j],
1005 header->byte_order)));
1007 motif_target_lists[i] = g_list_reverse (motif_target_lists[i]);
1017 XFree (target_bytes);
1021 if (motif_target_lists)
1023 g_free (motif_target_lists);
1024 motif_target_lists = NULL;
1025 motif_n_target_lists = 0;
1027 g_warning ("Error reading Motif target table\n");
1033 targets_sort_func (gconstpointer a, gconstpointer b)
1035 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1036 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1039 /* Check if given (sorted) list is in the targets table */
1041 motif_target_table_check (GList *sorted)
1043 GList *tmp_list1, *tmp_list2;
1046 for (i=0; i<motif_n_target_lists; i++)
1048 tmp_list1 = motif_target_lists[i];
1051 while (tmp_list1 && tmp_list2)
1053 if (tmp_list1->data != tmp_list2->data)
1056 tmp_list1 = tmp_list1->next;
1057 tmp_list2 = tmp_list2->next;
1059 if (!tmp_list1 && !tmp_list2) /* Found it */
1067 motif_add_to_target_table (GList *targets)
1069 GList *sorted = NULL;
1074 /* make a sorted copy of the list */
1078 sorted = g_list_insert_sorted (sorted, targets->data, targets_sort_func);
1079 targets = targets->next;
1082 /* First check if it is their already */
1084 if (motif_target_lists)
1085 index = motif_target_table_check (sorted);
1087 /* We need to grab the server while doing this, to ensure
1093 /* We need to make sure that it exists _before_ we grab the
1094 * server, since we can't open a new connection after we
1097 motif_find_drag_window (TRUE);
1099 XGrabServer(gdk_display);
1100 motif_read_target_table();
1102 /* Check again, in case it was added in the meantime */
1104 if (motif_target_lists)
1105 index = motif_target_table_check (sorted);
1109 guint32 total_size = 0;
1113 MotifTargetTableHeader *header;
1115 if (!motif_target_lists)
1117 motif_target_lists = g_new (GList *, 1);
1118 motif_n_target_lists = 1;
1122 motif_n_target_lists++;
1123 motif_target_lists = g_realloc (motif_target_lists,
1124 sizeof(GList *) * motif_n_target_lists);
1126 motif_target_lists[motif_n_target_lists - 1] = sorted;
1128 index = motif_n_target_lists - 1;
1130 total_size = sizeof (MotifTargetTableHeader);
1131 for (i = 0; i < motif_n_target_lists ; i++)
1132 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (motif_target_lists[i]);
1134 data = g_malloc (total_size);
1136 header = (MotifTargetTableHeader *)data;
1137 p = data + sizeof(MotifTargetTableHeader);
1139 header->byte_order = local_byte_order;
1140 header->protocol_version = 0;
1141 header->n_lists = motif_n_target_lists;
1142 header->total_size = total_size;
1144 for (i = 0; i < motif_n_target_lists ; i++)
1146 guint16 n_targets = g_list_length (motif_target_lists[i]);
1147 guint32 *targets = g_new (guint32, n_targets);
1148 guint32 *p32 = targets;
1150 tmp_list = motif_target_lists[i];
1153 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1155 tmp_list = tmp_list->next;
1160 p += sizeof(guint16);
1162 memcpy (p, targets, n_targets * sizeof(guint32));
1165 p += sizeof(guint32) * n_targets;
1169 XChangeProperty (gdk_display, motif_drag_window,
1170 motif_drag_targets_atom,
1171 motif_drag_targets_atom,
1175 XUngrabServer(gdk_display);
1178 g_list_free (sorted);
1182 /* Translate flags */
1185 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1187 guint recommended_op = flags & 0x000f;
1188 guint possible_ops = (flags & 0x0f0) >> 4;
1190 switch (recommended_op)
1193 context->suggested_action = GDK_ACTION_MOVE;
1196 context->suggested_action = GDK_ACTION_COPY;
1199 context->suggested_action = GDK_ACTION_LINK;
1202 context->suggested_action = GDK_ACTION_COPY;
1206 context->actions = 0;
1207 if (possible_ops & XmDROP_MOVE)
1208 context->actions |= GDK_ACTION_MOVE;
1209 if (possible_ops & XmDROP_COPY)
1210 context->actions |= GDK_ACTION_COPY;
1211 if (possible_ops & XmDROP_LINK)
1212 context->actions |= GDK_ACTION_LINK;
1216 motif_dnd_get_flags (GdkDragContext *context)
1220 switch (context->suggested_action)
1222 case GDK_ACTION_MOVE:
1223 flags = XmDROP_MOVE;
1225 case GDK_ACTION_COPY:
1226 flags = XmDROP_COPY;
1228 case GDK_ACTION_LINK:
1229 flags = XmDROP_LINK;
1232 flags = XmDROP_NOOP;
1236 if (context->actions & GDK_ACTION_MOVE)
1237 flags |= XmDROP_MOVE << 8;
1238 if (context->actions & GDK_ACTION_COPY)
1239 flags |= XmDROP_COPY << 8;
1240 if (context->actions & GDK_ACTION_LINK)
1241 flags |= XmDROP_LINK << 8;
1249 motif_set_targets (GdkDragContext *context)
1251 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1252 MotifDragInitiatorInfo info;
1254 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1256 if (!motif_drag_initiator_info)
1257 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1259 info.byte_order = local_byte_order;
1260 info.protocol_version = 0;
1262 info.targets_index = motif_add_to_target_table (context->targets);
1267 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1269 private->motif_selection = gdk_atom_intern (buf, FALSE);
1270 if (!XGetSelectionOwner (gdk_display, private->motif_selection))
1274 info.selection_atom = private->motif_selection;
1276 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1277 GDK_WINDOW_XWINDOW (context->source_window),
1278 private->motif_selection,
1279 motif_drag_initiator_info, 8, PropModeReplace,
1280 (guchar *)&info, sizeof (info));
1282 private->motif_targets_set = 1;
1286 motif_check_dest (Window win)
1288 gboolean retval = FALSE;
1289 MotifDragReceiverInfo *info;
1292 unsigned long nitems, after;
1294 if (!motif_drag_receiver_info_atom)
1295 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
1297 XGetWindowProperty (gdk_display, win,
1298 motif_drag_receiver_info_atom,
1299 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1300 &type, &format, &nitems, &after,
1305 if ((format == 8) && (nitems == sizeof(*info)))
1307 if ((info->protocol_version == 0) &&
1308 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1309 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1310 (info->protocol_style == XmDRAG_DYNAMIC)))
1316 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1322 return retval ? win : GDK_NONE;
1327 motif_send_enter (GdkDragContext *context,
1331 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1333 xev.xclient.type = ClientMessage;
1334 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1335 xev.xclient.format = 8;
1336 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1338 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1339 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1340 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1341 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1342 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XWINDOW (context->source_window);
1344 if (!private->motif_targets_set)
1345 motif_set_targets (context);
1347 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1349 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1352 g_message ("Send event to %lx failed",
1353 GDK_WINDOW_XWINDOW (context->dest_window)));
1357 motif_send_leave (GdkDragContext *context,
1362 xev.xclient.type = ClientMessage;
1363 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1364 xev.xclient.format = 8;
1365 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1367 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1368 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1369 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1370 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1371 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XWINDOW (context->source_window);
1372 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1374 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1377 g_message ("Send event to %lx failed",
1378 GDK_WINDOW_XWINDOW (context->dest_window)));
1382 motif_send_motion (GdkDragContext *context,
1385 GdkDragAction action,
1390 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1392 xev.xclient.type = ClientMessage;
1393 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1394 xev.xclient.format = 8;
1395 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1397 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1398 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1399 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1401 if ((context->suggested_action != private->old_action) ||
1402 (context->actions != private->old_actions))
1404 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1406 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1411 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1413 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1414 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1416 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1420 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1423 g_message ("Send event to %lx failed",
1424 GDK_WINDOW_XWINDOW (context->dest_window)));
1430 motif_send_drop (GdkDragContext *context, guint32 time)
1433 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1435 xev.xclient.type = ClientMessage;
1436 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1437 xev.xclient.format = 8;
1438 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1440 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1441 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1442 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1443 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1445 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1446 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1448 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1449 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XWINDOW (context->source_window);
1451 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1454 g_message ("Send event to %lx failed",
1455 GDK_WINDOW_XWINDOW (context->dest_window)));
1461 motif_read_initiator_info (Window source_window,
1467 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1473 MotifDragInitiatorInfo *initiator_info;
1475 if (!motif_drag_initiator_info)
1476 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1478 XGetWindowProperty (gdk_display, source_window, atom,
1479 0, sizeof(*initiator_info), FALSE,
1480 motif_drag_initiator_info,
1481 &type, &format, &nitems, &bytes_after,
1482 (guchar **)&initiator_info);
1484 if ((format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1486 g_warning ("Error reading initiator info\n");
1490 motif_read_target_table ();
1492 initiator_info->targets_index =
1493 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1494 initiator_info->selection_atom =
1495 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1497 if (initiator_info->targets_index >= motif_n_target_lists)
1499 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1500 XFree (initiator_info);
1501 return GDK_FILTER_REMOVE;
1504 tmp_list = g_list_last (motif_target_lists[initiator_info->targets_index]);
1509 *targets = g_list_prepend (*targets,
1511 tmp_list = tmp_list->prev;
1514 #ifdef G_ENABLE_DEBUG
1515 if (gdk_debug_flags & GDK_DEBUG_DND)
1516 print_target_list (*targets);
1517 #endif /* G_ENABLE_DEBUG */
1519 *selection = initiator_info->selection_atom;
1521 XFree (initiator_info);
1526 static GdkDragContext *
1527 motif_drag_context_new (GdkWindow *dest_window,
1529 guint32 source_window,
1532 GdkDragContext *new_context;
1533 GdkDragContextPrivate *private;
1535 /* FIXME, current_dest_drag really shouldn't be NULL'd
1536 * if we error below.
1538 if (current_dest_drag != NULL)
1540 if (timestamp >= current_dest_drag->start_time)
1542 gdk_drag_context_unref (current_dest_drag);
1543 current_dest_drag = NULL;
1549 new_context = gdk_drag_context_new ();
1550 private = (GdkDragContextPrivate *)new_context;
1552 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1553 new_context->is_source = FALSE;
1555 new_context->source_window = gdk_window_lookup (source_window);
1556 if (new_context->source_window)
1557 gdk_window_ref (new_context->source_window);
1560 new_context->source_window = gdk_window_foreign_new (source_window);
1561 if (!new_context->source_window)
1563 gdk_drag_context_unref (new_context);
1568 new_context->dest_window = dest_window;
1569 gdk_window_ref (dest_window);
1570 new_context->start_time = timestamp;
1572 if (!motif_read_initiator_info(source_window,
1574 &new_context->targets,
1575 &private->motif_selection))
1577 gdk_drag_context_unref (new_context);
1585 * The MOTIF drag protocol has no real provisions for distinguishing
1586 * multiple simultaneous drops. If the sources grab the pointer
1587 * when doing drags, that shouldn't happen, in any case. If it
1588 * does, we can't do much except hope for the best.
1591 static GdkFilterReturn
1592 motif_top_level_enter (GdkEvent *event,
1595 guint32 source_window,
1598 GdkDragContext *new_context;
1600 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1601 flags, timestamp, source_window, atom));
1603 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1605 return GDK_FILTER_REMOVE;
1607 event->dnd.type = GDK_DRAG_ENTER;
1608 event->dnd.context = new_context;
1609 gdk_drag_context_ref (new_context);
1611 current_dest_drag = new_context;
1613 return GDK_FILTER_TRANSLATE;
1617 motif_top_level_leave (GdkEvent *event,
1621 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1624 if ((current_dest_drag != NULL) &&
1625 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1626 (timestamp >= current_dest_drag->start_time))
1628 event->dnd.type = GDK_DRAG_LEAVE;
1629 /* Pass ownership of context to the event */
1630 event->dnd.context = current_dest_drag;
1632 current_dest_drag = NULL;
1634 return GDK_FILTER_TRANSLATE;
1637 return GDK_FILTER_REMOVE;
1641 motif_motion (GdkEvent *event,
1647 GdkDragContextPrivate *private;
1649 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1650 flags, timestamp, x_root, y_root));
1652 if ((current_dest_drag != NULL) &&
1653 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1654 (timestamp >= current_dest_drag->start_time))
1656 private = (GdkDragContextPrivate *)current_dest_drag;
1658 event->dnd.type = GDK_DRAG_MOTION;
1659 event->dnd.context = current_dest_drag;
1660 gdk_drag_context_ref (current_dest_drag);
1662 event->dnd.time = timestamp;
1664 motif_dnd_translate_flags (current_dest_drag, flags);
1666 event->dnd.x_root = x_root;
1667 event->dnd.y_root = y_root;
1669 private->last_x = x_root;
1670 private->last_y = y_root;
1672 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1674 return GDK_FILTER_TRANSLATE;
1677 return GDK_FILTER_REMOVE;
1681 motif_operation_changed (GdkEvent *event,
1685 GdkDragContextPrivate *private;
1687 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1690 if ((current_dest_drag != NULL) &&
1691 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1692 (timestamp >= current_dest_drag->start_time))
1694 event->dnd.type = GDK_DRAG_MOTION;
1695 event->dnd.send_event = FALSE;
1696 event->dnd.context = current_dest_drag;
1697 gdk_drag_context_ref (current_dest_drag);
1699 event->dnd.time = timestamp;
1700 private = (GdkDragContextPrivate *)current_dest_drag;
1702 motif_dnd_translate_flags (current_dest_drag, flags);
1704 event->dnd.x_root = private->last_x;
1705 event->dnd.y_root = private->last_y;
1707 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1709 return GDK_FILTER_TRANSLATE;
1712 return GDK_FILTER_REMOVE;
1716 motif_drop_start (GdkEvent *event,
1719 guint32 source_window,
1724 GdkDragContext *new_context;
1727 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1728 flags, timestamp, x_root, y_root, source_window, atom));
1730 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1732 return GDK_FILTER_REMOVE;
1734 motif_dnd_translate_flags (new_context, flags);
1736 event->dnd.type = GDK_DROP_START;
1737 event->dnd.context = new_context;
1738 event->dnd.time = timestamp;
1739 event->dnd.x_root = x_root;
1740 event->dnd.y_root = y_root;
1742 gdk_drag_context_ref (new_context);
1743 current_dest_drag = new_context;
1745 return GDK_FILTER_TRANSLATE;
1749 motif_drag_status (GdkEvent *event,
1753 GdkDragContext *context;
1756 g_message ("Motif status message: flags %x", flags));
1758 context = gdk_drag_context_find (TRUE,
1759 GDK_WINDOW_XWINDOW (event->any.window),
1764 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1765 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1766 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1767 private->drag_status = GDK_DRAG_STATUS_DRAG;
1769 event->dnd.type = GDK_DRAG_STATUS;
1770 event->dnd.send_event = FALSE;
1771 event->dnd.context = context;
1772 gdk_drag_context_ref (context);
1774 event->dnd.time = timestamp;
1776 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1778 switch (flags & 0x000f)
1781 context->action = 0;
1784 context->action = GDK_ACTION_MOVE;
1787 context->action = GDK_ACTION_COPY;
1790 context->action = GDK_ACTION_LINK;
1795 context->action = 0;
1797 return GDK_FILTER_TRANSLATE;
1799 return GDK_FILTER_REMOVE;
1803 motif_dnd_filter (GdkXEvent *xev,
1807 XEvent *xevent = (XEvent *)xev;
1812 guint32 source_window;
1814 gint16 x_root, y_root;
1817 /* First read some fields common to all Motif DND messages */
1819 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1820 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1821 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1823 is_reply = ((reason & 0x80) != 0);
1825 switch (reason & 0x7f)
1827 case XmTOP_LEVEL_ENTER:
1828 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1829 atom = MOTIF_UNPACK_LONG (xevent, 3);
1830 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1831 case XmTOP_LEVEL_LEAVE:
1832 return motif_top_level_leave (event, flags, timestamp);
1835 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1836 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1839 return motif_motion (event, flags, timestamp, x_root, y_root);
1841 return motif_drag_status (event, flags, timestamp);
1843 case XmDROP_SITE_ENTER:
1844 return motif_drag_status (event, flags, timestamp);
1846 case XmDROP_SITE_LEAVE:
1847 return motif_drag_status (event,
1848 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1851 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1852 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1853 atom = MOTIF_UNPACK_LONG (xevent, 3);
1854 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1857 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1860 case XmOPERATION_CHANGED:
1862 return motif_operation_changed (event, flags, timestamp);
1864 return motif_drag_status (event, flags, timestamp);
1867 /* To the best of my knowledge, these next two messages are
1868 * not part of the protocol, though they are defined in
1872 case XmDRAG_DROP_FINISH:
1876 return GDK_FILTER_REMOVE;
1879 /*************************************************************
1880 ***************************** XDND **************************
1881 *************************************************************/
1883 /* Utility functions */
1888 GdkDragAction action;
1889 } xdnd_actions_table[] = {
1890 { "XdndActionCopy", GDK_NONE, GDK_ACTION_COPY },
1891 { "XdndActionMove", GDK_NONE, GDK_ACTION_MOVE },
1892 { "XdndActionLink", GDK_NONE, GDK_ACTION_LINK },
1893 { "XdndActionAsk", GDK_NONE, GDK_ACTION_ASK },
1894 { "XdndActionPrivate", GDK_NONE, GDK_ACTION_COPY },
1897 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1898 static gboolean xdnd_actions_initialized = FALSE;
1901 xdnd_initialize_actions (void)
1905 xdnd_actions_initialized = TRUE;
1906 for (i=0; i < xdnd_n_actions; i++)
1907 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1910 static GdkDragAction
1911 xdnd_action_from_atom (GdkAtom atom)
1915 if (!xdnd_actions_initialized)
1916 xdnd_initialize_actions();
1918 for (i=0; i<xdnd_n_actions; i++)
1919 if (atom == xdnd_actions_table[i].atom)
1920 return xdnd_actions_table[i].action;
1926 xdnd_action_to_atom (GdkDragAction action)
1930 if (!xdnd_actions_initialized)
1931 xdnd_initialize_actions();
1933 for (i=0; i<xdnd_n_actions; i++)
1934 if (action == xdnd_actions_table[i].action)
1935 return xdnd_actions_table[i].atom;
1940 static GdkAtom xdnd_aware_atom = GDK_NONE;
1944 static GdkFilterReturn
1945 xdnd_status_filter (GdkXEvent *xev,
1949 XEvent *xevent = (XEvent *)xev;
1950 guint32 dest_window = xevent->xclient.data.l[0];
1951 guint32 flags = xevent->xclient.data.l[1];
1952 GdkAtom action = xevent->xclient.data.l[4];
1953 GdkDragContext *context;
1956 g_message ("XdndStatus: dest_window: %#x action: %ld",
1957 dest_window, action));
1960 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
1963 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1964 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1965 private->drag_status = GDK_DRAG_STATUS_DRAG;
1967 event->dnd.send_event = FALSE;
1968 event->dnd.type = GDK_DRAG_STATUS;
1969 event->dnd.context = context;
1970 gdk_drag_context_ref (context);
1972 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1973 if (!(action != 0) != !(flags & 1))
1976 g_warning ("Received status event with flags not corresponding to action!\n"));
1980 context->action = xdnd_action_from_atom (action);
1982 return GDK_FILTER_TRANSLATE;
1985 return GDK_FILTER_REMOVE;
1988 static GdkFilterReturn
1989 xdnd_finished_filter (GdkXEvent *xev,
1993 XEvent *xevent = (XEvent *)xev;
1994 guint32 dest_window = xevent->xclient.data.l[0];
1995 GdkDragContext *context;
1998 g_message ("XdndFinished: dest_window: %#x", dest_window));
2000 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
2003 event->dnd.type = GDK_DROP_FINISHED;
2004 event->dnd.context = context;
2005 gdk_drag_context_ref (context);
2007 return GDK_FILTER_TRANSLATE;
2010 return GDK_FILTER_REMOVE;
2014 xdnd_set_targets (GdkDragContext *context)
2016 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2018 GList *tmp_list = context->targets;
2020 gint n_atoms = g_list_length (context->targets);
2022 atomlist = g_new (GdkAtom, n_atoms);
2026 atomlist[i] = GPOINTER_TO_INT (tmp_list->data);
2027 tmp_list = tmp_list->next;
2031 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2032 GDK_WINDOW_XWINDOW (context->source_window),
2033 gdk_atom_intern ("XdndTypeList", FALSE),
2034 XA_ATOM, 32, PropModeReplace,
2035 (guchar *)atomlist, n_atoms);
2039 private->xdnd_targets_set = 1;
2043 xdnd_set_actions (GdkDragContext *context)
2045 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2051 if (!xdnd_actions_initialized)
2052 xdnd_initialize_actions();
2054 actions = context->actions;
2056 for (i=0; i<xdnd_n_actions; i++)
2058 if (actions & xdnd_actions_table[i].action)
2060 actions &= ~xdnd_actions_table[i].action;
2065 atomlist = g_new (GdkAtom, n_atoms);
2067 actions = context->actions;
2069 for (i=0; i<xdnd_n_actions; i++)
2071 if (actions & xdnd_actions_table[i].action)
2073 actions &= ~xdnd_actions_table[i].action;
2074 atomlist[n_atoms] = xdnd_actions_table[i].atom;
2079 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2080 GDK_WINDOW_XWINDOW (context->source_window),
2081 gdk_atom_intern ("XdndActionList", FALSE),
2082 XA_ATOM, 32, PropModeReplace,
2083 (guchar *)atomlist, n_atoms);
2087 private->xdnd_actions_set = 1;
2088 private->xdnd_actions = context->actions;
2091 /*************************************************************
2093 * Like gdk_send_event, but if the target is the root
2094 * window, sets an event mask of ButtonPressMask, otherwise
2095 * an event mask of 0.
2099 *************************************************************/
2102 xdnd_send_xevent (Window window, gboolean propagate,
2105 if (window == gdk_root_window)
2106 return gdk_send_xevent (window, propagate, ButtonPressMask, event_send);
2108 return gdk_send_xevent (window, propagate, 0, event_send);
2112 xdnd_send_enter (GdkDragContext *context)
2115 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2117 xev.xclient.type = ClientMessage;
2118 xev.xclient.message_type = gdk_atom_intern ("XdndEnter", FALSE);
2119 xev.xclient.format = 32;
2120 xev.xclient.window = private->drop_xid ?
2122 GDK_WINDOW_XWINDOW (context->dest_window);
2123 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2124 xev.xclient.data.l[1] = (3 << 24); /* version */
2125 xev.xclient.data.l[2] = 0;
2126 xev.xclient.data.l[3] = 0;
2127 xev.xclient.data.l[4] = 0;
2129 if (!private->xdnd_selection)
2130 private->xdnd_selection = gdk_atom_intern ("XdndSelection", FALSE);
2132 if (g_list_length (context->targets) > 3)
2134 if (!private->xdnd_targets_set)
2135 xdnd_set_targets (context);
2136 xev.xclient.data.l[1] |= 1;
2140 GList *tmp_list = context->targets;
2145 xev.xclient.data.l[i] = GPOINTER_TO_INT (tmp_list->data);
2146 tmp_list = tmp_list->next;
2151 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2155 g_message ("Send event to %lx failed",
2156 GDK_WINDOW_XWINDOW (context->dest_window)));
2157 gdk_window_unref (context->dest_window);
2158 context->dest_window = NULL;
2163 xdnd_send_leave (GdkDragContext *context)
2167 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2169 xev.xclient.type = ClientMessage;
2170 xev.xclient.message_type = gdk_atom_intern ("XdndLeave", FALSE);
2171 xev.xclient.format = 32;
2172 xev.xclient.window = private->drop_xid ?
2174 GDK_WINDOW_XWINDOW (context->dest_window);
2175 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2176 xev.xclient.data.l[1] = 0;
2177 xev.xclient.data.l[2] = 0;
2178 xev.xclient.data.l[3] = 0;
2179 xev.xclient.data.l[4] = 0;
2181 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2185 g_message ("Send event to %lx failed",
2186 GDK_WINDOW_XWINDOW (context->dest_window)));
2187 gdk_window_unref (context->dest_window);
2188 context->dest_window = NULL;
2193 xdnd_send_drop (GdkDragContext *context, guint32 time)
2195 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2198 xev.xclient.type = ClientMessage;
2199 xev.xclient.message_type = gdk_atom_intern ("XdndDrop", FALSE);
2200 xev.xclient.format = 32;
2201 xev.xclient.window = private->drop_xid ?
2203 GDK_WINDOW_XWINDOW (context->dest_window);
2204 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2205 xev.xclient.data.l[1] = 0;
2206 xev.xclient.data.l[2] = time;
2207 xev.xclient.data.l[3] = 0;
2208 xev.xclient.data.l[4] = 0;
2210 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2214 g_message ("Send event to %lx failed",
2215 GDK_WINDOW_XWINDOW (context->dest_window)));
2216 gdk_window_unref (context->dest_window);
2217 context->dest_window = NULL;
2222 xdnd_send_motion (GdkDragContext *context,
2225 GdkDragAction action,
2228 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2231 xev.xclient.type = ClientMessage;
2232 xev.xclient.message_type = gdk_atom_intern ("XdndPosition", FALSE);
2233 xev.xclient.format = 32;
2234 xev.xclient.window = private->drop_xid ?
2236 GDK_WINDOW_XWINDOW (context->dest_window);
2237 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2238 xev.xclient.data.l[1] = 0;
2239 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2240 xev.xclient.data.l[3] = time;
2241 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
2243 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2247 g_message ("Send event to %lx failed",
2248 GDK_WINDOW_XWINDOW (context->dest_window)));
2249 gdk_window_unref (context->dest_window);
2250 context->dest_window = NULL;
2252 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2256 xdnd_check_dest (Window win)
2258 gboolean retval = FALSE;
2261 unsigned long nitems, after;
2265 static GdkAtom xdnd_proxy_atom = GDK_NONE;
2267 gint old_warnings = gdk_error_warnings;
2269 if (!xdnd_proxy_atom)
2270 xdnd_proxy_atom = gdk_atom_intern ("XdndProxy", FALSE);
2272 if (!xdnd_aware_atom)
2273 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
2278 gdk_error_warnings = 0;
2280 XGetWindowProperty (gdk_display, win,
2282 1, False, AnyPropertyType,
2283 &type, &format, &nitems, &after,
2284 (guchar **)&proxy_data);
2286 if (!gdk_error_code)
2290 if ((format == 32) && (nitems == 1))
2292 proxy = *proxy_data;
2296 g_warning ("Invalid XdndOwner property on window %ld\n", win));
2301 XGetWindowProperty (gdk_display, proxy ? proxy : win,
2303 1, False, AnyPropertyType,
2304 &type, &format, &nitems, &after,
2305 (guchar **)&version);
2307 if (!gdk_error_code && type != None)
2309 if ((format == 32) && (nitems == 1))
2316 g_warning ("Invalid XdndAware property on window %ld\n", win));
2323 gdk_error_warnings = old_warnings;
2326 return retval ? (proxy ? proxy : win) : GDK_NONE;
2332 xdnd_read_actions (GdkDragContext *context)
2336 gulong nitems, after;
2341 gint old_warnings = gdk_error_warnings;
2344 gdk_error_warnings = 0;
2346 /* Get the XdndActionList, if set */
2348 XGetWindowProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2349 GDK_WINDOW_XWINDOW (context->source_window),
2350 gdk_atom_intern ("XdndActionList", FALSE), 0, 65536,
2351 False, XA_ATOM, &type, &format, &nitems,
2352 &after, (guchar **)&data);
2354 if (!gdk_error_code && (format == 32) && (type == XA_ATOM))
2356 context->actions = 0;
2358 for (i=0; i<nitems; i++)
2359 context->actions |= xdnd_action_from_atom (data[i]);
2361 ((GdkDragContextPrivate *)context)->xdnd_have_actions = TRUE;
2363 #ifdef G_ENABLE_DEBUG
2364 if (gdk_debug_flags & GDK_DEBUG_DND)
2366 GString *action_str = g_string_new (NULL);
2367 if (context->actions & GDK_ACTION_MOVE)
2368 g_string_append(action_str, "MOVE ");
2369 if (context->actions & GDK_ACTION_COPY)
2370 g_string_append(action_str, "COPY ");
2371 if (context->actions & GDK_ACTION_LINK)
2372 g_string_append(action_str, "LINK ");
2373 if (context->actions & GDK_ACTION_ASK)
2374 g_string_append(action_str, "ASK ");
2376 g_message("Xdnd actions = %s", action_str->str);
2377 g_string_free (action_str, TRUE);
2379 #endif /* G_ENABLE_DEBUG */
2384 gdk_error_warnings = old_warnings;
2388 /* We have to make sure that the XdndActionList we keep internally
2389 * is up to date with the XdndActionList on the source window
2390 * because we get no notification, because Xdnd wasn't meant
2391 * to continually send actions. So we select on PropertyChangeMask
2392 * and add this filter.
2394 static GdkFilterReturn
2395 xdnd_source_window_filter (GdkXEvent *xev,
2399 XEvent *xevent = (XEvent *)xev;
2400 GdkDragContext *context = cb_data;
2402 if ((xevent->xany.type == PropertyNotify) &&
2403 (xevent->xproperty.atom == gdk_atom_intern ("XdndActionList", FALSE)))
2405 xdnd_read_actions (context);
2407 return GDK_FILTER_REMOVE;
2410 return GDK_FILTER_CONTINUE;
2414 xdnd_manage_source_filter (GdkDragContext *context,
2416 gboolean add_filter)
2418 gint old_warnings = 0; /* quiet gcc */
2420 gboolean is_foreign = GDK_DRAWABLE_TYPE (window);
2424 old_warnings = gdk_error_warnings;
2425 gdk_error_warnings = 0;
2428 if (!GDK_DRAWABLE_DESTROYED (window))
2432 gdk_window_set_events (window,
2433 gdk_window_get_events (window) |
2434 GDK_PROPERTY_CHANGE_MASK);
2435 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2440 gdk_window_remove_filter (window,
2441 xdnd_source_window_filter,
2443 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2444 * but we might want it for other reasons. (Like
2445 * INCR selection transactions).
2453 gdk_error_warnings = old_warnings;
2457 static GdkFilterReturn
2458 xdnd_enter_filter (GdkXEvent *xev,
2462 XEvent *xevent = (XEvent *)xev;
2463 GdkDragContext *new_context;
2468 gulong nitems, after;
2471 guint32 source_window = xevent->xclient.data.l[0];
2472 gboolean get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2473 gint version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2476 g_message ("XdndEnter: source_window: %#x, version: %#x",
2477 source_window, version));
2481 /* Old source ignore */
2482 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2483 return GDK_FILTER_REMOVE;
2486 if (current_dest_drag != NULL)
2488 gdk_drag_context_unref (current_dest_drag);
2489 current_dest_drag = NULL;
2492 new_context = gdk_drag_context_new ();
2493 new_context->protocol = GDK_DRAG_PROTO_XDND;
2494 new_context->is_source = FALSE;
2496 new_context->source_window = gdk_window_lookup (source_window);
2497 if (new_context->source_window)
2498 gdk_window_ref (new_context->source_window);
2501 new_context->source_window = gdk_window_foreign_new (source_window);
2502 if (!new_context->source_window)
2504 gdk_drag_context_unref (new_context);
2505 return GDK_FILTER_REMOVE;
2508 new_context->dest_window = event->any.window;
2509 gdk_window_ref (new_context->dest_window);
2511 new_context->targets = NULL;
2514 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2516 gdk_atom_intern ("XdndTypeList", FALSE), 0, 65536,
2517 False, XA_ATOM, &type, &format, &nitems,
2518 &after, (guchar **)&data);
2520 if ((format != 32) || (type != XA_ATOM))
2522 gdk_drag_context_unref (new_context);
2523 return GDK_FILTER_REMOVE;
2526 for (i=0; i<nitems; i++)
2527 new_context->targets = g_list_append (new_context->targets,
2528 GUINT_TO_POINTER (data[i]));
2535 if (xevent->xclient.data.l[2+i])
2536 new_context->targets = g_list_append (new_context->targets,
2537 GUINT_TO_POINTER (xevent->xclient.data.l[2+i]));
2540 #ifdef G_ENABLE_DEBUG
2541 if (gdk_debug_flags & GDK_DEBUG_DND)
2542 print_target_list (new_context->targets);
2543 #endif /* G_ENABLE_DEBUG */
2545 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2546 xdnd_read_actions (new_context);
2548 event->dnd.type = GDK_DRAG_ENTER;
2549 event->dnd.context = new_context;
2550 gdk_drag_context_ref (new_context);
2552 current_dest_drag = new_context;
2553 ((GdkDragContextPrivate *)new_context)->xdnd_selection =
2554 gdk_atom_intern ("XdndSelection", FALSE);
2556 return GDK_FILTER_TRANSLATE;
2559 static GdkFilterReturn
2560 xdnd_leave_filter (GdkXEvent *xev,
2564 XEvent *xevent = (XEvent *)xev;
2565 guint32 source_window = xevent->xclient.data.l[0];
2568 g_message ("XdndLeave: source_window: %#x",
2571 if ((current_dest_drag != NULL) &&
2572 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2573 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2575 event->dnd.type = GDK_DRAG_LEAVE;
2576 /* Pass ownership of context to the event */
2577 event->dnd.context = current_dest_drag;
2579 current_dest_drag = NULL;
2581 return GDK_FILTER_TRANSLATE;
2584 return GDK_FILTER_REMOVE;
2587 static GdkFilterReturn
2588 xdnd_position_filter (GdkXEvent *xev,
2592 XEvent *xevent = (XEvent *)xev;
2593 guint32 source_window = xevent->xclient.data.l[0];
2594 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2595 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2596 guint32 time = xevent->xclient.data.l[3];
2597 GdkAtom action = xevent->xclient.data.l[4];
2600 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2601 source_window, x_root, y_root, time, action));
2604 if ((current_dest_drag != NULL) &&
2605 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2606 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2608 event->dnd.type = GDK_DRAG_MOTION;
2609 event->dnd.context = current_dest_drag;
2610 gdk_drag_context_ref (current_dest_drag);
2612 event->dnd.time = time;
2614 current_dest_drag->suggested_action = xdnd_action_from_atom (action);
2615 if (!((GdkDragContextPrivate *)current_dest_drag)->xdnd_have_actions)
2616 current_dest_drag->actions = current_dest_drag->suggested_action;
2618 event->dnd.x_root = x_root;
2619 event->dnd.y_root = y_root;
2621 ((GdkDragContextPrivate *)current_dest_drag)->last_x = x_root;
2622 ((GdkDragContextPrivate *)current_dest_drag)->last_y = y_root;
2624 return GDK_FILTER_TRANSLATE;
2627 return GDK_FILTER_REMOVE;
2630 static GdkFilterReturn
2631 xdnd_drop_filter (GdkXEvent *xev,
2635 XEvent *xevent = (XEvent *)xev;
2636 guint32 source_window = xevent->xclient.data.l[0];
2637 guint32 time = xevent->xclient.data.l[2];
2640 g_message ("XdndDrop: source_window: %#x time: %d",
2641 source_window, time));
2643 if ((current_dest_drag != NULL) &&
2644 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2645 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2647 GdkDragContextPrivate *private;
2648 private = (GdkDragContextPrivate *)current_dest_drag;
2650 event->dnd.type = GDK_DROP_START;
2652 event->dnd.context = current_dest_drag;
2653 gdk_drag_context_ref (current_dest_drag);
2655 event->dnd.time = time;
2656 event->dnd.x_root = private->last_x;
2657 event->dnd.y_root = private->last_y;
2659 return GDK_FILTER_TRANSLATE;
2662 return GDK_FILTER_REMOVE;
2665 /*************************************************************
2666 ************************** Public API ***********************
2667 *************************************************************/
2674 gdk_add_client_message_filter (
2675 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2676 motif_dnd_filter, NULL);
2677 gdk_add_client_message_filter (
2678 gdk_atom_intern ("XdndEnter", FALSE),
2679 xdnd_enter_filter, NULL);
2680 gdk_add_client_message_filter (
2681 gdk_atom_intern ("XdndLeave", FALSE),
2682 xdnd_leave_filter, NULL);
2683 gdk_add_client_message_filter (
2684 gdk_atom_intern ("XdndPosition", FALSE),
2685 xdnd_position_filter, NULL);
2686 gdk_add_client_message_filter (
2687 gdk_atom_intern ("XdndStatus", FALSE),
2688 xdnd_status_filter, NULL);
2689 gdk_add_client_message_filter (
2690 gdk_atom_intern ("XdndFinished", FALSE),
2691 xdnd_finished_filter, NULL);
2692 gdk_add_client_message_filter (
2693 gdk_atom_intern ("XdndDrop", FALSE),
2694 xdnd_drop_filter, NULL);
2700 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2702 if (context->dest_window)
2704 switch (context->protocol)
2706 case GDK_DRAG_PROTO_MOTIF:
2707 motif_send_leave (context, time);
2709 case GDK_DRAG_PROTO_XDND:
2710 xdnd_send_leave (context);
2712 case GDK_DRAG_PROTO_ROOTWIN:
2713 case GDK_DRAG_PROTO_NONE:
2717 gdk_window_unref (context->dest_window);
2718 context->dest_window = NULL;
2723 gdk_drag_begin (GdkWindow *window,
2727 GdkDragContext *new_context;
2729 g_return_val_if_fail (window != NULL, NULL);
2731 new_context = gdk_drag_context_new ();
2732 new_context->is_source = TRUE;
2733 new_context->source_window = window;
2734 gdk_window_ref (window);
2736 tmp_list = g_list_last (targets);
2737 new_context->targets = NULL;
2740 new_context->targets = g_list_prepend (new_context->targets,
2742 tmp_list = tmp_list->prev;
2745 new_context->actions = 0;
2751 gdk_drag_get_protocol (guint32 xid,
2752 GdkDragProtocol *protocol)
2756 if ((retval = xdnd_check_dest (xid)))
2758 *protocol = GDK_DRAG_PROTO_XDND;
2759 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2762 else if ((retval = motif_check_dest (xid)))
2764 *protocol = GDK_DRAG_PROTO_MOTIF;
2765 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2770 /* Check if this is a root window */
2772 gboolean rootwin = FALSE;
2773 gint old_warnings = gdk_error_warnings;
2776 unsigned long nitems, after;
2777 unsigned char *data;
2779 if (xid == gdk_root_window)
2786 XGetWindowProperty (gdk_display, xid,
2787 gdk_atom_intern ("ENLIGHTENMENT_DESKTOP", FALSE),
2788 0, 0, False, AnyPropertyType,
2789 &type, &format, &nitems, &after, &data);
2790 if ((gdk_error_code == 0) && type != None)
2797 /* Until I find out what window manager the next one is for,
2798 * I'm leaving it commented out. It's supported in the
2799 * xscreensaver sources, though.
2806 XGetWindowProperty (gdk_display, win,
2807 gdk_atom_intern ("__SWM_VROOT", FALSE),
2808 0, 0, False, AnyPropertyType,
2809 &type, &format, &nitems, &data);
2810 if ((gdk_error_code == 0) && type != None)
2815 gdk_error_warnings = old_warnings;
2819 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2824 *protocol = GDK_DRAG_PROTO_NONE;
2829 gdk_drag_find_window (GdkDragContext *context,
2830 GdkWindow *drag_window,
2833 GdkWindow **dest_window,
2834 GdkDragProtocol *protocol)
2836 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2839 g_return_if_fail (context != NULL);
2841 if (!private->window_cache)
2842 private->window_cache = gdk_window_cache_new();
2844 dest = get_client_window_at_coords (private->window_cache,
2846 GDK_WINDOW_XWINDOW (drag_window) : GDK_NONE,
2849 if (private->dest_xid != dest)
2852 private->dest_xid = dest;
2854 /* Check if new destination accepts drags, and which protocol */
2856 /* There is some ugliness here. We actually need to pass
2857 * _three_ pieces of information to drag_motion - dest_window,
2858 * protocol, and the XID of the unproxied window. The first
2859 * two are passed explicitely, the third implicitly through
2860 * protocol->dest_xid.
2862 if ((recipient = gdk_drag_get_protocol (dest, protocol)))
2864 *dest_window = gdk_window_lookup (recipient);
2866 gdk_window_ref (*dest_window);
2868 *dest_window = gdk_window_foreign_new (recipient);
2871 *dest_window = NULL;
2875 *dest_window = context->dest_window;
2877 gdk_window_ref (*dest_window);
2878 *protocol = context->protocol;
2883 gdk_drag_motion (GdkDragContext *context,
2884 GdkWindow *dest_window,
2885 GdkDragProtocol protocol,
2888 GdkDragAction suggested_action,
2889 GdkDragAction possible_actions,
2892 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2894 g_return_val_if_fail (context != NULL, FALSE);
2896 /* When we have a Xdnd target, make sure our XdndActionList
2897 * matches the current actions;
2899 private->old_actions = context->actions;
2900 context->actions = possible_actions;
2902 if ((protocol == GDK_DRAG_PROTO_XDND) &&
2903 (!private->xdnd_actions_set ||
2904 private->xdnd_actions != possible_actions))
2905 xdnd_set_actions (context);
2907 if (context->dest_window != dest_window)
2909 GdkEvent temp_event;
2911 /* Send a leave to the last destination */
2912 gdk_drag_do_leave (context, time);
2913 private->drag_status = GDK_DRAG_STATUS_DRAG;
2915 /* Check if new destination accepts drags, and which protocol */
2919 context->dest_window = dest_window;
2920 private->drop_xid = private->dest_xid;
2921 gdk_window_ref (context->dest_window);
2922 context->protocol = protocol;
2926 case GDK_DRAG_PROTO_MOTIF:
2927 motif_send_enter (context, time);
2930 case GDK_DRAG_PROTO_XDND:
2931 xdnd_send_enter (context);
2934 case GDK_DRAG_PROTO_ROOTWIN:
2935 case GDK_DRAG_PROTO_NONE:
2938 private->old_action = suggested_action;
2939 context->suggested_action = suggested_action;
2940 private->old_actions = possible_actions;
2944 context->dest_window = NULL;
2945 private->drop_xid = None;
2946 context->action = 0;
2949 /* Push a status event, to let the client know that
2953 temp_event.dnd.type = GDK_DRAG_STATUS;
2954 temp_event.dnd.window = context->source_window;
2955 /* We use this to signal a synthetic status. Perhaps
2956 * we should use an extra field...
2958 temp_event.dnd.send_event = TRUE;
2960 temp_event.dnd.context = context;
2961 temp_event.dnd.time = time;
2963 gdk_event_put (&temp_event);
2967 private->old_action = context->suggested_action;
2968 context->suggested_action = suggested_action;
2971 /* Send a drag-motion event */
2973 private->last_x = x_root;
2974 private->last_y = y_root;
2976 if (context->dest_window)
2978 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
2980 switch (context->protocol)
2982 case GDK_DRAG_PROTO_MOTIF:
2983 motif_send_motion (context, x_root, y_root, suggested_action, time);
2986 case GDK_DRAG_PROTO_XDND:
2987 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
2990 case GDK_DRAG_PROTO_ROOTWIN:
2992 GdkEvent temp_event;
2994 if (g_list_find (context->targets,
2995 GUINT_TO_POINTER (gdk_atom_intern ("application/x-rootwin-drop", FALSE))))
2996 context->action = context->suggested_action;
2998 context->action = 0;
3000 temp_event.dnd.type = GDK_DRAG_STATUS;
3001 temp_event.dnd.window = context->source_window;
3002 temp_event.dnd.send_event = FALSE;
3003 temp_event.dnd.context = context;
3004 temp_event.dnd.time = time;
3006 gdk_event_put (&temp_event);
3009 case GDK_DRAG_PROTO_NONE:
3010 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3022 gdk_drag_drop (GdkDragContext *context,
3025 g_return_if_fail (context != NULL);
3027 if (context->dest_window)
3029 switch (context->protocol)
3031 case GDK_DRAG_PROTO_MOTIF:
3032 motif_send_leave (context, time);
3033 motif_send_drop (context, time);
3036 case GDK_DRAG_PROTO_XDND:
3037 xdnd_send_drop (context, time);
3040 case GDK_DRAG_PROTO_ROOTWIN:
3041 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3043 case GDK_DRAG_PROTO_NONE:
3044 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3051 gdk_drag_abort (GdkDragContext *context,
3054 g_return_if_fail (context != NULL);
3056 gdk_drag_do_leave (context, time);
3059 /* Destination side */
3062 gdk_drag_status (GdkDragContext *context,
3063 GdkDragAction action,
3066 GdkDragContextPrivate *private;
3069 g_return_if_fail (context != NULL);
3071 private = (GdkDragContextPrivate *)context;
3073 context->action = action;
3075 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3077 xev.xclient.type = ClientMessage;
3078 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
3079 xev.xclient.format = 8;
3080 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
3082 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3084 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3088 if ((action != 0) != (private->old_action != 0))
3091 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3093 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3096 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3099 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3103 case GDK_ACTION_MOVE:
3104 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3106 case GDK_ACTION_COPY:
3107 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3109 case GDK_ACTION_LINK:
3110 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3113 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3118 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3120 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3122 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3123 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3124 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3126 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3129 g_message ("Send event to %lx failed",
3130 GDK_WINDOW_XWINDOW (context->source_window)));
3132 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3134 xev.xclient.type = ClientMessage;
3135 xev.xclient.message_type = gdk_atom_intern ("XdndStatus", FALSE);
3136 xev.xclient.format = 32;
3137 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
3139 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->dest_window);
3140 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3141 xev.xclient.data.l[2] = 0;
3142 xev.xclient.data.l[3] = 0;
3143 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
3145 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3148 g_message ("Send event to %lx failed",
3149 GDK_WINDOW_XWINDOW (context->source_window)));
3152 private->old_action = action;
3156 gdk_drop_reply (GdkDragContext *context,
3160 GdkDragContextPrivate *private;
3162 g_return_if_fail (context != NULL);
3164 private = (GdkDragContextPrivate *)context;
3166 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3170 xev.xclient.type = ClientMessage;
3171 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
3172 xev.xclient.format = 8;
3174 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3175 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3177 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3178 (XmDROP_SITE_VALID << 4) |
3179 (XmDROP_NOOP << 8) |
3182 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3183 (XmNO_DROP_SITE << 4) |
3184 (XmDROP_NOOP << 8) |
3185 (XmDROP_CANCEL << 12);
3186 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3187 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3189 gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3195 gdk_drop_finish (GdkDragContext *context,
3199 g_return_if_fail (context != NULL);
3201 if (context->protocol == GDK_DRAG_PROTO_XDND)
3205 xev.xclient.type = ClientMessage;
3206 xev.xclient.message_type = gdk_atom_intern ("XdndFinished", FALSE);
3207 xev.xclient.format = 32;
3208 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
3210 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->dest_window);
3211 xev.xclient.data.l[1] = 0;
3212 xev.xclient.data.l[2] = 0;
3213 xev.xclient.data.l[3] = 0;
3214 xev.xclient.data.l[4] = 0;
3216 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3219 g_message ("Send event to %lx failed",
3220 GDK_WINDOW_XWINDOW (context->source_window)));
3226 gdk_window_register_dnd (GdkWindow *window)
3228 static gulong xdnd_version = 3;
3229 MotifDragReceiverInfo info;
3231 g_return_if_fail (window != NULL);
3233 /* Set Motif drag receiver information property */
3235 if (!motif_drag_receiver_info_atom)
3236 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
3238 info.byte_order = local_byte_order;
3239 info.protocol_version = 0;
3240 info.protocol_style = XmDRAG_DYNAMIC;
3241 info.proxy_window = GDK_NONE;
3242 info.num_drop_sites = 0;
3243 info.total_size = sizeof(info);
3245 XChangeProperty (gdk_display, GDK_WINDOW_XWINDOW (window),
3246 motif_drag_receiver_info_atom,
3247 motif_drag_receiver_info_atom,
3254 if (!xdnd_aware_atom)
3255 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
3257 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3258 XChangeProperty (GDK_WINDOW_XDISPLAY (window),
3259 GDK_WINDOW_XWINDOW (window),
3260 xdnd_aware_atom, XA_ATOM,
3261 32, PropModeReplace,
3262 (guchar *)&xdnd_version, 1);
3265 /*************************************************************
3266 * gdk_drag_get_selection:
3267 * Returns the selection atom for the current source window
3271 *************************************************************/
3274 gdk_drag_get_selection (GdkDragContext *context)
3276 g_return_val_if_fail (context != NULL, GDK_NONE);
3278 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3279 return ((GdkDragContextPrivate *)context)->motif_selection;
3280 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3281 return ((GdkDragContextPrivate *)context)->xdnd_selection;