diff options
Diffstat (limited to 'src/envshell.cpp')
| -rw-r--r-- | src/envshell.cpp | 89 |
1 files changed, 72 insertions, 17 deletions
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<QMainWindow*>(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<QMainWindow*>(p)) { - return mw; - } - - p = p->parentWidget(); - } - - return nullptr; -} - COMPtr<IShellItem> 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<WndProcFilter>(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<UINT_PTR>(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 |
