blob: 7549451e6c5c78cc0def792d9526509014fcb118 [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 Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/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 Sinclairf766ad22016-03-14 13:51:24 -040010#include "fpdfsdk/javascript/Field.h"
11#include "fpdfsdk/javascript/JS_Context.h"
12#include "fpdfsdk/javascript/JS_Define.h"
13#include "fpdfsdk/javascript/JS_EventHandler.h"
14#include "fpdfsdk/javascript/JS_Object.h"
15#include "fpdfsdk/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
Dan Sinclairf766ad22016-03-14 13:51:24 -040052event::~event() {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053
Tom Sepezba038bc2015-10-08 12:03:00 -070054FX_BOOL event::change(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 CJS_PropValue& vp,
56 CFX_WideString& sError) {
57 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 CFX_WideString& wChange = pEvent->Change();
60 if (vp.IsSetting()) {
Tom Sepez39bfe122015-09-17 15:25:23 -070061 if (vp.GetType() == CJS_Value::VT_string)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 vp >> wChange;
63 } else {
64 vp << wChange;
65 }
66 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067}
68
Tom Sepezba038bc2015-10-08 12:03:00 -070069FX_BOOL event::changeEx(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 CJS_PropValue& vp,
71 CFX_WideString& sError) {
72 if (!vp.IsGetting())
73 return FALSE;
74
75 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077
78 vp << pEvent->ChangeEx();
79 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080}
81
Tom Sepezba038bc2015-10-08 12:03:00 -070082FX_BOOL event::commitKey(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 CJS_PropValue& vp,
84 CFX_WideString& sError) {
85 if (!vp.IsGetting())
86 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090
91 vp << pEvent->CommitKey();
92 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}
94
Tom Sepezba038bc2015-10-08 12:03:00 -070095FX_BOOL event::fieldFull(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 CJS_PropValue& vp,
97 CFX_WideString& sError) {
98 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 if (!vp.IsGetting() &&
102 wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
103 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 if (pEvent->FieldFull())
106 vp << TRUE;
107 else
108 vp << FALSE;
109 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110}
111
Tom Sepezba038bc2015-10-08 12:03:00 -0700112FX_BOOL event::keyDown(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 CJS_PropValue& vp,
114 CFX_WideString& sError) {
115 if (!vp.IsGetting())
116 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 if (pEvent->KeyDown())
122 vp << TRUE;
123 else
124 vp << FALSE;
125 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700126}
127
Tom Sepezba038bc2015-10-08 12:03:00 -0700128FX_BOOL event::modifier(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 CJS_PropValue& vp,
130 CFX_WideString& sError) {
131 if (!vp.IsGetting())
132 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 if (pEvent->Modifier())
138 vp << TRUE;
139 else
140 vp << FALSE;
141 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142}
143
Tom Sepezba038bc2015-10-08 12:03:00 -0700144FX_BOOL event::name(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 CJS_PropValue& vp,
146 CFX_WideString& sError) {
147 if (!vp.IsGetting())
148 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 vp << pEvent->Name();
154 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155}
156
Tom Sepezba038bc2015-10-08 12:03:00 -0700157FX_BOOL event::rc(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 FX_BOOL& bRc = pEvent->Rc();
162 if (vp.IsSetting()) {
163 vp >> bRc;
164 } else {
165 vp << bRc;
166 }
167 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168}
169
Tom Sepezba038bc2015-10-08 12:03:00 -0700170FX_BOOL event::richChange(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 CJS_PropValue& vp,
172 CFX_WideString& sError) {
173 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174}
175
Tom Sepezba038bc2015-10-08 12:03:00 -0700176FX_BOOL event::richChangeEx(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 CJS_PropValue& vp,
178 CFX_WideString& sError) {
179 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180}
181
Tom Sepezba038bc2015-10-08 12:03:00 -0700182FX_BOOL event::richValue(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 CJS_PropValue& vp,
184 CFX_WideString& sError) {
185 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186}
187
Tom Sepezba038bc2015-10-08 12:03:00 -0700188FX_BOOL event::selEnd(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 CJS_PropValue& vp,
190 CFX_WideString& sError) {
191 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 CJS_EventHandler* pEvent = pContext->GetEventHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193
194 if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) {
195 return TRUE;
196 }
197
198 int& iSelEnd = pEvent->SelEnd();
199 if (vp.IsSetting()) {
200 vp >> iSelEnd;
201 } else {
202 vp << iSelEnd;
203 }
204 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205}
206
Tom Sepezba038bc2015-10-08 12:03:00 -0700207FX_BOOL event::selStart(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 CJS_PropValue& vp,
209 CFX_WideString& sError) {
210 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) {
214 return TRUE;
215 }
216 int& iSelStart = pEvent->SelStart();
217 if (vp.IsSetting()) {
218 vp >> iSelStart;
219 } else {
220 vp << iSelStart;
221 }
222 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223}
224
Tom Sepezba038bc2015-10-08 12:03:00 -0700225FX_BOOL event::shift(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 CJS_PropValue& vp,
227 CFX_WideString& sError) {
228 if (!vp.IsGetting())
229 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 if (pEvent->Shift())
235 vp << TRUE;
236 else
237 vp << FALSE;
238 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239}
240
Tom Sepezba038bc2015-10-08 12:03:00 -0700241FX_BOOL event::source(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();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249
250 vp << pEvent->Source()->GetJSObject();
251 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252}
253
Tom Sepezba038bc2015-10-08 12:03:00 -0700254FX_BOOL event::target(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 CJS_PropValue& vp,
256 CFX_WideString& sError) {
257 if (!vp.IsGetting())
258 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 vp << pEvent->Target_Field()->GetJSObject();
264 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265}
266
Tom Sepezba038bc2015-10-08 12:03:00 -0700267FX_BOOL event::targetName(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 CJS_PropValue& vp,
269 CFX_WideString& sError) {
270 if (!vp.IsGetting())
271 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 vp << pEvent->TargetName();
277 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278}
279
Tom Sepezba038bc2015-10-08 12:03:00 -0700280FX_BOOL event::type(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 CJS_PropValue& vp,
282 CFX_WideString& sError) {
283 if (!vp.IsGetting())
284 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 vp << pEvent->Type();
290 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291}
292
Tom Sepezba038bc2015-10-08 12:03:00 -0700293FX_BOOL event::value(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 CJS_PropValue& vp,
295 CFX_WideString& sError) {
296 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 if (wcscmp((const wchar_t*)pEvent->Type(), L"Field") != 0)
300 return FALSE;
301 if (!pEvent->m_pValue)
302 return FALSE;
303 CFX_WideString& val = pEvent->Value();
304 if (vp.IsSetting()) {
305 vp >> val;
306 } else {
307 vp << val;
308 }
309 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700310}
311
Tom Sepezba038bc2015-10-08 12:03:00 -0700312FX_BOOL event::willCommit(IJS_Context* cc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 CJS_PropValue& vp,
314 CFX_WideString& sError) {
315 if (!vp.IsGetting())
316 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 CJS_Context* pContext = (CJS_Context*)cc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 CJS_EventHandler* pEvent = pContext->GetEventHandler();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700320
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 if (pEvent->WillCommit())
322 vp << TRUE;
323 else
324 vp << FALSE;
325 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700326}