xref: /trunk/main/basctl/source/basicide/basicbox.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 <ide_pch.hxx>
28 
29 
30 #include <basidesh.hrc>
31 #include <basidesh.hxx>
32 #include <basobj.hxx>
33 
34 #include <basicbox.hxx>
35 #include <iderid.hxx>
36 #include <iderdll.hxx>
37 #include <bastypes.hxx>
38 #include "bastype2.hxx"
39 #include "basdoc.hxx"
40 
41 #include "localizationmgr.hxx"
42 #include "managelang.hxx"
43 #include "dlgresid.hrc"
44 #include <editeng/unolingu.hxx>
45 
46 #include <svtools/langtab.hxx>
47 
48 
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::uno;
52 
53 SFX_IMPL_TOOLBOX_CONTROL( LibBoxControl, SfxStringItem );
54 
LibBoxControl(sal_uInt16 nSlotId,sal_uInt16 nId,ToolBox & rTbx)55 LibBoxControl::LibBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
56 	: SfxToolBoxControl( nSlotId, nId, rTbx )
57 {
58 }
59 
60 
61 
~LibBoxControl()62 LibBoxControl::~LibBoxControl()
63 {
64 }
65 
66 
67 
StateChanged(sal_uInt16,SfxItemState eState,const SfxPoolItem * pState)68 void LibBoxControl::StateChanged( sal_uInt16, SfxItemState eState, const SfxPoolItem* pState )
69 {
70 	BasicLibBox* pBox = (BasicLibBox*) GetToolBox().GetItemWindow( GetId() );
71 
72 	DBG_ASSERT( pBox, "Box not found" );
73 	if ( !pBox )
74 		return;
75 
76 	if ( eState != SFX_ITEM_AVAILABLE )
77 		pBox->Disable();
78 	else
79 	{
80 		pBox->Enable();
81 
82 		if ( pState->ISA(SfxStringItem) )
83 			pBox->Update( (const SfxStringItem*)pState );
84 		else
85 			pBox->Update( NULL );
86 	}
87 }
88 
89 
90 
CreateItemWindow(Window * pParent)91 Window* LibBoxControl::CreateItemWindow( Window *pParent )
92 {
93 	return new BasicLibBox( pParent, m_xFrame );
94 }
95 
96 //=============================================================================
97 //= DocListenerBox
98 //=============================================================================
99 
DocListenerBox(Window * pParent)100 DocListenerBox::DocListenerBox( Window* pParent )
101     :ListBox( pParent, WinBits( WB_BORDER | WB_DROPDOWN ) )
102     ,m_aNotifier( *this )
103 {
104 }
105 
~DocListenerBox()106 DocListenerBox::~DocListenerBox()
107 {
108     m_aNotifier.dispose();
109 }
110 
onDocumentCreated(const ScriptDocument &)111 void DocListenerBox::onDocumentCreated( const ScriptDocument& /*_rDocument*/ )
112 {
113     FillBox();
114 }
115 
onDocumentOpened(const ScriptDocument &)116 void DocListenerBox::onDocumentOpened( const ScriptDocument& /*_rDocument*/ )
117 {
118     FillBox();
119 }
120 
onDocumentSave(const ScriptDocument &)121 void DocListenerBox::onDocumentSave( const ScriptDocument& /*_rDocument*/ )
122 {
123     // not interested in
124 }
125 
onDocumentSaveDone(const ScriptDocument &)126 void DocListenerBox::onDocumentSaveDone( const ScriptDocument& /*_rDocument*/ )
127 {
128     // not interested in
129 }
130 
onDocumentSaveAs(const ScriptDocument &)131 void DocListenerBox::onDocumentSaveAs( const ScriptDocument& /*_rDocument*/ )
132 {
133     // not interested in
134 }
135 
onDocumentSaveAsDone(const ScriptDocument &)136 void DocListenerBox::onDocumentSaveAsDone( const ScriptDocument& /*_rDocument*/ )
137 {
138     FillBox();
139 }
140 
onDocumentClosed(const ScriptDocument &)141 void DocListenerBox::onDocumentClosed( const ScriptDocument& /*_rDocument*/ )
142 {
143     FillBox();
144 }
145 
onDocumentTitleChanged(const ScriptDocument &)146 void DocListenerBox::onDocumentTitleChanged( const ScriptDocument& /*_rDocument*/ )
147 {
148     // not interested in
149 }
150 
onDocumentModeChanged(const ScriptDocument &)151 void DocListenerBox::onDocumentModeChanged( const ScriptDocument& /*_rDocument*/ )
152 {
153     // not interested in
154 }
155 
156 //=============================================================================
157 //= BasicLibBox
158 //=============================================================================
BasicLibBox(Window * pParent,const uno::Reference<frame::XFrame> & rFrame)159 BasicLibBox::BasicLibBox( Window* pParent, const uno::Reference< frame::XFrame >& rFrame ) :
160     DocListenerBox( pParent ),
161     m_xFrame( rFrame )
162 {
163 	FillBox();
164 	bIgnoreSelect = sal_True;	// Select von 0 noch nicht weiterleiten
165 	bFillBox = sal_True;
166 	SelectEntryPos( 0 );
167 	aCurText = GetEntry( 0 );
168 	SetSizePixel( Size( 250, 200 ) );
169     bIgnoreSelect = sal_False;
170 }
171 
172 
173 
~BasicLibBox()174 __EXPORT BasicLibBox::~BasicLibBox()
175 {
176     ClearBox();
177 }
178 
Update(const SfxStringItem * pItem)179 void __EXPORT BasicLibBox::Update( const SfxStringItem* pItem )
180 {
181 	// Immer auf dem laufenden sein...
182 //	if ( !pItem  || !pItem->GetValue().Len() )
183 		FillBox();
184 
185 	if ( pItem )
186 	{
187 		aCurText = pItem->GetValue();
188 		if ( aCurText.Len() == 0 )
189 			aCurText = String( IDEResId( RID_STR_ALL ) );
190 	}
191 
192 	if ( GetSelectEntry() != aCurText )
193 		SelectEntry( aCurText );
194 }
195 
ReleaseFocus()196 void __EXPORT BasicLibBox::ReleaseFocus()
197 {
198 	SfxViewShell* pCurSh = SfxViewShell::Current();
199 	DBG_ASSERT( pCurSh, "Current ViewShell not found!" );
200 
201 	if ( pCurSh )
202 	{
203 		Window* pShellWin = pCurSh->GetWindow();
204 		if ( !pShellWin )		// sonst werde ich ihn nicht los
205 			pShellWin = Application::GetDefDialogParent();
206 
207 		pShellWin->GrabFocus();
208 	}
209 }
210 
FillBox()211 void BasicLibBox::FillBox()
212 {
213 	SetUpdateMode( sal_False );
214 	bIgnoreSelect = sal_True;
215 
216 	aCurText = GetSelectEntry();
217 
218 	SelectEntryPos( 0 );
219 	ClearBox();
220 
221     // create list box entries
222     sal_uInt16 nPos = InsertEntry( String( IDEResId( RID_STR_ALL ) ), LISTBOX_APPEND );
223     SetEntryData( nPos, new BasicLibEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_UNKNOWN, String() ) );
224     InsertEntries( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
225     InsertEntries( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
226 
227     ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
228     for (   ScriptDocuments::const_iterator doc = aDocuments.begin();
229             doc != aDocuments.end();
230             ++doc
231         )
232     {
233         InsertEntries( *doc, LIBRARY_LOCATION_DOCUMENT );
234     }
235 
236 	SetUpdateMode( sal_True );
237 
238 	SelectEntry( aCurText );
239 	if ( !GetSelectEntryCount() )
240 	{
241 		SelectEntryPos( GetEntryCount() );	// gibst es nicht => leer?
242 		aCurText = GetSelectEntry();
243 	}
244 	bIgnoreSelect = sal_False;
245 }
246 
InsertEntries(const ScriptDocument & rDocument,LibraryLocation eLocation)247 void BasicLibBox::InsertEntries( const ScriptDocument& rDocument, LibraryLocation eLocation )
248 {
249     // get a sorted list of library names
250     Sequence< ::rtl::OUString > aLibNames = rDocument.getLibraryNames();
251     sal_Int32 nLibCount = aLibNames.getLength();
252     const ::rtl::OUString* pLibNames = aLibNames.getConstArray();
253 
254     for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
255     {
256         String aLibName = pLibNames[ i ];
257         if ( eLocation == rDocument.getLibraryLocation( aLibName ) )
258         {
259             String aName( rDocument.getTitle( eLocation ) );
260             String aEntryText( CreateMgrAndLibStr( aName, aLibName ) );
261             sal_uInt16 nPos = InsertEntry( aEntryText, LISTBOX_APPEND );
262             SetEntryData( nPos, new BasicLibEntry( rDocument, eLocation, aLibName ) );
263         }
264     }
265 }
266 
PreNotify(NotifyEvent & rNEvt)267 long BasicLibBox::PreNotify( NotifyEvent& rNEvt )
268 {
269 	long nDone = 0;
270 	if( rNEvt.GetType() == EVENT_KEYINPUT )
271 	{
272 		KeyEvent aKeyEvt = *rNEvt.GetKeyEvent();
273 		sal_uInt16 nKeyCode = aKeyEvt.GetKeyCode().GetCode();
274 		switch( nKeyCode )
275 		{
276 			case KEY_RETURN:
277 			{
278 				NotifyIDE();
279 				nDone = 1;
280 			}
281 			break;
282 
283 			case KEY_ESCAPE:
284 			{
285 				SelectEntry( aCurText );
286 				ReleaseFocus();
287 				nDone = 1;
288 			}
289 			break;
290 		}
291 	}
292 	else if( rNEvt.GetType() == EVENT_GETFOCUS )
293 	{
294 		if ( bFillBox )
295 		{
296 			FillBox();
297 			bFillBox = sal_False;
298 		}
299 	}
300 	else if( rNEvt.GetType() == EVENT_LOSEFOCUS )
301 	{
302 		if ( !HasChildPathFocus( sal_True ) )
303 		{
304 			bIgnoreSelect = sal_True;
305 			bFillBox = sal_True;
306 		}
307 	}
308 
309 	return nDone ? nDone : ListBox::PreNotify( rNEvt );
310 }
311 
Select()312 void __EXPORT BasicLibBox::Select()
313 {
314 	if ( !IsTravelSelect() )
315 	{
316 		if ( !bIgnoreSelect )
317 			NotifyIDE();
318 		else
319 			SelectEntry( aCurText );	// Seit 306... (Select nach Escape)
320 	}
321 }
322 
NotifyIDE()323 void BasicLibBox::NotifyIDE()
324 {
325     sal_uInt16 nSelPos = GetSelectEntryPos();
326     BasicLibEntry* pEntry = (BasicLibEntry*)GetEntryData( nSelPos );
327     if ( pEntry )
328     {
329         ScriptDocument aDocument( pEntry->GetDocument() );
330         SfxUsrAnyItem aDocumentItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, uno::makeAny( aDocument.getDocumentOrNull() ) );
331         String aLibName = pEntry->GetLibName();
332         SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
333         BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
334         SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
335         SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
336         if ( pDispatcher )
337         {
338             pDispatcher->Execute( SID_BASICIDE_LIBSELECTED,
339                                   SFX_CALLMODE_SYNCHRON, &aDocumentItem, &aLibNameItem, 0L );
340         }
341     }
342     ReleaseFocus();
343 }
344 
ClearBox()345 void BasicLibBox::ClearBox()
346 {
347     sal_uInt16 nCount = GetEntryCount();
348     for ( sal_uInt16 i = 0; i < nCount; ++i )
349     {
350         BasicLibEntry* pEntry = (BasicLibEntry*)GetEntryData( i );
351         delete pEntry;
352     }
353     ListBox::Clear();
354 }
355 
356 // class LanguageBoxControl ----------------------------------------------
357 
358 SFX_IMPL_TOOLBOX_CONTROL( LanguageBoxControl, SfxStringItem );
359 
LanguageBoxControl(sal_uInt16 nSlotId,sal_uInt16 nId,ToolBox & rTbx)360 LanguageBoxControl::LanguageBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
361 	: SfxToolBoxControl( nSlotId, nId, rTbx )
362 {
363 }
364 
~LanguageBoxControl()365 LanguageBoxControl::~LanguageBoxControl()
366 {
367 }
368 
StateChanged(sal_uInt16 _nID,SfxItemState _eState,const SfxPoolItem * _pItem)369 void LanguageBoxControl::StateChanged( sal_uInt16 _nID, SfxItemState _eState, const SfxPoolItem* _pItem )
370 {
371 	(void)_nID;
372 
373 	BasicLanguageBox* pBox = (BasicLanguageBox*)( GetToolBox().GetItemWindow( GetId() ) );
374 
375 	if ( pBox )
376 	{
377 		if ( _eState != SFX_ITEM_AVAILABLE )
378 			pBox->Disable();
379 		else
380 		{
381 			pBox->Enable();
382 			if ( _pItem->ISA(SfxStringItem) )
383 				pBox->Update( (const SfxStringItem*)_pItem );
384 			else
385 				pBox->Update( NULL );
386 		}
387 	}
388 }
389 
CreateItemWindow(Window * pParent)390 Window* LanguageBoxControl::CreateItemWindow( Window *pParent )
391 {
392 	return new BasicLanguageBox( pParent );
393 }
394 
395 // class BasicLanguageBox ------------------------------------------------
396 
BasicLanguageBox(Window * pParent)397 BasicLanguageBox::BasicLanguageBox( Window* pParent ) :
398 
399 	DocListenerBox( pParent ),
400 
401 	m_sNotLocalizedStr( IDEResId( RID_STR_TRANSLATION_NOTLOCALIZED ) ),
402 	m_sDefaultLanguageStr( IDEResId( RID_STR_TRANSLATION_DEFAULT ) ),
403 
404 	m_bIgnoreSelect( false )
405 
406 {
407 	SetSizePixel( Size( 210, 200 ) );
408 
409 	FillBox();
410 }
411 
~BasicLanguageBox()412 BasicLanguageBox::~BasicLanguageBox()
413 {
414 	ClearBox();
415 }
416 
FillBox()417 void BasicLanguageBox::FillBox()
418 {
419 	SetUpdateMode( sal_False );
420 	m_bIgnoreSelect = true;
421 	m_sCurrentText = GetSelectEntry();
422 	ClearBox();
423 
424 	LocalizationMgr* pCurMgr = IDE_DLL()->GetShell()->GetCurLocalizationMgr();
425 	if ( pCurMgr->isLibraryLocalized() )
426 	{
427 		Enable();
428 		SvtLanguageTable aLangTable;
429 		Locale aDefaultLocale = pCurMgr->getStringResourceManager()->getDefaultLocale();
430 		Locale aCurrentLocale = pCurMgr->getStringResourceManager()->getCurrentLocale();
431 		Sequence< Locale > aLocaleSeq = pCurMgr->getStringResourceManager()->getLocales();
432 		const Locale* pLocale = aLocaleSeq.getConstArray();
433 		sal_Int32 i, nCount = aLocaleSeq.getLength();
434 		sal_uInt16 nSelPos = LISTBOX_ENTRY_NOTFOUND;
435 		for ( i = 0;  i < nCount;  ++i )
436 		{
437 			bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
438 			bool bIsCurrent = localesAreEqual( aCurrentLocale, pLocale[i] );
439 			LanguageType eLangType = SvxLocaleToLanguage( pLocale[i] );
440 			String sLanguage = aLangTable.GetString( eLangType );
441 			if ( bIsDefault )
442 			{
443 				sLanguage += ' ';
444 				sLanguage += m_sDefaultLanguageStr;
445 			}
446 			sal_uInt16 nPos = InsertEntry( sLanguage );
447 			SetEntryData( nPos, new LanguageEntry( sLanguage, pLocale[i], bIsDefault ) );
448 
449 			if ( bIsCurrent )
450 				nSelPos = nPos;
451 		}
452 
453 		if ( nSelPos != LISTBOX_ENTRY_NOTFOUND )
454 		{
455 			SelectEntryPos( nSelPos );
456 			m_sCurrentText = GetSelectEntry();
457 		}
458 	}
459 	else
460 	{
461 		InsertEntry( m_sNotLocalizedStr );
462 		SelectEntryPos(0);
463 		Disable();
464 	}
465 
466 	SetUpdateMode( sal_True );
467 	m_bIgnoreSelect = false;
468 }
469 
ClearBox()470 void BasicLanguageBox::ClearBox()
471 {
472     sal_uInt16 nCount = GetEntryCount();
473     for ( sal_uInt16 i = 0; i < nCount; ++i )
474     {
475         LanguageEntry* pEntry = (LanguageEntry*)GetEntryData(i);
476         delete pEntry;
477     }
478     ListBox::Clear();
479 }
480 
SetLanguage()481 void BasicLanguageBox::SetLanguage()
482 {
483     LanguageEntry* pEntry = (LanguageEntry*)GetEntryData( GetSelectEntryPos() );
484 	if ( pEntry )
485 		IDE_DLL()->GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale( pEntry->m_aLocale );
486 }
487 
Select()488 void BasicLanguageBox::Select()
489 {
490 	if ( !m_bIgnoreSelect )
491 		SetLanguage();
492 	else
493 		SelectEntry( m_sCurrentText );	// Select after Escape
494 }
495 
PreNotify(NotifyEvent & rNEvt)496 long BasicLanguageBox::PreNotify( NotifyEvent& rNEvt )
497 {
498 	long nDone = 0;
499 	if( rNEvt.GetType() == EVENT_KEYINPUT )
500 	{
501 		sal_uInt16 nKeyCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
502 		switch( nKeyCode )
503 		{
504 			case KEY_RETURN:
505 			{
506 				SetLanguage();
507 				nDone = 1;
508 			}
509 			break;
510 
511 			case KEY_ESCAPE:
512 			{
513 				SelectEntry( m_sCurrentText );
514 				nDone = 1;
515 			}
516 			break;
517 		}
518 	}
519 	else if( rNEvt.GetType() == EVENT_GETFOCUS )
520 	{
521 	}
522 	else if( rNEvt.GetType() == EVENT_LOSEFOCUS )
523 	{
524 	}
525 
526 	return nDone ? nDone : ListBox::PreNotify( rNEvt );
527 }
528 
Update(const SfxStringItem * pItem)529 void BasicLanguageBox::Update( const SfxStringItem* pItem )
530 {
531 	FillBox();
532 
533 	if ( pItem && pItem->GetValue().Len() > 0 )
534 	{
535 		m_sCurrentText = pItem->GetValue();
536 		if ( GetSelectEntry() != m_sCurrentText )
537 			SelectEntry( m_sCurrentText );
538 	}
539 }
540 
541