Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / basctl / source / dlged / managelang.cxx
blob757b98af872eabb9b1249cd023ae49d05978ae6c
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 <com/sun/star/i18n/Boundary.hpp>
30 #include <com/sun/star/i18n/WordType.hpp>
31 #include <com/sun/star/i18n/XBreakIterator.hpp>
32 #include <editeng/unolingu.hxx>
33 #include <sfx2/bindings.hxx>
34 #include <svtools/langtab.hxx>
35 #include <svx/langbox.hxx>
36 #include <vcl/unohelp.hxx>
37 #include <vcl/svapp.hxx>
38 #include <vcl/weld.hxx>
39 #include <vcl/settings.hxx>
41 namespace basctl
44 using namespace ::com::sun::star::i18n;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::resource;
47 using namespace ::com::sun::star::uno;
49 bool localesAreEqual( const Locale& rLocaleLeft, const Locale& rLocaleRight )
51 bool bRet = ( rLocaleLeft.Language == rLocaleRight.Language &&
52 rLocaleLeft.Country == rLocaleRight.Country &&
53 rLocaleLeft.Variant == rLocaleRight.Variant );
54 return bRet;
57 ManageLanguageDialog::ManageLanguageDialog(vcl::Window* pParent, std::shared_ptr<LocalizationMgr> const & xLMgr)
58 : ModalDialog(pParent, "ManageLanguagesDialog", "modules/BasicIDE/ui/managelanguages.ui")
59 , m_xLocalizationMgr(xLMgr)
60 , m_sDefLangStr(IDEResId(RID_STR_DEF_LANG))
61 , m_sCreateLangStr(IDEResId(RID_STR_CREATE_LANG))
63 get(m_pLanguageLB, "treeview");
64 m_pLanguageLB->set_height_request(m_pLanguageLB->GetTextHeight() * 10);
65 m_pLanguageLB->set_width_request(m_pLanguageLB->approximate_char_width() * 50);
66 get(m_pAddPB, "add");
67 get(m_pDeletePB, "delete");
68 get(m_pMakeDefPB, "default");
70 Init();
71 FillLanguageBox();
72 SelectHdl( *m_pLanguageLB );
75 ManageLanguageDialog::~ManageLanguageDialog()
77 disposeOnce();
80 void ManageLanguageDialog::dispose()
82 ClearLanguageBox();
83 m_pLanguageLB.clear();
84 m_pAddPB.clear();
85 m_pDeletePB.clear();
86 m_pMakeDefPB.clear();
87 ModalDialog::dispose();
90 void ManageLanguageDialog::Init()
92 // get current IDE
93 Shell* pShell = GetShell();
94 OUString sLibName = pShell->GetCurLibName();
95 // set dialog title with library name
96 OUString sText = GetText();
97 sText = sText.replaceAll("$1", sLibName);
98 SetText( sText );
99 // set handler
100 m_pAddPB->SetClickHdl( LINK( this, ManageLanguageDialog, AddHdl ) );
101 m_pDeletePB->SetClickHdl( LINK( this, ManageLanguageDialog, DeleteHdl ) );
102 m_pMakeDefPB->SetClickHdl( LINK( this, ManageLanguageDialog, MakeDefHdl ) );
103 m_pLanguageLB->SetSelectHdl( LINK( this, ManageLanguageDialog, SelectHdl ) );
105 m_pLanguageLB->EnableMultiSelection( true );
108 void ManageLanguageDialog::FillLanguageBox()
110 DBG_ASSERT( m_xLocalizationMgr, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
112 if ( m_xLocalizationMgr->isLibraryLocalized() )
114 Locale aDefaultLocale = m_xLocalizationMgr->getStringResourceManager()->getDefaultLocale();
115 Sequence< Locale > aLocaleSeq = m_xLocalizationMgr->getStringResourceManager()->getLocales();
116 const Locale* pLocale = aLocaleSeq.getConstArray();
117 sal_Int32 i, nCount = aLocaleSeq.getLength();
118 for ( i = 0; i < nCount; ++i )
120 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
121 LanguageType eLangType = LanguageTag::convertToLanguageType( pLocale[i] );
122 OUString sLanguage = SvtLanguageTable::GetLanguageString( eLangType );
123 if ( bIsDefault )
125 sLanguage += " " + m_sDefLangStr;
127 const sal_Int32 nPos = m_pLanguageLB->InsertEntry( sLanguage );
128 m_pLanguageLB->SetEntryData( nPos, new LanguageEntry( pLocale[i], bIsDefault ) );
131 else
132 m_pLanguageLB->InsertEntry( m_sCreateLangStr );
135 void ManageLanguageDialog::ClearLanguageBox()
137 const sal_Int32 nCount = m_pLanguageLB->GetEntryCount();
138 for ( sal_Int32 i = 0; i < nCount; ++i )
140 LanguageEntry* pEntry = static_cast<LanguageEntry*>(m_pLanguageLB->GetEntryData(i));
141 delete pEntry;
143 m_pLanguageLB->Clear();
146 IMPL_LINK_NOARG(ManageLanguageDialog, AddHdl, Button*, void)
148 ScopedVclPtrInstance< SetDefaultLanguageDialog > aDlg( this, m_xLocalizationMgr );
149 if ( aDlg->Execute() == RET_OK )
151 // add new locales
152 Sequence< Locale > aLocaleSeq = aDlg->GetLocales();
153 m_xLocalizationMgr->handleAddLocales( aLocaleSeq );
154 // update listbox
155 ClearLanguageBox();
156 FillLanguageBox();
158 if (SfxBindings* pBindings = GetBindingsPtr())
159 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
163 IMPL_LINK_NOARG(ManageLanguageDialog, DeleteHdl, Button*, void)
165 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetFrameWeld(), "modules/BasicIDE/ui/deletelangdialog.ui"));
166 std::unique_ptr<weld::MessageDialog> xQBox(xBuilder->weld_message_dialog("DeleteLangDialog"));
167 if (xQBox->run() == RET_OK)
169 sal_Int32 nCount = m_pLanguageLB->GetSelectedEntryCount();
170 sal_Int32 nPos = m_pLanguageLB->GetSelectedEntryPos();
171 // remove locales
172 Sequence< Locale > aLocaleSeq( nCount );
173 for ( sal_Int32 i = 0; i < nCount; ++i )
175 const sal_Int32 nSelPos = m_pLanguageLB->GetSelectedEntryPos(i);
176 LanguageEntry* pEntry = static_cast<LanguageEntry*>(m_pLanguageLB->GetEntryData( nSelPos ));
177 if ( pEntry )
178 aLocaleSeq[i] = pEntry->m_aLocale;
180 m_xLocalizationMgr->handleRemoveLocales( aLocaleSeq );
181 // update listbox
182 ClearLanguageBox();
183 FillLanguageBox();
184 // reset selection
185 nCount = m_pLanguageLB->GetEntryCount();
186 if ( nCount <= nPos )
187 nPos = nCount - 1;
188 m_pLanguageLB->SelectEntryPos( nPos );
189 SelectHdl( *m_pLanguageLB );
193 IMPL_LINK_NOARG(ManageLanguageDialog, MakeDefHdl, Button*, void)
195 const sal_Int32 nPos = m_pLanguageLB->GetSelectedEntryPos();
196 LanguageEntry* pSelectEntry = static_cast<LanguageEntry*>(m_pLanguageLB->GetEntryData( nPos ));
197 if ( pSelectEntry && !pSelectEntry->m_bIsDefault )
199 // set new default entry
200 m_xLocalizationMgr->handleSetDefaultLocale( pSelectEntry->m_aLocale );
201 // update Listbox
202 ClearLanguageBox();
203 FillLanguageBox();
204 // reset selection
205 m_pLanguageLB->SelectEntryPos( nPos );
206 SelectHdl( *m_pLanguageLB );
210 IMPL_LINK_NOARG(ManageLanguageDialog, SelectHdl, ListBox&, void)
212 const sal_Int32 nCount = m_pLanguageLB->GetEntryCount();
213 bool bEmpty = ( !nCount ||
214 m_pLanguageLB->GetEntryPos( m_sCreateLangStr ) != LISTBOX_ENTRY_NOTFOUND );
215 bool bSelect = ( m_pLanguageLB->GetSelectedEntryPos() != LISTBOX_ENTRY_NOTFOUND );
216 bool bEnable = !bEmpty && bSelect;
218 m_pDeletePB->Enable(bEnable);
219 m_pMakeDefPB->Enable(bEnable && nCount > 1 && m_pLanguageLB->GetSelectedEntryCount() == 1);
222 // class SetDefaultLanguageDialog -----------------------------------------------
224 SetDefaultLanguageDialog::SetDefaultLanguageDialog(vcl::Window* pParent, std::shared_ptr<LocalizationMgr> const & xLMgr)
225 : ModalDialog(pParent, "DefaultLanguageDialog", "modules/BasicIDE/ui/defaultlanguage.ui")
226 , m_pCheckLangLB(nullptr)
227 , m_xLocalizationMgr(xLMgr)
229 get(m_pLanguageLB, "entries");
230 get(m_pCheckLangLB, "checkedentries");
231 get(m_pDefinedFT, "defined");
232 get(m_pAddedFT, "added");
233 get(m_pLanguageFT, "defaultlabel");
234 get(m_pCheckLangFT, "checkedlabel");
236 m_pLanguageLB->set_height_request(m_pLanguageLB->GetTextHeight() * 10);
237 m_pCheckLangLB->set_height_request(m_pCheckLangLB->GetTextHeight() * 10);
239 if (m_xLocalizationMgr->isLibraryLocalized())
241 // change to "Add Interface Language" mode
242 m_pLanguageLB->Hide();
243 m_pCheckLangLB->Show();
244 SetText(get<FixedText>("alttitle")->GetText());
245 m_pLanguageFT->Hide();
246 m_pCheckLangFT->Show();
247 m_pDefinedFT->Hide();
248 m_pAddedFT->Show();
251 FillLanguageBox();
254 SetDefaultLanguageDialog::~SetDefaultLanguageDialog()
256 disposeOnce();
259 void SetDefaultLanguageDialog::dispose()
261 m_pLanguageFT.clear();
262 m_pLanguageLB.clear();
263 m_pCheckLangFT.clear();
264 m_pCheckLangLB.clear();
265 m_pDefinedFT.clear();
266 m_pAddedFT.clear();
267 ModalDialog::dispose();
270 void SetDefaultLanguageDialog::FillLanguageBox()
272 // fill list with all languages
273 m_pLanguageLB->SetLanguageList( SvxLanguageListFlags::ALL, false );
275 if ( m_xLocalizationMgr->isLibraryLocalized() )
277 // remove the already localized languages
278 Sequence< Locale > aLocaleSeq = m_xLocalizationMgr->getStringResourceManager()->getLocales();
279 const Locale* pLocale = aLocaleSeq.getConstArray();
280 const sal_Int32 nCountLoc = aLocaleSeq.getLength();
281 for ( sal_Int32 i = 0; i < nCountLoc; ++i )
282 m_pLanguageLB->RemoveLanguage( LanguageTag::convertToLanguageType( pLocale[i] ) );
284 // fill checklistbox if not in default mode
285 const sal_Int32 nCountLang = m_pLanguageLB->GetEntryCount();
286 for ( sal_Int32 j = 0; j < nCountLang; ++j )
288 m_pCheckLangLB->InsertEntry(
289 m_pLanguageLB->GetEntry(j), LISTBOX_APPEND, m_pLanguageLB->GetEntryData(j) );
291 m_pLanguageLB = nullptr;
293 else
294 // preselect current UI language
295 m_pLanguageLB->SelectLanguage( Application::GetSettings().GetUILanguageTag().getLanguageType() );
298 Sequence< Locale > SetDefaultLanguageDialog::GetLocales() const
300 bool bNotLocalized = !m_xLocalizationMgr->isLibraryLocalized();
301 sal_Int32 nSize = bNotLocalized ? 1 : m_pCheckLangLB->GetCheckedEntryCount();
302 Sequence< Locale > aLocaleSeq( nSize );
303 if ( bNotLocalized )
305 aLocaleSeq[0] = LanguageTag( m_pLanguageLB->GetSelectedLanguage() ).getLocale();
307 else
309 const sal_Int32 nCount = m_pCheckLangLB->GetEntryCount();
310 sal_Int32 j = 0;
311 for ( sal_Int32 i = 0; i < nCount; ++i )
313 if ( m_pCheckLangLB->IsChecked(i) )
315 LanguageType eType = LanguageType( reinterpret_cast<sal_uLong>(m_pCheckLangLB->GetEntryData(i)) );
316 aLocaleSeq[j++] = LanguageTag::convertToLocale( eType );
319 DBG_ASSERT( nSize == j, "SetDefaultLanguageDialog::GetLocales(): invalid indexes" );
321 return aLocaleSeq;
324 } // namespace basctl
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */