blob: a119781a46fa47a82d0c19a395561ac684d21331 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhang60f507b2015-06-13 00:41:00 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez1ed8a212015-05-11 15:25:39 -07007#include "../../public/fpdf_ext.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07008#include "../include/fsdk_define.h"
Bo Xufdc00a72014-10-28 23:03:33 -07009#include "../include/fpdfxfa/fpdfxfa_doc.h"
10#include "../include/fpdfxfa/fpdfxfa_page.h"
11#include "../include/fpdfxfa/fpdfxfa_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012#include "../include/fsdk_mgr.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013#include "../include/formfiller/FFL_FormFiller.h"
14#include "../include/javascript/IJavaScript.h"
Bo Xufdc00a72014-10-28 23:03:33 -070015#include "../include/fpdfxfa/fpdfxfa_app.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
17#if _FX_OS_ == _FX_ANDROID_
18#include "time.h"
19#else
20#include <ctime>
21#endif
22
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023class CFX_SystemHandler : public IFX_SystemHandler {
24 public:
25 CFX_SystemHandler(CPDFDoc_Environment* pEnv) : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070026 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070029 // IFX_SystemHandler
30 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
31 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
32 FX_BOOL IsSelectionImplemented() override;
33 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
34 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 return FALSE;
36 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070037 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
38 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
39 void SetCursor(int32_t nCursorType) override;
40 FX_HMENU CreatePopupMenu() override { return NULL; }
41 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
42 int32_t nIDNewItem,
43 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 return FALSE;
45 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070046 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
47 int32_t nIDItem,
48 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 return FALSE;
50 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070051 int32_t TrackPopupMenu(FX_HMENU hMenu,
52 int32_t x,
53 int32_t y,
54 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 return -1;
56 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070057 void DestroyMenu(FX_HMENU hMenu) override {}
58 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
59 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
60 CFX_ByteString sFontFaceName) override;
61 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
62 CFX_ByteString sFontFaceName,
63 uint8_t nCharset) override;
64 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
65 void KillTimer(int32_t nID) override;
66 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
68 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070069 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
71 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070072 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 return m_pEnv->FFI_IsALTKeyDown(nFlag);
74 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070075 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
77 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070078 FX_SYSTEMTIME GetLocalTime() override;
79 int32_t GetCharSet() override { return m_nCharSet; }
80 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081
82 private:
83 CPDFDoc_Environment* m_pEnv;
84 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085};
86
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
88 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089}
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
92 // g_pFormFillApp->FFI_Invalidate();
93 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
94 CPDFXFA_Page* pPage = NULL;
95 CPDFSDK_PageView* pPageView = NULL;
96 pPageView = pSDKAnnot->GetPageView();
97 pPage = pSDKAnnot->GetPDFXFAPage();
98 if (!pPage || !pPageView)
99 return;
100 CPDF_Matrix page2device;
101 pPageView->GetCurrentMatrix(page2device);
102 CPDF_Matrix device2page;
103 device2page.SetReverse(page2device);
104 FX_FLOAT left, top, right, bottom;
105 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
106 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
107 bottom);
108 // m_pEnv->FFI_DeviceToPage(pPage, rect.left, rect.top, (double*)&left,
109 // (double*)&top);
110 // m_pEnv->FFI_DeviceToPage(pPage, rect.right, rect.bottom, (double*)&right,
111 // (double*)&bottom);
112 CPDF_Rect rcPDF(left, bottom, right, top);
113 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
116 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
119 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
120 if (pFFL) {
121 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
122 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
123 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
124 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
125 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
126 ASSERT(pAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700127 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 ASSERT(pPage);
129 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
130 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131}
132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133FX_BOOL CFX_SystemHandler::IsSelectionImplemented() {
134 if (m_pEnv) {
135 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
136 if (pInfo && pInfo->FFI_OutputSelectedRect)
137 return TRUE;
138 }
139 return FALSE;
140}
141
142CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) {
143 return "";
144}
145
146FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(
147 int32_t nCharset,
148 CFX_ByteString sFontFaceName) {
149 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
150 if (pFontMgr) {
151 CFX_FontMapper* pFontMapper = pFontMgr->m_pBuiltinMapper;
152 if (pFontMapper) {
153 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
154 if (nSize == 0) {
155 pFontMapper->LoadInstalledFonts();
156 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
157 }
158
159 for (int i = 0; i < nSize; i++) {
160 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
161 return TRUE;
162 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700163 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 }
165
166 return FALSE;
167}
168
169static int CharSet2CP(int charset) {
170 if (charset == 128)
171 return 932;
172 if (charset == 134)
173 return 936;
174 if (charset == 129)
175 return 949;
176 if (charset == 136)
177 return 950;
178 return 0;
179}
180CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
181 CPDF_Document* pDoc,
182 CFX_ByteString sFontFaceName,
183 uint8_t nCharset) {
184 if (pDoc) {
185 CFX_Font* pFXFont = new CFX_Font();
186 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
187 FALSE);
188 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
189 delete pFXFont;
190 return pFont;
191 }
192
193 return NULL;
194}
195
196int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
197 TimerCallback lpTimerFunc) {
198 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
199}
200void CFX_SystemHandler::KillTimer(int32_t nID) {
201 m_pEnv->FFI_KillTimer(nID);
202}
203
204FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
205 return m_pEnv->FFI_GetLocalTime();
206}
207
208CPDFDoc_Environment::CPDFDoc_Environment(CPDFXFA_Document* pDoc,
209 FPDF_FORMFILLINFO* pFFinfo)
210 : m_pAnnotHandlerMgr(NULL),
211 m_pActionHandler(NULL),
212 m_pJSRuntime(NULL),
213 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();
225 if (m_pJSRuntime && pProvider->GetRuntimeFactory())
226 pProvider->GetRuntimeFactory()->DeleteJSRuntime(m_pJSRuntime);
227
228 if (pProvider->m_pEnvList.GetSize() == 0) {
229 pProvider->ReleaseRuntime();
230 pProvider->InitRuntime(TRUE);
231 }
232
233 delete m_pSysHandler;
234 m_pSysHandler = NULL;
235
236 delete m_pAnnotHandlerMgr;
237 m_pAnnotHandlerMgr = NULL;
238
239 delete m_pActionHandler;
240 m_pActionHandler = NULL;
241}
242
243int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
244 const FX_WCHAR* Title,
245 FX_UINT Type,
246 FX_UINT Icon) {
247 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
248 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
249 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
250 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
251 FPDF_WIDESTRING pTitle =
252 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
253 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
254 pTitle, Type, Icon);
255 bsMsg.ReleaseBuffer();
256 bsTitle.ReleaseBuffer();
257 return ret;
258 }
259 return -1;
260}
261
262int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
263 const FX_WCHAR* Title,
264 const FX_WCHAR* Default,
265 const FX_WCHAR* cLabel,
266 FPDF_BOOL bPassword,
267 void* response,
268 int length) {
269 if (m_pInfo && m_pInfo->m_pJsPlatform &&
270 m_pInfo->m_pJsPlatform->app_response) {
271 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
272 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
273 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
274 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
275 FPDF_WIDESTRING pQuestion =
276 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
277 FPDF_WIDESTRING pTitle =
278 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
279 FPDF_WIDESTRING pDefault =
280 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
281 FPDF_WIDESTRING pLabel =
282 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
283 int ret = m_pInfo->m_pJsPlatform->app_response(
284 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
285 response, length);
286 bsQuestion.ReleaseBuffer();
287 bsTitle.ReleaseBuffer();
288 bsDefault.ReleaseBuffer();
289 bsLabel.ReleaseBuffer();
290 return ret;
291 }
292 return -1;
293}
294
295CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
296 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
297 !m_pInfo->m_pJsPlatform->Field_browse) {
298 return L"";
299 }
300
301 const int nRequiredLen =
302 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
303 if (nRequiredLen <= 0)
304 return L"";
305
306 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
307 memset(pBuff.get(), 0, nRequiredLen);
308 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
309 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
310 if (nActualLen <= 0 || nActualLen > nRequiredLen)
311 return L"";
312
313 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
314 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
315 return wsRet;
316}
317
318CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
319 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
320 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
321 return L"";
322 }
323
324 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
325 m_pInfo->m_pJsPlatform, nullptr, 0);
326 if (nRequiredLen <= 0)
327 return L"";
328
329 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
330 memset(pBuff.get(), 0, nRequiredLen);
331 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
332 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
333 if (nActualLen <= 0 || nActualLen > nRequiredLen)
334 return L"";
335
336 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
337 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
338 return wsRet;
339}
340
341void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
342 int length,
343 const FX_WCHAR* URL) {
344 if (m_pInfo && m_pInfo->m_pJsPlatform &&
345 m_pInfo->m_pJsPlatform->Doc_submitForm) {
346 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
347 FPDF_WIDESTRING pDestination =
348 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
349 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
350 length, pDestination);
351 bsDestination.ReleaseBuffer();
352 }
353}
354
355void CPDFDoc_Environment::JS_docmailForm(void* mailData,
356 int length,
357 FPDF_BOOL bUI,
358 const FX_WCHAR* To,
359 const FX_WCHAR* Subject,
360 const FX_WCHAR* CC,
361 const FX_WCHAR* BCC,
362 const FX_WCHAR* Msg) {
363 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
364 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
365 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
366 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
367 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
368 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
369 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
370 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
371 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
372 FPDF_WIDESTRING pSubject =
373 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
374 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
375 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
376 bUI, pTo, pSubject, pCC, pBcc, pMsg);
377 bsTo.ReleaseBuffer();
378 bsCC.ReleaseBuffer();
379 bsBcc.ReleaseBuffer();
380 bsSubject.ReleaseBuffer();
381 bsMsg.ReleaseBuffer();
382 }
383}
384
385IFXJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
386 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700387 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388 if (!m_pJSRuntime)
389 m_pJSRuntime =
390 CPDFXFA_App::GetInstance()->GetRuntimeFactory()->NewJSRuntime(this);
391 return m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392}
393
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
395 if (!m_pAnnotHandlerMgr)
396 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
397 return m_pAnnotHandlerMgr;
Bo Xufdc00a72014-10-28 23:03:33 -0700398}
399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
401 if (!m_pActionHandler)
402 m_pActionHandler = new CPDFSDK_ActionHandler(this);
403 return m_pActionHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700404}
405
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
407 if (!m_pIFormFiller)
408 m_pIFormFiller = new CFFL_IFormFiller(this);
409 return m_pIFormFiller;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700410}
411
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
413 CPDFDoc_Environment* pEnv)
414 : m_pDoc(pDoc),
415 m_pInterForm(nullptr),
416 m_pFocusAnnot(nullptr),
417 m_pEnv(pEnv),
418 m_pOccontent(nullptr),
419 m_bChangeMask(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700420
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421CPDFSDK_Document::~CPDFSDK_Document() {
422 for (auto& it : m_pageMap)
423 delete it.second;
424 m_pageMap.clear();
425
426 delete m_pInterForm;
427 m_pInterForm = nullptr;
428
429 delete m_pOccontent;
430 m_pOccontent = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700431}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700432
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
434 FX_BOOL ReNew) {
435 auto it = m_pageMap.find(pPDFXFAPage);
436 if (it != m_pageMap.end())
437 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700438
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 if (!ReNew)
440 return nullptr;
441
442 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
443 m_pageMap[pPDFXFAPage] = pPageView;
444 // Delay to load all the annotations, to avoid endless loop.
445 pPageView->LoadFXAnnots();
446 return pPageView;
447}
448
449CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
450 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
451 return pPage ? GetPageView(pPage, TRUE) : nullptr;
452}
453
454CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
455 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
456 if (!pTempPage)
457 return nullptr;
458
459 auto it = m_pageMap.find(pTempPage);
460 return it->second;
461}
462
463void CPDFSDK_Document::ProcJavascriptFun() {
464 CPDFXFA_Document* pPDFDoc = GetDocument();
465 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
466 int iCount = docJS.CountJSActions();
467 if (iCount < 1)
468 return;
469 for (int i = 0; i < iCount; i++) {
470 CFX_ByteString csJSName;
471 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
472 if (m_pEnv->GetActionHander())
473 m_pEnv->GetActionHander()->DoAction_JavaScript(
474 jsAction, CFX_WideString::FromLocal(csJSName), this);
475 }
476}
477
478FX_BOOL CPDFSDK_Document::ProcOpenAction() {
479 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700480 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481
482 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
483 if (!pRoot)
484 return FALSE;
485
486 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
487 if (!pOpenAction)
488 pOpenAction = pRoot->GetArray("OpenAction");
489
490 if (!pOpenAction)
491 return FALSE;
492
493 if (pOpenAction->GetType() == PDFOBJ_ARRAY)
494 return TRUE;
495
496 if (pOpenAction->GetType() == PDFOBJ_DICTIONARY) {
497 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pOpenAction;
498 CPDF_Action action(pDict);
499 if (m_pEnv->GetActionHander())
500 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
501 return TRUE;
502 }
503 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700504}
505
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
507 if (!m_pOccontent)
508 m_pOccontent = new CPDF_OCContext(m_pDoc->GetPDFDoc());
509 return m_pOccontent;
510}
Bo Xufdc00a72014-10-28 23:03:33 -0700511
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700512void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
513 auto it = m_pageMap.find(pPDFXFAPage);
514 if (it == m_pageMap.end())
515 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700516
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517 CPDFSDK_PageView* pPageView = it->second;
518 if (pPageView->IsLocked())
519 return;
520
521 delete pPageView;
522 m_pageMap.erase(it);
523}
524
525CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
526 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
527 if (!pTempPage)
528 return NULL;
529 return pTempPage;
530}
531
532CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
533 if (!m_pInterForm)
534 m_pInterForm = new CPDFSDK_InterForm(this);
535 return m_pInterForm;
536}
537
538void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
539 CPDFSDK_Annot* pAnnot) {
540 for (const auto& it : m_pageMap) {
541 CPDFSDK_PageView* pPageView = it.second;
542 if (pPageView != pSender) {
543 pPageView->UpdateView(pAnnot);
544 }
545 }
546}
547
548CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
549 return m_pFocusAnnot;
550}
551
552FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
553 if (m_pFocusAnnot == pAnnot)
554 return TRUE;
555
556 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
557
558 if (m_pFocusAnnot) {
559 if (!KillFocusAnnot(nFlag))
560 return FALSE;
561 }
562 CPDFSDK_PageView* pPageView = NULL;
563 if (pAnnot)
564 pPageView = pAnnot->GetPageView();
565 if (pAnnot && pPageView->IsValid()) {
566 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
567
568 if (pAnnotHandler && !m_pFocusAnnot) {
569 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700570 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700571
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
573 return FALSE;
574 if (!m_pFocusAnnot) {
575 m_pFocusAnnot = pAnnot;
576 return TRUE;
577 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700578 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579 }
580 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700581}
582
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
584 if (m_pFocusAnnot) {
585 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
586 if (pAnnotHandler) {
587 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
588 m_pFocusAnnot = NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700589
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590 if (!pAnnotHandler->Annot_OnChangeFocus(NULL, pFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700591 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700592
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700593 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
594 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
595 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
596 int nFieldType = pWidget->GetFieldType();
597 if (FIELDTYPE_TEXTFIELD == nFieldType ||
598 FIELDTYPE_COMBOBOX == nFieldType)
599 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
600 }
601
602 if (!m_pFocusAnnot)
603 return TRUE;
604 } else {
605 m_pFocusAnnot = pFocusAnnot;
606 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700607 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608 }
609 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700610}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700611
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700612FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
613 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
614 return FALSE;
615 }
616
617 CPDFXFA_Page* pTempPage = NULL;
618 for (int i = nCount - 1; i >= 0; i--) {
619 pTempPage = GetPage(nStart + i);
620 if (pTempPage != NULL) {
621 ReMovePageView(pTempPage);
622 }
623 }
624 return TRUE;
625}
626
627void CPDFSDK_Document::OnCloseDocument() {
628 KillFocusAnnot();
629}
630
631FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
632 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
633 return dwPermissions & nFlag;
634}
635
636IFXJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
637 ASSERT(m_pEnv != NULL);
638 return m_pEnv->GetJSRuntime();
639}
640
641CFX_WideString CPDFSDK_Document::GetPath() {
642 ASSERT(m_pEnv != NULL);
643 return m_pEnv->JS_docGetFilePath();
644}
645
646CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
647 CPDFXFA_Page* page)
648 : m_page(page), m_pSDKDoc(pSDKDoc) {
649 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
650 if (pInterForm) {
651 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
652 if (page->GetPDFPage())
653 pPDFInterForm->FixPageFields(page->GetPDFPage());
654 }
655 m_fxAnnotArray.RemoveAll();
656
657 m_bEnterWidget = FALSE;
658 m_bExitWidget = FALSE;
659 m_bOnWidget = FALSE;
660 m_CaptureWidget = NULL;
661 m_bValid = FALSE;
662 m_bLocked = FALSE;
663 m_pAnnotList = NULL;
664}
665
666CPDFSDK_PageView::~CPDFSDK_PageView() {
667 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
668 int nAnnotCount = m_fxAnnotArray.GetSize();
669 for (int i = 0; i < nAnnotCount; i++) {
670 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
671 // if there is a focused annot on the page, we should kill the focus first.
672 if (pAnnot == m_pSDKDoc->GetFocusAnnot())
673 KillFocusAnnot();
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700674 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
675 ASSERT(pAnnotHandlerMgr);
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;
689 // m_pAnnotList->DisplayAnnots(m_page, pDevice, pUser2Device, FALSE,
690 // pOptions);
691 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
692 CPDFXFA_Page* pPage = GetPDFXFAPage();
693
694 if (pPage == NULL)
695 return;
696
697 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
698 CFX_Graphics gs;
699 gs.Create(pDevice);
700 if (pClip) {
701 CFX_RectF rectClip;
702 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
703 static_cast<FX_FLOAT>(pClip->top),
704 static_cast<FX_FLOAT>(pClip->Width()),
705 static_cast<FX_FLOAT>(pClip->Height()));
706 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700707 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
709 if (!pRenderContext)
710 return;
711 CXFA_RenderOptions renderOptions;
712 renderOptions.m_bHighlight = TRUE;
713 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
714 renderOptions);
715 pRenderContext->DoRender();
716 pRenderContext->StopRender();
717 pRenderContext->Release();
718 return;
719 }
720 // for pdf/static xfa.
721 CPDFSDK_AnnotIterator annotIterator(this, TRUE);
722 CPDFSDK_Annot* pSDKAnnot = NULL;
723 int index = -1;
724 pSDKAnnot = annotIterator.Next(index);
725 while (pSDKAnnot) {
726 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
727 ASSERT(pAnnotHandlerMgr);
728 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
729 pSDKAnnot = annotIterator.Next(index);
730 }
731}
732CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
733 FX_FLOAT pageY) {
734 int nCount = m_pAnnotList->Count();
735 for (int i = 0; i < nCount; i++) {
736 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
737 CFX_FloatRect annotRect;
738 pAnnot->GetRect(annotRect);
739 if (annotRect.Contains(pageX, pageY))
740 return pAnnot;
741 }
742 return NULL;
743}
744
745CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
746 FX_FLOAT pageY) {
747 int nCount = m_pAnnotList->Count();
748 for (int i = 0; i < nCount; i++) {
749 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
750 if (pAnnot->GetSubType() == "Widget") {
751 CFX_FloatRect annotRect;
752 pAnnot->GetRect(annotRect);
753 if (annotRect.Contains(pageX, pageY))
754 return pAnnot;
755 }
756 }
757 return NULL;
758}
759
760CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
761 FX_FLOAT pageY) {
762 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
763 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
764 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
765 CPDFSDK_Annot* pSDKAnnot = NULL;
766 int index = -1;
767 pSDKAnnot = annotIterator.Next(index);
768 while (pSDKAnnot) {
769 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
770 if (rc.Contains(pageX, pageY))
771 return pSDKAnnot;
772 pSDKAnnot = annotIterator.Next(index);
773 }
774
775 return NULL;
776}
777
778CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
779 FX_FLOAT pageY) {
780 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
781 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
782 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
783 CPDFSDK_Annot* pSDKAnnot = NULL;
784 int index = -1;
785 pSDKAnnot = annotIterator.Next(index);
786 while (pSDKAnnot) {
787 if (pSDKAnnot->GetType() == "Widget" ||
788 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
789 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
790 CPDF_Point point(pageX, pageY);
791 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
792 return pSDKAnnot;
793 }
794 pSDKAnnot = annotIterator.Next(index);
795 }
796
797 return NULL;
798}
799
800FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
801 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
802 if (pAnnotDic)
803 return pAnnotDic->KeyExist("AS");
804 return FALSE;
805}
806
807CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
808 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
809 ASSERT(pEnv);
810 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
811
812 CPDFSDK_Annot* pSDKAnnot = NULL;
813
814 if (pAnnotHandler) {
815 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
816 }
817 if (!pSDKAnnot)
818 return NULL;
819
820 m_fxAnnotArray.Add(pSDKAnnot);
821
822 if (pAnnotHandler) {
823 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
824 }
825
826 return pSDKAnnot;
827}
828
829CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
830 if (!pPDFAnnot)
831 return NULL;
832
833 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
834 if (pSDKAnnot)
835 return pSDKAnnot;
836
837 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
838 ASSERT(pEnv);
839 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
840
841 pSDKAnnot = NULL;
842
843 if (pAnnotHandler) {
844 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
845 }
846 if (!pSDKAnnot)
847 return NULL;
848
849 m_fxAnnotArray.Add(pSDKAnnot);
850
851 return pSDKAnnot;
852}
853
854CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
855 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
856}
857
858CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
859 CPDF_Dictionary* pDict) {
860 return NULL;
861}
862
863FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
864 if (!pAnnot)
865 return FALSE;
866 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
867 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
868 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
869 return FALSE;
870
871 int index = m_fxAnnotArray.Find(pAnnot);
872 m_fxAnnotArray.RemoveAt(index);
873 if (m_CaptureWidget == pAnnot)
874 m_CaptureWidget = NULL;
875
876 return TRUE;
877}
878
879CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
880 if (m_page) {
881 return m_page->GetDocument()->GetPDFDoc();
882 }
883 return NULL;
884}
885
886CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
887 if (m_page) {
888 return m_page->GetPDFPage();
889 }
890
891 return NULL;
892}
893
894int CPDFSDK_PageView::CountAnnots() {
895 return m_fxAnnotArray.GetSize();
896}
897
898CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(int nIndex) {
899 int nCount = m_fxAnnotArray.GetSize();
900 if (nIndex < 0 || nIndex >= nCount) {
901 return NULL;
902 }
903
904 return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
905}
906
907CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
908 int nCount = m_fxAnnotArray.GetSize();
909 for (int i = 0; i < nCount; i++) {
910 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
911 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
912 return pAnnot;
913 }
914 return NULL;
915}
916CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
917 if (hWidget == NULL)
918 return NULL;
919 int annotCount = m_fxAnnotArray.GetSize();
920
921 for (int i = 0; i < annotCount; i++) {
922 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
923 if (pAnnot->GetXFAWidget() == hWidget)
924 return pAnnot;
925 }
926 return NULL;
927}
928
929FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
930 FX_UINT nFlag) {
931 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
932 ASSERT(pEnv);
933 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
934 if (!pFXAnnot) {
935 KillFocusAnnot(nFlag);
936 } else {
937 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
938 ASSERT(pAnnotHandlerMgr);
939
940 FX_BOOL bRet =
941 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
942 if (bRet) {
943 SetFocusAnnot(pFXAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700944 }
945 return bRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700946 }
947 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700948}
949
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700950FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
951 FX_UINT nFlag) {
952 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
953 ASSERT(pEnv);
954 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
955 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700956
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
958
959 if (pFXAnnot == NULL)
960 return FALSE;
961
962 FX_BOOL bRet =
963 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
964 if (bRet) {
965 SetFocusAnnot(pFXAnnot);
966 }
967 return TRUE;
968}
969
970FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
971 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
972 ASSERT(pEnv);
973 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
974 ASSERT(pAnnotHandlerMgr);
975
976 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
977
978 if (pFXAnnot == NULL)
979 return FALSE;
980
981 FX_BOOL bRet =
982 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
983 if (bRet) {
984 SetFocusAnnot(pFXAnnot);
985 }
986 return TRUE;
987}
988
989FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
990 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
991 ASSERT(pEnv);
992 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
993 ASSERT(pAnnotHandlerMgr);
994 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
995 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
996 FX_BOOL bRet = FALSE;
997 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
998 // Last focus Annot gets a chance to handle the event.
999 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
1000 }
1001 if (pFXAnnot && !bRet) {
1002 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
1003 return bRet;
1004 }
1005 return bRet;
1006}
1007
1008FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
1009 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1010 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1011 ASSERT(pAnnotHandlerMgr);
1012 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
1013 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
1014 m_bExitWidget = TRUE;
1015 m_bEnterWidget = FALSE;
1016 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1017 }
1018 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
1019 m_bOnWidget = TRUE;
1020 if (!m_bEnterWidget) {
1021 m_bEnterWidget = TRUE;
1022 m_bExitWidget = FALSE;
1023 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
1024 }
1025 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
1026 return TRUE;
1027 }
1028 if (m_bOnWidget) {
1029 m_bOnWidget = FALSE;
1030 m_bExitWidget = TRUE;
1031 m_bEnterWidget = FALSE;
1032 if (m_CaptureWidget) {
1033 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1034 m_CaptureWidget = NULL;
1035 }
1036 }
1037 return FALSE;
1038}
1039
1040FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1041 double deltaY,
1042 const CPDF_Point& point,
1043 int nFlag) {
1044 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001045 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1046 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1047 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001048 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1049 (int)deltaY, point);
1050 }
1051 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001052}
1053
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001054FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1055 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001056 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001057 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058 ASSERT(pAnnotHandlerMgr);
1059 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1060 }
Bo Xufdc00a72014-10-28 23:03:33 -07001061
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001062 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001063}
1064
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001065FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1066 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001067 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1069 ASSERT(pAnnotHandlerMgr);
1070 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1071 }
1072 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001073}
1074
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
1076 // if(CPDFSDK_Annot* pAnnot = GetFocusAnnot())
1077 // {
1078 // CFFL_IFormFiller* pIFormFiller = g_pFormFillApp->GetIFormFiller();
1079 // return pIFormFiller->OnKeyUp(pAnnot, nKeyCode, nFlag);
1080 // }
1081 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001082}
1083
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084extern void CheckUnSupportAnnot(CPDF_Document* pDoc, CPDF_Annot* pPDFAnnot);
1085
1086void CPDFSDK_PageView::LoadFXAnnots() {
1087 ASSERT(m_page != NULL);
1088
1089 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1090 ASSERT(pEnv != NULL);
1091
1092 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1093 ASSERT(pAnnotHandlerMgr != NULL);
1094
1095 SetLock(TRUE);
1096 m_page->AddRef();
1097 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1098 IXFA_PageView* pageView = NULL;
1099 pageView = m_page->GetXFAPageView();
1100 ASSERT(pageView != NULL);
1101
1102 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1103 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1104 XFA_WIDGETFILTER_Viewable |
1105 XFA_WIDGETFILTER_AllType);
1106 if (!pWidgetHander) {
1107 m_page->Release();
1108 SetLock(FALSE);
1109 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001110 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111
1112 IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext();
1113 while (pXFAAnnot) {
1114 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
1115 if (!pAnnot) {
1116 pXFAAnnot = pWidgetHander->MoveToNext();
1117 continue;
1118 }
1119 m_fxAnnotArray.Add(pAnnot);
1120
1121 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1122
1123 pXFAAnnot = pWidgetHander->MoveToNext();
1124 }
1125
1126 pWidgetHander->Release();
1127 } else {
1128 CPDF_Page* pPage = m_page->GetPDFPage();
1129 ASSERT(pPage != NULL);
1130
1131 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1132 // Disable the default AP construction.
1133 CPDF_InterForm::EnableUpdateAP(FALSE);
1134 m_pAnnotList = new CPDF_AnnotList(pPage);
1135 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1136
1137 int nCount = m_pAnnotList->Count();
1138 for (int i = 0; i < nCount; i++) {
1139 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1140 CPDF_Document* pDoc = GetPDFDocument();
1141
1142 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1143
1144 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1145 if (!pAnnot)
1146 continue;
1147 m_fxAnnotArray.Add(pAnnot);
1148
1149 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1150 }
1151 }
1152 m_page->Release();
1153 SetLock(FALSE);
1154}
1155
1156void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1157 for (int i = 0; i < rects.GetSize(); i++) {
1158 CPDF_Rect rc = rects.GetAt(i);
1159 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1160 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1161 }
1162}
1163
1164void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1165 CPDF_Rect rcWindow = pAnnot->GetRect();
1166 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1167 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1168 rcWindow.bottom);
1169}
1170
1171int CPDFSDK_PageView::GetPageIndex() {
1172 if (m_page) {
1173 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1174 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1175 if (pDoc && pDic) {
1176 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1177 }
1178 }
1179 return -1;
1180}
1181
1182FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) {
1183 if (p == NULL)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001184 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001185 int iCount = m_pAnnotList->Count();
1186 for (int i = 0; i < iCount; i++) {
1187 if (m_pAnnotList->GetAt(i) == p)
1188 return TRUE;
1189 }
1190 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001191}
1192
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001193CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1194 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1195 if (!pFocusAnnot)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001196 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001197
1198 for (int i = 0; i < m_fxAnnotArray.GetSize(); i++) {
1199 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1200 if (pAnnot == pFocusAnnot)
1201 return pAnnot;
1202 }
1203 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001204}