1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/vclxaccessibletoolboxitem.hxx>
21 #include <toolkit/helper/convert.hxx>
22 #include <helper/accresmgr.hxx>
23 #include <strings.hrc>
24 #include <com/sun/star/awt/Rectangle.hpp>
26 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
27 #include <com/sun/star/accessibility/AccessibleRole.hpp>
28 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
29 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
30 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
31 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
32 #include <comphelper/accessiblecontexthelper.hxx>
33 #include <cppuhelper/supportsservice.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/toolbox.hxx>
36 #include <vcl/unohelp2.hxx>
37 #include <vcl/help.hxx>
38 #include <vcl/settings.hxx>
39 #include <unotools/accessiblerelationsethelper.hxx>
40 #include <strings.hxx>
41 #include <sal/log.hxx>
42 #include <i18nlangtag/languagetag.hxx>
44 #include <com/sun/star/accessibility/XAccessibleSelection.hpp>
48 // class VCLXAccessibleToolBoxItem ------------------------------------------
50 using namespace ::com::sun::star::accessibility
;
51 using namespace ::com::sun::star::uno
;
52 using namespace ::com::sun::star::beans
;
53 using namespace ::com::sun::star::lang
;
54 using namespace ::com::sun::star
;
55 using namespace ::comphelper
;
60 VCLXAccessibleToolBoxItem::VCLXAccessibleToolBoxItem( ToolBox
* _pToolBox
, sal_Int32 _nPos
) :
61 m_pToolBox ( _pToolBox
),
62 m_nIndexInParent( _nPos
),
63 m_nRole ( AccessibleRole::PUSH_BUTTON
),
65 m_bHasFocus ( false ),
66 m_bIsChecked ( false ),
67 m_bIndeterminate( false )
71 m_nItemId
= m_pToolBox
->GetItemId( m_nIndexInParent
);
72 m_sOldName
= GetText();
73 m_bIsChecked
= m_pToolBox
->IsItemChecked( m_nItemId
);
74 m_bIndeterminate
= ( m_pToolBox
->GetItemState( m_nItemId
) == TRISTATE_INDET
);
75 ToolBoxItemType eType
= m_pToolBox
->GetItemType( m_nIndexInParent
);
78 case ToolBoxItemType::BUTTON
:
80 ToolBoxItemBits nBits
= m_pToolBox
->GetItemBits( m_nItemId
);
82 (( nBits
& ToolBoxItemBits::DROPDOWN
) == ToolBoxItemBits::DROPDOWN
) ||
83 (( nBits
& ToolBoxItemBits::DROPDOWNONLY
) == ToolBoxItemBits::DROPDOWNONLY
)
85 m_nRole
= AccessibleRole::BUTTON_DROPDOWN
;
87 ( ( nBits
& ToolBoxItemBits::CHECKABLE
) == ToolBoxItemBits::CHECKABLE
) ||
88 ( ( nBits
& ToolBoxItemBits::RADIOCHECK
) == ToolBoxItemBits::RADIOCHECK
) ||
89 ( ( nBits
& ToolBoxItemBits::AUTOCHECK
) == ToolBoxItemBits::AUTOCHECK
)
91 m_nRole
= AccessibleRole::TOGGLE_BUTTON
;
92 else if ( m_pToolBox
->GetItemWindow( m_nItemId
) )
93 m_nRole
= AccessibleRole::PANEL
;
97 case ToolBoxItemType::SPACE
:
98 m_nRole
= AccessibleRole::FILLER
;
101 case ToolBoxItemType::SEPARATOR
:
102 case ToolBoxItemType::BREAK
:
103 m_nRole
= AccessibleRole::SEPARATOR
;
108 SAL_WARN( "accessibility", "unsupported toolbox itemtype" );
113 VCLXAccessibleToolBoxItem::~VCLXAccessibleToolBoxItem()
117 OUString
VCLXAccessibleToolBoxItem::GetText() const
120 // no text for separators and spaces
121 if ( m_pToolBox
&& m_nItemId
> ToolBoxItemId(0) )
123 sRet
= m_pToolBox
->GetItemText( m_nItemId
);
126 sRet
= m_pToolBox
->GetQuickHelpText( m_nItemId
);
129 vcl::Window
* pItemWindow
= m_pToolBox
->GetItemWindow( m_nItemId
);
130 if ( m_nRole
== AccessibleRole::PANEL
&& pItemWindow
&& pItemWindow
->GetAccessible().is() &&
131 pItemWindow
->GetAccessible()->getAccessibleContext().is() )
133 OUString sWinText
= pItemWindow
->GetAccessible()->getAccessibleContext()->getAccessibleName();
134 if (!sWinText
.isEmpty())
144 void VCLXAccessibleToolBoxItem::SetFocus( bool _bFocus
)
146 if ( m_bHasFocus
!= _bFocus
)
151 aOldValue
<<= AccessibleStateType::FOCUSED
;
153 aNewValue
<<= AccessibleStateType::FOCUSED
;
154 m_bHasFocus
= _bFocus
;
155 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
, aNewValue
);
159 void VCLXAccessibleToolBoxItem::SetChecked( bool _bCheck
)
161 if( m_nRole
== AccessibleRole::PANEL
)
163 if ( m_bIsChecked
!= _bCheck
)
168 aOldValue
<<= AccessibleStateType::CHECKED
;
170 aNewValue
<<= AccessibleStateType::CHECKED
;
171 m_bIsChecked
= _bCheck
;
172 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
, aNewValue
);
176 void VCLXAccessibleToolBoxItem::SetIndeterminate( bool _bIndeterminate
)
178 if ( m_bIndeterminate
!= _bIndeterminate
)
180 Any aOldValue
, aNewValue
;
181 if ( m_bIndeterminate
)
182 aOldValue
<<= AccessibleStateType::INDETERMINATE
;
184 aNewValue
<<= AccessibleStateType::INDETERMINATE
;
185 m_bIndeterminate
= _bIndeterminate
;
186 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
, aNewValue
);
190 void VCLXAccessibleToolBoxItem::NameChanged()
192 OUString sNewName
= implGetText();
193 if ( sNewName
!= m_sOldName
)
195 Any aOldValue
, aNewValue
;
196 aOldValue
<<= m_sOldName
;
197 // save new name as old name for next change
198 m_sOldName
= sNewName
;
199 aNewValue
<<= m_sOldName
;
200 NotifyAccessibleEvent( AccessibleEventId::NAME_CHANGED
, aOldValue
, aNewValue
);
204 void VCLXAccessibleToolBoxItem::SetChild( const Reference
< XAccessible
>& _xChild
)
209 void VCLXAccessibleToolBoxItem::NotifyChildEvent( const Reference
< XAccessible
>& _xChild
, bool _bShow
)
211 Any aOld
= _bShow
? Any() : Any( _xChild
);
212 Any aNew
= _bShow
? Any( _xChild
) : Any();
213 NotifyAccessibleEvent( AccessibleEventId::CHILD
, aOld
, aNew
);
216 void VCLXAccessibleToolBoxItem::ToggleEnableState()
218 std::array
<Any
, 2> aOldValue
, aNewValue
;
219 if ( m_pToolBox
->IsItemEnabled( m_nItemId
) )
221 aNewValue
[0] <<= AccessibleStateType::SENSITIVE
;
222 aNewValue
[1] <<= AccessibleStateType::ENABLED
;
226 aOldValue
[0] <<= AccessibleStateType::ENABLED
;
227 aOldValue
[1] <<= AccessibleStateType::SENSITIVE
;
230 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
[0], aNewValue
[0] );
231 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
[1], aNewValue
[1] );
234 awt::Rectangle
VCLXAccessibleToolBoxItem::implGetBounds( )
236 awt::Rectangle aRect
;
238 aRect
= AWTRectangle( m_pToolBox
->GetItemPosRect( m_nIndexInParent
) );
243 OUString
VCLXAccessibleToolBoxItem::implGetText()
248 Locale
VCLXAccessibleToolBoxItem::implGetLocale()
250 return Application::GetSettings().GetUILanguageTag().getLocale();
253 void VCLXAccessibleToolBoxItem::implGetSelection( sal_Int32
& nStartIndex
, sal_Int32
& nEndIndex
)
261 Any SAL_CALL
VCLXAccessibleToolBoxItem::queryInterface( const Type
& _rType
)
263 // #i33611# - toolbox buttons without text don't support XAccessibleText
264 if ( _rType
== cppu::UnoType
<XAccessibleText
>::get()
265 && ( !m_pToolBox
|| m_pToolBox
->GetButtonType() == ButtonType::SYMBOLONLY
) )
268 return ImplInheritanceHelper::queryInterface( _rType
);
273 void SAL_CALL
VCLXAccessibleToolBoxItem::disposing()
275 AccessibleTextHelper_BASE::disposing();
276 m_pToolBox
= nullptr;
281 OUString
VCLXAccessibleToolBoxItem::getImplementationName()
283 return "com.sun.star.comp.toolkit.AccessibleToolBoxItem";
286 sal_Bool
VCLXAccessibleToolBoxItem::supportsService( const OUString
& rServiceName
)
288 return cppu::supportsService(this, rServiceName
);
291 Sequence
< OUString
> VCLXAccessibleToolBoxItem::getSupportedServiceNames()
293 return {"com.sun.star.accessibility.AccessibleContext",
294 "com.sun.star.accessibility.AccessibleComponent",
295 "com.sun.star.accessibility.AccessibleExtendedComponent",
296 "com.sun.star.accessibility.AccessibleToolBoxItem"};
301 Reference
< XAccessibleContext
> SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleContext( )
306 // XAccessibleContext
308 sal_Int64 SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleChildCount( )
310 OContextEntryGuard
aGuard( this );
312 return m_xChild
.is() ? 1 : 0;
315 Reference
< XAccessible
> SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleChild( sal_Int64 i
)
317 OContextEntryGuard
aGuard( this );
319 // no child -> so index is out of bounds
320 if ( !m_xChild
.is() || i
!= 0 )
321 throw IndexOutOfBoundsException();
326 Reference
< XAccessible
> SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleParent( )
328 OContextEntryGuard
aGuard( this );
330 return m_pToolBox
->GetAccessible();
333 sal_Int64 SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleIndexInParent( )
335 OContextEntryGuard
aGuard( this );
337 return m_nIndexInParent
;
340 sal_Int16 SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleRole( )
342 OContextEntryGuard
aGuard( this );
347 OUString SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleDescription( )
349 OExternalLockGuard
aGuard( this );
351 if (m_nRole
== AccessibleRole::PANEL
&& m_xChild
.is())
353 return AccResId( RID_STR_ACC_PANEL_DESCRIPTION
);
357 OUString sDescription
;
359 sDescription
= m_pToolBox
->GetHelpText( m_nItemId
);
364 OUString SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleName( )
366 OExternalLockGuard
aGuard( this );
368 // entry text == accessible name
372 Reference
< XAccessibleRelationSet
> SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleRelationSet( )
374 OContextEntryGuard
aGuard( this );
376 return new utl::AccessibleRelationSetHelper
;
379 sal_Int64 SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleStateSet( )
381 OExternalLockGuard
aGuard( this );
383 sal_Int64 nStateSet
= 0;
385 if ( m_pToolBox
&& !rBHelper
.bDisposed
&& !rBHelper
.bInDispose
)
387 nStateSet
|= AccessibleStateType::FOCUSABLE
;
388 if ( m_bIsChecked
&& m_nRole
!= AccessibleRole::PANEL
)
389 nStateSet
|= AccessibleStateType::CHECKED
;
390 if ( m_bIndeterminate
)
391 nStateSet
|= AccessibleStateType::INDETERMINATE
;
392 if ( m_pToolBox
->IsEnabled() && m_pToolBox
->IsItemEnabled( m_nItemId
) )
394 nStateSet
|= AccessibleStateType::ENABLED
;
395 nStateSet
|= AccessibleStateType::SENSITIVE
;
397 if ( m_pToolBox
->IsItemVisible( m_nItemId
) )
398 nStateSet
|= AccessibleStateType::VISIBLE
;
399 if ( m_pToolBox
->IsItemReallyVisible( m_nItemId
) )
400 nStateSet
|= AccessibleStateType::SHOWING
;
402 nStateSet
|= AccessibleStateType::FOCUSED
;
405 nStateSet
|= AccessibleStateType::DEFUNC
;
412 OUString
VCLXAccessibleToolBoxItem::getText()
414 OExternalLockGuard
aGuard( this );
419 sal_Int32
VCLXAccessibleToolBoxItem::getCharacterCount()
421 return GetText().getLength();
424 sal_Unicode
VCLXAccessibleToolBoxItem::getCharacter( sal_Int32 nIndex
)
426 OExternalLockGuard
aGuard( this );
428 return OCommonAccessibleText::implGetCharacter( GetText(), nIndex
);
431 OUString
VCLXAccessibleToolBoxItem::getTextRange( sal_Int32 nStartIndex
, sal_Int32 nEndIndex
)
433 OExternalLockGuard
aGuard( this );
435 return OCommonAccessibleText::implGetTextRange( GetText(), nStartIndex
, nEndIndex
);
438 sal_Int32 SAL_CALL
VCLXAccessibleToolBoxItem::getCaretPosition()
443 sal_Bool SAL_CALL
VCLXAccessibleToolBoxItem::setCaretPosition( sal_Int32 nIndex
)
445 OExternalLockGuard
aGuard( this );
447 if ( !implIsValidRange( nIndex
, nIndex
, GetText().getLength() ) )
448 throw IndexOutOfBoundsException();
453 Sequence
< PropertyValue
> SAL_CALL
VCLXAccessibleToolBoxItem::getCharacterAttributes( sal_Int32 nIndex
, const Sequence
< OUString
>& )
455 OExternalLockGuard
aGuard( this );
457 OUString
sText( implGetText() );
459 if ( !implIsValidIndex( nIndex
, sText
.getLength() ) )
460 throw IndexOutOfBoundsException();
462 return Sequence
< PropertyValue
>();
465 awt::Rectangle SAL_CALL
VCLXAccessibleToolBoxItem::getCharacterBounds( sal_Int32 nIndex
)
467 OExternalLockGuard
aGuard( this );
469 OUString
sText( implGetText() );
471 if ( !implIsValidIndex( nIndex
, sText
.getLength() ) )
472 throw IndexOutOfBoundsException();
474 awt::Rectangle
aBounds( 0, 0, 0, 0 );
475 if ( m_pToolBox
&& m_pToolBox
->GetButtonType() != ButtonType::SYMBOLONLY
) // symbol buttons have no character bounds
477 tools::Rectangle aCharRect
= m_pToolBox
->GetCharacterBounds( m_nItemId
, nIndex
);
478 tools::Rectangle aItemRect
= m_pToolBox
->GetItemRect( m_nItemId
);
479 aCharRect
.Move( -aItemRect
.Left(), -aItemRect
.Top() );
480 aBounds
= AWTRectangle( aCharRect
);
486 sal_Int32 SAL_CALL
VCLXAccessibleToolBoxItem::getIndexAtPoint( const awt::Point
& aPoint
)
488 OExternalLockGuard
aGuard( this );
490 sal_Int32 nIndex
= -1;
491 if ( m_pToolBox
&& m_pToolBox
->GetButtonType() != ButtonType::SYMBOLONLY
) // symbol buttons have no character bounds
493 ToolBoxItemId nItemId
;
494 tools::Rectangle aItemRect
= m_pToolBox
->GetItemRect( m_nItemId
);
495 Point
aPnt( VCLPoint( aPoint
) );
496 aPnt
+= aItemRect
.TopLeft();
497 sal_Int32 nIdx
= m_pToolBox
->GetIndexForPoint( aPnt
, nItemId
);
498 if ( nIdx
!= -1 && nItemId
== m_nItemId
)
505 sal_Bool SAL_CALL
VCLXAccessibleToolBoxItem::setSelection( sal_Int32 nStartIndex
, sal_Int32 nEndIndex
)
507 OExternalLockGuard
aGuard( this );
509 if ( !implIsValidRange( nStartIndex
, nEndIndex
, implGetText().getLength() ) )
510 throw IndexOutOfBoundsException();
515 sal_Bool SAL_CALL
VCLXAccessibleToolBoxItem::copyText( sal_Int32 nStartIndex
, sal_Int32 nEndIndex
)
517 OExternalLockGuard
aGuard( this );
519 if ( !implIsValidRange( nStartIndex
, nEndIndex
, implGetText().getLength() ) )
520 throw IndexOutOfBoundsException();
522 bool bReturn
= false;
526 Reference
< datatransfer::clipboard::XClipboard
> xClipboard
= m_pToolBox
->GetClipboard();
527 if ( xClipboard
.is() )
529 OUString
sText( OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex
, nEndIndex
) );
531 rtl::Reference
<vcl::unohelper::TextDataObject
> pDataObj
= new vcl::unohelper::TextDataObject( sText
);
533 SolarMutexReleaser aReleaser
;
534 xClipboard
->setContents( pDataObj
, nullptr );
536 Reference
< datatransfer::clipboard::XFlushableClipboard
> xFlushableClipboard( xClipboard
, uno::UNO_QUERY
);
537 if( xFlushableClipboard
.is() )
538 xFlushableClipboard
->flushClipboard();
547 sal_Bool
VCLXAccessibleToolBoxItem::scrollSubstringTo( sal_Int32
, sal_Int32
, AccessibleScrollType
)
552 // XAccessibleComponent
554 Reference
< XAccessible
> SAL_CALL
VCLXAccessibleToolBoxItem::getAccessibleAtPoint( const awt::Point
& )
556 return Reference
< XAccessible
>();
559 void SAL_CALL
VCLXAccessibleToolBoxItem::grabFocus( )
561 Reference
< XAccessible
> xParent(getAccessibleParent());
565 Reference
< XAccessibleSelection
> rxAccessibleSelection(xParent
->getAccessibleContext(), UNO_QUERY
);
567 if ( rxAccessibleSelection
.is() )
569 rxAccessibleSelection
-> selectAccessibleChild ( getAccessibleIndexInParent() );
574 sal_Int32 SAL_CALL
VCLXAccessibleToolBoxItem::getForeground( )
576 OExternalLockGuard
aGuard( this );
580 nColor
= m_pToolBox
->GetControlForeground();
582 return sal_Int32(nColor
);
585 sal_Int32 SAL_CALL
VCLXAccessibleToolBoxItem::getBackground( )
587 OExternalLockGuard
aGuard( this );
591 nColor
= m_pToolBox
->GetControlBackground();
593 return sal_Int32(nColor
);
596 // XAccessibleExtendedComponent
597 Reference
< awt::XFont
> SAL_CALL
VCLXAccessibleToolBoxItem::getFont( )
599 return uno::Reference
< awt::XFont
>();
602 OUString SAL_CALL
VCLXAccessibleToolBoxItem::getTitledBorderText( )
604 OExternalLockGuard
aGuard( this );
608 sRet
= m_pToolBox
->GetItemText( m_nItemId
);
613 OUString SAL_CALL
VCLXAccessibleToolBoxItem::getToolTipText( )
615 OExternalLockGuard
aGuard( this );
620 if ( Help::IsExtHelpEnabled() )
621 sRet
= m_pToolBox
->GetHelpText( m_nItemId
);
623 sRet
= m_pToolBox
->GetQuickHelpText( m_nItemId
);
624 if ( sRet
.isEmpty() )
625 // no help text set, so use item text
626 sRet
= m_pToolBox
->GetItemText( m_nItemId
);
633 sal_Int32
VCLXAccessibleToolBoxItem::getAccessibleActionCount( )
635 // only one action -> "Click"
639 sal_Bool
VCLXAccessibleToolBoxItem::doAccessibleAction ( sal_Int32 nIndex
)
641 OExternalLockGuard
aGuard( this );
644 throw IndexOutOfBoundsException();
647 m_pToolBox
->TriggerItem( m_nItemId
);
652 OUString
VCLXAccessibleToolBoxItem::getAccessibleActionDescription ( sal_Int32 nIndex
)
654 OExternalLockGuard
aGuard( this );
657 throw IndexOutOfBoundsException();
659 return RID_STR_ACC_ACTION_CLICK
;
662 Reference
< XAccessibleKeyBinding
> VCLXAccessibleToolBoxItem::getAccessibleActionKeyBinding( sal_Int32 nIndex
)
664 OContextEntryGuard
aGuard( this );
667 throw IndexOutOfBoundsException();
669 return Reference
< XAccessibleKeyBinding
>();
674 Any
VCLXAccessibleToolBoxItem::getCurrentValue( )
676 OExternalLockGuard
aGuard( this );
680 aValue
<<= static_cast<sal_Int32
>(m_pToolBox
->IsItemChecked( m_nItemId
));
682 if( m_nRole
== AccessibleRole::PANEL
)
683 aValue
<<= sal_Int32(0);
687 sal_Bool
VCLXAccessibleToolBoxItem::setCurrentValue( const Any
& aNumber
)
689 OExternalLockGuard
aGuard( this );
691 bool bReturn
= false;
695 sal_Int32 nValue
= 0;
696 OSL_VERIFY( aNumber
>>= nValue
);
700 else if ( nValue
> 1 )
703 m_pToolBox
->CheckItem( m_nItemId
, nValue
== 1 );
710 Any
VCLXAccessibleToolBoxItem::getMaximumValue( )
712 return Any(sal_Int32(1));
715 Any
VCLXAccessibleToolBoxItem::getMinimumValue( )
717 return Any(sal_Int32(0));
720 Any
VCLXAccessibleToolBoxItem::getMinimumIncrement( )
722 return Any(sal_Int32(1));
726 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */