From 9baf31f8c38e1c5266609e184cc07e369b744760 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Fri, 12 Jan 2018 18:36:30 +0000 Subject: 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 Reviewed-by: Henrique Nakashima --- core/fpdfdoc/cpdf_formfield.cpp | 27 ++++++++++++++++---------- core/fpdfdoc/cpdf_formfield.h | 42 ++++++++++++++++++++++++++++++++--------- core/fpdfdoc/cpdf_interform.cpp | 16 ++++++++++------ 3 files changed, 60 insertions(+), 25 deletions(-) (limited to 'core') diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp index 2502fe6faa..df59369723 100644 --- a/core/fpdfdoc/cpdf_formfield.cpp +++ b/core/fpdfdoc/cpdf_formfield.cpp @@ -45,6 +45,14 @@ bool IsUnison(CPDF_FormField* pField) { } // namespace +Optional IntToFormFieldType(int value) { + if (value >= static_cast(FormFieldType::kUnknown) && + value < static_cast(kFormFieldTypeCount)) { + return {static_cast(value)}; + } + return {}; +} + CPDF_Object* FPDF_GetFieldAttr(const CPDF_Dictionary* pFieldDict, const char* name, int nLevel) { @@ -238,28 +246,27 @@ int CPDF_FormField::GetControlIndex(const CPDF_FormControl* pControl) const { return it != m_ControlList.end() ? it - m_ControlList.begin() : -1; } -int CPDF_FormField::GetFieldType() const { +FormFieldType CPDF_FormField::GetFieldType() const { switch (m_Type) { case PushButton: - return FIELDTYPE_PUSHBUTTON; + return FormFieldType::kPushButton; case CheckBox: - return FIELDTYPE_CHECKBOX; + return FormFieldType::kCheckBox; case RadioButton: - return FIELDTYPE_RADIOBUTTON; + return FormFieldType::kRadioButton; case ComboBox: - return FIELDTYPE_COMBOBOX; + return FormFieldType::kComboBox; case ListBox: - return FIELDTYPE_LISTBOX; + return FormFieldType::kListBox; case Text: case RichText: case File: - return FIELDTYPE_TEXTFIELD; + return FormFieldType::kTextField; case Sign: - return FIELDTYPE_SIGNATURE; + return FormFieldType::kSignature; default: - break; + return FormFieldType::kUnknown; } - return FIELDTYPE_UNKNOWN; } CPDF_AAction CPDF_FormField::GetAdditionalAction() const { diff --git a/core/fpdfdoc/cpdf_formfield.h b/core/fpdfdoc/cpdf_formfield.h index db1c514969..1f1205dabe 100644 --- a/core/fpdfdoc/cpdf_formfield.h +++ b/core/fpdfdoc/cpdf_formfield.h @@ -18,14 +18,38 @@ #include "core/fxcrt/unowned_ptr.h" #include "third_party/base/stl_util.h" -#define FIELDTYPE_UNKNOWN 0 -#define FIELDTYPE_PUSHBUTTON 1 -#define FIELDTYPE_CHECKBOX 2 -#define FIELDTYPE_RADIOBUTTON 3 -#define FIELDTYPE_COMBOBOX 4 -#define FIELDTYPE_LISTBOX 5 -#define FIELDTYPE_TEXTFIELD 6 -#define FIELDTYPE_SIGNATURE 7 +enum class FormFieldType : uint8_t { + kUnknown = 0, + kPushButton = 1, + kCheckBox = 2, + kRadioButton = 3, + kComboBox = 4, + kListBox = 5, + kTextField = 6, + kSignature = 7, +#ifdef PDF_ENABLE_XFA + kXFA = 8, // Generic XFA field. +#endif // PDF_ENABLE_XFA +}; + +Optional IntToFormFieldType(int value); + +// If values are added to FormFieldType, these will need to be updated. +#ifdef PDF_ENABLE_XFA +constexpr size_t kFormFieldTypeCount = 9; +#else +constexpr size_t kFormFieldTypeCount = 8; +#endif // PDF_ENABLE_XFA + +constexpr FormFieldType kFormFieldTypes[kFormFieldTypeCount] = { + FormFieldType::kUnknown, FormFieldType::kPushButton, + FormFieldType::kCheckBox, FormFieldType::kRadioButton, + FormFieldType::kComboBox, FormFieldType::kListBox, + FormFieldType::kTextField, FormFieldType::kSignature, +#ifdef PDF_ENABLE_XFA + FormFieldType::kXFA, +#endif // PDF_ENABLE_XFA +}; #define FORMFLAG_READONLY 0x01 #define FORMFLAG_REQUIRED 0x02 @@ -79,7 +103,7 @@ class CPDF_FormField { } int GetControlIndex(const CPDF_FormControl* pControl) const; - int GetFieldType() const; + FormFieldType GetFieldType() const; CPDF_AAction GetAdditionalAction() const; WideString GetAlternateName() const; diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp index 281d4d33fc..1596b17d82 100644 --- a/core/fpdfdoc/cpdf_interform.cpp +++ b/core/fpdfdoc/cpdf_interform.cpp @@ -1206,12 +1206,13 @@ void CPDF_InterForm::FDF_ImportField(CPDF_Dictionary* pFieldDict, return; WideString csWValue = GetFieldValue(*pFieldDict, m_bsEncoding); - int iType = pField->GetFieldType(); + FormFieldType fieldType = pField->GetFieldType(); if (bNotify && m_pFormNotify) { - if (iType == FIELDTYPE_LISTBOX) { + if (fieldType == FormFieldType::kListBox) { if (m_pFormNotify->BeforeSelectionChange(pField, csWValue) < 0) return; - } else if (iType == FIELDTYPE_COMBOBOX || iType == FIELDTYPE_TEXTFIELD) { + } else if (fieldType == FormFieldType::kComboBox || + fieldType == FormFieldType::kTextField) { if (m_pFormNotify->BeforeValueChange(pField, csWValue) < 0) return; } @@ -1224,12 +1225,15 @@ void CPDF_InterForm::FDF_ImportField(CPDF_Dictionary* pFieldDict, } if (bNotify && m_pFormNotify) { - if (iType == FIELDTYPE_CHECKBOX || iType == FIELDTYPE_RADIOBUTTON) + if (fieldType == FormFieldType::kCheckBox || + fieldType == FormFieldType::kRadioButton) { m_pFormNotify->AfterCheckedStatusChange(pField); - else if (iType == FIELDTYPE_LISTBOX) + } else if (fieldType == FormFieldType::kListBox) { m_pFormNotify->AfterSelectionChange(pField); - else if (iType == FIELDTYPE_COMBOBOX || iType == FIELDTYPE_TEXTFIELD) + } else if (fieldType == FormFieldType::kComboBox || + fieldType == FormFieldType::kTextField) { m_pFormNotify->AfterValueChange(pField); + } } } -- cgit v1.2.3