blob: 11074f927e11232b977f7d7c67130069a0ea786a [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
Lei Zhanga676aca2019-02-02 00:41:09 +000011#include "constants/form_flags.h"
dsinclair41872fa2016-10-04 11:29:35 -070012#include "core/fpdfapi/page/cpdf_page.h"
Tom Sepez3609c982019-06-04 21:35:25 +000013#include "core/fpdfdoc/cba_fontmap.h"
dsinclair74a34fc2016-09-29 16:41:42 -070014#include "core/fxge/cfx_renderdevice.h"
dsinclair735606d2016-10-05 15:47:02 -070015#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070016#include "fpdfsdk/cpdfsdk_pageview.h"
17#include "fpdfsdk/cpdfsdk_widget.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -080018
dsinclair8779fa82016-10-12 12:05:44 -070019CFFL_FormFiller::CFFL_FormFiller(CPDFSDK_FormFillEnvironment* pFormFillEnv,
Lei Zhang4f34c642017-06-02 00:45:14 -070020 CPDFSDK_Widget* pWidget)
Tom Sepez333165a2018-10-24 23:56:43 +000021 : m_pFormFillEnv(pFormFillEnv), m_pWidget(pWidget) {
Lei Zhang4f34c642017-06-02 00:45:14 -070022 ASSERT(m_pFormFillEnv);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023}
24
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025CFFL_FormFiller::~CFFL_FormFiller() {
dsinclair28a4a242016-08-22 13:36:02 -070026 DestroyWindows();
27}
28
29void CFFL_FormFiller::DestroyWindows() {
Tom Sepez333165a2018-10-24 23:56:43 +000030 while (!m_Maps.empty()) {
Tom Sepez6fe32f82018-10-25 23:25:58 +000031 auto it = m_Maps.begin();
32 std::unique_ptr<CPWL_Wnd> pWnd = std::move(it->second);
33 m_Maps.erase(it);
34 pWnd->InvalidateProvider(this);
35 pWnd->Destroy();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037}
38
Lei Zhangd6b72872019-02-21 21:33:53 +000039FX_RECT CFFL_FormFiller::GetViewBBox(CPDFSDK_PageView* pPageView) {
Lei Zhang96660d62015-12-14 18:27:25 -080040 ASSERT(pPageView);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041
Tom Sepez281a9ea2016-02-26 14:24:28 -080042 CFX_FloatRect rcAnnot = m_pWidget->GetRect();
Dan Sinclairb9eed2f2017-07-10 11:35:18 -040043 if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false))
44 rcAnnot = PWLtoFFL(pWnd->GetWindowRect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045
Tom Sepez281a9ea2016-02-26 14:24:28 -080046 CFX_FloatRect rcWin = rcAnnot;
Tom Sepez281a9ea2016-02-26 14:24:28 -080047 CFX_FloatRect rcFocus = GetFocusBox(pPageView);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 if (!rcFocus.IsEmpty())
49 rcWin.Union(rcFocus);
50
dan sinclairadf922f2017-07-12 21:56:27 -040051 if (!rcWin.IsEmpty()) {
52 rcWin.Inflate(1, 1);
53 rcWin.Normalize();
54 }
55 return rcWin.GetOuterRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056}
57
58void CFFL_FormFiller::OnDraw(CPDFSDK_PageView* pPageView,
59 CPDFSDK_Annot* pAnnot,
60 CFX_RenderDevice* pDevice,
Lei Zhang2b6e2a72017-08-28 11:34:16 -070061 const CFX_Matrix& mtUser2Device) {
Lei Zhanga1f94aa2018-12-20 01:56:06 +000062 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
63 if (pWnd) {
64 pWnd->DrawAppearance(pDevice, GetCurMatrix() * mtUser2Device);
Dan Sinclairb9eed2f2017-07-10 11:35:18 -040065 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 }
Dan Sinclairb9eed2f2017-07-10 11:35:18 -040067
Tom Sepez11a6bec2018-07-25 23:25:55 +000068 CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
Dan Sinclairb9eed2f2017-07-10 11:35:18 -040069 if (!CFFL_InteractiveFormFiller::IsVisible(pWidget))
70 return;
71
Lei Zhang2b6e2a72017-08-28 11:34:16 -070072 pWidget->DrawAppearance(pDevice, mtUser2Device, CPDF_Annot::Normal, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073}
74
75void CFFL_FormFiller::OnDrawDeactive(CPDFSDK_PageView* pPageView,
76 CPDFSDK_Annot* pAnnot,
77 CFX_RenderDevice* pDevice,
Lei Zhang2b6e2a72017-08-28 11:34:16 -070078 const CFX_Matrix& mtUser2Device) {
Tom Sepez11a6bec2018-07-25 23:25:55 +000079 ToCPDFSDKWidget(pAnnot)->DrawAppearance(pDevice, mtUser2Device,
80 CPDF_Annot::Normal, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081}
82
Lei Zhangd6b72872019-02-21 21:33:53 +000083void CFFL_FormFiller::OnMouseEnter(CPDFSDK_PageView* pPageView) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084
Lei Zhangd6b72872019-02-21 21:33:53 +000085void CFFL_FormFiller::OnMouseExit(CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 EndTimer();
Lei Zhang96660d62015-12-14 18:27:25 -080087 ASSERT(m_pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088}
89
tsepez4cf55152016-11-02 14:37:54 -070090bool CFFL_FormFiller::OnLButtonDown(CPDFSDK_PageView* pPageView,
91 CPDFSDK_Annot* pAnnot,
92 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -050093 const CFX_PointF& point) {
Lei Zhangf552cca2017-06-29 18:28:05 -070094 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true);
95 if (!pWnd)
96 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097
Lei Zhangf552cca2017-06-29 18:28:05 -070098 m_bValid = true;
Lei Zhangd6b72872019-02-21 21:33:53 +000099 FX_RECT rect = GetViewBBox(pPageView);
Lei Zhangf552cca2017-06-29 18:28:05 -0700100 InvalidateRect(rect);
101 if (!rect.Contains(static_cast<int>(point.x), static_cast<int>(point.y)))
102 return false;
103 return pWnd->OnLButtonDown(WndtoPWL(pPageView, point), nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104}
105
tsepez4cf55152016-11-02 14:37:54 -0700106bool CFFL_FormFiller::OnLButtonUp(CPDFSDK_PageView* pPageView,
107 CPDFSDK_Annot* pAnnot,
108 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500109 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700110 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
tsepez63f545c2016-09-13 16:08:49 -0700111 if (!pWnd)
tsepez4cf55152016-11-02 14:37:54 -0700112 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113
Lei Zhangd6b72872019-02-21 21:33:53 +0000114 InvalidateRect(GetViewBBox(pPageView));
tsepez63f545c2016-09-13 16:08:49 -0700115 pWnd->OnLButtonUp(WndtoPWL(pPageView, point), nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700116 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117}
118
tsepez4cf55152016-11-02 14:37:54 -0700119bool CFFL_FormFiller::OnLButtonDblClk(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700120 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500121 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700122 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
tsepez63f545c2016-09-13 16:08:49 -0700123 if (!pWnd)
tsepez4cf55152016-11-02 14:37:54 -0700124 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125
tsepez63f545c2016-09-13 16:08:49 -0700126 pWnd->OnLButtonDblClk(WndtoPWL(pPageView, point), nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700127 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128}
129
tsepez4cf55152016-11-02 14:37:54 -0700130bool CFFL_FormFiller::OnMouseMove(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700131 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500132 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700133 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
tsepez63f545c2016-09-13 16:08:49 -0700134 if (!pWnd)
tsepez4cf55152016-11-02 14:37:54 -0700135 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136
tsepez63f545c2016-09-13 16:08:49 -0700137 pWnd->OnMouseMove(WndtoPWL(pPageView, point), nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700138 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139}
140
tsepez4cf55152016-11-02 14:37:54 -0700141bool CFFL_FormFiller::OnMouseWheel(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700142 uint32_t nFlags,
143 short zDelta,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500144 const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 if (!IsValid())
tsepez4cf55152016-11-02 14:37:54 -0700146 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147
tsepez4cf55152016-11-02 14:37:54 -0700148 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true);
tsepez63f545c2016-09-13 16:08:49 -0700149 return pWnd && pWnd->OnMouseWheel(zDelta, WndtoPWL(pPageView, point), nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150}
151
tsepez4cf55152016-11-02 14:37:54 -0700152bool CFFL_FormFiller::OnRButtonDown(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700153 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500154 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700155 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true);
tsepez63f545c2016-09-13 16:08:49 -0700156 if (!pWnd)
tsepez4cf55152016-11-02 14:37:54 -0700157 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158
tsepez63f545c2016-09-13 16:08:49 -0700159 pWnd->OnRButtonDown(WndtoPWL(pPageView, point), nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700160 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161}
162
tsepez4cf55152016-11-02 14:37:54 -0700163bool CFFL_FormFiller::OnRButtonUp(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700164 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -0500165 const CFX_PointF& point) {
tsepez4cf55152016-11-02 14:37:54 -0700166 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
tsepez63f545c2016-09-13 16:08:49 -0700167 if (!pWnd)
tsepez4cf55152016-11-02 14:37:54 -0700168 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169
tsepez63f545c2016-09-13 16:08:49 -0700170 pWnd->OnRButtonUp(WndtoPWL(pPageView, point), nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700171 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172}
173
Lei Zhangd6b72872019-02-21 21:33:53 +0000174bool CFFL_FormFiller::OnKeyDown(uint32_t nKeyCode, uint32_t nFlags) {
Lei Zhangf552cca2017-06-29 18:28:05 -0700175 if (!IsValid())
176 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177
Lei Zhangf552cca2017-06-29 18:28:05 -0700178 CPDFSDK_PageView* pPageView = GetCurPageView(true);
179 ASSERT(pPageView);
tsepez4cf55152016-11-02 14:37:54 -0700180
Lei Zhangf552cca2017-06-29 18:28:05 -0700181 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
182 return pWnd && pWnd->OnKeyDown(nKeyCode, nFlags);
tsepez4cf55152016-11-02 14:37:54 -0700183}
184
185bool CFFL_FormFiller::OnChar(CPDFSDK_Annot* pAnnot,
186 uint32_t nChar,
187 uint32_t nFlags) {
Lei Zhangf552cca2017-06-29 18:28:05 -0700188 if (!IsValid())
189 return false;
tsepez4cf55152016-11-02 14:37:54 -0700190
Lei Zhangf552cca2017-06-29 18:28:05 -0700191 CPDFSDK_PageView* pPageView = GetCurPageView(true);
192 ASSERT(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193
Lei Zhangf552cca2017-06-29 18:28:05 -0700194 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
195 return pWnd && pWnd->OnChar(nChar, nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700197
rycsmitha5230e22019-02-21 17:33:03 +0000198bool CFFL_FormFiller::SetIndexSelected(int index, bool selected) {
199 return false;
200}
201
202bool CFFL_FormFiller::IsIndexSelected(int index) {
203 return false;
204}
205
Lei Zhangd6b72872019-02-21 21:33:53 +0000206WideString CFFL_FormFiller::GetText() {
Lei Zhanga4c7ac42018-04-17 15:12:58 +0000207 if (!IsValid())
208 return WideString();
209
210 CPDFSDK_PageView* pPageView = GetCurPageView(true);
211 ASSERT(pPageView);
212
213 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
214 return pWnd ? pWnd->GetText() : WideString();
215}
216
Lei Zhangd6b72872019-02-21 21:33:53 +0000217WideString CFFL_FormFiller::GetSelectedText() {
Diana Gagedce2d722017-06-20 11:17:11 -0700218 if (!IsValid())
Ryan Harrison275e2602017-09-18 14:23:18 -0400219 return WideString();
Diana Gagedce2d722017-06-20 11:17:11 -0700220
221 CPDFSDK_PageView* pPageView = GetCurPageView(true);
222 ASSERT(pPageView);
223
224 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
Ryan Harrison275e2602017-09-18 14:23:18 -0400225 return pWnd ? pWnd->GetSelectedText() : WideString();
Diana Gagedce2d722017-06-20 11:17:11 -0700226}
227
Lei Zhangd6b72872019-02-21 21:33:53 +0000228void CFFL_FormFiller::ReplaceSelection(const WideString& text) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700229 if (!IsValid())
230 return;
231
232 CPDFSDK_PageView* pPageView = GetCurPageView(true);
233 ASSERT(pPageView);
234
235 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
236 if (!pWnd)
237 return;
238
Diana Gageab390972017-07-28 17:07:39 -0700239 pWnd->ReplaceSelection(text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700240}
241
Lei Zhangd6b72872019-02-21 21:33:53 +0000242bool CFFL_FormFiller::CanUndo() {
Lei Zhangee967722018-04-19 20:55:54 +0000243 if (!IsValid())
244 return false;
245
246 CPDFSDK_PageView* pPageView = GetCurPageView(true);
247 ASSERT(pPageView);
248
249 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
250 return pWnd && pWnd->CanUndo();
251}
252
Lei Zhangd6b72872019-02-21 21:33:53 +0000253bool CFFL_FormFiller::CanRedo() {
Lei Zhangee967722018-04-19 20:55:54 +0000254 if (!IsValid())
255 return false;
256
257 CPDFSDK_PageView* pPageView = GetCurPageView(true);
258 ASSERT(pPageView);
259
260 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
261 return pWnd && pWnd->CanRedo();
262}
263
Lei Zhangd6b72872019-02-21 21:33:53 +0000264bool CFFL_FormFiller::Undo() {
Lei Zhangee967722018-04-19 20:55:54 +0000265 if (!IsValid())
266 return false;
267
268 CPDFSDK_PageView* pPageView = GetCurPageView(true);
269 ASSERT(pPageView);
270
271 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
272 return pWnd && pWnd->Undo();
273}
274
Lei Zhangd6b72872019-02-21 21:33:53 +0000275bool CFFL_FormFiller::Redo() {
Lei Zhangee967722018-04-19 20:55:54 +0000276 if (!IsValid())
277 return false;
278
279 CPDFSDK_PageView* pPageView = GetCurPageView(true);
280 ASSERT(pPageView);
281
282 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
283 return pWnd && pWnd->Redo();
284}
285
dsinclair72177da2016-09-15 12:07:23 -0700286void CFFL_FormFiller::SetFocusForAnnot(CPDFSDK_Annot* pAnnot, uint32_t nFlag) {
Tom Sepez4ef943b2018-07-26 19:06:06 +0000287 CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
Tom Sepez101535f2018-06-12 13:36:05 +0000288 IPDF_Page* pPage = pWidget->GetPage();
dsinclair8779fa82016-10-12 12:05:44 -0700289 CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, true);
tsepez4cf55152016-11-02 14:37:54 -0700290 if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 pWnd->SetFocus();
292
tsepez4cf55152016-11-02 14:37:54 -0700293 m_bValid = true;
Lei Zhangd6b72872019-02-21 21:33:53 +0000294 InvalidateRect(GetViewBBox(pPageView));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295}
296
Lei Zhangd6b72872019-02-21 21:33:53 +0000297void CFFL_FormFiller::KillFocusForAnnot(uint32_t nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 if (!IsValid())
Lei Zhang8c217c52015-10-03 23:26:09 -0700299 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300
dsinclair461eeaf2016-07-27 07:40:05 -0700301 CPDFSDK_PageView* pPageView = GetCurPageView(false);
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400302 if (!pPageView || !CommitData(pPageView, nFlag))
Lei Zhang8c217c52015-10-03 23:26:09 -0700303 return;
tsepez4cf55152016-11-02 14:37:54 -0700304 if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 pWnd->KillFocus();
306
tsepez4cf55152016-11-02 14:37:54 -0700307 bool bDestroyPDFWindow;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 switch (m_pWidget->GetFieldType()) {
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000309 case FormFieldType::kPushButton:
310 case FormFieldType::kCheckBox:
311 case FormFieldType::kRadioButton:
tsepez4cf55152016-11-02 14:37:54 -0700312 bDestroyPDFWindow = true;
Lei Zhangd77f03f2015-12-28 13:12:26 -0800313 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 default:
tsepez4cf55152016-11-02 14:37:54 -0700315 bDestroyPDFWindow = false;
Lei Zhangd77f03f2015-12-28 13:12:26 -0800316 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 }
Lei Zhang8c217c52015-10-03 23:26:09 -0700318 EscapeFiller(pPageView, bDestroyPDFWindow);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319}
320
tsepez4cf55152016-11-02 14:37:54 -0700321bool CFFL_FormFiller::IsValid() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 return m_bValid;
323}
324
Tom Sepezbf157302017-09-15 13:26:32 -0700325CPWL_Wnd::CreateParams CFFL_FormFiller::GetCreateParam() {
326 CPWL_Wnd::CreateParams cp;
Dan Sinclairbc8dcc32017-01-19 13:53:02 -0500327 cp.pProvider.Reset(this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 cp.rcRectWnd = GetPDFWindowRect();
329
tsepezc3255f52016-03-25 14:52:27 -0700330 uint32_t dwCreateFlags = PWS_BORDER | PWS_BACKGROUND | PWS_VISIBLE;
331 uint32_t dwFieldFlag = m_pWidget->GetFieldFlags();
Lei Zhanga676aca2019-02-02 00:41:09 +0000332 if (dwFieldFlag & pdfium::form_flags::kReadOnly)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 dwCreateFlags |= PWS_READONLY;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334
Lei Zhangb353f8c2018-10-10 18:39:14 +0000335 Optional<FX_COLORREF> color = m_pWidget->GetFillColor();
336 if (color.has_value())
337 cp.sBackgroundColor = CFX_Color(color.value());
338 color = m_pWidget->GetBorderColor();
339 if (color.has_value())
340 cp.sBorderColor = CFX_Color(color.value());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341
Dan Sinclair8e7f9322017-10-16 11:35:42 -0400342 cp.sTextColor = CFX_Color(CFX_Color::kGray, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343
Lei Zhangb353f8c2018-10-10 18:39:14 +0000344 color = m_pWidget->GetTextColor();
345 if (color.has_value())
346 cp.sTextColor = CFX_Color(color.value());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347
348 cp.fFontSize = m_pWidget->GetFontSize();
349 cp.dwBorderWidth = m_pWidget->GetBorderWidth();
350
dsinclair92cb5e52016-05-16 11:38:28 -0700351 cp.nBorderStyle = m_pWidget->GetBorderStyle();
352 switch (cp.nBorderStyle) {
353 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 cp.sDash = CPWL_Dash(3, 3, 0);
355 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700356 case BorderStyle::BEVELED:
dsinclair92cb5e52016-05-16 11:38:28 -0700357 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358 cp.dwBorderWidth *= 2;
359 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700360 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 break;
362 }
363
dsinclair92cb5e52016-05-16 11:38:28 -0700364 if (cp.fFontSize <= 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 dwCreateFlags |= PWS_AUTOFONTSIZE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366
367 cp.dwFlags = dwCreateFlags;
dsinclair8779fa82016-10-12 12:05:44 -0700368 cp.pSystemHandler = m_pFormFillEnv->GetSysHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 return cp;
370}
371
372CPWL_Wnd* CFFL_FormFiller::GetPDFWindow(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700373 bool bNew) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 ASSERT(pPageView);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 auto it = m_Maps.find(pPageView);
Tom Sepez333165a2018-10-24 23:56:43 +0000376 if (it == m_Maps.end()) {
377 if (!bNew)
378 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379
Tom Sepezbf157302017-09-15 13:26:32 -0700380 CPWL_Wnd::CreateParams cp = GetCreateParam();
Tom Sepezd0409af2017-05-25 15:53:57 -0700381 cp.pAttachedWidget.Reset(m_pWidget.Get());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382
Tom Sepez333165a2018-10-24 23:56:43 +0000383 auto pPrivateData = pdfium::MakeUnique<CFFL_PrivateData>();
Tom Sepezd0409af2017-05-25 15:53:57 -0700384 pPrivateData->pWidget = m_pWidget.Get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 pPrivateData->pPageView = pPageView;
Tom Sepez7b1e53c2017-09-15 15:43:11 -0700386 pPrivateData->nWidgetAppearanceAge = m_pWidget->GetAppearanceAge();
387 pPrivateData->nWidgetValueAge = 0;
Tom Sepez385bf2e2018-10-26 17:37:17 +0000388 m_Maps[pPageView] = NewPWLWindow(cp, std::move(pPrivateData));
Tom Sepez333165a2018-10-24 23:56:43 +0000389 return m_Maps[pPageView].get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 }
391
Tom Sepez333165a2018-10-24 23:56:43 +0000392 CPWL_Wnd* pWnd = it->second.get();
393 if (!bNew)
394 return pWnd;
395
Tom Sepez6fe32f82018-10-25 23:25:58 +0000396 const auto* pPrivateData =
397 static_cast<const CFFL_PrivateData*>(pWnd->GetAttachedData());
Tom Sepez7b1e53c2017-09-15 15:43:11 -0700398 if (pPrivateData->nWidgetAppearanceAge == m_pWidget->GetAppearanceAge())
Lei Zhangf552cca2017-06-29 18:28:05 -0700399 return pWnd;
400
Tom Sepez7b1e53c2017-09-15 15:43:11 -0700401 return ResetPDFWindow(
402 pPageView, pPrivateData->nWidgetValueAge == m_pWidget->GetValueAge());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403}
404
405void CFFL_FormFiller::DestroyPDFWindow(CPDFSDK_PageView* pPageView) {
406 auto it = m_Maps.find(pPageView);
407 if (it == m_Maps.end())
408 return;
409
Tom Sepez6fe32f82018-10-25 23:25:58 +0000410 std::unique_ptr<CPWL_Wnd> pWnd = std::move(it->second);
411 m_Maps.erase(it);
412 pWnd->Destroy();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413}
414
Tom Sepez6fe32f82018-10-25 23:25:58 +0000415CFX_Matrix CFFL_FormFiller::GetWindowMatrix(
416 const CPWL_Wnd::PrivateData* pAttached) {
Tom Sepez6fe32f82018-10-25 23:25:58 +0000417 const auto* pPrivateData = static_cast<const CFFL_PrivateData*>(pAttached);
Tom Sepez134ac912017-09-15 15:32:01 -0700418 if (!pPrivateData || !pPrivateData->pPageView)
Lei Zhanga1f94aa2018-12-20 01:56:06 +0000419 return CFX_Matrix();
Dan Sinclairbba2a7c2017-02-07 16:36:39 -0500420
Lei Zhanga1f94aa2018-12-20 01:56:06 +0000421 return GetCurMatrix() * pPrivateData->pPageView->GetCurrentMatrix();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700422}
423
Tom Sepez60d909e2015-12-10 15:34:55 -0800424CFX_Matrix CFFL_FormFiller::GetCurMatrix() {
425 CFX_Matrix mt;
jaeparka1d21112016-08-25 13:33:34 -0700426 CFX_FloatRect rcDA = m_pWidget->GetPDFAnnot()->GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 switch (m_pWidget->GetRotate()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700428 case 90:
Tom Sepez60d909e2015-12-10 15:34:55 -0800429 mt = CFX_Matrix(0, 1, -1, 0, rcDA.right - rcDA.left, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 break;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700431 case 180:
Tom Sepez60d909e2015-12-10 15:34:55 -0800432 mt = CFX_Matrix(-1, 0, 0, -1, rcDA.right - rcDA.left,
433 rcDA.top - rcDA.bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 break;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700435 case 270:
Tom Sepez60d909e2015-12-10 15:34:55 -0800436 mt = CFX_Matrix(0, -1, 1, 0, 0, rcDA.top - rcDA.bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 break;
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400438 case 0:
439 default:
440 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700441 }
442 mt.e += rcDA.left;
443 mt.f += rcDA.bottom;
444
445 return mt;
446}
447
Tom Sepez281a9ea2016-02-26 14:24:28 -0800448CFX_FloatRect CFFL_FormFiller::GetPDFWindowRect() const {
jaeparka1d21112016-08-25 13:33:34 -0700449 CFX_FloatRect rectAnnot = m_pWidget->GetPDFAnnot()->GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450
Lei Zhang98758892018-03-15 15:02:22 +0000451 float fWidth = rectAnnot.Width();
452 float fHeight = rectAnnot.Height();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 if ((m_pWidget->GetRotate() / 90) & 0x01)
Lei Zhangd24236a2017-06-29 18:28:58 -0700454 std::swap(fWidth, fHeight);
Tom Sepez281a9ea2016-02-26 14:24:28 -0800455 return CFX_FloatRect(0, 0, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456}
457
dsinclair461eeaf2016-07-27 07:40:05 -0700458CPDFSDK_PageView* CFFL_FormFiller::GetCurPageView(bool renew) {
Tom Sepez101535f2018-06-12 13:36:05 +0000459 IPDF_Page* pPage = m_pWidget->GetPage();
Lei Zhang4f34c642017-06-02 00:45:14 -0700460 return m_pFormFillEnv->GetPageView(pPage, renew);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461}
462
Tom Sepez281a9ea2016-02-26 14:24:28 -0800463CFX_FloatRect CFFL_FormFiller::GetFocusBox(CPDFSDK_PageView* pPageView) {
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400464 CPWL_Wnd* pWnd = GetPDFWindow(pPageView, false);
465 if (!pWnd)
466 return CFX_FloatRect();
467
468 CFX_FloatRect rcFocus = FFLtoWnd(pPageView, PWLtoFFL(pWnd->GetFocusRect()));
Tom Sepez778e59e2018-06-05 19:24:42 +0000469 return pPageView->GetPDFPage()->GetBBox().Contains(rcFocus) ? rcFocus
470 : CFX_FloatRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471}
472
Tom Sepez281a9ea2016-02-26 14:24:28 -0800473CFX_FloatRect CFFL_FormFiller::FFLtoPWL(const CFX_FloatRect& rect) {
Jane Liu878b27d2017-08-22 10:50:06 -0400474 return GetCurMatrix().GetInverse().TransformRect(rect);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475}
476
Tom Sepez281a9ea2016-02-26 14:24:28 -0800477CFX_FloatRect CFFL_FormFiller::PWLtoFFL(const CFX_FloatRect& rect) {
Jane Liu878b27d2017-08-22 10:50:06 -0400478 return GetCurMatrix().TransformRect(rect);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479}
480
Dan Sinclairf528eee2017-02-14 11:52:07 -0500481CFX_PointF CFFL_FormFiller::FFLtoPWL(const CFX_PointF& point) {
Nicolas Penab21f1742017-06-29 12:02:06 -0400482 return GetCurMatrix().GetInverse().Transform(point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483}
484
Dan Sinclairf528eee2017-02-14 11:52:07 -0500485CFX_PointF CFFL_FormFiller::PWLtoFFL(const CFX_PointF& point) {
Dan Sinclaira0061af2017-02-23 09:25:17 -0500486 return GetCurMatrix().Transform(point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487}
488
Dan Sinclairf528eee2017-02-14 11:52:07 -0500489CFX_PointF CFFL_FormFiller::WndtoPWL(CPDFSDK_PageView* pPageView,
490 const CFX_PointF& pt) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491 return FFLtoPWL(pt);
492}
493
Tom Sepez281a9ea2016-02-26 14:24:28 -0800494CFX_FloatRect CFFL_FormFiller::FFLtoWnd(CPDFSDK_PageView* pPageView,
495 const CFX_FloatRect& rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 return rect;
497}
498
tsepez4cf55152016-11-02 14:37:54 -0700499bool CFFL_FormFiller::CommitData(CPDFSDK_PageView* pPageView, uint32_t nFlag) {
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400500 if (!IsDataChanged(pPageView))
501 return true;
Lei Zhang6a3fc452017-06-13 14:04:02 -0700502
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400503 CFFL_InteractiveFormFiller* pFormFiller =
504 m_pFormFillEnv->GetInteractiveFormFiller();
Tom Sepezd8ae8f82019-06-12 17:58:33 +0000505 ObservedPtr<CPDFSDK_Annot> pObserved(m_pWidget.Get());
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400506
507 if (!pFormFiller->OnKeyStrokeCommit(&pObserved, pPageView, nFlag)) {
Dan Sinclairbdb96532017-07-06 10:40:51 -0400508 if (!pObserved)
509 return false;
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400510 ResetPDFWindow(pPageView, false);
511 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700512 }
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400513 if (!pObserved)
514 return false;
515
516 if (!pFormFiller->OnValidate(&pObserved, pPageView, nFlag)) {
517 if (!pObserved)
518 return false;
519 ResetPDFWindow(pPageView, false);
520 return true;
521 }
522 if (!pObserved)
523 return false;
524
Lei Zhangdd92aa52018-09-20 12:56:04 +0000525 SaveData(pPageView); // may invoking JS to delete this widget.
Luật Nguyễn18864712017-10-10 12:39:22 +0800526 if (!pObserved)
527 return false;
528
Dan Sinclairb9eed2f2017-07-10 11:35:18 -0400529 pFormFiller->OnCalculate(&pObserved, pPageView, nFlag);
530 if (!pObserved)
531 return false;
532
533 pFormFiller->OnFormat(&pObserved, pPageView, nFlag);
534 if (!pObserved)
535 return false;
536
tsepez4cf55152016-11-02 14:37:54 -0700537 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700538}
539
tsepez4cf55152016-11-02 14:37:54 -0700540bool CFFL_FormFiller::IsDataChanged(CPDFSDK_PageView* pPageView) {
541 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700542}
543
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544void CFFL_FormFiller::SaveData(CPDFSDK_PageView* pPageView) {}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700545
Tom Sepez51da0932015-11-25 16:05:49 -0800546#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700547bool CFFL_FormFiller::IsFieldFull(CPDFSDK_PageView* pPageView) {
548 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700549}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800550#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700551
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552void CFFL_FormFiller::SetChangeMark() {
dsinclair8779fa82016-10-12 12:05:44 -0700553 m_pFormFillEnv->OnChange();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700554}
555
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700556void CFFL_FormFiller::GetActionData(CPDFSDK_PageView* pPageView,
557 CPDF_AAction::AActionType type,
Dan Sinclair7d125322018-03-28 18:49:34 +0000558 CPDFSDK_FieldAction& fa) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559 fa.sValue = m_pWidget->GetValue();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700560}
561
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562void CFFL_FormFiller::SetActionData(CPDFSDK_PageView* pPageView,
563 CPDF_AAction::AActionType type,
Dan Sinclair7d125322018-03-28 18:49:34 +0000564 const CPDFSDK_FieldAction& fa) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565
tsepez4cf55152016-11-02 14:37:54 -0700566bool CFFL_FormFiller::IsActionDataChanged(CPDF_AAction::AActionType type,
Dan Sinclair7d125322018-03-28 18:49:34 +0000567 const CPDFSDK_FieldAction& faOld,
568 const CPDFSDK_FieldAction& faNew) {
tsepez4cf55152016-11-02 14:37:54 -0700569 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700570}
571
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572void CFFL_FormFiller::SaveState(CPDFSDK_PageView* pPageView) {}
573
574void CFFL_FormFiller::RestoreState(CPDFSDK_PageView* pPageView) {}
575
576CPWL_Wnd* CFFL_FormFiller::ResetPDFWindow(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700577 bool bRestoreValue) {
578 return GetPDFWindow(pPageView, false);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700579}
580
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581void CFFL_FormFiller::TimerProc() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700582
dsinclairb9590102016-04-27 06:38:59 -0700583CFX_SystemHandler* CFFL_FormFiller::GetSystemHandler() const {
dsinclair8779fa82016-10-12 12:05:44 -0700584 return m_pFormFillEnv->GetSysHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700585}
586
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700587void CFFL_FormFiller::EscapeFiller(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700588 bool bDestroyPDFWindow) {
589 m_bValid = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590
Lei Zhangd6b72872019-02-21 21:33:53 +0000591 InvalidateRect(GetViewBBox(pPageView));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700592 if (bDestroyPDFWindow)
593 DestroyPDFWindow(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700594}
595
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500596void CFFL_FormFiller::InvalidateRect(const FX_RECT& rect) {
Tom Sepez101535f2018-06-12 13:36:05 +0000597 m_pFormFillEnv->Invalidate(m_pWidget->GetPage(), rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700598}