bump product version to 5.0.4.1
[LibreOffice.git] / basctl / source / dlged / managelang.cxx
blobbde64e28cf6e9c11353c1ddf663d2a9ed4df0c32
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 "dlgresid.hrc"
28 #include "helpid.hrc"
30 #include <com/sun/star/i18n/Boundary.hpp>
31 #include <com/sun/star/i18n/WordType.hpp>
32 #include <com/sun/star/i18n/XBreakIterator.hpp>
33 #include <editeng/unolingu.hxx>
34 #include <sfx2/bindings.hxx>
35 #include <svtools/langtab.hxx>
36 #include <svx/langbox.hxx>
37 #include <vcl/unohelp.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vcl/layout.hxx>
40 #include <vcl/settings.hxx>
42 namespace basctl
45 using namespace ::com::sun::star::i18n;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::resource;
48 using namespace ::com::sun::star::uno;
50 bool localesAreEqual( const Locale& rLocaleLeft, const Locale& rLocaleRight )
52 bool bRet = ( rLocaleLeft.Language.equals( rLocaleRight.Language ) &&
53 rLocaleLeft.Country.equals( rLocaleRight.Country ) &&
54 rLocaleLeft.Variant.equals( rLocaleRight.Variant ) );
55 return bRet;
58 ManageLanguageDialog::ManageLanguageDialog(vcl::Window* pParent, boost::shared_ptr<LocalizationMgr> xLMgr)
59 : ModalDialog(pParent, "ManageLanguagesDialog", "modules/BasicIDE/ui/managelanguages.ui")
60 , m_xLocalizationMgr(xLMgr)
61 , m_sDefLangStr(IDE_RESSTR(RID_STR_DEF_LANG))
62 , m_sCreateLangStr(IDE_RESSTR(RID_STR_CREATE_LANG))
64 get(m_pLanguageLB, "treeview");
65 m_pLanguageLB->set_height_request(m_pLanguageLB->GetTextHeight() * 10);
66 m_pLanguageLB->set_width_request(m_pLanguageLB->approximate_char_width() * 50);
67 get(m_pAddPB, "add");
68 get(m_pDeletePB, "delete");
69 get(m_pMakeDefPB, "default");
71 Init();
72 FillLanguageBox();
73 SelectHdl( NULL );
76 ManageLanguageDialog::~ManageLanguageDialog()
78 disposeOnce();
81 void ManageLanguageDialog::dispose()
83 ClearLanguageBox();
84 m_pLanguageLB.clear();
85 m_pAddPB.clear();
86 m_pDeletePB.clear();
87 m_pMakeDefPB.clear();
88 ModalDialog::dispose();
91 void ManageLanguageDialog::Init()
93 // get current IDE
94 Shell* pShell = GetShell();
95 OUString sLibName = pShell->GetCurLibName();
96 // set dialog title with library name
97 OUString sText = GetText();
98 sText = sText.replaceAll("$1", sLibName);
99 SetText( sText );
100 // set handler
101 m_pAddPB->SetClickHdl( LINK( this, ManageLanguageDialog, AddHdl ) );
102 m_pDeletePB->SetClickHdl( LINK( this, ManageLanguageDialog, DeleteHdl ) );
103 m_pMakeDefPB->SetClickHdl( LINK( this, ManageLanguageDialog, MakeDefHdl ) );
104 m_pLanguageLB->SetSelectHdl( LINK( this, ManageLanguageDialog, SelectHdl ) );
106 m_pLanguageLB->EnableMultiSelection( true );
109 void ManageLanguageDialog::FillLanguageBox()
111 DBG_ASSERT( m_xLocalizationMgr, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
113 if ( m_xLocalizationMgr->isLibraryLocalized() )
115 Locale aDefaultLocale = m_xLocalizationMgr->getStringResourceManager()->getDefaultLocale();
116 Sequence< Locale > aLocaleSeq = m_xLocalizationMgr->getStringResourceManager()->getLocales();
117 const Locale* pLocale = aLocaleSeq.getConstArray();
118 sal_Int32 i, nCount = aLocaleSeq.getLength();
119 for ( i = 0; i < nCount; ++i )
121 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
122 LanguageType eLangType = LanguageTag::convertToLanguageType( pLocale[i] );
123 OUString sLanguage = SvtLanguageTable::GetLanguageString( eLangType );
124 if ( bIsDefault )
126 sLanguage += " " + m_sDefLangStr;
128 sal_uInt16 nPos = m_pLanguageLB->InsertEntry( sLanguage );
129 m_pLanguageLB->SetEntryData( nPos, new LanguageEntry( sLanguage, pLocale[i], bIsDefault ) );
132 else
133 m_pLanguageLB->InsertEntry( m_sCreateLangStr );
136 void ManageLanguageDialog::ClearLanguageBox()
138 sal_uInt16 i, nCount = m_pLanguageLB->GetEntryCount();
139 for ( i = 0; i < nCount; ++i )
141 LanguageEntry* pEntry = static_cast<LanguageEntry*>(m_pLanguageLB->GetEntryData(i));
142 delete pEntry;
144 m_pLanguageLB->Clear();
147 IMPL_LINK_NOARG(ManageLanguageDialog, AddHdl)
149 ScopedVclPtrInstance< SetDefaultLanguageDialog > aDlg( this, m_xLocalizationMgr );
150 if ( RET_OK == aDlg->Execute() )
152 if (!m_xLocalizationMgr->isLibraryLocalized())
154 SAL_WARN("basctl.basicide", "Adding langs to non-localized library tdf#93077");
156 else
158 // add new locales
159 Sequence< Locale > aLocaleSeq = aDlg->GetLocales();
160 m_xLocalizationMgr->handleAddLocales( aLocaleSeq );
161 // update listbox
162 ClearLanguageBox();
163 FillLanguageBox();
166 if (SfxBindings* pBindings = GetBindingsPtr())
167 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
169 return 1;
172 IMPL_LINK_NOARG(ManageLanguageDialog, DeleteHdl)
174 ScopedVclPtrInstance< MessageDialog > aQBox(this, "DeleteLangDialog", "modules/BasicIDE/ui/deletelangdialog.ui");
175 if ( aQBox->Execute() == RET_OK )
177 sal_uInt16 i, nCount = m_pLanguageLB->GetSelectEntryCount();
178 sal_uInt16 nPos = m_pLanguageLB->GetSelectEntryPos();
179 // remove locales
180 Sequence< Locale > aLocaleSeq( nCount );
181 for ( i = 0; i < nCount; ++i )
183 sal_uInt16 nSelPos = m_pLanguageLB->GetSelectEntryPos(i);
184 LanguageEntry* pEntry = static_cast<LanguageEntry*>(m_pLanguageLB->GetEntryData( nSelPos ));
185 if ( pEntry )
186 aLocaleSeq[i] = pEntry->m_aLocale;
188 m_xLocalizationMgr->handleRemoveLocales( aLocaleSeq );
189 // update listbox
190 ClearLanguageBox();
191 FillLanguageBox();
192 // reset selection
193 nCount = m_pLanguageLB->GetEntryCount();
194 if ( nCount <= nPos )
195 nPos = nCount - 1;
196 m_pLanguageLB->SelectEntryPos( nPos );
197 SelectHdl( NULL );
199 return 1;
202 IMPL_LINK_NOARG(ManageLanguageDialog, MakeDefHdl)
204 sal_uInt16 nPos = m_pLanguageLB->GetSelectEntryPos();
205 LanguageEntry* pSelectEntry = static_cast<LanguageEntry*>(m_pLanguageLB->GetEntryData( nPos ));
206 if ( pSelectEntry && !pSelectEntry->m_bIsDefault )
208 // set new default entry
209 m_xLocalizationMgr->handleSetDefaultLocale( pSelectEntry->m_aLocale );
210 // update Listbox
211 ClearLanguageBox();
212 FillLanguageBox();
213 // reset selection
214 m_pLanguageLB->SelectEntryPos( nPos );
215 SelectHdl( NULL );
218 return 1;
221 IMPL_LINK_NOARG(ManageLanguageDialog, SelectHdl)
223 sal_uInt16 nCount = m_pLanguageLB->GetEntryCount();
224 bool bEmpty = ( !nCount ||
225 m_pLanguageLB->GetEntryPos( m_sCreateLangStr ) != LISTBOX_ENTRY_NOTFOUND );
226 bool bSelect = ( m_pLanguageLB->GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND );
227 bool bEnable = !bEmpty && bSelect;
229 m_pDeletePB->Enable(bEnable);
230 m_pMakeDefPB->Enable(bEnable && nCount > 1 && m_pLanguageLB->GetSelectEntryCount() == 1);
232 return 1;
235 // class SetDefaultLanguageDialog -----------------------------------------------
237 SetDefaultLanguageDialog::SetDefaultLanguageDialog(vcl::Window* pParent, boost::shared_ptr<LocalizationMgr> xLMgr)
238 : ModalDialog(pParent, "DefaultLanguageDialog", "modules/BasicIDE/ui/defaultlanguage.ui")
239 , m_pCheckLangLB(NULL)
240 , m_xLocalizationMgr(xLMgr)
242 get(m_pLanguageLB, "entries");
243 get(m_pCheckLangLB, "checkedentries");
244 get(m_pDefinedFT, "defined");
245 get(m_pAddedFT, "added");
246 get(m_pLanguageFT, "defaultlabel");
247 get(m_pCheckLangFT, "checkedlabel");
249 m_pLanguageLB->set_height_request(m_pLanguageLB->GetTextHeight() * 10);
250 m_pCheckLangLB->set_height_request(m_pCheckLangLB->GetTextHeight() * 10);
252 if (m_xLocalizationMgr->isLibraryLocalized())
254 // change to "Add Interface Language" mode
255 m_pLanguageLB->Hide();
256 m_pCheckLangLB->Show();
257 SetText(get<FixedText>("alttitle")->GetText());
258 m_pLanguageFT->Hide();
259 m_pCheckLangFT->Show();
260 m_pDefinedFT->Hide();
261 m_pAddedFT->Show();
264 FillLanguageBox();
267 SetDefaultLanguageDialog::~SetDefaultLanguageDialog()
269 disposeOnce();
272 void SetDefaultLanguageDialog::dispose()
274 m_pLanguageFT.clear();
275 m_pLanguageLB.clear();
276 m_pCheckLangFT.clear();
277 m_pCheckLangLB.clear();
278 m_pDefinedFT.clear();
279 m_pAddedFT.clear();
280 ModalDialog::dispose();
283 void SetDefaultLanguageDialog::FillLanguageBox()
285 // fill list with all languages
286 m_pLanguageLB->SetLanguageList( SvxLanguageListFlags::ALL, false );
288 if ( m_xLocalizationMgr->isLibraryLocalized() )
290 // remove the already localized languages
291 Sequence< Locale > aLocaleSeq = m_xLocalizationMgr->getStringResourceManager()->getLocales();
292 const Locale* pLocale = aLocaleSeq.getConstArray();
293 sal_Int32 i, nCount = aLocaleSeq.getLength();
294 for ( i = 0; i < nCount; ++i )
295 m_pLanguageLB->RemoveLanguage( LanguageTag::convertToLanguageType( pLocale[i] ) );
297 // fill checklistbox if not in default mode
298 sal_uInt16 j, nCount_ = m_pLanguageLB->GetEntryCount();
299 for ( j = 0; j < nCount_; ++j )
301 m_pCheckLangLB->InsertEntry(
302 m_pLanguageLB->GetEntry(j), LISTBOX_APPEND, m_pLanguageLB->GetEntryData(j) );
304 m_pLanguageLB = NULL;
306 else
307 // preselect current UI language
308 m_pLanguageLB->SelectLanguage( Application::GetSettings().GetUILanguageTag().getLanguageType() );
311 Sequence< Locale > SetDefaultLanguageDialog::GetLocales() const
313 bool bNotLocalized = !m_xLocalizationMgr->isLibraryLocalized();
314 sal_Int32 nSize = bNotLocalized ? 1 : m_pCheckLangLB->GetCheckedEntryCount();
315 Sequence< Locale > aLocaleSeq( nSize );
316 if ( bNotLocalized )
318 aLocaleSeq[0] = LanguageTag( m_pLanguageLB->GetSelectLanguage() ).getLocale();
320 else
322 sal_uInt16 i, nCount = static_cast< sal_uInt16 >( m_pCheckLangLB->GetEntryCount() );
323 sal_Int32 j = 0;
324 for ( i = 0; i < nCount; ++i )
326 if ( m_pCheckLangLB->IsChecked(i) )
328 LanguageType eType = LanguageType( reinterpret_cast<sal_uLong>(m_pCheckLangLB->GetEntryData(i)) );
329 aLocaleSeq[j++] = LanguageTag::convertToLocale( eType );
332 DBG_ASSERT( nSize == j, "SetDefaultLanguageDialog::GetLocales(): invalid indexes" );
334 return aLocaleSeq;
337 } // namespace basctl
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */