blob: 038890d67ffc2b279b6fd7efa9dee5ba12733481 [file] [log] [blame]
jaepark8c541822016-08-30 13:43:05 -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_widgethandler.h"
jaepark8c541822016-08-30 13:43:05 -07008
9#include <memory>
10#include <vector>
11
dsinclair41872fa2016-10-04 11:29:35 -070012#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070013#include "core/fpdfapi/parser/cpdf_document.h"
Lei Zhangc3450652018-10-11 16:54:42 +000014#include "core/fpdfdoc/cpdf_interactiveform.h"
dsinclair114e46a2016-09-29 17:18:21 -070015#include "fpdfsdk/cpdfsdk_annot.h"
dsinclair735606d2016-10-05 15:47:02 -070016#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
Lei Zhangc3450652018-10-11 16:54:42 +000017#include "fpdfsdk/cpdfsdk_interactiveform.h"
dsinclair114e46a2016-09-29 17:18:21 -070018#include "fpdfsdk/cpdfsdk_pageview.h"
19#include "fpdfsdk/cpdfsdk_widget.h"
jaepark8c541822016-08-30 13:43:05 -070020#include "fpdfsdk/formfiller/cffl_formfiller.h"
jaepark8c541822016-08-30 13:43:05 -070021
22#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070023#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
jaepark8c541822016-08-30 13:43:05 -070024#endif // PDF_ENABLE_XFA
25
dsinclair8779fa82016-10-12 12:05:44 -070026CPDFSDK_WidgetHandler::CPDFSDK_WidgetHandler(
27 CPDFSDK_FormFillEnvironment* pFormFillEnv)
Lei Zhang60fa2fc2017-07-21 17:42:19 -070028 : m_pFormFillEnv(pFormFillEnv),
Lei Zhang7db136a2018-10-10 21:34:17 +000029 m_pFormFiller(pFormFillEnv->GetInteractiveFormFiller()) {
30 ASSERT(m_pFormFillEnv);
31 ASSERT(m_pFormFiller);
32}
jaepark8c541822016-08-30 13:43:05 -070033
Lei Zhang7db136a2018-10-10 21:34:17 +000034CPDFSDK_WidgetHandler::~CPDFSDK_WidgetHandler() = default;
jaepark8c541822016-08-30 13:43:05 -070035
tsepez4cf55152016-11-02 14:37:54 -070036bool CPDFSDK_WidgetHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
Tom Sepez11a6bec2018-07-25 23:25:55 +000037 CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
38 if (pWidget->IsSignatureWidget())
tsepez4cf55152016-11-02 14:37:54 -070039 return false;
jaepark8c541822016-08-30 13:43:05 -070040
jaepark8c541822016-08-30 13:43:05 -070041 if (!pWidget->IsVisible())
tsepez4cf55152016-11-02 14:37:54 -070042 return false;
jaepark8c541822016-08-30 13:43:05 -070043
44 int nFieldFlags = pWidget->GetFieldFlags();
45 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
tsepez4cf55152016-11-02 14:37:54 -070046 return false;
jaepark8c541822016-08-30 13:43:05 -070047
Ryan Harrison9baf31f2018-01-12 18:36:30 +000048 if (pWidget->GetFieldType() == FormFieldType::kPushButton)
tsepez4cf55152016-11-02 14:37:54 -070049 return true;
jaepark8c541822016-08-30 13:43:05 -070050
51 CPDF_Page* pPage = pWidget->GetPDFPage();
Lei Zhang8f637632018-05-07 23:36:26 +000052 uint32_t dwPermissions = pPage->GetDocument()->GetUserPermissions();
jaepark8c541822016-08-30 13:43:05 -070053 return (dwPermissions & FPDFPERM_FILL_FORM) ||
54 (dwPermissions & FPDFPERM_ANNOT_FORM);
55}
56
57CPDFSDK_Annot* CPDFSDK_WidgetHandler::NewAnnot(CPDF_Annot* pAnnot,
58 CPDFSDK_PageView* pPage) {
Lei Zhang073ecf42018-10-11 16:56:00 +000059 CPDFSDK_InteractiveForm* pForm = m_pFormFillEnv->GetInteractiveForm();
60 CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm();
61 CPDF_FormControl* pCtrl = pPDFForm->GetControlByDict(pAnnot->GetAnnotDict());
jaepark8c541822016-08-30 13:43:05 -070062 if (!pCtrl)
63 return nullptr;
64
Lei Zhang073ecf42018-10-11 16:56:00 +000065 CPDFSDK_Widget* pWidget = new CPDFSDK_Widget(pAnnot, pPage, pForm);
66 pForm->AddMap(pCtrl, pWidget);
67 if (pPDFForm->NeedConstructAP())
Lei Zhang4f261ff2018-10-10 18:44:45 +000068 pWidget->ResetAppearance(pdfium::nullopt, false);
jaepark8c541822016-08-30 13:43:05 -070069 return pWidget;
70}
71
72#ifdef PDF_ENABLE_XFA
73CPDFSDK_Annot* CPDFSDK_WidgetHandler::NewAnnot(CXFA_FFWidget* hWidget,
74 CPDFSDK_PageView* pPage) {
75 return nullptr;
76}
77#endif // PDF_ENABLE_XFA
78
79void CPDFSDK_WidgetHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
80 ASSERT(pAnnot);
Lei Zhang7db136a2018-10-10 21:34:17 +000081 m_pFormFiller->OnDelete(pAnnot);
jaepark8c541822016-08-30 13:43:05 -070082
Tom Sepez4ef943b2018-07-26 19:06:06 +000083 std::unique_ptr<CPDFSDK_Widget> pWidget(ToCPDFSDKWidget(pAnnot));
Lei Zhang073ecf42018-10-11 16:56:00 +000084 CPDFSDK_InteractiveForm* pForm = pWidget->GetInteractiveForm();
jaepark8c541822016-08-30 13:43:05 -070085 CPDF_FormControl* pControl = pWidget->GetFormControl();
Lei Zhang073ecf42018-10-11 16:56:00 +000086 pForm->RemoveMap(pControl);
jaepark8c541822016-08-30 13:43:05 -070087}
88
jaepark8c541822016-08-30 13:43:05 -070089void CPDFSDK_WidgetHandler::OnDraw(CPDFSDK_PageView* pPageView,
90 CPDFSDK_Annot* pAnnot,
91 CFX_RenderDevice* pDevice,
Lei Zhang09ee0872018-10-09 19:13:55 +000092 const CFX_Matrix& mtUser2Device,
jaepark75f84a52016-09-09 15:39:09 -070093 bool bDrawAnnots) {
jaepark8c541822016-08-30 13:43:05 -070094 if (pAnnot->IsSignatureWidget()) {
Lei Zhang09ee0872018-10-09 19:13:55 +000095 pAnnot->AsBAAnnot()->DrawAppearance(pDevice, mtUser2Device,
Tom Sepez522d77d2018-07-28 00:00:25 +000096 CPDF_Annot::Normal, nullptr);
jaepark8c541822016-08-30 13:43:05 -070097 } else {
Lei Zhang7db136a2018-10-10 21:34:17 +000098 m_pFormFiller->OnDraw(pPageView, pAnnot, pDevice, mtUser2Device);
jaepark8c541822016-08-30 13:43:05 -070099 }
100}
101
jaepark8c541822016-08-30 13:43:05 -0700102void CPDFSDK_WidgetHandler::OnMouseEnter(CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700103 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark8c541822016-08-30 13:43:05 -0700104 uint32_t nFlag) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000105 if (!(*pAnnot)->IsSignatureWidget())
jaepark8c541822016-08-30 13:43:05 -0700106 m_pFormFiller->OnMouseEnter(pPageView, pAnnot, nFlag);
107}
108
109void CPDFSDK_WidgetHandler::OnMouseExit(CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700110 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark8c541822016-08-30 13:43:05 -0700111 uint32_t nFlag) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000112 if (!(*pAnnot)->IsSignatureWidget())
jaepark8c541822016-08-30 13:43:05 -0700113 m_pFormFiller->OnMouseExit(pPageView, pAnnot, nFlag);
114}
115
tsepez4cf55152016-11-02 14:37:54 -0700116bool CPDFSDK_WidgetHandler::OnLButtonDown(CPDFSDK_PageView* pPageView,
117 CPDFSDK_Annot::ObservedPtr* pAnnot,
118 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500119 const CFX_PointF& point) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000120 return !(*pAnnot)->IsSignatureWidget() &&
121 m_pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
jaepark8c541822016-08-30 13:43:05 -0700122}
123
tsepez4cf55152016-11-02 14:37:54 -0700124bool CPDFSDK_WidgetHandler::OnLButtonUp(CPDFSDK_PageView* pPageView,
125 CPDFSDK_Annot::ObservedPtr* pAnnot,
126 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500127 const CFX_PointF& point) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000128 return !(*pAnnot)->IsSignatureWidget() &&
129 m_pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
jaepark8c541822016-08-30 13:43:05 -0700130}
131
tsepez4cf55152016-11-02 14:37:54 -0700132bool CPDFSDK_WidgetHandler::OnLButtonDblClk(CPDFSDK_PageView* pPageView,
133 CPDFSDK_Annot::ObservedPtr* pAnnot,
134 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500135 const CFX_PointF& point) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000136 return !(*pAnnot)->IsSignatureWidget() &&
137 m_pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
jaepark8c541822016-08-30 13:43:05 -0700138}
139
tsepez4cf55152016-11-02 14:37:54 -0700140bool CPDFSDK_WidgetHandler::OnMouseMove(CPDFSDK_PageView* pPageView,
141 CPDFSDK_Annot::ObservedPtr* pAnnot,
142 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500143 const CFX_PointF& point) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000144 return !(*pAnnot)->IsSignatureWidget() &&
145 m_pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
jaepark8c541822016-08-30 13:43:05 -0700146}
147
tsepez4cf55152016-11-02 14:37:54 -0700148bool CPDFSDK_WidgetHandler::OnMouseWheel(CPDFSDK_PageView* pPageView,
149 CPDFSDK_Annot::ObservedPtr* pAnnot,
150 uint32_t nFlags,
151 short zDelta,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500152 const CFX_PointF& point) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000153 return !(*pAnnot)->IsSignatureWidget() &&
154 m_pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta, point);
jaepark8c541822016-08-30 13:43:05 -0700155}
156
tsepez4cf55152016-11-02 14:37:54 -0700157bool CPDFSDK_WidgetHandler::OnRButtonDown(CPDFSDK_PageView* pPageView,
158 CPDFSDK_Annot::ObservedPtr* pAnnot,
159 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500160 const CFX_PointF& point) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000161 return !(*pAnnot)->IsSignatureWidget() &&
162 m_pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
jaepark8c541822016-08-30 13:43:05 -0700163}
164
tsepez4cf55152016-11-02 14:37:54 -0700165bool CPDFSDK_WidgetHandler::OnRButtonUp(CPDFSDK_PageView* pPageView,
166 CPDFSDK_Annot::ObservedPtr* pAnnot,
167 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500168 const CFX_PointF& point) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000169 return !(*pAnnot)->IsSignatureWidget() &&
170 m_pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
jaepark8c541822016-08-30 13:43:05 -0700171}
172
tsepez4cf55152016-11-02 14:37:54 -0700173bool CPDFSDK_WidgetHandler::OnRButtonDblClk(CPDFSDK_PageView* pPageView,
174 CPDFSDK_Annot::ObservedPtr* pAnnot,
175 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500176 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700177 return false;
jaepark8c541822016-08-30 13:43:05 -0700178}
179
tsepez4cf55152016-11-02 14:37:54 -0700180bool CPDFSDK_WidgetHandler::OnChar(CPDFSDK_Annot* pAnnot,
181 uint32_t nChar,
182 uint32_t nFlags) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000183 return !pAnnot->IsSignatureWidget() &&
184 m_pFormFiller->OnChar(pAnnot, nChar, nFlags);
jaepark8c541822016-08-30 13:43:05 -0700185}
186
tsepez4cf55152016-11-02 14:37:54 -0700187bool CPDFSDK_WidgetHandler::OnKeyDown(CPDFSDK_Annot* pAnnot,
188 int nKeyCode,
189 int nFlag) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000190 return !pAnnot->IsSignatureWidget() &&
191 m_pFormFiller->OnKeyDown(pAnnot, nKeyCode, nFlag);
jaepark8c541822016-08-30 13:43:05 -0700192}
193
tsepez4cf55152016-11-02 14:37:54 -0700194bool CPDFSDK_WidgetHandler::OnKeyUp(CPDFSDK_Annot* pAnnot,
195 int nKeyCode,
196 int nFlag) {
197 return false;
jaepark8c541822016-08-30 13:43:05 -0700198}
199
jaepark8c541822016-08-30 13:43:05 -0700200void CPDFSDK_WidgetHandler::OnLoad(CPDFSDK_Annot* pAnnot) {
201 if (pAnnot->IsSignatureWidget())
202 return;
203
Tom Sepez4ef943b2018-07-26 19:06:06 +0000204 CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
jaepark8c541822016-08-30 13:43:05 -0700205 if (!pWidget->IsAppearanceValid())
Lei Zhang4f261ff2018-10-10 18:44:45 +0000206 pWidget->ResetAppearance(pdfium::nullopt, false);
jaepark8c541822016-08-30 13:43:05 -0700207
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000208 FormFieldType fieldType = pWidget->GetFieldType();
209 if (fieldType == FormFieldType::kTextField ||
210 fieldType == FormFieldType::kComboBox) {
Lei Zhangcd8ff7e2017-06-13 14:09:46 -0700211 CPDFSDK_Annot::ObservedPtr pObserved(pWidget);
Lei Zhang4f261ff2018-10-10 18:44:45 +0000212 Optional<WideString> sValue = pWidget->OnFormat();
Lei Zhangcd8ff7e2017-06-13 14:09:46 -0700213 if (!pObserved)
214 return;
215
Lei Zhang4f261ff2018-10-10 18:44:45 +0000216 if (sValue.has_value() && fieldType == FormFieldType::kComboBox)
217 pWidget->ResetAppearance(sValue, false);
jaepark8c541822016-08-30 13:43:05 -0700218 }
219
220#ifdef PDF_ENABLE_XFA
221 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
dsinclair521b7502016-11-02 13:02:28 -0700222 CPDFXFA_Context* pContext = pPageView->GetFormFillEnv()->GetXFAContext();
Ryan Harrison854d71c2017-10-18 12:28:14 -0400223 if (pContext->GetFormType() == FormType::kXFAForeground) {
jaepark8c541822016-08-30 13:43:05 -0700224 if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700225 pWidget->ResetAppearance(false);
jaepark8c541822016-08-30 13:43:05 -0700226 }
227#endif // PDF_ENABLE_XFA
jaepark8c541822016-08-30 13:43:05 -0700228}
229
tsepez4cf55152016-11-02 14:37:54 -0700230bool CPDFSDK_WidgetHandler::OnSetFocus(CPDFSDK_Annot::ObservedPtr* pAnnot,
231 uint32_t nFlag) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000232 return (*pAnnot)->IsSignatureWidget() ||
233 m_pFormFiller->OnSetFocus(pAnnot, nFlag);
jaepark8c541822016-08-30 13:43:05 -0700234}
235
tsepez4cf55152016-11-02 14:37:54 -0700236bool CPDFSDK_WidgetHandler::OnKillFocus(CPDFSDK_Annot::ObservedPtr* pAnnot,
237 uint32_t nFlag) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000238 return (*pAnnot)->IsSignatureWidget() ||
239 m_pFormFiller->OnKillFocus(pAnnot, nFlag);
jaepark8c541822016-08-30 13:43:05 -0700240}
241
242#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700243bool CPDFSDK_WidgetHandler::OnXFAChangedFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700244 CPDFSDK_Annot::ObservedPtr* pOldAnnot,
245 CPDFSDK_Annot::ObservedPtr* pNewAnnot) {
tsepez4cf55152016-11-02 14:37:54 -0700246 return true;
jaepark8c541822016-08-30 13:43:05 -0700247}
248#endif // PDF_ENABLE_XFA
249
250CFX_FloatRect CPDFSDK_WidgetHandler::GetViewBBox(CPDFSDK_PageView* pPageView,
251 CPDFSDK_Annot* pAnnot) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000252 if (!pAnnot->IsSignatureWidget())
jaepark8c541822016-08-30 13:43:05 -0700253 return CFX_FloatRect(m_pFormFiller->GetViewBBox(pPageView, pAnnot));
Lei Zhangd24236a2017-06-29 18:28:58 -0700254 return CFX_FloatRect();
jaepark8c541822016-08-30 13:43:05 -0700255}
256
Lei Zhanga4c7ac42018-04-17 15:12:58 +0000257WideString CPDFSDK_WidgetHandler::GetText(CPDFSDK_Annot* pAnnot) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000258 if (!pAnnot->IsSignatureWidget())
Lei Zhanga4c7ac42018-04-17 15:12:58 +0000259 return m_pFormFiller->GetText(pAnnot);
260 return WideString();
261}
262
Ryan Harrison275e2602017-09-18 14:23:18 -0400263WideString CPDFSDK_WidgetHandler::GetSelectedText(CPDFSDK_Annot* pAnnot) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000264 if (!pAnnot->IsSignatureWidget())
Diana Gagedce2d722017-06-20 11:17:11 -0700265 return m_pFormFiller->GetSelectedText(pAnnot);
Ryan Harrison275e2602017-09-18 14:23:18 -0400266 return WideString();
Diana Gagedce2d722017-06-20 11:17:11 -0700267}
268
Diana Gageab390972017-07-28 17:07:39 -0700269void CPDFSDK_WidgetHandler::ReplaceSelection(CPDFSDK_Annot* pAnnot,
Ryan Harrison275e2602017-09-18 14:23:18 -0400270 const WideString& text) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000271 if (!pAnnot->IsSignatureWidget())
Diana Gageab390972017-07-28 17:07:39 -0700272 m_pFormFiller->ReplaceSelection(pAnnot, text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700273}
274
Lei Zhangee967722018-04-19 20:55:54 +0000275bool CPDFSDK_WidgetHandler::CanUndo(CPDFSDK_Annot* pAnnot) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000276 return !pAnnot->IsSignatureWidget() && m_pFormFiller->CanUndo(pAnnot);
Lei Zhangee967722018-04-19 20:55:54 +0000277}
278
279bool CPDFSDK_WidgetHandler::CanRedo(CPDFSDK_Annot* pAnnot) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000280 return !pAnnot->IsSignatureWidget() && m_pFormFiller->CanRedo(pAnnot);
Lei Zhangee967722018-04-19 20:55:54 +0000281}
282
283bool CPDFSDK_WidgetHandler::Undo(CPDFSDK_Annot* pAnnot) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000284 return !pAnnot->IsSignatureWidget() && m_pFormFiller->Undo(pAnnot);
Lei Zhangee967722018-04-19 20:55:54 +0000285}
286
287bool CPDFSDK_WidgetHandler::Redo(CPDFSDK_Annot* pAnnot) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000288 return !pAnnot->IsSignatureWidget() && m_pFormFiller->Redo(pAnnot);
Lei Zhangee967722018-04-19 20:55:54 +0000289}
290
tsepez4cf55152016-11-02 14:37:54 -0700291bool CPDFSDK_WidgetHandler::HitTest(CPDFSDK_PageView* pPageView,
292 CPDFSDK_Annot* pAnnot,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500293 const CFX_PointF& point) {
jaepark8c541822016-08-30 13:43:05 -0700294 ASSERT(pPageView);
295 ASSERT(pAnnot);
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500296 return GetViewBBox(pPageView, pAnnot).Contains(point);
jaepark8c541822016-08-30 13:43:05 -0700297}