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 "dlgresid.hrc"
29 #include "managelang.hrc"
31 #include <com/sun/star/i18n/Boundary.hpp>
32 #include <com/sun/star/i18n/WordType.hpp>
33 #include <com/sun/star/i18n/XBreakIterator.hpp>
34 #include <editeng/unolingu.hxx>
35 #include <sfx2/bindings.hxx>
36 #include <svtools/langtab.hxx>
37 #include <svx/langbox.hxx>
38 #include <vcl/unohelp.hxx>
39 #include <vcl/svapp.hxx>
40 #include <vcl/msgbox.hxx>
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
) );
59 long getLongestWordWidth( const OUString
& rText
, const Window
& rWin
)
62 Reference
< XBreakIterator
> xBreakIter( vcl::unohelper::CreateBreakIterator() );
63 sal_Int32 nStartPos
= 0;
64 const Locale aLocale
= Application::GetSettings().GetUILanguageTag().getLocale();
65 Boundary aBoundary
= xBreakIter
->getWordBoundary(
66 rText
, nStartPos
, aLocale
, WordType::ANYWORD_IGNOREWHITESPACES
, true );
68 while ( aBoundary
.startPos
!= aBoundary
.endPos
)
70 nStartPos
= aBoundary
.endPos
;
71 OUString
sWord(rText
.copy(aBoundary
.startPos
, aBoundary
.endPos
- aBoundary
.startPos
));
72 long nTemp
= rWin
.GetCtrlTextWidth( sWord
);
75 aBoundary
= xBreakIter
->nextWord(
76 rText
, nStartPos
, aLocale
, WordType::ANYWORD_IGNOREWHITESPACES
);
83 ManageLanguageDialog::ManageLanguageDialog( Window
* pParent
, boost::shared_ptr
<LocalizationMgr
> _pLMgr
) :
84 ModalDialog( pParent
, IDEResId( RID_DLG_MANAGE_LANGUAGE
) ),
85 m_aLanguageFT ( this, IDEResId( FT_LANGUAGE
) ),
86 m_aLanguageLB ( this, IDEResId( LB_LANGUAGE
) ),
87 m_aAddPB ( this, IDEResId( PB_ADD_LANG
) ),
88 m_aDeletePB ( this, IDEResId( PB_DEL_LANG
) ),
89 m_aMakeDefPB ( this, IDEResId( PB_MAKE_DEFAULT
) ),
90 m_aInfoFT ( this, IDEResId( FT_INFO
) ),
91 m_aBtnLine ( this, IDEResId( FL_BUTTONS
) ),
92 m_aHelpBtn ( this, IDEResId( PB_HELP
) ),
93 m_aCloseBtn ( this, IDEResId( PB_CLOSE
) ),
94 m_pLocalizationMgr ( _pLMgr
),
95 m_sDefLangStr (IDE_RESSTR(STR_DEF_LANG
)),
96 m_sDeleteStr (IDE_RESSTR(STR_DELETE
)),
97 m_sCreateLangStr (IDE_RESSTR(STR_CREATE_LANG
))
106 ManageLanguageDialog::~ManageLanguageDialog()
111 void ManageLanguageDialog::Init()
114 Shell
* pShell
= GetShell();
115 OUString sLibName
= pShell
->GetCurLibName();
116 // set dialog title with library name
117 OUString sText
= GetText();
118 sText
= sText
.replaceAll("$1", sLibName
);
121 m_aAddPB
.SetClickHdl( LINK( this, ManageLanguageDialog
, AddHdl
) );
122 m_aDeletePB
.SetClickHdl( LINK( this, ManageLanguageDialog
, DeleteHdl
) );
123 m_aMakeDefPB
.SetClickHdl( LINK( this, ManageLanguageDialog
, MakeDefHdl
) );
124 m_aLanguageLB
.SetSelectHdl( LINK( this, ManageLanguageDialog
, SelectHdl
) );
126 m_aLanguageLB
.EnableMultiSelection( true );
130 void ManageLanguageDialog::CalcInfoSize()
132 OUString sInfoStr
= m_aInfoFT
.GetText();
133 long nInfoWidth
= m_aInfoFT
.GetSizePixel().Width();
134 long nLongWord
= getLongestWordWidth( sInfoStr
, m_aInfoFT
);
135 long nTxtWidth
= m_aInfoFT
.GetCtrlTextWidth( sInfoStr
) + nLongWord
;
136 long nLines
= ( nTxtWidth
/ nInfoWidth
) + 1;
137 if ( nLines
> INFO_LINES_COUNT
)
139 Size aFTSize
= m_aLanguageFT
.GetSizePixel();
140 Size aSize
= m_aInfoFT
.GetSizePixel();
141 long nNewHeight
= aFTSize
.Height() * nLines
;
142 long nDelta
= nNewHeight
- aSize
.Height();
143 aSize
.Height() = nNewHeight
;
144 m_aInfoFT
.SetSizePixel( aSize
);
146 aSize
= m_aLanguageLB
.GetSizePixel();
147 aSize
.Height() -= nDelta
;
148 m_aLanguageLB
.SetSizePixel( aSize
);
150 Point aNewPos
= m_aInfoFT
.GetPosPixel();
151 aNewPos
.Y() -= nDelta
;
152 m_aInfoFT
.SetPosPixel( aNewPos
);
153 aNewPos
= m_aMakeDefPB
.GetPosPixel();
154 aNewPos
.Y() -= nDelta
;
155 m_aMakeDefPB
.SetPosPixel( aNewPos
);
159 void ManageLanguageDialog::FillLanguageBox()
161 DBG_ASSERT( m_pLocalizationMgr
, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
163 if ( m_pLocalizationMgr
->isLibraryLocalized() )
165 SvtLanguageTable aLangTable
;
166 Locale aDefaultLocale
= m_pLocalizationMgr
->getStringResourceManager()->getDefaultLocale();
167 Sequence
< Locale
> aLocaleSeq
= m_pLocalizationMgr
->getStringResourceManager()->getLocales();
168 const Locale
* pLocale
= aLocaleSeq
.getConstArray();
169 sal_Int32 i
, nCount
= aLocaleSeq
.getLength();
170 for ( i
= 0; i
< nCount
; ++i
)
172 bool bIsDefault
= localesAreEqual( aDefaultLocale
, pLocale
[i
] );
173 LanguageType eLangType
= LanguageTag( pLocale
[i
] ).getLanguageType();
174 OUStringBuffer
sLanguageBuf(aLangTable
.GetString( eLangType
));
177 sLanguageBuf
.append(' ');
178 sLanguageBuf
.append(m_sDefLangStr
);
180 OUString
sLanguage(sLanguageBuf
.makeStringAndClear());
181 sal_uInt16 nPos
= m_aLanguageLB
.InsertEntry( sLanguage
);
182 m_aLanguageLB
.SetEntryData( nPos
, new LanguageEntry( sLanguage
, pLocale
[i
], bIsDefault
) );
186 m_aLanguageLB
.InsertEntry( m_sCreateLangStr
);
189 void ManageLanguageDialog::ClearLanguageBox()
191 sal_uInt16 i
, nCount
= m_aLanguageLB
.GetEntryCount();
192 for ( i
= 0; i
< nCount
; ++i
)
194 LanguageEntry
* pEntry
= (LanguageEntry
*)( m_aLanguageLB
.GetEntryData(i
) );
197 m_aLanguageLB
.Clear();
200 IMPL_LINK_NOARG(ManageLanguageDialog
, AddHdl
)
202 SetDefaultLanguageDialog
aDlg( this, m_pLocalizationMgr
);
203 if ( RET_OK
== aDlg
.Execute() )
206 Sequence
< Locale
> aLocaleSeq
= aDlg
.GetLocales();
207 m_pLocalizationMgr
->handleAddLocales( aLocaleSeq
);
212 if (SfxBindings
* pBindings
= GetBindingsPtr())
213 pBindings
->Invalidate( SID_BASICIDE_CURRENT_LANG
);
218 IMPL_LINK_NOARG(ManageLanguageDialog
, DeleteHdl
)
220 QueryBox
aQBox( this, IDEResId( RID_QRYBOX_LANGUAGE
) );
221 aQBox
.SetButtonText( RET_OK
, m_sDeleteStr
);
222 if ( aQBox
.Execute() == RET_OK
)
224 sal_uInt16 i
, nCount
= m_aLanguageLB
.GetSelectEntryCount();
225 sal_uInt16 nPos
= m_aLanguageLB
.GetSelectEntryPos();
227 Sequence
< Locale
> aLocaleSeq( nCount
);
228 for ( i
= 0; i
< nCount
; ++i
)
230 sal_uInt16 nSelPos
= m_aLanguageLB
.GetSelectEntryPos(i
);
231 LanguageEntry
* pEntry
= (LanguageEntry
*)( m_aLanguageLB
.GetEntryData( nSelPos
) );
233 aLocaleSeq
[i
] = pEntry
->m_aLocale
;
235 m_pLocalizationMgr
->handleRemoveLocales( aLocaleSeq
);
240 nCount
= m_aLanguageLB
.GetEntryCount();
241 if ( nCount
<= nPos
)
243 m_aLanguageLB
.SelectEntryPos( nPos
);
249 IMPL_LINK_NOARG(ManageLanguageDialog
, MakeDefHdl
)
251 sal_uInt16 nPos
= m_aLanguageLB
.GetSelectEntryPos();
252 LanguageEntry
* pSelectEntry
= (LanguageEntry
*)( m_aLanguageLB
.GetEntryData( nPos
) );
253 if ( pSelectEntry
&& !pSelectEntry
->m_bIsDefault
)
255 // set new default entry
256 m_pLocalizationMgr
->handleSetDefaultLocale( pSelectEntry
->m_aLocale
);
261 m_aLanguageLB
.SelectEntryPos( nPos
);
268 IMPL_LINK_NOARG(ManageLanguageDialog
, SelectHdl
)
270 sal_uInt16 nCount
= m_aLanguageLB
.GetEntryCount();
271 bool bEmpty
= ( !nCount
||
272 m_aLanguageLB
.GetEntryPos( m_sCreateLangStr
) != LISTBOX_ENTRY_NOTFOUND
);
273 bool bSelect
= ( m_aLanguageLB
.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND
);
274 bool bEnable
= ( !bEmpty
&& bSelect
!= false );
276 m_aDeletePB
.Enable( bEnable
!= false );
277 m_aMakeDefPB
.Enable( bEnable
!= false && nCount
> 1 && m_aLanguageLB
.GetSelectEntryCount() == 1 );
282 // class SetDefaultLanguageDialog -----------------------------------------------
284 SetDefaultLanguageDialog::SetDefaultLanguageDialog( Window
* pParent
, boost::shared_ptr
<LocalizationMgr
> _pLMgr
) :
286 ModalDialog( pParent
, IDEResId( RID_DLG_SETDEF_LANGUAGE
) ),
287 m_aLanguageFT ( this, IDEResId( FT_DEF_LANGUAGE
) ),
288 m_pLanguageLB ( new SvxLanguageBox( this, IDEResId( LB_DEF_LANGUAGE
) ) ),
289 m_pCheckLangLB ( NULL
),
290 m_aInfoFT ( this, IDEResId( FT_DEF_INFO
) ),
291 m_aBtnLine ( this, IDEResId( FL_DEF_BUTTONS
) ),
292 m_aOKBtn ( this, IDEResId( PB_DEF_OK
) ),
293 m_aCancelBtn ( this, IDEResId( PB_DEF_CANCEL
) ),
294 m_aHelpBtn ( this, IDEResId( PB_DEF_HELP
) ),
295 m_pLocalizationMgr( _pLMgr
)
297 if ( m_pLocalizationMgr
->isLibraryLocalized() )
299 // change to "Add Interface Language" mode
300 SetHelpId( HID_BASICIDE_ADDNEW_LANGUAGE
);
301 m_pCheckLangLB
= new SvxCheckListBox( this, IDEResId( LB_ADD_LANGUAGE
) );
302 SetText( IDE_RESSTR(STR_ADDLANG_TITLE
) );
303 m_aLanguageFT
.SetText( IDE_RESSTR(STR_ADDLANG_LABEL
) );
304 m_aInfoFT
.SetText( IDE_RESSTR(STR_ADDLANG_INFO
) );
313 SetDefaultLanguageDialog::~SetDefaultLanguageDialog()
315 delete m_pLanguageLB
;
316 delete m_pCheckLangLB
;
319 void SetDefaultLanguageDialog::FillLanguageBox()
321 // fill list with all languages
322 m_pLanguageLB
->SetLanguageList( LANG_LIST_ALL
, false );
323 // remove the already localized languages
324 Sequence
< Locale
> aLocaleSeq
= m_pLocalizationMgr
->getStringResourceManager()->getLocales();
325 const Locale
* pLocale
= aLocaleSeq
.getConstArray();
326 sal_Int32 i
, nCount
= aLocaleSeq
.getLength();
327 for ( i
= 0; i
< nCount
; ++i
)
328 m_pLanguageLB
->RemoveLanguage( LanguageTag( pLocale
[i
] ).getLanguageType() );
330 // fill checklistbox if not in default mode
331 if ( m_pLocalizationMgr
->isLibraryLocalized() )
333 sal_uInt16 j
, nCount_
= m_pLanguageLB
->GetEntryCount();
334 for ( j
= 0; j
< nCount_
; ++j
)
336 m_pCheckLangLB
->InsertEntry(
337 m_pLanguageLB
->GetEntry(j
), LISTBOX_APPEND
, m_pLanguageLB
->GetEntryData(j
) );
339 delete m_pLanguageLB
;
340 m_pLanguageLB
= NULL
;
343 // preselect current UI language
344 m_pLanguageLB
->SelectLanguage( Application::GetSettings().GetUILanguageTag().getLanguageType() );
347 void SetDefaultLanguageDialog::CalcInfoSize()
349 OUString sInfoStr
= m_aInfoFT
.GetText();
350 long nInfoWidth
= m_aInfoFT
.GetSizePixel().Width();
351 long nLongWord
= getLongestWordWidth( sInfoStr
, m_aInfoFT
);
352 long nTxtWidth
= m_aInfoFT
.GetCtrlTextWidth( sInfoStr
) + nLongWord
;
353 long nLines
= ( nTxtWidth
/ nInfoWidth
) + 1;
354 if ( nLines
> INFO_LINES_COUNT
)
356 Size aFTSize
= m_aLanguageFT
.GetSizePixel();
357 Size aSize
= m_aInfoFT
.GetSizePixel();
358 long nNewHeight
= aFTSize
.Height() * nLines
;
359 long nDelta
= nNewHeight
- aSize
.Height();
360 aSize
.Height() = nNewHeight
;
361 m_aInfoFT
.SetSizePixel( aSize
);
363 Window
* pWin
= ( m_pLanguageLB
!= NULL
) ? dynamic_cast< Window
* >( m_pLanguageLB
)
364 : dynamic_cast< Window
* >( m_pCheckLangLB
);
365 aSize
= pWin
->GetSizePixel();
366 aSize
.Height() -= nDelta
;
367 pWin
->SetSizePixel( aSize
);
369 Point aNewPos
= m_aInfoFT
.GetPosPixel();
370 aNewPos
.Y() -= nDelta
;
371 m_aInfoFT
.SetPosPixel( aNewPos
);
375 Sequence
< Locale
> SetDefaultLanguageDialog::GetLocales() const
377 bool bNotLocalized
= !m_pLocalizationMgr
->isLibraryLocalized();
378 sal_Int32 nSize
= bNotLocalized
? 1 : m_pCheckLangLB
->GetCheckedEntryCount();
379 Sequence
< Locale
> aLocaleSeq( nSize
);
382 aLocaleSeq
[0] = LanguageTag( m_pLanguageLB
->GetSelectLanguage() ).getLocale();
386 sal_uInt16 i
, nCount
= static_cast< sal_uInt16
>( m_pCheckLangLB
->GetEntryCount() );
388 for ( i
= 0; i
< nCount
; ++i
)
390 if ( m_pCheckLangLB
->IsChecked(i
) )
392 LanguageType eType
= LanguageType( (sal_uLong
)m_pCheckLangLB
->GetEntryData(i
) );
393 aLocaleSeq
[j
++] = LanguageTag( eType
).getLocale();
396 DBG_ASSERT( nSize
== j
, "SetDefaultLanguageDialog::GetLocales(): invalid indexes" );
401 } // namespace basctl
403 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */