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