John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 Zhang | 60f507b | 2015-06-13 00:41:00 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 9 | #include "fpdfsdk/include/fsdk_mgr.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 10 | |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 11 | #include "fpdfsdk/include/formfiller/FFL_FormFiller.h" |
| 12 | #include "fpdfsdk/include/fsdk_define.h" |
| 13 | #include "fpdfsdk/include/javascript/IJavaScript.h" |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 14 | #include "public/fpdf_ext.h" |
Lei Zhang | 8241df7 | 2015-11-06 14:38:48 -0800 | [diff] [blame] | 15 | #include "third_party/base/nonstd_unique_ptr.h" |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 16 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 17 | #include "../include/fpdfxfa/fpdfxfa_app.h" |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 18 | #include "../include/fpdfxfa/fpdfxfa_doc.h" |
| 19 | #include "../include/fpdfxfa/fpdfxfa_page.h" |
| 20 | #include "../include/fpdfxfa/fpdfxfa_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 21 | |
| 22 | #if _FX_OS_ == _FX_ANDROID_ |
| 23 | #include "time.h" |
| 24 | #else |
| 25 | #include <ctime> |
| 26 | #endif |
| 27 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 28 | class CFX_SystemHandler : public IFX_SystemHandler { |
| 29 | public: |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 30 | explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv) |
| 31 | : m_pEnv(pEnv), m_nCharSet(-1) {} |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 32 | ~CFX_SystemHandler() override {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | public: |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 35 | // IFX_SystemHandler |
| 36 | void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override; |
| 37 | void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override; |
| 38 | FX_BOOL IsSelectionImplemented() override; |
| 39 | CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; } |
| 40 | FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 41 | return FALSE; |
| 42 | } |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 43 | void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {} |
| 44 | void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {} |
| 45 | void SetCursor(int32_t nCursorType) override; |
| 46 | FX_HMENU CreatePopupMenu() override { return NULL; } |
| 47 | FX_BOOL AppendMenuItem(FX_HMENU hMenu, |
| 48 | int32_t nIDNewItem, |
| 49 | CFX_WideString string) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 50 | return FALSE; |
| 51 | } |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 52 | FX_BOOL EnableMenuItem(FX_HMENU hMenu, |
| 53 | int32_t nIDItem, |
| 54 | FX_BOOL bEnabled) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 55 | return FALSE; |
| 56 | } |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 57 | int32_t TrackPopupMenu(FX_HMENU hMenu, |
| 58 | int32_t x, |
| 59 | int32_t y, |
| 60 | FX_HWND hParent) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 61 | return -1; |
| 62 | } |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 63 | void DestroyMenu(FX_HMENU hMenu) override {} |
| 64 | CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override; |
| 65 | FX_BOOL FindNativeTrueTypeFont(int32_t nCharset, |
| 66 | CFX_ByteString sFontFaceName) override; |
| 67 | CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc, |
| 68 | CFX_ByteString sFontFaceName, |
| 69 | uint8_t nCharset) override; |
| 70 | int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override; |
| 71 | void KillTimer(int32_t nID) override; |
| 72 | FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 73 | return m_pEnv->FFI_IsSHIFTKeyDown(nFlag); |
| 74 | } |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 75 | FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 76 | return m_pEnv->FFI_IsCTRLKeyDown(nFlag); |
| 77 | } |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 78 | FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | return m_pEnv->FFI_IsALTKeyDown(nFlag); |
| 80 | } |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 81 | FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 82 | return m_pEnv->FFI_IsINSERTKeyDown(nFlag); |
| 83 | } |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 84 | FX_SYSTEMTIME GetLocalTime() override; |
| 85 | int32_t GetCharSet() override { return m_nCharSet; } |
| 86 | void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | CPDFDoc_Environment* m_pEnv; |
| 90 | int m_nCharSet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | void CFX_SystemHandler::SetCursor(int32_t nCursorType) { |
| 94 | m_pEnv->FFI_SetCursor(nCursorType); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd; |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 99 | CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView(); |
| 100 | UnderlyingPageType* pPage = pSDKAnnot->GetUnderlyingPage(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | if (!pPage || !pPageView) |
| 102 | return; |
| 103 | CPDF_Matrix page2device; |
| 104 | pPageView->GetCurrentMatrix(page2device); |
| 105 | CPDF_Matrix device2page; |
| 106 | device2page.SetReverse(page2device); |
| 107 | FX_FLOAT left, top, right, bottom; |
| 108 | device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top); |
| 109 | device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right, |
| 110 | bottom); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | CPDF_Rect rcPDF(left, bottom, right, top); |
| 112 | rcPDF.Normalize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 113 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 114 | m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right, |
| 115 | rcPDF.bottom); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 116 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 117 | void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) { |
| 118 | CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller; |
| 119 | if (pFFL) { |
| 120 | CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom); |
| 121 | CPDF_Point righttop = CPDF_Point(rect.right, rect.top); |
| 122 | CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom); |
| 123 | CPDF_Point ptB = pFFL->PWLtoFFL(righttop); |
| 124 | CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot(); |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 125 | UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | ASSERT(pPage); |
| 127 | m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y); |
| 128 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 131 | FX_BOOL CFX_SystemHandler::IsSelectionImplemented() { |
| 132 | if (m_pEnv) { |
| 133 | FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo(); |
| 134 | if (pInfo && pInfo->FFI_OutputSelectedRect) |
| 135 | return TRUE; |
| 136 | } |
| 137 | return FALSE; |
| 138 | } |
| 139 | |
| 140 | CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) { |
| 141 | return ""; |
| 142 | } |
| 143 | |
| 144 | FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont( |
| 145 | int32_t nCharset, |
| 146 | CFX_ByteString sFontFaceName) { |
| 147 | CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); |
Lei Zhang | 122ee21 | 2015-10-21 17:07:24 -0700 | [diff] [blame] | 148 | if (!pFontMgr) |
| 149 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 150 | |
Lei Zhang | 122ee21 | 2015-10-21 17:07:24 -0700 | [diff] [blame] | 151 | CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper(); |
| 152 | if (!pFontMapper) |
| 153 | return FALSE; |
| 154 | |
| 155 | int nSize = pFontMapper->m_InstalledTTFonts.GetSize(); |
| 156 | if (nSize == 0) { |
| 157 | pFontMapper->LoadInstalledFonts(); |
| 158 | nSize = pFontMapper->m_InstalledTTFonts.GetSize(); |
| 159 | } |
| 160 | |
| 161 | for (int i = 0; i < nSize; ++i) { |
| 162 | if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName)) |
| 163 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return FALSE; |
| 167 | } |
| 168 | |
| 169 | static 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 | } |
| 180 | CPDF_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 | |
| 196 | int32_t CFX_SystemHandler::SetTimer(int32_t uElapse, |
| 197 | TimerCallback lpTimerFunc) { |
| 198 | return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc); |
| 199 | } |
| 200 | void CFX_SystemHandler::KillTimer(int32_t nID) { |
| 201 | m_pEnv->FFI_KillTimer(nID); |
| 202 | } |
| 203 | |
| 204 | FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() { |
| 205 | return m_pEnv->FFI_GetLocalTime(); |
| 206 | } |
| 207 | |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 208 | CPDFDoc_Environment::CPDFDoc_Environment(UnderlyingDocumentType* pDoc, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | FPDF_FORMFILLINFO* pFFinfo) |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 210 | : m_pInfo(pFFinfo), m_pSDKDoc(NULL), m_pUnderlyingDoc(pDoc) { |
Oliver Chang | de6088d | 2015-10-30 16:30:28 -0700 | [diff] [blame] | 211 | m_pSysHandler.reset(new CFX_SystemHandler(this)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | CPDFDoc_Environment::~CPDFDoc_Environment() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 215 | CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 216 | if (pProvider->m_pEnvList.GetSize() == 0) |
| 217 | pProvider->SetJavaScriptInitialized(FALSE); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg, |
| 221 | const FX_WCHAR* Title, |
| 222 | FX_UINT Type, |
| 223 | FX_UINT Icon) { |
| 224 | if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) { |
| 225 | CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); |
| 226 | CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); |
| 227 | FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength()); |
| 228 | FPDF_WIDESTRING pTitle = |
| 229 | (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength()); |
| 230 | int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg, |
| 231 | pTitle, Type, Icon); |
| 232 | bsMsg.ReleaseBuffer(); |
| 233 | bsTitle.ReleaseBuffer(); |
| 234 | return ret; |
| 235 | } |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question, |
| 240 | const FX_WCHAR* Title, |
| 241 | const FX_WCHAR* Default, |
| 242 | const FX_WCHAR* cLabel, |
| 243 | FPDF_BOOL bPassword, |
| 244 | void* response, |
| 245 | int length) { |
| 246 | if (m_pInfo && m_pInfo->m_pJsPlatform && |
| 247 | m_pInfo->m_pJsPlatform->app_response) { |
| 248 | CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode(); |
| 249 | CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode(); |
| 250 | CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode(); |
| 251 | CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode(); |
| 252 | FPDF_WIDESTRING pQuestion = |
| 253 | (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength()); |
| 254 | FPDF_WIDESTRING pTitle = |
| 255 | (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength()); |
| 256 | FPDF_WIDESTRING pDefault = |
| 257 | (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength()); |
| 258 | FPDF_WIDESTRING pLabel = |
| 259 | (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength()); |
| 260 | int ret = m_pInfo->m_pJsPlatform->app_response( |
| 261 | m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword, |
| 262 | response, length); |
| 263 | bsQuestion.ReleaseBuffer(); |
| 264 | bsTitle.ReleaseBuffer(); |
| 265 | bsDefault.ReleaseBuffer(); |
| 266 | bsLabel.ReleaseBuffer(); |
| 267 | return ret; |
| 268 | } |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() { |
| 273 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 274 | !m_pInfo->m_pJsPlatform->Field_browse) { |
| 275 | return L""; |
| 276 | } |
| 277 | |
| 278 | const int nRequiredLen = |
| 279 | m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0); |
| 280 | if (nRequiredLen <= 0) |
| 281 | return L""; |
| 282 | |
| 283 | nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]); |
| 284 | memset(pBuff.get(), 0, nRequiredLen); |
| 285 | const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse( |
| 286 | m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen); |
| 287 | if (nActualLen <= 0 || nActualLen > nRequiredLen) |
| 288 | return L""; |
| 289 | |
| 290 | CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen); |
| 291 | CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet); |
| 292 | return wsRet; |
| 293 | } |
| 294 | |
| 295 | CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() { |
| 296 | if (!m_pInfo || !m_pInfo->m_pJsPlatform || |
| 297 | !m_pInfo->m_pJsPlatform->Doc_getFilePath) { |
| 298 | return L""; |
| 299 | } |
| 300 | |
| 301 | const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath( |
| 302 | 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->Doc_getFilePath( |
| 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 | |
| 318 | void CPDFDoc_Environment::JS_docSubmitForm(void* formData, |
| 319 | int length, |
| 320 | const FX_WCHAR* URL) { |
| 321 | if (m_pInfo && m_pInfo->m_pJsPlatform && |
| 322 | m_pInfo->m_pJsPlatform->Doc_submitForm) { |
| 323 | CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode(); |
| 324 | FPDF_WIDESTRING pDestination = |
| 325 | (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength()); |
| 326 | m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData, |
| 327 | length, pDestination); |
| 328 | bsDestination.ReleaseBuffer(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void CPDFDoc_Environment::JS_docmailForm(void* mailData, |
| 333 | int length, |
| 334 | FPDF_BOOL bUI, |
| 335 | const FX_WCHAR* To, |
| 336 | const FX_WCHAR* Subject, |
| 337 | const FX_WCHAR* CC, |
| 338 | const FX_WCHAR* BCC, |
| 339 | const FX_WCHAR* Msg) { |
| 340 | if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) { |
| 341 | CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode(); |
| 342 | CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode(); |
| 343 | CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode(); |
| 344 | CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode(); |
| 345 | CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode(); |
| 346 | FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength()); |
| 347 | FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength()); |
| 348 | FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength()); |
| 349 | FPDF_WIDESTRING pSubject = |
| 350 | (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength()); |
| 351 | FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength()); |
| 352 | m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length, |
| 353 | bUI, pTo, pSubject, pCC, pBcc, pMsg); |
| 354 | bsTo.ReleaseBuffer(); |
| 355 | bsCC.ReleaseBuffer(); |
| 356 | bsBcc.ReleaseBuffer(); |
| 357 | bsSubject.ReleaseBuffer(); |
| 358 | bsMsg.ReleaseBuffer(); |
| 359 | } |
| 360 | } |
| 361 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 362 | IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 363 | if (!IsJSInitiated()) |
Tom Sepez | dcbc02f | 2015-07-17 09:16:17 -0700 | [diff] [blame] | 364 | return NULL; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 365 | if (!m_pJSRuntime) |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 366 | m_pJSRuntime.reset(IJS_Runtime::Create(this)); |
Tom Sepez | 854a7f6 | 2015-09-21 17:07:49 -0700 | [diff] [blame] | 367 | return m_pJSRuntime.get(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 370 | CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() { |
| 371 | if (!m_pAnnotHandlerMgr) |
Oliver Chang | 2475249 | 2015-10-30 16:08:20 -0700 | [diff] [blame] | 372 | m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this)); |
| 373 | return m_pAnnotHandlerMgr.get(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 376 | CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() { |
| 377 | if (!m_pActionHandler) |
Oliver Chang | 2475249 | 2015-10-30 16:08:20 -0700 | [diff] [blame] | 378 | m_pActionHandler.reset(new CPDFSDK_ActionHandler()); |
| 379 | return m_pActionHandler.get(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 382 | CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() { |
| 383 | if (!m_pIFormFiller) |
Oliver Chang | 2475249 | 2015-10-30 16:08:20 -0700 | [diff] [blame] | 384 | m_pIFormFiller.reset(new CFFL_IFormFiller(this)); |
| 385 | return m_pIFormFiller.get(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 388 | CPDFSDK_Document::CPDFSDK_Document(UnderlyingDocumentType* pDoc, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 389 | CPDFDoc_Environment* pEnv) |
| 390 | : m_pDoc(pDoc), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 391 | m_pFocusAnnot(nullptr), |
| 392 | m_pEnv(pEnv), |
Oliver Chang | 972b78d | 2015-10-30 12:59:29 -0700 | [diff] [blame] | 393 | m_bChangeMask(FALSE), |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 394 | m_bBeingDestroyed(FALSE) {} |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 395 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 396 | CPDFSDK_Document::~CPDFSDK_Document() { |
Oliver Chang | 972b78d | 2015-10-30 12:59:29 -0700 | [diff] [blame] | 397 | m_bBeingDestroyed = TRUE; |
| 398 | |
| 399 | for (auto& it : m_pageMap) |
| 400 | it.second->KillFocusAnnotIfNeeded(); |
| 401 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 402 | for (auto& it : m_pageMap) |
| 403 | delete it.second; |
| 404 | m_pageMap.clear(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 405 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 406 | |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 407 | CPDFSDK_PageView* CPDFSDK_Document::GetPageView( |
| 408 | UnderlyingPageType* pUnderlyingPage, |
| 409 | FX_BOOL ReNew) { |
| 410 | auto it = m_pageMap.find(pUnderlyingPage); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 411 | if (it != m_pageMap.end()) |
| 412 | return it->second; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 413 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 414 | if (!ReNew) |
| 415 | return nullptr; |
| 416 | |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 417 | CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage); |
| 418 | m_pageMap[pUnderlyingPage] = pPageView; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 419 | // Delay to load all the annotations, to avoid endless loop. |
| 420 | pPageView->LoadFXAnnots(); |
| 421 | return pPageView; |
| 422 | } |
| 423 | |
| 424 | CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 425 | UnderlyingPageType* pPage = |
| 426 | UnderlyingFromFPDFPage(m_pEnv->FFI_GetCurrentPage(m_pDoc)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 427 | return pPage ? GetPageView(pPage, TRUE) : nullptr; |
| 428 | } |
| 429 | |
| 430 | CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 431 | UnderlyingPageType* pTempPage = |
| 432 | UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 433 | if (!pTempPage) |
| 434 | return nullptr; |
| 435 | |
| 436 | auto it = m_pageMap.find(pTempPage); |
| 437 | return it->second; |
| 438 | } |
| 439 | |
| 440 | void CPDFSDK_Document::ProcJavascriptFun() { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 441 | CPDF_Document* pPDFDoc = GetPDFDocument(); |
| 442 | CPDF_DocJSActions docJS(pPDFDoc); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 443 | int iCount = docJS.CountJSActions(); |
| 444 | if (iCount < 1) |
| 445 | return; |
| 446 | for (int i = 0; i < iCount; i++) { |
| 447 | CFX_ByteString csJSName; |
| 448 | CPDF_Action jsAction = docJS.GetJSAction(i, csJSName); |
| 449 | if (m_pEnv->GetActionHander()) |
| 450 | m_pEnv->GetActionHander()->DoAction_JavaScript( |
| 451 | jsAction, CFX_WideString::FromLocal(csJSName), this); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | FX_BOOL CPDFSDK_Document::ProcOpenAction() { |
| 456 | if (!m_pDoc) |
Tom Sepez | dcbc02f | 2015-07-17 09:16:17 -0700 | [diff] [blame] | 457 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 458 | |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 459 | CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 460 | if (!pRoot) |
| 461 | return FALSE; |
| 462 | |
| 463 | CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction"); |
| 464 | if (!pOpenAction) |
| 465 | pOpenAction = pRoot->GetArray("OpenAction"); |
| 466 | |
| 467 | if (!pOpenAction) |
| 468 | return FALSE; |
| 469 | |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 470 | if (pOpenAction->IsArray()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 471 | return TRUE; |
| 472 | |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 473 | if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 474 | CPDF_Action action(pDict); |
| 475 | if (m_pEnv->GetActionHander()) |
| 476 | m_pEnv->GetActionHander()->DoAction_DocOpen(action, this); |
| 477 | return TRUE; |
| 478 | } |
| 479 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 482 | CPDF_OCContext* CPDFSDK_Document::GetOCContext() { |
| 483 | if (!m_pOccontent) |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 484 | m_pOccontent.reset(new CPDF_OCContext(GetPDFDocument())); |
Oliver Chang | 2475249 | 2015-10-30 16:08:20 -0700 | [diff] [blame] | 485 | return m_pOccontent.get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 486 | } |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 487 | |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 488 | void CPDFSDK_Document::RemovePageView(UnderlyingPageType* pUnderlyingPage) { |
| 489 | auto it = m_pageMap.find(pUnderlyingPage); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 490 | if (it == m_pageMap.end()) |
| 491 | return; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 492 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 493 | CPDFSDK_PageView* pPageView = it->second; |
| 494 | if (pPageView->IsLocked()) |
| 495 | return; |
| 496 | |
Oliver Chang | 972b78d | 2015-10-30 12:59:29 -0700 | [diff] [blame] | 497 | pPageView->KillFocusAnnotIfNeeded(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 498 | delete pPageView; |
| 499 | m_pageMap.erase(it); |
| 500 | } |
| 501 | |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 502 | UnderlyingPageType* CPDFSDK_Document::GetPage(int nIndex) { |
| 503 | return UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() { |
| 507 | if (!m_pInterForm) |
Oliver Chang | 2475249 | 2015-10-30 16:08:20 -0700 | [diff] [blame] | 508 | m_pInterForm.reset(new CPDFSDK_InterForm(this)); |
| 509 | return m_pInterForm.get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender, |
| 513 | CPDFSDK_Annot* pAnnot) { |
| 514 | for (const auto& it : m_pageMap) { |
| 515 | CPDFSDK_PageView* pPageView = it.second; |
| 516 | if (pPageView != pSender) { |
| 517 | pPageView->UpdateView(pAnnot); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() { |
| 523 | return m_pFocusAnnot; |
| 524 | } |
| 525 | |
| 526 | FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) { |
Oliver Chang | 972b78d | 2015-10-30 12:59:29 -0700 | [diff] [blame] | 527 | if (m_bBeingDestroyed) |
| 528 | return FALSE; |
| 529 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 530 | if (m_pFocusAnnot == pAnnot) |
| 531 | return TRUE; |
| 532 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 533 | if (m_pFocusAnnot) { |
| 534 | if (!KillFocusAnnot(nFlag)) |
| 535 | return FALSE; |
| 536 | } |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 537 | |
| 538 | if (!pAnnot) |
| 539 | return FALSE; |
| 540 | |
| 541 | CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot; |
| 542 | CPDFSDK_PageView* pPageView = pAnnot->GetPageView(); |
| 543 | if (pPageView && pPageView->IsValid()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 544 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr(); |
Lei Zhang | a060146 | 2015-10-30 14:54:37 -0700 | [diff] [blame] | 545 | if (!m_pFocusAnnot) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 546 | if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot)) |
Tom Sepez | dcbc02f | 2015-07-17 09:16:17 -0700 | [diff] [blame] | 547 | return FALSE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 548 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 549 | if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag)) |
| 550 | return FALSE; |
| 551 | if (!m_pFocusAnnot) { |
| 552 | m_pFocusAnnot = pAnnot; |
| 553 | return TRUE; |
| 554 | } |
Tom Sepez | dcbc02f | 2015-07-17 09:16:17 -0700 | [diff] [blame] | 555 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 556 | } |
| 557 | return FALSE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 560 | FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) { |
| 561 | if (m_pFocusAnnot) { |
| 562 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr(); |
Lei Zhang | a060146 | 2015-10-30 14:54:37 -0700 | [diff] [blame] | 563 | CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot; |
| 564 | m_pFocusAnnot = nullptr; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 565 | |
Lei Zhang | a060146 | 2015-10-30 14:54:37 -0700 | [diff] [blame] | 566 | if (!pAnnotHandler->Annot_OnChangeFocus(nullptr, pFocusAnnot)) |
| 567 | return FALSE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 568 | |
Lei Zhang | a060146 | 2015-10-30 14:54:37 -0700 | [diff] [blame] | 569 | if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) { |
| 570 | if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) { |
| 571 | CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot; |
| 572 | int nFieldType = pWidget->GetFieldType(); |
| 573 | if (FIELDTYPE_TEXTFIELD == nFieldType || |
| 574 | FIELDTYPE_COMBOBOX == nFieldType) { |
| 575 | m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 576 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 577 | } |
Lei Zhang | a060146 | 2015-10-30 14:54:37 -0700 | [diff] [blame] | 578 | |
| 579 | if (!m_pFocusAnnot) |
| 580 | return TRUE; |
| 581 | } else { |
| 582 | m_pFocusAnnot = pFocusAnnot; |
Tom Sepez | dcbc02f | 2015-07-17 09:16:17 -0700 | [diff] [blame] | 583 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 584 | } |
| 585 | return FALSE; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 586 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 587 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 588 | void CPDFSDK_Document::OnCloseDocument() { |
| 589 | KillFocusAnnot(); |
| 590 | } |
| 591 | |
| 592 | FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) { |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 593 | return GetPDFDocument()->GetUserPermissions() & nFlag; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 596 | IJS_Runtime* CPDFSDK_Document::GetJsRuntime() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 597 | ASSERT(m_pEnv != NULL); |
| 598 | return m_pEnv->GetJSRuntime(); |
| 599 | } |
| 600 | |
| 601 | CFX_WideString CPDFSDK_Document::GetPath() { |
| 602 | ASSERT(m_pEnv != NULL); |
| 603 | return m_pEnv->JS_docGetFilePath(); |
| 604 | } |
| 605 | |
| 606 | CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc, |
Tom Sepez | 540c436 | 2015-11-24 13:33:57 -0800 | [diff] [blame] | 607 | UnderlyingPageType* page) |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 608 | : m_page(page), |
| 609 | m_pSDKDoc(pSDKDoc), |
| 610 | m_CaptureWidget(nullptr), |
| 611 | m_bEnterWidget(FALSE), |
| 612 | m_bExitWidget(FALSE), |
| 613 | m_bOnWidget(FALSE), |
| 614 | m_bValid(FALSE), |
| 615 | m_bLocked(FALSE) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 616 | CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm(); |
| 617 | if (pInterForm) { |
| 618 | CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm(); |
| 619 | if (page->GetPDFPage()) |
| 620 | pPDFInterForm->FixPageFields(page->GetPDFPage()); |
| 621 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | CPDFSDK_PageView::~CPDFSDK_PageView() { |
| 625 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
Tom Sepez | 396e872 | 2015-09-09 10:16:08 -0700 | [diff] [blame] | 626 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 627 | for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 628 | pAnnotHandlerMgr->ReleaseAnnot(pAnnot); |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 629 | m_fxAnnotArray.clear(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 630 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 631 | m_pAnnotList.reset(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice, |
| 635 | CPDF_Matrix* pUser2Device, |
| 636 | CPDF_RenderOptions* pOptions, |
Lei Zhang | f0e2e1b | 2015-11-02 13:27:54 -0800 | [diff] [blame] | 637 | const FX_RECT& pClip) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 638 | m_curMatrix = *pUser2Device; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 639 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 640 | CPDFXFA_Page* pPage = GetPDFXFAPage(); |
| 641 | |
| 642 | if (pPage == NULL) |
| 643 | return; |
| 644 | |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 645 | #ifdef PDF_ENABLE_XFA |
Tom Sepez | d3116dc | 2015-11-24 15:58:06 -0800 | [diff] [blame^] | 646 | if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 647 | CFX_Graphics gs; |
| 648 | gs.Create(pDevice); |
Lei Zhang | f0e2e1b | 2015-11-02 13:27:54 -0800 | [diff] [blame] | 649 | CFX_RectF rectClip; |
| 650 | rectClip.Set(static_cast<FX_FLOAT>(pClip.left), |
| 651 | static_cast<FX_FLOAT>(pClip.top), |
| 652 | static_cast<FX_FLOAT>(pClip.Width()), |
| 653 | static_cast<FX_FLOAT>(pClip.Height())); |
| 654 | gs.SetClipRect(rectClip); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 655 | IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create(); |
| 656 | if (!pRenderContext) |
| 657 | return; |
| 658 | CXFA_RenderOptions renderOptions; |
| 659 | renderOptions.m_bHighlight = TRUE; |
| 660 | pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device, |
| 661 | renderOptions); |
| 662 | pRenderContext->DoRender(); |
| 663 | pRenderContext->StopRender(); |
| 664 | pRenderContext->Release(); |
| 665 | return; |
| 666 | } |
Tom Sepez | a8a39e2 | 2015-10-12 15:47:07 -0700 | [diff] [blame] | 667 | #endif // PDF_ENABLE_XFA |
| 668 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 669 | // for pdf/static xfa. |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 670 | CPDFSDK_AnnotIterator annotIterator(this, true); |
| 671 | while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 672 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 673 | pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 676 | |
Lei Zhang | 1b700c3 | 2015-10-30 23:55:35 -0700 | [diff] [blame] | 677 | const CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX, |
| 678 | FX_FLOAT pageY) { |
| 679 | for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 680 | CFX_FloatRect annotRect; |
| 681 | pAnnot->GetRect(annotRect); |
| 682 | if (annotRect.Contains(pageX, pageY)) |
| 683 | return pAnnot; |
| 684 | } |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 685 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Lei Zhang | 1b700c3 | 2015-10-30 23:55:35 -0700 | [diff] [blame] | 688 | const CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX, |
| 689 | FX_FLOAT pageY) { |
| 690 | for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 691 | if (pAnnot->GetSubType() == "Widget") { |
| 692 | CFX_FloatRect annotRect; |
| 693 | pAnnot->GetRect(annotRect); |
| 694 | if (annotRect.Contains(pageX, pageY)) |
| 695 | return pAnnot; |
| 696 | } |
| 697 | } |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 698 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX, |
| 702 | FX_FLOAT pageY) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 703 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 704 | CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr(); |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 705 | CPDFSDK_AnnotIterator annotIterator(this, false); |
| 706 | while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 707 | CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot); |
| 708 | if (rc.Contains(pageX, pageY)) |
| 709 | return pSDKAnnot; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 712 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX, |
| 716 | FX_FLOAT pageY) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 717 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 718 | CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr(); |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 719 | CPDFSDK_AnnotIterator annotIterator(this, false); |
| 720 | while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 721 | if (pSDKAnnot->GetType() == "Widget" || |
| 722 | pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) { |
| 723 | pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot); |
| 724 | CPDF_Point point(pageX, pageY); |
| 725 | if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point)) |
| 726 | return pSDKAnnot; |
| 727 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 730 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Oliver Chang | 972b78d | 2015-10-30 12:59:29 -0700 | [diff] [blame] | 733 | void CPDFSDK_PageView::KillFocusAnnotIfNeeded() { |
| 734 | // if there is a focused annot on the page, we should kill the focus first. |
| 735 | if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) { |
| 736 | auto it = |
| 737 | std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot); |
| 738 | if (it != m_fxAnnotArray.end()) |
| 739 | KillFocusAnnot(); |
| 740 | } |
| 741 | } |
| 742 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 743 | FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) { |
| 744 | CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict(); |
| 745 | if (pAnnotDic) |
| 746 | return pAnnotDic->KeyExist("AS"); |
| 747 | return FALSE; |
| 748 | } |
| 749 | |
| 750 | CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) { |
| 751 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 752 | ASSERT(pEnv); |
| 753 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr(); |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 754 | CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 755 | if (!pSDKAnnot) |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 756 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 757 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 758 | m_fxAnnotArray.push_back(pSDKAnnot); |
| 759 | pAnnotHandler->Annot_OnCreate(pSDKAnnot); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 760 | return pSDKAnnot; |
| 761 | } |
| 762 | |
| 763 | CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) { |
| 764 | if (!pPDFAnnot) |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 765 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 766 | |
| 767 | CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot); |
| 768 | if (pSDKAnnot) |
| 769 | return pSDKAnnot; |
| 770 | |
| 771 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 772 | CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr(); |
Lei Zhang | a060146 | 2015-10-30 14:54:37 -0700 | [diff] [blame] | 773 | pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 774 | if (!pSDKAnnot) |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 775 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 776 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 777 | m_fxAnnotArray.push_back(pSDKAnnot); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 778 | return pSDKAnnot; |
| 779 | } |
| 780 | |
| 781 | CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) { |
| 782 | return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr; |
| 783 | } |
| 784 | |
| 785 | CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType, |
| 786 | CPDF_Dictionary* pDict) { |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) { |
| 791 | if (!pAnnot) |
| 792 | return FALSE; |
| 793 | CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage(); |
| 794 | if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA && |
Tom Sepez | d3116dc | 2015-11-24 15:58:06 -0800 | [diff] [blame^] | 795 | pPage->GetDocument()->GetDocType() != DOCTYPE_DYNAMIC_XFA)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 796 | return FALSE; |
| 797 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 798 | auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot); |
| 799 | if (it != m_fxAnnotArray.end()) |
| 800 | m_fxAnnotArray.erase(it); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 801 | if (m_CaptureWidget == pAnnot) |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 802 | m_CaptureWidget = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 803 | |
| 804 | return TRUE; |
| 805 | } |
| 806 | |
| 807 | CPDF_Document* CPDFSDK_PageView::GetPDFDocument() { |
| 808 | if (m_page) { |
| 809 | return m_page->GetDocument()->GetPDFDoc(); |
| 810 | } |
| 811 | return NULL; |
| 812 | } |
| 813 | |
| 814 | CPDF_Page* CPDFSDK_PageView::GetPDFPage() { |
| 815 | if (m_page) { |
| 816 | return m_page->GetPDFPage(); |
| 817 | } |
| 818 | |
| 819 | return NULL; |
| 820 | } |
| 821 | |
Lei Zhang | 5021853 | 2015-10-30 14:03:43 -0700 | [diff] [blame] | 822 | size_t CPDFSDK_PageView::CountAnnots() const { |
| 823 | return m_fxAnnotArray.size(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 826 | CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) { |
| 827 | return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) { |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 831 | for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) { |
| 832 | if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 833 | return pAnnot; |
| 834 | } |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 835 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 836 | } |
| 837 | CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) { |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 838 | if (!hWidget) |
| 839 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 840 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 841 | for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 842 | if (pAnnot->GetXFAWidget() == hWidget) |
| 843 | return pAnnot; |
| 844 | } |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 845 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point, |
| 849 | FX_UINT nFlag) { |
| 850 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 851 | ASSERT(pEnv); |
| 852 | CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y); |
| 853 | if (!pFXAnnot) { |
| 854 | KillFocusAnnot(nFlag); |
Lei Zhang | a060146 | 2015-10-30 14:54:37 -0700 | [diff] [blame] | 855 | return FALSE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 856 | } |
Lei Zhang | a060146 | 2015-10-30 14:54:37 -0700 | [diff] [blame] | 857 | |
| 858 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
| 859 | FX_BOOL bRet = |
| 860 | pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point); |
| 861 | if (bRet) |
| 862 | SetFocusAnnot(pFXAnnot); |
| 863 | return bRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 866 | FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point, |
| 867 | FX_UINT nFlag) { |
| 868 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 869 | ASSERT(pEnv); |
| 870 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
| 871 | ASSERT(pAnnotHandlerMgr); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 872 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 873 | CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y); |
| 874 | |
| 875 | if (pFXAnnot == NULL) |
| 876 | return FALSE; |
| 877 | |
| 878 | FX_BOOL bRet = |
| 879 | pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point); |
| 880 | if (bRet) { |
| 881 | SetFocusAnnot(pFXAnnot); |
| 882 | } |
| 883 | return TRUE; |
| 884 | } |
| 885 | |
| 886 | FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) { |
| 887 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 888 | ASSERT(pEnv); |
| 889 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 890 | |
| 891 | CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y); |
| 892 | |
| 893 | if (pFXAnnot == NULL) |
| 894 | return FALSE; |
| 895 | |
| 896 | FX_BOOL bRet = |
| 897 | pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point); |
| 898 | if (bRet) { |
| 899 | SetFocusAnnot(pFXAnnot); |
| 900 | } |
| 901 | return TRUE; |
| 902 | } |
| 903 | |
| 904 | FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) { |
| 905 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 906 | ASSERT(pEnv); |
| 907 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 908 | CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y); |
| 909 | CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot(); |
| 910 | FX_BOOL bRet = FALSE; |
| 911 | if (pFocusAnnot && pFocusAnnot != pFXAnnot) { |
| 912 | // Last focus Annot gets a chance to handle the event. |
| 913 | bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point); |
| 914 | } |
Lei Zhang | a060146 | 2015-10-30 14:54:37 -0700 | [diff] [blame] | 915 | if (pFXAnnot && !bRet) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 916 | bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 917 | return bRet; |
| 918 | } |
| 919 | |
| 920 | FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) { |
| 921 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 922 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 923 | if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) { |
| 924 | if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) { |
| 925 | m_bExitWidget = TRUE; |
| 926 | m_bEnterWidget = FALSE; |
| 927 | pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag); |
| 928 | } |
| 929 | m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot; |
| 930 | m_bOnWidget = TRUE; |
| 931 | if (!m_bEnterWidget) { |
| 932 | m_bEnterWidget = TRUE; |
| 933 | m_bExitWidget = FALSE; |
| 934 | pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag); |
| 935 | } |
| 936 | pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point); |
| 937 | return TRUE; |
| 938 | } |
| 939 | if (m_bOnWidget) { |
| 940 | m_bOnWidget = FALSE; |
| 941 | m_bExitWidget = TRUE; |
| 942 | m_bEnterWidget = FALSE; |
| 943 | if (m_CaptureWidget) { |
| 944 | pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag); |
| 945 | m_CaptureWidget = NULL; |
| 946 | } |
| 947 | } |
| 948 | return FALSE; |
| 949 | } |
| 950 | |
| 951 | FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX, |
| 952 | double deltaY, |
| 953 | const CPDF_Point& point, |
| 954 | int nFlag) { |
| 955 | if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) { |
Tom Sepez | dcbc02f | 2015-07-17 09:16:17 -0700 | [diff] [blame] | 956 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 957 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 958 | return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag, |
| 959 | (int)deltaY, point); |
| 960 | } |
| 961 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 964 | FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) { |
| 965 | if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) { |
Tom Sepez | dcbc02f | 2015-07-17 09:16:17 -0700 | [diff] [blame] | 966 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
Tom Sepez | dcbc02f | 2015-07-17 09:16:17 -0700 | [diff] [blame] | 967 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 968 | return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag); |
| 969 | } |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 970 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 971 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 974 | FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) { |
| 975 | if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) { |
Lei Zhang | 60f507b | 2015-06-13 00:41:00 -0700 | [diff] [blame] | 976 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 977 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 978 | return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag); |
| 979 | } |
| 980 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 983 | FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 984 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 987 | void CPDFSDK_PageView::LoadFXAnnots() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 988 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 989 | CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 990 | |
| 991 | SetLock(TRUE); |
| 992 | m_page->AddRef(); |
Tom Sepez | d3116dc | 2015-11-24 15:58:06 -0800 | [diff] [blame^] | 993 | if (m_pSDKDoc->GetXFADocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 994 | IXFA_PageView* pageView = NULL; |
| 995 | pageView = m_page->GetXFAPageView(); |
| 996 | ASSERT(pageView != NULL); |
| 997 | |
| 998 | IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator( |
| 999 | XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible | |
| 1000 | XFA_WIDGETFILTER_Viewable | |
| 1001 | XFA_WIDGETFILTER_AllType); |
| 1002 | if (!pWidgetHander) { |
| 1003 | m_page->Release(); |
| 1004 | SetLock(FALSE); |
| 1005 | return; |
Tom Sepez | dcbc02f | 2015-07-17 09:16:17 -0700 | [diff] [blame] | 1006 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1007 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 1008 | while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1009 | CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this); |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 1010 | if (!pAnnot) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1011 | continue; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1012 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 1013 | m_fxAnnotArray.push_back(pAnnot); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1014 | pAnnotHandlerMgr->Annot_OnLoad(pAnnot); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | pWidgetHander->Release(); |
| 1018 | } else { |
| 1019 | CPDF_Page* pPage = m_page->GetPDFPage(); |
| 1020 | ASSERT(pPage != NULL); |
| 1021 | |
| 1022 | FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled(); |
| 1023 | // Disable the default AP construction. |
| 1024 | CPDF_InterForm::EnableUpdateAP(FALSE); |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 1025 | m_pAnnotList.reset(new CPDF_AnnotList(pPage)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1026 | CPDF_InterForm::EnableUpdateAP(enableAPUpdate); |
| 1027 | |
Lei Zhang | 1b700c3 | 2015-10-30 23:55:35 -0700 | [diff] [blame] | 1028 | const size_t nCount = m_pAnnotList->Count(); |
| 1029 | for (size_t i = 0; i < nCount; ++i) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1030 | CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i); |
Lei Zhang | 5021853 | 2015-10-30 14:03:43 -0700 | [diff] [blame] | 1031 | CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1032 | |
| 1033 | CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this); |
| 1034 | if (!pAnnot) |
| 1035 | continue; |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 1036 | m_fxAnnotArray.push_back(pAnnot); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1037 | |
| 1038 | pAnnotHandlerMgr->Annot_OnLoad(pAnnot); |
| 1039 | } |
| 1040 | } |
| 1041 | m_page->Release(); |
| 1042 | SetLock(FALSE); |
| 1043 | } |
| 1044 | |
| 1045 | void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) { |
| 1046 | for (int i = 0; i < rects.GetSize(); i++) { |
| 1047 | CPDF_Rect rc = rects.GetAt(i); |
| 1048 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 1049 | pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) { |
| 1054 | CPDF_Rect rcWindow = pAnnot->GetRect(); |
| 1055 | CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv(); |
| 1056 | pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right, |
| 1057 | rcWindow.bottom); |
| 1058 | } |
| 1059 | |
| 1060 | int CPDFSDK_PageView::GetPageIndex() { |
| 1061 | if (m_page) { |
| 1062 | CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict; |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 1063 | CPDF_Document* pDoc = m_pSDKDoc->GetPDFDocument(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1064 | if (pDoc && pDic) { |
Tom Sepez | 50d12ad | 2015-11-24 09:50:51 -0800 | [diff] [blame] | 1065 | return pDoc->GetPageIndex(pDic->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | return -1; |
| 1069 | } |
| 1070 | |
Lei Zhang | 1b700c3 | 2015-10-30 23:55:35 -0700 | [diff] [blame] | 1071 | bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const { |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 1072 | if (!p) |
Lei Zhang | 1b700c3 | 2015-10-30 23:55:35 -0700 | [diff] [blame] | 1073 | return false; |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 1074 | |
Lei Zhang | 1b700c3 | 2015-10-30 23:55:35 -0700 | [diff] [blame] | 1075 | const auto& annots = m_pAnnotList->All(); |
| 1076 | return std::find(annots.begin(), annots.end(), p) != annots.end(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1079 | CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() { |
| 1080 | CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot(); |
| 1081 | if (!pFocusAnnot) |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 1082 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1083 | |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 1084 | for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1085 | if (pAnnot == pFocusAnnot) |
| 1086 | return pAnnot; |
| 1087 | } |
Lei Zhang | bf60b29 | 2015-10-26 12:14:35 -0700 | [diff] [blame] | 1088 | return nullptr; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1089 | } |