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

View File

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

View File

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

View File

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

View File

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