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