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 enum _PDFSDK_XFAAActionType
33 {
34         PDFSDK_XFA_Click = 0,
35         PDFSDK_XFA_Full,
36         PDFSDK_XFA_PreOpen,
37         PDFSDK_XFA_PostOpen
38 }PDFSDK_XFAAActionType;
39
40 typedef struct _PDFSDK_FieldAction
41 {
42         _PDFSDK_FieldAction()
43         {
44                 bModifier = FALSE;
45                 bShift = FALSE;
46                 nCommitKey = 0;
47                 bKeyDown = FALSE;
48                 nSelEnd = nSelStart = 0;
49                 bWillCommit = FALSE;
50                 bFieldFull = FALSE;
51                 bRC = TRUE;
52         }
53
54         FX_BOOL                                 bModifier;              //in
55         FX_BOOL                                 bShift;                 //in
56         int                                             nCommitKey;             //in
57         CFX_WideString                  sChange;                //in[out]
58         CFX_WideString                  sChangeEx;              //in
59         FX_BOOL                                 bKeyDown;               //in
60         int                                             nSelEnd;                //in[out]
61         int                                             nSelStart;              //in[out]
62         CFX_WideString                  sValue;                 //in[out]
63         FX_BOOL                                 bWillCommit;    //in
64         FX_BOOL                                 bFieldFull;             //in
65         FX_BOOL                                 bRC;                    //in[out]
66 }PDFSDK_FieldAction;
67 class CPDFSDK_Widget : public CPDFSDK_BAAnnot
68 {
69 public:
70         IXFA_Widget*                                            GetMixXFAWidget();
71         IXFA_Widget*                                            GetGroupMixXFAWidget();
72         IXFA_WidgetHandler*                             GetXFAWidgetHandler();
73
74         FX_BOOL                                                 HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT);
75         FX_BOOL                                                 OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT, PDFSDK_FieldAction& data, CPDFSDK_PageView* pPageView);
76
77         void                                                    Synchronize(FX_BOOL bSynchronizeElse);
78         void                                                    SynchronizeXFAValue();
79         void                                                    SynchronizeXFAItems();
80
81         static void                                             SynchronizeXFAValue(IXFA_DocView* pXFADocView, IXFA_Widget* hWidget,
82                 CPDF_FormField* pFormField, CPDF_FormControl* pFormControl);
83         static void                                             SynchronizeXFAItems(IXFA_DocView* pXFADocView, IXFA_Widget* hWidget,
84                 CPDF_FormField* pFormField, CPDF_FormControl* pFormControl);
85
86 public:
87         CPDFSDK_Widget(CPDF_Annot* pAnnot, CPDFSDK_PageView* pPageView, CPDFSDK_InterForm* pInterForm);
88         virtual ~CPDFSDK_Widget();
89
90         virtual CFX_ByteString                  GetSubType() const;
91
92         virtual CPDF_Action                             GetAAction(CPDF_AAction::AActionType eAAT);
93
94         int                                                             GetFieldType() const;
95         //define layout order to 2.
96         virtual int                                             GetLayoutOrder() const {return 2;}
97         virtual FX_BOOL                                 IsAppearanceValid();
98         /*
99         FIELDFLAG_READONLY
100         FIELDFLAG_REQUIRED
101         FIELDFLAG_NOEXPORT
102         */
103
104         int                                                             GetFieldFlags() const;
105         int                                                             GetRotate() const;
106
107         FX_BOOL                                                 GetFillColor(FX_COLORREF& color) const;
108         FX_BOOL                                                 GetBorderColor(FX_COLORREF& color) const;
109         FX_BOOL                                                 GetTextColor(FX_COLORREF& color) const;
110         FX_FLOAT                                                GetFontSize() const;
111
112         int                                                             GetSelectedIndex(int nIndex);
113         CFX_WideString                                  GetValue(FX_BOOL bDisplay = TRUE);
114         CFX_WideString                                  GetDefaultValue() const;
115         CFX_WideString                                  GetOptionLabel(int nIndex) const;
116         int                                                             CountOptions() const;
117         FX_BOOL                                                 IsOptionSelected(int nIndex);
118         int                                                             GetTopVisibleIndex() const;
119         FX_BOOL                                                 IsChecked();
120         /*
121         BF_ALIGN_LEFT
122         BF_ALIGN_MIDDL
123         BF_ALIGN_RIGHT
124         */
125         int                                                             GetAlignment() const;
126         int                                                             GetMaxLen() const;
127         CFX_WideString                                  GetName();
128         CFX_WideString                                  GetAlternateName() const;
129
130 //Set Properties.
131         void                                                    SetCheck(FX_BOOL bChecked, FX_BOOL bNotify);
132         void                                                    SetValue(const CFX_WideString& sValue, FX_BOOL bNotify);
133         void                                                    SetDefaultValue(const CFX_WideString& sValue);
134         void                                                    SetOptionSelection(int index, FX_BOOL bSelected, FX_BOOL bNotify);
135         void                                                    ClearSelection(FX_BOOL bNotify);
136         void                                                    SetTopVisibleIndex(int index);
137
138         void                                                    ResetAppearance(FX_BOOL bValueChanged);
139         void                                                    ResetAppearance(const FX_WCHAR* sValue, FX_BOOL bValueChanged);
140         void                                                    ResetFieldAppearance(FX_BOOL bValueChanged);
141         void                                                    UpdateField();
142         CFX_WideString                                  OnFormat(FX_BOOL& bFormated);
143
144 //Message.
145         FX_BOOL                                                 OnAAction(CPDF_AAction::AActionType type, PDFSDK_FieldAction& data,
146                                                                                                 CPDFSDK_PageView* pPageView);
147
148         CPDFSDK_InterForm*                              GetInterForm() const {return m_pInterForm;}
149         CPDF_FormField*                                 GetFormField() const;
150         CPDF_FormControl*                               GetFormControl() const;
151         static CPDF_FormControl*                GetFormControl(CPDF_InterForm* pInterForm, CPDF_Dictionary* pAnnotDict);
152
153         void                                                    DrawShadow(CFX_RenderDevice* pDevice, CPDFSDK_PageView* pPageView);
154
155         void                                                    SetAppModified();
156         void                                                    ClearAppModified();
157         FX_BOOL                                                 IsAppModified() const;
158
159         int32_t                                         GetAppearanceAge() const;
160         int32_t                                         GetValueAge() const;
161
162 private:
163         void                                                    ResetAppearance_PushButton();
164         void                                                    ResetAppearance_CheckBox();
165         void                                                    ResetAppearance_RadioButton();
166         void                                                    ResetAppearance_ComboBox(const FX_WCHAR* sValue);
167         void                                                    ResetAppearance_ListBox();
168         void                                                    ResetAppearance_TextField(const FX_WCHAR* sValue);
169
170         CPDF_Rect                                               GetClientRect() const;
171         CPDF_Rect                                               GetRotatedRect() const;
172
173         CFX_ByteString                                  GetBackgroundAppStream() const;
174         CFX_ByteString                                  GetBorderAppStream() const;
175         CPDF_Matrix                                             GetMatrix() const;
176
177         CPWL_Color                                              GetTextPWLColor() const;
178         CPWL_Color                                              GetBorderPWLColor() const;
179         CPWL_Color                                              GetFillPWLColor() const;
180
181         void                                                    AddImageToAppearance(const CFX_ByteString& sAPType, CPDF_Stream* pImage);
182         void                                                    RemoveAppearance(const CFX_ByteString& sAPType);
183 public:
184         FX_BOOL                                                 IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode mode);
185         void                                                    DrawAppearance(CFX_RenderDevice* pDevice, const CPDF_Matrix* pUser2Device,
186                 CPDF_Annot::AppearanceMode mode, const CPDF_RenderOptions* pOptions);
187 public:
188         FX_BOOL                                                 HitTest(FX_FLOAT pageX, FX_FLOAT pageY);
189 private:
190         CPDFSDK_InterForm*                              m_pInterForm;
191         FX_BOOL                                                 m_bAppModified;
192         int32_t                                         m_nAppAge;
193         int32_t                                         m_nValueAge;
194
195         IXFA_Widget*                                            m_hMixXFAWidget;
196         IXFA_WidgetHandler*                             m_pWidgetHandler;
197 };
198
199 class CPDFSDK_XFAWidget : public CPDFSDK_Annot
200 {
201 public:
202         CPDFSDK_XFAWidget(IXFA_Widget* pAnnot, CPDFSDK_PageView* pPageView, CPDFSDK_InterForm* pInterForm);
203         virtual ~CPDFSDK_XFAWidget(){}
204
205 public:
206         virtual FX_BOOL                         IsXFAField();
207         virtual IXFA_Widget*                    GetXFAWidget() { return m_hXFAWidget; }
208
209         virtual CFX_ByteString          GetType() const ;
210         virtual CFX_ByteString          GetSubType() const { return ""; }
211
212         virtual CFX_FloatRect           GetRect();
213
214 public:
215         CPDFSDK_InterForm*                      GetInterForm() { return m_pInterForm; }
216
217 private:
218         CPDFSDK_InterForm*                              m_pInterForm;
219         IXFA_Widget*                                            m_hXFAWidget;
220 };
221
222 #define CPDFSDK_WidgetMap                               CFX_MapPtrTemplate<CPDF_FormControl*, CPDFSDK_Widget*>
223 #define CPDFSDK_XFAWidgetMap                    CFX_MapPtrTemplate<IXFA_Widget*, CPDFSDK_XFAWidget*>
224 #define CPDFSDK_FieldSynchronizeMap             CFX_MapPtrTemplate<CPDF_FormField*, int>
225
226 class CPDFSDK_InterForm : public CPDF_FormNotify
227 {
228 public:
229         explicit CPDFSDK_InterForm(CPDFSDK_Document* pDocument);
230         ~CPDFSDK_InterForm() override;
231
232         CPDF_InterForm* GetInterForm() const { return m_pInterForm; }
233         CPDFSDK_Document* GetDocument() const { return m_pDocument; }
234
235         FX_BOOL                                                 HighlightWidgets();
236
237         CPDFSDK_Widget*                                 GetSibling(CPDFSDK_Widget* pWidget, FX_BOOL bNext) const;
238         CPDFSDK_Widget*                                 GetWidget(CPDF_FormControl* pControl) const;
239         void                                                    GetWidgets(const CFX_WideString& sFieldName, CFX_PtrArray& widgets);
240         void                                                    GetWidgets(CPDF_FormField* pField, CFX_PtrArray& widgets);
241
242         void                                                    AddMap(CPDF_FormControl* pControl, CPDFSDK_Widget* pWidget);
243         void                                                    RemoveMap(CPDF_FormControl* pControl);
244
245         void                                                    AddXFAMap(IXFA_Widget* hWidget, CPDFSDK_XFAWidget* pWidget);
246         void                                                    RemoveXFAMap(IXFA_Widget* hWidget);
247         CPDFSDK_XFAWidget*                              GetXFAWidget(IXFA_Widget* hWidget);
248
249         void                                                    EnableCalculate(FX_BOOL bEnabled);
250         FX_BOOL                                                 IsCalculateEnabled() const;
251
252         void                                                    XfaEnableCalculate(FX_BOOL bEnabled);
253         FX_BOOL                                                 IsXfaCalculateEnabled() const;
254
255         FX_BOOL                                                 IsXfaValidationsEnabled();
256         void                                                    XfaSetValidationsEnabled(FX_BOOL bEnabled);
257
258 #ifdef _WIN32
259         CPDF_Stream*                                    LoadImageFromFile(const CFX_WideString& sFile);
260 #endif
261
262         void                                                    OnKeyStrokeCommit(CPDF_FormField* pFormField, CFX_WideString& csValue, FX_BOOL& bRC);
263         void                                                    OnValidate(CPDF_FormField* pFormField, CFX_WideString& csValue, FX_BOOL& bRC);
264         void                                                    OnCalculate(CPDF_FormField* pFormField = NULL);
265         CFX_WideString                                  OnFormat(CPDF_FormField* pFormField, FX_BOOL& bFormated);
266
267         void                                                    ResetFieldAppearance(CPDF_FormField* pFormField, const FX_WCHAR* sValue, FX_BOOL bValueChanged);
268         void                                                    UpdateField(CPDF_FormField* pFormField);
269
270 public:
271         FX_BOOL                                                 DoAction_Hide(const CPDF_Action& action);
272         FX_BOOL                                                 DoAction_SubmitForm(const CPDF_Action& action);
273         FX_BOOL                                                 DoAction_ResetForm(const CPDF_Action& action);
274         FX_BOOL                                                 DoAction_ImportData(const CPDF_Action& action);
275
276         void                                                    GetFieldFromObjects(const CFX_PtrArray& objects, CFX_PtrArray& fields);
277         FX_BOOL                                                 IsValidField(CPDF_Dictionary* pFieldDict);
278         FX_BOOL                                                 SubmitFields(const CFX_WideString& csDestination, const CFX_PtrArray& fields,
279                 FX_BOOL bIncludeOrExclude, FX_BOOL bUrlEncoded);
280         FX_BOOL                                                 SubmitForm(const CFX_WideString& sDestination, FX_BOOL bUrlEncoded);
281         FX_BOOL                                                 ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf);
282         FX_BOOL                                                 ExportFieldsToFDFTextBuf(const CFX_PtrArray& fields,FX_BOOL bIncludeOrExclude, CFX_ByteTextBuf& textBuf);
283         CFX_WideString                                  GetTemporaryFileName(const CFX_WideString& sFileExt);
284
285         void                                                    SynchronizeField(CPDF_FormField* pFormField, FX_BOOL bSynchronizeElse);
286
287 private:
288         virtual int                                             BeforeValueChange(const CPDF_FormField* pField, CFX_WideString& csValue);
289         virtual int                                             AfterValueChange(const CPDF_FormField* pField);
290         virtual int                                             BeforeSelectionChange(const CPDF_FormField* pField, CFX_WideString& csValue);
291         virtual int                                             AfterSelectionChange(const CPDF_FormField* pField);
292         virtual int                                             AfterCheckedStatusChange(const CPDF_FormField* pField, const CFX_ByteArray& statusArray);
293         virtual int                                             BeforeFormReset(const CPDF_InterForm* pForm);
294         virtual int                                             AfterFormReset(const CPDF_InterForm* pForm);
295         virtual int                                             BeforeFormImportData(const CPDF_InterForm* pForm);
296         virtual int                                             AfterFormImportData(const CPDF_InterForm* pForm);
297
298 private:
299         FX_BOOL                                                 FDFToURLEncodedData(CFX_WideString csFDFFile, CFX_WideString csTxtFile);
300         FX_BOOL                                                 FDFToURLEncodedData(uint8_t*& pBuf, FX_STRSIZE& nBufSize);
301         int                                                             GetPageIndexByAnnotDict(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict) const;
302         void                                                    DoFDFBuffer(CFX_ByteString sBuffer);
303
304 private:
305         CPDFSDK_Document*                               m_pDocument;
306         CPDF_InterForm*                                 m_pInterForm;
307         CPDFSDK_WidgetMap                               m_Map;
308         CPDFSDK_XFAWidgetMap                    m_XFAMap;
309         CPDFSDK_FieldSynchronizeMap             m_FieldSynchronizeMap;
310         FX_BOOL                                                 m_bCalculate;
311         FX_BOOL                                                 m_bXfaCalculate;
312         FX_BOOL                                                 m_bXfaValidationsEnabled;
313         FX_BOOL                                                 m_bBusy;
314
315 public:
316         FX_BOOL IsNeedHighLight(int nFieldType);
317         void    RemoveAllHighLight();
318         void    SetHighlightAlpha(uint8_t alpha) {m_iHighlightAlpha = alpha;}
319         uint8_t GetHighlightAlpha() {return m_iHighlightAlpha;}
320         void    SetHighlightColor(FX_COLORREF clr, int nFieldType);
321         FX_COLORREF GetHighlightColor(int nFieldType);
322 private:
323         FX_COLORREF m_aHighlightColor[7];
324         uint8_t m_iHighlightAlpha;
325         FX_BOOL m_bNeedHightlight[7];
326 };
327
328 #define BAI_STRUCTURE           0
329 #define BAI_ROW                         1
330 #define BAI_COLUMN                      2
331
332 #define CPDFSDK_Annots                          CFX_ArrayTemplate<CPDFSDK_Annot*>
333 #define CPDFSDK_SortAnnots                      CGW_ArrayTemplate<CPDFSDK_Annot*>
334 class CBA_AnnotIterator
335 {
336 public:
337         CBA_AnnotIterator(CPDFSDK_PageView* pPageView, const CFX_ByteString& sType, const CFX_ByteString& sSubType);
338         ~CBA_AnnotIterator();
339
340         CPDFSDK_Annot*                          GetFirstAnnot();
341         CPDFSDK_Annot*                          GetLastAnnot();
342         CPDFSDK_Annot*                          GetNextAnnot(CPDFSDK_Annot* pAnnot);
343         CPDFSDK_Annot*                          GetPrevAnnot(CPDFSDK_Annot* pAnnot);
344
345 private:
346         void                                                            GenerateResults();
347         static int                                                      CompareByLeft(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
348         static int                                                      CompareByTop(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
349         static CPDF_Rect                                        GetAnnotRect(CPDFSDK_Annot* pAnnot);
350
351         CPDFSDK_PageView*                                       m_pPageView;
352         CFX_ByteString                                          m_sType;
353         CFX_ByteString                                          m_sSubType;
354         int                                                                     m_nTabs;
355         CPDFSDK_Annots                                          m_Annots;
356 };
357
358 #endif  // FPDFSDK_INCLUDE_FSDK_BASEFORM_H_