Commit | Line | Data |
---|---|---|
133aad74 JD |
1 | ;;; x-dnd.el --- drag and drop support for X. |
2 | ||
3 | ;; Copyright (C) 2004 | |
4 | ;; Free Software Foundation, Inc. | |
5 | ||
6 | ;; Author: Jan Dj\e,Ad\e(Brv <jan.h.d@swipnet.se> | |
7 | ;; Maintainer: FSF | |
8 | ;; Keywords: window, drag, drop | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation; either version 2, or (at your option) | |
15 | ;; any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
26 | ||
27 | ;;; Commentary: | |
28 | ||
29 | ;; This file provides the drop part only. Currently supported protocols | |
49f87d23 | 30 | ;; are XDND, Motif and the old KDE 1.x protocol. |
133aad74 JD |
31 | |
32 | ;;; Code: | |
33 | ||
67988557 | 34 | (require 'dnd) |
133aad74 | 35 | |
67988557 | 36 | ;;; Customizable variables |
133aad74 JD |
37 | (defcustom x-dnd-test-function 'x-dnd-default-test-function |
38 | "The function drag and drop uses to determine if to accept or reject a drop. | |
39 | The function takes three arguments, WINDOW ACTION and TYPES. | |
40 | WINDOW is where the mouse is when the function is called. WINDOW may be a | |
41 | frame if the mouse isn't over a real window (i.e. menu bar, tool bar or | |
42 | scroll bar). ACTION is the suggested action from the drag and drop source, | |
43 | one of the symbols move, copy link or ask. TYPES is a list of available types | |
44 | for the drop. | |
45 | ||
46 | The function shall return nil to reject the drop or a cons with two values, | |
47 | the wanted action as car and the wanted type as cdr. The wanted action | |
48 | can be copy, move, link, ask or private. | |
49 | The default value for this variable is `x-dnd-default-test-function'." | |
bf247b6e | 50 | :version "22.1" |
133aad74 JD |
51 | :type 'symbol |
52 | :group 'x) | |
53 | ||
133aad74 JD |
54 | |
55 | ||
56 | (defcustom x-dnd-types-alist | |
57 | '( | |
58 | ("text/uri-list" . x-dnd-handle-uri-list) | |
59 | ("text/x-moz-url" . x-dnd-handle-moz-url) | |
133aad74 | 60 | ("_NETSCAPE_URL" . x-dnd-handle-uri-list) |
b9aafad5 | 61 | ("FILE_NAME" . x-dnd-handle-file-name) |
133aad74 JD |
62 | ("UTF8_STRING" . x-dnd-insert-utf8-text) |
63 | ("text/plain;charset=UTF-8" . x-dnd-insert-utf8-text) | |
64 | ("text/plain;charset=utf-8" . x-dnd-insert-utf8-text) | |
65 | ("text/unicode" . x-dnd-insert-utf16-text) | |
67988557 | 66 | ("text/plain" . dnd-insert-text) |
b9aafad5 | 67 | ("COMPOUND_TEXT" . x-dnd-insert-ctext) |
67988557 JD |
68 | ("STRING" . dnd-insert-text) |
69 | ("TEXT" . dnd-insert-text) | |
133aad74 JD |
70 | ) |
71 | "Which function to call to handle a drop of that type. | |
72 | If the type for the drop is not present, or the function is nil, | |
73 | the drop is rejected. The function takes three arguments, WINDOW, ACTION | |
74 | and DATA. WINDOW is where the drop occured, ACTION is the action for | |
75 | this drop (copy, move, link, private or ask) as determined by a previous | |
76 | call to `x-dnd-test-function'. DATA is the drop data. | |
77 | The function shall return the action used (copy, move, link or private) if drop | |
78 | is successful, nil if not." | |
bf247b6e | 79 | :version "22.1" |
133aad74 JD |
80 | :type 'alist |
81 | :group 'x) | |
82 | ||
7a01b040 | 83 | (defcustom x-dnd-known-types |
133aad74 JD |
84 | '("text/uri-list" |
85 | "text/x-moz-url" | |
133aad74 | 86 | "_NETSCAPE_URL" |
b9aafad5 | 87 | "FILE_NAME" |
133aad74 JD |
88 | "UTF8_STRING" |
89 | "text/plain;charset=UTF-8" | |
90 | "text/plain;charset=utf-8" | |
91 | "text/unicode" | |
92 | "text/plain" | |
b9aafad5 | 93 | "COMPOUND_TEXT" |
133aad74 JD |
94 | "STRING" |
95 | "TEXT" | |
96 | ) | |
97 | "The types accepted by default for dropped data. | |
7a01b040 | 98 | The types are chosen in the order they appear in the list." |
bf247b6e | 99 | :version "22.1" |
7a01b040 JD |
100 | :type '(repeat string) |
101 | :group 'x | |
102 | ) | |
103 | ||
104 | ;; Internal variables | |
133aad74 JD |
105 | |
106 | (defvar x-dnd-current-state nil | |
107 | "The current state for a drop. | |
108 | This is an alist with one entry for each display. The value for each display | |
109 | is a vector that contains the state for drag and drop for that display. | |
bf247b6e | 110 | Elements in the vector are: |
133aad74 JD |
111 | Last buffer drag was in, |
112 | last window drag was in, | |
bf247b6e | 113 | types available for drop, |
133aad74 JD |
114 | the action suggested by the source, |
115 | the type we want for the drop, | |
b9aafad5 JD |
116 | the action we want for the drop, |
117 | any protocol specific data.") | |
133aad74 | 118 | |
b9aafad5 | 119 | (defvar x-dnd-empty-state [nil nil nil nil nil nil nil]) |
133aad74 JD |
120 | |
121 | ||
122 | ||
123 | (defun x-dnd-init-frame (&optional frame) | |
124 | "Setup drag and drop for FRAME (i.e. create appropriate properties)." | |
b9aafad5 JD |
125 | (x-dnd-init-xdnd-for-frame frame) |
126 | (x-dnd-init-motif-for-frame frame)) | |
133aad74 JD |
127 | |
128 | (defun x-dnd-get-state-cons-for-frame (frame-or-window) | |
129 | "Return the entry in x-dnd-current-state for a frame or window." | |
130 | (let* ((frame (if (framep frame-or-window) frame-or-window | |
131 | (window-frame frame-or-window))) | |
132 | (display (frame-parameter frame 'display))) | |
133 | (if (not (assoc display x-dnd-current-state)) | |
b9aafad5 JD |
134 | (push (cons display (copy-sequence x-dnd-empty-state)) |
135 | x-dnd-current-state)) | |
133aad74 JD |
136 | (assoc display x-dnd-current-state))) |
137 | ||
138 | (defun x-dnd-get-state-for-frame (frame-or-window) | |
139 | "Return the state in x-dnd-current-state for a frame or window." | |
140 | (cdr (x-dnd-get-state-cons-for-frame frame-or-window))) | |
141 | ||
142 | (defun x-dnd-default-test-function (window action types) | |
143 | "The default test function for drag and drop. | |
144 | WINDOW is where the mouse is when this function is called. It may be a frame | |
145 | if the mouse is over the menu bar, scroll bar or tool bar. | |
146 | ACTION is the suggested action from the source, and TYPES are the | |
147 | types the drop data can have. This function only accepts drops with | |
148 | types in `x-dnd-known-types'. It always returns the action private." | |
149 | (let ((type (x-dnd-choose-type types))) | |
150 | (when type (cons 'private type)))) | |
151 | ||
152 | ||
153 | (defun x-dnd-current-type (frame-or-window) | |
154 | "Return the type we want the DND data to be in for the current drop. | |
155 | FRAME-OR-WINDOW is the frame or window that the mouse is over." | |
156 | (aref (x-dnd-get-state-for-frame frame-or-window) 4)) | |
157 | ||
158 | (defun x-dnd-forget-drop (frame-or-window) | |
159 | "Remove all state for the last drop. | |
160 | FRAME-OR-WINDOW is the frame or window that the mouse is over." | |
b9aafad5 JD |
161 | (setcdr (x-dnd-get-state-cons-for-frame frame-or-window) |
162 | (copy-sequence x-dnd-empty-state))) | |
133aad74 JD |
163 | |
164 | (defun x-dnd-maybe-call-test-function (window action) | |
165 | "Call `x-dnd-test-function' if something has changed. | |
166 | WINDOW is the window the mouse is over. ACTION is the suggested | |
167 | action from the source. If nothing has changed, return the last | |
168 | action and type we got from `x-dnd-test-function'." | |
169 | (let ((buffer (when (and (windowp window) (window-live-p window)) | |
170 | (window-buffer window))) | |
171 | (current-state (x-dnd-get-state-for-frame window))) | |
172 | (when (or (not (equal buffer (aref current-state 0))) | |
173 | (not (equal window (aref current-state 1))) | |
174 | (not (equal action (aref current-state 3)))) | |
175 | (save-excursion | |
176 | (when buffer (set-buffer buffer)) | |
177 | (let* ((action-type (funcall x-dnd-test-function | |
178 | window | |
179 | action | |
180 | (aref current-state 2))) | |
181 | (handler (cdr (assoc (cdr action-type) x-dnd-types-alist)))) | |
182 | ;; Ignore action-type if we have no handler. | |
183 | (setq current-state | |
bf247b6e | 184 | (x-dnd-save-state window |
133aad74 JD |
185 | action |
186 | (when handler action-type))))))) | |
187 | (let ((current-state (x-dnd-get-state-for-frame window))) | |
188 | (cons (aref current-state 5) | |
189 | (aref current-state 4)))) | |
190 | ||
b9aafad5 | 191 | (defun x-dnd-save-state (window action action-type &optional types extra-data) |
133aad74 JD |
192 | "Save the state of the current drag and drop. |
193 | WINDOW is the window the mouse is over. ACTION is the action suggested | |
194 | by the source. ACTION-TYPE is the result of calling `x-dnd-test-function'. | |
b9aafad5 JD |
195 | If given, TYPES are the types for the drop data that the source supports. |
196 | EXTRA-DATA is data needed for a specific protocol." | |
133aad74 JD |
197 | (let ((current-state (x-dnd-get-state-for-frame window))) |
198 | (aset current-state 5 (car action-type)) | |
199 | (aset current-state 4 (cdr action-type)) | |
200 | (aset current-state 3 action) | |
b9aafad5 JD |
201 | (when types (aset current-state 2 types)) |
202 | (when extra-data (aset current-state 6 extra-data)) | |
133aad74 JD |
203 | (aset current-state 1 window) |
204 | (aset current-state 0 (if (and (windowp window) | |
205 | (window-live-p window)) | |
206 | (window-buffer window) nil)) | |
207 | (setcdr (x-dnd-get-state-cons-for-frame window) current-state))) | |
208 | ||
209 | ||
133aad74 JD |
210 | (defun x-dnd-handle-moz-url (window action data) |
211 | "Handle one item of type text/x-moz-url. | |
212 | WINDOW is the window where the drop happened. ACTION is ignored. | |
213 | DATA is the moz-url, which is formatted as two strings separated by \r\n. | |
214 | The first string is the URL, the second string is the title of that URL. | |
215 | DATA is encoded in utf-16. Decode the URL and call `x-dnd-handle-uri-list'." | |
bcfa9925 JD |
216 | ;; Mozilla and applications based on it (Galeon for example) uses |
217 | ;; text/unicode, but it is impossible to tell if it is le or be. Use what | |
218 | ;; the machine Emacs runs on use. This looses if dropping between machines | |
219 | ;; with different endian, but it is the best we can do. | |
220 | (let* ((coding (if (eq (byteorder) ?B) 'utf-16be 'utf-16le)) | |
221 | (string (decode-coding-string data coding)) | |
133aad74 JD |
222 | (strings (split-string string "[\r\n]" t)) |
223 | ;; Can one drop more than one moz-url ?? Assume not. | |
224 | (url (car strings)) | |
225 | (title (car (cdr strings)))) | |
226 | (x-dnd-handle-uri-list window action url))) | |
227 | ||
228 | (defun x-dnd-insert-utf8-text (window action text) | |
229 | "Decode the UTF-8 text and insert it at point. | |
230 | TEXT is the text as a string, WINDOW is the window where the drop happened." | |
67988557 | 231 | (dnd-insert-text window action (decode-coding-string text 'utf-8))) |
133aad74 JD |
232 | |
233 | (defun x-dnd-insert-utf16-text (window action text) | |
234 | "Decode the UTF-16 text and insert it at point. | |
235 | TEXT is the text as a string, WINDOW is the window where the drop happened." | |
bcfa9925 JD |
236 | ;; See comment in x-dnd-handle-moz-url about coding. |
237 | (let ((coding (if (eq (byteorder) ?B) 'utf-16be 'utf-16le))) | |
67988557 | 238 | (dnd-insert-text window action (decode-coding-string text coding)))) |
133aad74 | 239 | |
b9aafad5 JD |
240 | (defun x-dnd-insert-ctext (window action text) |
241 | "Decode the compound text and insert it at point. | |
242 | TEXT is the text as a string, WINDOW is the window where the drop happened." | |
67988557 JD |
243 | (dnd-insert-text window action |
244 | (decode-coding-string text | |
245 | 'compound-text-with-extensions))) | |
133aad74 JD |
246 | |
247 | (defun x-dnd-handle-uri-list (window action string) | |
67988557 | 248 | "Split an uri-list into separate URIs and call `dnd-handle-one-url'. |
133aad74 JD |
249 | WINDOW is the window where the drop happened. |
250 | STRING is the uri-list as a string. The URIs are separated by \r\n." | |
251 | (let ((uri-list (split-string string "[\0\r\n]" t)) | |
252 | retval) | |
253 | (dolist (bf uri-list) | |
254 | ;; If one URL is handeled, treat as if the whole drop succeeded. | |
67988557 | 255 | (let ((did-action (dnd-handle-one-url window action bf))) |
133aad74 JD |
256 | (when did-action (setq retval did-action)))) |
257 | retval)) | |
258 | ||
b9aafad5 | 259 | (defun x-dnd-handle-file-name (window action string) |
67988557 | 260 | "Prepend file:// to file names and call `dnd-handle-one-url'. |
b9aafad5 JD |
261 | WINDOW is the window where the drop happened. |
262 | STRING is the file names as a string, separated by nulls." | |
263 | (let ((uri-list (split-string string "[\0\r\n]" t)) | |
264 | retval) | |
265 | (dolist (bf uri-list) | |
266 | ;; If one URL is handeled, treat as if the whole drop succeeded. | |
267 | (let* ((file-uri (concat "file://" bf)) | |
67988557 | 268 | (did-action (dnd-handle-one-url window action file-uri))) |
b9aafad5 JD |
269 | (when did-action (setq retval did-action)))) |
270 | retval)) | |
271 | ||
133aad74 JD |
272 | |
273 | (defun x-dnd-choose-type (types &optional known-types) | |
274 | "Choose which type we want to receive for the drop. | |
275 | TYPES are the types the source of the drop offers, a vector of type names | |
276 | as strings or symbols. Select among the types in `x-dnd-known-types' or | |
277 | KNOWN-TYPES if given, and return that type name. | |
278 | If no suitable type is found, return nil." | |
279 | (let* ((known-list (or known-types x-dnd-known-types)) | |
280 | (first-known-type (car known-list)) | |
281 | (types-array types) | |
282 | (found (when first-known-type | |
283 | (catch 'done | |
284 | (dotimes (i (length types-array)) | |
285 | (let* ((type (aref types-array i)) | |
286 | (typename (if (symbolp type) | |
287 | (symbol-name type) type))) | |
288 | (when (equal first-known-type typename) | |
289 | (throw 'done first-known-type)))) | |
290 | nil)))) | |
291 | ||
292 | (if (and (not found) (cdr known-list)) | |
293 | (x-dnd-choose-type types (cdr known-list)) | |
294 | found))) | |
295 | ||
296 | (defun x-dnd-drop-data (event frame window data type) | |
297 | "Drop one data item onto a frame. | |
298 | EVENT is the client message for the drop, FRAME is the frame the drop occurred | |
299 | on. WINDOW is the window of FRAME where the drop happened. DATA is the data | |
300 | received from the source, and type is the type for DATA, see | |
301 | `x-dnd-types-alist'). | |
302 | ||
303 | Returns the action used (move, copy, link, private) if drop was successful, | |
304 | nil if not." | |
305 | (let* ((type-info (assoc type x-dnd-types-alist)) | |
306 | (handler (cdr type-info)) | |
307 | (state (x-dnd-get-state-for-frame frame)) | |
308 | (action (aref state 5)) | |
309 | (w (posn-window (event-start event)))) | |
310 | (when handler | |
311 | (if (and (windowp w) (window-live-p w)) | |
312 | ;; If dropping in a window, open files in that window rather | |
313 | ;; than in a new widow. | |
67988557 | 314 | (let ((dnd-open-file-other-window nil)) |
133aad74 JD |
315 | (goto-char (posn-point (event-start event))) |
316 | (funcall handler window action data)) | |
67988557 | 317 | (let ((dnd-open-file-other-window t)) ;; Dropping on non-window. |
133aad74 JD |
318 | (select-frame frame) |
319 | (funcall handler window action data)))))) | |
320 | ||
321 | (defun x-dnd-handle-drag-n-drop-event (event) | |
322 | "Receive drag and drop events (X client messages). | |
49f87d23 | 323 | Currently XDND, Motif and old KDE 1.x protocols are recognized." |
133aad74 JD |
324 | (interactive "e") |
325 | (let* ((client-message (car (cdr (cdr event)))) | |
326 | (window (posn-window (event-start event))) | |
327 | (message-atom (aref client-message 0)) | |
328 | (frame (aref client-message 1)) | |
329 | (format (aref client-message 2)) | |
330 | (data (aref client-message 3))) | |
331 | ||
b9aafad5 | 332 | (cond ((equal "DndProtocol" message-atom) ; Old KDE 1.x. |
133aad74 JD |
333 | (x-dnd-handle-old-kde event frame window message-atom format data)) |
334 | ||
b9aafad5 JD |
335 | ((equal "_MOTIF_DRAG_AND_DROP_MESSAGE" message-atom) ; Motif |
336 | (x-dnd-handle-motif event frame window message-atom format data)) | |
337 | ||
338 | ((and (> (length message-atom) 4) ; XDND protocol. | |
133aad74 | 339 | (equal "Xdnd" (substring message-atom 0 4))) |
b9aafad5 | 340 | (x-dnd-handle-xdnd event frame window message-atom format data))))) |
133aad74 | 341 | |
133aad74 JD |
342 | |
343 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
344 | ;;; Old KDE protocol. Only dropping of files. | |
345 | ||
346 | (defun x-dnd-handle-old-kde (event frame window message format data) | |
347 | "Open the files in a KDE 1.x drop." | |
348 | (let ((values (x-window-property "DndSelection" frame nil 0 t))) | |
349 | (x-dnd-handle-uri-list window 'private | |
350 | (replace-regexp-in-string "\0$" "" values)))) | |
351 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
352 | ||
353 | ||
354 | ||
355 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
356 | ;;; XDND protocol. | |
357 | ||
358 | (defvar x-dnd-xdnd-to-action | |
359 | '(("XdndActionPrivate" . private) | |
360 | ("XdndActionCopy" . copy) | |
361 | ("XdndActionMove" . move) | |
362 | ("XdndActionLink" . link) | |
363 | ("XdndActionAsk" . ask)) | |
364 | "Mapping from XDND action types to lisp symbols.") | |
365 | ||
366 | (defun x-dnd-init-xdnd-for-frame (frame) | |
b9aafad5 | 367 | "Set the XdndAware property for FRAME to indicate that we do XDND." |
133aad74 JD |
368 | (x-change-window-property "XdndAware" |
369 | '(5) ;; The version of XDND we support. | |
370 | frame "ATOM" 32 t)) | |
371 | ||
372 | (defun x-dnd-get-drop-width-height (frame w accept) | |
373 | "Return the widht/height to be sent in a XDndStatus message. | |
374 | FRAME is the frame and W is the window where the drop happened. | |
375 | If ACCEPT is nil return 0 (empty rectangle), | |
376 | otherwise if W is a window, return its widht/height, | |
377 | otherwise return the frame width/height." | |
378 | (if accept | |
379 | (if (windowp w) ;; w is not a window if dropping on the menu bar, | |
380 | ;; scroll bar or tool bar. | |
381 | (let ((edges (window-inside-pixel-edges w))) | |
382 | (cons | |
383 | (- (nth 2 edges) (nth 0 edges)) ;; right - left | |
384 | (- (nth 3 edges) (nth 1 edges)))) ;; bottom - top | |
385 | (cons (frame-pixel-width frame) | |
386 | (frame-pixel-height frame))) | |
387 | 0)) | |
388 | ||
389 | (defun x-dnd-get-drop-x-y (frame w) | |
390 | "Return the x/y coordinates to be sent in a XDndStatus message. | |
391 | Coordinates are required to be absolute. | |
392 | FRAME is the frame and W is the window where the drop happened. | |
393 | If W is a window, return its absolute corrdinates, | |
394 | otherwise return the frame coordinates." | |
395 | (let* ((frame-left (frame-parameter frame 'left)) | |
396 | ;; If the frame is outside the display, frame-left looks like | |
397 | ;; '(0 -16). Extract the -16. | |
398 | (frame-real-left (if (consp frame-left) (car (cdr frame-left)) | |
399 | frame-left)) | |
400 | (frame-top (frame-parameter frame 'top)) | |
401 | (frame-real-top (if (consp frame-top) (car (cdr frame-top)) | |
402 | frame-top))) | |
403 | (if (windowp w) | |
404 | (let ((edges (window-inside-pixel-edges w))) | |
405 | (cons | |
406 | (+ frame-real-left (nth 0 edges)) | |
407 | (+ frame-real-top (nth 1 edges)))) | |
408 | (cons frame-real-left frame-real-top)))) | |
409 | ||
410 | (defun x-dnd-handle-xdnd (event frame window message format data) | |
411 | "Receive one XDND event (client message) and send the appropriate reply. | |
412 | EVENT is the client message. FRAME is where the mouse is now. | |
413 | WINDOW is the window within FRAME where the mouse is now. | |
414 | FORMAT is 32 (not used). MESSAGE is the data part of an XClientMessageEvent." | |
415 | (cond ((equal "XdndEnter" message) | |
18daafed JD |
416 | (let* ((flags (aref data 1)) |
417 | (version (and (consp flags) (ash (car flags) -8))) | |
418 | (more-than-3 (and (consp flags) (cdr flags))) | |
419 | (dnd-source (aref data 0))) | |
420 | (if version ;; If flags is bad, version will be nil. | |
421 | (x-dnd-save-state | |
422 | window nil nil | |
423 | (if (> more-than-3 0) | |
424 | (x-window-property "XdndTypeList" | |
425 | frame "AnyPropertyType" | |
426 | dnd-source nil t) | |
427 | (vector (x-get-atom-name (aref data 2)) | |
428 | (x-get-atom-name (aref data 3)) | |
429 | (x-get-atom-name (aref data 4)))))))) | |
133aad74 JD |
430 | |
431 | ((equal "XdndPosition" message) | |
432 | (let* ((x (car (aref data 2))) | |
433 | (y (cdr (aref data 2))) | |
434 | (action (x-get-atom-name (aref data 4))) | |
435 | (dnd-source (aref data 0)) | |
436 | (dnd-time (aref data 3)) | |
437 | (action-type (x-dnd-maybe-call-test-function | |
438 | window | |
439 | (cdr (assoc action x-dnd-xdnd-to-action)))) | |
440 | (reply-action (car (rassoc (car action-type) | |
441 | x-dnd-xdnd-to-action))) | |
442 | (accept ;; 1 = accept, 0 = reject | |
443 | (if (and reply-action action-type) 1 0)) | |
444 | (list-to-send | |
445 | (list (string-to-number | |
446 | (frame-parameter frame 'outer-window-id)) | |
447 | accept ;; 1 = Accept, 0 = reject. | |
448 | (x-dnd-get-drop-x-y frame window) | |
bf247b6e | 449 | (x-dnd-get-drop-width-height |
133aad74 JD |
450 | frame window (eq accept 1)) |
451 | (or reply-action 0) | |
452 | ))) | |
453 | (x-send-client-message | |
454 | frame dnd-source frame "XdndStatus" 32 list-to-send) | |
455 | )) | |
456 | ||
457 | ((equal "XdndLeave" message) | |
458 | (x-dnd-forget-drop window)) | |
459 | ||
460 | ((equal "XdndDrop" message) | |
461 | (if (windowp window) (select-window window)) | |
462 | (let* ((dnd-source (aref data 0)) | |
463 | (value (and (x-dnd-current-type window) | |
133aad74 JD |
464 | (x-get-selection-internal |
465 | 'XdndSelection | |
466 | (intern (x-dnd-current-type window))))) | |
467 | success action ret-action) | |
468 | ||
469 | (setq action (if value | |
470 | (condition-case info | |
bf247b6e | 471 | (x-dnd-drop-data event frame window value |
133aad74 | 472 | (x-dnd-current-type window)) |
bf247b6e | 473 | (error |
133aad74 JD |
474 | (message "Error: %s" info) |
475 | nil)))) | |
476 | ||
477 | (setq success (if action 1 0)) | |
478 | (setq ret-action | |
479 | (if (eq success 1) | |
480 | (or (car (rassoc action x-dnd-xdnd-to-action)) | |
481 | "XdndActionPrivate") | |
482 | 0)) | |
483 | ||
484 | (x-send-client-message | |
485 | frame dnd-source frame "XdndFinished" 32 | |
486 | (list (string-to-number (frame-parameter frame 'outer-window-id)) | |
487 | success ;; 1 = Success, 0 = Error | |
488 | (if success "XdndActionPrivate" 0) | |
489 | )) | |
490 | (x-dnd-forget-drop window))) | |
491 | ||
492 | (t (error "Unknown XDND message %s %s" message data)))) | |
493 | ||
b9aafad5 JD |
494 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
495 | ;;; Motif protocol. | |
496 | ||
497 | (defun x-dnd-init-motif-for-frame (frame) | |
498 | "Set _MOTIF_DRAG_RECEIVER_INFO for FRAME to indicate that we do Motif DND." | |
499 | (x-change-window-property "_MOTIF_DRAG_RECEIVER_INFO" | |
500 | (list | |
501 | (byteorder) | |
502 | 0 ; The Motif DND version. | |
503 | 5 ; We want drag dynamic. | |
504 | 0 0 0 0 0 0 0 | |
505 | 0 0 0 0 0 0) ; Property must be 16 bytes. | |
506 | frame "_MOTIF_DRAG_RECEIVER_INFO" 8 t)) | |
507 | ||
508 | (defun x-dnd-get-motif-value (data offset size byteorder) | |
509 | (cond ((eq size 2) | |
510 | (if (eq byteorder ?l) | |
511 | (+ (ash (aref data (1+ offset)) 8) | |
512 | (aref data offset)) | |
513 | (+ (ash (aref data offset) 8) | |
514 | (aref data (1+ offset))))) | |
515 | ||
516 | ((eq size 4) | |
517 | (if (eq byteorder ?l) | |
518 | (cons (+ (ash (aref data (+ 3 offset)) 8) | |
519 | (aref data (+ 2 offset))) | |
520 | (+ (ash (aref data (1+ offset)) 8) | |
521 | (aref data offset))) | |
522 | (cons (+ (ash (aref data offset) 8) | |
523 | (aref data (1+ offset))) | |
524 | (+ (ash (aref data (+ 2 offset)) 8) | |
525 | (aref data (+ 3 offset)))))))) | |
526 | ||
527 | (defun x-dnd-motif-value-to-list (value size byteorder) | |
528 | (let ((bytes (cond ((eq size 2) | |
529 | (list (logand (lsh value -8) ?\xff) | |
530 | (logand value ?\xff))) | |
531 | ||
532 | ((eq size 4) | |
533 | (if (consp value) | |
534 | (list (logand (lsh (car value) -8) ?\xff) | |
535 | (logand (car value) ?\xff) | |
536 | (logand (lsh (cdr value) -8) ?\xff) | |
537 | (logand (cdr value) ?\xff)) | |
538 | (list (logand (lsh value -24) ?\xff) | |
539 | (logand (lsh value -16) ?\xff) | |
540 | (logand (lsh value -8) ?\xff) | |
541 | (logand value ?\xff))))))) | |
542 | (if (eq byteorder ?l) | |
543 | (reverse bytes) | |
544 | bytes))) | |
545 | ||
546 | ||
547 | (defvar x-dnd-motif-message-types | |
548 | '((0 . XmTOP_LEVEL_ENTER) | |
549 | (1 . XmTOP_LEVEL_LEAVE) | |
550 | (2 . XmDRAG_MOTION) | |
551 | (3 . XmDROP_SITE_ENTER) | |
552 | (4 . XmDROP_SITE_LEAVE) | |
553 | (5 . XmDROP_START) | |
554 | (6 . XmDROP_FINISH) | |
555 | (7 . XmDRAG_DROP_FINISH) | |
556 | (8 . XmOPERATION_CHANGED)) | |
557 | "Mapping from numbers to Motif DND message types.") | |
558 | ||
559 | (defvar x-dnd-motif-to-action | |
560 | '((1 . move) | |
561 | (2 . copy) | |
562 | (3 . link) ; Both 3 and 4 has been seen as link. | |
563 | (4 . link) | |
564 | (2 . private)) ; Motif does not have private, so use copy for private. | |
565 | "Mapping from number to operation for Motif DND.") | |
566 | ||
567 | (defun x-dnd-handle-motif (event frame window message-atom format data) | |
568 | (let* ((message-type (cdr (assoc (aref data 0) x-dnd-motif-message-types))) | |
569 | (source-byteorder (aref data 1)) | |
570 | (my-byteorder (byteorder)) | |
571 | (source-flags (x-dnd-get-motif-value data 2 2 source-byteorder)) | |
572 | (source-action (cdr (assoc (logand ?\xF source-flags) | |
573 | x-dnd-motif-to-action)))) | |
574 | ||
575 | (cond ((eq message-type 'XmTOP_LEVEL_ENTER) | |
576 | (let* ((dnd-source (x-dnd-get-motif-value | |
577 | data 8 4 source-byteorder)) | |
578 | (selection-atom (x-dnd-get-motif-value | |
579 | data 12 4 source-byteorder)) | |
580 | (atom-name (x-get-atom-name selection-atom)) | |
581 | (types (when atom-name | |
582 | (x-get-selection-internal (intern atom-name) | |
583 | 'TARGETS)))) | |
584 | (x-dnd-forget-drop frame) | |
585 | (when types (x-dnd-save-state window nil nil | |
586 | types | |
587 | dnd-source)))) | |
588 | ||
589 | ;; Can not forget drop here, LEAVE comes before DROP_START and | |
590 | ;; we need the state in DROP_START. | |
591 | ((eq message-type 'XmTOP_LEVEL_LEAVE) | |
592 | nil) | |
593 | ||
594 | ((eq message-type 'XmDRAG_MOTION) | |
595 | (let* ((state (x-dnd-get-state-for-frame frame)) | |
596 | (timestamp (x-dnd-motif-value-to-list | |
bf247b6e | 597 | (x-dnd-get-motif-value data 4 4 |
b9aafad5 JD |
598 | source-byteorder) |
599 | 4 my-byteorder)) | |
600 | (x (x-dnd-motif-value-to-list | |
601 | (x-dnd-get-motif-value data 8 2 source-byteorder) | |
602 | 2 my-byteorder)) | |
603 | (y (x-dnd-motif-value-to-list | |
604 | (x-dnd-get-motif-value data 10 2 source-byteorder) | |
605 | 2 my-byteorder)) | |
606 | (dnd-source (aref state 6)) | |
607 | (first-move (not (aref state 3))) | |
608 | (action-type (x-dnd-maybe-call-test-function | |
609 | window | |
610 | source-action)) | |
611 | (reply-action (car (rassoc (car action-type) | |
612 | x-dnd-motif-to-action))) | |
613 | (reply-flags | |
614 | (x-dnd-motif-value-to-list | |
615 | (if reply-action | |
bf247b6e | 616 | (+ reply-action |
b9aafad5 JD |
617 | ?\x30 ; 30: valid drop site |
618 | ?\x700) ; 700: can do copy, move or link | |
619 | ?\x30) ; 30: drop site, but noop. | |
620 | 2 my-byteorder)) | |
621 | (reply (append | |
622 | (list | |
623 | (+ ?\x80 ; 0x80 indicates a reply. | |
624 | (if first-move | |
625 | 3 ; First time, reply is SITE_ENTER. | |
626 | 2)) ; Not first time, reply is DRAG_MOTION. | |
627 | my-byteorder) | |
628 | reply-flags | |
629 | timestamp | |
630 | x | |
631 | y))) | |
632 | (x-send-client-message frame | |
633 | dnd-source | |
634 | frame | |
635 | "_MOTIF_DRAG_AND_DROP_MESSAGE" | |
636 | 8 | |
637 | reply))) | |
638 | ||
639 | ((eq message-type 'XmOPERATION_CHANGED) | |
640 | (let* ((state (x-dnd-get-state-for-frame frame)) | |
641 | (timestamp (x-dnd-motif-value-to-list | |
642 | (x-dnd-get-motif-value data 4 4 source-byteorder) | |
643 | 4 my-byteorder)) | |
644 | (dnd-source (aref state 6)) | |
645 | (action-type (x-dnd-maybe-call-test-function | |
646 | window | |
647 | source-action)) | |
648 | (reply-action (car (rassoc (car action-type) | |
649 | x-dnd-motif-to-action))) | |
650 | (reply-flags | |
651 | (x-dnd-motif-value-to-list | |
652 | (if reply-action | |
bf247b6e | 653 | (+ reply-action |
b9aafad5 JD |
654 | ?\x30 ; 30: valid drop site |
655 | ?\x700) ; 700: can do copy, move or link | |
656 | ?\x30) ; 30: drop site, but noop | |
657 | 2 my-byteorder)) | |
658 | (reply (append | |
659 | (list | |
660 | (+ ?\x80 ; 0x80 indicates a reply. | |
661 | 8) ; 8 is OPERATION_CHANGED | |
662 | my-byteorder) | |
663 | reply-flags | |
664 | timestamp))) | |
665 | (x-send-client-message frame | |
666 | dnd-source | |
667 | frame | |
668 | "_MOTIF_DRAG_AND_DROP_MESSAGE" | |
669 | 8 | |
670 | reply))) | |
671 | ||
672 | ((eq message-type 'XmDROP_START) | |
673 | (let* ((x (x-dnd-motif-value-to-list | |
674 | (x-dnd-get-motif-value data 8 2 source-byteorder) | |
675 | 2 my-byteorder)) | |
676 | (y (x-dnd-motif-value-to-list | |
677 | (x-dnd-get-motif-value data 10 2 source-byteorder) | |
678 | 2 my-byteorder)) | |
679 | (selection-atom (x-dnd-get-motif-value | |
680 | data 12 4 source-byteorder)) | |
681 | (atom-name (x-get-atom-name selection-atom)) | |
682 | (dnd-source (x-dnd-get-motif-value | |
683 | data 16 4 source-byteorder)) | |
684 | (action-type (x-dnd-maybe-call-test-function | |
685 | window | |
686 | source-action)) | |
687 | (reply-action (car (rassoc (car action-type) | |
688 | x-dnd-motif-to-action))) | |
689 | (reply-flags | |
690 | (x-dnd-motif-value-to-list | |
691 | (if reply-action | |
bf247b6e | 692 | (+ reply-action |
b9aafad5 JD |
693 | ?\x30 ; 30: valid drop site |
694 | ?\x700) ; 700: can do copy, move or link | |
695 | (+ ?\x30 ; 30: drop site, but noop. | |
696 | ?\x200)) ; 200: drop cancel. | |
697 | 2 my-byteorder)) | |
698 | (reply (append | |
699 | (list | |
700 | (+ ?\x80 ; 0x80 indicates a reply. | |
701 | 5) ; DROP_START. | |
702 | my-byteorder) | |
703 | reply-flags | |
704 | x | |
705 | y)) | |
bf247b6e | 706 | (timestamp (x-dnd-get-motif-value |
b9aafad5 JD |
707 | data 4 4 source-byteorder)) |
708 | action) | |
709 | ||
710 | (x-send-client-message frame | |
711 | dnd-source | |
712 | frame | |
713 | "_MOTIF_DRAG_AND_DROP_MESSAGE" | |
714 | 8 | |
715 | reply) | |
bf247b6e | 716 | (setq action |
b9aafad5 JD |
717 | (when (and reply-action atom-name) |
718 | (let* ((value (x-get-selection-internal | |
719 | (intern atom-name) | |
720 | (intern (x-dnd-current-type window))))) | |
721 | (when value | |
722 | (condition-case info | |
bf247b6e | 723 | (x-dnd-drop-data event frame window value |
b9aafad5 JD |
724 | (x-dnd-current-type window)) |
725 | (error | |
726 | (message "Error: %s" info) | |
727 | nil)))))) | |
728 | (x-get-selection-internal | |
bf247b6e | 729 | (intern atom-name) |
b9aafad5 JD |
730 | (if action 'XmTRANSFER_SUCCESS 'XmTRANSFER_FAILURE) |
731 | timestamp) | |
732 | (x-dnd-forget-drop frame))) | |
733 | ||
7a01b040 | 734 | (t (error "Unknown Motif DND message %s %s" message-atom data))))) |
bf247b6e | 735 | |
b9aafad5 JD |
736 | |
737 | ;;; | |
738 | ||
133aad74 JD |
739 | (provide 'x-dnd) |
740 | ||
95e224b7 | 741 | ;;; arch-tag: b621fb7e-50da-4323-850b-5fc71ae64621 |
133aad74 | 742 | ;;; x-dnd.el ends here |