LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / accessibility / source / extended / accessibletabbarpagelist.cxx
blob3c7f9976e0af73312477eaffe8121877cc53d10a
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 <extended/accessibletabbarpagelist.hxx>
21 #include <svtools/tabbar.hxx>
22 #include <extended/accessibletabbarpage.hxx>
23 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
24 #include <com/sun/star/accessibility/AccessibleRole.hpp>
25 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
26 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <unotools/accessiblestatesethelper.hxx>
29 #include <unotools/accessiblerelationsethelper.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/settings.hxx>
32 #include <toolkit/helper/convert.hxx>
33 #include <i18nlangtag/languagetag.hxx>
36 namespace accessibility
40 using namespace ::com::sun::star::accessibility;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star;
44 using namespace ::comphelper;
49 AccessibleTabBarPageList::AccessibleTabBarPageList( TabBar* pTabBar, sal_Int32 nIndexInParent )
50 :AccessibleTabBarBase( pTabBar )
51 ,m_nIndexInParent( nIndexInParent )
53 if ( m_pTabBar )
54 m_aAccessibleChildren.assign( m_pTabBar->GetPageCount(), Reference< XAccessible >() );
58 void AccessibleTabBarPageList::UpdateShowing( bool bShowing )
60 for (const Reference<XAccessible>& xChild : m_aAccessibleChildren)
62 if ( xChild.is() )
64 AccessibleTabBarPage* pAccessibleTabBarPage = static_cast< AccessibleTabBarPage* >( xChild.get() );
65 if ( pAccessibleTabBarPage )
66 pAccessibleTabBarPage->SetShowing( bShowing );
72 void AccessibleTabBarPageList::UpdateSelected( sal_Int32 i, bool bSelected )
74 NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, Any(), Any() );
76 if ( i >= 0 && i < static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
78 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
79 if ( xChild.is() )
81 AccessibleTabBarPage* pAccessibleTabBarPage = static_cast< AccessibleTabBarPage* >( xChild.get() );
82 if ( pAccessibleTabBarPage )
83 pAccessibleTabBarPage->SetSelected( bSelected );
89 void AccessibleTabBarPageList::UpdatePageText( sal_Int32 i )
91 if ( i < 0 || i >= static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
92 return;
94 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
95 if ( xChild.is() )
97 AccessibleTabBarPage* pAccessibleTabBarPage = static_cast< AccessibleTabBarPage* >( xChild.get() );
98 if ( pAccessibleTabBarPage && m_pTabBar )
100 OUString sPageText = m_pTabBar->GetPageText( m_pTabBar->GetPageId( static_cast<sal_uInt16>(i) ) );
101 pAccessibleTabBarPage->SetPageText( sPageText );
107 void AccessibleTabBarPageList::InsertChild( sal_Int32 i )
109 if ( i < 0 || i > static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
110 return;
112 // insert entry in child list
113 m_aAccessibleChildren.insert( m_aAccessibleChildren.begin() + i, Reference< XAccessible >() );
115 // send accessible child event
116 Reference< XAccessible > xChild( getAccessibleChild( i ) );
117 if ( xChild.is() )
119 Any aOldValue, aNewValue;
120 aNewValue <<= xChild;
121 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
126 void AccessibleTabBarPageList::RemoveChild( sal_Int32 i )
128 if ( i < 0 || i >= static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
129 return;
131 // get the accessible of the removed page
132 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
134 // remove entry in child list
135 m_aAccessibleChildren.erase( m_aAccessibleChildren.begin() + i );
137 // send accessible child event
138 if ( xChild.is() )
140 Any aOldValue, aNewValue;
141 aOldValue <<= xChild;
142 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
144 Reference< XComponent > xComponent( xChild, UNO_QUERY );
145 if ( xComponent.is() )
146 xComponent->dispose();
151 void AccessibleTabBarPageList::MoveChild( sal_Int32 i, sal_Int32 j )
153 if ( !(i >= 0 && i < static_cast<sal_Int32>(m_aAccessibleChildren.size()) &&
154 j >= 0 && j <= static_cast<sal_Int32>(m_aAccessibleChildren.size())) )
155 return;
157 if ( i < j )
158 --j;
160 // get the accessible of the moved page
161 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
163 // remove entry in child list at old position
164 m_aAccessibleChildren.erase( m_aAccessibleChildren.begin() + i );
166 // insert entry in child list at new position
167 m_aAccessibleChildren.insert( m_aAccessibleChildren.begin() + j, xChild );
171 void AccessibleTabBarPageList::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
173 switch ( rVclWindowEvent.GetId() )
175 case VclEventId::WindowEnabled:
177 Any aNewValue;
178 aNewValue <<= AccessibleStateType::SENSITIVE;
179 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, Any(), aNewValue );
180 aNewValue <<= AccessibleStateType::ENABLED;
181 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, Any(), aNewValue );
183 break;
184 case VclEventId::WindowDisabled:
186 Any aOldValue;
187 aOldValue <<= AccessibleStateType::ENABLED;
188 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, Any() );
189 aOldValue <<= AccessibleStateType::SENSITIVE;
190 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, Any() );
192 break;
193 case VclEventId::WindowShow:
195 Any aOldValue, aNewValue;
196 aNewValue <<= AccessibleStateType::SHOWING;
197 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
198 UpdateShowing( true );
200 break;
201 case VclEventId::WindowHide:
203 Any aOldValue, aNewValue;
204 aOldValue <<= AccessibleStateType::SHOWING;
205 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
206 UpdateShowing( false );
208 break;
209 case VclEventId::TabbarPageSelected:
211 // do nothing
213 break;
214 case VclEventId::TabbarPageActivated:
216 if ( m_pTabBar )
218 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
219 sal_uInt16 nPagePos = m_pTabBar->GetPagePos( nPageId );
220 UpdateSelected( nPagePos, true );
223 break;
224 case VclEventId::TabbarPageDeactivated:
226 if ( m_pTabBar )
228 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
229 sal_uInt16 nPagePos = m_pTabBar->GetPagePos( nPageId );
230 UpdateSelected( nPagePos, false );
233 break;
234 case VclEventId::TabbarPageInserted:
236 if ( m_pTabBar )
238 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
239 sal_uInt16 nPagePos = m_pTabBar->GetPagePos( nPageId );
240 InsertChild( nPagePos );
243 break;
244 case VclEventId::TabbarPageRemoved:
246 if ( m_pTabBar )
248 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
250 if ( nPageId == TabBar::PAGE_NOT_FOUND )
252 for ( sal_Int32 i = m_aAccessibleChildren.size() - 1; i >= 0; --i )
253 RemoveChild( i );
255 else
257 for ( sal_Int32 i = 0, nCount = getAccessibleChildCount(); i < nCount; ++i )
259 Reference< XAccessible > xChild( getAccessibleChild( i ) );
260 if ( xChild.is() )
262 AccessibleTabBarPage* pAccessibleTabBarPage = static_cast< AccessibleTabBarPage* >( xChild.get() );
263 if ( pAccessibleTabBarPage && pAccessibleTabBarPage->GetPageId() == nPageId )
265 RemoveChild( i );
266 break;
273 break;
274 case VclEventId::TabbarPageMoved:
276 Pair* pPair = static_cast<Pair*>(rVclWindowEvent.GetData());
277 if ( pPair )
278 MoveChild( pPair->A(), pPair->B() );
280 break;
281 case VclEventId::TabbarPageTextChanged:
283 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
284 sal_uInt16 nPagePos = m_pTabBar->GetPagePos( nPageId );
285 UpdatePageText( nPagePos );
287 break;
288 default:
290 AccessibleTabBarBase::ProcessWindowEvent( rVclWindowEvent );
292 break;
297 void AccessibleTabBarPageList::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
299 if ( !m_pTabBar )
300 return;
302 if ( m_pTabBar->IsEnabled() )
304 rStateSet.AddState( AccessibleStateType::ENABLED );
305 rStateSet.AddState( AccessibleStateType::SENSITIVE );
308 rStateSet.AddState( AccessibleStateType::VISIBLE );
310 if ( m_pTabBar->IsVisible() )
311 rStateSet.AddState( AccessibleStateType::SHOWING );
315 // OCommonAccessibleComponent
318 awt::Rectangle AccessibleTabBarPageList::implGetBounds()
320 awt::Rectangle aBounds;
321 if ( m_pTabBar )
322 aBounds = AWTRectangle( m_pTabBar->GetPageArea() );
324 return aBounds;
328 // XInterface
331 IMPLEMENT_FORWARD_XINTERFACE2( AccessibleTabBarPageList, OAccessibleExtendedComponentHelper, AccessibleTabBarPageList_BASE )
334 // XTypeProvider
337 IMPLEMENT_FORWARD_XTYPEPROVIDER2( AccessibleTabBarPageList, OAccessibleExtendedComponentHelper, AccessibleTabBarPageList_BASE )
340 // XComponent
343 void AccessibleTabBarPageList::disposing()
345 AccessibleTabBarBase::disposing();
347 // dispose all children
348 for (const Reference<XAccessible>& i : m_aAccessibleChildren)
350 Reference< XComponent > xComponent( i, UNO_QUERY );
351 if ( xComponent.is() )
352 xComponent->dispose();
354 m_aAccessibleChildren.clear();
358 // XServiceInfo
361 OUString AccessibleTabBarPageList::getImplementationName()
363 return "com.sun.star.comp.svtools.AccessibleTabBarPageList";
367 sal_Bool AccessibleTabBarPageList::supportsService( const OUString& rServiceName )
369 return cppu::supportsService(this, rServiceName);
373 Sequence< OUString > AccessibleTabBarPageList::getSupportedServiceNames()
375 return { "com.sun.star.awt.AccessibleTabBarPageList" };
379 // XAccessible
382 Reference< XAccessibleContext > AccessibleTabBarPageList::getAccessibleContext( )
384 OExternalLockGuard aGuard( this );
386 return this;
390 // XAccessibleContext
393 sal_Int32 AccessibleTabBarPageList::getAccessibleChildCount()
395 OExternalLockGuard aGuard( this );
397 return m_aAccessibleChildren.size();
401 Reference< XAccessible > AccessibleTabBarPageList::getAccessibleChild( sal_Int32 i )
403 OExternalLockGuard aGuard( this );
405 if ( i < 0 || i >= getAccessibleChildCount() )
406 throw IndexOutOfBoundsException();
408 Reference< XAccessible > xChild = m_aAccessibleChildren[i];
409 if ( !xChild.is() )
411 if ( m_pTabBar )
413 sal_uInt16 nPageId = m_pTabBar->GetPageId( static_cast<sal_uInt16>(i) );
415 xChild = new AccessibleTabBarPage( m_pTabBar, nPageId, this );
417 // insert into child list
418 m_aAccessibleChildren[i] = xChild;
422 return xChild;
426 Reference< XAccessible > AccessibleTabBarPageList::getAccessibleParent( )
428 OExternalLockGuard aGuard( this );
430 Reference< XAccessible > xParent;
431 if ( m_pTabBar )
432 xParent = m_pTabBar->GetAccessible();
434 return xParent;
438 sal_Int32 AccessibleTabBarPageList::getAccessibleIndexInParent( )
440 OExternalLockGuard aGuard( this );
442 return m_nIndexInParent;
446 sal_Int16 AccessibleTabBarPageList::getAccessibleRole( )
448 return AccessibleRole::PAGE_TAB_LIST;
452 OUString AccessibleTabBarPageList::getAccessibleDescription( )
454 return OUString();
458 OUString AccessibleTabBarPageList::getAccessibleName( )
460 return OUString();
464 Reference< XAccessibleRelationSet > AccessibleTabBarPageList::getAccessibleRelationSet( )
466 OExternalLockGuard aGuard( this );
468 return new utl::AccessibleRelationSetHelper;
472 Reference< XAccessibleStateSet > AccessibleTabBarPageList::getAccessibleStateSet( )
474 OExternalLockGuard aGuard( this );
476 rtl::Reference<utl::AccessibleStateSetHelper> pStateSetHelper = new utl::AccessibleStateSetHelper;
478 if ( !rBHelper.bDisposed && !rBHelper.bInDispose )
480 FillAccessibleStateSet( *pStateSetHelper );
482 else
484 pStateSetHelper->AddState( AccessibleStateType::DEFUNC );
487 return pStateSetHelper;
491 Locale AccessibleTabBarPageList::getLocale( )
493 OExternalLockGuard aGuard( this );
495 return Application::GetSettings().GetLanguageTag().getLocale();
499 // XAccessibleComponent
502 Reference< XAccessible > AccessibleTabBarPageList::getAccessibleAtPoint( const awt::Point& rPoint )
504 OExternalLockGuard aGuard( this );
506 Reference< XAccessible > xChild;
507 for ( size_t i = 0; i < m_aAccessibleChildren.size(); ++i )
509 Reference< XAccessible > xAcc = getAccessibleChild( i );
510 if ( xAcc.is() )
512 Reference< XAccessibleComponent > xComp( xAcc->getAccessibleContext(), UNO_QUERY );
513 if ( xComp.is() )
515 tools::Rectangle aRect = VCLRectangle( xComp->getBounds() );
516 Point aPos = VCLPoint( rPoint );
517 if ( aRect.Contains( aPos ) )
519 xChild = xAcc;
520 break;
526 return xChild;
530 void AccessibleTabBarPageList::grabFocus( )
532 // no focus
536 sal_Int32 AccessibleTabBarPageList::getForeground( )
538 OExternalLockGuard aGuard( this );
540 sal_Int32 nColor = 0;
541 Reference< XAccessible > xParent = getAccessibleParent();
542 if ( xParent.is() )
544 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
545 if ( xParentComp.is() )
546 nColor = xParentComp->getForeground();
549 return nColor;
553 sal_Int32 AccessibleTabBarPageList::getBackground( )
555 OExternalLockGuard aGuard( this );
557 sal_Int32 nColor = 0;
558 Reference< XAccessible > xParent = getAccessibleParent();
559 if ( xParent.is() )
561 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
562 if ( xParentComp.is() )
563 nColor = xParentComp->getBackground();
566 return nColor;
570 // XAccessibleExtendedComponent
573 Reference< awt::XFont > AccessibleTabBarPageList::getFont( )
575 OExternalLockGuard aGuard( this );
577 Reference< awt::XFont > xFont;
578 Reference< XAccessible > xParent = getAccessibleParent();
579 if ( xParent.is() )
581 Reference< XAccessibleExtendedComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
582 if ( xParentComp.is() )
583 xFont = xParentComp->getFont();
586 return xFont;
590 OUString AccessibleTabBarPageList::getTitledBorderText( )
592 return OUString();
596 OUString AccessibleTabBarPageList::getToolTipText( )
598 return OUString();
602 // XAccessibleSelection
605 void AccessibleTabBarPageList::selectAccessibleChild( sal_Int32 nChildIndex )
607 OExternalLockGuard aGuard( this );
609 if ( nChildIndex < 0 || nChildIndex >= getAccessibleChildCount() )
610 throw IndexOutOfBoundsException();
612 if ( m_pTabBar )
614 m_pTabBar->SetCurPageId( m_pTabBar->GetPageId( static_cast<sal_uInt16>(nChildIndex) ) );
615 m_pTabBar->PaintImmediately();
616 m_pTabBar->ActivatePage();
617 m_pTabBar->Select();
622 sal_Bool AccessibleTabBarPageList::isAccessibleChildSelected( sal_Int32 nChildIndex )
624 OExternalLockGuard aGuard( this );
626 if ( nChildIndex < 0 || nChildIndex >= getAccessibleChildCount() )
627 throw IndexOutOfBoundsException();
629 bool bSelected = false;
630 if ( m_pTabBar && m_pTabBar->GetCurPageId() == m_pTabBar->GetPageId( static_cast<sal_uInt16>(nChildIndex) ) )
631 bSelected = true;
633 return bSelected;
637 void AccessibleTabBarPageList::clearAccessibleSelection( )
639 // This method makes no sense in a TabBar, and so does nothing.
643 void AccessibleTabBarPageList::selectAllAccessibleChildren( )
645 selectAccessibleChild( 0 );
649 sal_Int32 AccessibleTabBarPageList::getSelectedAccessibleChildCount( )
651 return 1;
655 Reference< XAccessible > AccessibleTabBarPageList::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex )
657 OExternalLockGuard aGuard( this );
659 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
660 throw IndexOutOfBoundsException();
662 Reference< XAccessible > xChild;
664 for ( sal_Int32 i = 0, j = 0, nCount = getAccessibleChildCount(); i < nCount; i++ )
666 if ( isAccessibleChildSelected( i ) && ( j++ == nSelectedChildIndex ) )
668 xChild = getAccessibleChild( i );
669 break;
673 return xChild;
677 void AccessibleTabBarPageList::deselectAccessibleChild( sal_Int32 nChildIndex )
679 OExternalLockGuard aGuard( this );
681 if ( nChildIndex < 0 || nChildIndex >= getAccessibleChildCount() )
682 throw IndexOutOfBoundsException();
684 // This method makes no sense in a TabBar, and so does nothing.
688 } // namespace accessibility
691 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */