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