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