Replace XFA_HWIDGET with IXFA_Widget*
[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/fpdfxfa/fpdfxfa_doc.h"
10 #include "../include/fpdfxfa/fpdfxfa_page.h"
11 #include "../include/fpdfxfa/fpdfxfa_util.h"
12 #include "../include/fsdk_mgr.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 CPDFDoc_Environment::CPDFDoc_Environment(CPDFXFA_Document* pDoc) :
219         m_pAnnotHandlerMgr(NULL),
220         m_pActionHandler(NULL),
221         m_pJSRuntime(NULL),
222         m_pInfo(NULL),
223         m_pSDKDoc(NULL),
224         m_pDoc(pDoc),
225         m_pIFormFiller(NULL)
226 {
227         m_pSysHandler = NULL;
228         m_pSysHandler = new CFX_SystemHandler(this);
229 }
230
231 CPDFDoc_Environment::~CPDFDoc_Environment()
232 {
233
234     delete m_pIFormFiller;
235     m_pIFormFiller = NULL;
236
237     CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
238     if (m_pJSRuntime && pProvider->GetRuntimeFactory())
239         pProvider->GetRuntimeFactory()->DeleteJSRuntime(m_pJSRuntime);
240
241     if (pProvider->m_pEnvList.GetSize() == 0)
242     {
243         pProvider->ReleaseRuntime();
244         pProvider->InitRuntime(TRUE);
245     }
246
247     delete m_pSysHandler;
248     m_pSysHandler = NULL;
249
250     delete m_pAnnotHandlerMgr;
251     m_pAnnotHandlerMgr = NULL;
252
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         if(!m_pJSRuntime)
263                 m_pJSRuntime = CPDFXFA_App::GetInstance()->GetRuntimeFactory()->NewJSRuntime(this);
264         return m_pJSRuntime;
265 }
266
267 CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr()
268 {
269         if(!m_pAnnotHandlerMgr)
270                 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
271         return m_pAnnotHandlerMgr;
272 }
273
274 CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander()
275 {
276         if(!m_pActionHandler)
277                 m_pActionHandler = new CPDFSDK_ActionHandler(this);
278         return m_pActionHandler;
279 }
280
281 int CPDFDoc_Environment::RegAppHandle(FPDF_FORMFILLINFO* pFFinfo)
282 {
283         m_pInfo  = pFFinfo; 
284         return TRUE;
285 }
286
287 CPDFSDK_Document* CPDFDoc_Environment::GetCurrentDoc()
288 {
289         return m_pSDKDoc;
290 }
291
292 CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller()
293 {
294         if(!m_pIFormFiller)
295                 m_pIFormFiller = new CFFL_IFormFiller(this);
296         return m_pIFormFiller;
297 }
298
299 FX_BOOL CPDFDoc_Environment::IsJSInitiated()
300 {
301         if(m_pInfo)
302         {
303                 if(m_pInfo->m_pJsPlatform)
304                         return TRUE;
305                 else
306                         return FALSE;
307         }
308         return FALSE;
309 }
310
311 CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,CPDFDoc_Environment* pEnv):m_pDoc(pDoc),
312                                                 m_pInterForm(NULL),m_pEnv(pEnv),m_pOccontent(NULL),m_bChangeMask(FALSE)
313 {
314         m_pFocusAnnot = NULL;
315 }
316
317 CPDFSDK_Document::~CPDFSDK_Document()
318 {
319         FX_POSITION pos = m_pageMap.GetStartPosition();
320         while(pos)
321         {
322                 CPDFXFA_Page* pPage = NULL;
323                 CPDFSDK_PageView* pPageView = NULL;
324                 m_pageMap.GetNextAssoc(pos, pPage, pPageView);
325                 delete pPageView;
326         }
327         m_pageMap.RemoveAll();
328         if(m_pInterForm)
329         {
330                 m_pInterForm->Destroy();
331                 m_pInterForm = NULL;
332         }
333         if(m_pOccontent)
334         {
335                 delete m_pOccontent;
336                 m_pOccontent = NULL;
337         }
338 }
339
340 void CPDFSDK_Document::InitPageView()
341 {
342         int nCount = m_pDoc->GetPageCount();
343         for(int i=0; i<nCount; i++)
344         {
345         // To do
346 //              CPDF_Dictionary* pDic = m_pDoc->GetPage(i);
347 //              m_pageMap.SetAt(pDic, pPageView);
348         }
349 }
350
351 void CPDFSDK_Document::AddPageView(CPDFXFA_Page* pPDFXFAPage, CPDFSDK_PageView* pPageView)
352 {
353         m_pageMap.SetAt(pPDFXFAPage, pPageView);
354 }
355
356 CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage, FX_BOOL ReNew)
357 {
358         CPDFSDK_PageView* pPageView = (CPDFSDK_PageView*)m_pageMap.GetValueAt(pPDFXFAPage);
359         if(pPageView != NULL)
360                 return pPageView;
361         if(ReNew)
362         {
363                 pPageView = new CPDFSDK_PageView(this,pPDFXFAPage);
364                 m_pageMap.SetAt(pPDFXFAPage, pPageView);
365                 //Delay to load all the annotations, to avoid endless loop.
366                 pPageView->LoadFXAnnots();
367         }
368         return pPageView;
369
370 }
371
372 CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView()
373 {
374         CPDFXFA_Page * pPage = (CPDFXFA_Page *)m_pEnv->FFI_GetCurrentPage(m_pDoc);
375         if(pPage)
376                 return this->GetPageView(pPage, TRUE);
377         return NULL;
378 }
379
380 CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex)
381 {
382         CPDFSDK_PageView * pTempPageView = NULL;
383         CPDFXFA_Page * pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc,nIndex);
384         if(!pTempPage)
385                 return NULL;
386
387         m_pageMap.Lookup(pTempPage, pTempPageView);
388         
389         ASSERT(pTempPageView != NULL);
390         
391         return pTempPageView;
392 }
393
394 void CPDFSDK_Document:: ProcJavascriptFun()
395 {
396         CPDFXFA_Document* pPDFDoc = this->GetDocument();
397         CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
398         int iCount = docJS.CountJSActions();
399         if (iCount < 1) return;
400         for (int i = 0; i < iCount; i ++)
401         {
402                 CFX_ByteString csJSName;
403                 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
404                 if(m_pEnv->GetActionHander())
405                         m_pEnv->GetActionHander()->DoAction_JavaScript(jsAction,CFX_WideString::FromLocal(csJSName),this);
406         }
407         
408 }
409
410 FX_BOOL CPDFSDK_Document::ProcOpenAction()
411 {
412         if(!m_pDoc)
413                 return FALSE;
414
415         CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
416         if (!pRoot)
417                 return FALSE;
418
419         CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
420         if(!pOpenAction)
421                 pOpenAction = pRoot->GetArray("OpenAction");
422
423         if(!pOpenAction)
424                 return FALSE;
425
426         if(pOpenAction->GetType()==PDFOBJ_ARRAY)
427                 return TRUE;
428
429         if(pOpenAction->GetType()==PDFOBJ_DICTIONARY)
430         {
431                 CPDF_Dictionary * pDict=(CPDF_Dictionary*)pOpenAction;
432                 CPDF_Action action(pDict);
433                 if(m_pEnv->GetActionHander())
434                         m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
435                 return TRUE;
436         }
437         return FALSE;
438 }
439
440 CPDF_OCContext* CPDFSDK_Document::GetOCContext()
441 {
442         if(!m_pOccontent)
443                 m_pOccontent = new CPDF_OCContext(m_pDoc->GetPDFDoc());
444         return m_pOccontent;
445 }
446
447 void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage)
448 {
449         CPDFSDK_PageView* pPageView = (CPDFSDK_PageView*)m_pageMap.GetValueAt(pPDFXFAPage);
450         if(pPageView && !pPageView->IsLocked())
451         {
452                 delete pPageView;
453                 m_pageMap.RemoveKey(pPDFXFAPage);
454         }
455 }
456
457 CPDFXFA_Page * CPDFSDK_Document::GetPage(int nIndex)
458 {
459         CPDFXFA_Page * pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc,nIndex);
460         if(!pTempPage)
461                 return NULL;
462         return pTempPage;
463 }
464
465 CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm()
466 {
467         if(!m_pInterForm)
468                 m_pInterForm = new CPDFSDK_InterForm(this);
469         return m_pInterForm;
470 }
471
472 void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot)
473 {
474         
475         FX_POSITION pos = m_pageMap.GetStartPosition();
476         CPDFXFA_Page * pPage = NULL;
477         CPDFSDK_PageView * pPageView = NULL;
478         while(pos)
479         {
480                 m_pageMap.GetNextAssoc(pos, pPage, pPageView);
481
482                 if(pPageView != pSender)
483                 {
484                         pPageView->UpdateView(pAnnot);
485                 }
486         }
487 }
488
489 CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot()
490 {
491         return this->m_pFocusAnnot;     
492 }
493
494 FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot,FX_UINT nFlag)
495 {
496
497         if(m_pFocusAnnot==pAnnot) return TRUE;
498         
499         CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
500         
501         if(m_pFocusAnnot)
502         {
503                 if(!this->KillFocusAnnot(nFlag) ) return FALSE; 
504         }
505         CPDFSDK_PageView* pPageView = NULL;
506         if (pAnnot)
507                 pPageView = pAnnot->GetPageView();
508         if(pAnnot && pPageView->IsValid())
509         {
510                 CPDFSDK_AnnotHandlerMgr *pAnnotHandler=m_pEnv->GetAnnotHandlerMgr();
511
512                 if(pAnnotHandler&&!m_pFocusAnnot)
513                 {
514                         if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot,pLastFocusAnnot))
515                                 return FALSE;
516
517                         if (!pAnnotHandler->Annot_OnSetFocus(pAnnot,nFlag))
518                                 return FALSE;
519                         if(!m_pFocusAnnot)
520                         {
521                                 this->m_pFocusAnnot=pAnnot;
522                                 return TRUE;
523                         }
524                 }                       
525         }
526         return FALSE;
527 }
528
529 FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag)
530 {
531         if(m_pFocusAnnot)
532         {
533                 CPDFSDK_AnnotHandlerMgr *pAnnotHandler=m_pEnv->GetAnnotHandlerMgr();
534                 if(pAnnotHandler)
535                 {
536                         CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
537                         m_pFocusAnnot = NULL;
538
539                         if (!pAnnotHandler->Annot_OnChangeFocus(NULL, pFocusAnnot))
540                                 return FALSE;
541
542                         if(pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag))
543                         {
544                                 
545                                 if(pFocusAnnot->GetType() == FX_BSTRC("Widget"))
546                                 {
547                                         CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
548                                         int nFieldType = pWidget->GetFieldType();
549                                         if(FIELDTYPE_TEXTFIELD == nFieldType || FIELDTYPE_COMBOBOX == nFieldType)
550                                                 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
551                                 }
552
553                                 if(!m_pFocusAnnot)
554                                         return TRUE;
555                         }
556                         else
557                         {
558                                 m_pFocusAnnot = pFocusAnnot;
559                         }
560                 }
561         }
562         return FALSE;
563 }
564
565 FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int  nCount)
566 {
567         if ( nStart < 0 || nStart >= GetPageCount() || nCount <= 0 )
568         {
569                 return FALSE;
570         }
571
572         CPDFXFA_Page * pTempPage = NULL;
573         for ( int i = nCount-1; i >= 0; i-- )
574         {
575                 pTempPage = GetPage(nStart+i);
576                 if ( pTempPage != NULL )
577                 {
578                         ReMovePageView(pTempPage);
579                 }
580         }
581         return TRUE;
582 }
583
584 void CPDFSDK_Document::OnCloseDocument()
585 {
586         KillFocusAnnot();
587 }
588
589 FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag)
590 {
591         FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
592         return dwPermissions&nFlag;
593 }
594
595 IFXJS_Runtime * CPDFSDK_Document::GetJsRuntime()
596 {
597         ASSERT(m_pEnv!=NULL);
598         return m_pEnv->GetJSRuntime();
599 }
600
601 CFX_WideString  CPDFSDK_Document::GetPath() 
602 {
603         ASSERT(m_pEnv != NULL);
604         return m_pEnv->JS_docGetFilePath();
605 }
606
607
608 CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,CPDFXFA_Page* page):m_page(page),m_pSDKDoc(pSDKDoc)
609 {
610         CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
611         if(pInterForm)
612         {
613                 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
614                 if (page->GetPDFPage())
615                         pPDFInterForm->FixPageFields(page->GetPDFPage());
616         }
617
618         m_fxAnnotArray.RemoveAll();
619
620         m_bEnterWidget = FALSE;
621         m_bExitWidget = FALSE;
622         m_bOnWidget = FALSE;
623         m_CaptureWidget = NULL;
624         m_bValid = FALSE;
625         m_bLocked = FALSE;
626         m_pAnnotList = NULL;
627 }
628
629 CPDFSDK_PageView::~CPDFSDK_PageView()
630 {
631         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();        
632         int nAnnotCount = m_fxAnnotArray.GetSize();
633         for (int i=0; i<nAnnotCount; i++)
634         {
635                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
636                 //if there is a focused annot on the page, we should kill the focus first.
637                 if(pAnnot == m_pSDKDoc->GetFocusAnnot())
638                         KillFocusAnnot();
639                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
640                 ASSERT(pAnnotHandlerMgr);
641                 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
642         }
643         m_fxAnnotArray.RemoveAll();
644         if(m_pAnnotList)
645         {
646                 delete m_pAnnotList;
647                 m_pAnnotList = NULL;
648         }
649 }
650
651 void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,CPDF_RenderOptions* pOptions, FX_RECT* pClip)
652 {
653         m_curMatrix = *pUser2Device;
654         //      m_pAnnotList->DisplayAnnots(m_page, pDevice, pUser2Device, FALSE, pOptions);
655         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
656         CPDFXFA_Page* pPage = GetPDFXFAPage();
657
658         if (pPage == NULL) return;
659
660         if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
661                 CFX_Graphics gs;
662                 gs.Create(pDevice);
663                 if (pClip) {
664                         CFX_RectF rectClip;
665                         rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
666                                 static_cast<FX_FLOAT>(pClip->top),
667                                 static_cast<FX_FLOAT>(pClip->Width()),
668                                 static_cast<FX_FLOAT>(pClip->Height()));
669                         gs.SetClipRect(rectClip);
670                 }       
671                 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
672                 if (!pRenderContext) 
673                         return;
674                 CXFA_RenderOptions renderOptions;
675                 renderOptions.m_bHighlight = TRUE;
676                 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device, renderOptions);
677                 pRenderContext->DoRender();
678                 pRenderContext->StopRender();
679                 pRenderContext->Release();
680                 return;
681         }
682         // for pdf/static xfa.
683         CPDFSDK_AnnotIterator annotIterator(this, TRUE);
684         CPDFSDK_Annot * pSDKAnnot=NULL;
685         int index=-1;
686         pSDKAnnot = annotIterator.Next(index);
687         while(pSDKAnnot)
688         {
689                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
690                 ASSERT(pAnnotHandlerMgr);
691                 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
692                 pSDKAnnot = annotIterator.Next(index);
693         }
694 }
695 CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
696 {
697         
698         int nCount = m_pAnnotList->Count();
699         for(int i = 0 ; i<nCount; i++)
700         {
701                 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
702                 CFX_FloatRect annotRect;
703                 pAnnot->GetRect(annotRect);
704                 if(annotRect.Contains(pageX, pageY))
705                         return pAnnot;
706         }
707         return NULL;
708 }
709
710 CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
711 {
712         
713         int nCount = m_pAnnotList->Count();
714         for(int i = 0 ; i<nCount; i++)
715         {
716                 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
717                 if(pAnnot->GetSubType() == "Widget")
718                 {
719                         CFX_FloatRect annotRect;
720                         pAnnot->GetRect(annotRect);
721                         if(annotRect.Contains(pageX, pageY))
722                                 return pAnnot;
723                 }
724         }
725         return NULL;
726 }
727
728 CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
729 {
730
731         CPDFSDK_AnnotIterator annotIterator(this, FALSE);
732         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
733         CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
734         CPDFSDK_Annot* pSDKAnnot = NULL;
735         int index = -1;
736         pSDKAnnot = annotIterator.Next(index);
737         while(pSDKAnnot)
738         {
739                 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
740                 if(rc.Contains(pageX, pageY))
741                         return pSDKAnnot;
742                 pSDKAnnot = annotIterator.Next(index);
743         }
744
745         return NULL;
746 }
747
748 CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY)
749 {
750
751         CPDFSDK_AnnotIterator annotIterator(this, FALSE);
752         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
753         CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
754         CPDFSDK_Annot* pSDKAnnot = NULL;
755         int index = -1;
756         pSDKAnnot = annotIterator.Next(index);
757         while(pSDKAnnot)
758         {
759                 if(pSDKAnnot->GetType() == "Widget" || pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME)
760                 {       
761                         pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
762                         CPDF_Point point(pageX, pageY);
763                         if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
764 //                      if(rc.Contains(pageX, pageY))
765                                 return pSDKAnnot;
766                 }
767                 pSDKAnnot = annotIterator.Next(index);
768         }
769         
770         return NULL;
771 }
772
773
774 FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot)
775 {
776         CPDF_Dictionary* pAnnotDic = pAnnot->m_pAnnotDict;
777         if(pAnnotDic)
778                 return  pAnnotDic->KeyExist("AS");
779         return FALSE;
780 }
781
782 CPDFSDK_Annot*  CPDFSDK_PageView::AddAnnot(CPDF_Annot * pPDFAnnot)
783 {
784         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
785         ASSERT(pEnv);
786         CPDFSDK_AnnotHandlerMgr * pAnnotHandler= pEnv->GetAnnotHandlerMgr();    
787         
788         CPDFSDK_Annot* pSDKAnnot =NULL;
789         
790         if(pAnnotHandler)
791         {
792                 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
793         }
794         if(!pSDKAnnot)   
795                 return NULL;
796
797         m_fxAnnotArray.Add(pSDKAnnot);  
798                 
799         if(pAnnotHandler)
800         {                                
801                 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
802                 
803         }
804
805          return pSDKAnnot;
806 }
807
808 CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot)
809 {
810         if (!pPDFAnnot) return NULL;
811
812         CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
813         if (pSDKAnnot)
814                 return pSDKAnnot;
815
816         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
817         ASSERT(pEnv);
818         CPDFSDK_AnnotHandlerMgr * pAnnotHandler= pEnv->GetAnnotHandlerMgr();    
819
820         pSDKAnnot =NULL;
821
822         if(pAnnotHandler)
823         {
824                 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
825         }
826         if(!pSDKAnnot)   
827                 return NULL;
828
829         m_fxAnnotArray.Add(pSDKAnnot);  
830
831         return pSDKAnnot;
832 }
833
834 CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary * pDict)
835 {
836         if(pDict)  
837                 return this->AddAnnot(pDict->GetString("Subtype"),pDict);
838          return NULL;
839 }
840
841 CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(FX_LPCSTR lpSubType,CPDF_Dictionary * pDict)
842 {
843         return NULL;
844 }
845
846 FX_BOOL  CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot)
847 {
848         if (!pAnnot)
849                 return FALSE;
850         CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
851         if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA && pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
852                 return FALSE;
853
854         int index = m_fxAnnotArray.Find(pAnnot);
855         m_fxAnnotArray.RemoveAt(index);
856         if (m_CaptureWidget == pAnnot)
857                 m_CaptureWidget = NULL;
858
859         return TRUE;    
860 }
861
862 CPDF_Document* CPDFSDK_PageView::GetPDFDocument()
863 {
864         if(m_page)
865         {
866                 return m_page->GetDocument()->GetPDFDoc();
867         }
868         return NULL;
869 }
870
871 CPDF_Page* CPDFSDK_PageView::GetPDFPage()
872 {
873         if (m_page)
874         {
875                 return m_page->GetPDFPage();
876         }
877
878         return NULL;
879 }
880
881 int     CPDFSDK_PageView::CountAnnots()
882 {
883         return m_fxAnnotArray.GetSize();
884 }
885
886 CPDFSDK_Annot*  CPDFSDK_PageView::GetAnnot(int nIndex)
887 {
888         int nCount = m_fxAnnotArray.GetSize();
889         if ( nIndex < 0 || nIndex >= nCount )
890         {
891                 return NULL;
892         }
893
894         return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
895 }
896
897 CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary * pDict)
898 {
899         int nCount = m_fxAnnotArray.GetSize();
900         for(int i=0; i<nCount; i++)
901         {
902                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
903                 if(pDict==pAnnot->GetPDFAnnot()->m_pAnnotDict) 
904                         return pAnnot;
905         }
906         return NULL;
907 }
908 CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget)
909 {
910         if (hWidget == NULL)
911                 return NULL;
912         int annotCount = m_fxAnnotArray.GetSize();
913
914         for(int i = 0; i < annotCount; i++)
915         {
916                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
917                 if(pAnnot->GetXFAWidget() == hWidget)
918                         return pAnnot;
919         }
920         return NULL;
921 }
922
923 FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point & point, FX_UINT nFlag)
924 {
925         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
926         ASSERT(pEnv);
927         CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
928         if(!pFXAnnot)
929         {
930                 KillFocusAnnot(nFlag);
931         }
932         else
933         {
934                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
935                 ASSERT(pAnnotHandlerMgr);
936
937                 FX_BOOL bRet = pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag,point);
938                 if(bRet)
939                 {
940                         SetFocusAnnot(pFXAnnot);
941                 }
942                 return bRet;
943         }
944         return FALSE;
945 }
946
947 FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point & point, FX_UINT nFlag)
948 {
949         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
950         ASSERT(pEnv);
951         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
952         ASSERT(pAnnotHandlerMgr);
953
954         CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
955
956         if (pFXAnnot == NULL)
957                 return FALSE;
958
959         FX_BOOL bRet = pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag,point);
960         if (bRet)
961         {
962                 SetFocusAnnot(pFXAnnot);
963         }
964         return TRUE;
965 }
966
967 FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point & point, FX_UINT nFlag)
968 {
969         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
970         ASSERT(pEnv);
971         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
972         ASSERT(pAnnotHandlerMgr);
973
974         CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
975
976         if (pFXAnnot == NULL)
977                 return FALSE;
978
979         FX_BOOL bRet = pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag,point);
980         if (bRet)
981         {
982                 SetFocusAnnot(pFXAnnot);
983         }
984         return TRUE;
985 }
986
987 FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point & point, FX_UINT nFlag)
988 {
989         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
990         ASSERT(pEnv);
991         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
992         ASSERT(pAnnotHandlerMgr);
993         CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
994         CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
995         FX_BOOL bRet  = FALSE;
996         if(pFocusAnnot && pFocusAnnot != pFXAnnot)
997         {
998                 //Last focus Annot gets a chance to handle the event.
999                 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag,point);
1000         }
1001         if(pFXAnnot && !bRet)
1002         {
1003                 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag,point);
1004                 return bRet;
1005         }
1006         return bRet;
1007 }
1008
1009 FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point & point, int nFlag)
1010 {
1011
1012         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1013         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1014         ASSERT(pAnnotHandlerMgr);
1015         if(CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y))
1016         {
1017                 if(m_CaptureWidget && m_CaptureWidget != pFXAnnot)
1018                 {
1019                         m_bExitWidget = TRUE;
1020                         m_bEnterWidget = FALSE;
1021                         pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1022                 }
1023                 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
1024                 m_bOnWidget = TRUE;
1025                 if(!m_bEnterWidget)
1026                 {
1027                         m_bEnterWidget = TRUE;
1028                         m_bExitWidget = FALSE;
1029                         pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot,nFlag);
1030                 }
1031                 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
1032                 return TRUE;
1033         }
1034         else
1035         {
1036                 if(m_bOnWidget)
1037                 {       
1038                         m_bOnWidget = FALSE;
1039                         m_bExitWidget = TRUE;
1040                         m_bEnterWidget = FALSE;
1041                         if(m_CaptureWidget)
1042                         {
1043                                 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1044                                 m_CaptureWidget = NULL;
1045                         }
1046                 }
1047                 return FALSE;
1048         }
1049         
1050         return FALSE;;
1051 }
1052
1053 FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX, double deltaY,const CPDF_Point& point, int nFlag)
1054 {
1055         if(CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y))
1056         {
1057                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1058                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1059                 ASSERT(pAnnotHandlerMgr);
1060                 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag, (int)deltaY, point);
1061         }
1062         return FALSE;
1063
1064 }
1065
1066 FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag)
1067 {
1068         if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
1069         {
1070                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1071                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1072                 ASSERT(pAnnotHandlerMgr);
1073                 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1074         }
1075
1076         return FALSE;
1077 }
1078
1079 FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag)
1080 {
1081         if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
1082         {
1083                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1084                 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1085                 ASSERT(pAnnotHandlerMgr);
1086                 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1087         }
1088         return FALSE;
1089 }
1090
1091 FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag)
1092 {
1093 //      if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
1094 //      {
1095 //              CFFL_IFormFiller* pIFormFiller = g_pFormFillApp->GetIFormFiller();
1096 //              return pIFormFiller->OnKeyUp(pAnnot, nKeyCode, nFlag);
1097 //      }
1098         return FALSE;
1099 }
1100
1101 extern void CheckUnSupportAnnot(CPDF_Document * pDoc, CPDF_Annot* pPDFAnnot);
1102
1103 void CPDFSDK_PageView::LoadFXAnnots()
1104 {
1105         ASSERT(m_page != NULL);
1106
1107         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1108         ASSERT(pEnv != NULL);
1109
1110         CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1111         ASSERT(pAnnotHandlerMgr != NULL);
1112
1113         SetLock(TRUE);
1114         m_page->AddRef();
1115         if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA)
1116         {
1117                 IXFA_PageView* pageView = NULL;
1118                 pageView = m_page->GetXFAPageView();
1119                 ASSERT(pageView != NULL);
1120
1121                 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible|XFA_WIDGETFILTER_Viewable|XFA_WIDGETFILTER_AllType);
1122                 if (!pWidgetHander)
1123                 {
1124                         m_page->Release();
1125                         SetLock(FALSE);
1126                         return;
1127                 }
1128
1129         IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext();
1130                 while (pXFAAnnot) {
1131                         CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
1132                         if(!pAnnot) {
1133                                 pXFAAnnot = pWidgetHander->MoveToNext();
1134                                 continue;
1135                         }
1136                         m_fxAnnotArray.Add(pAnnot);
1137                 
1138                         pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1139
1140                         pXFAAnnot = pWidgetHander->MoveToNext();
1141
1142                 }
1143
1144                 pWidgetHander->Release();
1145         } 
1146         else 
1147         {
1148                 CPDF_Page* pPage = m_page->GetPDFPage();
1149                 ASSERT(pPage != NULL);
1150
1151                 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1152                 //Disable the default AP construction.
1153                 CPDF_InterForm::EnableUpdateAP(FALSE);
1154                 m_pAnnotList = new CPDF_AnnotList(pPage);
1155                 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1156
1157                 int nCount = m_pAnnotList->Count();
1158                 for(int i=0; i<nCount; i++)
1159                 {
1160                         CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1161                         CPDF_Document * pDoc = this->GetPDFDocument();
1162
1163                         CheckUnSupportAnnot(pDoc, pPDFAnnot);
1164
1165                         CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1166                         if(!pAnnot)
1167                                 continue;
1168                         m_fxAnnotArray.Add(pAnnot);
1169
1170                         pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1171                 }
1172
1173         }
1174         m_page->Release();
1175         SetLock(FALSE);
1176 }
1177
1178 void    CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects)
1179 {
1180         for(int i=0; i<rects.GetSize(); i++)
1181         {
1182                 CPDF_Rect rc = rects.GetAt(i);
1183                 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1184                 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1185         }
1186 }
1187
1188 void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot)
1189 {
1190         CPDF_Rect rcWindow;
1191
1192         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();        
1193 //      CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
1194         
1195         rcWindow = pAnnot->GetRect();//pAnnotHandler->Annot_OnGetViewBBox(this,pAnnot); 
1196         pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right, rcWindow.bottom);
1197
1198 }
1199
1200 int CPDFSDK_PageView::GetPageIndex()
1201 {
1202         if(m_page)
1203         {
1204                 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1205                 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1206                 if(pDoc && pDic)
1207                 {
1208                         return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1209                 }
1210         }
1211         return -1;
1212 }
1213
1214 FX_BOOL CPDFSDK_PageView::IsValidAnnot(FX_LPVOID p)
1215 {
1216         if (p == NULL) return FALSE;
1217         int iCount = m_pAnnotList->Count();
1218         for (int i = 0; i < iCount; i++)
1219         {
1220                 if (m_pAnnotList->GetAt(i) == p)
1221                         return TRUE;
1222         }
1223         return FALSE;
1224 }
1225
1226
1227 CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot()
1228 {
1229         CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1230         if(!pFocusAnnot)
1231                 return NULL;
1232         
1233         for(int i=0; i<m_fxAnnotArray.GetSize(); i++)
1234         {
1235                 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1236                 if(pAnnot == pFocusAnnot)
1237                         return pAnnot;
1238         }
1239         return NULL;
1240 }
1241