blob: 5bdd720596e8e09151e35c4f741bddac5375c9c4 [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
Lei Zhang63b01262017-08-31 08:54:46 -0700262bool CPDFSDK_PageView::OnFocus(const CFX_PointF& point, uint32_t nFlag) {
263 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
264 if (!pAnnot) {
265 m_pFormFillEnv->KillFocusAnnot(nFlag);
266 return false;
267 }
268
269 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
270 return true;
271}
272
Dan Sinclairf528eee2017-02-14 11:52:07 -0500273bool CPDFSDK_PageView::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500274 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700275 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700276 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700277 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700278 }
279
dsinclairb402b172016-10-11 09:26:32 -0700280 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
281 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700282 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700283 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700284
285 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700286 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700287
dsinclair7cbe68e2016-10-12 11:56:23 -0700288 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700289 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700290}
291
292#ifdef PDF_ENABLE_XFA
Dan Sinclairf528eee2017-02-14 11:52:07 -0500293bool CPDFSDK_PageView::OnRButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500294 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700295 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700296 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700297
dsinclairb402b172016-10-11 09:26:32 -0700298 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
299 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700300 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700301 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700302 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700303
304 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700305 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700306
tsepez4cf55152016-11-02 14:37:54 -0700307 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700308}
309
Dan Sinclairf528eee2017-02-14 11:52:07 -0500310bool CPDFSDK_PageView::OnRButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700311 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
312 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500313 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
dsinclairf34518b2016-09-13 12:03:48 -0700314 if (!pFXAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700315 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700316
tsepezf8074ce2016-09-27 14:29:57 -0700317 if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
dsinclair7cbe68e2016-10-12 11:56:23 -0700318 m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700319
tsepez4cf55152016-11-02 14:37:54 -0700320 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700321}
322#endif // PDF_ENABLE_XFA
323
Dan Sinclairf528eee2017-02-14 11:52:07 -0500324bool CPDFSDK_PageView::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700325 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
326 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500327 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700328 CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot());
dsinclairf34518b2016-09-13 12:03:48 -0700329 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
330 // Last focus Annot gets a chance to handle the event.
tsepezf8074ce2016-09-27 14:29:57 -0700331 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700332 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700333 }
tsepezf8074ce2016-09-27 14:29:57 -0700334 return pFXAnnot &&
335 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
dsinclairf34518b2016-09-13 12:03:48 -0700336}
337
Dan Sinclairf528eee2017-02-14 11:52:07 -0500338bool CPDFSDK_PageView::OnMouseMove(const CFX_PointF& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700339 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
340 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500341 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point));
Henrique Nakashima65513622017-08-16 14:48:13 -0400342
Henrique Nakashima4a652542017-08-22 10:59:49 -0400343 if (m_bOnWidget && m_pCaptureWidget != pFXAnnot)
344 ExitWidget(pAnnotHandlerMgr, true, nFlag);
345
346 if (pFXAnnot) {
347 if (!m_bOnWidget) {
348 EnterWidget(pAnnotHandlerMgr, &pFXAnnot, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400349
350 // Annot_OnMouseEnter may have invalidated pFXAnnot.
351 if (!pFXAnnot) {
Henrique Nakashima4a652542017-08-22 10:59:49 -0400352 ExitWidget(pAnnotHandlerMgr, false, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400353 return true;
354 }
dsinclairf34518b2016-09-13 12:03:48 -0700355 }
Henrique Nakashima4a652542017-08-22 10:59:49 -0400356
tsepezf8074ce2016-09-27 14:29:57 -0700357 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
tsepez4cf55152016-11-02 14:37:54 -0700358 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700359 }
Henrique Nakashima65513622017-08-16 14:48:13 -0400360
tsepez4cf55152016-11-02 14:37:54 -0700361 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700362}
363
Henrique Nakashima4a652542017-08-22 10:59:49 -0400364void CPDFSDK_PageView::EnterWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
365 CPDFSDK_Annot::ObservedPtr* pAnnot,
366 uint32_t nFlag) {
367 m_bOnWidget = true;
368 m_pCaptureWidget.Reset(pAnnot->Get());
369 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pAnnot, nFlag);
370}
371
372void CPDFSDK_PageView::ExitWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
373 bool callExitCallback,
374 uint32_t nFlag) {
375 m_bOnWidget = false;
376 if (m_pCaptureWidget) {
377 if (callExitCallback)
378 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
379
380 m_pCaptureWidget.Reset();
381 }
382}
383
tsepez4cf55152016-11-02 14:37:54 -0700384bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
385 double deltaY,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500386 const CFX_PointF& point,
tsepez4cf55152016-11-02 14:37:54 -0700387 int nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500388 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700389 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700390 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700391
dsinclairb402b172016-10-11 09:26:32 -0700392 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
393 m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang222e1a42017-06-20 14:47:00 -0700394 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag,
395 static_cast<int>(deltaY), point);
dsinclairf34518b2016-09-13 12:03:48 -0700396}
397
tsepez4cf55152016-11-02 14:37:54 -0700398bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700399 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700400 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
401 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700402 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
403 }
404
tsepez4cf55152016-11-02 14:37:54 -0700405 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700406}
407
tsepez4cf55152016-11-02 14:37:54 -0700408bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700409 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700410 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
411 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700412 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
413 }
tsepez4cf55152016-11-02 14:37:54 -0700414 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700415}
416
tsepez4cf55152016-11-02 14:37:54 -0700417bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
418 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700419}
420
421void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700422 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
423 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700424
Henrique Nakashima65513622017-08-16 14:48:13 -0400425 CFX_AutoRestorer<bool> lock(&m_bLocked);
426 m_bLocked = true;
dsinclairf34518b2016-09-13 12:03:48 -0700427
428#ifdef PDF_ENABLE_XFA
429 CFX_RetainPtr<CPDFXFA_Page> protector(m_page);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400430 if (m_pFormFillEnv->GetXFAContext()->GetDocType() == XFA_DocType::Dynamic) {
dsinclairf34518b2016-09-13 12:03:48 -0700431 CXFA_FFPageView* pageView = m_page->GetXFAPageView();
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700432 std::unique_ptr<IXFA_WidgetIterator> pWidgetHandler(
dsinclairf34518b2016-09-13 12:03:48 -0700433 pageView->CreateWidgetIterator(
434 XFA_TRAVERSEWAY_Form,
435 XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable));
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700436 if (!pWidgetHandler) {
dsinclairf34518b2016-09-13 12:03:48 -0700437 return;
438 }
439
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700440 while (CXFA_FFWidget* pXFAAnnot = pWidgetHandler->MoveToNext()) {
dsinclairf34518b2016-09-13 12:03:48 -0700441 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
442 if (!pAnnot)
443 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700444 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700445 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
446 }
447
dsinclairf34518b2016-09-13 12:03:48 -0700448 return;
449 }
450#endif // PDF_ENABLE_XFA
451
452 CPDF_Page* pPage = GetPDFPage();
453 ASSERT(pPage);
thestig7c292e02016-09-28 14:14:26 -0700454 bool bUpdateAP = CPDF_InterForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700455 // Disable the default AP construction.
thestig7c292e02016-09-28 14:14:26 -0700456 CPDF_InterForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700457 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
dsinclairf34518b2016-09-13 12:03:48 -0700458 CPDF_InterForm::SetUpdateAP(bUpdateAP);
459
460 const size_t nCount = m_pAnnotList->Count();
461 for (size_t i = 0; i < nCount; ++i) {
462 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
463 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
464 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
465 if (!pAnnot)
466 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700467 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700468 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
469 }
dsinclairf34518b2016-09-13 12:03:48 -0700470}
471
dsinclairf34518b2016-09-13 12:03:48 -0700472void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700473 for (const auto& rc : rects)
Lei Zhang77f9bff2017-08-29 11:34:12 -0700474 m_pFormFillEnv->Invalidate(m_page, rc.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700475}
476
477void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
478 CFX_FloatRect rcWindow = pAnnot->GetRect();
Lei Zhang77f9bff2017-08-29 11:34:12 -0700479 m_pFormFillEnv->Invalidate(m_page, rcWindow.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700480}
481
482int CPDFSDK_PageView::GetPageIndex() const {
483 if (!m_page)
484 return -1;
485
486#ifdef PDF_ENABLE_XFA
Dan Sinclaircdba7472017-03-23 09:17:10 -0400487 switch (m_page->GetContext()->GetDocType()) {
488 case XFA_DocType::Dynamic: {
dsinclairf34518b2016-09-13 12:03:48 -0700489 CXFA_FFPageView* pPageView = m_page->GetXFAPageView();
490 return pPageView ? pPageView->GetPageIndex() : -1;
491 }
Dan Sinclaircdba7472017-03-23 09:17:10 -0400492 case XFA_DocType::Static:
493 case XFA_DocType::PDF:
dsinclairf34518b2016-09-13 12:03:48 -0700494 return GetPageIndexForStaticPDF();
495 default:
496 return -1;
497 }
498#else // PDF_ENABLE_XFA
499 return GetPageIndexForStaticPDF();
500#endif // PDF_ENABLE_XFA
501}
502
503bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
504 if (!p)
505 return false;
506
507 const auto& annots = m_pAnnotList->All();
508 auto it = std::find_if(annots.begin(), annots.end(),
509 [p](const std::unique_ptr<CPDF_Annot>& annot) {
510 return annot.get() == p;
511 });
512 return it != annots.end();
513}
514
dsinclair8afe15a2016-10-05 12:00:34 -0700515bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
516 if (!p)
517 return false;
518 return pdfium::ContainsValue(m_SDKAnnotArray, p);
519}
520
dsinclairf34518b2016-09-13 12:03:48 -0700521CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700522 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700523 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700524}
525
526int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700527 CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict.Get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700528 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700529 return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
530}