Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "vbahyperlink.hxx"
30 : : #include <vbahelper/helperdecl.hxx>
31 : : #include <com/sun/star/container/XIndexAccess.hpp>
32 : : #include <com/sun/star/text/XText.hpp>
33 : : #include <com/sun/star/text/XTextFieldsSupplier.hpp>
34 : : #include <com/sun/star/container/XEnumerationAccess.hpp>
35 : : #include <ooo/vba/office/MsoHyperlinkType.hpp>
36 : : #include <ooo/vba/msforms/XShape.hpp>
37 : : #include "vbarange.hxx"
38 : :
39 : : using namespace ::ooo::vba;
40 : : using namespace ::com::sun::star;
41 : : using ::rtl::OUString;
42 : : using ::rtl::OUStringBuffer;
43 : :
44 : : // ============================================================================
45 : :
46 : 0 : ScVbaHyperlink::ScVbaHyperlink( const uno::Sequence< uno::Any >& rArgs,
47 : : const uno::Reference< uno::XComponentContext >& rxContext ) throw (lang::IllegalArgumentException) :
48 : : HyperlinkImpl_BASE( getXSomethingFromArgs< XHelperInterface >( rArgs, 0 ), rxContext ),
49 : : mxCell( getXSomethingFromArgs< table::XCell >( rArgs, 1, false ) ),
50 [ # # ][ # # ]: 0 : mnType( office::MsoHyperlinkType::msoHyperlinkRange )
51 : : {
52 [ # # ]: 0 : uno::Reference< text::XTextFieldsSupplier > xTextFields( mxCell, uno::UNO_QUERY_THROW );
53 [ # # ][ # # ]: 0 : uno::Reference< container::XIndexAccess > xIndex( xTextFields->getTextFields(), uno::UNO_QUERY_THROW );
[ # # ]
54 [ # # ][ # # ]: 0 : mxTextField.set( xIndex->getByIndex(0), uno::UNO_QUERY_THROW );
[ # # ]
55 : 0 : }
56 : :
57 : 0 : ScVbaHyperlink::ScVbaHyperlink( const uno::Reference< XHelperInterface >& rxAnchor,
58 : : const uno::Reference< uno::XComponentContext >& rxContext,
59 : : const uno::Any& rAddress, const uno::Any& rSubAddress,
60 : : const uno::Any& rScreenTip, const uno::Any& rTextToDisplay ) throw (uno::RuntimeException) :
61 : 0 : HyperlinkImpl_BASE( rxAnchor, rxContext ) // parent of Hyperlink is the anchor object
62 : : {
63 : : // extract parameters, Address must not be empty
64 : 0 : UrlComponents aUrlComp;
65 : 0 : OUString aTextToDisplay;
66 [ # # ][ # # ]: 0 : if( !(rAddress >>= aUrlComp.first) || aUrlComp.first.isEmpty() )
[ # # ]
67 [ # # ][ # # ]: 0 : throw uno::RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM( "Cannot get address" ) ), uno::Reference< uno::XInterface >() );
68 : 0 : rSubAddress >>= aUrlComp.second;
69 : 0 : rScreenTip >>= maScreenTip;
70 : 0 : rTextToDisplay >>= aTextToDisplay;
71 : :
72 : : // get anchor range or anchor shape
73 [ # # ]: 0 : uno::Reference< excel::XRange > xAnchorRange( rxAnchor, uno::UNO_QUERY );
74 [ # # ]: 0 : if( xAnchorRange.is() )
75 : : {
76 : 0 : mnType = office::MsoHyperlinkType::msoHyperlinkRange;
77 : : // only single ranges are allowed
78 [ # # ][ # # ]: 0 : uno::Reference< table::XCellRange > xUnoRange( ScVbaRange::getCellRange( xAnchorRange ), uno::UNO_QUERY_THROW );
79 : : // insert the hyperlink into the top-left cell only
80 [ # # ][ # # ]: 0 : mxCell.set( xUnoRange->getCellByPosition( 0, 0 ), uno::UNO_SET_THROW );
[ # # ]
81 [ # # ]: 0 : uno::Reference< text::XText > xText( mxCell, uno::UNO_QUERY_THROW );
82 : : // use cell text or URL if no TextToDisplay has been passed
83 [ # # ]: 0 : if( aTextToDisplay.isEmpty() )
84 : : {
85 [ # # ][ # # ]: 0 : aTextToDisplay = xText->getString();
86 [ # # ]: 0 : if( aTextToDisplay.isEmpty() )
87 : : {
88 [ # # ]: 0 : OUStringBuffer aBuffer( aUrlComp.first );
89 [ # # ]: 0 : if( !aUrlComp.second.isEmpty() )
90 [ # # ][ # # ]: 0 : aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " - " ) ).append( aUrlComp.second );
91 [ # # ]: 0 : aTextToDisplay = aBuffer.makeStringAndClear();
92 : : }
93 : : }
94 : : // create and initialize a new URL text field
95 [ # # ][ # # ]: 0 : uno::Reference< lang::XMultiServiceFactory > xFactory( ScVbaRange::getUnoModel( xAnchorRange ), uno::UNO_QUERY_THROW );
96 [ # # ][ # # ]: 0 : uno::Reference< text::XTextContent > xUrlField( xFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.text.TextField.URL" ) ) ), uno::UNO_QUERY_THROW );
[ # # ][ # # ]
97 [ # # ]: 0 : mxTextField.set( xUrlField, uno::UNO_QUERY_THROW );
98 [ # # ]: 0 : setUrlComponents( aUrlComp );
99 [ # # ]: 0 : setTextToDisplay( aTextToDisplay );
100 : : // insert the text field into the document
101 [ # # ][ # # ]: 0 : xText->setString( OUString() );
102 [ # # ][ # # ]: 0 : uno::Reference< text::XTextRange > xRange( xText->createTextCursor(), uno::UNO_QUERY_THROW );
[ # # ]
103 [ # # ][ # # ]: 0 : xText->insertTextContent( xRange, xUrlField, false );
104 : : }
105 : : else
106 : : {
107 [ # # ]: 0 : uno::Reference< msforms::XShape > xAnchorShape( rxAnchor, uno::UNO_QUERY_THROW );
108 : 0 : mnType = office::MsoHyperlinkType::msoHyperlinkShape;
109 : : // FIXME: insert hyperlink into shape
110 [ # # ]: 0 : throw uno::RuntimeException();
111 : 0 : }
112 : 0 : }
113 : :
114 : 0 : ScVbaHyperlink::~ScVbaHyperlink()
115 : : {
116 [ # # ]: 0 : }
117 : :
118 : 0 : OUString ScVbaHyperlink::getName() throw (uno::RuntimeException)
119 : : {
120 : : // it seems this attribute is same as TextToDisplay
121 : 0 : return getTextToDisplay();
122 : : }
123 : :
124 : 0 : void ScVbaHyperlink::setName( const OUString& rName ) throw (uno::RuntimeException)
125 : : {
126 : 0 : setTextToDisplay( rName );
127 : 0 : }
128 : :
129 : 0 : OUString ScVbaHyperlink::getAddress() throw (uno::RuntimeException)
130 : : {
131 : 0 : return getUrlComponents().first;
132 : : }
133 : :
134 : 0 : void ScVbaHyperlink::setAddress( const OUString& rAddress ) throw (uno::RuntimeException)
135 : : {
136 [ # # ]: 0 : UrlComponents aUrlComp = getUrlComponents();
137 : 0 : aUrlComp.first = rAddress;
138 [ # # ]: 0 : setUrlComponents( aUrlComp );
139 : 0 : }
140 : :
141 : 0 : OUString ScVbaHyperlink::getSubAddress() throw (uno::RuntimeException)
142 : : {
143 : 0 : return getUrlComponents().second;
144 : : }
145 : :
146 : 0 : void ScVbaHyperlink::setSubAddress( const OUString& rSubAddress ) throw (uno::RuntimeException)
147 : : {
148 [ # # ]: 0 : UrlComponents aUrlComp = getUrlComponents();
149 : 0 : aUrlComp.second = rSubAddress;
150 [ # # ]: 0 : setUrlComponents( aUrlComp );
151 : 0 : }
152 : :
153 : 0 : OUString SAL_CALL ScVbaHyperlink::getScreenTip() throw (uno::RuntimeException)
154 : : {
155 : 0 : return maScreenTip;
156 : : }
157 : :
158 : 0 : void SAL_CALL ScVbaHyperlink::setScreenTip( const OUString& rScreenTip ) throw (uno::RuntimeException)
159 : : {
160 : 0 : maScreenTip = rScreenTip;
161 : 0 : }
162 : :
163 : 0 : OUString ScVbaHyperlink::getTextToDisplay() throw (uno::RuntimeException)
164 : : {
165 : 0 : ensureTextField();
166 : 0 : OUString aTextToDisplay;
167 [ # # ][ # # ]: 0 : mxTextField->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Representation" ) ) ) >>= aTextToDisplay;
[ # # ]
168 : 0 : return aTextToDisplay;
169 : : }
170 : :
171 : 0 : void ScVbaHyperlink::setTextToDisplay( const OUString& rTextToDisplay ) throw (uno::RuntimeException)
172 : : {
173 : 0 : ensureTextField();
174 [ # # ][ # # ]: 0 : mxTextField->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Representation" ) ), uno::Any( rTextToDisplay ) );
175 : 0 : }
176 : :
177 : 0 : sal_Int32 SAL_CALL ScVbaHyperlink::getType() throw (uno::RuntimeException)
178 : : {
179 : 0 : return mnType;
180 : : }
181 : :
182 : 0 : uno::Reference< excel::XRange > SAL_CALL ScVbaHyperlink::getRange() throw (uno::RuntimeException)
183 : : {
184 [ # # ]: 0 : if( mnType == office::MsoHyperlinkType::msoHyperlinkRange )
185 : : {
186 : : // if constructed from Hyperlinks object, range has been passed as parent
187 [ # # ][ # # ]: 0 : uno::Reference< excel::XRange > xAnchorRange( getParent(), uno::UNO_QUERY );
188 [ # # ]: 0 : if( !xAnchorRange.is() )
189 : : {
190 : : // if constructed via service c'tor, create new range based on cell
191 [ # # ]: 0 : uno::Reference< table::XCellRange > xRange( mxCell, uno::UNO_QUERY_THROW );
192 : : // FIXME: need to pass current worksheet as the parent of XRange.
193 [ # # ][ # # ]: 0 : xAnchorRange.set( new ScVbaRange( uno::Reference< XHelperInterface >(), mxContext, xRange ) );
[ # # ]
194 : : }
195 : 0 : return xAnchorRange;
196 : : }
197 : : // error if called at a shape Hyperlink object
198 [ # # ]: 0 : throw uno::RuntimeException();
199 : : }
200 : :
201 : 0 : uno::Reference< msforms::XShape > SAL_CALL ScVbaHyperlink::getShape() throw (uno::RuntimeException)
202 : : {
203 : : // error if called at a range Hyperlink object
204 [ # # ]: 0 : return uno::Reference< msforms::XShape >( getParent(), uno::UNO_QUERY_THROW );
205 : : }
206 : :
207 [ # # ][ # # ]: 0 : VBAHELPER_IMPL_XHELPERINTERFACE( ScVbaHyperlink, "ooo.vba.excel.Hyperlink" )
[ # # ][ # # ]
[ # # ][ # # ]
208 : :
209 : : // private --------------------------------------------------------------------
210 : :
211 : 0 : void ScVbaHyperlink::ensureTextField() throw (uno::RuntimeException)
212 : : {
213 [ # # ]: 0 : if( !mxTextField.is() )
214 [ # # ]: 0 : throw uno::RuntimeException();
215 : 0 : }
216 : :
217 : 0 : ScVbaHyperlink::UrlComponents ScVbaHyperlink::getUrlComponents() throw (uno::RuntimeException)
218 : : {
219 [ # # ]: 0 : ensureTextField();
220 : 0 : OUString aUrl;
221 [ # # ][ # # ]: 0 : mxTextField->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) ) ) >>= aUrl;
[ # # ]
222 : 0 : sal_Int32 nHashPos = aUrl.indexOf( '#' );
223 [ # # ]: 0 : if( nHashPos < 0 )
224 : 0 : return UrlComponents( aUrl, OUString() );
225 : 0 : return UrlComponents( aUrl.copy( 0, nHashPos ), aUrl.copy( nHashPos + 1 ) );
226 : : }
227 : :
228 : 0 : void ScVbaHyperlink::setUrlComponents( const UrlComponents& rUrlComp ) throw (uno::RuntimeException)
229 : : {
230 [ # # ]: 0 : ensureTextField();
231 [ # # ]: 0 : OUStringBuffer aUrl( rUrlComp.first );
232 [ # # ]: 0 : if( !rUrlComp.second.isEmpty() )
233 [ # # ][ # # ]: 0 : aUrl.append( sal_Unicode( '#' ) ).append( rUrlComp.second );
234 [ # # ][ # # ]: 0 : mxTextField->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) ), uno::Any( aUrl.makeStringAndClear() ) );
[ # # ][ # # ]
[ # # ]
235 : 0 : }
236 : :
237 : : namespace hyperlink
238 : : {
239 : : namespace sdecl = comphelper::service_decl;
240 : 3 : sdecl::vba_service_class_<ScVbaHyperlink, sdecl::with_args<true> > serviceImpl;
241 : 3 : extern sdecl::ServiceDecl const serviceDecl(
242 : : serviceImpl,
243 : : "ScVbaHyperlink",
244 : : "ooo.vba.excel.Hyperlink" );
245 [ + - ][ + - ]: 9 : }
246 : :
247 : : // ============================================================================
248 : :
249 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|