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