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