Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifdef AIX
21 : #define _LINUX_SOURCE_COMPAT
22 : #include <sys/timer.h>
23 : #undef _LINUX_SOURCE_COMPAT
24 : #endif
25 :
26 : #include <com/sun/star/accessibility/TextSegment.hpp>
27 : #include <com/sun/star/accessibility/AccessibleEventId.hpp>
28 : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
29 : #include <com/sun/star/accessibility/AccessibleTableModelChange.hpp>
30 : #include <com/sun/star/accessibility/AccessibleTableModelChangeType.hpp>
31 : #include <com/sun/star/accessibility/XAccessibleEventBroadcaster.hpp>
32 :
33 : #include "atklistener.hxx"
34 : #include "atkwrapper.hxx"
35 : #include "vcl/svapp.hxx"
36 :
37 : #include <rtl/ref.hxx>
38 :
39 : using namespace com::sun::star;
40 :
41 0 : AtkListener::AtkListener( AtkObjectWrapper* pWrapper ) : mpWrapper( pWrapper )
42 : {
43 0 : if( mpWrapper )
44 : {
45 0 : g_object_ref( mpWrapper );
46 0 : updateChildList( mpWrapper->mpContext );
47 : }
48 0 : }
49 :
50 0 : AtkListener::~AtkListener()
51 : {
52 0 : if( mpWrapper )
53 0 : g_object_unref( mpWrapper );
54 0 : }
55 :
56 : /*****************************************************************************/
57 :
58 0 : AtkStateType mapState( const uno::Any &rAny )
59 : {
60 0 : sal_Int16 nState = accessibility::AccessibleStateType::INVALID;
61 0 : rAny >>= nState;
62 0 : return mapAtkState( nState );
63 : }
64 :
65 : /*****************************************************************************/
66 :
67 : extern "C" {
68 : // rhbz#1001768 - down to horrific problems releasing the solar mutex
69 : // while destroying a Window - which occurs inside these notifications.
70 : static gint
71 0 : idle_defunc_state_change( AtkObject *atk_obj )
72 : {
73 0 : SolarMutexGuard aGuard;
74 :
75 : // This is an equivalent to a state change to DEFUNC(T).
76 0 : atk_object_notify_state_change( atk_obj, ATK_STATE_DEFUNCT, TRUE );
77 0 : if( atk_get_focus_object() == atk_obj )
78 : {
79 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
80 0 : atk_focus_tracker_notify( NULL );
81 : SAL_WNODEPRECATED_DECLARATIONS_POP
82 : }
83 0 : g_object_unref( G_OBJECT( atk_obj ) );
84 0 : return FALSE;
85 : }
86 : }
87 :
88 : // XEventListener implementation
89 0 : void AtkListener::disposing( const lang::EventObject& ) throw (uno::RuntimeException, std::exception)
90 : {
91 0 : if( mpWrapper )
92 : {
93 0 : AtkObject *atk_obj = ATK_OBJECT( mpWrapper );
94 :
95 : // Release all interface references to avoid shutdown problems with
96 : // global mutex
97 0 : atk_object_wrapper_dispose( mpWrapper );
98 :
99 : g_idle_add( reinterpret_cast<GSourceFunc>(idle_defunc_state_change),
100 0 : g_object_ref( G_OBJECT( atk_obj ) ) );
101 :
102 : // Release the wrapper object so that it can vanish ..
103 0 : g_object_unref( mpWrapper );
104 0 : mpWrapper = NULL;
105 : }
106 0 : }
107 :
108 : /*****************************************************************************/
109 :
110 0 : static AtkObject *getObjFromAny( const uno::Any &rAny )
111 : {
112 0 : uno::Reference< accessibility::XAccessible > xAccessible;
113 0 : rAny >>= xAccessible;
114 0 : return xAccessible.is() ? atk_object_wrapper_ref( xAccessible ) : NULL;
115 : }
116 :
117 : /*****************************************************************************/
118 :
119 : // Updates the child list held to provide the old IndexInParent on children_changed::remove
120 0 : void AtkListener::updateChildList(accessibility::XAccessibleContext* pContext)
121 : {
122 0 : m_aChildList.clear();
123 :
124 0 : uno::Reference< accessibility::XAccessibleStateSet > xStateSet = pContext->getAccessibleStateSet();
125 0 : if( xStateSet.is()
126 0 : && !xStateSet->contains(accessibility::AccessibleStateType::DEFUNC)
127 0 : && !xStateSet->contains(accessibility::AccessibleStateType::MANAGES_DESCENDANTS) )
128 : {
129 0 : sal_Int32 nChildren = pContext->getAccessibleChildCount();
130 0 : m_aChildList.resize(nChildren);
131 0 : for(sal_Int32 n = 0; n < nChildren; n++)
132 : {
133 : try
134 : {
135 0 : m_aChildList[n] = pContext->getAccessibleChild(n);
136 : }
137 0 : catch (lang::IndexOutOfBoundsException const&)
138 : {
139 0 : sal_Int32 nChildren2 = pContext->getAccessibleChildCount();
140 : assert(nChildren2 <= n && "consistency?");
141 0 : m_aChildList.resize(std::min(nChildren2, n));
142 0 : break;
143 : }
144 : OSL_ASSERT(m_aChildList[n].is());
145 : }
146 0 : }
147 0 : }
148 :
149 : /*****************************************************************************/
150 :
151 0 : void AtkListener::handleChildAdded(
152 : const uno::Reference< accessibility::XAccessibleContext >& rxParent,
153 : const uno::Reference< accessibility::XAccessible>& rxAccessible)
154 : {
155 0 : AtkObject * pChild = rxAccessible.is() ? atk_object_wrapper_ref( rxAccessible ) : NULL;
156 :
157 0 : if( pChild )
158 : {
159 0 : updateChildList(rxParent.get());
160 :
161 : atk_object_wrapper_add_child( mpWrapper, pChild,
162 0 : atk_object_get_index_in_parent( pChild ));
163 :
164 0 : g_object_unref( pChild );
165 : }
166 0 : }
167 :
168 : /*****************************************************************************/
169 :
170 0 : void AtkListener::handleChildRemoved(
171 : const uno::Reference< accessibility::XAccessibleContext >& rxParent,
172 : const uno::Reference< accessibility::XAccessible>& rxChild)
173 : {
174 0 : sal_Int32 nIndex = -1;
175 :
176 : // Locate the child in the children list
177 0 : size_t n, nmax = m_aChildList.size();
178 0 : for( n = 0; n < nmax; ++n )
179 : {
180 0 : if( rxChild == m_aChildList[n] )
181 : {
182 0 : nIndex = n;
183 0 : break;
184 : }
185 : }
186 :
187 : // FIXME: two problems here:
188 : // a) we get child-removed events for objects that are no real children
189 : // in the accessibility hierarchy or have been removed before due to
190 : // some child removing batch.
191 : // b) spi_atk_bridge_signal_listener ignores the given parameters
192 : // for children_changed events and always asks the parent for the
193 : // 0. child, which breaks somehow on vanishing list boxes.
194 : // Ignoring "remove" events for objects not in the m_aChildList
195 : // for now.
196 0 : if( nIndex >= 0 )
197 : {
198 0 : updateChildList(rxParent.get());
199 :
200 0 : AtkObject * pChild = atk_object_wrapper_ref( rxChild, false );
201 0 : if( pChild )
202 : {
203 0 : atk_object_wrapper_remove_child( mpWrapper, pChild, nIndex );
204 0 : g_object_unref( pChild );
205 : }
206 : }
207 0 : }
208 :
209 : /*****************************************************************************/
210 :
211 0 : void AtkListener::handleInvalidateChildren(
212 : const uno::Reference< accessibility::XAccessibleContext >& rxParent)
213 : {
214 : // Send notifications for all previous children
215 0 : size_t n = m_aChildList.size();
216 0 : while( n-- > 0 )
217 : {
218 0 : if( m_aChildList[n].is() )
219 : {
220 0 : AtkObject * pChild = atk_object_wrapper_ref( m_aChildList[n], false );
221 0 : if( pChild )
222 : {
223 0 : atk_object_wrapper_remove_child( mpWrapper, pChild, n );
224 0 : g_object_unref( pChild );
225 : }
226 : }
227 : }
228 :
229 0 : updateChildList(rxParent.get());
230 :
231 : // Send notifications for all new children
232 0 : size_t nmax = m_aChildList.size();
233 0 : for( n = 0; n < nmax; ++n )
234 : {
235 0 : if( m_aChildList[n].is() )
236 : {
237 0 : AtkObject * pChild = atk_object_wrapper_ref( m_aChildList[n] );
238 :
239 0 : if( pChild )
240 : {
241 0 : atk_object_wrapper_add_child( mpWrapper, pChild, n );
242 0 : g_object_unref( pChild );
243 : }
244 : }
245 : }
246 0 : }
247 :
248 : /*****************************************************************************/
249 :
250 : static uno::Reference< accessibility::XAccessibleContext >
251 0 : getAccessibleContextFromSource( const uno::Reference< uno::XInterface >& rxSource )
252 : {
253 0 : uno::Reference< accessibility::XAccessibleContext > xContext(rxSource, uno::UNO_QUERY);
254 0 : if( ! xContext.is() )
255 : {
256 0 : g_warning( "ERROR: Event source does not implement XAccessibleContext" );
257 :
258 : // Second try - query for XAccessible, which should give us access to
259 : // XAccessibleContext.
260 0 : uno::Reference< accessibility::XAccessible > xAccessible(rxSource, uno::UNO_QUERY);
261 0 : if( xAccessible.is() )
262 0 : xContext = xAccessible->getAccessibleContext();
263 : }
264 :
265 0 : return xContext;
266 : }
267 :
268 : /*****************************************************************************/
269 :
270 : // XAccessibleEventListener
271 0 : void AtkListener::notifyEvent( const accessibility::AccessibleEventObject& aEvent ) throw( uno::RuntimeException, std::exception )
272 : {
273 0 : if( !mpWrapper )
274 0 : return;
275 :
276 0 : AtkObject *atk_obj = ATK_OBJECT( mpWrapper );
277 :
278 0 : switch( aEvent.EventId )
279 : {
280 : // AtkObject signals:
281 : // Hierarchy signals
282 : case accessibility::AccessibleEventId::CHILD:
283 : {
284 0 : uno::Reference< accessibility::XAccessibleContext > xParent;
285 0 : uno::Reference< accessibility::XAccessible > xChild;
286 :
287 0 : xParent = getAccessibleContextFromSource(aEvent.Source);
288 0 : g_return_if_fail( xParent.is() );
289 :
290 0 : if( aEvent.OldValue >>= xChild )
291 0 : handleChildRemoved(xParent, xChild);
292 :
293 0 : if( aEvent.NewValue >>= xChild )
294 0 : handleChildAdded(xParent, xChild);
295 : }
296 0 : break;
297 :
298 : case accessibility::AccessibleEventId::INVALIDATE_ALL_CHILDREN:
299 : {
300 0 : uno::Reference< accessibility::XAccessibleContext > xParent;
301 :
302 0 : xParent = getAccessibleContextFromSource(aEvent.Source);
303 0 : g_return_if_fail( xParent.is() );
304 :
305 0 : handleInvalidateChildren(xParent);
306 : }
307 0 : break;
308 :
309 : case accessibility::AccessibleEventId::NAME_CHANGED:
310 : {
311 0 : OUString aName;
312 0 : if( aEvent.NewValue >>= aName )
313 : {
314 : atk_object_set_name(atk_obj,
315 0 : OUStringToOString(aName, RTL_TEXTENCODING_UTF8).getStr());
316 0 : }
317 : }
318 0 : break;
319 :
320 : case accessibility::AccessibleEventId::DESCRIPTION_CHANGED:
321 : {
322 0 : OUString aDescription;
323 0 : if( aEvent.NewValue >>= aDescription )
324 : {
325 : atk_object_set_description(atk_obj,
326 0 : OUStringToOString(aDescription, RTL_TEXTENCODING_UTF8).getStr());
327 0 : }
328 : }
329 0 : break;
330 :
331 : case accessibility::AccessibleEventId::STATE_CHANGED:
332 : {
333 0 : AtkStateType eOldState = mapState( aEvent.OldValue );
334 0 : AtkStateType eNewState = mapState( aEvent.NewValue );
335 :
336 0 : gboolean bState = eNewState != ATK_STATE_INVALID;
337 0 : AtkStateType eRealState = bState ? eNewState : eOldState;
338 :
339 0 : atk_object_notify_state_change( atk_obj, eRealState, bState );
340 0 : break;
341 : }
342 :
343 : case accessibility::AccessibleEventId::BOUNDRECT_CHANGED:
344 :
345 : #ifdef HAS_ATKRECTANGLE
346 : if( ATK_IS_COMPONENT( atk_obj ) )
347 : {
348 : AtkRectangle rect;
349 :
350 : atk_component_get_extents( ATK_COMPONENT( atk_obj ),
351 : &rect.x,
352 : &rect.y,
353 : &rect.width,
354 : &rect.height,
355 : ATK_XY_SCREEN );
356 :
357 : g_signal_emit_by_name( atk_obj, "bounds_changed", &rect );
358 : }
359 : else
360 : g_warning( "bounds_changed event for object not implementing AtkComponent\n");
361 : #endif
362 :
363 0 : break;
364 :
365 : case accessibility::AccessibleEventId::VISIBLE_DATA_CHANGED:
366 0 : g_signal_emit_by_name( atk_obj, "visible-data-changed" );
367 0 : break;
368 :
369 : case accessibility::AccessibleEventId::ACTIVE_DESCENDANT_CHANGED:
370 : {
371 0 : AtkObject *pChild = getObjFromAny( aEvent.NewValue );
372 0 : if( pChild )
373 : {
374 0 : g_signal_emit_by_name( atk_obj, "active-descendant-changed", pChild );
375 0 : g_object_unref( pChild );
376 : }
377 0 : break;
378 : }
379 :
380 : // #i92103#
381 : case accessibility::AccessibleEventId::LISTBOX_ENTRY_EXPANDED:
382 : {
383 0 : AtkObject *pChild = getObjFromAny( aEvent.NewValue );
384 0 : if( pChild )
385 : {
386 0 : AtkStateType eExpandedState = ATK_STATE_EXPANDED;
387 0 : atk_object_notify_state_change( pChild, eExpandedState, true );
388 0 : g_object_unref( pChild );
389 : }
390 0 : break;
391 : }
392 :
393 : case accessibility::AccessibleEventId::LISTBOX_ENTRY_COLLAPSED:
394 : {
395 0 : AtkObject *pChild = getObjFromAny( aEvent.NewValue );
396 0 : if( pChild )
397 : {
398 0 : AtkStateType eExpandedState = ATK_STATE_EXPANDED;
399 0 : atk_object_notify_state_change( pChild, eExpandedState, false );
400 0 : g_object_unref( pChild );
401 : }
402 0 : break;
403 : }
404 :
405 : // AtkAction signals ...
406 : case accessibility::AccessibleEventId::ACTION_CHANGED:
407 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-actions");
408 0 : break;
409 :
410 : // AtkText
411 : case accessibility::AccessibleEventId::CARET_CHANGED:
412 : {
413 0 : sal_Int32 nPos=0;
414 0 : aEvent.NewValue >>= nPos;
415 0 : g_signal_emit_by_name( atk_obj, "text_caret_moved", nPos );
416 0 : break;
417 : }
418 : case accessibility::AccessibleEventId::TEXT_CHANGED:
419 : {
420 : // TESTME: and remove this comment:
421 : // cf. comphelper/source/misc/accessibletexthelper.cxx (implInitTextChangedEvent)
422 0 : accessibility::TextSegment aDeletedText;
423 0 : accessibility::TextSegment aInsertedText;
424 :
425 : // TODO: when GNOME starts to send "update" kind of events, change
426 : // we need to re-think this implementation as well
427 0 : if( aEvent.OldValue >>= aDeletedText )
428 : {
429 : /* Remember the text segment here to be able to return removed text in get_text().
430 : * This is clearly a hack to be used until appropriate API exists in atk to pass
431 : * the string value directly or we find a compelling reason to start caching the
432 : * UTF-8 converted strings in the atk wrapper object.
433 : */
434 :
435 0 : g_object_set_data( G_OBJECT(atk_obj), "ooo::text_changed::delete", &aDeletedText);
436 :
437 : g_signal_emit_by_name( atk_obj, "text_changed::delete",
438 : (gint) aDeletedText.SegmentStart,
439 0 : (gint)( aDeletedText.SegmentEnd - aDeletedText.SegmentStart ) );
440 :
441 0 : g_object_steal_data( G_OBJECT(atk_obj), "ooo::text_changed::delete" );
442 : }
443 :
444 0 : if( aEvent.NewValue >>= aInsertedText )
445 : g_signal_emit_by_name( atk_obj, "text_changed::insert",
446 : (gint) aInsertedText.SegmentStart,
447 0 : (gint)( aInsertedText.SegmentEnd - aInsertedText.SegmentStart ) );
448 0 : break;
449 : }
450 :
451 : case accessibility::AccessibleEventId::TEXT_SELECTION_CHANGED:
452 : {
453 0 : g_signal_emit_by_name( atk_obj, "text-selection-changed" );
454 0 : break;
455 : }
456 :
457 : case accessibility::AccessibleEventId::TEXT_ATTRIBUTE_CHANGED:
458 0 : g_signal_emit_by_name( atk_obj, "text-attributes-changed" );
459 0 : break;
460 :
461 : // AtkValue
462 : case accessibility::AccessibleEventId::VALUE_CHANGED:
463 0 : g_object_notify( G_OBJECT( atk_obj ), "accessible-value" );
464 0 : break;
465 :
466 : case accessibility::AccessibleEventId::CONTENT_FLOWS_FROM_RELATION_CHANGED:
467 : case accessibility::AccessibleEventId::CONTENT_FLOWS_TO_RELATION_CHANGED:
468 : case accessibility::AccessibleEventId::CONTROLLED_BY_RELATION_CHANGED:
469 : case accessibility::AccessibleEventId::CONTROLLER_FOR_RELATION_CHANGED:
470 : case accessibility::AccessibleEventId::LABEL_FOR_RELATION_CHANGED:
471 : case accessibility::AccessibleEventId::LABELED_BY_RELATION_CHANGED:
472 : case accessibility::AccessibleEventId::MEMBER_OF_RELATION_CHANGED:
473 : case accessibility::AccessibleEventId::SUB_WINDOW_OF_RELATION_CHANGED:
474 : // FIXME: ask Bill how Atk copes with this little lot ...
475 0 : break;
476 :
477 : // AtkTable
478 : case accessibility::AccessibleEventId::TABLE_MODEL_CHANGED:
479 : {
480 0 : accessibility::AccessibleTableModelChange aChange;
481 0 : aEvent.NewValue >>= aChange;
482 :
483 0 : sal_Int32 nRowsChanged = aChange.LastRow - aChange.FirstRow + 1;
484 0 : sal_Int32 nColumnsChanged = aChange.LastColumn - aChange.FirstColumn + 1;
485 :
486 : static const struct {
487 : const char *row;
488 : const char *col;
489 : } aSignalNames[] =
490 : {
491 : { NULL, NULL }, // dummy
492 : { "row_inserted", "column_inserted" }, // INSERT = 1
493 : { "row_deleted", "column_deleted" } // DELETE = 2
494 : };
495 0 : switch( aChange.Type )
496 : {
497 : case accessibility::AccessibleTableModelChangeType::INSERT:
498 : case accessibility::AccessibleTableModelChangeType::DELETE:
499 0 : if( nRowsChanged > 0 )
500 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ),
501 : aSignalNames[aChange.Type].row,
502 0 : aChange.FirstRow, nRowsChanged );
503 0 : if( nColumnsChanged > 0 )
504 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ),
505 : aSignalNames[aChange.Type].col,
506 0 : aChange.FirstColumn, nColumnsChanged );
507 0 : break;
508 :
509 : case accessibility::AccessibleTableModelChangeType::UPDATE:
510 : // This is not really a model change, is it ?
511 0 : break;
512 : default:
513 0 : g_warning( "TESTME: unusual table model change %d\n", aChange.Type );
514 0 : break;
515 : }
516 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ), "model-changed" );
517 0 : break;
518 : }
519 :
520 : case accessibility::AccessibleEventId::TABLE_COLUMN_HEADER_CHANGED:
521 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-column-header");
522 0 : break;
523 :
524 : case accessibility::AccessibleEventId::TABLE_CAPTION_CHANGED:
525 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-caption");
526 0 : break;
527 :
528 : case accessibility::AccessibleEventId::TABLE_COLUMN_DESCRIPTION_CHANGED:
529 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-column-description");
530 0 : break;
531 :
532 : case accessibility::AccessibleEventId::TABLE_ROW_DESCRIPTION_CHANGED:
533 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-row-description");
534 0 : break;
535 :
536 : case accessibility::AccessibleEventId::TABLE_ROW_HEADER_CHANGED:
537 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-row-header");
538 0 : break;
539 :
540 : case accessibility::AccessibleEventId::TABLE_SUMMARY_CHANGED:
541 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-table-summary");
542 0 : break;
543 :
544 : case accessibility::AccessibleEventId::SELECTION_CHANGED:
545 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ), "selection_changed");
546 0 : break;
547 :
548 : case accessibility::AccessibleEventId::HYPERTEXT_CHANGED:
549 0 : g_signal_emit_by_name( G_OBJECT( atk_obj ), "property_change::accessible-hypertext-offset");
550 0 : break;
551 :
552 : case accessibility::AccessibleEventId::ROLE_CHANGED:
553 : {
554 0 : uno::Reference< accessibility::XAccessibleContext > xContext;
555 0 : xContext = getAccessibleContextFromSource( aEvent.Source );
556 0 : atk_object_wrapper_set_role( mpWrapper, xContext->getAccessibleRole() );
557 0 : break;
558 : }
559 :
560 : default:
561 0 : g_warning( "Unknown event notification %d", aEvent.EventId );
562 0 : break;
563 : }
564 : }
565 :
566 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|