diff options
author | Lei Zhang <thestig@chromium.org> | 2015-10-23 18:06:17 -0700 |
---|---|---|
committer | Lei Zhang <thestig@chromium.org> | 2015-10-23 18:06:17 -0700 |
commit | 9b2741829b8a8c511ef0f2f313ff53d77ac96307 (patch) | |
tree | 44e4351192f0af909671df95a1aac16a1609f750 | |
parent | bef190fcacb7dde4fedc1360e3019ab9968db16a (diff) | |
download | pdfium-chromium/2546.tar.xz |
Cleanup parts of CPDFSDK_AnnotIterator and CPDFSDK_PageView.chromium/2547chromium/2546
R=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1411203007 .
-rw-r--r-- | fpdfsdk/include/fsdk_annothandler.h | 45 | ||||
-rw-r--r-- | fpdfsdk/include/fsdk_mgr.h | 17 | ||||
-rw-r--r-- | fpdfsdk/src/fsdk_annothandler.cpp | 181 | ||||
-rw-r--r-- | fpdfsdk/src/fsdk_baseform.cpp | 6 | ||||
-rw-r--r-- | fpdfsdk/src/fsdk_mgr.cpp | 139 |
5 files changed, 117 insertions, 271 deletions
diff --git a/fpdfsdk/include/fsdk_annothandler.h b/fpdfsdk/include/fsdk_annothandler.h index 369a080a9b..89705459f2 100644 --- a/fpdfsdk/include/fsdk_annothandler.h +++ b/fpdfsdk/include/fsdk_annothandler.h @@ -8,6 +8,7 @@ #define FPDFSDK_INCLUDE_FSDK_ANNOTHANDLER_H_ #include <map> +#include <vector> #include "../../core/include/fxcrt/fx_basic.h" @@ -122,7 +123,7 @@ class IPDFSDK_AnnotHandler { class CPDFSDK_BFAnnotHandler : public IPDFSDK_AnnotHandler { public: - CPDFSDK_BFAnnotHandler(CPDFDoc_Environment* pApp) + explicit CPDFSDK_BFAnnotHandler(CPDFDoc_Environment* pApp) : m_pApp(pApp), m_pFormFiller(NULL) {} ~CPDFSDK_BFAnnotHandler() override {} @@ -303,38 +304,20 @@ class CPDFSDK_AnnotHandlerMgr { CPDFDoc_Environment* m_pApp; }; -typedef int (*AI_COMPARE)(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2); - class CPDFSDK_AnnotIterator { public: - CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView, - FX_BOOL bReverse, - FX_BOOL bIgnoreTopmost = FALSE, - FX_BOOL bCircle = FALSE, - CFX_PtrArray* pList = NULL); - virtual ~CPDFSDK_AnnotIterator() {} - - virtual CPDFSDK_Annot* Next(const CPDFSDK_Annot* pCurrent); - virtual CPDFSDK_Annot* Prev(const CPDFSDK_Annot* pCurrent); - virtual CPDFSDK_Annot* Next(int& index); - virtual CPDFSDK_Annot* Prev(int& index); - virtual int Count() { return m_pIteratorAnnotList.GetSize(); } - - virtual FX_BOOL InitIteratorAnnotList(CPDFSDK_PageView* pPageView, - CFX_PtrArray* pList = NULL); - - void InsertSort(CFX_PtrArray& arrayList, AI_COMPARE pCompare); - - protected: - CPDFSDK_Annot* NextAnnot(const CPDFSDK_Annot* pCurrent); - CPDFSDK_Annot* PrevAnnot(const CPDFSDK_Annot* pCurrent); - CPDFSDK_Annot* NextAnnot(int& index); - CPDFSDK_Annot* PrevAnnot(int& index); - - CFX_PtrArray m_pIteratorAnnotList; - FX_BOOL m_bReverse; - FX_BOOL m_bIgnoreTopmost; - FX_BOOL m_bCircle; + CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView, bool bReverse); + ~CPDFSDK_AnnotIterator(); + + CPDFSDK_Annot* Next(); + + private: + CPDFSDK_Annot* NextAnnot(); + CPDFSDK_Annot* PrevAnnot(); + + std::vector<CPDFSDK_Annot*> m_iteratorAnnotList; + const bool m_bReverse; + size_t m_pos; }; #endif // FPDFSDK_INCLUDE_FSDK_ANNOTHANDLER_H_ diff --git a/fpdfsdk/include/fsdk_mgr.h b/fpdfsdk/include/fsdk_mgr.h index 068731ee8f..fa2860aa00 100644 --- a/fpdfsdk/include/fsdk_mgr.h +++ b/fpdfsdk/include/fsdk_mgr.h @@ -12,6 +12,7 @@ #include "../../core/include/fpdftext/fpdf_text.h" #include "../../public/fpdf_formfill.h" #include "../../public/fpdf_fwlevent.h" // cross platform keycode and events define. +#include "../../third_party/base/nonstd_unique_ptr.h" #include "fsdk_common.h" #include "fsdk_define.h" #include "fx_systemhandler.h" @@ -307,8 +308,8 @@ class CPDFSDK_PageView final { CPDFSDK_Annot* AddAnnot(const FX_CHAR* lpSubType, CPDF_Dictionary* pDict); CPDFSDK_Annot* AddAnnot(CPDF_Annot* pPDFAnnot); FX_BOOL DeleteAnnot(CPDFSDK_Annot* pAnnot); - int CountAnnots(); - CPDFSDK_Annot* GetAnnot(int nIndex); + int CountAnnots() const; + CPDFSDK_Annot* GetAnnot(size_t nIndex); CPDFSDK_Annot* GetAnnotByDict(CPDF_Dictionary* pDict); CPDF_Page* GetPDFPage() { return m_page; } CPDF_Document* GetPDFDocument(); @@ -324,11 +325,13 @@ class CPDFSDK_PageView final { double deltaY, const CPDF_Point& point, int nFlag); - FX_BOOL IsValidAnnot(void* p); + FX_BOOL IsValidAnnot(CPDF_Annot* p) const; void GetCurrentMatrix(CPDF_Matrix& matrix) { matrix = m_curMatrix; } void UpdateRects(CFX_RectArray& rects); void UpdateView(CPDFSDK_Annot* pAnnot); - CFX_PtrArray* GetAnnotList() { return &m_fxAnnotArray; } + const std::vector<CPDFSDK_Annot*>& GetAnnotList() const { + return m_fxAnnotArray; + } int GetPageIndex(); void LoadFXAnnots(); @@ -341,11 +344,11 @@ class CPDFSDK_PageView final { private: void PageView_OnHighlightFormFields(CFX_RenderDevice* pDevice, CPDFSDK_Widget* pWidget); + CPDF_Matrix m_curMatrix; CPDF_Page* m_page; - CPDF_AnnotList* m_pAnnotList; - // CPDFSDK_Annot* m_pFocusAnnot; - CFX_PtrArray m_fxAnnotArray; + nonstd::unique_ptr<CPDF_AnnotList> m_pAnnotList; + std::vector<CPDFSDK_Annot*> m_fxAnnotArray; CPDFSDK_Document* m_pSDKDoc; CPDFSDK_Widget* m_CaptureWidget; FX_BOOL m_bEnterWidget; diff --git a/fpdfsdk/src/fsdk_annothandler.cpp b/fpdfsdk/src/fsdk_annothandler.cpp index bb999e1f08..b1b2e2cc76 100644 --- a/fpdfsdk/src/fsdk_annothandler.cpp +++ b/fpdfsdk/src/fsdk_annothandler.cpp @@ -4,6 +4,8 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include <algorithm> + #include "../include/fsdk_define.h" #include "../include/fsdk_mgr.h" #include "../include/formfiller/FFL_FormFiller.h" @@ -649,164 +651,45 @@ FX_BOOL CPDFSDK_BFAnnotHandler::HitTest(CPDFSDK_PageView* pPageView, return rect.Contains(point.x, point.y); } -// CReader_AnnotIteratorEx - CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView, - FX_BOOL bReverse, - FX_BOOL bIgnoreTopmost /*=FALSE*/, - FX_BOOL bCircle /*=FALSE*/, - CFX_PtrArray* pList /*=NULL*/) { - ASSERT(pPageView); - m_bReverse = bReverse; - m_bIgnoreTopmost = bIgnoreTopmost; - m_bCircle = bCircle; - m_pIteratorAnnotList.RemoveAll(); - InitIteratorAnnotList(pPageView, pList); -} - -CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot(const CPDFSDK_Annot* pCurrent) { - int index = -1; - int nCount = m_pIteratorAnnotList.GetSize(); - if (pCurrent) { - for (int i = 0; i < nCount; i++) { - CPDFSDK_Annot* pReaderAnnot = - (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i); - if (pReaderAnnot == pCurrent) { - index = i; - break; - } - } - } - return NextAnnot(index); -} -CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot(const CPDFSDK_Annot* pCurrent) { - int index = -1; - int nCount = m_pIteratorAnnotList.GetSize(); - if (pCurrent) { - for (int i = 0; i < nCount; i++) { - CPDFSDK_Annot* pReaderAnnot = - (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i); - if (pReaderAnnot == pCurrent) { - index = i; - break; - } - } - } - return PrevAnnot(index); -} -CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot(int& index) { - int nCount = m_pIteratorAnnotList.GetSize(); - if (nCount <= 0) - index = -1; - else { - if (index < 0) { - index = 0; - } else { - if (m_bCircle) { - index = (index < nCount - 1) ? (index + 1) : 0; - } else { - index = (index < nCount - 1) ? (index + 1) : -1; - } - } - } - return (index < 0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index); -} - -CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot(int& index) { - int nCount = m_pIteratorAnnotList.GetSize(); - if (nCount <= 0) - index = -1; - else { - if (index < 0) { - index = nCount - 1; - } else { - if (m_bCircle) { - index = (index > 0) ? (index - 1) : nCount - 1; - } else { - index = (index > 0) ? (index - 1) : -1; - } - } - } - return (index < 0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index); -} + bool bReverse) + : m_bReverse(bReverse), m_pos(0) { + const std::vector<CPDFSDK_Annot*>& annots = pPageView->GetAnnotList(); + m_iteratorAnnotList.insert(m_iteratorAnnotList.begin(), annots.rbegin(), + annots.rend()); + std::stable_sort(m_iteratorAnnotList.begin(), m_iteratorAnnotList.end(), + [](CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) { + return p1->GetLayoutOrder() < p2->GetLayoutOrder(); + }); -CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next(const CPDFSDK_Annot* pCurrent) { - return (m_bReverse) ? PrevAnnot(pCurrent) : NextAnnot(pCurrent); -} + CPDFSDK_Annot* pTopMostAnnot = pPageView->GetFocusAnnot(); + if (!pTopMostAnnot) + return; -CPDFSDK_Annot* CPDFSDK_AnnotIterator::Prev(const CPDFSDK_Annot* pCurrent) { - return (m_bReverse) ? NextAnnot(pCurrent) : PrevAnnot(pCurrent); + auto it = std::find(m_iteratorAnnotList.begin(), m_iteratorAnnotList.end(), + pTopMostAnnot); + if (it != m_iteratorAnnotList.end()) { + CPDFSDK_Annot* pReaderAnnot = *it; + m_iteratorAnnotList.erase(it); + m_iteratorAnnotList.insert(m_iteratorAnnotList.begin(), pReaderAnnot); + } } -CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next(int& index) { - return (m_bReverse) ? PrevAnnot(index) : NextAnnot(index); +CPDFSDK_AnnotIterator::~CPDFSDK_AnnotIterator() { } -CPDFSDK_Annot* CPDFSDK_AnnotIterator::Prev(int& index) { - return (m_bReverse) ? NextAnnot(index) : PrevAnnot(index); +CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot() { + if (m_pos < m_iteratorAnnotList.size()) + return m_iteratorAnnotList[m_pos++]; + return nullptr; } -void CPDFSDK_AnnotIterator::InsertSort(CFX_PtrArray& arrayList, - AI_COMPARE pCompare) { - for (int i = 1; i < arrayList.GetSize(); i++) { - if (pCompare((CPDFSDK_Annot*)(arrayList[i]), - (CPDFSDK_Annot*)(arrayList[i - 1])) < 0) { - int j = i - 1; - CPDFSDK_Annot* pTemp = (CPDFSDK_Annot*)arrayList[i]; - - do { - arrayList[j + 1] = arrayList[j]; - } while (--j >= 0 && pCompare(pTemp, (CPDFSDK_Annot*)arrayList[j]) < 0); - - arrayList[j + 1] = pTemp; - } - } -} - -int LyOrderCompare(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) { - if (p1->GetLayoutOrder() < p2->GetLayoutOrder()) - return -1; - if (p1->GetLayoutOrder() > p2->GetLayoutOrder()) - return 1; - return 0; +CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot() { + if (m_pos < m_iteratorAnnotList.size()) + return m_iteratorAnnotList[m_iteratorAnnotList.size() - ++m_pos]; + return nullptr; } -FX_BOOL CPDFSDK_AnnotIterator::InitIteratorAnnotList( - CPDFSDK_PageView* pPageView, - CFX_PtrArray* pAnnotList) { - ASSERT(pPageView); - - if (pAnnotList == NULL) { - pAnnotList = pPageView->GetAnnotList(); - } - - m_pIteratorAnnotList.RemoveAll(); - if (!pAnnotList) - return FALSE; - - CPDFSDK_Annot* pTopMostAnnot = - (m_bIgnoreTopmost) ? NULL : pPageView->GetFocusAnnot(); - - int nCount = pAnnotList->GetSize(); - - for (int i = nCount - 1; i >= 0; i--) { - CPDFSDK_Annot* pReaderAnnot = (CPDFSDK_Annot*)pAnnotList->GetAt(i); - m_pIteratorAnnotList.Add(pReaderAnnot); - } - - InsertSort(m_pIteratorAnnotList, &LyOrderCompare); - - if (pTopMostAnnot) { - for (int i = 0; i < nCount; i++) { - CPDFSDK_Annot* pReaderAnnot = - (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i); - if (pReaderAnnot == pTopMostAnnot) { - m_pIteratorAnnotList.RemoveAt(i); - m_pIteratorAnnotList.InsertAt(0, pReaderAnnot); - break; - } - } - } - - return TRUE; +CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next() { + return m_bReverse ? PrevAnnot() : NextAnnot(); } diff --git a/fpdfsdk/src/fsdk_baseform.cpp b/fpdfsdk/src/fsdk_baseform.cpp index e083220fe2..fad28e7b67 100644 --- a/fpdfsdk/src/fsdk_baseform.cpp +++ b/fpdfsdk/src/fsdk_baseform.cpp @@ -2431,7 +2431,7 @@ void CBA_AnnotIterator::GenerateResults() { switch (m_nTabs) { case BAI_STRUCTURE: { - for (int i = 0, sz = m_pPageView->CountAnnots(); i < sz; i++) { + for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); ASSERT(pAnnot != NULL); @@ -2443,7 +2443,7 @@ void CBA_AnnotIterator::GenerateResults() { CPDFSDK_SortAnnots sa; { - for (int i = 0, sz = m_pPageView->CountAnnots(); i < sz; i++) { + for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); ASSERT(pAnnot != NULL); @@ -2522,7 +2522,7 @@ void CBA_AnnotIterator::GenerateResults() { CPDFSDK_SortAnnots sa; { - for (int i = 0, sz = m_pPageView->CountAnnots(); i < sz; i++) { + for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); ASSERT(pAnnot != NULL); diff --git a/fpdfsdk/src/fsdk_mgr.cpp b/fpdfsdk/src/fsdk_mgr.cpp index f43a45b1f7..53a774edcd 100644 --- a/fpdfsdk/src/fsdk_mgr.cpp +++ b/fpdfsdk/src/fsdk_mgr.cpp @@ -4,6 +4,8 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +#include <algorithm> + #include "../../public/fpdf_ext.h" #include "../../third_party/base/nonstd_unique_ptr.h" #include "../include/formfiller/FFL_FormFiller.h" @@ -19,7 +21,8 @@ class CFX_SystemHandler : public IFX_SystemHandler { public: - CFX_SystemHandler(CPDFDoc_Environment* pEnv) : m_pEnv(pEnv), m_nCharSet(-1) {} + explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv) + : m_pEnv(pEnv), m_nCharSet(-1) {} ~CFX_SystemHandler() override {} public: @@ -606,47 +609,39 @@ CFX_WideString CPDFSDK_Document::GetPath() { } CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc, CPDF_Page* page) - : m_page(page), m_pSDKDoc(pSDKDoc) { + : m_page(page), + m_pSDKDoc(pSDKDoc), + m_CaptureWidget(nullptr), + m_bEnterWidget(FALSE), + m_bExitWidget(FALSE), + m_bOnWidget(FALSE), + m_bValid(FALSE), + m_bLocked(FALSE), + m_bTakeOverPage(FALSE) { CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm(); if (pInterForm) { CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm(); pPDFInterForm->FixPageFields(page); } - m_page->SetPrivateData((void*)m_page, (void*)this, NULL); - m_fxAnnotArray.RemoveAll(); - - m_bEnterWidget = FALSE; - m_bExitWidget = FALSE; - m_bOnWidget = FALSE; - m_CaptureWidget = NULL; - m_bValid = FALSE; - m_bLocked = FALSE; - m_bTakeOverPage = FALSE; + m_page->SetPrivateData((void*)m_page, (void*)this, nullptr); } CPDFSDK_PageView::~CPDFSDK_PageView() { // if there is a focused annot on the page, we should kill the focus first. if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) { - for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) { - CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i); - if (pAnnot == focusedAnnot) { - KillFocusAnnot(); - break; - } - } + auto it = + std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot); + if (it != m_fxAnnotArray.end()) + KillFocusAnnot(); } CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); - ASSERT(pAnnotHandlerMgr); - for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) { - CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i); + for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) pAnnotHandlerMgr->ReleaseAnnot(pAnnot); - } - m_fxAnnotArray.RemoveAll(); + m_fxAnnotArray.clear(); - delete m_pAnnotList; - m_pAnnotList = NULL; + m_pAnnotList.reset(); m_page->RemovePrivateData((void*)m_page); if (m_bTakeOverPage) { @@ -660,9 +655,8 @@ void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice, m_curMatrix = *pUser2Device; CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); - CPDFSDK_AnnotIterator annotIterator(this, TRUE); - int index = -1; - while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next(index)) { + CPDFSDK_AnnotIterator annotIterator(this, true); + while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) { CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0); } @@ -670,7 +664,7 @@ void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice, CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY) { - int nCount = m_pAnnotList->Count(); + int nCount = CountAnnots(); for (int i = 0; i < nCount; i++) { CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i); CFX_FloatRect annotRect; @@ -678,13 +672,13 @@ CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX, if (annotRect.Contains(pageX, pageY)) return pAnnot; } - return NULL; + return nullptr; } CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY) { - int nCount = m_pAnnotList->Count(); - for (int i = 0; i < nCount; i++) { + int nCount = CountAnnots(); + for (int i = 0; i < nCount; ++i) { CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i); if (pAnnot->GetSubType() == "Widget") { CFX_FloatRect annotRect; @@ -693,33 +687,29 @@ CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX, return pAnnot; } } - return NULL; + return nullptr; } CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY) { - CPDFSDK_AnnotIterator annotIterator(this, FALSE); CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr(); - CPDFSDK_Annot* pSDKAnnot = NULL; - int index = -1; - while ((pSDKAnnot = annotIterator.Next(index))) { + CPDFSDK_AnnotIterator annotIterator(this, false); + while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) { CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot); if (rc.Contains(pageX, pageY)) return pSDKAnnot; } - return NULL; + return nullptr; } CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY) { - CPDFSDK_AnnotIterator annotIterator(this, FALSE); CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr(); - CPDFSDK_Annot* pSDKAnnot = NULL; - int index = -1; - while ((pSDKAnnot = annotIterator.Next(index))) { + CPDFSDK_AnnotIterator annotIterator(this, false); + while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) { if (pSDKAnnot->GetType() == "Widget") { pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot); CPDF_Point point(pageX, pageY); @@ -728,7 +718,7 @@ CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX, } } - return NULL; + return nullptr; } FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) { @@ -742,21 +732,15 @@ CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) { CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); ASSERT(pEnv); CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr(); + if (!pAnnotHandler) + return nullptr; - CPDFSDK_Annot* pSDKAnnot = NULL; - - if (pAnnotHandler) { - pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this); - } + CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this); if (!pSDKAnnot) - return NULL; - - m_fxAnnotArray.Add(pSDKAnnot); - - if (pAnnotHandler) { - pAnnotHandler->Annot_OnCreate(pSDKAnnot); - } + return nullptr; + m_fxAnnotArray.push_back(pSDKAnnot); + pAnnotHandler->Annot_OnCreate(pSDKAnnot); return pSDKAnnot; } @@ -780,27 +764,20 @@ CPDF_Document* CPDFSDK_PageView::GetPDFDocument() { return NULL; } -int CPDFSDK_PageView::CountAnnots() { +int CPDFSDK_PageView::CountAnnots() const { return m_pAnnotList->Count(); } -CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(int nIndex) { - int nCount = m_fxAnnotArray.GetSize(); - if (nIndex < 0 || nIndex >= nCount) { - return NULL; - } - - return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex); +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) { - int nCount = m_fxAnnotArray.GetSize(); - for (int i = 0; i < nCount; i++) { - CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i); - if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict()) + for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) { + if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict) return pAnnot; } - return NULL; + return nullptr; } FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point, @@ -920,11 +897,11 @@ void CPDFSDK_PageView::LoadFXAnnots() { FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled(); // Disable the default AP construction. CPDF_InterForm::EnableUpdateAP(FALSE); - m_pAnnotList = new CPDF_AnnotList(m_page); + m_pAnnotList.reset(new CPDF_AnnotList(m_page)); CPDF_InterForm::EnableUpdateAP(enableAPUpdate); - int nCount = m_pAnnotList->Count(); + int nCount = CountAnnots(); SetLock(TRUE); - for (int i = 0; i < nCount; i++) { + for (int i = 0; i < nCount; ++i) { CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i); CPDF_Document* pDoc = GetPDFDocument(); @@ -937,7 +914,7 @@ void CPDFSDK_PageView::LoadFXAnnots() { CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this); if (!pAnnot) continue; - m_fxAnnotArray.Add(pAnnot); + m_fxAnnotArray.push_back(pAnnot); pAnnotHandlerMgr->Annot_OnLoad(pAnnot); } @@ -971,11 +948,12 @@ int CPDFSDK_PageView::GetPageIndex() { return -1; } -FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) { - if (p == NULL) +FX_BOOL CPDFSDK_PageView::IsValidAnnot(CPDF_Annot* p) const { + if (!p) return FALSE; - int iCount = m_pAnnotList->Count(); - for (int i = 0; i < iCount; i++) { + + int nCount = CountAnnots(); + for (int i = 0; i < nCount; ++i) { if (m_pAnnotList->GetAt(i) == p) return TRUE; } @@ -985,12 +963,11 @@ FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) { CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() { CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot(); if (!pFocusAnnot) - return NULL; + return nullptr; - for (int i = 0; i < m_fxAnnotArray.GetSize(); i++) { - CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i); + for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) { if (pAnnot == pFocusAnnot) return pAnnot; } - return NULL; + return nullptr; } |