Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / include / oox / ole / vbaproject.hxx
blobcc9763ccb46d9c6caaeb09a49b2f26ec655b946c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #ifndef INCLUDED_OOX_OLE_VBAPROJECT_HXX
21 #define INCLUDED_OOX_OLE_VBAPROJECT_HXX
23 #include <functional>
24 #include <map>
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <oox/dllapi.h>
28 #include <oox/helper/refmap.hxx>
29 #include <oox/helper/refvector.hxx>
30 #include <rtl/ustring.hxx>
31 #include <sal/types.h>
33 namespace com { namespace sun { namespace star {
34 namespace container { class XNameContainer; }
35 namespace frame { class XModel; }
36 namespace script { class XLibraryContainer; }
37 namespace script { namespace vba { class XVBAMacroResolver; } }
38 namespace uno { class XComponentContext; }
39 namespace uno { class XInterface; }
40 } } }
42 namespace oox {
43 class GraphicHelper;
44 class StorageBase;
47 namespace oox {
48 namespace ole {
50 class VbaModule;
52 class OOX_DLLPUBLIC VbaFilterConfig
54 public:
55 explicit VbaFilterConfig(
56 const css::uno::Reference< css::uno::XComponentContext >& rxContext,
57 const OUString& rConfigCompName );
58 ~VbaFilterConfig();
60 /** Returns true, if the VBA source code and forms should be imported. */
61 bool isImportVba() const;
62 /** Returns true, if the VBA source code should be imported executable. */
63 bool isImportVbaExecutable() const;
64 /** Returns true, if the VBA source code and forms should be exported. */
65 bool isExportVba() const;
67 private:
68 css::uno::Reference< css::uno::XInterface >
69 mxConfigAccess;
73 /** Base class for objects that attach a macro to a specific action.
75 Purpose is to collect objects that need to attach a VBA macro to an action.
76 The VBA project will be loaded at a very late point of the document import
77 process, because it depends on an initialized core document model (e.g.
78 spreadsheet codenames). Some objects that want to attach a VBA macro to an
79 action (e.g. mouse click action for drawing shapes) are loaded long before
80 the VBA project. The drawback is that in most cases macros are specified
81 without module name, or the VBA project name is part of the macro name.
82 In the former case, all code modules have to be scanned for the macro to be
83 able to create a valid script URL.
85 The import code will register these requests to attach a VBA macro with an
86 instance of a class derived from this base class. The derived class will
87 store all information needed to finally attach the macro to the action,
88 once the VBA project has been imported.
90 class OOX_DLLPUBLIC VbaMacroAttacherBase
92 public:
93 explicit VbaMacroAttacherBase( const OUString& rMacroName );
94 virtual ~VbaMacroAttacherBase();
96 /** Resolves the internal macro name to the related macro URL, and attaches
97 the macro to the object. */
98 void resolveAndAttachMacro(
99 const css::uno::Reference< css::script::vba::XVBAMacroResolver >& rxResolver );
101 private:
102 /** Called after the VBA project has been imported. Derived classes will
103 attach the passed script to the object represented by this instance. */
104 virtual void attachMacro( const OUString& rScriptUrl ) = 0;
106 private:
107 OUString maMacroName;
110 typedef std::shared_ptr< VbaMacroAttacherBase > VbaMacroAttacherRef;
113 class OOX_DLLPUBLIC VbaProject : public VbaFilterConfig
115 public:
116 explicit VbaProject(
117 const css::uno::Reference< css::uno::XComponentContext >& rxContext,
118 const css::uno::Reference< css::frame::XModel >& rxDocModel,
119 const OUString& rConfigCompName );
120 virtual ~VbaProject();
122 /** Imports the entire VBA project from the passed storage.
124 @param rVbaPrjStrg The root storage of the entire VBA project.
126 void importVbaProject(
127 StorageBase& rVbaPrjStrg,
128 const GraphicHelper& rGraphicHelper );
130 bool importVbaProject(
131 StorageBase& rVbaPrjStrg );
133 /** Reads vba module related information from the project streams */
134 void readVbaModules( StorageBase& rVbaPrjStrg );
135 /** Imports (and creates) vba modules and user forms from the vba project records previously read.
136 Note: ( expects that readVbaModules was already called ) */
137 void importModulesAndForms( StorageBase& rVbaPrjStrg, const GraphicHelper& rGraphicHelper, bool bDefaultColorBgr = true );
138 /** Registers a macro attacher object. For details, see description of the
139 VbaMacroAttacherBase class. */
140 void registerMacroAttacher( const VbaMacroAttacherRef& rxAttacher );
142 /** Attaches VBA macros to objects registered via registerMacroAttacher(). */
143 void attachMacros();
145 /** Returns true, if the document contains at least one code module. */
146 bool hasModules() const;
148 /** Returns true, if the document contains at least one dialog. */
149 bool hasDialogs() const;
151 void setOleOverridesSink( css::uno::Reference< css::container::XNameContainer >& rxOleOverridesSink ){ mxOleOverridesSink = rxOleOverridesSink; }
153 protected:
154 /** Registers a dummy module that will be created when the VBA project is
155 imported. */
156 void addDummyModule( const OUString& rName, sal_Int32 nType );
158 /** Called when the import process of the VBA project has been started. */
159 virtual void prepareImport();
161 private:
162 VbaProject( const VbaProject& ) = delete;
163 VbaProject& operator=( const VbaProject& ) = delete;
165 /** Returns the Basic or dialog library container. */
166 css::uno::Reference< css::script::XLibraryContainer >
167 getLibraryContainer( sal_Int32 nPropId );
168 /** Opens a Basic or dialog library (creates missing if specified). */
169 css::uno::Reference< css::container::XNameContainer >
170 openLibrary( sal_Int32 nPropId, bool bCreateMissing );
171 /** Creates and returns the Basic library of the document used for import. */
172 css::uno::Reference< css::container::XNameContainer >
173 createBasicLibrary();
174 /** Creates and returns the dialog library of the document used for import. */
175 css::uno::Reference< css::container::XNameContainer >
176 createDialogLibrary();
178 /** Imports the VBA code modules and forms. */
179 void importVba(
180 StorageBase& rVbaPrjStrg,
181 const GraphicHelper& rGraphicHelper,
182 bool bDefaultColorBgr );
184 /** Copies the entire VBA project storage to the passed document model. */
185 void copyStorage( StorageBase& rVbaPrjStrg );
187 private:
188 typedef RefVector< VbaMacroAttacherBase > MacroAttacherVector;
189 typedef ::std::map< OUString, sal_Int32 > DummyModuleMap;
191 css::uno::Reference< css::uno::XComponentContext >
192 mxContext; ///< Component context with service manager.
193 css::uno::Reference< css::frame::XModel >
194 mxDocModel; ///< Document model used to import/export the VBA project.
195 css::uno::Reference< css::container::XNameContainer >
196 mxBasicLib; ///< The Basic library of the document used for import.
197 css::uno::Reference< css::container::XNameContainer >
198 mxDialogLib; ///< The dialog library of the document used for import.
199 MacroAttacherVector maMacroAttachers; ///< Objects that want to attach a VBA macro to an action.
200 DummyModuleMap maDummyModules; ///< Additional empty modules created on import.
201 OUString maPrjName; ///< Name of the VBA project.
202 css::uno::Reference< css::container::XNameContainer >
203 mxOleOverridesSink;
204 typedef RefMap< rtl::OUString, VbaModule > VbaModuleMap;
205 VbaModuleMap maModules;
206 VbaModuleMap maModulesByStrm;
210 } // namespace ole
211 } // namespace oox
213 #endif
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */