Files
libreoffice/sw/source/core/access/acccell.cxx

479 lines
14 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
re-base on ALv2 code. Includes: Patches contributed by Oliver-Rainer Wittmann sw34bf06: #i117783# - Writer's implementation of XPagePrintable - apply print settings to new printing routines http://svn.apache.org/viewvc?view=revision&revision=1172115 sw34bf06: #o12311627# use <rtl_random> methods to create unique ids for list styles and list ids http://svn.apache.org/viewvc?view=revision&revision=1172112 sw34bf06 #i114725#,#i115828# - method <SwDoc::ClearDoc()> - clear list structures completely http://svn.apache.org/viewvc?view=revision&revision=1172122 i#118572 - remove ui string and help content regarding usage of Java Mail in Writer's Mail Merge as Java Mail is not used. http://svn.apache.org/viewvc?view=revision&revision=1197035 Patches contributed by Mathias Bauer cws mba34issues01: #i117718#: provide filter name in case storage of medium does not allow to detect one http://svn.apache.org/viewvc?view=revision&revision=1172350 cws mba34issues01: #i117721#: directly provide parameters retrieved from SfxMedium http://svn.apache.org/viewvc?view=revision&revision=1172353 gnumake4 work variously http://svn.apache.org/viewvc?view=revision&revision=1394707 http://svn.apache.org/viewvc?view=revision&revision=1394326 http://svn.apache.org/viewvc?view=revision&revision=1396797 http://svn.apache.org/viewvc?view=revision&revision=1397315 cws mba34issues01: #i117723#: convert assertion into trace http://svn.apache.org/viewvc?view=revision&revision=1172355 cws mba34issues01: #i117699#: keep layout alive until swdoc dies http://svn.apache.org/viewvc?view=revision&revision=1172362 cws mba34issues01: #i117943#: missing color attributes in RTF clipboard http://svn.apache.org/viewvc?view=revision&revision=1172363 Patch contributed by Henning Brinkmann imported patch i#103878 http://svn.apache.org/viewvc?view=revision&revision=1172109 Patches contributed by Michael Stahl sw34bf06: #i117955#: WW8 export: disable storing of section breaks in endnotes http://svn.apache.org/viewvc?view=revision&revision=1172119 Patch contributed by imacat Fixed the Asian language work count. http://svn.apache.org/viewvc?view=revision&revision=1241345 Patch contributed by Pedro Giffuni i#20878 - Add comment with BZ issue for reference. http://svn.apache.org/viewvc?view=revision&revision=1244517 Patch contributed by Andre Fischer Do not add targets for junit tests when junit is disabled. http://svn.apache.org/viewvc?view=revision&revision=1241508 add writerperfect dependency.
2011-03-31 10:05:04 +02:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2002-04-11 12:42:31 +00:00
#include <osl/mutex.hxx>
#include <com/sun/star/accessibility/AccessibleRole.hpp>
#include <com/sun/star/accessibility/AccessibleStateType.hpp>
#include <com/sun/star/accessibility/AccessibleEventId.hpp>
2002-04-11 12:42:31 +00:00
#include <unotools/accessiblestatesethelper.hxx>
#include <comphelper/servicehelper.hxx>
#include <cppuhelper/supportsservice.hxx>
2002-04-11 12:42:31 +00:00
#include <vcl/svapp.hxx>
#include <cellfrm.hxx>
#include <tabfrm.hxx>
#include <swtable.hxx>
#include <crsrsh.hxx>
#include <viscrs.hxx>
#include "accfrmobj.hxx"
#include "accfrmobjslist.hxx"
#include <frmfmt.hxx>
#include <cellatr.hxx>
#include <accmap.hxx>
#include "acccell.hxx"
2002-04-11 12:42:31 +00:00
#include <cfloat>
#include <limits.h>
#include <ndtxt.hxx>
#include <editeng/brushitem.hxx>
#include <swatrset.hxx>
#include <frmatr.hxx>
#include "acctable.hxx"
using namespace ::com::sun::star;
using namespace ::com::sun::star::accessibility;
using namespace sw::access;
2002-04-11 12:42:31 +00:00
const sal_Char sImplementationName[] = "com.sun.star.comp.Writer.SwAccessibleCellView";
2002-04-11 12:42:31 +00:00
bool SwAccessibleCell::IsSelected()
2002-04-11 12:42:31 +00:00
{
bool bRet = false;
2002-04-11 12:42:31 +00:00
assert(GetMap());
const SwViewShell *pVSh = GetMap()->GetShell();
assert(pVSh);
if( dynamic_cast<const SwCursorShell*>( pVSh) != nullptr )
2002-04-11 12:42:31 +00:00
{
const SwCursorShell *pCSh = static_cast< const SwCursorShell * >( pVSh );
2002-04-11 12:42:31 +00:00
if( pCSh->IsTableMode() )
{
const SwCellFrame *pCFrame =
static_cast< const SwCellFrame * >( GetFrame() );
2002-04-11 12:42:31 +00:00
SwTableBox *pBox =
const_cast< SwTableBox *>( pCFrame->GetTabBox() );
SwSelBoxes const& rBoxes(pCSh->GetTableCursor()->GetSelectedBoxes());
bRet = rBoxes.find(pBox) != rBoxes.end();
2002-04-11 12:42:31 +00:00
}
}
return bRet;
}
void SwAccessibleCell::GetStates( ::utl::AccessibleStateSetHelper& rStateSet )
2002-04-11 12:42:31 +00:00
{
SwAccessibleContext::GetStates( rStateSet );
// SELECTABLE
const SwViewShell *pVSh = GetMap()->GetShell();
assert(pVSh);
if( dynamic_cast<const SwCursorShell*>( pVSh) != nullptr )
2002-07-10 15:53:35 +00:00
rStateSet.AddState( AccessibleStateType::SELECTABLE );
//Add resizable state to table cell.
rStateSet.AddState( AccessibleStateType::RESIZABLE );
2002-04-11 12:42:31 +00:00
// SELECTED
if( IsSelected() )
{
rStateSet.AddState( AccessibleStateType::SELECTED );
assert(m_bIsSelected && "bSelected out of sync");
2010-10-15 11:44:38 -05:00
::rtl::Reference < SwAccessibleContext > xThis( this );
2002-04-11 12:42:31 +00:00
GetMap()->SetCursorContext( xThis );
}
}
tdf#58624 sw: fix ~SwAccessibleContext() use-after-free race As seen in JunitTest_toolkit_unoapi_1: Method doAccessibleAction() finished with state OK LOG> doAccessibleAction(): COMPLETED.OK debug:27272:12: -SwAccessibleParagraph mutexwait 0x3fd9f50 debug:27272:9: SwAccessibleContext::Dispose 0x3872620 11SwRootFrame debug:27272:9: SwAccessibleContext::DisposeChildren 0x4047c80 0x386d600 11SwPageFrame debug:27272:9: SwAccessibleContext::DisposeChildren xAcc 0 debug:27272:9: SwAccessibleContext::DisposeChildren 0x4047c80 0x386cef0 11SwBodyFrame debug:27272:9: SwAccessibleContext::DisposeChildren xAcc 0 debug:27272:9: SwAccessibleContext::DisposeChildren 0x4047c80 0x3878fe0 11SwTextFrame debug:27272:9: SwAccessibleContext::DisposeChildren xAcc 0 debug:27272:9: SwAccessibleMap::RemoveContext erase 0x3872620 debug:27272:9: ~SwAccessibleMap: frame entry 0x3878fe0 debug:27272:9: ~SwAccessibleMap: mpFrameMap 0x3eb64a0 debug:27272:9: ~SwAccessibleMap: mpShapeMap 0 soffice.bin: sw/source/core/access/accmap.cxx:1726: virtual SwAccessibleMap::~SwAccessibleMap(): Assertion `(!mpFrameMap || mpFrameMap->empty()) && "Frame map should be empty after disposing the root frame"' The problem here is that thread 12 is blocked on SolarMutex in ~SwAccessibleParagraph(), while thread 9 is in ~SwAccessibleMap(). This means that in SwAccessibleContext::DisposeChildren(), the WeakReference to the SwAccessibleParagraph cannot create a uno::Reference because its reference count is 0, so SwAccessibleContext::Dispose() is not called on it and it remains in the SwAccessibleMap::mpFrameMap. This triggers the assert and later on ~SwAccessibleContext() would access the deleted SwAccessibleMap and crash. To fix this, introduce a weak reference from SwAccessibleContext to SwAccessibleMap; use a std::weak_ptr because that is not derived from OWeakObject. The weak_ptr is only used in the dtor ~SwAccessibleContext(); as long as the ref-count of SwAccessibleContext is > 0 it is guaranteed that the SwAccessibleContext::m_pMap is either null or valid as the recursive Dispose() will work fine. It is possible that additional temporary owning references could delay the destruction of SwAccessibleMap, and the order of destruction of Writer documents is very fragile, so rely on the SolarMutex lock to prevent that; the only shared_ptr that owns SwAccessibleMap while SolarMutex is not locked is the one in SwViewShellImp. (An alternative fix would be to represent the 3 lifecycle stages of SwAccessibleContext by adding a C++-pointer to the SwAccessibleMap::mpFrameMap, so that DisposeChildren() can, if the WeakReference is no longer valid due to ref-count 0, use the pointer and clear SwAccessibleContext::m_pMap - this and the corresponding call to SwAccessibleMap::RemoveContext() from ~SwAccessibleContext() under a mutex that is shared_ptr-owned by SwAccessibleMap and all SwAccessibleContext.) Change-Id: If2b44c79189e3b3d276491a5c57d5404bb2be71a
2017-03-24 13:04:32 +01:00
SwAccessibleCell::SwAccessibleCell(std::shared_ptr<SwAccessibleMap> const& pInitMap,
const SwCellFrame *pCellFrame )
: SwAccessibleContext( pInitMap, AccessibleRole::TABLE_CELL, pCellFrame )
, m_aSelectionHelper( *this )
, m_bIsSelected( false )
2002-04-11 12:42:31 +00:00
{
OUString sBoxName( pCellFrame->GetTabBox()->GetName() );
SetName( sBoxName );
2002-04-11 12:42:31 +00:00
m_bIsSelected = IsSelected();
css::uno::Reference<css::accessibility::XAccessible> xTableReference(
getAccessibleParentImpl());
css::uno::Reference<css::accessibility::XAccessibleContext> xContextTable(
xTableReference, css::uno::UNO_QUERY);
SAL_WARN_IF(
(!xContextTable.is()
|| xContextTable->getAccessibleRole() != AccessibleRole::TABLE),
"sw.a11y", "bad accessible context");
m_pAccTable = static_cast<SwAccessibleTable *>(xTableReference.get());
2002-04-11 12:42:31 +00:00
}
bool SwAccessibleCell::InvalidateMyCursorPos()
2002-04-11 12:42:31 +00:00
{
bool bNew = IsSelected();
bool bOld;
2002-04-11 12:42:31 +00:00
{
osl::MutexGuard aGuard( m_Mutex );
bOld = m_bIsSelected;
m_bIsSelected = bNew;
2002-04-11 12:42:31 +00:00
}
if( bNew )
{
// remember that object as the one that has the caret. This is
// necessary to notify that object if the cursor leaves it.
2010-10-15 11:44:38 -05:00
::rtl::Reference < SwAccessibleContext > xThis( this );
2002-04-11 12:42:31 +00:00
GetMap()->SetCursorContext( xThis );
}
bool bChanged = bOld != bNew;
2002-04-11 12:42:31 +00:00
if( bChanged )
{
2002-04-11 12:42:31 +00:00
FireStateChangedEvent( AccessibleStateType::SELECTED, bNew );
if (m_pAccTable.is())
{
m_pAccTable->AddSelectionCell(this,bNew);
}
}
2002-04-11 12:42:31 +00:00
return bChanged;
}
bool SwAccessibleCell::InvalidateChildrenCursorPos( const SwFrame *pFrame )
2002-04-11 12:42:31 +00:00
{
bool bChanged = false;
2002-04-11 12:42:31 +00:00
const SwAccessibleChildSList aVisList( GetVisArea(), *pFrame, *GetMap() );
SwAccessibleChildSList::const_iterator aIter( aVisList.begin() );
2002-04-11 12:42:31 +00:00
while( aIter != aVisList.end() )
{
const SwAccessibleChild& rLower = *aIter;
const SwFrame *pLower = rLower.GetSwFrame();
2002-04-11 12:42:31 +00:00
if( pLower )
{
if( rLower.IsAccessible( GetMap()->GetShell()->IsPreview() ) )
2002-04-11 12:42:31 +00:00
{
2010-10-15 11:44:38 -05:00
::rtl::Reference< SwAccessibleContext > xAccImpl(
GetMap()->GetContextImpl( pLower, false ) );
2010-10-15 11:44:38 -05:00
if( xAccImpl.is() )
2002-04-11 12:42:31 +00:00
{
assert(xAccImpl->GetFrame()->IsCellFrame());
bChanged = static_cast< SwAccessibleCell *>(
xAccImpl.get() )->InvalidateMyCursorPos();
2002-04-11 12:42:31 +00:00
}
else
bChanged = true; // If the context is not know we
2002-04-11 12:42:31 +00:00
// don't know whether the selection
// changed or not.
}
else
{
// This is a box with sub rows.
bChanged |= InvalidateChildrenCursorPos( pLower );
2002-04-11 12:42:31 +00:00
}
}
++aIter;
}
return bChanged;
}
void SwAccessibleCell::InvalidateCursorPos_()
2002-04-11 12:42:31 +00:00
{
if (IsSelected())
{
const SwAccessibleChild aChild( GetChild( *(GetMap()), 0 ) );
if( aChild.IsValid() && aChild.GetSwFrame() )
{
::rtl::Reference < SwAccessibleContext > xChildImpl( GetMap()->GetContextImpl( aChild.GetSwFrame()) );
if (xChildImpl.is())
{
AccessibleEventObject aEvent;
aEvent.EventId = AccessibleEventId::STATE_CHANGED;
aEvent.NewValue <<= AccessibleStateType::FOCUSED;
xChildImpl->FireAccessibleEvent( aEvent );
}
}
}
2002-04-11 12:42:31 +00:00
const SwFrame *pParent = GetParent( SwAccessibleChild(GetFrame()), IsInPagePreview() );
assert(pParent->IsTabFrame());
const SwTabFrame *pTabFrame = static_cast< const SwTabFrame * >( pParent );
if( pTabFrame->IsFollow() )
pTabFrame = pTabFrame->FindMaster();
2002-04-11 12:42:31 +00:00
while( pTabFrame )
2002-04-11 12:42:31 +00:00
{
InvalidateChildrenCursorPos( pTabFrame );
pTabFrame = pTabFrame->GetFollow();
2002-04-11 12:42:31 +00:00
}
if (m_pAccTable.is())
{
m_pAccTable->FireSelectionEvent();
}
2002-04-11 12:42:31 +00:00
}
bool SwAccessibleCell::HasCursor()
2002-04-11 12:42:31 +00:00
{
osl::MutexGuard aGuard( m_Mutex );
return m_bIsSelected;
2002-04-11 12:42:31 +00:00
}
SwAccessibleCell::~SwAccessibleCell()
{
}
OUString SAL_CALL SwAccessibleCell::getAccessibleDescription()
2002-04-11 12:42:31 +00:00
{
return GetName();
}
OUString SAL_CALL SwAccessibleCell::getImplementationName()
{
return OUString(sImplementationName);
2002-04-11 12:42:31 +00:00
}
sal_Bool SAL_CALL SwAccessibleCell::supportsService(const OUString& sTestServiceName)
2002-04-11 12:42:31 +00:00
{
return cppu::supportsService(this, sTestServiceName);
2002-04-11 12:42:31 +00:00
}
uno::Sequence< OUString > SAL_CALL SwAccessibleCell::getSupportedServiceNames()
2002-04-11 12:42:31 +00:00
{
uno::Sequence< OUString > aRet(2);
2002-04-11 12:42:31 +00:00
OUString* pArray = aRet.getArray();
pArray[0] = "com.sun.star.table.AccessibleCellView";
pArray[1] = sAccessibleServiceName;
2002-04-11 12:42:31 +00:00
return aRet;
}
2002-04-17 13:07:39 +00:00
void SwAccessibleCell::Dispose(bool bRecursive, bool bCanSkipInvisible)
2002-04-17 13:07:39 +00:00
{
const SwFrame *pParent = GetParent( SwAccessibleChild(GetFrame()), IsInPagePreview() );
2010-10-15 11:44:38 -05:00
::rtl::Reference< SwAccessibleContext > xAccImpl(
GetMap()->GetContextImpl( pParent, false ) );
2010-10-15 11:44:38 -05:00
if( xAccImpl.is() )
xAccImpl->DisposeChild(SwAccessibleChild(GetFrame()), bRecursive, bCanSkipInvisible);
2002-05-03 11:34:00 +00:00
SwAccessibleContext::Dispose( bRecursive );
2002-04-17 13:07:39 +00:00
}
void SwAccessibleCell::InvalidatePosOrSize( const SwRect& rOldBox )
{
const SwFrame *pParent = GetParent( SwAccessibleChild(GetFrame()), IsInPagePreview() );
2010-10-15 11:44:38 -05:00
::rtl::Reference< SwAccessibleContext > xAccImpl(
GetMap()->GetContextImpl( pParent, false ) );
2010-10-15 11:44:38 -05:00
if( xAccImpl.is() )
xAccImpl->InvalidateChildPosOrSize( SwAccessibleChild(GetFrame()), rOldBox );
2002-05-03 11:34:00 +00:00
SwAccessibleContext::InvalidatePosOrSize( rOldBox );
2002-04-17 13:07:39 +00:00
}
// XAccessibleInterface
uno::Any SwAccessibleCell::queryInterface( const uno::Type& rType )
{
if (rType == cppu::UnoType<XAccessibleExtendedAttributes>::get())
{
uno::Any aR;
aR <<= uno::Reference<XAccessibleExtendedAttributes>(this);
return aR;
}
if (rType == cppu::UnoType<XAccessibleSelection>::get())
{
uno::Any aR;
aR <<= uno::Reference<XAccessibleSelection>(this);
return aR;
}
if ( rType == ::cppu::UnoType<XAccessibleValue>::get() )
{
uno::Reference<XAccessibleValue> xValue = this;
uno::Any aRet;
aRet <<= xValue;
return aRet;
}
else
{
return SwAccessibleContext::queryInterface( rType );
}
}
// XTypeProvider
uno::Sequence< uno::Type > SAL_CALL SwAccessibleCell::getTypes()
{
uno::Sequence< uno::Type > aTypes( SwAccessibleContext::getTypes() );
sal_Int32 nIndex = aTypes.getLength();
aTypes.realloc( nIndex + 1 );
uno::Type* pTypes = aTypes.getArray();
pTypes[nIndex] = ::cppu::UnoType<XAccessibleValue>::get();
return aTypes;
}
uno::Sequence< sal_Int8 > SAL_CALL SwAccessibleCell::getImplementationId()
{
return css::uno::Sequence<sal_Int8>();
}
// XAccessibleValue
SwFrameFormat* SwAccessibleCell::GetTableBoxFormat() const
{
assert(GetFrame());
assert(GetFrame()->IsCellFrame());
const SwCellFrame* pCellFrame = static_cast<const SwCellFrame*>( GetFrame() );
return pCellFrame->GetTabBox()->GetFrameFormat();
}
//Implement TableCell currentValue
uno::Any SwAccessibleCell::getCurrentValue( )
{
SolarMutexGuard aGuard;
ThrowIfDisposed();
return uno::Any( GetTableBoxFormat()->GetTableBoxValue().GetValue() );
}
sal_Bool SwAccessibleCell::setCurrentValue( const uno::Any& aNumber )
{
SolarMutexGuard aGuard;
ThrowIfDisposed();
double fValue = 0;
bool bValid = (aNumber >>= fValue);
if( bValid )
{
SwTableBoxValue aValue( fValue );
GetTableBoxFormat()->SetFormatAttr( aValue );
}
return bValid;
}
uno::Any SwAccessibleCell::getMaximumValue( )
{
return uno::Any(DBL_MAX);
}
uno::Any SwAccessibleCell::getMinimumValue( )
{
return uno::Any(-DBL_MAX);
}
static OUString ReplaceOneChar(const OUString& oldOUString, const OUString& replacedChar, const OUString& replaceStr)
{
int iReplace = oldOUString.lastIndexOf(replacedChar);
OUString aRet = oldOUString;
while(iReplace > -1)
{
aRet = aRet.replaceAt(iReplace,1, replaceStr);
iReplace = aRet.lastIndexOf(replacedChar,iReplace);
}
return aRet;
}
static OUString ReplaceFourChar(const OUString& oldOUString)
{
OUString aRet = ReplaceOneChar(oldOUString,"\\","\\\\");
aRet = ReplaceOneChar(aRet,";","\\;");
aRet = ReplaceOneChar(aRet,"=","\\=");
aRet = ReplaceOneChar(aRet,",","\\,");
aRet = ReplaceOneChar(aRet,":","\\:");
return aRet;
}
css::uno::Any SAL_CALL SwAccessibleCell::getExtendedAttributes()
{
SolarMutexGuard g;
css::uno::Any strRet;
SwFrameFormat *pFrameFormat = GetTableBoxFormat();
assert(pFrameFormat);
const SwTableBoxFormula& tbl_formula = pFrameFormat->GetTableBoxFormula();
OUString strFormula = ReplaceFourChar(tbl_formula.GetFormula());
OUString strFor("Formula:");
strFor += strFormula;
strFor += ";" ;
strRet <<= strFor;
return strRet;
}
sal_Int32 SAL_CALL SwAccessibleCell::getBackground()
{
SolarMutexGuard g;
const SvxBrushItem &rBack = GetFrame()->GetAttrSet()->GetBackground();
sal_uInt32 crBack = rBack.GetColor().GetColor();
if (COL_AUTO == crBack)
{
uno::Reference<XAccessible> xAccDoc = getAccessibleParent();
if (xAccDoc.is())
{
uno::Reference<XAccessibleComponent> xCompoentDoc(xAccDoc, uno::UNO_QUERY);
if (xCompoentDoc.is())
{
crBack = (sal_uInt32)xCompoentDoc->getBackground();
}
}
}
return crBack;
}
// XAccessibleSelection
void SwAccessibleCell::selectAccessibleChild(
sal_Int32 nChildIndex )
{
m_aSelectionHelper.selectAccessibleChild(nChildIndex);
}
sal_Bool SwAccessibleCell::isAccessibleChildSelected(
sal_Int32 nChildIndex )
{
return m_aSelectionHelper.isAccessibleChildSelected(nChildIndex);
}
void SwAccessibleCell::clearAccessibleSelection( )
{
}
void SwAccessibleCell::selectAllAccessibleChildren( )
{
m_aSelectionHelper.selectAllAccessibleChildren();
}
sal_Int32 SwAccessibleCell::getSelectedAccessibleChildCount( )
{
return m_aSelectionHelper.getSelectedAccessibleChildCount();
}
uno::Reference<XAccessible> SwAccessibleCell::getSelectedAccessibleChild(
sal_Int32 nSelectedChildIndex )
{
return m_aSelectionHelper.getSelectedAccessibleChild(nSelectedChildIndex);
}
void SwAccessibleCell::deselectAccessibleChild(
sal_Int32 nSelectedChildIndex )
{
m_aSelectionHelper.deselectAccessibleChild(nSelectedChildIndex);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */