Home | History | Annotate | Download | only in fpdfdoc
      1 // Copyright 2016 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.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #ifndef CORE_FPDFDOC_CPDF_FORMFIELD_H_
      8 #define CORE_FPDFDOC_CPDF_FORMFIELD_H_
      9 
     10 #include <memory>
     11 #include <utility>
     12 #include <vector>
     13 
     14 #include "core/fpdfdoc/cpdf_aaction.h"
     15 #include "core/fpdfdoc/cpdf_formfield.h"
     16 #include "core/fxcrt/fx_string.h"
     17 #include "core/fxcrt/fx_system.h"
     18 #include "core/fxcrt/unowned_ptr.h"
     19 #include "third_party/base/stl_util.h"
     20 
     21 enum class FormFieldType : uint8_t {
     22   kUnknown = 0,
     23   kPushButton = 1,
     24   kCheckBox = 2,
     25   kRadioButton = 3,
     26   kComboBox = 4,
     27   kListBox = 5,
     28   kTextField = 6,
     29   kSignature = 7,
     30 #ifdef PDF_ENABLE_XFA
     31   kXFA = 8,  // Generic XFA field, should use value below if possible.
     32   kXFA_CheckBox = 9,
     33   kXFA_ComboBox = 10,
     34   kXFA_ImageField = 11,
     35   kXFA_ListBox = 12,
     36   kXFA_PushButton = 13,
     37   kXFA_Signature = 14,
     38   kXFA_TextField = 15
     39 #endif  // PDF_ENABLE_XFA
     40 };
     41 
     42 Optional<FormFieldType> IntToFormFieldType(int value);
     43 
     44 // If values are added to FormFieldType, these will need to be updated.
     45 #ifdef PDF_ENABLE_XFA
     46 constexpr size_t kFormFieldTypeCount = 16;
     47 #else
     48 constexpr size_t kFormFieldTypeCount = 8;
     49 #endif  // PDF_ENABLE_XFA
     50 
     51 constexpr FormFieldType kFormFieldTypes[kFormFieldTypeCount] = {
     52     FormFieldType::kUnknown,
     53     FormFieldType::kPushButton,
     54     FormFieldType::kCheckBox,
     55     FormFieldType::kRadioButton,
     56     FormFieldType::kComboBox,
     57     FormFieldType::kListBox,
     58     FormFieldType::kTextField,
     59     FormFieldType::kSignature,
     60 #ifdef PDF_ENABLE_XFA
     61     FormFieldType::kXFA,
     62     FormFieldType::kXFA_CheckBox,
     63     FormFieldType::kXFA_ComboBox,
     64     FormFieldType::kXFA_ImageField,
     65     FormFieldType::kXFA_ListBox,
     66     FormFieldType::kXFA_PushButton,
     67     FormFieldType::kXFA_Signature,
     68     FormFieldType::kXFA_TextField
     69 #endif  // PDF_ENABLE_XFA
     70 };
     71 
     72 #define FORMFLAG_READONLY 0x01
     73 #define FORMFLAG_REQUIRED 0x02
     74 #define FORMFLAG_NOEXPORT 0x04
     75 
     76 class CPDF_Dictionary;
     77 class CPDF_Font;
     78 class CPDF_FormControl;
     79 class CPDF_InterForm;
     80 class CPDF_String;
     81 
     82 CPDF_Object* FPDF_GetFieldAttr(const CPDF_Dictionary* pFieldDict,
     83                                const char* name,
     84                                int nLevel = 0);
     85 WideString FPDF_GetFullName(CPDF_Dictionary* pFieldDict);
     86 
     87 class CPDF_FormField {
     88  public:
     89   enum Type {
     90     Unknown,
     91     PushButton,
     92     RadioButton,
     93     CheckBox,
     94     Text,
     95     RichText,
     96     File,
     97     ListBox,
     98     ComboBox,
     99     Sign
    100   };
    101 
    102   CPDF_FormField(CPDF_InterForm* pForm, CPDF_Dictionary* pDict);
    103   ~CPDF_FormField();
    104 
    105   WideString GetFullName() const;
    106 
    107   Type GetType() const { return m_Type; }
    108   uint32_t GetFlags() const { return m_Flags; }
    109 
    110   CPDF_Dictionary* GetFieldDict() const { return m_pDict.Get(); }
    111   void SetFieldDict(CPDF_Dictionary* pDict) { m_pDict = pDict; }
    112 
    113   bool ResetField(bool bNotify = false);
    114 
    115   int CountControls() const {
    116     return pdfium::CollectionSize<int>(m_ControlList);
    117   }
    118 
    119   CPDF_FormControl* GetControl(int index) const {
    120     return m_ControlList[index].Get();
    121   }
    122 
    123   int GetControlIndex(const CPDF_FormControl* pControl) const;
    124   FormFieldType GetFieldType() const;
    125 
    126   CPDF_AAction GetAdditionalAction() const;
    127   WideString GetAlternateName() const;
    128   WideString GetMappingName() const;
    129 
    130   uint32_t GetFieldFlags() const;
    131   ByteString GetDefaultStyle() const;
    132   WideString GetRichTextString() const;
    133 
    134   WideString GetValue() const;
    135   WideString GetDefaultValue() const;
    136   bool SetValue(const WideString& value, bool bNotify = false);
    137 
    138   int GetMaxLen() const;
    139   int CountSelectedItems() const;
    140   int GetSelectedIndex(int index) const;
    141 
    142   bool ClearSelection(bool bNotify = false);
    143   bool IsItemSelected(int index) const;
    144   bool SetItemSelection(int index, bool bSelected, bool bNotify = false);
    145 
    146   bool IsItemDefaultSelected(int index) const;
    147 
    148   int GetDefaultSelectedItem() const;
    149   int CountOptions() const;
    150 
    151   WideString GetOptionLabel(int index) const;
    152   WideString GetOptionValue(int index) const;
    153 
    154   int FindOption(WideString csOptLabel) const;
    155   int FindOptionValue(const WideString& csOptValue) const;
    156 
    157   bool CheckControl(int iControlIndex, bool bChecked, bool bNotify = false);
    158 
    159   int GetTopVisibleIndex() const;
    160   int CountSelectedOptions() const;
    161 
    162   int GetSelectedOptionIndex(int index) const;
    163   bool IsOptionSelected(int iOptIndex) const;
    164 
    165   bool SelectOption(int iOptIndex, bool bSelected, bool bNotify = false);
    166 
    167   bool ClearSelectedOptions(bool bNotify = false);
    168 
    169 #ifdef PDF_ENABLE_XFA
    170   bool ClearOptions(bool bNotify = false);
    171 
    172   int InsertOption(WideString csOptLabel, int index = -1, bool bNotify = false);
    173 #endif  // PDF_ENABLE_XFA
    174 
    175   float GetFontSize() const { return m_FontSize; }
    176   CPDF_Font* GetFont() const { return m_pFont.Get(); }
    177 
    178   const CPDF_Dictionary* GetDict() const { return m_pDict.Get(); }
    179   const CPDF_InterForm* GetForm() const { return m_pForm.Get(); }
    180 
    181   WideString GetCheckValue(bool bDefault) const;
    182 
    183   void AddFormControl(CPDF_FormControl* pFormControl) {
    184     m_ControlList.emplace_back(pFormControl);
    185   }
    186 
    187   void SetOpt(std::unique_ptr<CPDF_Object> pOpt) {
    188     m_pDict->SetFor("Opt", std::move(pOpt));
    189   }
    190 
    191  private:
    192   WideString GetValue(bool bDefault) const;
    193   bool SetValue(const WideString& value, bool bDefault, bool bNotify);
    194 
    195   void SyncFieldFlags();
    196   int FindListSel(CPDF_String* str);
    197   WideString GetOptionText(int index, int sub_index) const;
    198 
    199   void LoadDA();
    200   bool SetCheckValue(const WideString& value, bool bDefault, bool bNotify);
    201 
    202   bool NotifyBeforeSelectionChange(const WideString& value);
    203   void NotifyAfterSelectionChange();
    204 
    205   bool NotifyBeforeValueChange(const WideString& value);
    206   void NotifyAfterValueChange();
    207 
    208   bool NotifyListOrComboBoxBeforeChange(const WideString& value);
    209   void NotifyListOrComboBoxAfterChange();
    210 
    211   CPDF_FormField::Type m_Type;
    212   uint32_t m_Flags;
    213   UnownedPtr<CPDF_InterForm> const m_pForm;
    214   UnownedPtr<CPDF_Dictionary> m_pDict;
    215   // Owned by InterForm parent.
    216   std::vector<UnownedPtr<CPDF_FormControl>> m_ControlList;
    217   float m_FontSize;
    218   UnownedPtr<CPDF_Font> m_pFont;
    219 };
    220 
    221 #endif  // CORE_FPDFDOC_CPDF_FORMFIELD_H_
    222