cid#1636690 Dereference after null check
[LibreOffice.git] / svx / source / accessibility / svxpixelctlaccessiblecontext.cxx
blob38a643a2b1f438f7fad42ce64a22138b52b521f1
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 <com/sun/star/accessibility/AccessibleRole.hpp>
21 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
22 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
23 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24 #include <toolkit/helper/vclunohelper.hxx>
25 #include <utility>
26 #include <vcl/svapp.hxx>
27 #include <vcl/settings.hxx>
28 #include <vcl/unohelp.hxx>
29 #include <osl/mutex.hxx>
30 #include <tools/debug.hxx>
31 #include <tools/gen.hxx>
33 #include <svx/dlgctrl.hxx>
35 #include <svxpixelctlaccessiblecontext.hxx>
37 using namespace ::cppu;
38 using namespace ::osl;
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::accessibility;
43 SvxPixelCtlAccessible::SvxPixelCtlAccessible(SvxPixelCtl* pControl)
44 : mpPixelCtl(pControl)
48 SvxPixelCtlAccessible::~SvxPixelCtlAccessible()
50 ensureDisposed();
53 uno::Reference< XAccessibleContext > SvxPixelCtlAccessible::getAccessibleContext( )
55 return this;
58 sal_Int64 SvxPixelCtlAccessible::getAccessibleChildCount( )
60 return SvxPixelCtl::GetSquares();
62 uno::Reference< XAccessible > SvxPixelCtlAccessible::getAccessibleChild( sal_Int64 i )
64 ::osl::MutexGuard aGuard( m_aMutex );
65 if ( i < 0 || i >= getAccessibleChildCount())
66 throw lang::IndexOutOfBoundsException();
67 Reference< XAccessible > xChild;
68 if (mpPixelCtl)
69 xChild = CreateChild(i, mpPixelCtl->IndexToPoint(i));
70 return xChild;
73 uno::Reference< XAccessible > SvxPixelCtlAccessible::getAccessibleParent( )
75 ::osl::MutexGuard aGuard( m_aMutex );
76 if (mpPixelCtl)
77 return mpPixelCtl->getAccessibleParent();
78 return uno::Reference<css::accessibility::XAccessible>();
81 sal_Int16 SvxPixelCtlAccessible::getAccessibleRole( )
83 return AccessibleRole::LIST;
86 OUString SvxPixelCtlAccessible::getAccessibleDescription( )
89 ::osl::MutexGuard aGuard( m_aMutex );
90 return mpPixelCtl ? mpPixelCtl->GetAccessibleDescription() : u""_ustr;
93 OUString SvxPixelCtlAccessible::getAccessibleName( )
95 ::osl::MutexGuard aGuard( m_aMutex );
96 return mpPixelCtl ? mpPixelCtl->GetAccessibleName() : u""_ustr;
99 Reference< XAccessibleRelationSet > SAL_CALL SvxPixelCtlAccessible::getAccessibleRelationSet()
101 if (mpPixelCtl)
102 return mpPixelCtl->get_accessible_relation_set();
103 return uno::Reference<css::accessibility::XAccessibleRelationSet>();
106 sal_Int64 SvxPixelCtlAccessible::getAccessibleStateSet( )
108 ::osl::MutexGuard aGuard( m_aMutex );
109 sal_Int64 nStateSet = 0;
111 if (mpPixelCtl)
113 nStateSet |=
114 AccessibleStateType::FOCUSABLE |
115 AccessibleStateType::SELECTABLE |
116 AccessibleStateType::SHOWING |
117 AccessibleStateType::VISIBLE |
118 AccessibleStateType::OPAQUE;
119 if (mpPixelCtl->IsEnabled())
120 nStateSet |= AccessibleStateType::ENABLED;
121 if (mpPixelCtl->HasFocus())
122 nStateSet |= AccessibleStateType::FOCUSED;
123 nStateSet |= AccessibleStateType::MANAGES_DESCENDANTS;
126 return nStateSet;
129 uno::Reference<XAccessible > SAL_CALL SvxPixelCtlAccessible::getAccessibleAtPoint (
130 const awt::Point& rPoint)
132 ::osl::MutexGuard aGuard( m_aMutex );
134 Reference< XAccessible > xRet;
136 if (mpPixelCtl)
138 tools::Long nIndex = mpPixelCtl->PointToIndex(Point(rPoint.X, rPoint.Y));
139 xRet = CreateChild(nIndex, mpPixelCtl->IndexToPoint(nIndex));
142 return xRet;
145 awt::Rectangle SvxPixelCtlAccessible::implGetBounds()
147 ::osl::MutexGuard aGuard( m_aMutex );
149 awt::Rectangle aRet;
151 if (mpPixelCtl)
153 const Point aOutPos;
154 Size aOutSize(mpPixelCtl->GetOutputSizePixel());
156 aRet.X = aOutPos.X();
157 aRet.Y = aOutPos.Y();
158 aRet.Width = aOutSize.Width();
159 aRet.Height = aOutSize.Height();
162 return aRet;
165 void SvxPixelCtlAccessible::grabFocus( )
167 ::osl::MutexGuard aGuard( m_aMutex );
168 if (mpPixelCtl)
169 mpPixelCtl->GrabFocus();
172 sal_Int32 SvxPixelCtlAccessible::getForeground( )
174 ::osl::MutexGuard aGuard( m_aMutex );
176 //see SvxPixelCtl::Paint
177 const StyleSettings& rStyles = Application::GetSettings().GetStyleSettings();
178 return sal_Int32(rStyles.GetLabelTextColor());
181 sal_Int32 SvxPixelCtlAccessible::getBackground( )
183 ::osl::MutexGuard aGuard( m_aMutex );
185 //see SvxPixelCtl::Paint
186 const StyleSettings& rStyles = Application::GetSettings().GetStyleSettings();
187 return sal_Int32(rStyles.GetDialogColor());
190 void SvxPixelCtlAccessible::implSelect(sal_Int64 nChildIndex, bool bSelect)
192 ::osl::MutexGuard aGuard( m_aMutex );
194 if ( nChildIndex < 0 || nChildIndex >= getAccessibleChildCount())
195 throw lang::IndexOutOfBoundsException();
197 if (!mpPixelCtl)
198 return;
200 tools::Long nIndex = mpPixelCtl->ShowPosition(mpPixelCtl->IndexToPoint(nChildIndex));
201 NotifyChild(nIndex, bSelect, false);
204 bool SvxPixelCtlAccessible::implIsSelected(sal_Int64 nChildIndex)
206 ::osl::MutexGuard aGuard( m_aMutex );
208 if (!mpPixelCtl)
209 return false;
211 return mpPixelCtl->GetFocusPosIndex() == nChildIndex;
214 void SAL_CALL SvxPixelCtlAccessible::disposing()
216 ::osl::MutexGuard aGuard(m_aMutex);
217 OAccessibleSelectionHelper::disposing();
218 m_xCurChild.clear();
219 mpPixelCtl = nullptr;
222 void SvxPixelCtlAccessible::NotifyChild(tools::Long nIndex,bool bSelect ,bool bCheck)
224 DBG_ASSERT( !(!bSelect && !bCheck),"" );//non is false
226 rtl::Reference<SvxPixelCtlAccessibleChild> pChild = m_xCurChild;
227 if (pChild && pChild->getAccessibleIndexInParent() == nIndex )
229 if (bSelect)
231 pChild->SelectChild(true);
233 if (bCheck)
235 pChild->ChangePixelColorOrBG(mpPixelCtl->GetBitmapPixel(sal_uInt16(nIndex)) != 0);
236 pChild->CheckChild();
238 return;
240 rtl::Reference<SvxPixelCtlAccessibleChild> xNewChild = CreateChild(nIndex, mpPixelCtl->IndexToPoint(nIndex));
241 DBG_ASSERT(xNewChild,"Child Must be Valid");
243 Any aNewValue,aOldValue;
244 aNewValue <<= uno::Reference<XAccessible>(xNewChild);
245 NotifyAccessibleEvent(AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldValue, aNewValue);
247 if (bSelect)
249 if (pChild)
251 pChild->SelectChild(false);
253 xNewChild->SelectChild(true);
255 if (bCheck)
257 xNewChild->CheckChild();
259 m_xCurChild = std::move(xNewChild);
262 rtl::Reference<SvxPixelCtlAccessibleChild> SvxPixelCtlAccessible::CreateChild (tools::Long nIndex,Point mPoint)
264 bool bPixelColorOrBG = mpPixelCtl->GetBitmapPixel(sal_uInt16(nIndex)) != 0;
265 Size size(mpPixelCtl->GetWidth() / SvxPixelCtl::GetLineCount(), mpPixelCtl->GetHeight() / SvxPixelCtl::GetLineCount());
266 rtl::Reference<SvxPixelCtlAccessibleChild> xChild = new SvxPixelCtlAccessibleChild(*mpPixelCtl,
267 bPixelColorOrBG,
268 tools::Rectangle(mPoint,size),
269 this,
270 nIndex);
272 return xChild;
275 void SvxPixelCtlAccessibleChild::CheckChild()
277 Any aChecked;
278 aChecked <<= AccessibleStateType::CHECKED;
280 if (m_bPixelColorOrBG)//Current Child State
282 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED, Any(), aChecked);
284 else
286 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED, aChecked, Any());
290 void SvxPixelCtlAccessibleChild::SelectChild( bool bSelect)
292 Any aSelected;
293 aSelected <<= AccessibleStateType::SELECTED;
295 if (bSelect)
297 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED, Any(), aSelected);
299 else
301 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED, aSelected, Any());
305 SvxPixelCtlAccessibleChild::SvxPixelCtlAccessibleChild( SvxPixelCtl& rWindow, bool bPixelColorOrBG,
306 const tools::Rectangle& rBoundingBox, rtl::Reference<SvxPixelCtlAccessible> xParent,
307 tools::Long nIndexInParent)
308 : mrParentWindow( rWindow )
309 , mxParent(std::move(xParent))
310 , m_bPixelColorOrBG(bPixelColorOrBG)
311 , maBoundingBox( rBoundingBox )
312 , mnIndexInParent( nIndexInParent )
316 SvxPixelCtlAccessibleChild::~SvxPixelCtlAccessibleChild()
318 ensureDisposed();
321 // XAccessible
322 uno::Reference< XAccessibleContext> SAL_CALL SvxPixelCtlAccessibleChild::getAccessibleContext()
324 return this;
327 uno::Reference< XAccessible > SAL_CALL SvxPixelCtlAccessibleChild::getAccessibleAtPoint( const awt::Point& )
329 return uno::Reference< XAccessible >();
332 void SAL_CALL SvxPixelCtlAccessibleChild::grabFocus()
336 sal_Int32 SvxPixelCtlAccessibleChild::getForeground()
338 ::osl::MutexGuard aGuard( m_aMutex );
339 return mxParent.is() ? mxParent->getForeground() : -1;
342 sal_Int32 SvxPixelCtlAccessibleChild::getBackground()
344 ::osl::MutexGuard aGuard( m_aMutex );
345 return mxParent.is() ? mxParent->getBackground() : -1;
348 // XAccessibleContext
349 sal_Int64 SAL_CALL SvxPixelCtlAccessibleChild::getAccessibleChildCount()
351 return 0;
354 uno::Reference< XAccessible > SAL_CALL SvxPixelCtlAccessibleChild::getAccessibleChild( sal_Int64 )
356 throw lang::IndexOutOfBoundsException();
359 uno::Reference< XAccessible > SAL_CALL SvxPixelCtlAccessibleChild::getAccessibleParent()
361 return mxParent;
364 sal_Int16 SAL_CALL SvxPixelCtlAccessibleChild::getAccessibleRole()
366 return AccessibleRole::CHECK_BOX;
369 OUString SAL_CALL SvxPixelCtlAccessibleChild::getAccessibleDescription()
371 ::osl::MutexGuard aGuard( m_aMutex );
373 return GetName();
376 OUString SAL_CALL SvxPixelCtlAccessibleChild::getAccessibleName()
378 ::osl::MutexGuard aGuard( m_aMutex );
379 return GetName();
382 /** Return empty uno::Reference to indicate that the relation set is not
383 supported.
385 uno::Reference<XAccessibleRelationSet> SAL_CALL SvxPixelCtlAccessibleChild::getAccessibleRelationSet()
387 return uno::Reference< XAccessibleRelationSet >();
390 sal_Int64 SAL_CALL SvxPixelCtlAccessibleChild::getAccessibleStateSet()
392 ::osl::MutexGuard aGuard( m_aMutex );
393 sal_Int64 nStateSet = 0;
395 if (!rBHelper.bDisposed)
397 nStateSet |= AccessibleStateType::TRANSIENT;
398 nStateSet |= AccessibleStateType::ENABLED;
399 nStateSet |= AccessibleStateType::OPAQUE;
400 nStateSet |= AccessibleStateType::SELECTABLE;
401 nStateSet |= AccessibleStateType::SHOWING;
402 nStateSet |= AccessibleStateType::VISIBLE;
404 tools::Long nIndex = mrParentWindow.GetFocusPosIndex();
405 if ( nIndex == mnIndexInParent)
407 nStateSet |= AccessibleStateType::SELECTED;
409 if (mrParentWindow.GetBitmapPixel(sal_uInt16(mnIndexInParent)))
411 nStateSet |= AccessibleStateType::CHECKED;
414 else
415 nStateSet |= AccessibleStateType::DEFUNC;
417 return nStateSet;
420 void SAL_CALL SvxPixelCtlAccessibleChild::disposing()
422 OAccessibleComponentHelper::disposing();
423 mxParent.clear();
426 awt::Rectangle SvxPixelCtlAccessibleChild::implGetBounds()
428 // no guard necessary, because no one changes maBoundingBox after creating it
429 return vcl::unohelper::ConvertToAWTRect(maBoundingBox);
432 OUString SvxPixelCtlAccessibleChild::GetName() const
434 sal_Int32 nXIndex = mnIndexInParent % SvxPixelCtl::GetLineCount();
435 sal_Int32 nYIndex = mnIndexInParent / SvxPixelCtl::GetLineCount();
437 OUString str = "("
438 + OUString::number(nXIndex)
439 + ","
440 + OUString::number(nYIndex)
441 + ")";
442 return str;
445 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */