blob: 516e0641dab6fafb7faef8fe748db1f16ba8b836 [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"
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_bEnterWidget(false),
tsepez4cf55152016-11-02 14:37:54 -070040 m_bOnWidget(false),
41 m_bValid(false),
42 m_bLocked(false),
dsinclairbcf46232016-10-03 13:02:27 -070043 m_bBeingDestroyed(false) {
dsinclair7cbe68e2016-10-12 11:56:23 -070044 CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
Lei Zhang222e1a42017-06-20 14:47:00 -070045 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
dsinclairf34518b2016-09-13 12:03:48 -070046#ifdef PDF_ENABLE_XFA
Lei Zhang222e1a42017-06-20 14:47:00 -070047 if (page->GetPDFPage())
48 pPDFInterForm->FixPageFields(page->GetPDFPage());
dsinclairf34518b2016-09-13 12:03:48 -070049#else // PDF_ENABLE_XFA
Lei Zhang222e1a42017-06-20 14:47:00 -070050 pPDFInterForm->FixPageFields(page);
dsinclairf34518b2016-09-13 12:03:48 -070051 m_page->SetView(this);
52#endif // PDF_ENABLE_XFA
53}
54
55CPDFSDK_PageView::~CPDFSDK_PageView() {
56#ifndef PDF_ENABLE_XFA
57 // The call to |ReleaseAnnot| can cause the page pointed to by |m_page| to
58 // be freed, which will cause issues if we try to cleanup the pageview pointer
59 // in |m_page|. So, reset the pageview pointer before doing anything else.
60 m_page->SetView(nullptr);
61#endif // PDF_ENABLE_XFA
62
dsinclairb402b172016-10-11 09:26:32 -070063 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
64 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclair8afe15a2016-10-05 12:00:34 -070065 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray)
dsinclairf34518b2016-09-13 12:03:48 -070066 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
67
dsinclair8afe15a2016-10-05 12:00:34 -070068 m_SDKAnnotArray.clear();
dsinclairf34518b2016-09-13 12:03:48 -070069 m_pAnnotList.reset();
70
71#ifndef PDF_ENABLE_XFA
72 if (m_bOwnsPage)
73 delete m_page;
74#endif // PDF_ENABLE_XFA
75}
76
77void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
78 CFX_Matrix* pUser2Device,
79#ifdef PDF_ENABLE_XFA
80 CPDF_RenderOptions* pOptions,
81 const FX_RECT& pClip) {
82#else
83 CPDF_RenderOptions* pOptions) {
84#endif // PDF_ENABLE_XFA
85 m_curMatrix = *pUser2Device;
dsinclairf34518b2016-09-13 12:03:48 -070086
87#ifdef PDF_ENABLE_XFA
88 CPDFXFA_Page* pPage = GetPDFXFAPage();
89 if (!pPage)
90 return;
91
Dan Sinclaircdba7472017-03-23 09:17:10 -040092 if (pPage->GetContext()->GetDocType() == XFA_DocType::Dynamic) {
Dan Sinclair05df0752017-03-14 14:43:42 -040093 CFX_RectF rectClip(
94 static_cast<float>(pClip.left), static_cast<float>(pClip.top),
95 static_cast<float>(pClip.Width()), static_cast<float>(pClip.Height()));
Dan Sinclaircf53b782017-05-04 14:08:01 -040096
Dan Sinclair2b918c82017-07-13 14:47:10 -040097 CXFA_Graphics gs(pDevice);
dsinclairf34518b2016-09-13 12:03:48 -070098 gs.SetClipRect(rectClip);
Dan Sinclaircf53b782017-05-04 14:08:01 -040099
dsinclairf34518b2016-09-13 12:03:48 -0700100 CXFA_FFPageView* xfaView = pPage->GetXFAPageView();
Dan Sinclaircf53b782017-05-04 14:08:01 -0400101 CXFA_RenderContext renderContext(xfaView, rectClip, *pUser2Device);
102 renderContext.DoRender(&gs);
103
dsinclairf34518b2016-09-13 12:03:48 -0700104 CXFA_FFDocView* docView = xfaView->GetDocView();
105 if (!docView)
106 return;
107 CPDFSDK_Annot* annot = GetFocusAnnot();
108 if (!annot)
109 return;
110 // Render the focus widget
111 docView->GetWidgetHandler()->RenderWidget(annot->GetXFAWidget(), &gs,
Lei Zhang7c9d4c22017-08-17 13:53:52 -0700112 *pUser2Device, false);
dsinclairf34518b2016-09-13 12:03:48 -0700113 return;
114 }
115#endif // PDF_ENABLE_XFA
116
117 // for pdf/static xfa.
tsepezd805eec2017-01-11 14:03:54 -0800118 CPDFSDK_AnnotIteration annotIteration(this, true);
119 for (const auto& pSDKAnnot : annotIteration) {
120 m_pFormFillEnv->GetAnnotHandlerMgr()->Annot_OnDraw(
121 this, pSDKAnnot.Get(), pDevice, pUser2Device, pOptions->m_bDrawAnnots);
dsinclairf34518b2016-09-13 12:03:48 -0700122 }
123}
124
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500125CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(const CFX_PointF& point) {
dsinclairb402b172016-10-11 09:26:32 -0700126 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800127 CPDFSDK_AnnotIteration annotIteration(this, false);
128 for (const auto& pSDKAnnot : annotIteration) {
129 CFX_FloatRect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
dsinclairf34518b2016-09-13 12:03:48 -0700130 if (pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::POPUP)
131 continue;
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500132 if (rc.Contains(point))
tsepezd805eec2017-01-11 14:03:54 -0800133 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700134 }
dsinclairf34518b2016-09-13 12:03:48 -0700135 return nullptr;
136}
137
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500138CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(const CFX_PointF& point) {
dsinclairb402b172016-10-11 09:26:32 -0700139 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800140 CPDFSDK_AnnotIteration annotIteration(this, false);
141 for (const auto& pSDKAnnot : annotIteration) {
dsinclairf34518b2016-09-13 12:03:48 -0700142 bool bHitTest = pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET;
143#ifdef PDF_ENABLE_XFA
144 bHitTest = bHitTest ||
145 pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::XFAWIDGET;
146#endif // PDF_ENABLE_XFA
147 if (bHitTest) {
tsepezd805eec2017-01-11 14:03:54 -0800148 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500149 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot.Get(), point))
tsepezd805eec2017-01-11 14:03:54 -0800150 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700151 }
152 }
dsinclairf34518b2016-09-13 12:03:48 -0700153 return nullptr;
154}
155
dsinclairf34518b2016-09-13 12:03:48 -0700156#ifdef PDF_ENABLE_XFA
157CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CXFA_FFWidget* pPDFAnnot) {
158 if (!pPDFAnnot)
159 return nullptr;
160
161 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
162 if (pSDKAnnot)
163 return pSDKAnnot;
164
dsinclairb402b172016-10-11 09:26:32 -0700165 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700166 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
167 if (!pSDKAnnot)
168 return nullptr;
169
dsinclair8afe15a2016-10-05 12:00:34 -0700170 m_SDKAnnotArray.push_back(pSDKAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700171 return pSDKAnnot;
172}
dsinclairf34518b2016-09-13 12:03:48 -0700173
tsepez4cf55152016-11-02 14:37:54 -0700174bool CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
dsinclairf34518b2016-09-13 12:03:48 -0700175 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700176 return false;
Tom Sepezbfa2a972017-07-24 11:38:31 -0700177
dsinclairf34518b2016-09-13 12:03:48 -0700178 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
Dan Sinclaircdba7472017-03-23 09:17:10 -0400179 if (!pPage || (pPage->GetContext()->GetDocType() != XFA_DocType::Static &&
180 pPage->GetContext()->GetDocType() != XFA_DocType::Dynamic)) {
tsepez4cf55152016-11-02 14:37:54 -0700181 return false;
Dan Sinclaircdba7472017-03-23 09:17:10 -0400182 }
dsinclairf34518b2016-09-13 12:03:48 -0700183
Tom Sepezbfa2a972017-07-24 11:38:31 -0700184 CPDFSDK_Annot::ObservedPtr pObserved(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700185 if (GetFocusAnnot() == pAnnot)
Tom Sepezbfa2a972017-07-24 11:38:31 -0700186 m_pFormFillEnv->KillFocusAnnot(0); // May invoke JS, invalidating pAnnot.
187
188 if (pObserved) {
189 CPDFSDK_AnnotHandlerMgr* pAnnotHandler =
190 m_pFormFillEnv->GetAnnotHandlerMgr();
191 if (pAnnotHandler)
192 pAnnotHandler->ReleaseAnnot(pObserved.Get());
193 }
dsinclairf34518b2016-09-13 12:03:48 -0700194
dsinclair8afe15a2016-10-05 12:00:34 -0700195 auto it = std::find(m_SDKAnnotArray.begin(), m_SDKAnnotArray.end(), pAnnot);
196 if (it != m_SDKAnnotArray.end())
197 m_SDKAnnotArray.erase(it);
tsepezf8074ce2016-09-27 14:29:57 -0700198 if (m_pCaptureWidget.Get() == pAnnot)
199 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700200
tsepez4cf55152016-11-02 14:37:54 -0700201 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700202}
dsinclair8afe15a2016-10-05 12:00:34 -0700203#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -0700204
205CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
206 if (m_page) {
207#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700208 return m_page->GetContext()->GetPDFDoc();
dsinclairf34518b2016-09-13 12:03:48 -0700209#else // PDF_ENABLE_XFA
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700210 return m_page->m_pDocument.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700211#endif // PDF_ENABLE_XFA
212 }
213 return nullptr;
214}
215
216CPDF_Page* CPDFSDK_PageView::GetPDFPage() const {
217#ifdef PDF_ENABLE_XFA
218 return m_page ? m_page->GetPDFPage() : nullptr;
219#else // PDF_ENABLE_XFA
220 return m_page;
221#endif // PDF_ENABLE_XFA
222}
223
dsinclairf34518b2016-09-13 12:03:48 -0700224CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
dsinclair8afe15a2016-10-05 12:00:34 -0700225 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700226 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
227 return pAnnot;
228 }
229 return nullptr;
230}
231
232#ifdef PDF_ENABLE_XFA
233CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(CXFA_FFWidget* hWidget) {
234 if (!hWidget)
235 return nullptr;
236
dsinclair8afe15a2016-10-05 12:00:34 -0700237 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700238 if (pAnnot->GetXFAWidget() == hWidget)
239 return pAnnot;
240 }
241 return nullptr;
242}
243#endif // PDF_ENABLE_XFA
244
Diana Gagedce2d722017-06-20 11:17:11 -0700245CFX_WideString CPDFSDK_PageView::GetSelectedText() {
246 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
247 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
248 m_pFormFillEnv->GetAnnotHandlerMgr();
249 return pAnnotHandlerMgr->Annot_GetSelectedText(pAnnot);
250 }
251
252 return CFX_WideString();
253}
254
Diana Gageab390972017-07-28 17:07:39 -0700255void CPDFSDK_PageView::ReplaceSelection(const CFX_WideString& text) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700256 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
257 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
258 m_pFormFillEnv->GetAnnotHandlerMgr();
Diana Gageab390972017-07-28 17:07:39 -0700259 pAnnotHandlerMgr->Annot_ReplaceSelection(pAnnot, text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700260 }
261}
262
Dan Sinclairf528eee2017-02-14 11:52:07 -0500263bool CPDFSDK_PageView::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500264 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700265 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700266 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700267 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700268 }
269
dsinclairb402b172016-10-11 09:26:32 -0700270 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
271 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700272 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700273 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700274
275 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700276 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700277
dsinclair7cbe68e2016-10-12 11:56:23 -0700278 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700279 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700280}
281
282#ifdef PDF_ENABLE_XFA
Dan Sinclairf528eee2017-02-14 11:52:07 -0500283bool CPDFSDK_PageView::OnRButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500284 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700285 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700286 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700287
dsinclairb402b172016-10-11 09:26:32 -0700288 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
289 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700290 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700291 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700292 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700293
294 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700295 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700296
tsepez4cf55152016-11-02 14:37:54 -0700297 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700298}
299
Dan Sinclairf528eee2017-02-14 11:52:07 -0500300bool CPDFSDK_PageView::OnRButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700301 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
302 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500303 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
dsinclairf34518b2016-09-13 12:03:48 -0700304 if (!pFXAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700305 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700306
tsepezf8074ce2016-09-27 14:29:57 -0700307 if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
dsinclair7cbe68e2016-10-12 11:56:23 -0700308 m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700309
tsepez4cf55152016-11-02 14:37:54 -0700310 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700311}
312#endif // PDF_ENABLE_XFA
313
Dan Sinclairf528eee2017-02-14 11:52:07 -0500314bool CPDFSDK_PageView::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700315 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
316 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500317 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700318 CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot());
dsinclairf34518b2016-09-13 12:03:48 -0700319 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
320 // Last focus Annot gets a chance to handle the event.
tsepezf8074ce2016-09-27 14:29:57 -0700321 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700322 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700323 }
tsepezf8074ce2016-09-27 14:29:57 -0700324 return pFXAnnot &&
325 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
dsinclairf34518b2016-09-13 12:03:48 -0700326}
327
Dan Sinclairf528eee2017-02-14 11:52:07 -0500328bool CPDFSDK_PageView::OnMouseMove(const CFX_PointF& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700329 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
330 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500331 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700332 if (pFXAnnot) {
333 if (m_pCaptureWidget && m_pCaptureWidget != pFXAnnot) {
tsepez4cf55152016-11-02 14:37:54 -0700334 m_bEnterWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700335 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
dsinclairf34518b2016-09-13 12:03:48 -0700336 }
tsepez4cf55152016-11-02 14:37:54 -0700337 m_bOnWidget = true;
Henrique Nakashima65513622017-08-16 14:48:13 -0400338 m_pCaptureWidget.Reset(pFXAnnot.Get());
339
dsinclairf34518b2016-09-13 12:03:48 -0700340 if (!m_bEnterWidget) {
tsepez4cf55152016-11-02 14:37:54 -0700341 m_bEnterWidget = true;
tsepezf8074ce2016-09-27 14:29:57 -0700342 pAnnotHandlerMgr->Annot_OnMouseEnter(this, &pFXAnnot, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400343
344 // Annot_OnMouseEnter may have invalidated pFXAnnot.
345 if (!pFXAnnot) {
346 m_bOnWidget = false;
347 m_bEnterWidget = false;
348 m_pCaptureWidget.Reset();
349 return true;
350 }
dsinclairf34518b2016-09-13 12:03:48 -0700351 }
tsepezf8074ce2016-09-27 14:29:57 -0700352 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
tsepez4cf55152016-11-02 14:37:54 -0700353 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700354 }
Henrique Nakashima65513622017-08-16 14:48:13 -0400355
dsinclairf34518b2016-09-13 12:03:48 -0700356 if (m_bOnWidget) {
tsepez4cf55152016-11-02 14:37:54 -0700357 m_bOnWidget = false;
tsepez4cf55152016-11-02 14:37:54 -0700358 m_bEnterWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700359 if (m_pCaptureWidget) {
360 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
361 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700362 }
363 }
Henrique Nakashima65513622017-08-16 14:48:13 -0400364
tsepez4cf55152016-11-02 14:37:54 -0700365 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700366}
367
tsepez4cf55152016-11-02 14:37:54 -0700368bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
369 double deltaY,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500370 const CFX_PointF& point,
tsepez4cf55152016-11-02 14:37:54 -0700371 int nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500372 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700373 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700374 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700375
dsinclairb402b172016-10-11 09:26:32 -0700376 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
377 m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang222e1a42017-06-20 14:47:00 -0700378 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag,
379 static_cast<int>(deltaY), point);
dsinclairf34518b2016-09-13 12:03:48 -0700380}
381
tsepez4cf55152016-11-02 14:37:54 -0700382bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700383 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700384 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
385 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700386 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
387 }
388
tsepez4cf55152016-11-02 14:37:54 -0700389 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700390}
391
tsepez4cf55152016-11-02 14:37:54 -0700392bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700393 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700394 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
395 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700396 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
397 }
tsepez4cf55152016-11-02 14:37:54 -0700398 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700399}
400
tsepez4cf55152016-11-02 14:37:54 -0700401bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
402 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700403}
404
405void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700406 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
407 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700408
Henrique Nakashima65513622017-08-16 14:48:13 -0400409 CFX_AutoRestorer<bool> lock(&m_bLocked);
410 m_bLocked = true;
dsinclairf34518b2016-09-13 12:03:48 -0700411
412#ifdef PDF_ENABLE_XFA
413 CFX_RetainPtr<CPDFXFA_Page> protector(m_page);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400414 if (m_pFormFillEnv->GetXFAContext()->GetDocType() == XFA_DocType::Dynamic) {
dsinclairf34518b2016-09-13 12:03:48 -0700415 CXFA_FFPageView* pageView = m_page->GetXFAPageView();
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700416 std::unique_ptr<IXFA_WidgetIterator> pWidgetHandler(
dsinclairf34518b2016-09-13 12:03:48 -0700417 pageView->CreateWidgetIterator(
418 XFA_TRAVERSEWAY_Form,
419 XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable));
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700420 if (!pWidgetHandler) {
dsinclairf34518b2016-09-13 12:03:48 -0700421 return;
422 }
423
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700424 while (CXFA_FFWidget* pXFAAnnot = pWidgetHandler->MoveToNext()) {
dsinclairf34518b2016-09-13 12:03:48 -0700425 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
426 if (!pAnnot)
427 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700428 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700429 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
430 }
431
dsinclairf34518b2016-09-13 12:03:48 -0700432 return;
433 }
434#endif // PDF_ENABLE_XFA
435
436 CPDF_Page* pPage = GetPDFPage();
437 ASSERT(pPage);
thestig7c292e02016-09-28 14:14:26 -0700438 bool bUpdateAP = CPDF_InterForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700439 // Disable the default AP construction.
thestig7c292e02016-09-28 14:14:26 -0700440 CPDF_InterForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700441 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
dsinclairf34518b2016-09-13 12:03:48 -0700442 CPDF_InterForm::SetUpdateAP(bUpdateAP);
443
444 const size_t nCount = m_pAnnotList->Count();
445 for (size_t i = 0; i < nCount; ++i) {
446 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
447 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
448 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
449 if (!pAnnot)
450 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700451 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700452 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
453 }
dsinclairf34518b2016-09-13 12:03:48 -0700454}
455
dsinclairf34518b2016-09-13 12:03:48 -0700456void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700457 for (const auto& rc : rects)
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500458 m_pFormFillEnv->Invalidate(m_page, rc.ToFxRect());
dsinclairf34518b2016-09-13 12:03:48 -0700459}
460
461void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
462 CFX_FloatRect rcWindow = pAnnot->GetRect();
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500463 m_pFormFillEnv->Invalidate(m_page, rcWindow.ToFxRect());
dsinclairf34518b2016-09-13 12:03:48 -0700464}
465
466int CPDFSDK_PageView::GetPageIndex() const {
467 if (!m_page)
468 return -1;
469
470#ifdef PDF_ENABLE_XFA
Dan Sinclaircdba7472017-03-23 09:17:10 -0400471 switch (m_page->GetContext()->GetDocType()) {
472 case XFA_DocType::Dynamic: {
dsinclairf34518b2016-09-13 12:03:48 -0700473 CXFA_FFPageView* pPageView = m_page->GetXFAPageView();
474 return pPageView ? pPageView->GetPageIndex() : -1;
475 }
Dan Sinclaircdba7472017-03-23 09:17:10 -0400476 case XFA_DocType::Static:
477 case XFA_DocType::PDF:
dsinclairf34518b2016-09-13 12:03:48 -0700478 return GetPageIndexForStaticPDF();
479 default:
480 return -1;
481 }
482#else // PDF_ENABLE_XFA
483 return GetPageIndexForStaticPDF();
484#endif // PDF_ENABLE_XFA
485}
486
487bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
488 if (!p)
489 return false;
490
491 const auto& annots = m_pAnnotList->All();
492 auto it = std::find_if(annots.begin(), annots.end(),
493 [p](const std::unique_ptr<CPDF_Annot>& annot) {
494 return annot.get() == p;
495 });
496 return it != annots.end();
497}
498
dsinclair8afe15a2016-10-05 12:00:34 -0700499bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
500 if (!p)
501 return false;
502 return pdfium::ContainsValue(m_SDKAnnotArray, p);
503}
504
dsinclairf34518b2016-09-13 12:03:48 -0700505CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700506 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700507 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700508}
509
510int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700511 CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict.Get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700512 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700513 return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
514}