Replace CFX_MapPtrTemplate with std::map.
[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 FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int  nCount)
521 {
522         if ( nStart < 0 || nStart >= GetPageCount() || nCount <= 0 )
523         {
524                 return FALSE;
525         }
526
527         CPDF_Page * pTempPage = NULL;
528         for ( int i = nCount-1; i >= 0; i-- )
529         {
530                 pTempPage = GetPage(nStart+i);
531                 if ( pTempPage != NULL )
532                 {
533                         ReMovePageView(pTempPage);
534                 }
535         }
536         return TRUE;
537 }
538
539 void CPDFSDK_Document::OnCloseDocument()
540 {
541         KillFocusAnnot();
542 }
543
544 FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag)
545 {
546         FX_DWORD dwPermissions = m_pDoc->GetUserPermissions();
547         return dwPermissions&nFlag;
548 }
549
550 IFXJS_Runtime * CPDFSDK_Document::GetJsRuntime()
551 {
552         ASSERT(m_pEnv!=NULL);
553         return m_pEnv->GetJSRuntime();
554 }
555
556 CFX_WideString  CPDFSDK_Document::GetPath()
557 {
558         ASSERT(m_pEnv != NULL);
559         return m_pEnv->JS_docGetFilePath();
560 }
561
562
563 CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,CPDF_Page* page):m_page(page),m_pSDKDoc(pSDKDoc)
564 {
565         CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
566         if(pInterForm)
567         {
568                 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
569                 pPDFInterForm->FixPageFields(page);
570         }
571         m_page->SetPrivateData((void*)m_page, (void*)this, NULL);
572         m_fxAnnotArray.RemoveAll();
573
574         m_bEnterWidget = FALSE;
575         m_bExitWidget = FALSE;
576         m_bOnWidget = FALSE;
577         m_CaptureWidget = NULL;
578         m_bValid = FALSE;
579         m_bLocked = FALSE;
580         m_bTakeOverPage = FALSE;
581 }
582
583 CPDFSDK_PageView::~CPDFSDK_PageView()
584 {
585         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
586         int nAnnotCount = m_fxAnnotArray.GetSize();
587
588         for (int i=0; i<nAnnotCount; i++)
589         {
590                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
591                 //if there is a focused annot on the page, we should kill the focus first.
592                 if(pAnnot == m_pSDKDoc->GetFocusAnnot())
593                         KillFocusAnnot();
594                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
595                 ASSERT(pAnnotHandlerMgr);
596                 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
597         }
598         m_fxAnnotArray.RemoveAll();
599
600         delete m_pAnnotList;
601         m_pAnnotList = NULL;
602
603         m_page->RemovePrivateData((void*)m_page);
604         if(m_bTakeOverPage) {
605             delete m_page;
606         }
607 }
608
609 void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,CPDF_RenderOptions* pOptions)
610 {
611     m_curMatrix = *pUser2Device;
612
613     CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
614     CPDFSDK_AnnotIterator annotIterator(this, TRUE);
615     CPDFSDK_Annot* pSDKAnnot = nullptr;
616     int index = -1;
617     while ((pSDKAnnot = annotIterator.Next(index))) {
618         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
619         ASSERT(pAnnotHandlerMgr);
620         pAnnotHandlerMgr->Annot_OnDraw(
621             this, pSDKAnnot, pDevice, pUser2Device, 0);
622     }
623 }
624
625 CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
626 {
627
628         int nCount = m_pAnnotList->Count();
629         for(int i = 0 ; i<nCount; i++)
630         {
631                 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
632                 CFX_FloatRect annotRect;
633                 pAnnot->GetRect(annotRect);
634                 if(annotRect.Contains(pageX, pageY))
635                         return pAnnot;
636         }
637         return NULL;
638 }
639
640 CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
641 {
642
643         int nCount = m_pAnnotList->Count();
644         for(int i = 0 ; i<nCount; i++)
645         {
646                 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
647                 if(pAnnot->GetSubType() == "Widget")
648                 {
649                         CFX_FloatRect annotRect;
650                         pAnnot->GetRect(annotRect);
651                         if(annotRect.Contains(pageX, pageY))
652                                 return pAnnot;
653                 }
654         }
655         return NULL;
656 }
657
658 CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
659 {
660
661         CPDFSDK_AnnotIterator annotIterator(this, FALSE);
662         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
663         CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
664         CPDFSDK_Annot* pSDKAnnot = NULL;
665         int index = -1;
666         while((pSDKAnnot = annotIterator.Next(index)))
667         {
668                 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
669                 if(rc.Contains(pageX, pageY))
670                         return pSDKAnnot;
671         }
672
673         return NULL;
674 }
675
676 CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
677 {
678
679         CPDFSDK_AnnotIterator annotIterator(this, FALSE);
680         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
681         CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
682         CPDFSDK_Annot* pSDKAnnot = NULL;
683         int index = -1;
684         while((pSDKAnnot = annotIterator.Next(index)))
685         {
686                 if(pSDKAnnot->GetType() == "Widget")
687                 {
688                         pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
689                         CPDF_Point point(pageX, pageY);
690                         if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
691                                 return pSDKAnnot;
692                 }
693         }
694
695         return NULL;
696 }
697
698
699 FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot)
700 {
701         CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
702         if(pAnnotDic)
703                 return  pAnnotDic->KeyExist("AS");
704         return FALSE;
705 }
706
707 CPDFSDK_Annot*  CPDFSDK_PageView::AddAnnot(CPDF_Annot * pPDFAnnot)
708 {
709         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
710         ASSERT(pEnv);
711         CPDFSDK_AnnotHandlerMgr * pAnnotHandler= pEnv->GetAnnotHandlerMgr();
712
713         CPDFSDK_Annot* pSDKAnnot =NULL;
714
715         if(pAnnotHandler)
716         {
717                 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
718         }
719         if(!pSDKAnnot)
720                 return NULL;
721
722         m_fxAnnotArray.Add(pSDKAnnot);
723
724         if(pAnnotHandler)
725         {
726                 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
727
728         }
729
730          return pSDKAnnot;
731 }
732
733 CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary * pDict)
734 {
735     return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
736 }
737
738 CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,CPDF_Dictionary * pDict)
739 {
740         return NULL;
741 }
742
743 FX_BOOL  CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot)
744 {
745         return FALSE;
746 }
747
748 CPDF_Document* CPDFSDK_PageView::GetPDFDocument()
749 {
750         if(m_page)
751         {
752                 return m_page->m_pDocument;
753         }
754         return NULL;
755 }
756
757 int     CPDFSDK_PageView::CountAnnots()
758 {
759         return m_pAnnotList->Count();
760 }
761
762 CPDFSDK_Annot*  CPDFSDK_PageView::GetAnnot(int nIndex)
763 {
764         int nCount = m_fxAnnotArray.GetSize();
765         if ( nIndex < 0 || nIndex >= nCount )
766         {
767                 return NULL;
768         }
769
770         return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
771 }
772
773 CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary * pDict)
774 {
775         int nCount = m_fxAnnotArray.GetSize();
776         for(int i=0; i<nCount; i++)
777         {
778                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
779                 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
780                         return pAnnot;
781         }
782         return NULL;
783 }
784
785 FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point & point, FX_UINT nFlag)
786 {
787         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
788         ASSERT(pEnv);
789         CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
790         if(!pFXAnnot)
791         {
792                 KillFocusAnnot(nFlag);
793         }
794         else
795         {
796                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
797                 ASSERT(pAnnotHandlerMgr);
798
799                 FX_BOOL bRet = pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag,point);
800                 if(bRet)
801                 {
802                         SetFocusAnnot(pFXAnnot);
803                 }
804                 return bRet;
805         }
806         return FALSE;
807 }
808
809
810 FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point & point, FX_UINT nFlag)
811 {
812         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
813         ASSERT(pEnv);
814         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
815         ASSERT(pAnnotHandlerMgr);
816         CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
817         CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
818         FX_BOOL bRet  = FALSE;
819         if(pFocusAnnot && pFocusAnnot != pFXAnnot)
820         {
821                 //Last focus Annot gets a chance to handle the event.
822                 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag,point);
823         }
824         if(pFXAnnot && !bRet)
825         {
826                 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag,point);
827                 return bRet;
828         }
829         return bRet;
830 }
831
832 FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point & point, int nFlag)
833 {
834
835         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
836         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
837         ASSERT(pAnnotHandlerMgr);
838         if(CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y))
839         {
840                 if(m_CaptureWidget && m_CaptureWidget != pFXAnnot)
841                 {
842                         m_bExitWidget = TRUE;
843                         m_bEnterWidget = FALSE;
844                         pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
845                 }
846                 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
847                 m_bOnWidget = TRUE;
848                 if(!m_bEnterWidget)
849                 {
850                         m_bEnterWidget = TRUE;
851                         m_bExitWidget = FALSE;
852                         pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot,nFlag);
853                 }
854                 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
855                 return TRUE;
856         }
857         else
858         {
859                 if(m_bOnWidget)
860                 {
861                         m_bOnWidget = FALSE;
862                         m_bExitWidget = TRUE;
863                         m_bEnterWidget = FALSE;
864                         if(m_CaptureWidget)
865                         {
866                                 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
867                                 m_CaptureWidget = NULL;
868                         }
869                 }
870                 return FALSE;
871         }
872
873         return FALSE;;
874 }
875
876 FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX, double deltaY,const CPDF_Point& point, int nFlag)
877 {
878         if(CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y))
879         {
880                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
881                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
882                 ASSERT(pAnnotHandlerMgr);
883                 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag, (int)deltaY, point);
884         }
885         return FALSE;
886
887 }
888
889 FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag)
890 {
891         if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
892         {
893                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
894                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
895                 ASSERT(pAnnotHandlerMgr);
896                 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
897         }
898
899         return FALSE;
900 }
901
902 FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag)
903 {
904         if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
905         {
906                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
907                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
908                 ASSERT(pAnnotHandlerMgr);
909                 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
910         }
911         return FALSE;
912 }
913
914 FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag)
915 {
916 //      if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
917 //      {
918 //              CFFL_IFormFiller* pIFormFiller = g_pFormFillApp->GetIFormFiller();
919 //              return pIFormFiller->OnKeyUp(pAnnot, nKeyCode, nFlag);
920 //      }
921         return FALSE;
922 }
923
924 extern void CheckUnSupportAnnot(CPDF_Document * pDoc, CPDF_Annot* pPDFAnnot);
925
926 void CPDFSDK_PageView::LoadFXAnnots()
927 {
928         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
929
930         FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
931         //Disable the default AP construction.
932         CPDF_InterForm::EnableUpdateAP(FALSE);
933         m_pAnnotList = new CPDF_AnnotList(m_page);
934         CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
935         int nCount = m_pAnnotList->Count();
936         SetLock(TRUE);
937         for(int i=0; i<nCount; i++)
938         {
939                 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
940                 CPDF_Document * pDoc = GetPDFDocument();
941
942                 CheckUnSupportAnnot(pDoc, pPDFAnnot);
943
944                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
945                 ASSERT(pAnnotHandlerMgr != NULL);
946
947                 if(pAnnotHandlerMgr)
948                 {
949                         CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
950                         if(!pAnnot)
951                                 continue;
952                         m_fxAnnotArray.Add(pAnnot);
953
954                         pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
955                 }
956
957         }
958         SetLock(FALSE);
959 }
960
961 void    CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects)
962 {
963         for(int i=0; i<rects.GetSize(); i++)
964         {
965                 CPDF_Rect rc = rects.GetAt(i);
966                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
967                 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
968         }
969 }
970
971 void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot)
972 {
973     CPDF_Rect rcWindow = pAnnot->GetRect();
974     CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
975     pEnv->FFI_Invalidate(
976         m_page, rcWindow.left, rcWindow.top, rcWindow.right, rcWindow.bottom);
977 }
978
979 int CPDFSDK_PageView::GetPageIndex()
980 {
981         if(m_page)
982         {
983                 CPDF_Dictionary* pDic = m_page->m_pFormDict;
984                 CPDF_Document* pDoc = m_pSDKDoc->GetDocument();
985                 if(pDoc && pDic)
986                 {
987                         return pDoc->GetPageIndex(pDic->GetObjNum());
988                 }
989         }
990         return -1;
991 }
992
993 FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p)
994 {
995         if (p == NULL) return FALSE;
996         int iCount = m_pAnnotList->Count();
997         for (int i = 0; i < iCount; i++)
998         {
999                 if (m_pAnnotList->GetAt(i) == p)
1000                         return TRUE;
1001         }
1002         return FALSE;
1003 }
1004
1005
1006 CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot()
1007 {
1008         CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1009         if(!pFocusAnnot)
1010                 return NULL;
1011
1012         for(int i=0; i<m_fxAnnotArray.GetSize(); i++)
1013         {
1014                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1015                 if(pAnnot == pFocusAnnot)
1016                         return pAnnot;
1017         }
1018         return NULL;
1019 }
1020