Replace some Release() calls with virtual destructors.
[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
243         if ( m_pIFormFiller )
244         {
245                 delete m_pIFormFiller;
246                 m_pIFormFiller = NULL;
247         }
248         if(m_pJSRuntime && m_pJSRuntimeFactory)
249                 m_pJSRuntimeFactory->DeleteJSRuntime(m_pJSRuntime);
250         m_pJSRuntimeFactory->Release();
251
252         if(m_pSysHandler)
253         {
254                 delete m_pSysHandler;
255                 m_pSysHandler = NULL;
256         }
257
258         if(m_pAnnotHandlerMgr)
259         {
260                 delete m_pAnnotHandlerMgr;
261                 m_pAnnotHandlerMgr = NULL;
262         }
263         if(m_pActionHandler)
264         {
265                 delete m_pActionHandler;
266                 m_pActionHandler = NULL;
267         }
268
269
270 }
271
272
273 IFXJS_Runtime* CPDFDoc_Environment::GetJSRuntime()
274 {
275         if(!IsJSInitiated())
276                 return NULL;
277         assert(m_pJSRuntimeFactory);
278         if(!m_pJSRuntime)
279                 m_pJSRuntime = m_pJSRuntimeFactory->NewJSRuntime(this);
280         return m_pJSRuntime;
281 }
282
283 CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr()
284 {
285         if(!m_pAnnotHandlerMgr)
286                 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
287         return m_pAnnotHandlerMgr;
288 }
289
290 CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander()
291 {
292         if(!m_pActionHandler)
293                 m_pActionHandler = new CPDFSDK_ActionHandler(this);
294         return m_pActionHandler;
295 }
296
297 int CPDFDoc_Environment::RegAppHandle(FPDF_FORMFILLINFO* pFFinfo)
298 {
299         m_pInfo  = pFFinfo;
300         return TRUE;
301 }
302
303 CPDFSDK_Document* CPDFDoc_Environment::GetCurrentDoc()
304 {
305         return m_pSDKDoc;
306 }
307
308 CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller()
309 {
310         if(!m_pIFormFiller)
311                 m_pIFormFiller = new CFFL_IFormFiller(this);
312         return m_pIFormFiller;
313 }
314
315 FX_BOOL CPDFDoc_Environment::IsJSInitiated()
316 {
317         if(m_pInfo)
318         {
319                 if(m_pInfo->m_pJsPlatform)
320                         return TRUE;
321                 else
322                         return FALSE;
323         }
324         return FALSE;
325 }
326
327 CPDFSDK_Document::CPDFSDK_Document(CPDF_Document* pDoc,CPDFDoc_Environment* pEnv):m_pDoc(pDoc),
328                                                 m_pInterForm(NULL),m_pEnv(pEnv),m_pOccontent(NULL),m_bChangeMask(FALSE)
329 {
330         m_pFocusAnnot = NULL;
331 }
332
333 CPDFSDK_Document::~CPDFSDK_Document()
334 {
335     FX_POSITION pos = m_pageMap.GetStartPosition();
336     while (pos) {
337         CPDF_Page* pPage = NULL;
338         CPDFSDK_PageView* pPageView = NULL;
339         m_pageMap.GetNextAssoc(pos, pPage, pPageView);
340         delete pPageView;
341     }
342     m_pageMap.RemoveAll();
343
344     delete m_pInterForm;
345     m_pInterForm = nullptr;
346
347     delete m_pOccontent;
348     m_pOccontent = nullptr;
349 }
350
351 void CPDFSDK_Document::InitPageView()
352 {
353         int nCount = m_pDoc->GetPageCount();
354         for(int i=0; i<nCount; i++)
355         {
356         // To do
357 //              CPDF_Dictionary* pDic = m_pDoc->GetPage(i);
358 //              m_pageMap.SetAt(pDic, pPageView);
359         }
360 }
361
362 void CPDFSDK_Document::AddPageView(CPDF_Page* pPDFPage, CPDFSDK_PageView* pPageView)
363 {
364         m_pageMap.SetAt(pPDFPage, pPageView);
365 }
366
367 CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDF_Page* pPDFPage, FX_BOOL ReNew)
368 {
369         CPDFSDK_PageView* pPageView = (CPDFSDK_PageView*)m_pageMap.GetValueAt(pPDFPage);
370         if(pPageView != NULL)
371                 return pPageView;
372         if(ReNew)
373         {
374                 pPageView = new CPDFSDK_PageView(this,pPDFPage);
375                 m_pageMap.SetAt(pPDFPage, pPageView);
376                 //Delay to load all the annotations, to avoid endless loop.
377                 pPageView->LoadFXAnnots();
378         }
379         return pPageView;
380
381 }
382
383 CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView()
384 {
385     CPDF_Page* pPage = (CPDF_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
386     return pPage ? GetPageView(pPage, TRUE) : nullptr;
387 }
388
389 CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex)
390 {
391         CPDFSDK_PageView * pTempPageView = NULL;
392         CPDF_Page * pTempPage = (CPDF_Page*)m_pEnv->FFI_GetPage(m_pDoc,nIndex);
393         if(!pTempPage)
394                 return NULL;
395
396         m_pageMap.Lookup(pTempPage, pTempPageView);
397
398         ASSERT(pTempPageView != NULL);
399
400         return pTempPageView;
401 }
402
403 void CPDFSDK_Document:: ProcJavascriptFun()
404 {
405         CPDF_Document* pPDFDoc = GetDocument();
406         CPDF_DocJSActions docJS(pPDFDoc);
407         int iCount = docJS.CountJSActions();
408         if (iCount < 1) return;
409         for (int i = 0; i < iCount; i ++)
410         {
411                 CFX_ByteString csJSName;
412                 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
413                 if(m_pEnv->GetActionHander())
414                         m_pEnv->GetActionHander()->DoAction_JavaScript(jsAction,CFX_WideString::FromLocal(csJSName),this);
415         }
416
417 }
418
419 FX_BOOL CPDFSDK_Document::ProcOpenAction()
420 {
421         if(!m_pDoc)
422                 return FALSE;
423
424         CPDF_Dictionary* pRoot = m_pDoc->GetRoot();
425         if (!pRoot)
426                 return FALSE;
427
428         CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
429         if(!pOpenAction)
430                 pOpenAction = pRoot->GetArray("OpenAction");
431
432         if(!pOpenAction)
433                 return FALSE;
434
435         if(pOpenAction->GetType()==PDFOBJ_ARRAY)
436                 return TRUE;
437
438         if(pOpenAction->GetType()==PDFOBJ_DICTIONARY)
439         {
440                 CPDF_Dictionary * pDict=(CPDF_Dictionary*)pOpenAction;
441                 CPDF_Action action(pDict);
442                 if(m_pEnv->GetActionHander())
443                         m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
444                 return TRUE;
445         }
446         return FALSE;
447 }
448
449 CPDF_OCContext* CPDFSDK_Document::GetOCContext()
450 {
451         if(!m_pOccontent)
452                 m_pOccontent = new CPDF_OCContext(m_pDoc);
453         return m_pOccontent;
454 }
455
456 void CPDFSDK_Document::ReMovePageView(CPDF_Page* pPDFPage)
457 {
458         CPDFSDK_PageView* pPageView = (CPDFSDK_PageView*)m_pageMap.GetValueAt(pPDFPage);
459         if(pPageView && !pPageView->IsLocked())
460         {
461                 delete pPageView;
462                 m_pageMap.RemoveKey(pPDFPage);
463         }
464 }
465
466 CPDF_Page * CPDFSDK_Document::GetPage(int nIndex)
467 {
468         CPDF_Page * pTempPage = (CPDF_Page*)m_pEnv->FFI_GetPage(m_pDoc,nIndex);
469         if(!pTempPage)
470                 return NULL;
471         return pTempPage;
472 }
473
474 CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm()
475 {
476         if(!m_pInterForm)
477                 m_pInterForm = new CPDFSDK_InterForm(this);
478         return m_pInterForm;
479 }
480
481 void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot)
482 {
483
484         FX_POSITION pos = m_pageMap.GetStartPosition();
485         CPDF_Page * pPage = NULL;
486         CPDFSDK_PageView * pPageView = NULL;
487         while(pos)
488         {
489                 m_pageMap.GetNextAssoc(pos, pPage, pPageView);
490
491                 if(pPageView != pSender)
492                 {
493                         pPageView->UpdateView(pAnnot);
494                 }
495         }
496 }
497
498 CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot()
499 {
500     return m_pFocusAnnot;
501 }
502
503 FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot,FX_UINT nFlag)
504 {
505
506         if(m_pFocusAnnot==pAnnot) return TRUE;
507
508         if(m_pFocusAnnot)
509         {
510                 if(!KillFocusAnnot(nFlag) ) return FALSE;
511         }
512         CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
513         if(pAnnot && pPageView->IsValid())
514         {
515                 CPDFSDK_AnnotHandlerMgr *pAnnotHandler=m_pEnv->GetAnnotHandlerMgr();
516
517                 if(pAnnotHandler&&!m_pFocusAnnot)
518                 {
519                         if (!pAnnotHandler->Annot_OnSetFocus(pAnnot,nFlag))
520                                 return FALSE;
521                         if(!m_pFocusAnnot)
522                         {
523                                 m_pFocusAnnot=pAnnot;
524                                 return TRUE;
525                         }
526                 }
527         }
528         return FALSE;
529 }
530
531 FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag)
532 {
533         if(m_pFocusAnnot)
534         {
535                 CPDFSDK_AnnotHandlerMgr *pAnnotHandler=m_pEnv->GetAnnotHandlerMgr();
536                 if(pAnnotHandler)
537                 {
538                         CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
539                         m_pFocusAnnot = NULL;
540                         if(pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag))
541                         {
542
543                                 if(pFocusAnnot->GetType() == FX_BSTRC("Widget"))
544                                 {
545                                         CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
546                                         int nFieldType = pWidget->GetFieldType();
547                                         if(FIELDTYPE_TEXTFIELD == nFieldType || FIELDTYPE_COMBOBOX == nFieldType)
548                                                 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
549                                 }
550
551                                 if(!m_pFocusAnnot)
552                                         return TRUE;
553                         }
554                         else
555                         {
556                                 m_pFocusAnnot = pFocusAnnot;
557                         }
558                 }
559         }
560         return FALSE;
561 }
562
563 FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int  nCount)
564 {
565         if ( nStart < 0 || nStart >= GetPageCount() || nCount <= 0 )
566         {
567                 return FALSE;
568         }
569
570         CPDF_Page * pTempPage = NULL;
571         for ( int i = nCount-1; i >= 0; i-- )
572         {
573                 pTempPage = GetPage(nStart+i);
574                 if ( pTempPage != NULL )
575                 {
576                         ReMovePageView(pTempPage);
577                 }
578         }
579         return TRUE;
580 }
581
582 void CPDFSDK_Document::OnCloseDocument()
583 {
584         KillFocusAnnot();
585 }
586
587 FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag)
588 {
589         FX_DWORD dwPermissions = m_pDoc->GetUserPermissions();
590         return dwPermissions&nFlag;
591 }
592
593 IFXJS_Runtime * CPDFSDK_Document::GetJsRuntime()
594 {
595         ASSERT(m_pEnv!=NULL);
596         return m_pEnv->GetJSRuntime();
597 }
598
599 CFX_WideString  CPDFSDK_Document::GetPath()
600 {
601         ASSERT(m_pEnv != NULL);
602         return m_pEnv->JS_docGetFilePath();
603 }
604
605
606 CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,CPDF_Page* page):m_page(page),m_pSDKDoc(pSDKDoc)
607 {
608         CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
609         if(pInterForm)
610         {
611                 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
612                 pPDFInterForm->FixPageFields(page);
613         }
614         m_page->SetPrivateData((void*)m_page, (void*)this, NULL);
615         m_fxAnnotArray.RemoveAll();
616
617         m_bEnterWidget = FALSE;
618         m_bExitWidget = FALSE;
619         m_bOnWidget = FALSE;
620         m_CaptureWidget = NULL;
621         m_bValid = FALSE;
622         m_bLocked = FALSE;
623         m_bTakeOverPage = FALSE;
624 }
625
626 CPDFSDK_PageView::~CPDFSDK_PageView()
627 {
628         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
629         int nAnnotCount = m_fxAnnotArray.GetSize();
630
631         for (int i=0; i<nAnnotCount; i++)
632         {
633                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
634                 //if there is a focused annot on the page, we should kill the focus first.
635                 if(pAnnot == m_pSDKDoc->GetFocusAnnot())
636                         KillFocusAnnot();
637                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
638                 ASSERT(pAnnotHandlerMgr);
639                 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
640         }
641         m_fxAnnotArray.RemoveAll();
642         if(m_pAnnotList)
643         {
644                 delete m_pAnnotList;
645                 m_pAnnotList = NULL;
646         }
647         m_page->RemovePrivateData((void*)m_page);
648         if(m_bTakeOverPage) {
649             delete m_page;
650         }
651 }
652
653 void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,CPDF_RenderOptions* pOptions)
654 {
655     m_curMatrix = *pUser2Device;
656
657     CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
658     CPDFSDK_AnnotIterator annotIterator(this, TRUE);
659     CPDFSDK_Annot* pSDKAnnot = nullptr;
660     int index = -1;
661     while ((pSDKAnnot = annotIterator.Next(index))) {
662         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
663         ASSERT(pAnnotHandlerMgr);
664         pAnnotHandlerMgr->Annot_OnDraw(
665             this, pSDKAnnot, pDevice, pUser2Device, 0);
666     }
667 }
668
669 CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
670 {
671
672         int nCount = m_pAnnotList->Count();
673         for(int i = 0 ; i<nCount; i++)
674         {
675                 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
676                 CFX_FloatRect annotRect;
677                 pAnnot->GetRect(annotRect);
678                 if(annotRect.Contains(pageX, pageY))
679                         return pAnnot;
680         }
681         return NULL;
682 }
683
684 CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
685 {
686
687         int nCount = m_pAnnotList->Count();
688         for(int i = 0 ; i<nCount; i++)
689         {
690                 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
691                 if(pAnnot->GetSubType() == "Widget")
692                 {
693                         CFX_FloatRect annotRect;
694                         pAnnot->GetRect(annotRect);
695                         if(annotRect.Contains(pageX, pageY))
696                                 return pAnnot;
697                 }
698         }
699         return NULL;
700 }
701
702 CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
703 {
704
705         CPDFSDK_AnnotIterator annotIterator(this, FALSE);
706         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
707         CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
708         CPDFSDK_Annot* pSDKAnnot = NULL;
709         int index = -1;
710         while((pSDKAnnot = annotIterator.Next(index)))
711         {
712                 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
713                 if(rc.Contains(pageX, pageY))
714                         return pSDKAnnot;
715         }
716
717         return NULL;
718 }
719
720 CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
721 {
722
723         CPDFSDK_AnnotIterator annotIterator(this, FALSE);
724         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
725         CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
726         CPDFSDK_Annot* pSDKAnnot = NULL;
727         int index = -1;
728         while((pSDKAnnot = annotIterator.Next(index)))
729         {
730                 if(pSDKAnnot->GetType() == "Widget")
731                 {
732                         pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
733                         CPDF_Point point(pageX, pageY);
734                         if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
735                                 return pSDKAnnot;
736                 }
737         }
738
739         return NULL;
740 }
741
742
743 FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot)
744 {
745         CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
746         if(pAnnotDic)
747                 return  pAnnotDic->KeyExist("AS");
748         return FALSE;
749 }
750
751 CPDFSDK_Annot*  CPDFSDK_PageView::AddAnnot(CPDF_Annot * pPDFAnnot)
752 {
753         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
754         ASSERT(pEnv);
755         CPDFSDK_AnnotHandlerMgr * pAnnotHandler= pEnv->GetAnnotHandlerMgr();
756
757         CPDFSDK_Annot* pSDKAnnot =NULL;
758
759         if(pAnnotHandler)
760         {
761                 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
762         }
763         if(!pSDKAnnot)
764                 return NULL;
765
766         m_fxAnnotArray.Add(pSDKAnnot);
767
768         if(pAnnotHandler)
769         {
770                 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
771
772         }
773
774          return pSDKAnnot;
775 }
776
777 CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary * pDict)
778 {
779     return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
780 }
781
782 CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,CPDF_Dictionary * pDict)
783 {
784         return NULL;
785 }
786
787 FX_BOOL  CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot)
788 {
789         return FALSE;
790 }
791
792 CPDF_Document* CPDFSDK_PageView::GetPDFDocument()
793 {
794         if(m_page)
795         {
796                 return m_page->m_pDocument;
797         }
798         return NULL;
799 }
800
801 int     CPDFSDK_PageView::CountAnnots()
802 {
803         return m_pAnnotList->Count();
804 }
805
806 CPDFSDK_Annot*  CPDFSDK_PageView::GetAnnot(int nIndex)
807 {
808         int nCount = m_fxAnnotArray.GetSize();
809         if ( nIndex < 0 || nIndex >= nCount )
810         {
811                 return NULL;
812         }
813
814         return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
815 }
816
817 CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary * pDict)
818 {
819         int nCount = m_fxAnnotArray.GetSize();
820         for(int i=0; i<nCount; i++)
821         {
822                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
823                 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
824                         return pAnnot;
825         }
826         return NULL;
827 }
828
829 FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point & point, FX_UINT nFlag)
830 {
831         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
832         ASSERT(pEnv);
833         CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
834         if(!pFXAnnot)
835         {
836                 KillFocusAnnot(nFlag);
837         }
838         else
839         {
840                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
841                 ASSERT(pAnnotHandlerMgr);
842
843                 FX_BOOL bRet = pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag,point);
844                 if(bRet)
845                 {
846                         SetFocusAnnot(pFXAnnot);
847                 }
848                 return bRet;
849         }
850         return FALSE;
851 }
852
853
854 FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point & point, FX_UINT nFlag)
855 {
856         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
857         ASSERT(pEnv);
858         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
859         ASSERT(pAnnotHandlerMgr);
860         CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
861         CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
862         FX_BOOL bRet  = FALSE;
863         if(pFocusAnnot && pFocusAnnot != pFXAnnot)
864         {
865                 //Last focus Annot gets a chance to handle the event.
866                 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag,point);
867         }
868         if(pFXAnnot && !bRet)
869         {
870                 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag,point);
871                 return bRet;
872         }
873         return bRet;
874 }
875
876 FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point & point, int nFlag)
877 {
878
879         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
880         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
881         ASSERT(pAnnotHandlerMgr);
882         if(CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y))
883         {
884                 if(m_CaptureWidget && m_CaptureWidget != pFXAnnot)
885                 {
886                         m_bExitWidget = TRUE;
887                         m_bEnterWidget = FALSE;
888                         pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
889                 }
890                 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
891                 m_bOnWidget = TRUE;
892                 if(!m_bEnterWidget)
893                 {
894                         m_bEnterWidget = TRUE;
895                         m_bExitWidget = FALSE;
896                         pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot,nFlag);
897                 }
898                 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
899                 return TRUE;
900         }
901         else
902         {
903                 if(m_bOnWidget)
904                 {
905                         m_bOnWidget = FALSE;
906                         m_bExitWidget = TRUE;
907                         m_bEnterWidget = FALSE;
908                         if(m_CaptureWidget)
909                         {
910                                 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
911                                 m_CaptureWidget = NULL;
912                         }
913                 }
914                 return FALSE;
915         }
916
917         return FALSE;;
918 }
919
920 FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX, double deltaY,const CPDF_Point& point, int nFlag)
921 {
922         if(CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y))
923         {
924                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
925                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
926                 ASSERT(pAnnotHandlerMgr);
927                 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag, (int)deltaY, point);
928         }
929         return FALSE;
930
931 }
932
933 FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag)
934 {
935         if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
936         {
937                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
938                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
939                 ASSERT(pAnnotHandlerMgr);
940                 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
941         }
942
943         return FALSE;
944 }
945
946 FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag)
947 {
948         if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
949         {
950                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
951                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
952                 ASSERT(pAnnotHandlerMgr);
953                 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
954         }
955         return FALSE;
956 }
957
958 FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag)
959 {
960 //      if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
961 //      {
962 //              CFFL_IFormFiller* pIFormFiller = g_pFormFillApp->GetIFormFiller();
963 //              return pIFormFiller->OnKeyUp(pAnnot, nKeyCode, nFlag);
964 //      }
965         return FALSE;
966 }
967
968 extern void CheckUnSupportAnnot(CPDF_Document * pDoc, CPDF_Annot* pPDFAnnot);
969
970 void CPDFSDK_PageView::LoadFXAnnots()
971 {
972         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
973
974         FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
975         //Disable the default AP construction.
976         CPDF_InterForm::EnableUpdateAP(FALSE);
977         m_pAnnotList = new CPDF_AnnotList(m_page);
978         CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
979         int nCount = m_pAnnotList->Count();
980         SetLock(TRUE);
981         for(int i=0; i<nCount; i++)
982         {
983                 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
984                 CPDF_Document * pDoc = GetPDFDocument();
985
986                 CheckUnSupportAnnot(pDoc, pPDFAnnot);
987
988                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
989                 ASSERT(pAnnotHandlerMgr != NULL);
990
991                 if(pAnnotHandlerMgr)
992                 {
993                         CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
994                         if(!pAnnot)
995                                 continue;
996                         m_fxAnnotArray.Add(pAnnot);
997
998                         pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
999                 }
1000
1001         }
1002         SetLock(FALSE);
1003 }
1004
1005 void    CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects)
1006 {
1007         for(int i=0; i<rects.GetSize(); i++)
1008         {
1009                 CPDF_Rect rc = rects.GetAt(i);
1010                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1011                 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1012         }
1013 }
1014
1015 void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot)
1016 {
1017     CPDF_Rect rcWindow = pAnnot->GetRect();
1018     CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1019     pEnv->FFI_Invalidate(
1020         m_page, rcWindow.left, rcWindow.top, rcWindow.right, rcWindow.bottom);
1021 }
1022
1023 int CPDFSDK_PageView::GetPageIndex()
1024 {
1025         if(m_page)
1026         {
1027                 CPDF_Dictionary* pDic = m_page->m_pFormDict;
1028                 CPDF_Document* pDoc = m_pSDKDoc->GetDocument();
1029                 if(pDoc && pDic)
1030                 {
1031                         return pDoc->GetPageIndex(pDic->GetObjNum());
1032                 }
1033         }
1034         return -1;
1035 }
1036
1037 FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p)
1038 {
1039         if (p == NULL) return FALSE;
1040         int iCount = m_pAnnotList->Count();
1041         for (int i = 0; i < iCount; i++)
1042         {
1043                 if (m_pAnnotList->GetAt(i) == p)
1044                         return TRUE;
1045         }
1046         return FALSE;
1047 }
1048
1049
1050 CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot()
1051 {
1052         CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1053         if(!pFocusAnnot)
1054                 return NULL;
1055
1056         for(int i=0; i<m_fxAnnotArray.GetSize(); i++)
1057         {
1058                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1059                 if(pAnnot == pFocusAnnot)
1060                         return pAnnot;
1061         }
1062         return NULL;
1063 }
1064