Update ooo320-m1
[ooovba.git] / sw / source / ui / vba / vbatables.cxx
blob53178d1c791dc98076b3e0710ab7cc035798be19
1 #include "vbatables.hxx"
2 #include "vbatable.hxx"
3 #include "vbarange.hxx"
4 #include <com/sun/star/text/XTextTable.hpp>
5 #include <com/sun/star/text/XTextTablesSupplier.hpp>
6 #include <com/sun/star/text/XTextDocument.hpp>
7 #include <comphelper/componentcontext.hxx>
9 using namespace ::ooo::vba;
10 using namespace css;
12 uno::Reference< container::XIndexAccess > lcl_getTables( const uno::Reference< frame::XModel >& xDoc )
14 uno::Reference< container::XIndexAccess > xTables;
15 uno::Reference< text::XTextTablesSupplier > xSupp( xDoc, uno::UNO_QUERY );
16 if ( xSupp.is() )
17 xTables.set( xSupp->getTextTables(), uno::UNO_QUERY_THROW );
18 return xTables;
21 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 )
23 uno::Reference< text::XTextTable > xTextTable( aSource, uno::UNO_QUERY_THROW );
24 uno::Reference< text::XTextDocument > xTextDocument( xDocument, uno::UNO_QUERY_THROW );
25 uno::Reference< word::XTable > xTable( new SwVbaTable( xParent, xContext, xTextDocument, xTextTable ) );
26 return uno::makeAny( xTable );
29 typedef ::cppu::WeakImplHelper1< css::container::XEnumeration > EnumBase;
31 class TableEnumerationImpl : public EnumBase
33 uno::Reference< XHelperInterface > mxParent;
34 uno::Reference< uno::XComponentContext > mxContext;
35 uno::Reference< frame::XModel > mxDocument;
36 uno::Reference< container::XIndexAccess > mxIndexAccess;
37 sal_Int32 mnCurIndex;
38 public:
39 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)
42 virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException)
44 return ( mnCurIndex < mxIndexAccess->getCount() );
46 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
48 if ( !hasMoreElements() )
49 throw container::NoSuchElementException();
50 return lcl_createTable( mxParent, mxContext, mxDocument, mxIndexAccess->getByIndex( mnCurIndex++ ) );
55 SwVbaTables::SwVbaTables( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xDocument ) : SwVbaTables_BASE( xParent, xContext , lcl_getTables( xDocument ) ), mxDocument( xDocument )
60 uno::Reference< word::XTable > SAL_CALL
61 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)
63 sal_Int32 nCols = 0;
64 sal_Int32 nRows = 0;
65 SwVbaRange* pVbaRange = dynamic_cast< SwVbaRange* >( Range.get() );
66 // Preconditions
67 if ( !( pVbaRange && ( NumRows >>= nRows ) && ( NumColumns >>= nCols ) ) )
68 throw uno::RuntimeException(); // #FIXME better exception??
69 if ( nCols <= 0 || nRows <= 0 )
70 throw uno::RuntimeException(); // #FIXME better exception??
72 uno::Reference< frame::XModel > xModel( pVbaRange->getDocument(), uno::UNO_QUERY_THROW );
73 uno::Reference< lang::XMultiServiceFactory > xMsf( xModel, uno::UNO_QUERY_THROW );
74 uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
76 uno::Reference< text::XTextTable > xTable;
77 xTable.set( xMsf->createInstance( rtl::OUString::createFromAscii("com.sun.star.text.TextTable") ), uno::UNO_QUERY_THROW );
79 comphelper::ComponentContext aCtx( xMsf );
80 if ( !aCtx.createComponent( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.TextTable") ), xTable ) );
81 throw uno::RuntimeException(); // #FIXME better exception??
83 xTable->initialize( nRows, nCols );
84 uno::Reference< text::XText > xText = xTextRange->getText();
85 uno::Reference< text::XTextContent > xContext( xTable, uno::UNO_QUERY_THROW );
87 xText->insertTextContent( xTextRange, xContext, true );
88 uno::Reference< word::XTable > xVBATable( new SwVbaTable( mxParent, mxContext, pVbaRange->getDocument(), xTable ) );
89 return xVBATable;
92 uno::Reference< container::XEnumeration > SAL_CALL
93 SwVbaTables::createEnumeration() throw (uno::RuntimeException)
95 return new TableEnumerationImpl( mxParent, mxContext, mxDocument, m_xIndexAccess );
98 // ScVbaCollectionBaseImpl
99 uno::Any
100 SwVbaTables::createCollectionObject( const uno::Any& aSource )
102 return lcl_createTable( mxParent, mxContext, mxDocument, aSource );
105 // XHelperInterface
106 rtl::OUString&
107 SwVbaTables::getServiceImplName()
109 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaTables") );
110 return sImplName;
113 // XEnumerationAccess
114 uno::Type SAL_CALL
115 SwVbaTables::getElementType() throw (uno::RuntimeException)
117 return word::XTable::static_type(0);
120 uno::Sequence<rtl::OUString>
121 SwVbaTables::getServiceNames()
123 static uno::Sequence< rtl::OUString > aServiceNames;
124 if ( aServiceNames.getLength() == 0 )
126 aServiceNames.realloc( 1 );
127 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Tables" ) );
129 return aServiceNames;