Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / accessibility / source / standard / vclxaccessibletabpage.cxx
blob0dae45ca840c9555fa82cd0dc5538c8f268dfaf8
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/vclxaccessibletabpage.hxx>
21 #include <toolkit/helper/convert.hxx>
22 #include <helper/characterattributeshelper.hxx>
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 <comphelper/accessiblecontexthelper.hxx>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <unotools/accessiblerelationsethelper.hxx>
33 #include <vcl/mnemonic.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/unohelp2.hxx>
36 #include <vcl/tabctrl.hxx>
37 #include <vcl/tabpage.hxx>
38 #include <vcl/settings.hxx>
39 #include <i18nlangtag/languagetag.hxx>
41 using namespace ::com::sun::star::accessibility;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star;
46 using namespace ::comphelper;
51 VCLXAccessibleTabPage::VCLXAccessibleTabPage( TabControl* pTabControl, sal_uInt16 nPageId )
52 :m_pTabControl( pTabControl )
53 ,m_nPageId( nPageId )
55 m_bFocused = IsFocused();
56 m_bSelected = IsSelected();
57 m_sPageText = GetPageText();
61 VCLXAccessibleTabPage::~VCLXAccessibleTabPage()
66 bool VCLXAccessibleTabPage::IsFocused() const
68 bool bFocused = false;
70 if ( m_pTabControl && m_pTabControl->HasFocus() && m_pTabControl->GetCurPageId() == m_nPageId )
71 bFocused = true;
73 return bFocused;
77 bool VCLXAccessibleTabPage::IsSelected() const
79 bool bSelected = false;
81 if ( m_pTabControl && m_pTabControl->GetCurPageId() == m_nPageId )
82 bSelected = true;
84 return bSelected;
88 void VCLXAccessibleTabPage::SetFocused( bool bFocused )
90 if ( m_bFocused != bFocused )
92 Any aOldValue, aNewValue;
93 if ( m_bFocused )
94 aOldValue <<= AccessibleStateType::FOCUSED;
95 else
96 aNewValue <<= AccessibleStateType::FOCUSED;
97 m_bFocused = bFocused;
98 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
103 void VCLXAccessibleTabPage::SetSelected( bool bSelected )
105 if ( m_bSelected != bSelected )
107 Any aOldValue, aNewValue;
108 if ( m_bSelected )
109 aOldValue <<= AccessibleStateType::SELECTED;
110 else
111 aNewValue <<= AccessibleStateType::SELECTED;
112 m_bSelected = bSelected;
113 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
118 void VCLXAccessibleTabPage::SetPageText( const OUString& sPageText )
120 Any aOldValue, aNewValue;
121 if ( OCommonAccessibleText::implInitTextChangedEvent( m_sPageText, sPageText, aOldValue, aNewValue ) )
123 Any aOldName, aNewName;
124 aOldName <<= m_sPageText;
125 aNewName <<= sPageText;
126 m_sPageText = sPageText;
127 NotifyAccessibleEvent( AccessibleEventId::NAME_CHANGED, aOldName, aNewName );
128 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
133 OUString VCLXAccessibleTabPage::GetPageText()
135 OUString sText;
136 if ( m_pTabControl )
137 sText = removeMnemonicFromString( m_pTabControl->GetPageText( m_nPageId ) );
139 return sText;
143 void VCLXAccessibleTabPage::Update( bool bNew )
145 if ( !m_pTabControl )
146 return;
148 TabPage* pTabPage = m_pTabControl->GetTabPage( m_nPageId );
149 if ( !pTabPage )
150 return;
152 Reference< XAccessible > xChild( pTabPage->GetAccessible( bNew ) );
153 if ( xChild.is() )
155 Any aOldValue, aNewValue;
156 if ( bNew )
157 aNewValue <<= xChild;
158 else
159 aOldValue <<= xChild;
160 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
165 void VCLXAccessibleTabPage::FillAccessibleStateSet( sal_Int64& rStateSet )
167 rStateSet |= AccessibleStateType::ENABLED;
168 rStateSet |= AccessibleStateType::SENSITIVE;
170 rStateSet |= AccessibleStateType::FOCUSABLE;
172 if ( IsFocused() )
173 rStateSet |= AccessibleStateType::FOCUSED;
175 rStateSet |= AccessibleStateType::VISIBLE;
177 rStateSet |= AccessibleStateType::SHOWING;
179 rStateSet |= AccessibleStateType::SELECTABLE;
181 if ( IsSelected() )
182 rStateSet |= AccessibleStateType::SELECTED;
186 // OCommonAccessibleComponent
189 awt::Rectangle VCLXAccessibleTabPage::implGetBounds()
191 awt::Rectangle aBounds( 0, 0, 0, 0 );
193 if ( m_pTabControl )
194 aBounds = AWTRectangle( m_pTabControl->GetTabBounds( m_nPageId ) );
196 return aBounds;
200 // OCommonAccessibleText
203 OUString VCLXAccessibleTabPage::implGetText()
205 return GetPageText();
209 lang::Locale VCLXAccessibleTabPage::implGetLocale()
211 return Application::GetSettings().GetLanguageTag().getLocale();
215 void VCLXAccessibleTabPage::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
217 nStartIndex = 0;
218 nEndIndex = 0;
222 // XComponent
225 void VCLXAccessibleTabPage::disposing()
227 AccessibleTextHelper_BASE::disposing();
229 m_pTabControl = nullptr;
230 m_sPageText.clear();
234 // XServiceInfo
237 OUString VCLXAccessibleTabPage::getImplementationName()
239 return "com.sun.star.comp.toolkit.AccessibleTabPage";
243 sal_Bool VCLXAccessibleTabPage::supportsService( const OUString& rServiceName )
245 return cppu::supportsService(this, rServiceName);
249 Sequence< OUString > VCLXAccessibleTabPage::getSupportedServiceNames()
251 return { "com.sun.star.awt.AccessibleTabPage" };
255 // XAccessible
258 Reference< XAccessibleContext > VCLXAccessibleTabPage::getAccessibleContext( )
260 OExternalLockGuard aGuard( this );
262 return this;
266 // XAccessibleContext
269 sal_Int64 VCLXAccessibleTabPage::getAccessibleChildCount()
271 OExternalLockGuard aGuard( this );
272 return implGetAccessibleChildCount();
275 sal_Int64 VCLXAccessibleTabPage::implGetAccessibleChildCount()
277 sal_Int64 nCount = 0;
278 if ( m_pTabControl )
280 TabPage* pTabPage = m_pTabControl->GetTabPage( m_nPageId );
281 if ( pTabPage && pTabPage->IsVisible() )
282 nCount = 1;
285 return nCount;
289 Reference< XAccessible > VCLXAccessibleTabPage::getAccessibleChild( sal_Int64 i )
291 OExternalLockGuard aGuard( this );
293 if ( i < 0 || i >= implGetAccessibleChildCount() )
294 throw IndexOutOfBoundsException();
296 Reference< XAccessible > xChild;
297 if ( m_pTabControl )
299 TabPage* pTabPage = m_pTabControl->GetTabPage( m_nPageId );
300 if ( pTabPage && pTabPage->IsVisible() )
301 xChild = pTabPage->GetAccessible();
304 return xChild;
308 Reference< XAccessible > VCLXAccessibleTabPage::getAccessibleParent( )
310 OExternalLockGuard aGuard( this );
312 Reference< XAccessible > xParent;
313 if ( m_pTabControl )
314 xParent = m_pTabControl->GetAccessible();
316 return xParent;
320 sal_Int64 VCLXAccessibleTabPage::getAccessibleIndexInParent( )
322 OExternalLockGuard aGuard( this );
324 sal_Int64 nIndexInParent = -1;
325 if ( m_pTabControl )
326 nIndexInParent = m_pTabControl->GetPagePos( m_nPageId );
328 return nIndexInParent;
331 sal_Int16 VCLXAccessibleTabPage::getAccessibleRole()
333 OExternalLockGuard aGuard( this );
335 return AccessibleRole::PAGE_TAB;
338 OUString VCLXAccessibleTabPage::getAccessibleDescription()
340 OExternalLockGuard aGuard( this );
342 OUString sDescription;
343 if ( m_pTabControl )
344 sDescription = m_pTabControl->GetAccessibleDescription( m_nPageId );
346 return sDescription;
349 OUString VCLXAccessibleTabPage::getAccessibleName()
351 OExternalLockGuard aGuard( this );
353 OUString sName;
354 if ( m_pTabControl )
355 sName = m_pTabControl->GetAccessibleName( m_nPageId );
357 return sName;
360 Reference< XAccessibleRelationSet > VCLXAccessibleTabPage::getAccessibleRelationSet( )
362 OExternalLockGuard aGuard( this );
364 return new utl::AccessibleRelationSetHelper;
368 sal_Int64 VCLXAccessibleTabPage::getAccessibleStateSet( )
370 OExternalLockGuard aGuard( this );
372 sal_Int64 nStateSet = 0;
374 if ( !rBHelper.bDisposed && !rBHelper.bInDispose )
376 FillAccessibleStateSet( nStateSet );
378 else
380 nStateSet |= AccessibleStateType::DEFUNC;
383 return nStateSet;
387 Locale VCLXAccessibleTabPage::getLocale( )
389 OExternalLockGuard aGuard( this );
391 return Application::GetSettings().GetLanguageTag().getLocale();
395 // XAccessibleComponent
398 Reference< XAccessible > VCLXAccessibleTabPage::getAccessibleAtPoint( const awt::Point& rPoint )
400 OExternalLockGuard aGuard( this );
402 Reference< XAccessible > xChild;
403 for ( sal_Int64 i = 0, nCount = getAccessibleChildCount(); i < nCount; ++i )
405 Reference< XAccessible > xAcc = getAccessibleChild( i );
406 if ( xAcc.is() )
408 Reference< XAccessibleComponent > xComp( xAcc->getAccessibleContext(), UNO_QUERY );
409 if ( xComp.is() )
411 tools::Rectangle aRect = VCLRectangle( xComp->getBounds() );
412 Point aPos = VCLPoint( rPoint );
413 if ( aRect.Contains( aPos ) )
415 xChild = xAcc;
416 break;
422 return xChild;
426 void VCLXAccessibleTabPage::grabFocus( )
428 OExternalLockGuard aGuard( this );
430 if ( m_pTabControl )
432 m_pTabControl->SelectTabPage( m_nPageId );
433 m_pTabControl->GrabFocus();
438 sal_Int32 VCLXAccessibleTabPage::getForeground( )
440 OExternalLockGuard aGuard( this );
442 sal_Int32 nColor = 0;
443 Reference< XAccessible > xParent = getAccessibleParent();
444 if ( xParent.is() )
446 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
447 if ( xParentComp.is() )
448 nColor = xParentComp->getForeground();
451 return nColor;
455 sal_Int32 VCLXAccessibleTabPage::getBackground( )
457 OExternalLockGuard aGuard( this );
459 sal_Int32 nColor = 0;
460 Reference< XAccessible > xParent = getAccessibleParent();
461 if ( xParent.is() )
463 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
464 if ( xParentComp.is() )
465 nColor = xParentComp->getBackground();
468 return nColor;
472 // XAccessibleExtendedComponent
475 Reference< awt::XFont > VCLXAccessibleTabPage::getFont( )
477 OExternalLockGuard aGuard( this );
479 Reference< awt::XFont > xFont;
480 Reference< XAccessible > xParent = getAccessibleParent();
481 if ( xParent.is() )
483 Reference< XAccessibleExtendedComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
484 if ( xParentComp.is() )
485 xFont = xParentComp->getFont();
488 return xFont;
492 OUString VCLXAccessibleTabPage::getTitledBorderText( )
494 OExternalLockGuard aGuard( this );
496 return OUString();
500 OUString VCLXAccessibleTabPage::getToolTipText( )
502 OExternalLockGuard aGuard( this );
504 return OUString();
508 // XAccessibleText
510 OUString VCLXAccessibleTabPage::getText()
512 OExternalLockGuard aGuard( this );
514 return GetPageText();
517 OUString VCLXAccessibleTabPage::getTextRange(sal_Int32 nStartIndex, sal_Int32 nEndIndex)
519 OExternalLockGuard aGuard( this );
521 return OCommonAccessibleText::implGetTextRange(GetPageText(), nStartIndex, nEndIndex);
524 sal_Unicode VCLXAccessibleTabPage::getCharacter( sal_Int32 nIndex )
526 OExternalLockGuard aGuard( this );
528 return OCommonAccessibleText::implGetCharacter( GetPageText(), nIndex );
531 sal_Int32 VCLXAccessibleTabPage::getCharacterCount()
533 return GetPageText().getLength();
536 sal_Int32 VCLXAccessibleTabPage::getCaretPosition()
538 OExternalLockGuard aGuard( this );
540 return -1;
544 sal_Bool VCLXAccessibleTabPage::setCaretPosition( sal_Int32 nIndex )
546 OExternalLockGuard aGuard( this );
548 if ( !implIsValidRange( nIndex, nIndex, GetPageText().getLength() ) )
549 throw IndexOutOfBoundsException();
551 return false;
555 Sequence< PropertyValue > VCLXAccessibleTabPage::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes )
557 OExternalLockGuard aGuard( this );
559 Sequence< PropertyValue > aValues;
560 OUString sText( GetPageText() );
562 if ( !implIsValidIndex( nIndex, sText.getLength() ) )
563 throw IndexOutOfBoundsException();
565 if ( m_pTabControl )
567 vcl::Font aFont = m_pTabControl->GetFont();
568 sal_Int32 nBackColor = getBackground();
569 sal_Int32 nColor = getForeground();
570 aValues = CharacterAttributesHelper( aFont, nBackColor, nColor )
571 .GetCharacterAttributes( aRequestedAttributes );
574 return aValues;
578 awt::Rectangle VCLXAccessibleTabPage::getCharacterBounds( sal_Int32 nIndex )
580 OExternalLockGuard aGuard( this );
582 if ( !implIsValidIndex( nIndex, GetPageText().getLength() ) )
583 throw IndexOutOfBoundsException();
585 awt::Rectangle aBounds( 0, 0, 0, 0 );
586 if ( m_pTabControl )
588 tools::Rectangle aPageRect = m_pTabControl->GetTabBounds( m_nPageId );
589 tools::Rectangle aCharRect = m_pTabControl->GetCharacterBounds( m_nPageId, nIndex );
590 aCharRect.Move( -aPageRect.Left(), -aPageRect.Top() );
591 aBounds = AWTRectangle( aCharRect );
594 return aBounds;
598 sal_Int32 VCLXAccessibleTabPage::getIndexAtPoint( const awt::Point& aPoint )
600 OExternalLockGuard aGuard( this );
602 sal_Int32 nIndex = -1;
603 if ( m_pTabControl )
605 sal_uInt16 nPageId = 0;
606 tools::Rectangle aPageRect = m_pTabControl->GetTabBounds( m_nPageId );
607 Point aPnt( VCLPoint( aPoint ) );
608 aPnt += aPageRect.TopLeft();
609 sal_Int32 nI = m_pTabControl->GetIndexForPoint( aPnt, nPageId );
610 if ( nI != -1 && m_nPageId == nPageId )
611 nIndex = nI;
614 return nIndex;
618 sal_Bool VCLXAccessibleTabPage::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
620 OExternalLockGuard aGuard( this );
622 if ( !implIsValidRange( nStartIndex, nEndIndex, GetPageText().getLength() ) )
623 throw IndexOutOfBoundsException();
625 return false;
629 sal_Bool VCLXAccessibleTabPage::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
631 OExternalLockGuard aGuard( this );
633 bool bReturn = false;
635 if ( m_pTabControl )
637 Reference< datatransfer::clipboard::XClipboard > xClipboard = m_pTabControl->GetClipboard();
638 if ( xClipboard.is() )
640 OUString sText( implGetTextRange( GetPageText(), nStartIndex, nEndIndex ) );
642 rtl::Reference<vcl::unohelper::TextDataObject> pDataObj = new vcl::unohelper::TextDataObject( sText );
644 SolarMutexReleaser aReleaser;
645 xClipboard->setContents( pDataObj, nullptr );
647 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
648 if( xFlushableClipboard.is() )
649 xFlushableClipboard->flushClipboard();
651 bReturn = true;
655 return bReturn;
658 sal_Bool VCLXAccessibleTabPage::scrollSubstringTo( sal_Int32, sal_Int32, AccessibleScrollType )
660 return false;
664 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */