Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[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);//{ m_pInfo  = pFFinfo; return TRUE;}
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         CFX_WideString JS_appbrowseForDoc(FPDF_BOOL bSave, FX_LPCWSTR cFilenameInit)
271         {
272                 //to do....
273                 return L"";
274 //              if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_browseForDoc)
275 //              {
276 //                      CFX_ByteString bsFilenameInit = CFX_WideString(cFilenameInit).UTF16LE_Encode();
277 //                      FPDF_WIDESTRING pFileNameInit = (FPDF_WIDESTRING)bsFilenameInit.GetBuffer(bsFilenameInit.GetLength());
278 // 
279 //                      m_pInfo->m_pJsPlatform->app_browseForDoc(m_pInfo->m_pJsPlatform, pFileNameInit);
280 //                      bsFilenameInit.ReleaseBuffer();
281 //              }
282         }
283
284         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)
285         {
286                 if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_print)
287                 {
288                         m_pInfo->m_pJsPlatform->Doc_print(m_pInfo->m_pJsPlatform, bUI, nStart, nEnd, bSilent, bShrinkToFit, bPrintAsImage, bReverse, bAnnotations);
289                 }
290         }
291         void JS_docgotoPage(int nPageNum)
292         {
293                 if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_gotoPage)
294                 {
295                         m_pInfo->m_pJsPlatform->Doc_gotoPage(m_pInfo->m_pJsPlatform, nPageNum);
296                 }
297         }
298
299         FPDF_PAGE       FFI_GetPage(FPDF_DOCUMENT document,int nPageIndex)
300         {
301                 if(m_pInfo && m_pInfo->FFI_GetPage)
302                 {
303                         return m_pInfo->FFI_GetPage(m_pInfo, document, nPageIndex);
304                 }
305                 return NULL;
306         }
307
308         FPDF_PAGE FFI_GetCurrentPage(FPDF_DOCUMENT document)
309         {
310                 if(m_pInfo && m_pInfo->FFI_GetCurrentPage)
311                 {
312                         return m_pInfo->FFI_GetCurrentPage(m_pInfo, document);
313                 }
314                 return NULL;
315         }
316
317         int     FFI_GetRotation(FPDF_PAGE page)
318         {
319                 if(m_pInfo && m_pInfo->FFI_GetRotation)
320                 {
321                         return m_pInfo->FFI_GetRotation(m_pInfo, page);
322                 }
323                 return 0;
324         }
325         void    FFI_ExecuteNamedAction(FX_LPCSTR namedAction)
326         {
327                 if(m_pInfo && m_pInfo->FFI_ExecuteNamedAction)
328                 {
329                         m_pInfo->FFI_ExecuteNamedAction(m_pInfo, namedAction);
330                 }
331         }
332         void    FFI_OnSetFieldInputFocus(void* field,FPDF_WIDESTRING focusText, FPDF_DWORD nTextLen, FX_BOOL bFocus)
333         {
334                 if(m_pInfo && m_pInfo->FFI_SetTextFieldFocus)
335                 {
336                         m_pInfo->FFI_SetTextFieldFocus(m_pInfo, focusText, nTextLen, bFocus);
337                 }
338         }
339
340         void    FFI_DoURIAction(FX_LPCSTR bsURI)
341         {
342                 if(m_pInfo && m_pInfo->FFI_DoURIAction)
343                 {
344                         m_pInfo->FFI_DoURIAction(m_pInfo, bsURI);
345                 }
346         }
347
348         void    FFI_DoGoToAction(int nPageIndex, int zoomMode, float* fPosArray, int sizeOfArray)
349         {
350                 if(m_pInfo && m_pInfo->FFI_DoGoToAction)
351                 {
352                         m_pInfo->FFI_DoGoToAction(m_pInfo, nPageIndex, zoomMode, fPosArray, sizeOfArray);
353                 }
354         }
355
356         void    FFI_DisplayCaret(FPDF_PAGE page, FPDF_BOOL bVisible, double left, double top, double right, double bottom)
357         {
358                 if (m_pInfo && m_pInfo->FFI_DisplayCaret)
359                 {
360                         m_pInfo->FFI_DisplayCaret(m_pInfo, page, bVisible, left, top, right, bottom);
361                 }
362         }
363         int     FFI_GetCurrentPageIndex(FPDF_DOCUMENT document)
364         {
365                 if (m_pInfo && m_pInfo->FFI_GetCurrentPageIndex)
366                 {
367                         m_pInfo->FFI_GetCurrentPageIndex(m_pInfo, document);
368                 }
369                 return -1;
370         }
371         int     FFI_SetCurrentPage(FPDF_DOCUMENT document, int iCurPage)
372         {
373                 if (m_pInfo && m_pInfo->FFI_SetCurrentPage)
374                 {
375                         m_pInfo->FFI_SetCurrentPage(m_pInfo, document, iCurPage);
376                 }
377                 return -1;
378         }
379
380         CFX_WideString  FFI_GetAppName()
381         {
382                 return CFX_WideString(L"Acrobat");
383
384                 //if (m_pInfo && m_pInfo->FFI_GetAppName)
385                 //{
386                 //      int nLen = m_pInfo->FFI_GetAppName(m_pInfo, NULL, 0);
387                 //      if(nLen <= 0)
388                 //              return L"";
389                 //      char* pbuff = new char[nLen];
390                 //      if(pbuff)
391                 //              memset(pbuff, 0, nLen);
392                 //      else    
393                 //              return L"";
394                 //      nLen = m_pInfo->FFI_GetAppName(m_pInfo, pbuff, nLen);
395                 //      CFX_ByteString bsRet = CFX_ByteString(pbuff, nLen);
396                 //      CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
397                 //      delete[] pbuff;
398                 //      return wsRet;
399                 //}
400
401                 //return L"";
402         }
403
404         CFX_WideString  FFI_GetPlatform()
405         {
406                 if (m_pInfo && m_pInfo->FFI_GetPlatform)
407                 {
408                         int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, NULL, 0);
409                         if (nRequiredLen <= 0)
410                                 return L"";
411
412                         char* pbuff = new char[nRequiredLen];
413                         if (!pbuff)
414                                 return L"";
415
416                         memset(pbuff, 0, nRequiredLen);
417                         int nActualLen = m_pInfo->FFI_GetPlatform(m_pInfo, pbuff, nRequiredLen);
418                         if (nActualLen <= 0 || nActualLen > nRequiredLen)
419                         {
420                                 delete[] pbuff;
421                                 return L"";
422                         }
423                         CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
424                         CFX_WideString wsRet = CFX_WideString::FromUTF16LE((unsigned short*)bsRet.GetBuffer(bsRet.GetLength()), bsRet.GetLength()/sizeof(unsigned short));
425                         delete[] pbuff;
426                         return wsRet;
427                 }
428                 return L"";
429         }
430
431         void FFI_GotoURL(FPDF_DOCUMENT document, FX_WSTR wsURL, FX_BOOL bAppend)
432         {
433                 if (m_pInfo && m_pInfo->FFI_GotoURL)
434                 {
435                         CFX_ByteString bsTo = CFX_WideString(wsURL).UTF16LE_Encode();
436                         FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(wsURL.GetLength());
437                         m_pInfo->FFI_GotoURL(m_pInfo, document, pTo);
438                         bsTo.ReleaseBuffer();
439                 }
440         }
441         void FFI_GetURL(FPDF_DOCUMENT document, CFX_WideString& wsURL)
442         {
443                 wsURL = CFX_WideString();
444                 return;
445                 //if (m_pInfo && m_pInfo->FFI_GetURL)
446                 //{
447                 //      FPDF_WIDESTRING url = m_pInfo->FFI_GetURL(m_pInfo, document);
448                 //      CFX_WideString tmp;
449                 //      tmp.FromUTF16LE(url);
450                 //      wsURL = tmp;
451                 //}
452         }
453
454         void FFI_AddDoRecord(FPDF_DOCUMENT document, FPDF_WIDGET hWidget)
455         {
456                 //if (m_pInfo && m_pInfo->FFI_AddDoRecord)
457                 //{
458                 //      m_pInfo->FFI_AddDoRecord(m_pInfo, document, hWidget);
459                 //}
460         }
461         void FFI_PageEvent(FPDF_PAGE page, FPDF_DWORD flag)
462         {
463                 //if (m_pInfo && m_pInfo->FFI_PageEvent)
464                 //{
465                 //      m_pInfo->FFI_PageEvent(m_pInfo, page, flag);
466                 //}
467         }
468
469         void FFI_GetPageViewRect(FPDF_PAGE page, FS_RECTF& dstRect)
470         {
471                 if (m_pInfo && m_pInfo->FFI_GetPageViewRect)
472                 {
473                         double left; 
474                         double top;
475                         double right;
476                         double bottom;
477                         m_pInfo->FFI_GetPageViewRect(m_pInfo, page, &left, &top, &right, &bottom);
478
479                         dstRect.left = left;
480                         dstRect.top = top < bottom? bottom:top;
481                         dstRect.bottom = top < bottom? top:bottom;
482                         dstRect.right = right;
483                 }
484         }
485
486
487         FX_BOOL  FFI_PopupMenu(FPDF_PAGE page, FPDF_WIDGET hWidget, int menuFlag, CFX_PointF ptPopup, const CFX_PointF* pRectExclude)
488         {
489                 if (m_pInfo && m_pInfo->FFI_PopupMenu)
490                 {
491                         return m_pInfo->FFI_PopupMenu(m_pInfo, page, hWidget, menuFlag, ptPopup.x, ptPopup.y);
492                 }
493                 return FALSE;
494         }
495         
496         void    FFI_Alert(FPDF_WIDESTRING Msg, FPDF_WIDESTRING Title, int Type, int Icon)
497         {
498                 if(m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert)
499                 {
500                         m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, Msg, Title, Type, Icon);
501                 }
502         }
503         void    FFI_EmailTo(FPDF_FILEHANDLER* fileHandler, FPDF_WIDESTRING pTo, FPDF_WIDESTRING pSubject, FPDF_WIDESTRING pCC, FPDF_WIDESTRING pBcc, FPDF_WIDESTRING pMsg)
504         {
505                 if(m_pInfo && m_pInfo->FFI_EmailTo)
506                 {
507                         m_pInfo->FFI_EmailTo(m_pInfo, fileHandler, pTo, pSubject, pCC, pBcc, pMsg);
508                 }
509         }
510         void    FFI_UploadTo(FPDF_FILEHANDLER* fileHandler, int fileFlag, FPDF_WIDESTRING uploadTo)
511         {
512                 if(m_pInfo && m_pInfo->FFI_UploadTo)
513                 {
514                         m_pInfo->FFI_UploadTo(m_pInfo, fileHandler, fileFlag, uploadTo);
515                 }
516         }
517         FPDF_FILEHANDLER* FFI_OpenFile(int fileType, FPDF_WIDESTRING wsURL, const char* mode)
518         {
519                 if(m_pInfo && m_pInfo->FFI_OpenFile)
520                 {
521                         return m_pInfo->FFI_OpenFile(m_pInfo, fileType, wsURL, mode);
522                 }
523                 return NULL;
524         }
525         CFX_WideString FFI_GetFilePath(FPDF_FILEHANDLER* pFileHandler)
526         {
527 #if 0
528                 if (m_pInfo && m_pInfo->FFI_GetFilePath)
529                 {
530                         int nRequiredLen = m_pInfo->FFI_GetFilePath(m_pInfo, pFileHandler, NULL, 0);
531                         if (nRequiredLen <= 0)
532                                 return L"";
533
534                         char* pbuff = new char[nRequiredLen];
535                         if (!pbuff)
536                                 return L"";
537
538                         memset(pbuff, 0, nRequiredLen);
539                         int nActualLen = m_pInfo->FFI_GetFilePath(m_pInfo, pFileHandler, pbuff, nRequiredLen);
540                         if (nActualLen <= 0 || nActualLen > nRequiredLen)
541                         {
542                                 delete[] pbuff;
543                                 return L"";
544                         }
545                         CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
546                         CFX_WideString wsRet = CFX_WideString::FromUTF16LE((unsigned short*)bsRet.GetBuffer(bsRet.GetLength()), bsRet.GetLength()/sizeof(unsigned short));
547                         delete[] pbuff;
548                         return wsRet;
549                 }
550                 return L"";
551 #else
552                 return L"";
553 #endif
554         }
555         int             FFI_GetDocumentCount()
556         {
557                 //if (m_pInfo && m_pInfo->FFI_GetDocumentCount)
558                 //{
559                         //return m_pInfo->FFI_GetDocumentCount(m_pInfo);
560                 //}
561                 return 0;
562         }
563
564         int             FFI_GetCurDocument()
565         {
566                 //if (m_pInfo && m_pInfo->FFI_GetCurDocumentIndex)
567                 //{
568                 //      return m_pInfo->FFI_GetCurDocumentIndex(m_pInfo);
569                 //}
570                 return 0;
571         }
572
573         IFX_FileRead*   FFI_DownloadFromURL(FX_LPCWSTR url)
574         {
575                 if (m_pInfo && m_pInfo->FFI_DownloadFromURL)
576                 {
577                         CFX_ByteString bstrURL = CFX_WideString(url).UTF16LE_Encode();
578                         FPDF_WIDESTRING wsURL = (FPDF_WIDESTRING)bstrURL.GetBuffer(bstrURL.GetLength());
579
580                         FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL);
581
582                         CFPDF_FileStream* fileStream = FX_NEW CFPDF_FileStream(fileHandler);
583                         return fileStream;
584                 }
585                 return NULL;
586         }
587
588         CFX_WideString  FFI_PostRequestURL(FX_LPCWSTR wsURL, FX_LPCWSTR wsData, FX_LPCWSTR wsContentType, FX_LPCWSTR wsEncode, FX_LPCWSTR wsHeader)
589         {
590                 if (m_pInfo && m_pInfo->FFI_PostRequestURL)
591                 {
592                         CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
593                         FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
594
595                         CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
596                         FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
597
598                         CFX_ByteString bsContentType = CFX_WideString(wsContentType).UTF16LE_Encode();
599                         FPDF_WIDESTRING contentType = (FPDF_WIDESTRING)bsContentType.GetBuffer(bsContentType.GetLength());
600
601                         CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
602                         FPDF_WIDESTRING encode = (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
603
604                         CFX_ByteString bsHeader = CFX_WideString(wsHeader).UTF16LE_Encode();
605                         FPDF_WIDESTRING header = (FPDF_WIDESTRING)bsHeader.GetBuffer(bsHeader.GetLength());
606
607                         FPDF_BSTR respone;
608                         FPDF_BStr_Init(&respone);
609                         FPDF_BOOL bRet = m_pInfo->FFI_PostRequestURL(m_pInfo, URL, data, contentType, encode, header, &respone);
610
611                         CFX_WideString wsRet = CFX_WideString::FromUTF16LE((unsigned short*)respone.str, respone.len/sizeof(unsigned short));
612                         FPDF_BStr_Clear(&respone);
613
614                         return wsRet;
615                 }
616                 return L"";
617         }
618
619         FPDF_BOOL       FFI_PutRequestURL(FX_LPCWSTR wsURL, FX_LPCWSTR wsData, FX_LPCWSTR wsEncode)
620         {
621                 if (m_pInfo && m_pInfo->FFI_PutRequestURL)
622                 {
623                         CFX_ByteString bsURL = CFX_WideString(wsURL).UTF16LE_Encode();
624                         FPDF_WIDESTRING URL = (FPDF_WIDESTRING)bsURL.GetBuffer(bsURL.GetLength());
625
626                         CFX_ByteString bsData = CFX_WideString(wsData).UTF16LE_Encode();
627                         FPDF_WIDESTRING data = (FPDF_WIDESTRING)bsData.GetBuffer(bsData.GetLength());
628
629                         CFX_ByteString bsEncode = CFX_WideString(wsEncode).UTF16LE_Encode();
630                         FPDF_WIDESTRING encode = (FPDF_WIDESTRING)bsEncode.GetBuffer(bsEncode.GetLength());
631
632                         return m_pInfo->FFI_PutRequestURL(m_pInfo, URL, data, encode);
633                 }
634                 return FALSE;
635         }
636
637         FPDF_BOOL       FFI_ShowFileDialog(FX_LPCWSTR wsTitle, FX_LPCWSTR wsFilter, CFX_WideStringArray &wsPathArr, FX_BOOL bOpen)
638         {
639                 /*CFX_ByteString bsTitle = CFX_WideString(wsTitle).UTF16LE_Encode();
640                 FPDF_WIDESTRING title = (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
641
642                 CFX_ByteString bsFilter = CFX_WideString(wsFilter).UTF16LE_Encode();
643                 FPDF_WIDESTRING filter = (FPDF_WIDESTRING)bsFilter.GetBuffer(bsFilter.GetLength());
644
645                 CFX_ByteStringArray* strPathArr = FX_NEW CFX_ByteStringArray();
646                 if (m_pInfo && m_pInfo->FFI_ShowFileDialog)
647                 {
648                         FPDF_BOOL bRet = m_pInfo->FFI_ShowFileDialog(m_pInfo, title, filter, bOpen, (FPDF_STRINGHANDLE)strPathArr);
649                         if (bRet) 
650                         {
651                                 int count = strPathArr->GetSize();
652                                 for (int i=0; i<count; i++)
653                                 {
654                                         CFX_ByteString bsPath = *strPathArr->GetDataPtr(i);
655                                         CFX_WideString wsPath = CFX_WideString::FromUTF16LE((unsigned short*)bsPath.GetBuffer(bsPath.GetLength()), bsPath.GetLength()/sizeof(unsigned short));
656                                         if (!wsPath.IsEmpty())
657                                                 wsPathArr.Add(wsPath);
658                                         wsPath = L"";
659                                 }
660                                 return TRUE;
661                         }
662
663                         return FALSE;
664                 }
665                 */
666
667                 return FALSE;
668         }
669         CFX_WideString FFI_GetLanguage()
670         {
671                 if (m_pInfo && m_pInfo->FFI_GetLanguage)
672                 {
673                         int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, NULL, 0);
674                         if (nRequiredLen <= 0)
675                                 return L"";
676
677                         char* pbuff = new char[nRequiredLen];
678                         if (!pbuff)
679                                 return L"";
680
681                         memset(pbuff, 0, nRequiredLen);
682                         int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen);
683                         if (nActualLen <= 0 || nActualLen > nRequiredLen)
684                         {
685                                 delete[] pbuff;
686                                 return L"";
687                         }
688                         CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
689                         CFX_WideString wsRet = CFX_WideString::FromUTF16LE((unsigned short*)bsRet.GetBuffer(bsRet.GetLength()), bsRet.GetLength()/sizeof(unsigned short));
690                         delete[] pbuff;
691                         return wsRet;
692                 }
693                 return L"";
694         }
695
696 public:
697         FX_BOOL                         IsJSInitiated();
698
699 public: 
700         void                            SetCurrentDoc(CPDFSDK_Document* pFXDoc) {m_pSDKDoc = pFXDoc;}
701         CPDFSDK_Document*       GetCurrentDoc();
702         CPDFXFA_Document*       GetPDFXFADocument() {return m_pDoc;}
703 //      CPDFSDK_Document*   GetDocument(int nIndex);
704 //      int                                     CountDocuments() {return m_docMap.GetCount();}
705
706         CPDFSDK_Document*               OpenDocument(CFX_WideString &fileName){return NULL;}
707         CPDFSDK_Document*               OpenMemPDFDoc(CPDF_Document* pNewDoc, CFX_WideString &fileName){return NULL;}
708         FX_BOOL                                 OpenURL(CFX_WideString &filePath){return FALSE;}
709         
710
711         CFX_ByteString          GetAppName() {return "";}
712
713         CFFL_IFormFiller*       GetIFormFiller();
714         IFX_SystemHandler*      GetSysHandler() {return m_pSysHandler;}
715
716 public:
717         CPDFSDK_AnnotHandlerMgr* GetAnnotHandlerMgr();
718         IFXJS_Runtime*  GetJSRuntime();
719         CPDFSDK_ActionHandler* GetActionHander();
720 private:
721         CPDFSDK_AnnotHandlerMgr* m_pAnnotHandlerMgr;
722         CPDFSDK_ActionHandler*  m_pActionHandler;
723         IFXJS_Runtime*  m_pJSRuntime;
724 public:
725         FPDF_FORMFILLINFO* GetFormFillInfo() {return m_pInfo;}
726 private:
727         FPDF_FORMFILLINFO*      m_pInfo;
728 //      CFX_MapPtrTemplate<CPDF_Document*, CPDFSDK_Document*> m_docMap;
729         CPDFSDK_Document* m_pSDKDoc;
730         CPDFXFA_Document* m_pDoc;
731
732         CFFL_IFormFiller* m_pIFormFiller;
733         IFX_SystemHandler* m_pSysHandler;
734
735 public:
736         //CJS_RuntimeFactory*  m_pJSRuntimeFactory;
737 };
738
739
740
741 // class CFX_App
742 // {
743 // public:
744 //      CFX_App():m_pCurDoc(NULL) {}
745 //      void SetAt(CPDF_Document* pPDFDoc, CPDFSDK_Document* pFXDoc);
746 //      CPDFSDK_Document* GetAt(CPDF_Document* pPDFDoc);
747 // public:
748 //      void SetCurrentDocument(CPDFSDK_Document* pFXDoc) {m_pCurDoc = pFXDoc;}
749 //      CPDFSDK_Document* GetCurrentDocument() {return m_pCurDoc;}
750 // private:
751 //      CFX_MapPtrTemplate<CPDF_Document*, CPDFSDK_Document*> m_docArray;
752 //      CPDFSDK_Document* m_pCurDoc;
753 // };
754 class CPDFSDK_InterForm;
755 class CPDFSDK_Document
756 {
757 public:
758         CPDFSDK_Document(CPDFXFA_Document* pDoc, CPDFDoc_Environment* pEnv);
759         ~CPDFSDK_Document();
760 public:
761         CPDFSDK_InterForm*              GetInterForm() ;
762         CPDFXFA_Document*               GetDocument() {return m_pDoc;}
763
764 public:
765         void                                    InitPageView();
766         int                                             GetPageViewCount(){return m_pageMap.GetCount();}
767         void                                    AddPageView(CPDFXFA_Page* pPDFXFAPage, CPDFSDK_PageView* pPageView);
768         CPDFSDK_PageView*               GetPageView(CPDFXFA_Page* pPDFXFAPage, FX_BOOL ReNew = TRUE);
769         CPDFSDK_PageView*               GetPageView(int nIndex);
770         CPDFSDK_PageView*               GetCurrentView();
771         void                                    ReMovePageView(CPDFXFA_Page* pPDFPage);
772         void                                    UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot);
773
774         CPDFSDK_Annot*                  GetFocusAnnot();//{return NULL;}
775
776         IFXJS_Runtime *                 GetJsRuntime();
777         
778         FX_BOOL                                 SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag = 0);//{return FALSE;}
779         FX_BOOL                                 KillFocusAnnot(FX_UINT nFlag = 0);
780
781         FX_BOOL                                 ExtractPages(const CFX_WordArray &arrExtraPages, CPDF_Document* pDstDoc);
782         FX_BOOL                                 InsertPages(int nInsertAt, const CPDF_Document* pSrcDoc, const CFX_WordArray &arrSrcPages);
783         FX_BOOL                                 DeletePages(int nStart, int nCount);
784         FX_BOOL                                 ReplacePages(int nPage, const CPDF_Document* pSrcDoc, const CFX_WordArray &arrSrcPages);
785
786         void                                    OnCloseDocument();
787
788         int                                             GetPageCount() {return m_pDoc->GetPageCount();}
789         FX_BOOL                                 GetPermissions(int nFlag);
790         FX_BOOL                                 GetChangeMark() {return m_bChangeMask;}
791         void                                    SetChangeMark() {m_bChangeMask = TRUE;}
792         void                                    ClearChangeMark() {m_bChangeMask= FALSE;}
793 //      FX_BOOL                                 GetChangeMark(){return FALSE;}//IsAnnotModified()||IsFormModified() || IsWidgetModified()|| m_nChangeMark>0 ;}  
794 //      void                    ClearChangeMark(){}
795         CFX_WideString                  GetPath() ;
796         CPDFXFA_Page*                   GetPage(int nIndex);
797         CPDFDoc_Environment *   GetEnv() {return m_pEnv; }
798         void                                ProcJavascriptFun();
799         FX_BOOL                                 ProcOpenAction();
800         CPDF_OCContext*                 GetOCContext();
801 private:
802         //CFX_ArrayTemplate<CPDFSDK_PageView*> m_pageArray;
803         CFX_MapPtrTemplate<CPDFXFA_Page*, CPDFSDK_PageView*> m_pageMap;
804         CPDFXFA_Document*                       m_pDoc;
805
806         CPDFSDK_InterForm*              m_pInterForm;
807         CPDFSDK_Annot*                  m_pFocusAnnot;
808         CPDFDoc_Environment *   m_pEnv;
809         CPDF_OCContext *                m_pOccontent;
810         FX_BOOL                                 m_bChangeMask;
811 };
812
813 class CPDFSDK_PageView FX_FINAL
814 {
815 public:
816         CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,CPDFXFA_Page* page);
817         ~CPDFSDK_PageView();
818         void PageView_OnDraw(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device,CPDF_RenderOptions* pOptions, FX_RECT* pClip = NULL) ;
819         CPDF_Annot*                                             GetPDFAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
820         CPDFSDK_Annot*                                  GetFXAnnotAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
821         CPDF_Annot*                                             GetPDFWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
822         CPDFSDK_Annot*                                  GetFXWidgetAtPoint(FX_FLOAT pageX, FX_FLOAT pageY);
823         CPDFSDK_Annot*                                  GetFocusAnnot() ;
824         void                                                    SetFocusAnnot(CPDFSDK_Annot* pSDKAnnot,FX_UINT nFlag = 0) {m_pSDKDoc->SetFocusAnnot(pSDKAnnot, nFlag);}
825         FX_BOOL                                                 KillFocusAnnot(FX_UINT nFlag = 0) {return m_pSDKDoc->KillFocusAnnot(nFlag);}
826         FX_BOOL                                                 Annot_HasAppearance(CPDF_Annot* pAnnot);
827
828         CPDFSDK_Annot*                                  AddAnnot(CPDF_Dictionary * pDict);
829         CPDFSDK_Annot*                                  AddAnnot(FX_LPCSTR lpSubType,CPDF_Dictionary * pDict);
830         CPDFSDK_Annot*                                  AddAnnot(CPDF_Annot * pPDFAnnot);
831         CPDFSDK_Annot*                                  AddAnnot(XFA_HWIDGET pPDFAnnot);
832         FX_BOOL                                                 DeleteAnnot(CPDFSDK_Annot* pAnnot);                                                     
833         
834         int                                                             CountAnnots();
835         CPDFSDK_Annot*                                  GetAnnot(int nIndex);
836         CPDFSDK_Annot*                              GetAnnotByDict(CPDF_Dictionary * pDict);
837         CPDFSDK_Annot*                                  GetAnnotByXFAWidget(XFA_HWIDGET hWidget);
838         CPDFXFA_Page*                                   GetPDFXFAPage(){return m_page;}
839         CPDF_Page*                                              GetPDFPage();
840         CPDF_Document*                                  GetPDFDocument();
841         CPDFSDK_Document*                               GetSDKDocument() {return m_pSDKDoc;}    
842         FX_BOOL                                 OnLButtonDown(const CPDF_Point & point, FX_UINT nFlag);
843         FX_BOOL                                 OnLButtonUp(const CPDF_Point & point, FX_UINT nFlag);
844         FX_BOOL                                 OnRButtonDown(const CPDF_Point & point, FX_UINT nFlag);
845         FX_BOOL                                 OnRButtonUp(const CPDF_Point & point, FX_UINT nFlag);
846         FX_BOOL                                 OnChar(int nChar, FX_UINT nFlag);
847         FX_BOOL                                 OnKeyDown(int nKeyCode, int nFlag);
848         FX_BOOL                                 OnKeyUp(int nKeyCode, int nFlag);
849
850         FX_BOOL                                 OnMouseMove(const CPDF_Point & point, int nFlag);
851         FX_BOOL                                 OnMouseWheel(double deltaX, double deltaY,const CPDF_Point& point, int nFlag);
852         FX_BOOL                                 IsValidAnnot(FX_LPVOID p);
853         void                                    GetCurrentMatrix(CPDF_Matrix& matrix) {matrix = m_curMatrix;}
854         void                                    UpdateRects(CFX_RectArray& rects);
855         void                                                    UpdateView(CPDFSDK_Annot* pAnnot);
856         CFX_PtrArray*                                   GetAnnotList(){ return &m_fxAnnotArray; }
857
858         int                                             GetPageIndex();
859         void                                                    LoadFXAnnots();
860         void SetValid(FX_BOOL bValid) {m_bValid = bValid;}
861     FX_BOOL IsValid() {return m_bValid;}
862     void SetLock(FX_BOOL bLocked) {m_bLocked= bLocked;}
863     FX_BOOL IsLocked() {return m_bLocked;}
864     void TakeOverPage() {m_bTakeOverPage = TRUE;}
865 private:
866         void PageView_OnHighlightFormFields(CFX_RenderDevice* pDevice, CPDFSDK_Widget* pWidget);
867         CPDF_Matrix m_curMatrix;
868         CPDFXFA_Page* m_page;
869         CPDF_AnnotList* m_pAnnotList;
870         //CPDFSDK_Annot* m_pFocusAnnot;
871         CFX_PtrArray  m_fxAnnotArray;
872         CPDFSDK_Document* m_pSDKDoc;
873         CPDFSDK_Annot* m_CaptureWidget;
874         FX_BOOL m_bEnterWidget;
875         FX_BOOL m_bExitWidget;
876         FX_BOOL m_bOnWidget;
877         FX_BOOL m_bValid;
878     FX_BOOL m_bLocked;
879     FX_BOOL m_bTakeOverPage;
880 };
881
882
883 template<class TYPE>
884 class CGW_ArrayTemplate : public CFX_ArrayTemplate<TYPE>
885 {
886 public: 
887         CGW_ArrayTemplate(){}
888         virtual ~CGW_ArrayTemplate(){}
889         
890         typedef int (*LP_COMPARE)(TYPE p1, TYPE p2);
891         
892         void Sort(LP_COMPARE pCompare, FX_BOOL bAscent = TRUE)
893         {
894                 int nSize = this->GetSize();
895                 QuickSort(0, nSize -1, bAscent, pCompare);
896         }
897         
898 private:
899         void QuickSort(FX_UINT nStartPos, FX_UINT nStopPos, FX_BOOL bAscend, LP_COMPARE pCompare)
900         {
901                 if (nStartPos >= nStopPos) return;
902                 
903                 if ((nStopPos - nStartPos) == 1)
904                 {
905                         TYPE Value1 = this->GetAt(nStartPos);
906                         TYPE Value2 = this->GetAt(nStopPos);
907                         
908                         int iGreate = (*pCompare)(Value1, Value2);
909                         if ((bAscend && iGreate > 0) || (!bAscend && iGreate < 0))
910                         {
911                                 this->SetAt(nStartPos, Value2);
912                                 this->SetAt(nStopPos, Value1);
913                         }
914                         return;
915                 }
916                 
917                 FX_UINT m = nStartPos + (nStopPos - nStartPos) / 2;
918                 FX_UINT i = nStartPos;
919                 
920                 TYPE Value = this->GetAt(m);
921                 
922                 while (i < m)
923                 {
924                         TYPE temp = this->GetAt(i);
925                         
926                         int iGreate = (*pCompare)(temp, Value);
927                         if ((bAscend && iGreate > 0) || (!bAscend && iGreate < 0))
928                         {
929                                 this->InsertAt(m+1, temp);
930                                 this->RemoveAt(i);
931                                 m--;
932                         }
933                         else
934                         {
935                                 i++;
936                         }
937                 }
938                 
939                 FX_UINT j = nStopPos;
940                 
941                 while (j > m)
942                 {
943                         TYPE temp = this->GetAt(j);
944                         
945                         int iGreate = (*pCompare)(temp, Value);
946                         if ((bAscend && iGreate < 0) || (!bAscend && iGreate > 0))
947                         {
948                                 this->RemoveAt(j);
949                                 this->InsertAt(m, temp);
950                                 m++;
951                         }
952                         else
953                         {
954                                 j--;
955                         }
956                 }
957                 
958                 if (nStartPos < m) QuickSort(nStartPos, m, bAscend, pCompare);
959                 if (nStopPos > m) QuickSort(m, nStopPos, bAscend, pCompare);
960         }
961 };
962
963
964 #endif //_FPDFSDK_MGR_H
965