FLTK 1.3.9: fl_dnd_x.cxx Source File

FLTK 1.3.9

Loading...
Searching...
No Matches
fl_dnd_x.cxx
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
23extern Atom fl_XdndAware;
24extern Atom fl_XdndSelection;
25extern Atom fl_XdndEnter;
26extern Atom fl_XdndTypeList;
27extern Atom fl_XdndPosition;
28extern Atom fl_XdndLeave;
29extern Atom fl_XdndDrop;
30extern Atom fl_XdndStatus;
31extern Atom fl_XdndActionCopy;
32extern Atom fl_XdndFinished;
33extern Atom fl_XdndURIList;
34extern Atom fl_XaUtf8String;
35
36extern char fl_i_own_selection[2];
37extern char *fl_selection_buffer[2];
38
39extern 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:
48static 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
62static int grabfunc(int event) {
63 if (event == FL_RELEASE) Fl::pushed(0);
64 return 0;
65}
66
67extern int (*fl_local_grab)(int); // in Fl.cxx
68
69// send an event to an fltk window belonging to this program:
70static 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
79int 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//
FL_CURSOR_MOVE
@ FL_CURSOR_MOVE
4-pointed arrow or hand.
Definition Enumerations.H:1054
FL_CURSOR_DEFAULT
@ FL_CURSOR_DEFAULT
the default cursor, usually an arrow.
Definition Enumerations.H:1047
FL_DND_LEAVE
@ FL_DND_LEAVE
The mouse has moved out of the widget.
Definition Enumerations.H:409
FL_RELEASE
@ FL_RELEASE
A mouse button has been released.
Definition Enumerations.H:242
FL_DND_RELEASE
@ FL_DND_RELEASE
The user has released the mouse button dropping data into the widget.
Definition Enumerations.H:415
FL_DND_DRAG
@ FL_DND_DRAG
The mouse has been moved inside a widget while dragging data.
Definition Enumerations.H:405
FL_DND_ENTER
@ FL_DND_ENTER
The mouse has been moved to point at this widget.
Definition Enumerations.H:399
Fl.H
Fl static class.
Fl_Window.H
Fl_Window widget .
Fl_Widget::x
void x(int v)
Internal use only.
Definition Fl_Widget.H:139
Fl_Widget::y
void y(int v)
Internal use only.
Definition Fl_Widget.H:141
Fl_Window
This widget produces an actual window.
Definition Fl_Window.H:57
Fl_Window::cursor
void cursor(Fl_Cursor)
Changes the cursor for this window.
Definition fl_cursor.cxx:111
Fl::wait
static int wait()
Waits until "something happens" and then returns.
Definition Fl.cxx:670
Fl::paste
static void paste(Fl_Widget &receiver, int source, const char *type=Fl::clipboard_plain_text)
Pastes the data from the selection buffer (source is 0) or the clipboard (source is 1) into receiver.
Fl::dnd
static int dnd()
Initiate a Drag And Drop operation.
Definition fl_dnd_win32.cxx:535
Fl::handle
static int handle(int, Fl_Window *)
Handle events from the window system.
Definition Fl.cxx:1295
Fl::pushed
static Fl_Widget * pushed()
Gets the widget that is being pushed.
Definition Fl.H:844
Fl::belowmouse
static Fl_Widget * belowmouse()
Gets the widget that is below the mouse.
Definition Fl.H:840
Fl::first_window
static Fl_Window * first_window()
Returns the first top-level window in the list of shown() windows.
Definition Fl.cxx:755