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 <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>
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
);
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);
67 get(m_pDeletePB
, "delete");
68 get(m_pMakeDefPB
, "default");
72 SelectHdl( *m_pLanguageLB
);
75 ManageLanguageDialog::~ManageLanguageDialog()
80 void ManageLanguageDialog::dispose()
83 m_pLanguageLB
.clear();
87 ModalDialog::dispose();
90 void ManageLanguageDialog::Init()
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
);
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
);
125 sLanguage
+= " " + m_sDefLangStr
;
127 const sal_Int32 nPos
= m_pLanguageLB
->InsertEntry( sLanguage
);
128 m_pLanguageLB
->SetEntryData( nPos
, new LanguageEntry( pLocale
[i
], bIsDefault
) );
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
));
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
)
152 Sequence
< Locale
> aLocaleSeq
= aDlg
->GetLocales();
153 m_xLocalizationMgr
->handleAddLocales( aLocaleSeq
);
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();
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
));
178 aLocaleSeq
[i
] = pEntry
->m_aLocale
;
180 m_xLocalizationMgr
->handleRemoveLocales( aLocaleSeq
);
185 nCount
= m_pLanguageLB
->GetEntryCount();
186 if ( nCount
<= nPos
)
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
);
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();
254 SetDefaultLanguageDialog::~SetDefaultLanguageDialog()
259 void SetDefaultLanguageDialog::dispose()
261 m_pLanguageFT
.clear();
262 m_pLanguageLB
.clear();
263 m_pCheckLangFT
.clear();
264 m_pCheckLangLB
.clear();
265 m_pDefinedFT
.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;
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
);
305 aLocaleSeq
[0] = LanguageTag( m_pLanguageLB
->GetSelectedLanguage() ).getLocale();
309 const sal_Int32 nCount
= m_pCheckLangLB
->GetEntryCount();
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" );
324 } // namespace basctl
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */