1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_accessibility.hxx"
26 #include "accessibility/extended/accessibleiconchoicectrl.hxx"
27 #include "accessibility/extended/accessibleiconchoicectrlentry.hxx"
28 #include <svtools/ivctrl.hxx>
29 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
30 #include <com/sun/star/accessibility/AccessibleRole.hpp>
31 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
32 #include <unotools/accessiblestatesethelper.hxx>
33 #include <tools/debug.hxx>
34 #include <vcl/svapp.hxx>
35 #include <cppuhelper/typeprovider.hxx>
36 
37 //........................................................................
38 namespace accessibility
39 {
40 //........................................................................
41 
42 	// class AccessibleIconChoiceCtrl ----------------------------------------------
43 
44 	using namespace ::com::sun::star::accessibility;
45 	using namespace ::com::sun::star::uno;
46 	using namespace ::com::sun::star::lang;
47 	using namespace ::com::sun::star;
48 
49 	DBG_NAME(AccessibleIconChoiceCtrl)
50 
51 	// -----------------------------------------------------------------------------
52 	// Ctor() and Dtor()
53 	// -----------------------------------------------------------------------------
54 	AccessibleIconChoiceCtrl::AccessibleIconChoiceCtrl( SvtIconChoiceCtrl& _rIconCtrl, const Reference< XAccessible >& _xParent ) :
55 
56 		VCLXAccessibleComponent( _rIconCtrl.GetWindowPeer() ),
57 		m_xParent		( _xParent )
58 	{
59 		DBG_CTOR( AccessibleIconChoiceCtrl, NULL );
60 	}
61 	// -----------------------------------------------------------------------------
62 	AccessibleIconChoiceCtrl::~AccessibleIconChoiceCtrl()
63 	{
64 		DBG_DTOR( AccessibleIconChoiceCtrl, NULL );
65 	}
66 	// -----------------------------------------------------------------------------
67 	IMPLEMENT_FORWARD_XINTERFACE2(AccessibleIconChoiceCtrl, VCLXAccessibleComponent, AccessibleIconChoiceCtrl_BASE)
68 	IMPLEMENT_FORWARD_XTYPEPROVIDER2(AccessibleIconChoiceCtrl, VCLXAccessibleComponent, AccessibleIconChoiceCtrl_BASE)
69 	// -----------------------------------------------------------------------------
70 	void AccessibleIconChoiceCtrl::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
71 	{
72     	if ( isAlive() )
73 		{
74 			switch ( rVclWindowEvent.GetId() )
75 			{
76 				case VCLEVENT_LISTBOX_SELECT :
77 				{
78                     // First send an event that tells the listeners of a
79                     // modified selection.  The active descendant event is
80                     // send after that so that the receiving AT has time to
81                     // read the text or name of the active child.
82                     NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, Any(), Any() );
83 					SvtIconChoiceCtrl* pCtrl = getCtrl();
84 					if ( pCtrl && pCtrl->HasFocus() )
85 					{
86 						SvxIconChoiceCtrlEntry* pEntry = static_cast< SvxIconChoiceCtrlEntry* >( rVclWindowEvent.GetData() );
87 						if ( pEntry )
88 						{
89 							sal_uLong nPos = pCtrl->GetEntryListPos( pEntry );
90                             Reference< XAccessible > xChild = new AccessibleIconChoiceCtrlEntry( *pCtrl, nPos, this );
91 							uno::Any aOldValue, aNewValue;
92 							aNewValue <<= xChild;
93 							NotifyAccessibleEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldValue, aNewValue );
94 						}
95 					}
96 					break;
97 				}
98 				default:
99 					VCLXAccessibleComponent::ProcessWindowChildEvent (rVclWindowEvent);
100 			}
101 		}
102 	}
103 	// -----------------------------------------------------------------------------
104 	// XComponent
105 	// -----------------------------------------------------------------------------
106 	void SAL_CALL AccessibleIconChoiceCtrl::disposing()
107 	{
108 		::osl::MutexGuard aGuard( m_aMutex );
109 
110 	    m_xParent = NULL;
111 	}
112 	// -----------------------------------------------------------------------------
113 	// XServiceInfo
114 	// -----------------------------------------------------------------------------
115 	::rtl::OUString SAL_CALL AccessibleIconChoiceCtrl::getImplementationName() throw (RuntimeException)
116 	{
117 		return getImplementationName_Static();
118 	}
119 	// -----------------------------------------------------------------------------
120 	Sequence< ::rtl::OUString > SAL_CALL AccessibleIconChoiceCtrl::getSupportedServiceNames() throw (RuntimeException)
121 	{
122 		return getSupportedServiceNames_Static();
123 	}
124 	// -----------------------------------------------------------------------------
125 	sal_Bool SAL_CALL AccessibleIconChoiceCtrl::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
126 	{
127 		Sequence< ::rtl::OUString > aSupported( getSupportedServiceNames() );
128 		const ::rtl::OUString* pSupported = aSupported.getConstArray();
129 		const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
130 		for ( ; pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported )
131 			;
132 
133 		return pSupported != pEnd;
134 	}
135 	// -----------------------------------------------------------------------------
136 	// XServiceInfo - static methods
137 	// -----------------------------------------------------------------------------
138 	Sequence< ::rtl::OUString > AccessibleIconChoiceCtrl::getSupportedServiceNames_Static(void) throw (RuntimeException)
139 	{
140 		Sequence< ::rtl::OUString > aSupported(3);
141 		aSupported[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.accessibility.AccessibleContext") );
142 		aSupported[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.accessibility.AccessibleComponent") );
143 		aSupported[2] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.AccessibleIconChoiceControl") );
144 		return aSupported;
145 	}
146 	// -----------------------------------------------------------------------------
147 	::rtl::OUString AccessibleIconChoiceCtrl::getImplementationName_Static(void) throw (RuntimeException)
148 	{
149 		return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.svtools.AccessibleIconChoiceControl") );
150 	}
151 	// -----------------------------------------------------------------------------
152 	// XAccessible
153 	// -----------------------------------------------------------------------------
154 	Reference< XAccessibleContext > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleContext(  ) throw (RuntimeException)
155 	{
156 		ensureAlive();
157 		return this;
158 	}
159 	// -----------------------------------------------------------------------------
160 	// XAccessibleContext
161 	// -----------------------------------------------------------------------------
162 	sal_Int32 SAL_CALL AccessibleIconChoiceCtrl::getAccessibleChildCount(  ) throw (RuntimeException)
163 	{
164 		::comphelper::OExternalLockGuard aGuard( this );
165 
166 		ensureAlive();
167 		return getCtrl()->GetEntryCount();
168 	}
169 	// -----------------------------------------------------------------------------
170 	Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleChild( sal_Int32 i ) throw (RuntimeException, IndexOutOfBoundsException)
171 	{
172 		::comphelper::OExternalLockGuard aGuard( this );
173 
174 		ensureAlive();
175 		SvtIconChoiceCtrl* pCtrl = getCtrl();
176 		SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry(i);
177 		if ( !pEntry )
178 			throw RuntimeException();
179 
180 		return new AccessibleIconChoiceCtrlEntry( *pCtrl, i, this );
181 	}
182 	// -----------------------------------------------------------------------------
183 	Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleParent(  ) throw (RuntimeException)
184 	{
185 		::osl::MutexGuard aGuard( m_aMutex );
186 
187 		ensureAlive();
188 		return m_xParent;
189 	}
190 	// -----------------------------------------------------------------------------
191 	sal_Int16 SAL_CALL AccessibleIconChoiceCtrl::getAccessibleRole(  ) throw (RuntimeException)
192 	{
193 		return AccessibleRole::TREE;
194 	}
195 	// -----------------------------------------------------------------------------
196 	::rtl::OUString SAL_CALL AccessibleIconChoiceCtrl::getAccessibleDescription(  ) throw (RuntimeException)
197 	{
198 		::comphelper::OExternalLockGuard aGuard( this );
199 
200 		ensureAlive();
201 		return getCtrl()->GetAccessibleDescription();
202 	}
203 	// -----------------------------------------------------------------------------
204 	::rtl::OUString SAL_CALL AccessibleIconChoiceCtrl::getAccessibleName(  ) throw (RuntimeException)
205 	{
206 		::comphelper::OExternalLockGuard aGuard( this );
207 
208 		ensureAlive();
209 
210         ::rtl::OUString sName = getCtrl()->GetAccessibleName();
211         if ( sName.getLength() == 0 )
212             sName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IconChoiceControl" ) );
213         return sName;
214 	}
215 	// -----------------------------------------------------------------------------
216 	// XAccessibleSelection
217 	// -----------------------------------------------------------------------------
218 	void SAL_CALL AccessibleIconChoiceCtrl::selectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
219 	{
220     	::comphelper::OExternalLockGuard aGuard( this );
221 
222 		ensureAlive();
223 
224 		SvtIconChoiceCtrl* pCtrl = getCtrl();
225 		SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( nChildIndex );
226 		if ( !pEntry )
227 			throw IndexOutOfBoundsException();
228 
229 		pCtrl->SetCursor( pEntry );
230 	}
231 	// -----------------------------------------------------------------------------
232 	sal_Bool SAL_CALL AccessibleIconChoiceCtrl::isAccessibleChildSelected( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
233 	{
234     	::comphelper::OExternalLockGuard aGuard( this );
235 
236 		ensureAlive();
237 
238 		SvtIconChoiceCtrl* pCtrl = getCtrl();
239 		SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( nChildIndex );
240 		if ( !pEntry )
241 			throw IndexOutOfBoundsException();
242 
243 		return ( pCtrl->GetCursor() == pEntry );
244 	}
245 	// -----------------------------------------------------------------------------
246 	void SAL_CALL AccessibleIconChoiceCtrl::clearAccessibleSelection(  ) throw (RuntimeException)
247 	{
248     	::comphelper::OExternalLockGuard aGuard( this );
249 
250 		ensureAlive();
251 		getCtrl()->SetNoSelection();
252 	}
253 	// -----------------------------------------------------------------------------
254 	void SAL_CALL AccessibleIconChoiceCtrl::selectAllAccessibleChildren(  ) throw (RuntimeException)
255 	{
256     	::comphelper::OExternalLockGuard aGuard( this );
257 
258 		ensureAlive();
259 
260     	sal_Int32 i, nCount = 0;
261 		SvtIconChoiceCtrl* pCtrl = getCtrl();
262 		nCount = pCtrl->GetEntryCount();
263 		for ( i = 0; i < nCount; ++i )
264 		{
265 			SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
266 			if ( pCtrl->GetCursor() != pEntry )
267 				pCtrl->SetCursor( pEntry );
268 		}
269 	}
270 	// -----------------------------------------------------------------------------
271 	sal_Int32 SAL_CALL AccessibleIconChoiceCtrl::getSelectedAccessibleChildCount(  ) throw (RuntimeException)
272 	{
273     	::comphelper::OExternalLockGuard aGuard( this );
274 
275 		ensureAlive();
276 
277     	sal_Int32 i, nSelCount = 0, nCount = 0;
278 		SvtIconChoiceCtrl* pCtrl = getCtrl();
279 		nCount = pCtrl->GetEntryCount();
280 		for ( i = 0; i < nCount; ++i )
281 		{
282 			SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
283 			if ( pCtrl->GetCursor() == pEntry )
284 				++nSelCount;
285 		}
286 
287     	return nSelCount;
288 	}
289 	// -----------------------------------------------------------------------------
290 	Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
291 	{
292     	::comphelper::OExternalLockGuard aGuard( this );
293 
294 		ensureAlive();
295 
296 		if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
297 			throw IndexOutOfBoundsException();
298 
299 		Reference< XAccessible > xChild;
300     	sal_Int32 i, nSelCount = 0, nCount = 0;
301 		SvtIconChoiceCtrl* pCtrl = getCtrl();
302 		nCount = pCtrl->GetEntryCount();
303 		for ( i = 0; i < nCount; ++i )
304 		{
305 			SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
306 			if ( pCtrl->GetCursor() == pEntry )
307 				++nSelCount;
308 
309             if ( nSelCount == ( nSelectedChildIndex + 1 ) )
310 			{
311                 xChild = new AccessibleIconChoiceCtrlEntry( *pCtrl, i, this );
312 				break;
313 			}
314 		}
315 
316 		return xChild;
317 	}
318 	// -----------------------------------------------------------------------------
319 	void SAL_CALL AccessibleIconChoiceCtrl::deselectAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
320 	{
321     	::comphelper::OExternalLockGuard aGuard( this );
322 
323 		ensureAlive();
324 
325 		if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getAccessibleChildCount() )
326 			throw IndexOutOfBoundsException();
327 
328 		Reference< XAccessible > xChild;
329     	sal_Int32 i, nSelCount = 0, nCount = 0;
330 		SvtIconChoiceCtrl* pCtrl = getCtrl();
331 		nCount = pCtrl->GetEntryCount();
332 		bool bFound = false;
333 		for ( i = 0; i < nCount; ++i )
334 		{
335 			SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
336 			if ( pEntry->IsSelected() )
337 			{
338 				++nSelCount;
339 				if ( i == nSelectedChildIndex )
340 					bFound = true;
341 			}
342 		}
343 
344 		// if only one entry is selected and its index is choosen to deselect -> no selection anymore
345 		if ( 1 == nSelCount && bFound )
346 			pCtrl->SetNoSelection();
347 	}
348 	// -----------------------------------------------------------------------------
349 	void AccessibleIconChoiceCtrl::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
350 	{
351 		VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet );
352 		if ( isAlive() )
353 		{
354 			rStateSet.AddState( AccessibleStateType::FOCUSABLE );
355 			rStateSet.AddState( AccessibleStateType::MANAGES_DESCENDANTS );
356             rStateSet.AddState( AccessibleStateType::SELECTABLE );
357 		}
358 	}
359 	// -----------------------------------------------------------------------------
360 	SvtIconChoiceCtrl* AccessibleIconChoiceCtrl::getCtrl()
361 	{
362 		return static_cast<SvtIconChoiceCtrl*>(GetWindow());
363 	}
364 
365 //........................................................................
366 }// namespace accessibility
367 //........................................................................
368 
369