blob: 465cc00d55429253e01062fd624d48e1fe4ebe31 [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
Lei Zhang81535612018-10-09 21:15:17 +000012#include "core/fpdfapi/parser/cpdf_dictionary.h"
dsinclair488b7ad2016-10-04 11:55:50 -070013#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair69d9c682016-10-04 12:18:35 -070014#include "core/fpdfapi/render/cpdf_renderoptions.h"
dsinclair1727aee2016-09-29 13:12:56 -070015#include "core/fpdfdoc/cpdf_annotlist.h"
Lei Zhangc3450652018-10-11 16:54:42 +000016#include "core/fpdfdoc/cpdf_interactiveform.h"
Dan Sinclairb8966992017-09-21 14:51:57 -040017#include "core/fxcrt/autorestorer.h"
dsinclair114e46a2016-09-29 17:18:21 -070018#include "fpdfsdk/cpdfsdk_annot.h"
tsepezd805eec2017-01-11 14:03:54 -080019#include "fpdfsdk/cpdfsdk_annotiteration.h"
dsinclair735606d2016-10-05 15:47:02 -070020#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
Tom Sepez8e94c182019-06-11 23:57:25 +000021#include "fpdfsdk/cpdfsdk_helpers.h"
Lei Zhangc3450652018-10-11 16:54:42 +000022#include "fpdfsdk/cpdfsdk_interactiveform.h"
tsepez36eb4bd2016-10-03 15:24:27 -070023#include "third_party/base/ptr_util.h"
Lei Zhang35d45872018-10-12 19:07:03 +000024#include "third_party/base/stl_util.h"
dsinclairf34518b2016-09-13 12:03:48 -070025
26#ifdef PDF_ENABLE_XFA
dsinclair4d29e782016-10-04 14:02:47 -070027#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
Tom Sepez451ed2d2019-08-19 22:55:31 +000028#include "fpdfsdk/fpdfxfa/cpdfxfa_widget.h"
Dan Sinclair80c48782017-03-23 12:11:20 -040029#include "xfa/fxfa/cxfa_ffdocview.h"
30#include "xfa/fxfa/cxfa_ffpageview.h"
dsinclairf34518b2016-09-13 12:03:48 -070031#endif // PDF_ENABLE_XFA
32
dsinclairb402b172016-10-11 09:26:32 -070033CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_FormFillEnvironment* pFormFillEnv,
Tom Sepez101535f2018-06-12 13:36:05 +000034 IPDF_Page* page)
Tom Sepez2aa4b2f2018-05-25 22:38:49 +000035 : m_page(page), m_pFormFillEnv(pFormFillEnv) {
Tom Sepeza3843c92018-06-12 19:21:46 +000036 ASSERT(m_page);
Tom Sepez101535f2018-06-12 13:36:05 +000037 CPDF_Page* pPDFPage = ToPDFPage(page);
38 if (pPDFPage) {
Lei Zhang073ecf42018-10-11 16:56:00 +000039 CPDFSDK_InteractiveForm* pForm = pFormFillEnv->GetInteractiveForm();
40 CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm();
41 pPDFForm->FixPageFields(pPDFPage);
Tom Sepeza3843c92018-06-12 19:21:46 +000042 if (!page->AsXFAPage())
43 pPDFPage->SetView(this);
Tom Sepez101535f2018-06-12 13:36:05 +000044 }
dsinclairf34518b2016-09-13 12:03:48 -070045}
46
47CPDFSDK_PageView::~CPDFSDK_PageView() {
Tom Sepeza3843c92018-06-12 19:21:46 +000048 if (!m_page->AsXFAPage()) {
49 // The call to |ReleaseAnnot| can cause the page pointed to by |m_page| to
50 // be freed, which will cause issues if we try to cleanup the pageview
51 // pointer in |m_page|. So, reset the pageview pointer before doing anything
52 // else.
53 m_page->AsPDFPage()->SetView(nullptr);
54 }
dsinclairf34518b2016-09-13 12:03:48 -070055
dsinclairb402b172016-10-11 09:26:32 -070056 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
57 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclair8afe15a2016-10-05 12:00:34 -070058 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray)
Tom Sepez0f35a9e2019-04-23 00:32:37 +000059 pAnnotHandlerMgr->ReleaseAnnot(pdfium::WrapUnique(pAnnot));
dsinclairf34518b2016-09-13 12:03:48 -070060
dsinclair8afe15a2016-10-05 12:00:34 -070061 m_SDKAnnotArray.clear();
dsinclairf34518b2016-09-13 12:03:48 -070062 m_pAnnotList.reset();
dsinclairf34518b2016-09-13 12:03:48 -070063}
64
65void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
Lei Zhang09ee0872018-10-09 19:13:55 +000066 const CFX_Matrix& mtUser2Device,
dsinclairf34518b2016-09-13 12:03:48 -070067 CPDF_RenderOptions* pOptions,
68 const FX_RECT& pClip) {
Lei Zhang09ee0872018-10-09 19:13:55 +000069 m_curMatrix = mtUser2Device;
dsinclairf34518b2016-09-13 12:03:48 -070070
71#ifdef PDF_ENABLE_XFA
Tom Sepez9bf01812019-08-19 18:59:27 +000072 IPDF_Page* pPage = GetXFAPage();
dsinclairf34518b2016-09-13 12:03:48 -070073 if (!pPage)
74 return;
75
Tom Sepez9bf01812019-08-19 18:59:27 +000076 CPDF_Document::Extension* pContext = pPage->GetDocument()->GetExtension();
Tom Sepez0126b032019-08-19 16:48:25 +000077 if (pContext->ContainsExtensionFullForm()) {
Tom Sepez9bf01812019-08-19 18:59:27 +000078 static_cast<CPDFXFA_Page*>(pPage)->DrawFocusAnnot(pDevice, GetFocusAnnot(),
79 mtUser2Device, pClip);
dsinclairf34518b2016-09-13 12:03:48 -070080 return;
81 }
82#endif // PDF_ENABLE_XFA
83
84 // for pdf/static xfa.
tsepezd805eec2017-01-11 14:03:54 -080085 CPDFSDK_AnnotIteration annotIteration(this, true);
86 for (const auto& pSDKAnnot : annotIteration) {
87 m_pFormFillEnv->GetAnnotHandlerMgr()->Annot_OnDraw(
Lei Zhang09ee0872018-10-09 19:13:55 +000088 this, pSDKAnnot.Get(), pDevice, mtUser2Device,
Dan Sinclair921fe6b2017-10-05 11:14:12 -040089 pOptions->GetDrawAnnots());
dsinclairf34518b2016-09-13 12:03:48 -070090 }
91}
92
Dan Sinclairb45ea1f2017-02-21 14:27:59 -050093CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(const CFX_PointF& point) {
dsinclairb402b172016-10-11 09:26:32 -070094 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -080095 CPDFSDK_AnnotIteration annotIteration(this, false);
96 for (const auto& pSDKAnnot : annotIteration) {
97 CFX_FloatRect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
dsinclairf34518b2016-09-13 12:03:48 -070098 if (pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::POPUP)
99 continue;
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500100 if (rc.Contains(point))
tsepezd805eec2017-01-11 14:03:54 -0800101 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700102 }
dsinclairf34518b2016-09-13 12:03:48 -0700103 return nullptr;
104}
105
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500106CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(const CFX_PointF& point) {
dsinclairb402b172016-10-11 09:26:32 -0700107 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezd805eec2017-01-11 14:03:54 -0800108 CPDFSDK_AnnotIteration annotIteration(this, false);
109 for (const auto& pSDKAnnot : annotIteration) {
dsinclairf34518b2016-09-13 12:03:48 -0700110 bool bHitTest = pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET;
111#ifdef PDF_ENABLE_XFA
112 bHitTest = bHitTest ||
113 pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::XFAWIDGET;
114#endif // PDF_ENABLE_XFA
115 if (bHitTest) {
tsepezd805eec2017-01-11 14:03:54 -0800116 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot.Get());
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500117 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot.Get(), point))
tsepezd805eec2017-01-11 14:03:54 -0800118 return pSDKAnnot.Get();
dsinclairf34518b2016-09-13 12:03:48 -0700119 }
120 }
dsinclairf34518b2016-09-13 12:03:48 -0700121 return nullptr;
122}
123
dsinclairf34518b2016-09-13 12:03:48 -0700124#ifdef PDF_ENABLE_XFA
125CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CXFA_FFWidget* pPDFAnnot) {
dsinclairf34518b2016-09-13 12:03:48 -0700126 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
127 if (pSDKAnnot)
128 return pSDKAnnot;
129
dsinclairb402b172016-10-11 09:26:32 -0700130 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang51d02662019-10-31 18:28:51 +0000131 std::unique_ptr<CPDFSDK_Annot> pNewAnnot =
132 pAnnotHandler->NewXFAAnnot(pPDFAnnot, this);
133 ASSERT(pNewAnnot);
134 pSDKAnnot = pNewAnnot.get();
135 // TODO(thestig): See if |m_SDKAnnotArray|, which takes ownership of
136 // |pNewAnnot|, can hold std::unique_ptrs instead of raw pointers.
137 m_SDKAnnotArray.push_back(pNewAnnot.release());
dsinclairf34518b2016-09-13 12:03:48 -0700138 return pSDKAnnot;
139}
dsinclairf34518b2016-09-13 12:03:48 -0700140
tsepez4cf55152016-11-02 14:37:54 -0700141bool CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepez9bf01812019-08-19 18:59:27 +0000142 IPDF_Page* pPage = pAnnot->GetXFAPage();
Tom Sepezccd94212018-05-03 13:53:02 +0000143 if (!pPage)
144 return false;
145
Tom Sepez9bf01812019-08-19 18:59:27 +0000146 CPDF_Document::Extension* pContext = pPage->GetDocument()->GetExtension();
Tom Sepezd48bd292019-08-14 19:48:55 +0000147 if (!pContext->ContainsExtensionForm())
tsepez4cf55152016-11-02 14:37:54 -0700148 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700149
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000150 ObservedPtr<CPDFSDK_Annot> pObserved(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700151 if (GetFocusAnnot() == pAnnot)
Tom Sepezbfa2a972017-07-24 11:38:31 -0700152 m_pFormFillEnv->KillFocusAnnot(0); // May invoke JS, invalidating pAnnot.
153
154 if (pObserved) {
155 CPDFSDK_AnnotHandlerMgr* pAnnotHandler =
156 m_pFormFillEnv->GetAnnotHandlerMgr();
157 if (pAnnotHandler)
Tom Sepez0f35a9e2019-04-23 00:32:37 +0000158 pAnnotHandler->ReleaseAnnot(pdfium::WrapUnique(pObserved.Get()));
Tom Sepezbfa2a972017-07-24 11:38:31 -0700159 }
dsinclairf34518b2016-09-13 12:03:48 -0700160
dsinclair8afe15a2016-10-05 12:00:34 -0700161 auto it = std::find(m_SDKAnnotArray.begin(), m_SDKAnnotArray.end(), pAnnot);
162 if (it != m_SDKAnnotArray.end())
163 m_SDKAnnotArray.erase(it);
tsepezf8074ce2016-09-27 14:29:57 -0700164 if (m_pCaptureWidget.Get() == pAnnot)
165 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700166
tsepez4cf55152016-11-02 14:37:54 -0700167 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700168}
dsinclair8afe15a2016-10-05 12:00:34 -0700169#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -0700170
171CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
Tom Sepez7b0ff3c2018-10-04 22:19:32 +0000172 return m_page->GetDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700173}
174
175CPDF_Page* CPDFSDK_PageView::GetPDFPage() const {
Tom Sepez101535f2018-06-12 13:36:05 +0000176 return ToPDFPage(m_page);
dsinclairf34518b2016-09-13 12:03:48 -0700177}
178
dsinclairf34518b2016-09-13 12:03:48 -0700179CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
dsinclair8afe15a2016-10-05 12:00:34 -0700180 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700181 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
182 return pAnnot;
183 }
184 return nullptr;
185}
186
187#ifdef PDF_ENABLE_XFA
188CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(CXFA_FFWidget* hWidget) {
189 if (!hWidget)
190 return nullptr;
191
dsinclair8afe15a2016-10-05 12:00:34 -0700192 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
Tom Sepez451ed2d2019-08-19 22:55:31 +0000193 if (ToXFAWidget(pAnnot)->GetXFAFFWidget() == hWidget)
dsinclairf34518b2016-09-13 12:03:48 -0700194 return pAnnot;
195 }
196 return nullptr;
197}
Tom Sepez9bf01812019-08-19 18:59:27 +0000198
199IPDF_Page* CPDFSDK_PageView::GetXFAPage() {
200 return ToXFAPage(m_page);
201}
dsinclairf34518b2016-09-13 12:03:48 -0700202#endif // PDF_ENABLE_XFA
203
Lei Zhanga4c7ac42018-04-17 15:12:58 +0000204WideString CPDFSDK_PageView::GetFocusedFormText() {
205 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
206 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
207 m_pFormFillEnv->GetAnnotHandlerMgr();
208 return pAnnotHandlerMgr->Annot_GetText(pAnnot);
209 }
210
211 return WideString();
212}
213
Ryan Harrison275e2602017-09-18 14:23:18 -0400214WideString CPDFSDK_PageView::GetSelectedText() {
Diana Gagedce2d722017-06-20 11:17:11 -0700215 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
216 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
217 m_pFormFillEnv->GetAnnotHandlerMgr();
218 return pAnnotHandlerMgr->Annot_GetSelectedText(pAnnot);
219 }
220
Ryan Harrison275e2602017-09-18 14:23:18 -0400221 return WideString();
Diana Gagedce2d722017-06-20 11:17:11 -0700222}
223
Ryan Harrison275e2602017-09-18 14:23:18 -0400224void CPDFSDK_PageView::ReplaceSelection(const WideString& text) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700225 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
226 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
227 m_pFormFillEnv->GetAnnotHandlerMgr();
Diana Gageab390972017-07-28 17:07:39 -0700228 pAnnotHandlerMgr->Annot_ReplaceSelection(pAnnot, text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700229 }
230}
231
Lei Zhangee967722018-04-19 20:55:54 +0000232bool CPDFSDK_PageView::CanUndo() {
233 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
234 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
235 m_pFormFillEnv->GetAnnotHandlerMgr();
236 return pAnnotHandlerMgr->Annot_CanUndo(pAnnot);
237 }
238 return false;
239}
240
241bool CPDFSDK_PageView::CanRedo() {
242 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
243 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
244 m_pFormFillEnv->GetAnnotHandlerMgr();
245 return pAnnotHandlerMgr->Annot_CanRedo(pAnnot);
246 }
247 return false;
248}
249
250bool CPDFSDK_PageView::Undo() {
251 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
252 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
253 m_pFormFillEnv->GetAnnotHandlerMgr();
254 return pAnnotHandlerMgr->Annot_Undo(pAnnot);
255 }
256 return false;
257}
258
259bool CPDFSDK_PageView::Redo() {
260 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
261 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
262 m_pFormFillEnv->GetAnnotHandlerMgr();
263 return pAnnotHandlerMgr->Annot_Redo(pAnnot);
264 }
265 return false;
266}
267
Lei Zhang63b01262017-08-31 08:54:46 -0700268bool CPDFSDK_PageView::OnFocus(const CFX_PointF& point, uint32_t nFlag) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000269 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
Lei Zhang63b01262017-08-31 08:54:46 -0700270 if (!pAnnot) {
271 m_pFormFillEnv->KillFocusAnnot(nFlag);
272 return false;
273 }
274
275 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
276 return true;
277}
278
Dan Sinclairf528eee2017-02-14 11:52:07 -0500279bool CPDFSDK_PageView::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000280 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700281 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700282 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700283 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700284 }
285
dsinclairb402b172016-10-11 09:26:32 -0700286 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
287 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700288 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700289 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700290
291 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700292 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700293
dsinclair7cbe68e2016-10-12 11:56:23 -0700294 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700295 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700296}
297
Lei Zhanga37989f2018-10-17 20:32:41 +0000298bool CPDFSDK_PageView::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
299 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
300 m_pFormFillEnv->GetAnnotHandlerMgr();
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000301 ObservedPtr<CPDFSDK_Annot> pFXAnnot(GetFXWidgetAtPoint(point));
302 ObservedPtr<CPDFSDK_Annot> pFocusAnnot(GetFocusAnnot());
Lei Zhanga37989f2018-10-17 20:32:41 +0000303 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
304 // Last focus Annot gets a chance to handle the event.
305 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
306 return true;
307 }
308 return pFXAnnot &&
309 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
310}
311
312bool CPDFSDK_PageView::OnLButtonDblClk(const CFX_PointF& point,
313 uint32_t nFlag) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000314 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
Lei Zhanga37989f2018-10-17 20:32:41 +0000315 if (!pAnnot) {
316 m_pFormFillEnv->KillFocusAnnot(nFlag);
317 return false;
318 }
319
320 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
321 m_pFormFillEnv->GetAnnotHandlerMgr();
322 if (!pAnnotHandlerMgr->Annot_OnLButtonDblClk(this, &pAnnot, nFlag, point))
323 return false;
324
325 if (!pAnnot)
326 return false;
327
328 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
329 return true;
330}
331
Dan Sinclairf528eee2017-02-14 11:52:07 -0500332bool CPDFSDK_PageView::OnRButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000333 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700334 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700335 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700336
dsinclairb402b172016-10-11 09:26:32 -0700337 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
338 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700339 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700340 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700341 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700342
343 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700344 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700345
tsepez4cf55152016-11-02 14:37:54 -0700346 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700347}
348
Dan Sinclairf528eee2017-02-14 11:52:07 -0500349bool CPDFSDK_PageView::OnRButtonUp(const CFX_PointF& point, uint32_t nFlag) {
Tom Sepezb2745de2020-01-15 21:50:48 +0000350 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
351 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700352 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700353
Tom Sepezb2745de2020-01-15 21:50:48 +0000354 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
355 m_pFormFillEnv->GetAnnotHandlerMgr();
356 bool ok = pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pAnnot, nFlag, point);
357 if (!pAnnot)
358 return false;
359
360 if (ok)
361 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700362
tsepez4cf55152016-11-02 14:37:54 -0700363 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700364}
dsinclairf34518b2016-09-13 12:03:48 -0700365
Dan Sinclairf528eee2017-02-14 11:52:07 -0500366bool CPDFSDK_PageView::OnMouseMove(const CFX_PointF& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700367 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
368 m_pFormFillEnv->GetAnnotHandlerMgr();
Tom Sepezc3a91732019-11-01 21:53:00 +0000369
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000370 ObservedPtr<CPDFSDK_Annot> pFXAnnot(GetFXAnnotAtPoint(point));
Tom Sepezc3a91732019-11-01 21:53:00 +0000371 ObservedPtr<CPDFSDK_PageView> pThis(this);
Henrique Nakashima65513622017-08-16 14:48:13 -0400372
Henrique Nakashima4a652542017-08-22 10:59:49 -0400373 if (m_bOnWidget && m_pCaptureWidget != pFXAnnot)
374 ExitWidget(pAnnotHandlerMgr, true, nFlag);
375
Tom Sepezc3a91732019-11-01 21:53:00 +0000376 // ExitWidget() may have invalidated objects.
377 if (!pThis || !pFXAnnot)
378 return false;
Henrique Nakashima65513622017-08-16 14:48:13 -0400379
Tom Sepezc3a91732019-11-01 21:53:00 +0000380 if (!m_bOnWidget) {
381 EnterWidget(pAnnotHandlerMgr, &pFXAnnot, nFlag);
382
383 // EnterWidget() may have invalidated objects.
384 if (!pThis)
385 return false;
386
387 if (!pFXAnnot) {
388 ExitWidget(pAnnotHandlerMgr, false, nFlag);
389 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700390 }
dsinclairf34518b2016-09-13 12:03:48 -0700391 }
Tom Sepezc3a91732019-11-01 21:53:00 +0000392 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
393 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700394}
395
Henrique Nakashima4a652542017-08-22 10:59:49 -0400396void CPDFSDK_PageView::EnterWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000397 ObservedPtr<CPDFSDK_Annot>* pAnnot,
Henrique Nakashima4a652542017-08-22 10:59:49 -0400398 uint32_t nFlag) {
399 m_bOnWidget = true;
400 m_pCaptureWidget.Reset(pAnnot->Get());
401 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pAnnot, nFlag);
402}
403
404void CPDFSDK_PageView::ExitWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
405 bool callExitCallback,
406 uint32_t nFlag) {
407 m_bOnWidget = false;
408 if (m_pCaptureWidget) {
409 if (callExitCallback)
410 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
411
412 m_pCaptureWidget.Reset();
413 }
414}
415
tsepez4cf55152016-11-02 14:37:54 -0700416bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
417 double deltaY,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500418 const CFX_PointF& point,
tsepez4cf55152016-11-02 14:37:54 -0700419 int nFlag) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000420 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700421 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700422 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700423
dsinclairb402b172016-10-11 09:26:32 -0700424 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
425 m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang222e1a42017-06-20 14:47:00 -0700426 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag,
427 static_cast<int>(deltaY), point);
dsinclairf34518b2016-09-13 12:03:48 -0700428}
429
rycsmitha5230e22019-02-21 17:33:03 +0000430bool CPDFSDK_PageView::SetIndexSelected(int index, bool selected) {
431 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000432 ObservedPtr<CPDFSDK_Annot> pAnnotObserved(pAnnot);
rycsmitha5230e22019-02-21 17:33:03 +0000433 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
434 m_pFormFillEnv->GetAnnotHandlerMgr();
435 return pAnnotHandlerMgr->Annot_SetIndexSelected(&pAnnotObserved, index,
436 selected);
437 }
438
439 return false;
440}
441
442bool CPDFSDK_PageView::IsIndexSelected(int index) {
443 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000444 ObservedPtr<CPDFSDK_Annot> pAnnotObserved(pAnnot);
rycsmitha5230e22019-02-21 17:33:03 +0000445 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
446 m_pFormFillEnv->GetAnnotHandlerMgr();
447 return pAnnotHandlerMgr->Annot_IsIndexSelected(&pAnnotObserved, index);
448 }
449
450 return false;
451}
452
tsepez4cf55152016-11-02 14:37:54 -0700453bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700454 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700455 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
456 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700457 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
458 }
459
tsepez4cf55152016-11-02 14:37:54 -0700460 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700461}
462
tsepez4cf55152016-11-02 14:37:54 -0700463bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700464 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700465 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
466 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700467 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
468 }
tsepez4cf55152016-11-02 14:37:54 -0700469 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700470}
471
tsepez4cf55152016-11-02 14:37:54 -0700472bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
473 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700474}
475
476void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700477 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
478 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700479
Dan Sinclairb8966992017-09-21 14:51:57 -0400480 AutoRestorer<bool> lock(&m_bLocked);
Henrique Nakashima65513622017-08-16 14:48:13 -0400481 m_bLocked = true;
dsinclairf34518b2016-09-13 12:03:48 -0700482
483#ifdef PDF_ENABLE_XFA
Tom Sepez101535f2018-06-12 13:36:05 +0000484 RetainPtr<CPDFXFA_Page> protector(ToXFAPage(m_page));
Tom Sepez0126b032019-08-19 16:48:25 +0000485 auto* pContext = m_pFormFillEnv->GetDocExtension();
486 if (pContext->ContainsExtensionFullForm()) {
Tom Sepez101535f2018-06-12 13:36:05 +0000487 CXFA_FFPageView* pageView = protector->GetXFAPageView();
Tom Sepezc2564692020-01-09 23:34:16 +0000488 std::unique_ptr<IXFA_WidgetIterator> pWidgetHandler =
489 pageView->CreateFormWidgetIterator(XFA_WidgetStatus_Visible |
490 XFA_WidgetStatus_Viewable);
dsinclairf34518b2016-09-13 12:03:48 -0700491
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700492 while (CXFA_FFWidget* pXFAAnnot = pWidgetHandler->MoveToNext()) {
Lei Zhang51d02662019-10-31 18:28:51 +0000493 std::unique_ptr<CPDFSDK_Annot> pNewAnnot =
494 pAnnotHandlerMgr->NewXFAAnnot(pXFAAnnot, this);
495 ASSERT(pNewAnnot);
496 CPDFSDK_Annot* pAnnot = pNewAnnot.get();
497 m_SDKAnnotArray.push_back(pNewAnnot.release());
dsinclairf34518b2016-09-13 12:03:48 -0700498 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
499 }
500
dsinclairf34518b2016-09-13 12:03:48 -0700501 return;
502 }
503#endif // PDF_ENABLE_XFA
504
505 CPDF_Page* pPage = GetPDFPage();
506 ASSERT(pPage);
Lei Zhang073ecf42018-10-11 16:56:00 +0000507 bool bUpdateAP = CPDF_InteractiveForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700508 // Disable the default AP construction.
Lei Zhang073ecf42018-10-11 16:56:00 +0000509 CPDF_InteractiveForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700510 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
Lei Zhang073ecf42018-10-11 16:56:00 +0000511 CPDF_InteractiveForm::SetUpdateAP(bUpdateAP);
dsinclairf34518b2016-09-13 12:03:48 -0700512
513 const size_t nCount = m_pAnnotList->Count();
514 for (size_t i = 0; i < nCount; ++i) {
515 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang4efdb512019-02-26 19:48:39 +0000516 CheckForUnsupportedAnnot(pPDFAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700517 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
518 if (!pAnnot)
519 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700520 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700521 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
522 }
dsinclairf34518b2016-09-13 12:03:48 -0700523}
524
dsinclairf34518b2016-09-13 12:03:48 -0700525void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700526 for (const auto& rc : rects)
Lei Zhang77f9bff2017-08-29 11:34:12 -0700527 m_pFormFillEnv->Invalidate(m_page, rc.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700528}
529
530void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
531 CFX_FloatRect rcWindow = pAnnot->GetRect();
Lei Zhang77f9bff2017-08-29 11:34:12 -0700532 m_pFormFillEnv->Invalidate(m_page, rcWindow.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700533}
534
535int CPDFSDK_PageView::GetPageIndex() const {
dsinclairf34518b2016-09-13 12:03:48 -0700536#ifdef PDF_ENABLE_XFA
Tom Sepez9bf01812019-08-19 18:59:27 +0000537 CPDF_Document::Extension* pContext = m_page->GetDocument()->GetExtension();
Tom Sepez0126b032019-08-19 16:48:25 +0000538 if (pContext->ContainsExtensionFullForm()) {
539 CXFA_FFPageView* pPageView = m_page->AsXFAPage()->GetXFAPageView();
540 return pPageView ? pPageView->GetLayoutItem()->GetPageIndex() : -1;
dsinclairf34518b2016-09-13 12:03:48 -0700541 }
dsinclairf34518b2016-09-13 12:03:48 -0700542#endif // PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400543 return GetPageIndexForStaticPDF();
dsinclairf34518b2016-09-13 12:03:48 -0700544}
545
546bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
547 if (!p)
548 return false;
549
550 const auto& annots = m_pAnnotList->All();
551 auto it = std::find_if(annots.begin(), annots.end(),
552 [p](const std::unique_ptr<CPDF_Annot>& annot) {
553 return annot.get() == p;
554 });
555 return it != annots.end();
556}
557
dsinclair8afe15a2016-10-05 12:00:34 -0700558bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
559 if (!p)
560 return false;
561 return pdfium::ContainsValue(m_SDKAnnotArray, p);
562}
563
dsinclairf34518b2016-09-13 12:03:48 -0700564CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700565 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700566 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700567}
568
569int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
Henrique Nakashima888af472018-06-07 19:43:42 +0000570 const CPDF_Dictionary* pDict = GetPDFPage()->GetDict();
dsinclair7cbe68e2016-10-12 11:56:23 -0700571 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
Lei Zhangf97fd062019-08-09 22:18:39 +0000572 return pDoc->GetPageIndex(pDict->GetObjNum());
dsinclairf34518b2016-09-13 12:03:48 -0700573}