Skip to content

Commit

Permalink
Fix drag from 32-bit application drop to 64-bit Notepad2 not working …
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Aug 12, 2018
1 parent e87c16a commit d6aa2f1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
38 changes: 36 additions & 2 deletions scintilla/win32/ScintillaWin.cxx
Expand Up @@ -349,6 +349,7 @@ class ScintillaWin :
int GetCtrlID() override;
void NotifyParent(SCNotification &scn) override;
void NotifyDoubleClick(const Point &pt, int modifiers) override;
void NotifyURIDropped(const char *list);
CaseFolder *CaseFolderForEncoding() override;
std::string CaseMapString(const std::string &s, int caseMapping) override;
void Copy() override;
Expand Down Expand Up @@ -2023,6 +2024,14 @@ void ScintillaWin::NotifyDoubleClick(const Point &pt, int modifiers) {
MAKELPARAM(pt.x, pt.y));
}

void ScintillaWin::NotifyURIDropped(const char *list) {
SCNotification scn = {};
scn.nmhdr.code = SCN_URIDROPPED;
scn.text = list;

NotifyParent(scn);
}

class CaseFolderDBCS : public CaseFolderTable {
// Allocate the expandable storage here so that it does not need to be reallocated
// for each call to Fold.
Expand Down Expand Up @@ -3040,6 +3049,11 @@ STDMETHODIMP ScintillaWin::DragEnter(LPDATAOBJECT pIDataSource, DWORD grfKeyStat
const HRESULT hrHasText = pIDataSource->QueryGetData(&fmte);
hasOKText = (hrHasText == S_OK);
}
if (!hasOKText) {
FORMATETC fmtd = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
const HRESULT hrHasText = pIDataSource->QueryGetData(&fmtd);
hasOKText = (hrHasText == S_OK);
}
if (!hasOKText) {
*pdwEffect = DROPEFFECT_NONE;
return S_OK;
Expand Down Expand Up @@ -3093,6 +3107,7 @@ STDMETHODIMP ScintillaWin::Drop(LPDATAOBJECT pIDataSource, DWORD grfKeyState,
STGMEDIUM medium = { 0, {nullptr}, nullptr };

std::vector<char> data; // Includes terminating NUL
bool fileDrop = false;

FORMATETC fmtu = { CF_UNICODETEXT, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
HRESULT hr = pIDataSource->GetData(&fmtu, &medium);
Expand Down Expand Up @@ -3129,6 +3144,20 @@ STDMETHODIMP ScintillaWin::Drop(LPDATAOBJECT pIDataSource, DWORD grfKeyState,
if (cdata)
data.assign(cdata, cdata + strlen(cdata));
memDrop.Unlock();
} else {
FORMATETC fmtd = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
hr = pIDataSource->GetData(&fmtd, &medium);
if (SUCCEEDED(hr) && medium.hGlobal) {
fileDrop = true;
WCHAR pathDropped[MAX_PATH];
if (::DragQueryFileW(static_cast<HDROP>(medium.hGlobal), 0, pathDropped, MAX_PATH) > 0) {
// Convert UTF-16 to UTF-8
const std::wstring_view wsv(pathDropped);
const size_t dataLen = UTF8Length(wsv);
data.resize(dataLen + 1); // NUL
UTF8FromUTF16(wsv, &data[0], dataLen);
}
}
}
}

Expand All @@ -3137,6 +3166,9 @@ STDMETHODIMP ScintillaWin::Drop(LPDATAOBJECT pIDataSource, DWORD grfKeyState,
return hr;
}

if (fileDrop) {
NotifyURIDropped(data.data());
} else {
FORMATETC fmtr = { cfColumnSelect, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
const HRESULT hrRectangular = pIDataSource->QueryGetData(&fmtr);

Expand All @@ -3145,6 +3177,7 @@ STDMETHODIMP ScintillaWin::Drop(LPDATAOBJECT pIDataSource, DWORD grfKeyState,
const SelectionPosition movePos = SPositionFromLocation(PointFromPOINT(rpt), false, false, UserVirtualSpace());

DropAt(movePos, &data[0], data.size(), *pdwEffect == DROPEFFECT_MOVE, hrRectangular == S_OK);
}

// Free data
if (medium.pUnkForRelease)
Expand All @@ -3162,7 +3195,8 @@ STDMETHODIMP ScintillaWin::Drop(LPDATAOBJECT pIDataSource, DWORD grfKeyState,
/// Implement important part of IDataObject
STDMETHODIMP ScintillaWin::GetData(FORMATETC *pFEIn, STGMEDIUM *pSTM) {
const bool formatOK = (pFEIn->cfFormat == CF_TEXT) ||
((pFEIn->cfFormat == CF_UNICODETEXT) && IsUnicodeMode());
((pFEIn->cfFormat == CF_UNICODETEXT) && IsUnicodeMode()) ||
(pFEIn->cfFormat == CF_HDROP);
if (!formatOK ||
pFEIn->ptd != nullptr ||
(pFEIn->dwAspect & DVASPECT_CONTENT) == 0 ||
Expand All @@ -3183,7 +3217,7 @@ STDMETHODIMP ScintillaWin::GetData(FORMATETC *pFEIn, STGMEDIUM *pSTM) {
if (text) {
UTF16FromUTF8(sv, static_cast<wchar_t *>(text.ptr), uchars);
}
} else {
} else if (pFEIn->cfFormat == CF_TEXT) {
text.Allocate(drag.LengthWithTerminator());
if (text) {
memcpy(text.ptr, drag.Data(), drag.LengthWithTerminator());
Expand Down
20 changes: 20 additions & 0 deletions src/Notepad2.c
Expand Up @@ -5020,6 +5020,26 @@ LRESULT MsgNotify(HWND hwnd, WPARAM wParam, LPARAM lParam) {
iPathNameFormat, bModified || iEncoding != iOriginalEncoding,
IDS_READONLY, bReadOnly, szTitleExcerpt);
break;

case SCN_URIDROPPED: {
// see WM_DROPFILES
WCHAR szBuf[MAX_PATH + 40];
if (MultiByteToWideChar(CP_UTF8, 0, scn->text, -1, szBuf, COUNTOF(szBuf)) > 0) {
if (IsIconic(hwnd)) {
ShowWindow(hwnd, SW_RESTORE);
}
//SetForegroundWindow(hwnd);
if (PathIsDirectory(szBuf)) {
WCHAR tchFile[MAX_PATH];
if (OpenFileDlg(hwndMain, tchFile, COUNTOF(tchFile), szBuf)) {
FileLoad(FALSE, FALSE, FALSE, FALSE, tchFile);
}
} else {
FileLoad(FALSE, FALSE, FALSE, FALSE, szBuf);
}
}
}
break;
}
break;

Expand Down

0 comments on commit d6aa2f1

Please sign in to comment.