diff options
author | Tamas Bunth <tamas.bunth@collabora.co.uk> | 2017-07-11 12:22:06 +0200 |
---|---|---|
committer | Tamás Bunth <btomi96@gmail.com> | 2017-07-14 23:24:44 +0200 |
commit | 07fb14be7f79cbbf65f47458b7b2337c8ffcc4f7 (patch) | |
tree | 01a47a0db59f9cfbb5394cfa418dba5c58329629 /sc/source/ui/vba | |
parent | d6a16aedca28ad5a907104c02174abd88fb03aed (diff) |
implement Application.FileDialog attribute
Documentation:
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-filedialog-property-excel
https://msdn.microsoft.com/VBA/Office-Shared-VBA/articles/filedialog-object-office
Using FilePicker and FolderPicker uno services.
Change-Id: Ifd3b3fc9c135efb0663d2ef36ecbe2b2fbe6132f
Reviewed-on: https://gerrit.libreoffice.org/39806
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Tamás Bunth <btomi96@gmail.com>
Diffstat (limited to 'sc/source/ui/vba')
-rw-r--r-- | sc/source/ui/vba/vbaapplication.cxx | 16 | ||||
-rw-r--r-- | sc/source/ui/vba/vbaapplication.hxx | 6 | ||||
-rw-r--r-- | sc/source/ui/vba/vbafiledialog.cxx | 141 | ||||
-rw-r--r-- | sc/source/ui/vba/vbafiledialog.hxx | 56 | ||||
-rw-r--r-- | sc/source/ui/vba/vbafiledialogitems.cxx | 119 | ||||
-rw-r--r-- | sc/source/ui/vba/vbafiledialogitems.hxx | 42 |
6 files changed, 379 insertions, 1 deletions
diff --git a/sc/source/ui/vba/vbaapplication.cxx b/sc/source/ui/vba/vbaapplication.cxx index f751ccff7f65..1805a3f950b1 100644 --- a/sc/source/ui/vba/vbaapplication.cxx +++ b/sc/source/ui/vba/vbaapplication.cxx @@ -55,6 +55,7 @@ #include "sc.hrc" #include "macromgr.hxx" #include "defaultsoptions.hxx" +#include "vbafiledialog.hxx" #include <osl/file.hxx> #include <rtl/instance.hxx> @@ -126,7 +127,8 @@ struct ScVbaStaticAppSettings : public ::rtl::Static< ScVbaAppSettings, ScVbaSta ScVbaApplication::ScVbaApplication( const uno::Reference<uno::XComponentContext >& xContext ) : ScVbaApplication_BASE( xContext ), - mrAppSettings( ScVbaStaticAppSettings::get() ) + mrAppSettings( ScVbaStaticAppSettings::get() ), + m_nDialogType(0) { } @@ -313,6 +315,18 @@ ScVbaApplication::International( sal_Int32 /*Index*/ ) } uno::Any SAL_CALL +ScVbaApplication::FileDialog( const uno::Any& DialogType ) +{ + sal_Int32 nType = 0; + DialogType >>= nType; + + m_nDialogType = nType; + if( !m_xFileDialog || nType != m_nDialogType ) + m_xFileDialog = uno::Reference<excel::XFileDialog> ( new ScVbaFileDialog( this, mxContext, nType )); + return uno::Any( m_xFileDialog ); +} + +uno::Any SAL_CALL ScVbaApplication::Workbooks( const uno::Any& aIndex ) { uno::Reference< XCollection > xWorkBooks( new ScVbaWorkbooks( this, mxContext ) ); diff --git a/sc/source/ui/vba/vbaapplication.hxx b/sc/source/ui/vba/vbaapplication.hxx index 49180d214055..34d62b7b1f87 100644 --- a/sc/source/ui/vba/vbaapplication.hxx +++ b/sc/source/ui/vba/vbaapplication.hxx @@ -21,6 +21,7 @@ #include <ooo/vba/excel/XWorksheetFunction.hpp> #include <ooo/vba/excel/XApplication.hpp> +#include <ooo/vba/excel/XFileDialog.hpp> #include <com/sun/star/uno/XComponentContext.hpp> #include <vbahelper/vbahelperinterface.hxx> @@ -37,6 +38,10 @@ private: // note: member variables moved to struct "ScVbaAppSettings", see cxx file, to be shared by all application instances ScVbaAppSettings& mrAppSettings; + // must be stored in order to get result paths from the same instance + css::uno::Reference< ov::excel::XFileDialog > m_xFileDialog; + sal_Int32 m_nDialogType; + /// @throws css::uno::RuntimeException OUString getOfficePath( const OUString& sPath ); @@ -85,6 +90,7 @@ public: virtual css::uno::Reference< ov::XAssistant > SAL_CALL getAssistant() override; virtual css::uno::Reference< ov::excel::XWorkbook > SAL_CALL getThisWorkbook() override; virtual css::uno::Any SAL_CALL International( sal_Int32 Index ) override; + virtual css::uno::Any SAL_CALL FileDialog( const css::uno::Any& DialogType ) override; virtual css::uno::Any SAL_CALL Workbooks( const css::uno::Any& aIndex ) override; virtual css::uno::Any SAL_CALL Worksheets( const css::uno::Any& aIndex ) override; virtual css::uno::Any SAL_CALL WorksheetFunction( ) override; diff --git a/sc/source/ui/vba/vbafiledialog.cxx b/sc/source/ui/vba/vbafiledialog.cxx new file mode 100644 index 000000000000..1129953dff02 --- /dev/null +++ b/sc/source/ui/vba/vbafiledialog.cxx @@ -0,0 +1,141 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "vbafiledialog.hxx" + +#include <ooo/vba/office/MsoFileDialogType.hpp> + +#include <com/sun/star/container/XIndexAccess.hpp> +#include <com/sun/star/ui/dialogs/FilePicker.hpp> +#include <com/sun/star/ui/dialogs/FolderPicker.hpp> +#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp> +#include <com/sun/star/ui/dialogs/TemplateDescription.hpp> + +using namespace ::com::sun::star; +using namespace ::ooo::vba; + +ScVbaFileDialog::ScVbaFileDialog( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const sal_Int32 nType ) + : ScVbaFileDialog_BASE( xParent, xContext) + , m_nType(nType) + , m_sTitle("FileDialog") +{} + +uno::Any +ScVbaFileDialog::getInitialFileName() { return uno::makeAny( m_sInitialFileName ); } + +void ScVbaFileDialog::setInitialFileName( const css::uno::Any& rName ) +{ + rName >>= m_sInitialFileName; +} + +css::uno::Any ScVbaFileDialog::getTitle() { return uno::makeAny( m_sTitle ); } + +void ScVbaFileDialog::setTitle( const css::uno::Any& rTitle ) +{ + rTitle >>= m_sTitle; +} + +uno::Reference< excel::XFileDialogSelectedItems > SAL_CALL ScVbaFileDialog::getSelectedItems() +{ + // TODO use InitialFileName when m_xItems is empty + return m_xItems; +} + +sal_Int32 ScVbaFileDialog::Show() +{ + std::vector<OUString> sSelectedPaths; + sal_Int32 nRet = -1; + + switch (m_nType) + { + case office::MsoFileDialogType::msoFileDialogOpen: + // TODO implement + break; + case office::MsoFileDialogType::msoFileDialogSaveAs: + // TODO implement + break; + case office::MsoFileDialogType::msoFileDialogFilePicker: + { + uno::Reference<ui::dialogs::XFilePicker3> xFilePicker = + ui::dialogs::FilePicker::createWithMode( + mxContext, ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE ); + + // TODO set initial directory + + if( xFilePicker->execute() != ui::dialogs::ExecutableDialogResults::OK) + { + nRet = 0; // cancel pressed + break; + } + + for( auto& sPath : xFilePicker->getSelectedFiles() ) + { + sSelectedPaths.push_back(sPath); + } + } + break; + case office::MsoFileDialogType::msoFileDialogFolderPicker: + { + uno::Reference< ui::dialogs::XFolderPicker2 > xFolderPicker = + ui::dialogs::FolderPicker::create(mxContext); + + // TODO set initial directory + + if( xFolderPicker->execute() != ui::dialogs::ExecutableDialogResults::OK) + { + nRet = 0; // cancel pressed + break; + } + + OUString sPath = xFolderPicker->getDirectory(); + + if(!sPath.isEmpty()) + sSelectedPaths.push_back(sPath); + + } + break; + default: + throw uno::RuntimeException(); + } + + m_xItems = css::uno::Reference< ov::excel::XFileDialogSelectedItems >( + new ScVbaFileDialogSelectedItems(this, mxContext, sSelectedPaths) ); + return nRet; +} + +// XHelperInterface +OUString +ScVbaFileDialog::getServiceImplName() +{ + return OUString("ScVbaFileDialog"); +} + +uno::Sequence<OUString> +ScVbaFileDialog::getServiceNames() +{ + static uno::Sequence< OUString > aServiceNames; + if ( aServiceNames.getLength() == 0 ) + { + aServiceNames.realloc( 1 ); + aServiceNames[ 0 ] = "ooo.vba.FileDialog"; + } + return aServiceNames; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/vba/vbafiledialog.hxx b/sc/source/ui/vba/vbafiledialog.hxx new file mode 100644 index 000000000000..202eefa020e0 --- /dev/null +++ b/sc/source/ui/vba/vbafiledialog.hxx @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#ifndef INCLUDED_SC_SOURCE_UI_VBA_VBAFILEDIALOG_HXX +#define INCLUDED_SC_SOURCE_UI_VBA_VBAFILEDIALOG_HXX + +#include <ooo/vba/excel/XFileDialog.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <vbahelper/vbahelperinterface.hxx> + +#include "vbafiledialogitems.hxx" + +typedef InheritedHelperInterfaceWeakImpl< ov::excel::XFileDialog > ScVbaFileDialog_BASE; + +class ScVbaFileDialog : public ScVbaFileDialog_BASE +{ +private: + sal_Int32 m_nType; + OUString m_sTitle; + OUString m_sInitialFileName; + css::uno::Reference< ov::excel::XFileDialogSelectedItems> m_xItems; +public: + ScVbaFileDialog( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const sal_Int32 nType); + + virtual css::uno::Any SAL_CALL getInitialFileName() override; + virtual void SAL_CALL setInitialFileName( const css::uno::Any& rName ) override; + virtual css::uno::Any SAL_CALL getTitle() override; + virtual void SAL_CALL setTitle( const css::uno::Any& rTitle ) override; + virtual css::uno::Reference< ov::excel::XFileDialogSelectedItems > SAL_CALL getSelectedItems() override; + + virtual sal_Int32 SAL_CALL Show() override; + + //XHelperInterface + virtual OUString getServiceImplName() override; + virtual css::uno::Sequence<OUString> getServiceNames() override; +}; + + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/vba/vbafiledialogitems.cxx b/sc/source/ui/vba/vbafiledialogitems.cxx new file mode 100644 index 000000000000..4e57a2e0d10a --- /dev/null +++ b/sc/source/ui/vba/vbafiledialogitems.cxx @@ -0,0 +1,119 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#include "vbafiledialogitems.hxx" +#include <com/sun/star/lang/XSingleComponentFactory.hpp> +#include <com/sun/star/container/XIndexContainer.hpp> + +using namespace ::com::sun::star; +using namespace ::ooo::vba; + +typedef std::vector< OUString > StringVector; + +class FileDialogItemEnumeration : public ::cppu::WeakImplHelper< container::XEnumeration > +{ + StringVector m_sItems; + StringVector::iterator mIt; +public: + explicit FileDialogItemEnumeration( const StringVector& rVector ) : m_sItems( rVector ), mIt( m_sItems.begin() ) {} + virtual sal_Bool SAL_CALL hasMoreElements() override + { + return ( mIt != m_sItems.end() ); + } + virtual uno::Any SAL_CALL nextElement() override + { + if( !hasMoreElements() ) + throw container::NoSuchElementException(); + OUString sPath = *mIt++; + return uno::makeAny( sPath ); + } +}; + +ScVbaFileDialogSelectedItems::ScVbaFileDialogSelectedItems( + const css::uno::Reference< ov::XHelperInterface >& xParent + ,const css::uno::Reference< css::uno::XComponentContext >& xContext + ,const StringVector& rItems) + : FileDialogSelectedItems_BASE( xParent, xContext, uno::Reference< container::XIndexAccess>() ) + , m_sItems(rItems) {} + + +// XEnumerationAccess +uno::Type SAL_CALL +ScVbaFileDialogSelectedItems::getElementType() +{ + return cppu::UnoType<OUString>::get(); +} + +uno::Reference< container::XEnumeration > +ScVbaFileDialogSelectedItems::createEnumeration() +{ + return uno::Reference< container::XEnumeration >( new FileDialogItemEnumeration( m_sItems ) ); +} + +uno::Any +ScVbaFileDialogSelectedItems::createCollectionObject( const uno::Any& aSource ) +{ + sal_Int32 nPosition = -1; + aSource >>= nPosition; + + OUString sPath = m_sItems[nPosition]; + return uno::makeAny( sPath ); +} + +// Methods +uno::Any SAL_CALL +ScVbaFileDialogSelectedItems::Item( const uno::Any& aIndex, const uno::Any& /*aIndex*/ ) +{ + sal_Int32 nPosition = -1; + aIndex >>= nPosition; + + --nPosition; // vba indexing starts with 1 + + if( nPosition < 0 || nPosition >= getCount() ) + { + throw uno::RuntimeException(); + } + + return createCollectionObject( uno::makeAny( nPosition ) ); +} + +sal_Int32 ScVbaFileDialogSelectedItems::getCount() +{ + return m_sItems.size(); +} + +// XHelperInterface +OUString +ScVbaFileDialogSelectedItems::getServiceImplName() +{ + return OUString("ScVbaFileDialogSelectedItems"); +} + +uno::Sequence<OUString> +ScVbaFileDialogSelectedItems::getServiceNames() +{ + static uno::Sequence< OUString > aServiceNames; + if ( aServiceNames.getLength() == 0 ) + { + aServiceNames.realloc( 1 ); + aServiceNames[ 0 ] = "ooo.vba.FileDialogSelectedItems"; + } + return aServiceNames; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/vba/vbafiledialogitems.hxx b/sc/source/ui/vba/vbafiledialogitems.hxx new file mode 100644 index 000000000000..3a789f886eb5 --- /dev/null +++ b/sc/source/ui/vba/vbafiledialogitems.hxx @@ -0,0 +1,42 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +#ifndef INCLUDED_SC_SOURCE_UI_VBA_VBAFILEDIALOGSELECTEDITEMS +#define INCLUDED_SC_SOURCE_UI_VBA_VBAFILEDIALOGSELECTEDITEMS + +#include <ooo/vba//excel/XFileDialogSelectedItems.hpp> +#include <vbahelper/vbahelperinterface.hxx> +#include <vbahelper/vbacollectionimpl.hxx> +#include <com/sun/star/container/XIndexAccess.hpp> + +typedef CollTestImplHelper< ov::excel::XFileDialogSelectedItems > FileDialogSelectedItems_BASE; + +class ScVbaFileDialogSelectedItems : public FileDialogSelectedItems_BASE +{ +protected: + const std::vector<OUString> m_sItems; +public: + ScVbaFileDialogSelectedItems( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const std::vector<OUString>& sItems); + + // XEnumerationAccess + virtual css::uno::Type SAL_CALL getElementType() override; + virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override; + virtual css::uno::Any createCollectionObject( const css::uno::Any& aSource ) override; + + // Methods + virtual css::uno::Any SAL_CALL Item( const css::uno::Any& Index, const css::uno::Any& /*Index2*/ ) override; + virtual sal_Int32 SAL_CALL getCount() override; + + // XHelperInterface + virtual OUString getServiceImplName() override; + virtual css::uno::Sequence<OUString> getServiceNames() override; +}; + +#endif // INCLUDED_SC_SOURCE_UI_VBA_VBAFILEDIALOGSELECTEDITEMS + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |