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