blob: aae903eeb1aced3a7646940b28ad02f88d3bce42 [file] [log] [blame]
dsinclairf34518b2016-09-13 12:03:48 -07001// Copyright 2016 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.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
dsinclair114e46a2016-09-29 17:18:21 -07007#include "fpdfsdk/cpdfsdk_pageview.h"
dsinclairf34518b2016-09-13 12:03:48 -07008
thestig7c292e02016-09-28 14:14:26 -07009#include <memory>
10#include <vector>
11
dsinclair488b7ad2016-10-04 11:55:50 -070012#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair69d9c682016-10-04 12:18:35 -070013#include "core/fpdfapi/render/cpdf_renderoptions.h"
dsinclair1727aee2016-09-29 13:12:56 -070014#include "core/fpdfdoc/cpdf_annotlist.h"
15#include "core/fpdfdoc/cpdf_interform.h"
dsinclair114e46a2016-09-29 17:18:21 -070016#include "fpdfsdk/cpdfsdk_annot.h"
17#include "fpdfsdk/cpdfsdk_annothandlermgr.h"
tsepezd805eec2017-01-11 14:03:54 -080018#include "fpdfsdk/cpdfsdk_annotiteration.h"
dsinclair735606d2016-10-05 15:47:02 -070019#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/cpdfsdk_interform.h"
tsepez36eb4bd2016-10-03 15:24:27 -070021#include "third_party/base/ptr_util.h"
dsinclairf34518b2016-09-13 12:03:48 -070022
23#ifdef PDF_ENABLE_XFA
dsinclair4d29e782016-10-04 14:02:47 -070024#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
dsinclair5b493092016-09-29 20:20:24 -070025#include "xfa/fxfa/xfa_ffdocview.h"
26#include "xfa/fxfa/xfa_ffpageview.h"
27#include "xfa/fxfa/xfa_ffwidgethandler.h"
28#include "xfa/fxfa/xfa_rendercontext.h"
dsinclair202ad722016-09-29 17:41:42 -070029#include "xfa/fxgraphics/cfx_graphics.h"
dsinclairf34518b2016-09-13 12:03:48 -070030#endif // PDF_ENABLE_XFA
31
dsinclairb402b172016-10-11 09:26:32 -070032CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_FormFillEnvironment* pFormFillEnv,
dsinclairf34518b2016-09-13 12:03:48 -070033 UnderlyingPageType* page)
34 : m_page(page),
dsinclairb402b172016-10-11 09:26:32 -070035 m_pFormFillEnv(pFormFillEnv),
dsinclairf34518b2016-09-13 12:03:48 -070036#ifndef PDF_ENABLE_XFA
37 m_bOwnsPage(false),
38#endif // PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -070039 m_bEnterWidget(false),
40 m_bExitWidget(false),
41 m_bOnWidget(false),
42 m_bValid(false),
43 m_bLocked(false),
dsinclairbcf46232016-10-03 13:02:27 -070044 m_bBeingDestroyed(false) {
dsinclair7cbe68e2016-10-12 11:56:23 -070045 CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
dsinclairf34518b2016-09-13 12:03:48 -070046 if (pInterForm) {
47 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
48#ifdef PDF_ENABLE_XFA
49 if (page->GetPDFPage())
50 pPDFInterForm->FixPageFields(page->GetPDFPage());
51#else // PDF_ENABLE_XFA
52 pPDFInterForm->FixPageFields(page);
53#endif // PDF_ENABLE_XFA
54 }
55#ifndef PDF_ENABLE_XFA
56 m_page->SetView(this);
57#endif // PDF_ENABLE_XFA
58}
59
60CPDFSDK_PageView::~CPDFSDK_PageView() {
61#ifndef PDF_ENABLE_XFA
62 // The call to |ReleaseAnnot| can cause the page pointed to by |m_page| to
63 // be freed, which will cause issues if we try to cleanup the pageview pointer
64 // in |m_page|. So, reset the pageview pointer before doing anything else.
65 m_page->SetView(nullptr);
66#endif // PDF_ENABLE_XFA
67
dsinclairb402b172016-10-11 09:26:32 -070068 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
69 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclair8afe15a2016-10-05 12:00:34 -070070 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray)
dsinclairf34518b2016-09-13 12:03:48 -070071 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
72
dsinclair8afe15a2016-10-05 12:00:34 -070073 m_SDKAnnotArray.clear();
dsinclairf34518b2016-09-13 12:03:48 -070074 m_pAnnotList.reset();
75
76#ifndef PDF_ENABLE_XFA
77 if (m_bOwnsPage)
78 delete m_page;
79#endif // PDF_ENABLE_XFA
80}
81
82void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
83 CFX_Matrix* pUser2Device,
84#ifdef PDF_ENABLE_XFA
85 CPDF_RenderOptions* pOptions,
86 const FX_RECT& pClip) {
87#else
88 CPDF_RenderOptions* pOptions) {
89#endif // PDF_ENABLE_XFA
90 m_curMatrix = *pUser2Device;
dsinclairf34518b2016-09-13 12:03:48 -070091
92#ifdef PDF_ENABLE_XFA
93 CPDFXFA_Page* pPage = GetPDFXFAPage();
94 if (!pPage)
95 return;
96
dsinclair521b7502016-11-02 13:02:28 -070097 if (pPage->GetContext()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
dsinclairf34518b2016-09-13 12:03:48 -070098 CFX_Graphics gs;
99 gs.Create(pDevice);
Dan Sinclairbba2a7c2017-02-07 16:36:39 -0500100 CFX_RectF rectClip(static_cast<FX_FLOAT>(pClip.left),
101 static_cast<FX_FLOAT>(pClip.top),
102 static_cast<FX_FLOAT>(pClip.Width()),
103 static_cast<FX_FLOAT>(pClip.Height()));
dsinclairf34518b2016-09-13 12:03:48 -0700104 gs.SetClipRect(rectClip);
105 std::unique_ptr<CXFA_RenderContext> pRenderContext(new CXFA_RenderContext);
106 CXFA_RenderOptions renderOptions;
tsepez4cf55152016-11-02 14:37:54 -0700107 renderOptions.m_bHighlight = true;
dsinclairf34518b2016-09-13 12:03:48 -0700108 CXFA_FFPageView* xfaView = pPage->GetXFAPageView();
109 pRenderContext->StartRender(xfaView, &gs, *pUser2Device, renderOptions);
110 pRenderContext->DoRender();
111 pRenderContext->StopRender();
112 CXFA_FFDocView* docView = xfaView->GetDocView();
113 if (!docView)
114 return;
115 CPDFSDK_Annot* annot = GetFocusAnnot();
116 if (!annot)
117 return;
118 // Render the focus widget
119 docView->GetWidgetHandler()->RenderWidget(annot->GetXFAWidget(), &gs,
tsepez4cf55152016-11-02 14:37:54 -0700120 pUser2Device, false);
dsinclairf34518b2016-09-13 12:03:48 -0700121 return;
122 }
123#endif // PDF_ENABLE_XFA
124
125 // for pdf/static xfa.
tsepezd805eec2017-01-11 14:03:54 -0800126 CPDFSDK_AnnotIteration annotIteration(this, true);
127 for (const auto& pSDKAnnot : annotIteration) {
128 m_pFormFillEnv->GetAnnotHandlerMgr()->Annot_OnDraw(
129 this, pSDKAnnot.Get(), pDevice, pUser2Device, pOptions->m_bDrawAnnots);
dsinclairf34518b2016-09-13 12:03:48 -0700130 }
131}
132
dsinclairf34518b2016-09-13 12:03:48 -0700133CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
134 FX_FLOAT pageY) {
dsinclairb402b172016-10-11 09:26:32 -0700135 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800136 CPDFSDK_AnnotIteration annotIteration(this, false);
137 for (const auto& pSDKAnnot : annotIteration) {
138 CFX_FloatRect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
dsinclairf34518b2016-09-13 12:03:48 -0700139 if (pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::POPUP)
140 continue;
141 if (rc.Contains(pageX, pageY))
tsepezd805eec2017-01-11 14:03:54 -0800142 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700143 }
dsinclairf34518b2016-09-13 12:03:48 -0700144 return nullptr;
145}
146
147CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
148 FX_FLOAT pageY) {
dsinclairb402b172016-10-11 09:26:32 -0700149 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800150 CPDFSDK_AnnotIteration annotIteration(this, false);
151 for (const auto& pSDKAnnot : annotIteration) {
dsinclairf34518b2016-09-13 12:03:48 -0700152 bool bHitTest = pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET;
153#ifdef PDF_ENABLE_XFA
154 bHitTest = bHitTest ||
155 pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::XFAWIDGET;
156#endif // PDF_ENABLE_XFA
157 if (bHitTest) {
tsepezd805eec2017-01-11 14:03:54 -0800158 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
dsinclairf34518b2016-09-13 12:03:48 -0700159 CFX_FloatPoint point(pageX, pageY);
tsepezd805eec2017-01-11 14:03:54 -0800160 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot.Get(), point))
161 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700162 }
163 }
dsinclairf34518b2016-09-13 12:03:48 -0700164 return nullptr;
165}
166
dsinclairf34518b2016-09-13 12:03:48 -0700167#ifdef PDF_ENABLE_XFA
168CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CXFA_FFWidget* pPDFAnnot) {
169 if (!pPDFAnnot)
170 return nullptr;
171
172 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
173 if (pSDKAnnot)
174 return pSDKAnnot;
175
dsinclairb402b172016-10-11 09:26:32 -0700176 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700177 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
178 if (!pSDKAnnot)
179 return nullptr;
180
dsinclair8afe15a2016-10-05 12:00:34 -0700181 m_SDKAnnotArray.push_back(pSDKAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700182 return pSDKAnnot;
183}
dsinclairf34518b2016-09-13 12:03:48 -0700184
tsepez4cf55152016-11-02 14:37:54 -0700185bool CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
dsinclairf34518b2016-09-13 12:03:48 -0700186 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700187 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700188 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
dsinclair521b7502016-11-02 13:02:28 -0700189 if (!pPage || (pPage->GetContext()->GetDocType() != DOCTYPE_STATIC_XFA &&
190 pPage->GetContext()->GetDocType() != DOCTYPE_DYNAMIC_XFA))
tsepez4cf55152016-11-02 14:37:54 -0700191 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700192
193 if (GetFocusAnnot() == pAnnot)
dsinclair7cbe68e2016-10-12 11:56:23 -0700194 m_pFormFillEnv->KillFocusAnnot(0);
dsinclairb402b172016-10-11 09:26:32 -0700195 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700196 if (pAnnotHandler)
197 pAnnotHandler->ReleaseAnnot(pAnnot);
198
dsinclair8afe15a2016-10-05 12:00:34 -0700199 auto it = std::find(m_SDKAnnotArray.begin(), m_SDKAnnotArray.end(), pAnnot);
200 if (it != m_SDKAnnotArray.end())
201 m_SDKAnnotArray.erase(it);
tsepezf8074ce2016-09-27 14:29:57 -0700202 if (m_pCaptureWidget.Get() == pAnnot)
203 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700204
tsepez4cf55152016-11-02 14:37:54 -0700205 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700206}
dsinclair8afe15a2016-10-05 12:00:34 -0700207#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -0700208
209CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
210 if (m_page) {
211#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700212 return m_page->GetContext()->GetPDFDoc();
dsinclairf34518b2016-09-13 12:03:48 -0700213#else // PDF_ENABLE_XFA
214 return m_page->m_pDocument;
215#endif // PDF_ENABLE_XFA
216 }
217 return nullptr;
218}
219
220CPDF_Page* CPDFSDK_PageView::GetPDFPage() const {
221#ifdef PDF_ENABLE_XFA
222 return m_page ? m_page->GetPDFPage() : nullptr;
223#else // PDF_ENABLE_XFA
224 return m_page;
225#endif // PDF_ENABLE_XFA
226}
227
dsinclairf34518b2016-09-13 12:03:48 -0700228CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
dsinclair8afe15a2016-10-05 12:00:34 -0700229 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700230 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
231 return pAnnot;
232 }
233 return nullptr;
234}
235
236#ifdef PDF_ENABLE_XFA
237CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(CXFA_FFWidget* hWidget) {
238 if (!hWidget)
239 return nullptr;
240
dsinclair8afe15a2016-10-05 12:00:34 -0700241 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700242 if (pAnnot->GetXFAWidget() == hWidget)
243 return pAnnot;
244 }
245 return nullptr;
246}
247#endif // PDF_ENABLE_XFA
248
tsepez4cf55152016-11-02 14:37:54 -0700249bool CPDFSDK_PageView::OnLButtonDown(const CFX_FloatPoint& point,
250 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700251 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y));
252 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700253 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700254 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700255 }
256
dsinclairb402b172016-10-11 09:26:32 -0700257 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
258 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700259 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700260 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700261
262 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700263 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700264
dsinclair7cbe68e2016-10-12 11:56:23 -0700265 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700266 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700267}
268
269#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700270bool CPDFSDK_PageView::OnRButtonDown(const CFX_FloatPoint& point,
271 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700272 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y));
273 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700274 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700275
dsinclairb402b172016-10-11 09:26:32 -0700276 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
277 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700278 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700279 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700280 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700281
282 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700283 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700284
tsepez4cf55152016-11-02 14:37:54 -0700285 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700286}
287
tsepez4cf55152016-11-02 14:37:54 -0700288bool CPDFSDK_PageView::OnRButtonUp(const CFX_FloatPoint& point,
289 uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700290 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
291 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700292 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point.x, point.y));
dsinclairf34518b2016-09-13 12:03:48 -0700293 if (!pFXAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700294 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700295
tsepezf8074ce2016-09-27 14:29:57 -0700296 if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
dsinclair7cbe68e2016-10-12 11:56:23 -0700297 m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700298
tsepez4cf55152016-11-02 14:37:54 -0700299 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700300}
301#endif // PDF_ENABLE_XFA
302
tsepez4cf55152016-11-02 14:37:54 -0700303bool CPDFSDK_PageView::OnLButtonUp(const CFX_FloatPoint& point,
304 uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700305 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
306 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700307 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point.x, point.y));
308 CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot());
dsinclairf34518b2016-09-13 12:03:48 -0700309 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
310 // Last focus Annot gets a chance to handle the event.
tsepezf8074ce2016-09-27 14:29:57 -0700311 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700312 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700313 }
tsepezf8074ce2016-09-27 14:29:57 -0700314 return pFXAnnot &&
315 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
dsinclairf34518b2016-09-13 12:03:48 -0700316}
317
tsepez4cf55152016-11-02 14:37:54 -0700318bool CPDFSDK_PageView::OnMouseMove(const CFX_FloatPoint& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700319 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
320 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700321 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point.x, point.y));
322 if (pFXAnnot) {
323 if (m_pCaptureWidget && m_pCaptureWidget != pFXAnnot) {
tsepez4cf55152016-11-02 14:37:54 -0700324 m_bExitWidget = true;
325 m_bEnterWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700326 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
dsinclairf34518b2016-09-13 12:03:48 -0700327 }
tsepezf8074ce2016-09-27 14:29:57 -0700328 m_pCaptureWidget.Reset(pFXAnnot.Get());
tsepez4cf55152016-11-02 14:37:54 -0700329 m_bOnWidget = true;
dsinclairf34518b2016-09-13 12:03:48 -0700330 if (!m_bEnterWidget) {
tsepez4cf55152016-11-02 14:37:54 -0700331 m_bEnterWidget = true;
332 m_bExitWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700333 pAnnotHandlerMgr->Annot_OnMouseEnter(this, &pFXAnnot, nFlag);
dsinclairf34518b2016-09-13 12:03:48 -0700334 }
tsepezf8074ce2016-09-27 14:29:57 -0700335 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
tsepez4cf55152016-11-02 14:37:54 -0700336 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700337 }
338 if (m_bOnWidget) {
tsepez4cf55152016-11-02 14:37:54 -0700339 m_bOnWidget = false;
340 m_bExitWidget = true;
341 m_bEnterWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700342 if (m_pCaptureWidget) {
343 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
344 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700345 }
346 }
tsepez4cf55152016-11-02 14:37:54 -0700347 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700348}
349
tsepez4cf55152016-11-02 14:37:54 -0700350bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
351 double deltaY,
352 const CFX_FloatPoint& point,
353 int nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700354 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y));
355 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700356 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700357
dsinclairb402b172016-10-11 09:26:32 -0700358 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
359 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700360 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag, (int)deltaY,
361 point);
dsinclairf34518b2016-09-13 12:03:48 -0700362}
363
tsepez4cf55152016-11-02 14:37:54 -0700364bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700365 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700366 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
367 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700368 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
369 }
370
tsepez4cf55152016-11-02 14:37:54 -0700371 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700372}
373
tsepez4cf55152016-11-02 14:37:54 -0700374bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700375 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700376 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
377 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700378 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
379 }
tsepez4cf55152016-11-02 14:37:54 -0700380 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700381}
382
tsepez4cf55152016-11-02 14:37:54 -0700383bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
384 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700385}
386
387void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700388 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
389 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700390
tsepez4cf55152016-11-02 14:37:54 -0700391 SetLock(true);
dsinclairf34518b2016-09-13 12:03:48 -0700392
393#ifdef PDF_ENABLE_XFA
394 CFX_RetainPtr<CPDFXFA_Page> protector(m_page);
dsinclair521b7502016-11-02 13:02:28 -0700395 if (m_pFormFillEnv->GetXFAContext()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
dsinclairf34518b2016-09-13 12:03:48 -0700396 CXFA_FFPageView* pageView = m_page->GetXFAPageView();
397 std::unique_ptr<IXFA_WidgetIterator> pWidgetHander(
398 pageView->CreateWidgetIterator(
399 XFA_TRAVERSEWAY_Form,
400 XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable));
401 if (!pWidgetHander) {
tsepez4cf55152016-11-02 14:37:54 -0700402 SetLock(false);
dsinclairf34518b2016-09-13 12:03:48 -0700403 return;
404 }
405
406 while (CXFA_FFWidget* pXFAAnnot = pWidgetHander->MoveToNext()) {
407 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
408 if (!pAnnot)
409 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700410 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700411 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
412 }
413
tsepez4cf55152016-11-02 14:37:54 -0700414 SetLock(false);
dsinclairf34518b2016-09-13 12:03:48 -0700415 return;
416 }
417#endif // PDF_ENABLE_XFA
418
419 CPDF_Page* pPage = GetPDFPage();
420 ASSERT(pPage);
thestig7c292e02016-09-28 14:14:26 -0700421 bool bUpdateAP = CPDF_InterForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700422 // Disable the default AP construction.
thestig7c292e02016-09-28 14:14:26 -0700423 CPDF_InterForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700424 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
dsinclairf34518b2016-09-13 12:03:48 -0700425 CPDF_InterForm::SetUpdateAP(bUpdateAP);
426
427 const size_t nCount = m_pAnnotList->Count();
428 for (size_t i = 0; i < nCount; ++i) {
429 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
430 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
431 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
432 if (!pAnnot)
433 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700434 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700435 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
436 }
437
tsepez4cf55152016-11-02 14:37:54 -0700438 SetLock(false);
dsinclairf34518b2016-09-13 12:03:48 -0700439}
440
dsinclairf34518b2016-09-13 12:03:48 -0700441void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700442 for (const auto& rc : rects)
dsinclairb402b172016-10-11 09:26:32 -0700443 m_pFormFillEnv->Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
dsinclairf34518b2016-09-13 12:03:48 -0700444}
445
446void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
447 CFX_FloatRect rcWindow = pAnnot->GetRect();
dsinclairb402b172016-10-11 09:26:32 -0700448 m_pFormFillEnv->Invalidate(m_page, rcWindow.left, rcWindow.top,
449 rcWindow.right, rcWindow.bottom);
dsinclairf34518b2016-09-13 12:03:48 -0700450}
451
452int CPDFSDK_PageView::GetPageIndex() const {
453 if (!m_page)
454 return -1;
455
456#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700457 int nDocType = m_page->GetContext()->GetDocType();
dsinclairf34518b2016-09-13 12:03:48 -0700458 switch (nDocType) {
459 case DOCTYPE_DYNAMIC_XFA: {
460 CXFA_FFPageView* pPageView = m_page->GetXFAPageView();
461 return pPageView ? pPageView->GetPageIndex() : -1;
462 }
463 case DOCTYPE_STATIC_XFA:
464 case DOCTYPE_PDF:
465 return GetPageIndexForStaticPDF();
466 default:
467 return -1;
468 }
469#else // PDF_ENABLE_XFA
470 return GetPageIndexForStaticPDF();
471#endif // PDF_ENABLE_XFA
472}
473
474bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
475 if (!p)
476 return false;
477
478 const auto& annots = m_pAnnotList->All();
479 auto it = std::find_if(annots.begin(), annots.end(),
480 [p](const std::unique_ptr<CPDF_Annot>& annot) {
481 return annot.get() == p;
482 });
483 return it != annots.end();
484}
485
dsinclair8afe15a2016-10-05 12:00:34 -0700486bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
487 if (!p)
488 return false;
489 return pdfium::ContainsValue(m_SDKAnnotArray, p);
490}
491
dsinclairf34518b2016-09-13 12:03:48 -0700492CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700493 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700494 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700495}
496
497int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
498 CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict;
dsinclair7cbe68e2016-10-12 11:56:23 -0700499 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700500 return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
501}