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 "xfa/fxfa/parser/cscript_eventpseudomodel.h"
8
9 #include "fxjs/cfxjse_arguments.h"
10 #include "xfa/fxfa/app/xfa_ffnotify.h"
11 #include "xfa/fxfa/cxfa_eventparam.h"
12 #include "xfa/fxfa/parser/cxfa_document.h"
13 #include "xfa/fxfa/parser/cxfa_scriptcontext.h"
14 #include "xfa/fxfa/parser/xfa_localemgr.h"
15 #include "xfa/fxfa/parser/xfa_object.h"
16 #include "xfa/fxfa/parser/xfa_utils.h"
17 #include "xfa/fxfa/xfa_ffwidgethandler.h"
18
19 namespace {
20
StringProperty(CFXJSE_Value * pValue,CFX_WideString & wsValue,bool bSetting)21 void StringProperty(CFXJSE_Value* pValue,
22 CFX_WideString& wsValue,
23 bool bSetting) {
24 if (bSetting) {
25 wsValue = pValue->ToWideString();
26 return;
27 }
28 pValue->SetString(wsValue.UTF8Encode().AsStringC());
29 }
30
InterProperty(CFXJSE_Value * pValue,int32_t & iValue,bool bSetting)31 void InterProperty(CFXJSE_Value* pValue, int32_t& iValue, bool bSetting) {
32 if (bSetting) {
33 iValue = pValue->ToInteger();
34 return;
35 }
36 pValue->SetInteger(iValue);
37 }
38
BooleanProperty(CFXJSE_Value * pValue,bool & bValue,bool bSetting)39 void BooleanProperty(CFXJSE_Value* pValue, bool& bValue, bool bSetting) {
40 if (bSetting) {
41 bValue = pValue->ToBoolean();
42 return;
43 }
44 pValue->SetBoolean(bValue);
45 }
46
47 } // namespace
48
CScript_EventPseudoModel(CXFA_Document * pDocument)49 CScript_EventPseudoModel::CScript_EventPseudoModel(CXFA_Document* pDocument)
50 : CXFA_Object(pDocument,
51 XFA_ObjectType::Object,
52 XFA_Element::EventPseudoModel,
53 CFX_WideStringC(L"eventPseudoModel")) {}
54
~CScript_EventPseudoModel()55 CScript_EventPseudoModel::~CScript_EventPseudoModel() {}
56
Property(CFXJSE_Value * pValue,XFA_Event dwFlag,bool bSetting)57 void CScript_EventPseudoModel::Property(CFXJSE_Value* pValue,
58 XFA_Event dwFlag,
59 bool bSetting) {
60 CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext();
61 if (!pScriptContext)
62 return;
63
64 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();
65 if (!pEventParam)
66 return;
67
68 switch (dwFlag) {
69 case XFA_Event::CancelAction:
70 BooleanProperty(pValue, pEventParam->m_bCancelAction, bSetting);
71 break;
72 case XFA_Event::Change:
73 StringProperty(pValue, pEventParam->m_wsChange, bSetting);
74 break;
75 case XFA_Event::CommitKey:
76 InterProperty(pValue, pEventParam->m_iCommitKey, bSetting);
77 break;
78 case XFA_Event::FullText:
79 StringProperty(pValue, pEventParam->m_wsFullText, bSetting);
80 break;
81 case XFA_Event::Keydown:
82 BooleanProperty(pValue, pEventParam->m_bKeyDown, bSetting);
83 break;
84 case XFA_Event::Modifier:
85 BooleanProperty(pValue, pEventParam->m_bModifier, bSetting);
86 break;
87 case XFA_Event::NewContentType:
88 StringProperty(pValue, pEventParam->m_wsNewContentType, bSetting);
89 break;
90 case XFA_Event::NewText:
91 StringProperty(pValue, pEventParam->m_wsNewText, bSetting);
92 break;
93 case XFA_Event::PreviousContentType:
94 StringProperty(pValue, pEventParam->m_wsPrevContentType, bSetting);
95 break;
96 case XFA_Event::PreviousText:
97 StringProperty(pValue, pEventParam->m_wsPrevText, bSetting);
98 break;
99 case XFA_Event::Reenter:
100 BooleanProperty(pValue, pEventParam->m_bReenter, bSetting);
101 break;
102 case XFA_Event::SelectionEnd:
103 InterProperty(pValue, pEventParam->m_iSelEnd, bSetting);
104 break;
105 case XFA_Event::SelectionStart:
106 InterProperty(pValue, pEventParam->m_iSelStart, bSetting);
107 break;
108 case XFA_Event::Shift:
109 BooleanProperty(pValue, pEventParam->m_bShift, bSetting);
110 break;
111 case XFA_Event::SoapFaultCode:
112 StringProperty(pValue, pEventParam->m_wsSoapFaultCode, bSetting);
113 break;
114 case XFA_Event::SoapFaultString:
115 StringProperty(pValue, pEventParam->m_wsSoapFaultString, bSetting);
116 break;
117 case XFA_Event::Target:
118 break;
119 default:
120 break;
121 }
122 }
Change(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)123 void CScript_EventPseudoModel::Change(CFXJSE_Value* pValue,
124 bool bSetting,
125 XFA_ATTRIBUTE eAttribute) {
126 Property(pValue, XFA_Event::Change, bSetting);
127 }
CommitKey(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)128 void CScript_EventPseudoModel::CommitKey(CFXJSE_Value* pValue,
129 bool bSetting,
130 XFA_ATTRIBUTE eAttribute) {
131 Property(pValue, XFA_Event::CommitKey, bSetting);
132 }
FullText(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)133 void CScript_EventPseudoModel::FullText(CFXJSE_Value* pValue,
134 bool bSetting,
135 XFA_ATTRIBUTE eAttribute) {
136 Property(pValue, XFA_Event::FullText, bSetting);
137 }
KeyDown(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)138 void CScript_EventPseudoModel::KeyDown(CFXJSE_Value* pValue,
139 bool bSetting,
140 XFA_ATTRIBUTE eAttribute) {
141 Property(pValue, XFA_Event::Keydown, bSetting);
142 }
Modifier(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)143 void CScript_EventPseudoModel::Modifier(CFXJSE_Value* pValue,
144 bool bSetting,
145 XFA_ATTRIBUTE eAttribute) {
146 Property(pValue, XFA_Event::Modifier, bSetting);
147 }
NewContentType(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)148 void CScript_EventPseudoModel::NewContentType(CFXJSE_Value* pValue,
149 bool bSetting,
150 XFA_ATTRIBUTE eAttribute) {
151 Property(pValue, XFA_Event::NewContentType, bSetting);
152 }
NewText(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)153 void CScript_EventPseudoModel::NewText(CFXJSE_Value* pValue,
154 bool bSetting,
155 XFA_ATTRIBUTE eAttribute) {
156 Property(pValue, XFA_Event::NewText, bSetting);
157 }
PrevContentType(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)158 void CScript_EventPseudoModel::PrevContentType(CFXJSE_Value* pValue,
159 bool bSetting,
160 XFA_ATTRIBUTE eAttribute) {
161 Property(pValue, XFA_Event::PreviousContentType, bSetting);
162 }
PrevText(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)163 void CScript_EventPseudoModel::PrevText(CFXJSE_Value* pValue,
164 bool bSetting,
165 XFA_ATTRIBUTE eAttribute) {
166 Property(pValue, XFA_Event::PreviousText, bSetting);
167 }
Reenter(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)168 void CScript_EventPseudoModel::Reenter(CFXJSE_Value* pValue,
169 bool bSetting,
170 XFA_ATTRIBUTE eAttribute) {
171 Property(pValue, XFA_Event::Reenter, bSetting);
172 }
SelEnd(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)173 void CScript_EventPseudoModel::SelEnd(CFXJSE_Value* pValue,
174 bool bSetting,
175 XFA_ATTRIBUTE eAttribute) {
176 Property(pValue, XFA_Event::SelectionEnd, bSetting);
177 }
SelStart(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)178 void CScript_EventPseudoModel::SelStart(CFXJSE_Value* pValue,
179 bool bSetting,
180 XFA_ATTRIBUTE eAttribute) {
181 Property(pValue, XFA_Event::SelectionStart, bSetting);
182 }
Shift(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)183 void CScript_EventPseudoModel::Shift(CFXJSE_Value* pValue,
184 bool bSetting,
185 XFA_ATTRIBUTE eAttribute) {
186 Property(pValue, XFA_Event::Shift, bSetting);
187 }
SoapFaultCode(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)188 void CScript_EventPseudoModel::SoapFaultCode(CFXJSE_Value* pValue,
189 bool bSetting,
190 XFA_ATTRIBUTE eAttribute) {
191 Property(pValue, XFA_Event::SoapFaultCode, bSetting);
192 }
SoapFaultString(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)193 void CScript_EventPseudoModel::SoapFaultString(CFXJSE_Value* pValue,
194 bool bSetting,
195 XFA_ATTRIBUTE eAttribute) {
196 Property(pValue, XFA_Event::SoapFaultString, bSetting);
197 }
Target(CFXJSE_Value * pValue,bool bSetting,XFA_ATTRIBUTE eAttribute)198 void CScript_EventPseudoModel::Target(CFXJSE_Value* pValue,
199 bool bSetting,
200 XFA_ATTRIBUTE eAttribute) {
201 Property(pValue, XFA_Event::Target, bSetting);
202 }
Emit(CFXJSE_Arguments * pArguments)203 void CScript_EventPseudoModel::Emit(CFXJSE_Arguments* pArguments) {
204 CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext();
205 if (!pScriptContext) {
206 return;
207 }
208 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();
209 if (!pEventParam) {
210 return;
211 }
212 CXFA_FFNotify* pNotify = m_pDocument->GetNotify();
213 if (!pNotify) {
214 return;
215 }
216 CXFA_FFWidgetHandler* pWidgetHandler = pNotify->GetWidgetHandler();
217 if (!pWidgetHandler) {
218 return;
219 }
220 pWidgetHandler->ProcessEvent(pEventParam->m_pTarget, pEventParam);
221 }
Reset(CFXJSE_Arguments * pArguments)222 void CScript_EventPseudoModel::Reset(CFXJSE_Arguments* pArguments) {
223 CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext();
224 if (!pScriptContext) {
225 return;
226 }
227 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();
228 if (!pEventParam) {
229 return;
230 }
231 pEventParam->Reset();
232 }
233