diff options
author | dsinclair <dsinclair@chromium.org> | 2016-10-05 12:00:34 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-10-05 12:00:34 -0700 |
commit | 8afe15a019c78a24168ebc42306342c25358ccdf (patch) | |
tree | 09012697e63b0a1fa4654f6e6f93b69a04421171 | |
parent | 7c1b07fde27dfbbf1d979d9dfd616b1a42591e5f (diff) | |
download | pdfium-8afe15a019c78a24168ebc42306342c25358ccdf.tar.xz |
Cleanup some CPDFSDK_PageView annotation code.
This Cl cleans up the code regarding CPDFSDK_Annots in CPDFSDK_PageView.
This includes:
* Makes DeleteAnnot XFA only and wraps at the call site.
* Removes unused methods
* Replaces use of CountAnnots and GetAnnot with vector iteration
* Removes {Set|Kill}FocusAnnot from CPDFSDK_PageView
* Renames m_fxAnnotArray to m_SDKAnnotArray
Review-Url: https://codereview.chromium.org/2384323005
-rw-r--r-- | fpdfsdk/cba_annotiterator.cpp | 133 | ||||
-rw-r--r-- | fpdfsdk/cba_annotiterator.h | 11 | ||||
-rw-r--r-- | fpdfsdk/cpdfsdk_document.cpp | 34 | ||||
-rw-r--r-- | fpdfsdk/cpdfsdk_document.h | 7 | ||||
-rw-r--r-- | fpdfsdk/cpdfsdk_pageview.cpp | 112 | ||||
-rw-r--r-- | fpdfsdk/cpdfsdk_pageview.h | 34 | ||||
-rw-r--r-- | fpdfsdk/javascript/Document.cpp | 2 | ||||
-rw-r--r-- | fpdfsdk/javascript/app.cpp | 2 |
8 files changed, 124 insertions, 211 deletions
diff --git a/fpdfsdk/cba_annotiterator.cpp b/fpdfsdk/cba_annotiterator.cpp index ef9ab2ad31..a447034cda 100644 --- a/fpdfsdk/cba_annotiterator.cpp +++ b/fpdfsdk/cba_annotiterator.cpp @@ -10,18 +10,22 @@ #include "fpdfsdk/cpdfsdk_annot.h" #include "fpdfsdk/cpdfsdk_pageview.h" -// static -bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1, - const CPDFSDK_Annot* p2) { +namespace { + +CFX_FloatRect GetAnnotRect(const CPDFSDK_Annot* pAnnot) { + return pAnnot->GetPDFAnnot()->GetRect(); +} + +bool CompareByLeftAscending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) { return GetAnnotRect(p1).left < GetAnnotRect(p2).left; } -// static -bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1, - const CPDFSDK_Annot* p2) { +bool CompareByTopDescending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) { return GetAnnotRect(p1).top > GetAnnotRect(p2).top; } +} // namespace + CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView, CPDF_Annot::Subtype nAnnotSubtype) : m_eTabOrder(STRUCTURE), @@ -66,27 +70,45 @@ CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) { return *(--iter); } +void CBA_AnnotIterator::CollectAnnots(std::vector<CPDFSDK_Annot*>* pArray) { + for (auto pAnnot : m_pPageView->GetAnnotList()) { + if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && + !pAnnot->IsSignatureWidget()) { + pArray->push_back(pAnnot); + } + } +} + +CFX_FloatRect CBA_AnnotIterator::AddToAnnotsList( + std::vector<CPDFSDK_Annot*>* sa, + size_t idx) { + CPDFSDK_Annot* pLeftTopAnnot = sa->at(idx); + CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot); + m_Annots.push_back(pLeftTopAnnot); + sa->erase(sa->begin() + idx); + return rcLeftTop; +} + +void CBA_AnnotIterator::AddSelectedToAnnots(std::vector<CPDFSDK_Annot*>* sa, + std::vector<size_t>* aSelect) { + for (size_t i = 0; i < aSelect->size(); ++i) + m_Annots.push_back(sa->at(aSelect->at(i))); + + for (int i = aSelect->size() - 1; i >= 0; --i) + sa->erase(sa->begin() + aSelect->at(i)); +} + void CBA_AnnotIterator::GenerateResults() { switch (m_eTabOrder) { - case STRUCTURE: { - for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { - CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); - if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && - !pAnnot->IsSignatureWidget()) - m_Annots.push_back(pAnnot); - } + case STRUCTURE: + CollectAnnots(&m_Annots); break; - } + case ROW: { std::vector<CPDFSDK_Annot*> sa; - for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { - CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); - if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && - !pAnnot->IsSignatureWidget()) - sa.push_back(pAnnot); - } - + CollectAnnots(&sa); std::sort(sa.begin(), sa.end(), CompareByLeftAscending); + while (!sa.empty()) { int nLeftTopIndex = -1; FX_FLOAT fTop = 0.0f; @@ -97,38 +119,28 @@ void CBA_AnnotIterator::GenerateResults() { fTop = rcAnnot.top; } } - if (nLeftTopIndex >= 0) { - CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex]; - CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot); - m_Annots.push_back(pLeftTopAnnot); - sa.erase(sa.begin() + nLeftTopIndex); - - std::vector<int> aSelect; - for (size_t i = 0; i < sa.size(); ++i) { - CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); - FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f; - if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top) - aSelect.push_back(i); - } - for (size_t i = 0; i < aSelect.size(); ++i) - m_Annots.push_back(sa[aSelect[i]]); + if (nLeftTopIndex < 0) + continue; + + CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex); - for (int i = aSelect.size() - 1; i >= 0; --i) - sa.erase(sa.begin() + aSelect[i]); + std::vector<size_t> aSelect; + for (size_t i = 0; i < sa.size(); ++i) { + CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); + FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f; + if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top) + aSelect.push_back(i); } + AddSelectedToAnnots(&sa, &aSelect); } break; } + case COLUMN: { std::vector<CPDFSDK_Annot*> sa; - for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { - CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); - if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && - !pAnnot->IsSignatureWidget()) - sa.push_back(pAnnot); - } - + CollectAnnots(&sa); std::sort(sa.begin(), sa.end(), CompareByTopDescending); + while (!sa.empty()) { int nLeftTopIndex = -1; FX_FLOAT fLeft = -1.0f; @@ -142,32 +154,21 @@ void CBA_AnnotIterator::GenerateResults() { fLeft = rcAnnot.left; } } + if (nLeftTopIndex < 0) + continue; - if (nLeftTopIndex >= 0) { - CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex]; - CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot); - m_Annots.push_back(pLeftTopAnnot); - sa.erase(sa.begin() + nLeftTopIndex); - - std::vector<int> aSelect; - for (size_t i = 0; i < sa.size(); ++i) { - CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); - FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f; - if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right) - aSelect.push_back(i); - } - for (size_t i = 0; i < aSelect.size(); ++i) - m_Annots.push_back(sa[aSelect[i]]); + CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex); - for (int i = aSelect.size() - 1; i >= 0; --i) - sa.erase(sa.begin() + aSelect[i]); + std::vector<size_t> aSelect; + for (size_t i = 0; i < sa.size(); ++i) { + CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); + FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f; + if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right) + aSelect.push_back(i); } + AddSelectedToAnnots(&sa, &aSelect); } break; } } } - -CFX_FloatRect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) { - return pAnnot->GetPDFAnnot()->GetRect(); -} diff --git a/fpdfsdk/cba_annotiterator.h b/fpdfsdk/cba_annotiterator.h index a896c862c7..5cbe8e3d57 100644 --- a/fpdfsdk/cba_annotiterator.h +++ b/fpdfsdk/cba_annotiterator.h @@ -31,13 +31,10 @@ class CBA_AnnotIterator { private: void GenerateResults(); - static CFX_FloatRect GetAnnotRect(const CPDFSDK_Annot* pAnnot); - - // Function signature compatible with std::sort(). - static bool CompareByLeftAscending(const CPDFSDK_Annot* p1, - const CPDFSDK_Annot* p2); - static bool CompareByTopDescending(const CPDFSDK_Annot* p1, - const CPDFSDK_Annot* p2); + void CollectAnnots(std::vector<CPDFSDK_Annot*>* pArray); + CFX_FloatRect AddToAnnotsList(std::vector<CPDFSDK_Annot*>* sa, size_t idx); + void AddSelectedToAnnots(std::vector<CPDFSDK_Annot*>* sa, + std::vector<size_t>* aSelect); TabOrder m_eTabOrder; CPDFSDK_PageView* m_pPageView; diff --git a/fpdfsdk/cpdfsdk_document.cpp b/fpdfsdk/cpdfsdk_document.cpp index 86d3b76e81..544c1e6e19 100644 --- a/fpdfsdk/cpdfsdk_document.cpp +++ b/fpdfsdk/cpdfsdk_document.cpp @@ -39,8 +39,10 @@ CPDFSDK_Document::CPDFSDK_Document(UnderlyingDocumentType* pDoc, CPDFSDK_Document::~CPDFSDK_Document() { m_bBeingDestroyed = TRUE; - for (auto& it : m_pageMap) - it.second->KillFocusAnnotIfNeeded(); + for (auto& it : m_pageMap) { + if (it.second->IsValidSDKAnnot(GetFocusAnnot())) + KillFocusAnnot(0); + } for (auto& it : m_pageMap) delete it.second; @@ -144,11 +146,12 @@ void CPDFSDK_Document::RemovePageView(UnderlyingPageType* pUnderlyingPage) { pPageView->SetBeingDestroyed(); // This must happen before we remove |pPageView| from the map because - // |KillFocusAnnotIfNeeded| can call into the |GetPage| method which will + // |KillFocusAnnot| can call into the |GetPage| method which will // look for this page view in the map, if it doesn't find it a new one will // be created. We then have two page views pointing to the same page and // bad things happen. - pPageView->KillFocusAnnotIfNeeded(); + if (pPageView->IsValidSDKAnnot(GetFocusAnnot())) + KillFocusAnnot(0); // Remove the page from the map to make sure we don't accidentally attempt // to use the |pPageView| while we're cleaning it up. @@ -171,29 +174,18 @@ void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot) { for (const auto& it : m_pageMap) { CPDFSDK_PageView* pPageView = it.second; - if (pPageView != pSender) { + if (pPageView != pSender) pPageView->UpdateView(pAnnot); - } } } -CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() { - return m_pFocusAnnot.Get(); -} - -FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot::ObservedPtr* pAnnot, - uint32_t nFlag) { +FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot::ObservedPtr* pAnnot) { if (m_bBeingDestroyed) return FALSE; - if (m_pFocusAnnot == *pAnnot) return TRUE; - - if (m_pFocusAnnot) { - if (!KillFocusAnnot(nFlag)) - return FALSE; - } - + if (m_pFocusAnnot && !KillFocusAnnot(0)) + return FALSE; if (!*pAnnot) return FALSE; @@ -208,7 +200,7 @@ FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot::ObservedPtr* pAnnot, if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, &pLastFocusAnnot)) return FALSE; #endif // PDF_ENABLE_XFA - if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag)) + if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, 0)) return FALSE; if (!m_pFocusAnnot) { m_pFocusAnnot.Reset(pAnnot->Get()); @@ -251,7 +243,7 @@ FX_BOOL CPDFSDK_Document::KillFocusAnnot(uint32_t nFlag) { } void CPDFSDK_Document::OnCloseDocument() { - KillFocusAnnot(); + KillFocusAnnot(0); } FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) { diff --git a/fpdfsdk/cpdfsdk_document.h b/fpdfsdk/cpdfsdk_document.h index 196fba42cc..697ee48f9d 100644 --- a/fpdfsdk/cpdfsdk_document.h +++ b/fpdfsdk/cpdfsdk_document.h @@ -64,12 +64,11 @@ class CPDFSDK_Document : public CFX_Observable<CPDFSDK_Document> { void RemovePageView(UnderlyingPageType* pPage); void UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot); - CPDFSDK_Annot* GetFocusAnnot(); - IJS_Runtime* GetJsRuntime(); - FX_BOOL SetFocusAnnot(CPDFSDK_Annot::ObservedPtr* pAnnot, uint32_t nFlag = 0); - FX_BOOL KillFocusAnnot(uint32_t nFlag = 0); + CPDFSDK_Annot* GetFocusAnnot() { return m_pFocusAnnot.Get(); } + FX_BOOL SetFocusAnnot(CPDFSDK_Annot::ObservedPtr* pAnnot); + FX_BOOL KillFocusAnnot(uint32_t nFlag); FX_BOOL ExtractPages(const std::vector<uint16_t>& arrExtraPages, CPDF_Document* pDstDoc); diff --git a/fpdfsdk/cpdfsdk_pageview.cpp b/fpdfsdk/cpdfsdk_pageview.cpp index 921e4a396b..7825701f15 100644 --- a/fpdfsdk/cpdfsdk_pageview.cpp +++ b/fpdfsdk/cpdfsdk_pageview.cpp @@ -67,10 +67,10 @@ CPDFSDK_PageView::~CPDFSDK_PageView() { CPDFSDK_Environment* pEnv = m_pSDKDoc->GetEnv(); CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); - for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) + for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) pAnnotHandlerMgr->ReleaseAnnot(pAnnot); - m_fxAnnotArray.clear(); + m_SDKAnnotArray.clear(); m_pAnnotList.reset(); #ifndef PDF_ENABLE_XFA @@ -133,28 +133,6 @@ void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice, } } -const CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX, - FX_FLOAT pageY) { - for (const auto& pAnnot : m_pAnnotList->All()) { - CFX_FloatRect annotRect = pAnnot->GetRect(); - if (annotRect.Contains(pageX, pageY)) - return pAnnot.get(); - } - return nullptr; -} - -const CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX, - FX_FLOAT pageY) { - for (const auto& pAnnot : m_pAnnotList->All()) { - if (pAnnot->GetSubtype() == CPDF_Annot::Subtype::WIDGET) { - CFX_FloatRect annotRect = pAnnot->GetRect(); - if (annotRect.Contains(pageX, pageY)) - return pAnnot.get(); - } - } - return nullptr; -} - CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY) { CPDFSDK_Environment* pEnv = m_pSDKDoc->GetEnv(); @@ -193,27 +171,6 @@ CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX, return nullptr; } -void CPDFSDK_PageView::KillFocusAnnotIfNeeded() { - // if there is a focused annot on the page, we should kill the focus first. - if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) { - if (pdfium::ContainsValue(m_fxAnnotArray, focusedAnnot)) - KillFocusAnnot(); - } -} - -CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) { - CPDFSDK_Environment* pEnv = m_pSDKDoc->GetEnv(); - ASSERT(pEnv); - CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr(); - CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this); - if (!pSDKAnnot) - return nullptr; - - m_fxAnnotArray.push_back(pSDKAnnot); - pAnnotHandler->Annot_OnCreate(pSDKAnnot); - return pSDKAnnot; -} - #ifdef PDF_ENABLE_XFA CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CXFA_FFWidget* pPDFAnnot) { if (!pPDFAnnot) @@ -229,23 +186,11 @@ CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CXFA_FFWidget* pPDFAnnot) { if (!pSDKAnnot) return nullptr; - m_fxAnnotArray.push_back(pSDKAnnot); + m_SDKAnnotArray.push_back(pSDKAnnot); return pSDKAnnot; } -#endif // PDF_ENABLE_XFA - -CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) { - return pDict ? AddAnnot(pDict->GetStringFor("Subtype").c_str(), pDict) - : nullptr; -} - -CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType, - CPDF_Dictionary* pDict) { - return nullptr; -} FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) { -#ifdef PDF_ENABLE_XFA if (!pAnnot) return FALSE; CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage(); @@ -254,23 +199,21 @@ FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) { return FALSE; if (GetFocusAnnot() == pAnnot) - KillFocusAnnot(); + m_pSDKDoc->KillFocusAnnot(0); CPDFSDK_Environment* pEnv = m_pSDKDoc->GetEnv(); CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr(); if (pAnnotHandler) pAnnotHandler->ReleaseAnnot(pAnnot); - auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot); - if (it != m_fxAnnotArray.end()) - m_fxAnnotArray.erase(it); + auto it = std::find(m_SDKAnnotArray.begin(), m_SDKAnnotArray.end(), pAnnot); + if (it != m_SDKAnnotArray.end()) + m_SDKAnnotArray.erase(it); if (m_pCaptureWidget.Get() == pAnnot) m_pCaptureWidget.Reset(); return TRUE; -#else // PDF_ENABLE_XFA - return FALSE; -#endif // PDF_ENABLE_XFA } +#endif // PDF_ENABLE_XFA CPDF_Document* CPDFSDK_PageView::GetPDFDocument() { if (m_page) { @@ -291,16 +234,8 @@ CPDF_Page* CPDFSDK_PageView::GetPDFPage() const { #endif // PDF_ENABLE_XFA } -size_t CPDFSDK_PageView::CountAnnots() const { - return m_fxAnnotArray.size(); -} - -CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) { - return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr; -} - CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) { - for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) { + for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) { if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict) return pAnnot; } @@ -312,7 +247,7 @@ CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(CXFA_FFWidget* hWidget) { if (!hWidget) return nullptr; - for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) { + for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) { if (pAnnot->GetXFAWidget() == hWidget) return pAnnot; } @@ -324,7 +259,7 @@ FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CFX_FloatPoint& point, uint32_t nFlag) { CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y)); if (!pAnnot) { - KillFocusAnnot(nFlag); + m_pSDKDoc->KillFocusAnnot(nFlag); return FALSE; } @@ -336,7 +271,7 @@ FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CFX_FloatPoint& point, if (!pAnnot) return FALSE; - SetFocusAnnot(&pAnnot); + m_pSDKDoc->SetFocusAnnot(&pAnnot); return TRUE; } @@ -355,7 +290,7 @@ FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CFX_FloatPoint& point, return FALSE; if (ok) - SetFocusAnnot(&pAnnot); + m_pSDKDoc->SetFocusAnnot(&pAnnot); return TRUE; } @@ -369,7 +304,7 @@ FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CFX_FloatPoint& point, return FALSE; if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point)) - SetFocusAnnot(&pFXAnnot); + m_pSDKDoc->SetFocusAnnot(&pFXAnnot); return TRUE; } @@ -482,7 +417,7 @@ void CPDFSDK_PageView::LoadFXAnnots() { CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this); if (!pAnnot) continue; - m_fxAnnotArray.push_back(pAnnot); + m_SDKAnnotArray.push_back(pAnnot); pAnnotHandlerMgr->Annot_OnLoad(pAnnot); } @@ -506,7 +441,7 @@ void CPDFSDK_PageView::LoadFXAnnots() { CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this); if (!pAnnot) continue; - m_fxAnnotArray.push_back(pAnnot); + m_SDKAnnotArray.push_back(pAnnot); pAnnotHandlerMgr->Annot_OnLoad(pAnnot); } @@ -560,16 +495,15 @@ bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const { return it != annots.end(); } +bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const { + if (!p) + return false; + return pdfium::ContainsValue(m_SDKAnnotArray, p); +} + CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() { CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot(); - if (!pFocusAnnot) - return nullptr; - - for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) { - if (pAnnot == pFocusAnnot) - return pAnnot; - } - return nullptr; + return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr; } int CPDFSDK_PageView::GetPageIndexForStaticPDF() const { diff --git a/fpdfsdk/cpdfsdk_pageview.h b/fpdfsdk/cpdfsdk_pageview.h index f29af7055a..cd9ddec6bd 100644 --- a/fpdfsdk/cpdfsdk_pageview.h +++ b/fpdfsdk/cpdfsdk_pageview.h @@ -35,32 +35,24 @@ class CPDFSDK_PageView final : public CPDF_Page::View { CPDF_RenderOptions* pOptions); #endif // PDF_ENABLE_XFA - const CPDF_Annot* GetPDFAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY); CPDFSDK_Annot* GetFXAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY); - const CPDF_Annot* GetPDFWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY); CPDFSDK_Annot* GetFXWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY); - CPDFSDK_Annot* GetFocusAnnot(); - void SetFocusAnnot(CPDFSDK_Annot::ObservedPtr* pSDKAnnot, - uint32_t nFlag = 0) { - m_pSDKDoc->SetFocusAnnot(pSDKAnnot, nFlag); - } - FX_BOOL KillFocusAnnot(uint32_t nFlag = 0) { - return m_pSDKDoc->KillFocusAnnot(nFlag); - } - void KillFocusAnnotIfNeeded(); - CPDFSDK_Annot* AddAnnot(CPDF_Dictionary* pDict); - CPDFSDK_Annot* AddAnnot(const FX_CHAR* lpSubType, CPDF_Dictionary* pDict); - CPDFSDK_Annot* AddAnnot(CPDF_Annot* pPDFAnnot); + void LoadFXAnnots(); + CPDFSDK_Annot* GetFocusAnnot(); + bool IsValidAnnot(const CPDF_Annot* p) const; + bool IsValidSDKAnnot(const CPDFSDK_Annot* p) const; - FX_BOOL DeleteAnnot(CPDFSDK_Annot* pAnnot); - size_t CountAnnots() const; - CPDFSDK_Annot* GetAnnot(size_t nIndex); + const std::vector<CPDFSDK_Annot*>& GetAnnotList() const { + return m_SDKAnnotArray; + } CPDFSDK_Annot* GetAnnotByDict(CPDF_Dictionary* pDict); #ifdef PDF_ENABLE_XFA + FX_BOOL DeleteAnnot(CPDFSDK_Annot* pAnnot); CPDFSDK_Annot* AddAnnot(CXFA_FFWidget* pPDFAnnot); CPDFSDK_Annot* GetAnnotByXFAWidget(CXFA_FFWidget* hWidget); + CPDFXFA_Page* GetPDFXFAPage() { return m_page; } #endif // PDF_ENABLE_XFA @@ -82,16 +74,12 @@ class CPDFSDK_PageView final : public CPDF_Page::View { double deltaY, const CFX_FloatPoint& point, int nFlag); - bool IsValidAnnot(const CPDF_Annot* p) const; + void GetCurrentMatrix(CFX_Matrix& matrix) { matrix = m_curMatrix; } void UpdateRects(const std::vector<CFX_FloatRect>& rects); void UpdateView(CPDFSDK_Annot* pAnnot); - const std::vector<CPDFSDK_Annot*>& GetAnnotList() const { - return m_fxAnnotArray; - } int GetPageIndex() const; - void LoadFXAnnots(); void SetValid(FX_BOOL bValid) { m_bValid = bValid; } FX_BOOL IsValid() { return m_bValid; } @@ -113,7 +101,7 @@ class CPDFSDK_PageView final : public CPDF_Page::View { CFX_Matrix m_curMatrix; UnderlyingPageType* const m_page; std::unique_ptr<CPDF_AnnotList> m_pAnnotList; - std::vector<CPDFSDK_Annot*> m_fxAnnotArray; + std::vector<CPDFSDK_Annot*> m_SDKAnnotArray; CPDFSDK_Document* const m_pSDKDoc; CPDFSDK_Annot::ObservedPtr m_pCaptureWidget; #ifndef PDF_ENABLE_XFA diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp index 233097e42c..c143edd88a 100644 --- a/fpdfsdk/javascript/Document.cpp +++ b/fpdfsdk/javascript/Document.cpp @@ -531,7 +531,9 @@ FX_BOOL Document::removeField(IJS_Context* cc, // and creating a new pageview at this point will cause bad things. CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, false); if (pPageView) { +#if PDF_ENABLE_XFA pPageView->DeleteAnnot(pWidget); +#endif // PDF_ENABLE_XFA pPageView->UpdateRects(aRefresh); } } diff --git a/fpdfsdk/javascript/app.cpp b/fpdfsdk/javascript/app.cpp index eba8d4d74f..55f976e522 100644 --- a/fpdfsdk/javascript/app.cpp +++ b/fpdfsdk/javascript/app.cpp @@ -433,7 +433,7 @@ FX_BOOL app::alert(IJS_Context* cc, pRuntime->BeginBlock(); if (CPDFSDK_Document* pDoc = pEnv->GetSDKDocument()) - pDoc->KillFocusAnnot(); + pDoc->KillFocusAnnot(0); vRet = CJS_Value(pRuntime, pEnv->JS_appAlert(swMsg.c_str(), swTitle.c_str(), iType, iIcon)); |