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