blob: cab210db428ef7022a355f6823243362cc1b00f8 [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)
28 : m_pFormFillEnv(pFormFillEnv), m_pFormFiller(nullptr) {}
jaepark8c541822016-08-30 13:43:05 -070029
30CPDFSDK_WidgetHandler::~CPDFSDK_WidgetHandler() {}
31
tsepez4cf55152016-11-02 14:37:54 -070032bool CPDFSDK_WidgetHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
jaepark956553e2016-08-31 06:49:27 -070033 ASSERT(pAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET);
jaepark8c541822016-08-30 13:43:05 -070034 if (pAnnot->IsSignatureWidget())
tsepez4cf55152016-11-02 14:37:54 -070035 return false;
jaepark8c541822016-08-30 13:43:05 -070036
37 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot);
38 if (!pWidget->IsVisible())
tsepez4cf55152016-11-02 14:37:54 -070039 return false;
jaepark8c541822016-08-30 13:43:05 -070040
41 int nFieldFlags = pWidget->GetFieldFlags();
42 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
tsepez4cf55152016-11-02 14:37:54 -070043 return false;
jaepark8c541822016-08-30 13:43:05 -070044
45 if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
tsepez4cf55152016-11-02 14:37:54 -070046 return true;
jaepark8c541822016-08-30 13:43:05 -070047
48 CPDF_Page* pPage = pWidget->GetPDFPage();
49 CPDF_Document* pDocument = pPage->m_pDocument;
50 uint32_t dwPermissions = pDocument->GetUserPermissions();
51 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(
98 pDevice, pUser2Device, CPDF_Annot::Normal, nullptr);
99 } 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;
tsepez8c2a8cd2016-09-07 15:29:11 -0700233 CFX_WideString sValue = pWidget->OnFormat(bFormatted);
234 if (bFormatted && nFieldType == FIELDTYPE_COMBOBOX)
tsepez4cf55152016-11-02 14:37:54 -0700235 pWidget->ResetAppearance(&sValue, false);
jaepark8c541822016-08-30 13:43:05 -0700236 }
237
238#ifdef PDF_ENABLE_XFA
239 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
dsinclair521b7502016-11-02 13:02:28 -0700240 CPDFXFA_Context* pContext = pPageView->GetFormFillEnv()->GetXFAContext();
Dan Sinclaircdba7472017-03-23 09:17:10 -0400241 if (pContext->GetDocType() == XFA_DocType::Static) {
jaepark8c541822016-08-30 13:43:05 -0700242 if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700243 pWidget->ResetAppearance(false);
jaepark8c541822016-08-30 13:43:05 -0700244 }
245#endif // PDF_ENABLE_XFA
jaepark8c541822016-08-30 13:43:05 -0700246}
247
tsepez4cf55152016-11-02 14:37:54 -0700248bool CPDFSDK_WidgetHandler::OnSetFocus(CPDFSDK_Annot::ObservedPtr* pAnnot,
249 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700250 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700251 return m_pFormFiller->OnSetFocus(pAnnot, nFlag);
252
tsepez4cf55152016-11-02 14:37:54 -0700253 return true;
jaepark8c541822016-08-30 13:43:05 -0700254}
255
tsepez4cf55152016-11-02 14:37:54 -0700256bool CPDFSDK_WidgetHandler::OnKillFocus(CPDFSDK_Annot::ObservedPtr* pAnnot,
257 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700258 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700259 return m_pFormFiller->OnKillFocus(pAnnot, nFlag);
260
tsepez4cf55152016-11-02 14:37:54 -0700261 return true;
jaepark8c541822016-08-30 13:43:05 -0700262}
263
264#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700265bool CPDFSDK_WidgetHandler::OnXFAChangedFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700266 CPDFSDK_Annot::ObservedPtr* pOldAnnot,
267 CPDFSDK_Annot::ObservedPtr* pNewAnnot) {
tsepez4cf55152016-11-02 14:37:54 -0700268 return true;
jaepark8c541822016-08-30 13:43:05 -0700269}
270#endif // PDF_ENABLE_XFA
271
272CFX_FloatRect CPDFSDK_WidgetHandler::GetViewBBox(CPDFSDK_PageView* pPageView,
273 CPDFSDK_Annot* pAnnot) {
274 if (!pAnnot->IsSignatureWidget() && m_pFormFiller)
275 return CFX_FloatRect(m_pFormFiller->GetViewBBox(pPageView, pAnnot));
276
277 return CFX_FloatRect(0, 0, 0, 0);
278}
279
tsepez4cf55152016-11-02 14:37:54 -0700280bool CPDFSDK_WidgetHandler::HitTest(CPDFSDK_PageView* pPageView,
281 CPDFSDK_Annot* pAnnot,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500282 const CFX_PointF& point) {
jaepark8c541822016-08-30 13:43:05 -0700283 ASSERT(pPageView);
284 ASSERT(pAnnot);
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500285 return GetViewBBox(pPageView, pAnnot).Contains(point);
jaepark8c541822016-08-30 13:43:05 -0700286}