LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / commandpopup / CommandPopup.cxx
blob7575a5794b32731d669ac40382455c691fa6f376
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 <workwin.hxx>
14 #include <sfx2/msgpool.hxx>
15 #include <sfx2/bindings.hxx>
17 #include <comphelper/processfactory.hxx>
18 #include <comphelper/dispatchcommand.hxx>
20 #include <com/sun/star/frame/Desktop.hpp>
21 #include <com/sun/star/frame/theUICommandDescription.hpp>
22 #include <com/sun/star/ui/theUICategoryDescription.hpp>
23 #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
24 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <com/sun/star/util/URL.hpp>
27 #include <com/sun/star/util/URLTransformer.hpp>
28 #include <com/sun/star/i18n/CharacterClassification.hpp>
30 #include <vcl/commandinfoprovider.hxx>
31 #include <vcl/svapp.hxx>
32 #include <i18nlangtag/languagetag.hxx>
34 using namespace css;
36 MenuContentHandler::MenuContentHandler(uno::Reference<frame::XFrame> const& xFrame)
37 : m_xContext(comphelper::getProcessComponentContext())
38 , m_xFrame(xFrame)
39 , m_xCharacterClassification(i18n::CharacterClassification::create(m_xContext))
40 , m_xURLTransformer(util::URLTransformer::create(m_xContext))
41 , m_sModuleLongName(vcl::CommandInfoProvider::GetModuleIdentifier(xFrame))
43 uno::Reference<ui::XModuleUIConfigurationManagerSupplier> xModuleConfigSupplier;
44 xModuleConfigSupplier.set(ui::theModuleUIConfigurationManagerSupplier::get(m_xContext));
46 uno::Reference<ui::XUIConfigurationManager> xConfigurationManager;
47 xConfigurationManager = xModuleConfigSupplier->getUIConfigurationManager(m_sModuleLongName);
49 uno::Reference<container::XIndexAccess> xConfigData;
50 xConfigData = xConfigurationManager->getSettings("private:resource/menubar/menubar", false);
52 gatherMenuContent(xConfigData, m_aMenuContent);
55 void MenuContentHandler::gatherMenuContent(
56 uno::Reference<container::XIndexAccess> const& xIndexAccess, MenuContent& rMenuContent)
58 for (sal_Int32 n = 0; n < xIndexAccess->getCount(); n++)
60 MenuContent aNewContent;
61 uno::Sequence<beans::PropertyValue> aProperties;
62 uno::Reference<container::XIndexAccess> xIndexContainer;
64 if (!(xIndexAccess->getByIndex(n) >>= aProperties))
65 continue;
67 bool bIsVisible = true;
68 bool bIsEnabled = true;
70 for (auto const& rProperty : std::as_const(aProperties))
72 OUString aPropertyName = rProperty.Name;
73 if (aPropertyName == "CommandURL")
74 rProperty.Value >>= aNewContent.m_aCommandURL;
75 else if (aPropertyName == "ItemDescriptorContainer")
76 rProperty.Value >>= xIndexContainer;
77 else if (aPropertyName == "IsVisible")
78 rProperty.Value >>= bIsVisible;
79 else if (aPropertyName == "Enabled")
80 rProperty.Value >>= bIsEnabled;
83 if (!bIsEnabled || !bIsVisible)
84 continue;
86 auto aCommandProperties = vcl::CommandInfoProvider::GetCommandProperties(
87 aNewContent.m_aCommandURL, m_sModuleLongName);
88 OUString aLabel = vcl::CommandInfoProvider::GetLabelForCommand(aCommandProperties);
89 aNewContent.m_aMenuLabel = aLabel;
90 aNewContent.m_aSearchableMenuLabel = toLower(aLabel);
92 if (!rMenuContent.m_aFullLabelWithPath.isEmpty())
93 aNewContent.m_aFullLabelWithPath = rMenuContent.m_aFullLabelWithPath + " / ";
94 aNewContent.m_aFullLabelWithPath += aNewContent.m_aMenuLabel;
96 aNewContent.m_aTooltip = vcl::CommandInfoProvider::GetTooltipForCommand(
97 aNewContent.m_aCommandURL, aCommandProperties, m_xFrame);
99 if (xIndexContainer.is())
100 gatherMenuContent(xIndexContainer, aNewContent);
102 rMenuContent.m_aSubMenuContent.push_back(aNewContent);
106 void MenuContentHandler::findInMenu(OUString const& rText,
107 std::unique_ptr<weld::TreeView>& rpCommandTreeView,
108 std::vector<CurrentEntry>& rCommandList)
110 m_aAdded.clear();
112 OUString aLowerCaseText = toLower(rText);
114 auto aTextStartCriterium = [](MenuContent const& rMenuContent, OUString const& rSearchText) {
115 return rMenuContent.m_aSearchableMenuLabel.startsWith(rSearchText);
118 findInMenuRecursive(m_aMenuContent, aLowerCaseText, rpCommandTreeView, rCommandList,
119 aTextStartCriterium);
121 auto aTextAllCriterium = [](MenuContent const& rMenuContent, OUString const& rSearchText) {
122 return rMenuContent.m_aSearchableMenuLabel.indexOf(rSearchText) > 0;
125 findInMenuRecursive(m_aMenuContent, aLowerCaseText, rpCommandTreeView, rCommandList,
126 aTextAllCriterium);
129 void MenuContentHandler::findInMenuRecursive(
130 MenuContent const& rMenuContent, OUString const& rText,
131 std::unique_ptr<weld::TreeView>& rpCommandTreeView, std::vector<CurrentEntry>& rCommandList,
132 std::function<bool(MenuContent const&, OUString const&)> const& rSearchCriterium)
134 for (MenuContent const& aSubContent : rMenuContent.m_aSubMenuContent)
136 if (rSearchCriterium(aSubContent, rText))
138 addCommandIfPossible(aSubContent, rpCommandTreeView, rCommandList);
140 findInMenuRecursive(aSubContent, rText, rpCommandTreeView, rCommandList, rSearchCriterium);
144 void MenuContentHandler::addCommandIfPossible(
145 MenuContent const& rMenuContent, const std::unique_ptr<weld::TreeView>& rpCommandTreeView,
146 std::vector<CurrentEntry>& rCommandList)
148 if (m_aAdded.find(rMenuContent.m_aFullLabelWithPath) != m_aAdded.end())
149 return;
151 OUString sCommandURL = rMenuContent.m_aCommandURL;
152 util::URL aCommandURL;
153 aCommandURL.Complete = sCommandURL;
155 if (!m_xURLTransformer->parseStrict(aCommandURL))
156 return;
158 auto* pViewFrame = SfxViewFrame::Current();
160 SfxSlotPool& rSlotPool = SfxSlotPool::GetSlotPool(pViewFrame);
161 const SfxSlot* pSlot = rSlotPool.GetUnoSlot(aCommandURL.Path);
162 if (!pSlot)
163 return;
165 std::unique_ptr<SfxPoolItem> pState;
166 SfxItemState eState = pViewFrame->GetBindings().QueryState(pSlot->GetSlotId(), pState);
167 if (eState == SfxItemState::DISABLED)
168 return;
170 auto xGraphic = vcl::CommandInfoProvider::GetXGraphicForCommand(sCommandURL, m_xFrame);
171 rCommandList.emplace_back(sCommandURL, rMenuContent.m_aTooltip);
173 auto pIter = rpCommandTreeView->make_iterator();
174 rpCommandTreeView->insert(nullptr, -1, &rMenuContent.m_aFullLabelWithPath, nullptr, nullptr,
175 nullptr, false, pIter.get());
176 rpCommandTreeView->set_image(*pIter, xGraphic);
177 m_aAdded.insert(rMenuContent.m_aFullLabelWithPath);
180 OUString MenuContentHandler::toLower(OUString const& rString)
182 const css::lang::Locale& rLocale = Application::GetSettings().GetUILanguageTag().getLocale();
184 return m_xCharacterClassification->toLower(rString, 0, rString.getLength(), rLocale);
187 CommandListBox::CommandListBox(weld::Window* pParent, uno::Reference<frame::XFrame> const& xFrame)
188 : mxBuilder(Application::CreateBuilder(pParent, "sfx/ui/commandpopup.ui"))
189 , mxPopover(mxBuilder->weld_popover("CommandPopup"))
190 , mpEntry(mxBuilder->weld_entry("command_entry"))
191 , mpCommandTreeView(mxBuilder->weld_tree_view("command_treeview"))
192 , mpMenuContentHandler(std::make_unique<MenuContentHandler>(xFrame))
194 mpEntry->connect_changed(LINK(this, CommandListBox, ModifyHdl));
195 mpEntry->connect_key_press(LINK(this, CommandListBox, TreeViewKeyPress));
196 mpCommandTreeView->connect_query_tooltip(LINK(this, CommandListBox, QueryTooltip));
197 mpCommandTreeView->connect_row_activated(LINK(this, CommandListBox, RowActivated));
199 Size aFrameSize = pParent->get_size();
201 // Set size of the pop-over window
202 tools::Long nWidth = std::max(tools::Long(400), aFrameSize.Width() / 3);
203 mpCommandTreeView->set_size_request(nWidth, 400);
205 // Set the location of the pop-over window
206 tools::Rectangle aRect(Point(aFrameSize.Width() / 2, 0), Size(0, 0));
207 mxPopover->popup_at_rect(pParent, aRect);
208 mpEntry->grab_focus();
211 IMPL_LINK_NOARG(CommandListBox, QueryTooltip, const weld::TreeIter&, OUString)
213 size_t nSelected = mpCommandTreeView->get_selected_index();
214 if (nSelected < maCommandList.size())
216 auto const& rCurrent = maCommandList[nSelected];
217 return rCurrent.m_aTooltip;
219 return OUString();
222 IMPL_LINK_NOARG(CommandListBox, RowActivated, weld::TreeView&, bool)
224 OUString aCommandURL;
225 int nSelected = mpCommandTreeView->get_selected_index();
226 if (nSelected != -1 && nSelected < int(maCommandList.size()))
228 auto const& rCurrent = maCommandList[nSelected];
229 aCommandURL = rCurrent.m_aCommandURL;
231 dispatchCommandAndClose(aCommandURL);
232 return true;
235 IMPL_LINK(CommandListBox, TreeViewKeyPress, const KeyEvent&, rKeyEvent, bool)
237 if (rKeyEvent.GetKeyCode().GetCode() == KEY_DOWN || rKeyEvent.GetKeyCode().GetCode() == KEY_UP)
239 int nDirection = rKeyEvent.GetKeyCode().GetCode() == KEY_DOWN ? 1 : -1;
240 int nNewIndex = mpCommandTreeView->get_selected_index() + nDirection;
241 nNewIndex = std::clamp(nNewIndex, 0, mpCommandTreeView->n_children() - 1);
242 mpCommandTreeView->select(nNewIndex);
243 mpCommandTreeView->set_cursor(nNewIndex);
244 return true;
246 else if (rKeyEvent.GetKeyCode().GetCode() == KEY_RETURN)
248 RowActivated(*mpCommandTreeView);
249 return true;
252 return false;
255 IMPL_LINK_NOARG(CommandListBox, ModifyHdl, weld::Entry&, void)
257 mpCommandTreeView->clear();
258 maCommandList.clear();
260 OUString sText = mpEntry->get_text();
261 if (sText.isEmpty())
262 return;
264 mpCommandTreeView->freeze();
265 mpMenuContentHandler->findInMenu(sText, mpCommandTreeView, maCommandList);
266 mpCommandTreeView->thaw();
268 if (mpCommandTreeView->n_children() > 0)
270 mpCommandTreeView->set_cursor(0);
271 mpCommandTreeView->select(0);
274 mpEntry->grab_focus();
277 void CommandListBox::dispatchCommandAndClose(OUString const& rCommand)
279 mxPopover->popdown();
281 if (!rCommand.isEmpty())
282 comphelper::dispatchCommand(rCommand, uno::Sequence<beans::PropertyValue>());
285 void CommandPopupHandler::showPopup(weld::Window* pParent,
286 css::uno::Reference<css::frame::XFrame> const& xFrame)
288 auto pCommandListBox = std::make_unique<CommandListBox>(pParent, xFrame);
289 pCommandListBox->connect_closed(LINK(this, CommandPopupHandler, PopupModeEnd));
290 mpListBox = std::move(pCommandListBox);
293 IMPL_LINK_NOARG(CommandPopupHandler, PopupModeEnd, weld::Popover&, void) { mpListBox.reset(); }
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */