update dev300-m58
[ooovba.git] / sw / source / ui / vba / vbasections.cxx
blob8fca880bab9730d11d7c485bb5d93e0a7a282424
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile:
10 * $Revision:
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbasections.hxx"
31 #include "vbasection.hxx"
32 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
33 #include <com/sun/star/style/XStyle.hpp>
34 #include <docsh.hxx>
35 #include <doc.hxx>
36 #include "wordvbahelper.hxx"
38 using namespace ::ooo::vba;
39 using namespace ::com::sun::star;
41 typedef ::cppu::WeakImplHelper1< container::XEnumeration > SectionEnumeration_BASE;
42 typedef ::cppu::WeakImplHelper2< container::XIndexAccess, container::XEnumerationAccess > SectionCollectionHelper_Base;
43 typedef std::vector< uno::Reference< beans::XPropertySet > > XSectionVec;
45 class SectionEnumeration : public SectionEnumeration_BASE
47 XSectionVec mxSections;
48 XSectionVec::iterator mIt;
50 public:
51 SectionEnumeration( const XSectionVec& rVec ) : mxSections( rVec ), mIt( mxSections.begin() ) {}
52 virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException)
54 return ( mIt != mxSections.end() );
57 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
59 if ( hasMoreElements() )
60 return uno::makeAny( *mIt++ );
61 throw container::NoSuchElementException();
65 // here I regard pagestyle as section
66 class SectionCollectionHelper : public SectionCollectionHelper_Base
68 private:
69 uno::Reference< XHelperInterface > mxParent;
70 uno::Reference< uno::XComponentContext > mxContext;
71 uno::Reference< frame::XModel > mxModel;
72 XSectionVec mxSections;
74 public:
75 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 )
77 uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( mxModel, uno::UNO_QUERY_THROW );
78 uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_QUERY_THROW );
79 uno::Reference< container::XIndexAccess > xPageStyles( xSytleFamNames->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyles") ) ), uno::UNO_QUERY_THROW );
80 sal_Int32 nCount = xPageStyles->getCount();
81 for( sal_Int32 index = 0; index < nCount; ++index )
83 uno::Reference< style::XStyle > xStyle( xPageStyles->getByIndex( index ), uno::UNO_QUERY_THROW );
84 // only the pagestyles in using are considered
85 if( xStyle->isInUse( ) )
87 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
88 mxSections.push_back( xPageProps );
93 ~SectionCollectionHelper(){}
95 // XIndexAccess
96 virtual sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException)
98 return mxSections.size();
100 virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
102 if ( Index < 0 || Index >= getCount() )
103 throw css::lang::IndexOutOfBoundsException();
105 uno::Reference< beans::XPropertySet > xPageProps( mxSections[ Index ], uno::UNO_QUERY_THROW );
106 return uno::makeAny( uno::Reference< word::XSection >( new SwVbaSection( mxParent, mxContext, mxModel, xPageProps ) ) );
108 virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException)
110 return word::XSection::static_type(0);
112 virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException)
114 return sal_True;
116 // XEnumerationAccess
117 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) throw (uno::RuntimeException)
119 return new SectionEnumeration( mxSections );
123 class SectionsEnumWrapper : public EnumerationHelperImpl
125 uno::Reference< frame::XModel > mxModel;
126 uno::WeakReference< XHelperInterface > mxParent;
127 public:
128 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( xContext, xEnumeration ), mxModel( xModel ), mxParent( xParent ) {}
130 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
132 uno::Reference< beans::XPropertySet > xPageProps( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
133 return uno::makeAny( uno::Reference< word::XSection > ( new SwVbaSection( mxParent, m_xContext, mxModel, xPageProps ) ) );
137 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 )
141 uno::Any SAL_CALL
142 SwVbaSections::PageSetup( ) throw (uno::RuntimeException)
144 if( m_xIndexAccess->getCount() )
146 // check if the first section is our want
147 uno::Reference< word::XSection > xSection( m_xIndexAccess->getByIndex( 0 ), uno::UNO_QUERY_THROW );
148 return xSection->PageSetup();
150 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("There is no section") ), uno::Reference< uno::XInterface >() );
153 // XEnumerationAccess
154 uno::Type SAL_CALL
155 SwVbaSections::getElementType() throw (uno::RuntimeException)
157 return word::XSection::static_type(0);
160 uno::Reference< container::XEnumeration > SAL_CALL
161 SwVbaSections::createEnumeration() throw (uno::RuntimeException)
163 uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
164 return new SectionsEnumWrapper( this, mxContext, xEnumAccess->createEnumeration(), mxModel );
167 uno::Any
168 SwVbaSections::createCollectionObject( const css::uno::Any& aSource )
170 return aSource;
173 rtl::OUString&
174 SwVbaSections::getServiceImplName()
176 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaSections") );
177 return sImplName;
180 css::uno::Sequence<rtl::OUString>
181 SwVbaSections::getServiceNames()
183 static uno::Sequence< rtl::OUString > sNames;
184 if ( sNames.getLength() == 0 )
186 sNames.realloc( 1 );
187 sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Sections") );
189 return sNames;