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