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