use UnoCursorPointer in SwXTextCursor

Change-Id: I46d464405d5de6460b10a2965e0c1f011c8142b2
This commit is contained in:
Bjoern Michaelsen 2015-06-04 22:58:25 +02:00
parent a2c467a58a
commit 30f3315d52
5 changed files with 46 additions and 86 deletions

View File

@ -108,16 +108,17 @@ namespace sw
{
public:
UnoCursorPointer()
: m_pCursor(nullptr)
: m_pCursor(nullptr), m_bSectionRestricted(false)
{}
UnoCursorPointer(std::shared_ptr<SwUnoCrsr> pCursor)
: m_pCursor(pCursor)
UnoCursorPointer(std::shared_ptr<SwUnoCrsr> pCursor, bool bSectionRestricted=false)
: m_pCursor(pCursor), m_bSectionRestricted(bSectionRestricted)
{
m_pCursor->Add(this);
}
UnoCursorPointer(const UnoCursorPointer& pOther)
UnoCursorPointer(const UnoCursorPointer& rOther)
: SwClient(nullptr)
, m_pCursor(pOther.m_pCursor)
, m_pCursor(rOther.m_pCursor)
, m_bSectionRestricted(rOther.m_bSectionRestricted)
{
if(m_pCursor)
m_pCursor->Add(this);
@ -130,8 +131,17 @@ namespace sw
virtual void SwClientNotify(const SwModify& rModify, const SfxHint& rHint) SAL_OVERRIDE
{
SwClient::SwClientNotify(rModify, rHint);
if(m_pCursor && typeid(rHint) == typeid(sw::DocDisposingHint))
m_pCursor->Remove(this);
if(m_pCursor)
{
if(typeid(rHint) == typeid(DocDisposingHint))
m_pCursor->Remove(this);
else if(m_bSectionRestricted && typeid(rHint) == typeid(LegacyModifyHint))
{
const auto pLegacyHint = static_cast<const LegacyModifyHint*>(&rHint);
if(pLegacyHint->m_pOld && pLegacyHint->m_pOld->Which() == RES_UNOCURSOR_LEAVES_SECTION)
m_pCursor->Remove(this);
}
}
if(!GetRegisteredIn())
m_pCursor.reset();
};
@ -158,6 +168,7 @@ namespace sw
}
private:
std::shared_ptr<SwUnoCrsr> m_pCursor;
const bool m_bSectionRestricted;
};
}
#endif

View File

@ -91,9 +91,7 @@ public:
SwPaM const& rSourceCursor,
const enum CursorType eType = CURSOR_ALL);
std::shared_ptr<SwUnoCrsr> GetCursor();
//const SwUnoCrsr* GetConstCursor() const;
SwUnoCrsr* GetCursor();
bool IsAtEndOfMeta() const;
void DeleteAndInsert(OUString const& rText,

View File

@ -432,8 +432,8 @@ public:
void InitNewDoc();
std::shared_ptr<SwUnoCrsr> CreateCursorForSearch(css::uno::Reference< css::text::XTextCursor > & xCrsr);
std::shared_ptr<SwUnoCrsr> FindAny(const css::uno::Reference< css::util::XSearchDescriptor > & xDesc,
SwUnoCrsr* CreateCursorForSearch(css::uno::Reference< css::text::XTextCursor > & xCrsr);
SwUnoCrsr* FindAny(const css::uno::Reference< css::util::XSearchDescriptor > & xDesc,
css::uno::Reference< css::text::XTextCursor > & xCrsr, bool bAll,
sal_Int32& nResult,
css::uno::Reference< css::uno::XInterface > xLastResult);

View File

@ -654,14 +654,13 @@ SwUnoCursorHelper::GetCurTextFormatColl(SwPaM & rPaM, const bool bConditional)
}
class SwXTextCursor::Impl
: public SwClient
{
public:
const SfxItemPropertySet & m_rPropSet;
const enum CursorType m_eType;
const uno::Reference< text::XText > m_xParentText;
std::shared_ptr<SwUnoCrsr> m_pUnoCursor;
sw::UnoCursorPointer m_pUnoCursor;
Impl( SwDoc & rDoc,
const enum CursorType eType,
@ -670,84 +669,36 @@ public:
: m_rPropSet(*aSwMapProvider.GetPropertySet(PROPERTY_MAP_TEXT_CURSOR))
, m_eType(eType)
, m_xParentText(xParent)
, m_pUnoCursor(rDoc.CreateUnoCrsr(rPoint, false))
, m_pUnoCursor(rDoc.CreateUnoCrsr(rPoint, false), true)
{
m_pUnoCursor->Add(this);
if (pMark)
{
GetCursor()->SetMark();
*GetCursor()->GetMark() = *pMark;
m_pUnoCursor->SetMark();
*m_pUnoCursor->GetMark() = *pMark;
}
}
virtual ~Impl()
{
Invalidate();
}
std::shared_ptr<SwUnoCrsr> GetCursor() {
return m_pUnoCursor;
}
SwUnoCrsr& GetCursorOrThrow() {
if(!m_pUnoCursor)
throw uno::RuntimeException("SwXTextCursor: disposed or invalid", 0);
return *m_pUnoCursor.get();
return *m_pUnoCursor;
}
void Invalidate() {
if(m_pUnoCursor)
{
if(GetRegisteredIn() == m_pUnoCursor.get())
m_pUnoCursor->Remove(this);
m_pUnoCursor.reset();
}
}
protected:
// SwClient
virtual void Modify(const SfxPoolItem *pOld, const SfxPoolItem *pNew) SAL_OVERRIDE;
virtual void SwClientNotify(const SwModify& rModify, const SfxHint& rHint) SAL_OVERRIDE;
};
void SwXTextCursor::Impl::Modify(const SfxPoolItem *pOld, const SfxPoolItem *pNew)
{
ClientModify(this, pOld, pNew);
// if the cursor leaves its designated section, it becomes invalid
if (((pOld != NULL) && (pOld->Which() == RES_UNOCURSOR_LEAVES_SECTION)))
Invalidate();
}
void SwXTextCursor::Impl::SwClientNotify(const SwModify& rModify, const SfxHint& rHint)
{
SwClient::SwClientNotify(rModify, rHint);
if(m_pUnoCursor && typeid(rHint) == typeid(sw::DocDisposingHint))
{
Invalidate();
}
}
std::shared_ptr<SwUnoCrsr> SwXTextCursor::GetCursor()
{
return m_pImpl->GetCursor();
}
SwUnoCrsr*SwXTextCursor::GetCursor()
{ return &(*m_pImpl->m_pUnoCursor); }
SwPaM const* SwXTextCursor::GetPaM() const
{
return m_pImpl->GetCursor().get();
}
{ return &(*m_pImpl->m_pUnoCursor); }
SwPaM * SwXTextCursor::GetPaM()
{
return m_pImpl->GetCursor().get();
}
SwPaM* SwXTextCursor::GetPaM()
{ return &(*m_pImpl->m_pUnoCursor); }
SwDoc const* SwXTextCursor::GetDoc() const
{
return m_pImpl->GetCursor() ? m_pImpl->GetCursor()->GetDoc() : 0;
}
{ return m_pImpl->m_pUnoCursor ? m_pImpl->m_pUnoCursor->GetDoc() : nullptr; }
SwDoc * SwXTextCursor::GetDoc()
{
return m_pImpl->GetCursor() ? m_pImpl->GetCursor()->GetDoc() : 0;
}
SwDoc* SwXTextCursor::GetDoc()
{ return m_pImpl->m_pUnoCursor ? m_pImpl->m_pUnoCursor->GetDoc() : nullptr; }
SwXTextCursor::SwXTextCursor(
SwDoc & rDoc,
@ -774,7 +725,7 @@ SwXTextCursor::~SwXTextCursor()
void SwXTextCursor::DeleteAndInsert(const OUString& rText,
const bool bForceExpandHints)
{
auto pUnoCrsr = m_pImpl->GetCursor();
auto pUnoCrsr = static_cast<SwCursor*>(&(*m_pImpl->m_pUnoCursor));
if(pUnoCrsr)
{
// Start/EndAction
@ -782,7 +733,7 @@ void SwXTextCursor::DeleteAndInsert(const OUString& rText,
UnoActionContext aAction(pDoc);
const sal_Int32 nTextLen = rText.getLength();
pDoc->GetIDocumentUndoRedo().StartUndo(UNDO_INSERT, NULL);
SwCursor * pCurrent = pUnoCrsr.get();
auto pCurrent = static_cast<SwCursor*>(pUnoCrsr);
do
{
if (pCurrent->HasMark())
@ -801,8 +752,8 @@ void SwXTextCursor::DeleteAndInsert(const OUString& rText,
pCurrent->Left(rText.getLength(),
CRSR_SKIP_CHARS, false, false);
}
pCurrent = static_cast<SwCursor *>(pCurrent->GetNext());
} while (pCurrent != pUnoCrsr.get());
pCurrent = static_cast<SwCursor*>(pCurrent->GetNext());
} while (pCurrent != pUnoCrsr);
pDoc->GetIDocumentUndoRedo().EndUndo(UNDO_INSERT, NULL);
}
}
@ -857,7 +808,7 @@ bool SwXTextCursor::IsAtEndOfMeta() const
{
if (CURSOR_META == m_pImpl->m_eType)
{
auto pCursor( m_pImpl->GetCursor() );
auto pCursor( m_pImpl->m_pUnoCursor );
SwXMeta const*const pXMeta(
dynamic_cast<SwXMeta*>(m_pImpl->m_xParentText.get()) );
OSL_ENSURE(pXMeta, "no meta?");
@ -971,7 +922,7 @@ sal_Bool SAL_CALL SwXTextCursor::isCollapsed() throw (uno::RuntimeException, std
SolarMutexGuard aGuard;
bool bRet = true;
auto pUnoCrsr(m_pImpl->GetCursor());
auto pUnoCrsr(m_pImpl->m_pUnoCursor);
if(pUnoCrsr && pUnoCrsr->GetMark())
{
bRet = (*pUnoCrsr->GetPoint() == *pUnoCrsr->GetMark());

View File

@ -711,7 +711,7 @@ Reference< util::XReplaceDescriptor > SwXTextDocument::createReplaceDescriptor(
return xRet;
}
std::shared_ptr<SwUnoCrsr> SwXTextDocument::CreateCursorForSearch(Reference< XTextCursor > & xCrsr)
SwUnoCrsr* SwXTextDocument::CreateCursorForSearch(Reference< XTextCursor > & xCrsr)
{
getText();
XText *const pText = xBodyText.get();
@ -808,7 +808,7 @@ Reference< util::XSearchDescriptor > SwXTextDocument::createSearchDescriptor()
// Used for findAll/First/Next
std::shared_ptr<SwUnoCrsr> SwXTextDocument::FindAny(const Reference< util::XSearchDescriptor > & xDesc,
SwUnoCrsr* SwXTextDocument::FindAny(const Reference< util::XSearchDescriptor > & xDesc,
Reference< XTextCursor > & xCrsr,
bool bAll,
sal_Int32& nResult,
@ -816,9 +816,9 @@ std::shared_ptr<SwUnoCrsr> SwXTextDocument::FindAny(const Reference< util::XSea
{
Reference< XUnoTunnel > xDescTunnel(xDesc, UNO_QUERY);
if(!IsValid() || !xDescTunnel.is() || !xDescTunnel->getSomething(SwXTextSearch::getUnoTunnelId()))
return 0;
return nullptr;
std::shared_ptr<SwUnoCrsr> pUnoCrsr(CreateCursorForSearch(xCrsr));
auto pUnoCrsr(CreateCursorForSearch(xCrsr));
const SwXTextSearch* pSearch = reinterpret_cast<const SwXTextSearch*>(
xDescTunnel->getSomething(SwXTextSearch::getUnoTunnelId()));
@ -847,7 +847,7 @@ std::shared_ptr<SwUnoCrsr> SwXTextDocument::FindAny(const Reference< util::XSea
SwXTextRange::getUnoTunnelId()));
}
if(!pRange)
return 0;
return nullptr;
pRange->GetPositions(*pUnoCrsr);
if(pUnoCrsr->HasMark())
{
@ -942,7 +942,7 @@ Reference< XIndexAccess >
if(!pResultCrsr)
throw RuntimeException();
Reference< XIndexAccess > xRet;
xRet = SwXTextRanges::Create( (nResult) ? pResultCrsr.get() : nullptr );
xRet = SwXTextRanges::Create( (nResult) ? &(*pResultCrsr) : nullptr );
return xRet;
}