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 <standard/vclxaccessiblestatusbar.hxx>
21 #include <standard/vclxaccessiblestatusbaritem.hxx>
22 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
23 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24 #include <comphelper/accessiblecontexthelper.hxx>
25 #include <o3tl/safeint.hxx>
26 #include <vcl/status.hxx>
27 #include <vcl/unohelp.hxx>
28 #include <vcl/vclevent.hxx>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::lang
;
34 using namespace ::com::sun::star::accessibility
;
35 using namespace ::comphelper
;
40 VCLXAccessibleStatusBar::VCLXAccessibleStatusBar(vcl::Window
* pWindow
)
41 : VCLXAccessibleComponent(pWindow
)
43 m_pStatusBar
= GetAs
<StatusBar
>();
46 m_aAccessibleChildren
.assign( m_pStatusBar
->GetItemCount(), rtl::Reference
< VCLXAccessibleStatusBarItem
>() );
50 void VCLXAccessibleStatusBar::UpdateShowing( sal_Int32 i
, bool bShowing
)
52 if ( i
>= 0 && o3tl::make_unsigned(i
) < m_aAccessibleChildren
.size() )
54 rtl::Reference
< VCLXAccessibleStatusBarItem
> pVCLXAccessibleStatusBarItem( m_aAccessibleChildren
[i
] );
55 if ( pVCLXAccessibleStatusBarItem
)
56 pVCLXAccessibleStatusBarItem
->SetShowing( bShowing
);
61 void VCLXAccessibleStatusBar::UpdateItemName( sal_Int32 i
)
63 if ( i
< 0 || o3tl::make_unsigned(i
) >= m_aAccessibleChildren
.size() )
66 rtl::Reference
< VCLXAccessibleStatusBarItem
> pVCLXAccessibleStatusBarItem( m_aAccessibleChildren
[i
] );
67 if ( pVCLXAccessibleStatusBarItem
.is() )
69 OUString sItemName
= pVCLXAccessibleStatusBarItem
->GetItemName();
70 pVCLXAccessibleStatusBarItem
->SetItemName( sItemName
);
75 void VCLXAccessibleStatusBar::UpdateItemText( sal_Int32 i
)
77 if ( i
< 0 || o3tl::make_unsigned(i
) >= m_aAccessibleChildren
.size() )
80 rtl::Reference
< VCLXAccessibleStatusBarItem
> pVCLXAccessibleStatusBarItem( m_aAccessibleChildren
[i
] );
81 if ( pVCLXAccessibleStatusBarItem
.is() )
83 OUString sItemText
= pVCLXAccessibleStatusBarItem
->GetItemText();
84 pVCLXAccessibleStatusBarItem
->SetItemText( sItemText
);
89 void VCLXAccessibleStatusBar::InsertChild( sal_Int32 i
)
91 if ( i
< 0 || o3tl::make_unsigned(i
) > m_aAccessibleChildren
.size() )
94 // insert entry in child list
95 m_aAccessibleChildren
.insert( m_aAccessibleChildren
.begin() + i
, rtl::Reference
< VCLXAccessibleStatusBarItem
>() );
97 // send accessible child event
98 Reference
< XAccessible
> xChild( getAccessibleChild( i
) );
101 Any aOldValue
, aNewValue
;
102 aNewValue
<<= xChild
;
103 NotifyAccessibleEvent( AccessibleEventId::CHILD
, aOldValue
, aNewValue
);
108 void VCLXAccessibleStatusBar::RemoveChild( sal_Int32 i
)
110 if ( i
< 0 || o3tl::make_unsigned(i
) >= m_aAccessibleChildren
.size() )
113 // get the accessible of the removed page
114 Reference
< XAccessible
> xChild( m_aAccessibleChildren
[i
] );
116 // remove entry in child list
117 m_aAccessibleChildren
.erase( m_aAccessibleChildren
.begin() + i
);
119 // send accessible child event
122 Any aOldValue
, aNewValue
;
123 aOldValue
<<= xChild
;
124 NotifyAccessibleEvent( AccessibleEventId::CHILD
, aOldValue
, aNewValue
);
126 Reference
< XComponent
> xComponent( xChild
, UNO_QUERY
);
127 if ( xComponent
.is() )
128 xComponent
->dispose();
133 void VCLXAccessibleStatusBar::ProcessWindowEvent( const VclWindowEvent
& rVclWindowEvent
)
135 switch ( rVclWindowEvent
.GetId() )
137 case VclEventId::StatusbarItemAdded
:
141 sal_uInt16 nItemId
= static_cast<sal_uInt16
>(reinterpret_cast<sal_IntPtr
>(rVclWindowEvent
.GetData()));
142 sal_uInt16 nItemPos
= m_pStatusBar
->GetItemPos( nItemId
);
143 InsertChild( nItemPos
);
147 case VclEventId::StatusbarItemRemoved
:
151 OExternalLockGuard
aGuard( this );
153 sal_uInt16 nItemId
= static_cast<sal_uInt16
>(reinterpret_cast<sal_IntPtr
>(rVclWindowEvent
.GetData()));
154 for ( sal_Int64 i
= 0, nCount
= m_aAccessibleChildren
.size(); i
< nCount
; ++i
)
156 sal_uInt16 nChildItemId
= m_pStatusBar
->GetItemId( static_cast<sal_uInt16
>(i
) );
157 if ( nChildItemId
== nItemId
)
166 case VclEventId::StatusbarAllItemsRemoved
:
168 for ( sal_Int32 i
= m_aAccessibleChildren
.size() - 1; i
>= 0; --i
)
172 case VclEventId::StatusbarShowItem
:
173 case VclEventId::StatusbarHideItem
:
177 sal_uInt16 nItemId
= static_cast<sal_uInt16
>(reinterpret_cast<sal_IntPtr
>(rVclWindowEvent
.GetData()));
178 sal_uInt16 nItemPos
= m_pStatusBar
->GetItemPos( nItemId
);
179 UpdateShowing( nItemPos
, rVclWindowEvent
.GetId() == VclEventId::StatusbarShowItem
);
183 case VclEventId::StatusbarNameChanged
:
187 sal_uInt16 nItemId
= static_cast<sal_uInt16
>(reinterpret_cast<sal_IntPtr
>(rVclWindowEvent
.GetData()));
188 sal_uInt16 nItemPos
= m_pStatusBar
->GetItemPos( nItemId
);
189 UpdateItemName( nItemPos
);
193 case VclEventId::StatusbarDrawItem
:
197 sal_uInt16 nItemId
= static_cast<sal_uInt16
>(reinterpret_cast<sal_IntPtr
>(rVclWindowEvent
.GetData()));
198 sal_uInt16 nItemPos
= m_pStatusBar
->GetItemPos( nItemId
);
199 UpdateItemText( nItemPos
);
203 case VclEventId::ObjectDying
:
207 m_pStatusBar
= nullptr;
209 // dispose all children
210 for (const rtl::Reference
<VCLXAccessibleStatusBarItem
>& xComponent
: m_aAccessibleChildren
)
212 if ( xComponent
.is() )
213 xComponent
->dispose();
215 m_aAccessibleChildren
.clear();
218 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent
);
222 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent
);
230 void VCLXAccessibleStatusBar::disposing()
232 VCLXAccessibleComponent::disposing();
237 m_pStatusBar
= nullptr;
239 // dispose all children
240 for (const rtl::Reference
<VCLXAccessibleStatusBarItem
>& xComponent
: m_aAccessibleChildren
)
242 if ( xComponent
.is() )
243 xComponent
->dispose();
245 m_aAccessibleChildren
.clear();
252 OUString
VCLXAccessibleStatusBar::getImplementationName()
254 return u
"com.sun.star.comp.toolkit.AccessibleStatusBar"_ustr
;
258 Sequence
< OUString
> VCLXAccessibleStatusBar::getSupportedServiceNames()
260 return { u
"com.sun.star.awt.AccessibleStatusBar"_ustr
};
264 // XAccessibleContext
267 sal_Int64
VCLXAccessibleStatusBar::getAccessibleChildCount()
269 OExternalLockGuard
aGuard( this );
271 return m_aAccessibleChildren
.size();
275 Reference
< XAccessible
> VCLXAccessibleStatusBar::getAccessibleChild( sal_Int64 i
)
277 OExternalLockGuard
aGuard( this );
279 if ( i
< 0 || o3tl::make_unsigned(i
) >= m_aAccessibleChildren
.size() )
280 throw IndexOutOfBoundsException();
282 rtl::Reference
< VCLXAccessibleStatusBarItem
> xChild
= m_aAccessibleChildren
[i
];
287 sal_uInt16 nItemId
= m_pStatusBar
->GetItemId( static_cast<sal_uInt16
>(i
) );
289 xChild
= new VCLXAccessibleStatusBarItem( m_pStatusBar
, nItemId
);
291 // insert into status bar item list
292 m_aAccessibleChildren
[i
] = xChild
;
300 // XAccessibleComponent
303 Reference
< XAccessible
> VCLXAccessibleStatusBar::getAccessibleAtPoint( const awt::Point
& rPoint
)
305 OExternalLockGuard
aGuard( this );
307 Reference
< XAccessible
> xChild
;
310 sal_uInt16 nItemId
= m_pStatusBar
->GetItemId(vcl::unohelper::ConvertToVCLPoint(rPoint
));
311 sal_Int32 nItemPos
= m_pStatusBar
->GetItemPos( nItemId
);
312 if ( nItemPos
>= 0 && o3tl::make_unsigned(nItemPos
) < m_aAccessibleChildren
.size() )
313 xChild
= getAccessibleChild( nItemPos
);
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */