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