LibreOffice Module sw (master)  1
vbatables.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "vbatables.hxx"
21 #include "vbatable.hxx"
22 #include "vbarange.hxx"
23 #include "wordvbahelper.hxx"
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>
31 #include <cppuhelper/implbase.hxx>
32 
33 using namespace ::ooo::vba;
34 using namespace css;
35 
36 static uno::Reference< container::XIndexAccess > lcl_getTables( const uno::Reference< frame::XModel >& xDoc )
37 {
38  uno::Reference< container::XIndexAccess > xTables;
39  uno::Reference< text::XTextTablesSupplier > xSupp( xDoc, uno::UNO_QUERY );
40  if ( xSupp.is() )
41  xTables.set( xSupp->getTextTables(), uno::UNO_QUERY_THROW );
42  return xTables;
43 }
44 
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 )
46 {
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 );
51 }
52 
53 static bool lcl_isInHeaderFooter( const uno::Reference< text::XTextTable >& xTable )
54 {
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";
60 }
61 
62 typedef std::vector< uno::Reference< text::XTextTable > > XTextTableVec;
63 
64 class TableCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
65  container::XNameAccess >
66 {
68  XTextTableVec::iterator cachePos;
69 
70 public:
71  explicit TableCollectionHelper( const uno::Reference< frame::XModel >& xDocument )
72  {
73  // only count the tables in the body text, not in the header/footer
74  uno::Reference< container::XIndexAccess > xTables = lcl_getTables( xDocument );
75  sal_Int32 nCount = xTables->getCount();
76  for( sal_Int32 i = 0; i < nCount; i++ )
77  {
78  uno::Reference< text::XTextTable > xTable( xTables->getByIndex( i ) , uno::UNO_QUERY_THROW );
79  if( !lcl_isInHeaderFooter( xTable ) )
80  mxTables.push_back( xTable );
81  }
82  cachePos = mxTables.begin();
83  }
84  // XIndexAccess
85  virtual sal_Int32 SAL_CALL getCount( ) override
86  {
87  return mxTables.size();
88  }
89  virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override
90  {
91  if ( Index < 0 || Index >= getCount() )
92  throw lang::IndexOutOfBoundsException();
93  uno::Reference< text::XTextTable > xTable( mxTables[ Index ], uno::UNO_SET_THROW );
94  return uno::makeAny( xTable );
95  }
96  // XElementAccess
97  virtual uno::Type SAL_CALL getElementType( ) override { return cppu::UnoType<text::XTextTable>::get(); }
98  virtual sal_Bool SAL_CALL hasElements( ) override { return getCount() > 0 ; }
99  // XNameAccess
100  virtual uno::Any SAL_CALL getByName( const OUString& aName ) override
101  {
102  if ( !hasByName(aName) )
103  throw container::NoSuchElementException();
104  uno::Reference< text::XTextTable > xTable( *cachePos, uno::UNO_SET_THROW );
105  return uno::makeAny( xTable );
106  }
107  virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
108  {
109  uno::Sequence< OUString > sNames( mxTables.size() );
110  OUString* pString = sNames.getArray();
111  for ( const auto& rxTable : mxTables )
112  {
113  uno::Reference< container::XNamed > xName( rxTable, uno::UNO_QUERY_THROW );
114  *pString = xName->getName();
115  ++pString;
116  }
117  return sNames;
118  }
119  virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
120  {
121  cachePos = mxTables.begin();
122  XTextTableVec::iterator it_end = mxTables.end();
123  for ( ; cachePos != it_end; ++cachePos )
124  {
125  uno::Reference< container::XNamed > xName( *cachePos, uno::UNO_QUERY_THROW );
126  if ( aName.equalsIgnoreAsciiCase( xName->getName() ) )
127  break;
128  }
129  return ( cachePos != it_end );
130  }
131 };
132 
133 class TableEnumerationImpl : public ::cppu::WeakImplHelper< css::container::XEnumeration >
134 {
135  uno::Reference< XHelperInterface > mxParent;
136  uno::Reference< uno::XComponentContext > mxContext;
137  uno::Reference< frame::XModel > mxDocument;
138  uno::Reference< container::XIndexAccess > mxIndexAccess;
139  sal_Int32 mnCurIndex;
140 public:
141  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)
142  {
143  }
144  virtual sal_Bool SAL_CALL hasMoreElements( ) override
145  {
146  return ( mnCurIndex < mxIndexAccess->getCount() );
147  }
148  virtual uno::Any SAL_CALL nextElement( ) override
149  {
150  if ( !hasMoreElements() )
151  throw container::NoSuchElementException();
152  return lcl_createTable( mxParent, mxContext, mxDocument, mxIndexAccess->getByIndex( mnCurIndex++ ) );
153  }
154 
155 };
156 
157 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 )
158 {
159 }
160 
161 uno::Reference< word::XTable > SAL_CALL
162 SwVbaTables::Add( const uno::Reference< word::XRange >& Range, const uno::Any& NumRows, const uno::Any& NumColumns, const uno::Any& /*DefaultTableBehavior*/, const uno::Any& /*AutoFitBehavior*/ )
163 {
164  sal_Int32 nCols = 0;
165  sal_Int32 nRows = 0;
166  SwVbaRange* pVbaRange = dynamic_cast< SwVbaRange* >( Range.get() );
167  // Preconditions
168  if ( !( pVbaRange && ( NumRows >>= nRows ) && ( NumColumns >>= nCols ) ) )
169  throw uno::RuntimeException(); // #FIXME better exception??
170  if ( nCols <= 0 || nRows <= 0 )
171  throw uno::RuntimeException(); // #FIXME better exception??
172 
173  uno::Reference< frame::XModel > xModel( pVbaRange->getDocument(), uno::UNO_QUERY_THROW );
174  uno::Reference< lang::XMultiServiceFactory > xMsf( xModel, uno::UNO_QUERY_THROW );
175  uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
176 
177  uno::Reference< text::XTextTable > xTable;
178  xTable.set( xMsf->createInstance("com.sun.star.text.TextTable"), uno::UNO_QUERY_THROW );
179 
180  xTable->initialize( nRows, nCols );
181  uno::Reference< text::XText > xText = xTextRange->getText();
182  uno::Reference< text::XTextContent > xContext( xTable, uno::UNO_QUERY_THROW );
183 
184  xText->insertTextContent( xTextRange, xContext, true );
185 
186  // move the current cursor to the first table cell
187  uno::Reference< table::XCellRange > xCellRange( xTable, uno::UNO_QUERY_THROW );
188  uno::Reference< text::XText> xFirstCellText( xCellRange->getCellByPosition(0, 0), uno::UNO_QUERY_THROW );
189  word::getXTextViewCursor( mxDocument )->gotoRange( xFirstCellText->getStart(), false );
190 
191  uno::Reference< word::XTable > xVBATable( new SwVbaTable( mxParent, mxContext, pVbaRange->getDocument(), xTable ) );
192  return xVBATable;
193 }
194 
195 uno::Reference< container::XEnumeration > SAL_CALL
197 {
199 }
200 
201 // ScVbaCollectionBaseImpl
202 uno::Any
204 {
205  return lcl_createTable( mxParent, mxContext, mxDocument, aSource );
206 }
207 
208 // XHelperInterface
209 OUString
211 {
212  return "SwVbaTables";
213 }
214 
215 // XEnumerationAccess
216 uno::Type SAL_CALL
218 {
220 }
221 
222 uno::Sequence<OUString>
224 {
225  static uno::Sequence< OUString > const aServiceNames
226  {
227  "ooo.vba.word.Tables"
228  };
229  return aServiceNames;
230 }
231 
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static uno::Reference< container::XIndexAccess > lcl_getTables(const uno::Reference< frame::XModel > &xDoc)
Definition: vbatables.cxx:36
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbatables.cxx:223
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
Reference
virtual uno::Any SAL_CALL nextElement() override
Definition: vbatables.cxx:148
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: vbatables.cxx:196
uno::Reference< container::XIndexAccess > mxIndexAccess
Definition: vbatables.cxx:138
uno::Reference< uno::XComponentContext > mxContext
Definition: vbatables.cxx:136
css::uno::Reference< css::frame::XModel > mxDocument
Definition: vbatables.hxx:30
virtual uno::Sequence< OUString > SAL_CALL getElementNames() override
Definition: vbatables.cxx:107
virtual css::uno::Reference< css::text::XTextRange > SAL_CALL getXTextRange() override
Definition: vbarange.cxx:89
uno::Reference< text::XTextViewCursor > getXTextViewCursor(const uno::Reference< frame::XModel > &xModel)
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
Definition: vbatables.cxx:203
static bool lcl_isInHeaderFooter(const uno::Reference< text::XTextTable > &xTable)
Definition: vbatables.cxx:53
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)
Definition: vbatables.cxx:45
virtual sal_Int32 SAL_CALL getCount() override
Definition: vbatables.cxx:85
virtual uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
Definition: vbatables.cxx:89
TableCollectionHelper(const uno::Reference< frame::XModel > &xDocument)
Definition: vbatables.cxx:71
int i
XTextTableVec::iterator cachePos
Definition: vbatables.cxx:68
unsigned char sal_Bool
css::uno::WeakReference< ov::XHelperInterface > mxParent
css::uno::Type const & get()
virtual sal_Bool SAL_CALL hasElements() override
Definition: vbatables.cxx:98
uno::Reference< frame::XModel > mxDocument
Definition: vbatables.cxx:137
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
Definition: vbatables.cxx:119
std::vector< uno::Reference< text::XTextTable > > XTextTableVec
Definition: vbatables.cxx:62
virtual css::uno::Type SAL_CALL getElementType() override
Definition: vbatables.cxx:217
virtual uno::Any SAL_CALL getByName(const OUString &aName) override
Definition: vbatables.cxx:100
css::uno::Reference< css::uno::XComponentContext > mxContext
const css::uno::Reference< css::text::XTextDocument > & getDocument() const
Definition: vbarange.hxx:56
SwVbaTables(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::frame::XModel > &xDocument)
Definition: vbatables.cxx:157
virtual OUString getServiceImplName() override
Definition: vbatables.cxx:210
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
Definition: vbatables.cxx:162
XTextTableVec mxTables
Definition: vbatables.cxx:67
virtual sal_Bool SAL_CALL hasMoreElements() override
Definition: vbatables.cxx:144
uno::Reference< XHelperInterface > mxParent
Definition: vbatables.cxx:135
Reference< XModel > xModel
virtual uno::Type SAL_CALL getElementType() override
Definition: vbatables.cxx:97
TableEnumerationImpl(const uno::Reference< XHelperInterface > &xParent, const uno::Reference< uno::XComponentContext > &xContext, const uno::Reference< frame::XModel > &xDocument, const uno::Reference< container::XIndexAccess > &xIndexAccess)
Definition: vbatables.cxx:141