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 namespace {
65 
66 class TableCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
67  container::XNameAccess >
68 {
69  XTextTableVec mxTables;
70  XTextTableVec::iterator cachePos;
71 
72 public:
73  explicit TableCollectionHelper( const uno::Reference< frame::XModel >& xDocument )
74  {
75  // only count the tables in the body text, not in the header/footer
76  uno::Reference< container::XIndexAccess > xTables = lcl_getTables( xDocument );
77  sal_Int32 nCount = xTables->getCount();
78  for( sal_Int32 i = 0; i < nCount; i++ )
79  {
80  uno::Reference< text::XTextTable > xTable( xTables->getByIndex( i ) , uno::UNO_QUERY_THROW );
81  if( !lcl_isInHeaderFooter( xTable ) )
82  mxTables.push_back( xTable );
83  }
84  cachePos = mxTables.begin();
85  }
86  // XIndexAccess
87  virtual sal_Int32 SAL_CALL getCount( ) override
88  {
89  return mxTables.size();
90  }
91  virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override
92  {
93  if ( Index < 0 || Index >= getCount() )
94  throw lang::IndexOutOfBoundsException();
95  uno::Reference< text::XTextTable > xTable( mxTables[ Index ], uno::UNO_SET_THROW );
96  return uno::makeAny( xTable );
97  }
98  // XElementAccess
99  virtual uno::Type SAL_CALL getElementType( ) override { return cppu::UnoType<text::XTextTable>::get(); }
100  virtual sal_Bool SAL_CALL hasElements( ) override { return getCount() > 0 ; }
101  // XNameAccess
102  virtual uno::Any SAL_CALL getByName( const OUString& aName ) override
103  {
104  if ( !hasByName(aName) )
105  throw container::NoSuchElementException();
106  uno::Reference< text::XTextTable > xTable( *cachePos, uno::UNO_SET_THROW );
107  return uno::makeAny( xTable );
108  }
109  virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
110  {
111  uno::Sequence< OUString > sNames( mxTables.size() );
112  OUString* pString = sNames.getArray();
113  for ( const auto& rxTable : mxTables )
114  {
115  uno::Reference< container::XNamed > xName( rxTable, uno::UNO_QUERY_THROW );
116  *pString = xName->getName();
117  ++pString;
118  }
119  return sNames;
120  }
121  virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
122  {
123  cachePos = mxTables.begin();
124  XTextTableVec::iterator it_end = mxTables.end();
125  for ( ; cachePos != it_end; ++cachePos )
126  {
127  uno::Reference< container::XNamed > xName( *cachePos, uno::UNO_QUERY_THROW );
128  if ( aName.equalsIgnoreAsciiCase( xName->getName() ) )
129  break;
130  }
131  return ( cachePos != it_end );
132  }
133 };
134 
135 class TableEnumerationImpl : public ::cppu::WeakImplHelper< css::container::XEnumeration >
136 {
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;
142 public:
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)
144  {
145  }
146  virtual sal_Bool SAL_CALL hasMoreElements( ) override
147  {
148  return ( mnCurIndex < mxIndexAccess->getCount() );
149  }
150  virtual uno::Any SAL_CALL nextElement( ) override
151  {
152  if ( !hasMoreElements() )
153  throw container::NoSuchElementException();
154  return lcl_createTable( mxParent, mxContext, mxDocument, mxIndexAccess->getByIndex( mnCurIndex++ ) );
155  }
156 
157 };
158 
159 }
160 
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 )
162 {
163 }
164 
165 uno::Reference< word::XTable > SAL_CALL
166 SwVbaTables::Add( const uno::Reference< word::XRange >& Range, const uno::Any& NumRows, const uno::Any& NumColumns, const uno::Any& /*DefaultTableBehavior*/, const uno::Any& /*AutoFitBehavior*/ )
167 {
168  sal_Int32 nCols = 0;
169  sal_Int32 nRows = 0;
170  SwVbaRange* pVbaRange = dynamic_cast< SwVbaRange* >( Range.get() );
171  // Preconditions
172  if ( !( pVbaRange && ( NumRows >>= nRows ) && ( NumColumns >>= nCols ) ) )
173  throw uno::RuntimeException(); // #FIXME better exception??
174  if ( nCols <= 0 || nRows <= 0 )
175  throw uno::RuntimeException(); // #FIXME better exception??
176 
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();
180 
181  uno::Reference< text::XTextTable > xTable;
182  xTable.set( xMsf->createInstance("com.sun.star.text.TextTable"), uno::UNO_QUERY_THROW );
183 
184  xTable->initialize( nRows, nCols );
185  uno::Reference< text::XText > xText = xTextRange->getText();
186  uno::Reference< text::XTextContent > xContext( xTable, uno::UNO_QUERY_THROW );
187 
188  xText->insertTextContent( xTextRange, xContext, true );
189 
190  // move the current cursor to the first table cell
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 );
193  word::getXTextViewCursor( mxDocument )->gotoRange( xFirstCellText->getStart(), false );
194 
195  uno::Reference< word::XTable > xVBATable( new SwVbaTable( mxParent, mxContext, pVbaRange->getDocument(), xTable ) );
196  return xVBATable;
197 }
198 
199 uno::Reference< container::XEnumeration > SAL_CALL
201 {
202  return new TableEnumerationImpl( mxParent, mxContext, mxDocument, m_xIndexAccess );
203 }
204 
205 // ScVbaCollectionBaseImpl
206 uno::Any
208 {
209  return lcl_createTable( mxParent, mxContext, mxDocument, aSource );
210 }
211 
212 // XHelperInterface
213 OUString
215 {
216  return "SwVbaTables";
217 }
218 
219 // XEnumerationAccess
220 uno::Type SAL_CALL
222 {
224 }
225 
226 uno::Sequence<OUString>
228 {
229  static uno::Sequence< OUString > const aServiceNames
230  {
231  "ooo.vba.word.Tables"
232  };
233  return aServiceNames;
234 }
235 
236 /* 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:227
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
WeakReference< XInterface > mxParent
Reference
Sequence< OUString > aServiceNames
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: vbatables.cxx:200
css::uno::Reference< css::frame::XModel > mxDocument
Definition: vbatables.hxx:30
int nCount
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:207
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
int i
unsigned char sal_Bool
css::uno::WeakReference< ov::XHelperInterface > mxParent
css::uno::Type const & get()
std::vector< uno::Reference< text::XTextTable > > XTextTableVec
Definition: vbatables.cxx:62
virtual css::uno::Type SAL_CALL getElementType() override
Definition: vbatables.cxx:221
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:161
virtual OUString getServiceImplName() override
Definition: vbatables.cxx:214
OUString aName
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:166
Reference< XModel > xModel
struct _ADOIndex Index
Reference< XComponentContext > mxContext