• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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 <stddef.h>
11 #include <stdint.h>
12 
13 #include <utility>
14 #include <vector>
15 
16 #include "core/fpdfdoc/cpdf_aaction.h"
17 #include "core/fxcrt/fx_string.h"
18 #include "core/fxcrt/retain_ptr.h"
19 #include "core/fxcrt/unowned_ptr.h"
20 
21 class CPDF_Dictionary;
22 class CPDF_FormControl;
23 class CPDF_InteractiveForm;
24 class CPDF_Object;
25 class CPDF_String;
26 
27 enum class NotificationOption : bool { kDoNotNotify = false, kNotify = true };
28 
29 enum class FormFieldType : uint8_t {
30   kUnknown = 0,
31   kPushButton = 1,
32   kCheckBox = 2,
33   kRadioButton = 3,
34   kComboBox = 4,
35   kListBox = 5,
36   kTextField = 6,
37   kSignature = 7,
38 #ifdef PDF_ENABLE_XFA
39   kXFA = 8,  // Generic XFA field, should use value below if possible.
40   kXFA_CheckBox = 9,
41   kXFA_ComboBox = 10,
42   kXFA_ImageField = 11,
43   kXFA_ListBox = 12,
44   kXFA_PushButton = 13,
45   kXFA_Signature = 14,
46   kXFA_TextField = 15
47 #endif  // PDF_ENABLE_XFA
48 };
49 
50 // If values are added to FormFieldType, these will need to be updated.
51 #ifdef PDF_ENABLE_XFA
52 constexpr size_t kFormFieldTypeCount = 16;
53 #else   // PDF_ENABLE_XFA
54 constexpr size_t kFormFieldTypeCount = 8;
55 #endif  // PDF_ENABLE_XFA
56 
57 class CPDF_FormField {
58  public:
59   enum Type {
60     kUnknown,
61     kPushButton,
62     kRadioButton,
63     kCheckBox,
64     kText,
65     kRichText,
66     kFile,
67     kListBox,
68     kComboBox,
69     kSign
70   };
71 
72   CPDF_FormField(CPDF_InteractiveForm* pForm, RetainPtr<CPDF_Dictionary> pDict);
73   ~CPDF_FormField();
74 
75   static std::optional<FormFieldType> IntToFormFieldType(int value);
76   static WideString GetFullNameForDict(const CPDF_Dictionary* pFieldDict);
77   static RetainPtr<const CPDF_Object> GetFieldAttrForDict(
78       const CPDF_Dictionary* pFieldDict,
79       const ByteString& name);
80   static RetainPtr<CPDF_Object> GetMutableFieldAttrForDict(
81       CPDF_Dictionary* pFieldDict,
82       const ByteString& name);
83 
84   WideString GetFullName() const;
GetType()85   Type GetType() const { return m_Type; }
86 
87   RetainPtr<const CPDF_Object> GetFieldAttr(const ByteString& name) const;
88   RetainPtr<const CPDF_Dictionary> GetFieldDict() const;
89   bool ResetField();
90 
91   int CountControls() const;
92   CPDF_FormControl* GetControl(int index) const;
93   int GetControlIndex(const CPDF_FormControl* pControl) const;
94 
95   FormFieldType GetFieldType() const;
96 
97   CPDF_AAction GetAdditionalAction() const;
98   WideString GetAlternateName() const;
99   WideString GetMappingName() const;
100 
101   uint32_t GetFieldFlags() const;
102   void SetFieldFlags(uint32_t dwFlags);
103 
IsRequired()104   bool IsRequired() const { return m_bRequired; }
IsNoExport()105   bool IsNoExport() const { return m_bNoExport; }
106 
107   WideString GetValue() const;
108   WideString GetDefaultValue() const;
109   bool SetValue(const WideString& value, NotificationOption notify);
110 
111   int GetMaxLen() const;
112   int CountSelectedItems() const;
113   int GetSelectedIndex(int index) const;
114 
115   bool ClearSelection(NotificationOption notify);
116   bool IsItemSelected(int index) const;
117   bool SetItemSelection(int index, NotificationOption notify);
118 
119   int GetDefaultSelectedItem() const;
120 
121   bool HasOptField() const;
122 
123   // These can only be called if HasOptField() returns true.
124   int CountOptions() const;
125   WideString GetOptionLabel(int index) const;
126   WideString GetOptionValue(int index) const;
127   int FindOption(const WideString& csOptValue) const;
128 
129   bool CheckControl(int iControlIndex,
130                     bool bChecked,
131                     NotificationOption notify);
132 
133   int GetTopVisibleIndex() const;
134   int CountSelectedOptions() const;
135   int GetSelectedOptionIndex(int index) const;
136   bool IsSelectedOption(const WideString& wsOptValue) const;
137   bool IsSelectedIndex(int iOptIndex) const;
138   void SelectOption(int iOptIndex);
139 
140   // Verifies if there is a valid selected indicies (/I) object and whether its
141   // entries are consistent with the value (/V) object.
142   bool UseSelectedIndicesObject() const;
143 
144   WideString GetCheckValue(bool bDefault) const;
145 
146  private:
147   WideString GetValue(bool bDefault) const;
148   bool SetValue(const WideString& value,
149                 bool bDefault,
150                 NotificationOption notify);
151   void InitFieldFlags();
152   int FindListSel(CPDF_String* str);
153   WideString GetOptionText(int index, int sub_index) const;
154   bool SetCheckValue(const WideString& value,
155                      bool bDefault,
156                      NotificationOption notify);
157   void SetItemSelectionSelected(int index, const WideString& opt_value);
158   bool NotifyListOrComboBoxBeforeChange(const WideString& value);
159   void NotifyListOrComboBoxAfterChange();
160 
161   RetainPtr<const CPDF_Object> GetFieldAttrInternal(
162       const ByteString& name) const;
163   const CPDF_Dictionary* GetFieldDictInternal() const;
164   RetainPtr<const CPDF_Object> GetDefaultValueObject() const;
165   RetainPtr<const CPDF_Object> GetValueObject() const;
166 
167   // For choice fields.
168   RetainPtr<const CPDF_Object> GetSelectedIndicesObject() const;
169 
170   // For choice fields.
171   // Value object takes precedence over selected indices object.
172   RetainPtr<const CPDF_Object> GetValueOrSelectedIndicesObject() const;
173 
174   const std::vector<UnownedPtr<CPDF_FormControl>>& GetControls() const;
175 
176   CPDF_FormField::Type m_Type = kUnknown;
177   bool m_bRequired = false;
178   bool m_bNoExport = false;
179   bool m_bIsMultiSelectListBox = false;
180   bool m_bIsUnison = false;
181   bool m_bUseSelectedIndices = false;
182   UnownedPtr<CPDF_InteractiveForm> const m_pForm;
183   RetainPtr<CPDF_Dictionary> const m_pDict;
184 };
185 
186 #endif  // CORE_FPDFDOC_CPDF_FORMFIELD_H_
187