Line data Source code
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 "vbahyperlinks.hxx"
21 : #include <algorithm>
22 : #include <vector>
23 : #include <ooo/vba/office/MsoHyperlinkType.hpp>
24 : #include "rangelst.hxx"
25 : #include "vbahyperlink.hxx"
26 : #include "vbarange.hxx"
27 :
28 : using namespace ::ooo::vba;
29 : using namespace ::com::sun::star;
30 :
31 : namespace {
32 :
33 : /** Returns true, if every range of rxInner is contained in any range of rScOuter. */
34 0 : bool lclContains( const ScRangeList& rScOuter, const uno::Reference< excel::XRange >& rxInner ) throw (uno::RuntimeException)
35 : {
36 0 : const ScRangeList& rScInner = ScVbaRange::getScRangeList( rxInner );
37 0 : if( rScInner.empty() || rScOuter.empty() )
38 0 : throw uno::RuntimeException("Empty range objects", uno::Reference< uno::XInterface >() );
39 :
40 0 : for( size_t nIndex = 0, nCount = rScInner.size(); nIndex < nCount; ++nIndex )
41 0 : if( !rScOuter.In( *rScInner[ nIndex ] ) )
42 0 : return false;
43 0 : return true;
44 : }
45 :
46 : /** Functor to decide whether the anchors of two Hyperlink objects are equal. */
47 0 : struct EqualAnchorFunctor
48 : {
49 : uno::Reference< excel::XRange > mxAnchorRange;
50 : uno::Reference< msforms::XShape > mxAnchorShape;
51 : sal_Int32 mnType;
52 : EqualAnchorFunctor( const uno::Reference< excel::XHyperlink >& rxHlink ) throw (uno::RuntimeException);
53 : bool operator()( const uno::Reference< excel::XHyperlink >& rxHlink ) const throw (uno::RuntimeException);
54 : };
55 :
56 0 : EqualAnchorFunctor::EqualAnchorFunctor( const uno::Reference< excel::XHyperlink >& rxHlink ) throw (uno::RuntimeException) :
57 0 : mnType( rxHlink->getType() )
58 : {
59 0 : switch( mnType )
60 : {
61 : case office::MsoHyperlinkType::msoHyperlinkRange:
62 0 : mxAnchorRange.set( rxHlink->getRange(), uno::UNO_QUERY_THROW );
63 0 : break;
64 : case office::MsoHyperlinkType::msoHyperlinkShape:
65 : case office::MsoHyperlinkType::msoHyperlinkInlineShape:
66 0 : mxAnchorShape.set( rxHlink->getShape(), uno::UNO_QUERY_THROW );
67 0 : break;
68 : default:
69 0 : throw uno::RuntimeException();
70 : }
71 0 : }
72 :
73 0 : bool EqualAnchorFunctor::operator()( const uno::Reference< excel::XHyperlink >& rxHlink ) const throw (uno::RuntimeException)
74 : {
75 0 : sal_Int32 nType = rxHlink->getType();
76 0 : if( nType != mnType )
77 0 : return false;
78 :
79 0 : switch( nType )
80 : {
81 : case office::MsoHyperlinkType::msoHyperlinkRange:
82 : {
83 0 : uno::Reference< excel::XRange > xAnchorRange( rxHlink->getRange(), uno::UNO_QUERY_THROW );
84 0 : const ScRangeList& rScRanges1 = ScVbaRange::getScRangeList( xAnchorRange );
85 0 : const ScRangeList& rScRanges2 = ScVbaRange::getScRangeList( mxAnchorRange );
86 0 : return (rScRanges1.size() == 1) && (rScRanges2.size() == 1) && (*rScRanges1[ 0 ] == *rScRanges2[ 0 ]);
87 : }
88 : case office::MsoHyperlinkType::msoHyperlinkShape:
89 : case office::MsoHyperlinkType::msoHyperlinkInlineShape:
90 : {
91 0 : uno::Reference< msforms::XShape > xAnchorShape( rxHlink->getShape(), uno::UNO_QUERY_THROW );
92 0 : return xAnchorShape.get() == mxAnchorShape.get();
93 : }
94 : default:
95 0 : throw uno::RuntimeException();
96 : }
97 : }
98 :
99 : } // namespace
100 :
101 : namespace detail {
102 :
103 : class ScVbaHlinkContainer : public ::cppu::WeakImplHelper1< container::XIndexAccess >
104 : {
105 : public:
106 : explicit ScVbaHlinkContainer() throw (uno::RuntimeException);
107 : explicit ScVbaHlinkContainer( const ScVbaHlinkContainerRef& rxSheetContainer, const ScRangeList& rScRanges ) throw (uno::RuntimeException);
108 : virtual ~ScVbaHlinkContainer();
109 :
110 : /** Inserts the passed hyperlink into the collection. Will remove a
111 : Hyperlink object with the same anchor as the passed Hyperlink object. */
112 : void insertHyperlink( const uno::Reference< excel::XHyperlink >& rxHlink ) throw (uno::RuntimeException);
113 :
114 : // XIndexAccess
115 : virtual sal_Int32 SAL_CALL getCount() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
116 : virtual uno::Any SAL_CALL getByIndex( sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
117 :
118 : // XElementAccess
119 : virtual uno::Type SAL_CALL getElementType() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
120 : virtual sal_Bool SAL_CALL hasElements() throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
121 :
122 : private:
123 : typedef ::std::vector< uno::Reference< excel::XHyperlink > > HyperlinkVector;
124 : HyperlinkVector maHlinks;
125 : };
126 :
127 0 : ScVbaHlinkContainer::ScVbaHlinkContainer() throw (uno::RuntimeException)
128 : {
129 : // TODO FIXME: fill with existing hyperlinks
130 0 : }
131 :
132 0 : ScVbaHlinkContainer::ScVbaHlinkContainer( const ScVbaHlinkContainerRef& rxSheetContainer,
133 0 : const ScRangeList& rScRanges ) throw (uno::RuntimeException)
134 : {
135 0 : for( sal_Int32 nIndex = 0, nCount = rxSheetContainer->getCount(); nIndex < nCount; ++nIndex )
136 : {
137 0 : uno::Reference< excel::XHyperlink > xHlink( rxSheetContainer->getByIndex( nIndex ), uno::UNO_QUERY_THROW );
138 0 : uno::Reference< excel::XRange > xHlinkRange( xHlink->getRange(), uno::UNO_QUERY_THROW );
139 0 : if( lclContains( rScRanges, xHlinkRange ) )
140 0 : maHlinks.push_back( xHlink );
141 0 : }
142 0 : }
143 :
144 0 : ScVbaHlinkContainer::~ScVbaHlinkContainer()
145 : {
146 0 : }
147 :
148 0 : void ScVbaHlinkContainer::insertHyperlink( const uno::Reference< excel::XHyperlink >& rxHlink ) throw (uno::RuntimeException)
149 : {
150 0 : HyperlinkVector::iterator aIt = ::std::find_if( maHlinks.begin(), maHlinks.end(), EqualAnchorFunctor( rxHlink ) );
151 0 : if( aIt == maHlinks.end() )
152 0 : maHlinks.push_back( rxHlink );
153 : else
154 0 : *aIt = rxHlink;
155 0 : }
156 :
157 0 : sal_Int32 SAL_CALL ScVbaHlinkContainer::getCount() throw (uno::RuntimeException, std::exception)
158 : {
159 0 : return static_cast< sal_Int32 >( maHlinks.size() );
160 : }
161 :
162 0 : uno::Any SAL_CALL ScVbaHlinkContainer::getByIndex( sal_Int32 nIndex )
163 : throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
164 : {
165 0 : if( (0 <= nIndex) && (nIndex < getCount()) )
166 0 : return uno::Any( maHlinks[ static_cast< size_t >( nIndex ) ] );
167 0 : throw lang::IndexOutOfBoundsException();
168 : }
169 :
170 0 : uno::Type SAL_CALL ScVbaHlinkContainer::getElementType() throw (uno::RuntimeException, std::exception)
171 : {
172 0 : return cppu::UnoType<excel::XHyperlink>::get();
173 : }
174 :
175 0 : sal_Bool SAL_CALL ScVbaHlinkContainer::hasElements() throw (uno::RuntimeException, std::exception)
176 : {
177 0 : return !maHlinks.empty();
178 : }
179 :
180 0 : ScVbaHlinkContainerMember::ScVbaHlinkContainerMember( ScVbaHlinkContainer* pContainer ) :
181 0 : mxContainer( pContainer )
182 : {
183 0 : }
184 :
185 0 : ScVbaHlinkContainerMember::~ScVbaHlinkContainerMember()
186 : {
187 0 : }
188 :
189 : } // namespace detail
190 :
191 0 : ScVbaHyperlinks::ScVbaHyperlinks( const uno::Reference< XHelperInterface >& rxParent,
192 : const uno::Reference< uno::XComponentContext >& rxContext ) throw (uno::RuntimeException) :
193 0 : detail::ScVbaHlinkContainerMember( new detail::ScVbaHlinkContainer ),
194 0 : ScVbaHyperlinks_BASE( rxParent, rxContext, uno::Reference< container::XIndexAccess >( mxContainer.get() ) )
195 : {
196 0 : }
197 :
198 0 : ScVbaHyperlinks::ScVbaHyperlinks( const uno::Reference< XHelperInterface >& rxParent,
199 : const uno::Reference< uno::XComponentContext >& rxContext,
200 : const ScVbaHyperlinksRef& rxSheetHlinks, const ScRangeList& rScRanges ) throw (uno::RuntimeException) :
201 0 : detail::ScVbaHlinkContainerMember( new detail::ScVbaHlinkContainer( rxSheetHlinks->mxContainer, rScRanges ) ),
202 0 : ScVbaHyperlinks_BASE( rxParent, rxContext, uno::Reference< container::XIndexAccess >( mxContainer.get() ) ),
203 0 : mxSheetHlinks( rxSheetHlinks )
204 : {
205 0 : }
206 :
207 0 : ScVbaHyperlinks::~ScVbaHyperlinks()
208 : {
209 0 : }
210 :
211 : // XHyperlinks ----------------------------------------------------------------
212 :
213 0 : uno::Reference< excel::XHyperlink > SAL_CALL ScVbaHyperlinks::Add(
214 : const uno::Any& rAnchor, const uno::Any& rAddress, const uno::Any& rSubAddress,
215 : const uno::Any& rScreenTip, const uno::Any& rTextToDisplay ) throw (uno::RuntimeException, std::exception)
216 : {
217 : /* If this Hyperlinks object has been craeted from a Range object, the
218 : call to Add() is passed to the Hyperlinks object of the parent
219 : worksheet. This container will not be modified (it will not contain the
220 : inserted hyperlink).
221 : For details, see documentation in hyperlinks.hxx.
222 : */
223 0 : if( mxSheetHlinks.is() )
224 0 : return mxSheetHlinks->Add( rAnchor, rAddress, rSubAddress, rScreenTip, rTextToDisplay );
225 :
226 : // get anchor object (can be a Range or a Shape object)
227 0 : uno::Reference< XHelperInterface > xAnchor( rAnchor, uno::UNO_QUERY_THROW );
228 :
229 : /* Create the Hyperlink object, this tries to insert the hyperlink into
230 : the spreadsheet document. Parent of the Hyperlink is the anchor object. */
231 : uno::Reference< excel::XHyperlink > xHlink( new ScVbaHyperlink(
232 0 : xAnchor, mxContext, rAddress, rSubAddress, rScreenTip, rTextToDisplay ) );
233 :
234 : /* If creation of the hyperlink did not throw, insert it into the
235 : collection. */
236 0 : mxContainer->insertHyperlink( xHlink );
237 0 : return xHlink;
238 : }
239 :
240 0 : void SAL_CALL ScVbaHyperlinks::Delete() throw (uno::RuntimeException, std::exception)
241 : {
242 : // FIXME not implemented
243 0 : throw uno::RuntimeException();
244 : }
245 :
246 : // XEnumerationAccess ---------------------------------------------------------
247 :
248 0 : uno::Reference< container::XEnumeration > SAL_CALL ScVbaHyperlinks::createEnumeration() throw (uno::RuntimeException)
249 : {
250 0 : return new SimpleIndexAccessToEnumeration( m_xIndexAccess );
251 : }
252 :
253 : // XElementAccess -------------------------------------------------------------
254 :
255 0 : uno::Type SAL_CALL ScVbaHyperlinks::getElementType() throw (uno::RuntimeException)
256 : {
257 0 : return cppu::UnoType<excel::XHyperlink>::get();
258 : }
259 :
260 : // ScVbaCollectionBase --------------------------------------------------------
261 :
262 0 : uno::Any ScVbaHyperlinks::createCollectionObject( const uno::Any& rSource )
263 : {
264 : // container stores XHyperlink objects, just return the passed object
265 0 : return rSource;
266 : }
267 :
268 : // XHelperInterface -----------------------------------------------------------
269 :
270 0 : VBAHELPER_IMPL_XHELPERINTERFACE( ScVbaHyperlinks, "ooo.vba.excel.Hyperlinks" )
271 :
272 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|