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