tdf#154827 and tdf#154428 graphics anchored in Writer as character..

..become anchored at paragraph when reopening the document

regression from
    commit 8611f6e259b807b4f19c8dc0eab86ca648891ce3
    ref-count SdrObject

This used to work by accident, previously, the SwXGroupShape that we
need would get created and destroyed and handful of times, and then
properly created.

Now, however, the first time we create it it sticks around, which means
it does not get the m_pFormat field configured correctly.

Fix this by removing the caching of m_pFormat from SwXShape, and just
fetching it from the model when we need it.

Change-Id: I10fe2ad0b877b3d928d5288e73cfed373b27a506
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150687
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
diff --git a/sw/inc/unodraw.hxx b/sw/inc/unodraw.hxx
index 1f9efea..cc3daaf 100644
--- a/sw/inc/unodraw.hxx
+++ b/sw/inc/unodraw.hxx
@@ -132,7 +132,6 @@ class SwXShape : public SwXShapeBaseClass, public SvtListener
    friend class SwXGroupShape;
    friend class SwFmDrawPage;
    const SwFmDrawPage* m_pPage;
    SwFrameFormat* m_pFormat;

    css::uno::Reference< css::uno::XAggregation > m_xShapeAgg;
    // reference to <XShape>, determined in the
@@ -195,12 +194,6 @@ class SwXShape : public SwXShapeBaseClass, public SvtListener
        @throws css::uno::RuntimeException
    */
    css::uno::Any _getPropAtAggrObj( const OUString& _rPropertyName );
    void SetFrameFormat(SwFrameFormat* pFormat)
    {
        EndListeningAll();
        StartListening(pFormat->GetNotifier());
        m_pFormat = pFormat;
    }

protected:
    virtual ~SwXShape() override;
@@ -252,7 +245,7 @@ public:
    virtual OUString SAL_CALL getShapeType(  ) override;

    SwShapeDescriptor_Impl*     GetDescImpl() {return m_pImpl.get();}
    SwFrameFormat* GetFrameFormat() const { return m_pFormat; }
    SwFrameFormat* GetFrameFormat() const;
    const css::uno::Reference< css::uno::XAggregation >& GetAggregationInterface() const {return m_xShapeAgg;}

    // helper
diff --git a/sw/source/core/unocore/unodraw.cxx b/sw/source/core/unocore/unodraw.cxx
index 7337ce2..61e2649 100644
--- a/sw/source/core/unocore/unodraw.cxx
+++ b/sw/source/core/unocore/unodraw.cxx
@@ -639,7 +639,7 @@ void SwFmDrawPage::add(const uno::Reference< drawing::XShape > & xShape)
                                    static_cast< cppu::OWeakObject * > ( this ) );

    // we're already registered in the model / SwXDrawPage::add() already called
    if(pShape->m_pPage || pShape->m_pFormat || !pShape->m_bDescriptor )
    if(pShape->m_pPage || !pShape->m_bDescriptor )
        return;

    // we're inserted elsewhere already
@@ -787,7 +787,6 @@ void SwFmDrawPage::add(const uno::Reference< drawing::XShape > & xShape)
        {
            pFormat->SetFormatName(pSvxShape->GetSdrObject()->GetName(), false);
        }
        pShape->SetFrameFormat(pFormat);
    }
    pShape->m_bDescriptor = false;

@@ -917,7 +916,6 @@ SwXShape::SwXShape(
        uno::Reference<uno::XInterface> & xShape,
        SwDoc const*const pDoc)
    : m_pPage(nullptr)
    , m_pFormat(nullptr)
    , m_pPropSet(aSwMapProvider.GetPropertySet(PROPERTY_MAP_TEXT_SHAPE))
    , m_pPropertyMapEntries(aSwMapProvider.GetPropertyMapEntries(PROPERTY_MAP_TEXT_SHAPE))
    , m_pImpl(new SwShapeDescriptor_Impl(pDoc))
@@ -948,16 +946,20 @@ SwXShape::SwXShape(
    SdrObject* pObj = SdrObject::getSdrObjectFromXShape(m_xShapeAgg);
    if(pObj)
    {
        auto pFormat = ::FindFrameFormat( pObj );
        if(pFormat)
            SetFrameFormat(pFormat);

        lcl_addShapePropertyEventFactories( *pObj, *this );
        m_pImpl->m_bInitializedPropertyNotifier = true;
    }

}

SwFrameFormat* SwXShape::GetFrameFormat() const
{
    SdrObject* pObj = SdrObject::getSdrObjectFromXShape(m_xShapeAgg);
    if(pObj)
        return ::FindFrameFormat( pObj );
    return nullptr;
}

void SwXShape::AddExistingShapeToFormat( SdrObject const & _rObj )
{
    SdrObjListIter aIter( _rObj, SdrIterMode::DeepNoGroups );
@@ -972,12 +974,7 @@ void SwXShape::AddExistingShapeToFormat( SdrObject const & _rObj )
        if ( pSwShape )
        {
            if ( pSwShape->m_bDescriptor )
            {
                auto pFormat = ::FindFrameFormat( pCurrent );
                if ( pFormat )
                    pSwShape->SetFrameFormat(pFormat);
                pSwShape->m_bDescriptor = false;
            }

            if ( !pSwShape->m_pImpl->m_bInitializedPropertyNotifier )
            {
@@ -2091,7 +2088,6 @@ void SwXShape::Notify(const SfxHint& rHint)
{
    if(rHint.GetId() == SfxHintId::Dying)
    {
        m_pFormat = nullptr;
        EndListeningAll();
    }
}
@@ -2835,12 +2831,6 @@ void SwXGroupShape::add( const uno::Reference< XShape >& xShape )
        }
    }
    pSwShape->m_bDescriptor = false;
    //add the group member to the format of the group
    SwFrameFormat* pShapeFormat = ::FindFrameFormat( pSvxShape->GetSdrObject() );

    if(pShapeFormat)
        pSwShape->SetFrameFormat(pShapeFormat);

}

void SwXGroupShape::remove( const uno::Reference< XShape >& xShape )