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