1 // 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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../include/formfiller/FormFiller.h"
8 #include "../../include/formfiller/FFL_FormFiller.h"
9 #include "../../include/formfiller/FFL_IFormFiller.h"
10 #include "../../include/formfiller/FFL_CBA_Fontmap.h"
11 #include "../../include/formfiller/FFL_ComboBox.h"
13 /* ------------------------------- CFFL_ComboBox -------------------------------
16 CFFL_ComboBox::CFFL_ComboBox(CPDFDoc_Environment* pApp, CPDFSDK_Annot* pAnnot)
17 : CFFL_FormFiller(pApp, pAnnot), m_pFontMap(NULL) {
23 CFFL_ComboBox::~CFFL_ComboBox() {
27 PWL_CREATEPARAM CFFL_ComboBox::GetCreateParam() {
28 PWL_CREATEPARAM cp = CFFL_FormFiller::GetCreateParam();
30 ASSERT(m_pWidget != NULL);
32 int nFlags = m_pWidget->GetFieldFlags();
34 if (nFlags & FIELDFLAG_EDIT) {
35 cp.dwFlags |= PCBS_ALLOWCUSTOMTEXT;
39 m_pFontMap = new CBA_FontMap(m_pWidget, GetSystemHandler());
40 m_pFontMap->Initial();
43 cp.pFontMap = m_pFontMap;
44 cp.pFocusHandler = this;
49 CPWL_Wnd* CFFL_ComboBox::NewPDFWindow(const PWL_CREATEPARAM& cp,
50 CPDFSDK_PageView* pPageView) {
51 CPWL_ComboBox* pWnd = new CPWL_ComboBox();
52 pWnd->AttachFFLData(this);
55 CFFL_IFormFiller* pFormFiller = m_pApp->GetIFormFiller();
56 pWnd->SetFillerNotify(pFormFiller);
58 int32_t nCurSel = m_pWidget->GetSelectedIndex(0);
59 CFX_WideString swText;
61 swText = m_pWidget->GetValue();
63 swText = m_pWidget->GetOptionLabel(nCurSel);
65 for (int32_t i = 0, sz = m_pWidget->CountOptions(); i < sz; i++) {
66 pWnd->AddString(m_pWidget->GetOptionLabel(i).c_str());
69 pWnd->SetSelect(nCurSel);
70 pWnd->SetText(swText.c_str());
74 FX_BOOL CFFL_ComboBox::OnChar(CPDFSDK_Annot* pAnnot,
77 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
80 FX_BOOL CFFL_ComboBox::IsDataChanged(CPDFSDK_PageView* pPageView) {
81 CPWL_ComboBox* pWnd = (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE);
85 int32_t nCurSel = pWnd->GetSelect();
86 if (!(m_pWidget->GetFieldFlags() & FIELDFLAG_EDIT))
87 return nCurSel != m_pWidget->GetSelectedIndex(0);
90 return nCurSel != m_pWidget->GetSelectedIndex(0);
92 return pWnd->GetText() != m_pWidget->GetValue();
95 void CFFL_ComboBox::SaveData(CPDFSDK_PageView* pPageView) {
96 ASSERT(m_pWidget != NULL);
98 if (CPWL_ComboBox* pWnd = (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE)) {
99 CFX_WideString swText = pWnd->GetText();
100 int32_t nCurSel = pWnd->GetSelect();
103 FX_BOOL bSetValue = TRUE;
105 if (m_pWidget->GetFieldFlags() & FIELDFLAG_EDIT) {
107 if (swText != m_pWidget->GetOptionLabel(nCurSel))
116 CFX_WideString sOldValue;
119 sOldValue = m_pWidget->GetValue();
120 m_pWidget->SetValue(swText, FALSE);
122 m_pWidget->GetSelectedIndex(0);
123 m_pWidget->SetOptionSelection(nCurSel, TRUE, FALSE);
126 m_pWidget->ResetFieldAppearance(TRUE);
127 m_pWidget->UpdateField();
130 m_pWidget->GetPDFPage();
134 void CFFL_ComboBox::GetActionData(CPDFSDK_PageView* pPageView,
135 CPDF_AAction::AActionType type,
136 PDFSDK_FieldAction& fa) {
138 case CPDF_AAction::KeyStroke:
139 if (CPWL_ComboBox* pComboBox =
140 (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE)) {
141 if (CPWL_Edit* pEdit = (CPWL_Edit*)*pComboBox) {
142 fa.bFieldFull = pEdit->IsTextFull();
145 pEdit->GetSel(nSelStart, nSelEnd);
146 fa.nSelEnd = nSelEnd;
147 fa.nSelStart = nSelStart;
148 fa.sValue = pEdit->GetText();
149 fa.sChangeEx = GetSelectExportText();
158 case CPDF_AAction::Validate:
159 if (CPWL_ComboBox* pComboBox =
160 (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE)) {
161 if (CPWL_Edit* pEdit = (CPWL_Edit*)*pComboBox) {
162 fa.sValue = pEdit->GetText();
166 case CPDF_AAction::LoseFocus:
167 case CPDF_AAction::GetFocus:
168 ASSERT(m_pWidget != NULL);
169 fa.sValue = m_pWidget->GetValue();
176 void CFFL_ComboBox::SetActionData(CPDFSDK_PageView* pPageView,
177 CPDF_AAction::AActionType type,
178 const PDFSDK_FieldAction& fa) {
180 case CPDF_AAction::KeyStroke:
181 if (CPWL_ComboBox* pComboBox =
182 (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE)) {
183 if (CPWL_Edit* pEdit = (CPWL_Edit*)*pComboBox) {
184 pEdit->SetSel(fa.nSelStart, fa.nSelEnd);
185 pEdit->ReplaceSel(fa.sChange.c_str());
194 FX_BOOL CFFL_ComboBox::IsActionDataChanged(CPDF_AAction::AActionType type,
195 const PDFSDK_FieldAction& faOld,
196 const PDFSDK_FieldAction& faNew) {
198 case CPDF_AAction::KeyStroke:
199 return (!faOld.bFieldFull && faOld.nSelEnd != faNew.nSelEnd) ||
200 faOld.nSelStart != faNew.nSelStart ||
201 faOld.sChange != faNew.sChange;
209 void CFFL_ComboBox::SaveState(CPDFSDK_PageView* pPageView) {
210 ASSERT(pPageView != NULL);
212 if (CPWL_ComboBox* pComboBox =
213 (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE)) {
214 m_State.nIndex = pComboBox->GetSelect();
216 if (CPWL_Edit* pEdit = (CPWL_Edit*)*pComboBox) {
217 pEdit->GetSel(m_State.nStart, m_State.nEnd);
218 m_State.sValue = pEdit->GetText();
223 void CFFL_ComboBox::RestoreState(CPDFSDK_PageView* pPageView) {
224 ASSERT(pPageView != NULL);
226 if (CPWL_ComboBox* pComboBox =
227 (CPWL_ComboBox*)GetPDFWindow(pPageView, TRUE)) {
228 if (m_State.nIndex >= 0)
229 pComboBox->SetSelect(m_State.nIndex);
231 if (CPWL_Edit* pEdit = (CPWL_Edit*)*pComboBox) {
232 pEdit->SetText(m_State.sValue.c_str());
233 pEdit->SetSel(m_State.nStart, m_State.nEnd);
239 CPWL_Wnd* CFFL_ComboBox::ResetPDFWindow(CPDFSDK_PageView* pPageView,
240 FX_BOOL bRestoreValue) {
242 SaveState(pPageView);
244 DestroyPDFWindow(pPageView);
246 CPWL_Wnd* pRet = NULL;
249 RestoreState(pPageView);
250 pRet = GetPDFWindow(pPageView, FALSE);
252 pRet = GetPDFWindow(pPageView, TRUE);
254 m_pWidget->UpdateField();
259 void CFFL_ComboBox::OnSetFocus(CPWL_Wnd* pWnd) {
260 ASSERT(m_pApp != NULL);
262 ASSERT(pWnd != NULL);
264 if (pWnd->GetClassName() == PWL_CLASSNAME_EDIT) {
265 CPWL_Edit* pEdit = (CPWL_Edit*)pWnd;
266 pEdit->SetCharSet(134);
267 pEdit->SetCodePage(936);
269 pEdit->SetReadyToInput();
270 CFX_WideString wsText = pEdit->GetText();
271 int nCharacters = wsText.GetLength();
272 CFX_ByteString bsUTFText = wsText.UTF16LE_Encode();
273 unsigned short* pBuffer = (unsigned short*)bsUTFText.c_str();
274 m_pApp->FFI_OnSetFieldInputFocus(m_pWidget->GetFormField(), pBuffer,
277 pEdit->SetEditNotify(this);
281 void CFFL_ComboBox::OnKillFocus(CPWL_Wnd* pWnd) {
282 ASSERT(m_pApp != NULL);
285 void CFFL_ComboBox::OnAddUndo(CPWL_Edit* pEdit) {
286 ASSERT(pEdit != NULL);
289 CFX_WideString CFFL_ComboBox::GetSelectExportText() {
290 CFX_WideString swRet;
293 CPDFSDK_PageView* pPageView = GetCurPageView();
294 if (CPWL_ComboBox* pComboBox =
295 (CPWL_ComboBox*)GetPDFWindow(pPageView, FALSE)) {
296 nExport = pComboBox->GetSelect();
300 if (CPDF_FormField* pFormField = m_pWidget->GetFormField()) {
301 swRet = pFormField->GetOptionValue(nExport);
303 swRet = pFormField->GetOptionLabel(nExport);