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 {
_PDFSDK_FieldAction_PDFSDK_FieldAction34 	_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.
GetLayoutOrder()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(FX_LPCWSTR sValue, FX_BOOL bValueChanged);
112 	void							ResetFieldAppearance(FX_BOOL bValueChanged);
113 	void							UpdateField();
114 	CFX_WideString					OnFormat(int nCommitKey, FX_BOOL& bFormated);
115 
116 //Message.
117  	FX_BOOL							OnAAction(CPDF_AAction::AActionType type, PDFSDK_FieldAction& data,
118 												CPDFSDK_PageView* pPageView);
119 
GetInterForm()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 	FX_INT32						GetAppearanceAge() const;
132 	FX_INT32						GetValueAge() const;
133 
134 private:
135 	void							ResetAppearance_PushButton();
136 	void							ResetAppearance_CheckBox();
137 	void							ResetAppearance_RadioButton();
138 	void							ResetAppearance_ComboBox(FX_LPCWSTR sValue);
139 	void							ResetAppearance_ListBox();
140 	void							ResetAppearance_TextField(FX_LPCWSTR 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 	FX_INT32						m_nAppAge;
165 	FX_INT32						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 	CPDFSDK_InterForm(CPDFSDK_Document* pDocument);
174 	virtual ~CPDFSDK_InterForm();
175 
176 public:
177 	virtual void					Destroy();
178 	virtual CPDF_InterForm*			GetInterForm();
179 
180 	CPDFSDK_Document*				GetDocument();
181 	FX_BOOL							HighlightWidgets();
182 
183 	CPDFSDK_Widget*					GetSibling(CPDFSDK_Widget* pWidget, FX_BOOL bNext) const;
184 	CPDFSDK_Widget*					GetWidget(CPDF_FormControl* pControl) const;
185 	void							GetWidgets(const CFX_WideString& sFieldName, CFX_PtrArray& widgets);
186 	void							GetWidgets(CPDF_FormField* pField, CFX_PtrArray& widgets);
187 
188 	void							AddMap(CPDF_FormControl* pControl, CPDFSDK_Widget* pWidget);
189 	void							RemoveMap(CPDF_FormControl* pControl);
190 
191 	void							EnableCalculate(FX_BOOL bEnabled);
192 	FX_BOOL							IsCalculateEnabled() const;
193 
194 #ifdef _WIN32
195 	CPDF_Stream*					LoadImageFromFile(const CFX_WideString& sFile);
196 #endif
197 
198 	void							OnKeyStrokeCommit(CPDF_FormField* pFormField, CFX_WideString& csValue, FX_BOOL& bRC);
199 	void							OnValidate(CPDF_FormField* pFormField, CFX_WideString& csValue, FX_BOOL& bRC);
200 	void							OnCalculate(CPDF_FormField* pFormField = NULL);
201 	CFX_WideString					OnFormat(CPDF_FormField* pFormField, int nCommitKey, FX_BOOL& bFormated);
202 
203 	void							ResetFieldAppearance(CPDF_FormField* pFormField, FX_LPCWSTR sValue, FX_BOOL bValueChanged);
204 	void							UpdateField(CPDF_FormField* pFormField);
205 
206 public:
207 	FX_BOOL							DoAction_Hide(const CPDF_Action& action);
208 	FX_BOOL							DoAction_SubmitForm(const CPDF_Action& action);
209 	FX_BOOL							DoAction_ResetForm(const CPDF_Action& action);
210 	FX_BOOL							DoAction_ImportData(const CPDF_Action& action);
211 
212 	void							GetFieldFromObjects(const CFX_PtrArray& objects, CFX_PtrArray& fields);
213 	FX_BOOL							IsValidField(CPDF_Dictionary* pFieldDict);
214 	FX_BOOL							SubmitFields(const CFX_WideString& csDestination, const CFX_PtrArray& fields,
215 		FX_BOOL bIncludeOrExclude, FX_BOOL bUrlEncoded);
216 	FX_BOOL							SubmitForm(const CFX_WideString& sDestination, FX_BOOL bUrlEncoded);
217 	FX_BOOL							ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf);
218 	FX_BOOL							ExportFieldsToFDFTextBuf(const CFX_PtrArray& fields,FX_BOOL bIncludeOrExclude, CFX_ByteTextBuf& textBuf);
219 	CFX_WideString					GetTemporaryFileName(const CFX_WideString& sFileExt);
220 
221 private:
222 	virtual int						BeforeValueChange(const CPDF_FormField* pField, CFX_WideString& csValue);
223 	virtual int						AfterValueChange(const CPDF_FormField* pField);
224 	virtual int						BeforeSelectionChange(const CPDF_FormField* pField, CFX_WideString& csValue);
225 	virtual int						AfterSelectionChange(const CPDF_FormField* pField);
226 	virtual int						AfterCheckedStatusChange(const CPDF_FormField* pField, const CFX_ByteArray& statusArray);
227 	virtual int						BeforeFormReset(const CPDF_InterForm* pForm);
228 	virtual int						AfterFormReset(const CPDF_InterForm* pForm);
229 	virtual int						BeforeFormImportData(const CPDF_InterForm* pForm);
230 	virtual int						AfterFormImportData(const CPDF_InterForm* pForm);
231 
232 private:
233 	FX_BOOL							FDFToURLEncodedData(CFX_WideString csFDFFile, CFX_WideString csTxtFile);
234 	FX_BOOL							FDFToURLEncodedData(FX_LPBYTE& pBuf, FX_STRSIZE& nBufSize);
235 	int								GetPageIndexByAnnotDict(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict) const;
236 	void							DoFDFBuffer(CFX_ByteString sBuffer);
237 
238 private:
239 	CPDFSDK_Document*				m_pDocument;
240 	CPDF_InterForm*					m_pInterForm;
241 	CPDFSDK_WidgetMap				m_Map;
242 	FX_BOOL							m_bCalculate;
243 	FX_BOOL							m_bBusy;
244 
245 public:
246 	FX_BOOL IsNeedHighLight(int nFieldType);
247 	void    RemoveAllHighLight();
SetHighlightAlpha(FX_BYTE alpha)248 	void    SetHighlightAlpha(FX_BYTE alpha) {m_iHighlightAlpha = alpha;}
GetHighlightAlpha()249 	FX_BYTE GetHighlightAlpha() {return m_iHighlightAlpha;}
250 	void    SetHighlightColor(FX_COLORREF clr, int nFieldType);
251 	FX_COLORREF GetHighlightColor(int nFieldType);
252 private:
253 	FX_COLORREF m_aHighlightColor[6];
254 	FX_BYTE m_iHighlightAlpha;
255 	FX_BOOL	m_bNeedHightlight[6];
256 };
257 
258 #define BAI_STRUCTURE		0
259 #define BAI_ROW				1
260 #define BAI_COLUMN			2
261 
262 #define CPDFSDK_Annots				CFX_ArrayTemplate<CPDFSDK_Annot*>
263 #define CPDFSDK_SortAnnots			CGW_ArrayTemplate<CPDFSDK_Annot*>
264 class CBA_AnnotIterator
265 {
266 public:
267 	CBA_AnnotIterator(CPDFSDK_PageView* pPageView, const CFX_ByteString& sType, const CFX_ByteString& sSubType);
268 	~CBA_AnnotIterator();
269 
270 	CPDFSDK_Annot*				GetFirstAnnot();
271 	CPDFSDK_Annot*				GetLastAnnot();
272 	CPDFSDK_Annot*				GetNextAnnot(CPDFSDK_Annot* pAnnot);
273 	CPDFSDK_Annot*				GetPrevAnnot(CPDFSDK_Annot* pAnnot);
274 
275 private:
276 	void								GenerateResults();
277 	static int							CompareByLeft(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
278 	static int							CompareByTop(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
279 	static CPDF_Rect					GetAnnotRect(CPDFSDK_Annot* pAnnot);
280 
281 	CPDFSDK_PageView*					m_pPageView;
282 	CFX_ByteString						m_sType;
283 	CFX_ByteString						m_sSubType;
284 	int									m_nTabs;
285 	CPDFSDK_Annots						m_Annots;
286 };
287 
288 #endif  // FPDFSDK_INCLUDE_FSDK_BASEFORM_H_
289