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 <sal/config.h>
21
22 #include "service.hxx"
23 #include "vbahyperlink.hxx"
24 #include <vbahelper/helperdecl.hxx>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/container/XIndexAccess.hpp>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/table/XCellRange.hpp>
29 #include <com/sun/star/text/XText.hpp>
30 #include <com/sun/star/text/XTextFieldsSupplier.hpp>
31 #include <ooo/vba/office/MsoHyperlinkType.hpp>
32 #include <ooo/vba/msforms/XShape.hpp>
33 #include "vbarange.hxx"
34
35 using namespace ::ooo::vba;
36 using namespace ::com::sun::star;
37
ScVbaHyperlink(const uno::Sequence<uno::Any> & rArgs,const uno::Reference<uno::XComponentContext> & rxContext)38 ScVbaHyperlink::ScVbaHyperlink( const uno::Sequence< uno::Any >& rArgs,
39 const uno::Reference< uno::XComponentContext >& rxContext ) :
40 HyperlinkImpl_BASE( getXSomethingFromArgs< XHelperInterface >( rArgs, 0 ), rxContext ),
41 mxCell( getXSomethingFromArgs< table::XCell >( rArgs, 1, false ) ),
42 mnType( office::MsoHyperlinkType::msoHyperlinkRange )
43 {
44 uno::Reference< text::XTextFieldsSupplier > xTextFields( mxCell, uno::UNO_QUERY_THROW );
45 uno::Reference< container::XIndexAccess > xIndex( xTextFields->getTextFields(), uno::UNO_QUERY_THROW );
46 mxTextField.set( xIndex->getByIndex(0), uno::UNO_QUERY_THROW );
47 }
48
ScVbaHyperlink(const uno::Reference<XHelperInterface> & rxAnchor,const uno::Reference<uno::XComponentContext> & rxContext,const uno::Any & rAddress,const uno::Any & rSubAddress,const uno::Any & rScreenTip,const uno::Any & rTextToDisplay)49 ScVbaHyperlink::ScVbaHyperlink( const uno::Reference< XHelperInterface >& rxAnchor,
50 const uno::Reference< uno::XComponentContext >& rxContext,
51 const uno::Any& rAddress, const uno::Any& rSubAddress,
52 const uno::Any& rScreenTip, const uno::Any& rTextToDisplay ) :
53 HyperlinkImpl_BASE( rxAnchor, rxContext ) // parent of Hyperlink is the anchor object
54 {
55 // extract parameters, Address must not be empty
56 UrlComponents aUrlComp;
57 OUString aTextToDisplay;
58 if( !(rAddress >>= aUrlComp.first) || aUrlComp.first.isEmpty() )
59 throw uno::RuntimeException("Cannot get address" );
60 rSubAddress >>= aUrlComp.second;
61 rScreenTip >>= maScreenTip;
62 rTextToDisplay >>= aTextToDisplay;
63
64 // get anchor range or anchor shape
65 uno::Reference< excel::XRange > xAnchorRange( rxAnchor, uno::UNO_QUERY );
66 if( xAnchorRange.is() )
67 {
68 mnType = office::MsoHyperlinkType::msoHyperlinkRange;
69 // only single ranges are allowed
70 uno::Reference< table::XCellRange > xUnoRange( ScVbaRange::getCellRange( xAnchorRange ), uno::UNO_QUERY_THROW );
71 // insert the hyperlink into the top-left cell only
72 mxCell.set( xUnoRange->getCellByPosition( 0, 0 ), uno::UNO_SET_THROW );
73 uno::Reference< text::XText > xText( mxCell, uno::UNO_QUERY_THROW );
74 // use cell text or URL if no TextToDisplay has been passed
75 if( aTextToDisplay.isEmpty() )
76 {
77 aTextToDisplay = xText->getString();
78 if( aTextToDisplay.isEmpty() )
79 {
80 OUStringBuffer aBuffer( aUrlComp.first );
81 if( !aUrlComp.second.isEmpty() )
82 aBuffer.append( " - " ).append( aUrlComp.second );
83 aTextToDisplay = aBuffer.makeStringAndClear();
84 }
85 }
86 // create and initialize a new URL text field
87 uno::Reference< lang::XMultiServiceFactory > xFactory( ScVbaRange::getUnoModel( xAnchorRange ), uno::UNO_QUERY_THROW );
88 uno::Reference< text::XTextContent > xUrlField( xFactory->createInstance("com.sun.star.text.TextField.URL"), uno::UNO_QUERY_THROW );
89 mxTextField.set( xUrlField, uno::UNO_QUERY_THROW );
90 setUrlComponents( aUrlComp );
91 setTextToDisplay( aTextToDisplay );
92 // insert the text field into the document
93 xText->setString( OUString() );
94 uno::Reference< text::XTextRange > xRange( xText->createTextCursor(), uno::UNO_QUERY_THROW );
95 xText->insertTextContent( xRange, xUrlField, false );
96 }
97 else
98 {
99 uno::Reference< msforms::XShape > xAnchorShape( rxAnchor, uno::UNO_QUERY_THROW );
100 mnType = office::MsoHyperlinkType::msoHyperlinkShape;
101 // FIXME: insert hyperlink into shape
102 throw uno::RuntimeException();
103 }
104 }
105
~ScVbaHyperlink()106 ScVbaHyperlink::~ScVbaHyperlink()
107 {
108 }
109
getName()110 OUString ScVbaHyperlink::getName()
111 {
112 // it seems this attribute is same as TextToDisplay
113 return getTextToDisplay();
114 }
115
setName(const OUString & rName)116 void ScVbaHyperlink::setName( const OUString& rName )
117 {
118 setTextToDisplay( rName );
119 }
120
getAddress()121 OUString ScVbaHyperlink::getAddress()
122 {
123 return getUrlComponents().first;
124 }
125
setAddress(const OUString & rAddress)126 void ScVbaHyperlink::setAddress( const OUString& rAddress )
127 {
128 UrlComponents aUrlComp = getUrlComponents();
129 aUrlComp.first = rAddress;
130 setUrlComponents( aUrlComp );
131 }
132
getSubAddress()133 OUString ScVbaHyperlink::getSubAddress()
134 {
135 return getUrlComponents().second;
136 }
137
setSubAddress(const OUString & rSubAddress)138 void ScVbaHyperlink::setSubAddress( const OUString& rSubAddress )
139 {
140 UrlComponents aUrlComp = getUrlComponents();
141 aUrlComp.second = rSubAddress;
142 setUrlComponents( aUrlComp );
143 }
144
getScreenTip()145 OUString SAL_CALL ScVbaHyperlink::getScreenTip()
146 {
147 return maScreenTip;
148 }
149
setScreenTip(const OUString & rScreenTip)150 void SAL_CALL ScVbaHyperlink::setScreenTip( const OUString& rScreenTip )
151 {
152 maScreenTip = rScreenTip;
153 }
154
getTextToDisplay()155 OUString ScVbaHyperlink::getTextToDisplay()
156 {
157 ensureTextField();
158 OUString aTextToDisplay;
159 mxTextField->getPropertyValue("Representation") >>= aTextToDisplay;
160 return aTextToDisplay;
161 }
162
setTextToDisplay(const OUString & rTextToDisplay)163 void ScVbaHyperlink::setTextToDisplay( const OUString& rTextToDisplay )
164 {
165 ensureTextField();
166 mxTextField->setPropertyValue("Representation", uno::Any( rTextToDisplay ) );
167 }
168
getType()169 sal_Int32 SAL_CALL ScVbaHyperlink::getType()
170 {
171 return mnType;
172 }
173
getRange()174 uno::Reference< excel::XRange > SAL_CALL ScVbaHyperlink::getRange()
175 {
176 if( mnType == office::MsoHyperlinkType::msoHyperlinkRange )
177 {
178 // if constructed from Hyperlinks object, range has been passed as parent
179 uno::Reference< excel::XRange > xAnchorRange( getParent(), uno::UNO_QUERY );
180 if( !xAnchorRange.is() )
181 {
182 // if constructed via service c'tor, create new range based on cell
183 uno::Reference< table::XCellRange > xRange( mxCell, uno::UNO_QUERY_THROW );
184 // FIXME: need to pass current worksheet as the parent of XRange.
185 xAnchorRange.set( new ScVbaRange( uno::Reference< XHelperInterface >(), mxContext, xRange ) );
186 }
187 return xAnchorRange;
188 }
189 // error if called at a shape Hyperlink object
190 throw uno::RuntimeException();
191 }
192
getShape()193 uno::Reference< msforms::XShape > SAL_CALL ScVbaHyperlink::getShape()
194 {
195 // error if called at a range Hyperlink object
196 return uno::Reference< msforms::XShape >( getParent(), uno::UNO_QUERY_THROW );
197 }
198
199 VBAHELPER_IMPL_XHELPERINTERFACE( ScVbaHyperlink, "ooo.vba.excel.Hyperlink" )
200
201 // private --------------------------------------------------------------------
202
ensureTextField()203 void ScVbaHyperlink::ensureTextField()
204 {
205 if( !mxTextField.is() )
206 throw uno::RuntimeException();
207 }
208
getUrlComponents()209 ScVbaHyperlink::UrlComponents ScVbaHyperlink::getUrlComponents()
210 {
211 ensureTextField();
212 OUString aUrl;
213 mxTextField->getPropertyValue("URL") >>= aUrl;
214 sal_Int32 nHashPos = aUrl.indexOf( '#' );
215 if( nHashPos < 0 )
216 return UrlComponents( aUrl, OUString() );
217 return UrlComponents( aUrl.copy( 0, nHashPos ), aUrl.copy( nHashPos + 1 ) );
218 }
219
setUrlComponents(const UrlComponents & rUrlComp)220 void ScVbaHyperlink::setUrlComponents( const UrlComponents& rUrlComp )
221 {
222 ensureTextField();
223 OUStringBuffer aUrl( rUrlComp.first );
224 if( !rUrlComp.second.isEmpty() )
225 aUrl.append( '#' ).append( rUrlComp.second );
226 mxTextField->setPropertyValue("URL", uno::Any( aUrl.makeStringAndClear() ) );
227 }
228
229 namespace hyperlink
230 {
231 namespace sdecl = comphelper::service_decl;
232 sdecl::vba_service_class_<ScVbaHyperlink, sdecl::with_args<true> > const serviceImpl;
233 sdecl::ServiceDecl const serviceDecl(
234 serviceImpl,
235 "ScVbaHyperlink",
236 "ooo.vba.excel.Hyperlink" );
237 }
238
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
240