blob: f0f52f77cc9e0473eefd6685c9a7222cb3b7ec40 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairefbc1912016-02-17 16:54:43 -05007#include "fpdfsdk/src/javascript/event.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Lei Zhangbde53d22015-11-12 22:21:30 -08009#include "fpdfsdk/include/javascript/IJavaScript.h"
Dan Sinclairefbc1912016-02-17 16:54:43 -050010#include "fpdfsdk/src/javascript/Field.h"
11#include "fpdfsdk/src/javascript/JS_Context.h"
12#include "fpdfsdk/src/javascript/JS_Define.h"
13#include "fpdfsdk/src/javascript/JS_EventHandler.h"
14#include "fpdfsdk/src/javascript/JS_Object.h"
15#include "fpdfsdk/src/javascript/JS_Value.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016
17/* -------------------------- event -------------------------- */
18
19BEGIN_JS_STATIC_CONST(CJS_Event)
20END_JS_STATIC_CONST()
21
22BEGIN_JS_STATIC_PROP(CJS_Event)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023JS_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)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043END_JS_STATIC_PROP()
44
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070045BEGIN_JS_STATIC_METHOD(CJS_Event)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046END_JS_STATIC_METHOD()
47
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048IMPLEMENT_JS_CLASS(CJS_Event, event)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050event::event(CJS_Object* pJsObject) : CJS_EmbedObj(pJsObject) {}
51
Lei Zhang2b1a2d52015-08-14 22:16:22 -070052event::~event() {
53}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054
Tom Sepezba038bc2015-10-08 12:03:00 -070055FX_BOOL event::change(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 CJS_PropValue& vp,
57 CFX_WideString& sError) {
58 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 CFX_WideString& wChange = pEvent->Change();
61 if (vp.IsSetting()) {
Tom Sepez39bfe122015-09-17 15:25:23 -070062 if (vp.GetType() == CJS_Value::VT_string)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 vp >> wChange;
64 } else {
65 vp << wChange;
66 }
67 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068}
69
Tom Sepezba038bc2015-10-08 12:03:00 -070070FX_BOOL event::changeEx(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 CJS_PropValue& vp,
72 CFX_WideString& sError) {
73 if (!vp.IsGetting())
74 return FALSE;
75
76 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078
79 vp << pEvent->ChangeEx();
80 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081}
82
Tom Sepezba038bc2015-10-08 12:03:00 -070083FX_BOOL event::commitKey(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 CJS_PropValue& vp,
85 CFX_WideString& sError) {
86 if (!vp.IsGetting())
87 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091
92 vp << pEvent->CommitKey();
93 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
95
Tom Sepezba038bc2015-10-08 12:03:00 -070096FX_BOOL event::fieldFull(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 CJS_PropValue& vp,
98 CFX_WideString& sError) {
99 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 if (!vp.IsGetting() &&
103 wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
104 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 if (pEvent->FieldFull())
107 vp << TRUE;
108 else
109 vp << FALSE;
110 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111}
112
Tom Sepezba038bc2015-10-08 12:03:00 -0700113FX_BOOL event::keyDown(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 CJS_PropValue& vp,
115 CFX_WideString& sError) {
116 if (!vp.IsGetting())
117 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700118
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 if (pEvent->KeyDown())
123 vp << TRUE;
124 else
125 vp << FALSE;
126 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127}
128
Tom Sepezba038bc2015-10-08 12:03:00 -0700129FX_BOOL event::modifier(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 CJS_PropValue& vp,
131 CFX_WideString& sError) {
132 if (!vp.IsGetting())
133 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 if (pEvent->Modifier())
139 vp << TRUE;
140 else
141 vp << FALSE;
142 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143}
144
Tom Sepezba038bc2015-10-08 12:03:00 -0700145FX_BOOL event::name(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 CJS_PropValue& vp,
147 CFX_WideString& sError) {
148 if (!vp.IsGetting())
149 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 vp << pEvent->Name();
155 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156}
157
Tom Sepezba038bc2015-10-08 12:03:00 -0700158FX_BOOL event::rc(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 FX_BOOL& bRc = pEvent->Rc();
163 if (vp.IsSetting()) {
164 vp >> bRc;
165 } else {
166 vp << bRc;
167 }
168 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700169}
170
Tom Sepezba038bc2015-10-08 12:03:00 -0700171FX_BOOL event::richChange(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 CJS_PropValue& vp,
173 CFX_WideString& sError) {
174 return TRUE;
175 if (vp.IsSetting()) {
176 } else {
177 ;
178 }
179 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180}
181
Tom Sepezba038bc2015-10-08 12:03:00 -0700182FX_BOOL event::richChangeEx(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 CJS_PropValue& vp,
184 CFX_WideString& sError) {
185 return TRUE;
186 if (vp.IsSetting()) {
187 } else {
188 ;
189 }
190 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191}
192
Tom Sepezba038bc2015-10-08 12:03:00 -0700193FX_BOOL event::richValue(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 CJS_PropValue& vp,
195 CFX_WideString& sError) {
196 return TRUE;
197 if (vp.IsSetting()) {
198 } else {
199 ;
200 }
201 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
Tom Sepezba038bc2015-10-08 12:03:00 -0700204FX_BOOL event::selEnd(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 CJS_PropValue& vp,
206 CFX_WideString& sError) {
207 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209
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;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221}
222
Tom Sepezba038bc2015-10-08 12:03:00 -0700223FX_BOOL event::selStart(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 CJS_PropValue& vp,
225 CFX_WideString& sError) {
226 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700228
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 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;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239}
240
Tom Sepezba038bc2015-10-08 12:03:00 -0700241FX_BOOL event::shift(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 CJS_PropValue& vp,
243 CFX_WideString& sError) {
244 if (!vp.IsGetting())
245 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 if (pEvent->Shift())
251 vp << TRUE;
252 else
253 vp << FALSE;
254 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255}
256
Tom Sepezba038bc2015-10-08 12:03:00 -0700257FX_BOOL event::source(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 CJS_PropValue& vp,
259 CFX_WideString& sError) {
260 if (!vp.IsGetting())
261 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265
266 vp << pEvent->Source()->GetJSObject();
267 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268}
269
Tom Sepezba038bc2015-10-08 12:03:00 -0700270FX_BOOL event::target(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 CJS_PropValue& vp,
272 CFX_WideString& sError) {
273 if (!vp.IsGetting())
274 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 vp << pEvent->Target_Field()->GetJSObject();
280 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281}
282
Tom Sepezba038bc2015-10-08 12:03:00 -0700283FX_BOOL event::targetName(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 CJS_PropValue& vp,
285 CFX_WideString& sError) {
286 if (!vp.IsGetting())
287 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 vp << pEvent->TargetName();
293 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700294}
295
Tom Sepezba038bc2015-10-08 12:03:00 -0700296FX_BOOL event::type(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 CJS_PropValue& vp,
298 CFX_WideString& sError) {
299 if (!vp.IsGetting())
300 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700301
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700304
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 vp << pEvent->Type();
306 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307}
308
Tom Sepezba038bc2015-10-08 12:03:00 -0700309FX_BOOL event::value(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 CJS_PropValue& vp,
311 CFX_WideString& sError) {
312 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700314
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 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;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700326}
327
Tom Sepezba038bc2015-10-08 12:03:00 -0700328FX_BOOL event::willCommit(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 CJS_PropValue& vp,
330 CFX_WideString& sError) {
331 if (!vp.IsGetting())
332 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 if (pEvent->WillCommit())
338 vp << TRUE;
339 else
340 vp << FALSE;
341 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342}