tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / basctl / source / dlged / managelang.cxx
blob3222733ebc10bf765a28e2a3c72d78ad978f4cad
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <basobj.hxx>
22 #include <iderdll.hxx>
23 #include <iderid.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>
34 #include <utility>
35 #include <vcl/svapp.hxx>
36 #include <vcl/weld.hxx>
37 #include <vcl/settings.hxx>
38 #include <tools/debug.hxx>
40 namespace basctl
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::resource;
45 using namespace ::com::sun::star::uno;
47 bool localesAreEqual( const Locale& rLocaleLeft, const Locale& rLocaleRight )
49 bool bRet = ( rLocaleLeft.Language == rLocaleRight.Language &&
50 rLocaleLeft.Country == rLocaleRight.Country &&
51 rLocaleLeft.Variant == rLocaleRight.Variant );
52 return bRet;
55 ManageLanguageDialog::ManageLanguageDialog(weld::Window* pParent, std::shared_ptr<LocalizationMgr> xLMgr)
56 : GenericDialogController(pParent, u"modules/BasicIDE/ui/managelanguages.ui"_ustr, u"ManageLanguagesDialog"_ustr)
57 , m_xLocalizationMgr(std::move(xLMgr))
58 , m_sDefLangStr(IDEResId(RID_STR_DEF_LANG))
59 , m_sCreateLangStr(IDEResId(RID_STR_CREATE_LANG))
60 , m_xLanguageLB(m_xBuilder->weld_tree_view(u"treeview"_ustr))
61 , m_xAddPB(m_xBuilder->weld_button(u"add"_ustr))
62 , m_xDeletePB(m_xBuilder->weld_button(u"delete"_ustr))
63 , m_xMakeDefPB(m_xBuilder->weld_button(u"default"_ustr))
65 m_xLanguageLB->set_size_request(m_xLanguageLB->get_approximate_digit_width() * 42,
66 m_xLanguageLB->get_height_rows(10));
68 Init();
69 FillLanguageBox();
70 SelectHdl( *m_xLanguageLB );
73 ManageLanguageDialog::~ManageLanguageDialog()
75 ClearLanguageBox();
78 void ManageLanguageDialog::Init()
80 // get current IDE
81 Shell* pShell = GetShell();
82 const OUString& sLibName = pShell->GetCurLibName();
83 // set dialog title with library name
84 OUString sText = m_xDialog->get_title();
85 sText = sText.replaceAll("$1", sLibName);
86 m_xDialog->set_title(sText);
87 // set handler
88 m_xAddPB->connect_clicked( LINK( this, ManageLanguageDialog, AddHdl ) );
89 m_xDeletePB->connect_clicked( LINK( this, ManageLanguageDialog, DeleteHdl ) );
90 m_xMakeDefPB->connect_clicked( LINK( this, ManageLanguageDialog, MakeDefHdl ) );
91 m_xLanguageLB->connect_selection_changed(LINK(this, ManageLanguageDialog, SelectHdl));
93 m_xLanguageLB->set_selection_mode(SelectionMode::Multiple);
96 void ManageLanguageDialog::FillLanguageBox()
98 DBG_ASSERT( m_xLocalizationMgr, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
100 if ( m_xLocalizationMgr->isLibraryLocalized() )
102 Locale aDefaultLocale = m_xLocalizationMgr->getStringResourceManager()->getDefaultLocale();
103 for (auto& rLocale : m_xLocalizationMgr->getStringResourceManager()->getLocales())
105 bool bIsDefault = localesAreEqual(aDefaultLocale, rLocale);
106 LanguageType eLangType = LanguageTag::convertToLanguageType(rLocale);
107 OUString sLanguage = SvtLanguageTable::GetLanguageString( eLangType );
108 if ( bIsDefault )
110 sLanguage += " " + m_sDefLangStr;
112 LanguageEntry* pEntry = new LanguageEntry(rLocale, bIsDefault);
113 m_xLanguageLB->append(weld::toId(pEntry), sLanguage);
116 else
117 m_xLanguageLB->append_text(m_sCreateLangStr);
120 void ManageLanguageDialog::ClearLanguageBox()
122 const sal_Int32 nCount = m_xLanguageLB->n_children();
123 for (sal_Int32 i = 0; i < nCount; ++i)
125 LanguageEntry* pEntry = weld::fromId<LanguageEntry*>(m_xLanguageLB->get_id(i));
126 delete pEntry;
128 m_xLanguageLB->clear();
131 IMPL_LINK_NOARG(ManageLanguageDialog, AddHdl, weld::Button&, void)
133 auto xDlg = std::make_shared<SetDefaultLanguageDialog>(m_xDialog.get(), m_xLocalizationMgr);
134 weld::DialogController::runAsync(xDlg, [xDlg,this](sal_Int32 nResult)
136 if (!nResult )
137 return;
138 // add new locales
139 Sequence< Locale > aLocaleSeq = xDlg->GetLocales();
140 m_xLocalizationMgr->handleAddLocales( aLocaleSeq );
141 // update listbox
142 ClearLanguageBox();
143 FillLanguageBox();
145 if (SfxBindings* pBindings = GetBindingsPtr())
146 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
150 IMPL_LINK_NOARG(ManageLanguageDialog, DeleteHdl, weld::Button&, void)
152 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(m_xDialog.get(), u"modules/BasicIDE/ui/deletelangdialog.ui"_ustr));
153 std::unique_ptr<weld::MessageDialog> xQBox(xBuilder->weld_message_dialog(u"DeleteLangDialog"_ustr));
154 if (xQBox->run() != RET_OK)
155 return;
157 std::vector<int> aSelection = m_xLanguageLB->get_selected_rows();
158 int nCount = aSelection.size();
159 int nPos = m_xLanguageLB->get_selected_index();
160 // remove locales
161 Sequence< Locale > aLocaleSeq( nCount );
162 auto aLocaleSeqRange = asNonConstRange(aLocaleSeq);
163 for (int i = 0; i < nCount; ++i)
165 const sal_Int32 nSelPos = aSelection[i];
166 LanguageEntry* pEntry = weld::fromId<LanguageEntry*>(m_xLanguageLB->get_id(nSelPos));
167 if ( pEntry )
168 aLocaleSeqRange[i] = pEntry->m_aLocale;
170 m_xLocalizationMgr->handleRemoveLocales( aLocaleSeq );
171 // update listbox
172 ClearLanguageBox();
173 FillLanguageBox();
174 // reset selection
175 nCount = m_xLanguageLB->n_children();
176 if (nCount <= nPos)
177 nPos = nCount - 1;
178 m_xLanguageLB->select(nPos);
179 SelectHdl( *m_xLanguageLB );
182 IMPL_LINK_NOARG(ManageLanguageDialog, MakeDefHdl, weld::Button&, void)
184 const sal_Int32 nPos = m_xLanguageLB->get_selected_index();
185 LanguageEntry* pSelectEntry = weld::fromId<LanguageEntry*>(m_xLanguageLB->get_id(nPos));
186 if (pSelectEntry && !pSelectEntry->m_bIsDefault)
188 // set new default entry
189 m_xLocalizationMgr->handleSetDefaultLocale( pSelectEntry->m_aLocale );
190 // update Listbox
191 ClearLanguageBox();
192 FillLanguageBox();
193 // reset selection
194 m_xLanguageLB->select(nPos);
195 SelectHdl( *m_xLanguageLB );
199 IMPL_LINK_NOARG(ManageLanguageDialog, SelectHdl, weld::TreeView&, void)
201 const sal_Int32 nCount = m_xLanguageLB->n_children();
202 bool bEmpty = ( !nCount ||
203 m_xLanguageLB->find_text(m_sCreateLangStr) != -1 );
204 bool bSelect = ( m_xLanguageLB->get_selected_index() != -1 );
205 bool bEnable = !bEmpty && bSelect;
207 m_xDeletePB->set_sensitive(bEnable);
208 m_xMakeDefPB->set_sensitive(bEnable && nCount > 1 && m_xLanguageLB->count_selected_rows() == 1);
211 // class SetDefaultLanguageDialog -----------------------------------------------
213 SetDefaultLanguageDialog::SetDefaultLanguageDialog(weld::Window* pParent, std::shared_ptr<LocalizationMgr> xLMgr)
214 : GenericDialogController(pParent, u"modules/BasicIDE/ui/defaultlanguage.ui"_ustr, u"DefaultLanguageDialog"_ustr)
215 , m_xLocalizationMgr(std::move(xLMgr))
216 , m_xLanguageFT(m_xBuilder->weld_label(u"defaultlabel"_ustr))
217 , m_xLanguageLB(m_xBuilder->weld_tree_view(u"entries"_ustr))
218 , m_xCheckLangFT(m_xBuilder->weld_label(u"checkedlabel"_ustr))
219 , m_xCheckLangLB(m_xBuilder->weld_tree_view(u"checkedentries"_ustr))
220 , m_xDefinedFT(m_xBuilder->weld_label(u"defined"_ustr))
221 , m_xAddedFT(m_xBuilder->weld_label(u"added"_ustr))
222 , m_xAltTitle(m_xBuilder->weld_label(u"alttitle"_ustr))
223 , m_xLanguageCB(new SvxLanguageBox(m_xBuilder->weld_combo_box(u"hidden"_ustr)))
225 m_xLanguageLB->set_size_request(-1, m_xLanguageLB->get_height_rows(10));
226 m_xCheckLangLB->set_size_request(-1, m_xCheckLangLB->get_height_rows(10));
227 m_xCheckLangLB->enable_toggle_buttons(weld::ColumnToggleType::Check);
229 if (m_xLocalizationMgr->isLibraryLocalized())
231 // change to "Add Interface Language" mode
232 m_xLanguageLB->hide();
233 m_xCheckLangLB->show();
234 m_xDialog->set_title(m_xAltTitle->get_label());
235 m_xLanguageFT->hide();
236 m_xCheckLangFT->show();
237 m_xDefinedFT->hide();
238 m_xAddedFT->show();
241 FillLanguageBox();
244 SetDefaultLanguageDialog::~SetDefaultLanguageDialog()
248 void SetDefaultLanguageDialog::FillLanguageBox()
250 // fill list with all languages
251 m_xLanguageCB->SetLanguageList(SvxLanguageListFlags::ALL, false);
253 if (m_xLocalizationMgr->isLibraryLocalized())
255 // remove the already localized languages
256 for (auto& rLocale : m_xLocalizationMgr->getStringResourceManager()->getLocales())
257 m_xLanguageCB->remove_id(LanguageTag::convertToLanguageType(rLocale));
259 // fill checklistbox if not in default mode
260 const sal_Int32 nCountLang = m_xLanguageCB->get_count();
261 for (sal_Int32 j = 0; j < nCountLang; ++j)
263 LanguageType eLang = m_xLanguageCB->get_id(j);
264 m_xCheckLangLB->append();
265 const int nRow = m_xCheckLangLB->n_children() - 1;
266 m_xCheckLangLB->set_toggle(nRow, TRISTATE_FALSE);
267 m_xCheckLangLB->set_text(nRow, m_xLanguageCB->get_text(j), 0);
268 m_xCheckLangLB->set_id(nRow, OUString::number(eLang.get()));
270 m_xLanguageCB.reset();
271 m_xLanguageLB.reset();
273 else
275 const sal_Int32 nCountLang = m_xLanguageCB->get_count();
276 for (sal_Int32 j = 0; j < nCountLang; ++j)
278 LanguageType eLang = m_xLanguageCB->get_id(j);
279 m_xLanguageLB->append(OUString::number(eLang.get()), m_xLanguageCB->get_text(j));
281 m_xLanguageCB.reset();
283 // preselect current UI language
284 m_xLanguageLB->select_id(OUString::number(Application::GetSettings().GetUILanguageTag().getLanguageType().get()));
288 Sequence< Locale > SetDefaultLanguageDialog::GetLocales() const
290 bool bNotLocalized = !m_xLocalizationMgr->isLibraryLocalized();
291 if (bNotLocalized)
293 LanguageType eType(m_xLanguageLB->get_selected_id().toUInt32());
294 return {LanguageTag(eType).getLocale()};
296 std::vector<Locale> aLocaleSeq;
297 const sal_Int32 nCount = m_xCheckLangLB->n_children();
298 for (sal_Int32 i = 0; i < nCount; ++i)
300 if (m_xCheckLangLB->get_toggle(i) == TRISTATE_TRUE)
302 LanguageType eType(m_xCheckLangLB->get_id(i).toUInt32());
303 aLocaleSeq.push_back(LanguageTag::convertToLocale(eType));
306 return comphelper::containerToSequence(aLocaleSeq);
309 } // namespace basctl
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */