LibreOffice Module basctl (master) 1
managelang.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <basidesh.hxx>
21#include <basobj.hxx>
22#include <iderdll.hxx>
23#include <iderid.hxx>
24#include <localizationmgr.hxx>
25#include <managelang.hxx>
26
27#include <strings.hrc>
28
30#include <sfx2/bindings.hxx>
31#include <sfx2/sfxsids.hrc>
32#include <svtools/langtab.hxx>
33#include <svx/langbox.hxx>
34#include <utility>
35#include <vcl/svapp.hxx>
36#include <vcl/weld.hxx>
37#include <vcl/settings.hxx>
38#include <tools/debug.hxx>
39
40namespace basctl
41{
42
43using namespace ::com::sun::star::i18n;
44using namespace ::com::sun::star::lang;
45using namespace ::com::sun::star::resource;
46using namespace ::com::sun::star::uno;
47
48bool localesAreEqual( const Locale& rLocaleLeft, const Locale& rLocaleRight )
49{
50 bool bRet = ( rLocaleLeft.Language == rLocaleRight.Language &&
51 rLocaleLeft.Country == rLocaleRight.Country &&
52 rLocaleLeft.Variant == rLocaleRight.Variant );
53 return bRet;
54}
55
56ManageLanguageDialog::ManageLanguageDialog(weld::Window* pParent, std::shared_ptr<LocalizationMgr> xLMgr)
57 : GenericDialogController(pParent, "modules/BasicIDE/ui/managelanguages.ui", "ManageLanguagesDialog")
58 , m_xLocalizationMgr(std::move(xLMgr))
59 , m_sDefLangStr(IDEResId(RID_STR_DEF_LANG))
60 , m_sCreateLangStr(IDEResId(RID_STR_CREATE_LANG))
61 , m_xLanguageLB(m_xBuilder->weld_tree_view("treeview"))
62 , m_xAddPB(m_xBuilder->weld_button("add"))
63 , m_xDeletePB(m_xBuilder->weld_button("delete"))
64 , m_xMakeDefPB(m_xBuilder->weld_button("default"))
65{
66 m_xLanguageLB->set_size_request(m_xLanguageLB->get_approximate_digit_width() * 42,
67 m_xLanguageLB->get_height_rows(10));
68
69 Init();
71 SelectHdl( *m_xLanguageLB );
72}
73
75{
77}
78
80{
81 // get current IDE
82 Shell* pShell = GetShell();
83 const OUString& sLibName = pShell->GetCurLibName();
84 // set dialog title with library name
85 OUString sText = m_xDialog->get_title();
86 sText = sText.replaceAll("$1", sLibName);
87 m_xDialog->set_title(sText);
88 // set handler
89 m_xAddPB->connect_clicked( LINK( this, ManageLanguageDialog, AddHdl ) );
90 m_xDeletePB->connect_clicked( LINK( this, ManageLanguageDialog, DeleteHdl ) );
91 m_xMakeDefPB->connect_clicked( LINK( this, ManageLanguageDialog, MakeDefHdl ) );
92 m_xLanguageLB->connect_changed( LINK( this, ManageLanguageDialog, SelectHdl ) );
93
94 m_xLanguageLB->set_selection_mode(SelectionMode::Multiple);
95}
96
98{
99 DBG_ASSERT( m_xLocalizationMgr, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
100
101 if ( m_xLocalizationMgr->isLibraryLocalized() )
102 {
103 Locale aDefaultLocale = m_xLocalizationMgr->getStringResourceManager()->getDefaultLocale();
104 Sequence< Locale > aLocaleSeq = m_xLocalizationMgr->getStringResourceManager()->getLocales();
105 const Locale* pLocale = aLocaleSeq.getConstArray();
106 sal_Int32 i, nCount = aLocaleSeq.getLength();
107 for ( i = 0; i < nCount; ++i )
108 {
109 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
111 OUString sLanguage = SvtLanguageTable::GetLanguageString( eLangType );
112 if ( bIsDefault )
113 {
114 sLanguage += " " + m_sDefLangStr;
115 }
116 LanguageEntry* pEntry = new LanguageEntry(pLocale[i], bIsDefault);
117 m_xLanguageLB->append(weld::toId(pEntry), sLanguage);
118 }
119 }
120 else
121 m_xLanguageLB->append_text(m_sCreateLangStr);
122}
123
125{
126 const sal_Int32 nCount = m_xLanguageLB->n_children();
127 for (sal_Int32 i = 0; i < nCount; ++i)
128 {
129 LanguageEntry* pEntry = weld::fromId<LanguageEntry*>(m_xLanguageLB->get_id(i));
130 delete pEntry;
131 }
132 m_xLanguageLB->clear();
133}
134
136{
137 auto xDlg = std::make_shared<SetDefaultLanguageDialog>(m_xDialog.get(), m_xLocalizationMgr);
138 weld::DialogController::runAsync(xDlg, [xDlg,this](sal_Int32 nResult)
139 {
140 if (!nResult )
141 return;
142 // add new locales
143 Sequence< Locale > aLocaleSeq = xDlg->GetLocales();
144 m_xLocalizationMgr->handleAddLocales( aLocaleSeq );
145 // update listbox
146 ClearLanguageBox();
147 FillLanguageBox();
148
149 if (SfxBindings* pBindings = GetBindingsPtr())
150 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
151 });
152}
153
155{
156 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(m_xDialog.get(), "modules/BasicIDE/ui/deletelangdialog.ui"));
157 std::unique_ptr<weld::MessageDialog> xQBox(xBuilder->weld_message_dialog("DeleteLangDialog"));
158 if (xQBox->run() != RET_OK)
159 return;
160
161 std::vector<int> aSelection = m_xLanguageLB->get_selected_rows();
162 int nCount = aSelection.size();
163 int nPos = m_xLanguageLB->get_selected_index();
164 // remove locales
165 Sequence< Locale > aLocaleSeq( nCount );
166 auto aLocaleSeqRange = asNonConstRange(aLocaleSeq);
167 for (int i = 0; i < nCount; ++i)
168 {
169 const sal_Int32 nSelPos = aSelection[i];
170 LanguageEntry* pEntry = weld::fromId<LanguageEntry*>(m_xLanguageLB->get_id(nSelPos));
171 if ( pEntry )
172 aLocaleSeqRange[i] = pEntry->m_aLocale;
173 }
174 m_xLocalizationMgr->handleRemoveLocales( aLocaleSeq );
175 // update listbox
176 ClearLanguageBox();
177 FillLanguageBox();
178 // reset selection
179 nCount = m_xLanguageLB->n_children();
180 if (nCount <= nPos)
181 nPos = nCount - 1;
182 m_xLanguageLB->select(nPos);
183 SelectHdl( *m_xLanguageLB );
184}
185
187{
188 const sal_Int32 nPos = m_xLanguageLB->get_selected_index();
189 LanguageEntry* pSelectEntry = weld::fromId<LanguageEntry*>(m_xLanguageLB->get_id(nPos));
190 if (pSelectEntry && !pSelectEntry->m_bIsDefault)
191 {
192 // set new default entry
193 m_xLocalizationMgr->handleSetDefaultLocale( pSelectEntry->m_aLocale );
194 // update Listbox
195 ClearLanguageBox();
196 FillLanguageBox();
197 // reset selection
198 m_xLanguageLB->select(nPos);
199 SelectHdl( *m_xLanguageLB );
200 }
201}
202
204{
205 const sal_Int32 nCount = m_xLanguageLB->n_children();
206 bool bEmpty = ( !nCount ||
207 m_xLanguageLB->find_text(m_sCreateLangStr) != -1 );
208 bool bSelect = ( m_xLanguageLB->get_selected_index() != -1 );
209 bool bEnable = !bEmpty && bSelect;
210
211 m_xDeletePB->set_sensitive(bEnable);
212 m_xMakeDefPB->set_sensitive(bEnable && nCount > 1 && m_xLanguageLB->count_selected_rows() == 1);
213}
214
215// class SetDefaultLanguageDialog -----------------------------------------------
216
217SetDefaultLanguageDialog::SetDefaultLanguageDialog(weld::Window* pParent, std::shared_ptr<LocalizationMgr> xLMgr)
218 : GenericDialogController(pParent, "modules/BasicIDE/ui/defaultlanguage.ui", "DefaultLanguageDialog")
219 , m_xLocalizationMgr(std::move(xLMgr))
220 , m_xLanguageFT(m_xBuilder->weld_label("defaultlabel"))
221 , m_xLanguageLB(m_xBuilder->weld_tree_view("entries"))
222 , m_xCheckLangFT(m_xBuilder->weld_label("checkedlabel"))
223 , m_xCheckLangLB(m_xBuilder->weld_tree_view("checkedentries"))
224 , m_xDefinedFT(m_xBuilder->weld_label("defined"))
225 , m_xAddedFT(m_xBuilder->weld_label("added"))
226 , m_xAltTitle(m_xBuilder->weld_label("alttitle"))
227 , m_xLanguageCB(new SvxLanguageBox(m_xBuilder->weld_combo_box("hidden")))
228{
229 m_xLanguageLB->set_size_request(-1, m_xLanguageLB->get_height_rows(10));
230 m_xCheckLangLB->set_size_request(-1, m_xCheckLangLB->get_height_rows(10));
231 m_xCheckLangLB->enable_toggle_buttons(weld::ColumnToggleType::Check);
232
233 if (m_xLocalizationMgr->isLibraryLocalized())
234 {
235 // change to "Add Interface Language" mode
236 m_xLanguageLB->hide();
237 m_xCheckLangLB->show();
238 m_xDialog->set_title(m_xAltTitle->get_label());
239 m_xLanguageFT->hide();
240 m_xCheckLangFT->show();
241 m_xDefinedFT->hide();
242 m_xAddedFT->show();
243 }
244
246}
247
249{
250}
251
253{
254 // fill list with all languages
255 m_xLanguageCB->SetLanguageList(SvxLanguageListFlags::ALL, false);
256
257 if (m_xLocalizationMgr->isLibraryLocalized())
258 {
259 // remove the already localized languages
260 Sequence< Locale > aLocaleSeq = m_xLocalizationMgr->getStringResourceManager()->getLocales();
261 const Locale* pLocale = aLocaleSeq.getConstArray();
262 const sal_Int32 nCountLoc = aLocaleSeq.getLength();
263 for ( sal_Int32 i = 0; i < nCountLoc; ++i )
265
266 // fill checklistbox if not in default mode
267 const sal_Int32 nCountLang = m_xLanguageCB->get_count();
268 for (sal_Int32 j = 0; j < nCountLang; ++j)
269 {
270 LanguageType eLang = m_xLanguageCB->get_id(j);
271 m_xCheckLangLB->append();
272 const int nRow = m_xCheckLangLB->n_children() - 1;
273 m_xCheckLangLB->set_toggle(nRow, TRISTATE_FALSE);
274 m_xCheckLangLB->set_text(nRow, m_xLanguageCB->get_text(j), 0);
275 m_xCheckLangLB->set_id(nRow, OUString::number(eLang.get()));
276 }
277 m_xLanguageCB.reset();
278 m_xLanguageLB.reset();
279 }
280 else
281 {
282 const sal_Int32 nCountLang = m_xLanguageCB->get_count();
283 for (sal_Int32 j = 0; j < nCountLang; ++j)
284 {
285 LanguageType eLang = m_xLanguageCB->get_id(j);
286 m_xLanguageLB->append(OUString::number(eLang.get()), m_xLanguageCB->get_text(j));
287 }
288 m_xLanguageCB.reset();
289
290 // preselect current UI language
291 m_xLanguageLB->select_id(OUString::number(Application::GetSettings().GetUILanguageTag().getLanguageType().get()));
292 }
293}
294
296{
297 bool bNotLocalized = !m_xLocalizationMgr->isLibraryLocalized();
298 if (bNotLocalized)
299 {
300 LanguageType eType(m_xLanguageLB->get_selected_id().toUInt32());
301 return {LanguageTag(eType).getLocale()};
302 }
303 std::vector<Locale> aLocaleSeq;
304 const sal_Int32 nCount = m_xCheckLangLB->n_children();
305 for (sal_Int32 i = 0; i < nCount; ++i)
306 {
307 if (m_xCheckLangLB->get_toggle(i) == TRISTATE_TRUE)
308 {
309 LanguageType eType(m_xCheckLangLB->get_id(i).toUInt32());
310 aLocaleSeq.push_back(LanguageTag::convertToLocale(eType));
311 }
312 }
313 return comphelper::containerToSequence(aLocaleSeq);
314}
315
316} // namespace basctl
317
318/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
static const AllSettings & GetSettings()
static std::unique_ptr< weld::Builder > CreateBuilder(weld::Widget *pParent, const OUString &rUIFile, bool bMobile=false, sal_uInt64 nLOKWindowId=0)
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
static OUString GetLanguageString(const LanguageType eType)
std::unique_ptr< weld::Button > m_xDeletePB
Definition: managelang.hxx:56
std::unique_ptr< weld::Button > m_xMakeDefPB
Definition: managelang.hxx:57
std::unique_ptr< weld::Button > m_xAddPB
Definition: managelang.hxx:55
ManageLanguageDialog(weld::Window *pParent, std::shared_ptr< LocalizationMgr > _pLMgr)
Definition: managelang.cxx:56
std::shared_ptr< LocalizationMgr > m_xLocalizationMgr
Definition: managelang.hxx:49
virtual ~ManageLanguageDialog() override
Definition: managelang.cxx:74
std::unique_ptr< weld::TreeView > m_xLanguageLB
Definition: managelang.hxx:54
SetDefaultLanguageDialog(weld::Window *pParent, std::shared_ptr< LocalizationMgr > xLMgr)
Definition: managelang.cxx:217
std::unique_ptr< weld::Label > m_xAltTitle
Definition: managelang.hxx:86
virtual ~SetDefaultLanguageDialog() override
Definition: managelang.cxx:248
std::unique_ptr< weld::Label > m_xCheckLangFT
Definition: managelang.hxx:82
std::unique_ptr< weld::Label > m_xLanguageFT
Definition: managelang.hxx:80
css::uno::Sequence< css::lang::Locale > GetLocales() const
Definition: managelang.cxx:295
std::unique_ptr< weld::Label > m_xAddedFT
Definition: managelang.hxx:85
std::shared_ptr< LocalizationMgr > m_xLocalizationMgr
Definition: managelang.hxx:76
std::unique_ptr< SvxLanguageBox > m_xLanguageCB
Definition: managelang.hxx:87
std::unique_ptr< weld::Label > m_xDefinedFT
Definition: managelang.hxx:84
std::unique_ptr< weld::TreeView > m_xCheckLangLB
Definition: managelang.hxx:83
std::unique_ptr< weld::TreeView > m_xLanguageLB
Definition: managelang.hxx:81
OUString const & GetCurLibName() const
Definition: basidesh.hxx:163
static bool runAsync(const std::shared_ptr< DialogController > &rController, const std::function< void(sal_Int32)> &)
std::shared_ptr< weld::Dialog > m_xDialog
int nCount
#define DBG_ASSERT(sCon, aError)
DocumentType eType
TRISTATE_FALSE
TRISTATE_TRUE
sal_uInt16 nPos
const char * pLocale
SfxBindings * GetBindingsPtr()
Definition: basobj3.cxx:426
bool localesAreEqual(const Locale &rLocaleLeft, const Locale &rLocaleRight)
Definition: managelang.cxx:48
Shell * GetShell()
Definition: iderdll.cxx:80
IMPL_LINK_NOARG(EditorWindow, SetSourceInBasicHdl, void *, void)
Definition: baside2b.cxx:987
OUString IDEResId(TranslateId aId)
Definition: iderdll.cxx:108
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
int i
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
OUString toId(const void *pValue)
css::lang::Locale m_aLocale
Definition: managelang.hxx:34
UNDERLYING_TYPE get() const
RET_OK