xref: /trunk/main/basctl/source/dlged/managelang.cxx (revision 31598a22)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_basctl.hxx"
26 
27 #include "managelang.hxx"
28 #include "iderid.hxx"
29 #include "localizationmgr.hxx"
30 #include "iderdll.hxx"
31 #include "basidesh.hxx"
32 #include "basobj.hxx"
33 
34 #include "managelang.hrc"
35 #include "dlgresid.hrc"
36 #include "helpid.hrc"
37 
38 #ifndef _COM_SUN_STAR_TEXT_XBREAKITERATOR_HPP_
39 #include <com/sun/star/i18n/XBreakIterator.hpp>
40 #endif
41 #include <com/sun/star/i18n/WordType.hpp>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/resource/XStringResourceManager.hpp>
44 #include <comphelper/processfactory.hxx>
45 #include <vcl/unohelp.hxx>
46 #ifndef _VCL_I18NHELP_HXX
47 //#include <vcl/i18nhelp.hxx>
48 #endif
49 #include <vcl/svapp.hxx>
50 #include <vcl/msgbox.hxx>
51 #include <unotools/localedatawrapper.hxx>
52 #include <editeng/unolingu.hxx>
53 
54 #include <svtools/langtab.hxx>
55 
56 #include <sfx2/bindings.hxx>
57 
58 using namespace ::com::sun::star::i18n;
59 using namespace ::com::sun::star::lang;
60 using namespace ::com::sun::star::resource;
61 using namespace ::com::sun::star::uno;
62 
localesAreEqual(const Locale & rLocaleLeft,const Locale & rLocaleRight)63 bool localesAreEqual( const Locale& rLocaleLeft, const Locale& rLocaleRight )
64 {
65 	bool bRet = ( rLocaleLeft.Language.equals( rLocaleRight.Language ) &&
66 				  rLocaleLeft.Country.equals( rLocaleRight.Country ) &&
67 				  rLocaleLeft.Variant.equals( rLocaleRight.Variant ) );
68 	return bRet;
69 }
70 
71 namespace {
getLongestWordWidth(const String & rText,const Window & rWin)72 	long getLongestWordWidth( const String& rText, const Window& rWin )
73 	{
74 		long nWidth = 0;
75 		Reference< XBreakIterator >	xBreakIter( vcl::unohelper::CreateBreakIterator() );
76 		sal_Int32 nStartPos = 0;
77 		const Locale aLocale = Application::GetSettings().GetUILocale();
78 		Boundary aBoundary = xBreakIter->getWordBoundary(
79 			rText, nStartPos, aLocale, WordType::ANYWORD_IGNOREWHITESPACES, sal_True );
80 
81 		while ( aBoundary.startPos != aBoundary.endPos )
82 		{
83 			nStartPos = aBoundary.endPos;
84 			String sWord( rText.Copy(
85             	(sal_uInt16)aBoundary.startPos,
86             	(sal_uInt16)aBoundary.endPos - (sal_uInt16)aBoundary.startPos ) );
87 			long nTemp = rWin.GetCtrlTextWidth( sWord );
88 			if ( nTemp > nWidth )
89 				nWidth = nTemp;
90 			aBoundary = xBreakIter->nextWord(
91 				rText, nStartPos, aLocale, WordType::ANYWORD_IGNOREWHITESPACES );
92 		}
93 
94 		return nWidth;
95 	}
96 }
97 
ManageLanguageDialog(Window * pParent,LocalizationMgr * _pLMgr)98 ManageLanguageDialog::ManageLanguageDialog( Window* pParent, LocalizationMgr* _pLMgr ) :
99 
100 	ModalDialog( pParent, IDEResId( RID_DLG_MANAGE_LANGUAGE ) ),
101 
102 	m_aLanguageFT		( this, IDEResId( FT_LANGUAGE ) ),
103 	m_aLanguageLB		( this, IDEResId( LB_LANGUAGE ) ),
104 	m_aAddPB			( this, IDEResId( PB_ADD_LANG ) ),
105 	m_aDeletePB			( this, IDEResId( PB_DEL_LANG ) ),
106 	m_aMakeDefPB		( this, IDEResId( PB_MAKE_DEFAULT ) ),
107 	m_aInfoFT			( this, IDEResId( FT_INFO ) ),
108 	m_aBtnLine			( this, IDEResId( FL_BUTTONS ) ),
109 	m_aHelpBtn			( this, IDEResId( PB_HELP ) ),
110 	m_aCloseBtn			( this, IDEResId( PB_CLOSE ) ),
111 	m_pLocalizationMgr	( _pLMgr ),
112 	m_sDefLangStr		( 		IDEResId( STR_DEF_LANG ) ),
113 	m_sDeleteStr		( 		IDEResId( STR_DELETE ) ),
114 	m_sCreateLangStr	(		IDEResId( STR_CREATE_LANG ) )
115 
116 {
117 	FreeResource();
118 
119 	Init();
120 	FillLanguageBox();
121 	SelectHdl( NULL );
122 }
123 
~ManageLanguageDialog()124 ManageLanguageDialog::~ManageLanguageDialog()
125 {
126 	ClearLanguageBox();
127 }
128 
Init()129 void ManageLanguageDialog::Init()
130 {
131 	// get current IDE
132 	BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
133 	String sLibName = pIDEShell->GetCurLibName();
134 	// set dialog title with library name
135 	String sText = GetText();
136 	sText.SearchAndReplace( String::CreateFromAscii("$1"), sLibName );
137 	SetText( sText );
138 	// set handler
139 	m_aAddPB.SetClickHdl( LINK( this, ManageLanguageDialog, AddHdl ) );
140 	m_aDeletePB.SetClickHdl( LINK( this, ManageLanguageDialog, DeleteHdl ) );
141 	m_aMakeDefPB.SetClickHdl( LINK( this, ManageLanguageDialog, MakeDefHdl ) );
142 	m_aLanguageLB.SetSelectHdl( LINK( this, ManageLanguageDialog, SelectHdl ) );
143 
144 	m_aLanguageLB.EnableMultiSelection( sal_True );
145 	CalcInfoSize();
146 }
147 
CalcInfoSize()148 void ManageLanguageDialog::CalcInfoSize()
149 {
150 	String sInfoStr = m_aInfoFT.GetText();
151 	long nInfoWidth = m_aInfoFT.GetSizePixel().Width();
152 	long nLongWord = getLongestWordWidth( sInfoStr, m_aInfoFT );
153 	long nTxtWidth = m_aInfoFT.GetCtrlTextWidth( sInfoStr ) + nLongWord;
154 	long nLines = ( nTxtWidth / nInfoWidth ) + 1;
155 	if ( nLines > INFO_LINES_COUNT )
156 	{
157 		Size aFTSize = m_aLanguageFT.GetSizePixel();
158 		Size aSize = m_aInfoFT.GetSizePixel();
159 		long nNewHeight = aFTSize.Height() * nLines;
160 		long nDelta = nNewHeight - aSize.Height();
161 		aSize.Height() = nNewHeight;
162 		m_aInfoFT.SetSizePixel( aSize );
163 
164 		aSize = m_aLanguageLB.GetSizePixel();
165 		aSize.Height() -= nDelta;
166 		m_aLanguageLB.SetSizePixel( aSize );
167 
168         Point aNewPos = m_aInfoFT.GetPosPixel();
169         aNewPos.Y() -= nDelta;
170         m_aInfoFT.SetPosPixel( aNewPos );
171         aNewPos = m_aMakeDefPB.GetPosPixel();
172         aNewPos.Y() -= nDelta;
173         m_aMakeDefPB.SetPosPixel( aNewPos );
174 	}
175 }
176 
FillLanguageBox()177 void ManageLanguageDialog::FillLanguageBox()
178 {
179 	DBG_ASSERT( m_pLocalizationMgr, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
180 
181 	if ( m_pLocalizationMgr->isLibraryLocalized() )
182 	{
183 		SvtLanguageTable aLangTable;
184 		Locale aDefaultLocale = m_pLocalizationMgr->getStringResourceManager()->getDefaultLocale();
185 		Sequence< Locale > aLocaleSeq = m_pLocalizationMgr->getStringResourceManager()->getLocales();
186 		const Locale* pLocale = aLocaleSeq.getConstArray();
187 		sal_Int32 i, nCount = aLocaleSeq.getLength();
188 		for ( i = 0;  i < nCount;  ++i )
189 		{
190 			bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
191 			LanguageType eLangType = SvxLocaleToLanguage( pLocale[i] );
192 			String sLanguage = aLangTable.GetString( eLangType );
193 			if ( bIsDefault )
194 			{
195 				sLanguage += ' ';
196 				sLanguage += m_sDefLangStr;
197 			}
198 			sal_uInt16 nPos = m_aLanguageLB.InsertEntry( sLanguage );
199 			m_aLanguageLB.SetEntryData( nPos, new LanguageEntry( sLanguage, pLocale[i], bIsDefault ) );
200 		}
201 	}
202 	else
203 		m_aLanguageLB.InsertEntry( m_sCreateLangStr );
204 }
205 
ClearLanguageBox()206 void ManageLanguageDialog::ClearLanguageBox()
207 {
208 	sal_uInt16 i, nCount = m_aLanguageLB.GetEntryCount();
209 	for ( i = 0; i < nCount; ++i )
210 	{
211 		LanguageEntry* pEntry = (LanguageEntry*)( m_aLanguageLB.GetEntryData(i) );
212 		if ( pEntry )
213 			delete pEntry;
214 	}
215 	m_aLanguageLB.Clear();
216 }
217 
IMPL_LINK(ManageLanguageDialog,AddHdl,Button *,EMPTYARG)218 IMPL_LINK( ManageLanguageDialog, AddHdl, Button *, EMPTYARG )
219 {
220 	SetDefaultLanguageDialog aDlg( this, m_pLocalizationMgr );
221 	if ( RET_OK == aDlg.Execute() )
222 	{
223 		// add new locales
224 	    Sequence< Locale > aLocaleSeq = aDlg.GetLocales();
225 		m_pLocalizationMgr->handleAddLocales( aLocaleSeq );
226 		// update listbox
227 		ClearLanguageBox();
228 		FillLanguageBox();
229 
230         SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
231         if ( pBindings )
232             pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
233 	}
234 	return 1;
235 }
236 
IMPL_LINK(ManageLanguageDialog,DeleteHdl,Button *,EMPTYARG)237 IMPL_LINK( ManageLanguageDialog, DeleteHdl, Button *, EMPTYARG )
238 {
239 	QueryBox aQBox( this, IDEResId( RID_QRYBOX_LANGUAGE ) );
240 	aQBox.SetButtonText( RET_OK, m_sDeleteStr );
241 	if ( aQBox.Execute() == RET_OK )
242 	{
243 		sal_uInt16 i, nCount = m_aLanguageLB.GetSelectEntryCount();
244 		sal_uInt16 nPos = m_aLanguageLB.GetSelectEntryPos();
245 		// remove locales
246 	    Sequence< Locale > aLocaleSeq( nCount );
247 		for ( i = 0; i < nCount; ++i )
248 		{
249 			sal_uInt16 nSelPos = m_aLanguageLB.GetSelectEntryPos(i);
250 			LanguageEntry* pEntry = (LanguageEntry*)( m_aLanguageLB.GetEntryData( nSelPos ) );
251 			if ( pEntry )
252 				aLocaleSeq[i] = pEntry->m_aLocale;
253 		}
254 		m_pLocalizationMgr->handleRemoveLocales( aLocaleSeq );
255 		// update listbox
256 		ClearLanguageBox();
257 		FillLanguageBox();
258 		// reset selection
259 		nCount = m_aLanguageLB.GetEntryCount();
260 		if ( nCount <= nPos )
261 			nPos = nCount - 1;
262 		m_aLanguageLB.SelectEntryPos( nPos );
263 		SelectHdl( NULL );
264 	}
265 	return 1;
266 }
267 
IMPL_LINK(ManageLanguageDialog,MakeDefHdl,Button *,EMPTYARG)268 IMPL_LINK( ManageLanguageDialog, MakeDefHdl, Button *, EMPTYARG )
269 {
270 	sal_uInt16 nPos = m_aLanguageLB.GetSelectEntryPos();
271 	LanguageEntry* pSelectEntry = (LanguageEntry*)( m_aLanguageLB.GetEntryData( nPos ) );
272 	if ( pSelectEntry && !pSelectEntry->m_bIsDefault )
273 	{
274 		// set new default entry
275 		m_pLocalizationMgr->handleSetDefaultLocale( pSelectEntry->m_aLocale );
276 		// update Listbox
277 		ClearLanguageBox();
278 		FillLanguageBox();
279 		// reset selection
280 		m_aLanguageLB.SelectEntryPos( nPos );
281 		SelectHdl( NULL );
282 	}
283 
284 	return 1;
285 }
286 
IMPL_LINK(ManageLanguageDialog,SelectHdl,ListBox *,EMPTYARG)287 IMPL_LINK( ManageLanguageDialog, SelectHdl, ListBox *, EMPTYARG )
288 {
289 	sal_uInt16 nCount = m_aLanguageLB.GetEntryCount();
290 	bool bEmpty = ( !nCount ||
291 					m_aLanguageLB.GetEntryPos( m_sCreateLangStr ) != LISTBOX_ENTRY_NOTFOUND );
292 	bool bSelect = ( m_aLanguageLB.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND );
293 	bool bEnable = ( !bEmpty && bSelect != false );
294 
295 	m_aDeletePB.Enable( bEnable != false );
296 	m_aMakeDefPB.Enable( bEnable != false && nCount > 1 && m_aLanguageLB.GetSelectEntryCount() == 1 );
297 
298 	return 1;
299 }
300 
301 // class SetDefaultLanguageDialog -----------------------------------------------
302 
SetDefaultLanguageDialog(Window * pParent,LocalizationMgr * _pLMgr)303 SetDefaultLanguageDialog::SetDefaultLanguageDialog( Window* pParent, LocalizationMgr* _pLMgr ) :
304 
305 	ModalDialog( pParent, IDEResId( RID_DLG_SETDEF_LANGUAGE ) ),
306 
307 	m_aLanguageFT	( this, IDEResId( FT_DEF_LANGUAGE ) ),
308 	m_pLanguageLB	( new SvxLanguageBox( this, IDEResId( LB_DEF_LANGUAGE ) ) ),
309 	m_pCheckLangLB	( NULL ),
310 	m_aInfoFT		( this, IDEResId( FT_DEF_INFO ) ),
311 	m_aBtnLine		( this, IDEResId( FL_DEF_BUTTONS ) ),
312 	m_aOKBtn		( this, IDEResId( PB_DEF_OK ) ),
313 	m_aCancelBtn	( this, IDEResId( PB_DEF_CANCEL ) ),
314 	m_aHelpBtn		( this, IDEResId( PB_DEF_HELP ) ),
315 
316 	m_pLocalizationMgr( _pLMgr )
317 
318 {
319 	if ( m_pLocalizationMgr->isLibraryLocalized() )
320 	{
321 		// change to "Add Interface Language" mode
322 		SetHelpId( HID_BASICIDE_ADDNEW_LANGUAGE );
323 	    m_pCheckLangLB = new SvxCheckListBox( this, IDEResId( LB_ADD_LANGUAGE ) );
324 		SetText( String( IDEResId( STR_ADDLANG_TITLE ) ) );
325 		m_aLanguageFT.SetText( String( IDEResId( STR_ADDLANG_LABEL ) ) );
326 		m_aInfoFT.SetText( String( IDEResId( STR_ADDLANG_INFO ) ) );
327 	}
328 
329 	FreeResource();
330 
331 	FillLanguageBox();
332 	CalcInfoSize();
333 }
334 
~SetDefaultLanguageDialog()335 SetDefaultLanguageDialog::~SetDefaultLanguageDialog()
336 {
337 	delete m_pLanguageLB;
338 	delete m_pCheckLangLB;
339 }
340 
FillLanguageBox()341 void SetDefaultLanguageDialog::FillLanguageBox()
342 {
343 	// fill list with all languages
344 	m_pLanguageLB->SetLanguageList( LANG_LIST_ALL, sal_False );
345 	// remove the already localized languages
346 	Sequence< Locale > aLocaleSeq = m_pLocalizationMgr->getStringResourceManager()->getLocales();
347 	const Locale* pLocale = aLocaleSeq.getConstArray();
348 	sal_Int32 i, nCount = aLocaleSeq.getLength();
349 	for ( i = 0;  i < nCount;  ++i )
350 		m_pLanguageLB->RemoveLanguage( SvxLocaleToLanguage( pLocale[i] ) );
351 
352 	// fill checklistbox if not in default mode
353 	if ( m_pLocalizationMgr->isLibraryLocalized() )
354 	{
355 		sal_uInt16 j, nCount_ = m_pLanguageLB->GetEntryCount();
356 		for ( j = 0;  j < nCount_;  ++j )
357 		{
358             m_pCheckLangLB->InsertEntry(
359 				m_pLanguageLB->GetEntry(j), LISTBOX_APPEND, m_pLanguageLB->GetEntryData(j) );
360         }
361 		delete m_pLanguageLB;
362 		m_pLanguageLB = NULL;
363 	}
364 	else
365 		// preselect current UI language
366 		m_pLanguageLB->SelectLanguage( Application::GetSettings().GetUILanguage() );
367 }
368 
CalcInfoSize()369 void SetDefaultLanguageDialog::CalcInfoSize()
370 {
371 	String sInfoStr = m_aInfoFT.GetText();
372 	long nInfoWidth = m_aInfoFT.GetSizePixel().Width();
373 	long nLongWord = getLongestWordWidth( sInfoStr, m_aInfoFT );
374 	long nTxtWidth = m_aInfoFT.GetCtrlTextWidth( sInfoStr ) + nLongWord;
375 	long nLines = ( nTxtWidth / nInfoWidth ) + 1;
376 	if ( nLines > INFO_LINES_COUNT )
377 	{
378 		Size aFTSize = m_aLanguageFT.GetSizePixel();
379 		Size aSize = m_aInfoFT.GetSizePixel();
380 		long nNewHeight = aFTSize.Height() * nLines;
381 		long nDelta = nNewHeight - aSize.Height();
382 		aSize.Height() = nNewHeight;
383 		m_aInfoFT.SetSizePixel( aSize );
384 
385 		Window* pWin = ( m_pLanguageLB != NULL ) ? dynamic_cast< Window* >( m_pLanguageLB )
386 												 : dynamic_cast< Window* >( m_pCheckLangLB );
387 		aSize = pWin->GetSizePixel();
388 		aSize.Height() -= nDelta;
389 		pWin->SetSizePixel( aSize );
390 
391         Point aNewPos = m_aInfoFT.GetPosPixel();
392         aNewPos.Y() -= nDelta;
393         m_aInfoFT.SetPosPixel( aNewPos );
394 	}
395 }
396 
GetLocales() const397 Sequence< Locale > SetDefaultLanguageDialog::GetLocales() const
398 {
399 	bool bNotLocalized = !m_pLocalizationMgr->isLibraryLocalized();
400 	sal_Int32 nSize = bNotLocalized ? 1 : m_pCheckLangLB->GetCheckedEntryCount();
401 	Sequence< Locale > aLocaleSeq( nSize );
402 	if ( bNotLocalized )
403 	{
404 		Locale aLocale;
405 		SvxLanguageToLocale( aLocale, m_pLanguageLB->GetSelectLanguage() );
406 		aLocaleSeq[0] = aLocale;
407 	}
408 	else
409 	{
410 		sal_uInt16 i, nCount = static_cast< sal_uInt16 >( m_pCheckLangLB->GetEntryCount() );
411 		sal_Int32 j = 0;
412 		for ( i = 0; i < nCount; ++i )
413 		{
414 			if ( m_pCheckLangLB->IsChecked(i) )
415 			{
416 				LanguageType eType = LanguageType( (sal_uLong)m_pCheckLangLB->GetEntryData(i) );
417 				Locale aLocale;
418 				SvxLanguageToLocale( aLocale, eType );
419 				aLocaleSeq[j++] = aLocale;
420 			}
421 		}
422 		DBG_ASSERT( nSize == j, "SetDefaultLanguageDialog::GetLocales(): invalid indexes" );
423 	}
424 	return aLocaleSeq;
425 }
426 
427