blob: b7de36c7d3f405cccc3bc119253667e9546fb03d [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();
Lei Zhang122ee212015-10-21 17:07:24 -0700149 if (!pFontMgr)
150 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151
Lei Zhang122ee212015-10-21 17:07:24 -0700152 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
153 if (!pFontMapper)
154 return FALSE;
155
156 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
157 if (nSize == 0) {
158 pFontMapper->LoadInstalledFonts();
159 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
160 }
161
162 for (int i = 0; i < nSize; ++i) {
163 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
164 return TRUE;
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
Dan Sinclairf1251c12015-10-20 16:24:45 -0400490 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491 CPDF_Action action(pDict);
492 if (m_pEnv->GetActionHander())
493 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
494 return TRUE;
495 }
496 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700497}
498
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700499CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
500 if (!m_pOccontent)
501 m_pOccontent = new CPDF_OCContext(m_pDoc->GetPDFDoc());
502 return m_pOccontent;
503}
Bo Xufdc00a72014-10-28 23:03:33 -0700504
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
506 auto it = m_pageMap.find(pPDFXFAPage);
507 if (it == m_pageMap.end())
508 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700509
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 CPDFSDK_PageView* pPageView = it->second;
511 if (pPageView->IsLocked())
512 return;
513
514 delete pPageView;
515 m_pageMap.erase(it);
516}
517
518CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
519 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
520 if (!pTempPage)
521 return NULL;
522 return pTempPage;
523}
524
525CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
526 if (!m_pInterForm)
527 m_pInterForm = new CPDFSDK_InterForm(this);
528 return m_pInterForm;
529}
530
531void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
532 CPDFSDK_Annot* pAnnot) {
533 for (const auto& it : m_pageMap) {
534 CPDFSDK_PageView* pPageView = it.second;
535 if (pPageView != pSender) {
536 pPageView->UpdateView(pAnnot);
537 }
538 }
539}
540
541CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
542 return m_pFocusAnnot;
543}
544
545FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
546 if (m_pFocusAnnot == pAnnot)
547 return TRUE;
548
549 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
550
551 if (m_pFocusAnnot) {
552 if (!KillFocusAnnot(nFlag))
553 return FALSE;
554 }
555 CPDFSDK_PageView* pPageView = NULL;
556 if (pAnnot)
557 pPageView = pAnnot->GetPageView();
558 if (pAnnot && pPageView->IsValid()) {
559 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
560
561 if (pAnnotHandler && !m_pFocusAnnot) {
562 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700563 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700564
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
566 return FALSE;
567 if (!m_pFocusAnnot) {
568 m_pFocusAnnot = pAnnot;
569 return TRUE;
570 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700571 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572 }
573 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700574}
575
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
577 if (m_pFocusAnnot) {
578 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
579 if (pAnnotHandler) {
580 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
581 m_pFocusAnnot = NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700582
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 if (!pAnnotHandler->Annot_OnChangeFocus(NULL, pFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700584 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700585
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
587 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
588 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
589 int nFieldType = pWidget->GetFieldType();
590 if (FIELDTYPE_TEXTFIELD == nFieldType ||
591 FIELDTYPE_COMBOBOX == nFieldType)
592 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
593 }
594
595 if (!m_pFocusAnnot)
596 return TRUE;
597 } else {
598 m_pFocusAnnot = pFocusAnnot;
599 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700600 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 }
602 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700603}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700604
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
606 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
607 return FALSE;
608 }
609
610 CPDFXFA_Page* pTempPage = NULL;
611 for (int i = nCount - 1; i >= 0; i--) {
612 pTempPage = GetPage(nStart + i);
613 if (pTempPage != NULL) {
614 ReMovePageView(pTempPage);
615 }
616 }
617 return TRUE;
618}
619
620void CPDFSDK_Document::OnCloseDocument() {
621 KillFocusAnnot();
622}
623
624FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
625 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
626 return dwPermissions & nFlag;
627}
628
Tom Sepezba038bc2015-10-08 12:03:00 -0700629IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700630 ASSERT(m_pEnv != NULL);
631 return m_pEnv->GetJSRuntime();
632}
633
634CFX_WideString CPDFSDK_Document::GetPath() {
635 ASSERT(m_pEnv != NULL);
636 return m_pEnv->JS_docGetFilePath();
637}
638
639CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
640 CPDFXFA_Page* page)
641 : m_page(page), m_pSDKDoc(pSDKDoc) {
642 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
643 if (pInterForm) {
644 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
645 if (page->GetPDFPage())
646 pPDFInterForm->FixPageFields(page->GetPDFPage());
647 }
648 m_fxAnnotArray.RemoveAll();
649
650 m_bEnterWidget = FALSE;
651 m_bExitWidget = FALSE;
652 m_bOnWidget = FALSE;
653 m_CaptureWidget = NULL;
654 m_bValid = FALSE;
655 m_bLocked = FALSE;
656 m_pAnnotList = NULL;
657}
658
659CPDFSDK_PageView::~CPDFSDK_PageView() {
Tom Sepez396e8722015-09-09 10:16:08 -0700660 // if there is a focused annot on the page, we should kill the focus first.
661 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
662 for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
663 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
664 if (pAnnot == focusedAnnot) {
665 KillFocusAnnot();
666 break;
667 }
668 }
669 }
670
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700671 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700672 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
673 ASSERT(pAnnotHandlerMgr);
674 for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700676 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
677 }
678 m_fxAnnotArray.RemoveAll();
679
680 delete m_pAnnotList;
681 m_pAnnotList = NULL;
682}
683
684void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
685 CPDF_Matrix* pUser2Device,
686 CPDF_RenderOptions* pOptions,
687 FX_RECT* pClip) {
688 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700689 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
690 CPDFXFA_Page* pPage = GetPDFXFAPage();
691
692 if (pPage == NULL)
693 return;
694
Tom Sepeza8a39e22015-10-12 15:47:07 -0700695#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700696 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
697 CFX_Graphics gs;
698 gs.Create(pDevice);
699 if (pClip) {
700 CFX_RectF rectClip;
701 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
702 static_cast<FX_FLOAT>(pClip->top),
703 static_cast<FX_FLOAT>(pClip->Width()),
704 static_cast<FX_FLOAT>(pClip->Height()));
705 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700706 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700707 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
708 if (!pRenderContext)
709 return;
710 CXFA_RenderOptions renderOptions;
711 renderOptions.m_bHighlight = TRUE;
712 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
713 renderOptions);
714 pRenderContext->DoRender();
715 pRenderContext->StopRender();
716 pRenderContext->Release();
717 return;
718 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700719#endif // PDF_ENABLE_XFA
720
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721 // for pdf/static xfa.
722 CPDFSDK_AnnotIterator annotIterator(this, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723 int index = -1;
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700724 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next(index)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727 }
728}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700729
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
731 FX_FLOAT pageY) {
732 int nCount = m_pAnnotList->Count();
733 for (int i = 0; i < nCount; i++) {
734 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
735 CFX_FloatRect annotRect;
736 pAnnot->GetRect(annotRect);
737 if (annotRect.Contains(pageX, pageY))
738 return pAnnot;
739 }
740 return NULL;
741}
742
743CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
744 FX_FLOAT pageY) {
745 int nCount = m_pAnnotList->Count();
746 for (int i = 0; i < nCount; i++) {
747 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
748 if (pAnnot->GetSubType() == "Widget") {
749 CFX_FloatRect annotRect;
750 pAnnot->GetRect(annotRect);
751 if (annotRect.Contains(pageX, pageY))
752 return pAnnot;
753 }
754 }
755 return NULL;
756}
757
758CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
759 FX_FLOAT pageY) {
760 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
761 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
762 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
763 CPDFSDK_Annot* pSDKAnnot = NULL;
764 int index = -1;
765 pSDKAnnot = annotIterator.Next(index);
766 while (pSDKAnnot) {
767 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
768 if (rc.Contains(pageX, pageY))
769 return pSDKAnnot;
770 pSDKAnnot = annotIterator.Next(index);
771 }
772
773 return NULL;
774}
775
776CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
777 FX_FLOAT pageY) {
778 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
779 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
780 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700781 int index = -1;
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700782 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next(index)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700783 if (pSDKAnnot->GetType() == "Widget" ||
784 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
785 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
786 CPDF_Point point(pageX, pageY);
787 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
788 return pSDKAnnot;
789 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700790 }
791
792 return NULL;
793}
794
795FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
796 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
797 if (pAnnotDic)
798 return pAnnotDic->KeyExist("AS");
799 return FALSE;
800}
801
802CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
803 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
804 ASSERT(pEnv);
805 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
806
807 CPDFSDK_Annot* pSDKAnnot = NULL;
808
809 if (pAnnotHandler) {
810 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
811 }
812 if (!pSDKAnnot)
813 return NULL;
814
815 m_fxAnnotArray.Add(pSDKAnnot);
816
817 if (pAnnotHandler) {
818 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
819 }
820
821 return pSDKAnnot;
822}
823
824CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
825 if (!pPDFAnnot)
826 return NULL;
827
828 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
829 if (pSDKAnnot)
830 return pSDKAnnot;
831
832 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
833 ASSERT(pEnv);
834 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
835
836 pSDKAnnot = NULL;
837
838 if (pAnnotHandler) {
839 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
840 }
841 if (!pSDKAnnot)
842 return NULL;
843
844 m_fxAnnotArray.Add(pSDKAnnot);
845
846 return pSDKAnnot;
847}
848
849CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
850 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
851}
852
853CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
854 CPDF_Dictionary* pDict) {
855 return NULL;
856}
857
858FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
859 if (!pAnnot)
860 return FALSE;
861 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
862 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
863 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
864 return FALSE;
865
866 int index = m_fxAnnotArray.Find(pAnnot);
867 m_fxAnnotArray.RemoveAt(index);
868 if (m_CaptureWidget == pAnnot)
869 m_CaptureWidget = NULL;
870
871 return TRUE;
872}
873
874CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
875 if (m_page) {
876 return m_page->GetDocument()->GetPDFDoc();
877 }
878 return NULL;
879}
880
881CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
882 if (m_page) {
883 return m_page->GetPDFPage();
884 }
885
886 return NULL;
887}
888
889int CPDFSDK_PageView::CountAnnots() {
890 return m_fxAnnotArray.GetSize();
891}
892
893CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(int nIndex) {
894 int nCount = m_fxAnnotArray.GetSize();
895 if (nIndex < 0 || nIndex >= nCount) {
896 return NULL;
897 }
898
899 return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
900}
901
902CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
903 int nCount = m_fxAnnotArray.GetSize();
904 for (int i = 0; i < nCount; i++) {
905 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
906 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
907 return pAnnot;
908 }
909 return NULL;
910}
911CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
912 if (hWidget == NULL)
913 return NULL;
914 int annotCount = m_fxAnnotArray.GetSize();
915
916 for (int i = 0; i < annotCount; i++) {
917 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
918 if (pAnnot->GetXFAWidget() == hWidget)
919 return pAnnot;
920 }
921 return NULL;
922}
923
924FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
925 FX_UINT nFlag) {
926 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
927 ASSERT(pEnv);
928 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
929 if (!pFXAnnot) {
930 KillFocusAnnot(nFlag);
931 } else {
932 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
933 ASSERT(pAnnotHandlerMgr);
934
935 FX_BOOL bRet =
936 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
937 if (bRet) {
938 SetFocusAnnot(pFXAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700939 }
940 return bRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941 }
942 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700943}
944
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
946 FX_UINT nFlag) {
947 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
948 ASSERT(pEnv);
949 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
950 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700951
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
953
954 if (pFXAnnot == NULL)
955 return FALSE;
956
957 FX_BOOL bRet =
958 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
959 if (bRet) {
960 SetFocusAnnot(pFXAnnot);
961 }
962 return TRUE;
963}
964
965FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
966 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
967 ASSERT(pEnv);
968 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
969 ASSERT(pAnnotHandlerMgr);
970
971 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
972
973 if (pFXAnnot == NULL)
974 return FALSE;
975
976 FX_BOOL bRet =
977 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
978 if (bRet) {
979 SetFocusAnnot(pFXAnnot);
980 }
981 return TRUE;
982}
983
984FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
985 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
986 ASSERT(pEnv);
987 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
988 ASSERT(pAnnotHandlerMgr);
989 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
990 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
991 FX_BOOL bRet = FALSE;
992 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
993 // Last focus Annot gets a chance to handle the event.
994 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
995 }
996 if (pFXAnnot && !bRet) {
997 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
998 return bRet;
999 }
1000 return bRet;
1001}
1002
1003FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
1004 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1005 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1006 ASSERT(pAnnotHandlerMgr);
1007 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
1008 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
1009 m_bExitWidget = TRUE;
1010 m_bEnterWidget = FALSE;
1011 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1012 }
1013 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
1014 m_bOnWidget = TRUE;
1015 if (!m_bEnterWidget) {
1016 m_bEnterWidget = TRUE;
1017 m_bExitWidget = FALSE;
1018 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
1019 }
1020 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
1021 return TRUE;
1022 }
1023 if (m_bOnWidget) {
1024 m_bOnWidget = FALSE;
1025 m_bExitWidget = TRUE;
1026 m_bEnterWidget = FALSE;
1027 if (m_CaptureWidget) {
1028 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1029 m_CaptureWidget = NULL;
1030 }
1031 }
1032 return FALSE;
1033}
1034
1035FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1036 double deltaY,
1037 const CPDF_Point& point,
1038 int nFlag) {
1039 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001040 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1041 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1042 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1044 (int)deltaY, point);
1045 }
1046 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001047}
1048
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1050 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001051 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001052 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053 ASSERT(pAnnotHandlerMgr);
1054 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1055 }
Bo Xufdc00a72014-10-28 23:03:33 -07001056
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001058}
1059
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001060FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1061 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001062 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1064 ASSERT(pAnnotHandlerMgr);
1065 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1066 }
1067 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001068}
1069
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001070FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001072}
1073
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077
1078 SetLock(TRUE);
1079 m_page->AddRef();
1080 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1081 IXFA_PageView* pageView = NULL;
1082 pageView = m_page->GetXFAPageView();
1083 ASSERT(pageView != NULL);
1084
1085 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1086 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1087 XFA_WIDGETFILTER_Viewable |
1088 XFA_WIDGETFILTER_AllType);
1089 if (!pWidgetHander) {
1090 m_page->Release();
1091 SetLock(FALSE);
1092 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001093 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094
1095 IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext();
1096 while (pXFAAnnot) {
1097 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
1098 if (!pAnnot) {
1099 pXFAAnnot = pWidgetHander->MoveToNext();
1100 continue;
1101 }
1102 m_fxAnnotArray.Add(pAnnot);
1103
1104 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1105
1106 pXFAAnnot = pWidgetHander->MoveToNext();
1107 }
1108
1109 pWidgetHander->Release();
1110 } else {
1111 CPDF_Page* pPage = m_page->GetPDFPage();
1112 ASSERT(pPage != NULL);
1113
1114 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1115 // Disable the default AP construction.
1116 CPDF_InterForm::EnableUpdateAP(FALSE);
1117 m_pAnnotList = new CPDF_AnnotList(pPage);
1118 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1119
1120 int nCount = m_pAnnotList->Count();
1121 for (int i = 0; i < nCount; i++) {
1122 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1123 CPDF_Document* pDoc = GetPDFDocument();
1124
1125 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1126
1127 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1128 if (!pAnnot)
1129 continue;
1130 m_fxAnnotArray.Add(pAnnot);
1131
1132 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1133 }
1134 }
1135 m_page->Release();
1136 SetLock(FALSE);
1137}
1138
1139void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1140 for (int i = 0; i < rects.GetSize(); i++) {
1141 CPDF_Rect rc = rects.GetAt(i);
1142 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1143 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1144 }
1145}
1146
1147void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1148 CPDF_Rect rcWindow = pAnnot->GetRect();
1149 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1150 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1151 rcWindow.bottom);
1152}
1153
1154int CPDFSDK_PageView::GetPageIndex() {
1155 if (m_page) {
1156 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1157 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1158 if (pDoc && pDic) {
1159 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1160 }
1161 }
1162 return -1;
1163}
1164
1165FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) {
1166 if (p == NULL)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001167 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001168 int iCount = m_pAnnotList->Count();
1169 for (int i = 0; i < iCount; i++) {
1170 if (m_pAnnotList->GetAt(i) == p)
1171 return TRUE;
1172 }
1173 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001174}
1175
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001176CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1177 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1178 if (!pFocusAnnot)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001179 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180
1181 for (int i = 0; i < m_fxAnnotArray.GetSize(); i++) {
1182 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1183 if (pAnnot == pFocusAnnot)
1184 return pAnnot;
1185 }
1186 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001187}