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 "fxjs/cjs_event.h"
8
9 #include "fxjs/JS_Define.h"
10 #include "fxjs/cjs_event_context.h"
11 #include "fxjs/cjs_eventhandler.h"
12 #include "fxjs/cjs_field.h"
13 #include "fxjs/cjs_object.h"
14
15 const JSPropertySpec CJS_Event::PropertySpecs[] = {
16 {"change", get_change_static, set_change_static},
17 {"changeEx", get_change_ex_static, set_change_ex_static},
18 {"commitKey", get_commit_key_static, set_commit_key_static},
19 {"fieldFull", get_field_full_static, set_field_full_static},
20 {"keyDown", get_key_down_static, set_key_down_static},
21 {"modifier", get_modifier_static, set_modifier_static},
22 {"name", get_name_static, set_name_static},
23 {"rc", get_rc_static, set_rc_static},
24 {"richChange", get_rich_change_static, set_rich_change_static},
25 {"richChangeEx", get_rich_change_ex_static, set_rich_change_ex_static},
26 {"richValue", get_rich_value_static, set_rich_value_static},
27 {"selEnd", get_sel_end_static, set_sel_end_static},
28 {"selStart", get_sel_start_static, set_sel_start_static},
29 {"shift", get_shift_static, set_shift_static},
30 {"source", get_source_static, set_source_static},
31 {"target", get_target_static, set_target_static},
32 {"targetName", get_target_name_static, set_target_name_static},
33 {"type", get_type_static, set_type_static},
34 {"value", get_value_static, set_value_static},
35 {"willCommit", get_will_commit_static, set_will_commit_static}};
36
37 int CJS_Event::ObjDefnID = -1;
38
39 // static
DefineJSObjects(CFXJS_Engine * pEngine)40 void CJS_Event::DefineJSObjects(CFXJS_Engine* pEngine) {
41 ObjDefnID = pEngine->DefineObj("event", FXJSOBJTYPE_STATIC,
42 JSConstructor<CJS_Event, event>,
43 JSDestructor<CJS_Event>);
44 DefineProps(pEngine, ObjDefnID, PropertySpecs, FX_ArraySize(PropertySpecs));
45 }
46
event(CJS_Object * pJsObject)47 event::event(CJS_Object* pJsObject) : CJS_EmbedObj(pJsObject) {}
48
~event()49 event::~event() {}
50
get_change(CJS_Runtime * pRuntime)51 CJS_Return event::get_change(CJS_Runtime* pRuntime) {
52 CJS_EventHandler* pEvent =
53 pRuntime->GetCurrentEventContext()->GetEventHandler();
54 return CJS_Return(pRuntime->NewString(pEvent->Change().c_str()));
55 }
56
set_change(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)57 CJS_Return event::set_change(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
58 CJS_EventHandler* pEvent =
59 pRuntime->GetCurrentEventContext()->GetEventHandler();
60
61 if (vp->IsString()) {
62 WideString& wChange = pEvent->Change();
63 wChange = pRuntime->ToWideString(vp);
64 }
65 return CJS_Return(true);
66 }
67
get_change_ex(CJS_Runtime * pRuntime)68 CJS_Return event::get_change_ex(CJS_Runtime* pRuntime) {
69 CJS_EventHandler* pEvent =
70 pRuntime->GetCurrentEventContext()->GetEventHandler();
71
72 return CJS_Return(pRuntime->NewString(pEvent->ChangeEx().c_str()));
73 }
74
set_change_ex(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)75 CJS_Return event::set_change_ex(CJS_Runtime* pRuntime,
76 v8::Local<v8::Value> vp) {
77 return CJS_Return(false);
78 }
79
get_commit_key(CJS_Runtime * pRuntime)80 CJS_Return event::get_commit_key(CJS_Runtime* pRuntime) {
81 CJS_EventHandler* pEvent =
82 pRuntime->GetCurrentEventContext()->GetEventHandler();
83
84 return CJS_Return(pRuntime->NewNumber(pEvent->CommitKey()));
85 }
86
set_commit_key(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)87 CJS_Return event::set_commit_key(CJS_Runtime* pRuntime,
88 v8::Local<v8::Value> vp) {
89 return CJS_Return(false);
90 }
91
get_field_full(CJS_Runtime * pRuntime)92 CJS_Return event::get_field_full(CJS_Runtime* pRuntime) {
93 CJS_EventHandler* pEvent =
94 pRuntime->GetCurrentEventContext()->GetEventHandler();
95
96 if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
97 return CJS_Return(false);
98
99 return CJS_Return(pRuntime->NewBoolean(pEvent->FieldFull()));
100 }
101
set_field_full(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)102 CJS_Return event::set_field_full(CJS_Runtime* pRuntime,
103 v8::Local<v8::Value> vp) {
104 return CJS_Return(false);
105 }
106
get_key_down(CJS_Runtime * pRuntime)107 CJS_Return event::get_key_down(CJS_Runtime* pRuntime) {
108 CJS_EventHandler* pEvent =
109 pRuntime->GetCurrentEventContext()->GetEventHandler();
110 return CJS_Return(pRuntime->NewBoolean(pEvent->KeyDown()));
111 }
112
set_key_down(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)113 CJS_Return event::set_key_down(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
114 return CJS_Return(false);
115 }
116
get_modifier(CJS_Runtime * pRuntime)117 CJS_Return event::get_modifier(CJS_Runtime* pRuntime) {
118 CJS_EventHandler* pEvent =
119 pRuntime->GetCurrentEventContext()->GetEventHandler();
120 return CJS_Return(pRuntime->NewBoolean(pEvent->Modifier()));
121 }
122
set_modifier(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)123 CJS_Return event::set_modifier(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
124 return CJS_Return(false);
125 }
126
get_name(CJS_Runtime * pRuntime)127 CJS_Return event::get_name(CJS_Runtime* pRuntime) {
128 CJS_EventHandler* pEvent =
129 pRuntime->GetCurrentEventContext()->GetEventHandler();
130 return CJS_Return(pRuntime->NewString(pEvent->Name()));
131 }
132
set_name(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)133 CJS_Return event::set_name(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
134 return CJS_Return(false);
135 }
136
get_rc(CJS_Runtime * pRuntime)137 CJS_Return event::get_rc(CJS_Runtime* pRuntime) {
138 CJS_EventHandler* pEvent =
139 pRuntime->GetCurrentEventContext()->GetEventHandler();
140 return CJS_Return(pRuntime->NewBoolean(pEvent->Rc()));
141 }
142
set_rc(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)143 CJS_Return event::set_rc(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
144 CJS_EventHandler* pEvent =
145 pRuntime->GetCurrentEventContext()->GetEventHandler();
146 pEvent->Rc() = pRuntime->ToBoolean(vp);
147 return CJS_Return(true);
148 }
149
get_rich_change(CJS_Runtime * pRuntime)150 CJS_Return event::get_rich_change(CJS_Runtime* pRuntime) {
151 return CJS_Return(true);
152 }
153
set_rich_change(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)154 CJS_Return event::set_rich_change(CJS_Runtime* pRuntime,
155 v8::Local<v8::Value> vp) {
156 return CJS_Return(true);
157 }
158
get_rich_change_ex(CJS_Runtime * pRuntime)159 CJS_Return event::get_rich_change_ex(CJS_Runtime* pRuntime) {
160 return CJS_Return(true);
161 }
162
set_rich_change_ex(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)163 CJS_Return event::set_rich_change_ex(CJS_Runtime* pRuntime,
164 v8::Local<v8::Value> vp) {
165 return CJS_Return(true);
166 }
167
get_rich_value(CJS_Runtime * pRuntime)168 CJS_Return event::get_rich_value(CJS_Runtime* pRuntime) {
169 return CJS_Return(true);
170 }
171
set_rich_value(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)172 CJS_Return event::set_rich_value(CJS_Runtime* pRuntime,
173 v8::Local<v8::Value> vp) {
174 return CJS_Return(true);
175 }
176
get_sel_end(CJS_Runtime * pRuntime)177 CJS_Return event::get_sel_end(CJS_Runtime* pRuntime) {
178 CJS_EventHandler* pEvent =
179 pRuntime->GetCurrentEventContext()->GetEventHandler();
180
181 if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
182 return CJS_Return(true);
183
184 return CJS_Return(pRuntime->NewNumber(pEvent->SelEnd()));
185 }
186
set_sel_end(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)187 CJS_Return event::set_sel_end(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
188 CJS_EventHandler* pEvent =
189 pRuntime->GetCurrentEventContext()->GetEventHandler();
190
191 if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
192 return CJS_Return(true);
193
194 pEvent->SetSelEnd(pRuntime->ToInt32(vp));
195 return CJS_Return(true);
196 }
197
get_sel_start(CJS_Runtime * pRuntime)198 CJS_Return event::get_sel_start(CJS_Runtime* pRuntime) {
199 CJS_EventHandler* pEvent =
200 pRuntime->GetCurrentEventContext()->GetEventHandler();
201
202 if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
203 return CJS_Return(true);
204
205 return CJS_Return(pRuntime->NewNumber(pEvent->SelStart()));
206 }
207
set_sel_start(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)208 CJS_Return event::set_sel_start(CJS_Runtime* pRuntime,
209 v8::Local<v8::Value> vp) {
210 CJS_EventHandler* pEvent =
211 pRuntime->GetCurrentEventContext()->GetEventHandler();
212
213 if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
214 return CJS_Return(true);
215
216 pEvent->SetSelStart(pRuntime->ToInt32(vp));
217 return CJS_Return(true);
218 }
219
get_shift(CJS_Runtime * pRuntime)220 CJS_Return event::get_shift(CJS_Runtime* pRuntime) {
221 CJS_EventHandler* pEvent =
222 pRuntime->GetCurrentEventContext()->GetEventHandler();
223 return CJS_Return(pRuntime->NewBoolean(pEvent->Shift()));
224 }
225
set_shift(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)226 CJS_Return event::set_shift(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
227 return CJS_Return(false);
228 }
229
get_source(CJS_Runtime * pRuntime)230 CJS_Return event::get_source(CJS_Runtime* pRuntime) {
231 CJS_EventHandler* pEvent =
232 pRuntime->GetCurrentEventContext()->GetEventHandler();
233 return CJS_Return(pEvent->Source()->GetJSObject()->ToV8Object());
234 }
235
set_source(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)236 CJS_Return event::set_source(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
237 return CJS_Return(false);
238 }
239
get_target(CJS_Runtime * pRuntime)240 CJS_Return event::get_target(CJS_Runtime* pRuntime) {
241 CJS_EventHandler* pEvent =
242 pRuntime->GetCurrentEventContext()->GetEventHandler();
243 return CJS_Return(pEvent->Target_Field()->GetJSObject()->ToV8Object());
244 }
245
set_target(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)246 CJS_Return event::set_target(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
247 return CJS_Return(false);
248 }
249
get_target_name(CJS_Runtime * pRuntime)250 CJS_Return event::get_target_name(CJS_Runtime* pRuntime) {
251 CJS_EventHandler* pEvent =
252 pRuntime->GetCurrentEventContext()->GetEventHandler();
253 return CJS_Return(pRuntime->NewString(pEvent->TargetName().c_str()));
254 }
255
set_target_name(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)256 CJS_Return event::set_target_name(CJS_Runtime* pRuntime,
257 v8::Local<v8::Value> vp) {
258 return CJS_Return(false);
259 }
260
get_type(CJS_Runtime * pRuntime)261 CJS_Return event::get_type(CJS_Runtime* pRuntime) {
262 CJS_EventHandler* pEvent =
263 pRuntime->GetCurrentEventContext()->GetEventHandler();
264 return CJS_Return(pRuntime->NewString(pEvent->Type()));
265 }
266
set_type(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)267 CJS_Return event::set_type(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
268 return CJS_Return(false);
269 }
270
get_value(CJS_Runtime * pRuntime)271 CJS_Return event::get_value(CJS_Runtime* pRuntime) {
272 CJS_EventHandler* pEvent =
273 pRuntime->GetCurrentEventContext()->GetEventHandler();
274
275 if (wcscmp((const wchar_t*)pEvent->Type(), L"Field") != 0)
276 return CJS_Return(false);
277
278 if (!pEvent->m_pValue)
279 return CJS_Return(false);
280
281 return CJS_Return(pRuntime->NewString(pEvent->Value().c_str()));
282 }
283
set_value(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)284 CJS_Return event::set_value(CJS_Runtime* pRuntime, v8::Local<v8::Value> vp) {
285 CJS_EventHandler* pEvent =
286 pRuntime->GetCurrentEventContext()->GetEventHandler();
287
288 if (wcscmp((const wchar_t*)pEvent->Type(), L"Field") != 0)
289 return CJS_Return(false);
290
291 if (!pEvent->m_pValue)
292 return CJS_Return(false);
293
294 pEvent->Value() = pRuntime->ToWideString(vp);
295 return CJS_Return(true);
296 }
297
get_will_commit(CJS_Runtime * pRuntime)298 CJS_Return event::get_will_commit(CJS_Runtime* pRuntime) {
299 CJS_EventHandler* pEvent =
300 pRuntime->GetCurrentEventContext()->GetEventHandler();
301 return CJS_Return(pRuntime->NewBoolean(pEvent->WillCommit()));
302 }
303
set_will_commit(CJS_Runtime * pRuntime,v8::Local<v8::Value> vp)304 CJS_Return event::set_will_commit(CJS_Runtime* pRuntime,
305 v8::Local<v8::Value> vp) {
306 return CJS_Return(false);
307 }
308