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