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