xref: /trunk/main/cui/source/dialogs/thesdlg.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
335     uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeanings(
336             xThesaurus->queryMeanings( rTerm, rLocale, rProperties ) );
337 
338     // text with '.' at the end?
339     if (0 == aMeanings.getLength() && rTerm.getLength() &&
340         rTerm.getStr()[ rTerm.getLength() - 1 ] == '.')
341     {
342         // try again without trailing '.' chars. It may be a word at the
343         // end of a sentence and not an abbreviation...
344         String aTxt( rTerm );
345         aTxt.EraseTrailingChars( '.' );
346         aMeanings = xThesaurus->queryMeanings( aTxt, rLocale, rProperties );
347         if (aMeanings.getLength())
348         {
349             rTerm = aTxt;
350         }
351     }
352 
353     return aMeanings;
354 }
355 
356 
UpdateAlternativesBox_Impl()357 bool SvxThesaurusDialog_Impl::UpdateAlternativesBox_Impl()
358 {
359     lang::Locale aLocale( SvxCreateLocale( nLookUpLanguage ) );
360     uno::Sequence< uno::Reference< linguistic2::XMeaning > > aMeanings = queryMeanings_Impl(
361             aLookUpText, aLocale, uno::Sequence< beans::PropertyValue >() );
362     const sal_Int32 nMeanings = aMeanings.getLength();
363     const uno::Reference< linguistic2::XMeaning > *pMeanings = aMeanings.getConstArray();
364 
365     m_pAlternativesCT->SetUpdateMode( sal_False );
366 
367     // clear old user data of control before creating new ones via AddEntry below
368     m_pAlternativesCT->ClearExtraData();
369 
370     m_pAlternativesCT->Clear();
371     for (sal_Int32 i = 0;  i < nMeanings;  ++i)
372     {
373         OUString rMeaningTxt = pMeanings[i]->getMeaning();
374         uno::Sequence< OUString > aSynonyms( pMeanings[i]->querySynonyms() );
375         const sal_Int32 nSynonyms = aSynonyms.getLength();
376         const OUString *pSynonyms = aSynonyms.getConstArray();
377         DBG_ASSERT( rMeaningTxt.getLength() > 0, "meaning with empty text" );
378         DBG_ASSERT( nSynonyms > 0, "meaning without synonym" );
379 
380         m_pAlternativesCT->AddEntry( i + 1, rMeaningTxt, true );
381         for (sal_Int32 k = 0;  k < nSynonyms;  ++k)
382             m_pAlternativesCT->AddEntry( -1, pSynonyms[k], false );
383     }
384 
385     m_pAlternativesCT->SetUpdateMode( sal_True );
386 
387     return nMeanings > 0;
388 }
389 
390 
LookUp(const String & rText)391 void SvxThesaurusDialog_Impl::LookUp( const String &rText )
392 {
393     if (rText != aWordCB.GetText()) // avoid moving of the cursor if the text is the same
394         aWordCB.SetText( rText );
395     LookUp_Impl();
396 }
397 
398 
IMPL_LINK(SvxThesaurusDialog_Impl,LeftBtnHdl_Impl,Button *,pBtn)399 IMPL_LINK( SvxThesaurusDialog_Impl, LeftBtnHdl_Impl, Button *, pBtn )
400 {
401     if (pBtn && aLookUpHistory.size() >= 2)
402     {
403         aLookUpHistory.pop();                       // remove current look up word from stack
404         aWordCB.SetText( aLookUpHistory.top() );    // retrieve previous look up word
405         aLookUpHistory.pop();
406         LookUp_Impl();
407     }
408     return 0;
409 }
410 
411 
IMPL_LINK(SvxThesaurusDialog_Impl,LanguageHdl_Impl,MenuButton *,pBtn)412 IMPL_LINK( SvxThesaurusDialog_Impl, LanguageHdl_Impl, MenuButton *, pBtn )
413 {
414     PopupMenu *pMenu = aLangMBtn.GetPopupMenu();
415     if (pMenu && pBtn)
416     {
417         sal_uInt16 nItem = pBtn->GetCurItemId();
418         String aLangText( pMenu->GetItemText( nItem ) );
419         LanguageType nLang = SvtLanguageTable().GetType( aLangText );
420         DBG_ASSERT( nLang != LANGUAGE_NONE && nLang != LANGUAGE_DONTKNOW, "failed to get language" );
421         if (xThesaurus->hasLocale( SvxCreateLocale( nLang ) ))
422             nLookUpLanguage = nLang;
423         m_pDialog->SetWindowTitle( nLang );
424         UpdateVendorImage();
425         LookUp_Impl();
426     }
427     return 0;
428 }
429 
430 
LookUp_Impl()431 void SvxThesaurusDialog_Impl::LookUp_Impl()
432 {
433     String aText( aWordCB.GetText() );
434 
435     aLookUpText = OUString( aText );
436     if (aLookUpText.getLength() > 0 &&
437             (aLookUpHistory.size() == 0 || aLookUpText != aLookUpHistory.top()))
438         aLookUpHistory.push( aLookUpText );
439 
440     m_bWordFound = UpdateAlternativesBox_Impl();
441     m_pAlternativesCT->Enable( m_bWordFound );
442 
443     if ( aWordCB.GetEntryPos( aText ) == LISTBOX_ENTRY_NOTFOUND )
444         aWordCB.InsertEntry( aText );
445 
446     aReplaceEdit.SetText( String() );
447     aLeftBtn.Enable( aLookUpHistory.size() > 1 );
448 }
449 
450 
IMPL_LINK(SvxThesaurusDialog_Impl,WordSelectHdl_Impl,ComboBox *,pBox)451 IMPL_LINK( SvxThesaurusDialog_Impl, WordSelectHdl_Impl, ComboBox *, pBox )
452 {
453     if (pBox && !aWordCB.IsTravelSelect())  // act only upon return key and not when traveling with cursor keys
454     {
455         sal_uInt16 nPos = pBox->GetSelectEntryPos();
456         String aStr( pBox->GetEntry( nPos ) );
457         aStr = linguistic::GetThesaurusReplaceText( aStr );
458         aWordCB.SetText( aStr );
459         LookUp_Impl();
460     }
461 
462     return 0;
463 }
464 
465 
IMPL_LINK(SvxThesaurusDialog_Impl,AlternativesSelectHdl_Impl,SvxCheckListBox *,pBox)466 IMPL_LINK( SvxThesaurusDialog_Impl, AlternativesSelectHdl_Impl, SvxCheckListBox *, pBox )
467 {
468     SvLBoxEntry *pEntry = pBox ? pBox->GetCurEntry() : NULL;
469     if (pEntry)
470     {
471         AlternativesExtraData * pData = m_pAlternativesCT->GetExtraData( pEntry );
472         String aStr;
473         if (pData && !pData->IsHeader())
474         {
475             aStr = pData->GetText();
476             aStr = linguistic::GetThesaurusReplaceText( aStr );
477         }
478         aReplaceEdit.SetText( aStr );
479     }
480     return 0;
481 }
482 
483 
IMPL_LINK(SvxThesaurusDialog_Impl,AlternativesDoubleClickHdl_Impl,SvxCheckListBox *,pBox)484 IMPL_LINK( SvxThesaurusDialog_Impl, AlternativesDoubleClickHdl_Impl, SvxCheckListBox *, pBox )
485 {
486     SvLBoxEntry *pEntry = pBox ? pBox->GetCurEntry() : NULL;
487     if (pEntry)
488     {
489         AlternativesExtraData * pData = m_pAlternativesCT->GetExtraData( pEntry );
490         String aStr;
491         if (pData && !pData->IsHeader())
492         {
493             aStr = pData->GetText();
494             aStr = linguistic::GetThesaurusReplaceText( aStr );
495         }
496 
497         aWordCB.SetText( aStr );
498         if (aStr.Len() > 0)
499             LookUp_Impl();
500     }
501 
502     //! workaround to set the selection since calling SelectEntryPos within
503     //! the double click handler does not work
504     Application::PostUserEvent( STATIC_LINK( this, SvxThesaurusDialog_Impl, SelectFirstHdl_Impl ), pBox );
505     return 0;
506 }
507 
508 
IMPL_STATIC_LINK(SvxThesaurusDialog_Impl,SelectFirstHdl_Impl,SvxCheckListBox *,pBox)509 IMPL_STATIC_LINK( SvxThesaurusDialog_Impl, SelectFirstHdl_Impl, SvxCheckListBox *, pBox )
510 {
511     (void) pThis;
512     if (pBox && pBox->GetEntryCount() >= 2)
513         pBox->SelectEntryPos( 1 );  // pos 0 is a 'header' that is not selectable
514     return 0;
515 }
516 
517 ////////////////////////////////////////////////////////////
518 
lcl_GetImageFromPngUrl(const OUString & rFileUrl)519 static Image lcl_GetImageFromPngUrl( const OUString &rFileUrl )
520 {
521     Image aRes;
522 
523     OUString aTmp;
524     osl::FileBase::getSystemPathFromFileURL( rFileUrl, aTmp );
525 
526     Graphic aGraphic;
527     const String aFilterName( RTL_CONSTASCII_USTRINGPARAM( IMP_PNG ) );
528     if( GRFILTER_OK == GraphicFilter::LoadGraphic( aTmp, aFilterName, aGraphic, NULL, NULL ) )
529     {
530         aRes = Image( aGraphic.GetBitmapEx() );
531     }
532     return aRes;
533 }
534 
535 
lcl_GetThesImplName(const lang::Locale & rLocale)536 static String lcl_GetThesImplName( const lang::Locale &rLocale )
537 {
538     String aRes;
539 
540     uno::Reference< linguistic2::XLinguServiceManager >     xLngMgr;
541     try
542     {
543         uno::Reference< lang::XMultiServiceFactory >  xMSF( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
544         xLngMgr = uno::Reference< linguistic2::XLinguServiceManager >( xMSF->createInstance(
545                 OUString( RTL_CONSTASCII_USTRINGPARAM(
546                     "com.sun.star.linguistic2.LinguServiceManager" ))), uno::UNO_QUERY_THROW );
547 
548         DBG_ASSERT( xLngMgr.is(), "LinguServiceManager missing" );
549         if (xLngMgr.is())
550         {
551             uno::Sequence< OUString > aServiceNames = xLngMgr->getConfiguredServices(
552                     OUString::createFromAscii("com.sun.star.linguistic2.Thesaurus"), rLocale );
553             // there should be at most one thesaurus configured for each language
554             DBG_ASSERT( aServiceNames.getLength() <= 1, "more than one thesaurus found. Should not be possible" );
555             if (aServiceNames.getLength() == 1)
556                 aRes = aServiceNames[0];
557         }
558     }
559     catch (uno::Exception &e)
560     {
561         (void) e;
562         DBG_ASSERT( 0, "failed to get thesaurus" );
563     }
564 
565     return aRes;
566 }
567 
568 
UpdateVendorImage()569 void SvxThesaurusDialog_Impl::UpdateVendorImage()
570 {
571     m_pDialog->SetUpdateMode( sal_False );
572 
573     SvtLinguConfig aCfg;
574     if (aCfg.HasVendorImages( "ThesaurusDialogImage" ))
575     {
576         const bool bHC = Application::GetSettings().GetStyleSettings().GetHighContrastMode();
577 
578         Image aImage;
579         String sThesImplName( lcl_GetThesImplName( SvxCreateLocale( nLookUpLanguage ) ) );
580         OUString aThesDialogImageUrl( aCfg.GetThesaurusDialogImage( sThesImplName, bHC ) );
581         if (sThesImplName.Len() > 0 && aThesDialogImageUrl.getLength() > 0)
582             aImage = Image( lcl_GetImageFromPngUrl( aThesDialogImageUrl ) );
583         else
584             aImage = bHC ? aVendorDefaultImageHC : aVendorDefaultImage;
585         aVendorImageFI.SetImage( aImage );
586     }
587 
588     m_pDialog->SetUpdateMode( sal_True );
589 }
590 
591 
IMPL_STATIC_LINK(SvxThesaurusDialog_Impl,VendorImageInitHdl,SvxThesaurusDialog_Impl *,EMPTYARG)592 IMPL_STATIC_LINK( SvxThesaurusDialog_Impl, VendorImageInitHdl, SvxThesaurusDialog_Impl *, EMPTYARG )
593 {
594     pThis->m_pDialog->SetUpdateMode( sal_False );
595 
596     SvtLinguConfig aCfg;
597     if (aCfg.HasVendorImages( "ThesaurusDialogImage" ))
598     {
599         const bool bHC = Application::GetSettings().GetStyleSettings().GetHighContrastMode();
600         Image aImage( bHC ? pThis->aVendorDefaultImageHC : pThis->aVendorDefaultImage );
601         pThis->aVendorImageFI.SetImage( aImage );
602         pThis->aVendorImageFI.Show();
603 
604         // move down visible controls according to the vendor images height
605         Size aVendorSize = pThis->aVendorImageFI.GetSizePixel();
606         Size aImageSize  = pThis->aVendorImageFI.GetImage().GetSizePixel();
607         if (aImageSize.Height())
608         {
609             aVendorSize.Height() = aImageSize.Height();
610             if(aVendorSize.Width() < aImageSize.Width())
611                 aVendorSize.Width() = aImageSize.Width();
612             pThis->aVendorImageFI.SetSizePixel( aVendorSize );
613         }
614         const sal_Int32 nDiff = aVendorSize.Height();
615         pThis->aVendorImageFI.SetSizePixel( aVendorSize );
616         Control* aControls[] = {
617             &pThis->aLeftBtn,
618             &pThis->aWordText,
619             &pThis->aWordCB,
620             &pThis->m_aAlternativesText,
621             pThis->m_pAlternativesCT.get(),
622             &pThis->aReplaceText,
623             &pThis->aReplaceEdit,
624             &pThis->aFL,
625             &pThis->aHelpBtn,
626             &pThis->aLangMBtn,
627             &pThis->aReplaceBtn,
628             &pThis->aCancelBtn,
629             0
630         };
631         sal_Int32 nControl = 0;
632         while (aControls[nControl])
633         {
634             Point aPos = aControls[nControl]->GetPosPixel();
635             aPos.Y() += nDiff;
636             aControls[nControl]->SetPosPixel(aPos);
637             ++nControl;
638         }
639         Size aDlgSize = pThis->m_pDialog->GetSizePixel();
640         aDlgSize.Height() += nDiff;
641         pThis->m_pDialog->SetSizePixel( aDlgSize );
642         pThis->m_pDialog->Invalidate();
643     }
644 
645     pThis->UpdateVendorImage();
646     pThis->m_pDialog->SetUpdateMode( sal_True );
647 
648     return 0;
649 };
650 
651 
652 // class SvxThesaurusDialog ----------------------------------------------
653 
SvxThesaurusDialog(Window * pParent,uno::Reference<linguistic2::XThesaurus> xThes,const String & rWord,LanguageType nLanguage)654 SvxThesaurusDialog::SvxThesaurusDialog(
655     Window* pParent,
656     uno::Reference< linguistic2::XThesaurus >  xThes,
657     const String &rWord,
658     LanguageType nLanguage ) :
659 
660     SvxStandardDialog( pParent, CUI_RES( RID_SVXDLG_THESAURUS ) )
661 {
662     m_pImpl = std::auto_ptr< SvxThesaurusDialog_Impl >(new SvxThesaurusDialog_Impl( this ));
663 
664     m_pImpl->xThesaurus = xThes;
665     m_pImpl->aLookUpText = OUString( rWord );
666     m_pImpl->nLookUpLanguage = nLanguage;
667     if (rWord.Len() > 0)
668         m_pImpl->aLookUpHistory.push( rWord );
669 
670     FreeResource();
671 
672     OUString aTmp( rWord );
673     linguistic::RemoveHyphens( aTmp );
674     linguistic::ReplaceControlChars( aTmp );
675     String aTmp2( aTmp );
676     m_pImpl->aReplaceEdit.SetText( aTmp2 );
677     m_pImpl->aWordCB.InsertEntry( aTmp2 );
678 
679     m_pImpl->LookUp( aTmp2 );
680     m_pImpl->m_pAlternativesCT->GrabFocus();
681     m_pImpl->aLeftBtn.Enable( sal_False );
682 
683     // fill language menu button list
684     SvtLanguageTable aLangTab;
685     uno::Sequence< lang::Locale > aLocales;
686     if (m_pImpl->xThesaurus.is())
687         aLocales = m_pImpl->xThesaurus->getLocales();
688     const sal_Int32 nLocales = aLocales.getLength();
689     const lang::Locale *pLocales = aLocales.getConstArray();
690     delete m_pImpl->aLangMBtn.GetPopupMenu();
691     PopupMenu* pMenu = new PopupMenu;
692     pMenu->SetMenuFlags( MENU_FLAG_NOAUTOMNEMONICS );
693     std::vector< OUString > aLangVec;
694     for (sal_Int32 i = 0;  i < nLocales;  ++i )
695     {
696         const LanguageType nLang = SvxLocaleToLanguage( pLocales[i] );
697         DBG_ASSERT( nLang != LANGUAGE_NONE && nLang != LANGUAGE_DONTKNOW, "failed to get language" );
698         aLangVec.push_back( aLangTab.GetString( nLang ) );
699     }
700     std::sort( aLangVec.begin(), aLangVec.end() );
701     for (size_t i = 0;  i < aLangVec.size();  ++i)
702         pMenu->InsertItem( (sal_uInt16)i+1, aLangVec[i] );  // menu items should be enumerated from 1 and not 0
703     m_pImpl->aLangMBtn.SetPopupMenu( pMenu );
704 
705     SetWindowTitle( nLanguage );
706 
707     // disable controls if service is missing
708     if (!m_pImpl->xThesaurus.is())
709         Enable( sal_False );
710 }
711 
712 
~SvxThesaurusDialog()713 SvxThesaurusDialog::~SvxThesaurusDialog()
714 {
715 }
716 
717 
SetWindowTitle(LanguageType nLanguage)718 void SvxThesaurusDialog::SetWindowTitle( LanguageType nLanguage )
719 {
720     // Sprache anpassen
721     String aStr( GetText() );
722     aStr.Erase( aStr.Search( sal_Unicode( '(' ) ) - 1 );
723     aStr.Append( UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( " (" ) ) );
724     aStr += SvtLanguageTable().GetLanguageString( nLanguage );
725     aStr.Append( sal_Unicode( ')' ) );
726     SetText( aStr );    // set window title
727 }
728 
729 
GetWord()730 String SvxThesaurusDialog::GetWord()
731 {
732     return m_pImpl->aReplaceEdit.GetText();
733 }
734 
735 
GetLanguage() const736 sal_uInt16 SvxThesaurusDialog::GetLanguage() const
737 {
738     return m_pImpl->nLookUpLanguage;
739 }
740 
741 
Apply()742 void SvxThesaurusDialog::Apply()
743 {
744 }
745