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 #include "vbasections.hxx" 28*cdf0e10cSrcweir #include "vbasection.hxx" 29*cdf0e10cSrcweir #include <com/sun/star/style/XStyleFamiliesSupplier.hpp> 30*cdf0e10cSrcweir #include <com/sun/star/style/XStyle.hpp> 31*cdf0e10cSrcweir #include <docsh.hxx> 32*cdf0e10cSrcweir #include <doc.hxx> 33*cdf0e10cSrcweir #include "wordvbahelper.hxx" 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir using namespace ::ooo::vba; 36*cdf0e10cSrcweir using namespace ::com::sun::star; 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1< container::XEnumeration > SectionEnumeration_BASE; 39*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper2< container::XIndexAccess, container::XEnumerationAccess > SectionCollectionHelper_Base; 40*cdf0e10cSrcweir typedef std::vector< uno::Reference< beans::XPropertySet > > XSectionVec; 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir class SectionEnumeration : public SectionEnumeration_BASE 43*cdf0e10cSrcweir { 44*cdf0e10cSrcweir XSectionVec mxSections; 45*cdf0e10cSrcweir XSectionVec::iterator mIt; 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir public: 48*cdf0e10cSrcweir SectionEnumeration( const XSectionVec& rVec ) : mxSections( rVec ), mIt( mxSections.begin() ) {} 49*cdf0e10cSrcweir virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException) 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir return ( mIt != mxSections.end() ); 52*cdf0e10cSrcweir } 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir if ( hasMoreElements() ) 57*cdf0e10cSrcweir return uno::makeAny( *mIt++ ); 58*cdf0e10cSrcweir throw container::NoSuchElementException(); 59*cdf0e10cSrcweir } 60*cdf0e10cSrcweir }; 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir // here I regard pagestyle as section 63*cdf0e10cSrcweir class SectionCollectionHelper : public SectionCollectionHelper_Base 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir private: 66*cdf0e10cSrcweir uno::Reference< XHelperInterface > mxParent; 67*cdf0e10cSrcweir uno::Reference< uno::XComponentContext > mxContext; 68*cdf0e10cSrcweir uno::Reference< frame::XModel > mxModel; 69*cdf0e10cSrcweir XSectionVec mxSections; 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir public: 72*cdf0e10cSrcweir SectionCollectionHelper( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel ) throw (uno::RuntimeException) : mxParent( xParent ), mxContext( xContext ), mxModel( xModel ) 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( mxModel, uno::UNO_QUERY_THROW ); 75*cdf0e10cSrcweir uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_QUERY_THROW ); 76*cdf0e10cSrcweir uno::Reference< container::XIndexAccess > xPageStyles( xSytleFamNames->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyles") ) ), uno::UNO_QUERY_THROW ); 77*cdf0e10cSrcweir sal_Int32 nCount = xPageStyles->getCount(); 78*cdf0e10cSrcweir for( sal_Int32 index = 0; index < nCount; ++index ) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir uno::Reference< style::XStyle > xStyle( xPageStyles->getByIndex( index ), uno::UNO_QUERY_THROW ); 81*cdf0e10cSrcweir // only the pagestyles in using are considered 82*cdf0e10cSrcweir if( xStyle->isInUse( ) ) 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW ); 85*cdf0e10cSrcweir mxSections.push_back( xPageProps ); 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir ~SectionCollectionHelper(){} 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir // XIndexAccess 93*cdf0e10cSrcweir virtual sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir return mxSections.size(); 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException) 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir if ( Index < 0 || Index >= getCount() ) 100*cdf0e10cSrcweir throw css::lang::IndexOutOfBoundsException(); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xPageProps( mxSections[ Index ], uno::UNO_QUERY_THROW ); 103*cdf0e10cSrcweir return uno::makeAny( uno::Reference< word::XSection >( new SwVbaSection( mxParent, mxContext, mxModel, xPageProps ) ) ); 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir return word::XSection::static_type(0); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir return sal_True; 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir // XEnumerationAccess 114*cdf0e10cSrcweir virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) throw (uno::RuntimeException) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir return new SectionEnumeration( mxSections ); 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir }; 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir class SectionsEnumWrapper : public EnumerationHelperImpl 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir uno::Reference< frame::XModel > mxModel; 123*cdf0e10cSrcweir public: 124*cdf0e10cSrcweir SectionsEnumWrapper( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< frame::XModel >& xModel ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), mxModel( xModel ){} 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xPageProps( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW ); 129*cdf0e10cSrcweir return uno::makeAny( uno::Reference< word::XSection > ( new SwVbaSection( m_xParent, m_xContext, mxModel, xPageProps ) ) ); 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir }; 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir SwVbaSections::SwVbaSections( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xModel ): SwVbaSections_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new SectionCollectionHelper( xParent, xContext, xModel ) ) ), mxModel( xModel ) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir uno::Any SAL_CALL 138*cdf0e10cSrcweir SwVbaSections::PageSetup( ) throw (uno::RuntimeException) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir if( m_xIndexAccess->getCount() ) 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir // check if the first section is our want 143*cdf0e10cSrcweir uno::Reference< word::XSection > xSection( m_xIndexAccess->getByIndex( 0 ), uno::UNO_QUERY_THROW ); 144*cdf0e10cSrcweir return xSection->PageSetup(); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("There is no section") ), uno::Reference< uno::XInterface >() ); 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir // XEnumerationAccess 150*cdf0e10cSrcweir uno::Type SAL_CALL 151*cdf0e10cSrcweir SwVbaSections::getElementType() throw (uno::RuntimeException) 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir return word::XSection::static_type(0); 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir uno::Reference< container::XEnumeration > SAL_CALL 157*cdf0e10cSrcweir SwVbaSections::createEnumeration() throw (uno::RuntimeException) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW ); 160*cdf0e10cSrcweir return new SectionsEnumWrapper( this, mxContext, xEnumAccess->createEnumeration(), mxModel ); 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir uno::Any 164*cdf0e10cSrcweir SwVbaSections::createCollectionObject( const css::uno::Any& aSource ) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir return aSource; 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir rtl::OUString& 170*cdf0e10cSrcweir SwVbaSections::getServiceImplName() 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaSections") ); 173*cdf0e10cSrcweir return sImplName; 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir css::uno::Sequence<rtl::OUString> 177*cdf0e10cSrcweir SwVbaSections::getServiceNames() 178*cdf0e10cSrcweir { 179*cdf0e10cSrcweir static uno::Sequence< rtl::OUString > sNames; 180*cdf0e10cSrcweir if ( sNames.getLength() == 0 ) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir sNames.realloc( 1 ); 183*cdf0e10cSrcweir sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Sections") ); 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir return sNames; 186*cdf0e10cSrcweir } 187