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