From dd4bd5b17ddedcaf64df09f7a10d34267b8834c3 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Mon, 20 Jan 2020 23:25:53 -0500 Subject: shift+right click for shell menu --- src/envshell.cpp | 212 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 src/envshell.cpp (limited to 'src/envshell.cpp') diff --git a/src/envshell.cpp b/src/envshell.cpp new file mode 100644 index 00000000..f7033fba --- /dev/null +++ b/src/envshell.cpp @@ -0,0 +1,212 @@ +#include "envshell.h" +#include "env.h" +#include +#include + +namespace env +{ + +using namespace MOBase; + +const int QCM_FIRST = 1; +const int QCM_LAST = 0x7ff; + +class MenuFailed : public std::runtime_error +{ +public: + MenuFailed(HRESULT r, const std::string& what) + : runtime_error(fmt::format( + "{}, {}", + what, QString::fromStdWString(formatSystemMessage(r)).toStdString())) + { + } +}; + + +class WndProcFilter : public QAbstractNativeEventFilter +{ +public: + WndProcFilter(IContextMenu* cm) + : m_cm2(nullptr), m_cm3(nullptr) + { + IContextMenu2* cm2 = nullptr; + if (SUCCEEDED(cm->QueryInterface(IID_IContextMenu2, (void**)&cm2))) { + m_cm2.reset(cm2); + } + + IContextMenu3* cm3 = nullptr; + if (SUCCEEDED(cm->QueryInterface(IID_IContextMenu3, (void**)&cm3))) { + m_cm3.reset(cm3); + } + } + + bool nativeEventFilter(const QByteArray& type, void* m, long* lresultOut) override + { + if (m_cm3) { + MSG* msg = (MSG*)m; + LRESULT lresult = 0; + + const auto r = m_cm3->HandleMenuMsg2( + msg->message, msg->wParam, msg->lParam, &lresult); + + if (SUCCEEDED(r)) { + if (lresultOut) { + *lresultOut = lresult; + } + + return true; + } + } + + if (m_cm2) { + MSG* msg = (MSG*)m; + + const auto r = m_cm2->HandleMenuMsg( + msg->message, msg->wParam, msg->lParam); + + if (SUCCEEDED(r)) { + if (lresultOut) { + *lresultOut = 0; + } + + return true; + } + } + + return false; + } + +private: + COMPtr m_cm2; + COMPtr m_cm3; +}; + + + +CoTaskMemPtr getIDL(const wchar_t* path) +{ + LPITEMIDLIST pidl; + SFGAOF sfgao; + + const auto r = SHParseDisplayName(path, nullptr, &pidl, 0, &sfgao); + + if (FAILED(r)) { + throw MenuFailed(r, "SHParseDisplayName failed"); + } + + return CoTaskMemPtr(pidl); +} + +std::pair, LPCITEMIDLIST> getShellFolder(LPITEMIDLIST idl) +{ + IShellFolder* psf = nullptr; + LPCITEMIDLIST pidlChild = nullptr; + + const auto r = SHBindToParent( + idl, IID_IShellFolder, reinterpret_cast(&psf), &pidlChild); + + if (FAILED(r)) { + throw MenuFailed(r, "SHBindToParent failed"); + } + + return {COMPtr(psf), pidlChild}; +} + +COMPtr getContextMenu(IShellFolder* psf, LPCITEMIDLIST idl) +{ + IContextMenu* pcm = nullptr; + + const auto r = psf->GetUIObjectOf( + 0, 1, &idl, IID_IContextMenu, nullptr, + reinterpret_cast(&pcm)); + + if (FAILED(r)) { + throw MenuFailed(r, "GetUIObjectOf failed"); + } + + return COMPtr(pcm); +} + +HMenuPtr createMenu(IContextMenu* cm) +{ + HMENU hmenu = CreatePopupMenu(); + if (!hmenu) { + const auto e = GetLastError(); + throw MenuFailed(e, "CreatePopupMenu failed"); + } + + const auto r = cm->QueryContextMenu( + hmenu, 0, QCM_FIRST, QCM_LAST, CMF_EXTENDEDVERBS); + + if (FAILED(r)) { + throw MenuFailed(r, "QueryContextMenu failed"); + } + + return HMenuPtr(hmenu); +} + +int runMenu(IContextMenu* cm, HWND hwnd, HMENU menu, const QPoint& p) +{ + auto filter = std::make_unique(cm); + QCoreApplication::instance()->installNativeEventFilter(filter.get()); + + return TrackPopupMenuEx(menu, TPM_RETURNCMD, p.x(), p.y(), hwnd, nullptr); +} + +void invoke(HWND hwnd, const QPoint& p, int cmd, IContextMenu* cm) +{ + CMINVOKECOMMANDINFOEX info = {}; + + info.cbSize = sizeof(info); + info.fMask = CMIC_MASK_UNICODE | CMIC_MASK_PTINVOKE; + info.hwnd = hwnd; + info.lpVerb = MAKEINTRESOURCEA(cmd); + info.lpVerbW = MAKEINTRESOURCEW(cmd); + info.nShow = SW_SHOWNORMAL; + info.ptInvoke = {p.x(), p.y()}; + + // note: this calls the query version because the Qt even loop hasn't run + // yet and shift is still considered pressed + const auto m = QApplication::queryKeyboardModifiers(); + + if (m & Qt::ShiftModifier) { + info.fMask |= CMIC_MASK_SHIFT_DOWN; + } + + if (m & Qt::ControlModifier) { + info.fMask |= CMIC_MASK_CONTROL_DOWN; + } + + const auto r = cm->InvokeCommand((CMINVOKECOMMANDINFO*)&info); + + if (FAILED(r)) { + throw MenuFailed(r, fmt::format("InvokeCommand failed, verb={}", cmd)); + } +} + +void showShellMenu(QWidget* parent, const QFileInfo& file, const QPoint& pos) +{ + const auto path = QDir::toNativeSeparators(file.absoluteFilePath()); + + try + { + auto idl = getIDL(path.toStdWString().c_str()); + auto [sf, childIdl] = getShellFolder(idl.get()); + auto cm = getContextMenu(sf.get(), childIdl); + auto hmenu = createMenu(cm.get()); + auto hwnd = (HWND)parent->window()->winId(); + + const int cmd = runMenu(cm.get(), hwnd, hmenu.get(), pos); + if (cmd <= 0) { + return; + } + + invoke(hwnd, pos, cmd - QCM_FIRST, cm.get()); + } + catch(MenuFailed& e) + { + log::error("can't create shell menu for '{}': {}", path, e.what()); + } +} + +} // namespace -- cgit v1.3.1 From abdf98bbfe5b9a5635a158d4554e1b6e1155789b Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Mon, 20 Jan 2020 23:50:00 -0500 Subject: status bar messages --- src/envshell.cpp | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++---- src/pch.h | 1 + 2 files changed, 139 insertions(+), 11 deletions(-) (limited to 'src/envshell.cpp') diff --git a/src/envshell.cpp b/src/envshell.cpp index f7033fba..ca392a9c 100644 --- a/src/envshell.cpp +++ b/src/envshell.cpp @@ -2,6 +2,7 @@ #include "env.h" #include #include +#include namespace env { @@ -26,8 +27,8 @@ public: class WndProcFilter : public QAbstractNativeEventFilter { public: - WndProcFilter(IContextMenu* cm) - : m_cm2(nullptr), m_cm3(nullptr) + WndProcFilter(QMainWindow* mw, IContextMenu* cm) + : m_mw(mw), m_cm(cm), m_cm2(nullptr), m_cm3(nullptr) { IContextMenu2* cm2 = nullptr; if (SUCCEEDED(cm->QueryInterface(IID_IContextMenu2, (void**)&cm2))) { @@ -40,10 +41,23 @@ public: } } + ~WndProcFilter() + { + if (auto* sb=m_mw->statusBar()) { + sb->clearMessage(); + } + } + bool nativeEventFilter(const QByteArray& type, void* m, long* lresultOut) override { + MSG* msg = (MSG*)m; + + if (msg->message == WM_MENUSELECT) { + HANDLE_WM_MENUSELECT(msg->hwnd, msg->wParam, msg->lParam, onMenuSelect); + return true; + } + if (m_cm3) { - MSG* msg = (MSG*)m; LRESULT lresult = 0; const auto r = m_cm3->HandleMenuMsg2( @@ -59,8 +73,6 @@ public: } if (m_cm2) { - MSG* msg = (MSG*)m; - const auto r = m_cm2->HandleMenuMsg( msg->message, msg->wParam, msg->lParam); @@ -77,8 +89,104 @@ public: } private: + QMainWindow* m_mw; + IContextMenu* m_cm; COMPtr m_cm2; COMPtr m_cm3; + + // adapted from + // https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723 + // + void onMenuSelect( + HWND hwnd, HMENU hmenu, int item, HMENU hmenuPopup, UINT flags) + { + if (m_cm && item >= QCM_FIRST && item <= QCM_LAST) { + WCHAR szBuf[MAX_PATH]; + + const auto r = IContextMenu_GetCommandString( + m_cm, item - QCM_FIRST, GCS_HELPTEXTW, NULL, szBuf, MAX_PATH); + + if (FAILED(r)) { + lstrcpynW(szBuf, L"No help available.", MAX_PATH); + } + + if (m_mw) { + if (auto* sb=m_mw->statusBar()) { + sb->showMessage(QString::fromWCharArray(szBuf)); + } + } + } + } + + // adapted from + // https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723 + // + HRESULT IContextMenu_GetCommandString( + IContextMenu *pcm, UINT_PTR idCmd, UINT uFlags, + UINT *pwReserved, LPWSTR pszName, UINT cchMax) + { + // Callers are expected to be using Unicode. + if (!(uFlags & GCS_UNICODE)) { + return E_INVALIDARG; + } + + // Some context menu handlers have off-by-one bugs and will + // overflow the output buffer. Let’s artificially reduce the + // buffer size so a one-character overflow won’t corrupt memory. + if (cchMax <= 1) { + return E_FAIL; + } + + cchMax--; + + // First try the Unicode message. Preset the output buffer + // with a known value because some handlers return S_OK without + // doing anything. + pszName[0] = L'\0'; + + HRESULT hr = pcm->GetCommandString( + idCmd, uFlags, pwReserved, (LPSTR)pszName, cchMax); + + if (SUCCEEDED(hr) && pszName[0] == L'\0') { + // Rats, a buggy IContextMenu handler that returned success + // even though it failed. + hr = E_NOTIMPL; + } + + if (FAILED(hr)) { + // try again with ANSI – pad the buffer with one extra character + // to compensate for context menu handlers that overflow by + // one character. + LPSTR pszAnsi = (LPSTR)LocalAlloc( + LMEM_FIXED, (cchMax + 1) * sizeof(CHAR)); + + if (pszAnsi) { + pszAnsi[0] = '\0'; + + hr = pcm->GetCommandString( + idCmd, uFlags & ~GCS_UNICODE, pwReserved, pszAnsi, cchMax); + + if (SUCCEEDED(hr) && pszAnsi[0] == '\0') { + // Rats, a buggy IContextMenu handler that returned success + // even though it failed. + hr = E_NOTIMPL; + } + + if (SUCCEEDED(hr)) { + if (MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, pszName, cchMax) == 0) { + hr = E_FAIL; + } + } + + LocalFree(pszAnsi); + + } else { + hr = E_OUTOFMEMORY; + } + } + + return hr; + } }; @@ -145,16 +253,20 @@ HMenuPtr createMenu(IContextMenu* cm) return HMenuPtr(hmenu); } -int runMenu(IContextMenu* cm, HWND hwnd, HMENU menu, const QPoint& p) +int runMenu(QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p) { - auto filter = std::make_unique(cm); + const auto hwnd = (HWND)mw->winId(); + + auto filter = std::make_unique(mw, cm); QCoreApplication::instance()->installNativeEventFilter(filter.get()); return TrackPopupMenuEx(menu, TPM_RETURNCMD, p.x(), p.y(), hwnd, nullptr); } -void invoke(HWND hwnd, const QPoint& p, int cmd, IContextMenu* cm) +void invoke(QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm) { + const auto hwnd = (HWND)mw->winId(); + CMINVOKECOMMANDINFOEX info = {}; info.cbSize = sizeof(info); @@ -184,24 +296,39 @@ void invoke(HWND hwnd, const QPoint& p, int cmd, IContextMenu* cm) } } +QMainWindow* getMainWindow(QWidget* w) +{ + QWidget* p = w; + + while (p) { + if (auto* mw=dynamic_cast(p)) { + return mw; + } + + p = p->parentWidget(); + } + + return nullptr; +} + void showShellMenu(QWidget* parent, const QFileInfo& file, const QPoint& pos) { const auto path = QDir::toNativeSeparators(file.absoluteFilePath()); try { + auto* mw = getMainWindow(parent); auto idl = getIDL(path.toStdWString().c_str()); auto [sf, childIdl] = getShellFolder(idl.get()); auto cm = getContextMenu(sf.get(), childIdl); auto hmenu = createMenu(cm.get()); - auto hwnd = (HWND)parent->window()->winId(); - const int cmd = runMenu(cm.get(), hwnd, hmenu.get(), pos); + const int cmd = runMenu(mw, cm.get(), hmenu.get(), pos); if (cmd <= 0) { return; } - invoke(hwnd, pos, cmd - QCM_FIRST, cm.get()); + invoke(mw, pos, cmd - QCM_FIRST, cm.get()); } catch(MenuFailed& e) { diff --git a/src/pch.h b/src/pch.h index 8e8d33f7..030ee634 100644 --- a/src/pch.h +++ b/src/pch.h @@ -38,6 +38,7 @@ #include #include #include +#include // boost #include -- cgit v1.3.1 From e3211683fd75b4c297f2f670819ad5dacb18a19c Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Tue, 21 Jan 2020 23:45:11 -0500 Subject: shell menu for multiple files --- src/envshell.cpp | 164 +++++++++++++++++++++++++++++++++----------- src/envshell.h | 6 +- src/filetree.cpp | 30 ++++---- src/filetree.h | 3 +- src/mainwindow.ui | 3 + src/shared/directoryentry.h | 2 +- 6 files changed, 153 insertions(+), 55 deletions(-) (limited to 'src/envshell.cpp') diff --git a/src/envshell.cpp b/src/envshell.cpp index ca392a9c..dcf337c5 100644 --- a/src/envshell.cpp +++ b/src/envshell.cpp @@ -24,6 +24,24 @@ public: }; +struct IdlsFreer +{ + const std::vector& v; + + IdlsFreer(const std::vector& v) + : v(v) + { + } + + ~IdlsFreer() + { + for (auto&& idl : v) { + ::CoTaskMemFree(const_cast(idl)); + } + } +}; + + class WndProcFilter : public QAbstractNativeEventFilter { public: @@ -191,48 +209,103 @@ private: -CoTaskMemPtr getIDL(const wchar_t* path) +QMainWindow* getMainWindow(QWidget* w) { - LPITEMIDLIST pidl; - SFGAOF sfgao; + QWidget* p = w; - const auto r = SHParseDisplayName(path, nullptr, &pidl, 0, &sfgao); + while (p) { + if (auto* mw=dynamic_cast(p)) { + return mw; + } + + p = p->parentWidget(); + } + + return nullptr; +} + +COMPtr createShellItem(const std::wstring& path) +{ + IShellItem* item = nullptr; + + auto r = SHCreateItemFromParsingName( + path.c_str(), nullptr, IID_IShellItem, (void**)&item); if (FAILED(r)) { - throw MenuFailed(r, "SHParseDisplayName failed"); + throw MenuFailed(r, "SHCreateItemFromParsingName failed"); } - return CoTaskMemPtr(pidl); + return COMPtr(item); } -std::pair, LPCITEMIDLIST> getShellFolder(LPITEMIDLIST idl) +COMPtr getPersistIDList(IShellItem* item) { - IShellFolder* psf = nullptr; - LPCITEMIDLIST pidlChild = nullptr; + IPersistIDList* idl = nullptr; + auto r = item->QueryInterface(IID_IPersistIDList, (void**)&idl); + + if (FAILED(r)) { + throw MenuFailed(r, "QueryInterface IID_IPersistIDList failed"); + } - const auto r = SHBindToParent( - idl, IID_IShellFolder, reinterpret_cast(&psf), &pidlChild); + return COMPtr(idl); +} + +CoTaskMemPtr getIDList(IPersistIDList* pidlist) +{ + LPITEMIDLIST absIdl = nullptr; + auto r = pidlist->GetIDList(&absIdl); if (FAILED(r)) { - throw MenuFailed(r, "SHBindToParent failed"); + throw MenuFailed(r, "GetIDList failed"); } - return {COMPtr(psf), pidlChild}; + return CoTaskMemPtr(absIdl); } -COMPtr getContextMenu(IShellFolder* psf, LPCITEMIDLIST idl) +std::vector createIdls( + const std::vector& files) { - IContextMenu* pcm = nullptr; + std::vector idls; + + for (auto&& f : files) { + const auto path = QDir::toNativeSeparators(f.absoluteFilePath()).toStdWString(); - const auto r = psf->GetUIObjectOf( - 0, 1, &idl, IID_IContextMenu, nullptr, - reinterpret_cast(&pcm)); + auto item = createShellItem(path); + auto pidlist = getPersistIDList(item.get()); + auto absIdl = getIDList(pidlist.get()); + + idls.push_back(absIdl.release()); + } + + return idls; +} + +COMPtr createItemArray( + std::vector& idls) +{ + IShellItemArray* array = nullptr; + auto r = SHCreateShellItemArrayFromIDLists( + static_cast(idls.size()), &idls[0], &array); + + if (FAILED(r)) { + throw MenuFailed(r, "SHCreateShellItemArrayFromIDLists failed"); + } + + return COMPtr(array); +} + +COMPtr createContextMenu(IShellItemArray* array) +{ + IContextMenu* cm = nullptr; + + auto r = array->BindToHandler( + nullptr, BHID_SFUIObject, IID_IContextMenu, (void**)&cm); if (FAILED(r)) { - throw MenuFailed(r, "GetUIObjectOf failed"); + throw MenuFailed(r, "BindToHandler failed"); } - return COMPtr(pcm); + return COMPtr(cm); } HMenuPtr createMenu(IContextMenu* cm) @@ -296,31 +369,29 @@ void invoke(QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm) } } -QMainWindow* getMainWindow(QWidget* w) -{ - QWidget* p = w; - - while (p) { - if (auto* mw=dynamic_cast(p)) { - return mw; - } - - p = p->parentWidget(); - } - return nullptr; -} - -void showShellMenu(QWidget* parent, const QFileInfo& file, const QPoint& pos) +void showShellMenu( + QWidget* parent, const std::vector& files, const QPoint& pos) { - const auto path = QDir::toNativeSeparators(file.absoluteFilePath()); + if (files.empty()) { + log::warn("showShellMenu(): no files given"); + return; + } try { auto* mw = getMainWindow(parent); - auto idl = getIDL(path.toStdWString().c_str()); - auto [sf, childIdl] = getShellFolder(idl.get()); - auto cm = getContextMenu(sf.get(), childIdl); + auto idls = createIdls(files); + + if (idls.empty()) { + log::error("no idls, can't create context menu"); + return; + } + + IdlsFreer freer(idls); + + auto array = createItemArray(idls); + auto cm = createContextMenu(array.get()); auto hmenu = createMenu(cm.get()); const int cmd = runMenu(mw, cm.get(), hmenu.get(), pos); @@ -332,8 +403,21 @@ void showShellMenu(QWidget* parent, const QFileInfo& file, const QPoint& pos) } catch(MenuFailed& e) { - log::error("can't create shell menu for '{}': {}", path, e.what()); + if (files.size() == 1) { + log::error( + "can't create shell menu for '{}': {}", + QDir::toNativeSeparators(files[0].absoluteFilePath()), e.what()); + } else { + log::error( + "can't create shell menu for {} files: {}", + files.size(), e.what()); + } } } +void showShellMenu(QWidget* parent, const QFileInfo& file, const QPoint& pos) +{ + showShellMenu(parent, std::vector{file}, pos); +} + } // namespace diff --git a/src/envshell.h b/src/envshell.h index f30495e0..3be53841 100644 --- a/src/envshell.h +++ b/src/envshell.h @@ -7,7 +7,11 @@ namespace env { -void showShellMenu(QWidget* parent, const QFileInfo& file, const QPoint& pos); +void showShellMenu( + QWidget* parent, const QFileInfo& file, const QPoint& pos); + +void showShellMenu( + QWidget* parent, const std::vector& files, const QPoint& pos); } diff --git a/src/filetree.cpp b/src/filetree.cpp index 3c99ab05..d7dbc6e6 100644 --- a/src/filetree.cpp +++ b/src/filetree.cpp @@ -442,18 +442,8 @@ void FileTree::onContextMenu(const QPoint &pos) const auto m = QApplication::keyboardModifiers(); if (m & Qt::ShiftModifier) { - if (auto* item=singleSelection()) { - if (!item->isDirectory()) { - const auto file = m_core.directoryStructure()->searchFile( - item->dataRelativeFilePath().toStdWString(), nullptr); - - if (file) { - const QFileInfo fi(QString::fromStdWString(file->getFullPath())); - env::showShellMenu(m_tree, fi, m_tree->viewport()->mapToGlobal(pos)); - return; - } - } - } + showShellMenu(pos); + return; } QMenu menu; @@ -476,6 +466,22 @@ void FileTree::onContextMenu(const QPoint &pos) menu.exec(m_tree->viewport()->mapToGlobal(pos)); } +void FileTree::showShellMenu(QPoint pos) +{ + std::vector files; + + for (auto&& index : m_tree->selectionModel()->selectedRows()) { + auto* item = m_model->itemFromIndex(index); + if (!item) { + continue; + } + + files.push_back(item->realPath()); + } + + env::showShellMenu(m_tree, files, m_tree->viewport()->mapToGlobal(pos)); +} + void FileTree::addDirectoryMenus(QMenu&, FileTreeItem&) { // noop diff --git a/src/filetree.h b/src/filetree.h index 40b5b2ff..39c9d0c6 100644 --- a/src/filetree.h +++ b/src/filetree.h @@ -48,9 +48,10 @@ private: FileTreeModel* m_model; FileTreeItem* singleSelection(); + void onExpandedChanged(const QModelIndex& index, bool expanded); void onContextMenu(const QPoint &pos); - void showShellMenu(const MOShared::FileEntry& file, QPoint pos); + void showShellMenu(QPoint pos); void addDirectoryMenus(QMenu& menu, FileTreeItem& item); void addFileMenus(QMenu& menu, const MOShared::FileEntry& file, int originID); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index b27122ef..da9f949c 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -1085,6 +1085,9 @@ p, li { white-space: pre-wrap; } true + + QAbstractItemView::ExtendedSelection + true diff --git a/src/shared/directoryentry.h b/src/shared/directoryentry.h index f1d3ba03..b5a1dced 100644 --- a/src/shared/directoryentry.h +++ b/src/shared/directoryentry.h @@ -416,7 +416,7 @@ public: // path containing the file // const FileEntry::Ptr searchFile( - const std::wstring &path, const DirectoryEntry **directory) const; + const std::wstring &path, const DirectoryEntry **directory=nullptr) const; void insertFile(const std::wstring &filePath, FilesOrigin &origin, FILETIME fileTime); -- cgit v1.3.1 From d0be8a0d3b8c021e3efda0041ebb55eae25dbfde Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Tue, 21 Jan 2020 23:51:08 -0500 Subject: ShellMenu class --- src/envshell.cpp | 123 +++++++++++++++++++++++++++---------------------------- src/envshell.h | 26 +++++++++--- src/filetree.cpp | 6 +-- 3 files changed, 85 insertions(+), 70 deletions(-) (limited to 'src/envshell.cpp') diff --git a/src/envshell.cpp b/src/envshell.cpp index dcf337c5..1f9c4032 100644 --- a/src/envshell.cpp +++ b/src/envshell.cpp @@ -1,5 +1,4 @@ #include "envshell.h" -#include "env.h" #include #include #include @@ -209,7 +208,56 @@ private: -QMainWindow* getMainWindow(QWidget* w) +void ShellMenu::addFile(QFileInfo fi) +{ + m_files.emplace_back(std::move(fi)); +} + +void ShellMenu::exec(QWidget* parent, const QPoint& pos) +{ + if (m_files.empty()) { + log::warn("showShellMenu(): no files given"); + return; + } + + try + { + auto* mw = getMainWindow(parent); + auto idls = createIdls(m_files); + + if (idls.empty()) { + log::error("no idls, can't create context menu"); + return; + } + + IdlsFreer freer(idls); + + auto array = createItemArray(idls); + auto cm = createContextMenu(array.get()); + auto hmenu = createMenu(cm.get()); + + const int cmd = runMenu(mw, cm.get(), hmenu.get(), pos); + if (cmd <= 0) { + return; + } + + invoke(mw, pos, cmd - QCM_FIRST, cm.get()); + } + catch(MenuFailed& e) + { + if (m_files.size() == 1) { + log::error( + "can't create shell menu for '{}': {}", + QDir::toNativeSeparators(m_files[0].absoluteFilePath()), e.what()); + } else { + log::error( + "can't create shell menu for {} files: {}", + m_files.size(), e.what()); + } + } +} + +QMainWindow* ShellMenu::getMainWindow(QWidget* w) { QWidget* p = w; @@ -224,7 +272,7 @@ QMainWindow* getMainWindow(QWidget* w) return nullptr; } -COMPtr createShellItem(const std::wstring& path) +COMPtr ShellMenu::createShellItem(const std::wstring& path) { IShellItem* item = nullptr; @@ -238,7 +286,7 @@ COMPtr createShellItem(const std::wstring& path) return COMPtr(item); } -COMPtr getPersistIDList(IShellItem* item) +COMPtr ShellMenu::getPersistIDList(IShellItem* item) { IPersistIDList* idl = nullptr; auto r = item->QueryInterface(IID_IPersistIDList, (void**)&idl); @@ -250,7 +298,7 @@ COMPtr getPersistIDList(IShellItem* item) return COMPtr(idl); } -CoTaskMemPtr getIDList(IPersistIDList* pidlist) +CoTaskMemPtr ShellMenu::getIDList(IPersistIDList* pidlist) { LPITEMIDLIST absIdl = nullptr; auto r = pidlist->GetIDList(&absIdl); @@ -262,7 +310,7 @@ CoTaskMemPtr getIDList(IPersistIDList* pidlist) return CoTaskMemPtr(absIdl); } -std::vector createIdls( +std::vector ShellMenu::createIdls( const std::vector& files) { std::vector idls; @@ -280,7 +328,7 @@ std::vector createIdls( return idls; } -COMPtr createItemArray( +COMPtr ShellMenu::createItemArray( std::vector& idls) { IShellItemArray* array = nullptr; @@ -294,7 +342,7 @@ COMPtr createItemArray( return COMPtr(array); } -COMPtr createContextMenu(IShellItemArray* array) +COMPtr ShellMenu::createContextMenu(IShellItemArray* array) { IContextMenu* cm = nullptr; @@ -308,7 +356,7 @@ COMPtr createContextMenu(IShellItemArray* array) return COMPtr(cm); } -HMenuPtr createMenu(IContextMenu* cm) +HMenuPtr ShellMenu::createMenu(IContextMenu* cm) { HMENU hmenu = CreatePopupMenu(); if (!hmenu) { @@ -326,7 +374,8 @@ HMenuPtr createMenu(IContextMenu* cm) return HMenuPtr(hmenu); } -int runMenu(QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p) +int ShellMenu::runMenu( + QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p) { const auto hwnd = (HWND)mw->winId(); @@ -336,7 +385,8 @@ int runMenu(QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p) return TrackPopupMenuEx(menu, TPM_RETURNCMD, p.x(), p.y(), hwnd, nullptr); } -void invoke(QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm) +void ShellMenu::invoke( + QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm) { const auto hwnd = (HWND)mw->winId(); @@ -369,55 +419,4 @@ void invoke(QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm) } } - -void showShellMenu( - QWidget* parent, const std::vector& files, const QPoint& pos) -{ - if (files.empty()) { - log::warn("showShellMenu(): no files given"); - return; - } - - try - { - auto* mw = getMainWindow(parent); - auto idls = createIdls(files); - - if (idls.empty()) { - log::error("no idls, can't create context menu"); - return; - } - - IdlsFreer freer(idls); - - auto array = createItemArray(idls); - auto cm = createContextMenu(array.get()); - auto hmenu = createMenu(cm.get()); - - const int cmd = runMenu(mw, cm.get(), hmenu.get(), pos); - if (cmd <= 0) { - return; - } - - invoke(mw, pos, cmd - QCM_FIRST, cm.get()); - } - catch(MenuFailed& e) - { - if (files.size() == 1) { - log::error( - "can't create shell menu for '{}': {}", - QDir::toNativeSeparators(files[0].absoluteFilePath()), e.what()); - } else { - log::error( - "can't create shell menu for {} files: {}", - files.size(), e.what()); - } - } -} - -void showShellMenu(QWidget* parent, const QFileInfo& file, const QPoint& pos) -{ - showShellMenu(parent, std::vector{file}, pos); -} - } // namespace diff --git a/src/envshell.h b/src/envshell.h index 3be53841..3e694562 100644 --- a/src/envshell.h +++ b/src/envshell.h @@ -1,18 +1,34 @@ #ifndef ENV_SHELL_H #define ENV_SHELL_H +#include "env.h" #include #include namespace env { -void showShellMenu( - QWidget* parent, const QFileInfo& file, const QPoint& pos); +class ShellMenu +{ +public: + void addFile(QFileInfo fi); + void exec(QWidget* parent, const QPoint& pos); + +private: + std::vector m_files; -void showShellMenu( - QWidget* parent, const std::vector& files, const QPoint& pos); + QMainWindow* getMainWindow(QWidget* w); + COMPtr createShellItem(const std::wstring& path); + COMPtr getPersistIDList(IShellItem* item); + CoTaskMemPtr getIDList(IPersistIDList* pidlist); + std::vector createIdls(const std::vector& files); + COMPtr createItemArray(std::vector& idls); + COMPtr createContextMenu(IShellItemArray* array); + HMenuPtr createMenu(IContextMenu* cm); + int runMenu(QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p); + void invoke(QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm); +}; -} +} // namespace #endif // ENV_SHELL_H diff --git a/src/filetree.cpp b/src/filetree.cpp index d7dbc6e6..404c994b 100644 --- a/src/filetree.cpp +++ b/src/filetree.cpp @@ -468,7 +468,7 @@ void FileTree::onContextMenu(const QPoint &pos) void FileTree::showShellMenu(QPoint pos) { - std::vector files; + env::ShellMenu menu; for (auto&& index : m_tree->selectionModel()->selectedRows()) { auto* item = m_model->itemFromIndex(index); @@ -476,10 +476,10 @@ void FileTree::showShellMenu(QPoint pos) continue; } - files.push_back(item->realPath()); + menu.addFile(item->realPath()); } - env::showShellMenu(m_tree, files, m_tree->viewport()->mapToGlobal(pos)); + menu.exec(m_tree, m_tree->viewport()->mapToGlobal(pos)); } void FileTree::addDirectoryMenus(QMenu&, FileTreeItem&) -- cgit v1.3.1 From 0f6205bea500169b48b86e321d4d3f650603da2d Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Wed, 22 Jan 2020 00:13:15 -0500 Subject: dummy menu for files in multiple directories --- src/envshell.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/envshell.h | 1 + 2 files changed, 64 insertions(+), 1 deletion(-) (limited to 'src/envshell.cpp') diff --git a/src/envshell.cpp b/src/envshell.cpp index 1f9c4032..7754928e 100644 --- a/src/envshell.cpp +++ b/src/envshell.cpp @@ -23,6 +23,24 @@ public: }; +class DummyMenu +{ +public: + DummyMenu(QString s) + : m_what(s) + { + } + + const QString& what() const + { + return m_what; + } + +private: + QString m_what; +}; + + struct IdlsFreer { const std::vector& v; @@ -220,9 +238,10 @@ void ShellMenu::exec(QWidget* parent, const QPoint& pos) return; } + auto* mw = getMainWindow(parent); + try { - auto* mw = getMainWindow(parent); auto idls = createIdls(m_files); if (idls.empty()) { @@ -243,6 +262,18 @@ void ShellMenu::exec(QWidget* parent, const QPoint& pos) invoke(mw, pos, cmd - QCM_FIRST, cm.get()); } + catch(DummyMenu& dm) + { + try + { + showDummyMenu(mw, pos, dm.what()); + } + catch(MenuFailed& e) + { + log::error("{}", dm.what()); + log::error("additionally, creating the dummy menu failed: {}", e.what()); + } + } catch(MenuFailed& e) { if (m_files.size() == 1) { @@ -257,6 +288,27 @@ void ShellMenu::exec(QWidget* parent, const QPoint& pos) } } +void ShellMenu::showDummyMenu( + QMainWindow* mw, const QPoint& pos, const QString& what) +{ + HMENU menu = CreatePopupMenu(); + if (!menu) { + const auto e = GetLastError(); + throw MenuFailed(e, "CreatePopupMenu failed"); + } + + if (!AppendMenuW(menu, MF_STRING | MF_DISABLED, 0, what.toStdWString().c_str())) { + const auto e = GetLastError(); + throw MenuFailed(e, "AppendMenuW failed"); + } + + const auto hwnd = (HWND)mw->winId(); + if (!TrackPopupMenuEx(menu, 0, pos.x(), pos.y(), hwnd, nullptr)) { + const auto e = GetLastError(); + throw MenuFailed(e, "TrackPopupMenuEx failed"); + } +} + QMainWindow* ShellMenu::getMainWindow(QWidget* w) { QWidget* p = w; @@ -314,10 +366,20 @@ std::vector ShellMenu::createIdls( const std::vector& files) { std::vector idls; + std::optional parent; for (auto&& f : files) { const auto path = QDir::toNativeSeparators(f.absoluteFilePath()).toStdWString(); + if (!parent) { + parent = f.absoluteDir(); + } else { + if (*parent != f.absoluteDir()) { + throw DummyMenu(QObject::tr( + "Selected files must be in the same directory")); + } + } + auto item = createShellItem(path); auto pidlist = getPersistIDList(item.get()); auto absIdl = getIDList(pidlist.get()); diff --git a/src/envshell.h b/src/envshell.h index 3e694562..86d9b0fc 100644 --- a/src/envshell.h +++ b/src/envshell.h @@ -27,6 +27,7 @@ private: HMenuPtr createMenu(IContextMenu* cm); int runMenu(QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p); void invoke(QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm); + void showDummyMenu(QMainWindow* mw, const QPoint& pos, const QString& what); }; } // namespace -- cgit v1.3.1 From a7a406e5538b343d87b3221b13b7bf3503bbfd70 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Wed, 22 Jan 2020 01:03:00 -0500 Subject: preparing for multiple origins shell menus split exec() from createMenu() --- src/envshell.cpp | 128 ++++++++++++++++++++++++++++-------------- src/envshell.h | 9 ++- src/filetree.cpp | 68 +++++++++++++++++++++- src/shared/directoryentry.cpp | 16 ++++++ src/shared/directoryentry.h | 12 +++- 5 files changed, 185 insertions(+), 48 deletions(-) (limited to 'src/envshell.cpp') diff --git a/src/envshell.cpp b/src/envshell.cpp index 7754928e..992ce1ef 100644 --- a/src/envshell.cpp +++ b/src/envshell.cpp @@ -65,15 +65,7 @@ public: WndProcFilter(QMainWindow* mw, IContextMenu* cm) : m_mw(mw), m_cm(cm), m_cm2(nullptr), m_cm3(nullptr) { - IContextMenu2* cm2 = nullptr; - if (SUCCEEDED(cm->QueryInterface(IID_IContextMenu2, (void**)&cm2))) { - m_cm2.reset(cm2); - } - - IContextMenu3* cm3 = nullptr; - if (SUCCEEDED(cm->QueryInterface(IID_IContextMenu3, (void**)&cm3))) { - m_cm3.reset(cm3); - } + createInterfaces(); } ~WndProcFilter() @@ -86,6 +78,9 @@ public: bool nativeEventFilter(const QByteArray& type, void* m, long* lresultOut) override { MSG* msg = (MSG*)m; + if (!msg) { + return false; + } if (msg->message == WM_MENUSELECT) { HANDLE_WM_MENUSELECT(msg->hwnd, msg->wParam, msg->lParam, onMenuSelect); @@ -129,6 +124,23 @@ private: COMPtr m_cm2; COMPtr m_cm3; + void createInterfaces() + { + if (!m_cm) { + return; + } + + IContextMenu2* cm2 = nullptr; + if (SUCCEEDED(m_cm->QueryInterface(IID_IContextMenu2, (void**)&cm2))) { + m_cm2.reset(cm2); + } + + IContextMenu3* cm3 = nullptr; + if (SUCCEEDED(m_cm->QueryInterface(IID_IContextMenu3, (void**)&cm3))) { + m_cm3.reset(cm3); + } + } + // adapted from // https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723 // @@ -232,14 +244,52 @@ void ShellMenu::addFile(QFileInfo fi) } void ShellMenu::exec(QWidget* parent, const QPoint& pos) +{ + auto* mw = getMainWindow(parent); + HMENU menu = getMenu(); + if (!menu) { + return; + } + + try + { + const int cmd = runMenu(mw, m_cm.get(), m_menu.get(), pos); + if (cmd <= 0) { + return; + } + + invoke(mw, pos, cmd - QCM_FIRST, m_cm.get()); + } + catch(MenuFailed& e) + { + if (m_files.size() == 1) { + log::error( + "can't exec shell menu for '{}': {}", + QDir::toNativeSeparators(m_files[0].absoluteFilePath()), e.what()); + } else { + log::error( + "can't exec shell menu for {} files: {}", + m_files.size(), e.what()); + } + } +} + +HMENU ShellMenu::getMenu() +{ + if (!m_menu) { + createMenu(); + } + + return m_menu.get(); +} + +void ShellMenu::createMenu() { if (m_files.empty()) { log::warn("showShellMenu(): no files given"); return; } - auto* mw = getMainWindow(parent); - try { auto idls = createIdls(m_files); @@ -252,27 +302,12 @@ void ShellMenu::exec(QWidget* parent, const QPoint& pos) IdlsFreer freer(idls); auto array = createItemArray(idls); - auto cm = createContextMenu(array.get()); - auto hmenu = createMenu(cm.get()); - - const int cmd = runMenu(mw, cm.get(), hmenu.get(), pos); - if (cmd <= 0) { - return; - } - - invoke(mw, pos, cmd - QCM_FIRST, cm.get()); + m_cm = createContextMenu(array.get()); + m_menu = createMenu(m_cm.get()); } catch(DummyMenu& dm) { - try - { - showDummyMenu(mw, pos, dm.what()); - } - catch(MenuFailed& e) - { - log::error("{}", dm.what()); - log::error("additionally, creating the dummy menu failed: {}", e.what()); - } + m_menu = createDummyMenu(dm.what()); } catch(MenuFailed& e) { @@ -285,27 +320,34 @@ void ShellMenu::exec(QWidget* parent, const QPoint& pos) "can't create shell menu for {} files: {}", m_files.size(), e.what()); } + + m_menu = createDummyMenu(QObject::tr("No menu available")); } } -void ShellMenu::showDummyMenu( - QMainWindow* mw, const QPoint& pos, const QString& what) +HMenuPtr ShellMenu::createDummyMenu(const QString& what) { - HMENU menu = CreatePopupMenu(); - if (!menu) { - const auto e = GetLastError(); - throw MenuFailed(e, "CreatePopupMenu failed"); - } + try + { + HMENU menu = CreatePopupMenu(); + if (!menu) { + const auto e = GetLastError(); + throw MenuFailed(e, "CreatePopupMenu failed"); + } - if (!AppendMenuW(menu, MF_STRING | MF_DISABLED, 0, what.toStdWString().c_str())) { - const auto e = GetLastError(); - throw MenuFailed(e, "AppendMenuW failed"); + if (!AppendMenuW(menu, MF_STRING | MF_DISABLED, 0, what.toStdWString().c_str())) { + const auto e = GetLastError(); + throw MenuFailed(e, "AppendMenuW failed"); + } + + return HMenuPtr(menu); } + catch(MenuFailed& e) + { + log::error("{}", what); + log::error("additionally, creating the dummy menu failed: {}", e.what()); - const auto hwnd = (HWND)mw->winId(); - if (!TrackPopupMenuEx(menu, 0, pos.x(), pos.y(), hwnd, nullptr)) { - const auto e = GetLastError(); - throw MenuFailed(e, "TrackPopupMenuEx failed"); + return {}; } } diff --git a/src/envshell.h b/src/envshell.h index 86d9b0fc..f25aeda7 100644 --- a/src/envshell.h +++ b/src/envshell.h @@ -12,10 +12,16 @@ class ShellMenu { public: void addFile(QFileInfo fi); + void exec(QWidget* parent, const QPoint& pos); + HMENU getMenu(); private: std::vector m_files; + COMPtr m_cm; + HMenuPtr m_menu; + + void createMenu(); QMainWindow* getMainWindow(QWidget* w); COMPtr createShellItem(const std::wstring& path); @@ -25,9 +31,10 @@ private: COMPtr createItemArray(std::vector& idls); COMPtr createContextMenu(IShellItemArray* array); HMenuPtr createMenu(IContextMenu* cm); + HMenuPtr createDummyMenu(const QString& what); + int runMenu(QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p); void invoke(QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm); - void showDummyMenu(QMainWindow* mw, const QPoint& pos, const QString& what); }; } // namespace diff --git a/src/filetree.cpp b/src/filetree.cpp index 404c994b..7128665d 100644 --- a/src/filetree.cpp +++ b/src/filetree.cpp @@ -468,7 +468,8 @@ void FileTree::onContextMenu(const QPoint &pos) void FileTree::showShellMenu(QPoint pos) { - env::ShellMenu menu; + // menus by origin + std::map menus; for (auto&& index : m_tree->selectionModel()->selectedRows()) { auto* item = m_model->itemFromIndex(index); @@ -476,10 +477,71 @@ void FileTree::showShellMenu(QPoint pos) continue; } - menu.addFile(item->realPath()); + menus[item->originID()].addFile(item->realPath()); + + if (item->isConflicted()) { + const auto file = m_core.directoryStructure()->searchFile( + item->dataRelativeFilePath().toStdWString(), nullptr); + + if (!file) { + log::error( + "file '{}' not found, data path={}, real path={}", + item->filename(), item->dataRelativeFilePath(), item->realPath()); + + continue; + } + + const auto alts = file->getAlternatives(); + if (alts.empty()) { + log::warn( + "file '{}' has no alternative origins but is marked as conflicted", + item->dataRelativeFilePath()); + } + + for (auto&& alt : alts) { + auto* dir = file->getParent(); + if (!dir) { + log::error( + "file {} from origin {} has no parent", + item->dataRelativeFilePath(), alt.first); + + continue; + } + + const auto* origin = dir->findOriginByID(alt.first); + if (!origin) { + log::error( + "origin {} for file {} cannot be found", + alt.first, item->dataRelativeFilePath()); + + continue; + } + + const auto originFile = origin->findFile(file->getIndex()); + if (!originFile) { + log::error( + "file {} not found in origin {} ({})", + item->dataRelativeFilePath(), origin->getName(), file->getIndex()); + + continue; + } + + menus[alt.first].addFile( + QString::fromStdWString(originFile->getFullPath())); + } + } } - menu.exec(m_tree, m_tree->viewport()->mapToGlobal(pos)); + if (menus.empty()) { + log::warn("no menus to show"); + return; + } + else if (menus.size() == 1) { + auto& menu = menus.begin()->second; + menu.exec(m_tree, m_tree->viewport()->mapToGlobal(pos)); + } else { + + } } void FileTree::addDirectoryMenus(QMenu&, FileTreeItem&) diff --git a/src/shared/directoryentry.cpp b/src/shared/directoryentry.cpp index 819075ae..460431a4 100644 --- a/src/shared/directoryentry.cpp +++ b/src/shared/directoryentry.cpp @@ -104,6 +104,17 @@ public: return m_Origins[ID]; } + const FilesOrigin* findByID(Index ID) const + { + auto itor = m_Origins.find(ID); + + if (itor == m_Origins.end()) { + return nullptr; + } else { + return &itor->second; + } + } + FilesOrigin &getByName(const std::wstring &name) { std::map::iterator iter = m_OriginsNameMap.find(name); @@ -736,6 +747,11 @@ FilesOrigin &DirectoryEntry::getOriginByName(const std::wstring &name) const return m_OriginConnection->getByName(name); } +const FilesOrigin* DirectoryEntry::findOriginByID(int ID) const +{ + return m_OriginConnection->findByID(ID); +} + int DirectoryEntry::anyOrigin() const { bool ignore; diff --git a/src/shared/directoryentry.h b/src/shared/directoryentry.h index b5a1dced..69eb7574 100644 --- a/src/shared/directoryentry.h +++ b/src/shared/directoryentry.h @@ -63,7 +63,16 @@ class FileEntry public: typedef unsigned int Index; typedef boost::shared_ptr Ptr; - typedef std::vector>> AlternativesVector; + + // a vector of {originId, {archiveName, order}} + // + // if a file is in an archive, archiveName is the name of the bsa and order + // is the order of the associated plugin in the plugins list + // + // is a file is not in an archive, archiveName is empty and order is usually + // -1 + typedef std::vector>> + AlternativesVector; FileEntry(); FileEntry(Index index, const std::wstring &name, DirectoryEntry *parent); @@ -339,6 +348,7 @@ public: bool originExists(const std::wstring &name) const; FilesOrigin &getOriginByID(int ID) const; FilesOrigin &getOriginByName(const std::wstring &name) const; + const FilesOrigin* findOriginByID(int ID) const; int anyOrigin() const; -- cgit v1.3.1 From 6005da618775d545c664371f53571a75eacab7f2 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Wed, 22 Jan 2020 01:40:56 -0500 Subject: ShellMenuCollection, events not processed yet --- src/envshell.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++++----------- src/envshell.h | 26 ++++++++++++++++- src/filetree.cpp | 12 ++++++++ 3 files changed, 109 insertions(+), 18 deletions(-) (limited to 'src/envshell.cpp') diff --git a/src/envshell.cpp b/src/envshell.cpp index 992ce1ef..58948d31 100644 --- a/src/envshell.cpp +++ b/src/envshell.cpp @@ -237,6 +237,30 @@ private: }; +QMainWindow* getMainWindow(QWidget* w) +{ + QWidget* p = w; + + while (p) { + if (auto* mw=dynamic_cast(p)) { + return mw; + } + + p = p->parentWidget(); + } + + return nullptr; +} + +HWND getHWND(QMainWindow* mw) +{ + if (mw) { + return (HWND)mw->winId(); + } else { + return 0; + } +} + void ShellMenu::addFile(QFileInfo fi) { @@ -351,21 +375,6 @@ HMenuPtr ShellMenu::createDummyMenu(const QString& what) } } -QMainWindow* ShellMenu::getMainWindow(QWidget* w) -{ - QWidget* p = w; - - while (p) { - if (auto* mw=dynamic_cast(p)) { - return mw; - } - - p = p->parentWidget(); - } - - return nullptr; -} - COMPtr ShellMenu::createShellItem(const std::wstring& path) { IShellItem* item = nullptr; @@ -481,7 +490,7 @@ HMenuPtr ShellMenu::createMenu(IContextMenu* cm) int ShellMenu::runMenu( QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p) { - const auto hwnd = (HWND)mw->winId(); + const auto hwnd = getHWND(mw); auto filter = std::make_unique(mw, cm); QCoreApplication::instance()->installNativeEventFilter(filter.get()); @@ -492,7 +501,7 @@ int ShellMenu::runMenu( void ShellMenu::invoke( QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm) { - const auto hwnd = (HWND)mw->winId(); + const auto hwnd = getHWND(mw); CMINVOKECOMMANDINFOEX info = {}; @@ -523,4 +532,50 @@ void ShellMenu::invoke( } } + +void ShellMenuCollection::add(QString name, ShellMenu m) +{ + m_menus.push_back({name, std::move(m)}); +} + +void ShellMenuCollection::exec(QWidget* parent, const QPoint& pos) +{ + HMENU menu = ::CreatePopupMenu(); + if (!menu) { + const auto e = GetLastError(); + + log::error( + "CreatePopupMenu for merged menus failed, {}", + formatSystemMessage(e)); + + return; + } + + for (auto&& m : m_menus) { + auto hmenu = m.menu.getMenu(); + if (!hmenu) { + continue; + } + + const auto r = AppendMenuW( + menu, MF_POPUP | MF_STRING, + reinterpret_cast(hmenu), m.name.toStdWString().c_str()); + + if (!r) { + const auto e = GetLastError(); + + log::error( + "AppendMenuW failed for merged menu {}, {}", + m.name, formatSystemMessage(e)); + + continue; + } + } + + auto* mw = getMainWindow(parent); + auto hwnd = getHWND(mw); + + TrackPopupMenuEx(menu, TPM_RETURNCMD, pos.x(), pos.y(), hwnd, nullptr); +} + } // namespace diff --git a/src/envshell.h b/src/envshell.h index f25aeda7..79dce552 100644 --- a/src/envshell.h +++ b/src/envshell.h @@ -11,6 +11,14 @@ namespace env class ShellMenu { public: + ShellMenu() = default; + + // noncopyable + ShellMenu(const ShellMenu&) = delete; + ShellMenu& operator=(const ShellMenu&) = delete; + ShellMenu(ShellMenu&&) = default; + ShellMenu& operator=(ShellMenu&&) = default; + void addFile(QFileInfo fi); void exec(QWidget* parent, const QPoint& pos); @@ -23,7 +31,6 @@ private: void createMenu(); - QMainWindow* getMainWindow(QWidget* w); COMPtr createShellItem(const std::wstring& path); COMPtr getPersistIDList(IShellItem* item); CoTaskMemPtr getIDList(IPersistIDList* pidlist); @@ -37,6 +44,23 @@ private: void invoke(QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm); }; + +class ShellMenuCollection +{ +public: + void add(QString name, ShellMenu m); + void exec(QWidget* parent, const QPoint& pos); + +private: + struct MenuInfo + { + QString name; + ShellMenu menu; + }; + + std::vector m_menus; +}; + } // namespace #endif // ENV_SHELL_H diff --git a/src/filetree.cpp b/src/filetree.cpp index 7128665d..543be82b 100644 --- a/src/filetree.cpp +++ b/src/filetree.cpp @@ -540,7 +540,19 @@ void FileTree::showShellMenu(QPoint pos) auto& menu = menus.begin()->second; menu.exec(m_tree, m_tree->viewport()->mapToGlobal(pos)); } else { + env::ShellMenuCollection mc; + for (auto&& m : menus) { + const auto* origin = m_core.directoryStructure()->findOriginByID(m.first); + if (!origin) { + log::error("origin {} not found for merged menus", m.first); + continue; + } + + mc.add(QString::fromStdWString(origin->getName()), std::move(m.second)); + } + + mc.exec(m_tree, m_tree->viewport()->mapToGlobal(pos)); } } -- cgit v1.3.1 From 2a0e78e3cf0c1106a1fb7e470148f6e0b093b2b1 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Wed, 22 Jan 2020 02:54:29 -0500 Subject: fixed bad path for alternate origins finished ShellMenuCollection, had to split a bunch of things --- src/envshell.cpp | 499 ++++++++++++++++++++++++------------------ src/envshell.h | 36 ++- src/filetree.cpp | 60 ++--- src/shared/directoryentry.cpp | 14 +- src/shared/directoryentry.h | 7 +- 5 files changed, 358 insertions(+), 258 deletions(-) (limited to 'src/envshell.cpp') diff --git a/src/envshell.cpp b/src/envshell.cpp index 58948d31..e01bb804 100644 --- a/src/envshell.cpp +++ b/src/envshell.cpp @@ -62,17 +62,12 @@ struct IdlsFreer class WndProcFilter : public QAbstractNativeEventFilter { public: - WndProcFilter(QMainWindow* mw, IContextMenu* cm) - : m_mw(mw), m_cm(cm), m_cm2(nullptr), m_cm3(nullptr) - { - createInterfaces(); - } + using function_type = std::function< + bool (HWND hwnd, UINT m, WPARAM wp, LPARAM lp, LRESULT* out)>; - ~WndProcFilter() + WndProcFilter(function_type f) + : m_f(std::move(f)) { - if (auto* sb=m_mw->statusBar()) { - sb->clearMessage(); - } } bool nativeEventFilter(const QByteArray& type, void* m, long* lresultOut) override @@ -82,194 +77,116 @@ public: return false; } - if (msg->message == WM_MENUSELECT) { - HANDLE_WM_MENUSELECT(msg->hwnd, msg->wParam, msg->lParam, onMenuSelect); - return true; - } - - if (m_cm3) { - LRESULT lresult = 0; + LRESULT lr = 0; - const auto r = m_cm3->HandleMenuMsg2( - msg->message, msg->wParam, msg->lParam, &lresult); + const bool r = m_f(msg->hwnd, msg->message, msg->wParam, msg->lParam, &lr); - if (SUCCEEDED(r)) { - if (lresultOut) { - *lresultOut = lresult; - } - - return true; - } + if (lresultOut) { + *lresultOut = lr; } - if (m_cm2) { - const auto r = m_cm2->HandleMenuMsg( - msg->message, msg->wParam, msg->lParam); - - if (SUCCEEDED(r)) { - if (lresultOut) { - *lresultOut = 0; - } - - return true; - } - } - - return false; + return r; } private: - QMainWindow* m_mw; - IContextMenu* m_cm; - COMPtr m_cm2; - COMPtr m_cm3; - - void createInterfaces() - { - if (!m_cm) { - return; - } - - IContextMenu2* cm2 = nullptr; - if (SUCCEEDED(m_cm->QueryInterface(IID_IContextMenu2, (void**)&cm2))) { - m_cm2.reset(cm2); - } - - IContextMenu3* cm3 = nullptr; - if (SUCCEEDED(m_cm->QueryInterface(IID_IContextMenu3, (void**)&cm3))) { - m_cm3.reset(cm3); - } - } + function_type m_f; +}; - // adapted from - // https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723 - // - void onMenuSelect( - HWND hwnd, HMENU hmenu, int item, HMENU hmenuPopup, UINT flags) - { - if (m_cm && item >= QCM_FIRST && item <= QCM_LAST) { - WCHAR szBuf[MAX_PATH]; - const auto r = IContextMenu_GetCommandString( - m_cm, item - QCM_FIRST, GCS_HELPTEXTW, NULL, szBuf, MAX_PATH); - if (FAILED(r)) { - lstrcpynW(szBuf, L"No help available.", MAX_PATH); - } - if (m_mw) { - if (auto* sb=m_mw->statusBar()) { - sb->showMessage(QString::fromWCharArray(szBuf)); - } - } - } +HWND getHWND(QMainWindow* mw) +{ + if (mw) { + return (HWND)mw->winId(); + } else { + return 0; } +} - // adapted from - // https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723 - // - HRESULT IContextMenu_GetCommandString( - IContextMenu *pcm, UINT_PTR idCmd, UINT uFlags, - UINT *pwReserved, LPWSTR pszName, UINT cchMax) - { - // Callers are expected to be using Unicode. - if (!(uFlags & GCS_UNICODE)) { - return E_INVALIDARG; - } +// adapted from +// https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723 +// +HRESULT IContextMenu_GetCommandString( + IContextMenu *pcm, UINT_PTR idCmd, UINT uFlags, + UINT *pwReserved, LPWSTR pszName, UINT cchMax) +{ + // Callers are expected to be using Unicode. + if (!(uFlags & GCS_UNICODE)) { + return E_INVALIDARG; + } - // Some context menu handlers have off-by-one bugs and will - // overflow the output buffer. Let’s artificially reduce the - // buffer size so a one-character overflow won’t corrupt memory. - if (cchMax <= 1) { - return E_FAIL; - } + // Some context menu handlers have off-by-one bugs and will + // overflow the output buffer. Let’s artificially reduce the + // buffer size so a one-character overflow won’t corrupt memory. + if (cchMax <= 1) { + return E_FAIL; + } - cchMax--; + cchMax--; - // First try the Unicode message. Preset the output buffer - // with a known value because some handlers return S_OK without - // doing anything. - pszName[0] = L'\0'; + // First try the Unicode message. Preset the output buffer + // with a known value because some handlers return S_OK without + // doing anything. + pszName[0] = L'\0'; - HRESULT hr = pcm->GetCommandString( - idCmd, uFlags, pwReserved, (LPSTR)pszName, cchMax); + HRESULT hr = pcm->GetCommandString( + idCmd, uFlags, pwReserved, (LPSTR)pszName, cchMax); - if (SUCCEEDED(hr) && pszName[0] == L'\0') { - // Rats, a buggy IContextMenu handler that returned success - // even though it failed. - hr = E_NOTIMPL; - } + if (SUCCEEDED(hr) && pszName[0] == L'\0') { + // Rats, a buggy IContextMenu handler that returned success + // even though it failed. + hr = E_NOTIMPL; + } - if (FAILED(hr)) { - // try again with ANSI – pad the buffer with one extra character - // to compensate for context menu handlers that overflow by - // one character. - LPSTR pszAnsi = (LPSTR)LocalAlloc( - LMEM_FIXED, (cchMax + 1) * sizeof(CHAR)); + if (FAILED(hr)) { + // try again with ANSI – pad the buffer with one extra character + // to compensate for context menu handlers that overflow by + // one character. + LPSTR pszAnsi = (LPSTR)LocalAlloc( + LMEM_FIXED, (cchMax + 1) * sizeof(CHAR)); - if (pszAnsi) { - pszAnsi[0] = '\0'; + if (pszAnsi) { + pszAnsi[0] = '\0'; - hr = pcm->GetCommandString( - idCmd, uFlags & ~GCS_UNICODE, pwReserved, pszAnsi, cchMax); + hr = pcm->GetCommandString( + idCmd, uFlags & ~GCS_UNICODE, pwReserved, pszAnsi, cchMax); - if (SUCCEEDED(hr) && pszAnsi[0] == '\0') { - // Rats, a buggy IContextMenu handler that returned success - // even though it failed. - hr = E_NOTIMPL; - } + if (SUCCEEDED(hr) && pszAnsi[0] == '\0') { + // Rats, a buggy IContextMenu handler that returned success + // even though it failed. + hr = E_NOTIMPL; + } - if (SUCCEEDED(hr)) { - if (MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, pszName, cchMax) == 0) { - hr = E_FAIL; - } + if (SUCCEEDED(hr)) { + if (MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, pszName, cchMax) == 0) { + hr = E_FAIL; } - - LocalFree(pszAnsi); - - } else { - hr = E_OUTOFMEMORY; } - } - - return hr; - } -}; + LocalFree(pszAnsi); -QMainWindow* getMainWindow(QWidget* w) -{ - QWidget* p = w; - - while (p) { - if (auto* mw=dynamic_cast(p)) { - return mw; + } else { + hr = E_OUTOFMEMORY; } - - p = p->parentWidget(); } - return nullptr; + return hr; } -HWND getHWND(QMainWindow* mw) + +ShellMenu::ShellMenu(QMainWindow* mw) + : m_mw(mw) { - if (mw) { - return (HWND)mw->winId(); - } else { - return 0; - } } - void ShellMenu::addFile(QFileInfo fi) { m_files.emplace_back(std::move(fi)); } -void ShellMenu::exec(QWidget* parent, const QPoint& pos) +void ShellMenu::exec(const QPoint& pos) { - auto* mw = getMainWindow(parent); HMENU menu = getMenu(); if (!menu) { return; @@ -277,12 +194,29 @@ void ShellMenu::exec(QWidget* parent, const QPoint& pos) try { - const int cmd = runMenu(mw, m_cm.get(), m_menu.get(), pos); + const auto hwnd = getHWND(m_mw); + + auto filter = std::make_unique( + [&](HWND h, UINT m, WPARAM wp, LPARAM lp, LRESULT* out) { + return wndProc(h, m, wp, lp, out); + }); + + QCoreApplication::instance()->installNativeEventFilter(filter.get()); + + const int cmd = TrackPopupMenuEx( + menu, TPM_RETURNCMD, pos.x(), pos.y(), hwnd, nullptr); + + if (m_mw) { + if (auto* sb=m_mw->statusBar()) { + sb->clearMessage(); + } + } + if (cmd <= 0) { return; } - invoke(mw, pos, cmd - QCM_FIRST, m_cm.get()); + invoke(pos, cmd - QCM_FIRST); } catch(MenuFailed& e) { @@ -301,13 +235,67 @@ void ShellMenu::exec(QWidget* parent, const QPoint& pos) HMENU ShellMenu::getMenu() { if (!m_menu) { - createMenu(); + create(); } return m_menu.get(); } -void ShellMenu::createMenu() +bool ShellMenu::wndProc(HWND h, UINT m, WPARAM wp, LPARAM lp, LRESULT* out) +{ + if (m == WM_MENUSELECT) { + HANDLE_WM_MENUSELECT(h, wp, lp, onMenuSelect); + return true; + } + + if (m_cm3) { + const auto r = m_cm3->HandleMenuMsg2(m, wp, lp, out); + + if (SUCCEEDED(r)) { + return true; + } + } + + if (m_cm2) { + const auto r = m_cm2->HandleMenuMsg(m, wp, lp); + + if (SUCCEEDED(r)) { + if (out) { + *out = 0; + } + + return true; + } + } + + return false; +} + +// adapted from +// https://devblogs.microsoft.com/oldnewthing/20040928-00/?p=37723 +// +void ShellMenu::onMenuSelect( + HWND hwnd, HMENU hmenu, int item, HMENU hmenuPopup, UINT flags) +{ + if (m_cm && item >= QCM_FIRST && item <= QCM_LAST) { + WCHAR szBuf[MAX_PATH]; + + const auto r = IContextMenu_GetCommandString( + m_cm.get(), item - QCM_FIRST, GCS_HELPTEXTW, NULL, szBuf, MAX_PATH); + + if (FAILED(r)) { + lstrcpynW(szBuf, L"No help available.", MAX_PATH); + } + + if (m_mw) { + if (auto* sb=m_mw->statusBar()) { + sb->showMessage(QString::fromWCharArray(szBuf)); + } + } + } +} + +void ShellMenu::create() { if (m_files.empty()) { log::warn("showShellMenu(): no files given"); @@ -326,8 +314,9 @@ void ShellMenu::createMenu() IdlsFreer freer(idls); auto array = createItemArray(idls); - m_cm = createContextMenu(array.get()); - m_menu = createMenu(m_cm.get()); + + createContextMenu(array.get()); + createPopupMenu(m_cm.get()); } catch(DummyMenu& dm) { @@ -375,44 +364,6 @@ HMenuPtr ShellMenu::createDummyMenu(const QString& what) } } -COMPtr ShellMenu::createShellItem(const std::wstring& path) -{ - IShellItem* item = nullptr; - - auto r = SHCreateItemFromParsingName( - path.c_str(), nullptr, IID_IShellItem, (void**)&item); - - if (FAILED(r)) { - throw MenuFailed(r, "SHCreateItemFromParsingName failed"); - } - - return COMPtr(item); -} - -COMPtr ShellMenu::getPersistIDList(IShellItem* item) -{ - IPersistIDList* idl = nullptr; - auto r = item->QueryInterface(IID_IPersistIDList, (void**)&idl); - - if (FAILED(r)) { - throw MenuFailed(r, "QueryInterface IID_IPersistIDList failed"); - } - - return COMPtr(idl); -} - -CoTaskMemPtr ShellMenu::getIDList(IPersistIDList* pidlist) -{ - LPITEMIDLIST absIdl = nullptr; - auto r = pidlist->GetIDList(&absIdl); - - if (FAILED(r)) { - throw MenuFailed(r, "GetIDList failed"); - } - - return CoTaskMemPtr(absIdl); -} - std::vector ShellMenu::createIdls( const std::vector& files) { @@ -455,7 +406,7 @@ COMPtr ShellMenu::createItemArray( return COMPtr(array); } -COMPtr ShellMenu::createContextMenu(IShellItemArray* array) +void ShellMenu::createContextMenu(IShellItemArray* array) { IContextMenu* cm = nullptr; @@ -466,10 +417,24 @@ COMPtr ShellMenu::createContextMenu(IShellItemArray* array) throw MenuFailed(r, "BindToHandler failed"); } - return COMPtr(cm); + m_cm.reset(cm); + + { + IContextMenu2* cm2 = nullptr; + if (SUCCEEDED(m_cm->QueryInterface(IID_IContextMenu2, (void**)&cm2))) { + m_cm2.reset(cm2); + } + } + + { + IContextMenu3* cm3 = nullptr; + if (SUCCEEDED(m_cm->QueryInterface(IID_IContextMenu3, (void**)&cm3))) { + m_cm3.reset(cm3); + } + } } -HMenuPtr ShellMenu::createMenu(IContextMenu* cm) +void ShellMenu::createPopupMenu(IContextMenu* cm) { HMENU hmenu = CreatePopupMenu(); if (!hmenu) { @@ -484,24 +449,50 @@ HMenuPtr ShellMenu::createMenu(IContextMenu* cm) throw MenuFailed(r, "QueryContextMenu failed"); } - return HMenuPtr(hmenu); + m_menu.reset(hmenu); } -int ShellMenu::runMenu( - QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p) +COMPtr ShellMenu::createShellItem(const std::wstring& path) { - const auto hwnd = getHWND(mw); + IShellItem* item = nullptr; - auto filter = std::make_unique(mw, cm); - QCoreApplication::instance()->installNativeEventFilter(filter.get()); + auto r = SHCreateItemFromParsingName( + path.c_str(), nullptr, IID_IShellItem, (void**)&item); + + if (FAILED(r)) { + throw MenuFailed(r, "SHCreateItemFromParsingName failed"); + } + + return COMPtr(item); +} + +COMPtr ShellMenu::getPersistIDList(IShellItem* item) +{ + IPersistIDList* idl = nullptr; + auto r = item->QueryInterface(IID_IPersistIDList, (void**)&idl); + + if (FAILED(r)) { + throw MenuFailed(r, "QueryInterface IID_IPersistIDList failed"); + } - return TrackPopupMenuEx(menu, TPM_RETURNCMD, p.x(), p.y(), hwnd, nullptr); + return COMPtr(idl); } -void ShellMenu::invoke( - QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm) +CoTaskMemPtr ShellMenu::getIDList(IPersistIDList* pidlist) { - const auto hwnd = getHWND(mw); + LPITEMIDLIST absIdl = nullptr; + auto r = pidlist->GetIDList(&absIdl); + + if (FAILED(r)) { + throw MenuFailed(r, "GetIDList failed"); + } + + return CoTaskMemPtr(absIdl); +} + +void ShellMenu::invoke(const QPoint& p, int cmd) +{ + const auto hwnd = getHWND(m_mw); CMINVOKECOMMANDINFOEX info = {}; @@ -525,7 +516,7 @@ void ShellMenu::invoke( info.fMask |= CMIC_MASK_CONTROL_DOWN; } - const auto r = cm->InvokeCommand((CMINVOKECOMMANDINFO*)&info); + const auto r = m_cm->InvokeCommand((CMINVOKECOMMANDINFO*)&info); if (FAILED(r)) { throw MenuFailed(r, fmt::format("InvokeCommand failed, verb={}", cmd)); @@ -533,12 +524,17 @@ void ShellMenu::invoke( } +ShellMenuCollection::ShellMenuCollection(QMainWindow* mw) + : m_mw(mw), m_active(nullptr) +{ +} + void ShellMenuCollection::add(QString name, ShellMenu m) { m_menus.push_back({name, std::move(m)}); } -void ShellMenuCollection::exec(QWidget* parent, const QPoint& pos) +void ShellMenuCollection::exec(const QPoint& pos) { HMENU menu = ::CreatePopupMenu(); if (!menu) { @@ -572,10 +568,77 @@ void ShellMenuCollection::exec(QWidget* parent, const QPoint& pos) } } - auto* mw = getMainWindow(parent); - auto hwnd = getHWND(mw); + auto hwnd = getHWND(m_mw); + + auto filter = std::make_unique( + [&](HWND h, UINT m, WPARAM wp, LPARAM lp, LRESULT* out) { + return wndProc(h, m, wp, lp, out); + }); + + QCoreApplication::instance()->installNativeEventFilter(filter.get()); + + const int cmd = TrackPopupMenuEx( + menu, TPM_RETURNCMD, pos.x(), pos.y(), hwnd, nullptr); + + if (m_mw) { + if (auto* sb=m_mw->statusBar()) { + sb->clearMessage(); + } + } + + if (cmd <= 0) { + return; + } + + if (!m_active) { + log::debug("SMC: command {} selected without active submenu"); + return; + } + + const auto realCmd = cmd - QCM_FIRST; + + log::debug("SMC: invoking {} on {}", realCmd, m_active->name); + m_active->menu.invoke(pos, realCmd); +} + +bool ShellMenuCollection::wndProc( + HWND h, UINT m, WPARAM wp, LPARAM lp, LRESULT* out) +{ + if (m == WM_MENUSELECT) { + auto* oldActive = m_active; + m_active = nullptr; - TrackPopupMenuEx(menu, TPM_RETURNCMD, pos.x(), pos.y(), hwnd, nullptr); + HANDLE_WM_MENUSELECT(h, wp, lp, onMenuSelect); + + if (!m_active && oldActive) { + // this was not a top level, forward to active + m_active = oldActive; + } else if (m_active && m_active == oldActive) { + // same top level menu was selected twice, ignore + return true; + } else if (m_active && m_active != oldActive) { + // new top level selected + log::debug("SMC: switching to {}", m_active->name); + } + } + + if (!m_active) { + // no active menu, forward it to the default handler + return false; + } + + return m_active->menu.wndProc(h, m, wp, lp, out); +} + +void ShellMenuCollection::onMenuSelect( + HWND hwnd, HMENU hmenu, int item, HMENU hmenuPopup, UINT flags) +{ + for (auto&& m : m_menus) { + if (m.menu.getMenu() == hmenuPopup) { + m_active = &m; + break; + } + } } } // namespace diff --git a/src/envshell.h b/src/envshell.h index 79dce552..52125a5c 100644 --- a/src/envshell.h +++ b/src/envshell.h @@ -11,7 +11,7 @@ namespace env class ShellMenu { public: - ShellMenu() = default; + ShellMenu(QMainWindow* mw); // noncopyable ShellMenu(const ShellMenu&) = delete; @@ -21,35 +21,44 @@ public: void addFile(QFileInfo fi); - void exec(QWidget* parent, const QPoint& pos); + void exec(const QPoint& pos); HMENU getMenu(); + bool wndProc(HWND hwnd, UINT m, WPARAM wp, LPARAM lp, LRESULT* out); + void invoke(const QPoint& p, int cmd); private: + QMainWindow* m_mw; std::vector m_files; COMPtr m_cm; + COMPtr m_cm2; + COMPtr m_cm3; HMenuPtr m_menu; - void createMenu(); + void create(); + + std::vector createIdls(const std::vector& files); + COMPtr createItemArray(std::vector& idls); + + void createContextMenu(IShellItemArray* array); + void createPopupMenu(IContextMenu* cm); COMPtr createShellItem(const std::wstring& path); COMPtr getPersistIDList(IShellItem* item); CoTaskMemPtr getIDList(IPersistIDList* pidlist); - std::vector createIdls(const std::vector& files); - COMPtr createItemArray(std::vector& idls); - COMPtr createContextMenu(IShellItemArray* array); - HMenuPtr createMenu(IContextMenu* cm); HMenuPtr createDummyMenu(const QString& what); - int runMenu(QMainWindow* mw, IContextMenu* cm, HMENU menu, const QPoint& p); - void invoke(QMainWindow* mw, const QPoint& p, int cmd, IContextMenu* cm); + void onMenuSelect( + HWND hwnd, HMENU hmenu, int item, HMENU hmenuPopup, UINT flags); }; class ShellMenuCollection { public: + ShellMenuCollection(QMainWindow* mw); + void add(QString name, ShellMenu m); - void exec(QWidget* parent, const QPoint& pos); + void exec(const QPoint& pos); private: struct MenuInfo @@ -58,7 +67,14 @@ private: ShellMenu menu; }; + QMainWindow* m_mw; std::vector m_menus; + MenuInfo* m_active; + + bool wndProc(HWND hwnd, UINT m, WPARAM wp, LPARAM lp, LRESULT* out); + + void onMenuSelect( + HWND hwnd, HMENU hmenu, int item, HMENU hmenuPopup, UINT flags); }; } // namespace diff --git a/src/filetree.cpp b/src/filetree.cpp index 543be82b..4e3db1f7 100644 --- a/src/filetree.cpp +++ b/src/filetree.cpp @@ -466,8 +466,25 @@ void FileTree::onContextMenu(const QPoint &pos) menu.exec(m_tree->viewport()->mapToGlobal(pos)); } +QMainWindow* getMainWindow(QWidget* w) +{ + QWidget* p = w; + + while (p) { + if (auto* mw=dynamic_cast(p)) { + return mw; + } + + p = p->parentWidget(); + } + + return nullptr; +} + void FileTree::showShellMenu(QPoint pos) { + auto* mw = getMainWindow(m_tree); + // menus by origin std::map menus; @@ -477,7 +494,12 @@ void FileTree::showShellMenu(QPoint pos) continue; } - menus[item->originID()].addFile(item->realPath()); + auto itor = menus.find(item->originID()); + if (itor == menus.end()) { + itor = menus.emplace(item->originID(), mw).first; + } + + itor->second.addFile(item->realPath()); if (item->isConflicted()) { const auto file = m_core.directoryStructure()->searchFile( @@ -499,35 +521,21 @@ void FileTree::showShellMenu(QPoint pos) } for (auto&& alt : alts) { - auto* dir = file->getParent(); - if (!dir) { - log::error( - "file {} from origin {} has no parent", - item->dataRelativeFilePath(), alt.first); - - continue; + auto itor = menus.find(alt.first); + if (itor == menus.end()) { + itor = menus.emplace(alt.first, mw).first; } - const auto* origin = dir->findOriginByID(alt.first); - if (!origin) { + const auto fullPath = file->getFullPath(alt.first); + if (fullPath.empty()) { log::error( - "origin {} for file {} cannot be found", - alt.first, item->dataRelativeFilePath()); - - continue; - } - - const auto originFile = origin->findFile(file->getIndex()); - if (!originFile) { - log::error( - "file {} not found in origin {} ({})", - item->dataRelativeFilePath(), origin->getName(), file->getIndex()); + "file {} not found in origin {}", + item->dataRelativeFilePath(), alt.first); continue; } - menus[alt.first].addFile( - QString::fromStdWString(originFile->getFullPath())); + itor->second.addFile(QString::fromStdWString(fullPath)); } } } @@ -538,9 +546,9 @@ void FileTree::showShellMenu(QPoint pos) } else if (menus.size() == 1) { auto& menu = menus.begin()->second; - menu.exec(m_tree, m_tree->viewport()->mapToGlobal(pos)); + menu.exec(m_tree->viewport()->mapToGlobal(pos)); } else { - env::ShellMenuCollection mc; + env::ShellMenuCollection mc(mw); for (auto&& m : menus) { const auto* origin = m_core.directoryStructure()->findOriginByID(m.first); @@ -552,7 +560,7 @@ void FileTree::showShellMenu(QPoint pos) mc.add(QString::fromStdWString(origin->getName()), std::move(m.second)); } - mc.exec(m_tree, m_tree->viewport()->mapToGlobal(pos)); + mc.exec(m_tree->viewport()->mapToGlobal(pos)); } } diff --git a/src/shared/directoryentry.cpp b/src/shared/directoryentry.cpp index 460431a4..4181098c 100644 --- a/src/shared/directoryentry.cpp +++ b/src/shared/directoryentry.cpp @@ -354,12 +354,20 @@ bool FileEntry::isFromArchive(std::wstring archiveName) const return false; } -std::wstring FileEntry::getFullPath() const +std::wstring FileEntry::getFullPath(int originID) const { - bool ignore = false; + if (originID == -1) { + bool ignore = false; + originID = getOrigin(ignore); + } // base directory for origin - std::wstring result = m_Parent->getOriginByID(getOrigin(ignore)).getPath(); + const auto* o = m_Parent->findOriginByID(originID); + if (!o) { + return {}; + } + + std::wstring result = o->getPath(); // all intermediate directories recurseParents(result, m_Parent); diff --git a/src/shared/directoryentry.h b/src/shared/directoryentry.h index 69eb7574..6102c88f 100644 --- a/src/shared/directoryentry.h +++ b/src/shared/directoryentry.h @@ -127,7 +127,12 @@ public: } bool isFromArchive(std::wstring archiveName = L"") const; - std::wstring getFullPath() const; + + // if originID is -1, uses the main origin; if this file doesn't exist in the + // given origin, returns an empty string + // + std::wstring getFullPath(int originID=-1) const; + std::wstring getRelativePath() const; DirectoryEntry *getParent() -- cgit v1.3.1 From 412165fcfbbd27d679a7400ebdef75ff3a9d6aa7 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Wed, 22 Jan 2020 03:29:40 -0500 Subject: show file counts when there are discrepancies --- src/envshell.cpp | 32 ++++++++++++++++++++++++++++++++ src/envshell.h | 4 ++++ src/filetree.cpp | 16 +++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) (limited to 'src/envshell.cpp') diff --git a/src/envshell.cpp b/src/envshell.cpp index e01bb804..33ec0624 100644 --- a/src/envshell.cpp +++ b/src/envshell.cpp @@ -185,6 +185,11 @@ void ShellMenu::addFile(QFileInfo fi) m_files.emplace_back(std::move(fi)); } +int ShellMenu::fileCount() const +{ + return static_cast(m_files.size()); +} + void ShellMenu::exec(const QPoint& pos) { HMENU menu = getMenu(); @@ -529,6 +534,11 @@ ShellMenuCollection::ShellMenuCollection(QMainWindow* mw) { } +void ShellMenuCollection::addDetails(QString s) +{ + m_details.emplace_back(std::move(s)); +} + void ShellMenuCollection::add(QString name, ShellMenu m) { m_menus.push_back({name, std::move(m)}); @@ -547,6 +557,28 @@ void ShellMenuCollection::exec(const QPoint& pos) return; } + if (!m_details.empty()) { + for (auto&& d : m_details) { + const auto s = d.toStdWString(); + const auto r = AppendMenuW(menu, MF_STRING|MF_DISABLED, 0, s.c_str()); + + if (!r) { + const auto e = GetLastError(); + log::error( + "AppendMenuW failed for details '{}', {}", + d, formatSystemMessage(e)); + } + } + + const auto r = AppendMenuW(menu, MF_SEPARATOR, 0, nullptr); + if (!r) { + const auto e = GetLastError(); + log::error( + "AppendMenuW failed for separator, {}", + formatSystemMessage(e)); + } + } + for (auto&& m : m_menus) { auto hmenu = m.menu.getMenu(); if (!hmenu) { diff --git a/src/envshell.h b/src/envshell.h index 52125a5c..f9245a37 100644 --- a/src/envshell.h +++ b/src/envshell.h @@ -20,6 +20,7 @@ public: ShellMenu& operator=(ShellMenu&&) = default; void addFile(QFileInfo fi); + int fileCount() const; void exec(const QPoint& pos); HMENU getMenu(); @@ -57,7 +58,9 @@ class ShellMenuCollection public: ShellMenuCollection(QMainWindow* mw); + void addDetails(QString s); void add(QString name, ShellMenu m); + void exec(const QPoint& pos); private: @@ -68,6 +71,7 @@ private: }; QMainWindow* m_mw; + std::vector m_details; std::vector m_menus; MenuInfo* m_active; diff --git a/src/filetree.cpp b/src/filetree.cpp index 4e3db1f7..a826ed9a 100644 --- a/src/filetree.cpp +++ b/src/filetree.cpp @@ -487,6 +487,7 @@ void FileTree::showShellMenu(QPoint pos) // menus by origin std::map menus; + int totalFiles = 0; for (auto&& index : m_tree->selectionModel()->selectedRows()) { auto* item = m_model->itemFromIndex(index); @@ -500,6 +501,7 @@ void FileTree::showShellMenu(QPoint pos) } itor->second.addFile(item->realPath()); + ++totalFiles; if (item->isConflicted()) { const auto file = m_core.directoryStructure()->searchFile( @@ -549,6 +551,7 @@ void FileTree::showShellMenu(QPoint pos) menu.exec(m_tree->viewport()->mapToGlobal(pos)); } else { env::ShellMenuCollection mc(mw); + bool hasDiscrepancies = false; for (auto&& m : menus) { const auto* origin = m_core.directoryStructure()->findOriginByID(m.first); @@ -557,7 +560,18 @@ void FileTree::showShellMenu(QPoint pos) continue; } - mc.add(QString::fromStdWString(origin->getName()), std::move(m.second)); + QString caption = QString::fromStdWString(origin->getName()); + if (m.second.fileCount() < totalFiles) { + const auto d = m.second.fileCount(); + caption += " " + tr("(only has %1 file(s))").arg(d); + hasDiscrepancies = true; + } + + mc.add(caption, std::move(m.second)); + } + + if (hasDiscrepancies) { + mc.addDetails(tr("%1 file(s) selected").arg(totalFiles)); } mc.exec(m_tree->viewport()->mapToGlobal(pos)); -- cgit v1.3.1