Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sfx2 / source / commandpopup / CommandPopup.cxx
blobf4cdf92436c77392099fce1fad4d0dd44477d31b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <commandpopup/CommandPopup.hxx>
12 #include <sfx2/msgpool.hxx>
13 #include <sfx2/bindings.hxx>
14 #include <sfx2/msg.hxx>
15 #include <sfx2/viewfrm.hxx>
17 #include <comphelper/processfactory.hxx>
18 #include <comphelper/dispatchcommand.hxx>
20 #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
21 #include <com/sun/star/util/URL.hpp>
22 #include <com/sun/star/util/URLTransformer.hpp>
23 #include <com/sun/star/i18n/CharacterClassification.hpp>
25 #include <vcl/commandinfoprovider.hxx>
26 #include <vcl/event.hxx>
27 #include <vcl/svapp.hxx>
28 #include <i18nlangtag/languagetag.hxx>
30 using namespace css;
32 MenuContentHandler::MenuContentHandler(uno::Reference<frame::XFrame> const& xFrame)
33 : m_xContext(comphelper::getProcessComponentContext())
34 , m_xFrame(xFrame)
35 , m_xCharacterClassification(i18n::CharacterClassification::create(m_xContext))
36 , m_xURLTransformer(util::URLTransformer::create(m_xContext))
37 , m_sModuleLongName(vcl::CommandInfoProvider::GetModuleIdentifier(xFrame))
39 uno::Reference<ui::XModuleUIConfigurationManagerSupplier> xModuleConfigSupplier;
40 xModuleConfigSupplier.set(ui::theModuleUIConfigurationManagerSupplier::get(m_xContext));
42 uno::Reference<ui::XUIConfigurationManager> xConfigurationManager;
43 xConfigurationManager = xModuleConfigSupplier->getUIConfigurationManager(m_sModuleLongName);
45 uno::Reference<container::XIndexAccess> xConfigData;
46 xConfigData = xConfigurationManager->getSettings("private:resource/menubar/menubar", false);
48 gatherMenuContent(xConfigData, m_aMenuContent);
51 void MenuContentHandler::gatherMenuContent(
52 uno::Reference<container::XIndexAccess> const& xIndexAccess, MenuContent& rMenuContent)
54 for (sal_Int32 n = 0; n < xIndexAccess->getCount(); n++)
56 MenuContent aNewContent;
57 uno::Sequence<beans::PropertyValue> aProperties;
58 uno::Reference<container::XIndexAccess> xIndexContainer;
60 if (!(xIndexAccess->getByIndex(n) >>= aProperties))
61 continue;
63 bool bIsVisible = true;
64 bool bIsEnabled = true;
66 for (auto const& rProperty : std::as_const(aProperties))
68 OUString aPropertyName = rProperty.Name;
69 if (aPropertyName == "CommandURL")
70 rProperty.Value >>= aNewContent.m_aCommandURL;
71 else if (aPropertyName == "ItemDescriptorContainer")
72 rProperty.Value >>= xIndexContainer;
73 else if (aPropertyName == "IsVisible")
74 rProperty.Value >>= bIsVisible;
75 else if (aPropertyName == "Enabled")
76 rProperty.Value >>= bIsEnabled;
79 if (!bIsEnabled || !bIsVisible)
80 continue;
82 auto aCommandProperties = vcl::CommandInfoProvider::GetCommandProperties(
83 aNewContent.m_aCommandURL, m_sModuleLongName);
84 OUString aLabel = vcl::CommandInfoProvider::GetLabelForCommand(aCommandProperties);
85 aNewContent.m_aMenuLabel = aLabel;
86 aNewContent.m_aSearchableMenuLabel = toLower(aLabel);
88 if (!rMenuContent.m_aFullLabelWithPath.isEmpty())
89 aNewContent.m_aFullLabelWithPath = rMenuContent.m_aFullLabelWithPath + " / ";
90 aNewContent.m_aFullLabelWithPath += aNewContent.m_aMenuLabel;
92 aNewContent.m_aTooltip = vcl::CommandInfoProvider::GetTooltipForCommand(
93 aNewContent.m_aCommandURL, aCommandProperties, m_xFrame);
95 if (xIndexContainer.is())
96 gatherMenuContent(xIndexContainer, aNewContent);
98 rMenuContent.m_aSubMenuContent.push_back(aNewContent);
102 void MenuContentHandler::findInMenu(OUString const& rText,
103 std::unique_ptr<weld::TreeView>& rpCommandTreeView,
104 std::vector<CurrentEntry>& rCommandList)
106 m_aAdded.clear();
108 OUString aLowerCaseText = toLower(rText);
110 auto aTextStartCriterium = [](MenuContent const& rMenuContent, OUString const& rSearchText) {
111 return rMenuContent.m_aSearchableMenuLabel.startsWith(rSearchText);
114 findInMenuRecursive(m_aMenuContent, aLowerCaseText, rpCommandTreeView, rCommandList,
115 aTextStartCriterium);
117 auto aTextAllCriterium = [](MenuContent const& rMenuContent, OUString const& rSearchText) {
118 return rMenuContent.m_aSearchableMenuLabel.indexOf(rSearchText) > 0;
121 findInMenuRecursive(m_aMenuContent, aLowerCaseText, rpCommandTreeView, rCommandList,
122 aTextAllCriterium);
125 void MenuContentHandler::findInMenuRecursive(
126 MenuContent const& rMenuContent, OUString const& rText,
127 std::unique_ptr<weld::TreeView>& rpCommandTreeView, std::vector<CurrentEntry>& rCommandList,
128 std::function<bool(MenuContent const&, OUString const&)> const& rSearchCriterium)
130 for (MenuContent const& aSubContent : rMenuContent.m_aSubMenuContent)
132 if (rSearchCriterium(aSubContent, rText))
134 addCommandIfPossible(aSubContent, rpCommandTreeView, rCommandList);
136 findInMenuRecursive(aSubContent, rText, rpCommandTreeView, rCommandList, rSearchCriterium);
140 void MenuContentHandler::addCommandIfPossible(
141 MenuContent const& rMenuContent, const std::unique_ptr<weld::TreeView>& rpCommandTreeView,
142 std::vector<CurrentEntry>& rCommandList)
144 if (m_aAdded.find(rMenuContent.m_aFullLabelWithPath) != m_aAdded.end())
145 return;
147 OUString sCommandURL = rMenuContent.m_aCommandURL;
148 util::URL aCommandURL;
149 aCommandURL.Complete = sCommandURL;
151 if (!m_xURLTransformer->parseStrict(aCommandURL))
152 return;
154 auto* pViewFrame = SfxViewFrame::Current();
155 if (!pViewFrame)
156 return;
158 SfxSlotPool& rSlotPool = SfxSlotPool::GetSlotPool(pViewFrame);
159 const SfxSlot* pSlot = rSlotPool.GetUnoSlot(aCommandURL.Path);
160 if (!pSlot)
161 return;
163 std::unique_ptr<SfxPoolItem> pState;
164 SfxItemState eState = pViewFrame->GetBindings().QueryState(pSlot->GetSlotId(), pState);
165 if (eState == SfxItemState::DISABLED)
166 return;
168 auto xGraphic = vcl::CommandInfoProvider::GetXGraphicForCommand(sCommandURL, m_xFrame);
169 rCommandList.emplace_back(sCommandURL, rMenuContent.m_aTooltip);
171 auto pIter = rpCommandTreeView->make_iterator();
172 rpCommandTreeView->insert(nullptr, -1, &rMenuContent.m_aFullLabelWithPath, nullptr, nullptr,
173 nullptr, false, pIter.get());
174 rpCommandTreeView->set_image(*pIter, xGraphic);
175 m_aAdded.insert(rMenuContent.m_aFullLabelWithPath);
178 OUString MenuContentHandler::toLower(OUString const& rString)
180 const css::lang::Locale& rLocale = Application::GetSettings().GetUILanguageTag().getLocale();
182 return m_xCharacterClassification->toLower(rString, 0, rString.getLength(), rLocale);
185 CommandListBox::CommandListBox(weld::Window* pParent, uno::Reference<frame::XFrame> const& xFrame)
186 : mxBuilder(Application::CreateBuilder(pParent, "sfx/ui/commandpopup.ui"))
187 , mxPopover(mxBuilder->weld_popover("CommandPopup"))
188 , mpEntry(mxBuilder->weld_entry("command_entry"))
189 , mpCommandTreeView(mxBuilder->weld_tree_view("command_treeview"))
190 , mpMenuContentHandler(std::make_unique<MenuContentHandler>(xFrame))
192 mpEntry->connect_changed(LINK(this, CommandListBox, ModifyHdl));
193 mpEntry->connect_key_press(LINK(this, CommandListBox, TreeViewKeyPress));
194 mpCommandTreeView->connect_query_tooltip(LINK(this, CommandListBox, QueryTooltip));
195 mpCommandTreeView->connect_row_activated(LINK(this, CommandListBox, RowActivated));
197 Size aFrameSize = pParent->get_size();
199 // Set size of the pop-over window
200 tools::Long nWidth = std::max(tools::Long(400), aFrameSize.Width() / 3);
201 mpCommandTreeView->set_size_request(nWidth, 400);
203 // Set the location of the pop-over window
204 tools::Rectangle aRect(Point(aFrameSize.Width() / 2, 0), Size(0, 0));
205 mxPopover->popup_at_rect(pParent, aRect);
206 mpEntry->grab_focus();
209 IMPL_LINK_NOARG(CommandListBox, QueryTooltip, const weld::TreeIter&, OUString)
211 size_t nSelected = mpCommandTreeView->get_selected_index();
212 if (nSelected < maCommandList.size())
214 auto const& rCurrent = maCommandList[nSelected];
215 return rCurrent.m_aTooltip;
217 return OUString();
220 IMPL_LINK_NOARG(CommandListBox, RowActivated, weld::TreeView&, bool)
222 OUString aCommandURL;
223 int nSelected = mpCommandTreeView->get_selected_index();
224 if (nSelected != -1 && nSelected < int(maCommandList.size()))
226 auto const& rCurrent = maCommandList[nSelected];
227 aCommandURL = rCurrent.m_aCommandURL;
229 dispatchCommandAndClose(aCommandURL);
230 return true;
233 IMPL_LINK(CommandListBox, TreeViewKeyPress, const KeyEvent&, rKeyEvent, bool)
235 if (rKeyEvent.GetKeyCode().GetCode() == KEY_DOWN || rKeyEvent.GetKeyCode().GetCode() == KEY_UP)
237 int nDirection = rKeyEvent.GetKeyCode().GetCode() == KEY_DOWN ? 1 : -1;
238 int nNewIndex = mpCommandTreeView->get_selected_index() + nDirection;
239 nNewIndex = std::clamp(nNewIndex, 0, mpCommandTreeView->n_children() - 1);
240 mpCommandTreeView->select(nNewIndex);
241 mpCommandTreeView->set_cursor(nNewIndex);
242 return true;
244 else if (rKeyEvent.GetKeyCode().GetCode() == KEY_RETURN)
246 RowActivated(*mpCommandTreeView);
247 return true;
250 return false;
253 IMPL_LINK_NOARG(CommandListBox, ModifyHdl, weld::Entry&, void)
255 mpCommandTreeView->clear();
256 maCommandList.clear();
258 OUString sText = mpEntry->get_text();
259 if (sText.isEmpty())
260 return;
262 mpCommandTreeView->freeze();
263 mpMenuContentHandler->findInMenu(sText, mpCommandTreeView, maCommandList);
264 mpCommandTreeView->thaw();
266 if (mpCommandTreeView->n_children() > 0)
268 mpCommandTreeView->set_cursor(0);
269 mpCommandTreeView->select(0);
272 mpEntry->grab_focus();
275 void CommandListBox::dispatchCommandAndClose(OUString const& rCommand)
277 mxPopover->popdown();
279 if (!rCommand.isEmpty())
280 comphelper::dispatchCommand(rCommand, uno::Sequence<beans::PropertyValue>());
283 void CommandPopupHandler::showPopup(weld::Window* pParent,
284 css::uno::Reference<css::frame::XFrame> const& xFrame)
286 auto pCommandListBox = std::make_unique<CommandListBox>(pParent, xFrame);
287 pCommandListBox->connect_closed(LINK(this, CommandPopupHandler, PopupModeEnd));
288 mpListBox = std::move(pCommandListBox);
291 IMPL_LINK_NOARG(CommandPopupHandler, PopupModeEnd, weld::Popover&, void) { mpListBox.reset(); }
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */