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