xref: /aoo4110/main/oox/inc/oox/ole/vbaproject.hxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef OOX_OLE_VBAPROJECT_HXX
25 #define OOX_OLE_VBAPROJECT_HXX
26 
27 #include <map>
28 #include <com/sun/star/uno/XInterface.hpp>
29 #include "oox/helper/refvector.hxx"
30 #include "oox/helper/storagebase.hxx"
31 #include "oox/dllapi.h"
32 
33 namespace com { namespace sun { namespace star {
34     namespace container { class XNameContainer; }
35     namespace document { class XEventsSupplier; }
36     namespace frame { class XModel; }
37     namespace script { class XLibraryContainer; }
38     namespace script { namespace vba { class XVBAMacroResolver; } }
39     namespace uno { class XComponentContext; }
40 } } }
41 
42 namespace oox { class GraphicHelper; }
43 
44 namespace oox {
45 namespace ole {
46 
47 // ============================================================================
48 
49 class OOX_DLLPUBLIC VbaFilterConfig
50 {
51 public:
52     explicit            VbaFilterConfig(
53                             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
54                             const ::rtl::OUString& rConfigCompName );
55                         ~VbaFilterConfig();
56 
57     /** Returns true, if the VBA source code and forms should be imported. */
58     bool                isImportVba() const;
59     /** Returns true, if the VBA source code should be imported executable. */
60     bool                isImportVbaExecutable() const;
61     /** Returns true, if the VBA source code and forms should be exported. */
62     bool                isExportVba() const;
63 
64 private:
65     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
66                         mxConfigAccess;
67 };
68 
69 // ============================================================================
70 
71 /** Base class for objects that attach a amcro to a specific action.
72 
73     Purpose is to collect objects that need to attach a VBA macro to an action.
74     The VBA project will be loaded at a very late point of the document import
75     process, because it depends on an initialized core document model (e.g.
76     spreadsheet codenames). Some objects that want to attach a VBA macro to an
77     action (e.g. mouse click action for drawing shapes) are loaded long before
78     the VBA project. The drawback is that in most cases macros are specified
79     without module name, or the VBA project name is part of the macro name.
80     In the former case, all code modules have to be scanned for the macro to be
81     able to create a valid script URL.
82 
83     The import code will register these requests to attach a VBA macro with an
84     instance of a class derived from this base class. The derived class will
85     store all information needed to finally attach the macro to the action,
86     once the VBA project has been imported.
87  */
88 class VbaMacroAttacherBase
89 {
90 public:
91     explicit            VbaMacroAttacherBase( const ::rtl::OUString& rMacroName );
92     virtual             ~VbaMacroAttacherBase();
93 
94     /** Resolves the internal macro name to the related macro URL, and attaches
95         the macro to the object. */
96     void                resolveAndAttachMacro(
97                             const ::com::sun::star::uno::Reference< ::com::sun::star::script::vba::XVBAMacroResolver >& rxResolver );
98 
99 private:
100     /** Called after the VBA project has been imported. Derived classes will
101         attach the passed script to the object represented by this instance. */
102     virtual void        attachMacro( const ::rtl::OUString& rScriptUrl ) = 0;
103 
104 private:
105     ::rtl::OUString     maMacroName;
106 };
107 
108 typedef ::boost::shared_ptr< VbaMacroAttacherBase > VbaMacroAttacherRef;
109 
110 // ============================================================================
111 
112 class OOX_DLLPUBLIC VbaProject : public VbaFilterConfig
113 {
114 public:
115     explicit            VbaProject(
116                             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
117                             const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >& rxDocModel,
118                             const ::rtl::OUString& rConfigCompName );
119     virtual             ~VbaProject();
120 
121     /** Imports the entire VBA project from the passed storage.
122 
123         @param rVbaPrjStrg  The root storage of the entire VBA project.
124      */
125     void                importVbaProject(
126                             StorageBase& rVbaPrjStrg,
127                             const GraphicHelper& rGraphicHelper,
128                             bool bDefaultColorBgr = true );
129 
130     /** Registers a macro atatcher object. For details, see description of the
131         VbaMacroAttacherBase class. */
132     void                registerMacroAttacher( const VbaMacroAttacherRef& rxAttacher );
133 
134     /** Returns true, if the document contains at least one code module. */
135     bool                hasModules() const;
136     /** Returns true, if the document contains the specified code module. */
137     bool                hasModule( const ::rtl::OUString& rModuleName ) const;
138 
139     /** Returns true, if the document contains at least one dialog. */
140     bool                hasDialogs() const;
141     /** Returns true, if the document contains the specified dialog. */
142     bool                hasDialog( const ::rtl::OUString& rDialogName ) const;
143 
144 protected:
145     /** Registers a dummy module that will be created when the VBA project is
146         imported. */
147     void                addDummyModule( const ::rtl::OUString& rName, sal_Int32 nType );
148 
149     /** Called when the import process of the VBA project has been started. */
150     virtual void        prepareImport();
151     /** Called when the import process of the VBA project is finished. */
152     virtual void        finalizeImport();
153 
154 private:
155                         VbaProject( const VbaProject& );
156     VbaProject&         operator=( const VbaProject& );
157 
158     /** Returns the Basic or dialog library container. */
159     ::com::sun::star::uno::Reference< ::com::sun::star::script::XLibraryContainer >
160                         getLibraryContainer( sal_Int32 nPropId );
161     /** Opens a Basic or dialog library (creates missing if specified). */
162     ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >
163                         openLibrary( sal_Int32 nPropId, bool bCreateMissing );
164     /** Creates and returns the Basic library of the document used for import. */
165     ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >
166                         createBasicLibrary();
167     /** Creates and returns the dialog library of the document used for import. */
168     ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >
169                         createDialogLibrary();
170 
171     /** Imports the VBA code modules and forms. */
172     void                importVba(
173                             StorageBase& rVbaPrjStrg,
174                             const GraphicHelper& rGraphicHelper,
175                             bool bDefaultColorBgr );
176 
177     /** Attaches VBA macros to objects registered via registerMacroAttacher(). */
178     void                attachMacros();
179 
180     /** Copies the entire VBA project storage to the passed document model. */
181     void                copyStorage( StorageBase& rVbaPrjStrg );
182 
183 private:
184     typedef RefVector< VbaMacroAttacherBase >           MacroAttacherVector;
185     typedef ::std::map< ::rtl::OUString, sal_Int32 >    DummyModuleMap;
186 
187     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >
188                         mxContext;          /// Component context with service manager.
189     ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >
190                         mxDocModel;         /// Document model used to import/export the VBA project.
191     ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >
192                         mxBasicLib;         /// The Basic library of the document used for import.
193     ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >
194                         mxDialogLib;        /// The dialog library of the document used for import.
195     MacroAttacherVector maMacroAttachers;   /// Objects that want to attach a VBA macro to an action.
196     DummyModuleMap      maDummyModules;     /// Additional empty modules created on import.
197     ::rtl::OUString     maPrjName;          /// Name of the VBA project.
198 };
199 
200 // ============================================================================
201 
202 } // namespace ole
203 } // namespace oox
204 
205 #endif
206