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 #include <accessibility/standard/vclxaccessiblemenubar.hxx>
31 
32 #include <com/sun/star/accessibility/AccessibleRole.hpp>
33 #include <vcl/svapp.hxx>
34 #include <vcl/window.hxx>
35 #include <vcl/menu.hxx>
36 
37 
38 using namespace ::com::sun::star::accessibility;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star;
41 using namespace ::comphelper;
42 
43 
44 // -----------------------------------------------------------------------------
45 // class VCLXAccessibleMenuBar
46 // -----------------------------------------------------------------------------
47 
48 VCLXAccessibleMenuBar::VCLXAccessibleMenuBar( Menu* pMenu )
49 	:OAccessibleMenuComponent( pMenu )
50 {
51 	if ( pMenu )
52 	{
53 		m_pWindow = pMenu->GetWindow();
54 
55 		if ( m_pWindow )
56 			m_pWindow->AddEventListener( LINK( this, VCLXAccessibleMenuBar, WindowEventListener ) );
57 	}
58 }
59 
60 // -----------------------------------------------------------------------------
61 
62 VCLXAccessibleMenuBar::~VCLXAccessibleMenuBar()
63 {
64 	if ( m_pWindow )
65 		m_pWindow->RemoveEventListener( LINK( this, VCLXAccessibleMenuBar, WindowEventListener ) );
66 }
67 
68 // -----------------------------------------------------------------------------
69 
70 sal_Bool VCLXAccessibleMenuBar::IsFocused()
71 {
72     sal_Bool bFocused = sal_False;
73 
74     if ( m_pWindow && m_pWindow->HasFocus() && !IsChildHighlighted() )
75         bFocused = sal_True;
76 
77     return bFocused;
78 }
79 
80 // -----------------------------------------------------------------------------
81 
82 IMPL_LINK( VCLXAccessibleMenuBar, WindowEventListener, VclSimpleEvent*, pEvent )
83 {
84 	DBG_ASSERT( pEvent && pEvent->ISA( VclWindowEvent ), "VCLXAccessibleMenuBar::WindowEventListener: unknown window event!" );
85 	if ( pEvent && pEvent->ISA( VclWindowEvent ) )
86 	{
87 		DBG_ASSERT( ((VclWindowEvent*)pEvent)->GetWindow(), "VCLXAccessibleMenuBar::WindowEventListener: no window!" );
88         if ( !((VclWindowEvent*)pEvent)->GetWindow()->IsAccessibilityEventsSuppressed() || ( pEvent->GetId() == VCLEVENT_OBJECT_DYING ) )
89 		{
90 		    ProcessWindowEvent( *(VclWindowEvent*)pEvent );
91 		}
92 	}
93 	return 0;
94 }
95 
96 // -----------------------------------------------------------------------------
97 
98 void VCLXAccessibleMenuBar::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
99 {
100 	switch ( rVclWindowEvent.GetId() )
101 	{
102 		case VCLEVENT_WINDOW_GETFOCUS:
103 		case VCLEVENT_WINDOW_LOSEFOCUS:
104 		{
105 			SetFocused( rVclWindowEvent.GetId() == VCLEVENT_WINDOW_GETFOCUS );
106 		}
107 		break;
108 		case VCLEVENT_OBJECT_DYING:
109 		{
110 			if ( m_pWindow )
111 			{
112 				m_pWindow->RemoveEventListener( LINK( this, VCLXAccessibleMenuBar, WindowEventListener ) );
113 				m_pWindow = NULL;
114 			}
115 		}
116 		break;
117 		default:
118 		{
119 		}
120 		break;
121 	}
122 }
123 
124 // -----------------------------------------------------------------------------
125 // XComponent
126 // -----------------------------------------------------------------------------
127 
128 void VCLXAccessibleMenuBar::disposing()
129 {
130 	OAccessibleMenuComponent::disposing();
131 
132 	if ( m_pWindow )
133 	{
134 		m_pWindow->RemoveEventListener( LINK( this, VCLXAccessibleMenuBar, WindowEventListener ) );
135 		m_pWindow = NULL;
136 	}
137 }
138 
139 // -----------------------------------------------------------------------------
140 // XServiceInfo
141 // -----------------------------------------------------------------------------
142 
143 ::rtl::OUString VCLXAccessibleMenuBar::getImplementationName() throw (RuntimeException)
144 {
145 	return ::rtl::OUString::createFromAscii( "com.sun.star.comp.toolkit.AccessibleMenuBar" );
146 }
147 
148 // -----------------------------------------------------------------------------
149 
150 Sequence< ::rtl::OUString > VCLXAccessibleMenuBar::getSupportedServiceNames() throw (RuntimeException)
151 {
152 	Sequence< ::rtl::OUString > aNames(1);
153 	aNames[0] = ::rtl::OUString::createFromAscii( "com.sun.star.awt.AccessibleMenuBar" );
154 	return aNames;
155 }
156 
157 // -----------------------------------------------------------------------------
158 // XAccessibleContext
159 // -----------------------------------------------------------------------------
160 
161 sal_Int32 VCLXAccessibleMenuBar::getAccessibleIndexInParent(  ) throw (RuntimeException)
162 {
163 	OExternalLockGuard aGuard( this );
164 
165 	sal_Int32 nIndexInParent = -1;
166 
167 	if ( m_pMenu )
168 	{
169 		Window* pWindow = m_pMenu->GetWindow();
170 		if ( pWindow )
171 		{
172 			Window* pParent = pWindow->GetAccessibleParentWindow();
173 			if ( pParent )
174 			{
175 				for ( sal_uInt16 n = pParent->GetAccessibleChildWindowCount(); n; )
176 				{
177 					Window* pChild = pParent->GetAccessibleChildWindow( --n );
178 					if ( pChild == pWindow )
179 					{
180 						nIndexInParent = n;
181 						break;
182 					}
183 				}
184 			}
185 		}
186 	}
187 
188 	return nIndexInParent;
189 }
190 
191 // -----------------------------------------------------------------------------
192 
193 sal_Int16 VCLXAccessibleMenuBar::getAccessibleRole(  ) throw (RuntimeException)
194 {
195 	OExternalLockGuard aGuard( this );
196 
197 	return AccessibleRole::MENU_BAR;
198 }
199 
200 // -----------------------------------------------------------------------------
201 // XAccessibleExtendedComponent
202 // -----------------------------------------------------------------------------
203 
204 sal_Int32 VCLXAccessibleMenuBar::getBackground(  ) throw (RuntimeException)
205 {
206 	OExternalLockGuard aGuard( this );
207 
208 	return Application::GetSettings().GetStyleSettings().GetMenuBarColor().GetColor();
209 }
210 
211 // -----------------------------------------------------------------------------
212