blob: c6395bfc832af4910229828ea6a9798bf3ee1c8f [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhang60f507b2015-06-13 00:41:00 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez1ed8a212015-05-11 15:25:39 -07007#include "../../public/fpdf_ext.h"
Tom Sepez37458412015-10-06 11:33:46 -07008#include "../include/formfiller/FFL_FormFiller.h"
9#include "../include/fpdfxfa/fpdfxfa_app.h"
Bo Xufdc00a72014-10-28 23:03:33 -070010#include "../include/fpdfxfa/fpdfxfa_doc.h"
11#include "../include/fpdfxfa/fpdfxfa_page.h"
12#include "../include/fpdfxfa/fpdfxfa_util.h"
Tom Sepez37458412015-10-06 11:33:46 -070013#include "../include/fsdk_define.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014#include "../include/fsdk_mgr.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015#include "../include/javascript/IJavaScript.h"
16
17#if _FX_OS_ == _FX_ANDROID_
18#include "time.h"
19#else
20#include <ctime>
21#endif
22
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023class CFX_SystemHandler : public IFX_SystemHandler {
24 public:
25 CFX_SystemHandler(CPDFDoc_Environment* pEnv) : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070026 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070029 // IFX_SystemHandler
30 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
31 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
32 FX_BOOL IsSelectionImplemented() override;
33 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
34 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 return FALSE;
36 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070037 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
38 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
39 void SetCursor(int32_t nCursorType) override;
40 FX_HMENU CreatePopupMenu() override { return NULL; }
41 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
42 int32_t nIDNewItem,
43 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 return FALSE;
45 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070046 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
47 int32_t nIDItem,
48 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 return FALSE;
50 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070051 int32_t TrackPopupMenu(FX_HMENU hMenu,
52 int32_t x,
53 int32_t y,
54 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 return -1;
56 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070057 void DestroyMenu(FX_HMENU hMenu) override {}
58 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
59 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
60 CFX_ByteString sFontFaceName) override;
61 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
62 CFX_ByteString sFontFaceName,
63 uint8_t nCharset) override;
64 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
65 void KillTimer(int32_t nID) override;
66 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
68 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070069 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
71 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070072 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 return m_pEnv->FFI_IsALTKeyDown(nFlag);
74 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070075 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
77 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070078 FX_SYSTEMTIME GetLocalTime() override;
79 int32_t GetCharSet() override { return m_nCharSet; }
80 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081
82 private:
83 CPDFDoc_Environment* m_pEnv;
84 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085};
86
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
88 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089}
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
92 // g_pFormFillApp->FFI_Invalidate();
93 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
94 CPDFXFA_Page* pPage = NULL;
95 CPDFSDK_PageView* pPageView = NULL;
96 pPageView = pSDKAnnot->GetPageView();
97 pPage = pSDKAnnot->GetPDFXFAPage();
98 if (!pPage || !pPageView)
99 return;
100 CPDF_Matrix page2device;
101 pPageView->GetCurrentMatrix(page2device);
102 CPDF_Matrix device2page;
103 device2page.SetReverse(page2device);
104 FX_FLOAT left, top, right, bottom;
105 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
106 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
107 bottom);
108 // m_pEnv->FFI_DeviceToPage(pPage, rect.left, rect.top, (double*)&left,
109 // (double*)&top);
110 // m_pEnv->FFI_DeviceToPage(pPage, rect.right, rect.bottom, (double*)&right,
111 // (double*)&bottom);
112 CPDF_Rect rcPDF(left, bottom, right, top);
113 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
116 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
119 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
120 if (pFFL) {
121 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
122 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
123 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
124 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
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),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 m_pInfo(pFFinfo),
213 m_pSDKDoc(NULL),
214 m_pXFADoc(pDoc),
215 m_pIFormFiller(NULL) {
216 m_pSysHandler = new CFX_SystemHandler(this);
217}
218
219CPDFDoc_Environment::~CPDFDoc_Environment() {
220 delete m_pIFormFiller;
221 m_pIFormFiller = NULL;
222
223 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700224 if (pProvider->m_pEnvList.GetSize() == 0)
225 pProvider->SetJavaScriptInitialized(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226
227 delete m_pSysHandler;
228 m_pSysHandler = NULL;
229
230 delete m_pAnnotHandlerMgr;
231 m_pAnnotHandlerMgr = NULL;
232
233 delete m_pActionHandler;
234 m_pActionHandler = NULL;
235}
236
237int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
238 const FX_WCHAR* Title,
239 FX_UINT Type,
240 FX_UINT Icon) {
241 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
242 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
243 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
244 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
245 FPDF_WIDESTRING pTitle =
246 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
247 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
248 pTitle, Type, Icon);
249 bsMsg.ReleaseBuffer();
250 bsTitle.ReleaseBuffer();
251 return ret;
252 }
253 return -1;
254}
255
256int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
257 const FX_WCHAR* Title,
258 const FX_WCHAR* Default,
259 const FX_WCHAR* cLabel,
260 FPDF_BOOL bPassword,
261 void* response,
262 int length) {
263 if (m_pInfo && m_pInfo->m_pJsPlatform &&
264 m_pInfo->m_pJsPlatform->app_response) {
265 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
266 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
267 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
268 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
269 FPDF_WIDESTRING pQuestion =
270 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
271 FPDF_WIDESTRING pTitle =
272 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
273 FPDF_WIDESTRING pDefault =
274 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
275 FPDF_WIDESTRING pLabel =
276 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
277 int ret = m_pInfo->m_pJsPlatform->app_response(
278 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
279 response, length);
280 bsQuestion.ReleaseBuffer();
281 bsTitle.ReleaseBuffer();
282 bsDefault.ReleaseBuffer();
283 bsLabel.ReleaseBuffer();
284 return ret;
285 }
286 return -1;
287}
288
289CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
290 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
291 !m_pInfo->m_pJsPlatform->Field_browse) {
292 return L"";
293 }
294
295 const int nRequiredLen =
296 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
297 if (nRequiredLen <= 0)
298 return L"";
299
300 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
301 memset(pBuff.get(), 0, nRequiredLen);
302 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
303 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
304 if (nActualLen <= 0 || nActualLen > nRequiredLen)
305 return L"";
306
307 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
308 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
309 return wsRet;
310}
311
312CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
313 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
314 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
315 return L"";
316 }
317
318 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
319 m_pInfo->m_pJsPlatform, nullptr, 0);
320 if (nRequiredLen <= 0)
321 return L"";
322
323 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
324 memset(pBuff.get(), 0, nRequiredLen);
325 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
326 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
327 if (nActualLen <= 0 || nActualLen > nRequiredLen)
328 return L"";
329
330 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
331 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
332 return wsRet;
333}
334
335void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
336 int length,
337 const FX_WCHAR* URL) {
338 if (m_pInfo && m_pInfo->m_pJsPlatform &&
339 m_pInfo->m_pJsPlatform->Doc_submitForm) {
340 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
341 FPDF_WIDESTRING pDestination =
342 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
343 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
344 length, pDestination);
345 bsDestination.ReleaseBuffer();
346 }
347}
348
349void CPDFDoc_Environment::JS_docmailForm(void* mailData,
350 int length,
351 FPDF_BOOL bUI,
352 const FX_WCHAR* To,
353 const FX_WCHAR* Subject,
354 const FX_WCHAR* CC,
355 const FX_WCHAR* BCC,
356 const FX_WCHAR* Msg) {
357 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
358 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
359 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
360 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
361 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
362 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
363 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
364 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
365 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
366 FPDF_WIDESTRING pSubject =
367 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
368 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
369 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
370 bUI, pTo, pSubject, pCC, pBcc, pMsg);
371 bsTo.ReleaseBuffer();
372 bsCC.ReleaseBuffer();
373 bsBcc.ReleaseBuffer();
374 bsSubject.ReleaseBuffer();
375 bsMsg.ReleaseBuffer();
376 }
377}
378
379IFXJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
380 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700381 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 if (!m_pJSRuntime)
Tom Sepez37458412015-10-06 11:33:46 -0700383 m_pJSRuntime.reset(IFXJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700384 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700385}
386
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
388 if (!m_pAnnotHandlerMgr)
389 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
390 return m_pAnnotHandlerMgr;
Bo Xufdc00a72014-10-28 23:03:33 -0700391}
392
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
394 if (!m_pActionHandler)
Tom Sepez3b5f1242015-09-01 14:06:55 -0700395 m_pActionHandler = new CPDFSDK_ActionHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 return m_pActionHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700397}
398
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
400 if (!m_pIFormFiller)
401 m_pIFormFiller = new CFFL_IFormFiller(this);
402 return m_pIFormFiller;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700403}
404
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
406 CPDFDoc_Environment* pEnv)
407 : m_pDoc(pDoc),
408 m_pInterForm(nullptr),
409 m_pFocusAnnot(nullptr),
410 m_pEnv(pEnv),
411 m_pOccontent(nullptr),
412 m_bChangeMask(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700413
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414CPDFSDK_Document::~CPDFSDK_Document() {
415 for (auto& it : m_pageMap)
416 delete it.second;
417 m_pageMap.clear();
418
419 delete m_pInterForm;
420 m_pInterForm = nullptr;
421
422 delete m_pOccontent;
423 m_pOccontent = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700424}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700425
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
427 FX_BOOL ReNew) {
428 auto it = m_pageMap.find(pPDFXFAPage);
429 if (it != m_pageMap.end())
430 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700431
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432 if (!ReNew)
433 return nullptr;
434
435 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
436 m_pageMap[pPDFXFAPage] = pPageView;
437 // Delay to load all the annotations, to avoid endless loop.
438 pPageView->LoadFXAnnots();
439 return pPageView;
440}
441
442CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
443 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
444 return pPage ? GetPageView(pPage, TRUE) : nullptr;
445}
446
447CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
448 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
449 if (!pTempPage)
450 return nullptr;
451
452 auto it = m_pageMap.find(pTempPage);
453 return it->second;
454}
455
456void CPDFSDK_Document::ProcJavascriptFun() {
457 CPDFXFA_Document* pPDFDoc = GetDocument();
458 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
459 int iCount = docJS.CountJSActions();
460 if (iCount < 1)
461 return;
462 for (int i = 0; i < iCount; i++) {
463 CFX_ByteString csJSName;
464 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
465 if (m_pEnv->GetActionHander())
466 m_pEnv->GetActionHander()->DoAction_JavaScript(
467 jsAction, CFX_WideString::FromLocal(csJSName), this);
468 }
469}
470
471FX_BOOL CPDFSDK_Document::ProcOpenAction() {
472 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700473 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474
475 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
476 if (!pRoot)
477 return FALSE;
478
479 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
480 if (!pOpenAction)
481 pOpenAction = pRoot->GetArray("OpenAction");
482
483 if (!pOpenAction)
484 return FALSE;
485
486 if (pOpenAction->GetType() == PDFOBJ_ARRAY)
487 return TRUE;
488
489 if (pOpenAction->GetType() == PDFOBJ_DICTIONARY) {
490 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pOpenAction;
491 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
629IFXJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
630 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;
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) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001077}
1078
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079void CPDFSDK_PageView::LoadFXAnnots() {
1080 ASSERT(m_page != NULL);
1081
1082 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1083 ASSERT(pEnv != NULL);
1084
1085 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1086 ASSERT(pAnnotHandlerMgr != NULL);
1087
1088 SetLock(TRUE);
1089 m_page->AddRef();
1090 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1091 IXFA_PageView* pageView = NULL;
1092 pageView = m_page->GetXFAPageView();
1093 ASSERT(pageView != NULL);
1094
1095 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1096 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1097 XFA_WIDGETFILTER_Viewable |
1098 XFA_WIDGETFILTER_AllType);
1099 if (!pWidgetHander) {
1100 m_page->Release();
1101 SetLock(FALSE);
1102 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001103 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001104
1105 IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext();
1106 while (pXFAAnnot) {
1107 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
1108 if (!pAnnot) {
1109 pXFAAnnot = pWidgetHander->MoveToNext();
1110 continue;
1111 }
1112 m_fxAnnotArray.Add(pAnnot);
1113
1114 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1115
1116 pXFAAnnot = pWidgetHander->MoveToNext();
1117 }
1118
1119 pWidgetHander->Release();
1120 } else {
1121 CPDF_Page* pPage = m_page->GetPDFPage();
1122 ASSERT(pPage != NULL);
1123
1124 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1125 // Disable the default AP construction.
1126 CPDF_InterForm::EnableUpdateAP(FALSE);
1127 m_pAnnotList = new CPDF_AnnotList(pPage);
1128 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1129
1130 int nCount = m_pAnnotList->Count();
1131 for (int i = 0; i < nCount; i++) {
1132 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1133 CPDF_Document* pDoc = GetPDFDocument();
1134
1135 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1136
1137 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1138 if (!pAnnot)
1139 continue;
1140 m_fxAnnotArray.Add(pAnnot);
1141
1142 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1143 }
1144 }
1145 m_page->Release();
1146 SetLock(FALSE);
1147}
1148
1149void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1150 for (int i = 0; i < rects.GetSize(); i++) {
1151 CPDF_Rect rc = rects.GetAt(i);
1152 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1153 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1154 }
1155}
1156
1157void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1158 CPDF_Rect rcWindow = pAnnot->GetRect();
1159 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1160 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1161 rcWindow.bottom);
1162}
1163
1164int CPDFSDK_PageView::GetPageIndex() {
1165 if (m_page) {
1166 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1167 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1168 if (pDoc && pDic) {
1169 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1170 }
1171 }
1172 return -1;
1173}
1174
1175FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) {
1176 if (p == NULL)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001177 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178 int iCount = m_pAnnotList->Count();
1179 for (int i = 0; i < iCount; i++) {
1180 if (m_pAnnotList->GetAt(i) == p)
1181 return TRUE;
1182 }
1183 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001184}
1185
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001186CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1187 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1188 if (!pFocusAnnot)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001189 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001190
1191 for (int i = 0; i < m_fxAnnotArray.GetSize(); i++) {
1192 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
1193 if (pAnnot == pFocusAnnot)
1194 return pAnnot;
1195 }
1196 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001197}