blob: c1ab1bd78083df205efbeb4ce9ad21fcaed7598b [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 Sepez37458412015-10-06 11:33:46 -07008#include "../include/formfiller/FFL_FormFiller.h"
9#include "../include/fpdfxfa/fpdfxfa_app.h"
Bo Xufdc00a72014-10-28 23:03:33 -070010#include "../include/fpdfxfa/fpdfxfa_doc.h"
11#include "../include/fpdfxfa/fpdfxfa_page.h"
12#include "../include/fpdfxfa/fpdfxfa_util.h"
Tom Sepez37458412015-10-06 11:33:46 -070013#include "../include/fsdk_define.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014#include "../include/fsdk_mgr.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015#include "../include/javascript/IJavaScript.h"
16
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 -070023class CFX_SystemHandler : public IFX_SystemHandler {
24 public:
25 CFX_SystemHandler(CPDFDoc_Environment* pEnv) : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070026 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070029 // IFX_SystemHandler
30 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
31 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
32 FX_BOOL IsSelectionImplemented() override;
33 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
34 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 return FALSE;
36 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070037 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
38 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
39 void SetCursor(int32_t nCursorType) override;
40 FX_HMENU CreatePopupMenu() override { return NULL; }
41 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
42 int32_t nIDNewItem,
43 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 return FALSE;
45 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070046 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
47 int32_t nIDItem,
48 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 return FALSE;
50 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070051 int32_t TrackPopupMenu(FX_HMENU hMenu,
52 int32_t x,
53 int32_t y,
54 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 return -1;
56 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070057 void DestroyMenu(FX_HMENU hMenu) override {}
58 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
59 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
60 CFX_ByteString sFontFaceName) override;
61 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
62 CFX_ByteString sFontFaceName,
63 uint8_t nCharset) override;
64 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
65 void KillTimer(int32_t nID) override;
66 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
68 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070069 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
71 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070072 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 return m_pEnv->FFI_IsALTKeyDown(nFlag);
74 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070075 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
77 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070078 FX_SYSTEMTIME GetLocalTime() override;
79 int32_t GetCharSet() override { return m_nCharSet; }
80 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081
82 private:
83 CPDFDoc_Environment* m_pEnv;
84 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085};
86
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
88 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089}
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
92 // g_pFormFillApp->FFI_Invalidate();
93 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
94 CPDFXFA_Page* pPage = NULL;
95 CPDFSDK_PageView* pPageView = NULL;
96 pPageView = pSDKAnnot->GetPageView();
97 pPage = pSDKAnnot->GetPDFXFAPage();
98 if (!pPage || !pPageView)
99 return;
100 CPDF_Matrix page2device;
101 pPageView->GetCurrentMatrix(page2device);
102 CPDF_Matrix device2page;
103 device2page.SetReverse(page2device);
104 FX_FLOAT left, top, right, bottom;
105 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
106 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
107 bottom);
108 // m_pEnv->FFI_DeviceToPage(pPage, rect.left, rect.top, (double*)&left,
109 // (double*)&top);
110 // m_pEnv->FFI_DeviceToPage(pPage, rect.right, rect.bottom, (double*)&right,
111 // (double*)&bottom);
112 CPDF_Rect rcPDF(left, bottom, right, top);
113 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
116 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
119 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
120 if (pFFL) {
121 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
122 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
123 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
124 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
Tom Sepez468e5892015-10-13 15:49:36 -0700125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 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
Tom Sepezba038bc2015-10-08 12:03:00 -0700380IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700382 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 if (!m_pJSRuntime)
Tom Sepezba038bc2015-10-08 12:03:00 -0700384 m_pJSRuntime.reset(IJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700385 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
Tom Sepezba038bc2015-10-08 12:03:00 -0700630IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700631 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
Tom Sepeza8a39e22015-10-12 15:47:07 -0700698#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700699 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
700 CFX_Graphics gs;
701 gs.Create(pDevice);
702 if (pClip) {
703 CFX_RectF rectClip;
704 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
705 static_cast<FX_FLOAT>(pClip->top),
706 static_cast<FX_FLOAT>(pClip->Width()),
707 static_cast<FX_FLOAT>(pClip->Height()));
708 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700709 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700710 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
711 if (!pRenderContext)
712 return;
713 CXFA_RenderOptions renderOptions;
714 renderOptions.m_bHighlight = TRUE;
715 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
716 renderOptions);
717 pRenderContext->DoRender();
718 pRenderContext->StopRender();
719 pRenderContext->Release();
720 return;
721 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700722#endif // PDF_ENABLE_XFA
723
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724 // for pdf/static xfa.
725 CPDFSDK_AnnotIterator annotIterator(this, TRUE);
726 CPDFSDK_Annot* pSDKAnnot = NULL;
727 int index = -1;
728 pSDKAnnot = annotIterator.Next(index);
729 while (pSDKAnnot) {
730 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
731 ASSERT(pAnnotHandlerMgr);
732 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
733 pSDKAnnot = annotIterator.Next(index);
734 }
735}
736CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
737 FX_FLOAT pageY) {
738 int nCount = m_pAnnotList->Count();
739 for (int i = 0; i < nCount; i++) {
740 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
741 CFX_FloatRect annotRect;
742 pAnnot->GetRect(annotRect);
743 if (annotRect.Contains(pageX, pageY))
744 return pAnnot;
745 }
746 return NULL;
747}
748
749CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(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 if (pAnnot->GetSubType() == "Widget") {
755 CFX_FloatRect annotRect;
756 pAnnot->GetRect(annotRect);
757 if (annotRect.Contains(pageX, pageY))
758 return pAnnot;
759 }
760 }
761 return NULL;
762}
763
764CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
765 FX_FLOAT pageY) {
766 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
767 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
768 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
769 CPDFSDK_Annot* pSDKAnnot = NULL;
770 int index = -1;
771 pSDKAnnot = annotIterator.Next(index);
772 while (pSDKAnnot) {
773 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
774 if (rc.Contains(pageX, pageY))
775 return pSDKAnnot;
776 pSDKAnnot = annotIterator.Next(index);
777 }
778
779 return NULL;
780}
781
782CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
783 FX_FLOAT pageY) {
784 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
785 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
786 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
787 CPDFSDK_Annot* pSDKAnnot = NULL;
788 int index = -1;
789 pSDKAnnot = annotIterator.Next(index);
790 while (pSDKAnnot) {
791 if (pSDKAnnot->GetType() == "Widget" ||
792 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
793 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
794 CPDF_Point point(pageX, pageY);
795 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
796 return pSDKAnnot;
797 }
798 pSDKAnnot = annotIterator.Next(index);
799 }
800
801 return NULL;
802}
803
804FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
805 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
806 if (pAnnotDic)
807 return pAnnotDic->KeyExist("AS");
808 return FALSE;
809}
810
811CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
812 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
813 ASSERT(pEnv);
814 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
815
816 CPDFSDK_Annot* pSDKAnnot = NULL;
817
818 if (pAnnotHandler) {
819 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
820 }
821 if (!pSDKAnnot)
822 return NULL;
823
824 m_fxAnnotArray.Add(pSDKAnnot);
825
826 if (pAnnotHandler) {
827 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
828 }
829
830 return pSDKAnnot;
831}
832
833CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
834 if (!pPDFAnnot)
835 return NULL;
836
837 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
838 if (pSDKAnnot)
839 return pSDKAnnot;
840
841 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
842 ASSERT(pEnv);
843 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
844
845 pSDKAnnot = NULL;
846
847 if (pAnnotHandler) {
848 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
849 }
850 if (!pSDKAnnot)
851 return NULL;
852
853 m_fxAnnotArray.Add(pSDKAnnot);
854
855 return pSDKAnnot;
856}
857
858CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
859 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
860}
861
862CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
863 CPDF_Dictionary* pDict) {
864 return NULL;
865}
866
867FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
868 if (!pAnnot)
869 return FALSE;
870 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
871 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
872 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
873 return FALSE;
874
875 int index = m_fxAnnotArray.Find(pAnnot);
876 m_fxAnnotArray.RemoveAt(index);
877 if (m_CaptureWidget == pAnnot)
878 m_CaptureWidget = NULL;
879
880 return TRUE;
881}
882
883CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
884 if (m_page) {
885 return m_page->GetDocument()->GetPDFDoc();
886 }
887 return NULL;
888}
889
890CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
891 if (m_page) {
892 return m_page->GetPDFPage();
893 }
894
895 return NULL;
896}
897
898int CPDFSDK_PageView::CountAnnots() {
899 return m_fxAnnotArray.GetSize();
900}
901
902CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(int nIndex) {
903 int nCount = m_fxAnnotArray.GetSize();
904 if (nIndex < 0 || nIndex >= nCount) {
905 return NULL;
906 }
907
908 return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
909}
910
911CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
912 int nCount = m_fxAnnotArray.GetSize();
913 for (int i = 0; i < nCount; i++) {
914 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
915 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
916 return pAnnot;
917 }
918 return NULL;
919}
920CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
921 if (hWidget == NULL)
922 return NULL;
923 int annotCount = m_fxAnnotArray.GetSize();
924
925 for (int i = 0; i < annotCount; i++) {
926 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
927 if (pAnnot->GetXFAWidget() == hWidget)
928 return pAnnot;
929 }
930 return NULL;
931}
932
933FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
934 FX_UINT nFlag) {
935 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
936 ASSERT(pEnv);
937 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
938 if (!pFXAnnot) {
939 KillFocusAnnot(nFlag);
940 } else {
941 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
942 ASSERT(pAnnotHandlerMgr);
943
944 FX_BOOL bRet =
945 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
946 if (bRet) {
947 SetFocusAnnot(pFXAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700948 }
949 return bRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700950 }
951 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700952}
953
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700954FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
955 FX_UINT nFlag) {
956 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
957 ASSERT(pEnv);
958 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
959 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700960
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700961 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
962
963 if (pFXAnnot == NULL)
964 return FALSE;
965
966 FX_BOOL bRet =
967 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
968 if (bRet) {
969 SetFocusAnnot(pFXAnnot);
970 }
971 return TRUE;
972}
973
974FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
975 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
976 ASSERT(pEnv);
977 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
978 ASSERT(pAnnotHandlerMgr);
979
980 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
981
982 if (pFXAnnot == NULL)
983 return FALSE;
984
985 FX_BOOL bRet =
986 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
987 if (bRet) {
988 SetFocusAnnot(pFXAnnot);
989 }
990 return TRUE;
991}
992
993FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
994 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
995 ASSERT(pEnv);
996 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
997 ASSERT(pAnnotHandlerMgr);
998 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
999 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
1000 FX_BOOL bRet = FALSE;
1001 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
1002 // Last focus Annot gets a chance to handle the event.
1003 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
1004 }
1005 if (pFXAnnot && !bRet) {
1006 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
1007 return bRet;
1008 }
1009 return bRet;
1010}
1011
1012FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
1013 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1014 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1015 ASSERT(pAnnotHandlerMgr);
1016 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
1017 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
1018 m_bExitWidget = TRUE;
1019 m_bEnterWidget = FALSE;
1020 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1021 }
1022 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
1023 m_bOnWidget = TRUE;
1024 if (!m_bEnterWidget) {
1025 m_bEnterWidget = TRUE;
1026 m_bExitWidget = FALSE;
1027 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
1028 }
1029 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
1030 return TRUE;
1031 }
1032 if (m_bOnWidget) {
1033 m_bOnWidget = FALSE;
1034 m_bExitWidget = TRUE;
1035 m_bEnterWidget = FALSE;
1036 if (m_CaptureWidget) {
1037 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1038 m_CaptureWidget = NULL;
1039 }
1040 }
1041 return FALSE;
1042}
1043
1044FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1045 double deltaY,
1046 const CPDF_Point& point,
1047 int nFlag) {
1048 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001049 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1050 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1051 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001052 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1053 (int)deltaY, point);
1054 }
1055 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001056}
1057
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1059 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001060 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001061 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001062 ASSERT(pAnnotHandlerMgr);
1063 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1064 }
Bo Xufdc00a72014-10-28 23:03:33 -07001065
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001067}
1068
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1070 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001071 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1073 ASSERT(pAnnotHandlerMgr);
1074 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1075 }
1076 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001077}
1078
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001081}
1082
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001083void CPDFSDK_PageView::LoadFXAnnots() {
1084 ASSERT(m_page != NULL);
1085
1086 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1087 ASSERT(pEnv != NULL);
1088
1089 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1090 ASSERT(pAnnotHandlerMgr != NULL);
1091
1092 SetLock(TRUE);
1093 m_page->AddRef();
1094 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1095 IXFA_PageView* pageView = NULL;
1096 pageView = m_page->GetXFAPageView();
1097 ASSERT(pageView != NULL);
1098
1099 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1100 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1101 XFA_WIDGETFILTER_Viewable |
1102 XFA_WIDGETFILTER_AllType);
1103 if (!pWidgetHander) {
1104 m_page->Release();
1105 SetLock(FALSE);
1106 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001107 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001108
1109 IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext();
1110 while (pXFAAnnot) {
1111 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
1112 if (!pAnnot) {
1113 pXFAAnnot = pWidgetHander->MoveToNext();
1114 continue;
1115 }
1116 m_fxAnnotArray.Add(pAnnot);
1117
1118 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1119
1120 pXFAAnnot = pWidgetHander->MoveToNext();
1121 }
1122
1123 pWidgetHander->Release();
1124 } else {
1125 CPDF_Page* pPage = m_page->GetPDFPage();
1126 ASSERT(pPage != NULL);
1127
1128 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1129 // Disable the default AP construction.
1130 CPDF_InterForm::EnableUpdateAP(FALSE);
1131 m_pAnnotList = new CPDF_AnnotList(pPage);
1132 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1133
1134 int nCount = m_pAnnotList->Count();
1135 for (int i = 0; i < nCount; i++) {
1136 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1137 CPDF_Document* pDoc = GetPDFDocument();
1138
1139 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1140
1141 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1142 if (!pAnnot)
1143 continue;
1144 m_fxAnnotArray.Add(pAnnot);
1145
1146 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1147 }
1148 }
1149 m_page->Release();
1150 SetLock(FALSE);
1151}
1152
1153void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1154 for (int i = 0; i < rects.GetSize(); i++) {
1155 CPDF_Rect rc = rects.GetAt(i);
1156 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1157 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1158 }
1159}
1160
1161void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1162 CPDF_Rect rcWindow = pAnnot->GetRect();
1163 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1164 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1165 rcWindow.bottom);
1166}
1167
1168int CPDFSDK_PageView::GetPageIndex() {
1169 if (m_page) {
1170 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1171 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1172 if (pDoc && pDic) {
1173 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1174 }
1175 }
1176 return -1;
1177}
1178
1179FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) {
1180 if (p == NULL)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001181 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001182 int iCount = m_pAnnotList->Count();
1183 for (int i = 0; i < iCount; i++) {
1184 if (m_pAnnotList->GetAt(i) == p)
1185 return TRUE;
1186 }
1187 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001188}
1189
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1191 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1192 if (!pFocusAnnot)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001193 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001194
1195 for (int i = 0; i < m_fxAnnotArray.GetSize(); i++) {
1196 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1197 if (pAnnot == pFocusAnnot)
1198 return pAnnot;
1199 }
1200 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001201}