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 : #include <accessiblecell.hxx>
22 :
23 : #include "svx/DescriptionGenerator.hxx"
24 :
25 : #include <com/sun/star/accessibility/AccessibleRole.hpp>
26 : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
27 :
28 : #include <vcl/svapp.hxx>
29 :
30 : #include <unotools/accessiblestatesethelper.hxx>
31 :
32 : #include <editeng/outlobj.hxx>
33 : #include <svx/unoshtxt.hxx>
34 : #include <svx/svdotext.hxx>
35 :
36 : using ::rtl::OUString;
37 : using namespace ::sdr::table;
38 : using namespace ::com::sun::star;
39 : using namespace ::com::sun::star::uno;
40 : using namespace ::com::sun::star::accessibility;
41 : using namespace ::com::sun::star::lang;
42 : using namespace ::com::sun::star::container;
43 :
44 : namespace accessibility {
45 :
46 : // --------------------------------------------------------------------
47 : // AccessibleCell
48 : // --------------------------------------------------------------------
49 :
50 0 : AccessibleCell::AccessibleCell( const ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible>& rxParent, const sdr::table::CellRef& rCell, sal_Int32 nIndex, const AccessibleShapeTreeInfo& rShapeTreeInfo )
51 : : AccessibleCellBase( rxParent, AccessibleRole::TABLE_CELL )
52 : , maShapeTreeInfo( rShapeTreeInfo )
53 : , mnIndexInParent( nIndex )
54 : , mpText( NULL )
55 0 : , mxCell( rCell )
56 : {
57 0 : }
58 :
59 : // --------------------------------------------------------------------
60 :
61 0 : AccessibleCell::~AccessibleCell (void)
62 : {
63 : DBG_ASSERT( mpText == 0, "svx::AccessibleCell::~AccessibleCell(), not disposed!?" );
64 0 : }
65 :
66 : // --------------------------------------------------------------------
67 :
68 0 : void AccessibleCell::Init (void)
69 : {
70 0 : SdrView* pView = maShapeTreeInfo.GetSdrView();
71 0 : const Window* pWindow = maShapeTreeInfo.GetWindow ();
72 0 : if( (pView != NULL) && (pWindow != NULL) && mxCell.is())
73 : {
74 0 : OutlinerParaObject* pOutlinerParaObject = mxCell->GetEditOutlinerParaObject(); // Get the OutlinerParaObject if text edit is active
75 :
76 0 : bool bOwnParaObject = pOutlinerParaObject != 0;
77 :
78 0 : if( !pOutlinerParaObject )
79 0 : pOutlinerParaObject = mxCell->GetOutlinerParaObject();
80 :
81 : // create AccessibleTextHelper to handle this shape's text
82 0 : if( pOutlinerParaObject )
83 : {
84 : // non-empty text -> use full-fledged edit source right away
85 0 : ::std::auto_ptr<SvxEditSource> pEditSource( new SvxTextEditSource( mxCell->GetObject(), mxCell.get(), *pView, *pWindow) );
86 0 : mpText = new AccessibleTextHelper( pEditSource );
87 0 : mpText->SetEventSource(this);
88 : }
89 :
90 0 : if( bOwnParaObject)
91 0 : delete pOutlinerParaObject;
92 : }
93 0 : }
94 :
95 : // --------------------------------------------------------------------
96 :
97 0 : sal_Bool AccessibleCell::SetState (sal_Int16 aState)
98 : {
99 0 : sal_Bool bStateHasChanged = sal_False;
100 :
101 0 : if (aState == AccessibleStateType::FOCUSED && mpText != NULL)
102 : {
103 : // Offer FOCUSED state to edit engine and detect whether the state
104 : // changes.
105 0 : sal_Bool bIsFocused = mpText->HaveFocus ();
106 0 : mpText->SetFocus (sal_True);
107 0 : bStateHasChanged = (bIsFocused != mpText->HaveFocus ());
108 : }
109 : else
110 0 : bStateHasChanged = AccessibleContextBase::SetState (aState);
111 :
112 0 : return bStateHasChanged;
113 : }
114 :
115 : // --------------------------------------------------------------------
116 :
117 0 : sal_Bool AccessibleCell::ResetState (sal_Int16 aState)
118 : {
119 0 : sal_Bool bStateHasChanged = sal_False;
120 :
121 0 : if (aState == AccessibleStateType::FOCUSED && mpText != NULL)
122 : {
123 : // Try to remove FOCUSED state from the edit engine and detect
124 : // whether the state changes.
125 0 : sal_Bool bIsFocused = mpText->HaveFocus ();
126 0 : mpText->SetFocus (sal_False);
127 0 : bStateHasChanged = (bIsFocused != mpText->HaveFocus ());
128 : }
129 : else
130 0 : bStateHasChanged = AccessibleContextBase::ResetState (aState);
131 :
132 0 : return bStateHasChanged;
133 : }
134 :
135 : //-----------------------------------------------------------------------------
136 :
137 0 : bool AccessibleCell::operator== (const AccessibleCell& rAccessibleCell)
138 : {
139 0 : return this == &rAccessibleCell;
140 : }
141 :
142 : //-----------------------------------------------------------------------------
143 : // XInterface
144 : //-----------------------------------------------------------------------------
145 :
146 0 : Any SAL_CALL AccessibleCell::queryInterface( const Type& aType ) throw (RuntimeException)
147 : {
148 0 : return AccessibleCellBase::queryInterface( aType );
149 : }
150 :
151 : //-----------------------------------------------------------------------------
152 :
153 0 : void SAL_CALL AccessibleCell::acquire( ) throw ()
154 : {
155 0 : AccessibleCellBase::acquire();
156 0 : }
157 :
158 : //-----------------------------------------------------------------------------
159 :
160 0 : void SAL_CALL AccessibleCell::release( ) throw ()
161 : {
162 0 : AccessibleCellBase::release();
163 0 : }
164 :
165 : // --------------------------------------------------------------------
166 : // XAccessibleContext
167 : // --------------------------------------------------------------------
168 :
169 : /** The children of this cell come from the paragraphs of text.
170 : */
171 0 : sal_Int32 SAL_CALL AccessibleCell::getAccessibleChildCount() throw (::com::sun::star::uno::RuntimeException)
172 : {
173 0 : SolarMutexGuard aSolarGuard;
174 0 : ThrowIfDisposed ();
175 0 : return mpText != NULL ? mpText->GetChildCount () : 0;
176 : }
177 :
178 : // --------------------------------------------------------------------
179 :
180 : /** Forward the request to the shape. Return the requested shape or throw
181 : an exception for a wrong index.
182 : */
183 0 : Reference<XAccessible> SAL_CALL AccessibleCell::getAccessibleChild (sal_Int32 nIndex) throw (IndexOutOfBoundsException, RuntimeException)
184 : {
185 0 : SolarMutexGuard aSolarGuard;
186 0 : ThrowIfDisposed ();
187 :
188 : // todo: does GetChild throw IndexOutOfBoundsException?
189 0 : return mpText->GetChild (nIndex);
190 : }
191 :
192 : // --------------------------------------------------------------------
193 :
194 : /** Return a copy of the state set.
195 : Possible states are:
196 : ENABLED
197 : SHOWING
198 : VISIBLE
199 : */
200 0 : Reference<XAccessibleStateSet> SAL_CALL AccessibleCell::getAccessibleStateSet (void) throw (RuntimeException)
201 : {
202 0 : SolarMutexGuard aSolarGuard;
203 0 : ::osl::MutexGuard aGuard (maMutex);
204 0 : Reference<XAccessibleStateSet> xStateSet;
205 :
206 0 : if (rBHelper.bDisposed || mpText == NULL)
207 : {
208 : // Return a minimal state set that only contains the DEFUNC state.
209 0 : xStateSet = AccessibleContextBase::getAccessibleStateSet ();
210 : }
211 : else
212 : {
213 0 : ::utl::AccessibleStateSetHelper* pStateSet = static_cast< ::utl::AccessibleStateSetHelper*>(mxStateSet.get());
214 :
215 0 : if(pStateSet)
216 : {
217 : // Merge current FOCUSED state from edit engine.
218 0 : if (mpText != NULL)
219 : {
220 0 : if (mpText->HaveFocus())
221 0 : pStateSet->AddState (AccessibleStateType::FOCUSED);
222 : else
223 0 : pStateSet->RemoveState (AccessibleStateType::FOCUSED);
224 : }
225 :
226 : // Create a copy of the state set that may be modified by the
227 : // caller without affecting the current state set.
228 0 : xStateSet = Reference<XAccessibleStateSet>(new ::utl::AccessibleStateSetHelper (*pStateSet));
229 : }
230 : }
231 :
232 0 : return xStateSet;
233 : }
234 :
235 : // --------------------------------------------------------------------
236 : // XAccessibleComponent
237 : // --------------------------------------------------------------------
238 :
239 0 : sal_Bool SAL_CALL AccessibleCell::containsPoint( const ::com::sun::star::awt::Point& aPoint) throw (::com::sun::star::uno::RuntimeException)
240 : {
241 0 : return AccessibleComponentBase::containsPoint( aPoint );
242 : }
243 :
244 : /** The implementation below is at the moment straightforward. It iterates
245 : over all children (and thereby instances all children which have not
246 : been already instatiated) until a child covering the specifed point is
247 : found.
248 : This leaves room for improvement. For instance, first iterate only over
249 : the already instantiated children and only if no match is found
250 : instantiate the remaining ones.
251 : */
252 0 : Reference<XAccessible > SAL_CALL AccessibleCell::getAccessibleAtPoint ( const ::com::sun::star::awt::Point& aPoint) throw(RuntimeException)
253 : {
254 0 : SolarMutexGuard aSolarGuard;
255 0 : ::osl::MutexGuard aGuard (maMutex);
256 :
257 0 : sal_Int32 nChildCount = getAccessibleChildCount ();
258 0 : for (sal_Int32 i=0; i<nChildCount; ++i)
259 : {
260 0 : Reference<XAccessible> xChild (getAccessibleChild (i));
261 0 : if (xChild.is())
262 : {
263 0 : Reference<XAccessibleComponent> xChildComponent (xChild->getAccessibleContext(), uno::UNO_QUERY);
264 0 : if (xChildComponent.is())
265 : {
266 0 : awt::Rectangle aBBox (xChildComponent->getBounds());
267 0 : if ( (aPoint.X >= aBBox.X)
268 : && (aPoint.Y >= aBBox.Y)
269 : && (aPoint.X < aBBox.X+aBBox.Width)
270 : && (aPoint.Y < aBBox.Y+aBBox.Height) )
271 0 : return xChild;
272 0 : }
273 : }
274 0 : }
275 :
276 : // Have not found a child under the given point. Returning empty
277 : // reference to indicate this.
278 0 : return uno::Reference<XAccessible>();
279 : }
280 :
281 : // --------------------------------------------------------------------
282 :
283 0 : ::com::sun::star::awt::Rectangle SAL_CALL AccessibleCell::getBounds(void) throw(RuntimeException)
284 : {
285 0 : SolarMutexGuard aSolarGuard;
286 0 : ::osl::MutexGuard aGuard (maMutex);
287 :
288 0 : ThrowIfDisposed ();
289 0 : ::com::sun::star::awt::Rectangle aBoundingBox;
290 0 : if( mxCell.is() )
291 : {
292 : // Get the cell's bounding box in internal coordinates (in 100th of mm)
293 0 : const ::Rectangle aCellRect( mxCell->getCellRect() );
294 :
295 : // Transform coordinates from internal to pixel.
296 0 : if (maShapeTreeInfo.GetViewForwarder() == NULL)
297 0 : throw uno::RuntimeException (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AccessibleCell has no valid view forwarder")),static_cast<uno::XWeak*>(this));
298 :
299 0 : ::Size aPixelSize( maShapeTreeInfo.GetViewForwarder()->LogicToPixel(::Size(aCellRect.GetWidth(), aCellRect.GetHeight())) );
300 0 : ::Point aPixelPosition( maShapeTreeInfo.GetViewForwarder()->LogicToPixel( aCellRect.TopLeft() ));
301 :
302 : // Clip the shape's bounding box with the bounding box of its parent.
303 0 : Reference<XAccessibleComponent> xParentComponent ( getAccessibleParent(), uno::UNO_QUERY);
304 0 : if (xParentComponent.is())
305 : {
306 : // Make the coordinates relative to the parent.
307 0 : awt::Point aParentLocation (xParentComponent->getLocationOnScreen());
308 0 : int x = aPixelPosition.getX() - aParentLocation.X;
309 0 : int y = aPixelPosition.getY() - aParentLocation.Y;
310 :
311 : // Clip with parent (with coordinates relative to itself).
312 0 : ::Rectangle aBBox ( x, y, x + aPixelSize.getWidth(), y + aPixelSize.getHeight());
313 0 : awt::Size aParentSize (xParentComponent->getSize());
314 0 : ::Rectangle aParentBBox (0,0, aParentSize.Width, aParentSize.Height);
315 0 : aBBox = aBBox.GetIntersection (aParentBBox);
316 0 : aBoundingBox = awt::Rectangle ( aBBox.getX(), aBBox.getY(), aBBox.getWidth(), aBBox.getHeight());
317 : }
318 : else
319 : {
320 : OSL_TRACE ("parent does not support component");
321 0 : aBoundingBox = awt::Rectangle (aPixelPosition.getX(), aPixelPosition.getY(),aPixelSize.getWidth(), aPixelSize.getHeight());
322 0 : }
323 : }
324 :
325 0 : return aBoundingBox;
326 : }
327 :
328 : // --------------------------------------------------------------------
329 :
330 0 : ::com::sun::star::awt::Point SAL_CALL AccessibleCell::getLocation(void) throw (RuntimeException)
331 : {
332 0 : ThrowIfDisposed ();
333 0 : ::com::sun::star::awt::Rectangle aBoundingBox(getBounds());
334 0 : return ::com::sun::star::awt::Point(aBoundingBox.X, aBoundingBox.Y);
335 : }
336 :
337 : // --------------------------------------------------------------------
338 :
339 0 : ::com::sun::star::awt::Point SAL_CALL AccessibleCell::getLocationOnScreen(void) throw(RuntimeException)
340 : {
341 0 : ThrowIfDisposed ();
342 :
343 : // Get relative position...
344 0 : ::com::sun::star::awt::Point aLocation(getLocation ());
345 :
346 : // ... and add absolute position of the parent.
347 0 : Reference<XAccessibleComponent> xParentComponent( getAccessibleParent(), uno::UNO_QUERY);
348 0 : if(xParentComponent.is())
349 : {
350 0 : ::com::sun::star::awt::Point aParentLocation(xParentComponent->getLocationOnScreen());
351 0 : aLocation.X += aParentLocation.X;
352 0 : aLocation.Y += aParentLocation.Y;
353 : }
354 : else
355 : {
356 : OSL_TRACE ("getLocation: parent does not support XAccessibleComponent");
357 : }
358 :
359 0 : return aLocation;
360 : }
361 :
362 : // --------------------------------------------------------------------
363 :
364 0 : awt::Size SAL_CALL AccessibleCell::getSize (void) throw (RuntimeException)
365 : {
366 0 : ThrowIfDisposed ();
367 0 : awt::Rectangle aBoundingBox (getBounds());
368 0 : return awt::Size (aBoundingBox.Width, aBoundingBox.Height);
369 : }
370 :
371 : // --------------------------------------------------------------------
372 :
373 0 : void SAL_CALL AccessibleCell::addFocusListener ( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XFocusListener >& xListener) throw (::com::sun::star::uno::RuntimeException)
374 : {
375 0 : AccessibleComponentBase::addFocusListener( xListener );
376 0 : }
377 :
378 : // --------------------------------------------------------------------
379 :
380 0 : void SAL_CALL AccessibleCell::removeFocusListener (const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XFocusListener >& xListener ) throw (::com::sun::star::uno::RuntimeException)
381 : {
382 0 : AccessibleComponentBase::removeFocusListener( xListener );
383 0 : }
384 :
385 : // --------------------------------------------------------------------
386 :
387 0 : void SAL_CALL AccessibleCell::grabFocus (void) throw (::com::sun::star::uno::RuntimeException)
388 : {
389 0 : AccessibleComponentBase::grabFocus();
390 0 : }
391 :
392 : // --------------------------------------------------------------------
393 :
394 0 : sal_Int32 SAL_CALL AccessibleCell::getForeground(void) throw (RuntimeException)
395 : {
396 0 : ThrowIfDisposed ();
397 0 : sal_Int32 nColor (0x0ffffffL);
398 :
399 : // todo
400 0 : return nColor;
401 : }
402 :
403 : // --------------------------------------------------------------------
404 :
405 0 : sal_Int32 SAL_CALL AccessibleCell::getBackground (void) throw (RuntimeException)
406 : {
407 0 : ThrowIfDisposed ();
408 0 : sal_Int32 nColor (0L);
409 :
410 : // todo
411 0 : return nColor;
412 : }
413 :
414 : // --------------------------------------------------------------------
415 : // XAccessibleExtendedComponent
416 : // --------------------------------------------------------------------
417 :
418 0 : ::com::sun::star::uno::Reference< ::com::sun::star::awt::XFont > SAL_CALL AccessibleCell::getFont (void) throw (::com::sun::star::uno::RuntimeException)
419 : {
420 : //todo
421 0 : return AccessibleComponentBase::getFont();
422 : }
423 :
424 : // --------------------------------------------------------------------
425 :
426 0 : ::rtl::OUString SAL_CALL AccessibleCell::getTitledBorderText (void) throw (::com::sun::star::uno::RuntimeException)
427 : {
428 0 : return AccessibleComponentBase::getTitledBorderText();
429 : }
430 :
431 : // --------------------------------------------------------------------
432 :
433 0 : ::rtl::OUString SAL_CALL AccessibleCell::getToolTipText (void) throw (::com::sun::star::uno::RuntimeException)
434 : {
435 0 : return AccessibleComponentBase::getToolTipText();
436 : }
437 :
438 : // --------------------------------------------------------------------
439 : // XAccessibleEventBroadcaster
440 : // --------------------------------------------------------------------
441 :
442 0 : void SAL_CALL AccessibleCell::addAccessibleEventListener( const Reference<XAccessibleEventListener >& rxListener) throw (RuntimeException)
443 : {
444 0 : SolarMutexGuard aSolarGuard;
445 0 : ::osl::MutexGuard aGuard (maMutex);
446 0 : if (rBHelper.bDisposed || rBHelper.bInDispose)
447 : {
448 0 : Reference<XInterface> xSource( static_cast<XComponent *>(this) );
449 0 : lang::EventObject aEventObj(xSource);
450 0 : rxListener->disposing(aEventObj);
451 : }
452 : else
453 : {
454 0 : AccessibleContextBase::addAccessibleEventListener (rxListener);
455 0 : if (mpText != NULL)
456 0 : mpText->AddEventListener (rxListener);
457 0 : }
458 0 : }
459 :
460 : // --------------------------------------------------------------------
461 :
462 0 : void SAL_CALL AccessibleCell::removeAccessibleEventListener( const Reference<XAccessibleEventListener >& rxListener) throw (RuntimeException)
463 : {
464 0 : SolarMutexGuard aSolarGuard;
465 0 : AccessibleContextBase::removeAccessibleEventListener(rxListener);
466 0 : if (mpText != NULL)
467 0 : mpText->RemoveEventListener (rxListener);
468 0 : }
469 :
470 : // --------------------------------------------------------------------
471 : // XServiceInfo
472 : // --------------------------------------------------------------------
473 :
474 0 : OUString SAL_CALL AccessibleCell::getImplementationName(void) throw (RuntimeException)
475 : {
476 0 : return OUString(RTL_CONSTASCII_USTRINGPARAM("AccessibleCell"));
477 : }
478 :
479 : // --------------------------------------------------------------------
480 :
481 0 : Sequence<OUString> SAL_CALL AccessibleCell::getSupportedServiceNames(void) throw (RuntimeException)
482 : {
483 0 : ThrowIfDisposed ();
484 :
485 : // Get list of supported service names from base class...
486 0 : uno::Sequence<OUString> aServiceNames = AccessibleContextBase::getSupportedServiceNames();
487 0 : sal_Int32 nCount (aServiceNames.getLength());
488 :
489 : // ...and add additional names.
490 0 : aServiceNames.realloc (nCount + 1);
491 0 : static const OUString sAdditionalServiceName (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.AccessibleCell"));
492 0 : aServiceNames[nCount] = sAdditionalServiceName;
493 :
494 0 : return aServiceNames;
495 : }
496 :
497 : // --------------------------------------------------------------------
498 : // IAccessibleViewForwarderListener
499 : // --------------------------------------------------------------------
500 :
501 0 : void AccessibleCell::ViewForwarderChanged (ChangeType /*aChangeType*/, const IAccessibleViewForwarder* /*pViewForwarder*/)
502 : {
503 : // Inform all listeners that the graphical representation (i.e. size
504 : // and/or position) of the shape has changed.
505 0 : CommitChange(AccessibleEventId::VISIBLE_DATA_CHANGED, Any(), Any());
506 :
507 : // update our children that our screen position might have changed
508 0 : if( mpText )
509 0 : mpText->UpdateChildren();
510 0 : }
511 :
512 : // --------------------------------------------------------------------
513 : // protected
514 : // --------------------------------------------------------------------
515 :
516 0 : void AccessibleCell::disposing (void)
517 : {
518 0 : SolarMutexGuard aSolarGuard;
519 0 : ::osl::MutexGuard aGuard (maMutex);
520 :
521 : // Make sure to send an event that this object looses the focus in the
522 : // case that it has the focus.
523 0 : ::utl::AccessibleStateSetHelper* pStateSet = static_cast< ::utl::AccessibleStateSetHelper*>(mxStateSet.get());
524 0 : if (pStateSet != NULL)
525 0 : pStateSet->RemoveState(AccessibleStateType::FOCUSED);
526 :
527 0 : if (mpText != NULL)
528 : {
529 0 : mpText->Dispose();
530 0 : delete mpText;
531 0 : mpText = NULL;
532 : }
533 :
534 : // Cleanup. Remove references to objects to allow them to be
535 : // destroyed.
536 0 : mxCell.clear();
537 0 : maShapeTreeInfo = AccessibleShapeTreeInfo();
538 :
539 : // Call base classes.
540 0 : AccessibleContextBase::dispose ();
541 0 : }
542 :
543 0 : sal_Int32 SAL_CALL AccessibleCell::getAccessibleIndexInParent (void) throw (RuntimeException)
544 : {
545 0 : ThrowIfDisposed ();
546 0 : return mnIndexInParent;
547 : }
548 :
549 0 : ::rtl::OUString SAL_CALL AccessibleCell::getAccessibleName (void) throw (::com::sun::star::uno::RuntimeException)
550 : {
551 0 : ThrowIfDisposed ();
552 0 : SolarMutexGuard aSolarGuard;
553 :
554 0 : if( mxCell.is() )
555 0 : return mxCell->getName();
556 :
557 0 : return AccessibleCellBase::getAccessibleName();
558 : }
559 :
560 : } // end of namespace accessibility
561 :
562 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|