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/gdkprivate.h"
36 typedef struct _GdkDragContextPrivate GdkDragContextPrivate;
40 GDK_DRAG_STATUS_MOTION_WAIT,
41 GDK_DRAG_STATUS_ACTION_WAIT,
47 gint x, y, width, height;
53 GHashTable *child_hash;
57 /* Structure that holds information about a drag in progress.
58 * this is used on both source and destination sides.
60 struct _GdkDragContextPrivate {
61 GdkDragContext context;
63 GdkAtom motif_selection;
64 GdkAtom xdnd_selection;
67 guint16 last_x; /* Coordinates from last event */
69 GdkDragAction old_action; /* The last action we sent to the source */
70 GdkDragAction old_actions; /* The last actions we sent to the source */
71 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
74 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
75 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
76 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
77 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
78 guint drag_status : 4; /* current status of drag */
80 GdkWindowCache *window_cache;
83 GdkDragContext *current_dest_drag = NULL;
85 /* Forward declarations */
87 static void gdk_window_cache_destroy (GdkWindowCache *cache);
89 static void motif_read_target_table (void);
90 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
94 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
98 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
102 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
106 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
110 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
114 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
118 static void xdnd_manage_source_filter (GdkDragContext *context,
120 gboolean add_filter);
124 static GList *contexts;
127 gdk_drag_context_new (void)
129 GdkDragContextPrivate *result;
131 result = g_new0 (GdkDragContextPrivate, 1);
133 result->ref_count = 1;
135 contexts = g_list_prepend (contexts, result);
137 return (GdkDragContext *)result;
141 gdk_drag_context_ref (GdkDragContext *context)
143 g_return_if_fail (context != NULL);
145 ((GdkDragContextPrivate *)context)->ref_count++;
149 gdk_drag_context_unref (GdkDragContext *context)
151 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
153 g_return_if_fail (context != NULL);
155 private->ref_count--;
157 if (private->ref_count == 0)
159 g_dataset_destroy (private);
161 g_list_free (context->targets);
163 if (context->source_window)
165 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
167 xdnd_manage_source_filter (context, context->source_window, FALSE);
169 gdk_window_unref (context->source_window);
172 if (context->dest_window)
173 gdk_window_unref (context->dest_window);
175 if (private->window_cache)
176 gdk_window_cache_destroy (private->window_cache);
178 contexts = g_list_remove (contexts, private);
183 static GdkDragContext *
184 gdk_drag_context_find (gboolean is_source,
188 GList *tmp_list = contexts;
189 GdkDragContext *context;
190 GdkDragContextPrivate *private;
191 Window context_dest_xid;
195 context = (GdkDragContext *)tmp_list->data;
196 private = (GdkDragContextPrivate *)context;
198 context_dest_xid = context->dest_window ?
201 GDK_WINDOW_XWINDOW (context->dest_window)) :
204 if ((!context->is_source == !is_source) &&
205 ((source_xid == None) || (context->source_window &&
206 (GDK_WINDOW_XWINDOW (context->source_window) == source_xid))) &&
207 ((dest_xid == None) || (context_dest_xid == dest_xid)))
210 tmp_list = tmp_list->next;
216 /* Utility functions */
219 gdk_window_cache_add (GdkWindowCache *cache,
221 gint x, gint y, gint width, gint height,
224 GdkCacheChild *child = g_new (GdkCacheChild, 1);
229 child->width = width;
230 child->height = height;
231 child->mapped = mapped;
233 cache->children = g_list_prepend (cache->children, child);
234 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
238 static GdkFilterReturn
239 gdk_window_cache_filter (GdkXEvent *xev,
243 XEvent *xevent = (XEvent *)xev;
244 GdkWindowCache *cache = data;
246 switch (xevent->type)
248 case CirculateNotify:
250 case ConfigureNotify:
252 XConfigureEvent *xce = &xevent->xconfigure;
255 node = g_hash_table_lookup (cache->child_hash,
256 GUINT_TO_POINTER (xce->window));
259 GdkCacheChild *child = node->data;
262 child->width = xce->width;
263 child->height = xce->height;
264 if (xce->above == None && (node->next))
266 GList *last = g_list_last (cache->children);
267 cache->children = g_list_remove_link (cache->children, node);
274 GList *above_node = g_hash_table_lookup (cache->child_hash,
275 GUINT_TO_POINTER (xce->above));
276 if (above_node && node->prev != above_node)
278 cache->children = g_list_remove_link (cache->children, node);
279 node->next = above_node->next;
281 node->next->prev = node;
282 node->prev = above_node;
283 above_node->next = node;
291 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
293 if (!g_hash_table_lookup (cache->child_hash,
294 GUINT_TO_POINTER (xcwe->window)))
295 gdk_window_cache_add (cache, xcwe->window,
296 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
302 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
305 node = g_hash_table_lookup (cache->child_hash,
306 GUINT_TO_POINTER (xdwe->window));
309 g_hash_table_remove (cache->child_hash,
310 GUINT_TO_POINTER (xdwe->window));
311 cache->children = g_list_remove_link (cache->children, node);
313 g_list_free_1 (node);
319 XMapEvent *xme = &xevent->xmap;
322 node = g_hash_table_lookup (cache->child_hash,
323 GUINT_TO_POINTER (xme->window));
326 GdkCacheChild *child = node->data;
327 child->mapped = TRUE;
335 XMapEvent *xume = &xevent->xmap;
338 node = g_hash_table_lookup (cache->child_hash,
339 GUINT_TO_POINTER (xume->window));
342 GdkCacheChild *child = node->data;
343 child->mapped = FALSE;
348 return GDK_FILTER_CONTINUE;
350 return GDK_FILTER_REMOVE;
353 static GdkWindowCache *
354 gdk_window_cache_new (void)
356 XWindowAttributes xwa;
357 Window root, parent, *children;
358 unsigned int nchildren;
361 gint old_warnings = gdk_error_warnings;
363 GdkWindowCache *result = g_new (GdkWindowCache, 1);
365 result->children = NULL;
366 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
368 XGetWindowAttributes (gdk_display, gdk_root_window, &xwa);
369 result->old_event_mask = xwa.your_event_mask;
370 XSelectInput (gdk_display, gdk_root_window,
371 result->old_event_mask | SubstructureNotifyMask);
372 gdk_window_add_filter ((GdkWindow *)&gdk_root_parent,
373 gdk_window_cache_filter, result);
376 gdk_error_warnings = 0;
378 if (XQueryTree(gdk_display, gdk_root_window,
379 &root, &parent, &children, &nchildren) == 0)
382 for (i = 0; i < nchildren ; i++)
384 XGetWindowAttributes (gdk_display, children[i], &xwa);
386 gdk_window_cache_add (result, children[i],
387 xwa.x, xwa.y, xwa.width, xwa.height,
388 xwa.map_state != IsUnmapped);
390 if (gdk_error_code != 0)
394 gdk_window_cache_add (result, children[i],
395 xwa.x, xwa.y, xwa.width, xwa.height,
396 (xwa.map_state != IsUnmapped));
402 gdk_error_warnings = old_warnings;
408 gdk_window_cache_destroy (GdkWindowCache *cache)
410 XSelectInput (gdk_display, gdk_root_window, cache->old_event_mask);
411 gdk_window_remove_filter ((GdkWindow *)&gdk_root_parent,
412 gdk_window_cache_filter, cache);
414 g_list_foreach (cache->children, (GFunc)g_free, NULL);
415 g_list_free (cache->children);
416 g_hash_table_destroy (cache->child_hash);
422 get_client_window_at_coords_recurse (Window win,
426 Window root, tmp_parent, *children;
427 unsigned int nchildren;
432 unsigned long nitems, after;
435 static Atom wm_state_atom = None;
438 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
440 XGetWindowProperty (gdk_display, win,
441 wm_state_atom, 0, 0, False, AnyPropertyType,
442 &type, &format, &nitems, &after, &data);
444 if (gdk_error_code != 0)
457 /* This is beautiful! Damn Enlightenment and click-to-focus */
458 XTranslateCoordinates (gdk_display, gdk_root_window, win,
459 x_root, y_root, &dest_x, &dest_y, &child);
461 if (gdk_error_code != 0)
468 if (XQueryTree(gdk_display, win,
469 &root, &tmp_parent, &children, &nchildren) == 0)
472 if (gdk_error_code == 0)
474 for (i = nchildren - 1; (i >= 0) && (child == None); i--)
476 XWindowAttributes xwa;
478 XGetWindowAttributes (gdk_display, children[i], &xwa);
480 if (gdk_error_code != 0)
482 else if ((xwa.map_state == IsViewable) && (xwa.class == InputOutput) &&
483 (x >= xwa.x) && (x < xwa.x + (gint)xwa.width) &&
484 (y >= xwa.y) && (y < xwa.y + (gint)xwa.height))
499 return get_client_window_at_coords_recurse (child, x, y);
505 get_client_window_at_coords (GdkWindowCache *cache,
511 Window retval = None;
513 gint old_warnings = gdk_error_warnings;
516 gdk_error_warnings = 0;
518 tmp_list = cache->children;
520 while (tmp_list && !retval)
522 GdkCacheChild *child = tmp_list->data;
524 if ((child->xid != ignore) && (child->mapped))
526 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
527 (y_root >= child->y) && (y_root < child->y + child->height))
529 retval = get_client_window_at_coords_recurse (child->xid,
537 tmp_list = tmp_list->next;
540 gdk_error_warnings = old_warnings;
544 return gdk_root_window;
549 get_client_window_at_coords_recurse (Window win,
556 unsigned long nitems, after;
560 static Atom wm_state_atom = None;
563 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
565 XGetWindowProperty (gdk_display, win,
566 wm_state_atom, 0, 0, False, AnyPropertyType,
567 &type, &format, &nitems, &after, &data);
569 if (gdk_error_code != 0)
581 XTranslateCoordinates (gdk_display, gdk_root_window, win,
582 x_root, y_root, &dest_x, &dest_y, &child);
584 if (gdk_error_code != 0)
591 return get_client_window_at_coords_recurse (child, x_root, y_root);
597 get_client_window_at_coords (Window ignore,
601 Window root, parent, *children;
602 unsigned int nchildren;
604 Window retval = None;
606 gint old_warnings = gdk_error_warnings;
609 gdk_error_warnings = 0;
611 if (XQueryTree(gdk_display, gdk_root_window,
612 &root, &parent, &children, &nchildren) == 0)
615 for (i = nchildren - 1; (i >= 0) && (retval == None); i--)
617 if (children[i] != ignore)
619 XWindowAttributes xwa;
621 XGetWindowAttributes (gdk_display, children[i], &xwa);
623 if (gdk_error_code != 0)
625 else if ((xwa.map_state == IsViewable) &&
626 (x_root >= xwa.x) && (x_root < xwa.x + (gint)xwa.width) &&
627 (y_root >= xwa.y) && (y_root < xwa.y + (gint)xwa.height))
629 retval = get_client_window_at_coords_recurse (children[i],
632 retval = children[i];
639 gdk_error_warnings = old_warnings;
643 return gdk_root_window;
647 /*************************************************************
648 ***************************** MOTIF *************************
649 *************************************************************/
651 /* values used in the message type for Motif DND */
664 /* Values used to specify type of protocol to use */
668 XmDRAG_PREFER_PREREGISTER,
670 XmDRAG_PREFER_DYNAMIC,
672 XmDRAG_PREFER_RECEIVER
675 /* Operation codes */
683 /* Drop site status */
685 XmNO_DROP_SITE = 0x01,
686 XmDROP_SITE_INVALID = 0x02,
687 XmDROP_SITE_VALID = 0x03
690 /* completion status */
698 /* Static data for MOTIF DND */
699 static GList **motif_target_lists = NULL;
700 static gint motif_n_target_lists = -1;
702 /* Byte swapping routines. The motif specification leaves it
703 * up to us to save a few bytes in the client messages
705 static gchar local_byte_order = '\0';
707 #ifdef G_ENABLE_DEBUG
709 print_target_list (GList *targets)
713 gchar *name = gdk_atom_name (GPOINTER_TO_INT (targets->data));
714 g_message ("\t%s", name);
716 targets = targets->next;
719 #endif /* G_ENABLE_DEBUG */
722 init_byte_order (void)
724 guint32 myint = 0x01020304;
725 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
729 card16_to_host (guint16 x, gchar byte_order) {
730 if (byte_order == local_byte_order)
733 return (x << 8) | (x >> 8);
737 card32_to_host (guint32 x, gchar byte_order) {
738 if (byte_order == local_byte_order)
741 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
744 /* Motif packs together fields of varying length into the
745 * client message. We can't rely on accessing these
746 * through data.s[], data.l[], etc, because on some architectures
747 * (i.e., Alpha) these won't be valid for format == 8.
750 #define MOTIF_XCLIENT_BYTE(xevent,i) \
751 (xevent)->xclient.data.b[i]
752 #define MOTIF_XCLIENT_SHORT(xevent,i) \
753 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
754 #define MOTIF_XCLIENT_LONG(xevent,i) \
755 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
757 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
758 #define MOTIF_UNPACK_SHORT(xevent,i) \
759 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
760 #define MOTIF_UNPACK_LONG(xevent,i) \
761 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
763 /***** Dest side ***********/
765 /* Property placed on source windows */
766 typedef struct _MotifDragInitiatorInfo {
768 guint8 protocol_version;
769 guint16 targets_index;
770 guint32 selection_atom;
771 } MotifDragInitiatorInfo;
773 /* Header for target table on the drag window */
774 typedef struct _MotifTargetTableHeader {
776 guchar protocol_version;
779 } MotifTargetTableHeader;
781 /* Property placed on target windows */
782 typedef struct _MotifDragReceiverInfo {
784 guint8 protocol_version;
785 guint8 protocol_style;
787 guint32 proxy_window;
788 guint16 num_drop_sites;
791 } MotifDragReceiverInfo;
793 static Window motif_drag_window = None;
794 static GdkWindow *motif_drag_gdk_window = NULL;
796 static GdkAtom motif_drag_targets_atom = GDK_NONE;
797 static GdkAtom motif_drag_receiver_info_atom = GDK_NONE;
799 /* Target table handling */
802 motif_drag_window_filter (GdkXEvent *xevent,
806 XEvent *xev = (XEvent *)xevent;
808 switch (xev->xany.type)
811 motif_drag_window = None;
812 motif_drag_gdk_window = NULL;
815 if (motif_target_lists &&
816 motif_drag_targets_atom &&
817 (xev->xproperty.atom == motif_drag_targets_atom))
818 motif_read_target_table();
821 return GDK_FILTER_REMOVE;
824 static Atom motif_drag_window_atom = GDK_NONE;
827 motif_lookup_drag_window (Display *display)
829 Window retval = None;
830 gulong bytes_after, nitems;
835 XGetWindowProperty (gdk_display, gdk_root_window, motif_drag_window_atom,
837 XA_WINDOW, &type, &format, &nitems, &bytes_after,
840 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
842 retval = *(Window *)data;
844 g_message ("Found drag window %#lx\n", motif_drag_window));
853 /* Finds the window where global Motif drag information is stored.
854 * If it doesn't exist and 'create' is TRUE, create one.
857 motif_find_drag_window (gboolean create)
859 if (!motif_drag_window)
861 if (!motif_drag_window_atom)
862 motif_drag_window_atom = gdk_atom_intern ("_MOTIF_DRAG_WINDOW", TRUE);
864 motif_drag_window = motif_lookup_drag_window (gdk_display);
866 if (!motif_drag_window && create)
868 /* Create a persistant window. (Copied from LessTif) */
871 XSetWindowAttributes attr;
872 display = XOpenDisplay (NULL);
873 XSetCloseDownMode (display, RetainPermanent);
875 XGrabServer (display);
877 motif_drag_window = motif_lookup_drag_window (display);
879 if (!motif_drag_window)
881 attr.override_redirect = True;
882 attr.event_mask = PropertyChangeMask;
885 XCreateWindow(display, DefaultRootWindow(display),
886 -100, -100, 10, 10, 0, 0,
887 InputOnly, CopyFromParent,
888 (CWOverrideRedirect | CWEventMask), &attr);
891 g_message ("Created drag window %#lx\n", motif_drag_window));
893 XChangeProperty (display, gdk_root_window,
894 motif_drag_window_atom, XA_WINDOW,
896 (guchar *)&motif_drag_window_atom, 1);
899 XUngrabServer (display);
900 XCloseDisplay (display);
903 /* There is a miniscule race condition here if the drag window
904 * gets destroyed exactly now.
906 if (motif_drag_window)
908 motif_drag_gdk_window = gdk_window_foreign_new (motif_drag_window);
909 gdk_window_add_filter (motif_drag_gdk_window,
910 motif_drag_window_filter,
915 return motif_drag_window;
919 motif_read_target_table (void)
921 gulong bytes_after, nitems;
926 if (!motif_drag_targets_atom)
927 motif_drag_targets_atom = gdk_atom_intern ("_MOTIF_DRAG_TARGETS", FALSE);
929 if (motif_target_lists)
931 for (i=0; i<motif_n_target_lists; i++)
932 g_list_free (motif_target_lists[i]);
934 g_free (motif_target_lists);
935 motif_target_lists = NULL;
936 motif_n_target_lists = 0;
939 if (motif_find_drag_window (FALSE))
941 MotifTargetTableHeader *header = NULL;
942 guchar *target_bytes = NULL;
944 gboolean success = FALSE;
946 XGetWindowProperty (gdk_display, motif_drag_window,
947 motif_drag_targets_atom,
948 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
949 motif_drag_targets_atom,
950 &type, &format, &nitems, &bytes_after,
953 if ((format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
956 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
957 header->total_size = card32_to_host (header->total_size, header->byte_order);
959 XGetWindowProperty (gdk_display, motif_drag_window, motif_drag_targets_atom,
960 (sizeof(MotifTargetTableHeader)+3)/4,
961 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
963 motif_drag_targets_atom, &type, &format, &nitems,
964 &bytes_after, &target_bytes);
966 if ((format != 8) || (bytes_after != 0) ||
967 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
970 motif_n_target_lists = header->n_lists;
971 motif_target_lists = g_new0 (GList *, motif_n_target_lists);
974 for (i=0; i<header->n_lists; i++)
979 if (p + sizeof(guint16) - target_bytes > nitems)
982 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
984 /* We need to make a copy of the targets, since it may
987 targets = g_new (guint32, n_targets);
988 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
990 p += sizeof(guint16) + n_targets * sizeof(guint32);
991 if (p - target_bytes > nitems)
994 for (j=0; j<n_targets; j++)
995 motif_target_lists[i] =
996 g_list_prepend (motif_target_lists[i],
997 GUINT_TO_POINTER (card32_to_host (targets[j],
998 header->byte_order)));
1000 motif_target_lists[i] = g_list_reverse (motif_target_lists[i]);
1010 XFree (target_bytes);
1014 if (motif_target_lists)
1016 g_free (motif_target_lists);
1017 motif_target_lists = NULL;
1018 motif_n_target_lists = 0;
1020 g_warning ("Error reading Motif target table\n");
1026 targets_sort_func (gconstpointer a, gconstpointer b)
1028 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1029 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1032 /* Check if given (sorted) list is in the targets table */
1034 motif_target_table_check (GList *sorted)
1036 GList *tmp_list1, *tmp_list2;
1039 for (i=0; i<motif_n_target_lists; i++)
1041 tmp_list1 = motif_target_lists[i];
1044 while (tmp_list1 && tmp_list2)
1046 if (tmp_list1->data != tmp_list2->data)
1049 tmp_list1 = tmp_list1->next;
1050 tmp_list2 = tmp_list2->next;
1052 if (!tmp_list1 && !tmp_list2) /* Found it */
1060 motif_add_to_target_table (GList *targets)
1062 GList *sorted = NULL;
1067 /* make a sorted copy of the list */
1071 sorted = g_list_insert_sorted (sorted, targets->data, targets_sort_func);
1072 targets = targets->next;
1075 /* First check if it is their already */
1077 if (motif_target_lists)
1078 index = motif_target_table_check (sorted);
1080 /* We need to grab the server while doing this, to ensure
1086 /* We need to make sure that it exists _before_ we grab the
1087 * server, since we can't open a new connection after we
1090 motif_find_drag_window (TRUE);
1092 XGrabServer(gdk_display);
1093 motif_read_target_table();
1095 /* Check again, in case it was added in the meantime */
1097 if (motif_target_lists)
1098 index = motif_target_table_check (sorted);
1102 guint32 total_size = 0;
1106 MotifTargetTableHeader *header;
1108 if (!motif_target_lists)
1110 motif_target_lists = g_new (GList *, 1);
1111 motif_n_target_lists = 1;
1115 motif_n_target_lists++;
1116 motif_target_lists = g_realloc (motif_target_lists,
1117 sizeof(GList *) * motif_n_target_lists);
1119 motif_target_lists[motif_n_target_lists - 1] = sorted;
1121 index = motif_n_target_lists - 1;
1123 total_size = sizeof (MotifTargetTableHeader);
1124 for (i = 0; i < motif_n_target_lists ; i++)
1125 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (motif_target_lists[i]);
1127 data = g_malloc (total_size);
1129 header = (MotifTargetTableHeader *)data;
1130 p = data + sizeof(MotifTargetTableHeader);
1132 header->byte_order = local_byte_order;
1133 header->protocol_version = 0;
1134 header->n_lists = motif_n_target_lists;
1135 header->total_size = total_size;
1137 for (i = 0; i < motif_n_target_lists ; i++)
1139 guint16 n_targets = g_list_length (motif_target_lists[i]);
1140 guint32 *targets = g_new (guint32, n_targets);
1141 guint32 *p32 = targets;
1143 tmp_list = motif_target_lists[i];
1146 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1148 tmp_list = tmp_list->next;
1153 p += sizeof(guint16);
1155 memcpy (p, targets, n_targets * sizeof(guint32));
1158 p += sizeof(guint32) * n_targets;
1162 XChangeProperty (gdk_display, motif_drag_window,
1163 motif_drag_targets_atom,
1164 motif_drag_targets_atom,
1168 XUngrabServer(gdk_display);
1171 g_list_free (sorted);
1175 /* Translate flags */
1178 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1180 guint recommended_op = flags & 0x000f;
1181 guint possible_ops = (flags & 0x0f0) >> 4;
1183 switch (recommended_op)
1186 context->suggested_action = GDK_ACTION_MOVE;
1189 context->suggested_action = GDK_ACTION_COPY;
1192 context->suggested_action = GDK_ACTION_LINK;
1195 context->suggested_action = GDK_ACTION_COPY;
1199 context->actions = 0;
1200 if (possible_ops & XmDROP_MOVE)
1201 context->actions |= GDK_ACTION_MOVE;
1202 if (possible_ops & XmDROP_COPY)
1203 context->actions |= GDK_ACTION_COPY;
1204 if (possible_ops & XmDROP_LINK)
1205 context->actions |= GDK_ACTION_LINK;
1209 motif_dnd_get_flags (GdkDragContext *context)
1213 switch (context->suggested_action)
1215 case GDK_ACTION_MOVE:
1216 flags = XmDROP_MOVE;
1218 case GDK_ACTION_COPY:
1219 flags = XmDROP_COPY;
1221 case GDK_ACTION_LINK:
1222 flags = XmDROP_LINK;
1225 flags = XmDROP_NOOP;
1229 if (context->actions & GDK_ACTION_MOVE)
1230 flags |= XmDROP_MOVE << 8;
1231 if (context->actions & GDK_ACTION_COPY)
1232 flags |= XmDROP_COPY << 8;
1233 if (context->actions & GDK_ACTION_LINK)
1234 flags |= XmDROP_LINK << 8;
1242 motif_set_targets (GdkDragContext *context)
1244 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1245 MotifDragInitiatorInfo info;
1247 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1249 if (!motif_drag_initiator_info)
1250 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1252 info.byte_order = local_byte_order;
1253 info.protocol_version = 0;
1255 info.targets_index = motif_add_to_target_table (context->targets);
1260 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1262 private->motif_selection = gdk_atom_intern (buf, FALSE);
1263 if (!XGetSelectionOwner (gdk_display, private->motif_selection))
1267 info.selection_atom = private->motif_selection;
1269 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1270 GDK_WINDOW_XWINDOW (context->source_window),
1271 private->motif_selection,
1272 motif_drag_initiator_info, 8, PropModeReplace,
1273 (guchar *)&info, sizeof (info));
1275 private->motif_targets_set = 1;
1279 motif_check_dest (Window win)
1281 gboolean retval = FALSE;
1282 MotifDragReceiverInfo *info;
1285 unsigned long nitems, after;
1287 if (!motif_drag_receiver_info_atom)
1288 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
1290 XGetWindowProperty (gdk_display, win,
1291 motif_drag_receiver_info_atom,
1292 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1293 &type, &format, &nitems, &after,
1298 if ((format == 8) && (nitems == sizeof(*info)))
1300 if ((info->protocol_version == 0) &&
1301 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1302 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1303 (info->protocol_style == XmDRAG_DYNAMIC)))
1309 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1315 return retval ? win : GDK_NONE;
1320 motif_send_enter (GdkDragContext *context,
1324 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1326 xev.xclient.type = ClientMessage;
1327 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1328 xev.xclient.format = 8;
1329 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1331 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1332 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1333 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1334 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1335 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XWINDOW (context->source_window);
1337 if (!private->motif_targets_set)
1338 motif_set_targets (context);
1340 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1342 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1345 g_message ("Send event to %lx failed",
1346 GDK_WINDOW_XWINDOW (context->dest_window)));
1350 motif_send_leave (GdkDragContext *context,
1355 xev.xclient.type = ClientMessage;
1356 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1357 xev.xclient.format = 8;
1358 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1360 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1361 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1362 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1363 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1364 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XWINDOW (context->source_window);
1365 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1367 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1370 g_message ("Send event to %lx failed",
1371 GDK_WINDOW_XWINDOW (context->dest_window)));
1375 motif_send_motion (GdkDragContext *context,
1378 GdkDragAction action,
1383 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1385 xev.xclient.type = ClientMessage;
1386 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1387 xev.xclient.format = 8;
1388 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1390 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1391 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1392 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1394 if ((context->suggested_action != private->old_action) ||
1395 (context->actions != private->old_actions))
1397 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1399 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1404 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1406 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1407 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1409 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1413 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1416 g_message ("Send event to %lx failed",
1417 GDK_WINDOW_XWINDOW (context->dest_window)));
1423 motif_send_drop (GdkDragContext *context, guint32 time)
1426 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1428 xev.xclient.type = ClientMessage;
1429 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1430 xev.xclient.format = 8;
1431 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1433 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1434 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1435 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1436 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1438 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1439 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1441 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1442 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XWINDOW (context->source_window);
1444 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1447 g_message ("Send event to %lx failed",
1448 GDK_WINDOW_XWINDOW (context->dest_window)));
1454 motif_read_initiator_info (Window source_window,
1460 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1466 MotifDragInitiatorInfo *initiator_info;
1468 if (!motif_drag_initiator_info)
1469 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1471 XGetWindowProperty (gdk_display, source_window, atom,
1472 0, sizeof(*initiator_info), FALSE,
1473 motif_drag_initiator_info,
1474 &type, &format, &nitems, &bytes_after,
1475 (guchar **)&initiator_info);
1477 if ((format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1479 g_warning ("Error reading initiator info\n");
1483 motif_read_target_table ();
1485 initiator_info->targets_index =
1486 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1487 initiator_info->selection_atom =
1488 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1490 if (initiator_info->targets_index >= motif_n_target_lists)
1492 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1493 XFree (initiator_info);
1494 return GDK_FILTER_REMOVE;
1497 tmp_list = g_list_last (motif_target_lists[initiator_info->targets_index]);
1502 *targets = g_list_prepend (*targets,
1504 tmp_list = tmp_list->prev;
1507 #ifdef G_ENABLE_DEBUG
1508 if (gdk_debug_flags & GDK_DEBUG_DND)
1509 print_target_list (*targets);
1510 #endif /* G_ENABLE_DEBUG */
1512 *selection = initiator_info->selection_atom;
1514 XFree (initiator_info);
1519 static GdkDragContext *
1520 motif_drag_context_new (GdkWindow *dest_window,
1522 guint32 source_window,
1525 GdkDragContext *new_context;
1526 GdkDragContextPrivate *private;
1528 /* FIXME, current_dest_drag really shouldn't be NULL'd
1529 * if we error below.
1531 if (current_dest_drag != NULL)
1533 if (timestamp >= current_dest_drag->start_time)
1535 gdk_drag_context_unref (current_dest_drag);
1536 current_dest_drag = NULL;
1542 new_context = gdk_drag_context_new ();
1543 private = (GdkDragContextPrivate *)new_context;
1545 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1546 new_context->is_source = FALSE;
1548 new_context->source_window = gdk_window_lookup (source_window);
1549 if (new_context->source_window)
1550 gdk_window_ref (new_context->source_window);
1553 new_context->source_window = gdk_window_foreign_new (source_window);
1554 if (!new_context->source_window)
1556 gdk_drag_context_unref (new_context);
1561 new_context->dest_window = dest_window;
1562 gdk_window_ref (dest_window);
1563 new_context->start_time = timestamp;
1565 if (!motif_read_initiator_info(source_window,
1567 &new_context->targets,
1568 &private->motif_selection))
1570 gdk_drag_context_unref (new_context);
1578 * The MOTIF drag protocol has no real provisions for distinguishing
1579 * multiple simultaneous drops. If the sources grab the pointer
1580 * when doing drags, that shouldn't happen, in any case. If it
1581 * does, we can't do much except hope for the best.
1584 static GdkFilterReturn
1585 motif_top_level_enter (GdkEvent *event,
1588 guint32 source_window,
1591 GdkDragContext *new_context;
1593 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1594 flags, timestamp, source_window, atom));
1596 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1598 return GDK_FILTER_REMOVE;
1600 event->dnd.type = GDK_DRAG_ENTER;
1601 event->dnd.context = new_context;
1602 gdk_drag_context_ref (new_context);
1604 current_dest_drag = new_context;
1606 return GDK_FILTER_TRANSLATE;
1610 motif_top_level_leave (GdkEvent *event,
1614 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1617 if ((current_dest_drag != NULL) &&
1618 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1619 (timestamp >= current_dest_drag->start_time))
1621 event->dnd.type = GDK_DRAG_LEAVE;
1622 /* Pass ownership of context to the event */
1623 event->dnd.context = current_dest_drag;
1625 current_dest_drag = NULL;
1627 return GDK_FILTER_TRANSLATE;
1630 return GDK_FILTER_REMOVE;
1634 motif_motion (GdkEvent *event,
1640 GdkDragContextPrivate *private;
1642 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1643 flags, timestamp, x_root, y_root));
1645 if ((current_dest_drag != NULL) &&
1646 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1647 (timestamp >= current_dest_drag->start_time))
1649 private = (GdkDragContextPrivate *)current_dest_drag;
1651 event->dnd.type = GDK_DRAG_MOTION;
1652 event->dnd.context = current_dest_drag;
1653 gdk_drag_context_ref (current_dest_drag);
1655 event->dnd.time = timestamp;
1657 motif_dnd_translate_flags (current_dest_drag, flags);
1659 event->dnd.x_root = x_root;
1660 event->dnd.y_root = y_root;
1662 private->last_x = x_root;
1663 private->last_y = y_root;
1665 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1667 return GDK_FILTER_TRANSLATE;
1670 return GDK_FILTER_REMOVE;
1674 motif_operation_changed (GdkEvent *event,
1678 GdkDragContextPrivate *private;
1680 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1683 if ((current_dest_drag != NULL) &&
1684 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1685 (timestamp >= current_dest_drag->start_time))
1687 event->dnd.type = GDK_DRAG_MOTION;
1688 event->dnd.send_event = FALSE;
1689 event->dnd.context = current_dest_drag;
1690 gdk_drag_context_ref (current_dest_drag);
1692 event->dnd.time = timestamp;
1693 private = (GdkDragContextPrivate *)current_dest_drag;
1695 motif_dnd_translate_flags (current_dest_drag, flags);
1697 event->dnd.x_root = private->last_x;
1698 event->dnd.y_root = private->last_y;
1700 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1702 return GDK_FILTER_TRANSLATE;
1705 return GDK_FILTER_REMOVE;
1709 motif_drop_start (GdkEvent *event,
1712 guint32 source_window,
1717 GdkDragContext *new_context;
1720 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1721 flags, timestamp, x_root, y_root, source_window, atom));
1723 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1725 return GDK_FILTER_REMOVE;
1727 motif_dnd_translate_flags (new_context, flags);
1729 event->dnd.type = GDK_DROP_START;
1730 event->dnd.context = new_context;
1731 event->dnd.time = timestamp;
1732 event->dnd.x_root = x_root;
1733 event->dnd.y_root = y_root;
1735 gdk_drag_context_ref (new_context);
1736 current_dest_drag = new_context;
1738 return GDK_FILTER_TRANSLATE;
1742 motif_drag_status (GdkEvent *event,
1746 GdkDragContext *context;
1749 g_message ("Motif status message: flags %x", flags));
1751 context = gdk_drag_context_find (TRUE,
1752 GDK_WINDOW_XWINDOW (event->any.window),
1757 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1758 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1759 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1760 private->drag_status = GDK_DRAG_STATUS_DRAG;
1762 event->dnd.type = GDK_DRAG_STATUS;
1763 event->dnd.send_event = FALSE;
1764 event->dnd.context = context;
1765 gdk_drag_context_ref (context);
1767 event->dnd.time = timestamp;
1769 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1771 switch (flags & 0x000f)
1774 context->action = 0;
1777 context->action = GDK_ACTION_MOVE;
1780 context->action = GDK_ACTION_COPY;
1783 context->action = GDK_ACTION_LINK;
1788 context->action = 0;
1790 return GDK_FILTER_TRANSLATE;
1792 return GDK_FILTER_REMOVE;
1796 motif_dnd_filter (GdkXEvent *xev,
1800 XEvent *xevent = (XEvent *)xev;
1805 guint32 source_window;
1807 gint16 x_root, y_root;
1810 /* First read some fields common to all Motif DND messages */
1812 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1813 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1814 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1816 is_reply = ((reason & 0x80) != 0);
1818 switch (reason & 0x7f)
1820 case XmTOP_LEVEL_ENTER:
1821 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1822 atom = MOTIF_UNPACK_LONG (xevent, 3);
1823 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1824 case XmTOP_LEVEL_LEAVE:
1825 return motif_top_level_leave (event, flags, timestamp);
1828 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1829 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1832 return motif_motion (event, flags, timestamp, x_root, y_root);
1834 return motif_drag_status (event, flags, timestamp);
1836 case XmDROP_SITE_ENTER:
1837 return motif_drag_status (event, flags, timestamp);
1839 case XmDROP_SITE_LEAVE:
1840 return motif_drag_status (event,
1841 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1844 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1845 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1846 atom = MOTIF_UNPACK_LONG (xevent, 3);
1847 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1850 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1853 case XmOPERATION_CHANGED:
1855 return motif_operation_changed (event, flags, timestamp);
1857 return motif_drag_status (event, flags, timestamp);
1860 /* To the best of my knowledge, these next two messages are
1861 * not part of the protocol, though they are defined in
1865 case XmDRAG_DROP_FINISH:
1869 return GDK_FILTER_REMOVE;
1872 /*************************************************************
1873 ***************************** XDND **************************
1874 *************************************************************/
1876 /* Utility functions */
1881 GdkDragAction action;
1882 } xdnd_actions_table[] = {
1883 { "XdndActionCopy", GDK_NONE, GDK_ACTION_COPY },
1884 { "XdndActionMove", GDK_NONE, GDK_ACTION_MOVE },
1885 { "XdndActionLink", GDK_NONE, GDK_ACTION_LINK },
1886 { "XdndActionAsk", GDK_NONE, GDK_ACTION_ASK },
1887 { "XdndActionPrivate", GDK_NONE, GDK_ACTION_COPY },
1890 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1891 static gboolean xdnd_actions_initialized = FALSE;
1894 xdnd_initialize_actions (void)
1898 xdnd_actions_initialized = TRUE;
1899 for (i=0; i < xdnd_n_actions; i++)
1900 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1903 static GdkDragAction
1904 xdnd_action_from_atom (GdkAtom atom)
1908 if (!xdnd_actions_initialized)
1909 xdnd_initialize_actions();
1911 for (i=0; i<xdnd_n_actions; i++)
1912 if (atom == xdnd_actions_table[i].atom)
1913 return xdnd_actions_table[i].action;
1919 xdnd_action_to_atom (GdkDragAction action)
1923 if (!xdnd_actions_initialized)
1924 xdnd_initialize_actions();
1926 for (i=0; i<xdnd_n_actions; i++)
1927 if (action == xdnd_actions_table[i].action)
1928 return xdnd_actions_table[i].atom;
1933 static GdkAtom xdnd_aware_atom = GDK_NONE;
1937 static GdkFilterReturn
1938 xdnd_status_filter (GdkXEvent *xev,
1942 XEvent *xevent = (XEvent *)xev;
1943 guint32 dest_window = xevent->xclient.data.l[0];
1944 guint32 flags = xevent->xclient.data.l[1];
1945 GdkAtom action = xevent->xclient.data.l[4];
1946 GdkDragContext *context;
1949 g_message ("XdndStatus: dest_window: %#x action: %ld",
1950 dest_window, action));
1953 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
1956 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1957 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1958 private->drag_status = GDK_DRAG_STATUS_DRAG;
1960 event->dnd.send_event = FALSE;
1961 event->dnd.type = GDK_DRAG_STATUS;
1962 event->dnd.context = context;
1963 gdk_drag_context_ref (context);
1965 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1966 if (!(action != 0) != !(flags & 1))
1969 g_warning ("Received status event with flags not corresponding to action!\n"));
1973 context->action = xdnd_action_from_atom (action);
1975 return GDK_FILTER_TRANSLATE;
1978 return GDK_FILTER_REMOVE;
1981 static GdkFilterReturn
1982 xdnd_finished_filter (GdkXEvent *xev,
1986 XEvent *xevent = (XEvent *)xev;
1987 guint32 dest_window = xevent->xclient.data.l[0];
1988 GdkDragContext *context;
1991 g_message ("XdndFinished: dest_window: %#x", dest_window));
1993 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
1996 event->dnd.type = GDK_DROP_FINISHED;
1997 event->dnd.context = context;
1998 gdk_drag_context_ref (context);
2000 return GDK_FILTER_TRANSLATE;
2003 return GDK_FILTER_REMOVE;
2007 xdnd_set_targets (GdkDragContext *context)
2009 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2011 GList *tmp_list = context->targets;
2013 gint n_atoms = g_list_length (context->targets);
2015 atomlist = g_new (GdkAtom, n_atoms);
2019 atomlist[i] = GPOINTER_TO_INT (tmp_list->data);
2020 tmp_list = tmp_list->next;
2024 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2025 GDK_WINDOW_XWINDOW (context->source_window),
2026 gdk_atom_intern ("XdndTypeList", FALSE),
2027 XA_ATOM, 32, PropModeReplace,
2028 (guchar *)atomlist, n_atoms);
2030 private->xdnd_targets_set = 1;
2034 xdnd_set_actions (GdkDragContext *context)
2036 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2042 if (!xdnd_actions_initialized)
2043 xdnd_initialize_actions();
2045 actions = context->actions;
2047 for (i=0; i<xdnd_n_actions; i++)
2049 if (actions & xdnd_actions_table[i].action)
2051 actions &= ~xdnd_actions_table[i].action;
2056 atomlist = g_new (GdkAtom, n_atoms);
2058 actions = context->actions;
2060 for (i=0; i<xdnd_n_actions; i++)
2062 if (actions & xdnd_actions_table[i].action)
2064 actions &= ~xdnd_actions_table[i].action;
2065 atomlist[n_atoms] = xdnd_actions_table[i].atom;
2070 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2071 GDK_WINDOW_XWINDOW (context->source_window),
2072 gdk_atom_intern ("XdndActionList", FALSE),
2073 XA_ATOM, 32, PropModeReplace,
2074 (guchar *)atomlist, n_atoms);
2076 private->xdnd_actions_set = 1;
2077 private->xdnd_actions = context->actions;
2080 /*************************************************************
2082 * Like gdk_send_event, but if the target is the root
2083 * window, sets an event mask of ButtonPressMask, otherwise
2084 * an event mask of 0.
2088 *************************************************************/
2091 xdnd_send_xevent (Window window, gboolean propagate,
2094 if (window == gdk_root_window)
2095 return gdk_send_xevent (window, propagate, ButtonPressMask, event_send);
2097 return gdk_send_xevent (window, propagate, 0, event_send);
2101 xdnd_send_enter (GdkDragContext *context)
2104 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2106 xev.xclient.type = ClientMessage;
2107 xev.xclient.message_type = gdk_atom_intern ("XdndEnter", FALSE);
2108 xev.xclient.format = 32;
2110 xev.xclient.window = private->dest_xid ?
2112 GDK_WINDOW_XWINDOW (context->dest_window);
2114 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2116 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2117 xev.xclient.data.l[1] = (3 << 24); /* version */
2118 xev.xclient.data.l[2] = 0;
2119 xev.xclient.data.l[3] = 0;
2120 xev.xclient.data.l[4] = 0;
2122 if (!private->xdnd_selection)
2123 private->xdnd_selection = gdk_atom_intern ("XdndSelection", FALSE);
2125 if (g_list_length (context->targets) > 3)
2127 if (!private->xdnd_targets_set)
2128 xdnd_set_targets (context);
2129 xev.xclient.data.l[1] |= 1;
2133 GList *tmp_list = context->targets;
2138 xev.xclient.data.l[i] = GPOINTER_TO_INT (tmp_list->data);
2139 tmp_list = tmp_list->next;
2144 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2148 g_message ("Send event to %lx failed",
2149 GDK_WINDOW_XWINDOW (context->dest_window)));
2150 gdk_window_unref (context->dest_window);
2151 context->dest_window = NULL;
2156 xdnd_send_leave (GdkDragContext *context)
2160 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2162 xev.xclient.type = ClientMessage;
2163 xev.xclient.message_type = gdk_atom_intern ("XdndLeave", FALSE);
2164 xev.xclient.format = 32;
2166 xev.xclient.window = private->dest_xid ?
2168 GDK_WINDOW_XWINDOW (context->dest_window);
2170 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2172 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2173 xev.xclient.data.l[1] = 0;
2174 xev.xclient.data.l[2] = 0;
2175 xev.xclient.data.l[3] = 0;
2176 xev.xclient.data.l[4] = 0;
2178 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2182 g_message ("Send event to %lx failed",
2183 GDK_WINDOW_XWINDOW (context->dest_window)));
2184 gdk_window_unref (context->dest_window);
2185 context->dest_window = NULL;
2190 xdnd_send_drop (GdkDragContext *context, guint32 time)
2192 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2195 xev.xclient.type = ClientMessage;
2196 xev.xclient.message_type = gdk_atom_intern ("XdndDrop", FALSE);
2197 xev.xclient.format = 32;
2199 xev.xclient.window = private->dest_xid ?
2201 GDK_WINDOW_XWINDOW (context->dest_window);
2203 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2205 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2206 xev.xclient.data.l[1] = 0;
2207 xev.xclient.data.l[2] = time;
2208 xev.xclient.data.l[3] = 0;
2209 xev.xclient.data.l[4] = 0;
2211 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2215 g_message ("Send event to %lx failed",
2216 GDK_WINDOW_XWINDOW (context->dest_window)));
2217 gdk_window_unref (context->dest_window);
2218 context->dest_window = NULL;
2223 xdnd_send_motion (GdkDragContext *context,
2226 GdkDragAction action,
2229 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2232 xev.xclient.type = ClientMessage;
2233 xev.xclient.message_type = gdk_atom_intern ("XdndPosition", FALSE);
2234 xev.xclient.format = 32;
2236 xev.xclient.window = private->dest_xid ?
2238 GDK_WINDOW_XWINDOW (context->dest_window);
2240 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2242 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2243 xev.xclient.data.l[1] = 0;
2244 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2245 xev.xclient.data.l[3] = time;
2246 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
2248 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2252 g_message ("Send event to %lx failed",
2253 GDK_WINDOW_XWINDOW (context->dest_window)));
2254 gdk_window_unref (context->dest_window);
2255 context->dest_window = NULL;
2257 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2261 xdnd_check_dest (Window win)
2263 gboolean retval = FALSE;
2266 unsigned long nitems, after;
2270 static GdkAtom xdnd_proxy_atom = GDK_NONE;
2272 gint old_warnings = gdk_error_warnings;
2274 if (!xdnd_proxy_atom)
2275 xdnd_proxy_atom = gdk_atom_intern ("XdndProxy", FALSE);
2277 if (!xdnd_aware_atom)
2278 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
2283 gdk_error_warnings = 0;
2285 XGetWindowProperty (gdk_display, win,
2287 1, False, AnyPropertyType,
2288 &type, &format, &nitems, &after,
2289 (guchar **)&proxy_data);
2291 if (!gdk_error_code)
2295 if ((format == 32) && (nitems == 1))
2297 proxy = *proxy_data;
2301 g_warning ("Invalid XdndOwner property on window %ld\n", win));
2306 XGetWindowProperty (gdk_display, proxy ? proxy : win,
2308 1, False, AnyPropertyType,
2309 &type, &format, &nitems, &after,
2310 (guchar **)&version);
2312 if (!gdk_error_code && type != None)
2314 if ((format == 32) && (nitems == 1))
2321 g_warning ("Invalid XdndAware property on window %ld\n", win));
2328 gdk_error_warnings = old_warnings;
2331 return retval ? (proxy ? proxy : win) : GDK_NONE;
2337 xdnd_read_actions (GdkDragContext *context)
2341 gulong nitems, after;
2346 gint old_warnings = gdk_error_warnings;
2349 gdk_error_warnings = 0;
2351 /* Get the XdndActionList, if set */
2353 XGetWindowProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2354 GDK_WINDOW_XWINDOW (context->source_window),
2355 gdk_atom_intern ("XdndActionList", FALSE), 0, 65536,
2356 False, XA_ATOM, &type, &format, &nitems,
2357 &after, (guchar **)&data);
2359 if (!gdk_error_code && (format == 32) && (type == XA_ATOM))
2361 context->actions = 0;
2363 for (i=0; i<nitems; i++)
2364 context->actions |= xdnd_action_from_atom (data[i]);
2366 ((GdkDragContextPrivate *)context)->xdnd_have_actions = TRUE;
2368 #ifdef G_ENABLE_DEBUG
2369 if (gdk_debug_flags & GDK_DEBUG_DND)
2371 GString *action_str = g_string_new (NULL);
2372 if (context->actions & GDK_ACTION_MOVE)
2373 g_string_append(action_str, "MOVE ");
2374 if (context->actions & GDK_ACTION_COPY)
2375 g_string_append(action_str, "COPY ");
2376 if (context->actions & GDK_ACTION_LINK)
2377 g_string_append(action_str, "LINK ");
2378 if (context->actions & GDK_ACTION_ASK)
2379 g_string_append(action_str, "ASK ");
2381 g_message("Xdnd actions = %s", action_str->str);
2382 g_string_free (action_str, TRUE);
2384 #endif /* G_ENABLE_DEBUG */
2389 gdk_error_warnings = old_warnings;
2393 /* We have to make sure that the XdndActionList we keep internally
2394 * is up to date with the XdndActionList on the source window
2395 * because we get no notification, because Xdnd wasn't meant
2396 * to continually send actions. So we select on PropertyChangeMask
2397 * and add this filter.
2399 static GdkFilterReturn
2400 xdnd_source_window_filter (GdkXEvent *xev,
2404 XEvent *xevent = (XEvent *)xev;
2405 GdkDragContext *context = cb_data;
2407 if ((xevent->xany.type == PropertyNotify) &&
2408 (xevent->xproperty.atom == gdk_atom_intern ("XdndActionList", FALSE)))
2410 xdnd_read_actions (context);
2412 return GDK_FILTER_REMOVE;
2415 return GDK_FILTER_CONTINUE;
2419 xdnd_manage_source_filter (GdkDragContext *context,
2421 gboolean add_filter)
2423 gint old_warnings = 0; /* quiet gcc */
2424 GdkWindowPrivate *private = (GdkWindowPrivate *)window;
2426 gboolean is_foreign = (private->window_type == GDK_WINDOW_FOREIGN);
2430 old_warnings = gdk_error_warnings;
2431 gdk_error_warnings = 0;
2434 if (!private->destroyed)
2438 gdk_window_set_events (window,
2439 gdk_window_get_events (window) |
2440 GDK_PROPERTY_CHANGE_MASK);
2441 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2446 gdk_window_remove_filter (window,
2447 xdnd_source_window_filter,
2449 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2450 * but we might want it for other reasons. (Like
2451 * INCR selection transactions).
2459 gdk_error_warnings = old_warnings;
2463 static GdkFilterReturn
2464 xdnd_enter_filter (GdkXEvent *xev,
2468 XEvent *xevent = (XEvent *)xev;
2469 GdkDragContext *new_context;
2474 gulong nitems, after;
2477 guint32 source_window = xevent->xclient.data.l[0];
2478 gboolean get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2479 gint version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2482 g_message ("XdndEnter: source_window: %#x, version: %#x",
2483 source_window, version));
2487 /* Old source ignore */
2488 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2489 return GDK_FILTER_REMOVE;
2492 if (current_dest_drag != NULL)
2494 gdk_drag_context_unref (current_dest_drag);
2495 current_dest_drag = NULL;
2498 new_context = gdk_drag_context_new ();
2499 new_context->protocol = GDK_DRAG_PROTO_XDND;
2500 new_context->is_source = FALSE;
2502 new_context->source_window = gdk_window_lookup (source_window);
2503 if (new_context->source_window)
2504 gdk_window_ref (new_context->source_window);
2507 new_context->source_window = gdk_window_foreign_new (source_window);
2508 if (!new_context->source_window)
2510 gdk_drag_context_unref (new_context);
2511 return GDK_FILTER_REMOVE;
2514 new_context->dest_window = event->any.window;
2515 gdk_window_ref (new_context->dest_window);
2517 new_context->targets = NULL;
2520 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2522 gdk_atom_intern ("XdndTypeList", FALSE), 0, 65536,
2523 False, XA_ATOM, &type, &format, &nitems,
2524 &after, (guchar **)&data);
2526 if ((format != 32) || (type != XA_ATOM))
2528 gdk_drag_context_unref (new_context);
2529 return GDK_FILTER_REMOVE;
2532 for (i=0; i<nitems; i++)
2533 new_context->targets = g_list_append (new_context->targets,
2534 GUINT_TO_POINTER (data[i]));
2541 if (xevent->xclient.data.l[2+i])
2542 new_context->targets = g_list_append (new_context->targets,
2543 GUINT_TO_POINTER (xevent->xclient.data.l[2+i]));
2546 #ifdef G_ENABLE_DEBUG
2547 if (gdk_debug_flags & GDK_DEBUG_DND)
2548 print_target_list (new_context->targets);
2549 #endif /* G_ENABLE_DEBUG */
2551 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2552 xdnd_read_actions (new_context);
2554 event->dnd.type = GDK_DRAG_ENTER;
2555 event->dnd.context = new_context;
2556 gdk_drag_context_ref (new_context);
2558 current_dest_drag = new_context;
2559 ((GdkDragContextPrivate *)new_context)->xdnd_selection =
2560 gdk_atom_intern ("XdndSelection", FALSE);
2562 return GDK_FILTER_TRANSLATE;
2565 static GdkFilterReturn
2566 xdnd_leave_filter (GdkXEvent *xev,
2570 XEvent *xevent = (XEvent *)xev;
2571 guint32 source_window = xevent->xclient.data.l[0];
2574 g_message ("XdndLeave: source_window: %#x",
2577 if ((current_dest_drag != NULL) &&
2578 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2579 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2581 event->dnd.type = GDK_DRAG_LEAVE;
2582 /* Pass ownership of context to the event */
2583 event->dnd.context = current_dest_drag;
2585 current_dest_drag = NULL;
2587 return GDK_FILTER_TRANSLATE;
2590 return GDK_FILTER_REMOVE;
2593 static GdkFilterReturn
2594 xdnd_position_filter (GdkXEvent *xev,
2598 XEvent *xevent = (XEvent *)xev;
2599 guint32 source_window = xevent->xclient.data.l[0];
2600 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2601 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2602 guint32 time = xevent->xclient.data.l[3];
2603 GdkAtom action = xevent->xclient.data.l[4];
2606 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2607 source_window, x_root, y_root, time, action));
2610 if ((current_dest_drag != NULL) &&
2611 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2612 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2614 event->dnd.type = GDK_DRAG_MOTION;
2615 event->dnd.context = current_dest_drag;
2616 gdk_drag_context_ref (current_dest_drag);
2618 event->dnd.time = time;
2620 current_dest_drag->suggested_action = xdnd_action_from_atom (action);
2621 if (!((GdkDragContextPrivate *)current_dest_drag)->xdnd_have_actions)
2622 current_dest_drag->actions = current_dest_drag->suggested_action;
2624 event->dnd.x_root = x_root;
2625 event->dnd.y_root = y_root;
2627 ((GdkDragContextPrivate *)current_dest_drag)->last_x = x_root;
2628 ((GdkDragContextPrivate *)current_dest_drag)->last_y = y_root;
2630 return GDK_FILTER_TRANSLATE;
2633 return GDK_FILTER_REMOVE;
2636 static GdkFilterReturn
2637 xdnd_drop_filter (GdkXEvent *xev,
2641 XEvent *xevent = (XEvent *)xev;
2642 guint32 source_window = xevent->xclient.data.l[0];
2643 guint32 time = xevent->xclient.data.l[2];
2646 g_message ("XdndDrop: source_window: %#x time: %d",
2647 source_window, time));
2649 if ((current_dest_drag != NULL) &&
2650 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2651 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2653 GdkDragContextPrivate *private;
2654 private = (GdkDragContextPrivate *)current_dest_drag;
2656 event->dnd.type = GDK_DROP_START;
2658 event->dnd.context = current_dest_drag;
2659 gdk_drag_context_ref (current_dest_drag);
2661 event->dnd.time = time;
2662 event->dnd.x_root = private->last_x;
2663 event->dnd.y_root = private->last_y;
2665 return GDK_FILTER_TRANSLATE;
2668 return GDK_FILTER_REMOVE;
2671 /*************************************************************
2672 ************************** Public API ***********************
2673 *************************************************************/
2680 gdk_add_client_message_filter (
2681 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2682 motif_dnd_filter, NULL);
2683 gdk_add_client_message_filter (
2684 gdk_atom_intern ("XdndEnter", FALSE),
2685 xdnd_enter_filter, NULL);
2686 gdk_add_client_message_filter (
2687 gdk_atom_intern ("XdndLeave", FALSE),
2688 xdnd_leave_filter, NULL);
2689 gdk_add_client_message_filter (
2690 gdk_atom_intern ("XdndPosition", FALSE),
2691 xdnd_position_filter, NULL);
2692 gdk_add_client_message_filter (
2693 gdk_atom_intern ("XdndStatus", FALSE),
2694 xdnd_status_filter, NULL);
2695 gdk_add_client_message_filter (
2696 gdk_atom_intern ("XdndFinished", FALSE),
2697 xdnd_finished_filter, NULL);
2698 gdk_add_client_message_filter (
2699 gdk_atom_intern ("XdndDrop", FALSE),
2700 xdnd_drop_filter, NULL);
2706 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2708 if (context->dest_window)
2710 switch (context->protocol)
2712 case GDK_DRAG_PROTO_MOTIF:
2713 motif_send_leave (context, time);
2715 case GDK_DRAG_PROTO_XDND:
2716 xdnd_send_leave (context);
2718 case GDK_DRAG_PROTO_ROOTWIN:
2719 case GDK_DRAG_PROTO_NONE:
2723 gdk_window_unref (context->dest_window);
2724 context->dest_window = NULL;
2729 gdk_drag_begin (GdkWindow *window,
2733 GdkDragContext *new_context;
2735 g_return_val_if_fail (window != NULL, NULL);
2737 new_context = gdk_drag_context_new ();
2738 new_context->is_source = TRUE;
2739 new_context->source_window = window;
2740 gdk_window_ref (window);
2742 tmp_list = g_list_last (targets);
2743 new_context->targets = NULL;
2746 new_context->targets = g_list_prepend (new_context->targets,
2748 tmp_list = tmp_list->prev;
2751 new_context->actions = 0;
2757 gdk_drag_get_protocol (guint32 xid,
2758 GdkDragProtocol *protocol)
2762 if ((retval = xdnd_check_dest (xid)))
2764 *protocol = GDK_DRAG_PROTO_XDND;
2765 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2768 else if ((retval = motif_check_dest (xid)))
2770 *protocol = GDK_DRAG_PROTO_MOTIF;
2771 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2776 /* Check if this is a root window */
2778 gboolean rootwin = FALSE;
2779 gint old_warnings = gdk_error_warnings;
2782 unsigned long nitems, after;
2783 unsigned char *data;
2785 if (xid == gdk_root_window)
2792 XGetWindowProperty (gdk_display, xid,
2793 gdk_atom_intern ("ENLIGHTENMENT_DESKTOP", FALSE),
2794 0, 0, False, AnyPropertyType,
2795 &type, &format, &nitems, &after, &data);
2796 if ((gdk_error_code == 0) && type != None)
2803 /* Until I find out what window manager the next one is for,
2804 * I'm leaving it commented out. It's supported in the
2805 * xscreensaver sources, though.
2812 XGetWindowProperty (gdk_display, win,
2813 gdk_atom_intern ("__SWM_VROOT", FALSE),
2814 0, 0, False, AnyPropertyType,
2815 &type, &format, &nitems, &data);
2816 if ((gdk_error_code == 0) && type != None)
2821 gdk_error_warnings = old_warnings;
2825 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2830 *protocol = GDK_DRAG_PROTO_NONE;
2835 gdk_drag_find_window (GdkDragContext *context,
2836 GdkWindow *drag_window,
2839 GdkWindow **dest_window,
2840 GdkDragProtocol *protocol)
2842 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2845 g_return_if_fail (context != NULL);
2847 if (!private->window_cache)
2848 private->window_cache = gdk_window_cache_new();
2850 dest = get_client_window_at_coords (private->window_cache,
2852 GDK_WINDOW_XWINDOW (drag_window) : GDK_NONE,
2855 if (private->dest_xid != dest)
2858 private->dest_xid = dest;
2860 /* Check if new destination accepts drags, and which protocol */
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 gdk_window_ref (context->dest_window);
2921 context->protocol = protocol;
2925 case GDK_DRAG_PROTO_MOTIF:
2926 motif_send_enter (context, time);
2929 case GDK_DRAG_PROTO_XDND:
2930 xdnd_send_enter (context);
2933 case GDK_DRAG_PROTO_ROOTWIN:
2934 case GDK_DRAG_PROTO_NONE:
2937 private->old_action = suggested_action;
2938 context->suggested_action = suggested_action;
2939 private->old_actions = possible_actions;
2943 context->dest_window = NULL;
2944 context->action = 0;
2947 /* Push a status event, to let the client know that
2951 temp_event.dnd.type = GDK_DRAG_STATUS;
2952 temp_event.dnd.window = context->source_window;
2953 /* We use this to signal a synthetic status. Perhaps
2954 * we should use an extra field...
2956 temp_event.dnd.send_event = TRUE;
2958 temp_event.dnd.context = context;
2959 temp_event.dnd.time = time;
2961 gdk_event_put (&temp_event);
2965 private->old_action = context->suggested_action;
2966 context->suggested_action = suggested_action;
2969 /* Send a drag-motion event */
2971 private->last_x = x_root;
2972 private->last_y = y_root;
2974 if (context->dest_window)
2976 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
2978 switch (context->protocol)
2980 case GDK_DRAG_PROTO_MOTIF:
2981 motif_send_motion (context, x_root, y_root, suggested_action, time);
2984 case GDK_DRAG_PROTO_XDND:
2985 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
2988 case GDK_DRAG_PROTO_ROOTWIN:
2990 GdkEvent temp_event;
2992 if (g_list_find (context->targets,
2993 GUINT_TO_POINTER (gdk_atom_intern ("application/x-rootwin-drop", FALSE))))
2994 context->action = context->suggested_action;
2996 context->action = 0;
2998 temp_event.dnd.type = GDK_DRAG_STATUS;
2999 temp_event.dnd.window = context->source_window;
3000 temp_event.dnd.send_event = FALSE;
3001 temp_event.dnd.context = context;
3002 temp_event.dnd.time = time;
3004 gdk_event_put (&temp_event);
3007 case GDK_DRAG_PROTO_NONE:
3008 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3020 gdk_drag_drop (GdkDragContext *context,
3023 g_return_if_fail (context != NULL);
3025 if (context->dest_window)
3027 switch (context->protocol)
3029 case GDK_DRAG_PROTO_MOTIF:
3030 motif_send_leave (context, time);
3031 motif_send_drop (context, time);
3034 case GDK_DRAG_PROTO_XDND:
3035 xdnd_send_drop (context, time);
3038 case GDK_DRAG_PROTO_ROOTWIN:
3039 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3041 case GDK_DRAG_PROTO_NONE:
3042 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3049 gdk_drag_abort (GdkDragContext *context,
3052 g_return_if_fail (context != NULL);
3054 gdk_drag_do_leave (context, time);
3057 /* Destination side */
3060 gdk_drag_status (GdkDragContext *context,
3061 GdkDragAction action,
3064 GdkDragContextPrivate *private;
3067 g_return_if_fail (context != NULL);
3069 private = (GdkDragContextPrivate *)context;
3071 context->action = action;
3073 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3075 xev.xclient.type = ClientMessage;
3076 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
3077 xev.xclient.format = 8;
3078 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
3080 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3082 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3086 if ((action != 0) != (private->old_action != 0))
3089 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3091 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3094 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3097 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3101 case GDK_ACTION_MOVE:
3102 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3104 case GDK_ACTION_COPY:
3105 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3107 case GDK_ACTION_LINK:
3108 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3111 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3116 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3118 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3120 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3121 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3122 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3124 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3127 g_message ("Send event to %lx failed",
3128 GDK_WINDOW_XWINDOW (context->source_window)));
3130 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3132 xev.xclient.type = ClientMessage;
3133 xev.xclient.message_type = gdk_atom_intern ("XdndStatus", FALSE);
3134 xev.xclient.format = 32;
3135 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
3137 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->dest_window);
3138 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3139 xev.xclient.data.l[2] = 0;
3140 xev.xclient.data.l[3] = 0;
3141 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
3143 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3146 g_message ("Send event to %lx failed",
3147 GDK_WINDOW_XWINDOW (context->source_window)));
3150 private->old_action = action;
3154 gdk_drop_reply (GdkDragContext *context,
3158 GdkDragContextPrivate *private;
3160 g_return_if_fail (context != NULL);
3162 private = (GdkDragContextPrivate *)context;
3164 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3168 xev.xclient.type = ClientMessage;
3169 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
3170 xev.xclient.format = 8;
3172 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3173 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3175 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3176 (XmDROP_SITE_VALID << 4) |
3177 (XmDROP_NOOP << 8) |
3180 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3181 (XmNO_DROP_SITE << 4) |
3182 (XmDROP_NOOP << 8) |
3183 (XmDROP_CANCEL << 12);
3184 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3185 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3187 gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3193 gdk_drop_finish (GdkDragContext *context,
3197 g_return_if_fail (context != NULL);
3199 if (context->protocol == GDK_DRAG_PROTO_XDND)
3203 xev.xclient.type = ClientMessage;
3204 xev.xclient.message_type = gdk_atom_intern ("XdndFinished", FALSE);
3205 xev.xclient.format = 32;
3206 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
3208 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->dest_window);
3209 xev.xclient.data.l[1] = 0;
3210 xev.xclient.data.l[2] = 0;
3211 xev.xclient.data.l[3] = 0;
3212 xev.xclient.data.l[4] = 0;
3214 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3217 g_message ("Send event to %lx failed",
3218 GDK_WINDOW_XWINDOW (context->source_window)));
3224 gdk_window_register_dnd (GdkWindow *window)
3226 static gulong xdnd_version = 3;
3227 MotifDragReceiverInfo info;
3229 g_return_if_fail (window != NULL);
3231 /* Set Motif drag receiver information property */
3233 if (!motif_drag_receiver_info_atom)
3234 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
3236 info.byte_order = local_byte_order;
3237 info.protocol_version = 0;
3238 info.protocol_style = XmDRAG_DYNAMIC;
3239 info.proxy_window = GDK_NONE;
3240 info.num_drop_sites = 0;
3241 info.total_size = sizeof(info);
3243 XChangeProperty (gdk_display, GDK_WINDOW_XWINDOW (window),
3244 motif_drag_receiver_info_atom,
3245 motif_drag_receiver_info_atom,
3252 if (!xdnd_aware_atom)
3253 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
3255 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3256 XChangeProperty (GDK_WINDOW_XDISPLAY (window),
3257 GDK_WINDOW_XWINDOW (window),
3258 xdnd_aware_atom, XA_ATOM,
3259 32, PropModeReplace,
3260 (guchar *)&xdnd_version, 1);
3263 /*************************************************************
3264 * gdk_drag_get_selection:
3265 * Returns the selection atom for the current source window
3269 *************************************************************/
3272 gdk_drag_get_selection (GdkDragContext *context)
3274 g_return_val_if_fail (context != NULL, GDK_NONE);
3276 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3277 return ((GdkDragContextPrivate *)context)->motif_selection;
3278 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3279 return ((GdkDragContextPrivate *)context)->xdnd_selection;