clang-format all pdfium code.
[pdfium.git] / fpdfsdk / include / fsdk_baseform.h
1 // Copyright 2014 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 FPDFSDK_INCLUDE_FSDK_BASEFORM_H_
8 #define FPDFSDK_INCLUDE_FSDK_BASEFORM_H_
9
10 #if _FX_OS_ == _FX_ANDROID_
11 #include "time.h"
12 #else
13 #include <ctime>
14 #endif
15
16 #include <map>
17
18 #include "../../core/include/fpdfapi/fpdf_parser.h"
19 #include "../../core/include/fpdfdoc/fpdf_doc.h"
20 #include "../../core/include/fxcrt/fx_basic.h"
21 #include "../../core/include/fxge/fx_dib.h"
22 #include "fsdk_baseannot.h"
23
24 class CFFL_FormFiller;
25 class CPDFSDK_Annot;
26 class CPDFSDK_DateTime;
27 class CPDFSDK_Document;
28 class CPDFSDK_InterForm;
29 class CPDFSDK_PageView;
30 class CPDF_Action;
31 class CPDF_FormField;
32 struct CPWL_Color;
33
34 typedef struct _PDFSDK_FieldAction {
35   _PDFSDK_FieldAction() {
36     bModifier = FALSE;
37     bShift = FALSE;
38     nCommitKey = 0;
39     bKeyDown = FALSE;
40     nSelEnd = nSelStart = 0;
41     bWillCommit = FALSE;
42     bFieldFull = FALSE;
43     bRC = TRUE;
44   }
45
46   FX_BOOL bModifier;  // in
47   FX_BOOL bShift;  // in
48   int nCommitKey;  // in
49   CFX_WideString sChange;  // in[out]
50   CFX_WideString sChangeEx;  // in
51   FX_BOOL bKeyDown;  // in
52   int nSelEnd;  // in[out]
53   int nSelStart;  // in[out]
54   CFX_WideString sValue;  // in[out]
55   FX_BOOL bWillCommit;  // in
56   FX_BOOL bFieldFull;  // in
57   FX_BOOL bRC;  // in[out]
58 } PDFSDK_FieldAction;
59 class CPDFSDK_Widget : public CPDFSDK_Annot {
60  public:
61   CPDFSDK_Widget(CPDF_Annot* pAnnot,
62                  CPDFSDK_PageView* pPageView,
63                  CPDFSDK_InterForm* pInterForm);
64   virtual ~CPDFSDK_Widget();
65
66   virtual CFX_ByteString GetSubType() const;
67
68   virtual CPDF_Action GetAAction(CPDF_AAction::AActionType eAAT);
69
70   int GetFieldType() const;
71   // define layout order to 2.
72   virtual int GetLayoutOrder() const { return 2; }
73   /*
74   FIELDFLAG_READONLY
75   FIELDFLAG_REQUIRED
76   FIELDFLAG_NOEXPORT
77   */
78
79   int GetFieldFlags() const;
80   int GetRotate() const;
81
82   FX_BOOL GetFillColor(FX_COLORREF& color) const;
83   FX_BOOL GetBorderColor(FX_COLORREF& color) const;
84   FX_BOOL GetTextColor(FX_COLORREF& color) const;
85   FX_FLOAT GetFontSize() const;
86
87   int GetSelectedIndex(int nIndex) const;
88   CFX_WideString GetValue() const;
89   CFX_WideString GetDefaultValue() const;
90   CFX_WideString GetOptionLabel(int nIndex) const;
91   int CountOptions() const;
92   FX_BOOL IsOptionSelected(int nIndex) const;
93   int GetTopVisibleIndex() const;
94   FX_BOOL IsChecked() const;
95   /*
96   BF_ALIGN_LEFT
97   BF_ALIGN_MIDDL
98   BF_ALIGN_RIGHT
99   */
100   int GetAlignment() const;
101   int GetMaxLen() const;
102   CFX_WideString GetAlternateName() const;
103
104   // Set Properties.
105   void SetCheck(FX_BOOL bChecked, FX_BOOL bNotify);
106   void SetValue(const CFX_WideString& sValue, FX_BOOL bNotify);
107   void SetDefaultValue(const CFX_WideString& sValue);
108   void SetOptionSelection(int index, FX_BOOL bSelected, FX_BOOL bNotify);
109   void ClearSelection(FX_BOOL bNotify);
110   void SetTopVisibleIndex(int index);
111
112   void ResetAppearance(const FX_WCHAR* sValue, FX_BOOL bValueChanged);
113   void ResetFieldAppearance(FX_BOOL bValueChanged);
114   void UpdateField();
115   CFX_WideString OnFormat(FX_BOOL& bFormated);
116
117   // Message.
118   FX_BOOL OnAAction(CPDF_AAction::AActionType type,
119                     PDFSDK_FieldAction& data,
120                     CPDFSDK_PageView* pPageView);
121
122   CPDFSDK_InterForm* GetInterForm() const { return m_pInterForm; }
123   CPDF_FormField* GetFormField() const;
124   CPDF_FormControl* GetFormControl() const;
125   static CPDF_FormControl* GetFormControl(CPDF_InterForm* pInterForm,
126                                           CPDF_Dictionary* pAnnotDict);
127
128   void DrawShadow(CFX_RenderDevice* pDevice, CPDFSDK_PageView* pPageView);
129
130   void SetAppModified();
131   void ClearAppModified();
132   FX_BOOL IsAppModified() const;
133
134   int32_t GetAppearanceAge() const;
135   int32_t GetValueAge() const;
136
137  private:
138   void ResetAppearance_PushButton();
139   void ResetAppearance_CheckBox();
140   void ResetAppearance_RadioButton();
141   void ResetAppearance_ComboBox(const FX_WCHAR* sValue);
142   void ResetAppearance_ListBox();
143   void ResetAppearance_TextField(const FX_WCHAR* sValue);
144
145   CPDF_Rect GetClientRect() const;
146   CPDF_Rect GetRotatedRect() const;
147
148   CFX_ByteString GetBackgroundAppStream() const;
149   CFX_ByteString GetBorderAppStream() const;
150   CPDF_Matrix GetMatrix() const;
151
152   CPWL_Color GetTextPWLColor() const;
153   CPWL_Color GetBorderPWLColor() const;
154   CPWL_Color GetFillPWLColor() const;
155
156   void AddImageToAppearance(const CFX_ByteString& sAPType, CPDF_Stream* pImage);
157   void RemoveAppearance(const CFX_ByteString& sAPType);
158
159  public:
160   FX_BOOL IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode mode);
161   void DrawAppearance(CFX_RenderDevice* pDevice,
162                       const CPDF_Matrix* pUser2Device,
163                       CPDF_Annot::AppearanceMode mode,
164                       const CPDF_RenderOptions* pOptions);
165
166  public:
167   FX_BOOL HitTest(FX_FLOAT pageX, FX_FLOAT pageY);
168
169  private:
170   CPDFSDK_InterForm* m_pInterForm;
171   FX_BOOL m_bAppModified;
172   int32_t m_nAppAge;
173   int32_t m_nValueAge;
174 };
175
176 class CPDFSDK_InterForm : public CPDF_FormNotify {
177  public:
178   explicit CPDFSDK_InterForm(CPDFSDK_Document* pDocument);
179   ~CPDFSDK_InterForm() override;
180
181   CPDF_InterForm* GetInterForm() const { return m_pInterForm; }
182   CPDFSDK_Document* GetDocument() const { return m_pDocument; }
183
184   FX_BOOL HighlightWidgets();
185
186   CPDFSDK_Widget* GetSibling(CPDFSDK_Widget* pWidget, FX_BOOL bNext) const;
187   CPDFSDK_Widget* GetWidget(CPDF_FormControl* pControl) const;
188   void GetWidgets(const CFX_WideString& sFieldName, CFX_PtrArray& widgets);
189   void GetWidgets(CPDF_FormField* pField, CFX_PtrArray& widgets);
190
191   void AddMap(CPDF_FormControl* pControl, CPDFSDK_Widget* pWidget);
192   void RemoveMap(CPDF_FormControl* pControl);
193
194   void EnableCalculate(FX_BOOL bEnabled);
195   FX_BOOL IsCalculateEnabled() const;
196
197 #ifdef _WIN32
198   CPDF_Stream* LoadImageFromFile(const CFX_WideString& sFile);
199 #endif
200
201   void OnKeyStrokeCommit(CPDF_FormField* pFormField,
202                          CFX_WideString& csValue,
203                          FX_BOOL& bRC);
204   void OnValidate(CPDF_FormField* pFormField,
205                   CFX_WideString& csValue,
206                   FX_BOOL& bRC);
207   void OnCalculate(CPDF_FormField* pFormField = NULL);
208   CFX_WideString OnFormat(CPDF_FormField* pFormField, FX_BOOL& bFormated);
209
210   void ResetFieldAppearance(CPDF_FormField* pFormField,
211                             const FX_WCHAR* sValue,
212                             FX_BOOL bValueChanged);
213   void UpdateField(CPDF_FormField* pFormField);
214
215   FX_BOOL DoAction_Hide(const CPDF_Action& action);
216   FX_BOOL DoAction_SubmitForm(const CPDF_Action& action);
217   FX_BOOL DoAction_ResetForm(const CPDF_Action& action);
218   FX_BOOL DoAction_ImportData(const CPDF_Action& action);
219
220   void GetFieldFromObjects(const CFX_PtrArray& objects, CFX_PtrArray& fields);
221   FX_BOOL IsValidField(CPDF_Dictionary* pFieldDict);
222   FX_BOOL SubmitFields(const CFX_WideString& csDestination,
223                        const CFX_PtrArray& fields,
224                        FX_BOOL bIncludeOrExclude,
225                        FX_BOOL bUrlEncoded);
226   FX_BOOL SubmitForm(const CFX_WideString& sDestination, FX_BOOL bUrlEncoded);
227   FX_BOOL ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf);
228   FX_BOOL ExportFieldsToFDFTextBuf(const CFX_PtrArray& fields,
229                                    FX_BOOL bIncludeOrExclude,
230                                    CFX_ByteTextBuf& textBuf);
231   CFX_WideString GetTemporaryFileName(const CFX_WideString& sFileExt);
232
233  private:
234   virtual int BeforeValueChange(const CPDF_FormField* pField,
235                                 CFX_WideString& csValue);
236   virtual int AfterValueChange(const CPDF_FormField* pField);
237   virtual int BeforeSelectionChange(const CPDF_FormField* pField,
238                                     CFX_WideString& csValue);
239   virtual int AfterSelectionChange(const CPDF_FormField* pField);
240   virtual int AfterCheckedStatusChange(const CPDF_FormField* pField,
241                                        const CFX_ByteArray& statusArray);
242   virtual int BeforeFormReset(const CPDF_InterForm* pForm);
243   virtual int AfterFormReset(const CPDF_InterForm* pForm);
244   virtual int BeforeFormImportData(const CPDF_InterForm* pForm);
245   virtual int AfterFormImportData(const CPDF_InterForm* pForm);
246
247   FX_BOOL FDFToURLEncodedData(CFX_WideString csFDFFile,
248                               CFX_WideString csTxtFile);
249   FX_BOOL FDFToURLEncodedData(uint8_t*& pBuf, FX_STRSIZE& nBufSize);
250   int GetPageIndexByAnnotDict(CPDF_Document* pDocument,
251                               CPDF_Dictionary* pAnnotDict) const;
252   void DoFDFBuffer(CFX_ByteString sBuffer);
253
254   using CPDFSDK_WidgetMap = std::map<CPDF_FormControl*, CPDFSDK_Widget*>;
255
256   CPDFSDK_Document* m_pDocument;
257   CPDF_InterForm* m_pInterForm;
258   CPDFSDK_WidgetMap m_Map;
259   FX_BOOL m_bCalculate;
260   FX_BOOL m_bBusy;
261
262  public:
263   FX_BOOL IsNeedHighLight(int nFieldType);
264   void RemoveAllHighLight();
265   void SetHighlightAlpha(uint8_t alpha) { m_iHighlightAlpha = alpha; }
266   uint8_t GetHighlightAlpha() { return m_iHighlightAlpha; }
267   void SetHighlightColor(FX_COLORREF clr, int nFieldType);
268   FX_COLORREF GetHighlightColor(int nFieldType);
269
270  private:
271   FX_COLORREF m_aHighlightColor[6];
272   uint8_t m_iHighlightAlpha;
273   FX_BOOL m_bNeedHightlight[6];
274 };
275
276 #define BAI_STRUCTURE 0
277 #define BAI_ROW 1
278 #define BAI_COLUMN 2
279
280 #define CPDFSDK_Annots CFX_ArrayTemplate<CPDFSDK_Annot*>
281 #define CPDFSDK_SortAnnots CGW_ArrayTemplate<CPDFSDK_Annot*>
282 class CBA_AnnotIterator {
283  public:
284   CBA_AnnotIterator(CPDFSDK_PageView* pPageView,
285                     const CFX_ByteString& sType,
286                     const CFX_ByteString& sSubType);
287   ~CBA_AnnotIterator();
288
289   CPDFSDK_Annot* GetFirstAnnot();
290   CPDFSDK_Annot* GetLastAnnot();
291   CPDFSDK_Annot* GetNextAnnot(CPDFSDK_Annot* pAnnot);
292   CPDFSDK_Annot* GetPrevAnnot(CPDFSDK_Annot* pAnnot);
293
294  private:
295   void GenerateResults();
296   static int CompareByLeft(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
297   static int CompareByTop(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
298   static CPDF_Rect GetAnnotRect(CPDFSDK_Annot* pAnnot);
299
300   CPDFSDK_PageView* m_pPageView;
301   CFX_ByteString m_sType;
302   CFX_ByteString m_sSubType;
303   int m_nTabs;
304   CPDFSDK_Annots m_Annots;
305 };
306
307 #endif  // FPDFSDK_INCLUDE_FSDK_BASEFORM_H_