bump product version to 7.6.3.2-android
[LibreOffice.git] / accessibility / source / standard / vclxaccessiblelistitem.cxx
blob55fd0693718d781d51b6e3c6bbc715d70d2c0f12
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 <toolkit/helper/convert.hxx>
22 #include <helper/IComboListBoxHelper.hxx>
23 #include <com/sun/star/awt/Rectangle.hpp>
25 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
26 #include <com/sun/star/accessibility/AccessibleRole.hpp>
27 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
28 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
29 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
30 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <utility>
33 #include <vcl/svapp.hxx>
34 #include <vcl/toolkit/lstbox.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 : VCLXAccessibleListItem_BASE(m_aMutex)
64 , m_nIndexInParent(_nIndexInParent)
65 , m_bSelected(false)
66 , m_bVisible(false)
67 , m_nClientId(0)
68 , m_xParent(std::move(_xParent))
70 assert(m_xParent.is());
71 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent->getListBoxHelper();
72 if (pListBoxHelper)
73 m_sEntryText = pListBoxHelper->GetEntry(static_cast<sal_uInt16>(_nIndexInParent));
76 void VCLXAccessibleListItem::SetSelected( bool _bSelected )
78 if ( m_bSelected != _bSelected )
80 Any aOldValue;
81 Any aNewValue;
82 if ( m_bSelected )
83 aOldValue <<= AccessibleStateType::SELECTED;
84 else
85 aNewValue <<= AccessibleStateType::SELECTED;
86 m_bSelected = _bSelected;
87 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
91 void VCLXAccessibleListItem::SetVisible( bool _bVisible )
93 if ( m_bVisible != _bVisible )
95 Any aOldValue, aNewValue;
96 m_bVisible = _bVisible;
97 (_bVisible ? aNewValue : aOldValue ) <<= AccessibleStateType::VISIBLE;
98 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
99 (_bVisible ? aNewValue : aOldValue ) <<= AccessibleStateType::SHOWING;
100 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
104 void VCLXAccessibleListItem::NotifyAccessibleEvent( sal_Int16 _nEventId,
105 const css::uno::Any& _aOldValue,
106 const css::uno::Any& _aNewValue )
108 AccessibleEventObject aEvt;
109 aEvt.Source = *this;
110 aEvt.EventId = _nEventId;
111 aEvt.OldValue = _aOldValue;
112 aEvt.NewValue = _aNewValue;
114 if (m_nClientId)
115 comphelper::AccessibleEventNotifier::addEvent( m_nClientId, aEvt );
118 // OCommonAccessibleText
120 OUString VCLXAccessibleListItem::implGetText()
122 return m_sEntryText;
125 Locale VCLXAccessibleListItem::implGetLocale()
127 return Application::GetSettings().GetLanguageTag().getLocale();
130 void VCLXAccessibleListItem::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
132 nStartIndex = 0;
133 nEndIndex = 0;
136 // XTypeProvider
138 Sequence< sal_Int8 > VCLXAccessibleListItem::getImplementationId()
140 return css::uno::Sequence<sal_Int8>();
143 // XComponent
145 void SAL_CALL VCLXAccessibleListItem::disposing()
147 comphelper::AccessibleEventNotifier::TClientId nId( 0 );
148 Reference< XInterface > xEventSource;
150 ::osl::MutexGuard aGuard( m_aMutex );
152 VCLXAccessibleListItem_BASE::disposing();
153 m_sEntryText.clear();
154 m_xParent = nullptr;
156 nId = m_nClientId;
157 m_nClientId = 0;
158 if ( nId )
159 xEventSource = *this;
162 // Send a disposing to all listeners.
163 if ( nId )
164 comphelper::AccessibleEventNotifier::revokeClientNotifyDisposing( nId, *this );
167 // XServiceInfo
169 OUString VCLXAccessibleListItem::getImplementationName()
171 return "com.sun.star.comp.toolkit.AccessibleListItem";
174 sal_Bool VCLXAccessibleListItem::supportsService( const OUString& rServiceName )
176 return cppu::supportsService(this, rServiceName);
179 Sequence< OUString > VCLXAccessibleListItem::getSupportedServiceNames()
181 return {"com.sun.star.accessibility.AccessibleContext",
182 "com.sun.star.accessibility.AccessibleComponent",
183 "com.sun.star.accessibility.AccessibleListItem"};
186 // XAccessible
188 Reference< XAccessibleContext > SAL_CALL VCLXAccessibleListItem::getAccessibleContext( )
190 return this;
193 // XAccessibleContext
195 sal_Int64 SAL_CALL VCLXAccessibleListItem::getAccessibleChildCount( )
197 return 0;
200 Reference< XAccessible > SAL_CALL VCLXAccessibleListItem::getAccessibleChild( sal_Int64 )
202 return Reference< XAccessible >();
205 Reference< XAccessible > SAL_CALL VCLXAccessibleListItem::getAccessibleParent( )
207 ::osl::MutexGuard aGuard( m_aMutex );
209 return m_xParent;
212 sal_Int64 SAL_CALL VCLXAccessibleListItem::getAccessibleIndexInParent( )
214 ::osl::MutexGuard aGuard( m_aMutex );
215 return m_nIndexInParent;
218 sal_Int16 SAL_CALL VCLXAccessibleListItem::getAccessibleRole( )
220 return AccessibleRole::LIST_ITEM;
221 // return AccessibleRole::LABEL;
224 OUString SAL_CALL VCLXAccessibleListItem::getAccessibleDescription( )
226 // no description for every item
227 return OUString();
230 OUString SAL_CALL VCLXAccessibleListItem::getAccessibleName( )
232 ::osl::MutexGuard aGuard( m_aMutex );
234 // entry text == accessible name
235 return m_sEntryText;
238 Reference< XAccessibleRelationSet > SAL_CALL VCLXAccessibleListItem::getAccessibleRelationSet( )
240 return new utl::AccessibleRelationSetHelper;
243 sal_Int64 SAL_CALL VCLXAccessibleListItem::getAccessibleStateSet( )
245 ::osl::MutexGuard aGuard( m_aMutex );
247 sal_Int64 nStateSet = 0;
249 if ( !rBHelper.bDisposed && !rBHelper.bInDispose )
251 nStateSet |= AccessibleStateType::TRANSIENT;
253 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
254 if (pListBoxHelper && pListBoxHelper->IsEnabled())
256 nStateSet |= AccessibleStateType::SELECTABLE;
257 nStateSet |= AccessibleStateType::ENABLED;
258 nStateSet |= AccessibleStateType::SENSITIVE;
261 if ( m_bSelected )
262 nStateSet |= AccessibleStateType::SELECTED;
263 if ( m_bVisible )
265 nStateSet |= AccessibleStateType::VISIBLE;
266 nStateSet |= AccessibleStateType::SHOWING;
269 else
270 nStateSet |= AccessibleStateType::DEFUNC;
272 return nStateSet;
275 Locale SAL_CALL VCLXAccessibleListItem::getLocale( )
277 SolarMutexGuard aSolarGuard;
278 ::osl::MutexGuard aGuard( m_aMutex );
280 return implGetLocale();
283 // XAccessibleComponent
285 sal_Bool SAL_CALL VCLXAccessibleListItem::containsPoint( const awt::Point& _aPoint )
287 SolarMutexGuard aSolarGuard;
288 ::osl::MutexGuard aGuard( m_aMutex );
290 bool bInside = false;
291 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
292 if (pListBoxHelper)
294 tools::Rectangle aRect(pListBoxHelper->GetBoundingRectangle(static_cast<sal_uInt16>(m_nIndexInParent)));
295 aRect.Move(-aRect.Left(), -aRect.Top());
296 bInside = aRect.Contains( VCLPoint( _aPoint ) );
298 return bInside;
301 Reference< XAccessible > SAL_CALL VCLXAccessibleListItem::getAccessibleAtPoint( const awt::Point& )
303 return Reference< XAccessible >();
306 awt::Rectangle SAL_CALL VCLXAccessibleListItem::getBounds( )
308 SolarMutexGuard aSolarGuard;
309 ::osl::MutexGuard aGuard( m_aMutex );
311 awt::Rectangle aRect;
312 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
313 if (pListBoxHelper)
314 aRect = AWTRectangle(pListBoxHelper->GetBoundingRectangle(static_cast<sal_uInt16>(m_nIndexInParent)));
316 return aRect;
319 awt::Point SAL_CALL VCLXAccessibleListItem::getLocation( )
321 SolarMutexGuard aSolarGuard;
322 ::osl::MutexGuard aGuard( m_aMutex );
324 Point aPoint(0,0);
325 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
326 if (pListBoxHelper)
328 tools::Rectangle aRect = pListBoxHelper->GetBoundingRectangle( static_cast<sal_uInt16>(m_nIndexInParent) );
329 aPoint = aRect.TopLeft();
331 return AWTPoint( aPoint );
334 awt::Point SAL_CALL VCLXAccessibleListItem::getLocationOnScreen( )
336 SolarMutexGuard aSolarGuard;
337 ::osl::MutexGuard aGuard( m_aMutex );
339 Point aPoint(0,0);
340 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
341 if (pListBoxHelper)
343 tools::Rectangle aRect = pListBoxHelper->GetBoundingRectangle(static_cast<sal_uInt16>(m_nIndexInParent));
344 aPoint = aRect.TopLeft();
345 aPoint += pListBoxHelper->GetWindowExtentsRelative().TopLeft();
347 return AWTPoint( aPoint );
350 awt::Size SAL_CALL VCLXAccessibleListItem::getSize( )
352 SolarMutexGuard aSolarGuard;
353 ::osl::MutexGuard aGuard( m_aMutex );
355 Size aSize;
356 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
357 if (pListBoxHelper)
358 aSize = pListBoxHelper->GetBoundingRectangle( static_cast<sal_uInt16>(m_nIndexInParent) ).GetSize();
360 return AWTSize( aSize );
363 void SAL_CALL VCLXAccessibleListItem::grabFocus( )
365 // no focus for each item
368 // XAccessibleText
370 sal_Int32 SAL_CALL VCLXAccessibleListItem::getCaretPosition()
372 return -1;
375 sal_Bool SAL_CALL VCLXAccessibleListItem::setCaretPosition( sal_Int32 nIndex )
377 SolarMutexGuard aSolarGuard;
378 ::osl::MutexGuard aGuard( m_aMutex );
380 if ( !implIsValidRange( nIndex, nIndex, m_sEntryText.getLength() ) )
381 throw IndexOutOfBoundsException();
383 return false;
386 sal_Unicode SAL_CALL VCLXAccessibleListItem::getCharacter( sal_Int32 nIndex )
388 SolarMutexGuard aSolarGuard;
389 ::osl::MutexGuard aGuard( m_aMutex );
391 return OCommonAccessibleText::implGetCharacter( m_sEntryText, nIndex );
394 Sequence< PropertyValue > SAL_CALL VCLXAccessibleListItem::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& )
396 SolarMutexGuard aSolarGuard;
397 ::osl::MutexGuard aGuard( m_aMutex );
399 if ( !implIsValidIndex( nIndex, m_sEntryText.getLength() ) )
400 throw IndexOutOfBoundsException();
402 return Sequence< PropertyValue >();
405 awt::Rectangle SAL_CALL VCLXAccessibleListItem::getCharacterBounds( sal_Int32 nIndex )
407 SolarMutexGuard aSolarGuard;
408 ::osl::MutexGuard aGuard( m_aMutex );
410 if ( !implIsValidIndex( nIndex, m_sEntryText.getLength() ) )
411 throw IndexOutOfBoundsException();
413 awt::Rectangle aBounds( 0, 0, 0, 0 );
414 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
415 if (pListBoxHelper)
417 tools::Rectangle aCharRect = pListBoxHelper->GetEntryCharacterBounds( m_nIndexInParent, nIndex );
418 tools::Rectangle aItemRect = pListBoxHelper->GetBoundingRectangle( static_cast<sal_uInt16>(m_nIndexInParent) );
419 aCharRect.Move( -aItemRect.Left(), -aItemRect.Top() );
420 aBounds = AWTRectangle( aCharRect );
423 return aBounds;
426 sal_Int32 SAL_CALL VCLXAccessibleListItem::getCharacterCount()
428 SolarMutexGuard aSolarGuard;
429 ::osl::MutexGuard aGuard( m_aMutex );
431 return m_sEntryText.getLength();
434 sal_Int32 SAL_CALL VCLXAccessibleListItem::getIndexAtPoint( const awt::Point& aPoint )
436 SolarMutexGuard aSolarGuard;
437 ::osl::MutexGuard aGuard( m_aMutex );
439 sal_Int32 nIndex = -1;
440 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
441 if (pListBoxHelper)
443 sal_Int32 nPos = LISTBOX_ENTRY_NOTFOUND;
444 tools::Rectangle aItemRect = pListBoxHelper->GetBoundingRectangle( static_cast<sal_uInt16>(m_nIndexInParent) );
445 Point aPnt( VCLPoint( aPoint ) );
446 aPnt += aItemRect.TopLeft();
447 sal_Int32 nI = pListBoxHelper->GetIndexForPoint( aPnt, nPos );
448 if ( nI != -1 && m_nIndexInParent == nPos )
449 nIndex = nI;
451 return nIndex;
454 OUString SAL_CALL VCLXAccessibleListItem::getSelectedText()
456 SolarMutexGuard aSolarGuard;
457 ::osl::MutexGuard aGuard( m_aMutex );
459 return OUString();
462 sal_Int32 SAL_CALL VCLXAccessibleListItem::getSelectionStart()
464 SolarMutexGuard aSolarGuard;
465 ::osl::MutexGuard aGuard( m_aMutex );
467 return 0;
470 sal_Int32 SAL_CALL VCLXAccessibleListItem::getSelectionEnd()
472 SolarMutexGuard aSolarGuard;
473 ::osl::MutexGuard aGuard( m_aMutex );
475 return 0;
478 sal_Bool SAL_CALL VCLXAccessibleListItem::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
480 SolarMutexGuard aSolarGuard;
481 ::osl::MutexGuard aGuard( m_aMutex );
483 if ( !implIsValidRange( nStartIndex, nEndIndex, m_sEntryText.getLength() ) )
484 throw IndexOutOfBoundsException();
486 return false;
489 OUString SAL_CALL VCLXAccessibleListItem::getText()
491 SolarMutexGuard aSolarGuard;
492 ::osl::MutexGuard aGuard( m_aMutex );
494 return m_sEntryText;
497 OUString SAL_CALL VCLXAccessibleListItem::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
499 SolarMutexGuard aSolarGuard;
500 ::osl::MutexGuard aGuard( m_aMutex );
502 return OCommonAccessibleText::implGetTextRange( m_sEntryText, nStartIndex, nEndIndex );
505 css::accessibility::TextSegment SAL_CALL VCLXAccessibleListItem::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType )
507 SolarMutexGuard aSolarGuard;
508 ::osl::MutexGuard aGuard( m_aMutex );
510 return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
513 css::accessibility::TextSegment SAL_CALL VCLXAccessibleListItem::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType )
515 SolarMutexGuard aSolarGuard;
516 ::osl::MutexGuard aGuard( m_aMutex );
518 return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
521 css::accessibility::TextSegment SAL_CALL VCLXAccessibleListItem::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType )
523 SolarMutexGuard aSolarGuard;
524 ::osl::MutexGuard aGuard( m_aMutex );
526 return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
529 sal_Bool SAL_CALL VCLXAccessibleListItem::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
531 SolarMutexGuard aSolarGuard;
532 ::osl::MutexGuard aGuard( m_aMutex );
534 checkIndex_Impl( nStartIndex, m_sEntryText );
535 checkIndex_Impl( nEndIndex, m_sEntryText );
537 bool bRet = false;
538 ::accessibility::IComboListBoxHelper* pListBoxHelper = m_xParent.is() ? m_xParent->getListBoxHelper() : nullptr;
539 if (pListBoxHelper)
541 Reference< datatransfer::clipboard::XClipboard > xClipboard = pListBoxHelper->GetClipboard();
542 if ( xClipboard.is() )
544 OUString sText( getTextRange( nStartIndex, nEndIndex ) );
545 rtl::Reference<vcl::unohelper::TextDataObject> pDataObj = new vcl::unohelper::TextDataObject( sText );
547 SolarMutexReleaser aReleaser;
548 xClipboard->setContents( pDataObj, nullptr );
549 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
550 if( xFlushableClipboard.is() )
551 xFlushableClipboard->flushClipboard();
553 bRet = true;
557 return bRet;
560 sal_Bool VCLXAccessibleListItem::scrollSubstringTo( sal_Int32, sal_Int32, AccessibleScrollType )
562 return false;
565 // XAccessibleEventBroadcaster
567 void SAL_CALL VCLXAccessibleListItem::addAccessibleEventListener( const Reference< XAccessibleEventListener >& xListener )
569 if (xListener.is())
571 if (!m_nClientId)
572 m_nClientId = comphelper::AccessibleEventNotifier::registerClient( );
573 comphelper::AccessibleEventNotifier::addEventListener( m_nClientId, xListener );
577 void SAL_CALL VCLXAccessibleListItem::removeAccessibleEventListener( const Reference< XAccessibleEventListener >& xListener )
579 if ( !(xListener.is() && m_nClientId) )
580 return;
582 sal_Int32 nListenerCount = comphelper::AccessibleEventNotifier::removeEventListener( m_nClientId, xListener );
583 if ( nListenerCount )
584 return;
586 // no listeners anymore
587 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
588 // and at least to us not firing any events anymore, in case somebody calls
589 // NotifyAccessibleEvent, again
590 if ( m_nClientId )
592 comphelper::AccessibleEventNotifier::TClientId nId( m_nClientId );
593 m_nClientId = 0;
594 comphelper::AccessibleEventNotifier::revokeClient( nId );
599 // AF (Oct. 29 2002): Return black as constant foreground color. This is an
600 // initial implementation and has to be substituted by code that determines
601 // the color that is actually used.
602 sal_Int32 SAL_CALL VCLXAccessibleListItem::getForeground()
604 return sal_Int32(COL_BLACK);
607 // AF (Oct. 29 2002): Return white as constant background color. This is an
608 // initial implementation and has to be substituted by code that determines
609 // the color that is actually used.
610 sal_Int32 SAL_CALL VCLXAccessibleListItem::getBackground()
612 return sal_Int32(COL_WHITE);
616 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */