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