clang-format all pdfium code.
[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(void) {}
54
55 FX_BOOL event::change(IFXJS_Context* cc,
56                       CJS_PropValue& vp,
57                       CFX_WideString& sError) {
58   CJS_Context* pContext = (CJS_Context*)cc;
59   ASSERT(pContext != NULL);
60   CJS_EventHandler* pEvent = pContext->GetEventHandler();
61   ASSERT(pEvent != NULL);
62
63   CFX_WideString& wChange = pEvent->Change();
64   if (vp.IsSetting()) {
65     if (vp.GetType() == VT_string)
66       vp >> wChange;
67   } else {
68     vp << wChange;
69   }
70   return TRUE;
71 }
72
73 FX_BOOL event::changeEx(IFXJS_Context* cc,
74                         CJS_PropValue& vp,
75                         CFX_WideString& sError) {
76   if (!vp.IsGetting())
77     return FALSE;
78
79   CJS_Context* pContext = (CJS_Context*)cc;
80   ASSERT(pContext != NULL);
81   CJS_EventHandler* pEvent = pContext->GetEventHandler();
82   ASSERT(pEvent != NULL);
83
84   vp << pEvent->ChangeEx();
85   return TRUE;
86 }
87
88 FX_BOOL event::commitKey(IFXJS_Context* cc,
89                          CJS_PropValue& vp,
90                          CFX_WideString& sError) {
91   if (!vp.IsGetting())
92     return FALSE;
93
94   CJS_Context* pContext = (CJS_Context*)cc;
95   ASSERT(pContext != NULL);
96   CJS_EventHandler* pEvent = pContext->GetEventHandler();
97   ASSERT(pEvent != NULL);
98
99   vp << pEvent->CommitKey();
100   return TRUE;
101 }
102
103 FX_BOOL event::fieldFull(IFXJS_Context* cc,
104                          CJS_PropValue& vp,
105                          CFX_WideString& sError) {
106   CJS_Context* pContext = (CJS_Context*)cc;
107   ASSERT(pContext != NULL);
108   CJS_EventHandler* pEvent = pContext->GetEventHandler();
109   ASSERT(pEvent != NULL);
110
111   if (!vp.IsGetting() &&
112       wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0)
113     return FALSE;
114
115   if (pEvent->FieldFull())
116     vp << TRUE;
117   else
118     vp << FALSE;
119   return TRUE;
120 }
121
122 FX_BOOL event::keyDown(IFXJS_Context* cc,
123                        CJS_PropValue& vp,
124                        CFX_WideString& sError) {
125   if (!vp.IsGetting())
126     return FALSE;
127
128   CJS_Context* pContext = (CJS_Context*)cc;
129   ASSERT(pContext != NULL);
130   CJS_EventHandler* pEvent = pContext->GetEventHandler();
131   ASSERT(pEvent != NULL);
132
133   if (pEvent->KeyDown())
134     vp << TRUE;
135   else
136     vp << FALSE;
137   return TRUE;
138 }
139
140 FX_BOOL event::modifier(IFXJS_Context* cc,
141                         CJS_PropValue& vp,
142                         CFX_WideString& sError) {
143   if (!vp.IsGetting())
144     return FALSE;
145
146   CJS_Context* pContext = (CJS_Context*)cc;
147   ASSERT(pContext != NULL);
148   CJS_EventHandler* pEvent = pContext->GetEventHandler();
149   ASSERT(pEvent != NULL);
150
151   if (pEvent->Modifier())
152     vp << TRUE;
153   else
154     vp << FALSE;
155   return TRUE;
156 }
157
158 FX_BOOL event::name(IFXJS_Context* cc,
159                     CJS_PropValue& vp,
160                     CFX_WideString& sError) {
161   if (!vp.IsGetting())
162     return FALSE;
163
164   CJS_Context* pContext = (CJS_Context*)cc;
165   ASSERT(pContext != NULL);
166   CJS_EventHandler* pEvent = pContext->GetEventHandler();
167   ASSERT(pEvent != NULL);
168
169   vp << pEvent->Name();
170   return TRUE;
171 }
172
173 FX_BOOL event::rc(IFXJS_Context* cc,
174                   CJS_PropValue& vp,
175                   CFX_WideString& sError) {
176   CJS_Context* pContext = (CJS_Context*)cc;
177   ASSERT(pContext != NULL);
178   CJS_EventHandler* pEvent = pContext->GetEventHandler();
179   ASSERT(pEvent != NULL);
180
181   FX_BOOL& bRc = pEvent->Rc();
182   if (vp.IsSetting()) {
183     vp >> bRc;
184   } else {
185     vp << bRc;
186   }
187   return TRUE;
188 }
189
190 FX_BOOL event::richChange(IFXJS_Context* cc,
191                           CJS_PropValue& vp,
192                           CFX_WideString& sError) {
193   return TRUE;
194   if (vp.IsSetting()) {
195   } else {
196     ;
197   }
198   return TRUE;
199 }
200
201 FX_BOOL event::richChangeEx(IFXJS_Context* cc,
202                             CJS_PropValue& vp,
203                             CFX_WideString& sError) {
204   return TRUE;
205   if (vp.IsSetting()) {
206   } else {
207     ;
208   }
209   return TRUE;
210 }
211
212 FX_BOOL event::richValue(IFXJS_Context* cc,
213                          CJS_PropValue& vp,
214                          CFX_WideString& sError) {
215   return TRUE;
216   if (vp.IsSetting()) {
217   } else {
218     ;
219   }
220   return TRUE;
221 }
222
223 FX_BOOL event::selEnd(IFXJS_Context* cc,
224                       CJS_PropValue& vp,
225                       CFX_WideString& sError) {
226   CJS_Context* pContext = (CJS_Context*)cc;
227   ASSERT(pContext != NULL);
228   CJS_EventHandler* pEvent = pContext->GetEventHandler();
229   ASSERT(pEvent != NULL);
230
231   if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) {
232     return TRUE;
233   }
234
235   int& iSelEnd = pEvent->SelEnd();
236   if (vp.IsSetting()) {
237     vp >> iSelEnd;
238   } else {
239     vp << iSelEnd;
240   }
241   return TRUE;
242 }
243
244 FX_BOOL event::selStart(IFXJS_Context* cc,
245                         CJS_PropValue& vp,
246                         CFX_WideString& sError) {
247   CJS_Context* pContext = (CJS_Context*)cc;
248   ASSERT(pContext != NULL);
249   CJS_EventHandler* pEvent = pContext->GetEventHandler();
250   ASSERT(pEvent != NULL);
251
252   if (wcscmp((const wchar_t*)pEvent->Name(), L"Keystroke") != 0) {
253     return TRUE;
254   }
255   int& iSelStart = pEvent->SelStart();
256   if (vp.IsSetting()) {
257     vp >> iSelStart;
258   } else {
259     vp << iSelStart;
260   }
261   return TRUE;
262 }
263
264 FX_BOOL event::shift(IFXJS_Context* cc,
265                      CJS_PropValue& vp,
266                      CFX_WideString& sError) {
267   if (!vp.IsGetting())
268     return FALSE;
269
270   CJS_Context* pContext = (CJS_Context*)cc;
271   ASSERT(pContext != NULL);
272   CJS_EventHandler* pEvent = pContext->GetEventHandler();
273   ASSERT(pEvent != NULL);
274
275   if (pEvent->Shift())
276     vp << TRUE;
277   else
278     vp << FALSE;
279   return TRUE;
280 }
281
282 FX_BOOL event::source(IFXJS_Context* cc,
283                       CJS_PropValue& vp,
284                       CFX_WideString& sError) {
285   if (!vp.IsGetting())
286     return FALSE;
287
288   CJS_Context* pContext = (CJS_Context*)cc;
289   ASSERT(pContext != NULL);
290   CJS_EventHandler* pEvent = pContext->GetEventHandler();
291   ASSERT(pEvent != NULL);
292
293   vp << pEvent->Source()->GetJSObject();
294   return TRUE;
295 }
296
297 FX_BOOL event::target(IFXJS_Context* cc,
298                       CJS_PropValue& vp,
299                       CFX_WideString& sError) {
300   if (!vp.IsGetting())
301     return FALSE;
302
303   CJS_Context* pContext = (CJS_Context*)cc;
304   ASSERT(pContext != NULL);
305   CJS_EventHandler* pEvent = pContext->GetEventHandler();
306   ASSERT(pEvent != NULL);
307
308   vp << pEvent->Target_Field()->GetJSObject();
309   return TRUE;
310 }
311
312 FX_BOOL event::targetName(IFXJS_Context* cc,
313                           CJS_PropValue& vp,
314                           CFX_WideString& sError) {
315   if (!vp.IsGetting())
316     return FALSE;
317
318   CJS_Context* pContext = (CJS_Context*)cc;
319   ASSERT(pContext != NULL);
320   CJS_EventHandler* pEvent = pContext->GetEventHandler();
321   ASSERT(pEvent != NULL);
322
323   vp << pEvent->TargetName();
324   return TRUE;
325 }
326
327 FX_BOOL event::type(IFXJS_Context* cc,
328                     CJS_PropValue& vp,
329                     CFX_WideString& sError) {
330   if (!vp.IsGetting())
331     return FALSE;
332
333   CJS_Context* pContext = (CJS_Context*)cc;
334   ASSERT(pContext != NULL);
335   CJS_EventHandler* pEvent = pContext->GetEventHandler();
336   ASSERT(pEvent != NULL);
337
338   vp << pEvent->Type();
339   return TRUE;
340 }
341
342 FX_BOOL event::value(IFXJS_Context* cc,
343                      CJS_PropValue& vp,
344                      CFX_WideString& sError) {
345   CJS_Context* pContext = (CJS_Context*)cc;
346   ASSERT(pContext != NULL);
347   CJS_EventHandler* pEvent = pContext->GetEventHandler();
348   ASSERT(pEvent != NULL);
349
350   if (wcscmp((const wchar_t*)pEvent->Type(), L"Field") != 0)
351     return FALSE;
352   if (!pEvent->m_pValue)
353     return FALSE;
354   CFX_WideString& val = pEvent->Value();
355   if (vp.IsSetting()) {
356     vp >> val;
357   } else {
358     vp << val;
359   }
360   return TRUE;
361 }
362
363 FX_BOOL event::willCommit(IFXJS_Context* cc,
364                           CJS_PropValue& vp,
365                           CFX_WideString& sError) {
366   if (!vp.IsGetting())
367     return FALSE;
368
369   CJS_Context* pContext = (CJS_Context*)cc;
370   ASSERT(pContext != NULL);
371   CJS_EventHandler* pEvent = pContext->GetEventHandler();
372   ASSERT(pEvent != NULL);
373
374   if (pEvent->WillCommit())
375     vp << TRUE;
376   else
377     vp << FALSE;
378   return TRUE;
379 }