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