LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / accessibility / source / extended / accessibleiconchoicectrl.cxx
blob9ad90c3879ebca03f2e956814b6eb167a7386f63
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/accessibleiconchoicectrl.hxx>
21 #include <extended/accessibleiconchoicectrlentry.hxx>
22 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
23 #include <com/sun/star/accessibility/AccessibleRole.hpp>
24 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <unotools/accessiblestatesethelper.hxx>
27 #include <vcl/toolkit/ivctrl.hxx>
28 #include <cppuhelper/supportsservice.hxx>
31 namespace accessibility
35 // class AccessibleIconChoiceCtrl ----------------------------------------------
37 using namespace ::com::sun::star::accessibility;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star;
43 // Ctor() and Dtor()
45 AccessibleIconChoiceCtrl::AccessibleIconChoiceCtrl( SvtIconChoiceCtrl const & _rIconCtrl, const Reference< XAccessible >& _xParent ) :
46 VCLXAccessibleComponent( _rIconCtrl.GetWindowPeer() ),
47 m_xParent ( _xParent )
51 IMPLEMENT_FORWARD_XINTERFACE2(AccessibleIconChoiceCtrl, VCLXAccessibleComponent, AccessibleIconChoiceCtrl_BASE)
52 IMPLEMENT_FORWARD_XTYPEPROVIDER2(AccessibleIconChoiceCtrl, VCLXAccessibleComponent, AccessibleIconChoiceCtrl_BASE)
54 void AccessibleIconChoiceCtrl::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
56 if ( !isAlive() )
57 return;
59 switch ( rVclWindowEvent.GetId() )
61 case VclEventId::ListboxSelect :
63 // First send an event that tells the listeners of a
64 // modified selection. The active descendant event is
65 // send after that so that the receiving AT has time to
66 // read the text or name of the active child.
67 // NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, Any(), Any() );
69 if ( getCtrl() && getCtrl()->HasFocus() )
71 SvxIconChoiceCtrlEntry* pEntry = static_cast< SvxIconChoiceCtrlEntry* >( rVclWindowEvent.GetData() );
72 if ( pEntry )
74 sal_Int32 nPos = getCtrl()->GetEntryListPos( pEntry );
75 Reference< XAccessible > xChild = new AccessibleIconChoiceCtrlEntry( *getCtrl(), nPos, this );
76 uno::Any aOldValue, aNewValue;
77 aNewValue <<= xChild;
78 NotifyAccessibleEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldValue, aNewValue );
80 NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, aOldValue, aNewValue );
84 break;
86 case VclEventId::WindowGetFocus :
88 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
89 if ( pCtrl && pCtrl->HasFocus() )
91 SvxIconChoiceCtrlEntry* pEntry = static_cast< SvxIconChoiceCtrlEntry* >( rVclWindowEvent.GetData() );
92 if ( pEntry == nullptr )
94 pEntry = getCtrl()->GetSelectedEntry();
96 if ( pEntry )
98 sal_Int32 nPos = pCtrl->GetEntryListPos( pEntry );
99 Reference< XAccessible > xChild = new AccessibleIconChoiceCtrlEntry( *pCtrl, nPos, this );
100 uno::Any aOldValue, aNewValue;
101 aNewValue <<= xChild;
102 NotifyAccessibleEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldValue, aNewValue );
103 NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, aOldValue, aNewValue );
106 break;
108 default:
109 VCLXAccessibleComponent::ProcessWindowChildEvent (rVclWindowEvent);
113 // XComponent
115 void SAL_CALL AccessibleIconChoiceCtrl::disposing()
117 ::osl::MutexGuard aGuard( m_aMutex );
119 m_xParent = nullptr;
122 // XServiceInfo
124 OUString SAL_CALL AccessibleIconChoiceCtrl::getImplementationName()
126 return "com.sun.star.comp.svtools.AccessibleIconChoiceControl";
129 Sequence< OUString > SAL_CALL AccessibleIconChoiceCtrl::getSupportedServiceNames()
131 return {"com.sun.star.accessibility.AccessibleContext",
132 "com.sun.star.accessibility.AccessibleComponent",
133 "com.sun.star.awt.AccessibleIconChoiceControl"};
136 sal_Bool SAL_CALL AccessibleIconChoiceCtrl::supportsService( const OUString& _rServiceName )
138 return cppu::supportsService(this, _rServiceName);
141 // XAccessible
143 Reference< XAccessibleContext > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleContext( )
145 ensureAlive();
146 return this;
149 // XAccessibleContext
151 sal_Int32 SAL_CALL AccessibleIconChoiceCtrl::getAccessibleChildCount( )
153 ::comphelper::OExternalLockGuard aGuard( this );
155 return getCtrl()->GetEntryCount();
158 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleChild( sal_Int32 i )
160 ::comphelper::OExternalLockGuard aGuard( this );
162 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
163 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry(i);
164 if ( !pEntry )
165 throw RuntimeException("getAccessibleChild: Entry "
166 + OUString::number(i) + " not found",
167 static_cast<css::lang::XTypeProvider*>(
168 static_cast<VCLXAccessibleComponent_BASE*>(this)));
170 return new AccessibleIconChoiceCtrlEntry( *pCtrl, i, this );
173 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleParent( )
175 ::osl::MutexGuard aGuard( m_aMutex );
177 ensureAlive();
178 return m_xParent;
181 sal_Int16 SAL_CALL AccessibleIconChoiceCtrl::getAccessibleRole( )
183 //return AccessibleRole::TREE;
184 return AccessibleRole::LIST;
187 OUString SAL_CALL AccessibleIconChoiceCtrl::getAccessibleDescription( )
189 ::comphelper::OExternalLockGuard aGuard( this );
191 return getCtrl()->GetAccessibleDescription();
194 OUString SAL_CALL AccessibleIconChoiceCtrl::getAccessibleName( )
196 ::comphelper::OExternalLockGuard aGuard( this );
198 OUString sName = getCtrl()->GetAccessibleName();
199 if ( sName.isEmpty() )
200 sName = "IconChoiceControl";
201 return sName;
204 // XAccessibleSelection
206 void SAL_CALL AccessibleIconChoiceCtrl::selectAccessibleChild( sal_Int32 nChildIndex )
208 ::comphelper::OExternalLockGuard aGuard( this );
210 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
211 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( nChildIndex );
212 if ( !pEntry )
213 throw IndexOutOfBoundsException();
215 pCtrl->SetCursor( pEntry );
218 sal_Bool SAL_CALL AccessibleIconChoiceCtrl::isAccessibleChildSelected( sal_Int32 nChildIndex )
220 ::comphelper::OExternalLockGuard aGuard( this );
222 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
223 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( nChildIndex );
224 if ( !pEntry )
225 throw IndexOutOfBoundsException();
227 return ( pCtrl->GetCursor() == pEntry );
230 void SAL_CALL AccessibleIconChoiceCtrl::clearAccessibleSelection( )
232 ::comphelper::OExternalLockGuard aGuard( this );
233 getCtrl()->SetNoSelection();
236 void SAL_CALL AccessibleIconChoiceCtrl::selectAllAccessibleChildren( )
238 ::comphelper::OExternalLockGuard aGuard( this );
240 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
241 sal_Int32 nCount = pCtrl->GetEntryCount();
242 for ( sal_Int32 i = 0; i < nCount; ++i )
244 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
245 if ( pCtrl->GetCursor() != pEntry )
246 pCtrl->SetCursor( pEntry );
250 sal_Int32 SAL_CALL AccessibleIconChoiceCtrl::getSelectedAccessibleChildCount( )
252 ::comphelper::OExternalLockGuard aGuard( this );
254 sal_Int32 nSelCount = 0;
255 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
256 sal_Int32 nCount = pCtrl->GetEntryCount();
257 for ( sal_Int32 i = 0; i < nCount; ++i )
259 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
260 if ( pCtrl->GetCursor() == pEntry )
261 ++nSelCount;
264 return nSelCount;
267 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex )
269 ::comphelper::OExternalLockGuard aGuard( this );
271 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
272 throw IndexOutOfBoundsException();
274 Reference< XAccessible > xChild;
275 sal_Int32 nSelCount = 0;
276 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
277 sal_Int32 nCount = pCtrl->GetEntryCount();
278 for ( sal_Int32 i = 0; i < nCount; ++i )
280 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
281 if ( pCtrl->GetCursor() == pEntry )
282 ++nSelCount;
284 if ( nSelCount == ( nSelectedChildIndex + 1 ) )
286 xChild = new AccessibleIconChoiceCtrlEntry( *pCtrl, i, this );
287 break;
291 return xChild;
294 void SAL_CALL AccessibleIconChoiceCtrl::deselectAccessibleChild( sal_Int32 nSelectedChildIndex )
296 ::comphelper::OExternalLockGuard aGuard( this );
298 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getAccessibleChildCount() )
299 throw IndexOutOfBoundsException();
301 sal_Int32 nSelCount = 0;
302 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
303 sal_Int32 nCount = pCtrl->GetEntryCount();
304 bool bFound = false;
305 for ( sal_Int32 i = 0; i < nCount; ++i )
307 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
308 if ( pEntry->IsSelected() )
310 ++nSelCount;
311 if ( i == nSelectedChildIndex )
312 bFound = true;
316 // if only one entry is selected and its index is chosen to deselect -> no selection anymore
317 if ( nSelCount == 1 && bFound )
318 pCtrl->SetNoSelection();
321 void AccessibleIconChoiceCtrl::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
323 VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet );
324 if ( isAlive() )
326 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
327 rStateSet.AddState( AccessibleStateType::MANAGES_DESCENDANTS );
328 rStateSet.AddState( AccessibleStateType::SELECTABLE );
332 VclPtr< SvtIconChoiceCtrl > AccessibleIconChoiceCtrl::getCtrl() const
334 return GetAs<SvtIconChoiceCtrl >();
337 }// namespace accessibility
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */