Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fxfa / src / parser / xfa_script_eventpseudomodel.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 "../../../foxitlib.h"\r
8 #include "../common/xfa_utils.h"\r
9 #include "../common/xfa_object.h"\r
10 #include "../common/xfa_document.h"\r
11 #include "../common/xfa_parser.h"\r
12 #include "../common/xfa_script.h"\r
13 #include "../common/xfa_docdata.h"\r
14 #include "../common/xfa_doclayout.h"\r
15 #include "../common/xfa_debug.h"\r
16 #include "../common/xfa_localemgr.h"\r
17 #include "../common/xfa_fm2jsapi.h"\r
18 #include "xfa_debug_parser.h"\r
19 #include "xfa_script_eventpseudomodel.h"\r
20 CScript_EventPseudoModel::CScript_EventPseudoModel(CXFA_Document* pDocument)\r
21     : CXFA_OrdinaryObject(pDocument, XFA_ELEMENT_EventPseudoModel)\r
22 {\r
23     m_uScriptHash = XFA_HASHCODE_Event;\r
24 }\r
25 CScript_EventPseudoModel::~CScript_EventPseudoModel()\r
26 {\r
27 }\r
28 void Script_EventPseudoModel_StringProperty(FXJSE_HVALUE hValue, CFX_WideString& wsValue, FX_BOOL bSetting)\r
29 {\r
30     if (bSetting) {\r
31         CFX_ByteString bsValue;\r
32         FXJSE_Value_ToUTF8String(hValue, bsValue);\r
33         wsValue = CFX_WideString::FromUTF8(bsValue, bsValue.GetLength());\r
34     } else {\r
35         FXJSE_Value_SetUTF8String(hValue, FX_UTF8Encode(wsValue));\r
36     }\r
37 }\r
38 void Script_EventPseudoModel_InterProperty(FXJSE_HVALUE hValue, FX_INT32& iValue, FX_BOOL bSetting)\r
39 {\r
40     if (bSetting) {\r
41         iValue = FXJSE_Value_ToInteger(hValue);\r
42     } else {\r
43         FXJSE_Value_SetInteger(hValue, iValue);\r
44     }\r
45 }\r
46 void Script_EventPseudoModel_BooleanProperty(FXJSE_HVALUE hValue, FX_BOOL& bValue, FX_BOOL bSetting)\r
47 {\r
48     if (bSetting) {\r
49         bValue = FXJSE_Value_ToBoolean(hValue);\r
50     } else {\r
51         FXJSE_Value_SetBoolean(hValue, bValue);\r
52     }\r
53 }\r
54 void CScript_EventPseudoModel::Script_EventPseudoModel_Property(FXJSE_HVALUE hValue, FX_DWORD dwFlag, FX_BOOL bSetting)\r
55 {\r
56     IXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext();\r
57     if (!pScriptContext) {\r
58         return;\r
59     }\r
60     CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();\r
61     if (!pEventParam) {\r
62         return;\r
63     }\r
64     switch (dwFlag) {\r
65         case XFA_EVENT_CANCELACTION:\r
66             Script_EventPseudoModel_BooleanProperty(hValue, pEventParam->m_bCancelAction, bSetting);\r
67             break;\r
68         case XFA_EVENT_CHANGE:\r
69             Script_EventPseudoModel_StringProperty(hValue, pEventParam->m_wsChange, bSetting);\r
70             break;\r
71         case XFA_EVENT_COMMITKEY:\r
72             Script_EventPseudoModel_InterProperty(hValue, pEventParam->m_iCommitKey, bSetting);\r
73             break;\r
74         case XFA_EVENT_FULLTEXT:\r
75             Script_EventPseudoModel_StringProperty(hValue, pEventParam->m_wsFullText, bSetting);\r
76             break;\r
77         case XFA_EVENT_KEYDOWN:\r
78             Script_EventPseudoModel_BooleanProperty(hValue, pEventParam->m_bKeyDown, bSetting);\r
79             break;\r
80         case XFA_EVENT_MODIFIER:\r
81             Script_EventPseudoModel_BooleanProperty(hValue, pEventParam->m_bModifier, bSetting);\r
82             break;\r
83         case XFA_EVENT_NEWCONTENTTYPE:\r
84             Script_EventPseudoModel_StringProperty(hValue, pEventParam->m_wsNewContentType, bSetting);\r
85             break;\r
86         case XFA_EVENT_NEWTEXT:\r
87             Script_EventPseudoModel_StringProperty(hValue, pEventParam->m_wsNewText, bSetting);\r
88             break;\r
89         case XFA_EVENT_PREVCONTENTTYPE:\r
90             Script_EventPseudoModel_StringProperty(hValue, pEventParam->m_wsPrevContentType, bSetting);\r
91             break;\r
92         case XFA_EVENT_PREVTEXT:\r
93             Script_EventPseudoModel_StringProperty(hValue, pEventParam->m_wsPrevText, bSetting);\r
94             break;\r
95         case XFA_EVENT_REENTER:\r
96             Script_EventPseudoModel_BooleanProperty(hValue, pEventParam->m_bReenter, bSetting);\r
97             break;\r
98         case XFA_EVENT_SELEND:\r
99             Script_EventPseudoModel_InterProperty(hValue, pEventParam->m_iSelEnd, bSetting);\r
100             break;\r
101         case XFA_EVENT_SELSTART:\r
102             Script_EventPseudoModel_InterProperty(hValue, pEventParam->m_iSelStart, bSetting);\r
103             break;\r
104         case XFA_EVENT_SHIFT:\r
105             Script_EventPseudoModel_BooleanProperty(hValue, pEventParam->m_bShift, bSetting);\r
106             break;\r
107         case XFA_EVENT_SOAPFAULTCODE:\r
108             Script_EventPseudoModel_StringProperty(hValue, pEventParam->m_wsSoapFaultCode, bSetting);\r
109             break;\r
110         case XFA_EVENT_SOAPFAULTSTRING:\r
111             Script_EventPseudoModel_StringProperty(hValue, pEventParam->m_wsSoapFaultString, bSetting);\r
112             break;\r
113         case XFA_EVENT_TARGET:\r
114             break;\r
115         default:\r
116             break;\r
117     }\r
118 }\r
119 void CScript_EventPseudoModel::Script_EventPseudoModel_CancelAction(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
120 {\r
121     Script_EventPseudoModel_Property(hValue, XFA_EVENT_CANCELACTION, bSetting);\r
122 }\r
123 void CScript_EventPseudoModel::Script_EventPseudoModel_Change(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
124 {\r
125     Script_EventPseudoModel_Property(hValue, XFA_EVENT_CHANGE, bSetting);\r
126 }\r
127 void CScript_EventPseudoModel::Script_EventPseudoModel_CommitKey(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
128 {\r
129     Script_EventPseudoModel_Property(hValue, XFA_EVENT_COMMITKEY, bSetting);\r
130 }\r
131 void CScript_EventPseudoModel::Script_EventPseudoModel_FullText(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
132 {\r
133     Script_EventPseudoModel_Property(hValue, XFA_EVENT_FULLTEXT, bSetting);\r
134 }\r
135 void CScript_EventPseudoModel::Script_EventPseudoModel_KeyDown(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
136 {\r
137     Script_EventPseudoModel_Property(hValue, XFA_EVENT_KEYDOWN, bSetting);\r
138 }\r
139 void CScript_EventPseudoModel::Script_EventPseudoModel_Modifier(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
140 {\r
141     Script_EventPseudoModel_Property(hValue, XFA_EVENT_MODIFIER, bSetting);\r
142 }\r
143 void CScript_EventPseudoModel::Script_EventPseudoModel_NewContentType(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
144 {\r
145     Script_EventPseudoModel_Property(hValue, XFA_EVENT_NEWCONTENTTYPE, bSetting);\r
146 }\r
147 void CScript_EventPseudoModel::Script_EventPseudoModel_NewText(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
148 {\r
149     Script_EventPseudoModel_Property(hValue, XFA_EVENT_NEWTEXT, bSetting);\r
150 }\r
151 void CScript_EventPseudoModel::Script_EventPseudoModel_PrevContentType(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
152 {\r
153     Script_EventPseudoModel_Property(hValue, XFA_EVENT_PREVCONTENTTYPE, bSetting);\r
154 }\r
155 void CScript_EventPseudoModel::Script_EventPseudoModel_PrevText(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
156 {\r
157     Script_EventPseudoModel_Property(hValue, XFA_EVENT_PREVTEXT, bSetting);\r
158 }\r
159 void CScript_EventPseudoModel::Script_EventPseudoModel_Reenter(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
160 {\r
161     Script_EventPseudoModel_Property(hValue, XFA_EVENT_REENTER, bSetting);\r
162 }\r
163 void CScript_EventPseudoModel::Script_EventPseudoModel_SelEnd(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
164 {\r
165     Script_EventPseudoModel_Property(hValue, XFA_EVENT_SELEND, bSetting);\r
166 }\r
167 void CScript_EventPseudoModel::Script_EventPseudoModel_SelStart(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
168 {\r
169     Script_EventPseudoModel_Property(hValue, XFA_EVENT_SELSTART, bSetting);\r
170 }\r
171 void CScript_EventPseudoModel::Script_EventPseudoModel_Shift(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
172 {\r
173     Script_EventPseudoModel_Property(hValue, XFA_EVENT_SHIFT, bSetting);\r
174 }\r
175 void CScript_EventPseudoModel::Script_EventPseudoModel_SoapFaultCode(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
176 {\r
177     Script_EventPseudoModel_Property(hValue, XFA_EVENT_SOAPFAULTCODE, bSetting);\r
178 }\r
179 void CScript_EventPseudoModel::Script_EventPseudoModel_SoapFaultString(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
180 {\r
181     Script_EventPseudoModel_Property(hValue, XFA_EVENT_SOAPFAULTSTRING, bSetting);\r
182 }\r
183 void CScript_EventPseudoModel::Script_EventPseudoModel_Target(FXJSE_HVALUE hValue, FX_BOOL bSetting, XFA_ATTRIBUTE eAttribute)\r
184 {\r
185     Script_EventPseudoModel_Property(hValue, XFA_EVENT_TARGET, bSetting);\r
186 }\r
187 void CScript_EventPseudoModel::Script_EventPseudoModel_Emit(CFXJSE_Arguments* pArguments)\r
188 {\r
189     IXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext();\r
190     if (!pScriptContext) {\r
191         return;\r
192     }\r
193     CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();\r
194     if (!pEventParam) {\r
195         return;\r
196     }\r
197     IXFA_Notify* pNotify = m_pDocument->GetParser()->GetNotify();\r
198     if (!pNotify) {\r
199         return;\r
200     }\r
201     IXFA_WidgetHandler* pWidgetHandler = pNotify->GetWidgetHandler();\r
202     if (!pWidgetHandler) {\r
203         return;\r
204     }\r
205     pWidgetHandler->ProcessEvent(pEventParam->m_pTarget, pEventParam);\r
206 }\r
207 void CScript_EventPseudoModel::Script_EventPseudoModel_Reset(CFXJSE_Arguments* pArguments)\r
208 {\r
209     IXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext();\r
210     if (!pScriptContext) {\r
211         return;\r
212     }\r
213     CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();\r
214     if (!pEventParam) {\r
215         return;\r
216     }\r
217     pEventParam->Reset();\r
218 }\r