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