1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
27 #define PATH_MAX _MAX_PATH
31 #include <sfx2/app.hxx>
32 #include <sfx2/frame.hxx>
33 #include <basic/basrdll.hxx>
34 #include <basic/sbmeth.hxx>
35 #include <basic/sbmod.hxx>
36 #include <svtools/asynclink.hxx>
37 #include <svl/stritem.hxx>
38 #include <svl/eitem.hxx>
39 #include <svl/urlbmk.hxx>
40 #include <vcl/msgbox.hxx>
41 #include <svtools/sfxecode.hxx>
42 #include <svtools/ehdl.hxx>
44 #include <svl/svdde.hxx>
45 #include <unotools/tempfile.hxx>
46 #include <osl/file.hxx>
47 #include <com/sun/star/uno/Sequence.hxx>
48 #include <com/sun/star/uno/Any.hxx>
49 #include <com/sun/star/lang/XInitialization.hpp>
50 #include <com/sun/star/frame/XFrameActionListener.hpp>
51 #include <com/sun/star/frame/XComponentLoader.hpp>
52 #include <com/sun/star/frame/XFrame.hpp>
53 #include <com/sun/star/frame/FrameActionEvent.hpp>
54 #include <com/sun/star/frame/FrameAction.hpp>
55 #include <com/sun/star/loader/XImplementationLoader.hpp>
56 #include <com/sun/star/mozilla/XPluginInstance.hpp>
57 #include <com/sun/star/frame/XFramesSupplier.hpp>
58 #include <com/sun/star/container/XIndexAccess.hpp>
59 #include <com/sun/star/beans/XPropertySet.hpp>
60 #include <comphelper/processfactory.hxx>
61 #include <com/sun/star/uri/UriReferenceFactory.hpp>
62 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
63 #include <basic/basmgr.hxx>
64 #include <toolkit/helper/vclunohelper.hxx>
65 #include <vcl/svapp.hxx>
66 #include <sfx2/appuno.hxx>
67 #include <sfx2/sfxhelp.hxx>
68 #include <sfx2/request.hxx>
69 #include "sfxtypes.hxx"
70 #include <sfx2/sfxresid.hxx>
71 #include "arrdecl.hxx"
72 #include <sfx2/progress.hxx>
73 #include <sfx2/objsh.hxx>
74 #include <sfx2/docfac.hxx>
75 #include <sfx2/docfile.hxx>
76 #include <sfx2/docfilt.hxx>
77 #include <sfx2/new.hxx>
78 #include <sfx2/bindings.hxx>
79 #include <sfx2/dispatch.hxx>
80 #include <sfx2/viewsh.hxx>
81 #include <sfx2/genlink.hxx>
82 #include <sfx2/viewfrm.hxx>
83 #include "appdata.hxx"
84 #include "openflag.hxx"
86 #include "virtmenu.hxx"
87 #include <sfx2/module.hxx>
88 #include <sfx2/event.hxx>
89 #include "imestatuswindow.hxx"
90 #include "workwin.hxx"
91 #include <sfx2/sidebar/Theme.hxx>
92 #include <sfx2/tbxctrl.hxx>
93 #include <sfx2/sfxdlg.hxx>
94 #include <sfx2/stbitem.hxx>
95 #include "eventsupplier.hxx"
96 #include <sfx2/dockwin.hxx>
99 #include <sfx2/mnuitem.hxx>
102 #include <unotools/saveopt.hxx>
103 #include <svtools/helpopt.hxx>
104 #include <unotools/pathoptions.hxx>
105 #include <unotools/viewoptions.hxx>
106 #include <unotools/moduleoptions.hxx>
107 #include <unotools/historyoptions.hxx>
108 #include <svtools/menuoptions.hxx>
109 #include <svtools/miscopt.hxx>
110 #include <unotools/useroptions.hxx>
111 #include <unotools/securityoptions.hxx>
112 #include <unotools/localisationoptions.hxx>
113 #include <unotools/fontoptions.hxx>
114 #include <unotools/syslocaleoptions.hxx>
115 #include <unotools/syslocale.hxx>
116 #include <framework/addonsoptions.hxx>
117 #include <unotools/extendedsecurityoptions.hxx>
118 #include <rtl/instance.hxx>
119 #include <rtl/strbuf.hxx>
121 using namespace ::com::sun::star
;
124 SfxApplication
* SfxApplication::pApp
= NULL
;
125 #ifndef DISABLE_SCRIPTING
126 static BasicDLL
* pBasic
= NULL
;
129 #if HAVE_FEATURE_DESKTOP
130 static SfxHelp
* pSfxHelp
= NULL
;
135 class theApplicationMutex
136 : public rtl::Static
<osl::Mutex
, theApplicationMutex
> {};
139 #include <framework/imageproducer.hxx>
140 #include <framework/sfxhelperfunctions.hxx>
141 #include <sfx2/imagemgr.hxx>
142 #include "fwkhelper.hxx"
144 SfxApplication
* SfxApplication::GetOrCreate()
147 ::osl::MutexGuard
aGuard(theApplicationMutex::get());
150 SAL_INFO( "sfx.appl", "sfx2 (mb93783) ::SfxApplication::SetApp" );
152 pApp
= new SfxApplication
;
154 // at the moment a bug may occur when Initialize_Impl returns FALSE,
155 // but this is only temporary because all code that may cause such
156 // a fault will be moved outside the SFX
157 pApp
->Initialize_Impl();
159 ::framework::SetImageProducer( GetImage
);
160 ::framework::SetRefreshToolbars( RefreshToolbars
);
161 ::framework::SetToolBoxControllerCreator( SfxToolBoxControllerFactory
);
162 ::framework::SetStatusBarControllerCreator( SfxStatusBarControllerFactory
);
163 ::framework::SetDockingWindowCreator( SfxDockingWindowFactory
);
164 ::framework::SetIsDockingWindowVisible( IsDockingWindowVisible
);
165 ::framework::SetActivateToolPanel( &SfxViewFrame::ActivateToolPanel
);
166 #if HAVE_FEATURE_DESKTOP
167 Application::SetHelp( pSfxHelp
);
168 if ( SvtHelpOptions().IsHelpTips() )
169 Help::EnableQuickHelp();
171 Help::DisableQuickHelp();
172 if ( SvtHelpOptions().IsHelpTips() && SvtHelpOptions().IsExtendedHelp() )
173 Help::EnableBalloonHelp();
175 Help::DisableBalloonHelp();
181 SfxApplication::SfxApplication()
184 SAL_INFO( "sfx.appl", "sfx2 (mb93783) ::SfxApplication::SfxApplication" );
186 SetName( OUString("StarOffice") );
187 SvtViewOptions::AcquireOptions();
189 pAppData_Impl
= new SfxAppData_Impl( this );
190 pAppData_Impl
->m_xImeStatusWindow
->init();
192 SAL_INFO( "sfx.appl", "{ initialize DDE" );
194 sal_Bool bOk
= InitializeDde();
200 RTL_CONSTASCII_STRINGPARAM("No DDE-Service possible. Error: "));
201 if( GetDdeService() )
202 aStr
.append(static_cast<sal_Int32
>(GetDdeService()->GetError()));
205 DBG_ASSERT( sal_False
, aStr
.getStr() );
211 #if HAVE_FEATURE_DESKTOP
212 pSfxHelp
= new SfxHelp
;
215 #ifndef DISABLE_SCRIPTING
216 pBasic
= new BasicDLL
;
217 StarBASIC::SetGlobalErrorHdl( LINK( this, SfxApplication
, GlobalBasicErrorHdl_Impl
) );
219 SAL_INFO( "sfx.appl", "} initialize DDE" );
222 SfxApplication::~SfxApplication()
224 OSL_ENSURE( GetObjectShells_Impl().size() == 0, "Memory leak: some object shells were not removed!" );
226 Broadcast( SfxSimpleHint(SFX_HINT_DYING
) );
228 SfxModule::DestroyModules_Impl();
230 #if HAVE_FEATURE_DESKTOP
232 Application::SetHelp( NULL
);
235 // delete global options
236 SvtViewOptions::ReleaseOptions();
238 #ifndef DISABLE_SCRIPTING
241 if ( !pAppData_Impl
->bDowning
)
244 delete pAppData_Impl
;
248 //====================================================================
250 const OUString
& SfxApplication::GetLastDir_Impl() const
254 Internal method by which the last set directory with the method
255 <SfxApplication::SetLastDir_Impl()> in SFX is returned.
257 This is usually the most recently addressed by the
258 SfxFileDialog directory.
261 <SfxApplication::SetLastDir_Impl()>
265 return pAppData_Impl
->aLastDir
;
268 const OUString
& SfxApplication::GetLastSaveDirectory() const
272 As <SfxApplication::GetLastDir_Impl()>, only external
275 <SfxApplication::GetLastDir_Impl()>
279 return GetLastDir_Impl();
282 //--------------------------------------------------------------------
284 void SfxApplication::SetLastDir_Impl
286 const OUString
& rNewDir
/* Complete directory path as a string */
291 Internal Method, by which a directory path is set that was last addressed
292 (eg by the SfxFileDialog).
295 <SfxApplication::GetLastDir_Impl()>
299 pAppData_Impl
->aLastDir
= rNewDir
;
302 //--------------------------------------------------------------------
304 void SfxApplication::ResetLastDir()
306 pAppData_Impl
->aLastDir
= "";
309 //--------------------------------------------------------------------
311 SfxDispatcher
* SfxApplication::GetDispatcher_Impl()
313 return pAppData_Impl
->pViewFrame
? pAppData_Impl
->pViewFrame
->GetDispatcher(): pAppData_Impl
->pAppDispat
;
316 //--------------------------------------------------------------------
317 void SfxApplication::SetViewFrame_Impl( SfxViewFrame
*pFrame
)
319 if ( pFrame
!= pAppData_Impl
->pViewFrame
)
321 // get the containerframes ( if one of the frames is an InPlaceFrame )
322 SfxViewFrame
*pOldContainerFrame
= pAppData_Impl
->pViewFrame
;
323 while ( pOldContainerFrame
&& pOldContainerFrame
->GetParentViewFrame_Impl() )
324 pOldContainerFrame
= pOldContainerFrame
->GetParentViewFrame_Impl();
325 SfxViewFrame
*pNewContainerFrame
= pFrame
;
326 while ( pNewContainerFrame
&& pNewContainerFrame
->GetParentViewFrame_Impl() )
327 pNewContainerFrame
= pNewContainerFrame
->GetParentViewFrame_Impl();
329 // DocWinActivate : both frames belong to the same TopWindow
330 // TopWinActivate : both frames belong to different TopWindows
332 sal_Bool bTaskActivate
= pOldContainerFrame
!= pNewContainerFrame
;
334 if ( pOldContainerFrame
)
337 NotifyEvent( SfxViewEventHint( SFX_EVENT_DEACTIVATEDOC
, GlobalEventConfig::GetEventName(STR_EVENT_DEACTIVATEDOC
), pOldContainerFrame
->GetObjectShell(), pOldContainerFrame
->GetFrame().GetController() ) );
338 pOldContainerFrame
->DoDeactivate( bTaskActivate
, pFrame
);
340 if( pOldContainerFrame
->GetProgress() )
341 pOldContainerFrame
->GetProgress()->Suspend();
344 pAppData_Impl
->pViewFrame
= pFrame
;
346 if( pNewContainerFrame
)
348 pNewContainerFrame
->DoActivate( bTaskActivate
);
349 if ( bTaskActivate
&& pNewContainerFrame
->GetObjectShell() )
351 pNewContainerFrame
->GetObjectShell()->PostActivateEvent_Impl( pNewContainerFrame
);
352 NotifyEvent(SfxViewEventHint(SFX_EVENT_ACTIVATEDOC
, GlobalEventConfig::GetEventName(STR_EVENT_ACTIVATEDOC
), pNewContainerFrame
->GetObjectShell(), pNewContainerFrame
->GetFrame().GetController() ) );
355 SfxProgress
*pProgress
= pNewContainerFrame
->GetProgress();
358 if( pProgress
->IsSuspended() )
361 pProgress
->SetState( pProgress
->GetState() );
364 if ( pAppData_Impl
->pViewFrame
->GetViewShell() )
366 SfxDispatcher
* pDisp
= pAppData_Impl
->pViewFrame
->GetDispatcher();
368 pDisp
->Update_Impl(sal_True
);
373 // even if the frame actually didn't change, ensure its document is forwarded
374 // to SfxObjectShell::SetCurrentComponent.
375 // Otherwise, the CurrentComponent might not be correct, in case it has meanwhile
376 // been reset to some other document, by some non-SFX component.
377 // #i49133# / 2007-12-19 / frank.schoenheit@sun.com
378 if ( pFrame
&& pFrame
->GetViewShell() )
379 pFrame
->GetViewShell()->SetCurrentDocument();
382 //---------------------------------------------------------------------
384 ResMgr
* SfxApplication::CreateResManager( const char *pPrefix
)
386 return ResMgr::CreateResMgr(pPrefix
);
389 //--------------------------------------------------------------------
391 ResMgr
* SfxApplication::GetSfxResManager()
393 return SfxResId::GetResMgr();
396 //------------------------------------------------------------------------
398 void SfxApplication::SetProgress_Impl
400 SfxProgress
*pProgress
404 DBG_ASSERT( ( !pAppData_Impl
->pProgress
&& pProgress
) ||
405 ( pAppData_Impl
->pProgress
&& !pProgress
),
406 "Progress acitivation/deacitivation mismatch" );
408 if ( pAppData_Impl
->pProgress
&& pProgress
)
410 pAppData_Impl
->pProgress
->Suspend();
411 pAppData_Impl
->pProgress
->UnLock();
412 delete pAppData_Impl
->pProgress
;
415 pAppData_Impl
->pProgress
= pProgress
;
418 //------------------------------------------------------------------------
420 sal_uInt16
SfxApplication::GetFreeIndex()
422 return pAppData_Impl
->aIndexBitSet
.GetFreeIndex()+1;
425 //------------------------------------------------------------------------
427 void SfxApplication::ReleaseIndex(sal_uInt16 i
)
429 pAppData_Impl
->aIndexBitSet
.ReleaseIndex(i
-1);
432 //--------------------------------------------------------------------
434 Window
* SfxApplication::GetTopWindow() const
436 SfxWorkWindow
* pWork
= GetWorkWindow_Impl( SfxViewFrame::Current() );
437 return pWork
? pWork
->GetWindow() : NULL
;
440 SfxTbxCtrlFactArr_Impl
& SfxApplication::GetTbxCtrlFactories_Impl() const
442 return *pAppData_Impl
->pTbxCtrlFac
;
445 SfxStbCtrlFactArr_Impl
& SfxApplication::GetStbCtrlFactories_Impl() const
447 return *pAppData_Impl
->pStbCtrlFac
;
450 SfxMenuCtrlFactArr_Impl
& SfxApplication::GetMenuCtrlFactories_Impl() const
452 return *pAppData_Impl
->pMenuCtrlFac
;
455 SfxViewFrameArr_Impl
& SfxApplication::GetViewFrames_Impl() const
457 return *pAppData_Impl
->pViewFrames
;
460 SfxViewShellArr_Impl
& SfxApplication::GetViewShells_Impl() const
462 return *pAppData_Impl
->pViewShells
;
465 SfxObjectShellArr_Impl
& SfxApplication::GetObjectShells_Impl() const
467 return *pAppData_Impl
->pObjShells
;
470 void SfxApplication::Invalidate( sal_uInt16 nId
)
472 for( SfxViewFrame
* pFrame
= SfxViewFrame::GetFirst(); pFrame
; pFrame
= SfxViewFrame::GetNext( *pFrame
) )
473 Invalidate_Impl( pFrame
->GetBindings(), nId
);
476 #ifndef DISABLE_SCRIPTING
478 #ifndef DISABLE_DYNLOADING
480 typedef long (SAL_CALL
*basicide_handle_basic_error
)(void*);
481 typedef void* (SAL_CALL
*basicide_macro_organizer
)(sal_Int16
);
483 extern "C" { static void SAL_CALL
thisModule() {} }
487 extern "C" long basicide_handle_basic_error(void*);
488 extern "C" void *basicide_macro_organizer(sal_Int16
);
494 IMPL_LINK( SfxApplication
, GlobalBasicErrorHdl_Impl
, StarBASIC
*, pStarBasic
)
496 #ifdef DISABLE_SCRIPTING
501 #ifndef DISABLE_DYNLOADING
502 // get basctl dllname
503 static OUString
aLibName( SVLIBRARY( "basctl" ) );
506 oslModule handleMod
= osl_loadModuleRelative(
507 &thisModule
, aLibName
.pData
, 0 );
510 OUString
aSymbol( "basicide_handle_basic_error" );
511 basicide_handle_basic_error pSymbol
= (basicide_handle_basic_error
) osl_getFunctionSymbol( handleMod
, aSymbol
.pData
);
513 // call basicide_handle_basic_error in basctl
514 long nRet
= pSymbol
? pSymbol( pStarBasic
) : 0;
518 long nRet
= basicide_handle_basic_error( pStarBasic
);
527 sal_Bool
SfxApplication::IsXScriptURL( const OUString
& rScriptURL
)
529 sal_Bool result
= sal_False
;
531 #ifdef DISABLE_SCRIPTING
534 ::com::sun::star::uno::Reference
535 < ::com::sun::star::uno::XComponentContext
> xContext
=
536 ::comphelper::getProcessComponentContext();
538 ::com::sun::star::uno::Reference
539 < ::com::sun::star::uri::XUriReferenceFactory
>
540 xFactory
= ::com::sun::star::uri::UriReferenceFactory::create( xContext
);
544 ::com::sun::star::uno::Reference
545 < ::com::sun::star::uri::XVndSunStarScriptUrl
>
546 xUrl( xFactory
->parse( rScriptURL
),
547 ::com::sun::star::uno::UNO_QUERY
);
554 catch (const ::com::sun::star::uno::RuntimeException
&)
556 // ignore, will just return FALSE
563 SfxApplication::ChooseScript()
567 #ifndef DISABLE_SCRIPTING
568 SfxAbstractDialogFactory
* pFact
= SfxAbstractDialogFactory::Create();
571 SAL_INFO( "sfx.appl", "create selector dialog");
573 const SfxViewFrame
* pViewFrame
= SfxViewFrame::Current();
574 const SfxFrame
* pFrame
= pViewFrame
? &pViewFrame
->GetFrame() : NULL
;
575 uno::Reference
< frame::XFrame
> xFrame( pFrame
? pFrame
->GetFrameInterface() : uno::Reference
< frame::XFrame
>() );
577 AbstractScriptSelectorDialog
* pDlg
=
578 pFact
->CreateScriptSelectorDialog( NULL
, sal_False
, xFrame
);
580 SAL_INFO( "sfx.appl", "done, now exec it");
582 sal_uInt16 nRet
= pDlg
->Execute();
584 SAL_INFO( "sfx.appl", "has returned");
586 if ( nRet
== RET_OK
)
588 aScriptURL
= pDlg
->GetScriptURL();
597 void SfxApplication::MacroOrganizer( sal_Int16 nTabId
)
599 #ifdef DISABLE_SCRIPTING
603 #ifndef DISABLE_DYNLOADING
604 // get basctl dllname
605 static OUString
aLibName( SVLIBRARY( "basctl" ) );
608 oslModule handleMod
= osl_loadModuleRelative(
609 &thisModule
, aLibName
.pData
, 0 );
612 OUString
aSymbol( "basicide_macro_organizer" );
613 basicide_macro_organizer pSymbol
= (basicide_macro_organizer
) osl_getFunctionSymbol( handleMod
, aSymbol
.pData
);
615 // call basicide_macro_organizer in basctl
620 basicide_macro_organizer( nTabId
);
627 ErrCode
SfxApplication::CallBasic( const OUString
& rCode
, BasicManager
* pMgr
, SbxArray
* pArgs
, SbxValue
* pRet
)
629 #ifdef DISABLE_SCRIPTING
634 return ERRCODE_BASIC_CANNOT_LOAD
;
636 return pMgr
->ExecuteMacro( rCode
, pArgs
, pRet
);
640 sfx2::sidebar::Theme
& SfxApplication::GetSidebarTheme()
642 if (!pAppData_Impl
->m_pSidebarTheme
.is())
644 pAppData_Impl
->m_pSidebarTheme
.set(new sfx2::sidebar::Theme
);
645 pAppData_Impl
->m_pSidebarTheme
->InitializeTheme();
647 return *pAppData_Impl
->m_pSidebarTheme
;
650 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */