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