2018-05-08 16:05:59 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2018-06-01 15:28:26 +02:00
|
|
|
#include <Qt5Frame.hxx>
|
2018-08-09 17:14:14 +02:00
|
|
|
#include <Qt5MainWindow.hxx>
|
2018-06-01 15:28:26 +02:00
|
|
|
#include <Qt5Menu.hxx>
|
2018-05-24 15:04:02 +02:00
|
|
|
#include <Qt5Menu.moc>
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-05-15 16:29:15 +02:00
|
|
|
#include <QtWidgets/QtWidgets>
|
|
|
|
|
2018-05-09 14:34:24 +02:00
|
|
|
#include <vcl/svapp.hxx>
|
2018-07-28 15:57:23 +02:00
|
|
|
#include <sal/log.hxx>
|
2018-05-09 14:34:24 +02:00
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
Qt5Menu::Qt5Menu(bool bMenuBar)
|
|
|
|
: mpVCLMenu(nullptr)
|
|
|
|
, mpParentSalMenu(nullptr)
|
|
|
|
, mpFrame(nullptr)
|
|
|
|
, mbMenuBar(bMenuBar)
|
2018-05-08 16:05:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
Qt5Menu::~Qt5Menu() { maItems.clear(); }
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-08-10 15:14:07 +02:00
|
|
|
bool Qt5Menu::VisibleMenuBar() { return true; }
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
void Qt5Menu::InsertItem(SalMenuItem* pSalMenuItem, unsigned nPos)
|
2018-05-08 16:05:59 +02:00
|
|
|
{
|
2018-05-09 14:34:24 +02:00
|
|
|
SolarMutexGuard aGuard;
|
2018-05-31 12:14:55 +02:00
|
|
|
Qt5MenuItem* pItem = static_cast<Qt5MenuItem*>(pSalMenuItem);
|
2018-05-09 14:34:24 +02:00
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
if (nPos == MENU_APPEND)
|
|
|
|
maItems.push_back(pItem);
|
2018-05-09 14:34:24 +02:00
|
|
|
else
|
2018-05-31 12:14:55 +02:00
|
|
|
maItems.insert(maItems.begin() + nPos, pItem);
|
2018-05-09 14:34:24 +02:00
|
|
|
|
|
|
|
pItem->mpParentMenu = this;
|
2018-05-08 16:05:59 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
void Qt5Menu::RemoveItem(unsigned nPos)
|
2018-05-08 16:05:59 +02:00
|
|
|
{
|
2018-05-09 14:34:24 +02:00
|
|
|
SolarMutexGuard aGuard;
|
2018-05-31 12:14:55 +02:00
|
|
|
maItems.erase(maItems.begin() + nPos);
|
2018-05-08 16:05:59 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 15:28:26 +02:00
|
|
|
void Qt5Menu::SetSubMenu(SalMenuItem* pSalMenuItem, SalMenu* pSubMenu, unsigned)
|
2018-05-08 16:05:59 +02:00
|
|
|
{
|
2018-05-23 10:51:30 +02:00
|
|
|
SolarMutexGuard aGuard;
|
2018-05-31 12:14:55 +02:00
|
|
|
Qt5MenuItem* pItem = static_cast<Qt5MenuItem*>(pSalMenuItem);
|
|
|
|
Qt5Menu* pQSubMenu = static_cast<Qt5Menu*>(pSubMenu);
|
2018-05-23 10:51:30 +02:00
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
if (pQSubMenu == nullptr)
|
2018-05-23 10:51:30 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
pQSubMenu->mpParentSalMenu = this;
|
|
|
|
pItem->mpSubMenu = pQSubMenu;
|
2018-05-08 16:05:59 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
void Qt5Menu::SetFrame(const SalFrame* pFrame)
|
2018-05-08 16:05:59 +02:00
|
|
|
{
|
2018-05-15 16:29:15 +02:00
|
|
|
SolarMutexGuard aGuard;
|
|
|
|
assert(mbMenuBar);
|
2018-05-31 12:14:55 +02:00
|
|
|
mpFrame = const_cast<Qt5Frame*>(static_cast<const Qt5Frame*>(pFrame));
|
2018-05-15 16:29:15 +02:00
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
mpFrame->SetMenu(this);
|
2018-05-15 16:29:15 +02:00
|
|
|
|
2018-08-09 17:14:14 +02:00
|
|
|
Qt5MainWindow* pMainWindow = mpFrame->GetTopLevelWindow();
|
2018-05-31 12:14:55 +02:00
|
|
|
if (pMainWindow)
|
2018-05-15 16:29:15 +02:00
|
|
|
mpQMenuBar = pMainWindow->menuBar();
|
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
DoFullMenuUpdate(mpVCLMenu);
|
2018-05-15 16:29:15 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
void Qt5Menu::DoFullMenuUpdate(Menu* pMenuBar, QMenu* pParentMenu)
|
2018-05-16 11:37:09 +02:00
|
|
|
{
|
|
|
|
Menu* pVCLMenu = mpVCLMenu;
|
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
if (mbMenuBar && mpQMenuBar)
|
2018-05-17 16:48:53 +02:00
|
|
|
mpQMenuBar->clear();
|
2018-05-29 09:53:43 +02:00
|
|
|
QActionGroup* pQAG = nullptr;
|
2018-05-17 16:48:53 +02:00
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
for (sal_Int32 nItem = 0; nItem < static_cast<sal_Int32>(GetItemCount()); nItem++)
|
2018-05-16 11:37:09 +02:00
|
|
|
{
|
2018-05-31 12:14:55 +02:00
|
|
|
Qt5MenuItem* pSalMenuItem = GetItemAtPos(nItem);
|
2018-05-16 11:37:09 +02:00
|
|
|
sal_uInt16 nId = pSalMenuItem->mnId;
|
2018-05-31 12:14:55 +02:00
|
|
|
OUString aText = pVCLMenu->GetItemText(nId);
|
2018-05-23 10:51:30 +02:00
|
|
|
QMenu* pQMenu = pParentMenu;
|
2018-05-31 12:14:55 +02:00
|
|
|
NativeItemText(aText);
|
|
|
|
vcl::KeyCode nAccelKey = pVCLMenu->GetAccelKey(nId);
|
|
|
|
bool bChecked = pVCLMenu->IsItemChecked(nId);
|
|
|
|
MenuItemBits itemBits = pVCLMenu->GetItemBits(nId);
|
2018-05-16 11:37:09 +02:00
|
|
|
|
|
|
|
if (mbMenuBar && mpQMenuBar)
|
2018-05-23 10:51:30 +02:00
|
|
|
// top-level menu
|
2018-05-31 12:14:55 +02:00
|
|
|
pQMenu = mpQMenuBar->addMenu(toQString(aText));
|
2018-05-23 10:51:30 +02:00
|
|
|
else
|
|
|
|
{
|
2018-05-31 12:14:55 +02:00
|
|
|
if (pSalMenuItem->mpSubMenu)
|
2018-05-29 09:53:43 +02:00
|
|
|
{
|
2018-05-23 10:51:30 +02:00
|
|
|
// submenu
|
2018-05-31 12:14:55 +02:00
|
|
|
pQMenu = pQMenu->addMenu(toQString(aText));
|
|
|
|
pQAG = new QActionGroup(pQMenu);
|
2018-05-29 09:53:43 +02:00
|
|
|
}
|
2018-05-23 10:51:30 +02:00
|
|
|
else
|
2018-05-23 11:23:34 +02:00
|
|
|
{
|
2018-05-31 12:14:55 +02:00
|
|
|
if (pSalMenuItem->mnType == MenuItemType::SEPARATOR)
|
2018-05-23 11:23:34 +02:00
|
|
|
pQMenu->addSeparator();
|
|
|
|
else
|
2018-05-23 17:39:44 +02:00
|
|
|
{
|
2018-05-23 11:23:34 +02:00
|
|
|
// leaf menu
|
2018-05-31 12:14:55 +02:00
|
|
|
QAction* pAction = pQMenu->addAction(toQString(aText));
|
|
|
|
pAction->setShortcut(toQString(nAccelKey.GetName(GetFrame()->GetWindow())));
|
2018-05-28 13:59:43 +02:00
|
|
|
|
|
|
|
if (itemBits & MenuItemBits::CHECKABLE)
|
|
|
|
{
|
2018-05-31 12:14:55 +02:00
|
|
|
pAction->setCheckable(true);
|
|
|
|
pAction->setChecked(bChecked);
|
2018-05-28 13:59:43 +02:00
|
|
|
}
|
2018-05-29 09:53:43 +02:00
|
|
|
else if (itemBits & MenuItemBits::RADIOCHECK)
|
|
|
|
{
|
|
|
|
pAction->setCheckable(true);
|
2018-05-31 12:14:55 +02:00
|
|
|
if (!pQAG)
|
2018-05-29 09:53:43 +02:00
|
|
|
{
|
2018-05-31 12:14:55 +02:00
|
|
|
pQAG = new QActionGroup(pQMenu);
|
2018-05-29 09:53:43 +02:00
|
|
|
pQAG->setExclusive(true);
|
|
|
|
}
|
2018-05-31 12:14:55 +02:00
|
|
|
pQAG->addAction(pAction);
|
2018-05-29 09:53:43 +02:00
|
|
|
}
|
2018-05-28 13:59:43 +02:00
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
connect(pAction, &QAction::triggered, this,
|
|
|
|
[this, pSalMenuItem] { DispatchCommand(pSalMenuItem); });
|
2018-05-23 17:39:44 +02:00
|
|
|
}
|
2018-05-23 11:23:34 +02:00
|
|
|
}
|
2018-05-23 10:51:30 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
if (pSalMenuItem->mpSubMenu != nullptr)
|
2018-05-16 11:37:09 +02:00
|
|
|
{
|
2018-05-23 10:51:30 +02:00
|
|
|
pMenuBar->HandleMenuActivateEvent(pSalMenuItem->mpSubMenu->GetMenu());
|
2018-05-31 12:14:55 +02:00
|
|
|
pSalMenuItem->mpSubMenu->DoFullMenuUpdate(pMenuBar, pQMenu);
|
2018-05-23 10:51:30 +02:00
|
|
|
pMenuBar->HandleMenuDeActivateEvent(pSalMenuItem->mpSubMenu->GetMenu());
|
2018-05-16 11:37:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:28:26 +02:00
|
|
|
void Qt5Menu::ShowItem(unsigned, bool) {}
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-06-01 15:28:26 +02:00
|
|
|
void Qt5Menu::CheckItem(unsigned, bool) {}
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-06-01 15:28:26 +02:00
|
|
|
void Qt5Menu::EnableItem(unsigned, bool) {}
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-06-01 15:28:26 +02:00
|
|
|
void Qt5Menu::SetItemText(unsigned, SalMenuItem*, const rtl::OUString&) {}
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-06-01 15:28:26 +02:00
|
|
|
void Qt5Menu::SetItemImage(unsigned, SalMenuItem*, const Image&) {}
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-06-01 15:28:26 +02:00
|
|
|
void Qt5Menu::SetAccelerator(unsigned, SalMenuItem*, const vcl::KeyCode&, const OUString&) {}
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-06-01 15:28:26 +02:00
|
|
|
void Qt5Menu::GetSystemMenuData(SystemMenuData*) {}
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-05-25 16:04:24 +02:00
|
|
|
Qt5Menu* Qt5Menu::GetTopLevel()
|
|
|
|
{
|
2018-05-31 12:14:55 +02:00
|
|
|
Qt5Menu* pMenu = this;
|
2018-05-25 16:04:24 +02:00
|
|
|
while (pMenu->mpParentSalMenu)
|
|
|
|
pMenu = pMenu->mpParentSalMenu;
|
|
|
|
return pMenu;
|
|
|
|
}
|
|
|
|
|
2018-05-23 17:39:44 +02:00
|
|
|
const Qt5Frame* Qt5Menu::GetFrame() const
|
|
|
|
{
|
|
|
|
SolarMutexGuard aGuard;
|
|
|
|
const Qt5Menu* pMenu = this;
|
2018-05-31 12:14:55 +02:00
|
|
|
while (pMenu && !pMenu->mpFrame)
|
2018-05-23 17:39:44 +02:00
|
|
|
pMenu = pMenu->mpParentSalMenu;
|
|
|
|
return pMenu ? pMenu->mpFrame : nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
void Qt5Menu::DispatchCommand(Qt5MenuItem* pQItem)
|
2018-05-24 15:04:02 +02:00
|
|
|
{
|
2018-05-31 12:14:55 +02:00
|
|
|
if (pQItem)
|
2018-05-25 16:04:24 +02:00
|
|
|
{
|
|
|
|
Qt5Menu* pSalMenu = pQItem->mpParentMenu;
|
|
|
|
Qt5Menu* pTopLevel = pSalMenu->GetTopLevel();
|
|
|
|
pTopLevel->GetMenu()->HandleMenuCommandEvent(pSalMenu->GetMenu(), pQItem->mnId);
|
2018-05-31 12:14:55 +02:00
|
|
|
SAL_WARN("vcl.qt5", "menu triggered " << pQItem->mnId);
|
2018-05-25 16:04:24 +02:00
|
|
|
}
|
2018-05-24 15:04:02 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
void Qt5Menu::NativeItemText(OUString& rItemText) { rItemText = rItemText.replace('~', '&'); }
|
2018-05-08 16:05:59 +02:00
|
|
|
|
2018-05-31 12:14:55 +02:00
|
|
|
Qt5MenuItem::Qt5MenuItem(const SalItemParams* pItemData)
|
|
|
|
: mnId(pItemData->nId)
|
|
|
|
, mnType(pItemData->eType)
|
|
|
|
, mpParentMenu(nullptr)
|
|
|
|
, mpSubMenu(nullptr)
|
2018-05-08 16:05:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|