From Ulf Jasper <ulf.jasper@web.de>:
[emacs.git] / lisp / x-dnd.el
blobef7cc1ccc734275cceaafd6b22894a7866a13dcc
1 ;;; x-dnd.el --- drag and drop support for X.
3 ;; Copyright (C) 2004
4 ;; Free Software Foundation, Inc.
6 ;; Author: Jan Dj\e,Ad\e(Brv <jan.h.d@swipnet.se>
7 ;; Maintainer: FSF
8 ;; Keywords: window, drag, drop
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
29 ;; This file provides the drop part only. Currently supported protocols
30 ;; are XDND, Motif and the old KDE 1.x protocol.
32 ;;; Code:
34 ;;; Customizable variables
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.
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'."
50 :version "22.1"
51 :type 'symbol
52 :group 'x)
54 (defcustom x-dnd-protocol-alist
56 ("^file:///" . x-dnd-open-local-file) ; XDND format.
57 ("^file://" . x-dnd-open-file) ; URL with host
58 ("^file:" . x-dnd-open-local-file) ; Old KDE, Motif, Sun
61 "The functions to call for different protocols when a drop is made.
62 This variable is used by `x-dnd-handle-uri-list', `x-dnd-handle-file-name'
63 and `x-dnd-handle-moz-url'. The list contains of (REGEXP . FUNCTION) pairs.
64 The functions shall take two arguments, URL, which is the URL dropped and
65 ACTION which is the action to be performed for the drop (move, copy, link,
66 private or ask).
67 If no match is found here, and the value of `browse-url-browser-function'
68 is a pair of (REGEXP . FUNCTION), those regexps are tried for a match.
69 Insertion of text is not handeled by these functions, see `x-dnd-types-alist'
70 for that.
71 The function shall return the action done (move, copy, link or private)
72 if some action was made, or nil if the URL is ignored."
73 :version "22.1"
74 :type 'alist
75 :group 'x)
78 (defcustom x-dnd-types-alist
80 ("text/uri-list" . x-dnd-handle-uri-list)
81 ("text/x-moz-url" . x-dnd-handle-moz-url)
82 ("_NETSCAPE_URL" . x-dnd-handle-uri-list)
83 ("FILE_NAME" . x-dnd-handle-file-name)
84 ("UTF8_STRING" . x-dnd-insert-utf8-text)
85 ("text/plain;charset=UTF-8" . x-dnd-insert-utf8-text)
86 ("text/plain;charset=utf-8" . x-dnd-insert-utf8-text)
87 ("text/unicode" . x-dnd-insert-utf16-text)
88 ("text/plain" . x-dnd-insert-text)
89 ("COMPOUND_TEXT" . x-dnd-insert-ctext)
90 ("STRING" . x-dnd-insert-text)
91 ("TEXT" . x-dnd-insert-text)
93 "Which function to call to handle a drop of that type.
94 If the type for the drop is not present, or the function is nil,
95 the drop is rejected. The function takes three arguments, WINDOW, ACTION
96 and DATA. WINDOW is where the drop occured, ACTION is the action for
97 this drop (copy, move, link, private or ask) as determined by a previous
98 call to `x-dnd-test-function'. DATA is the drop data.
99 The function shall return the action used (copy, move, link or private) if drop
100 is successful, nil if not."
101 :version "22.1"
102 :type 'alist
103 :group 'x)
105 (defcustom x-dnd-open-file-other-window nil
106 "If non-nil, always use find-file-other-window to open dropped files."
107 :version "22.1"
108 :type 'boolean
109 :group 'x)
111 (defcustom x-dnd-known-types
112 '("text/uri-list"
113 "text/x-moz-url"
114 "_NETSCAPE_URL"
115 "FILE_NAME"
116 "UTF8_STRING"
117 "text/plain;charset=UTF-8"
118 "text/plain;charset=utf-8"
119 "text/unicode"
120 "text/plain"
121 "COMPOUND_TEXT"
122 "STRING"
123 "TEXT"
125 "The types accepted by default for dropped data.
126 The types are chosen in the order they appear in the list."
127 :version "22.1"
128 :type '(repeat string)
129 :group 'x
132 ;; Internal variables
134 (defvar x-dnd-current-state nil
135 "The current state for a drop.
136 This is an alist with one entry for each display. The value for each display
137 is a vector that contains the state for drag and drop for that display.
138 Elements in the vector are:
139 Last buffer drag was in,
140 last window drag was in,
141 types available for drop,
142 the action suggested by the source,
143 the type we want for the drop,
144 the action we want for the drop,
145 any protocol specific data.")
147 (defvar x-dnd-empty-state [nil nil nil nil nil nil nil])
151 (defun x-dnd-init-frame (&optional frame)
152 "Setup drag and drop for FRAME (i.e. create appropriate properties)."
153 (x-dnd-init-xdnd-for-frame frame)
154 (x-dnd-init-motif-for-frame frame))
156 (defun x-dnd-get-state-cons-for-frame (frame-or-window)
157 "Return the entry in x-dnd-current-state for a frame or window."
158 (let* ((frame (if (framep frame-or-window) frame-or-window
159 (window-frame frame-or-window)))
160 (display (frame-parameter frame 'display)))
161 (if (not (assoc display x-dnd-current-state))
162 (push (cons display (copy-sequence x-dnd-empty-state))
163 x-dnd-current-state))
164 (assoc display x-dnd-current-state)))
166 (defun x-dnd-get-state-for-frame (frame-or-window)
167 "Return the state in x-dnd-current-state for a frame or window."
168 (cdr (x-dnd-get-state-cons-for-frame frame-or-window)))
170 (defun x-dnd-default-test-function (window action types)
171 "The default test function for drag and drop.
172 WINDOW is where the mouse is when this function is called. It may be a frame
173 if the mouse is over the menu bar, scroll bar or tool bar.
174 ACTION is the suggested action from the source, and TYPES are the
175 types the drop data can have. This function only accepts drops with
176 types in `x-dnd-known-types'. It always returns the action private."
177 (let ((type (x-dnd-choose-type types)))
178 (when type (cons 'private type))))
181 (defun x-dnd-current-type (frame-or-window)
182 "Return the type we want the DND data to be in for the current drop.
183 FRAME-OR-WINDOW is the frame or window that the mouse is over."
184 (aref (x-dnd-get-state-for-frame frame-or-window) 4))
186 (defun x-dnd-forget-drop (frame-or-window)
187 "Remove all state for the last drop.
188 FRAME-OR-WINDOW is the frame or window that the mouse is over."
189 (setcdr (x-dnd-get-state-cons-for-frame frame-or-window)
190 (copy-sequence x-dnd-empty-state)))
192 (defun x-dnd-maybe-call-test-function (window action)
193 "Call `x-dnd-test-function' if something has changed.
194 WINDOW is the window the mouse is over. ACTION is the suggested
195 action from the source. If nothing has changed, return the last
196 action and type we got from `x-dnd-test-function'."
197 (let ((buffer (when (and (windowp window) (window-live-p window))
198 (window-buffer window)))
199 (current-state (x-dnd-get-state-for-frame window)))
200 (when (or (not (equal buffer (aref current-state 0)))
201 (not (equal window (aref current-state 1)))
202 (not (equal action (aref current-state 3))))
203 (save-excursion
204 (when buffer (set-buffer buffer))
205 (let* ((action-type (funcall x-dnd-test-function
206 window
207 action
208 (aref current-state 2)))
209 (handler (cdr (assoc (cdr action-type) x-dnd-types-alist))))
210 ;; Ignore action-type if we have no handler.
211 (setq current-state
212 (x-dnd-save-state window
213 action
214 (when handler action-type)))))))
215 (let ((current-state (x-dnd-get-state-for-frame window)))
216 (cons (aref current-state 5)
217 (aref current-state 4))))
219 (defun x-dnd-save-state (window action action-type &optional types extra-data)
220 "Save the state of the current drag and drop.
221 WINDOW is the window the mouse is over. ACTION is the action suggested
222 by the source. ACTION-TYPE is the result of calling `x-dnd-test-function'.
223 If given, TYPES are the types for the drop data that the source supports.
224 EXTRA-DATA is data needed for a specific protocol."
225 (let ((current-state (x-dnd-get-state-for-frame window)))
226 (aset current-state 5 (car action-type))
227 (aset current-state 4 (cdr action-type))
228 (aset current-state 3 action)
229 (when types (aset current-state 2 types))
230 (when extra-data (aset current-state 6 extra-data))
231 (aset current-state 1 window)
232 (aset current-state 0 (if (and (windowp window)
233 (window-live-p window))
234 (window-buffer window) nil))
235 (setcdr (x-dnd-get-state-cons-for-frame window) current-state)))
238 (defun x-dnd-handle-one-url (window action arg)
239 "Handle one dropped url by calling the appropriate handler.
240 The handler is first localted by looking at `x-dnd-protocol-alist'.
241 If no match is found here, and the value of `browse-url-browser-function'
242 is a pair of (REGEXP . FUNCTION), those regexps are tried for a match.
243 If no match is found, just call `x-dnd-insert-text'.
244 WINDOW is where the drop happend, ACTION is the action for the drop,
245 ARG is the URL that has been dropped.
246 Returns ACTION."
247 (require 'browse-url)
248 (let* ((uri (replace-regexp-in-string
249 "%[A-Z0-9][A-Z0-9]"
250 (lambda (arg)
251 (format "%c" (string-to-number (substring arg 1) 16)))
252 arg))
253 ret)
255 (catch 'done
256 (dolist (bf x-dnd-protocol-alist)
257 (when (string-match (car bf) uri)
258 (setq ret (funcall (cdr bf) uri action))
259 (throw 'done t)))
260 nil)
261 (when (not (functionp browse-url-browser-function))
262 (catch 'done
263 (dolist (bf browse-url-browser-function)
264 (when (string-match (car bf) uri)
265 (setq ret 'private)
266 (funcall (cdr bf) uri action)
267 (throw 'done t)))
268 nil))
269 (progn
270 (x-dnd-insert-text window action uri)
271 (setq ret 'private)))
272 ret))
275 (defun x-dnd-get-local-file-uri (uri)
276 "Return an uri converted to file:/// syntax if uri is a local file.
277 Return nil if URI is not a local file."
279 ;; The hostname may be our hostname, in that case, convert to a local
280 ;; file. Otherwise return nil. TODO: How about an IP-address as hostname?
281 (let ((hostname (when (string-match "^file://\\([^/]*\\)" uri)
282 (downcase (match-string 1 uri))))
283 (system-name-no-dot
284 (downcase (if (string-match "^[^\\.]+" system-name)
285 (match-string 0 system-name)
286 system-name))))
287 (when (and hostname
288 (or (string-equal "localhost" hostname)
289 (string-equal (downcase system-name) hostname)
290 (string-equal system-name-no-dot hostname)))
291 (concat "file://" (substring uri (+ 7 (length hostname)))))))
293 (defun x-dnd-get-local-file-name (uri &optional must-exist)
294 "Return file name converted from file:/// or file: syntax.
295 URI is the uri for the file. If MUST-EXIST is given and non-nil,
296 only return non-nil if the file exists.
297 Return nil if URI is not a local file."
298 (let ((f (cond ((string-match "^file:///" uri) ; XDND format.
299 (substring uri (1- (match-end 0))))
300 ((string-match "^file:" uri) ; Old KDE, Motif, Sun
301 (substring uri (match-end 0))))))
302 (when (and f must-exist)
303 (let* ((decoded-f (decode-coding-string
305 (or file-name-coding-system
306 default-file-name-coding-system)))
307 (try-f (if (file-readable-p decoded-f) decoded-f f)))
308 (when (file-readable-p try-f) try-f)))))
311 (defun x-dnd-open-local-file (uri action)
312 "Open a local file.
313 The file is opened in the current window, or a new window if
314 `x-dnd-open-file-other-window' is set. URI is the url for the file,
315 and must have the format file:file-name or file:///file-name.
316 The last / in file:/// is part of the file name. ACTION is ignored."
318 (let* ((f (x-dnd-get-local-file-name uri t)))
319 (if (and f (file-readable-p f))
320 (progn
321 (if x-dnd-open-file-other-window
322 (find-file-other-window f)
323 (find-file f))
324 'private)
325 (error "Can not read %s" uri))))
327 (defun x-dnd-open-file (uri action)
328 "Open a local or remote file.
329 The file is opened in the current window, or a new window if
330 `x-dnd-open-file-other-window' is set. URI is the url for the file,
331 and must have the format file://hostname/file-name. ACTION is ignored.
332 The last / in file://hostname/ is part of the file name."
334 ;; The hostname may be our hostname, in that case, convert to a local
335 ;; file. Otherwise return nil.
336 (let ((local-file (x-dnd-get-local-file-uri uri)))
337 (if local-file (x-dnd-open-local-file local-file action)
338 (error "Remote files not supported"))))
341 (defun x-dnd-handle-moz-url (window action data)
342 "Handle one item of type text/x-moz-url.
343 WINDOW is the window where the drop happened. ACTION is ignored.
344 DATA is the moz-url, which is formatted as two strings separated by \r\n.
345 The first string is the URL, the second string is the title of that URL.
346 DATA is encoded in utf-16. Decode the URL and call `x-dnd-handle-uri-list'."
347 ;; Mozilla and applications based on it (Galeon for example) uses
348 ;; text/unicode, but it is impossible to tell if it is le or be. Use what
349 ;; the machine Emacs runs on use. This looses if dropping between machines
350 ;; with different endian, but it is the best we can do.
351 (let* ((coding (if (eq (byteorder) ?B) 'utf-16be 'utf-16le))
352 (string (decode-coding-string data coding))
353 (strings (split-string string "[\r\n]" t))
354 ;; Can one drop more than one moz-url ?? Assume not.
355 (url (car strings))
356 (title (car (cdr strings))))
357 (x-dnd-handle-uri-list window action url)))
359 (defun x-dnd-insert-utf8-text (window action text)
360 "Decode the UTF-8 text and insert it at point.
361 TEXT is the text as a string, WINDOW is the window where the drop happened."
362 (x-dnd-insert-text window action (decode-coding-string text 'utf-8)))
364 (defun x-dnd-insert-utf16-text (window action text)
365 "Decode the UTF-16 text and insert it at point.
366 TEXT is the text as a string, WINDOW is the window where the drop happened."
367 ;; See comment in x-dnd-handle-moz-url about coding.
368 (let ((coding (if (eq (byteorder) ?B) 'utf-16be 'utf-16le)))
369 (x-dnd-insert-text window action (decode-coding-string text coding))))
371 (defun x-dnd-insert-ctext (window action text)
372 "Decode the compound text and insert it at point.
373 TEXT is the text as a string, WINDOW is the window where the drop happened."
374 (x-dnd-insert-text window action
375 (decode-coding-string text
376 'compound-text-with-extensions)))
378 (defun x-dnd-insert-text (window action text)
379 "Insert text at point or push to the kill ring if buffer is read only.
380 TEXT is the text as a string, WINDOW is the window where the drop happened."
381 (if (or buffer-read-only
382 (not (windowp window)))
383 (progn
384 (kill-new text)
385 (message
386 (substitute-command-keys
387 "The dropped text can be accessed with \\[yank]")))
388 (insert text))
389 action)
391 (defun x-dnd-handle-uri-list (window action string)
392 "Split an uri-list into separate URIs and call `x-dnd-handle-one-url'.
393 WINDOW is the window where the drop happened.
394 STRING is the uri-list as a string. The URIs are separated by \r\n."
395 (let ((uri-list (split-string string "[\0\r\n]" t))
396 retval)
397 (dolist (bf uri-list)
398 ;; If one URL is handeled, treat as if the whole drop succeeded.
399 (let ((did-action (x-dnd-handle-one-url window action bf)))
400 (when did-action (setq retval did-action))))
401 retval))
403 (defun x-dnd-handle-file-name (window action string)
404 "Prepend file:// to file names and call `x-dnd-handle-one-url'.
405 WINDOW is the window where the drop happened.
406 STRING is the file names as a string, separated by nulls."
407 (let ((uri-list (split-string string "[\0\r\n]" t))
408 retval)
409 (dolist (bf uri-list)
410 ;; If one URL is handeled, treat as if the whole drop succeeded.
411 (let* ((file-uri (concat "file://" bf))
412 (did-action (x-dnd-handle-one-url window action file-uri)))
413 (when did-action (setq retval did-action))))
414 retval))
417 (defun x-dnd-choose-type (types &optional known-types)
418 "Choose which type we want to receive for the drop.
419 TYPES are the types the source of the drop offers, a vector of type names
420 as strings or symbols. Select among the types in `x-dnd-known-types' or
421 KNOWN-TYPES if given, and return that type name.
422 If no suitable type is found, return nil."
423 (let* ((known-list (or known-types x-dnd-known-types))
424 (first-known-type (car known-list))
425 (types-array types)
426 (found (when first-known-type
427 (catch 'done
428 (dotimes (i (length types-array))
429 (let* ((type (aref types-array i))
430 (typename (if (symbolp type)
431 (symbol-name type) type)))
432 (when (equal first-known-type typename)
433 (throw 'done first-known-type))))
434 nil))))
436 (if (and (not found) (cdr known-list))
437 (x-dnd-choose-type types (cdr known-list))
438 found)))
440 (defun x-dnd-drop-data (event frame window data type)
441 "Drop one data item onto a frame.
442 EVENT is the client message for the drop, FRAME is the frame the drop occurred
443 on. WINDOW is the window of FRAME where the drop happened. DATA is the data
444 received from the source, and type is the type for DATA, see
445 `x-dnd-types-alist').
447 Returns the action used (move, copy, link, private) if drop was successful,
448 nil if not."
449 (let* ((type-info (assoc type x-dnd-types-alist))
450 (handler (cdr type-info))
451 (state (x-dnd-get-state-for-frame frame))
452 (action (aref state 5))
453 (w (posn-window (event-start event))))
454 (when handler
455 (if (and (windowp w) (window-live-p w))
456 ;; If dropping in a window, open files in that window rather
457 ;; than in a new widow.
458 (let ((x-dnd-open-file-other-window nil))
459 (goto-char (posn-point (event-start event)))
460 (funcall handler window action data))
461 (let ((x-dnd-open-file-other-window t)) ;; Dropping on non-window.
462 (select-frame frame)
463 (funcall handler window action data))))))
465 (defun x-dnd-handle-drag-n-drop-event (event)
466 "Receive drag and drop events (X client messages).
467 Currently XDND, Motif and old KDE 1.x protocols are recognized."
468 (interactive "e")
469 (let* ((client-message (car (cdr (cdr event))))
470 (window (posn-window (event-start event)))
471 (message-atom (aref client-message 0))
472 (frame (aref client-message 1))
473 (format (aref client-message 2))
474 (data (aref client-message 3)))
476 (cond ((equal "DndProtocol" message-atom) ; Old KDE 1.x.
477 (x-dnd-handle-old-kde event frame window message-atom format data))
479 ((equal "_MOTIF_DRAG_AND_DROP_MESSAGE" message-atom) ; Motif
480 (x-dnd-handle-motif event frame window message-atom format data))
482 ((and (> (length message-atom) 4) ; XDND protocol.
483 (equal "Xdnd" (substring message-atom 0 4)))
484 (x-dnd-handle-xdnd event frame window message-atom format data)))))
487 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
488 ;;; Old KDE protocol. Only dropping of files.
490 (defun x-dnd-handle-old-kde (event frame window message format data)
491 "Open the files in a KDE 1.x drop."
492 (let ((values (x-window-property "DndSelection" frame nil 0 t)))
493 (x-dnd-handle-uri-list window 'private
494 (replace-regexp-in-string "\0$" "" values))))
495 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
499 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
500 ;;; XDND protocol.
502 (defvar x-dnd-xdnd-to-action
503 '(("XdndActionPrivate" . private)
504 ("XdndActionCopy" . copy)
505 ("XdndActionMove" . move)
506 ("XdndActionLink" . link)
507 ("XdndActionAsk" . ask))
508 "Mapping from XDND action types to lisp symbols.")
510 (defun x-dnd-init-xdnd-for-frame (frame)
511 "Set the XdndAware property for FRAME to indicate that we do XDND."
512 (x-change-window-property "XdndAware"
513 '(5) ;; The version of XDND we support.
514 frame "ATOM" 32 t))
516 (defun x-dnd-get-drop-width-height (frame w accept)
517 "Return the widht/height to be sent in a XDndStatus message.
518 FRAME is the frame and W is the window where the drop happened.
519 If ACCEPT is nil return 0 (empty rectangle),
520 otherwise if W is a window, return its widht/height,
521 otherwise return the frame width/height."
522 (if accept
523 (if (windowp w) ;; w is not a window if dropping on the menu bar,
524 ;; scroll bar or tool bar.
525 (let ((edges (window-inside-pixel-edges w)))
526 (cons
527 (- (nth 2 edges) (nth 0 edges)) ;; right - left
528 (- (nth 3 edges) (nth 1 edges)))) ;; bottom - top
529 (cons (frame-pixel-width frame)
530 (frame-pixel-height frame)))
533 (defun x-dnd-get-drop-x-y (frame w)
534 "Return the x/y coordinates to be sent in a XDndStatus message.
535 Coordinates are required to be absolute.
536 FRAME is the frame and W is the window where the drop happened.
537 If W is a window, return its absolute corrdinates,
538 otherwise return the frame coordinates."
539 (let* ((frame-left (frame-parameter frame 'left))
540 ;; If the frame is outside the display, frame-left looks like
541 ;; '(0 -16). Extract the -16.
542 (frame-real-left (if (consp frame-left) (car (cdr frame-left))
543 frame-left))
544 (frame-top (frame-parameter frame 'top))
545 (frame-real-top (if (consp frame-top) (car (cdr frame-top))
546 frame-top)))
547 (if (windowp w)
548 (let ((edges (window-inside-pixel-edges w)))
549 (cons
550 (+ frame-real-left (nth 0 edges))
551 (+ frame-real-top (nth 1 edges))))
552 (cons frame-real-left frame-real-top))))
554 (defun x-dnd-handle-xdnd (event frame window message format data)
555 "Receive one XDND event (client message) and send the appropriate reply.
556 EVENT is the client message. FRAME is where the mouse is now.
557 WINDOW is the window within FRAME where the mouse is now.
558 FORMAT is 32 (not used). MESSAGE is the data part of an XClientMessageEvent."
559 (cond ((equal "XdndEnter" message)
560 (let* ((flags (aref data 1))
561 (version (and (consp flags) (ash (car flags) -8)))
562 (more-than-3 (and (consp flags) (cdr flags)))
563 (dnd-source (aref data 0)))
564 (if version ;; If flags is bad, version will be nil.
565 (x-dnd-save-state
566 window nil nil
567 (if (> more-than-3 0)
568 (x-window-property "XdndTypeList"
569 frame "AnyPropertyType"
570 dnd-source nil t)
571 (vector (x-get-atom-name (aref data 2))
572 (x-get-atom-name (aref data 3))
573 (x-get-atom-name (aref data 4))))))))
575 ((equal "XdndPosition" message)
576 (let* ((x (car (aref data 2)))
577 (y (cdr (aref data 2)))
578 (action (x-get-atom-name (aref data 4)))
579 (dnd-source (aref data 0))
580 (dnd-time (aref data 3))
581 (action-type (x-dnd-maybe-call-test-function
582 window
583 (cdr (assoc action x-dnd-xdnd-to-action))))
584 (reply-action (car (rassoc (car action-type)
585 x-dnd-xdnd-to-action)))
586 (accept ;; 1 = accept, 0 = reject
587 (if (and reply-action action-type) 1 0))
588 (list-to-send
589 (list (string-to-number
590 (frame-parameter frame 'outer-window-id))
591 accept ;; 1 = Accept, 0 = reject.
592 (x-dnd-get-drop-x-y frame window)
593 (x-dnd-get-drop-width-height
594 frame window (eq accept 1))
595 (or reply-action 0)
597 (x-send-client-message
598 frame dnd-source frame "XdndStatus" 32 list-to-send)
601 ((equal "XdndLeave" message)
602 (x-dnd-forget-drop window))
604 ((equal "XdndDrop" message)
605 (if (windowp window) (select-window window))
606 (let* ((dnd-source (aref data 0))
607 (value (and (x-dnd-current-type window)
608 (x-get-selection-internal
609 'XdndSelection
610 (intern (x-dnd-current-type window)))))
611 success action ret-action)
613 (setq action (if value
614 (condition-case info
615 (x-dnd-drop-data event frame window value
616 (x-dnd-current-type window))
617 (error
618 (message "Error: %s" info)
619 nil))))
621 (setq success (if action 1 0))
622 (setq ret-action
623 (if (eq success 1)
624 (or (car (rassoc action x-dnd-xdnd-to-action))
625 "XdndActionPrivate")
628 (x-send-client-message
629 frame dnd-source frame "XdndFinished" 32
630 (list (string-to-number (frame-parameter frame 'outer-window-id))
631 success ;; 1 = Success, 0 = Error
632 (if success "XdndActionPrivate" 0)
634 (x-dnd-forget-drop window)))
636 (t (error "Unknown XDND message %s %s" message data))))
638 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
639 ;;; Motif protocol.
641 (defun x-dnd-init-motif-for-frame (frame)
642 "Set _MOTIF_DRAG_RECEIVER_INFO for FRAME to indicate that we do Motif DND."
643 (x-change-window-property "_MOTIF_DRAG_RECEIVER_INFO"
644 (list
645 (byteorder)
646 0 ; The Motif DND version.
647 5 ; We want drag dynamic.
648 0 0 0 0 0 0 0
649 0 0 0 0 0 0) ; Property must be 16 bytes.
650 frame "_MOTIF_DRAG_RECEIVER_INFO" 8 t))
652 (defun x-dnd-get-motif-value (data offset size byteorder)
653 (cond ((eq size 2)
654 (if (eq byteorder ?l)
655 (+ (ash (aref data (1+ offset)) 8)
656 (aref data offset))
657 (+ (ash (aref data offset) 8)
658 (aref data (1+ offset)))))
660 ((eq size 4)
661 (if (eq byteorder ?l)
662 (cons (+ (ash (aref data (+ 3 offset)) 8)
663 (aref data (+ 2 offset)))
664 (+ (ash (aref data (1+ offset)) 8)
665 (aref data offset)))
666 (cons (+ (ash (aref data offset) 8)
667 (aref data (1+ offset)))
668 (+ (ash (aref data (+ 2 offset)) 8)
669 (aref data (+ 3 offset))))))))
671 (defun x-dnd-motif-value-to-list (value size byteorder)
672 (let ((bytes (cond ((eq size 2)
673 (list (logand (lsh value -8) ?\xff)
674 (logand value ?\xff)))
676 ((eq size 4)
677 (if (consp value)
678 (list (logand (lsh (car value) -8) ?\xff)
679 (logand (car value) ?\xff)
680 (logand (lsh (cdr value) -8) ?\xff)
681 (logand (cdr value) ?\xff))
682 (list (logand (lsh value -24) ?\xff)
683 (logand (lsh value -16) ?\xff)
684 (logand (lsh value -8) ?\xff)
685 (logand value ?\xff)))))))
686 (if (eq byteorder ?l)
687 (reverse bytes)
688 bytes)))
691 (defvar x-dnd-motif-message-types
692 '((0 . XmTOP_LEVEL_ENTER)
693 (1 . XmTOP_LEVEL_LEAVE)
694 (2 . XmDRAG_MOTION)
695 (3 . XmDROP_SITE_ENTER)
696 (4 . XmDROP_SITE_LEAVE)
697 (5 . XmDROP_START)
698 (6 . XmDROP_FINISH)
699 (7 . XmDRAG_DROP_FINISH)
700 (8 . XmOPERATION_CHANGED))
701 "Mapping from numbers to Motif DND message types.")
703 (defvar x-dnd-motif-to-action
704 '((1 . move)
705 (2 . copy)
706 (3 . link) ; Both 3 and 4 has been seen as link.
707 (4 . link)
708 (2 . private)) ; Motif does not have private, so use copy for private.
709 "Mapping from number to operation for Motif DND.")
711 (defun x-dnd-handle-motif (event frame window message-atom format data)
712 (let* ((message-type (cdr (assoc (aref data 0) x-dnd-motif-message-types)))
713 (source-byteorder (aref data 1))
714 (my-byteorder (byteorder))
715 (source-flags (x-dnd-get-motif-value data 2 2 source-byteorder))
716 (source-action (cdr (assoc (logand ?\xF source-flags)
717 x-dnd-motif-to-action))))
719 (cond ((eq message-type 'XmTOP_LEVEL_ENTER)
720 (let* ((dnd-source (x-dnd-get-motif-value
721 data 8 4 source-byteorder))
722 (selection-atom (x-dnd-get-motif-value
723 data 12 4 source-byteorder))
724 (atom-name (x-get-atom-name selection-atom))
725 (types (when atom-name
726 (x-get-selection-internal (intern atom-name)
727 'TARGETS))))
728 (x-dnd-forget-drop frame)
729 (when types (x-dnd-save-state window nil nil
730 types
731 dnd-source))))
733 ;; Can not forget drop here, LEAVE comes before DROP_START and
734 ;; we need the state in DROP_START.
735 ((eq message-type 'XmTOP_LEVEL_LEAVE)
736 nil)
738 ((eq message-type 'XmDRAG_MOTION)
739 (let* ((state (x-dnd-get-state-for-frame frame))
740 (timestamp (x-dnd-motif-value-to-list
741 (x-dnd-get-motif-value data 4 4
742 source-byteorder)
743 4 my-byteorder))
744 (x (x-dnd-motif-value-to-list
745 (x-dnd-get-motif-value data 8 2 source-byteorder)
746 2 my-byteorder))
747 (y (x-dnd-motif-value-to-list
748 (x-dnd-get-motif-value data 10 2 source-byteorder)
749 2 my-byteorder))
750 (dnd-source (aref state 6))
751 (first-move (not (aref state 3)))
752 (action-type (x-dnd-maybe-call-test-function
753 window
754 source-action))
755 (reply-action (car (rassoc (car action-type)
756 x-dnd-motif-to-action)))
757 (reply-flags
758 (x-dnd-motif-value-to-list
759 (if reply-action
760 (+ reply-action
761 ?\x30 ; 30: valid drop site
762 ?\x700) ; 700: can do copy, move or link
763 ?\x30) ; 30: drop site, but noop.
764 2 my-byteorder))
765 (reply (append
766 (list
767 (+ ?\x80 ; 0x80 indicates a reply.
768 (if first-move
769 3 ; First time, reply is SITE_ENTER.
770 2)) ; Not first time, reply is DRAG_MOTION.
771 my-byteorder)
772 reply-flags
773 timestamp
775 y)))
776 (x-send-client-message frame
777 dnd-source
778 frame
779 "_MOTIF_DRAG_AND_DROP_MESSAGE"
781 reply)))
783 ((eq message-type 'XmOPERATION_CHANGED)
784 (let* ((state (x-dnd-get-state-for-frame frame))
785 (timestamp (x-dnd-motif-value-to-list
786 (x-dnd-get-motif-value data 4 4 source-byteorder)
787 4 my-byteorder))
788 (dnd-source (aref state 6))
789 (action-type (x-dnd-maybe-call-test-function
790 window
791 source-action))
792 (reply-action (car (rassoc (car action-type)
793 x-dnd-motif-to-action)))
794 (reply-flags
795 (x-dnd-motif-value-to-list
796 (if reply-action
797 (+ reply-action
798 ?\x30 ; 30: valid drop site
799 ?\x700) ; 700: can do copy, move or link
800 ?\x30) ; 30: drop site, but noop
801 2 my-byteorder))
802 (reply (append
803 (list
804 (+ ?\x80 ; 0x80 indicates a reply.
805 8) ; 8 is OPERATION_CHANGED
806 my-byteorder)
807 reply-flags
808 timestamp)))
809 (x-send-client-message frame
810 dnd-source
811 frame
812 "_MOTIF_DRAG_AND_DROP_MESSAGE"
814 reply)))
816 ((eq message-type 'XmDROP_START)
817 (let* ((x (x-dnd-motif-value-to-list
818 (x-dnd-get-motif-value data 8 2 source-byteorder)
819 2 my-byteorder))
820 (y (x-dnd-motif-value-to-list
821 (x-dnd-get-motif-value data 10 2 source-byteorder)
822 2 my-byteorder))
823 (selection-atom (x-dnd-get-motif-value
824 data 12 4 source-byteorder))
825 (atom-name (x-get-atom-name selection-atom))
826 (dnd-source (x-dnd-get-motif-value
827 data 16 4 source-byteorder))
828 (action-type (x-dnd-maybe-call-test-function
829 window
830 source-action))
831 (reply-action (car (rassoc (car action-type)
832 x-dnd-motif-to-action)))
833 (reply-flags
834 (x-dnd-motif-value-to-list
835 (if reply-action
836 (+ reply-action
837 ?\x30 ; 30: valid drop site
838 ?\x700) ; 700: can do copy, move or link
839 (+ ?\x30 ; 30: drop site, but noop.
840 ?\x200)) ; 200: drop cancel.
841 2 my-byteorder))
842 (reply (append
843 (list
844 (+ ?\x80 ; 0x80 indicates a reply.
845 5) ; DROP_START.
846 my-byteorder)
847 reply-flags
850 (timestamp (x-dnd-get-motif-value
851 data 4 4 source-byteorder))
852 action)
854 (x-send-client-message frame
855 dnd-source
856 frame
857 "_MOTIF_DRAG_AND_DROP_MESSAGE"
859 reply)
860 (setq action
861 (when (and reply-action atom-name)
862 (let* ((value (x-get-selection-internal
863 (intern atom-name)
864 (intern (x-dnd-current-type window)))))
865 (when value
866 (condition-case info
867 (x-dnd-drop-data event frame window value
868 (x-dnd-current-type window))
869 (error
870 (message "Error: %s" info)
871 nil))))))
872 (x-get-selection-internal
873 (intern atom-name)
874 (if action 'XmTRANSFER_SUCCESS 'XmTRANSFER_FAILURE)
875 timestamp)
876 (x-dnd-forget-drop frame)))
878 (t (error "Unknown Motif DND message %s %s" message-atom data)))))
884 (provide 'x-dnd)
886 ;;; arch-tag: b621fb7e-50da-4323-850b-5fc71ae64621
887 ;;; x-dnd.el ends here