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/fsdk_define.h"
8#include "../include/fsdk_mgr.h"
9#include "../include/formfiller/FFL_FormFiller.h"
10#include "../include/fsdk_annothandler.h"
11
12
13CPDFSDK_AnnotHandlerMgr::CPDFSDK_AnnotHandlerMgr(CPDFDoc_Environment* pApp)
14{
15	m_pApp = pApp;
16
17	CPDFSDK_BFAnnotHandler* pHandler = new CPDFSDK_BFAnnotHandler(m_pApp);
18	pHandler->SetFormFiller(m_pApp->GetIFormFiller());
19	RegisterAnnotHandler(pHandler);
20}
21
22CPDFSDK_AnnotHandlerMgr::~CPDFSDK_AnnotHandlerMgr()
23{
24	for(int i=0; i<m_Handlers.GetSize(); i++)
25	{
26		IPDFSDK_AnnotHandler* pHandler = m_Handlers.GetAt(i);
27		delete pHandler;
28	}
29	m_Handlers.RemoveAll();
30	m_mapType2Handler.RemoveAll();
31}
32
33void	CPDFSDK_AnnotHandlerMgr::RegisterAnnotHandler(IPDFSDK_AnnotHandler* pAnnotHandler)
34{
35	ASSERT(pAnnotHandler != NULL);
36
37	ASSERT(GetAnnotHandler(pAnnotHandler->GetType()) == NULL);
38
39	m_Handlers.Add(pAnnotHandler);
40	m_mapType2Handler.SetAt(pAnnotHandler->GetType(), (void*)pAnnotHandler);
41}
42
43void CPDFSDK_AnnotHandlerMgr::UnRegisterAnnotHandler(IPDFSDK_AnnotHandler* pAnnotHandler)
44{
45	ASSERT(pAnnotHandler != NULL);
46
47	m_mapType2Handler.RemoveKey(pAnnotHandler->GetType());
48
49	for (int i=0, sz=m_Handlers.GetSize(); i<sz; i++)
50	{
51		if (m_Handlers.GetAt(i) == pAnnotHandler)
52		{
53			m_Handlers.RemoveAt(i);
54			break;
55		}
56	}
57}
58
59CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::NewAnnot(CPDF_Annot * pAnnot, CPDFSDK_PageView *pPageView)
60{
61	ASSERT(pAnnot != NULL);
62	ASSERT(pPageView != NULL);
63
64	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot->GetSubType()))
65	{
66		return pAnnotHandler->NewAnnot(pAnnot, pPageView);
67	}
68
69	return new CPDFSDK_Annot(pAnnot, pPageView);
70}
71
72void CPDFSDK_AnnotHandlerMgr::ReleaseAnnot(CPDFSDK_Annot* pAnnot)
73{
74	ASSERT(pAnnot != NULL);
75
76	pAnnot->GetPDFPage();
77
78	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
79	{
80		pAnnotHandler->OnRelease(pAnnot);
81		pAnnotHandler->ReleaseAnnot(pAnnot);
82	}
83	else
84	{
85		delete (CPDFSDK_Annot*)pAnnot;
86	}
87}
88
89void CPDFSDK_AnnotHandlerMgr::Annot_OnCreate(CPDFSDK_Annot* pAnnot)
90{
91	ASSERT(pAnnot != NULL);
92
93	CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
94	ASSERT(pPDFAnnot != NULL);
95	ASSERT(pPDFAnnot->m_pAnnotDict != NULL);
96
97	CPDFSDK_DateTime curTime;
98	pPDFAnnot->m_pAnnotDict->SetAtString("M", curTime.ToPDFDateTimeString());
99	pPDFAnnot->m_pAnnotDict->SetAtNumber("F", (int)0);
100
101	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
102	{
103		pAnnotHandler->OnCreate(pAnnot);
104	}
105}
106
107void CPDFSDK_AnnotHandlerMgr::Annot_OnLoad(CPDFSDK_Annot* pAnnot)
108{
109	ASSERT(pAnnot != NULL);
110
111	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
112	{
113		pAnnotHandler->OnLoad(pAnnot);
114	}
115}
116
117IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(CPDFSDK_Annot* pAnnot) const
118{
119	ASSERT(pAnnot != NULL);
120
121	CPDF_Annot* pPDFAnnot = pAnnot->GetPDFAnnot();
122	ASSERT(pPDFAnnot != NULL);
123
124	return GetAnnotHandler(pPDFAnnot->GetSubType());
125}
126
127IPDFSDK_AnnotHandler* CPDFSDK_AnnotHandlerMgr::GetAnnotHandler(const CFX_ByteString& sType) const
128{
129	void* pRet = NULL;
130	m_mapType2Handler.Lookup(sType, pRet);
131	return (IPDFSDK_AnnotHandler*)pRet;
132}
133
134void CPDFSDK_AnnotHandlerMgr::Annot_OnDraw(CPDFSDK_PageView* pPageView, CPDFSDK_Annot* pAnnot, CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,FX_DWORD dwFlags)
135{
136	ASSERT(pAnnot != NULL);
137
138	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
139	{
140		pAnnotHandler->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
141	}
142	else
143	{
144		pAnnot->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
145	}
146}
147
148
149FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDown(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
150{
151	ASSERT(pAnnot != NULL);
152
153	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
154	{
155		return pAnnotHandler->OnLButtonDown(pPageView, pAnnot, nFlags, point);
156	}
157	return FALSE;
158}
159FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonUp(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
160 {
161	ASSERT(pAnnot != NULL);
162
163	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
164	{
165		return pAnnotHandler->OnLButtonUp(pPageView, pAnnot, nFlags, point);
166	}
167	return FALSE;
168 }
169FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnLButtonDblClk(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
170{
171	ASSERT(pAnnot != NULL);
172
173	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
174	{
175		return pAnnotHandler->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
176	}
177	return FALSE;
178}
179FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseMove(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
180{
181	ASSERT(pAnnot != NULL);
182
183	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
184	{
185		return pAnnotHandler->OnMouseMove(pPageView, pAnnot, nFlags, point);
186	}
187	return FALSE;
188}
189FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnMouseWheel(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, short zDelta, const CPDF_Point& point)
190{
191	ASSERT(pAnnot != NULL);
192
193	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
194	{
195		return pAnnotHandler->OnMouseWheel(pPageView, pAnnot,nFlags,zDelta, point);
196	}
197	return FALSE;
198}
199FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonDown(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
200{
201	ASSERT(pAnnot != NULL);
202
203	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
204	{
205		return pAnnotHandler->OnRButtonDown(pPageView, pAnnot, nFlags, point);
206	}
207	return FALSE;
208}
209FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnRButtonUp(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
210{
211	ASSERT(pAnnot != NULL);
212
213	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
214	{
215		return pAnnotHandler->OnRButtonUp(pPageView, pAnnot, nFlags, point);
216	}
217	return FALSE;
218}
219
220void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseEnter(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
221{
222	ASSERT(pAnnot != NULL);
223
224	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
225	{
226		pAnnotHandler->OnMouseEnter(pPageView, pAnnot, nFlag);
227	}
228	return ;
229}
230
231void CPDFSDK_AnnotHandlerMgr::Annot_OnMouseExit(CPDFSDK_PageView * pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
232{
233	ASSERT(pAnnot != NULL);
234
235	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
236	{
237		pAnnotHandler->OnMouseExit(pPageView, pAnnot, nFlag);
238	}
239	return;
240}
241
242FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnChar(CPDFSDK_Annot* pAnnot, FX_DWORD nChar, FX_DWORD nFlags)
243{
244
245	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
246	{
247		return pAnnotHandler->OnChar(pAnnot,nChar, nFlags);
248	}
249	return FALSE;
250
251}
252
253FX_BOOL			CPDFSDK_AnnotHandlerMgr::Annot_OnKeyDown(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
254{
255
256	if (!m_pApp->FFI_IsCTRLKeyDown(nFlag) && !m_pApp->FFI_IsALTKeyDown(nFlag))
257	{
258		CPDFSDK_PageView* pPage = pAnnot->GetPageView();
259		CPDFSDK_Annot* pFocusAnnot = pPage->GetFocusAnnot();
260		if (pFocusAnnot && (nKeyCode == FWL_VKEY_Tab))
261		{
262			CPDFSDK_Annot* pNext = GetNextAnnot(pFocusAnnot, !m_pApp->FFI_IsSHIFTKeyDown(nFlag));
263
264			if(pNext && pNext != pFocusAnnot)
265			{
266				CPDFSDK_Document* pDocument = pPage->GetSDKDocument();
267				pDocument->SetFocusAnnot(pNext);
268				return TRUE;
269			}
270		}
271	}
272
273	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
274	{
275		return pAnnotHandler->OnKeyDown(pAnnot,nKeyCode, nFlag);
276	}
277	return FALSE;
278}
279FX_BOOL			CPDFSDK_AnnotHandlerMgr::Annot_OnKeyUp(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
280{
281	return FALSE;
282}
283
284FX_BOOL			CPDFSDK_AnnotHandlerMgr::Annot_OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
285{
286	ASSERT(pAnnot != NULL);
287
288	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
289	{
290		if (pAnnotHandler->OnSetFocus(pAnnot, nFlag))
291		{
292			CPDFSDK_PageView* pPage = pAnnot->GetPageView();
293			ASSERT(pPage != NULL);
294
295			pPage->GetSDKDocument();
296	//		pDocument->SetTopmostAnnot(pAnnot);
297
298			return TRUE;
299		}
300		else
301		{
302			return FALSE;
303		}
304	}
305
306	return FALSE;
307}
308
309FX_BOOL			CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
310{
311	ASSERT(pAnnot != NULL);
312
313	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
314	{
315		if (pAnnotHandler->OnKillFocus(pAnnot, nFlag))
316		{
317			return TRUE;
318		}
319		else
320			return FALSE;
321	}
322
323	return FALSE;
324}
325
326CPDF_Rect	CPDFSDK_AnnotHandlerMgr::Annot_OnGetViewBBox(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot)
327{
328	ASSERT(pAnnot);
329	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
330	{
331		return pAnnotHandler->GetViewBBox(pPageView, pAnnot);
332	}
333	return pAnnot->GetRect();
334}
335
336FX_BOOL	CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, const CPDF_Point& point)
337{
338	ASSERT(pAnnot);
339	if (IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot))
340	{
341		if(pAnnotHandler->CanAnswer(pAnnot))
342			return pAnnotHandler->HitTest(pPageView, pAnnot, point);
343	}
344	return FALSE;
345}
346
347CPDFSDK_Annot*	CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,FX_BOOL bNext)
348{
349	 CBA_AnnotIterator ai(pSDKAnnot->GetPageView(), "Widget", "");
350
351	 CPDFSDK_Annot* pNext = bNext ?
352		 ai.GetNextAnnot(pSDKAnnot) :
353		ai.GetPrevAnnot(pSDKAnnot);
354
355		return pNext;
356}
357
358FX_BOOL CPDFSDK_BFAnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot)
359{
360	ASSERT(pAnnot);
361	ASSERT(pAnnot->GetType() == "Widget");
362	CFX_ByteString sSubType = pAnnot->GetSubType();
363
364	if (sSubType == BFFT_SIGNATURE)
365	{
366	}
367	else
368	{
369		CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
370		if (!pWidget->IsVisible()) return FALSE;
371
372		int nFieldFlags = pWidget->GetFieldFlags();
373		if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY) return FALSE;
374		if (pWidget->GetFieldType() == FIELDTYPE_PUSHBUTTON)
375			return TRUE;
376		else
377		{
378			CPDF_Page* pPage = pWidget->GetPDFPage();
379			ASSERT(pPage != NULL);
380
381			CPDF_Document* pDocument = pPage->m_pDocument;
382			ASSERT(pDocument != NULL);
383
384			FX_DWORD dwPermissions = pDocument->GetUserPermissions();
385			return (dwPermissions&FPDFPERM_FILL_FORM) ||
386				(dwPermissions&FPDFPERM_ANNOT_FORM) ||
387 			(dwPermissions&FPDFPERM_ANNOT_FORM);
388		}
389	}
390
391	return FALSE;
392}
393
394CPDFSDK_Annot*		CPDFSDK_BFAnnotHandler::NewAnnot(CPDF_Annot* pAnnot, CPDFSDK_PageView* pPage)
395{
396	ASSERT(pPage != NULL);
397	pPage->GetPDFDocument();
398
399	CPDFSDK_Document* pSDKDoc  = m_pApp->GetCurrentDoc();
400	ASSERT(pSDKDoc);
401	CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pSDKDoc->GetInterForm();
402	ASSERT(pInterForm != NULL);
403
404	CPDFSDK_Widget* pWidget = NULL;
405	if (CPDF_FormControl* pCtrl = CPDFSDK_Widget::GetFormControl(pInterForm->GetInterForm(), pAnnot->m_pAnnotDict))
406	{
407		pWidget = new CPDFSDK_Widget(pAnnot, pPage, pInterForm);
408		pInterForm->AddMap(pCtrl, pWidget);
409		CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
410		if(pPDFInterForm && pPDFInterForm->NeedConstructAP())
411			pWidget->ResetAppearance(NULL,FALSE);
412	}
413
414	return pWidget;
415}
416
417void CPDFSDK_BFAnnotHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot)
418{
419	ASSERT(pAnnot != NULL);
420
421	if (m_pFormFiller)
422		m_pFormFiller->OnDelete(pAnnot);
423
424	CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
425	CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm();
426	ASSERT(pInterForm != NULL);
427
428	CPDF_FormControl* pCtrol = pWidget->GetFormControl();
429	pInterForm->RemoveMap(pCtrol);
430
431
432	delete pWidget;
433}
434
435
436void CPDFSDK_BFAnnotHandler::OnDraw(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,  FX_DWORD dwFlags)
437{
438	ASSERT(pAnnot != NULL);
439	CFX_ByteString sSubType = pAnnot->GetSubType();
440
441	if (sSubType == BFFT_SIGNATURE)
442	{
443		pAnnot->DrawAppearance(pDevice, pUser2Device, CPDF_Annot::Normal, NULL);
444	}
445	else
446	{
447		if (m_pFormFiller)
448		{
449			m_pFormFiller->OnDraw(pPageView, pAnnot, pDevice, pUser2Device, dwFlags);
450		}
451	}
452}
453
454void CPDFSDK_BFAnnotHandler::OnMouseEnter(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
455{
456	ASSERT(pAnnot != NULL);
457	CFX_ByteString sSubType = pAnnot->GetSubType();
458
459	if (sSubType == BFFT_SIGNATURE)
460	{
461	}
462	else
463	{
464		if (m_pFormFiller)
465			 m_pFormFiller->OnMouseEnter(pPageView, pAnnot, nFlag);
466	}
467
468
469}
470void CPDFSDK_BFAnnotHandler::OnMouseExit(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
471{
472	ASSERT(pAnnot != NULL);
473	CFX_ByteString sSubType = pAnnot->GetSubType();
474
475	if (sSubType == BFFT_SIGNATURE)
476	{
477	}
478	else
479	{
480		if (m_pFormFiller)
481			 m_pFormFiller->OnMouseExit(pPageView, pAnnot, nFlag);
482	}
483
484}
485FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonDown(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
486{
487	ASSERT(pAnnot != NULL);
488	CFX_ByteString sSubType = pAnnot->GetSubType();
489
490	if (sSubType == BFFT_SIGNATURE)
491	{
492	}
493	else
494	{
495		if (m_pFormFiller)
496			return m_pFormFiller->OnLButtonDown(pPageView, pAnnot, nFlags, point);
497	}
498
499	return FALSE;
500}
501
502FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
503{
504	ASSERT(pAnnot != NULL);
505	CFX_ByteString sSubType = pAnnot->GetSubType();
506
507	if (sSubType == BFFT_SIGNATURE)
508	{
509	}
510	else
511	{
512		if (m_pFormFiller)
513			return m_pFormFiller->OnLButtonUp(pPageView, pAnnot, nFlags, point);
514	}
515
516	return FALSE;
517}
518
519FX_BOOL CPDFSDK_BFAnnotHandler::OnLButtonDblClk(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
520{
521	ASSERT(pAnnot != NULL);
522	CFX_ByteString sSubType = pAnnot->GetSubType();
523
524	if (sSubType == BFFT_SIGNATURE)
525	{
526	}
527	else
528	{
529		if (m_pFormFiller)
530			return m_pFormFiller->OnLButtonDblClk(pPageView, pAnnot, nFlags, point);
531	}
532
533	return FALSE;
534}
535
536FX_BOOL CPDFSDK_BFAnnotHandler::OnMouseMove(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
537{
538	ASSERT(pAnnot != NULL);
539	CFX_ByteString sSubType = pAnnot->GetSubType();
540
541	if (sSubType == BFFT_SIGNATURE)
542	{
543	}
544	else
545	{
546		if (m_pFormFiller)
547			return m_pFormFiller->OnMouseMove(pPageView, pAnnot, nFlags, point);
548	}
549
550	return FALSE;
551
552}
553
554
555FX_BOOL CPDFSDK_BFAnnotHandler::OnMouseWheel(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, short zDelta, const CPDF_Point& point)
556{
557	ASSERT(pAnnot != NULL);
558	CFX_ByteString sSubType = pAnnot->GetSubType();
559
560	if (sSubType == BFFT_SIGNATURE)
561	{
562	}
563	else
564	{
565		if (m_pFormFiller)
566			return m_pFormFiller->OnMouseWheel(pPageView, pAnnot, nFlags, zDelta,point);
567	}
568
569	return FALSE;
570}
571
572FX_BOOL CPDFSDK_BFAnnotHandler::OnRButtonDown(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
573{
574	ASSERT(pAnnot != NULL);
575	CFX_ByteString sSubType = pAnnot->GetSubType();
576
577	if (sSubType == BFFT_SIGNATURE)
578	{
579	}
580	else
581	{
582		if (m_pFormFiller)
583			return m_pFormFiller->OnRButtonDown(pPageView, pAnnot, nFlags, point);
584	}
585
586	return FALSE;
587}
588FX_BOOL CPDFSDK_BFAnnotHandler::OnRButtonUp(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, FX_DWORD nFlags, const CPDF_Point& point)
589{
590	ASSERT(pAnnot != NULL);
591	CFX_ByteString sSubType = pAnnot->GetSubType();
592
593	if (sSubType == BFFT_SIGNATURE)
594	{
595	}
596	else
597	{
598		if (m_pFormFiller)
599			return m_pFormFiller->OnRButtonUp(pPageView, pAnnot, nFlags, point);
600	}
601
602	return FALSE;
603}
604
605FX_BOOL CPDFSDK_BFAnnotHandler::OnChar(CPDFSDK_Annot* pAnnot, FX_DWORD nChar, FX_DWORD nFlags)
606{
607	ASSERT(pAnnot != NULL);
608	CFX_ByteString sSubType = pAnnot->GetSubType();
609
610	if (sSubType == BFFT_SIGNATURE)
611	{
612	}
613	else
614	{
615		if (m_pFormFiller)
616			return m_pFormFiller->OnChar(pAnnot,nChar, nFlags);
617	}
618
619	return FALSE;
620}
621
622FX_BOOL CPDFSDK_BFAnnotHandler::OnKeyDown(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
623{
624	ASSERT(pAnnot != NULL);
625	CFX_ByteString sSubType = pAnnot->GetSubType();
626
627	if (sSubType == BFFT_SIGNATURE)
628	{
629	}
630	else
631	{
632		if (m_pFormFiller)
633			return m_pFormFiller->OnKeyDown(pAnnot,nKeyCode, nFlag);
634	}
635
636	return FALSE;
637}
638
639FX_BOOL CPDFSDK_BFAnnotHandler::OnKeyUp(CPDFSDK_Annot* pAnnot, int nKeyCode, int nFlag)
640{
641
642	return FALSE;
643}
644void	CPDFSDK_BFAnnotHandler::OnCreate(CPDFSDK_Annot* pAnnot)
645{
646	ASSERT(pAnnot != NULL);
647	CFX_ByteString sSubType = pAnnot->GetSubType();
648
649	if (sSubType == BFFT_SIGNATURE)
650	{
651	}
652	else
653	{
654		if (m_pFormFiller)
655			m_pFormFiller->OnCreate(pAnnot);
656	}
657}
658
659void CPDFSDK_BFAnnotHandler::OnLoad(CPDFSDK_Annot* pAnnot)
660{
661	ASSERT(pAnnot != NULL);
662
663	CFX_ByteString sSubType = pAnnot->GetSubType();
664
665	if (sSubType == BFFT_SIGNATURE)
666	{
667	}
668	else
669	{
670		CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
671		if (!pWidget->IsAppearanceValid())
672			pWidget->ResetAppearance(NULL, FALSE);
673
674		int nFieldType = pWidget->GetFieldType();
675		if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX)
676		{
677			FX_BOOL bFormated = FALSE;
678			CFX_WideString sValue = pWidget->OnFormat(0, bFormated);
679			if (bFormated && nFieldType == FIELDTYPE_COMBOBOX)
680			{
681				pWidget->ResetAppearance(sValue.c_str(), FALSE);
682			}
683		}
684
685		if (m_pFormFiller)
686			m_pFormFiller->OnLoad(pAnnot);
687	}
688}
689
690FX_BOOL	CPDFSDK_BFAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
691{
692	ASSERT(pAnnot != NULL);
693	CFX_ByteString sSubType = pAnnot->GetSubType();
694
695	if (sSubType == BFFT_SIGNATURE)
696	{
697	}
698	else
699	{
700		if (m_pFormFiller)
701			return m_pFormFiller->OnSetFocus(pAnnot,nFlag);
702	}
703
704	return TRUE;
705}
706FX_BOOL	CPDFSDK_BFAnnotHandler::OnKillFocus(CPDFSDK_Annot* pAnnot, FX_DWORD nFlag)
707{
708	ASSERT(pAnnot != NULL);
709	CFX_ByteString sSubType = pAnnot->GetSubType();
710
711	if (sSubType == BFFT_SIGNATURE)
712	{
713	}
714	else
715	{
716		if (m_pFormFiller)
717			return m_pFormFiller->OnKillFocus(pAnnot,nFlag);
718	}
719
720	return TRUE;
721}
722
723CPDF_Rect CPDFSDK_BFAnnotHandler::GetViewBBox(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot)
724{
725	ASSERT(pAnnot != NULL);
726	CFX_ByteString sSubType = pAnnot->GetSubType();
727
728	if (sSubType == BFFT_SIGNATURE)
729	{
730	}
731	else
732	{
733		if (m_pFormFiller)
734			return m_pFormFiller->GetViewBBox(pPageView, pAnnot);
735
736	}
737
738	return CPDF_Rect(0,0,0,0);
739}
740
741FX_BOOL	CPDFSDK_BFAnnotHandler::HitTest(CPDFSDK_PageView *pPageView, CPDFSDK_Annot* pAnnot, const CPDF_Point& point)
742{
743	ASSERT(pPageView);
744	ASSERT(pAnnot);
745
746	CPDF_Rect rect = GetViewBBox(pPageView, pAnnot);
747	return rect.Contains(point.x, point.y);
748}
749
750//CReader_AnnotIteratorEx
751
752CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(CPDFSDK_PageView * pPageView,FX_BOOL bReverse,
753												 FX_BOOL bIgnoreTopmost/*=FALSE*/,
754												 FX_BOOL bCircle/*=FALSE*/,
755												 CFX_PtrArray *pList/*=NULL*/)
756{
757	ASSERT(pPageView);
758	m_bReverse=bReverse;
759	m_bIgnoreTopmost= bIgnoreTopmost;
760	m_bCircle=bCircle;
761	m_pIteratorAnnotList.RemoveAll();
762	InitIteratorAnnotList(pPageView,pList);
763}
764
765CPDFSDK_Annot*	CPDFSDK_AnnotIterator::NextAnnot (const CPDFSDK_Annot* pCurrent)
766{
767
768	int index=-1;
769	int nCount=this->m_pIteratorAnnotList.GetSize();
770	if(pCurrent){
771		for(int i=0;i<nCount;i++){
772			CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot *)m_pIteratorAnnotList.GetAt(i);
773			if(pReaderAnnot ==pCurrent){
774				index=i;
775				break;
776			}
777		}
778	}
779	return NextAnnot(index);
780}
781CPDFSDK_Annot*	CPDFSDK_AnnotIterator::PrevAnnot (const CPDFSDK_Annot*pCurrent)
782{
783
784	int index=-1;
785	int nCount=this->m_pIteratorAnnotList.GetSize();
786	if(pCurrent){
787		for(int i=0;i<nCount;i++){
788			CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
789			if(pReaderAnnot ==pCurrent){
790				index=i;
791				break;
792			}
793		}
794	}
795	return PrevAnnot(index);
796}
797CPDFSDK_Annot*	CPDFSDK_AnnotIterator::NextAnnot (int& index)
798{
799
800	int nCount=m_pIteratorAnnotList.GetSize();
801    if(nCount<=0) index=-1;
802    else{
803		if(index<0){
804			index=0;
805		}
806		else{
807			if(m_bCircle){
808				index=( index <nCount-1) ? (index+1) :0;
809			}
810			else{
811				index=( index <nCount-1) ? (index+1) :-1;
812			}
813
814		}
815	}
816	return (index <0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);
817}
818
819
820CPDFSDK_Annot*	CPDFSDK_AnnotIterator::PrevAnnot (int& index)
821{
822
823	int nCount=m_pIteratorAnnotList.GetSize();
824    if(nCount<=0) index=-1;
825	else{
826		if(index<0){
827			index=nCount-1;
828		}
829		else{
830			if(m_bCircle){
831				index = ( index >0) ? (index-1) :nCount-1;
832			}
833			else{
834				index = ( index >0) ? (index-1) :-1;
835			}
836		}
837	}
838	return (index <0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);
839}
840
841
842CPDFSDK_Annot*CPDFSDK_AnnotIterator::Next(const CPDFSDK_Annot* pCurrent)
843{
844
845	return (m_bReverse) ? PrevAnnot(pCurrent):NextAnnot(pCurrent);
846
847}
848
849CPDFSDK_Annot*	CPDFSDK_AnnotIterator::Prev(const CPDFSDK_Annot* pCurrent)
850{
851
852	return (m_bReverse) ? NextAnnot(pCurrent):PrevAnnot(pCurrent);
853}
854
855CPDFSDK_Annot*CPDFSDK_AnnotIterator::Next(int& index )
856{
857
858	return (m_bReverse) ? PrevAnnot(index):NextAnnot(index);
859
860}
861
862CPDFSDK_Annot*	CPDFSDK_AnnotIterator::Prev(int& index )
863{
864
865	return (m_bReverse) ? NextAnnot(index):PrevAnnot(index);
866}
867
868
869void CPDFSDK_AnnotIterator::InsertSort(CFX_PtrArray &arrayList, AI_COMPARE pCompare)
870{
871	for (int i = 1; i < arrayList.GetSize(); i++)
872	{
873		if (pCompare((CPDFSDK_Annot*)(arrayList[i]) , (CPDFSDK_Annot*)(arrayList[i-1])) < 0)
874		{
875			int j = i-1;
876			CPDFSDK_Annot* pTemp = (CPDFSDK_Annot*)arrayList[i];
877
878			do
879			{
880				arrayList[j + 1] = arrayList[j];
881			} while (--j >= 0 && pCompare(pTemp, (CPDFSDK_Annot*)arrayList[j]) < 0);
882
883			arrayList[j+1] = pTemp;
884		}
885	}
886}
887
888int LyOrderCompare(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2)
889{
890	if(p1->GetLayoutOrder() < p2->GetLayoutOrder())
891		return -1;
892	else if (p1->GetLayoutOrder() == p2->GetLayoutOrder())
893		return 0;
894	else
895		return 1;
896}
897
898FX_BOOL CPDFSDK_AnnotIterator::InitIteratorAnnotList(CPDFSDK_PageView* pPageView,CFX_PtrArray * pAnnotList)
899{
900	ASSERT(pPageView);
901
902
903
904	if(pAnnotList==NULL){
905		pAnnotList=pPageView->GetAnnotList();
906	}
907
908	this->m_pIteratorAnnotList.RemoveAll();
909	if(!pAnnotList) return FALSE;
910
911	CPDFSDK_Annot * pTopMostAnnot= (m_bIgnoreTopmost) ? NULL : pPageView->GetFocusAnnot();
912
913
914	int nCount =pAnnotList->GetSize();
915
916	for(int i = nCount- 1 ;i >= 0;i--)
917	{
918		CPDFSDK_Annot * pReaderAnnot= (CPDFSDK_Annot*)pAnnotList->GetAt(i);
919		m_pIteratorAnnotList.Add(pReaderAnnot);
920	}
921
922	InsertSort(m_pIteratorAnnotList,&LyOrderCompare);
923
924	if(pTopMostAnnot)
925	{
926		for(int i=0 ;i<nCount;i++)
927		{
928			CPDFSDK_Annot * pReaderAnnot = (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
929			if(pReaderAnnot == pTopMostAnnot)
930			{
931				m_pIteratorAnnotList.RemoveAt(i);
932				m_pIteratorAnnotList.InsertAt(0, pReaderAnnot);
933				break;
934			}
935		}
936	}
937
938	return TRUE;
939}
940
941