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/sfxhelp.hxx>
67 #include <sfx2/request.hxx>
68 #include "sfxtypes.hxx"
69 #include <sfx2/sfxresid.hxx>
70 #include "arrdecl.hxx"
71 #include <sfx2/progress.hxx>
72 #include <sfx2/objsh.hxx>
73 #include <sfx2/docfac.hxx>
74 #include <sfx2/docfile.hxx>
75 #include <sfx2/docfilt.hxx>
76 #include <sfx2/new.hxx>
77 #include <sfx2/bindings.hxx>
78 #include <sfx2/dispatch.hxx>
79 #include <sfx2/viewsh.hxx>
80 #include <sfx2/genlink.hxx>
81 #include <sfx2/viewfrm.hxx>
82 #include "appdata.hxx"
83 #include "openflag.hxx"
85 #include "virtmenu.hxx"
86 #include <sfx2/module.hxx>
87 #include <sfx2/event.hxx>
88 #include "imestatuswindow.hxx"
89 #include "workwin.hxx"
90 #include <sfx2/sidebar/Theme.hxx>
91 #include <sfx2/tbxctrl.hxx>
92 #include <sfx2/sfxdlg.hxx>
93 #include <sfx2/stbitem.hxx>
94 #include "eventsupplier.hxx"
95 #include <sfx2/dockwin.hxx>
96 #include "shellimpl.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>
120 #include <boost/scoped_ptr.hpp>
122 using namespace ::com::sun::star
;
125 SfxApplication
* SfxApplication::pApp
= NULL
;
126 #if HAVE_FEATURE_SCRIPTING
127 static BasicDLL
* pBasic
= NULL
;
130 #if HAVE_FEATURE_DESKTOP
131 static SfxHelp
* pSfxHelp
= NULL
;
136 class theApplicationMutex
137 : public rtl::Static
<osl::Mutex
, theApplicationMutex
> {};
140 #include <framework/imageproducer.hxx>
141 #include <framework/sfxhelperfunctions.hxx>
142 #include <sfx2/imagemgr.hxx>
143 #include "fwkhelper.hxx"
145 SfxApplication
* SfxApplication::GetOrCreate()
148 ::osl::MutexGuard
aGuard(theApplicationMutex::get());
151 SAL_INFO( "sfx.appl", "SfxApplication::SetApp" );
153 pApp
= new SfxApplication
;
155 // at the moment a bug may occur when Initialize_Impl returns FALSE,
156 // but this is only temporary because all code that may cause such
157 // a fault will be moved outside the SFX
158 pApp
->Initialize_Impl();
160 ::framework::SetImageProducer( GetImage
);
161 ::framework::SetRefreshToolbars( RefreshToolbars
);
162 ::framework::SetToolBoxControllerCreator( SfxToolBoxControllerFactory
);
163 ::framework::SetStatusBarControllerCreator( SfxStatusBarControllerFactory
);
164 ::framework::SetDockingWindowCreator( SfxDockingWindowFactory
);
165 ::framework::SetIsDockingWindowVisible( IsDockingWindowVisible
);
166 ::framework::SetActivateToolPanel( &SfxViewFrame::ActivateToolPanel
);
167 #if HAVE_FEATURE_DESKTOP
168 Application::SetHelp( pSfxHelp
);
169 if ( SvtHelpOptions().IsHelpTips() )
170 Help::EnableQuickHelp();
172 Help::DisableQuickHelp();
173 if ( SvtHelpOptions().IsHelpTips() && SvtHelpOptions().IsExtendedHelp() )
174 Help::EnableBalloonHelp();
176 Help::DisableBalloonHelp();
182 SfxApplication::SfxApplication()
185 SetName( OUString("StarOffice") );
186 SvtViewOptions::AcquireOptions();
188 pAppData_Impl
= new SfxAppData_Impl( this );
189 pAppData_Impl
->m_xImeStatusWindow
->init();
191 SAL_INFO( "sfx.appl", "{ initialize DDE" );
193 bool bOk
= InitializeDde();
198 OStringBuffer
aStr("No DDE-Service possible. Error: ");
199 if( GetDdeService() )
200 aStr
.append(static_cast<sal_Int32
>(GetDdeService()->GetError()));
203 DBG_ASSERT( false, aStr
.getStr() );
209 #if HAVE_FEATURE_DESKTOP
210 pSfxHelp
= new SfxHelp
;
213 #if HAVE_FEATURE_SCRIPTING
214 pBasic
= new BasicDLL
;
215 StarBASIC::SetGlobalErrorHdl( LINK( this, SfxApplication
, GlobalBasicErrorHdl_Impl
) );
217 SAL_INFO( "sfx.appl", "} initialize DDE" );
220 SfxApplication::~SfxApplication()
222 OSL_ENSURE( GetObjectShells_Impl().size() == 0, "Memory leak: some object shells were not removed!" );
224 Broadcast( SfxSimpleHint(SFX_HINT_DYING
) );
226 SfxModule::DestroyModules_Impl();
228 #if HAVE_FEATURE_DESKTOP
230 Application::SetHelp( NULL
);
233 // delete global options
234 SvtViewOptions::ReleaseOptions();
236 if ( !pAppData_Impl
->bDowning
)
239 #if HAVE_FEATURE_SCRIPTING
243 delete pAppData_Impl
;
249 const OUString
& SfxApplication::GetLastDir_Impl() const
253 Internal method by which the last set directory with the method
254 <SfxApplication::SetLastDir_Impl()> in SFX is returned.
256 This is usually the most recently addressed by the
257 SfxFileDialog directory.
260 <SfxApplication::SetLastDir_Impl()>
264 return pAppData_Impl
->aLastDir
;
267 const OUString
& SfxApplication::GetLastSaveDirectory() const
271 As <SfxApplication::GetLastDir_Impl()>, only external
274 <SfxApplication::GetLastDir_Impl()>
278 return GetLastDir_Impl();
283 void SfxApplication::SetLastDir_Impl
285 const OUString
& rNewDir
/* Complete directory path as a string */
290 Internal Method, by which a directory path is set that was last addressed
291 (eg by the SfxFileDialog).
294 <SfxApplication::GetLastDir_Impl()>
298 pAppData_Impl
->aLastDir
= rNewDir
;
303 void SfxApplication::ResetLastDir()
305 pAppData_Impl
->aLastDir
.clear();
310 SfxDispatcher
* SfxApplication::GetDispatcher_Impl()
312 return pAppData_Impl
->pViewFrame
? pAppData_Impl
->pViewFrame
->GetDispatcher(): pAppData_Impl
->pAppDispat
;
316 void SfxApplication::SetViewFrame_Impl( SfxViewFrame
*pFrame
)
318 if ( pFrame
!= pAppData_Impl
->pViewFrame
)
320 // get the containerframes ( if one of the frames is an InPlaceFrame )
321 SfxViewFrame
*pOldContainerFrame
= pAppData_Impl
->pViewFrame
;
322 while ( pOldContainerFrame
&& pOldContainerFrame
->GetParentViewFrame_Impl() )
323 pOldContainerFrame
= pOldContainerFrame
->GetParentViewFrame_Impl();
324 SfxViewFrame
*pNewContainerFrame
= pFrame
;
325 while ( pNewContainerFrame
&& pNewContainerFrame
->GetParentViewFrame_Impl() )
326 pNewContainerFrame
= pNewContainerFrame
->GetParentViewFrame_Impl();
328 // DocWinActivate : both frames belong to the same TopWindow
329 // TopWinActivate : both frames belong to different TopWindows
331 bool bTaskActivate
= pOldContainerFrame
!= pNewContainerFrame
;
333 if ( pOldContainerFrame
)
336 NotifyEvent( SfxViewEventHint( SFX_EVENT_DEACTIVATEDOC
, GlobalEventConfig::GetEventName(GlobalEventId::DEACTIVATEDOC
), pOldContainerFrame
->GetObjectShell(), pOldContainerFrame
->GetFrame().GetController() ) );
337 pOldContainerFrame
->DoDeactivate( bTaskActivate
, pFrame
);
339 if( pOldContainerFrame
->GetProgress() )
340 pOldContainerFrame
->GetProgress()->Suspend();
343 pAppData_Impl
->pViewFrame
= pFrame
;
345 if( pNewContainerFrame
)
347 pNewContainerFrame
->DoActivate( bTaskActivate
);
348 if ( bTaskActivate
&& pNewContainerFrame
->GetObjectShell() )
350 pNewContainerFrame
->GetObjectShell()->PostActivateEvent_Impl( pNewContainerFrame
);
351 NotifyEvent(SfxViewEventHint(SFX_EVENT_ACTIVATEDOC
, GlobalEventConfig::GetEventName(GlobalEventId::ACTIVATEDOC
), pNewContainerFrame
->GetObjectShell(), pNewContainerFrame
->GetFrame().GetController() ) );
354 SfxProgress
*pProgress
= pNewContainerFrame
->GetProgress();
357 if( pProgress
->IsSuspended() )
360 pProgress
->SetState( pProgress
->GetState() );
363 if ( pAppData_Impl
->pViewFrame
->GetViewShell() )
365 SfxDispatcher
* pDisp
= pAppData_Impl
->pViewFrame
->GetDispatcher();
367 pDisp
->Update_Impl(true);
372 // even if the frame actually didn't change, ensure its document is forwarded
373 // to SfxObjectShell::SetCurrentComponent.
374 // Otherwise, the CurrentComponent might not be correct, in case it has meanwhile
375 // been reset to some other document, by some non-SFX component.
376 // #i49133# / 2007-12-19 / frank.schoenheit@sun.com
377 if ( pFrame
&& pFrame
->GetViewShell() )
378 pFrame
->GetViewShell()->SetCurrentDocument();
381 ResMgr
* SfxApplication::GetSfxResManager()
383 return SfxResId::GetResMgr();
388 void SfxApplication::SetProgress_Impl
390 SfxProgress
*pProgress
394 DBG_ASSERT( ( !pAppData_Impl
->pProgress
&& pProgress
) ||
395 ( pAppData_Impl
->pProgress
&& !pProgress
),
396 "Progress acitivation/deacitivation mismatch" );
398 if ( pAppData_Impl
->pProgress
&& pProgress
)
400 pAppData_Impl
->pProgress
->Suspend();
401 pAppData_Impl
->pProgress
->UnLock();
402 delete pAppData_Impl
->pProgress
;
405 pAppData_Impl
->pProgress
= pProgress
;
410 sal_uInt16
SfxApplication::GetFreeIndex()
412 return pAppData_Impl
->aIndexBitSet
.GetFreeIndex()+1;
417 void SfxApplication::ReleaseIndex(sal_uInt16 i
)
419 pAppData_Impl
->aIndexBitSet
.ReleaseIndex(i
-1);
424 vcl::Window
* SfxApplication::GetTopWindow() const
426 SfxWorkWindow
* pWork
= GetWorkWindow_Impl( SfxViewFrame::Current() );
427 return pWork
? pWork
->GetWindow() : NULL
;
430 SfxTbxCtrlFactArr_Impl
& SfxApplication::GetTbxCtrlFactories_Impl() const
432 return *pAppData_Impl
->pTbxCtrlFac
;
435 SfxStbCtrlFactArr_Impl
& SfxApplication::GetStbCtrlFactories_Impl() const
437 return *pAppData_Impl
->pStbCtrlFac
;
440 SfxMenuCtrlFactArr_Impl
& SfxApplication::GetMenuCtrlFactories_Impl() const
442 return *pAppData_Impl
->pMenuCtrlFac
;
445 SfxViewFrameArr_Impl
& SfxApplication::GetViewFrames_Impl() const
447 return *pAppData_Impl
->pViewFrames
;
450 SfxViewShellArr_Impl
& SfxApplication::GetViewShells_Impl() const
452 return *pAppData_Impl
->pViewShells
;
455 SfxObjectShellArr_Impl
& SfxApplication::GetObjectShells_Impl() const
457 return *pAppData_Impl
->pObjShells
;
460 void SfxApplication::Invalidate( sal_uInt16 nId
)
462 for( SfxViewFrame
* pFrame
= SfxViewFrame::GetFirst(); pFrame
; pFrame
= SfxViewFrame::GetNext( *pFrame
) )
463 Invalidate_Impl( pFrame
->GetBindings(), nId
);
466 #if HAVE_FEATURE_SCRIPTING
468 #ifndef DISABLE_DYNLOADING
470 typedef long (SAL_CALL
*basicide_handle_basic_error
)(void*);
471 typedef void* (SAL_CALL
*basicide_macro_organizer
)(sal_Int16
);
473 extern "C" { static void SAL_CALL
thisModule() {} }
477 extern "C" long basicide_handle_basic_error(void*);
478 extern "C" void *basicide_macro_organizer(sal_Int16
);
484 IMPL_LINK_TYPED( SfxApplication
, GlobalBasicErrorHdl_Impl
, StarBASIC
*, pStarBasic
, bool )
486 #if !HAVE_FEATURE_SCRIPTING
491 #ifndef DISABLE_DYNLOADING
492 // load basctl module
494 aMod
.loadRelative(&thisModule
, SVLIBRARY("basctl"), 0);
497 basicide_handle_basic_error pSymbol
= reinterpret_cast<basicide_handle_basic_error
>(aMod
.getFunctionSymbol("basicide_handle_basic_error"));
501 // call basicide_handle_basic_error in basctl
502 bool nRet
= pSymbol
&& pSymbol( pStarBasic
);
506 bool nRet
= basicide_handle_basic_error( pStarBasic
);
515 bool SfxApplication::IsXScriptURL( const OUString
& rScriptURL
)
519 #if !HAVE_FEATURE_SCRIPTING
522 ::com::sun::star::uno::Reference
523 < ::com::sun::star::uno::XComponentContext
> xContext
=
524 ::comphelper::getProcessComponentContext();
526 ::com::sun::star::uno::Reference
527 < ::com::sun::star::uri::XUriReferenceFactory
>
528 xFactory
= ::com::sun::star::uri::UriReferenceFactory::create( xContext
);
532 ::com::sun::star::uno::Reference
533 < ::com::sun::star::uri::XVndSunStarScriptUrl
>
534 xUrl( xFactory
->parse( rScriptURL
),
535 ::com::sun::star::uno::UNO_QUERY
);
542 catch (const ::com::sun::star::uno::RuntimeException
&)
544 // ignore, will just return FALSE
551 SfxApplication::ChooseScript()
555 #if HAVE_FEATURE_SCRIPTING
556 SfxAbstractDialogFactory
* pFact
= SfxAbstractDialogFactory::Create();
559 SAL_INFO( "sfx.appl", "create selector dialog");
561 const SfxViewFrame
* pViewFrame
= SfxViewFrame::Current();
562 const SfxFrame
* pFrame
= pViewFrame
? &pViewFrame
->GetFrame() : NULL
;
563 uno::Reference
< frame::XFrame
> xFrame( pFrame
? pFrame
->GetFrameInterface() : uno::Reference
< frame::XFrame
>() );
565 boost::scoped_ptr
<AbstractScriptSelectorDialog
> pDlg(
566 pFact
->CreateScriptSelectorDialog( NULL
, false, xFrame
));
568 SAL_INFO( "sfx.appl", "done, now exec it");
570 sal_uInt16 nRet
= pDlg
->Execute();
572 SAL_INFO( "sfx.appl", "has returned");
574 if ( nRet
== RET_OK
)
576 aScriptURL
= pDlg
->GetScriptURL();
583 void SfxApplication::MacroOrganizer( sal_Int16 nTabId
)
585 #if !HAVE_FEATURE_SCRIPTING
589 #ifndef DISABLE_DYNLOADING
590 // load basctl module
592 aMod
.loadRelative(&thisModule
, SVLIBRARY("basctl"), 0);
595 basicide_macro_organizer pSymbol
= reinterpret_cast<basicide_macro_organizer
>(aMod
.getFunctionSymbol("basicide_macro_organizer"));
599 SAL_WARN_IF(!pSymbol
, "sfx.doc", "SfxApplication::MacroOrganizer, no symbol!");
603 // call basicide_macro_organizer in basctl
608 basicide_macro_organizer( nTabId
);
615 ErrCode
SfxApplication::CallBasic( const OUString
& rCode
, BasicManager
* pMgr
, SbxArray
* pArgs
, SbxValue
* pRet
)
617 #if !HAVE_FEATURE_SCRIPTING
622 return ERRCODE_BASIC_CANNOT_LOAD
;
624 return pMgr
->ExecuteMacro( rCode
, pArgs
, pRet
);
628 sfx2::sidebar::Theme
& SfxApplication::GetSidebarTheme()
630 if (!pAppData_Impl
->m_pSidebarTheme
.is())
632 pAppData_Impl
->m_pSidebarTheme
.set(new sfx2::sidebar::Theme
);
633 pAppData_Impl
->m_pSidebarTheme
->InitializeTheme();
635 return *pAppData_Impl
->m_pSidebarTheme
;
638 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */