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