blob: c12673491c0545541623fac8966d677a7eaf72af [file] [log] [blame]
dsinclairf34518b2016-09-13 12:03:48 -07001// Copyright 2016 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
dsinclair114e46a2016-09-29 17:18:21 -07007#include "fpdfsdk/cpdfsdk_pageview.h"
dsinclairf34518b2016-09-13 12:03:48 -07008
thestig7c292e02016-09-28 14:14:26 -07009#include <memory>
10#include <vector>
11
dsinclair488b7ad2016-10-04 11:55:50 -070012#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair69d9c682016-10-04 12:18:35 -070013#include "core/fpdfapi/render/cpdf_renderoptions.h"
dsinclair1727aee2016-09-29 13:12:56 -070014#include "core/fpdfdoc/cpdf_annotlist.h"
15#include "core/fpdfdoc/cpdf_interform.h"
dsinclair114e46a2016-09-29 17:18:21 -070016#include "fpdfsdk/cpdfsdk_annot.h"
17#include "fpdfsdk/cpdfsdk_annothandlermgr.h"
tsepezd805eec2017-01-11 14:03:54 -080018#include "fpdfsdk/cpdfsdk_annotiteration.h"
dsinclair735606d2016-10-05 15:47:02 -070019#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/cpdfsdk_interform.h"
tsepez36eb4bd2016-10-03 15:24:27 -070021#include "third_party/base/ptr_util.h"
dsinclairf34518b2016-09-13 12:03:48 -070022
23#ifdef PDF_ENABLE_XFA
dsinclair4d29e782016-10-04 14:02:47 -070024#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
Dan Sinclair80c48782017-03-23 12:11:20 -040025#include "xfa/fxfa/cxfa_ffdocview.h"
26#include "xfa/fxfa/cxfa_ffpageview.h"
27#include "xfa/fxfa/cxfa_ffwidgethandler.h"
28#include "xfa/fxfa/cxfa_rendercontext.h"
dsinclair202ad722016-09-29 17:41:42 -070029#include "xfa/fxgraphics/cfx_graphics.h"
dsinclairf34518b2016-09-13 12:03:48 -070030#endif // PDF_ENABLE_XFA
31
dsinclairb402b172016-10-11 09:26:32 -070032CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_FormFillEnvironment* pFormFillEnv,
dsinclairf34518b2016-09-13 12:03:48 -070033 UnderlyingPageType* page)
34 : m_page(page),
dsinclairb402b172016-10-11 09:26:32 -070035 m_pFormFillEnv(pFormFillEnv),
dsinclairf34518b2016-09-13 12:03:48 -070036#ifndef PDF_ENABLE_XFA
37 m_bOwnsPage(false),
38#endif // PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -070039 m_bEnterWidget(false),
40 m_bExitWidget(false),
41 m_bOnWidget(false),
42 m_bValid(false),
43 m_bLocked(false),
dsinclairbcf46232016-10-03 13:02:27 -070044 m_bBeingDestroyed(false) {
dsinclair7cbe68e2016-10-12 11:56:23 -070045 CPDFSDK_InterForm* pInterForm = pFormFillEnv->GetInterForm();
Lei Zhang222e1a42017-06-20 14:47:00 -070046 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
dsinclairf34518b2016-09-13 12:03:48 -070047#ifdef PDF_ENABLE_XFA
Lei Zhang222e1a42017-06-20 14:47:00 -070048 if (page->GetPDFPage())
49 pPDFInterForm->FixPageFields(page->GetPDFPage());
dsinclairf34518b2016-09-13 12:03:48 -070050#else // PDF_ENABLE_XFA
Lei Zhang222e1a42017-06-20 14:47:00 -070051 pPDFInterForm->FixPageFields(page);
dsinclairf34518b2016-09-13 12:03:48 -070052 m_page->SetView(this);
53#endif // PDF_ENABLE_XFA
54}
55
56CPDFSDK_PageView::~CPDFSDK_PageView() {
57#ifndef PDF_ENABLE_XFA
58 // The call to |ReleaseAnnot| can cause the page pointed to by |m_page| to
59 // be freed, which will cause issues if we try to cleanup the pageview pointer
60 // in |m_page|. So, reset the pageview pointer before doing anything else.
61 m_page->SetView(nullptr);
62#endif // PDF_ENABLE_XFA
63
dsinclairb402b172016-10-11 09:26:32 -070064 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
65 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclair8afe15a2016-10-05 12:00:34 -070066 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray)
dsinclairf34518b2016-09-13 12:03:48 -070067 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
68
dsinclair8afe15a2016-10-05 12:00:34 -070069 m_SDKAnnotArray.clear();
dsinclairf34518b2016-09-13 12:03:48 -070070 m_pAnnotList.reset();
71
72#ifndef PDF_ENABLE_XFA
73 if (m_bOwnsPage)
74 delete m_page;
75#endif // PDF_ENABLE_XFA
76}
77
78void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
79 CFX_Matrix* pUser2Device,
80#ifdef PDF_ENABLE_XFA
81 CPDF_RenderOptions* pOptions,
82 const FX_RECT& pClip) {
83#else
84 CPDF_RenderOptions* pOptions) {
85#endif // PDF_ENABLE_XFA
86 m_curMatrix = *pUser2Device;
dsinclairf34518b2016-09-13 12:03:48 -070087
88#ifdef PDF_ENABLE_XFA
89 CPDFXFA_Page* pPage = GetPDFXFAPage();
90 if (!pPage)
91 return;
92
Dan Sinclaircdba7472017-03-23 09:17:10 -040093 if (pPage->GetContext()->GetDocType() == XFA_DocType::Dynamic) {
Dan Sinclair05df0752017-03-14 14:43:42 -040094 CFX_RectF rectClip(
95 static_cast<float>(pClip.left), static_cast<float>(pClip.top),
96 static_cast<float>(pClip.Width()), static_cast<float>(pClip.Height()));
Dan Sinclaircf53b782017-05-04 14:08:01 -040097
98 CFX_Graphics gs(pDevice);
dsinclairf34518b2016-09-13 12:03:48 -070099 gs.SetClipRect(rectClip);
Dan Sinclaircf53b782017-05-04 14:08:01 -0400100
dsinclairf34518b2016-09-13 12:03:48 -0700101 CXFA_FFPageView* xfaView = pPage->GetXFAPageView();
Dan Sinclaircf53b782017-05-04 14:08:01 -0400102 CXFA_RenderContext renderContext(xfaView, rectClip, *pUser2Device);
103 renderContext.DoRender(&gs);
104
dsinclairf34518b2016-09-13 12:03:48 -0700105 CXFA_FFDocView* docView = xfaView->GetDocView();
106 if (!docView)
107 return;
108 CPDFSDK_Annot* annot = GetFocusAnnot();
109 if (!annot)
110 return;
111 // Render the focus widget
112 docView->GetWidgetHandler()->RenderWidget(annot->GetXFAWidget(), &gs,
tsepez4cf55152016-11-02 14:37:54 -0700113 pUser2Device, false);
dsinclairf34518b2016-09-13 12:03:48 -0700114 return;
115 }
116#endif // PDF_ENABLE_XFA
117
118 // for pdf/static xfa.
tsepezd805eec2017-01-11 14:03:54 -0800119 CPDFSDK_AnnotIteration annotIteration(this, true);
120 for (const auto& pSDKAnnot : annotIteration) {
121 m_pFormFillEnv->GetAnnotHandlerMgr()->Annot_OnDraw(
122 this, pSDKAnnot.Get(), pDevice, pUser2Device, pOptions->m_bDrawAnnots);
dsinclairf34518b2016-09-13 12:03:48 -0700123 }
124}
125
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500126CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(const CFX_PointF& point) {
dsinclairb402b172016-10-11 09:26:32 -0700127 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800128 CPDFSDK_AnnotIteration annotIteration(this, false);
129 for (const auto& pSDKAnnot : annotIteration) {
130 CFX_FloatRect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
dsinclairf34518b2016-09-13 12:03:48 -0700131 if (pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::POPUP)
132 continue;
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500133 if (rc.Contains(point))
tsepezd805eec2017-01-11 14:03:54 -0800134 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700135 }
dsinclairf34518b2016-09-13 12:03:48 -0700136 return nullptr;
137}
138
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500139CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(const CFX_PointF& point) {
dsinclairb402b172016-10-11 09:26:32 -0700140 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800141 CPDFSDK_AnnotIteration annotIteration(this, false);
142 for (const auto& pSDKAnnot : annotIteration) {
dsinclairf34518b2016-09-13 12:03:48 -0700143 bool bHitTest = pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET;
144#ifdef PDF_ENABLE_XFA
145 bHitTest = bHitTest ||
146 pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::XFAWIDGET;
147#endif // PDF_ENABLE_XFA
148 if (bHitTest) {
tsepezd805eec2017-01-11 14:03:54 -0800149 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500150 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot.Get(), point))
tsepezd805eec2017-01-11 14:03:54 -0800151 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700152 }
153 }
dsinclairf34518b2016-09-13 12:03:48 -0700154 return nullptr;
155}
156
dsinclairf34518b2016-09-13 12:03:48 -0700157#ifdef PDF_ENABLE_XFA
158CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CXFA_FFWidget* pPDFAnnot) {
159 if (!pPDFAnnot)
160 return nullptr;
161
162 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
163 if (pSDKAnnot)
164 return pSDKAnnot;
165
dsinclairb402b172016-10-11 09:26:32 -0700166 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700167 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
168 if (!pSDKAnnot)
169 return nullptr;
170
dsinclair8afe15a2016-10-05 12:00:34 -0700171 m_SDKAnnotArray.push_back(pSDKAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700172 return pSDKAnnot;
173}
dsinclairf34518b2016-09-13 12:03:48 -0700174
tsepez4cf55152016-11-02 14:37:54 -0700175bool CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
dsinclairf34518b2016-09-13 12:03:48 -0700176 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700177 return false;
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
184 if (GetFocusAnnot() == pAnnot)
dsinclair7cbe68e2016-10-12 11:56:23 -0700185 m_pFormFillEnv->KillFocusAnnot(0);
dsinclairb402b172016-10-11 09:26:32 -0700186 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700187 if (pAnnotHandler)
188 pAnnotHandler->ReleaseAnnot(pAnnot);
189
dsinclair8afe15a2016-10-05 12:00:34 -0700190 auto it = std::find(m_SDKAnnotArray.begin(), m_SDKAnnotArray.end(), pAnnot);
191 if (it != m_SDKAnnotArray.end())
192 m_SDKAnnotArray.erase(it);
tsepezf8074ce2016-09-27 14:29:57 -0700193 if (m_pCaptureWidget.Get() == pAnnot)
194 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700195
tsepez4cf55152016-11-02 14:37:54 -0700196 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700197}
dsinclair8afe15a2016-10-05 12:00:34 -0700198#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -0700199
200CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
201 if (m_page) {
202#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700203 return m_page->GetContext()->GetPDFDoc();
dsinclairf34518b2016-09-13 12:03:48 -0700204#else // PDF_ENABLE_XFA
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700205 return m_page->m_pDocument.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700206#endif // PDF_ENABLE_XFA
207 }
208 return nullptr;
209}
210
211CPDF_Page* CPDFSDK_PageView::GetPDFPage() const {
212#ifdef PDF_ENABLE_XFA
213 return m_page ? m_page->GetPDFPage() : nullptr;
214#else // PDF_ENABLE_XFA
215 return m_page;
216#endif // PDF_ENABLE_XFA
217}
218
dsinclairf34518b2016-09-13 12:03:48 -0700219CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
dsinclair8afe15a2016-10-05 12:00:34 -0700220 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700221 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
222 return pAnnot;
223 }
224 return nullptr;
225}
226
227#ifdef PDF_ENABLE_XFA
228CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(CXFA_FFWidget* hWidget) {
229 if (!hWidget)
230 return nullptr;
231
dsinclair8afe15a2016-10-05 12:00:34 -0700232 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700233 if (pAnnot->GetXFAWidget() == hWidget)
234 return pAnnot;
235 }
236 return nullptr;
237}
238#endif // PDF_ENABLE_XFA
239
Diana Gagedce2d722017-06-20 11:17:11 -0700240CFX_WideString CPDFSDK_PageView::GetSelectedText() {
241 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
242 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
243 m_pFormFillEnv->GetAnnotHandlerMgr();
244 return pAnnotHandlerMgr->Annot_GetSelectedText(pAnnot);
245 }
246
247 return CFX_WideString();
248}
249
Dan Sinclairf528eee2017-02-14 11:52:07 -0500250bool CPDFSDK_PageView::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500251 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700252 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700253 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700254 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700255 }
256
dsinclairb402b172016-10-11 09:26:32 -0700257 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
258 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700259 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700260 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700261
262 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700263 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700264
dsinclair7cbe68e2016-10-12 11:56:23 -0700265 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700266 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700267}
268
269#ifdef PDF_ENABLE_XFA
Dan Sinclairf528eee2017-02-14 11:52:07 -0500270bool CPDFSDK_PageView::OnRButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500271 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700272 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700273 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700274
dsinclairb402b172016-10-11 09:26:32 -0700275 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
276 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700277 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700278 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700279 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700280
281 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700282 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700283
tsepez4cf55152016-11-02 14:37:54 -0700284 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700285}
286
Dan Sinclairf528eee2017-02-14 11:52:07 -0500287bool CPDFSDK_PageView::OnRButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700288 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
289 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500290 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
dsinclairf34518b2016-09-13 12:03:48 -0700291 if (!pFXAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700292 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700293
tsepezf8074ce2016-09-27 14:29:57 -0700294 if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
dsinclair7cbe68e2016-10-12 11:56:23 -0700295 m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700296
tsepez4cf55152016-11-02 14:37:54 -0700297 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700298}
299#endif // PDF_ENABLE_XFA
300
Dan Sinclairf528eee2017-02-14 11:52:07 -0500301bool CPDFSDK_PageView::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700302 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
303 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500304 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700305 CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot());
dsinclairf34518b2016-09-13 12:03:48 -0700306 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
307 // Last focus Annot gets a chance to handle the event.
tsepezf8074ce2016-09-27 14:29:57 -0700308 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700309 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700310 }
tsepezf8074ce2016-09-27 14:29:57 -0700311 return pFXAnnot &&
312 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
dsinclairf34518b2016-09-13 12:03:48 -0700313}
314
Dan Sinclairf528eee2017-02-14 11:52:07 -0500315bool CPDFSDK_PageView::OnMouseMove(const CFX_PointF& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700316 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
317 m_pFormFillEnv->GetAnnotHandlerMgr();
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500318 CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700319 if (pFXAnnot) {
320 if (m_pCaptureWidget && m_pCaptureWidget != pFXAnnot) {
tsepez4cf55152016-11-02 14:37:54 -0700321 m_bExitWidget = true;
322 m_bEnterWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700323 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
dsinclairf34518b2016-09-13 12:03:48 -0700324 }
tsepezf8074ce2016-09-27 14:29:57 -0700325 m_pCaptureWidget.Reset(pFXAnnot.Get());
tsepez4cf55152016-11-02 14:37:54 -0700326 m_bOnWidget = true;
dsinclairf34518b2016-09-13 12:03:48 -0700327 if (!m_bEnterWidget) {
tsepez4cf55152016-11-02 14:37:54 -0700328 m_bEnterWidget = true;
329 m_bExitWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700330 pAnnotHandlerMgr->Annot_OnMouseEnter(this, &pFXAnnot, nFlag);
dsinclairf34518b2016-09-13 12:03:48 -0700331 }
tsepezf8074ce2016-09-27 14:29:57 -0700332 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
tsepez4cf55152016-11-02 14:37:54 -0700333 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700334 }
335 if (m_bOnWidget) {
tsepez4cf55152016-11-02 14:37:54 -0700336 m_bOnWidget = false;
337 m_bExitWidget = true;
338 m_bEnterWidget = false;
tsepezf8074ce2016-09-27 14:29:57 -0700339 if (m_pCaptureWidget) {
340 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
341 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700342 }
343 }
tsepez4cf55152016-11-02 14:37:54 -0700344 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700345}
346
tsepez4cf55152016-11-02 14:37:54 -0700347bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
348 double deltaY,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500349 const CFX_PointF& point,
tsepez4cf55152016-11-02 14:37:54 -0700350 int nFlag) {
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500351 CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700352 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700353 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700354
dsinclairb402b172016-10-11 09:26:32 -0700355 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
356 m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang222e1a42017-06-20 14:47:00 -0700357 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag,
358 static_cast<int>(deltaY), point);
dsinclairf34518b2016-09-13 12:03:48 -0700359}
360
tsepez4cf55152016-11-02 14:37:54 -0700361bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700362 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700363 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
364 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700365 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
366 }
367
tsepez4cf55152016-11-02 14:37:54 -0700368 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700369}
370
tsepez4cf55152016-11-02 14:37:54 -0700371bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700372 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700373 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
374 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700375 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
376 }
tsepez4cf55152016-11-02 14:37:54 -0700377 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700378}
379
tsepez4cf55152016-11-02 14:37:54 -0700380bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
381 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700382}
383
384void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700385 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
386 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700387
tsepez4cf55152016-11-02 14:37:54 -0700388 SetLock(true);
dsinclairf34518b2016-09-13 12:03:48 -0700389
390#ifdef PDF_ENABLE_XFA
391 CFX_RetainPtr<CPDFXFA_Page> protector(m_page);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400392 if (m_pFormFillEnv->GetXFAContext()->GetDocType() == XFA_DocType::Dynamic) {
dsinclairf34518b2016-09-13 12:03:48 -0700393 CXFA_FFPageView* pageView = m_page->GetXFAPageView();
394 std::unique_ptr<IXFA_WidgetIterator> pWidgetHander(
395 pageView->CreateWidgetIterator(
396 XFA_TRAVERSEWAY_Form,
397 XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable));
398 if (!pWidgetHander) {
tsepez4cf55152016-11-02 14:37:54 -0700399 SetLock(false);
dsinclairf34518b2016-09-13 12:03:48 -0700400 return;
401 }
402
403 while (CXFA_FFWidget* pXFAAnnot = pWidgetHander->MoveToNext()) {
404 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
405 if (!pAnnot)
406 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700407 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700408 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
409 }
410
tsepez4cf55152016-11-02 14:37:54 -0700411 SetLock(false);
dsinclairf34518b2016-09-13 12:03:48 -0700412 return;
413 }
414#endif // PDF_ENABLE_XFA
415
416 CPDF_Page* pPage = GetPDFPage();
417 ASSERT(pPage);
thestig7c292e02016-09-28 14:14:26 -0700418 bool bUpdateAP = CPDF_InterForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700419 // Disable the default AP construction.
thestig7c292e02016-09-28 14:14:26 -0700420 CPDF_InterForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700421 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
dsinclairf34518b2016-09-13 12:03:48 -0700422 CPDF_InterForm::SetUpdateAP(bUpdateAP);
423
424 const size_t nCount = m_pAnnotList->Count();
425 for (size_t i = 0; i < nCount; ++i) {
426 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
427 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
428 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
429 if (!pAnnot)
430 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700431 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700432 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
433 }
434
tsepez4cf55152016-11-02 14:37:54 -0700435 SetLock(false);
dsinclairf34518b2016-09-13 12:03:48 -0700436}
437
dsinclairf34518b2016-09-13 12:03:48 -0700438void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700439 for (const auto& rc : rects)
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500440 m_pFormFillEnv->Invalidate(m_page, rc.ToFxRect());
dsinclairf34518b2016-09-13 12:03:48 -0700441}
442
443void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
444 CFX_FloatRect rcWindow = pAnnot->GetRect();
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500445 m_pFormFillEnv->Invalidate(m_page, rcWindow.ToFxRect());
dsinclairf34518b2016-09-13 12:03:48 -0700446}
447
448int CPDFSDK_PageView::GetPageIndex() const {
449 if (!m_page)
450 return -1;
451
452#ifdef PDF_ENABLE_XFA
Dan Sinclaircdba7472017-03-23 09:17:10 -0400453 switch (m_page->GetContext()->GetDocType()) {
454 case XFA_DocType::Dynamic: {
dsinclairf34518b2016-09-13 12:03:48 -0700455 CXFA_FFPageView* pPageView = m_page->GetXFAPageView();
456 return pPageView ? pPageView->GetPageIndex() : -1;
457 }
Dan Sinclaircdba7472017-03-23 09:17:10 -0400458 case XFA_DocType::Static:
459 case XFA_DocType::PDF:
dsinclairf34518b2016-09-13 12:03:48 -0700460 return GetPageIndexForStaticPDF();
461 default:
462 return -1;
463 }
464#else // PDF_ENABLE_XFA
465 return GetPageIndexForStaticPDF();
466#endif // PDF_ENABLE_XFA
467}
468
469bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
470 if (!p)
471 return false;
472
473 const auto& annots = m_pAnnotList->All();
474 auto it = std::find_if(annots.begin(), annots.end(),
475 [p](const std::unique_ptr<CPDF_Annot>& annot) {
476 return annot.get() == p;
477 });
478 return it != annots.end();
479}
480
dsinclair8afe15a2016-10-05 12:00:34 -0700481bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
482 if (!p)
483 return false;
484 return pdfium::ContainsValue(m_SDKAnnotArray, p);
485}
486
dsinclairf34518b2016-09-13 12:03:48 -0700487CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700488 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700489 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700490}
491
492int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700493 CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict.Get();
dsinclair7cbe68e2016-10-12 11:56:23 -0700494 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700495 return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
496}