blob: 05b4dd06a5c9ebfb74eaf2517bfea7d7f77cedfa [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangc2fb35f2016-01-05 16:46:58 -08007#include "fpdfsdk/include/fsdk_baseform.h"
8
Tom Sepezb9cc7a02016-02-01 13:42:30 -08009#include <algorithm>
tsepez16a20332016-04-22 14:00:47 -070010#include <map>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080011#include <memory>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050012#include <vector>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080013
Dan Sinclair455a4192016-03-16 09:48:56 -040014#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040015#include "core/fpdfapi/fpdf_parser/include/cfdf_document.h"
16#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
17#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
Dan Sinclair584b1e62016-03-21 09:15:45 -040018#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
thestig25fa42f2016-05-25 21:39:46 -070019#include "core/fxge/include/fx_ge.h"
Dan Sinclairedbb3192016-03-21 09:08:24 -040020#include "fpdfsdk/formfiller/cffl_formfiller.h"
dsinclaire35af1e2016-07-13 11:26:20 -070021#include "fpdfsdk/fxedit/include/fxet_edit.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080022#include "fpdfsdk/include/fsdk_actionhandler.h"
23#include "fpdfsdk/include/fsdk_baseannot.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080024#include "fpdfsdk/include/fsdk_define.h"
25#include "fpdfsdk/include/fsdk_mgr.h"
dsinclair64376be2016-03-31 20:03:24 -070026#include "fpdfsdk/javascript/ijs_context.h"
27#include "fpdfsdk/javascript/ijs_runtime.h"
dan sinclair89e904b2016-03-23 19:29:15 -040028#include "fpdfsdk/pdfwindow/PWL_Utils.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Tom Sepez40e9ff32015-11-30 12:39:54 -080030#ifdef PDF_ENABLE_XFA
dsinclair89bdd082016-04-06 10:47:54 -070031#include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h"
32#include "fpdfsdk/fpdfxfa/include/fpdfxfa_util.h"
weili625ad662016-06-15 11:21:33 -070033#include "xfa/fxfa/include/cxfa_eventparam.h"
34#include "xfa/fxfa/include/xfa_ffdocview.h"
dsinclair7222ea62016-04-06 14:33:07 -070035#include "xfa/fxfa/include/xfa_ffwidget.h"
weili625ad662016-06-15 11:21:33 -070036#include "xfa/fxfa/include/xfa_ffwidgethandler.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080037#endif // PDF_ENABLE_XFA
38
weili625ad662016-06-15 11:21:33 -070039PDFSDK_FieldAction::PDFSDK_FieldAction()
40 : bModifier(FALSE),
41 bShift(FALSE),
42 nCommitKey(0),
43 bKeyDown(FALSE),
44 nSelEnd(0),
45 nSelStart(0),
46 bWillCommit(FALSE),
47 bFieldFull(FALSE),
48 bRC(TRUE) {}
49
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050CPDFSDK_Widget::CPDFSDK_Widget(CPDF_Annot* pAnnot,
51 CPDFSDK_PageView* pPageView,
52 CPDFSDK_InterForm* pInterForm)
53 : CPDFSDK_BAAnnot(pAnnot, pPageView),
54 m_pInterForm(pInterForm),
55 m_nAppAge(0),
Tom Sepez40e9ff32015-11-30 12:39:54 -080056 m_nValueAge(0)
57#ifdef PDF_ENABLE_XFA
58 ,
thestig1cd352e2016-06-07 17:53:06 -070059 m_hMixXFAWidget(nullptr),
60 m_pWidgetHandler(nullptr)
Tom Sepez40e9ff32015-11-30 12:39:54 -080061#endif // PDF_ENABLE_XFA
62{
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063}
64
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065CPDFSDK_Widget::~CPDFSDK_Widget() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066
Tom Sepez51da0932015-11-25 16:05:49 -080067#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -070068CXFA_FFWidget* CPDFSDK_Widget::GetMixXFAWidget() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080070 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
72 if (!m_hMixXFAWidget) {
dsinclairdf4bc592016-03-31 20:34:43 -070073 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 CFX_WideString sName;
Lei Zhang99766722016-02-23 11:21:48 -080075 if (GetFieldType() == FIELDTYPE_RADIOBUTTON) {
76 sName = GetAnnotName();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 if (sName.IsEmpty())
78 sName = GetName();
Dan Sinclair738b08c2016-03-01 14:45:20 -050079 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 sName = GetName();
Dan Sinclair738b08c2016-03-01 14:45:20 -050081 }
Bo Xufdc00a72014-10-28 23:03:33 -070082
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 if (!sName.IsEmpty())
tsepez70c55202016-04-14 15:32:35 -070084 m_hMixXFAWidget = pDocView->GetWidgetByName(sName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -070086 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 return m_hMixXFAWidget;
88 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070089
thestig1cd352e2016-06-07 17:53:06 -070090 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091}
92
dsinclairdf4bc592016-03-31 20:34:43 -070093CXFA_FFWidget* CPDFSDK_Widget::GetGroupMixXFAWidget() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080095 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
dsinclairdf4bc592016-03-31 20:34:43 -070097 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 CFX_WideString sName = GetName();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 if (!sName.IsEmpty())
tsepez70c55202016-04-14 15:32:35 -0700100 return pDocView->GetWidgetByName(sName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 }
102 }
103
Tom Sepez50d12ad2015-11-24 09:50:51 -0800104 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105}
106
dsinclairdf4bc592016-03-31 20:34:43 -0700107CXFA_FFWidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800109 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
111 if (!m_pWidgetHandler) {
dsinclairdf4bc592016-03-31 20:34:43 -0700112 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 m_pWidgetHandler = pDocView->GetWidgetHandler();
114 }
115 }
116 return m_pWidgetHandler;
117 }
118
thestig1cd352e2016-06-07 17:53:06 -0700119 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120}
121
122static XFA_EVENTTYPE GetXFAEventType(PDFSDK_XFAAActionType eXFAAAT) {
123 XFA_EVENTTYPE eEventType = XFA_EVENT_Unknown;
124
125 switch (eXFAAAT) {
126 case PDFSDK_XFA_Click:
127 eEventType = XFA_EVENT_Click;
128 break;
129 case PDFSDK_XFA_Full:
130 eEventType = XFA_EVENT_Full;
131 break;
132 case PDFSDK_XFA_PreOpen:
133 eEventType = XFA_EVENT_PreOpen;
134 break;
135 case PDFSDK_XFA_PostOpen:
136 eEventType = XFA_EVENT_PostOpen;
137 break;
138 }
139
140 return eEventType;
141}
142
143static XFA_EVENTTYPE GetXFAEventType(CPDF_AAction::AActionType eAAT,
144 FX_BOOL bWillCommit) {
145 XFA_EVENTTYPE eEventType = XFA_EVENT_Unknown;
146
147 switch (eAAT) {
148 case CPDF_AAction::CursorEnter:
149 eEventType = XFA_EVENT_MouseEnter;
150 break;
151 case CPDF_AAction::CursorExit:
152 eEventType = XFA_EVENT_MouseExit;
153 break;
154 case CPDF_AAction::ButtonDown:
155 eEventType = XFA_EVENT_MouseDown;
156 break;
157 case CPDF_AAction::ButtonUp:
158 eEventType = XFA_EVENT_MouseUp;
159 break;
160 case CPDF_AAction::GetFocus:
161 eEventType = XFA_EVENT_Enter;
162 break;
163 case CPDF_AAction::LoseFocus:
164 eEventType = XFA_EVENT_Exit;
165 break;
166 case CPDF_AAction::PageOpen:
167 break;
168 case CPDF_AAction::PageClose:
169 break;
170 case CPDF_AAction::PageVisible:
171 break;
172 case CPDF_AAction::PageInvisible:
173 break;
174 case CPDF_AAction::KeyStroke:
175 if (!bWillCommit) {
176 eEventType = XFA_EVENT_Change;
177 }
178 break;
179 case CPDF_AAction::Validate:
180 eEventType = XFA_EVENT_Validate;
181 break;
182 case CPDF_AAction::OpenPage:
183 case CPDF_AAction::ClosePage:
184 case CPDF_AAction::Format:
185 case CPDF_AAction::Calculate:
186 case CPDF_AAction::CloseDocument:
187 case CPDF_AAction::SaveDocument:
188 case CPDF_AAction::DocumentSaved:
189 case CPDF_AAction::PrintDocument:
190 case CPDF_AAction::DocumentPrinted:
191 break;
192 }
193
194 return eEventType;
195}
196
197FX_BOOL CPDFSDK_Widget::HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT) {
dsinclairdf4bc592016-03-31 20:34:43 -0700198 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
199 if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
201
202 if ((eEventType == XFA_EVENT_Click || eEventType == XFA_EVENT_Change) &&
203 GetFieldType() == FIELDTYPE_RADIOBUTTON) {
dsinclairdf4bc592016-03-31 20:34:43 -0700204 if (CXFA_FFWidget* hGroupWidget = GetGroupMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700205 CXFA_WidgetAcc* pAcc = hGroupWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 if (pXFAWidgetHandler->HasEvent(pAcc, eEventType))
207 return TRUE;
208 }
209 }
210
211 {
dsinclair221caf62016-04-04 12:08:40 -0700212 CXFA_WidgetAcc* pAcc = hWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 return pXFAWidgetHandler->HasEvent(pAcc, eEventType);
214 }
215 }
216 }
217
218 return FALSE;
219}
220
221FX_BOOL CPDFSDK_Widget::OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
222 PDFSDK_FieldAction& data,
223 CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800225 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -0700226 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
228
229 if (eEventType != XFA_EVENT_Unknown) {
dsinclairdf4bc592016-03-31 20:34:43 -0700230 if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 CXFA_EventParam param;
232 param.m_eType = eEventType;
233 param.m_wsChange = data.sChange;
234 param.m_iCommitKey = data.nCommitKey;
235 param.m_bShift = data.bShift;
236 param.m_iSelStart = data.nSelStart;
237 param.m_iSelEnd = data.nSelEnd;
238 param.m_wsFullText = data.sValue;
239 param.m_bKeyDown = data.bKeyDown;
240 param.m_bModifier = data.bModifier;
241 param.m_wsNewText = data.sValue;
242 if (data.nSelEnd > data.nSelStart)
243 param.m_wsNewText.Delete(data.nSelStart,
244 data.nSelEnd - data.nSelStart);
245 for (int i = 0; i < data.sChange.GetLength(); i++)
246 param.m_wsNewText.Insert(data.nSelStart, data.sChange[i]);
247 param.m_wsPrevText = data.sValue;
248
249 if ((eEventType == XFA_EVENT_Click || eEventType == XFA_EVENT_Change) &&
250 GetFieldType() == FIELDTYPE_RADIOBUTTON) {
dsinclairdf4bc592016-03-31 20:34:43 -0700251 if (CXFA_FFWidget* hGroupWidget = GetGroupMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700252 CXFA_WidgetAcc* pAcc = hGroupWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 param.m_pTarget = pAcc;
Wei Li42d1c1c2016-03-22 12:52:50 -0700254 if (pXFAWidgetHandler->ProcessEvent(pAcc, &param) !=
255 XFA_EVENTERROR_Success) {
256 return FALSE;
257 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 }
dsinclair221caf62016-04-04 12:08:40 -0700260 CXFA_WidgetAcc* pAcc = hWidget->GetDataAcc();
Wei Li42d1c1c2016-03-22 12:52:50 -0700261 param.m_pTarget = pAcc;
262 int32_t nRet = pXFAWidgetHandler->ProcessEvent(pAcc, &param);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263
dsinclairdf4bc592016-03-31 20:34:43 -0700264 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 pDocView->UpdateDocView();
266 }
Wei Li42d1c1c2016-03-22 12:52:50 -0700267 return nRet == XFA_EVENTERROR_Success;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 }
269 }
270 }
271
272 return FALSE;
273}
274
275void CPDFSDK_Widget::Synchronize(FX_BOOL bSynchronizeElse) {
dsinclairdf4bc592016-03-31 20:34:43 -0700276 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700277 CPDF_FormField* pFormField = GetFormField();
278 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
279 switch (GetFieldType()) {
280 case FIELDTYPE_CHECKBOX:
281 case FIELDTYPE_RADIOBUTTON: {
282 CPDF_FormControl* pFormCtrl = GetFormControl();
283 XFA_CHECKSTATE eCheckState =
284 pFormCtrl->IsChecked() ? XFA_CHECKSTATE_On : XFA_CHECKSTATE_Off;
thestig800222e2016-05-26 22:00:29 -0700285 pWidgetAcc->SetCheckState(eCheckState, true);
dsinclair221caf62016-04-04 12:08:40 -0700286 } break;
287 case FIELDTYPE_TEXTFIELD:
288 pWidgetAcc->SetValue(pFormField->GetValue(), XFA_VALUEPICTURE_Edit);
289 break;
290 case FIELDTYPE_LISTBOX: {
291 pWidgetAcc->ClearAllSelections();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292
dsinclair221caf62016-04-04 12:08:40 -0700293 for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz; i++) {
294 int nIndex = pFormField->GetSelectedIndex(i);
295 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
thestig800222e2016-05-26 22:00:29 -0700296 pWidgetAcc->SetItemState(nIndex, TRUE, false, FALSE, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 }
dsinclair221caf62016-04-04 12:08:40 -0700298 } break;
299 case FIELDTYPE_COMBOBOX: {
300 pWidgetAcc->ClearAllSelections();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301
dsinclair221caf62016-04-04 12:08:40 -0700302 for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz; i++) {
303 int nIndex = pFormField->GetSelectedIndex(i);
304 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
thestig800222e2016-05-26 22:00:29 -0700305 pWidgetAcc->SetItemState(nIndex, TRUE, false, FALSE, TRUE);
dsinclair221caf62016-04-04 12:08:40 -0700306 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 }
308
dsinclair221caf62016-04-04 12:08:40 -0700309 pWidgetAcc->SetValue(pFormField->GetValue(), XFA_VALUEPICTURE_Edit);
310 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 }
dsinclair221caf62016-04-04 12:08:40 -0700312
313 if (bSynchronizeElse)
314 pWidgetAcc->ProcessValueChanged();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 }
316 }
317}
318
319void CPDFSDK_Widget::SynchronizeXFAValue() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800321 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -0700322 CXFA_FFDocView* pXFADocView = pDoc->GetXFADocView();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 if (!pXFADocView)
324 return;
325
dsinclairdf4bc592016-03-31 20:34:43 -0700326 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700327 if (GetXFAWidgetHandler()) {
328 CPDFSDK_Widget::SynchronizeXFAValue(pXFADocView, hWidget, GetFormField(),
329 GetFormControl());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 }
331 }
332}
333
334void CPDFSDK_Widget::SynchronizeXFAItems() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800336 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -0700337 CXFA_FFDocView* pXFADocView = pDoc->GetXFADocView();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 if (!pXFADocView)
339 return;
340
dsinclairdf4bc592016-03-31 20:34:43 -0700341 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700342 if (GetXFAWidgetHandler())
343 SynchronizeXFAItems(pXFADocView, hWidget, GetFormField(), nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 }
345}
346
dsinclairdf4bc592016-03-31 20:34:43 -0700347void CPDFSDK_Widget::SynchronizeXFAValue(CXFA_FFDocView* pXFADocView,
348 CXFA_FFWidget* hWidget,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 CPDF_FormField* pFormField,
350 CPDF_FormControl* pFormControl) {
Lei Zhang5eca3052016-02-22 20:32:21 -0800351 ASSERT(hWidget);
dsinclair221caf62016-04-04 12:08:40 -0700352 ASSERT(pFormControl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353
dsinclair221caf62016-04-04 12:08:40 -0700354 switch (pFormField->GetFieldType()) {
355 case FIELDTYPE_CHECKBOX: {
356 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
357 pFormField->CheckControl(
358 pFormField->GetControlIndex(pFormControl),
359 pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On, true);
360 }
361 } break;
362 case FIELDTYPE_RADIOBUTTON: {
363 // TODO(weili): Check whether we need to handle checkbox and radio
364 // button differently, otherwise, merge these two cases.
365 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
366 pFormField->CheckControl(
367 pFormField->GetControlIndex(pFormControl),
368 pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On, true);
369 }
370 } break;
371 case FIELDTYPE_TEXTFIELD: {
372 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
373 CFX_WideString sValue;
374 pWidgetAcc->GetValue(sValue, XFA_VALUEPICTURE_Display);
375 pFormField->SetValue(sValue, TRUE);
376 }
377 } break;
378 case FIELDTYPE_LISTBOX: {
379 pFormField->ClearSelection(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380
dsinclair221caf62016-04-04 12:08:40 -0700381 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
382 for (int i = 0, sz = pWidgetAcc->CountSelectedItems(); i < sz; i++) {
383 int nIndex = pWidgetAcc->GetSelectedItem(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384
dsinclair221caf62016-04-04 12:08:40 -0700385 if (nIndex > -1 && nIndex < pFormField->CountOptions()) {
386 pFormField->SetItemSelection(nIndex, TRUE, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 }
388 }
dsinclair221caf62016-04-04 12:08:40 -0700389 }
390 } break;
391 case FIELDTYPE_COMBOBOX: {
392 pFormField->ClearSelection(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393
dsinclair221caf62016-04-04 12:08:40 -0700394 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
395 for (int i = 0, sz = pWidgetAcc->CountSelectedItems(); i < sz; i++) {
396 int nIndex = pWidgetAcc->GetSelectedItem(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397
dsinclair221caf62016-04-04 12:08:40 -0700398 if (nIndex > -1 && nIndex < pFormField->CountOptions()) {
399 pFormField->SetItemSelection(nIndex, TRUE, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 }
dsinclair221caf62016-04-04 12:08:40 -0700402
403 CFX_WideString sValue;
404 pWidgetAcc->GetValue(sValue, XFA_VALUEPICTURE_Display);
405 pFormField->SetValue(sValue, TRUE);
406 }
407 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408 }
409}
410
dsinclairdf4bc592016-03-31 20:34:43 -0700411void CPDFSDK_Widget::SynchronizeXFAItems(CXFA_FFDocView* pXFADocView,
412 CXFA_FFWidget* hWidget,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413 CPDF_FormField* pFormField,
414 CPDF_FormControl* pFormControl) {
Lei Zhang5eca3052016-02-22 20:32:21 -0800415 ASSERT(hWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416
dsinclair221caf62016-04-04 12:08:40 -0700417 switch (pFormField->GetFieldType()) {
418 case FIELDTYPE_LISTBOX: {
419 pFormField->ClearSelection(FALSE);
420 pFormField->ClearOptions(TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421
dsinclair221caf62016-04-04 12:08:40 -0700422 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
423 for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(); i < sz; i++) {
424 CFX_WideString swText;
425 pWidgetAcc->GetChoiceListItem(swText, i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426
dsinclair221caf62016-04-04 12:08:40 -0700427 pFormField->InsertOption(swText, i, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 }
dsinclair221caf62016-04-04 12:08:40 -0700429 }
430 } break;
431 case FIELDTYPE_COMBOBOX: {
432 pFormField->ClearSelection(FALSE);
433 pFormField->ClearOptions(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434
dsinclair221caf62016-04-04 12:08:40 -0700435 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
436 for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(); i < sz; i++) {
437 CFX_WideString swText;
438 pWidgetAcc->GetChoiceListItem(swText, i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439
dsinclair221caf62016-04-04 12:08:40 -0700440 pFormField->InsertOption(swText, i, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700441 }
dsinclair221caf62016-04-04 12:08:40 -0700442 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443
dsinclair221caf62016-04-04 12:08:40 -0700444 pFormField->SetValue(L"", TRUE);
445 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700446 }
447}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800448#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449
450FX_BOOL CPDFSDK_Widget::IsWidgetAppearanceValid(
451 CPDF_Annot::AppearanceMode mode) {
Wei Li9b761132016-01-29 15:44:20 -0800452 CPDF_Dictionary* pAP = m_pAnnot->GetAnnotDict()->GetDictBy("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 if (!pAP)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700454 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700455
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 // Choose the right sub-ap
457 const FX_CHAR* ap_entry = "N";
458 if (mode == CPDF_Annot::Down)
459 ap_entry = "D";
460 else if (mode == CPDF_Annot::Rollover)
461 ap_entry = "R";
462 if (!pAP->KeyExist(ap_entry))
463 ap_entry = "N";
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700464
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 // Get the AP stream or subdirectory
tsepezbd567552016-03-29 14:51:50 -0700466 CPDF_Object* psub = pAP->GetDirectObjectBy(ap_entry);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 if (!psub)
468 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700469
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 int nFieldType = GetFieldType();
471 switch (nFieldType) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700472 case FIELDTYPE_PUSHBUTTON:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700473 case FIELDTYPE_COMBOBOX:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700474 case FIELDTYPE_LISTBOX:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700475 case FIELDTYPE_TEXTFIELD:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 case FIELDTYPE_SIGNATURE:
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400477 return psub->IsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 case FIELDTYPE_CHECKBOX:
479 case FIELDTYPE_RADIOBUTTON:
Dan Sinclairf1251c12015-10-20 16:24:45 -0400480 if (CPDF_Dictionary* pSubDict = psub->AsDictionary()) {
tsepez7b1ccf92016-04-14 11:04:57 -0700481 return !!pSubDict->GetStreamBy(GetAppState());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482 }
483 return FALSE;
484 }
485 return TRUE;
486}
487
488int CPDFSDK_Widget::GetFieldType() const {
Lei Zhang96660d62015-12-14 18:27:25 -0800489 return GetFormField()->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490}
491
492FX_BOOL CPDFSDK_Widget::IsAppearanceValid() {
Tom Sepez51da0932015-11-25 16:05:49 -0800493#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800495 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 int nDocType = pDoc->GetDocType();
Tom Sepez540c4362015-11-24 13:33:57 -0800497 if (nDocType != DOCTYPE_PDF && nDocType != DOCTYPE_STATIC_XFA)
498 return TRUE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800499#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800500 return CPDFSDK_BAAnnot::IsAppearanceValid();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700501}
502
weili625ad662016-06-15 11:21:33 -0700503int CPDFSDK_Widget::GetLayoutOrder() const {
504 return 2;
505}
506
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700507int CPDFSDK_Widget::GetFieldFlags() const {
508 CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 CPDF_FormControl* pFormControl =
510 pPDFInterForm->GetControlByDict(m_pAnnot->GetAnnotDict());
511 CPDF_FormField* pFormField = pFormControl->GetField();
512 return pFormField->GetFieldFlags();
513}
514
515CFX_ByteString CPDFSDK_Widget::GetSubType() const {
516 int nType = GetFieldType();
517
518 if (nType == FIELDTYPE_SIGNATURE)
519 return BFFT_SIGNATURE;
520 return CPDFSDK_Annot::GetSubType();
521}
522
523CPDF_FormField* CPDFSDK_Widget::GetFormField() const {
Lei Zhang1b700c32015-10-30 23:55:35 -0700524 return GetFormControl()->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525}
526
527CPDF_FormControl* CPDFSDK_Widget::GetFormControl() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528 CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700529 return pPDFInterForm->GetControlByDict(GetAnnotDict());
530}
531
Lei Zhang1b700c32015-10-30 23:55:35 -0700532CPDF_FormControl* CPDFSDK_Widget::GetFormControl(
533 CPDF_InterForm* pInterForm,
534 const CPDF_Dictionary* pAnnotDict) {
Lei Zhang96660d62015-12-14 18:27:25 -0800535 ASSERT(pAnnotDict);
Lei Zhang1b700c32015-10-30 23:55:35 -0700536 return pInterForm->GetControlByDict(pAnnotDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537}
538
539int CPDFSDK_Widget::GetRotate() const {
540 CPDF_FormControl* pCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 return pCtrl->GetRotation() % 360;
542}
543
Tom Sepez51da0932015-11-25 16:05:49 -0800544#ifdef PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -0700545CFX_WideString CPDFSDK_Widget::GetName() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700547 return pFormField->GetFullName();
548}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800549#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550
551FX_BOOL CPDFSDK_Widget::GetFillColor(FX_COLORREF& color) const {
552 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553 int iColorType = 0;
554 color = FX_ARGBTOCOLORREF(pFormCtrl->GetBackgroundColor(iColorType));
555
556 return iColorType != COLORTYPE_TRANSPARENT;
557}
558
559FX_BOOL CPDFSDK_Widget::GetBorderColor(FX_COLORREF& color) const {
560 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 int iColorType = 0;
562 color = FX_ARGBTOCOLORREF(pFormCtrl->GetBorderColor(iColorType));
563
564 return iColorType != COLORTYPE_TRANSPARENT;
565}
566
567FX_BOOL CPDFSDK_Widget::GetTextColor(FX_COLORREF& color) const {
568 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
570 if (da.HasColor()) {
571 FX_ARGB argb;
572 int iColorType = COLORTYPE_TRANSPARENT;
573 da.GetColor(argb, iColorType);
574 color = FX_ARGBTOCOLORREF(argb);
575
576 return iColorType != COLORTYPE_TRANSPARENT;
577 }
578
579 return FALSE;
580}
581
582FX_FLOAT CPDFSDK_Widget::GetFontSize() const {
583 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 CPDF_DefaultAppearance pDa = pFormCtrl->GetDefaultAppearance();
585 CFX_ByteString csFont = "";
586 FX_FLOAT fFontSize = 0.0f;
587 pDa.GetFont(csFont, fFontSize);
588
589 return fFontSize;
590}
591
Tom Sepezbf59a072015-10-21 14:07:23 -0700592int CPDFSDK_Widget::GetSelectedIndex(int nIndex) const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700593#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -0700594 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700595 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
596 if (nIndex < pWidgetAcc->CountSelectedItems())
597 return pWidgetAcc->GetSelectedItem(nIndex);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700598 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700600#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602 return pFormField->GetSelectedIndex(nIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700603}
604
Tom Sepeza8a39e22015-10-12 15:47:07 -0700605#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -0800606CFX_WideString CPDFSDK_Widget::GetValue(FX_BOOL bDisplay) const {
dsinclairdf4bc592016-03-31 20:34:43 -0700607 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700608 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
609 CFX_WideString sValue;
610 pWidgetAcc->GetValue(
611 sValue, bDisplay ? XFA_VALUEPICTURE_Display : XFA_VALUEPICTURE_Edit);
612 return sValue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700613 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800615#else
616CFX_WideString CPDFSDK_Widget::GetValue() const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700617#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 return pFormField->GetValue();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700620}
621
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700622CFX_WideString CPDFSDK_Widget::GetDefaultValue() const {
623 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700624 return pFormField->GetDefaultValue();
625}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700626
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700627CFX_WideString CPDFSDK_Widget::GetOptionLabel(int nIndex) const {
628 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700629 return pFormField->GetOptionLabel(nIndex);
630}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700631
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632int CPDFSDK_Widget::CountOptions() const {
633 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 return pFormField->CountOptions();
635}
636
Tom Sepezbf59a072015-10-21 14:07:23 -0700637FX_BOOL CPDFSDK_Widget::IsOptionSelected(int nIndex) const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700638#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -0700639 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700640 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
641 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
642 return pWidgetAcc->GetItemState(nIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643
dsinclair221caf62016-04-04 12:08:40 -0700644 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 }
646 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700647#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648 CPDF_FormField* pFormField = GetFormField();
649 return pFormField->IsItemSelected(nIndex);
650}
651
652int CPDFSDK_Widget::GetTopVisibleIndex() const {
653 CPDF_FormField* pFormField = GetFormField();
654 return pFormField->GetTopVisibleIndex();
655}
656
Wei Li97da9762016-03-11 17:00:48 -0800657bool CPDFSDK_Widget::IsChecked() const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700658#ifdef PDF_ENABLE_XFA
dsinclair221caf62016-04-04 12:08:40 -0700659 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
660 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc())
661 return pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700662 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700663#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700665 return pFormCtrl->IsChecked();
666}
667
668int CPDFSDK_Widget::GetAlignment() const {
669 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700670 return pFormCtrl->GetControlAlignment();
671}
672
673int CPDFSDK_Widget::GetMaxLen() const {
674 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 return pFormField->GetMaxLen();
676}
677
Wei Li97da9762016-03-11 17:00:48 -0800678void CPDFSDK_Widget::SetCheck(bool bChecked, bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700680 CPDF_FormField* pFormField = pFormCtrl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700681 pFormField->CheckControl(pFormField->GetControlIndex(pFormCtrl), bChecked,
682 bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800683#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700684 if (!IsWidgetAppearanceValid(CPDF_Annot::Normal))
685 ResetAppearance(TRUE);
686 if (!bNotify)
687 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800688#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700689}
690
691void CPDFSDK_Widget::SetValue(const CFX_WideString& sValue, FX_BOOL bNotify) {
692 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693 pFormField->SetValue(sValue, bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800694#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700695 if (!bNotify)
696 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800697#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700698}
699
700void CPDFSDK_Widget::SetDefaultValue(const CFX_WideString& sValue) {}
701void CPDFSDK_Widget::SetOptionSelection(int index,
702 FX_BOOL bSelected,
703 FX_BOOL bNotify) {
704 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700705 pFormField->SetItemSelection(index, bSelected, bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800706#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700707 if (!bNotify)
708 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800709#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700710}
711
712void CPDFSDK_Widget::ClearSelection(FX_BOOL bNotify) {
713 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714 pFormField->ClearSelection(bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800715#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700716 if (!bNotify)
717 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800718#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719}
720
721void CPDFSDK_Widget::SetTopVisibleIndex(int index) {}
722
723void CPDFSDK_Widget::SetAppModified() {
724 m_bAppModified = TRUE;
725}
726
727void CPDFSDK_Widget::ClearAppModified() {
728 m_bAppModified = FALSE;
729}
730
731FX_BOOL CPDFSDK_Widget::IsAppModified() const {
732 return m_bAppModified;
733}
734
Tom Sepez51da0932015-11-25 16:05:49 -0800735#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736void CPDFSDK_Widget::ResetAppearance(FX_BOOL bValueChanged) {
737 switch (GetFieldType()) {
738 case FIELDTYPE_TEXTFIELD:
739 case FIELDTYPE_COMBOBOX: {
740 FX_BOOL bFormated = FALSE;
Lei Zhang99766722016-02-23 11:21:48 -0800741 CFX_WideString sValue = OnFormat(bFormated);
tsepezbd9748d2016-04-13 21:40:19 -0700742 ResetAppearance(bFormated ? sValue.c_str() : nullptr, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743 } break;
744 default:
Lei Zhang99766722016-02-23 11:21:48 -0800745 ResetAppearance(nullptr, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700746 break;
747 }
748}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800749#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700750
751void CPDFSDK_Widget::ResetAppearance(const FX_WCHAR* sValue,
752 FX_BOOL bValueChanged) {
753 SetAppModified();
754
755 m_nAppAge++;
756 if (m_nAppAge > 999999)
757 m_nAppAge = 0;
758 if (bValueChanged)
759 m_nValueAge++;
760
761 int nFieldType = GetFieldType();
762
763 switch (nFieldType) {
764 case FIELDTYPE_PUSHBUTTON:
765 ResetAppearance_PushButton();
766 break;
767 case FIELDTYPE_CHECKBOX:
768 ResetAppearance_CheckBox();
769 break;
770 case FIELDTYPE_RADIOBUTTON:
771 ResetAppearance_RadioButton();
772 break;
773 case FIELDTYPE_COMBOBOX:
774 ResetAppearance_ComboBox(sValue);
775 break;
776 case FIELDTYPE_LISTBOX:
777 ResetAppearance_ListBox();
778 break;
779 case FIELDTYPE_TEXTFIELD:
780 ResetAppearance_TextField(sValue);
781 break;
782 }
783
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700784 m_pAnnot->ClearCachedAP();
785}
786
787CFX_WideString CPDFSDK_Widget::OnFormat(FX_BOOL& bFormated) {
788 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800789 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700790 return m_pInterForm->OnFormat(pFormField, bFormated);
791}
792
793void CPDFSDK_Widget::ResetFieldAppearance(FX_BOOL bValueChanged) {
794 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800795 ASSERT(pFormField);
thestig1cd352e2016-06-07 17:53:06 -0700796 m_pInterForm->ResetFieldAppearance(pFormField, nullptr, bValueChanged);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700797}
798
799void CPDFSDK_Widget::DrawAppearance(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800800 const CFX_Matrix* pUser2Device,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700801 CPDF_Annot::AppearanceMode mode,
802 const CPDF_RenderOptions* pOptions) {
803 int nFieldType = GetFieldType();
804
805 if ((nFieldType == FIELDTYPE_CHECKBOX ||
806 nFieldType == FIELDTYPE_RADIOBUTTON) &&
807 mode == CPDF_Annot::Normal &&
808 !IsWidgetAppearanceValid(CPDF_Annot::Normal)) {
809 CFX_PathData pathData;
810
Tom Sepez281a9ea2016-02-26 14:24:28 -0800811 CFX_FloatRect rcAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700812
813 pathData.AppendRect(rcAnnot.left, rcAnnot.bottom, rcAnnot.right,
814 rcAnnot.top);
815
816 CFX_GraphStateData gsd;
817 gsd.m_LineWidth = 0.0f;
818
819 pDevice->DrawPath(&pathData, pUser2Device, &gsd, 0, 0xFFAAAAAA,
820 FXFILL_ALTERNATE);
821 } else {
822 CPDFSDK_BAAnnot::DrawAppearance(pDevice, pUser2Device, mode, pOptions);
823 }
824}
825
826void CPDFSDK_Widget::UpdateField() {
827 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800828 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829 m_pInterForm->UpdateField(pFormField);
830}
831
832void CPDFSDK_Widget::DrawShadow(CFX_RenderDevice* pDevice,
833 CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700834 int nFieldType = GetFieldType();
835 if (m_pInterForm->IsNeedHighLight(nFieldType)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800836 CFX_FloatRect rc = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700837 FX_COLORREF color = m_pInterForm->GetHighlightColor(nFieldType);
838 uint8_t alpha = m_pInterForm->GetHighlightAlpha();
839
840 CFX_FloatRect rcDevice;
841 ASSERT(m_pInterForm->GetDocument());
842 CPDFDoc_Environment* pEnv = m_pInterForm->GetDocument()->GetEnv();
843 if (!pEnv)
844 return;
Tom Sepez60d909e2015-12-10 15:34:55 -0800845 CFX_Matrix page2device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846 pPageView->GetCurrentMatrix(page2device);
847 page2device.Transform(((FX_FLOAT)rc.left), ((FX_FLOAT)rc.bottom),
848 rcDevice.left, rcDevice.bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849 page2device.Transform(((FX_FLOAT)rc.right), ((FX_FLOAT)rc.top),
850 rcDevice.right, rcDevice.top);
851
852 rcDevice.Normalize();
853
854 FX_ARGB argb = ArgbEncode((int)alpha, color);
855 FX_RECT rcDev((int)rcDevice.left, (int)rcDevice.top, (int)rcDevice.right,
856 (int)rcDevice.bottom);
857 pDevice->FillRect(&rcDev, argb);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858 }
859}
860
861void CPDFSDK_Widget::ResetAppearance_PushButton() {
862 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800863 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700864 int32_t nLayout = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700865 switch (pControl->GetTextPosition()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700866 case TEXTPOS_ICON:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700867 nLayout = PPBL_ICON;
868 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700869 case TEXTPOS_BELOW:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700870 nLayout = PPBL_ICONTOPLABELBOTTOM;
871 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700872 case TEXTPOS_ABOVE:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873 nLayout = PPBL_LABELTOPICONBOTTOM;
874 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700875 case TEXTPOS_RIGHT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700876 nLayout = PPBL_ICONLEFTLABELRIGHT;
877 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700878 case TEXTPOS_LEFT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879 nLayout = PPBL_LABELLEFTICONRIGHT;
880 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700881 case TEXTPOS_OVERLAID:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700882 nLayout = PPBL_LABELOVERICON;
883 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700884 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885 nLayout = PPBL_LABEL;
886 break;
887 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700888
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 CPWL_Color crBackground, crBorder;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700890
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891 int iColorType;
892 FX_FLOAT fc[4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700893
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894 pControl->GetOriginalBackgroundColor(iColorType, fc);
895 if (iColorType > 0)
896 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700897
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898 pControl->GetOriginalBorderColor(iColorType, fc);
899 if (iColorType > 0)
900 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700901
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 CPWL_Dash dsBorder(3, 0, 0);
904 CPWL_Color crLeftTop, crRightBottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700905
dsinclair92cb5e52016-05-16 11:38:28 -0700906 BorderStyle nBorderStyle = GetBorderStyle();
907 switch (nBorderStyle) {
908 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700909 dsBorder = CPWL_Dash(3, 3, 0);
910 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700911 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700912 fBorderWidth *= 2;
913 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
914 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
915 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700916 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 fBorderWidth *= 2;
918 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
919 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
920 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700921 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700922 break;
923 }
924
Tom Sepez281a9ea2016-02-26 14:24:28 -0800925 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926
927 CPWL_Color crText(COLORTYPE_GRAY, 0);
928
929 FX_FLOAT fFontSize = 12.0f;
930 CFX_ByteString csNameTag;
931
932 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
933 if (da.HasColor()) {
934 da.GetColor(iColorType, fc);
935 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
936 }
937
938 if (da.HasFont())
939 da.GetFont(csNameTag, fFontSize);
940
941 CFX_WideString csWCaption;
942 CFX_WideString csNormalCaption, csRolloverCaption, csDownCaption;
943
944 if (pControl->HasMKEntry("CA")) {
945 csNormalCaption = pControl->GetNormalCaption();
946 }
947 if (pControl->HasMKEntry("RC")) {
948 csRolloverCaption = pControl->GetRolloverCaption();
949 }
950 if (pControl->HasMKEntry("AC")) {
951 csDownCaption = pControl->GetDownCaption();
952 }
953
thestig1cd352e2016-06-07 17:53:06 -0700954 CPDF_Stream* pNormalIcon = nullptr;
955 CPDF_Stream* pRolloverIcon = nullptr;
956 CPDF_Stream* pDownIcon = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957
958 if (pControl->HasMKEntry("I")) {
959 pNormalIcon = pControl->GetNormalIcon();
960 }
961 if (pControl->HasMKEntry("RI")) {
962 pRolloverIcon = pControl->GetRolloverIcon();
963 }
964 if (pControl->HasMKEntry("IX")) {
965 pDownIcon = pControl->GetDownIcon();
966 }
967
968 if (pNormalIcon) {
969 if (CPDF_Dictionary* pImageDict = pNormalIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800970 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700971 pImageDict->SetAtString("Name", "ImgA");
972 }
973 }
974
975 if (pRolloverIcon) {
976 if (CPDF_Dictionary* pImageDict = pRolloverIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800977 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978 pImageDict->SetAtString("Name", "ImgB");
979 }
980 }
981
982 if (pDownIcon) {
983 if (CPDF_Dictionary* pImageDict = pDownIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800984 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700985 pImageDict->SetAtString("Name", "ImgC");
986 }
987 }
988
989 CPDF_IconFit iconFit = pControl->GetIconFit();
990
991 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
993
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800994 CBA_FontMap font_map(this, pEnv->GetSysHandler());
995 font_map.SetAPType("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996
997 CFX_ByteString csAP =
998 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
999 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1000 crLeftTop, crRightBottom, nBorderStyle,
1001 dsBorder) +
1002 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001003 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 pNormalIcon, iconFit, csNormalCaption, crText, fFontSize, nLayout);
1005
1006 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP);
1007 if (pNormalIcon)
1008 AddImageToAppearance("N", pNormalIcon);
1009
1010 CPDF_FormControl::HighlightingMode eHLM = pControl->GetHighlightingMode();
1011 if (eHLM == CPDF_FormControl::Push || eHLM == CPDF_FormControl::Toggle) {
1012 if (csRolloverCaption.IsEmpty() && !pRolloverIcon) {
1013 csRolloverCaption = csNormalCaption;
1014 pRolloverIcon = pNormalIcon;
1015 }
1016
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001017 font_map.SetAPType("R");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018
1019 csAP = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1020 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1021 crLeftTop, crRightBottom,
1022 nBorderStyle, dsBorder) +
1023 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001024 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025 pRolloverIcon, iconFit, csRolloverCaption, crText, fFontSize,
1026 nLayout);
1027
1028 WriteAppearance("R", GetRotatedRect(), GetMatrix(), csAP);
1029 if (pRolloverIcon)
1030 AddImageToAppearance("R", pRolloverIcon);
1031
1032 if (csDownCaption.IsEmpty() && !pDownIcon) {
1033 csDownCaption = csNormalCaption;
1034 pDownIcon = pNormalIcon;
1035 }
1036
1037 switch (nBorderStyle) {
dsinclair92cb5e52016-05-16 11:38:28 -07001038 case BorderStyle::BEVELED: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039 CPWL_Color crTemp = crLeftTop;
1040 crLeftTop = crRightBottom;
1041 crRightBottom = crTemp;
dsinclair92cb5e52016-05-16 11:38:28 -07001042 break;
1043 }
1044 case BorderStyle::INSET: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1046 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001047 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001048 }
1049 default:
1050 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001051 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001052
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001053 font_map.SetAPType("D");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001054
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055 csAP = CPWL_Utils::GetRectFillAppStream(
1056 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1057 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1058 crLeftTop, crRightBottom,
1059 nBorderStyle, dsBorder) +
1060 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001061 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001062 pDownIcon, iconFit, csDownCaption, crText, fFontSize, nLayout);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001063
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001064 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001065 if (pDownIcon)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 AddImageToAppearance("D", pDownIcon);
1067 } else {
1068 RemoveAppearance("D");
1069 RemoveAppearance("R");
1070 }
1071}
1072
1073void CPDFSDK_Widget::ResetAppearance_CheckBox() {
1074 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 CPWL_Color crBackground, crBorder, crText;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076 int iColorType;
1077 FX_FLOAT fc[4];
1078
1079 pControl->GetOriginalBackgroundColor(iColorType, fc);
1080 if (iColorType > 0)
1081 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1082
1083 pControl->GetOriginalBorderColor(iColorType, fc);
1084 if (iColorType > 0)
1085 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1086
1087 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088 CPWL_Dash dsBorder(3, 0, 0);
1089 CPWL_Color crLeftTop, crRightBottom;
1090
dsinclair92cb5e52016-05-16 11:38:28 -07001091 BorderStyle nBorderStyle = GetBorderStyle();
1092 switch (nBorderStyle) {
1093 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001094 dsBorder = CPWL_Dash(3, 3, 0);
1095 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001096 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001097 fBorderWidth *= 2;
1098 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1099 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1100 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001101 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001102 fBorderWidth *= 2;
1103 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1104 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1105 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107 break;
1108 }
1109
Tom Sepez281a9ea2016-02-26 14:24:28 -08001110 CFX_FloatRect rcWindow = GetRotatedRect();
1111 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001112
1113 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
1114 if (da.HasColor()) {
1115 da.GetColor(iColorType, fc);
1116 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1117 }
1118
1119 int32_t nStyle = 0;
1120
1121 CFX_WideString csWCaption = pControl->GetNormalCaption();
1122 if (csWCaption.GetLength() > 0) {
1123 switch (csWCaption[0]) {
1124 case L'l':
1125 nStyle = PCS_CIRCLE;
1126 break;
1127 case L'8':
1128 nStyle = PCS_CROSS;
1129 break;
1130 case L'u':
1131 nStyle = PCS_DIAMOND;
1132 break;
1133 case L'n':
1134 nStyle = PCS_SQUARE;
1135 break;
1136 case L'H':
1137 nStyle = PCS_STAR;
1138 break;
1139 default: // L'4'
1140 nStyle = PCS_CHECK;
1141 break;
1142 }
1143 } else {
1144 nStyle = PCS_CHECK;
1145 }
1146
1147 CFX_ByteString csAP_N_ON =
1148 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1149 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1150 crLeftTop, crRightBottom, nBorderStyle,
1151 dsBorder);
1152
1153 CFX_ByteString csAP_N_OFF = csAP_N_ON;
1154
1155 switch (nBorderStyle) {
dsinclair92cb5e52016-05-16 11:38:28 -07001156 case BorderStyle::BEVELED: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001157 CPWL_Color crTemp = crLeftTop;
1158 crLeftTop = crRightBottom;
1159 crRightBottom = crTemp;
dsinclair92cb5e52016-05-16 11:38:28 -07001160 break;
1161 }
1162 case BorderStyle::INSET: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1164 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1165 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001166 }
1167 default:
1168 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169 }
1170
1171 CFX_ByteString csAP_D_ON =
1172 CPWL_Utils::GetRectFillAppStream(
1173 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1174 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1175 crLeftTop, crRightBottom, nBorderStyle,
1176 dsBorder);
1177
1178 CFX_ByteString csAP_D_OFF = csAP_D_ON;
1179
1180 csAP_N_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
1181 csAP_D_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
1182
1183 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
1184 pControl->GetCheckedAPState());
1185 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1186
1187 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
1188 pControl->GetCheckedAPState());
1189 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1190
1191 CFX_ByteString csAS = GetAppState();
1192 if (csAS.IsEmpty())
1193 SetAppState("Off");
1194}
1195
1196void CPDFSDK_Widget::ResetAppearance_RadioButton() {
1197 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001198 CPWL_Color crBackground, crBorder, crText;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001199 int iColorType;
1200 FX_FLOAT fc[4];
1201
1202 pControl->GetOriginalBackgroundColor(iColorType, fc);
1203 if (iColorType > 0)
1204 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1205
1206 pControl->GetOriginalBorderColor(iColorType, fc);
1207 if (iColorType > 0)
1208 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1209
1210 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001211 CPWL_Dash dsBorder(3, 0, 0);
1212 CPWL_Color crLeftTop, crRightBottom;
1213
dsinclair92cb5e52016-05-16 11:38:28 -07001214 BorderStyle nBorderStyle = GetBorderStyle();
1215 switch (nBorderStyle) {
1216 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001217 dsBorder = CPWL_Dash(3, 3, 0);
1218 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001219 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001220 fBorderWidth *= 2;
1221 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1222 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1223 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001224 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001225 fBorderWidth *= 2;
1226 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1227 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1228 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001229 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001230 break;
1231 }
1232
Tom Sepez281a9ea2016-02-26 14:24:28 -08001233 CFX_FloatRect rcWindow = GetRotatedRect();
1234 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001235
1236 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
1237 if (da.HasColor()) {
1238 da.GetColor(iColorType, fc);
1239 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1240 }
1241
1242 int32_t nStyle = 0;
1243
1244 CFX_WideString csWCaption = pControl->GetNormalCaption();
1245 if (csWCaption.GetLength() > 0) {
1246 switch (csWCaption[0]) {
1247 default: // L'l':
1248 nStyle = PCS_CIRCLE;
1249 break;
1250 case L'8':
1251 nStyle = PCS_CROSS;
1252 break;
1253 case L'u':
1254 nStyle = PCS_DIAMOND;
1255 break;
1256 case L'n':
1257 nStyle = PCS_SQUARE;
1258 break;
1259 case L'H':
1260 nStyle = PCS_STAR;
1261 break;
1262 case L'4':
1263 nStyle = PCS_CHECK;
1264 break;
1265 }
1266 } else {
1267 nStyle = PCS_CIRCLE;
1268 }
1269
1270 CFX_ByteString csAP_N_ON;
1271
Tom Sepez281a9ea2016-02-26 14:24:28 -08001272 CFX_FloatRect rcCenter =
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001273 CPWL_Utils::DeflateRect(CPWL_Utils::GetCenterSquare(rcWindow), 1.0f);
1274
1275 if (nStyle == PCS_CIRCLE) {
dsinclair92cb5e52016-05-16 11:38:28 -07001276 if (nBorderStyle == BorderStyle::BEVELED) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1278 crRightBottom = CPWL_Utils::SubstractColor(crBackground, 0.25f);
dsinclair92cb5e52016-05-16 11:38:28 -07001279 } else if (nBorderStyle == BorderStyle::INSET) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001280 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5f);
1281 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75f);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001282 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001283
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001284 csAP_N_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBackground) +
1285 CPWL_Utils::GetCircleBorderAppStream(
1286 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
1287 nBorderStyle, dsBorder);
1288 } else {
1289 csAP_N_ON = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1290 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1291 crLeftTop, crRightBottom,
1292 nBorderStyle, dsBorder);
1293 }
1294
1295 CFX_ByteString csAP_N_OFF = csAP_N_ON;
1296
1297 switch (nBorderStyle) {
dsinclair92cb5e52016-05-16 11:38:28 -07001298 case BorderStyle::BEVELED: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299 CPWL_Color crTemp = crLeftTop;
1300 crLeftTop = crRightBottom;
1301 crRightBottom = crTemp;
dsinclair92cb5e52016-05-16 11:38:28 -07001302 break;
1303 }
1304 case BorderStyle::INSET: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1306 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1307 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001308 }
1309 default:
1310 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001311 }
1312
1313 CFX_ByteString csAP_D_ON;
1314
1315 if (nStyle == PCS_CIRCLE) {
1316 CPWL_Color crBK = CPWL_Utils::SubstractColor(crBackground, 0.25f);
dsinclair92cb5e52016-05-16 11:38:28 -07001317 if (nBorderStyle == BorderStyle::BEVELED) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001318 crLeftTop = CPWL_Utils::SubstractColor(crBackground, 0.25f);
1319 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1320 crBK = crBackground;
dsinclair92cb5e52016-05-16 11:38:28 -07001321 } else if (nBorderStyle == BorderStyle::INSET) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001322 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1323 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1324 }
1325
1326 csAP_D_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBK) +
1327 CPWL_Utils::GetCircleBorderAppStream(
1328 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
1329 nBorderStyle, dsBorder);
1330 } else {
1331 csAP_D_ON = CPWL_Utils::GetRectFillAppStream(
1332 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1333 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1334 crLeftTop, crRightBottom,
1335 nBorderStyle, dsBorder);
1336 }
1337
1338 CFX_ByteString csAP_D_OFF = csAP_D_ON;
1339
1340 csAP_N_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
1341 csAP_D_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
1342
1343 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
1344 pControl->GetCheckedAPState());
1345 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1346
1347 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
1348 pControl->GetCheckedAPState());
1349 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1350
1351 CFX_ByteString csAS = GetAppState();
1352 if (csAS.IsEmpty())
1353 SetAppState("Off");
1354}
1355
1356void CPDFSDK_Widget::ResetAppearance_ComboBox(const FX_WCHAR* sValue) {
1357 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358 CPDF_FormField* pField = pControl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001359 CFX_ByteTextBuf sBody, sLines;
1360
Tom Sepez281a9ea2016-02-26 14:24:28 -08001361 CFX_FloatRect rcClient = GetClientRect();
1362 CFX_FloatRect rcButton = rcClient;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001363 rcButton.left = rcButton.right - 13;
1364 rcButton.Normalize();
1365
dsinclaire35af1e2016-07-13 11:26:20 -07001366 std::unique_ptr<CFX_Edit> pEdit(new CFX_Edit);
thestig732f6a02016-05-12 10:41:56 -07001367 pEdit->EnableRefresh(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001368
thestig732f6a02016-05-12 10:41:56 -07001369 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1370 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
1371 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1372 pEdit->SetFontMap(&font_map);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001373
thestig732f6a02016-05-12 10:41:56 -07001374 CFX_FloatRect rcEdit = rcClient;
1375 rcEdit.right = rcButton.left;
1376 rcEdit.Normalize();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377
thestig732f6a02016-05-12 10:41:56 -07001378 pEdit->SetPlateRect(rcEdit);
dsinclairefd5a992016-07-18 10:04:07 -07001379 pEdit->SetAlignmentV(1, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380
thestig732f6a02016-05-12 10:41:56 -07001381 FX_FLOAT fFontSize = GetFontSize();
1382 if (IsFloatZero(fFontSize))
dsinclairefd5a992016-07-18 10:04:07 -07001383 pEdit->SetAutoFontSize(TRUE, TRUE);
thestig732f6a02016-05-12 10:41:56 -07001384 else
1385 pEdit->SetFontSize(fFontSize);
1386
1387 pEdit->Initialize();
1388
1389 if (sValue) {
1390 pEdit->SetText(sValue);
1391 } else {
1392 int32_t nCurSel = pField->GetSelectedIndex(0);
1393
1394 if (nCurSel < 0)
1395 pEdit->SetText(pField->GetValue().c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001396 else
thestig732f6a02016-05-12 10:41:56 -07001397 pEdit->SetText(pField->GetOptionLabel(nCurSel).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001398 }
1399
thestig732f6a02016-05-12 10:41:56 -07001400 CFX_FloatRect rcContent = pEdit->GetContentRect();
1401
1402 CFX_ByteString sEdit =
dsinclaire35af1e2016-07-13 11:26:20 -07001403 CPWL_Utils::GetEditAppStream(pEdit.get(), CFX_FloatPoint(0.0f, 0.0f));
thestig732f6a02016-05-12 10:41:56 -07001404 if (sEdit.GetLength() > 0) {
1405 sBody << "/Tx BMC\n"
1406 << "q\n";
1407 if (rcContent.Width() > rcEdit.Width() ||
1408 rcContent.Height() > rcEdit.Height()) {
1409 sBody << rcEdit.left << " " << rcEdit.bottom << " " << rcEdit.Width()
1410 << " " << rcEdit.Height() << " re\nW\nn\n";
1411 }
1412
1413 CPWL_Color crText = GetTextPWLColor();
1414 sBody << "BT\n"
1415 << CPWL_Utils::GetColorAppStream(crText) << sEdit << "ET\n"
1416 << "Q\nEMC\n";
1417 }
1418
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001419 sBody << CPWL_Utils::GetDropButtonAppStream(rcButton);
1420
1421 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001422 sLines.AsStringC() + sBody.AsStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001423
1424 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1425}
1426
1427void CPDFSDK_Widget::ResetAppearance_ListBox() {
1428 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001429 CPDF_FormField* pField = pControl->GetField();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001430 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001431 CFX_ByteTextBuf sBody, sLines;
1432
dsinclaire35af1e2016-07-13 11:26:20 -07001433 std::unique_ptr<CFX_Edit> pEdit(new CFX_Edit);
thestig732f6a02016-05-12 10:41:56 -07001434 pEdit->EnableRefresh(FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001435
thestig732f6a02016-05-12 10:41:56 -07001436 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1437 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001438
thestig732f6a02016-05-12 10:41:56 -07001439 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1440 pEdit->SetFontMap(&font_map);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001441
thestig732f6a02016-05-12 10:41:56 -07001442 pEdit->SetPlateRect(CFX_FloatRect(rcClient.left, 0.0f, rcClient.right, 0.0f));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001443
thestig732f6a02016-05-12 10:41:56 -07001444 FX_FLOAT fFontSize = GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001445
thestig594b20b2016-05-12 21:56:43 -07001446 pEdit->SetFontSize(IsFloatZero(fFontSize) ? 12.0f : fFontSize);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001447
thestig732f6a02016-05-12 10:41:56 -07001448 pEdit->Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001449
thestig732f6a02016-05-12 10:41:56 -07001450 CFX_ByteTextBuf sList;
1451 FX_FLOAT fy = rcClient.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001452
thestig732f6a02016-05-12 10:41:56 -07001453 int32_t nTop = pField->GetTopVisibleIndex();
1454 int32_t nCount = pField->CountOptions();
1455 int32_t nSelCount = pField->CountSelectedItems();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001456
thestig594b20b2016-05-12 21:56:43 -07001457 for (int32_t i = nTop; i < nCount; ++i) {
1458 bool bSelected = false;
1459 for (int32_t j = 0; j < nSelCount; ++j) {
thestig732f6a02016-05-12 10:41:56 -07001460 if (pField->GetSelectedIndex(j) == i) {
thestig594b20b2016-05-12 21:56:43 -07001461 bSelected = true;
thestig732f6a02016-05-12 10:41:56 -07001462 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001463 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001464 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001465
thestig732f6a02016-05-12 10:41:56 -07001466 pEdit->SetText(pField->GetOptionLabel(i).c_str());
1467
1468 CFX_FloatRect rcContent = pEdit->GetContentRect();
1469 FX_FLOAT fItemHeight = rcContent.Height();
1470
1471 if (bSelected) {
1472 CFX_FloatRect rcItem =
1473 CFX_FloatRect(rcClient.left, fy - fItemHeight, rcClient.right, fy);
1474 sList << "q\n"
1475 << CPWL_Utils::GetColorAppStream(
1476 CPWL_Color(COLORTYPE_RGB, 0, 51.0f / 255.0f,
1477 113.0f / 255.0f),
1478 TRUE)
1479 << rcItem.left << " " << rcItem.bottom << " " << rcItem.Width()
1480 << " " << rcItem.Height() << " re f\n"
1481 << "Q\n";
1482
1483 sList << "BT\n"
1484 << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_GRAY, 1),
1485 TRUE)
dsinclaire35af1e2016-07-13 11:26:20 -07001486 << CPWL_Utils::GetEditAppStream(pEdit.get(),
1487 CFX_FloatPoint(0.0f, fy))
thestig732f6a02016-05-12 10:41:56 -07001488 << "ET\n";
1489 } else {
1490 CPWL_Color crText = GetTextPWLColor();
1491 sList << "BT\n"
1492 << CPWL_Utils::GetColorAppStream(crText, TRUE)
dsinclaire35af1e2016-07-13 11:26:20 -07001493 << CPWL_Utils::GetEditAppStream(pEdit.get(),
1494 CFX_FloatPoint(0.0f, fy))
thestig732f6a02016-05-12 10:41:56 -07001495 << "ET\n";
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001496 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001497
thestig732f6a02016-05-12 10:41:56 -07001498 fy -= fItemHeight;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001500
thestig732f6a02016-05-12 10:41:56 -07001501 if (sList.GetSize() > 0) {
1502 sBody << "/Tx BMC\n"
1503 << "q\n"
1504 << rcClient.left << " " << rcClient.bottom << " " << rcClient.Width()
1505 << " " << rcClient.Height() << " re\nW\nn\n";
1506 sBody << sList << "Q\nEMC\n";
1507 }
1508
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001510 sLines.AsStringC() + sBody.AsStringC();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001511
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001513}
1514
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001515void CPDFSDK_Widget::ResetAppearance_TextField(const FX_WCHAR* sValue) {
1516 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001517 CPDF_FormField* pField = pControl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001518 CFX_ByteTextBuf sBody, sLines;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001519
dsinclaire35af1e2016-07-13 11:26:20 -07001520 std::unique_ptr<CFX_Edit> pEdit(new CFX_Edit);
thestig732f6a02016-05-12 10:41:56 -07001521 pEdit->EnableRefresh(FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001522
thestig732f6a02016-05-12 10:41:56 -07001523 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1524 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001525
thestig732f6a02016-05-12 10:41:56 -07001526 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1527 pEdit->SetFontMap(&font_map);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001528
thestig732f6a02016-05-12 10:41:56 -07001529 CFX_FloatRect rcClient = GetClientRect();
1530 pEdit->SetPlateRect(rcClient);
dsinclairefd5a992016-07-18 10:04:07 -07001531 pEdit->SetAlignmentH(pControl->GetControlAlignment(), TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001532
thestig732f6a02016-05-12 10:41:56 -07001533 uint32_t dwFieldFlags = pField->GetFieldFlags();
1534 FX_BOOL bMultiLine = (dwFieldFlags >> 12) & 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001535
thestig732f6a02016-05-12 10:41:56 -07001536 if (bMultiLine) {
dsinclairefd5a992016-07-18 10:04:07 -07001537 pEdit->SetMultiLine(TRUE, TRUE);
1538 pEdit->SetAutoReturn(TRUE, TRUE);
thestig732f6a02016-05-12 10:41:56 -07001539 } else {
dsinclairefd5a992016-07-18 10:04:07 -07001540 pEdit->SetAlignmentV(1, TRUE);
thestig732f6a02016-05-12 10:41:56 -07001541 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001542
thestig732f6a02016-05-12 10:41:56 -07001543 uint16_t subWord = 0;
1544 if ((dwFieldFlags >> 13) & 1) {
1545 subWord = '*';
dsinclairefd5a992016-07-18 10:04:07 -07001546 pEdit->SetPasswordChar(subWord, TRUE);
thestig732f6a02016-05-12 10:41:56 -07001547 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001548
thestig732f6a02016-05-12 10:41:56 -07001549 int nMaxLen = pField->GetMaxLen();
1550 FX_BOOL bCharArray = (dwFieldFlags >> 24) & 1;
1551 FX_FLOAT fFontSize = GetFontSize();
Bo Xufdc00a72014-10-28 23:03:33 -07001552
Tom Sepez51da0932015-11-25 16:05:49 -08001553#ifdef PDF_ENABLE_XFA
thestig732f6a02016-05-12 10:41:56 -07001554 CFX_WideString sValueTmp;
1555 if (!sValue && GetMixXFAWidget()) {
1556 sValueTmp = GetValue(TRUE);
1557 sValue = sValueTmp.c_str();
1558 }
Tom Sepez40e9ff32015-11-30 12:39:54 -08001559#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001560
thestig732f6a02016-05-12 10:41:56 -07001561 if (nMaxLen > 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001562 if (bCharArray) {
thestig732f6a02016-05-12 10:41:56 -07001563 pEdit->SetCharArray(nMaxLen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001564
thestig732f6a02016-05-12 10:41:56 -07001565 if (IsFloatZero(fFontSize)) {
1566 fFontSize = CPWL_Edit::GetCharArrayAutoFontSize(font_map.GetPDFFont(0),
1567 rcClient, nMaxLen);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001568 }
thestig732f6a02016-05-12 10:41:56 -07001569 } else {
1570 if (sValue)
1571 nMaxLen = wcslen((const wchar_t*)sValue);
1572 pEdit->SetLimitChar(nMaxLen);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001573 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001574 }
1575
thestig732f6a02016-05-12 10:41:56 -07001576 if (IsFloatZero(fFontSize))
dsinclairefd5a992016-07-18 10:04:07 -07001577 pEdit->SetAutoFontSize(TRUE, TRUE);
thestig732f6a02016-05-12 10:41:56 -07001578 else
1579 pEdit->SetFontSize(fFontSize);
1580
1581 pEdit->Initialize();
1582
1583 if (sValue)
1584 pEdit->SetText(sValue);
1585 else
1586 pEdit->SetText(pField->GetValue().c_str());
1587
1588 CFX_FloatRect rcContent = pEdit->GetContentRect();
1589
1590 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(
dsinclaire35af1e2016-07-13 11:26:20 -07001591 pEdit.get(), CFX_FloatPoint(0.0f, 0.0f), nullptr, !bCharArray, subWord);
thestig732f6a02016-05-12 10:41:56 -07001592
1593 if (sEdit.GetLength() > 0) {
1594 sBody << "/Tx BMC\n"
1595 << "q\n";
1596 if (rcContent.Width() > rcClient.Width() ||
1597 rcContent.Height() > rcClient.Height()) {
1598 sBody << rcClient.left << " " << rcClient.bottom << " "
1599 << rcClient.Width() << " " << rcClient.Height() << " re\nW\nn\n";
1600 }
1601 CPWL_Color crText = GetTextPWLColor();
1602 sBody << "BT\n"
1603 << CPWL_Utils::GetColorAppStream(crText) << sEdit << "ET\n"
1604 << "Q\nEMC\n";
1605 }
1606
1607 if (bCharArray) {
1608 switch (GetBorderStyle()) {
dsinclair92cb5e52016-05-16 11:38:28 -07001609 case BorderStyle::SOLID: {
thestig732f6a02016-05-12 10:41:56 -07001610 CFX_ByteString sColor =
1611 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1612 if (sColor.GetLength() > 0) {
1613 sLines << "q\n"
1614 << GetBorderWidth() << " w\n"
1615 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1616 << " 2 J 0 j\n";
1617
thestig594b20b2016-05-12 21:56:43 -07001618 for (int32_t i = 1; i < nMaxLen; ++i) {
thestig732f6a02016-05-12 10:41:56 -07001619 sLines << rcClient.left +
1620 ((rcClient.right - rcClient.left) / nMaxLen) * i
1621 << " " << rcClient.bottom << " m\n"
1622 << rcClient.left +
1623 ((rcClient.right - rcClient.left) / nMaxLen) * i
1624 << " " << rcClient.top << " l S\n";
1625 }
1626
1627 sLines << "Q\n";
1628 }
dsinclair92cb5e52016-05-16 11:38:28 -07001629 break;
1630 }
1631 case BorderStyle::DASH: {
thestig732f6a02016-05-12 10:41:56 -07001632 CFX_ByteString sColor =
1633 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1634 if (sColor.GetLength() > 0) {
1635 CPWL_Dash dsBorder = CPWL_Dash(3, 3, 0);
1636
1637 sLines << "q\n"
1638 << GetBorderWidth() << " w\n"
1639 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1640 << "[" << dsBorder.nDash << " " << dsBorder.nGap << "] "
1641 << dsBorder.nPhase << " d\n";
1642
thestig594b20b2016-05-12 21:56:43 -07001643 for (int32_t i = 1; i < nMaxLen; ++i) {
thestig732f6a02016-05-12 10:41:56 -07001644 sLines << rcClient.left +
1645 ((rcClient.right - rcClient.left) / nMaxLen) * i
1646 << " " << rcClient.bottom << " m\n"
1647 << rcClient.left +
1648 ((rcClient.right - rcClient.left) / nMaxLen) * i
1649 << " " << rcClient.top << " l S\n";
1650 }
1651
1652 sLines << "Q\n";
1653 }
dsinclair92cb5e52016-05-16 11:38:28 -07001654 break;
1655 }
1656 default:
1657 break;
thestig732f6a02016-05-12 10:41:56 -07001658 }
1659 }
1660
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001661 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001662 sLines.AsStringC() + sBody.AsStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001663 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001664}
1665
Tom Sepez281a9ea2016-02-26 14:24:28 -08001666CFX_FloatRect CPDFSDK_Widget::GetClientRect() const {
1667 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001668 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1669 switch (GetBorderStyle()) {
dsinclair92cb5e52016-05-16 11:38:28 -07001670 case BorderStyle::BEVELED:
1671 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001672 fBorderWidth *= 2.0f;
1673 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001674 default:
1675 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001676 }
1677
1678 return CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001679}
1680
Tom Sepez281a9ea2016-02-26 14:24:28 -08001681CFX_FloatRect CPDFSDK_Widget::GetRotatedRect() const {
1682 CFX_FloatRect rectAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001683 FX_FLOAT fWidth = rectAnnot.right - rectAnnot.left;
1684 FX_FLOAT fHeight = rectAnnot.top - rectAnnot.bottom;
1685
1686 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001687 CFX_FloatRect rcPDFWindow;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001688 switch (abs(pControl->GetRotation() % 360)) {
1689 case 0:
1690 case 180:
1691 default:
Tom Sepez281a9ea2016-02-26 14:24:28 -08001692 rcPDFWindow = CFX_FloatRect(0, 0, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001693 break;
1694 case 90:
1695 case 270:
Tom Sepez281a9ea2016-02-26 14:24:28 -08001696 rcPDFWindow = CFX_FloatRect(0, 0, fHeight, fWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001697 break;
1698 }
1699
1700 return rcPDFWindow;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001701}
1702
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001703CFX_ByteString CPDFSDK_Widget::GetBackgroundAppStream() const {
1704 CPWL_Color crBackground = GetFillPWLColor();
1705 if (crBackground.nColorType != COLORTYPE_TRANSPARENT) {
1706 return CPWL_Utils::GetRectFillAppStream(GetRotatedRect(), crBackground);
1707 }
1708 return "";
Bo Xufdc00a72014-10-28 23:03:33 -07001709}
1710
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001711CFX_ByteString CPDFSDK_Widget::GetBorderAppStream() const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001712 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713 CPWL_Color crBorder = GetBorderPWLColor();
1714 CPWL_Color crBackground = GetFillPWLColor();
1715 CPWL_Color crLeftTop, crRightBottom;
1716
1717 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001718 CPWL_Dash dsBorder(3, 0, 0);
1719
dsinclair92cb5e52016-05-16 11:38:28 -07001720 BorderStyle nBorderStyle = GetBorderStyle();
1721 switch (nBorderStyle) {
1722 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001723 dsBorder = CPWL_Dash(3, 3, 0);
1724 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001725 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001726 fBorderWidth *= 2;
1727 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1728 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1729 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001730 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001731 fBorderWidth *= 2;
1732 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1733 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1734 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001735 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001736 break;
1737 }
1738
1739 return CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1740 crLeftTop, crRightBottom, nBorderStyle,
1741 dsBorder);
Bo Xufdc00a72014-10-28 23:03:33 -07001742}
1743
Tom Sepez60d909e2015-12-10 15:34:55 -08001744CFX_Matrix CPDFSDK_Widget::GetMatrix() const {
1745 CFX_Matrix mt;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001746 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001747 CFX_FloatRect rcAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001748 FX_FLOAT fWidth = rcAnnot.right - rcAnnot.left;
1749 FX_FLOAT fHeight = rcAnnot.top - rcAnnot.bottom;
1750
1751 switch (abs(pControl->GetRotation() % 360)) {
1752 case 0:
1753 default:
Tom Sepez60d909e2015-12-10 15:34:55 -08001754 mt = CFX_Matrix(1, 0, 0, 1, 0, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001755 break;
1756 case 90:
Tom Sepez60d909e2015-12-10 15:34:55 -08001757 mt = CFX_Matrix(0, 1, -1, 0, fWidth, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001758 break;
1759 case 180:
Tom Sepez60d909e2015-12-10 15:34:55 -08001760 mt = CFX_Matrix(-1, 0, 0, -1, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001761 break;
1762 case 270:
Tom Sepez60d909e2015-12-10 15:34:55 -08001763 mt = CFX_Matrix(0, -1, 1, 0, 0, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001764 break;
1765 }
1766
1767 return mt;
Bo Xufdc00a72014-10-28 23:03:33 -07001768}
1769
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001770CPWL_Color CPDFSDK_Widget::GetTextPWLColor() const {
1771 CPWL_Color crText = CPWL_Color(COLORTYPE_GRAY, 0);
1772
1773 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001774 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
1775 if (da.HasColor()) {
1776 int32_t iColorType;
1777 FX_FLOAT fc[4];
1778 da.GetColor(iColorType, fc);
1779 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1780 }
1781
1782 return crText;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001783}
1784
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001785CPWL_Color CPDFSDK_Widget::GetBorderPWLColor() const {
1786 CPWL_Color crBorder;
1787
1788 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001789 int32_t iColorType;
1790 FX_FLOAT fc[4];
1791 pFormCtrl->GetOriginalBorderColor(iColorType, fc);
1792 if (iColorType > 0)
1793 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1794
1795 return crBorder;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001796}
1797
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001798CPWL_Color CPDFSDK_Widget::GetFillPWLColor() const {
1799 CPWL_Color crFill;
1800
1801 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001802 int32_t iColorType;
1803 FX_FLOAT fc[4];
1804 pFormCtrl->GetOriginalBackgroundColor(iColorType, fc);
1805 if (iColorType > 0)
1806 crFill = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1807
1808 return crFill;
Bo Xufdc00a72014-10-28 23:03:33 -07001809}
1810
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001811void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType,
1812 CPDF_Stream* pImage) {
Tom Sepez33420902015-10-13 15:00:10 -07001813 CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
Lei Zhang96660d62015-12-14 18:27:25 -08001814 ASSERT(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001815
Wei Li9b761132016-01-29 15:44:20 -08001816 CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP");
tsepez7b1ccf92016-04-14 11:04:57 -07001817 CPDF_Stream* pStream = pAPDict->GetStreamBy(sAPType);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001818 CPDF_Dictionary* pStreamDict = pStream->GetDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001819 CFX_ByteString sImageAlias = "IMG";
1820
1821 if (CPDF_Dictionary* pImageDict = pImage->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -08001822 sImageAlias = pImageDict->GetStringBy("Name");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001823 if (sImageAlias.IsEmpty())
1824 sImageAlias = "IMG";
1825 }
1826
Wei Li9b761132016-01-29 15:44:20 -08001827 CPDF_Dictionary* pStreamResList = pStreamDict->GetDictBy("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001828 if (!pStreamResList) {
Tom Sepezae51c812015-08-05 12:34:06 -07001829 pStreamResList = new CPDF_Dictionary();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001830 pStreamDict->SetAt("Resources", pStreamResList);
1831 }
1832
1833 if (pStreamResList) {
Tom Sepezae51c812015-08-05 12:34:06 -07001834 CPDF_Dictionary* pXObject = new CPDF_Dictionary;
tsepez7b1ccf92016-04-14 11:04:57 -07001835 pXObject->SetAtReference(sImageAlias, pDoc, pImage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001836 pStreamResList->SetAt("XObject", pXObject);
1837 }
Bo Xufdc00a72014-10-28 23:03:33 -07001838}
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001839
1840void CPDFSDK_Widget::RemoveAppearance(const CFX_ByteString& sAPType) {
tsepez7b1ccf92016-04-14 11:04:57 -07001841 if (CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP"))
1842 pAPDict->RemoveAt(sAPType);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001843}
1844
1845FX_BOOL CPDFSDK_Widget::OnAAction(CPDF_AAction::AActionType type,
1846 PDFSDK_FieldAction& data,
1847 CPDFSDK_PageView* pPageView) {
1848 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001849 CPDFDoc_Environment* pEnv = pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001850
Tom Sepez40e9ff32015-11-30 12:39:54 -08001851#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -08001852 CPDFXFA_Document* pDoc = pDocument->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -07001853 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001854 XFA_EVENTTYPE eEventType = GetXFAEventType(type, data.bWillCommit);
1855
1856 if (eEventType != XFA_EVENT_Unknown) {
dsinclairdf4bc592016-03-31 20:34:43 -07001857 if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001858 CXFA_EventParam param;
1859 param.m_eType = eEventType;
1860 param.m_wsChange = data.sChange;
1861 param.m_iCommitKey = data.nCommitKey;
1862 param.m_bShift = data.bShift;
1863 param.m_iSelStart = data.nSelStart;
1864 param.m_iSelEnd = data.nSelEnd;
1865 param.m_wsFullText = data.sValue;
1866 param.m_bKeyDown = data.bKeyDown;
1867 param.m_bModifier = data.bModifier;
1868 param.m_wsNewText = data.sValue;
1869 if (data.nSelEnd > data.nSelStart)
1870 param.m_wsNewText.Delete(data.nSelStart,
1871 data.nSelEnd - data.nSelStart);
1872 for (int i = data.sChange.GetLength() - 1; i >= 0; i--)
1873 param.m_wsNewText.Insert(data.nSelStart, data.sChange[i]);
1874 param.m_wsPrevText = data.sValue;
1875
dsinclair221caf62016-04-04 12:08:40 -07001876 CXFA_WidgetAcc* pAcc = hWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001877 param.m_pTarget = pAcc;
1878 int32_t nRet = pXFAWidgetHandler->ProcessEvent(pAcc, &param);
1879
dsinclairdf4bc592016-03-31 20:34:43 -07001880 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001881 pDocView->UpdateDocView();
1882 }
1883
Wei Lie98ac2e2016-03-18 15:43:04 -07001884 if (nRet == XFA_EVENTERROR_Success)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001885 return TRUE;
1886 }
1887 }
1888 }
Tom Sepez40e9ff32015-11-30 12:39:54 -08001889#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001890
1891 CPDF_Action action = GetAAction(type);
Wei Li0fc6b252016-03-01 16:29:41 -08001892 if (action.GetDict() && action.GetType() != CPDF_Action::Unknown) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001893 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
1894 return pActionHandler->DoAction_Field(action, type, pDocument,
1895 GetFormField(), data);
1896 }
1897 return FALSE;
1898}
1899
1900CPDF_Action CPDFSDK_Widget::GetAAction(CPDF_AAction::AActionType eAAT) {
1901 switch (eAAT) {
1902 case CPDF_AAction::CursorEnter:
1903 case CPDF_AAction::CursorExit:
1904 case CPDF_AAction::ButtonDown:
1905 case CPDF_AAction::ButtonUp:
1906 case CPDF_AAction::GetFocus:
1907 case CPDF_AAction::LoseFocus:
1908 case CPDF_AAction::PageOpen:
1909 case CPDF_AAction::PageClose:
1910 case CPDF_AAction::PageVisible:
1911 case CPDF_AAction::PageInvisible:
1912 return CPDFSDK_BAAnnot::GetAAction(eAAT);
1913
1914 case CPDF_AAction::KeyStroke:
1915 case CPDF_AAction::Format:
1916 case CPDF_AAction::Validate:
1917 case CPDF_AAction::Calculate: {
1918 CPDF_FormField* pField = GetFormField();
Wei Li0fc6b252016-03-01 16:29:41 -08001919 if (pField->GetAdditionalAction().GetDict())
1920 return pField->GetAdditionalAction().GetAction(eAAT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001921 return CPDFSDK_BAAnnot::GetAAction(eAAT);
1922 }
1923 default:
1924 break;
1925 }
1926
1927 return CPDF_Action();
1928}
1929
1930CFX_WideString CPDFSDK_Widget::GetAlternateName() const {
1931 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001932 return pFormField->GetAlternateName();
1933}
1934
1935int32_t CPDFSDK_Widget::GetAppearanceAge() const {
1936 return m_nAppAge;
1937}
1938
1939int32_t CPDFSDK_Widget::GetValueAge() const {
1940 return m_nValueAge;
1941}
1942
1943FX_BOOL CPDFSDK_Widget::HitTest(FX_FLOAT pageX, FX_FLOAT pageY) {
1944 CPDF_Annot* pAnnot = GetPDFAnnot();
1945 CFX_FloatRect annotRect;
1946 pAnnot->GetRect(annotRect);
1947 if (annotRect.Contains(pageX, pageY)) {
1948 if (!IsVisible())
1949 return FALSE;
1950
1951 int nFieldFlags = GetFieldFlags();
1952 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
1953 return FALSE;
1954
1955 return TRUE;
1956 }
1957 return FALSE;
1958}
1959
Tom Sepez51da0932015-11-25 16:05:49 -08001960#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -07001961CPDFSDK_XFAWidget::CPDFSDK_XFAWidget(CXFA_FFWidget* pAnnot,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001962 CPDFSDK_PageView* pPageView,
1963 CPDFSDK_InterForm* pInterForm)
Dan Sinclairf766ad22016-03-14 13:51:24 -04001964 : CPDFSDK_Annot(pPageView),
1965 m_pInterForm(pInterForm),
1966 m_hXFAWidget(pAnnot) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001967
1968FX_BOOL CPDFSDK_XFAWidget::IsXFAField() {
1969 return TRUE;
1970}
1971
weili625ad662016-06-15 11:21:33 -07001972CXFA_FFWidget* CPDFSDK_XFAWidget::GetXFAWidget() const {
1973 return m_hXFAWidget;
1974}
1975
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001976CFX_ByteString CPDFSDK_XFAWidget::GetType() const {
1977 return FSDK_XFAWIDGET_TYPENAME;
1978}
1979
weili625ad662016-06-15 11:21:33 -07001980CFX_ByteString CPDFSDK_XFAWidget::GetSubType() const {
1981 return "";
1982}
1983
Tom Sepez3343d142015-11-02 09:54:54 -08001984CFX_FloatRect CPDFSDK_XFAWidget::GetRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001985 CFX_RectF rcBBox;
dsinclair221caf62016-04-04 12:08:40 -07001986 GetXFAWidget()->GetRect(rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001987 return CFX_FloatRect(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
1988 rcBBox.top + rcBBox.height);
1989}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001990#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001991
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001992CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_Document* pDocument)
1993 : m_pDocument(pDocument),
thestigdb1a24e2016-05-23 16:55:09 -07001994 m_pInterForm(new CPDF_InterForm(m_pDocument->GetPDFDocument())),
Tom Sepez51da0932015-11-25 16:05:49 -08001995#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001996 m_bXfaCalculate(TRUE),
1997 m_bXfaValidationsEnabled(TRUE),
Tom Sepez40e9ff32015-11-30 12:39:54 -08001998#endif // PDF_ENABLE_XFA
1999 m_bCalculate(TRUE),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002000 m_bBusy(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002001 m_pInterForm->SetFormNotify(this);
Tom Sepezae7a9172015-11-23 09:22:46 -08002002 for (int i = 0; i < kNumFieldTypes; ++i)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002003 m_bNeedHightlight[i] = FALSE;
2004 m_iHighlightAlpha = 0;
2005}
2006
2007CPDFSDK_InterForm::~CPDFSDK_InterForm() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002008 m_Map.clear();
Tom Sepez51da0932015-11-25 16:05:49 -08002009#ifdef PDF_ENABLE_XFA
tsepez16a20332016-04-22 14:00:47 -07002010 m_XFAMap.clear();
Tom Sepez40e9ff32015-11-30 12:39:54 -08002011#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002012}
2013
2014FX_BOOL CPDFSDK_InterForm::HighlightWidgets() {
2015 return FALSE;
2016}
2017
2018CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget,
2019 FX_BOOL bNext) const {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08002020 std::unique_ptr<CBA_AnnotIterator> pIterator(
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002021 new CBA_AnnotIterator(pWidget->GetPageView(), "Widget", ""));
2022
2023 if (bNext) {
2024 return (CPDFSDK_Widget*)pIterator->GetNextAnnot(pWidget);
2025 }
2026 return (CPDFSDK_Widget*)pIterator->GetPrevAnnot(pWidget);
2027}
2028
2029CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const {
2030 if (!pControl || !m_pInterForm)
2031 return nullptr;
2032
2033 CPDFSDK_Widget* pWidget = nullptr;
2034 const auto it = m_Map.find(pControl);
2035 if (it != m_Map.end())
2036 pWidget = it->second;
2037
2038 if (pWidget)
2039 return pWidget;
2040
2041 CPDF_Dictionary* pControlDict = pControl->GetWidget();
Tom Sepez50d12ad2015-11-24 09:50:51 -08002042 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002043 CPDFSDK_PageView* pPage = nullptr;
2044
Wei Li9b761132016-01-29 15:44:20 -08002045 if (CPDF_Dictionary* pPageDict = pControlDict->GetDictBy("P")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002046 int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
2047 if (nPageIndex >= 0) {
2048 pPage = m_pDocument->GetPageView(nPageIndex);
2049 }
2050 }
2051
2052 if (!pPage) {
2053 int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict);
2054 if (nPageIndex >= 0) {
2055 pPage = m_pDocument->GetPageView(nPageIndex);
2056 }
2057 }
2058
2059 if (!pPage)
2060 return nullptr;
2061 return (CPDFSDK_Widget*)pPage->GetAnnotByDict(pControlDict);
2062}
2063
Lei Zhangd88a3642015-11-10 09:38:57 -08002064void CPDFSDK_InterForm::GetWidgets(
2065 const CFX_WideString& sFieldName,
2066 std::vector<CPDFSDK_Widget*>* widgets) const {
2067 for (int i = 0, sz = m_pInterForm->CountFields(sFieldName); i < sz; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002068 CPDF_FormField* pFormField = m_pInterForm->GetField(i, sFieldName);
Lei Zhangd88a3642015-11-10 09:38:57 -08002069 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002070 GetWidgets(pFormField, widgets);
2071 }
2072}
2073
Lei Zhangd88a3642015-11-10 09:38:57 -08002074void CPDFSDK_InterForm::GetWidgets(
2075 CPDF_FormField* pField,
2076 std::vector<CPDFSDK_Widget*>* widgets) const {
2077 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002078 CPDF_FormControl* pFormCtrl = pField->GetControl(i);
Lei Zhangd88a3642015-11-10 09:38:57 -08002079 ASSERT(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002080 CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002081 if (pWidget)
Lei Zhangd88a3642015-11-10 09:38:57 -08002082 widgets->push_back(pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002083 }
2084}
2085
2086int CPDFSDK_InterForm::GetPageIndexByAnnotDict(
2087 CPDF_Document* pDocument,
2088 CPDF_Dictionary* pAnnotDict) const {
Lei Zhang96660d62015-12-14 18:27:25 -08002089 ASSERT(pAnnotDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002090
2091 for (int i = 0, sz = pDocument->GetPageCount(); i < sz; i++) {
2092 if (CPDF_Dictionary* pPageDict = pDocument->GetPage(i)) {
Wei Li9b761132016-01-29 15:44:20 -08002093 if (CPDF_Array* pAnnots = pPageDict->GetArrayBy("Annots")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002094 for (int j = 0, jsz = pAnnots->GetCount(); j < jsz; j++) {
tsepezbd567552016-03-29 14:51:50 -07002095 CPDF_Object* pDict = pAnnots->GetDirectObjectAt(j);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002096 if (pAnnotDict == pDict) {
2097 return i;
2098 }
2099 }
2100 }
2101 }
2102 }
2103
2104 return -1;
2105}
2106
2107void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl,
2108 CPDFSDK_Widget* pWidget) {
2109 m_Map[pControl] = pWidget;
2110}
2111
2112void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl) {
2113 m_Map.erase(pControl);
2114}
2115
Tom Sepez40e9ff32015-11-30 12:39:54 -08002116void CPDFSDK_InterForm::EnableCalculate(FX_BOOL bEnabled) {
2117 m_bCalculate = bEnabled;
2118}
2119
2120FX_BOOL CPDFSDK_InterForm::IsCalculateEnabled() const {
2121 return m_bCalculate;
2122}
2123
Tom Sepez51da0932015-11-25 16:05:49 -08002124#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -07002125void CPDFSDK_InterForm::AddXFAMap(CXFA_FFWidget* hWidget,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002126 CPDFSDK_XFAWidget* pWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002127 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002128 m_XFAMap[hWidget] = pWidget;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002129}
2130
dsinclairdf4bc592016-03-31 20:34:43 -07002131void CPDFSDK_InterForm::RemoveXFAMap(CXFA_FFWidget* hWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002132 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002133 m_XFAMap.erase(hWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002134}
2135
dsinclairdf4bc592016-03-31 20:34:43 -07002136CPDFSDK_XFAWidget* CPDFSDK_InterForm::GetXFAWidget(CXFA_FFWidget* hWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002137 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002138 auto it = m_XFAMap.find(hWidget);
2139 return it != m_XFAMap.end() ? it->second : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002140}
2141
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002142void CPDFSDK_InterForm::XfaEnableCalculate(FX_BOOL bEnabled) {
2143 m_bXfaCalculate = bEnabled;
2144}
2145FX_BOOL CPDFSDK_InterForm::IsXfaCalculateEnabled() const {
2146 return m_bXfaCalculate;
2147}
2148
2149FX_BOOL CPDFSDK_InterForm::IsXfaValidationsEnabled() {
2150 return m_bXfaValidationsEnabled;
2151}
2152void CPDFSDK_InterForm::XfaSetValidationsEnabled(FX_BOOL bEnabled) {
2153 m_bXfaValidationsEnabled = bEnabled;
Bo Xufdc00a72014-10-28 23:03:33 -07002154}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002155#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -07002156
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002157void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002158 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2159 ASSERT(pEnv);
2160 if (!pEnv->IsJSInitiated())
2161 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002162
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002163 if (m_bBusy)
2164 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002165
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002166 m_bBusy = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002167
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002168 if (IsCalculateEnabled()) {
Tom Sepezba038bc2015-10-08 12:03:00 -07002169 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002170 pRuntime->SetReaderDocument(m_pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002171
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002172 int nSize = m_pInterForm->CountFieldsInCalculationOrder();
2173 for (int i = 0; i < nSize; i++) {
2174 if (CPDF_FormField* pField =
2175 m_pInterForm->GetFieldInCalculationOrder(i)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002176 int nType = pField->GetFieldType();
2177 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2178 CPDF_AAction aAction = pField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002179 if (aAction.GetDict() &&
2180 aAction.ActionExist(CPDF_AAction::Calculate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002181 CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate);
Wei Li0fc6b252016-03-01 16:29:41 -08002182 if (action.GetDict()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002183 CFX_WideString csJS = action.GetJavaScript();
2184 if (!csJS.IsEmpty()) {
Tom Sepezba038bc2015-10-08 12:03:00 -07002185 IJS_Context* pContext = pRuntime->NewContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002186 CFX_WideString sOldValue = pField->GetValue();
2187 CFX_WideString sValue = sOldValue;
2188 FX_BOOL bRC = TRUE;
2189 pContext->OnField_Calculate(pFormField, pField, sValue, bRC);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002190
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002191 CFX_WideString sInfo;
Tom Sepez33420902015-10-13 15:00:10 -07002192 FX_BOOL bRet = pContext->RunScript(csJS, &sInfo);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002193 pRuntime->ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002194
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002195 if (bRet) {
2196 if (bRC) {
2197 if (sValue.Compare(sOldValue) != 0)
2198 pField->SetValue(sValue, TRUE);
2199 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002200 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002201 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002202 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002203 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002204 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002205 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002206 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002207 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002208
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002209 m_bBusy = FALSE;
2210}
2211
2212CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField,
2213 FX_BOOL& bFormated) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002214 CFX_WideString sValue = pFormField->GetValue();
2215 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2216 ASSERT(pEnv);
2217 if (!pEnv->IsJSInitiated()) {
2218 bFormated = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002219 return sValue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002220 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002221
Tom Sepezba038bc2015-10-08 12:03:00 -07002222 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002223 pRuntime->SetReaderDocument(m_pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002224
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002225 if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX) {
2226 if (pFormField->CountSelectedItems() > 0) {
2227 int index = pFormField->GetSelectedIndex(0);
2228 if (index >= 0)
2229 sValue = pFormField->GetOptionLabel(index);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002230 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002231 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002232
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002233 bFormated = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002234
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002235 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002236 if (aAction.GetDict() && aAction.ActionExist(CPDF_AAction::Format)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002237 CPDF_Action action = aAction.GetAction(CPDF_AAction::Format);
Wei Li0fc6b252016-03-01 16:29:41 -08002238 if (action.GetDict()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002239 CFX_WideString script = action.GetJavaScript();
2240 if (!script.IsEmpty()) {
2241 CFX_WideString Value = sValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002242
Tom Sepezba038bc2015-10-08 12:03:00 -07002243 IJS_Context* pContext = pRuntime->NewContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002244 pContext->OnField_Format(pFormField, Value, TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002245
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002246 CFX_WideString sInfo;
Tom Sepez33420902015-10-13 15:00:10 -07002247 FX_BOOL bRet = pContext->RunScript(script, &sInfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002248 pRuntime->ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002249
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002250 if (bRet) {
2251 sValue = Value;
2252 bFormated = TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002253 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002254 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002255 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002256 }
2257
2258 return sValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002259}
2260
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002261void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField,
2262 const FX_WCHAR* sValue,
2263 FX_BOOL bValueChanged) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002264 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2265 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang96660d62015-12-14 18:27:25 -08002266 ASSERT(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002267 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
2268 pWidget->ResetAppearance(sValue, bValueChanged);
2269 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002270}
2271
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002272void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002273 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2274 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang96660d62015-12-14 18:27:25 -08002275 ASSERT(pFormCtrl);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002276
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002277 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
2278 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2279 CFFL_IFormFiller* pIFormFiller = pEnv->GetIFormFiller();
Tom Sepez540c4362015-11-24 13:33:57 -08002280 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002281 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002282 FX_RECT rcBBox = pIFormFiller->GetViewBBox(pPageView, pWidget);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002283
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002284 pEnv->FFI_Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right,
2285 rcBBox.bottom);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002286 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002287 }
2288}
2289
Tom Sepezd6262c12016-02-03 14:47:06 -08002290FX_BOOL CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField,
2291 const CFX_WideString& csValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002292 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002293 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::KeyStroke))
Tom Sepezd6262c12016-02-03 14:47:06 -08002294 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002295
Tom Sepezd6262c12016-02-03 14:47:06 -08002296 CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke);
Wei Li0fc6b252016-03-01 16:29:41 -08002297 if (!action.GetDict())
Tom Sepezd6262c12016-02-03 14:47:06 -08002298 return TRUE;
2299
2300 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2301 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2302 PDFSDK_FieldAction fa;
2303 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2304 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2305 fa.sValue = csValue;
2306 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::KeyStroke,
2307 m_pDocument, pFormField, fa);
2308 return fa.bRC;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002309}
2310
Tom Sepezd6262c12016-02-03 14:47:06 -08002311FX_BOOL CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField,
2312 const CFX_WideString& csValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002313 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002314 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Validate))
Tom Sepezd6262c12016-02-03 14:47:06 -08002315 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002316
Tom Sepezd6262c12016-02-03 14:47:06 -08002317 CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate);
Wei Li0fc6b252016-03-01 16:29:41 -08002318 if (!action.GetDict())
Tom Sepezd6262c12016-02-03 14:47:06 -08002319 return TRUE;
2320
2321 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2322 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2323 PDFSDK_FieldAction fa;
2324 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2325 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2326 fa.sValue = csValue;
2327 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::Validate,
2328 m_pDocument, pFormField, fa);
2329 return fa.bRC;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002330}
2331
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002332FX_BOOL CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action) {
Wei Li0fc6b252016-03-01 16:29:41 -08002333 ASSERT(action.GetDict());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002334
Wei Li0fc6b252016-03-01 16:29:41 -08002335 CPDF_ActionFields af(&action);
Lei Zhangd88a3642015-11-10 09:38:57 -08002336 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2337 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002338
thestigded36342016-05-23 17:54:02 -07002339 bool bHide = action.GetHideStatus();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002340 FX_BOOL bChanged = FALSE;
2341
Lei Zhangd88a3642015-11-10 09:38:57 -08002342 for (CPDF_FormField* pField : fields) {
2343 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
2344 CPDF_FormControl* pControl = pField->GetControl(i);
2345 ASSERT(pControl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002346
2347 if (CPDFSDK_Widget* pWidget = GetWidget(pControl)) {
tsepezc3255f52016-03-25 14:52:27 -07002348 uint32_t nFlags = pWidget->GetFlags();
Lei Zhangd88a3642015-11-10 09:38:57 -08002349 nFlags &= ~ANNOTFLAG_INVISIBLE;
2350 nFlags &= ~ANNOTFLAG_NOVIEW;
2351 if (bHide)
2352 nFlags |= ANNOTFLAG_HIDDEN;
2353 else
2354 nFlags &= ~ANNOTFLAG_HIDDEN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002355 pWidget->SetFlags(nFlags);
Lei Zhangd88a3642015-11-10 09:38:57 -08002356 pWidget->GetPageView()->UpdateView(pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002357 bChanged = TRUE;
2358 }
2359 }
2360 }
2361
2362 return bChanged;
2363}
2364
2365FX_BOOL CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002366 CFX_WideString sDestination = action.GetFilePath();
2367 if (sDestination.IsEmpty())
2368 return FALSE;
2369
2370 CPDF_Dictionary* pActionDict = action.GetDict();
2371 if (pActionDict->KeyExist("Fields")) {
Wei Li0fc6b252016-03-01 16:29:41 -08002372 CPDF_ActionFields af(&action);
tsepezc3255f52016-03-25 14:52:27 -07002373 uint32_t dwFlags = action.GetFlags();
Lei Zhangd88a3642015-11-10 09:38:57 -08002374 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2375 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
2376 if (!fields.empty()) {
2377 bool bIncludeOrExclude = !(dwFlags & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002378 if (m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002379 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002380
Wei Li97da9762016-03-11 17:00:48 -08002381 return SubmitFields(sDestination, fields, bIncludeOrExclude, false);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002382 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002383 }
Lei Zhangd88a3642015-11-10 09:38:57 -08002384 if (m_pInterForm->CheckRequiredFields(nullptr, true))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002385 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002386
2387 return SubmitForm(sDestination, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002388}
2389
Lei Zhangd88a3642015-11-10 09:38:57 -08002390FX_BOOL CPDFSDK_InterForm::SubmitFields(
2391 const CFX_WideString& csDestination,
2392 const std::vector<CPDF_FormField*>& fields,
Wei Li97da9762016-03-11 17:00:48 -08002393 bool bIncludeOrExclude,
2394 bool bUrlEncoded) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002395 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002396
2397 CFX_ByteTextBuf textBuf;
2398 ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf);
2399
2400 uint8_t* pBuffer = textBuf.GetBuffer();
2401 FX_STRSIZE nBufSize = textBuf.GetLength();
2402
Lei Zhangd88a3642015-11-10 09:38:57 -08002403 if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize))
2404 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002405
2406 pEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002407 return TRUE;
2408}
2409
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002410FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile,
2411 CFX_WideString csTxtFile) {
2412 return TRUE;
2413}
2414
2415FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf,
2416 FX_STRSIZE& nBufSize) {
2417 CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize);
2418 if (pFDF) {
Wei Li9b761132016-01-29 15:44:20 -08002419 CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictBy("FDF");
Lei Zhang997de612015-11-04 18:17:53 -08002420 if (!pMainDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002421 return FALSE;
2422
2423 // Get fields
Wei Li9b761132016-01-29 15:44:20 -08002424 CPDF_Array* pFields = pMainDict->GetArrayBy("Fields");
Lei Zhang997de612015-11-04 18:17:53 -08002425 if (!pFields)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002426 return FALSE;
2427
2428 CFX_ByteTextBuf fdfEncodedData;
tsepezc3255f52016-03-25 14:52:27 -07002429 for (uint32_t i = 0; i < pFields->GetCount(); i++) {
Wei Li9b761132016-01-29 15:44:20 -08002430 CPDF_Dictionary* pField = pFields->GetDictAt(i);
Lei Zhang997de612015-11-04 18:17:53 -08002431 if (!pField)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002432 continue;
2433 CFX_WideString name;
Wei Li9b761132016-01-29 15:44:20 -08002434 name = pField->GetUnicodeTextBy("T");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002435 CFX_ByteString name_b = CFX_ByteString::FromUnicode(name);
Wei Li9b761132016-01-29 15:44:20 -08002436 CFX_ByteString csBValue = pField->GetStringBy("V");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002437 CFX_WideString csWValue = PDF_DecodeText(csBValue);
2438 CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue);
2439
Tom Sepez052a8d92016-02-19 14:41:46 -08002440 fdfEncodedData << name_b.GetBuffer(name_b.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002441 name_b.ReleaseBuffer();
Tom Sepez052a8d92016-02-19 14:41:46 -08002442 fdfEncodedData << "=";
2443 fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002444 csValue_b.ReleaseBuffer();
2445 if (i != pFields->GetCount() - 1)
Tom Sepez052a8d92016-02-19 14:41:46 -08002446 fdfEncodedData << "&";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002447 }
2448
2449 nBufSize = fdfEncodedData.GetLength();
2450 pBuf = FX_Alloc(uint8_t, nBufSize);
2451 FXSYS_memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize);
2452 }
2453 return TRUE;
2454}
2455
Lei Zhangd88a3642015-11-10 09:38:57 -08002456FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(
2457 const std::vector<CPDF_FormField*>& fields,
Wei Li97da9762016-03-11 17:00:48 -08002458 bool bIncludeOrExclude,
Lei Zhangd88a3642015-11-10 09:38:57 -08002459 CFX_ByteTextBuf& textBuf) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08002460 std::unique_ptr<CFDF_Document> pFDF(m_pInterForm->ExportToFDF(
tsepez4c3debb2016-04-08 12:20:38 -07002461 m_pDocument->GetPath().AsStringC(), fields, bIncludeOrExclude));
Lei Zhangd88a3642015-11-10 09:38:57 -08002462 return pFDF ? pFDF->WriteBuf(textBuf) : FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002463}
2464
Tom Sepez51da0932015-11-25 16:05:49 -08002465#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002466void CPDFSDK_InterForm::SynchronizeField(CPDF_FormField* pFormField,
2467 FX_BOOL bSynchronizeElse) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002468 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2469 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002470 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
2471 pWidget->Synchronize(bSynchronizeElse);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002472 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002473 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002474}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002475#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002476
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002477CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName(
2478 const CFX_WideString& sFileExt) {
2479 CFX_WideString sFileName;
2480 return L"";
2481}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002482
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002483FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination,
2484 FX_BOOL bUrlEncoded) {
2485 if (sDestination.IsEmpty())
2486 return FALSE;
2487
Lei Zhang96660d62015-12-14 18:27:25 -08002488 if (!m_pDocument || !m_pInterForm)
2489 return FALSE;
2490
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002491 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002492 CFX_WideString wsPDFFilePath = m_pDocument->GetPath();
tsepez4c3debb2016-04-08 12:20:38 -07002493 CFDF_Document* pFDFDoc = m_pInterForm->ExportToFDF(wsPDFFilePath.AsStringC());
Lei Zhang997de612015-11-04 18:17:53 -08002494 if (!pFDFDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002495 return FALSE;
2496
2497 CFX_ByteTextBuf FdfBuffer;
2498 FX_BOOL bRet = pFDFDoc->WriteBuf(FdfBuffer);
2499 delete pFDFDoc;
2500 if (!bRet)
2501 return FALSE;
2502
2503 uint8_t* pBuffer = FdfBuffer.GetBuffer();
2504 FX_STRSIZE nBufSize = FdfBuffer.GetLength();
2505
2506 if (bUrlEncoded) {
2507 if (!FDFToURLEncodedData(pBuffer, nBufSize))
2508 return FALSE;
2509 }
2510
2511 pEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
2512
thestig1cd352e2016-06-07 17:53:06 -07002513 if (bUrlEncoded)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002514 FX_Free(pBuffer);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002515
2516 return TRUE;
2517}
2518
2519FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) {
tsepezfc58ad12016-04-05 12:22:15 -07002520 CFDF_Document* pFDF =
tsepez4c3debb2016-04-08 12:20:38 -07002521 m_pInterForm->ExportToFDF(m_pDocument->GetPath().AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002522 if (!pFDF)
2523 return FALSE;
2524
2525 FX_BOOL bRet = pFDF->WriteBuf(textBuf);
2526 delete pFDF;
2527
2528 return bRet;
2529}
2530
2531FX_BOOL CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) {
Wei Li0fc6b252016-03-01 16:29:41 -08002532 ASSERT(action.GetDict());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002533
2534 CPDF_Dictionary* pActionDict = action.GetDict();
Lei Zhangd88a3642015-11-10 09:38:57 -08002535 if (!pActionDict->KeyExist("Fields"))
2536 return m_pInterForm->ResetForm(true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002537
Wei Li0fc6b252016-03-01 16:29:41 -08002538 CPDF_ActionFields af(&action);
tsepezc3255f52016-03-25 14:52:27 -07002539 uint32_t dwFlags = action.GetFlags();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002540
Lei Zhangd88a3642015-11-10 09:38:57 -08002541 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2542 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
2543 return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002544}
2545
2546FX_BOOL CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action) {
2547 return FALSE;
2548}
2549
Lei Zhangd88a3642015-11-10 09:38:57 -08002550std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects(
2551 const std::vector<CPDF_Object*>& objects) const {
2552 std::vector<CPDF_FormField*> fields;
2553 for (CPDF_Object* pObject : objects) {
2554 if (pObject && pObject->IsString()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002555 CFX_WideString csName = pObject->GetUnicodeText();
2556 CPDF_FormField* pField = m_pInterForm->GetField(0, csName);
Dan Sinclaird43ebdd2015-10-27 15:37:54 -04002557 if (pField)
Lei Zhangd88a3642015-11-10 09:38:57 -08002558 fields.push_back(pField);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002559 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002560 }
Lei Zhangd88a3642015-11-10 09:38:57 -08002561 return fields;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002562}
2563
Tom Sepezd6262c12016-02-03 14:47:06 -08002564int CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField,
2565 const CFX_WideString& csValue) {
2566 int nType = pField->GetFieldType();
2567 if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002568 return 0;
2569
Tom Sepezd6262c12016-02-03 14:47:06 -08002570 if (!OnKeyStrokeCommit(pField, csValue))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002571 return -1;
2572
Tom Sepezd6262c12016-02-03 14:47:06 -08002573 if (!OnValidate(pField, csValue))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002574 return -1;
2575
2576 return 1;
2577}
2578
Tom Sepezd6262c12016-02-03 14:47:06 -08002579void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) {
2580#ifdef PDF_ENABLE_XFA
2581 SynchronizeField(pField, FALSE);
2582#endif // PDF_ENABLE_XFA
2583 int nType = pField->GetFieldType();
2584 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2585 OnCalculate(pField);
2586 FX_BOOL bFormated = FALSE;
2587 CFX_WideString sValue = OnFormat(pField, bFormated);
2588 ResetFieldAppearance(pField, bFormated ? sValue.c_str() : nullptr, TRUE);
2589 UpdateField(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002590 }
Tom Sepezd6262c12016-02-03 14:47:06 -08002591}
2592
2593int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField,
2594 const CFX_WideString& csValue) {
2595 if (pField->GetFieldType() != FIELDTYPE_LISTBOX)
2596 return 0;
2597
2598 if (!OnKeyStrokeCommit(pField, csValue))
2599 return -1;
2600
2601 if (!OnValidate(pField, csValue))
2602 return -1;
2603
2604 return 1;
2605}
2606
2607void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) {
2608 if (pField->GetFieldType() == FIELDTYPE_LISTBOX) {
2609 OnCalculate(pField);
thestig1cd352e2016-06-07 17:53:06 -07002610 ResetFieldAppearance(pField, nullptr, TRUE);
Tom Sepezd6262c12016-02-03 14:47:06 -08002611 UpdateField(pField);
2612 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002613}
2614
Tom Sepezed5d7aa2016-02-02 16:02:03 -08002615void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) {
2616 int nType = pField->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002617 if (nType == FIELDTYPE_CHECKBOX || nType == FIELDTYPE_RADIOBUTTON) {
Tom Sepezed5d7aa2016-02-02 16:02:03 -08002618 OnCalculate(pField);
2619 UpdateField(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002620 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002621}
2622
Tom Sepezd6262c12016-02-03 14:47:06 -08002623int CPDFSDK_InterForm::BeforeFormReset(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002624 return 0;
2625}
2626
Tom Sepezd6262c12016-02-03 14:47:06 -08002627void CPDFSDK_InterForm::AfterFormReset(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002628 OnCalculate(nullptr);
Tom Sepezd6262c12016-02-03 14:47:06 -08002629}
2630
2631int CPDFSDK_InterForm::BeforeFormImportData(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002632 return 0;
2633}
2634
Tom Sepezd6262c12016-02-03 14:47:06 -08002635void CPDFSDK_InterForm::AfterFormImportData(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002636 OnCalculate(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002637}
2638
2639FX_BOOL CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002640 if (nFieldType < 1 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002641 return FALSE;
2642 return m_bNeedHightlight[nFieldType - 1];
2643}
2644
2645void CPDFSDK_InterForm::RemoveAllHighLight() {
Tom Sepezae7a9172015-11-23 09:22:46 -08002646 for (int i = 0; i < kNumFieldTypes; ++i)
2647 m_bNeedHightlight[i] = FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002648}
Tom Sepezae7a9172015-11-23 09:22:46 -08002649
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002650void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002651 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002652 return;
2653 switch (nFieldType) {
2654 case 0: {
Tom Sepezae7a9172015-11-23 09:22:46 -08002655 for (int i = 0; i < kNumFieldTypes; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002656 m_aHighlightColor[i] = clr;
2657 m_bNeedHightlight[i] = TRUE;
2658 }
2659 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002660 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002661 default: {
2662 m_aHighlightColor[nFieldType - 1] = clr;
2663 m_bNeedHightlight[nFieldType - 1] = TRUE;
2664 break;
2665 }
2666 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002667}
2668
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002669FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002670 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002671 return FXSYS_RGB(255, 255, 255);
2672 if (nFieldType == 0)
2673 return m_aHighlightColor[0];
2674 return m_aHighlightColor[nFieldType - 1];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002675}
2676
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002677CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView,
2678 const CFX_ByteString& sType,
2679 const CFX_ByteString& sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002680 : m_eTabOrder(STRUCTURE),
2681 m_pPageView(pPageView),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002682 m_sType(sType),
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002683 m_sSubType(sSubType) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002684 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage();
Wei Li9b761132016-01-29 15:44:20 -08002685 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringBy("Tabs");
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002686 if (sTabs == "R")
2687 m_eTabOrder = ROW;
2688 else if (sTabs == "C")
2689 m_eTabOrder = COLUMN;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002690
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002691 GenerateResults();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002692}
2693
Dan Sinclairf766ad22016-03-14 13:51:24 -04002694CBA_AnnotIterator::~CBA_AnnotIterator() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002695
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002696CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002697 return m_Annots.empty() ? nullptr : m_Annots.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002698}
2699
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002700CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002701 return m_Annots.empty() ? nullptr : m_Annots.back();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002702}
2703
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002704CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002705 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2706 if (iter == m_Annots.end())
2707 return nullptr;
2708 ++iter;
2709 if (iter == m_Annots.end())
2710 iter = m_Annots.begin();
2711 return *iter;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002712}
2713
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002714CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002715 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2716 if (iter == m_Annots.end())
2717 return nullptr;
2718 if (iter == m_Annots.begin())
2719 iter = m_Annots.end();
2720 return *(--iter);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002721}
2722
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002723// static
2724bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1,
2725 const CPDFSDK_Annot* p2) {
2726 return GetAnnotRect(p1).left < GetAnnotRect(p2).left;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002727}
2728
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002729// static
2730bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1,
2731 const CPDFSDK_Annot* p2) {
2732 return GetAnnotRect(p1).top > GetAnnotRect(p2).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002733}
2734
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002735void CBA_AnnotIterator::GenerateResults() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002736 switch (m_eTabOrder) {
2737 case STRUCTURE: {
Lei Zhangbf60b292015-10-26 12:14:35 -07002738 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002739 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002740 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002741 m_Annots.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002742 }
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002743 } break;
2744 case ROW: {
2745 std::vector<CPDFSDK_Annot*> sa;
Lei Zhang50218532015-10-30 14:03:43 -07002746 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2747 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2748 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002749 sa.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002750 }
2751
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002752 std::sort(sa.begin(), sa.end(), CompareByLeftAscending);
2753 while (!sa.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002754 int nLeftTopIndex = -1;
Lei Zhang50218532015-10-30 14:03:43 -07002755 FX_FLOAT fTop = 0.0f;
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002756 for (int i = sa.size() - 1; i >= 0; i--) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002757 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002758 if (rcAnnot.top > fTop) {
2759 nLeftTopIndex = i;
2760 fTop = rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002761 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002762 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002763 if (nLeftTopIndex >= 0) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002764 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
Tom Sepez281a9ea2016-02-26 14:24:28 -08002765 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002766 m_Annots.push_back(pLeftTopAnnot);
2767 sa.erase(sa.begin() + nLeftTopIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002768
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002769 std::vector<int> aSelect;
Wei Li89409932016-03-28 10:33:33 -07002770 for (size_t i = 0; i < sa.size(); ++i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002771 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002772 FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f;
2773 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002774 aSelect.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002775 }
Wei Li89409932016-03-28 10:33:33 -07002776 for (size_t i = 0; i < aSelect.size(); ++i)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002777 m_Annots.push_back(sa[aSelect[i]]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002778
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002779 for (int i = aSelect.size() - 1; i >= 0; --i)
2780 sa.erase(sa.begin() + aSelect[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002781 }
2782 }
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002783 } break;
2784 case COLUMN: {
2785 std::vector<CPDFSDK_Annot*> sa;
Lei Zhang50218532015-10-30 14:03:43 -07002786 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2787 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2788 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002789 sa.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002790 }
2791
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002792 std::sort(sa.begin(), sa.end(), CompareByTopDescending);
2793 while (!sa.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002794 int nLeftTopIndex = -1;
Lei Zhang50218532015-10-30 14:03:43 -07002795 FX_FLOAT fLeft = -1.0f;
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002796 for (int i = sa.size() - 1; i >= 0; --i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002797 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002798 if (fLeft < 0) {
2799 nLeftTopIndex = 0;
2800 fLeft = rcAnnot.left;
2801 } else if (rcAnnot.left < fLeft) {
2802 nLeftTopIndex = i;
2803 fLeft = rcAnnot.left;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002804 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002805 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002806
2807 if (nLeftTopIndex >= 0) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002808 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
Tom Sepez281a9ea2016-02-26 14:24:28 -08002809 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002810 m_Annots.push_back(pLeftTopAnnot);
2811 sa.erase(sa.begin() + nLeftTopIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002812
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002813 std::vector<int> aSelect;
Wei Li89409932016-03-28 10:33:33 -07002814 for (size_t i = 0; i < sa.size(); ++i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002815 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002816 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f;
2817 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002818 aSelect.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002819 }
Wei Li89409932016-03-28 10:33:33 -07002820 for (size_t i = 0; i < aSelect.size(); ++i)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002821 m_Annots.push_back(sa[aSelect[i]]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002822
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002823 for (int i = aSelect.size() - 1; i >= 0; --i)
2824 sa.erase(sa.begin() + aSelect[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002825 }
2826 }
Lei Zhang50218532015-10-30 14:03:43 -07002827 break;
2828 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002829 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002830}
2831
Tom Sepez281a9ea2016-02-26 14:24:28 -08002832CFX_FloatRect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) {
2833 CFX_FloatRect rcAnnot;
Lei Zhang50218532015-10-30 14:03:43 -07002834 pAnnot->GetPDFAnnot()->GetRect(rcAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002835 return rcAnnot;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002836}