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