31c12a4e9599ea512f80b52d8d14b4f19d01d504
[pdfium.git] / fpdfsdk / include / fsdk_mgr.h
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_MGR_H
8 #define _FPDFSDK_MGR_H
9
10 #include "fsdk_common.h"
11 #include "fsdk_define.h"
12 #include "fx_systemhandler.h"
13 #include "fpdfxfa/fpdfxfa_doc.h"
14 #include "fpdfxfa/fpdfxfa_page.h"
15 #include "fsdk_baseannot.h"
16 #include "fsdk_baseform.h"
17 #include "fpdfformfill.h"
18 #include "fsdk_annothandler.h"
19 #include "fsdk_actionhandler.h"
20
21 //cross platform keycode and events define.
22 #include "fpdf_fwlevent.h"
23
24
25 class CPDFSDK_Document;
26 class CPDFSDK_PageView;
27 class CPDFSDK_Annot;
28 class CFFL_IFormFiller;
29 class CPDFSDK_Widget;
30 class IFX_SystemHandler;
31 class CPDFSDK_ActionHandler;
32 class CJS_RuntimeFactory;
33
34 #include "javascript/IJavaScript.h"
35
36 class CPDFDoc_Environment FX_FINAL
37 {
38 public:
39         CPDFDoc_Environment(CPDFXFA_Document * pDoc);
40         ~CPDFDoc_Environment();
41
42         int RegAppHandle(FPDF_FORMFILLINFO* pFFinfo);
43
44         virtual void            Release()
45         {
46                 if (m_pInfo && m_pInfo->Release)
47                         m_pInfo->Release(m_pInfo);
48                 delete this;
49         }
50         void FFI_Invalidate(FPDF_PAGE page, double left, double top, double right, double bottom)
51         {
52                 if (m_pInfo && m_pInfo->FFI_Invalidate)
53                 {
54                         m_pInfo->FFI_Invalidate(m_pInfo, page, left, top, right, bottom);
55                 }
56         }
57         void FFI_OutputSelectedRect(FPDF_PAGE page, double left, double top, double right, double bottom)
58         {
59                 if (m_pInfo && m_pInfo->FFI_OutputSelectedRect)
60                 {
61                         m_pInfo->FFI_OutputSelectedRect(m_pInfo, page, left, top, right, bottom);
62                 }
63         }
64
65         void FFI_SetCursor(int nCursorType)
66         {
67                 if (m_pInfo && m_pInfo->FFI_SetCursor)
68                 {
69                         m_pInfo->FFI_SetCursor(m_pInfo, nCursorType);
70                 }
71         }
72
73         int  FFI_SetTimer(int uElapse, TimerCallback lpTimerFunc)
74         {
75                 if (m_pInfo && m_pInfo->FFI_SetTimer)
76                 {
77                         return m_pInfo->FFI_SetTimer(m_pInfo, uElapse, lpTimerFunc);
78                 }
79                 return -1;
80         }
81
82         void FFI_KillTimer(int nTimerID)
83         {
84                 if (m_pInfo && m_pInfo->FFI_KillTimer)
85                 {
86                         m_pInfo->FFI_KillTimer(m_pInfo, nTimerID);
87                 }
88         }
89         FX_SYSTEMTIME FFI_GetLocalTime()
90         {
91                 FX_SYSTEMTIME fxtime;
92                 if(m_pInfo && m_pInfo->FFI_GetLocalTime)
93                 {
94                         FPDF_SYSTEMTIME systime = m_pInfo->FFI_GetLocalTime(m_pInfo);
95                         fxtime.wDay = systime.wDay;
96                         fxtime.wDayOfWeek = systime.wDayOfWeek;
97                         fxtime.wHour = systime.wHour;
98                         fxtime.wMilliseconds = systime.wMilliseconds;
99                         fxtime.wMinute = systime.wMinute;
100                         fxtime.wMonth = systime.wMonth;
101                         fxtime.wSecond = systime.wSecond;
102                         fxtime.wYear = systime.wYear;
103                 }
104                 return fxtime;
105         }
106
107         void FFI_OnChange()
108         {
109                 if(m_pInfo && m_pInfo->FFI_OnChange)
110                 {
111                         m_pInfo->FFI_OnChange(m_pInfo);
112                 }
113         }
114
115         FX_BOOL FFI_IsSHIFTKeyDown(FX_DWORD nFlag)
116         {
117
118                 return (nFlag & FWL_EVENTFLAG_ShiftKey) != 0;
119         }
120         FX_BOOL FFI_IsCTRLKeyDown(FX_DWORD nFlag)
121         {
122
123                 return (nFlag & FWL_EVENTFLAG_ControlKey) != 0;
124         }
125         FX_BOOL FFI_IsALTKeyDown(FX_DWORD nFlag)
126         {
127
128                 return (nFlag & FWL_EVENTFLAG_AltKey) != 0;
129         }
130         FX_BOOL FFI_IsINSERTKeyDown(FX_DWORD nFlag)
131         {
132                 return FALSE;
133         }
134
135         int JS_appAlert(FX_LPCWSTR Msg, FX_LPCWSTR Title, FX_UINT Type, FX_UINT Icon)
136         {
137                 if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert)
138                 {
139                         CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
140                         CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
141                         FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
142                         FPDF_WIDESTRING pTitle = (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
143                         int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg, pTitle, Type, Icon);
144                         bsMsg.ReleaseBuffer();
145                         bsTitle.ReleaseBuffer();
146                         return ret;
147                 }
148                 return -1;
149         }
150
151         int JS_appResponse(FX_LPCWSTR Question, FX_LPCWSTR Title, FX_LPCWSTR Default, FX_LPCWSTR cLabel, FPDF_BOOL bPassword, void* response, int length)
152         {
153                 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_response)
154                 {
155                         CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
156                         CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
157                         CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
158                         CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
159                         FPDF_WIDESTRING pQuestion = (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
160                         FPDF_WIDESTRING pTitle = (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
161                         FPDF_WIDESTRING pDefault = (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
162                         FPDF_WIDESTRING pLabel = (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
163                         int ret = m_pInfo->m_pJsPlatform->app_response(m_pInfo->m_pJsPlatform, pQuestion, pTitle,
164                                 pDefault, pLabel, bPassword, response, length);
165                         bsQuestion.ReleaseBuffer();
166                         bsTitle.ReleaseBuffer();
167                         bsDefault.ReleaseBuffer();
168                         bsLabel.ReleaseBuffer();
169                         return ret;
170                 }
171                 return -1;
172         }
173
174         void JS_appBeep(int nType)
175         {
176                 if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_beep)
177                 {
178                         m_pInfo->m_pJsPlatform->app_beep(m_pInfo->m_pJsPlatform, nType);
179                 }
180         }
181
182         CFX_WideString JS_fieldBrowse()
183         {
184                 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Field_browse)
185                 {
186                         int nRequiredLen = m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, NULL, 0);
187                         if (nRequiredLen <= 0)
188                                 return L"";
189
190                         char* pbuff = new char[nRequiredLen];
191                         if (!pbuff)
192                                 return L"";
193
194                         memset(pbuff, 0, nRequiredLen);
195                         int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, pbuff, nRequiredLen);
196                         if (nActualLen <= 0 || nActualLen > nRequiredLen)
197                         {
198                                 delete[] pbuff;
199                                 return L"";
200                         }
201                         CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
202                         CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
203                         delete[] pbuff;
204                         return wsRet;
205                 }
206                 return L"";
207         }
208
209         CFX_WideString JS_docGetFilePath()
210         {
211                 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_getFilePath)
212                 {
213                         int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(m_pInfo->m_pJsPlatform, NULL, 0);
214                         if (nRequiredLen <= 0)
215                                 return L"";
216
217                         char* pbuff = new char[nRequiredLen];
218                         if (!pbuff)
219                                 return L"";
220
221                         memset(pbuff, 0, nRequiredLen);
222                         int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(m_pInfo->m_pJsPlatform, pbuff, nRequiredLen);
223                         if (nActualLen <= 0 || nActualLen > nRequiredLen)
224                         {
225                                 delete[] pbuff;
226                                 return L"";
227                         }
228                         CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
229                         CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
230                         delete[] pbuff;
231                         return wsRet;
232                 }
233                 return L"";
234         }
235
236         void JS_docSubmitForm(void* formData, int length, FX_LPCWSTR URL)
237         {
238                 if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_submitForm)
239                 {
240                         CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
241                         FPDF_WIDESTRING pDestination = (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
242                         m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData, length, pDestination);
243                         bsDestination.ReleaseBuffer();
244                 }
245         }
246
247         void JS_docmailForm(void* mailData, int length, FPDF_BOOL bUI,FX_LPCWSTR To, FX_LPCWSTR Subject, FX_LPCWSTR CC, FX_LPCWSTR BCC, FX_LPCWSTR Msg)
248         {
249                 if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail)
250                 {
251                         CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
252                         CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
253                         CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
254                         CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
255                         CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
256                         FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
257                         FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
258                         FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
259                         FPDF_WIDESTRING pSubject = (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
260                         FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
261                         m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length, bUI, pTo, pSubject,
262                                 pCC, pBcc, pMsg);
263                         bsTo.ReleaseBuffer();
264                         bsCC.ReleaseBuffer();
265                         bsBcc.ReleaseBuffer();
266                         bsSubject.ReleaseBuffer();
267                         bsMsg.ReleaseBuffer();
268                 }
269         }
270
271         void JS_docprint(FPDF_BOOL bUI , int nStart, int nEnd, FPDF_BOOL bSilent ,FPDF_BOOL bShrinkToFit,FPDF_BOOL bPrintAsImage ,FPDF_BOOL bReverse ,FPDF_BOOL bAnnotations)
272         {
273                 if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_print)
274                 {
275                         m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd, bSilent, bShrinkToFit, bPrintAsImage, bReverse, bAnnotations);
276                 }
277         }
278         void JS_docgotoPage(int nPageNum)
279         {
280                 if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_gotoPage)
281                 {
282                         m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum);
283                 }
284         }
285
286         FPDF_PAGE       FFI_GetPage(FPDF_DOCUMENT document,int nPageIndex)
287         {
288                 if(m_pInfo && m_pInfo->FFI_GetPage)
289                 {
290                         return m_pInfo->FFI_GetPage(m_pInfo, document, nPageIndex);
291                 }
292                 return NULL;
293         }
294
295         FPDF_PAGE FFI_GetCurrentPage(FPDF_DOCUMENT document)
296         {
297                 if(m_pInfo && m_pInfo->FFI_GetCurrentPage)
298                 {
299                         return m_pInfo->FFI_GetCurrentPage(m_pInfo, document);
300                 }
301                 return NULL;
302         }
303
304         int     FFI_GetRotation(FPDF_PAGE page)
305         {
306                 if(m_pInfo && m_pInfo->FFI_GetRotation)
307                 {
308                         return m_pInfo->FFI_GetRotation(m_pInfo, page);
309                 }
310                 return 0;
311         }
312         void    FFI_ExecuteNamedAction(FX_LPCSTR namedAction)
313         {
314                 if(m_pInfo && m_pInfo->FFI_ExecuteNamedAction)
315                 {
316                         m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction);
317                 }
318         }
319         void    FFI_OnSetFieldInputFocus(void* field,FPDF_WIDESTRING focusText, FPDF_DWORD nTextLen, FX_BOOL bFocus)
320         {
321                 if(m_pInfo && m_pInfo->FFI_SetTextFieldFocus)
322                 {
323                         m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus);
324                 }
325         }
326
327         void    FFI_DoURIAction(FX_LPCSTR bsURI)
328         {
329                 if(m_pInfo && m_pInfo->FFI_DoURIAction)
330                 {
331                         m_pInfo->FFI_DoURIAction(m_pInfo, bsURI);
332                 }
333         }
334
335         void    FFI_DoGoToAction(int nPageIndex, int zoomMode, float* fPosArray, int sizeOfArray)
336         {
337                 if(m_pInfo && m_pInfo->FFI_DoGoToAction)
338                 {
339                         m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray, sizeOfArray);
340                 }
341         }
342
343         void    FFI_DisplayCaret(FPDF_PAGE page, FPDF_BOOL bVisible, double left, double top, double right, double bottom)
344         {
345                 if (m_pInfo && m_pInfo->FFI_DisplayCaret)
346                 {
347                         m_pInfo->FFI_DisplayCaret(m_pInfo, page, bVisible, left, top, right, bottom);
348                 }
349         }
350         int     FFI_GetCurrentPageIndex(FPDF_DOCUMENT document)
351         {
352                 if (m_pInfo && m_pInfo->FFI_GetCurrentPageIndex)
353                 {
354                         m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document);
355                 }
356                 return -1;
357         }
358         int     FFI_SetCurrentPage(FPDF_DOCUMENT document, int iCurPage)
359         {
360                 if (m_pInfo && m_pInfo->FFI_SetCurrentPage)
361                 {
362                         m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage);
363                 }
364                 return -1;
365         }
366
367         CFX_WideString  FFI_GetAppName()
368         {
369                 return CFX_WideString(L"Acrobat");
370
371                 //if (m_pInfo && m_pInfo->FFI_GetAppName)
372                 //{
373                 //      int nLen = m_pInfo->FFI_GetAppName(m_pInfo, NULL, 0);
374                 //      if(nLen <= 0)
375                 //              return L"";
376                 //      char* pbuff = new char[nLen];
377                 //      if(pbuff)
378                 //              memset(pbuff, 0, nLen);
379                 //      else
380                 //              return L"";
381                 //      nLen = m_pInfo->FFI_GetAppName(m_pInfo, pbuff, nLen);
382                 //      CFX_ByteString bsRet = CFX_ByteString(pbuff, nLen);
383                 //      CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
384                 //      delete[] pbuff;
385                 //      return wsRet;
386                 //}
387
388                 //return L"";
389         }
390
391         CFX_WideString  FFI_GetPlatform()
392         {
393                 if (m_pInfo && m_pInfo->FFI_GetPlatform)
394                 {
395                         int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, NULL, 0);
396                         if (nRequiredLen <= 0)
397                                 return L"";
398
399                         char* pbuff = new char[nRequiredLen];
400                         if (!pbuff)
401                                 return L"";
402
403                         memset(pbuff, 0, nRequiredLen);
404                         int nActualLen = m_pInfo->FFI_GetPlatform(m_pInfo, pbuff, nRequiredLen);
405                         if (nActualLen <= 0 || nActualLen > nRequiredLen)
406                         {
407                                 delete[] pbuff;
408                                 return L"";
409                         }
410                         CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
411                         CFX_WideString wsRet = CFX_WideString::FromUTF16LE((unsigned short*)bsRet.GetBuffer(bsRet.GetLength()), bsRet.GetLength()/sizeof(unsigned short));
412                         delete[] pbuff;
413                         return wsRet;
414                 }
415                 return L"";
416         }
417
418         void FFI_GotoURL(FPDF_DOCUMENT document, FX_WSTR wsURL, FX_BOOL bAppend)
419         {
420                 if (m_pInfo && m_pInfo->FFI_GotoURL)
421                 {
422                         CFX_ByteString bsTo = CFX_WideString(wsURL).UTF16LE_Encode();
423                         FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength());
424                         m_pInfo->FFI_GotoURL(m_pInfo, document, pTo);
425                         bsTo.ReleaseBuffer();
426                 }
427         }
428         void FFI_GetURL(FPDF_DOCUMENT document, CFX_WideString& wsURL)
429         {
430                 wsURL = CFX_WideString();
431                 return;
432                 //if (m_pInfo && m_pInfo->FFI_GetURL)
433                 //{
434                 //      FPDF_WIDESTRING url = m_pInfo->FFI_GetURL(m_pInfo, document);
435                 //      CFX_WideString tmp;
436                 //      tmp.FromUTF16LE(url);
437                 //      wsURL = tmp;
438                 //}
439         }
440
441         void FFI_AddDoRecord(FPDF_DOCUMENT document, FPDF_WIDGET hWidget)
442         {
443                 //if (m_pInfo && m_pInfo->FFI_AddDoRecord)
444                 //{
445                 //      m_pInfo->FFI_AddDoRecord(m_pInfo, document, hWidget);
446                 //}
447         }
448         void FFI_PageEvent(FPDF_PAGE page, FPDF_DWORD flag)
449         {
450                 //if (m_pInfo && m_pInfo->FFI_PageEvent)
451                 //{
452                 //      m_pInfo->FFI_PageEvent(m_pInfo, page, flag);
453                 //}
454         }
455
456         void FFI_GetPageViewRect(FPDF_PAGE page, FS_RECTF& dstRect)
457         {
458                 if (m_pInfo && m_pInfo->FFI_GetPageViewRect)
459                 {
460                         double left;
461                         double top;
462                         double right;
463                         double bottom;
464                         m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom);
465
466                         dstRect.left = static_cast<float>(left);
467                         dstRect.top = static_cast<float>(top < bottom ? bottom : top);
468                         dstRect.bottom = static_cast<float>(top < bottom ? top : bottom);
469                         dstRect.right = static_cast<float>(right);
470                 }
471         }
472
473
474         FX_BOOL  FFI_PopupMenu(FPDF_PAGE page, FPDF_WIDGET hWidget, int menuFlag, CFX_PointF ptPopup, const CFX_PointF* pRectExclude)
475         {
476                 if (m_pInfo && m_pInfo->FFI_PopupMenu)
477                 {
478                         return m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, ptPopup.x, ptPopup.y);
479                 }
480                 return FALSE;
481         }
482
483         void    FFI_Alert(FPDF_WIDESTRING Msg, FPDF_WIDESTRING Title, int Type, int Icon)
484         {
485                 if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert)
486                 {
487                         m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type, Icon);
488                 }
489         }
490         void    FFI_EmailTo(FPDF_FILEHANDLER* fileHandler, FPDF_WIDESTRING pTo, FPDF_WIDESTRING pSubject, FPDF_WIDESTRING pCC, FPDF_WIDESTRING pBcc, FPDF_WIDESTRING pMsg)
491         {
492                 if(m_pInfo && m_pInfo->FFI_EmailTo)
493                 {
494                         m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg);
495                 }
496         }
497         void    FFI_UploadTo(FPDF_FILEHANDLER* fileHandler, int fileFlag, FPDF_WIDESTRING uploadTo)
498         {
499                 if(m_pInfo && m_pInfo->FFI_UploadTo)
500                 {
501                         m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo);
502                 }
503         }
504         FPDF_FILEHANDLER* FFI_OpenFile(int fileType, FPDF_WIDESTRING wsURL, const char* mode)
505         {
506                 if(m_pInfo && m_pInfo->FFI_OpenFile)
507                 {
508                         return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode);
509                 }
510                 return NULL;
511         }
512         CFX_WideString FFI_GetFilePath(FPDF_FILEHANDLER* pFileHandler)
513         {
514 #if 0
515                 if (m_pInfo && m_pInfo->FFI_GetFilePath)
516                 {
517                         int nRequiredLen = m_pInfo->FFI_GetFilePath(m_pInfo, pFileHandler, NULL, 0);
518                         if (nRequiredLen <= 0)
519                                 return L"";
520
521                         char* pbuff = new char[nRequiredLen];
522                         if (!pbuff)
523                                 return L"";
524
525                         memset(pbuff, 0, nRequiredLen);
526                         int nActualLen = m_pInfo->FFI_GetFilePath(m_pInfo, pFileHandler, pbuff, nRequiredLen);
527                         if (nActualLen <= 0 || nActualLen > nRequiredLen)
528                         {
529                                 delete[] pbuff;
530                                 return L"";
531                         }
532                         CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
533                         CFX_WideString wsRet = CFX_WideString::FromUTF16LE((unsigned short*)bsRet.GetBuffer(bsRet.GetLength()), bsRet.GetLength()/sizeof(unsigned short));
534                         delete[] pbuff;
535                         return wsRet;
536                 }
537                 return L"";
538 #else
539                 return L"";
540 #endif
541         }
542         int             FFI_GetDocumentCount()
543         {
544                 //if (m_pInfo && m_pInfo->FFI_GetDocumentCount)
545                 //{
546                         //return m_pInfo->FFI_GetDocumentCount(m_pInfo);
547                 //}
548                 return 0;
549         }
550
551         int             FFI_GetCurDocument()
552         {
553                 //if (m_pInfo && m_pInfo->FFI_GetCurDocumentIndex)
554                 //{
555                 //      return m_pInfo->FFI_GetCurDocumentIndex(m_pInfo);
556                 //}
557                 return 0;
558         }
559
560         IFX_FileRead*   FFI_DownloadFromURL(FX_LPCWSTR url)
561         {
562                 if (m_pInfo && m_pInfo->FFI_DownloadFromURL)
563                 {
564                         CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode();
565                         FPDF_WIDESTRING wsURL = (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength());
566
567                         FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL);
568
569                         CFPDF_FileStream* fileStream = FX_NEW CFPDF_FileStream(fileHandler);
570                         return fileStream;
571                 }
572                 return NULL;
573         }
574
575         CFX_WideString  FFI_PostRequestURL(FX_LPCWSTR wsURL, FX_LPCWSTR wsData, FX_LPCWSTR wsContentType, FX_LPCWSTR wsEncode, FX_LPCWSTR wsHeader)
576         {
577                 if (m_pInfo && m_pInfo->FFI_PostRequestURL)
578                 {
579                         CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
580                         FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
581
582                         CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
583                         FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
584
585                         CFX_ByteString bsContentType = CFX_WideString(wsContentType).UTF16LE_Encode();
586                         FPDF_WIDESTRING contentType = (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength());
587
588                         CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
589                         FPDF_WIDESTRING encode = (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
590
591                         CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode();
592                         FPDF_WIDESTRING header = (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength());
593
594                         FPDF_BSTR respone;
595                         FPDF_BStr_Init(&respone);
596                         FPDF_BOOL bRet = m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header, &respone);
597
598                         CFX_WideString wsRet = CFX_WideString::FromUTF16LE((unsigned short*)respone.str, respone.len/sizeof(unsigned short));
599                         FPDF_BStr_Clear(&respone);
600
601                         return wsRet;
602                 }
603                 return L"";
604         }
605
606         FPDF_BOOL       FFI_PutRequestURL(FX_LPCWSTR wsURL, FX_LPCWSTR wsData, FX_LPCWSTR wsEncode)
607         {
608                 if (m_pInfo && m_pInfo->FFI_PutRequestURL)
609                 {
610                         CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
611                         FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
612
613                         CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
614                         FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
615
616                         CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
617                         FPDF_WIDESTRING encode = (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
618
619                         return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode);
620                 }
621                 return FALSE;
622         }
623
624         FPDF_BOOL       FFI_ShowFileDialog(FX_LPCWSTR wsTitle, FX_LPCWSTR wsFilter, CFX_WideStringArray &wsPathArr, FX_BOOL bOpen)
625         {
626                 /*CFX_ByteString bsTitle = CFX_WideString(wsTitle).UTF16LE_Encode();
627                 FPDF_WIDESTRING title = (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
628
629                 CFX_ByteString bsFilter = CFX_WideString(wsFilter).UTF16LE_Encode();
630                 FPDF_WIDESTRING filter = (FPDF_WIDESTRING)bsFilter.GetBuffer(bsFilter.GetLength());
631
632                 CFX_ByteStringArray* strPathArr = FX_NEW CFX_ByteStringArray();
633                 if (m_pInfo && m_pInfo->FFI_ShowFileDialog)
634                 {
635                         FPDF_BOOL bRet = m_pInfo->FFI_ShowFileDialog(m_pInfo, title, filter, bOpen, (FPDF_STRINGHANDLE)strPathArr);
636                         if (bRet)
637                         {
638                                 int count = strPathArr->GetSize();
639                                 for (int i=0; i<count; i++)
640                                 {
641                                         CFX_ByteString bsPath = *strPathArr->GetDataPtr(i);
642                                         CFX_WideString wsPath = CFX_WideString::FromUTF16LE((unsigned short*)bsPath.GetBuffer(bsPath.GetLength()), bsPath.GetLength()/sizeof(unsigned short));
643                                         if (!wsPath.IsEmpty())
644                                                 wsPathArr.Add(wsPath);
645                                         wsPath = L"";
646                                 }
647                                 return TRUE;
648                         }
649
650                         return FALSE;
651                 }
652                 */
653
654                 return FALSE;
655         }
656         CFX_WideString FFI_GetLanguage()
657         {
658                 if (m_pInfo && m_pInfo->FFI_GetLanguage)
659                 {
660                         int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, NULL, 0);
661                         if (nRequiredLen <= 0)
662                                 return L"";
663
664                         char* pbuff = new char[nRequiredLen];
665                         if (!pbuff)
666                                 return L"";
667
668                         memset(pbuff, 0, nRequiredLen);
669                         int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen);
670                         if (nActualLen <= 0 || nActualLen > nRequiredLen)
671                         {
672                                 delete[] pbuff;
673                                 return L"";
674                         }
675                         CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
676                         CFX_WideString wsRet = CFX_WideString::FromUTF16LE((unsigned short*)bsRet.GetBuffer(bsRet.GetLength()), bsRet.GetLength()/sizeof(unsigned short));
677                         delete[] pbuff;
678                         return wsRet;
679                 }
680                 return L"";
681         }
682
683         FX_BOOL                         IsJSInitiated();
684
685         void                            SetCurrentDoc(CPDFSDK_Document* pFXDoc) {m_pSDKDoc = pFXDoc;}
686         CPDFSDK_Document*       GetCurrentDoc();
687         CPDFXFA_Document*       GetPDFXFADocument() {return m_pDoc;}
688
689         CFX_ByteString          GetAppName() {return "";}
690
691         CFFL_IFormFiller*       GetIFormFiller();
692         IFX_SystemHandler*      GetSysHandler() {return m_pSysHandler;}
693
694         CPDFSDK_AnnotHandlerMgr* GetAnnotHandlerMgr();
695         IFXJS_Runtime*  GetJSRuntime();
696         CPDFSDK_ActionHandler* GetActionHander();
697         FPDF_FORMFILLINFO* GetFormFillInfo() {return m_pInfo;}
698
699 private:
700         CPDFSDK_AnnotHandlerMgr* m_pAnnotHandlerMgr;
701         CPDFSDK_ActionHandler*  m_pActionHandler;
702         IFXJS_Runtime*  m_pJSRuntime;
703         FPDF_FORMFILLINFO*      m_pInfo;
704         CPDFSDK_Document* m_pSDKDoc;
705         CPDFXFA_Document* m_pDoc;
706
707         CFFL_IFormFiller* m_pIFormFiller;
708         IFX_SystemHandler* m_pSysHandler;
709
710 public:
711         //CJS_RuntimeFactory*  m_pJSRuntimeFactory;
712 };
713
714
715
716 class CPDFSDK_InterForm;
717 class CPDFSDK_Document
718 {
719 public:
720         CPDFSDK_Document(CPDFXFA_Document* pDoc, CPDFDoc_Environment* pEnv);
721         ~CPDFSDK_Document();
722
723         CPDFSDK_InterForm*              GetInterForm() ;
724         CPDFXFA_Document*               GetDocument() {return m_pDoc;}
725
726         void                                    InitPageView();
727         int                                             GetPageViewCount(){return m_pageMap.GetCount();}
728         void                                    AddPageView(CPDFXFA_Page* pPDFXFAPage, CPDFSDK_PageView* pPageView);
729         CPDFSDK_PageView*               GetPageView(CPDFXFA_Page* pPDFXFAPage, FX_BOOL ReNew = TRUE);
730         CPDFSDK_PageView*               GetPageView(int nIndex);
731         CPDFSDK_PageView*               GetCurrentView();
732         void                                    ReMovePageView(CPDFXFA_Page* pPDFPage);
733         void                                    UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot);
734
735         CPDFSDK_Annot*                  GetFocusAnnot();
736
737         IFXJS_Runtime *                 GetJsRuntime();
738
739         FX_BOOL                                 SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag = 0);
740         FX_BOOL                                 KillFocusAnnot(FX_UINT nFlag = 0);
741
742         FX_BOOL                                 ExtractPages(const CFX_WordArray &arrExtraPages, CPDF_Document* pDstDoc);
743         FX_BOOL                                 InsertPages(int nInsertAt, const CPDF_Document* pSrcDoc, const CFX_WordArray &arrSrcPages);
744         FX_BOOL                                 DeletePages(int nStart, int nCount);
745         FX_BOOL                                 ReplacePages(int nPage, const CPDF_Document* pSrcDoc, const CFX_WordArray &arrSrcPages);
746
747         void                                    OnCloseDocument();
748
749         int                                             GetPageCount() {return m_pDoc->GetPageCount();}
750         FX_BOOL                                 GetPermissions(int nFlag);
751         FX_BOOL                                 GetChangeMark() {return m_bChangeMask;}
752         void                                    SetChangeMark() {m_bChangeMask = TRUE;}
753         void                                    ClearChangeMark() {m_bChangeMask= FALSE;}
754         CFX_WideString                  GetPath() ;
755         CPDFXFA_Page*                   GetPage(int nIndex);
756         CPDFDoc_Environment *   GetEnv() {return m_pEnv; }
757         void                                ProcJavascriptFun();
758         FX_BOOL                                 ProcOpenAction();
759         CPDF_OCContext*                 GetOCContext();
760 private:
761         CFX_MapPtrTemplate<CPDFXFA_Page*, CPDFSDK_PageView*> m_pageMap;
762         CPDFXFA_Document*                       m_pDoc;
763         CPDFSDK_InterForm*              m_pInterForm;
764         CPDFSDK_Annot*                  m_pFocusAnnot;
765         CPDFDoc_Environment *   m_pEnv;
766         CPDF_OCContext *                m_pOccontent;
767         FX_BOOL                                 m_bChangeMask;
768 };
769
770 class CPDFSDK_PageView FX_FINAL
771 {
772 public:
773         CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,CPDFXFA_Page* page);
774         ~CPDFSDK_PageView();
775         void PageView_OnDraw(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,CPDF_RenderOptions* pOptions, FX_RECT* pClip = NULL) ;
776         CPDF_Annot*                                             GetPDFAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
777         CPDFSDK_Annot*                                  GetFXAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
778         CPDF_Annot*                                             GetPDFWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
779         CPDFSDK_Annot*                                  GetFXWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
780         CPDFSDK_Annot*                                  GetFocusAnnot() ;
781         void                                                    SetFocusAnnot(CPDFSDK_Annot* pSDKAnnot,FX_UINT nFlag = 0) {m_pSDKDoc->SetFocusAnnot(pSDKAnnot, nFlag);}
782         FX_BOOL                                                 KillFocusAnnot(FX_UINT nFlag = 0) {return m_pSDKDoc->KillFocusAnnot(nFlag);}
783         FX_BOOL                                                 Annot_HasAppearance(CPDF_Annot* pAnnot);
784
785         CPDFSDK_Annot*                                  AddAnnot(CPDF_Dictionary * pDict);
786         CPDFSDK_Annot*                                  AddAnnot(FX_LPCSTR lpSubType,CPDF_Dictionary * pDict);
787         CPDFSDK_Annot*                                  AddAnnot(CPDF_Annot * pPDFAnnot);
788         CPDFSDK_Annot*                                  AddAnnot(XFA_HWIDGET pPDFAnnot);
789         FX_BOOL                                                 DeleteAnnot(CPDFSDK_Annot* pAnnot);
790
791         int                                                             CountAnnots();
792         CPDFSDK_Annot*                                  GetAnnot(int nIndex);
793         CPDFSDK_Annot*                              GetAnnotByDict(CPDF_Dictionary * pDict);
794         CPDFSDK_Annot*                                  GetAnnotByXFAWidget(XFA_HWIDGET hWidget);
795         CPDFXFA_Page*                                   GetPDFXFAPage(){return m_page;}
796         CPDF_Page*                                              GetPDFPage();
797         CPDF_Document*                                  GetPDFDocument();
798         CPDFSDK_Document*                               GetSDKDocument() {return m_pSDKDoc;}
799         FX_BOOL                                 OnLButtonDown(const CPDF_Point & point, FX_UINT nFlag);
800         FX_BOOL                                 OnLButtonUp(const CPDF_Point & point, FX_UINT nFlag);
801         FX_BOOL                                 OnRButtonDown(const CPDF_Point & point, FX_UINT nFlag);
802         FX_BOOL                                 OnRButtonUp(const CPDF_Point & point, FX_UINT nFlag);
803         FX_BOOL                                 OnChar(int nChar, FX_UINT nFlag);
804         FX_BOOL                                 OnKeyDown(int nKeyCode, int nFlag);
805         FX_BOOL                                 OnKeyUp(int nKeyCode, int nFlag);
806
807         FX_BOOL                                 OnMouseMove(const CPDF_Point & point, int nFlag);
808         FX_BOOL                                 OnMouseWheel(double deltaX, double deltaY,const CPDF_Point& point, int nFlag);
809         FX_BOOL                                 IsValidAnnot(FX_LPVOID p);
810         void                                    GetCurrentMatrix(CPDF_Matrix& matrix) {matrix = m_curMatrix;}
811         void                                    UpdateRects(CFX_RectArray& rects);
812         void                                                    UpdateView(CPDFSDK_Annot* pAnnot);
813         CFX_PtrArray*                                   GetAnnotList(){ return &m_fxAnnotArray; }
814
815         int                                             GetPageIndex();
816         void                                                    LoadFXAnnots();
817         void SetValid(FX_BOOL bValid) {m_bValid = bValid;}
818     FX_BOOL IsValid() {return m_bValid;}
819     void SetLock(FX_BOOL bLocked) {m_bLocked= bLocked;}
820     FX_BOOL IsLocked() {return m_bLocked;}
821 private:
822         void PageView_OnHighlightFormFields(CFX_RenderDevice* pDevice, CPDFSDK_Widget* pWidget);
823         CPDF_Matrix m_curMatrix;
824         CPDFXFA_Page* m_page;
825         CPDF_AnnotList* m_pAnnotList;
826         //CPDFSDK_Annot* m_pFocusAnnot;
827         CFX_PtrArray  m_fxAnnotArray;
828         CPDFSDK_Document* m_pSDKDoc;
829         CPDFSDK_Annot* m_CaptureWidget;
830         FX_BOOL m_bEnterWidget;
831         FX_BOOL m_bExitWidget;
832         FX_BOOL m_bOnWidget;
833         FX_BOOL m_bValid;
834     FX_BOOL m_bLocked;
835 };
836
837
838 template<class TYPE>
839 class CGW_ArrayTemplate : public CFX_ArrayTemplate<TYPE>
840 {
841 public:
842         CGW_ArrayTemplate(){}
843         virtual ~CGW_ArrayTemplate(){}
844
845         typedef int (*LP_COMPARE)(TYPE p1, TYPE p2);
846
847         void Sort(LP_COMPARE pCompare, FX_BOOL bAscent = TRUE)
848         {
849                 int nSize = this->GetSize();
850                 QuickSort(0, nSize -1, bAscent, pCompare);
851         }
852
853 private:
854         void QuickSort(FX_UINT nStartPos, FX_UINT nStopPos, FX_BOOL bAscend, LP_COMPARE pCompare)
855         {
856                 if (nStartPos >= nStopPos) return;
857
858                 if ((nStopPos - nStartPos) == 1)
859                 {
860                         TYPE Value1 = this->GetAt(nStartPos);
861                         TYPE Value2 = this->GetAt(nStopPos);
862
863                         int iGreate = (*pCompare)(Value1, Value2);
864                         if ((bAscend && iGreate > 0) || (!bAscend && iGreate < 0))
865                         {
866                                 this->SetAt(nStartPos, Value2);
867                                 this->SetAt(nStopPos, Value1);
868                         }
869                         return;
870                 }
871
872                 FX_UINT m = nStartPos + (nStopPos - nStartPos) / 2;
873                 FX_UINT i = nStartPos;
874
875                 TYPE Value = this->GetAt(m);
876
877                 while (i < m)
878                 {
879                         TYPE temp = this->GetAt(i);
880
881                         int iGreate = (*pCompare)(temp, Value);
882                         if ((bAscend && iGreate > 0) || (!bAscend && iGreate < 0))
883                         {
884                                 this->InsertAt(m+1, temp);
885                                 this->RemoveAt(i);
886                                 m--;
887                         }
888                         else
889                         {
890                                 i++;
891                         }
892                 }
893
894                 FX_UINT j = nStopPos;
895
896                 while (j > m)
897                 {
898                         TYPE temp = this->GetAt(j);
899
900                         int iGreate = (*pCompare)(temp, Value);
901                         if ((bAscend && iGreate < 0) || (!bAscend && iGreate > 0))
902                         {
903                                 this->RemoveAt(j);
904                                 this->InsertAt(m, temp);
905                                 m++;
906                         }
907                         else
908                         {
909                                 j--;
910                         }
911                 }
912
913                 if (nStartPos < m) QuickSort(nStartPos, m, bAscend, pCompare);
914                 if (nStopPos > m) QuickSort(m, nStopPos, bAscend, pCompare);
915         }
916 };
917
918
919 #endif //_FPDFSDK_MGR_H