blob: 5aeb2dfb7fe0cbf73ce39e3e13e26d76e16070bd [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) {
126 if (!pPDFAnnot)
127 return nullptr;
128
129 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
130 if (pSDKAnnot)
131 return pSDKAnnot;
132
dsinclairb402b172016-10-11 09:26:32 -0700133 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang51d02662019-10-31 18:28:51 +0000134 std::unique_ptr<CPDFSDK_Annot> pNewAnnot =
135 pAnnotHandler->NewXFAAnnot(pPDFAnnot, this);
136 ASSERT(pNewAnnot);
137 pSDKAnnot = pNewAnnot.get();
138 // TODO(thestig): See if |m_SDKAnnotArray|, which takes ownership of
139 // |pNewAnnot|, can hold std::unique_ptrs instead of raw pointers.
140 m_SDKAnnotArray.push_back(pNewAnnot.release());
dsinclairf34518b2016-09-13 12:03:48 -0700141 return pSDKAnnot;
142}
dsinclairf34518b2016-09-13 12:03:48 -0700143
tsepez4cf55152016-11-02 14:37:54 -0700144bool CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
dsinclairf34518b2016-09-13 12:03:48 -0700145 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700146 return false;
Tom Sepezbfa2a972017-07-24 11:38:31 -0700147
Tom Sepez9bf01812019-08-19 18:59:27 +0000148 IPDF_Page* pPage = pAnnot->GetXFAPage();
Tom Sepezccd94212018-05-03 13:53:02 +0000149 if (!pPage)
150 return false;
151
Tom Sepez9bf01812019-08-19 18:59:27 +0000152 CPDF_Document::Extension* pContext = pPage->GetDocument()->GetExtension();
Tom Sepezd48bd292019-08-14 19:48:55 +0000153 if (!pContext->ContainsExtensionForm())
tsepez4cf55152016-11-02 14:37:54 -0700154 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700155
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000156 ObservedPtr<CPDFSDK_Annot> pObserved(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700157 if (GetFocusAnnot() == pAnnot)
Tom Sepezbfa2a972017-07-24 11:38:31 -0700158 m_pFormFillEnv->KillFocusAnnot(0); // May invoke JS, invalidating pAnnot.
159
160 if (pObserved) {
161 CPDFSDK_AnnotHandlerMgr* pAnnotHandler =
162 m_pFormFillEnv->GetAnnotHandlerMgr();
163 if (pAnnotHandler)
Tom Sepez0f35a9e2019-04-23 00:32:37 +0000164 pAnnotHandler->ReleaseAnnot(pdfium::WrapUnique(pObserved.Get()));
Tom Sepezbfa2a972017-07-24 11:38:31 -0700165 }
dsinclairf34518b2016-09-13 12:03:48 -0700166
dsinclair8afe15a2016-10-05 12:00:34 -0700167 auto it = std::find(m_SDKAnnotArray.begin(), m_SDKAnnotArray.end(), pAnnot);
168 if (it != m_SDKAnnotArray.end())
169 m_SDKAnnotArray.erase(it);
tsepezf8074ce2016-09-27 14:29:57 -0700170 if (m_pCaptureWidget.Get() == pAnnot)
171 m_pCaptureWidget.Reset();
dsinclairf34518b2016-09-13 12:03:48 -0700172
tsepez4cf55152016-11-02 14:37:54 -0700173 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700174}
dsinclair8afe15a2016-10-05 12:00:34 -0700175#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -0700176
177CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
Tom Sepez7b0ff3c2018-10-04 22:19:32 +0000178 return m_page->GetDocument();
dsinclairf34518b2016-09-13 12:03:48 -0700179}
180
181CPDF_Page* CPDFSDK_PageView::GetPDFPage() const {
Tom Sepez101535f2018-06-12 13:36:05 +0000182 return ToPDFPage(m_page);
dsinclairf34518b2016-09-13 12:03:48 -0700183}
184
dsinclairf34518b2016-09-13 12:03:48 -0700185CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
dsinclair8afe15a2016-10-05 12:00:34 -0700186 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
dsinclairf34518b2016-09-13 12:03:48 -0700187 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
188 return pAnnot;
189 }
190 return nullptr;
191}
192
193#ifdef PDF_ENABLE_XFA
194CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(CXFA_FFWidget* hWidget) {
195 if (!hWidget)
196 return nullptr;
197
dsinclair8afe15a2016-10-05 12:00:34 -0700198 for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray) {
Tom Sepez451ed2d2019-08-19 22:55:31 +0000199 if (ToXFAWidget(pAnnot)->GetXFAFFWidget() == hWidget)
dsinclairf34518b2016-09-13 12:03:48 -0700200 return pAnnot;
201 }
202 return nullptr;
203}
Tom Sepez9bf01812019-08-19 18:59:27 +0000204
205IPDF_Page* CPDFSDK_PageView::GetXFAPage() {
206 return ToXFAPage(m_page);
207}
dsinclairf34518b2016-09-13 12:03:48 -0700208#endif // PDF_ENABLE_XFA
209
Lei Zhanga4c7ac42018-04-17 15:12:58 +0000210WideString CPDFSDK_PageView::GetFocusedFormText() {
211 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
212 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
213 m_pFormFillEnv->GetAnnotHandlerMgr();
214 return pAnnotHandlerMgr->Annot_GetText(pAnnot);
215 }
216
217 return WideString();
218}
219
Ryan Harrison275e2602017-09-18 14:23:18 -0400220WideString CPDFSDK_PageView::GetSelectedText() {
Diana Gagedce2d722017-06-20 11:17:11 -0700221 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
222 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
223 m_pFormFillEnv->GetAnnotHandlerMgr();
224 return pAnnotHandlerMgr->Annot_GetSelectedText(pAnnot);
225 }
226
Ryan Harrison275e2602017-09-18 14:23:18 -0400227 return WideString();
Diana Gagedce2d722017-06-20 11:17:11 -0700228}
229
Ryan Harrison275e2602017-09-18 14:23:18 -0400230void CPDFSDK_PageView::ReplaceSelection(const WideString& text) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700231 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
232 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
233 m_pFormFillEnv->GetAnnotHandlerMgr();
Diana Gageab390972017-07-28 17:07:39 -0700234 pAnnotHandlerMgr->Annot_ReplaceSelection(pAnnot, text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700235 }
236}
237
Lei Zhangee967722018-04-19 20:55:54 +0000238bool CPDFSDK_PageView::CanUndo() {
239 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
240 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
241 m_pFormFillEnv->GetAnnotHandlerMgr();
242 return pAnnotHandlerMgr->Annot_CanUndo(pAnnot);
243 }
244 return false;
245}
246
247bool CPDFSDK_PageView::CanRedo() {
248 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
249 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
250 m_pFormFillEnv->GetAnnotHandlerMgr();
251 return pAnnotHandlerMgr->Annot_CanRedo(pAnnot);
252 }
253 return false;
254}
255
256bool CPDFSDK_PageView::Undo() {
257 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
258 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
259 m_pFormFillEnv->GetAnnotHandlerMgr();
260 return pAnnotHandlerMgr->Annot_Undo(pAnnot);
261 }
262 return false;
263}
264
265bool CPDFSDK_PageView::Redo() {
266 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
267 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
268 m_pFormFillEnv->GetAnnotHandlerMgr();
269 return pAnnotHandlerMgr->Annot_Redo(pAnnot);
270 }
271 return false;
272}
273
Lei Zhang63b01262017-08-31 08:54:46 -0700274bool CPDFSDK_PageView::OnFocus(const CFX_PointF& point, uint32_t nFlag) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000275 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
Lei Zhang63b01262017-08-31 08:54:46 -0700276 if (!pAnnot) {
277 m_pFormFillEnv->KillFocusAnnot(nFlag);
278 return false;
279 }
280
281 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
282 return true;
283}
284
Dan Sinclairf528eee2017-02-14 11:52:07 -0500285bool CPDFSDK_PageView::OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000286 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700287 if (!pAnnot) {
dsinclair7cbe68e2016-10-12 11:56:23 -0700288 m_pFormFillEnv->KillFocusAnnot(nFlag);
tsepez4cf55152016-11-02 14:37:54 -0700289 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700290 }
291
dsinclairb402b172016-10-11 09:26:32 -0700292 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
293 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepezf8074ce2016-09-27 14:29:57 -0700294 if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
tsepez4cf55152016-11-02 14:37:54 -0700295 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700296
297 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700298 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700299
dsinclair7cbe68e2016-10-12 11:56:23 -0700300 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
tsepez4cf55152016-11-02 14:37:54 -0700301 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700302}
303
Lei Zhanga37989f2018-10-17 20:32:41 +0000304bool CPDFSDK_PageView::OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) {
305 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
306 m_pFormFillEnv->GetAnnotHandlerMgr();
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000307 ObservedPtr<CPDFSDK_Annot> pFXAnnot(GetFXWidgetAtPoint(point));
308 ObservedPtr<CPDFSDK_Annot> pFocusAnnot(GetFocusAnnot());
Lei Zhanga37989f2018-10-17 20:32:41 +0000309 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
310 // Last focus Annot gets a chance to handle the event.
311 if (pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFocusAnnot, nFlag, point))
312 return true;
313 }
314 return pFXAnnot &&
315 pAnnotHandlerMgr->Annot_OnLButtonUp(this, &pFXAnnot, nFlag, point);
316}
317
318bool CPDFSDK_PageView::OnLButtonDblClk(const CFX_PointF& point,
319 uint32_t nFlag) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000320 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
Lei Zhanga37989f2018-10-17 20:32:41 +0000321 if (!pAnnot) {
322 m_pFormFillEnv->KillFocusAnnot(nFlag);
323 return false;
324 }
325
326 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
327 m_pFormFillEnv->GetAnnotHandlerMgr();
328 if (!pAnnotHandlerMgr->Annot_OnLButtonDblClk(this, &pAnnot, nFlag, point))
329 return false;
330
331 if (!pAnnot)
332 return false;
333
334 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
335 return true;
336}
337
Dan Sinclairf528eee2017-02-14 11:52:07 -0500338bool CPDFSDK_PageView::OnRButtonDown(const CFX_PointF& point, uint32_t nFlag) {
Tom Sepez22d97f22019-12-04 20:18:03 +0000339#ifdef PDF_ENABLE_XFA
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000340 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700341 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700342 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700343
dsinclairb402b172016-10-11 09:26:32 -0700344 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
345 m_pFormFillEnv->GetAnnotHandlerMgr();
tsepez4cf55152016-11-02 14:37:54 -0700346 bool ok = pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
tsepezf8074ce2016-09-27 14:29:57 -0700347 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700348 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700349
350 if (ok)
dsinclair7cbe68e2016-10-12 11:56:23 -0700351 m_pFormFillEnv->SetFocusAnnot(&pAnnot);
Tom Sepez22d97f22019-12-04 20:18:03 +0000352#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -0700353
tsepez4cf55152016-11-02 14:37:54 -0700354 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700355}
356
Dan Sinclairf528eee2017-02-14 11:52:07 -0500357bool CPDFSDK_PageView::OnRButtonUp(const CFX_PointF& point, uint32_t nFlag) {
Tom Sepez22d97f22019-12-04 20:18:03 +0000358#ifdef PDF_ENABLE_XFA
dsinclairb402b172016-10-11 09:26:32 -0700359 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
360 m_pFormFillEnv->GetAnnotHandlerMgr();
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000361 ObservedPtr<CPDFSDK_Annot> pFXAnnot(GetFXWidgetAtPoint(point));
dsinclairf34518b2016-09-13 12:03:48 -0700362 if (!pFXAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700363 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700364
tsepezf8074ce2016-09-27 14:29:57 -0700365 if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
dsinclair7cbe68e2016-10-12 11:56:23 -0700366 m_pFormFillEnv->SetFocusAnnot(&pFXAnnot);
Tom Sepez22d97f22019-12-04 20:18:03 +0000367#endif // PDF_ENABLE_XFA
dsinclairf34518b2016-09-13 12:03:48 -0700368
tsepez4cf55152016-11-02 14:37:54 -0700369 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700370}
dsinclairf34518b2016-09-13 12:03:48 -0700371
Dan Sinclairf528eee2017-02-14 11:52:07 -0500372bool CPDFSDK_PageView::OnMouseMove(const CFX_PointF& point, int nFlag) {
dsinclairb402b172016-10-11 09:26:32 -0700373 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
374 m_pFormFillEnv->GetAnnotHandlerMgr();
Tom Sepezc3a91732019-11-01 21:53:00 +0000375
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000376 ObservedPtr<CPDFSDK_Annot> pFXAnnot(GetFXAnnotAtPoint(point));
Tom Sepezc3a91732019-11-01 21:53:00 +0000377 ObservedPtr<CPDFSDK_PageView> pThis(this);
Henrique Nakashima65513622017-08-16 14:48:13 -0400378
Henrique Nakashima4a652542017-08-22 10:59:49 -0400379 if (m_bOnWidget && m_pCaptureWidget != pFXAnnot)
380 ExitWidget(pAnnotHandlerMgr, true, nFlag);
381
Tom Sepezc3a91732019-11-01 21:53:00 +0000382 // ExitWidget() may have invalidated objects.
383 if (!pThis || !pFXAnnot)
384 return false;
Henrique Nakashima65513622017-08-16 14:48:13 -0400385
Tom Sepezc3a91732019-11-01 21:53:00 +0000386 if (!m_bOnWidget) {
387 EnterWidget(pAnnotHandlerMgr, &pFXAnnot, nFlag);
388
389 // EnterWidget() may have invalidated objects.
390 if (!pThis)
391 return false;
392
393 if (!pFXAnnot) {
394 ExitWidget(pAnnotHandlerMgr, false, nFlag);
395 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700396 }
dsinclairf34518b2016-09-13 12:03:48 -0700397 }
Tom Sepezc3a91732019-11-01 21:53:00 +0000398 pAnnotHandlerMgr->Annot_OnMouseMove(this, &pFXAnnot, nFlag, point);
399 return true;
dsinclairf34518b2016-09-13 12:03:48 -0700400}
401
Henrique Nakashima4a652542017-08-22 10:59:49 -0400402void CPDFSDK_PageView::EnterWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000403 ObservedPtr<CPDFSDK_Annot>* pAnnot,
Henrique Nakashima4a652542017-08-22 10:59:49 -0400404 uint32_t nFlag) {
405 m_bOnWidget = true;
406 m_pCaptureWidget.Reset(pAnnot->Get());
407 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pAnnot, nFlag);
408}
409
410void CPDFSDK_PageView::ExitWidget(CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr,
411 bool callExitCallback,
412 uint32_t nFlag) {
413 m_bOnWidget = false;
414 if (m_pCaptureWidget) {
415 if (callExitCallback)
416 pAnnotHandlerMgr->Annot_OnMouseExit(this, &m_pCaptureWidget, nFlag);
417
418 m_pCaptureWidget.Reset();
419 }
420}
421
tsepez4cf55152016-11-02 14:37:54 -0700422bool CPDFSDK_PageView::OnMouseWheel(double deltaX,
423 double deltaY,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500424 const CFX_PointF& point,
tsepez4cf55152016-11-02 14:37:54 -0700425 int nFlag) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000426 ObservedPtr<CPDFSDK_Annot> pAnnot(GetFXWidgetAtPoint(point));
tsepezf8074ce2016-09-27 14:29:57 -0700427 if (!pAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700428 return false;
tsepezf8074ce2016-09-27 14:29:57 -0700429
dsinclairb402b172016-10-11 09:26:32 -0700430 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
431 m_pFormFillEnv->GetAnnotHandlerMgr();
Lei Zhang222e1a42017-06-20 14:47:00 -0700432 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag,
433 static_cast<int>(deltaY), point);
dsinclairf34518b2016-09-13 12:03:48 -0700434}
435
rycsmitha5230e22019-02-21 17:33:03 +0000436bool CPDFSDK_PageView::SetIndexSelected(int index, bool selected) {
437 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000438 ObservedPtr<CPDFSDK_Annot> pAnnotObserved(pAnnot);
rycsmitha5230e22019-02-21 17:33:03 +0000439 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
440 m_pFormFillEnv->GetAnnotHandlerMgr();
441 return pAnnotHandlerMgr->Annot_SetIndexSelected(&pAnnotObserved, index,
442 selected);
443 }
444
445 return false;
446}
447
448bool CPDFSDK_PageView::IsIndexSelected(int index) {
449 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000450 ObservedPtr<CPDFSDK_Annot> pAnnotObserved(pAnnot);
rycsmitha5230e22019-02-21 17:33:03 +0000451 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
452 m_pFormFillEnv->GetAnnotHandlerMgr();
453 return pAnnotHandlerMgr->Annot_IsIndexSelected(&pAnnotObserved, index);
454 }
455
456 return false;
457}
458
tsepez4cf55152016-11-02 14:37:54 -0700459bool CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700460 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700461 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
462 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700463 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
464 }
465
tsepez4cf55152016-11-02 14:37:54 -0700466 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700467}
468
tsepez4cf55152016-11-02 14:37:54 -0700469bool CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
dsinclairf34518b2016-09-13 12:03:48 -0700470 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
dsinclairb402b172016-10-11 09:26:32 -0700471 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
472 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700473 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
474 }
tsepez4cf55152016-11-02 14:37:54 -0700475 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700476}
477
tsepez4cf55152016-11-02 14:37:54 -0700478bool CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
479 return false;
dsinclairf34518b2016-09-13 12:03:48 -0700480}
481
482void CPDFSDK_PageView::LoadFXAnnots() {
dsinclairb402b172016-10-11 09:26:32 -0700483 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
484 m_pFormFillEnv->GetAnnotHandlerMgr();
dsinclairf34518b2016-09-13 12:03:48 -0700485
Dan Sinclairb8966992017-09-21 14:51:57 -0400486 AutoRestorer<bool> lock(&m_bLocked);
Henrique Nakashima65513622017-08-16 14:48:13 -0400487 m_bLocked = true;
dsinclairf34518b2016-09-13 12:03:48 -0700488
489#ifdef PDF_ENABLE_XFA
Tom Sepez101535f2018-06-12 13:36:05 +0000490 RetainPtr<CPDFXFA_Page> protector(ToXFAPage(m_page));
Tom Sepez0126b032019-08-19 16:48:25 +0000491 auto* pContext = m_pFormFillEnv->GetDocExtension();
492 if (pContext->ContainsExtensionFullForm()) {
Tom Sepez101535f2018-06-12 13:36:05 +0000493 CXFA_FFPageView* pageView = protector->GetXFAPageView();
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700494 std::unique_ptr<IXFA_WidgetIterator> pWidgetHandler(
dsinclairf34518b2016-09-13 12:03:48 -0700495 pageView->CreateWidgetIterator(
496 XFA_TRAVERSEWAY_Form,
497 XFA_WidgetStatus_Visible | XFA_WidgetStatus_Viewable));
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700498 if (!pWidgetHandler) {
dsinclairf34518b2016-09-13 12:03:48 -0700499 return;
500 }
501
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700502 while (CXFA_FFWidget* pXFAAnnot = pWidgetHandler->MoveToNext()) {
Lei Zhang51d02662019-10-31 18:28:51 +0000503 std::unique_ptr<CPDFSDK_Annot> pNewAnnot =
504 pAnnotHandlerMgr->NewXFAAnnot(pXFAAnnot, this);
505 ASSERT(pNewAnnot);
506 CPDFSDK_Annot* pAnnot = pNewAnnot.get();
507 m_SDKAnnotArray.push_back(pNewAnnot.release());
dsinclairf34518b2016-09-13 12:03:48 -0700508 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
509 }
510
dsinclairf34518b2016-09-13 12:03:48 -0700511 return;
512 }
513#endif // PDF_ENABLE_XFA
514
515 CPDF_Page* pPage = GetPDFPage();
516 ASSERT(pPage);
Lei Zhang073ecf42018-10-11 16:56:00 +0000517 bool bUpdateAP = CPDF_InteractiveForm::IsUpdateAPEnabled();
dsinclairf34518b2016-09-13 12:03:48 -0700518 // Disable the default AP construction.
Lei Zhang073ecf42018-10-11 16:56:00 +0000519 CPDF_InteractiveForm::SetUpdateAP(false);
tsepez36eb4bd2016-10-03 15:24:27 -0700520 m_pAnnotList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
Lei Zhang073ecf42018-10-11 16:56:00 +0000521 CPDF_InteractiveForm::SetUpdateAP(bUpdateAP);
dsinclairf34518b2016-09-13 12:03:48 -0700522
523 const size_t nCount = m_pAnnotList->Count();
524 for (size_t i = 0; i < nCount; ++i) {
525 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang4efdb512019-02-26 19:48:39 +0000526 CheckForUnsupportedAnnot(pPDFAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700527 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
528 if (!pAnnot)
529 continue;
dsinclair8afe15a2016-10-05 12:00:34 -0700530 m_SDKAnnotArray.push_back(pAnnot);
dsinclairf34518b2016-09-13 12:03:48 -0700531 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
532 }
dsinclairf34518b2016-09-13 12:03:48 -0700533}
534
dsinclairf34518b2016-09-13 12:03:48 -0700535void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
dsinclairf34518b2016-09-13 12:03:48 -0700536 for (const auto& rc : rects)
Lei Zhang77f9bff2017-08-29 11:34:12 -0700537 m_pFormFillEnv->Invalidate(m_page, rc.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700538}
539
540void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
541 CFX_FloatRect rcWindow = pAnnot->GetRect();
Lei Zhang77f9bff2017-08-29 11:34:12 -0700542 m_pFormFillEnv->Invalidate(m_page, rcWindow.GetOuterRect());
dsinclairf34518b2016-09-13 12:03:48 -0700543}
544
545int CPDFSDK_PageView::GetPageIndex() const {
dsinclairf34518b2016-09-13 12:03:48 -0700546#ifdef PDF_ENABLE_XFA
Tom Sepez9bf01812019-08-19 18:59:27 +0000547 CPDF_Document::Extension* pContext = m_page->GetDocument()->GetExtension();
Tom Sepez0126b032019-08-19 16:48:25 +0000548 if (pContext->ContainsExtensionFullForm()) {
549 CXFA_FFPageView* pPageView = m_page->AsXFAPage()->GetXFAPageView();
550 return pPageView ? pPageView->GetLayoutItem()->GetPageIndex() : -1;
dsinclairf34518b2016-09-13 12:03:48 -0700551 }
dsinclairf34518b2016-09-13 12:03:48 -0700552#endif // PDF_ENABLE_XFA
Ryan Harrison854d71c2017-10-18 12:28:14 -0400553 return GetPageIndexForStaticPDF();
dsinclairf34518b2016-09-13 12:03:48 -0700554}
555
556bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
557 if (!p)
558 return false;
559
560 const auto& annots = m_pAnnotList->All();
561 auto it = std::find_if(annots.begin(), annots.end(),
562 [p](const std::unique_ptr<CPDF_Annot>& annot) {
563 return annot.get() == p;
564 });
565 return it != annots.end();
566}
567
dsinclair8afe15a2016-10-05 12:00:34 -0700568bool CPDFSDK_PageView::IsValidSDKAnnot(const CPDFSDK_Annot* p) const {
569 if (!p)
570 return false;
571 return pdfium::ContainsValue(m_SDKAnnotArray, p);
572}
573
dsinclairf34518b2016-09-13 12:03:48 -0700574CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
dsinclair7cbe68e2016-10-12 11:56:23 -0700575 CPDFSDK_Annot* pFocusAnnot = m_pFormFillEnv->GetFocusAnnot();
dsinclair8afe15a2016-10-05 12:00:34 -0700576 return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
dsinclairf34518b2016-09-13 12:03:48 -0700577}
578
579int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
Henrique Nakashima888af472018-06-07 19:43:42 +0000580 const CPDF_Dictionary* pDict = GetPDFPage()->GetDict();
dsinclair7cbe68e2016-10-12 11:56:23 -0700581 CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
Lei Zhangf97fd062019-08-09 22:18:39 +0000582 return pDoc->GetPageIndex(pDict->GetObjNum());
dsinclairf34518b2016-09-13 12:03:48 -0700583}