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