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