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