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