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.
20 #include <X11/Xatom.h>
23 #include "gdk/gdkprivate.h"
28 typedef struct _GdkDragContextPrivate GdkDragContextPrivate;
32 GDK_DRAG_STATUS_MOTION_WAIT,
33 GDK_DRAG_STATUS_ACTION_WAIT,
39 gint x, y, width, height;
45 GHashTable *child_hash;
49 /* Structure that holds information about a drag in progress.
50 * this is used on both source and destination sides.
52 struct _GdkDragContextPrivate {
53 GdkDragContext context;
55 GdkAtom motif_selection;
56 GdkAtom xdnd_selection;
59 guint16 last_x; /* Coordinates from last event */
61 GdkDragAction old_action; /* The last action we sent to the source */
62 GdkDragAction old_actions; /* The last actions we sent to the source */
63 GdkDragAction xdnd_actions; /* What is currently set in XdndActionList */
66 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
67 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionList */
68 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
69 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
70 guint drag_status : 4; /* current status of drag */
72 GdkWindowCache *window_cache;
75 GdkDragContext *current_dest_drag = NULL;
77 /* Forward declarations */
79 static void gdk_window_cache_destroy (GdkWindowCache *cache);
81 static void motif_read_target_table (void);
82 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
86 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
90 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
94 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
98 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
102 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
106 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
110 static void xdnd_manage_source_filter (GdkDragContext *context,
112 gboolean add_filter);
116 static GList *contexts;
119 gdk_drag_context_new (void)
121 GdkDragContextPrivate *result;
123 result = g_new0 (GdkDragContextPrivate, 1);
125 result->ref_count = 1;
127 contexts = g_list_prepend (contexts, result);
129 return (GdkDragContext *)result;
133 gdk_drag_context_ref (GdkDragContext *context)
135 g_return_if_fail (context != NULL);
137 ((GdkDragContextPrivate *)context)->ref_count++;
141 gdk_drag_context_unref (GdkDragContext *context)
143 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
145 g_return_if_fail (context != NULL);
147 private->ref_count--;
149 if (private->ref_count == 0)
151 g_dataset_destroy (private);
153 g_list_free (context->targets);
155 if (context->source_window)
157 if ((context->protocol == GDK_DRAG_PROTO_XDND) &&
159 xdnd_manage_source_filter (context, context->source_window, FALSE);
161 gdk_window_unref (context->source_window);
164 if (context->dest_window)
165 gdk_window_unref (context->dest_window);
167 if (private->window_cache)
168 gdk_window_cache_destroy (private->window_cache);
170 contexts = g_list_remove (contexts, private);
175 static GdkDragContext *
176 gdk_drag_context_find (gboolean is_source,
180 GList *tmp_list = contexts;
181 GdkDragContext *context;
182 GdkDragContextPrivate *private;
183 Window context_dest_xid;
187 context = (GdkDragContext *)tmp_list->data;
188 private = (GdkDragContextPrivate *)context;
190 context_dest_xid = context->dest_window ?
193 GDK_WINDOW_XWINDOW (context->dest_window)) :
196 if ((!context->is_source == !is_source) &&
197 ((source_xid == None) || (context->source_window &&
198 (GDK_WINDOW_XWINDOW (context->source_window) == source_xid))) &&
199 ((dest_xid == None) || (context_dest_xid == dest_xid)))
202 tmp_list = tmp_list->next;
208 /* Utility functions */
211 gdk_window_cache_add (GdkWindowCache *cache,
213 gint x, gint y, gint width, gint height,
216 GdkCacheChild *child = g_new (GdkCacheChild, 1);
221 child->width = width;
222 child->height = height;
223 child->mapped = mapped;
225 cache->children = g_list_prepend (cache->children, child);
226 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
230 static GdkFilterReturn
231 gdk_window_cache_filter (GdkXEvent *xev,
235 XEvent *xevent = (XEvent *)xev;
236 GdkWindowCache *cache = data;
238 switch (xevent->type)
240 case CirculateNotify:
242 case ConfigureNotify:
244 XConfigureEvent *xce = &xevent->xconfigure;
247 node = g_hash_table_lookup (cache->child_hash,
248 GUINT_TO_POINTER (xce->window));
251 GdkCacheChild *child = node->data;
254 child->width = xce->width;
255 child->height = xce->height;
256 if (xce->above == None && (node->next))
258 GList *last = g_list_last (cache->children);
259 cache->children = g_list_remove_link (cache->children, node);
266 GList *above_node = g_hash_table_lookup (cache->child_hash,
267 GUINT_TO_POINTER (xce->above));
268 if (above_node && node->prev != above_node)
270 cache->children = g_list_remove_link (cache->children, node);
271 node->next = above_node->next;
273 node->next->prev = node;
274 node->prev = above_node;
275 above_node->next = node;
283 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
285 if (!g_hash_table_lookup (cache->child_hash,
286 GUINT_TO_POINTER (xcwe->window)))
287 gdk_window_cache_add (cache, xcwe->window,
288 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
294 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
297 node = g_hash_table_lookup (cache->child_hash,
298 GUINT_TO_POINTER (xdwe->window));
301 g_hash_table_remove (cache->child_hash,
302 GUINT_TO_POINTER (xdwe->window));
303 cache->children = g_list_remove_link (cache->children, node);
305 g_list_free_1 (node);
311 XMapEvent *xme = &xevent->xmap;
314 node = g_hash_table_lookup (cache->child_hash,
315 GUINT_TO_POINTER (xme->window));
318 GdkCacheChild *child = node->data;
319 child->mapped = TRUE;
327 XMapEvent *xume = &xevent->xmap;
330 node = g_hash_table_lookup (cache->child_hash,
331 GUINT_TO_POINTER (xume->window));
334 GdkCacheChild *child = node->data;
335 child->mapped = FALSE;
340 return GDK_FILTER_CONTINUE;
342 return GDK_FILTER_REMOVE;
345 static GdkWindowCache *
346 gdk_window_cache_new (void)
348 XWindowAttributes xwa;
349 Window root, parent, *children;
350 unsigned int nchildren;
353 gint old_warnings = gdk_error_warnings;
355 GdkWindowCache *result = g_new (GdkWindowCache, 1);
357 result->children = NULL;
358 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
360 XGetWindowAttributes (gdk_display, gdk_root_window, &xwa);
361 result->old_event_mask = xwa.your_event_mask;
362 XSelectInput (gdk_display, gdk_root_window,
363 result->old_event_mask | SubstructureNotifyMask);
364 gdk_window_add_filter ((GdkWindow *)&gdk_root_parent,
365 gdk_window_cache_filter, result);
368 gdk_error_warnings = 0;
370 if (XQueryTree(gdk_display, gdk_root_window,
371 &root, &parent, &children, &nchildren) == 0)
374 for (i = 0; i < nchildren ; i++)
376 XGetWindowAttributes (gdk_display, children[i], &xwa);
378 gdk_window_cache_add (result, children[i],
379 xwa.x, xwa.y, xwa.width, xwa.height,
380 xwa.map_state != IsUnmapped);
382 if (gdk_error_code != 0)
386 gdk_window_cache_add (result, children[i],
387 xwa.x, xwa.y, xwa.width, xwa.height,
388 (xwa.map_state != IsUnmapped));
394 gdk_error_warnings = old_warnings;
400 gdk_window_cache_destroy (GdkWindowCache *cache)
402 XSelectInput (gdk_display, gdk_root_window, cache->old_event_mask);
403 gdk_window_remove_filter ((GdkWindow *)&gdk_root_parent,
404 gdk_window_cache_filter, cache);
406 g_list_foreach (cache->children, (GFunc)g_free, NULL);
407 g_list_free (cache->children);
408 g_hash_table_destroy (cache->child_hash);
414 get_client_window_at_coords_recurse (Window win,
418 Window root, tmp_parent, *children;
419 unsigned int nchildren;
424 unsigned long nitems, after;
427 static Atom wm_state_atom = None;
430 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
432 XGetWindowProperty (gdk_display, win,
433 wm_state_atom, 0, 0, False, AnyPropertyType,
434 &type, &format, &nitems, &after, &data);
436 if (gdk_error_code != 0)
449 /* This is beautiful! Damn Enlightenment and click-to-focus */
450 XTranslateCoordinates (gdk_display, gdk_root_window, win,
451 x_root, y_root, &dest_x, &dest_y, &child);
453 if (gdk_error_code != 0)
460 if (XQueryTree(gdk_display, win,
461 &root, &tmp_parent, &children, &nchildren) == 0)
464 if (gdk_error_code == 0)
466 for (i = nchildren - 1; (i >= 0) && (child == None); i--)
468 XWindowAttributes xwa;
470 XGetWindowAttributes (gdk_display, children[i], &xwa);
472 if (gdk_error_code != 0)
474 else if ((xwa.map_state == IsViewable) && (xwa.class == InputOutput) &&
475 (x >= xwa.x) && (x < xwa.x + (gint)xwa.width) &&
476 (y >= xwa.y) && (y < xwa.y + (gint)xwa.height))
491 return get_client_window_at_coords_recurse (child, x, y);
497 get_client_window_at_coords (GdkWindowCache *cache,
503 Window retval = None;
505 gint old_warnings = gdk_error_warnings;
508 gdk_error_warnings = 0;
510 tmp_list = cache->children;
512 while (tmp_list && !retval)
514 GdkCacheChild *child = tmp_list->data;
516 if ((child->xid != ignore) && (child->mapped))
518 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
519 (y_root >= child->y) && (y_root < child->y + child->height))
521 retval = get_client_window_at_coords_recurse (child->xid,
529 tmp_list = tmp_list->next;
532 gdk_error_warnings = old_warnings;
536 return gdk_root_window;
541 get_client_window_at_coords_recurse (Window win,
548 unsigned long nitems, after;
552 static Atom wm_state_atom = None;
555 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
557 XGetWindowProperty (gdk_display, win,
558 wm_state_atom, 0, 0, False, AnyPropertyType,
559 &type, &format, &nitems, &after, &data);
561 if (gdk_error_code != 0)
573 XTranslateCoordinates (gdk_display, gdk_root_window, win,
574 x_root, y_root, &dest_x, &dest_y, &child);
576 if (gdk_error_code != 0)
583 return get_client_window_at_coords_recurse (child, x_root, y_root);
589 get_client_window_at_coords (Window ignore,
593 Window root, parent, *children;
594 unsigned int nchildren;
596 Window retval = None;
598 gint old_warnings = gdk_error_warnings;
601 gdk_error_warnings = 0;
603 if (XQueryTree(gdk_display, gdk_root_window,
604 &root, &parent, &children, &nchildren) == 0)
607 for (i = nchildren - 1; (i >= 0) && (retval == None); i--)
609 if (children[i] != ignore)
611 XWindowAttributes xwa;
613 XGetWindowAttributes (gdk_display, children[i], &xwa);
615 if (gdk_error_code != 0)
617 else if ((xwa.map_state == IsViewable) &&
618 (x_root >= xwa.x) && (x_root < xwa.x + (gint)xwa.width) &&
619 (y_root >= xwa.y) && (y_root < xwa.y + (gint)xwa.height))
621 retval = get_client_window_at_coords_recurse (children[i],
624 retval = children[i];
631 gdk_error_warnings = old_warnings;
635 return gdk_root_window;
639 /*************************************************************
640 ***************************** MOTIF *************************
641 *************************************************************/
643 /* values used in the message type for Motif DND */
656 /* Values used to specify type of protocol to use */
660 XmDRAG_PREFER_PREREGISTER,
662 XmDRAG_PREFER_DYNAMIC,
664 XmDRAG_PREFER_RECEIVER
667 /* Operation codes */
675 /* Drop site status */
677 XmNO_DROP_SITE = 0x01,
678 XmDROP_SITE_INVALID = 0x02,
679 XmDROP_SITE_VALID = 0x03
682 /* completion status */
690 /* Static data for MOTIF DND */
691 static GList **motif_target_lists = NULL;
692 static gint motif_n_target_lists = -1;
694 /* Byte swapping routines. The motif specification leaves it
695 * up to us to save a few bytes in the client messages
697 static gchar local_byte_order = '\0';
699 #ifdef G_ENABLE_DEBUG
701 print_target_list (GList *targets)
705 gchar *name = gdk_atom_name (GPOINTER_TO_INT (targets->data));
706 g_message ("\t%s", name);
708 targets = targets->next;
711 #endif /* G_ENABLE_DEBUG */
714 init_byte_order (void)
716 guint32 myint = 0x01020304;
717 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
721 card16_to_host (guint16 x, gchar byte_order) {
722 if (byte_order == local_byte_order)
725 return (x << 8) | (x >> 8);
729 card32_to_host (guint32 x, gchar byte_order) {
730 if (byte_order == local_byte_order)
733 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
736 /* Motif packs together fields of varying length into the
737 * client message. We can't rely on accessing these
738 * through data.s[], data.l[], etc, because on some architectures
739 * (i.e., Alpha) these won't be valid for format == 8.
742 #define MOTIF_XCLIENT_BYTE(xevent,i) \
743 (xevent)->xclient.data.b[i]
744 #define MOTIF_XCLIENT_SHORT(xevent,i) \
745 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
746 #define MOTIF_XCLIENT_LONG(xevent,i) \
747 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
749 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
750 #define MOTIF_UNPACK_SHORT(xevent,i) \
751 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
752 #define MOTIF_UNPACK_LONG(xevent,i) \
753 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
755 /***** Dest side ***********/
757 /* Property placed on source windows */
758 typedef struct _MotifDragInitiatorInfo {
760 guint8 protocol_version;
761 guint16 targets_index;
762 guint32 selection_atom;
763 } MotifDragInitiatorInfo;
765 /* Header for target table on the drag window */
766 typedef struct _MotifTargetTableHeader {
768 guchar protocol_version;
771 } MotifTargetTableHeader;
773 /* Property placed on target windows */
774 typedef struct _MotifDragReceiverInfo {
776 guint8 protocol_version;
777 guint8 protocol_style;
779 guint32 proxy_window;
780 guint16 num_drop_sites;
783 } MotifDragReceiverInfo;
785 static Window motif_drag_window = None;
786 static GdkWindow *motif_drag_gdk_window = NULL;
788 static GdkAtom motif_drag_targets_atom = GDK_NONE;
789 static GdkAtom motif_drag_receiver_info_atom = GDK_NONE;
791 /* Target table handling */
794 motif_drag_window_filter (GdkXEvent *xevent,
798 XEvent *xev = (XEvent *)xevent;
800 switch (xev->xany.type)
803 motif_drag_window = None;
804 motif_drag_gdk_window = NULL;
807 if (motif_target_lists &&
808 motif_drag_targets_atom &&
809 (xev->xproperty.atom == motif_drag_targets_atom))
810 motif_read_target_table();
813 return GDK_FILTER_REMOVE;
816 static Atom motif_drag_window_atom = GDK_NONE;
819 motif_lookup_drag_window (Display *display)
821 Window retval = None;
822 gulong bytes_after, nitems;
827 XGetWindowProperty (gdk_display, gdk_root_window, motif_drag_window_atom,
829 XA_WINDOW, &type, &format, &nitems, &bytes_after,
832 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
834 retval = *(Window *)data;
836 g_message ("Found drag window %#lx\n", motif_drag_window));
845 /* Finds the window where global Motif drag information is stored.
846 * If it doesn't exist and 'create' is TRUE, create one.
849 motif_find_drag_window (gboolean create)
851 if (!motif_drag_window)
853 if (!motif_drag_window_atom)
854 motif_drag_window_atom = gdk_atom_intern ("_MOTIF_DRAG_WINDOW", TRUE);
856 motif_drag_window = motif_lookup_drag_window (gdk_display);
858 if (!motif_drag_window && create)
860 /* Create a persistant window. (Copied from LessTif) */
863 XSetWindowAttributes attr;
864 display = XOpenDisplay (NULL);
865 XSetCloseDownMode (display, RetainPermanent);
867 XGrabServer (display);
869 motif_drag_window = motif_lookup_drag_window (display);
871 if (!motif_drag_window)
873 attr.override_redirect = True;
874 attr.event_mask = PropertyChangeMask;
877 XCreateWindow(display, DefaultRootWindow(display),
878 -100, -100, 10, 10, 0, 0,
879 InputOnly, CopyFromParent,
880 (CWOverrideRedirect | CWEventMask), &attr);
883 g_message ("Created drag window %#lx\n", motif_drag_window));
885 XChangeProperty (display, gdk_root_window,
886 motif_drag_window_atom, XA_WINDOW,
888 (guchar *)&motif_drag_window_atom, 1);
891 XUngrabServer (display);
892 XCloseDisplay (display);
895 /* There is a miniscule race condition here if the drag window
896 * gets destroyed exactly now.
898 if (motif_drag_window)
900 motif_drag_gdk_window = gdk_window_foreign_new (motif_drag_window);
901 gdk_window_add_filter (motif_drag_gdk_window,
902 motif_drag_window_filter,
907 return motif_drag_window;
911 motif_read_target_table (void)
913 gulong bytes_after, nitems;
918 if (!motif_drag_targets_atom)
919 motif_drag_targets_atom = gdk_atom_intern ("_MOTIF_DRAG_TARGETS", FALSE);
921 if (motif_target_lists)
923 for (i=0; i<motif_n_target_lists; i++)
924 g_list_free (motif_target_lists[i]);
926 g_free (motif_target_lists);
927 motif_target_lists = NULL;
928 motif_n_target_lists = 0;
931 if (motif_find_drag_window (FALSE))
933 MotifTargetTableHeader *header = NULL;
934 guchar *target_bytes = NULL;
936 gboolean success = FALSE;
938 XGetWindowProperty (gdk_display, motif_drag_window,
939 motif_drag_targets_atom,
940 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
941 motif_drag_targets_atom,
942 &type, &format, &nitems, &bytes_after,
945 if ((format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
948 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
949 header->total_size = card32_to_host (header->total_size, header->byte_order);
951 XGetWindowProperty (gdk_display, motif_drag_window, motif_drag_targets_atom,
952 (sizeof(MotifTargetTableHeader)+3)/4,
953 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
955 motif_drag_targets_atom, &type, &format, &nitems,
956 &bytes_after, &target_bytes);
958 if ((format != 8) || (bytes_after != 0) ||
959 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
962 motif_n_target_lists = header->n_lists;
963 motif_target_lists = g_new0 (GList *, motif_n_target_lists);
966 for (i=0; i<header->n_lists; i++)
971 if (p + sizeof(guint16) - target_bytes > nitems)
974 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
976 /* We need to make a copy of the targets, since it may
979 targets = g_new (guint32, n_targets);
980 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
982 p += sizeof(guint16) + n_targets * sizeof(guint32);
983 if (p - target_bytes > nitems)
986 for (j=0; j<n_targets; j++)
987 motif_target_lists[i] =
988 g_list_prepend (motif_target_lists[i],
989 GUINT_TO_POINTER (card32_to_host (targets[j],
990 header->byte_order)));
992 motif_target_lists[i] = g_list_reverse (motif_target_lists[i]);
1002 XFree (target_bytes);
1006 if (motif_target_lists)
1008 g_free (motif_target_lists);
1009 motif_target_lists = NULL;
1010 motif_n_target_lists = 0;
1012 g_warning ("Error reading Motif target table\n");
1018 targets_sort_func (gconstpointer a, gconstpointer b)
1020 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
1021 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1024 /* Check if given (sorted) list is in the targets table */
1026 motif_target_table_check (GList *sorted)
1028 GList *tmp_list1, *tmp_list2;
1031 for (i=0; i<motif_n_target_lists; i++)
1033 tmp_list1 = motif_target_lists[i];
1036 while (tmp_list1 && tmp_list2)
1038 if (tmp_list1->data != tmp_list2->data)
1041 tmp_list1 = tmp_list1->next;
1042 tmp_list2 = tmp_list2->next;
1044 if (!tmp_list1 && !tmp_list2) /* Found it */
1052 motif_add_to_target_table (GList *targets)
1054 GList *sorted = NULL;
1059 /* make a sorted copy of the list */
1063 sorted = g_list_insert_sorted (sorted, targets->data, targets_sort_func);
1064 targets = targets->next;
1067 /* First check if it is their already */
1069 if (motif_target_lists)
1070 index = motif_target_table_check (sorted);
1072 /* We need to grab the server while doing this, to ensure
1078 /* We need to make sure that it exists _before_ we grab the
1079 * server, since we can't open a new connection after we
1082 motif_find_drag_window (TRUE);
1084 XGrabServer(gdk_display);
1085 motif_read_target_table();
1087 /* Check again, in case it was added in the meantime */
1089 if (motif_target_lists)
1090 index = motif_target_table_check (sorted);
1094 guint32 total_size = 0;
1098 MotifTargetTableHeader *header;
1100 if (!motif_target_lists)
1102 motif_target_lists = g_new (GList *, 1);
1103 motif_n_target_lists = 1;
1107 motif_n_target_lists++;
1108 motif_target_lists = g_realloc (motif_target_lists,
1109 sizeof(GList *) * motif_n_target_lists);
1111 motif_target_lists[motif_n_target_lists - 1] = sorted;
1113 index = motif_n_target_lists - 1;
1115 total_size = sizeof (MotifTargetTableHeader);
1116 for (i = 0; i < motif_n_target_lists ; i++)
1117 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (motif_target_lists[i]);
1119 data = g_malloc (total_size);
1121 header = (MotifTargetTableHeader *)data;
1122 p = data + sizeof(MotifTargetTableHeader);
1124 header->byte_order = local_byte_order;
1125 header->protocol_version = 0;
1126 header->n_lists = motif_n_target_lists;
1127 header->total_size = total_size;
1129 for (i = 0; i < motif_n_target_lists ; i++)
1131 guint16 n_targets = g_list_length (motif_target_lists[i]);
1132 guint32 *targets = g_new (guint32, n_targets);
1133 guint32 *p32 = targets;
1135 tmp_list = motif_target_lists[i];
1138 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1140 tmp_list = tmp_list->next;
1145 p += sizeof(guint16);
1147 memcpy (p, targets, n_targets * sizeof(guint32));
1150 p += sizeof(guint32) * n_targets;
1154 XChangeProperty (gdk_display, motif_drag_window,
1155 motif_drag_targets_atom,
1156 motif_drag_targets_atom,
1160 XUngrabServer(gdk_display);
1163 g_list_free (sorted);
1167 /* Translate flags */
1170 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1172 guint recommended_op = flags & 0x000f;
1173 guint possible_ops = (flags & 0x0f0) >> 4;
1175 switch (recommended_op)
1178 context->suggested_action = GDK_ACTION_MOVE;
1181 context->suggested_action = GDK_ACTION_COPY;
1184 context->suggested_action = GDK_ACTION_LINK;
1187 context->suggested_action = GDK_ACTION_COPY;
1191 context->actions = 0;
1192 if (possible_ops & XmDROP_MOVE)
1193 context->actions |= GDK_ACTION_MOVE;
1194 if (possible_ops & XmDROP_COPY)
1195 context->actions |= GDK_ACTION_COPY;
1196 if (possible_ops & XmDROP_LINK)
1197 context->actions |= GDK_ACTION_LINK;
1201 motif_dnd_get_flags (GdkDragContext *context)
1205 switch (context->suggested_action)
1207 case GDK_ACTION_MOVE:
1208 flags = XmDROP_MOVE;
1210 case GDK_ACTION_COPY:
1211 flags = XmDROP_COPY;
1213 case GDK_ACTION_LINK:
1214 flags = XmDROP_LINK;
1217 flags = XmDROP_NOOP;
1221 if (context->actions & GDK_ACTION_MOVE)
1222 flags |= XmDROP_MOVE << 8;
1223 if (context->actions & GDK_ACTION_COPY)
1224 flags |= XmDROP_COPY << 8;
1225 if (context->actions & GDK_ACTION_LINK)
1226 flags |= XmDROP_LINK << 8;
1234 motif_set_targets (GdkDragContext *context)
1236 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1237 MotifDragInitiatorInfo info;
1239 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1241 if (!motif_drag_initiator_info)
1242 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1244 info.byte_order = local_byte_order;
1245 info.protocol_version = 0;
1247 info.targets_index = motif_add_to_target_table (context->targets);
1252 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1254 private->motif_selection = gdk_atom_intern (buf, FALSE);
1255 if (!XGetSelectionOwner (gdk_display, private->motif_selection))
1259 info.selection_atom = private->motif_selection;
1261 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1262 GDK_WINDOW_XWINDOW (context->source_window),
1263 private->motif_selection,
1264 motif_drag_initiator_info, 8, PropModeReplace,
1265 (guchar *)&info, sizeof (info));
1267 private->motif_targets_set = 1;
1271 motif_check_dest (Window win)
1273 gboolean retval = FALSE;
1274 MotifDragReceiverInfo *info;
1277 unsigned long nitems, after;
1279 if (!motif_drag_receiver_info_atom)
1280 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
1282 XGetWindowProperty (gdk_display, win,
1283 motif_drag_receiver_info_atom,
1284 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1285 &type, &format, &nitems, &after,
1290 if ((format == 8) && (nitems == sizeof(*info)))
1292 if ((info->protocol_version == 0) &&
1293 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1294 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1295 (info->protocol_style == XmDRAG_DYNAMIC)))
1301 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1307 return retval ? win : GDK_NONE;
1312 motif_send_enter (GdkDragContext *context,
1316 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1318 xev.xclient.type = ClientMessage;
1319 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1320 xev.xclient.format = 8;
1321 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1323 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1324 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1325 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1326 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1327 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XWINDOW (context->source_window);
1329 if (!private->motif_targets_set)
1330 motif_set_targets (context);
1332 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1334 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1337 g_message ("Send event to %lx failed",
1338 GDK_WINDOW_XWINDOW (context->dest_window)));
1342 motif_send_leave (GdkDragContext *context,
1347 xev.xclient.type = ClientMessage;
1348 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1349 xev.xclient.format = 8;
1350 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1352 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1353 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1354 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1355 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1356 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XWINDOW (context->source_window);
1357 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1359 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1362 g_message ("Send event to %lx failed",
1363 GDK_WINDOW_XWINDOW (context->dest_window)));
1367 motif_send_motion (GdkDragContext *context,
1370 GdkDragAction action,
1375 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1377 xev.xclient.type = ClientMessage;
1378 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1379 xev.xclient.format = 8;
1380 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1382 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1383 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1384 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1386 if ((context->suggested_action != private->old_action) ||
1387 (context->actions != private->old_actions))
1389 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1391 /* private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT; */
1396 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1398 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1399 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1401 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1405 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1408 g_message ("Send event to %lx failed",
1409 GDK_WINDOW_XWINDOW (context->dest_window)));
1415 motif_send_drop (GdkDragContext *context, guint32 time)
1418 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1420 xev.xclient.type = ClientMessage;
1421 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1422 xev.xclient.format = 8;
1423 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1425 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1426 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1427 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1428 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1430 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1431 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1433 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1434 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XWINDOW (context->source_window);
1436 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1439 g_message ("Send event to %lx failed",
1440 GDK_WINDOW_XWINDOW (context->dest_window)));
1446 motif_read_initiator_info (Window source_window,
1452 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1458 MotifDragInitiatorInfo *initiator_info;
1460 if (!motif_drag_initiator_info)
1461 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1463 XGetWindowProperty (gdk_display, source_window, atom,
1464 0, sizeof(*initiator_info), FALSE,
1465 motif_drag_initiator_info,
1466 &type, &format, &nitems, &bytes_after,
1467 (guchar **)&initiator_info);
1469 if ((format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1471 g_warning ("Error reading initiator info\n");
1475 motif_read_target_table ();
1477 initiator_info->targets_index =
1478 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1479 initiator_info->selection_atom =
1480 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1482 if (initiator_info->targets_index >= motif_n_target_lists)
1484 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1485 XFree (initiator_info);
1486 return GDK_FILTER_REMOVE;
1489 tmp_list = g_list_last (motif_target_lists[initiator_info->targets_index]);
1494 *targets = g_list_prepend (*targets,
1496 tmp_list = tmp_list->prev;
1499 #ifdef G_ENABLE_DEBUG
1500 if (gdk_debug_flags & GDK_DEBUG_DND)
1501 print_target_list (*targets);
1502 #endif /* G_ENABLE_DEBUG */
1504 *selection = initiator_info->selection_atom;
1506 XFree (initiator_info);
1511 static GdkDragContext *
1512 motif_drag_context_new (GdkWindow *dest_window,
1514 guint32 source_window,
1517 GdkDragContext *new_context;
1518 GdkDragContextPrivate *private;
1520 /* FIXME, current_dest_drag really shouldn't be NULL'd
1521 * if we error below.
1523 if (current_dest_drag != NULL)
1525 if (timestamp >= current_dest_drag->start_time)
1527 gdk_drag_context_unref (current_dest_drag);
1528 current_dest_drag = NULL;
1534 new_context = gdk_drag_context_new ();
1535 private = (GdkDragContextPrivate *)new_context;
1537 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1538 new_context->is_source = FALSE;
1540 new_context->source_window = gdk_window_lookup (source_window);
1541 if (new_context->source_window)
1542 gdk_window_ref (new_context->source_window);
1545 new_context->source_window = gdk_window_foreign_new (source_window);
1546 if (!new_context->source_window)
1548 gdk_drag_context_unref (new_context);
1553 new_context->dest_window = dest_window;
1554 gdk_window_ref (dest_window);
1555 new_context->start_time = timestamp;
1557 if (!motif_read_initiator_info(source_window,
1559 &new_context->targets,
1560 &private->motif_selection))
1562 gdk_drag_context_unref (new_context);
1570 * The MOTIF drag protocol has no real provisions for distinguishing
1571 * multiple simultaneous drops. If the sources grab the pointer
1572 * when doing drags, that shouldn't happen, in any case. If it
1573 * does, we can't do much except hope for the best.
1576 static GdkFilterReturn
1577 motif_top_level_enter (GdkEvent *event,
1580 guint32 source_window,
1583 GdkDragContext *new_context;
1585 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1586 flags, timestamp, source_window, atom));
1588 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1590 return GDK_FILTER_REMOVE;
1592 event->dnd.type = GDK_DRAG_ENTER;
1593 event->dnd.context = new_context;
1594 gdk_drag_context_ref (new_context);
1596 current_dest_drag = new_context;
1598 return GDK_FILTER_TRANSLATE;
1602 motif_top_level_leave (GdkEvent *event,
1606 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1609 if ((current_dest_drag != NULL) &&
1610 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1611 (timestamp >= current_dest_drag->start_time))
1613 event->dnd.type = GDK_DRAG_LEAVE;
1614 /* Pass ownership of context to the event */
1615 event->dnd.context = current_dest_drag;
1617 current_dest_drag = NULL;
1619 return GDK_FILTER_TRANSLATE;
1622 return GDK_FILTER_REMOVE;
1626 motif_motion (GdkEvent *event,
1632 GdkDragContextPrivate *private;
1634 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1635 flags, timestamp, x_root, y_root));
1637 if ((current_dest_drag != NULL) &&
1638 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1639 (timestamp >= current_dest_drag->start_time))
1641 private = (GdkDragContextPrivate *)current_dest_drag;
1643 event->dnd.type = GDK_DRAG_MOTION;
1644 event->dnd.context = current_dest_drag;
1645 gdk_drag_context_ref (current_dest_drag);
1647 event->dnd.time = timestamp;
1649 motif_dnd_translate_flags (current_dest_drag, flags);
1651 event->dnd.x_root = x_root;
1652 event->dnd.y_root = y_root;
1654 private->last_x = x_root;
1655 private->last_y = y_root;
1657 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1659 return GDK_FILTER_TRANSLATE;
1662 return GDK_FILTER_REMOVE;
1666 motif_operation_changed (GdkEvent *event,
1670 GdkDragContextPrivate *private;
1672 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1675 if ((current_dest_drag != NULL) &&
1676 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1677 (timestamp >= current_dest_drag->start_time))
1679 event->dnd.type = GDK_DRAG_MOTION;
1680 event->dnd.send_event = FALSE;
1681 event->dnd.context = current_dest_drag;
1682 gdk_drag_context_ref (current_dest_drag);
1684 event->dnd.time = timestamp;
1685 private = (GdkDragContextPrivate *)current_dest_drag;
1687 motif_dnd_translate_flags (current_dest_drag, flags);
1689 event->dnd.x_root = private->last_x;
1690 event->dnd.y_root = private->last_y;
1692 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1694 return GDK_FILTER_TRANSLATE;
1697 return GDK_FILTER_REMOVE;
1701 motif_drop_start (GdkEvent *event,
1704 guint32 source_window,
1709 GdkDragContext *new_context;
1712 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1713 flags, timestamp, x_root, y_root, source_window, atom));
1715 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1717 return GDK_FILTER_REMOVE;
1719 motif_dnd_translate_flags (new_context, flags);
1721 event->dnd.type = GDK_DROP_START;
1722 event->dnd.context = new_context;
1723 event->dnd.time = timestamp;
1724 event->dnd.x_root = x_root;
1725 event->dnd.y_root = y_root;
1727 gdk_drag_context_ref (new_context);
1728 current_dest_drag = new_context;
1730 return GDK_FILTER_TRANSLATE;
1734 motif_drag_status (GdkEvent *event,
1738 GdkDragContext *context;
1741 g_message ("Motif status message: flags %x", flags));
1743 context = gdk_drag_context_find (TRUE,
1744 GDK_WINDOW_XWINDOW (event->any.window),
1749 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1750 if ((private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT) ||
1751 (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT))
1752 private->drag_status = GDK_DRAG_STATUS_DRAG;
1754 event->dnd.type = GDK_DRAG_STATUS;
1755 event->dnd.send_event = FALSE;
1756 event->dnd.context = context;
1757 gdk_drag_context_ref (context);
1759 event->dnd.time = timestamp;
1761 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1763 switch (flags & 0x000f)
1766 context->action = 0;
1769 context->action = GDK_ACTION_MOVE;
1772 context->action = GDK_ACTION_COPY;
1775 context->action = GDK_ACTION_LINK;
1780 context->action = 0;
1782 return GDK_FILTER_TRANSLATE;
1784 return GDK_FILTER_REMOVE;
1788 motif_dnd_filter (GdkXEvent *xev,
1792 XEvent *xevent = (XEvent *)xev;
1797 guint32 source_window;
1799 gint16 x_root, y_root;
1802 /* First read some fields common to all Motif DND messages */
1804 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1805 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1806 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1808 is_reply = ((reason & 0x80) != 0);
1810 switch (reason & 0x7f)
1812 case XmTOP_LEVEL_ENTER:
1813 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1814 atom = MOTIF_UNPACK_LONG (xevent, 3);
1815 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1816 case XmTOP_LEVEL_LEAVE:
1817 return motif_top_level_leave (event, flags, timestamp);
1820 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1821 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1824 return motif_motion (event, flags, timestamp, x_root, y_root);
1826 return motif_drag_status (event, flags, timestamp);
1828 case XmDROP_SITE_ENTER:
1829 return motif_drag_status (event, flags, timestamp);
1831 case XmDROP_SITE_LEAVE:
1832 return motif_drag_status (event,
1833 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1836 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1837 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1838 atom = MOTIF_UNPACK_LONG (xevent, 3);
1839 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1842 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1845 case XmOPERATION_CHANGED:
1847 return motif_operation_changed (event, flags, timestamp);
1849 return motif_drag_status (event, flags, timestamp);
1852 /* To the best of my knowledge, these next two messages are
1853 * not part of the protocol, though they are defined in
1857 case XmDRAG_DROP_FINISH:
1861 return GDK_FILTER_REMOVE;
1864 /*************************************************************
1865 ***************************** XDND **************************
1866 *************************************************************/
1868 /* Utility functions */
1873 GdkDragAction action;
1874 } xdnd_actions_table[] = {
1875 { "XdndActionCopy", GDK_NONE, GDK_ACTION_COPY },
1876 { "XdndActionMove", GDK_NONE, GDK_ACTION_MOVE },
1877 { "XdndActionLink", GDK_NONE, GDK_ACTION_LINK },
1878 { "XdndActionAsk", GDK_NONE, GDK_ACTION_ASK },
1879 { "XdndActionPrivate", GDK_NONE, GDK_ACTION_COPY },
1882 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1883 static gboolean xdnd_actions_initialized = FALSE;
1886 xdnd_initialize_actions (void)
1890 xdnd_actions_initialized = TRUE;
1891 for (i=0; i < xdnd_n_actions; i++)
1892 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1895 static GdkDragAction
1896 xdnd_action_from_atom (GdkAtom atom)
1900 if (!xdnd_actions_initialized)
1901 xdnd_initialize_actions();
1903 for (i=0; i<xdnd_n_actions; i++)
1904 if (atom == xdnd_actions_table[i].atom)
1905 return xdnd_actions_table[i].action;
1911 xdnd_action_to_atom (GdkDragAction action)
1915 if (!xdnd_actions_initialized)
1916 xdnd_initialize_actions();
1918 for (i=0; i<xdnd_n_actions; i++)
1919 if (action == xdnd_actions_table[i].action)
1920 return xdnd_actions_table[i].atom;
1925 static GdkAtom xdnd_aware_atom = GDK_NONE;
1929 static GdkFilterReturn
1930 xdnd_status_filter (GdkXEvent *xev,
1934 XEvent *xevent = (XEvent *)xev;
1935 guint32 dest_window = xevent->xclient.data.l[0];
1936 guint32 flags = xevent->xclient.data.l[1];
1937 GdkAtom action = xevent->xclient.data.l[4];
1938 GdkDragContext *context;
1941 g_message ("XdndStatus: dest_window: %#x action: %ld",
1942 dest_window, action));
1945 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
1948 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1949 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1950 private->drag_status = GDK_DRAG_STATUS_DRAG;
1952 event->dnd.send_event = FALSE;
1953 event->dnd.type = GDK_DRAG_STATUS;
1954 event->dnd.context = context;
1955 gdk_drag_context_ref (context);
1957 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1958 if (!(action != 0) != !(flags & 1))
1961 g_warning ("Received status event with flags not corresponding to action!\n"));
1965 context->action = xdnd_action_from_atom (action);
1967 return GDK_FILTER_TRANSLATE;
1970 return GDK_FILTER_REMOVE;
1973 static GdkFilterReturn
1974 xdnd_finished_filter (GdkXEvent *xev,
1978 XEvent *xevent = (XEvent *)xev;
1979 guint32 dest_window = xevent->xclient.data.l[0];
1980 GdkDragContext *context;
1983 g_message ("XdndFinished: dest_window: %#x", dest_window));
1985 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
1988 event->dnd.type = GDK_DROP_FINISHED;
1989 event->dnd.context = context;
1990 gdk_drag_context_ref (context);
1992 return GDK_FILTER_TRANSLATE;
1995 return GDK_FILTER_REMOVE;
1999 xdnd_set_targets (GdkDragContext *context)
2001 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2003 GList *tmp_list = context->targets;
2005 gint n_atoms = g_list_length (context->targets);
2007 atomlist = g_new (GdkAtom, n_atoms);
2011 atomlist[i] = GPOINTER_TO_INT (tmp_list->data);
2012 tmp_list = tmp_list->next;
2016 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2017 GDK_WINDOW_XWINDOW (context->source_window),
2018 gdk_atom_intern ("XdndTypeList", FALSE),
2019 XA_ATOM, 32, PropModeReplace,
2020 (guchar *)atomlist, n_atoms);
2022 private->xdnd_targets_set = 1;
2026 xdnd_set_actions (GdkDragContext *context)
2028 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2034 if (!xdnd_actions_initialized)
2035 xdnd_initialize_actions();
2037 actions = context->actions;
2039 for (i=0; i<xdnd_n_actions; i++)
2041 if (actions & xdnd_actions_table[i].action)
2043 actions &= ~xdnd_actions_table[i].action;
2048 atomlist = g_new (GdkAtom, n_atoms);
2050 actions = context->actions;
2052 for (i=0; i<xdnd_n_actions; i++)
2054 if (actions & xdnd_actions_table[i].action)
2056 actions &= ~xdnd_actions_table[i].action;
2057 atomlist[n_atoms] = xdnd_actions_table[i].atom;
2062 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2063 GDK_WINDOW_XWINDOW (context->source_window),
2064 gdk_atom_intern ("XdndActionList", FALSE),
2065 XA_ATOM, 32, PropModeReplace,
2066 (guchar *)atomlist, n_atoms);
2068 private->xdnd_actions_set = 1;
2069 private->xdnd_actions = context->actions;
2072 /*************************************************************
2074 * Like gdk_send_event, but if the target is the root
2075 * window, sets an event mask of ButtonPressMask, otherwise
2076 * an event mask of 0.
2080 *************************************************************/
2083 xdnd_send_xevent (Window window, gboolean propagate,
2086 if (window == gdk_root_window)
2087 return gdk_send_xevent (window, propagate, ButtonPressMask, event_send);
2089 return gdk_send_xevent (window, propagate, 0, event_send);
2093 xdnd_send_enter (GdkDragContext *context)
2096 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2098 xev.xclient.type = ClientMessage;
2099 xev.xclient.message_type = gdk_atom_intern ("XdndEnter", FALSE);
2100 xev.xclient.format = 32;
2102 xev.xclient.window = private->dest_xid ?
2104 GDK_WINDOW_XWINDOW (context->dest_window);
2106 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2108 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2109 xev.xclient.data.l[1] = (3 << 24); /* version */
2110 xev.xclient.data.l[2] = 0;
2111 xev.xclient.data.l[3] = 0;
2112 xev.xclient.data.l[4] = 0;
2114 if (!private->xdnd_selection)
2115 private->xdnd_selection = gdk_atom_intern ("XdndSelection", FALSE);
2117 if (g_list_length (context->targets) > 3)
2119 if (!private->xdnd_targets_set)
2120 xdnd_set_targets (context);
2121 xev.xclient.data.l[1] |= 1;
2125 GList *tmp_list = context->targets;
2130 xev.xclient.data.l[i] = GPOINTER_TO_INT (tmp_list->data);
2131 tmp_list = tmp_list->next;
2136 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2140 g_message ("Send event to %lx failed",
2141 GDK_WINDOW_XWINDOW (context->dest_window)));
2142 gdk_window_unref (context->dest_window);
2143 context->dest_window = NULL;
2148 xdnd_send_leave (GdkDragContext *context)
2152 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2154 xev.xclient.type = ClientMessage;
2155 xev.xclient.message_type = gdk_atom_intern ("XdndLeave", FALSE);
2156 xev.xclient.format = 32;
2158 xev.xclient.window = private->dest_xid ?
2160 GDK_WINDOW_XWINDOW (context->dest_window);
2162 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2164 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2165 xev.xclient.data.l[1] = 0;
2166 xev.xclient.data.l[2] = 0;
2167 xev.xclient.data.l[3] = 0;
2168 xev.xclient.data.l[4] = 0;
2170 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2174 g_message ("Send event to %lx failed",
2175 GDK_WINDOW_XWINDOW (context->dest_window)));
2176 gdk_window_unref (context->dest_window);
2177 context->dest_window = NULL;
2182 xdnd_send_drop (GdkDragContext *context, guint32 time)
2184 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2187 xev.xclient.type = ClientMessage;
2188 xev.xclient.message_type = gdk_atom_intern ("XdndDrop", FALSE);
2189 xev.xclient.format = 32;
2191 xev.xclient.window = private->dest_xid ?
2193 GDK_WINDOW_XWINDOW (context->dest_window);
2195 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2197 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2198 xev.xclient.data.l[1] = 0;
2199 xev.xclient.data.l[2] = time;
2200 xev.xclient.data.l[3] = 0;
2201 xev.xclient.data.l[4] = 0;
2203 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2207 g_message ("Send event to %lx failed",
2208 GDK_WINDOW_XWINDOW (context->dest_window)));
2209 gdk_window_unref (context->dest_window);
2210 context->dest_window = NULL;
2215 xdnd_send_motion (GdkDragContext *context,
2218 GdkDragAction action,
2221 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2224 xev.xclient.type = ClientMessage;
2225 xev.xclient.message_type = gdk_atom_intern ("XdndPosition", FALSE);
2226 xev.xclient.format = 32;
2228 xev.xclient.window = private->dest_xid ?
2230 GDK_WINDOW_XWINDOW (context->dest_window);
2232 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2234 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2235 xev.xclient.data.l[1] = 0;
2236 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2237 xev.xclient.data.l[3] = time;
2238 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
2240 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2244 g_message ("Send event to %lx failed",
2245 GDK_WINDOW_XWINDOW (context->dest_window)));
2246 gdk_window_unref (context->dest_window);
2247 context->dest_window = NULL;
2249 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2253 xdnd_check_dest (Window win)
2255 gboolean retval = FALSE;
2258 unsigned long nitems, after;
2262 static GdkAtom xdnd_proxy_atom = GDK_NONE;
2264 gint old_warnings = gdk_error_warnings;
2266 if (!xdnd_proxy_atom)
2267 xdnd_proxy_atom = gdk_atom_intern ("XdndProxy", FALSE);
2269 if (!xdnd_aware_atom)
2270 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
2275 gdk_error_warnings = 0;
2277 XGetWindowProperty (gdk_display, win,
2279 1, False, AnyPropertyType,
2280 &type, &format, &nitems, &after,
2281 (guchar **)&proxy_data);
2283 if (!gdk_error_code)
2287 if ((format == 32) && (nitems == 1))
2289 proxy = *proxy_data;
2293 g_warning ("Invalid XdndOwner property on window %ld\n", win));
2298 XGetWindowProperty (gdk_display, proxy ? proxy : win,
2300 1, False, AnyPropertyType,
2301 &type, &format, &nitems, &after,
2302 (guchar **)&version);
2304 if (!gdk_error_code && type != None)
2306 if ((format == 32) && (nitems == 1))
2313 g_warning ("Invalid XdndAware property on window %ld\n", win));
2320 gdk_error_warnings = old_warnings;
2323 return retval ? (proxy ? proxy : win) : GDK_NONE;
2329 xdnd_read_actions (GdkDragContext *context)
2333 gulong nitems, after;
2338 gint old_warnings = gdk_error_warnings;
2341 gdk_error_warnings = 0;
2343 /* Get the XdndActionList, if set */
2345 XGetWindowProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2346 GDK_WINDOW_XWINDOW (context->source_window),
2347 gdk_atom_intern ("XdndActionList", FALSE), 0, 65536,
2348 False, XA_ATOM, &type, &format, &nitems,
2349 &after, (guchar **)&data);
2351 if (!gdk_error_code && (format == 32) && (type == XA_ATOM))
2353 context->actions = 0;
2355 for (i=0; i<nitems; i++)
2356 context->actions |= xdnd_action_from_atom (data[i]);
2358 ((GdkDragContextPrivate *)context)->xdnd_have_actions = TRUE;
2360 #ifdef G_ENABLE_DEBUG
2361 if (gdk_debug_flags & GDK_DEBUG_DND)
2363 GString *action_str = g_string_new (NULL);
2364 if (context->actions & GDK_ACTION_MOVE)
2365 g_string_append(action_str, "MOVE ");
2366 if (context->actions & GDK_ACTION_COPY)
2367 g_string_append(action_str, "COPY ");
2368 if (context->actions & GDK_ACTION_LINK)
2369 g_string_append(action_str, "LINK ");
2370 if (context->actions & GDK_ACTION_ASK)
2371 g_string_append(action_str, "ASK ");
2373 g_message("Xdnd actions = %s", action_str->str);
2374 g_string_free (action_str, TRUE);
2376 #endif /* G_ENABLE_DEBUG */
2381 gdk_error_warnings = old_warnings;
2385 /* We have to make sure that the XdndActionList we keep internally
2386 * is up to date with the XdndActionList on the source window
2387 * because we get no notification, because Xdnd wasn't meant
2388 * to continually send actions. So we select on PropertyChangeMask
2389 * and add this filter.
2391 static GdkFilterReturn
2392 xdnd_source_window_filter (GdkXEvent *xev,
2396 XEvent *xevent = (XEvent *)xev;
2397 GdkDragContext *context = cb_data;
2399 if ((xevent->xany.type == PropertyNotify) &&
2400 (xevent->xproperty.atom == gdk_atom_intern ("XdndActionList", FALSE)))
2402 xdnd_read_actions (context);
2404 return GDK_FILTER_REMOVE;
2407 return GDK_FILTER_CONTINUE;
2411 xdnd_manage_source_filter (GdkDragContext *context,
2413 gboolean add_filter)
2415 gint old_warnings = 0; /* quiet gcc */
2416 GdkWindowPrivate *private = (GdkWindowPrivate *)window;
2418 gboolean is_foreign = (private->window_type == GDK_WINDOW_FOREIGN);
2422 old_warnings = gdk_error_warnings;
2423 gdk_error_warnings = 0;
2426 if (!private->destroyed)
2430 gdk_window_set_events (window,
2431 gdk_window_get_events (window) |
2432 GDK_PROPERTY_CHANGE_MASK);
2433 gdk_window_add_filter (window, xdnd_source_window_filter, context);
2438 gdk_window_remove_filter (window,
2439 xdnd_source_window_filter,
2441 /* Should we remove the GDK_PROPERTY_NOTIFY mask?
2442 * but we might want it for other reasons. (Like
2443 * INCR selection transactions).
2451 gdk_error_warnings = old_warnings;
2455 static GdkFilterReturn
2456 xdnd_enter_filter (GdkXEvent *xev,
2460 XEvent *xevent = (XEvent *)xev;
2461 GdkDragContext *new_context;
2466 gulong nitems, after;
2469 guint32 source_window = xevent->xclient.data.l[0];
2470 gboolean get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2471 gint version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2474 g_message ("XdndEnter: source_window: %#x, version: %#x",
2475 source_window, version));
2479 /* Old source ignore */
2480 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2481 return GDK_FILTER_REMOVE;
2484 if (current_dest_drag != NULL)
2486 gdk_drag_context_unref (current_dest_drag);
2487 current_dest_drag = NULL;
2490 new_context = gdk_drag_context_new ();
2491 new_context->protocol = GDK_DRAG_PROTO_XDND;
2492 new_context->is_source = FALSE;
2494 new_context->source_window = gdk_window_lookup (source_window);
2495 if (new_context->source_window)
2496 gdk_window_ref (new_context->source_window);
2499 new_context->source_window = gdk_window_foreign_new (source_window);
2500 if (!new_context->source_window)
2502 gdk_drag_context_unref (new_context);
2503 return GDK_FILTER_REMOVE;
2506 new_context->dest_window = event->any.window;
2507 gdk_window_ref (new_context->dest_window);
2509 new_context->targets = NULL;
2512 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2514 gdk_atom_intern ("XdndTypeList", FALSE), 0, 65536,
2515 False, XA_ATOM, &type, &format, &nitems,
2516 &after, (guchar **)&data);
2518 if ((format != 32) || (type != XA_ATOM))
2520 gdk_drag_context_unref (new_context);
2521 return GDK_FILTER_REMOVE;
2524 for (i=0; i<nitems; i++)
2525 new_context->targets = g_list_append (new_context->targets,
2526 GUINT_TO_POINTER (data[i]));
2533 if (xevent->xclient.data.l[2+i])
2534 new_context->targets = g_list_append (new_context->targets,
2535 GUINT_TO_POINTER (xevent->xclient.data.l[2+i]));
2538 #ifdef G_ENABLE_DEBUG
2539 if (gdk_debug_flags & GDK_DEBUG_DND)
2540 print_target_list (new_context->targets);
2541 #endif /* G_ENABLE_DEBUG */
2543 xdnd_manage_source_filter (new_context, new_context->source_window, TRUE);
2544 xdnd_read_actions (new_context);
2546 event->dnd.type = GDK_DRAG_ENTER;
2547 event->dnd.context = new_context;
2548 gdk_drag_context_ref (new_context);
2550 current_dest_drag = new_context;
2551 ((GdkDragContextPrivate *)new_context)->xdnd_selection =
2552 gdk_atom_intern ("XdndSelection", FALSE);
2554 return GDK_FILTER_TRANSLATE;
2557 static GdkFilterReturn
2558 xdnd_leave_filter (GdkXEvent *xev,
2562 XEvent *xevent = (XEvent *)xev;
2563 guint32 source_window = xevent->xclient.data.l[0];
2566 g_message ("XdndLeave: source_window: %#x",
2569 if ((current_dest_drag != NULL) &&
2570 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2571 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2573 event->dnd.type = GDK_DRAG_LEAVE;
2574 /* Pass ownership of context to the event */
2575 event->dnd.context = current_dest_drag;
2577 current_dest_drag = NULL;
2579 return GDK_FILTER_TRANSLATE;
2582 return GDK_FILTER_REMOVE;
2585 static GdkFilterReturn
2586 xdnd_position_filter (GdkXEvent *xev,
2590 XEvent *xevent = (XEvent *)xev;
2591 guint32 source_window = xevent->xclient.data.l[0];
2592 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2593 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2594 guint32 time = xevent->xclient.data.l[3];
2595 GdkAtom action = xevent->xclient.data.l[4];
2598 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2599 source_window, x_root, y_root, time, action));
2602 if ((current_dest_drag != NULL) &&
2603 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2604 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2606 event->dnd.type = GDK_DRAG_MOTION;
2607 event->dnd.context = current_dest_drag;
2608 gdk_drag_context_ref (current_dest_drag);
2610 event->dnd.time = time;
2612 current_dest_drag->suggested_action = xdnd_action_from_atom (action);
2613 if (!((GdkDragContextPrivate *)current_dest_drag)->xdnd_have_actions)
2614 current_dest_drag->actions = current_dest_drag->suggested_action;
2616 event->dnd.x_root = x_root;
2617 event->dnd.y_root = y_root;
2619 ((GdkDragContextPrivate *)current_dest_drag)->last_x = x_root;
2620 ((GdkDragContextPrivate *)current_dest_drag)->last_y = y_root;
2622 return GDK_FILTER_TRANSLATE;
2625 return GDK_FILTER_REMOVE;
2628 static GdkFilterReturn
2629 xdnd_drop_filter (GdkXEvent *xev,
2633 XEvent *xevent = (XEvent *)xev;
2634 guint32 source_window = xevent->xclient.data.l[0];
2635 guint32 time = xevent->xclient.data.l[2];
2638 g_message ("XdndDrop: source_window: %#x time: %d",
2639 source_window, time));
2641 if ((current_dest_drag != NULL) &&
2642 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2643 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2645 GdkDragContextPrivate *private;
2646 private = (GdkDragContextPrivate *)current_dest_drag;
2648 event->dnd.type = GDK_DROP_START;
2650 event->dnd.context = current_dest_drag;
2651 gdk_drag_context_ref (current_dest_drag);
2653 event->dnd.time = time;
2654 event->dnd.x_root = private->last_x;
2655 event->dnd.y_root = private->last_y;
2657 return GDK_FILTER_TRANSLATE;
2660 return GDK_FILTER_REMOVE;
2663 /*************************************************************
2664 ************************** Public API ***********************
2665 *************************************************************/
2672 gdk_add_client_message_filter (
2673 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2674 motif_dnd_filter, NULL);
2675 gdk_add_client_message_filter (
2676 gdk_atom_intern ("XdndEnter", FALSE),
2677 xdnd_enter_filter, NULL);
2678 gdk_add_client_message_filter (
2679 gdk_atom_intern ("XdndLeave", FALSE),
2680 xdnd_leave_filter, NULL);
2681 gdk_add_client_message_filter (
2682 gdk_atom_intern ("XdndPosition", FALSE),
2683 xdnd_position_filter, NULL);
2684 gdk_add_client_message_filter (
2685 gdk_atom_intern ("XdndStatus", FALSE),
2686 xdnd_status_filter, NULL);
2687 gdk_add_client_message_filter (
2688 gdk_atom_intern ("XdndFinished", FALSE),
2689 xdnd_finished_filter, NULL);
2690 gdk_add_client_message_filter (
2691 gdk_atom_intern ("XdndDrop", FALSE),
2692 xdnd_drop_filter, NULL);
2698 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2700 if (context->dest_window)
2702 switch (context->protocol)
2704 case GDK_DRAG_PROTO_MOTIF:
2705 motif_send_leave (context, time);
2707 case GDK_DRAG_PROTO_XDND:
2708 xdnd_send_leave (context);
2710 case GDK_DRAG_PROTO_ROOTWIN:
2711 case GDK_DRAG_PROTO_NONE:
2715 gdk_window_unref (context->dest_window);
2716 context->dest_window = NULL;
2721 gdk_drag_begin (GdkWindow *window,
2725 GdkDragContext *new_context;
2727 g_return_val_if_fail (window != NULL, NULL);
2729 new_context = gdk_drag_context_new ();
2730 new_context->is_source = TRUE;
2731 new_context->source_window = window;
2732 gdk_window_ref (window);
2734 tmp_list = g_list_last (targets);
2735 new_context->targets = NULL;
2738 new_context->targets = g_list_prepend (new_context->targets,
2740 tmp_list = tmp_list->prev;
2743 new_context->actions = 0;
2749 gdk_drag_get_protocol (guint32 xid,
2750 GdkDragProtocol *protocol)
2754 if ((retval = xdnd_check_dest (xid)))
2756 *protocol = GDK_DRAG_PROTO_XDND;
2757 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2760 else if ((retval = motif_check_dest (xid)))
2762 *protocol = GDK_DRAG_PROTO_MOTIF;
2763 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2768 /* Check if this is a root window */
2770 gboolean rootwin = FALSE;
2771 gint old_warnings = gdk_error_warnings;
2774 unsigned long nitems, after;
2775 unsigned char *data;
2777 if (xid == gdk_root_window)
2784 XGetWindowProperty (gdk_display, xid,
2785 gdk_atom_intern ("ENLIGHTENMENT_DESKTOP", FALSE),
2786 0, 0, False, AnyPropertyType,
2787 &type, &format, &nitems, &after, &data);
2788 if ((gdk_error_code == 0) && type != None)
2795 /* Until I find out what window manager the next one is for,
2796 * I'm leaving it commented out. It's supported in the
2797 * xscreensaver sources, though.
2804 XGetWindowProperty (gdk_display, win,
2805 gdk_atom_intern ("__SWM_VROOT", FALSE),
2806 0, 0, False, AnyPropertyType,
2807 &type, &format, &nitems, &data);
2808 if ((gdk_error_code == 0) && type != None)
2813 gdk_error_warnings = old_warnings;
2817 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2822 *protocol = GDK_DRAG_PROTO_NONE;
2827 gdk_drag_find_window (GdkDragContext *context,
2828 GdkWindow *drag_window,
2831 GdkWindow **dest_window,
2832 GdkDragProtocol *protocol)
2834 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2837 g_return_if_fail (context != NULL);
2839 if (!private->window_cache)
2840 private->window_cache = gdk_window_cache_new();
2842 dest = get_client_window_at_coords (private->window_cache,
2844 GDK_WINDOW_XWINDOW (drag_window) : GDK_NONE,
2847 if (private->dest_xid != dest)
2850 private->dest_xid = dest;
2852 /* Check if new destination accepts drags, and which protocol */
2854 if ((recipient = gdk_drag_get_protocol (dest, protocol)))
2856 *dest_window = gdk_window_lookup (recipient);
2858 gdk_window_ref (*dest_window);
2860 *dest_window = gdk_window_foreign_new (recipient);
2863 *dest_window = NULL;
2867 *dest_window = context->dest_window;
2869 gdk_window_ref (*dest_window);
2870 *protocol = context->protocol;
2875 gdk_drag_motion (GdkDragContext *context,
2876 GdkWindow *dest_window,
2877 GdkDragProtocol protocol,
2880 GdkDragAction suggested_action,
2881 GdkDragAction possible_actions,
2884 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2886 g_return_val_if_fail (context != NULL, FALSE);
2888 /* When we have a Xdnd target, make sure our XdndActionList
2889 * matches the current actions;
2891 private->old_actions = context->actions;
2892 context->actions = possible_actions;
2894 if ((protocol == GDK_DRAG_PROTO_XDND) &&
2895 (!private->xdnd_actions_set ||
2896 private->xdnd_actions != possible_actions))
2897 xdnd_set_actions (context);
2899 if (context->dest_window != dest_window)
2901 GdkEvent temp_event;
2903 /* Send a leave to the last destination */
2904 gdk_drag_do_leave (context, time);
2905 private->drag_status = GDK_DRAG_STATUS_DRAG;
2907 /* Check if new destination accepts drags, and which protocol */
2911 context->dest_window = dest_window;
2912 gdk_window_ref (context->dest_window);
2913 context->protocol = protocol;
2917 case GDK_DRAG_PROTO_MOTIF:
2918 motif_send_enter (context, time);
2921 case GDK_DRAG_PROTO_XDND:
2922 xdnd_send_enter (context);
2925 case GDK_DRAG_PROTO_ROOTWIN:
2926 case GDK_DRAG_PROTO_NONE:
2929 private->old_action = suggested_action;
2930 context->suggested_action = suggested_action;
2931 private->old_actions = possible_actions;
2935 context->dest_window = NULL;
2936 context->action = 0;
2939 /* Push a status event, to let the client know that
2943 temp_event.dnd.type = GDK_DRAG_STATUS;
2944 temp_event.dnd.window = context->source_window;
2945 /* We use this to signal a synthetic status. Perhaps
2946 * we should use an extra field...
2948 temp_event.dnd.send_event = TRUE;
2950 temp_event.dnd.context = context;
2951 temp_event.dnd.time = time;
2953 gdk_event_put (&temp_event);
2957 private->old_action = context->suggested_action;
2958 context->suggested_action = suggested_action;
2961 /* Send a drag-motion event */
2963 private->last_x = x_root;
2964 private->last_y = y_root;
2966 if (context->dest_window)
2968 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
2970 switch (context->protocol)
2972 case GDK_DRAG_PROTO_MOTIF:
2973 motif_send_motion (context, x_root, y_root, suggested_action, time);
2976 case GDK_DRAG_PROTO_XDND:
2977 xdnd_send_motion (context, x_root, y_root, suggested_action, time);
2980 case GDK_DRAG_PROTO_ROOTWIN:
2982 GdkEvent temp_event;
2984 if (g_list_find (context->targets,
2985 GUINT_TO_POINTER (gdk_atom_intern ("application/x-rootwin-drop", FALSE))))
2986 context->action = context->suggested_action;
2988 context->action = 0;
2990 temp_event.dnd.type = GDK_DRAG_STATUS;
2991 temp_event.dnd.window = context->source_window;
2992 temp_event.dnd.send_event = FALSE;
2993 temp_event.dnd.context = context;
2994 temp_event.dnd.time = time;
2996 gdk_event_put (&temp_event);
2999 case GDK_DRAG_PROTO_NONE:
3000 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
3012 gdk_drag_drop (GdkDragContext *context,
3015 g_return_if_fail (context != NULL);
3017 if (context->dest_window)
3019 switch (context->protocol)
3021 case GDK_DRAG_PROTO_MOTIF:
3022 motif_send_leave (context, time);
3023 motif_send_drop (context, time);
3026 case GDK_DRAG_PROTO_XDND:
3027 xdnd_send_drop (context, time);
3030 case GDK_DRAG_PROTO_ROOTWIN:
3031 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
3033 case GDK_DRAG_PROTO_NONE:
3034 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
3041 gdk_drag_abort (GdkDragContext *context,
3044 g_return_if_fail (context != NULL);
3046 gdk_drag_do_leave (context, time);
3049 /* Destination side */
3052 gdk_drag_status (GdkDragContext *context,
3053 GdkDragAction action,
3056 GdkDragContextPrivate *private;
3059 g_return_if_fail (context != NULL);
3061 private = (GdkDragContextPrivate *)context;
3063 context->action = action;
3065 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3067 xev.xclient.type = ClientMessage;
3068 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
3069 xev.xclient.format = 8;
3070 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
3072 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
3074 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
3078 if ((action != 0) != (private->old_action != 0))
3081 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
3083 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
3086 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
3089 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3093 case GDK_ACTION_MOVE:
3094 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
3096 case GDK_ACTION_COPY:
3097 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
3099 case GDK_ACTION_LINK:
3100 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
3103 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
3108 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
3110 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
3112 MOTIF_XCLIENT_LONG (&xev, 1) = time;
3113 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
3114 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
3116 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3119 g_message ("Send event to %lx failed",
3120 GDK_WINDOW_XWINDOW (context->source_window)));
3122 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3124 xev.xclient.type = ClientMessage;
3125 xev.xclient.message_type = gdk_atom_intern ("XdndStatus", FALSE);
3126 xev.xclient.format = 32;
3127 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
3129 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->dest_window);
3130 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
3131 xev.xclient.data.l[2] = 0;
3132 xev.xclient.data.l[3] = 0;
3133 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
3135 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3138 g_message ("Send event to %lx failed",
3139 GDK_WINDOW_XWINDOW (context->source_window)));
3142 private->old_action = action;
3146 gdk_drop_reply (GdkDragContext *context,
3150 GdkDragContextPrivate *private;
3152 g_return_if_fail (context != NULL);
3154 private = (GdkDragContextPrivate *)context;
3156 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3160 xev.xclient.type = ClientMessage;
3161 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
3162 xev.xclient.format = 8;
3164 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
3165 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
3167 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
3168 (XmDROP_SITE_VALID << 4) |
3169 (XmDROP_NOOP << 8) |
3172 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3173 (XmNO_DROP_SITE << 4) |
3174 (XmDROP_NOOP << 8) |
3175 (XmDROP_CANCEL << 12);
3176 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3177 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3179 gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3185 gdk_drop_finish (GdkDragContext *context,
3189 g_return_if_fail (context != NULL);
3191 if (context->protocol == GDK_DRAG_PROTO_XDND)
3195 xev.xclient.type = ClientMessage;
3196 xev.xclient.message_type = gdk_atom_intern ("XdndFinished", FALSE);
3197 xev.xclient.format = 32;
3198 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
3200 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->dest_window);
3201 xev.xclient.data.l[1] = 0;
3202 xev.xclient.data.l[2] = 0;
3203 xev.xclient.data.l[3] = 0;
3204 xev.xclient.data.l[4] = 0;
3206 if (!xdnd_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3209 g_message ("Send event to %lx failed",
3210 GDK_WINDOW_XWINDOW (context->source_window)));
3216 gdk_window_register_dnd (GdkWindow *window)
3218 static gulong xdnd_version = 3;
3219 MotifDragReceiverInfo info;
3221 g_return_if_fail (window != NULL);
3223 /* Set Motif drag receiver information property */
3225 if (!motif_drag_receiver_info_atom)
3226 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
3228 info.byte_order = local_byte_order;
3229 info.protocol_version = 0;
3230 info.protocol_style = XmDRAG_DYNAMIC;
3231 info.proxy_window = GDK_NONE;
3232 info.num_drop_sites = 0;
3233 info.total_size = sizeof(info);
3235 XChangeProperty (gdk_display, GDK_WINDOW_XWINDOW (window),
3236 motif_drag_receiver_info_atom,
3237 motif_drag_receiver_info_atom,
3244 if (!xdnd_aware_atom)
3245 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
3247 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3248 XChangeProperty (GDK_WINDOW_XDISPLAY (window),
3249 GDK_WINDOW_XWINDOW (window),
3250 xdnd_aware_atom, XA_ATOM,
3251 32, PropModeReplace,
3252 (guchar *)&xdnd_version, 1);
3255 /*************************************************************
3256 * gdk_drag_get_selection:
3257 * Returns the selection atom for the current source window
3261 *************************************************************/
3264 gdk_drag_get_selection (GdkDragContext *context)
3266 g_return_val_if_fail (context != NULL, GDK_NONE);
3268 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3269 return ((GdkDragContextPrivate *)context)->motif_selection;
3270 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3271 return ((GdkDragContextPrivate *)context)->xdnd_selection;