xref: /aoo41x/main/sc/source/ui/vba/vbahyperlinks.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "vbahyperlinks.hxx"
29*cdf0e10cSrcweir #include <algorithm>
30*cdf0e10cSrcweir #include <vector>
31*cdf0e10cSrcweir #include <ooo/vba/office/MsoHyperlinkType.hpp>
32*cdf0e10cSrcweir #include "rangelst.hxx"
33*cdf0e10cSrcweir #include "vbahyperlink.hxx"
34*cdf0e10cSrcweir #include "vbarange.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir using namespace ::ooo::vba;
37*cdf0e10cSrcweir using namespace ::com::sun::star;
38*cdf0e10cSrcweir using ::rtl::OUString;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir // ============================================================================
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir namespace {
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir /** Returns true, if every range of rxInner is contained in any range of rScOuter. */
45*cdf0e10cSrcweir bool lclContains( const ScRangeList& rScOuter, const uno::Reference< excel::XRange >& rxInner ) throw (uno::RuntimeException)
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir     const ScRangeList& rScInner = ScVbaRange::getScRangeList( rxInner );
48*cdf0e10cSrcweir     if( (rScInner.Count() == 0) || (rScOuter.Count() == 0) )
49*cdf0e10cSrcweir         throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Empty range objects" ) ), uno::Reference< uno::XInterface >() );
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir     for( sal_uLong nIndex = 0, nCount = rScInner.Count(); nIndex < nCount; ++nIndex )
52*cdf0e10cSrcweir         if( !rScOuter.In( *rScInner.GetObject( nIndex ) ) )
53*cdf0e10cSrcweir             return false;
54*cdf0e10cSrcweir     return true;
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir // ----------------------------------------------------------------------------
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir /** Functor to decide whether the anchors of two Hyperlink objects are equal. */
60*cdf0e10cSrcweir struct EqualAnchorFunctor
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir     uno::Reference< excel::XRange > mxAnchorRange;
63*cdf0e10cSrcweir     uno::Reference< msforms::XShape > mxAnchorShape;
64*cdf0e10cSrcweir     sal_Int32 mnType;
65*cdf0e10cSrcweir     EqualAnchorFunctor( const uno::Reference< excel::XHyperlink >& rxHlink ) throw (uno::RuntimeException);
66*cdf0e10cSrcweir     bool operator()( const uno::Reference< excel::XHyperlink >& rxHlink ) const throw (uno::RuntimeException);
67*cdf0e10cSrcweir };
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir EqualAnchorFunctor::EqualAnchorFunctor( const uno::Reference< excel::XHyperlink >& rxHlink ) throw (uno::RuntimeException) :
70*cdf0e10cSrcweir     mnType( rxHlink->getType() )
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir     switch( mnType )
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         case office::MsoHyperlinkType::msoHyperlinkRange:
75*cdf0e10cSrcweir             mxAnchorRange.set( rxHlink->getRange(), uno::UNO_QUERY_THROW );
76*cdf0e10cSrcweir         break;
77*cdf0e10cSrcweir         case office::MsoHyperlinkType::msoHyperlinkShape:
78*cdf0e10cSrcweir         case office::MsoHyperlinkType::msoHyperlinkInlineShape:
79*cdf0e10cSrcweir             mxAnchorShape.set( rxHlink->getShape(), uno::UNO_QUERY_THROW );
80*cdf0e10cSrcweir         break;
81*cdf0e10cSrcweir         default:
82*cdf0e10cSrcweir             throw uno::RuntimeException();
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir bool EqualAnchorFunctor::operator()( const uno::Reference< excel::XHyperlink >& rxHlink ) const throw (uno::RuntimeException)
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir     sal_Int32 nType = rxHlink->getType();
89*cdf0e10cSrcweir     if( nType != mnType )
90*cdf0e10cSrcweir         return false;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     switch( nType )
93*cdf0e10cSrcweir     {
94*cdf0e10cSrcweir         case office::MsoHyperlinkType::msoHyperlinkRange:
95*cdf0e10cSrcweir         {
96*cdf0e10cSrcweir             uno::Reference< excel::XRange > xAnchorRange( rxHlink->getRange(), uno::UNO_QUERY_THROW );
97*cdf0e10cSrcweir             const ScRangeList& rScRanges1 = ScVbaRange::getScRangeList( xAnchorRange );
98*cdf0e10cSrcweir             const ScRangeList& rScRanges2 = ScVbaRange::getScRangeList( mxAnchorRange );
99*cdf0e10cSrcweir             return (rScRanges1.Count() == 1) && (rScRanges2.Count() == 1) && (*rScRanges1.GetObject( 0 ) == *rScRanges2.GetObject( 0 ));
100*cdf0e10cSrcweir         }
101*cdf0e10cSrcweir         case office::MsoHyperlinkType::msoHyperlinkShape:
102*cdf0e10cSrcweir         case office::MsoHyperlinkType::msoHyperlinkInlineShape:
103*cdf0e10cSrcweir         {
104*cdf0e10cSrcweir             uno::Reference< msforms::XShape > xAnchorShape( rxHlink->getShape(), uno::UNO_QUERY_THROW );
105*cdf0e10cSrcweir             return xAnchorShape.get() == mxAnchorShape.get();
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir         default:
108*cdf0e10cSrcweir             throw uno::RuntimeException();
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir } // namespace
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir // ============================================================================
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir namespace detail {
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir class ScVbaHlinkContainer : public ::cppu::WeakImplHelper1< container::XIndexAccess >
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir public:
121*cdf0e10cSrcweir     explicit ScVbaHlinkContainer() throw (uno::RuntimeException);
122*cdf0e10cSrcweir     explicit ScVbaHlinkContainer( const ScVbaHlinkContainerRef& rxSheetContainer, const ScRangeList& rScRanges ) throw (uno::RuntimeException);
123*cdf0e10cSrcweir     virtual ~ScVbaHlinkContainer();
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     /** Inserts the passed hyperlink into the collection. Will remove a
126*cdf0e10cSrcweir         Hyperlink object with the same anchor as the passed Hyperlink object. */
127*cdf0e10cSrcweir     void insertHyperlink( const uno::Reference< excel::XHyperlink >& rxHlink ) throw (uno::RuntimeException);
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     // XIndexAccess
130*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL getCount() throw (uno::RuntimeException);
131*cdf0e10cSrcweir     virtual uno::Any SAL_CALL getByIndex( sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException);
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     // XElementAccess
134*cdf0e10cSrcweir     virtual uno::Type SAL_CALL getElementType() throw (uno::RuntimeException);
135*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasElements() throw (uno::RuntimeException);
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir private:
138*cdf0e10cSrcweir     typedef ::std::vector< uno::Reference< excel::XHyperlink > > HyperlinkVector;
139*cdf0e10cSrcweir     HyperlinkVector     maHlinks;
140*cdf0e10cSrcweir };
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir // ----------------------------------------------------------------------------
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir ScVbaHlinkContainer::ScVbaHlinkContainer() throw (uno::RuntimeException)
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir     // TODO FIXME: fill with existing hyperlinks
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir ScVbaHlinkContainer::ScVbaHlinkContainer( const ScVbaHlinkContainerRef& rxSheetContainer,
150*cdf0e10cSrcweir         const ScRangeList& rScRanges ) throw (uno::RuntimeException)
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir     for( sal_Int32 nIndex = 0, nCount = rxSheetContainer->getCount(); nIndex < nCount; ++nIndex )
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         uno::Reference< excel::XHyperlink > xHlink( rxSheetContainer->getByIndex( nIndex ), uno::UNO_QUERY_THROW );
155*cdf0e10cSrcweir         uno::Reference< excel::XRange > xHlinkRange( xHlink->getRange(), uno::UNO_QUERY_THROW );
156*cdf0e10cSrcweir         if( lclContains( rScRanges, xHlinkRange ) )
157*cdf0e10cSrcweir             maHlinks.push_back( xHlink );
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir ScVbaHlinkContainer::~ScVbaHlinkContainer()
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir void ScVbaHlinkContainer::insertHyperlink( const uno::Reference< excel::XHyperlink >& rxHlink ) throw (uno::RuntimeException)
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir     HyperlinkVector::iterator aIt = ::std::find_if( maHlinks.begin(), maHlinks.end(), EqualAnchorFunctor( rxHlink ) );
168*cdf0e10cSrcweir     if( aIt == maHlinks.end() )
169*cdf0e10cSrcweir         maHlinks.push_back( rxHlink );
170*cdf0e10cSrcweir     else
171*cdf0e10cSrcweir         *aIt = rxHlink;
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir sal_Int32 SAL_CALL ScVbaHlinkContainer::getCount() throw (uno::RuntimeException)
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir     return static_cast< sal_Int32 >( maHlinks.size() );
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir uno::Any SAL_CALL ScVbaHlinkContainer::getByIndex( sal_Int32 nIndex )
180*cdf0e10cSrcweir         throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir     if( (0 <= nIndex) && (nIndex < getCount()) )
183*cdf0e10cSrcweir         return uno::Any( maHlinks[ static_cast< size_t >( nIndex ) ] );
184*cdf0e10cSrcweir     throw lang::IndexOutOfBoundsException();
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir uno::Type SAL_CALL ScVbaHlinkContainer::getElementType() throw (uno::RuntimeException)
188*cdf0e10cSrcweir {
189*cdf0e10cSrcweir     return excel::XHyperlink::static_type( 0 );
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir sal_Bool SAL_CALL ScVbaHlinkContainer::hasElements() throw (uno::RuntimeException)
193*cdf0e10cSrcweir {
194*cdf0e10cSrcweir     return !maHlinks.empty();
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir // ============================================================================
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir ScVbaHlinkContainerMember::ScVbaHlinkContainerMember( ScVbaHlinkContainer* pContainer ) :
200*cdf0e10cSrcweir     mxContainer( pContainer )
201*cdf0e10cSrcweir {
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir ScVbaHlinkContainerMember::~ScVbaHlinkContainerMember()
205*cdf0e10cSrcweir {
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir } // namespace detail
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir // ============================================================================
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir ScVbaHyperlinks::ScVbaHyperlinks( const uno::Reference< XHelperInterface >& rxParent,
213*cdf0e10cSrcweir         const uno::Reference< uno::XComponentContext >& rxContext ) throw (uno::RuntimeException) :
214*cdf0e10cSrcweir     detail::ScVbaHlinkContainerMember( new detail::ScVbaHlinkContainer ),
215*cdf0e10cSrcweir     ScVbaHyperlinks_BASE( rxParent, rxContext, uno::Reference< container::XIndexAccess >( mxContainer.get() ) )
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir ScVbaHyperlinks::ScVbaHyperlinks( const uno::Reference< XHelperInterface >& rxParent,
220*cdf0e10cSrcweir         const uno::Reference< uno::XComponentContext >& rxContext,
221*cdf0e10cSrcweir         const ScVbaHyperlinksRef& rxSheetHlinks, const ScRangeList& rScRanges ) throw (uno::RuntimeException) :
222*cdf0e10cSrcweir     detail::ScVbaHlinkContainerMember( new detail::ScVbaHlinkContainer( rxSheetHlinks->mxContainer, rScRanges ) ),
223*cdf0e10cSrcweir     ScVbaHyperlinks_BASE( rxParent, rxContext, uno::Reference< container::XIndexAccess >( mxContainer.get() ) ),
224*cdf0e10cSrcweir     mxSheetHlinks( rxSheetHlinks )
225*cdf0e10cSrcweir {
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir ScVbaHyperlinks::~ScVbaHyperlinks()
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir // XHyperlinks ----------------------------------------------------------------
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir uno::Reference< excel::XHyperlink > SAL_CALL ScVbaHyperlinks::Add(
235*cdf0e10cSrcweir     const uno::Any& rAnchor, const uno::Any& rAddress, const uno::Any& rSubAddress,
236*cdf0e10cSrcweir     const uno::Any& rScreenTip, const uno::Any& rTextToDisplay ) throw (uno::RuntimeException)
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir     /*  If this Hyperlinks object has been craeted from a Range object, the
239*cdf0e10cSrcweir         call to Add() is passed to the Hyperlinks object of the parent
240*cdf0e10cSrcweir         worksheet. This container will not be modified (it will not contain the
241*cdf0e10cSrcweir         inserted hyperlink).
242*cdf0e10cSrcweir         For details, see documentation in hyperlinks.hxx.
243*cdf0e10cSrcweir      */
244*cdf0e10cSrcweir     if( mxSheetHlinks.is() )
245*cdf0e10cSrcweir         return mxSheetHlinks->Add( rAnchor, rAddress, rSubAddress, rScreenTip, rTextToDisplay );
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir     // get anchor object (can be a Range or a Shape object)
248*cdf0e10cSrcweir     uno::Reference< XHelperInterface > xAnchor( rAnchor, uno::UNO_QUERY_THROW );
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir     /*  Create the Hyperlink object, this tries to insert the hyperlink into
251*cdf0e10cSrcweir         the spreadsheet document. Parent of the Hyperlink is the anchor object. */
252*cdf0e10cSrcweir     uno::Reference< excel::XHyperlink > xHlink( new ScVbaHyperlink(
253*cdf0e10cSrcweir         xAnchor, mxContext, rAddress, rSubAddress, rScreenTip, rTextToDisplay ) );
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir     /*  If creation of the hyperlink did not throw, insert it into the
256*cdf0e10cSrcweir         collection. */
257*cdf0e10cSrcweir     mxContainer->insertHyperlink( xHlink );
258*cdf0e10cSrcweir     return xHlink;
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir void SAL_CALL ScVbaHyperlinks::Delete() throw (uno::RuntimeException)
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir     // FIXME not implemented
264*cdf0e10cSrcweir     throw uno::RuntimeException();
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir // XEnumerationAccess ---------------------------------------------------------
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir uno::Reference< container::XEnumeration > SAL_CALL ScVbaHyperlinks::createEnumeration() throw (uno::RuntimeException)
270*cdf0e10cSrcweir {
271*cdf0e10cSrcweir     return new SimpleIndexAccessToEnumeration( m_xIndexAccess );
272*cdf0e10cSrcweir }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir // XElementAccess -------------------------------------------------------------
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir uno::Type SAL_CALL ScVbaHyperlinks::getElementType() throw (uno::RuntimeException)
277*cdf0e10cSrcweir {
278*cdf0e10cSrcweir     return excel::XHyperlink::static_type( 0 );
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir // ScVbaCollectionBase --------------------------------------------------------
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir uno::Any ScVbaHyperlinks::createCollectionObject( const uno::Any& rSource )
284*cdf0e10cSrcweir {
285*cdf0e10cSrcweir     // container stores XHyperlink objects, just return the passed object
286*cdf0e10cSrcweir     return rSource;
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir // XHelperInterface -----------------------------------------------------------
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir VBAHELPER_IMPL_XHELPERINTERFACE( ScVbaHyperlinks, "ooo.vba.excel.Hyperlinks" )
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir // ============================================================================
294