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