merge the formfield patch from ooo-build
[ooovba.git] / basctl / source / basicide / basicbox.cxx
blobb5e4503f07a11d3c6900057c444f99df754a7064
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: basicbox.cxx,v $
10 * $Revision: 1.16 $
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 <ide_pch.hxx>
37 #include <basidesh.hrc>
38 #include <basidesh.hxx>
39 #include <basobj.hxx>
41 #include <basicbox.hxx>
42 #include <iderid.hxx>
43 #include <iderdll.hxx>
44 #include <bastypes.hxx>
45 #include "bastype2.hxx"
46 #include "basdoc.hxx"
48 #include "localizationmgr.hxx"
49 #include "managelang.hxx"
50 #include "dlgresid.hrc"
51 #include <svx/unolingu.hxx>
53 #include <svtools/langtab.hxx>
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::lang;
58 using namespace ::com::sun::star::uno;
60 SFX_IMPL_TOOLBOX_CONTROL( LibBoxControl, SfxStringItem );
62 LibBoxControl::LibBoxControl( USHORT nSlotId, USHORT nId, ToolBox& rTbx )
63 : SfxToolBoxControl( nSlotId, nId, rTbx )
69 LibBoxControl::~LibBoxControl()
75 void LibBoxControl::StateChanged( USHORT, SfxItemState eState, const SfxPoolItem* pState )
77 BasicLibBox* pBox = (BasicLibBox*) GetToolBox().GetItemWindow( GetId() );
79 DBG_ASSERT( pBox, "Box not found" );
80 if ( !pBox )
81 return;
83 if ( eState != SFX_ITEM_AVAILABLE )
84 pBox->Disable();
85 else
87 pBox->Enable();
89 if ( pState->ISA(SfxStringItem) )
90 pBox->Update( (const SfxStringItem*)pState );
91 else
92 pBox->Update( NULL );
98 Window* LibBoxControl::CreateItemWindow( Window *pParent )
100 return new BasicLibBox( pParent, m_xFrame );
103 //=============================================================================
104 //= DocListenerBox
105 //=============================================================================
107 DocListenerBox::DocListenerBox( Window* pParent )
108 :ListBox( pParent, WinBits( WB_BORDER | WB_DROPDOWN ) )
109 ,m_aNotifier( *this )
113 DocListenerBox::~DocListenerBox()
115 m_aNotifier.dispose();
118 void DocListenerBox::onDocumentCreated( const ScriptDocument& /*_rDocument*/ )
120 FillBox();
123 void DocListenerBox::onDocumentOpened( const ScriptDocument& /*_rDocument*/ )
125 FillBox();
128 void DocListenerBox::onDocumentSave( const ScriptDocument& /*_rDocument*/ )
130 // not interested in
133 void DocListenerBox::onDocumentSaveDone( const ScriptDocument& /*_rDocument*/ )
135 // not interested in
138 void DocListenerBox::onDocumentSaveAs( const ScriptDocument& /*_rDocument*/ )
140 // not interested in
143 void DocListenerBox::onDocumentSaveAsDone( const ScriptDocument& /*_rDocument*/ )
145 FillBox();
148 void DocListenerBox::onDocumentClosed( const ScriptDocument& /*_rDocument*/ )
150 if ( SFX_APP()->IsInBasicCall() )
151 // Nicht wenn Office beendet
152 FillBox();
155 void DocListenerBox::onDocumentTitleChanged( const ScriptDocument& /*_rDocument*/ )
157 // not interested in
160 void DocListenerBox::onDocumentModeChanged( const ScriptDocument& /*_rDocument*/ )
162 // not interested in
165 //=============================================================================
166 //= BasicLibBox
167 //=============================================================================
168 BasicLibBox::BasicLibBox( Window* pParent, const uno::Reference< frame::XFrame >& rFrame ) :
169 DocListenerBox( pParent ),
170 m_xFrame( rFrame )
172 FillBox();
173 bIgnoreSelect = TRUE; // Select von 0 noch nicht weiterleiten
174 bFillBox = TRUE;
175 SelectEntryPos( 0 );
176 aCurText = GetEntry( 0 );
177 SetSizePixel( Size( 250, 200 ) );
178 bIgnoreSelect = FALSE;
183 __EXPORT BasicLibBox::~BasicLibBox()
185 ClearBox();
188 void __EXPORT BasicLibBox::Update( const SfxStringItem* pItem )
190 // Immer auf dem laufenden sein...
191 // if ( !pItem || !pItem->GetValue().Len() )
192 FillBox();
194 if ( pItem )
196 aCurText = pItem->GetValue();
197 if ( aCurText.Len() == 0 )
198 aCurText = String( IDEResId( RID_STR_ALL ) );
201 if ( GetSelectEntry() != aCurText )
202 SelectEntry( aCurText );
205 void __EXPORT BasicLibBox::ReleaseFocus()
207 SfxViewShell* pCurSh = SfxViewShell::Current();
208 DBG_ASSERT( pCurSh, "Current ViewShell not found!" );
210 if ( pCurSh )
212 Window* pShellWin = pCurSh->GetWindow();
213 if ( !pShellWin ) // sonst werde ich ihn nicht los
214 pShellWin = Application::GetDefDialogParent();
216 pShellWin->GrabFocus();
220 void BasicLibBox::FillBox()
222 SetUpdateMode( FALSE );
223 bIgnoreSelect = TRUE;
225 aCurText = GetSelectEntry();
227 SelectEntryPos( 0 );
228 ClearBox();
230 // create list box entries
231 USHORT nPos = InsertEntry( String( IDEResId( RID_STR_ALL ) ), LISTBOX_APPEND );
232 SetEntryData( nPos, new BasicLibEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_UNKNOWN, String() ) );
233 InsertEntries( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
234 InsertEntries( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
236 ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
237 for ( ScriptDocuments::const_iterator doc = aDocuments.begin();
238 doc != aDocuments.end();
239 ++doc
242 InsertEntries( *doc, LIBRARY_LOCATION_DOCUMENT );
245 SetUpdateMode( TRUE );
247 SelectEntry( aCurText );
248 if ( !GetSelectEntryCount() )
250 SelectEntryPos( GetEntryCount() ); // gibst es nicht => leer?
251 aCurText = GetSelectEntry();
253 bIgnoreSelect = FALSE;
256 void BasicLibBox::InsertEntries( const ScriptDocument& rDocument, LibraryLocation eLocation )
258 // get a sorted list of library names
259 Sequence< ::rtl::OUString > aLibNames = rDocument.getLibraryNames();
260 sal_Int32 nLibCount = aLibNames.getLength();
261 const ::rtl::OUString* pLibNames = aLibNames.getConstArray();
263 for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
265 String aLibName = pLibNames[ i ];
266 if ( eLocation == rDocument.getLibraryLocation( aLibName ) )
268 String aName( rDocument.getTitle( eLocation ) );
269 String aEntryText( CreateMgrAndLibStr( aName, aLibName ) );
270 USHORT nPos = InsertEntry( aEntryText, LISTBOX_APPEND );
271 SetEntryData( nPos, new BasicLibEntry( rDocument, eLocation, aLibName ) );
276 long BasicLibBox::PreNotify( NotifyEvent& rNEvt )
278 long nDone = 0;
279 if( rNEvt.GetType() == EVENT_KEYINPUT )
281 KeyEvent aKeyEvt = *rNEvt.GetKeyEvent();
282 USHORT nKeyCode = aKeyEvt.GetKeyCode().GetCode();
283 switch( nKeyCode )
285 case KEY_RETURN:
287 NotifyIDE();
288 nDone = 1;
290 break;
292 case KEY_ESCAPE:
294 SelectEntry( aCurText );
295 ReleaseFocus();
296 nDone = 1;
298 break;
301 else if( rNEvt.GetType() == EVENT_GETFOCUS )
303 if ( bFillBox )
305 FillBox();
306 bFillBox = FALSE;
309 else if( rNEvt.GetType() == EVENT_LOSEFOCUS )
311 if ( !HasChildPathFocus( TRUE ) )
313 bIgnoreSelect = TRUE;
314 bFillBox = TRUE;
318 return nDone ? nDone : ListBox::PreNotify( rNEvt );
321 void __EXPORT BasicLibBox::Select()
323 if ( !IsTravelSelect() )
325 if ( !bIgnoreSelect )
326 NotifyIDE();
327 else
328 SelectEntry( aCurText ); // Seit 306... (Select nach Escape)
332 void BasicLibBox::NotifyIDE()
334 USHORT nSelPos = GetSelectEntryPos();
335 BasicLibEntry* pEntry = (BasicLibEntry*)GetEntryData( nSelPos );
336 if ( pEntry )
338 ScriptDocument aDocument( pEntry->GetDocument() );
339 SfxUsrAnyItem aDocumentItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, uno::makeAny( aDocument.getDocumentOrNull() ) );
340 String aLibName = pEntry->GetLibName();
341 SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
342 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
343 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
344 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
345 if ( pDispatcher )
347 pDispatcher->Execute( SID_BASICIDE_LIBSELECTED,
348 SFX_CALLMODE_SYNCHRON, &aDocumentItem, &aLibNameItem, 0L );
351 ReleaseFocus();
354 void BasicLibBox::ClearBox()
356 USHORT nCount = GetEntryCount();
357 for ( USHORT i = 0; i < nCount; ++i )
359 BasicLibEntry* pEntry = (BasicLibEntry*)GetEntryData( i );
360 delete pEntry;
362 ListBox::Clear();
365 // class LanguageBoxControl ----------------------------------------------
367 SFX_IMPL_TOOLBOX_CONTROL( LanguageBoxControl, SfxStringItem );
369 LanguageBoxControl::LanguageBoxControl( USHORT nSlotId, USHORT nId, ToolBox& rTbx )
370 : SfxToolBoxControl( nSlotId, nId, rTbx )
374 LanguageBoxControl::~LanguageBoxControl()
378 void LanguageBoxControl::StateChanged( USHORT _nID, SfxItemState _eState, const SfxPoolItem* _pItem )
380 (void)_nID;
382 BasicLanguageBox* pBox = (BasicLanguageBox*)( GetToolBox().GetItemWindow( GetId() ) );
384 if ( pBox )
386 if ( _eState != SFX_ITEM_AVAILABLE )
387 pBox->Disable();
388 else
390 pBox->Enable();
391 if ( _pItem->ISA(SfxStringItem) )
392 pBox->Update( (const SfxStringItem*)_pItem );
393 else
394 pBox->Update( NULL );
399 Window* LanguageBoxControl::CreateItemWindow( Window *pParent )
401 return new BasicLanguageBox( pParent );
404 // class BasicLanguageBox ------------------------------------------------
406 BasicLanguageBox::BasicLanguageBox( Window* pParent ) :
408 DocListenerBox( pParent ),
410 m_sNotLocalizedStr( IDEResId( RID_STR_TRANSLATION_NOTLOCALIZED ) ),
411 m_sDefaultLanguageStr( IDEResId( RID_STR_TRANSLATION_DEFAULT ) ),
413 m_bIgnoreSelect( false )
416 SetSizePixel( Size( 210, 200 ) );
418 FillBox();
421 BasicLanguageBox::~BasicLanguageBox()
423 ClearBox();
426 void BasicLanguageBox::FillBox()
428 SetUpdateMode( FALSE );
429 m_bIgnoreSelect = true;
430 m_sCurrentText = GetSelectEntry();
431 ClearBox();
433 LocalizationMgr* pCurMgr = IDE_DLL()->GetShell()->GetCurLocalizationMgr();
434 if ( pCurMgr->isLibraryLocalized() )
436 Enable();
437 SvtLanguageTable aLangTable;
438 Locale aDefaultLocale = pCurMgr->getStringResourceManager()->getDefaultLocale();
439 Locale aCurrentLocale = pCurMgr->getStringResourceManager()->getCurrentLocale();
440 Sequence< Locale > aLocaleSeq = pCurMgr->getStringResourceManager()->getLocales();
441 const Locale* pLocale = aLocaleSeq.getConstArray();
442 INT32 i, nCount = aLocaleSeq.getLength();
443 USHORT nSelPos = LISTBOX_ENTRY_NOTFOUND;
444 for ( i = 0; i < nCount; ++i )
446 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
447 bool bIsCurrent = localesAreEqual( aCurrentLocale, pLocale[i] );
448 LanguageType eLangType = SvxLocaleToLanguage( pLocale[i] );
449 String sLanguage = aLangTable.GetString( eLangType );
450 if ( bIsDefault )
452 sLanguage += ' ';
453 sLanguage += m_sDefaultLanguageStr;
455 USHORT nPos = InsertEntry( sLanguage );
456 SetEntryData( nPos, new LanguageEntry( sLanguage, pLocale[i], bIsDefault ) );
458 if ( bIsCurrent )
459 nSelPos = nPos;
462 if ( nSelPos != LISTBOX_ENTRY_NOTFOUND )
464 SelectEntryPos( nSelPos );
465 m_sCurrentText = GetSelectEntry();
468 else
470 InsertEntry( m_sNotLocalizedStr );
471 SelectEntryPos(0);
472 Disable();
475 SetUpdateMode( TRUE );
476 m_bIgnoreSelect = false;
479 void BasicLanguageBox::ClearBox()
481 USHORT nCount = GetEntryCount();
482 for ( USHORT i = 0; i < nCount; ++i )
484 LanguageEntry* pEntry = (LanguageEntry*)GetEntryData(i);
485 delete pEntry;
487 ListBox::Clear();
490 void BasicLanguageBox::SetLanguage()
492 LanguageEntry* pEntry = (LanguageEntry*)GetEntryData( GetSelectEntryPos() );
493 if ( pEntry )
494 IDE_DLL()->GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale( pEntry->m_aLocale );
497 void BasicLanguageBox::Select()
499 if ( !m_bIgnoreSelect )
500 SetLanguage();
501 else
502 SelectEntry( m_sCurrentText ); // Select after Escape
505 long BasicLanguageBox::PreNotify( NotifyEvent& rNEvt )
507 long nDone = 0;
508 if( rNEvt.GetType() == EVENT_KEYINPUT )
510 USHORT nKeyCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
511 switch( nKeyCode )
513 case KEY_RETURN:
515 SetLanguage();
516 nDone = 1;
518 break;
520 case KEY_ESCAPE:
522 SelectEntry( m_sCurrentText );
523 nDone = 1;
525 break;
528 else if( rNEvt.GetType() == EVENT_GETFOCUS )
531 else if( rNEvt.GetType() == EVENT_LOSEFOCUS )
535 return nDone ? nDone : ListBox::PreNotify( rNEvt );
538 void BasicLanguageBox::Update( const SfxStringItem* pItem )
540 FillBox();
542 if ( pItem && pItem->GetValue().Len() > 0 )
544 m_sCurrentText = pItem->GetValue();
545 if ( GetSelectEntry() != m_sCurrentText )
546 SelectEntry( m_sCurrentText );