(smtpmail-try-auth-methods): Fix typo.
[emacs.git] / lisp / x-dnd.el
blob14681ae7497176353405cb0a458ae2032bc72a71
1 ;;; x-dnd.el --- drag and drop support for X.
3 ;; Copyright (C) 2004
4 ;; Free Software Foundation, Inc.
6 ;; Author: Jan Dj\e,Ad\e(Brv <jan.h.d@swipnet.se>
7 ;; Maintainer: FSF
8 ;; Keywords: window, drag, drop
10 ;; This file is part of GNU Emacs.
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.
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.
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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This file provides the drop part only. Currently supported protocols
30 ;; are XDND and the old KDE 1.x protocol.
32 ;;; Code:
34 ;;; 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.
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 :type 'symbol
51 :group 'x)
53 (defcustom x-dnd-protocol-alist
55 ("^file:///" . x-dnd-open-local-file) ; XDND format.
56 ("^file://" . x-dnd-open-file) ; URL with host
57 ("^file:" . x-dnd-open-local-file) ; Old KDE, Motif, Sun
60 "The functions to call for different protocols when a drop is made.
61 This variable is used by `x-dnd-handle-uri-list' and `x-dnd-handle-moz-url'.
62 The list contains of (REGEXP . FUNCTION) pairs.
63 The functions shall take two arguments, URL, which is the URL dropped and
64 ACTION which is the action to be performed for the drop (move, copy, link,
65 private or ask).
66 If no match is found here, and the value of `browse-url-browser-function'
67 is a pair of (REGEXP . FUNCTION), those regexps are tried for a match.
68 Insertion of text is not handeled by these functions, see `x-dnd-types-alist'
69 for that.
70 The function shall return the action done (move, copy, link or private)
71 if some action was made, or nil if the URL is ignored."
72 :type 'alist
73 :group 'x)
76 (defcustom x-dnd-types-alist
78 ("text/uri-list" . x-dnd-handle-uri-list)
79 ("text/x-moz-url" . x-dnd-handle-moz-url)
80 ("_NETSCAPE_URL" . x-dnd-handle-uri-list)
81 ("FILE_NAME" . x-dnd-handle-file-name)
82 ("UTF8_STRING" . x-dnd-insert-utf8-text)
83 ("text/plain;charset=UTF-8" . x-dnd-insert-utf8-text)
84 ("text/plain;charset=utf-8" . x-dnd-insert-utf8-text)
85 ("text/unicode" . x-dnd-insert-utf16-text)
86 ("text/plain" . x-dnd-insert-text)
87 ("COMPOUND_TEXT" . x-dnd-insert-ctext)
88 ("STRING" . x-dnd-insert-text)
89 ("TEXT" . x-dnd-insert-text)
91 "Which function to call to handle a drop of that type.
92 If the type for the drop is not present, or the function is nil,
93 the drop is rejected. The function takes three arguments, WINDOW, ACTION
94 and DATA. WINDOW is where the drop occured, ACTION is the action for
95 this drop (copy, move, link, private or ask) as determined by a previous
96 call to `x-dnd-test-function'. DATA is the drop data.
97 The function shall return the action used (copy, move, link or private) if drop
98 is successful, nil if not."
99 :type 'alist
100 :group 'x)
102 (defcustom x-dnd-open-file-other-window nil
103 "If non-nil, always use find-file-other-window to open dropped files."
104 :type 'boolean
105 :group 'x)
107 ;; Internal variables
109 (defvar x-dnd-known-types
110 '("text/uri-list"
111 "text/x-moz-url"
112 "_NETSCAPE_URL"
113 "FILE_NAME"
114 "UTF8_STRING"
115 "text/plain;charset=UTF-8"
116 "text/plain;charset=utf-8"
117 "text/unicode"
118 "text/plain"
119 "COMPOUND_TEXT"
120 "STRING"
121 "TEXT"
123 "The types accepted by default for dropped data.
124 The types are chosen in the order they appear in the list.")
126 (defvar x-dnd-current-state nil
127 "The current state for a drop.
128 This is an alist with one entry for each display. The value for each display
129 is a vector that contains the state for drag and drop for that display.
130 Elements in the vector are:
131 Last buffer drag was in,
132 last window drag was in,
133 types available for drop,
134 the action suggested by the source,
135 the type we want for the drop,
136 the action we want for the drop,
137 any protocol specific data.")
139 (defvar x-dnd-empty-state [nil nil nil nil nil nil nil])
143 (defun x-dnd-init-frame (&optional frame)
144 "Setup drag and drop for FRAME (i.e. create appropriate properties)."
145 (x-dnd-init-xdnd-for-frame frame)
146 (x-dnd-init-motif-for-frame frame))
148 (defun x-dnd-get-state-cons-for-frame (frame-or-window)
149 "Return the entry in x-dnd-current-state for a frame or window."
150 (let* ((frame (if (framep frame-or-window) frame-or-window
151 (window-frame frame-or-window)))
152 (display (frame-parameter frame 'display)))
153 (if (not (assoc display x-dnd-current-state))
154 (push (cons display (copy-sequence x-dnd-empty-state))
155 x-dnd-current-state))
156 (assoc display x-dnd-current-state)))
158 (defun x-dnd-get-state-for-frame (frame-or-window)
159 "Return the state in x-dnd-current-state for a frame or window."
160 (cdr (x-dnd-get-state-cons-for-frame frame-or-window)))
162 (defun x-dnd-default-test-function (window action types)
163 "The default test function for drag and drop.
164 WINDOW is where the mouse is when this function is called. It may be a frame
165 if the mouse is over the menu bar, scroll bar or tool bar.
166 ACTION is the suggested action from the source, and TYPES are the
167 types the drop data can have. This function only accepts drops with
168 types in `x-dnd-known-types'. It always returns the action private."
169 (let ((type (x-dnd-choose-type types)))
170 (when type (cons 'private type))))
173 (defun x-dnd-current-type (frame-or-window)
174 "Return the type we want the DND data to be in for the current drop.
175 FRAME-OR-WINDOW is the frame or window that the mouse is over."
176 (aref (x-dnd-get-state-for-frame frame-or-window) 4))
178 (defun x-dnd-forget-drop (frame-or-window)
179 "Remove all state for the last drop.
180 FRAME-OR-WINDOW is the frame or window that the mouse is over."
181 (setcdr (x-dnd-get-state-cons-for-frame frame-or-window)
182 (copy-sequence x-dnd-empty-state)))
184 (defun x-dnd-maybe-call-test-function (window action)
185 "Call `x-dnd-test-function' if something has changed.
186 WINDOW is the window the mouse is over. ACTION is the suggested
187 action from the source. If nothing has changed, return the last
188 action and type we got from `x-dnd-test-function'."
189 (let ((buffer (when (and (windowp window) (window-live-p window))
190 (window-buffer window)))
191 (current-state (x-dnd-get-state-for-frame window)))
192 (when (or (not (equal buffer (aref current-state 0)))
193 (not (equal window (aref current-state 1)))
194 (not (equal action (aref current-state 3))))
195 (save-excursion
196 (when buffer (set-buffer buffer))
197 (let* ((action-type (funcall x-dnd-test-function
198 window
199 action
200 (aref current-state 2)))
201 (handler (cdr (assoc (cdr action-type) x-dnd-types-alist))))
202 ;; Ignore action-type if we have no handler.
203 (setq current-state
204 (x-dnd-save-state window
205 action
206 (when handler action-type)))))))
207 (let ((current-state (x-dnd-get-state-for-frame window)))
208 (cons (aref current-state 5)
209 (aref current-state 4))))
211 (defun x-dnd-save-state (window action action-type &optional types extra-data)
212 "Save the state of the current drag and drop.
213 WINDOW is the window the mouse is over. ACTION is the action suggested
214 by the source. ACTION-TYPE is the result of calling `x-dnd-test-function'.
215 If given, TYPES are the types for the drop data that the source supports.
216 EXTRA-DATA is data needed for a specific protocol."
217 (let ((current-state (x-dnd-get-state-for-frame window)))
218 (aset current-state 5 (car action-type))
219 (aset current-state 4 (cdr action-type))
220 (aset current-state 3 action)
221 (when types (aset current-state 2 types))
222 (when extra-data (aset current-state 6 extra-data))
223 (aset current-state 1 window)
224 (aset current-state 0 (if (and (windowp window)
225 (window-live-p window))
226 (window-buffer window) nil))
227 (setcdr (x-dnd-get-state-cons-for-frame window) current-state)))
230 (defun x-dnd-handle-one-url (window action arg)
231 "Handle one dropped url by calling the appropriate handler.
232 The handler is first localted by looking at `x-dnd-protocol-alist'.
233 If no match is found here, and the value of `browse-url-browser-function'
234 is a pair of (REGEXP . FUNCTION), those regexps are tried for a match.
235 If no match is found, just call `x-dnd-insert-text'.
236 WINDOW is where the drop happend, ACTION is the action for the drop,
237 ARG is the URL that has been dropped.
238 Returns ACTION."
239 (require 'browse-url)
240 (let* ((uri (replace-regexp-in-string
241 "%[A-Z0-9][A-Z0-9]"
242 (lambda (arg)
243 (format "%c" (string-to-number (substring arg 1) 16)))
244 arg))
245 ret)
247 (catch 'done
248 (dolist (bf x-dnd-protocol-alist)
249 (when (string-match (car bf) uri)
250 (setq ret (funcall (cdr bf) uri action))
251 (throw 'done t)))
252 nil)
253 (when (not (functionp browse-url-browser-function))
254 (catch 'done
255 (dolist (bf browse-url-browser-function)
256 (when (string-match (car bf) uri)
257 (setq ret 'private)
258 (funcall (cdr bf) uri action)
259 (throw 'done t)))
260 nil))
261 (progn
262 (x-dnd-insert-text window action uri)
263 (setq ret 'private)))
264 ret))
267 (defun x-dnd-get-local-file-uri (uri)
268 "Return an uri converted to file:/// syntax if uri is a local file.
269 Return nil if URI is not a local file."
271 ;; The hostname may be our hostname, in that case, convert to a local
272 ;; file. Otherwise return nil. TODO: How about an IP-address as hostname?
273 (let ((hostname (when (string-match "^file://\\([^/]*\\)" uri)
274 (downcase (match-string 1 uri))))
275 (system-name-no-dot
276 (downcase (if (string-match "^[^\\.]+" system-name)
277 (match-string 0 system-name)
278 system-name))))
279 (when (and hostname
280 (or (string-equal "localhost" hostname)
281 (string-equal (downcase system-name) hostname)
282 (string-equal system-name-no-dot hostname)))
283 (concat "file://" (substring uri (+ 7 (length hostname)))))))
285 (defun x-dnd-get-local-file-name (uri &optional must-exist)
286 "Return file name converted from file:/// or file: syntax.
287 URI is the uri for the file. If MUST-EXIST is given and non-nil,
288 only return non-nil if the file exists.
289 Return nil if URI is not a local file."
290 (let ((f (cond ((string-match "^file:///" uri) ; XDND format.
291 (substring uri (1- (match-end 0))))
292 ((string-match "^file:" uri) ; Old KDE, Motif, Sun
293 (substring uri (match-end 0))))))
294 (when (and f must-exist)
295 (let* ((decoded-f (decode-coding-string
297 (or file-name-coding-system
298 default-file-name-coding-system)))
299 (try-f (if (file-readable-p decoded-f) decoded-f f)))
300 (when (file-readable-p try-f) try-f)))))
303 (defun x-dnd-open-local-file (uri action)
304 "Open a local file.
305 The file is opened in the current window, or a new window if
306 `x-dnd-open-file-other-window' is set. URI is the url for the file,
307 and must have the format file:file-name or file:///file-name.
308 The last / in file:/// is part of the file name. ACTION is ignored."
310 (let* ((f (x-dnd-get-local-file-name uri t)))
311 (when f
312 (if (file-readable-p f)
313 (progn
314 (if x-dnd-open-file-other-window
315 (find-file-other-window f)
316 (find-file f))
317 'private)
318 (error "Can not read %s (%s)" f uri)))))
320 (defun x-dnd-open-file (uri action)
321 "Open a local or remote file.
322 The file is opened in the current window, or a new window if
323 `x-dnd-open-file-other-window' is set. URI is the url for the file,
324 and must have the format file://hostname/file-name. ACTION is ignored.
325 The last / in file://hostname/ is part of the file name."
327 ;; The hostname may be our hostname, in that case, convert to a local
328 ;; file. Otherwise return nil.
329 (let ((local-file (x-dnd-get-local-file-uri uri)))
330 (when local-file (x-dnd-open-local-file local-file action))))
333 (defun x-dnd-handle-moz-url (window action data)
334 "Handle one item of type text/x-moz-url.
335 WINDOW is the window where the drop happened. ACTION is ignored.
336 DATA is the moz-url, which is formatted as two strings separated by \r\n.
337 The first string is the URL, the second string is the title of that URL.
338 DATA is encoded in utf-16. Decode the URL and call `x-dnd-handle-uri-list'."
339 (let* ((string (decode-coding-string data 'utf-16le)) ;; ALWAYS LE???
340 (strings (split-string string "[\r\n]" t))
341 ;; Can one drop more than one moz-url ?? Assume not.
342 (url (car strings))
343 (title (car (cdr strings))))
344 (x-dnd-handle-uri-list window action url)))
346 (defun x-dnd-insert-utf8-text (window action text)
347 "Decode the UTF-8 text and insert it at point.
348 TEXT is the text as a string, WINDOW is the window where the drop happened."
349 (x-dnd-insert-text window action (decode-coding-string text 'utf-8)))
351 (defun x-dnd-insert-utf16-text (window action text)
352 "Decode the UTF-16 text and insert it at point.
353 TEXT is the text as a string, WINDOW is the window where the drop happened."
354 (x-dnd-insert-text window action (decode-coding-string text 'utf-16le)))
356 (defun x-dnd-insert-ctext (window action text)
357 "Decode the compound text and insert it at point.
358 TEXT is the text as a string, WINDOW is the window where the drop happened."
359 (x-dnd-insert-text window action
360 (decode-coding-string text
361 'compound-text-with-extensions)))
363 (defun x-dnd-insert-text (window action text)
364 "Insert text at point or push to the kill ring if buffer is read only.
365 TEXT is the text as a string, WINDOW is the window where the drop happened."
366 (if (or buffer-read-only
367 (not (windowp window)))
368 (progn
369 (kill-new text)
370 (message
371 (substitute-command-keys
372 "The dropped text can be accessed with \\[yank]")))
373 (insert text))
374 action)
376 (defun x-dnd-handle-uri-list (window action string)
377 "Split an uri-list into separate URIs and call `x-dnd-handle-one-url'.
378 WINDOW is the window where the drop happened.
379 STRING is the uri-list as a string. The URIs are separated by \r\n."
380 (let ((uri-list (split-string string "[\0\r\n]" t))
381 retval)
382 (dolist (bf uri-list)
383 ;; If one URL is handeled, treat as if the whole drop succeeded.
384 (let ((did-action (x-dnd-handle-one-url window action bf)))
385 (when did-action (setq retval did-action))))
386 retval))
388 (defun x-dnd-handle-file-name (window action string)
389 "Prepend file:// to file names and call `x-dnd-handle-one-url'.
390 WINDOW is the window where the drop happened.
391 STRING is the file names as a string, separated by nulls."
392 (let ((uri-list (split-string string "[\0\r\n]" t))
393 retval)
394 (dolist (bf uri-list)
395 ;; If one URL is handeled, treat as if the whole drop succeeded.
396 (let* ((file-uri (concat "file://" bf))
397 (did-action (x-dnd-handle-one-url window action file-uri)))
398 (when did-action (setq retval did-action))))
399 retval))
402 (defun x-dnd-choose-type (types &optional known-types)
403 "Choose which type we want to receive for the drop.
404 TYPES are the types the source of the drop offers, a vector of type names
405 as strings or symbols. Select among the types in `x-dnd-known-types' or
406 KNOWN-TYPES if given, and return that type name.
407 If no suitable type is found, return nil."
408 (let* ((known-list (or known-types x-dnd-known-types))
409 (first-known-type (car known-list))
410 (types-array types)
411 (found (when first-known-type
412 (catch 'done
413 (dotimes (i (length types-array))
414 (let* ((type (aref types-array i))
415 (typename (if (symbolp type)
416 (symbol-name type) type)))
417 (when (equal first-known-type typename)
418 (throw 'done first-known-type))))
419 nil))))
421 (if (and (not found) (cdr known-list))
422 (x-dnd-choose-type types (cdr known-list))
423 found)))
425 (defun x-dnd-drop-data (event frame window data type)
426 "Drop one data item onto a frame.
427 EVENT is the client message for the drop, FRAME is the frame the drop occurred
428 on. WINDOW is the window of FRAME where the drop happened. DATA is the data
429 received from the source, and type is the type for DATA, see
430 `x-dnd-types-alist').
432 Returns the action used (move, copy, link, private) if drop was successful,
433 nil if not."
434 (let* ((type-info (assoc type x-dnd-types-alist))
435 (handler (cdr type-info))
436 (state (x-dnd-get-state-for-frame frame))
437 (action (aref state 5))
438 (w (posn-window (event-start event))))
439 (when handler
440 (if (and (windowp w) (window-live-p w))
441 ;; If dropping in a window, open files in that window rather
442 ;; than in a new widow.
443 (let ((x-dnd-open-file-other-window nil))
444 (goto-char (posn-point (event-start event)))
445 (funcall handler window action data))
446 (let ((x-dnd-open-file-other-window t)) ;; Dropping on non-window.
447 (select-frame frame)
448 (funcall handler window action data))))))
450 (defun x-dnd-handle-drag-n-drop-event (event)
451 "Receive drag and drop events (X client messages).
452 Currently XDND and old KDE 1.x protocols are recognized.
453 TODO: Add Motif and OpenWindows."
454 (interactive "e")
455 (let* ((client-message (car (cdr (cdr event))))
456 (window (posn-window (event-start event)))
457 (message-atom (aref client-message 0))
458 (frame (aref client-message 1))
459 (format (aref client-message 2))
460 (data (aref client-message 3)))
462 (cond ((equal "DndProtocol" message-atom) ; Old KDE 1.x.
463 (x-dnd-handle-old-kde event frame window message-atom format data))
465 ((equal "_MOTIF_DRAG_AND_DROP_MESSAGE" message-atom) ; Motif
466 (x-dnd-handle-motif event frame window message-atom format data))
468 ((and (> (length message-atom) 4) ; XDND protocol.
469 (equal "Xdnd" (substring message-atom 0 4)))
470 (x-dnd-handle-xdnd event frame window message-atom format data)))))
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
474 ;;; Old KDE protocol. Only dropping of files.
476 (defun x-dnd-handle-old-kde (event frame window message format data)
477 "Open the files in a KDE 1.x drop."
478 (let ((values (x-window-property "DndSelection" frame nil 0 t)))
479 (x-dnd-handle-uri-list window 'private
480 (replace-regexp-in-string "\0$" "" values))))
481 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
485 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
486 ;;; XDND protocol.
488 (defvar x-dnd-xdnd-to-action
489 '(("XdndActionPrivate" . private)
490 ("XdndActionCopy" . copy)
491 ("XdndActionMove" . move)
492 ("XdndActionLink" . link)
493 ("XdndActionAsk" . ask))
494 "Mapping from XDND action types to lisp symbols.")
496 (defun x-dnd-init-xdnd-for-frame (frame)
497 "Set the XdndAware property for FRAME to indicate that we do XDND."
498 (x-change-window-property "XdndAware"
499 '(5) ;; The version of XDND we support.
500 frame "ATOM" 32 t))
502 (defun x-dnd-get-drop-width-height (frame w accept)
503 "Return the widht/height to be sent in a XDndStatus message.
504 FRAME is the frame and W is the window where the drop happened.
505 If ACCEPT is nil return 0 (empty rectangle),
506 otherwise if W is a window, return its widht/height,
507 otherwise return the frame width/height."
508 (if accept
509 (if (windowp w) ;; w is not a window if dropping on the menu bar,
510 ;; scroll bar or tool bar.
511 (let ((edges (window-inside-pixel-edges w)))
512 (cons
513 (- (nth 2 edges) (nth 0 edges)) ;; right - left
514 (- (nth 3 edges) (nth 1 edges)))) ;; bottom - top
515 (cons (frame-pixel-width frame)
516 (frame-pixel-height frame)))
519 (defun x-dnd-get-drop-x-y (frame w)
520 "Return the x/y coordinates to be sent in a XDndStatus message.
521 Coordinates are required to be absolute.
522 FRAME is the frame and W is the window where the drop happened.
523 If W is a window, return its absolute corrdinates,
524 otherwise return the frame coordinates."
525 (let* ((frame-left (frame-parameter frame 'left))
526 ;; If the frame is outside the display, frame-left looks like
527 ;; '(0 -16). Extract the -16.
528 (frame-real-left (if (consp frame-left) (car (cdr frame-left))
529 frame-left))
530 (frame-top (frame-parameter frame 'top))
531 (frame-real-top (if (consp frame-top) (car (cdr frame-top))
532 frame-top)))
533 (if (windowp w)
534 (let ((edges (window-inside-pixel-edges w)))
535 (cons
536 (+ frame-real-left (nth 0 edges))
537 (+ frame-real-top (nth 1 edges))))
538 (cons frame-real-left frame-real-top))))
540 (defun x-dnd-handle-xdnd (event frame window message format data)
541 "Receive one XDND event (client message) and send the appropriate reply.
542 EVENT is the client message. FRAME is where the mouse is now.
543 WINDOW is the window within FRAME where the mouse is now.
544 FORMAT is 32 (not used). MESSAGE is the data part of an XClientMessageEvent."
545 (cond ((equal "XdndEnter" message)
546 (let ((version (ash (car (aref data 1)) -8))
547 (more-than-3 (cdr (aref data 1)))
548 (dnd-source (aref data 0)))
549 (x-dnd-save-state
550 window nil nil
551 (if (> more-than-3 0)
552 (x-window-property "XdndTypeList"
553 frame "AnyPropertyType"
554 dnd-source nil t)
555 (vector (x-get-atom-name (aref data 2))
556 (x-get-atom-name (aref data 3))
557 (x-get-atom-name (aref data 4)))))))
559 ((equal "XdndPosition" message)
560 (let* ((x (car (aref data 2)))
561 (y (cdr (aref data 2)))
562 (action (x-get-atom-name (aref data 4)))
563 (dnd-source (aref data 0))
564 (dnd-time (aref data 3))
565 (action-type (x-dnd-maybe-call-test-function
566 window
567 (cdr (assoc action x-dnd-xdnd-to-action))))
568 (reply-action (car (rassoc (car action-type)
569 x-dnd-xdnd-to-action)))
570 (accept ;; 1 = accept, 0 = reject
571 (if (and reply-action action-type) 1 0))
572 (list-to-send
573 (list (string-to-number
574 (frame-parameter frame 'outer-window-id))
575 accept ;; 1 = Accept, 0 = reject.
576 (x-dnd-get-drop-x-y frame window)
577 (x-dnd-get-drop-width-height
578 frame window (eq accept 1))
579 (or reply-action 0)
581 (x-send-client-message
582 frame dnd-source frame "XdndStatus" 32 list-to-send)
585 ((equal "XdndLeave" message)
586 (x-dnd-forget-drop window))
588 ((equal "XdndDrop" message)
589 (if (windowp window) (select-window window))
590 (let* ((dnd-source (aref data 0))
591 (value (and (x-dnd-current-type window)
592 (x-get-selection-internal
593 'XdndSelection
594 (intern (x-dnd-current-type window)))))
595 success action ret-action)
597 (setq action (if value
598 (condition-case info
599 (x-dnd-drop-data event frame window value
600 (x-dnd-current-type window))
601 (error
602 (message "Error: %s" info)
603 nil))))
605 (setq success (if action 1 0))
606 (setq ret-action
607 (if (eq success 1)
608 (or (car (rassoc action x-dnd-xdnd-to-action))
609 "XdndActionPrivate")
612 (x-send-client-message
613 frame dnd-source frame "XdndFinished" 32
614 (list (string-to-number (frame-parameter frame 'outer-window-id))
615 success ;; 1 = Success, 0 = Error
616 (if success "XdndActionPrivate" 0)
618 (x-dnd-forget-drop window)))
620 (t (error "Unknown XDND message %s %s" message data))))
622 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
623 ;;; Motif protocol.
625 (defun x-dnd-init-motif-for-frame (frame)
626 "Set _MOTIF_DRAG_RECEIVER_INFO for FRAME to indicate that we do Motif DND."
627 (x-change-window-property "_MOTIF_DRAG_RECEIVER_INFO"
628 (list
629 (byteorder)
630 0 ; The Motif DND version.
631 5 ; We want drag dynamic.
632 0 0 0 0 0 0 0
633 0 0 0 0 0 0) ; Property must be 16 bytes.
634 frame "_MOTIF_DRAG_RECEIVER_INFO" 8 t))
636 (defun x-dnd-get-motif-value (data offset size byteorder)
637 (cond ((eq size 2)
638 (if (eq byteorder ?l)
639 (+ (ash (aref data (1+ offset)) 8)
640 (aref data offset))
641 (+ (ash (aref data offset) 8)
642 (aref data (1+ offset)))))
644 ((eq size 4)
645 (if (eq byteorder ?l)
646 (cons (+ (ash (aref data (+ 3 offset)) 8)
647 (aref data (+ 2 offset)))
648 (+ (ash (aref data (1+ offset)) 8)
649 (aref data offset)))
650 (cons (+ (ash (aref data offset) 8)
651 (aref data (1+ offset)))
652 (+ (ash (aref data (+ 2 offset)) 8)
653 (aref data (+ 3 offset))))))))
655 (defun x-dnd-motif-value-to-list (value size byteorder)
656 (let ((bytes (cond ((eq size 2)
657 (list (logand (lsh value -8) ?\xff)
658 (logand value ?\xff)))
660 ((eq size 4)
661 (if (consp value)
662 (list (logand (lsh (car value) -8) ?\xff)
663 (logand (car value) ?\xff)
664 (logand (lsh (cdr value) -8) ?\xff)
665 (logand (cdr value) ?\xff))
666 (list (logand (lsh value -24) ?\xff)
667 (logand (lsh value -16) ?\xff)
668 (logand (lsh value -8) ?\xff)
669 (logand value ?\xff)))))))
670 (if (eq byteorder ?l)
671 (reverse bytes)
672 bytes)))
675 (defvar x-dnd-motif-message-types
676 '((0 . XmTOP_LEVEL_ENTER)
677 (1 . XmTOP_LEVEL_LEAVE)
678 (2 . XmDRAG_MOTION)
679 (3 . XmDROP_SITE_ENTER)
680 (4 . XmDROP_SITE_LEAVE)
681 (5 . XmDROP_START)
682 (6 . XmDROP_FINISH)
683 (7 . XmDRAG_DROP_FINISH)
684 (8 . XmOPERATION_CHANGED))
685 "Mapping from numbers to Motif DND message types.")
687 (defvar x-dnd-motif-to-action
688 '((1 . move)
689 (2 . copy)
690 (3 . link) ; Both 3 and 4 has been seen as link.
691 (4 . link)
692 (2 . private)) ; Motif does not have private, so use copy for private.
693 "Mapping from number to operation for Motif DND.")
695 (defun x-dnd-handle-motif (event frame window message-atom format data)
696 (let* ((message-type (cdr (assoc (aref data 0) x-dnd-motif-message-types)))
697 (source-byteorder (aref data 1))
698 (my-byteorder (byteorder))
699 (source-flags (x-dnd-get-motif-value data 2 2 source-byteorder))
700 (source-action (cdr (assoc (logand ?\xF source-flags)
701 x-dnd-motif-to-action))))
703 (cond ((eq message-type 'XmTOP_LEVEL_ENTER)
704 (let* ((dnd-source (x-dnd-get-motif-value
705 data 8 4 source-byteorder))
706 (selection-atom (x-dnd-get-motif-value
707 data 12 4 source-byteorder))
708 (atom-name (x-get-atom-name selection-atom))
709 (types (when atom-name
710 (x-get-selection-internal (intern atom-name)
711 'TARGETS))))
712 (x-dnd-forget-drop frame)
713 (when types (x-dnd-save-state window nil nil
714 types
715 dnd-source))))
717 ;; Can not forget drop here, LEAVE comes before DROP_START and
718 ;; we need the state in DROP_START.
719 ((eq message-type 'XmTOP_LEVEL_LEAVE)
720 nil)
722 ((eq message-type 'XmDRAG_MOTION)
723 (let* ((state (x-dnd-get-state-for-frame frame))
724 (timestamp (x-dnd-motif-value-to-list
725 (x-dnd-get-motif-value data 4 4
726 source-byteorder)
727 4 my-byteorder))
728 (x (x-dnd-motif-value-to-list
729 (x-dnd-get-motif-value data 8 2 source-byteorder)
730 2 my-byteorder))
731 (y (x-dnd-motif-value-to-list
732 (x-dnd-get-motif-value data 10 2 source-byteorder)
733 2 my-byteorder))
734 (dnd-source (aref state 6))
735 (first-move (not (aref state 3)))
736 (action-type (x-dnd-maybe-call-test-function
737 window
738 source-action))
739 (reply-action (car (rassoc (car action-type)
740 x-dnd-motif-to-action)))
741 (reply-flags
742 (x-dnd-motif-value-to-list
743 (if reply-action
744 (+ reply-action
745 ?\x30 ; 30: valid drop site
746 ?\x700) ; 700: can do copy, move or link
747 ?\x30) ; 30: drop site, but noop.
748 2 my-byteorder))
749 (reply (append
750 (list
751 (+ ?\x80 ; 0x80 indicates a reply.
752 (if first-move
753 3 ; First time, reply is SITE_ENTER.
754 2)) ; Not first time, reply is DRAG_MOTION.
755 my-byteorder)
756 reply-flags
757 timestamp
759 y)))
760 (x-send-client-message frame
761 dnd-source
762 frame
763 "_MOTIF_DRAG_AND_DROP_MESSAGE"
765 reply)))
767 ((eq message-type 'XmOPERATION_CHANGED)
768 (let* ((state (x-dnd-get-state-for-frame frame))
769 (timestamp (x-dnd-motif-value-to-list
770 (x-dnd-get-motif-value data 4 4 source-byteorder)
771 4 my-byteorder))
772 (dnd-source (aref state 6))
773 (action-type (x-dnd-maybe-call-test-function
774 window
775 source-action))
776 (reply-action (car (rassoc (car action-type)
777 x-dnd-motif-to-action)))
778 (reply-flags
779 (x-dnd-motif-value-to-list
780 (if reply-action
781 (+ reply-action
782 ?\x30 ; 30: valid drop site
783 ?\x700) ; 700: can do copy, move or link
784 ?\x30) ; 30: drop site, but noop
785 2 my-byteorder))
786 (reply (append
787 (list
788 (+ ?\x80 ; 0x80 indicates a reply.
789 8) ; 8 is OPERATION_CHANGED
790 my-byteorder)
791 reply-flags
792 timestamp)))
793 (x-send-client-message frame
794 dnd-source
795 frame
796 "_MOTIF_DRAG_AND_DROP_MESSAGE"
798 reply)))
800 ((eq message-type 'XmDROP_START)
801 (let* ((x (x-dnd-motif-value-to-list
802 (x-dnd-get-motif-value data 8 2 source-byteorder)
803 2 my-byteorder))
804 (y (x-dnd-motif-value-to-list
805 (x-dnd-get-motif-value data 10 2 source-byteorder)
806 2 my-byteorder))
807 (selection-atom (x-dnd-get-motif-value
808 data 12 4 source-byteorder))
809 (atom-name (x-get-atom-name selection-atom))
810 (dnd-source (x-dnd-get-motif-value
811 data 16 4 source-byteorder))
812 (action-type (x-dnd-maybe-call-test-function
813 window
814 source-action))
815 (reply-action (car (rassoc (car action-type)
816 x-dnd-motif-to-action)))
817 (reply-flags
818 (x-dnd-motif-value-to-list
819 (if reply-action
820 (+ reply-action
821 ?\x30 ; 30: valid drop site
822 ?\x700) ; 700: can do copy, move or link
823 (+ ?\x30 ; 30: drop site, but noop.
824 ?\x200)) ; 200: drop cancel.
825 2 my-byteorder))
826 (reply (append
827 (list
828 (+ ?\x80 ; 0x80 indicates a reply.
829 5) ; DROP_START.
830 my-byteorder)
831 reply-flags
834 (timestamp (x-dnd-get-motif-value
835 data 4 4 source-byteorder))
836 action)
838 (x-send-client-message frame
839 dnd-source
840 frame
841 "_MOTIF_DRAG_AND_DROP_MESSAGE"
843 reply)
844 (setq action
845 (when (and reply-action atom-name)
846 (let* ((value (x-get-selection-internal
847 (intern atom-name)
848 (intern (x-dnd-current-type window)))))
849 (when value
850 (condition-case info
851 (x-dnd-drop-data event frame window value
852 (x-dnd-current-type window))
853 (error
854 (message "Error: %s" info)
855 nil))))))
856 (x-get-selection-internal
857 (intern atom-name)
858 (if action 'XmTRANSFER_SUCCESS 'XmTRANSFER_FAILURE)
859 timestamp)
860 (x-dnd-forget-drop frame)))
862 (t (error "Unknown Motif DND message %s %s" message data)))))
868 (provide 'x-dnd)
870 ;;; arch-tag: b621fb7e-50da-4323-850b-5fc71ae64621
871 ;;; x-dnd.el ends here