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/standard/vclxaccessiblelist.hxx>
27 #include <accessibility/standard/vclxaccessiblelistitem.hxx>
28 #include <accessibility/helper/listboxhelper.hxx>
29 
30 #include <unotools/accessiblestatesethelper.hxx>
31 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
32 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
33 #include <com/sun/star/accessibility/AccessibleRole.hpp>
34 #include <vcl/svapp.hxx>
35 #include <vcl/combobox.hxx>
36 #include <vcl/lstbox.hxx>
37 #include <toolkit/helper/convert.hxx>
38 
39 #ifndef _UTL_ACCESSIBLERELATIONSETHELPER_HXX_
40 #include <unotools/accessiblerelationsethelper.hxx>
41 #endif
42 #ifndef _COM_SUN_STAR_ACCESSIBILITY_ACCESSIBLERELATIONTYPE_HPP_
43 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
44 #endif
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::lang;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::accessibility;
50 using namespace ::accessibility;
51 
52 namespace
53 {
54 	void checkSelection_Impl( sal_Int32 _nIndex, const IComboListBoxHelper& _rListBox, sal_Bool bSelected )
55         throw (::com::sun::star::lang::IndexOutOfBoundsException)
56 	{
57 		sal_Int32 nCount = bSelected ? (sal_Int32)_rListBox.GetSelectEntryCount()
58 									 : (sal_Int32)_rListBox.GetEntryCount();
59 		if ( _nIndex < 0 || _nIndex >= nCount )
60 			throw ::com::sun::star::lang::IndexOutOfBoundsException();
61 	}
62 }
63 
64 VCLXAccessibleList::VCLXAccessibleList (VCLXWindow* pVCLWindow, BoxType aBoxType,
65 										const Reference< XAccessible >& _xParent)
66     : VCLXAccessibleComponent	(pVCLWindow),
67       m_aBoxType				(aBoxType),
68       m_nVisibleLineCount		(0),
69       m_nIndexInParent			(DEFAULT_INDEX_IN_PARENT),
70 	  m_nLastTopEntry			( 0 ),
71 	  m_nLastSelectedPos		( LISTBOX_ENTRY_NOTFOUND ),
72 	  m_bDisableProcessEvent	( false ),
73 	  m_bVisible				( true ),
74 	m_nCurSelectedPos		( LISTBOX_ENTRY_NOTFOUND ),
75       m_xParent                 ( _xParent )
76 {
77     // Because combo boxes and list boxes have the no common interface for
78     // methods with identical signature we have to write down twice the
79     // same code.
80     switch (m_aBoxType)
81     {
82         case COMBOBOX:
83         {
84             ComboBox* pBox = static_cast<ComboBox*>(GetWindow());
85             if ( pBox != NULL )
86                 m_pListBoxHelper = new VCLListBoxHelper<ComboBox> (*pBox);
87             break;
88         }
89 
90         case LISTBOX:
91         {
92             ListBox* pBox = static_cast<ListBox*>(GetWindow());
93             if ( pBox != NULL )
94                 m_pListBoxHelper = new VCLListBoxHelper<ListBox> (*pBox);
95             break;
96         }
97     }
98     UpdateVisibleLineCount();
99 	m_nCurSelectedPos=m_pListBoxHelper->GetSelectEntryPos();
100 
101 	sal_uInt16 nCount = static_cast<sal_uInt16>(getAccessibleChildCount());
102 	m_aAccessibleChildren.reserve(nCount);
103 }
104 // -----------------------------------------------------------------------------
105 
106 VCLXAccessibleList::~VCLXAccessibleList (void)
107 {
108     delete m_pListBoxHelper;
109 }
110 // -----------------------------------------------------------------------------
111 
112 void VCLXAccessibleList::SetIndexInParent (sal_Int32 nIndex)
113 {
114     m_nIndexInParent = nIndex;
115 }
116 // -----------------------------------------------------------------------------
117 
118 void SAL_CALL VCLXAccessibleList::disposing (void)
119 {
120 	VCLXAccessibleComponent::disposing();
121 
122     // Dispose all items in the list.
123 	clearItems();
124 
125 	delete m_pListBoxHelper;
126 	m_pListBoxHelper = NULL;
127 }
128 // -----------------------------------------------------------------------------
129 
130 void VCLXAccessibleList::clearItems()
131 {
132 //	ListItems::iterator aEnd = m_aAccessibleChildren.end();
133 //	for (ListItems::iterator aIter = m_aAccessibleChildren.begin(); aIter != aEnd; ++aIter)
134 //		::comphelper::disposeComponent(*aIter);
135 
136     // Clear the list itself and delete all the rest.
137 	ListItems().swap(m_aAccessibleChildren); // clear and minimize
138 }
139 // -----------------------------------------------------------------------------
140 
141 void VCLXAccessibleList::FillAccessibleStateSet (utl::AccessibleStateSetHelper& rStateSet)
142 {
143 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
144 
145 	VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet );
146 	// check if our list should be visible
147 	if (	m_pListBoxHelper
148 		&& (m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) == WB_DROPDOWN
149 		&& !m_pListBoxHelper->IsInDropDown() )
150 	{
151 		rStateSet.RemoveState (AccessibleStateType::VISIBLE);
152 		rStateSet.RemoveState (AccessibleStateType::SHOWING);
153 		m_bVisible = false;
154 	}
155 
156     // Both the combo box and list box are handled identical in the
157     // following but for some reason they don't have a common interface for
158     // the methods used.
159 	if ( m_pListBoxHelper )
160 	{
161         if ( m_pListBoxHelper->IsMultiSelectionEnabled() )
162             rStateSet.AddState( AccessibleStateType::MULTI_SELECTABLE);
163 		rStateSet.AddState (AccessibleStateType::FOCUSABLE);
164         // All children are transient.
165         rStateSet.AddState (AccessibleStateType::MANAGES_DESCENDANTS);
166 	}
167 }
168 // -----------------------------------------------------------------------------
169 void VCLXAccessibleList::notifyVisibleStates(sal_Bool _bSetNew )
170 {
171 	m_bVisible = _bSetNew ? true : false;
172 	Any aOldValue, aNewValue;
173 	(_bSetNew ? aNewValue : aOldValue ) <<= AccessibleStateType::VISIBLE;
174 	NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
175 	(_bSetNew ? aNewValue : aOldValue ) <<= AccessibleStateType::SHOWING;
176 	NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
177 
178 	ListItems::iterator aIter = m_aAccessibleChildren.begin();
179 	ListItems::iterator aEnd = m_aAccessibleChildren.end();
180     UpdateVisibleLineCount();
181 	// adjust the index inside the VCLXAccessibleListItem
182 	for (;aIter != aEnd ; ++aIter)
183 	{
184 		Reference< XAccessible > xHold = *aIter;
185 		VCLXAccessibleListItem* pItem = static_cast<VCLXAccessibleListItem*>(xHold.get());
186 		if ( pItem )
187 		{
188 			sal_uInt16 nTopEntry = 0;
189 			if ( m_pListBoxHelper )
190 				nTopEntry = m_pListBoxHelper->GetTopEntry();
191             sal_uInt16 nPos = (sal_uInt16)(aIter - m_aAccessibleChildren.begin());
192 			sal_Bool bVisible = ( nPos>=nTopEntry && nPos<( nTopEntry + m_nVisibleLineCount ) );
193 			pItem->SetVisible( m_bVisible && bVisible );
194 		}
195 
196 	}
197 }
198 // -----------------------------------------------------------------------------
199 void VCLXAccessibleList::UpdateSelection_Acc (::rtl::OUString sTextOfSelectedItem, bool b_IsDropDownList)
200 {
201     if ( m_aBoxType == COMBOBOX )
202     {
203         ComboBox* pBox = static_cast<ComboBox*>(GetWindow());
204         if ( pBox != NULL )
205         {
206 		// Find the index of the selected item inside the VCL control...
207 		sal_uInt16 nIndex = pBox->GetEntryPos (XubString(sTextOfSelectedItem));
208 		// ...and then find the associated accessibility object.
209 		if ( nIndex == LISTBOX_ENTRY_NOTFOUND )
210 			nIndex = 0;
211 		UpdateSelection_Impl_Acc(b_IsDropDownList);
212         }
213     }
214 }
215 
216 // -----------------------------------------------------------------------------
217 void VCLXAccessibleList::UpdateSelection_Impl_Acc(bool b_IsDropDownList)
218 {
219 	uno::Any aOldValue, aNewValue;
220 	VCLXAccessibleListItem* pCurItem =NULL;
221 
222 	{
223 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
224 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
225 	    	Reference< XAccessible > xNewAcc;
226 		if ( m_pListBoxHelper )
227 		{
228 			sal_uInt16 i=0;
229 			m_nCurSelectedPos = LISTBOX_ENTRY_NOTFOUND;
230 			for ( ListItems::iterator aIter = m_aAccessibleChildren.begin();
231 				  aIter != m_aAccessibleChildren.end(); ++aIter,++i)
232 			{
233 				Reference< XAccessible > xHold = *aIter;
234 				if ( xHold.is() )
235 				{
236 					VCLXAccessibleListItem* pItem = static_cast< VCLXAccessibleListItem* >( xHold.get() );
237 					// Retrieve the item's index from the list entry.
238 					sal_Bool bNowSelected = m_pListBoxHelper->IsEntryPosSelected (i);
239 					if (bNowSelected)
240 						m_nCurSelectedPos = i;
241 
242 					if ( bNowSelected && !pItem->IsSelected() )
243 					{
244 						xNewAcc = *aIter;
245 						aNewValue <<= xNewAcc;
246 
247 						pCurItem = pItem;
248 
249 					}
250 					else if ( pItem->IsSelected() )
251 						m_nLastSelectedPos = i;
252 
253 					pItem->SetSelected( bNowSelected );
254 				}
255 				else
256 				{ // it could happen that a child was not created before
257 					checkEntrySelected(i,aNewValue,xNewAcc);
258 				}
259 			}
260 			sal_uInt16 nCount = m_pListBoxHelper->GetEntryCount();
261 			if ( i < nCount ) // here we have to check the if any other listbox entry is selected
262 			{
263 				for (; i < nCount && !checkEntrySelected(i,aNewValue,xNewAcc) ;++i )
264 					;
265 			}
266 			if ( xNewAcc.is() && GetWindow()->HasFocus() )
267 			{
268 				if ( m_nLastSelectedPos != LISTBOX_ENTRY_NOTFOUND )
269 					aOldValue <<= getAccessibleChild( (sal_Int32)m_nLastSelectedPos );
270 				aNewValue <<= xNewAcc;
271 			}
272 		}
273 	}
274 	if (m_aBoxType == COMBOBOX && b_IsDropDownList)
275 	{
276 		//VCLXAccessibleDropDownComboBox
277 		//when in list is dropped down, xText = NULL
278 		if (m_pListBoxHelper->IsInDropDown())
279 		{
280 			if ( aNewValue.hasValue() || aOldValue.hasValue() )
281 			{
282 				NotifyAccessibleEvent(
283 					AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
284 					aOldValue,
285 					aNewValue );
286 
287 				NotifyListItem(aNewValue);
288 
289 			}
290 		}
291 	}
292 	else if (m_aBoxType == COMBOBOX && !b_IsDropDownList)
293 	{
294 		//VCLXAccessibleComboBox
295 		NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, uno::Any(), uno::Any() );
296 	}
297 	else if (m_aBoxType == LISTBOX && b_IsDropDownList)
298 	{
299 		//VCLXAccessibleDropdownListBox
300 		//when in list is dropped down, xText = NULL
301 		if (m_pListBoxHelper->IsInDropDown())
302 		{
303 			if ( aNewValue.hasValue() || aOldValue.hasValue() )
304 			{
305 				NotifyAccessibleEvent(
306 					AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
307 					aOldValue,
308 					aNewValue );
309 
310 				NotifyListItem(aNewValue);
311 			}
312 		}
313 	}
314 	else if (m_aBoxType == LISTBOX && !b_IsDropDownList)
315 	{
316 		//VCLXAccessibleListBox, xText = NULL.
317 
318 
319 		if ( aNewValue.hasValue())
320 		{
321 			NotifyListItem(aNewValue);
322 		}
323 	}
324 }
325 void VCLXAccessibleList::NotifyListItem(::com::sun::star::uno::Any& val)
326 {
327 	Reference< XAccessible > xCurItem;
328 	val >>= xCurItem;
329 	if (xCurItem.is())
330 	{
331 		VCLXAccessibleListItem* pCurItem = static_cast< VCLXAccessibleListItem* >(xCurItem.get());
332 		if (pCurItem)
333 		{
334 			pCurItem->NotifyAccessibleEvent(AccessibleEventId::SELECTION_CHANGED,Any(),Any());
335 		}
336 	}
337 }
338 
339 
340 void VCLXAccessibleList::UpdateFocus_Impl_Acc (sal_uInt16 nPos ,bool b_IsDropDownList)
341 {
342 	if (!(m_aBoxType == LISTBOX && !b_IsDropDownList))
343 	{
344 		return ;
345 	}
346     Reference<XAccessible> xChild= CreateChild(nPos);
347 	if ( !xChild.is() )
348 	{
349 		return ;
350 	}
351 	m_nCurSelectedPos = nPos;
352 	uno::Any aOldValue, aNewValue;
353 	aNewValue <<= xChild;
354 
355 	NotifyAccessibleEvent(
356 			AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
357 			aOldValue,
358 			aNewValue );
359 }
360 
361 // -----------------------------------------------------------------------------
362 void VCLXAccessibleList::ProcessWindowEvent (const VclWindowEvent& rVclWindowEvent,  bool b_IsDropDownList)
363 {
364 	switch ( rVclWindowEvent.GetId() )
365       {
366 		case VCLEVENT_LISTBOX_SELECT:
367 			if ( !m_bDisableProcessEvent )
368 				UpdateSelection_Impl_Acc(b_IsDropDownList);
369 			break;
370 		case VCLEVENT_LISTBOX_FOCUSITEMCHANGED:
371 			if ( !m_bDisableProcessEvent )
372 				UpdateFocus_Impl_Acc((sal_uInt16)reinterpret_cast<sal_uIntPtr>(rVclWindowEvent.GetData()),b_IsDropDownList);
373 			break;
374 		case VCLEVENT_WINDOW_GETFOCUS:
375 			break;
376 		case VCLEVENT_CONTROL_GETFOCUS:
377 			{
378 				VCLXAccessibleComponent::ProcessWindowEvent (rVclWindowEvent);
379 				if (m_aBoxType == COMBOBOX && b_IsDropDownList)
380 				{
381 					//VCLXAccessibleDropDownComboBox
382 				}
383 				else if (m_aBoxType == LISTBOX && b_IsDropDownList)
384 				{
385 				}
386 				else if ( m_aBoxType == LISTBOX && !b_IsDropDownList)
387 				{
388 					if ( m_pListBoxHelper )
389 					{
390 						uno::Any	aOldValue,
391 									aNewValue;
392 						sal_uInt16 nPos = m_nCurSelectedPos; //m_pListBoxHelper->GetSelectEntryPos();
393 
394 						if ( nPos == LISTBOX_ENTRY_NOTFOUND )
395 							nPos = m_pListBoxHelper->GetTopEntry();
396 						if ( nPos != LISTBOX_ENTRY_NOTFOUND )
397 							aNewValue <<= CreateChild(nPos);
398 						NotifyAccessibleEvent(	AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
399 												aOldValue,
400 												aNewValue );
401 					}
402 				}
403 			}
404 			break;
405 		default:
406 			break;
407 	}
408 
409 }
410 // -----------------------------------------------------------------------------
411 void VCLXAccessibleList::ProcessWindowEvent (const VclWindowEvent& rVclWindowEvent)
412 {
413     // Create a reference to this object to prevent an early release of the
414     // listbox (VCLEVENT_OBJECT_DYING).
415 	Reference< XAccessible > xTemp = this;
416 
417 	switch ( rVclWindowEvent.GetId() )
418     {
419 		case VCLEVENT_DROPDOWN_OPEN:
420 			notifyVisibleStates(sal_True);
421 			break;
422 		case VCLEVENT_DROPDOWN_CLOSE:
423 			notifyVisibleStates(sal_False);
424 			break;
425 		case VCLEVENT_LISTBOX_SCROLLED:
426 		case VCLEVENT_COMBOBOX_SCROLLED:
427 			UpdateEntryRange_Impl();
428 			break;
429 
430 		// The selection events VCLEVENT_COMBOBOX_SELECT and
431 		// VCLEVENT_COMBOBOX_DESELECT are not handled here because here we
432 		// have no access to the edit field.  Its text is necessary to
433 		// identify the currently selected item.
434 
435 		case VCLEVENT_OBJECT_DYING:
436 		{
437             dispose();
438 
439             VCLXAccessibleComponent::ProcessWindowEvent (rVclWindowEvent);
440             break;
441 		}
442 
443         case VCLEVENT_LISTBOX_ITEMREMOVED:
444         case VCLEVENT_COMBOBOX_ITEMREMOVED:
445             HandleChangedItemList (false, reinterpret_cast<sal_IntPtr>(
446                 rVclWindowEvent.GetData()));
447             break;
448 
449         case VCLEVENT_LISTBOX_ITEMADDED:
450         case VCLEVENT_COMBOBOX_ITEMADDED:
451             HandleChangedItemList (true, reinterpret_cast<sal_IntPtr>(
452                 rVclWindowEvent.GetData()));
453             break;
454 		case VCLEVENT_CONTROL_GETFOCUS:
455 			{
456 	            VCLXAccessibleComponent::ProcessWindowEvent (rVclWindowEvent);
457 				// Added by IBM Symphony Acc team to handle the list item focus when List control get focus
458 				sal_Bool b_IsDropDownList = sal_True;
459 				if (m_pListBoxHelper)
460 					b_IsDropDownList = ((m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) == WB_DROPDOWN);
461 				if ( m_aBoxType == LISTBOX && !b_IsDropDownList )
462 				{
463 					if ( m_pListBoxHelper )
464 					{
465 						uno::Any	aOldValue,
466 									aNewValue;
467 						sal_uInt16 nPos = m_nCurSelectedPos;
468 
469 						if ( nPos == LISTBOX_ENTRY_NOTFOUND )
470 							nPos = m_pListBoxHelper->GetTopEntry();
471 						if ( nPos != LISTBOX_ENTRY_NOTFOUND )
472 							aNewValue <<= CreateChild(nPos);
473 						NotifyAccessibleEvent(	AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
474 												aOldValue,
475 												aNewValue );
476 					}
477 				}
478 			}
479 			break;
480 
481         default:
482             VCLXAccessibleComponent::ProcessWindowEvent (rVclWindowEvent);
483     }
484 }
485 
486  void VCLXAccessibleList::FillAccessibleRelationSet( utl::AccessibleRelationSetHelper& rRelationSet )
487 {
488 	ListBox* pBox = static_cast<ListBox*>(GetWindow());
489 	if( m_aBoxType == LISTBOX  )
490 	{
491 		if (m_pListBoxHelper && (m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) != WB_DROPDOWN)
492 		{
493 			uno::Sequence< uno::Reference< uno::XInterface > > aSequence(1);
494 			aSequence[0] = pBox->GetAccessible();
495 			rRelationSet.AddRelation( com::sun::star::accessibility::AccessibleRelation( com::sun::star::accessibility::AccessibleRelationType::MEMBER_OF, aSequence ) );
496 		}
497 	}
498 	else
499 	{
500 		VCLXAccessibleComponent::FillAccessibleRelationSet(rRelationSet);
501 	}
502 }
503 // -----------------------------------------------------------------------------
504 
505 /** To find out which item is currently selected and to update the SELECTED
506     state of the associated accessibility objects accordingly we exploit the
507     fact that the
508 */
509 void VCLXAccessibleList::UpdateSelection (::rtl::OUString sTextOfSelectedItem)
510 {
511     if ( m_aBoxType == COMBOBOX )
512     {
513         ComboBox* pBox = static_cast<ComboBox*>(GetWindow());
514         if ( pBox != NULL )
515         {
516             // Find the index of the selected item inside the VCL control...
517             sal_uInt16 nIndex = pBox->GetEntryPos (XubString(sTextOfSelectedItem));
518             // ...and then find the associated accessibility object.
519 			if ( nIndex == LISTBOX_ENTRY_NOTFOUND )
520 				nIndex = 0;
521 			UpdateSelection_Impl(nIndex);
522         }
523     }
524 }
525 // -----------------------------------------------------------------------------
526 
527 void VCLXAccessibleList::adjustEntriesIndexInParent(ListItems::iterator& _aBegin,::std::mem_fun_t<bool,VCLXAccessibleListItem>& _rMemFun)
528 {
529 	ListItems::iterator aIter = _aBegin;
530 	ListItems::iterator aEnd = m_aAccessibleChildren.end();
531 	// adjust the index inside the VCLXAccessibleListItem
532 	for (;aIter != aEnd ; ++aIter)
533 	{
534 		Reference< XAccessible > xHold = *aIter;
535 		VCLXAccessibleListItem* pItem = static_cast<VCLXAccessibleListItem*>(xHold.get());
536 		if ( pItem )
537 			_rMemFun(pItem);
538 	}
539 }
540 // -----------------------------------------------------------------------------
541 
542 Reference<XAccessible> VCLXAccessibleList::CreateChild (sal_Int32 i)
543 {
544     Reference<XAccessible> xChild;
545 
546 	sal_uInt16 nPos = static_cast<sal_uInt16>(i);
547 	if ( nPos >= m_aAccessibleChildren.size() )
548 	{
549 		m_aAccessibleChildren.resize(nPos + 1);
550 
551 		// insert into the container
552 		xChild = new VCLXAccessibleListItem(m_pListBoxHelper, i, this);
553 		m_aAccessibleChildren[nPos] = xChild;
554 	}
555 	else
556 	{
557 		xChild = m_aAccessibleChildren[nPos];
558 		// check if position is empty and can be used else we have to adjust all entries behind this
559 		if ( !xChild.is() )
560 		{
561 			xChild = new VCLXAccessibleListItem(m_pListBoxHelper, i, this);
562 			m_aAccessibleChildren[nPos] = xChild;
563 		}
564 	}
565 
566     if ( xChild.is() )
567     {
568 		// Just add the SELECTED state.
569 		sal_Bool bNowSelected = sal_False;
570 		if ( m_pListBoxHelper )
571 			bNowSelected = m_pListBoxHelper->IsEntryPosSelected ((sal_uInt16)i);
572 		if (bNowSelected)
573 			m_nCurSelectedPos = sal_uInt16(i);
574         VCLXAccessibleListItem* pItem = static_cast< VCLXAccessibleListItem* >(xChild.get());
575         pItem->SetSelected( bNowSelected );
576 
577 		// Set the child's VISIBLE state.
578         UpdateVisibleLineCount();
579 		sal_uInt16 nTopEntry = 0;
580 		if ( m_pListBoxHelper )
581 			nTopEntry = m_pListBoxHelper->GetTopEntry();
582 		sal_Bool bVisible = ( nPos>=nTopEntry && nPos<( nTopEntry + m_nVisibleLineCount ) );
583 		pItem->SetVisible( m_bVisible && bVisible );
584     }
585 
586     return xChild;
587 }
588 // -----------------------------------------------------------------------------
589 
590 void VCLXAccessibleList::HandleChangedItemList (bool bItemInserted, sal_Int32 nIndex)
591 {
592 	clearItems();
593     NotifyAccessibleEvent (
594         AccessibleEventId::INVALIDATE_ALL_CHILDREN,
595         Any(), Any());
596 }
597 // -----------------------------------------------------------------------------
598 
599 IMPLEMENT_FORWARD_XINTERFACE2(VCLXAccessibleList, VCLXAccessibleComponent, VCLXAccessibleList_BASE)
600 IMPLEMENT_FORWARD_XTYPEPROVIDER2(VCLXAccessibleList, VCLXAccessibleComponent, VCLXAccessibleList_BASE)
601 
602 //=====  XAccessible  =========================================================
603 
604 Reference<XAccessibleContext> SAL_CALL
605     VCLXAccessibleList::getAccessibleContext (void)
606     throw (RuntimeException)
607 {
608 	return this;
609 }
610 // -----------------------------------------------------------------------------
611 
612 //=====  XAccessibleContext  ==================================================
613 
614 sal_Int32 SAL_CALL VCLXAccessibleList::getAccessibleChildCount (void)
615     throw (RuntimeException)
616 {
617 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
618 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
619 
620 	sal_Int32 nCount = 0;
621 	if ( m_pListBoxHelper )
622 		nCount = m_pListBoxHelper->GetEntryCount();
623 
624     return nCount;
625 }
626 // -----------------------------------------------------------------------------
627 
628 Reference<XAccessible> SAL_CALL VCLXAccessibleList::getAccessibleChild (sal_Int32 i)
629     throw (IndexOutOfBoundsException, RuntimeException)
630 {
631 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
632 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
633 
634 	if ( i < 0 || i >= getAccessibleChildCount() )
635 		throw IndexOutOfBoundsException();
636 
637     Reference< XAccessible > xChild;
638     // search for the child
639 	if ( i >= static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
640 		xChild = CreateChild (i);
641 	else
642 	{
643 		xChild = m_aAccessibleChildren[i];
644 		if ( !xChild.is() )
645 			xChild = CreateChild (i);
646 	}
647 	OSL_ENSURE( xChild.is(), "VCLXAccessibleList::getAccessibleChild: returning empty child!" );
648     return xChild;
649 }
650 // -----------------------------------------------------------------------------
651 
652 Reference< XAccessible > SAL_CALL VCLXAccessibleList::getAccessibleParent(  )
653 	throw (RuntimeException)
654 {
655 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
656 
657 	return m_xParent;
658 }
659 // -----------------------------------------------------------------------------
660 
661 sal_Int32 SAL_CALL VCLXAccessibleList::getAccessibleIndexInParent (void)
662     throw (::com::sun::star::uno::RuntimeException)
663 {
664     if (m_nIndexInParent != DEFAULT_INDEX_IN_PARENT)
665         return m_nIndexInParent;
666     else
667         return VCLXAccessibleComponent::getAccessibleIndexInParent();
668 }
669 // -----------------------------------------------------------------------------
670 
671 sal_Int16 SAL_CALL VCLXAccessibleList::getAccessibleRole (void)
672     throw (RuntimeException)
673 {
674     return AccessibleRole::LIST;
675 }
676 // -----------------------------------------------------------------------------
677 
678 //=====  XAccessibleComponent  ================================================
679 
680 sal_Bool SAL_CALL VCLXAccessibleList::contains( const awt::Point& rPoint ) throw (RuntimeException)
681 {
682 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
683 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
684 
685 	sal_Bool bInside = sal_False;
686 
687     Window* pListBox = GetWindow();
688     if ( pListBox )
689 	{
690 		Rectangle aRect( Point(0,0), pListBox->GetSizePixel() );
691 		bInside = aRect.IsInside( VCLPoint( rPoint ) );
692 	}
693 
694 	return bInside;
695 }
696 // -----------------------------------------------------------------------------
697 
698 Reference< XAccessible > SAL_CALL VCLXAccessibleList::getAccessibleAt( const awt::Point& rPoint )
699     throw (RuntimeException)
700 {
701 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
702 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
703 
704 	Reference< XAccessible > xChild;
705 	if ( m_pListBoxHelper )
706 	{
707         UpdateVisibleLineCount();
708 		if ( contains( rPoint ) && m_nVisibleLineCount > 0 )
709 		{
710 			Point aPos = VCLPoint( rPoint );
711 			sal_uInt16 nEndPos = m_pListBoxHelper->GetTopEntry() + (sal_uInt16)m_nVisibleLineCount;
712 			for ( sal_uInt16 i = m_pListBoxHelper->GetTopEntry(); i < nEndPos; ++i )
713 			{
714 				if ( m_pListBoxHelper->GetBoundingRectangle(i).IsInside( aPos ) )
715 				{
716 					xChild = getAccessibleChild(i);
717 					break;
718 				}
719 			}
720 		}
721 	}
722 
723     return xChild;
724 }
725 // -----------------------------------------------------------------------------
726 
727 //===== XServiceInfo ==========================================================
728 
729 ::rtl::OUString VCLXAccessibleList::getImplementationName (void)
730     throw (RuntimeException)
731 {
732 	return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.toolkit.AccessibleList"));
733 }
734 // -----------------------------------------------------------------------------
735 
736 Sequence< ::rtl::OUString > VCLXAccessibleList::getSupportedServiceNames (void)
737     throw (RuntimeException)
738 {
739 	Sequence< ::rtl::OUString > aNames = VCLXAccessibleComponent::getSupportedServiceNames();
740 	sal_Int32 nLength = aNames.getLength();
741 	aNames.realloc( nLength + 1 );
742 	aNames[nLength] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.accessibility.AccessibleList"));
743 	return aNames;
744 }
745 // -----------------------------------------------------------------------------
746 
747 void VCLXAccessibleList::UpdateVisibleLineCount()
748 {
749 	if ( m_pListBoxHelper )
750 	{
751 		if ( (m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) == WB_DROPDOWN )
752 			m_nVisibleLineCount = m_pListBoxHelper->GetDisplayLineCount();
753 		else
754 		{
755 			sal_uInt16 nCols = 0,
756 				nLines = 0;
757 			m_pListBoxHelper->GetMaxVisColumnsAndLines (nCols, nLines);
758 			m_nVisibleLineCount = nLines;
759 		}
760 	}
761 }
762 
763 // -----------------------------------------------------------------------------
764 void VCLXAccessibleList::UpdateEntryRange_Impl()
765 {
766 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
767 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
768 
769 	sal_Int32 nTop = m_nLastTopEntry;
770 
771 	if ( m_pListBoxHelper )
772 		nTop = m_pListBoxHelper->GetTopEntry();
773 	if ( nTop != m_nLastTopEntry )
774 	{
775         UpdateVisibleLineCount();
776 		sal_Int32 nBegin = Min( m_nLastTopEntry, nTop );
777 		sal_Int32 nEnd = Max( m_nLastTopEntry + m_nVisibleLineCount, nTop + m_nVisibleLineCount );
778 		for (sal_uInt16 i = static_cast<sal_uInt16>(nBegin); (i <= static_cast<sal_uInt16>(nEnd)); ++i)
779 		{
780 			sal_Bool bVisible = ( i >= nTop && i < ( nTop + m_nVisibleLineCount ) );
781 			Reference< XAccessible > xHold;
782 			if ( i < m_aAccessibleChildren.size() )
783 				xHold = m_aAccessibleChildren[i];
784 			else if ( bVisible )
785 				xHold = CreateChild(i);
786 
787 			if ( xHold.is() )
788 				static_cast< VCLXAccessibleListItem* >( xHold.get() )->SetVisible( m_bVisible && bVisible );
789 		}
790 	}
791 
792 	m_nLastTopEntry = nTop;
793 }
794 // -----------------------------------------------------------------------------
795 sal_Bool VCLXAccessibleList::checkEntrySelected(sal_uInt16 _nPos,Any& _rNewValue,Reference< XAccessible >& _rxNewAcc)
796 {
797 	OSL_ENSURE(m_pListBoxHelper,"Helper is not valid!");
798 	sal_Bool bNowSelected = sal_False;
799 	if ( m_pListBoxHelper )
800 	{
801 		bNowSelected = m_pListBoxHelper->IsEntryPosSelected (_nPos);
802 		if ( bNowSelected )
803 		{
804 			_rxNewAcc = CreateChild(_nPos);
805 			_rNewValue <<= _rxNewAcc;
806 		}
807 	}
808 	return bNowSelected;
809 }
810 // -----------------------------------------------------------------------------
811 
812 void VCLXAccessibleList::UpdateSelection_Impl(sal_uInt16)
813 {
814 	uno::Any aOldValue, aNewValue;
815 
816 	{
817 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
818 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
819 	    Reference< XAccessible > xNewAcc;
820 
821 		if ( m_pListBoxHelper )
822 		{
823 			sal_uInt16 i=0;
824 			m_nCurSelectedPos = LISTBOX_ENTRY_NOTFOUND;
825 			for ( ListItems::iterator aIter = m_aAccessibleChildren.begin();
826 				  aIter != m_aAccessibleChildren.end(); ++aIter,++i)
827 			{
828 				Reference< XAccessible > xHold = *aIter;
829 				if ( xHold.is() )
830 				{
831 					VCLXAccessibleListItem* pItem = static_cast< VCLXAccessibleListItem* >( xHold.get() );
832 					// Retrieve the item's index from the list entry.
833 					sal_Bool bNowSelected = m_pListBoxHelper->IsEntryPosSelected (i);
834 					if (bNowSelected)
835 						m_nCurSelectedPos = i;
836 
837 					if ( bNowSelected && !pItem->IsSelected() )
838 					{
839 						xNewAcc = *aIter;
840 						aNewValue <<= xNewAcc;
841 					}
842 					else if ( pItem->IsSelected() )
843 						m_nLastSelectedPos = i;
844 
845 					pItem->SetSelected( bNowSelected );
846 				}
847 				else
848 				{ // it could happen that a child was not created before
849 					checkEntrySelected(i,aNewValue,xNewAcc);
850 				}
851 			}
852 			sal_uInt16 nCount = m_pListBoxHelper->GetEntryCount();
853 			if ( i < nCount ) // here we have to check the if any other listbox entry is selected
854 			{
855 				for (; i < nCount && !checkEntrySelected(i,aNewValue,xNewAcc) ;++i )
856 					;
857 			}
858 			if ( xNewAcc.is() && GetWindow()->HasFocus() )
859 			{
860 				if ( m_nLastSelectedPos != LISTBOX_ENTRY_NOTFOUND )
861 					aOldValue <<= getAccessibleChild( (sal_Int32)m_nLastSelectedPos );
862 				aNewValue <<= xNewAcc;
863 			}
864 		}
865 	}
866 	if (!m_pListBoxHelper->IsInDropDown())
867 	{
868 	}
869 	else
870 	{
871 		if ( aNewValue.hasValue() || aOldValue.hasValue() )
872 			NotifyAccessibleEvent(
873 				AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
874 				aOldValue,
875 				aNewValue );
876 		//the SELECTION_CHANGED is not necessary
877 		//NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, Any(), Any() );
878 	}
879 }
880 
881 // -----------------------------------------------------------------------------
882 // XAccessibleSelection
883 // -----------------------------------------------------------------------------
884 void SAL_CALL VCLXAccessibleList::selectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
885 {
886 	sal_Bool bNotify = sal_False;
887 
888 	{
889 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
890 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
891 
892 		if ( m_pListBoxHelper )
893 		{
894 			checkSelection_Impl(nChildIndex,*m_pListBoxHelper,sal_False);
895 
896 			m_pListBoxHelper->SelectEntryPos( (sal_uInt16)nChildIndex, sal_True );
897 			// call the select handler, don't handle events in this time
898 			m_bDisableProcessEvent = true;
899 			m_pListBoxHelper->Select();
900 			m_bDisableProcessEvent = false;
901 			bNotify = sal_True;
902 		}
903 	}
904 
905 	if ( bNotify )
906         UpdateSelection_Impl();
907 }
908 // -----------------------------------------------------------------------------
909 sal_Bool SAL_CALL VCLXAccessibleList::isAccessibleChildSelected( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
910 {
911 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
912 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
913 
914     sal_Bool bRet = sal_False;
915 	if ( m_pListBoxHelper )
916 	{
917 		checkSelection_Impl(nChildIndex,*m_pListBoxHelper,sal_False);
918 
919     	bRet = m_pListBoxHelper->IsEntryPosSelected( (sal_uInt16)nChildIndex );
920 	}
921     return bRet;
922 }
923 // -----------------------------------------------------------------------------
924 void SAL_CALL VCLXAccessibleList::clearAccessibleSelection(  ) throw (RuntimeException)
925 {
926 	sal_Bool bNotify = sal_False;
927 
928 	{
929 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
930 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
931 
932 		if ( m_pListBoxHelper )
933 		{
934 	    	m_pListBoxHelper->SetNoSelection();
935 			bNotify = sal_True;
936 		}
937 	}
938 
939 	if ( bNotify )
940         UpdateSelection_Impl();
941 }
942 // -----------------------------------------------------------------------------
943 void SAL_CALL VCLXAccessibleList::selectAllAccessibleChildren(  ) throw (RuntimeException)
944 {
945 	sal_Bool bNotify = sal_False;
946 
947 	{
948 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
949 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
950 
951 		if ( m_pListBoxHelper )
952 		{
953 	    	sal_uInt16 nCount = m_pListBoxHelper->GetEntryCount();
954 	    	for ( sal_uInt16 i = 0; i < nCount; ++i )
955 	    		m_pListBoxHelper->SelectEntryPos( i, sal_True );
956 			// call the select handler, don't handle events in this time
957 			m_bDisableProcessEvent = true;
958 			m_pListBoxHelper->Select();
959 			m_bDisableProcessEvent = false;
960 			bNotify = sal_True;
961 		}
962 	}
963 
964 	if ( bNotify )
965         UpdateSelection_Impl();
966 }
967 // -----------------------------------------------------------------------------
968 sal_Int32 SAL_CALL VCLXAccessibleList::getSelectedAccessibleChildCount(  ) throw (RuntimeException)
969 {
970 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
971 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
972 
973     sal_Int32 nCount = 0;
974 	if ( m_pListBoxHelper )
975    		nCount = m_pListBoxHelper->GetSelectEntryCount();
976     return nCount;
977 }
978 // -----------------------------------------------------------------------------
979 Reference< XAccessible > SAL_CALL VCLXAccessibleList::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
980 {
981 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
982 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
983 
984 	if ( m_pListBoxHelper )
985 	{
986 		checkSelection_Impl(nSelectedChildIndex,*m_pListBoxHelper,sal_True);
987 		return getAccessibleChild( (sal_Int32)m_pListBoxHelper->GetSelectEntryPos( (sal_uInt16)nSelectedChildIndex ) );
988 	}
989 
990 	return NULL;
991 }
992 // -----------------------------------------------------------------------------
993 void SAL_CALL VCLXAccessibleList::deselectAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
994 {
995 	sal_Bool bNotify = sal_False;
996 
997 	{
998 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
999 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
1000 
1001 		if ( m_pListBoxHelper )
1002 		{
1003 			checkSelection_Impl(nSelectedChildIndex,*m_pListBoxHelper,sal_False);
1004 
1005 			m_pListBoxHelper->SelectEntryPos( (sal_uInt16)nSelectedChildIndex, sal_False );
1006 			// call the select handler, don't handle events in this time
1007 			m_bDisableProcessEvent = true;
1008 			m_pListBoxHelper->Select();
1009 			m_bDisableProcessEvent = false;
1010 			bNotify = sal_True;
1011 		}
1012 	}
1013 
1014 	if ( bNotify )
1015         UpdateSelection_Impl();
1016 }
1017 // -----------------------------------------------------------------------------
1018 // accessibility::XAccessibleComponent
1019 awt::Rectangle VCLXAccessibleList::implGetBounds() throw (uno::RuntimeException)
1020 {
1021 	awt::Rectangle aBounds ( 0, 0, 0, 0 );
1022 	if ( m_pListBoxHelper
1023 		&& (m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) == WB_DROPDOWN )
1024 	{
1025 		if ( m_pListBoxHelper->IsInDropDown() )
1026 			aBounds = AWTRectangle(m_pListBoxHelper->GetDropDownPosSizePixel());
1027 	}
1028 	else
1029 	{
1030 		// a list has the same bounds as his parent but starts at (0,0)
1031 		aBounds = VCLXAccessibleComponent::implGetBounds();
1032 		aBounds.X = 0;
1033 		aBounds.Y = 0;
1034 		if ( m_aBoxType == COMBOBOX )
1035 		{
1036 			ComboBox* pBox = static_cast<ComboBox*>(GetWindow());
1037 			if ( pBox )
1038 			{
1039 				Size aSize = pBox->GetSubEdit()->GetSizePixel();
1040 				aBounds.Y += aSize.Height();
1041 				aBounds.Height -= aSize.Height();
1042 			}
1043 		}
1044 	}
1045 	return aBounds;
1046 }
1047 // -----------------------------------------------------------------------------
1048 
1049 awt::Point VCLXAccessibleList::getLocationOnScreen(  ) throw (uno::RuntimeException)
1050 {
1051 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
1052 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
1053 
1054 	awt::Point aPos;
1055 	if ( m_pListBoxHelper
1056 		&& (m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) == WB_DROPDOWN )
1057 	{
1058 		if ( m_pListBoxHelper->IsInDropDown() )
1059 			aPos = AWTPoint(m_pListBoxHelper->GetDropDownPosSizePixel().TopLeft());
1060 	}
1061 	else
1062 	{
1063 		aPos = VCLXAccessibleComponent::getLocationOnScreen();
1064 		if ( m_aBoxType == COMBOBOX )
1065 		{
1066 			ComboBox* pBox = static_cast<ComboBox*>(GetWindow());
1067 			if ( pBox )
1068 			{
1069 				aPos.Y += pBox->GetSubEdit()->GetSizePixel().Height();
1070 			}
1071 		}
1072 	}
1073 	return aPos;
1074 }
1075 // -----------------------------------------------------------------------------
1076 sal_Bool	VCLXAccessibleList::IsInDropDown()
1077 {
1078 	return m_pListBoxHelper->IsInDropDown();
1079 }
1080 // -----------------------------------------------------------------------------
1081 void VCLXAccessibleList::HandleDropOpen()
1082 {
1083 	if ( !m_bDisableProcessEvent )
1084 		UpdateSelection_Impl();
1085 	if (m_nCurSelectedPos != LISTBOX_ENTRY_NOTFOUND &&
1086 		m_nLastSelectedPos != LISTBOX_ENTRY_NOTFOUND)
1087 	{
1088 		Reference< XAccessible > xChild = getAccessibleChild(m_nCurSelectedPos);
1089 		if(xChild.is())
1090 		{
1091 			uno::Any aNewValue;
1092 			aNewValue <<= xChild;
1093 			NotifyAccessibleEvent(AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,	uno::Any(), aNewValue );
1094 		}
1095 	}
1096 }
1097