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 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 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()); -- cgit v1.3.1