blob: 9bbf7aae3fcf6efb0609b02d96489b301603f1cf [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
Lei Zhangbf60b292015-10-26 12:14:35 -07007#include <algorithm>
8
Lei Zhangb4e7f302015-11-06 15:52:32 -08009#include "../include/fsdk_mgr.h"
10
Tom Sepez37458412015-10-06 11:33:46 -070011#include "../include/formfiller/FFL_FormFiller.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070012#include "../include/fsdk_define.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070013#include "../include/javascript/IJavaScript.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080014#include "public/fpdf_ext.h"
Lei Zhang8241df72015-11-06 14:38:48 -080015#include "third_party/base/nonstd_unique_ptr.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070016
Tom Sepez37458412015-10-06 11:33:46 -070017#include "../include/fpdfxfa/fpdfxfa_app.h"
Bo Xufdc00a72014-10-28 23:03:33 -070018#include "../include/fpdfxfa/fpdfxfa_doc.h"
19#include "../include/fpdfxfa/fpdfxfa_page.h"
20#include "../include/fpdfxfa/fpdfxfa_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021
22#if _FX_OS_ == _FX_ANDROID_
23#include "time.h"
24#else
25#include <ctime>
26#endif
27
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028class CFX_SystemHandler : public IFX_SystemHandler {
29 public:
Lei Zhangbf60b292015-10-26 12:14:35 -070030 explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv)
31 : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070032 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070035 // IFX_SystemHandler
36 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
37 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
38 FX_BOOL IsSelectionImplemented() override;
39 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
40 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 return FALSE;
42 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070043 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
44 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
45 void SetCursor(int32_t nCursorType) override;
46 FX_HMENU CreatePopupMenu() override { return NULL; }
47 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
48 int32_t nIDNewItem,
49 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 return FALSE;
51 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070052 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
53 int32_t nIDItem,
54 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 return FALSE;
56 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070057 int32_t TrackPopupMenu(FX_HMENU hMenu,
58 int32_t x,
59 int32_t y,
60 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 return -1;
62 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070063 void DestroyMenu(FX_HMENU hMenu) override {}
64 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
65 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
66 CFX_ByteString sFontFaceName) override;
67 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
68 CFX_ByteString sFontFaceName,
69 uint8_t nCharset) override;
70 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
71 void KillTimer(int32_t nID) override;
72 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
74 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070075 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
77 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070078 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 return m_pEnv->FFI_IsALTKeyDown(nFlag);
80 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070081 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
83 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070084 FX_SYSTEMTIME GetLocalTime() override;
85 int32_t GetCharSet() override { return m_nCharSet; }
86 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087
88 private:
89 CPDFDoc_Environment* m_pEnv;
90 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091};
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
94 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095}
96
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
98 // g_pFormFillApp->FFI_Invalidate();
99 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
100 CPDFXFA_Page* pPage = NULL;
101 CPDFSDK_PageView* pPageView = NULL;
102 pPageView = pSDKAnnot->GetPageView();
103 pPage = pSDKAnnot->GetPDFXFAPage();
104 if (!pPage || !pPageView)
105 return;
106 CPDF_Matrix page2device;
107 pPageView->GetCurrentMatrix(page2device);
108 CPDF_Matrix device2page;
109 device2page.SetReverse(page2device);
110 FX_FLOAT left, top, right, bottom;
111 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
112 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
113 bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 CPDF_Rect rcPDF(left, bottom, right, top);
115 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
118 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
121 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
122 if (pFFL) {
123 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
124 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
125 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
126 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
Tom Sepez468e5892015-10-13 15:49:36 -0700127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
129 ASSERT(pAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700130 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 ASSERT(pPage);
132 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
133 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134}
135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136FX_BOOL CFX_SystemHandler::IsSelectionImplemented() {
137 if (m_pEnv) {
138 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
139 if (pInfo && pInfo->FFI_OutputSelectedRect)
140 return TRUE;
141 }
142 return FALSE;
143}
144
145CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) {
146 return "";
147}
148
149FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(
150 int32_t nCharset,
151 CFX_ByteString sFontFaceName) {
152 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
Lei Zhang122ee212015-10-21 17:07:24 -0700153 if (!pFontMgr)
154 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155
Lei Zhang122ee212015-10-21 17:07:24 -0700156 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
157 if (!pFontMapper)
158 return FALSE;
159
160 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
161 if (nSize == 0) {
162 pFontMapper->LoadInstalledFonts();
163 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
164 }
165
166 for (int i = 0; i < nSize; ++i) {
167 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
168 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 }
170
171 return FALSE;
172}
173
174static int CharSet2CP(int charset) {
175 if (charset == 128)
176 return 932;
177 if (charset == 134)
178 return 936;
179 if (charset == 129)
180 return 949;
181 if (charset == 136)
182 return 950;
183 return 0;
184}
185CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
186 CPDF_Document* pDoc,
187 CFX_ByteString sFontFaceName,
188 uint8_t nCharset) {
189 if (pDoc) {
190 CFX_Font* pFXFont = new CFX_Font();
191 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
192 FALSE);
193 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
194 delete pFXFont;
195 return pFont;
196 }
197
198 return NULL;
199}
200
201int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
202 TimerCallback lpTimerFunc) {
203 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
204}
205void CFX_SystemHandler::KillTimer(int32_t nID) {
206 m_pEnv->FFI_KillTimer(nID);
207}
208
209FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
210 return m_pEnv->FFI_GetLocalTime();
211}
212
213CPDFDoc_Environment::CPDFDoc_Environment(CPDFXFA_Document* pDoc,
214 FPDF_FORMFILLINFO* pFFinfo)
Oliver Changde6088d2015-10-30 16:30:28 -0700215 : m_pInfo(pFFinfo), m_pSDKDoc(NULL), m_pXFADoc(pDoc) {
216 m_pSysHandler.reset(new CFX_SystemHandler(this));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217}
218
219CPDFDoc_Environment::~CPDFDoc_Environment() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700221 if (pProvider->m_pEnvList.GetSize() == 0)
222 pProvider->SetJavaScriptInitialized(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223}
224
225int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
226 const FX_WCHAR* Title,
227 FX_UINT Type,
228 FX_UINT Icon) {
229 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
230 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
231 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
232 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
233 FPDF_WIDESTRING pTitle =
234 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
235 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
236 pTitle, Type, Icon);
237 bsMsg.ReleaseBuffer();
238 bsTitle.ReleaseBuffer();
239 return ret;
240 }
241 return -1;
242}
243
244int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
245 const FX_WCHAR* Title,
246 const FX_WCHAR* Default,
247 const FX_WCHAR* cLabel,
248 FPDF_BOOL bPassword,
249 void* response,
250 int length) {
251 if (m_pInfo && m_pInfo->m_pJsPlatform &&
252 m_pInfo->m_pJsPlatform->app_response) {
253 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
254 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
255 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
256 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
257 FPDF_WIDESTRING pQuestion =
258 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
259 FPDF_WIDESTRING pTitle =
260 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
261 FPDF_WIDESTRING pDefault =
262 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
263 FPDF_WIDESTRING pLabel =
264 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
265 int ret = m_pInfo->m_pJsPlatform->app_response(
266 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
267 response, length);
268 bsQuestion.ReleaseBuffer();
269 bsTitle.ReleaseBuffer();
270 bsDefault.ReleaseBuffer();
271 bsLabel.ReleaseBuffer();
272 return ret;
273 }
274 return -1;
275}
276
277CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
278 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
279 !m_pInfo->m_pJsPlatform->Field_browse) {
280 return L"";
281 }
282
283 const int nRequiredLen =
284 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
285 if (nRequiredLen <= 0)
286 return L"";
287
288 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
289 memset(pBuff.get(), 0, nRequiredLen);
290 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
291 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
292 if (nActualLen <= 0 || nActualLen > nRequiredLen)
293 return L"";
294
295 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
296 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
297 return wsRet;
298}
299
300CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
301 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
302 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
303 return L"";
304 }
305
306 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
307 m_pInfo->m_pJsPlatform, nullptr, 0);
308 if (nRequiredLen <= 0)
309 return L"";
310
311 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
312 memset(pBuff.get(), 0, nRequiredLen);
313 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
314 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
315 if (nActualLen <= 0 || nActualLen > nRequiredLen)
316 return L"";
317
318 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
319 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
320 return wsRet;
321}
322
323void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
324 int length,
325 const FX_WCHAR* URL) {
326 if (m_pInfo && m_pInfo->m_pJsPlatform &&
327 m_pInfo->m_pJsPlatform->Doc_submitForm) {
328 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
329 FPDF_WIDESTRING pDestination =
330 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
331 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
332 length, pDestination);
333 bsDestination.ReleaseBuffer();
334 }
335}
336
337void CPDFDoc_Environment::JS_docmailForm(void* mailData,
338 int length,
339 FPDF_BOOL bUI,
340 const FX_WCHAR* To,
341 const FX_WCHAR* Subject,
342 const FX_WCHAR* CC,
343 const FX_WCHAR* BCC,
344 const FX_WCHAR* Msg) {
345 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
346 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
347 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
348 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
349 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
350 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
351 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
352 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
353 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
354 FPDF_WIDESTRING pSubject =
355 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
356 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
357 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
358 bUI, pTo, pSubject, pCC, pBcc, pMsg);
359 bsTo.ReleaseBuffer();
360 bsCC.ReleaseBuffer();
361 bsBcc.ReleaseBuffer();
362 bsSubject.ReleaseBuffer();
363 bsMsg.ReleaseBuffer();
364 }
365}
366
Tom Sepezba038bc2015-10-08 12:03:00 -0700367IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700369 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 if (!m_pJSRuntime)
Tom Sepezba038bc2015-10-08 12:03:00 -0700371 m_pJSRuntime.reset(IJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700372 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373}
374
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
376 if (!m_pAnnotHandlerMgr)
Oliver Chang24752492015-10-30 16:08:20 -0700377 m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this));
378 return m_pAnnotHandlerMgr.get();
Bo Xufdc00a72014-10-28 23:03:33 -0700379}
380
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
382 if (!m_pActionHandler)
Oliver Chang24752492015-10-30 16:08:20 -0700383 m_pActionHandler.reset(new CPDFSDK_ActionHandler());
384 return m_pActionHandler.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700385}
386
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
388 if (!m_pIFormFiller)
Oliver Chang24752492015-10-30 16:08:20 -0700389 m_pIFormFiller.reset(new CFFL_IFormFiller(this));
390 return m_pIFormFiller.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700391}
392
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
394 CPDFDoc_Environment* pEnv)
395 : m_pDoc(pDoc),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 m_pFocusAnnot(nullptr),
397 m_pEnv(pEnv),
Oliver Chang972b78d2015-10-30 12:59:29 -0700398 m_bChangeMask(FALSE),
399 m_bBeingDestroyed(FALSE) {
400}
Bo Xufdc00a72014-10-28 23:03:33 -0700401
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402CPDFSDK_Document::~CPDFSDK_Document() {
Oliver Chang972b78d2015-10-30 12:59:29 -0700403 m_bBeingDestroyed = TRUE;
404
405 for (auto& it : m_pageMap)
406 it.second->KillFocusAnnotIfNeeded();
407
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408 for (auto& it : m_pageMap)
409 delete it.second;
410 m_pageMap.clear();
Bo Xufdc00a72014-10-28 23:03:33 -0700411}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700412
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
414 FX_BOOL ReNew) {
415 auto it = m_pageMap.find(pPDFXFAPage);
416 if (it != m_pageMap.end())
417 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700418
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 if (!ReNew)
420 return nullptr;
421
422 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
423 m_pageMap[pPDFXFAPage] = pPageView;
424 // Delay to load all the annotations, to avoid endless loop.
425 pPageView->LoadFXAnnots();
426 return pPageView;
427}
428
429CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
430 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
431 return pPage ? GetPageView(pPage, TRUE) : nullptr;
432}
433
434CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
435 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
436 if (!pTempPage)
437 return nullptr;
438
439 auto it = m_pageMap.find(pTempPage);
440 return it->second;
441}
442
443void CPDFSDK_Document::ProcJavascriptFun() {
444 CPDFXFA_Document* pPDFDoc = GetDocument();
445 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
446 int iCount = docJS.CountJSActions();
447 if (iCount < 1)
448 return;
449 for (int i = 0; i < iCount; i++) {
450 CFX_ByteString csJSName;
451 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
452 if (m_pEnv->GetActionHander())
453 m_pEnv->GetActionHander()->DoAction_JavaScript(
454 jsAction, CFX_WideString::FromLocal(csJSName), this);
455 }
456}
457
458FX_BOOL CPDFSDK_Document::ProcOpenAction() {
459 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700460 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461
462 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
463 if (!pRoot)
464 return FALSE;
465
466 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
467 if (!pOpenAction)
468 pOpenAction = pRoot->GetArray("OpenAction");
469
470 if (!pOpenAction)
471 return FALSE;
472
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400473 if (pOpenAction->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474 return TRUE;
475
Dan Sinclairf1251c12015-10-20 16:24:45 -0400476 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700477 CPDF_Action action(pDict);
478 if (m_pEnv->GetActionHander())
479 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
480 return TRUE;
481 }
482 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700483}
484
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
486 if (!m_pOccontent)
Oliver Chang24752492015-10-30 16:08:20 -0700487 m_pOccontent.reset(new CPDF_OCContext(m_pDoc->GetPDFDoc()));
488 return m_pOccontent.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489}
Bo Xufdc00a72014-10-28 23:03:33 -0700490
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
492 auto it = m_pageMap.find(pPDFXFAPage);
493 if (it == m_pageMap.end())
494 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700495
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 CPDFSDK_PageView* pPageView = it->second;
497 if (pPageView->IsLocked())
498 return;
499
Oliver Chang972b78d2015-10-30 12:59:29 -0700500 pPageView->KillFocusAnnotIfNeeded();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700501 delete pPageView;
502 m_pageMap.erase(it);
503}
504
505CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
506 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
507 if (!pTempPage)
508 return NULL;
509 return pTempPage;
510}
511
512CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
513 if (!m_pInterForm)
Oliver Chang24752492015-10-30 16:08:20 -0700514 m_pInterForm.reset(new CPDFSDK_InterForm(this));
515 return m_pInterForm.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516}
517
518void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
519 CPDFSDK_Annot* pAnnot) {
520 for (const auto& it : m_pageMap) {
521 CPDFSDK_PageView* pPageView = it.second;
522 if (pPageView != pSender) {
523 pPageView->UpdateView(pAnnot);
524 }
525 }
526}
527
528CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
529 return m_pFocusAnnot;
530}
531
532FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
Oliver Chang972b78d2015-10-30 12:59:29 -0700533 if (m_bBeingDestroyed)
534 return FALSE;
535
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536 if (m_pFocusAnnot == pAnnot)
537 return TRUE;
538
539 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
540
541 if (m_pFocusAnnot) {
542 if (!KillFocusAnnot(nFlag))
543 return FALSE;
544 }
545 CPDFSDK_PageView* pPageView = NULL;
546 if (pAnnot)
547 pPageView = pAnnot->GetPageView();
548 if (pAnnot && pPageView->IsValid()) {
549 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700550 if (!m_pFocusAnnot) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700551 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700552 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700553
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
555 return FALSE;
556 if (!m_pFocusAnnot) {
557 m_pFocusAnnot = pAnnot;
558 return TRUE;
559 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700560 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 }
562 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700563}
564
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
566 if (m_pFocusAnnot) {
567 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700568 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
569 m_pFocusAnnot = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700570
Lei Zhanga0601462015-10-30 14:54:37 -0700571 if (!pAnnotHandler->Annot_OnChangeFocus(nullptr, pFocusAnnot))
572 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700573
Lei Zhanga0601462015-10-30 14:54:37 -0700574 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
575 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
576 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
577 int nFieldType = pWidget->GetFieldType();
578 if (FIELDTYPE_TEXTFIELD == nFieldType ||
579 FIELDTYPE_COMBOBOX == nFieldType) {
580 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 }
Lei Zhanga0601462015-10-30 14:54:37 -0700583
584 if (!m_pFocusAnnot)
585 return TRUE;
586 } else {
587 m_pFocusAnnot = pFocusAnnot;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700588 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700589 }
590 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700591}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700592
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700593FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
594 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
595 return FALSE;
596 }
597
598 CPDFXFA_Page* pTempPage = NULL;
599 for (int i = nCount - 1; i >= 0; i--) {
600 pTempPage = GetPage(nStart + i);
601 if (pTempPage != NULL) {
602 ReMovePageView(pTempPage);
603 }
604 }
605 return TRUE;
606}
607
608void CPDFSDK_Document::OnCloseDocument() {
609 KillFocusAnnot();
610}
611
612FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
613 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
614 return dwPermissions & nFlag;
615}
616
Tom Sepezba038bc2015-10-08 12:03:00 -0700617IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 ASSERT(m_pEnv != NULL);
619 return m_pEnv->GetJSRuntime();
620}
621
622CFX_WideString CPDFSDK_Document::GetPath() {
623 ASSERT(m_pEnv != NULL);
624 return m_pEnv->JS_docGetFilePath();
625}
626
627CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
628 CPDFXFA_Page* page)
Lei Zhangbf60b292015-10-26 12:14:35 -0700629 : m_page(page),
630 m_pSDKDoc(pSDKDoc),
631 m_CaptureWidget(nullptr),
632 m_bEnterWidget(FALSE),
633 m_bExitWidget(FALSE),
634 m_bOnWidget(FALSE),
635 m_bValid(FALSE),
636 m_bLocked(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
638 if (pInterForm) {
639 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
640 if (page->GetPDFPage())
641 pPDFInterForm->FixPageFields(page->GetPDFPage());
642 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643}
644
645CPDFSDK_PageView::~CPDFSDK_PageView() {
646 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700647 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700648 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700649 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
Lei Zhangbf60b292015-10-26 12:14:35 -0700650 m_fxAnnotArray.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651
Lei Zhangbf60b292015-10-26 12:14:35 -0700652 m_pAnnotList.reset();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700653}
654
655void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
656 CPDF_Matrix* pUser2Device,
657 CPDF_RenderOptions* pOptions,
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800658 const FX_RECT& pClip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700659 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
661 CPDFXFA_Page* pPage = GetPDFXFAPage();
662
663 if (pPage == NULL)
664 return;
665
Tom Sepeza8a39e22015-10-12 15:47:07 -0700666#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
668 CFX_Graphics gs;
669 gs.Create(pDevice);
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800670 CFX_RectF rectClip;
671 rectClip.Set(static_cast<FX_FLOAT>(pClip.left),
672 static_cast<FX_FLOAT>(pClip.top),
673 static_cast<FX_FLOAT>(pClip.Width()),
674 static_cast<FX_FLOAT>(pClip.Height()));
675 gs.SetClipRect(rectClip);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700676 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
677 if (!pRenderContext)
678 return;
679 CXFA_RenderOptions renderOptions;
680 renderOptions.m_bHighlight = TRUE;
681 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
682 renderOptions);
683 pRenderContext->DoRender();
684 pRenderContext->StopRender();
685 pRenderContext->Release();
686 return;
687 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700688#endif // PDF_ENABLE_XFA
689
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 // for pdf/static xfa.
Lei Zhangbf60b292015-10-26 12:14:35 -0700691 CPDFSDK_AnnotIterator annotIterator(this, true);
692 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700694 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700695 }
696}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700697
Lei Zhang1b700c32015-10-30 23:55:35 -0700698const CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
699 FX_FLOAT pageY) {
700 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700701 CFX_FloatRect annotRect;
702 pAnnot->GetRect(annotRect);
703 if (annotRect.Contains(pageX, pageY))
704 return pAnnot;
705 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700706 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700707}
708
Lei Zhang1b700c32015-10-30 23:55:35 -0700709const CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
710 FX_FLOAT pageY) {
711 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700712 if (pAnnot->GetSubType() == "Widget") {
713 CFX_FloatRect annotRect;
714 pAnnot->GetRect(annotRect);
715 if (annotRect.Contains(pageX, pageY))
716 return pAnnot;
717 }
718 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700719 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700720}
721
722CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
723 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
725 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700726 CPDFSDK_AnnotIterator annotIterator(this, false);
727 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700728 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
729 if (rc.Contains(pageX, pageY))
730 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731 }
732
Lei Zhangbf60b292015-10-26 12:14:35 -0700733 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700734}
735
736CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
737 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700738 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
739 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700740 CPDFSDK_AnnotIterator annotIterator(this, false);
741 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700742 if (pSDKAnnot->GetType() == "Widget" ||
743 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
744 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
745 CPDF_Point point(pageX, pageY);
746 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
747 return pSDKAnnot;
748 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700749 }
750
Lei Zhangbf60b292015-10-26 12:14:35 -0700751 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700752}
753
Oliver Chang972b78d2015-10-30 12:59:29 -0700754void CPDFSDK_PageView::KillFocusAnnotIfNeeded() {
755 // if there is a focused annot on the page, we should kill the focus first.
756 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
757 auto it =
758 std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot);
759 if (it != m_fxAnnotArray.end())
760 KillFocusAnnot();
761 }
762}
763
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700764FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
765 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
766 if (pAnnotDic)
767 return pAnnotDic->KeyExist("AS");
768 return FALSE;
769}
770
771CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
772 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
773 ASSERT(pEnv);
774 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700775 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700776 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700777 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700778
Lei Zhangbf60b292015-10-26 12:14:35 -0700779 m_fxAnnotArray.push_back(pSDKAnnot);
780 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700781 return pSDKAnnot;
782}
783
784CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
785 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700786 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700787
788 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
789 if (pSDKAnnot)
790 return pSDKAnnot;
791
792 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700793 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700794 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700795 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700796 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700797
Lei Zhangbf60b292015-10-26 12:14:35 -0700798 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700799 return pSDKAnnot;
800}
801
802CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
803 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
804}
805
806CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
807 CPDF_Dictionary* pDict) {
808 return NULL;
809}
810
811FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
812 if (!pAnnot)
813 return FALSE;
814 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
815 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
816 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
817 return FALSE;
818
Lei Zhangbf60b292015-10-26 12:14:35 -0700819 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
820 if (it != m_fxAnnotArray.end())
821 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700823 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700824
825 return TRUE;
826}
827
828CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
829 if (m_page) {
830 return m_page->GetDocument()->GetPDFDoc();
831 }
832 return NULL;
833}
834
835CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
836 if (m_page) {
837 return m_page->GetPDFPage();
838 }
839
840 return NULL;
841}
842
Lei Zhang50218532015-10-30 14:03:43 -0700843size_t CPDFSDK_PageView::CountAnnots() const {
844 return m_fxAnnotArray.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845}
846
Lei Zhangbf60b292015-10-26 12:14:35 -0700847CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
848 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849}
850
851CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700852 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
853 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 return pAnnot;
855 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700856 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700857}
858CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700859 if (!hWidget)
860 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700861
Lei Zhangbf60b292015-10-26 12:14:35 -0700862 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700863 if (pAnnot->GetXFAWidget() == hWidget)
864 return pAnnot;
865 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700866 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700867}
868
869FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
870 FX_UINT nFlag) {
871 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
872 ASSERT(pEnv);
873 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
874 if (!pFXAnnot) {
875 KillFocusAnnot(nFlag);
Lei Zhanga0601462015-10-30 14:54:37 -0700876 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877 }
Lei Zhanga0601462015-10-30 14:54:37 -0700878
879 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
880 FX_BOOL bRet =
881 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
882 if (bRet)
883 SetFocusAnnot(pFXAnnot);
884 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700885}
886
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
888 FX_UINT nFlag) {
889 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
890 ASSERT(pEnv);
891 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
892 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700893
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
895
896 if (pFXAnnot == NULL)
897 return FALSE;
898
899 FX_BOOL bRet =
900 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
901 if (bRet) {
902 SetFocusAnnot(pFXAnnot);
903 }
904 return TRUE;
905}
906
907FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
908 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
909 ASSERT(pEnv);
910 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700911
912 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
913
914 if (pFXAnnot == NULL)
915 return FALSE;
916
917 FX_BOOL bRet =
918 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
919 if (bRet) {
920 SetFocusAnnot(pFXAnnot);
921 }
922 return TRUE;
923}
924
925FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
926 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
927 ASSERT(pEnv);
928 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700929 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
930 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
931 FX_BOOL bRet = FALSE;
932 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
933 // Last focus Annot gets a chance to handle the event.
934 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
935 }
Lei Zhanga0601462015-10-30 14:54:37 -0700936 if (pFXAnnot && !bRet)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938 return bRet;
939}
940
941FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
942 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
943 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
945 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
946 m_bExitWidget = TRUE;
947 m_bEnterWidget = FALSE;
948 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
949 }
950 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
951 m_bOnWidget = TRUE;
952 if (!m_bEnterWidget) {
953 m_bEnterWidget = TRUE;
954 m_bExitWidget = FALSE;
955 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
956 }
957 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
958 return TRUE;
959 }
960 if (m_bOnWidget) {
961 m_bOnWidget = FALSE;
962 m_bExitWidget = TRUE;
963 m_bEnterWidget = FALSE;
964 if (m_CaptureWidget) {
965 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
966 m_CaptureWidget = NULL;
967 }
968 }
969 return FALSE;
970}
971
972FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
973 double deltaY,
974 const CPDF_Point& point,
975 int nFlag) {
976 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700977 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
978 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700979 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
980 (int)deltaY, point);
981 }
982 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700983}
984
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700985FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
986 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700987 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700988 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700989 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
990 }
Bo Xufdc00a72014-10-28 23:03:33 -0700991
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700993}
994
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700995FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
996 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -0700997 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1000 }
1001 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001002}
1003
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001006}
1007
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011
1012 SetLock(TRUE);
1013 m_page->AddRef();
1014 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1015 IXFA_PageView* pageView = NULL;
1016 pageView = m_page->GetXFAPageView();
1017 ASSERT(pageView != NULL);
1018
1019 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1020 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1021 XFA_WIDGETFILTER_Viewable |
1022 XFA_WIDGETFILTER_AllType);
1023 if (!pWidgetHander) {
1024 m_page->Release();
1025 SetLock(FALSE);
1026 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001027 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028
Lei Zhangbf60b292015-10-26 12:14:35 -07001029 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001031 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033
Lei Zhangbf60b292015-10-26 12:14:35 -07001034 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001036 }
1037
1038 pWidgetHander->Release();
1039 } else {
1040 CPDF_Page* pPage = m_page->GetPDFPage();
1041 ASSERT(pPage != NULL);
1042
1043 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1044 // Disable the default AP construction.
1045 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001046 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1048
Lei Zhang1b700c32015-10-30 23:55:35 -07001049 const size_t nCount = m_pAnnotList->Count();
1050 for (size_t i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang50218532015-10-30 14:03:43 -07001052 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053
1054 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1055 if (!pAnnot)
1056 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001057 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058
1059 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1060 }
1061 }
1062 m_page->Release();
1063 SetLock(FALSE);
1064}
1065
1066void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1067 for (int i = 0; i < rects.GetSize(); i++) {
1068 CPDF_Rect rc = rects.GetAt(i);
1069 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1070 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1071 }
1072}
1073
1074void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1075 CPDF_Rect rcWindow = pAnnot->GetRect();
1076 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1077 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1078 rcWindow.bottom);
1079}
1080
1081int CPDFSDK_PageView::GetPageIndex() {
1082 if (m_page) {
1083 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1084 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1085 if (pDoc && pDic) {
1086 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1087 }
1088 }
1089 return -1;
1090}
1091
Lei Zhang1b700c32015-10-30 23:55:35 -07001092bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
Lei Zhangbf60b292015-10-26 12:14:35 -07001093 if (!p)
Lei Zhang1b700c32015-10-30 23:55:35 -07001094 return false;
Lei Zhangbf60b292015-10-26 12:14:35 -07001095
Lei Zhang1b700c32015-10-30 23:55:35 -07001096 const auto& annots = m_pAnnotList->All();
1097 return std::find(annots.begin(), annots.end(), p) != annots.end();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001098}
1099
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1101 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1102 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001103 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104
Lei Zhangbf60b292015-10-26 12:14:35 -07001105 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106 if (pAnnot == pFocusAnnot)
1107 return pAnnot;
1108 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001109 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001110}