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
]);
120 m_xAlternativesCT
->thaw();
122 return nMeanings
> 0;
125 void SvxThesaurusDialog::LookUp( const OUString
&rText
)
127 if (rText
!= m_xWordCB
->get_active_text()) // avoid moving of the cursor if the text is the same
128 m_xWordCB
->set_entry_text(rText
);
132 IMPL_LINK_NOARG(SvxThesaurusDialog
, LeftBtnHdl_Impl
, weld::Button
&, void)
134 if (aLookUpHistory
.size() >= 2)
136 aLookUpHistory
.pop(); // remove current look up word from stack
137 m_xWordCB
->set_entry_text(aLookUpHistory
.top()); // retrieve previous look up word
138 aLookUpHistory
.pop();
143 IMPL_LINK( SvxThesaurusDialog
, LanguageHdl_Impl
, weld::ComboBox
&, rLB
, void )
145 OUString
aLangText(rLB
.get_active_text());
146 LanguageType nLang
= SvtLanguageTable::GetLanguageType( aLangText
);
147 DBG_ASSERT( nLang
!= LANGUAGE_NONE
&& nLang
!= LANGUAGE_DONTKNOW
, "failed to get language" );
148 if (xThesaurus
->hasLocale( LanguageTag::convertToLocale( nLang
) ))
149 nLookUpLanguage
= nLang
;
150 SetWindowTitle( nLang
);
154 void SvxThesaurusDialog::LookUp_Impl()
156 OUString
aText(m_xWordCB
->get_active_text());
159 if (!aLookUpText
.isEmpty() &&
160 (aLookUpHistory
.empty() || aLookUpText
!= aLookUpHistory
.top()))
161 aLookUpHistory
.push( aLookUpText
);
163 m_bWordFound
= UpdateAlternativesBox_Impl();
164 m_xAlternativesCT
->set_visible(m_bWordFound
);
165 m_xNotFound
->set_visible(!m_bWordFound
);
168 Application::PostUserEvent(LINK(this, SvxThesaurusDialog
, SelectFirstHdl_Impl
));
170 if (m_xWordCB
->find_text(aText
) == -1)
171 m_xWordCB
->append_text(aText
);
173 m_xReplaceEdit
->set_text( OUString() );
174 ReplaceEditHdl_Impl(*m_xReplaceEdit
);
175 m_xLeftBtn
->set_sensitive( aLookUpHistory
.size() > 1 );
178 IMPL_LINK_NOARG(SvxThesaurusDialog
, WordSelectHdl_Impl
, weld::ComboBox
&, void)
180 m_aModifyIdle
.Start();
183 IMPL_LINK( SvxThesaurusDialog
, AlternativesSelectHdl_Impl
, weld::TreeView
&, rBox
, void )
185 int nEntry
= rBox
.get_selected_index();
188 bool bIsHeader
= rBox
.get_text_emphasis(nEntry
, 0);
194 OUString aStr
= linguistic::GetThesaurusReplaceText(rBox
.get_text(nEntry
));
195 m_xReplaceEdit
->set_text(aStr
);
196 ReplaceEditHdl_Impl(*m_xReplaceEdit
);
200 IMPL_LINK( SvxThesaurusDialog
, AlternativesDoubleClickHdl_Impl
, weld::TreeView
&, rBox
, bool )
202 int nEntry
= rBox
.get_selected_index();
205 bool bIsHeader
= rBox
.get_text_emphasis(nEntry
, 0);
211 OUString aStr
= linguistic::GetThesaurusReplaceText(rBox
.get_text(nEntry
));
212 m_xWordCB
->set_entry_text(aStr
);
217 //! workaround to set the selection since calling SelectEntryPos within
218 //! the double click handler does not work
219 Application::PostUserEvent(LINK(this, SvxThesaurusDialog
, SelectFirstHdl_Impl
));
224 IMPL_LINK_NOARG(SvxThesaurusDialog
, SelectFirstHdl_Impl
, void *, void)
226 if (m_xAlternativesCT
->n_children() >= 2)
228 m_xAlternativesCT
->select(1); // pos 0 is a 'header' that is not selectable
229 AlternativesSelectHdl_Impl(*m_xAlternativesCT
);
233 // class SvxThesaurusDialog ----------------------------------------------
235 SvxThesaurusDialog::SvxThesaurusDialog(
236 weld::Window
* pParent
,
237 uno::Reference
< linguistic2::XThesaurus
> const & xThes
,
238 const OUString
&rWord
,
239 LanguageType nLanguage
)
240 : SfxDialogController(pParent
, "cui/ui/thesaurus.ui", "ThesaurusDialog")
241 , m_aModifyIdle("cui SvxThesaurusDialog LookUp Modify")
243 , nLookUpLanguage(LANGUAGE_NONE
)
244 , m_bWordFound(false)
245 , m_xLeftBtn(m_xBuilder
->weld_button("left"))
246 , m_xWordCB(m_xBuilder
->weld_combo_box("wordcb"))
247 , m_xAlternativesCT(m_xBuilder
->weld_tree_view("alternatives"))
248 , m_xNotFound(m_xBuilder
->weld_label("notfound"))
249 , m_xReplaceEdit(m_xBuilder
->weld_entry("replaceed"))
250 , m_xLangLB(m_xBuilder
->weld_combo_box("langcb"))
251 , m_xReplaceBtn(m_xBuilder
->weld_button("ok"))
253 m_aModifyIdle
.SetInvokeHandler( LINK( this, SvxThesaurusDialog
, ModifyTimer_Hdl
) );
254 m_aModifyIdle
.SetPriority( TaskPriority::LOWEST
);
256 m_xReplaceEdit
->connect_changed( LINK( this, SvxThesaurusDialog
, ReplaceEditHdl_Impl
) );
257 m_xReplaceBtn
->connect_clicked( LINK( this, SvxThesaurusDialog
, ReplaceBtnHdl_Impl
) );
258 m_xLeftBtn
->connect_clicked( LINK( this, SvxThesaurusDialog
, LeftBtnHdl_Impl
) );
259 m_xWordCB
->set_entry_completion(false);
260 m_xWordCB
->connect_changed( LINK( this, SvxThesaurusDialog
, WordSelectHdl_Impl
) );
261 m_xLangLB
->connect_changed( LINK( this, SvxThesaurusDialog
, LanguageHdl_Impl
) );
262 m_xAlternativesCT
->connect_changed( LINK( this, SvxThesaurusDialog
, AlternativesSelectHdl_Impl
));
263 m_xAlternativesCT
->connect_row_activated( LINK( this, SvxThesaurusDialog
, AlternativesDoubleClickHdl_Impl
));
264 m_xAlternativesCT
->connect_key_press(LINK(this, SvxThesaurusDialog
, KeyInputHdl
));
268 nLookUpLanguage
= nLanguage
;
269 if (!rWord
.isEmpty())
270 aLookUpHistory
.push( rWord
);
272 OUString
aTmp( rWord
);
273 (void)linguistic::RemoveHyphens( aTmp
);
274 (void)linguistic::ReplaceControlChars( aTmp
);
275 m_xReplaceEdit
->set_text( aTmp
);
276 ReplaceEditHdl_Impl(*m_xReplaceEdit
);
277 m_xWordCB
->append_text( aTmp
);
280 m_xAlternativesCT
->grab_focus();
281 m_xLeftBtn
->set_sensitive(false);
283 // fill language menu button list
284 uno::Sequence
< lang::Locale
> aLocales
;
286 aLocales
= xThesaurus
->getLocales();
287 const sal_Int32 nLocales
= aLocales
.getLength();
288 const lang::Locale
*pLocales
= aLocales
.getConstArray();
290 std::vector
< OUString
> aLangVec
;
291 for (sal_Int32 i
= 0; i
< nLocales
; ++i
)
293 const LanguageType nLang
= LanguageTag::convertToLanguageType( pLocales
[i
] );
294 DBG_ASSERT( nLang
!= LANGUAGE_NONE
&& nLang
!= LANGUAGE_DONTKNOW
, "failed to get language" );
295 aLangVec
.push_back( SvtLanguageTable::GetLanguageString( nLang
) );
297 std::sort( aLangVec
.begin(), aLangVec
.end() );
299 for (const OUString
& i
: aLangVec
)
300 m_xLangLB
->append_text(i
);
303 std::vector
< OUString
>::iterator aI
= std::find(aLangVec
.begin(), aLangVec
.end(),
304 SvtLanguageTable::GetLanguageString(nLanguage
));
305 if (aI
!= aLangVec
.end())
307 m_xLangLB
->set_active_text(*aI
);
310 SetWindowTitle(nLanguage
);
312 // disable controls if service is missing
313 if (!xThesaurus
.is())
314 m_xDialog
->set_sensitive(false);
317 SvxThesaurusDialog::~SvxThesaurusDialog()
321 IMPL_LINK_NOARG(SvxThesaurusDialog
, ReplaceBtnHdl_Impl
, weld::Button
&, void)
323 m_xDialog
->response(RET_OK
);
326 void SvxThesaurusDialog::SetWindowTitle( LanguageType nLanguage
)
329 OUString
aStr(m_xDialog
->get_title());
330 sal_Int32 nIndex
= aStr
.indexOf( '(' );
332 aStr
= aStr
.copy( 0, nIndex
- 1 );
333 aStr
+= " (" + SvtLanguageTable::GetLanguageString( nLanguage
) + ")";
334 m_xDialog
->set_title(aStr
); // set window title
337 OUString
SvxThesaurusDialog::GetWord() const
339 return m_xReplaceEdit
->get_text();
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */