Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : #include "vbatables.hxx"
3 : : #include "vbatable.hxx"
4 : : #include "vbarange.hxx"
5 : : #include <com/sun/star/text/XTextTable.hpp>
6 : : #include <com/sun/star/text/XTextTablesSupplier.hpp>
7 : : #include <com/sun/star/text/XTextDocument.hpp>
8 : : #include <com/sun/star/lang/XServiceInfo.hpp>
9 : : #include <com/sun/star/text/XText.hpp>
10 : : #include <com/sun/star/table/XCellRange.hpp>
11 : : #include <comphelper/componentcontext.hxx>
12 : :
13 : : using namespace ::ooo::vba;
14 : : using namespace css;
15 : :
16 : 0 : uno::Reference< container::XIndexAccess > lcl_getTables( const uno::Reference< frame::XModel >& xDoc )
17 : : {
18 : 0 : uno::Reference< container::XIndexAccess > xTables;
19 [ # # ]: 0 : uno::Reference< text::XTextTablesSupplier > xSupp( xDoc, uno::UNO_QUERY );
20 [ # # ]: 0 : if ( xSupp.is() )
21 [ # # ][ # # ]: 0 : xTables.set( xSupp->getTextTables(), uno::UNO_QUERY_THROW );
[ # # ]
22 : 0 : return xTables;
23 : : }
24 : :
25 : 0 : uno::Any lcl_createTable( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xDocument, const uno::Any& aSource )
26 : : {
27 [ # # ]: 0 : uno::Reference< text::XTextTable > xTextTable( aSource, uno::UNO_QUERY_THROW );
28 [ # # ]: 0 : uno::Reference< text::XTextDocument > xTextDocument( xDocument, uno::UNO_QUERY_THROW );
29 [ # # ][ # # ]: 0 : uno::Reference< word::XTable > xTable( new SwVbaTable( xParent, xContext, xTextDocument, xTextTable ) );
[ # # ]
30 [ # # ]: 0 : return uno::makeAny( xTable );
31 : : }
32 : :
33 : 0 : sal_Bool lcl_isInHeaderFooter( const uno::Reference< text::XTextTable >& xTable )
34 : : {
35 [ # # ]: 0 : uno::Reference< text::XTextContent > xTextContent( xTable, uno::UNO_QUERY_THROW );
36 [ # # ][ # # ]: 0 : uno::Reference< text::XText > xText = xTextContent->getAnchor()->getText();
[ # # ][ # # ]
37 [ # # ]: 0 : uno::Reference< lang::XServiceInfo > xServiceInfo( xText, uno::UNO_QUERY_THROW );
38 [ # # ][ # # ]: 0 : rtl::OUString aImplName = xServiceInfo->getImplementationName();
39 [ # # ]: 0 : if ( aImplName == "SwXHeadFootText" )
40 : 0 : return sal_True;
41 : 0 : return sal_False;
42 : : }
43 : :
44 : : typedef ::cppu::WeakImplHelper1< css::container::XEnumeration > EnumBase;
45 : : typedef ::cppu::WeakImplHelper2< container::XIndexAccess, container::XNameAccess > TableCollectionHelper_Base;
46 : : typedef std::vector< uno::Reference< text::XTextTable > > XTextTableVec;
47 : :
48 [ # # ]: 0 : class TableCollectionHelper : public TableCollectionHelper_Base
49 : : {
50 : : XTextTableVec mxTables;
51 : : XTextTableVec::iterator cachePos;
52 : :
53 : : public:
54 : 0 : TableCollectionHelper( const uno::Reference< frame::XModel >& xDocument )
55 [ # # ]: 0 : {
56 : : // only count the tables in the body text, not in the header/footer
57 [ # # ]: 0 : uno::Reference< container::XIndexAccess > xTables = lcl_getTables( xDocument );
58 [ # # ][ # # ]: 0 : sal_Int32 nCount = xTables->getCount();
59 [ # # ]: 0 : for( sal_Int32 i = 0; i < nCount; i++ )
60 : : {
61 [ # # ][ # # ]: 0 : uno::Reference< text::XTextTable > xTable( xTables->getByIndex( i ) , uno::UNO_QUERY_THROW );
[ # # ]
62 [ # # ][ # # ]: 0 : if( !lcl_isInHeaderFooter( xTable ) )
63 [ # # ]: 0 : mxTables.push_back( xTable );
64 : 0 : }
65 : 0 : cachePos = mxTables.begin();
66 : 0 : }
67 : : // XIndexAccess
68 : 0 : virtual sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException)
69 : : {
70 : 0 : return mxTables.size();
71 : : }
72 : 0 : virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
73 : : {
74 [ # # ][ # # ]: 0 : if ( Index < 0 || Index >= getCount() )
[ # # ][ # # ]
75 [ # # ]: 0 : throw lang::IndexOutOfBoundsException();
76 [ # # ]: 0 : uno::Reference< text::XTextTable > xTable( mxTables[ Index ], uno::UNO_QUERY_THROW );
77 [ # # ]: 0 : return uno::makeAny( xTable );
78 : : }
79 : : // XElementAccess
80 : 0 : virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException) { return text::XTextTable::static_type(0); }
81 : 0 : virtual ::sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException) { return getCount() > 0 ; }
82 : : // XNameAcess
83 : 0 : virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
84 : : {
85 [ # # ][ # # ]: 0 : if ( !hasByName(aName) )
86 [ # # ]: 0 : throw container::NoSuchElementException();
87 [ # # ]: 0 : uno::Reference< text::XTextTable > xTable( *cachePos, uno::UNO_QUERY_THROW );
88 [ # # ]: 0 : return uno::makeAny( xTable );
89 : : }
90 : 0 : virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException)
91 : : {
92 [ # # ]: 0 : uno::Sequence< rtl::OUString > sNames( mxTables.size() );
93 [ # # ]: 0 : rtl::OUString* pString = sNames.getArray();
94 : 0 : XTextTableVec::iterator it = mxTables.begin();
95 : 0 : XTextTableVec::iterator it_end = mxTables.end();
96 [ # # ][ # # ]: 0 : for ( ; it != it_end; ++it, ++pString )
97 : : {
98 [ # # ]: 0 : uno::Reference< container::XNamed > xName( *it, uno::UNO_QUERY_THROW );
99 [ # # ][ # # ]: 0 : *pString = xName->getName();
100 : 0 : }
101 : 0 : return sNames;
102 : : }
103 : 0 : virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
104 : : {
105 : 0 : cachePos = mxTables.begin();
106 : 0 : XTextTableVec::iterator it_end = mxTables.end();
107 [ # # ][ # # ]: 0 : for ( ; cachePos != it_end; ++cachePos )
108 : : {
109 [ # # ]: 0 : uno::Reference< container::XNamed > xName( *cachePos, uno::UNO_QUERY_THROW );
110 [ # # ][ # # ]: 0 : if ( aName.equalsIgnoreAsciiCase( xName->getName() ) )
[ # # ]
111 : : break;
112 [ # # ]: 0 : }
113 [ # # ]: 0 : return ( cachePos != it_end );
114 : : }
115 : : };
116 : :
117 [ # # ]: 0 : class TableEnumerationImpl : public EnumBase
118 : : {
119 : : uno::Reference< XHelperInterface > mxParent;
120 : : uno::Reference< uno::XComponentContext > mxContext;
121 : : uno::Reference< frame::XModel > mxDocument;
122 : : uno::Reference< container::XIndexAccess > mxIndexAccess;
123 : : sal_Int32 mnCurIndex;
124 : : public:
125 : 0 : TableEnumerationImpl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xDocument, const uno::Reference< container::XIndexAccess >& xIndexAccess ) : mxParent( xParent ), mxContext( xContext ), mxDocument( xDocument ), mxIndexAccess( xIndexAccess ), mnCurIndex(0)
126 : : {
127 : 0 : }
128 : 0 : virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException)
129 : : {
130 : 0 : return ( mnCurIndex < mxIndexAccess->getCount() );
131 : : }
132 : 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
133 : : {
134 [ # # ]: 0 : if ( !hasMoreElements() )
135 [ # # ]: 0 : throw container::NoSuchElementException();
136 [ # # ]: 0 : return lcl_createTable( mxParent, mxContext, mxDocument, mxIndexAccess->getByIndex( mnCurIndex++ ) );
137 : : }
138 : :
139 : : };
140 : :
141 [ # # ][ # # ]: 0 : SwVbaTables::SwVbaTables( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xDocument ) : SwVbaTables_BASE( xParent, xContext , uno::Reference< container::XIndexAccess >( new TableCollectionHelper( xDocument ) ) ), mxDocument( xDocument )
[ # # ]
142 : : {
143 : 0 : }
144 : :
145 : :
146 : : uno::Reference< word::XTable > SAL_CALL
147 : 0 : SwVbaTables::Add( const uno::Reference< word::XRange >& Range, const uno::Any& NumRows, const uno::Any& NumColumns, const uno::Any& /*DefaultTableBehavior*/, const uno::Any& /*AutoFitBehavior*/ ) throw (script::BasicErrorException, uno::RuntimeException)
148 : : {
149 : 0 : sal_Int32 nCols = 0;
150 : 0 : sal_Int32 nRows = 0;
151 [ # # ][ # # ]: 0 : SwVbaRange* pVbaRange = dynamic_cast< SwVbaRange* >( Range.get() );
152 : : // Preconditions
153 [ # # ][ # # ]: 0 : if ( !( pVbaRange && ( NumRows >>= nRows ) && ( NumColumns >>= nCols ) ) )
[ # # ][ # # ]
154 [ # # ]: 0 : throw uno::RuntimeException(); // #FIXME better exception??
155 [ # # ][ # # ]: 0 : if ( nCols <= 0 || nRows <= 0 )
156 [ # # ]: 0 : throw uno::RuntimeException(); // #FIXME better exception??
157 : :
158 [ # # ][ # # ]: 0 : uno::Reference< frame::XModel > xModel( pVbaRange->getDocument(), uno::UNO_QUERY_THROW );
159 [ # # ]: 0 : uno::Reference< lang::XMultiServiceFactory > xMsf( xModel, uno::UNO_QUERY_THROW );
160 [ # # ]: 0 : uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
161 : :
162 : 0 : uno::Reference< text::XTextTable > xTable;
163 [ # # ][ # # ]: 0 : xTable.set( xMsf->createInstance( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.TextTable")) ), uno::UNO_QUERY_THROW );
[ # # ][ # # ]
164 : :
165 [ # # ][ # # ]: 0 : xTable->initialize( nRows, nCols );
166 [ # # ][ # # ]: 0 : uno::Reference< text::XText > xText = xTextRange->getText();
167 [ # # ]: 0 : uno::Reference< text::XTextContent > xContext( xTable, uno::UNO_QUERY_THROW );
168 : :
169 [ # # ][ # # ]: 0 : xText->insertTextContent( xTextRange, xContext, true );
170 : :
171 : : // move the current cursor to the first table cell
172 [ # # ]: 0 : uno::Reference< table::XCellRange > xCellRange( xTable, uno::UNO_QUERY_THROW );
173 [ # # ][ # # ]: 0 : uno::Reference< text::XText> xFirstCellText( xCellRange->getCellByPosition(0, 0), uno::UNO_QUERY_THROW );
[ # # ]
174 [ # # ][ # # ]: 0 : word::getXTextViewCursor( mxDocument )->gotoRange( xFirstCellText->getStart(), sal_False );
[ # # ][ # # ]
[ # # ]
175 : :
176 [ # # ][ # # ]: 0 : uno::Reference< word::XTable > xVBATable( new SwVbaTable( mxParent, mxContext, pVbaRange->getDocument(), xTable ) );
[ # # ][ # # ]
[ # # ]
177 : 0 : return xVBATable;
178 : : }
179 : :
180 : : uno::Reference< container::XEnumeration > SAL_CALL
181 : 0 : SwVbaTables::createEnumeration() throw (uno::RuntimeException)
182 : : {
183 [ # # ][ # # ]: 0 : return new TableEnumerationImpl( mxParent, mxContext, mxDocument, m_xIndexAccess );
[ # # ]
184 : : }
185 : :
186 : : // ScVbaCollectionBaseImpl
187 : : uno::Any
188 : 0 : SwVbaTables::createCollectionObject( const uno::Any& aSource )
189 : : {
190 [ # # ]: 0 : return lcl_createTable( mxParent, mxContext, mxDocument, aSource );
191 : : }
192 : :
193 : : // XHelperInterface
194 : : rtl::OUString
195 : 0 : SwVbaTables::getServiceImplName()
196 : : {
197 : 0 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SwVbaTables"));
198 : : }
199 : :
200 : : // XEnumerationAccess
201 : : uno::Type SAL_CALL
202 : 0 : SwVbaTables::getElementType() throw (uno::RuntimeException)
203 : : {
204 : 0 : return word::XTable::static_type(0);
205 : : }
206 : :
207 : : uno::Sequence<rtl::OUString>
208 : 0 : SwVbaTables::getServiceNames()
209 : : {
210 [ # # ][ # # ]: 0 : static uno::Sequence< rtl::OUString > aServiceNames;
[ # # ][ # # ]
211 [ # # ]: 0 : if ( aServiceNames.getLength() == 0 )
212 : : {
213 : 0 : aServiceNames.realloc( 1 );
214 [ # # ]: 0 : aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Tables" ) );
215 : : }
216 : 0 : return aServiceNames;
217 : : }
218 : :
219 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|