24 #include <com/sun/star/text/XTextTable.hpp>
25 #include <com/sun/star/text/XTextTablesSupplier.hpp>
26 #include <com/sun/star/text/XTextDocument.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/text/XText.hpp>
30 #include <com/sun/star/table/XCellRange.hpp>
36 static uno::Reference< container::XIndexAccess >
lcl_getTables(
const uno::Reference< frame::XModel >& xDoc )
38 uno::Reference< container::XIndexAccess > xTables;
39 uno::Reference< text::XTextTablesSupplier > xSupp( xDoc, uno::UNO_QUERY );
41 xTables.set( xSupp->getTextTables(), uno::UNO_QUERY_THROW );
45 static 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 )
47 uno::Reference< text::XTextTable > xTextTable( aSource, uno::UNO_QUERY_THROW );
48 uno::Reference< text::XTextDocument > xTextDocument( xDocument, uno::UNO_QUERY_THROW );
49 uno::Reference< word::XTable > xTable(
new SwVbaTable( xParent, xContext, xTextDocument, xTextTable ) );
50 return uno::makeAny( xTable );
55 uno::Reference< text::XTextContent > xTextContent( xTable, uno::UNO_QUERY_THROW );
56 uno::Reference< text::XText > xText = xTextContent->getAnchor()->getText();
57 uno::Reference< lang::XServiceInfo > xServiceInfo( xText, uno::UNO_QUERY_THROW );
58 OUString aImplName = xServiceInfo->getImplementationName();
59 return aImplName ==
"SwXHeadFootText";
62 typedef std::vector< uno::Reference< text::XTextTable > >
XTextTableVec;
66 class TableCollectionHelper :
public ::cppu::WeakImplHelper< container::XIndexAccess,
67 container::XNameAccess >
70 XTextTableVec::iterator cachePos;
73 explicit TableCollectionHelper(
const uno::Reference< frame::XModel >& xDocument )
76 uno::Reference< container::XIndexAccess > xTables =
lcl_getTables( xDocument );
77 sal_Int32
nCount = xTables->getCount();
78 for( sal_Int32
i = 0;
i < nCount;
i++ )
80 uno::Reference< text::XTextTable > xTable( xTables->getByIndex(
i ) , uno::UNO_QUERY_THROW );
82 mxTables.push_back( xTable );
84 cachePos = mxTables.begin();
87 virtual sal_Int32 SAL_CALL getCount( )
override
89 return mxTables.size();
91 virtual uno::Any SAL_CALL getByIndex( sal_Int32
Index )
override
94 throw lang::IndexOutOfBoundsException();
95 uno::Reference< text::XTextTable > xTable( mxTables[ Index ], uno::UNO_SET_THROW );
96 return uno::makeAny( xTable );
100 virtual sal_Bool SAL_CALL hasElements( )
override {
return getCount() > 0 ; }
102 virtual uno::Any SAL_CALL getByName(
const OUString&
aName )
override
104 if ( !hasByName(aName) )
105 throw container::NoSuchElementException();
106 uno::Reference< text::XTextTable > xTable( *cachePos, uno::UNO_SET_THROW );
107 return uno::makeAny( xTable );
109 virtual uno::Sequence< OUString > SAL_CALL getElementNames( )
override
111 uno::Sequence< OUString > sNames( mxTables.size() );
112 OUString* pString = sNames.getArray();
113 for (
const auto& rxTable : mxTables )
115 uno::Reference< container::XNamed > xName( rxTable, uno::UNO_QUERY_THROW );
116 *pString = xName->getName();
121 virtual sal_Bool SAL_CALL hasByName(
const OUString& aName )
override
123 cachePos = mxTables.begin();
124 XTextTableVec::iterator it_end = mxTables.end();
125 for ( ; cachePos != it_end; ++cachePos )
127 uno::Reference< container::XNamed > xName( *cachePos, uno::UNO_QUERY_THROW );
128 if ( aName.equalsIgnoreAsciiCase( xName->getName() ) )
131 return ( cachePos != it_end );
135 class TableEnumerationImpl :
public ::cppu::WeakImplHelper< css::container::XEnumeration >
137 uno::Reference< XHelperInterface >
mxParent;
138 uno::Reference< uno::XComponentContext >
mxContext;
139 uno::Reference< frame::XModel > mxDocument;
140 uno::Reference< container::XIndexAccess > mxIndexAccess;
141 sal_Int32 mnCurIndex;
143 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)
146 virtual sal_Bool SAL_CALL hasMoreElements( )
override
148 return ( mnCurIndex < mxIndexAccess->getCount() );
150 virtual uno::Any SAL_CALL nextElement( )
override
152 if ( !hasMoreElements() )
153 throw container::NoSuchElementException();
154 return lcl_createTable( mxParent, mxContext, mxDocument, mxIndexAccess->getByIndex( mnCurIndex++ ) );
161 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 )
165 uno::Reference< word::XTable > SAL_CALL
172 if ( !( pVbaRange && ( NumRows >>= nRows ) && ( NumColumns >>= nCols ) ) )
173 throw uno::RuntimeException();
174 if ( nCols <= 0 || nRows <= 0 )
175 throw uno::RuntimeException();
177 uno::Reference< frame::XModel >
xModel( pVbaRange->
getDocument(), uno::UNO_QUERY_THROW );
178 uno::Reference< lang::XMultiServiceFactory > xMsf(
xModel, uno::UNO_QUERY_THROW );
179 uno::Reference< text::XTextRange > xTextRange = pVbaRange->
getXTextRange();
181 uno::Reference< text::XTextTable > xTable;
182 xTable.set( xMsf->createInstance(
"com.sun.star.text.TextTable"), uno::UNO_QUERY_THROW );
184 xTable->initialize( nRows, nCols );
185 uno::Reference< text::XText > xText = xTextRange->getText();
186 uno::Reference< text::XTextContent > xContext( xTable, uno::UNO_QUERY_THROW );
188 xText->insertTextContent( xTextRange, xContext,
true );
191 uno::Reference< table::XCellRange > xCellRange( xTable, uno::UNO_QUERY_THROW );
192 uno::Reference< text::XText> xFirstCellText( xCellRange->getCellByPosition(0, 0), uno::UNO_QUERY_THROW );
199 uno::Reference< container::XEnumeration > SAL_CALL
216 return "SwVbaTables";
226 uno::Sequence<OUString>
231 "ooo.vba.word.Tables"
static uno::Reference< container::XIndexAccess > lcl_getTables(const uno::Reference< frame::XModel > &xDoc)
virtual css::uno::Sequence< OUString > getServiceNames() override
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
WeakReference< XInterface > mxParent
Sequence< OUString > aServiceNames
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
css::uno::Reference< css::frame::XModel > mxDocument
virtual css::uno::Reference< css::text::XTextRange > SAL_CALL getXTextRange() override
uno::Reference< text::XTextViewCursor > getXTextViewCursor(const uno::Reference< frame::XModel > &xModel)
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
static bool lcl_isInHeaderFooter(const uno::Reference< text::XTextTable > &xTable)
static 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)
css::uno::WeakReference< ov::XHelperInterface > mxParent
css::uno::Type const & get()
std::vector< uno::Reference< text::XTextTable > > XTextTableVec
virtual css::uno::Type SAL_CALL getElementType() override
css::uno::Reference< css::uno::XComponentContext > mxContext
const css::uno::Reference< css::text::XTextDocument > & getDocument() const
SwVbaTables(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::frame::XModel > &xDocument)
virtual OUString getServiceImplName() override
virtual css::uno::Reference< ov::word::XTable > SAL_CALL Add(const css::uno::Reference< ::ooo::vba::word::XRange > &Range, const css::uno::Any &NumRows, const css::uno::Any &NumColumns, const css::uno::Any &DefaultTableBehavior, const css::uno::Any &AutoFitBehavior) override
Reference< XModel > xModel
Reference< XComponentContext > mxContext