1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "AccessibleDocumentViewBase.hxx"
21 #include <com/sun/star/drawing/XDrawPage.hpp>
22 #include <com/sun/star/drawing/XDrawView.hpp>
23 #include <com/sun/star/drawing/XShapes.hpp>
24 #include <com/sun/star/container/XChild.hpp>
25 #include <com/sun/star/frame/XController.hpp>
26 #include <com/sun/star/frame/XFrame.hpp>
27 #include <com/sun/star/document/XEventBroadcaster.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
30 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <rtl/ustring.h>
33 #include<sfx2/viewfrm.hxx>
35 #include <svx/AccessibleShape.hxx>
37 #include <svx/svdobj.hxx>
38 #include <svx/svdmodel.hxx>
39 #include <svx/unoapi.hxx>
40 #include <toolkit/helper/vclunohelper.hxx>
42 #include <vcl/svapp.hxx>
45 #include "ViewShell.hxx"
49 using namespace ::com::sun::star
;
50 using namespace ::com::sun::star::accessibility
;
51 using ::com::sun::star::uno::Reference
;
53 namespace accessibility
{
55 //===== internal ============================================================
56 AccessibleDocumentViewBase::AccessibleDocumentViewBase (
57 ::sd::Window
* pSdWindow
,
58 ::sd::ViewShell
* pViewShell
,
59 const uno::Reference
<frame::XController
>& rxController
,
60 const uno::Reference
<XAccessible
>& rxParent
)
61 : AccessibleContextBase (rxParent
, AccessibleRole::DOCUMENT
),
63 mxController (rxController
),
66 static_cast<SdrPaintView
*>(pViewShell
->GetView()),
67 *static_cast<OutputDevice
*>(pSdWindow
))
69 if (mxController
.is())
70 mxModel
= mxController
->getModel();
72 // Fill the shape tree info.
73 maShapeTreeInfo
.SetModelBroadcaster (
74 uno::Reference
<document::XEventBroadcaster
>(
75 mxModel
, uno::UNO_QUERY
));
76 maShapeTreeInfo
.SetController (mxController
);
77 maShapeTreeInfo
.SetSdrView (pViewShell
->GetView());
78 maShapeTreeInfo
.SetWindow (pSdWindow
);
79 maShapeTreeInfo
.SetViewForwarder (&maViewForwarder
);
81 mxWindow
= ::VCLUnoHelper::GetInterface (pSdWindow
);
87 AccessibleDocumentViewBase::~AccessibleDocumentViewBase (void)
89 // At this place we should be disposed. You may want to add a
90 // corresponding assertion into the destructor of a derived class.
96 void AccessibleDocumentViewBase::Init (void)
98 // Finish the initialization of the shape tree info container.
99 maShapeTreeInfo
.SetDocumentWindow (this);
101 // Register as window listener to stay up to date with its size and
103 mxWindow
->addWindowListener (this);
104 // Register as focus listener to
105 mxWindow
->addFocusListener (this);
107 // Determine the list of shapes on the current page.
108 uno::Reference
<drawing::XShapes
> xShapeList
;
109 uno::Reference
<drawing::XDrawView
> xView (mxController
, uno::UNO_QUERY
);
111 xShapeList
= uno::Reference
<drawing::XShapes
> (
112 xView
->getCurrentPage(), uno::UNO_QUERY
);
114 // Register this object as dispose event listener at the model.
116 mxModel
->addEventListener (
117 static_cast<awt::XWindowListener
*>(this));
119 // Register as property change listener at the controller.
120 uno::Reference
<beans::XPropertySet
> xSet (mxController
, uno::UNO_QUERY
);
122 xSet
->addPropertyChangeListener (
124 static_cast<beans::XPropertyChangeListener
*>(this));
126 // Register this object as dispose event listener at the controller.
127 if (mxController
.is())
128 mxController
->addEventListener (
129 static_cast<awt::XWindowListener
*>(this));
131 // Register at VCL Window to be informed of activated and deactivated
133 Window
* pWindow
= maShapeTreeInfo
.GetWindow();
137 this, AccessibleDocumentViewBase
, WindowChildEventListener
);
139 pWindow
->AddChildEventListener (maWindowLink
);
141 sal_uInt16 nCount
= pWindow
->GetChildCount();
142 for (sal_uInt16 i
=0; i
<nCount
; i
++)
144 Window
* pChildWindow
= pWindow
->GetChild (i
);
146 (AccessibleRole::EMBEDDED_OBJECT
147 ==pChildWindow
->GetAccessibleRole()))
149 SetAccessibleOLEObject (pChildWindow
->GetAccessible());
158 IMPL_LINK(AccessibleDocumentViewBase
, WindowChildEventListener
,
159 VclSimpleEvent
*, pEvent
)
161 OSL_ASSERT(pEvent
!=NULL
&& pEvent
->ISA(VclWindowEvent
));
162 if (pEvent
!=NULL
&& pEvent
->ISA(VclWindowEvent
))
164 VclWindowEvent
* pWindowEvent
= static_cast<VclWindowEvent
*>(pEvent
);
165 // DBG_ASSERT( pVclEvent->GetWindow(), "Window???" );
166 switch (pWindowEvent
->GetId())
168 case VCLEVENT_OBJECT_DYING
:
170 // Window is dying. Unregister from VCL Window.
171 // This is also attempted in the disposing() method.
172 Window
* pWindow
= maShapeTreeInfo
.GetWindow();
173 Window
* pDyingWindow
= static_cast<Window
*>(
174 pWindowEvent
->GetWindow());
175 if (pWindow
==pDyingWindow
&& pWindow
!=NULL
&& maWindowLink
.IsSet())
177 pWindow
->RemoveChildEventListener (maWindowLink
);
178 maWindowLink
= Link();
183 case VCLEVENT_WINDOW_SHOW
:
185 // A new window has been created. Is it an OLE object?
186 Window
* pChildWindow
= static_cast<Window
*>(
187 pWindowEvent
->GetData());
188 if (pChildWindow
!=NULL
189 && (pChildWindow
->GetAccessibleRole()
190 == AccessibleRole::EMBEDDED_OBJECT
))
192 SetAccessibleOLEObject (pChildWindow
->GetAccessible());
197 case VCLEVENT_WINDOW_HIDE
:
199 // A window has been destroyed. Has that been an OLE
201 Window
* pChildWindow
= static_cast<Window
*>(
202 pWindowEvent
->GetData());
203 if (pChildWindow
!=NULL
204 && (pChildWindow
->GetAccessibleRole()
205 == AccessibleRole::EMBEDDED_OBJECT
))
207 SetAccessibleOLEObject (NULL
);
220 //===== IAccessibleViewForwarderListener ====================================
222 void AccessibleDocumentViewBase::ViewForwarderChanged(ChangeType
, const IAccessibleViewForwarder
* )
230 //===== XAccessibleContext ==================================================
232 Reference
<XAccessible
> SAL_CALL
233 AccessibleDocumentViewBase::getAccessibleParent (void)
234 throw (uno::RuntimeException
)
238 return AccessibleContextBase::getAccessibleParent();
244 AccessibleDocumentViewBase::getAccessibleChildCount (void)
245 throw (uno::RuntimeException
)
249 if (mxAccessibleOLEObject
.is())
258 Reference
<XAccessible
> SAL_CALL
259 AccessibleDocumentViewBase::getAccessibleChild (sal_Int32 nIndex
)
260 throw (uno::RuntimeException
, lang::IndexOutOfBoundsException
)
264 ::osl::MutexGuard
aGuard (maMutex
);
265 if (mxAccessibleOLEObject
.is())
267 return mxAccessibleOLEObject
;
269 throw lang::IndexOutOfBoundsException ( "no child with index " + OUString::valueOf(nIndex
), NULL
);
275 //===== XAccessibleComponent ================================================
277 /** Iterate over all children and test whether the specified point lies
278 within one of their bounding boxes. Return the first child for which
281 uno::Reference
<XAccessible
> SAL_CALL
282 AccessibleDocumentViewBase::getAccessibleAtPoint (
283 const awt::Point
& aPoint
)
284 throw (uno::RuntimeException
)
288 ::osl::MutexGuard
aGuard (maMutex
);
289 uno::Reference
<XAccessible
> xChildAtPosition
;
291 sal_Int32 nChildCount
= getAccessibleChildCount ();
292 for (sal_Int32 i
=nChildCount
-1; i
>=0; --i
)
294 Reference
<XAccessible
> xChild (getAccessibleChild (i
));
297 Reference
<XAccessibleComponent
> xChildComponent (
298 xChild
->getAccessibleContext(), uno::UNO_QUERY
);
299 if (xChildComponent
.is())
301 awt::Rectangle
aBBox (xChildComponent
->getBounds());
302 if ( (aPoint
.X
>= aBBox
.X
)
303 && (aPoint
.Y
>= aBBox
.Y
)
304 && (aPoint
.X
< aBBox
.X
+aBBox
.Width
)
305 && (aPoint
.Y
< aBBox
.Y
+aBBox
.Height
) )
307 xChildAtPosition
= xChild
;
314 // Have not found a child under the given point. Returning empty
315 // reference to indicate this.
316 return xChildAtPosition
;
322 awt::Rectangle SAL_CALL
323 AccessibleDocumentViewBase::getBounds (void)
324 throw (::com::sun::star::uno::RuntimeException
)
328 // Transform visible area into screen coordinates.
329 ::Rectangle
aVisibleArea (
330 maShapeTreeInfo
.GetViewForwarder()->GetVisibleArea());
331 ::Point
aPixelTopLeft (
332 maShapeTreeInfo
.GetViewForwarder()->LogicToPixel (
333 aVisibleArea
.TopLeft()));
335 maShapeTreeInfo
.GetViewForwarder()->LogicToPixel (
336 aVisibleArea
.BottomRight())
339 // Prepare to subtract the parent position to transform into relative
341 awt::Point aParentPosition
;
342 Reference
<XAccessible
> xParent
= getAccessibleParent ();
345 Reference
<XAccessibleComponent
> xParentComponent (
346 xParent
->getAccessibleContext(), uno::UNO_QUERY
);
347 if (xParentComponent
.is())
348 aParentPosition
= xParentComponent
->getLocationOnScreen();
351 return awt::Rectangle (
352 aPixelTopLeft
.X() - aParentPosition
.X
,
353 aPixelTopLeft
.Y() - aParentPosition
.Y
,
362 AccessibleDocumentViewBase::getLocation (void)
363 throw (uno::RuntimeException
)
366 awt::Rectangle
aBoundingBox (getBounds());
367 return awt::Point (aBoundingBox
.X
, aBoundingBox
.Y
);
374 AccessibleDocumentViewBase::getLocationOnScreen (void)
375 throw (uno::RuntimeException
)
378 ::Point
aLogicalPoint (maShapeTreeInfo
.GetViewForwarder()->GetVisibleArea().TopLeft());
379 ::Point
aPixelPoint (maShapeTreeInfo
.GetViewForwarder()->LogicToPixel (aLogicalPoint
));
380 return awt::Point (aPixelPoint
.X(), aPixelPoint
.Y());
387 AccessibleDocumentViewBase::getSize (void)
388 throw (uno::RuntimeException
)
392 // Transform visible area into screen coordinates.
393 ::Rectangle
aVisibleArea (
394 maShapeTreeInfo
.GetViewForwarder()->GetVisibleArea());
395 ::Point
aPixelTopLeft (
396 maShapeTreeInfo
.GetViewForwarder()->LogicToPixel (
397 aVisibleArea
.TopLeft()));
399 maShapeTreeInfo
.GetViewForwarder()->LogicToPixel (
400 aVisibleArea
.BottomRight())
403 return awt::Size (aPixelSize
.X(), aPixelSize
.Y());
409 //===== XInterface ==========================================================
412 AccessibleDocumentViewBase::queryInterface (const uno::Type
& rType
)
413 throw (uno::RuntimeException
)
415 uno::Any aReturn
= AccessibleContextBase::queryInterface (rType
);
416 if ( ! aReturn
.hasValue())
417 aReturn
= ::cppu::queryInterface (rType
,
418 static_cast<XAccessibleComponent
*>(this),
419 static_cast<XAccessibleSelection
*>(this),
420 static_cast<lang::XEventListener
*>(
421 static_cast<awt::XWindowListener
*>(this)),
422 static_cast<beans::XPropertyChangeListener
*>(this),
423 static_cast<awt::XWindowListener
*>(this),
424 static_cast<awt::XFocusListener
*>(this)
433 AccessibleDocumentViewBase::acquire (void)
436 AccessibleContextBase::acquire ();
443 AccessibleDocumentViewBase::release (void)
446 AccessibleContextBase::release ();
452 //===== XServiceInfo ========================================================
455 AccessibleDocumentViewBase::getImplementationName (void)
456 throw (::com::sun::star::uno::RuntimeException
)
458 return OUString("AccessibleDocumentViewBase");
464 ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
465 AccessibleDocumentViewBase::getSupportedServiceNames (void)
466 throw (::com::sun::star::uno::RuntimeException
)
469 return AccessibleContextBase::getSupportedServiceNames ();
476 //===== XTypeProvider =======================================================
478 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Type
> SAL_CALL
479 AccessibleDocumentViewBase::getTypes (void)
480 throw (::com::sun::star::uno::RuntimeException
)
484 // Get list of types from the context base implementation, ...
485 uno::Sequence
<uno::Type
> aTypeList (AccessibleContextBase::getTypes());
486 // ... get list of types from component base implementation, ...
487 uno::Sequence
<uno::Type
> aComponentTypeList (AccessibleComponentBase::getTypes());
490 // ...and add the additional type for the component, ...
491 const uno::Type aLangEventListenerType
=
492 ::getCppuType((const uno::Reference
<lang::XEventListener
>*)0);
493 const uno::Type aPropertyChangeListenerType
=
494 ::getCppuType((const uno::Reference
<beans::XPropertyChangeListener
>*)0);
495 const uno::Type aWindowListenerType
=
496 ::getCppuType((const uno::Reference
<awt::XWindowListener
>*)0);
497 const uno::Type aFocusListenerType
=
498 ::getCppuType((const uno::Reference
<awt::XFocusListener
>*)0);
499 const uno::Type aEventBroadcaster
=
500 ::getCppuType((const uno::Reference
<XAccessibleEventBroadcaster
>*)0);
502 // ... and merge them all into one list.
503 sal_Int32
nTypeCount (aTypeList
.getLength()),
504 nComponentTypeCount (aComponentTypeList
.getLength()),
507 aTypeList
.realloc (nTypeCount
+ nComponentTypeCount
+ 5);
509 for (i
=0; i
<nComponentTypeCount
; i
++)
510 aTypeList
[nTypeCount
+ i
] = aComponentTypeList
[i
];
512 aTypeList
[nTypeCount
+ i
++ ] = aLangEventListenerType
;
513 aTypeList
[nTypeCount
+ i
++] = aPropertyChangeListenerType
;
514 aTypeList
[nTypeCount
+ i
++] = aWindowListenerType
;
515 aTypeList
[nTypeCount
+ i
++] = aFocusListenerType
;
516 aTypeList
[nTypeCount
+ i
++] = aEventBroadcaster
;
524 void AccessibleDocumentViewBase::impl_dispose()
526 // Unregister from VCL Window.
527 Window
* pWindow
= maShapeTreeInfo
.GetWindow();
528 if (maWindowLink
.IsSet())
531 pWindow
->RemoveChildEventListener (maWindowLink
);
532 maWindowLink
= Link();
536 DBG_ASSERT (pWindow
, "AccessibleDocumentViewBase::disposing");
539 // Unregister from window.
542 mxWindow
->removeWindowListener (this);
543 mxWindow
->removeFocusListener (this);
547 // Unregister form the model.
549 mxModel
->removeEventListener (
550 static_cast<awt::XWindowListener
*>(this));
552 // Unregister from the controller.
553 if (mxController
.is())
555 uno::Reference
<beans::XPropertySet
> xSet (mxController
, uno::UNO_QUERY
);
557 xSet
->removePropertyChangeListener ("", static_cast<beans::XPropertyChangeListener
*>(this));
559 mxController
->removeEventListener (
560 static_cast<awt::XWindowListener
*>(this));
563 // Propagate change of controller down the shape tree.
564 maShapeTreeInfo
.SetControllerBroadcaster (NULL
);
566 // Reset the model reference.
568 // Reset the model reference.
571 maShapeTreeInfo
.SetDocumentWindow (NULL
);
577 //===== XEventListener ======================================================
580 AccessibleDocumentViewBase::disposing (const lang::EventObject
& rEventObject
)
581 throw (::com::sun::star::uno::RuntimeException
)
585 // Register this object as dispose event and document::XEventListener
586 // listener at the model.
588 if ( ! rEventObject
.Source
.is())
590 // Paranoia. Can this really happen?
592 else if (rEventObject
.Source
== mxModel
|| rEventObject
.Source
== mxController
)
598 //===== XPropertyChangeListener =============================================
600 void SAL_CALL
AccessibleDocumentViewBase::propertyChange (const beans::PropertyChangeEvent
& )
601 throw (::com::sun::star::uno::RuntimeException
)
609 //===== XWindowListener =====================================================
612 AccessibleDocumentViewBase::windowResized (const ::com::sun::star::awt::WindowEvent
& )
613 throw (::com::sun::star::uno::RuntimeException
)
618 ViewForwarderChanged (
619 IAccessibleViewForwarderListener::VISIBLE_AREA
,
627 AccessibleDocumentViewBase::windowMoved (const ::com::sun::star::awt::WindowEvent
& )
628 throw (::com::sun::star::uno::RuntimeException
)
633 ViewForwarderChanged (
634 IAccessibleViewForwarderListener::VISIBLE_AREA
,
642 AccessibleDocumentViewBase::windowShown (const ::com::sun::star::lang::EventObject
& )
643 throw (::com::sun::star::uno::RuntimeException
)
648 ViewForwarderChanged (
649 IAccessibleViewForwarderListener::VISIBLE_AREA
,
657 AccessibleDocumentViewBase::windowHidden (const ::com::sun::star::lang::EventObject
& )
658 throw (::com::sun::star::uno::RuntimeException
)
663 ViewForwarderChanged (
664 IAccessibleViewForwarderListener::VISIBLE_AREA
,
671 //===== XFocusListener ==================================================
673 void AccessibleDocumentViewBase::focusGained (const ::com::sun::star::awt::FocusEvent
& e
)
674 throw (::com::sun::star::uno::RuntimeException
)
677 if (e
.Source
== mxWindow
)
681 void AccessibleDocumentViewBase::focusLost (const ::com::sun::star::awt::FocusEvent
& e
)
682 throw (::com::sun::star::uno::RuntimeException
)
685 if (e
.Source
== mxWindow
)
692 //===== protected internal ==================================================
694 // This method is called from the component helper base class while disposing.
695 void SAL_CALL
AccessibleDocumentViewBase::disposing (void)
699 AccessibleContextBase::disposing ();
705 /// Create a name for this view.
707 AccessibleDocumentViewBase::CreateAccessibleName (void)
708 throw (::com::sun::star::uno::RuntimeException
)
710 return OUString ("AccessibleDocumentViewBase");
716 /** Create a description for this view. Use the model's description or URL
717 if a description is not available.
720 AccessibleDocumentViewBase::CreateAccessibleDescription (void)
721 throw (::com::sun::star::uno::RuntimeException
)
723 OUString sDescription
;
725 uno::Reference
<lang::XServiceInfo
> xInfo (mxController
, uno::UNO_QUERY
);
728 OUString sFirstService
= xInfo
->getSupportedServiceNames()[0];
729 if ( sFirstService
== "com.sun.star.drawing.DrawingDocumentDrawView" )
731 sDescription
= "Draw Document";
734 sDescription
= sFirstService
;
737 sDescription
= "Accessible Draw Document";
744 void AccessibleDocumentViewBase::Activated (void)
746 // Empty. Overwrite to do something useful.
752 void AccessibleDocumentViewBase::Deactivated (void)
754 // Empty. Overwrite to do something useful.
760 void AccessibleDocumentViewBase::SetAccessibleOLEObject (
761 const Reference
<XAccessible
>& xOLEObject
)
763 // Send child event about removed accessible OLE object if necessary.
764 if (mxAccessibleOLEObject
!= xOLEObject
)
765 if (mxAccessibleOLEObject
.is())
767 AccessibleEventId::CHILD
,
769 uno::makeAny (mxAccessibleOLEObject
));
771 // Assume that the accessible OLE Object disposes itself correctly.
774 ::osl::MutexGuard
aGuard (maMutex
);
775 mxAccessibleOLEObject
= xOLEObject
;
778 // Send child event about new accessible OLE object if necessary.
779 if (mxAccessibleOLEObject
.is())
781 AccessibleEventId::CHILD
,
782 uno::makeAny (mxAccessibleOLEObject
),
789 //===== methods from AccessibleSelectionBase ==================================================
791 // return the member maMutex;
793 AccessibleDocumentViewBase::implGetMutex()
798 // return ourself as context in default case
799 uno::Reference
< XAccessibleContext
>
800 AccessibleDocumentViewBase::implGetAccessibleContext()
801 throw (uno::RuntimeException
)
806 // return sal_False in default case
808 AccessibleDocumentViewBase::implIsSelected( sal_Int32
)
809 throw (uno::RuntimeException
)
814 // return nothing in default case
816 AccessibleDocumentViewBase::implSelect( sal_Int32
, sal_Bool
)
817 throw (lang::IndexOutOfBoundsException
, uno::RuntimeException
)
821 } // end of namespace accessibility
823 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */