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 <basidesh.hxx>
22 #include <iderdll.hxx>
24 #include <localizationmgr.hxx>
25 #include <managelang.hxx>
27 #include <strings.hrc>
29 #include <comphelper/sequence.hxx>
30 #include <sfx2/bindings.hxx>
31 #include <sfx2/sfxsids.hrc>
32 #include <svtools/langtab.hxx>
33 #include <svx/langbox.hxx>
35 #include <vcl/svapp.hxx>
36 #include <vcl/weld.hxx>
37 #include <vcl/settings.hxx>
38 #include <tools/debug.hxx>
43 using namespace ::com::sun::star::i18n
;
44 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::resource
;
46 using namespace ::com::sun::star::uno
;
48 bool localesAreEqual( const Locale
& rLocaleLeft
, const Locale
& rLocaleRight
)
50 bool bRet
= ( rLocaleLeft
.Language
== rLocaleRight
.Language
&&
51 rLocaleLeft
.Country
== rLocaleRight
.Country
&&
52 rLocaleLeft
.Variant
== rLocaleRight
.Variant
);
56 ManageLanguageDialog::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"))
66 m_xLanguageLB
->set_size_request(m_xLanguageLB
->get_approximate_digit_width() * 42,
67 m_xLanguageLB
->get_height_rows(10));
71 SelectHdl( *m_xLanguageLB
);
74 ManageLanguageDialog::~ManageLanguageDialog()
79 void ManageLanguageDialog::Init()
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
);
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
) );
94 m_xLanguageLB
->set_selection_mode(SelectionMode::Multiple
);
97 void ManageLanguageDialog::FillLanguageBox()
99 DBG_ASSERT( m_xLocalizationMgr
, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
101 if ( m_xLocalizationMgr
->isLibraryLocalized() )
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
)
109 bool bIsDefault
= localesAreEqual( aDefaultLocale
, pLocale
[i
] );
110 LanguageType eLangType
= LanguageTag::convertToLanguageType( pLocale
[i
] );
111 OUString sLanguage
= SvtLanguageTable::GetLanguageString( eLangType
);
114 sLanguage
+= " " + m_sDefLangStr
;
116 LanguageEntry
* pEntry
= new LanguageEntry(pLocale
[i
], bIsDefault
);
117 m_xLanguageLB
->append(weld::toId(pEntry
), sLanguage
);
121 m_xLanguageLB
->append_text(m_sCreateLangStr
);
124 void ManageLanguageDialog::ClearLanguageBox()
126 const sal_Int32 nCount
= m_xLanguageLB
->n_children();
127 for (sal_Int32 i
= 0; i
< nCount
; ++i
)
129 LanguageEntry
* pEntry
= weld::fromId
<LanguageEntry
*>(m_xLanguageLB
->get_id(i
));
132 m_xLanguageLB
->clear();
135 IMPL_LINK_NOARG(ManageLanguageDialog
, AddHdl
, weld::Button
&, void)
137 auto xDlg
= std::make_shared
<SetDefaultLanguageDialog
>(m_xDialog
.get(), m_xLocalizationMgr
);
138 weld::DialogController::runAsync(xDlg
, [xDlg
,this](sal_Int32 nResult
)
143 Sequence
< Locale
> aLocaleSeq
= xDlg
->GetLocales();
144 m_xLocalizationMgr
->handleAddLocales( aLocaleSeq
);
149 if (SfxBindings
* pBindings
= GetBindingsPtr())
150 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
154 IMPL_LINK_NOARG(ManageLanguageDialog
, DeleteHdl
, weld::Button
&, void)
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
)
161 std::vector
<int> aSelection
= m_xLanguageLB
->get_selected_rows();
162 int nCount
= aSelection
.size();
163 int nPos
= m_xLanguageLB
->get_selected_index();
165 Sequence
< Locale
> aLocaleSeq( nCount
);
166 auto aLocaleSeqRange
= asNonConstRange(aLocaleSeq
);
167 for (int i
= 0; i
< nCount
; ++i
)
169 const sal_Int32 nSelPos
= aSelection
[i
];
170 LanguageEntry
* pEntry
= weld::fromId
<LanguageEntry
*>(m_xLanguageLB
->get_id(nSelPos
));
172 aLocaleSeqRange
[i
] = pEntry
->m_aLocale
;
174 m_xLocalizationMgr
->handleRemoveLocales( aLocaleSeq
);
179 nCount
= m_xLanguageLB
->n_children();
182 m_xLanguageLB
->select(nPos
);
183 SelectHdl( *m_xLanguageLB
);
186 IMPL_LINK_NOARG(ManageLanguageDialog
, MakeDefHdl
, weld::Button
&, void)
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
)
192 // set new default entry
193 m_xLocalizationMgr
->handleSetDefaultLocale( pSelectEntry
->m_aLocale
);
198 m_xLanguageLB
->select(nPos
);
199 SelectHdl( *m_xLanguageLB
);
203 IMPL_LINK_NOARG(ManageLanguageDialog
, SelectHdl
, weld::TreeView
&, void)
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
;
211 m_xDeletePB
->set_sensitive(bEnable
);
212 m_xMakeDefPB
->set_sensitive(bEnable
&& nCount
> 1 && m_xLanguageLB
->count_selected_rows() == 1);
215 // class SetDefaultLanguageDialog -----------------------------------------------
217 SetDefaultLanguageDialog::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")))
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
);
233 if (m_xLocalizationMgr
->isLibraryLocalized())
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();
248 SetDefaultLanguageDialog::~SetDefaultLanguageDialog()
252 void SetDefaultLanguageDialog::FillLanguageBox()
254 // fill list with all languages
255 m_xLanguageCB
->SetLanguageList(SvxLanguageListFlags::ALL
, false);
257 if (m_xLocalizationMgr
->isLibraryLocalized())
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
)
264 m_xLanguageCB
->remove_id(LanguageTag::convertToLanguageType(pLocale
[i
]));
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
)
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()));
277 m_xLanguageCB
.reset();
278 m_xLanguageLB
.reset();
282 const sal_Int32 nCountLang
= m_xLanguageCB
->get_count();
283 for (sal_Int32 j
= 0; j
< nCountLang
; ++j
)
285 LanguageType eLang
= m_xLanguageCB
->get_id(j
);
286 m_xLanguageLB
->append(OUString::number(eLang
.get()), m_xLanguageCB
->get_text(j
));
288 m_xLanguageCB
.reset();
290 // preselect current UI language
291 m_xLanguageLB
->select_id(OUString::number(Application::GetSettings().GetUILanguageTag().getLanguageType().get()));
295 Sequence
< Locale
> SetDefaultLanguageDialog::GetLocales() const
297 bool bNotLocalized
= !m_xLocalizationMgr
->isLibraryLocalized();
300 LanguageType
eType(m_xLanguageLB
->get_selected_id().toUInt32());
301 return {LanguageTag(eType
).getLocale()};
303 std::vector
<Locale
> aLocaleSeq
;
304 const sal_Int32 nCount
= m_xCheckLangLB
->n_children();
305 for (sal_Int32 i
= 0; i
< nCount
; ++i
)
307 if (m_xCheckLangLB
->get_toggle(i
) == TRISTATE_TRUE
)
309 LanguageType
eType(m_xCheckLangLB
->get_id(i
).toUInt32());
310 aLocaleSeq
.push_back(LanguageTag::convertToLocale(eType
));
313 return comphelper::containerToSequence(aLocaleSeq
);
316 } // namespace basctl
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */