diff options
author | Ryan Harrison <rharrison@chromium.org> | 2018-01-12 18:36:30 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-01-12 18:36:30 +0000 |
commit | 9baf31f8c38e1c5266609e184cc07e369b744760 (patch) | |
tree | 37002a8148539b21f2562454b4864f0056f2aed2 /fpdfsdk/cpdfsdk_interform.cpp | |
parent | 72d34be06324624ee4dcf27b1f001ab573ddec06 (diff) | |
download | pdfium-9baf31f8c38e1c5266609e184cc07e369b744760.tar.xz |
Use enum for tracking form field types
Within PDFium use enum class for better type safety when working with
form field types. These values will still be converted to ints as part
of the public API, since that is the existing API.
This work is preperation for extending the number of form field types
to have more specific entries for XFA.
BUG=pdfium:952,chromium:763129,chromium:592758
Change-Id: Ie6c29f02ae22be782ff36eb87d27f1a4bf2c099e
Reviewed-on: https://pdfium-review.googlesource.com/22742
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Diffstat (limited to 'fpdfsdk/cpdfsdk_interform.cpp')
-rw-r--r-- | fpdfsdk/cpdfsdk_interform.cpp | 92 |
1 files changed, 52 insertions, 40 deletions
diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp index 4f0012d3ec..04ccc35d56 100644 --- a/fpdfsdk/cpdfsdk_interform.cpp +++ b/fpdfsdk/cpdfsdk_interform.cpp @@ -45,6 +45,20 @@ #include "xfa/fxfa/cxfa_ffwidgethandler.h" #endif // PDF_ENABLE_XFA +namespace { + +bool IsFormFieldTypeComboOrText(FormFieldType fieldType) { + switch (fieldType) { + case FormFieldType::kComboBox: + case FormFieldType::kTextField: + return true; + default: + return false; + } +} + +} // namespace + CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_FormFillEnvironment* pFormFillEnv) : m_pFormFillEnv(pFormFillEnv), m_pInterForm( @@ -55,10 +69,9 @@ CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_FormFillEnvironment* pFormFillEnv) #endif // PDF_ENABLE_XFA m_bCalculate(true), m_bBusy(false), - m_iHighlightAlpha(0) { + m_HighlightAlpha(0) { m_pInterForm->SetFormNotify(this); - for (int i = 0; i < kNumFieldTypes; ++i) - m_bNeedHightlight[i] = false; + RemoveAllHighLights(); } CPDFSDK_InterForm::~CPDFSDK_InterForm() { @@ -238,8 +251,8 @@ void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) { if (!pField) continue; - int nType = pField->GetFieldType(); - if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD) + FormFieldType fieldType = pField->GetFieldType(); + if (!IsFormFieldTypeComboOrText(fieldType)) continue; CPDF_AAction aAction = pField->GetAdditionalAction(); @@ -278,7 +291,7 @@ WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField, } IJS_Runtime* pRuntime = m_pFormFillEnv->GetJSRuntime(); - if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX && + if (pFormField->GetFieldType() == FormFieldType::kComboBox && pFormField->CountSelectedItems() > 0) { int index = pFormField->GetSelectedIndex(0); if (index >= 0) @@ -598,8 +611,8 @@ std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects( int CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField, const WideString& csValue) { - int nType = pField->GetFieldType(); - if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD) + FormFieldType fieldType = pField->GetFieldType(); + if (!IsFormFieldTypeComboOrText(fieldType)) return 0; if (!OnKeyStrokeCommit(pField, csValue)) @@ -615,8 +628,8 @@ void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) { #ifdef PDF_ENABLE_XFA SynchronizeField(pField, false); #endif // PDF_ENABLE_XFA - int nType = pField->GetFieldType(); - if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) { + FormFieldType fieldType = pField->GetFieldType(); + if (IsFormFieldTypeComboOrText(fieldType)) { OnCalculate(pField); bool bFormatted = false; WideString sValue = OnFormat(pField, bFormatted); @@ -627,7 +640,7 @@ void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) { int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField, const WideString& csValue) { - if (pField->GetFieldType() != FIELDTYPE_LISTBOX) + if (pField->GetFieldType() != FormFieldType::kListBox) return 0; if (!OnKeyStrokeCommit(pField, csValue)) @@ -640,7 +653,7 @@ int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField, } void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) { - if (pField->GetFieldType() != FIELDTYPE_LISTBOX) + if (pField->GetFieldType() != FormFieldType::kListBox) return; OnCalculate(pField); @@ -649,8 +662,9 @@ void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) { } void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) { - int nType = pField->GetFieldType(); - if (nType != FIELDTYPE_CHECKBOX && nType != FIELDTYPE_RADIOBUTTON) + FormFieldType fieldType = pField->GetFieldType(); + if (fieldType != FormFieldType::kCheckBox && + fieldType != FormFieldType::kRadioButton) return; OnCalculate(pField); @@ -673,40 +687,38 @@ void CPDFSDK_InterForm::AfterFormImportData(CPDF_InterForm* pForm) { OnCalculate(nullptr); } -bool CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) { - if (nFieldType < 1 || nFieldType > kNumFieldTypes) +bool CPDFSDK_InterForm::IsNeedHighLight(FormFieldType fieldType) { + if (fieldType == FormFieldType::kUnknown) return false; - return m_bNeedHightlight[nFieldType - 1]; + + return m_NeedsHighlight[static_cast<size_t>(fieldType)]; } -void CPDFSDK_InterForm::RemoveAllHighLight() { - for (int i = 0; i < kNumFieldTypes; ++i) - m_bNeedHightlight[i] = false; +void CPDFSDK_InterForm::RemoveAllHighLights() { + std::fill(m_HighlightColor, m_HighlightColor + kFormFieldTypeCount, + FXSYS_RGB(255, 255, 255)); + std::fill(m_NeedsHighlight, m_NeedsHighlight + kFormFieldTypeCount, false); } -void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) { - if (nFieldType < 0 || nFieldType > kNumFieldTypes) +void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, + FormFieldType fieldType) { + if (fieldType == FormFieldType::kUnknown) return; - switch (nFieldType) { - case 0: { - for (int i = 0; i < kNumFieldTypes; ++i) { - m_aHighlightColor[i] = clr; - m_bNeedHightlight[i] = true; - } - break; - } - default: { - m_aHighlightColor[nFieldType - 1] = clr; - m_bNeedHightlight[nFieldType - 1] = true; - break; - } + + m_HighlightColor[static_cast<size_t>(fieldType)] = clr; + m_NeedsHighlight[static_cast<size_t>(fieldType)] = true; +} + +void CPDFSDK_InterForm::SetAllHighlightColors(FX_COLORREF clr) { + for (auto type : kFormFieldTypes) { + m_HighlightColor[static_cast<size_t>(type)] = clr; + m_NeedsHighlight[static_cast<size_t>(type)] = true; } } -FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) { - if (nFieldType < 0 || nFieldType > kNumFieldTypes) +FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(FormFieldType fieldType) { + if (fieldType == FormFieldType::kUnknown) return FXSYS_RGB(255, 255, 255); - if (nFieldType == 0) - return m_aHighlightColor[0]; - return m_aHighlightColor[nFieldType - 1]; + + return m_HighlightColor[static_cast<size_t>(fieldType)]; } |