Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / standard / vclxaccessibletabpage.cxx
blobf0bf4d36279bd7fea949a5c07dff4e1cd952448d
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 <accessibility/standard/vclxaccessibletabpage.hxx>
21 #include <toolkit/helper/externallock.hxx>
22 #include <toolkit/helper/convert.hxx>
23 #include <accessibility/helper/characterattributeshelper.hxx>
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 <cppuhelper/supportsservice.hxx>
31 #include <unotools/accessiblestatesethelper.hxx>
32 #include <unotools/accessiblerelationsethelper.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/unohelp2.hxx>
35 #include <vcl/tabctrl.hxx>
36 #include <vcl/tabpage.hxx>
37 #include <vcl/settings.hxx>
39 using namespace ::com::sun::star::accessibility;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star;
44 using namespace ::comphelper;
48 // class VCLXAccessibleTabPage
51 VCLXAccessibleTabPage::VCLXAccessibleTabPage( TabControl* pTabControl, sal_uInt16 nPageId )
52 :AccessibleTextHelper_BASE( new VCLExternalSolarLock() )
53 ,m_pTabControl( pTabControl )
54 ,m_nPageId( nPageId )
56 m_pExternalLock = static_cast< VCLExternalSolarLock* >( getExternalLock() );
57 m_bFocused = IsFocused();
58 m_bSelected = IsSelected();
59 m_sPageText = GetPageText();
64 VCLXAccessibleTabPage::~VCLXAccessibleTabPage()
66 delete m_pExternalLock;
67 m_pExternalLock = NULL;
72 bool VCLXAccessibleTabPage::IsFocused()
74 bool bFocused = false;
76 if ( m_pTabControl && m_pTabControl->HasFocus() && m_pTabControl->GetCurPageId() == m_nPageId )
77 bFocused = true;
79 return bFocused;
84 bool VCLXAccessibleTabPage::IsSelected()
86 bool bSelected = false;
88 if ( m_pTabControl && m_pTabControl->GetCurPageId() == m_nPageId )
89 bSelected = true;
91 return bSelected;
96 void VCLXAccessibleTabPage::SetFocused( bool bFocused )
98 if ( m_bFocused != bFocused )
100 Any aOldValue, aNewValue;
101 if ( m_bFocused )
102 aOldValue <<= AccessibleStateType::FOCUSED;
103 else
104 aNewValue <<= AccessibleStateType::FOCUSED;
105 m_bFocused = bFocused;
106 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
112 void VCLXAccessibleTabPage::SetSelected( bool bSelected )
114 if ( m_bSelected != bSelected )
116 Any aOldValue, aNewValue;
117 if ( m_bSelected )
118 aOldValue <<= AccessibleStateType::SELECTED;
119 else
120 aNewValue <<= AccessibleStateType::SELECTED;
121 m_bSelected = bSelected;
122 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
128 void VCLXAccessibleTabPage::SetPageText( const OUString& sPageText )
130 Any aOldValue, aNewValue;
131 if ( OCommonAccessibleText::implInitTextChangedEvent( m_sPageText, sPageText, aOldValue, aNewValue ) )
133 Any aOldName, aNewName;
134 aOldName <<= m_sPageText;
135 aNewName <<= sPageText;
136 m_sPageText = sPageText;
137 NotifyAccessibleEvent( AccessibleEventId::NAME_CHANGED, aOldName, aNewName );
138 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
144 OUString VCLXAccessibleTabPage::GetPageText()
146 OUString sText;
147 if ( m_pTabControl )
148 sText = OutputDevice::GetNonMnemonicString( m_pTabControl->GetPageText( m_nPageId ) );
150 return sText;
155 void VCLXAccessibleTabPage::Update( bool bNew )
157 if ( m_pTabControl )
159 TabPage* pTabPage = m_pTabControl->GetTabPage( m_nPageId );
160 if ( pTabPage )
162 Reference< XAccessible > xChild( pTabPage->GetAccessible( bNew ) );
163 if ( xChild.is() )
165 Any aOldValue, aNewValue;
166 if ( bNew )
167 aNewValue <<= xChild;
168 else
169 aOldValue <<= xChild;
170 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
178 void VCLXAccessibleTabPage::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
180 rStateSet.AddState( AccessibleStateType::ENABLED );
181 rStateSet.AddState( AccessibleStateType::SENSITIVE );
183 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
185 if ( IsFocused() )
186 rStateSet.AddState( AccessibleStateType::FOCUSED );
188 rStateSet.AddState( AccessibleStateType::VISIBLE );
190 rStateSet.AddState( AccessibleStateType::SHOWING );
192 rStateSet.AddState( AccessibleStateType::SELECTABLE );
194 if ( IsSelected() )
195 rStateSet.AddState( AccessibleStateType::SELECTED );
199 // OCommonAccessibleComponent
202 awt::Rectangle VCLXAccessibleTabPage::implGetBounds() throw (RuntimeException)
204 awt::Rectangle aBounds( 0, 0, 0, 0 );
206 if ( m_pTabControl )
207 aBounds = AWTRectangle( m_pTabControl->GetTabBounds( m_nPageId ) );
209 return aBounds;
213 // OCommonAccessibleText
216 OUString VCLXAccessibleTabPage::implGetText()
218 return GetPageText();
223 lang::Locale VCLXAccessibleTabPage::implGetLocale()
225 return Application::GetSettings().GetLanguageTag().getLocale();
230 void VCLXAccessibleTabPage::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
232 nStartIndex = 0;
233 nEndIndex = 0;
237 // XInterface
240 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTabPage, AccessibleTextHelper_BASE, VCLXAccessibleTabPage_BASE )
243 // XTypeProvider
246 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTabPage, AccessibleTextHelper_BASE, VCLXAccessibleTabPage_BASE )
249 // XComponent
252 void VCLXAccessibleTabPage::disposing()
254 AccessibleTextHelper_BASE::disposing();
256 m_pTabControl = NULL;
257 m_sPageText.clear();
261 // XServiceInfo
264 OUString VCLXAccessibleTabPage::getImplementationName() throw (RuntimeException, std::exception)
266 return OUString( "com.sun.star.comp.toolkit.AccessibleTabPage" );
271 sal_Bool VCLXAccessibleTabPage::supportsService( const OUString& rServiceName ) throw (RuntimeException, std::exception)
273 return cppu::supportsService(this, rServiceName);
278 Sequence< OUString > VCLXAccessibleTabPage::getSupportedServiceNames() throw (RuntimeException, std::exception)
280 Sequence< OUString > aNames(1);
281 aNames[0] = "com.sun.star.awt.AccessibleTabPage";
282 return aNames;
286 // XAccessible
289 Reference< XAccessibleContext > VCLXAccessibleTabPage::getAccessibleContext( ) throw (RuntimeException, std::exception)
291 OExternalLockGuard aGuard( this );
293 return this;
297 // XAccessibleContext
300 sal_Int32 VCLXAccessibleTabPage::getAccessibleChildCount() throw (RuntimeException, std::exception)
302 OExternalLockGuard aGuard( this );
304 sal_Int32 nCount = 0;
305 if ( m_pTabControl )
307 TabPage* pTabPage = m_pTabControl->GetTabPage( m_nPageId );
308 if ( pTabPage && pTabPage->IsVisible() )
309 nCount = 1;
312 return nCount;
317 Reference< XAccessible > VCLXAccessibleTabPage::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
319 OExternalLockGuard aGuard( this );
321 if ( i < 0 || i >= getAccessibleChildCount() )
322 throw IndexOutOfBoundsException();
324 Reference< XAccessible > xChild;
325 if ( m_pTabControl )
327 TabPage* pTabPage = m_pTabControl->GetTabPage( m_nPageId );
328 if ( pTabPage && pTabPage->IsVisible() )
329 xChild = pTabPage->GetAccessible();
332 return xChild;
337 Reference< XAccessible > VCLXAccessibleTabPage::getAccessibleParent( ) throw (RuntimeException, std::exception)
339 OExternalLockGuard aGuard( this );
341 Reference< XAccessible > xParent;
342 if ( m_pTabControl )
343 xParent = m_pTabControl->GetAccessible();
345 return xParent;
350 sal_Int32 VCLXAccessibleTabPage::getAccessibleIndexInParent( ) throw (RuntimeException, std::exception)
352 OExternalLockGuard aGuard( this );
354 sal_Int32 nIndexInParent = -1;
355 if ( m_pTabControl )
356 nIndexInParent = m_pTabControl->GetPagePos( m_nPageId );
358 return nIndexInParent;
363 sal_Int16 VCLXAccessibleTabPage::getAccessibleRole( ) throw (RuntimeException, std::exception)
365 OExternalLockGuard aGuard( this );
367 return AccessibleRole::PAGE_TAB;
372 OUString VCLXAccessibleTabPage::getAccessibleDescription( ) throw (RuntimeException, std::exception)
374 OExternalLockGuard aGuard( this );
376 OUString sDescription;
377 if ( m_pTabControl )
378 sDescription = m_pTabControl->GetHelpText( m_nPageId );
380 return sDescription;
385 OUString VCLXAccessibleTabPage::getAccessibleName( ) throw (RuntimeException, std::exception)
387 OExternalLockGuard aGuard( this );
389 return GetPageText();
394 Reference< XAccessibleRelationSet > VCLXAccessibleTabPage::getAccessibleRelationSet( ) throw (RuntimeException, std::exception)
396 OExternalLockGuard aGuard( this );
398 utl::AccessibleRelationSetHelper* pRelationSetHelper = new utl::AccessibleRelationSetHelper;
399 Reference< XAccessibleRelationSet > xSet = pRelationSetHelper;
400 return xSet;
405 Reference< XAccessibleStateSet > VCLXAccessibleTabPage::getAccessibleStateSet( ) throw (RuntimeException, std::exception)
407 OExternalLockGuard aGuard( this );
409 utl::AccessibleStateSetHelper* pStateSetHelper = new utl::AccessibleStateSetHelper;
410 Reference< XAccessibleStateSet > xSet = pStateSetHelper;
412 if ( !rBHelper.bDisposed && !rBHelper.bInDispose )
414 FillAccessibleStateSet( *pStateSetHelper );
416 else
418 pStateSetHelper->AddState( AccessibleStateType::DEFUNC );
421 return xSet;
426 Locale VCLXAccessibleTabPage::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException, std::exception)
428 OExternalLockGuard aGuard( this );
430 return Application::GetSettings().GetLanguageTag().getLocale();
434 // XAccessibleComponent
437 Reference< XAccessible > VCLXAccessibleTabPage::getAccessibleAtPoint( const awt::Point& rPoint ) throw (RuntimeException, std::exception)
439 OExternalLockGuard aGuard( this );
441 Reference< XAccessible > xChild;
442 for ( sal_uInt32 i = 0, nCount = getAccessibleChildCount(); i < nCount; ++i )
444 Reference< XAccessible > xAcc = getAccessibleChild( i );
445 if ( xAcc.is() )
447 Reference< XAccessibleComponent > xComp( xAcc->getAccessibleContext(), UNO_QUERY );
448 if ( xComp.is() )
450 Rectangle aRect = VCLRectangle( xComp->getBounds() );
451 Point aPos = VCLPoint( rPoint );
452 if ( aRect.IsInside( aPos ) )
454 xChild = xAcc;
455 break;
461 return xChild;
466 void VCLXAccessibleTabPage::grabFocus( ) throw (RuntimeException, std::exception)
468 OExternalLockGuard aGuard( this );
470 if ( m_pTabControl )
472 m_pTabControl->SelectTabPage( m_nPageId );
473 m_pTabControl->GrabFocus();
479 sal_Int32 VCLXAccessibleTabPage::getForeground( ) throw (RuntimeException, std::exception)
481 OExternalLockGuard aGuard( this );
483 sal_Int32 nColor = 0;
484 Reference< XAccessible > xParent = getAccessibleParent();
485 if ( xParent.is() )
487 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
488 if ( xParentComp.is() )
489 nColor = xParentComp->getForeground();
492 return nColor;
497 sal_Int32 VCLXAccessibleTabPage::getBackground( ) throw (RuntimeException, std::exception)
499 OExternalLockGuard aGuard( this );
501 sal_Int32 nColor = 0;
502 Reference< XAccessible > xParent = getAccessibleParent();
503 if ( xParent.is() )
505 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
506 if ( xParentComp.is() )
507 nColor = xParentComp->getBackground();
510 return nColor;
514 // XAccessibleExtendedComponent
517 Reference< awt::XFont > VCLXAccessibleTabPage::getFont( ) throw (RuntimeException, std::exception)
519 OExternalLockGuard aGuard( this );
521 Reference< awt::XFont > xFont;
522 Reference< XAccessible > xParent = getAccessibleParent();
523 if ( xParent.is() )
525 Reference< XAccessibleExtendedComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
526 if ( xParentComp.is() )
527 xFont = xParentComp->getFont();
530 return xFont;
535 OUString VCLXAccessibleTabPage::getTitledBorderText( ) throw (RuntimeException, std::exception)
537 OExternalLockGuard aGuard( this );
539 return OUString();
544 OUString VCLXAccessibleTabPage::getToolTipText( ) throw (RuntimeException, std::exception)
546 OExternalLockGuard aGuard( this );
548 return OUString();
552 // XAccessibleText
555 sal_Int32 VCLXAccessibleTabPage::getCaretPosition() throw (RuntimeException, std::exception)
557 OExternalLockGuard aGuard( this );
559 return -1;
564 sal_Bool VCLXAccessibleTabPage::setCaretPosition( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
566 OExternalLockGuard aGuard( this );
568 if ( !implIsValidRange( nIndex, nIndex, implGetText().getLength() ) )
569 throw IndexOutOfBoundsException();
571 return false;
576 Sequence< PropertyValue > VCLXAccessibleTabPage::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
578 OExternalLockGuard aGuard( this );
580 Sequence< PropertyValue > aValues;
581 OUString sText( implGetText() );
583 if ( !implIsValidIndex( nIndex, sText.getLength() ) )
584 throw IndexOutOfBoundsException();
586 if ( m_pTabControl )
588 vcl::Font aFont = m_pTabControl->GetFont();
589 sal_Int32 nBackColor = getBackground();
590 sal_Int32 nColor = getForeground();
591 aValues = CharacterAttributesHelper( aFont, nBackColor, nColor )
592 .GetCharacterAttributes( aRequestedAttributes );
595 return aValues;
600 awt::Rectangle VCLXAccessibleTabPage::getCharacterBounds( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
602 OExternalLockGuard aGuard( this );
604 if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
605 throw IndexOutOfBoundsException();
607 awt::Rectangle aBounds( 0, 0, 0, 0 );
608 if ( m_pTabControl )
610 Rectangle aPageRect = m_pTabControl->GetTabBounds( m_nPageId );
611 Rectangle aCharRect = m_pTabControl->GetCharacterBounds( m_nPageId, nIndex );
612 aCharRect.Move( -aPageRect.Left(), -aPageRect.Top() );
613 aBounds = AWTRectangle( aCharRect );
616 return aBounds;
621 sal_Int32 VCLXAccessibleTabPage::getIndexAtPoint( const awt::Point& aPoint ) throw (RuntimeException, std::exception)
623 OExternalLockGuard aGuard( this );
625 sal_Int32 nIndex = -1;
626 if ( m_pTabControl )
628 sal_uInt16 nPageId = 0;
629 Rectangle aPageRect = m_pTabControl->GetTabBounds( m_nPageId );
630 Point aPnt( VCLPoint( aPoint ) );
631 aPnt += aPageRect.TopLeft();
632 sal_Int32 nI = m_pTabControl->GetIndexForPoint( aPnt, nPageId );
633 if ( nI != -1 && m_nPageId == nPageId )
634 nIndex = nI;
637 return nIndex;
642 sal_Bool VCLXAccessibleTabPage::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
644 OExternalLockGuard aGuard( this );
646 if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
647 throw IndexOutOfBoundsException();
649 return false;
654 sal_Bool VCLXAccessibleTabPage::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
656 OExternalLockGuard aGuard( this );
658 bool bReturn = false;
660 if ( m_pTabControl )
662 Reference< datatransfer::clipboard::XClipboard > xClipboard = m_pTabControl->GetClipboard();
663 if ( xClipboard.is() )
665 OUString sText( getTextRange( nStartIndex, nEndIndex ) );
667 vcl::unohelper::TextDataObject* pDataObj = new vcl::unohelper::TextDataObject( sText );
669 SolarMutexReleaser aReleaser;
670 xClipboard->setContents( pDataObj, NULL );
672 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
673 if( xFlushableClipboard.is() )
674 xFlushableClipboard->flushClipboard();
676 bReturn = true;
680 return bReturn;
685 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */