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