Clean up CPDF_AnnotList.
[pdfium.git] / fpdfsdk / src / javascript / JS_Context.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 "JS_Context.h"
8
9 #include "../../include/javascript/IJavaScript.h"
10 #include "JS_EventHandler.h"
11 #include "JS_Runtime.h"
12 #include "resource.h"
13
14 /* -------------------------- CJS_Context -------------------------- */
15
16 CJS_Context::CJS_Context(CJS_Runtime* pRuntime)
17     : m_pRuntime(pRuntime), m_bBusy(FALSE), m_bMsgBoxEnable(TRUE) {
18   m_pEventHandler = new CJS_EventHandler(this);
19 }
20
21 CJS_Context::~CJS_Context() {
22   delete m_pEventHandler;
23 }
24
25 CPDFSDK_Document* CJS_Context::GetReaderDocument() {
26   return m_pRuntime->GetReaderDocument();
27 }
28
29 CPDFDoc_Environment* CJS_Context::GetReaderApp() {
30   ASSERT(m_pRuntime != NULL);
31
32   return m_pRuntime->GetReaderApp();
33 }
34
35 FX_BOOL CJS_Context::RunScript(const CFX_WideString& script,
36                                CFX_WideString* info) {
37   v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
38   v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
39   v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
40   v8::Context::Scope context_scope(context);
41
42   if (m_bBusy) {
43     *info = JSGetStringFromID(this, IDS_STRING_JSBUSY);
44     return FALSE;
45   }
46   m_bBusy = TRUE;
47
48   ASSERT(m_pEventHandler->IsValid());
49   CJS_Runtime::FieldEvent event(m_pEventHandler->TargetName(),
50                                 m_pEventHandler->EventType());
51   if (!m_pRuntime->AddEventToSet(event)) {
52     *info = JSGetStringFromID(this, IDS_STRING_JSEVENT);
53     return FALSE;
54   }
55
56   CFX_WideString sErrorMessage;
57   int nRet = 0;
58   if (script.GetLength() > 0) {
59     nRet = m_pRuntime->Execute(this, script.c_str(), &sErrorMessage);
60   }
61
62   if (nRet < 0) {
63     *info += sErrorMessage;
64   } else {
65     *info = JSGetStringFromID(this, IDS_STRING_RUN);
66   }
67
68   m_pRuntime->RemoveEventFromSet(event);
69   m_pEventHandler->Destroy();
70   m_bBusy = FALSE;
71
72   return nRet >= 0;
73 }
74
75 void CJS_Context::OnApp_Init() {
76   m_pEventHandler->OnApp_Init();
77 }
78
79 void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc,
80                              const CFX_WideString& strTargetName) {
81   m_pEventHandler->OnDoc_Open(pDoc, strTargetName);
82 }
83
84 void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc) {
85   m_pEventHandler->OnDoc_WillPrint(pDoc);
86 }
87
88 void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc) {
89   m_pEventHandler->OnDoc_DidPrint(pDoc);
90 }
91
92 void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc) {
93   m_pEventHandler->OnDoc_WillSave(pDoc);
94 }
95
96 void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc) {
97   m_pEventHandler->OnDoc_DidSave(pDoc);
98 }
99
100 void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc) {
101   m_pEventHandler->OnDoc_WillClose(pDoc);
102 }
103
104 void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget) {
105   m_pEventHandler->OnPage_Open(pTarget);
106 }
107
108 void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget) {
109   m_pEventHandler->OnPage_Close(pTarget);
110 }
111
112 void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget) {
113   m_pEventHandler->OnPage_InView(pTarget);
114 }
115
116 void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget) {
117   m_pEventHandler->OnPage_OutView(pTarget);
118 }
119
120 void CJS_Context::OnField_MouseDown(FX_BOOL bModifier,
121                                     FX_BOOL bShift,
122                                     CPDF_FormField* pTarget) {
123   m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
124 }
125
126 void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier,
127                                      FX_BOOL bShift,
128                                      CPDF_FormField* pTarget) {
129   m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
130 }
131
132 void CJS_Context::OnField_MouseExit(FX_BOOL bModifier,
133                                     FX_BOOL bShift,
134                                     CPDF_FormField* pTarget) {
135   m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
136 }
137
138 void CJS_Context::OnField_MouseUp(FX_BOOL bModifier,
139                                   FX_BOOL bShift,
140                                   CPDF_FormField* pTarget) {
141   m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
142 }
143
144 void CJS_Context::OnField_Focus(FX_BOOL bModifier,
145                                 FX_BOOL bShift,
146                                 CPDF_FormField* pTarget,
147                                 const CFX_WideString& Value) {
148   m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
149 }
150
151 void CJS_Context::OnField_Blur(FX_BOOL bModifier,
152                                FX_BOOL bShift,
153                                CPDF_FormField* pTarget,
154                                const CFX_WideString& Value) {
155   m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
156 }
157
158 void CJS_Context::OnField_Calculate(CPDF_FormField* pSource,
159                                     CPDF_FormField* pTarget,
160                                     CFX_WideString& Value,
161                                     FX_BOOL& bRc) {
162   m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
163 }
164
165 void CJS_Context::OnField_Format(CPDF_FormField* pTarget,
166                                  CFX_WideString& Value,
167                                  FX_BOOL bWillCommit) {
168   m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
169 }
170
171 void CJS_Context::OnField_Keystroke(CFX_WideString& strChange,
172                                     const CFX_WideString& strChangeEx,
173                                     FX_BOOL bKeyDown,
174                                     FX_BOOL bModifier,
175                                     int& nSelEnd,
176                                     int& nSelStart,
177                                     FX_BOOL bShift,
178                                     CPDF_FormField* pTarget,
179                                     CFX_WideString& Value,
180                                     FX_BOOL bWillCommit,
181                                     FX_BOOL bFieldFull,
182                                     FX_BOOL& bRc) {
183   m_pEventHandler->OnField_Keystroke(
184       strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
185       pTarget, Value, bWillCommit, bFieldFull, bRc);
186 }
187
188 void CJS_Context::OnField_Validate(CFX_WideString& strChange,
189                                    const CFX_WideString& strChangeEx,
190                                    FX_BOOL bKeyDown,
191                                    FX_BOOL bModifier,
192                                    FX_BOOL bShift,
193                                    CPDF_FormField* pTarget,
194                                    CFX_WideString& Value,
195                                    FX_BOOL& bRc) {
196   m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier,
197                                     bShift, pTarget, Value, bRc);
198 }
199
200 void CJS_Context::OnScreen_Focus(FX_BOOL bModifier,
201                                  FX_BOOL bShift,
202                                  CPDFSDK_Annot* pScreen) {
203   m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
204 }
205
206 void CJS_Context::OnScreen_Blur(FX_BOOL bModifier,
207                                 FX_BOOL bShift,
208                                 CPDFSDK_Annot* pScreen) {
209   m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
210 }
211
212 void CJS_Context::OnScreen_Open(FX_BOOL bModifier,
213                                 FX_BOOL bShift,
214                                 CPDFSDK_Annot* pScreen) {
215   m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
216 }
217
218 void CJS_Context::OnScreen_Close(FX_BOOL bModifier,
219                                  FX_BOOL bShift,
220                                  CPDFSDK_Annot* pScreen) {
221   m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
222 }
223
224 void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier,
225                                      FX_BOOL bShift,
226                                      CPDFSDK_Annot* pScreen) {
227   m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
228 }
229
230 void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier,
231                                    FX_BOOL bShift,
232                                    CPDFSDK_Annot* pScreen) {
233   m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
234 }
235
236 void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier,
237                                       FX_BOOL bShift,
238                                       CPDFSDK_Annot* pScreen) {
239   m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
240 }
241
242 void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier,
243                                      FX_BOOL bShift,
244                                      CPDFSDK_Annot* pScreen) {
245   m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
246 }
247
248 void CJS_Context::OnScreen_InView(FX_BOOL bModifier,
249                                   FX_BOOL bShift,
250                                   CPDFSDK_Annot* pScreen) {
251   m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
252 }
253
254 void CJS_Context::OnScreen_OutView(FX_BOOL bModifier,
255                                    FX_BOOL bShift,
256                                    CPDFSDK_Annot* pScreen) {
257   m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
258 }
259
260 void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
261   m_pEventHandler->OnBookmark_MouseUp(pBookMark);
262 }
263
264 void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget) {
265   m_pEventHandler->OnLink_MouseUp(pTarget);
266 }
267
268 void CJS_Context::OnConsole_Exec() {
269   m_pEventHandler->OnConsole_Exec();
270 }
271
272 void CJS_Context::OnExternal_Exec() {
273   m_pEventHandler->OnExternal_Exec();
274 }
275
276 void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget) {
277   m_pEventHandler->OnBatchExec(pTarget);
278 }
279
280 void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,
281                               const CFX_WideString& strTargetName) {
282   m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
283 }