Bump version to 21.06.18.1
[LibreOffice.git] / starmath / source / AccessibleSmElementsControl.cxx
blob29701ce81ad94074ebaa3f184e56c1e552e04320
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <AccessibleSmElementsControl.hxx>
21 #include <AccessibleSmElement.hxx>
22 #include <ElementsDockingWindow.hxx>
23 #include <smmod.hxx>
25 #include <comphelper/accessiblewrapper.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <comphelper/types.hxx>
28 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
29 #include <com/sun/star/accessibility/AccessibleRole.hpp>
30 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
31 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <cppuhelper/typeprovider.hxx>
34 #include <o3tl/safeint.hxx>
35 #include <toolkit/helper/convert.hxx>
36 #include <unotools/accessiblerelationsethelper.hxx>
37 #include <unotools/accessiblestatesethelper.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vcl/vclevent.hxx>
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::accessibility;
43 using OContextEntryGuard = ::comphelper::OContextEntryGuard;
44 using OExternalLockGuard = ::comphelper::OExternalLockGuard;
46 // AccessibleSmElementsControl
48 AccessibleSmElementsControl::AccessibleSmElementsControl(SmElementsControl& rControl)
49 : m_pControl(&rControl)
53 AccessibleSmElementsControl::~AccessibleSmElementsControl() {}
55 void AccessibleSmElementsControl::UpdateFocus(sal_uInt16 nPos)
57 const bool bSetFocus = (nPos == SAL_MAX_UINT16);
59 // submit events only if the widget has the focus to avoid sending events due to mouse move
60 if (!m_pControl || (bSetFocus && !m_pControl->HasFocus()))
61 return;
63 if (bSetFocus)
64 nPos = m_pControl->itemHighlighted() - m_pControl->itemOffset();
66 if (nPos < m_aAccessibleChildren.size())
68 const auto& rxChild = m_aAccessibleChildren[nPos];
69 if (rxChild.is())
70 rxChild->SetFocus(bSetFocus);
74 void AccessibleSmElementsControl::ReleaseAllItems()
76 if (m_aAccessibleChildren.empty())
77 return;
79 m_aAccessibleChildren.clear();
81 // The original toolbox accessibility code uses individual NAME_CHANGED
82 // events in a loop. We can't do this, because on each remove event the
83 // list of known children is rebuild. But since we rely on the child
84 // count of the SmElementsControl, we'll always have no or all items.
85 // In the latter case this would automatically recreate all items!
86 assert(m_pControl && m_pControl->itemCount() == 0);
87 NotifyAccessibleEvent(AccessibleEventId::INVALIDATE_ALL_CHILDREN, uno::Any(), uno::Any());
90 void AccessibleSmElementsControl::AddAllItems()
92 assert(m_pControl);
93 if (!m_pControl)
94 return;
96 uno::Any aNewName(getAccessibleName());
97 NotifyAccessibleEvent(AccessibleEventId::NAME_CHANGED, uno::Any(), aNewName);
99 // register the new items
100 sal_uInt16 nCount = getAccessibleChildCount();
101 for (sal_uInt16 i = 0; i < nCount; ++i)
103 uno::Any aNewValue;
104 aNewValue <<= getAccessibleChild(static_cast<sal_Int32>(i));
105 NotifyAccessibleEvent(AccessibleEventId::CHILD, uno::Any(), aNewValue);
109 IMPLEMENT_FORWARD_XINTERFACE2(AccessibleSmElementsControl, comphelper::OAccessibleComponentHelper,
110 AccessibleSmElementsControl_BASE)
112 IMPLEMENT_FORWARD_XTYPEPROVIDER2(AccessibleSmElementsControl,
113 comphelper::OAccessibleComponentHelper,
114 AccessibleSmElementsControl_BASE)
116 // XAccessible
117 uno::Reference<XAccessibleContext> AccessibleSmElementsControl::getAccessibleContext()
119 return this;
122 // XComponent
123 void AccessibleSmElementsControl::disposing()
125 comphelper::OAccessibleComponentHelper::disposing();
126 m_aAccessibleChildren.clear();
129 void AccessibleSmElementsControl::grabFocus()
131 SolarMutexGuard aGuard;
132 if (!m_pControl)
133 throw uno::RuntimeException();
135 m_pControl->GrabFocus();
138 sal_Int32 AccessibleSmElementsControl::getForeground()
140 SolarMutexGuard aGuard;
142 return static_cast<sal_Int32>(SmElementsControl::GetTextColor());
145 sal_Int32 AccessibleSmElementsControl::getBackground()
147 SolarMutexGuard aGuard;
149 Color nCol = SmElementsControl::GetControlBackground();
150 return static_cast<sal_Int32>(nCol);
153 // XServiceInfo
154 OUString AccessibleSmElementsControl::getImplementationName()
156 return "com.sun.star.comp.toolkit.AccessibleSmElementsControl";
159 sal_Bool AccessibleSmElementsControl::supportsService(const OUString& rServiceName)
161 return cppu::supportsService(this, rServiceName);
164 uno::Sequence<OUString> AccessibleSmElementsControl::getSupportedServiceNames()
166 return { "com.sun.star.accessibility.AccessibleContext",
167 "com.sun.star.accessibility.AccessibleComponent",
168 "com.sun.star.accessibility.AccessibleSelection",
169 "com.sun.star.accessibility.AccessibleSmElementsControl" };
172 // XAccessibleContext
173 sal_Int32 AccessibleSmElementsControl::getAccessibleChildCount()
175 comphelper::OExternalLockGuard aGuard(this);
176 sal_Int32 nCount = 0;
177 if (m_pControl)
179 nCount = m_pControl->itemCount();
180 if (m_aAccessibleChildren.size() != sal_uInt16(nCount))
181 m_aAccessibleChildren.resize(nCount);
183 return nCount;
186 uno::Reference<XAccessible> AccessibleSmElementsControl::getAccessibleChild(sal_Int32 c)
188 comphelper::OExternalLockGuard aGuard(this);
190 if (c < 0 || c >= getAccessibleChildCount())
191 throw lang::IndexOutOfBoundsException();
193 rtl::Reference<AccessibleSmElement> xChild = m_aAccessibleChildren[c];
194 const sal_uInt16 nItemId = m_pControl->itemOffset() + c;
195 if (xChild.is() && xChild->itemId() != nItemId)
196 xChild.clear();
197 if (!xChild.is())
199 sal_uInt16 nHighlightItemId = m_pControl->itemHighlighted();
200 AccessibleSmElement* pChild = new AccessibleSmElement(m_pControl, nItemId, c);
201 if (pChild->itemId() == nHighlightItemId)
202 pChild->SetFocus(true);
203 m_aAccessibleChildren[c] = pChild;
204 xChild = pChild;
206 return xChild.get();
209 uno::Reference<XAccessible> AccessibleSmElementsControl::getAccessibleParent()
211 SolarMutexGuard aGuard;
212 if (!m_pControl)
213 throw uno::RuntimeException();
215 return m_pControl->GetDrawingArea()->get_accessible_parent();
218 uno::Reference<XAccessible>
219 AccessibleSmElementsControl::getAccessibleAtPoint(const awt::Point& rPoint)
221 comphelper::OExternalLockGuard aGuard(this);
223 uno::Reference<XAccessible> xAccessible;
224 if (m_pControl)
226 sal_uInt16 nPos = m_pControl->itemAtPos(VCLPoint(rPoint));
227 nPos -= m_pControl->itemOffset();
228 if (nPos <= m_aAccessibleChildren.size())
229 xAccessible = getAccessibleChild(nPos);
231 return xAccessible;
234 sal_Int16 AccessibleSmElementsControl::getAccessibleRole() { return AccessibleRole::SCROLL_PANE; }
236 OUString AccessibleSmElementsControl::getAccessibleDescription() { return OUString(); }
238 OUString AccessibleSmElementsControl::getAccessibleName()
240 SolarMutexGuard aGuard;
241 OUString aName;
242 if (m_pControl)
243 aName = SmResId(m_pControl->elementSetId().getStr());
244 return aName;
247 // XAccessibleSelection
248 void AccessibleSmElementsControl::selectAccessibleChild(sal_Int32 nChildIndex)
250 OExternalLockGuard aGuard(this);
252 if ((!m_pControl) || nChildIndex < 0
253 || o3tl::make_unsigned(nChildIndex) >= m_aAccessibleChildren.size())
254 throw lang::IndexOutOfBoundsException();
256 m_pControl->setItemHighlighted(nChildIndex);
259 sal_Bool AccessibleSmElementsControl::isAccessibleChildSelected(sal_Int32 nChildIndex)
261 OExternalLockGuard aGuard(this);
262 if ((!m_pControl) || nChildIndex < 0
263 || o3tl::make_unsigned(nChildIndex) >= m_aAccessibleChildren.size())
264 throw lang::IndexOutOfBoundsException();
266 return (m_pControl->itemHighlighted() == nChildIndex);
269 void AccessibleSmElementsControl::clearAccessibleSelection()
271 OExternalLockGuard aGuard(this);
272 if (m_pControl)
273 m_pControl->setItemHighlighted(SAL_MAX_UINT16);
276 void AccessibleSmElementsControl::selectAllAccessibleChildren()
278 // intentionally empty
281 sal_Int32 AccessibleSmElementsControl::getSelectedAccessibleChildCount()
283 OExternalLockGuard aGuard(this);
285 sal_Int32 nRet = 0;
286 if (m_pControl
287 && (m_pControl->itemHighlighted() - m_pControl->itemOffset()) < getAccessibleChildCount())
288 nRet = 1;
289 return nRet;
292 uno::Reference<XAccessible>
293 AccessibleSmElementsControl::getSelectedAccessibleChild(sal_Int32 nSelectedChildIndex)
295 OExternalLockGuard aGuard(this);
296 if (nSelectedChildIndex != 0 || !m_pControl)
297 throw lang::IndexOutOfBoundsException();
298 return getAccessibleChild(m_pControl->itemHighlighted() - m_pControl->itemOffset());
301 void AccessibleSmElementsControl::deselectAccessibleChild(sal_Int32 nChildIndex)
303 OExternalLockGuard aGuard(this);
304 if (nChildIndex != 0 || nChildIndex >= getAccessibleChildCount())
305 throw lang::IndexOutOfBoundsException();
306 clearAccessibleSelection(); // there can be just one selected child
309 void AccessibleSmElementsControl::TestControl()
311 if (!m_pControl)
312 throw uno::RuntimeException();
315 awt::Rectangle AccessibleSmElementsControl::implGetBounds()
317 SolarMutexGuard aGuard;
318 TestControl();
320 awt::Rectangle aRet;
322 const Point aOutPos;
323 Size aOutSize(m_pControl->GetOutputSizePixel());
325 aRet.X = aOutPos.X();
326 aRet.Y = aOutPos.Y();
327 aRet.Width = aOutSize.Width();
328 aRet.Height = aOutSize.Height();
330 return aRet;
333 sal_Bool AccessibleSmElementsControl::containsPoint(const awt::Point& aPoint)
335 SolarMutexGuard aGuard;
336 TestControl();
337 Size aSz(m_pControl->GetOutputSizePixel());
338 return aPoint.X >= 0 && aPoint.Y >= 0 && aPoint.X < aSz.Width() && aPoint.Y < aSz.Height();
341 uno::Reference<XAccessibleRelationSet> AccessibleSmElementsControl::getAccessibleRelationSet()
343 uno::Reference<XAccessibleRelationSet> xRelSet = new utl::AccessibleRelationSetHelper();
344 return xRelSet; // empty relation set
347 uno::Reference<XAccessibleStateSet> AccessibleSmElementsControl::getAccessibleStateSet()
349 SolarMutexGuard aGuard;
350 ::utl::AccessibleStateSetHelper* pStateSet = new ::utl::AccessibleStateSetHelper;
352 uno::Reference<XAccessibleStateSet> xStateSet(pStateSet);
354 if (!m_pControl)
355 pStateSet->AddState(AccessibleStateType::DEFUNC);
356 else
358 pStateSet->AddState(AccessibleStateType::ENABLED);
359 pStateSet->AddState(AccessibleStateType::FOCUSABLE);
360 if (m_pControl->HasFocus())
361 pStateSet->AddState(AccessibleStateType::FOCUSED);
362 if (m_pControl->IsActive())
363 pStateSet->AddState(AccessibleStateType::ACTIVE);
364 if (m_pControl->IsVisible())
365 pStateSet->AddState(AccessibleStateType::SHOWING);
366 if (m_pControl->IsReallyVisible())
367 pStateSet->AddState(AccessibleStateType::VISIBLE);
368 if (COL_TRANSPARENT != SmElementsControl::GetControlBackground())
369 pStateSet->AddState(AccessibleStateType::OPAQUE);
372 return xStateSet;
375 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */