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#include "../../include/formfiller/FFL_FormFiller.h"
8#include "../../include/formfiller/FFL_IFormFiller.h"
9#include "../../include/formfiller/FFL_CheckBox.h"
10#include "../../include/formfiller/FFL_ComboBox.h"
11#include "../../include/formfiller/FFL_ListBox.h"
12#include "../../include/formfiller/FFL_PushButton.h"
13#include "../../include/formfiller/FFL_RadioButton.h"
14#include "../../include/formfiller/FFL_TextField.h"
15
16#define FFL_MAXLISTBOXHEIGHT		140.0f
17
18// HHOOK CFFL_IFormFiller::m_hookSheet = NULL;
19// MSG CFFL_IFormFiller::g_Msg;
20
21/* ----------------------------- CFFL_IFormFiller ----------------------------- */
22
23CFFL_IFormFiller::CFFL_IFormFiller(CPDFDoc_Environment* pApp) :
24	m_pApp(pApp),
25	m_bNotifying(FALSE)
26{
27}
28
29CFFL_IFormFiller::~CFFL_IFormFiller()
30{
31	FX_POSITION pos = m_Maps.GetStartPosition();
32	while (pos)
33	{
34		CPDFSDK_Annot * pAnnot = NULL;
35		CFFL_FormFiller * pFormFiller = NULL;
36		m_Maps.GetNextAssoc(pos,pAnnot,pFormFiller);
37		delete pFormFiller;
38	}
39	m_Maps.RemoveAll();
40}
41
42FX_BOOL	CFFL_IFormFiller::Annot_HitTest(CPDFSDK_PageView* pPageView,CPDFSDK_Annot* pAnnot, CPDF_Point point)
43{
44	CPDF_Rect rc = pAnnot->GetRect();
45	if(rc.Contains(point.x, point.y))
46		return TRUE;
47	return FALSE;
48}
49
50FX_RECT CFFL_IFormFiller::GetViewBBox(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot)
51{
52	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
53	{
54		return pFormFiller->GetViewBBox(pPageView, pAnnot);
55	}
56	else
57	{
58		ASSERT(pPageView != NULL);
59		ASSERT(pAnnot != NULL);
60
61		CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
62		ASSERT(pPDFAnnot != NULL);
63
64		CPDF_Rect rcAnnot;
65		pPDFAnnot->GetRect(rcAnnot);
66
67// 		CRect rcWin;
68// 		pPageView->DocToWindow(rcAnnot, rcWin);
69		CPDF_Rect rcWin = CPWL_Utils::InflateRect(rcAnnot,1);
70//		rcWin.InflateRect(1, 1);
71
72		return rcWin.GetOutterRect();
73	}
74}
75
76void CFFL_IFormFiller::OnDraw(CPDFSDK_PageView* pPageView, /*HDC hDC,*/ CPDFSDK_Annot* pAnnot,
77						CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,
78						/*const CRect& rcWindow,*/ FX_DWORD dwFlags)
79{
80	ASSERT(pPageView != NULL);
81	CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
82
83	if (IsVisible(pWidget))
84	{
85		if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
86		{
87 			if (pFormFiller->IsValid())
88 			{
89				pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
90
91				pAnnot->GetPDFPage();
92
93
94				CPDFSDK_Document* pDocument = m_pApp->GetCurrentDoc();
95				ASSERT(pDocument != NULL);
96
97				if (pDocument->GetFocusAnnot() == pAnnot)
98				{
99					CPDF_Rect rcFocus = pFormFiller->GetFocusBox(pPageView);
100					if (!rcFocus.IsEmpty())
101					{
102						CFX_PathData path;
103
104						path.SetPointCount(5);
105						path.SetPoint(0, rcFocus.left,  rcFocus.top, FXPT_MOVETO);
106						path.SetPoint(1, rcFocus.left,  rcFocus.bottom, FXPT_LINETO);
107						path.SetPoint(2, rcFocus.right,  rcFocus.bottom, FXPT_LINETO);
108						path.SetPoint(3, rcFocus.right,  rcFocus.top, FXPT_LINETO);
109						path.SetPoint(4, rcFocus.left,  rcFocus.top, FXPT_LINETO);
110
111						CFX_GraphStateData gsd;
112						gsd.SetDashCount(1);
113						gsd.m_DashArray[0] = 1.0f;
114						gsd.m_DashPhase = 0;
115
116						gsd.m_LineWidth = 1.0f;
117						pDevice->DrawPath(&path, pUser2Device, &gsd, 0, ArgbEncode(255,0,0,0), FXFILL_ALTERNATE);
118
119					//	::DrawFocusRect(hDC, &rcFocus);
120					}
121				}
122
123				return;
124			}
125		}
126
127		if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
128			pFormFiller->OnDrawDeactive(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
129		else
130			pWidget->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
131
132		if (!IsReadOnly(pWidget) && IsFillingAllowed(pWidget))
133		{
134			pWidget->DrawShadow(pDevice, pPageView);
135		}
136
137	}
138}
139
140void CFFL_IFormFiller::OnCreate(CPDFSDK_Annot* pAnnot)
141{
142	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
143	{
144		pFormFiller->OnCreate(pAnnot);
145	}
146}
147
148void CFFL_IFormFiller::OnLoad(CPDFSDK_Annot* pAnnot)
149{
150	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
151	{
152		pFormFiller->OnLoad(pAnnot);
153	}
154}
155
156void CFFL_IFormFiller::OnDelete(CPDFSDK_Annot* pAnnot)
157{
158	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
159	{
160		pFormFiller->OnDelete(pAnnot);
161	}
162
163	UnRegisterFormFiller(pAnnot);
164}
165
166void CFFL_IFormFiller::OnMouseEnter(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlag)
167{
168	ASSERT(pAnnot != NULL);
169	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
170
171	if (!m_bNotifying)
172	{
173		CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
174		if (pWidget->GetAAction(CPDF_AAction::CursorEnter))
175		{
176			m_bNotifying = TRUE;
177
178			int nValueAge = pWidget->GetValueAge();
179
180			pWidget->ClearAppModified();
181
182			ASSERT(pPageView != NULL);
183
184
185
186			PDFSDK_FieldAction fa;
187			fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
188 			fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
189			pWidget->OnAAction(CPDF_AAction::CursorEnter, fa, pPageView );
190			m_bNotifying = FALSE;
191
192			//if ( !IsValidAnnot(pPageView, pAnnot) ) return;
193
194			if (pWidget->IsAppModified())
195			{
196				if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
197				{
198					pFormFiller->ResetPDFWindow(pPageView, pWidget->GetValueAge() == nValueAge);
199				}
200			}
201		}
202	}
203
204	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, TRUE))
205	{
206		pFormFiller->OnMouseEnter(pPageView, pAnnot);
207	}
208}
209
210void CFFL_IFormFiller::OnMouseExit(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlag)
211{
212	ASSERT(pAnnot != NULL);
213	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
214
215	if (!m_bNotifying)
216	{
217		CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
218		if (pWidget->GetAAction(CPDF_AAction::CursorExit))
219		{
220			m_bNotifying = TRUE;
221			pWidget->GetAppearanceAge();
222			int nValueAge = pWidget->GetValueAge();
223			pWidget->ClearAppModified();
224
225			ASSERT(pPageView != NULL);
226
227
228
229			PDFSDK_FieldAction fa;
230			fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
231 			fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
232
233			pWidget->OnAAction(CPDF_AAction::CursorExit, fa, pPageView);
234			m_bNotifying = FALSE;
235
236			//if (!IsValidAnnot(pPageView, pAnnot)) return;
237
238			if (pWidget->IsAppModified())
239			{
240				if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
241				{
242					pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
243				}
244			}
245		}
246	}
247
248	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
249	{
250		pFormFiller->OnMouseExit(pPageView, pAnnot);
251	}
252}
253
254FX_BOOL	CFFL_IFormFiller::OnLButtonDown(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
255{
256	ASSERT(pAnnot != NULL);
257	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
258
259	if (!m_bNotifying)
260	{
261		CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
262		if (Annot_HitTest(pPageView, pAnnot, point) && pWidget->GetAAction(CPDF_AAction::ButtonDown))
263		{
264			m_bNotifying = TRUE;
265			pWidget->GetAppearanceAge();
266			int nValueAge = pWidget->GetValueAge();
267			pWidget->ClearAppModified();
268
269			ASSERT(pPageView != NULL);
270
271
272
273			PDFSDK_FieldAction fa;
274			fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlags);
275 			fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlags);
276			pWidget->OnAAction(CPDF_AAction::ButtonDown, fa, pPageView);
277			m_bNotifying = FALSE;
278
279			if (!IsValidAnnot(pPageView, pAnnot)) return TRUE;
280
281			if (pWidget->IsAppModified())
282			{
283				if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
284				{
285					pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
286				}
287			}
288		}
289	}
290
291	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
292	{
293		return pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
294	}
295
296	return FALSE;
297}
298
299FX_BOOL	CFFL_IFormFiller::OnLButtonUp(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
300{
301	ASSERT(pAnnot != NULL);
302	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
303
304	CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
305	// 	CReader_Page* pPage = pAnnot->GetPage();
306	// 	ASSERT(pPage != NULL);
307	CPDFSDK_Document* pDocument = m_pApp->GetCurrentDoc();
308	ASSERT(pDocument != NULL);
309
310	switch (pWidget->GetFieldType())
311	{
312	case FIELDTYPE_PUSHBUTTON:
313	case FIELDTYPE_CHECKBOX:
314	case FIELDTYPE_RADIOBUTTON:
315		if (GetViewBBox(pPageView, pAnnot).Contains((int)point.x, (int)point.y))
316		{
317			pDocument->SetFocusAnnot(pAnnot);
318		}
319		break;
320	default:
321		pDocument->SetFocusAnnot(pAnnot);
322		break;
323	}
324
325	FX_BOOL bRet = FALSE;
326
327	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
328	{
329		bRet = pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
330	}
331
332	if (pDocument->GetFocusAnnot() == pAnnot)
333	{
334		FX_BOOL bExit = FALSE;
335		FX_BOOL bReset = FALSE;
336		OnButtonUp(pWidget, pPageView, bReset, bExit,nFlags);
337		if (bExit) return TRUE;
338	}
339	return bRet;
340}
341
342void CFFL_IFormFiller::OnButtonUp(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bReset, FX_BOOL& bExit,FX_UINT nFlag)
343{
344	ASSERT(pWidget != NULL);
345
346	if (!m_bNotifying)
347	{
348		if (pWidget->GetAAction(CPDF_AAction::ButtonUp))
349		{
350			m_bNotifying = TRUE;
351			int nAge = pWidget->GetAppearanceAge();
352			int nValueAge = pWidget->GetValueAge();
353
354			ASSERT(pPageView != NULL);
355// 			CReader_DocView* pDocView = pPageView->GetDocView();
356// 			ASSERT(pDocView != NULL);
357
358
359
360			PDFSDK_FieldAction fa;
361			fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
362 			fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
363
364			pWidget->OnAAction(CPDF_AAction::ButtonUp, fa, pPageView);
365			m_bNotifying = FALSE;
366
367			if (!IsValidAnnot(pPageView, pWidget))
368			{
369				bExit = TRUE;
370				return;
371			}
372
373			if (nAge != pWidget->GetAppearanceAge())
374			{
375				if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
376				{
377					pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
378				}
379
380				bReset = TRUE;
381			}
382		}
383	}
384}
385
386FX_BOOL	CFFL_IFormFiller::OnLButtonDblClk(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
387{
388	ASSERT(pAnnot != NULL);
389	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
390
391	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
392	{
393		return pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
394	}
395
396	return FALSE;
397}
398
399FX_BOOL	CFFL_IFormFiller::OnMouseMove(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
400{
401	ASSERT(pAnnot != NULL);
402	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
403
404	//change cursor
405	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, TRUE))
406	{
407		return pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
408	}
409
410	return FALSE;
411}
412
413FX_BOOL	CFFL_IFormFiller::OnMouseWheel(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, short zDelta, const CPDF_Point& point)
414{
415	ASSERT(pAnnot != NULL);
416	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
417
418	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
419	{
420		return pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta, point);
421	}
422
423	return FALSE;
424}
425
426FX_BOOL	CFFL_IFormFiller::OnRButtonDown(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
427{
428	ASSERT(pAnnot != NULL);
429	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
430
431	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
432	{
433		return pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
434	}
435
436	return FALSE;
437}
438
439FX_BOOL	CFFL_IFormFiller::OnRButtonUp(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
440{
441	ASSERT(pAnnot != NULL);
442	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
443
444	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
445	{
446		return pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
447	}
448
449	return FALSE;
450}
451
452FX_BOOL	CFFL_IFormFiller::OnRButtonDblClk(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, FX_UINT nFlags, const CPDF_Point& point)
453{
454	ASSERT(pAnnot != NULL);
455	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
456
457	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
458	{
459		return pFormFiller->OnRButtonDblClk(pPageView, pAnnot, nFlags, point);
460	}
461
462	return FALSE;
463}
464
465FX_BOOL	CFFL_IFormFiller::OnKeyDown(CPDFSDK_Annot* pAnnot, FX_UINT nKeyCode, FX_UINT nFlags)
466{
467	ASSERT(pAnnot != NULL);
468	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
469
470	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
471	{
472		return pFormFiller->OnKeyDown(pAnnot, nKeyCode, nFlags);
473	}
474
475	return FALSE;
476}
477
478FX_BOOL	CFFL_IFormFiller::OnChar(CPDFSDK_Annot* pAnnot, FX_UINT nChar, FX_UINT nFlags)
479{
480	ASSERT(pAnnot != NULL);
481	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
482
483	if (nChar == FWL_VKEY_Tab) return TRUE;
484
485	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
486	{
487		return pFormFiller->OnChar(pAnnot, nChar, nFlags);
488	}
489
490	return FALSE;
491}
492
493void CFFL_IFormFiller::OnDeSelected(CPDFSDK_Annot* pAnnot)
494{
495	ASSERT(pAnnot != NULL);
496	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
497
498	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
499	{
500		pFormFiller->OnDeSelected(pAnnot);
501	}
502}
503
504void CFFL_IFormFiller::OnSelected(CPDFSDK_Annot* pAnnot)
505{
506	ASSERT(pAnnot != NULL);
507	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
508
509	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
510	{
511		pFormFiller->OnSelected(pAnnot);
512	}
513}
514
515FX_BOOL CFFL_IFormFiller::OnSetFocus(CPDFSDK_Annot* pAnnot,FX_UINT nFlag)
516{
517	if(!pAnnot) return FALSE;
518	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
519
520	if (!m_bNotifying)
521	{
522		CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
523 		if (pWidget->GetAAction(CPDF_AAction::GetFocus))
524 		{
525  			m_bNotifying = TRUE;
526			pWidget->GetAppearanceAge();
527			int nValueAge = pWidget->GetValueAge();
528 			pWidget->ClearAppModified();
529
530
531 			CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
532 			ASSERT(pPageView != NULL);
533
534 			PDFSDK_FieldAction fa;
535			fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
536 			fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
537
538
539 			CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, TRUE);
540 			if(!pFormFiller) return FALSE;
541 			pFormFiller->GetActionData(pPageView, CPDF_AAction::GetFocus, fa);
542
543 			pWidget->OnAAction(CPDF_AAction::GetFocus, fa, pPageView);
544 			m_bNotifying = FALSE;
545
546 //			if (!IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pAnnot)) return FALSE;
547
548 			if (pWidget->IsAppModified())
549 			{
550 				if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE))
551 				{
552 					pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
553 				}
554 			}
555		}
556	}
557
558	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, TRUE))
559	{
560		if (pFormFiller->OnSetFocus(pAnnot, nFlag))
561		{
562			return TRUE;
563		}
564		else
565			return FALSE;
566	}
567
568	return TRUE;
569}
570
571FX_BOOL	CFFL_IFormFiller::OnKillFocus(CPDFSDK_Annot* pAnnot,FX_UINT nFlag)
572{
573	if(!pAnnot) return FALSE;
574	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
575
576	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, FALSE))
577	{
578		if (pFormFiller->OnKillFocus(pAnnot, nFlag))
579		{
580 			if (!m_bNotifying)
581 			{
582 				CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
583 				if (pWidget->GetAAction(CPDF_AAction::LoseFocus))
584 				{
585 					m_bNotifying = TRUE;
586 					pWidget->ClearAppModified();
587
588 					CPDFSDK_PageView* pPageView = pWidget->GetPageView();
589 					ASSERT(pPageView != NULL);
590
591 					PDFSDK_FieldAction fa;
592					fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
593 					fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
594
595 					pFormFiller->GetActionData(pPageView, CPDF_AAction::LoseFocus, fa);
596
597 					pWidget->OnAAction(CPDF_AAction::LoseFocus, fa, pPageView);
598 					m_bNotifying = FALSE;
599
600 				}
601 			}
602		}
603		else
604			return FALSE;
605	}
606
607	return TRUE;
608}
609
610FX_BOOL	CFFL_IFormFiller::IsVisible(CPDFSDK_Widget* pWidget)
611{
612	return pWidget->IsVisible();
613}
614
615FX_BOOL	CFFL_IFormFiller::IsReadOnly(CPDFSDK_Widget* pWidget)
616{
617	ASSERT(pWidget != NULL);
618
619	int nFieldFlags = pWidget->GetFieldFlags();
620
621	return (nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY;
622}
623
624FX_BOOL	CFFL_IFormFiller::IsFillingAllowed(CPDFSDK_Widget* pWidget)
625{
626	ASSERT(pWidget != NULL);
627
628	if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
629		return TRUE;
630 	else
631 	{
632 		CPDF_Page* pPage = pWidget->GetPDFPage();
633 		ASSERT(pPage != NULL);
634
635 		CPDF_Document* pDocument = pPage->m_pDocument;
636 		ASSERT(pDocument != NULL);
637
638		FX_DWORD dwPermissions = pDocument->GetUserPermissions();
639 		return (dwPermissions&FPDFPERM_FILL_FORM) ||
640 				(dwPermissions&FPDFPERM_ANNOT_FORM) ||
641 			(dwPermissions&FPDFPERM_MODIFY);
642 	}
643	return TRUE;
644}
645
646CFFL_FormFiller* CFFL_IFormFiller::GetFormFiller(CPDFSDK_Annot* pAnnot, FX_BOOL bRegister)
647{
648// 	ASSERT(pAnnot != NULL);
649// 	ASSERT(pAnnot->GetPDFAnnot()->GetSubType() == "Widget");
650
651	CFFL_FormFiller * pFormFiller = NULL;
652	m_Maps.Lookup(pAnnot, pFormFiller);
653
654	if (pFormFiller)
655		return pFormFiller;
656
657	if (bRegister)
658	{
659		CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
660
661		int nFieldType = pWidget->GetFieldType();
662		switch(nFieldType)
663		{
664 		case FIELDTYPE_PUSHBUTTON:
665 			pFormFiller = new CFFL_PushButton(m_pApp, pWidget);
666 			break;
667		case FIELDTYPE_CHECKBOX:
668			pFormFiller = new CFFL_CheckBox(m_pApp, pWidget);
669			break;
670 		case FIELDTYPE_RADIOBUTTON:
671 			pFormFiller = new CFFL_RadioButton(m_pApp, pWidget);
672 			break;
673 		case FIELDTYPE_TEXTFIELD:
674			pFormFiller = new CFFL_TextField(m_pApp, pWidget);
675			break;
676		case FIELDTYPE_LISTBOX:
677			pFormFiller = new CFFL_ListBox(m_pApp, pWidget);
678			break;
679		case FIELDTYPE_COMBOBOX:
680			pFormFiller = new CFFL_ComboBox(m_pApp, pWidget);
681			break;
682		case FIELDTYPE_UNKNOWN:
683		default:
684			pFormFiller = NULL;
685			break;
686		}
687
688		if (pFormFiller)
689		{
690			m_Maps.SetAt(pAnnot, pFormFiller);
691		}
692	}
693
694	return pFormFiller;
695}
696
697void CFFL_IFormFiller::RemoveFormFiller(CPDFSDK_Annot* pAnnot)
698{
699	if ( pAnnot != NULL )
700	{
701		UnRegisterFormFiller( pAnnot );
702	}
703}
704
705void CFFL_IFormFiller::UnRegisterFormFiller(CPDFSDK_Annot* pAnnot)
706{
707	CFFL_FormFiller * pFormFiller = NULL;
708
709	if (m_Maps.Lookup(pAnnot,pFormFiller))
710	{
711		if (pFormFiller)
712			delete pFormFiller;
713		m_Maps.RemoveKey(pAnnot);
714	}
715}
716
717void CFFL_IFormFiller::SetFocusAnnotTab(CPDFSDK_Annot* pWidget, FX_BOOL bSameField, FX_BOOL bNext)
718{
719
720}
721
722void CFFL_IFormFiller::QueryWherePopup(void* pPrivateData, FX_FLOAT fPopupMin,FX_FLOAT fPopupMax, FX_INT32 & nRet, FX_FLOAT & fPopupRet)
723{
724	ASSERT(pPrivateData != NULL);
725
726	CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
727
728
729
730
731	CPDF_Rect rcPageView(0,0,0,0);
732	rcPageView.right = pData->pWidget->GetPDFPage()->GetPageWidth();
733	rcPageView.bottom = pData->pWidget->GetPDFPage()->GetPageHeight();
734	rcPageView.Normalize();
735
736
737	ASSERT(pData->pWidget != NULL);
738	CPDF_Rect rcAnnot = pData->pWidget->GetRect();
739
740	FX_FLOAT fTop = 0.0f;
741	FX_FLOAT fBottom = 0.0f;
742
743	CPDFSDK_Widget * pWidget = (CPDFSDK_Widget*)pData->pWidget;
744	switch (pWidget->GetRotate() / 90)
745	{
746	default:
747	case 0:
748		fTop = rcPageView.top - rcAnnot.top;
749		fBottom = rcAnnot.bottom - rcPageView.bottom;
750		break;
751	case 1:
752		fTop = rcAnnot.left - rcPageView.left;
753		fBottom = rcPageView.right - rcAnnot.right;
754		break;
755	case 2:
756		fTop = rcAnnot.bottom - rcPageView.bottom;
757		fBottom = rcPageView.top - rcAnnot.top;
758		break;
759	case 3:
760		fTop = rcPageView.right - rcAnnot.right;
761		fBottom = rcAnnot.left - rcPageView.left;
762		break;
763	}
764
765	FX_FLOAT fFactHeight = 0;
766	FX_BOOL bBottom = TRUE;
767	FX_FLOAT fMaxListBoxHeight = 0;
768	if (fPopupMax > FFL_MAXLISTBOXHEIGHT)
769	{
770		if (fPopupMin > FFL_MAXLISTBOXHEIGHT)
771		{
772			fMaxListBoxHeight = fPopupMin;
773		}
774		else
775		{
776			fMaxListBoxHeight = FFL_MAXLISTBOXHEIGHT;
777		}
778	}
779	else
780		fMaxListBoxHeight = fPopupMax;
781
782	if (fBottom > fMaxListBoxHeight)
783	{
784		fFactHeight = fMaxListBoxHeight;
785		bBottom = TRUE;
786	}
787	else
788	{
789		if (fTop > fMaxListBoxHeight)
790		{
791			fFactHeight = fMaxListBoxHeight;
792			bBottom = FALSE;
793		}
794		else
795		{
796			if (fTop > fBottom)
797			{
798				fFactHeight = fTop;
799				bBottom = FALSE;
800			}
801			else
802			{
803				fFactHeight = fBottom;
804				bBottom = TRUE;
805			}
806		}
807	}
808
809	nRet = bBottom ? 0 : 1;
810	fPopupRet = fFactHeight;
811}
812
813void CFFL_IFormFiller::OnSetWindowRect(void* pPrivateData, const CPDF_Rect & rcWindow)
814{
815	ASSERT(pPrivateData != NULL);
816
817	CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
818
819	if (CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, TRUE))
820	{
821
822		CPDF_Rect rcOld = pFormFiller->PWLtoFFL(pFormFiller->GetWindowRect(pData->pPageView));
823		CPDF_Rect rcNew = pFormFiller->PWLtoFFL(rcWindow);
824		pFormFiller->SetWindowRect(pData->pPageView, rcWindow);
825
826		CPDF_Rect unRect = rcOld;
827		unRect.Union(rcNew);
828		//FX_RECT rcRect = unRect.GetOutterRect();
829		unRect.left = (FX_FLOAT)(unRect.left - 0.5);
830		unRect.right = (FX_FLOAT)(unRect.right + 0.5);
831		unRect.top = (FX_FLOAT)(unRect.top + 0.5);
832		unRect.bottom = (FX_FLOAT)(unRect.bottom -0.5);
833		m_pApp->FFI_Invalidate(pData->pWidget->GetPDFPage(), unRect.left, unRect.top, unRect.right, unRect.bottom);
834	}
835}
836
837void CFFL_IFormFiller::OnKeyStroke(FX_BOOL bEditOrList, void* pPrivateData, FX_INT32 nKeyCode, CFX_WideString& strChange,
838								   const CFX_WideString& strChangeEx, FX_BOOL bKeyDown,
839								   FX_BOOL & bRC, FX_BOOL & bExit)
840{
841	ASSERT(pPrivateData != NULL);
842	CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
843	ASSERT(pData->pWidget != NULL);
844
845	CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, FALSE);
846	ASSERT(pFormFiller != NULL);
847
848	pFormFiller->OnKeyStroke(bKeyDown);
849}
850
851void CFFL_IFormFiller::OnKeyStrokeCommit(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bRC, FX_BOOL& bExit, FX_DWORD nFlag)
852{
853	if (!m_bNotifying)
854	{
855		ASSERT(pWidget != NULL);
856		if (pWidget->GetAAction(CPDF_AAction::KeyStroke))
857		{
858			m_bNotifying = TRUE;
859			pWidget->ClearAppModified();
860
861			ASSERT(pPageView != NULL);
862// 			CReader_DocView* pDocView = pPageView->GetDocView();
863// 			ASSERT(pDocView != NULL);
864
865
866
867			PDFSDK_FieldAction fa;
868			fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
869 			fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
870			fa.bWillCommit = TRUE;
871			fa.nCommitKey = GetCommitKey();
872			fa.bKeyDown = GetKeyDown();
873			fa.bRC = TRUE;
874
875			CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE);
876			ASSERT(pFormFiller != NULL);
877
878			pFormFiller->GetActionData(pPageView, CPDF_AAction::KeyStroke, fa);
879			pFormFiller->SaveState(pPageView);
880
881			PDFSDK_FieldAction faOld = fa;
882			pWidget->OnAAction(CPDF_AAction::KeyStroke, fa, pPageView);
883
884			bRC = fa.bRC;
885//			bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
886
887			m_bNotifying = FALSE;
888		}
889	}
890}
891
892void CFFL_IFormFiller::OnValidate(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bRC, FX_BOOL& bExit, FX_DWORD nFlag)
893{
894	if (!m_bNotifying)
895	{
896		ASSERT(pWidget != NULL);
897		if (pWidget->GetAAction(CPDF_AAction::Validate))
898		{
899			m_bNotifying = TRUE;
900			pWidget->ClearAppModified();
901
902			ASSERT(pPageView != NULL);
903// 			CReader_DocView* pDocView = pPageView->GetDocView();
904// 			ASSERT(pDocView != NULL);
905
906
907
908			PDFSDK_FieldAction fa;
909			fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
910 			fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
911			fa.bKeyDown = GetKeyDown();
912			fa.bRC = TRUE;
913
914			CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, FALSE);
915			ASSERT(pFormFiller != NULL);
916
917			pFormFiller->GetActionData(pPageView, CPDF_AAction::Validate, fa);
918			pFormFiller->SaveState(pPageView);
919
920			PDFSDK_FieldAction faOld = fa;
921			pWidget->OnAAction(CPDF_AAction::Validate, fa, pPageView);
922
923			bRC = fa.bRC;
924//			bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
925
926			m_bNotifying = FALSE;
927		}
928	}
929}
930
931void CFFL_IFormFiller::OnCalculate(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bExit, FX_DWORD nFlag)
932{
933	if (!m_bNotifying)
934	{
935		ASSERT(pWidget != NULL);
936		ASSERT(pPageView != NULL);
937// 		CReader_DocView* pDocView = pPageView->GetDocView();
938// 		ASSERT(pDocView != NULL);
939		CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
940		ASSERT(pDocument != NULL);
941
942		CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
943		ASSERT(pInterForm != NULL);
944
945		pInterForm->OnCalculate(pWidget->GetFormField());
946
947//		bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
948
949		m_bNotifying = FALSE;
950	}
951}
952
953void CFFL_IFormFiller::OnFormat(CPDFSDK_Widget* pWidget, CPDFSDK_PageView* pPageView, FX_BOOL& bExit, FX_DWORD nFlag)
954{
955	if (!m_bNotifying)
956	{
957		ASSERT(pWidget != NULL);
958		ASSERT(pPageView != NULL);
959// 		CReader_DocView* pDocView = pPageView->GetDocView();
960// 		ASSERT(pDocView != NULL);
961		CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
962		ASSERT(pDocument != NULL);
963
964		CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
965		ASSERT(pInterForm != NULL);
966
967		FX_BOOL bFormated = FALSE;
968		CFX_WideString sValue = pInterForm->OnFormat(pWidget->GetFormField(), GetCommitKey(), bFormated);
969
970//		bExit = !IsValidAnnot(m_pApp, pDocument, pDocView, pPageView, pWidget);
971
972		if (bExit) return;
973
974		if (bFormated)
975		{
976			pInterForm->ResetFieldAppearance(pWidget->GetFormField(), sValue, TRUE);
977			pInterForm->UpdateField(pWidget->GetFormField());
978		}
979
980		m_bNotifying = FALSE;
981	}
982}
983
984// LRESULT CALLBACK CFFL_IFormFiller::FFL_WndProc(
985// 									  int code,       // hook code
986// 									  WPARAM wParam,  // virtual-key code
987// 									  LPARAM lParam   // keystroke-message information
988// 										)
989// {
990// 	if (code != HC_ACTION)
991// 	{
992// 		return CallNextHookEx (m_hookSheet, code, wParam, lParam);
993// 	}
994//
995// 	FXSYS_memcpy(&g_Msg, (void*)lParam, sizeof(MSG));
996//
997// 	return 0;
998// }
999
1000// MSG	CFFL_IFormFiller::GetLastMessage()
1001// {
1002// 	return g_Msg;
1003// }
1004
1005int CFFL_IFormFiller::GetCommitKey()
1006{
1007//	MSG msg = CFFL_IFormFiller::GetLastMessage();
1008
1009	int nCommitKey = 0;
1010// 	switch (msg.message)
1011// 	{
1012// 	case WM_LBUTTONDOWN:
1013// 	case WM_LBUTTONUP:
1014// 		nCommitKey = 1;
1015// 		break;
1016// 	case WM_KEYDOWN:
1017// 		switch (msg.wParam)
1018// 		{
1019// 		case VK_RETURN:
1020// 			nCommitKey = 2;
1021// 			break;
1022// 		case VK_TAB:
1023// 			nCommitKey = 3;
1024// 			break;
1025// 		}
1026// 		break;
1027// 	}
1028
1029	return nCommitKey;
1030}
1031
1032FX_BOOL CFFL_IFormFiller::GetKeyDown()
1033{
1034	return TRUE;
1035// 	MSG msg = CFFL_IFormFiller::GetLastMessage();
1036//
1037// 	return msg.message == WM_KEYDOWN || msg.message == WM_CHAR;
1038}
1039
1040FX_BOOL	CFFL_IFormFiller::IsValidAnnot(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot)
1041{
1042
1043	ASSERT(pPageView != NULL);
1044	ASSERT(pAnnot != NULL);
1045
1046	if(pPageView)
1047		return pPageView->IsValidAnnot(pAnnot->GetPDFAnnot());
1048	else
1049		return FALSE;
1050}
1051
1052void CFFL_IFormFiller::BeforeUndo(CPDFSDK_Document* pDocument)
1053{
1054
1055}
1056
1057void CFFL_IFormFiller::BeforeRedo(CPDFSDK_Document* pDocument)
1058{
1059	BeforeUndo(pDocument);
1060}
1061
1062void CFFL_IFormFiller::AfterUndo(CPDFSDK_Document* pDocument)
1063{
1064}
1065
1066void CFFL_IFormFiller::AfterRedo(CPDFSDK_Document* pDocument)
1067{
1068}
1069
1070FX_BOOL	CFFL_IFormFiller::CanCopy(CPDFSDK_Document* pDocument)
1071{
1072
1073	return FALSE;
1074}
1075
1076FX_BOOL	CFFL_IFormFiller::CanCut(CPDFSDK_Document* pDocument)
1077{
1078
1079	return FALSE;
1080}
1081
1082FX_BOOL	CFFL_IFormFiller::CanPaste(CPDFSDK_Document* pDocument)
1083{
1084
1085	return FALSE;
1086}
1087
1088void CFFL_IFormFiller::DoCopy(CPDFSDK_Document* pDocument)
1089{
1090}
1091
1092void CFFL_IFormFiller::DoCut(CPDFSDK_Document* pDocument)
1093{
1094}
1095
1096void CFFL_IFormFiller::DoPaste(CPDFSDK_Document* pDocument)
1097{
1098
1099}
1100void CFFL_IFormFiller::OnBeforeKeyStroke(FX_BOOL bEditOrList, void* pPrivateData, FX_INT32 nKeyCode,
1101											  CFX_WideString & strChange, const CFX_WideString& strChangeEx,
1102											  int nSelStart, int nSelEnd,
1103										FX_BOOL bKeyDown, FX_BOOL & bRC, FX_BOOL & bExit, FX_DWORD nFlag)
1104{
1105	ASSERT(pPrivateData != NULL);
1106	CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
1107	ASSERT(pData->pWidget != NULL);
1108
1109	CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, FALSE);
1110	ASSERT(pFormFiller != NULL);
1111
1112	if (!m_bNotifying)
1113	{
1114		if (pData->pWidget->GetAAction(CPDF_AAction::KeyStroke))
1115		{
1116			m_bNotifying = TRUE;
1117			int nAge = pData->pWidget->GetAppearanceAge();
1118			int nValueAge = pData->pWidget->GetValueAge();
1119
1120			ASSERT(pData->pPageView != NULL);
1121			CPDFSDK_Document* pDocument  = pData->pPageView->GetSDKDocument();
1122
1123			PDFSDK_FieldAction fa;
1124 			fa.bModifier = m_pApp->FFI_IsCTRLKeyDown(nFlag);
1125 			fa.bShift = m_pApp->FFI_IsSHIFTKeyDown(nFlag);
1126			fa.sChange = strChange;
1127			fa.sChangeEx = strChangeEx;
1128			fa.bKeyDown = bKeyDown;
1129			fa.bWillCommit = FALSE;
1130			fa.bRC = TRUE;
1131			fa.nSelStart = nSelStart;
1132			fa.nSelEnd = nSelEnd;
1133
1134
1135			pFormFiller->GetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa);
1136			pFormFiller->SaveState(pData->pPageView);
1137
1138			if (pData->pWidget->OnAAction(CPDF_AAction::KeyStroke, fa, pData->pPageView))
1139			{
1140				if (!IsValidAnnot(pData->pPageView, pData->pWidget))
1141				{
1142					bExit = TRUE;
1143					m_bNotifying = FALSE;
1144					return;
1145				}
1146
1147				if (nAge != pData->pWidget->GetAppearanceAge())
1148				{
1149					CPWL_Wnd* pWnd = pFormFiller->ResetPDFWindow(pData->pPageView, nValueAge == pData->pWidget->GetValueAge());
1150					pData = (CFFL_PrivateData*)pWnd->GetAttachedData();
1151					bExit = TRUE;
1152				}
1153
1154				if (fa.bRC)
1155				{
1156					pFormFiller->SetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa);
1157					bRC = FALSE;
1158				}
1159				else
1160				{
1161					pFormFiller->RestoreState(pData->pPageView);
1162					bRC = FALSE;
1163				}
1164
1165				if (pDocument->GetFocusAnnot() != pData->pWidget)
1166				{
1167					pFormFiller->CommitData(pData->pPageView,nFlag);
1168					bExit = TRUE;
1169				}
1170			}
1171			else
1172			{
1173				if (!IsValidAnnot(pData->pPageView, pData->pWidget))
1174				{
1175					bExit = TRUE;
1176					m_bNotifying = FALSE;
1177					return;
1178				}
1179			}
1180
1181			m_bNotifying = FALSE;
1182		}
1183	}
1184}
1185
1186void	CFFL_IFormFiller::OnAfterKeyStroke(FX_BOOL bEditOrList, void* pPrivateData, FX_BOOL & bExit,FX_DWORD nFlag)
1187{
1188	ASSERT(pPrivateData != NULL);
1189	CFFL_PrivateData* pData = (CFFL_PrivateData*)pPrivateData;
1190	ASSERT(pData->pWidget != NULL);
1191
1192	CFFL_FormFiller* pFormFiller = GetFormFiller(pData->pWidget, FALSE);
1193	ASSERT(pFormFiller != NULL);
1194
1195	if (!bEditOrList)
1196		pFormFiller->OnKeyStroke(bExit);
1197}
1198