blob: 2155b5d3d8d249a737656d571445bb637f3ba28a [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"
Dan Sinclair674bbfe2017-08-30 15:37:47 -040016#include "core/fxcrt/cfx_autorestorer.h"
dsinclair114e46a2016-09-29 17:18:21 -070017#include "fpdfsdk/cpdfsdk_annot.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"
Dan Sinclair80c48782017-03-23 12:11:20 -040025#include "xfa/fxfa/cxfa_ffdocview.h"
26#include "xfa/fxfa/cxfa_ffpageview.h"
27#include "xfa/fxfa/cxfa_ffwidgethandler.h"
28#include "xfa/fxfa/cxfa_rendercontext.h"
Dan Sinclair2b918c82017-07-13 14:47:10 -040029#include "xfa/fxgraphics/cxfa_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_bOnWidget(false),
40 m_bValid(false),
41 m_bLocked(false),
dsinclairbcf46232016-10-03 13:02:27 -070042 m_bBeingDestroyed(false) {
dsinclair7cbe68e2016-10-12 11:56:23 -070043 CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
Lei Zhang222e1a42017-06-20 14:47:00 -070044 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
dsinclairf34518b2016-09-13 12:03:48 -070045#ifdef PDF_ENABLE_XFA
Lei Zhang222e1a42017-06-20 14:47:00 -070046 if (page->GetPDFPage())
47 pPDFInterForm->FixPageFields(page->GetPDFPage());
dsinclairf34518b2016-09-13 12:03:48 -070048#else // PDF_ENABLE_XFA
Lei Zhang222e1a42017-06-20 14:47:00 -070049 pPDFInterForm->FixPageFields(page);
dsinclairf34518b2016-09-13 12:03:48 -070050 m_page->SetView(this);
51#endif // PDF_ENABLE_XFA
52}
53
54CPDFSDK_PageView::~CPDFSDK_PageView() {
55#ifndef PDF_ENABLE_XFA
56 // The call to |ReleaseAnnot| can cause the page pointed to by |m_page| to
57 // be freed, which will cause issues if we try to cleanup the pageview pointer
58 // in |m_page|. So, reset the pageview pointer before doing anything else.
59 m_page->SetView(nullptr);
60#endif // PDF_ENABLE_XFA
61
dsinclairb402b172016-10-11 09:26:32 -070062 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
63 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclair8afe15a2016-10-05 12:00:34 -070064 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray)
dsinclairf34518b2016-09-13 12:03:48 -070065 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
66
dsinclair8afe15a2016-10-05 12:00:34 -070067 m_SDKAnnotArray.clear();
dsinclairf34518b2016-09-13 12:03:48 -070068 m_pAnnotList.reset();
69
70#ifndef PDF_ENABLE_XFA
71 if (m_bOwnsPage)
72 delete m_page;
73#endif // PDF_ENABLE_XFA
74}
75
76void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
77 CFX_Matrix* pUser2Device,
78#ifdef PDF_ENABLE_XFA
79 CPDF_RenderOptions* pOptions,
80 const FX_RECT& pClip) {
81#else
82 CPDF_RenderOptions* pOptions) {
83#endif // PDF_ENABLE_XFA
84 m_curMatrix = *pUser2Device;
dsinclairf34518b2016-09-13 12:03:48 -070085
86#ifdef PDF_ENABLE_XFA
87 CPDFXFA_Page* pPage = GetPDFXFAPage();
88 if (!pPage)
89 return;
90
Dan Sinclaircdba7472017-03-23 09:17:10 -040091 if (pPage->GetContext()->GetDocType() == XFA_DocType::Dynamic) {
Dan Sinclair05df0752017-03-14 14:43:42 -040092 CFX_RectF rectClip(
93 static_cast<float>(pClip.left), static_cast<float>(pClip.top),
94 static_cast<float>(pClip.Width()), static_cast<float>(pClip.Height()));
Dan Sinclaircf53b782017-05-04 14:08:01 -040095
Dan Sinclair2b918c82017-07-13 14:47:10 -040096 CXFA_Graphics gs(pDevice);
dsinclairf34518b2016-09-13 12:03:48 -070097 gs.SetClipRect(rectClip);
Dan Sinclaircf53b782017-05-04 14:08:01 -040098
dsinclairf34518b2016-09-13 12:03:48 -070099 CXFA_FFPageView* xfaView = pPage->GetXFAPageView();
Dan Sinclaircf53b782017-05-04 14:08:01 -0400100 CXFA_RenderContext renderContext(xfaView, rectClip, *pUser2Device);
101 renderContext.DoRender(&gs);
102
dsinclairf34518b2016-09-13 12:03:48 -0700103 CXFA_FFDocView* docView = xfaView->GetDocView();
104 if (!docView)
105 return;
106 CPDFSDK_Annot* annot = GetFocusAnnot();
107 if (!annot)
108 return;
109 // Render the focus widget
110 docView->GetWidgetHandler()->RenderWidget(annot->GetXFAWidget(), &gs,
Lei Zhang7c9d4c22017-08-17 13:53:52 -0700111 *pUser2Device, false);
dsinclairf34518b2016-09-13 12:03:48 -0700112 return;
113 }
114#endif // PDF_ENABLE_XFA
115
116 // for pdf/static xfa.
tsepezd805eec2017-01-11 14:03:54 -0800117 CPDFSDK_AnnotIteration annotIteration(this, true);
118 for (const auto& pSDKAnnot : annotIteration) {
119 m_pFormFillEnv->GetAnnotHandlerMgr()->Annot_OnDraw(
120 this, pSDKAnnot.Get(), pDevice, pUser2Device, pOptions->m_bDrawAnnots);
dsinclairf34518b2016-09-13 12:03:48 -0700121 }
122}
123
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500124CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(const CFX_PointF& point) {
dsinclairb402b172016-10-11 09:26:32 -0700125 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800126 CPDFSDK_AnnotIteration annotIteration(this, false);
127 for (const auto& pSDKAnnot : annotIteration) {
128 CFX_FloatRect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
dsinclairf34518b2016-09-13 12:03:48 -0700129 if (pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::POPUP)
130 continue;
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500131 if (rc.Contains(point))
tsepezd805eec2017-01-11 14:03:54 -0800132 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700133 }
dsinclairf34518b2016-09-13 12:03:48 -0700134 return nullptr;
135}
136
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500137CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(const CFX_PointF& point) {
dsinclairb402b172016-10-11 09:26:32 -0700138 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800139 CPDFSDK_AnnotIteration annotIteration(this, false);
140 for (const auto& pSDKAnnot : annotIteration) {
dsinclairf34518b2016-09-13 12:03:48 -0700141 bool bHitTest = pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET;
142#ifdef PDF_ENABLE_XFA
143 bHitTest = bHitTest ||
144 pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::XFAWIDGET;
145#endif // PDF_ENABLE_XFA
146 if (bHitTest) {
tsepezd805eec2017-01-11 14:03:54 -0800147 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500148 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot.Get(), point))
tsepezd805eec2017-01-11 14:03:54 -0800149 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700150 }
151 }
dsinclairf34518b2016-09-13 12:03:48 -0700152 return nullptr;
153}
154
dsinclairf34518b2016-09-13 12:03:48 -0700155#ifdef PDF_ENABLE_XFA
156CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CXFA_FFWidget* pPDFAnnot) {
157 if (!pPDFAnnot)
158 return nullptr;
159
160 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
161 if (pSDKAnnot)
162 return pSDKAnnot;
163
dsinclairb402b172016-10-11 09:26:32 -0700164 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700165 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
166 if (!pSDKAnnot)
167 return nullptr;
168
dsinclair8afe15a2016-10-05 12:00:34 -0700169 m_SDKAnnotArray.push_back(pSDKAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700170 return pSDKAnnot;
171}
dsinclairf34518b2016-09-13 12:03:48 -0700172
tsepez4cf55152016-11-02 14:37:54 -0700173bool CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
dsinclairf34518b2016-09-13 12:03:48 -0700174 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700175 return false;
Tom Sepezbfa2a972017-07-24 11:38:31 -0700176
dsinclairf34518b2016-09-13 12:03:48 -0700177 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
Dan Sinclaircdba7472017-03-23 09:17:10 -0400178 if (!pPage || (pPage->GetContext()->GetDocType() != XFA_DocType::Static &&
179 pPage->GetContext()->GetDocType() != XFA_DocType::Dynamic)) {
tsepez4cf55152016-11-02 14:37:54 -0700180 return false;
Dan Sinclaircdba7472017-03-23 09:17:10 -0400181 }
dsinclairf34518b2016-09-13 12:03:48 -0700182
Tom Sepezbfa2a972017-07-24 11:38:31 -0700183 CPDFSDK_Annot::ObservedPtr pObserved(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700184 if (GetFocusAnnot() == pAnnot)
Tom Sepezbfa2a972017-07-24 11:38:31 -0700185 m_pFormFillEnv->KillFocusAnnot(0); // May invoke JS, invalidating pAnnot.
186
187 if (pObserved) {
188 CPDFSDK_AnnotHandlerMgr* pAnnotHandler =
189 m_pFormFillEnv->GetAnnotHandlerMgr();
190 if (pAnnotHandler)
191 pAnnotHandler->ReleaseAnnot(pObserved.Get());
192 }
dsinclairf34518b2016-09-13 12:03:48 -0700193
dsinclair8afe15a2016-10-05 12:00:34 -0700194 auto it = std::find(m_SDKAnnotArray.begin(), m_SDKAnnotArray.end(), pAnnot);
195 if (it != m_SDKAnnotArray.end())
196 m_SDKAnnotArray.erase(it);
tsepezf8074ce2016-09-27 14:29:57 -0700197 if (m_pCaptureWidget.Get() == pAnnot)
198 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700199
tsepez4cf55152016-11-02 14:37:54 -0700200 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700201}
dsinclair8afe15a2016-10-05 12:00:34 -0700202#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -0700203
204CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
205 if (m_page) {
206#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700207 return m_page->GetContext()->GetPDFDoc();
dsinclairf34518b2016-09-13 12:03:48 -0700208#else // PDF_ENABLE_XFA
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700209 return m_page->m_pDocument.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700210#endif // PDF_ENABLE_XFA
211 }
212 return nullptr;
213}
214
215CPDF_Page* CPDFSDK_PageView::GetPDFPage() const {
216#ifdef PDF_ENABLE_XFA
217 return m_page ? m_page->GetPDFPage() : nullptr;
218#else // PDF_ENABLE_XFA
219 return m_page;
220#endif // PDF_ENABLE_XFA
221}
222
dsinclairf34518b2016-09-13 12:03:48 -0700223CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
dsinclair8afe15a2016-10-05 12:00:34 -0700224 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700225 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
226 return pAnnot;
227 }
228 return nullptr;
229}
230
231#ifdef PDF_ENABLE_XFA
232CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(CXFA_FFWidget* hWidget) {
233 if (!hWidget)
234 return nullptr;
235
dsinclair8afe15a2016-10-05 12:00:34 -0700236 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700237 if (pAnnot->GetXFAWidget() == hWidget)
238 return pAnnot;
239 }
240 return nullptr;
241}
242#endif // PDF_ENABLE_XFA
243
Diana Gagedce2d722017-06-20 11:17:11 -0700244CFX_WideString CPDFSDK_PageView::GetSelectedText() {
245 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
246 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
247 m_pFormFillEnv->GetAnnotHandlerMgr();
248 return pAnnotHandlerMgr->Annot_GetSelectedText(pAnnot);
249 }
250
251 return CFX_WideString();
252}
253
Diana Gageab390972017-07-28 17:07:39 -0700254void CPDFSDK_PageView::ReplaceSelection(const CFX_WideString& text) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700255 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
256 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
257 m_pFormFillEnv->GetAnnotHandlerMgr();
Diana Gageab390972017-07-28 17:07:39 -0700258 pAnnotHandlerMgr->Annot_ReplaceSelection(pAnnot, text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700259 }
260}
261
Dan Sinclairf528eee2017-02-14 11:52:07 -0500262bool CPDFSDK_PageView::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500263 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700264 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700265 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700266 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700267 }
268
dsinclairb402b172016-10-11 09:26:32 -0700269 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
270 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700271 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700272 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700273
274 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700275 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700276
dsinclair7cbe68e2016-10-12 11:56:23 -0700277 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700278 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700279}
280
281#ifdef PDF_ENABLE_XFA
Dan Sinclairf528eee2017-02-14 11:52:07 -0500282bool CPDFSDK_PageView::OnRButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500283 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700284 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700285 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700286
dsinclairb402b172016-10-11 09:26:32 -0700287 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
288 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700289 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700290 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700291 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700292
293 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700294 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700295
tsepez4cf55152016-11-02 14:37:54 -0700296 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700297}
298
Dan Sinclairf528eee2017-02-14 11:52:07 -0500299bool CPDFSDK_PageView::OnRButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700300 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
301 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500302 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
dsinclairf34518b2016-09-13 12:03:48 -0700303 if (!pFXAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700304 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700305
tsepezf8074ce2016-09-27 14:29:57 -0700306 if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
dsinclair7cbe68e2016-10-12 11:56:23 -0700307 m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700308
tsepez4cf55152016-11-02 14:37:54 -0700309 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700310}
311#endif // PDF_ENABLE_XFA
312
Dan Sinclairf528eee2017-02-14 11:52:07 -0500313bool CPDFSDK_PageView::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700314 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
315 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500316 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700317 CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot());
dsinclairf34518b2016-09-13 12:03:48 -0700318 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
319 // Last focus Annot gets a chance to handle the event.
tsepezf8074ce2016-09-27 14:29:57 -0700320 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700321 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700322 }
tsepezf8074ce2016-09-27 14:29:57 -0700323 return pFXAnnot &&
324 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
dsinclairf34518b2016-09-13 12:03:48 -0700325}
326
Dan Sinclairf528eee2017-02-14 11:52:07 -0500327bool CPDFSDK_PageView::OnMouseMove(const CFX_PointF& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700328 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
329 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500330 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point));
Henrique Nakashima65513622017-08-16 14:48:13 -0400331
Henrique Nakashima4a652542017-08-22 10:59:49 -0400332 if (m_bOnWidget && m_pCaptureWidget != pFXAnnot)
333 ExitWidget(pAnnotHandlerMgr, true, nFlag);
334
335 if (pFXAnnot) {
336 if (!m_bOnWidget) {
337 EnterWidget(pAnnotHandlerMgr, &pFXAnnot, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400338
339 // Annot_OnMouseEnter may have invalidated pFXAnnot.
340 if (!pFXAnnot) {
Henrique Nakashima4a652542017-08-22 10:59:49 -0400341 ExitWidget(pAnnotHandlerMgr, false, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400342 return true;
343 }
dsinclairf34518b2016-09-13 12:03:48 -0700344 }
Henrique Nakashima4a652542017-08-22 10:59:49 -0400345
tsepezf8074ce2016-09-27 14:29:57 -0700346 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
tsepez4cf55152016-11-02 14:37:54 -0700347 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700348 }
Henrique Nakashima65513622017-08-16 14:48:13 -0400349
tsepez4cf55152016-11-02 14:37:54 -0700350 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700351}
352
Henrique Nakashima4a652542017-08-22 10:59:49 -0400353void CPDFSDK_PageView::EnterWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
354 CPDFSDK_Annot::ObservedPtr* pAnnot,
355 uint32_t nFlag) {
356 m_bOnWidget = true;
357 m_pCaptureWidget.Reset(pAnnot->Get());
358 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pAnnot, nFlag);
359}
360
361void CPDFSDK_PageView::ExitWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
362 bool callExitCallback,
363 uint32_t nFlag) {
364 m_bOnWidget = false;
365 if (m_pCaptureWidget) {
366 if (callExitCallback)
367 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
368
369 m_pCaptureWidget.Reset();
370 }
371}
372
tsepez4cf55152016-11-02 14:37:54 -0700373bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
374 double deltaY,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500375 const CFX_PointF& point,
tsepez4cf55152016-11-02 14:37:54 -0700376 int nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500377 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700378 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700379 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700380
dsinclairb402b172016-10-11 09:26:32 -0700381 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
382 m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang222e1a42017-06-20 14:47:00 -0700383 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag,
384 static_cast<int>(deltaY), point);
dsinclairf34518b2016-09-13 12:03:48 -0700385}
386
tsepez4cf55152016-11-02 14:37:54 -0700387bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700388 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700389 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
390 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700391 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
392 }
393
tsepez4cf55152016-11-02 14:37:54 -0700394 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700395}
396
tsepez4cf55152016-11-02 14:37:54 -0700397bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700398 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700399 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
400 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700401 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
402 }
tsepez4cf55152016-11-02 14:37:54 -0700403 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700404}
405
tsepez4cf55152016-11-02 14:37:54 -0700406bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
407 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700408}
409
410void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700411 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
412 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700413
Henrique Nakashima65513622017-08-16 14:48:13 -0400414 CFX_AutoRestorer<bool> lock(&m_bLocked);
415 m_bLocked = true;
dsinclairf34518b2016-09-13 12:03:48 -0700416
417#ifdef PDF_ENABLE_XFA
418 CFX_RetainPtr<CPDFXFA_Page> protector(m_page);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400419 if (m_pFormFillEnv->GetXFAContext()->GetDocType() == XFA_DocType::Dynamic) {
dsinclairf34518b2016-09-13 12:03:48 -0700420 CXFA_FFPageView* pageView = m_page->GetXFAPageView();
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700421 std::unique_ptr<IXFA_WidgetIterator> pWidgetHandler(
dsinclairf34518b2016-09-13 12:03:48 -0700422 pageView->CreateWidgetIterator(
423 XFA_TRAVERSEWAY_Form,
424 XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable));
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700425 if (!pWidgetHandler) {
dsinclairf34518b2016-09-13 12:03:48 -0700426 return;
427 }
428
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700429 while (CXFA_FFWidget* pXFAAnnot = pWidgetHandler->MoveToNext()) {
dsinclairf34518b2016-09-13 12:03:48 -0700430 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
431 if (!pAnnot)
432 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700433 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700434 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
435 }
436
dsinclairf34518b2016-09-13 12:03:48 -0700437 return;
438 }
439#endif // PDF_ENABLE_XFA
440
441 CPDF_Page* pPage = GetPDFPage();
442 ASSERT(pPage);
thestig7c292e02016-09-28 14:14:26 -0700443 bool bUpdateAP = CPDF_InterForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700444 // Disable the default AP construction.
thestig7c292e02016-09-28 14:14:26 -0700445 CPDF_InterForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700446 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
dsinclairf34518b2016-09-13 12:03:48 -0700447 CPDF_InterForm::SetUpdateAP(bUpdateAP);
448
449 const size_t nCount = m_pAnnotList->Count();
450 for (size_t i = 0; i < nCount; ++i) {
451 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
452 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
453 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
454 if (!pAnnot)
455 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700456 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700457 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
458 }
dsinclairf34518b2016-09-13 12:03:48 -0700459}
460
dsinclairf34518b2016-09-13 12:03:48 -0700461void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700462 for (const auto& rc : rects)
Lei Zhang77f9bff2017-08-29 11:34:12 -0700463 m_pFormFillEnv->Invalidate(m_page, rc.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700464}
465
466void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
467 CFX_FloatRect rcWindow = pAnnot->GetRect();
Lei Zhang77f9bff2017-08-29 11:34:12 -0700468 m_pFormFillEnv->Invalidate(m_page, rcWindow.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700469}
470
471int CPDFSDK_PageView::GetPageIndex() const {
472 if (!m_page)
473 return -1;
474
475#ifdef PDF_ENABLE_XFA
Dan Sinclaircdba7472017-03-23 09:17:10 -0400476 switch (m_page->GetContext()->GetDocType()) {
477 case XFA_DocType::Dynamic: {
dsinclairf34518b2016-09-13 12:03:48 -0700478 CXFA_FFPageView* pPageView = m_page->GetXFAPageView();
479 return pPageView ? pPageView->GetPageIndex() : -1;
480 }
Dan Sinclaircdba7472017-03-23 09:17:10 -0400481 case XFA_DocType::Static:
482 case XFA_DocType::PDF:
dsinclairf34518b2016-09-13 12:03:48 -0700483 return GetPageIndexForStaticPDF();
484 default:
485 return -1;
486 }
487#else // PDF_ENABLE_XFA
488 return GetPageIndexForStaticPDF();
489#endif // PDF_ENABLE_XFA
490}
491
492bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
493 if (!p)
494 return false;
495
496 const auto& annots = m_pAnnotList->All();
497 auto it = std::find_if(annots.begin(), annots.end(),
498 [p](const std::unique_ptr<CPDF_Annot>& annot) {
499 return annot.get() == p;
500 });
501 return it != annots.end();
502}
503
dsinclair8afe15a2016-10-05 12:00:34 -0700504bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
505 if (!p)
506 return false;
507 return pdfium::ContainsValue(m_SDKAnnotArray, p);
508}
509
dsinclairf34518b2016-09-13 12:03:48 -0700510CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700511 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700512 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700513}
514
515int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700516 CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict.Get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700517 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700518 return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
519}