blob: f46c74f79a7d60a648eef422db36cbd4ab06a754 [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 Sinclairb8966992017-09-21 14:51:57 -040016#include "core/fxcrt/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(
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400120 this, pSDKAnnot.Get(), pDevice, pUser2Device,
121 pOptions->GetDrawAnnots());
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
Ryan Harrison275e2602017-09-18 14:23:18 -0400245WideString CPDFSDK_PageView::GetSelectedText() {
Diana Gagedce2d722017-06-20 11:17:11 -0700246 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
247 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
248 m_pFormFillEnv->GetAnnotHandlerMgr();
249 return pAnnotHandlerMgr->Annot_GetSelectedText(pAnnot);
250 }
251
Ryan Harrison275e2602017-09-18 14:23:18 -0400252 return WideString();
Diana Gagedce2d722017-06-20 11:17:11 -0700253}
254
Ryan Harrison275e2602017-09-18 14:23:18 -0400255void CPDFSDK_PageView::ReplaceSelection(const 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
Lei Zhang63b01262017-08-31 08:54:46 -0700263bool CPDFSDK_PageView::OnFocus(const CFX_PointF& point, uint32_t nFlag) {
264 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
265 if (!pAnnot) {
266 m_pFormFillEnv->KillFocusAnnot(nFlag);
267 return false;
268 }
269
270 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
271 return true;
272}
273
Dan Sinclairf528eee2017-02-14 11:52:07 -0500274bool CPDFSDK_PageView::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500275 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700276 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700277 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700278 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700279 }
280
dsinclairb402b172016-10-11 09:26:32 -0700281 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
282 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700283 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700284 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700285
286 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700287 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700288
dsinclair7cbe68e2016-10-12 11:56:23 -0700289 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700290 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700291}
292
293#ifdef PDF_ENABLE_XFA
Dan Sinclairf528eee2017-02-14 11:52:07 -0500294bool CPDFSDK_PageView::OnRButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500295 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700296 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700297 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700298
dsinclairb402b172016-10-11 09:26:32 -0700299 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
300 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700301 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700302 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700303 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700304
305 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700306 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700307
tsepez4cf55152016-11-02 14:37:54 -0700308 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700309}
310
Dan Sinclairf528eee2017-02-14 11:52:07 -0500311bool CPDFSDK_PageView::OnRButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700312 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
313 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500314 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
dsinclairf34518b2016-09-13 12:03:48 -0700315 if (!pFXAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700316 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700317
tsepezf8074ce2016-09-27 14:29:57 -0700318 if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
dsinclair7cbe68e2016-10-12 11:56:23 -0700319 m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700320
tsepez4cf55152016-11-02 14:37:54 -0700321 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700322}
323#endif // PDF_ENABLE_XFA
324
Dan Sinclairf528eee2017-02-14 11:52:07 -0500325bool CPDFSDK_PageView::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700326 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
327 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500328 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700329 CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot());
dsinclairf34518b2016-09-13 12:03:48 -0700330 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
331 // Last focus Annot gets a chance to handle the event.
tsepezf8074ce2016-09-27 14:29:57 -0700332 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700333 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700334 }
tsepezf8074ce2016-09-27 14:29:57 -0700335 return pFXAnnot &&
336 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
dsinclairf34518b2016-09-13 12:03:48 -0700337}
338
Dan Sinclairf528eee2017-02-14 11:52:07 -0500339bool CPDFSDK_PageView::OnMouseMove(const CFX_PointF& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700340 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
341 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500342 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point));
Henrique Nakashima65513622017-08-16 14:48:13 -0400343
Henrique Nakashima4a652542017-08-22 10:59:49 -0400344 if (m_bOnWidget && m_pCaptureWidget != pFXAnnot)
345 ExitWidget(pAnnotHandlerMgr, true, nFlag);
346
347 if (pFXAnnot) {
348 if (!m_bOnWidget) {
349 EnterWidget(pAnnotHandlerMgr, &pFXAnnot, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400350
351 // Annot_OnMouseEnter may have invalidated pFXAnnot.
352 if (!pFXAnnot) {
Henrique Nakashima4a652542017-08-22 10:59:49 -0400353 ExitWidget(pAnnotHandlerMgr, false, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400354 return true;
355 }
dsinclairf34518b2016-09-13 12:03:48 -0700356 }
Henrique Nakashima4a652542017-08-22 10:59:49 -0400357
tsepezf8074ce2016-09-27 14:29:57 -0700358 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
tsepez4cf55152016-11-02 14:37:54 -0700359 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700360 }
Henrique Nakashima65513622017-08-16 14:48:13 -0400361
tsepez4cf55152016-11-02 14:37:54 -0700362 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700363}
364
Henrique Nakashima4a652542017-08-22 10:59:49 -0400365void CPDFSDK_PageView::EnterWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
366 CPDFSDK_Annot::ObservedPtr* pAnnot,
367 uint32_t nFlag) {
368 m_bOnWidget = true;
369 m_pCaptureWidget.Reset(pAnnot->Get());
370 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pAnnot, nFlag);
371}
372
373void CPDFSDK_PageView::ExitWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
374 bool callExitCallback,
375 uint32_t nFlag) {
376 m_bOnWidget = false;
377 if (m_pCaptureWidget) {
378 if (callExitCallback)
379 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
380
381 m_pCaptureWidget.Reset();
382 }
383}
384
tsepez4cf55152016-11-02 14:37:54 -0700385bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
386 double deltaY,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500387 const CFX_PointF& point,
tsepez4cf55152016-11-02 14:37:54 -0700388 int nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500389 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700390 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700391 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700392
dsinclairb402b172016-10-11 09:26:32 -0700393 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
394 m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang222e1a42017-06-20 14:47:00 -0700395 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag,
396 static_cast<int>(deltaY), point);
dsinclairf34518b2016-09-13 12:03:48 -0700397}
398
tsepez4cf55152016-11-02 14:37:54 -0700399bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700400 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700401 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
402 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700403 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
404 }
405
tsepez4cf55152016-11-02 14:37:54 -0700406 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700407}
408
tsepez4cf55152016-11-02 14:37:54 -0700409bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700410 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700411 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
412 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700413 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
414 }
tsepez4cf55152016-11-02 14:37:54 -0700415 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700416}
417
tsepez4cf55152016-11-02 14:37:54 -0700418bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
419 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700420}
421
422void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700423 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
424 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700425
Dan Sinclairb8966992017-09-21 14:51:57 -0400426 AutoRestorer<bool> lock(&m_bLocked);
Henrique Nakashima65513622017-08-16 14:48:13 -0400427 m_bLocked = true;
dsinclairf34518b2016-09-13 12:03:48 -0700428
429#ifdef PDF_ENABLE_XFA
Dan Sinclair0b950422017-09-21 15:49:49 -0400430 RetainPtr<CPDFXFA_Page> protector(m_page);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400431 if (m_pFormFillEnv->GetXFAContext()->GetDocType() == XFA_DocType::Dynamic) {
dsinclairf34518b2016-09-13 12:03:48 -0700432 CXFA_FFPageView* pageView = m_page->GetXFAPageView();
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700433 std::unique_ptr<IXFA_WidgetIterator> pWidgetHandler(
dsinclairf34518b2016-09-13 12:03:48 -0700434 pageView->CreateWidgetIterator(
435 XFA_TRAVERSEWAY_Form,
436 XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable));
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700437 if (!pWidgetHandler) {
dsinclairf34518b2016-09-13 12:03:48 -0700438 return;
439 }
440
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700441 while (CXFA_FFWidget* pXFAAnnot = pWidgetHandler->MoveToNext()) {
dsinclairf34518b2016-09-13 12:03:48 -0700442 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
443 if (!pAnnot)
444 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700445 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700446 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
447 }
448
dsinclairf34518b2016-09-13 12:03:48 -0700449 return;
450 }
451#endif // PDF_ENABLE_XFA
452
453 CPDF_Page* pPage = GetPDFPage();
454 ASSERT(pPage);
thestig7c292e02016-09-28 14:14:26 -0700455 bool bUpdateAP = CPDF_InterForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700456 // Disable the default AP construction.
thestig7c292e02016-09-28 14:14:26 -0700457 CPDF_InterForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700458 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
dsinclairf34518b2016-09-13 12:03:48 -0700459 CPDF_InterForm::SetUpdateAP(bUpdateAP);
460
461 const size_t nCount = m_pAnnotList->Count();
462 for (size_t i = 0; i < nCount; ++i) {
463 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
464 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
465 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
466 if (!pAnnot)
467 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700468 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700469 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
470 }
dsinclairf34518b2016-09-13 12:03:48 -0700471}
472
dsinclairf34518b2016-09-13 12:03:48 -0700473void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700474 for (const auto& rc : rects)
Lei Zhang77f9bff2017-08-29 11:34:12 -0700475 m_pFormFillEnv->Invalidate(m_page, rc.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700476}
477
478void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
479 CFX_FloatRect rcWindow = pAnnot->GetRect();
Lei Zhang77f9bff2017-08-29 11:34:12 -0700480 m_pFormFillEnv->Invalidate(m_page, rcWindow.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700481}
482
483int CPDFSDK_PageView::GetPageIndex() const {
484 if (!m_page)
485 return -1;
486
487#ifdef PDF_ENABLE_XFA
Dan Sinclaircdba7472017-03-23 09:17:10 -0400488 switch (m_page->GetContext()->GetDocType()) {
489 case XFA_DocType::Dynamic: {
dsinclairf34518b2016-09-13 12:03:48 -0700490 CXFA_FFPageView* pPageView = m_page->GetXFAPageView();
491 return pPageView ? pPageView->GetPageIndex() : -1;
492 }
Dan Sinclaircdba7472017-03-23 09:17:10 -0400493 case XFA_DocType::Static:
494 case XFA_DocType::PDF:
dsinclairf34518b2016-09-13 12:03:48 -0700495 return GetPageIndexForStaticPDF();
496 default:
497 return -1;
498 }
499#else // PDF_ENABLE_XFA
500 return GetPageIndexForStaticPDF();
501#endif // PDF_ENABLE_XFA
502}
503
504bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
505 if (!p)
506 return false;
507
508 const auto& annots = m_pAnnotList->All();
509 auto it = std::find_if(annots.begin(), annots.end(),
510 [p](const std::unique_ptr<CPDF_Annot>& annot) {
511 return annot.get() == p;
512 });
513 return it != annots.end();
514}
515
dsinclair8afe15a2016-10-05 12:00:34 -0700516bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
517 if (!p)
518 return false;
519 return pdfium::ContainsValue(m_SDKAnnotArray, p);
520}
521
dsinclairf34518b2016-09-13 12:03:48 -0700522CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700523 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700524 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700525}
526
527int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700528 CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict.Get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700529 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700530 return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
531}