Resolves: rhbz#1378521 csv dialog a11y returns a new accessible on every query

the gtk a11y rather assumes that the a11y things "belong" to the matching
widget/component and those owners will dispose them, so follow that pattern
here and dispose the a11y objects when the parent is disposed

to reproduce, enable a11y, load a csv, click once in the first column
of the preview area and cancel the dialog and close libreoffice

Change-Id: Ib830da499e9f2d6fed94fb12ede7c929b607ab10
Reviewed-on: https://gerrit.libreoffice.org/29301
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/sc/source/ui/Accessibility/AccessibleCsvControl.cxx b/sc/source/ui/Accessibility/AccessibleCsvControl.cxx
index 61d32ee..39ab0c4 100644
--- a/sc/source/ui/Accessibility/AccessibleCsvControl.cxx
+++ b/sc/source/ui/Accessibility/AccessibleCsvControl.cxx
@@ -865,6 +865,15 @@ ScAccessibleCsvGrid::~ScAccessibleCsvGrid()
    implDispose();
}

void ScAccessibleCsvGrid::disposing()
{
    SolarMutexGuard aGuard;
    for (XAccessibleSet::iterator aI = maAccessibleChildren.begin(); aI != maAccessibleChildren.end(); ++aI)
        aI->second->dispose();
    maAccessibleChildren.clear();
    ScAccessibleCsvControl::disposing();
}

// XAccessibleComponent -------------------------------------------------------

Reference< XAccessible > SAL_CALL ScAccessibleCsvGrid::getAccessibleAtPoint( const css::awt::Point& rPoint )
@@ -882,7 +891,7 @@ Reference< XAccessible > SAL_CALL ScAccessibleCsvGrid::getAccessibleAtPoint( con
            lcl_GetApiColumn( rGrid.GetColumnFromX( rPoint.X ) ) : 0;
        sal_Int32 nRow = (rPoint.Y >= rGrid.GetHdrHeight()) ?
            (rGrid.GetLineFromY( rPoint.Y ) - rGrid.GetFirstVisLine() + 1) : 0;
        xRet = implCreateCellObj( nRow, nColumn );
        xRet = getAccessibleCell(nRow, nColumn);
    }
    return xRet;
}
@@ -912,13 +921,30 @@ sal_Int32 SAL_CALL ScAccessibleCsvGrid::getAccessibleChildCount() throw( Runtime
    return implGetCellCount();
}

Reference<XAccessible> ScAccessibleCsvGrid::getAccessibleCell(sal_Int32 nRow, sal_Int32 nColumn)
{
    sal_Int32 nIndex = implGetIndex(nRow, nColumn);

    XAccessibleSet::iterator aI = maAccessibleChildren.lower_bound(nIndex);
    if (aI != maAccessibleChildren.end() && !(maAccessibleChildren.key_comp()(nIndex, aI->first)))
    {
        // key already exists
        return Reference<XAccessible>(aI->second.get());
    }
    // key does not exist
    rtl::Reference<ScAccessibleCsvControl> xNew = implCreateCellObj(nRow, nColumn);
    maAccessibleChildren.insert(aI, XAccessibleSet::value_type(nIndex, xNew));
    return Reference<XAccessible>(xNew.get());
}

Reference< XAccessible > SAL_CALL ScAccessibleCsvGrid::getAccessibleChild( sal_Int32 nIndex )
        throw( IndexOutOfBoundsException, RuntimeException, std::exception )
{
    SolarMutexGuard aGuard;
    ensureAlive();
    ensureValidIndex( nIndex );
    return implCreateCellObj( implGetRow( nIndex ), implGetColumn( nIndex ) );

    return getAccessibleCell(implGetRow(nIndex), implGetColumn(nIndex));
}

Reference< XAccessibleRelationSet > SAL_CALL ScAccessibleCsvGrid::getAccessibleRelationSet()
@@ -1066,7 +1092,7 @@ Reference< XAccessible > SAL_CALL ScAccessibleCsvGrid::getAccessibleCellAt( sal_
    SolarMutexGuard aGuard;
    ensureAlive();
    ensureValidPosition( nRow, nColumn );
    return implCreateCellObj( nRow, nColumn );
    return getAccessibleCell(nRow, nColumn);
}

Reference< XAccessible > SAL_CALL ScAccessibleCsvGrid::getAccessibleCaption()
@@ -1235,7 +1261,6 @@ Sequence< sal_Int8 > SAL_CALL ScAccessibleCsvGrid::getImplementationId() throw( 
void ScAccessibleCsvGrid::SendFocusEvent( bool bFocused )
{
    ScAccessibleCsvControl::SendFocusEvent( bFocused );

    AccessibleEventObject aEvent;
    aEvent.EventId = AccessibleEventId::ACTIVE_DESCENDANT_CHANGED;
    aEvent.Source = Reference< XAccessible >( this );
diff --git a/sc/source/ui/inc/AccessibleCsvControl.hxx b/sc/source/ui/inc/AccessibleCsvControl.hxx
index abf6e65c..d599c2d 100644
--- a/sc/source/ui/inc/AccessibleCsvControl.hxx
+++ b/sc/source/ui/inc/AccessibleCsvControl.hxx
@@ -31,6 +31,7 @@
#include <comphelper/uno3.hxx>
#include <vcl/vclptr.hxx>
#include "AccessibleContextBase.hxx"
#include <map>

class ScCsvControl;
namespace utl { class AccessibleStateSetHelper; }
@@ -285,10 +286,16 @@ class ScAccessibleCsvGrid : public ScAccessibleCsvControl, public ScAccessibleCs
{
protected:
    typedef css::uno::Reference< css::accessibility::XAccessibleTable > XAccessibleTableRef;
    typedef std::map< sal_Int32, rtl::Reference<ScAccessibleCsvControl> > XAccessibleSet;

private:
    XAccessibleSet maAccessibleChildren;

public:
    explicit                    ScAccessibleCsvGrid( ScCsvGrid& rGrid );
    virtual                     ~ScAccessibleCsvGrid() override;
    using ScAccessibleContextBase::disposing;
    virtual void SAL_CALL       disposing() override;

    // XAccessibleComponent ---------------------------------------------------

@@ -512,6 +519,8 @@ private:
    OUString implGetCellText( sal_Int32 nRow, sal_Int32 nColumn ) const;
    /** Creates a new accessible object of the specified cell. Indexes must be valid. */
    ScAccessibleCsvControl* implCreateCellObj( sal_Int32 nRow, sal_Int32 nColumn ) const;

    css::uno::Reference<css::accessibility::XAccessible> getAccessibleCell(sal_Int32 nRow, sal_Int32 nColumn);
};

/** Accessible class representing a cell of the CSV grid control. */