]> code.delx.au - gnu-emacs/blob - lisp/x-dnd.el
* x-dnd.el (x-dnd-test-function, x-dnd-protocol-alist)
[gnu-emacs] / lisp / x-dnd.el
1 ;;; x-dnd.el --- drag and drop support for X.
2
3 ;; Copyright (C) 2004
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Jan Dj\e,Ad\e(Brv <jan.h.d@swipnet.se>
7 ;; Maintainer: FSF
8 ;; Keywords: window, drag, drop
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This file provides the drop part only. Currently supported protocols
30 ;; are XDND, Motif and the old KDE 1.x protocol.
31
32 ;;; Code:
33
34 ;;; Customizable variables
35
36
37 (defcustom x-dnd-test-function 'x-dnd-default-test-function
38 "The function drag and drop uses to determine if to accept or reject a drop.
39 The function takes three arguments, WINDOW ACTION and TYPES.
40 WINDOW is where the mouse is when the function is called. WINDOW may be a
41 frame if the mouse isn't over a real window (i.e. menu bar, tool bar or
42 scroll bar). ACTION is the suggested action from the drag and drop source,
43 one of the symbols move, copy link or ask. TYPES is a list of available types
44 for the drop.
45
46 The function shall return nil to reject the drop or a cons with two values,
47 the wanted action as car and the wanted type as cdr. The wanted action
48 can be copy, move, link, ask or private.
49 The default value for this variable is `x-dnd-default-test-function'."
50 :version "21.4"
51 :type 'symbol
52 :group 'x)
53
54 (defcustom x-dnd-protocol-alist
55 '(
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
59 )
60
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 "21.4"
74 :type 'alist
75 :group 'x)
76
77
78 (defcustom x-dnd-types-alist
79 '(
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)
92 )
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 "21.4"
102 :type 'alist
103 :group 'x)
104
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 "21.4"
108 :type 'boolean
109 :group 'x)
110
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"
124 )
125 "The types accepted by default for dropped data.
126 The types are chosen in the order they appear in the list."
127 :version "21.4"
128 :type '(repeat string)
129 :group 'x
130 )
131
132 ;; Internal variables
133
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.")
146
147 (defvar x-dnd-empty-state [nil nil nil nil nil nil nil])
148
149
150
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))
155
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)))
165
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)))
169
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))))
179
180
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))
185
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)))
191
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))))
218
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)))
236
237
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)
254 (or
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))
273
274
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."
278
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)))))))
292
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
304 f
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)))))
309
310
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."
317
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))))
326
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."
333
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"))))
339
340
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)))
358
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)))
363
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))))
370
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)))
377
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)
390
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))
402
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))
415
416
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))))
435
436 (if (and (not found) (cdr known-list))
437 (x-dnd-choose-type types (cdr known-list))
438 found)))
439
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').
446
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))))))
464
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)))
475
476 (cond ((equal "DndProtocol" message-atom) ; Old KDE 1.x.
477 (x-dnd-handle-old-kde event frame window message-atom format data))
478
479 ((equal "_MOTIF_DRAG_AND_DROP_MESSAGE" message-atom) ; Motif
480 (x-dnd-handle-motif event frame window message-atom format data))
481
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)))))
485
486
487 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
488 ;;; Old KDE protocol. Only dropping of files.
489
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
496
497
498
499 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
500 ;;; XDND protocol.
501
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.")
509
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))
515
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)))
531 0))
532
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))))
553
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 ((version (ash (car (aref data 1)) -8))
561 (more-than-3 (cdr (aref data 1)))
562 (dnd-source (aref data 0)))
563 (x-dnd-save-state
564 window nil nil
565 (if (> more-than-3 0)
566 (x-window-property "XdndTypeList"
567 frame "AnyPropertyType"
568 dnd-source nil t)
569 (vector (x-get-atom-name (aref data 2))
570 (x-get-atom-name (aref data 3))
571 (x-get-atom-name (aref data 4)))))))
572
573 ((equal "XdndPosition" message)
574 (let* ((x (car (aref data 2)))
575 (y (cdr (aref data 2)))
576 (action (x-get-atom-name (aref data 4)))
577 (dnd-source (aref data 0))
578 (dnd-time (aref data 3))
579 (action-type (x-dnd-maybe-call-test-function
580 window
581 (cdr (assoc action x-dnd-xdnd-to-action))))
582 (reply-action (car (rassoc (car action-type)
583 x-dnd-xdnd-to-action)))
584 (accept ;; 1 = accept, 0 = reject
585 (if (and reply-action action-type) 1 0))
586 (list-to-send
587 (list (string-to-number
588 (frame-parameter frame 'outer-window-id))
589 accept ;; 1 = Accept, 0 = reject.
590 (x-dnd-get-drop-x-y frame window)
591 (x-dnd-get-drop-width-height
592 frame window (eq accept 1))
593 (or reply-action 0)
594 )))
595 (x-send-client-message
596 frame dnd-source frame "XdndStatus" 32 list-to-send)
597 ))
598
599 ((equal "XdndLeave" message)
600 (x-dnd-forget-drop window))
601
602 ((equal "XdndDrop" message)
603 (if (windowp window) (select-window window))
604 (let* ((dnd-source (aref data 0))
605 (value (and (x-dnd-current-type window)
606 (x-get-selection-internal
607 'XdndSelection
608 (intern (x-dnd-current-type window)))))
609 success action ret-action)
610
611 (setq action (if value
612 (condition-case info
613 (x-dnd-drop-data event frame window value
614 (x-dnd-current-type window))
615 (error
616 (message "Error: %s" info)
617 nil))))
618
619 (setq success (if action 1 0))
620 (setq ret-action
621 (if (eq success 1)
622 (or (car (rassoc action x-dnd-xdnd-to-action))
623 "XdndActionPrivate")
624 0))
625
626 (x-send-client-message
627 frame dnd-source frame "XdndFinished" 32
628 (list (string-to-number (frame-parameter frame 'outer-window-id))
629 success ;; 1 = Success, 0 = Error
630 (if success "XdndActionPrivate" 0)
631 ))
632 (x-dnd-forget-drop window)))
633
634 (t (error "Unknown XDND message %s %s" message data))))
635
636 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
637 ;;; Motif protocol.
638
639 (defun x-dnd-init-motif-for-frame (frame)
640 "Set _MOTIF_DRAG_RECEIVER_INFO for FRAME to indicate that we do Motif DND."
641 (x-change-window-property "_MOTIF_DRAG_RECEIVER_INFO"
642 (list
643 (byteorder)
644 0 ; The Motif DND version.
645 5 ; We want drag dynamic.
646 0 0 0 0 0 0 0
647 0 0 0 0 0 0) ; Property must be 16 bytes.
648 frame "_MOTIF_DRAG_RECEIVER_INFO" 8 t))
649
650 (defun x-dnd-get-motif-value (data offset size byteorder)
651 (cond ((eq size 2)
652 (if (eq byteorder ?l)
653 (+ (ash (aref data (1+ offset)) 8)
654 (aref data offset))
655 (+ (ash (aref data offset) 8)
656 (aref data (1+ offset)))))
657
658 ((eq size 4)
659 (if (eq byteorder ?l)
660 (cons (+ (ash (aref data (+ 3 offset)) 8)
661 (aref data (+ 2 offset)))
662 (+ (ash (aref data (1+ offset)) 8)
663 (aref data offset)))
664 (cons (+ (ash (aref data offset) 8)
665 (aref data (1+ offset)))
666 (+ (ash (aref data (+ 2 offset)) 8)
667 (aref data (+ 3 offset))))))))
668
669 (defun x-dnd-motif-value-to-list (value size byteorder)
670 (let ((bytes (cond ((eq size 2)
671 (list (logand (lsh value -8) ?\xff)
672 (logand value ?\xff)))
673
674 ((eq size 4)
675 (if (consp value)
676 (list (logand (lsh (car value) -8) ?\xff)
677 (logand (car value) ?\xff)
678 (logand (lsh (cdr value) -8) ?\xff)
679 (logand (cdr value) ?\xff))
680 (list (logand (lsh value -24) ?\xff)
681 (logand (lsh value -16) ?\xff)
682 (logand (lsh value -8) ?\xff)
683 (logand value ?\xff)))))))
684 (if (eq byteorder ?l)
685 (reverse bytes)
686 bytes)))
687
688
689 (defvar x-dnd-motif-message-types
690 '((0 . XmTOP_LEVEL_ENTER)
691 (1 . XmTOP_LEVEL_LEAVE)
692 (2 . XmDRAG_MOTION)
693 (3 . XmDROP_SITE_ENTER)
694 (4 . XmDROP_SITE_LEAVE)
695 (5 . XmDROP_START)
696 (6 . XmDROP_FINISH)
697 (7 . XmDRAG_DROP_FINISH)
698 (8 . XmOPERATION_CHANGED))
699 "Mapping from numbers to Motif DND message types.")
700
701 (defvar x-dnd-motif-to-action
702 '((1 . move)
703 (2 . copy)
704 (3 . link) ; Both 3 and 4 has been seen as link.
705 (4 . link)
706 (2 . private)) ; Motif does not have private, so use copy for private.
707 "Mapping from number to operation for Motif DND.")
708
709 (defun x-dnd-handle-motif (event frame window message-atom format data)
710 (let* ((message-type (cdr (assoc (aref data 0) x-dnd-motif-message-types)))
711 (source-byteorder (aref data 1))
712 (my-byteorder (byteorder))
713 (source-flags (x-dnd-get-motif-value data 2 2 source-byteorder))
714 (source-action (cdr (assoc (logand ?\xF source-flags)
715 x-dnd-motif-to-action))))
716
717 (cond ((eq message-type 'XmTOP_LEVEL_ENTER)
718 (let* ((dnd-source (x-dnd-get-motif-value
719 data 8 4 source-byteorder))
720 (selection-atom (x-dnd-get-motif-value
721 data 12 4 source-byteorder))
722 (atom-name (x-get-atom-name selection-atom))
723 (types (when atom-name
724 (x-get-selection-internal (intern atom-name)
725 'TARGETS))))
726 (x-dnd-forget-drop frame)
727 (when types (x-dnd-save-state window nil nil
728 types
729 dnd-source))))
730
731 ;; Can not forget drop here, LEAVE comes before DROP_START and
732 ;; we need the state in DROP_START.
733 ((eq message-type 'XmTOP_LEVEL_LEAVE)
734 nil)
735
736 ((eq message-type 'XmDRAG_MOTION)
737 (let* ((state (x-dnd-get-state-for-frame frame))
738 (timestamp (x-dnd-motif-value-to-list
739 (x-dnd-get-motif-value data 4 4
740 source-byteorder)
741 4 my-byteorder))
742 (x (x-dnd-motif-value-to-list
743 (x-dnd-get-motif-value data 8 2 source-byteorder)
744 2 my-byteorder))
745 (y (x-dnd-motif-value-to-list
746 (x-dnd-get-motif-value data 10 2 source-byteorder)
747 2 my-byteorder))
748 (dnd-source (aref state 6))
749 (first-move (not (aref state 3)))
750 (action-type (x-dnd-maybe-call-test-function
751 window
752 source-action))
753 (reply-action (car (rassoc (car action-type)
754 x-dnd-motif-to-action)))
755 (reply-flags
756 (x-dnd-motif-value-to-list
757 (if reply-action
758 (+ reply-action
759 ?\x30 ; 30: valid drop site
760 ?\x700) ; 700: can do copy, move or link
761 ?\x30) ; 30: drop site, but noop.
762 2 my-byteorder))
763 (reply (append
764 (list
765 (+ ?\x80 ; 0x80 indicates a reply.
766 (if first-move
767 3 ; First time, reply is SITE_ENTER.
768 2)) ; Not first time, reply is DRAG_MOTION.
769 my-byteorder)
770 reply-flags
771 timestamp
772 x
773 y)))
774 (x-send-client-message frame
775 dnd-source
776 frame
777 "_MOTIF_DRAG_AND_DROP_MESSAGE"
778 8
779 reply)))
780
781 ((eq message-type 'XmOPERATION_CHANGED)
782 (let* ((state (x-dnd-get-state-for-frame frame))
783 (timestamp (x-dnd-motif-value-to-list
784 (x-dnd-get-motif-value data 4 4 source-byteorder)
785 4 my-byteorder))
786 (dnd-source (aref state 6))
787 (action-type (x-dnd-maybe-call-test-function
788 window
789 source-action))
790 (reply-action (car (rassoc (car action-type)
791 x-dnd-motif-to-action)))
792 (reply-flags
793 (x-dnd-motif-value-to-list
794 (if reply-action
795 (+ reply-action
796 ?\x30 ; 30: valid drop site
797 ?\x700) ; 700: can do copy, move or link
798 ?\x30) ; 30: drop site, but noop
799 2 my-byteorder))
800 (reply (append
801 (list
802 (+ ?\x80 ; 0x80 indicates a reply.
803 8) ; 8 is OPERATION_CHANGED
804 my-byteorder)
805 reply-flags
806 timestamp)))
807 (x-send-client-message frame
808 dnd-source
809 frame
810 "_MOTIF_DRAG_AND_DROP_MESSAGE"
811 8
812 reply)))
813
814 ((eq message-type 'XmDROP_START)
815 (let* ((x (x-dnd-motif-value-to-list
816 (x-dnd-get-motif-value data 8 2 source-byteorder)
817 2 my-byteorder))
818 (y (x-dnd-motif-value-to-list
819 (x-dnd-get-motif-value data 10 2 source-byteorder)
820 2 my-byteorder))
821 (selection-atom (x-dnd-get-motif-value
822 data 12 4 source-byteorder))
823 (atom-name (x-get-atom-name selection-atom))
824 (dnd-source (x-dnd-get-motif-value
825 data 16 4 source-byteorder))
826 (action-type (x-dnd-maybe-call-test-function
827 window
828 source-action))
829 (reply-action (car (rassoc (car action-type)
830 x-dnd-motif-to-action)))
831 (reply-flags
832 (x-dnd-motif-value-to-list
833 (if reply-action
834 (+ reply-action
835 ?\x30 ; 30: valid drop site
836 ?\x700) ; 700: can do copy, move or link
837 (+ ?\x30 ; 30: drop site, but noop.
838 ?\x200)) ; 200: drop cancel.
839 2 my-byteorder))
840 (reply (append
841 (list
842 (+ ?\x80 ; 0x80 indicates a reply.
843 5) ; DROP_START.
844 my-byteorder)
845 reply-flags
846 x
847 y))
848 (timestamp (x-dnd-get-motif-value
849 data 4 4 source-byteorder))
850 action)
851
852 (x-send-client-message frame
853 dnd-source
854 frame
855 "_MOTIF_DRAG_AND_DROP_MESSAGE"
856 8
857 reply)
858 (setq action
859 (when (and reply-action atom-name)
860 (let* ((value (x-get-selection-internal
861 (intern atom-name)
862 (intern (x-dnd-current-type window)))))
863 (when value
864 (condition-case info
865 (x-dnd-drop-data event frame window value
866 (x-dnd-current-type window))
867 (error
868 (message "Error: %s" info)
869 nil))))))
870 (x-get-selection-internal
871 (intern atom-name)
872 (if action 'XmTRANSFER_SUCCESS 'XmTRANSFER_FAILURE)
873 timestamp)
874 (x-dnd-forget-drop frame)))
875
876 (t (error "Unknown Motif DND message %s %s" message-atom data)))))
877
878
879 ;;;
880
881
882 (provide 'x-dnd)
883
884 ;;; arch-tag: b621fb7e-50da-4323-850b-5fc71ae64621
885 ;;; x-dnd.el ends here