blob: 346dacce59c054e0ad26a1827754c72001e86bc3 [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();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700225 if (pProvider->m_pEnvList.GetSize() == 0)
226 pProvider->SetJavaScriptInitialized(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227
228 delete m_pSysHandler;
229 m_pSysHandler = NULL;
230
231 delete m_pAnnotHandlerMgr;
232 m_pAnnotHandlerMgr = NULL;
233
234 delete m_pActionHandler;
235 m_pActionHandler = NULL;
236}
237
238int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
239 const FX_WCHAR* Title,
240 FX_UINT Type,
241 FX_UINT Icon) {
242 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
243 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
244 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
245 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
246 FPDF_WIDESTRING pTitle =
247 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
248 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
249 pTitle, Type, Icon);
250 bsMsg.ReleaseBuffer();
251 bsTitle.ReleaseBuffer();
252 return ret;
253 }
254 return -1;
255}
256
257int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
258 const FX_WCHAR* Title,
259 const FX_WCHAR* Default,
260 const FX_WCHAR* cLabel,
261 FPDF_BOOL bPassword,
262 void* response,
263 int length) {
264 if (m_pInfo && m_pInfo->m_pJsPlatform &&
265 m_pInfo->m_pJsPlatform->app_response) {
266 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
267 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
268 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
269 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
270 FPDF_WIDESTRING pQuestion =
271 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
272 FPDF_WIDESTRING pTitle =
273 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
274 FPDF_WIDESTRING pDefault =
275 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
276 FPDF_WIDESTRING pLabel =
277 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
278 int ret = m_pInfo->m_pJsPlatform->app_response(
279 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
280 response, length);
281 bsQuestion.ReleaseBuffer();
282 bsTitle.ReleaseBuffer();
283 bsDefault.ReleaseBuffer();
284 bsLabel.ReleaseBuffer();
285 return ret;
286 }
287 return -1;
288}
289
290CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
291 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
292 !m_pInfo->m_pJsPlatform->Field_browse) {
293 return L"";
294 }
295
296 const int nRequiredLen =
297 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
298 if (nRequiredLen <= 0)
299 return L"";
300
301 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
302 memset(pBuff.get(), 0, nRequiredLen);
303 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
304 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
305 if (nActualLen <= 0 || nActualLen > nRequiredLen)
306 return L"";
307
308 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
309 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
310 return wsRet;
311}
312
313CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
314 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
315 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
316 return L"";
317 }
318
319 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
320 m_pInfo->m_pJsPlatform, nullptr, 0);
321 if (nRequiredLen <= 0)
322 return L"";
323
324 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
325 memset(pBuff.get(), 0, nRequiredLen);
326 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
327 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
328 if (nActualLen <= 0 || nActualLen > nRequiredLen)
329 return L"";
330
331 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
332 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
333 return wsRet;
334}
335
336void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
337 int length,
338 const FX_WCHAR* URL) {
339 if (m_pInfo && m_pInfo->m_pJsPlatform &&
340 m_pInfo->m_pJsPlatform->Doc_submitForm) {
341 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
342 FPDF_WIDESTRING pDestination =
343 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
344 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
345 length, pDestination);
346 bsDestination.ReleaseBuffer();
347 }
348}
349
350void CPDFDoc_Environment::JS_docmailForm(void* mailData,
351 int length,
352 FPDF_BOOL bUI,
353 const FX_WCHAR* To,
354 const FX_WCHAR* Subject,
355 const FX_WCHAR* CC,
356 const FX_WCHAR* BCC,
357 const FX_WCHAR* Msg) {
358 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
359 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
360 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
361 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
362 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
363 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
364 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
365 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
366 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
367 FPDF_WIDESTRING pSubject =
368 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
369 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
370 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
371 bUI, pTo, pSubject, pCC, pBcc, pMsg);
372 bsTo.ReleaseBuffer();
373 bsCC.ReleaseBuffer();
374 bsBcc.ReleaseBuffer();
375 bsSubject.ReleaseBuffer();
376 bsMsg.ReleaseBuffer();
377 }
378}
379
380IFXJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
381 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700382 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 if (!m_pJSRuntime)
Tom Sepez854a7f62015-09-21 17:07:49 -0700384 m_pJSRuntime.reset(new CJS_Runtime(this));
385 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700386}
387
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
389 if (!m_pAnnotHandlerMgr)
390 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
391 return m_pAnnotHandlerMgr;
Bo Xufdc00a72014-10-28 23:03:33 -0700392}
393
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
395 if (!m_pActionHandler)
Tom Sepez3b5f1242015-09-01 14:06:55 -0700396 m_pActionHandler = new CPDFSDK_ActionHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 return m_pActionHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700398}
399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
401 if (!m_pIFormFiller)
402 m_pIFormFiller = new CFFL_IFormFiller(this);
403 return m_pIFormFiller;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700404}
405
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
407 CPDFDoc_Environment* pEnv)
408 : m_pDoc(pDoc),
409 m_pInterForm(nullptr),
410 m_pFocusAnnot(nullptr),
411 m_pEnv(pEnv),
412 m_pOccontent(nullptr),
413 m_bChangeMask(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700414
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415CPDFSDK_Document::~CPDFSDK_Document() {
416 for (auto& it : m_pageMap)
417 delete it.second;
418 m_pageMap.clear();
419
420 delete m_pInterForm;
421 m_pInterForm = nullptr;
422
423 delete m_pOccontent;
424 m_pOccontent = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700425}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700426
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
428 FX_BOOL ReNew) {
429 auto it = m_pageMap.find(pPDFXFAPage);
430 if (it != m_pageMap.end())
431 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700432
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 if (!ReNew)
434 return nullptr;
435
436 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
437 m_pageMap[pPDFXFAPage] = pPageView;
438 // Delay to load all the annotations, to avoid endless loop.
439 pPageView->LoadFXAnnots();
440 return pPageView;
441}
442
443CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
444 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
445 return pPage ? GetPageView(pPage, TRUE) : nullptr;
446}
447
448CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
449 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
450 if (!pTempPage)
451 return nullptr;
452
453 auto it = m_pageMap.find(pTempPage);
454 return it->second;
455}
456
457void CPDFSDK_Document::ProcJavascriptFun() {
458 CPDFXFA_Document* pPDFDoc = GetDocument();
459 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
460 int iCount = docJS.CountJSActions();
461 if (iCount < 1)
462 return;
463 for (int i = 0; i < iCount; i++) {
464 CFX_ByteString csJSName;
465 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
466 if (m_pEnv->GetActionHander())
467 m_pEnv->GetActionHander()->DoAction_JavaScript(
468 jsAction, CFX_WideString::FromLocal(csJSName), this);
469 }
470}
471
472FX_BOOL CPDFSDK_Document::ProcOpenAction() {
473 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700474 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475
476 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
477 if (!pRoot)
478 return FALSE;
479
480 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
481 if (!pOpenAction)
482 pOpenAction = pRoot->GetArray("OpenAction");
483
484 if (!pOpenAction)
485 return FALSE;
486
487 if (pOpenAction->GetType() == PDFOBJ_ARRAY)
488 return TRUE;
489
490 if (pOpenAction->GetType() == PDFOBJ_DICTIONARY) {
491 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pOpenAction;
492 CPDF_Action action(pDict);
493 if (m_pEnv->GetActionHander())
494 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
495 return TRUE;
496 }
497 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700498}
499
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
501 if (!m_pOccontent)
502 m_pOccontent = new CPDF_OCContext(m_pDoc->GetPDFDoc());
503 return m_pOccontent;
504}
Bo Xufdc00a72014-10-28 23:03:33 -0700505
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
507 auto it = m_pageMap.find(pPDFXFAPage);
508 if (it == m_pageMap.end())
509 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700510
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700511 CPDFSDK_PageView* pPageView = it->second;
512 if (pPageView->IsLocked())
513 return;
514
515 delete pPageView;
516 m_pageMap.erase(it);
517}
518
519CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
520 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
521 if (!pTempPage)
522 return NULL;
523 return pTempPage;
524}
525
526CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
527 if (!m_pInterForm)
528 m_pInterForm = new CPDFSDK_InterForm(this);
529 return m_pInterForm;
530}
531
532void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
533 CPDFSDK_Annot* pAnnot) {
534 for (const auto& it : m_pageMap) {
535 CPDFSDK_PageView* pPageView = it.second;
536 if (pPageView != pSender) {
537 pPageView->UpdateView(pAnnot);
538 }
539 }
540}
541
542CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
543 return m_pFocusAnnot;
544}
545
546FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
547 if (m_pFocusAnnot == pAnnot)
548 return TRUE;
549
550 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
551
552 if (m_pFocusAnnot) {
553 if (!KillFocusAnnot(nFlag))
554 return FALSE;
555 }
556 CPDFSDK_PageView* pPageView = NULL;
557 if (pAnnot)
558 pPageView = pAnnot->GetPageView();
559 if (pAnnot && pPageView->IsValid()) {
560 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
561
562 if (pAnnotHandler && !m_pFocusAnnot) {
563 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700564 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700565
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
567 return FALSE;
568 if (!m_pFocusAnnot) {
569 m_pFocusAnnot = pAnnot;
570 return TRUE;
571 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700572 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573 }
574 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700575}
576
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700577FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
578 if (m_pFocusAnnot) {
579 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
580 if (pAnnotHandler) {
581 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
582 m_pFocusAnnot = NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700583
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 if (!pAnnotHandler->Annot_OnChangeFocus(NULL, pFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700585 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700586
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700587 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
588 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
589 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
590 int nFieldType = pWidget->GetFieldType();
591 if (FIELDTYPE_TEXTFIELD == nFieldType ||
592 FIELDTYPE_COMBOBOX == nFieldType)
593 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
594 }
595
596 if (!m_pFocusAnnot)
597 return TRUE;
598 } else {
599 m_pFocusAnnot = pFocusAnnot;
600 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700601 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602 }
603 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700604}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700605
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
607 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
608 return FALSE;
609 }
610
611 CPDFXFA_Page* pTempPage = NULL;
612 for (int i = nCount - 1; i >= 0; i--) {
613 pTempPage = GetPage(nStart + i);
614 if (pTempPage != NULL) {
615 ReMovePageView(pTempPage);
616 }
617 }
618 return TRUE;
619}
620
621void CPDFSDK_Document::OnCloseDocument() {
622 KillFocusAnnot();
623}
624
625FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
626 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
627 return dwPermissions & nFlag;
628}
629
630IFXJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
631 ASSERT(m_pEnv != NULL);
632 return m_pEnv->GetJSRuntime();
633}
634
635CFX_WideString CPDFSDK_Document::GetPath() {
636 ASSERT(m_pEnv != NULL);
637 return m_pEnv->JS_docGetFilePath();
638}
639
640CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
641 CPDFXFA_Page* page)
642 : m_page(page), m_pSDKDoc(pSDKDoc) {
643 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
644 if (pInterForm) {
645 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
646 if (page->GetPDFPage())
647 pPDFInterForm->FixPageFields(page->GetPDFPage());
648 }
649 m_fxAnnotArray.RemoveAll();
650
651 m_bEnterWidget = FALSE;
652 m_bExitWidget = FALSE;
653 m_bOnWidget = FALSE;
654 m_CaptureWidget = NULL;
655 m_bValid = FALSE;
656 m_bLocked = FALSE;
657 m_pAnnotList = NULL;
658}
659
660CPDFSDK_PageView::~CPDFSDK_PageView() {
Tom Sepez396e8722015-09-09 10:16:08 -0700661 // if there is a focused annot on the page, we should kill the focus first.
662 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
663 for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
664 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
665 if (pAnnot == focusedAnnot) {
666 KillFocusAnnot();
667 break;
668 }
669 }
670 }
671
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700673 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
674 ASSERT(pAnnotHandlerMgr);
675 for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700676 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
678 }
679 m_fxAnnotArray.RemoveAll();
680
681 delete m_pAnnotList;
682 m_pAnnotList = NULL;
683}
684
685void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
686 CPDF_Matrix* pUser2Device,
687 CPDF_RenderOptions* pOptions,
688 FX_RECT* pClip) {
689 m_curMatrix = *pUser2Device;
690 // m_pAnnotList->DisplayAnnots(m_page, pDevice, pUser2Device, FALSE,
691 // pOptions);
692 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
693 CPDFXFA_Page* pPage = GetPDFXFAPage();
694
695 if (pPage == NULL)
696 return;
697
698 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
699 CFX_Graphics gs;
700 gs.Create(pDevice);
701 if (pClip) {
702 CFX_RectF rectClip;
703 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
704 static_cast<FX_FLOAT>(pClip->top),
705 static_cast<FX_FLOAT>(pClip->Width()),
706 static_cast<FX_FLOAT>(pClip->Height()));
707 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700708 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700709 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
710 if (!pRenderContext)
711 return;
712 CXFA_RenderOptions renderOptions;
713 renderOptions.m_bHighlight = TRUE;
714 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
715 renderOptions);
716 pRenderContext->DoRender();
717 pRenderContext->StopRender();
718 pRenderContext->Release();
719 return;
720 }
721 // for pdf/static xfa.
722 CPDFSDK_AnnotIterator annotIterator(this, TRUE);
723 CPDFSDK_Annot* pSDKAnnot = NULL;
724 int index = -1;
725 pSDKAnnot = annotIterator.Next(index);
726 while (pSDKAnnot) {
727 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
728 ASSERT(pAnnotHandlerMgr);
729 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
730 pSDKAnnot = annotIterator.Next(index);
731 }
732}
733CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
734 FX_FLOAT pageY) {
735 int nCount = m_pAnnotList->Count();
736 for (int i = 0; i < nCount; i++) {
737 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
738 CFX_FloatRect annotRect;
739 pAnnot->GetRect(annotRect);
740 if (annotRect.Contains(pageX, pageY))
741 return pAnnot;
742 }
743 return NULL;
744}
745
746CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
747 FX_FLOAT pageY) {
748 int nCount = m_pAnnotList->Count();
749 for (int i = 0; i < nCount; i++) {
750 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
751 if (pAnnot->GetSubType() == "Widget") {
752 CFX_FloatRect annotRect;
753 pAnnot->GetRect(annotRect);
754 if (annotRect.Contains(pageX, pageY))
755 return pAnnot;
756 }
757 }
758 return NULL;
759}
760
761CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
762 FX_FLOAT pageY) {
763 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
764 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
765 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
766 CPDFSDK_Annot* pSDKAnnot = NULL;
767 int index = -1;
768 pSDKAnnot = annotIterator.Next(index);
769 while (pSDKAnnot) {
770 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
771 if (rc.Contains(pageX, pageY))
772 return pSDKAnnot;
773 pSDKAnnot = annotIterator.Next(index);
774 }
775
776 return NULL;
777}
778
779CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
780 FX_FLOAT pageY) {
781 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
782 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
783 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
784 CPDFSDK_Annot* pSDKAnnot = NULL;
785 int index = -1;
786 pSDKAnnot = annotIterator.Next(index);
787 while (pSDKAnnot) {
788 if (pSDKAnnot->GetType() == "Widget" ||
789 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
790 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
791 CPDF_Point point(pageX, pageY);
792 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
793 return pSDKAnnot;
794 }
795 pSDKAnnot = annotIterator.Next(index);
796 }
797
798 return NULL;
799}
800
801FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
802 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
803 if (pAnnotDic)
804 return pAnnotDic->KeyExist("AS");
805 return FALSE;
806}
807
808CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
809 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
810 ASSERT(pEnv);
811 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
812
813 CPDFSDK_Annot* pSDKAnnot = NULL;
814
815 if (pAnnotHandler) {
816 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
817 }
818 if (!pSDKAnnot)
819 return NULL;
820
821 m_fxAnnotArray.Add(pSDKAnnot);
822
823 if (pAnnotHandler) {
824 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
825 }
826
827 return pSDKAnnot;
828}
829
830CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
831 if (!pPDFAnnot)
832 return NULL;
833
834 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
835 if (pSDKAnnot)
836 return pSDKAnnot;
837
838 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
839 ASSERT(pEnv);
840 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
841
842 pSDKAnnot = NULL;
843
844 if (pAnnotHandler) {
845 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
846 }
847 if (!pSDKAnnot)
848 return NULL;
849
850 m_fxAnnotArray.Add(pSDKAnnot);
851
852 return pSDKAnnot;
853}
854
855CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
856 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
857}
858
859CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
860 CPDF_Dictionary* pDict) {
861 return NULL;
862}
863
864FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
865 if (!pAnnot)
866 return FALSE;
867 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
868 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
869 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
870 return FALSE;
871
872 int index = m_fxAnnotArray.Find(pAnnot);
873 m_fxAnnotArray.RemoveAt(index);
874 if (m_CaptureWidget == pAnnot)
875 m_CaptureWidget = NULL;
876
877 return TRUE;
878}
879
880CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
881 if (m_page) {
882 return m_page->GetDocument()->GetPDFDoc();
883 }
884 return NULL;
885}
886
887CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
888 if (m_page) {
889 return m_page->GetPDFPage();
890 }
891
892 return NULL;
893}
894
895int CPDFSDK_PageView::CountAnnots() {
896 return m_fxAnnotArray.GetSize();
897}
898
899CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(int nIndex) {
900 int nCount = m_fxAnnotArray.GetSize();
901 if (nIndex < 0 || nIndex >= nCount) {
902 return NULL;
903 }
904
905 return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
906}
907
908CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
909 int nCount = m_fxAnnotArray.GetSize();
910 for (int i = 0; i < nCount; i++) {
911 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
912 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
913 return pAnnot;
914 }
915 return NULL;
916}
917CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
918 if (hWidget == NULL)
919 return NULL;
920 int annotCount = m_fxAnnotArray.GetSize();
921
922 for (int i = 0; i < annotCount; i++) {
923 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
924 if (pAnnot->GetXFAWidget() == hWidget)
925 return pAnnot;
926 }
927 return NULL;
928}
929
930FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
931 FX_UINT nFlag) {
932 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
933 ASSERT(pEnv);
934 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
935 if (!pFXAnnot) {
936 KillFocusAnnot(nFlag);
937 } else {
938 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
939 ASSERT(pAnnotHandlerMgr);
940
941 FX_BOOL bRet =
942 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
943 if (bRet) {
944 SetFocusAnnot(pFXAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700945 }
946 return bRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700947 }
948 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700949}
950
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
952 FX_UINT nFlag) {
953 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
954 ASSERT(pEnv);
955 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
956 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700957
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
959
960 if (pFXAnnot == NULL)
961 return FALSE;
962
963 FX_BOOL bRet =
964 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
965 if (bRet) {
966 SetFocusAnnot(pFXAnnot);
967 }
968 return TRUE;
969}
970
971FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
972 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
973 ASSERT(pEnv);
974 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
975 ASSERT(pAnnotHandlerMgr);
976
977 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
978
979 if (pFXAnnot == NULL)
980 return FALSE;
981
982 FX_BOOL bRet =
983 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
984 if (bRet) {
985 SetFocusAnnot(pFXAnnot);
986 }
987 return TRUE;
988}
989
990FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
991 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
992 ASSERT(pEnv);
993 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
994 ASSERT(pAnnotHandlerMgr);
995 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
996 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
997 FX_BOOL bRet = FALSE;
998 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
999 // Last focus Annot gets a chance to handle the event.
1000 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
1001 }
1002 if (pFXAnnot && !bRet) {
1003 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
1004 return bRet;
1005 }
1006 return bRet;
1007}
1008
1009FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
1010 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1011 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1012 ASSERT(pAnnotHandlerMgr);
1013 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
1014 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
1015 m_bExitWidget = TRUE;
1016 m_bEnterWidget = FALSE;
1017 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1018 }
1019 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
1020 m_bOnWidget = TRUE;
1021 if (!m_bEnterWidget) {
1022 m_bEnterWidget = TRUE;
1023 m_bExitWidget = FALSE;
1024 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
1025 }
1026 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
1027 return TRUE;
1028 }
1029 if (m_bOnWidget) {
1030 m_bOnWidget = FALSE;
1031 m_bExitWidget = TRUE;
1032 m_bEnterWidget = FALSE;
1033 if (m_CaptureWidget) {
1034 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1035 m_CaptureWidget = NULL;
1036 }
1037 }
1038 return FALSE;
1039}
1040
1041FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1042 double deltaY,
1043 const CPDF_Point& point,
1044 int nFlag) {
1045 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001046 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1047 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1048 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1050 (int)deltaY, point);
1051 }
1052 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001053}
1054
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1056 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001057 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001058 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 ASSERT(pAnnotHandlerMgr);
1060 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1061 }
Bo Xufdc00a72014-10-28 23:03:33 -07001062
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001064}
1065
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1067 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001068 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1070 ASSERT(pAnnotHandlerMgr);
1071 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1072 }
1073 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001074}
1075
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001078}
1079
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080void CPDFSDK_PageView::LoadFXAnnots() {
1081 ASSERT(m_page != NULL);
1082
1083 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1084 ASSERT(pEnv != NULL);
1085
1086 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1087 ASSERT(pAnnotHandlerMgr != NULL);
1088
1089 SetLock(TRUE);
1090 m_page->AddRef();
1091 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1092 IXFA_PageView* pageView = NULL;
1093 pageView = m_page->GetXFAPageView();
1094 ASSERT(pageView != NULL);
1095
1096 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1097 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1098 XFA_WIDGETFILTER_Viewable |
1099 XFA_WIDGETFILTER_AllType);
1100 if (!pWidgetHander) {
1101 m_page->Release();
1102 SetLock(FALSE);
1103 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001104 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105
1106 IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext();
1107 while (pXFAAnnot) {
1108 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
1109 if (!pAnnot) {
1110 pXFAAnnot = pWidgetHander->MoveToNext();
1111 continue;
1112 }
1113 m_fxAnnotArray.Add(pAnnot);
1114
1115 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1116
1117 pXFAAnnot = pWidgetHander->MoveToNext();
1118 }
1119
1120 pWidgetHander->Release();
1121 } else {
1122 CPDF_Page* pPage = m_page->GetPDFPage();
1123 ASSERT(pPage != NULL);
1124
1125 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1126 // Disable the default AP construction.
1127 CPDF_InterForm::EnableUpdateAP(FALSE);
1128 m_pAnnotList = new CPDF_AnnotList(pPage);
1129 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1130
1131 int nCount = m_pAnnotList->Count();
1132 for (int i = 0; i < nCount; i++) {
1133 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1134 CPDF_Document* pDoc = GetPDFDocument();
1135
1136 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1137
1138 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1139 if (!pAnnot)
1140 continue;
1141 m_fxAnnotArray.Add(pAnnot);
1142
1143 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1144 }
1145 }
1146 m_page->Release();
1147 SetLock(FALSE);
1148}
1149
1150void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1151 for (int i = 0; i < rects.GetSize(); i++) {
1152 CPDF_Rect rc = rects.GetAt(i);
1153 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1154 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1155 }
1156}
1157
1158void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1159 CPDF_Rect rcWindow = pAnnot->GetRect();
1160 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1161 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1162 rcWindow.bottom);
1163}
1164
1165int CPDFSDK_PageView::GetPageIndex() {
1166 if (m_page) {
1167 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1168 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1169 if (pDoc && pDic) {
1170 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1171 }
1172 }
1173 return -1;
1174}
1175
1176FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) {
1177 if (p == NULL)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001178 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179 int iCount = m_pAnnotList->Count();
1180 for (int i = 0; i < iCount; i++) {
1181 if (m_pAnnotList->GetAt(i) == p)
1182 return TRUE;
1183 }
1184 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001185}
1186
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001187CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1188 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1189 if (!pFocusAnnot)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001190 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001191
1192 for (int i = 0; i < m_fxAnnotArray.GetSize(); i++) {
1193 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1194 if (pAnnot == pFocusAnnot)
1195 return pAnnot;
1196 }
1197 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001198}