Update ooo320-m1
[ooovba.git] / basctl / source / dlged / managelang.cxx
blob098ad9bc40e880eadbb6dbb2e1fd490e9eb13d47
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: managelang.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basctl.hxx"
34 #include "managelang.hxx"
35 #include "iderid.hxx"
36 #include "localizationmgr.hxx"
37 #include "iderdll.hxx"
38 #include "basidesh.hxx"
39 #include "basobj.hxx"
41 #include "managelang.hrc"
42 #include "dlgresid.hrc"
43 #include "helpid.hrc"
45 #ifndef _COM_SUN_STAR_TEXT_XBREAKITERATOR_HPP_
46 #include <com/sun/star/i18n/XBreakIterator.hpp>
47 #endif
48 #include <com/sun/star/i18n/WordType.hpp>
49 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
50 #include <com/sun/star/resource/XStringResourceManager.hpp>
51 #include <comphelper/processfactory.hxx>
52 #include <vcl/unohelp.hxx>
53 #ifndef _VCL_I18NHELP_HXX
54 //#include <vcl/i18nhelp.hxx>
55 #endif
56 #include <vcl/svapp.hxx>
57 #include <vcl/msgbox.hxx>
58 #include <unotools/localedatawrapper.hxx>
59 #include <svx/unolingu.hxx>
61 #include <svtools/langtab.hxx>
63 #include <sfx2/bindings.hxx>
65 using namespace ::com::sun::star::i18n;
66 using namespace ::com::sun::star::lang;
67 using namespace ::com::sun::star::resource;
68 using namespace ::com::sun::star::uno;
70 bool localesAreEqual( const Locale& rLocaleLeft, const Locale& rLocaleRight )
72 bool bRet = ( rLocaleLeft.Language.equals( rLocaleRight.Language ) &&
73 rLocaleLeft.Country.equals( rLocaleRight.Country ) &&
74 rLocaleLeft.Variant.equals( rLocaleRight.Variant ) );
75 return bRet;
78 namespace {
79 long getLongestWordWidth( const String& rText, const Window& rWin )
81 long nWidth = 0;
82 Reference< XBreakIterator > xBreakIter( vcl::unohelper::CreateBreakIterator() );
83 sal_Int32 nStartPos = 0;
84 const Locale aLocale = Application::GetSettings().GetUILocale();
85 Boundary aBoundary = xBreakIter->getWordBoundary(
86 rText, nStartPos, aLocale, WordType::ANYWORD_IGNOREWHITESPACES, sal_True );
88 while ( aBoundary.startPos != aBoundary.endPos )
90 nStartPos = aBoundary.endPos;
91 String sWord( rText.Copy(
92 (USHORT)aBoundary.startPos,
93 (USHORT)aBoundary.endPos - (USHORT)aBoundary.startPos ) );
94 long nTemp = rWin.GetCtrlTextWidth( sWord );
95 if ( nTemp > nWidth )
96 nWidth = nTemp;
97 aBoundary = xBreakIter->nextWord(
98 rText, nStartPos, aLocale, WordType::ANYWORD_IGNOREWHITESPACES );
101 return nWidth;
105 ManageLanguageDialog::ManageLanguageDialog( Window* pParent, LocalizationMgr* _pLMgr ) :
107 ModalDialog( pParent, IDEResId( RID_DLG_MANAGE_LANGUAGE ) ),
109 m_aLanguageFT ( this, IDEResId( FT_LANGUAGE ) ),
110 m_aLanguageLB ( this, IDEResId( LB_LANGUAGE ) ),
111 m_aAddPB ( this, IDEResId( PB_ADD_LANG ) ),
112 m_aDeletePB ( this, IDEResId( PB_DEL_LANG ) ),
113 m_aMakeDefPB ( this, IDEResId( PB_MAKE_DEFAULT ) ),
114 m_aInfoFT ( this, IDEResId( FT_INFO ) ),
115 m_aBtnLine ( this, IDEResId( FL_BUTTONS ) ),
116 m_aHelpBtn ( this, IDEResId( PB_HELP ) ),
117 m_aCloseBtn ( this, IDEResId( PB_CLOSE ) ),
118 m_pLocalizationMgr ( _pLMgr ),
119 m_sDefLangStr ( IDEResId( STR_DEF_LANG ) ),
120 m_sDeleteStr ( IDEResId( STR_DELETE ) ),
121 m_sCreateLangStr ( IDEResId( STR_CREATE_LANG ) )
124 FreeResource();
126 Init();
127 FillLanguageBox();
128 SelectHdl( NULL );
131 ManageLanguageDialog::~ManageLanguageDialog()
133 ClearLanguageBox();
136 void ManageLanguageDialog::Init()
138 // get current IDE
139 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
140 String sLibName = pIDEShell->GetCurLibName();
141 // set dialog title with library name
142 String sText = GetText();
143 sText.SearchAndReplace( String::CreateFromAscii("$1"), sLibName );
144 SetText( sText );
145 // set handler
146 m_aAddPB.SetClickHdl( LINK( this, ManageLanguageDialog, AddHdl ) );
147 m_aDeletePB.SetClickHdl( LINK( this, ManageLanguageDialog, DeleteHdl ) );
148 m_aMakeDefPB.SetClickHdl( LINK( this, ManageLanguageDialog, MakeDefHdl ) );
149 m_aLanguageLB.SetSelectHdl( LINK( this, ManageLanguageDialog, SelectHdl ) );
151 m_aLanguageLB.EnableMultiSelection( TRUE );
152 CalcInfoSize();
155 void ManageLanguageDialog::CalcInfoSize()
157 String sInfoStr = m_aInfoFT.GetText();
158 long nInfoWidth = m_aInfoFT.GetSizePixel().Width();
159 long nLongWord = getLongestWordWidth( sInfoStr, m_aInfoFT );
160 long nTxtWidth = m_aInfoFT.GetCtrlTextWidth( sInfoStr ) + nLongWord;
161 long nLines = ( nTxtWidth / nInfoWidth ) + 1;
162 if ( nLines > INFO_LINES_COUNT )
164 Size aFTSize = m_aLanguageFT.GetSizePixel();
165 Size aSize = m_aInfoFT.GetSizePixel();
166 long nNewHeight = aFTSize.Height() * nLines;
167 long nDelta = nNewHeight - aSize.Height();
168 aSize.Height() = nNewHeight;
169 m_aInfoFT.SetSizePixel( aSize );
171 aSize = m_aLanguageLB.GetSizePixel();
172 aSize.Height() -= nDelta;
173 m_aLanguageLB.SetSizePixel( aSize );
175 Point aNewPos = m_aInfoFT.GetPosPixel();
176 aNewPos.Y() -= nDelta;
177 m_aInfoFT.SetPosPixel( aNewPos );
178 aNewPos = m_aMakeDefPB.GetPosPixel();
179 aNewPos.Y() -= nDelta;
180 m_aMakeDefPB.SetPosPixel( aNewPos );
184 void ManageLanguageDialog::FillLanguageBox()
186 DBG_ASSERT( m_pLocalizationMgr, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
188 if ( m_pLocalizationMgr->isLibraryLocalized() )
190 SvtLanguageTable aLangTable;
191 Locale aDefaultLocale = m_pLocalizationMgr->getStringResourceManager()->getDefaultLocale();
192 Sequence< Locale > aLocaleSeq = m_pLocalizationMgr->getStringResourceManager()->getLocales();
193 const Locale* pLocale = aLocaleSeq.getConstArray();
194 INT32 i, nCount = aLocaleSeq.getLength();
195 for ( i = 0; i < nCount; ++i )
197 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
198 LanguageType eLangType = SvxLocaleToLanguage( pLocale[i] );
199 String sLanguage = aLangTable.GetString( eLangType );
200 if ( bIsDefault )
202 sLanguage += ' ';
203 sLanguage += m_sDefLangStr;
205 USHORT nPos = m_aLanguageLB.InsertEntry( sLanguage );
206 m_aLanguageLB.SetEntryData( nPos, new LanguageEntry( sLanguage, pLocale[i], bIsDefault ) );
209 else
210 m_aLanguageLB.InsertEntry( m_sCreateLangStr );
213 void ManageLanguageDialog::ClearLanguageBox()
215 USHORT i, nCount = m_aLanguageLB.GetEntryCount();
216 for ( i = 0; i < nCount; ++i )
218 LanguageEntry* pEntry = (LanguageEntry*)( m_aLanguageLB.GetEntryData(i) );
219 if ( pEntry )
220 delete pEntry;
222 m_aLanguageLB.Clear();
225 IMPL_LINK( ManageLanguageDialog, AddHdl, Button *, EMPTYARG )
227 SetDefaultLanguageDialog aDlg( this, m_pLocalizationMgr );
228 if ( RET_OK == aDlg.Execute() )
230 // add new locales
231 Sequence< Locale > aLocaleSeq = aDlg.GetLocales();
232 m_pLocalizationMgr->handleAddLocales( aLocaleSeq );
233 // update listbox
234 ClearLanguageBox();
235 FillLanguageBox();
237 SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
238 if ( pBindings )
239 pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
241 return 1;
244 IMPL_LINK( ManageLanguageDialog, DeleteHdl, Button *, EMPTYARG )
246 QueryBox aQBox( this, IDEResId( RID_QRYBOX_LANGUAGE ) );
247 aQBox.SetButtonText( RET_OK, m_sDeleteStr );
248 if ( aQBox.Execute() == RET_OK )
250 USHORT i, nCount = m_aLanguageLB.GetSelectEntryCount();
251 USHORT nPos = m_aLanguageLB.GetSelectEntryPos();
252 // remove locales
253 Sequence< Locale > aLocaleSeq( nCount );
254 for ( i = 0; i < nCount; ++i )
256 USHORT nSelPos = m_aLanguageLB.GetSelectEntryPos(i);
257 LanguageEntry* pEntry = (LanguageEntry*)( m_aLanguageLB.GetEntryData( nSelPos ) );
258 if ( pEntry )
259 aLocaleSeq[i] = pEntry->m_aLocale;
261 m_pLocalizationMgr->handleRemoveLocales( aLocaleSeq );
262 // update listbox
263 ClearLanguageBox();
264 FillLanguageBox();
265 // reset selection
266 nCount = m_aLanguageLB.GetEntryCount();
267 if ( nCount <= nPos )
268 nPos = nCount - 1;
269 m_aLanguageLB.SelectEntryPos( nPos );
270 SelectHdl( NULL );
272 return 1;
275 IMPL_LINK( ManageLanguageDialog, MakeDefHdl, Button *, EMPTYARG )
277 USHORT nPos = m_aLanguageLB.GetSelectEntryPos();
278 LanguageEntry* pSelectEntry = (LanguageEntry*)( m_aLanguageLB.GetEntryData( nPos ) );
279 if ( pSelectEntry && !pSelectEntry->m_bIsDefault )
281 // set new default entry
282 m_pLocalizationMgr->handleSetDefaultLocale( pSelectEntry->m_aLocale );
283 // update Listbox
284 ClearLanguageBox();
285 FillLanguageBox();
286 // reset selection
287 m_aLanguageLB.SelectEntryPos( nPos );
288 SelectHdl( NULL );
291 return 1;
294 IMPL_LINK( ManageLanguageDialog, SelectHdl, ListBox *, EMPTYARG )
296 USHORT nCount = m_aLanguageLB.GetEntryCount();
297 bool bEmpty = ( !nCount ||
298 m_aLanguageLB.GetEntryPos( m_sCreateLangStr ) != LISTBOX_ENTRY_NOTFOUND );
299 bool bSelect = ( m_aLanguageLB.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND );
300 bool bEnable = ( !bEmpty && bSelect != false );
302 m_aDeletePB.Enable( bEnable != false );
303 m_aMakeDefPB.Enable( bEnable != false && nCount > 1 && m_aLanguageLB.GetSelectEntryCount() == 1 );
305 return 1;
308 // class SetDefaultLanguageDialog -----------------------------------------------
310 SetDefaultLanguageDialog::SetDefaultLanguageDialog( Window* pParent, LocalizationMgr* _pLMgr ) :
312 ModalDialog( pParent, IDEResId( RID_DLG_SETDEF_LANGUAGE ) ),
314 m_aLanguageFT ( this, IDEResId( FT_DEF_LANGUAGE ) ),
315 m_pLanguageLB ( new SvxLanguageBox( this, IDEResId( LB_DEF_LANGUAGE ) ) ),
316 m_pCheckLangLB ( NULL ),
317 m_aInfoFT ( this, IDEResId( FT_DEF_INFO ) ),
318 m_aBtnLine ( this, IDEResId( FL_DEF_BUTTONS ) ),
319 m_aOKBtn ( this, IDEResId( PB_DEF_OK ) ),
320 m_aCancelBtn ( this, IDEResId( PB_DEF_CANCEL ) ),
321 m_aHelpBtn ( this, IDEResId( PB_DEF_HELP ) ),
323 m_pLocalizationMgr( _pLMgr )
326 if ( m_pLocalizationMgr->isLibraryLocalized() )
328 // change to "Add Interface Language" mode
329 SetHelpId( HID_BASICIDE_ADDNEW_LANGUAGE );
330 m_pCheckLangLB = new SvxCheckListBox( this, IDEResId( LB_ADD_LANGUAGE ) );
331 SetText( String( IDEResId( STR_ADDLANG_TITLE ) ) );
332 m_aLanguageFT.SetText( String( IDEResId( STR_ADDLANG_LABEL ) ) );
333 m_aInfoFT.SetText( String( IDEResId( STR_ADDLANG_INFO ) ) );
336 FreeResource();
338 FillLanguageBox();
339 CalcInfoSize();
342 SetDefaultLanguageDialog::~SetDefaultLanguageDialog()
344 delete m_pLanguageLB;
345 delete m_pCheckLangLB;
348 void SetDefaultLanguageDialog::FillLanguageBox()
350 // fill list with all languages
351 m_pLanguageLB->SetLanguageList( LANG_LIST_ALL, FALSE );
352 // remove the already localized languages
353 Sequence< Locale > aLocaleSeq = m_pLocalizationMgr->getStringResourceManager()->getLocales();
354 const Locale* pLocale = aLocaleSeq.getConstArray();
355 INT32 i, nCount = aLocaleSeq.getLength();
356 for ( i = 0; i < nCount; ++i )
357 m_pLanguageLB->RemoveLanguage( SvxLocaleToLanguage( pLocale[i] ) );
359 // fill checklistbox if not in default mode
360 if ( m_pLocalizationMgr->isLibraryLocalized() )
362 USHORT j, nCount_ = m_pLanguageLB->GetEntryCount();
363 for ( j = 0; j < nCount_; ++j )
365 m_pCheckLangLB->InsertEntry(
366 m_pLanguageLB->GetEntry(j), LISTBOX_APPEND, m_pLanguageLB->GetEntryData(j) );
368 delete m_pLanguageLB;
369 m_pLanguageLB = NULL;
371 else
372 // preselect current UI language
373 m_pLanguageLB->SelectLanguage( Application::GetSettings().GetUILanguage() );
376 void SetDefaultLanguageDialog::CalcInfoSize()
378 String sInfoStr = m_aInfoFT.GetText();
379 long nInfoWidth = m_aInfoFT.GetSizePixel().Width();
380 long nLongWord = getLongestWordWidth( sInfoStr, m_aInfoFT );
381 long nTxtWidth = m_aInfoFT.GetCtrlTextWidth( sInfoStr ) + nLongWord;
382 long nLines = ( nTxtWidth / nInfoWidth ) + 1;
383 if ( nLines > INFO_LINES_COUNT )
385 Size aFTSize = m_aLanguageFT.GetSizePixel();
386 Size aSize = m_aInfoFT.GetSizePixel();
387 long nNewHeight = aFTSize.Height() * nLines;
388 long nDelta = nNewHeight - aSize.Height();
389 aSize.Height() = nNewHeight;
390 m_aInfoFT.SetSizePixel( aSize );
392 Window* pWin = ( m_pLanguageLB != NULL ) ? dynamic_cast< Window* >( m_pLanguageLB )
393 : dynamic_cast< Window* >( m_pCheckLangLB );
394 aSize = pWin->GetSizePixel();
395 aSize.Height() -= nDelta;
396 pWin->SetSizePixel( aSize );
398 Point aNewPos = m_aInfoFT.GetPosPixel();
399 aNewPos.Y() -= nDelta;
400 m_aInfoFT.SetPosPixel( aNewPos );
404 Sequence< Locale > SetDefaultLanguageDialog::GetLocales() const
406 bool bNotLocalized = !m_pLocalizationMgr->isLibraryLocalized();
407 INT32 nSize = bNotLocalized ? 1 : m_pCheckLangLB->GetCheckedEntryCount();
408 Sequence< Locale > aLocaleSeq( nSize );
409 if ( bNotLocalized )
411 Locale aLocale;
412 SvxLanguageToLocale( aLocale, m_pLanguageLB->GetSelectLanguage() );
413 aLocaleSeq[0] = aLocale;
415 else
417 USHORT i, nCount = static_cast< USHORT >( m_pCheckLangLB->GetEntryCount() );
418 INT32 j = 0;
419 for ( i = 0; i < nCount; ++i )
421 if ( m_pCheckLangLB->IsChecked(i) )
423 LanguageType eType = LanguageType( (ULONG)m_pCheckLangLB->GetEntryData(i) );
424 Locale aLocale;
425 SvxLanguageToLocale( aLocale, eType );
426 aLocaleSeq[j++] = aLocale;
429 DBG_ASSERT( nSize == j, "SetDefaultLanguageDialog::GetLocales(): invalid indexes" );
431 return aLocaleSeq;