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 		case VCLEVENT_LISTBOX_SELECT:
431 			if ( !m_bDisableProcessEvent )
432 				UpdateSelection_Impl();
433 			break;
434 		*/
435 		// The selection events VCLEVENT_COMBOBOX_SELECT and
436 		// VCLEVENT_COMBOBOX_DESELECT are not handled here because here we
437 		// have no access to the edit field.  Its text is necessary to
438 		// identify the currently selected item.
439 
440 		case VCLEVENT_OBJECT_DYING:
441 		{
442             dispose();
443 
444             VCLXAccessibleComponent::ProcessWindowEvent (rVclWindowEvent);
445             break;
446 		}
447 
448         case VCLEVENT_LISTBOX_ITEMREMOVED:
449         case VCLEVENT_COMBOBOX_ITEMREMOVED:
450             HandleChangedItemList (false, reinterpret_cast<sal_IntPtr>(
451                 rVclWindowEvent.GetData()));
452             break;
453 
454         case VCLEVENT_LISTBOX_ITEMADDED:
455         case VCLEVENT_COMBOBOX_ITEMADDED:
456             HandleChangedItemList (true, reinterpret_cast<sal_IntPtr>(
457                 rVclWindowEvent.GetData()));
458             break;
459 		case VCLEVENT_CONTROL_GETFOCUS:
460 			{
461 	            VCLXAccessibleComponent::ProcessWindowEvent (rVclWindowEvent);
462 				// Added by IBM Symphony Acc team to handle the list item focus when List control get focus
463 				sal_Bool b_IsDropDownList = sal_True;
464 				if (m_pListBoxHelper)
465 					b_IsDropDownList = ((m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) == WB_DROPDOWN);
466 				if ( m_aBoxType == LISTBOX && !b_IsDropDownList )
467 				{
468 					if ( m_pListBoxHelper )
469 					{
470 						uno::Any	aOldValue,
471 									aNewValue;
472 						sal_uInt16 nPos = m_nCurSelectedPos;
473 
474 						if ( nPos == LISTBOX_ENTRY_NOTFOUND )
475 							nPos = m_pListBoxHelper->GetTopEntry();
476 						if ( nPos != LISTBOX_ENTRY_NOTFOUND )
477 							aNewValue <<= CreateChild(nPos);
478 						NotifyAccessibleEvent(	AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
479 												aOldValue,
480 												aNewValue );
481 					}
482 				}
483 			}
484 			break;
485 
486         default:
487             VCLXAccessibleComponent::ProcessWindowEvent (rVclWindowEvent);
488     }
489 }
490 
491  void VCLXAccessibleList::FillAccessibleRelationSet( utl::AccessibleRelationSetHelper& rRelationSet )
492 {
493 	ListBox* pBox = static_cast<ListBox*>(GetWindow());
494 	if( m_aBoxType == LISTBOX  )
495 	{
496 		if (m_pListBoxHelper && (m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) != WB_DROPDOWN)
497 		{
498 			uno::Sequence< uno::Reference< uno::XInterface > > aSequence(1);
499 			aSequence[0] = pBox->GetAccessible();
500 			rRelationSet.AddRelation( com::sun::star::accessibility::AccessibleRelation( com::sun::star::accessibility::AccessibleRelationType::MEMBER_OF, aSequence ) );
501 		}
502 	}
503 	else
504 	{
505 		VCLXAccessibleComponent::FillAccessibleRelationSet(rRelationSet);
506 	}
507 }
508 // -----------------------------------------------------------------------------
509 
510 /** To find out which item is currently selected and to update the SELECTED
511     state of the associated accessibility objects accordingly we exploit the
512     fact that the
513 */
514 void VCLXAccessibleList::UpdateSelection (::rtl::OUString sTextOfSelectedItem)
515 {
516     if ( m_aBoxType == COMBOBOX )
517     {
518         ComboBox* pBox = static_cast<ComboBox*>(GetWindow());
519         if ( pBox != NULL )
520         {
521             // Find the index of the selected item inside the VCL control...
522             sal_uInt16 nIndex = pBox->GetEntryPos (XubString(sTextOfSelectedItem));
523             // ...and then find the associated accessibility object.
524 			if ( nIndex == LISTBOX_ENTRY_NOTFOUND )
525 				nIndex = 0;
526 			UpdateSelection_Impl(nIndex);
527         }
528     }
529 }
530 // -----------------------------------------------------------------------------
531 
532 void VCLXAccessibleList::adjustEntriesIndexInParent(ListItems::iterator& _aBegin,::std::mem_fun_t<bool,VCLXAccessibleListItem>& _rMemFun)
533 {
534 	ListItems::iterator aIter = _aBegin;
535 	ListItems::iterator aEnd = m_aAccessibleChildren.end();
536 	// adjust the index inside the VCLXAccessibleListItem
537 	for (;aIter != aEnd ; ++aIter)
538 	{
539 		Reference< XAccessible > xHold = *aIter;
540 		VCLXAccessibleListItem* pItem = static_cast<VCLXAccessibleListItem*>(xHold.get());
541 		if ( pItem )
542 			_rMemFun(pItem);
543 	}
544 }
545 // -----------------------------------------------------------------------------
546 
547 Reference<XAccessible> VCLXAccessibleList::CreateChild (sal_Int32 i)
548 {
549     Reference<XAccessible> xChild;
550 
551 	sal_uInt16 nPos = static_cast<sal_uInt16>(i);
552 	if ( nPos >= m_aAccessibleChildren.size() )
553 	{
554 		m_aAccessibleChildren.resize(nPos + 1);
555 
556 		// insert into the container
557 		xChild = new VCLXAccessibleListItem(m_pListBoxHelper, i, this);
558 		m_aAccessibleChildren[nPos] = xChild;
559 	}
560 	else
561 	{
562 		xChild = m_aAccessibleChildren[nPos];
563 		// check if position is empty and can be used else we have to adjust all entries behind this
564 		if ( xChild.is() )
565 		{
566 			/*
567 			ListItems::iterator aIter = m_aAccessibleChildren.begin() + nPos;
568             ::std::mem_fun_t<bool, VCLXAccessibleListItem> aTemp(&VCLXAccessibleListItem::IncrementIndexInParent);
569 			adjustEntriesIndexInParent(	aIter, aTemp);
570 			*/
571 		}
572 		else
573 		{
574 			xChild = new VCLXAccessibleListItem(m_pListBoxHelper, i, this);
575 			m_aAccessibleChildren[nPos] = xChild;
576 		}
577 	}
578 
579     if ( xChild.is() )
580     {
581 		// Just add the SELECTED state.
582 		sal_Bool bNowSelected = sal_False;
583 		if ( m_pListBoxHelper )
584 			bNowSelected = m_pListBoxHelper->IsEntryPosSelected ((sal_uInt16)i);
585 		if (bNowSelected)
586 			m_nCurSelectedPos = sal_uInt16(i);
587         VCLXAccessibleListItem* pItem = static_cast< VCLXAccessibleListItem* >(xChild.get());
588         pItem->SetSelected( bNowSelected );
589 
590 		// Set the child's VISIBLE state.
591         UpdateVisibleLineCount();
592 		sal_uInt16 nTopEntry = 0;
593 		if ( m_pListBoxHelper )
594 			nTopEntry = m_pListBoxHelper->GetTopEntry();
595 		sal_Bool bVisible = ( nPos>=nTopEntry && nPos<( nTopEntry + m_nVisibleLineCount ) );
596 		pItem->SetVisible( m_bVisible && bVisible );
597     }
598 
599     return xChild;
600 }
601 // -----------------------------------------------------------------------------
602 
603 void VCLXAccessibleList::HandleChangedItemList (bool bItemInserted, sal_Int32 nIndex)
604 {
605 	/*
606     if ( !bItemInserted )
607 	{
608 		if ( nIndex == -1 ) // special handling here
609 		{
610 			clearItems();
611 		}
612 		else
613 		{
614 			if ( nIndex >= 0 && static_cast<sal_uInt16>(nIndex) < m_aAccessibleChildren.size() )
615 			{
616 				ListItems::iterator aIter = m_aAccessibleChildren.erase(m_aAccessibleChildren.begin()+nIndex);
617             ::std::mem_fun_t<bool, VCLXAccessibleListItem> aTemp(&VCLXAccessibleListItem::DecrementIndexInParent);
618 				adjustEntriesIndexInParent(	aIter, aTemp );
619 			}
620 		}
621 	}
622 	else
623 		getAccessibleChild(nIndex);
624 	*/
625 	clearItems();
626     NotifyAccessibleEvent (
627         AccessibleEventId::INVALIDATE_ALL_CHILDREN,
628         Any(), Any());
629 }
630 // -----------------------------------------------------------------------------
631 
632 IMPLEMENT_FORWARD_XINTERFACE2(VCLXAccessibleList, VCLXAccessibleComponent, VCLXAccessibleList_BASE)
633 IMPLEMENT_FORWARD_XTYPEPROVIDER2(VCLXAccessibleList, VCLXAccessibleComponent, VCLXAccessibleList_BASE)
634 
635 //=====  XAccessible  =========================================================
636 
637 Reference<XAccessibleContext> SAL_CALL
638     VCLXAccessibleList::getAccessibleContext (void)
639     throw (RuntimeException)
640 {
641 	return this;
642 }
643 // -----------------------------------------------------------------------------
644 
645 //=====  XAccessibleContext  ==================================================
646 
647 sal_Int32 SAL_CALL VCLXAccessibleList::getAccessibleChildCount (void)
648     throw (RuntimeException)
649 {
650 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
651 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
652 
653 	sal_Int32 nCount = 0;
654 	if ( m_pListBoxHelper )
655 		nCount = m_pListBoxHelper->GetEntryCount();
656 
657     return nCount;
658 }
659 // -----------------------------------------------------------------------------
660 
661 Reference<XAccessible> SAL_CALL VCLXAccessibleList::getAccessibleChild (sal_Int32 i)
662     throw (IndexOutOfBoundsException, RuntimeException)
663 {
664 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
665 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
666 
667 	if ( i < 0 || i >= getAccessibleChildCount() )
668 		throw IndexOutOfBoundsException();
669 
670     Reference< XAccessible > xChild;
671     // search for the child
672 	if ( i >= static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
673 		xChild = CreateChild (i);
674 	else
675 	{
676 		xChild = m_aAccessibleChildren[i];
677 		if ( !xChild.is() )
678 			xChild = CreateChild (i);
679 	}
680 	OSL_ENSURE( xChild.is(), "VCLXAccessibleList::getAccessibleChild: returning empty child!" );
681     return xChild;
682 }
683 // -----------------------------------------------------------------------------
684 
685 Reference< XAccessible > SAL_CALL VCLXAccessibleList::getAccessibleParent(  )
686 	throw (RuntimeException)
687 {
688 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
689 
690 	return m_xParent;
691 }
692 // -----------------------------------------------------------------------------
693 
694 sal_Int32 SAL_CALL VCLXAccessibleList::getAccessibleIndexInParent (void)
695     throw (::com::sun::star::uno::RuntimeException)
696 {
697     if (m_nIndexInParent != DEFAULT_INDEX_IN_PARENT)
698         return m_nIndexInParent;
699     else
700         return VCLXAccessibleComponent::getAccessibleIndexInParent();
701 }
702 // -----------------------------------------------------------------------------
703 
704 sal_Int16 SAL_CALL VCLXAccessibleList::getAccessibleRole (void)
705     throw (RuntimeException)
706 {
707     return AccessibleRole::LIST;
708 }
709 // -----------------------------------------------------------------------------
710 
711 //=====  XAccessibleComponent  ================================================
712 
713 sal_Bool SAL_CALL VCLXAccessibleList::contains( const awt::Point& rPoint ) throw (RuntimeException)
714 {
715 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
716 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
717 
718 	sal_Bool bInside = sal_False;
719 
720     Window* pListBox = GetWindow();
721     if ( pListBox )
722 	{
723 		Rectangle aRect( Point(0,0), pListBox->GetSizePixel() );
724 		bInside = aRect.IsInside( VCLPoint( rPoint ) );
725 	}
726 
727 	return bInside;
728 }
729 // -----------------------------------------------------------------------------
730 
731 Reference< XAccessible > SAL_CALL VCLXAccessibleList::getAccessibleAt( const awt::Point& rPoint )
732     throw (RuntimeException)
733 {
734 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
735 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
736 
737 	Reference< XAccessible > xChild;
738 	if ( m_pListBoxHelper )
739 	{
740         UpdateVisibleLineCount();
741 		if ( contains( rPoint ) && m_nVisibleLineCount > 0 )
742 		{
743 			Point aPos = VCLPoint( rPoint );
744 			sal_uInt16 nEndPos = m_pListBoxHelper->GetTopEntry() + (sal_uInt16)m_nVisibleLineCount;
745 			for ( sal_uInt16 i = m_pListBoxHelper->GetTopEntry(); i < nEndPos; ++i )
746 			{
747 				if ( m_pListBoxHelper->GetBoundingRectangle(i).IsInside( aPos ) )
748 				{
749 					xChild = getAccessibleChild(i);
750 					break;
751 				}
752 			}
753 		}
754 	}
755 
756     return xChild;
757 }
758 // -----------------------------------------------------------------------------
759 
760 //===== XServiceInfo ==========================================================
761 
762 ::rtl::OUString VCLXAccessibleList::getImplementationName (void)
763     throw (RuntimeException)
764 {
765 	return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.toolkit.AccessibleList"));
766 }
767 // -----------------------------------------------------------------------------
768 
769 Sequence< ::rtl::OUString > VCLXAccessibleList::getSupportedServiceNames (void)
770     throw (RuntimeException)
771 {
772 	Sequence< ::rtl::OUString > aNames = VCLXAccessibleComponent::getSupportedServiceNames();
773 	sal_Int32 nLength = aNames.getLength();
774 	aNames.realloc( nLength + 1 );
775 	aNames[nLength] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.accessibility.AccessibleList"));
776 	return aNames;
777 }
778 // -----------------------------------------------------------------------------
779 
780 void VCLXAccessibleList::UpdateVisibleLineCount()
781 {
782 	if ( m_pListBoxHelper )
783 	{
784 		if ( (m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) == WB_DROPDOWN )
785 			m_nVisibleLineCount = m_pListBoxHelper->GetDisplayLineCount();
786 		else
787 		{
788 			sal_uInt16 nCols = 0,
789 				nLines = 0;
790 			m_pListBoxHelper->GetMaxVisColumnsAndLines (nCols, nLines);
791 			m_nVisibleLineCount = nLines;
792 		}
793 	}
794 }
795 
796 // -----------------------------------------------------------------------------
797 void VCLXAccessibleList::UpdateEntryRange_Impl()
798 {
799 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
800 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
801 
802 	sal_Int32 nTop = m_nLastTopEntry;
803 
804 	if ( m_pListBoxHelper )
805 		nTop = m_pListBoxHelper->GetTopEntry();
806 	if ( nTop != m_nLastTopEntry )
807 	{
808         UpdateVisibleLineCount();
809 		sal_Int32 nBegin = Min( m_nLastTopEntry, nTop );
810 		sal_Int32 nEnd = Max( m_nLastTopEntry + m_nVisibleLineCount, nTop + m_nVisibleLineCount );
811 		for (sal_uInt16 i = static_cast<sal_uInt16>(nBegin); (i <= static_cast<sal_uInt16>(nEnd)); ++i)
812 		{
813 			sal_Bool bVisible = ( i >= nTop && i < ( nTop + m_nVisibleLineCount ) );
814 			Reference< XAccessible > xHold;
815 			if ( i < m_aAccessibleChildren.size() )
816 				xHold = m_aAccessibleChildren[i];
817 			else if ( bVisible )
818 				xHold = CreateChild(i);
819 
820 			if ( xHold.is() )
821 				static_cast< VCLXAccessibleListItem* >( xHold.get() )->SetVisible( m_bVisible && bVisible );
822 		}
823 	}
824 
825 	m_nLastTopEntry = nTop;
826 }
827 // -----------------------------------------------------------------------------
828 sal_Bool VCLXAccessibleList::checkEntrySelected(sal_uInt16 _nPos,Any& _rNewValue,Reference< XAccessible >& _rxNewAcc)
829 {
830 	OSL_ENSURE(m_pListBoxHelper,"Helper is not valid!");
831 	sal_Bool bNowSelected = sal_False;
832 	if ( m_pListBoxHelper )
833 	{
834 		bNowSelected = m_pListBoxHelper->IsEntryPosSelected (_nPos);
835 		if ( bNowSelected )
836 		{
837 			_rxNewAcc = CreateChild(_nPos);
838 			_rNewValue <<= _rxNewAcc;
839 		}
840 	}
841 	return bNowSelected;
842 }
843 // -----------------------------------------------------------------------------
844 
845 void VCLXAccessibleList::UpdateSelection_Impl(sal_uInt16)
846 {
847 	uno::Any aOldValue, aNewValue;
848 
849 	{
850 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
851 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
852 	    Reference< XAccessible > xNewAcc;
853 
854 		if ( m_pListBoxHelper )
855 		{
856 			sal_uInt16 i=0;
857 			m_nCurSelectedPos = LISTBOX_ENTRY_NOTFOUND;
858 			for ( ListItems::iterator aIter = m_aAccessibleChildren.begin();
859 				  aIter != m_aAccessibleChildren.end(); ++aIter,++i)
860 			{
861 				Reference< XAccessible > xHold = *aIter;
862 				if ( xHold.is() )
863 				{
864 					VCLXAccessibleListItem* pItem = static_cast< VCLXAccessibleListItem* >( xHold.get() );
865 					// Retrieve the item's index from the list entry.
866 					sal_Bool bNowSelected = m_pListBoxHelper->IsEntryPosSelected (i);
867 					if (bNowSelected)
868 						m_nCurSelectedPos = i;
869 
870 					if ( bNowSelected && !pItem->IsSelected() )
871 					{
872 						xNewAcc = *aIter;
873 						aNewValue <<= xNewAcc;
874 					}
875 					else if ( pItem->IsSelected() )
876 						m_nLastSelectedPos = i;
877 
878 					pItem->SetSelected( bNowSelected );
879 				}
880 				else
881 				{ // it could happen that a child was not created before
882 					checkEntrySelected(i,aNewValue,xNewAcc);
883 				}
884 			}
885 			sal_uInt16 nCount = m_pListBoxHelper->GetEntryCount();
886 			if ( i < nCount ) // here we have to check the if any other listbox entry is selected
887 			{
888 				for (; i < nCount && !checkEntrySelected(i,aNewValue,xNewAcc) ;++i )
889 					;
890 			}
891 			if ( xNewAcc.is() && GetWindow()->HasFocus() )
892 			{
893 				if ( m_nLastSelectedPos != LISTBOX_ENTRY_NOTFOUND )
894 					aOldValue <<= getAccessibleChild( (sal_Int32)m_nLastSelectedPos );
895 				aNewValue <<= xNewAcc;
896 			}
897 		}
898 	}
899 	if (!m_pListBoxHelper->IsInDropDown())
900 	{
901 	}
902 	else
903 	{
904 		if ( aNewValue.hasValue() || aOldValue.hasValue() )
905 			NotifyAccessibleEvent(
906 				AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,
907 				aOldValue,
908 				aNewValue );
909 		//the SELECTION_CHANGED is not necessary
910 		//NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, Any(), Any() );
911 	}
912 }
913 
914 // -----------------------------------------------------------------------------
915 // XAccessibleSelection
916 // -----------------------------------------------------------------------------
917 void SAL_CALL VCLXAccessibleList::selectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
918 {
919 	sal_Bool bNotify = sal_False;
920 
921 	{
922 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
923 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
924 
925 		if ( m_pListBoxHelper )
926 		{
927 			checkSelection_Impl(nChildIndex,*m_pListBoxHelper,sal_False);
928 
929 			m_pListBoxHelper->SelectEntryPos( (sal_uInt16)nChildIndex, sal_True );
930 			// call the select handler, don't handle events in this time
931 			m_bDisableProcessEvent = true;
932 			m_pListBoxHelper->Select();
933 			m_bDisableProcessEvent = false;
934 			bNotify = sal_True;
935 		}
936 	}
937 
938 	if ( bNotify )
939         UpdateSelection_Impl();
940 }
941 // -----------------------------------------------------------------------------
942 sal_Bool SAL_CALL VCLXAccessibleList::isAccessibleChildSelected( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
943 {
944 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
945 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
946 
947     sal_Bool bRet = sal_False;
948 	if ( m_pListBoxHelper )
949 	{
950 		checkSelection_Impl(nChildIndex,*m_pListBoxHelper,sal_False);
951 
952     	bRet = m_pListBoxHelper->IsEntryPosSelected( (sal_uInt16)nChildIndex );
953 	}
954     return bRet;
955 }
956 // -----------------------------------------------------------------------------
957 void SAL_CALL VCLXAccessibleList::clearAccessibleSelection(  ) throw (RuntimeException)
958 {
959 	sal_Bool bNotify = sal_False;
960 
961 	{
962 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
963 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
964 
965 		if ( m_pListBoxHelper )
966 		{
967 	    	m_pListBoxHelper->SetNoSelection();
968 			bNotify = sal_True;
969 		}
970 	}
971 
972 	if ( bNotify )
973         UpdateSelection_Impl();
974 }
975 // -----------------------------------------------------------------------------
976 void SAL_CALL VCLXAccessibleList::selectAllAccessibleChildren(  ) throw (RuntimeException)
977 {
978 	sal_Bool bNotify = sal_False;
979 
980 	{
981 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
982 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
983 
984 		if ( m_pListBoxHelper )
985 		{
986 	    	sal_uInt16 nCount = m_pListBoxHelper->GetEntryCount();
987 	    	for ( sal_uInt16 i = 0; i < nCount; ++i )
988 	    		m_pListBoxHelper->SelectEntryPos( i, sal_True );
989 			// call the select handler, don't handle events in this time
990 			m_bDisableProcessEvent = true;
991 			m_pListBoxHelper->Select();
992 			m_bDisableProcessEvent = false;
993 			bNotify = sal_True;
994 		}
995 	}
996 
997 	if ( bNotify )
998         UpdateSelection_Impl();
999 }
1000 // -----------------------------------------------------------------------------
1001 sal_Int32 SAL_CALL VCLXAccessibleList::getSelectedAccessibleChildCount(  ) throw (RuntimeException)
1002 {
1003 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
1004 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
1005 
1006     sal_Int32 nCount = 0;
1007 	if ( m_pListBoxHelper )
1008    		nCount = m_pListBoxHelper->GetSelectEntryCount();
1009     return nCount;
1010 }
1011 // -----------------------------------------------------------------------------
1012 Reference< XAccessible > SAL_CALL VCLXAccessibleList::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
1013 {
1014 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
1015 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
1016 
1017 	if ( m_pListBoxHelper )
1018 	{
1019 		checkSelection_Impl(nSelectedChildIndex,*m_pListBoxHelper,sal_True);
1020 		return getAccessibleChild( (sal_Int32)m_pListBoxHelper->GetSelectEntryPos( (sal_uInt16)nSelectedChildIndex ) );
1021 	}
1022 
1023 	return NULL;
1024 }
1025 // -----------------------------------------------------------------------------
1026 void SAL_CALL VCLXAccessibleList::deselectAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
1027 {
1028 	sal_Bool bNotify = sal_False;
1029 
1030 	{
1031 		vos::OGuard aSolarGuard( Application::GetSolarMutex() );
1032 		::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
1033 
1034 		if ( m_pListBoxHelper )
1035 		{
1036 			checkSelection_Impl(nSelectedChildIndex,*m_pListBoxHelper,sal_False);
1037 
1038 			m_pListBoxHelper->SelectEntryPos( (sal_uInt16)nSelectedChildIndex, sal_False );
1039 			// call the select handler, don't handle events in this time
1040 			m_bDisableProcessEvent = true;
1041 			m_pListBoxHelper->Select();
1042 			m_bDisableProcessEvent = false;
1043 			bNotify = sal_True;
1044 		}
1045 	}
1046 
1047 	if ( bNotify )
1048         UpdateSelection_Impl();
1049 }
1050 // -----------------------------------------------------------------------------
1051 // accessibility::XAccessibleComponent
1052 awt::Rectangle VCLXAccessibleList::implGetBounds() throw (uno::RuntimeException)
1053 {
1054 	awt::Rectangle aBounds ( 0, 0, 0, 0 );
1055 	if ( m_pListBoxHelper
1056 		&& (m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) == WB_DROPDOWN )
1057 	{
1058 		if ( m_pListBoxHelper->IsInDropDown() )
1059 			aBounds = AWTRectangle(m_pListBoxHelper->GetDropDownPosSizePixel());
1060 	}
1061 	else
1062 	{
1063 		// a list has the same bounds as his parent but starts at (0,0)
1064 		aBounds = VCLXAccessibleComponent::implGetBounds();
1065 		aBounds.X = 0;
1066 		aBounds.Y = 0;
1067 		if ( m_aBoxType == COMBOBOX )
1068 		{
1069 			ComboBox* pBox = static_cast<ComboBox*>(GetWindow());
1070 			if ( pBox )
1071 			{
1072 				Size aSize = pBox->GetSubEdit()->GetSizePixel();
1073 				//aBounds.X += aSize.Height();
1074 				//aBounds.Y += aSize.Width();
1075 				aBounds.Y += aSize.Height();
1076 				aBounds.Height -= aSize.Height();
1077 				//aBounds.Width  -= aSize.Width();
1078 			}
1079 		}
1080 	}
1081 	return aBounds;
1082 }
1083 // -----------------------------------------------------------------------------
1084 
1085 awt::Point VCLXAccessibleList::getLocationOnScreen(  ) throw (uno::RuntimeException)
1086 {
1087 	vos::OGuard aSolarGuard( Application::GetSolarMutex() );
1088 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
1089 
1090 	awt::Point aPos;
1091 	if ( m_pListBoxHelper
1092 		&& (m_pListBoxHelper->GetStyle() & WB_DROPDOWN ) == WB_DROPDOWN )
1093 	{
1094 		if ( m_pListBoxHelper->IsInDropDown() )
1095 			aPos = AWTPoint(m_pListBoxHelper->GetDropDownPosSizePixel().TopLeft());
1096 	}
1097 	else
1098 	{
1099 		aPos = VCLXAccessibleComponent::getLocationOnScreen();
1100 		if ( m_aBoxType == COMBOBOX )
1101 		{
1102 			ComboBox* pBox = static_cast<ComboBox*>(GetWindow());
1103 			if ( pBox )
1104 			{
1105 				//aPos.X += pBox->GetSubEdit()->GetSizePixel().Height();
1106 				//aPos.Y += pBox->GetSubEdit()->GetSizePixel().Width();
1107 				aPos.Y += pBox->GetSubEdit()->GetSizePixel().Height();
1108 			}
1109 		}
1110 	}
1111 	return aPos;
1112 }
1113 // -----------------------------------------------------------------------------
1114 sal_Bool	VCLXAccessibleList::IsInDropDown()
1115 {
1116 	return m_pListBoxHelper->IsInDropDown();
1117 }
1118 // -----------------------------------------------------------------------------
1119 void VCLXAccessibleList::HandleDropOpen()
1120 {
1121 	if ( !m_bDisableProcessEvent )
1122 		UpdateSelection_Impl();
1123 	if (m_nCurSelectedPos != LISTBOX_ENTRY_NOTFOUND &&
1124 		m_nLastSelectedPos != LISTBOX_ENTRY_NOTFOUND)
1125 	{
1126 		Reference< XAccessible > xChild = getAccessibleChild(m_nCurSelectedPos);
1127 		if(xChild.is())
1128 		{
1129 			uno::Any aNewValue;
1130 			aNewValue <<= xChild;
1131 			NotifyAccessibleEvent(AccessibleEventId::ACTIVE_DESCENDANT_CHANGED,	uno::Any(), aNewValue );
1132 		}
1133 	}
1134 }
1135