merge the formfield patch from ooo-build
[ooovba.git] / accessibility / source / standard / vclxaccessiblebox.cxx
blob93f65432334f018dc50f55a1c316222ca9c65adc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vclxaccessiblebox.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_accessibility.hxx"
33 #include <accessibility/standard/vclxaccessiblebox.hxx>
34 #include <accessibility/standard/vclxaccessibletextfield.hxx>
35 #include <accessibility/standard/vclxaccessibleedit.hxx>
36 #include <accessibility/standard/vclxaccessiblelist.hxx>
37 #include <accessibility/standard/vclxaccessiblelistboxlist.hxx>
38 #include <accessibility/helper/listboxhelper.hxx>
40 #include <unotools/accessiblestatesethelper.hxx>
41 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
42 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
43 #include <com/sun/star/accessibility/AccessibleRole.hpp>
44 #include <vcl/svapp.hxx>
45 #include <vcl/combobox.hxx>
46 #include <vcl/lstbox.hxx>
47 #include <accessibility/helper/accresmgr.hxx>
48 #include <accessibility/helper/accessiblestrings.hrc>
50 using namespace ::com::sun::star;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::accessibility;
56 VCLXAccessibleBox::VCLXAccessibleBox (VCLXWindow* pVCLWindow, BoxType aType, bool bIsDropDownBox)
57 : VCLXAccessibleComponent (pVCLWindow),
58 m_aBoxType (aType),
59 m_bIsDropDownBox (bIsDropDownBox),
60 m_nIndexInParent (DEFAULT_INDEX_IN_PARENT)
62 // Set up the flags that indicate which children this object has.
63 m_bHasListChild = true;
65 // A text field is not present for non drop down list boxes.
66 if ((m_aBoxType==LISTBOX) && ! m_bIsDropDownBox)
67 m_bHasTextChild = false;
68 else
69 m_bHasTextChild = true;
72 VCLXAccessibleBox::~VCLXAccessibleBox (void)
76 void VCLXAccessibleBox::ProcessWindowChildEvent( const VclWindowEvent& rVclWindowEvent )
78 uno::Any aOldValue, aNewValue;
79 uno::Reference<XAccessible> xAcc;
81 switch ( rVclWindowEvent.GetId() )
83 case VCLEVENT_WINDOW_SHOW:
84 case VCLEVENT_WINDOW_HIDE:
86 Window* pChildWindow = (Window *) rVclWindowEvent.GetData();
87 // Just compare to the combo box text field. All other children
88 // are identical to this object in which case this object will
89 // be removed in a short time.
90 if (m_aBoxType==COMBOBOX)
92 ComboBox* pComboBox = static_cast<ComboBox*>(GetWindow());
93 if ( ( pComboBox != NULL ) && ( pChildWindow != NULL ) )
94 if (pChildWindow == pComboBox->GetSubEdit())
96 if (rVclWindowEvent.GetId() == VCLEVENT_WINDOW_SHOW)
98 // Instantiate text field.
99 getAccessibleChild (0);
100 aNewValue <<= m_xText;
102 else
104 // Release text field.
105 aOldValue <<= m_xText;
106 m_xText = NULL;
108 // Tell the listeners about the new/removed child.
109 NotifyAccessibleEvent (
110 AccessibleEventId::CHILD,
111 aOldValue, aNewValue);
116 break;
118 default:
119 VCLXAccessibleComponent::ProcessWindowChildEvent (rVclWindowEvent);
123 void VCLXAccessibleBox::ProcessWindowEvent (const VclWindowEvent& rVclWindowEvent)
125 switch ( rVclWindowEvent.GetId() )
127 case VCLEVENT_DROPDOWN_OPEN:
128 case VCLEVENT_DROPDOWN_CLOSE:
129 case VCLEVENT_LISTBOX_DOUBLECLICK:
130 case VCLEVENT_LISTBOX_SCROLLED:
131 case VCLEVENT_LISTBOX_SELECT:
132 case VCLEVENT_LISTBOX_ITEMADDED:
133 case VCLEVENT_LISTBOX_ITEMREMOVED:
134 case VCLEVENT_COMBOBOX_ITEMADDED:
135 case VCLEVENT_COMBOBOX_ITEMREMOVED:
136 case VCLEVENT_COMBOBOX_SCROLLED:
138 // Forward the call to the list child.
139 VCLXAccessibleList* pList = static_cast<VCLXAccessibleList*>(m_xList.get());
140 if ( pList == NULL )
142 getAccessibleChild ( m_bHasTextChild ? 1 : 0 );
143 pList = static_cast<VCLXAccessibleList*>(m_xList.get());
145 if ( pList != NULL )
146 pList->ProcessWindowEvent (rVclWindowEvent);
147 break;
150 case VCLEVENT_COMBOBOX_SELECT:
151 case VCLEVENT_COMBOBOX_DESELECT:
153 // Selection is handled by VCLXAccessibleList which operates on
154 // the same VCL object as this box does. In case of the
155 // combobox, however, we have to help the list with providing
156 // the text of the currently selected item.
157 VCLXAccessibleList* pList = static_cast<VCLXAccessibleList*>(m_xList.get());
158 if (pList != NULL && m_xText.is())
160 Reference<XAccessibleText> xText (m_xText->getAccessibleContext(), UNO_QUERY);
161 if ( xText.is() )
163 ::rtl::OUString sText = xText->getSelectedText();
164 if ( !sText.getLength() )
165 sText = xText->getText();
166 pList->UpdateSelection (sText);
169 break;
172 case VCLEVENT_EDIT_MODIFY:
173 case VCLEVENT_EDIT_SELECTIONCHANGED:
174 // Modify/Selection events are handled by the combo box instead of
175 // directly by the edit field (Why?). Therefore, delegate this
176 // call to the edit field.
177 if (m_aBoxType==COMBOBOX)
179 if (m_xText.is())
181 Reference<XAccessibleContext> xContext = m_xText->getAccessibleContext();
182 VCLXAccessibleEdit* pEdit = static_cast<VCLXAccessibleEdit*>(xContext.get());
183 if (pEdit != NULL)
184 pEdit->ProcessWindowEvent (rVclWindowEvent);
187 break;
189 default:
190 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
194 IMPLEMENT_FORWARD_XINTERFACE2(VCLXAccessibleBox, VCLXAccessibleComponent, VCLXAccessibleBox_BASE)
195 IMPLEMENT_FORWARD_XTYPEPROVIDER2(VCLXAccessibleBox, VCLXAccessibleComponent, VCLXAccessibleBox_BASE)
197 //===== XAccessible =========================================================
199 Reference< XAccessibleContext > SAL_CALL VCLXAccessibleBox::getAccessibleContext( )
200 throw (RuntimeException)
202 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
204 return this;
207 //===== XAccessibleContext ==================================================
209 sal_Int32 SAL_CALL VCLXAccessibleBox::getAccessibleChildCount (void)
210 throw (RuntimeException)
212 vos::OGuard aSolarGuard( Application::GetSolarMutex() );
213 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
215 // Usually a box has a text field and a list of items as its children.
216 // Non drop down list boxes have no text field. Additionally check
217 // whether the object is valid.
218 sal_Int32 nCount = 0;
219 if (IsValid())
220 nCount += (m_bHasTextChild?1:0) + (m_bHasListChild?1:0);
221 else
223 // Object not valid anymore. Release references to children.
224 m_bHasTextChild = false;
225 m_xText = NULL;
226 m_bHasListChild = false;
227 m_xList = NULL;
230 return nCount;
233 Reference<XAccessible> SAL_CALL VCLXAccessibleBox::getAccessibleChild (sal_Int32 i)
234 throw (IndexOutOfBoundsException, RuntimeException)
236 vos::OGuard aSolarGuard( Application::GetSolarMutex() );
237 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
239 if (i<0 || i>=getAccessibleChildCount())
240 throw IndexOutOfBoundsException();
242 Reference< XAccessible > xChild;
243 if (IsValid())
245 if (i==1 || ! m_bHasTextChild)
247 // List.
248 if ( ! m_xList.is())
250 VCLXAccessibleList* pList = new VCLXAccessibleList ( GetVCLXWindow(),
251 (m_aBoxType == LISTBOX ? VCLXAccessibleList::LISTBOX : VCLXAccessibleList::COMBOBOX),
252 this);
253 pList->SetIndexInParent (i);
254 m_xList = pList;
256 xChild = m_xList;
258 else
260 // Text Field.
261 if ( ! m_xText.is())
263 if (m_aBoxType==COMBOBOX)
265 ComboBox* pComboBox = static_cast<ComboBox*>(GetWindow());
266 if (pComboBox!=NULL && pComboBox->GetSubEdit()!=NULL)
267 m_xText = pComboBox->GetSubEdit()->GetAccessible();
269 else if (m_bIsDropDownBox)
270 m_xText = new VCLXAccessibleTextField (GetVCLXWindow(),this);
272 xChild = m_xText;
276 return xChild;
279 sal_Int16 SAL_CALL VCLXAccessibleBox::getAccessibleRole (void) throw (RuntimeException)
281 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
283 // Return the role <const>COMBO_BOX</const> for both VCL combo boxes and
284 // VCL list boxes in DropDown-Mode else <const>PANEL</const>.
285 // This way the Java bridge has not to handle both independently.
286 return m_bIsDropDownBox ? AccessibleRole::COMBO_BOX : AccessibleRole::PANEL;
289 sal_Int32 SAL_CALL VCLXAccessibleBox::getAccessibleIndexInParent (void)
290 throw (::com::sun::star::uno::RuntimeException)
292 if (m_nIndexInParent != DEFAULT_INDEX_IN_PARENT)
293 return m_nIndexInParent;
294 else
295 return VCLXAccessibleComponent::getAccessibleIndexInParent();
298 //===== XAccessibleAction ===================================================
300 sal_Int32 SAL_CALL VCLXAccessibleBox::getAccessibleActionCount (void)
301 throw (RuntimeException)
303 ::osl::Guard< ::osl::Mutex> aGuard (GetMutex());
305 // There is one action for drop down boxes (toggle popup) and none for
306 // the other boxes.
307 return m_bIsDropDownBox ? 1 : 0;
310 sal_Bool SAL_CALL VCLXAccessibleBox::doAccessibleAction (sal_Int32 nIndex)
311 throw (IndexOutOfBoundsException, RuntimeException)
313 sal_Bool bNotify = sal_False;
316 vos::OGuard aSolarGuard( Application::GetSolarMutex() );
317 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
319 if (nIndex<0 || nIndex>=getAccessibleActionCount())
320 throw ::com::sun::star::lang::IndexOutOfBoundsException();
322 if (m_aBoxType == COMBOBOX)
324 ComboBox* pComboBox = static_cast< ComboBox* >( GetWindow() );
325 if (pComboBox != NULL)
327 pComboBox->ToggleDropDown();
328 bNotify = sal_True;
331 else if (m_aBoxType == LISTBOX)
333 ListBox* pListBox = static_cast< ListBox* >( GetWindow() );
334 if (pListBox != NULL)
336 pListBox->ToggleDropDown();
337 bNotify = sal_True;
342 if (bNotify)
343 NotifyAccessibleEvent (AccessibleEventId::ACTION_CHANGED, Any(), Any());
345 return bNotify;
348 ::rtl::OUString SAL_CALL VCLXAccessibleBox::getAccessibleActionDescription (sal_Int32 nIndex)
349 throw (IndexOutOfBoundsException, RuntimeException)
351 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
352 if (nIndex<0 || nIndex>=getAccessibleActionCount())
353 throw ::com::sun::star::lang::IndexOutOfBoundsException();
354 return TK_RES_STRING( RID_STR_ACC_ACTION_TOGGLEPOPUP);
357 Reference< XAccessibleKeyBinding > VCLXAccessibleBox::getAccessibleActionKeyBinding( sal_Int32 nIndex )
358 throw (IndexOutOfBoundsException, RuntimeException)
360 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
362 Reference< XAccessibleKeyBinding > xRet;
364 if (nIndex<0 || nIndex>=getAccessibleActionCount())
365 throw ::com::sun::star::lang::IndexOutOfBoundsException();
367 // ... which key?
368 return xRet;
371 //===== XComponent ==========================================================
373 void SAL_CALL VCLXAccessibleBox::disposing (void)
375 VCLXAccessibleComponent::disposing();