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