update dev300-m58
[ooovba.git] / svx / source / dialog / thesdlg.cxx
blob78fb5fc85adf7f318b327475d3f94700e7d07612
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: thesdlg.cxx,v $
10 * $Revision: 1.21 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <tools/shl.hxx>
34 #include <svtools/lngmisc.hxx>
35 #include <vcl/wrkwin.hxx>
36 #ifndef _MSGBOX_HXX //autogen
37 #include <vcl/msgbox.hxx>
38 #endif
39 #ifndef _APP_HXX //autogen
40 #include <vcl/svapp.hxx>
41 #endif
42 #include <com/sun/star/linguistic2/XThesaurus.hpp>
43 #include <com/sun/star/linguistic2/XMeaning.hpp>
46 #define _SVX_THESDLG_CXX
48 #include <svx/thesdlg.hxx>
49 #include "dlgutil.hxx"
50 #include <svx/dialmgr.hxx>
51 #include "svxerr.hxx"
53 #include <svx/dialogs.hrc>
54 #include "thesdlg.hrc"
55 #include <unolingu.hxx>
56 #include <svx/langbox.hxx>
59 using namespace ::com::sun::star;
60 using namespace ::com::sun::star::lang;
61 using namespace ::com::sun::star::uno;
62 using namespace ::com::sun::star::beans;
63 using namespace ::com::sun::star::linguistic2;
65 #undef S2U
66 #undef U2S
67 //#define S2U(s) StringToOUString(s, CHARSET_SYSTEM)
68 //#define U2S(s) OUStringToString(s, CHARSET_SYSTEM)
71 // GetReplaceEditString -------------------------------
73 static void GetReplaceEditString( String &rText )
75 // The strings returned by the thesaurus saometimes have some
76 // explanation text put in between '(' and ')' or a trailing '*'.
77 // These parts should not be put in the ReplaceEdit Text that may get
78 // inserted into the document. Thus we strip them from the text.
80 xub_StrLen nPos = rText.Search( sal_Unicode('(') );
81 while (STRING_NOTFOUND != nPos)
83 xub_StrLen nEnd = rText.Search( sal_Unicode(')'), nPos );
84 if (STRING_NOTFOUND != nEnd)
85 rText.Erase( nPos, nEnd-nPos+1 );
86 else
87 break;
88 nPos = rText.Search( sal_Unicode('(') );
91 nPos = rText.Search( sal_Unicode('*') );
92 if (STRING_NOTFOUND != nPos)
93 rText.Erase( nPos );
95 // remove any possible remaining ' ' that may confuse the thesaurus
96 // when it gets called with the text
97 rText.EraseLeadingAndTrailingChars( sal_Unicode(' ') );
100 // struct ThesDlg_Impl ---------------------------------------------------
102 struct ThesDlg_Impl
104 Reference< XThesaurus > xThesaurus;
105 ::rtl::OUString aLookUpText;
106 sal_Int16 nLookUpLanguage;
108 ThesDlg_Impl( Reference< XThesaurus > & xThes );
109 SfxErrorContext* pErrContext; // ErrorContext,
110 // w"ahrend der Dialog oben ist
113 ThesDlg_Impl::ThesDlg_Impl(Reference< XThesaurus > & xThes) :
114 xThesaurus (xThes)
116 pErrContext = NULL;
117 nLookUpLanguage = LANGUAGE_NONE;
121 // class SvxThesaurusLanguageDlg_Impl ------------------------------------
123 class SvxThesaurusLanguageDlg_Impl : public ModalDialog
125 private:
126 SvxLanguageBox aLangLB;
127 FixedLine aLangFL;
128 OKButton aOKBtn;
129 CancelButton aCancelBtn;
130 HelpButton aHelpBtn;
132 DECL_LINK( DoubleClickHdl_Impl, ListBox * );
134 public:
135 SvxThesaurusLanguageDlg_Impl( Window* pParent );
137 sal_uInt16 GetLanguage() const;
138 void SetLanguage( sal_uInt16 nLang );
141 // -----------------------------------------------------------------------
144 SvxThesaurusLanguageDlg_Impl::SvxThesaurusLanguageDlg_Impl( Window* pParent ) :
146 ModalDialog( pParent, SVX_RES( RID_SVXDLG_THES_LANGUAGE ) ),
148 aLangLB ( this, SVX_RES( LB_THES_LANGUAGE ) ),
149 aLangFL ( this, SVX_RES( FL_THES_LANGUAGE ) ),
150 aOKBtn ( this, SVX_RES( BTN_LANG_OK ) ),
151 aCancelBtn ( this, SVX_RES( BTN_LANG_CANCEL ) ),
152 aHelpBtn ( this, SVX_RES( BTN_LANG_HELP ) )
154 FreeResource();
156 aLangLB.SetLanguageList( LANG_LIST_THES_USED, FALSE, FALSE );
157 aLangLB.SetDoubleClickHdl(
158 LINK( this, SvxThesaurusLanguageDlg_Impl, DoubleClickHdl_Impl ) );
162 // -----------------------------------------------------------------------
164 sal_uInt16 SvxThesaurusLanguageDlg_Impl::GetLanguage() const
166 sal_uInt16 nLang = aLangLB.GetSelectLanguage();
167 return nLang;
170 // -----------------------------------------------------------------------
172 void SvxThesaurusLanguageDlg_Impl::SetLanguage( sal_uInt16 nLang )
174 aLangLB.SelectLanguage( nLang );
177 // -----------------------------------------------------------------------
179 IMPL_LINK_INLINE_START( SvxThesaurusLanguageDlg_Impl, DoubleClickHdl_Impl, ListBox *, EMPTYARG )
181 EndDialog( RET_OK );
182 return 0;
184 IMPL_LINK_INLINE_END( SvxThesaurusLanguageDlg_Impl, DoubleClickHdl_Impl, ListBox *, EMPTYARG )
186 // class SvxThesaurusDialog ----------------------------------------------
189 // -----------------------------------------------------------------------
192 SvxThesaurusDialog::SvxThesaurusDialog( Window* pParent, Reference< XThesaurus > xThes,
193 const String &rWord, sal_Int16 nLanguage) :
195 SvxStandardDialog( pParent, SVX_RES( RID_SVXDLG_THESAURUS ) ),
197 aWordText ( this, SVX_RES( FT_WORD ) ),
198 aWordLB ( this, SVX_RES( LB_WORD ) ),
199 aReplaceText( this, SVX_RES( FT_REPL ) ),
200 aReplaceEdit( this, SVX_RES( ED_REPL ) ),
201 aMeanText ( this, SVX_RES( FT_MEAN ) ),
202 aMeanLB ( this, SVX_RES( LB_MEAN ) ),
203 aSynonymText( this, SVX_RES( FT_SYNON ) ),
204 aSynonymLB ( this, SVX_RES( LB_SYNON ) ),
205 aVarFL ( this, SVX_RES( FL_VAR ) ),
206 aOkBtn ( this, SVX_RES( BTN_THES_OK ) ),
207 aCancelBtn ( this, SVX_RES( BTN_THES_CANCEL ) ),
208 aLookUpBtn ( this, SVX_RES( BTN_LOOKUP ) ),
209 aLangBtn ( this, SVX_RES( BTN_LANGUAGE ) ),
210 aHelpBtn ( this, SVX_RES( BTN_THES_HELP ) ),
211 aErrStr ( SVX_RES( STR_ERR_WORDNOTFOUND ) )
213 pImpl = new ThesDlg_Impl( xThes );
214 pImpl->aLookUpText = ::rtl::OUString( rWord );
215 pImpl->nLookUpLanguage = nLanguage;
216 pImpl->pErrContext =
217 new SfxErrorContext( ERRCTX_SVX_LINGU_THESAURUS, String(), this,
218 RID_SVXERRCTX, &DIALOG_MGR() );
220 aLangBtn.SetClickHdl( LINK( this, SvxThesaurusDialog, LanguageHdl_Impl ) );
221 aLookUpBtn.SetClickHdl( LINK( this, SvxThesaurusDialog, LookUpHdl_Impl ) );
222 aMeanLB.SetSelectHdl( LINK( this, SvxThesaurusDialog, EntryHdl_Impl ) );
223 aSynonymLB.SetSelectHdl( LINK( this, SvxThesaurusDialog, SynonymHdl_Impl ) );
224 Link aLink = LINK( this, SvxThesaurusDialog, SelectHdl_Impl );
225 aMeanLB.SetDoubleClickHdl( aLink );
226 aSynonymLB.SetDoubleClickHdl( aLink );
227 aWordLB.SetSelectHdl( aLink );
229 FreeResource();
231 ::rtl::OUString aTmp( rWord );
232 linguistic::RemoveHyphens( aTmp );
233 linguistic::ReplaceControlChars( aTmp );
234 aReplaceEdit.SetText( aTmp );
235 aWordLB.InsertEntry( aTmp );
236 aWordLB.SelectEntry( aTmp );
238 Init_Impl( nLanguage );
240 // disable controls if service is missing
241 if (!pImpl->xThesaurus.is())
242 Enable( sal_False );
246 // -----------------------------------------------------------------------
248 SvxThesaurusDialog::~SvxThesaurusDialog()
250 delete pImpl->pErrContext;
251 delete pImpl;
254 // -----------------------------------------------------------------------
256 uno::Sequence< Reference< XMeaning > > SAL_CALL
257 SvxThesaurusDialog::queryMeanings_Impl(
258 ::rtl::OUString& rTerm,
259 const Locale& rLocale,
260 const beans::PropertyValues& rProperties )
261 throw(lang::IllegalArgumentException, uno::RuntimeException)
263 uno::Sequence< Reference< XMeaning > > aMeanings(
264 pImpl->xThesaurus->queryMeanings( rTerm, rLocale, rProperties ) );
266 // text with '.' at the end?
267 if (0 == aMeanings.getLength() && rTerm.getLength() &&
268 rTerm.getStr()[ rTerm.getLength() - 1 ] == '.')
270 // try again without trailing '.' chars. It may be a word at the
271 // end of a sentence and not an abbreviation...
272 String aTxt( rTerm );
273 aTxt.EraseTrailingChars( '.' );
274 aMeanings = pImpl->xThesaurus->queryMeanings( aTxt, rLocale, rProperties );
275 if (aMeanings.getLength())
277 rTerm = aTxt;
281 return aMeanings;
284 // -----------------------------------------------------------------------
286 sal_uInt16 SvxThesaurusDialog::GetLanguage() const
288 return pImpl->nLookUpLanguage;
291 // -----------------------------------------------------------------------
293 void SvxThesaurusDialog::UpdateMeaningBox_Impl( uno::Sequence< Reference< XMeaning > > *pMeaningSeq )
295 // create temporary meaning list if not supplied from somewhere else
296 sal_Bool bTmpSeq = sal_False;
297 if (!pMeaningSeq && pImpl->xThesaurus.is())
299 bTmpSeq = sal_True;
300 lang::Locale aLocale( SvxCreateLocale( pImpl->nLookUpLanguage ) );
301 uno::Sequence< Reference< XMeaning > > aTmpMean = queryMeanings_Impl(
302 pImpl->aLookUpText, aLocale, Sequence< PropertyValue >() );
304 pMeaningSeq = new Sequence< Reference< XMeaning > > ( aTmpMean );
306 // set new replace edit text if a different look up text was used
307 // see: queryMeanings_Impl
308 aReplaceEdit.SetText( pImpl->aLookUpText );
311 sal_Int32 nMeaningCount = pMeaningSeq ? pMeaningSeq->getLength() : 0;
312 const Reference< XMeaning > *pMeaning = pMeaningSeq ? pMeaningSeq->getConstArray() : NULL;
313 aMeanLB.Clear();
314 for ( sal_Int32 i = 0; i < nMeaningCount; ++i )
315 aMeanLB.InsertEntry( pMeaning[i]->getMeaning() );
317 // remove temporary meaning list
318 if (bTmpSeq)
319 delete pMeaningSeq;
321 if (aMeanLB.GetEntryCount() > 0)
322 aMeanLB.SelectEntryPos(0);
323 UpdateSynonymBox_Impl();
327 // -----------------------------------------------------------------------
328 void SvxThesaurusDialog::UpdateSynonymBox_Impl()
331 aSynonymLB.Clear();
333 sal_uInt16 nPos = aMeanLB.GetSelectEntryPos(); // active meaning pos
334 if (nPos != LISTBOX_ENTRY_NOTFOUND && pImpl->xThesaurus.is())
336 // get Reference< XMeaning > for selected meaning
337 lang::Locale aLocale( SvxCreateLocale( pImpl->nLookUpLanguage ) );
338 Reference< XMeaning > xMeaning = queryMeanings_Impl(
339 pImpl->aLookUpText, aLocale, Sequence< PropertyValue >() )
340 .getConstArray()[ nPos ];
342 uno::Sequence< ::rtl::OUString > aSynonyms;
343 if (xMeaning.is())
344 aSynonyms = xMeaning->querySynonyms();
346 sal_Int32 nSynonymCount = aSynonyms.getLength();
347 const ::rtl::OUString *pSynonym = aSynonyms.getConstArray();
348 for ( sal_Int32 i=0; i < nSynonymCount; ++i )
349 aSynonymLB.InsertEntry( pSynonym[i] );
354 // -----------------------------------------------------------------------
356 void SvxThesaurusDialog::Apply()
360 // -----------------------------------------------------------------------
361 void SvxThesaurusDialog::Init_Impl(sal_Int16 nLanguage)
363 // Sprache anpassen
364 String aStr( GetText() );
365 aStr.Erase( aStr.Search( sal_Unicode( '(' ) ) - 1 );
366 aStr.Append( UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( " (" ) ) );
367 //aStr += GetLanguageString( (LanguageType)pImpl->pThesaurus->GetLanguage() );
368 aStr += GetLanguageString( (LanguageType) nLanguage );
369 aStr.Append( sal_Unicode( ')' ) );
370 SetText( aStr ); // Text der Window-Leiste setzen (Wort + Sprache)
372 // adapt meanings according to (new) language
373 UpdateMeaningBox_Impl();
376 // -----------------------------------------------------------------------
378 IMPL_LINK( SvxThesaurusDialog, LookUpHdl_Impl, Button *, pBtn )
381 EnterWait();
383 String aText( aReplaceEdit.GetText() );
385 ::rtl::OUString aOldLookUpText = pImpl->aLookUpText;
386 pImpl->aLookUpText = ::rtl::OUString( aText );
388 uno::Sequence< Reference< XMeaning > > aMeanings;
389 if (pImpl->xThesaurus.is())
390 aMeanings = queryMeanings_Impl(
391 pImpl->aLookUpText,
392 SvxCreateLocale( pImpl->nLookUpLanguage ),
393 Sequence< PropertyValue >() );
395 LeaveWait();
396 if ( aMeanings.getLength() == 0 )
398 if( pBtn == &aCancelBtn ) // called via double click
400 pImpl->aLookUpText = aOldLookUpText;
402 else
404 UpdateMeaningBox_Impl( &aMeanings );
405 if( pBtn == &aLookUpBtn )
406 InfoBox( this, aErrStr ).Execute();
408 return 0;
411 UpdateMeaningBox_Impl( &aMeanings );
413 if ( aWordLB.GetEntryPos( aText ) == LISTBOX_ENTRY_NOTFOUND )
414 aWordLB.InsertEntry( aText );
416 aWordLB.SelectEntry( aText );
417 aMeanLB.SelectEntryPos( 0 );
419 String aStr( aMeanLB.GetSelectEntry() );
420 GetReplaceEditString( aStr );
421 aReplaceEdit.SetText( aStr );
422 aSynonymLB.SetNoSelection();
424 return 0;
427 // -----------------------------------------------------------------------
429 IMPL_LINK( SvxThesaurusDialog, LanguageHdl_Impl, Button *, EMPTYARG )
431 if (!pImpl->xThesaurus.is())
432 return 0;
434 SvxThesaurusLanguageDlg_Impl aDlg( this );
435 sal_uInt16 nLang = pImpl->nLookUpLanguage;
436 aDlg.SetLanguage( nLang );
438 if ( aDlg.Execute() == RET_OK )
440 nLang = aDlg.GetLanguage();
441 if (pImpl->xThesaurus->hasLocale( SvxCreateLocale( nLang ) ))
442 pImpl->nLookUpLanguage = nLang;
443 UpdateMeaningBox_Impl();
444 Init_Impl( nLang );
447 return 0;
450 // -----------------------------------------------------------------------
452 IMPL_LINK( SvxThesaurusDialog, SynonymHdl_Impl, ListBox *, EMPTYARG )
454 if ( aSynonymLB.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND )
456 String aStr( aSynonymLB.GetSelectEntry() );
457 GetReplaceEditString( aStr );
458 aReplaceEdit.SetText( aStr );
460 return 0;
463 // -----------------------------------------------------------------------
465 IMPL_LINK( SvxThesaurusDialog, SelectHdl_Impl, ListBox *, pBox )
467 String aStr( pBox->GetSelectEntry() );
468 GetReplaceEditString( aStr );
469 aReplaceEdit.SetText( aStr );
471 //! 'aCancelBtn' is used to indicate that the handler is called as result
472 //! of a double click action.
473 LookUpHdl_Impl( &aCancelBtn /* ??? &aLookUpBtn */ );
475 return 0;
478 // -----------------------------------------------------------------------
480 IMPL_LINK( SvxThesaurusDialog, EntryHdl_Impl, ListBox *, EMPTYARG )
483 UpdateSynonymBox_Impl();
485 return 0;
488 // -----------------------------------------------------------------------
490 IMPL_LINK( SvxThesaurusDialog, SpellErrorHdl_Impl, void *, pError )
492 // Der "ubergebene Pointer pError ist die falsche Sprachen-Nummer
493 LanguageType eLang = (LanguageType)(sal_uIntPtr)pError;
494 String aErr( ::GetLanguageString( eLang ) );
495 // Fehlermeldung ausgeben
496 ErrorHandler::HandleError(
497 *new StringErrorInfo( ERRCODE_SVX_LINGU_LANGUAGENOTEXISTS, aErr ) );
498 return 0;