1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/accessiblekeybindinghelper.hxx>
21 #include <comphelper/processfactory.hxx>
22 #include <com/sun/star/frame/XDesktop.hpp>
23 #include <com/sun/star/document/XLinkTargetSupplier.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
27 #include <vcl/keycodes.hxx>
28 #include <vcl/svapp.hxx>
29 #include <txtinet.hxx>
30 #include "accpara.hxx"
31 #include "acchyperlink.hxx"
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::accessibility
;
35 using ::com::sun::star::lang::IndexOutOfBoundsException
;
37 SwAccessibleHyperlink::SwAccessibleHyperlink(const SwTextAttr
& rTextAttr
,
38 SwAccessibleParagraph
& rAccPara
,
39 sal_Int32
const nStt
, sal_Int32
const nEnd
)
40 : m_pHyperlink(const_cast<SwFormatINetFormat
*>(&rTextAttr
.GetINetFormat()))
41 , m_xParagraph(&rAccPara
)
42 , m_nStartIndex( nStt
)
45 StartListening(m_pHyperlink
->GetNotifier());
48 SwAccessibleHyperlink::~SwAccessibleHyperlink()
50 Invalidate(); // with SolarMutex!
53 // when the pool item dies, invalidate! this is the only reason for Listener...
54 void SwAccessibleHyperlink::Notify(SfxHint
const& rHint
)
56 if (rHint
.GetId() == SfxHintId::Dying
)
62 // both the parent SwAccessibleParagraph and the pool-item must be valid
63 const SwFormatINetFormat
*SwAccessibleHyperlink::GetTextAttr() const
65 return (m_xParagraph
.is() && m_xParagraph
->GetMap())
71 sal_Int32 SAL_CALL
SwAccessibleHyperlink::getAccessibleActionCount()
73 return isValid() ? 1 : 0;
76 sal_Bool SAL_CALL
SwAccessibleHyperlink::doAccessibleAction( sal_Int32 nIndex
)
78 SolarMutexGuard aGuard
;
83 throw lang::IndexOutOfBoundsException();
84 SwFormatINetFormat
const*const pINetFormat
= GetTextAttr();
85 if (pINetFormat
&& !pINetFormat
->GetValue().isEmpty())
87 SwViewShell
*pVSh
= m_xParagraph
->GetShell();
90 LoadURL(*pVSh
, pINetFormat
->GetValue(), LoadUrlFlags::NONE
,
91 pINetFormat
->GetTargetFrame());
92 const SwTextINetFormat
*const pTextAttr
= pINetFormat
->GetTextINetFormat();
95 const_cast<SwTextINetFormat
*>(pTextAttr
)->SetVisited(true);
96 const_cast<SwTextINetFormat
*>(pTextAttr
)->SetVisitedValid(true);
105 OUString SAL_CALL
SwAccessibleHyperlink::getAccessibleActionDescription(
109 throw lang::IndexOutOfBoundsException();
112 if (SwFormatINetFormat
const*const pINetFormat
= GetTextAttr())
114 return pINetFormat
->GetValue();
120 uno::Reference
< XAccessibleKeyBinding
> SAL_CALL
121 SwAccessibleHyperlink::getAccessibleActionKeyBinding( sal_Int32
)
123 uno::Reference
< XAccessibleKeyBinding
> xKeyBinding
;
127 rtl::Reference
<::comphelper::OAccessibleKeyBindingHelper
> pKeyBindingHelper
=
128 new ::comphelper::OAccessibleKeyBindingHelper();
129 xKeyBinding
= pKeyBindingHelper
;
131 awt::KeyStroke aKeyStroke
;
132 aKeyStroke
.Modifiers
= 0;
133 aKeyStroke
.KeyCode
= KEY_RETURN
;
134 aKeyStroke
.KeyChar
= 0;
135 aKeyStroke
.KeyFunc
= 0;
136 pKeyBindingHelper
->AddKeyBinding( aKeyStroke
);
142 // XAccessibleHyperlink
143 uno::Any SAL_CALL
SwAccessibleHyperlink::getAccessibleActionAnchor(
150 throw lang::IndexOutOfBoundsException();
151 OUString
text( m_xParagraph
->GetString() );
152 OUString retText
= text
.copy(m_nStartIndex
, m_nEndIndex
- m_nStartIndex
);
157 uno::Any SAL_CALL
SwAccessibleHyperlink::getAccessibleActionObject(
163 throw lang::IndexOutOfBoundsException();
165 if (SwFormatINetFormat
const*const pINetFormat
= GetTextAttr())
167 retText
= pINetFormat
->GetValue();
174 sal_Int32 SAL_CALL
SwAccessibleHyperlink::getStartIndex()
176 return m_nStartIndex
;
179 sal_Int32 SAL_CALL
SwAccessibleHyperlink::getEndIndex()
184 sal_Bool SAL_CALL
SwAccessibleHyperlink::isValid( )
186 SolarMutexGuard aGuard
;
187 if (m_xParagraph
.is())
189 if (SwFormatINetFormat
const*const pINetFormat
= GetTextAttr())
191 OUString
const sText(pINetFormat
->GetValue());
192 sal_Int32 nPos
= sText
.indexOf("#");
193 if (nPos
==0)//document link
195 uno::Reference
< lang::XMultiServiceFactory
> xFactory( ::comphelper::getProcessServiceFactory() );
196 if( ! xFactory
.is() )
198 uno::Reference
< css::frame::XDesktop
> xDesktop( xFactory
->createInstance( "com.sun.star.frame.Desktop" ),
202 uno::Reference
< lang::XComponent
> xComp
= xDesktop
->getCurrentComponent();
205 uno::Reference
< css::document::XLinkTargetSupplier
> xLTS(xComp
, uno::UNO_QUERY
);
209 uno::Reference
< css::container::XNameAccess
> xLinks
= xLTS
->getLinks();
210 uno::Reference
< css::container::XNameAccess
> xSubLinks
;
211 const uno::Sequence
< OUString
> aNames( xLinks
->getElementNames() );
213 for( const OUString
& aLink
: aNames
)
215 uno::Any aAny
= xLinks
->getByName( aLink
);
217 if (xSubLinks
->hasByName(sText
.copy(1)) )
228 void SwAccessibleHyperlink::Invalidate()
230 SolarMutexGuard aGuard
;
231 m_xParagraph
= nullptr;
232 m_pHyperlink
= nullptr;
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */