update dev300-m58
[ooovba.git] / basctl / source / basicide / moduldlg.cxx
blob7ec2c1337f38b19c5ffd64c1038a59560d8a99e2
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: moduldlg.cxx,v $
10 * $Revision: 1.34 $
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 <memory>
36 #include <ide_pch.hxx>
39 #include <moduldlg.hrc>
40 #include <moduldlg.hxx>
41 #include <basidesh.hrc>
42 #include <basidesh.hxx>
43 #include <bastypes.hxx>
44 #include <baside3.hxx>
45 #include <basobj.hxx>
46 #include <baside2.hrc>
47 #include <sbxitem.hxx>
48 #include <iderdll.hxx>
50 #ifndef _COM_SUN_STAR_IO_XINPUTSTREAMPROVIDER_HXX_
51 #include <com/sun/star/io/XInputStreamProvider.hpp>
52 #endif
53 #ifndef _COM_SUN_STAR_SCRIPT_XLIBRYARYCONTAINER2_HPP_
54 #include <com/sun/star/script/XLibraryContainer2.hpp>
55 #endif
56 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
57 #include <com/sun/star/resource/XStringResourceManager.hpp>
58 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
59 #include <comphelper/processfactory.hxx>
60 #include <xmlscript/xmldlg_imexp.hxx>
62 #include "localizationmgr.hxx"
63 #include <basic/sbx.hxx>
64 #include <tools/diagnose_ex.h>
66 using namespace ::com::sun::star;
67 using namespace ::com::sun::star::uno;
68 using namespace ::com::sun::star::lang;
69 using namespace ::com::sun::star::resource;
72 ExtBasicTreeListBox::ExtBasicTreeListBox( Window* pParent, const ResId& rRes )
73 : BasicTreeListBox( pParent, rRes )
79 ExtBasicTreeListBox::~ExtBasicTreeListBox()
83 BOOL __EXPORT ExtBasicTreeListBox::EditingEntry( SvLBoxEntry* pEntry, Selection& )
85 BOOL bRet = FALSE;
87 if ( pEntry )
89 USHORT nDepth = GetModel()->GetDepth( pEntry );
90 if ( nDepth >= 2 )
92 BasicEntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
93 ScriptDocument aDocument( aDesc.GetDocument() );
94 ::rtl::OUString aOULibName( aDesc.GetLibName() );
95 Reference< script::XLibraryContainer2 > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
96 Reference< script::XLibraryContainer2 > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
97 if ( !( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) ) ||
98 ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) ) ) )
100 // allow editing only for libraries, which are not readonly
101 bRet = TRUE;
106 return bRet;
109 BOOL __EXPORT ExtBasicTreeListBox::EditedEntry( SvLBoxEntry* pEntry, const String& rNewText )
111 BOOL bValid = BasicIDE::IsValidSbxName( rNewText );
112 if ( !bValid )
114 ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
115 return FALSE;
118 String aCurText( GetEntryText( pEntry ) );
119 if ( aCurText == rNewText )
120 // nothing to do
121 return TRUE;
123 BasicEntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
124 ScriptDocument aDocument( aDesc.GetDocument() );
125 DBG_ASSERT( aDocument.isValid(), "ExtBasicTreeListBox::EditedEntry: no document!" );
126 if ( !aDocument.isValid() )
127 return FALSE;
128 String aLibName( aDesc.GetLibName() );
129 BasicEntryType eType( aDesc.GetType() );
131 bool bSuccess = ( eType == OBJ_TYPE_MODULE )
132 ? BasicIDE::RenameModule( this, aDocument, aLibName, aCurText, rNewText )
133 : BasicIDE::RenameDialog( this, aDocument, aLibName, aCurText, rNewText );
135 if ( !bSuccess )
136 return FALSE;
138 BasicIDE::MarkDocumentModified( aDocument );
140 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
141 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
142 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
143 if( pDispatcher )
145 SbxItem aSbxItem( SID_BASICIDE_ARG_SBX, aDocument, aLibName, rNewText, ConvertType( eType ) );
146 pDispatcher->Execute( SID_BASICIDE_SBXRENAMED,
147 SFX_CALLMODE_SYNCHRON, &aSbxItem, 0L );
150 // OV-Bug?!
151 SetEntryText( pEntry, rNewText );
152 SetCurEntry( pEntry );
153 SetCurEntry( pEntry );
154 Select( pEntry, FALSE );
155 Select( pEntry ); // damit Handler gerufen wird => Edit updaten
157 return TRUE;
161 DragDropMode __EXPORT ExtBasicTreeListBox::NotifyStartDrag( TransferDataContainer&, SvLBoxEntry* pEntry )
163 DragDropMode nMode_ = SV_DRAGDROP_NONE;
165 if ( pEntry )
167 USHORT nDepth = GetModel()->GetDepth( pEntry );
168 if ( nDepth >= 2 )
170 nMode_ = SV_DRAGDROP_CTRL_COPY;
171 BasicEntryDescriptor aDesc( GetEntryDescriptor( pEntry ) );
172 ScriptDocument aDocument( aDesc.GetDocument() );
173 ::rtl::OUString aOULibName( aDesc.GetLibName() );
174 // allow MOVE mode only for libraries, which are not readonly
175 Reference< script::XLibraryContainer2 > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
176 Reference< script::XLibraryContainer2 > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
177 if ( !( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) ) ||
178 ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) ) ) )
180 // Only allow copy for localized libraries
181 bool bAllowMove = true;
182 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) )
184 // Get StringResourceManager
185 Reference< container::XNameContainer > xDialogLib( aDocument.getLibrary( E_DIALOGS, aOULibName, TRUE ) );
186 Reference< XStringResourceManager > xSourceMgr =
187 LocalizationMgr::getStringResourceFromDialogLibrary( xDialogLib );
188 if( xSourceMgr.is() )
189 bAllowMove = ( xSourceMgr->getLocales().getLength() == 0 );
191 if( bAllowMove )
192 nMode_ |= SV_DRAGDROP_CTRL_MOVE;
197 return nMode_;
201 BOOL __EXPORT ExtBasicTreeListBox::NotifyAcceptDrop( SvLBoxEntry* pEntry )
203 // don't drop on a BasicManager (nDepth == 0)
204 USHORT nDepth = pEntry ? GetModel()->GetDepth( pEntry ) : 0;
205 BOOL bValid = nDepth ? TRUE : FALSE;
207 // don't drop in the same library
208 SvLBoxEntry* pSelected = FirstSelected();
209 if ( ( nDepth == 1 ) && ( pEntry == GetParent( pSelected ) ) )
210 bValid = FALSE;
211 else if ( ( nDepth == 2 ) && ( GetParent( pEntry ) == GetParent( pSelected ) ) )
212 bValid = FALSE;
214 // don't drop on a library, which is not loaded, readonly or password protected
215 // or which already has a module/dialog with this name
216 if ( bValid && ( nDepth > 0 ) )
218 // get source module/dialog name
219 BasicEntryDescriptor aSourceDesc( GetEntryDescriptor( pSelected ) );
220 String aSourceName( aSourceDesc.GetName() );
221 BasicEntryType eSourceType( aSourceDesc.GetType() );
223 // get target shell and target library name
224 BasicEntryDescriptor aDestDesc( GetEntryDescriptor( pEntry ) );
225 const ScriptDocument& rDestDoc( aDestDesc.GetDocument() );
226 String aDestLibName( aDestDesc.GetLibName() );
227 ::rtl::OUString aOUDestLibName( aDestLibName );
229 // check if module library is not loaded, readonly or password protected
230 Reference< script::XLibraryContainer2 > xModLibContainer( rDestDoc.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
231 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOUDestLibName ) )
233 if ( !xModLibContainer->isLibraryLoaded( aOUDestLibName ) )
234 bValid = FALSE;
236 if ( xModLibContainer->isLibraryReadOnly( aOUDestLibName ) )
237 bValid = FALSE;
239 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
240 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOUDestLibName ) && !xPasswd->isLibraryPasswordVerified( aOUDestLibName ) )
241 bValid = FALSE;
244 // check if dialog library is not loaded or readonly
245 Reference< script::XLibraryContainer2 > xDlgLibContainer( rDestDoc.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
246 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOUDestLibName ) )
248 if ( !xDlgLibContainer->isLibraryLoaded( aOUDestLibName ) )
249 bValid = FALSE;
251 if ( xDlgLibContainer->isLibraryReadOnly( aOUDestLibName ) )
252 bValid = FALSE;
255 // check, if module/dialog with this name is already existing in target library
256 if ( ( eSourceType == OBJ_TYPE_MODULE && rDestDoc.hasModule( aDestLibName, aSourceName ) ) ||
257 ( eSourceType == OBJ_TYPE_DIALOG && rDestDoc.hasDialog( aDestLibName, aSourceName ) ) )
259 bValid = FALSE;
263 return bValid;
267 BOOL __EXPORT ExtBasicTreeListBox::NotifyMoving( SvLBoxEntry* pTarget, SvLBoxEntry* pEntry,
268 SvLBoxEntry*& rpNewParent, ULONG& rNewChildPos )
270 return NotifyCopyingMoving( pTarget, pEntry,
271 rpNewParent, rNewChildPos, TRUE );
275 BOOL __EXPORT ExtBasicTreeListBox::NotifyCopying( SvLBoxEntry* pTarget, SvLBoxEntry* pEntry,
276 SvLBoxEntry*& rpNewParent, ULONG& rNewChildPos )
278 // return FALSE; // Wie kopiere ich ein SBX ?!
279 return NotifyCopyingMoving( pTarget, pEntry,
280 rpNewParent, rNewChildPos, FALSE );
284 void BasicIDEShell::CopyDialogResources( Reference< io::XInputStreamProvider >& io_xISP,
285 const ScriptDocument& rSourceDoc, const String& rSourceLibName, const ScriptDocument& rDestDoc,
286 const String& rDestLibName, const String& rDlgName )
288 if ( !io_xISP.is() )
289 return;
291 // Get StringResourceManager
292 Reference< container::XNameContainer > xSourceDialogLib( rSourceDoc.getLibrary( E_DIALOGS, rSourceLibName, TRUE ) );
293 Reference< XStringResourceManager > xSourceMgr =
294 LocalizationMgr::getStringResourceFromDialogLibrary( xSourceDialogLib );
295 if( !xSourceMgr.is() )
296 return;
297 bool bSourceLocalized = ( xSourceMgr->getLocales().getLength() > 0 );
299 Reference< container::XNameContainer > xDestDialogLib( rDestDoc.getLibrary( E_DIALOGS, rDestLibName, TRUE ) );
300 Reference< XStringResourceManager > xDestMgr =
301 LocalizationMgr::getStringResourceFromDialogLibrary( xDestDialogLib );
302 if( !xDestMgr.is() )
303 return;
304 bool bDestLocalized = ( xDestMgr->getLocales().getLength() > 0 );
306 if( !bSourceLocalized && !bDestLocalized )
307 return;
309 // create dialog model
310 Reference< lang::XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
311 Reference< container::XNameContainer > xDialogModel = Reference< container::XNameContainer >( xMSF->createInstance
312 ( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ) ), UNO_QUERY );
313 Reference< io::XInputStream > xInput( io_xISP->createInputStream() );
314 Reference< XComponentContext > xContext;
315 Reference< beans::XPropertySet > xProps( xMSF, UNO_QUERY );
316 OSL_ASSERT( xProps.is() );
317 OSL_VERIFY( xProps->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")) ) >>= xContext );
318 ::xmlscript::importDialogModel( xInput, xDialogModel, xContext, rSourceDoc.getDocument() );
320 if( xDialogModel.is() )
322 if( bSourceLocalized && bDestLocalized )
324 Reference< resource::XStringResourceResolver > xSourceStringResolver( xSourceMgr, UNO_QUERY );
325 LocalizationMgr::copyResourceForDroppedDialog( xDialogModel, rDlgName, xDestMgr, xSourceStringResolver );
327 else if( bSourceLocalized )
329 LocalizationMgr::resetResourceForDialog( xDialogModel, xSourceMgr );
331 else if( bDestLocalized )
333 LocalizationMgr::setResourceIDsForDialog( xDialogModel, xDestMgr );
335 io_xISP = ::xmlscript::exportDialogModel( xDialogModel, xContext, rDestDoc.getDocument() );
340 BOOL __EXPORT ExtBasicTreeListBox::NotifyCopyingMoving( SvLBoxEntry* pTarget, SvLBoxEntry* pEntry,
341 SvLBoxEntry*& rpNewParent, ULONG& rNewChildPos, BOOL bMove )
343 (void)pEntry;
344 DBG_ASSERT( pEntry, "Kein Eintrag?" ); // Hier ASS ok, sollte nicht mit
345 DBG_ASSERT( pTarget, "Kein Ziel?" ); // NULL (ganz vorne) erreicht werden
346 USHORT nDepth = GetModel()->GetDepth( pTarget );
347 DBG_ASSERT( nDepth, "Tiefe?" );
348 if ( nDepth == 1 )
350 // Target = Basic => Modul/Dialog unter das Basic haengen...
351 rpNewParent = pTarget;
352 rNewChildPos = 0;
354 else if ( nDepth >= 2 )
356 // Target = Modul/Dialog => Modul/Dialog unter das uebergeordnete Basic haengen...
357 rpNewParent = GetParent( pTarget );
358 rNewChildPos = GetModel()->GetRelPos( pTarget ) + 1;
361 // get target shell and target library name
362 BasicEntryDescriptor aDestDesc( GetEntryDescriptor( rpNewParent ) );
363 const ScriptDocument& rDestDoc( aDestDesc.GetDocument() );
364 String aDestLibName( aDestDesc.GetLibName() );
366 // get source shell, library name and module/dialog name
367 BasicEntryDescriptor aSourceDesc( GetEntryDescriptor( FirstSelected() ) );
368 const ScriptDocument rSourceDoc( aSourceDesc.GetDocument() );
369 String aSourceLibName( aSourceDesc.GetLibName() );
370 String aSourceName( aSourceDesc.GetName() );
371 BasicEntryType eType( aSourceDesc.GetType() );
373 // get dispatcher
374 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
375 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
376 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
378 if ( bMove ) // move
380 // remove source module/dialog window
381 if ( rSourceDoc != rDestDoc || aSourceLibName != aDestLibName )
383 if( pDispatcher )
385 SbxItem aSbxItem( SID_BASICIDE_ARG_SBX, rSourceDoc, aSourceLibName, aSourceName, ConvertType( eType ) );
386 pDispatcher->Execute( SID_BASICIDE_SBXDELETED,
387 SFX_CALLMODE_SYNCHRON, &aSbxItem, 0L );
393 if ( eType == OBJ_TYPE_MODULE ) // module
395 // get module
396 ::rtl::OUString aModule;
397 if ( rSourceDoc.getModule( aSourceLibName, aSourceName, aModule ) )
399 // remove module from source library
400 if ( rSourceDoc.removeModule( aSourceLibName, aSourceName ) )
402 BasicIDE::MarkDocumentModified( rSourceDoc );
404 // insert module into target library
405 if ( rDestDoc.insertModule( aDestLibName, aSourceName, aModule ) )
406 BasicIDE::MarkDocumentModified( rDestDoc );
410 else if ( eType == OBJ_TYPE_DIALOG ) // dialog
412 // get dialog
413 Reference< io::XInputStreamProvider > xISP;
414 if ( rSourceDoc.getDialog( aSourceLibName, aSourceName, xISP ) )
416 BasicIDEShell::CopyDialogResources( xISP, rSourceDoc,
417 aSourceLibName, rDestDoc, aDestLibName, aSourceName );
419 // remove dialog from source library
420 if ( BasicIDE::RemoveDialog( rSourceDoc, aSourceLibName, aSourceName ) )
422 BasicIDE::MarkDocumentModified( rSourceDoc );
424 // insert dialog into target library
425 if ( rDestDoc.insertDialog( aDestLibName, aSourceName, xISP ) )
426 BasicIDE::MarkDocumentModified( rDestDoc );
431 catch ( uno::Exception& )
433 DBG_UNHANDLED_EXCEPTION();
436 else // copy
440 if ( eType == OBJ_TYPE_MODULE ) // module
442 // get module
443 ::rtl::OUString aModule;
444 if ( rSourceDoc.getModule( aSourceLibName, aSourceName, aModule ) )
446 // insert module into target library
447 if ( rDestDoc.insertModule( aDestLibName, aSourceName, aModule ) )
448 BasicIDE::MarkDocumentModified( rDestDoc );
451 else if ( eType == OBJ_TYPE_DIALOG ) // dialog
453 // get dialog
454 Reference< io::XInputStreamProvider > xISP;
455 if ( rSourceDoc.getDialog( aSourceLibName, aSourceName, xISP ) )
457 BasicIDEShell::CopyDialogResources( xISP, rSourceDoc,
458 aSourceLibName, rDestDoc, aDestLibName, aSourceName );
460 // insert dialog into target library
461 if ( rDestDoc.insertDialog( aDestLibName, aSourceName, xISP ) )
462 BasicIDE::MarkDocumentModified( rDestDoc );
466 catch ( const Exception& )
468 DBG_UNHANDLED_EXCEPTION();
472 // create target module/dialog window
473 if ( rSourceDoc != rDestDoc || aSourceLibName != aDestLibName )
475 if( pDispatcher )
477 SbxItem aSbxItem( SID_BASICIDE_ARG_SBX, rDestDoc, aDestLibName, aSourceName, ConvertType( eType ) );
478 pDispatcher->Execute( SID_BASICIDE_SBXINSERTED,
479 SFX_CALLMODE_SYNCHRON, &aSbxItem, 0L );
483 return 2; // Aufklappen...
486 OrganizeDialog::OrganizeDialog( Window* pParent, INT16 tabId, BasicEntryDescriptor& rDesc )
487 :TabDialog( pParent, IDEResId( RID_TD_ORGANIZE ) )
488 ,aTabCtrl( this, IDEResId( RID_TC_ORGANIZE ) )
489 ,m_aCurEntry( rDesc )
491 FreeResource();
492 aTabCtrl.SetActivatePageHdl( LINK( this, OrganizeDialog, ActivatePageHdl ) );
493 if( tabId == 0 )
495 aTabCtrl.SetCurPageId( RID_TP_MOD );
497 else if ( tabId == 1 )
499 aTabCtrl.SetCurPageId( RID_TP_DLG );
501 else
503 aTabCtrl.SetCurPageId( RID_TP_LIB );
506 ActivatePageHdl( &aTabCtrl );
508 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
509 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
510 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
511 if( pDispatcher )
513 pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
517 __EXPORT OrganizeDialog::~OrganizeDialog()
519 for ( USHORT i = 0; i < aTabCtrl.GetPageCount(); i++ )
520 delete aTabCtrl.GetTabPage( aTabCtrl.GetPageId( i ) );
523 short OrganizeDialog::Execute()
525 Window* pPrevDlgParent = Application::GetDefDialogParent();
526 Application::SetDefDialogParent( this );
527 short nRet = TabDialog::Execute();
528 Application::SetDefDialogParent( pPrevDlgParent );
529 return nRet;
533 IMPL_LINK( OrganizeDialog, ActivatePageHdl, TabControl *, pTabCtrl )
535 USHORT nId = pTabCtrl->GetCurPageId();
536 // Wenn TabPage noch nicht erzeugt wurde, dann erzeugen
537 if ( !pTabCtrl->GetTabPage( nId ) )
539 TabPage* pNewTabPage = 0;
540 switch ( nId )
542 case RID_TP_MOD:
544 pNewTabPage = new ObjectPage( pTabCtrl, IDEResId( RID_TP_MODULS ), BROWSEMODE_MODULES );
545 ((ObjectPage*)pNewTabPage)->SetTabDlg( this );
546 ((ObjectPage*)pNewTabPage)->SetCurrentEntry( m_aCurEntry );
548 break;
549 case RID_TP_DLG:
551 pNewTabPage = new ObjectPage( pTabCtrl, IDEResId( RID_TP_DLGS ), BROWSEMODE_DIALOGS );
552 ((ObjectPage*)pNewTabPage)->SetTabDlg( this );
553 ((ObjectPage*)pNewTabPage)->SetCurrentEntry( m_aCurEntry );
555 break;
556 case RID_TP_LIB:
558 pNewTabPage = new LibPage( pTabCtrl );
559 ((LibPage*)pNewTabPage)->SetTabDlg( this );
561 break;
562 default: DBG_ERROR( "PageHdl: Unbekannte ID!" );
564 DBG_ASSERT( pNewTabPage, "Keine Page!" );
565 pTabCtrl->SetTabPage( nId, pNewTabPage );
567 return 0;
570 ObjectPage::ObjectPage( Window * pParent, const ResId& rResId, USHORT nMode ) :
571 TabPage( pParent, rResId ),
572 aLibText( this, IDEResId( RID_STR_LIB ) ),
573 aBasicBox( this, IDEResId( RID_TRLBOX ) ),
574 aEditButton( this, IDEResId( RID_PB_EDIT ) ),
575 aCloseButton( this, IDEResId( RID_PB_CLOSE ) ),
576 aNewModButton( this, IDEResId( RID_PB_NEWMOD ) ),
577 aNewDlgButton( this, IDEResId( RID_PB_NEWDLG ) ),
578 aDelButton( this, IDEResId( RID_PB_DELETE ) )
580 FreeResource();
581 pTabDlg = 0;
583 aEditButton.SetClickHdl( LINK( this, ObjectPage, ButtonHdl ) );
584 aDelButton.SetClickHdl( LINK( this, ObjectPage, ButtonHdl ) );
585 aCloseButton.SetClickHdl( LINK( this, ObjectPage, ButtonHdl ) );
586 aBasicBox.SetSelectHdl( LINK( this, ObjectPage, BasicBoxHighlightHdl ) );
588 if( nMode & BROWSEMODE_MODULES )
590 aNewModButton.SetClickHdl( LINK( this, ObjectPage, ButtonHdl ) );
591 aNewDlgButton.Hide();
593 else if ( nMode & BROWSEMODE_DIALOGS )
595 aNewDlgButton.SetClickHdl( LINK( this, ObjectPage, ButtonHdl ) );
596 aNewModButton.Hide();
599 aBasicBox.SetDragDropMode( SV_DRAGDROP_CTRL_MOVE | SV_DRAGDROP_CTRL_COPY );
600 aBasicBox.EnableInplaceEditing( TRUE );
601 aBasicBox.SetMode( nMode );
602 aBasicBox.SetWindowBits( WB_HASLINES | WB_HASLINESATROOT |
603 WB_HASBUTTONS | WB_HASBUTTONSATROOT |
604 WB_HSCROLL );
605 aBasicBox.ScanAllEntries();
607 aEditButton.GrabFocus();
608 CheckButtons();
611 void ObjectPage::SetCurrentEntry( BasicEntryDescriptor& rDesc )
613 aBasicBox.SetCurrentEntry( rDesc );
616 void __EXPORT ObjectPage::ActivatePage()
618 aBasicBox.UpdateEntries();
621 void __EXPORT ObjectPage::DeactivatePage()
625 void ObjectPage::CheckButtons()
627 // enable/disable edit button
628 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
629 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
630 ScriptDocument aDocument( aDesc.GetDocument() );
631 ::rtl::OUString aOULibName( aDesc.GetLibName() );
632 String aLibSubName( aDesc.GetLibSubName() );
633 BasicManager* pBasMgr = aDocument.getBasicManager();
634 StarBASIC* pBasic = pBasMgr ? pBasMgr->GetLib( aOULibName ) : 0;
635 sal_Bool bVBAEnabled = pBasic ? pBasic->isVBAEnabled(): sal_False;
636 USHORT nMode = aBasicBox.GetMode();
638 USHORT nDepth = pCurEntry ? aBasicBox.GetModel()->GetDepth( pCurEntry ) : 0;
639 if ( nDepth >= 2 )
641 if( bVBAEnabled && ( nMode & BROWSEMODE_MODULES ) && ( nDepth == 2 ) )
642 aEditButton.Disable();
643 else
644 aEditButton.Enable();
646 else
647 aEditButton.Disable();
649 // enable/disable new module/dialog buttons
650 LibraryLocation eLocation( aDesc.GetLocation() );
651 BOOL bReadOnly = FALSE;
652 if ( nDepth > 0 )
654 Reference< script::XLibraryContainer2 > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
655 Reference< script::XLibraryContainer2 > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
656 if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) ) ||
657 ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) ) )
659 bReadOnly = TRUE;
662 if ( bReadOnly || eLocation == LIBRARY_LOCATION_SHARE )
664 aNewModButton.Disable();
665 aNewDlgButton.Disable();
667 else
669 aNewModButton.Enable();
670 aNewDlgButton.Enable();
673 // enable/disable delete button
674 if ( nDepth >= 2 && !bReadOnly && eLocation != LIBRARY_LOCATION_SHARE )
676 if( bVBAEnabled && ( nMode & BROWSEMODE_MODULES ) && ( ( nDepth == 2 ) || aLibSubName.Equals( String( IDEResId( RID_STR_DOCUMENT_OBJECTS ) ) ) ) )
677 aDelButton.Disable();
678 else
679 aDelButton.Enable();
681 else
682 aDelButton.Disable();
685 IMPL_LINK( ObjectPage, BasicBoxHighlightHdl, BasicTreeListBox *, pBox )
687 if ( !pBox->IsSelected( pBox->GetHdlEntry() ) )
688 return 0;
690 CheckButtons();
691 return 0;
694 IMPL_LINK( ObjectPage, ButtonHdl, Button *, pButton )
696 if ( pButton == &aEditButton )
698 SfxAllItemSet aArgs( SFX_APP()->GetPool() );
699 SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
700 SFX_APP()->ExecuteSlot( aRequest );
702 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
703 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
704 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
705 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
706 DBG_ASSERT( pCurEntry, "Entry?!" );
707 if ( aBasicBox.GetModel()->GetDepth( pCurEntry ) >= 2 )
709 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
710 if ( pDispatcher )
712 String aModName( aDesc.GetName() );
713 // extract the module name from the string like "Sheet1 (Example1)"
714 if( aDesc.GetLibSubName().Equals( String( IDEResId( RID_STR_DOCUMENT_OBJECTS ) ) ) )
716 sal_uInt16 nIndex = 0;
717 aModName = aModName.GetToken( 0, ' ', nIndex );
719 SbxItem aSbxItem( SID_BASICIDE_ARG_SBX, aDesc.GetDocument(), aDesc.GetLibName(),
720 aModName, aBasicBox.ConvertType( aDesc.GetType() ) );
721 pDispatcher->Execute( SID_BASICIDE_SHOWSBX, SFX_CALLMODE_SYNCHRON, &aSbxItem, 0L );
724 else // Nur Lib selektiert
726 DBG_ASSERT( aBasicBox.GetModel()->GetDepth( pCurEntry ) == 1, "Kein LibEntry?!" );
727 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
728 SvLBoxEntry* pParentEntry = aBasicBox.GetParent( pCurEntry );
729 if ( pParentEntry )
731 BasicDocumentEntry* pBasicDocumentEntry = (BasicDocumentEntry*)pParentEntry->GetUserData();
732 if ( pBasicDocumentEntry )
733 aDocument = pBasicDocumentEntry->GetDocument();
735 SfxUsrAnyItem aDocItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, makeAny( aDocument.getDocumentOrNull() ) );
736 String aLibName( aBasicBox.GetEntryText( pCurEntry ) );
737 SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
738 if ( pDispatcher )
740 pDispatcher->Execute( SID_BASICIDE_LIBSELECTED, SFX_CALLMODE_ASYNCHRON, &aDocItem, &aLibNameItem, 0L );
743 EndTabDialog( 1 );
745 else if ( pButton == &aNewModButton )
746 NewModule();
747 else if ( pButton == &aNewDlgButton )
748 NewDialog();
749 else if ( pButton == &aDelButton )
750 DeleteCurrent();
751 else if ( pButton == &aCloseButton )
752 EndTabDialog( 0 );
754 return 0;
757 bool ObjectPage::GetSelection( ScriptDocument& rDocument, String& rLibName )
759 bool bRet = false;
761 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
762 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
763 rDocument = aDesc.GetDocument();
764 rLibName = aDesc.GetLibName();
765 if ( !rLibName.Len() )
766 rLibName = String::CreateFromAscii( "Standard" );
768 DBG_ASSERT( rDocument.isAlive(), "ObjectPage::GetSelection: no or dead ScriptDocument in the selection!" );
769 if ( !rDocument.isAlive() )
770 return false;
772 // check if the module library is loaded
773 BOOL bOK = TRUE;
774 ::rtl::OUString aOULibName( rLibName );
775 Reference< script::XLibraryContainer > xModLibContainer( rDocument.getLibraryContainer( E_SCRIPTS ) );
776 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
778 // check password
779 Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
780 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
782 String aPassword;
783 bOK = QueryPassword( xModLibContainer, rLibName, aPassword );
786 // load library
787 if ( bOK )
788 xModLibContainer->loadLibrary( aOULibName );
791 // check if the dialog library is loaded
792 Reference< script::XLibraryContainer > xDlgLibContainer( rDocument.getLibraryContainer( E_DIALOGS ) );
793 if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
795 // load library
796 if ( bOK )
797 xDlgLibContainer->loadLibrary( aOULibName );
800 if ( bOK )
801 bRet = true;
803 return bRet;
806 void ObjectPage::NewModule()
808 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
809 String aLibName;
811 if ( GetSelection( aDocument, aLibName ) )
813 String aModName;
814 createModImpl( static_cast<Window*>( this ), aDocument,
815 aBasicBox, aLibName, aModName, true );
819 void ObjectPage::NewDialog()
821 ScriptDocument aDocument( ScriptDocument::getApplicationScriptDocument() );
822 String aLibName;
824 if ( GetSelection( aDocument, aLibName ) )
826 aDocument.getOrCreateLibrary( E_DIALOGS, aLibName );
828 std::auto_ptr< NewObjectDialog > xNewDlg(
829 new NewObjectDialog(this, NEWOBJECTMODE_DLG, true));
830 xNewDlg->SetObjectName( aDocument.createObjectName( E_DIALOGS, aLibName ) );
832 if (xNewDlg->Execute() != 0)
834 String aDlgName( xNewDlg->GetObjectName() );
835 if (aDlgName.Len() == 0)
836 aDlgName = aDocument.createObjectName( E_DIALOGS, aLibName);
838 if ( aDocument.hasDialog( aLibName, aDlgName ) )
840 ErrorBox( this, WB_OK | WB_DEF_OK,
841 String( IDEResId( RID_STR_SBXNAMEALLREADYUSED2 ) ) ).Execute();
843 else
845 Reference< io::XInputStreamProvider > xISP;
846 if ( !aDocument.createDialog( aLibName, aDlgName, xISP ) )
847 return;
849 SbxItem aSbxItem( SID_BASICIDE_ARG_SBX, aDocument, aLibName, aDlgName, BASICIDE_TYPE_DIALOG );
850 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
851 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
852 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
853 if( pDispatcher )
855 pDispatcher->Execute( SID_BASICIDE_SBXINSERTED,
856 SFX_CALLMODE_SYNCHRON, &aSbxItem, 0L );
858 LibraryLocation eLocation = aDocument.getLibraryLocation( aLibName );
859 SvLBoxEntry* pRootEntry = aBasicBox.FindRootEntry( aDocument, eLocation );
860 if ( pRootEntry )
862 if ( !aBasicBox.IsExpanded( pRootEntry ) )
863 aBasicBox.Expand( pRootEntry );
864 SvLBoxEntry* pLibEntry = aBasicBox.FindEntry( pRootEntry, aLibName, OBJ_TYPE_LIBRARY );
865 DBG_ASSERT( pLibEntry, "Libeintrag nicht gefunden!" );
866 if ( pLibEntry )
868 if ( !aBasicBox.IsExpanded( pLibEntry ) )
869 aBasicBox.Expand( pLibEntry );
870 SvLBoxEntry* pEntry = aBasicBox.FindEntry( pLibEntry, aDlgName, OBJ_TYPE_DIALOG );
871 if ( !pEntry )
873 pEntry = aBasicBox.AddEntry(
874 aDlgName,
875 Image( IDEResId( RID_IMG_DIALOG ) ),
876 Image( IDEResId( RID_IMG_DIALOG_HC ) ),
877 pLibEntry, false,
878 std::auto_ptr< BasicEntry >( new BasicEntry( OBJ_TYPE_DIALOG ) ) );
879 DBG_ASSERT( pEntry, "InsertEntry fehlgeschlagen!" );
881 aBasicBox.SetCurEntry( pEntry );
882 aBasicBox.Select( aBasicBox.GetCurEntry() ); // OV-Bug?!
890 void ObjectPage::DeleteCurrent()
892 SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
893 DBG_ASSERT( pCurEntry, "Kein aktueller Eintrag!" );
894 BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
895 ScriptDocument aDocument( aDesc.GetDocument() );
896 DBG_ASSERT( aDocument.isAlive(), "ObjectPage::DeleteCurrent: no document!" );
897 if ( !aDocument.isAlive() )
898 return;
899 String aLibName( aDesc.GetLibName() );
900 String aName( aDesc.GetName() );
901 BasicEntryType eType( aDesc.GetType() );
903 if ( ( eType == OBJ_TYPE_MODULE && QueryDelModule( aName, this ) ) ||
904 ( eType == OBJ_TYPE_DIALOG && QueryDelDialog( aName, this ) ) )
906 aBasicBox.GetModel()->Remove( pCurEntry );
907 if ( aBasicBox.GetCurEntry() ) // OV-Bug ?
908 aBasicBox.Select( aBasicBox.GetCurEntry() );
909 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
910 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
911 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
912 if( pDispatcher )
914 SbxItem aSbxItem( SID_BASICIDE_ARG_SBX, aDocument, aLibName, aName, aBasicBox.ConvertType( eType ) );
915 pDispatcher->Execute( SID_BASICIDE_SBXDELETED,
916 SFX_CALLMODE_SYNCHRON, &aSbxItem, 0L );
921 bool bSuccess = false;
922 if ( eType == OBJ_TYPE_MODULE )
923 bSuccess = aDocument.removeModule( aLibName, aName );
924 else if ( eType == OBJ_TYPE_DIALOG )
925 bSuccess = BasicIDE::RemoveDialog( aDocument, aLibName, aName );
927 if ( bSuccess )
928 BasicIDE::MarkDocumentModified( aDocument );
930 catch ( container::NoSuchElementException& )
932 DBG_UNHANDLED_EXCEPTION();
939 void ObjectPage::EndTabDialog( USHORT nRet )
941 DBG_ASSERT( pTabDlg, "TabDlg nicht gesetzt!" );
942 if ( pTabDlg )
943 pTabDlg->EndDialog( nRet );
947 LibDialog::LibDialog( Window* pParent )
948 : ModalDialog( pParent, IDEResId( RID_DLG_LIBS ) ),
949 aOKButton( this, IDEResId( RID_PB_OK ) ),
950 aCancelButton( this, IDEResId( RID_PB_CANCEL ) ),
951 aStorageName( this, IDEResId( RID_FT_STORAGENAME ) ),
952 aLibBox( this, IDEResId( RID_CTRL_LIBS ) ),
953 aFixedLine( this, IDEResId( RID_FL_OPTIONS ) ),
954 aReferenceBox( this, IDEResId( RID_CB_REF ) ),
955 aReplaceBox( this, IDEResId( RID_CB_REPL ) )
957 SetText( String( IDEResId( RID_STR_APPENDLIBS ) ) );
958 FreeResource();
962 LibDialog::~LibDialog()
966 void LibDialog::SetStorageName( const String& rName )
968 String aName( IDEResId( RID_STR_FILENAME ) );
969 aName += rName;
970 aStorageName.SetText( aName );
973 // Helper function
974 SbModule* createModImpl( Window* pWin, const ScriptDocument& rDocument,
975 BasicTreeListBox& rBasicBox, const String& rLibName, String aModName, bool bMain )
977 OSL_ENSURE( rDocument.isAlive(), "createModImpl: invalid document!" );
978 if ( !rDocument.isAlive() )
979 return NULL;
981 SbModule* pModule = NULL;
983 String aLibName( rLibName );
984 if ( !aLibName.Len() )
985 aLibName = String::CreateFromAscii( "Standard" );
986 rDocument.getOrCreateLibrary( E_SCRIPTS, aLibName );
987 if ( !aModName.Len() )
988 aModName = rDocument.createObjectName( E_SCRIPTS, aLibName );
990 std::auto_ptr< NewObjectDialog > xNewDlg(
991 new NewObjectDialog( pWin, NEWOBJECTMODE_MOD, true ) );
992 xNewDlg->SetObjectName( aModName );
994 if (xNewDlg->Execute() != 0)
996 if ( xNewDlg->GetObjectName().Len() )
997 aModName = xNewDlg->GetObjectName();
1001 ::rtl::OUString sModuleCode;
1002 // the module has existed
1003 if( rDocument.hasModule( aLibName, aModName ) )
1004 return NULL;
1005 rDocument.createModule( aLibName, aModName, bMain, sModuleCode );
1006 BasicManager* pBasMgr = rDocument.getBasicManager();
1007 StarBASIC* pBasic = pBasMgr? pBasMgr->GetLib( aLibName ) : 0;
1008 if ( pBasic )
1009 pModule = pBasic->FindModule( aModName );
1010 SbxItem aSbxItem( SID_BASICIDE_ARG_SBX, rDocument, aLibName, aModName, BASICIDE_TYPE_MODULE );
1011 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
1012 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
1013 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
1014 if( pDispatcher )
1016 pDispatcher->Execute( SID_BASICIDE_SBXINSERTED,
1017 SFX_CALLMODE_SYNCHRON, &aSbxItem, 0L );
1019 LibraryLocation eLocation = rDocument.getLibraryLocation( aLibName );
1020 SvLBoxEntry* pRootEntry = rBasicBox.FindRootEntry( rDocument, eLocation );
1021 if ( pRootEntry )
1023 if ( !rBasicBox.IsExpanded( pRootEntry ) )
1024 rBasicBox.Expand( pRootEntry );
1025 SvLBoxEntry* pLibEntry = rBasicBox.FindEntry( pRootEntry, aLibName, OBJ_TYPE_LIBRARY );
1026 DBG_ASSERT( pLibEntry, "Libeintrag nicht gefunden!" );
1027 if ( pLibEntry )
1029 if ( !rBasicBox.IsExpanded( pLibEntry ) )
1030 rBasicBox.Expand( pLibEntry );
1031 SvLBoxEntry* pSubRootEntry = pLibEntry;
1032 if( pBasic && pBasic->isVBAEnabled() )
1034 // add the new module in the "Modules" entry
1035 SvLBoxEntry* pLibSubEntry = rBasicBox.FindEntry( pLibEntry, String( IDEResId( RID_STR_NORMAL_MODULES ) ) , OBJ_TYPE_NORMAL_MODULES );
1036 if( pLibSubEntry )
1038 if( !rBasicBox.IsExpanded( pLibSubEntry ) )
1039 rBasicBox.Expand( pLibSubEntry );
1040 pSubRootEntry = pLibSubEntry;
1044 SvLBoxEntry* pEntry = rBasicBox.FindEntry( pSubRootEntry, aModName, OBJ_TYPE_MODULE );
1045 if ( !pEntry )
1047 pEntry = rBasicBox.AddEntry(
1048 aModName,
1049 Image( IDEResId( RID_IMG_MODULE ) ),
1050 Image( IDEResId( RID_IMG_MODULE_HC ) ),
1051 pSubRootEntry, false,
1052 std::auto_ptr< BasicEntry >( new BasicEntry( OBJ_TYPE_MODULE ) ) );
1053 DBG_ASSERT( pEntry, "InsertEntry fehlgeschlagen!" );
1055 rBasicBox.SetCurEntry( pEntry );
1056 rBasicBox.Select( rBasicBox.GetCurEntry() ); // OV-Bug?!
1060 catch ( container::ElementExistException& )
1062 ErrorBox( pWin, WB_OK | WB_DEF_OK,
1063 String( IDEResId( RID_STR_SBXNAMEALLREADYUSED2 ) ) ).Execute();
1065 catch ( container::NoSuchElementException& )
1067 DBG_UNHANDLED_EXCEPTION();
1070 return pModule;