xref: /aoo42x/main/sw/source/ui/vba/vbasections.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include "vbasections.hxx"
28 #include "vbasection.hxx"
29 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
30 #include <com/sun/star/style/XStyle.hpp>
31 #include <docsh.hxx>
32 #include <doc.hxx>
33 #include "wordvbahelper.hxx"
34 
35 using namespace ::ooo::vba;
36 using namespace ::com::sun::star;
37 
38 typedef ::cppu::WeakImplHelper1< container::XEnumeration > SectionEnumeration_BASE;
39 typedef ::cppu::WeakImplHelper2< container::XIndexAccess, container::XEnumerationAccess > SectionCollectionHelper_Base;
40 typedef std::vector< uno::Reference< beans::XPropertySet > > XSectionVec;
41 
42 class SectionEnumeration : public SectionEnumeration_BASE
43 {
44     XSectionVec mxSections;
45     XSectionVec::iterator mIt;
46 
47 public:
48 	SectionEnumeration( const XSectionVec& rVec ) : mxSections( rVec ), mIt( mxSections.begin() ) {}
49 	virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
50 	{
51 		return ( mIt != mxSections.end() );
52 	}
53 
54 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
55 	{
56 		if ( hasMoreElements() )
57 			return uno::makeAny( *mIt++ );
58 		throw container::NoSuchElementException();
59 	}
60 };
61 
62 // here I regard pagestyle as section
63 class SectionCollectionHelper : public SectionCollectionHelper_Base
64 {
65 private:
66     uno::Reference< XHelperInterface > mxParent;
67     uno::Reference< uno::XComponentContext > mxContext;
68     uno::Reference< frame::XModel > mxModel;
69     XSectionVec mxSections;
70 
71 public:
72     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     {
74         uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( mxModel, uno::UNO_QUERY_THROW );
75         uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_QUERY_THROW );
76         uno::Reference< container::XIndexAccess > xPageStyles( xSytleFamNames->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyles") ) ), uno::UNO_QUERY_THROW );
77         sal_Int32 nCount = xPageStyles->getCount();
78         for( sal_Int32 index = 0; index < nCount; ++index )
79         {
80             uno::Reference< style::XStyle > xStyle( xPageStyles->getByIndex( index ), uno::UNO_QUERY_THROW );
81             // only the pagestyles in using are considered
82             if( xStyle->isInUse( ) )
83             {
84                 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
85                 mxSections.push_back( xPageProps );
86             }
87         }
88     }
89 
90     ~SectionCollectionHelper(){}
91 
92     // XIndexAccess
93     virtual sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
94     {
95         return mxSections.size();
96     }
97     virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
98     {
99         if ( Index < 0 || Index >= getCount() )
100             throw css::lang::IndexOutOfBoundsException();
101 
102         uno::Reference< beans::XPropertySet > xPageProps( mxSections[ Index ], uno::UNO_QUERY_THROW );
103         return uno::makeAny( uno::Reference< word::XSection >( new SwVbaSection( mxParent,  mxContext, mxModel, xPageProps ) ) );
104     }
105     virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException)
106     {
107         return word::XSection::static_type(0);
108     }
109     virtual sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException)
110     {
111         return sal_True;
112     }
113     // XEnumerationAccess
114     virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration(  ) throw (uno::RuntimeException)
115     {
116         return new SectionEnumeration( mxSections );
117     }
118 };
119 
120 class SectionsEnumWrapper : public EnumerationHelperImpl
121 {
122 	uno::Reference< frame::XModel > mxModel;
123 public:
124 	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 
126 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
127 	{
128 		uno::Reference< beans::XPropertySet > xPageProps( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
129 		return uno::makeAny( uno::Reference< word::XSection > ( new SwVbaSection( m_xParent, m_xContext, mxModel, xPageProps ) ) );
130 	}
131 };
132 
133 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 {
135 }
136 
137 uno::Any SAL_CALL
138 SwVbaSections::PageSetup( ) throw (uno::RuntimeException)
139 {
140     if( m_xIndexAccess->getCount() )
141     {
142         // check if the first section is our want
143         uno::Reference< word::XSection > xSection( m_xIndexAccess->getByIndex( 0 ), uno::UNO_QUERY_THROW );
144         return xSection->PageSetup();
145     }
146     throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("There is no section") ), uno::Reference< uno::XInterface >() );
147 }
148 
149 // XEnumerationAccess
150 uno::Type SAL_CALL
151 SwVbaSections::getElementType() throw (uno::RuntimeException)
152 {
153 	return word::XSection::static_type(0);
154 }
155 
156 uno::Reference< container::XEnumeration > SAL_CALL
157 SwVbaSections::createEnumeration() throw (uno::RuntimeException)
158 {
159     uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
160     return new SectionsEnumWrapper( this, mxContext, xEnumAccess->createEnumeration(), mxModel );
161 }
162 
163 uno::Any
164 SwVbaSections::createCollectionObject( const css::uno::Any& aSource )
165 {
166     return aSource;
167 }
168 
169 rtl::OUString&
170 SwVbaSections::getServiceImplName()
171 {
172 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaSections") );
173 	return sImplName;
174 }
175 
176 css::uno::Sequence<rtl::OUString>
177 SwVbaSections::getServiceNames()
178 {
179 	static uno::Sequence< rtl::OUString > sNames;
180 	if ( sNames.getLength() == 0 )
181 	{
182 		sNames.realloc( 1 );
183 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Sections") );
184 	}
185 	return sNames;
186 }
187