Merge to XFA: Fix IWYU in formfiller/ directory.
[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 _FSDK_BASEFORM_H_
8 #define _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         XFA_HWIDGET                                             GetMixXFAWidget();
71         XFA_HWIDGET                                             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, XFA_HWIDGET hWidget, 
82                 CPDF_FormField* pFormField, CPDF_FormControl* pFormControl);
83         static void                                             SynchronizeXFAItems(IXFA_DocView* pXFADocView, XFA_HWIDGET 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(FX_LPCWSTR sValue, FX_BOOL bValueChanged);
140         void                                                    ResetFieldAppearance(FX_BOOL bValueChanged);
141         void                                                    UpdateField();
142         CFX_WideString                                  OnFormat(int nCommitKey, 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         FX_INT32                                                GetAppearanceAge() const;
160         FX_INT32                                                GetValueAge() const;
161         
162 private:
163         void                                                    ResetAppearance_PushButton();
164         void                                                    ResetAppearance_CheckBox();
165         void                                                    ResetAppearance_RadioButton();
166         void                                                    ResetAppearance_ComboBox(FX_LPCWSTR sValue);
167         void                                                    ResetAppearance_ListBox();
168         void                                                    ResetAppearance_TextField(FX_LPCWSTR 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         FX_INT32                                                m_nAppAge;
193         FX_INT32                                                m_nValueAge;
194
195         XFA_HWIDGET                                             m_hMixXFAWidget;
196         IXFA_WidgetHandler*                             m_pWidgetHandler;
197 };
198
199 class CPDFSDK_XFAWidget : public CPDFSDK_Annot
200 {
201 public:
202         CPDFSDK_XFAWidget(XFA_HWIDGET pAnnot, CPDFSDK_PageView* pPageView, CPDFSDK_InterForm* pInterForm);
203         virtual ~CPDFSDK_XFAWidget(){}
204
205 public:
206         virtual FX_BOOL                         IsXFAField();
207         virtual XFA_HWIDGET                     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         XFA_HWIDGET                                             m_hXFAWidget;
220 };
221
222 #define CPDFSDK_WidgetMap                               CFX_MapPtrTemplate<CPDF_FormControl*, CPDFSDK_Widget*>
223 #define CPDFSDK_XFAWidgetMap                    CFX_MapPtrTemplate<XFA_HWIDGET, CPDFSDK_XFAWidget*>
224 #define CPDFSDK_FieldSynchronizeMap             CFX_MapPtrTemplate<CPDF_FormField*, int>
225
226 class CPDFSDK_InterForm : public CPDF_FormNotify
227 {
228 public:
229         CPDFSDK_InterForm(CPDFSDK_Document* pDocument);
230         virtual ~CPDFSDK_InterForm();
231         
232 public:
233         virtual void                                    Destroy();
234         virtual CPDF_InterForm*                 GetInterForm();
235         
236         CPDFSDK_Document*                               GetDocument();
237         FX_BOOL                                                 HighlightWidgets();
238         
239         CPDFSDK_Widget*                                 GetSibling(CPDFSDK_Widget* pWidget, FX_BOOL bNext) const;
240         CPDFSDK_Widget*                                 GetWidget(CPDF_FormControl* pControl) const;
241         void                                                    GetWidgets(const CFX_WideString& sFieldName, CFX_PtrArray& widgets);
242         void                                                    GetWidgets(CPDF_FormField* pField, CFX_PtrArray& widgets);
243         
244         void                                                    AddMap(CPDF_FormControl* pControl, CPDFSDK_Widget* pWidget);
245         void                                                    RemoveMap(CPDF_FormControl* pControl);
246         
247         void                                                    AddXFAMap(XFA_HWIDGET hWidget, CPDFSDK_XFAWidget* pWidget);
248         void                                                    RemoveXFAMap(XFA_HWIDGET hWidget);
249         CPDFSDK_XFAWidget*                              GetXFAWidget(XFA_HWIDGET hWidget);
250         
251         void                                                    EnableCalculate(FX_BOOL bEnabled);
252         FX_BOOL                                                 IsCalculateEnabled() const;
253
254         void                                                    XfaEnableCalculate(FX_BOOL bEnabled);
255         FX_BOOL                                                 IsXfaCalculateEnabled() const;
256
257         FX_BOOL                                                 IsXfaValidationsEnabled();
258         void                                                    XfaSetValidationsEnabled(FX_BOOL bEnabled);
259
260 #ifdef _WIN32
261         CPDF_Stream*                                    LoadImageFromFile(const CFX_WideString& sFile);
262 #endif
263
264         void                                                    OnKeyStrokeCommit(CPDF_FormField* pFormField, CFX_WideString& csValue, FX_BOOL& bRC);
265         void                                                    OnValidate(CPDF_FormField* pFormField, CFX_WideString& csValue, FX_BOOL& bRC);
266         void                                                    OnCalculate(CPDF_FormField* pFormField = NULL);
267         CFX_WideString                                  OnFormat(CPDF_FormField* pFormField, int nCommitKey, FX_BOOL& bFormated);
268         
269         void                                                    ResetFieldAppearance(CPDF_FormField* pFormField, FX_LPCWSTR sValue, FX_BOOL bValueChanged);
270         void                                                    UpdateField(CPDF_FormField* pFormField);
271         
272 public:
273         FX_BOOL                                                 DoAction_Hide(const CPDF_Action& action);
274         FX_BOOL                                                 DoAction_SubmitForm(const CPDF_Action& action);
275         FX_BOOL                                                 DoAction_ResetForm(const CPDF_Action& action);
276         FX_BOOL                                                 DoAction_ImportData(const CPDF_Action& action);
277         
278         void                                                    GetFieldFromObjects(const CFX_PtrArray& objects, CFX_PtrArray& fields);
279         FX_BOOL                                                 IsValidField(CPDF_Dictionary* pFieldDict);
280         FX_BOOL                                                 SubmitFields(const CFX_WideString& csDestination, const CFX_PtrArray& fields, 
281                 FX_BOOL bIncludeOrExclude, FX_BOOL bUrlEncoded);
282         FX_BOOL                                                 SubmitForm(const CFX_WideString& sDestination, FX_BOOL bUrlEncoded);
283         FX_BOOL                                                 ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf);
284         FX_BOOL                                                 ExportFieldsToFDFTextBuf(const CFX_PtrArray& fields,FX_BOOL bIncludeOrExclude, CFX_ByteTextBuf& textBuf);
285         CFX_WideString                                  GetTemporaryFileName(const CFX_WideString& sFileExt);
286         
287         void                                                    SynchronizeField(CPDF_FormField* pFormField, FX_BOOL bSynchronizeElse);
288         
289 private:
290         virtual int                                             BeforeValueChange(const CPDF_FormField* pField, CFX_WideString& csValue);
291         virtual int                                             AfterValueChange(const CPDF_FormField* pField);
292         virtual int                                             BeforeSelectionChange(const CPDF_FormField* pField, CFX_WideString& csValue);
293         virtual int                                             AfterSelectionChange(const CPDF_FormField* pField);
294         virtual int                                             AfterCheckedStatusChange(const CPDF_FormField* pField, const CFX_ByteArray& statusArray);
295         virtual int                                             BeforeFormReset(const CPDF_InterForm* pForm);
296         virtual int                                             AfterFormReset(const CPDF_InterForm* pForm);
297         virtual int                                             BeforeFormImportData(const CPDF_InterForm* pForm);
298         virtual int                                             AfterFormImportData(const CPDF_InterForm* pForm);
299         
300 private:
301         FX_BOOL                                                 FDFToURLEncodedData(CFX_WideString csFDFFile, CFX_WideString csTxtFile);
302         FX_BOOL                                                 FDFToURLEncodedData(FX_LPBYTE& pBuf, FX_STRSIZE& nBufSize);
303         int                                                             GetPageIndexByAnnotDict(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict) const;
304         void                                                    DoFDFBuffer(CFX_ByteString sBuffer);
305         
306 private:
307         CPDFSDK_Document*                               m_pDocument;
308         CPDF_InterForm*                                 m_pInterForm;
309         CPDFSDK_WidgetMap                               m_Map;
310         CPDFSDK_XFAWidgetMap                    m_XFAMap;
311         CPDFSDK_FieldSynchronizeMap             m_FieldSynchronizeMap;
312         FX_BOOL                                                 m_bCalculate;
313         FX_BOOL                                                 m_bXfaCalculate;
314         FX_BOOL                                                 m_bXfaValidationsEnabled;
315         FX_BOOL                                                 m_bBusy;
316
317 public:
318         FX_BOOL IsNeedHighLight(int nFieldType);
319         void    RemoveAllHighLight();
320         void    SetHighlightAlpha(FX_BYTE alpha) {m_iHighlightAlpha = alpha;}
321         FX_BYTE GetHighlightAlpha() {return m_iHighlightAlpha;}
322         void    SetHighlightColor(FX_COLORREF clr, int nFieldType);
323         FX_COLORREF GetHighlightColor(int nFieldType);
324 private:
325         FX_COLORREF m_aHighlightColor[7];
326         FX_BYTE m_iHighlightAlpha;
327         FX_BOOL m_bNeedHightlight[7];
328 };
329
330 #define BAI_STRUCTURE           0
331 #define BAI_ROW                         1
332 #define BAI_COLUMN                      2
333
334 #define CPDFSDK_Annots                          CFX_ArrayTemplate<CPDFSDK_Annot*>
335 #define CPDFSDK_SortAnnots                      CGW_ArrayTemplate<CPDFSDK_Annot*>
336 class CBA_AnnotIterator 
337 {
338 public:
339         CBA_AnnotIterator(CPDFSDK_PageView* pPageView, const CFX_ByteString& sType, const CFX_ByteString& sSubType);
340         virtual ~CBA_AnnotIterator();
341         
342         virtual CPDFSDK_Annot*                          GetFirstAnnot();
343         virtual CPDFSDK_Annot*                          GetLastAnnot();
344         virtual CPDFSDK_Annot*                          GetNextAnnot(CPDFSDK_Annot* pAnnot);
345         virtual CPDFSDK_Annot*                          GetPrevAnnot(CPDFSDK_Annot* pAnnot);
346         
347         virtual void                                            Release(){delete this;}
348         
349 private:
350         void                                                            GenerateResults();
351         static int                                                      CompareByLeft(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
352         static int                                                      CompareByTop(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
353         
354         static CPDF_Rect                                        GetAnnotRect(CPDFSDK_Annot* pAnnot);
355         
356 private:
357         CPDFSDK_PageView*                                       m_pPageView;
358         CFX_ByteString                                          m_sType;
359         CFX_ByteString                                          m_sSubType;
360         int                                                                     m_nTabs;
361         
362         CPDFSDK_Annots                                          m_Annots;
363 };
364
365 #endif //#define _FSDK_BASEFORM_H_
366