blob: 358f72705d84457c53bb7564618259d89e8b964a [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"
tsepezd805eec2017-01-11 14:03:54 -080017#include "fpdfsdk/cpdfsdk_annotiteration.h"
dsinclair735606d2016-10-05 15:47:02 -070018#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070019#include "fpdfsdk/cpdfsdk_interform.h"
tsepez36eb4bd2016-10-03 15:24:27 -070020#include "third_party/base/ptr_util.h"
dsinclairf34518b2016-09-13 12:03:48 -070021
22#ifdef PDF_ENABLE_XFA
dsinclair4d29e782016-10-04 14:02:47 -070023#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
Dan Sinclair80c48782017-03-23 12:11:20 -040024#include "xfa/fxfa/cxfa_ffdocview.h"
25#include "xfa/fxfa/cxfa_ffpageview.h"
26#include "xfa/fxfa/cxfa_ffwidgethandler.h"
27#include "xfa/fxfa/cxfa_rendercontext.h"
Dan Sinclair2b918c82017-07-13 14:47:10 -040028#include "xfa/fxgraphics/cxfa_graphics.h"
dsinclairf34518b2016-09-13 12:03:48 -070029#endif // PDF_ENABLE_XFA
30
dsinclairb402b172016-10-11 09:26:32 -070031CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_FormFillEnvironment* pFormFillEnv,
dsinclairf34518b2016-09-13 12:03:48 -070032 UnderlyingPageType* page)
33 : m_page(page),
dsinclairb402b172016-10-11 09:26:32 -070034 m_pFormFillEnv(pFormFillEnv),
dsinclairf34518b2016-09-13 12:03:48 -070035#ifndef PDF_ENABLE_XFA
36 m_bOwnsPage(false),
37#endif // PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -070038 m_bOnWidget(false),
39 m_bValid(false),
40 m_bLocked(false),
dsinclairbcf46232016-10-03 13:02:27 -070041 m_bBeingDestroyed(false) {
dsinclair7cbe68e2016-10-12 11:56:23 -070042 CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
Lei Zhang222e1a42017-06-20 14:47:00 -070043 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
dsinclairf34518b2016-09-13 12:03:48 -070044#ifdef PDF_ENABLE_XFA
Lei Zhang222e1a42017-06-20 14:47:00 -070045 if (page->GetPDFPage())
46 pPDFInterForm->FixPageFields(page->GetPDFPage());
dsinclairf34518b2016-09-13 12:03:48 -070047#else // PDF_ENABLE_XFA
Lei Zhang222e1a42017-06-20 14:47:00 -070048 pPDFInterForm->FixPageFields(page);
dsinclairf34518b2016-09-13 12:03:48 -070049 m_page->SetView(this);
50#endif // PDF_ENABLE_XFA
51}
52
53CPDFSDK_PageView::~CPDFSDK_PageView() {
54#ifndef PDF_ENABLE_XFA
55 // The call to |ReleaseAnnot| can cause the page pointed to by |m_page| to
56 // be freed, which will cause issues if we try to cleanup the pageview pointer
57 // in |m_page|. So, reset the pageview pointer before doing anything else.
58 m_page->SetView(nullptr);
59#endif // PDF_ENABLE_XFA
60
dsinclairb402b172016-10-11 09:26:32 -070061 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
62 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclair8afe15a2016-10-05 12:00:34 -070063 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray)
dsinclairf34518b2016-09-13 12:03:48 -070064 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
65
dsinclair8afe15a2016-10-05 12:00:34 -070066 m_SDKAnnotArray.clear();
dsinclairf34518b2016-09-13 12:03:48 -070067 m_pAnnotList.reset();
68
69#ifndef PDF_ENABLE_XFA
70 if (m_bOwnsPage)
71 delete m_page;
72#endif // PDF_ENABLE_XFA
73}
74
75void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
76 CFX_Matrix* pUser2Device,
77#ifdef PDF_ENABLE_XFA
78 CPDF_RenderOptions* pOptions,
79 const FX_RECT& pClip) {
80#else
81 CPDF_RenderOptions* pOptions) {
82#endif // PDF_ENABLE_XFA
83 m_curMatrix = *pUser2Device;
dsinclairf34518b2016-09-13 12:03:48 -070084
85#ifdef PDF_ENABLE_XFA
86 CPDFXFA_Page* pPage = GetPDFXFAPage();
87 if (!pPage)
88 return;
89
Dan Sinclaircdba7472017-03-23 09:17:10 -040090 if (pPage->GetContext()->GetDocType() == XFA_DocType::Dynamic) {
Dan Sinclair05df0752017-03-14 14:43:42 -040091 CFX_RectF rectClip(
92 static_cast<float>(pClip.left), static_cast<float>(pClip.top),
93 static_cast<float>(pClip.Width()), static_cast<float>(pClip.Height()));
Dan Sinclaircf53b782017-05-04 14:08:01 -040094
Dan Sinclair2b918c82017-07-13 14:47:10 -040095 CXFA_Graphics gs(pDevice);
dsinclairf34518b2016-09-13 12:03:48 -070096 gs.SetClipRect(rectClip);
Dan Sinclaircf53b782017-05-04 14:08:01 -040097
dsinclairf34518b2016-09-13 12:03:48 -070098 CXFA_FFPageView* xfaView = pPage->GetXFAPageView();
Dan Sinclaircf53b782017-05-04 14:08:01 -040099 CXFA_RenderContext renderContext(xfaView, rectClip, *pUser2Device);
100 renderContext.DoRender(&gs);
101
dsinclairf34518b2016-09-13 12:03:48 -0700102 CXFA_FFDocView* docView = xfaView->GetDocView();
103 if (!docView)
104 return;
105 CPDFSDK_Annot* annot = GetFocusAnnot();
106 if (!annot)
107 return;
108 // Render the focus widget
109 docView->GetWidgetHandler()->RenderWidget(annot->GetXFAWidget(), &gs,
Lei Zhang7c9d4c22017-08-17 13:53:52 -0700110 *pUser2Device, false);
dsinclairf34518b2016-09-13 12:03:48 -0700111 return;
112 }
113#endif // PDF_ENABLE_XFA
114
115 // for pdf/static xfa.
tsepezd805eec2017-01-11 14:03:54 -0800116 CPDFSDK_AnnotIteration annotIteration(this, true);
117 for (const auto& pSDKAnnot : annotIteration) {
118 m_pFormFillEnv->GetAnnotHandlerMgr()->Annot_OnDraw(
119 this, pSDKAnnot.Get(), pDevice, pUser2Device, pOptions->m_bDrawAnnots);
dsinclairf34518b2016-09-13 12:03:48 -0700120 }
121}
122
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500123CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(const CFX_PointF& point) {
dsinclairb402b172016-10-11 09:26:32 -0700124 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800125 CPDFSDK_AnnotIteration annotIteration(this, false);
126 for (const auto& pSDKAnnot : annotIteration) {
127 CFX_FloatRect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
dsinclairf34518b2016-09-13 12:03:48 -0700128 if (pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::POPUP)
129 continue;
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500130 if (rc.Contains(point))
tsepezd805eec2017-01-11 14:03:54 -0800131 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700132 }
dsinclairf34518b2016-09-13 12:03:48 -0700133 return nullptr;
134}
135
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500136CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(const CFX_PointF& point) {
dsinclairb402b172016-10-11 09:26:32 -0700137 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800138 CPDFSDK_AnnotIteration annotIteration(this, false);
139 for (const auto& pSDKAnnot : annotIteration) {
dsinclairf34518b2016-09-13 12:03:48 -0700140 bool bHitTest = pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET;
141#ifdef PDF_ENABLE_XFA
142 bHitTest = bHitTest ||
143 pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::XFAWIDGET;
144#endif // PDF_ENABLE_XFA
145 if (bHitTest) {
tsepezd805eec2017-01-11 14:03:54 -0800146 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500147 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot.Get(), point))
tsepezd805eec2017-01-11 14:03:54 -0800148 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700149 }
150 }
dsinclairf34518b2016-09-13 12:03:48 -0700151 return nullptr;
152}
153
dsinclairf34518b2016-09-13 12:03:48 -0700154#ifdef PDF_ENABLE_XFA
155CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CXFA_FFWidget* pPDFAnnot) {
156 if (!pPDFAnnot)
157 return nullptr;
158
159 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
160 if (pSDKAnnot)
161 return pSDKAnnot;
162
dsinclairb402b172016-10-11 09:26:32 -0700163 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700164 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
165 if (!pSDKAnnot)
166 return nullptr;
167
dsinclair8afe15a2016-10-05 12:00:34 -0700168 m_SDKAnnotArray.push_back(pSDKAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700169 return pSDKAnnot;
170}
dsinclairf34518b2016-09-13 12:03:48 -0700171
tsepez4cf55152016-11-02 14:37:54 -0700172bool CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
dsinclairf34518b2016-09-13 12:03:48 -0700173 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700174 return false;
Tom Sepezbfa2a972017-07-24 11:38:31 -0700175
dsinclairf34518b2016-09-13 12:03:48 -0700176 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
Dan Sinclaircdba7472017-03-23 09:17:10 -0400177 if (!pPage || (pPage->GetContext()->GetDocType() != XFA_DocType::Static &&
178 pPage->GetContext()->GetDocType() != XFA_DocType::Dynamic)) {
tsepez4cf55152016-11-02 14:37:54 -0700179 return false;
Dan Sinclaircdba7472017-03-23 09:17:10 -0400180 }
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
Diana Gagedce2d722017-06-20 11:17:11 -0700243CFX_WideString CPDFSDK_PageView::GetSelectedText() {
244 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
245 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
246 m_pFormFillEnv->GetAnnotHandlerMgr();
247 return pAnnotHandlerMgr->Annot_GetSelectedText(pAnnot);
248 }
249
250 return CFX_WideString();
251}
252
Diana Gageab390972017-07-28 17:07:39 -0700253void CPDFSDK_PageView::ReplaceSelection(const CFX_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
Dan Sinclairf528eee2017-02-14 11:52:07 -0500261bool CPDFSDK_PageView::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500262 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700263 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700264 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700265 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700266 }
267
dsinclairb402b172016-10-11 09:26:32 -0700268 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
269 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700270 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700271 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700272
273 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700274 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700275
dsinclair7cbe68e2016-10-12 11:56:23 -0700276 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700277 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700278}
279
280#ifdef PDF_ENABLE_XFA
Dan Sinclairf528eee2017-02-14 11:52:07 -0500281bool CPDFSDK_PageView::OnRButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500282 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700283 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700284 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700285
dsinclairb402b172016-10-11 09:26:32 -0700286 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
287 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700288 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700289 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700290 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700291
292 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700293 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700294
tsepez4cf55152016-11-02 14:37:54 -0700295 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700296}
297
Dan Sinclairf528eee2017-02-14 11:52:07 -0500298bool CPDFSDK_PageView::OnRButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700299 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
300 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500301 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
dsinclairf34518b2016-09-13 12:03:48 -0700302 if (!pFXAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700303 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700304
tsepezf8074ce2016-09-27 14:29:57 -0700305 if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
dsinclair7cbe68e2016-10-12 11:56:23 -0700306 m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700307
tsepez4cf55152016-11-02 14:37:54 -0700308 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700309}
310#endif // PDF_ENABLE_XFA
311
Dan Sinclairf528eee2017-02-14 11:52:07 -0500312bool CPDFSDK_PageView::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700313 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
314 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500315 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700316 CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot());
dsinclairf34518b2016-09-13 12:03:48 -0700317 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
318 // Last focus Annot gets a chance to handle the event.
tsepezf8074ce2016-09-27 14:29:57 -0700319 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700320 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700321 }
tsepezf8074ce2016-09-27 14:29:57 -0700322 return pFXAnnot &&
323 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
dsinclairf34518b2016-09-13 12:03:48 -0700324}
325
Dan Sinclairf528eee2017-02-14 11:52:07 -0500326bool CPDFSDK_PageView::OnMouseMove(const CFX_PointF& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700327 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
328 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500329 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point));
Henrique Nakashima65513622017-08-16 14:48:13 -0400330
Henrique Nakashima4a652542017-08-22 10:59:49 -0400331 if (m_bOnWidget && m_pCaptureWidget != pFXAnnot)
332 ExitWidget(pAnnotHandlerMgr, true, nFlag);
333
334 if (pFXAnnot) {
335 if (!m_bOnWidget) {
336 EnterWidget(pAnnotHandlerMgr, &pFXAnnot, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400337
338 // Annot_OnMouseEnter may have invalidated pFXAnnot.
339 if (!pFXAnnot) {
Henrique Nakashima4a652542017-08-22 10:59:49 -0400340 ExitWidget(pAnnotHandlerMgr, false, nFlag);
Henrique Nakashima65513622017-08-16 14:48:13 -0400341 return true;
342 }
dsinclairf34518b2016-09-13 12:03:48 -0700343 }
Henrique Nakashima4a652542017-08-22 10:59:49 -0400344
tsepezf8074ce2016-09-27 14:29:57 -0700345 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
tsepez4cf55152016-11-02 14:37:54 -0700346 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700347 }
Henrique Nakashima65513622017-08-16 14:48:13 -0400348
tsepez4cf55152016-11-02 14:37:54 -0700349 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700350}
351
Henrique Nakashima4a652542017-08-22 10:59:49 -0400352void CPDFSDK_PageView::EnterWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
353 CPDFSDK_Annot::ObservedPtr* pAnnot,
354 uint32_t nFlag) {
355 m_bOnWidget = true;
356 m_pCaptureWidget.Reset(pAnnot->Get());
357 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pAnnot, nFlag);
358}
359
360void CPDFSDK_PageView::ExitWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
361 bool callExitCallback,
362 uint32_t nFlag) {
363 m_bOnWidget = false;
364 if (m_pCaptureWidget) {
365 if (callExitCallback)
366 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
367
368 m_pCaptureWidget.Reset();
369 }
370}
371
tsepez4cf55152016-11-02 14:37:54 -0700372bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
373 double deltaY,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500374 const CFX_PointF& point,
tsepez4cf55152016-11-02 14:37:54 -0700375 int nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500376 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700377 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700378 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700379
dsinclairb402b172016-10-11 09:26:32 -0700380 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
381 m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang222e1a42017-06-20 14:47:00 -0700382 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag,
383 static_cast<int>(deltaY), point);
dsinclairf34518b2016-09-13 12:03:48 -0700384}
385
tsepez4cf55152016-11-02 14:37:54 -0700386bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700387 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700388 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
389 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700390 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
391 }
392
tsepez4cf55152016-11-02 14:37:54 -0700393 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700394}
395
tsepez4cf55152016-11-02 14:37:54 -0700396bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700397 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700398 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
399 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700400 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
401 }
tsepez4cf55152016-11-02 14:37:54 -0700402 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700403}
404
tsepez4cf55152016-11-02 14:37:54 -0700405bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
406 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700407}
408
409void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700410 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
411 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700412
Henrique Nakashima65513622017-08-16 14:48:13 -0400413 CFX_AutoRestorer<bool> lock(&m_bLocked);
414 m_bLocked = true;
dsinclairf34518b2016-09-13 12:03:48 -0700415
416#ifdef PDF_ENABLE_XFA
417 CFX_RetainPtr<CPDFXFA_Page> protector(m_page);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400418 if (m_pFormFillEnv->GetXFAContext()->GetDocType() == XFA_DocType::Dynamic) {
dsinclairf34518b2016-09-13 12:03:48 -0700419 CXFA_FFPageView* pageView = m_page->GetXFAPageView();
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700420 std::unique_ptr<IXFA_WidgetIterator> pWidgetHandler(
dsinclairf34518b2016-09-13 12:03:48 -0700421 pageView->CreateWidgetIterator(
422 XFA_TRAVERSEWAY_Form,
423 XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable));
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700424 if (!pWidgetHandler) {
dsinclairf34518b2016-09-13 12:03:48 -0700425 return;
426 }
427
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700428 while (CXFA_FFWidget* pXFAAnnot = pWidgetHandler->MoveToNext()) {
dsinclairf34518b2016-09-13 12:03:48 -0700429 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
430 if (!pAnnot)
431 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700432 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700433 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
434 }
435
dsinclairf34518b2016-09-13 12:03:48 -0700436 return;
437 }
438#endif // PDF_ENABLE_XFA
439
440 CPDF_Page* pPage = GetPDFPage();
441 ASSERT(pPage);
thestig7c292e02016-09-28 14:14:26 -0700442 bool bUpdateAP = CPDF_InterForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700443 // Disable the default AP construction.
thestig7c292e02016-09-28 14:14:26 -0700444 CPDF_InterForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700445 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
dsinclairf34518b2016-09-13 12:03:48 -0700446 CPDF_InterForm::SetUpdateAP(bUpdateAP);
447
448 const size_t nCount = m_pAnnotList->Count();
449 for (size_t i = 0; i < nCount; ++i) {
450 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
451 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
452 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
453 if (!pAnnot)
454 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700455 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700456 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
457 }
dsinclairf34518b2016-09-13 12:03:48 -0700458}
459
dsinclairf34518b2016-09-13 12:03:48 -0700460void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700461 for (const auto& rc : rects)
Lei Zhang77f9bff2017-08-29 11:34:12 -0700462 m_pFormFillEnv->Invalidate(m_page, rc.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700463}
464
465void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
466 CFX_FloatRect rcWindow = pAnnot->GetRect();
Lei Zhang77f9bff2017-08-29 11:34:12 -0700467 m_pFormFillEnv->Invalidate(m_page, rcWindow.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700468}
469
470int CPDFSDK_PageView::GetPageIndex() const {
471 if (!m_page)
472 return -1;
473
474#ifdef PDF_ENABLE_XFA
Dan Sinclaircdba7472017-03-23 09:17:10 -0400475 switch (m_page->GetContext()->GetDocType()) {
476 case XFA_DocType::Dynamic: {
dsinclairf34518b2016-09-13 12:03:48 -0700477 CXFA_FFPageView* pPageView = m_page->GetXFAPageView();
478 return pPageView ? pPageView->GetPageIndex() : -1;
479 }
Dan Sinclaircdba7472017-03-23 09:17:10 -0400480 case XFA_DocType::Static:
481 case XFA_DocType::PDF:
dsinclairf34518b2016-09-13 12:03:48 -0700482 return GetPageIndexForStaticPDF();
483 default:
484 return -1;
485 }
486#else // PDF_ENABLE_XFA
487 return GetPageIndexForStaticPDF();
488#endif // PDF_ENABLE_XFA
489}
490
491bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
492 if (!p)
493 return false;
494
495 const auto& annots = m_pAnnotList->All();
496 auto it = std::find_if(annots.begin(), annots.end(),
497 [p](const std::unique_ptr<CPDF_Annot>& annot) {
498 return annot.get() == p;
499 });
500 return it != annots.end();
501}
502
dsinclair8afe15a2016-10-05 12:00:34 -0700503bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
504 if (!p)
505 return false;
506 return pdfium::ContainsValue(m_SDKAnnotArray, p);
507}
508
dsinclairf34518b2016-09-13 12:03:48 -0700509CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700510 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700511 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700512}
513
514int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700515 CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict.Get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700516 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700517 return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
518}