xref: /aoo42x/main/sw/source/ui/vba/vbatables.cxx (revision efeef26f)
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 #include "vbatables.hxx"
23 #include "vbatable.hxx"
24 #include "vbarange.hxx"
25 #include <com/sun/star/text/XTextTable.hpp>
26 #include <com/sun/star/text/XTextTablesSupplier.hpp>
27 #include <com/sun/star/text/XTextDocument.hpp>
28 #include <comphelper/componentcontext.hxx>
29 
30 using namespace ::ooo::vba;
31 using namespace css;
32 
lcl_getTables(const uno::Reference<frame::XModel> & xDoc)33 uno::Reference< container::XIndexAccess > lcl_getTables( const uno::Reference< frame::XModel >& xDoc )
34 {
35     uno::Reference< container::XIndexAccess > xTables;
36     uno::Reference< text::XTextTablesSupplier > xSupp( xDoc, uno::UNO_QUERY );
37     if ( xSupp.is() )
38         xTables.set( xSupp->getTextTables(), uno::UNO_QUERY_THROW );
39     return xTables;
40 }
41 
lcl_createTable(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xDocument,const uno::Any & aSource)42 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 )
43 {
44     uno::Reference< text::XTextTable > xTextTable( aSource, uno::UNO_QUERY_THROW );
45     uno::Reference< text::XTextDocument > xTextDocument( xDocument, uno::UNO_QUERY_THROW );
46     uno::Reference< word::XTable > xTable( new SwVbaTable( xParent, xContext, xTextDocument, xTextTable ) );
47     return uno::makeAny( xTable );
48 }
49 
50 typedef ::cppu::WeakImplHelper1< css::container::XEnumeration > EnumBase;
51 
52 class TableEnumerationImpl : public EnumBase
53 {
54     uno::Reference< XHelperInterface > mxParent;
55     uno::Reference< uno::XComponentContext > mxContext;
56     uno::Reference< frame::XModel > mxDocument;
57     uno::Reference< container::XIndexAccess > mxIndexAccess;
58     sal_Int32 mnCurIndex;
59 public:
TableEnumerationImpl(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xDocument,const uno::Reference<container::XIndexAccess> & xIndexAccess)60     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)
61     {
62     }
hasMoreElements()63     virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
64     {
65         return ( mnCurIndex < mxIndexAccess->getCount() );
66     }
nextElement()67     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
68     {
69         if ( !hasMoreElements() )
70             throw container::NoSuchElementException();
71         return lcl_createTable( mxParent, mxContext, mxDocument, mxIndexAccess->getByIndex( mnCurIndex++ ) );
72     }
73 
74 };
75 
SwVbaTables(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xDocument)76 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 )
77 {
78 }
79 
80 
81 uno::Reference< word::XTable > SAL_CALL
Add(const uno::Reference<word::XRange> & Range,const uno::Any & NumRows,const uno::Any & NumColumns,const uno::Any &,const uno::Any &)82 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)
83 {
84     sal_Int32 nCols = 0;
85     sal_Int32 nRows = 0;
86     SwVbaRange* pVbaRange = dynamic_cast< SwVbaRange* >( Range.get() );
87     // Preconditions
88     if ( !( pVbaRange && ( NumRows >>= nRows ) && ( NumColumns >>= nCols ) ) )
89         throw uno::RuntimeException(); // #FIXME better exception??
90     if ( nCols <= 0 || nRows <= 0 )
91         throw uno::RuntimeException(); // #FIXME better exception??
92 
93     uno::Reference< frame::XModel > xModel( pVbaRange->getDocument(), uno::UNO_QUERY_THROW );
94     uno::Reference< lang::XMultiServiceFactory > xMsf( xModel, uno::UNO_QUERY_THROW );
95     uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
96 
97     uno::Reference< text::XTextTable > xTable;
98     xTable.set( xMsf->createInstance( rtl::OUString::createFromAscii("com.sun.star.text.TextTable") ), uno::UNO_QUERY_THROW );
99 /*
100     comphelper::ComponentContext aCtx( xMsf );
101     if ( !aCtx.createComponent( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.TextTable") ), xTable ) );
102         throw uno::RuntimeException(); // #FIXME better exception??
103 */
104     xTable->initialize( nRows, nCols );
105     uno::Reference< text::XText > xText = xTextRange->getText();
106     uno::Reference< text::XTextContent > xContext( xTable, uno::UNO_QUERY_THROW );
107 
108     xText->insertTextContent( xTextRange, xContext, true );
109     uno::Reference< word::XTable > xVBATable( new SwVbaTable( mxParent, mxContext,  pVbaRange->getDocument(), xTable ) );
110     return xVBATable;
111 }
112 
113 uno::Reference< container::XEnumeration > SAL_CALL
createEnumeration()114 SwVbaTables::createEnumeration() throw (uno::RuntimeException)
115 {
116     return new TableEnumerationImpl( mxParent, mxContext, mxDocument, m_xIndexAccess );
117 }
118 
119 // ScVbaCollectionBaseImpl
120 uno::Any
createCollectionObject(const uno::Any & aSource)121 SwVbaTables::createCollectionObject( const uno::Any& aSource )
122 {
123     return lcl_createTable( mxParent, mxContext, mxDocument, aSource );
124 }
125 
126 // XHelperInterface
127 rtl::OUString&
getServiceImplName()128 SwVbaTables::getServiceImplName()
129 {
130     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaTables") );
131     return sImplName;
132 }
133 
134 // XEnumerationAccess
135 uno::Type SAL_CALL
getElementType()136 SwVbaTables::getElementType() throw (uno::RuntimeException)
137 {
138     return  word::XTable::static_type(0);
139 }
140 
141 uno::Sequence<rtl::OUString>
getServiceNames()142 SwVbaTables::getServiceNames()
143 {
144     static uno::Sequence< rtl::OUString > aServiceNames;
145     if ( aServiceNames.getLength() == 0 )
146     {
147         aServiceNames.realloc( 1 );
148         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Tables" ) );
149     }
150     return aServiceNames;
151 }
152 
153