Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / fpdfsdk / src / fpdfformfill.cpp
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 #include "../include/fpdfview.h"
8 #include "../include/fpdfformfill.h"
9 #include "../include/fsdk_define.h"
10 #include "../include/fpdfxfa/fpdfxfa_doc.h"
11 #include "../include/fsdk_mgr.h"
12 #include "../include/fpdfxfa/fpdfxfa_page.h"
13 #include "../include/fpdfxfa/fpdfxfa_app.h"
14
15
16 #include "../include/javascript/IJavaScript.h"
17
18
19 DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle, FPDF_PAGE page,double page_x, double page_y)
20 {
21         if(!page || !hHandle)
22                 return -1;
23         CPDF_Page * pPage = ((CPDFXFA_Page*) page)->GetPDFPage();
24         if (pPage)
25         {
26                 CPDF_InterForm * pInterForm = NULL;
27                 pInterForm = new CPDF_InterForm(pPage->m_pDocument,FALSE);
28                 if (!pInterForm)
29                         return -1;
30                 CPDF_FormControl* pFormCtrl = pInterForm->GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y);
31                 if(!pFormCtrl)
32                 {
33                         delete pInterForm;
34                         return -1;
35                 }
36                 CPDF_FormField* pFormField = pFormCtrl->GetField();
37                 if(!pFormField)
38                 {
39                         delete pInterForm;
40                         return -1;
41                 }
42
43                 int nType = pFormField->GetFieldType();
44                 delete pInterForm;
45                 return nType;
46         }
47
48         IXFA_PageView* pPageView = ((CPDFXFA_Page*)page)->GetXFAPageView();
49         if (pPageView) 
50         {
51                 IXFA_WidgetHandler* pWidgetHandler = NULL;
52                 IXFA_DocView* pDocView = pPageView->GetDocView();
53                 if (!pDocView)
54                         return -1;
55
56                 pWidgetHandler = pDocView->GetWidgetHandler();
57                 if (!pWidgetHandler)
58                         return -1;
59
60                 XFA_HWIDGET pXFAAnnot = NULL;
61                 IXFA_WidgetIterator* pWidgetIterator = pPageView->CreateWidgetIterator(XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Viewable|XFA_WIDGETFILTER_AllType);
62                 if (!pWidgetIterator)
63                         return -1;
64                 pXFAAnnot = pWidgetIterator->MoveToNext();
65                 while (pXFAAnnot) {
66                         CFX_RectF rcBBox;
67                         pWidgetHandler->GetBBox (pXFAAnnot, rcBBox, 0);
68                         CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width, rcBBox.top+rcBBox.height);
69                         rcWidget.left -= 1.0f;
70                         rcWidget.right += 1.0f;
71                         rcWidget.bottom -= 1.0f;
72                         rcWidget.top += 1.0f;
73
74                         if (rcWidget.Contains(page_x, page_y)) {
75                                 pWidgetIterator->Release();
76                                 return FPDF_FORMFIELD_XFA;
77                         }
78                         pXFAAnnot = pWidgetIterator->MoveToNext();
79                 }
80
81                 pWidgetIterator->Release();
82         }
83         
84         return -1;
85 }
86
87 DLLEXPORT FPDF_FORMHANDLE STDCALL FPDFDOC_InitFormFillEnviroument(FPDF_DOCUMENT document, FPDF_FORMFILLINFO* formInfo)
88 {
89         if(!document || !formInfo || formInfo->version!=1)
90                 return NULL;
91         CPDFXFA_Document * pDocument = (CPDFXFA_Document*) document;
92         CPDFDoc_Environment * pEnv = NULL;
93         pEnv = new CPDFDoc_Environment(pDocument);
94         if (!pEnv)
95                 return NULL;
96         pEnv->RegAppHandle(formInfo);
97
98         CPDFXFA_App* pApp = FPDFXFA_GetApp();
99         pApp->AddFormFillEnv(pEnv);
100
101         if(pEnv->GetPDFXFADocument())
102         {
103                 //CPDFSDK_Document* pSDKDoc = new CPDFSDK_Document(pEnv->GetPDFXFADocument(), pEnv);
104                 CPDFSDK_Document* pSDKDoc = pDocument->GetSDKDocument(pEnv);
105                 if(pSDKDoc) {
106                         pEnv->SetCurrentDoc(pSDKDoc);
107                 }
108         }
109         return pEnv;
110 }
111
112 DLLEXPORT void STDCALL FPDFDOC_ExitFormFillEnviroument(FPDF_FORMHANDLE hHandle)
113 {
114         if (!hHandle)
115                 return; 
116         //CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
117         //if(pSDKDoc)
118         //{
119                 //((CPDFDoc_Environment*)hHandle)->SetCurrentDoc(NULL);
120                 //CPDFXFA_Document* pDoc = ((CPDFDoc_Environment*)hHandle)->GetPDFXFADocument();
121                 //if (pDoc)
122                 //      pDoc->ReleaseSDKDoc();
123         //}
124         CPDFXFA_App* pApp = FPDFXFA_GetApp();
125         pApp->RemoveFormFillEnv((CPDFDoc_Environment*)hHandle);
126         delete (CPDFDoc_Environment*)hHandle;
127         hHandle = NULL;
128 }
129
130 DLLEXPORT FPDF_BOOL STDCALL FORM_OnMouseMove(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
131 {       
132         if (!hHandle || !page)
133                 return FALSE;
134 //      CPDF_Page * pPage = (CPDF_Page*) page;
135 //      CPDF_Document * pDoc = pPage->m_pDocument;
136 //      CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
137         CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
138         if(!pFXDoc)
139                 return FALSE;
140         CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page);
141         if(!pPageView)
142                 return FALSE;
143         
144 //      double page_x = 0;
145 //      double page_y = 0;
146 //      pEnv->FFI_DeviceToPage(page, point_x, point_y, &page_x, &page_y);
147         CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
148         return pPageView->OnMouseMove(pt, modifier);
149 }
150
151 DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
152 {
153         if (!hHandle || !page)
154                 return FALSE;
155         CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
156         if(!pFXDoc)
157                 return FALSE;
158         CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page);
159         if(!pPageView)
160                 return FALSE;
161 //      double page_x = 0;
162 //      double page_y = 0;
163 //      pEnv->FFI_DeviceToPage(page, point_x, point_y, &page_x, &page_y);
164         CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
165         return pPageView->OnLButtonDown(pt, modifier);
166 }
167
168 DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
169 {
170         if (!hHandle || !page)
171                 return FALSE;
172         CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
173         if(!pFXDoc)
174                 return FALSE;
175         CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page);
176         if(!pPageView)
177                 return FALSE;
178 //      double page_x = 0;
179 //      double page_y = 0;
180 //      pEnv->FFI_DeviceToPage(page, point_x, point_y, &page_x, &page_y);
181         CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
182         return pPageView->OnLButtonUp(pt, modifier);
183 }
184
185 DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
186 {
187         if (!hHandle || !page)
188                 return FALSE;
189
190         CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
191         if(!pFXDoc)
192                 return FALSE;
193         CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page);
194         if(!pPageView)
195                 return FALSE;
196
197         CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
198         return pPageView->OnRButtonDown(pt, modifier);
199 }
200
201 DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
202 {
203         if (!hHandle || !page)
204                 return FALSE;
205
206         CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
207         if(!pFXDoc)
208                 return FALSE;
209         CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page);
210         if(!pPageView)
211                 return FALSE;
212
213         CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
214         return pPageView->OnRButtonUp(pt, modifier);
215 }
216
217 DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nKeyCode, int modifier)
218 {
219         if (!hHandle || !page)
220                 return FALSE;
221         CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
222         if(!pFXDoc)
223                 return FALSE;
224         CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page);
225         if(!pPageView)
226                 return FALSE;
227         
228         
229         return pPageView->OnKeyDown(nKeyCode, modifier);
230 }
231
232 DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyUp(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nKeyCode, int modifier)
233 {
234         if (!hHandle || !page)
235                 return FALSE;
236         CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
237         if(!pFXDoc)
238                 return FALSE;
239         CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page);
240         if(!pPageView)
241                 return FALSE;
242         
243         
244         return pPageView->OnKeyUp(nKeyCode, modifier);
245 }
246
247
248 DLLEXPORT FPDF_BOOL STDCALL FORM_OnChar(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nChar,  int modifier)
249 {
250         if (!hHandle || !page)
251                 return FALSE;
252         CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
253         if(!pFXDoc)
254                 return FALSE;
255         CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page);
256         if(!pPageView)
257                 return FALSE;
258         return pPageView->OnChar(nChar, modifier);
259
260 }
261
262 DLLEXPORT FPDF_BOOL STDCALL FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle)
263 {
264         if(!hHandle)
265                 return FALSE;
266         CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
267         if(!pSDKDoc)
268                 return FALSE;
269         //Kill the current focus. 
270         return pSDKDoc->KillFocusAnnot(0);
271 }
272
273 DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle, FPDF_BITMAP bitmap, FPDF_PAGE page, int start_x, int start_y, 
274                                                                                                   int size_x, int size_y, int rotate, int flags)
275 {
276         if (!hHandle || !page)
277                 return ;
278         
279         CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
280         CPDFXFA_Document* pDocument = pPage->GetDocument();
281         if (!pDocument)
282                 return;
283         CPDF_Document* pPDFDoc = pDocument->GetPDFDoc();
284         if (!pPDFDoc)
285                 return;
286
287         CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
288         CPDFSDK_Document* pFXDoc = pEnv->GetCurrentDoc();
289         if (!pFXDoc)
290                 return;
291         
292         CFX_AffineMatrix matrix;
293         pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate); 
294         
295         FX_RECT clip;
296         clip.left = start_x;
297         clip.right = start_x + size_x;
298         clip.top = start_y;
299         clip.bottom = start_y + size_y;
300
301 #ifdef _SKIA_SUPPORT_
302         CFX_SkiaDevice* pDevice = FX_NEW CFX_SkiaDevice;
303 #else
304         CFX_FxgeDevice* pDevice = NULL;
305         pDevice = FX_NEW CFX_FxgeDevice;
306 #endif
307
308         if (!pDevice)
309                 return;
310         pDevice->Attach((CFX_DIBitmap*)bitmap);
311         pDevice->SaveState();
312         pDevice->SetClip_Rect(&clip);
313         
314         CPDF_RenderOptions options;
315         if (flags & FPDF_LCD_TEXT)
316                 options.m_Flags |= RENDER_CLEARTYPE;
317         else
318                 options.m_Flags &= ~RENDER_CLEARTYPE;
319
320         //Grayscale output
321         if (flags & FPDF_GRAYSCALE)
322         {
323                 options.m_ColorMode = RENDER_COLOR_GRAY;
324                 options.m_ForeColor = 0;
325                 options.m_BackColor = 0xffffff;
326         }
327         options.m_AddFlags = flags >> 8;
328
329         options.m_pOCContext = FX_NEW CPDF_OCContext(pPDFDoc);
330
331         if(CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page))
332         {
333                 pPageView->PageView_OnDraw(pDevice, &matrix, &options, &clip);
334         }
335         pDevice->RestoreState();
336
337         if(options.m_pOCContext)
338         {
339                 delete options.m_pOCContext;
340                 options.m_pOCContext = NULL;
341         }
342         if(pDevice)
343         {
344                 delete pDevice;
345                 pDevice = NULL;
346         }
347
348 }
349 DLLEXPORT void STDCALL FPDF_Widget_Undo(FPDF_DOCUMENT document, FPDF_WIDGET hWidget)
350 {
351         if (NULL == hWidget || NULL == document)
352                 return;
353         
354         CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;      
355
356         if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
357                 return;
358
359         IXFA_MenuHandler* pXFAMenuHander = FPDFXFA_GetApp()->GetXFAApp()->GetMenuHandler();
360
361         if (pXFAMenuHander == NULL)
362                 return;
363
364         pXFAMenuHander->Undo((XFA_HWIDGET)hWidget);
365
366 }
367 DLLEXPORT void STDCALL FPDF_Widget_Redo(FPDF_DOCUMENT document, FPDF_WIDGET hWidget)
368 {
369         if (NULL == hWidget || NULL == document)
370                 return;
371         
372         CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;      
373         if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
374                 return;
375
376         IXFA_MenuHandler* pXFAMenuHander = FPDFXFA_GetApp()->GetXFAApp()->GetMenuHandler();
377
378         if (pXFAMenuHander == NULL)
379                 return;
380
381         pXFAMenuHander->Redo((XFA_HWIDGET)hWidget);
382 }
383
384 DLLEXPORT void STDCALL FPDF_Widget_SelectAll(FPDF_DOCUMENT document, FPDF_WIDGET hWidget)
385 {
386         if (NULL == hWidget || NULL == document)
387                 return;
388         
389         CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;      
390         if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
391                 return;
392
393         IXFA_MenuHandler* pXFAMenuHander = FPDFXFA_GetApp()->GetXFAApp()->GetMenuHandler();
394
395         if (pXFAMenuHander == NULL)
396                 return;
397
398         pXFAMenuHander->SelectAll((XFA_HWIDGET)hWidget);
399 }
400 DLLEXPORT void STDCALL FPDF_Widget_Copy(FPDF_DOCUMENT document, FPDF_WIDGET hWidget, FPDF_WIDESTRING wsText, FPDF_DWORD* size)
401 {
402         if (NULL == hWidget || NULL == document)
403                 return;
404         
405         CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;      
406         if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
407                 return;
408
409         IXFA_MenuHandler* pXFAMenuHander = FPDFXFA_GetApp()->GetXFAApp()->GetMenuHandler();
410
411         if (pXFAMenuHander == NULL)
412                 return;
413
414         CFX_WideString wsCpText;
415         pXFAMenuHander->Copy((XFA_HWIDGET)hWidget, wsCpText);
416
417         CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
418
419         int len = bsCpText.GetLength()/sizeof(unsigned short);
420         
421         if (wsText == NULL) {
422                 *size = len;
423                 return;
424         }
425
426         int real_size = len < *size ? len : *size;
427         if (real_size > 0)
428         {
429                 FXSYS_memcpy((void*)wsText,bsCpText.GetBuffer(real_size*sizeof(unsigned short)),real_size*sizeof(unsigned short));
430                 bsCpText.ReleaseBuffer(real_size*sizeof(unsigned short));
431         }
432         *size = real_size;
433 }
434 DLLEXPORT void STDCALL FPDF_Widget_Cut(FPDF_DOCUMENT document, FPDF_WIDGET hWidget, FPDF_WIDESTRING wsText, FPDF_DWORD* size)
435 {
436         if (NULL == hWidget || NULL == document)
437                 return;
438         
439         CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;      
440         if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
441                 return;
442
443         IXFA_MenuHandler* pXFAMenuHander = FPDFXFA_GetApp()->GetXFAApp()->GetMenuHandler();
444
445         if (pXFAMenuHander == NULL)
446                 return;
447
448         CFX_WideString wsCpText;
449         pXFAMenuHander->Cut((XFA_HWIDGET)hWidget, wsCpText);
450
451         CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
452
453         int len = bsCpText.GetLength()/sizeof(unsigned short);
454         
455         if (wsText == NULL) {
456                 *size = len;
457                 return;
458         }
459
460         int real_size = len < *size ? len : *size;
461         if (real_size > 0)
462         {
463                 FXSYS_memcpy((void*)wsText,bsCpText.GetBuffer(real_size*sizeof(unsigned short)),real_size*sizeof(unsigned short));
464                 bsCpText.ReleaseBuffer(real_size*sizeof(unsigned short));
465         }
466         *size = real_size;
467 }
468 DLLEXPORT void STDCALL FPDF_Widget_Paste(FPDF_DOCUMENT document, FPDF_WIDGET hWidget, FPDF_WIDESTRING wsText, FPDF_DWORD size)
469 {
470         if (NULL == hWidget || NULL == document)
471                 return;
472         
473         CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;      
474         if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
475                 return;
476
477         IXFA_MenuHandler* pXFAMenuHander = FPDFXFA_GetApp()->GetXFAApp()->GetMenuHandler();
478
479         if (pXFAMenuHander == NULL)
480                 return;
481
482         CFX_WideString wstr = CFX_WideString::FromUTF16LE(wsText, size);
483
484         pXFAMenuHander->Paste((XFA_HWIDGET)hWidget, wstr);
485
486 }
487 DLLEXPORT void STDCALL FPDF_Widget_ReplaceSpellCheckWord(FPDF_DOCUMENT document, FPDF_WIDGET hWidget, float x, float y, FPDF_BYTESTRING bsText)
488 {
489         if (NULL == hWidget || NULL == document)
490                 return;
491         
492         CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;      
493         if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
494                 return;
495
496         IXFA_MenuHandler* pXFAMenuHander = FPDFXFA_GetApp()->GetXFAApp()->GetMenuHandler();
497
498         if (pXFAMenuHander == NULL)
499                 return;
500
501         CFX_PointF ptPopup;
502         ptPopup.x = x;
503         ptPopup.y = y;
504         CFX_ByteStringC bs(bsText); 
505         pXFAMenuHander->ReplaceSpellCheckWord((XFA_HWIDGET)hWidget,ptPopup,bs);
506
507 }
508 DLLEXPORT void STDCALL FPDF_Widget_GetSpellCheckWords(FPDF_DOCUMENT document, FPDF_WIDGET hWidget, float x, float y, FPDF_STRINGHANDLE* stringHandle)
509 {
510         if (NULL == hWidget || NULL == document)
511                 return;
512         
513         CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;      
514         if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
515                 return;
516
517         IXFA_MenuHandler* pXFAMenuHander = FPDFXFA_GetApp()->GetXFAApp()->GetMenuHandler();
518
519         if (pXFAMenuHander == NULL)
520                 return;
521
522         CFX_ByteStringArray* sSuggestWords = FX_NEW CFX_ByteStringArray;
523         CFX_PointF ptPopup;
524         ptPopup.x = x;
525         ptPopup.y = y;
526         pXFAMenuHander->GetSuggestWords((XFA_HWIDGET)hWidget, ptPopup, *sSuggestWords);
527         *stringHandle = (FPDF_STRINGHANDLE)sSuggestWords;
528 }
529 DLLEXPORT int STDCALL FPDF_StringHandleCounts(FPDF_STRINGHANDLE stringHandle)
530 {
531         if (stringHandle == NULL)
532                 return -1;
533         CFX_ByteStringArray* sSuggestWords = (CFX_ByteStringArray*)stringHandle;
534         return sSuggestWords->GetSize();
535 }
536 DLLEXPORT FPDF_BOOL STDCALL FPDF_StringHandleGetStringByIndex(FPDF_STRINGHANDLE stringHandle, int index, FPDF_BYTESTRING bsText, FPDF_DWORD* size)
537 {
538         if (stringHandle == NULL || size == NULL)
539                 return FALSE;
540         int count = FPDF_StringHandleCounts(stringHandle);
541         if (index < 0|| index >= count)
542                 return FALSE;
543
544         CFX_ByteStringArray sSuggestWords = *(CFX_ByteStringArray*)stringHandle;
545         int len = sSuggestWords[index].GetLength();
546
547         if (bsText == NULL) {
548                 *size = len;
549                 return TRUE;
550         }
551         
552         int real_size = len < *size ? len : *size;
553         if (real_size > 0)
554                 FXSYS_memcpy((void*)bsText, (FX_LPCSTR)(sSuggestWords[index]), real_size);
555         *size = real_size;
556
557         return TRUE;
558 }
559 DLLEXPORT void STDCALL FPDF_StringHandleRelease(FPDF_STRINGHANDLE stringHandle)
560 {
561         if (stringHandle == NULL)
562                 return;
563         CFX_ByteStringArray* sSuggestWords = (CFX_ByteStringArray*)stringHandle;
564         delete sSuggestWords;
565 }
566
567 DLLEXPORT FPDF_BOOL STDCALL FPDF_StringHandleAddString(FPDF_STRINGHANDLE stringHandle, FPDF_BYTESTRING bsText, FPDF_DWORD size)
568 {
569         if (stringHandle == NULL || bsText == NULL || size <= 0)
570                 return FALSE;
571         
572         CFX_ByteStringArray* stringArr = (CFX_ByteStringArray*)stringHandle;
573         CFX_ByteString bsStr(bsText, size);
574
575         stringArr->Add(bsStr);
576         return TRUE;
577 }
578
579 DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle, int fieldType, unsigned long color)
580 {
581         if (!hHandle)
582                 return;
583 //      CPDFDoc_Environment* pEnv = (CPDFDoc_Environment* )hHandle;
584         CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
585         if(pSDKDoc)
586         {
587                 if(CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm())
588                 {
589                         pInterForm->SetHighlightColor(color, fieldType);
590                 }
591         
592         }
593
594 }
595
596 DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle, unsigned char alpha)
597 {
598         if (!hHandle)
599                 return;
600         CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
601         if(pSDKDoc)
602         {
603                 if(CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm())
604                         pInterForm->SetHighlightAlpha(alpha);
605         }
606 }
607
608 DLLEXPORT void STDCALL FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle)
609 {
610         if (!hHandle)
611                 return;
612         CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
613         if(pSDKDoc)
614         {
615                 if(CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm())
616                         pInterForm->RemoveAllHighLight();
617         }
618 }
619
620 DLLEXPORT void STDCALL FORM_OnAfterLoadPage(FPDF_PAGE page, FPDF_FORMHANDLE hHandle)
621 {
622         if(!hHandle || !page)
623                 return;
624         CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
625         if(!pSDKDoc)
626                 return;
627         CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
628         CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, TRUE);
629         if(pPageView)
630         {
631                 pPageView->SetValid(TRUE);
632         }       
633 }
634
635 DLLEXPORT void STDCALL FORM_OnBeforeClosePage(FPDF_PAGE page, FPDF_FORMHANDLE hHandle)
636 {
637         if(!hHandle || !page)
638                 return;
639         CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
640         CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
641         CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
642         if(pPageView)
643         {
644                 pPageView->SetValid(FALSE);
645                 // ReMovePageView() takes care of the delete for us.
646                 pSDKDoc->ReMovePageView(pPage);
647         }
648 }
649 DLLEXPORT void STDCALL FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle)
650 {
651         if(!hHandle)
652                 return;
653         if( CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc())
654         {
655                 pSDKDoc->InitPageView();
656                 if(((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
657                         pSDKDoc->ProcJavascriptFun();
658         }       
659 }
660
661 DLLEXPORT void STDCALL FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle)
662 {
663         if(!hHandle)
664                 return;
665         if( CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc())
666         {
667                 if(((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
668                         pSDKDoc->ProcOpenAction();
669         }
670 }
671 DLLEXPORT void STDCALL FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle, int aaType)
672 {
673         if(!hHandle)
674                 return;
675         CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
676         if(pSDKDoc)
677         {
678                 CPDF_Document* pDoc = pSDKDoc->GetDocument()->GetPDFDoc();
679                 CPDF_Dictionary* pDic = pDoc->GetRoot();
680                 if (!pDic)
681                         return;
682                 CPDF_AAction aa = pDic->GetDict(FX_BSTRC("AA"));
683                 
684                 if(aa.ActionExist((CPDF_AAction::AActionType)aaType))
685                 {
686                         CPDF_Action action = aa.GetAction((CPDF_AAction::AActionType)aaType);
687                         CPDFSDK_ActionHandler *pActionHandler = ((CPDFDoc_Environment*)hHandle)->GetActionHander();
688                         ASSERT(pActionHandler != NULL);
689                         pActionHandler->DoAction_Document(action, (CPDF_AAction::AActionType)aaType, pSDKDoc);
690                 }
691         }
692 }
693 DLLEXPORT void STDCALL FORM_DoPageAAction(FPDF_PAGE page, FPDF_FORMHANDLE hHandle, int aaType)
694 {
695         if(!hHandle || !page)
696                 return;
697         CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
698         CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
699         CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
700         if(pPageView)
701         {
702                 CPDFDoc_Environment *pEnv = pSDKDoc->GetEnv();
703                 ASSERT(pEnv != NULL);
704                         
705                 CPDFSDK_ActionHandler *pActionHandler = pEnv->GetActionHander();
706                 ASSERT(pActionHandler != NULL);
707                 
708                 if (!pPage->GetPDFPage())
709                         return;
710                 CPDF_Dictionary *pPageDict = pPage->GetPDFPage()->m_pFormDict;
711                 ASSERT(pPageDict != NULL);
712                  
713                 CPDF_AAction aa = pPageDict->GetDict(FX_BSTRC("AA"));
714
715                 FX_BOOL bExistOAAction = FALSE;
716                 FX_BOOL bExistCAAction = FALSE;
717                 if (FPDFPAGE_AACTION_OPEN == aaType)
718                 {
719                         bExistOAAction = aa.ActionExist(CPDF_AAction::OpenPage);
720                         if (bExistOAAction)
721                         {
722                                 CPDF_Action action = aa.GetAction(CPDF_AAction::OpenPage);
723                                 pActionHandler->DoAction_Page(action, CPDF_AAction::OpenPage, pSDKDoc);
724                         }
725                 }
726                 else
727                 {
728                         bExistCAAction = aa.ActionExist(CPDF_AAction::ClosePage);
729                         if (bExistCAAction)
730                         {
731                                 CPDF_Action action = aa.GetAction(CPDF_AAction::ClosePage);
732                                 pActionHandler->DoAction_Page(action, CPDF_AAction::ClosePage, pSDKDoc);
733                         }
734                 }
735         }
736 }
737
738