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