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