tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / accessibility / source / standard / vclxaccessiblelistitem.cxx
blob0a9d2bee1806059eeb642a703dfc8ac42520f58c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <standard/vclxaccessiblelistitem.hxx>
21 #include <helper/IComboListBoxHelper.hxx>
22 #include <com/sun/star/awt/Rectangle.hpp>
24 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
25 #include <com/sun/star/accessibility/AccessibleRole.hpp>
26 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
27 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
28 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
29 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
30 #include <cppuhelper/supportsservice.hxx>
31 #include <utility>
32 #include <vcl/svapp.hxx>
33 #include <vcl/toolkit/lstbox.hxx>
34 #include <vcl/unohelp.hxx>
35 #include <vcl/unohelp2.hxx>
36 #include <vcl/settings.hxx>
37 #include <unotools/accessiblerelationsethelper.hxx>
38 #include <comphelper/accessibleeventnotifier.hxx>
39 #include <i18nlangtag/languagetag.hxx>
41 namespace
43 /// @throws css::lang::IndexOutOfBoundsException
44 void checkIndex_Impl( sal_Int32 _nIndex, std::u16string_view _sText )
46 if ( _nIndex < 0 || _nIndex > static_cast<sal_Int32>(_sText.size()) )
47 throw css::lang::IndexOutOfBoundsException();
51 // class VCLXAccessibleListItem ------------------------------------------
53 using namespace ::com::sun::star::accessibility;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::beans;
56 using namespace ::com::sun::star::lang;
57 using namespace ::com::sun::star;
60 // Ctor() and Dtor()
62 VCLXAccessibleListItem::VCLXAccessibleListItem(sal_Int32 _nIndexInParent, rtl::Reference< VCLXAccessibleList > _xParent)
63 : m_nIndexInParent(_nIndexInParent)
64 , m_bSelected(false)
65 , m_bVisible(false)
66 , m_nClientId(0)
67 , m_xParent(std::move(_xParent))
69 assert(m_xParent.is());
70 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent->getListBoxHelper();
71 if (pListBoxHelper)
72 m_sEntryText = pListBoxHelper->GetEntry(static_cast<sal_uInt16>(_nIndexInParent));
75 void VCLXAccessibleListItem::SetSelected( bool _bSelected )
77 if ( m_bSelected != _bSelected )
79 Any aOldValue;
80 Any aNewValue;
81 if ( m_bSelected )
82 aOldValue <<= AccessibleStateType::SELECTED;
83 else
84 aNewValue <<= AccessibleStateType::SELECTED;
85 m_bSelected = _bSelected;
86 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
90 void VCLXAccessibleListItem::SetVisible( bool _bVisible )
92 if ( m_bVisible != _bVisible )
94 Any aOldValue, aNewValue;
95 m_bVisible = _bVisible;
96 (_bVisible ? aNewValue : aOldValue ) <<= AccessibleStateType::VISIBLE;
97 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
98 (_bVisible ? aNewValue : aOldValue ) <<= AccessibleStateType::SHOWING;
99 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
103 void VCLXAccessibleListItem::NotifyAccessibleEvent( sal_Int16 _nEventId,
104 const css::uno::Any& _aOldValue,
105 const css::uno::Any& _aNewValue )
107 AccessibleEventObject aEvt;
108 aEvt.Source = *this;
109 aEvt.EventId = _nEventId;
110 aEvt.OldValue = _aOldValue;
111 aEvt.NewValue = _aNewValue;
113 if (m_nClientId)
114 comphelper::AccessibleEventNotifier::addEvent( m_nClientId, aEvt );
117 // OCommonAccessibleText
119 OUString VCLXAccessibleListItem::implGetText()
121 return m_sEntryText;
124 Locale VCLXAccessibleListItem::implGetLocale()
126 return Application::GetSettings().GetLanguageTag().getLocale();
129 void VCLXAccessibleListItem::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
131 nStartIndex = 0;
132 nEndIndex = 0;
135 // XTypeProvider
137 Sequence< sal_Int8 > VCLXAccessibleListItem::getImplementationId()
139 return css::uno::Sequence<sal_Int8>();
142 // XComponent
144 void VCLXAccessibleListItem::disposing(std::unique_lock<std::mutex>& rGuard)
146 VCLXAccessibleListItem_BASE::disposing(rGuard);
148 m_sEntryText.clear();
149 m_xParent = nullptr;
150 comphelper::AccessibleEventNotifier::TClientId nId = m_nClientId;
151 m_nClientId = 0;
152 Reference< XInterface > xEventSource;
153 if ( nId )
155 xEventSource = *this;
157 // Send a disposing to all listeners.
158 rGuard.unlock();
159 comphelper::AccessibleEventNotifier::revokeClientNotifyDisposing( nId, *this );
160 rGuard.lock();
164 // XServiceInfo
166 OUString VCLXAccessibleListItem::getImplementationName()
168 return u"com.sun.star.comp.toolkit.AccessibleListItem"_ustr;
171 sal_Bool VCLXAccessibleListItem::supportsService( const OUString& rServiceName )
173 return cppu::supportsService(this, rServiceName);
176 Sequence< OUString > VCLXAccessibleListItem::getSupportedServiceNames()
178 return {u"com.sun.star.accessibility.AccessibleContext"_ustr,
179 u"com.sun.star.accessibility.AccessibleComponent"_ustr,
180 u"com.sun.star.accessibility.AccessibleListItem"_ustr};
183 // XAccessible
185 Reference< XAccessibleContext > SAL_CALL VCLXAccessibleListItem::getAccessibleContext( )
187 return this;
190 // XAccessibleContext
192 sal_Int64 SAL_CALL VCLXAccessibleListItem::getAccessibleChildCount( )
194 return 0;
197 Reference< XAccessible > SAL_CALL VCLXAccessibleListItem::getAccessibleChild( sal_Int64 )
199 return Reference< XAccessible >();
202 Reference< XAccessible > SAL_CALL VCLXAccessibleListItem::getAccessibleParent( )
204 std::unique_lock aGuard( m_aMutex );
206 return m_xParent;
209 sal_Int64 SAL_CALL VCLXAccessibleListItem::getAccessibleIndexInParent( )
211 return m_nIndexInParent;
214 sal_Int16 SAL_CALL VCLXAccessibleListItem::getAccessibleRole( )
216 return AccessibleRole::LIST_ITEM;
217 // return AccessibleRole::LABEL;
220 OUString SAL_CALL VCLXAccessibleListItem::getAccessibleDescription( )
222 // no description for every item
223 return OUString();
226 OUString SAL_CALL VCLXAccessibleListItem::getAccessibleName( )
228 std::unique_lock aGuard( m_aMutex );
230 // entry text == accessible name
231 return m_sEntryText;
234 Reference< XAccessibleRelationSet > SAL_CALL VCLXAccessibleListItem::getAccessibleRelationSet( )
236 return new utl::AccessibleRelationSetHelper;
239 sal_Int64 SAL_CALL VCLXAccessibleListItem::getAccessibleStateSet( )
241 std::unique_lock aGuard( m_aMutex );
243 sal_Int64 nStateSet = 0;
245 if ( !m_bDisposed )
247 nStateSet |= AccessibleStateType::TRANSIENT;
249 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
250 if (pListBoxHelper && pListBoxHelper->IsEnabled())
252 nStateSet |= AccessibleStateType::SELECTABLE;
253 nStateSet |= AccessibleStateType::ENABLED;
254 nStateSet |= AccessibleStateType::SENSITIVE;
257 if ( m_bSelected )
258 nStateSet |= AccessibleStateType::SELECTED;
259 if ( m_bVisible )
261 nStateSet |= AccessibleStateType::VISIBLE;
262 nStateSet |= AccessibleStateType::SHOWING;
265 else
266 nStateSet |= AccessibleStateType::DEFUNC;
268 return nStateSet;
271 Locale SAL_CALL VCLXAccessibleListItem::getLocale( )
273 SolarMutexGuard aSolarGuard;
274 std::unique_lock aGuard( m_aMutex );
276 return implGetLocale();
279 // XAccessibleComponent
281 sal_Bool SAL_CALL VCLXAccessibleListItem::containsPoint( const awt::Point& _aPoint )
283 SolarMutexGuard aSolarGuard;
284 std::unique_lock aGuard( m_aMutex );
286 bool bInside = false;
287 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
288 if (pListBoxHelper)
290 tools::Rectangle aRect(pListBoxHelper->GetBoundingRectangle(static_cast<sal_uInt16>(m_nIndexInParent)));
291 aRect.Move(-aRect.Left(), -aRect.Top());
292 bInside = aRect.Contains(vcl::unohelper::ConvertToVCLPoint(_aPoint));
294 return bInside;
297 Reference< XAccessible > SAL_CALL VCLXAccessibleListItem::getAccessibleAtPoint( const awt::Point& )
299 return Reference< XAccessible >();
302 awt::Rectangle SAL_CALL VCLXAccessibleListItem::getBounds( )
304 SolarMutexGuard aSolarGuard;
305 std::unique_lock aGuard( m_aMutex );
307 awt::Rectangle aRect;
308 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
309 if (pListBoxHelper)
310 aRect = vcl::unohelper::ConvertToAWTRect(
311 pListBoxHelper->GetBoundingRectangle(static_cast<sal_uInt16>(m_nIndexInParent)));
313 return aRect;
316 awt::Point SAL_CALL VCLXAccessibleListItem::getLocation( )
318 SolarMutexGuard aSolarGuard;
319 std::unique_lock aGuard( m_aMutex );
321 Point aPoint(0,0);
322 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
323 if (pListBoxHelper)
325 tools::Rectangle aRect = pListBoxHelper->GetBoundingRectangle( static_cast<sal_uInt16>(m_nIndexInParent) );
326 aPoint = aRect.TopLeft();
328 return vcl::unohelper::ConvertToAWTPoint(aPoint);
331 awt::Point SAL_CALL VCLXAccessibleListItem::getLocationOnScreen( )
333 SolarMutexGuard aSolarGuard;
334 std::unique_lock aGuard( m_aMutex );
336 Point aPoint(0,0);
337 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
338 if (pListBoxHelper)
340 tools::Rectangle aRect = pListBoxHelper->GetBoundingRectangle(static_cast<sal_uInt16>(m_nIndexInParent));
341 aPoint = aRect.TopLeft();
342 aPoint += Point(pListBoxHelper->GetWindowExtentsAbsolute().TopLeft());
344 return vcl::unohelper::ConvertToAWTPoint(aPoint);
347 awt::Size SAL_CALL VCLXAccessibleListItem::getSize( )
349 SolarMutexGuard aSolarGuard;
350 std::unique_lock aGuard( m_aMutex );
352 Size aSize;
353 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
354 if (pListBoxHelper)
355 aSize = pListBoxHelper->GetBoundingRectangle( static_cast<sal_uInt16>(m_nIndexInParent) ).GetSize();
357 return vcl::unohelper::ConvertToAWTSize(aSize);
360 void SAL_CALL VCLXAccessibleListItem::grabFocus( )
362 // no focus for each item
365 // XAccessibleText
367 sal_Int32 SAL_CALL VCLXAccessibleListItem::getCaretPosition()
369 return -1;
372 sal_Bool SAL_CALL VCLXAccessibleListItem::setCaretPosition( sal_Int32 nIndex )
374 SolarMutexGuard aSolarGuard;
375 std::unique_lock aGuard( m_aMutex );
377 if ( !implIsValidRange( nIndex, nIndex, m_sEntryText.getLength() ) )
378 throw IndexOutOfBoundsException();
380 return false;
383 sal_Unicode SAL_CALL VCLXAccessibleListItem::getCharacter( sal_Int32 nIndex )
385 SolarMutexGuard aSolarGuard;
386 std::unique_lock aGuard( m_aMutex );
388 return OCommonAccessibleText::implGetCharacter( m_sEntryText, nIndex );
391 Sequence< PropertyValue > SAL_CALL VCLXAccessibleListItem::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& )
393 SolarMutexGuard aSolarGuard;
394 std::unique_lock aGuard( m_aMutex );
396 if ( !implIsValidIndex( nIndex, m_sEntryText.getLength() ) )
397 throw IndexOutOfBoundsException();
399 return Sequence< PropertyValue >();
402 awt::Rectangle SAL_CALL VCLXAccessibleListItem::getCharacterBounds( sal_Int32 nIndex )
404 SolarMutexGuard aSolarGuard;
405 std::unique_lock aGuard( m_aMutex );
407 if ( !implIsValidIndex( nIndex, m_sEntryText.getLength() ) )
408 throw IndexOutOfBoundsException();
410 awt::Rectangle aBounds( 0, 0, 0, 0 );
411 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
412 if (pListBoxHelper)
414 tools::Rectangle aCharRect = pListBoxHelper->GetEntryCharacterBounds( m_nIndexInParent, nIndex );
415 tools::Rectangle aItemRect = pListBoxHelper->GetBoundingRectangle( static_cast<sal_uInt16>(m_nIndexInParent) );
416 aCharRect.Move( -aItemRect.Left(), -aItemRect.Top() );
417 aBounds = vcl::unohelper::ConvertToAWTRect(aCharRect);
420 return aBounds;
423 sal_Int32 SAL_CALL VCLXAccessibleListItem::getCharacterCount()
425 std::unique_lock aGuard( m_aMutex );
427 return m_sEntryText.getLength();
430 sal_Int32 SAL_CALL VCLXAccessibleListItem::getIndexAtPoint( const awt::Point& aPoint )
432 SolarMutexGuard aSolarGuard;
433 std::unique_lock aGuard( m_aMutex );
435 sal_Int32 nIndex = -1;
436 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
437 if (pListBoxHelper)
439 sal_Int32 nPos = LISTBOX_ENTRY_NOTFOUND;
440 tools::Rectangle aItemRect = pListBoxHelper->GetBoundingRectangle( static_cast<sal_uInt16>(m_nIndexInParent) );
441 Point aPnt(vcl::unohelper::ConvertToVCLPoint(aPoint));
442 aPnt += aItemRect.TopLeft();
443 sal_Int32 nI = pListBoxHelper->GetIndexForPoint( aPnt, nPos );
444 if ( nI != -1 && m_nIndexInParent == nPos )
445 nIndex = nI;
447 return nIndex;
450 OUString SAL_CALL VCLXAccessibleListItem::getSelectedText()
452 return OUString();
455 sal_Int32 SAL_CALL VCLXAccessibleListItem::getSelectionStart()
457 return 0;
460 sal_Int32 SAL_CALL VCLXAccessibleListItem::getSelectionEnd()
462 return 0;
465 sal_Bool SAL_CALL VCLXAccessibleListItem::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
467 std::unique_lock aGuard( m_aMutex );
469 if ( !implIsValidRange( nStartIndex, nEndIndex, m_sEntryText.getLength() ) )
470 throw IndexOutOfBoundsException();
472 return false;
475 OUString SAL_CALL VCLXAccessibleListItem::getText()
477 std::unique_lock aGuard( m_aMutex );
479 return m_sEntryText;
482 OUString VCLXAccessibleListItem::getTextRangeImpl(std::unique_lock<std::mutex>& /*rGuard*/, sal_Int32 nStartIndex, sal_Int32 nEndIndex)
484 return OCommonAccessibleText::implGetTextRange(m_sEntryText, nStartIndex, nEndIndex);
487 OUString SAL_CALL VCLXAccessibleListItem::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
489 SolarMutexGuard aSolarGuard;
490 std::unique_lock aGuard( m_aMutex );
492 return getTextRangeImpl(aGuard, nStartIndex, nEndIndex);
495 css::accessibility::TextSegment SAL_CALL VCLXAccessibleListItem::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType )
497 SolarMutexGuard aSolarGuard;
498 std::unique_lock aGuard( m_aMutex );
500 return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
503 css::accessibility::TextSegment SAL_CALL VCLXAccessibleListItem::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType )
505 SolarMutexGuard aSolarGuard;
506 std::unique_lock aGuard( m_aMutex );
508 return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
511 css::accessibility::TextSegment SAL_CALL VCLXAccessibleListItem::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType )
513 SolarMutexGuard aSolarGuard;
514 std::unique_lock aGuard( m_aMutex );
516 return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
519 sal_Bool SAL_CALL VCLXAccessibleListItem::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
521 SolarMutexGuard aSolarGuard;
522 std::unique_lock aGuard( m_aMutex );
524 checkIndex_Impl( nStartIndex, m_sEntryText );
525 checkIndex_Impl( nEndIndex, m_sEntryText );
527 bool bRet = false;
528 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
529 if (pListBoxHelper)
531 Reference< datatransfer::clipboard::XClipboard > xClipboard = pListBoxHelper->GetClipboard();
532 if ( xClipboard.is() )
534 OUString sText(getTextRangeImpl(aGuard, nStartIndex, nEndIndex));
535 rtl::Reference<vcl::unohelper::TextDataObject> pDataObj = new vcl::unohelper::TextDataObject( sText );
537 SolarMutexReleaser aReleaser;
538 xClipboard->setContents( pDataObj, nullptr );
539 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
540 if( xFlushableClipboard.is() )
541 xFlushableClipboard->flushClipboard();
543 bRet = true;
547 return bRet;
550 sal_Bool VCLXAccessibleListItem::scrollSubstringTo( sal_Int32, sal_Int32, AccessibleScrollType )
552 return false;
555 // XAccessibleEventBroadcaster
557 void SAL_CALL VCLXAccessibleListItem::addAccessibleEventListener( const Reference< XAccessibleEventListener >& xListener )
559 if (xListener.is())
561 if (!m_nClientId)
562 m_nClientId = comphelper::AccessibleEventNotifier::registerClient( );
563 comphelper::AccessibleEventNotifier::addEventListener( m_nClientId, xListener );
567 void SAL_CALL VCLXAccessibleListItem::removeAccessibleEventListener( const Reference< XAccessibleEventListener >& xListener )
569 if ( !(xListener.is() && m_nClientId) )
570 return;
572 sal_Int32 nListenerCount = comphelper::AccessibleEventNotifier::removeEventListener( m_nClientId, xListener );
573 if ( nListenerCount )
574 return;
576 // no listeners anymore
577 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
578 // and at least to us not firing any events anymore, in case somebody calls
579 // NotifyAccessibleEvent, again
580 if ( m_nClientId )
582 comphelper::AccessibleEventNotifier::TClientId nId( m_nClientId );
583 m_nClientId = 0;
584 comphelper::AccessibleEventNotifier::revokeClient( nId );
589 // AF (Oct. 29 2002): Return black as constant foreground color. This is an
590 // initial implementation and has to be substituted by code that determines
591 // the color that is actually used.
592 sal_Int32 SAL_CALL VCLXAccessibleListItem::getForeground()
594 return sal_Int32(COL_BLACK);
597 // AF (Oct. 29 2002): Return white as constant background color. This is an
598 // initial implementation and has to be substituted by code that determines
599 // the color that is actually used.
600 sal_Int32 SAL_CALL VCLXAccessibleListItem::getBackground()
602 return sal_Int32(COL_WHITE);
606 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */