1 //
2 // Drag & Drop code for the Fast Light Tool Kit (FLTK).
3 //
4 // Copyright 1998-2021 by Bill Spitzak and others.
5 //
6 // This library is free software. Distribution and use rights are outlined in
7 // the file "COPYING" which should have been included with this file.  If this
8 // file is missing or damaged, see the license at:
9 //
10 //     https://www.fltk.org/COPYING.php
11 //
12 // Please see the following page on how to report bugs and issues:
13 //
14 //     https://www.fltk.org/bugs.php
15 //
16 
17 #include <FL/Fl.H>
18 #include <FL/Fl_Window.H>
19 #include <FL/x.H>
20 #include "flstring.h"
21 
22 
23 extern Atom fl_XdndAware;
24 extern Atom fl_XdndSelection;
25 extern Atom fl_XdndEnter;
26 extern Atom fl_XdndTypeList;
27 extern Atom fl_XdndPosition;
28 extern Atom fl_XdndLeave;
29 extern Atom fl_XdndDrop;
30 extern Atom fl_XdndStatus;
31 extern Atom fl_XdndActionCopy;
32 extern Atom fl_XdndFinished;
33 extern Atom fl_XdndURIList;
34 extern Atom fl_XaUtf8String;
35 
36 extern char fl_i_own_selection[2];
37 extern char *fl_selection_buffer[2];
38 
39 extern void fl_sendClientMessage(Window window, Atom message,
40                                  unsigned long d0,
41                                  unsigned long d1=0,
42                                  unsigned long d2=0,
43                                  unsigned long d3=0,
44                                  unsigned long d4=0);
45 
46 // return version # of Xdnd this window supports.  Also change the
47 // window to the proxy if it uses a proxy:
dnd_aware(Window & window)48 static int dnd_aware(Window& window) {
49   Atom actual; int format; unsigned long count, remaining;
50   unsigned char *data = 0;
51   XGetWindowProperty(fl_display, window, fl_XdndAware,
52 		     0, 4, False, XA_ATOM,
53 		     &actual, &format,
54 		     &count, &remaining, &data);
55   int ret = 0;
56   if (actual == XA_ATOM && format==32 && count && data)
57     ret = int(*(Atom*)data);
58   if (data) { XFree(data); data = 0; }
59   return ret;
60 }
61 
grabfunc(int event)62 static int grabfunc(int event) {
63   if (event == FL_RELEASE) Fl::pushed(0);
64   return 0;
65 }
66 
67 extern int (*fl_local_grab)(int); // in Fl.cxx
68 
69 // send an event to an fltk window belonging to this program:
local_handle(int event,Fl_Window * window)70 static int local_handle(int event, Fl_Window* window) {
71   fl_local_grab = 0;
72   Fl::e_x = Fl::e_x_root-window->x();
73   Fl::e_y = Fl::e_y_root-window->y();
74   int ret = Fl::handle(event,window);
75   fl_local_grab = grabfunc;
76   return ret;
77 }
78 
dnd()79 int Fl::dnd() {
80   Fl_Window *source_fl_win = Fl::first_window();
81   Fl::first_window()->cursor(FL_CURSOR_MOVE);
82   Window source_window = fl_xid(Fl::first_window());
83   fl_local_grab = grabfunc;
84   Window target_window = 0;
85   Fl_Window* local_window = 0;
86   int dndversion = 4; int dest_x, dest_y;
87   XSetSelectionOwner(fl_display, fl_XdndSelection, fl_message_window, fl_event_time);
88 
89   while (Fl::pushed()) {
90     // figure out what window we are pointing at:
91     Window new_window = 0; int new_version = 0;
92     Fl_Window* new_local_window = 0;
93     for (Window child = RootWindow(fl_display, fl_screen);;) {
94       Window root; unsigned int junk3;
95       XQueryPointer(fl_display, child, &root, &child,
96 		    &e_x_root, &e_y_root, &dest_x, &dest_y, &junk3);
97       if (!child) {
98 	if (!new_window && (new_version = dnd_aware(root))) new_window = root;
99 	break;
100       }
101       new_window = child;
102       if ((new_local_window = fl_find(child))) break;
103       if ((new_version = dnd_aware(new_window))) break;
104     }
105 
106     if (new_window != target_window) {
107       if (local_window) {
108 	local_handle(FL_DND_LEAVE, local_window);
109       } else if (dndversion) {
110 	fl_sendClientMessage(target_window, fl_XdndLeave, source_window);
111       }
112       dndversion = new_version;
113       target_window = new_window;
114       local_window = new_local_window;
115       if (local_window) {
116 	local_handle(FL_DND_ENTER, local_window);
117       } else if (dndversion) {
118         // Send an X-DND message to the target window.  In order to
119 	// support dragging of files/URLs as well as arbitrary text,
120 	// we look at the selection buffer - if the buffer starts
121 	// with a common URI scheme, does not contain spaces, and
122 	// contains at least one CR LF, then we flag the data as
123 	// both a URI list (MIME media type "text/uri-list") and
124 	// plain text.  Otherwise, we just say it is plain text.
125         if ((!strncmp(fl_selection_buffer[0], "file:///", 8) ||
126 	     !strncmp(fl_selection_buffer[0], "ftp://", 6) ||
127 	     !strncmp(fl_selection_buffer[0], "http://", 7) ||
128 	     !strncmp(fl_selection_buffer[0], "https://", 8) ||
129 	     !strncmp(fl_selection_buffer[0], "ipp://", 6) ||
130 	     !strncmp(fl_selection_buffer[0], "ldap:", 5) ||
131 	     !strncmp(fl_selection_buffer[0], "mailto:", 7) ||
132 	     !strncmp(fl_selection_buffer[0], "news:", 5) ||
133 	     !strncmp(fl_selection_buffer[0], "smb://", 6)) &&
134 	    !strchr(fl_selection_buffer[0], ' ') &&
135 	    strstr(fl_selection_buffer[0], "\r\n")) {
136 	  // Send file/URI list...
137           fl_sendClientMessage(target_window, fl_XdndEnter, source_window, dndversion<<24,
138                                fl_XdndURIList, fl_XaUtf8String, XA_STRING);
139         } else {
140           // Send plain text...
141           fl_sendClientMessage(target_window, fl_XdndEnter, source_window, dndversion<<24,
142                                fl_XaUtf8String, XA_STRING, 0);
143         }
144       }
145     }
146     if (local_window) {
147       local_handle(FL_DND_DRAG, local_window);
148     } else if (dndversion) {
149       fl_sendClientMessage(target_window, fl_XdndPosition, source_window,
150 			   0, (e_x_root<<16)|e_y_root, fl_event_time,
151 			   fl_XdndActionCopy);
152     }
153     Fl::wait();
154   }
155 
156   if (local_window) {
157     fl_i_own_selection[0] = 1;
158     if (local_handle(FL_DND_RELEASE, local_window)) paste(*belowmouse(), 0);
159   } else if (dndversion) {
160     fl_sendClientMessage(target_window, fl_XdndDrop, source_window,
161 			 0, fl_event_time);
162   } else if (target_window) {
163     // fake a drop by clicking the middle mouse button:
164     XButtonEvent msg;
165     msg.type = ButtonPress;
166     msg.window = target_window;
167     msg.root = RootWindow(fl_display, fl_screen);
168     msg.subwindow = 0;
169     msg.time = fl_event_time+1;
170     msg.x = dest_x;
171     msg.y = dest_y;
172     msg.x_root = Fl::e_x_root;
173     msg.y_root = Fl::e_y_root;
174     msg.state = 0x0;
175     msg.button = Button2;
176     XSendEvent(fl_display, target_window, False, 0L, (XEvent*)&msg);
177     msg.time++;
178     msg.state = 0x200;
179     msg.type = ButtonRelease;
180     XSendEvent(fl_display, target_window, False, 0L, (XEvent*)&msg);
181   }
182 
183   fl_local_grab = 0;
184   source_fl_win->cursor(FL_CURSOR_DEFAULT);
185   return 1;
186 }
187 
188 
189 //
190 // End of "$Id$".
191 //
192