xref: /aoo41x/main/cui/source/dialogs/thesdlg.cxx (revision 79aad27f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 
26 #include "precompiled_cui.hxx"
27 
28 #include "thesdlg.hrc"
29 #include "thesdlg.hxx"
30 #include "thesdlg_impl.hxx"
31 #include "cuires.hrc"
32 #include "dialmgr.hxx"
33 
34 #include <editeng/unolingu.hxx>
35 #include <tools/shl.hxx>
36 #include <svl/lngmisc.hxx>
37 #include <svtools/filter.hxx>
38 #include <svtools/svlbitm.hxx>
39 #include <svtools/svtreebx.hxx>
40 #include <vcl/wrkwin.hxx>
41 #include <vcl/msgbox.hxx>
42 #include <vcl/svapp.hxx>
43 #include <com/sun/star/linguistic2/XThesaurus.hpp>
44 #include <com/sun/star/linguistic2/XMeaning.hpp>
45 #include <thesdlg.hxx>
46 #include <svx/dlgutil.hxx>
47 #include <svx/dialmgr.hxx>
48 #include <svx/svxerr.hxx>
49 #include <editeng/unolingu.hxx>
50 #include <svx/langbox.hxx>
51 #include <svtools/langtab.hxx>
52 #include <unotools/lingucfg.hxx>
53 #include <i18npool/mslangid.hxx>
54 #include <comphelper/processfactory.hxx>
55 #include <osl/file.hxx>
56 #include <svl/lngmisc.hxx>
57 
58 
59 #include <stack>
60 #include <algorithm>
61 
62 #include <com/sun/star/linguistic2/XThesaurus.hpp>
63 #include <com/sun/star/linguistic2/XMeaning.hpp>
64 #include <com/sun/star/linguistic2/XLinguServiceManager.hpp>
65 
66 using namespace ::com::sun::star;
67 using ::rtl::OUString;
68 
69 
70 #define A2S(x)          String::CreateFromAscii( x )
71 
72 
73 // class LookUpComboBox_Impl --------------------------------------------------
74 
LookUpComboBox_Impl(Window * pParent,const ResId & rResId,SvxThesaurusDialog_Impl & rImpl)75 LookUpComboBox_Impl::LookUpComboBox_Impl(
76     Window *pParent, const ResId &rResId, SvxThesaurusDialog_Impl &rImpl ) :
77     ComboBox        (pParent, rResId),
78     m_rDialogImpl( rImpl )
79 {
80     m_aModifyTimer.SetTimeoutHdl( LINK( this, LookUpComboBox_Impl, ModifyTimer_Hdl ) );
81     m_aModifyTimer.SetTimeout( 500 );
82 
83     EnableAutocomplete( sal_False );
84 }
85 
86 
~LookUpComboBox_Impl()87 LookUpComboBox_Impl::~LookUpComboBox_Impl()
88 {
89 }
90 
91 
Modify()92 void LookUpComboBox_Impl::Modify()
93 {
94     m_aModifyTimer.Start();
95 }
96 
97 
IMPL_LINK(LookUpComboBox_Impl,ModifyTimer_Hdl,Timer *,EMPTYARG)98 IMPL_LINK( LookUpComboBox_Impl, ModifyTimer_Hdl, Timer *, EMPTYARG /*pTimer*/ )
99 {
100     m_rDialogImpl.LookUp( GetText() );
101     m_aModifyTimer.Stop();
102     return 0;
103 }
104 
105 
106 // class ReplaceEdit_Impl --------------------------------------------------
107 
ReplaceEdit_Impl(Window * pParent,const ResId & rResId)108 ReplaceEdit_Impl::ReplaceEdit_Impl(
109     Window *pParent, const ResId &rResId ) :
110     Edit        (pParent, rResId)
111 {
112 }
113 
114 
~ReplaceEdit_Impl()115 ReplaceEdit_Impl::~ReplaceEdit_Impl()
116 {
117 }
118 
119 
Modify()120 void ReplaceEdit_Impl::Modify()
121 {
122     if (m_pBtn)
123         m_pBtn->Enable( GetText().Len() > 0 );
124 }
125 
SetText(const XubString & rStr)126 void ReplaceEdit_Impl::SetText( const XubString& rStr )
127 {
128     Edit::SetText( rStr );
129     Modify();
130 }
131 
132 
SetText(const XubString & rStr,const Selection & rNewSelection)133 void ReplaceEdit_Impl::SetText( const XubString& rStr, const Selection& rNewSelection )
134 {
135     Edit::SetText( rStr, rNewSelection );
136     Modify();
137 }
138 
139 
140 // class ThesaurusAlternativesCtrl_Impl ----------------------------------
141 
AlternativesString_Impl(ThesaurusAlternativesCtrl_Impl & rControl,SvLBoxEntry * pEntry,sal_uInt16 nFlags,const String & rStr)142 AlternativesString_Impl::AlternativesString_Impl(
143     ThesaurusAlternativesCtrl_Impl &rControl,
144     SvLBoxEntry* pEntry, sal_uInt16 nFlags, const String& rStr ) :
145     //
146     SvLBoxString( pEntry, nFlags, rStr ),
147     m_rControlImpl( rControl )
148 {
149 }
150 
Paint(const Point & rPos,SvLBox & rDev,sal_uInt16,SvLBoxEntry * pEntry)151 void AlternativesString_Impl::Paint(
152     const Point& rPos,
153     SvLBox& rDev, sal_uInt16,
154 	SvLBoxEntry* pEntry )
155 {
156 	AlternativesExtraData* pData = m_rControlImpl.GetExtraData( pEntry );
157 	Point aPos( rPos );
158 	Font aOldFont( rDev.GetFont());
159 	if (pData && pData->IsHeader())
160 	{
161 		Font aFont( aOldFont );
162 		aFont.SetWeight( WEIGHT_BOLD );
163 		rDev.SetFont( aFont );
164 		aPos.X() = 0;
165 	}
166 	else
167 		aPos.X() += 5;
168 	rDev.DrawText( aPos, GetText() );
169 	rDev.SetFont( aOldFont );
170 }
171 
172 
ThesaurusAlternativesCtrl_Impl(Window * pParent,SvxThesaurusDialog_Impl & rImpl)173 ThesaurusAlternativesCtrl_Impl::ThesaurusAlternativesCtrl_Impl(
174         Window* pParent,
175         SvxThesaurusDialog_Impl &rImpl ) :
176     SvxCheckListBox( pParent, CUI_RES( CT_THES_ALTERNATIVES ) ),
177     m_rDialogImpl( rImpl )
178 {
179 	SetStyle( GetStyle() | WB_CLIPCHILDREN | WB_HSCROLL | WB_FORCE_MAKEVISIBLE );
180 	SetHighlightRange();
181 }
182 
183 
~ThesaurusAlternativesCtrl_Impl()184 ThesaurusAlternativesCtrl_Impl::~ThesaurusAlternativesCtrl_Impl()
185 {
186     ClearExtraData();
187 }
188 
189 
ClearExtraData()190 void ThesaurusAlternativesCtrl_Impl::ClearExtraData()
191 {
192     UserDataMap_t   aEmpty;
193     m_aUserData.swap( aEmpty );
194 }
195 
196 
SetExtraData(const SvLBoxEntry * pEntry,const AlternativesExtraData & rData)197 void ThesaurusAlternativesCtrl_Impl::SetExtraData(
198     const SvLBoxEntry *pEntry,
199     const AlternativesExtraData &rData )
200 {
201     if (!pEntry)
202         return;
203 
204     UserDataMap_t::iterator aIt( m_aUserData.find( pEntry ) );
205     if (aIt != m_aUserData.end())
206         aIt->second = rData;
207     else
208         m_aUserData[ pEntry ] = rData;
209 }
210 
211 
GetExtraData(const SvLBoxEntry * pEntry)212 AlternativesExtraData * ThesaurusAlternativesCtrl_Impl::GetExtraData(
213     const SvLBoxEntry *pEntry )
214 {
215     AlternativesExtraData *pRes = NULL;
216     UserDataMap_t::iterator aIt( m_aUserData.find( pEntry ) );
217     if (aIt != m_aUserData.end())
218         pRes = &aIt->second;
219     return pRes;
220 }
221 
222 
AddEntry(sal_Int32 nVal,const String & rText,bool bIsHeader)223 SvLBoxEntry * ThesaurusAlternativesCtrl_Impl::AddEntry( sal_Int32 nVal, const String &rText, bool bIsHeader )
224 {
225 	SvLBoxEntry* pEntry = new SvLBoxEntry;
226     String aText;
227     if (bIsHeader && nVal >= 0)
228     {
229         aText = String::CreateFromInt32( nVal );
230         aText += A2S( ". " );
231     }
232     pEntry->AddItem( new SvLBoxString( pEntry, 0, String() ) );	// add empty column
233     aText += rText;
234     pEntry->AddItem( new SvLBoxContextBmp( pEntry, 0, Image(), Image(), 0 ) );  // otherwise crash
235 	pEntry->AddItem( new AlternativesString_Impl( *this, pEntry, 0, aText ) );
236 
237 	SetExtraData( pEntry, AlternativesExtraData( rText, bIsHeader ) );
238     GetModel()->Insert( pEntry );
239 
240     if (bIsHeader)
241         GetViewDataEntry( pEntry )->SetSelectable( false );
242 
243 	return pEntry;
244 }
245 
246 
KeyInput(const KeyEvent & rKEvt)247 void ThesaurusAlternativesCtrl_Impl::KeyInput( const KeyEvent& rKEvt )
248 {
249     const KeyCode& rKey = rKEvt.GetKeyCode();
250 
251     if (rKey.GetCode() == KEY_RETURN || rKey.GetCode() == KEY_ESCAPE)
252         GetParent()->KeyInput( rKEvt ); // parent will close dialog...
253     else if (rKey.GetCode() == KEY_SPACE)
254         m_rDialogImpl.AlternativesDoubleClickHdl_Impl( this ); // look up current selected entry
255     else if (GetEntryCount())
256         SvxCheckListBox::KeyInput( rKEvt );
257 }
258 
259 
Paint(const Rectangle & rRect)260 void ThesaurusAlternativesCtrl_Impl::Paint( const Rectangle& rRect )
261 {
262     if (!m_rDialogImpl.m_bWordFound)
263     {
264 /*        Push( PUSH_FONT );
265 
266         Font aFont( GetFont() );
267         aFont.SetHeight( 2 * aFont.GetHeight() );
268         SetFont( aFont );
269 */
270         Size aTextSize( GetTextWidth( m_rDialogImpl.aErrStr ), GetTextHeight() );
271         aTextSize  = LogicToPixel( aTextSize );
272         Point aPos;
273         aPos.X() += GetSizePixel().Width() / 2  - aTextSize.Width() / 2;
274         aPos.Y() += GetSizePixel().Height() / 2 /*- aTextSize.Height() / 2*/;
275         aPos = PixelToLogic( aPos );
276         DrawText( aPos, m_rDialogImpl.aErrStr );
277 
278 //        Pop();
279     }
280     else
281         SvxCheckListBox::Paint( rRect );
282 }
283 
284 
285 // struct SvxThesaurusDialog_Impl ----------------------------------------
286 
SvxThesaurusDialog_Impl(SvxThesaurusDialog * pDialog)287 SvxThesaurusDialog_Impl::SvxThesaurusDialog_Impl( SvxThesaurusDialog * pDialog ) :
288     m_pDialog       ( pDialog ),
289     aVendorImageFI  ( pDialog, CUI_RES( IMG_VENDOR ) ),
290     aLeftBtn        ( pDialog, CUI_RES( BTN_LEFT ) ),
291     aWordText       ( pDialog, CUI_RES( FT_WORD ) ),
292     aWordCB         ( pDialog, CUI_RES( CB_WORD ), *this ),
293     m_aAlternativesText  ( pDialog, CUI_RES( FT_THES_ALTERNATIVES ) ),
294     m_pAlternativesCT    ( new ThesaurusAlternativesCtrl_Impl( pDialog, *this ) ),
295     aReplaceText    ( pDialog, CUI_RES( FT_REPL ) ),
296     aReplaceEdit    ( pDialog, CUI_RES( ED_REPL ) ),
297     aFL             ( pDialog, CUI_RES( FL_VAR ) ),
298     aHelpBtn        ( pDialog, CUI_RES( BTN_THES_HELP ) ),
299     aLangMBtn       ( pDialog, CUI_RES( MB_LANGUAGE ) ),
300     aReplaceBtn     ( pDialog, CUI_RES( BTN_THES_OK ) ),
301     aCancelBtn      ( pDialog, CUI_RES( BTN_THES_CANCEL ) ),
302     aErrStr                 ( CUI_RES( STR_ERR_TEXTNOTFOUND ) ),
303     aVendorDefaultImage     ( CUI_RES( IMG_DEFAULT_VENDOR ) ),
304     aVendorDefaultImageHC   ( CUI_RES( IMG_DEFAULT_VENDOR_HC ) ),
305     xThesaurus      ( NULL ),
306     aLookUpText     (),
307     nLookUpLanguage ( LANGUAGE_NONE ),
308     m_bWordFound( false )
309 {
310     // note: FreeResource must only be called in the c-tor of SvxThesaurusDialog
311 
312     aReplaceEdit.SetButton( &aReplaceBtn );
313 
314     aLeftBtn.SetClickHdl( LINK( this, SvxThesaurusDialog_Impl, LeftBtnHdl_Impl ) );
315     aWordCB.SetSelectHdl( LINK( this, SvxThesaurusDialog_Impl, WordSelectHdl_Impl ) );
316     aLangMBtn.SetSelectHdl( LINK( this, SvxThesaurusDialog_Impl, LanguageHdl_Impl ) );
317     m_pAlternativesCT->SetSelectHdl( LINK( this, SvxThesaurusDialog_Impl, AlternativesSelectHdl_Impl ));
318     m_pAlternativesCT->SetDoubleClickHdl( LINK( this, SvxThesaurusDialog_Impl, AlternativesDoubleClickHdl_Impl ));
319 
320     Application::PostUserEvent( STATIC_LINK( this, SvxThesaurusDialog_Impl, VendorImageInitHdl ) );
321 }
322 
323 
~SvxThesaurusDialog_Impl()324 SvxThesaurusDialog_Impl::~SvxThesaurusDialog_Impl()
325 {
326     delete aLangMBtn.GetPopupMenu();
327 }
328 
329 
queryMeanings_Impl(OUString & rTerm,const lang::Locale & rLocale,const beans::PropertyValues & rProperties)330 uno::Sequence< uno::Reference< linguistic2::XMeaning > > SAL_CALL SvxThesaurusDialog_Impl::queryMeanings_Impl(
331         OUString& rTerm,
332         const lang::Locale& rLocale,
333         const beans::PropertyValues& rProperties )
334     throw(lang::IllegalArgumentException, uno::RuntimeException)
335 {
336     uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeanings(
337             xThesaurus->queryMeanings( rTerm, rLocale, rProperties ) );
338 
339     // text with '.' at the end?
340     if (0 == aMeanings.getLength() && rTerm.getLength() &&
341         rTerm.getStr()[ rTerm.getLength() - 1 ] == '.')
342     {
343         // try again without trailing '.' chars. It may be a word at the
344         // end of a sentence and not an abbreviation...
345         String aTxt( rTerm );
346         aTxt.EraseTrailingChars( '.' );
347         aMeanings = xThesaurus->queryMeanings( aTxt, rLocale, rProperties );
348         if (aMeanings.getLength())
349         {
350             rTerm = aTxt;
351         }
352     }
353 
354     return aMeanings;
355 }
356 
357 
UpdateAlternativesBox_Impl()358 bool SvxThesaurusDialog_Impl::UpdateAlternativesBox_Impl()
359 {
360     lang::Locale aLocale( SvxCreateLocale( nLookUpLanguage ) );
361     uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeanings = queryMeanings_Impl(
362             aLookUpText, aLocale, uno::Sequence< beans::PropertyValue >() );
363     const sal_Int32 nMeanings = aMeanings.getLength();
364     const uno::Reference< linguistic2::XMeaning > *pMeanings = aMeanings.getConstArray();
365 
366     m_pAlternativesCT->SetUpdateMode( sal_False );
367 
368     // clear old user data of control before creating new ones via AddEntry below
369     m_pAlternativesCT->ClearExtraData();
370 
371     m_pAlternativesCT->Clear();
372     for (sal_Int32 i = 0;  i < nMeanings;  ++i)
373     {
374         OUString rMeaningTxt = pMeanings[i]->getMeaning();
375         uno::Sequence< OUString > aSynonyms( pMeanings[i]->querySynonyms() );
376         const sal_Int32 nSynonyms = aSynonyms.getLength();
377         const OUString *pSynonyms = aSynonyms.getConstArray();
378         DBG_ASSERT( rMeaningTxt.getLength() > 0, "meaning with empty text" );
379         DBG_ASSERT( nSynonyms > 0, "meaning without synonym" );
380 
381         m_pAlternativesCT->AddEntry( i + 1, rMeaningTxt, true );
382         for (sal_Int32 k = 0;  k < nSynonyms;  ++k)
383             m_pAlternativesCT->AddEntry( -1, pSynonyms[k], false );
384     }
385 
386     m_pAlternativesCT->SetUpdateMode( sal_True );
387 
388     return nMeanings > 0;
389 }
390 
391 
LookUp(const String & rText)392 void SvxThesaurusDialog_Impl::LookUp( const String &rText )
393 {
394     if (rText != aWordCB.GetText()) // avoid moving of the cursor if the text is the same
395         aWordCB.SetText( rText );
396     LookUp_Impl();
397 }
398 
399 
IMPL_LINK(SvxThesaurusDialog_Impl,LeftBtnHdl_Impl,Button *,pBtn)400 IMPL_LINK( SvxThesaurusDialog_Impl, LeftBtnHdl_Impl, Button *, pBtn )
401 {
402     if (pBtn && aLookUpHistory.size() >= 2)
403     {
404         aLookUpHistory.pop();                       // remove current look up word from stack
405         aWordCB.SetText( aLookUpHistory.top() );    // retrieve previous look up word
406         aLookUpHistory.pop();
407         LookUp_Impl();
408     }
409     return 0;
410 }
411 
412 
IMPL_LINK(SvxThesaurusDialog_Impl,LanguageHdl_Impl,MenuButton *,pBtn)413 IMPL_LINK( SvxThesaurusDialog_Impl, LanguageHdl_Impl, MenuButton *, pBtn )
414 {
415     PopupMenu *pMenu = aLangMBtn.GetPopupMenu();
416     if (pMenu && pBtn)
417     {
418         sal_uInt16 nItem = pBtn->GetCurItemId();
419         String aLangText( pMenu->GetItemText( nItem ) );
420         LanguageType nLang = SvtLanguageTable().GetType( aLangText );
421         DBG_ASSERT( nLang != LANGUAGE_NONE && nLang != LANGUAGE_DONTKNOW, "failed to get language" );
422         if (xThesaurus->hasLocale( SvxCreateLocale( nLang ) ))
423             nLookUpLanguage = nLang;
424         m_pDialog->SetWindowTitle( nLang );
425         UpdateVendorImage();
426         LookUp_Impl();
427     }
428     return 0;
429 }
430 
431 
LookUp_Impl()432 void SvxThesaurusDialog_Impl::LookUp_Impl()
433 {
434     String aText( aWordCB.GetText() );
435 
436     aLookUpText = OUString( aText );
437     if (aLookUpText.getLength() > 0 &&
438             (aLookUpHistory.size() == 0 || aLookUpText != aLookUpHistory.top()))
439         aLookUpHistory.push( aLookUpText );
440 
441     m_bWordFound = UpdateAlternativesBox_Impl();
442     m_pAlternativesCT->Enable( m_bWordFound );
443 
444     if ( aWordCB.GetEntryPos( aText ) == LISTBOX_ENTRY_NOTFOUND )
445         aWordCB.InsertEntry( aText );
446 
447     aReplaceEdit.SetText( String() );
448     aLeftBtn.Enable( aLookUpHistory.size() > 1 );
449 }
450 
451 
IMPL_LINK(SvxThesaurusDialog_Impl,WordSelectHdl_Impl,ComboBox *,pBox)452 IMPL_LINK( SvxThesaurusDialog_Impl, WordSelectHdl_Impl, ComboBox *, pBox )
453 {
454     if (pBox && !aWordCB.IsTravelSelect())  // act only upon return key and not when traveling with cursor keys
455     {
456         sal_uInt16 nPos = pBox->GetSelectEntryPos();
457         String aStr( pBox->GetEntry( nPos ) );
458         aStr = linguistic::GetThesaurusReplaceText( aStr );
459         aWordCB.SetText( aStr );
460         LookUp_Impl();
461     }
462 
463     return 0;
464 }
465 
466 
IMPL_LINK(SvxThesaurusDialog_Impl,AlternativesSelectHdl_Impl,SvxCheckListBox *,pBox)467 IMPL_LINK( SvxThesaurusDialog_Impl, AlternativesSelectHdl_Impl, SvxCheckListBox *, pBox )
468 {
469     SvLBoxEntry *pEntry = pBox ? pBox->GetCurEntry() : NULL;
470     if (pEntry)
471     {
472         AlternativesExtraData * pData = m_pAlternativesCT->GetExtraData( pEntry );
473         String aStr;
474         if (pData && !pData->IsHeader())
475         {
476             aStr = pData->GetText();
477             aStr = linguistic::GetThesaurusReplaceText( aStr );
478         }
479         aReplaceEdit.SetText( aStr );
480     }
481     return 0;
482 }
483 
484 
IMPL_LINK(SvxThesaurusDialog_Impl,AlternativesDoubleClickHdl_Impl,SvxCheckListBox *,pBox)485 IMPL_LINK( SvxThesaurusDialog_Impl, AlternativesDoubleClickHdl_Impl, SvxCheckListBox *, pBox )
486 {
487     SvLBoxEntry *pEntry = pBox ? pBox->GetCurEntry() : NULL;
488     if (pEntry)
489     {
490         AlternativesExtraData * pData = m_pAlternativesCT->GetExtraData( pEntry );
491         String aStr;
492         if (pData && !pData->IsHeader())
493         {
494             aStr = pData->GetText();
495             aStr = linguistic::GetThesaurusReplaceText( aStr );
496         }
497 
498         aWordCB.SetText( aStr );
499         if (aStr.Len() > 0)
500             LookUp_Impl();
501     }
502 
503     //! workaround to set the selection since calling SelectEntryPos within
504     //! the double click handler does not work
505     Application::PostUserEvent( STATIC_LINK( this, SvxThesaurusDialog_Impl, SelectFirstHdl_Impl ), pBox );
506     return 0;
507 }
508 
509 
IMPL_STATIC_LINK(SvxThesaurusDialog_Impl,SelectFirstHdl_Impl,SvxCheckListBox *,pBox)510 IMPL_STATIC_LINK( SvxThesaurusDialog_Impl, SelectFirstHdl_Impl, SvxCheckListBox *, pBox )
511 {
512 	(void) pThis;
513     if (pBox && pBox->GetEntryCount() >= 2)
514         pBox->SelectEntryPos( 1 );  // pos 0 is a 'header' that is not selectable
515     return 0;
516 }
517 
518 ////////////////////////////////////////////////////////////
519 
lcl_GetImageFromPngUrl(const OUString & rFileUrl)520 static Image lcl_GetImageFromPngUrl( const OUString &rFileUrl )
521 {
522     Image aRes;
523 
524     OUString aTmp;
525     osl::FileBase::getSystemPathFromFileURL( rFileUrl, aTmp );
526 
527     Graphic aGraphic;
528     const String aFilterName( RTL_CONSTASCII_USTRINGPARAM( IMP_PNG ) );
529     if( GRFILTER_OK == GraphicFilter::LoadGraphic( aTmp, aFilterName, aGraphic, NULL, NULL ) )
530     {
531         aRes = Image( aGraphic.GetBitmapEx() );
532     }
533     return aRes;
534 }
535 
536 
lcl_GetThesImplName(const lang::Locale & rLocale)537 static String lcl_GetThesImplName( const lang::Locale &rLocale )
538 {
539     String aRes;
540 
541     uno::Reference< linguistic2::XLinguServiceManager >     xLngMgr;
542     try
543     {
544         uno::Reference< lang::XMultiServiceFactory >  xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
545         xLngMgr = uno::Reference< linguistic2::XLinguServiceManager >( xMSF->createInstance(
546                 OUString( RTL_CONSTASCII_USTRINGPARAM(
547                     "com.sun.star.linguistic2.LinguServiceManager" ))), uno::UNO_QUERY_THROW );
548 
549         DBG_ASSERT( xLngMgr.is(), "LinguServiceManager missing" );
550         if (xLngMgr.is())
551         {
552             uno::Sequence< OUString > aServiceNames = xLngMgr->getConfiguredServices(
553                     OUString::createFromAscii("com.sun.star.linguistic2.Thesaurus"), rLocale );
554             // there should be at most one thesaurus configured for each language
555             DBG_ASSERT( aServiceNames.getLength() <= 1, "more than one thesaurus found. Should not be possible" );
556             if (aServiceNames.getLength() == 1)
557                 aRes = aServiceNames[0];
558         }
559     }
560     catch (uno::Exception &e)
561     {
562         (void) e;
563         DBG_ASSERT( 0, "failed to get thesaurus" );
564     }
565 
566     return aRes;
567 }
568 
569 
UpdateVendorImage()570 void SvxThesaurusDialog_Impl::UpdateVendorImage()
571 {
572     m_pDialog->SetUpdateMode( sal_False );
573 
574     SvtLinguConfig aCfg;
575     if (aCfg.HasVendorImages( "ThesaurusDialogImage" ))
576     {
577         const bool bHC = Application::GetSettings().GetStyleSettings().GetHighContrastMode();
578 
579         Image aImage;
580         String sThesImplName( lcl_GetThesImplName( SvxCreateLocale( nLookUpLanguage ) ) );
581         OUString aThesDialogImageUrl( aCfg.GetThesaurusDialogImage( sThesImplName, bHC ) );
582         if (sThesImplName.Len() > 0 && aThesDialogImageUrl.getLength() > 0)
583             aImage = Image( lcl_GetImageFromPngUrl( aThesDialogImageUrl ) );
584         else
585             aImage = bHC ? aVendorDefaultImageHC : aVendorDefaultImage;
586         aVendorImageFI.SetImage( aImage );
587     }
588 
589     m_pDialog->SetUpdateMode( sal_True );
590 }
591 
592 
IMPL_STATIC_LINK(SvxThesaurusDialog_Impl,VendorImageInitHdl,SvxThesaurusDialog_Impl *,EMPTYARG)593 IMPL_STATIC_LINK( SvxThesaurusDialog_Impl, VendorImageInitHdl, SvxThesaurusDialog_Impl *, EMPTYARG )
594 {
595     pThis->m_pDialog->SetUpdateMode( sal_False );
596 
597     SvtLinguConfig aCfg;
598     if (aCfg.HasVendorImages( "ThesaurusDialogImage" ))
599     {
600         const bool bHC = Application::GetSettings().GetStyleSettings().GetHighContrastMode();
601         Image aImage( bHC ? pThis->aVendorDefaultImageHC : pThis->aVendorDefaultImage );
602         pThis->aVendorImageFI.SetImage( aImage );
603         pThis->aVendorImageFI.Show();
604 
605         // move down visible controls according to the vendor images height
606         Size aVendorSize = pThis->aVendorImageFI.GetSizePixel();
607         Size aImageSize  = pThis->aVendorImageFI.GetImage().GetSizePixel();
608         if (aImageSize.Height())
609         {
610             aVendorSize.Height() = aImageSize.Height();
611             if(aVendorSize.Width() < aImageSize.Width())
612                 aVendorSize.Width() = aImageSize.Width();
613             pThis->aVendorImageFI.SetSizePixel( aVendorSize );
614         }
615         const sal_Int32 nDiff = aVendorSize.Height();
616         pThis->aVendorImageFI.SetSizePixel( aVendorSize );
617         Control* aControls[] = {
618             &pThis->aLeftBtn,
619             &pThis->aWordText,
620             &pThis->aWordCB,
621             &pThis->m_aAlternativesText,
622             pThis->m_pAlternativesCT.get(),
623             &pThis->aReplaceText,
624             &pThis->aReplaceEdit,
625             &pThis->aFL,
626             &pThis->aHelpBtn,
627             &pThis->aLangMBtn,
628             &pThis->aReplaceBtn,
629             &pThis->aCancelBtn,
630             0
631         };
632         sal_Int32 nControl = 0;
633         while (aControls[nControl])
634         {
635             Point aPos = aControls[nControl]->GetPosPixel();
636             aPos.Y() += nDiff;
637             aControls[nControl]->SetPosPixel(aPos);
638             ++nControl;
639         }
640         Size aDlgSize = pThis->m_pDialog->GetSizePixel();
641         aDlgSize.Height() += nDiff;
642         pThis->m_pDialog->SetSizePixel( aDlgSize );
643         pThis->m_pDialog->Invalidate();
644     }
645 
646     pThis->UpdateVendorImage();
647     pThis->m_pDialog->SetUpdateMode( sal_True );
648 
649     return 0;
650 };
651 
652 
653 // class SvxThesaurusDialog ----------------------------------------------
654 
SvxThesaurusDialog(Window * pParent,uno::Reference<linguistic2::XThesaurus> xThes,const String & rWord,LanguageType nLanguage)655 SvxThesaurusDialog::SvxThesaurusDialog(
656     Window* pParent,
657     uno::Reference< linguistic2::XThesaurus >  xThes,
658     const String &rWord,
659     LanguageType nLanguage ) :
660 
661 	SvxStandardDialog( pParent, CUI_RES( RID_SVXDLG_THESAURUS ) )
662 {
663     m_pImpl = std::auto_ptr< SvxThesaurusDialog_Impl >(new SvxThesaurusDialog_Impl( this ));
664 
665     m_pImpl->xThesaurus = xThes;
666     m_pImpl->aLookUpText = OUString( rWord );
667     m_pImpl->nLookUpLanguage = nLanguage;
668     if (rWord.Len() > 0)
669         m_pImpl->aLookUpHistory.push( rWord );
670 
671 	FreeResource();
672 
673     OUString aTmp( rWord );
674 	linguistic::RemoveHyphens( aTmp );
675     linguistic::ReplaceControlChars( aTmp );
676     String aTmp2( aTmp );
677     m_pImpl->aReplaceEdit.SetText( aTmp2 );
678     m_pImpl->aWordCB.InsertEntry( aTmp2 );
679 
680     m_pImpl->LookUp( aTmp2 );
681     m_pImpl->m_pAlternativesCT->GrabFocus();
682     m_pImpl->aLeftBtn.Enable( sal_False );
683 
684     // fill language menu button list
685     SvtLanguageTable aLangTab;
686     uno::Sequence< lang::Locale > aLocales;
687     if (m_pImpl->xThesaurus.is())
688         aLocales = m_pImpl->xThesaurus->getLocales();
689     const sal_Int32 nLocales = aLocales.getLength();
690     const lang::Locale *pLocales = aLocales.getConstArray();
691     delete m_pImpl->aLangMBtn.GetPopupMenu();
692     PopupMenu* pMenu = new PopupMenu;
693     pMenu->SetMenuFlags( MENU_FLAG_NOAUTOMNEMONICS );
694     std::vector< OUString > aLangVec;
695     for (sal_Int32 i = 0;  i < nLocales;  ++i )
696     {
697         const LanguageType nLang = SvxLocaleToLanguage( pLocales[i] );
698         DBG_ASSERT( nLang != LANGUAGE_NONE && nLang != LANGUAGE_DONTKNOW, "failed to get language" );
699         aLangVec.push_back( aLangTab.GetString( nLang ) );
700     }
701     std::sort( aLangVec.begin(), aLangVec.end() );
702     for (size_t i = 0;  i < aLangVec.size();  ++i)
703         pMenu->InsertItem( (sal_uInt16)i+1, aLangVec[i] );  // menu items should be enumerated from 1 and not 0
704     m_pImpl->aLangMBtn.SetPopupMenu( pMenu );
705 
706     SetWindowTitle( nLanguage );
707 
708 	// disable controls if service is missing
709     if (!m_pImpl->xThesaurus.is())
710 		Enable( sal_False );
711 }
712 
713 
~SvxThesaurusDialog()714 SvxThesaurusDialog::~SvxThesaurusDialog()
715 {
716 }
717 
718 
SetWindowTitle(LanguageType nLanguage)719 void SvxThesaurusDialog::SetWindowTitle( LanguageType nLanguage )
720 {
721     // Sprache anpassen
722     String aStr( GetText() );
723     aStr.Erase( aStr.Search( sal_Unicode( '(' ) ) - 1 );
724     aStr.Append( UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( " (" ) ) );
725     aStr += SvtLanguageTable().GetLanguageString( nLanguage );
726     aStr.Append( sal_Unicode( ')' ) );
727     SetText( aStr );    // set window title
728 }
729 
730 
GetWord()731 String SvxThesaurusDialog::GetWord()
732 {
733     return m_pImpl->aReplaceEdit.GetText();
734 }
735 
736 
GetLanguage() const737 sal_uInt16 SvxThesaurusDialog::GetLanguage() const
738 {
739     return m_pImpl->nLookUpLanguage;
740 }
741 
742 
Apply()743 void SvxThesaurusDialog::Apply()
744 {
745 }
746 
747 
748