blob: 91db0959dbec7104f84c75a5dfbf24fddb500468 [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_textfield.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07008
dsinclair735606d2016-10-05 15:47:02 -07009#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070010#include "fpdfsdk/cpdfsdk_widget.h"
Dan Sinclairedbb3192016-03-21 09:08:24 -040011#include "fpdfsdk/formfiller/cba_fontmap.h"
dsinclair114e46a2016-09-29 17:18:21 -070012#include "fpdfsdk/fsdk_common.h"
tsepez36eb4bd2016-10-03 15:24:27 -070013#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
dsinclair735606d2016-10-05 15:47:02 -070015CFFL_TextField::CFFL_TextField(CPDFSDK_FormFillEnvironment* pApp,
16 CPDFSDK_Annot* pAnnot)
weili2d5b0202016-08-03 11:06:49 -070017 : CFFL_FormFiller(pApp, pAnnot) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019CFFL_TextField::~CFFL_TextField() {
Lei Zhangab5537d2016-01-06 14:58:14 -080020 for (const auto& it : m_Maps)
21 it.second->InvalidateFocusHandler(this);
dsinclair28a4a242016-08-22 13:36:02 -070022
23 // See comment in cffl_formfiller.h.
24 // The font map should be stored somewhere more appropriate so it will live
25 // until the PWL_Edit is done with it. pdfium:566
26 DestroyWindows();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027}
28
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029PWL_CREATEPARAM CFFL_TextField::GetCreateParam() {
30 PWL_CREATEPARAM cp = CFFL_FormFiller::GetCreateParam();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 int nFlags = m_pWidget->GetFieldFlags();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 if (nFlags & FIELDFLAG_PASSWORD) {
35 cp.dwFlags |= PES_PASSWORD;
36 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038 if (nFlags & FIELDFLAG_MULTILINE) {
39 cp.dwFlags |= PES_MULTILINE | PES_AUTORETURN | PES_TOP;
40
41 if (!(nFlags & FIELDFLAG_DONOTSCROLL)) {
42 cp.dwFlags |= PWS_VSCROLL | PES_AUTOSCROLL;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070043 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 } else {
45 cp.dwFlags |= PES_CENTER;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 if (!(nFlags & FIELDFLAG_DONOTSCROLL)) {
48 cp.dwFlags |= PES_AUTOSCROLL;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070049 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 if (nFlags & FIELDFLAG_COMB) {
53 cp.dwFlags |= PES_CHARARRAY;
54 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 if (nFlags & FIELDFLAG_RICHTEXT) {
57 cp.dwFlags |= PES_RICH;
58 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 cp.dwFlags |= PES_UNDO;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 switch (m_pWidget->GetAlignment()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -070063 default:
64 case BF_ALIGN_LEFT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 cp.dwFlags |= PES_LEFT;
66 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070067 case BF_ALIGN_MIDDLE:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 cp.dwFlags |= PES_MIDDLE;
69 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070070 case BF_ALIGN_RIGHT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 cp.dwFlags |= PES_RIGHT;
72 break;
73 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074
thestigd4c34f22016-09-28 17:04:51 -070075 if (!m_pFontMap) {
dsinclair8779fa82016-10-12 12:05:44 -070076 m_pFontMap = pdfium::MakeUnique<CBA_FontMap>(
77 m_pWidget, m_pFormFillEnv->GetSysHandler());
thestigd4c34f22016-09-28 17:04:51 -070078 }
weili2d5b0202016-08-03 11:06:49 -070079 cp.pFontMap = m_pFontMap.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 cp.pFocusHandler = this;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 return cp;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083}
84
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085CPWL_Wnd* CFFL_TextField::NewPDFWindow(const PWL_CREATEPARAM& cp,
86 CPDFSDK_PageView* pPageView) {
87 CPWL_Edit* pWnd = new CPWL_Edit();
88 pWnd->AttachFFLData(this);
89 pWnd->Create(cp);
dsinclair8779fa82016-10-12 12:05:44 -070090 pWnd->SetFillerNotify(m_pFormFillEnv->GetInteractiveFormFiller());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 int32_t nMaxLen = m_pWidget->GetMaxLen();
93 CFX_WideString swValue = m_pWidget->GetValue();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 if (nMaxLen > 0) {
96 if (pWnd->HasFlag(PES_CHARARRAY)) {
97 pWnd->SetCharArray(nMaxLen);
98 pWnd->SetAlignFormatV(PEAV_CENTER);
99 } else {
100 pWnd->SetLimitChar(nMaxLen);
101 }
102 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700103
tsepez067990c2016-09-13 06:46:40 -0700104 pWnd->SetText(swValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 return pWnd;
106}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700107
tsepez4cf55152016-11-02 14:37:54 -0700108bool CFFL_TextField::OnChar(CPDFSDK_Annot* pAnnot,
109 uint32_t nChar,
110 uint32_t nFlags) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 switch (nChar) {
112 case FWL_VKEY_Return:
113 if (!(m_pWidget->GetFieldFlags() & FIELDFLAG_MULTILINE)) {
dsinclair461eeaf2016-07-27 07:40:05 -0700114 CPDFSDK_PageView* pPageView = GetCurPageView(true);
Lei Zhang96660d62015-12-14 18:27:25 -0800115 ASSERT(pPageView);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 m_bValid = !m_bValid;
Dan Sinclair6eec1c42017-02-21 17:20:43 -0500117 m_pFormFillEnv->Invalidate(pAnnot->GetUnderlyingPage(),
118 pAnnot->GetRect().ToFxRect());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119
120 if (m_bValid) {
tsepez4cf55152016-11-02 14:37:54 -0700121 if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 pWnd->SetFocus();
123 } else {
124 if (CommitData(pPageView, nFlags)) {
125 DestroyPDFWindow(pPageView);
tsepez4cf55152016-11-02 14:37:54 -0700126 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 }
tsepez4cf55152016-11-02 14:37:54 -0700128 return false;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700129 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 }
131 break;
132 case FWL_VKEY_Escape: {
dsinclair461eeaf2016-07-27 07:40:05 -0700133 CPDFSDK_PageView* pPageView = GetCurPageView(true);
Lei Zhang96660d62015-12-14 18:27:25 -0800134 ASSERT(pPageView);
tsepez4cf55152016-11-02 14:37:54 -0700135 EscapeFiller(pPageView, true);
136 return true;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700137 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700139
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141}
142
tsepez4cf55152016-11-02 14:37:54 -0700143bool CFFL_TextField::IsDataChanged(CPDFSDK_PageView* pPageView) {
144 if (CPWL_Edit* pEdit = (CPWL_Edit*)GetPDFWindow(pPageView, false))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 return pEdit->GetText() != m_pWidget->GetValue();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700146
tsepez4cf55152016-11-02 14:37:54 -0700147 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700148}
149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150void CFFL_TextField::SaveData(CPDFSDK_PageView* pPageView) {
tsepez4cf55152016-11-02 14:37:54 -0700151 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 CFX_WideString sOldValue = m_pWidget->GetValue();
153 CFX_WideString sNewValue = pWnd->GetText();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154
tsepez4cf55152016-11-02 14:37:54 -0700155 m_pWidget->SetValue(sNewValue, false);
156 m_pWidget->ResetFieldAppearance(true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 m_pWidget->UpdateField();
158 SetChangeMark();
159 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160}
161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162void CFFL_TextField::GetActionData(CPDFSDK_PageView* pPageView,
163 CPDF_AAction::AActionType type,
164 PDFSDK_FieldAction& fa) {
165 switch (type) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700166 case CPDF_AAction::KeyStroke:
tsepez4cf55152016-11-02 14:37:54 -0700167 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 fa.bFieldFull = pWnd->IsTextFull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700169
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 fa.sValue = pWnd->GetText();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700171
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 if (fa.bFieldFull) {
173 fa.sChange = L"";
174 fa.sChangeEx = L"";
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700175 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 }
177 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700178 case CPDF_AAction::Validate:
tsepez4cf55152016-11-02 14:37:54 -0700179 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 fa.sValue = pWnd->GetText();
181 }
182 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700183 case CPDF_AAction::LoseFocus:
184 case CPDF_AAction::GetFocus:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 fa.sValue = m_pWidget->GetValue();
186 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700187 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 break;
189 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190}
191
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192void CFFL_TextField::SetActionData(CPDFSDK_PageView* pPageView,
193 CPDF_AAction::AActionType type,
194 const PDFSDK_FieldAction& fa) {
195 switch (type) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700196 case CPDF_AAction::KeyStroke:
tsepez4cf55152016-11-02 14:37:54 -0700197 if (CPWL_Edit* pEdit = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 pEdit->SetFocus();
199 pEdit->SetSel(fa.nSelStart, fa.nSelEnd);
tsepez067990c2016-09-13 06:46:40 -0700200 pEdit->ReplaceSel(fa.sChange);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 }
202 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700203 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 break;
205 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700206}
207
tsepez4cf55152016-11-02 14:37:54 -0700208bool CFFL_TextField::IsActionDataChanged(CPDF_AAction::AActionType type,
209 const PDFSDK_FieldAction& faOld,
210 const PDFSDK_FieldAction& faNew) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 switch (type) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700212 case CPDF_AAction::KeyStroke:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 return (!faOld.bFieldFull && faOld.nSelEnd != faNew.nSelEnd) ||
214 faOld.nSelStart != faNew.nSelStart ||
215 faOld.sChange != faNew.sChange;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700216 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 break;
218 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700219
tsepez4cf55152016-11-02 14:37:54 -0700220 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221}
222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223void CFFL_TextField::SaveState(CPDFSDK_PageView* pPageView) {
Lei Zhang96660d62015-12-14 18:27:25 -0800224 ASSERT(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225
tsepez4cf55152016-11-02 14:37:54 -0700226 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 pWnd->GetSel(m_State.nStart, m_State.nEnd);
228 m_State.sValue = pWnd->GetText();
229 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230}
231
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232void CFFL_TextField::RestoreState(CPDFSDK_PageView* pPageView) {
Lei Zhang96660d62015-12-14 18:27:25 -0800233 ASSERT(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234
tsepez4cf55152016-11-02 14:37:54 -0700235 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, true)) {
tsepez067990c2016-09-13 06:46:40 -0700236 pWnd->SetText(m_State.sValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 pWnd->SetSel(m_State.nStart, m_State.nEnd);
238 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239}
240
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241CPWL_Wnd* CFFL_TextField::ResetPDFWindow(CPDFSDK_PageView* pPageView,
tsepez4cf55152016-11-02 14:37:54 -0700242 bool bRestoreValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 if (bRestoreValue)
244 SaveState(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 DestroyPDFWindow(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
thestig1cd352e2016-06-07 17:53:06 -0700248 CPWL_Wnd* pRet = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 if (bRestoreValue) {
251 RestoreState(pPageView);
tsepez4cf55152016-11-02 14:37:54 -0700252 pRet = GetPDFWindow(pPageView, false);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800253 } else {
tsepez4cf55152016-11-02 14:37:54 -0700254 pRet = GetPDFWindow(pPageView, true);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800255 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 m_pWidget->UpdateField();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700258
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 return pRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260}
261
Tom Sepez51da0932015-11-25 16:05:49 -0800262#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700263bool CFFL_TextField::IsFieldFull(CPDFSDK_PageView* pPageView) {
264 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 return pWnd->IsTextFull();
266 }
Bo Xufdc00a72014-10-28 23:03:33 -0700267
tsepez4cf55152016-11-02 14:37:54 -0700268 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700269}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800270#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272void CFFL_TextField::OnSetFocus(CPWL_Wnd* pWnd) {
dsinclair8779fa82016-10-12 12:05:44 -0700273 ASSERT(m_pFormFillEnv);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 if (pWnd->GetClassName() == PWL_CLASSNAME_EDIT) {
275 CPWL_Edit* pEdit = (CPWL_Edit*)pWnd;
thestig907a5222016-06-21 14:38:27 -0700276 pEdit->SetCharSet(FXFONT_GB2312_CHARSET);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 pEdit->SetCodePage(936);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 pEdit->SetReadyToInput();
280 CFX_WideString wsText = pEdit->GetText();
281 int nCharacters = wsText.GetLength();
282 CFX_ByteString bsUTFText = wsText.UTF16LE_Encode();
283 unsigned short* pBuffer = (unsigned short*)bsUTFText.c_str();
tsepez4cf55152016-11-02 14:37:54 -0700284 m_pFormFillEnv->OnSetFieldInputFocus(pBuffer, nCharacters, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286}