Bump version to 4.3-4
[LibreOffice.git] / basctl / source / dlged / managelang.cxx
blob175e464d32270e73710080e9ec9e84541ee598c3
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/msgbox.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(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_sDeleteStr(IDE_RESSTR(RID_STR_DELETE))
63 , m_sCreateLangStr(IDE_RESSTR(RID_STR_CREATE_LANG))
65 get(m_pLanguageLB, "treeview");
66 m_pLanguageLB->set_height_request(m_pLanguageLB->GetTextHeight() * 10);
67 m_pLanguageLB->set_width_request(m_pLanguageLB->approximate_char_width() * 50);
68 get(m_pAddPB, "add");
69 get(m_pDeletePB, "delete");
70 get(m_pMakeDefPB, "default");
72 Init();
73 FillLanguageBox();
74 SelectHdl( NULL );
77 ManageLanguageDialog::~ManageLanguageDialog()
79 ClearLanguageBox();
82 void ManageLanguageDialog::Init()
84 // get current IDE
85 Shell* pShell = GetShell();
86 OUString sLibName = pShell->GetCurLibName();
87 // set dialog title with library name
88 OUString sText = GetText();
89 sText = sText.replaceAll("$1", sLibName);
90 SetText( sText );
91 // set handler
92 m_pAddPB->SetClickHdl( LINK( this, ManageLanguageDialog, AddHdl ) );
93 m_pDeletePB->SetClickHdl( LINK( this, ManageLanguageDialog, DeleteHdl ) );
94 m_pMakeDefPB->SetClickHdl( LINK( this, ManageLanguageDialog, MakeDefHdl ) );
95 m_pLanguageLB->SetSelectHdl( LINK( this, ManageLanguageDialog, SelectHdl ) );
97 m_pLanguageLB->EnableMultiSelection( true );
100 void ManageLanguageDialog::FillLanguageBox()
102 DBG_ASSERT( m_xLocalizationMgr, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
104 if ( m_xLocalizationMgr->isLibraryLocalized() )
106 Locale aDefaultLocale = m_xLocalizationMgr->getStringResourceManager()->getDefaultLocale();
107 Sequence< Locale > aLocaleSeq = m_xLocalizationMgr->getStringResourceManager()->getLocales();
108 const Locale* pLocale = aLocaleSeq.getConstArray();
109 sal_Int32 i, nCount = aLocaleSeq.getLength();
110 for ( i = 0; i < nCount; ++i )
112 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
113 LanguageType eLangType = LanguageTag::convertToLanguageType( pLocale[i] );
114 OUString sLanguage = SvtLanguageTable::GetLanguageString( eLangType );
115 if ( bIsDefault )
117 sLanguage += " " + m_sDefLangStr;
119 sal_uInt16 nPos = m_pLanguageLB->InsertEntry( sLanguage );
120 m_pLanguageLB->SetEntryData( nPos, new LanguageEntry( sLanguage, pLocale[i], bIsDefault ) );
123 else
124 m_pLanguageLB->InsertEntry( m_sCreateLangStr );
127 void ManageLanguageDialog::ClearLanguageBox()
129 sal_uInt16 i, nCount = m_pLanguageLB->GetEntryCount();
130 for ( i = 0; i < nCount; ++i )
132 LanguageEntry* pEntry = (LanguageEntry*)( m_pLanguageLB->GetEntryData(i) );
133 delete pEntry;
135 m_pLanguageLB->Clear();
138 IMPL_LINK_NOARG(ManageLanguageDialog, AddHdl)
140 SetDefaultLanguageDialog aDlg( this, m_xLocalizationMgr );
141 if ( RET_OK == aDlg.Execute() )
143 // add new locales
144 Sequence< Locale > aLocaleSeq = aDlg.GetLocales();
145 m_xLocalizationMgr->handleAddLocales( aLocaleSeq );
146 // update listbox
147 ClearLanguageBox();
148 FillLanguageBox();
150 if (SfxBindings* pBindings = GetBindingsPtr())
151 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
153 return 1;
156 IMPL_LINK_NOARG(ManageLanguageDialog, DeleteHdl)
158 QueryBox aQBox( this, IDEResId( RID_QRYBOX_LANGUAGE ) );
159 aQBox.SetButtonText( RET_OK, m_sDeleteStr );
160 if ( aQBox.Execute() == RET_OK )
162 sal_uInt16 i, nCount = m_pLanguageLB->GetSelectEntryCount();
163 sal_uInt16 nPos = m_pLanguageLB->GetSelectEntryPos();
164 // remove locales
165 Sequence< Locale > aLocaleSeq( nCount );
166 for ( i = 0; i < nCount; ++i )
168 sal_uInt16 nSelPos = m_pLanguageLB->GetSelectEntryPos(i);
169 LanguageEntry* pEntry = (LanguageEntry*)( m_pLanguageLB->GetEntryData( nSelPos ) );
170 if ( pEntry )
171 aLocaleSeq[i] = pEntry->m_aLocale;
173 m_xLocalizationMgr->handleRemoveLocales( aLocaleSeq );
174 // update listbox
175 ClearLanguageBox();
176 FillLanguageBox();
177 // reset selection
178 nCount = m_pLanguageLB->GetEntryCount();
179 if ( nCount <= nPos )
180 nPos = nCount - 1;
181 m_pLanguageLB->SelectEntryPos( nPos );
182 SelectHdl( NULL );
184 return 1;
187 IMPL_LINK_NOARG(ManageLanguageDialog, MakeDefHdl)
189 sal_uInt16 nPos = m_pLanguageLB->GetSelectEntryPos();
190 LanguageEntry* pSelectEntry = (LanguageEntry*)( m_pLanguageLB->GetEntryData( nPos ) );
191 if ( pSelectEntry && !pSelectEntry->m_bIsDefault )
193 // set new default entry
194 m_xLocalizationMgr->handleSetDefaultLocale( pSelectEntry->m_aLocale );
195 // update Listbox
196 ClearLanguageBox();
197 FillLanguageBox();
198 // reset selection
199 m_pLanguageLB->SelectEntryPos( nPos );
200 SelectHdl( NULL );
203 return 1;
206 IMPL_LINK_NOARG(ManageLanguageDialog, SelectHdl)
208 sal_uInt16 nCount = m_pLanguageLB->GetEntryCount();
209 bool bEmpty = ( !nCount ||
210 m_pLanguageLB->GetEntryPos( m_sCreateLangStr ) != LISTBOX_ENTRY_NOTFOUND );
211 bool bSelect = ( m_pLanguageLB->GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND );
212 bool bEnable = !bEmpty && bSelect;
214 m_pDeletePB->Enable(bEnable);
215 m_pMakeDefPB->Enable(bEnable && nCount > 1 && m_pLanguageLB->GetSelectEntryCount() == 1);
217 return 1;
220 // class SetDefaultLanguageDialog -----------------------------------------------
222 SetDefaultLanguageDialog::SetDefaultLanguageDialog(Window* pParent, boost::shared_ptr<LocalizationMgr> xLMgr)
223 : ModalDialog(pParent, "DefaultLanguageDialog", "modules/BasicIDE/ui/defaultlanguage.ui")
224 , m_pCheckLangLB(NULL)
225 , m_xLocalizationMgr(xLMgr)
227 get(m_pLanguageLB, "entries");
228 get(m_pCheckLangLB, "checkedentries");
229 get(m_pDefinedFT, "defined");
230 get(m_pAddedFT, "added");
231 get(m_pLanguageFT, "defaultlabel");
232 get(m_pCheckLangFT, "checkedlabel");
234 m_pLanguageLB->set_height_request(m_pLanguageLB->GetTextHeight() * 10);
235 m_pCheckLangLB->set_height_request(m_pCheckLangLB->GetTextHeight() * 10);
237 if (m_xLocalizationMgr->isLibraryLocalized())
239 // change to "Add Interface Language" mode
240 m_pLanguageLB->Hide();
241 m_pCheckLangLB->Show();
242 SetText(get<FixedText>("alttitle")->GetText());
243 m_pLanguageFT->Hide();
244 m_pCheckLangFT->Show();
245 m_pDefinedFT->Hide();
246 m_pAddedFT->Show();
249 FillLanguageBox();
252 void SetDefaultLanguageDialog::FillLanguageBox()
254 // fill list with all languages
255 m_pLanguageLB->SetLanguageList( LANG_LIST_ALL, false );
256 // remove the already localized languages
257 Sequence< Locale > aLocaleSeq = m_xLocalizationMgr->getStringResourceManager()->getLocales();
258 const Locale* pLocale = aLocaleSeq.getConstArray();
259 sal_Int32 i, nCount = aLocaleSeq.getLength();
260 for ( i = 0; i < nCount; ++i )
261 m_pLanguageLB->RemoveLanguage( LanguageTag::convertToLanguageType( pLocale[i] ) );
263 // fill checklistbox if not in default mode
264 if ( m_xLocalizationMgr->isLibraryLocalized() )
266 sal_uInt16 j, nCount_ = m_pLanguageLB->GetEntryCount();
267 for ( j = 0; j < nCount_; ++j )
269 m_pCheckLangLB->InsertEntry(
270 m_pLanguageLB->GetEntry(j), LISTBOX_APPEND, m_pLanguageLB->GetEntryData(j) );
272 m_pLanguageLB = NULL;
274 else
275 // preselect current UI language
276 m_pLanguageLB->SelectLanguage( Application::GetSettings().GetUILanguageTag().getLanguageType() );
279 Sequence< Locale > SetDefaultLanguageDialog::GetLocales() const
281 bool bNotLocalized = !m_xLocalizationMgr->isLibraryLocalized();
282 sal_Int32 nSize = bNotLocalized ? 1 : m_pCheckLangLB->GetCheckedEntryCount();
283 Sequence< Locale > aLocaleSeq( nSize );
284 if ( bNotLocalized )
286 aLocaleSeq[0] = LanguageTag( m_pLanguageLB->GetSelectLanguage() ).getLocale();
288 else
290 sal_uInt16 i, nCount = static_cast< sal_uInt16 >( m_pCheckLangLB->GetEntryCount() );
291 sal_Int32 j = 0;
292 for ( i = 0; i < nCount; ++i )
294 if ( m_pCheckLangLB->IsChecked(i) )
296 LanguageType eType = LanguageType( (sal_uLong)m_pCheckLangLB->GetEntryData(i) );
297 aLocaleSeq[j++] = LanguageTag::convertToLocale( eType );
300 DBG_ASSERT( nSize == j, "SetDefaultLanguageDialog::GetLocales(): invalid indexes" );
302 return aLocaleSeq;
305 } // namespace basctl
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */