blob: 262c485138a8d5719c60cc3cdb70e87a753ec4b8 [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_combobox.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
Dan Sinclairedbb3192016-03-21 09:08:24 -04009#include "fpdfsdk/formfiller/cba_fontmap.h"
10#include "fpdfsdk/formfiller/cffl_formfiller.h"
11#include "fpdfsdk/formfiller/cffl_iformfiller.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -080012#include "fpdfsdk/include/fsdk_common.h"
13#include "fpdfsdk/include/fsdk_mgr.h"
dan sinclair89e904b2016-03-23 19:29:15 -040014#include "fpdfsdk/pdfwindow/PWL_ComboBox.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016CFFL_ComboBox::CFFL_ComboBox(CPDFDoc_Environment* pApp, CPDFSDK_Annot* pAnnot)
thestig1cd352e2016-06-07 17:53:06 -070017 : CFFL_FormFiller(pApp, pAnnot), m_pFontMap(nullptr) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070018 m_State.nIndex = 0;
19 m_State.nStart = 0;
20 m_State.nEnd = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021}
22
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023CFFL_ComboBox::~CFFL_ComboBox() {
Lei Zhangab5537d2016-01-06 14:58:14 -080024 for (const auto& it : m_Maps)
25 it.second->InvalidateFocusHandler(this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 delete m_pFontMap;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027}
28
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029PWL_CREATEPARAM CFFL_ComboBox::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();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 if (nFlags & FIELDFLAG_EDIT) {
34 cp.dwFlags |= PCBS_ALLOWCUSTOMTEXT;
35 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
Lei Zhangfcfa3b82015-12-24 21:07:28 -080037 if (!m_pFontMap)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038 m_pFontMap = new CBA_FontMap(m_pWidget, GetSystemHandler());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 cp.pFontMap = m_pFontMap;
40 cp.pFocusHandler = this;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 return cp;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043}
44
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045CPWL_Wnd* CFFL_ComboBox::NewPDFWindow(const PWL_CREATEPARAM& cp,
46 CPDFSDK_PageView* pPageView) {
47 CPWL_ComboBox* pWnd = new CPWL_ComboBox();
48 pWnd->AttachFFLData(this);
49 pWnd->Create(cp);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 CFFL_IFormFiller* pFormFiller = m_pApp->GetIFormFiller();
52 pWnd->SetFillerNotify(pFormFiller);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 int32_t nCurSel = m_pWidget->GetSelectedIndex(0);
55 CFX_WideString swText;
56 if (nCurSel < 0)
57 swText = m_pWidget->GetValue();
58 else
59 swText = m_pWidget->GetOptionLabel(nCurSel);
Tom Sepez4f7bc042015-04-27 12:06:58 -070060
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 for (int32_t i = 0, sz = m_pWidget->CountOptions(); i < sz; i++) {
62 pWnd->AddString(m_pWidget->GetOptionLabel(i).c_str());
63 }
Tom Sepez4f7bc042015-04-27 12:06:58 -070064
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 pWnd->SetSelect(nCurSel);
66 pWnd->SetText(swText.c_str());
67 return pWnd;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068}
69
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070FX_BOOL CFFL_ComboBox::OnChar(CPDFSDK_Annot* pAnnot,
71 FX_UINT nChar,
72 FX_UINT nFlags) {
73 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074}
75
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076FX_BOOL CFFL_ComboBox::IsDataChanged(CPDFSDK_PageView* pPageView) {
77 CPWL_ComboBox* pWnd = (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE);
78 if (!pWnd)
79 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 int32_t nCurSel = pWnd->GetSelect();
82 if (!(m_pWidget->GetFieldFlags() & FIELDFLAG_EDIT))
83 return nCurSel != m_pWidget->GetSelectedIndex(0);
84
85 if (nCurSel >= 0)
86 return nCurSel != m_pWidget->GetSelectedIndex(0);
87
88 return pWnd->GetText() != m_pWidget->GetValue();
89}
90
91void CFFL_ComboBox::SaveData(CPDFSDK_PageView* pPageView) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -080092 CPWL_ComboBox* pWnd =
93 static_cast<CPWL_ComboBox*>(GetPDFWindow(pPageView, FALSE));
94 if (!pWnd)
95 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096
Lei Zhangc2fb35f2016-01-05 16:46:58 -080097 CFX_WideString swText = pWnd->GetText();
98 int32_t nCurSel = pWnd->GetSelect();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070099
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800100 bool bSetValue = false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800102 if (m_pWidget->GetFieldFlags() & FIELDFLAG_EDIT)
103 bSetValue = (nCurSel < 0) || (swText != m_pWidget->GetOptionLabel(nCurSel));
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700104
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800105 if (bSetValue) {
106 m_pWidget->SetValue(swText, FALSE);
107 } else {
108 m_pWidget->GetSelectedIndex(0);
109 m_pWidget->SetOptionSelection(nCurSel, TRUE, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 }
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800111
112 m_pWidget->ResetFieldAppearance(TRUE);
113 m_pWidget->UpdateField();
114 SetChangeMark();
115
116 m_pWidget->GetPDFPage();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117}
118
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119void CFFL_ComboBox::GetActionData(CPDFSDK_PageView* pPageView,
120 CPDF_AAction::AActionType type,
121 PDFSDK_FieldAction& fa) {
122 switch (type) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700123 case CPDF_AAction::KeyStroke:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 if (CPWL_ComboBox* pComboBox =
Tom Sepezb4f00292015-11-23 09:13:33 -0800125 static_cast<CPWL_ComboBox*>(GetPDFWindow(pPageView, FALSE))) {
126 if (CPWL_Edit* pEdit = pComboBox->GetEdit()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 fa.bFieldFull = pEdit->IsTextFull();
128 int nSelStart = 0;
129 int nSelEnd = 0;
130 pEdit->GetSel(nSelStart, nSelEnd);
131 fa.nSelEnd = nSelEnd;
132 fa.nSelStart = nSelStart;
133 fa.sValue = pEdit->GetText();
134 fa.sChangeEx = GetSelectExportText();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 if (fa.bFieldFull) {
137 fa.sChange = L"";
138 fa.sChangeEx = L"";
139 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700140 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 }
142 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700143 case CPDF_AAction::Validate:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 if (CPWL_ComboBox* pComboBox =
Tom Sepezb4f00292015-11-23 09:13:33 -0800145 static_cast<CPWL_ComboBox*>(GetPDFWindow(pPageView, FALSE))) {
146 if (CPWL_Edit* pEdit = pComboBox->GetEdit()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 fa.sValue = pEdit->GetText();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700148 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 }
150 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700151 case CPDF_AAction::LoseFocus:
152 case CPDF_AAction::GetFocus:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 fa.sValue = m_pWidget->GetValue();
154 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700155 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 break;
157 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700158}
159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160void CFFL_ComboBox::SetActionData(CPDFSDK_PageView* pPageView,
161 CPDF_AAction::AActionType type,
162 const PDFSDK_FieldAction& fa) {
163 switch (type) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700164 case CPDF_AAction::KeyStroke:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 if (CPWL_ComboBox* pComboBox =
Tom Sepezb4f00292015-11-23 09:13:33 -0800166 static_cast<CPWL_ComboBox*>(GetPDFWindow(pPageView, FALSE))) {
167 if (CPWL_Edit* pEdit = pComboBox->GetEdit()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 pEdit->SetSel(fa.nSelStart, fa.nSelEnd);
169 pEdit->ReplaceSel(fa.sChange.c_str());
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700170 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 }
172 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700173 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 break;
175 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176}
177
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178FX_BOOL CFFL_ComboBox::IsActionDataChanged(CPDF_AAction::AActionType type,
179 const PDFSDK_FieldAction& faOld,
180 const PDFSDK_FieldAction& faNew) {
181 switch (type) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700182 case CPDF_AAction::KeyStroke:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 return (!faOld.bFieldFull && faOld.nSelEnd != faNew.nSelEnd) ||
184 faOld.nSelStart != faNew.nSelStart ||
185 faOld.sChange != faNew.sChange;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700186 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 break;
188 }
189
190 return FALSE;
191}
192
193void CFFL_ComboBox::SaveState(CPDFSDK_PageView* pPageView) {
Lei Zhang96660d62015-12-14 18:27:25 -0800194 ASSERT(pPageView);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195
196 if (CPWL_ComboBox* pComboBox =
Tom Sepezb4f00292015-11-23 09:13:33 -0800197 static_cast<CPWL_ComboBox*>(GetPDFWindow(pPageView, FALSE))) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 m_State.nIndex = pComboBox->GetSelect();
199
Tom Sepezb4f00292015-11-23 09:13:33 -0800200 if (CPWL_Edit* pEdit = pComboBox->GetEdit()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 pEdit->GetSel(m_State.nStart, m_State.nEnd);
202 m_State.sValue = pEdit->GetText();
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700203 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205}
206
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207void CFFL_ComboBox::RestoreState(CPDFSDK_PageView* pPageView) {
Lei Zhang96660d62015-12-14 18:27:25 -0800208 ASSERT(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 if (CPWL_ComboBox* pComboBox =
Tom Sepezb4f00292015-11-23 09:13:33 -0800211 static_cast<CPWL_ComboBox*>(GetPDFWindow(pPageView, TRUE))) {
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800212 if (m_State.nIndex >= 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 pComboBox->SetSelect(m_State.nIndex);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800214 } else {
Tom Sepezb4f00292015-11-23 09:13:33 -0800215 if (CPWL_Edit* pEdit = pComboBox->GetEdit()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 pEdit->SetText(m_State.sValue.c_str());
217 pEdit->SetSel(m_State.nStart, m_State.nEnd);
218 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700219 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221}
222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223CPWL_Wnd* CFFL_ComboBox::ResetPDFWindow(CPDFSDK_PageView* pPageView,
224 FX_BOOL bRestoreValue) {
225 if (bRestoreValue)
226 SaveState(pPageView);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 DestroyPDFWindow(pPageView);
229
thestig1cd352e2016-06-07 17:53:06 -0700230 CPWL_Wnd* pRet = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231
232 if (bRestoreValue) {
233 RestoreState(pPageView);
234 pRet = GetPDFWindow(pPageView, FALSE);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800235 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 pRet = GetPDFWindow(pPageView, TRUE);
Lei Zhangc2fb35f2016-01-05 16:46:58 -0800237 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238
239 m_pWidget->UpdateField();
240
241 return pRet;
242}
243
Tom Sepez51da0932015-11-25 16:05:49 -0800244#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245FX_BOOL CFFL_ComboBox::IsFieldFull(CPDFSDK_PageView* pPageView) {
246 if (CPWL_ComboBox* pComboBox =
Tom Sepezb4f00292015-11-23 09:13:33 -0800247 static_cast<CPWL_ComboBox*>(GetPDFWindow(pPageView, FALSE))) {
248 if (CPWL_Edit* pEdit = pComboBox->GetEdit())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 return pEdit->IsTextFull();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800253#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255void CFFL_ComboBox::OnSetFocus(CPWL_Wnd* pWnd) {
Lei Zhang96660d62015-12-14 18:27:25 -0800256 ASSERT(m_pApp);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700257
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 if (pWnd->GetClassName() == PWL_CLASSNAME_EDIT) {
259 CPWL_Edit* pEdit = (CPWL_Edit*)pWnd;
thestig907a5222016-06-21 14:38:27 -0700260 pEdit->SetCharSet(FXFONT_GB2312_CHARSET);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 pEdit->SetCodePage(936);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 pEdit->SetReadyToInput();
264 CFX_WideString wsText = pEdit->GetText();
265 int nCharacters = wsText.GetLength();
266 CFX_ByteString bsUTFText = wsText.UTF16LE_Encode();
267 unsigned short* pBuffer = (unsigned short*)bsUTFText.c_str();
268 m_pApp->FFI_OnSetFieldInputFocus(m_pWidget->GetFormField(), pBuffer,
269 nCharacters, TRUE);
270
271 pEdit->SetEditNotify(this);
272 }
273}
274
275void CFFL_ComboBox::OnKillFocus(CPWL_Wnd* pWnd) {
Lei Zhang96660d62015-12-14 18:27:25 -0800276 ASSERT(m_pApp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277}
278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279void CFFL_ComboBox::OnAddUndo(CPWL_Edit* pEdit) {
Lei Zhang96660d62015-12-14 18:27:25 -0800280 ASSERT(pEdit);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281}
282
283CFX_WideString CFFL_ComboBox::GetSelectExportText() {
284 CFX_WideString swRet;
285
286 int nExport = -1;
287 CPDFSDK_PageView* pPageView = GetCurPageView();
288 if (CPWL_ComboBox* pComboBox =
289 (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE)) {
290 nExport = pComboBox->GetSelect();
291 }
292
293 if (nExport >= 0) {
294 if (CPDF_FormField* pFormField = m_pWidget->GetFormField()) {
295 swRet = pFormField->GetOptionValue(nExport);
296 if (swRet.IsEmpty())
297 swRet = pFormField->GetOptionLabel(nExport);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700298 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700300
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 return swRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700302}