file-name))
(defun w32-drag-n-drop (event &optional new-frame)
- "Edit the files listed in the drag-n-drop EVENT.
-Switch to a buffer editing the last file dropped."
+ "Perform drag-n-drop action according to data in EVENT.
+If EVENT is for one or more files, visit those files in corresponding
+buffers, and switch to the buffer that visits the last dropped file.
+If EVENT is for text, insert that text at point into the buffer
+shown in the window that is the target of the drop; if that buffer is
+read-only, add the dropped text to kill-ring.
+If the optional argument NEW-FRAME is non-nil, perform the
+drag-n-drop action in a newly-created frame using its selected-window
+and that window's buffer."
(interactive "e")
(save-excursion
;; Make sure the drop target has positive co-ords
;; won't work. <skx@tardis.ed.ac.uk>
(let* ((window (posn-window (event-start event)))
(coords (posn-x-y (event-start event)))
+ (arg (car (cdr (cdr event))))
(x (car coords))
(y (cdr coords)))
(if (and (> x 0) (> y 0))
(raise-frame)
(setq window (selected-window))
- (dnd-handle-multiple-urls
- window
- (mapcar #'w32-dropped-file-to-url
- (car (cdr (cdr event))))
- 'private))))
+ ;; arg (the payload of the event) is a string when the drop is
+ ;; text, and a list of strings when the drop is one or more files.
+ (if (stringp arg)
+ (dnd-insert-text window 'copy arg)
+ (dnd-handle-multiple-urls
+ window
+ (mapcar #'w32-dropped-file-to-url arg)
+ 'private)))))
(defun w32-drag-n-drop-other-frame (event)
"Edit the files listed in the drag-n-drop EVENT, in other frames.
#include <c-ctype.h>
+#define COBJMACROS /* Ask for C definitions for COM. */
+#include <shlobj.h>
+#include <oleidl.h>
+#include <objidl.h>
+#include <ole2.h>
+
#include "lisp.h"
#include "w32term.h"
#include "frame.h"
static struct w32_display_info *w32_display_info_for_name (Lisp_Object);
+static void my_post_msg (W32Msg*, HWND, UINT, WPARAM, LPARAM);
+static unsigned int w32_get_modifiers (void);
+
/* Let the user specify a display with a frame.
nil stands for the selected frame--or, if that is not a w32 frame,
the first display on the list. */
return hwnd;
}
+/* From the DROPFILES struct, extract the filenames and return as a list
+ of strings. */
+static Lisp_Object
+process_dropfiles (DROPFILES *files)
+{
+ char *start_of_files = (char *) files + files->pFiles;
+ char filename[MAX_UTF8_PATH];
+ Lisp_Object lisp_files = Qnil;
+
+ if (files->fWide)
+ {
+ WCHAR *p = (WCHAR *) start_of_files;
+ for (; *p; p += wcslen (p) + 1)
+ {
+ filename_from_utf16(p, filename);
+ lisp_files = Fcons (DECODE_FILE (build_unibyte_string (filename)),
+ lisp_files );
+ }
+ }
+ else
+ {
+ char *p = start_of_files;
+ for (; *p; p += strlen(p) + 1)
+ {
+ filename_from_ansi (p, filename);
+ lisp_files = Fcons (DECODE_FILE (build_unibyte_string (filename)),
+ lisp_files );
+ }
+ }
+ return lisp_files;
+}
+
+
+/* This function can be called ONLY between calls to
+ block_input/unblock_input. It is used in w32_read_socket. */
+Lisp_Object
+w32_process_dnd_data (int format, void *hGlobal)
+{
+ Lisp_Object result = Qnil;
+ HGLOBAL hg = (HGLOBAL) hGlobal;
+
+ switch (format)
+ {
+ case CF_HDROP:
+ {
+ DROPFILES *files = (DROPFILES *) GlobalLock (hg);
+ if (files)
+ result = process_dropfiles (files);
+ GlobalUnlock (hg);
+ break;
+ }
+ case CF_UNICODETEXT:
+ {
+ WCHAR *text = (WCHAR *) GlobalLock (hg);
+ result = from_unicode_buffer (text);
+ GlobalUnlock (hg);
+ break;
+ }
+ case CF_TEXT:
+ {
+ char *text = (char *) GlobalLock (hg);
+ result = DECODE_SYSTEM (build_unibyte_string (text));
+ GlobalUnlock (hg);
+ break;
+ }
+ }
+
+ GlobalFree (hg);
+
+ return result;
+}
+
+struct w32_drop_target {
+ /* i_drop_target must be the first member. */
+ IDropTarget i_drop_target;
+ HWND hwnd;
+};
+
+static HRESULT STDMETHODCALLTYPE
+w32_drop_target_QueryInterface (IDropTarget *t, REFIID ri, void **r)
+{
+ return E_NOINTERFACE;
+}
+
+static ULONG STDMETHODCALLTYPE
+w32_drop_target_AddRef (IDropTarget *This)
+{
+ return 1;
+}
+
+static ULONG STDMETHODCALLTYPE
+w32_drop_target_Release (IDropTarget *This)
+{
+ struct w32_drop_target *target = (struct w32_drop_target * ) This;
+ free (target->i_drop_target.lpVtbl);
+ free (target);
+ return 0;
+}
+
+static HRESULT STDMETHODCALLTYPE
+w32_drop_target_DragEnter (IDropTarget *This, IDataObject *pDataObj,
+ DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
+{
+ /* Possible 'effect' values are COPY, MOVE, LINK or NONE. This choice
+ changes the mouse pointer shape to inform the user of what will
+ happen on drop. We send COPY because our use cases don't modify
+ or link to the original data. */
+ *pdwEffect = DROPEFFECT_COPY;
+ return S_OK;
+}
+
+static HRESULT STDMETHODCALLTYPE
+w32_drop_target_DragOver (IDropTarget *This, DWORD grfKeyState, POINTL pt,
+ DWORD *pdwEffect)
+{
+ /* See comment in w32_drop_target_DragEnter. */
+ *pdwEffect = DROPEFFECT_COPY;
+ return S_OK;
+}
+
+static HRESULT STDMETHODCALLTYPE
+w32_drop_target_DragLeave (IDropTarget *This)
+{
+ return S_OK;
+}
+
+static HGLOBAL w32_try_get_data (IDataObject *pDataObj, int format)
+{
+ FORMATETC formatetc = { format, NULL, DVASPECT_CONTENT, -1,
+ TYMED_HGLOBAL };
+ STGMEDIUM stgmedium;
+ HRESULT r = IDataObject_GetData (pDataObj, &formatetc, &stgmedium);
+ if (SUCCEEDED (r))
+ {
+ if (stgmedium.tymed == TYMED_HGLOBAL)
+ return stgmedium.hGlobal;
+ ReleaseStgMedium (&stgmedium);
+ }
+ return NULL;
+}
+
+static HRESULT STDMETHODCALLTYPE
+w32_drop_target_Drop (IDropTarget *This, IDataObject *pDataObj,
+ DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
+{
+ struct w32_drop_target *target = (struct w32_drop_target *)This;
+ *pdwEffect = DROPEFFECT_COPY;
+
+ W32Msg msg = {0};
+ msg.dwModifiers = w32_get_modifiers();
+ msg.msg.time = GetMessageTime ();
+ msg.msg.pt.x = pt.x;
+ msg.msg.pt.y = pt.y;
+
+ int format = CF_HDROP;
+ HGLOBAL hGlobal = w32_try_get_data (pDataObj, format);
+
+ if (!hGlobal)
+ {
+ format = CF_UNICODETEXT;
+ hGlobal = w32_try_get_data (pDataObj, format);
+ }
+
+ if (!hGlobal)
+ {
+ format = CF_TEXT;
+ hGlobal = w32_try_get_data (pDataObj, format);
+ }
+
+ if (hGlobal)
+ my_post_msg (&msg, target->hwnd, WM_EMACS_DROP, format,
+ (LPARAM) hGlobal);
+
+ return S_OK;
+}
+
static void
w32_createwindow (struct frame *f, int *coords)
{
SetWindowLong (hwnd, WND_BACKGROUND_INDEX, FRAME_BACKGROUND_PIXEL (f));
/* Enable drag-n-drop. */
- DragAcceptFiles (hwnd, TRUE);
+ struct w32_drop_target *drop_target =
+ malloc (sizeof (struct w32_drop_target));
+
+ if (drop_target != NULL)
+ {
+ IDropTargetVtbl *vtbl = malloc (sizeof (IDropTargetVtbl));
+ if (vtbl != NULL)
+ {
+ drop_target->hwnd = hwnd;
+ drop_target->i_drop_target.lpVtbl = vtbl;
+ vtbl->QueryInterface = w32_drop_target_QueryInterface;
+ vtbl->AddRef = w32_drop_target_AddRef;
+ vtbl->Release = w32_drop_target_Release;
+ vtbl->DragEnter = w32_drop_target_DragEnter;
+ vtbl->DragOver = w32_drop_target_DragOver;
+ vtbl->DragLeave = w32_drop_target_DragLeave;
+ vtbl->Drop = w32_drop_target_Drop;
+ RegisterDragDrop (hwnd, &drop_target->i_drop_target);
+ }
+ else
+ {
+ free (drop_target);
+ }
+ }
/* Enable system light/dark theme. */
w32_applytheme (hwnd);
M (WM_EMACS_PAINT),
M (WM_EMACS_IME_STATUS),
M (WM_CHAR),
+ M (WM_EMACS_DROP),
#undef M
{ 0, 0 }
};
/* Produced by complete_deferred_msg; just ignore. */
break;
case WM_EMACS_CREATEWINDOW:
- /* Initialize COM for this window. Even though we don't use it,
- some third party shell extensions can cause it to be used in
+ /* Initialize COM for this window. Needed for RegisterDragDrop.
+ Some third party shell extensions can cause it to be used in
system dialogs, which causes a crash if it is not initialized.
This is a known bug in Windows, which was fixed long ago, but
the patch for XP is not publicly available until XP SP3,
and older versions will never be patched. */
- CoInitialize (NULL);
+ OleInitialize (NULL);
+
w32_createwindow ((struct frame *) msg.wParam,
(int *) msg.lParam);
if (!PostThreadMessage (dwMainThreadId, WM_EMACS_DONE, 0, 0))
return 0;
case WM_MOUSEWHEEL:
- case WM_DROPFILES:
wmsg.dwModifiers = w32_get_modifiers ();
my_post_msg (&wmsg, hwnd, msg, wParam, lParam);
signal_user_input ();
}
case WM_EMACS_DESTROYWINDOW:
- DragAcceptFiles ((HWND) wParam, FALSE);
+ RevokeDragDrop ((HWND) wParam);
return DestroyWindow ((HWND) wParam);
case WM_EMACS_HIDE_CARET:
return Qnil;
}
-static Lisp_Object
-w32_construct_drag_n_drop (struct input_event *result, W32Msg *msg,
- struct frame *f)
-{
- Lisp_Object files;
- Lisp_Object frame;
- HDROP hdrop;
- POINT p;
- WORD num_files;
- wchar_t name_w[MAX_PATH];
-#ifdef NTGUI_UNICODE
- const int use_unicode = 1;
-#else
- int use_unicode = w32_unicode_filenames;
- char name_a[MAX_PATH];
- char file[MAX_UTF8_PATH];
-#endif
- int i;
-
- result->kind = DRAG_N_DROP_EVENT;
- result->code = 0;
- result->timestamp = msg->msg.time;
- result->modifiers = msg->dwModifiers;
-
- hdrop = (HDROP) msg->msg.wParam;
- DragQueryPoint (hdrop, &p);
-
-#if 0
- p.x = LOWORD (msg->msg.lParam);
- p.y = HIWORD (msg->msg.lParam);
- ScreenToClient (msg->msg.hwnd, &p);
-#endif
-
- XSETINT (result->x, p.x);
- XSETINT (result->y, p.y);
-
- num_files = DragQueryFile (hdrop, 0xFFFFFFFF, NULL, 0);
- files = Qnil;
-
- for (i = 0; i < num_files; i++)
- {
- if (use_unicode)
- {
- eassert (DragQueryFileW (hdrop, i, NULL, 0) < MAX_PATH);
- /* If DragQueryFile returns zero, it failed to fetch a file
- name. */
- if (DragQueryFileW (hdrop, i, name_w, MAX_PATH) == 0)
- continue;
-#ifdef NTGUI_UNICODE
- files = Fcons (from_unicode_buffer (name_w), files);
-#else
- filename_from_utf16 (name_w, file);
- files = Fcons (DECODE_FILE (build_unibyte_string (file)), files);
-#endif /* NTGUI_UNICODE */
- }
-#ifndef NTGUI_UNICODE
- else
- {
- eassert (DragQueryFileA (hdrop, i, NULL, 0) < MAX_PATH);
- if (DragQueryFileA (hdrop, i, name_a, MAX_PATH) == 0)
- continue;
- filename_from_ansi (name_a, file);
- files = Fcons (DECODE_FILE (build_unibyte_string (file)), files);
- }
-#endif
- }
-
- DragFinish (hdrop);
-
- XSETFRAME (frame, f);
- result->frame_or_window = frame;
- result->arg = files;
- return Qnil;
-}
-
\f
#if HAVE_W32NOTIFY
}
break;
- case WM_DROPFILES:
- f = w32_window_to_frame (dpyinfo, msg.msg.hwnd);
+ case WM_EMACS_DROP:
+ {
+ int format = msg.msg.wParam;
+ Lisp_Object drop_object =
+ w32_process_dnd_data (format, (void *) msg.msg.lParam);
- if (f)
- w32_construct_drag_n_drop (&inev, &msg, f);
+ f = w32_window_to_frame (dpyinfo, msg.msg.hwnd);
+ if (!f || NILP (drop_object))
+ break;
+
+ XSETFRAME (inev.frame_or_window, f);
+ inev.kind = DRAG_N_DROP_EVENT;
+ inev.code = 0;
+ inev.timestamp = msg.msg.time;
+ inev.modifiers = msg.dwModifiers;
+ ScreenToClient (msg.msg.hwnd, &msg.msg.pt);
+ XSETINT (inev.x, msg.msg.pt.x);
+ XSETINT (inev.y, msg.msg.pt.y);
+ inev.arg = drop_object;
+ }
break;
case WM_HSCROLL: