blob: a3e77e45148c83e37ede9f4a348d8394cce97fe9 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhang60f507b2015-06-13 00:41:00 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez1ed8a212015-05-11 15:25:39 -07007#include "../../public/fpdf_ext.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07008#include "../include/fsdk_define.h"
Bo Xufdc00a72014-10-28 23:03:33 -07009#include "../include/fpdfxfa/fpdfxfa_doc.h"
10#include "../include/fpdfxfa/fpdfxfa_page.h"
11#include "../include/fpdfxfa/fpdfxfa_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012#include "../include/fsdk_mgr.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013#include "../include/formfiller/FFL_FormFiller.h"
14#include "../include/javascript/IJavaScript.h"
Bo Xufdc00a72014-10-28 23:03:33 -070015#include "../include/fpdfxfa/fpdfxfa_app.h"
Tom Sepez854a7f62015-09-21 17:07:49 -070016#include "../include/javascript/JS_Runtime.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
18#if _FX_OS_ == _FX_ANDROID_
19#include "time.h"
20#else
21#include <ctime>
22#endif
23
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024class CFX_SystemHandler : public IFX_SystemHandler {
25 public:
26 CFX_SystemHandler(CPDFDoc_Environment* pEnv) : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070027 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070030 // IFX_SystemHandler
31 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
32 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
33 FX_BOOL IsSelectionImplemented() override;
34 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
35 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 return FALSE;
37 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070038 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
39 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
40 void SetCursor(int32_t nCursorType) override;
41 FX_HMENU CreatePopupMenu() override { return NULL; }
42 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
43 int32_t nIDNewItem,
44 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 return FALSE;
46 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070047 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
48 int32_t nIDItem,
49 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 return FALSE;
51 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070052 int32_t TrackPopupMenu(FX_HMENU hMenu,
53 int32_t x,
54 int32_t y,
55 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 return -1;
57 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070058 void DestroyMenu(FX_HMENU hMenu) override {}
59 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
60 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
61 CFX_ByteString sFontFaceName) override;
62 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
63 CFX_ByteString sFontFaceName,
64 uint8_t nCharset) override;
65 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
66 void KillTimer(int32_t nID) override;
67 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
69 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070070 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
72 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070073 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 return m_pEnv->FFI_IsALTKeyDown(nFlag);
75 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070076 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
78 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070079 FX_SYSTEMTIME GetLocalTime() override;
80 int32_t GetCharSet() override { return m_nCharSet; }
81 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082
83 private:
84 CPDFDoc_Environment* m_pEnv;
85 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086};
87
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
89 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090}
91
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
93 // g_pFormFillApp->FFI_Invalidate();
94 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
95 CPDFXFA_Page* pPage = NULL;
96 CPDFSDK_PageView* pPageView = NULL;
97 pPageView = pSDKAnnot->GetPageView();
98 pPage = pSDKAnnot->GetPDFXFAPage();
99 if (!pPage || !pPageView)
100 return;
101 CPDF_Matrix page2device;
102 pPageView->GetCurrentMatrix(page2device);
103 CPDF_Matrix device2page;
104 device2page.SetReverse(page2device);
105 FX_FLOAT left, top, right, bottom;
106 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
107 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
108 bottom);
109 // m_pEnv->FFI_DeviceToPage(pPage, rect.left, rect.top, (double*)&left,
110 // (double*)&top);
111 // m_pEnv->FFI_DeviceToPage(pPage, rect.right, rect.bottom, (double*)&right,
112 // (double*)&bottom);
113 CPDF_Rect rcPDF(left, bottom, right, top);
114 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
117 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700118}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
120 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
121 if (pFFL) {
122 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
123 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
124 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
125 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
126 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
127 ASSERT(pAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700128 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 ASSERT(pPage);
130 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
131 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132}
133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134FX_BOOL CFX_SystemHandler::IsSelectionImplemented() {
135 if (m_pEnv) {
136 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
137 if (pInfo && pInfo->FFI_OutputSelectedRect)
138 return TRUE;
139 }
140 return FALSE;
141}
142
143CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) {
144 return "";
145}
146
147FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(
148 int32_t nCharset,
149 CFX_ByteString sFontFaceName) {
150 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
151 if (pFontMgr) {
152 CFX_FontMapper* pFontMapper = pFontMgr->m_pBuiltinMapper;
153 if (pFontMapper) {
154 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
155 if (nSize == 0) {
156 pFontMapper->LoadInstalledFonts();
157 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
158 }
159
160 for (int i = 0; i < nSize; i++) {
161 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
162 return TRUE;
163 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700164 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 }
166
167 return FALSE;
168}
169
170static int CharSet2CP(int charset) {
171 if (charset == 128)
172 return 932;
173 if (charset == 134)
174 return 936;
175 if (charset == 129)
176 return 949;
177 if (charset == 136)
178 return 950;
179 return 0;
180}
181CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
182 CPDF_Document* pDoc,
183 CFX_ByteString sFontFaceName,
184 uint8_t nCharset) {
185 if (pDoc) {
186 CFX_Font* pFXFont = new CFX_Font();
187 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
188 FALSE);
189 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
190 delete pFXFont;
191 return pFont;
192 }
193
194 return NULL;
195}
196
197int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
198 TimerCallback lpTimerFunc) {
199 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
200}
201void CFX_SystemHandler::KillTimer(int32_t nID) {
202 m_pEnv->FFI_KillTimer(nID);
203}
204
205FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
206 return m_pEnv->FFI_GetLocalTime();
207}
208
209CPDFDoc_Environment::CPDFDoc_Environment(CPDFXFA_Document* pDoc,
210 FPDF_FORMFILLINFO* pFFinfo)
211 : m_pAnnotHandlerMgr(NULL),
212 m_pActionHandler(NULL),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 m_pInfo(pFFinfo),
214 m_pSDKDoc(NULL),
215 m_pXFADoc(pDoc),
216 m_pIFormFiller(NULL) {
217 m_pSysHandler = new CFX_SystemHandler(this);
218}
219
220CPDFDoc_Environment::~CPDFDoc_Environment() {
221 delete m_pIFormFiller;
222 m_pIFormFiller = NULL;
223
224 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 if (pProvider->m_pEnvList.GetSize() == 0) {
226 pProvider->ReleaseRuntime();
227 pProvider->InitRuntime(TRUE);
228 }
229
230 delete m_pSysHandler;
231 m_pSysHandler = NULL;
232
233 delete m_pAnnotHandlerMgr;
234 m_pAnnotHandlerMgr = NULL;
235
236 delete m_pActionHandler;
237 m_pActionHandler = NULL;
238}
239
240int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
241 const FX_WCHAR* Title,
242 FX_UINT Type,
243 FX_UINT Icon) {
244 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
245 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
246 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
247 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
248 FPDF_WIDESTRING pTitle =
249 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
250 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
251 pTitle, Type, Icon);
252 bsMsg.ReleaseBuffer();
253 bsTitle.ReleaseBuffer();
254 return ret;
255 }
256 return -1;
257}
258
259int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
260 const FX_WCHAR* Title,
261 const FX_WCHAR* Default,
262 const FX_WCHAR* cLabel,
263 FPDF_BOOL bPassword,
264 void* response,
265 int length) {
266 if (m_pInfo && m_pInfo->m_pJsPlatform &&
267 m_pInfo->m_pJsPlatform->app_response) {
268 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
269 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
270 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
271 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
272 FPDF_WIDESTRING pQuestion =
273 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
274 FPDF_WIDESTRING pTitle =
275 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
276 FPDF_WIDESTRING pDefault =
277 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
278 FPDF_WIDESTRING pLabel =
279 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
280 int ret = m_pInfo->m_pJsPlatform->app_response(
281 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
282 response, length);
283 bsQuestion.ReleaseBuffer();
284 bsTitle.ReleaseBuffer();
285 bsDefault.ReleaseBuffer();
286 bsLabel.ReleaseBuffer();
287 return ret;
288 }
289 return -1;
290}
291
292CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
293 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
294 !m_pInfo->m_pJsPlatform->Field_browse) {
295 return L"";
296 }
297
298 const int nRequiredLen =
299 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
300 if (nRequiredLen <= 0)
301 return L"";
302
303 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
304 memset(pBuff.get(), 0, nRequiredLen);
305 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
306 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
307 if (nActualLen <= 0 || nActualLen > nRequiredLen)
308 return L"";
309
310 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
311 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
312 return wsRet;
313}
314
315CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
316 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
317 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
318 return L"";
319 }
320
321 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
322 m_pInfo->m_pJsPlatform, nullptr, 0);
323 if (nRequiredLen <= 0)
324 return L"";
325
326 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
327 memset(pBuff.get(), 0, nRequiredLen);
328 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
329 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
330 if (nActualLen <= 0 || nActualLen > nRequiredLen)
331 return L"";
332
333 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
334 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
335 return wsRet;
336}
337
338void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
339 int length,
340 const FX_WCHAR* URL) {
341 if (m_pInfo && m_pInfo->m_pJsPlatform &&
342 m_pInfo->m_pJsPlatform->Doc_submitForm) {
343 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
344 FPDF_WIDESTRING pDestination =
345 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
346 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
347 length, pDestination);
348 bsDestination.ReleaseBuffer();
349 }
350}
351
352void CPDFDoc_Environment::JS_docmailForm(void* mailData,
353 int length,
354 FPDF_BOOL bUI,
355 const FX_WCHAR* To,
356 const FX_WCHAR* Subject,
357 const FX_WCHAR* CC,
358 const FX_WCHAR* BCC,
359 const FX_WCHAR* Msg) {
360 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
361 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
362 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
363 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
364 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
365 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
366 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
367 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
368 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
369 FPDF_WIDESTRING pSubject =
370 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
371 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
372 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
373 bUI, pTo, pSubject, pCC, pBcc, pMsg);
374 bsTo.ReleaseBuffer();
375 bsCC.ReleaseBuffer();
376 bsBcc.ReleaseBuffer();
377 bsSubject.ReleaseBuffer();
378 bsMsg.ReleaseBuffer();
379 }
380}
381
382IFXJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
383 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700384 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 if (!m_pJSRuntime)
Tom Sepez854a7f62015-09-21 17:07:49 -0700386 m_pJSRuntime.reset(new CJS_Runtime(this));
387 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700388}
389
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
391 if (!m_pAnnotHandlerMgr)
392 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
393 return m_pAnnotHandlerMgr;
Bo Xufdc00a72014-10-28 23:03:33 -0700394}
395
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
397 if (!m_pActionHandler)
Tom Sepez3b5f1242015-09-01 14:06:55 -0700398 m_pActionHandler = new CPDFSDK_ActionHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 return m_pActionHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700400}
401
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
403 if (!m_pIFormFiller)
404 m_pIFormFiller = new CFFL_IFormFiller(this);
405 return m_pIFormFiller;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700406}
407
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
409 CPDFDoc_Environment* pEnv)
410 : m_pDoc(pDoc),
411 m_pInterForm(nullptr),
412 m_pFocusAnnot(nullptr),
413 m_pEnv(pEnv),
414 m_pOccontent(nullptr),
415 m_bChangeMask(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700416
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417CPDFSDK_Document::~CPDFSDK_Document() {
418 for (auto& it : m_pageMap)
419 delete it.second;
420 m_pageMap.clear();
421
422 delete m_pInterForm;
423 m_pInterForm = nullptr;
424
425 delete m_pOccontent;
426 m_pOccontent = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700427}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
430 FX_BOOL ReNew) {
431 auto it = m_pageMap.find(pPDFXFAPage);
432 if (it != m_pageMap.end())
433 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700434
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 if (!ReNew)
436 return nullptr;
437
438 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
439 m_pageMap[pPDFXFAPage] = pPageView;
440 // Delay to load all the annotations, to avoid endless loop.
441 pPageView->LoadFXAnnots();
442 return pPageView;
443}
444
445CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
446 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
447 return pPage ? GetPageView(pPage, TRUE) : nullptr;
448}
449
450CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
451 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
452 if (!pTempPage)
453 return nullptr;
454
455 auto it = m_pageMap.find(pTempPage);
456 return it->second;
457}
458
459void CPDFSDK_Document::ProcJavascriptFun() {
460 CPDFXFA_Document* pPDFDoc = GetDocument();
461 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
462 int iCount = docJS.CountJSActions();
463 if (iCount < 1)
464 return;
465 for (int i = 0; i < iCount; i++) {
466 CFX_ByteString csJSName;
467 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
468 if (m_pEnv->GetActionHander())
469 m_pEnv->GetActionHander()->DoAction_JavaScript(
470 jsAction, CFX_WideString::FromLocal(csJSName), this);
471 }
472}
473
474FX_BOOL CPDFSDK_Document::ProcOpenAction() {
475 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700476 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477
478 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
479 if (!pRoot)
480 return FALSE;
481
482 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
483 if (!pOpenAction)
484 pOpenAction = pRoot->GetArray("OpenAction");
485
486 if (!pOpenAction)
487 return FALSE;
488
489 if (pOpenAction->GetType() == PDFOBJ_ARRAY)
490 return TRUE;
491
492 if (pOpenAction->GetType() == PDFOBJ_DICTIONARY) {
493 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pOpenAction;
494 CPDF_Action action(pDict);
495 if (m_pEnv->GetActionHander())
496 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
497 return TRUE;
498 }
499 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700500}
501
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
503 if (!m_pOccontent)
504 m_pOccontent = new CPDF_OCContext(m_pDoc->GetPDFDoc());
505 return m_pOccontent;
506}
Bo Xufdc00a72014-10-28 23:03:33 -0700507
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
509 auto it = m_pageMap.find(pPDFXFAPage);
510 if (it == m_pageMap.end())
511 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700512
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700513 CPDFSDK_PageView* pPageView = it->second;
514 if (pPageView->IsLocked())
515 return;
516
517 delete pPageView;
518 m_pageMap.erase(it);
519}
520
521CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
522 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
523 if (!pTempPage)
524 return NULL;
525 return pTempPage;
526}
527
528CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
529 if (!m_pInterForm)
530 m_pInterForm = new CPDFSDK_InterForm(this);
531 return m_pInterForm;
532}
533
534void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
535 CPDFSDK_Annot* pAnnot) {
536 for (const auto& it : m_pageMap) {
537 CPDFSDK_PageView* pPageView = it.second;
538 if (pPageView != pSender) {
539 pPageView->UpdateView(pAnnot);
540 }
541 }
542}
543
544CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
545 return m_pFocusAnnot;
546}
547
548FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
549 if (m_pFocusAnnot == pAnnot)
550 return TRUE;
551
552 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
553
554 if (m_pFocusAnnot) {
555 if (!KillFocusAnnot(nFlag))
556 return FALSE;
557 }
558 CPDFSDK_PageView* pPageView = NULL;
559 if (pAnnot)
560 pPageView = pAnnot->GetPageView();
561 if (pAnnot && pPageView->IsValid()) {
562 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
563
564 if (pAnnotHandler && !m_pFocusAnnot) {
565 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700566 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700567
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
569 return FALSE;
570 if (!m_pFocusAnnot) {
571 m_pFocusAnnot = pAnnot;
572 return TRUE;
573 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700574 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575 }
576 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700577}
578
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
580 if (m_pFocusAnnot) {
581 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
582 if (pAnnotHandler) {
583 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
584 m_pFocusAnnot = NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700585
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 if (!pAnnotHandler->Annot_OnChangeFocus(NULL, pFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700587 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700588
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700589 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
590 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
591 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
592 int nFieldType = pWidget->GetFieldType();
593 if (FIELDTYPE_TEXTFIELD == nFieldType ||
594 FIELDTYPE_COMBOBOX == nFieldType)
595 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
596 }
597
598 if (!m_pFocusAnnot)
599 return TRUE;
600 } else {
601 m_pFocusAnnot = pFocusAnnot;
602 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700603 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 }
605 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700606}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700607
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
609 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
610 return FALSE;
611 }
612
613 CPDFXFA_Page* pTempPage = NULL;
614 for (int i = nCount - 1; i >= 0; i--) {
615 pTempPage = GetPage(nStart + i);
616 if (pTempPage != NULL) {
617 ReMovePageView(pTempPage);
618 }
619 }
620 return TRUE;
621}
622
623void CPDFSDK_Document::OnCloseDocument() {
624 KillFocusAnnot();
625}
626
627FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
628 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
629 return dwPermissions & nFlag;
630}
631
632IFXJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
633 ASSERT(m_pEnv != NULL);
634 return m_pEnv->GetJSRuntime();
635}
636
637CFX_WideString CPDFSDK_Document::GetPath() {
638 ASSERT(m_pEnv != NULL);
639 return m_pEnv->JS_docGetFilePath();
640}
641
642CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
643 CPDFXFA_Page* page)
644 : m_page(page), m_pSDKDoc(pSDKDoc) {
645 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
646 if (pInterForm) {
647 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
648 if (page->GetPDFPage())
649 pPDFInterForm->FixPageFields(page->GetPDFPage());
650 }
651 m_fxAnnotArray.RemoveAll();
652
653 m_bEnterWidget = FALSE;
654 m_bExitWidget = FALSE;
655 m_bOnWidget = FALSE;
656 m_CaptureWidget = NULL;
657 m_bValid = FALSE;
658 m_bLocked = FALSE;
659 m_pAnnotList = NULL;
660}
661
662CPDFSDK_PageView::~CPDFSDK_PageView() {
Tom Sepez396e8722015-09-09 10:16:08 -0700663 // if there is a focused annot on the page, we should kill the focus first.
664 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
665 for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
666 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
667 if (pAnnot == focusedAnnot) {
668 KillFocusAnnot();
669 break;
670 }
671 }
672 }
673
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700674 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700675 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
676 ASSERT(pAnnotHandlerMgr);
677 for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700678 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
680 }
681 m_fxAnnotArray.RemoveAll();
682
683 delete m_pAnnotList;
684 m_pAnnotList = NULL;
685}
686
687void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
688 CPDF_Matrix* pUser2Device,
689 CPDF_RenderOptions* pOptions,
690 FX_RECT* pClip) {
691 m_curMatrix = *pUser2Device;
692 // m_pAnnotList->DisplayAnnots(m_page, pDevice, pUser2Device, FALSE,
693 // pOptions);
694 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
695 CPDFXFA_Page* pPage = GetPDFXFAPage();
696
697 if (pPage == NULL)
698 return;
699
700 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
701 CFX_Graphics gs;
702 gs.Create(pDevice);
703 if (pClip) {
704 CFX_RectF rectClip;
705 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
706 static_cast<FX_FLOAT>(pClip->top),
707 static_cast<FX_FLOAT>(pClip->Width()),
708 static_cast<FX_FLOAT>(pClip->Height()));
709 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700710 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
712 if (!pRenderContext)
713 return;
714 CXFA_RenderOptions renderOptions;
715 renderOptions.m_bHighlight = TRUE;
716 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
717 renderOptions);
718 pRenderContext->DoRender();
719 pRenderContext->StopRender();
720 pRenderContext->Release();
721 return;
722 }
723 // for pdf/static xfa.
724 CPDFSDK_AnnotIterator annotIterator(this, TRUE);
725 CPDFSDK_Annot* pSDKAnnot = NULL;
726 int index = -1;
727 pSDKAnnot = annotIterator.Next(index);
728 while (pSDKAnnot) {
729 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
730 ASSERT(pAnnotHandlerMgr);
731 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
732 pSDKAnnot = annotIterator.Next(index);
733 }
734}
735CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
736 FX_FLOAT pageY) {
737 int nCount = m_pAnnotList->Count();
738 for (int i = 0; i < nCount; i++) {
739 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
740 CFX_FloatRect annotRect;
741 pAnnot->GetRect(annotRect);
742 if (annotRect.Contains(pageX, pageY))
743 return pAnnot;
744 }
745 return NULL;
746}
747
748CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
749 FX_FLOAT pageY) {
750 int nCount = m_pAnnotList->Count();
751 for (int i = 0; i < nCount; i++) {
752 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
753 if (pAnnot->GetSubType() == "Widget") {
754 CFX_FloatRect annotRect;
755 pAnnot->GetRect(annotRect);
756 if (annotRect.Contains(pageX, pageY))
757 return pAnnot;
758 }
759 }
760 return NULL;
761}
762
763CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
764 FX_FLOAT pageY) {
765 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
766 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
767 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
768 CPDFSDK_Annot* pSDKAnnot = NULL;
769 int index = -1;
770 pSDKAnnot = annotIterator.Next(index);
771 while (pSDKAnnot) {
772 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
773 if (rc.Contains(pageX, pageY))
774 return pSDKAnnot;
775 pSDKAnnot = annotIterator.Next(index);
776 }
777
778 return NULL;
779}
780
781CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
782 FX_FLOAT pageY) {
783 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
784 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
785 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
786 CPDFSDK_Annot* pSDKAnnot = NULL;
787 int index = -1;
788 pSDKAnnot = annotIterator.Next(index);
789 while (pSDKAnnot) {
790 if (pSDKAnnot->GetType() == "Widget" ||
791 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
792 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
793 CPDF_Point point(pageX, pageY);
794 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
795 return pSDKAnnot;
796 }
797 pSDKAnnot = annotIterator.Next(index);
798 }
799
800 return NULL;
801}
802
803FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
804 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
805 if (pAnnotDic)
806 return pAnnotDic->KeyExist("AS");
807 return FALSE;
808}
809
810CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
811 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
812 ASSERT(pEnv);
813 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
814
815 CPDFSDK_Annot* pSDKAnnot = NULL;
816
817 if (pAnnotHandler) {
818 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
819 }
820 if (!pSDKAnnot)
821 return NULL;
822
823 m_fxAnnotArray.Add(pSDKAnnot);
824
825 if (pAnnotHandler) {
826 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
827 }
828
829 return pSDKAnnot;
830}
831
832CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
833 if (!pPDFAnnot)
834 return NULL;
835
836 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
837 if (pSDKAnnot)
838 return pSDKAnnot;
839
840 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
841 ASSERT(pEnv);
842 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
843
844 pSDKAnnot = NULL;
845
846 if (pAnnotHandler) {
847 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
848 }
849 if (!pSDKAnnot)
850 return NULL;
851
852 m_fxAnnotArray.Add(pSDKAnnot);
853
854 return pSDKAnnot;
855}
856
857CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
858 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
859}
860
861CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
862 CPDF_Dictionary* pDict) {
863 return NULL;
864}
865
866FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
867 if (!pAnnot)
868 return FALSE;
869 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
870 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
871 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
872 return FALSE;
873
874 int index = m_fxAnnotArray.Find(pAnnot);
875 m_fxAnnotArray.RemoveAt(index);
876 if (m_CaptureWidget == pAnnot)
877 m_CaptureWidget = NULL;
878
879 return TRUE;
880}
881
882CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
883 if (m_page) {
884 return m_page->GetDocument()->GetPDFDoc();
885 }
886 return NULL;
887}
888
889CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
890 if (m_page) {
891 return m_page->GetPDFPage();
892 }
893
894 return NULL;
895}
896
897int CPDFSDK_PageView::CountAnnots() {
898 return m_fxAnnotArray.GetSize();
899}
900
901CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(int nIndex) {
902 int nCount = m_fxAnnotArray.GetSize();
903 if (nIndex < 0 || nIndex >= nCount) {
904 return NULL;
905 }
906
907 return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
908}
909
910CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
911 int nCount = m_fxAnnotArray.GetSize();
912 for (int i = 0; i < nCount; i++) {
913 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
914 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
915 return pAnnot;
916 }
917 return NULL;
918}
919CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
920 if (hWidget == NULL)
921 return NULL;
922 int annotCount = m_fxAnnotArray.GetSize();
923
924 for (int i = 0; i < annotCount; i++) {
925 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
926 if (pAnnot->GetXFAWidget() == hWidget)
927 return pAnnot;
928 }
929 return NULL;
930}
931
932FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
933 FX_UINT nFlag) {
934 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
935 ASSERT(pEnv);
936 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
937 if (!pFXAnnot) {
938 KillFocusAnnot(nFlag);
939 } else {
940 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
941 ASSERT(pAnnotHandlerMgr);
942
943 FX_BOOL bRet =
944 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
945 if (bRet) {
946 SetFocusAnnot(pFXAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700947 }
948 return bRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700949 }
950 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700951}
952
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
954 FX_UINT nFlag) {
955 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
956 ASSERT(pEnv);
957 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
958 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700959
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
961
962 if (pFXAnnot == NULL)
963 return FALSE;
964
965 FX_BOOL bRet =
966 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
967 if (bRet) {
968 SetFocusAnnot(pFXAnnot);
969 }
970 return TRUE;
971}
972
973FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
974 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
975 ASSERT(pEnv);
976 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
977 ASSERT(pAnnotHandlerMgr);
978
979 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
980
981 if (pFXAnnot == NULL)
982 return FALSE;
983
984 FX_BOOL bRet =
985 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
986 if (bRet) {
987 SetFocusAnnot(pFXAnnot);
988 }
989 return TRUE;
990}
991
992FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
993 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
994 ASSERT(pEnv);
995 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
996 ASSERT(pAnnotHandlerMgr);
997 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
998 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
999 FX_BOOL bRet = FALSE;
1000 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
1001 // Last focus Annot gets a chance to handle the event.
1002 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
1003 }
1004 if (pFXAnnot && !bRet) {
1005 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
1006 return bRet;
1007 }
1008 return bRet;
1009}
1010
1011FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
1012 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1013 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1014 ASSERT(pAnnotHandlerMgr);
1015 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
1016 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
1017 m_bExitWidget = TRUE;
1018 m_bEnterWidget = FALSE;
1019 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1020 }
1021 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
1022 m_bOnWidget = TRUE;
1023 if (!m_bEnterWidget) {
1024 m_bEnterWidget = TRUE;
1025 m_bExitWidget = FALSE;
1026 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
1027 }
1028 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
1029 return TRUE;
1030 }
1031 if (m_bOnWidget) {
1032 m_bOnWidget = FALSE;
1033 m_bExitWidget = TRUE;
1034 m_bEnterWidget = FALSE;
1035 if (m_CaptureWidget) {
1036 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1037 m_CaptureWidget = NULL;
1038 }
1039 }
1040 return FALSE;
1041}
1042
1043FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1044 double deltaY,
1045 const CPDF_Point& point,
1046 int nFlag) {
1047 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001048 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1049 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1050 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1052 (int)deltaY, point);
1053 }
1054 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001055}
1056
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1058 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001059 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001060 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001061 ASSERT(pAnnotHandlerMgr);
1062 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1063 }
Bo Xufdc00a72014-10-28 23:03:33 -07001064
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001065 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001066}
1067
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1069 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001070 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1072 ASSERT(pAnnotHandlerMgr);
1073 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1074 }
1075 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001076}
1077
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001080}
1081
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082void CPDFSDK_PageView::LoadFXAnnots() {
1083 ASSERT(m_page != NULL);
1084
1085 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1086 ASSERT(pEnv != NULL);
1087
1088 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1089 ASSERT(pAnnotHandlerMgr != NULL);
1090
1091 SetLock(TRUE);
1092 m_page->AddRef();
1093 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1094 IXFA_PageView* pageView = NULL;
1095 pageView = m_page->GetXFAPageView();
1096 ASSERT(pageView != NULL);
1097
1098 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1099 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1100 XFA_WIDGETFILTER_Viewable |
1101 XFA_WIDGETFILTER_AllType);
1102 if (!pWidgetHander) {
1103 m_page->Release();
1104 SetLock(FALSE);
1105 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001106 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107
1108 IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext();
1109 while (pXFAAnnot) {
1110 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
1111 if (!pAnnot) {
1112 pXFAAnnot = pWidgetHander->MoveToNext();
1113 continue;
1114 }
1115 m_fxAnnotArray.Add(pAnnot);
1116
1117 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1118
1119 pXFAAnnot = pWidgetHander->MoveToNext();
1120 }
1121
1122 pWidgetHander->Release();
1123 } else {
1124 CPDF_Page* pPage = m_page->GetPDFPage();
1125 ASSERT(pPage != NULL);
1126
1127 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1128 // Disable the default AP construction.
1129 CPDF_InterForm::EnableUpdateAP(FALSE);
1130 m_pAnnotList = new CPDF_AnnotList(pPage);
1131 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1132
1133 int nCount = m_pAnnotList->Count();
1134 for (int i = 0; i < nCount; i++) {
1135 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1136 CPDF_Document* pDoc = GetPDFDocument();
1137
1138 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1139
1140 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1141 if (!pAnnot)
1142 continue;
1143 m_fxAnnotArray.Add(pAnnot);
1144
1145 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1146 }
1147 }
1148 m_page->Release();
1149 SetLock(FALSE);
1150}
1151
1152void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1153 for (int i = 0; i < rects.GetSize(); i++) {
1154 CPDF_Rect rc = rects.GetAt(i);
1155 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1156 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1157 }
1158}
1159
1160void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1161 CPDF_Rect rcWindow = pAnnot->GetRect();
1162 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1163 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1164 rcWindow.bottom);
1165}
1166
1167int CPDFSDK_PageView::GetPageIndex() {
1168 if (m_page) {
1169 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1170 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1171 if (pDoc && pDic) {
1172 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1173 }
1174 }
1175 return -1;
1176}
1177
1178FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) {
1179 if (p == NULL)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001180 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001181 int iCount = m_pAnnotList->Count();
1182 for (int i = 0; i < iCount; i++) {
1183 if (m_pAnnotList->GetAt(i) == p)
1184 return TRUE;
1185 }
1186 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001187}
1188
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001189CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1190 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1191 if (!pFocusAnnot)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001192 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193
1194 for (int i = 0; i < m_fxAnnotArray.GetSize(); i++) {
1195 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1196 if (pAnnot == pFocusAnnot)
1197 return pAnnot;
1198 }
1199 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001200}