Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / standard / vclxaccessiblestatusbaritem.cxx
blob586a1d7a525f2e63d122b582fb2ee10b6772a837
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/vclxaccessiblestatusbaritem.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/status.hxx>
36 #include <vcl/controllayout.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 VCLXAccessibleStatusBarItem
51 VCLXAccessibleStatusBarItem::VCLXAccessibleStatusBarItem( StatusBar* pStatusBar, sal_uInt16 nItemId )
52 :AccessibleTextHelper_BASE( new VCLExternalSolarLock() )
53 ,m_pStatusBar( pStatusBar )
54 ,m_nItemId( nItemId )
56 m_pExternalLock = static_cast< VCLExternalSolarLock* >( getExternalLock() );
58 m_sItemName = GetItemName();
59 m_sItemText = GetItemText();
60 m_bShowing = IsShowing();
65 VCLXAccessibleStatusBarItem::~VCLXAccessibleStatusBarItem()
67 delete m_pExternalLock;
68 m_pExternalLock = NULL;
73 bool VCLXAccessibleStatusBarItem::IsShowing()
75 bool bShowing = false;
77 if ( m_pStatusBar )
78 bShowing = m_pStatusBar->IsItemVisible( m_nItemId );
80 return bShowing;
85 void VCLXAccessibleStatusBarItem::SetShowing( bool bShowing )
87 if ( m_bShowing != bShowing )
89 Any aOldValue, aNewValue;
90 if ( m_bShowing )
91 aOldValue <<= AccessibleStateType::SHOWING;
92 else
93 aNewValue <<= AccessibleStateType::SHOWING;
94 m_bShowing = bShowing;
95 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
101 void VCLXAccessibleStatusBarItem::SetItemName( const OUString& sItemName )
103 if ( !m_sItemName.equals( sItemName ) )
105 Any aOldValue, aNewValue;
106 aOldValue <<= m_sItemName;
107 aNewValue <<= sItemName;
108 m_sItemName = sItemName;
109 NotifyAccessibleEvent( AccessibleEventId::NAME_CHANGED, aOldValue, aNewValue );
115 OUString VCLXAccessibleStatusBarItem::GetItemName()
117 OUString sName;
118 if ( m_pStatusBar )
119 sName = m_pStatusBar->GetAccessibleName( m_nItemId );
121 return sName;
126 void VCLXAccessibleStatusBarItem::SetItemText( const OUString& sItemText )
128 Any aOldValue, aNewValue;
129 if ( implInitTextChangedEvent( m_sItemText, sItemText, aOldValue, aNewValue ) )
131 m_sItemText = sItemText;
132 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
138 OUString VCLXAccessibleStatusBarItem::GetItemText()
140 OUString sText;
141 vcl::ControlLayoutData aLayoutData;
142 if ( m_pStatusBar )
144 Rectangle aItemRect = m_pStatusBar->GetItemRect( m_nItemId );
145 m_pStatusBar->RecordLayoutData( &aLayoutData, aItemRect );
146 sText = aLayoutData.m_aDisplayText;
149 return sText;
154 void VCLXAccessibleStatusBarItem::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
156 rStateSet.AddState( AccessibleStateType::ENABLED );
157 rStateSet.AddState( AccessibleStateType::SENSITIVE );
159 rStateSet.AddState( AccessibleStateType::VISIBLE );
161 if ( IsShowing() )
162 rStateSet.AddState( AccessibleStateType::SHOWING );
166 // OCommonAccessibleComponent
169 awt::Rectangle VCLXAccessibleStatusBarItem::implGetBounds() throw (RuntimeException)
171 awt::Rectangle aBounds( 0, 0, 0, 0 );
173 if ( m_pStatusBar )
174 aBounds = AWTRectangle( m_pStatusBar->GetItemRect( m_nItemId ) );
176 return aBounds;
180 // OCommonAccessibleText
183 OUString VCLXAccessibleStatusBarItem::implGetText()
185 return GetItemText();
190 lang::Locale VCLXAccessibleStatusBarItem::implGetLocale()
192 return Application::GetSettings().GetLanguageTag().getLocale();
197 void VCLXAccessibleStatusBarItem::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
199 nStartIndex = 0;
200 nEndIndex = 0;
204 // XInterface
207 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleStatusBarItem, AccessibleTextHelper_BASE, VCLXAccessibleStatusBarItem_BASE )
210 // XTypeProvider
213 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleStatusBarItem, AccessibleTextHelper_BASE, VCLXAccessibleStatusBarItem_BASE )
216 // XComponent
219 void VCLXAccessibleStatusBarItem::disposing()
221 AccessibleTextHelper_BASE::disposing();
223 m_pStatusBar = NULL;
224 m_sItemName.clear();
225 m_sItemText.clear();
229 // XServiceInfo
232 OUString VCLXAccessibleStatusBarItem::getImplementationName() throw (RuntimeException, std::exception)
234 return OUString( "com.sun.star.comp.toolkit.AccessibleStatusBarItem" );
239 sal_Bool VCLXAccessibleStatusBarItem::supportsService( const OUString& rServiceName ) throw (RuntimeException, std::exception)
241 return cppu::supportsService(this, rServiceName);
246 Sequence< OUString > VCLXAccessibleStatusBarItem::getSupportedServiceNames() throw (RuntimeException, std::exception)
248 Sequence< OUString > aNames(1);
249 aNames[0] = "com.sun.star.awt.AccessibleStatusBarItem";
250 return aNames;
254 // XAccessible
257 Reference< XAccessibleContext > VCLXAccessibleStatusBarItem::getAccessibleContext( ) throw (RuntimeException, std::exception)
259 OExternalLockGuard aGuard( this );
261 return this;
265 // XAccessibleContext
268 sal_Int32 VCLXAccessibleStatusBarItem::getAccessibleChildCount() throw (RuntimeException, std::exception)
270 OExternalLockGuard aGuard( this );
272 return 0;
277 Reference< XAccessible > VCLXAccessibleStatusBarItem::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
279 OExternalLockGuard aGuard( this );
281 if ( i < 0 || i >= getAccessibleChildCount() )
282 throw IndexOutOfBoundsException();
284 return Reference< XAccessible >();
289 Reference< XAccessible > VCLXAccessibleStatusBarItem::getAccessibleParent( ) throw (RuntimeException, std::exception)
291 OExternalLockGuard aGuard( this );
293 Reference< XAccessible > xParent;
294 if ( m_pStatusBar )
295 xParent = m_pStatusBar->GetAccessible();
297 return xParent;
302 sal_Int32 VCLXAccessibleStatusBarItem::getAccessibleIndexInParent( ) throw (RuntimeException, std::exception)
304 OExternalLockGuard aGuard( this );
306 sal_Int32 nIndexInParent = -1;
307 if ( m_pStatusBar )
308 nIndexInParent = m_pStatusBar->GetItemPos( m_nItemId );
310 return nIndexInParent;
315 sal_Int16 VCLXAccessibleStatusBarItem::getAccessibleRole( ) throw (RuntimeException, std::exception)
317 OExternalLockGuard aGuard( this );
319 return AccessibleRole::LABEL;
324 OUString VCLXAccessibleStatusBarItem::getAccessibleDescription( ) throw (RuntimeException, std::exception)
326 OExternalLockGuard aGuard( this );
328 OUString sDescription;
329 if ( m_pStatusBar )
330 sDescription = m_pStatusBar->GetHelpText( m_nItemId );
332 return sDescription;
337 OUString VCLXAccessibleStatusBarItem::getAccessibleName( ) throw (RuntimeException, std::exception)
339 OExternalLockGuard aGuard( this );
341 return GetItemName();
346 Reference< XAccessibleRelationSet > VCLXAccessibleStatusBarItem::getAccessibleRelationSet( ) throw (RuntimeException, std::exception)
348 OExternalLockGuard aGuard( this );
350 utl::AccessibleRelationSetHelper* pRelationSetHelper = new utl::AccessibleRelationSetHelper;
351 Reference< XAccessibleRelationSet > xSet = pRelationSetHelper;
352 return xSet;
357 Reference< XAccessibleStateSet > VCLXAccessibleStatusBarItem::getAccessibleStateSet( ) throw (RuntimeException, std::exception)
359 OExternalLockGuard aGuard( this );
361 utl::AccessibleStateSetHelper* pStateSetHelper = new utl::AccessibleStateSetHelper;
362 Reference< XAccessibleStateSet > xSet = pStateSetHelper;
364 if ( !rBHelper.bDisposed && !rBHelper.bInDispose )
366 FillAccessibleStateSet( *pStateSetHelper );
368 else
370 pStateSetHelper->AddState( AccessibleStateType::DEFUNC );
373 return xSet;
378 Locale VCLXAccessibleStatusBarItem::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException, std::exception)
380 OExternalLockGuard aGuard( this );
382 return Application::GetSettings().GetLanguageTag().getLocale();
386 // XAccessibleComponent
389 Reference< XAccessible > VCLXAccessibleStatusBarItem::getAccessibleAtPoint( const awt::Point& ) throw (RuntimeException, std::exception)
391 OExternalLockGuard aGuard( this );
393 return Reference< XAccessible >();
398 void VCLXAccessibleStatusBarItem::grabFocus( ) throw (RuntimeException, std::exception)
400 // no focus for status bar items
405 sal_Int32 VCLXAccessibleStatusBarItem::getForeground( ) throw (RuntimeException, std::exception)
407 OExternalLockGuard aGuard( this );
409 sal_Int32 nColor = 0;
410 Reference< XAccessible > xParent = getAccessibleParent();
411 if ( xParent.is() )
413 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
414 if ( xParentComp.is() )
415 nColor = xParentComp->getForeground();
418 return nColor;
423 sal_Int32 VCLXAccessibleStatusBarItem::getBackground( ) throw (RuntimeException, std::exception)
425 OExternalLockGuard aGuard( this );
427 sal_Int32 nColor = 0;
428 Reference< XAccessible > xParent = getAccessibleParent();
429 if ( xParent.is() )
431 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
432 if ( xParentComp.is() )
433 nColor = xParentComp->getBackground();
436 return nColor;
440 // XAccessibleExtendedComponent
443 Reference< awt::XFont > VCLXAccessibleStatusBarItem::getFont( ) throw (RuntimeException, std::exception)
445 OExternalLockGuard aGuard( this );
447 Reference< awt::XFont > xFont;
448 Reference< XAccessible > xParent = getAccessibleParent();
449 if ( xParent.is() )
451 Reference< XAccessibleExtendedComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
452 if ( xParentComp.is() )
453 xFont = xParentComp->getFont();
456 return xFont;
461 OUString VCLXAccessibleStatusBarItem::getTitledBorderText( ) throw (RuntimeException, std::exception)
463 OExternalLockGuard aGuard( this );
465 return GetItemText();
470 OUString VCLXAccessibleStatusBarItem::getToolTipText( ) throw (RuntimeException, std::exception)
472 OExternalLockGuard aGuard( this );
474 return OUString();
478 // XAccessibleText
481 sal_Int32 VCLXAccessibleStatusBarItem::getCaretPosition() throw (RuntimeException, std::exception)
483 OExternalLockGuard aGuard( this );
485 return -1;
490 sal_Bool VCLXAccessibleStatusBarItem::setCaretPosition( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
492 OExternalLockGuard aGuard( this );
494 if ( !implIsValidRange( nIndex, nIndex, implGetText().getLength() ) )
495 throw IndexOutOfBoundsException();
497 return false;
502 Sequence< PropertyValue > VCLXAccessibleStatusBarItem::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
504 OExternalLockGuard aGuard( this );
506 Sequence< PropertyValue > aValues;
507 OUString sText( implGetText() );
509 if ( !implIsValidIndex( nIndex, sText.getLength() ) )
510 throw IndexOutOfBoundsException();
512 if ( m_pStatusBar )
514 vcl::Font aFont = m_pStatusBar->GetFont();
515 sal_Int32 nBackColor = getBackground();
516 sal_Int32 nColor = getForeground();
517 aValues = CharacterAttributesHelper( aFont, nBackColor, nColor )
518 .GetCharacterAttributes( aRequestedAttributes );
521 return aValues;
526 awt::Rectangle VCLXAccessibleStatusBarItem::getCharacterBounds( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
528 OExternalLockGuard aGuard( this );
530 if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
531 throw IndexOutOfBoundsException();
533 awt::Rectangle aBounds( 0, 0, 0, 0 );
534 if ( m_pStatusBar )
536 vcl::ControlLayoutData aLayoutData;
537 Rectangle aItemRect = m_pStatusBar->GetItemRect( m_nItemId );
538 m_pStatusBar->RecordLayoutData( &aLayoutData, aItemRect );
539 Rectangle aCharRect = aLayoutData.GetCharacterBounds( nIndex );
540 aCharRect.Move( -aItemRect.Left(), -aItemRect.Top() );
541 aBounds = AWTRectangle( aCharRect );
544 return aBounds;
549 sal_Int32 VCLXAccessibleStatusBarItem::getIndexAtPoint( const awt::Point& aPoint ) throw (RuntimeException, std::exception)
551 OExternalLockGuard aGuard( this );
553 sal_Int32 nIndex = -1;
554 if ( m_pStatusBar )
556 vcl::ControlLayoutData aLayoutData;
557 Rectangle aItemRect = m_pStatusBar->GetItemRect( m_nItemId );
558 m_pStatusBar->RecordLayoutData( &aLayoutData, aItemRect );
559 Point aPnt( VCLPoint( aPoint ) );
560 aPnt += aItemRect.TopLeft();
561 nIndex = aLayoutData.GetIndexForPoint( aPnt );
564 return nIndex;
569 sal_Bool VCLXAccessibleStatusBarItem::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
571 OExternalLockGuard aGuard( this );
573 if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
574 throw IndexOutOfBoundsException();
576 return false;
581 sal_Bool VCLXAccessibleStatusBarItem::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
583 OExternalLockGuard aGuard( this );
585 bool bReturn = false;
587 if ( m_pStatusBar )
589 Reference< datatransfer::clipboard::XClipboard > xClipboard = m_pStatusBar->GetClipboard();
590 if ( xClipboard.is() )
592 OUString sText( getTextRange( nStartIndex, nEndIndex ) );
594 vcl::unohelper::TextDataObject* pDataObj = new vcl::unohelper::TextDataObject( sText );
596 SolarMutexReleaser aReleaser;
597 xClipboard->setContents( pDataObj, NULL );
599 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
600 if( xFlushableClipboard.is() )
601 xFlushableClipboard->flushClipboard();
603 bReturn = true;
607 return bReturn;
612 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */