LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / accessibility / source / extended / AccessibleGridControl.cxx
blobc90cc0384b465a563d8744930c139304d5102c46
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 <extended/AccessibleGridControl.hxx>
21 #include <extended/AccessibleGridControlTable.hxx>
22 #include <extended/AccessibleGridControlHeader.hxx>
23 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
24 #include <com/sun/star/accessibility/AccessibleRole.hpp>
25 #include <com/sun/star/accessibility/AccessibleTableModelChange.hpp>
26 #include <com/sun/star/accessibility/AccessibleTableModelChangeType.hpp>
27 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
28 #include <toolkit/helper/convert.hxx>
29 #include <vcl/accessibletable.hxx>
30 #include <vcl/svapp.hxx>
32 namespace accessibility
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::accessibility;
39 using namespace ::vcl;
40 using namespace ::vcl::table;
42 AccessibleGridControl::AccessibleGridControl(
43 const css::uno::Reference< css::accessibility::XAccessible >& _rxParent, const css::uno::Reference< css::accessibility::XAccessible >& _rxCreator,
44 ::vcl::table::IAccessibleTable& _rTable )
45 : AccessibleGridControlBase( _rxParent, _rTable, TCTYPE_GRIDCONTROL ),
46 m_aCreator(_rxCreator)
51 void SAL_CALL AccessibleGridControl::disposing()
53 SolarMutexGuard g;
55 m_aCreator.clear();
57 if ( m_xTable.is() )
59 m_xTable->dispose();
60 m_xTable.clear();
62 if ( m_xCell.is() )
64 m_xCell->dispose();
65 m_xCell.clear();
67 if ( m_xRowHeaderBar.is() )
69 m_xRowHeaderBar->dispose();
70 m_xRowHeaderBar.clear();
72 if ( m_xColumnHeaderBar.is() )
74 m_xColumnHeaderBar->dispose();
75 m_xColumnHeaderBar.clear();
77 AccessibleGridControlBase::disposing();
80 sal_Int32 AccessibleGridControl::implGetAccessibleChildCount()
82 return m_aTable.GetAccessibleControlCount();
85 // css::accessibility::XAccessibleContext ---------------------------------------------------------
88 sal_Int32 SAL_CALL AccessibleGridControl::getAccessibleChildCount()
90 SolarMutexGuard aSolarGuard;
91 ensureIsAlive();
92 return implGetAccessibleChildCount();
96 css::uno::Reference< css::accessibility::XAccessible > SAL_CALL
97 AccessibleGridControl::getAccessibleChild( sal_Int32 nChildIndex )
99 SolarMutexGuard aSolarGuard;
101 if (nChildIndex<0 || nChildIndex>=implGetAccessibleChildCount())
102 throw IndexOutOfBoundsException();
104 css::uno::Reference< css::accessibility::XAccessible > xChild;
105 if (isAlive())
107 if(nChildIndex == 0 && m_aTable.HasColHeader())
109 if(!m_xColumnHeaderBar.is())
111 m_xColumnHeaderBar = new AccessibleGridControlHeader(m_aCreator, m_aTable, vcl::table::TCTYPE_COLUMNHEADERBAR);
113 xChild = m_xColumnHeaderBar.get();
115 else if(m_aTable.HasRowHeader() && (nChildIndex == 1 || nChildIndex == 0))
117 if(!m_xRowHeaderBar.is())
119 m_xRowHeaderBar = new AccessibleGridControlHeader(m_aCreator, m_aTable, vcl::table::TCTYPE_ROWHEADERBAR);
121 xChild = m_xRowHeaderBar.get();
123 else
125 if(!m_xTable.is())
127 m_xTable = new AccessibleGridControlTable(m_aCreator, m_aTable);
129 xChild = m_xTable.get();
132 return xChild;
136 sal_Int16 SAL_CALL AccessibleGridControl::getAccessibleRole()
138 SolarMutexGuard g;
140 ensureIsAlive();
141 return AccessibleRole::PANEL;
145 // css::accessibility::XAccessibleComponent -------------------------------------------------------
147 css::uno::Reference< css::accessibility::XAccessible > SAL_CALL
148 AccessibleGridControl::getAccessibleAtPoint( const awt::Point& rPoint )
150 SolarMutexGuard aSolarGuard;
151 ensureIsAlive();
153 css::uno::Reference< css::accessibility::XAccessible > xChild;
154 sal_Int32 nIndex = 0;
155 if( m_aTable.ConvertPointToControlIndex( nIndex, VCLPoint( rPoint ) ) )
156 xChild = m_aTable.CreateAccessibleControl( nIndex );
157 else
159 // try whether point is in one of the fixed children
160 // (table, header bars, corner control)
161 Point aPoint( VCLPoint( rPoint ) );
162 for( nIndex = 0; (nIndex < 3) && !xChild.is(); ++nIndex )
164 css::uno::Reference< css::accessibility::XAccessible > xCurrChild( implGetFixedChild( nIndex ) );
165 css::uno::Reference< css::accessibility::XAccessibleComponent >
166 xCurrChildComp( xCurrChild, uno::UNO_QUERY );
168 if( xCurrChildComp.is() &&
169 VCLRectangle( xCurrChildComp->getBounds() ).Contains( aPoint ) )
170 xChild = xCurrChild;
173 return xChild;
177 void SAL_CALL AccessibleGridControl::grabFocus()
179 SolarMutexGuard aSolarGuard;
180 ensureIsAlive();
181 m_aTable.GrabFocus();
184 // XServiceInfo ---------------------------------------------------------------
186 OUString SAL_CALL AccessibleGridControl::getImplementationName()
188 return "com.sun.star.accessibility.AccessibleGridControl";
192 // internal virtual methods ---------------------------------------------------
194 tools::Rectangle AccessibleGridControl::implGetBoundingBox()
196 vcl::Window* pParent = m_aTable.GetAccessibleParentWindow();
197 OSL_ENSURE( pParent, "implGetBoundingBox - missing parent window" );
198 return m_aTable.GetWindowExtentsRelative( pParent );
202 tools::Rectangle AccessibleGridControl::implGetBoundingBoxOnScreen()
204 return m_aTable.GetWindowExtentsRelative( nullptr );
206 // internal helper methods ----------------------------------------------------
208 css::uno::Reference< css::accessibility::XAccessible > AccessibleGridControl::implGetTable()
210 if( !m_xTable.is() )
212 m_xTable = createAccessibleTable();
214 return m_xTable;
218 css::uno::Reference< css::accessibility::XAccessible >
219 AccessibleGridControl::implGetHeaderBar( AccessibleTableControlObjType eObjType )
221 css::uno::Reference< css::accessibility::XAccessible > xRet;
222 rtl::Reference< AccessibleGridControlHeader >* pxMember = nullptr;
224 if( eObjType == TCTYPE_ROWHEADERBAR )
225 pxMember = &m_xRowHeaderBar;
226 else if( eObjType == TCTYPE_COLUMNHEADERBAR )
227 pxMember = &m_xColumnHeaderBar;
229 if( pxMember )
231 if( !pxMember->is() )
233 *pxMember = new AccessibleGridControlHeader(
234 m_aCreator, m_aTable, eObjType );
236 xRet = pxMember->get();
238 return xRet;
241 css::uno::Reference< css::accessibility::XAccessible >
242 AccessibleGridControl::implGetFixedChild( sal_Int32 nChildIndex )
244 css::uno::Reference< css::accessibility::XAccessible > xRet;
245 switch( nChildIndex )
247 /** Child index of the column header bar (first row). */
248 case 0:
249 xRet = implGetHeaderBar( TCTYPE_COLUMNHEADERBAR );
250 break;
251 /** Child index of the row header bar ("handle column"). */
252 case 1:
253 xRet = implGetHeaderBar( TCTYPE_ROWHEADERBAR );
254 break;
255 /** Child index of the data table. */
256 case 2:
257 xRet = implGetTable();
258 break;
260 return xRet;
263 rtl::Reference<AccessibleGridControlTable> AccessibleGridControl::createAccessibleTable()
265 css::uno::Reference< css::accessibility::XAccessible > xCreator(m_aCreator);
266 OSL_ENSURE( xCreator.is(), "extended/AccessibleGridControl::createAccessibleTable: my creator died - how this?" );
267 return new AccessibleGridControlTable( xCreator, m_aTable );
270 void AccessibleGridControl::commitCellEvent(sal_Int16 _nEventId,const Any& _rNewValue,const Any& _rOldValue)
272 sal_Int32 nChildCount = implGetAccessibleChildCount();
273 if(nChildCount != 0)
275 for(sal_Int32 i=0;i<nChildCount;i++)
277 css::uno::Reference< css::accessibility::XAccessible > xAccessible = getAccessibleChild(i);
278 if(css::uno::Reference< css::accessibility::XAccessible >(m_xTable) == xAccessible)
280 std::vector< rtl::Reference<AccessibleGridControlTableCell> >& rCells =
281 m_xTable->getCellVector();
282 size_t nIndex = m_aTable.GetCurrentRow() * m_aTable.GetColumnCount()
283 + m_aTable.GetCurrentColumn();
284 if (nIndex < rCells.size() && rCells[nIndex])
286 m_xCell = rCells[nIndex];
287 m_xCell->commitEvent( _nEventId, _rNewValue, _rOldValue );
292 else
294 if ( m_xTable.is() )
295 m_xTable->commitEvent(_nEventId,_rNewValue,_rOldValue);
299 void AccessibleGridControl::commitTableEvent(sal_Int16 _nEventId,const Any& _rNewValue,const Any& _rOldValue)
301 if ( !m_xTable.is() )
302 return;
304 if(_nEventId == AccessibleEventId::ACTIVE_DESCENDANT_CHANGED)
306 const sal_Int32 nCurrentRow = m_aTable.GetCurrentRow();
307 const sal_Int32 nCurrentCol = m_aTable.GetCurrentColumn();
308 css::uno::Reference< css::accessibility::XAccessible > xChild;
309 if (nCurrentRow > -1 && nCurrentCol > -1)
311 sal_Int32 nColumnCount = m_aTable.GetColumnCount();
312 xChild = m_xTable->getAccessibleChild(nCurrentRow * nColumnCount + nCurrentCol);
314 m_xTable->commitEvent(_nEventId, Any(xChild),_rOldValue);
316 else if(_nEventId == AccessibleEventId::TABLE_MODEL_CHANGED)
318 AccessibleTableModelChange aChange;
319 if(_rNewValue >>= aChange)
321 if(aChange.Type == AccessibleTableModelChangeType::DELETE)
323 std::vector< rtl::Reference<AccessibleGridControlTableCell> >& rCells =
324 m_xTable->getCellVector();
325 int nColCount = m_aTable.GetColumnCount();
326 // check valid index - entries are inserted lazily
327 size_t const nStart = nColCount * aChange.FirstRow;
328 size_t const nEnd = nColCount * aChange.LastRow;
329 if (nStart < rCells.size())
331 m_xTable->getCellVector().erase(
332 rCells.begin() + nStart,
333 rCells.begin() + std::min(rCells.size(), nEnd));
335 m_xTable->commitEvent(_nEventId,_rNewValue,_rOldValue);
337 else
338 m_xTable->commitEvent(_nEventId,_rNewValue,_rOldValue);
341 else
342 m_xTable->commitEvent(_nEventId,_rNewValue,_rOldValue);
345 // = AccessibleGridControlAccess
348 AccessibleGridControlAccess::AccessibleGridControlAccess(
349 const css::uno::Reference< css::accessibility::XAccessible >& rxParent, ::vcl::table::IAccessibleTable& rTable )
350 : m_xParent( rxParent )
351 , m_pTable( & rTable )
356 AccessibleGridControlAccess::~AccessibleGridControlAccess()
361 void AccessibleGridControlAccess::DisposeAccessImpl()
363 SolarMutexGuard g;
365 m_pTable = nullptr;
366 if (m_xContext.is())
368 m_xContext->dispose();
369 m_xContext.clear();
374 css::uno::Reference< css::accessibility::XAccessibleContext > SAL_CALL AccessibleGridControlAccess::getAccessibleContext()
376 SolarMutexGuard g;
378 // if the context died meanwhile (we're no listener, so it won't tell us explicitly when this happens),
379 // then reset and re-create.
380 if ( m_xContext.is() && !m_xContext->isAlive() )
381 m_xContext = nullptr;
383 if (!m_xContext.is() && m_pTable)
384 m_xContext = new AccessibleGridControl(m_xParent, this, *m_pTable);
386 return m_xContext;
390 } // namespace accessibility
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */