blob: 416adffbe610e4ab614117654adab259a76da2cc [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);
100 CFX_RectF rectClip;
101 rectClip.Set(static_cast<FX_FLOAT>(pClip.left),
102 static_cast<FX_FLOAT>(pClip.top),
103 static_cast<FX_FLOAT>(pClip.Width()),
104 static_cast<FX_FLOAT>(pClip.Height()));
105 gs.SetClipRect(rectClip);
106 std::unique_ptr<CXFA_RenderContext> pRenderContext(new CXFA_RenderContext);
107 CXFA_RenderOptions renderOptions;
tsepez4cf55152016-11-02 14:37:54 -0700108 renderOptions.m_bHighlight = true;
dsinclairf34518b2016-09-13 12:03:48 -0700109 CXFA_FFPageView* xfaView = pPage->GetXFAPageView();
110 pRenderContext->StartRender(xfaView, &gs, *pUser2Device, renderOptions);
111 pRenderContext->DoRender();
112 pRenderContext->StopRender();
113 CXFA_FFDocView* docView = xfaView->GetDocView();
114 if (!docView)
115 return;
116 CPDFSDK_Annot* annot = GetFocusAnnot();
117 if (!annot)
118 return;
119 // Render the focus widget
120 docView->GetWidgetHandler()->RenderWidget(annot->GetXFAWidget(), &gs,
tsepez4cf55152016-11-02 14:37:54 -0700121 pUser2Device, false);
dsinclairf34518b2016-09-13 12:03:48 -0700122 return;
123 }
124#endif // PDF_ENABLE_XFA
125
126 // for pdf/static xfa.
tsepezd805eec2017-01-11 14:03:54 -0800127 CPDFSDK_AnnotIteration annotIteration(this, true);
128 for (const auto& pSDKAnnot : annotIteration) {
129 m_pFormFillEnv->GetAnnotHandlerMgr()->Annot_OnDraw(
130 this, pSDKAnnot.Get(), pDevice, pUser2Device, pOptions->m_bDrawAnnots);
dsinclairf34518b2016-09-13 12:03:48 -0700131 }
132}
133
dsinclairf34518b2016-09-13 12:03:48 -0700134CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
135 FX_FLOAT pageY) {
dsinclairb402b172016-10-11 09:26:32 -0700136 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800137 CPDFSDK_AnnotIteration annotIteration(this, false);
138 for (const auto& pSDKAnnot : annotIteration) {
139 CFX_FloatRect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
dsinclairf34518b2016-09-13 12:03:48 -0700140 if (pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::POPUP)
141 continue;
142 if (rc.Contains(pageX, pageY))
tsepezd805eec2017-01-11 14:03:54 -0800143 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700144 }
dsinclairf34518b2016-09-13 12:03:48 -0700145 return nullptr;
146}
147
148CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
149 FX_FLOAT pageY) {
dsinclairb402b172016-10-11 09:26:32 -0700150 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800151 CPDFSDK_AnnotIteration annotIteration(this, false);
152 for (const auto& pSDKAnnot : annotIteration) {
dsinclairf34518b2016-09-13 12:03:48 -0700153 bool bHitTest = pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET;
154#ifdef PDF_ENABLE_XFA
155 bHitTest = bHitTest ||
156 pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::XFAWIDGET;
157#endif // PDF_ENABLE_XFA
158 if (bHitTest) {
tsepezd805eec2017-01-11 14:03:54 -0800159 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
dsinclairf34518b2016-09-13 12:03:48 -0700160 CFX_FloatPoint point(pageX, pageY);
tsepezd805eec2017-01-11 14:03:54 -0800161 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot.Get(), point))
162 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700163 }
164 }
dsinclairf34518b2016-09-13 12:03:48 -0700165 return nullptr;
166}
167
dsinclairf34518b2016-09-13 12:03:48 -0700168#ifdef PDF_ENABLE_XFA
169CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CXFA_FFWidget* pPDFAnnot) {
170 if (!pPDFAnnot)
171 return nullptr;
172
173 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
174 if (pSDKAnnot)
175 return pSDKAnnot;
176
dsinclairb402b172016-10-11 09:26:32 -0700177 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700178 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
179 if (!pSDKAnnot)
180 return nullptr;
181
dsinclair8afe15a2016-10-05 12:00:34 -0700182 m_SDKAnnotArray.push_back(pSDKAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700183 return pSDKAnnot;
184}
dsinclairf34518b2016-09-13 12:03:48 -0700185
tsepez4cf55152016-11-02 14:37:54 -0700186bool CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
dsinclairf34518b2016-09-13 12:03:48 -0700187 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700188 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700189 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
dsinclair521b7502016-11-02 13:02:28 -0700190 if (!pPage || (pPage->GetContext()->GetDocType() != DOCTYPE_STATIC_XFA &&
191 pPage->GetContext()->GetDocType() != DOCTYPE_DYNAMIC_XFA))
tsepez4cf55152016-11-02 14:37:54 -0700192 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700193
194 if (GetFocusAnnot() == pAnnot)
dsinclair7cbe68e2016-10-12 11:56:23 -0700195 m_pFormFillEnv->KillFocusAnnot(0);
dsinclairb402b172016-10-11 09:26:32 -0700196 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700197 if (pAnnotHandler)
198 pAnnotHandler->ReleaseAnnot(pAnnot);
199
dsinclair8afe15a2016-10-05 12:00:34 -0700200 auto it = std::find(m_SDKAnnotArray.begin(), m_SDKAnnotArray.end(), pAnnot);
201 if (it != m_SDKAnnotArray.end())
202 m_SDKAnnotArray.erase(it);
tsepezf8074ce2016-09-27 14:29:57 -0700203 if (m_pCaptureWidget.Get() == pAnnot)
204 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700205
tsepez4cf55152016-11-02 14:37:54 -0700206 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700207}
dsinclair8afe15a2016-10-05 12:00:34 -0700208#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -0700209
210CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
211 if (m_page) {
212#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700213 return m_page->GetContext()->GetPDFDoc();
dsinclairf34518b2016-09-13 12:03:48 -0700214#else // PDF_ENABLE_XFA
215 return m_page->m_pDocument;
216#endif // PDF_ENABLE_XFA
217 }
218 return nullptr;
219}
220
221CPDF_Page* CPDFSDK_PageView::GetPDFPage() const {
222#ifdef PDF_ENABLE_XFA
223 return m_page ? m_page->GetPDFPage() : nullptr;
224#else // PDF_ENABLE_XFA
225 return m_page;
226#endif // PDF_ENABLE_XFA
227}
228
dsinclairf34518b2016-09-13 12:03:48 -0700229CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
dsinclair8afe15a2016-10-05 12:00:34 -0700230 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700231 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
232 return pAnnot;
233 }
234 return nullptr;
235}
236
237#ifdef PDF_ENABLE_XFA
238CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(CXFA_FFWidget* hWidget) {
239 if (!hWidget)
240 return nullptr;
241
dsinclair8afe15a2016-10-05 12:00:34 -0700242 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700243 if (pAnnot->GetXFAWidget() == hWidget)
244 return pAnnot;
245 }
246 return nullptr;
247}
248#endif // PDF_ENABLE_XFA
249
tsepez4cf55152016-11-02 14:37:54 -0700250bool CPDFSDK_PageView::OnLButtonDown(const CFX_FloatPoint& point,
251 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700252 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y));
253 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700254 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700255 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700256 }
257
dsinclairb402b172016-10-11 09:26:32 -0700258 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
259 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700260 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700261 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700262
263 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700264 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700265
dsinclair7cbe68e2016-10-12 11:56:23 -0700266 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700267 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700268}
269
270#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700271bool CPDFSDK_PageView::OnRButtonDown(const CFX_FloatPoint& point,
272 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700273 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y));
274 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700275 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700276
dsinclairb402b172016-10-11 09:26:32 -0700277 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
278 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700279 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700280 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700281 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700282
283 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700284 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700285
tsepez4cf55152016-11-02 14:37:54 -0700286 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700287}
288
tsepez4cf55152016-11-02 14:37:54 -0700289bool CPDFSDK_PageView::OnRButtonUp(const CFX_FloatPoint& point,
290 uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700291 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
292 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700293 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point.x, point.y));
dsinclairf34518b2016-09-13 12:03:48 -0700294 if (!pFXAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700295 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700296
tsepezf8074ce2016-09-27 14:29:57 -0700297 if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
dsinclair7cbe68e2016-10-12 11:56:23 -0700298 m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700299
tsepez4cf55152016-11-02 14:37:54 -0700300 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700301}
302#endif // PDF_ENABLE_XFA
303
tsepez4cf55152016-11-02 14:37:54 -0700304bool CPDFSDK_PageView::OnLButtonUp(const CFX_FloatPoint& point,
305 uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700306 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
307 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700308 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point.x, point.y));
309 CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot());
dsinclairf34518b2016-09-13 12:03:48 -0700310 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
311 // Last focus Annot gets a chance to handle the event.
tsepezf8074ce2016-09-27 14:29:57 -0700312 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700313 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700314 }
tsepezf8074ce2016-09-27 14:29:57 -0700315 return pFXAnnot &&
316 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
dsinclairf34518b2016-09-13 12:03:48 -0700317}
318
tsepez4cf55152016-11-02 14:37:54 -0700319bool CPDFSDK_PageView::OnMouseMove(const CFX_FloatPoint& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700320 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
321 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700322 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point.x, point.y));
323 if (pFXAnnot) {
324 if (m_pCaptureWidget && m_pCaptureWidget != pFXAnnot) {
tsepez4cf55152016-11-02 14:37:54 -0700325 m_bExitWidget = true;
326 m_bEnterWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700327 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
dsinclairf34518b2016-09-13 12:03:48 -0700328 }
tsepezf8074ce2016-09-27 14:29:57 -0700329 m_pCaptureWidget.Reset(pFXAnnot.Get());
tsepez4cf55152016-11-02 14:37:54 -0700330 m_bOnWidget = true;
dsinclairf34518b2016-09-13 12:03:48 -0700331 if (!m_bEnterWidget) {
tsepez4cf55152016-11-02 14:37:54 -0700332 m_bEnterWidget = true;
333 m_bExitWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700334 pAnnotHandlerMgr->Annot_OnMouseEnter(this, &pFXAnnot, nFlag);
dsinclairf34518b2016-09-13 12:03:48 -0700335 }
tsepezf8074ce2016-09-27 14:29:57 -0700336 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
tsepez4cf55152016-11-02 14:37:54 -0700337 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700338 }
339 if (m_bOnWidget) {
tsepez4cf55152016-11-02 14:37:54 -0700340 m_bOnWidget = false;
341 m_bExitWidget = true;
342 m_bEnterWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700343 if (m_pCaptureWidget) {
344 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
345 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700346 }
347 }
tsepez4cf55152016-11-02 14:37:54 -0700348 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700349}
350
tsepez4cf55152016-11-02 14:37:54 -0700351bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
352 double deltaY,
353 const CFX_FloatPoint& point,
354 int nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700355 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y));
356 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700357 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700358
dsinclairb402b172016-10-11 09:26:32 -0700359 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
360 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700361 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag, (int)deltaY,
362 point);
dsinclairf34518b2016-09-13 12:03:48 -0700363}
364
tsepez4cf55152016-11-02 14:37:54 -0700365bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700366 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700367 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
368 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700369 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
370 }
371
tsepez4cf55152016-11-02 14:37:54 -0700372 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700373}
374
tsepez4cf55152016-11-02 14:37:54 -0700375bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700376 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700377 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
378 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700379 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
380 }
tsepez4cf55152016-11-02 14:37:54 -0700381 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700382}
383
tsepez4cf55152016-11-02 14:37:54 -0700384bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
385 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700386}
387
388void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700389 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
390 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700391
tsepez4cf55152016-11-02 14:37:54 -0700392 SetLock(true);
dsinclairf34518b2016-09-13 12:03:48 -0700393
394#ifdef PDF_ENABLE_XFA
395 CFX_RetainPtr<CPDFXFA_Page> protector(m_page);
dsinclair521b7502016-11-02 13:02:28 -0700396 if (m_pFormFillEnv->GetXFAContext()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
dsinclairf34518b2016-09-13 12:03:48 -0700397 CXFA_FFPageView* pageView = m_page->GetXFAPageView();
398 std::unique_ptr<IXFA_WidgetIterator> pWidgetHander(
399 pageView->CreateWidgetIterator(
400 XFA_TRAVERSEWAY_Form,
401 XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable));
402 if (!pWidgetHander) {
tsepez4cf55152016-11-02 14:37:54 -0700403 SetLock(false);
dsinclairf34518b2016-09-13 12:03:48 -0700404 return;
405 }
406
407 while (CXFA_FFWidget* pXFAAnnot = pWidgetHander->MoveToNext()) {
408 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
409 if (!pAnnot)
410 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700411 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700412 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
413 }
414
tsepez4cf55152016-11-02 14:37:54 -0700415 SetLock(false);
dsinclairf34518b2016-09-13 12:03:48 -0700416 return;
417 }
418#endif // PDF_ENABLE_XFA
419
420 CPDF_Page* pPage = GetPDFPage();
421 ASSERT(pPage);
thestig7c292e02016-09-28 14:14:26 -0700422 bool bUpdateAP = CPDF_InterForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700423 // Disable the default AP construction.
thestig7c292e02016-09-28 14:14:26 -0700424 CPDF_InterForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700425 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
dsinclairf34518b2016-09-13 12:03:48 -0700426 CPDF_InterForm::SetUpdateAP(bUpdateAP);
427
428 const size_t nCount = m_pAnnotList->Count();
429 for (size_t i = 0; i < nCount; ++i) {
430 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
431 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
432 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
433 if (!pAnnot)
434 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700435 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700436 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
437 }
438
tsepez4cf55152016-11-02 14:37:54 -0700439 SetLock(false);
dsinclairf34518b2016-09-13 12:03:48 -0700440}
441
dsinclairf34518b2016-09-13 12:03:48 -0700442void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700443 for (const auto& rc : rects)
dsinclairb402b172016-10-11 09:26:32 -0700444 m_pFormFillEnv->Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
dsinclairf34518b2016-09-13 12:03:48 -0700445}
446
447void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
448 CFX_FloatRect rcWindow = pAnnot->GetRect();
dsinclairb402b172016-10-11 09:26:32 -0700449 m_pFormFillEnv->Invalidate(m_page, rcWindow.left, rcWindow.top,
450 rcWindow.right, rcWindow.bottom);
dsinclairf34518b2016-09-13 12:03:48 -0700451}
452
453int CPDFSDK_PageView::GetPageIndex() const {
454 if (!m_page)
455 return -1;
456
457#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700458 int nDocType = m_page->GetContext()->GetDocType();
dsinclairf34518b2016-09-13 12:03:48 -0700459 switch (nDocType) {
460 case DOCTYPE_DYNAMIC_XFA: {
461 CXFA_FFPageView* pPageView = m_page->GetXFAPageView();
462 return pPageView ? pPageView->GetPageIndex() : -1;
463 }
464 case DOCTYPE_STATIC_XFA:
465 case DOCTYPE_PDF:
466 return GetPageIndexForStaticPDF();
467 default:
468 return -1;
469 }
470#else // PDF_ENABLE_XFA
471 return GetPageIndexForStaticPDF();
472#endif // PDF_ENABLE_XFA
473}
474
475bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
476 if (!p)
477 return false;
478
479 const auto& annots = m_pAnnotList->All();
480 auto it = std::find_if(annots.begin(), annots.end(),
481 [p](const std::unique_ptr<CPDF_Annot>& annot) {
482 return annot.get() == p;
483 });
484 return it != annots.end();
485}
486
dsinclair8afe15a2016-10-05 12:00:34 -0700487bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
488 if (!p)
489 return false;
490 return pdfium::ContainsValue(m_SDKAnnotArray, p);
491}
492
dsinclairf34518b2016-09-13 12:03:48 -0700493CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700494 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700495 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700496}
497
498int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
499 CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict;
dsinclair7cbe68e2016-10-12 11:56:23 -0700500 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700501 return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
502}