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