• 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 absl::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   int CountOptions() const;
121   WideString GetOptionLabel(int index) const;
122   WideString GetOptionValue(int index) const;
123   int FindOption(const WideString& csOptValue) const;
124 
125   bool CheckControl(int iControlIndex,
126                     bool bChecked,
127                     NotificationOption notify);
128 
129   int GetTopVisibleIndex() const;
130   int CountSelectedOptions() const;
131   int GetSelectedOptionIndex(int index) const;
132   bool IsSelectedOption(const WideString& wsOptValue) const;
133   bool IsSelectedIndex(int iOptIndex) const;
134   void SelectOption(int iOptIndex);
135 
136   // Verifies if there is a valid selected indicies (/I) object and whether its
137   // entries are consistent with the value (/V) object.
138   bool UseSelectedIndicesObject() const;
139 
140   WideString GetCheckValue(bool bDefault) const;
141 
142  private:
143   WideString GetValue(bool bDefault) const;
144   bool SetValue(const WideString& value,
145                 bool bDefault,
146                 NotificationOption notify);
147   void InitFieldFlags();
148   int FindListSel(CPDF_String* str);
149   WideString GetOptionText(int index, int sub_index) const;
150   void LoadDA();
151   bool SetCheckValue(const WideString& value,
152                      bool bDefault,
153                      NotificationOption notify);
154   void SetItemSelectionSelected(int index, const WideString& opt_value);
155   bool NotifyListOrComboBoxBeforeChange(const WideString& value);
156   void NotifyListOrComboBoxAfterChange();
157 
158   RetainPtr<const CPDF_Object> GetFieldAttrInternal(
159       const ByteString& name) const;
160   const CPDF_Dictionary* GetFieldDictInternal() const;
161   RetainPtr<const CPDF_Object> GetDefaultValueObject() const;
162   RetainPtr<const CPDF_Object> GetValueObject() const;
163 
164   // For choice fields.
165   RetainPtr<const CPDF_Object> GetSelectedIndicesObject() const;
166 
167   // For choice fields.
168   // Value object takes precedence over selected indices object.
169   RetainPtr<const CPDF_Object> GetValueOrSelectedIndicesObject() const;
170 
171   const std::vector<UnownedPtr<CPDF_FormControl>>& GetControls() const;
172 
173   CPDF_FormField::Type m_Type = kUnknown;
174   bool m_bRequired = false;
175   bool m_bNoExport = false;
176   bool m_bIsMultiSelectListBox = false;
177   bool m_bIsUnison = false;
178   bool m_bUseSelectedIndices = false;
179   UnownedPtr<CPDF_InteractiveForm> const m_pForm;
180   RetainPtr<CPDF_Dictionary> const m_pDict;
181 };
182 
183 #endif  // CORE_FPDFDOC_CPDF_FORMFIELD_H_
184