blob: 63ed642367f9062a4012880c872d424d8fb40b8b [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"
Tom Sepezdfbf8e72015-10-14 14:17:26 -07008#include "../../third_party/base/nonstd_unique_ptr.h"
Tom Sepez37458412015-10-06 11:33:46 -07009#include "../include/formfiller/FFL_FormFiller.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070010#include "../include/fsdk_define.h"
11#include "../include/fsdk_mgr.h"
12#include "../include/javascript/IJavaScript.h"
13
Tom Sepez37458412015-10-06 11:33:46 -070014#include "../include/fpdfxfa/fpdfxfa_app.h"
Bo Xufdc00a72014-10-28 23:03:33 -070015#include "../include/fpdfxfa/fpdfxfa_doc.h"
16#include "../include/fpdfxfa/fpdfxfa_page.h"
17#include "../include/fpdfxfa/fpdfxfa_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
19#if _FX_OS_ == _FX_ANDROID_
20#include "time.h"
21#else
22#include <ctime>
23#endif
24
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025class CFX_SystemHandler : public IFX_SystemHandler {
26 public:
27 CFX_SystemHandler(CPDFDoc_Environment* pEnv) : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070028 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070031 // IFX_SystemHandler
32 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
33 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
34 FX_BOOL IsSelectionImplemented() override;
35 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
36 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 return FALSE;
38 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070039 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
40 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
41 void SetCursor(int32_t nCursorType) override;
42 FX_HMENU CreatePopupMenu() override { return NULL; }
43 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
44 int32_t nIDNewItem,
45 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 return FALSE;
47 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070048 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
49 int32_t nIDItem,
50 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 return FALSE;
52 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070053 int32_t TrackPopupMenu(FX_HMENU hMenu,
54 int32_t x,
55 int32_t y,
56 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 return -1;
58 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070059 void DestroyMenu(FX_HMENU hMenu) override {}
60 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
61 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
62 CFX_ByteString sFontFaceName) override;
63 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
64 CFX_ByteString sFontFaceName,
65 uint8_t nCharset) override;
66 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
67 void KillTimer(int32_t nID) override;
68 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
70 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070071 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
73 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070074 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 return m_pEnv->FFI_IsALTKeyDown(nFlag);
76 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070077 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
79 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070080 FX_SYSTEMTIME GetLocalTime() override;
81 int32_t GetCharSet() override { return m_nCharSet; }
82 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083
84 private:
85 CPDFDoc_Environment* m_pEnv;
86 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087};
88
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
90 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091}
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
94 // g_pFormFillApp->FFI_Invalidate();
95 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
96 CPDFXFA_Page* pPage = NULL;
97 CPDFSDK_PageView* pPageView = NULL;
98 pPageView = pSDKAnnot->GetPageView();
99 pPage = pSDKAnnot->GetPDFXFAPage();
100 if (!pPage || !pPageView)
101 return;
102 CPDF_Matrix page2device;
103 pPageView->GetCurrentMatrix(page2device);
104 CPDF_Matrix device2page;
105 device2page.SetReverse(page2device);
106 FX_FLOAT left, top, right, bottom;
107 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
108 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
109 bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 CPDF_Rect rcPDF(left, bottom, right, top);
111 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
114 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
117 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
118 if (pFFL) {
119 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
120 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
121 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
122 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
Tom Sepez468e5892015-10-13 15:49:36 -0700123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
125 ASSERT(pAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700126 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 ASSERT(pPage);
128 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
129 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130}
131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132FX_BOOL CFX_SystemHandler::IsSelectionImplemented() {
133 if (m_pEnv) {
134 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
135 if (pInfo && pInfo->FFI_OutputSelectedRect)
136 return TRUE;
137 }
138 return FALSE;
139}
140
141CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) {
142 return "";
143}
144
145FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(
146 int32_t nCharset,
147 CFX_ByteString sFontFaceName) {
148 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
149 if (pFontMgr) {
150 CFX_FontMapper* pFontMapper = pFontMgr->m_pBuiltinMapper;
151 if (pFontMapper) {
152 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
153 if (nSize == 0) {
154 pFontMapper->LoadInstalledFonts();
155 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
156 }
157
158 for (int i = 0; i < nSize; i++) {
159 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
160 return TRUE;
161 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700162 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 }
164
165 return FALSE;
166}
167
168static int CharSet2CP(int charset) {
169 if (charset == 128)
170 return 932;
171 if (charset == 134)
172 return 936;
173 if (charset == 129)
174 return 949;
175 if (charset == 136)
176 return 950;
177 return 0;
178}
179CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
180 CPDF_Document* pDoc,
181 CFX_ByteString sFontFaceName,
182 uint8_t nCharset) {
183 if (pDoc) {
184 CFX_Font* pFXFont = new CFX_Font();
185 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
186 FALSE);
187 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
188 delete pFXFont;
189 return pFont;
190 }
191
192 return NULL;
193}
194
195int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
196 TimerCallback lpTimerFunc) {
197 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
198}
199void CFX_SystemHandler::KillTimer(int32_t nID) {
200 m_pEnv->FFI_KillTimer(nID);
201}
202
203FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
204 return m_pEnv->FFI_GetLocalTime();
205}
206
207CPDFDoc_Environment::CPDFDoc_Environment(CPDFXFA_Document* pDoc,
208 FPDF_FORMFILLINFO* pFFinfo)
209 : m_pAnnotHandlerMgr(NULL),
210 m_pActionHandler(NULL),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 m_pInfo(pFFinfo),
212 m_pSDKDoc(NULL),
213 m_pXFADoc(pDoc),
214 m_pIFormFiller(NULL) {
215 m_pSysHandler = new CFX_SystemHandler(this);
216}
217
218CPDFDoc_Environment::~CPDFDoc_Environment() {
219 delete m_pIFormFiller;
220 m_pIFormFiller = NULL;
221
222 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700223 if (pProvider->m_pEnvList.GetSize() == 0)
224 pProvider->SetJavaScriptInitialized(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225
226 delete m_pSysHandler;
227 m_pSysHandler = NULL;
228
229 delete m_pAnnotHandlerMgr;
230 m_pAnnotHandlerMgr = NULL;
231
232 delete m_pActionHandler;
233 m_pActionHandler = NULL;
234}
235
236int 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 && m_pInfo->m_pJsPlatform->app_alert) {
241 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
242 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
243 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
244 FPDF_WIDESTRING pTitle =
245 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
246 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
247 pTitle, Type, Icon);
248 bsMsg.ReleaseBuffer();
249 bsTitle.ReleaseBuffer();
250 return ret;
251 }
252 return -1;
253}
254
255int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
256 const FX_WCHAR* Title,
257 const FX_WCHAR* Default,
258 const FX_WCHAR* cLabel,
259 FPDF_BOOL bPassword,
260 void* response,
261 int length) {
262 if (m_pInfo && m_pInfo->m_pJsPlatform &&
263 m_pInfo->m_pJsPlatform->app_response) {
264 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
265 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
266 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
267 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
268 FPDF_WIDESTRING pQuestion =
269 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
270 FPDF_WIDESTRING pTitle =
271 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
272 FPDF_WIDESTRING pDefault =
273 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
274 FPDF_WIDESTRING pLabel =
275 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
276 int ret = m_pInfo->m_pJsPlatform->app_response(
277 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
278 response, length);
279 bsQuestion.ReleaseBuffer();
280 bsTitle.ReleaseBuffer();
281 bsDefault.ReleaseBuffer();
282 bsLabel.ReleaseBuffer();
283 return ret;
284 }
285 return -1;
286}
287
288CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
289 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
290 !m_pInfo->m_pJsPlatform->Field_browse) {
291 return L"";
292 }
293
294 const int nRequiredLen =
295 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
296 if (nRequiredLen <= 0)
297 return L"";
298
299 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
300 memset(pBuff.get(), 0, nRequiredLen);
301 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
302 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
303 if (nActualLen <= 0 || nActualLen > nRequiredLen)
304 return L"";
305
306 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
307 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
308 return wsRet;
309}
310
311CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
312 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
313 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
314 return L"";
315 }
316
317 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
318 m_pInfo->m_pJsPlatform, nullptr, 0);
319 if (nRequiredLen <= 0)
320 return L"";
321
322 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
323 memset(pBuff.get(), 0, nRequiredLen);
324 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
325 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
326 if (nActualLen <= 0 || nActualLen > nRequiredLen)
327 return L"";
328
329 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
330 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
331 return wsRet;
332}
333
334void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
335 int length,
336 const FX_WCHAR* URL) {
337 if (m_pInfo && m_pInfo->m_pJsPlatform &&
338 m_pInfo->m_pJsPlatform->Doc_submitForm) {
339 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
340 FPDF_WIDESTRING pDestination =
341 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
342 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
343 length, pDestination);
344 bsDestination.ReleaseBuffer();
345 }
346}
347
348void CPDFDoc_Environment::JS_docmailForm(void* mailData,
349 int length,
350 FPDF_BOOL bUI,
351 const FX_WCHAR* To,
352 const FX_WCHAR* Subject,
353 const FX_WCHAR* CC,
354 const FX_WCHAR* BCC,
355 const FX_WCHAR* Msg) {
356 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
357 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
358 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
359 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
360 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
361 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
362 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
363 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
364 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
365 FPDF_WIDESTRING pSubject =
366 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
367 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
368 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
369 bUI, pTo, pSubject, pCC, pBcc, pMsg);
370 bsTo.ReleaseBuffer();
371 bsCC.ReleaseBuffer();
372 bsBcc.ReleaseBuffer();
373 bsSubject.ReleaseBuffer();
374 bsMsg.ReleaseBuffer();
375 }
376}
377
Tom Sepezba038bc2015-10-08 12:03:00 -0700378IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700380 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 if (!m_pJSRuntime)
Tom Sepezba038bc2015-10-08 12:03:00 -0700382 m_pJSRuntime.reset(IJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700383 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700384}
385
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
387 if (!m_pAnnotHandlerMgr)
388 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
389 return m_pAnnotHandlerMgr;
Bo Xufdc00a72014-10-28 23:03:33 -0700390}
391
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
393 if (!m_pActionHandler)
Tom Sepez3b5f1242015-09-01 14:06:55 -0700394 m_pActionHandler = new CPDFSDK_ActionHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 return m_pActionHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700396}
397
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
399 if (!m_pIFormFiller)
400 m_pIFormFiller = new CFFL_IFormFiller(this);
401 return m_pIFormFiller;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700402}
403
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
405 CPDFDoc_Environment* pEnv)
406 : m_pDoc(pDoc),
407 m_pInterForm(nullptr),
408 m_pFocusAnnot(nullptr),
409 m_pEnv(pEnv),
410 m_pOccontent(nullptr),
411 m_bChangeMask(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700412
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413CPDFSDK_Document::~CPDFSDK_Document() {
414 for (auto& it : m_pageMap)
415 delete it.second;
416 m_pageMap.clear();
417
418 delete m_pInterForm;
419 m_pInterForm = nullptr;
420
421 delete m_pOccontent;
422 m_pOccontent = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700423}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700424
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
426 FX_BOOL ReNew) {
427 auto it = m_pageMap.find(pPDFXFAPage);
428 if (it != m_pageMap.end())
429 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700430
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 if (!ReNew)
432 return nullptr;
433
434 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
435 m_pageMap[pPDFXFAPage] = pPageView;
436 // Delay to load all the annotations, to avoid endless loop.
437 pPageView->LoadFXAnnots();
438 return pPageView;
439}
440
441CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
442 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
443 return pPage ? GetPageView(pPage, TRUE) : nullptr;
444}
445
446CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
447 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
448 if (!pTempPage)
449 return nullptr;
450
451 auto it = m_pageMap.find(pTempPage);
452 return it->second;
453}
454
455void CPDFSDK_Document::ProcJavascriptFun() {
456 CPDFXFA_Document* pPDFDoc = GetDocument();
457 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
458 int iCount = docJS.CountJSActions();
459 if (iCount < 1)
460 return;
461 for (int i = 0; i < iCount; i++) {
462 CFX_ByteString csJSName;
463 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
464 if (m_pEnv->GetActionHander())
465 m_pEnv->GetActionHander()->DoAction_JavaScript(
466 jsAction, CFX_WideString::FromLocal(csJSName), this);
467 }
468}
469
470FX_BOOL CPDFSDK_Document::ProcOpenAction() {
471 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700472 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473
474 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
475 if (!pRoot)
476 return FALSE;
477
478 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
479 if (!pOpenAction)
480 pOpenAction = pRoot->GetArray("OpenAction");
481
482 if (!pOpenAction)
483 return FALSE;
484
485 if (pOpenAction->GetType() == PDFOBJ_ARRAY)
486 return TRUE;
487
Dan Sinclairf1251c12015-10-20 16:24:45 -0400488 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489 CPDF_Action action(pDict);
490 if (m_pEnv->GetActionHander())
491 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
492 return TRUE;
493 }
494 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700495}
496
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
498 if (!m_pOccontent)
499 m_pOccontent = new CPDF_OCContext(m_pDoc->GetPDFDoc());
500 return m_pOccontent;
501}
Bo Xufdc00a72014-10-28 23:03:33 -0700502
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700503void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
504 auto it = m_pageMap.find(pPDFXFAPage);
505 if (it == m_pageMap.end())
506 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700507
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 CPDFSDK_PageView* pPageView = it->second;
509 if (pPageView->IsLocked())
510 return;
511
512 delete pPageView;
513 m_pageMap.erase(it);
514}
515
516CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
517 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
518 if (!pTempPage)
519 return NULL;
520 return pTempPage;
521}
522
523CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
524 if (!m_pInterForm)
525 m_pInterForm = new CPDFSDK_InterForm(this);
526 return m_pInterForm;
527}
528
529void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
530 CPDFSDK_Annot* pAnnot) {
531 for (const auto& it : m_pageMap) {
532 CPDFSDK_PageView* pPageView = it.second;
533 if (pPageView != pSender) {
534 pPageView->UpdateView(pAnnot);
535 }
536 }
537}
538
539CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
540 return m_pFocusAnnot;
541}
542
543FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
544 if (m_pFocusAnnot == pAnnot)
545 return TRUE;
546
547 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
548
549 if (m_pFocusAnnot) {
550 if (!KillFocusAnnot(nFlag))
551 return FALSE;
552 }
553 CPDFSDK_PageView* pPageView = NULL;
554 if (pAnnot)
555 pPageView = pAnnot->GetPageView();
556 if (pAnnot && pPageView->IsValid()) {
557 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
558
559 if (pAnnotHandler && !m_pFocusAnnot) {
560 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700561 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700562
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700563 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
564 return FALSE;
565 if (!m_pFocusAnnot) {
566 m_pFocusAnnot = pAnnot;
567 return TRUE;
568 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700569 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700570 }
571 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700572}
573
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700574FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
575 if (m_pFocusAnnot) {
576 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
577 if (pAnnotHandler) {
578 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
579 m_pFocusAnnot = NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700580
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581 if (!pAnnotHandler->Annot_OnChangeFocus(NULL, pFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700582 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700583
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
585 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
586 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
587 int nFieldType = pWidget->GetFieldType();
588 if (FIELDTYPE_TEXTFIELD == nFieldType ||
589 FIELDTYPE_COMBOBOX == nFieldType)
590 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
591 }
592
593 if (!m_pFocusAnnot)
594 return TRUE;
595 } else {
596 m_pFocusAnnot = pFocusAnnot;
597 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700598 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 }
600 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700601}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700602
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
604 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
605 return FALSE;
606 }
607
608 CPDFXFA_Page* pTempPage = NULL;
609 for (int i = nCount - 1; i >= 0; i--) {
610 pTempPage = GetPage(nStart + i);
611 if (pTempPage != NULL) {
612 ReMovePageView(pTempPage);
613 }
614 }
615 return TRUE;
616}
617
618void CPDFSDK_Document::OnCloseDocument() {
619 KillFocusAnnot();
620}
621
622FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
623 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
624 return dwPermissions & nFlag;
625}
626
Tom Sepezba038bc2015-10-08 12:03:00 -0700627IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700628 ASSERT(m_pEnv != NULL);
629 return m_pEnv->GetJSRuntime();
630}
631
632CFX_WideString CPDFSDK_Document::GetPath() {
633 ASSERT(m_pEnv != NULL);
634 return m_pEnv->JS_docGetFilePath();
635}
636
637CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
638 CPDFXFA_Page* page)
639 : m_page(page), m_pSDKDoc(pSDKDoc) {
640 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
641 if (pInterForm) {
642 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
643 if (page->GetPDFPage())
644 pPDFInterForm->FixPageFields(page->GetPDFPage());
645 }
646 m_fxAnnotArray.RemoveAll();
647
648 m_bEnterWidget = FALSE;
649 m_bExitWidget = FALSE;
650 m_bOnWidget = FALSE;
651 m_CaptureWidget = NULL;
652 m_bValid = FALSE;
653 m_bLocked = FALSE;
654 m_pAnnotList = NULL;
655}
656
657CPDFSDK_PageView::~CPDFSDK_PageView() {
Tom Sepez396e8722015-09-09 10:16:08 -0700658 // if there is a focused annot on the page, we should kill the focus first.
659 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
660 for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
661 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
662 if (pAnnot == focusedAnnot) {
663 KillFocusAnnot();
664 break;
665 }
666 }
667 }
668
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700670 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
671 ASSERT(pAnnotHandlerMgr);
672 for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700673 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700674 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
675 }
676 m_fxAnnotArray.RemoveAll();
677
678 delete m_pAnnotList;
679 m_pAnnotList = NULL;
680}
681
682void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
683 CPDF_Matrix* pUser2Device,
684 CPDF_RenderOptions* pOptions,
685 FX_RECT* pClip) {
686 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
688 CPDFXFA_Page* pPage = GetPDFXFAPage();
689
690 if (pPage == NULL)
691 return;
692
Tom Sepeza8a39e22015-10-12 15:47:07 -0700693#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700694 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
695 CFX_Graphics gs;
696 gs.Create(pDevice);
697 if (pClip) {
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);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700704 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700705 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
706 if (!pRenderContext)
707 return;
708 CXFA_RenderOptions renderOptions;
709 renderOptions.m_bHighlight = TRUE;
710 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
711 renderOptions);
712 pRenderContext->DoRender();
713 pRenderContext->StopRender();
714 pRenderContext->Release();
715 return;
716 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700717#endif // PDF_ENABLE_XFA
718
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719 // for pdf/static xfa.
720 CPDFSDK_AnnotIterator annotIterator(this, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721 int index = -1;
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700722 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next(index)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725 }
726}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700727
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700728CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
729 FX_FLOAT pageY) {
730 int nCount = m_pAnnotList->Count();
731 for (int i = 0; i < nCount; i++) {
732 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
733 CFX_FloatRect annotRect;
734 pAnnot->GetRect(annotRect);
735 if (annotRect.Contains(pageX, pageY))
736 return pAnnot;
737 }
738 return NULL;
739}
740
741CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
742 FX_FLOAT pageY) {
743 int nCount = m_pAnnotList->Count();
744 for (int i = 0; i < nCount; i++) {
745 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
746 if (pAnnot->GetSubType() == "Widget") {
747 CFX_FloatRect annotRect;
748 pAnnot->GetRect(annotRect);
749 if (annotRect.Contains(pageX, pageY))
750 return pAnnot;
751 }
752 }
753 return NULL;
754}
755
756CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
757 FX_FLOAT pageY) {
758 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
759 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
760 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
761 CPDFSDK_Annot* pSDKAnnot = NULL;
762 int index = -1;
763 pSDKAnnot = annotIterator.Next(index);
764 while (pSDKAnnot) {
765 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
766 if (rc.Contains(pageX, pageY))
767 return pSDKAnnot;
768 pSDKAnnot = annotIterator.Next(index);
769 }
770
771 return NULL;
772}
773
774CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
775 FX_FLOAT pageY) {
776 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
777 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
778 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700779 int index = -1;
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700780 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next(index)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700781 if (pSDKAnnot->GetType() == "Widget" ||
782 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
783 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
784 CPDF_Point point(pageX, pageY);
785 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
786 return pSDKAnnot;
787 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700788 }
789
790 return NULL;
791}
792
793FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
794 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
795 if (pAnnotDic)
796 return pAnnotDic->KeyExist("AS");
797 return FALSE;
798}
799
800CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
801 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
802 ASSERT(pEnv);
803 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
804
805 CPDFSDK_Annot* pSDKAnnot = NULL;
806
807 if (pAnnotHandler) {
808 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
809 }
810 if (!pSDKAnnot)
811 return NULL;
812
813 m_fxAnnotArray.Add(pSDKAnnot);
814
815 if (pAnnotHandler) {
816 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
817 }
818
819 return pSDKAnnot;
820}
821
822CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
823 if (!pPDFAnnot)
824 return NULL;
825
826 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
827 if (pSDKAnnot)
828 return pSDKAnnot;
829
830 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
831 ASSERT(pEnv);
832 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
833
834 pSDKAnnot = NULL;
835
836 if (pAnnotHandler) {
837 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
838 }
839 if (!pSDKAnnot)
840 return NULL;
841
842 m_fxAnnotArray.Add(pSDKAnnot);
843
844 return pSDKAnnot;
845}
846
847CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
848 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
849}
850
851CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
852 CPDF_Dictionary* pDict) {
853 return NULL;
854}
855
856FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
857 if (!pAnnot)
858 return FALSE;
859 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
860 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
861 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
862 return FALSE;
863
864 int index = m_fxAnnotArray.Find(pAnnot);
865 m_fxAnnotArray.RemoveAt(index);
866 if (m_CaptureWidget == pAnnot)
867 m_CaptureWidget = NULL;
868
869 return TRUE;
870}
871
872CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
873 if (m_page) {
874 return m_page->GetDocument()->GetPDFDoc();
875 }
876 return NULL;
877}
878
879CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
880 if (m_page) {
881 return m_page->GetPDFPage();
882 }
883
884 return NULL;
885}
886
887int CPDFSDK_PageView::CountAnnots() {
888 return m_fxAnnotArray.GetSize();
889}
890
891CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(int nIndex) {
892 int nCount = m_fxAnnotArray.GetSize();
893 if (nIndex < 0 || nIndex >= nCount) {
894 return NULL;
895 }
896
897 return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
898}
899
900CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
901 int nCount = m_fxAnnotArray.GetSize();
902 for (int i = 0; i < nCount; i++) {
903 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
904 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
905 return pAnnot;
906 }
907 return NULL;
908}
909CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
910 if (hWidget == NULL)
911 return NULL;
912 int annotCount = m_fxAnnotArray.GetSize();
913
914 for (int i = 0; i < annotCount; i++) {
915 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
916 if (pAnnot->GetXFAWidget() == hWidget)
917 return pAnnot;
918 }
919 return NULL;
920}
921
922FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
923 FX_UINT nFlag) {
924 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
925 ASSERT(pEnv);
926 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
927 if (!pFXAnnot) {
928 KillFocusAnnot(nFlag);
929 } else {
930 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
931 ASSERT(pAnnotHandlerMgr);
932
933 FX_BOOL bRet =
934 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
935 if (bRet) {
936 SetFocusAnnot(pFXAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700937 }
938 return bRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700939 }
940 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700941}
942
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700943FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
944 FX_UINT nFlag) {
945 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
946 ASSERT(pEnv);
947 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
948 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700949
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700950 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
951
952 if (pFXAnnot == NULL)
953 return FALSE;
954
955 FX_BOOL bRet =
956 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
957 if (bRet) {
958 SetFocusAnnot(pFXAnnot);
959 }
960 return TRUE;
961}
962
963FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
964 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
965 ASSERT(pEnv);
966 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
967 ASSERT(pAnnotHandlerMgr);
968
969 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
970
971 if (pFXAnnot == NULL)
972 return FALSE;
973
974 FX_BOOL bRet =
975 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
976 if (bRet) {
977 SetFocusAnnot(pFXAnnot);
978 }
979 return TRUE;
980}
981
982FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
983 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
984 ASSERT(pEnv);
985 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
986 ASSERT(pAnnotHandlerMgr);
987 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
988 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
989 FX_BOOL bRet = FALSE;
990 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
991 // Last focus Annot gets a chance to handle the event.
992 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
993 }
994 if (pFXAnnot && !bRet) {
995 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
996 return bRet;
997 }
998 return bRet;
999}
1000
1001FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
1002 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1003 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1004 ASSERT(pAnnotHandlerMgr);
1005 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
1006 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
1007 m_bExitWidget = TRUE;
1008 m_bEnterWidget = FALSE;
1009 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1010 }
1011 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
1012 m_bOnWidget = TRUE;
1013 if (!m_bEnterWidget) {
1014 m_bEnterWidget = TRUE;
1015 m_bExitWidget = FALSE;
1016 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
1017 }
1018 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
1019 return TRUE;
1020 }
1021 if (m_bOnWidget) {
1022 m_bOnWidget = FALSE;
1023 m_bExitWidget = TRUE;
1024 m_bEnterWidget = FALSE;
1025 if (m_CaptureWidget) {
1026 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1027 m_CaptureWidget = NULL;
1028 }
1029 }
1030 return FALSE;
1031}
1032
1033FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1034 double deltaY,
1035 const CPDF_Point& point,
1036 int nFlag) {
1037 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001038 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1039 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1040 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1042 (int)deltaY, point);
1043 }
1044 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001045}
1046
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1048 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001049 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001050 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 ASSERT(pAnnotHandlerMgr);
1052 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1053 }
Bo Xufdc00a72014-10-28 23:03:33 -07001054
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001056}
1057
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1059 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001060 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001061 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1062 ASSERT(pAnnotHandlerMgr);
1063 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1064 }
1065 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001066}
1067
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001070}
1071
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001073 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075
1076 SetLock(TRUE);
1077 m_page->AddRef();
1078 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1079 IXFA_PageView* pageView = NULL;
1080 pageView = m_page->GetXFAPageView();
1081 ASSERT(pageView != NULL);
1082
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;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001091 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092
1093 IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext();
1094 while (pXFAAnnot) {
1095 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
1096 if (!pAnnot) {
1097 pXFAAnnot = pWidgetHander->MoveToNext();
1098 continue;
1099 }
1100 m_fxAnnotArray.Add(pAnnot);
1101
1102 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1103
1104 pXFAAnnot = pWidgetHander->MoveToNext();
1105 }
1106
1107 pWidgetHander->Release();
1108 } else {
1109 CPDF_Page* pPage = m_page->GetPDFPage();
1110 ASSERT(pPage != NULL);
1111
1112 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1113 // Disable the default AP construction.
1114 CPDF_InterForm::EnableUpdateAP(FALSE);
1115 m_pAnnotList = new CPDF_AnnotList(pPage);
1116 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1117
1118 int nCount = m_pAnnotList->Count();
1119 for (int i = 0; i < nCount; i++) {
1120 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1121 CPDF_Document* pDoc = GetPDFDocument();
1122
1123 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1124
1125 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1126 if (!pAnnot)
1127 continue;
1128 m_fxAnnotArray.Add(pAnnot);
1129
1130 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1131 }
1132 }
1133 m_page->Release();
1134 SetLock(FALSE);
1135}
1136
1137void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1138 for (int i = 0; i < rects.GetSize(); i++) {
1139 CPDF_Rect rc = rects.GetAt(i);
1140 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1141 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1142 }
1143}
1144
1145void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1146 CPDF_Rect rcWindow = pAnnot->GetRect();
1147 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1148 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1149 rcWindow.bottom);
1150}
1151
1152int CPDFSDK_PageView::GetPageIndex() {
1153 if (m_page) {
1154 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1155 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1156 if (pDoc && pDic) {
1157 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1158 }
1159 }
1160 return -1;
1161}
1162
1163FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) {
1164 if (p == NULL)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001165 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001166 int iCount = m_pAnnotList->Count();
1167 for (int i = 0; i < iCount; i++) {
1168 if (m_pAnnotList->GetAt(i) == p)
1169 return TRUE;
1170 }
1171 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001172}
1173
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001174CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1175 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1176 if (!pFocusAnnot)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001177 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178
1179 for (int i = 0; i < m_fxAnnotArray.GetSize(); i++) {
1180 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1181 if (pAnnot == pFocusAnnot)
1182 return pAnnot;
1183 }
1184 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001185}