1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <thesdlg.hxx>
22 #include <tools/debug.hxx>
23 #include <svl/lngmisc.hxx>
24 #include <vcl/event.hxx>
25 #include <vcl/svapp.hxx>
26 #include <svtools/langtab.hxx>
27 #include <i18nlangtag/languagetag.hxx>
28 #include <comphelper/string.hxx>
33 #include <com/sun/star/linguistic2/XThesaurus.hpp>
34 #include <com/sun/star/linguistic2/XMeaning.hpp>
36 using namespace ::com::sun::star
;
38 IMPL_LINK_NOARG( SvxThesaurusDialog
, ModifyTimer_Hdl
, Timer
*, void )
40 LookUp(m_xWordCB
->get_active_text());
44 IMPL_LINK_NOARG(SvxThesaurusDialog
, ReplaceEditHdl_Impl
, weld::Entry
&, void)
46 m_xReplaceBtn
->set_sensitive(!m_xReplaceEdit
->get_text().isEmpty());
49 IMPL_LINK(SvxThesaurusDialog
, KeyInputHdl
, const KeyEvent
&, rKEvt
, bool)
51 const vcl::KeyCode
& rKey
= rKEvt
.GetKeyCode();
53 if (rKey
.GetCode() == KEY_RETURN
)
55 m_xDialog
->response(RET_OK
);
62 uno::Sequence
< uno::Reference
< linguistic2::XMeaning
> > SvxThesaurusDialog::queryMeanings_Impl(
64 const lang::Locale
& rLocale
,
65 const beans::PropertyValues
& rProperties
)
67 uno::Sequence
< uno::Reference
< linguistic2::XMeaning
> > aMeanings(
68 xThesaurus
->queryMeanings( rTerm
, rLocale
, rProperties
) );
70 // text with '.' at the end?
71 if ( !aMeanings
.hasElements() && rTerm
.endsWith(".") )
73 // try again without trailing '.' chars. It may be a word at the
74 // end of a sentence and not an abbreviation...
75 OUString
aTxt(comphelper::string::stripEnd(rTerm
, '.'));
76 aMeanings
= xThesaurus
->queryMeanings( aTxt
, rLocale
, rProperties
);
77 if (aMeanings
.hasElements())
86 bool SvxThesaurusDialog::UpdateAlternativesBox_Impl()
88 lang::Locale
aLocale( LanguageTag::convertToLocale( nLookUpLanguage
) );
89 uno::Sequence
< uno::Reference
< linguistic2::XMeaning
> > aMeanings
= queryMeanings_Impl(
90 aLookUpText
, aLocale
, uno::Sequence
< beans::PropertyValue
>() );
91 const sal_Int32 nMeanings
= aMeanings
.getLength();
92 const uno::Reference
< linguistic2::XMeaning
> *pMeanings
= aMeanings
.getConstArray();
94 m_xAlternativesCT
->freeze();
96 m_xAlternativesCT
->clear();
98 for (sal_Int32 i
= 0; i
< nMeanings
; ++i
)
100 OUString rMeaningTxt
= pMeanings
[i
]->getMeaning();
101 uno::Sequence
< OUString
> aSynonyms( pMeanings
[i
]->querySynonyms() );
102 const sal_Int32 nSynonyms
= aSynonyms
.getLength();
103 const OUString
*pSynonyms
= aSynonyms
.getConstArray();
104 DBG_ASSERT( !rMeaningTxt
.isEmpty(), "meaning with empty text" );
105 DBG_ASSERT( nSynonyms
> 0, "meaning without synonym" );
107 OUString sHeading
= OUString::number(i
+ 1) + ". " + rMeaningTxt
;
108 m_xAlternativesCT
->append_text(sHeading
);
109 m_xAlternativesCT
->set_text_emphasis(nRow
, true, 0);
112 for (sal_Int32 k
= 0; k
< nSynonyms
; ++k
)
114 // GetThesaurusReplaceText will strip the leading spaces
115 m_xAlternativesCT
->append_text(" " + pSynonyms
[k
]);
116 m_xAlternativesCT
->set_text_emphasis(nRow
, false, 0);
121 m_xAlternativesCT
->thaw();
123 return nMeanings
> 0;
126 void SvxThesaurusDialog::LookUp( const OUString
&rText
)
128 if (rText
!= m_xWordCB
->get_active_text()) // avoid moving of the cursor if the text is the same
129 m_xWordCB
->set_entry_text(rText
);
133 IMPL_LINK_NOARG(SvxThesaurusDialog
, LeftBtnHdl_Impl
, weld::Button
&, void)
135 if (aLookUpHistory
.size() >= 2)
137 aLookUpHistory
.pop(); // remove current look up word from stack
138 m_xWordCB
->set_entry_text(aLookUpHistory
.top()); // retrieve previous look up word
139 aLookUpHistory
.pop();
144 IMPL_LINK( SvxThesaurusDialog
, LanguageHdl_Impl
, weld::ComboBox
&, rLB
, void )
146 OUString
aLangText(rLB
.get_active_text());
147 LanguageType nLang
= SvtLanguageTable::GetLanguageType( aLangText
);
148 DBG_ASSERT( nLang
!= LANGUAGE_NONE
&& nLang
!= LANGUAGE_DONTKNOW
, "failed to get language" );
149 if (xThesaurus
->hasLocale( LanguageTag::convertToLocale( nLang
) ))
150 nLookUpLanguage
= nLang
;
151 SetWindowTitle( nLang
);
155 void SvxThesaurusDialog::LookUp_Impl()
157 OUString
aText(m_xWordCB
->get_active_text());
160 if (!aLookUpText
.isEmpty() &&
161 (aLookUpHistory
.empty() || aLookUpText
!= aLookUpHistory
.top()))
162 aLookUpHistory
.push( aLookUpText
);
164 m_bWordFound
= UpdateAlternativesBox_Impl();
165 m_xAlternativesCT
->set_visible(m_bWordFound
);
166 m_xNotFound
->set_visible(!m_bWordFound
);
168 if (m_bWordFound
&& !m_nSelectFirstEvent
)
169 m_nSelectFirstEvent
= Application::PostUserEvent(LINK(this, SvxThesaurusDialog
, SelectFirstHdl_Impl
));
171 if (m_xWordCB
->find_text(aText
) == -1)
172 m_xWordCB
->append_text(aText
);
174 m_xReplaceEdit
->set_text( OUString() );
175 ReplaceEditHdl_Impl(*m_xReplaceEdit
);
176 m_xLeftBtn
->set_sensitive( aLookUpHistory
.size() > 1 );
179 IMPL_LINK_NOARG(SvxThesaurusDialog
, WordSelectHdl_Impl
, weld::ComboBox
&, void)
181 m_aModifyIdle
.Start();
184 IMPL_LINK( SvxThesaurusDialog
, AlternativesSelectHdl_Impl
, weld::TreeView
&, rBox
, void )
186 int nEntry
= rBox
.get_selected_index();
189 bool bIsHeader
= rBox
.get_text_emphasis(nEntry
, 0);
195 OUString aStr
= linguistic::GetThesaurusReplaceText(rBox
.get_text(nEntry
));
196 m_xReplaceEdit
->set_text(aStr
);
197 ReplaceEditHdl_Impl(*m_xReplaceEdit
);
201 IMPL_LINK( SvxThesaurusDialog
, AlternativesDoubleClickHdl_Impl
, weld::TreeView
&, rBox
, bool )
203 int nEntry
= rBox
.get_selected_index();
206 bool bIsHeader
= rBox
.get_text_emphasis(nEntry
, 0);
212 OUString aStr
= linguistic::GetThesaurusReplaceText(rBox
.get_text(nEntry
));
213 m_xWordCB
->set_entry_text(aStr
);
218 //! workaround to set the selection since calling SelectEntryPos within
219 //! the double click handler does not work
220 if (!m_nSelectFirstEvent
)
221 m_nSelectFirstEvent
= Application::PostUserEvent(LINK(this, SvxThesaurusDialog
, SelectFirstHdl_Impl
));
226 IMPL_LINK_NOARG(SvxThesaurusDialog
, SelectFirstHdl_Impl
, void *, void)
228 m_nSelectFirstEvent
= nullptr;
229 if (m_xAlternativesCT
->n_children() >= 2)
231 m_xAlternativesCT
->select(1); // pos 0 is a 'header' that is not selectable
232 AlternativesSelectHdl_Impl(*m_xAlternativesCT
);
236 // class SvxThesaurusDialog ----------------------------------------------
238 SvxThesaurusDialog::SvxThesaurusDialog(
239 weld::Widget
* pParent
,
240 uno::Reference
< linguistic2::XThesaurus
> const & xThes
,
241 const OUString
&rWord
,
242 LanguageType nLanguage
)
243 : SfxDialogController(pParent
, "cui/ui/thesaurus.ui", "ThesaurusDialog")
244 , m_aModifyIdle("cui SvxThesaurusDialog LookUp Modify")
245 , nLookUpLanguage(LANGUAGE_NONE
)
246 , m_bWordFound(false)
247 , m_xLeftBtn(m_xBuilder
->weld_button("left"))
248 , m_xWordCB(m_xBuilder
->weld_combo_box("wordcb"))
249 , m_xAlternativesCT(m_xBuilder
->weld_tree_view("alternatives"))
250 , m_xNotFound(m_xBuilder
->weld_label("notfound"))
251 , m_xReplaceEdit(m_xBuilder
->weld_entry("replaceed"))
252 , m_xLangLB(m_xBuilder
->weld_combo_box("langcb"))
253 , m_xReplaceBtn(m_xBuilder
->weld_button("ok"))
254 , m_nSelectFirstEvent(nullptr)
256 m_aModifyIdle
.SetInvokeHandler( LINK( this, SvxThesaurusDialog
, ModifyTimer_Hdl
) );
257 m_aModifyIdle
.SetPriority( TaskPriority::LOWEST
);
259 m_xReplaceEdit
->connect_changed( LINK( this, SvxThesaurusDialog
, ReplaceEditHdl_Impl
) );
260 m_xReplaceBtn
->connect_clicked( LINK( this, SvxThesaurusDialog
, ReplaceBtnHdl_Impl
) );
261 m_xLeftBtn
->connect_clicked( LINK( this, SvxThesaurusDialog
, LeftBtnHdl_Impl
) );
262 m_xWordCB
->set_entry_completion(false);
263 m_xWordCB
->connect_changed( LINK( this, SvxThesaurusDialog
, WordSelectHdl_Impl
) );
264 m_xLangLB
->connect_changed( LINK( this, SvxThesaurusDialog
, LanguageHdl_Impl
) );
265 m_xAlternativesCT
->connect_changed( LINK( this, SvxThesaurusDialog
, AlternativesSelectHdl_Impl
));
266 m_xAlternativesCT
->connect_row_activated( LINK( this, SvxThesaurusDialog
, AlternativesDoubleClickHdl_Impl
));
267 m_xAlternativesCT
->connect_key_press(LINK(this, SvxThesaurusDialog
, KeyInputHdl
));
271 nLookUpLanguage
= nLanguage
;
272 if (!rWord
.isEmpty())
273 aLookUpHistory
.push( rWord
);
275 OUString
aTmp( rWord
);
276 (void)linguistic::RemoveHyphens( aTmp
);
277 (void)linguistic::ReplaceControlChars( aTmp
);
278 m_xReplaceEdit
->set_text( aTmp
);
279 ReplaceEditHdl_Impl(*m_xReplaceEdit
);
280 m_xWordCB
->append_text( aTmp
);
283 m_xAlternativesCT
->grab_focus();
284 m_xLeftBtn
->set_sensitive(false);
286 // fill language menu button list
287 uno::Sequence
< lang::Locale
> aLocales
;
289 aLocales
= xThesaurus
->getLocales();
290 const sal_Int32 nLocales
= aLocales
.getLength();
291 const lang::Locale
*pLocales
= aLocales
.getConstArray();
293 std::vector
< OUString
> aLangVec
;
294 for (sal_Int32 i
= 0; i
< nLocales
; ++i
)
296 const LanguageType nLang
= LanguageTag::convertToLanguageType( pLocales
[i
] );
297 DBG_ASSERT( nLang
!= LANGUAGE_NONE
&& nLang
!= LANGUAGE_DONTKNOW
, "failed to get language" );
298 aLangVec
.push_back( SvtLanguageTable::GetLanguageString( nLang
) );
300 std::sort( aLangVec
.begin(), aLangVec
.end() );
302 for (const OUString
& i
: aLangVec
)
303 m_xLangLB
->append_text(i
);
306 std::vector
< OUString
>::iterator aI
= std::find(aLangVec
.begin(), aLangVec
.end(),
307 SvtLanguageTable::GetLanguageString(nLanguage
));
308 if (aI
!= aLangVec
.end())
310 m_xLangLB
->set_active_text(*aI
);
313 SetWindowTitle(nLanguage
);
315 // disable controls if service is missing
316 if (!xThesaurus
.is())
317 m_xDialog
->set_sensitive(false);
319 m_xWordCB
->grab_focus();
322 SvxThesaurusDialog::~SvxThesaurusDialog()
324 if (m_nSelectFirstEvent
)
326 Application::RemoveUserEvent(m_nSelectFirstEvent
);
327 m_nSelectFirstEvent
= nullptr;
331 IMPL_LINK_NOARG(SvxThesaurusDialog
, ReplaceBtnHdl_Impl
, weld::Button
&, void)
333 m_xDialog
->response(RET_OK
);
336 void SvxThesaurusDialog::SetWindowTitle( LanguageType nLanguage
)
339 OUString
aStr(m_xDialog
->get_title());
340 sal_Int32 nIndex
= aStr
.indexOf( '(' );
342 aStr
= aStr
.copy( 0, nIndex
- 1 );
343 aStr
+= " (" + SvtLanguageTable::GetLanguageString( nLanguage
) + ")";
344 m_xDialog
->set_title(aStr
); // set window title
347 OUString
SvxThesaurusDialog::GetWord() const
349 return m_xReplaceEdit
->get_text();
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */