1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_accessibility.hxx"
30 
31 // includes --------------------------------------------------------------
32 #include <accessibility/standard/vclxaccessiblemenu.hxx>
33 
34 #include <com/sun/star/accessibility/AccessibleRole.hpp>
35 #include <vcl/menu.hxx>
36 
37 
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::accessibility;
42 using namespace ::comphelper;
43 
44 
45 // -----------------------------------------------------------------------------
46 // VCLXAccessibleMenu
47 // -----------------------------------------------------------------------------
48 
49 VCLXAccessibleMenu::VCLXAccessibleMenu( Menu* pParent, sal_uInt16 nItemPos, Menu* pMenu )
50 	:VCLXAccessibleMenuItem( pParent, nItemPos, pMenu )
51 {
52 }
53 
54 // -----------------------------------------------------------------------------
55 
56 VCLXAccessibleMenu::~VCLXAccessibleMenu()
57 {
58 }
59 
60 // -----------------------------------------------------------------------------
61 
62 sal_Bool VCLXAccessibleMenu::IsFocused()
63 {
64     sal_Bool bFocused = sal_False;
65 
66     if ( IsHighlighted() && !IsChildHighlighted() )
67         bFocused = sal_True;
68 
69     return bFocused;
70 }
71 
72 // -----------------------------------------------------------------------------
73 
74 sal_Bool VCLXAccessibleMenu::IsPopupMenuOpen()
75 {
76     sal_Bool bPopupMenuOpen = sal_False;
77 
78     if ( m_pParent )
79     {
80         PopupMenu* pPopupMenu = m_pParent->GetPopupMenu( m_pParent->GetItemId( m_nItemPos ) );
81         if ( pPopupMenu && pPopupMenu->IsMenuVisible() )
82             bPopupMenuOpen = sal_True;
83     }
84 
85     return bPopupMenuOpen;
86 }
87 
88 // -----------------------------------------------------------------------------
89 // XInterface
90 // -----------------------------------------------------------------------------
91 
92 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleMenu, VCLXAccessibleMenuItem, VCLXAccessibleMenu_BASE )
93 
94 // -----------------------------------------------------------------------------
95 // XTypeProvider
96 // -----------------------------------------------------------------------------
97 
98 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleMenu, VCLXAccessibleMenuItem, VCLXAccessibleMenu_BASE )
99 
100 // -----------------------------------------------------------------------------
101 // XServiceInfo
102 // -----------------------------------------------------------------------------
103 
104 ::rtl::OUString VCLXAccessibleMenu::getImplementationName() throw (RuntimeException)
105 {
106 	return ::rtl::OUString::createFromAscii( "com.sun.star.comp.toolkit.AccessibleMenu" );
107 }
108 
109 // -----------------------------------------------------------------------------
110 
111 Sequence< ::rtl::OUString > VCLXAccessibleMenu::getSupportedServiceNames() throw (RuntimeException)
112 {
113 	Sequence< ::rtl::OUString > aNames(1);
114 	aNames[0] = ::rtl::OUString::createFromAscii( "com.sun.star.awt.AccessibleMenu" );
115 	return aNames;
116 }
117 
118 // -----------------------------------------------------------------------------
119 // XAccessibleContext
120 // -----------------------------------------------------------------------------
121 
122 sal_Int32 VCLXAccessibleMenu::getAccessibleChildCount(  ) throw (RuntimeException)
123 {
124 	OExternalLockGuard aGuard( this );
125 
126 	return GetChildCount();
127 }
128 
129 // -----------------------------------------------------------------------------
130 
131 Reference< XAccessible > VCLXAccessibleMenu::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException, RuntimeException)
132 {
133 	OExternalLockGuard aGuard( this );
134 
135 	if ( i < 0 || i >= GetChildCount() )
136 		throw IndexOutOfBoundsException();
137 
138 	return GetChild( i );
139 }
140 
141 // -----------------------------------------------------------------------------
142 
143 sal_Int16 VCLXAccessibleMenu::getAccessibleRole(  ) throw (RuntimeException)
144 {
145 	OExternalLockGuard aGuard( this );
146 
147 	return AccessibleRole::MENU;
148 }
149 
150 // -----------------------------------------------------------------------------
151 // XAccessibleComponent
152 // -----------------------------------------------------------------------------
153 
154 Reference< XAccessible > VCLXAccessibleMenu::getAccessibleAtPoint( const awt::Point& rPoint ) throw (RuntimeException)
155 {
156 	OExternalLockGuard aGuard( this );
157 
158 	return GetChildAt( rPoint );
159 }
160 
161 // -----------------------------------------------------------------------------
162 // XAccessibleSelection
163 // -----------------------------------------------------------------------------
164 
165 void VCLXAccessibleMenu::selectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
166 {
167 	OExternalLockGuard aGuard( this );
168 
169 	if ( nChildIndex < 0 || nChildIndex >= GetChildCount() )
170 		throw IndexOutOfBoundsException();
171 
172 	SelectChild( nChildIndex );
173 }
174 
175 // -----------------------------------------------------------------------------
176 
177 sal_Bool VCLXAccessibleMenu::isAccessibleChildSelected( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
178 {
179 	OExternalLockGuard aGuard( this );
180 
181 	if ( nChildIndex < 0 || nChildIndex >= GetChildCount() )
182 		throw IndexOutOfBoundsException();
183 
184 	return IsChildSelected( nChildIndex );
185 }
186 
187 // -----------------------------------------------------------------------------
188 
189 void VCLXAccessibleMenu::clearAccessibleSelection(  ) throw (RuntimeException)
190 {
191 	OExternalLockGuard aGuard( this );
192 
193 	DeSelectAll();
194 }
195 
196 // -----------------------------------------------------------------------------
197 
198 void VCLXAccessibleMenu::selectAllAccessibleChildren(  ) throw (RuntimeException)
199 {
200 	// This method makes no sense in a menu, and so does nothing.
201 }
202 
203 // -----------------------------------------------------------------------------
204 
205 sal_Int32 VCLXAccessibleMenu::getSelectedAccessibleChildCount(  ) throw (RuntimeException)
206 {
207 	OExternalLockGuard aGuard( this );
208 
209 	sal_Int32 nRet = 0;
210 
211 	for ( sal_Int32 i = 0, nCount = GetChildCount(); i < nCount; i++ )
212 	{
213 		if ( IsChildSelected( i ) )
214 			++nRet;
215 	}
216 
217 	return nRet;
218 }
219 
220 // -----------------------------------------------------------------------------
221 
222 Reference< XAccessible > VCLXAccessibleMenu::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
223 {
224 	OExternalLockGuard aGuard( this );
225 
226 	if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
227 		throw IndexOutOfBoundsException();
228 
229 	Reference< XAccessible > xChild;
230 
231 	for ( sal_Int32 i = 0, j = 0, nCount = GetChildCount(); i < nCount; i++ )
232 	{
233 		if ( IsChildSelected( i ) && ( j++ == nSelectedChildIndex ) )
234 		{
235 			xChild = GetChild( i );
236 			break;
237 		}
238 	}
239 
240 	return xChild;
241 }
242 
243 // -----------------------------------------------------------------------------
244 
245 void VCLXAccessibleMenu::deselectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException)
246 {
247 	OExternalLockGuard aGuard( this );
248 
249 	if ( nChildIndex < 0 || nChildIndex >= GetChildCount() )
250 		throw IndexOutOfBoundsException();
251 
252 	DeSelectAll();
253 }
254 
255 // -----------------------------------------------------------------------------
256