xref: /aoo4110/main/sw/source/ui/vba/vbasections.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "vbasections.hxx"
24 #include "vbasection.hxx"
25 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
26 #include <com/sun/star/style/XStyle.hpp>
27 #include <docsh.hxx>
28 #include <doc.hxx>
29 #include "wordvbahelper.hxx"
30 
31 using namespace ::ooo::vba;
32 using namespace ::com::sun::star;
33 
34 typedef ::cppu::WeakImplHelper1< container::XEnumeration > SectionEnumeration_BASE;
35 typedef ::cppu::WeakImplHelper2< container::XIndexAccess, container::XEnumerationAccess > SectionCollectionHelper_Base;
36 typedef std::vector< uno::Reference< beans::XPropertySet > > XSectionVec;
37 
38 class SectionEnumeration : public SectionEnumeration_BASE
39 {
40     XSectionVec mxSections;
41     XSectionVec::iterator mIt;
42 
43 public:
SectionEnumeration(const XSectionVec & rVec)44 	SectionEnumeration( const XSectionVec& rVec ) : mxSections( rVec ), mIt( mxSections.begin() ) {}
hasMoreElements()45 	virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
46 	{
47 		return ( mIt != mxSections.end() );
48 	}
49 
nextElement()50 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
51 	{
52 		if ( hasMoreElements() )
53 			return uno::makeAny( *mIt++ );
54 		throw container::NoSuchElementException();
55 	}
56 };
57 
58 // here I regard pagestyle as section
59 class SectionCollectionHelper : public SectionCollectionHelper_Base
60 {
61 private:
62     uno::Reference< XHelperInterface > mxParent;
63     uno::Reference< uno::XComponentContext > mxContext;
64     uno::Reference< frame::XModel > mxModel;
65     XSectionVec mxSections;
66 
67 public:
SectionCollectionHelper(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel)68     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 )
69     {
70         uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( mxModel, uno::UNO_QUERY_THROW );
71         uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_QUERY_THROW );
72         uno::Reference< container::XIndexAccess > xPageStyles( xSytleFamNames->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyles") ) ), uno::UNO_QUERY_THROW );
73         sal_Int32 nCount = xPageStyles->getCount();
74         for( sal_Int32 index = 0; index < nCount; ++index )
75         {
76             uno::Reference< style::XStyle > xStyle( xPageStyles->getByIndex( index ), uno::UNO_QUERY_THROW );
77             // only the pagestyles in using are considered
78             if( xStyle->isInUse( ) )
79             {
80                 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
81                 mxSections.push_back( xPageProps );
82             }
83         }
84     }
85 
~SectionCollectionHelper()86     ~SectionCollectionHelper(){}
87 
88     // XIndexAccess
getCount()89     virtual sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
90     {
91         return mxSections.size();
92     }
getByIndex(sal_Int32 Index)93     virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
94     {
95         if ( Index < 0 || Index >= getCount() )
96             throw css::lang::IndexOutOfBoundsException();
97 
98         uno::Reference< beans::XPropertySet > xPageProps( mxSections[ Index ], uno::UNO_QUERY_THROW );
99         return uno::makeAny( uno::Reference< word::XSection >( new SwVbaSection( mxParent,  mxContext, mxModel, xPageProps ) ) );
100     }
getElementType()101     virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException)
102     {
103         return word::XSection::static_type(0);
104     }
hasElements()105     virtual sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException)
106     {
107         return sal_True;
108     }
109     // XEnumerationAccess
createEnumeration()110     virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration(  ) throw (uno::RuntimeException)
111     {
112         return new SectionEnumeration( mxSections );
113     }
114 };
115 
116 class SectionsEnumWrapper : public EnumerationHelperImpl
117 {
118 	uno::Reference< frame::XModel > mxModel;
119 public:
SectionsEnumWrapper(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XEnumeration> & xEnumeration,const uno::Reference<frame::XModel> & xModel)120 	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 ){}
121 
nextElement()122 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
123 	{
124 		uno::Reference< beans::XPropertySet > xPageProps( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
125 		return uno::makeAny( uno::Reference< word::XSection > ( new SwVbaSection( m_xParent, m_xContext, mxModel, xPageProps ) ) );
126 	}
127 };
128 
SwVbaSections(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel)129 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 )
130 {
131 }
132 
133 uno::Any SAL_CALL
PageSetup()134 SwVbaSections::PageSetup( ) throw (uno::RuntimeException)
135 {
136     if( m_xIndexAccess->getCount() )
137     {
138         // check if the first section is our want
139         uno::Reference< word::XSection > xSection( m_xIndexAccess->getByIndex( 0 ), uno::UNO_QUERY_THROW );
140         return xSection->PageSetup();
141     }
142     throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("There is no section") ), uno::Reference< uno::XInterface >() );
143 }
144 
145 // XEnumerationAccess
146 uno::Type SAL_CALL
getElementType()147 SwVbaSections::getElementType() throw (uno::RuntimeException)
148 {
149 	return word::XSection::static_type(0);
150 }
151 
152 uno::Reference< container::XEnumeration > SAL_CALL
createEnumeration()153 SwVbaSections::createEnumeration() throw (uno::RuntimeException)
154 {
155     uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
156     return new SectionsEnumWrapper( this, mxContext, xEnumAccess->createEnumeration(), mxModel );
157 }
158 
159 uno::Any
createCollectionObject(const css::uno::Any & aSource)160 SwVbaSections::createCollectionObject( const css::uno::Any& aSource )
161 {
162     return aSource;
163 }
164 
165 rtl::OUString&
getServiceImplName()166 SwVbaSections::getServiceImplName()
167 {
168 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaSections") );
169 	return sImplName;
170 }
171 
172 css::uno::Sequence<rtl::OUString>
getServiceNames()173 SwVbaSections::getServiceNames()
174 {
175 	static uno::Sequence< rtl::OUString > sNames;
176 	if ( sNames.getLength() == 0 )
177 	{
178 		sNames.realloc( 1 );
179 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Sections") );
180 	}
181 	return sNames;
182 }
183