calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / framework / source / uielement / thesaurusmenucontroller.cxx
bloba0e9f93218f2490c798d5d1ed5df5b55d704e206
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/processfactory.hxx>
21 #include <comphelper/propertyvalue.hxx>
22 #include <i18nlangtag/languagetag.hxx>
23 #include <sal/log.hxx>
24 #include <svl/lngmisc.hxx>
25 #include <svtools/popupmenucontrollerbase.hxx>
26 #include <comphelper/diagnose_ex.hxx>
27 #include <unotools/lingucfg.hxx>
28 #include <vcl/commandinfoprovider.hxx>
30 #include <com/sun/star/graphic/GraphicProvider.hpp>
31 #include <com/sun/star/linguistic2/LinguServiceManager.hpp>
33 namespace {
35 class ThesaurusMenuController : public svt::PopupMenuControllerBase
37 public:
38 explicit ThesaurusMenuController( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
40 // XStatusListener
41 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
43 // XServiceInfo
44 virtual OUString SAL_CALL getImplementationName() override;
45 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
47 private:
48 void fillPopupMenu();
49 void getMeanings( std::vector< OUString >& rSynonyms, const OUString& rWord, const css::lang::Locale& rLocale, size_t nMaxSynonms );
50 OUString getThesImplName( const css::lang::Locale& rLocale ) const;
51 css::uno::Reference< css::linguistic2::XLinguServiceManager2 > m_xLinguServiceManager;
52 css::uno::Reference< css::linguistic2::XThesaurus > m_xThesaurus;
53 OUString m_aLastWord;
58 ThesaurusMenuController::ThesaurusMenuController( const css::uno::Reference< css::uno::XComponentContext >& rxContext ) :
59 svt::PopupMenuControllerBase( rxContext ),
60 m_xLinguServiceManager( css::linguistic2::LinguServiceManager::create( rxContext ) ),
61 m_xThesaurus( m_xLinguServiceManager->getThesaurus() )
65 void ThesaurusMenuController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
67 rEvent.State >>= m_aLastWord;
68 m_xPopupMenu->clear();
69 if ( rEvent.IsEnabled )
70 fillPopupMenu();
73 void ThesaurusMenuController::fillPopupMenu()
75 sal_Int32 nIdx{ 0 };
76 OUString aText = m_aLastWord.getToken(0, '#', nIdx);
77 OUString aIsoLang = m_aLastWord.getToken(0, '#', nIdx);
78 if ( aText.isEmpty() || aIsoLang.isEmpty() )
79 return;
81 std::vector< OUString > aSynonyms;
82 css::lang::Locale aLocale = LanguageTag::convertToLocale( aIsoLang );
83 getMeanings( aSynonyms, aText, aLocale, 7 /*max number of synonyms to retrieve*/ );
85 m_xPopupMenu->enableAutoMnemonics(false);
86 if ( aSynonyms.empty() )
87 return;
89 SvtLinguConfig aCfg;
90 css::uno::Reference<css::graphic::XGraphic> xGraphic;
91 OUString aThesImplName( getThesImplName( aLocale ) );
92 OUString aSynonymsImageUrl( aCfg.GetSynonymsContextImage( aThesImplName ) );
93 if (!aThesImplName.isEmpty() && !aSynonymsImageUrl.isEmpty())
95 try
97 css::uno::Reference<css::uno::XComponentContext> xContext(::comphelper::getProcessComponentContext());
98 css::uno::Reference<css::graphic::XGraphicProvider> xProvider(css::graphic::GraphicProvider::create(xContext));
99 xGraphic = xProvider->queryGraphic({ comphelper::makePropertyValue("URL", aSynonymsImageUrl) });
101 catch (const css::uno::Exception&)
103 DBG_UNHANDLED_EXCEPTION("fwk");
107 sal_uInt16 nId = 1;
108 for ( const auto& aSynonym : aSynonyms )
110 OUString aItemText( linguistic::GetThesaurusReplaceText( aSynonym ) );
111 m_xPopupMenu->insertItem(nId, aItemText, 0, -1);
112 m_xPopupMenu->setCommand(nId, ".uno:ThesaurusFromContext?WordReplace:string=" + aItemText);
114 if (xGraphic.is())
115 m_xPopupMenu->setItemImage(nId, xGraphic, false);
117 nId++;
120 m_xPopupMenu->insertSeparator(-1);
121 OUString aThesaurusDialogCmd( ".uno:ThesaurusDialog" );
122 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(aThesaurusDialogCmd, m_aModuleName);
123 m_xPopupMenu->insertItem(nId, vcl::CommandInfoProvider::GetPopupLabelForCommand(aProperties), 0, -1);
124 m_xPopupMenu->setCommand(nId, aThesaurusDialogCmd);
127 void ThesaurusMenuController::getMeanings( std::vector< OUString >& rSynonyms, const OUString& rWord,
128 const css::lang::Locale& rLocale, size_t nMaxSynonms )
130 rSynonyms.clear();
131 if ( !(m_xThesaurus.is() && m_xThesaurus->hasLocale( rLocale ) && !rWord.isEmpty() && nMaxSynonms > 0) )
132 return;
136 const css::uno::Sequence< css::uno::Reference< css::linguistic2::XMeaning > > aMeaningSeq(
137 m_xThesaurus->queryMeanings( rWord, rLocale, css::uno::Sequence< css::beans::PropertyValue >() ) );
139 for ( const auto& xMeaning : aMeaningSeq )
141 const css::uno::Sequence< OUString > aSynonymSeq( xMeaning->querySynonyms() );
142 for ( const auto& aSynonym : aSynonymSeq )
144 rSynonyms.push_back( aSynonym );
145 if ( rSynonyms.size() == nMaxSynonms )
146 return;
150 catch ( const css::uno::Exception& )
152 SAL_WARN( "fwk.uielement", "Failed to get synonyms" );
156 OUString ThesaurusMenuController::getThesImplName( const css::lang::Locale& rLocale ) const
158 css::uno::Sequence< OUString > aServiceNames =
159 m_xLinguServiceManager->getConfiguredServices( "com.sun.star.linguistic2.Thesaurus", rLocale );
160 SAL_WARN_IF( aServiceNames.getLength() > 1, "fwk.uielement", "Only one thesaurus is allowed per locale, but found more!" );
161 if ( aServiceNames.getLength() == 1 )
162 return aServiceNames[0];
164 return OUString();
167 OUString ThesaurusMenuController::getImplementationName()
169 return "com.sun.star.comp.framework.ThesaurusMenuController";
172 css::uno::Sequence< OUString > ThesaurusMenuController::getSupportedServiceNames()
174 return { "com.sun.star.frame.PopupMenuController" };
177 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
178 com_sun_star_comp_framework_ThesaurusMenuController_get_implementation(
179 css::uno::XComponentContext* xContext,
180 css::uno::Sequence< css::uno::Any > const & )
182 return cppu::acquire( new ThesaurusMenuController( xContext ) );
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */