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