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.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#ifndef FPDFSDK_INCLUDE_FSDK_MGR_H_
8#define FPDFSDK_INCLUDE_FSDK_MGR_H_
9
10#include <map>
11#include <memory>
12
13#include "core/include/fpdftext/fpdf_text.h"
14#include "fsdk_actionhandler.h"
15#include "fsdk_annothandler.h"
16#include "fsdk_baseannot.h"
17#include "fsdk_baseform.h"
18#include "fsdk_common.h"
19#include "fsdk_define.h"
20#include "fx_systemhandler.h"
21#include "javascript/IJavaScript.h"
22#include "public/fpdf_formfill.h"
23#include "public/fpdf_fwlevent.h"  // cross platform keycode and events define.
24
25#ifdef PDF_ENABLE_XFA
26#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
27#include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h"
28#endif  // PDF_ENABLE_XFA
29
30class CFFL_IFormFiller;
31class CPDFSDK_ActionHandler;
32class CPDFSDK_Annot;
33class CPDFSDK_Document;
34class CPDFSDK_InterForm;
35class CPDFSDK_PageView;
36class CPDFSDK_Widget;
37class IFX_SystemHandler;
38
39class CPDFDoc_Environment final {
40 public:
41  CPDFDoc_Environment(UnderlyingDocumentType* pDoc, FPDF_FORMFILLINFO* pFFinfo);
42  ~CPDFDoc_Environment();
43
44#ifdef PDF_ENABLE_XFA
45  void Release() {
46    if (m_pInfo && m_pInfo->Release)
47      m_pInfo->Release(m_pInfo);
48    delete this;
49  }
50#endif  // PDF_ENABLE_XFA
51
52  void FFI_Invalidate(FPDF_PAGE page,
53                      double left,
54                      double top,
55                      double right,
56                      double bottom) {
57    if (m_pInfo && m_pInfo->FFI_Invalidate)
58      m_pInfo->FFI_Invalidate(m_pInfo, page, left, top, right, bottom);
59  }
60
61  void FFI_OutputSelectedRect(FPDF_PAGE page,
62                              double left,
63                              double top,
64                              double right,
65                              double bottom) {
66    if (m_pInfo && m_pInfo->FFI_OutputSelectedRect)
67      m_pInfo->FFI_OutputSelectedRect(m_pInfo, page, left, top, right, bottom);
68  }
69
70  void FFI_SetCursor(int nCursorType) {
71    if (m_pInfo && m_pInfo->FFI_SetCursor)
72      m_pInfo->FFI_SetCursor(m_pInfo, nCursorType);
73  }
74
75  int FFI_SetTimer(int uElapse, TimerCallback lpTimerFunc) {
76    if (m_pInfo && m_pInfo->FFI_SetTimer)
77      return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc);
78    return -1;
79  }
80
81  void FFI_KillTimer(int nTimerID) {
82    if (m_pInfo && m_pInfo->FFI_KillTimer)
83      m_pInfo->FFI_KillTimer(m_pInfo, nTimerID);
84  }
85
86  FX_SYSTEMTIME FFI_GetLocalTime() const {
87    FX_SYSTEMTIME fxtime;
88    if (m_pInfo && m_pInfo->FFI_GetLocalTime) {
89      FPDF_SYSTEMTIME systime = m_pInfo->FFI_GetLocalTime(m_pInfo);
90      fxtime.wDay = systime.wDay;
91      fxtime.wDayOfWeek = systime.wDayOfWeek;
92      fxtime.wHour = systime.wHour;
93      fxtime.wMilliseconds = systime.wMilliseconds;
94      fxtime.wMinute = systime.wMinute;
95      fxtime.wMonth = systime.wMonth;
96      fxtime.wSecond = systime.wSecond;
97      fxtime.wYear = systime.wYear;
98    }
99    return fxtime;
100  }
101
102  void FFI_OnChange() {
103    if (m_pInfo && m_pInfo->FFI_OnChange)
104      m_pInfo->FFI_OnChange(m_pInfo);
105  }
106
107  FX_BOOL FFI_IsSHIFTKeyDown(FX_DWORD nFlag) const {
108    return (nFlag & FWL_EVENTFLAG_ShiftKey) != 0;
109  }
110
111  FX_BOOL FFI_IsCTRLKeyDown(FX_DWORD nFlag) const {
112    return (nFlag & FWL_EVENTFLAG_ControlKey) != 0;
113  }
114
115  FX_BOOL FFI_IsALTKeyDown(FX_DWORD nFlag) const {
116    return (nFlag & FWL_EVENTFLAG_AltKey) != 0;
117  }
118
119  FX_BOOL FFI_IsINSERTKeyDown(FX_DWORD nFlag) const { return FALSE; }
120
121  int JS_appAlert(const FX_WCHAR* Msg,
122                  const FX_WCHAR* Title,
123                  FX_UINT Type,
124                  FX_UINT Icon);
125  int JS_appResponse(const FX_WCHAR* Question,
126                     const FX_WCHAR* Title,
127                     const FX_WCHAR* Default,
128                     const FX_WCHAR* cLabel,
129                     FPDF_BOOL bPassword,
130                     void* response,
131                     int length);
132
133  void JS_appBeep(int nType) {
134    if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_beep)
135      m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType);
136  }
137
138  CFX_WideString JS_fieldBrowse();
139  CFX_WideString JS_docGetFilePath();
140
141  void JS_docSubmitForm(void* formData, int length, const FX_WCHAR* URL);
142  void JS_docmailForm(void* mailData,
143                      int length,
144                      FPDF_BOOL bUI,
145                      const FX_WCHAR* To,
146                      const FX_WCHAR* Subject,
147                      const FX_WCHAR* CC,
148                      const FX_WCHAR* BCC,
149                      const FX_WCHAR* Msg);
150
151  void JS_docprint(FPDF_BOOL bUI,
152                   int nStart,
153                   int nEnd,
154                   FPDF_BOOL bSilent,
155                   FPDF_BOOL bShrinkToFit,
156                   FPDF_BOOL bPrintAsImage,
157                   FPDF_BOOL bReverse,
158                   FPDF_BOOL bAnnotations) {
159    if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_print)
160      m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart,
161                                        nEnd, bSilent, bShrinkToFit,
162                                        bPrintAsImage, bReverse, bAnnotations);
163  }
164
165  void JS_docgotoPage(int nPageNum) {
166    if (m_pInfo && m_pInfo->m_pJsPlatform &&
167        m_pInfo->m_pJsPlatform->Doc_gotoPage)
168      m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum);
169  }
170
171  FPDF_PAGE FFI_GetPage(FPDF_DOCUMENT document, int nPageIndex) {
172    if (m_pInfo && m_pInfo->FFI_GetPage)
173      return m_pInfo->FFI_GetPage(m_pInfo, document, nPageIndex);
174    return NULL;
175  }
176
177  FPDF_PAGE FFI_GetCurrentPage(FPDF_DOCUMENT document) {
178    if (m_pInfo && m_pInfo->FFI_GetCurrentPage)
179      return m_pInfo->FFI_GetCurrentPage(m_pInfo, document);
180    return NULL;
181  }
182
183  int FFI_GetRotation(FPDF_PAGE page) {
184    if (m_pInfo && m_pInfo->FFI_GetRotation)
185      return m_pInfo->FFI_GetRotation(m_pInfo, page);
186    return 0;
187  }
188
189  void FFI_ExecuteNamedAction(const FX_CHAR* namedAction) {
190    if (m_pInfo && m_pInfo->FFI_ExecuteNamedAction)
191      m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction);
192  }
193
194  void FFI_OnSetFieldInputFocus(void* field,
195                                FPDF_WIDESTRING focusText,
196                                FPDF_DWORD nTextLen,
197                                FX_BOOL bFocus) {
198    if (m_pInfo && m_pInfo->FFI_SetTextFieldFocus)
199      m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus);
200  }
201
202  void FFI_DoURIAction(const FX_CHAR* bsURI) {
203    if (m_pInfo && m_pInfo->FFI_DoURIAction)
204      m_pInfo->FFI_DoURIAction(m_pInfo, bsURI);
205  }
206
207  void FFI_DoGoToAction(int nPageIndex,
208                        int zoomMode,
209                        float* fPosArray,
210                        int sizeOfArray) {
211    if (m_pInfo && m_pInfo->FFI_DoGoToAction)
212      m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray,
213                                sizeOfArray);
214  }
215
216#ifdef PDF_ENABLE_XFA
217  void FFI_DisplayCaret(FPDF_PAGE page,
218                        FPDF_BOOL bVisible,
219                        double left,
220                        double top,
221                        double right,
222                        double bottom) {
223    if (m_pInfo && m_pInfo->FFI_DisplayCaret)
224      m_pInfo->FFI_DisplayCaret(m_pInfo, page, bVisible, left, top, right,
225                                bottom);
226  }
227
228  int FFI_GetCurrentPageIndex(FPDF_DOCUMENT document) {
229    if (!m_pInfo || !m_pInfo->FFI_GetCurrentPageIndex) {
230      return -1;
231    }
232    return m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document);
233  }
234
235  void FFI_SetCurrentPage(FPDF_DOCUMENT document, int iCurPage) {
236    if (m_pInfo && m_pInfo->FFI_SetCurrentPage)
237      m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage);
238  }
239
240  CFX_WideString FFI_GetAppName() const { return CFX_WideString(L"Acrobat"); }
241
242  CFX_WideString FFI_GetPlatform() {
243    if (m_pInfo && m_pInfo->FFI_GetPlatform) {
244      int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, NULL, 0);
245      if (nRequiredLen <= 0)
246        return L"";
247
248      char* pbuff = new char[nRequiredLen];
249      memset(pbuff, 0, nRequiredLen);
250      int nActualLen = m_pInfo->FFI_GetPlatform(m_pInfo, pbuff, nRequiredLen);
251      if (nActualLen <= 0 || nActualLen > nRequiredLen) {
252        delete[] pbuff;
253        return L"";
254      }
255      CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
256      CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
257          (unsigned short*)bsRet.GetBuffer(bsRet.GetLength()),
258          bsRet.GetLength() / sizeof(unsigned short));
259      delete[] pbuff;
260      return wsRet;
261    }
262    return L"";
263  }
264
265  void FFI_GotoURL(FPDF_DOCUMENT document,
266                   const CFX_WideStringC& wsURL,
267                   FX_BOOL bAppend) {
268    if (m_pInfo && m_pInfo->FFI_GotoURL) {
269      CFX_ByteString bsTo = CFX_WideString(wsURL).UTF16LE_Encode();
270      FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength());
271      m_pInfo->FFI_GotoURL(m_pInfo, document, pTo);
272      bsTo.ReleaseBuffer();
273    }
274  }
275
276  void FFI_GetURL(FPDF_DOCUMENT document, CFX_WideString& wsURL) {
277    wsURL = CFX_WideString();
278  }
279
280  void FFI_AddDoRecord(FPDF_DOCUMENT document, FPDF_WIDGET hWidget) {}
281  void FFI_PageEvent(FPDF_PAGE page, FPDF_DWORD flag) {}
282
283  void FFI_GetPageViewRect(FPDF_PAGE page, FS_RECTF& dstRect) {
284    if (m_pInfo && m_pInfo->FFI_GetPageViewRect) {
285      double left;
286      double top;
287      double right;
288      double bottom;
289      m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom);
290
291      dstRect.left = static_cast<float>(left);
292      dstRect.top = static_cast<float>(top < bottom ? bottom : top);
293      dstRect.bottom = static_cast<float>(top < bottom ? top : bottom);
294      dstRect.right = static_cast<float>(right);
295    }
296  }
297
298  FX_BOOL FFI_PopupMenu(FPDF_PAGE page,
299                        FPDF_WIDGET hWidget,
300                        int menuFlag,
301                        CFX_PointF ptPopup,
302                        const CFX_PointF* pRectExclude) {
303    if (m_pInfo && m_pInfo->FFI_PopupMenu)
304      return m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, ptPopup.x,
305                                    ptPopup.y);
306    return FALSE;
307  }
308
309  void FFI_Alert(FPDF_WIDESTRING Msg,
310                 FPDF_WIDESTRING Title,
311                 int Type,
312                 int Icon) {
313    if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert)
314      m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title,
315                                        Type, Icon);
316  }
317
318  void FFI_EmailTo(FPDF_FILEHANDLER* fileHandler,
319                   FPDF_WIDESTRING pTo,
320                   FPDF_WIDESTRING pSubject,
321                   FPDF_WIDESTRING pCC,
322                   FPDF_WIDESTRING pBcc,
323                   FPDF_WIDESTRING pMsg) {
324    if (m_pInfo && m_pInfo->FFI_EmailTo)
325      m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc,
326                           pMsg);
327  }
328
329  void FFI_UploadTo(FPDF_FILEHANDLER* fileHandler,
330                    int fileFlag,
331                    FPDF_WIDESTRING uploadTo) {
332    if (m_pInfo && m_pInfo->FFI_UploadTo)
333      m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo);
334  }
335
336  FPDF_FILEHANDLER* FFI_OpenFile(int fileType,
337                                 FPDF_WIDESTRING wsURL,
338                                 const char* mode) {
339    if (m_pInfo && m_pInfo->FFI_OpenFile)
340      return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode);
341    return NULL;
342  }
343
344  CFX_WideString FFI_GetFilePath(FPDF_FILEHANDLER* pFileHandler) const {
345    return L"";
346  }
347
348  int FFI_GetDocumentCount() const { return 0; }
349  int FFI_GetCurDocument() const { return 0; }
350
351  IFX_FileRead* FFI_DownloadFromURL(const FX_WCHAR* url) {
352    if (m_pInfo && m_pInfo->FFI_DownloadFromURL) {
353      CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode();
354      FPDF_WIDESTRING wsURL =
355          (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength());
356
357      FPDF_LPFILEHANDLER fileHandler =
358          m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL);
359
360      return new CFPDF_FileStream(fileHandler);
361    }
362    return NULL;
363  }
364
365  CFX_WideString FFI_PostRequestURL(const FX_WCHAR* wsURL,
366                                    const FX_WCHAR* wsData,
367                                    const FX_WCHAR* wsContentType,
368                                    const FX_WCHAR* wsEncode,
369                                    const FX_WCHAR* wsHeader) {
370    if (m_pInfo && m_pInfo->FFI_PostRequestURL) {
371      CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
372      FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
373
374      CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
375      FPDF_WIDESTRING data =
376          (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
377
378      CFX_ByteString bsContentType =
379          CFX_WideString(wsContentType).UTF16LE_Encode();
380      FPDF_WIDESTRING contentType =
381          (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength());
382
383      CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
384      FPDF_WIDESTRING encode =
385          (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
386
387      CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode();
388      FPDF_WIDESTRING header =
389          (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength());
390
391      FPDF_BSTR respone;
392      FPDF_BStr_Init(&respone);
393      m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode,
394                                  header, &respone);
395
396      CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
397          (unsigned short*)respone.str, respone.len / sizeof(unsigned short));
398      FPDF_BStr_Clear(&respone);
399
400      return wsRet;
401    }
402    return L"";
403  }
404
405  FPDF_BOOL FFI_PutRequestURL(const FX_WCHAR* wsURL,
406                              const FX_WCHAR* wsData,
407                              const FX_WCHAR* wsEncode) {
408    if (m_pInfo && m_pInfo->FFI_PutRequestURL) {
409      CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
410      FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
411
412      CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
413      FPDF_WIDESTRING data =
414          (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
415
416      CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
417      FPDF_WIDESTRING encode =
418          (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
419
420      return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode);
421    }
422    return FALSE;
423  }
424
425  FPDF_BOOL FFI_ShowFileDialog(const FX_WCHAR* wsTitle,
426                               const FX_WCHAR* wsFilter,
427                               CFX_WideStringArray& wsPathArr,
428                               FX_BOOL bOpen) {
429    return FALSE;
430  }
431
432  CFX_WideString FFI_GetLanguage() {
433    if (m_pInfo && m_pInfo->FFI_GetLanguage) {
434      int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, NULL, 0);
435      if (nRequiredLen <= 0)
436        return L"";
437
438      char* pbuff = new char[nRequiredLen];
439      memset(pbuff, 0, nRequiredLen);
440      int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen);
441      if (nActualLen <= 0 || nActualLen > nRequiredLen) {
442        delete[] pbuff;
443        return L"";
444      }
445      CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
446      CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
447          (unsigned short*)bsRet.GetBuffer(bsRet.GetLength()),
448          bsRet.GetLength() / sizeof(unsigned short));
449      delete[] pbuff;
450      return wsRet;
451    }
452    return L"";
453  }
454#endif  // PDF_ENABLE_XFA
455
456  FX_BOOL IsJSInitiated() const { return m_pInfo && m_pInfo->m_pJsPlatform; }
457  void SetSDKDocument(CPDFSDK_Document* pFXDoc) { m_pSDKDoc = pFXDoc; }
458  CPDFSDK_Document* GetSDKDocument() const { return m_pSDKDoc; }
459  UnderlyingDocumentType* GetUnderlyingDocument() const {
460    return m_pUnderlyingDoc;
461  }
462  CFX_ByteString GetAppName() const { return ""; }
463  IFX_SystemHandler* GetSysHandler() const { return m_pSysHandler.get(); }
464  FPDF_FORMFILLINFO* GetFormFillInfo() const { return m_pInfo; }
465
466  CFFL_IFormFiller* GetIFormFiller();             // Creates if not present.
467  CPDFSDK_AnnotHandlerMgr* GetAnnotHandlerMgr();  // Creates if not present.
468  IJS_Runtime* GetJSRuntime();                    // Creates if not present.
469  CPDFSDK_ActionHandler* GetActionHander();       // Creates if not present.
470
471 private:
472  std::unique_ptr<CPDFSDK_AnnotHandlerMgr> m_pAnnotHandlerMgr;
473  std::unique_ptr<CPDFSDK_ActionHandler> m_pActionHandler;
474  std::unique_ptr<IJS_Runtime> m_pJSRuntime;
475  FPDF_FORMFILLINFO* const m_pInfo;
476  CPDFSDK_Document* m_pSDKDoc;
477  UnderlyingDocumentType* const m_pUnderlyingDoc;
478  std::unique_ptr<CFFL_IFormFiller> m_pIFormFiller;
479  std::unique_ptr<IFX_SystemHandler> m_pSysHandler;
480};
481
482class CPDFSDK_Document {
483 public:
484  CPDFSDK_Document(UnderlyingDocumentType* pDoc, CPDFDoc_Environment* pEnv);
485  ~CPDFSDK_Document();
486
487  CPDFSDK_InterForm* GetInterForm();
488
489  // Gets the document object for the next layer down; for master this is
490  // a CPDF_Document, but for XFA it is a CPDFXFA_Document.
491  UnderlyingDocumentType* GetUnderlyingDocument() const {
492#ifdef PDF_ENABLE_XFA
493    return GetXFADocument();
494#else   // PDF_ENABLE_XFA
495    return GetPDFDocument();
496#endif  // PDF_ENABLE_XFA
497  }
498
499  // Gets the CPDF_Document, either directly in master, or from the
500  // CPDFXFA_Document for XFA.
501  CPDF_Document* GetPDFDocument() const {
502#ifdef PDF_ENABLE_XFA
503    return m_pDoc ? m_pDoc->GetPDFDoc() : nullptr;
504#else   // PDF_ENABLE_XFA
505    return m_pDoc;
506#endif  // PDF_ENABLE_XFA
507  }
508
509#ifdef PDF_ENABLE_XFA
510  // Gets the XFA document directly (XFA-only).
511  CPDFXFA_Document* GetXFADocument() const { return m_pDoc; }
512
513  int GetPageViewCount() const { return m_pageMap.size(); }
514#endif  // PDF_ENABLE_XFA
515
516  CPDFSDK_PageView* GetPageView(UnderlyingPageType* pPage,
517                                FX_BOOL ReNew = TRUE);
518  CPDFSDK_PageView* GetPageView(int nIndex);
519  CPDFSDK_PageView* GetCurrentView();
520  void RemovePageView(UnderlyingPageType* pPage);
521  void UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot);
522
523  CPDFSDK_Annot* GetFocusAnnot();
524
525  IJS_Runtime* GetJsRuntime();
526
527  FX_BOOL SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag = 0);
528  FX_BOOL KillFocusAnnot(FX_UINT nFlag = 0);
529
530  FX_BOOL ExtractPages(const CFX_WordArray& arrExtraPages,
531                       CPDF_Document* pDstDoc);
532  FX_BOOL InsertPages(int nInsertAt,
533                      const CPDF_Document* pSrcDoc,
534                      const CFX_WordArray& arrSrcPages);
535  FX_BOOL ReplacePages(int nPage,
536                       const CPDF_Document* pSrcDoc,
537                       const CFX_WordArray& arrSrcPages);
538
539  void OnCloseDocument();
540
541  int GetPageCount() { return m_pDoc->GetPageCount(); }
542  FX_BOOL GetPermissions(int nFlag);
543  FX_BOOL GetChangeMark() { return m_bChangeMask; }
544  void SetChangeMark() { m_bChangeMask = TRUE; }
545  void ClearChangeMark() { m_bChangeMask = FALSE; }
546  CFX_WideString GetPath();
547  UnderlyingPageType* GetPage(int nIndex);
548  CPDFDoc_Environment* GetEnv() { return m_pEnv; }
549  void ProcJavascriptFun();
550  FX_BOOL ProcOpenAction();
551  CPDF_OCContext* GetOCContext();
552
553 private:
554  std::map<UnderlyingPageType*, CPDFSDK_PageView*> m_pageMap;
555  UnderlyingDocumentType* m_pDoc;
556  std::unique_ptr<CPDFSDK_InterForm> m_pInterForm;
557  CPDFSDK_Annot* m_pFocusAnnot;
558  CPDFDoc_Environment* m_pEnv;
559  std::unique_ptr<CPDF_OCContext> m_pOccontent;
560  FX_BOOL m_bChangeMask;
561  FX_BOOL m_bBeingDestroyed;
562};
563class CPDFSDK_PageView final {
564 public:
565  CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc, UnderlyingPageType* page);
566  ~CPDFSDK_PageView();
567
568#ifdef PDF_ENABLE_XFA
569  void PageView_OnDraw(CFX_RenderDevice* pDevice,
570                       CFX_Matrix* pUser2Device,
571                       CPDF_RenderOptions* pOptions,
572                       const FX_RECT& pClip);
573#else   // PDF_ENABLE_XFA
574  void PageView_OnDraw(CFX_RenderDevice* pDevice,
575                       CFX_Matrix* pUser2Device,
576                       CPDF_RenderOptions* pOptions);
577#endif  // PDF_ENABLE_XFA
578
579  const CPDF_Annot* GetPDFAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
580  CPDFSDK_Annot* GetFXAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
581  const CPDF_Annot* GetPDFWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
582  CPDFSDK_Annot* GetFXWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
583  CPDFSDK_Annot* GetFocusAnnot();
584  void SetFocusAnnot(CPDFSDK_Annot* pSDKAnnot, FX_UINT nFlag = 0) {
585    m_pSDKDoc->SetFocusAnnot(pSDKAnnot, nFlag);
586  }
587  FX_BOOL KillFocusAnnot(FX_UINT nFlag = 0) {
588    return m_pSDKDoc->KillFocusAnnot(nFlag);
589  }
590  void KillFocusAnnotIfNeeded();
591  FX_BOOL Annot_HasAppearance(CPDF_Annot* pAnnot);
592
593  CPDFSDK_Annot* AddAnnot(CPDF_Dictionary* pDict);
594  CPDFSDK_Annot* AddAnnot(const FX_CHAR* lpSubType, CPDF_Dictionary* pDict);
595  CPDFSDK_Annot* AddAnnot(CPDF_Annot* pPDFAnnot);
596
597  FX_BOOL DeleteAnnot(CPDFSDK_Annot* pAnnot);
598  size_t CountAnnots() const;
599  CPDFSDK_Annot* GetAnnot(size_t nIndex);
600  CPDFSDK_Annot* GetAnnotByDict(CPDF_Dictionary* pDict);
601
602#ifdef PDF_ENABLE_XFA
603  CPDFSDK_Annot* AddAnnot(IXFA_Widget* pPDFAnnot);
604  CPDFSDK_Annot* GetAnnotByXFAWidget(IXFA_Widget* hWidget);
605  CPDFXFA_Page* GetPDFXFAPage() { return m_page; }
606  CPDF_Page* GetPDFPage();
607#else
608  CPDF_Page* GetPDFPage() { return m_page; }
609#endif  // PDF_ENABLE_XFA
610
611  CPDF_Document* GetPDFDocument();
612  CPDFSDK_Document* GetSDKDocument() { return m_pSDKDoc; }
613  FX_BOOL OnLButtonDown(const CPDF_Point& point, FX_UINT nFlag);
614  FX_BOOL OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag);
615#ifdef PDF_ENABLE_XFA
616  FX_BOOL OnRButtonDown(const CPDF_Point& point, FX_UINT nFlag);
617  FX_BOOL OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag);
618#endif  // PDF_ENABLE_XFA
619  FX_BOOL OnChar(int nChar, FX_UINT nFlag);
620  FX_BOOL OnKeyDown(int nKeyCode, int nFlag);
621  FX_BOOL OnKeyUp(int nKeyCode, int nFlag);
622
623  FX_BOOL OnMouseMove(const CPDF_Point& point, int nFlag);
624  FX_BOOL OnMouseWheel(double deltaX,
625                       double deltaY,
626                       const CPDF_Point& point,
627                       int nFlag);
628  bool IsValidAnnot(const CPDF_Annot* p) const;
629  void GetCurrentMatrix(CFX_Matrix& matrix) { matrix = m_curMatrix; }
630  void UpdateRects(CFX_RectArray& rects);
631  void UpdateView(CPDFSDK_Annot* pAnnot);
632  const std::vector<CPDFSDK_Annot*>& GetAnnotList() const {
633    return m_fxAnnotArray;
634  }
635
636  int GetPageIndex();
637  void LoadFXAnnots();
638  void SetValid(FX_BOOL bValid) { m_bValid = bValid; }
639  FX_BOOL IsValid() { return m_bValid; }
640  void SetLock(FX_BOOL bLocked) { m_bLocked = bLocked; }
641  FX_BOOL IsLocked() { return m_bLocked; }
642#ifndef PDF_ENABLE_XFA
643  void TakeOverPage() { m_bTakeOverPage = TRUE; }
644#endif  // PDF_ENABLE_XFA
645
646 private:
647  void PageView_OnHighlightFormFields(CFX_RenderDevice* pDevice,
648                                      CPDFSDK_Widget* pWidget);
649
650  CFX_Matrix m_curMatrix;
651  UnderlyingPageType* m_page;
652  std::unique_ptr<CPDF_AnnotList> m_pAnnotList;
653  std::vector<CPDFSDK_Annot*> m_fxAnnotArray;
654  CPDFSDK_Document* m_pSDKDoc;
655#ifdef PDF_ENABLE_XFA
656  CPDFSDK_Annot* m_CaptureWidget;
657#else  // PDF_ENABLE_XFA
658  CPDFSDK_Widget* m_CaptureWidget;
659  FX_BOOL m_bTakeOverPage;
660#endif  // PDF_ENABLE_XFA
661  FX_BOOL m_bEnterWidget;
662  FX_BOOL m_bExitWidget;
663  FX_BOOL m_bOnWidget;
664  FX_BOOL m_bValid;
665  FX_BOOL m_bLocked;
666};
667
668template <class TYPE>
669class CGW_ArrayTemplate : public CFX_ArrayTemplate<TYPE> {
670 public:
671  CGW_ArrayTemplate() {}
672  ~CGW_ArrayTemplate() {}
673
674  typedef int (*LP_COMPARE)(TYPE p1, TYPE p2);
675
676  void Sort(LP_COMPARE pCompare, FX_BOOL bAscent = TRUE) {
677    int nSize = this->GetSize();
678    QuickSort(0, nSize - 1, bAscent, pCompare);
679  }
680
681 private:
682  void QuickSort(FX_UINT nStartPos,
683                 FX_UINT nStopPos,
684                 FX_BOOL bAscend,
685                 LP_COMPARE pCompare) {
686    if (nStartPos >= nStopPos)
687      return;
688
689    if ((nStopPos - nStartPos) == 1) {
690      TYPE Value1 = this->GetAt(nStartPos);
691      TYPE Value2 = this->GetAt(nStopPos);
692
693      int iGreate = (*pCompare)(Value1, Value2);
694      if ((bAscend && iGreate > 0) || (!bAscend && iGreate < 0)) {
695        this->SetAt(nStartPos, Value2);
696        this->SetAt(nStopPos, Value1);
697      }
698      return;
699    }
700
701    FX_UINT m = nStartPos + (nStopPos - nStartPos) / 2;
702    FX_UINT i = nStartPos;
703
704    TYPE Value = this->GetAt(m);
705
706    while (i < m) {
707      TYPE temp = this->GetAt(i);
708
709      int iGreate = (*pCompare)(temp, Value);
710      if ((bAscend && iGreate > 0) || (!bAscend && iGreate < 0)) {
711        this->InsertAt(m + 1, temp);
712        this->RemoveAt(i);
713        m--;
714      } else {
715        i++;
716      }
717    }
718
719    FX_UINT j = nStopPos;
720
721    while (j > m) {
722      TYPE temp = this->GetAt(j);
723
724      int iGreate = (*pCompare)(temp, Value);
725      if ((bAscend && iGreate < 0) || (!bAscend && iGreate > 0)) {
726        this->RemoveAt(j);
727        this->InsertAt(m, temp);
728        m++;
729      } else {
730        j--;
731      }
732    }
733
734    if (nStartPos < m)
735      QuickSort(nStartPos, m, bAscend, pCompare);
736    if (nStopPos > m)
737      QuickSort(m, nStopPos, bAscend, pCompare);
738  }
739};
740
741#endif  // FPDFSDK_INCLUDE_FSDK_MGR_H_
742