xref: /aoo42x/main/sfx2/source/menu/thessubmenu.cxx (revision 79aad27f)
1*d119d52dSAndrew Rist /**************************************************************
2*d119d52dSAndrew Rist  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28cdf0e10cSrcweir #include <com/sun/star/linguistic2/XThesaurus.hpp>
29cdf0e10cSrcweir #include <com/sun/star/linguistic2/XMeaning.hpp>
30cdf0e10cSrcweir #include <com/sun/star/linguistic2/XLinguServiceManager.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
33cdf0e10cSrcweir #include <svl/stritem.hxx>
34cdf0e10cSrcweir #include <tools/debug.hxx>
35cdf0e10cSrcweir #include <vcl/graph.hxx>
36cdf0e10cSrcweir #include <svtools/filter.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <vector>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <sfx2/bindings.hxx>
42cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
43cdf0e10cSrcweir #include <sfx2/viewsh.hxx>
44cdf0e10cSrcweir #include "thessubmenu.hxx"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir using namespace ::com::sun::star;
48cdf0e10cSrcweir using ::rtl::OUString;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir // STATIC DATA -----------------------------------------------------------
52cdf0e10cSrcweir 
53cdf0e10cSrcweir SFX_IMPL_MENU_CONTROL(SfxThesSubMenuControl, SfxStringItem);
54cdf0e10cSrcweir 
55cdf0e10cSrcweir ////////////////////////////////////////////////////////////
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 
58cdf0e10cSrcweir /*
59cdf0e10cSrcweir 	Ctor; setzt Select-Handler am Menu und traegt Menu
60cdf0e10cSrcweir 	in seinen Parent ein.
61cdf0e10cSrcweir  */
SfxThesSubMenuControl(sal_uInt16 nSlotId,Menu & rMenu,SfxBindings & rBindings)62cdf0e10cSrcweir SfxThesSubMenuControl::SfxThesSubMenuControl( sal_uInt16 nSlotId, Menu &rMenu, SfxBindings &rBindings )
63cdf0e10cSrcweir     : SfxMenuControl( nSlotId, rBindings ),
64cdf0e10cSrcweir     pMenu(new PopupMenu),
65cdf0e10cSrcweir     rParent(rMenu)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 	rMenu.SetPopupMenu(nSlotId, pMenu);
68cdf0e10cSrcweir 	pMenu->SetSelectHdl(LINK(this, SfxThesSubMenuControl, MenuSelect));
69cdf0e10cSrcweir 	pMenu->Clear();
70cdf0e10cSrcweir 	rParent.EnableItem( GetId(), sal_False );
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 
~SfxThesSubMenuControl()74cdf0e10cSrcweir SfxThesSubMenuControl::~SfxThesSubMenuControl()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	delete pMenu;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 
80cdf0e10cSrcweir /*
81cdf0e10cSrcweir 	Statusbenachrichtigung;
82cdf0e10cSrcweir 	Ist die Funktionalit"at disabled, wird der entsprechende
83cdf0e10cSrcweir 	Menueeintrag im Parentmenu disabled, andernfalls wird er enabled.
84cdf0e10cSrcweir  */
StateChanged(sal_uInt16,SfxItemState eState,const SfxPoolItem *)85cdf0e10cSrcweir void SfxThesSubMenuControl::StateChanged(
86cdf0e10cSrcweir     sal_uInt16 /*nSID*/,
87cdf0e10cSrcweir     SfxItemState eState,
88cdf0e10cSrcweir 	const SfxPoolItem* /*pState*/ )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir 	rParent.EnableItem(GetId(), SFX_ITEM_AVAILABLE == eState );
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94cdf0e10cSrcweir /*
95cdf0e10cSrcweir 	Select-Handler des Menus;
96cdf0e10cSrcweir 	das selektierte Verb mit ausgef"uhrt,
97cdf0e10cSrcweir  */
IMPL_LINK_INLINE_START(SfxThesSubMenuControl,MenuSelect,Menu *,pSelMenu)98cdf0e10cSrcweir IMPL_LINK_INLINE_START( SfxThesSubMenuControl, MenuSelect, Menu *, pSelMenu )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	const sal_uInt16 nSlotId = pSelMenu->GetCurItemId();
101cdf0e10cSrcweir     if( nSlotId )
102cdf0e10cSrcweir         GetBindings().Execute(nSlotId);
103cdf0e10cSrcweir 	return 1;
104cdf0e10cSrcweir }
IMPL_LINK_INLINE_END(SfxThesSubMenuControl,MenuSelect,Menu *,pSelMenu)105cdf0e10cSrcweir IMPL_LINK_INLINE_END( SfxThesSubMenuControl, MenuSelect, Menu *, pSelMenu )
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 
108cdf0e10cSrcweir PopupMenu* SfxThesSubMenuControl::GetPopup() const
109cdf0e10cSrcweir {
110cdf0e10cSrcweir 	return pMenu;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 
114cdf0e10cSrcweir ////////////////////////////////////////////////////////////
115cdf0e10cSrcweir 
GetText(const String & rLookUpString,xub_StrLen nDelimPos)116cdf0e10cSrcweir OUString SfxThesSubMenuHelper::GetText(
117cdf0e10cSrcweir     const String &rLookUpString,
118cdf0e10cSrcweir     xub_StrLen nDelimPos )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir     return OUString( rLookUpString.Copy( 0, nDelimPos ) );
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 
GetLocale(lang::Locale & rLocale,const String & rLookUpString,xub_StrLen nDelimPos)124cdf0e10cSrcweir void SfxThesSubMenuHelper::GetLocale(
125cdf0e10cSrcweir     lang::Locale /*out */ &rLocale,
126cdf0e10cSrcweir     const String &rLookUpString,
127cdf0e10cSrcweir     xub_StrLen nDelimPos  )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     String aIsoLang( rLookUpString.Copy( nDelimPos + 1) );
130cdf0e10cSrcweir     const xub_StrLen nPos = aIsoLang.Search( '-' );
131cdf0e10cSrcweir     if (nPos != STRING_NOTFOUND)
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         rLocale.Language    = aIsoLang.Copy( 0, nPos );
134cdf0e10cSrcweir         rLocale.Country     = aIsoLang.Copy( nPos + 1 );
135cdf0e10cSrcweir         rLocale.Variant     = String::EmptyString();
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 
SfxThesSubMenuHelper()140cdf0e10cSrcweir SfxThesSubMenuHelper::SfxThesSubMenuHelper()
141cdf0e10cSrcweir {
142cdf0e10cSrcweir     try
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         uno::Reference< lang::XMultiServiceFactory >  xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
145cdf0e10cSrcweir         m_xLngMgr = uno::Reference< linguistic2::XLinguServiceManager >( xMSF->createInstance(
146cdf0e10cSrcweir                 OUString( RTL_CONSTASCII_USTRINGPARAM(
147cdf0e10cSrcweir                     "com.sun.star.linguistic2.LinguServiceManager" ))), uno::UNO_QUERY_THROW );
148cdf0e10cSrcweir         m_xThesarus = m_xLngMgr->getThesaurus();
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir     catch (uno::Exception &e)
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir         (void) e;
153cdf0e10cSrcweir         DBG_ASSERT( 0, "failed to get thesaurus" );
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 
~SfxThesSubMenuHelper()158cdf0e10cSrcweir SfxThesSubMenuHelper::~SfxThesSubMenuHelper()
159cdf0e10cSrcweir {
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 
IsSupportedLocale(const lang::Locale & rLocale) const163cdf0e10cSrcweir bool SfxThesSubMenuHelper::IsSupportedLocale( const lang::Locale & rLocale ) const
164cdf0e10cSrcweir {
165cdf0e10cSrcweir     return m_xThesarus.is() && m_xThesarus->hasLocale( rLocale );
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 
GetMeanings(std::vector<OUString> & rSynonyms,const OUString & rWord,const lang::Locale & rLocale,sal_Int16 nMaxSynonms)169cdf0e10cSrcweir bool SfxThesSubMenuHelper::GetMeanings(
170cdf0e10cSrcweir     std::vector< OUString > & rSynonyms,
171cdf0e10cSrcweir     const OUString & rWord,
172cdf0e10cSrcweir     const lang::Locale & rLocale,
173cdf0e10cSrcweir     sal_Int16 nMaxSynonms )
174cdf0e10cSrcweir {
175cdf0e10cSrcweir     bool bHasMoreSynonyms = false;
176cdf0e10cSrcweir     rSynonyms.clear();
177cdf0e10cSrcweir     if (IsSupportedLocale( rLocale ) && rWord.getLength() && nMaxSynonms > 0)
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir         try
180cdf0e10cSrcweir         {
181cdf0e10cSrcweir             // get all meannings
182cdf0e10cSrcweir             const uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeaningSeq(
183cdf0e10cSrcweir                     m_xThesarus->queryMeanings( rWord, rLocale, uno::Sequence< beans::PropertyValue >() ));
184cdf0e10cSrcweir             const uno::Reference< linguistic2::XMeaning > *pxMeaning = aMeaningSeq.getConstArray();
185cdf0e10cSrcweir             const sal_Int32 nMeanings = aMeaningSeq.getLength();
186cdf0e10cSrcweir 
187cdf0e10cSrcweir             // iterate over all meanings until nMaxSynonms are found or all meanings are processed
188cdf0e10cSrcweir             sal_Int32 nCount = 0;
189cdf0e10cSrcweir             sal_Int32 i = 0;
190cdf0e10cSrcweir             for ( ;  i < nMeanings && nCount < nMaxSynonms;  ++i)
191cdf0e10cSrcweir             {
192cdf0e10cSrcweir                 const uno::Sequence< OUString > aSynonymSeq( pxMeaning[i]->querySynonyms() );
193cdf0e10cSrcweir                 const OUString *pSynonyms = aSynonymSeq.getConstArray();
194cdf0e10cSrcweir                 const sal_Int32 nSynonyms = aSynonymSeq.getLength();
195cdf0e10cSrcweir                 sal_Int32 k = 0;
196cdf0e10cSrcweir                 for ( ;  k < nSynonyms && nCount < nMaxSynonms;  ++k)
197cdf0e10cSrcweir                 {
198cdf0e10cSrcweir                     rSynonyms.push_back( pSynonyms[k] );
199cdf0e10cSrcweir                     ++nCount;
200cdf0e10cSrcweir                 }
201cdf0e10cSrcweir                 bHasMoreSynonyms = k < nSynonyms;    // any synonym from this meaning skipped?
202cdf0e10cSrcweir             }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir             bHasMoreSynonyms |= i < nMeanings;   // any meaning skipped?
205cdf0e10cSrcweir         }
206cdf0e10cSrcweir         catch (uno::Exception &e)
207cdf0e10cSrcweir         {
208cdf0e10cSrcweir             (void) e;
209cdf0e10cSrcweir             DBG_ASSERT( 0, "failed to get synonyms" );
210cdf0e10cSrcweir         }
211cdf0e10cSrcweir     }
212cdf0e10cSrcweir     return bHasMoreSynonyms;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 
GetThesImplName(const lang::Locale & rLocale) const216cdf0e10cSrcweir String SfxThesSubMenuHelper::GetThesImplName( const lang::Locale &rLocale ) const
217cdf0e10cSrcweir {
218cdf0e10cSrcweir     String aRes;
219cdf0e10cSrcweir     DBG_ASSERT( m_xLngMgr.is(), "LinguServiceManager missing" );
220cdf0e10cSrcweir     if (m_xLngMgr.is())
221cdf0e10cSrcweir     {
222cdf0e10cSrcweir         uno::Sequence< OUString > aServiceNames = m_xLngMgr->getConfiguredServices(
223cdf0e10cSrcweir                 OUString::createFromAscii("com.sun.star.linguistic2.Thesaurus"), rLocale );
224cdf0e10cSrcweir         // there should be at most one thesaurus configured for each language
225cdf0e10cSrcweir         DBG_ASSERT( aServiceNames.getLength() <= 1, "more than one thesaurus found. Should not be possible" );
226cdf0e10cSrcweir         if (aServiceNames.getLength() == 1)
227cdf0e10cSrcweir             aRes = aServiceNames[0];
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir     return aRes;
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir ////////////////////////////////////////////////////////////
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 
235