blob: d471dc1e83a512945220e2080ab373ccea353637 [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"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
17#if _FX_OS_ == _FX_ANDROID_
18#include "time.h"
19#else
20#include <ctime>
21#endif
22
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023// extern CPDFDoc_Environment* g_pFormFillApp;
24class CFX_SystemHandler : public IFX_SystemHandler {
25 public:
26 CFX_SystemHandler(CPDFDoc_Environment* pEnv) : m_pEnv(pEnv), m_nCharSet(-1) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 public:
29 virtual void InvalidateRect(FX_HWND hWnd, FX_RECT rect);
30 virtual void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 virtual FX_BOOL IsSelectionImplemented();
Lei Zhang60f507b2015-06-13 00:41:00 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 virtual CFX_WideString GetClipboardText(FX_HWND hWnd) { return L""; }
35 virtual FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) {
36 return FALSE;
37 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 virtual void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) {}
40 virtual void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 /*cursor style
43 FXCT_ARROW
44 FXCT_NESW
45 FXCT_NWSE
46 FXCT_VBEAM
47 FXCT_HBEAM
48 FXCT_HAND
49 */
50 virtual void SetCursor(int32_t nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 virtual FX_HMENU CreatePopupMenu() { return NULL; }
53 virtual FX_BOOL AppendMenuItem(FX_HMENU hMenu,
54 int32_t nIDNewItem,
55 CFX_WideString string) {
56 return FALSE;
57 }
58 virtual FX_BOOL EnableMenuItem(FX_HMENU hMenu,
59 int32_t nIDItem,
60 FX_BOOL bEnabled) {
61 return FALSE;
62 }
63 virtual int32_t TrackPopupMenu(FX_HMENU hMenu,
64 int32_t x,
65 int32_t y,
66 FX_HWND hParent) {
67 return -1;
68 }
69 virtual void DestroyMenu(FX_HMENU hMenu) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 virtual CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset);
72 virtual FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
73 CFX_ByteString sFontFaceName);
74 virtual CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
75 CFX_ByteString sFontFaceName,
76 uint8_t nCharset);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 virtual int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc);
79 virtual void KillTimer(int32_t nID);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 virtual FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) {
82 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
83 }
84 virtual FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) {
85 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
86 }
87 virtual FX_BOOL IsALTKeyDown(FX_DWORD nFlag) {
88 return m_pEnv->FFI_IsALTKeyDown(nFlag);
89 }
90 virtual FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) {
91 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
92 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 virtual FX_SYSTEMTIME GetLocalTime();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 virtual int32_t GetCharSet() { return m_nCharSet; }
97 virtual void SetCharSet(int32_t nCharSet) { m_nCharSet = nCharSet; }
98
99 private:
100 CPDFDoc_Environment* m_pEnv;
101 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102};
103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
105 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
109 // g_pFormFillApp->FFI_Invalidate();
110 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
111 CPDFXFA_Page* pPage = NULL;
112 CPDFSDK_PageView* pPageView = NULL;
113 pPageView = pSDKAnnot->GetPageView();
114 pPage = pSDKAnnot->GetPDFXFAPage();
115 if (!pPage || !pPageView)
116 return;
117 CPDF_Matrix page2device;
118 pPageView->GetCurrentMatrix(page2device);
119 CPDF_Matrix device2page;
120 device2page.SetReverse(page2device);
121 FX_FLOAT left, top, right, bottom;
122 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
123 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
124 bottom);
125 // m_pEnv->FFI_DeviceToPage(pPage, rect.left, rect.top, (double*)&left,
126 // (double*)&top);
127 // m_pEnv->FFI_DeviceToPage(pPage, rect.right, rect.bottom, (double*)&right,
128 // (double*)&bottom);
129 CPDF_Rect rcPDF(left, bottom, right, top);
130 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
133 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
136 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
137 if (pFFL) {
138 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
139 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
140 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
141 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
142 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
143 ASSERT(pAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700144 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 ASSERT(pPage);
146 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
147 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700148}
149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150FX_BOOL CFX_SystemHandler::IsSelectionImplemented() {
151 if (m_pEnv) {
152 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
153 if (pInfo && pInfo->FFI_OutputSelectedRect)
154 return TRUE;
155 }
156 return FALSE;
157}
158
159CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) {
160 return "";
161}
162
163FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(
164 int32_t nCharset,
165 CFX_ByteString sFontFaceName) {
166 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
167 if (pFontMgr) {
168 CFX_FontMapper* pFontMapper = pFontMgr->m_pBuiltinMapper;
169 if (pFontMapper) {
170 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
171 if (nSize == 0) {
172 pFontMapper->LoadInstalledFonts();
173 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
174 }
175
176 for (int i = 0; i < nSize; i++) {
177 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
178 return TRUE;
179 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700180 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 }
182
183 return FALSE;
184}
185
186static int CharSet2CP(int charset) {
187 if (charset == 128)
188 return 932;
189 if (charset == 134)
190 return 936;
191 if (charset == 129)
192 return 949;
193 if (charset == 136)
194 return 950;
195 return 0;
196}
197CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
198 CPDF_Document* pDoc,
199 CFX_ByteString sFontFaceName,
200 uint8_t nCharset) {
201 if (pDoc) {
202 CFX_Font* pFXFont = new CFX_Font();
203 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
204 FALSE);
205 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
206 delete pFXFont;
207 return pFont;
208 }
209
210 return NULL;
211}
212
213int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
214 TimerCallback lpTimerFunc) {
215 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
216}
217void CFX_SystemHandler::KillTimer(int32_t nID) {
218 m_pEnv->FFI_KillTimer(nID);
219}
220
221FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
222 return m_pEnv->FFI_GetLocalTime();
223}
224
225CPDFDoc_Environment::CPDFDoc_Environment(CPDFXFA_Document* pDoc,
226 FPDF_FORMFILLINFO* pFFinfo)
227 : m_pAnnotHandlerMgr(NULL),
228 m_pActionHandler(NULL),
229 m_pJSRuntime(NULL),
230 m_pInfo(pFFinfo),
231 m_pSDKDoc(NULL),
232 m_pXFADoc(pDoc),
233 m_pIFormFiller(NULL) {
234 m_pSysHandler = new CFX_SystemHandler(this);
235}
236
237CPDFDoc_Environment::~CPDFDoc_Environment() {
238 delete m_pIFormFiller;
239 m_pIFormFiller = NULL;
240
241 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
242 if (m_pJSRuntime && pProvider->GetRuntimeFactory())
243 pProvider->GetRuntimeFactory()->DeleteJSRuntime(m_pJSRuntime);
244
245 if (pProvider->m_pEnvList.GetSize() == 0) {
246 pProvider->ReleaseRuntime();
247 pProvider->InitRuntime(TRUE);
248 }
249
250 delete m_pSysHandler;
251 m_pSysHandler = NULL;
252
253 delete m_pAnnotHandlerMgr;
254 m_pAnnotHandlerMgr = NULL;
255
256 delete m_pActionHandler;
257 m_pActionHandler = NULL;
258}
259
260int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
261 const FX_WCHAR* Title,
262 FX_UINT Type,
263 FX_UINT Icon) {
264 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
265 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
266 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
267 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
268 FPDF_WIDESTRING pTitle =
269 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
270 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
271 pTitle, Type, Icon);
272 bsMsg.ReleaseBuffer();
273 bsTitle.ReleaseBuffer();
274 return ret;
275 }
276 return -1;
277}
278
279int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
280 const FX_WCHAR* Title,
281 const FX_WCHAR* Default,
282 const FX_WCHAR* cLabel,
283 FPDF_BOOL bPassword,
284 void* response,
285 int length) {
286 if (m_pInfo && m_pInfo->m_pJsPlatform &&
287 m_pInfo->m_pJsPlatform->app_response) {
288 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
289 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
290 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
291 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
292 FPDF_WIDESTRING pQuestion =
293 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
294 FPDF_WIDESTRING pTitle =
295 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
296 FPDF_WIDESTRING pDefault =
297 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
298 FPDF_WIDESTRING pLabel =
299 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
300 int ret = m_pInfo->m_pJsPlatform->app_response(
301 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
302 response, length);
303 bsQuestion.ReleaseBuffer();
304 bsTitle.ReleaseBuffer();
305 bsDefault.ReleaseBuffer();
306 bsLabel.ReleaseBuffer();
307 return ret;
308 }
309 return -1;
310}
311
312CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
313 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
314 !m_pInfo->m_pJsPlatform->Field_browse) {
315 return L"";
316 }
317
318 const int nRequiredLen =
319 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
320 if (nRequiredLen <= 0)
321 return L"";
322
323 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
324 memset(pBuff.get(), 0, nRequiredLen);
325 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
326 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
327 if (nActualLen <= 0 || nActualLen > nRequiredLen)
328 return L"";
329
330 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
331 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
332 return wsRet;
333}
334
335CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
336 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
337 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
338 return L"";
339 }
340
341 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
342 m_pInfo->m_pJsPlatform, nullptr, 0);
343 if (nRequiredLen <= 0)
344 return L"";
345
346 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
347 memset(pBuff.get(), 0, nRequiredLen);
348 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
349 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
350 if (nActualLen <= 0 || nActualLen > nRequiredLen)
351 return L"";
352
353 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
354 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
355 return wsRet;
356}
357
358void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
359 int length,
360 const FX_WCHAR* URL) {
361 if (m_pInfo && m_pInfo->m_pJsPlatform &&
362 m_pInfo->m_pJsPlatform->Doc_submitForm) {
363 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
364 FPDF_WIDESTRING pDestination =
365 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
366 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
367 length, pDestination);
368 bsDestination.ReleaseBuffer();
369 }
370}
371
372void CPDFDoc_Environment::JS_docmailForm(void* mailData,
373 int length,
374 FPDF_BOOL bUI,
375 const FX_WCHAR* To,
376 const FX_WCHAR* Subject,
377 const FX_WCHAR* CC,
378 const FX_WCHAR* BCC,
379 const FX_WCHAR* Msg) {
380 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
381 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
382 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
383 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
384 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
385 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
386 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
387 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
388 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
389 FPDF_WIDESTRING pSubject =
390 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
391 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
392 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
393 bUI, pTo, pSubject, pCC, pBcc, pMsg);
394 bsTo.ReleaseBuffer();
395 bsCC.ReleaseBuffer();
396 bsBcc.ReleaseBuffer();
397 bsSubject.ReleaseBuffer();
398 bsMsg.ReleaseBuffer();
399 }
400}
401
402IFXJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
403 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700404 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 if (!m_pJSRuntime)
406 m_pJSRuntime =
407 CPDFXFA_App::GetInstance()->GetRuntimeFactory()->NewJSRuntime(this);
408 return m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700409}
410
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
412 if (!m_pAnnotHandlerMgr)
413 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
414 return m_pAnnotHandlerMgr;
Bo Xufdc00a72014-10-28 23:03:33 -0700415}
416
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
418 if (!m_pActionHandler)
419 m_pActionHandler = new CPDFSDK_ActionHandler(this);
420 return m_pActionHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700421}
422
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
424 if (!m_pIFormFiller)
425 m_pIFormFiller = new CFFL_IFormFiller(this);
426 return m_pIFormFiller;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700427}
428
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
430 CPDFDoc_Environment* pEnv)
431 : m_pDoc(pDoc),
432 m_pInterForm(nullptr),
433 m_pFocusAnnot(nullptr),
434 m_pEnv(pEnv),
435 m_pOccontent(nullptr),
436 m_bChangeMask(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700437
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438CPDFSDK_Document::~CPDFSDK_Document() {
439 for (auto& it : m_pageMap)
440 delete it.second;
441 m_pageMap.clear();
442
443 delete m_pInterForm;
444 m_pInterForm = nullptr;
445
446 delete m_pOccontent;
447 m_pOccontent = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700448}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700449
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
451 FX_BOOL ReNew) {
452 auto it = m_pageMap.find(pPDFXFAPage);
453 if (it != m_pageMap.end())
454 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700455
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 if (!ReNew)
457 return nullptr;
458
459 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
460 m_pageMap[pPDFXFAPage] = pPageView;
461 // Delay to load all the annotations, to avoid endless loop.
462 pPageView->LoadFXAnnots();
463 return pPageView;
464}
465
466CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
467 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
468 return pPage ? GetPageView(pPage, TRUE) : nullptr;
469}
470
471CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
472 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
473 if (!pTempPage)
474 return nullptr;
475
476 auto it = m_pageMap.find(pTempPage);
477 return it->second;
478}
479
480void CPDFSDK_Document::ProcJavascriptFun() {
481 CPDFXFA_Document* pPDFDoc = GetDocument();
482 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
483 int iCount = docJS.CountJSActions();
484 if (iCount < 1)
485 return;
486 for (int i = 0; i < iCount; i++) {
487 CFX_ByteString csJSName;
488 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
489 if (m_pEnv->GetActionHander())
490 m_pEnv->GetActionHander()->DoAction_JavaScript(
491 jsAction, CFX_WideString::FromLocal(csJSName), this);
492 }
493}
494
495FX_BOOL CPDFSDK_Document::ProcOpenAction() {
496 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700497 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498
499 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
500 if (!pRoot)
501 return FALSE;
502
503 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
504 if (!pOpenAction)
505 pOpenAction = pRoot->GetArray("OpenAction");
506
507 if (!pOpenAction)
508 return FALSE;
509
510 if (pOpenAction->GetType() == PDFOBJ_ARRAY)
511 return TRUE;
512
513 if (pOpenAction->GetType() == PDFOBJ_DICTIONARY) {
514 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pOpenAction;
515 CPDF_Action action(pDict);
516 if (m_pEnv->GetActionHander())
517 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
518 return TRUE;
519 }
520 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700521}
522
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
524 if (!m_pOccontent)
525 m_pOccontent = new CPDF_OCContext(m_pDoc->GetPDFDoc());
526 return m_pOccontent;
527}
Bo Xufdc00a72014-10-28 23:03:33 -0700528
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700529void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
530 auto it = m_pageMap.find(pPDFXFAPage);
531 if (it == m_pageMap.end())
532 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700533
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534 CPDFSDK_PageView* pPageView = it->second;
535 if (pPageView->IsLocked())
536 return;
537
538 delete pPageView;
539 m_pageMap.erase(it);
540}
541
542CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
543 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
544 if (!pTempPage)
545 return NULL;
546 return pTempPage;
547}
548
549CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
550 if (!m_pInterForm)
551 m_pInterForm = new CPDFSDK_InterForm(this);
552 return m_pInterForm;
553}
554
555void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
556 CPDFSDK_Annot* pAnnot) {
557 for (const auto& it : m_pageMap) {
558 CPDFSDK_PageView* pPageView = it.second;
559 if (pPageView != pSender) {
560 pPageView->UpdateView(pAnnot);
561 }
562 }
563}
564
565CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
566 return m_pFocusAnnot;
567}
568
569FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
570 if (m_pFocusAnnot == pAnnot)
571 return TRUE;
572
573 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
574
575 if (m_pFocusAnnot) {
576 if (!KillFocusAnnot(nFlag))
577 return FALSE;
578 }
579 CPDFSDK_PageView* pPageView = NULL;
580 if (pAnnot)
581 pPageView = pAnnot->GetPageView();
582 if (pAnnot && pPageView->IsValid()) {
583 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
584
585 if (pAnnotHandler && !m_pFocusAnnot) {
586 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
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_OnSetFocus(pAnnot, nFlag))
590 return FALSE;
591 if (!m_pFocusAnnot) {
592 m_pFocusAnnot = pAnnot;
593 return TRUE;
594 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700595 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596 }
597 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700598}
599
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
601 if (m_pFocusAnnot) {
602 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
603 if (pAnnotHandler) {
604 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
605 m_pFocusAnnot = NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700606
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700607 if (!pAnnotHandler->Annot_OnChangeFocus(NULL, pFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700608 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700609
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700610 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
611 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
612 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
613 int nFieldType = pWidget->GetFieldType();
614 if (FIELDTYPE_TEXTFIELD == nFieldType ||
615 FIELDTYPE_COMBOBOX == nFieldType)
616 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
617 }
618
619 if (!m_pFocusAnnot)
620 return TRUE;
621 } else {
622 m_pFocusAnnot = pFocusAnnot;
623 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700624 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700625 }
626 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700627}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700628
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700629FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
630 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
631 return FALSE;
632 }
633
634 CPDFXFA_Page* pTempPage = NULL;
635 for (int i = nCount - 1; i >= 0; i--) {
636 pTempPage = GetPage(nStart + i);
637 if (pTempPage != NULL) {
638 ReMovePageView(pTempPage);
639 }
640 }
641 return TRUE;
642}
643
644void CPDFSDK_Document::OnCloseDocument() {
645 KillFocusAnnot();
646}
647
648FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
649 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
650 return dwPermissions & nFlag;
651}
652
653IFXJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
654 ASSERT(m_pEnv != NULL);
655 return m_pEnv->GetJSRuntime();
656}
657
658CFX_WideString CPDFSDK_Document::GetPath() {
659 ASSERT(m_pEnv != NULL);
660 return m_pEnv->JS_docGetFilePath();
661}
662
663CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
664 CPDFXFA_Page* page)
665 : m_page(page), m_pSDKDoc(pSDKDoc) {
666 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
667 if (pInterForm) {
668 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
669 if (page->GetPDFPage())
670 pPDFInterForm->FixPageFields(page->GetPDFPage());
671 }
672 m_fxAnnotArray.RemoveAll();
673
674 m_bEnterWidget = FALSE;
675 m_bExitWidget = FALSE;
676 m_bOnWidget = FALSE;
677 m_CaptureWidget = NULL;
678 m_bValid = FALSE;
679 m_bLocked = FALSE;
680 m_pAnnotList = NULL;
681}
682
683CPDFSDK_PageView::~CPDFSDK_PageView() {
684 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
685 int nAnnotCount = m_fxAnnotArray.GetSize();
686 for (int i = 0; i < nAnnotCount; i++) {
687 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
688 // if there is a focused annot on the page, we should kill the focus first.
689 if (pAnnot == m_pSDKDoc->GetFocusAnnot())
690 KillFocusAnnot();
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700691 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
692 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
694 }
695 m_fxAnnotArray.RemoveAll();
696
697 delete m_pAnnotList;
698 m_pAnnotList = NULL;
699}
700
701void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
702 CPDF_Matrix* pUser2Device,
703 CPDF_RenderOptions* pOptions,
704 FX_RECT* pClip) {
705 m_curMatrix = *pUser2Device;
706 // m_pAnnotList->DisplayAnnots(m_page, pDevice, pUser2Device, FALSE,
707 // pOptions);
708 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
709 CPDFXFA_Page* pPage = GetPDFXFAPage();
710
711 if (pPage == NULL)
712 return;
713
714 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
715 CFX_Graphics gs;
716 gs.Create(pDevice);
717 if (pClip) {
718 CFX_RectF rectClip;
719 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
720 static_cast<FX_FLOAT>(pClip->top),
721 static_cast<FX_FLOAT>(pClip->Width()),
722 static_cast<FX_FLOAT>(pClip->Height()));
723 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700724 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
726 if (!pRenderContext)
727 return;
728 CXFA_RenderOptions renderOptions;
729 renderOptions.m_bHighlight = TRUE;
730 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
731 renderOptions);
732 pRenderContext->DoRender();
733 pRenderContext->StopRender();
734 pRenderContext->Release();
735 return;
736 }
737 // for pdf/static xfa.
738 CPDFSDK_AnnotIterator annotIterator(this, TRUE);
739 CPDFSDK_Annot* pSDKAnnot = NULL;
740 int index = -1;
741 pSDKAnnot = annotIterator.Next(index);
742 while (pSDKAnnot) {
743 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
744 ASSERT(pAnnotHandlerMgr);
745 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
746 pSDKAnnot = annotIterator.Next(index);
747 }
748}
749CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
750 FX_FLOAT pageY) {
751 int nCount = m_pAnnotList->Count();
752 for (int i = 0; i < nCount; i++) {
753 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
754 CFX_FloatRect annotRect;
755 pAnnot->GetRect(annotRect);
756 if (annotRect.Contains(pageX, pageY))
757 return pAnnot;
758 }
759 return NULL;
760}
761
762CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
763 FX_FLOAT pageY) {
764 int nCount = m_pAnnotList->Count();
765 for (int i = 0; i < nCount; i++) {
766 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
767 if (pAnnot->GetSubType() == "Widget") {
768 CFX_FloatRect annotRect;
769 pAnnot->GetRect(annotRect);
770 if (annotRect.Contains(pageX, pageY))
771 return pAnnot;
772 }
773 }
774 return NULL;
775}
776
777CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
778 FX_FLOAT pageY) {
779 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
780 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
781 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
782 CPDFSDK_Annot* pSDKAnnot = NULL;
783 int index = -1;
784 pSDKAnnot = annotIterator.Next(index);
785 while (pSDKAnnot) {
786 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
787 if (rc.Contains(pageX, pageY))
788 return pSDKAnnot;
789 pSDKAnnot = annotIterator.Next(index);
790 }
791
792 return NULL;
793}
794
795CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
796 FX_FLOAT pageY) {
797 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
798 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
799 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
800 CPDFSDK_Annot* pSDKAnnot = NULL;
801 int index = -1;
802 pSDKAnnot = annotIterator.Next(index);
803 while (pSDKAnnot) {
804 if (pSDKAnnot->GetType() == "Widget" ||
805 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
806 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
807 CPDF_Point point(pageX, pageY);
808 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
809 return pSDKAnnot;
810 }
811 pSDKAnnot = annotIterator.Next(index);
812 }
813
814 return NULL;
815}
816
817FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
818 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
819 if (pAnnotDic)
820 return pAnnotDic->KeyExist("AS");
821 return FALSE;
822}
823
824CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
825 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
826 ASSERT(pEnv);
827 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
828
829 CPDFSDK_Annot* pSDKAnnot = NULL;
830
831 if (pAnnotHandler) {
832 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
833 }
834 if (!pSDKAnnot)
835 return NULL;
836
837 m_fxAnnotArray.Add(pSDKAnnot);
838
839 if (pAnnotHandler) {
840 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
841 }
842
843 return pSDKAnnot;
844}
845
846CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
847 if (!pPDFAnnot)
848 return NULL;
849
850 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
851 if (pSDKAnnot)
852 return pSDKAnnot;
853
854 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
855 ASSERT(pEnv);
856 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
857
858 pSDKAnnot = NULL;
859
860 if (pAnnotHandler) {
861 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
862 }
863 if (!pSDKAnnot)
864 return NULL;
865
866 m_fxAnnotArray.Add(pSDKAnnot);
867
868 return pSDKAnnot;
869}
870
871CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
872 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
873}
874
875CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
876 CPDF_Dictionary* pDict) {
877 return NULL;
878}
879
880FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
881 if (!pAnnot)
882 return FALSE;
883 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
884 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
885 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
886 return FALSE;
887
888 int index = m_fxAnnotArray.Find(pAnnot);
889 m_fxAnnotArray.RemoveAt(index);
890 if (m_CaptureWidget == pAnnot)
891 m_CaptureWidget = NULL;
892
893 return TRUE;
894}
895
896CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
897 if (m_page) {
898 return m_page->GetDocument()->GetPDFDoc();
899 }
900 return NULL;
901}
902
903CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
904 if (m_page) {
905 return m_page->GetPDFPage();
906 }
907
908 return NULL;
909}
910
911int CPDFSDK_PageView::CountAnnots() {
912 return m_fxAnnotArray.GetSize();
913}
914
915CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(int nIndex) {
916 int nCount = m_fxAnnotArray.GetSize();
917 if (nIndex < 0 || nIndex >= nCount) {
918 return NULL;
919 }
920
921 return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
922}
923
924CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
925 int nCount = m_fxAnnotArray.GetSize();
926 for (int i = 0; i < nCount; i++) {
927 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
928 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
929 return pAnnot;
930 }
931 return NULL;
932}
933CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
934 if (hWidget == NULL)
935 return NULL;
936 int annotCount = m_fxAnnotArray.GetSize();
937
938 for (int i = 0; i < annotCount; i++) {
939 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
940 if (pAnnot->GetXFAWidget() == hWidget)
941 return pAnnot;
942 }
943 return NULL;
944}
945
946FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
947 FX_UINT nFlag) {
948 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
949 ASSERT(pEnv);
950 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
951 if (!pFXAnnot) {
952 KillFocusAnnot(nFlag);
953 } else {
954 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
955 ASSERT(pAnnotHandlerMgr);
956
957 FX_BOOL bRet =
958 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
959 if (bRet) {
960 SetFocusAnnot(pFXAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700961 }
962 return bRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700963 }
964 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700965}
966
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700967FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
968 FX_UINT nFlag) {
969 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
970 ASSERT(pEnv);
971 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
972 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700973
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700974 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
975
976 if (pFXAnnot == NULL)
977 return FALSE;
978
979 FX_BOOL bRet =
980 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
981 if (bRet) {
982 SetFocusAnnot(pFXAnnot);
983 }
984 return TRUE;
985}
986
987FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
988 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
989 ASSERT(pEnv);
990 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
991 ASSERT(pAnnotHandlerMgr);
992
993 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
994
995 if (pFXAnnot == NULL)
996 return FALSE;
997
998 FX_BOOL bRet =
999 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
1000 if (bRet) {
1001 SetFocusAnnot(pFXAnnot);
1002 }
1003 return TRUE;
1004}
1005
1006FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
1007 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1008 ASSERT(pEnv);
1009 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1010 ASSERT(pAnnotHandlerMgr);
1011 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
1012 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
1013 FX_BOOL bRet = FALSE;
1014 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
1015 // Last focus Annot gets a chance to handle the event.
1016 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
1017 }
1018 if (pFXAnnot && !bRet) {
1019 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
1020 return bRet;
1021 }
1022 return bRet;
1023}
1024
1025FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
1026 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1027 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1028 ASSERT(pAnnotHandlerMgr);
1029 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
1030 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
1031 m_bExitWidget = TRUE;
1032 m_bEnterWidget = FALSE;
1033 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1034 }
1035 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
1036 m_bOnWidget = TRUE;
1037 if (!m_bEnterWidget) {
1038 m_bEnterWidget = TRUE;
1039 m_bExitWidget = FALSE;
1040 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
1041 }
1042 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
1043 return TRUE;
1044 }
1045 if (m_bOnWidget) {
1046 m_bOnWidget = FALSE;
1047 m_bExitWidget = TRUE;
1048 m_bEnterWidget = FALSE;
1049 if (m_CaptureWidget) {
1050 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1051 m_CaptureWidget = NULL;
1052 }
1053 }
1054 return FALSE;
1055}
1056
1057FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1058 double deltaY,
1059 const CPDF_Point& point,
1060 int nFlag) {
1061 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001062 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1063 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1064 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001065 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1066 (int)deltaY, point);
1067 }
1068 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001069}
1070
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1072 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001073 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001074 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 ASSERT(pAnnotHandlerMgr);
1076 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1077 }
Bo Xufdc00a72014-10-28 23:03:33 -07001078
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 -07001082FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1083 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001084 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001085 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1086 ASSERT(pAnnotHandlerMgr);
1087 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1088 }
1089 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001090}
1091
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
1093 // if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
1094 // {
1095 // CFFL_IFormFiller* pIFormFiller = g_pFormFillApp->GetIFormFiller();
1096 // return pIFormFiller->OnKeyUp(pAnnot, nKeyCode, nFlag);
1097 // }
1098 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001099}
1100
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101extern void CheckUnSupportAnnot(CPDF_Document* pDoc, CPDF_Annot* pPDFAnnot);
1102
1103void CPDFSDK_PageView::LoadFXAnnots() {
1104 ASSERT(m_page != NULL);
1105
1106 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1107 ASSERT(pEnv != NULL);
1108
1109 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1110 ASSERT(pAnnotHandlerMgr != NULL);
1111
1112 SetLock(TRUE);
1113 m_page->AddRef();
1114 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1115 IXFA_PageView* pageView = NULL;
1116 pageView = m_page->GetXFAPageView();
1117 ASSERT(pageView != NULL);
1118
1119 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1120 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1121 XFA_WIDGETFILTER_Viewable |
1122 XFA_WIDGETFILTER_AllType);
1123 if (!pWidgetHander) {
1124 m_page->Release();
1125 SetLock(FALSE);
1126 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001127 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001128
1129 IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext();
1130 while (pXFAAnnot) {
1131 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
1132 if (!pAnnot) {
1133 pXFAAnnot = pWidgetHander->MoveToNext();
1134 continue;
1135 }
1136 m_fxAnnotArray.Add(pAnnot);
1137
1138 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1139
1140 pXFAAnnot = pWidgetHander->MoveToNext();
1141 }
1142
1143 pWidgetHander->Release();
1144 } else {
1145 CPDF_Page* pPage = m_page->GetPDFPage();
1146 ASSERT(pPage != NULL);
1147
1148 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1149 // Disable the default AP construction.
1150 CPDF_InterForm::EnableUpdateAP(FALSE);
1151 m_pAnnotList = new CPDF_AnnotList(pPage);
1152 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1153
1154 int nCount = m_pAnnotList->Count();
1155 for (int i = 0; i < nCount; i++) {
1156 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1157 CPDF_Document* pDoc = GetPDFDocument();
1158
1159 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1160
1161 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1162 if (!pAnnot)
1163 continue;
1164 m_fxAnnotArray.Add(pAnnot);
1165
1166 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1167 }
1168 }
1169 m_page->Release();
1170 SetLock(FALSE);
1171}
1172
1173void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1174 for (int i = 0; i < rects.GetSize(); i++) {
1175 CPDF_Rect rc = rects.GetAt(i);
1176 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1177 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1178 }
1179}
1180
1181void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1182 CPDF_Rect rcWindow = pAnnot->GetRect();
1183 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1184 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1185 rcWindow.bottom);
1186}
1187
1188int CPDFSDK_PageView::GetPageIndex() {
1189 if (m_page) {
1190 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1191 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1192 if (pDoc && pDic) {
1193 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1194 }
1195 }
1196 return -1;
1197}
1198
1199FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) {
1200 if (p == NULL)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001201 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001202 int iCount = m_pAnnotList->Count();
1203 for (int i = 0; i < iCount; i++) {
1204 if (m_pAnnotList->GetAt(i) == p)
1205 return TRUE;
1206 }
1207 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001208}
1209
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1211 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1212 if (!pFocusAnnot)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001213 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001214
1215 for (int i = 0; i < m_fxAnnotArray.GetSize(); i++) {
1216 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1217 if (pAnnot == pFocusAnnot)
1218 return pAnnot;
1219 }
1220 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001221}