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