blob: d3aa37e4d5d31a56d976b46a172a20fbe23330c8 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairedbb3192016-03-21 09:08:24 -04007#include "fpdfsdk/formfiller/cffl_formfiller.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07008
Lei Zhangd24236a2017-06-29 18:28:58 -07009#include <utility>
10
dsinclair41872fa2016-10-04 11:29:35 -070011#include "core/fpdfapi/page/cpdf_page.h"
dsinclair74a34fc2016-09-29 16:41:42 -070012#include "core/fxge/cfx_renderdevice.h"
dsinclair735606d2016-10-05 15:47:02 -070013#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070014#include "fpdfsdk/cpdfsdk_pageview.h"
15#include "fpdfsdk/cpdfsdk_widget.h"
Dan Sinclairedbb3192016-03-21 09:08:24 -040016#include "fpdfsdk/formfiller/cba_fontmap.h"
dsinclair114e46a2016-09-29 17:18:21 -070017#include "fpdfsdk/fsdk_common.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -080018
Dan Sinclairb9eed2f2017-07-10 11:35:18 -040019namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Dan Sinclairb9eed2f2017-07-10 11:35:18 -040021CPDFSDK_Widget* CPDFSDKAnnotToWidget(CPDFSDK_Annot* annot) {
22 return static_cast<CPDFSDK_Widget*>(annot);
23}
24
25} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
dsinclair8779fa82016-10-12 12:05:44 -070027CFFL_FormFiller::CFFL_FormFiller(CPDFSDK_FormFillEnvironment* pFormFillEnv,
Lei Zhang4f34c642017-06-02 00:45:14 -070028 CPDFSDK_Widget* pWidget)
29 : m_pFormFillEnv(pFormFillEnv), m_pWidget(pWidget), m_bValid(false) {
30 ASSERT(m_pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031}
32
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033CFFL_FormFiller::~CFFL_FormFiller() {
dsinclair28a4a242016-08-22 13:36:02 -070034 DestroyWindows();
35}
36
37void CFFL_FormFiller::DestroyWindows() {
Lei Zhangab5537d2016-01-06 14:58:14 -080038 for (const auto& it : m_Maps) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 CPWL_Wnd* pWnd = it.second;
Tom Sepez134ac912017-09-15 15:32:01 -070040 auto* pData = static_cast<CFFL_PrivateData*>(pWnd->GetAttachedData());
Lei Zhangab5537d2016-01-06 14:58:14 -080041 pWnd->InvalidateProvider(this);
Lei Zhang606346f2015-06-19 18:11:07 -070042 pWnd->Destroy();
43 delete pWnd;
44 delete pData;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 }
46 m_Maps.clear();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047}
48
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049FX_RECT CFFL_FormFiller::GetViewBBox(CPDFSDK_PageView* pPageView,
50 CPDFSDK_Annot* pAnnot) {
Lei Zhang96660d62015-12-14 18:27:25 -080051 ASSERT(pPageView);
52 ASSERT(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053
Tom Sepez281a9ea2016-02-26 14:24:28 -080054 CFX_FloatRect rcAnnot = m_pWidget->GetRect();
Dan Sinclairb9eed2f2017-07-10 11:35:18 -040055 if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false))
56 rcAnnot = PWLtoFFL(pWnd->GetWindowRect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057
Tom Sepez281a9ea2016-02-26 14:24:28 -080058 CFX_FloatRect rcWin = rcAnnot;
Tom Sepez281a9ea2016-02-26 14:24:28 -080059 CFX_FloatRect rcFocus = GetFocusBox(pPageView);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 if (!rcFocus.IsEmpty())
61 rcWin.Union(rcFocus);
62
dan sinclairadf922f2017-07-12 21:56:27 -040063 if (!rcWin.IsEmpty()) {
64 rcWin.Inflate(1, 1);
65 rcWin.Normalize();
66 }
67 return rcWin.GetOuterRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068}
69
70void CFFL_FormFiller::OnDraw(CPDFSDK_PageView* pPageView,
71 CPDFSDK_Annot* pAnnot,
72 CFX_RenderDevice* pDevice,
Lei Zhang2b6e2a72017-08-28 11:34:16 -070073 const CFX_Matrix& mtUser2Device) {
jaepark956553e2016-08-31 06:49:27 -070074 ASSERT(pAnnot->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075
tsepez4cf55152016-11-02 14:37:54 -070076 if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false)) {
Tom Sepez60d909e2015-12-10 15:34:55 -080077 CFX_Matrix mt = GetCurMatrix();
Lei Zhang2b6e2a72017-08-28 11:34:16 -070078 mt.Concat(mtUser2Device);
Lei Zhangeb14e042017-08-15 13:56:43 -070079 pWnd->DrawAppearance(pDevice, mt);
Dan Sinclairb9eed2f2017-07-10 11:35:18 -040080 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 }
Dan Sinclairb9eed2f2017-07-10 11:35:18 -040082
83 CPDFSDK_Widget* pWidget = CPDFSDKAnnotToWidget(pAnnot);
84 if (!CFFL_InteractiveFormFiller::IsVisible(pWidget))
85 return;
86
Lei Zhang2b6e2a72017-08-28 11:34:16 -070087 pWidget->DrawAppearance(pDevice, mtUser2Device, CPDF_Annot::Normal, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088}
89
90void CFFL_FormFiller::OnDrawDeactive(CPDFSDK_PageView* pPageView,
91 CPDFSDK_Annot* pAnnot,
92 CFX_RenderDevice* pDevice,
Lei Zhang2b6e2a72017-08-28 11:34:16 -070093 const CFX_Matrix& mtUser2Device) {
94 CPDFSDKAnnotToWidget(pAnnot)->DrawAppearance(pDevice, mtUser2Device,
Dan Sinclairb9eed2f2017-07-10 11:35:18 -040095 CPDF_Annot::Normal, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096}
97
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098void CFFL_FormFiller::OnMouseEnter(CPDFSDK_PageView* pPageView,
99 CPDFSDK_Annot* pAnnot) {}
100
101void CFFL_FormFiller::OnMouseExit(CPDFSDK_PageView* pPageView,
102 CPDFSDK_Annot* pAnnot) {
103 EndTimer();
Lei Zhang96660d62015-12-14 18:27:25 -0800104 ASSERT(m_pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105}
106
tsepez4cf55152016-11-02 14:37:54 -0700107bool CFFL_FormFiller::OnLButtonDown(CPDFSDK_PageView* pPageView,
108 CPDFSDK_Annot* pAnnot,
109 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500110 const CFX_PointF& point) {
Lei Zhangf552cca2017-06-29 18:28:05 -0700111 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true);
112 if (!pWnd)
113 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114
Lei Zhangf552cca2017-06-29 18:28:05 -0700115 m_bValid = true;
116 FX_RECT rect = GetViewBBox(pPageView, pAnnot);
117 InvalidateRect(rect);
118 if (!rect.Contains(static_cast<int>(point.x), static_cast<int>(point.y)))
119 return false;
120 return pWnd->OnLButtonDown(WndtoPWL(pPageView, point), nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121}
122
tsepez4cf55152016-11-02 14:37:54 -0700123bool CFFL_FormFiller::OnLButtonUp(CPDFSDK_PageView* pPageView,
124 CPDFSDK_Annot* pAnnot,
125 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500126 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700127 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
tsepez63f545c2016-09-13 16:08:49 -0700128 if (!pWnd)
tsepez4cf55152016-11-02 14:37:54 -0700129 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500131 InvalidateRect(GetViewBBox(pPageView, pAnnot));
tsepez63f545c2016-09-13 16:08:49 -0700132 pWnd->OnLButtonUp(WndtoPWL(pPageView, point), nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700133 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134}
135
tsepez4cf55152016-11-02 14:37:54 -0700136bool CFFL_FormFiller::OnLButtonDblClk(CPDFSDK_PageView* pPageView,
137 CPDFSDK_Annot* pAnnot,
138 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500139 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700140 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
tsepez63f545c2016-09-13 16:08:49 -0700141 if (!pWnd)
tsepez4cf55152016-11-02 14:37:54 -0700142 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143
tsepez63f545c2016-09-13 16:08:49 -0700144 pWnd->OnLButtonDblClk(WndtoPWL(pPageView, point), nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700145 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146}
147
tsepez4cf55152016-11-02 14:37:54 -0700148bool CFFL_FormFiller::OnMouseMove(CPDFSDK_PageView* pPageView,
149 CPDFSDK_Annot* pAnnot,
150 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500151 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700152 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
tsepez63f545c2016-09-13 16:08:49 -0700153 if (!pWnd)
tsepez4cf55152016-11-02 14:37:54 -0700154 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155
tsepez63f545c2016-09-13 16:08:49 -0700156 pWnd->OnMouseMove(WndtoPWL(pPageView, point), nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700157 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158}
159
tsepez4cf55152016-11-02 14:37:54 -0700160bool CFFL_FormFiller::OnMouseWheel(CPDFSDK_PageView* pPageView,
161 CPDFSDK_Annot* pAnnot,
162 uint32_t nFlags,
163 short zDelta,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500164 const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 if (!IsValid())
tsepez4cf55152016-11-02 14:37:54 -0700166 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167
tsepez4cf55152016-11-02 14:37:54 -0700168 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true);
tsepez63f545c2016-09-13 16:08:49 -0700169 return pWnd && pWnd->OnMouseWheel(zDelta, WndtoPWL(pPageView, point), nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170}
171
tsepez4cf55152016-11-02 14:37:54 -0700172bool CFFL_FormFiller::OnRButtonDown(CPDFSDK_PageView* pPageView,
173 CPDFSDK_Annot* pAnnot,
174 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500175 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700176 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true);
tsepez63f545c2016-09-13 16:08:49 -0700177 if (!pWnd)
tsepez4cf55152016-11-02 14:37:54 -0700178 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179
tsepez63f545c2016-09-13 16:08:49 -0700180 pWnd->OnRButtonDown(WndtoPWL(pPageView, point), nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700181 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182}
183
tsepez4cf55152016-11-02 14:37:54 -0700184bool CFFL_FormFiller::OnRButtonUp(CPDFSDK_PageView* pPageView,
185 CPDFSDK_Annot* pAnnot,
186 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500187 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700188 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
tsepez63f545c2016-09-13 16:08:49 -0700189 if (!pWnd)
tsepez4cf55152016-11-02 14:37:54 -0700190 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191
tsepez63f545c2016-09-13 16:08:49 -0700192 pWnd->OnRButtonUp(WndtoPWL(pPageView, point), nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700193 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194}
195
tsepez4cf55152016-11-02 14:37:54 -0700196bool CFFL_FormFiller::OnKeyDown(CPDFSDK_Annot* pAnnot,
197 uint32_t nKeyCode,
dsinclair72177da2016-09-15 12:07:23 -0700198 uint32_t nFlags) {
Lei Zhangf552cca2017-06-29 18:28:05 -0700199 if (!IsValid())
200 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201
Lei Zhangf552cca2017-06-29 18:28:05 -0700202 CPDFSDK_PageView* pPageView = GetCurPageView(true);
203 ASSERT(pPageView);
tsepez4cf55152016-11-02 14:37:54 -0700204
Lei Zhangf552cca2017-06-29 18:28:05 -0700205 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
206 return pWnd && pWnd->OnKeyDown(nKeyCode, nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700207}
208
209bool CFFL_FormFiller::OnChar(CPDFSDK_Annot* pAnnot,
210 uint32_t nChar,
211 uint32_t nFlags) {
Lei Zhangf552cca2017-06-29 18:28:05 -0700212 if (!IsValid())
213 return false;
tsepez4cf55152016-11-02 14:37:54 -0700214
Lei Zhangf552cca2017-06-29 18:28:05 -0700215 CPDFSDK_PageView* pPageView = GetCurPageView(true);
216 ASSERT(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217
Lei Zhangf552cca2017-06-29 18:28:05 -0700218 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
219 return pWnd && pWnd->OnChar(nChar, nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221
Ryan Harrison275e2602017-09-18 14:23:18 -0400222WideString CFFL_FormFiller::GetSelectedText(CPDFSDK_Annot* pAnnot) {
Diana Gagedce2d722017-06-20 11:17:11 -0700223 if (!IsValid())
Ryan Harrison275e2602017-09-18 14:23:18 -0400224 return WideString();
Diana Gagedce2d722017-06-20 11:17:11 -0700225
226 CPDFSDK_PageView* pPageView = GetCurPageView(true);
227 ASSERT(pPageView);
228
229 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
Ryan Harrison275e2602017-09-18 14:23:18 -0400230 return pWnd ? pWnd->GetSelectedText() : WideString();
Diana Gagedce2d722017-06-20 11:17:11 -0700231}
232
Diana Gageab390972017-07-28 17:07:39 -0700233void CFFL_FormFiller::ReplaceSelection(CPDFSDK_Annot* pAnnot,
Ryan Harrison275e2602017-09-18 14:23:18 -0400234 const WideString& text) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700235 if (!IsValid())
236 return;
237
238 CPDFSDK_PageView* pPageView = GetCurPageView(true);
239 ASSERT(pPageView);
240
241 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
242 if (!pWnd)
243 return;
244
Diana Gageab390972017-07-28 17:07:39 -0700245 pWnd->ReplaceSelection(text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700246}
247
dsinclair72177da2016-09-15 12:07:23 -0700248void CFFL_FormFiller::SetFocusForAnnot(CPDFSDK_Annot* pAnnot, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
Tom Sepez1b246282015-11-25 15:15:31 -0800250 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
dsinclair8779fa82016-10-12 12:05:44 -0700251 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, true);
tsepez4cf55152016-11-02 14:37:54 -0700252 if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 pWnd->SetFocus();
254
tsepez4cf55152016-11-02 14:37:54 -0700255 m_bValid = true;
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500256 InvalidateRect(GetViewBBox(pPageView, pAnnot));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257}
258
dsinclair72177da2016-09-15 12:07:23 -0700259void CFFL_FormFiller::KillFocusForAnnot(CPDFSDK_Annot* pAnnot, uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 if (!IsValid())
Lei Zhang8c217c52015-10-03 23:26:09 -0700261 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262
dsinclair461eeaf2016-07-27 07:40:05 -0700263 CPDFSDK_PageView* pPageView = GetCurPageView(false);
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400264 if (!pPageView || !CommitData(pPageView, nFlag))
Lei Zhang8c217c52015-10-03 23:26:09 -0700265 return;
tsepez4cf55152016-11-02 14:37:54 -0700266 if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 pWnd->KillFocus();
268
tsepez4cf55152016-11-02 14:37:54 -0700269 bool bDestroyPDFWindow;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 switch (m_pWidget->GetFieldType()) {
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000271 case FormFieldType::kPushButton:
272 case FormFieldType::kCheckBox:
273 case FormFieldType::kRadioButton:
tsepez4cf55152016-11-02 14:37:54 -0700274 bDestroyPDFWindow = true;
Lei Zhangd77f03f2015-12-28 13:12:26 -0800275 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 default:
tsepez4cf55152016-11-02 14:37:54 -0700277 bDestroyPDFWindow = false;
Lei Zhangd77f03f2015-12-28 13:12:26 -0800278 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 }
Lei Zhang8c217c52015-10-03 23:26:09 -0700280 EscapeFiller(pPageView, bDestroyPDFWindow);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281}
282
tsepez4cf55152016-11-02 14:37:54 -0700283bool CFFL_FormFiller::IsValid() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 return m_bValid;
285}
286
Tom Sepezbf157302017-09-15 13:26:32 -0700287CPWL_Wnd::CreateParams CFFL_FormFiller::GetCreateParam() {
288 CPWL_Wnd::CreateParams cp;
thestig1cd352e2016-06-07 17:53:06 -0700289 cp.pParentWnd = nullptr;
Dan Sinclairbc8dcc32017-01-19 13:53:02 -0500290 cp.pProvider.Reset(this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 cp.rcRectWnd = GetPDFWindowRect();
292
tsepezc3255f52016-03-25 14:52:27 -0700293 uint32_t dwCreateFlags = PWS_BORDER | PWS_BACKGROUND | PWS_VISIBLE;
294 uint32_t dwFieldFlag = m_pWidget->GetFieldFlags();
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400295 if (dwFieldFlag & FIELDFLAG_READONLY)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 dwCreateFlags |= PWS_READONLY;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297
298 FX_COLORREF color;
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400299 if (m_pWidget->GetFillColor(color))
Dan Sinclair7f55a542017-07-13 14:17:10 -0400300 cp.sBackgroundColor = CFX_Color(color);
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400301 if (m_pWidget->GetBorderColor(color))
Dan Sinclair7f55a542017-07-13 14:17:10 -0400302 cp.sBorderColor = CFX_Color(color);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303
Dan Sinclair8e7f9322017-10-16 11:35:42 -0400304 cp.sTextColor = CFX_Color(CFX_Color::kGray, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400306 if (m_pWidget->GetTextColor(color))
Dan Sinclair7f55a542017-07-13 14:17:10 -0400307 cp.sTextColor = CFX_Color(color);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308
309 cp.fFontSize = m_pWidget->GetFontSize();
310 cp.dwBorderWidth = m_pWidget->GetBorderWidth();
311
dsinclair92cb5e52016-05-16 11:38:28 -0700312 cp.nBorderStyle = m_pWidget->GetBorderStyle();
313 switch (cp.nBorderStyle) {
314 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 cp.sDash = CPWL_Dash(3, 3, 0);
316 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700317 case BorderStyle::BEVELED:
dsinclair92cb5e52016-05-16 11:38:28 -0700318 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 cp.dwBorderWidth *= 2;
320 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700321 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 break;
323 }
324
dsinclair92cb5e52016-05-16 11:38:28 -0700325 if (cp.fFontSize <= 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 dwCreateFlags |= PWS_AUTOFONTSIZE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327
328 cp.dwFlags = dwCreateFlags;
dsinclair8779fa82016-10-12 12:05:44 -0700329 cp.pSystemHandler = m_pFormFillEnv->GetSysHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 return cp;
331}
332
333CPWL_Wnd* CFFL_FormFiller::GetPDFWindow(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700334 bool bNew) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 ASSERT(pPageView);
336
337 auto it = m_Maps.find(pPageView);
338 const bool found = it != m_Maps.end();
339 CPWL_Wnd* pWnd = found ? it->second : nullptr;
340 if (!bNew)
341 return pWnd;
342
Lei Zhangf552cca2017-06-29 18:28:05 -0700343 if (!found) {
Tom Sepezbf157302017-09-15 13:26:32 -0700344 CPWL_Wnd::CreateParams cp = GetCreateParam();
Tom Sepezd0409af2017-05-25 15:53:57 -0700345 cp.pAttachedWidget.Reset(m_pWidget.Get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346
Tom Sepez134ac912017-09-15 15:32:01 -0700347 auto* pPrivateData = new CFFL_PrivateData;
Tom Sepezd0409af2017-05-25 15:53:57 -0700348 pPrivateData->pWidget = m_pWidget.Get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 pPrivateData->pPageView = pPageView;
Tom Sepez7b1e53c2017-09-15 15:43:11 -0700350 pPrivateData->nWidgetAppearanceAge = m_pWidget->GetAppearanceAge();
351 pPrivateData->nWidgetValueAge = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 cp.pAttachedData = pPrivateData;
Lei Zhangf552cca2017-06-29 18:28:05 -0700353 CPWL_Wnd* pNewWnd = NewPDFWindow(cp);
354 m_Maps[pPageView] = pNewWnd;
355 return pNewWnd;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 }
357
Lei Zhangf552cca2017-06-29 18:28:05 -0700358 auto* pPrivateData = static_cast<CFFL_PrivateData*>(pWnd->GetAttachedData());
Tom Sepez7b1e53c2017-09-15 15:43:11 -0700359 if (pPrivateData->nWidgetAppearanceAge == m_pWidget->GetAppearanceAge())
Lei Zhangf552cca2017-06-29 18:28:05 -0700360 return pWnd;
361
Tom Sepez7b1e53c2017-09-15 15:43:11 -0700362 return ResetPDFWindow(
363 pPageView, pPrivateData->nWidgetValueAge == m_pWidget->GetValueAge());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364}
365
366void CFFL_FormFiller::DestroyPDFWindow(CPDFSDK_PageView* pPageView) {
367 auto it = m_Maps.find(pPageView);
368 if (it == m_Maps.end())
369 return;
370
371 CPWL_Wnd* pWnd = it->second;
Tom Sepez134ac912017-09-15 15:32:01 -0700372 auto* pData = static_cast<CFFL_PrivateData*>(pWnd->GetAttachedData());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 pWnd->Destroy();
374 delete pWnd;
375 delete pData;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 m_Maps.erase(it);
377}
378
Tom Sepez134ac912017-09-15 15:32:01 -0700379CFX_Matrix CFFL_FormFiller::GetWindowMatrix(CPWL_Wnd::PrivateData* pAttached) {
Lei Zhang0a913942017-06-01 01:37:51 -0700380 CFX_Matrix mt;
Tom Sepez134ac912017-09-15 15:32:01 -0700381 auto* pPrivateData = static_cast<CFFL_PrivateData*>(pAttached);
382 if (!pPrivateData || !pPrivateData->pPageView)
Lei Zhang0a913942017-06-01 01:37:51 -0700383 return mt;
Dan Sinclairbba2a7c2017-02-07 16:36:39 -0500384
Lei Zhang0a913942017-06-01 01:37:51 -0700385 CFX_Matrix mtPageView;
386 pPrivateData->pPageView->GetCurrentMatrix(mtPageView);
387 mt = GetCurMatrix();
388 mt.Concat(mtPageView);
389 return mt;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390}
391
Tom Sepez60d909e2015-12-10 15:34:55 -0800392CFX_Matrix CFFL_FormFiller::GetCurMatrix() {
393 CFX_Matrix mt;
jaeparka1d21112016-08-25 13:33:34 -0700394 CFX_FloatRect rcDA = m_pWidget->GetPDFAnnot()->GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 switch (m_pWidget->GetRotate()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700396 case 90:
Tom Sepez60d909e2015-12-10 15:34:55 -0800397 mt = CFX_Matrix(0, 1, -1, 0, rcDA.right - rcDA.left, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 break;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700399 case 180:
Tom Sepez60d909e2015-12-10 15:34:55 -0800400 mt = CFX_Matrix(-1, 0, 0, -1, rcDA.right - rcDA.left,
401 rcDA.top - rcDA.bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 break;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700403 case 270:
Tom Sepez60d909e2015-12-10 15:34:55 -0800404 mt = CFX_Matrix(0, -1, 1, 0, 0, rcDA.top - rcDA.bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 break;
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400406 case 0:
407 default:
408 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 }
410 mt.e += rcDA.left;
411 mt.f += rcDA.bottom;
412
413 return mt;
414}
415
Tom Sepez281a9ea2016-02-26 14:24:28 -0800416CFX_FloatRect CFFL_FormFiller::GetPDFWindowRect() const {
jaeparka1d21112016-08-25 13:33:34 -0700417 CFX_FloatRect rectAnnot = m_pWidget->GetPDFAnnot()->GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418
Dan Sinclair05df0752017-03-14 14:43:42 -0400419 float fWidth = rectAnnot.right - rectAnnot.left;
420 float fHeight = rectAnnot.top - rectAnnot.bottom;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 if ((m_pWidget->GetRotate() / 90) & 0x01)
Lei Zhangd24236a2017-06-29 18:28:58 -0700422 std::swap(fWidth, fHeight);
Tom Sepez281a9ea2016-02-26 14:24:28 -0800423 return CFX_FloatRect(0, 0, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424}
425
dsinclair461eeaf2016-07-27 07:40:05 -0700426CPDFSDK_PageView* CFFL_FormFiller::GetCurPageView(bool renew) {
Lei Zhang4f34c642017-06-02 00:45:14 -0700427 UnderlyingPageType* pPage = m_pWidget->GetUnderlyingPage();
428 return m_pFormFillEnv->GetPageView(pPage, renew);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429}
430
Tom Sepez281a9ea2016-02-26 14:24:28 -0800431CFX_FloatRect CFFL_FormFiller::GetFocusBox(CPDFSDK_PageView* pPageView) {
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400432 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
433 if (!pWnd)
434 return CFX_FloatRect();
435
436 CFX_FloatRect rcFocus = FFLtoWnd(pPageView, PWLtoFFL(pWnd->GetFocusRect()));
437 return pPageView->GetPDFPage()->GetPageBBox().Contains(rcFocus)
438 ? rcFocus
439 : CFX_FloatRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440}
441
Tom Sepez281a9ea2016-02-26 14:24:28 -0800442CFX_FloatRect CFFL_FormFiller::FFLtoPWL(const CFX_FloatRect& rect) {
Jane Liu878b27d2017-08-22 10:50:06 -0400443 return GetCurMatrix().GetInverse().TransformRect(rect);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444}
445
Tom Sepez281a9ea2016-02-26 14:24:28 -0800446CFX_FloatRect CFFL_FormFiller::PWLtoFFL(const CFX_FloatRect& rect) {
Jane Liu878b27d2017-08-22 10:50:06 -0400447 return GetCurMatrix().TransformRect(rect);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448}
449
Dan Sinclairf528eee2017-02-14 11:52:07 -0500450CFX_PointF CFFL_FormFiller::FFLtoPWL(const CFX_PointF& point) {
Nicolas Penab21f1742017-06-29 12:02:06 -0400451 return GetCurMatrix().GetInverse().Transform(point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452}
453
Dan Sinclairf528eee2017-02-14 11:52:07 -0500454CFX_PointF CFFL_FormFiller::PWLtoFFL(const CFX_PointF& point) {
Dan Sinclaira0061af2017-02-23 09:25:17 -0500455 return GetCurMatrix().Transform(point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456}
457
Dan Sinclairf528eee2017-02-14 11:52:07 -0500458CFX_PointF CFFL_FormFiller::WndtoPWL(CPDFSDK_PageView* pPageView,
459 const CFX_PointF& pt) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 return FFLtoPWL(pt);
461}
462
Tom Sepez281a9ea2016-02-26 14:24:28 -0800463CFX_FloatRect CFFL_FormFiller::FFLtoWnd(CPDFSDK_PageView* pPageView,
464 const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 return rect;
466}
467
tsepez4cf55152016-11-02 14:37:54 -0700468bool CFFL_FormFiller::CommitData(CPDFSDK_PageView* pPageView, uint32_t nFlag) {
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400469 if (!IsDataChanged(pPageView))
470 return true;
Lei Zhang6a3fc452017-06-13 14:04:02 -0700471
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400472 CFFL_InteractiveFormFiller* pFormFiller =
473 m_pFormFillEnv->GetInteractiveFormFiller();
474 CPDFSDK_Annot::ObservedPtr pObserved(m_pWidget.Get());
475
476 if (!pFormFiller->OnKeyStrokeCommit(&pObserved, pPageView, nFlag)) {
Dan Sinclairbdb96532017-07-06 10:40:51 -0400477 if (!pObserved)
478 return false;
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400479 ResetPDFWindow(pPageView, false);
480 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 }
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400482 if (!pObserved)
483 return false;
484
485 if (!pFormFiller->OnValidate(&pObserved, pPageView, nFlag)) {
486 if (!pObserved)
487 return false;
488 ResetPDFWindow(pPageView, false);
489 return true;
490 }
491 if (!pObserved)
492 return false;
493
Luật Nguyễn18864712017-10-10 12:39:22 +0800494 SaveData(pPageView); // may invoking JS to delete this widget.
495 if (!pObserved)
496 return false;
497
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400498 pFormFiller->OnCalculate(&pObserved, pPageView, nFlag);
499 if (!pObserved)
500 return false;
501
502 pFormFiller->OnFormat(&pObserved, pPageView, nFlag);
503 if (!pObserved)
504 return false;
505
tsepez4cf55152016-11-02 14:37:54 -0700506 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700507}
508
tsepez4cf55152016-11-02 14:37:54 -0700509bool CFFL_FormFiller::IsDataChanged(CPDFSDK_PageView* pPageView) {
510 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700511}
512
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700513void CFFL_FormFiller::SaveData(CPDFSDK_PageView* pPageView) {}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700514
Tom Sepez51da0932015-11-25 16:05:49 -0800515#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700516bool CFFL_FormFiller::IsFieldFull(CPDFSDK_PageView* pPageView) {
517 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700518}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800519#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700520
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521void CFFL_FormFiller::SetChangeMark() {
dsinclair8779fa82016-10-12 12:05:44 -0700522 m_pFormFillEnv->OnChange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700523}
524
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525void CFFL_FormFiller::GetActionData(CPDFSDK_PageView* pPageView,
526 CPDF_AAction::AActionType type,
527 PDFSDK_FieldAction& fa) {
528 fa.sValue = m_pWidget->GetValue();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700529}
530
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700531void CFFL_FormFiller::SetActionData(CPDFSDK_PageView* pPageView,
532 CPDF_AAction::AActionType type,
533 const PDFSDK_FieldAction& fa) {}
534
tsepez4cf55152016-11-02 14:37:54 -0700535bool CFFL_FormFiller::IsActionDataChanged(CPDF_AAction::AActionType type,
536 const PDFSDK_FieldAction& faOld,
537 const PDFSDK_FieldAction& faNew) {
538 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700539}
540
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541void CFFL_FormFiller::SaveState(CPDFSDK_PageView* pPageView) {}
542
543void CFFL_FormFiller::RestoreState(CPDFSDK_PageView* pPageView) {}
544
545CPWL_Wnd* CFFL_FormFiller::ResetPDFWindow(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700546 bool bRestoreValue) {
547 return GetPDFWindow(pPageView, false);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700548}
549
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550void CFFL_FormFiller::TimerProc() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700551
dsinclairb9590102016-04-27 06:38:59 -0700552CFX_SystemHandler* CFFL_FormFiller::GetSystemHandler() const {
dsinclair8779fa82016-10-12 12:05:44 -0700553 return m_pFormFillEnv->GetSysHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700554}
555
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700556void CFFL_FormFiller::EscapeFiller(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700557 bool bDestroyPDFWindow) {
558 m_bValid = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559
Tom Sepezd0409af2017-05-25 15:53:57 -0700560 InvalidateRect(GetViewBBox(pPageView, m_pWidget.Get()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 if (bDestroyPDFWindow)
562 DestroyPDFWindow(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700563}
564
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500565void CFFL_FormFiller::InvalidateRect(const FX_RECT& rect) {
566 m_pFormFillEnv->Invalidate(m_pWidget->GetUnderlyingPage(), rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700567}