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 .
21 #include <basic/vbahelper.hxx>
26 #include <com/sun/star/container/XEnumeration.hpp>
27 #include <com/sun/star/frame/Desktop.hpp>
28 #include <com/sun/star/frame/XModel2.hpp>
29 #include <com/sun/star/frame/ModuleManager.hpp>
30 #include <comphelper/processfactory.hxx>
32 namespace basic::vba
{
34 using namespace ::com::sun::star
;
39 /** Create an instance of a module manager.
41 uno::Reference
< frame::XModuleManager2
> lclCreateModuleManager()
43 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext(), uno::UNO_SET_THROW
);
44 return frame::ModuleManager::create(xContext
);
47 typedef std::vector
<uno::Reference
<frame::XModel
>> ModelVector
;
49 ModelVector
CreateDocumentsEnumeration(
50 const uno::Reference
< frame::XModel
>& rxModel
)
55 uno::Reference
< frame::XModuleManager2
> xModuleManager( lclCreateModuleManager() );
56 OUString aIdentifier
= xModuleManager
->identify( rxModel
);
57 uno::Reference
< frame::XDesktop2
> xDesktop
= frame::Desktop::create( ::comphelper::getProcessComponentContext() );
58 uno::Reference
< container::XEnumerationAccess
> xComponentsEA( xDesktop
->getComponents(), uno::UNO_SET_THROW
);
59 uno::Reference
< container::XEnumeration
> xEnumeration( xComponentsEA
->createEnumeration(), uno::UNO_SET_THROW
);
60 while( xEnumeration
->hasMoreElements() )
62 uno::Reference
< frame::XModel
> xCurrModel( xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
63 if( xModuleManager
->identify( xCurrModel
) == aIdentifier
)
64 models
.push_back( xCurrModel
);
67 catch(const uno::Exception
& )
73 /** Locks or unlocks the controllers of the specified document model.
75 void lclLockControllers( const uno::Reference
< frame::XModel
>& rxModel
, bool bLockControllers
)
77 if( rxModel
.is() ) try
79 if( bLockControllers
)
80 rxModel
->lockControllers();
82 rxModel
->unlockControllers();
84 catch(const uno::Exception
& )
90 /** Enables or disables the container windows of all controllers of the
91 specified document model.
93 void lclEnableContainerWindows( const uno::Reference
< frame::XModel
>& rxModel
, bool bEnableWindows
)
97 uno::Reference
< frame::XModel2
> xModel2( rxModel
, uno::UNO_QUERY_THROW
);
98 uno::Reference
< container::XEnumeration
> xControllersEnum( xModel2
->getControllers(), uno::UNO_SET_THROW
);
99 // iterate over all controllers
100 while( xControllersEnum
->hasMoreElements() )
104 uno::Reference
< frame::XController
> xController( xControllersEnum
->nextElement(), uno::UNO_QUERY_THROW
);
105 uno::Reference
< frame::XFrame
> xFrame( xController
->getFrame(), uno::UNO_SET_THROW
);
106 uno::Reference
< awt::XWindow
> xWindow( xFrame
->getContainerWindow(), uno::UNO_SET_THROW
);
107 xWindow
->setEnable( bEnableWindows
);
109 catch(const uno::Exception
& )
114 catch(const uno::Exception
& )
120 typedef void (*ModifyDocumentFunc
)( const uno::Reference
< frame::XModel
>&, bool );
122 /** Implementation iterating over all documents that have the same type as the
123 specified model, and calling the passed functor.
125 void lclIterateDocuments( ModifyDocumentFunc pModifyDocumentFunc
, const uno::Reference
< frame::XModel
>& rxModel
, bool bModificator
)
127 ModelVector
models(CreateDocumentsEnumeration(rxModel
));
128 // iterate over all open documents
129 for (auto const& xCurrModel
: models
)
133 pModifyDocumentFunc(xCurrModel
, bModificator
);
135 catch (const uno::Exception
&)
145 std::map
< OUString
, OUString
> maCurrDirs
;
151 void lockControllersOfAllDocuments( const uno::Reference
< frame::XModel
>& rxModel
, bool bLockControllers
)
153 lclIterateDocuments( &lclLockControllers
, rxModel
, bLockControllers
);
157 void enableContainerWindowsOfAllDocuments( const uno::Reference
< frame::XModel
>& rxModel
, bool bEnableWindows
)
159 lclIterateDocuments( &lclEnableContainerWindows
, rxModel
, bEnableWindows
);
163 void registerCurrentDirectory( const uno::Reference
< frame::XModel
>& rxModel
, const OUString
& rPath
)
165 if( rPath
.isEmpty() )
168 static CurrDirPool StaticCurrDirPool
;
170 CurrDirPool
& rPool
= StaticCurrDirPool
;
171 std::unique_lock
aGuard( rPool
.maMutex
);
174 uno::Reference
< frame::XModuleManager2
> xModuleManager( lclCreateModuleManager() );
175 OUString aIdentifier
= xModuleManager
->identify( rxModel
);
176 if( !aIdentifier
.isEmpty() )
177 rPool
.maCurrDirs
[ aIdentifier
] = rPath
;
179 catch(const uno::Exception
& )
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */