Remove JavaScript.h
[pdfium.git] / fpdfsdk / src / javascript / event.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/javascript/IJavaScript.h"
8 #include "../../include/javascript/JS_Define.h"
9 #include "../../include/javascript/JS_Object.h"
10 #include "../../include/javascript/JS_Value.h"
11 #include "../../include/javascript/JS_EventHandler.h"
12 #include "../../include/javascript/JS_Context.h"
13 #include "../../include/javascript/event.h"
14 #include "../../include/javascript/Field.h"
15
16 /* -------------------------- event -------------------------- */
17
18 BEGIN_JS_STATIC_CONST(CJS_Event)
19 END_JS_STATIC_CONST()
20
21 BEGIN_JS_STATIC_PROP(CJS_Event)
22 JS_STATIC_PROP_ENTRY(change)
23 JS_STATIC_PROP_ENTRY(changeEx)
24 JS_STATIC_PROP_ENTRY(commitKey)
25 JS_STATIC_PROP_ENTRY(fieldFull)
26 JS_STATIC_PROP_ENTRY(keyDown)
27 JS_STATIC_PROP_ENTRY(modifier)
28 JS_STATIC_PROP_ENTRY(name)
29 JS_STATIC_PROP_ENTRY(rc)
30 JS_STATIC_PROP_ENTRY(richChange)
31 JS_STATIC_PROP_ENTRY(richChangeEx)
32 JS_STATIC_PROP_ENTRY(richValue)
33 JS_STATIC_PROP_ENTRY(selEnd)
34 JS_STATIC_PROP_ENTRY(selStart)
35 JS_STATIC_PROP_ENTRY(shift)
36 JS_STATIC_PROP_ENTRY(source)
37 JS_STATIC_PROP_ENTRY(target)
38 JS_STATIC_PROP_ENTRY(targetName)
39 JS_STATIC_PROP_ENTRY(type)
40 JS_STATIC_PROP_ENTRY(value)
41 JS_STATIC_PROP_ENTRY(willCommit)
42 END_JS_STATIC_PROP()
43
44 BEGIN_JS_STATIC_METHOD(CJS_Event)
45 END_JS_STATIC_METHOD()
46
47 IMPLEMENT_JS_CLASS(CJS_Event, event)
48
49 event::event(CJS_Object* pJsObject) : CJS_EmbedObj(pJsObject) {}
50
51 event::~event() {
52 }
53
54 FX_BOOL event::change(IFXJS_Context* cc,
55                       CJS_PropValue& vp,
56                       CFX_WideString& sError) {
57   CJS_Context* pContext = (CJS_Context*)cc;
58   ASSERT(pContext != NULL);
59   CJS_EventHandler* pEvent = pContext->GetEventHandler();
60   ASSERT(pEvent != NULL);
61
62   CFX_WideString& wChange = pEvent->Change();
63   if (vp.IsSetting()) {
64     if (vp.GetType() == CJS_Value::VT_string)
65       vp >> wChange;
66   } else {
67     vp << wChange;
68   }
69   return TRUE;
70 }
71
72 FX_BOOL event::changeEx(IFXJS_Context* cc,
73                         CJS_PropValue& vp,
74                         CFX_WideString& sError) {
75   if (!vp.IsGetting())
76     return FALSE;
77
78   CJS_Context* pContext = (CJS_Context*)cc;
79   ASSERT(pContext != NULL);
80   CJS_EventHandler* pEvent = pContext->GetEventHandler();
81   ASSERT(pEvent != NULL);
82
83   vp << pEvent->ChangeEx();
84   return TRUE;
85 }
86
87 FX_BOOL event::commitKey(IFXJS_Context* cc,
88                          CJS_PropValue& vp,
89                          CFX_WideString& sError) {
90   if (!vp.IsGetting())
91     return FALSE;
92
93   CJS_Context* pContext = (CJS_Context*)cc;
94   ASSERT(pContext != NULL);
95   CJS_EventHandler* pEvent = pContext->GetEventHandler();
96   ASSERT(pEvent != NULL);
97
98   vp << pEvent->CommitKey();
99   return TRUE;
100 }
101
102 FX_BOOL event::fieldFull(IFXJS_Context* cc,
103                          CJS_PropValue& vp,
104                          CFX_WideString& sError) {
105   CJS_Context* pContext = (CJS_Context*)cc;
106   ASSERT(pContext != NULL);
107   CJS_EventHandler* pEvent = pContext->GetEventHandler();
108   ASSERT(pEvent != NULL);
109
110   if (!vp.IsGetting() &&
111       wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
112     return FALSE;
113
114   if (pEvent->FieldFull())
115     vp << TRUE;
116   else
117     vp << FALSE;
118   return TRUE;
119 }
120
121 FX_BOOL event::keyDown(IFXJS_Context* cc,
122                        CJS_PropValue& vp,
123                        CFX_WideString& sError) {
124   if (!vp.IsGetting())
125     return FALSE;
126
127   CJS_Context* pContext = (CJS_Context*)cc;
128   ASSERT(pContext != NULL);
129   CJS_EventHandler* pEvent = pContext->GetEventHandler();
130   ASSERT(pEvent != NULL);
131
132   if (pEvent->KeyDown())
133     vp << TRUE;
134   else
135     vp << FALSE;
136   return TRUE;
137 }
138
139 FX_BOOL event::modifier(IFXJS_Context* cc,
140                         CJS_PropValue& vp,
141                         CFX_WideString& sError) {
142   if (!vp.IsGetting())
143     return FALSE;
144
145   CJS_Context* pContext = (CJS_Context*)cc;
146   ASSERT(pContext != NULL);
147   CJS_EventHandler* pEvent = pContext->GetEventHandler();
148   ASSERT(pEvent != NULL);
149
150   if (pEvent->Modifier())
151     vp << TRUE;
152   else
153     vp << FALSE;
154   return TRUE;
155 }
156
157 FX_BOOL event::name(IFXJS_Context* cc,
158                     CJS_PropValue& vp,
159                     CFX_WideString& sError) {
160   if (!vp.IsGetting())
161     return FALSE;
162
163   CJS_Context* pContext = (CJS_Context*)cc;
164   ASSERT(pContext != NULL);
165   CJS_EventHandler* pEvent = pContext->GetEventHandler();
166   ASSERT(pEvent != NULL);
167
168   vp << pEvent->Name();
169   return TRUE;
170 }
171
172 FX_BOOL event::rc(IFXJS_Context* cc,
173                   CJS_PropValue& vp,
174                   CFX_WideString& sError) {
175   CJS_Context* pContext = (CJS_Context*)cc;
176   ASSERT(pContext != NULL);
177   CJS_EventHandler* pEvent = pContext->GetEventHandler();
178   ASSERT(pEvent != NULL);
179
180   FX_BOOL& bRc = pEvent->Rc();
181   if (vp.IsSetting()) {
182     vp >> bRc;
183   } else {
184     vp << bRc;
185   }
186   return TRUE;
187 }
188
189 FX_BOOL event::richChange(IFXJS_Context* cc,
190                           CJS_PropValue& vp,
191                           CFX_WideString& sError) {
192   return TRUE;
193   if (vp.IsSetting()) {
194   } else {
195     ;
196   }
197   return TRUE;
198 }
199
200 FX_BOOL event::richChangeEx(IFXJS_Context* cc,
201                             CJS_PropValue& vp,
202                             CFX_WideString& sError) {
203   return TRUE;
204   if (vp.IsSetting()) {
205   } else {
206     ;
207   }
208   return TRUE;
209 }
210
211 FX_BOOL event::richValue(IFXJS_Context* cc,
212                          CJS_PropValue& vp,
213                          CFX_WideString& sError) {
214   return TRUE;
215   if (vp.IsSetting()) {
216   } else {
217     ;
218   }
219   return TRUE;
220 }
221
222 FX_BOOL event::selEnd(IFXJS_Context* cc,
223                       CJS_PropValue& vp,
224                       CFX_WideString& sError) {
225   CJS_Context* pContext = (CJS_Context*)cc;
226   ASSERT(pContext != NULL);
227   CJS_EventHandler* pEvent = pContext->GetEventHandler();
228   ASSERT(pEvent != NULL);
229
230   if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) {
231     return TRUE;
232   }
233
234   int& iSelEnd = pEvent->SelEnd();
235   if (vp.IsSetting()) {
236     vp >> iSelEnd;
237   } else {
238     vp << iSelEnd;
239   }
240   return TRUE;
241 }
242
243 FX_BOOL event::selStart(IFXJS_Context* cc,
244                         CJS_PropValue& vp,
245                         CFX_WideString& sError) {
246   CJS_Context* pContext = (CJS_Context*)cc;
247   ASSERT(pContext != NULL);
248   CJS_EventHandler* pEvent = pContext->GetEventHandler();
249   ASSERT(pEvent != NULL);
250
251   if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) {
252     return TRUE;
253   }
254   int& iSelStart = pEvent->SelStart();
255   if (vp.IsSetting()) {
256     vp >> iSelStart;
257   } else {
258     vp << iSelStart;
259   }
260   return TRUE;
261 }
262
263 FX_BOOL event::shift(IFXJS_Context* cc,
264                      CJS_PropValue& vp,
265                      CFX_WideString& sError) {
266   if (!vp.IsGetting())
267     return FALSE;
268
269   CJS_Context* pContext = (CJS_Context*)cc;
270   ASSERT(pContext != NULL);
271   CJS_EventHandler* pEvent = pContext->GetEventHandler();
272   ASSERT(pEvent != NULL);
273
274   if (pEvent->Shift())
275     vp << TRUE;
276   else
277     vp << FALSE;
278   return TRUE;
279 }
280
281 FX_BOOL event::source(IFXJS_Context* cc,
282                       CJS_PropValue& vp,
283                       CFX_WideString& sError) {
284   if (!vp.IsGetting())
285     return FALSE;
286
287   CJS_Context* pContext = (CJS_Context*)cc;
288   ASSERT(pContext != NULL);
289   CJS_EventHandler* pEvent = pContext->GetEventHandler();
290   ASSERT(pEvent != NULL);
291
292   vp << pEvent->Source()->GetJSObject();
293   return TRUE;
294 }
295
296 FX_BOOL event::target(IFXJS_Context* cc,
297                       CJS_PropValue& vp,
298                       CFX_WideString& sError) {
299   if (!vp.IsGetting())
300     return FALSE;
301
302   CJS_Context* pContext = (CJS_Context*)cc;
303   ASSERT(pContext != NULL);
304   CJS_EventHandler* pEvent = pContext->GetEventHandler();
305   ASSERT(pEvent != NULL);
306
307   vp << pEvent->Target_Field()->GetJSObject();
308   return TRUE;
309 }
310
311 FX_BOOL event::targetName(IFXJS_Context* cc,
312                           CJS_PropValue& vp,
313                           CFX_WideString& sError) {
314   if (!vp.IsGetting())
315     return FALSE;
316
317   CJS_Context* pContext = (CJS_Context*)cc;
318   ASSERT(pContext != NULL);
319   CJS_EventHandler* pEvent = pContext->GetEventHandler();
320   ASSERT(pEvent != NULL);
321
322   vp << pEvent->TargetName();
323   return TRUE;
324 }
325
326 FX_BOOL event::type(IFXJS_Context* cc,
327                     CJS_PropValue& vp,
328                     CFX_WideString& sError) {
329   if (!vp.IsGetting())
330     return FALSE;
331
332   CJS_Context* pContext = (CJS_Context*)cc;
333   ASSERT(pContext != NULL);
334   CJS_EventHandler* pEvent = pContext->GetEventHandler();
335   ASSERT(pEvent != NULL);
336
337   vp << pEvent->Type();
338   return TRUE;
339 }
340
341 FX_BOOL event::value(IFXJS_Context* cc,
342                      CJS_PropValue& vp,
343                      CFX_WideString& sError) {
344   CJS_Context* pContext = (CJS_Context*)cc;
345   ASSERT(pContext != NULL);
346   CJS_EventHandler* pEvent = pContext->GetEventHandler();
347   ASSERT(pEvent != NULL);
348
349   if (wcscmp((const wchar_t*)pEvent->Type(), L"Field") != 0)
350     return FALSE;
351   if (!pEvent->m_pValue)
352     return FALSE;
353   CFX_WideString& val = pEvent->Value();
354   if (vp.IsSetting()) {
355     vp >> val;
356   } else {
357     vp << val;
358   }
359   return TRUE;
360 }
361
362 FX_BOOL event::willCommit(IFXJS_Context* cc,
363                           CJS_PropValue& vp,
364                           CFX_WideString& sError) {
365   if (!vp.IsGetting())
366     return FALSE;
367
368   CJS_Context* pContext = (CJS_Context*)cc;
369   ASSERT(pContext != NULL);
370   CJS_EventHandler* pEvent = pContext->GetEventHandler();
371   ASSERT(pEvent != NULL);
372
373   if (pEvent->WillCommit())
374     vp << TRUE;
375   else
376     vp << FALSE;
377   return TRUE;
378 }