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