blob: 0f1a0ad0d182956c16a4aa3e461e9a0eeef9a937 [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"
Lei Zhangbde53d22015-11-12 22:21:30 -080021#include "fpdfsdk/include/fsdk_actionhandler.h"
22#include "fpdfsdk/include/fsdk_baseannot.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080023#include "fpdfsdk/include/fsdk_define.h"
24#include "fpdfsdk/include/fsdk_mgr.h"
dsinclair64376be2016-03-31 20:03:24 -070025#include "fpdfsdk/javascript/ijs_context.h"
26#include "fpdfsdk/javascript/ijs_runtime.h"
dan sinclair89e904b2016-03-23 19:29:15 -040027#include "fpdfsdk/pdfwindow/PWL_Utils.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028
Tom Sepez40e9ff32015-11-30 12:39:54 -080029#ifdef PDF_ENABLE_XFA
dsinclair89bdd082016-04-06 10:47:54 -070030#include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h"
31#include "fpdfsdk/fpdfxfa/include/fpdfxfa_util.h"
dsinclair7222ea62016-04-06 14:33:07 -070032#include "xfa/fxfa/include/xfa_ffwidget.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080033#endif // PDF_ENABLE_XFA
34
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035CPDFSDK_Widget::CPDFSDK_Widget(CPDF_Annot* pAnnot,
36 CPDFSDK_PageView* pPageView,
37 CPDFSDK_InterForm* pInterForm)
38 : CPDFSDK_BAAnnot(pAnnot, pPageView),
39 m_pInterForm(pInterForm),
40 m_nAppAge(0),
Tom Sepez40e9ff32015-11-30 12:39:54 -080041 m_nValueAge(0)
42#ifdef PDF_ENABLE_XFA
43 ,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 m_hMixXFAWidget(NULL),
Tom Sepez40e9ff32015-11-30 12:39:54 -080045 m_pWidgetHandler(NULL)
46#endif // PDF_ENABLE_XFA
47{
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048}
49
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050CPDFSDK_Widget::~CPDFSDK_Widget() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
Tom Sepez51da0932015-11-25 16:05:49 -080052#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -070053CXFA_FFWidget* CPDFSDK_Widget::GetMixXFAWidget() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080055 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
57 if (!m_hMixXFAWidget) {
dsinclairdf4bc592016-03-31 20:34:43 -070058 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 CFX_WideString sName;
Lei Zhang99766722016-02-23 11:21:48 -080060 if (GetFieldType() == FIELDTYPE_RADIOBUTTON) {
61 sName = GetAnnotName();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 if (sName.IsEmpty())
63 sName = GetName();
Dan Sinclair738b08c2016-03-01 14:45:20 -050064 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 sName = GetName();
Dan Sinclair738b08c2016-03-01 14:45:20 -050066 }
Bo Xufdc00a72014-10-28 23:03:33 -070067
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 if (!sName.IsEmpty())
tsepez70c55202016-04-14 15:32:35 -070069 m_hMixXFAWidget = pDocView->GetWidgetByName(sName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -070071 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 return m_hMixXFAWidget;
73 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070074
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 return NULL;
76}
77
dsinclairdf4bc592016-03-31 20:34:43 -070078CXFA_FFWidget* CPDFSDK_Widget::GetGroupMixXFAWidget() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080080 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
dsinclairdf4bc592016-03-31 20:34:43 -070082 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 CFX_WideString sName = GetName();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 if (!sName.IsEmpty())
tsepez70c55202016-04-14 15:32:35 -070085 return pDocView->GetWidgetByName(sName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 }
87 }
88
Tom Sepez50d12ad2015-11-24 09:50:51 -080089 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090}
91
dsinclairdf4bc592016-03-31 20:34:43 -070092CXFA_FFWidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080094 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
96 if (!m_pWidgetHandler) {
dsinclairdf4bc592016-03-31 20:34:43 -070097 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 m_pWidgetHandler = pDocView->GetWidgetHandler();
99 }
100 }
101 return m_pWidgetHandler;
102 }
103
104 return NULL;
105}
106
107static XFA_EVENTTYPE GetXFAEventType(PDFSDK_XFAAActionType eXFAAAT) {
108 XFA_EVENTTYPE eEventType = XFA_EVENT_Unknown;
109
110 switch (eXFAAAT) {
111 case PDFSDK_XFA_Click:
112 eEventType = XFA_EVENT_Click;
113 break;
114 case PDFSDK_XFA_Full:
115 eEventType = XFA_EVENT_Full;
116 break;
117 case PDFSDK_XFA_PreOpen:
118 eEventType = XFA_EVENT_PreOpen;
119 break;
120 case PDFSDK_XFA_PostOpen:
121 eEventType = XFA_EVENT_PostOpen;
122 break;
123 }
124
125 return eEventType;
126}
127
128static XFA_EVENTTYPE GetXFAEventType(CPDF_AAction::AActionType eAAT,
129 FX_BOOL bWillCommit) {
130 XFA_EVENTTYPE eEventType = XFA_EVENT_Unknown;
131
132 switch (eAAT) {
133 case CPDF_AAction::CursorEnter:
134 eEventType = XFA_EVENT_MouseEnter;
135 break;
136 case CPDF_AAction::CursorExit:
137 eEventType = XFA_EVENT_MouseExit;
138 break;
139 case CPDF_AAction::ButtonDown:
140 eEventType = XFA_EVENT_MouseDown;
141 break;
142 case CPDF_AAction::ButtonUp:
143 eEventType = XFA_EVENT_MouseUp;
144 break;
145 case CPDF_AAction::GetFocus:
146 eEventType = XFA_EVENT_Enter;
147 break;
148 case CPDF_AAction::LoseFocus:
149 eEventType = XFA_EVENT_Exit;
150 break;
151 case CPDF_AAction::PageOpen:
152 break;
153 case CPDF_AAction::PageClose:
154 break;
155 case CPDF_AAction::PageVisible:
156 break;
157 case CPDF_AAction::PageInvisible:
158 break;
159 case CPDF_AAction::KeyStroke:
160 if (!bWillCommit) {
161 eEventType = XFA_EVENT_Change;
162 }
163 break;
164 case CPDF_AAction::Validate:
165 eEventType = XFA_EVENT_Validate;
166 break;
167 case CPDF_AAction::OpenPage:
168 case CPDF_AAction::ClosePage:
169 case CPDF_AAction::Format:
170 case CPDF_AAction::Calculate:
171 case CPDF_AAction::CloseDocument:
172 case CPDF_AAction::SaveDocument:
173 case CPDF_AAction::DocumentSaved:
174 case CPDF_AAction::PrintDocument:
175 case CPDF_AAction::DocumentPrinted:
176 break;
177 }
178
179 return eEventType;
180}
181
182FX_BOOL CPDFSDK_Widget::HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT) {
dsinclairdf4bc592016-03-31 20:34:43 -0700183 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
184 if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
186
187 if ((eEventType == XFA_EVENT_Click || eEventType == XFA_EVENT_Change) &&
188 GetFieldType() == FIELDTYPE_RADIOBUTTON) {
dsinclairdf4bc592016-03-31 20:34:43 -0700189 if (CXFA_FFWidget* hGroupWidget = GetGroupMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700190 CXFA_WidgetAcc* pAcc = hGroupWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191 if (pXFAWidgetHandler->HasEvent(pAcc, eEventType))
192 return TRUE;
193 }
194 }
195
196 {
dsinclair221caf62016-04-04 12:08:40 -0700197 CXFA_WidgetAcc* pAcc = hWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 return pXFAWidgetHandler->HasEvent(pAcc, eEventType);
199 }
200 }
201 }
202
203 return FALSE;
204}
205
206FX_BOOL CPDFSDK_Widget::OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
207 PDFSDK_FieldAction& data,
208 CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800210 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -0700211 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
213
214 if (eEventType != XFA_EVENT_Unknown) {
dsinclairdf4bc592016-03-31 20:34:43 -0700215 if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 CXFA_EventParam param;
217 param.m_eType = eEventType;
218 param.m_wsChange = data.sChange;
219 param.m_iCommitKey = data.nCommitKey;
220 param.m_bShift = data.bShift;
221 param.m_iSelStart = data.nSelStart;
222 param.m_iSelEnd = data.nSelEnd;
223 param.m_wsFullText = data.sValue;
224 param.m_bKeyDown = data.bKeyDown;
225 param.m_bModifier = data.bModifier;
226 param.m_wsNewText = data.sValue;
227 if (data.nSelEnd > data.nSelStart)
228 param.m_wsNewText.Delete(data.nSelStart,
229 data.nSelEnd - data.nSelStart);
230 for (int i = 0; i < data.sChange.GetLength(); i++)
231 param.m_wsNewText.Insert(data.nSelStart, data.sChange[i]);
232 param.m_wsPrevText = data.sValue;
233
234 if ((eEventType == XFA_EVENT_Click || eEventType == XFA_EVENT_Change) &&
235 GetFieldType() == FIELDTYPE_RADIOBUTTON) {
dsinclairdf4bc592016-03-31 20:34:43 -0700236 if (CXFA_FFWidget* hGroupWidget = GetGroupMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700237 CXFA_WidgetAcc* pAcc = hGroupWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 param.m_pTarget = pAcc;
Wei Li42d1c1c2016-03-22 12:52:50 -0700239 if (pXFAWidgetHandler->ProcessEvent(pAcc, &param) !=
240 XFA_EVENTERROR_Success) {
241 return FALSE;
242 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 }
dsinclair221caf62016-04-04 12:08:40 -0700245 CXFA_WidgetAcc* pAcc = hWidget->GetDataAcc();
Wei Li42d1c1c2016-03-22 12:52:50 -0700246 param.m_pTarget = pAcc;
247 int32_t nRet = pXFAWidgetHandler->ProcessEvent(pAcc, &param);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248
dsinclairdf4bc592016-03-31 20:34:43 -0700249 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 pDocView->UpdateDocView();
251 }
Wei Li42d1c1c2016-03-22 12:52:50 -0700252 return nRet == XFA_EVENTERROR_Success;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 }
254 }
255 }
256
257 return FALSE;
258}
259
260void CPDFSDK_Widget::Synchronize(FX_BOOL bSynchronizeElse) {
dsinclairdf4bc592016-03-31 20:34:43 -0700261 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700262 CPDF_FormField* pFormField = GetFormField();
263 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
264 switch (GetFieldType()) {
265 case FIELDTYPE_CHECKBOX:
266 case FIELDTYPE_RADIOBUTTON: {
267 CPDF_FormControl* pFormCtrl = GetFormControl();
268 XFA_CHECKSTATE eCheckState =
269 pFormCtrl->IsChecked() ? XFA_CHECKSTATE_On : XFA_CHECKSTATE_Off;
thestig800222e2016-05-26 22:00:29 -0700270 pWidgetAcc->SetCheckState(eCheckState, true);
dsinclair221caf62016-04-04 12:08:40 -0700271 } break;
272 case FIELDTYPE_TEXTFIELD:
273 pWidgetAcc->SetValue(pFormField->GetValue(), XFA_VALUEPICTURE_Edit);
274 break;
275 case FIELDTYPE_LISTBOX: {
276 pWidgetAcc->ClearAllSelections();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277
dsinclair221caf62016-04-04 12:08:40 -0700278 for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz; i++) {
279 int nIndex = pFormField->GetSelectedIndex(i);
280 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
thestig800222e2016-05-26 22:00:29 -0700281 pWidgetAcc->SetItemState(nIndex, TRUE, false, FALSE, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 }
dsinclair221caf62016-04-04 12:08:40 -0700283 } break;
284 case FIELDTYPE_COMBOBOX: {
285 pWidgetAcc->ClearAllSelections();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286
dsinclair221caf62016-04-04 12:08:40 -0700287 for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz; i++) {
288 int nIndex = pFormField->GetSelectedIndex(i);
289 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
thestig800222e2016-05-26 22:00:29 -0700290 pWidgetAcc->SetItemState(nIndex, TRUE, false, FALSE, TRUE);
dsinclair221caf62016-04-04 12:08:40 -0700291 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 }
293
dsinclair221caf62016-04-04 12:08:40 -0700294 pWidgetAcc->SetValue(pFormField->GetValue(), XFA_VALUEPICTURE_Edit);
295 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 }
dsinclair221caf62016-04-04 12:08:40 -0700297
298 if (bSynchronizeElse)
299 pWidgetAcc->ProcessValueChanged();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 }
301 }
302}
303
304void CPDFSDK_Widget::SynchronizeXFAValue() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800306 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -0700307 CXFA_FFDocView* pXFADocView = pDoc->GetXFADocView();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 if (!pXFADocView)
309 return;
310
dsinclairdf4bc592016-03-31 20:34:43 -0700311 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700312 if (GetXFAWidgetHandler()) {
313 CPDFSDK_Widget::SynchronizeXFAValue(pXFADocView, hWidget, GetFormField(),
314 GetFormControl());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 }
316 }
317}
318
319void CPDFSDK_Widget::SynchronizeXFAItems() {
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 SynchronizeXFAItems(pXFADocView, hWidget, GetFormField(), nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700329 }
330}
331
dsinclairdf4bc592016-03-31 20:34:43 -0700332void CPDFSDK_Widget::SynchronizeXFAValue(CXFA_FFDocView* pXFADocView,
333 CXFA_FFWidget* hWidget,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 CPDF_FormField* pFormField,
335 CPDF_FormControl* pFormControl) {
Lei Zhang5eca3052016-02-22 20:32:21 -0800336 ASSERT(hWidget);
dsinclair221caf62016-04-04 12:08:40 -0700337 ASSERT(pFormControl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338
dsinclair221caf62016-04-04 12:08:40 -0700339 switch (pFormField->GetFieldType()) {
340 case FIELDTYPE_CHECKBOX: {
341 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
342 pFormField->CheckControl(
343 pFormField->GetControlIndex(pFormControl),
344 pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On, true);
345 }
346 } break;
347 case FIELDTYPE_RADIOBUTTON: {
348 // TODO(weili): Check whether we need to handle checkbox and radio
349 // button differently, otherwise, merge these two cases.
350 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
351 pFormField->CheckControl(
352 pFormField->GetControlIndex(pFormControl),
353 pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On, true);
354 }
355 } break;
356 case FIELDTYPE_TEXTFIELD: {
357 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
358 CFX_WideString sValue;
359 pWidgetAcc->GetValue(sValue, XFA_VALUEPICTURE_Display);
360 pFormField->SetValue(sValue, TRUE);
361 }
362 } break;
363 case FIELDTYPE_LISTBOX: {
364 pFormField->ClearSelection(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365
dsinclair221caf62016-04-04 12:08:40 -0700366 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
367 for (int i = 0, sz = pWidgetAcc->CountSelectedItems(); i < sz; i++) {
368 int nIndex = pWidgetAcc->GetSelectedItem(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369
dsinclair221caf62016-04-04 12:08:40 -0700370 if (nIndex > -1 && nIndex < pFormField->CountOptions()) {
371 pFormField->SetItemSelection(nIndex, TRUE, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 }
373 }
dsinclair221caf62016-04-04 12:08:40 -0700374 }
375 } break;
376 case FIELDTYPE_COMBOBOX: {
377 pFormField->ClearSelection(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378
dsinclair221caf62016-04-04 12:08:40 -0700379 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
380 for (int i = 0, sz = pWidgetAcc->CountSelectedItems(); i < sz; i++) {
381 int nIndex = pWidgetAcc->GetSelectedItem(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382
dsinclair221caf62016-04-04 12:08:40 -0700383 if (nIndex > -1 && nIndex < pFormField->CountOptions()) {
384 pFormField->SetItemSelection(nIndex, TRUE, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 }
dsinclair221caf62016-04-04 12:08:40 -0700387
388 CFX_WideString sValue;
389 pWidgetAcc->GetValue(sValue, XFA_VALUEPICTURE_Display);
390 pFormField->SetValue(sValue, TRUE);
391 }
392 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 }
394}
395
dsinclairdf4bc592016-03-31 20:34:43 -0700396void CPDFSDK_Widget::SynchronizeXFAItems(CXFA_FFDocView* pXFADocView,
397 CXFA_FFWidget* hWidget,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 CPDF_FormField* pFormField,
399 CPDF_FormControl* pFormControl) {
Lei Zhang5eca3052016-02-22 20:32:21 -0800400 ASSERT(hWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401
dsinclair221caf62016-04-04 12:08:40 -0700402 switch (pFormField->GetFieldType()) {
403 case FIELDTYPE_LISTBOX: {
404 pFormField->ClearSelection(FALSE);
405 pFormField->ClearOptions(TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406
dsinclair221caf62016-04-04 12:08:40 -0700407 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
408 for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(); i < sz; i++) {
409 CFX_WideString swText;
410 pWidgetAcc->GetChoiceListItem(swText, i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411
dsinclair221caf62016-04-04 12:08:40 -0700412 pFormField->InsertOption(swText, i, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413 }
dsinclair221caf62016-04-04 12:08:40 -0700414 }
415 } break;
416 case FIELDTYPE_COMBOBOX: {
417 pFormField->ClearSelection(FALSE);
418 pFormField->ClearOptions(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419
dsinclair221caf62016-04-04 12:08:40 -0700420 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
421 for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(); i < sz; i++) {
422 CFX_WideString swText;
423 pWidgetAcc->GetChoiceListItem(swText, i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424
dsinclair221caf62016-04-04 12:08:40 -0700425 pFormField->InsertOption(swText, i, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 }
dsinclair221caf62016-04-04 12:08:40 -0700427 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428
dsinclair221caf62016-04-04 12:08:40 -0700429 pFormField->SetValue(L"", TRUE);
430 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 }
432}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800433#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434
435FX_BOOL CPDFSDK_Widget::IsWidgetAppearanceValid(
436 CPDF_Annot::AppearanceMode mode) {
Wei Li9b761132016-01-29 15:44:20 -0800437 CPDF_Dictionary* pAP = m_pAnnot->GetAnnotDict()->GetDictBy("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438 if (!pAP)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700439 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700440
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700441 // Choose the right sub-ap
442 const FX_CHAR* ap_entry = "N";
443 if (mode == CPDF_Annot::Down)
444 ap_entry = "D";
445 else if (mode == CPDF_Annot::Rollover)
446 ap_entry = "R";
447 if (!pAP->KeyExist(ap_entry))
448 ap_entry = "N";
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700449
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700450 // Get the AP stream or subdirectory
tsepezbd567552016-03-29 14:51:50 -0700451 CPDF_Object* psub = pAP->GetDirectObjectBy(ap_entry);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 if (!psub)
453 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700454
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 int nFieldType = GetFieldType();
456 switch (nFieldType) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700457 case FIELDTYPE_PUSHBUTTON:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700458 case FIELDTYPE_COMBOBOX:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700459 case FIELDTYPE_LISTBOX:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700460 case FIELDTYPE_TEXTFIELD:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 case FIELDTYPE_SIGNATURE:
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400462 return psub->IsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 case FIELDTYPE_CHECKBOX:
464 case FIELDTYPE_RADIOBUTTON:
Dan Sinclairf1251c12015-10-20 16:24:45 -0400465 if (CPDF_Dictionary* pSubDict = psub->AsDictionary()) {
tsepez7b1ccf92016-04-14 11:04:57 -0700466 return !!pSubDict->GetStreamBy(GetAppState());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 }
468 return FALSE;
469 }
470 return TRUE;
471}
472
473int CPDFSDK_Widget::GetFieldType() const {
Lei Zhang96660d62015-12-14 18:27:25 -0800474 return GetFormField()->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475}
476
477FX_BOOL CPDFSDK_Widget::IsAppearanceValid() {
Tom Sepez51da0932015-11-25 16:05:49 -0800478#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800480 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 int nDocType = pDoc->GetDocType();
Tom Sepez540c4362015-11-24 13:33:57 -0800482 if (nDocType != DOCTYPE_PDF && nDocType != DOCTYPE_STATIC_XFA)
483 return TRUE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800484#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800485 return CPDFSDK_BAAnnot::IsAppearanceValid();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486}
487
488int CPDFSDK_Widget::GetFieldFlags() const {
489 CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 CPDF_FormControl* pFormControl =
491 pPDFInterForm->GetControlByDict(m_pAnnot->GetAnnotDict());
492 CPDF_FormField* pFormField = pFormControl->GetField();
493 return pFormField->GetFieldFlags();
494}
495
496CFX_ByteString CPDFSDK_Widget::GetSubType() const {
497 int nType = GetFieldType();
498
499 if (nType == FIELDTYPE_SIGNATURE)
500 return BFFT_SIGNATURE;
501 return CPDFSDK_Annot::GetSubType();
502}
503
504CPDF_FormField* CPDFSDK_Widget::GetFormField() const {
Lei Zhang1b700c32015-10-30 23:55:35 -0700505 return GetFormControl()->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506}
507
508CPDF_FormControl* CPDFSDK_Widget::GetFormControl() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 return pPDFInterForm->GetControlByDict(GetAnnotDict());
511}
512
Lei Zhang1b700c32015-10-30 23:55:35 -0700513CPDF_FormControl* CPDFSDK_Widget::GetFormControl(
514 CPDF_InterForm* pInterForm,
515 const CPDF_Dictionary* pAnnotDict) {
Lei Zhang96660d62015-12-14 18:27:25 -0800516 ASSERT(pAnnotDict);
Lei Zhang1b700c32015-10-30 23:55:35 -0700517 return pInterForm->GetControlByDict(pAnnotDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518}
519
520int CPDFSDK_Widget::GetRotate() const {
521 CPDF_FormControl* pCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522 return pCtrl->GetRotation() % 360;
523}
524
Tom Sepez51da0932015-11-25 16:05:49 -0800525#ifdef PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -0700526CFX_WideString CPDFSDK_Widget::GetName() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528 return pFormField->GetFullName();
529}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800530#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700531
532FX_BOOL CPDFSDK_Widget::GetFillColor(FX_COLORREF& color) const {
533 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534 int iColorType = 0;
535 color = FX_ARGBTOCOLORREF(pFormCtrl->GetBackgroundColor(iColorType));
536
537 return iColorType != COLORTYPE_TRANSPARENT;
538}
539
540FX_BOOL CPDFSDK_Widget::GetBorderColor(FX_COLORREF& color) const {
541 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700542 int iColorType = 0;
543 color = FX_ARGBTOCOLORREF(pFormCtrl->GetBorderColor(iColorType));
544
545 return iColorType != COLORTYPE_TRANSPARENT;
546}
547
548FX_BOOL CPDFSDK_Widget::GetTextColor(FX_COLORREF& color) const {
549 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
551 if (da.HasColor()) {
552 FX_ARGB argb;
553 int iColorType = COLORTYPE_TRANSPARENT;
554 da.GetColor(argb, iColorType);
555 color = FX_ARGBTOCOLORREF(argb);
556
557 return iColorType != COLORTYPE_TRANSPARENT;
558 }
559
560 return FALSE;
561}
562
563FX_FLOAT CPDFSDK_Widget::GetFontSize() const {
564 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565 CPDF_DefaultAppearance pDa = pFormCtrl->GetDefaultAppearance();
566 CFX_ByteString csFont = "";
567 FX_FLOAT fFontSize = 0.0f;
568 pDa.GetFont(csFont, fFontSize);
569
570 return fFontSize;
571}
572
Tom Sepezbf59a072015-10-21 14:07:23 -0700573int CPDFSDK_Widget::GetSelectedIndex(int nIndex) const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700574#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -0700575 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700576 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
577 if (nIndex < pWidgetAcc->CountSelectedItems())
578 return pWidgetAcc->GetSelectedItem(nIndex);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700579 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700581#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 return pFormField->GetSelectedIndex(nIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700584}
585
Tom Sepeza8a39e22015-10-12 15:47:07 -0700586#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -0800587CFX_WideString CPDFSDK_Widget::GetValue(FX_BOOL bDisplay) const {
dsinclairdf4bc592016-03-31 20:34:43 -0700588 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700589 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
590 CFX_WideString sValue;
591 pWidgetAcc->GetValue(
592 sValue, bDisplay ? XFA_VALUEPICTURE_Display : XFA_VALUEPICTURE_Edit);
593 return sValue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700594 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700595 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800596#else
597CFX_WideString CPDFSDK_Widget::GetValue() const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700598#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600 return pFormField->GetValue();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700601}
602
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603CFX_WideString CPDFSDK_Widget::GetDefaultValue() const {
604 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605 return pFormField->GetDefaultValue();
606}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700607
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608CFX_WideString CPDFSDK_Widget::GetOptionLabel(int nIndex) const {
609 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700610 return pFormField->GetOptionLabel(nIndex);
611}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700612
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613int CPDFSDK_Widget::CountOptions() const {
614 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700615 return pFormField->CountOptions();
616}
617
Tom Sepezbf59a072015-10-21 14:07:23 -0700618FX_BOOL CPDFSDK_Widget::IsOptionSelected(int nIndex) const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700619#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -0700620 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700621 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
622 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
623 return pWidgetAcc->GetItemState(nIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700624
dsinclair221caf62016-04-04 12:08:40 -0700625 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700626 }
627 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700628#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700629 CPDF_FormField* pFormField = GetFormField();
630 return pFormField->IsItemSelected(nIndex);
631}
632
633int CPDFSDK_Widget::GetTopVisibleIndex() const {
634 CPDF_FormField* pFormField = GetFormField();
635 return pFormField->GetTopVisibleIndex();
636}
637
Wei Li97da9762016-03-11 17:00:48 -0800638bool CPDFSDK_Widget::IsChecked() const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700639#ifdef PDF_ENABLE_XFA
dsinclair221caf62016-04-04 12:08:40 -0700640 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
641 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc())
642 return pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700644#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646 return pFormCtrl->IsChecked();
647}
648
649int CPDFSDK_Widget::GetAlignment() const {
650 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700651 return pFormCtrl->GetControlAlignment();
652}
653
654int CPDFSDK_Widget::GetMaxLen() const {
655 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656 return pFormField->GetMaxLen();
657}
658
Wei Li97da9762016-03-11 17:00:48 -0800659void CPDFSDK_Widget::SetCheck(bool bChecked, bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661 CPDF_FormField* pFormField = pFormCtrl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700662 pFormField->CheckControl(pFormField->GetControlIndex(pFormCtrl), bChecked,
663 bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800664#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700665 if (!IsWidgetAppearanceValid(CPDF_Annot::Normal))
666 ResetAppearance(TRUE);
667 if (!bNotify)
668 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800669#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700670}
671
672void CPDFSDK_Widget::SetValue(const CFX_WideString& sValue, FX_BOOL bNotify) {
673 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700674 pFormField->SetValue(sValue, bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800675#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700676 if (!bNotify)
677 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800678#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679}
680
681void CPDFSDK_Widget::SetDefaultValue(const CFX_WideString& sValue) {}
682void CPDFSDK_Widget::SetOptionSelection(int index,
683 FX_BOOL bSelected,
684 FX_BOOL bNotify) {
685 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700686 pFormField->SetItemSelection(index, bSelected, bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800687#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 if (!bNotify)
689 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800690#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700691}
692
693void CPDFSDK_Widget::ClearSelection(FX_BOOL bNotify) {
694 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700695 pFormField->ClearSelection(bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800696#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700697 if (!bNotify)
698 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800699#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700}
701
702void CPDFSDK_Widget::SetTopVisibleIndex(int index) {}
703
704void CPDFSDK_Widget::SetAppModified() {
705 m_bAppModified = TRUE;
706}
707
708void CPDFSDK_Widget::ClearAppModified() {
709 m_bAppModified = FALSE;
710}
711
712FX_BOOL CPDFSDK_Widget::IsAppModified() const {
713 return m_bAppModified;
714}
715
Tom Sepez51da0932015-11-25 16:05:49 -0800716#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700717void CPDFSDK_Widget::ResetAppearance(FX_BOOL bValueChanged) {
718 switch (GetFieldType()) {
719 case FIELDTYPE_TEXTFIELD:
720 case FIELDTYPE_COMBOBOX: {
721 FX_BOOL bFormated = FALSE;
Lei Zhang99766722016-02-23 11:21:48 -0800722 CFX_WideString sValue = OnFormat(bFormated);
tsepezbd9748d2016-04-13 21:40:19 -0700723 ResetAppearance(bFormated ? sValue.c_str() : nullptr, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724 } break;
725 default:
Lei Zhang99766722016-02-23 11:21:48 -0800726 ResetAppearance(nullptr, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727 break;
728 }
729}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800730#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731
732void CPDFSDK_Widget::ResetAppearance(const FX_WCHAR* sValue,
733 FX_BOOL bValueChanged) {
734 SetAppModified();
735
736 m_nAppAge++;
737 if (m_nAppAge > 999999)
738 m_nAppAge = 0;
739 if (bValueChanged)
740 m_nValueAge++;
741
742 int nFieldType = GetFieldType();
743
744 switch (nFieldType) {
745 case FIELDTYPE_PUSHBUTTON:
746 ResetAppearance_PushButton();
747 break;
748 case FIELDTYPE_CHECKBOX:
749 ResetAppearance_CheckBox();
750 break;
751 case FIELDTYPE_RADIOBUTTON:
752 ResetAppearance_RadioButton();
753 break;
754 case FIELDTYPE_COMBOBOX:
755 ResetAppearance_ComboBox(sValue);
756 break;
757 case FIELDTYPE_LISTBOX:
758 ResetAppearance_ListBox();
759 break;
760 case FIELDTYPE_TEXTFIELD:
761 ResetAppearance_TextField(sValue);
762 break;
763 }
764
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700765 m_pAnnot->ClearCachedAP();
766}
767
768CFX_WideString CPDFSDK_Widget::OnFormat(FX_BOOL& bFormated) {
769 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800770 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700771 return m_pInterForm->OnFormat(pFormField, bFormated);
772}
773
774void CPDFSDK_Widget::ResetFieldAppearance(FX_BOOL bValueChanged) {
775 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800776 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700777 m_pInterForm->ResetFieldAppearance(pFormField, NULL, bValueChanged);
778}
779
780void CPDFSDK_Widget::DrawAppearance(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800781 const CFX_Matrix* pUser2Device,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700782 CPDF_Annot::AppearanceMode mode,
783 const CPDF_RenderOptions* pOptions) {
784 int nFieldType = GetFieldType();
785
786 if ((nFieldType == FIELDTYPE_CHECKBOX ||
787 nFieldType == FIELDTYPE_RADIOBUTTON) &&
788 mode == CPDF_Annot::Normal &&
789 !IsWidgetAppearanceValid(CPDF_Annot::Normal)) {
790 CFX_PathData pathData;
791
Tom Sepez281a9ea2016-02-26 14:24:28 -0800792 CFX_FloatRect rcAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700793
794 pathData.AppendRect(rcAnnot.left, rcAnnot.bottom, rcAnnot.right,
795 rcAnnot.top);
796
797 CFX_GraphStateData gsd;
798 gsd.m_LineWidth = 0.0f;
799
800 pDevice->DrawPath(&pathData, pUser2Device, &gsd, 0, 0xFFAAAAAA,
801 FXFILL_ALTERNATE);
802 } else {
803 CPDFSDK_BAAnnot::DrawAppearance(pDevice, pUser2Device, mode, pOptions);
804 }
805}
806
807void CPDFSDK_Widget::UpdateField() {
808 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800809 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700810 m_pInterForm->UpdateField(pFormField);
811}
812
813void CPDFSDK_Widget::DrawShadow(CFX_RenderDevice* pDevice,
814 CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815 int nFieldType = GetFieldType();
816 if (m_pInterForm->IsNeedHighLight(nFieldType)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800817 CFX_FloatRect rc = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818 FX_COLORREF color = m_pInterForm->GetHighlightColor(nFieldType);
819 uint8_t alpha = m_pInterForm->GetHighlightAlpha();
820
821 CFX_FloatRect rcDevice;
822 ASSERT(m_pInterForm->GetDocument());
823 CPDFDoc_Environment* pEnv = m_pInterForm->GetDocument()->GetEnv();
824 if (!pEnv)
825 return;
Tom Sepez60d909e2015-12-10 15:34:55 -0800826 CFX_Matrix page2device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827 pPageView->GetCurrentMatrix(page2device);
828 page2device.Transform(((FX_FLOAT)rc.left), ((FX_FLOAT)rc.bottom),
829 rcDevice.left, rcDevice.bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700830 page2device.Transform(((FX_FLOAT)rc.right), ((FX_FLOAT)rc.top),
831 rcDevice.right, rcDevice.top);
832
833 rcDevice.Normalize();
834
835 FX_ARGB argb = ArgbEncode((int)alpha, color);
836 FX_RECT rcDev((int)rcDevice.left, (int)rcDevice.top, (int)rcDevice.right,
837 (int)rcDevice.bottom);
838 pDevice->FillRect(&rcDev, argb);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700839 }
840}
841
842void CPDFSDK_Widget::ResetAppearance_PushButton() {
843 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800844 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845 int32_t nLayout = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846 switch (pControl->GetTextPosition()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700847 case TEXTPOS_ICON:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700848 nLayout = PPBL_ICON;
849 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700850 case TEXTPOS_BELOW:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851 nLayout = PPBL_ICONTOPLABELBOTTOM;
852 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700853 case TEXTPOS_ABOVE:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 nLayout = PPBL_LABELTOPICONBOTTOM;
855 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700856 case TEXTPOS_RIGHT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700857 nLayout = PPBL_ICONLEFTLABELRIGHT;
858 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700859 case TEXTPOS_LEFT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860 nLayout = PPBL_LABELLEFTICONRIGHT;
861 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700862 case TEXTPOS_OVERLAID:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700863 nLayout = PPBL_LABELOVERICON;
864 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700865 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866 nLayout = PPBL_LABEL;
867 break;
868 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700869
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700870 CPWL_Color crBackground, crBorder;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700871
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872 int iColorType;
873 FX_FLOAT fc[4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700874
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875 pControl->GetOriginalBackgroundColor(iColorType, fc);
876 if (iColorType > 0)
877 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700878
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700879 pControl->GetOriginalBorderColor(iColorType, fc);
880 if (iColorType > 0)
881 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700882
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884 CPWL_Dash dsBorder(3, 0, 0);
885 CPWL_Color crLeftTop, crRightBottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700886
dsinclair92cb5e52016-05-16 11:38:28 -0700887 BorderStyle nBorderStyle = GetBorderStyle();
888 switch (nBorderStyle) {
889 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890 dsBorder = CPWL_Dash(3, 3, 0);
891 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700892 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 fBorderWidth *= 2;
894 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
895 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
896 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700897 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898 fBorderWidth *= 2;
899 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
900 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
901 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700902 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700903 break;
904 }
905
Tom Sepez281a9ea2016-02-26 14:24:28 -0800906 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700907
908 CPWL_Color crText(COLORTYPE_GRAY, 0);
909
910 FX_FLOAT fFontSize = 12.0f;
911 CFX_ByteString csNameTag;
912
913 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
914 if (da.HasColor()) {
915 da.GetColor(iColorType, fc);
916 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
917 }
918
919 if (da.HasFont())
920 da.GetFont(csNameTag, fFontSize);
921
922 CFX_WideString csWCaption;
923 CFX_WideString csNormalCaption, csRolloverCaption, csDownCaption;
924
925 if (pControl->HasMKEntry("CA")) {
926 csNormalCaption = pControl->GetNormalCaption();
927 }
928 if (pControl->HasMKEntry("RC")) {
929 csRolloverCaption = pControl->GetRolloverCaption();
930 }
931 if (pControl->HasMKEntry("AC")) {
932 csDownCaption = pControl->GetDownCaption();
933 }
934
935 CPDF_Stream* pNormalIcon = NULL;
936 CPDF_Stream* pRolloverIcon = NULL;
937 CPDF_Stream* pDownIcon = NULL;
938
939 if (pControl->HasMKEntry("I")) {
940 pNormalIcon = pControl->GetNormalIcon();
941 }
942 if (pControl->HasMKEntry("RI")) {
943 pRolloverIcon = pControl->GetRolloverIcon();
944 }
945 if (pControl->HasMKEntry("IX")) {
946 pDownIcon = pControl->GetDownIcon();
947 }
948
949 if (pNormalIcon) {
950 if (CPDF_Dictionary* pImageDict = pNormalIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800951 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700952 pImageDict->SetAtString("Name", "ImgA");
953 }
954 }
955
956 if (pRolloverIcon) {
957 if (CPDF_Dictionary* pImageDict = pRolloverIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800958 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700959 pImageDict->SetAtString("Name", "ImgB");
960 }
961 }
962
963 if (pDownIcon) {
964 if (CPDF_Dictionary* pImageDict = pDownIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800965 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700966 pImageDict->SetAtString("Name", "ImgC");
967 }
968 }
969
970 CPDF_IconFit iconFit = pControl->GetIconFit();
971
972 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700973 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
974
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800975 CBA_FontMap font_map(this, pEnv->GetSysHandler());
976 font_map.SetAPType("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977
978 CFX_ByteString csAP =
979 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
980 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
981 crLeftTop, crRightBottom, nBorderStyle,
982 dsBorder) +
983 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800984 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700985 pNormalIcon, iconFit, csNormalCaption, crText, fFontSize, nLayout);
986
987 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP);
988 if (pNormalIcon)
989 AddImageToAppearance("N", pNormalIcon);
990
991 CPDF_FormControl::HighlightingMode eHLM = pControl->GetHighlightingMode();
992 if (eHLM == CPDF_FormControl::Push || eHLM == CPDF_FormControl::Toggle) {
993 if (csRolloverCaption.IsEmpty() && !pRolloverIcon) {
994 csRolloverCaption = csNormalCaption;
995 pRolloverIcon = pNormalIcon;
996 }
997
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800998 font_map.SetAPType("R");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999
1000 csAP = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1001 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1002 crLeftTop, crRightBottom,
1003 nBorderStyle, dsBorder) +
1004 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001005 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006 pRolloverIcon, iconFit, csRolloverCaption, crText, fFontSize,
1007 nLayout);
1008
1009 WriteAppearance("R", GetRotatedRect(), GetMatrix(), csAP);
1010 if (pRolloverIcon)
1011 AddImageToAppearance("R", pRolloverIcon);
1012
1013 if (csDownCaption.IsEmpty() && !pDownIcon) {
1014 csDownCaption = csNormalCaption;
1015 pDownIcon = pNormalIcon;
1016 }
1017
1018 switch (nBorderStyle) {
dsinclair92cb5e52016-05-16 11:38:28 -07001019 case BorderStyle::BEVELED: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 CPWL_Color crTemp = crLeftTop;
1021 crLeftTop = crRightBottom;
1022 crRightBottom = crTemp;
dsinclair92cb5e52016-05-16 11:38:28 -07001023 break;
1024 }
1025 case BorderStyle::INSET: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1027 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001028 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001029 }
1030 default:
1031 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001032 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001033
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001034 font_map.SetAPType("D");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001035
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001036 csAP = CPWL_Utils::GetRectFillAppStream(
1037 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1038 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1039 crLeftTop, crRightBottom,
1040 nBorderStyle, dsBorder) +
1041 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001042 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043 pDownIcon, iconFit, csDownCaption, crText, fFontSize, nLayout);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001044
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001046 if (pDownIcon)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047 AddImageToAppearance("D", pDownIcon);
1048 } else {
1049 RemoveAppearance("D");
1050 RemoveAppearance("R");
1051 }
1052}
1053
1054void CPDFSDK_Widget::ResetAppearance_CheckBox() {
1055 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056 CPWL_Color crBackground, crBorder, crText;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057 int iColorType;
1058 FX_FLOAT fc[4];
1059
1060 pControl->GetOriginalBackgroundColor(iColorType, fc);
1061 if (iColorType > 0)
1062 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1063
1064 pControl->GetOriginalBorderColor(iColorType, fc);
1065 if (iColorType > 0)
1066 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1067
1068 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001069 CPWL_Dash dsBorder(3, 0, 0);
1070 CPWL_Color crLeftTop, crRightBottom;
1071
dsinclair92cb5e52016-05-16 11:38:28 -07001072 BorderStyle nBorderStyle = GetBorderStyle();
1073 switch (nBorderStyle) {
1074 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 dsBorder = CPWL_Dash(3, 3, 0);
1076 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001077 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078 fBorderWidth *= 2;
1079 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1080 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1081 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001082 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001083 fBorderWidth *= 2;
1084 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1085 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1086 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088 break;
1089 }
1090
Tom Sepez281a9ea2016-02-26 14:24:28 -08001091 CFX_FloatRect rcWindow = GetRotatedRect();
1092 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093
1094 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
1095 if (da.HasColor()) {
1096 da.GetColor(iColorType, fc);
1097 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1098 }
1099
1100 int32_t nStyle = 0;
1101
1102 CFX_WideString csWCaption = pControl->GetNormalCaption();
1103 if (csWCaption.GetLength() > 0) {
1104 switch (csWCaption[0]) {
1105 case L'l':
1106 nStyle = PCS_CIRCLE;
1107 break;
1108 case L'8':
1109 nStyle = PCS_CROSS;
1110 break;
1111 case L'u':
1112 nStyle = PCS_DIAMOND;
1113 break;
1114 case L'n':
1115 nStyle = PCS_SQUARE;
1116 break;
1117 case L'H':
1118 nStyle = PCS_STAR;
1119 break;
1120 default: // L'4'
1121 nStyle = PCS_CHECK;
1122 break;
1123 }
1124 } else {
1125 nStyle = PCS_CHECK;
1126 }
1127
1128 CFX_ByteString csAP_N_ON =
1129 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1130 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1131 crLeftTop, crRightBottom, nBorderStyle,
1132 dsBorder);
1133
1134 CFX_ByteString csAP_N_OFF = csAP_N_ON;
1135
1136 switch (nBorderStyle) {
dsinclair92cb5e52016-05-16 11:38:28 -07001137 case BorderStyle::BEVELED: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001138 CPWL_Color crTemp = crLeftTop;
1139 crLeftTop = crRightBottom;
1140 crRightBottom = crTemp;
dsinclair92cb5e52016-05-16 11:38:28 -07001141 break;
1142 }
1143 case BorderStyle::INSET: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001144 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1145 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1146 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001147 }
1148 default:
1149 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001150 }
1151
1152 CFX_ByteString csAP_D_ON =
1153 CPWL_Utils::GetRectFillAppStream(
1154 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1155 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1156 crLeftTop, crRightBottom, nBorderStyle,
1157 dsBorder);
1158
1159 CFX_ByteString csAP_D_OFF = csAP_D_ON;
1160
1161 csAP_N_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
1162 csAP_D_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
1163
1164 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
1165 pControl->GetCheckedAPState());
1166 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1167
1168 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
1169 pControl->GetCheckedAPState());
1170 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1171
1172 CFX_ByteString csAS = GetAppState();
1173 if (csAS.IsEmpty())
1174 SetAppState("Off");
1175}
1176
1177void CPDFSDK_Widget::ResetAppearance_RadioButton() {
1178 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179 CPWL_Color crBackground, crBorder, crText;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001180 int iColorType;
1181 FX_FLOAT fc[4];
1182
1183 pControl->GetOriginalBackgroundColor(iColorType, fc);
1184 if (iColorType > 0)
1185 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1186
1187 pControl->GetOriginalBorderColor(iColorType, fc);
1188 if (iColorType > 0)
1189 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1190
1191 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001192 CPWL_Dash dsBorder(3, 0, 0);
1193 CPWL_Color crLeftTop, crRightBottom;
1194
dsinclair92cb5e52016-05-16 11:38:28 -07001195 BorderStyle nBorderStyle = GetBorderStyle();
1196 switch (nBorderStyle) {
1197 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001198 dsBorder = CPWL_Dash(3, 3, 0);
1199 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001200 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001201 fBorderWidth *= 2;
1202 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1203 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1204 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001205 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001206 fBorderWidth *= 2;
1207 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1208 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1209 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001211 break;
1212 }
1213
Tom Sepez281a9ea2016-02-26 14:24:28 -08001214 CFX_FloatRect rcWindow = GetRotatedRect();
1215 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001216
1217 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
1218 if (da.HasColor()) {
1219 da.GetColor(iColorType, fc);
1220 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1221 }
1222
1223 int32_t nStyle = 0;
1224
1225 CFX_WideString csWCaption = pControl->GetNormalCaption();
1226 if (csWCaption.GetLength() > 0) {
1227 switch (csWCaption[0]) {
1228 default: // L'l':
1229 nStyle = PCS_CIRCLE;
1230 break;
1231 case L'8':
1232 nStyle = PCS_CROSS;
1233 break;
1234 case L'u':
1235 nStyle = PCS_DIAMOND;
1236 break;
1237 case L'n':
1238 nStyle = PCS_SQUARE;
1239 break;
1240 case L'H':
1241 nStyle = PCS_STAR;
1242 break;
1243 case L'4':
1244 nStyle = PCS_CHECK;
1245 break;
1246 }
1247 } else {
1248 nStyle = PCS_CIRCLE;
1249 }
1250
1251 CFX_ByteString csAP_N_ON;
1252
Tom Sepez281a9ea2016-02-26 14:24:28 -08001253 CFX_FloatRect rcCenter =
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001254 CPWL_Utils::DeflateRect(CPWL_Utils::GetCenterSquare(rcWindow), 1.0f);
1255
1256 if (nStyle == PCS_CIRCLE) {
dsinclair92cb5e52016-05-16 11:38:28 -07001257 if (nBorderStyle == BorderStyle::BEVELED) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001258 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1259 crRightBottom = CPWL_Utils::SubstractColor(crBackground, 0.25f);
dsinclair92cb5e52016-05-16 11:38:28 -07001260 } else if (nBorderStyle == BorderStyle::INSET) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001261 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5f);
1262 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75f);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001263 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001264
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001265 csAP_N_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBackground) +
1266 CPWL_Utils::GetCircleBorderAppStream(
1267 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
1268 nBorderStyle, dsBorder);
1269 } else {
1270 csAP_N_ON = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1271 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1272 crLeftTop, crRightBottom,
1273 nBorderStyle, dsBorder);
1274 }
1275
1276 CFX_ByteString csAP_N_OFF = csAP_N_ON;
1277
1278 switch (nBorderStyle) {
dsinclair92cb5e52016-05-16 11:38:28 -07001279 case BorderStyle::BEVELED: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001280 CPWL_Color crTemp = crLeftTop;
1281 crLeftTop = crRightBottom;
1282 crRightBottom = crTemp;
dsinclair92cb5e52016-05-16 11:38:28 -07001283 break;
1284 }
1285 case BorderStyle::INSET: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001286 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1287 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1288 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001289 }
1290 default:
1291 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001292 }
1293
1294 CFX_ByteString csAP_D_ON;
1295
1296 if (nStyle == PCS_CIRCLE) {
1297 CPWL_Color crBK = CPWL_Utils::SubstractColor(crBackground, 0.25f);
dsinclair92cb5e52016-05-16 11:38:28 -07001298 if (nBorderStyle == BorderStyle::BEVELED) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001299 crLeftTop = CPWL_Utils::SubstractColor(crBackground, 0.25f);
1300 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1301 crBK = crBackground;
dsinclair92cb5e52016-05-16 11:38:28 -07001302 } else if (nBorderStyle == BorderStyle::INSET) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001303 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1304 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1305 }
1306
1307 csAP_D_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBK) +
1308 CPWL_Utils::GetCircleBorderAppStream(
1309 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
1310 nBorderStyle, dsBorder);
1311 } else {
1312 csAP_D_ON = CPWL_Utils::GetRectFillAppStream(
1313 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1314 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1315 crLeftTop, crRightBottom,
1316 nBorderStyle, dsBorder);
1317 }
1318
1319 CFX_ByteString csAP_D_OFF = csAP_D_ON;
1320
1321 csAP_N_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
1322 csAP_D_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
1323
1324 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
1325 pControl->GetCheckedAPState());
1326 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1327
1328 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
1329 pControl->GetCheckedAPState());
1330 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1331
1332 CFX_ByteString csAS = GetAppState();
1333 if (csAS.IsEmpty())
1334 SetAppState("Off");
1335}
1336
1337void CPDFSDK_Widget::ResetAppearance_ComboBox(const FX_WCHAR* sValue) {
1338 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001339 CPDF_FormField* pField = pControl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001340 CFX_ByteTextBuf sBody, sLines;
1341
Tom Sepez281a9ea2016-02-26 14:24:28 -08001342 CFX_FloatRect rcClient = GetClientRect();
1343 CFX_FloatRect rcButton = rcClient;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001344 rcButton.left = rcButton.right - 13;
1345 rcButton.Normalize();
1346
thestig732f6a02016-05-12 10:41:56 -07001347 IFX_Edit* pEdit = IFX_Edit::NewEdit();
1348 pEdit->EnableRefresh(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001349
thestig732f6a02016-05-12 10:41:56 -07001350 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1351 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
1352 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1353 pEdit->SetFontMap(&font_map);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001354
thestig732f6a02016-05-12 10:41:56 -07001355 CFX_FloatRect rcEdit = rcClient;
1356 rcEdit.right = rcButton.left;
1357 rcEdit.Normalize();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001358
thestig732f6a02016-05-12 10:41:56 -07001359 pEdit->SetPlateRect(rcEdit);
1360 pEdit->SetAlignmentV(1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001361
thestig732f6a02016-05-12 10:41:56 -07001362 FX_FLOAT fFontSize = GetFontSize();
1363 if (IsFloatZero(fFontSize))
1364 pEdit->SetAutoFontSize(TRUE);
1365 else
1366 pEdit->SetFontSize(fFontSize);
1367
1368 pEdit->Initialize();
1369
1370 if (sValue) {
1371 pEdit->SetText(sValue);
1372 } else {
1373 int32_t nCurSel = pField->GetSelectedIndex(0);
1374
1375 if (nCurSel < 0)
1376 pEdit->SetText(pField->GetValue().c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001377 else
thestig732f6a02016-05-12 10:41:56 -07001378 pEdit->SetText(pField->GetOptionLabel(nCurSel).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001379 }
1380
thestig732f6a02016-05-12 10:41:56 -07001381 CFX_FloatRect rcContent = pEdit->GetContentRect();
1382
1383 CFX_ByteString sEdit =
1384 CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, 0.0f));
1385 if (sEdit.GetLength() > 0) {
1386 sBody << "/Tx BMC\n"
1387 << "q\n";
1388 if (rcContent.Width() > rcEdit.Width() ||
1389 rcContent.Height() > rcEdit.Height()) {
1390 sBody << rcEdit.left << " " << rcEdit.bottom << " " << rcEdit.Width()
1391 << " " << rcEdit.Height() << " re\nW\nn\n";
1392 }
1393
1394 CPWL_Color crText = GetTextPWLColor();
1395 sBody << "BT\n"
1396 << CPWL_Utils::GetColorAppStream(crText) << sEdit << "ET\n"
1397 << "Q\nEMC\n";
1398 }
1399
1400 IFX_Edit::DelEdit(pEdit);
1401
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001402 sBody << CPWL_Utils::GetDropButtonAppStream(rcButton);
1403
1404 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001405 sLines.AsStringC() + sBody.AsStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001406
1407 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1408}
1409
1410void CPDFSDK_Widget::ResetAppearance_ListBox() {
1411 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001412 CPDF_FormField* pField = pControl->GetField();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001413 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001414 CFX_ByteTextBuf sBody, sLines;
1415
thestig732f6a02016-05-12 10:41:56 -07001416 IFX_Edit* pEdit = IFX_Edit::NewEdit();
1417 pEdit->EnableRefresh(FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001418
thestig732f6a02016-05-12 10:41:56 -07001419 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1420 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001421
thestig732f6a02016-05-12 10:41:56 -07001422 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1423 pEdit->SetFontMap(&font_map);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001424
thestig732f6a02016-05-12 10:41:56 -07001425 pEdit->SetPlateRect(CFX_FloatRect(rcClient.left, 0.0f, rcClient.right, 0.0f));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001426
thestig732f6a02016-05-12 10:41:56 -07001427 FX_FLOAT fFontSize = GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001428
thestig594b20b2016-05-12 21:56:43 -07001429 pEdit->SetFontSize(IsFloatZero(fFontSize) ? 12.0f : fFontSize);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001430
thestig732f6a02016-05-12 10:41:56 -07001431 pEdit->Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001432
thestig732f6a02016-05-12 10:41:56 -07001433 CFX_ByteTextBuf sList;
1434 FX_FLOAT fy = rcClient.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001435
thestig732f6a02016-05-12 10:41:56 -07001436 int32_t nTop = pField->GetTopVisibleIndex();
1437 int32_t nCount = pField->CountOptions();
1438 int32_t nSelCount = pField->CountSelectedItems();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001439
thestig594b20b2016-05-12 21:56:43 -07001440 for (int32_t i = nTop; i < nCount; ++i) {
1441 bool bSelected = false;
1442 for (int32_t j = 0; j < nSelCount; ++j) {
thestig732f6a02016-05-12 10:41:56 -07001443 if (pField->GetSelectedIndex(j) == i) {
thestig594b20b2016-05-12 21:56:43 -07001444 bSelected = true;
thestig732f6a02016-05-12 10:41:56 -07001445 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001446 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001447 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001448
thestig732f6a02016-05-12 10:41:56 -07001449 pEdit->SetText(pField->GetOptionLabel(i).c_str());
1450
1451 CFX_FloatRect rcContent = pEdit->GetContentRect();
1452 FX_FLOAT fItemHeight = rcContent.Height();
1453
1454 if (bSelected) {
1455 CFX_FloatRect rcItem =
1456 CFX_FloatRect(rcClient.left, fy - fItemHeight, rcClient.right, fy);
1457 sList << "q\n"
1458 << CPWL_Utils::GetColorAppStream(
1459 CPWL_Color(COLORTYPE_RGB, 0, 51.0f / 255.0f,
1460 113.0f / 255.0f),
1461 TRUE)
1462 << rcItem.left << " " << rcItem.bottom << " " << rcItem.Width()
1463 << " " << rcItem.Height() << " re f\n"
1464 << "Q\n";
1465
1466 sList << "BT\n"
1467 << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_GRAY, 1),
1468 TRUE)
1469 << CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, fy))
1470 << "ET\n";
1471 } else {
1472 CPWL_Color crText = GetTextPWLColor();
1473 sList << "BT\n"
1474 << CPWL_Utils::GetColorAppStream(crText, TRUE)
1475 << CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, fy))
1476 << "ET\n";
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001477 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001478
thestig732f6a02016-05-12 10:41:56 -07001479 fy -= fItemHeight;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001480 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001481
thestig732f6a02016-05-12 10:41:56 -07001482 if (sList.GetSize() > 0) {
1483 sBody << "/Tx BMC\n"
1484 << "q\n"
1485 << rcClient.left << " " << rcClient.bottom << " " << rcClient.Width()
1486 << " " << rcClient.Height() << " re\nW\nn\n";
1487 sBody << sList << "Q\nEMC\n";
1488 }
1489
1490 IFX_Edit::DelEdit(pEdit);
1491
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001492 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001493 sLines.AsStringC() + sBody.AsStringC();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001494
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001495 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001496}
1497
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498void CPDFSDK_Widget::ResetAppearance_TextField(const FX_WCHAR* sValue) {
1499 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001500 CPDF_FormField* pField = pControl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001501 CFX_ByteTextBuf sBody, sLines;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001502
thestig732f6a02016-05-12 10:41:56 -07001503 IFX_Edit* pEdit = IFX_Edit::NewEdit();
1504 pEdit->EnableRefresh(FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001505
thestig732f6a02016-05-12 10:41:56 -07001506 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1507 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001508
thestig732f6a02016-05-12 10:41:56 -07001509 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1510 pEdit->SetFontMap(&font_map);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001511
thestig732f6a02016-05-12 10:41:56 -07001512 CFX_FloatRect rcClient = GetClientRect();
1513 pEdit->SetPlateRect(rcClient);
1514 pEdit->SetAlignmentH(pControl->GetControlAlignment());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001515
thestig732f6a02016-05-12 10:41:56 -07001516 uint32_t dwFieldFlags = pField->GetFieldFlags();
1517 FX_BOOL bMultiLine = (dwFieldFlags >> 12) & 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001518
thestig732f6a02016-05-12 10:41:56 -07001519 if (bMultiLine) {
1520 pEdit->SetMultiLine(TRUE);
1521 pEdit->SetAutoReturn(TRUE);
1522 } else {
1523 pEdit->SetAlignmentV(1);
1524 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001525
thestig732f6a02016-05-12 10:41:56 -07001526 uint16_t subWord = 0;
1527 if ((dwFieldFlags >> 13) & 1) {
1528 subWord = '*';
1529 pEdit->SetPasswordChar(subWord);
1530 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001531
thestig732f6a02016-05-12 10:41:56 -07001532 int nMaxLen = pField->GetMaxLen();
1533 FX_BOOL bCharArray = (dwFieldFlags >> 24) & 1;
1534 FX_FLOAT fFontSize = GetFontSize();
Bo Xufdc00a72014-10-28 23:03:33 -07001535
Tom Sepez51da0932015-11-25 16:05:49 -08001536#ifdef PDF_ENABLE_XFA
thestig732f6a02016-05-12 10:41:56 -07001537 CFX_WideString sValueTmp;
1538 if (!sValue && GetMixXFAWidget()) {
1539 sValueTmp = GetValue(TRUE);
1540 sValue = sValueTmp.c_str();
1541 }
Tom Sepez40e9ff32015-11-30 12:39:54 -08001542#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001543
thestig732f6a02016-05-12 10:41:56 -07001544 if (nMaxLen > 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001545 if (bCharArray) {
thestig732f6a02016-05-12 10:41:56 -07001546 pEdit->SetCharArray(nMaxLen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001547
thestig732f6a02016-05-12 10:41:56 -07001548 if (IsFloatZero(fFontSize)) {
1549 fFontSize = CPWL_Edit::GetCharArrayAutoFontSize(font_map.GetPDFFont(0),
1550 rcClient, nMaxLen);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001551 }
thestig732f6a02016-05-12 10:41:56 -07001552 } else {
1553 if (sValue)
1554 nMaxLen = wcslen((const wchar_t*)sValue);
1555 pEdit->SetLimitChar(nMaxLen);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001556 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001557 }
1558
thestig732f6a02016-05-12 10:41:56 -07001559 if (IsFloatZero(fFontSize))
1560 pEdit->SetAutoFontSize(TRUE);
1561 else
1562 pEdit->SetFontSize(fFontSize);
1563
1564 pEdit->Initialize();
1565
1566 if (sValue)
1567 pEdit->SetText(sValue);
1568 else
1569 pEdit->SetText(pField->GetValue().c_str());
1570
1571 CFX_FloatRect rcContent = pEdit->GetContentRect();
1572
1573 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(
1574 pEdit, CFX_FloatPoint(0.0f, 0.0f), NULL, !bCharArray, subWord);
1575
1576 if (sEdit.GetLength() > 0) {
1577 sBody << "/Tx BMC\n"
1578 << "q\n";
1579 if (rcContent.Width() > rcClient.Width() ||
1580 rcContent.Height() > rcClient.Height()) {
1581 sBody << rcClient.left << " " << rcClient.bottom << " "
1582 << rcClient.Width() << " " << rcClient.Height() << " re\nW\nn\n";
1583 }
1584 CPWL_Color crText = GetTextPWLColor();
1585 sBody << "BT\n"
1586 << CPWL_Utils::GetColorAppStream(crText) << sEdit << "ET\n"
1587 << "Q\nEMC\n";
1588 }
1589
1590 if (bCharArray) {
1591 switch (GetBorderStyle()) {
dsinclair92cb5e52016-05-16 11:38:28 -07001592 case BorderStyle::SOLID: {
thestig732f6a02016-05-12 10:41:56 -07001593 CFX_ByteString sColor =
1594 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1595 if (sColor.GetLength() > 0) {
1596 sLines << "q\n"
1597 << GetBorderWidth() << " w\n"
1598 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1599 << " 2 J 0 j\n";
1600
thestig594b20b2016-05-12 21:56:43 -07001601 for (int32_t i = 1; i < nMaxLen; ++i) {
thestig732f6a02016-05-12 10:41:56 -07001602 sLines << rcClient.left +
1603 ((rcClient.right - rcClient.left) / nMaxLen) * i
1604 << " " << rcClient.bottom << " m\n"
1605 << rcClient.left +
1606 ((rcClient.right - rcClient.left) / nMaxLen) * i
1607 << " " << rcClient.top << " l S\n";
1608 }
1609
1610 sLines << "Q\n";
1611 }
dsinclair92cb5e52016-05-16 11:38:28 -07001612 break;
1613 }
1614 case BorderStyle::DASH: {
thestig732f6a02016-05-12 10:41:56 -07001615 CFX_ByteString sColor =
1616 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1617 if (sColor.GetLength() > 0) {
1618 CPWL_Dash dsBorder = CPWL_Dash(3, 3, 0);
1619
1620 sLines << "q\n"
1621 << GetBorderWidth() << " w\n"
1622 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1623 << "[" << dsBorder.nDash << " " << dsBorder.nGap << "] "
1624 << dsBorder.nPhase << " d\n";
1625
thestig594b20b2016-05-12 21:56:43 -07001626 for (int32_t i = 1; i < nMaxLen; ++i) {
thestig732f6a02016-05-12 10:41:56 -07001627 sLines << rcClient.left +
1628 ((rcClient.right - rcClient.left) / nMaxLen) * i
1629 << " " << rcClient.bottom << " m\n"
1630 << rcClient.left +
1631 ((rcClient.right - rcClient.left) / nMaxLen) * i
1632 << " " << rcClient.top << " l S\n";
1633 }
1634
1635 sLines << "Q\n";
1636 }
dsinclair92cb5e52016-05-16 11:38:28 -07001637 break;
1638 }
1639 default:
1640 break;
thestig732f6a02016-05-12 10:41:56 -07001641 }
1642 }
1643
1644 IFX_Edit::DelEdit(pEdit);
1645
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001646 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001647 sLines.AsStringC() + sBody.AsStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001648 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001649}
1650
Tom Sepez281a9ea2016-02-26 14:24:28 -08001651CFX_FloatRect CPDFSDK_Widget::GetClientRect() const {
1652 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001653 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1654 switch (GetBorderStyle()) {
dsinclair92cb5e52016-05-16 11:38:28 -07001655 case BorderStyle::BEVELED:
1656 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001657 fBorderWidth *= 2.0f;
1658 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001659 default:
1660 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001661 }
1662
1663 return CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001664}
1665
Tom Sepez281a9ea2016-02-26 14:24:28 -08001666CFX_FloatRect CPDFSDK_Widget::GetRotatedRect() const {
1667 CFX_FloatRect rectAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001668 FX_FLOAT fWidth = rectAnnot.right - rectAnnot.left;
1669 FX_FLOAT fHeight = rectAnnot.top - rectAnnot.bottom;
1670
1671 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001672 CFX_FloatRect rcPDFWindow;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001673 switch (abs(pControl->GetRotation() % 360)) {
1674 case 0:
1675 case 180:
1676 default:
Tom Sepez281a9ea2016-02-26 14:24:28 -08001677 rcPDFWindow = CFX_FloatRect(0, 0, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001678 break;
1679 case 90:
1680 case 270:
Tom Sepez281a9ea2016-02-26 14:24:28 -08001681 rcPDFWindow = CFX_FloatRect(0, 0, fHeight, fWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001682 break;
1683 }
1684
1685 return rcPDFWindow;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001686}
1687
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001688CFX_ByteString CPDFSDK_Widget::GetBackgroundAppStream() const {
1689 CPWL_Color crBackground = GetFillPWLColor();
1690 if (crBackground.nColorType != COLORTYPE_TRANSPARENT) {
1691 return CPWL_Utils::GetRectFillAppStream(GetRotatedRect(), crBackground);
1692 }
1693 return "";
Bo Xufdc00a72014-10-28 23:03:33 -07001694}
1695
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001696CFX_ByteString CPDFSDK_Widget::GetBorderAppStream() const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001697 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001698 CPWL_Color crBorder = GetBorderPWLColor();
1699 CPWL_Color crBackground = GetFillPWLColor();
1700 CPWL_Color crLeftTop, crRightBottom;
1701
1702 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001703 CPWL_Dash dsBorder(3, 0, 0);
1704
dsinclair92cb5e52016-05-16 11:38:28 -07001705 BorderStyle nBorderStyle = GetBorderStyle();
1706 switch (nBorderStyle) {
1707 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001708 dsBorder = CPWL_Dash(3, 3, 0);
1709 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001710 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001711 fBorderWidth *= 2;
1712 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1713 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1714 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001715 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001716 fBorderWidth *= 2;
1717 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1718 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1719 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001720 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001721 break;
1722 }
1723
1724 return CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1725 crLeftTop, crRightBottom, nBorderStyle,
1726 dsBorder);
Bo Xufdc00a72014-10-28 23:03:33 -07001727}
1728
Tom Sepez60d909e2015-12-10 15:34:55 -08001729CFX_Matrix CPDFSDK_Widget::GetMatrix() const {
1730 CFX_Matrix mt;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001731 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001732 CFX_FloatRect rcAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001733 FX_FLOAT fWidth = rcAnnot.right - rcAnnot.left;
1734 FX_FLOAT fHeight = rcAnnot.top - rcAnnot.bottom;
1735
1736 switch (abs(pControl->GetRotation() % 360)) {
1737 case 0:
1738 default:
Tom Sepez60d909e2015-12-10 15:34:55 -08001739 mt = CFX_Matrix(1, 0, 0, 1, 0, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001740 break;
1741 case 90:
Tom Sepez60d909e2015-12-10 15:34:55 -08001742 mt = CFX_Matrix(0, 1, -1, 0, fWidth, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001743 break;
1744 case 180:
Tom Sepez60d909e2015-12-10 15:34:55 -08001745 mt = CFX_Matrix(-1, 0, 0, -1, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001746 break;
1747 case 270:
Tom Sepez60d909e2015-12-10 15:34:55 -08001748 mt = CFX_Matrix(0, -1, 1, 0, 0, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001749 break;
1750 }
1751
1752 return mt;
Bo Xufdc00a72014-10-28 23:03:33 -07001753}
1754
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001755CPWL_Color CPDFSDK_Widget::GetTextPWLColor() const {
1756 CPWL_Color crText = CPWL_Color(COLORTYPE_GRAY, 0);
1757
1758 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001759 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
1760 if (da.HasColor()) {
1761 int32_t iColorType;
1762 FX_FLOAT fc[4];
1763 da.GetColor(iColorType, fc);
1764 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1765 }
1766
1767 return crText;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001768}
1769
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001770CPWL_Color CPDFSDK_Widget::GetBorderPWLColor() const {
1771 CPWL_Color crBorder;
1772
1773 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001774 int32_t iColorType;
1775 FX_FLOAT fc[4];
1776 pFormCtrl->GetOriginalBorderColor(iColorType, fc);
1777 if (iColorType > 0)
1778 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1779
1780 return crBorder;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001781}
1782
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001783CPWL_Color CPDFSDK_Widget::GetFillPWLColor() const {
1784 CPWL_Color crFill;
1785
1786 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001787 int32_t iColorType;
1788 FX_FLOAT fc[4];
1789 pFormCtrl->GetOriginalBackgroundColor(iColorType, fc);
1790 if (iColorType > 0)
1791 crFill = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1792
1793 return crFill;
Bo Xufdc00a72014-10-28 23:03:33 -07001794}
1795
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001796void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType,
1797 CPDF_Stream* pImage) {
Tom Sepez33420902015-10-13 15:00:10 -07001798 CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
Lei Zhang96660d62015-12-14 18:27:25 -08001799 ASSERT(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001800
Wei Li9b761132016-01-29 15:44:20 -08001801 CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP");
tsepez7b1ccf92016-04-14 11:04:57 -07001802 CPDF_Stream* pStream = pAPDict->GetStreamBy(sAPType);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001803 CPDF_Dictionary* pStreamDict = pStream->GetDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001804 CFX_ByteString sImageAlias = "IMG";
1805
1806 if (CPDF_Dictionary* pImageDict = pImage->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -08001807 sImageAlias = pImageDict->GetStringBy("Name");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001808 if (sImageAlias.IsEmpty())
1809 sImageAlias = "IMG";
1810 }
1811
Wei Li9b761132016-01-29 15:44:20 -08001812 CPDF_Dictionary* pStreamResList = pStreamDict->GetDictBy("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001813 if (!pStreamResList) {
Tom Sepezae51c812015-08-05 12:34:06 -07001814 pStreamResList = new CPDF_Dictionary();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001815 pStreamDict->SetAt("Resources", pStreamResList);
1816 }
1817
1818 if (pStreamResList) {
Tom Sepezae51c812015-08-05 12:34:06 -07001819 CPDF_Dictionary* pXObject = new CPDF_Dictionary;
tsepez7b1ccf92016-04-14 11:04:57 -07001820 pXObject->SetAtReference(sImageAlias, pDoc, pImage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001821 pStreamResList->SetAt("XObject", pXObject);
1822 }
Bo Xufdc00a72014-10-28 23:03:33 -07001823}
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001824
1825void CPDFSDK_Widget::RemoveAppearance(const CFX_ByteString& sAPType) {
tsepez7b1ccf92016-04-14 11:04:57 -07001826 if (CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP"))
1827 pAPDict->RemoveAt(sAPType);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001828}
1829
1830FX_BOOL CPDFSDK_Widget::OnAAction(CPDF_AAction::AActionType type,
1831 PDFSDK_FieldAction& data,
1832 CPDFSDK_PageView* pPageView) {
1833 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001834 CPDFDoc_Environment* pEnv = pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001835
Tom Sepez40e9ff32015-11-30 12:39:54 -08001836#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -08001837 CPDFXFA_Document* pDoc = pDocument->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -07001838 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001839 XFA_EVENTTYPE eEventType = GetXFAEventType(type, data.bWillCommit);
1840
1841 if (eEventType != XFA_EVENT_Unknown) {
dsinclairdf4bc592016-03-31 20:34:43 -07001842 if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001843 CXFA_EventParam param;
1844 param.m_eType = eEventType;
1845 param.m_wsChange = data.sChange;
1846 param.m_iCommitKey = data.nCommitKey;
1847 param.m_bShift = data.bShift;
1848 param.m_iSelStart = data.nSelStart;
1849 param.m_iSelEnd = data.nSelEnd;
1850 param.m_wsFullText = data.sValue;
1851 param.m_bKeyDown = data.bKeyDown;
1852 param.m_bModifier = data.bModifier;
1853 param.m_wsNewText = data.sValue;
1854 if (data.nSelEnd > data.nSelStart)
1855 param.m_wsNewText.Delete(data.nSelStart,
1856 data.nSelEnd - data.nSelStart);
1857 for (int i = data.sChange.GetLength() - 1; i >= 0; i--)
1858 param.m_wsNewText.Insert(data.nSelStart, data.sChange[i]);
1859 param.m_wsPrevText = data.sValue;
1860
dsinclair221caf62016-04-04 12:08:40 -07001861 CXFA_WidgetAcc* pAcc = hWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001862 param.m_pTarget = pAcc;
1863 int32_t nRet = pXFAWidgetHandler->ProcessEvent(pAcc, &param);
1864
dsinclairdf4bc592016-03-31 20:34:43 -07001865 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001866 pDocView->UpdateDocView();
1867 }
1868
Wei Lie98ac2e2016-03-18 15:43:04 -07001869 if (nRet == XFA_EVENTERROR_Success)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001870 return TRUE;
1871 }
1872 }
1873 }
Tom Sepez40e9ff32015-11-30 12:39:54 -08001874#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001875
1876 CPDF_Action action = GetAAction(type);
Wei Li0fc6b252016-03-01 16:29:41 -08001877 if (action.GetDict() && action.GetType() != CPDF_Action::Unknown) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001878 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
1879 return pActionHandler->DoAction_Field(action, type, pDocument,
1880 GetFormField(), data);
1881 }
1882 return FALSE;
1883}
1884
1885CPDF_Action CPDFSDK_Widget::GetAAction(CPDF_AAction::AActionType eAAT) {
1886 switch (eAAT) {
1887 case CPDF_AAction::CursorEnter:
1888 case CPDF_AAction::CursorExit:
1889 case CPDF_AAction::ButtonDown:
1890 case CPDF_AAction::ButtonUp:
1891 case CPDF_AAction::GetFocus:
1892 case CPDF_AAction::LoseFocus:
1893 case CPDF_AAction::PageOpen:
1894 case CPDF_AAction::PageClose:
1895 case CPDF_AAction::PageVisible:
1896 case CPDF_AAction::PageInvisible:
1897 return CPDFSDK_BAAnnot::GetAAction(eAAT);
1898
1899 case CPDF_AAction::KeyStroke:
1900 case CPDF_AAction::Format:
1901 case CPDF_AAction::Validate:
1902 case CPDF_AAction::Calculate: {
1903 CPDF_FormField* pField = GetFormField();
Wei Li0fc6b252016-03-01 16:29:41 -08001904 if (pField->GetAdditionalAction().GetDict())
1905 return pField->GetAdditionalAction().GetAction(eAAT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001906 return CPDFSDK_BAAnnot::GetAAction(eAAT);
1907 }
1908 default:
1909 break;
1910 }
1911
1912 return CPDF_Action();
1913}
1914
1915CFX_WideString CPDFSDK_Widget::GetAlternateName() const {
1916 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001917 return pFormField->GetAlternateName();
1918}
1919
1920int32_t CPDFSDK_Widget::GetAppearanceAge() const {
1921 return m_nAppAge;
1922}
1923
1924int32_t CPDFSDK_Widget::GetValueAge() const {
1925 return m_nValueAge;
1926}
1927
1928FX_BOOL CPDFSDK_Widget::HitTest(FX_FLOAT pageX, FX_FLOAT pageY) {
1929 CPDF_Annot* pAnnot = GetPDFAnnot();
1930 CFX_FloatRect annotRect;
1931 pAnnot->GetRect(annotRect);
1932 if (annotRect.Contains(pageX, pageY)) {
1933 if (!IsVisible())
1934 return FALSE;
1935
1936 int nFieldFlags = GetFieldFlags();
1937 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
1938 return FALSE;
1939
1940 return TRUE;
1941 }
1942 return FALSE;
1943}
1944
Tom Sepez51da0932015-11-25 16:05:49 -08001945#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -07001946CPDFSDK_XFAWidget::CPDFSDK_XFAWidget(CXFA_FFWidget* pAnnot,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001947 CPDFSDK_PageView* pPageView,
1948 CPDFSDK_InterForm* pInterForm)
Dan Sinclairf766ad22016-03-14 13:51:24 -04001949 : CPDFSDK_Annot(pPageView),
1950 m_pInterForm(pInterForm),
1951 m_hXFAWidget(pAnnot) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001952
1953FX_BOOL CPDFSDK_XFAWidget::IsXFAField() {
1954 return TRUE;
1955}
1956
1957CFX_ByteString CPDFSDK_XFAWidget::GetType() const {
1958 return FSDK_XFAWIDGET_TYPENAME;
1959}
1960
Tom Sepez3343d142015-11-02 09:54:54 -08001961CFX_FloatRect CPDFSDK_XFAWidget::GetRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001962 CFX_RectF rcBBox;
dsinclair221caf62016-04-04 12:08:40 -07001963 GetXFAWidget()->GetRect(rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001964 return CFX_FloatRect(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
1965 rcBBox.top + rcBBox.height);
1966}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001967#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001968
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001969CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_Document* pDocument)
1970 : m_pDocument(pDocument),
thestigdb1a24e2016-05-23 16:55:09 -07001971 m_pInterForm(new CPDF_InterForm(m_pDocument->GetPDFDocument())),
Tom Sepez51da0932015-11-25 16:05:49 -08001972#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001973 m_bXfaCalculate(TRUE),
1974 m_bXfaValidationsEnabled(TRUE),
Tom Sepez40e9ff32015-11-30 12:39:54 -08001975#endif // PDF_ENABLE_XFA
1976 m_bCalculate(TRUE),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001977 m_bBusy(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001978 m_pInterForm->SetFormNotify(this);
Tom Sepezae7a9172015-11-23 09:22:46 -08001979 for (int i = 0; i < kNumFieldTypes; ++i)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001980 m_bNeedHightlight[i] = FALSE;
1981 m_iHighlightAlpha = 0;
1982}
1983
1984CPDFSDK_InterForm::~CPDFSDK_InterForm() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001985 m_Map.clear();
Tom Sepez51da0932015-11-25 16:05:49 -08001986#ifdef PDF_ENABLE_XFA
tsepez16a20332016-04-22 14:00:47 -07001987 m_XFAMap.clear();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001988#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001989}
1990
1991FX_BOOL CPDFSDK_InterForm::HighlightWidgets() {
1992 return FALSE;
1993}
1994
1995CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget,
1996 FX_BOOL bNext) const {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08001997 std::unique_ptr<CBA_AnnotIterator> pIterator(
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001998 new CBA_AnnotIterator(pWidget->GetPageView(), "Widget", ""));
1999
2000 if (bNext) {
2001 return (CPDFSDK_Widget*)pIterator->GetNextAnnot(pWidget);
2002 }
2003 return (CPDFSDK_Widget*)pIterator->GetPrevAnnot(pWidget);
2004}
2005
2006CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const {
2007 if (!pControl || !m_pInterForm)
2008 return nullptr;
2009
2010 CPDFSDK_Widget* pWidget = nullptr;
2011 const auto it = m_Map.find(pControl);
2012 if (it != m_Map.end())
2013 pWidget = it->second;
2014
2015 if (pWidget)
2016 return pWidget;
2017
2018 CPDF_Dictionary* pControlDict = pControl->GetWidget();
Tom Sepez50d12ad2015-11-24 09:50:51 -08002019 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002020 CPDFSDK_PageView* pPage = nullptr;
2021
Wei Li9b761132016-01-29 15:44:20 -08002022 if (CPDF_Dictionary* pPageDict = pControlDict->GetDictBy("P")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002023 int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
2024 if (nPageIndex >= 0) {
2025 pPage = m_pDocument->GetPageView(nPageIndex);
2026 }
2027 }
2028
2029 if (!pPage) {
2030 int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict);
2031 if (nPageIndex >= 0) {
2032 pPage = m_pDocument->GetPageView(nPageIndex);
2033 }
2034 }
2035
2036 if (!pPage)
2037 return nullptr;
2038 return (CPDFSDK_Widget*)pPage->GetAnnotByDict(pControlDict);
2039}
2040
Lei Zhangd88a3642015-11-10 09:38:57 -08002041void CPDFSDK_InterForm::GetWidgets(
2042 const CFX_WideString& sFieldName,
2043 std::vector<CPDFSDK_Widget*>* widgets) const {
2044 for (int i = 0, sz = m_pInterForm->CountFields(sFieldName); i < sz; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002045 CPDF_FormField* pFormField = m_pInterForm->GetField(i, sFieldName);
Lei Zhangd88a3642015-11-10 09:38:57 -08002046 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002047 GetWidgets(pFormField, widgets);
2048 }
2049}
2050
Lei Zhangd88a3642015-11-10 09:38:57 -08002051void CPDFSDK_InterForm::GetWidgets(
2052 CPDF_FormField* pField,
2053 std::vector<CPDFSDK_Widget*>* widgets) const {
2054 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002055 CPDF_FormControl* pFormCtrl = pField->GetControl(i);
Lei Zhangd88a3642015-11-10 09:38:57 -08002056 ASSERT(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002057 CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002058 if (pWidget)
Lei Zhangd88a3642015-11-10 09:38:57 -08002059 widgets->push_back(pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002060 }
2061}
2062
2063int CPDFSDK_InterForm::GetPageIndexByAnnotDict(
2064 CPDF_Document* pDocument,
2065 CPDF_Dictionary* pAnnotDict) const {
Lei Zhang96660d62015-12-14 18:27:25 -08002066 ASSERT(pAnnotDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002067
2068 for (int i = 0, sz = pDocument->GetPageCount(); i < sz; i++) {
2069 if (CPDF_Dictionary* pPageDict = pDocument->GetPage(i)) {
Wei Li9b761132016-01-29 15:44:20 -08002070 if (CPDF_Array* pAnnots = pPageDict->GetArrayBy("Annots")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002071 for (int j = 0, jsz = pAnnots->GetCount(); j < jsz; j++) {
tsepezbd567552016-03-29 14:51:50 -07002072 CPDF_Object* pDict = pAnnots->GetDirectObjectAt(j);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002073 if (pAnnotDict == pDict) {
2074 return i;
2075 }
2076 }
2077 }
2078 }
2079 }
2080
2081 return -1;
2082}
2083
2084void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl,
2085 CPDFSDK_Widget* pWidget) {
2086 m_Map[pControl] = pWidget;
2087}
2088
2089void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl) {
2090 m_Map.erase(pControl);
2091}
2092
Tom Sepez40e9ff32015-11-30 12:39:54 -08002093void CPDFSDK_InterForm::EnableCalculate(FX_BOOL bEnabled) {
2094 m_bCalculate = bEnabled;
2095}
2096
2097FX_BOOL CPDFSDK_InterForm::IsCalculateEnabled() const {
2098 return m_bCalculate;
2099}
2100
Tom Sepez51da0932015-11-25 16:05:49 -08002101#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -07002102void CPDFSDK_InterForm::AddXFAMap(CXFA_FFWidget* hWidget,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002103 CPDFSDK_XFAWidget* pWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002104 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002105 m_XFAMap[hWidget] = pWidget;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002106}
2107
dsinclairdf4bc592016-03-31 20:34:43 -07002108void CPDFSDK_InterForm::RemoveXFAMap(CXFA_FFWidget* hWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002109 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002110 m_XFAMap.erase(hWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002111}
2112
dsinclairdf4bc592016-03-31 20:34:43 -07002113CPDFSDK_XFAWidget* CPDFSDK_InterForm::GetXFAWidget(CXFA_FFWidget* hWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002114 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002115 auto it = m_XFAMap.find(hWidget);
2116 return it != m_XFAMap.end() ? it->second : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002117}
2118
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002119void CPDFSDK_InterForm::XfaEnableCalculate(FX_BOOL bEnabled) {
2120 m_bXfaCalculate = bEnabled;
2121}
2122FX_BOOL CPDFSDK_InterForm::IsXfaCalculateEnabled() const {
2123 return m_bXfaCalculate;
2124}
2125
2126FX_BOOL CPDFSDK_InterForm::IsXfaValidationsEnabled() {
2127 return m_bXfaValidationsEnabled;
2128}
2129void CPDFSDK_InterForm::XfaSetValidationsEnabled(FX_BOOL bEnabled) {
2130 m_bXfaValidationsEnabled = bEnabled;
Bo Xufdc00a72014-10-28 23:03:33 -07002131}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002132#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -07002133
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002134void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002135 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2136 ASSERT(pEnv);
2137 if (!pEnv->IsJSInitiated())
2138 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002139
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002140 if (m_bBusy)
2141 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002142
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002143 m_bBusy = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002144
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002145 if (IsCalculateEnabled()) {
Tom Sepezba038bc2015-10-08 12:03:00 -07002146 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002147 pRuntime->SetReaderDocument(m_pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002148
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002149 int nSize = m_pInterForm->CountFieldsInCalculationOrder();
2150 for (int i = 0; i < nSize; i++) {
2151 if (CPDF_FormField* pField =
2152 m_pInterForm->GetFieldInCalculationOrder(i)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002153 int nType = pField->GetFieldType();
2154 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2155 CPDF_AAction aAction = pField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002156 if (aAction.GetDict() &&
2157 aAction.ActionExist(CPDF_AAction::Calculate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002158 CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate);
Wei Li0fc6b252016-03-01 16:29:41 -08002159 if (action.GetDict()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002160 CFX_WideString csJS = action.GetJavaScript();
2161 if (!csJS.IsEmpty()) {
Tom Sepezba038bc2015-10-08 12:03:00 -07002162 IJS_Context* pContext = pRuntime->NewContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002163 CFX_WideString sOldValue = pField->GetValue();
2164 CFX_WideString sValue = sOldValue;
2165 FX_BOOL bRC = TRUE;
2166 pContext->OnField_Calculate(pFormField, pField, sValue, bRC);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002167
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002168 CFX_WideString sInfo;
Tom Sepez33420902015-10-13 15:00:10 -07002169 FX_BOOL bRet = pContext->RunScript(csJS, &sInfo);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002170 pRuntime->ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002171
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002172 if (bRet) {
2173 if (bRC) {
2174 if (sValue.Compare(sOldValue) != 0)
2175 pField->SetValue(sValue, TRUE);
2176 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002177 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002178 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002179 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002180 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002181 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002182 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002183 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002184 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002185
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002186 m_bBusy = FALSE;
2187}
2188
2189CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField,
2190 FX_BOOL& bFormated) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002191 CFX_WideString sValue = pFormField->GetValue();
2192 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2193 ASSERT(pEnv);
2194 if (!pEnv->IsJSInitiated()) {
2195 bFormated = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002196 return sValue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002197 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002198
Tom Sepezba038bc2015-10-08 12:03:00 -07002199 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002200 pRuntime->SetReaderDocument(m_pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002201
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002202 if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX) {
2203 if (pFormField->CountSelectedItems() > 0) {
2204 int index = pFormField->GetSelectedIndex(0);
2205 if (index >= 0)
2206 sValue = pFormField->GetOptionLabel(index);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002207 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002208 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002209
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002210 bFormated = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002211
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002212 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002213 if (aAction.GetDict() && aAction.ActionExist(CPDF_AAction::Format)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002214 CPDF_Action action = aAction.GetAction(CPDF_AAction::Format);
Wei Li0fc6b252016-03-01 16:29:41 -08002215 if (action.GetDict()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002216 CFX_WideString script = action.GetJavaScript();
2217 if (!script.IsEmpty()) {
2218 CFX_WideString Value = sValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002219
Tom Sepezba038bc2015-10-08 12:03:00 -07002220 IJS_Context* pContext = pRuntime->NewContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002221 pContext->OnField_Format(pFormField, Value, TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002222
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002223 CFX_WideString sInfo;
Tom Sepez33420902015-10-13 15:00:10 -07002224 FX_BOOL bRet = pContext->RunScript(script, &sInfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002225 pRuntime->ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002226
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002227 if (bRet) {
2228 sValue = Value;
2229 bFormated = TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002230 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002231 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002232 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002233 }
2234
2235 return sValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002236}
2237
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002238void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField,
2239 const FX_WCHAR* sValue,
2240 FX_BOOL bValueChanged) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002241 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2242 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang96660d62015-12-14 18:27:25 -08002243 ASSERT(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002244 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
2245 pWidget->ResetAppearance(sValue, bValueChanged);
2246 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002247}
2248
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002249void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002250 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2251 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang96660d62015-12-14 18:27:25 -08002252 ASSERT(pFormCtrl);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002253
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002254 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
2255 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2256 CFFL_IFormFiller* pIFormFiller = pEnv->GetIFormFiller();
Tom Sepez540c4362015-11-24 13:33:57 -08002257 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002258 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002259 FX_RECT rcBBox = pIFormFiller->GetViewBBox(pPageView, pWidget);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002260
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002261 pEnv->FFI_Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right,
2262 rcBBox.bottom);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002263 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002264 }
2265}
2266
Tom Sepezd6262c12016-02-03 14:47:06 -08002267FX_BOOL CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField,
2268 const CFX_WideString& csValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002269 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002270 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::KeyStroke))
Tom Sepezd6262c12016-02-03 14:47:06 -08002271 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002272
Tom Sepezd6262c12016-02-03 14:47:06 -08002273 CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke);
Wei Li0fc6b252016-03-01 16:29:41 -08002274 if (!action.GetDict())
Tom Sepezd6262c12016-02-03 14:47:06 -08002275 return TRUE;
2276
2277 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2278 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2279 PDFSDK_FieldAction fa;
2280 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2281 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2282 fa.sValue = csValue;
2283 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::KeyStroke,
2284 m_pDocument, pFormField, fa);
2285 return fa.bRC;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002286}
2287
Tom Sepezd6262c12016-02-03 14:47:06 -08002288FX_BOOL CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField,
2289 const CFX_WideString& csValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002290 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002291 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Validate))
Tom Sepezd6262c12016-02-03 14:47:06 -08002292 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002293
Tom Sepezd6262c12016-02-03 14:47:06 -08002294 CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate);
Wei Li0fc6b252016-03-01 16:29:41 -08002295 if (!action.GetDict())
Tom Sepezd6262c12016-02-03 14:47:06 -08002296 return TRUE;
2297
2298 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2299 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2300 PDFSDK_FieldAction fa;
2301 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2302 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2303 fa.sValue = csValue;
2304 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::Validate,
2305 m_pDocument, pFormField, fa);
2306 return fa.bRC;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002307}
2308
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002309FX_BOOL CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action) {
Wei Li0fc6b252016-03-01 16:29:41 -08002310 ASSERT(action.GetDict());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002311
Wei Li0fc6b252016-03-01 16:29:41 -08002312 CPDF_ActionFields af(&action);
Lei Zhangd88a3642015-11-10 09:38:57 -08002313 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2314 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002315
thestigded36342016-05-23 17:54:02 -07002316 bool bHide = action.GetHideStatus();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002317 FX_BOOL bChanged = FALSE;
2318
Lei Zhangd88a3642015-11-10 09:38:57 -08002319 for (CPDF_FormField* pField : fields) {
2320 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
2321 CPDF_FormControl* pControl = pField->GetControl(i);
2322 ASSERT(pControl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002323
2324 if (CPDFSDK_Widget* pWidget = GetWidget(pControl)) {
tsepezc3255f52016-03-25 14:52:27 -07002325 uint32_t nFlags = pWidget->GetFlags();
Lei Zhangd88a3642015-11-10 09:38:57 -08002326 nFlags &= ~ANNOTFLAG_INVISIBLE;
2327 nFlags &= ~ANNOTFLAG_NOVIEW;
2328 if (bHide)
2329 nFlags |= ANNOTFLAG_HIDDEN;
2330 else
2331 nFlags &= ~ANNOTFLAG_HIDDEN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002332 pWidget->SetFlags(nFlags);
Lei Zhangd88a3642015-11-10 09:38:57 -08002333 pWidget->GetPageView()->UpdateView(pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002334 bChanged = TRUE;
2335 }
2336 }
2337 }
2338
2339 return bChanged;
2340}
2341
2342FX_BOOL CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002343 CFX_WideString sDestination = action.GetFilePath();
2344 if (sDestination.IsEmpty())
2345 return FALSE;
2346
2347 CPDF_Dictionary* pActionDict = action.GetDict();
2348 if (pActionDict->KeyExist("Fields")) {
Wei Li0fc6b252016-03-01 16:29:41 -08002349 CPDF_ActionFields af(&action);
tsepezc3255f52016-03-25 14:52:27 -07002350 uint32_t dwFlags = action.GetFlags();
Lei Zhangd88a3642015-11-10 09:38:57 -08002351 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2352 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
2353 if (!fields.empty()) {
2354 bool bIncludeOrExclude = !(dwFlags & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002355 if (m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002356 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002357
Wei Li97da9762016-03-11 17:00:48 -08002358 return SubmitFields(sDestination, fields, bIncludeOrExclude, false);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002359 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002360 }
Lei Zhangd88a3642015-11-10 09:38:57 -08002361 if (m_pInterForm->CheckRequiredFields(nullptr, true))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002362 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002363
2364 return SubmitForm(sDestination, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002365}
2366
Lei Zhangd88a3642015-11-10 09:38:57 -08002367FX_BOOL CPDFSDK_InterForm::SubmitFields(
2368 const CFX_WideString& csDestination,
2369 const std::vector<CPDF_FormField*>& fields,
Wei Li97da9762016-03-11 17:00:48 -08002370 bool bIncludeOrExclude,
2371 bool bUrlEncoded) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002372 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002373
2374 CFX_ByteTextBuf textBuf;
2375 ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf);
2376
2377 uint8_t* pBuffer = textBuf.GetBuffer();
2378 FX_STRSIZE nBufSize = textBuf.GetLength();
2379
Lei Zhangd88a3642015-11-10 09:38:57 -08002380 if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize))
2381 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002382
2383 pEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002384 return TRUE;
2385}
2386
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002387FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile,
2388 CFX_WideString csTxtFile) {
2389 return TRUE;
2390}
2391
2392FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf,
2393 FX_STRSIZE& nBufSize) {
2394 CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize);
2395 if (pFDF) {
Wei Li9b761132016-01-29 15:44:20 -08002396 CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictBy("FDF");
Lei Zhang997de612015-11-04 18:17:53 -08002397 if (!pMainDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002398 return FALSE;
2399
2400 // Get fields
Wei Li9b761132016-01-29 15:44:20 -08002401 CPDF_Array* pFields = pMainDict->GetArrayBy("Fields");
Lei Zhang997de612015-11-04 18:17:53 -08002402 if (!pFields)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002403 return FALSE;
2404
2405 CFX_ByteTextBuf fdfEncodedData;
tsepezc3255f52016-03-25 14:52:27 -07002406 for (uint32_t i = 0; i < pFields->GetCount(); i++) {
Wei Li9b761132016-01-29 15:44:20 -08002407 CPDF_Dictionary* pField = pFields->GetDictAt(i);
Lei Zhang997de612015-11-04 18:17:53 -08002408 if (!pField)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002409 continue;
2410 CFX_WideString name;
Wei Li9b761132016-01-29 15:44:20 -08002411 name = pField->GetUnicodeTextBy("T");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002412 CFX_ByteString name_b = CFX_ByteString::FromUnicode(name);
Wei Li9b761132016-01-29 15:44:20 -08002413 CFX_ByteString csBValue = pField->GetStringBy("V");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002414 CFX_WideString csWValue = PDF_DecodeText(csBValue);
2415 CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue);
2416
Tom Sepez052a8d92016-02-19 14:41:46 -08002417 fdfEncodedData << name_b.GetBuffer(name_b.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002418 name_b.ReleaseBuffer();
Tom Sepez052a8d92016-02-19 14:41:46 -08002419 fdfEncodedData << "=";
2420 fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002421 csValue_b.ReleaseBuffer();
2422 if (i != pFields->GetCount() - 1)
Tom Sepez052a8d92016-02-19 14:41:46 -08002423 fdfEncodedData << "&";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002424 }
2425
2426 nBufSize = fdfEncodedData.GetLength();
2427 pBuf = FX_Alloc(uint8_t, nBufSize);
2428 FXSYS_memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize);
2429 }
2430 return TRUE;
2431}
2432
Lei Zhangd88a3642015-11-10 09:38:57 -08002433FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(
2434 const std::vector<CPDF_FormField*>& fields,
Wei Li97da9762016-03-11 17:00:48 -08002435 bool bIncludeOrExclude,
Lei Zhangd88a3642015-11-10 09:38:57 -08002436 CFX_ByteTextBuf& textBuf) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08002437 std::unique_ptr<CFDF_Document> pFDF(m_pInterForm->ExportToFDF(
tsepez4c3debb2016-04-08 12:20:38 -07002438 m_pDocument->GetPath().AsStringC(), fields, bIncludeOrExclude));
Lei Zhangd88a3642015-11-10 09:38:57 -08002439 return pFDF ? pFDF->WriteBuf(textBuf) : FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002440}
2441
Tom Sepez51da0932015-11-25 16:05:49 -08002442#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002443void CPDFSDK_InterForm::SynchronizeField(CPDF_FormField* pFormField,
2444 FX_BOOL bSynchronizeElse) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002445 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2446 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002447 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
2448 pWidget->Synchronize(bSynchronizeElse);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002449 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002450 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002451}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002452#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002453
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002454CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName(
2455 const CFX_WideString& sFileExt) {
2456 CFX_WideString sFileName;
2457 return L"";
2458}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002459
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002460FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination,
2461 FX_BOOL bUrlEncoded) {
2462 if (sDestination.IsEmpty())
2463 return FALSE;
2464
Lei Zhang96660d62015-12-14 18:27:25 -08002465 if (!m_pDocument || !m_pInterForm)
2466 return FALSE;
2467
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002468 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002469 CFX_WideString wsPDFFilePath = m_pDocument->GetPath();
tsepez4c3debb2016-04-08 12:20:38 -07002470 CFDF_Document* pFDFDoc = m_pInterForm->ExportToFDF(wsPDFFilePath.AsStringC());
Lei Zhang997de612015-11-04 18:17:53 -08002471 if (!pFDFDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002472 return FALSE;
2473
2474 CFX_ByteTextBuf FdfBuffer;
2475 FX_BOOL bRet = pFDFDoc->WriteBuf(FdfBuffer);
2476 delete pFDFDoc;
2477 if (!bRet)
2478 return FALSE;
2479
2480 uint8_t* pBuffer = FdfBuffer.GetBuffer();
2481 FX_STRSIZE nBufSize = FdfBuffer.GetLength();
2482
2483 if (bUrlEncoded) {
2484 if (!FDFToURLEncodedData(pBuffer, nBufSize))
2485 return FALSE;
2486 }
2487
2488 pEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
2489
Lei Zhangda180e92015-08-14 22:22:13 -07002490 if (bUrlEncoded) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002491 FX_Free(pBuffer);
2492 pBuffer = NULL;
2493 }
2494
2495 return TRUE;
2496}
2497
2498FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) {
tsepezfc58ad12016-04-05 12:22:15 -07002499 CFDF_Document* pFDF =
tsepez4c3debb2016-04-08 12:20:38 -07002500 m_pInterForm->ExportToFDF(m_pDocument->GetPath().AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002501 if (!pFDF)
2502 return FALSE;
2503
2504 FX_BOOL bRet = pFDF->WriteBuf(textBuf);
2505 delete pFDF;
2506
2507 return bRet;
2508}
2509
2510FX_BOOL CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) {
Wei Li0fc6b252016-03-01 16:29:41 -08002511 ASSERT(action.GetDict());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002512
2513 CPDF_Dictionary* pActionDict = action.GetDict();
Lei Zhangd88a3642015-11-10 09:38:57 -08002514 if (!pActionDict->KeyExist("Fields"))
2515 return m_pInterForm->ResetForm(true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002516
Wei Li0fc6b252016-03-01 16:29:41 -08002517 CPDF_ActionFields af(&action);
tsepezc3255f52016-03-25 14:52:27 -07002518 uint32_t dwFlags = action.GetFlags();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002519
Lei Zhangd88a3642015-11-10 09:38:57 -08002520 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2521 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
2522 return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002523}
2524
2525FX_BOOL CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action) {
2526 return FALSE;
2527}
2528
Lei Zhangd88a3642015-11-10 09:38:57 -08002529std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects(
2530 const std::vector<CPDF_Object*>& objects) const {
2531 std::vector<CPDF_FormField*> fields;
2532 for (CPDF_Object* pObject : objects) {
2533 if (pObject && pObject->IsString()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002534 CFX_WideString csName = pObject->GetUnicodeText();
2535 CPDF_FormField* pField = m_pInterForm->GetField(0, csName);
Dan Sinclaird43ebdd2015-10-27 15:37:54 -04002536 if (pField)
Lei Zhangd88a3642015-11-10 09:38:57 -08002537 fields.push_back(pField);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002538 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002539 }
Lei Zhangd88a3642015-11-10 09:38:57 -08002540 return fields;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002541}
2542
Tom Sepezd6262c12016-02-03 14:47:06 -08002543int CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField,
2544 const CFX_WideString& csValue) {
2545 int nType = pField->GetFieldType();
2546 if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002547 return 0;
2548
Tom Sepezd6262c12016-02-03 14:47:06 -08002549 if (!OnKeyStrokeCommit(pField, csValue))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002550 return -1;
2551
Tom Sepezd6262c12016-02-03 14:47:06 -08002552 if (!OnValidate(pField, csValue))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002553 return -1;
2554
2555 return 1;
2556}
2557
Tom Sepezd6262c12016-02-03 14:47:06 -08002558void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) {
2559#ifdef PDF_ENABLE_XFA
2560 SynchronizeField(pField, FALSE);
2561#endif // PDF_ENABLE_XFA
2562 int nType = pField->GetFieldType();
2563 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2564 OnCalculate(pField);
2565 FX_BOOL bFormated = FALSE;
2566 CFX_WideString sValue = OnFormat(pField, bFormated);
2567 ResetFieldAppearance(pField, bFormated ? sValue.c_str() : nullptr, TRUE);
2568 UpdateField(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002569 }
Tom Sepezd6262c12016-02-03 14:47:06 -08002570}
2571
2572int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField,
2573 const CFX_WideString& csValue) {
2574 if (pField->GetFieldType() != FIELDTYPE_LISTBOX)
2575 return 0;
2576
2577 if (!OnKeyStrokeCommit(pField, csValue))
2578 return -1;
2579
2580 if (!OnValidate(pField, csValue))
2581 return -1;
2582
2583 return 1;
2584}
2585
2586void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) {
2587 if (pField->GetFieldType() == FIELDTYPE_LISTBOX) {
2588 OnCalculate(pField);
2589 ResetFieldAppearance(pField, NULL, TRUE);
2590 UpdateField(pField);
2591 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002592}
2593
Tom Sepezed5d7aa2016-02-02 16:02:03 -08002594void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) {
2595 int nType = pField->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002596 if (nType == FIELDTYPE_CHECKBOX || nType == FIELDTYPE_RADIOBUTTON) {
Tom Sepezed5d7aa2016-02-02 16:02:03 -08002597 OnCalculate(pField);
2598 UpdateField(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002599 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002600}
2601
Tom Sepezd6262c12016-02-03 14:47:06 -08002602int CPDFSDK_InterForm::BeforeFormReset(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002603 return 0;
2604}
2605
Tom Sepezd6262c12016-02-03 14:47:06 -08002606void CPDFSDK_InterForm::AfterFormReset(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002607 OnCalculate(nullptr);
Tom Sepezd6262c12016-02-03 14:47:06 -08002608}
2609
2610int CPDFSDK_InterForm::BeforeFormImportData(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002611 return 0;
2612}
2613
Tom Sepezd6262c12016-02-03 14:47:06 -08002614void CPDFSDK_InterForm::AfterFormImportData(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002615 OnCalculate(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002616}
2617
2618FX_BOOL CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002619 if (nFieldType < 1 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002620 return FALSE;
2621 return m_bNeedHightlight[nFieldType - 1];
2622}
2623
2624void CPDFSDK_InterForm::RemoveAllHighLight() {
Tom Sepezae7a9172015-11-23 09:22:46 -08002625 for (int i = 0; i < kNumFieldTypes; ++i)
2626 m_bNeedHightlight[i] = FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002627}
Tom Sepezae7a9172015-11-23 09:22:46 -08002628
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002629void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002630 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002631 return;
2632 switch (nFieldType) {
2633 case 0: {
Tom Sepezae7a9172015-11-23 09:22:46 -08002634 for (int i = 0; i < kNumFieldTypes; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002635 m_aHighlightColor[i] = clr;
2636 m_bNeedHightlight[i] = TRUE;
2637 }
2638 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002639 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002640 default: {
2641 m_aHighlightColor[nFieldType - 1] = clr;
2642 m_bNeedHightlight[nFieldType - 1] = TRUE;
2643 break;
2644 }
2645 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002646}
2647
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002648FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002649 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002650 return FXSYS_RGB(255, 255, 255);
2651 if (nFieldType == 0)
2652 return m_aHighlightColor[0];
2653 return m_aHighlightColor[nFieldType - 1];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002654}
2655
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002656CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView,
2657 const CFX_ByteString& sType,
2658 const CFX_ByteString& sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002659 : m_eTabOrder(STRUCTURE),
2660 m_pPageView(pPageView),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002661 m_sType(sType),
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002662 m_sSubType(sSubType) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002663 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage();
Wei Li9b761132016-01-29 15:44:20 -08002664 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringBy("Tabs");
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002665 if (sTabs == "R")
2666 m_eTabOrder = ROW;
2667 else if (sTabs == "C")
2668 m_eTabOrder = COLUMN;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002669
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002670 GenerateResults();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002671}
2672
Dan Sinclairf766ad22016-03-14 13:51:24 -04002673CBA_AnnotIterator::~CBA_AnnotIterator() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002674
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002675CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002676 return m_Annots.empty() ? nullptr : m_Annots.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002677}
2678
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002679CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002680 return m_Annots.empty() ? nullptr : m_Annots.back();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002681}
2682
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002683CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002684 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2685 if (iter == m_Annots.end())
2686 return nullptr;
2687 ++iter;
2688 if (iter == m_Annots.end())
2689 iter = m_Annots.begin();
2690 return *iter;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002691}
2692
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002693CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002694 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2695 if (iter == m_Annots.end())
2696 return nullptr;
2697 if (iter == m_Annots.begin())
2698 iter = m_Annots.end();
2699 return *(--iter);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002700}
2701
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002702// static
2703bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1,
2704 const CPDFSDK_Annot* p2) {
2705 return GetAnnotRect(p1).left < GetAnnotRect(p2).left;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002706}
2707
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002708// static
2709bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1,
2710 const CPDFSDK_Annot* p2) {
2711 return GetAnnotRect(p1).top > GetAnnotRect(p2).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002712}
2713
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002714void CBA_AnnotIterator::GenerateResults() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002715 switch (m_eTabOrder) {
2716 case STRUCTURE: {
Lei Zhangbf60b292015-10-26 12:14:35 -07002717 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002718 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002719 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002720 m_Annots.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002721 }
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002722 } break;
2723 case ROW: {
2724 std::vector<CPDFSDK_Annot*> sa;
Lei Zhang50218532015-10-30 14:03:43 -07002725 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2726 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2727 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002728 sa.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002729 }
2730
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002731 std::sort(sa.begin(), sa.end(), CompareByLeftAscending);
2732 while (!sa.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002733 int nLeftTopIndex = -1;
Lei Zhang50218532015-10-30 14:03:43 -07002734 FX_FLOAT fTop = 0.0f;
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002735 for (int i = sa.size() - 1; i >= 0; i--) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002736 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002737 if (rcAnnot.top > fTop) {
2738 nLeftTopIndex = i;
2739 fTop = rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002740 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002741 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002742 if (nLeftTopIndex >= 0) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002743 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
Tom Sepez281a9ea2016-02-26 14:24:28 -08002744 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002745 m_Annots.push_back(pLeftTopAnnot);
2746 sa.erase(sa.begin() + nLeftTopIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002747
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002748 std::vector<int> aSelect;
Wei Li89409932016-03-28 10:33:33 -07002749 for (size_t i = 0; i < sa.size(); ++i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002750 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002751 FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f;
2752 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002753 aSelect.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002754 }
Wei Li89409932016-03-28 10:33:33 -07002755 for (size_t i = 0; i < aSelect.size(); ++i)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002756 m_Annots.push_back(sa[aSelect[i]]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002757
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002758 for (int i = aSelect.size() - 1; i >= 0; --i)
2759 sa.erase(sa.begin() + aSelect[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002760 }
2761 }
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002762 } break;
2763 case COLUMN: {
2764 std::vector<CPDFSDK_Annot*> sa;
Lei Zhang50218532015-10-30 14:03:43 -07002765 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2766 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2767 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002768 sa.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002769 }
2770
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002771 std::sort(sa.begin(), sa.end(), CompareByTopDescending);
2772 while (!sa.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002773 int nLeftTopIndex = -1;
Lei Zhang50218532015-10-30 14:03:43 -07002774 FX_FLOAT fLeft = -1.0f;
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002775 for (int i = sa.size() - 1; i >= 0; --i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002776 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002777 if (fLeft < 0) {
2778 nLeftTopIndex = 0;
2779 fLeft = rcAnnot.left;
2780 } else if (rcAnnot.left < fLeft) {
2781 nLeftTopIndex = i;
2782 fLeft = rcAnnot.left;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002783 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002784 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002785
2786 if (nLeftTopIndex >= 0) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002787 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
Tom Sepez281a9ea2016-02-26 14:24:28 -08002788 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002789 m_Annots.push_back(pLeftTopAnnot);
2790 sa.erase(sa.begin() + nLeftTopIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002791
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002792 std::vector<int> aSelect;
Wei Li89409932016-03-28 10:33:33 -07002793 for (size_t i = 0; i < sa.size(); ++i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002794 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002795 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f;
2796 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002797 aSelect.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002798 }
Wei Li89409932016-03-28 10:33:33 -07002799 for (size_t i = 0; i < aSelect.size(); ++i)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002800 m_Annots.push_back(sa[aSelect[i]]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002801
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002802 for (int i = aSelect.size() - 1; i >= 0; --i)
2803 sa.erase(sa.begin() + aSelect[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002804 }
2805 }
Lei Zhang50218532015-10-30 14:03:43 -07002806 break;
2807 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002808 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002809}
2810
Tom Sepez281a9ea2016-02-26 14:24:28 -08002811CFX_FloatRect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) {
2812 CFX_FloatRect rcAnnot;
Lei Zhang50218532015-10-30 14:03:43 -07002813 pAnnot->GetPDFAnnot()->GetRect(rcAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002814 return rcAnnot;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002815}