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