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"
26 typedef struct _GdkDragContextPrivate GdkDragContextPrivate;
30 GDK_DRAG_STATUS_MOTION_WAIT,
31 GDK_DRAG_STATUS_ACTION_WAIT,
37 gint x, y, width, height;
43 GHashTable *child_hash;
47 /* Structure that holds information about a drag in progress.
48 * this is used on both source and destination sides.
50 struct _GdkDragContextPrivate {
51 GdkDragContext context;
53 GdkAtom motif_selection;
54 GdkAtom xdnd_selection;
57 guint16 last_x; /* Coordinates from last event */
59 GdkDragAction old_action; /* The last action we sent to the source */
62 guint xdnd_targets_set : 1; /* Whether we've already set XdndTypeList */
63 guint xdnd_actions_set : 1; /* Whether we've already set XdndActionsList */
64 guint xdnd_have_actions : 1; /* Whether an XdndActionList was provided */
65 guint motif_targets_set : 1; /* Whether we've already set motif initiator info */
66 guint drag_status : 4; /* current status of drag */
68 GdkWindowCache *window_cache;
71 GdkDragContext *current_dest_drag = NULL;
73 /* Forward declarations */
75 static void gdk_window_cache_destroy (GdkWindowCache *cache);
77 static void motif_read_target_table (void);
78 static GdkFilterReturn motif_dnd_filter (GdkXEvent *xev,
82 static GdkFilterReturn xdnd_enter_filter (GdkXEvent *xev,
86 static GdkFilterReturn xdnd_leave_filter (GdkXEvent *xev,
90 static GdkFilterReturn xdnd_position_filter (GdkXEvent *xev,
94 static GdkFilterReturn xdnd_status_filter (GdkXEvent *xev,
98 static GdkFilterReturn xdnd_finished_filter (GdkXEvent *xev,
102 static GdkFilterReturn xdnd_drop_filter (GdkXEvent *xev,
108 static GList *contexts;
111 gdk_drag_context_new (void)
113 GdkDragContextPrivate *result;
115 result = g_new0 (GdkDragContextPrivate, 1);
117 result->ref_count = 1;
119 contexts = g_list_prepend (contexts, result);
121 return (GdkDragContext *)result;
125 gdk_drag_context_ref (GdkDragContext *context)
127 g_return_if_fail (context != NULL);
129 ((GdkDragContextPrivate *)context)->ref_count++;
133 gdk_drag_context_unref (GdkDragContext *context)
135 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
137 g_return_if_fail (context != NULL);
139 private->ref_count--;
141 if (private->ref_count == 0)
143 g_dataset_destroy (private);
145 g_list_free (context->targets);
147 if (context->source_window)
148 gdk_window_unref (context->source_window);
150 if (context->dest_window)
151 gdk_window_unref (context->dest_window);
153 if (private->window_cache)
154 gdk_window_cache_destroy (private->window_cache);
156 contexts = g_list_remove (contexts, private);
161 static GdkDragContext *
162 gdk_drag_context_find (gboolean is_source,
166 GList *tmp_list = contexts;
167 GdkDragContext *context;
171 context = (GdkDragContext *)tmp_list->data;
173 if ((!context->is_source == !is_source) &&
174 ((source_xid == None) || (context->source_window &&
175 (GDK_WINDOW_XWINDOW (context->source_window) == source_xid))) &&
176 ((dest_xid == None) || (context->dest_window &&
177 (GDK_WINDOW_XWINDOW (context->dest_window) == dest_xid))))
180 tmp_list = tmp_list->next;
186 /* Utility functions */
189 gdk_window_cache_add (GdkWindowCache *cache,
191 gint x, gint y, gint width, gint height,
194 GdkCacheChild *child = g_new (GdkCacheChild, 1);
199 child->width = width;
200 child->height = height;
201 child->mapped = mapped;
203 cache->children = g_list_prepend (cache->children, child);
204 g_hash_table_insert (cache->child_hash, GUINT_TO_POINTER (xid),
208 static GdkFilterReturn
209 gdk_window_cache_filter (GdkXEvent *xev,
213 XEvent *xevent = (XEvent *)xev;
214 GdkWindowCache *cache = data;
216 switch (xevent->type)
218 case CirculateNotify:
220 case ConfigureNotify:
222 XConfigureEvent *xce = &xevent->xconfigure;
225 node = g_hash_table_lookup (cache->child_hash,
226 GUINT_TO_POINTER (xce->window));
229 GdkCacheChild *child = node->data;
232 child->width = xce->width;
233 child->height = xce->height;
234 if (xce->above == None && (node->next))
236 GList *last = g_list_last (cache->children);
237 cache->children = g_list_remove_link (cache->children, node);
244 GList *above_node = g_hash_table_lookup (cache->child_hash,
245 GUINT_TO_POINTER (xce->above));
246 if (above_node && node->prev != above_node)
248 cache->children = g_list_remove_link (cache->children, node);
249 node->next = above_node->next;
251 node->next->prev = node;
252 node->prev = above_node;
253 above_node->next = node;
261 XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
263 if (!g_hash_table_lookup (cache->child_hash,
264 GUINT_TO_POINTER (xcwe->window)))
265 gdk_window_cache_add (cache, xcwe->window,
266 xcwe->x, xcwe->y, xcwe->width, xcwe->height,
272 XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
275 node = g_hash_table_lookup (cache->child_hash,
276 GUINT_TO_POINTER (xdwe->window));
279 g_hash_table_remove (cache->child_hash,
280 GUINT_TO_POINTER (xdwe->window));
281 cache->children = g_list_remove_link (cache->children, node);
283 g_list_free_1 (node);
289 XMapEvent *xme = &xevent->xmap;
292 node = g_hash_table_lookup (cache->child_hash,
293 GUINT_TO_POINTER (xme->window));
296 GdkCacheChild *child = node->data;
297 child->mapped = TRUE;
305 XMapEvent *xume = &xevent->xmap;
308 node = g_hash_table_lookup (cache->child_hash,
309 GUINT_TO_POINTER (xume->window));
312 GdkCacheChild *child = node->data;
313 child->mapped = FALSE;
318 return GDK_FILTER_CONTINUE;
320 return GDK_FILTER_REMOVE;
323 static GdkWindowCache *
324 gdk_window_cache_new (void)
326 XWindowAttributes xwa;
327 Window root, parent, *children;
328 unsigned int nchildren;
331 gint old_warnings = gdk_error_warnings;
333 GdkWindowCache *result = g_new (GdkWindowCache, 1);
335 result->children = NULL;
336 result->child_hash = g_hash_table_new (g_direct_hash, NULL);
338 XGetWindowAttributes (gdk_display, gdk_root_window, &xwa);
339 result->old_event_mask = xwa.your_event_mask;
340 XSelectInput (gdk_display, gdk_root_window,
341 result->old_event_mask | SubstructureNotifyMask);
342 gdk_window_add_filter ((GdkWindow *)&gdk_root_parent,
343 gdk_window_cache_filter, result);
346 gdk_error_warnings = 0;
348 if (XQueryTree(gdk_display, gdk_root_window,
349 &root, &parent, &children, &nchildren) == 0)
352 for (i = 0; i < nchildren ; i++)
354 XGetWindowAttributes (gdk_display, children[i], &xwa);
356 gdk_window_cache_add (result, children[i],
357 xwa.x, xwa.y, xwa.width, xwa.height,
358 xwa.map_state != IsUnmapped);
360 if (gdk_error_code != 0)
364 gdk_window_cache_add (result, children[i],
365 xwa.x, xwa.y, xwa.width, xwa.height,
366 (xwa.map_state != IsUnmapped));
372 gdk_error_warnings = old_warnings;
378 gdk_window_cache_destroy (GdkWindowCache *cache)
380 XSelectInput (gdk_display, gdk_root_window, cache->old_event_mask);
381 gdk_window_remove_filter ((GdkWindow *)&gdk_root_parent,
382 gdk_window_cache_filter, cache);
384 g_list_foreach (cache->children, (GFunc)g_free, NULL);
385 g_list_free (cache->children);
386 g_hash_table_destroy (cache->child_hash);
392 get_client_window_at_coords_recurse (Window win,
396 Window root, tmp_parent, *children;
397 unsigned int nchildren;
402 unsigned long nitems, after;
405 static Atom wm_state_atom = None;
408 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
410 XGetWindowProperty (gdk_display, win,
411 wm_state_atom, 0, 0, False, AnyPropertyType,
412 &type, &format, &nitems, &after, &data);
414 if (gdk_error_code != 0)
427 /* This is beautiful! Damn Enlightenment and click-to-focus */
428 XTranslateCoordinates (gdk_display, gdk_root_window, win,
429 x_root, y_root, &dest_x, &dest_y, &child);
431 if (gdk_error_code != 0)
438 if (XQueryTree(gdk_display, win,
439 &root, &tmp_parent, &children, &nchildren) == 0)
442 if (gdk_error_code == 0)
444 for (i = nchildren - 1; (i >= 0) && (child == None); i--)
446 XWindowAttributes xwa;
448 XGetWindowAttributes (gdk_display, children[i], &xwa);
450 if (gdk_error_code != 0)
452 else if ((xwa.map_state == IsViewable) && (xwa.class == InputOutput) &&
453 (x >= xwa.x) && (x < xwa.x + (gint)xwa.width) &&
454 (y >= xwa.y) && (y < xwa.y + (gint)xwa.height))
469 return get_client_window_at_coords_recurse (child, x, y);
475 get_client_window_at_coords (GdkWindowCache *cache,
481 Window retval = None;
483 gint old_warnings = gdk_error_warnings;
486 gdk_error_warnings = 0;
488 tmp_list = cache->children;
490 while (tmp_list && !retval)
492 GdkCacheChild *child = tmp_list->data;
494 if ((child->xid != ignore) && (child->mapped))
496 if ((x_root >= child->x) && (x_root < child->x + child->width) &&
497 (y_root >= child->y) && (y_root < child->y + child->height))
499 retval = get_client_window_at_coords_recurse (child->xid,
507 tmp_list = tmp_list->next;
510 gdk_error_warnings = old_warnings;
514 return gdk_root_window;
519 get_client_window_at_coords_recurse (Window win,
526 unsigned long nitems, after;
530 static Atom wm_state_atom = None;
533 wm_state_atom = gdk_atom_intern ("WM_STATE", FALSE);
535 XGetWindowProperty (gdk_display, win,
536 wm_state_atom, 0, 0, False, AnyPropertyType,
537 &type, &format, &nitems, &after, &data);
539 if (gdk_error_code != 0)
551 XTranslateCoordinates (gdk_display, gdk_root_window, win,
552 x_root, y_root, &dest_x, &dest_y, &child);
554 if (gdk_error_code != 0)
561 return get_client_window_at_coords_recurse (child, x_root, y_root);
567 get_client_window_at_coords (Window ignore,
571 Window root, parent, *children;
572 unsigned int nchildren;
574 Window retval = None;
576 gint old_warnings = gdk_error_warnings;
579 gdk_error_warnings = 0;
581 if (XQueryTree(gdk_display, gdk_root_window,
582 &root, &parent, &children, &nchildren) == 0)
585 for (i = nchildren - 1; (i >= 0) && (retval == None); i--)
587 if (children[i] != ignore)
589 XWindowAttributes xwa;
591 XGetWindowAttributes (gdk_display, children[i], &xwa);
593 if (gdk_error_code != 0)
595 else if ((xwa.map_state == IsViewable) &&
596 (x_root >= xwa.x) && (x_root < xwa.x + (gint)xwa.width) &&
597 (y_root >= xwa.y) && (y_root < xwa.y + (gint)xwa.height))
599 retval = get_client_window_at_coords_recurse (children[i],
602 retval = children[i];
609 gdk_error_warnings = old_warnings;
613 return gdk_root_window;
617 /*************************************************************
618 ***************************** MOTIF *************************
619 *************************************************************/
621 /* values used in the message type for Motif DND */
634 /* Values used to specify type of protocol to use */
638 XmDRAG_PREFER_PREREGISTER,
640 XmDRAG_PREFER_DYNAMIC,
642 XmDRAG_PREFER_RECEIVER
645 /* Operation codes */
653 /* Drop site status */
655 XmNO_DROP_SITE = 0x01,
656 XmDROP_SITE_INVALID = 0x02,
657 XmDROP_SITE_VALID = 0x03
660 /* completion status */
668 /* Static data for MOTIF DND */
669 static GList **motif_target_lists = NULL;
670 static gint motif_n_target_lists = -1;
672 /* Byte swapping routines. The motif specification leaves it
673 * up to us to save a few bytes in the client messages
675 static gchar local_byte_order = '\0';
677 #ifdef G_ENABLE_DEBUG
679 print_target_list (GList *targets)
683 gchar *name = gdk_atom_name (GPOINTER_TO_INT (targets->data));
684 g_message ("\t%s", name);
686 targets = targets->next;
689 #endif /* G_ENABLE_DEBUG */
692 init_byte_order (void)
694 guint32 myint = 0x01020304;
695 local_byte_order = (*(gchar *)&myint == 1) ? 'B' : 'l';
699 card16_to_host (guint16 x, gchar byte_order) {
700 if (byte_order == local_byte_order)
703 return (x << 8) | (x >> 8);
707 card32_to_host (guint32 x, gchar byte_order) {
708 if (byte_order == local_byte_order)
711 return (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24);
714 /* Motif packs together fields of varying length into the
715 * client message. We can't rely on accessing these
716 * through data.s[], data.l[], etc, because on some architectures
717 * (i.e., Alpha) these won't be valid for format == 8.
720 #define MOTIF_XCLIENT_BYTE(xevent,i) \
721 (xevent)->xclient.data.b[i]
722 #define MOTIF_XCLIENT_SHORT(xevent,i) \
723 ((gint16 *)&((xevent)->xclient.data.b[0]))[i]
724 #define MOTIF_XCLIENT_LONG(xevent,i) \
725 ((gint32 *)&((xevent)->xclient.data.b[0]))[i]
727 #define MOTIF_UNPACK_BYTE(xevent,i) MOTIF_XCLIENT_BYTE(xevent,i)
728 #define MOTIF_UNPACK_SHORT(xevent,i) \
729 card16_to_host (MOTIF_XCLIENT_SHORT(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
730 #define MOTIF_UNPACK_LONG(xevent,i) \
731 card32_to_host (MOTIF_XCLIENT_LONG(xevent,i), MOTIF_XCLIENT_BYTE(xevent, 1))
733 /***** Dest side ***********/
735 /* Property placed on source windows */
736 typedef struct _MotifDragInitiatorInfo {
738 guint8 protocol_version;
739 guint16 targets_index;
740 guint32 selection_atom;
741 } MotifDragInitiatorInfo;
743 /* Header for target table on the drag window */
744 typedef struct _MotifTargetTableHeader {
746 guchar protocol_version;
749 } MotifTargetTableHeader;
751 /* Property placed on target windows */
752 typedef struct _MotifDragReceiverInfo {
754 guint8 protocol_version;
755 guint8 protocol_style;
757 guint32 proxy_window;
758 guint16 num_drop_sites;
761 } MotifDragReceiverInfo;
763 static Window motif_drag_window = None;
764 static GdkWindow *motif_drag_gdk_window = NULL;
766 static GdkAtom motif_drag_targets_atom = GDK_NONE;
767 static GdkAtom motif_drag_receiver_info_atom = GDK_NONE;
769 /* Target table handling */
772 motif_drag_window_filter (GdkXEvent *xevent,
776 XEvent *xev = (XEvent *)xevent;
778 switch (xev->xany.type)
781 motif_drag_window = None;
782 motif_drag_gdk_window = NULL;
785 if (motif_target_lists &&
786 motif_drag_targets_atom &&
787 (xev->xproperty.atom == motif_drag_targets_atom))
788 motif_read_target_table();
791 return GDK_FILTER_REMOVE;
794 static Atom motif_drag_window_atom = GDK_NONE;
797 motif_lookup_drag_window (Display *display)
799 Window retval = None;
800 gulong bytes_after, nitems;
805 XGetWindowProperty (gdk_display, gdk_root_window, motif_drag_window_atom,
807 XA_WINDOW, &type, &format, &nitems, &bytes_after,
810 if ((format == 32) && (nitems == 1) && (bytes_after == 0))
812 retval = *(Window *)data;
814 g_message ("Found drag window %#lx\n", motif_drag_window));
823 /* Finds the window where global Motif drag information is stored.
824 * If it doesn't exist and 'create' is TRUE, create one.
827 motif_find_drag_window (gboolean create)
829 if (!motif_drag_window)
831 if (!motif_drag_window_atom)
832 motif_drag_window_atom = gdk_atom_intern ("_MOTIF_DRAG_WINDOW", TRUE);
834 motif_drag_window = motif_lookup_drag_window (gdk_display);
836 if (!motif_drag_window && create)
838 /* Create a persistant window. (Copied from LessTif) */
841 XSetWindowAttributes attr;
842 display = XOpenDisplay (NULL);
843 XSetCloseDownMode (display, RetainPermanent);
845 XGrabServer (display);
847 motif_drag_window = motif_lookup_drag_window (display);
849 if (!motif_drag_window)
851 attr.override_redirect = True;
852 attr.event_mask = PropertyChangeMask;
855 XCreateWindow(display, DefaultRootWindow(display),
856 -100, -100, 10, 10, 0, 0,
857 InputOnly, CopyFromParent,
858 (CWOverrideRedirect | CWEventMask), &attr);
861 g_message ("Created drag window %#lx\n", motif_drag_window));
863 XChangeProperty (display, gdk_root_window,
864 motif_drag_window_atom, XA_WINDOW,
866 (guchar *)&motif_drag_window_atom, 1);
869 XUngrabServer (display);
870 XCloseDisplay (display);
873 /* There is a miniscule race condition here if the drag window
874 * gets destroyed exactly now.
876 if (motif_drag_window)
878 motif_drag_gdk_window = gdk_window_foreign_new (motif_drag_window);
879 gdk_window_add_filter (motif_drag_gdk_window,
880 motif_drag_window_filter,
885 return motif_drag_window;
889 motif_read_target_table (void)
891 gulong bytes_after, nitems;
896 if (!motif_drag_targets_atom)
897 motif_drag_targets_atom = gdk_atom_intern ("_MOTIF_DRAG_TARGETS", FALSE);
899 if (motif_target_lists)
901 for (i=0; i<motif_n_target_lists; i++)
902 g_list_free (motif_target_lists[i]);
904 g_free (motif_target_lists);
905 motif_target_lists = NULL;
906 motif_n_target_lists = 0;
909 if (motif_find_drag_window (FALSE))
911 MotifTargetTableHeader *header = NULL;
912 guchar *target_bytes = NULL;
914 gboolean success = FALSE;
916 XGetWindowProperty (gdk_display, motif_drag_window,
917 motif_drag_targets_atom,
918 0, (sizeof(MotifTargetTableHeader)+3)/4, FALSE,
919 motif_drag_targets_atom,
920 &type, &format, &nitems, &bytes_after,
923 if ((format != 8) || (nitems < sizeof (MotifTargetTableHeader)))
926 header->n_lists = card16_to_host (header->n_lists, header->byte_order);
927 header->total_size = card32_to_host (header->total_size, header->byte_order);
929 XGetWindowProperty (gdk_display, motif_drag_window, motif_drag_targets_atom,
930 (sizeof(MotifTargetTableHeader)+3)/4,
931 (header->total_size + 3)/4 - (sizeof(MotifTargetTableHeader) + 3)/4,
933 motif_drag_targets_atom, &type, &format, &nitems,
934 &bytes_after, &target_bytes);
936 if ((format != 8) || (bytes_after != 0) ||
937 (nitems != header->total_size - sizeof(MotifTargetTableHeader)))
940 motif_n_target_lists = header->n_lists;
941 motif_target_lists = g_new0 (GList *, motif_n_target_lists);
944 for (i=0; i<header->n_lists; i++)
949 if (p + sizeof(guint16) - target_bytes > nitems)
952 n_targets = card16_to_host (*(gushort *)p, header->byte_order);
954 /* We need to make a copy of the targets, since it may
957 targets = g_new (guint32, n_targets);
958 memcpy (targets, p + sizeof(guint16), sizeof(guint32) * n_targets);
960 p += sizeof(guint16) + n_targets * sizeof(guint32);
961 if (p - target_bytes > nitems)
964 for (j=0; j<n_targets; j++)
965 motif_target_lists[i] =
966 g_list_prepend (motif_target_lists[i],
967 GUINT_TO_POINTER (card32_to_host (targets[j],
968 header->byte_order)));
970 motif_target_lists[i] = g_list_reverse (motif_target_lists[i]);
980 XFree (target_bytes);
984 if (motif_target_lists)
986 g_free (motif_target_lists);
987 motif_target_lists = NULL;
988 motif_n_target_lists = 0;
990 g_warning ("Error reading Motif target table\n");
996 targets_sort_func (gconstpointer a, gconstpointer b)
998 return (GPOINTER_TO_UINT (a) < GPOINTER_TO_UINT (b)) ?
999 -1 : ((GPOINTER_TO_UINT (a) > GPOINTER_TO_UINT (b)) ? 1 : 0);
1002 /* Check if given (sorted) list is in the targets table */
1004 motif_target_table_check (GList *sorted)
1006 GList *tmp_list1, *tmp_list2;
1009 for (i=0; i<motif_n_target_lists; i++)
1011 tmp_list1 = motif_target_lists[i];
1014 while (tmp_list1 && tmp_list2)
1016 if (tmp_list1->data != tmp_list2->data)
1019 tmp_list1 = tmp_list1->next;
1020 tmp_list2 = tmp_list2->next;
1022 if (!tmp_list1 && !tmp_list2) /* Found it */
1030 motif_add_to_target_table (GList *targets)
1032 GList *sorted = NULL;
1037 /* make a sorted copy of the list */
1041 sorted = g_list_insert_sorted (sorted, targets->data, targets_sort_func);
1042 targets = targets->next;
1045 /* First check if it is their already */
1047 if (motif_target_lists)
1048 index = motif_target_table_check (sorted);
1050 /* We need to grab the server while doing this, to ensure
1056 /* We need to make sure that it exists _before_ we grab the
1057 * server, since we can't open a new connection after we
1060 motif_find_drag_window (TRUE);
1062 XGrabServer(gdk_display);
1063 motif_read_target_table();
1065 /* Check again, in case it was added in the meantime */
1067 if (motif_target_lists)
1068 index = motif_target_table_check (sorted);
1072 guint32 total_size = 0;
1076 MotifTargetTableHeader *header;
1078 if (!motif_target_lists)
1080 motif_target_lists = g_new (GList *, 1);
1081 motif_n_target_lists = 1;
1085 motif_n_target_lists++;
1086 motif_target_lists = g_realloc (motif_target_lists,
1087 sizeof(GList *) * motif_n_target_lists);
1089 motif_target_lists[motif_n_target_lists - 1] = sorted;
1091 index = motif_n_target_lists - 1;
1093 total_size = sizeof (MotifTargetTableHeader);
1094 for (i = 0; i < motif_n_target_lists ; i++)
1095 total_size += sizeof(guint16) + sizeof(guint32) * g_list_length (motif_target_lists[i]);
1097 data = g_malloc (total_size);
1099 header = (MotifTargetTableHeader *)data;
1100 p = data + sizeof(MotifTargetTableHeader);
1102 header->byte_order = local_byte_order;
1103 header->protocol_version = 0;
1104 header->n_lists = motif_n_target_lists;
1105 header->total_size = total_size;
1107 for (i = 0; i < motif_n_target_lists ; i++)
1109 guint16 n_targets = g_list_length (motif_target_lists[i]);
1110 guint32 *targets = g_new (guint32, n_targets);
1111 guint32 *p32 = targets;
1113 tmp_list = motif_target_lists[i];
1116 *p32 = GPOINTER_TO_UINT (tmp_list->data);
1118 tmp_list = tmp_list->next;
1123 p += sizeof(guint16);
1125 memcpy (p, targets, n_targets * sizeof(guint32));
1128 p += sizeof(guint32) * n_targets;
1132 XChangeProperty (gdk_display, motif_drag_window,
1133 motif_drag_targets_atom,
1134 motif_drag_targets_atom,
1138 XUngrabServer(gdk_display);
1141 g_list_free (sorted);
1145 /* Translate flags */
1148 motif_dnd_translate_flags (GdkDragContext *context, guint16 flags)
1150 guint recommended_op = flags & 0x000f;
1151 guint possible_ops = (flags & 0x0f0) >> 4;
1153 switch (recommended_op)
1156 context->suggested_action = GDK_ACTION_MOVE;
1159 context->suggested_action = GDK_ACTION_COPY;
1162 context->suggested_action = GDK_ACTION_LINK;
1165 context->suggested_action = GDK_ACTION_COPY;
1169 context->actions = 0;
1170 if (possible_ops & XmDROP_MOVE)
1171 context->actions |= GDK_ACTION_MOVE;
1172 if (possible_ops & XmDROP_COPY)
1173 context->actions |= GDK_ACTION_COPY;
1174 if (possible_ops & XmDROP_LINK)
1175 context->actions |= GDK_ACTION_LINK;
1179 motif_dnd_get_flags (GdkDragContext *context)
1183 switch (context->suggested_action)
1185 case GDK_ACTION_MOVE:
1186 flags = XmDROP_MOVE;
1188 case GDK_ACTION_COPY:
1189 flags = XmDROP_COPY;
1191 case GDK_ACTION_LINK:
1192 flags = XmDROP_LINK;
1195 flags = XmDROP_NOOP;
1199 if (context->actions & GDK_ACTION_MOVE)
1200 flags |= XmDROP_MOVE << 8;
1201 if (context->actions & GDK_ACTION_COPY)
1202 flags |= XmDROP_COPY << 8;
1203 if (context->actions & GDK_ACTION_LINK)
1204 flags |= XmDROP_LINK << 8;
1212 motif_set_targets (GdkDragContext *context)
1214 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1215 MotifDragInitiatorInfo info;
1217 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1219 if (!motif_drag_initiator_info)
1220 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1222 info.byte_order = local_byte_order;
1223 info.protocol_version = 0;
1225 info.targets_index = motif_add_to_target_table (context->targets);
1230 g_snprintf(buf, 20, "_GDK_SELECTION_%d", i);
1232 private->motif_selection = gdk_atom_intern (buf, FALSE);
1233 if (!XGetSelectionOwner (gdk_display, private->motif_selection))
1237 info.selection_atom = private->motif_selection;
1239 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1240 GDK_WINDOW_XWINDOW (context->source_window),
1241 private->motif_selection,
1242 motif_drag_initiator_info, 8, PropModeReplace,
1243 (guchar *)&info, sizeof (info));
1245 private->motif_targets_set = 1;
1249 motif_check_dest (Window win)
1251 gboolean retval = FALSE;
1252 MotifDragReceiverInfo *info;
1255 unsigned long nitems, after;
1257 if (!motif_drag_receiver_info_atom)
1258 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
1260 XGetWindowProperty (gdk_display, win,
1261 motif_drag_receiver_info_atom,
1262 0, (sizeof(*info)+3)/4, False, AnyPropertyType,
1263 &type, &format, &nitems, &after,
1268 if ((format == 8) && (nitems == sizeof(*info)))
1270 if ((info->protocol_version == 0) &&
1271 ((info->protocol_style == XmDRAG_PREFER_PREREGISTER) ||
1272 (info->protocol_style == XmDRAG_PREFER_DYNAMIC) ||
1273 (info->protocol_style == XmDRAG_DYNAMIC)))
1279 g_warning ("Invalid Motif drag receiver property on window %ld\n", win));
1285 return retval ? win : GDK_NONE;
1290 motif_send_enter (GdkDragContext *context,
1294 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1296 xev.xclient.type = ClientMessage;
1297 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1298 xev.xclient.format = 8;
1299 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1301 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_ENTER;
1302 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1303 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1304 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1305 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XWINDOW (context->source_window);
1307 if (!private->motif_targets_set)
1308 motif_set_targets (context);
1310 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1312 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1315 g_message ("Send event to %lx failed",
1316 GDK_WINDOW_XWINDOW (context->dest_window)));
1320 motif_send_leave (GdkDragContext *context,
1325 xev.xclient.type = ClientMessage;
1326 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1327 xev.xclient.format = 8;
1328 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1330 MOTIF_XCLIENT_BYTE (&xev, 0) = XmTOP_LEVEL_LEAVE;
1331 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1332 MOTIF_XCLIENT_SHORT (&xev, 1) = 0;
1333 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1334 MOTIF_XCLIENT_LONG (&xev, 2) = GDK_WINDOW_XWINDOW (context->source_window);
1335 MOTIF_XCLIENT_LONG (&xev, 3) = 0;
1337 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1340 g_message ("Send event to %lx failed",
1341 GDK_WINDOW_XWINDOW (context->dest_window)));
1345 motif_send_motion (GdkDragContext *context,
1348 GdkDragAction action,
1353 GdkDragContextPrivate *private = (GdkDragContextPrivate *)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, 1) = local_byte_order;
1361 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1362 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1364 if (context->suggested_action != private->old_action)
1366 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED;
1368 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1373 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION;
1375 MOTIF_XCLIENT_SHORT (&xev, 4) = x_root;
1376 MOTIF_XCLIENT_SHORT (&xev, 5) = y_root;
1378 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1382 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
1385 g_message ("Send event to %lx failed",
1386 GDK_WINDOW_XWINDOW (context->dest_window)));
1392 motif_send_drop (GdkDragContext *context, guint32 time)
1395 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1397 xev.xclient.type = ClientMessage;
1398 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
1399 xev.xclient.format = 8;
1400 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
1402 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START;
1403 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
1404 MOTIF_XCLIENT_SHORT (&xev, 1) = motif_dnd_get_flags (context);
1405 MOTIF_XCLIENT_LONG (&xev, 1) = time;
1407 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
1408 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
1410 MOTIF_XCLIENT_LONG (&xev, 3) = private->motif_selection;
1411 MOTIF_XCLIENT_LONG (&xev, 4) = GDK_WINDOW_XWINDOW (context->source_window);
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_read_initiator_info (Window source_window,
1429 static GdkAtom motif_drag_initiator_info = GDK_NONE;
1435 MotifDragInitiatorInfo *initiator_info;
1437 if (!motif_drag_initiator_info)
1438 motif_drag_initiator_info = gdk_atom_intern ("_MOTIF_DRAG_INITIATOR_INFO", FALSE);
1440 XGetWindowProperty (gdk_display, source_window, atom,
1441 0, sizeof(*initiator_info), FALSE,
1442 motif_drag_initiator_info,
1443 &type, &format, &nitems, &bytes_after,
1444 (guchar **)&initiator_info);
1446 if ((format != 8) || (nitems != sizeof (MotifDragInitiatorInfo)) || (bytes_after != 0))
1448 g_warning ("Error reading initiator info\n");
1452 motif_read_target_table ();
1454 if (initiator_info->targets_index >= motif_n_target_lists)
1456 g_warning ("Invalid target index in TOP_LEVEL_ENTER MESSAGE");
1457 XFree (initiator_info);
1458 return GDK_FILTER_REMOVE;
1461 initiator_info->targets_index =
1462 card16_to_host (initiator_info->targets_index, initiator_info->byte_order);
1463 initiator_info->selection_atom =
1464 card32_to_host (initiator_info->selection_atom, initiator_info->byte_order);
1466 tmp_list = g_list_last (motif_target_lists[initiator_info->targets_index]);
1471 *targets = g_list_prepend (*targets,
1473 tmp_list = tmp_list->prev;
1476 #ifdef G_ENABLE_DEBUG
1477 if (gdk_debug_flags & GDK_DEBUG_DND)
1478 print_target_list (*targets);
1479 #endif /* G_ENABLE_DEBUG */
1481 *selection = initiator_info->selection_atom;
1483 XFree (initiator_info);
1488 static GdkDragContext *
1489 motif_drag_context_new (GdkWindow *dest_window,
1491 guint32 source_window,
1494 GdkDragContext *new_context;
1495 GdkDragContextPrivate *private;
1497 /* FIXME, current_dest_drag really shouldn't be NULL'd
1498 * if we error below.
1500 if (current_dest_drag != NULL)
1502 if (timestamp >= current_dest_drag->start_time)
1504 gdk_drag_context_unref (current_dest_drag);
1505 current_dest_drag = NULL;
1511 new_context = gdk_drag_context_new ();
1512 private = (GdkDragContextPrivate *)new_context;
1514 new_context->protocol = GDK_DRAG_PROTO_MOTIF;
1515 new_context->is_source = FALSE;
1517 new_context->source_window = gdk_window_lookup (source_window);
1518 if (new_context->source_window)
1519 gdk_window_ref (new_context->source_window);
1522 new_context->source_window = gdk_window_foreign_new (source_window);
1523 if (!new_context->source_window)
1525 gdk_drag_context_unref (new_context);
1530 new_context->dest_window = dest_window;
1531 gdk_window_ref (dest_window);
1532 new_context->start_time = timestamp;
1534 if (!motif_read_initiator_info(source_window,
1536 &new_context->targets,
1537 &private->motif_selection))
1539 gdk_drag_context_unref (new_context);
1547 * The MOTIF drag protocol has no real provisions for distinguishing
1548 * multiple simultaneous drops. If the sources grab the pointer
1549 * when doing drags, that shouldn't happen, in any case. If it
1550 * does, we can't do much except hope for the best.
1553 static GdkFilterReturn
1554 motif_top_level_enter (GdkEvent *event,
1557 guint32 source_window,
1560 GdkDragContext *new_context;
1562 GDK_NOTE(DND, g_message ("Motif DND top level enter: flags: %#4x time: %d source_widow: %#4x atom: %d",
1563 flags, timestamp, source_window, atom));
1565 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1567 return GDK_FILTER_REMOVE;
1569 event->dnd.type = GDK_DRAG_ENTER;
1570 event->dnd.context = new_context;
1571 gdk_drag_context_ref (new_context);
1573 current_dest_drag = new_context;
1575 return GDK_FILTER_TRANSLATE;
1579 motif_top_level_leave (GdkEvent *event,
1583 GDK_NOTE(DND, g_message ("Motif DND top level leave: flags: %#4x time: %d",
1586 if ((current_dest_drag != NULL) &&
1587 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1588 (timestamp >= current_dest_drag->start_time))
1590 event->dnd.type = GDK_DRAG_LEAVE;
1591 /* Pass ownership of context to the event */
1592 event->dnd.context = current_dest_drag;
1594 current_dest_drag = NULL;
1596 return GDK_FILTER_TRANSLATE;
1599 return GDK_FILTER_REMOVE;
1603 motif_motion (GdkEvent *event,
1609 GdkDragContextPrivate *private;
1611 GDK_NOTE(DND, g_message ("Motif DND motion: flags: %#4x time: %d (%d, %d)",
1612 flags, timestamp, x_root, y_root));
1614 if ((current_dest_drag != NULL) &&
1615 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1616 (timestamp >= current_dest_drag->start_time))
1618 private = (GdkDragContextPrivate *)current_dest_drag;
1620 event->dnd.type = GDK_DRAG_MOTION;
1621 event->dnd.context = current_dest_drag;
1622 gdk_drag_context_ref (current_dest_drag);
1624 event->dnd.time = timestamp;
1626 motif_dnd_translate_flags (current_dest_drag, flags);
1628 event->dnd.x_root = x_root;
1629 event->dnd.y_root = y_root;
1631 private->last_x = x_root;
1632 private->last_y = y_root;
1634 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
1636 return GDK_FILTER_TRANSLATE;
1639 return GDK_FILTER_REMOVE;
1643 motif_operation_changed (GdkEvent *event,
1647 GdkDragContextPrivate *private;
1649 GDK_NOTE(DND, g_message ("Motif DND operation changed: flags: %#4x time: %d",
1652 if ((current_dest_drag != NULL) &&
1653 (current_dest_drag->protocol == GDK_DRAG_PROTO_MOTIF) &&
1654 (timestamp >= current_dest_drag->start_time))
1656 event->dnd.type = GDK_DRAG_MOTION;
1657 event->dnd.send_event = FALSE;
1658 event->dnd.context = current_dest_drag;
1659 gdk_drag_context_ref (current_dest_drag);
1661 event->dnd.time = timestamp;
1662 private = (GdkDragContextPrivate *)current_dest_drag;
1664 motif_dnd_translate_flags (current_dest_drag, flags);
1666 event->dnd.x_root = private->last_x;
1667 event->dnd.y_root = private->last_y;
1669 private->drag_status = GDK_DRAG_STATUS_ACTION_WAIT;
1671 return GDK_FILTER_TRANSLATE;
1674 return GDK_FILTER_REMOVE;
1678 motif_drop_start (GdkEvent *event,
1681 guint32 source_window,
1686 GdkDragContext *new_context;
1689 GDK_NOTE(DND, g_message ("Motif DND drop start: flags: %#4x time: %d (%d, %d) source_widow: %#4x atom: %d",
1690 flags, timestamp, x_root, y_root, source_window, atom));
1692 new_context = motif_drag_context_new (event->any.window, timestamp, source_window, atom);
1694 return GDK_FILTER_REMOVE;
1696 motif_dnd_translate_flags (new_context, flags);
1698 event->dnd.type = GDK_DROP_START;
1699 event->dnd.context = new_context;
1700 event->dnd.time = timestamp;
1701 event->dnd.x_root = x_root;
1702 event->dnd.y_root = y_root;
1704 gdk_drag_context_ref (new_context);
1705 current_dest_drag = new_context;
1707 return GDK_FILTER_TRANSLATE;
1711 motif_drag_status (GdkEvent *event,
1715 GdkDragContext *context;
1718 g_message ("Motif status message: flags %x", flags));
1720 context = gdk_drag_context_find (TRUE,
1721 GDK_WINDOW_XWINDOW (event->any.window),
1726 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1727 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1728 private->drag_status = GDK_DRAG_STATUS_DRAG;
1730 event->dnd.type = GDK_DRAG_STATUS;
1731 event->dnd.send_event = FALSE;
1732 event->dnd.context = context;
1733 gdk_drag_context_ref (context);
1735 event->dnd.time = timestamp;
1737 if ((flags & 0x00f0) >> 4 == XmDROP_SITE_VALID)
1739 switch (flags & 0x000f)
1742 context->action = 0;
1745 context->action = GDK_ACTION_MOVE;
1748 context->action = GDK_ACTION_COPY;
1751 context->action = GDK_ACTION_LINK;
1756 context->action = 0;
1758 return GDK_FILTER_TRANSLATE;
1760 return GDK_FILTER_REMOVE;
1764 motif_dnd_filter (GdkXEvent *xev,
1768 XEvent *xevent = (XEvent *)xev;
1773 guint32 source_window;
1775 gint16 x_root, y_root;
1778 /* First read some fields common to all Motif DND messages */
1780 reason = MOTIF_UNPACK_BYTE (xevent, 0);
1781 flags = MOTIF_UNPACK_SHORT (xevent, 1);
1782 timestamp = MOTIF_UNPACK_LONG (xevent, 1);
1784 is_reply = ((reason & 0x80) != 0);
1786 switch (reason & 0x7f)
1788 case XmTOP_LEVEL_ENTER:
1789 source_window = MOTIF_UNPACK_LONG (xevent, 2);
1790 atom = MOTIF_UNPACK_LONG (xevent, 3);
1791 return motif_top_level_enter (event, flags, timestamp, source_window, atom);
1792 case XmTOP_LEVEL_LEAVE:
1793 return motif_top_level_leave (event, flags, timestamp);
1796 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1797 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1800 return motif_motion (event, flags, timestamp, x_root, y_root);
1802 return motif_drag_status (event, flags, timestamp);
1804 case XmDROP_SITE_ENTER:
1805 return motif_drag_status (event, flags, timestamp);
1807 case XmDROP_SITE_LEAVE:
1808 return motif_drag_status (event,
1809 XmNO_DROP_SITE << 8 | XmDROP_NOOP,
1812 x_root = MOTIF_UNPACK_SHORT (xevent, 4);
1813 y_root = MOTIF_UNPACK_SHORT (xevent, 5);
1814 atom = MOTIF_UNPACK_LONG (xevent, 3);
1815 source_window = MOTIF_UNPACK_LONG (xevent, 4);
1818 return motif_drop_start (event, flags, timestamp, source_window, atom, x_root, y_root);
1821 case XmOPERATION_CHANGED:
1823 return motif_operation_changed (event, flags, timestamp);
1825 return motif_drag_status (event, flags, timestamp);
1828 /* To the best of my knowledge, these next two messages are
1829 * not part of the protocol, though they are defined in
1833 case XmDRAG_DROP_FINISH:
1837 return GDK_FILTER_REMOVE;
1840 /*************************************************************
1841 ***************************** XDND **************************
1842 *************************************************************/
1844 /* Utility functions */
1849 GdkDragAction action;
1850 } xdnd_actions_table[] = {
1851 { "XdndActionCopy", GDK_NONE, GDK_ACTION_COPY },
1852 { "XdndActionMove", GDK_NONE, GDK_ACTION_MOVE },
1853 { "XdndActionLink", GDK_NONE, GDK_ACTION_LINK },
1854 { "XdndActionAsk", GDK_NONE, GDK_ACTION_ASK },
1855 { "XdndActionPrivate", GDK_NONE, GDK_ACTION_COPY },
1858 static const gint xdnd_n_actions = sizeof(xdnd_actions_table) / sizeof(xdnd_actions_table[0]);
1859 static gboolean xdnd_actions_initialized = FALSE;
1862 xdnd_initialize_actions (void)
1866 xdnd_actions_initialized = TRUE;
1867 for (i=0; i < xdnd_n_actions; i++)
1868 xdnd_actions_table[i].atom = gdk_atom_intern (xdnd_actions_table[i].name, FALSE);
1871 static GdkDragAction
1872 xdnd_action_from_atom (GdkAtom atom)
1876 if (!xdnd_actions_initialized)
1877 xdnd_initialize_actions();
1879 for (i=0; i<xdnd_n_actions; i++)
1880 if (atom == xdnd_actions_table[i].atom)
1881 return xdnd_actions_table[i].action;
1887 xdnd_action_to_atom (GdkDragAction action)
1891 if (!xdnd_actions_initialized)
1892 xdnd_initialize_actions();
1894 for (i=0; i<xdnd_n_actions; i++)
1895 if (action == xdnd_actions_table[i].action)
1896 return xdnd_actions_table[i].atom;
1901 static GdkAtom xdnd_aware_atom = GDK_NONE;
1905 static GdkFilterReturn
1906 xdnd_status_filter (GdkXEvent *xev,
1910 XEvent *xevent = (XEvent *)xev;
1911 guint32 dest_window = xevent->xclient.data.l[0];
1912 guint32 flags = xevent->xclient.data.l[1];
1913 GdkAtom action = xevent->xclient.data.l[4];
1914 GdkDragContext *context;
1917 g_message ("XdndStatus: dest_window: %#x action: %ld",
1918 dest_window, action));
1921 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
1924 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1925 if (private->drag_status == GDK_DRAG_STATUS_MOTION_WAIT)
1926 private->drag_status = GDK_DRAG_STATUS_DRAG;
1928 event->dnd.send_event = FALSE;
1929 event->dnd.type = GDK_DRAG_STATUS;
1930 event->dnd.context = context;
1931 gdk_drag_context_ref (context);
1933 event->dnd.time = GDK_CURRENT_TIME; /* FIXME? */
1934 if (!(action != 0) != !(flags & 1))
1937 g_warning ("Received status event with flags not corresponding to action!\n"));
1941 context->action = xdnd_action_from_atom (action);
1943 return GDK_FILTER_TRANSLATE;
1946 return GDK_FILTER_REMOVE;
1949 static GdkFilterReturn
1950 xdnd_finished_filter (GdkXEvent *xev,
1954 XEvent *xevent = (XEvent *)xev;
1955 guint32 dest_window = xevent->xclient.data.l[0];
1956 GdkDragContext *context;
1959 g_message ("XdndFinished: dest_window: %#x", dest_window));
1961 context = gdk_drag_context_find (TRUE, xevent->xclient.window, dest_window);
1964 event->dnd.type = GDK_DROP_FINISHED;
1965 event->dnd.context = context;
1966 gdk_drag_context_ref (context);
1968 return GDK_FILTER_TRANSLATE;
1971 return GDK_FILTER_REMOVE;
1975 xdnd_set_targets (GdkDragContext *context)
1977 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
1979 GList *tmp_list = context->targets;
1981 gint n_atoms = g_list_length (context->targets);
1983 atomlist = g_new (GdkAtom, n_atoms);
1987 atomlist[i] = GPOINTER_TO_INT (tmp_list->data);
1988 tmp_list = tmp_list->next;
1992 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
1993 GDK_WINDOW_XWINDOW (context->source_window),
1994 gdk_atom_intern ("XdndTypeList", FALSE),
1995 XA_ATOM, 32, PropModeReplace,
1996 (guchar *)atomlist, n_atoms);
1998 private->xdnd_targets_set = 1;
2002 xdnd_set_actions (GdkDragContext *context)
2004 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2010 if (!xdnd_actions_initialized)
2011 xdnd_initialize_actions();
2013 actions = context->actions;
2015 for (i=0; i<xdnd_n_actions; i++)
2017 if (actions & xdnd_actions_table[i].action)
2019 actions &= ~xdnd_actions_table[i].action;
2024 atomlist = g_new (GdkAtom, n_atoms);
2026 actions = context->actions;
2028 for (i=0; i<xdnd_n_actions; i++)
2030 if (actions & xdnd_actions_table[i].action)
2032 actions &= ~xdnd_actions_table[i].action;
2033 atomlist[n_atoms] = xdnd_actions_table[i].atom;
2038 XChangeProperty (GDK_WINDOW_XDISPLAY (context->source_window),
2039 GDK_WINDOW_XWINDOW (context->source_window),
2040 gdk_atom_intern ("XdndActionList", FALSE),
2041 XA_ATOM, 32, PropModeReplace,
2042 (guchar *)atomlist, n_atoms);
2044 private->xdnd_actions_set = 1;
2048 xdnd_send_enter (GdkDragContext *context)
2051 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2053 xev.xclient.type = ClientMessage;
2054 xev.xclient.message_type = gdk_atom_intern ("XdndEnter", FALSE);
2055 xev.xclient.format = 32;
2056 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2057 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2058 xev.xclient.data.l[1] = (3 << 24); /* version */
2059 xev.xclient.data.l[2] = 0;
2060 xev.xclient.data.l[3] = 0;
2061 xev.xclient.data.l[4] = 0;
2063 if (!private->xdnd_selection)
2064 private->xdnd_selection = gdk_atom_intern ("XdndSelection", FALSE);
2066 if (g_list_length (context->targets) > 3)
2068 if (!private->xdnd_targets_set)
2069 xdnd_set_targets (context);
2070 xev.xclient.data.l[1] |= 1;
2074 GList *tmp_list = context->targets;
2079 xev.xclient.data.l[i] = GPOINTER_TO_INT (tmp_list->data);
2080 tmp_list = tmp_list->next;
2085 if (!private->xdnd_actions_set)
2086 xdnd_set_actions (context);
2088 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2092 g_message ("Send event to %lx failed",
2093 GDK_WINDOW_XWINDOW (context->dest_window)));
2094 gdk_window_unref (context->dest_window);
2095 context->dest_window = NULL;
2100 xdnd_send_leave (GdkDragContext *context)
2104 xev.xclient.type = ClientMessage;
2105 xev.xclient.message_type = gdk_atom_intern ("XdndLeave", FALSE);
2106 xev.xclient.format = 32;
2107 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] = 0;
2110 xev.xclient.data.l[2] = 0;
2111 xev.xclient.data.l[3] = 0;
2112 xev.xclient.data.l[4] = 0;
2114 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2118 g_message ("Send event to %lx failed",
2119 GDK_WINDOW_XWINDOW (context->dest_window)));
2120 gdk_window_unref (context->dest_window);
2121 context->dest_window = NULL;
2126 xdnd_send_drop (GdkDragContext *context, guint32 time)
2130 xev.xclient.type = ClientMessage;
2131 xev.xclient.message_type = gdk_atom_intern ("XdndDrop", FALSE);
2132 xev.xclient.format = 32;
2133 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2134 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2135 xev.xclient.data.l[1] = 0;
2136 xev.xclient.data.l[2] = time;
2137 xev.xclient.data.l[3] = 0;
2138 xev.xclient.data.l[4] = 0;
2140 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2144 g_message ("Send event to %lx failed",
2145 GDK_WINDOW_XWINDOW (context->dest_window)));
2146 gdk_window_unref (context->dest_window);
2147 context->dest_window = NULL;
2152 xdnd_send_motion (GdkDragContext *context,
2155 GdkDragAction action,
2158 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2161 xev.xclient.type = ClientMessage;
2162 xev.xclient.message_type = gdk_atom_intern ("XdndPosition", FALSE);
2163 xev.xclient.format = 32;
2164 xev.xclient.window = GDK_WINDOW_XWINDOW (context->dest_window);
2165 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->source_window);
2166 xev.xclient.data.l[1] = 0;
2167 xev.xclient.data.l[2] = (x_root << 16) | y_root;
2168 xev.xclient.data.l[3] = time;
2169 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
2171 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->dest_window),
2175 g_message ("Send event to %lx failed",
2176 GDK_WINDOW_XWINDOW (context->dest_window)));
2177 gdk_window_unref (context->dest_window);
2178 context->dest_window = NULL;
2180 private->drag_status = GDK_DRAG_STATUS_MOTION_WAIT;
2184 xdnd_check_dest (Window win)
2186 gboolean retval = FALSE;
2189 unsigned long nitems, after;
2193 static GdkAtom xdnd_proxy_atom = GDK_NONE;
2195 gint old_warnings = gdk_error_warnings;
2197 if (!xdnd_proxy_atom)
2198 xdnd_proxy_atom = gdk_atom_intern ("XdndProxy", FALSE);
2200 if (!xdnd_aware_atom)
2201 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
2206 gdk_error_warnings = 0;
2208 XGetWindowProperty (gdk_display, win,
2210 1, False, AnyPropertyType,
2211 &type, &format, &nitems, &after,
2212 (guchar **)&proxy_data);
2214 if (!gdk_error_code)
2218 if ((format == 32) && (nitems == 1))
2220 proxy = *proxy_data;
2224 g_warning ("Invalid XdndOwner property on window %ld\n", win));
2229 XGetWindowProperty (gdk_display, proxy ? proxy : win,
2231 1, False, AnyPropertyType,
2232 &type, &format, &nitems, &after,
2233 (guchar **)&version);
2235 if (!gdk_error_code && type != None)
2237 if ((format == 32) && (nitems == 1))
2244 g_warning ("Invalid XdndAware property on window %ld\n", win));
2251 gdk_error_warnings = old_warnings;
2254 return retval ? (proxy ? proxy : win) : GDK_NONE;
2259 static GdkFilterReturn
2260 xdnd_enter_filter (GdkXEvent *xev,
2264 XEvent *xevent = (XEvent *)xev;
2265 GdkDragContext *new_context;
2270 gulong nitems, after;
2273 guint32 source_window = xevent->xclient.data.l[0];
2274 gboolean get_types = ((xevent->xclient.data.l[1] & 1) != 0);
2275 gint version = (xevent->xclient.data.l[1] & 0xff000000) >> 24;
2278 g_message ("XdndEnter: source_window: %#x, version: %#x",
2279 source_window, version));
2283 /* Old source ignore */
2284 GDK_NOTE (DND, g_message ("Ignored old XdndEnter message"));
2285 return GDK_FILTER_REMOVE;
2288 if (current_dest_drag != NULL)
2290 gdk_drag_context_unref (current_dest_drag);
2291 current_dest_drag = NULL;
2294 new_context = gdk_drag_context_new ();
2295 new_context->protocol = GDK_DRAG_PROTO_XDND;
2296 new_context->is_source = FALSE;
2298 new_context->source_window = gdk_window_lookup (source_window);
2299 if (new_context->source_window)
2300 gdk_window_ref (new_context->source_window);
2303 new_context->source_window = gdk_window_foreign_new (source_window);
2304 if (!new_context->source_window)
2306 gdk_drag_context_unref (new_context);
2307 return GDK_FILTER_REMOVE;
2310 new_context->dest_window = event->any.window;
2311 gdk_window_ref (new_context->dest_window);
2313 new_context->targets = NULL;
2316 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2318 gdk_atom_intern ("XdndTypeList", FALSE), 0, 65536,
2319 False, XA_ATOM, &type, &format, &nitems,
2320 &after, (guchar **)&data);
2322 if ((format != 32) || (type != XA_ATOM))
2324 gdk_drag_context_unref (new_context);
2325 return GDK_FILTER_REMOVE;
2328 for (i=0; i<nitems; i++)
2329 new_context->targets = g_list_append (new_context->targets,
2330 GUINT_TO_POINTER (data[i]));
2337 if (xevent->xclient.data.l[2+i])
2338 new_context->targets = g_list_append (new_context->targets,
2339 GUINT_TO_POINTER (xevent->xclient.data.l[2+i]));
2342 /* Get the XdndActionList, if set */
2344 XGetWindowProperty (GDK_WINDOW_XDISPLAY (event->any.window),
2346 gdk_atom_intern ("XdndActionList", FALSE), 0, 65536,
2347 False, XA_ATOM, &type, &format, &nitems,
2348 &after, (guchar **)&data);
2350 if ((format == 32) && (type == XA_ATOM))
2352 new_context->actions = 0;
2354 for (i=0; i<nitems; i++)
2355 new_context->actions |= xdnd_action_from_atom (data[i]);
2357 ((GdkDragContextPrivate *)new_context)->xdnd_have_actions = TRUE;
2359 #ifdef G_ENABLE_DEBUG
2360 if (gdk_debug_flags & GDK_DEBUG_DND)
2362 GString *action_str = g_string_new (NULL);
2363 if (new_context->actions & GDK_ACTION_MOVE)
2364 g_string_append(action_str, "MOVE ");
2365 if (new_context->actions & GDK_ACTION_COPY)
2366 g_string_append(action_str, "COPY ");
2367 if (new_context->actions & GDK_ACTION_LINK)
2368 g_string_append(action_str, "LINK ");
2369 if (new_context->actions & GDK_ACTION_ASK)
2370 g_string_append(action_str, "ASK ");
2372 g_message("\tactions = %s", action_str->str);
2373 g_string_free (action_str, TRUE);
2375 #endif /* G_ENABLE_DEBUG */
2380 #ifdef G_ENABLE_DEBUG
2381 if (gdk_debug_flags & GDK_DEBUG_DND)
2382 print_target_list (new_context->targets);
2383 #endif /* G_ENABLE_DEBUG */
2385 event->dnd.type = GDK_DRAG_ENTER;
2386 event->dnd.context = new_context;
2387 gdk_drag_context_ref (new_context);
2389 current_dest_drag = new_context;
2390 ((GdkDragContextPrivate *)new_context)->xdnd_selection =
2391 gdk_atom_intern ("XdndSelection", FALSE);
2393 return GDK_FILTER_TRANSLATE;
2396 static GdkFilterReturn
2397 xdnd_leave_filter (GdkXEvent *xev,
2401 XEvent *xevent = (XEvent *)xev;
2402 guint32 source_window = xevent->xclient.data.l[0];
2405 g_message ("XdndLeave: source_window: %#x",
2408 if ((current_dest_drag != NULL) &&
2409 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2410 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2412 event->dnd.type = GDK_DRAG_LEAVE;
2413 /* Pass ownership of context to the event */
2414 event->dnd.context = current_dest_drag;
2416 current_dest_drag = NULL;
2418 return GDK_FILTER_TRANSLATE;
2421 return GDK_FILTER_REMOVE;
2424 static GdkFilterReturn
2425 xdnd_position_filter (GdkXEvent *xev,
2429 XEvent *xevent = (XEvent *)xev;
2430 guint32 source_window = xevent->xclient.data.l[0];
2431 gint16 x_root = xevent->xclient.data.l[2] >> 16;
2432 gint16 y_root = xevent->xclient.data.l[2] & 0xffff;
2433 guint32 time = xevent->xclient.data.l[3];
2434 GdkAtom action = xevent->xclient.data.l[4];
2437 g_message ("XdndPosition: source_window: %#x position: (%d, %d) time: %d action: %ld",
2438 source_window, x_root, y_root, time, action));
2441 if ((current_dest_drag != NULL) &&
2442 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2443 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2445 event->dnd.type = GDK_DRAG_MOTION;
2446 event->dnd.context = current_dest_drag;
2447 gdk_drag_context_ref (current_dest_drag);
2449 event->dnd.time = time;
2451 current_dest_drag->suggested_action = xdnd_action_from_atom (action);
2452 if (!((GdkDragContextPrivate *)current_dest_drag)->xdnd_have_actions)
2453 current_dest_drag->actions = current_dest_drag->suggested_action;
2455 event->dnd.x_root = x_root;
2456 event->dnd.y_root = y_root;
2458 ((GdkDragContextPrivate *)current_dest_drag)->last_x = x_root;
2459 ((GdkDragContextPrivate *)current_dest_drag)->last_y = y_root;
2461 return GDK_FILTER_TRANSLATE;
2464 return GDK_FILTER_REMOVE;
2467 static GdkFilterReturn
2468 xdnd_drop_filter (GdkXEvent *xev,
2472 XEvent *xevent = (XEvent *)xev;
2473 guint32 source_window = xevent->xclient.data.l[0];
2474 guint32 time = xevent->xclient.data.l[2];
2477 g_message ("XdndDrop: source_window: %#x time: %d",
2478 source_window, time));
2480 if ((current_dest_drag != NULL) &&
2481 (current_dest_drag->protocol == GDK_DRAG_PROTO_XDND) &&
2482 (GDK_WINDOW_XWINDOW (current_dest_drag->source_window) == source_window))
2484 GdkDragContextPrivate *private;
2485 private = (GdkDragContextPrivate *)current_dest_drag;
2487 event->dnd.type = GDK_DROP_START;
2489 event->dnd.context = current_dest_drag;
2490 gdk_drag_context_ref (current_dest_drag);
2492 event->dnd.time = time;
2493 event->dnd.x_root = private->last_x;
2494 event->dnd.y_root = private->last_y;
2496 return GDK_FILTER_TRANSLATE;
2499 return GDK_FILTER_REMOVE;
2502 /*************************************************************
2503 ************************** Public API ***********************
2504 *************************************************************/
2511 gdk_add_client_message_filter (
2512 gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE),
2513 motif_dnd_filter, NULL);
2514 gdk_add_client_message_filter (
2515 gdk_atom_intern ("XdndEnter", FALSE),
2516 xdnd_enter_filter, NULL);
2517 gdk_add_client_message_filter (
2518 gdk_atom_intern ("XdndLeave", FALSE),
2519 xdnd_leave_filter, NULL);
2520 gdk_add_client_message_filter (
2521 gdk_atom_intern ("XdndPosition", FALSE),
2522 xdnd_position_filter, NULL);
2523 gdk_add_client_message_filter (
2524 gdk_atom_intern ("XdndStatus", FALSE),
2525 xdnd_status_filter, NULL);
2526 gdk_add_client_message_filter (
2527 gdk_atom_intern ("XdndFinished", FALSE),
2528 xdnd_finished_filter, NULL);
2529 gdk_add_client_message_filter (
2530 gdk_atom_intern ("XdndDrop", FALSE),
2531 xdnd_drop_filter, NULL);
2537 gdk_drag_do_leave (GdkDragContext *context, guint32 time)
2539 if (context->dest_window)
2541 switch (context->protocol)
2543 case GDK_DRAG_PROTO_MOTIF:
2544 motif_send_leave (context, time);
2546 case GDK_DRAG_PROTO_XDND:
2547 xdnd_send_leave (context);
2549 case GDK_DRAG_PROTO_ROOTWIN:
2550 case GDK_DRAG_PROTO_NONE:
2554 gdk_window_unref (context->dest_window);
2555 context->dest_window = NULL;
2560 gdk_drag_begin (GdkWindow *window,
2562 GdkDragAction actions)
2565 GdkDragContext *new_context;
2567 g_return_val_if_fail (window != NULL, NULL);
2569 new_context = gdk_drag_context_new ();
2570 new_context->is_source = TRUE;
2571 new_context->source_window = window;
2572 gdk_window_ref (window);
2574 tmp_list = g_list_last (targets);
2575 new_context->targets = NULL;
2578 new_context->targets = g_list_prepend (new_context->targets,
2580 tmp_list = tmp_list->prev;
2583 new_context->actions = actions;
2589 gdk_drag_get_protocol (guint32 xid,
2590 GdkDragProtocol *protocol)
2594 if ((retval = xdnd_check_dest (xid)))
2596 *protocol = GDK_DRAG_PROTO_XDND;
2597 GDK_NOTE (DND, g_message ("Entering dnd window %#x\n", xid));
2600 else if ((retval = motif_check_dest (xid)))
2602 *protocol = GDK_DRAG_PROTO_MOTIF;
2603 GDK_NOTE (DND, g_message ("Entering motif window %#x\n", xid));
2608 /* Check if this is a root window */
2610 gboolean rootwin = FALSE;
2611 gint old_warnings = gdk_error_warnings;
2614 unsigned long nitems, after;
2615 unsigned char *data;
2617 if (xid == gdk_root_window)
2624 XGetWindowProperty (gdk_display, xid,
2625 gdk_atom_intern ("ENLIGHTENMENT_DESKTOP", FALSE),
2626 0, 0, False, AnyPropertyType,
2627 &type, &format, &nitems, &after, &data);
2628 if ((gdk_error_code == 0) && type != None)
2635 /* Until I find out what window manager the next one is for,
2636 * I'm leaving it commented out. It's supported in the
2637 * xscreensaver sources, though.
2644 XGetWindowProperty (gdk_display, win,
2645 gdk_atom_intern ("__SWM_VROOT", FALSE),
2646 0, 0, False, AnyPropertyType,
2647 &type, &format, &nitems, &data);
2648 if ((gdk_error_code == 0) && type != None)
2653 gdk_error_warnings = old_warnings;
2657 *protocol = GDK_DRAG_PROTO_ROOTWIN;
2662 *protocol = GDK_DRAG_PROTO_NONE;
2667 gdk_drag_find_window (GdkDragContext *context,
2668 GdkWindow *drag_window,
2671 GdkWindow **dest_window,
2672 GdkDragProtocol *protocol)
2674 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2677 g_return_if_fail (context != NULL);
2679 if (!private->window_cache)
2680 private->window_cache = gdk_window_cache_new();
2682 dest = get_client_window_at_coords (private->window_cache,
2684 GDK_WINDOW_XWINDOW (drag_window) : GDK_NONE,
2687 if (private->dest_xid != dest)
2690 private->dest_xid = dest;
2692 /* Check if new destination accepts drags, and which protocol */
2694 if ((recipient = gdk_drag_get_protocol (dest, protocol)))
2696 *dest_window = gdk_window_lookup (recipient);
2698 gdk_window_ref (*dest_window);
2700 *dest_window = gdk_window_foreign_new (recipient);
2703 *dest_window = NULL;
2707 *dest_window = context->dest_window;
2709 gdk_window_ref (*dest_window);
2710 *protocol = context->protocol;
2715 gdk_drag_motion (GdkDragContext *context,
2716 GdkWindow *dest_window,
2717 GdkDragProtocol protocol,
2720 GdkDragAction action,
2723 GdkDragContextPrivate *private = (GdkDragContextPrivate *)context;
2725 g_return_val_if_fail (context != NULL, FALSE);
2727 if (context->dest_window != dest_window)
2729 GdkEvent temp_event;
2731 /* Send a leave to the last destination */
2732 gdk_drag_do_leave (context, time);
2733 private->drag_status = GDK_DRAG_STATUS_DRAG;
2735 /* Check if new destination accepts drags, and which protocol */
2739 context->dest_window = dest_window;
2740 gdk_window_ref (context->dest_window);
2741 context->protocol = protocol;
2745 case GDK_DRAG_PROTO_MOTIF:
2746 motif_send_enter (context, time);
2749 case GDK_DRAG_PROTO_XDND:
2750 xdnd_send_enter (context);
2753 case GDK_DRAG_PROTO_ROOTWIN:
2754 case GDK_DRAG_PROTO_NONE:
2757 private->old_action = action;
2758 context->suggested_action = action;
2762 context->dest_window = NULL;
2763 context->action = 0;
2766 /* Push a status event, to let the client know that
2770 temp_event.dnd.type = GDK_DRAG_STATUS;
2771 temp_event.dnd.window = context->source_window;
2772 /* We use this to signal a synthetic status. Perhaps
2773 * we should use an extra field...
2775 temp_event.dnd.send_event = TRUE;
2777 temp_event.dnd.context = context;
2778 temp_event.dnd.time = time;
2780 gdk_event_put (&temp_event);
2784 private->old_action = context->suggested_action;
2785 context->suggested_action = action;
2788 /* Send a drag-motion event */
2790 private->last_x = x_root;
2791 private->last_y = y_root;
2793 if (context->dest_window)
2795 if (private->drag_status == GDK_DRAG_STATUS_DRAG)
2797 switch (context->protocol)
2799 case GDK_DRAG_PROTO_MOTIF:
2800 motif_send_motion (context, x_root, y_root, action, time);
2803 case GDK_DRAG_PROTO_XDND:
2804 xdnd_send_motion (context, x_root, y_root, action, time);
2807 case GDK_DRAG_PROTO_ROOTWIN:
2809 GdkEvent temp_event;
2811 if (g_list_find (context->targets,
2812 GUINT_TO_POINTER (gdk_atom_intern ("application/x-rootwin-drop", FALSE))))
2813 context->action = context->suggested_action;
2815 context->action = 0;
2817 temp_event.dnd.type = GDK_DRAG_STATUS;
2818 temp_event.dnd.window = context->source_window;
2819 temp_event.dnd.send_event = FALSE;
2820 temp_event.dnd.context = context;
2821 temp_event.dnd.time = time;
2823 gdk_event_put (&temp_event);
2826 case GDK_DRAG_PROTO_NONE:
2827 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_motion()");
2839 gdk_drag_drop (GdkDragContext *context,
2842 g_return_if_fail (context != NULL);
2844 if (context->dest_window)
2846 switch (context->protocol)
2848 case GDK_DRAG_PROTO_MOTIF:
2849 motif_send_leave (context, time);
2850 motif_send_drop (context, time);
2853 case GDK_DRAG_PROTO_XDND:
2854 xdnd_send_drop (context, time);
2857 case GDK_DRAG_PROTO_ROOTWIN:
2858 g_warning ("Drops for GDK_DRAG_PROTO_ROOTWIN must be handled internally");
2860 case GDK_DRAG_PROTO_NONE:
2861 g_warning ("GDK_DRAG_PROTO_NONE is not valid in gdk_drag_drop()");
2868 gdk_drag_abort (GdkDragContext *context,
2871 g_return_if_fail (context != NULL);
2873 gdk_drag_do_leave (context, time);
2876 /* Destination side */
2879 gdk_drag_status (GdkDragContext *context,
2880 GdkDragAction action,
2883 GdkDragContextPrivate *private;
2886 g_return_if_fail (context != NULL);
2888 private = (GdkDragContextPrivate *)context;
2890 context->action = action;
2892 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
2894 xev.xclient.type = ClientMessage;
2895 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
2896 xev.xclient.format = 8;
2897 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
2899 if (private->drag_status == GDK_DRAG_STATUS_ACTION_WAIT)
2901 MOTIF_XCLIENT_BYTE (&xev, 0) = XmOPERATION_CHANGED | 0x80;
2905 if ((action != 0) != (private->old_action != 0))
2908 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_ENTER | 0x80;
2910 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_SITE_LEAVE | 0x80;
2913 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDRAG_MOTION | 0x80;
2916 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
2920 case GDK_ACTION_MOVE:
2921 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_MOVE;
2923 case GDK_ACTION_COPY:
2924 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY;
2926 case GDK_ACTION_LINK:
2927 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_LINK;
2930 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP;
2935 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmDROP_SITE_VALID << 4);
2937 MOTIF_XCLIENT_SHORT (&xev, 1) |= (XmNO_DROP_SITE << 4);
2939 MOTIF_XCLIENT_LONG (&xev, 1) = time;
2940 MOTIF_XCLIENT_SHORT (&xev, 4) = private->last_x;
2941 MOTIF_XCLIENT_SHORT (&xev, 5) = private->last_y;
2943 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
2946 g_message ("Send event to %lx failed",
2947 GDK_WINDOW_XWINDOW (context->source_window)));
2949 else if (context->protocol == GDK_DRAG_PROTO_XDND)
2951 xev.xclient.type = ClientMessage;
2952 xev.xclient.message_type = gdk_atom_intern ("XdndStatus", FALSE);
2953 xev.xclient.format = 32;
2954 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
2956 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->dest_window);
2957 xev.xclient.data.l[1] = (action != 0) ? (2 | 1) : 0;
2958 xev.xclient.data.l[2] = 0;
2959 xev.xclient.data.l[3] = 0;
2960 xev.xclient.data.l[4] = xdnd_action_to_atom (action);
2962 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
2965 g_message ("Send event to %lx failed",
2966 GDK_WINDOW_XWINDOW (context->source_window)));
2969 private->old_action = action;
2973 gdk_drop_reply (GdkDragContext *context,
2977 GdkDragContextPrivate *private;
2979 g_return_if_fail (context != NULL);
2981 private = (GdkDragContextPrivate *)context;
2983 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
2987 xev.xclient.type = ClientMessage;
2988 xev.xclient.message_type = gdk_atom_intern ("_MOTIF_DRAG_AND_DROP_MESSAGE", FALSE);
2989 xev.xclient.format = 8;
2991 MOTIF_XCLIENT_BYTE (&xev, 0) = XmDROP_START | 0x80;
2992 MOTIF_XCLIENT_BYTE (&xev, 1) = local_byte_order;
2994 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_COPY |
2995 (XmDROP_SITE_VALID << 4) |
2996 (XmDROP_NOOP << 8) |
2999 MOTIF_XCLIENT_SHORT (&xev, 1) = XmDROP_NOOP |
3000 (XmNO_DROP_SITE << 4) |
3001 (XmDROP_NOOP << 8) |
3002 (XmDROP_CANCEL << 12);
3003 MOTIF_XCLIENT_SHORT (&xev, 2) = private->last_x;
3004 MOTIF_XCLIENT_SHORT (&xev, 3) = private->last_y;
3006 gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3012 gdk_drop_finish (GdkDragContext *context,
3016 g_return_if_fail (context != NULL);
3018 if (context->protocol == GDK_DRAG_PROTO_XDND)
3022 xev.xclient.type = ClientMessage;
3023 xev.xclient.message_type = gdk_atom_intern ("XdndFinished", FALSE);
3024 xev.xclient.format = 32;
3025 xev.xclient.window = GDK_WINDOW_XWINDOW (context->source_window);
3027 xev.xclient.data.l[0] = GDK_WINDOW_XWINDOW (context->dest_window);
3028 xev.xclient.data.l[1] = 0;
3029 xev.xclient.data.l[2] = 0;
3030 xev.xclient.data.l[3] = 0;
3031 xev.xclient.data.l[4] = 0;
3033 if (!gdk_send_xevent (GDK_WINDOW_XWINDOW (context->source_window),
3036 g_message ("Send event to %lx failed",
3037 GDK_WINDOW_XWINDOW (context->source_window)));
3043 gdk_window_register_dnd (GdkWindow *window)
3045 static gulong xdnd_version = 3;
3046 MotifDragReceiverInfo info;
3048 g_return_if_fail (window != NULL);
3050 /* Set Motif drag receiver information property */
3052 if (!motif_drag_receiver_info_atom)
3053 motif_drag_receiver_info_atom = gdk_atom_intern ("_MOTIF_DRAG_RECEIVER_INFO", FALSE);
3055 info.byte_order = local_byte_order;
3056 info.protocol_version = 0;
3057 info.protocol_style = XmDRAG_DYNAMIC;
3058 info.proxy_window = GDK_NONE;
3059 info.num_drop_sites = 0;
3060 info.total_size = sizeof(info);
3062 XChangeProperty (gdk_display, GDK_WINDOW_XWINDOW (window),
3063 motif_drag_receiver_info_atom,
3064 motif_drag_receiver_info_atom,
3071 if (!xdnd_aware_atom)
3072 xdnd_aware_atom = gdk_atom_intern ("XdndAware", FALSE);
3074 /* The property needs to be of type XA_ATOM, not XA_INTEGER. Blech */
3075 XChangeProperty (GDK_WINDOW_XDISPLAY (window),
3076 GDK_WINDOW_XWINDOW (window),
3077 xdnd_aware_atom, XA_ATOM,
3078 32, PropModeReplace,
3079 (guchar *)&xdnd_version, 1);
3082 /*************************************************************
3083 * gdk_drag_get_selection:
3084 * Returns the selection atom for the current source window
3088 *************************************************************/
3091 gdk_drag_get_selection (GdkDragContext *context)
3093 g_return_val_if_fail (context != NULL, GDK_NONE);
3095 if (context->protocol == GDK_DRAG_PROTO_MOTIF)
3096 return ((GdkDragContextPrivate *)context)->motif_selection;
3097 else if (context->protocol == GDK_DRAG_PROTO_XDND)
3098 return ((GdkDragContextPrivate *)context)->xdnd_selection;