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