blob: 3eb8e5d3648937af8a816a85fdbdc7f5b9bbbf55 [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"
dsinclair1727aee2016-09-29 13:12:56 -070014#include "core/fpdfdoc/cpdf_interform.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"
dsinclair114e46a2016-09-29 17:18:21 -070017#include "fpdfsdk/cpdfsdk_interform.h"
18#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),
29 m_pFormFiller(pFormFillEnv->GetInteractiveFormFiller()) {}
jaepark8c541822016-08-30 13:43:05 -070030
31CPDFSDK_WidgetHandler::~CPDFSDK_WidgetHandler() {}
32
tsepez4cf55152016-11-02 14:37:54 -070033bool CPDFSDK_WidgetHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
jaepark956553e2016-08-31 06:49:27 -070034 ASSERT(pAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET);
jaepark8c541822016-08-30 13:43:05 -070035 if (pAnnot->IsSignatureWidget())
tsepez4cf55152016-11-02 14:37:54 -070036 return false;
jaepark8c541822016-08-30 13:43:05 -070037
38 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot);
39 if (!pWidget->IsVisible())
tsepez4cf55152016-11-02 14:37:54 -070040 return false;
jaepark8c541822016-08-30 13:43:05 -070041
42 int nFieldFlags = pWidget->GetFieldFlags();
43 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
tsepez4cf55152016-11-02 14:37:54 -070044 return false;
jaepark8c541822016-08-30 13:43:05 -070045
46 if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
tsepez4cf55152016-11-02 14:37:54 -070047 return true;
jaepark8c541822016-08-30 13:43:05 -070048
49 CPDF_Page* pPage = pWidget->GetPDFPage();
Tom Sepez4cb82ee2017-05-22 15:15:30 -070050 uint32_t dwPermissions = pPage->m_pDocument->GetUserPermissions();
jaepark8c541822016-08-30 13:43:05 -070051 return (dwPermissions & FPDFPERM_FILL_FORM) ||
52 (dwPermissions & FPDFPERM_ANNOT_FORM);
53}
54
55CPDFSDK_Annot* CPDFSDK_WidgetHandler::NewAnnot(CPDF_Annot* pAnnot,
56 CPDFSDK_PageView* pPage) {
dsinclair8779fa82016-10-12 12:05:44 -070057 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
jaepark8c541822016-08-30 13:43:05 -070058 CPDF_FormControl* pCtrl = CPDFSDK_Widget::GetFormControl(
59 pInterForm->GetInterForm(), pAnnot->GetAnnotDict());
60 if (!pCtrl)
61 return nullptr;
62
63 CPDFSDK_Widget* pWidget = new CPDFSDK_Widget(pAnnot, pPage, pInterForm);
64 pInterForm->AddMap(pCtrl, pWidget);
65 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
66 if (pPDFInterForm && pPDFInterForm->NeedConstructAP())
tsepez4cf55152016-11-02 14:37:54 -070067 pWidget->ResetAppearance(nullptr, false);
jaepark8c541822016-08-30 13:43:05 -070068
69 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);
81
82 if (m_pFormFiller)
83 m_pFormFiller->OnDelete(pAnnot);
84
85 std::unique_ptr<CPDFSDK_Widget> pWidget(static_cast<CPDFSDK_Widget*>(pAnnot));
86 CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
87 CPDF_FormControl* pControl = pWidget->GetFormControl();
88 pInterForm->RemoveMap(pControl);
89}
90
jaepark8c541822016-08-30 13:43:05 -070091void CPDFSDK_WidgetHandler::OnDraw(CPDFSDK_PageView* pPageView,
92 CPDFSDK_Annot* pAnnot,
93 CFX_RenderDevice* pDevice,
jaepark75f84a52016-09-09 15:39:09 -070094 CFX_Matrix* pUser2Device,
95 bool bDrawAnnots) {
jaepark8c541822016-08-30 13:43:05 -070096 if (pAnnot->IsSignatureWidget()) {
97 static_cast<CPDFSDK_BAAnnot*>(pAnnot)->DrawAppearance(
Lei Zhang8a449402017-08-17 15:07:47 -070098 pDevice, *pUser2Device, CPDF_Annot::Normal, nullptr);
jaepark8c541822016-08-30 13:43:05 -070099 } else {
100 if (m_pFormFiller)
jaepark4bae2962016-09-01 14:39:24 -0700101 m_pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device);
jaepark8c541822016-08-30 13:43:05 -0700102 }
103}
104
jaepark8c541822016-08-30 13:43:05 -0700105void CPDFSDK_WidgetHandler::OnMouseEnter(CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700106 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark8c541822016-08-30 13:43:05 -0700107 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700108 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700109 m_pFormFiller->OnMouseEnter(pPageView, pAnnot, nFlag);
110}
111
112void CPDFSDK_WidgetHandler::OnMouseExit(CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700113 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark8c541822016-08-30 13:43:05 -0700114 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700115 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700116 m_pFormFiller->OnMouseExit(pPageView, pAnnot, nFlag);
117}
118
tsepez4cf55152016-11-02 14:37:54 -0700119bool CPDFSDK_WidgetHandler::OnLButtonDown(CPDFSDK_PageView* pPageView,
120 CPDFSDK_Annot::ObservedPtr* pAnnot,
121 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500122 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700123 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700124 return m_pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
125
tsepez4cf55152016-11-02 14:37:54 -0700126 return false;
jaepark8c541822016-08-30 13:43:05 -0700127}
128
tsepez4cf55152016-11-02 14:37:54 -0700129bool CPDFSDK_WidgetHandler::OnLButtonUp(CPDFSDK_PageView* pPageView,
130 CPDFSDK_Annot::ObservedPtr* pAnnot,
131 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500132 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700133 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700134 return m_pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
135
tsepez4cf55152016-11-02 14:37:54 -0700136 return false;
jaepark8c541822016-08-30 13:43:05 -0700137}
138
tsepez4cf55152016-11-02 14:37:54 -0700139bool CPDFSDK_WidgetHandler::OnLButtonDblClk(CPDFSDK_PageView* pPageView,
140 CPDFSDK_Annot::ObservedPtr* pAnnot,
141 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500142 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700143 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700144 return m_pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
145
tsepez4cf55152016-11-02 14:37:54 -0700146 return false;
jaepark8c541822016-08-30 13:43:05 -0700147}
148
tsepez4cf55152016-11-02 14:37:54 -0700149bool CPDFSDK_WidgetHandler::OnMouseMove(CPDFSDK_PageView* pPageView,
150 CPDFSDK_Annot::ObservedPtr* pAnnot,
151 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500152 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700153 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700154 return m_pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
155
tsepez4cf55152016-11-02 14:37:54 -0700156 return false;
jaepark8c541822016-08-30 13:43:05 -0700157}
158
tsepez4cf55152016-11-02 14:37:54 -0700159bool CPDFSDK_WidgetHandler::OnMouseWheel(CPDFSDK_PageView* pPageView,
160 CPDFSDK_Annot::ObservedPtr* pAnnot,
161 uint32_t nFlags,
162 short zDelta,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500163 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700164 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700165 return m_pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,
166 point);
167
tsepez4cf55152016-11-02 14:37:54 -0700168 return false;
jaepark8c541822016-08-30 13:43:05 -0700169}
170
tsepez4cf55152016-11-02 14:37:54 -0700171bool CPDFSDK_WidgetHandler::OnRButtonDown(CPDFSDK_PageView* pPageView,
172 CPDFSDK_Annot::ObservedPtr* pAnnot,
173 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500174 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700175 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700176 return m_pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
177
tsepez4cf55152016-11-02 14:37:54 -0700178 return false;
jaepark8c541822016-08-30 13:43:05 -0700179}
180
tsepez4cf55152016-11-02 14:37:54 -0700181bool CPDFSDK_WidgetHandler::OnRButtonUp(CPDFSDK_PageView* pPageView,
182 CPDFSDK_Annot::ObservedPtr* pAnnot,
183 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500184 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700185 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700186 return m_pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
187
tsepez4cf55152016-11-02 14:37:54 -0700188 return false;
jaepark8c541822016-08-30 13:43:05 -0700189}
190
tsepez4cf55152016-11-02 14:37:54 -0700191bool CPDFSDK_WidgetHandler::OnRButtonDblClk(CPDFSDK_PageView* pPageView,
192 CPDFSDK_Annot::ObservedPtr* pAnnot,
193 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500194 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700195 return false;
jaepark8c541822016-08-30 13:43:05 -0700196}
197
tsepez4cf55152016-11-02 14:37:54 -0700198bool CPDFSDK_WidgetHandler::OnChar(CPDFSDK_Annot* pAnnot,
199 uint32_t nChar,
200 uint32_t nFlags) {
jaepark8c541822016-08-30 13:43:05 -0700201 if (!pAnnot->IsSignatureWidget() && m_pFormFiller)
202 return m_pFormFiller->OnChar(pAnnot, nChar, nFlags);
203
tsepez4cf55152016-11-02 14:37:54 -0700204 return false;
jaepark8c541822016-08-30 13:43:05 -0700205}
206
tsepez4cf55152016-11-02 14:37:54 -0700207bool CPDFSDK_WidgetHandler::OnKeyDown(CPDFSDK_Annot* pAnnot,
208 int nKeyCode,
209 int nFlag) {
jaepark8c541822016-08-30 13:43:05 -0700210 if (!pAnnot->IsSignatureWidget() && m_pFormFiller)
211 return m_pFormFiller->OnKeyDown(pAnnot, nKeyCode, nFlag);
212
tsepez4cf55152016-11-02 14:37:54 -0700213 return false;
jaepark8c541822016-08-30 13:43:05 -0700214}
215
tsepez4cf55152016-11-02 14:37:54 -0700216bool CPDFSDK_WidgetHandler::OnKeyUp(CPDFSDK_Annot* pAnnot,
217 int nKeyCode,
218 int nFlag) {
219 return false;
jaepark8c541822016-08-30 13:43:05 -0700220}
221
jaepark8c541822016-08-30 13:43:05 -0700222void CPDFSDK_WidgetHandler::OnLoad(CPDFSDK_Annot* pAnnot) {
223 if (pAnnot->IsSignatureWidget())
224 return;
225
226 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot);
227 if (!pWidget->IsAppearanceValid())
tsepez4cf55152016-11-02 14:37:54 -0700228 pWidget->ResetAppearance(nullptr, false);
jaepark8c541822016-08-30 13:43:05 -0700229
230 int nFieldType = pWidget->GetFieldType();
231 if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX) {
tsepez4cf55152016-11-02 14:37:54 -0700232 bool bFormatted = false;
Lei Zhangcd8ff7e2017-06-13 14:09:46 -0700233 CPDFSDK_Annot::ObservedPtr pObserved(pWidget);
Ryan Harrison275e2602017-09-18 14:23:18 -0400234 WideString sValue = pWidget->OnFormat(bFormatted);
Lei Zhangcd8ff7e2017-06-13 14:09:46 -0700235 if (!pObserved)
236 return;
237
tsepez8c2a8cd2016-09-07 15:29:11 -0700238 if (bFormatted && nFieldType == FIELDTYPE_COMBOBOX)
tsepez4cf55152016-11-02 14:37:54 -0700239 pWidget->ResetAppearance(&sValue, false);
jaepark8c541822016-08-30 13:43:05 -0700240 }
241
242#ifdef PDF_ENABLE_XFA
243 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
dsinclair521b7502016-11-02 13:02:28 -0700244 CPDFXFA_Context* pContext = pPageView->GetFormFillEnv()->GetXFAContext();
Ryan Harrison854d71c2017-10-18 12:28:14 -0400245 if (pContext->GetFormType() == FormType::kXFAForeground) {
jaepark8c541822016-08-30 13:43:05 -0700246 if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700247 pWidget->ResetAppearance(false);
jaepark8c541822016-08-30 13:43:05 -0700248 }
249#endif // PDF_ENABLE_XFA
jaepark8c541822016-08-30 13:43:05 -0700250}
251
tsepez4cf55152016-11-02 14:37:54 -0700252bool CPDFSDK_WidgetHandler::OnSetFocus(CPDFSDK_Annot::ObservedPtr* pAnnot,
253 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700254 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700255 return m_pFormFiller->OnSetFocus(pAnnot, nFlag);
256
tsepez4cf55152016-11-02 14:37:54 -0700257 return true;
jaepark8c541822016-08-30 13:43:05 -0700258}
259
tsepez4cf55152016-11-02 14:37:54 -0700260bool CPDFSDK_WidgetHandler::OnKillFocus(CPDFSDK_Annot::ObservedPtr* pAnnot,
261 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700262 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700263 return m_pFormFiller->OnKillFocus(pAnnot, nFlag);
264
tsepez4cf55152016-11-02 14:37:54 -0700265 return true;
jaepark8c541822016-08-30 13:43:05 -0700266}
267
268#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700269bool CPDFSDK_WidgetHandler::OnXFAChangedFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700270 CPDFSDK_Annot::ObservedPtr* pOldAnnot,
271 CPDFSDK_Annot::ObservedPtr* pNewAnnot) {
tsepez4cf55152016-11-02 14:37:54 -0700272 return true;
jaepark8c541822016-08-30 13:43:05 -0700273}
274#endif // PDF_ENABLE_XFA
275
276CFX_FloatRect CPDFSDK_WidgetHandler::GetViewBBox(CPDFSDK_PageView* pPageView,
277 CPDFSDK_Annot* pAnnot) {
278 if (!pAnnot->IsSignatureWidget() && m_pFormFiller)
279 return CFX_FloatRect(m_pFormFiller->GetViewBBox(pPageView, pAnnot));
Lei Zhangd24236a2017-06-29 18:28:58 -0700280 return CFX_FloatRect();
jaepark8c541822016-08-30 13:43:05 -0700281}
282
Ryan Harrison275e2602017-09-18 14:23:18 -0400283WideString CPDFSDK_WidgetHandler::GetSelectedText(CPDFSDK_Annot* pAnnot) {
Diana Gagedce2d722017-06-20 11:17:11 -0700284 if (!pAnnot->IsSignatureWidget() && m_pFormFiller)
285 return m_pFormFiller->GetSelectedText(pAnnot);
286
Ryan Harrison275e2602017-09-18 14:23:18 -0400287 return WideString();
Diana Gagedce2d722017-06-20 11:17:11 -0700288}
289
Diana Gageab390972017-07-28 17:07:39 -0700290void CPDFSDK_WidgetHandler::ReplaceSelection(CPDFSDK_Annot* pAnnot,
Ryan Harrison275e2602017-09-18 14:23:18 -0400291 const WideString& text) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700292 if (!pAnnot->IsSignatureWidget() && m_pFormFiller)
Diana Gageab390972017-07-28 17:07:39 -0700293 m_pFormFiller->ReplaceSelection(pAnnot, text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700294}
295
tsepez4cf55152016-11-02 14:37:54 -0700296bool CPDFSDK_WidgetHandler::HitTest(CPDFSDK_PageView* pPageView,
297 CPDFSDK_Annot* pAnnot,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500298 const CFX_PointF& point) {
jaepark8c541822016-08-30 13:43:05 -0700299 ASSERT(pPageView);
300 ASSERT(pAnnot);
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500301 return GetViewBBox(pPageView, pAnnot).Contains(point);
jaepark8c541822016-08-30 13:43:05 -0700302}