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