blob: 369bb4d2092c562eeec3e7d639e6cc15650fe42e [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
Dan Sinclairedbb3192016-03-21 09:08:24 -04009#include "fpdfsdk/formfiller/cba_fontmap.h"
jaepark611adb82016-08-17 11:34:36 -070010#include "fpdfsdk/include/cpdfsdk_widget.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -080011#include "fpdfsdk/include/fsdk_common.h"
12#include "fpdfsdk/include/fsdk_mgr.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013
Nico Weber9d8ec5a2015-08-04 13:00:21 -070014CFFL_TextField::CFFL_TextField(CPDFDoc_Environment* pApp, CPDFSDK_Annot* pAnnot)
weili2d5b0202016-08-03 11:06:49 -070015 : CFFL_FormFiller(pApp, pAnnot) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
Nico Weber9d8ec5a2015-08-04 13:00:21 -070017CFFL_TextField::~CFFL_TextField() {
Lei Zhangab5537d2016-01-06 14:58:14 -080018 for (const auto& it : m_Maps)
19 it.second->InvalidateFocusHandler(this);
dsinclair28a4a242016-08-22 13:36:02 -070020
21 // See comment in cffl_formfiller.h.
22 // The font map should be stored somewhere more appropriate so it will live
23 // until the PWL_Edit is done with it. pdfium:566
24 DestroyWindows();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025}
26
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027PWL_CREATEPARAM CFFL_TextField::GetCreateParam() {
28 PWL_CREATEPARAM cp = CFFL_FormFiller::GetCreateParam();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 int nFlags = m_pWidget->GetFieldFlags();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 if (nFlags & FIELDFLAG_PASSWORD) {
33 cp.dwFlags |= PES_PASSWORD;
34 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 if (nFlags & FIELDFLAG_MULTILINE) {
37 cp.dwFlags |= PES_MULTILINE | PES_AUTORETURN | PES_TOP;
38
39 if (!(nFlags & FIELDFLAG_DONOTSCROLL)) {
40 cp.dwFlags |= PWS_VSCROLL | PES_AUTOSCROLL;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070041 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 } else {
43 cp.dwFlags |= PES_CENTER;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 if (!(nFlags & FIELDFLAG_DONOTSCROLL)) {
46 cp.dwFlags |= PES_AUTOSCROLL;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070047 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 if (nFlags & FIELDFLAG_COMB) {
51 cp.dwFlags |= PES_CHARARRAY;
52 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 if (nFlags & FIELDFLAG_RICHTEXT) {
55 cp.dwFlags |= PES_RICH;
56 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 cp.dwFlags |= PES_UNDO;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 switch (m_pWidget->GetAlignment()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -070061 default:
62 case BF_ALIGN_LEFT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 cp.dwFlags |= PES_LEFT;
64 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070065 case BF_ALIGN_MIDDLE:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 cp.dwFlags |= PES_MIDDLE;
67 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -070068 case BF_ALIGN_RIGHT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 cp.dwFlags |= PES_RIGHT;
70 break;
71 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070072
Lei Zhangfcfa3b82015-12-24 21:07:28 -080073 if (!m_pFontMap)
weili2d5b0202016-08-03 11:06:49 -070074 m_pFontMap.reset(new CBA_FontMap(m_pWidget, m_pApp->GetSysHandler()));
75 cp.pFontMap = m_pFontMap.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 cp.pFocusHandler = this;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 return cp;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079}
80
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081CPWL_Wnd* CFFL_TextField::NewPDFWindow(const PWL_CREATEPARAM& cp,
82 CPDFSDK_PageView* pPageView) {
83 CPWL_Edit* pWnd = new CPWL_Edit();
84 pWnd->AttachFFLData(this);
85 pWnd->Create(cp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 CFFL_IFormFiller* pIFormFiller = m_pApp->GetIFormFiller();
88 pWnd->SetFillerNotify(pIFormFiller);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 int32_t nMaxLen = m_pWidget->GetMaxLen();
91 CFX_WideString swValue = m_pWidget->GetValue();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 if (nMaxLen > 0) {
94 if (pWnd->HasFlag(PES_CHARARRAY)) {
95 pWnd->SetCharArray(nMaxLen);
96 pWnd->SetAlignFormatV(PEAV_CENTER);
97 } else {
98 pWnd->SetLimitChar(nMaxLen);
99 }
100 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
tsepez067990c2016-09-13 06:46:40 -0700102 pWnd->SetText(swValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 return pWnd;
104}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106FX_BOOL CFFL_TextField::OnChar(CPDFSDK_Annot* pAnnot,
107 FX_UINT nChar,
108 FX_UINT nFlags) {
109 switch (nChar) {
110 case FWL_VKEY_Return:
111 if (!(m_pWidget->GetFieldFlags() & FIELDFLAG_MULTILINE)) {
dsinclair461eeaf2016-07-27 07:40:05 -0700112 CPDFSDK_PageView* pPageView = GetCurPageView(true);
Lei Zhang96660d62015-12-14 18:27:25 -0800113 ASSERT(pPageView);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 m_bValid = !m_bValid;
Tom Sepez281a9ea2016-02-26 14:24:28 -0800115 CFX_FloatRect rcAnnot = pAnnot->GetRect();
Tom Sepez1b246282015-11-25 15:15:31 -0800116 m_pApp->FFI_Invalidate(pAnnot->GetUnderlyingPage(), rcAnnot.left,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 rcAnnot.top, rcAnnot.right, rcAnnot.bottom);
118
119 if (m_bValid) {
120 if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, TRUE))
121 pWnd->SetFocus();
122 } else {
123 if (CommitData(pPageView, nFlags)) {
124 DestroyPDFWindow(pPageView);
125 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 }
Tom Sepez468e5892015-10-13 15:49:36 -0700127 return FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700128 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 }
130 break;
131 case FWL_VKEY_Escape: {
dsinclair461eeaf2016-07-27 07:40:05 -0700132 CPDFSDK_PageView* pPageView = GetCurPageView(true);
Lei Zhang96660d62015-12-14 18:27:25 -0800133 ASSERT(pPageView);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 EscapeFiller(pPageView, TRUE);
135 return TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700136 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 }
Tom Sepez4f7bc042015-04-27 12:06:58 -0700138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140}
141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142FX_BOOL CFFL_TextField::IsDataChanged(CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143 if (CPWL_Edit* pEdit = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE))
144 return pEdit->GetText() != m_pWidget->GetValue();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147}
148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149void CFFL_TextField::SaveData(CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE)) {
151 CFX_WideString sOldValue = m_pWidget->GetValue();
152 CFX_WideString sNewValue = pWnd->GetText();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 m_pWidget->SetValue(sNewValue, FALSE);
155 m_pWidget->ResetFieldAppearance(TRUE);
156 m_pWidget->UpdateField();
157 SetChangeMark();
158 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159}
160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161void CFFL_TextField::GetActionData(CPDFSDK_PageView* pPageView,
162 CPDF_AAction::AActionType type,
163 PDFSDK_FieldAction& fa) {
164 switch (type) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700165 case CPDF_AAction::KeyStroke:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE)) {
167 fa.bFieldFull = pWnd->IsTextFull();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 fa.sValue = pWnd->GetText();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 if (fa.bFieldFull) {
172 fa.sChange = L"";
173 fa.sChangeEx = L"";
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700174 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 }
176 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700177 case CPDF_AAction::Validate:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE)) {
179 fa.sValue = pWnd->GetText();
180 }
181 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700182 case CPDF_AAction::LoseFocus:
183 case CPDF_AAction::GetFocus:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 fa.sValue = m_pWidget->GetValue();
185 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700186 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 break;
188 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189}
190
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191void CFFL_TextField::SetActionData(CPDFSDK_PageView* pPageView,
192 CPDF_AAction::AActionType type,
193 const PDFSDK_FieldAction& fa) {
194 switch (type) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700195 case CPDF_AAction::KeyStroke:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 if (CPWL_Edit* pEdit = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE)) {
197 pEdit->SetFocus();
198 pEdit->SetSel(fa.nSelStart, fa.nSelEnd);
tsepez067990c2016-09-13 06:46:40 -0700199 pEdit->ReplaceSel(fa.sChange);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 }
201 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700202 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 break;
204 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205}
206
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207FX_BOOL CFFL_TextField::IsActionDataChanged(CPDF_AAction::AActionType type,
208 const PDFSDK_FieldAction& faOld,
209 const PDFSDK_FieldAction& faNew) {
210 switch (type) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700211 case CPDF_AAction::KeyStroke:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 return (!faOld.bFieldFull && faOld.nSelEnd != faNew.nSelEnd) ||
213 faOld.nSelStart != faNew.nSelStart ||
214 faOld.sChange != faNew.sChange;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700215 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 break;
217 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220}
221
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222void CFFL_TextField::SaveState(CPDFSDK_PageView* pPageView) {
Lei Zhang96660d62015-12-14 18:27:25 -0800223 ASSERT(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE)) {
226 pWnd->GetSel(m_State.nStart, m_State.nEnd);
227 m_State.sValue = pWnd->GetText();
228 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229}
230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231void CFFL_TextField::RestoreState(CPDFSDK_PageView* pPageView) {
Lei Zhang96660d62015-12-14 18:27:25 -0800232 ASSERT(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, TRUE)) {
tsepez067990c2016-09-13 06:46:40 -0700235 pWnd->SetText(m_State.sValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 pWnd->SetSel(m_State.nStart, m_State.nEnd);
237 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238}
239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240CPWL_Wnd* CFFL_TextField::ResetPDFWindow(CPDFSDK_PageView* pPageView,
241 FX_BOOL bRestoreValue) {
242 if (bRestoreValue)
243 SaveState(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 DestroyPDFWindow(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
thestig1cd352e2016-06-07 17:53:06 -0700247 CPWL_Wnd* pRet = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 if (bRestoreValue) {
250 RestoreState(pPageView);
251 pRet = GetPDFWindow(pPageView, FALSE);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800252 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 pRet = GetPDFWindow(pPageView, TRUE);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800254 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 m_pWidget->UpdateField();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700257
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 return pRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259}
260
Tom Sepez51da0932015-11-25 16:05:49 -0800261#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262FX_BOOL CFFL_TextField::IsFieldFull(CPDFSDK_PageView* pPageView) {
263 if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, FALSE)) {
264 return pWnd->IsTextFull();
265 }
Bo Xufdc00a72014-10-28 23:03:33 -0700266
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700268}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800269#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271void CFFL_TextField::OnSetFocus(CPWL_Wnd* pWnd) {
Lei Zhang96660d62015-12-14 18:27:25 -0800272 ASSERT(m_pApp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 if (pWnd->GetClassName() == PWL_CLASSNAME_EDIT) {
274 CPWL_Edit* pEdit = (CPWL_Edit*)pWnd;
thestig907a5222016-06-21 14:38:27 -0700275 pEdit->SetCharSet(FXFONT_GB2312_CHARSET);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 pEdit->SetCodePage(936);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 pEdit->SetReadyToInput();
279 CFX_WideString wsText = pEdit->GetText();
280 int nCharacters = wsText.GetLength();
281 CFX_ByteString bsUTFText = wsText.UTF16LE_Encode();
282 unsigned short* pBuffer = (unsigned short*)bsUTFText.c_str();
283 m_pApp->FFI_OnSetFieldInputFocus(m_pWidget->GetFormField(), pBuffer,
284 nCharacters, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286}