Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / winaccessibility / source / service / AccEventListener.cxx
bloba8f8e705fd2c4c4b2f1952dbe38c83e2ac74fb16
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cppuhelper/bootstrap.hxx>
21 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 #include <vcl/svapp.hxx>
26 #include <AccEventListener.hxx>
27 #include <AccObjectManagerAgent.hxx>
28 #include <unomsaaevent.hxx>
30 #include <com/sun/star/accessibility/XAccessible.hpp>
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 <com/sun/star/accessibility/XAccessibleEventBroadcaster.hpp>
35 #include <com/sun/star/accessibility/XAccessibleComponent.hpp>
37 #include <stdio.h>
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::accessibility;
41 using namespace cppu;
43 AccEventListener::AccEventListener(css::accessibility::XAccessible* pAcc,
44 AccObjectManagerAgent* Agent)
45 : m_xAccessible(pAcc)
46 , pAgent(Agent)
50 AccEventListener::~AccEventListener() {}
52 /**
53 * Uno's event notifier when event is captured
54 * @param AccessibleEventObject the event object which contains information about event
56 void AccEventListener::notifyEvent(const css::accessibility::AccessibleEventObject& aEvent)
58 SolarMutexGuard g;
60 switch (aEvent.EventId)
62 case AccessibleEventId::NAME_CHANGED:
63 HandleNameChangedEvent(aEvent.NewValue);
64 break;
65 case AccessibleEventId::DESCRIPTION_CHANGED:
66 HandleDescriptionChangedEvent();
67 break;
68 case AccessibleEventId::STATE_CHANGED:
69 HandleStateChangedEvent(aEvent.OldValue, aEvent.NewValue);
70 break;
71 default:
72 break;
76 /**
77 * handle the NAME_CHANGED event
78 * @param name the new name with changed.
80 void AccEventListener::HandleNameChangedEvent(Any name)
82 if (pAgent->IsTopWinAcc(m_xAccessible.get()))
84 XAccessible* pAccDoc = pAgent->GetAccDocByAccTopWin(m_xAccessible.get());
85 if (pAccDoc)
87 pAgent->UpdateAccName(pAccDoc);
88 pAgent->NotifyAccEvent(UnoMSAAEvent::OBJECT_NAMECHANGE, pAccDoc);
92 pAgent->UpdateAccName(m_xAccessible.get(), name);
93 pAgent->NotifyAccEvent(UnoMSAAEvent::OBJECT_NAMECHANGE, m_xAccessible.get());
96 /**
97 * handle the DESCRIPTION_CHANGED event
99 void AccEventListener::HandleDescriptionChangedEvent()
101 pAgent->NotifyAccEvent(UnoMSAAEvent::OBJECT_DESCRIPTIONCHANGE, m_xAccessible.get());
105 * handle the BOUNDRECT_CHANGED event
107 void AccEventListener::HandleBoundrectChangedEvent()
109 pAgent->NotifyAccEvent(UnoMSAAEvent::BOUNDRECT_CHANGED, m_xAccessible.get());
113 * handle the VISIBLE_DATA_CHANGED event
115 void AccEventListener::HandleVisibleDataChangedEvent()
117 pAgent->UpdateValue(m_xAccessible.get());
118 pAgent->NotifyAccEvent(UnoMSAAEvent::VISIBLE_DATA_CHANGED, m_xAccessible.get());
122 * handle the STATE_CHANGED event
123 * @param oldValue the old state of the source of event
124 * @param newValue the new state of the source of event
126 void AccEventListener::HandleStateChangedEvent(Any oldValue, Any newValue)
128 sal_Int64 newV, oldV;
129 if (newValue >>= newV)
131 SetComponentState(newV, true);
133 else if (oldValue >>= oldV)
135 SetComponentState(oldV, false);
140 * set the new state and fire the MSAA event
141 * @param state new state id
142 * @param enable true if state is set, false if state is unset
144 void AccEventListener::SetComponentState(sal_Int64 state, bool enable)
146 switch (state)
148 case AccessibleStateType::FOCUSED:
149 FireStateFocusedChange(enable);
150 break;
151 default:
152 FireStatePropertyChange(state, enable);
153 break;
158 * handle the focused event
159 * @param enable true if get focus, false if lose focus
161 void AccEventListener::FireStateFocusedChange(bool enable)
163 if (enable)
165 pAgent->IncreaseState(m_xAccessible.get(), AccessibleStateType::FOCUSED);
166 pAgent->NotifyAccEvent(UnoMSAAEvent::STATE_FOCUSED, m_xAccessible.get());
168 else
170 // no focus lost event in MSAA
175 * fire the MSAA state changed event
176 * @param state the state id
177 * @param set true if state is set, false if state is unset
179 void AccEventListener::FireStatePropertyChange(sal_Int64 /*state*/, bool set)
181 if (set)
183 //get new state
185 else
187 //lose old state
192 * get the role of accessible object which is observed
194 short AccEventListener::GetRole()
196 css::uno::Reference<css::accessibility::XAccessibleContext> const xContext(
197 m_xAccessible->getAccessibleContext());
198 if (xContext.is())
200 return xContext->getAccessibleRole();
202 return -1;
206 * get the role of accessible parent object which is observed
208 short AccEventListener::GetParentRole()
210 if (m_xAccessible.is())
212 return pAgent->GetParentRole(m_xAccessible.get());
214 return -1;
217 * remove the listener from accessible object
219 void AccEventListener::RemoveMeFromBroadcaster(bool const isNotifyDestroy)
223 if (!m_xAccessible.is())
225 return;
229 css::uno::Reference<XAccessibleEventBroadcaster> const xBroadcaster(
230 m_xAccessible->getAccessibleContext(), UNO_QUERY);
231 if (xBroadcaster.is())
233 //remove the lister from accessible object
234 xBroadcaster->removeAccessibleEventListener(this);
237 catch (Exception const&)
238 { // may throw if it's already disposed - ignore that
240 if (isNotifyDestroy)
242 pAgent->NotifyDestroy(m_xAccessible.get());
244 m_xAccessible.clear(); // release cyclic reference
246 catch (...)
248 return;
253 * this method is invoked before listener is disposed
255 void AccEventListener::disposing(const css::lang::EventObject& /*Source*/)
257 SolarMutexGuard g;
259 RemoveMeFromBroadcaster(true);
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */