Replace some Release() calls with virtual destructors.
[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 "../../core/include/fpdfapi/fpdf_parser.h"
17 #include "../../core/include/fpdfdoc/fpdf_doc.h"
18 #include "../../core/include/fxcrt/fx_basic.h"
19 #include "../../core/include/fxge/fx_dib.h"
20 #include "fsdk_baseannot.h"
21
22 class CFFL_FormFiller;
23 class CPDFSDK_Annot;
24 class CPDFSDK_DateTime;
25 class CPDFSDK_Document;
26 class CPDFSDK_InterForm;
27 class CPDFSDK_PageView;
28 class CPDF_Action;
29 class CPDF_FormField;
30 struct CPWL_Color;
31
32 typedef struct _PDFSDK_FieldAction
33 {
34         _PDFSDK_FieldAction()
35         {
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 {
61 public:
62         CPDFSDK_Widget(CPDF_Annot* pAnnot, CPDFSDK_PageView* pPageView, CPDFSDK_InterForm* pInterForm);
63         virtual ~CPDFSDK_Widget();
64
65         virtual CFX_ByteString                  GetSubType() const;
66
67         virtual CPDF_Action                             GetAAction(CPDF_AAction::AActionType eAAT);
68
69         int                                                             GetFieldType() const;
70         //define layout order to 2.
71         virtual int                                             GetLayoutOrder() const {return 2;}
72         /*
73         FIELDFLAG_READONLY
74         FIELDFLAG_REQUIRED
75         FIELDFLAG_NOEXPORT
76         */
77
78         int                                                             GetFieldFlags() const;
79         int                                                             GetRotate() const;
80
81         FX_BOOL                                                 GetFillColor(FX_COLORREF& color) const;
82         FX_BOOL                                                 GetBorderColor(FX_COLORREF& color) const;
83         FX_BOOL                                                 GetTextColor(FX_COLORREF& color) const;
84         FX_FLOAT                                                GetFontSize() const;
85
86         int                                                             GetSelectedIndex(int nIndex) const;
87         CFX_WideString                                  GetValue() const;
88         CFX_WideString                                  GetDefaultValue() const;
89         CFX_WideString                                  GetOptionLabel(int nIndex) const;
90         int                                                             CountOptions() const;
91         FX_BOOL                                                 IsOptionSelected(int nIndex) const;
92         int                                                             GetTopVisibleIndex() const;
93         FX_BOOL                                                 IsChecked() const;
94         /*
95         BF_ALIGN_LEFT
96         BF_ALIGN_MIDDL
97         BF_ALIGN_RIGHT
98         */
99         int                                                             GetAlignment() const;
100         int                                                             GetMaxLen() const;
101         CFX_WideString                                  GetAlternateName() const;
102
103 //Set Properties.
104         void                                                    SetCheck(FX_BOOL bChecked, FX_BOOL bNotify);
105         void                                                    SetValue(const CFX_WideString& sValue, FX_BOOL bNotify);
106         void                                                    SetDefaultValue(const CFX_WideString& sValue);
107         void                                                    SetOptionSelection(int index, FX_BOOL bSelected, FX_BOOL bNotify);
108         void                                                    ClearSelection(FX_BOOL bNotify);
109         void                                                    SetTopVisibleIndex(int index);
110
111         void                                                    ResetAppearance(const FX_WCHAR* sValue, FX_BOOL bValueChanged);
112         void                                                    ResetFieldAppearance(FX_BOOL bValueChanged);
113         void                                                    UpdateField();
114         CFX_WideString                                  OnFormat(FX_BOOL& bFormated);
115
116 //Message.
117         FX_BOOL                                                 OnAAction(CPDF_AAction::AActionType type, PDFSDK_FieldAction& data,
118                                                                                                 CPDFSDK_PageView* pPageView);
119
120         CPDFSDK_InterForm*                              GetInterForm() const {return m_pInterForm;}
121         CPDF_FormField*                                 GetFormField() const;
122         CPDF_FormControl*                               GetFormControl() const;
123         static CPDF_FormControl*                GetFormControl(CPDF_InterForm* pInterForm, CPDF_Dictionary* pAnnotDict);
124
125         void                                                    DrawShadow(CFX_RenderDevice* pDevice, CPDFSDK_PageView* pPageView);
126
127         void                                                    SetAppModified();
128         void                                                    ClearAppModified();
129         FX_BOOL                                                 IsAppModified() const;
130
131         int32_t                                         GetAppearanceAge() const;
132         int32_t                                         GetValueAge() const;
133
134 private:
135         void                                                    ResetAppearance_PushButton();
136         void                                                    ResetAppearance_CheckBox();
137         void                                                    ResetAppearance_RadioButton();
138         void                                                    ResetAppearance_ComboBox(const FX_WCHAR* sValue);
139         void                                                    ResetAppearance_ListBox();
140         void                                                    ResetAppearance_TextField(const FX_WCHAR* sValue);
141
142         CPDF_Rect                                               GetClientRect() const;
143         CPDF_Rect                                               GetRotatedRect() const;
144
145         CFX_ByteString                                  GetBackgroundAppStream() const;
146         CFX_ByteString                                  GetBorderAppStream() const;
147         CPDF_Matrix                                             GetMatrix() const;
148
149         CPWL_Color                                              GetTextPWLColor() const;
150         CPWL_Color                                              GetBorderPWLColor() const;
151         CPWL_Color                                              GetFillPWLColor() const;
152
153         void                                                    AddImageToAppearance(const CFX_ByteString& sAPType, CPDF_Stream* pImage);
154         void                                                    RemoveAppearance(const CFX_ByteString& sAPType);
155 public:
156         FX_BOOL                                                 IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode mode);
157         void                                                    DrawAppearance(CFX_RenderDevice* pDevice, const CPDF_Matrix* pUser2Device,
158                 CPDF_Annot::AppearanceMode mode, const CPDF_RenderOptions* pOptions);
159 public:
160         FX_BOOL                                                 HitTest(FX_FLOAT pageX, FX_FLOAT pageY);
161 private:
162         CPDFSDK_InterForm*                              m_pInterForm;
163         FX_BOOL                                                 m_bAppModified;
164         int32_t                                         m_nAppAge;
165         int32_t                                         m_nValueAge;
166 };
167
168 #define CPDFSDK_WidgetMap                               CFX_MapPtrTemplate<CPDF_FormControl*, CPDFSDK_Widget*>
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 public:
205         FX_BOOL                                                 DoAction_Hide(const CPDF_Action& action);
206         FX_BOOL                                                 DoAction_SubmitForm(const CPDF_Action& action);
207         FX_BOOL                                                 DoAction_ResetForm(const CPDF_Action& action);
208         FX_BOOL                                                 DoAction_ImportData(const CPDF_Action& action);
209
210         void                                                    GetFieldFromObjects(const CFX_PtrArray& objects, CFX_PtrArray& fields);
211         FX_BOOL                                                 IsValidField(CPDF_Dictionary* pFieldDict);
212         FX_BOOL                                                 SubmitFields(const CFX_WideString& csDestination, const CFX_PtrArray& fields,
213                 FX_BOOL bIncludeOrExclude, FX_BOOL bUrlEncoded);
214         FX_BOOL                                                 SubmitForm(const CFX_WideString& sDestination, FX_BOOL bUrlEncoded);
215         FX_BOOL                                                 ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf);
216         FX_BOOL                                                 ExportFieldsToFDFTextBuf(const CFX_PtrArray& fields,FX_BOOL bIncludeOrExclude, CFX_ByteTextBuf& textBuf);
217         CFX_WideString                                  GetTemporaryFileName(const CFX_WideString& sFileExt);
218
219 private:
220         virtual int                                             BeforeValueChange(const CPDF_FormField* pField, CFX_WideString& csValue);
221         virtual int                                             AfterValueChange(const CPDF_FormField* pField);
222         virtual int                                             BeforeSelectionChange(const CPDF_FormField* pField, CFX_WideString& csValue);
223         virtual int                                             AfterSelectionChange(const CPDF_FormField* pField);
224         virtual int                                             AfterCheckedStatusChange(const CPDF_FormField* pField, const CFX_ByteArray& statusArray);
225         virtual int                                             BeforeFormReset(const CPDF_InterForm* pForm);
226         virtual int                                             AfterFormReset(const CPDF_InterForm* pForm);
227         virtual int                                             BeforeFormImportData(const CPDF_InterForm* pForm);
228         virtual int                                             AfterFormImportData(const CPDF_InterForm* pForm);
229
230 private:
231         FX_BOOL                                                 FDFToURLEncodedData(CFX_WideString csFDFFile, CFX_WideString csTxtFile);
232         FX_BOOL                                                 FDFToURLEncodedData(uint8_t*& pBuf, FX_STRSIZE& nBufSize);
233         int                                                             GetPageIndexByAnnotDict(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict) const;
234         void                                                    DoFDFBuffer(CFX_ByteString sBuffer);
235
236 private:
237         CPDFSDK_Document*                               m_pDocument;
238         CPDF_InterForm*                                 m_pInterForm;
239         CPDFSDK_WidgetMap                               m_Map;
240         FX_BOOL                                                 m_bCalculate;
241         FX_BOOL                                                 m_bBusy;
242
243 public:
244         FX_BOOL IsNeedHighLight(int nFieldType);
245         void    RemoveAllHighLight();
246         void    SetHighlightAlpha(uint8_t alpha) {m_iHighlightAlpha = alpha;}
247         uint8_t GetHighlightAlpha() {return m_iHighlightAlpha;}
248         void    SetHighlightColor(FX_COLORREF clr, int nFieldType);
249         FX_COLORREF GetHighlightColor(int nFieldType);
250 private:
251         FX_COLORREF m_aHighlightColor[6];
252         uint8_t m_iHighlightAlpha;
253         FX_BOOL m_bNeedHightlight[6];
254 };
255
256 #define BAI_STRUCTURE           0
257 #define BAI_ROW                         1
258 #define BAI_COLUMN                      2
259
260 #define CPDFSDK_Annots                          CFX_ArrayTemplate<CPDFSDK_Annot*>
261 #define CPDFSDK_SortAnnots                      CGW_ArrayTemplate<CPDFSDK_Annot*>
262 class CBA_AnnotIterator
263 {
264 public:
265         CBA_AnnotIterator(CPDFSDK_PageView* pPageView, const CFX_ByteString& sType, const CFX_ByteString& sSubType);
266         ~CBA_AnnotIterator();
267
268         CPDFSDK_Annot*                          GetFirstAnnot();
269         CPDFSDK_Annot*                          GetLastAnnot();
270         CPDFSDK_Annot*                          GetNextAnnot(CPDFSDK_Annot* pAnnot);
271         CPDFSDK_Annot*                          GetPrevAnnot(CPDFSDK_Annot* pAnnot);
272
273 private:
274         void                                                            GenerateResults();
275         static int                                                      CompareByLeft(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
276         static int                                                      CompareByTop(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
277         static CPDF_Rect                                        GetAnnotRect(CPDFSDK_Annot* pAnnot);
278
279         CPDFSDK_PageView*                                       m_pPageView;
280         CFX_ByteString                                          m_sType;
281         CFX_ByteString                                          m_sSubType;
282         int                                                                     m_nTabs;
283         CPDFSDK_Annots                                          m_Annots;
284 };
285
286 #endif  // FPDFSDK_INCLUDE_FSDK_BASEFORM_H_