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