blob: 4a55622e577c684067c8e498f643e5499adb5ca9 [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)
Tom Sepez3b5f1242015-09-01 14:06:55 -0700402 m_pActionHandler = new CPDFSDK_ActionHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 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() {
Tom Sepez396e8722015-09-09 10:16:08 -0700667 // if there is a focused annot on the page, we should kill the focus first.
668 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
669 for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
670 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
671 if (pAnnot == focusedAnnot) {
672 KillFocusAnnot();
673 break;
674 }
675 }
676 }
677
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700678 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700679 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
680 ASSERT(pAnnotHandlerMgr);
681 for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700682 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700683 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
684 }
685 m_fxAnnotArray.RemoveAll();
686
687 delete m_pAnnotList;
688 m_pAnnotList = NULL;
689}
690
691void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
692 CPDF_Matrix* pUser2Device,
693 CPDF_RenderOptions* pOptions,
694 FX_RECT* pClip) {
695 m_curMatrix = *pUser2Device;
696 // m_pAnnotList->DisplayAnnots(m_page, pDevice, pUser2Device, FALSE,
697 // pOptions);
698 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
699 CPDFXFA_Page* pPage = GetPDFXFAPage();
700
701 if (pPage == NULL)
702 return;
703
704 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
705 CFX_Graphics gs;
706 gs.Create(pDevice);
707 if (pClip) {
708 CFX_RectF rectClip;
709 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
710 static_cast<FX_FLOAT>(pClip->top),
711 static_cast<FX_FLOAT>(pClip->Width()),
712 static_cast<FX_FLOAT>(pClip->Height()));
713 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700714 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
716 if (!pRenderContext)
717 return;
718 CXFA_RenderOptions renderOptions;
719 renderOptions.m_bHighlight = TRUE;
720 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
721 renderOptions);
722 pRenderContext->DoRender();
723 pRenderContext->StopRender();
724 pRenderContext->Release();
725 return;
726 }
727 // for pdf/static xfa.
728 CPDFSDK_AnnotIterator annotIterator(this, TRUE);
729 CPDFSDK_Annot* pSDKAnnot = NULL;
730 int index = -1;
731 pSDKAnnot = annotIterator.Next(index);
732 while (pSDKAnnot) {
733 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
734 ASSERT(pAnnotHandlerMgr);
735 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
736 pSDKAnnot = annotIterator.Next(index);
737 }
738}
739CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
740 FX_FLOAT pageY) {
741 int nCount = m_pAnnotList->Count();
742 for (int i = 0; i < nCount; i++) {
743 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
744 CFX_FloatRect annotRect;
745 pAnnot->GetRect(annotRect);
746 if (annotRect.Contains(pageX, pageY))
747 return pAnnot;
748 }
749 return NULL;
750}
751
752CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
753 FX_FLOAT pageY) {
754 int nCount = m_pAnnotList->Count();
755 for (int i = 0; i < nCount; i++) {
756 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
757 if (pAnnot->GetSubType() == "Widget") {
758 CFX_FloatRect annotRect;
759 pAnnot->GetRect(annotRect);
760 if (annotRect.Contains(pageX, pageY))
761 return pAnnot;
762 }
763 }
764 return NULL;
765}
766
767CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
768 FX_FLOAT pageY) {
769 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
770 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
771 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
772 CPDFSDK_Annot* pSDKAnnot = NULL;
773 int index = -1;
774 pSDKAnnot = annotIterator.Next(index);
775 while (pSDKAnnot) {
776 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
777 if (rc.Contains(pageX, pageY))
778 return pSDKAnnot;
779 pSDKAnnot = annotIterator.Next(index);
780 }
781
782 return NULL;
783}
784
785CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
786 FX_FLOAT pageY) {
787 CPDFSDK_AnnotIterator annotIterator(this, FALSE);
788 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
789 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
790 CPDFSDK_Annot* pSDKAnnot = NULL;
791 int index = -1;
792 pSDKAnnot = annotIterator.Next(index);
793 while (pSDKAnnot) {
794 if (pSDKAnnot->GetType() == "Widget" ||
795 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
796 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
797 CPDF_Point point(pageX, pageY);
798 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
799 return pSDKAnnot;
800 }
801 pSDKAnnot = annotIterator.Next(index);
802 }
803
804 return NULL;
805}
806
807FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
808 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
809 if (pAnnotDic)
810 return pAnnotDic->KeyExist("AS");
811 return FALSE;
812}
813
814CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
815 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
816 ASSERT(pEnv);
817 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
818
819 CPDFSDK_Annot* pSDKAnnot = NULL;
820
821 if (pAnnotHandler) {
822 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
823 }
824 if (!pSDKAnnot)
825 return NULL;
826
827 m_fxAnnotArray.Add(pSDKAnnot);
828
829 if (pAnnotHandler) {
830 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
831 }
832
833 return pSDKAnnot;
834}
835
836CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
837 if (!pPDFAnnot)
838 return NULL;
839
840 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
841 if (pSDKAnnot)
842 return pSDKAnnot;
843
844 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
845 ASSERT(pEnv);
846 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
847
848 pSDKAnnot = NULL;
849
850 if (pAnnotHandler) {
851 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
852 }
853 if (!pSDKAnnot)
854 return NULL;
855
856 m_fxAnnotArray.Add(pSDKAnnot);
857
858 return pSDKAnnot;
859}
860
861CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
862 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
863}
864
865CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
866 CPDF_Dictionary* pDict) {
867 return NULL;
868}
869
870FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
871 if (!pAnnot)
872 return FALSE;
873 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
874 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
875 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
876 return FALSE;
877
878 int index = m_fxAnnotArray.Find(pAnnot);
879 m_fxAnnotArray.RemoveAt(index);
880 if (m_CaptureWidget == pAnnot)
881 m_CaptureWidget = NULL;
882
883 return TRUE;
884}
885
886CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
887 if (m_page) {
888 return m_page->GetDocument()->GetPDFDoc();
889 }
890 return NULL;
891}
892
893CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
894 if (m_page) {
895 return m_page->GetPDFPage();
896 }
897
898 return NULL;
899}
900
901int CPDFSDK_PageView::CountAnnots() {
902 return m_fxAnnotArray.GetSize();
903}
904
905CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(int nIndex) {
906 int nCount = m_fxAnnotArray.GetSize();
907 if (nIndex < 0 || nIndex >= nCount) {
908 return NULL;
909 }
910
911 return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
912}
913
914CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
915 int nCount = m_fxAnnotArray.GetSize();
916 for (int i = 0; i < nCount; i++) {
917 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
918 if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
919 return pAnnot;
920 }
921 return NULL;
922}
923CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
924 if (hWidget == NULL)
925 return NULL;
926 int annotCount = m_fxAnnotArray.GetSize();
927
928 for (int i = 0; i < annotCount; i++) {
929 CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
930 if (pAnnot->GetXFAWidget() == hWidget)
931 return pAnnot;
932 }
933 return NULL;
934}
935
936FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
937 FX_UINT nFlag) {
938 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
939 ASSERT(pEnv);
940 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
941 if (!pFXAnnot) {
942 KillFocusAnnot(nFlag);
943 } else {
944 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
945 ASSERT(pAnnotHandlerMgr);
946
947 FX_BOOL bRet =
948 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
949 if (bRet) {
950 SetFocusAnnot(pFXAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700951 }
952 return bRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700953 }
954 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700955}
956
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
958 FX_UINT nFlag) {
959 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
960 ASSERT(pEnv);
961 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
962 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700963
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
965
966 if (pFXAnnot == NULL)
967 return FALSE;
968
969 FX_BOOL bRet =
970 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
971 if (bRet) {
972 SetFocusAnnot(pFXAnnot);
973 }
974 return TRUE;
975}
976
977FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
978 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
979 ASSERT(pEnv);
980 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
981 ASSERT(pAnnotHandlerMgr);
982
983 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
984
985 if (pFXAnnot == NULL)
986 return FALSE;
987
988 FX_BOOL bRet =
989 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
990 if (bRet) {
991 SetFocusAnnot(pFXAnnot);
992 }
993 return TRUE;
994}
995
996FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
997 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
998 ASSERT(pEnv);
999 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1000 ASSERT(pAnnotHandlerMgr);
1001 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
1002 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
1003 FX_BOOL bRet = FALSE;
1004 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
1005 // Last focus Annot gets a chance to handle the event.
1006 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
1007 }
1008 if (pFXAnnot && !bRet) {
1009 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
1010 return bRet;
1011 }
1012 return bRet;
1013}
1014
1015FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
1016 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1017 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1018 ASSERT(pAnnotHandlerMgr);
1019 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
1020 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
1021 m_bExitWidget = TRUE;
1022 m_bEnterWidget = FALSE;
1023 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1024 }
1025 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
1026 m_bOnWidget = TRUE;
1027 if (!m_bEnterWidget) {
1028 m_bEnterWidget = TRUE;
1029 m_bExitWidget = FALSE;
1030 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
1031 }
1032 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
1033 return TRUE;
1034 }
1035 if (m_bOnWidget) {
1036 m_bOnWidget = FALSE;
1037 m_bExitWidget = TRUE;
1038 m_bEnterWidget = FALSE;
1039 if (m_CaptureWidget) {
1040 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1041 m_CaptureWidget = NULL;
1042 }
1043 }
1044 return FALSE;
1045}
1046
1047FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1048 double deltaY,
1049 const CPDF_Point& point,
1050 int nFlag) {
1051 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001052 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1053 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1054 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1056 (int)deltaY, point);
1057 }
1058 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001059}
1060
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001061FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1062 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001063 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001064 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001065 ASSERT(pAnnotHandlerMgr);
1066 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1067 }
Bo Xufdc00a72014-10-28 23:03:33 -07001068
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001070}
1071
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1073 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001074 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1076 ASSERT(pAnnotHandlerMgr);
1077 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1078 }
1079 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001080}
1081
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001083 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001084}
1085
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086void 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}