blob: c1b522123514a06b452c7ea43e95f4fa2c3b9a3b [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
Ryan Harrison854d71c2017-10-18 12:28:14 -040091 if (pPage->GetContext()->GetFormType() == FormType::kXFAFull) {
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();
Ryan Harrison854d71c2017-10-18 12:28:14 -0400179 if (!pPage || !pPage->GetContext()->ContainsXFAForm())
tsepez4cf55152016-11-02 14:37:54 -0700180 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700181
Tom Sepezbfa2a972017-07-24 11:38:31 -0700182 CPDFSDK_Annot::ObservedPtr pObserved(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700183 if (GetFocusAnnot() == pAnnot)
Tom Sepezbfa2a972017-07-24 11:38:31 -0700184 m_pFormFillEnv->KillFocusAnnot(0); // May invoke JS, invalidating pAnnot.
185
186 if (pObserved) {
187 CPDFSDK_AnnotHandlerMgr* pAnnotHandler =
188 m_pFormFillEnv->GetAnnotHandlerMgr();
189 if (pAnnotHandler)
190 pAnnotHandler->ReleaseAnnot(pObserved.Get());
191 }
dsinclairf34518b2016-09-13 12:03:48 -0700192
dsinclair8afe15a2016-10-05 12:00:34 -0700193 auto it = std::find(m_SDKAnnotArray.begin(), m_SDKAnnotArray.end(), pAnnot);
194 if (it != m_SDKAnnotArray.end())
195 m_SDKAnnotArray.erase(it);
tsepezf8074ce2016-09-27 14:29:57 -0700196 if (m_pCaptureWidget.Get() == pAnnot)
197 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700198
tsepez4cf55152016-11-02 14:37:54 -0700199 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700200}
dsinclair8afe15a2016-10-05 12:00:34 -0700201#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -0700202
203CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
204 if (m_page) {
205#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700206 return m_page->GetContext()->GetPDFDoc();
dsinclairf34518b2016-09-13 12:03:48 -0700207#else // PDF_ENABLE_XFA
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700208 return m_page->m_pDocument.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700209#endif // PDF_ENABLE_XFA
210 }
211 return nullptr;
212}
213
214CPDF_Page* CPDFSDK_PageView::GetPDFPage() const {
215#ifdef PDF_ENABLE_XFA
216 return m_page ? m_page->GetPDFPage() : nullptr;
217#else // PDF_ENABLE_XFA
218 return m_page;
219#endif // PDF_ENABLE_XFA
220}
221
dsinclairf34518b2016-09-13 12:03:48 -0700222CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
dsinclair8afe15a2016-10-05 12:00:34 -0700223 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700224 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
225 return pAnnot;
226 }
227 return nullptr;
228}
229
230#ifdef PDF_ENABLE_XFA
231CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(CXFA_FFWidget* hWidget) {
232 if (!hWidget)
233 return nullptr;
234
dsinclair8afe15a2016-10-05 12:00:34 -0700235 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700236 if (pAnnot->GetXFAWidget() == hWidget)
237 return pAnnot;
238 }
239 return nullptr;
240}
241#endif // PDF_ENABLE_XFA
242
Ryan Harrison275e2602017-09-18 14:23:18 -0400243WideString CPDFSDK_PageView::GetSelectedText() {
Diana Gagedce2d722017-06-20 11:17:11 -0700244 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
245 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
246 m_pFormFillEnv->GetAnnotHandlerMgr();
247 return pAnnotHandlerMgr->Annot_GetSelectedText(pAnnot);
248 }
249
Ryan Harrison275e2602017-09-18 14:23:18 -0400250 return WideString();
Diana Gagedce2d722017-06-20 11:17:11 -0700251}
252
Ryan Harrison275e2602017-09-18 14:23:18 -0400253void CPDFSDK_PageView::ReplaceSelection(const WideString& text) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700254 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
255 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
256 m_pFormFillEnv->GetAnnotHandlerMgr();
Diana Gageab390972017-07-28 17:07:39 -0700257 pAnnotHandlerMgr->Annot_ReplaceSelection(pAnnot, text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700258 }
259}
260
Lei Zhang63b01262017-08-31 08:54:46 -0700261bool CPDFSDK_PageView::OnFocus(const CFX_PointF& point, uint32_t nFlag) {
262 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
263 if (!pAnnot) {
264 m_pFormFillEnv->KillFocusAnnot(nFlag);
265 return false;
266 }
267
268 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
269 return true;
270}
271
Dan Sinclairf528eee2017-02-14 11:52:07 -0500272bool CPDFSDK_PageView::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500273 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700274 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700275 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700276 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700277 }
278
dsinclairb402b172016-10-11 09:26:32 -0700279 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
280 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700281 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700282 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700283
284 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700285 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700286
dsinclair7cbe68e2016-10-12 11:56:23 -0700287 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700288 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700289}
290
291#ifdef PDF_ENABLE_XFA
Dan Sinclairf528eee2017-02-14 11:52:07 -0500292bool CPDFSDK_PageView::OnRButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500293 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700294 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700295 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700296
dsinclairb402b172016-10-11 09:26:32 -0700297 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
298 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700299 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700300 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700301 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700302
303 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700304 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700305
tsepez4cf55152016-11-02 14:37:54 -0700306 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700307}
308
Dan Sinclairf528eee2017-02-14 11:52:07 -0500309bool CPDFSDK_PageView::OnRButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700310 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
311 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500312 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
dsinclairf34518b2016-09-13 12:03:48 -0700313 if (!pFXAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700314 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700315
tsepezf8074ce2016-09-27 14:29:57 -0700316 if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
dsinclair7cbe68e2016-10-12 11:56:23 -0700317 m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700318
tsepez4cf55152016-11-02 14:37:54 -0700319 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700320}
321#endif // PDF_ENABLE_XFA
322
Dan Sinclairf528eee2017-02-14 11:52:07 -0500323bool CPDFSDK_PageView::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700324 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
325 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500326 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700327 CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot());
dsinclairf34518b2016-09-13 12:03:48 -0700328 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
329 // Last focus Annot gets a chance to handle the event.
tsepezf8074ce2016-09-27 14:29:57 -0700330 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700331 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700332 }
tsepezf8074ce2016-09-27 14:29:57 -0700333 return pFXAnnot &&
334 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
dsinclairf34518b2016-09-13 12:03:48 -0700335}
336
Dan Sinclairf528eee2017-02-14 11:52:07 -0500337bool CPDFSDK_PageView::OnMouseMove(const CFX_PointF& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700338 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
339 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500340 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point));
Henrique Nakashima65513622017-08-16 14:48:13 -0400341
Henrique Nakashima4a652542017-08-22 10:59:49 -0400342 if (m_bOnWidget && m_pCaptureWidget != pFXAnnot)
343 ExitWidget(pAnnotHandlerMgr, true, nFlag);
344
345 if (pFXAnnot) {
346 if (!m_bOnWidget) {
347 EnterWidget(pAnnotHandlerMgr, &pFXAnnot, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400348
349 // Annot_OnMouseEnter may have invalidated pFXAnnot.
350 if (!pFXAnnot) {
Henrique Nakashima4a652542017-08-22 10:59:49 -0400351 ExitWidget(pAnnotHandlerMgr, false, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400352 return true;
353 }
dsinclairf34518b2016-09-13 12:03:48 -0700354 }
Henrique Nakashima4a652542017-08-22 10:59:49 -0400355
tsepezf8074ce2016-09-27 14:29:57 -0700356 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
tsepez4cf55152016-11-02 14:37:54 -0700357 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700358 }
Henrique Nakashima65513622017-08-16 14:48:13 -0400359
tsepez4cf55152016-11-02 14:37:54 -0700360 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700361}
362
Henrique Nakashima4a652542017-08-22 10:59:49 -0400363void CPDFSDK_PageView::EnterWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
364 CPDFSDK_Annot::ObservedPtr* pAnnot,
365 uint32_t nFlag) {
366 m_bOnWidget = true;
367 m_pCaptureWidget.Reset(pAnnot->Get());
368 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pAnnot, nFlag);
369}
370
371void CPDFSDK_PageView::ExitWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
372 bool callExitCallback,
373 uint32_t nFlag) {
374 m_bOnWidget = false;
375 if (m_pCaptureWidget) {
376 if (callExitCallback)
377 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
378
379 m_pCaptureWidget.Reset();
380 }
381}
382
tsepez4cf55152016-11-02 14:37:54 -0700383bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
384 double deltaY,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500385 const CFX_PointF& point,
tsepez4cf55152016-11-02 14:37:54 -0700386 int nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500387 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700388 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700389 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700390
dsinclairb402b172016-10-11 09:26:32 -0700391 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
392 m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang222e1a42017-06-20 14:47:00 -0700393 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag,
394 static_cast<int>(deltaY), point);
dsinclairf34518b2016-09-13 12:03:48 -0700395}
396
tsepez4cf55152016-11-02 14:37:54 -0700397bool CPDFSDK_PageView::OnChar(int nChar, uint32_t 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_OnChar(pAnnot, nChar, nFlag);
402 }
403
tsepez4cf55152016-11-02 14:37:54 -0700404 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700405}
406
tsepez4cf55152016-11-02 14:37:54 -0700407bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700408 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700409 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
410 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700411 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
412 }
tsepez4cf55152016-11-02 14:37:54 -0700413 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700414}
415
tsepez4cf55152016-11-02 14:37:54 -0700416bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
417 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700418}
419
420void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700421 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
422 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700423
Dan Sinclairb8966992017-09-21 14:51:57 -0400424 AutoRestorer<bool> lock(&m_bLocked);
Henrique Nakashima65513622017-08-16 14:48:13 -0400425 m_bLocked = true;
dsinclairf34518b2016-09-13 12:03:48 -0700426
427#ifdef PDF_ENABLE_XFA
Dan Sinclair0b950422017-09-21 15:49:49 -0400428 RetainPtr<CPDFXFA_Page> protector(m_page);
Ryan Harrison854d71c2017-10-18 12:28:14 -0400429 if (m_pFormFillEnv->GetXFAContext()->GetFormType() == FormType::kXFAFull) {
dsinclairf34518b2016-09-13 12:03:48 -0700430 CXFA_FFPageView* pageView = m_page->GetXFAPageView();
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700431 std::unique_ptr<IXFA_WidgetIterator> pWidgetHandler(
dsinclairf34518b2016-09-13 12:03:48 -0700432 pageView->CreateWidgetIterator(
433 XFA_TRAVERSEWAY_Form,
434 XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable));
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700435 if (!pWidgetHandler) {
dsinclairf34518b2016-09-13 12:03:48 -0700436 return;
437 }
438
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700439 while (CXFA_FFWidget* pXFAAnnot = pWidgetHandler->MoveToNext()) {
dsinclairf34518b2016-09-13 12:03:48 -0700440 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
441 if (!pAnnot)
442 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700443 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700444 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
445 }
446
dsinclairf34518b2016-09-13 12:03:48 -0700447 return;
448 }
449#endif // PDF_ENABLE_XFA
450
451 CPDF_Page* pPage = GetPDFPage();
452 ASSERT(pPage);
thestig7c292e02016-09-28 14:14:26 -0700453 bool bUpdateAP = CPDF_InterForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700454 // Disable the default AP construction.
thestig7c292e02016-09-28 14:14:26 -0700455 CPDF_InterForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700456 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
dsinclairf34518b2016-09-13 12:03:48 -0700457 CPDF_InterForm::SetUpdateAP(bUpdateAP);
458
459 const size_t nCount = m_pAnnotList->Count();
460 for (size_t i = 0; i < nCount; ++i) {
461 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
462 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
463 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
464 if (!pAnnot)
465 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700466 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700467 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
468 }
dsinclairf34518b2016-09-13 12:03:48 -0700469}
470
dsinclairf34518b2016-09-13 12:03:48 -0700471void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700472 for (const auto& rc : rects)
Lei Zhang77f9bff2017-08-29 11:34:12 -0700473 m_pFormFillEnv->Invalidate(m_page, rc.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700474}
475
476void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
477 CFX_FloatRect rcWindow = pAnnot->GetRect();
Lei Zhang77f9bff2017-08-29 11:34:12 -0700478 m_pFormFillEnv->Invalidate(m_page, rcWindow.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700479}
480
481int CPDFSDK_PageView::GetPageIndex() const {
482 if (!m_page)
483 return -1;
484
485#ifdef PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400486 switch (m_page->GetContext()->GetFormType()) {
487 case FormType::kXFAFull: {
dsinclairf34518b2016-09-13 12:03:48 -0700488 CXFA_FFPageView* pPageView = m_page->GetXFAPageView();
489 return pPageView ? pPageView->GetPageIndex() : -1;
490 }
Ryan Harrison854d71c2017-10-18 12:28:14 -0400491 case FormType::kNone:
492 case FormType::kAcroForm:
493 case FormType::kXFAForeground:
494 break;
dsinclairf34518b2016-09-13 12:03:48 -0700495 }
dsinclairf34518b2016-09-13 12:03:48 -0700496#endif // PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400497 return GetPageIndexForStaticPDF();
dsinclairf34518b2016-09-13 12:03:48 -0700498}
499
500bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
501 if (!p)
502 return false;
503
504 const auto& annots = m_pAnnotList->All();
505 auto it = std::find_if(annots.begin(), annots.end(),
506 [p](const std::unique_ptr<CPDF_Annot>& annot) {
507 return annot.get() == p;
508 });
509 return it != annots.end();
510}
511
dsinclair8afe15a2016-10-05 12:00:34 -0700512bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
513 if (!p)
514 return false;
515 return pdfium::ContainsValue(m_SDKAnnotArray, p);
516}
517
dsinclairf34518b2016-09-13 12:03:48 -0700518CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700519 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700520 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700521}
522
523int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700524 CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict.Get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700525 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700526 return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
527}