blob: 1e63f5ecc6565546f14faeda6567e05be10dae6c [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();
Tom Sepez4cb82ee2017-05-22 15:15:30 -070049 uint32_t dwPermissions = pPage->m_pDocument->GetUserPermissions();
jaepark8c541822016-08-30 13:43:05 -070050 return (dwPermissions & FPDFPERM_FILL_FORM) ||
51 (dwPermissions & FPDFPERM_ANNOT_FORM);
52}
53
54CPDFSDK_Annot* CPDFSDK_WidgetHandler::NewAnnot(CPDF_Annot* pAnnot,
55 CPDFSDK_PageView* pPage) {
dsinclair8779fa82016-10-12 12:05:44 -070056 CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
jaepark8c541822016-08-30 13:43:05 -070057 CPDF_FormControl* pCtrl = CPDFSDK_Widget::GetFormControl(
58 pInterForm->GetInterForm(), pAnnot->GetAnnotDict());
59 if (!pCtrl)
60 return nullptr;
61
62 CPDFSDK_Widget* pWidget = new CPDFSDK_Widget(pAnnot, pPage, pInterForm);
63 pInterForm->AddMap(pCtrl, pWidget);
64 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
65 if (pPDFInterForm && pPDFInterForm->NeedConstructAP())
tsepez4cf55152016-11-02 14:37:54 -070066 pWidget->ResetAppearance(nullptr, false);
jaepark8c541822016-08-30 13:43:05 -070067
68 return pWidget;
69}
70
71#ifdef PDF_ENABLE_XFA
72CPDFSDK_Annot* CPDFSDK_WidgetHandler::NewAnnot(CXFA_FFWidget* hWidget,
73 CPDFSDK_PageView* pPage) {
74 return nullptr;
75}
76#endif // PDF_ENABLE_XFA
77
78void CPDFSDK_WidgetHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot) {
79 ASSERT(pAnnot);
80
81 if (m_pFormFiller)
82 m_pFormFiller->OnDelete(pAnnot);
83
84 std::unique_ptr<CPDFSDK_Widget> pWidget(static_cast<CPDFSDK_Widget*>(pAnnot));
85 CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
86 CPDF_FormControl* pControl = pWidget->GetFormControl();
87 pInterForm->RemoveMap(pControl);
88}
89
jaepark8c541822016-08-30 13:43:05 -070090void CPDFSDK_WidgetHandler::OnDraw(CPDFSDK_PageView* pPageView,
91 CPDFSDK_Annot* pAnnot,
92 CFX_RenderDevice* pDevice,
jaepark75f84a52016-09-09 15:39:09 -070093 CFX_Matrix* pUser2Device,
94 bool bDrawAnnots) {
jaepark8c541822016-08-30 13:43:05 -070095 if (pAnnot->IsSignatureWidget()) {
96 static_cast<CPDFSDK_BAAnnot*>(pAnnot)->DrawAppearance(
97 pDevice, pUser2Device, CPDF_Annot::Normal, nullptr);
98 } else {
99 if (m_pFormFiller)
jaepark4bae2962016-09-01 14:39:24 -0700100 m_pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device);
jaepark8c541822016-08-30 13:43:05 -0700101 }
102}
103
jaepark8c541822016-08-30 13:43:05 -0700104void CPDFSDK_WidgetHandler::OnMouseEnter(CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700105 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark8c541822016-08-30 13:43:05 -0700106 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700107 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700108 m_pFormFiller->OnMouseEnter(pPageView, pAnnot, nFlag);
109}
110
111void CPDFSDK_WidgetHandler::OnMouseExit(CPDFSDK_PageView* pPageView,
tsepezf8074ce2016-09-27 14:29:57 -0700112 CPDFSDK_Annot::ObservedPtr* pAnnot,
jaepark8c541822016-08-30 13:43:05 -0700113 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700114 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700115 m_pFormFiller->OnMouseExit(pPageView, pAnnot, nFlag);
116}
117
tsepez4cf55152016-11-02 14:37:54 -0700118bool CPDFSDK_WidgetHandler::OnLButtonDown(CPDFSDK_PageView* pPageView,
119 CPDFSDK_Annot::ObservedPtr* pAnnot,
120 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500121 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700122 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700123 return m_pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
124
tsepez4cf55152016-11-02 14:37:54 -0700125 return false;
jaepark8c541822016-08-30 13:43:05 -0700126}
127
tsepez4cf55152016-11-02 14:37:54 -0700128bool CPDFSDK_WidgetHandler::OnLButtonUp(CPDFSDK_PageView* pPageView,
129 CPDFSDK_Annot::ObservedPtr* pAnnot,
130 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500131 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700132 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700133 return m_pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
134
tsepez4cf55152016-11-02 14:37:54 -0700135 return false;
jaepark8c541822016-08-30 13:43:05 -0700136}
137
tsepez4cf55152016-11-02 14:37:54 -0700138bool CPDFSDK_WidgetHandler::OnLButtonDblClk(CPDFSDK_PageView* pPageView,
139 CPDFSDK_Annot::ObservedPtr* pAnnot,
140 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500141 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700142 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700143 return m_pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
144
tsepez4cf55152016-11-02 14:37:54 -0700145 return false;
jaepark8c541822016-08-30 13:43:05 -0700146}
147
tsepez4cf55152016-11-02 14:37:54 -0700148bool CPDFSDK_WidgetHandler::OnMouseMove(CPDFSDK_PageView* pPageView,
149 CPDFSDK_Annot::ObservedPtr* pAnnot,
150 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500151 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700152 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700153 return m_pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
154
tsepez4cf55152016-11-02 14:37:54 -0700155 return false;
jaepark8c541822016-08-30 13:43:05 -0700156}
157
tsepez4cf55152016-11-02 14:37:54 -0700158bool CPDFSDK_WidgetHandler::OnMouseWheel(CPDFSDK_PageView* pPageView,
159 CPDFSDK_Annot::ObservedPtr* pAnnot,
160 uint32_t nFlags,
161 short zDelta,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500162 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700163 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700164 return m_pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,
165 point);
166
tsepez4cf55152016-11-02 14:37:54 -0700167 return false;
jaepark8c541822016-08-30 13:43:05 -0700168}
169
tsepez4cf55152016-11-02 14:37:54 -0700170bool CPDFSDK_WidgetHandler::OnRButtonDown(CPDFSDK_PageView* pPageView,
171 CPDFSDK_Annot::ObservedPtr* pAnnot,
172 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500173 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700174 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700175 return m_pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
176
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::OnRButtonUp(CPDFSDK_PageView* pPageView,
181 CPDFSDK_Annot::ObservedPtr* pAnnot,
182 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500183 const CFX_PointF& point) {
tsepezf8074ce2016-09-27 14:29:57 -0700184 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700185 return m_pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
186
tsepez4cf55152016-11-02 14:37:54 -0700187 return false;
jaepark8c541822016-08-30 13:43:05 -0700188}
189
tsepez4cf55152016-11-02 14:37:54 -0700190bool CPDFSDK_WidgetHandler::OnRButtonDblClk(CPDFSDK_PageView* pPageView,
191 CPDFSDK_Annot::ObservedPtr* pAnnot,
192 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500193 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700194 return false;
jaepark8c541822016-08-30 13:43:05 -0700195}
196
tsepez4cf55152016-11-02 14:37:54 -0700197bool CPDFSDK_WidgetHandler::OnChar(CPDFSDK_Annot* pAnnot,
198 uint32_t nChar,
199 uint32_t nFlags) {
jaepark8c541822016-08-30 13:43:05 -0700200 if (!pAnnot->IsSignatureWidget() && m_pFormFiller)
201 return m_pFormFiller->OnChar(pAnnot, nChar, nFlags);
202
tsepez4cf55152016-11-02 14:37:54 -0700203 return false;
jaepark8c541822016-08-30 13:43:05 -0700204}
205
tsepez4cf55152016-11-02 14:37:54 -0700206bool CPDFSDK_WidgetHandler::OnKeyDown(CPDFSDK_Annot* pAnnot,
207 int nKeyCode,
208 int nFlag) {
jaepark8c541822016-08-30 13:43:05 -0700209 if (!pAnnot->IsSignatureWidget() && m_pFormFiller)
210 return m_pFormFiller->OnKeyDown(pAnnot, nKeyCode, nFlag);
211
tsepez4cf55152016-11-02 14:37:54 -0700212 return false;
jaepark8c541822016-08-30 13:43:05 -0700213}
214
tsepez4cf55152016-11-02 14:37:54 -0700215bool CPDFSDK_WidgetHandler::OnKeyUp(CPDFSDK_Annot* pAnnot,
216 int nKeyCode,
217 int nFlag) {
218 return false;
jaepark8c541822016-08-30 13:43:05 -0700219}
220
jaepark8c541822016-08-30 13:43:05 -0700221void CPDFSDK_WidgetHandler::OnLoad(CPDFSDK_Annot* pAnnot) {
222 if (pAnnot->IsSignatureWidget())
223 return;
224
225 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot);
226 if (!pWidget->IsAppearanceValid())
tsepez4cf55152016-11-02 14:37:54 -0700227 pWidget->ResetAppearance(nullptr, false);
jaepark8c541822016-08-30 13:43:05 -0700228
229 int nFieldType = pWidget->GetFieldType();
230 if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX) {
tsepez4cf55152016-11-02 14:37:54 -0700231 bool bFormatted = false;
tsepez8c2a8cd2016-09-07 15:29:11 -0700232 CFX_WideString sValue = pWidget->OnFormat(bFormatted);
233 if (bFormatted && nFieldType == FIELDTYPE_COMBOBOX)
tsepez4cf55152016-11-02 14:37:54 -0700234 pWidget->ResetAppearance(&sValue, false);
jaepark8c541822016-08-30 13:43:05 -0700235 }
236
237#ifdef PDF_ENABLE_XFA
238 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
dsinclair521b7502016-11-02 13:02:28 -0700239 CPDFXFA_Context* pContext = pPageView->GetFormFillEnv()->GetXFAContext();
Dan Sinclaircdba7472017-03-23 09:17:10 -0400240 if (pContext->GetDocType() == XFA_DocType::Static) {
jaepark8c541822016-08-30 13:43:05 -0700241 if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700242 pWidget->ResetAppearance(false);
jaepark8c541822016-08-30 13:43:05 -0700243 }
244#endif // PDF_ENABLE_XFA
jaepark8c541822016-08-30 13:43:05 -0700245}
246
tsepez4cf55152016-11-02 14:37:54 -0700247bool CPDFSDK_WidgetHandler::OnSetFocus(CPDFSDK_Annot::ObservedPtr* pAnnot,
248 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700249 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700250 return m_pFormFiller->OnSetFocus(pAnnot, nFlag);
251
tsepez4cf55152016-11-02 14:37:54 -0700252 return true;
jaepark8c541822016-08-30 13:43:05 -0700253}
254
tsepez4cf55152016-11-02 14:37:54 -0700255bool CPDFSDK_WidgetHandler::OnKillFocus(CPDFSDK_Annot::ObservedPtr* pAnnot,
256 uint32_t nFlag) {
tsepezf8074ce2016-09-27 14:29:57 -0700257 if (!(*pAnnot)->IsSignatureWidget() && m_pFormFiller)
jaepark8c541822016-08-30 13:43:05 -0700258 return m_pFormFiller->OnKillFocus(pAnnot, nFlag);
259
tsepez4cf55152016-11-02 14:37:54 -0700260 return true;
jaepark8c541822016-08-30 13:43:05 -0700261}
262
263#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700264bool CPDFSDK_WidgetHandler::OnXFAChangedFocus(
tsepezf8074ce2016-09-27 14:29:57 -0700265 CPDFSDK_Annot::ObservedPtr* pOldAnnot,
266 CPDFSDK_Annot::ObservedPtr* pNewAnnot) {
tsepez4cf55152016-11-02 14:37:54 -0700267 return true;
jaepark8c541822016-08-30 13:43:05 -0700268}
269#endif // PDF_ENABLE_XFA
270
271CFX_FloatRect CPDFSDK_WidgetHandler::GetViewBBox(CPDFSDK_PageView* pPageView,
272 CPDFSDK_Annot* pAnnot) {
273 if (!pAnnot->IsSignatureWidget() && m_pFormFiller)
274 return CFX_FloatRect(m_pFormFiller->GetViewBBox(pPageView, pAnnot));
275
276 return CFX_FloatRect(0, 0, 0, 0);
277}
278
tsepez4cf55152016-11-02 14:37:54 -0700279bool CPDFSDK_WidgetHandler::HitTest(CPDFSDK_PageView* pPageView,
280 CPDFSDK_Annot* pAnnot,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500281 const CFX_PointF& point) {
jaepark8c541822016-08-30 13:43:05 -0700282 ASSERT(pPageView);
283 ASSERT(pAnnot);
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500284 return GetViewBBox(pPageView, pAnnot).Contains(point);
jaepark8c541822016-08-30 13:43:05 -0700285}