1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../include/fsdk_define.h"
8 #include "../include/fsdk_mgr.h"
9 #include "../include/formfiller/FFL_FormFiller.h"
10 #include "../include/fsdk_annothandler.h"
12 CPDFSDK_AnnotHandlerMgr::CPDFSDK_AnnotHandlerMgr(CPDFDoc_Environment* pApp) {
15 CPDFSDK_AnnotHandler* pHandler = new CPDFSDK_AnnotHandler(m_pApp);
16 pHandler->SetFormFiller(m_pApp->GetIFormFiller());
17 RegisterAnnotHandler(pHandler);
20 CPDFSDK_AnnotHandlerMgr::~CPDFSDK_AnnotHandlerMgr() {
21 for (int i = 0; i < m_Handlers.GetSize(); i++) {
22 CPDFSDK_AnnotHandler* pHandler = m_Handlers.GetAt(i);
25 m_Handlers.RemoveAll();
26 m_mapType2Handler.clear();
29 void CPDFSDK_AnnotHandlerMgr::RegisterAnnotHandler(
30 CPDFSDK_AnnotHandler* pAnnotHandler) {
31 ASSERT(!GetAnnotHandler(pAnnotHandler->GetType()));
33 m_Handlers.Add(pAnnotHandler);
34 m_mapType2Handler[pAnnotHandler->GetType()] = pAnnotHandler;
37 void CPDFSDK_AnnotHandlerMgr::UnRegisterAnnotHandler(
38 CPDFSDK_AnnotHandler* pAnnotHandler) {
39 m_mapType2Handler.erase(pAnnotHandler->GetType());
40 for (int i = 0, sz = m_Handlers.GetSize(); i < sz; i++) {
41 if (m_Handlers.GetAt(i) == pAnnotHandler) {
42 m_Handlers.RemoveAt(i);
48 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CPDF_Annot* pAnnot,
49 CPDFSDK_PageView* pPageView) {
50 ASSERT(pAnnot != NULL);
51 ASSERT(pPageView != NULL);
53 if (CPDFSDK_AnnotHandler* pAnnotHandler =
54 GetAnnotHandler(pAnnot->GetSubType())) {
55 return pAnnotHandler->NewAnnot(pAnnot, pPageView);
58 return new CPDFSDK_Annot(pAnnot, pPageView);
61 void CPDFSDK_AnnotHandlerMgr::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
62 ASSERT(pAnnot != NULL);
66 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
67 pAnnotHandler->OnRelease(pAnnot);
68 pAnnotHandler->ReleaseAnnot(pAnnot);
70 delete (CPDFSDK_Annot*)pAnnot;
74 void CPDFSDK_AnnotHandlerMgr::Annot_OnCreate(CPDFSDK_Annot* pAnnot) {
75 ASSERT(pAnnot != NULL);
77 CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
79 CPDFSDK_DateTime curTime;
80 pPDFAnnot->GetAnnotDict()->SetAtString("M", curTime.ToPDFDateTimeString());
81 pPDFAnnot->GetAnnotDict()->SetAtNumber("F", 0);
83 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
84 pAnnotHandler->OnCreate(pAnnot);
88 void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot) {
89 ASSERT(pAnnot != NULL);
91 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
92 pAnnotHandler->OnLoad(pAnnot);
96 CPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
97 CPDFSDK_Annot* pAnnot) const {
98 ASSERT(pAnnot != NULL);
100 CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
101 ASSERT(pPDFAnnot != NULL);
103 return GetAnnotHandler(pPDFAnnot->GetSubType());
106 CPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(
107 const CFX_ByteString& sType) const {
108 auto it = m_mapType2Handler.find(sType);
109 return it != m_mapType2Handler.end() ? it->second : nullptr;
112 void CPDFSDK_AnnotHandlerMgr::Annot_OnDraw(CPDFSDK_PageView* pPageView,
113 CPDFSDK_Annot* pAnnot,
114 CFX_RenderDevice* pDevice,
115 CPDF_Matrix* pUser2Device,
117 ASSERT(pAnnot != NULL);
119 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
120 pAnnotHandler->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
122 pAnnot->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
126 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown(
127 CPDFSDK_PageView* pPageView,
128 CPDFSDK_Annot* pAnnot,
130 const CPDF_Point& point) {
131 ASSERT(pAnnot != NULL);
133 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
134 return pAnnotHandler->OnLButtonDown(pPageView, pAnnot, nFlags, point);
138 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp(CPDFSDK_PageView* pPageView,
139 CPDFSDK_Annot* pAnnot,
141 const CPDF_Point& point) {
142 ASSERT(pAnnot != NULL);
144 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
145 return pAnnotHandler->OnLButtonUp(pPageView, pAnnot, nFlags, point);
149 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk(
150 CPDFSDK_PageView* pPageView,
151 CPDFSDK_Annot* pAnnot,
153 const CPDF_Point& point) {
154 ASSERT(pAnnot != NULL);
156 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
157 return pAnnotHandler->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
161 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove(CPDFSDK_PageView* pPageView,
162 CPDFSDK_Annot* pAnnot,
164 const CPDF_Point& point) {
165 ASSERT(pAnnot != NULL);
167 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
168 return pAnnotHandler->OnMouseMove(pPageView, pAnnot, nFlags, point);
172 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel(CPDFSDK_PageView* pPageView,
173 CPDFSDK_Annot* pAnnot,
176 const CPDF_Point& point) {
177 ASSERT(pAnnot != NULL);
179 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
180 return pAnnotHandler->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,
185 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown(
186 CPDFSDK_PageView* pPageView,
187 CPDFSDK_Annot* pAnnot,
189 const CPDF_Point& point) {
190 ASSERT(pAnnot != NULL);
192 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
193 return pAnnotHandler->OnRButtonDown(pPageView, pAnnot, nFlags, point);
197 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp(CPDFSDK_PageView* pPageView,
198 CPDFSDK_Annot* pAnnot,
200 const CPDF_Point& point) {
201 ASSERT(pAnnot != NULL);
203 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
204 return pAnnotHandler->OnRButtonUp(pPageView, pAnnot, nFlags, point);
209 void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseEnter(CPDFSDK_PageView* pPageView,
210 CPDFSDK_Annot* pAnnot,
212 ASSERT(pAnnot != NULL);
214 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
215 pAnnotHandler->OnMouseEnter(pPageView, pAnnot, nFlag);
220 void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseExit(CPDFSDK_PageView* pPageView,
221 CPDFSDK_Annot* pAnnot,
223 ASSERT(pAnnot != NULL);
225 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
226 pAnnotHandler->OnMouseExit(pPageView, pAnnot, nFlag);
231 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChar(CPDFSDK_Annot* pAnnot,
234 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
235 return pAnnotHandler->OnChar(pAnnot, nChar, nFlags);
240 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot,
243 if (!m_pApp->FFI_IsCTRLKeyDown(nFlag) && !m_pApp->FFI_IsALTKeyDown(nFlag)) {
244 CPDFSDK_PageView* pPage = pAnnot->GetPageView();
245 CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot();
246 if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab)) {
247 CPDFSDK_Annot* pNext =
248 GetNextAnnot(pFocusAnnot, !m_pApp->FFI_IsSHIFTKeyDown(nFlag));
250 if (pNext && pNext != pFocusAnnot) {
251 CPDFSDK_Document* pDocument = pPage->GetSDKDocument();
252 pDocument->SetFocusAnnot(pNext);
258 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
259 return pAnnotHandler->OnKeyDown(pAnnot, nKeyCode, nFlag);
263 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot,
269 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(CPDFSDK_Annot* pAnnot,
271 ASSERT(pAnnot != NULL);
273 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
274 if (pAnnotHandler->OnSetFocus(pAnnot, nFlag)) {
275 CPDFSDK_PageView* pPage = pAnnot->GetPageView();
276 pPage->GetSDKDocument();
283 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(CPDFSDK_Annot* pAnnot,
286 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
287 return pAnnotHandler->OnKillFocus(pAnnot, nFlag);
292 CPDF_Rect CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox(
293 CPDFSDK_PageView* pPageView,
294 CPDFSDK_Annot* pAnnot) {
296 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
297 return pAnnotHandler->GetViewBBox(pPageView, pAnnot);
299 return pAnnot->GetRect();
302 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView* pPageView,
303 CPDFSDK_Annot* pAnnot,
304 const CPDF_Point& point) {
306 if (CPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot)) {
307 if (pAnnotHandler->CanAnswer(pAnnot))
308 return pAnnotHandler->HitTest(pPageView, pAnnot, point);
313 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,
315 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), "Widget", "");
316 return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
319 FX_BOOL CPDFSDK_AnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
320 ASSERT(pAnnot->GetType() == "Widget");
321 if (pAnnot->GetSubType() == BFFT_SIGNATURE)
324 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
325 if (!pWidget->IsVisible())
328 int nFieldFlags = pWidget->GetFieldFlags();
329 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
332 if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
335 CPDF_Page* pPage = pWidget->GetPDFPage();
336 CPDF_Document* pDocument = pPage->m_pDocument;
337 FX_DWORD dwPermissions = pDocument->GetUserPermissions();
338 return (dwPermissions & FPDFPERM_FILL_FORM) ||
339 (dwPermissions & FPDFPERM_ANNOT_FORM);
342 CPDFSDK_Annot* CPDFSDK_AnnotHandler::NewAnnot(CPDF_Annot* pAnnot,
343 CPDFSDK_PageView* pPage) {
344 CPDFSDK_Document* pSDKDoc = m_pApp->GetSDKDocument();
345 CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pSDKDoc->GetInterForm();
346 CPDF_FormControl* pCtrl = CPDFSDK_Widget::GetFormControl(
347 pInterForm->GetInterForm(), pAnnot->GetAnnotDict());
351 CPDFSDK_Widget* pWidget = new CPDFSDK_Widget(pAnnot, pPage, pInterForm);
352 pInterForm->AddMap(pCtrl, pWidget);
353 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
354 if (pPDFInterForm && pPDFInterForm->NeedConstructAP())
355 pWidget->ResetAppearance(nullptr, FALSE);
360 void CPDFSDK_AnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
361 ASSERT(pAnnot != NULL);
364 m_pFormFiller->OnDelete(pAnnot);
366 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
367 CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
368 ASSERT(pInterForm != NULL);
370 CPDF_FormControl* pCtrol = pWidget->GetFormControl();
371 pInterForm->RemoveMap(pCtrol);
376 void CPDFSDK_AnnotHandler::OnDraw(CPDFSDK_PageView* pPageView,
377 CPDFSDK_Annot* pAnnot,
378 CFX_RenderDevice* pDevice,
379 CPDF_Matrix* pUser2Device,
381 ASSERT(pAnnot != NULL);
382 CFX_ByteString sSubType = pAnnot->GetSubType();
384 if (sSubType == BFFT_SIGNATURE) {
385 pAnnot->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
388 m_pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
393 void CPDFSDK_AnnotHandler::OnMouseEnter(CPDFSDK_PageView* pPageView,
394 CPDFSDK_Annot* pAnnot,
396 ASSERT(pAnnot != NULL);
397 CFX_ByteString sSubType = pAnnot->GetSubType();
399 if (sSubType == BFFT_SIGNATURE) {
402 m_pFormFiller->OnMouseEnter(pPageView, pAnnot, nFlag);
405 void CPDFSDK_AnnotHandler::OnMouseExit(CPDFSDK_PageView* pPageView,
406 CPDFSDK_Annot* pAnnot,
408 ASSERT(pAnnot != NULL);
409 CFX_ByteString sSubType = pAnnot->GetSubType();
411 if (sSubType == BFFT_SIGNATURE) {
414 m_pFormFiller->OnMouseExit(pPageView, pAnnot, nFlag);
417 FX_BOOL CPDFSDK_AnnotHandler::OnLButtonDown(CPDFSDK_PageView* pPageView,
418 CPDFSDK_Annot* pAnnot,
420 const CPDF_Point& point) {
421 ASSERT(pAnnot != NULL);
422 CFX_ByteString sSubType = pAnnot->GetSubType();
424 if (sSubType == BFFT_SIGNATURE) {
427 return m_pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
433 FX_BOOL CPDFSDK_AnnotHandler::OnLButtonUp(CPDFSDK_PageView* pPageView,
434 CPDFSDK_Annot* pAnnot,
436 const CPDF_Point& point) {
437 ASSERT(pAnnot != NULL);
438 CFX_ByteString sSubType = pAnnot->GetSubType();
440 if (sSubType == BFFT_SIGNATURE) {
443 return m_pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
449 FX_BOOL CPDFSDK_AnnotHandler::OnLButtonDblClk(CPDFSDK_PageView* pPageView,
450 CPDFSDK_Annot* pAnnot,
452 const CPDF_Point& point) {
453 ASSERT(pAnnot != NULL);
454 CFX_ByteString sSubType = pAnnot->GetSubType();
456 if (sSubType == BFFT_SIGNATURE) {
459 return m_pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
465 FX_BOOL CPDFSDK_AnnotHandler::OnMouseMove(CPDFSDK_PageView* pPageView,
466 CPDFSDK_Annot* pAnnot,
468 const CPDF_Point& point) {
469 ASSERT(pAnnot != NULL);
470 CFX_ByteString sSubType = pAnnot->GetSubType();
472 if (sSubType == BFFT_SIGNATURE) {
475 return m_pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
481 FX_BOOL CPDFSDK_AnnotHandler::OnMouseWheel(CPDFSDK_PageView* pPageView,
482 CPDFSDK_Annot* pAnnot,
485 const CPDF_Point& point) {
486 ASSERT(pAnnot != NULL);
487 CFX_ByteString sSubType = pAnnot->GetSubType();
489 if (sSubType == BFFT_SIGNATURE) {
492 return m_pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,
499 FX_BOOL CPDFSDK_AnnotHandler::OnRButtonDown(CPDFSDK_PageView* pPageView,
500 CPDFSDK_Annot* pAnnot,
502 const CPDF_Point& point) {
503 ASSERT(pAnnot != NULL);
504 CFX_ByteString sSubType = pAnnot->GetSubType();
506 if (sSubType == BFFT_SIGNATURE) {
509 return m_pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
514 FX_BOOL CPDFSDK_AnnotHandler::OnRButtonUp(CPDFSDK_PageView* pPageView,
515 CPDFSDK_Annot* pAnnot,
517 const CPDF_Point& point) {
518 ASSERT(pAnnot != NULL);
519 CFX_ByteString sSubType = pAnnot->GetSubType();
521 if (sSubType == BFFT_SIGNATURE) {
524 return m_pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
530 FX_BOOL CPDFSDK_AnnotHandler::OnChar(CPDFSDK_Annot* pAnnot,
533 ASSERT(pAnnot != NULL);
534 CFX_ByteString sSubType = pAnnot->GetSubType();
536 if (sSubType == BFFT_SIGNATURE) {
539 return m_pFormFiller->OnChar(pAnnot, nChar, nFlags);
545 FX_BOOL CPDFSDK_AnnotHandler::OnKeyDown(CPDFSDK_Annot* pAnnot,
548 ASSERT(pAnnot != NULL);
549 CFX_ByteString sSubType = pAnnot->GetSubType();
551 if (sSubType == BFFT_SIGNATURE) {
554 return m_pFormFiller->OnKeyDown(pAnnot, nKeyCode, nFlag);
560 FX_BOOL CPDFSDK_AnnotHandler::OnKeyUp(CPDFSDK_Annot* pAnnot,
565 void CPDFSDK_AnnotHandler::OnCreate(CPDFSDK_Annot* pAnnot) {
566 ASSERT(pAnnot != NULL);
567 CFX_ByteString sSubType = pAnnot->GetSubType();
569 if (sSubType == BFFT_SIGNATURE) {
572 m_pFormFiller->OnCreate(pAnnot);
576 void CPDFSDK_AnnotHandler::OnLoad(CPDFSDK_Annot* pAnnot) {
577 ASSERT(pAnnot != NULL);
579 CFX_ByteString sSubType = pAnnot->GetSubType();
581 if (sSubType == BFFT_SIGNATURE) {
583 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
584 if (!pWidget->IsAppearanceValid())
585 pWidget->ResetAppearance(NULL, FALSE);
587 int nFieldType = pWidget->GetFieldType();
588 if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX) {
589 FX_BOOL bFormated = FALSE;
590 CFX_WideString sValue = pWidget->OnFormat(bFormated);
591 if (bFormated && nFieldType == FIELDTYPE_COMBOBOX) {
592 pWidget->ResetAppearance(sValue.c_str(), FALSE);
597 m_pFormFiller->OnLoad(pAnnot);
601 FX_BOOL CPDFSDK_AnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot,
603 ASSERT(pAnnot != NULL);
604 CFX_ByteString sSubType = pAnnot->GetSubType();
606 if (sSubType == BFFT_SIGNATURE) {
609 return m_pFormFiller->OnSetFocus(pAnnot, nFlag);
614 FX_BOOL CPDFSDK_AnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot,
616 ASSERT(pAnnot != NULL);
617 CFX_ByteString sSubType = pAnnot->GetSubType();
619 if (sSubType == BFFT_SIGNATURE) {
622 return m_pFormFiller->OnKillFocus(pAnnot, nFlag);
628 CPDF_Rect CPDFSDK_AnnotHandler::GetViewBBox(CPDFSDK_PageView* pPageView,
629 CPDFSDK_Annot* pAnnot) {
630 ASSERT(pAnnot != NULL);
631 CFX_ByteString sSubType = pAnnot->GetSubType();
633 if (sSubType == BFFT_SIGNATURE) {
636 return m_pFormFiller->GetViewBBox(pPageView, pAnnot);
639 return CPDF_Rect(0, 0, 0, 0);
642 FX_BOOL CPDFSDK_AnnotHandler::HitTest(CPDFSDK_PageView* pPageView,
643 CPDFSDK_Annot* pAnnot,
644 const CPDF_Point& point) {
648 CPDF_Rect rect = GetViewBBox(pPageView, pAnnot);
649 return rect.Contains(point.x, point.y);
652 // CReader_AnnotIteratorEx
654 CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView,
656 FX_BOOL bIgnoreTopmost /*=FALSE*/,
657 FX_BOOL bCircle /*=FALSE*/,
658 CFX_PtrArray* pList /*=NULL*/) {
660 m_bReverse = bReverse;
661 m_bIgnoreTopmost = bIgnoreTopmost;
663 m_pIteratorAnnotList.RemoveAll();
664 InitIteratorAnnotList(pPageView, pList);
667 CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot(const CPDFSDK_Annot* pCurrent) {
669 int nCount = m_pIteratorAnnotList.GetSize();
671 for (int i = 0; i < nCount; i++) {
672 CPDFSDK_Annot* pReaderAnnot =
673 (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
674 if (pReaderAnnot == pCurrent) {
680 return NextAnnot(index);
682 CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot(const CPDFSDK_Annot* pCurrent) {
684 int nCount = m_pIteratorAnnotList.GetSize();
686 for (int i = 0; i < nCount; i++) {
687 CPDFSDK_Annot* pReaderAnnot =
688 (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
689 if (pReaderAnnot == pCurrent) {
695 return PrevAnnot(index);
697 CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot(int& index) {
698 int nCount = m_pIteratorAnnotList.GetSize();
706 index = (index < nCount - 1) ? (index + 1) : 0;
708 index = (index < nCount - 1) ? (index + 1) : -1;
712 return (index < 0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);
715 CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot(int& index) {
716 int nCount = m_pIteratorAnnotList.GetSize();
724 index = (index > 0) ? (index - 1) : nCount - 1;
726 index = (index > 0) ? (index - 1) : -1;
730 return (index < 0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);
733 CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next(const CPDFSDK_Annot* pCurrent) {
734 return (m_bReverse) ? PrevAnnot(pCurrent) : NextAnnot(pCurrent);
737 CPDFSDK_Annot* CPDFSDK_AnnotIterator::Prev(const CPDFSDK_Annot* pCurrent) {
738 return (m_bReverse) ? NextAnnot(pCurrent) : PrevAnnot(pCurrent);
741 CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next(int& index) {
742 return (m_bReverse) ? PrevAnnot(index) : NextAnnot(index);
745 CPDFSDK_Annot* CPDFSDK_AnnotIterator::Prev(int& index) {
746 return (m_bReverse) ? NextAnnot(index) : PrevAnnot(index);
749 void CPDFSDK_AnnotIterator::InsertSort(CFX_PtrArray& arrayList,
750 AI_COMPARE pCompare) {
751 for (int i = 1; i < arrayList.GetSize(); i++) {
752 if (pCompare((CPDFSDK_Annot*)(arrayList[i]),
753 (CPDFSDK_Annot*)(arrayList[i - 1])) < 0) {
755 CPDFSDK_Annot* pTemp = (CPDFSDK_Annot*)arrayList[i];
758 arrayList[j + 1] = arrayList[j];
759 } while (--j >= 0 && pCompare(pTemp, (CPDFSDK_Annot*)arrayList[j]) < 0);
761 arrayList[j + 1] = pTemp;
766 int LyOrderCompare(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) {
767 if (p1->GetLayoutOrder() < p2->GetLayoutOrder())
769 if (p1->GetLayoutOrder() > p2->GetLayoutOrder())
774 FX_BOOL CPDFSDK_AnnotIterator::InitIteratorAnnotList(
775 CPDFSDK_PageView* pPageView,
776 CFX_PtrArray* pAnnotList) {
779 if (pAnnotList == NULL) {
780 pAnnotList = pPageView->GetAnnotList();
783 m_pIteratorAnnotList.RemoveAll();
787 CPDFSDK_Annot* pTopMostAnnot =
788 (m_bIgnoreTopmost) ? NULL : pPageView->GetFocusAnnot();
790 int nCount = pAnnotList->GetSize();
792 for (int i = nCount - 1; i >= 0; i--) {
793 CPDFSDK_Annot* pReaderAnnot = (CPDFSDK_Annot*)pAnnotList->GetAt(i);
794 m_pIteratorAnnotList.Add(pReaderAnnot);
797 InsertSort(m_pIteratorAnnotList, &LyOrderCompare);
800 for (int i = 0; i < nCount; i++) {
801 CPDFSDK_Annot* pReaderAnnot =
802 (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
803 if (pReaderAnnot == pTopMostAnnot) {
804 m_pIteratorAnnotList.RemoveAt(i);
805 m_pIteratorAnnotList.InsertAt(0, pReaderAnnot);