Merge to XFA: Make conversion between CPDF_Action and its dictionary explicit.
[pdfium.git] / fpdfsdk / src / fsdk_actionhandler.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/fsdk_define.h"
8 #include "../include/fpdfxfa/fpdfxfa_doc.h"
9 #include "../include/fsdk_mgr.h"
10 #include "../include/fsdk_actionhandler.h"
11 #include "../include/javascript/IJavaScript.h"
12
13 /* -------------------------- CBA_ActionHandler -------------------------- */
14
15 CPDFSDK_ActionHandler::CPDFSDK_ActionHandler(CPDFDoc_Environment* pEvi) :
16         m_pFormActionHandler(NULL),
17         m_pMediaActionHandler(NULL)
18 {
19                 m_pFormActionHandler = new CPDFSDK_FormActionHandler;
20 }
21
22 CPDFSDK_ActionHandler::~CPDFSDK_ActionHandler()
23 {
24         if(m_pFormActionHandler)
25         {
26                 delete m_pFormActionHandler;
27                 m_pFormActionHandler = NULL;
28         }
29 }
30
31 void CPDFSDK_ActionHandler::SetFormActionHandler(CPDFSDK_FormActionHandler* pHandler)
32 {
33         ASSERT(pHandler != NULL);
34         ASSERT(m_pFormActionHandler == NULL);
35         m_pFormActionHandler = pHandler;
36 }
37
38 void CPDFSDK_ActionHandler::SetMediaActionHandler(CPDFSDK_MediaActionHandler* pHandler)
39 {
40         ASSERT(pHandler != NULL);
41         ASSERT(m_pMediaActionHandler == NULL);
42         m_pMediaActionHandler = pHandler;
43 }
44
45 void CPDFSDK_ActionHandler::Destroy()
46 {
47         delete this;
48 }
49
50 //document open
51 FX_BOOL CPDFSDK_ActionHandler::DoAction_DocOpen(const CPDF_Action& action, CPDFSDK_Document* pDocument)
52 {
53         CFX_PtrList list;
54         return ExecuteDocumentOpenAction(action, pDocument, list);
55 }
56
57 //document open
58 FX_BOOL CPDFSDK_ActionHandler::DoAction_JavaScript(const CPDF_Action& JsAction,CFX_WideString csJSName,
59                                                         CPDFSDK_Document* pDocument)
60 {
61         if (JsAction.GetType() == CPDF_Action::JavaScript)
62         {
63                 CFX_WideString swJS = JsAction.GetJavaScript();
64                 if (!swJS.IsEmpty())
65                 {
66                         RunDocumentOpenJavaScript(pDocument, csJSName, swJS);
67                         return TRUE;
68                 }
69         }
70
71         return FALSE;
72 }
73
74 FX_BOOL CPDFSDK_ActionHandler::DoAction_FieldJavaScript(const CPDF_Action& JsAction, CPDF_AAction::AActionType type,
75                                                                         CPDFSDK_Document* pDocument, CPDF_FormField* pFormField,
76                                                                         PDFSDK_FieldAction& data)
77 {
78         CPDFDoc_Environment* pEnv = pDocument->GetEnv();
79         ASSERT(pEnv);
80         if (pEnv->IsJSInitiated() && JsAction.GetType() == CPDF_Action::JavaScript)
81         {
82                 CFX_WideString swJS = JsAction.GetJavaScript();
83                 if (!swJS.IsEmpty())
84                 {
85                         RunFieldJavaScript(pDocument, pFormField, type, data, swJS);
86                         return TRUE;
87                 }
88         }
89         return FALSE;
90 }
91
92 FX_BOOL CPDFSDK_ActionHandler::DoAction_Page(const CPDF_Action& action, enum CPDF_AAction::AActionType eType,
93                                                                                 CPDFSDK_Document* pDocument)
94 {
95         CFX_PtrList list;
96         return ExecuteDocumentPageAction(action, eType, pDocument, list);
97 }
98
99 FX_BOOL CPDFSDK_ActionHandler::DoAction_Document(const CPDF_Action& action, enum CPDF_AAction::AActionType eType,
100                                                                                          CPDFSDK_Document* pDocument)
101 {
102         CFX_PtrList list;
103         return ExecuteDocumentPageAction(action, eType, pDocument, list);
104 }
105
106 FX_BOOL CPDFSDK_ActionHandler::DoAction_BookMark(CPDF_Bookmark *pBookMark, const CPDF_Action& action, CPDF_AAction::AActionType type,
107                                                         CPDFSDK_Document* pDocument)
108 {
109         CFX_PtrList list;
110         return this->ExecuteBookMark(action, pDocument, pBookMark, list);
111 }
112
113 FX_BOOL CPDFSDK_ActionHandler::DoAction_Screen(const CPDF_Action& action, CPDF_AAction::AActionType type,
114                                                                                 CPDFSDK_Document* pDocument, CPDFSDK_Annot* pScreen)
115 {
116         CFX_PtrList list;
117         return this->ExecuteScreenAction(action, type, pDocument, pScreen, list);
118 }
119
120 FX_BOOL CPDFSDK_ActionHandler::DoAction_Link(const CPDF_Action& action,
121                                                                                 CPDFSDK_Document* pDocument)
122 {
123         CFX_PtrList list;
124         return ExecuteLinkAction(action, pDocument, list);
125 }
126
127 FX_BOOL CPDFSDK_ActionHandler::DoAction_Field(const CPDF_Action& action, CPDF_AAction::AActionType type,
128                                                                                 CPDFSDK_Document* pDocument,
129                                                                                 CPDF_FormField* pFormField, PDFSDK_FieldAction& data)
130 {
131         CFX_PtrList list;
132         return ExecuteFieldAction(action, type, pDocument, pFormField, data, list);
133 }
134
135 FX_BOOL CPDFSDK_ActionHandler::ExecuteDocumentOpenAction(const CPDF_Action& action, CPDFSDK_Document* pDocument,
136                                                          CFX_PtrList& list)
137 {
138         CPDF_Dictionary* pDict = action.GetDict();
139         if (list.Find(pDict))
140                 return FALSE;
141
142         list.AddTail(pDict);
143
144         CPDFDoc_Environment* pEnv = pDocument->GetEnv();
145         ASSERT(pEnv);
146         if (action.GetType() == CPDF_Action::JavaScript)
147         {
148                 if(pEnv->IsJSInitiated())
149                 {
150                         CFX_WideString swJS = action.GetJavaScript();
151                         if (!swJS.IsEmpty())
152                         {
153                                 RunDocumentOpenJavaScript(pDocument, L"", swJS);
154                         }
155                 }
156         }
157         else
158         {
159                 DoAction_NoJs(action, pDocument);
160         }
161
162         for (FX_INT32 i=0,sz=action.GetSubActionsCount(); i<sz; i++)
163         {
164                 CPDF_Action subaction = action.GetSubAction(i);
165                 if (!ExecuteDocumentOpenAction(subaction, pDocument, list)) return FALSE;
166         }
167
168         return TRUE;
169 }
170
171 FX_BOOL CPDFSDK_ActionHandler::ExecuteLinkAction(const CPDF_Action& action,     CPDFSDK_Document* pDocument,
172                                                                                                   CFX_PtrList& list)
173 {
174         ASSERT(pDocument != NULL);
175
176         CPDF_Dictionary* pDict = action.GetDict();
177         if (list.Find(pDict))
178                 return FALSE;
179
180         list.AddTail(pDict);
181
182         CPDFDoc_Environment* pEnv = pDocument->GetEnv();
183         ASSERT(pEnv);
184         if (action.GetType() == CPDF_Action::JavaScript)
185         {
186                 if(pEnv->IsJSInitiated())
187                 {
188                         CFX_WideString swJS = action.GetJavaScript();
189                         if (!swJS.IsEmpty())
190                         {
191                                 IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime(); //????
192                                 ASSERT(pRuntime != NULL);
193
194                                 pRuntime->SetReaderDocument(pDocument);
195
196                                 IFXJS_Context* pContext = pRuntime->NewContext();
197                                 ASSERT(pContext != NULL);
198
199                                 pContext->OnLink_MouseUp(pDocument);
200
201                                 CFX_WideString csInfo;
202                                 FX_BOOL bRet = pContext->RunScript(swJS, csInfo);
203                                 if (!bRet)
204                                 {
205                                         // FIXME: return error.
206                                 }
207
208                                 pRuntime->ReleaseContext(pContext);
209                         }
210                 }
211         }
212         else
213         {
214                 DoAction_NoJs(action, pDocument);
215         }
216
217         for (FX_INT32 i=0,sz=action.GetSubActionsCount(); i<sz; i++)
218         {
219                 CPDF_Action subaction = action.GetSubAction(i);
220                 if (!ExecuteLinkAction(subaction, pDocument, list)) return FALSE;
221         }
222
223         return TRUE;
224 }
225
226 FX_BOOL CPDFSDK_ActionHandler::ExecuteDocumentPageAction(const CPDF_Action& action, CPDF_AAction::AActionType type,
227                                                                                                  CPDFSDK_Document* pDocument, CFX_PtrList& list)
228 {
229         ASSERT(pDocument != NULL);
230
231         CPDF_Dictionary* pDict = action.GetDict();
232         if (list.Find(pDict))
233                 return FALSE;
234
235         list.AddTail(pDict);
236
237         CPDFDoc_Environment* pEnv = pDocument->GetEnv();
238         ASSERT(pEnv);
239         if (action.GetType() == CPDF_Action::JavaScript)
240         {
241                 if(pEnv->IsJSInitiated())
242                 {
243                         CFX_WideString swJS = action.GetJavaScript();
244                         if (!swJS.IsEmpty())
245                         {
246                                 RunDocumentPageJavaScript(pDocument, type, swJS);
247                         }
248                 }
249         }
250         else
251         {
252                 DoAction_NoJs(action, pDocument);
253         }
254
255         if (!IsValidDocView(pDocument))
256                 return FALSE;
257
258         for (FX_INT32 i=0,sz=action.GetSubActionsCount(); i<sz; i++)
259         {
260                 CPDF_Action subaction = action.GetSubAction(i);
261                 if (!ExecuteDocumentPageAction(subaction, type, pDocument, list)) return FALSE;
262         }
263
264         return TRUE;
265 }
266
267 FX_BOOL CPDFSDK_ActionHandler::IsValidField(CPDFSDK_Document* pDocument, CPDF_Dictionary* pFieldDict)
268 {
269   ASSERT(pDocument != NULL);
270   ASSERT(pFieldDict != NULL);
271
272   CPDFSDK_InterForm* pInterForm = pDocument->GetInterForm();
273   ASSERT(pInterForm != NULL);
274
275   CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
276   ASSERT(pPDFInterForm != NULL);
277
278   return pPDFInterForm->GetFieldByDict(pFieldDict) != NULL;
279 }
280
281 FX_BOOL CPDFSDK_ActionHandler::ExecuteFieldAction(const CPDF_Action& action, CPDF_AAction::AActionType type,
282                                                                                   CPDFSDK_Document* pDocument, CPDF_FormField* pFormField,
283                                                                                   PDFSDK_FieldAction& data, CFX_PtrList& list)
284 {
285         ASSERT(pDocument != NULL);
286
287         CPDF_Dictionary* pDict = action.GetDict();
288         if (list.Find(pDict))
289                 return FALSE;
290
291         list.AddTail(pDict);
292
293         CPDFDoc_Environment* pEnv = pDocument->GetEnv();
294         ASSERT(pEnv);
295         if (action.GetType() == CPDF_Action::JavaScript)
296         {
297                 if(pEnv->IsJSInitiated())
298                 {
299                         CFX_WideString swJS = action.GetJavaScript();
300                         if (!swJS.IsEmpty())
301                         {
302                                 RunFieldJavaScript(pDocument, pFormField, type, data, swJS);
303                                 if (!IsValidField(pDocument, pFormField->GetFieldDict()))
304                                         return FALSE;
305                         }
306                 }
307         }
308         else
309         {
310                 DoAction_NoJs(action, pDocument);
311         }
312
313         for (FX_INT32 i=0,sz=action.GetSubActionsCount(); i<sz; i++)
314         {
315                 CPDF_Action subaction = action.GetSubAction(i);
316                 if (!ExecuteFieldAction(subaction, type, pDocument, pFormField, data, list)) return FALSE;
317         }
318
319         return TRUE;
320 }
321
322 FX_BOOL CPDFSDK_ActionHandler::ExecuteScreenAction(const CPDF_Action& action, CPDF_AAction::AActionType type,
323                                                                                 CPDFSDK_Document* pDocument, CPDFSDK_Annot* pScreen, CFX_PtrList& list)
324 {
325         ASSERT(pDocument != NULL);
326
327         CPDF_Dictionary* pDict = action.GetDict();
328         if (list.Find(pDict))
329                 return FALSE;
330
331         list.AddTail(pDict);
332
333         CPDFDoc_Environment* pEnv = pDocument->GetEnv();
334         ASSERT(pEnv);
335         if (action.GetType() == CPDF_Action::JavaScript)
336         {
337                 if(pEnv->IsJSInitiated())
338                 {
339                         CFX_WideString swJS = action.GetJavaScript();
340                         if (!swJS.IsEmpty())
341                         {
342                                 IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();
343                                 ASSERT(pRuntime != NULL);
344
345                                 pRuntime->SetReaderDocument(pDocument);
346
347                                 IFXJS_Context* pContext = pRuntime->NewContext();
348                                 ASSERT(pContext != NULL);
349
350         //                      switch (type)
351         //                      {
352         //                      case CPDF_AAction::CursorEnter:
353         //                              pContext->OnScreen_MouseEnter(IsCTRLpressed(), IsSHIFTpressed(), pScreen);
354         //                              break;
355         //                      case CPDF_AAction::CursorExit:
356         //                              pContext->OnScreen_MouseExit(IsCTRLpressed(), IsSHIFTpressed(), pScreen);
357         //                              break;
358         //                      case CPDF_AAction::ButtonDown:
359         //                              pContext->OnScreen_MouseDown(IsCTRLpressed(), IsSHIFTpressed(), pScreen);
360         //                              break;
361         //                      case CPDF_AAction::ButtonUp:
362         //                              pContext->OnScreen_MouseUp(IsCTRLpressed(), IsSHIFTpressed(), pScreen);
363         //                              break;
364         //                      case CPDF_AAction::GetFocus:
365         //                              pContext->OnScreen_Focus(IsCTRLpressed(), IsSHIFTpressed(), pScreen);
366         //                              break;
367         //                      case CPDF_AAction::LoseFocus:
368         //                              pContext->OnScreen_Blur(IsCTRLpressed(), IsSHIFTpressed(), pScreen);
369         //                              break;
370         //                      case CPDF_AAction::PageOpen:
371         //                              pContext->OnScreen_Open(IsCTRLpressed(), IsSHIFTpressed(), pScreen);
372         //                              break;
373         //                      case CPDF_AAction::PageClose:
374         //                              pContext->OnScreen_Close(IsCTRLpressed(), IsSHIFTpressed(), pScreen);
375         //                              break;
376         //                      case CPDF_AAction::PageVisible:
377         //                              pContext->OnScreen_InView(IsCTRLpressed(), IsSHIFTpressed(), pScreen);
378         //                              break;
379         //                      case CPDF_AAction::PageInvisible:
380         //                              pContext->OnScreen_OutView(IsCTRLpressed(), IsSHIFTpressed(), pScreen);
381         //                              break;
382         //                      default:
383         //                              ASSERT(FALSE);
384         //                              break;
385         //                      }
386
387                                 CFX_WideString csInfo;
388                                 FX_BOOL bRet = pContext->RunScript(swJS, csInfo);
389                                 if (!bRet)
390                                 {
391                                         //CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo);
392                                 }
393
394                                 pRuntime->ReleaseContext(pContext);
395                         }
396                 }
397         }
398         else
399         {
400                 DoAction_NoJs(action, pDocument);
401         }
402
403         for (FX_INT32 i=0,sz=action.GetSubActionsCount(); i<sz; i++)
404         {
405                 CPDF_Action subaction = action.GetSubAction(i);
406                 if (!ExecuteScreenAction(subaction, type, pDocument, pScreen, list)) return FALSE;
407         }
408
409         return TRUE;
410 }
411
412 FX_BOOL CPDFSDK_ActionHandler::ExecuteBookMark(const CPDF_Action& action, CPDFSDK_Document* pDocument,
413                                                                                  CPDF_Bookmark* pBookmark, CFX_PtrList& list)
414 {
415         ASSERT(pDocument != NULL);
416
417         CPDF_Dictionary* pDict = action.GetDict();
418         if (list.Find(pDict))
419                 return FALSE;
420
421         list.AddTail(pDict);
422
423         CPDFDoc_Environment* pEnv = pDocument->GetEnv();
424         ASSERT(pEnv);
425         if (action.GetType() == CPDF_Action::JavaScript)
426         {
427                 if(pEnv->IsJSInitiated())
428                 {
429                         CFX_WideString swJS = action.GetJavaScript();
430                         if (!swJS.IsEmpty())
431                         {
432                                 IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();
433                                 ASSERT(pRuntime != NULL);
434
435                                 pRuntime->SetReaderDocument(pDocument);
436
437                                 IFXJS_Context* pContext = pRuntime->NewContext();
438                                 ASSERT(pContext != NULL);
439
440                                 pContext->OnBookmark_MouseUp(pBookmark);
441
442                                 CFX_WideString csInfo;
443                                 FX_BOOL bRet = pContext->RunScript(swJS, csInfo);
444                                 if (!bRet)
445                                 {
446                                         //CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo);
447                                 }
448
449                                 pRuntime->ReleaseContext(pContext);
450                         }
451                 }
452         }
453         else
454         {
455                 DoAction_NoJs(action, pDocument);
456         }
457
458         for (FX_INT32 i=0,sz=action.GetSubActionsCount(); i<sz; i++)
459         {
460                 CPDF_Action subaction = action.GetSubAction(i);
461                 if (!ExecuteBookMark(subaction, pDocument, pBookmark, list)) return FALSE;
462         }
463
464         return TRUE;
465 }
466
467 void CPDFSDK_ActionHandler::DoAction_NoJs(const CPDF_Action& action, CPDFSDK_Document* pDocument)
468 {
469         ASSERT(pDocument != NULL);
470
471         switch (action.GetType())
472         {
473         case CPDF_Action::GoTo:
474                 DoAction_GoTo(pDocument, action);
475                 break;
476         case CPDF_Action::GoToR:
477                 DoAction_GoToR(pDocument, action);
478                 break;
479         case CPDF_Action::GoToE:
480                 break;
481         case CPDF_Action::Launch:
482                 DoAction_Launch(pDocument, action);
483                 break;
484         case CPDF_Action::Thread:
485                 break;
486         case CPDF_Action::URI:
487                 DoAction_URI(pDocument, action);
488                 break;
489         case CPDF_Action::Sound:
490                 if (m_pMediaActionHandler)
491                 {
492                         m_pMediaActionHandler->DoAction_Sound(action, pDocument);
493                 }
494                 break;
495         case CPDF_Action::Movie:
496                 if (m_pMediaActionHandler)
497                 {
498                         m_pMediaActionHandler->DoAction_Movie(action, pDocument);
499                 }
500                 break;
501         case CPDF_Action::Hide:
502                 if (m_pFormActionHandler)
503                 {
504                         m_pFormActionHandler->DoAction_Hide(action, pDocument);
505                 }
506                 break;
507         case CPDF_Action::Named:
508                 DoAction_Named(pDocument, action);
509                 break;
510         case CPDF_Action::SubmitForm:
511                 if (m_pFormActionHandler)
512                 {
513                         m_pFormActionHandler->DoAction_SubmitForm(action, pDocument);
514                 }
515                 break;
516         case CPDF_Action::ResetForm:
517                 if (m_pFormActionHandler)
518                 {
519                         m_pFormActionHandler->DoAction_ResetForm(action, pDocument);
520                 }
521                 break;
522         case CPDF_Action::ImportData:
523                 if (m_pFormActionHandler)
524                 {
525                         m_pFormActionHandler->DoAction_ImportData(action, pDocument);
526                 }
527                 break;
528         case CPDF_Action::JavaScript:
529                 ASSERT(FALSE);
530                 break;
531         case CPDF_Action::SetOCGState:
532                 DoAction_SetOCGState(pDocument,  action);
533                 break;
534         case CPDF_Action::Rendition:
535                 if (m_pMediaActionHandler)
536                 {
537                         m_pMediaActionHandler->DoAction_Rendition(action, pDocument);
538                 }
539                 break;
540         case CPDF_Action::Trans:
541                 break;
542         case CPDF_Action::GoTo3DView:
543                 break;
544         default:
545                 break;
546         }
547 }
548
549 FX_BOOL CPDFSDK_ActionHandler::IsValidDocView(CPDFSDK_Document* pDocument)
550 {
551         ASSERT(pDocument != NULL);
552         return TRUE;
553 }
554
555 void CPDFSDK_ActionHandler::DoAction_GoTo(CPDFSDK_Document* pDocument,
556                                                                   const CPDF_Action& action)
557 {
558         ASSERT(action);
559
560         CPDF_Document* pPDFDocument = pDocument->GetDocument()->GetPDFDoc();
561         ASSERT(pPDFDocument != NULL);
562         CPDFDoc_Environment* pApp = pDocument->GetEnv();
563         ASSERT(pApp != NULL);
564
565         CPDF_Dest MyDest = action.GetDest(pPDFDocument);
566         int nPageIndex = MyDest.GetPageIndex(pPDFDocument);
567         int nFitType = MyDest.GetZoomMode();
568         const CPDF_Array * pMyArray = (CPDF_Array*)MyDest.m_pObj;
569         float* pPosAry = NULL;
570         int sizeOfAry = 0;
571         if (pMyArray != NULL)
572         {
573                 pPosAry = new float[pMyArray->GetCount()];
574                 int j = 0;
575                 for (int i = 2; i < (int)pMyArray->GetCount(); i++)
576                 {
577                         pPosAry[j++] = pMyArray->GetFloat(i);
578                 }
579                 sizeOfAry = j;
580         }
581         pApp->FFI_DoGoToAction(nPageIndex, nFitType, pPosAry, sizeOfAry);
582         if(pPosAry)
583                 delete[] pPosAry;
584 }
585
586 void CPDFSDK_ActionHandler::DoAction_GoToR(CPDFSDK_Document* pDocument, const CPDF_Action& action)
587 {
588
589 }
590
591 void CPDFSDK_ActionHandler::DoAction_Launch(CPDFSDK_Document* pDocument, const CPDF_Action& action)
592 {
593
594 }
595
596 void CPDFSDK_ActionHandler::DoAction_URI(CPDFSDK_Document* pDocument, const CPDF_Action& action)
597 {
598         ASSERT(action);
599
600         CPDFDoc_Environment* pApp = pDocument->GetEnv();
601         ASSERT(pApp != NULL);
602         CFX_ByteString sURI = action.GetURI(pDocument->GetDocument()->GetPDFDoc());
603         pApp->FFI_DoURIAction(FX_LPCSTR(sURI));
604 }
605
606 void CPDFSDK_ActionHandler::DoAction_Named(CPDFSDK_Document* pDocument, const CPDF_Action& action)
607 {
608         ASSERT(action);
609
610         CFX_ByteString csName = action.GetNamedAction();
611         pDocument->GetEnv()->FFI_ExecuteNamedAction(csName);
612 }
613
614
615 void CPDFSDK_ActionHandler::DoAction_SetOCGState(CPDFSDK_Document* pDocument, const CPDF_Action& action)
616 {
617 }
618
619 void CPDFSDK_ActionHandler::RunFieldJavaScript(CPDFSDK_Document* pDocument, CPDF_FormField* pFormField, CPDF_AAction::AActionType type,
620                                                                                 PDFSDK_FieldAction& data, const CFX_WideString& script)
621 {
622         ASSERT(type != CPDF_AAction::Calculate);
623         ASSERT(type != CPDF_AAction::Format);
624
625         ASSERT(pDocument != NULL);
626
627         IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();
628         ASSERT(pRuntime != NULL);
629
630         pRuntime->SetReaderDocument(pDocument);
631
632         IFXJS_Context* pContext = pRuntime->NewContext();
633         ASSERT(pContext != NULL);
634
635         switch (type)
636         {
637         case CPDF_AAction::CursorEnter:
638                 pContext->OnField_MouseEnter(data.bModifier, data.bShift, pFormField);
639                 break;
640         case CPDF_AAction::CursorExit:
641                 pContext->OnField_MouseExit(data.bModifier, data.bShift, pFormField);
642                 break;
643         case CPDF_AAction::ButtonDown:
644                 pContext->OnField_MouseDown(data.bModifier, data.bShift, pFormField);
645                 break;
646         case CPDF_AAction::ButtonUp:
647                 pContext->OnField_MouseUp(data.bModifier, data.bShift, pFormField);
648                 break;
649         case CPDF_AAction::GetFocus:
650                 pContext->OnField_Focus(data.bModifier, data.bShift, pFormField, data.sValue);
651                 break;
652         case CPDF_AAction::LoseFocus:
653                 pContext->OnField_Blur(data.bModifier, data.bShift, pFormField, data.sValue);
654                 break;
655         case CPDF_AAction::KeyStroke:
656                 pContext->OnField_Keystroke(data.nCommitKey, data.sChange, data.sChangeEx, data.bKeyDown,
657                         data.bModifier, data.nSelEnd, data.nSelStart, data.bShift, pFormField, data.sValue,
658                         data.bWillCommit, data.bFieldFull, data.bRC);
659                 break;
660         case CPDF_AAction::Validate:
661                 pContext->OnField_Validate(data.sChange, data.sChangeEx, data.bKeyDown, data.bModifier,
662                         data.bShift, pFormField, data.sValue, data.bRC);
663                 break;
664         default:
665                 ASSERT(FALSE);
666                 break;
667         }
668
669         CFX_WideString csInfo;
670         FX_BOOL bRet = pContext->RunScript(script, csInfo);
671         if (!bRet)
672         {
673                 //CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo);
674         }
675
676         pRuntime->ReleaseContext(pContext);
677 }
678
679 void CPDFSDK_ActionHandler::RunDocumentOpenJavaScript(CPDFSDK_Document* pDocument, const CFX_WideString& sScriptName, const CFX_WideString& script)
680 {
681         ASSERT(pDocument != NULL);
682
683         IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();
684         ASSERT(pRuntime != NULL);
685
686         pRuntime->SetReaderDocument(pDocument);
687
688         IFXJS_Context* pContext = pRuntime->NewContext();
689         ASSERT(pContext != NULL);
690
691         pContext->OnDoc_Open(pDocument, sScriptName);
692
693         CFX_WideString csInfo;
694         FX_BOOL bRet = pContext->RunScript(script, csInfo);
695         if (!bRet)
696         {
697                 //CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo);
698         }
699
700         pRuntime->ReleaseContext(pContext);
701 }
702
703 void CPDFSDK_ActionHandler::RunDocumentPageJavaScript(CPDFSDK_Document* pDocument, CPDF_AAction::AActionType type, const CFX_WideString& script)
704 {
705         ASSERT(pDocument != NULL);
706
707         IFXJS_Runtime* pRuntime = pDocument->GetJsRuntime();
708         ASSERT(pRuntime != NULL);
709
710         pRuntime->SetReaderDocument(pDocument);
711
712         IFXJS_Context* pContext = pRuntime->NewContext();
713         ASSERT(pContext != NULL);
714
715         switch (type)
716         {
717         case CPDF_AAction::OpenPage:
718                 pContext->OnPage_Open(pDocument);
719                 break;
720         case CPDF_AAction::ClosePage:
721                 pContext->OnPage_Close(pDocument);
722                 break;
723         case CPDF_AAction::CloseDocument:
724                 pContext->OnDoc_WillClose(pDocument);
725                 break;
726         case CPDF_AAction::SaveDocument:
727                 pContext->OnDoc_WillSave(pDocument);
728                 break;
729         case CPDF_AAction::DocumentSaved:
730                 pContext->OnDoc_DidSave(pDocument);
731                 break;
732         case CPDF_AAction::PrintDocument:
733                 pContext->OnDoc_WillPrint(pDocument);
734                 break;
735         case CPDF_AAction::DocumentPrinted:
736                 pContext->OnDoc_DidPrint(pDocument);
737                 break;
738         case CPDF_AAction::PageVisible:
739                 pContext->OnPage_InView(pDocument);
740                 break;
741         case CPDF_AAction::PageInvisible:
742                 pContext->OnPage_OutView(pDocument);
743                 break;
744         default:
745                 ASSERT(FALSE);
746                 break;
747         }
748
749         CFX_WideString csInfo;
750         FX_BOOL bRet = pContext->RunScript(script, csInfo);
751         if (!bRet)
752         {
753                 //CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo);
754         }
755
756         pRuntime->ReleaseContext(pContext);
757 }
758
759
760 FX_BOOL CPDFSDK_FormActionHandler::DoAction_Hide(const CPDF_Action& action, CPDFSDK_Document* pDocument)
761 {
762         ASSERT(pDocument != NULL);
763
764         CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
765         ASSERT(pInterForm != NULL);
766
767         if (pInterForm->DoAction_Hide(action))
768         {
769                 pDocument->SetChangeMark();
770                 return TRUE;
771         }
772
773         return FALSE;
774 }
775
776 FX_BOOL CPDFSDK_FormActionHandler::DoAction_SubmitForm(const CPDF_Action& action, CPDFSDK_Document* pDocument)
777 {
778         ASSERT(pDocument != NULL);
779
780         CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
781         ASSERT(pInterForm != NULL);
782
783         return pInterForm->DoAction_SubmitForm(action);
784 }
785
786 FX_BOOL CPDFSDK_FormActionHandler::DoAction_ResetForm(const CPDF_Action& action, CPDFSDK_Document* pDocument)
787 {
788         ASSERT(pDocument != NULL);
789
790         CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
791         ASSERT(pInterForm != NULL);
792
793         if (pInterForm->DoAction_ResetForm(action))
794         {
795                 return TRUE;
796         }
797
798         return FALSE;
799 }
800
801 FX_BOOL CPDFSDK_FormActionHandler::DoAction_ImportData(const CPDF_Action& action, CPDFSDK_Document* pDocument)
802 {
803         ASSERT(pDocument != NULL);
804
805         CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)pDocument->GetInterForm();
806         ASSERT(pInterForm != NULL);
807
808         if (pInterForm->DoAction_ImportData(action))
809         {
810                 pDocument->SetChangeMark();
811                 return TRUE;
812         }
813
814         return FALSE;
815 }
816
817 FX_BOOL CPDFSDK_MediaActionHandler::DoAction_Rendition(const CPDF_Action& action, CPDFSDK_Document* pDocument)
818 {
819         return FALSE;
820 }
821
822 FX_BOOL CPDFSDK_MediaActionHandler::DoAction_Sound(const CPDF_Action& action, CPDFSDK_Document* pDocument)
823 {
824         return FALSE;
825 }
826
827 FX_BOOL CPDFSDK_MediaActionHandler::DoAction_Movie(const CPDF_Action& action, CPDFSDK_Document* pDocument)
828 {
829         return FALSE;
830 }