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