ad27aeb92f4b1b198a615ca1c722206e3158d3e6
[pdfium.git] / fpdfsdk / src / fsdk_mgr.cpp
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 "../../public/fpdf_ext.h"
8 #include "../include/fsdk_define.h"
9 #include "../include/fsdk_mgr.h"
10 #include "../include/formfiller/FFL_FormFiller.h"
11 #include "../include/javascript/IJavaScript.h"
12
13 #if _FX_OS_ == _FX_ANDROID_
14 #include "time.h"
15 #else
16 #include <ctime>
17 #endif
18
19 //extern CPDFDoc_Environment* g_pFormFillApp;
20 class CFX_SystemHandler:public IFX_SystemHandler
21 {
22 public:
23         CFX_SystemHandler(CPDFDoc_Environment* pEnv):m_pEnv(pEnv),m_nCharSet(-1) {}
24 public:
25         virtual void                            InvalidateRect(FX_HWND hWnd, FX_RECT rect) ;
26         virtual void                            OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect);
27
28         virtual FX_BOOL                         IsSelectionImplemented();
29
30         virtual CFX_WideString          GetClipboardText(FX_HWND hWnd){return L"";}
31         virtual FX_BOOL                         SetClipboardText(FX_HWND hWnd, CFX_WideString string) {return FALSE;}
32
33         virtual void                            ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) {}
34         virtual void                            ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) {}
35
36         /*cursor style
37         FXCT_ARROW
38         FXCT_NESW
39         FXCT_NWSE
40         FXCT_VBEAM
41         FXCT_HBEAM
42         FXCT_HAND
43         */
44         virtual void                            SetCursor(int32_t nCursorType);
45
46         virtual FX_HMENU                        CreatePopupMenu() {return NULL;}
47         virtual FX_BOOL                         AppendMenuItem(FX_HMENU hMenu, int32_t nIDNewItem, CFX_WideString string) {return FALSE;}
48         virtual FX_BOOL                         EnableMenuItem(FX_HMENU hMenu, int32_t nIDItem, FX_BOOL bEnabled) {return FALSE;}
49         virtual int32_t                 TrackPopupMenu(FX_HMENU hMenu, int32_t x, int32_t y, FX_HWND hParent) {return -1;}
50         virtual void                            DestroyMenu(FX_HMENU hMenu) {}
51
52         virtual CFX_ByteString          GetNativeTrueTypeFont(int32_t nCharset);
53         virtual FX_BOOL                         FindNativeTrueTypeFont(int32_t nCharset, CFX_ByteString sFontFaceName);
54         virtual CPDF_Font*                      AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc, CFX_ByteString sFontFaceName, uint8_t nCharset);
55
56         virtual int32_t                 SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) ;
57         virtual void                            KillTimer(int32_t nID) ;
58
59
60         virtual FX_BOOL                         IsSHIFTKeyDown(FX_DWORD nFlag) {return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);}
61         virtual FX_BOOL                         IsCTRLKeyDown(FX_DWORD nFlag) {return m_pEnv->FFI_IsCTRLKeyDown(nFlag);}
62         virtual FX_BOOL                         IsALTKeyDown(FX_DWORD nFlag) {return m_pEnv->FFI_IsALTKeyDown(nFlag);}
63         virtual FX_BOOL                         IsINSERTKeyDown(FX_DWORD nFlag) {return m_pEnv->FFI_IsINSERTKeyDown(nFlag);}
64
65         virtual FX_SYSTEMTIME           GetLocalTime();
66
67         virtual int32_t                 GetCharSet() {return m_nCharSet;}
68         virtual void                            SetCharSet(int32_t nCharSet) {m_nCharSet = nCharSet;}
69 private:
70         CPDFDoc_Environment* m_pEnv;
71         int             m_nCharSet;
72 };
73
74 void CFX_SystemHandler::SetCursor(int32_t nCursorType)
75 {
76
77         m_pEnv->FFI_SetCursor(nCursorType);
78 }
79
80 void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect)
81 {
82         //g_pFormFillApp->FFI_Invalidate();
83         CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
84         CPDF_Page* pPage = NULL;
85         CPDFSDK_PageView* pPageView = NULL;
86         pPageView = pSDKAnnot->GetPageView();
87         pPage = pSDKAnnot->GetPDFPage();
88         if(!pPage || !pPageView)
89                 return;
90         CPDF_Matrix page2device;
91         pPageView->GetCurrentMatrix(page2device);
92         CPDF_Matrix device2page;
93         device2page.SetReverse(page2device);
94         FX_FLOAT left, top, right,bottom;
95         device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
96         device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right, bottom);
97 //      m_pEnv->FFI_DeviceToPage(pPage, rect.left, rect.top, (double*)&left, (double*)&top);
98 //      m_pEnv->FFI_DeviceToPage(pPage, rect.right, rect.bottom, (double*)&right, (double*)&bottom);
99         CPDF_Rect rcPDF(left, bottom, right, top);
100         rcPDF.Normalize();
101
102         m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right, rcPDF.bottom);
103 }
104 void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect)
105 {
106         CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
107         if(pFFL)
108         {
109                 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
110                 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
111                 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
112                 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
113
114
115                 CPDFSDK_Annot* pAnnot  = pFFL->GetSDKAnnot();
116                 ASSERT(pAnnot);
117                 CPDF_Page* pPage = pAnnot->GetPDFPage();
118                 ASSERT(pPage);
119                 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
120         }
121
122 }
123
124 FX_BOOL CFX_SystemHandler::IsSelectionImplemented()
125 {
126         if(m_pEnv)
127         {
128                 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
129                 if(pInfo && pInfo->FFI_OutputSelectedRect)
130                         return TRUE;
131         }
132         return FALSE;
133 }
134
135 CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset)
136 {
137         return "";
138 }
139
140 FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(int32_t nCharset, CFX_ByteString sFontFaceName)
141 {
142         CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
143 //      FXFT_Face nFace = pFontMgr->FindSubstFont(sFontFaceName,TRUE,0,0,0,0,NULL);
144 //      FXFT_Face nFace  = pFontMgr->m_pBuiltinMapper->FindSubstFont(sFontFaceName,TRUE,0,0,0,0,NULL);
145
146         if(pFontMgr)
147         {
148                 CFX_FontMapper* pFontMapper = pFontMgr->m_pBuiltinMapper;
149                 if(pFontMapper)
150                 {
151                         int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
152                         if(nSize ==0)
153                         {
154                                 pFontMapper->LoadInstalledFonts();
155                                 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
156                         }
157
158                         for(int i=0; i<nSize; i++)
159                         {
160                                 if(pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
161                                         return TRUE;
162                         }
163                 }
164
165         }
166
167         return FALSE;
168 //      pFontMgr->m_FaceMap.Lookup(sFontFaceName,pFont);
169 //      return (pFont!=NULL);
170 }
171
172 static int CharSet2CP(int charset)
173 {
174         if(charset == 128)
175                 return 932;
176         else if(charset == 134)
177                 return 936;
178         else if(charset == 129)
179                 return 949;
180         else if(charset == 136)
181                 return 950;
182         return 0;
183 }
184 CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc, CFX_ByteString sFontFaceName,
185                                                                                                                  uint8_t nCharset)
186 {
187         if(pDoc)
188         {
189                 CFX_Font* pFXFont = new CFX_Font();
190                 pFXFont->LoadSubst(sFontFaceName,TRUE,0,0,0,CharSet2CP(nCharset),FALSE);
191                 CPDF_Font* pFont = pDoc->AddFont(pFXFont,nCharset,FALSE);
192                 delete pFXFont;
193                 return pFont;
194         }
195
196         return NULL;
197 }
198
199
200 int32_t CFX_SystemHandler::SetTimer(int32_t uElapse, TimerCallback lpTimerFunc)
201 {
202         return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
203 }
204 void CFX_SystemHandler::KillTimer(int32_t nID)
205 {
206         m_pEnv->FFI_KillTimer(nID);
207 }
208
209 FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime()
210 {
211         return m_pEnv->FFI_GetLocalTime();
212 }
213
214
215 CJS_RuntimeFactory* GetJSRuntimeFactory()
216 {
217         static CJS_RuntimeFactory s_JSRuntimeFactory;
218         return &s_JSRuntimeFactory;
219 }
220
221 CPDFDoc_Environment::CPDFDoc_Environment(CPDF_Document* pDoc) :
222         m_pAnnotHandlerMgr(NULL),
223         m_pActionHandler(NULL),
224         m_pJSRuntime(NULL),
225         m_pInfo(NULL),
226         m_pSDKDoc(NULL),
227         m_pPDFDoc(pDoc),
228         m_pIFormFiller(NULL)
229 {
230
231         m_pSysHandler = NULL;
232         m_pSysHandler = new CFX_SystemHandler(this);
233
234
235         m_pJSRuntimeFactory = NULL;
236         m_pJSRuntimeFactory = GetJSRuntimeFactory();
237         m_pJSRuntimeFactory->AddRef();
238 }
239
240 CPDFDoc_Environment::~CPDFDoc_Environment()
241 {
242     delete m_pIFormFiller;
243     m_pIFormFiller = NULL;
244     if (m_pJSRuntime && m_pJSRuntimeFactory)
245         m_pJSRuntimeFactory->DeleteJSRuntime(m_pJSRuntime);
246     m_pJSRuntimeFactory->Release();
247
248     delete m_pSysHandler;
249     m_pSysHandler = NULL;
250
251     delete m_pAnnotHandlerMgr;
252     m_pAnnotHandlerMgr = NULL;
253     delete m_pActionHandler;
254     m_pActionHandler = NULL;
255 }
256
257
258 IFXJS_Runtime* CPDFDoc_Environment::GetJSRuntime()
259 {
260         if(!IsJSInitiated())
261                 return NULL;
262         assert(m_pJSRuntimeFactory);
263         if(!m_pJSRuntime)
264                 m_pJSRuntime = m_pJSRuntimeFactory->NewJSRuntime(this);
265         return m_pJSRuntime;
266 }
267
268 CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr()
269 {
270         if(!m_pAnnotHandlerMgr)
271                 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
272         return m_pAnnotHandlerMgr;
273 }
274
275 CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander()
276 {
277         if(!m_pActionHandler)
278                 m_pActionHandler = new CPDFSDK_ActionHandler(this);
279         return m_pActionHandler;
280 }
281
282 int CPDFDoc_Environment::RegAppHandle(FPDF_FORMFILLINFO* pFFinfo)
283 {
284         m_pInfo  = pFFinfo;
285         return TRUE;
286 }
287
288 CPDFSDK_Document* CPDFDoc_Environment::GetCurrentDoc()
289 {
290         return m_pSDKDoc;
291 }
292
293 CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller()
294 {
295         if(!m_pIFormFiller)
296                 m_pIFormFiller = new CFFL_IFormFiller(this);
297         return m_pIFormFiller;
298 }
299
300 FX_BOOL CPDFDoc_Environment::IsJSInitiated()
301 {
302         if(m_pInfo)
303         {
304                 if(m_pInfo->m_pJsPlatform)
305                         return TRUE;
306                 else
307                         return FALSE;
308         }
309         return FALSE;
310 }
311
312 CPDFSDK_Document::CPDFSDK_Document(CPDF_Document* pDoc,CPDFDoc_Environment* pEnv):m_pDoc(pDoc),
313                                                 m_pInterForm(NULL),m_pEnv(pEnv),m_pOccontent(NULL),m_bChangeMask(FALSE)
314 {
315         m_pFocusAnnot = NULL;
316 }
317
318 CPDFSDK_Document::~CPDFSDK_Document()
319 {
320     for (auto& it : m_pageMap)
321         delete it.second;
322     m_pageMap.clear();
323
324     delete m_pInterForm;
325     m_pInterForm = nullptr;
326
327     delete m_pOccontent;
328     m_pOccontent = nullptr;
329 }
330
331 CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDF_Page* pPDFPage, FX_BOOL ReNew)
332 {
333     auto it = m_pageMap.find(pPDFPage);
334     if (it != m_pageMap.end())
335         return it->second;
336
337     if (!ReNew)
338         return nullptr;
339
340     CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFPage);
341     m_pageMap[pPDFPage] = pPageView;
342     // Delay to load all the annotations, to avoid endless loop.
343     pPageView->LoadFXAnnots();
344     return pPageView;
345 }
346
347 CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView()
348 {
349     CPDF_Page* pPage = (CPDF_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
350     return pPage ? GetPageView(pPage, TRUE) : nullptr;
351 }
352
353 CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex)
354 {
355     CPDF_Page* pTempPage = (CPDF_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
356     if (!pTempPage)
357         return nullptr;
358
359     auto it = m_pageMap.find(pTempPage);
360     return it->second;
361 }
362
363 void CPDFSDK_Document:: ProcJavascriptFun()
364 {
365         CPDF_Document* pPDFDoc = GetDocument();
366         CPDF_DocJSActions docJS(pPDFDoc);
367         int iCount = docJS.CountJSActions();
368         if (iCount < 1) return;
369         for (int i = 0; i < iCount; i ++)
370         {
371                 CFX_ByteString csJSName;
372                 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
373                 if(m_pEnv->GetActionHander())
374                         m_pEnv->GetActionHander()->DoAction_JavaScript(jsAction,CFX_WideString::FromLocal(csJSName),this);
375         }
376
377 }
378
379 FX_BOOL CPDFSDK_Document::ProcOpenAction()
380 {
381         if(!m_pDoc)
382                 return FALSE;
383
384         CPDF_Dictionary* pRoot = m_pDoc->GetRoot();
385         if (!pRoot)
386                 return FALSE;
387
388         CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
389         if(!pOpenAction)
390                 pOpenAction = pRoot->GetArray("OpenAction");
391
392         if(!pOpenAction)
393                 return FALSE;
394
395         if(pOpenAction->GetType()==PDFOBJ_ARRAY)
396                 return TRUE;
397
398         if(pOpenAction->GetType()==PDFOBJ_DICTIONARY)
399         {
400                 CPDF_Dictionary * pDict=(CPDF_Dictionary*)pOpenAction;
401                 CPDF_Action action(pDict);
402                 if(m_pEnv->GetActionHander())
403                         m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
404                 return TRUE;
405         }
406         return FALSE;
407 }
408
409 CPDF_OCContext* CPDFSDK_Document::GetOCContext()
410 {
411         if(!m_pOccontent)
412                 m_pOccontent = new CPDF_OCContext(m_pDoc);
413         return m_pOccontent;
414 }
415
416 void CPDFSDK_Document::ReMovePageView(CPDF_Page* pPDFPage)
417 {
418     auto it = m_pageMap.find(pPDFPage);
419     if (it == m_pageMap.end())
420         return;
421
422     CPDFSDK_PageView* pPageView = it->second;
423     if (pPageView->IsLocked())
424         return;
425
426     delete pPageView;
427     m_pageMap.erase(it);
428 }
429
430 CPDF_Page * CPDFSDK_Document::GetPage(int nIndex)
431 {
432         CPDF_Page * pTempPage = (CPDF_Page*)m_pEnv->FFI_GetPage(m_pDoc,nIndex);
433         if(!pTempPage)
434                 return NULL;
435         return pTempPage;
436 }
437
438 CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm()
439 {
440         if(!m_pInterForm)
441                 m_pInterForm = new CPDFSDK_InterForm(this);
442         return m_pInterForm;
443 }
444
445 void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot)
446 {
447     for (const auto& it : m_pageMap) {
448         CPDFSDK_PageView* pPageView = it.second;
449         if (pPageView != pSender) {
450             pPageView->UpdateView(pAnnot);
451         }
452     }
453 }
454
455 CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot()
456 {
457     return m_pFocusAnnot;
458 }
459
460 FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot,FX_UINT nFlag)
461 {
462
463         if(m_pFocusAnnot==pAnnot) return TRUE;
464
465         if(m_pFocusAnnot)
466         {
467                 if(!KillFocusAnnot(nFlag) ) return FALSE;
468         }
469         CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
470         if(pAnnot && pPageView->IsValid())
471         {
472                 CPDFSDK_AnnotHandlerMgr *pAnnotHandler=m_pEnv->GetAnnotHandlerMgr();
473
474                 if(pAnnotHandler&&!m_pFocusAnnot)
475                 {
476                         if (!pAnnotHandler->Annot_OnSetFocus(pAnnot,nFlag))
477                                 return FALSE;
478                         if(!m_pFocusAnnot)
479                         {
480                                 m_pFocusAnnot=pAnnot;
481                                 return TRUE;
482                         }
483                 }
484         }
485         return FALSE;
486 }
487
488 FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag)
489 {
490         if(m_pFocusAnnot)
491         {
492                 CPDFSDK_AnnotHandlerMgr *pAnnotHandler=m_pEnv->GetAnnotHandlerMgr();
493                 if(pAnnotHandler)
494                 {
495                         CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
496                         m_pFocusAnnot = NULL;
497                         if(pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag))
498                         {
499
500                                 if(pFocusAnnot->GetType() == FX_BSTRC("Widget"))
501                                 {
502                                         CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
503                                         int nFieldType = pWidget->GetFieldType();
504                                         if(FIELDTYPE_TEXTFIELD == nFieldType || FIELDTYPE_COMBOBOX == nFieldType)
505                                                 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
506                                 }
507
508                                 if(!m_pFocusAnnot)
509                                         return TRUE;
510                         }
511                         else
512                         {
513                                 m_pFocusAnnot = pFocusAnnot;
514                         }
515                 }
516         }
517         return FALSE;
518 }
519
520 void CPDFSDK_Document::OnCloseDocument()
521 {
522         KillFocusAnnot();
523 }
524
525 FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag)
526 {
527         FX_DWORD dwPermissions = m_pDoc->GetUserPermissions();
528         return dwPermissions&nFlag;
529 }
530
531 IFXJS_Runtime * CPDFSDK_Document::GetJsRuntime()
532 {
533         ASSERT(m_pEnv!=NULL);
534         return m_pEnv->GetJSRuntime();
535 }
536
537 CFX_WideString  CPDFSDK_Document::GetPath()
538 {
539         ASSERT(m_pEnv != NULL);
540         return m_pEnv->JS_docGetFilePath();
541 }
542
543
544 CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,CPDF_Page* page):m_page(page),m_pSDKDoc(pSDKDoc)
545 {
546         CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
547         if(pInterForm)
548         {
549                 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
550                 pPDFInterForm->FixPageFields(page);
551         }
552         m_page->SetPrivateData((void*)m_page, (void*)this, NULL);
553         m_fxAnnotArray.RemoveAll();
554
555         m_bEnterWidget = FALSE;
556         m_bExitWidget = FALSE;
557         m_bOnWidget = FALSE;
558         m_CaptureWidget = NULL;
559         m_bValid = FALSE;
560         m_bLocked = FALSE;
561         m_bTakeOverPage = FALSE;
562 }
563
564 CPDFSDK_PageView::~CPDFSDK_PageView()
565 {
566         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
567         int nAnnotCount = m_fxAnnotArray.GetSize();
568
569         for (int i=0; i<nAnnotCount; i++)
570         {
571                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
572                 //if there is a focused annot on the page, we should kill the focus first.
573                 if(pAnnot == m_pSDKDoc->GetFocusAnnot())
574                         KillFocusAnnot();
575                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
576                 ASSERT(pAnnotHandlerMgr);
577                 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
578         }
579         m_fxAnnotArray.RemoveAll();
580
581         delete m_pAnnotList;
582         m_pAnnotList = NULL;
583
584         m_page->RemovePrivateData((void*)m_page);
585         if(m_bTakeOverPage) {
586             delete m_page;
587         }
588 }
589
590 void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,CPDF_RenderOptions* pOptions)
591 {
592     m_curMatrix = *pUser2Device;
593
594     CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
595     CPDFSDK_AnnotIterator annotIterator(this, TRUE);
596     CPDFSDK_Annot* pSDKAnnot = nullptr;
597     int index = -1;
598     while ((pSDKAnnot = annotIterator.Next(index))) {
599         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
600         ASSERT(pAnnotHandlerMgr);
601         pAnnotHandlerMgr->Annot_OnDraw(
602             this, pSDKAnnot, pDevice, pUser2Device, 0);
603     }
604 }
605
606 CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
607 {
608
609         int nCount = m_pAnnotList->Count();
610         for(int i = 0 ; i<nCount; i++)
611         {
612                 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
613                 CFX_FloatRect annotRect;
614                 pAnnot->GetRect(annotRect);
615                 if(annotRect.Contains(pageX, pageY))
616                         return pAnnot;
617         }
618         return NULL;
619 }
620
621 CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
622 {
623
624         int nCount = m_pAnnotList->Count();
625         for(int i = 0 ; i<nCount; i++)
626         {
627                 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
628                 if(pAnnot->GetSubType() == "Widget")
629                 {
630                         CFX_FloatRect annotRect;
631                         pAnnot->GetRect(annotRect);
632                         if(annotRect.Contains(pageX, pageY))
633                                 return pAnnot;
634                 }
635         }
636         return NULL;
637 }
638
639 CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
640 {
641
642         CPDFSDK_AnnotIterator annotIterator(this, FALSE);
643         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
644         CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
645         CPDFSDK_Annot* pSDKAnnot = NULL;
646         int index = -1;
647         while((pSDKAnnot = annotIterator.Next(index)))
648         {
649                 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
650                 if(rc.Contains(pageX, pageY))
651                         return pSDKAnnot;
652         }
653
654         return NULL;
655 }
656
657 CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
658 {
659
660         CPDFSDK_AnnotIterator annotIterator(this, FALSE);
661         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
662         CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
663         CPDFSDK_Annot* pSDKAnnot = NULL;
664         int index = -1;
665         while((pSDKAnnot = annotIterator.Next(index)))
666         {
667                 if(pSDKAnnot->GetType() == "Widget")
668                 {
669                         pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
670                         CPDF_Point point(pageX, pageY);
671                         if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
672                                 return pSDKAnnot;
673                 }
674         }
675
676         return NULL;
677 }
678
679
680 FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot)
681 {
682         CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
683         if(pAnnotDic)
684                 return  pAnnotDic->KeyExist("AS");
685         return FALSE;
686 }
687
688 CPDFSDK_Annot*  CPDFSDK_PageView::AddAnnot(CPDF_Annot * pPDFAnnot)
689 {
690         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
691         ASSERT(pEnv);
692         CPDFSDK_AnnotHandlerMgr * pAnnotHandler= pEnv->GetAnnotHandlerMgr();
693
694         CPDFSDK_Annot* pSDKAnnot =NULL;
695
696         if(pAnnotHandler)
697         {
698                 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
699         }
700         if(!pSDKAnnot)
701                 return NULL;
702
703         m_fxAnnotArray.Add(pSDKAnnot);
704
705         if(pAnnotHandler)
706         {
707                 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
708
709         }
710
711          return pSDKAnnot;
712 }
713
714 CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary * pDict)
715 {
716     return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
717 }
718
719 CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,CPDF_Dictionary * pDict)
720 {
721         return NULL;
722 }
723
724 FX_BOOL  CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot)
725 {
726         return FALSE;
727 }
728
729 CPDF_Document* CPDFSDK_PageView::GetPDFDocument()
730 {
731         if(m_page)
732         {
733                 return m_page->m_pDocument;
734         }
735         return NULL;
736 }
737
738 int     CPDFSDK_PageView::CountAnnots()
739 {
740         return m_pAnnotList->Count();
741 }
742
743 CPDFSDK_Annot*  CPDFSDK_PageView::GetAnnot(int nIndex)
744 {
745         int nCount = m_fxAnnotArray.GetSize();
746         if ( nIndex < 0 || nIndex >= nCount )
747         {
748                 return NULL;
749         }
750
751         return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
752 }
753
754 CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary * pDict)
755 {
756         int nCount = m_fxAnnotArray.GetSize();
757         for(int i=0; i<nCount; i++)
758         {
759                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
760                 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
761                         return pAnnot;
762         }
763         return NULL;
764 }
765
766 FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point & point, FX_UINT nFlag)
767 {
768         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
769         ASSERT(pEnv);
770         CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
771         if(!pFXAnnot)
772         {
773                 KillFocusAnnot(nFlag);
774         }
775         else
776         {
777                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
778                 ASSERT(pAnnotHandlerMgr);
779
780                 FX_BOOL bRet = pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag,point);
781                 if(bRet)
782                 {
783                         SetFocusAnnot(pFXAnnot);
784                 }
785                 return bRet;
786         }
787         return FALSE;
788 }
789
790
791 FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point & point, FX_UINT nFlag)
792 {
793         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
794         ASSERT(pEnv);
795         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
796         ASSERT(pAnnotHandlerMgr);
797         CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
798         CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
799         FX_BOOL bRet  = FALSE;
800         if(pFocusAnnot && pFocusAnnot != pFXAnnot)
801         {
802                 //Last focus Annot gets a chance to handle the event.
803                 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag,point);
804         }
805         if(pFXAnnot && !bRet)
806         {
807                 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag,point);
808                 return bRet;
809         }
810         return bRet;
811 }
812
813 FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point & point, int nFlag)
814 {
815
816         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
817         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
818         ASSERT(pAnnotHandlerMgr);
819         if(CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y))
820         {
821                 if(m_CaptureWidget && m_CaptureWidget != pFXAnnot)
822                 {
823                         m_bExitWidget = TRUE;
824                         m_bEnterWidget = FALSE;
825                         pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
826                 }
827                 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
828                 m_bOnWidget = TRUE;
829                 if(!m_bEnterWidget)
830                 {
831                         m_bEnterWidget = TRUE;
832                         m_bExitWidget = FALSE;
833                         pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot,nFlag);
834                 }
835                 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
836                 return TRUE;
837         }
838         else
839         {
840                 if(m_bOnWidget)
841                 {
842                         m_bOnWidget = FALSE;
843                         m_bExitWidget = TRUE;
844                         m_bEnterWidget = FALSE;
845                         if(m_CaptureWidget)
846                         {
847                                 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
848                                 m_CaptureWidget = NULL;
849                         }
850                 }
851                 return FALSE;
852         }
853
854         return FALSE;;
855 }
856
857 FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX, double deltaY,const CPDF_Point& point, int nFlag)
858 {
859         if(CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y))
860         {
861                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
862                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
863                 ASSERT(pAnnotHandlerMgr);
864                 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag, (int)deltaY, point);
865         }
866         return FALSE;
867
868 }
869
870 FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag)
871 {
872         if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
873         {
874                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
875                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
876                 ASSERT(pAnnotHandlerMgr);
877                 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
878         }
879
880         return FALSE;
881 }
882
883 FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag)
884 {
885         if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
886         {
887                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
888                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
889                 ASSERT(pAnnotHandlerMgr);
890                 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
891         }
892         return FALSE;
893 }
894
895 FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag)
896 {
897 //      if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
898 //      {
899 //              CFFL_IFormFiller* pIFormFiller = g_pFormFillApp->GetIFormFiller();
900 //              return pIFormFiller->OnKeyUp(pAnnot, nKeyCode, nFlag);
901 //      }
902         return FALSE;
903 }
904
905 extern void CheckUnSupportAnnot(CPDF_Document * pDoc, CPDF_Annot* pPDFAnnot);
906
907 void CPDFSDK_PageView::LoadFXAnnots()
908 {
909         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
910
911         FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
912         //Disable the default AP construction.
913         CPDF_InterForm::EnableUpdateAP(FALSE);
914         m_pAnnotList = new CPDF_AnnotList(m_page);
915         CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
916         int nCount = m_pAnnotList->Count();
917         SetLock(TRUE);
918         for(int i=0; i<nCount; i++)
919         {
920                 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
921                 CPDF_Document * pDoc = GetPDFDocument();
922
923                 CheckUnSupportAnnot(pDoc, pPDFAnnot);
924
925                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
926                 ASSERT(pAnnotHandlerMgr != NULL);
927
928                 if(pAnnotHandlerMgr)
929                 {
930                         CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
931                         if(!pAnnot)
932                                 continue;
933                         m_fxAnnotArray.Add(pAnnot);
934
935                         pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
936                 }
937
938         }
939         SetLock(FALSE);
940 }
941
942 void    CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects)
943 {
944         for(int i=0; i<rects.GetSize(); i++)
945         {
946                 CPDF_Rect rc = rects.GetAt(i);
947                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
948                 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
949         }
950 }
951
952 void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot)
953 {
954     CPDF_Rect rcWindow = pAnnot->GetRect();
955     CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
956     pEnv->FFI_Invalidate(
957         m_page, rcWindow.left, rcWindow.top, rcWindow.right, rcWindow.bottom);
958 }
959
960 int CPDFSDK_PageView::GetPageIndex()
961 {
962         if(m_page)
963         {
964                 CPDF_Dictionary* pDic = m_page->m_pFormDict;
965                 CPDF_Document* pDoc = m_pSDKDoc->GetDocument();
966                 if(pDoc && pDic)
967                 {
968                         return pDoc->GetPageIndex(pDic->GetObjNum());
969                 }
970         }
971         return -1;
972 }
973
974 FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p)
975 {
976         if (p == NULL) return FALSE;
977         int iCount = m_pAnnotList->Count();
978         for (int i = 0; i < iCount; i++)
979         {
980                 if (m_pAnnotList->GetAt(i) == p)
981                         return TRUE;
982         }
983         return FALSE;
984 }
985
986
987 CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot()
988 {
989         CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
990         if(!pFocusAnnot)
991                 return NULL;
992
993         for(int i=0; i<m_fxAnnotArray.GetSize(); i++)
994         {
995                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
996                 if(pAnnot == pFocusAnnot)
997                         return pAnnot;
998         }
999         return NULL;
1000 }
1001