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