blob: dbef4a9b670a3b4afab0cbe397cf746a2b308c8d [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"
Dan Sinclairedbb3192016-03-21 09:08:24 -040019#include "fpdfsdk/formfiller/cffl_formfiller.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080020#include "fpdfsdk/include/fsdk_actionhandler.h"
21#include "fpdfsdk/include/fsdk_baseannot.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080022#include "fpdfsdk/include/fsdk_define.h"
23#include "fpdfsdk/include/fsdk_mgr.h"
dsinclair64376be2016-03-31 20:03:24 -070024#include "fpdfsdk/javascript/ijs_context.h"
25#include "fpdfsdk/javascript/ijs_runtime.h"
dan sinclair89e904b2016-03-23 19:29:15 -040026#include "fpdfsdk/pdfwindow/PWL_Utils.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Tom Sepez40e9ff32015-11-30 12:39:54 -080028#ifdef PDF_ENABLE_XFA
dsinclair89bdd082016-04-06 10:47:54 -070029#include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h"
30#include "fpdfsdk/fpdfxfa/include/fpdfxfa_util.h"
dsinclair7222ea62016-04-06 14:33:07 -070031#include "xfa/fxfa/include/xfa_ffwidget.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080032#endif // PDF_ENABLE_XFA
33
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034CPDFSDK_Widget::CPDFSDK_Widget(CPDF_Annot* pAnnot,
35 CPDFSDK_PageView* pPageView,
36 CPDFSDK_InterForm* pInterForm)
37 : CPDFSDK_BAAnnot(pAnnot, pPageView),
38 m_pInterForm(pInterForm),
39 m_nAppAge(0),
Tom Sepez40e9ff32015-11-30 12:39:54 -080040 m_nValueAge(0)
41#ifdef PDF_ENABLE_XFA
42 ,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 m_hMixXFAWidget(NULL),
Tom Sepez40e9ff32015-11-30 12:39:54 -080044 m_pWidgetHandler(NULL)
45#endif // PDF_ENABLE_XFA
46{
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047}
48
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049CPDFSDK_Widget::~CPDFSDK_Widget() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Tom Sepez51da0932015-11-25 16:05:49 -080051#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -070052CXFA_FFWidget* CPDFSDK_Widget::GetMixXFAWidget() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080054 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
56 if (!m_hMixXFAWidget) {
dsinclairdf4bc592016-03-31 20:34:43 -070057 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 CFX_WideString sName;
Lei Zhang99766722016-02-23 11:21:48 -080059 if (GetFieldType() == FIELDTYPE_RADIOBUTTON) {
60 sName = GetAnnotName();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 if (sName.IsEmpty())
62 sName = GetName();
Dan Sinclair738b08c2016-03-01 14:45:20 -050063 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 sName = GetName();
Dan Sinclair738b08c2016-03-01 14:45:20 -050065 }
Bo Xufdc00a72014-10-28 23:03:33 -070066
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 if (!sName.IsEmpty())
tsepez70c55202016-04-14 15:32:35 -070068 m_hMixXFAWidget = pDocView->GetWidgetByName(sName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -070070 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 return m_hMixXFAWidget;
72 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070073
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 return NULL;
75}
76
dsinclairdf4bc592016-03-31 20:34:43 -070077CXFA_FFWidget* CPDFSDK_Widget::GetGroupMixXFAWidget() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080079 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
dsinclairdf4bc592016-03-31 20:34:43 -070081 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 CFX_WideString sName = GetName();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 if (!sName.IsEmpty())
tsepez70c55202016-04-14 15:32:35 -070084 return pDocView->GetWidgetByName(sName);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 }
86 }
87
Tom Sepez50d12ad2015-11-24 09:50:51 -080088 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089}
90
dsinclairdf4bc592016-03-31 20:34:43 -070091CXFA_FFWidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080093 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
95 if (!m_pWidgetHandler) {
dsinclairdf4bc592016-03-31 20:34:43 -070096 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 m_pWidgetHandler = pDocView->GetWidgetHandler();
98 }
99 }
100 return m_pWidgetHandler;
101 }
102
103 return NULL;
104}
105
106static XFA_EVENTTYPE GetXFAEventType(PDFSDK_XFAAActionType eXFAAAT) {
107 XFA_EVENTTYPE eEventType = XFA_EVENT_Unknown;
108
109 switch (eXFAAAT) {
110 case PDFSDK_XFA_Click:
111 eEventType = XFA_EVENT_Click;
112 break;
113 case PDFSDK_XFA_Full:
114 eEventType = XFA_EVENT_Full;
115 break;
116 case PDFSDK_XFA_PreOpen:
117 eEventType = XFA_EVENT_PreOpen;
118 break;
119 case PDFSDK_XFA_PostOpen:
120 eEventType = XFA_EVENT_PostOpen;
121 break;
122 }
123
124 return eEventType;
125}
126
127static XFA_EVENTTYPE GetXFAEventType(CPDF_AAction::AActionType eAAT,
128 FX_BOOL bWillCommit) {
129 XFA_EVENTTYPE eEventType = XFA_EVENT_Unknown;
130
131 switch (eAAT) {
132 case CPDF_AAction::CursorEnter:
133 eEventType = XFA_EVENT_MouseEnter;
134 break;
135 case CPDF_AAction::CursorExit:
136 eEventType = XFA_EVENT_MouseExit;
137 break;
138 case CPDF_AAction::ButtonDown:
139 eEventType = XFA_EVENT_MouseDown;
140 break;
141 case CPDF_AAction::ButtonUp:
142 eEventType = XFA_EVENT_MouseUp;
143 break;
144 case CPDF_AAction::GetFocus:
145 eEventType = XFA_EVENT_Enter;
146 break;
147 case CPDF_AAction::LoseFocus:
148 eEventType = XFA_EVENT_Exit;
149 break;
150 case CPDF_AAction::PageOpen:
151 break;
152 case CPDF_AAction::PageClose:
153 break;
154 case CPDF_AAction::PageVisible:
155 break;
156 case CPDF_AAction::PageInvisible:
157 break;
158 case CPDF_AAction::KeyStroke:
159 if (!bWillCommit) {
160 eEventType = XFA_EVENT_Change;
161 }
162 break;
163 case CPDF_AAction::Validate:
164 eEventType = XFA_EVENT_Validate;
165 break;
166 case CPDF_AAction::OpenPage:
167 case CPDF_AAction::ClosePage:
168 case CPDF_AAction::Format:
169 case CPDF_AAction::Calculate:
170 case CPDF_AAction::CloseDocument:
171 case CPDF_AAction::SaveDocument:
172 case CPDF_AAction::DocumentSaved:
173 case CPDF_AAction::PrintDocument:
174 case CPDF_AAction::DocumentPrinted:
175 break;
176 }
177
178 return eEventType;
179}
180
181FX_BOOL CPDFSDK_Widget::HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT) {
dsinclairdf4bc592016-03-31 20:34:43 -0700182 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
183 if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
185
186 if ((eEventType == XFA_EVENT_Click || eEventType == XFA_EVENT_Change) &&
187 GetFieldType() == FIELDTYPE_RADIOBUTTON) {
dsinclairdf4bc592016-03-31 20:34:43 -0700188 if (CXFA_FFWidget* hGroupWidget = GetGroupMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700189 CXFA_WidgetAcc* pAcc = hGroupWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 if (pXFAWidgetHandler->HasEvent(pAcc, eEventType))
191 return TRUE;
192 }
193 }
194
195 {
dsinclair221caf62016-04-04 12:08:40 -0700196 CXFA_WidgetAcc* pAcc = hWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 return pXFAWidgetHandler->HasEvent(pAcc, eEventType);
198 }
199 }
200 }
201
202 return FALSE;
203}
204
205FX_BOOL CPDFSDK_Widget::OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
206 PDFSDK_FieldAction& data,
207 CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800209 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -0700210 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
212
213 if (eEventType != XFA_EVENT_Unknown) {
dsinclairdf4bc592016-03-31 20:34:43 -0700214 if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 CXFA_EventParam param;
216 param.m_eType = eEventType;
217 param.m_wsChange = data.sChange;
218 param.m_iCommitKey = data.nCommitKey;
219 param.m_bShift = data.bShift;
220 param.m_iSelStart = data.nSelStart;
221 param.m_iSelEnd = data.nSelEnd;
222 param.m_wsFullText = data.sValue;
223 param.m_bKeyDown = data.bKeyDown;
224 param.m_bModifier = data.bModifier;
225 param.m_wsNewText = data.sValue;
226 if (data.nSelEnd > data.nSelStart)
227 param.m_wsNewText.Delete(data.nSelStart,
228 data.nSelEnd - data.nSelStart);
229 for (int i = 0; i < data.sChange.GetLength(); i++)
230 param.m_wsNewText.Insert(data.nSelStart, data.sChange[i]);
231 param.m_wsPrevText = data.sValue;
232
233 if ((eEventType == XFA_EVENT_Click || eEventType == XFA_EVENT_Change) &&
234 GetFieldType() == FIELDTYPE_RADIOBUTTON) {
dsinclairdf4bc592016-03-31 20:34:43 -0700235 if (CXFA_FFWidget* hGroupWidget = GetGroupMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700236 CXFA_WidgetAcc* pAcc = hGroupWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 param.m_pTarget = pAcc;
Wei Li42d1c1c2016-03-22 12:52:50 -0700238 if (pXFAWidgetHandler->ProcessEvent(pAcc, &param) !=
239 XFA_EVENTERROR_Success) {
240 return FALSE;
241 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 }
dsinclair221caf62016-04-04 12:08:40 -0700244 CXFA_WidgetAcc* pAcc = hWidget->GetDataAcc();
Wei Li42d1c1c2016-03-22 12:52:50 -0700245 param.m_pTarget = pAcc;
246 int32_t nRet = pXFAWidgetHandler->ProcessEvent(pAcc, &param);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247
dsinclairdf4bc592016-03-31 20:34:43 -0700248 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 pDocView->UpdateDocView();
250 }
Wei Li42d1c1c2016-03-22 12:52:50 -0700251 return nRet == XFA_EVENTERROR_Success;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 }
253 }
254 }
255
256 return FALSE;
257}
258
259void CPDFSDK_Widget::Synchronize(FX_BOOL bSynchronizeElse) {
dsinclairdf4bc592016-03-31 20:34:43 -0700260 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700261 CPDF_FormField* pFormField = GetFormField();
262 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
263 switch (GetFieldType()) {
264 case FIELDTYPE_CHECKBOX:
265 case FIELDTYPE_RADIOBUTTON: {
266 CPDF_FormControl* pFormCtrl = GetFormControl();
267 XFA_CHECKSTATE eCheckState =
268 pFormCtrl->IsChecked() ? XFA_CHECKSTATE_On : XFA_CHECKSTATE_Off;
269 pWidgetAcc->SetCheckState(eCheckState);
270 } break;
271 case FIELDTYPE_TEXTFIELD:
272 pWidgetAcc->SetValue(pFormField->GetValue(), XFA_VALUEPICTURE_Edit);
273 break;
274 case FIELDTYPE_LISTBOX: {
275 pWidgetAcc->ClearAllSelections();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276
dsinclair221caf62016-04-04 12:08:40 -0700277 for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz; i++) {
278 int nIndex = pFormField->GetSelectedIndex(i);
279 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
280 pWidgetAcc->SetItemState(nIndex, TRUE, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 }
dsinclair221caf62016-04-04 12:08:40 -0700282 } break;
283 case FIELDTYPE_COMBOBOX: {
284 pWidgetAcc->ClearAllSelections();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285
dsinclair221caf62016-04-04 12:08:40 -0700286 for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz; i++) {
287 int nIndex = pFormField->GetSelectedIndex(i);
288 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
289 pWidgetAcc->SetItemState(nIndex, TRUE, FALSE);
290 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 }
292
dsinclair221caf62016-04-04 12:08:40 -0700293 pWidgetAcc->SetValue(pFormField->GetValue(), XFA_VALUEPICTURE_Edit);
294 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 }
dsinclair221caf62016-04-04 12:08:40 -0700296
297 if (bSynchronizeElse)
298 pWidgetAcc->ProcessValueChanged();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 }
300 }
301}
302
303void CPDFSDK_Widget::SynchronizeXFAValue() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800305 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -0700306 CXFA_FFDocView* pXFADocView = pDoc->GetXFADocView();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 if (!pXFADocView)
308 return;
309
dsinclairdf4bc592016-03-31 20:34:43 -0700310 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700311 if (GetXFAWidgetHandler()) {
312 CPDFSDK_Widget::SynchronizeXFAValue(pXFADocView, hWidget, GetFormField(),
313 GetFormControl());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 }
315 }
316}
317
318void CPDFSDK_Widget::SynchronizeXFAItems() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800320 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -0700321 CXFA_FFDocView* pXFADocView = pDoc->GetXFADocView();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 if (!pXFADocView)
323 return;
324
dsinclairdf4bc592016-03-31 20:34:43 -0700325 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Tom Sepezbf59a072015-10-21 14:07:23 -0700326 if (GetXFAWidgetHandler())
327 SynchronizeXFAItems(pXFADocView, hWidget, GetFormField(), nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 }
329}
330
dsinclairdf4bc592016-03-31 20:34:43 -0700331void CPDFSDK_Widget::SynchronizeXFAValue(CXFA_FFDocView* pXFADocView,
332 CXFA_FFWidget* hWidget,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 CPDF_FormField* pFormField,
334 CPDF_FormControl* pFormControl) {
Lei Zhang5eca3052016-02-22 20:32:21 -0800335 ASSERT(hWidget);
dsinclair221caf62016-04-04 12:08:40 -0700336 ASSERT(pFormControl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337
dsinclair221caf62016-04-04 12:08:40 -0700338 switch (pFormField->GetFieldType()) {
339 case FIELDTYPE_CHECKBOX: {
340 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
341 pFormField->CheckControl(
342 pFormField->GetControlIndex(pFormControl),
343 pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On, true);
344 }
345 } break;
346 case FIELDTYPE_RADIOBUTTON: {
347 // TODO(weili): Check whether we need to handle checkbox and radio
348 // button differently, otherwise, merge these two cases.
349 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
350 pFormField->CheckControl(
351 pFormField->GetControlIndex(pFormControl),
352 pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On, true);
353 }
354 } break;
355 case FIELDTYPE_TEXTFIELD: {
356 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
357 CFX_WideString sValue;
358 pWidgetAcc->GetValue(sValue, XFA_VALUEPICTURE_Display);
359 pFormField->SetValue(sValue, TRUE);
360 }
361 } break;
362 case FIELDTYPE_LISTBOX: {
363 pFormField->ClearSelection(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364
dsinclair221caf62016-04-04 12:08:40 -0700365 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
366 for (int i = 0, sz = pWidgetAcc->CountSelectedItems(); i < sz; i++) {
367 int nIndex = pWidgetAcc->GetSelectedItem(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368
dsinclair221caf62016-04-04 12:08:40 -0700369 if (nIndex > -1 && nIndex < pFormField->CountOptions()) {
370 pFormField->SetItemSelection(nIndex, TRUE, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 }
372 }
dsinclair221caf62016-04-04 12:08:40 -0700373 }
374 } break;
375 case FIELDTYPE_COMBOBOX: {
376 pFormField->ClearSelection(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377
dsinclair221caf62016-04-04 12:08:40 -0700378 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
379 for (int i = 0, sz = pWidgetAcc->CountSelectedItems(); i < sz; i++) {
380 int nIndex = pWidgetAcc->GetSelectedItem(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381
dsinclair221caf62016-04-04 12:08:40 -0700382 if (nIndex > -1 && nIndex < pFormField->CountOptions()) {
383 pFormField->SetItemSelection(nIndex, TRUE, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 }
dsinclair221caf62016-04-04 12:08:40 -0700386
387 CFX_WideString sValue;
388 pWidgetAcc->GetValue(sValue, XFA_VALUEPICTURE_Display);
389 pFormField->SetValue(sValue, TRUE);
390 }
391 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 }
393}
394
dsinclairdf4bc592016-03-31 20:34:43 -0700395void CPDFSDK_Widget::SynchronizeXFAItems(CXFA_FFDocView* pXFADocView,
396 CXFA_FFWidget* hWidget,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 CPDF_FormField* pFormField,
398 CPDF_FormControl* pFormControl) {
Lei Zhang5eca3052016-02-22 20:32:21 -0800399 ASSERT(hWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400
dsinclair221caf62016-04-04 12:08:40 -0700401 switch (pFormField->GetFieldType()) {
402 case FIELDTYPE_LISTBOX: {
403 pFormField->ClearSelection(FALSE);
404 pFormField->ClearOptions(TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405
dsinclair221caf62016-04-04 12:08:40 -0700406 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
407 for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(); i < sz; i++) {
408 CFX_WideString swText;
409 pWidgetAcc->GetChoiceListItem(swText, i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410
dsinclair221caf62016-04-04 12:08:40 -0700411 pFormField->InsertOption(swText, i, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 }
dsinclair221caf62016-04-04 12:08:40 -0700413 }
414 } break;
415 case FIELDTYPE_COMBOBOX: {
416 pFormField->ClearSelection(FALSE);
417 pFormField->ClearOptions(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418
dsinclair221caf62016-04-04 12:08:40 -0700419 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
420 for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(); i < sz; i++) {
421 CFX_WideString swText;
422 pWidgetAcc->GetChoiceListItem(swText, i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423
dsinclair221caf62016-04-04 12:08:40 -0700424 pFormField->InsertOption(swText, i, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 }
dsinclair221caf62016-04-04 12:08:40 -0700426 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427
dsinclair221caf62016-04-04 12:08:40 -0700428 pFormField->SetValue(L"", TRUE);
429 } break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 }
431}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800432#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433
434FX_BOOL CPDFSDK_Widget::IsWidgetAppearanceValid(
435 CPDF_Annot::AppearanceMode mode) {
Wei Li9b761132016-01-29 15:44:20 -0800436 CPDF_Dictionary* pAP = m_pAnnot->GetAnnotDict()->GetDictBy("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 if (!pAP)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700438 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700439
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 // Choose the right sub-ap
441 const FX_CHAR* ap_entry = "N";
442 if (mode == CPDF_Annot::Down)
443 ap_entry = "D";
444 else if (mode == CPDF_Annot::Rollover)
445 ap_entry = "R";
446 if (!pAP->KeyExist(ap_entry))
447 ap_entry = "N";
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700448
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 // Get the AP stream or subdirectory
tsepezbd567552016-03-29 14:51:50 -0700450 CPDF_Object* psub = pAP->GetDirectObjectBy(ap_entry);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451 if (!psub)
452 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700453
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 int nFieldType = GetFieldType();
455 switch (nFieldType) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700456 case FIELDTYPE_PUSHBUTTON:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700457 case FIELDTYPE_COMBOBOX:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700458 case FIELDTYPE_LISTBOX:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700459 case FIELDTYPE_TEXTFIELD:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 case FIELDTYPE_SIGNATURE:
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400461 return psub->IsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 case FIELDTYPE_CHECKBOX:
463 case FIELDTYPE_RADIOBUTTON:
Dan Sinclairf1251c12015-10-20 16:24:45 -0400464 if (CPDF_Dictionary* pSubDict = psub->AsDictionary()) {
tsepez7b1ccf92016-04-14 11:04:57 -0700465 return !!pSubDict->GetStreamBy(GetAppState());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 }
467 return FALSE;
468 }
469 return TRUE;
470}
471
472int CPDFSDK_Widget::GetFieldType() const {
Lei Zhang96660d62015-12-14 18:27:25 -0800473 return GetFormField()->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474}
475
476FX_BOOL CPDFSDK_Widget::IsAppearanceValid() {
Tom Sepez51da0932015-11-25 16:05:49 -0800477#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800479 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 int nDocType = pDoc->GetDocType();
Tom Sepez540c4362015-11-24 13:33:57 -0800481 if (nDocType != DOCTYPE_PDF && nDocType != DOCTYPE_STATIC_XFA)
482 return TRUE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800483#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800484 return CPDFSDK_BAAnnot::IsAppearanceValid();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485}
486
487int CPDFSDK_Widget::GetFieldFlags() const {
488 CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489 CPDF_FormControl* pFormControl =
490 pPDFInterForm->GetControlByDict(m_pAnnot->GetAnnotDict());
491 CPDF_FormField* pFormField = pFormControl->GetField();
492 return pFormField->GetFieldFlags();
493}
494
495CFX_ByteString CPDFSDK_Widget::GetSubType() const {
496 int nType = GetFieldType();
497
498 if (nType == FIELDTYPE_SIGNATURE)
499 return BFFT_SIGNATURE;
500 return CPDFSDK_Annot::GetSubType();
501}
502
503CPDF_FormField* CPDFSDK_Widget::GetFormField() const {
Lei Zhang1b700c32015-10-30 23:55:35 -0700504 return GetFormControl()->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505}
506
507CPDF_FormControl* CPDFSDK_Widget::GetFormControl() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 return pPDFInterForm->GetControlByDict(GetAnnotDict());
510}
511
Lei Zhang1b700c32015-10-30 23:55:35 -0700512CPDF_FormControl* CPDFSDK_Widget::GetFormControl(
513 CPDF_InterForm* pInterForm,
514 const CPDF_Dictionary* pAnnotDict) {
Lei Zhang96660d62015-12-14 18:27:25 -0800515 ASSERT(pAnnotDict);
Lei Zhang1b700c32015-10-30 23:55:35 -0700516 return pInterForm->GetControlByDict(pAnnotDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517}
518
519int CPDFSDK_Widget::GetRotate() const {
520 CPDF_FormControl* pCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521 return pCtrl->GetRotation() % 360;
522}
523
Tom Sepez51da0932015-11-25 16:05:49 -0800524#ifdef PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -0700525CFX_WideString CPDFSDK_Widget::GetName() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700526 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527 return pFormField->GetFullName();
528}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800529#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700530
531FX_BOOL CPDFSDK_Widget::GetFillColor(FX_COLORREF& color) const {
532 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700533 int iColorType = 0;
534 color = FX_ARGBTOCOLORREF(pFormCtrl->GetBackgroundColor(iColorType));
535
536 return iColorType != COLORTYPE_TRANSPARENT;
537}
538
539FX_BOOL CPDFSDK_Widget::GetBorderColor(FX_COLORREF& color) const {
540 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 int iColorType = 0;
542 color = FX_ARGBTOCOLORREF(pFormCtrl->GetBorderColor(iColorType));
543
544 return iColorType != COLORTYPE_TRANSPARENT;
545}
546
547FX_BOOL CPDFSDK_Widget::GetTextColor(FX_COLORREF& color) const {
548 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
550 if (da.HasColor()) {
551 FX_ARGB argb;
552 int iColorType = COLORTYPE_TRANSPARENT;
553 da.GetColor(argb, iColorType);
554 color = FX_ARGBTOCOLORREF(argb);
555
556 return iColorType != COLORTYPE_TRANSPARENT;
557 }
558
559 return FALSE;
560}
561
562FX_FLOAT CPDFSDK_Widget::GetFontSize() const {
563 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564 CPDF_DefaultAppearance pDa = pFormCtrl->GetDefaultAppearance();
565 CFX_ByteString csFont = "";
566 FX_FLOAT fFontSize = 0.0f;
567 pDa.GetFont(csFont, fFontSize);
568
569 return fFontSize;
570}
571
Tom Sepezbf59a072015-10-21 14:07:23 -0700572int CPDFSDK_Widget::GetSelectedIndex(int nIndex) const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700573#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -0700574 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700575 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
576 if (nIndex < pWidgetAcc->CountSelectedItems())
577 return pWidgetAcc->GetSelectedItem(nIndex);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700578 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700580#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 return pFormField->GetSelectedIndex(nIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700583}
584
Tom Sepeza8a39e22015-10-12 15:47:07 -0700585#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -0800586CFX_WideString CPDFSDK_Widget::GetValue(FX_BOOL bDisplay) const {
dsinclairdf4bc592016-03-31 20:34:43 -0700587 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700588 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
589 CFX_WideString sValue;
590 pWidgetAcc->GetValue(
591 sValue, bDisplay ? XFA_VALUEPICTURE_Display : XFA_VALUEPICTURE_Edit);
592 return sValue;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700593 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800595#else
596CFX_WideString CPDFSDK_Widget::GetValue() const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700597#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700598 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 return pFormField->GetValue();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700600}
601
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602CFX_WideString CPDFSDK_Widget::GetDefaultValue() const {
603 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 return pFormField->GetDefaultValue();
605}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700606
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700607CFX_WideString CPDFSDK_Widget::GetOptionLabel(int nIndex) const {
608 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 return pFormField->GetOptionLabel(nIndex);
610}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700611
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700612int CPDFSDK_Widget::CountOptions() const {
613 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 return pFormField->CountOptions();
615}
616
Tom Sepezbf59a072015-10-21 14:07:23 -0700617FX_BOOL CPDFSDK_Widget::IsOptionSelected(int nIndex) const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700618#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -0700619 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
dsinclair221caf62016-04-04 12:08:40 -0700620 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
621 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
622 return pWidgetAcc->GetItemState(nIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700623
dsinclair221caf62016-04-04 12:08:40 -0700624 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700625 }
626 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700627#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700628 CPDF_FormField* pFormField = GetFormField();
629 return pFormField->IsItemSelected(nIndex);
630}
631
632int CPDFSDK_Widget::GetTopVisibleIndex() const {
633 CPDF_FormField* pFormField = GetFormField();
634 return pFormField->GetTopVisibleIndex();
635}
636
Wei Li97da9762016-03-11 17:00:48 -0800637bool CPDFSDK_Widget::IsChecked() const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700638#ifdef PDF_ENABLE_XFA
dsinclair221caf62016-04-04 12:08:40 -0700639 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
640 if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc())
641 return pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700643#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 return pFormCtrl->IsChecked();
646}
647
648int CPDFSDK_Widget::GetAlignment() const {
649 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700650 return pFormCtrl->GetControlAlignment();
651}
652
653int CPDFSDK_Widget::GetMaxLen() const {
654 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700655 return pFormField->GetMaxLen();
656}
657
Wei Li97da9762016-03-11 17:00:48 -0800658void CPDFSDK_Widget::SetCheck(bool bChecked, bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700659 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700660 CPDF_FormField* pFormField = pFormCtrl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661 pFormField->CheckControl(pFormField->GetControlIndex(pFormCtrl), bChecked,
662 bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800663#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 if (!IsWidgetAppearanceValid(CPDF_Annot::Normal))
665 ResetAppearance(TRUE);
666 if (!bNotify)
667 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800668#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669}
670
671void CPDFSDK_Widget::SetValue(const CFX_WideString& sValue, FX_BOOL bNotify) {
672 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700673 pFormField->SetValue(sValue, bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800674#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 if (!bNotify)
676 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800677#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700678}
679
680void CPDFSDK_Widget::SetDefaultValue(const CFX_WideString& sValue) {}
681void CPDFSDK_Widget::SetOptionSelection(int index,
682 FX_BOOL bSelected,
683 FX_BOOL bNotify) {
684 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700685 pFormField->SetItemSelection(index, bSelected, bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800686#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687 if (!bNotify)
688 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800689#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690}
691
692void CPDFSDK_Widget::ClearSelection(FX_BOOL bNotify) {
693 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700694 pFormField->ClearSelection(bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800695#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700696 if (!bNotify)
697 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800698#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700699}
700
701void CPDFSDK_Widget::SetTopVisibleIndex(int index) {}
702
703void CPDFSDK_Widget::SetAppModified() {
704 m_bAppModified = TRUE;
705}
706
707void CPDFSDK_Widget::ClearAppModified() {
708 m_bAppModified = FALSE;
709}
710
711FX_BOOL CPDFSDK_Widget::IsAppModified() const {
712 return m_bAppModified;
713}
714
Tom Sepez51da0932015-11-25 16:05:49 -0800715#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700716void CPDFSDK_Widget::ResetAppearance(FX_BOOL bValueChanged) {
717 switch (GetFieldType()) {
718 case FIELDTYPE_TEXTFIELD:
719 case FIELDTYPE_COMBOBOX: {
720 FX_BOOL bFormated = FALSE;
Lei Zhang99766722016-02-23 11:21:48 -0800721 CFX_WideString sValue = OnFormat(bFormated);
tsepezbd9748d2016-04-13 21:40:19 -0700722 ResetAppearance(bFormated ? sValue.c_str() : nullptr, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723 } break;
724 default:
Lei Zhang99766722016-02-23 11:21:48 -0800725 ResetAppearance(nullptr, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726 break;
727 }
728}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800729#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730
731void CPDFSDK_Widget::ResetAppearance(const FX_WCHAR* sValue,
732 FX_BOOL bValueChanged) {
733 SetAppModified();
734
735 m_nAppAge++;
736 if (m_nAppAge > 999999)
737 m_nAppAge = 0;
738 if (bValueChanged)
739 m_nValueAge++;
740
741 int nFieldType = GetFieldType();
742
743 switch (nFieldType) {
744 case FIELDTYPE_PUSHBUTTON:
745 ResetAppearance_PushButton();
746 break;
747 case FIELDTYPE_CHECKBOX:
748 ResetAppearance_CheckBox();
749 break;
750 case FIELDTYPE_RADIOBUTTON:
751 ResetAppearance_RadioButton();
752 break;
753 case FIELDTYPE_COMBOBOX:
754 ResetAppearance_ComboBox(sValue);
755 break;
756 case FIELDTYPE_LISTBOX:
757 ResetAppearance_ListBox();
758 break;
759 case FIELDTYPE_TEXTFIELD:
760 ResetAppearance_TextField(sValue);
761 break;
762 }
763
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700764 m_pAnnot->ClearCachedAP();
765}
766
767CFX_WideString CPDFSDK_Widget::OnFormat(FX_BOOL& bFormated) {
768 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800769 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770 return m_pInterForm->OnFormat(pFormField, bFormated);
771}
772
773void CPDFSDK_Widget::ResetFieldAppearance(FX_BOOL bValueChanged) {
774 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800775 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700776 m_pInterForm->ResetFieldAppearance(pFormField, NULL, bValueChanged);
777}
778
779void CPDFSDK_Widget::DrawAppearance(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800780 const CFX_Matrix* pUser2Device,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700781 CPDF_Annot::AppearanceMode mode,
782 const CPDF_RenderOptions* pOptions) {
783 int nFieldType = GetFieldType();
784
785 if ((nFieldType == FIELDTYPE_CHECKBOX ||
786 nFieldType == FIELDTYPE_RADIOBUTTON) &&
787 mode == CPDF_Annot::Normal &&
788 !IsWidgetAppearanceValid(CPDF_Annot::Normal)) {
789 CFX_PathData pathData;
790
Tom Sepez281a9ea2016-02-26 14:24:28 -0800791 CFX_FloatRect rcAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792
793 pathData.AppendRect(rcAnnot.left, rcAnnot.bottom, rcAnnot.right,
794 rcAnnot.top);
795
796 CFX_GraphStateData gsd;
797 gsd.m_LineWidth = 0.0f;
798
799 pDevice->DrawPath(&pathData, pUser2Device, &gsd, 0, 0xFFAAAAAA,
800 FXFILL_ALTERNATE);
801 } else {
802 CPDFSDK_BAAnnot::DrawAppearance(pDevice, pUser2Device, mode, pOptions);
803 }
804}
805
806void CPDFSDK_Widget::UpdateField() {
807 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800808 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700809 m_pInterForm->UpdateField(pFormField);
810}
811
812void CPDFSDK_Widget::DrawShadow(CFX_RenderDevice* pDevice,
813 CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700814 int nFieldType = GetFieldType();
815 if (m_pInterForm->IsNeedHighLight(nFieldType)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800816 CFX_FloatRect rc = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817 FX_COLORREF color = m_pInterForm->GetHighlightColor(nFieldType);
818 uint8_t alpha = m_pInterForm->GetHighlightAlpha();
819
820 CFX_FloatRect rcDevice;
821 ASSERT(m_pInterForm->GetDocument());
822 CPDFDoc_Environment* pEnv = m_pInterForm->GetDocument()->GetEnv();
823 if (!pEnv)
824 return;
Tom Sepez60d909e2015-12-10 15:34:55 -0800825 CFX_Matrix page2device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700826 pPageView->GetCurrentMatrix(page2device);
827 page2device.Transform(((FX_FLOAT)rc.left), ((FX_FLOAT)rc.bottom),
828 rcDevice.left, rcDevice.bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829 page2device.Transform(((FX_FLOAT)rc.right), ((FX_FLOAT)rc.top),
830 rcDevice.right, rcDevice.top);
831
832 rcDevice.Normalize();
833
834 FX_ARGB argb = ArgbEncode((int)alpha, color);
835 FX_RECT rcDev((int)rcDevice.left, (int)rcDevice.top, (int)rcDevice.right,
836 (int)rcDevice.bottom);
837 pDevice->FillRect(&rcDev, argb);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700838 }
839}
840
841void CPDFSDK_Widget::ResetAppearance_PushButton() {
842 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800843 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700844 int32_t nLayout = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845 switch (pControl->GetTextPosition()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700846 case TEXTPOS_ICON:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700847 nLayout = PPBL_ICON;
848 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700849 case TEXTPOS_BELOW:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700850 nLayout = PPBL_ICONTOPLABELBOTTOM;
851 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700852 case TEXTPOS_ABOVE:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700853 nLayout = PPBL_LABELTOPICONBOTTOM;
854 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700855 case TEXTPOS_RIGHT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856 nLayout = PPBL_ICONLEFTLABELRIGHT;
857 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700858 case TEXTPOS_LEFT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859 nLayout = PPBL_LABELLEFTICONRIGHT;
860 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700861 case TEXTPOS_OVERLAID:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700862 nLayout = PPBL_LABELOVERICON;
863 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700864 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700865 nLayout = PPBL_LABEL;
866 break;
867 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700868
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700869 CPWL_Color crBackground, crBorder;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700870
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700871 int iColorType;
872 FX_FLOAT fc[4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700873
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874 pControl->GetOriginalBackgroundColor(iColorType, fc);
875 if (iColorType > 0)
876 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700877
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878 pControl->GetOriginalBorderColor(iColorType, fc);
879 if (iColorType > 0)
880 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700881
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700882 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700883 CPWL_Dash dsBorder(3, 0, 0);
884 CPWL_Color crLeftTop, crRightBottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700885
dsinclair92cb5e52016-05-16 11:38:28 -0700886 BorderStyle nBorderStyle = GetBorderStyle();
887 switch (nBorderStyle) {
888 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 dsBorder = CPWL_Dash(3, 3, 0);
890 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700891 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700892 fBorderWidth *= 2;
893 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
894 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
895 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700896 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897 fBorderWidth *= 2;
898 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
899 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
900 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700901 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902 break;
903 }
904
Tom Sepez281a9ea2016-02-26 14:24:28 -0800905 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906
907 CPWL_Color crText(COLORTYPE_GRAY, 0);
908
909 FX_FLOAT fFontSize = 12.0f;
910 CFX_ByteString csNameTag;
911
912 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
913 if (da.HasColor()) {
914 da.GetColor(iColorType, fc);
915 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
916 }
917
918 if (da.HasFont())
919 da.GetFont(csNameTag, fFontSize);
920
921 CFX_WideString csWCaption;
922 CFX_WideString csNormalCaption, csRolloverCaption, csDownCaption;
923
924 if (pControl->HasMKEntry("CA")) {
925 csNormalCaption = pControl->GetNormalCaption();
926 }
927 if (pControl->HasMKEntry("RC")) {
928 csRolloverCaption = pControl->GetRolloverCaption();
929 }
930 if (pControl->HasMKEntry("AC")) {
931 csDownCaption = pControl->GetDownCaption();
932 }
933
934 CPDF_Stream* pNormalIcon = NULL;
935 CPDF_Stream* pRolloverIcon = NULL;
936 CPDF_Stream* pDownIcon = NULL;
937
938 if (pControl->HasMKEntry("I")) {
939 pNormalIcon = pControl->GetNormalIcon();
940 }
941 if (pControl->HasMKEntry("RI")) {
942 pRolloverIcon = pControl->GetRolloverIcon();
943 }
944 if (pControl->HasMKEntry("IX")) {
945 pDownIcon = pControl->GetDownIcon();
946 }
947
948 if (pNormalIcon) {
949 if (CPDF_Dictionary* pImageDict = pNormalIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800950 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 pImageDict->SetAtString("Name", "ImgA");
952 }
953 }
954
955 if (pRolloverIcon) {
956 if (CPDF_Dictionary* pImageDict = pRolloverIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800957 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958 pImageDict->SetAtString("Name", "ImgB");
959 }
960 }
961
962 if (pDownIcon) {
963 if (CPDF_Dictionary* pImageDict = pDownIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800964 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700965 pImageDict->SetAtString("Name", "ImgC");
966 }
967 }
968
969 CPDF_IconFit iconFit = pControl->GetIconFit();
970
971 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700972 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
973
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800974 CBA_FontMap font_map(this, pEnv->GetSysHandler());
975 font_map.SetAPType("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700976
977 CFX_ByteString csAP =
978 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
979 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
980 crLeftTop, crRightBottom, nBorderStyle,
981 dsBorder) +
982 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800983 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 pNormalIcon, iconFit, csNormalCaption, crText, fFontSize, nLayout);
985
986 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP);
987 if (pNormalIcon)
988 AddImageToAppearance("N", pNormalIcon);
989
990 CPDF_FormControl::HighlightingMode eHLM = pControl->GetHighlightingMode();
991 if (eHLM == CPDF_FormControl::Push || eHLM == CPDF_FormControl::Toggle) {
992 if (csRolloverCaption.IsEmpty() && !pRolloverIcon) {
993 csRolloverCaption = csNormalCaption;
994 pRolloverIcon = pNormalIcon;
995 }
996
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800997 font_map.SetAPType("R");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998
999 csAP = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1000 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1001 crLeftTop, crRightBottom,
1002 nBorderStyle, dsBorder) +
1003 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001004 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 pRolloverIcon, iconFit, csRolloverCaption, crText, fFontSize,
1006 nLayout);
1007
1008 WriteAppearance("R", GetRotatedRect(), GetMatrix(), csAP);
1009 if (pRolloverIcon)
1010 AddImageToAppearance("R", pRolloverIcon);
1011
1012 if (csDownCaption.IsEmpty() && !pDownIcon) {
1013 csDownCaption = csNormalCaption;
1014 pDownIcon = pNormalIcon;
1015 }
1016
1017 switch (nBorderStyle) {
dsinclair92cb5e52016-05-16 11:38:28 -07001018 case BorderStyle::BEVELED: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 CPWL_Color crTemp = crLeftTop;
1020 crLeftTop = crRightBottom;
1021 crRightBottom = crTemp;
dsinclair92cb5e52016-05-16 11:38:28 -07001022 break;
1023 }
1024 case BorderStyle::INSET: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1026 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001027 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001028 }
1029 default:
1030 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001031 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001032
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001033 font_map.SetAPType("D");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001034
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 csAP = CPWL_Utils::GetRectFillAppStream(
1036 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1037 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1038 crLeftTop, crRightBottom,
1039 nBorderStyle, dsBorder) +
1040 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001041 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042 pDownIcon, iconFit, csDownCaption, crText, fFontSize, nLayout);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001043
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001045 if (pDownIcon)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046 AddImageToAppearance("D", pDownIcon);
1047 } else {
1048 RemoveAppearance("D");
1049 RemoveAppearance("R");
1050 }
1051}
1052
1053void CPDFSDK_Widget::ResetAppearance_CheckBox() {
1054 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001055 CPWL_Color crBackground, crBorder, crText;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056 int iColorType;
1057 FX_FLOAT fc[4];
1058
1059 pControl->GetOriginalBackgroundColor(iColorType, fc);
1060 if (iColorType > 0)
1061 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1062
1063 pControl->GetOriginalBorderColor(iColorType, fc);
1064 if (iColorType > 0)
1065 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1066
1067 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068 CPWL_Dash dsBorder(3, 0, 0);
1069 CPWL_Color crLeftTop, crRightBottom;
1070
dsinclair92cb5e52016-05-16 11:38:28 -07001071 BorderStyle nBorderStyle = GetBorderStyle();
1072 switch (nBorderStyle) {
1073 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074 dsBorder = CPWL_Dash(3, 3, 0);
1075 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001076 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077 fBorderWidth *= 2;
1078 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1079 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1080 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001081 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082 fBorderWidth *= 2;
1083 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1084 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1085 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087 break;
1088 }
1089
Tom Sepez281a9ea2016-02-26 14:24:28 -08001090 CFX_FloatRect rcWindow = GetRotatedRect();
1091 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092
1093 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
1094 if (da.HasColor()) {
1095 da.GetColor(iColorType, fc);
1096 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1097 }
1098
1099 int32_t nStyle = 0;
1100
1101 CFX_WideString csWCaption = pControl->GetNormalCaption();
1102 if (csWCaption.GetLength() > 0) {
1103 switch (csWCaption[0]) {
1104 case L'l':
1105 nStyle = PCS_CIRCLE;
1106 break;
1107 case L'8':
1108 nStyle = PCS_CROSS;
1109 break;
1110 case L'u':
1111 nStyle = PCS_DIAMOND;
1112 break;
1113 case L'n':
1114 nStyle = PCS_SQUARE;
1115 break;
1116 case L'H':
1117 nStyle = PCS_STAR;
1118 break;
1119 default: // L'4'
1120 nStyle = PCS_CHECK;
1121 break;
1122 }
1123 } else {
1124 nStyle = PCS_CHECK;
1125 }
1126
1127 CFX_ByteString csAP_N_ON =
1128 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1129 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1130 crLeftTop, crRightBottom, nBorderStyle,
1131 dsBorder);
1132
1133 CFX_ByteString csAP_N_OFF = csAP_N_ON;
1134
1135 switch (nBorderStyle) {
dsinclair92cb5e52016-05-16 11:38:28 -07001136 case BorderStyle::BEVELED: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001137 CPWL_Color crTemp = crLeftTop;
1138 crLeftTop = crRightBottom;
1139 crRightBottom = crTemp;
dsinclair92cb5e52016-05-16 11:38:28 -07001140 break;
1141 }
1142 case BorderStyle::INSET: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001143 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1144 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1145 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001146 }
1147 default:
1148 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001149 }
1150
1151 CFX_ByteString csAP_D_ON =
1152 CPWL_Utils::GetRectFillAppStream(
1153 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1154 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1155 crLeftTop, crRightBottom, nBorderStyle,
1156 dsBorder);
1157
1158 CFX_ByteString csAP_D_OFF = csAP_D_ON;
1159
1160 csAP_N_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
1161 csAP_D_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
1162
1163 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
1164 pControl->GetCheckedAPState());
1165 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1166
1167 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
1168 pControl->GetCheckedAPState());
1169 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1170
1171 CFX_ByteString csAS = GetAppState();
1172 if (csAS.IsEmpty())
1173 SetAppState("Off");
1174}
1175
1176void CPDFSDK_Widget::ResetAppearance_RadioButton() {
1177 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178 CPWL_Color crBackground, crBorder, crText;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001179 int iColorType;
1180 FX_FLOAT fc[4];
1181
1182 pControl->GetOriginalBackgroundColor(iColorType, fc);
1183 if (iColorType > 0)
1184 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1185
1186 pControl->GetOriginalBorderColor(iColorType, fc);
1187 if (iColorType > 0)
1188 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1189
1190 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001191 CPWL_Dash dsBorder(3, 0, 0);
1192 CPWL_Color crLeftTop, crRightBottom;
1193
dsinclair92cb5e52016-05-16 11:38:28 -07001194 BorderStyle nBorderStyle = GetBorderStyle();
1195 switch (nBorderStyle) {
1196 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001197 dsBorder = CPWL_Dash(3, 3, 0);
1198 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001199 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001200 fBorderWidth *= 2;
1201 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1202 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1203 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001204 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001205 fBorderWidth *= 2;
1206 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1207 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1208 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001209 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001210 break;
1211 }
1212
Tom Sepez281a9ea2016-02-26 14:24:28 -08001213 CFX_FloatRect rcWindow = GetRotatedRect();
1214 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001215
1216 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
1217 if (da.HasColor()) {
1218 da.GetColor(iColorType, fc);
1219 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1220 }
1221
1222 int32_t nStyle = 0;
1223
1224 CFX_WideString csWCaption = pControl->GetNormalCaption();
1225 if (csWCaption.GetLength() > 0) {
1226 switch (csWCaption[0]) {
1227 default: // L'l':
1228 nStyle = PCS_CIRCLE;
1229 break;
1230 case L'8':
1231 nStyle = PCS_CROSS;
1232 break;
1233 case L'u':
1234 nStyle = PCS_DIAMOND;
1235 break;
1236 case L'n':
1237 nStyle = PCS_SQUARE;
1238 break;
1239 case L'H':
1240 nStyle = PCS_STAR;
1241 break;
1242 case L'4':
1243 nStyle = PCS_CHECK;
1244 break;
1245 }
1246 } else {
1247 nStyle = PCS_CIRCLE;
1248 }
1249
1250 CFX_ByteString csAP_N_ON;
1251
Tom Sepez281a9ea2016-02-26 14:24:28 -08001252 CFX_FloatRect rcCenter =
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001253 CPWL_Utils::DeflateRect(CPWL_Utils::GetCenterSquare(rcWindow), 1.0f);
1254
1255 if (nStyle == PCS_CIRCLE) {
dsinclair92cb5e52016-05-16 11:38:28 -07001256 if (nBorderStyle == BorderStyle::BEVELED) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001257 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1258 crRightBottom = CPWL_Utils::SubstractColor(crBackground, 0.25f);
dsinclair92cb5e52016-05-16 11:38:28 -07001259 } else if (nBorderStyle == BorderStyle::INSET) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001260 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5f);
1261 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75f);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001262 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001263
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001264 csAP_N_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBackground) +
1265 CPWL_Utils::GetCircleBorderAppStream(
1266 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
1267 nBorderStyle, dsBorder);
1268 } else {
1269 csAP_N_ON = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1270 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1271 crLeftTop, crRightBottom,
1272 nBorderStyle, dsBorder);
1273 }
1274
1275 CFX_ByteString csAP_N_OFF = csAP_N_ON;
1276
1277 switch (nBorderStyle) {
dsinclair92cb5e52016-05-16 11:38:28 -07001278 case BorderStyle::BEVELED: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001279 CPWL_Color crTemp = crLeftTop;
1280 crLeftTop = crRightBottom;
1281 crRightBottom = crTemp;
dsinclair92cb5e52016-05-16 11:38:28 -07001282 break;
1283 }
1284 case BorderStyle::INSET: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001285 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1286 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1287 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001288 }
1289 default:
1290 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001291 }
1292
1293 CFX_ByteString csAP_D_ON;
1294
1295 if (nStyle == PCS_CIRCLE) {
1296 CPWL_Color crBK = CPWL_Utils::SubstractColor(crBackground, 0.25f);
dsinclair92cb5e52016-05-16 11:38:28 -07001297 if (nBorderStyle == BorderStyle::BEVELED) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001298 crLeftTop = CPWL_Utils::SubstractColor(crBackground, 0.25f);
1299 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1300 crBK = crBackground;
dsinclair92cb5e52016-05-16 11:38:28 -07001301 } else if (nBorderStyle == BorderStyle::INSET) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001302 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1303 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1304 }
1305
1306 csAP_D_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBK) +
1307 CPWL_Utils::GetCircleBorderAppStream(
1308 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
1309 nBorderStyle, dsBorder);
1310 } else {
1311 csAP_D_ON = CPWL_Utils::GetRectFillAppStream(
1312 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1313 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1314 crLeftTop, crRightBottom,
1315 nBorderStyle, dsBorder);
1316 }
1317
1318 CFX_ByteString csAP_D_OFF = csAP_D_ON;
1319
1320 csAP_N_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
1321 csAP_D_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
1322
1323 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
1324 pControl->GetCheckedAPState());
1325 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1326
1327 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
1328 pControl->GetCheckedAPState());
1329 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1330
1331 CFX_ByteString csAS = GetAppState();
1332 if (csAS.IsEmpty())
1333 SetAppState("Off");
1334}
1335
1336void CPDFSDK_Widget::ResetAppearance_ComboBox(const FX_WCHAR* sValue) {
1337 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001338 CPDF_FormField* pField = pControl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001339 CFX_ByteTextBuf sBody, sLines;
1340
Tom Sepez281a9ea2016-02-26 14:24:28 -08001341 CFX_FloatRect rcClient = GetClientRect();
1342 CFX_FloatRect rcButton = rcClient;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001343 rcButton.left = rcButton.right - 13;
1344 rcButton.Normalize();
1345
thestig732f6a02016-05-12 10:41:56 -07001346 IFX_Edit* pEdit = IFX_Edit::NewEdit();
1347 pEdit->EnableRefresh(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001348
thestig732f6a02016-05-12 10:41:56 -07001349 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1350 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
1351 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1352 pEdit->SetFontMap(&font_map);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001353
thestig732f6a02016-05-12 10:41:56 -07001354 CFX_FloatRect rcEdit = rcClient;
1355 rcEdit.right = rcButton.left;
1356 rcEdit.Normalize();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357
thestig732f6a02016-05-12 10:41:56 -07001358 pEdit->SetPlateRect(rcEdit);
1359 pEdit->SetAlignmentV(1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001360
thestig732f6a02016-05-12 10:41:56 -07001361 FX_FLOAT fFontSize = GetFontSize();
1362 if (IsFloatZero(fFontSize))
1363 pEdit->SetAutoFontSize(TRUE);
1364 else
1365 pEdit->SetFontSize(fFontSize);
1366
1367 pEdit->Initialize();
1368
1369 if (sValue) {
1370 pEdit->SetText(sValue);
1371 } else {
1372 int32_t nCurSel = pField->GetSelectedIndex(0);
1373
1374 if (nCurSel < 0)
1375 pEdit->SetText(pField->GetValue().c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376 else
thestig732f6a02016-05-12 10:41:56 -07001377 pEdit->SetText(pField->GetOptionLabel(nCurSel).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001378 }
1379
thestig732f6a02016-05-12 10:41:56 -07001380 CFX_FloatRect rcContent = pEdit->GetContentRect();
1381
1382 CFX_ByteString sEdit =
1383 CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, 0.0f));
1384 if (sEdit.GetLength() > 0) {
1385 sBody << "/Tx BMC\n"
1386 << "q\n";
1387 if (rcContent.Width() > rcEdit.Width() ||
1388 rcContent.Height() > rcEdit.Height()) {
1389 sBody << rcEdit.left << " " << rcEdit.bottom << " " << rcEdit.Width()
1390 << " " << rcEdit.Height() << " re\nW\nn\n";
1391 }
1392
1393 CPWL_Color crText = GetTextPWLColor();
1394 sBody << "BT\n"
1395 << CPWL_Utils::GetColorAppStream(crText) << sEdit << "ET\n"
1396 << "Q\nEMC\n";
1397 }
1398
1399 IFX_Edit::DelEdit(pEdit);
1400
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001401 sBody << CPWL_Utils::GetDropButtonAppStream(rcButton);
1402
1403 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001404 sLines.AsStringC() + sBody.AsStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001405
1406 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1407}
1408
1409void CPDFSDK_Widget::ResetAppearance_ListBox() {
1410 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001411 CPDF_FormField* pField = pControl->GetField();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001412 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001413 CFX_ByteTextBuf sBody, sLines;
1414
thestig732f6a02016-05-12 10:41:56 -07001415 IFX_Edit* pEdit = IFX_Edit::NewEdit();
1416 pEdit->EnableRefresh(FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001417
thestig732f6a02016-05-12 10:41:56 -07001418 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1419 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001420
thestig732f6a02016-05-12 10:41:56 -07001421 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1422 pEdit->SetFontMap(&font_map);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001423
thestig732f6a02016-05-12 10:41:56 -07001424 pEdit->SetPlateRect(CFX_FloatRect(rcClient.left, 0.0f, rcClient.right, 0.0f));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001425
thestig732f6a02016-05-12 10:41:56 -07001426 FX_FLOAT fFontSize = GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001427
thestig594b20b2016-05-12 21:56:43 -07001428 pEdit->SetFontSize(IsFloatZero(fFontSize) ? 12.0f : fFontSize);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001429
thestig732f6a02016-05-12 10:41:56 -07001430 pEdit->Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001431
thestig732f6a02016-05-12 10:41:56 -07001432 CFX_ByteTextBuf sList;
1433 FX_FLOAT fy = rcClient.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001434
thestig732f6a02016-05-12 10:41:56 -07001435 int32_t nTop = pField->GetTopVisibleIndex();
1436 int32_t nCount = pField->CountOptions();
1437 int32_t nSelCount = pField->CountSelectedItems();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001438
thestig594b20b2016-05-12 21:56:43 -07001439 for (int32_t i = nTop; i < nCount; ++i) {
1440 bool bSelected = false;
1441 for (int32_t j = 0; j < nSelCount; ++j) {
thestig732f6a02016-05-12 10:41:56 -07001442 if (pField->GetSelectedIndex(j) == i) {
thestig594b20b2016-05-12 21:56:43 -07001443 bSelected = true;
thestig732f6a02016-05-12 10:41:56 -07001444 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001445 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001446 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001447
thestig732f6a02016-05-12 10:41:56 -07001448 pEdit->SetText(pField->GetOptionLabel(i).c_str());
1449
1450 CFX_FloatRect rcContent = pEdit->GetContentRect();
1451 FX_FLOAT fItemHeight = rcContent.Height();
1452
1453 if (bSelected) {
1454 CFX_FloatRect rcItem =
1455 CFX_FloatRect(rcClient.left, fy - fItemHeight, rcClient.right, fy);
1456 sList << "q\n"
1457 << CPWL_Utils::GetColorAppStream(
1458 CPWL_Color(COLORTYPE_RGB, 0, 51.0f / 255.0f,
1459 113.0f / 255.0f),
1460 TRUE)
1461 << rcItem.left << " " << rcItem.bottom << " " << rcItem.Width()
1462 << " " << rcItem.Height() << " re f\n"
1463 << "Q\n";
1464
1465 sList << "BT\n"
1466 << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_GRAY, 1),
1467 TRUE)
1468 << CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, fy))
1469 << "ET\n";
1470 } else {
1471 CPWL_Color crText = GetTextPWLColor();
1472 sList << "BT\n"
1473 << CPWL_Utils::GetColorAppStream(crText, TRUE)
1474 << CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, fy))
1475 << "ET\n";
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001476 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001477
thestig732f6a02016-05-12 10:41:56 -07001478 fy -= fItemHeight;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001479 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001480
thestig732f6a02016-05-12 10:41:56 -07001481 if (sList.GetSize() > 0) {
1482 sBody << "/Tx BMC\n"
1483 << "q\n"
1484 << rcClient.left << " " << rcClient.bottom << " " << rcClient.Width()
1485 << " " << rcClient.Height() << " re\nW\nn\n";
1486 sBody << sList << "Q\nEMC\n";
1487 }
1488
1489 IFX_Edit::DelEdit(pEdit);
1490
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001492 sLines.AsStringC() + sBody.AsStringC();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001493
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001494 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001495}
1496
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001497void CPDFSDK_Widget::ResetAppearance_TextField(const FX_WCHAR* sValue) {
1498 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001499 CPDF_FormField* pField = pControl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001500 CFX_ByteTextBuf sBody, sLines;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001501
thestig732f6a02016-05-12 10:41:56 -07001502 IFX_Edit* pEdit = IFX_Edit::NewEdit();
1503 pEdit->EnableRefresh(FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001504
thestig732f6a02016-05-12 10:41:56 -07001505 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1506 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001507
thestig732f6a02016-05-12 10:41:56 -07001508 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1509 pEdit->SetFontMap(&font_map);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001510
thestig732f6a02016-05-12 10:41:56 -07001511 CFX_FloatRect rcClient = GetClientRect();
1512 pEdit->SetPlateRect(rcClient);
1513 pEdit->SetAlignmentH(pControl->GetControlAlignment());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001514
thestig732f6a02016-05-12 10:41:56 -07001515 uint32_t dwFieldFlags = pField->GetFieldFlags();
1516 FX_BOOL bMultiLine = (dwFieldFlags >> 12) & 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001517
thestig732f6a02016-05-12 10:41:56 -07001518 if (bMultiLine) {
1519 pEdit->SetMultiLine(TRUE);
1520 pEdit->SetAutoReturn(TRUE);
1521 } else {
1522 pEdit->SetAlignmentV(1);
1523 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001524
thestig732f6a02016-05-12 10:41:56 -07001525 uint16_t subWord = 0;
1526 if ((dwFieldFlags >> 13) & 1) {
1527 subWord = '*';
1528 pEdit->SetPasswordChar(subWord);
1529 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001530
thestig732f6a02016-05-12 10:41:56 -07001531 int nMaxLen = pField->GetMaxLen();
1532 FX_BOOL bCharArray = (dwFieldFlags >> 24) & 1;
1533 FX_FLOAT fFontSize = GetFontSize();
Bo Xufdc00a72014-10-28 23:03:33 -07001534
Tom Sepez51da0932015-11-25 16:05:49 -08001535#ifdef PDF_ENABLE_XFA
thestig732f6a02016-05-12 10:41:56 -07001536 CFX_WideString sValueTmp;
1537 if (!sValue && GetMixXFAWidget()) {
1538 sValueTmp = GetValue(TRUE);
1539 sValue = sValueTmp.c_str();
1540 }
Tom Sepez40e9ff32015-11-30 12:39:54 -08001541#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001542
thestig732f6a02016-05-12 10:41:56 -07001543 if (nMaxLen > 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001544 if (bCharArray) {
thestig732f6a02016-05-12 10:41:56 -07001545 pEdit->SetCharArray(nMaxLen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001546
thestig732f6a02016-05-12 10:41:56 -07001547 if (IsFloatZero(fFontSize)) {
1548 fFontSize = CPWL_Edit::GetCharArrayAutoFontSize(font_map.GetPDFFont(0),
1549 rcClient, nMaxLen);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001550 }
thestig732f6a02016-05-12 10:41:56 -07001551 } else {
1552 if (sValue)
1553 nMaxLen = wcslen((const wchar_t*)sValue);
1554 pEdit->SetLimitChar(nMaxLen);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001555 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001556 }
1557
thestig732f6a02016-05-12 10:41:56 -07001558 if (IsFloatZero(fFontSize))
1559 pEdit->SetAutoFontSize(TRUE);
1560 else
1561 pEdit->SetFontSize(fFontSize);
1562
1563 pEdit->Initialize();
1564
1565 if (sValue)
1566 pEdit->SetText(sValue);
1567 else
1568 pEdit->SetText(pField->GetValue().c_str());
1569
1570 CFX_FloatRect rcContent = pEdit->GetContentRect();
1571
1572 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(
1573 pEdit, CFX_FloatPoint(0.0f, 0.0f), NULL, !bCharArray, subWord);
1574
1575 if (sEdit.GetLength() > 0) {
1576 sBody << "/Tx BMC\n"
1577 << "q\n";
1578 if (rcContent.Width() > rcClient.Width() ||
1579 rcContent.Height() > rcClient.Height()) {
1580 sBody << rcClient.left << " " << rcClient.bottom << " "
1581 << rcClient.Width() << " " << rcClient.Height() << " re\nW\nn\n";
1582 }
1583 CPWL_Color crText = GetTextPWLColor();
1584 sBody << "BT\n"
1585 << CPWL_Utils::GetColorAppStream(crText) << sEdit << "ET\n"
1586 << "Q\nEMC\n";
1587 }
1588
1589 if (bCharArray) {
1590 switch (GetBorderStyle()) {
dsinclair92cb5e52016-05-16 11:38:28 -07001591 case BorderStyle::SOLID: {
thestig732f6a02016-05-12 10:41:56 -07001592 CFX_ByteString sColor =
1593 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1594 if (sColor.GetLength() > 0) {
1595 sLines << "q\n"
1596 << GetBorderWidth() << " w\n"
1597 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1598 << " 2 J 0 j\n";
1599
thestig594b20b2016-05-12 21:56:43 -07001600 for (int32_t i = 1; i < nMaxLen; ++i) {
thestig732f6a02016-05-12 10:41:56 -07001601 sLines << rcClient.left +
1602 ((rcClient.right - rcClient.left) / nMaxLen) * i
1603 << " " << rcClient.bottom << " m\n"
1604 << rcClient.left +
1605 ((rcClient.right - rcClient.left) / nMaxLen) * i
1606 << " " << rcClient.top << " l S\n";
1607 }
1608
1609 sLines << "Q\n";
1610 }
dsinclair92cb5e52016-05-16 11:38:28 -07001611 break;
1612 }
1613 case BorderStyle::DASH: {
thestig732f6a02016-05-12 10:41:56 -07001614 CFX_ByteString sColor =
1615 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1616 if (sColor.GetLength() > 0) {
1617 CPWL_Dash dsBorder = CPWL_Dash(3, 3, 0);
1618
1619 sLines << "q\n"
1620 << GetBorderWidth() << " w\n"
1621 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1622 << "[" << dsBorder.nDash << " " << dsBorder.nGap << "] "
1623 << dsBorder.nPhase << " d\n";
1624
thestig594b20b2016-05-12 21:56:43 -07001625 for (int32_t i = 1; i < nMaxLen; ++i) {
thestig732f6a02016-05-12 10:41:56 -07001626 sLines << rcClient.left +
1627 ((rcClient.right - rcClient.left) / nMaxLen) * i
1628 << " " << rcClient.bottom << " m\n"
1629 << rcClient.left +
1630 ((rcClient.right - rcClient.left) / nMaxLen) * i
1631 << " " << rcClient.top << " l S\n";
1632 }
1633
1634 sLines << "Q\n";
1635 }
dsinclair92cb5e52016-05-16 11:38:28 -07001636 break;
1637 }
1638 default:
1639 break;
thestig732f6a02016-05-12 10:41:56 -07001640 }
1641 }
1642
1643 IFX_Edit::DelEdit(pEdit);
1644
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001645 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001646 sLines.AsStringC() + sBody.AsStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001647 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001648}
1649
Tom Sepez281a9ea2016-02-26 14:24:28 -08001650CFX_FloatRect CPDFSDK_Widget::GetClientRect() const {
1651 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001652 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1653 switch (GetBorderStyle()) {
dsinclair92cb5e52016-05-16 11:38:28 -07001654 case BorderStyle::BEVELED:
1655 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001656 fBorderWidth *= 2.0f;
1657 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001658 default:
1659 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001660 }
1661
1662 return CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001663}
1664
Tom Sepez281a9ea2016-02-26 14:24:28 -08001665CFX_FloatRect CPDFSDK_Widget::GetRotatedRect() const {
1666 CFX_FloatRect rectAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001667 FX_FLOAT fWidth = rectAnnot.right - rectAnnot.left;
1668 FX_FLOAT fHeight = rectAnnot.top - rectAnnot.bottom;
1669
1670 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001671 CFX_FloatRect rcPDFWindow;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001672 switch (abs(pControl->GetRotation() % 360)) {
1673 case 0:
1674 case 180:
1675 default:
Tom Sepez281a9ea2016-02-26 14:24:28 -08001676 rcPDFWindow = CFX_FloatRect(0, 0, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001677 break;
1678 case 90:
1679 case 270:
Tom Sepez281a9ea2016-02-26 14:24:28 -08001680 rcPDFWindow = CFX_FloatRect(0, 0, fHeight, fWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001681 break;
1682 }
1683
1684 return rcPDFWindow;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001685}
1686
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001687CFX_ByteString CPDFSDK_Widget::GetBackgroundAppStream() const {
1688 CPWL_Color crBackground = GetFillPWLColor();
1689 if (crBackground.nColorType != COLORTYPE_TRANSPARENT) {
1690 return CPWL_Utils::GetRectFillAppStream(GetRotatedRect(), crBackground);
1691 }
1692 return "";
Bo Xufdc00a72014-10-28 23:03:33 -07001693}
1694
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001695CFX_ByteString CPDFSDK_Widget::GetBorderAppStream() const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001696 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001697 CPWL_Color crBorder = GetBorderPWLColor();
1698 CPWL_Color crBackground = GetFillPWLColor();
1699 CPWL_Color crLeftTop, crRightBottom;
1700
1701 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001702 CPWL_Dash dsBorder(3, 0, 0);
1703
dsinclair92cb5e52016-05-16 11:38:28 -07001704 BorderStyle nBorderStyle = GetBorderStyle();
1705 switch (nBorderStyle) {
1706 case BorderStyle::DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001707 dsBorder = CPWL_Dash(3, 3, 0);
1708 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001709 case BorderStyle::BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001710 fBorderWidth *= 2;
1711 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1712 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1713 break;
dsinclair92cb5e52016-05-16 11:38:28 -07001714 case BorderStyle::INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001715 fBorderWidth *= 2;
1716 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1717 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1718 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001719 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001720 break;
1721 }
1722
1723 return CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1724 crLeftTop, crRightBottom, nBorderStyle,
1725 dsBorder);
Bo Xufdc00a72014-10-28 23:03:33 -07001726}
1727
Tom Sepez60d909e2015-12-10 15:34:55 -08001728CFX_Matrix CPDFSDK_Widget::GetMatrix() const {
1729 CFX_Matrix mt;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001730 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001731 CFX_FloatRect rcAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001732 FX_FLOAT fWidth = rcAnnot.right - rcAnnot.left;
1733 FX_FLOAT fHeight = rcAnnot.top - rcAnnot.bottom;
1734
1735 switch (abs(pControl->GetRotation() % 360)) {
1736 case 0:
1737 default:
Tom Sepez60d909e2015-12-10 15:34:55 -08001738 mt = CFX_Matrix(1, 0, 0, 1, 0, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001739 break;
1740 case 90:
Tom Sepez60d909e2015-12-10 15:34:55 -08001741 mt = CFX_Matrix(0, 1, -1, 0, fWidth, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001742 break;
1743 case 180:
Tom Sepez60d909e2015-12-10 15:34:55 -08001744 mt = CFX_Matrix(-1, 0, 0, -1, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001745 break;
1746 case 270:
Tom Sepez60d909e2015-12-10 15:34:55 -08001747 mt = CFX_Matrix(0, -1, 1, 0, 0, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001748 break;
1749 }
1750
1751 return mt;
Bo Xufdc00a72014-10-28 23:03:33 -07001752}
1753
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001754CPWL_Color CPDFSDK_Widget::GetTextPWLColor() const {
1755 CPWL_Color crText = CPWL_Color(COLORTYPE_GRAY, 0);
1756
1757 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001758 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
1759 if (da.HasColor()) {
1760 int32_t iColorType;
1761 FX_FLOAT fc[4];
1762 da.GetColor(iColorType, fc);
1763 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1764 }
1765
1766 return crText;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001767}
1768
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001769CPWL_Color CPDFSDK_Widget::GetBorderPWLColor() const {
1770 CPWL_Color crBorder;
1771
1772 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001773 int32_t iColorType;
1774 FX_FLOAT fc[4];
1775 pFormCtrl->GetOriginalBorderColor(iColorType, fc);
1776 if (iColorType > 0)
1777 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1778
1779 return crBorder;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001780}
1781
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001782CPWL_Color CPDFSDK_Widget::GetFillPWLColor() const {
1783 CPWL_Color crFill;
1784
1785 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001786 int32_t iColorType;
1787 FX_FLOAT fc[4];
1788 pFormCtrl->GetOriginalBackgroundColor(iColorType, fc);
1789 if (iColorType > 0)
1790 crFill = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1791
1792 return crFill;
Bo Xufdc00a72014-10-28 23:03:33 -07001793}
1794
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001795void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType,
1796 CPDF_Stream* pImage) {
Tom Sepez33420902015-10-13 15:00:10 -07001797 CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
Lei Zhang96660d62015-12-14 18:27:25 -08001798 ASSERT(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001799
Wei Li9b761132016-01-29 15:44:20 -08001800 CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP");
tsepez7b1ccf92016-04-14 11:04:57 -07001801 CPDF_Stream* pStream = pAPDict->GetStreamBy(sAPType);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001802 CPDF_Dictionary* pStreamDict = pStream->GetDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001803 CFX_ByteString sImageAlias = "IMG";
1804
1805 if (CPDF_Dictionary* pImageDict = pImage->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -08001806 sImageAlias = pImageDict->GetStringBy("Name");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001807 if (sImageAlias.IsEmpty())
1808 sImageAlias = "IMG";
1809 }
1810
Wei Li9b761132016-01-29 15:44:20 -08001811 CPDF_Dictionary* pStreamResList = pStreamDict->GetDictBy("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812 if (!pStreamResList) {
Tom Sepezae51c812015-08-05 12:34:06 -07001813 pStreamResList = new CPDF_Dictionary();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001814 pStreamDict->SetAt("Resources", pStreamResList);
1815 }
1816
1817 if (pStreamResList) {
Tom Sepezae51c812015-08-05 12:34:06 -07001818 CPDF_Dictionary* pXObject = new CPDF_Dictionary;
tsepez7b1ccf92016-04-14 11:04:57 -07001819 pXObject->SetAtReference(sImageAlias, pDoc, pImage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001820 pStreamResList->SetAt("XObject", pXObject);
1821 }
Bo Xufdc00a72014-10-28 23:03:33 -07001822}
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001823
1824void CPDFSDK_Widget::RemoveAppearance(const CFX_ByteString& sAPType) {
tsepez7b1ccf92016-04-14 11:04:57 -07001825 if (CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP"))
1826 pAPDict->RemoveAt(sAPType);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001827}
1828
1829FX_BOOL CPDFSDK_Widget::OnAAction(CPDF_AAction::AActionType type,
1830 PDFSDK_FieldAction& data,
1831 CPDFSDK_PageView* pPageView) {
1832 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001833 CPDFDoc_Environment* pEnv = pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001834
Tom Sepez40e9ff32015-11-30 12:39:54 -08001835#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -08001836 CPDFXFA_Document* pDoc = pDocument->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -07001837 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001838 XFA_EVENTTYPE eEventType = GetXFAEventType(type, data.bWillCommit);
1839
1840 if (eEventType != XFA_EVENT_Unknown) {
dsinclairdf4bc592016-03-31 20:34:43 -07001841 if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001842 CXFA_EventParam param;
1843 param.m_eType = eEventType;
1844 param.m_wsChange = data.sChange;
1845 param.m_iCommitKey = data.nCommitKey;
1846 param.m_bShift = data.bShift;
1847 param.m_iSelStart = data.nSelStart;
1848 param.m_iSelEnd = data.nSelEnd;
1849 param.m_wsFullText = data.sValue;
1850 param.m_bKeyDown = data.bKeyDown;
1851 param.m_bModifier = data.bModifier;
1852 param.m_wsNewText = data.sValue;
1853 if (data.nSelEnd > data.nSelStart)
1854 param.m_wsNewText.Delete(data.nSelStart,
1855 data.nSelEnd - data.nSelStart);
1856 for (int i = data.sChange.GetLength() - 1; i >= 0; i--)
1857 param.m_wsNewText.Insert(data.nSelStart, data.sChange[i]);
1858 param.m_wsPrevText = data.sValue;
1859
dsinclair221caf62016-04-04 12:08:40 -07001860 CXFA_WidgetAcc* pAcc = hWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001861 param.m_pTarget = pAcc;
1862 int32_t nRet = pXFAWidgetHandler->ProcessEvent(pAcc, &param);
1863
dsinclairdf4bc592016-03-31 20:34:43 -07001864 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001865 pDocView->UpdateDocView();
1866 }
1867
Wei Lie98ac2e2016-03-18 15:43:04 -07001868 if (nRet == XFA_EVENTERROR_Success)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001869 return TRUE;
1870 }
1871 }
1872 }
Tom Sepez40e9ff32015-11-30 12:39:54 -08001873#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001874
1875 CPDF_Action action = GetAAction(type);
Wei Li0fc6b252016-03-01 16:29:41 -08001876 if (action.GetDict() && action.GetType() != CPDF_Action::Unknown) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001877 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
1878 return pActionHandler->DoAction_Field(action, type, pDocument,
1879 GetFormField(), data);
1880 }
1881 return FALSE;
1882}
1883
1884CPDF_Action CPDFSDK_Widget::GetAAction(CPDF_AAction::AActionType eAAT) {
1885 switch (eAAT) {
1886 case CPDF_AAction::CursorEnter:
1887 case CPDF_AAction::CursorExit:
1888 case CPDF_AAction::ButtonDown:
1889 case CPDF_AAction::ButtonUp:
1890 case CPDF_AAction::GetFocus:
1891 case CPDF_AAction::LoseFocus:
1892 case CPDF_AAction::PageOpen:
1893 case CPDF_AAction::PageClose:
1894 case CPDF_AAction::PageVisible:
1895 case CPDF_AAction::PageInvisible:
1896 return CPDFSDK_BAAnnot::GetAAction(eAAT);
1897
1898 case CPDF_AAction::KeyStroke:
1899 case CPDF_AAction::Format:
1900 case CPDF_AAction::Validate:
1901 case CPDF_AAction::Calculate: {
1902 CPDF_FormField* pField = GetFormField();
Wei Li0fc6b252016-03-01 16:29:41 -08001903 if (pField->GetAdditionalAction().GetDict())
1904 return pField->GetAdditionalAction().GetAction(eAAT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001905 return CPDFSDK_BAAnnot::GetAAction(eAAT);
1906 }
1907 default:
1908 break;
1909 }
1910
1911 return CPDF_Action();
1912}
1913
1914CFX_WideString CPDFSDK_Widget::GetAlternateName() const {
1915 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001916 return pFormField->GetAlternateName();
1917}
1918
1919int32_t CPDFSDK_Widget::GetAppearanceAge() const {
1920 return m_nAppAge;
1921}
1922
1923int32_t CPDFSDK_Widget::GetValueAge() const {
1924 return m_nValueAge;
1925}
1926
1927FX_BOOL CPDFSDK_Widget::HitTest(FX_FLOAT pageX, FX_FLOAT pageY) {
1928 CPDF_Annot* pAnnot = GetPDFAnnot();
1929 CFX_FloatRect annotRect;
1930 pAnnot->GetRect(annotRect);
1931 if (annotRect.Contains(pageX, pageY)) {
1932 if (!IsVisible())
1933 return FALSE;
1934
1935 int nFieldFlags = GetFieldFlags();
1936 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
1937 return FALSE;
1938
1939 return TRUE;
1940 }
1941 return FALSE;
1942}
1943
Tom Sepez51da0932015-11-25 16:05:49 -08001944#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -07001945CPDFSDK_XFAWidget::CPDFSDK_XFAWidget(CXFA_FFWidget* pAnnot,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001946 CPDFSDK_PageView* pPageView,
1947 CPDFSDK_InterForm* pInterForm)
Dan Sinclairf766ad22016-03-14 13:51:24 -04001948 : CPDFSDK_Annot(pPageView),
1949 m_pInterForm(pInterForm),
1950 m_hXFAWidget(pAnnot) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001951
1952FX_BOOL CPDFSDK_XFAWidget::IsXFAField() {
1953 return TRUE;
1954}
1955
1956CFX_ByteString CPDFSDK_XFAWidget::GetType() const {
1957 return FSDK_XFAWIDGET_TYPENAME;
1958}
1959
Tom Sepez3343d142015-11-02 09:54:54 -08001960CFX_FloatRect CPDFSDK_XFAWidget::GetRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001961 CFX_RectF rcBBox;
dsinclair221caf62016-04-04 12:08:40 -07001962 GetXFAWidget()->GetRect(rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001963 return CFX_FloatRect(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
1964 rcBBox.top + rcBBox.height);
1965}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001966#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001967
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001968CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_Document* pDocument)
1969 : m_pDocument(pDocument),
thestigdb1a24e2016-05-23 16:55:09 -07001970 m_pInterForm(new CPDF_InterForm(m_pDocument->GetPDFDocument())),
Tom Sepez51da0932015-11-25 16:05:49 -08001971#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001972 m_bXfaCalculate(TRUE),
1973 m_bXfaValidationsEnabled(TRUE),
Tom Sepez40e9ff32015-11-30 12:39:54 -08001974#endif // PDF_ENABLE_XFA
1975 m_bCalculate(TRUE),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001976 m_bBusy(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001977 m_pInterForm->SetFormNotify(this);
Tom Sepezae7a9172015-11-23 09:22:46 -08001978 for (int i = 0; i < kNumFieldTypes; ++i)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001979 m_bNeedHightlight[i] = FALSE;
1980 m_iHighlightAlpha = 0;
1981}
1982
1983CPDFSDK_InterForm::~CPDFSDK_InterForm() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001984 m_Map.clear();
Tom Sepez51da0932015-11-25 16:05:49 -08001985#ifdef PDF_ENABLE_XFA
tsepez16a20332016-04-22 14:00:47 -07001986 m_XFAMap.clear();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001987#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001988}
1989
1990FX_BOOL CPDFSDK_InterForm::HighlightWidgets() {
1991 return FALSE;
1992}
1993
1994CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget,
1995 FX_BOOL bNext) const {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08001996 std::unique_ptr<CBA_AnnotIterator> pIterator(
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001997 new CBA_AnnotIterator(pWidget->GetPageView(), "Widget", ""));
1998
1999 if (bNext) {
2000 return (CPDFSDK_Widget*)pIterator->GetNextAnnot(pWidget);
2001 }
2002 return (CPDFSDK_Widget*)pIterator->GetPrevAnnot(pWidget);
2003}
2004
2005CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const {
2006 if (!pControl || !m_pInterForm)
2007 return nullptr;
2008
2009 CPDFSDK_Widget* pWidget = nullptr;
2010 const auto it = m_Map.find(pControl);
2011 if (it != m_Map.end())
2012 pWidget = it->second;
2013
2014 if (pWidget)
2015 return pWidget;
2016
2017 CPDF_Dictionary* pControlDict = pControl->GetWidget();
Tom Sepez50d12ad2015-11-24 09:50:51 -08002018 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002019 CPDFSDK_PageView* pPage = nullptr;
2020
Wei Li9b761132016-01-29 15:44:20 -08002021 if (CPDF_Dictionary* pPageDict = pControlDict->GetDictBy("P")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002022 int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
2023 if (nPageIndex >= 0) {
2024 pPage = m_pDocument->GetPageView(nPageIndex);
2025 }
2026 }
2027
2028 if (!pPage) {
2029 int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict);
2030 if (nPageIndex >= 0) {
2031 pPage = m_pDocument->GetPageView(nPageIndex);
2032 }
2033 }
2034
2035 if (!pPage)
2036 return nullptr;
2037 return (CPDFSDK_Widget*)pPage->GetAnnotByDict(pControlDict);
2038}
2039
Lei Zhangd88a3642015-11-10 09:38:57 -08002040void CPDFSDK_InterForm::GetWidgets(
2041 const CFX_WideString& sFieldName,
2042 std::vector<CPDFSDK_Widget*>* widgets) const {
2043 for (int i = 0, sz = m_pInterForm->CountFields(sFieldName); i < sz; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002044 CPDF_FormField* pFormField = m_pInterForm->GetField(i, sFieldName);
Lei Zhangd88a3642015-11-10 09:38:57 -08002045 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002046 GetWidgets(pFormField, widgets);
2047 }
2048}
2049
Lei Zhangd88a3642015-11-10 09:38:57 -08002050void CPDFSDK_InterForm::GetWidgets(
2051 CPDF_FormField* pField,
2052 std::vector<CPDFSDK_Widget*>* widgets) const {
2053 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002054 CPDF_FormControl* pFormCtrl = pField->GetControl(i);
Lei Zhangd88a3642015-11-10 09:38:57 -08002055 ASSERT(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002056 CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002057 if (pWidget)
Lei Zhangd88a3642015-11-10 09:38:57 -08002058 widgets->push_back(pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002059 }
2060}
2061
2062int CPDFSDK_InterForm::GetPageIndexByAnnotDict(
2063 CPDF_Document* pDocument,
2064 CPDF_Dictionary* pAnnotDict) const {
Lei Zhang96660d62015-12-14 18:27:25 -08002065 ASSERT(pAnnotDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002066
2067 for (int i = 0, sz = pDocument->GetPageCount(); i < sz; i++) {
2068 if (CPDF_Dictionary* pPageDict = pDocument->GetPage(i)) {
Wei Li9b761132016-01-29 15:44:20 -08002069 if (CPDF_Array* pAnnots = pPageDict->GetArrayBy("Annots")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002070 for (int j = 0, jsz = pAnnots->GetCount(); j < jsz; j++) {
tsepezbd567552016-03-29 14:51:50 -07002071 CPDF_Object* pDict = pAnnots->GetDirectObjectAt(j);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002072 if (pAnnotDict == pDict) {
2073 return i;
2074 }
2075 }
2076 }
2077 }
2078 }
2079
2080 return -1;
2081}
2082
2083void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl,
2084 CPDFSDK_Widget* pWidget) {
2085 m_Map[pControl] = pWidget;
2086}
2087
2088void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl) {
2089 m_Map.erase(pControl);
2090}
2091
Tom Sepez40e9ff32015-11-30 12:39:54 -08002092void CPDFSDK_InterForm::EnableCalculate(FX_BOOL bEnabled) {
2093 m_bCalculate = bEnabled;
2094}
2095
2096FX_BOOL CPDFSDK_InterForm::IsCalculateEnabled() const {
2097 return m_bCalculate;
2098}
2099
Tom Sepez51da0932015-11-25 16:05:49 -08002100#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -07002101void CPDFSDK_InterForm::AddXFAMap(CXFA_FFWidget* hWidget,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002102 CPDFSDK_XFAWidget* pWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002103 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002104 m_XFAMap[hWidget] = pWidget;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002105}
2106
dsinclairdf4bc592016-03-31 20:34:43 -07002107void CPDFSDK_InterForm::RemoveXFAMap(CXFA_FFWidget* hWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002108 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002109 m_XFAMap.erase(hWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002110}
2111
dsinclairdf4bc592016-03-31 20:34:43 -07002112CPDFSDK_XFAWidget* CPDFSDK_InterForm::GetXFAWidget(CXFA_FFWidget* hWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002113 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002114 auto it = m_XFAMap.find(hWidget);
2115 return it != m_XFAMap.end() ? it->second : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002116}
2117
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002118void CPDFSDK_InterForm::XfaEnableCalculate(FX_BOOL bEnabled) {
2119 m_bXfaCalculate = bEnabled;
2120}
2121FX_BOOL CPDFSDK_InterForm::IsXfaCalculateEnabled() const {
2122 return m_bXfaCalculate;
2123}
2124
2125FX_BOOL CPDFSDK_InterForm::IsXfaValidationsEnabled() {
2126 return m_bXfaValidationsEnabled;
2127}
2128void CPDFSDK_InterForm::XfaSetValidationsEnabled(FX_BOOL bEnabled) {
2129 m_bXfaValidationsEnabled = bEnabled;
Bo Xufdc00a72014-10-28 23:03:33 -07002130}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002131#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -07002132
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002133void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002134 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2135 ASSERT(pEnv);
2136 if (!pEnv->IsJSInitiated())
2137 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002138
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002139 if (m_bBusy)
2140 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002141
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002142 m_bBusy = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002143
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002144 if (IsCalculateEnabled()) {
Tom Sepezba038bc2015-10-08 12:03:00 -07002145 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002146 pRuntime->SetReaderDocument(m_pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002147
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002148 int nSize = m_pInterForm->CountFieldsInCalculationOrder();
2149 for (int i = 0; i < nSize; i++) {
2150 if (CPDF_FormField* pField =
2151 m_pInterForm->GetFieldInCalculationOrder(i)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002152 int nType = pField->GetFieldType();
2153 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2154 CPDF_AAction aAction = pField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002155 if (aAction.GetDict() &&
2156 aAction.ActionExist(CPDF_AAction::Calculate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002157 CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate);
Wei Li0fc6b252016-03-01 16:29:41 -08002158 if (action.GetDict()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002159 CFX_WideString csJS = action.GetJavaScript();
2160 if (!csJS.IsEmpty()) {
Tom Sepezba038bc2015-10-08 12:03:00 -07002161 IJS_Context* pContext = pRuntime->NewContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002162 CFX_WideString sOldValue = pField->GetValue();
2163 CFX_WideString sValue = sOldValue;
2164 FX_BOOL bRC = TRUE;
2165 pContext->OnField_Calculate(pFormField, pField, sValue, bRC);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002166
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002167 CFX_WideString sInfo;
Tom Sepez33420902015-10-13 15:00:10 -07002168 FX_BOOL bRet = pContext->RunScript(csJS, &sInfo);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002169 pRuntime->ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002170
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002171 if (bRet) {
2172 if (bRC) {
2173 if (sValue.Compare(sOldValue) != 0)
2174 pField->SetValue(sValue, TRUE);
2175 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002176 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002177 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002178 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002179 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002180 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002181 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002182 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002183 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002184
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002185 m_bBusy = FALSE;
2186}
2187
2188CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField,
2189 FX_BOOL& bFormated) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002190 CFX_WideString sValue = pFormField->GetValue();
2191 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2192 ASSERT(pEnv);
2193 if (!pEnv->IsJSInitiated()) {
2194 bFormated = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002195 return sValue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002196 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002197
Tom Sepezba038bc2015-10-08 12:03:00 -07002198 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002199 pRuntime->SetReaderDocument(m_pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002200
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002201 if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX) {
2202 if (pFormField->CountSelectedItems() > 0) {
2203 int index = pFormField->GetSelectedIndex(0);
2204 if (index >= 0)
2205 sValue = pFormField->GetOptionLabel(index);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002206 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002207 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002208
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002209 bFormated = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002210
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002211 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002212 if (aAction.GetDict() && aAction.ActionExist(CPDF_AAction::Format)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002213 CPDF_Action action = aAction.GetAction(CPDF_AAction::Format);
Wei Li0fc6b252016-03-01 16:29:41 -08002214 if (action.GetDict()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002215 CFX_WideString script = action.GetJavaScript();
2216 if (!script.IsEmpty()) {
2217 CFX_WideString Value = sValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002218
Tom Sepezba038bc2015-10-08 12:03:00 -07002219 IJS_Context* pContext = pRuntime->NewContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002220 pContext->OnField_Format(pFormField, Value, TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002221
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002222 CFX_WideString sInfo;
Tom Sepez33420902015-10-13 15:00:10 -07002223 FX_BOOL bRet = pContext->RunScript(script, &sInfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002224 pRuntime->ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002225
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002226 if (bRet) {
2227 sValue = Value;
2228 bFormated = TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002229 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002230 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002231 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002232 }
2233
2234 return sValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002235}
2236
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002237void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField,
2238 const FX_WCHAR* sValue,
2239 FX_BOOL bValueChanged) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002240 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2241 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang96660d62015-12-14 18:27:25 -08002242 ASSERT(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002243 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
2244 pWidget->ResetAppearance(sValue, bValueChanged);
2245 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002246}
2247
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002248void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002249 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2250 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang96660d62015-12-14 18:27:25 -08002251 ASSERT(pFormCtrl);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002252
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002253 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
2254 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2255 CFFL_IFormFiller* pIFormFiller = pEnv->GetIFormFiller();
Tom Sepez540c4362015-11-24 13:33:57 -08002256 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002257 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002258 FX_RECT rcBBox = pIFormFiller->GetViewBBox(pPageView, pWidget);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002259
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002260 pEnv->FFI_Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right,
2261 rcBBox.bottom);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002262 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002263 }
2264}
2265
Tom Sepezd6262c12016-02-03 14:47:06 -08002266FX_BOOL CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField,
2267 const CFX_WideString& csValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002268 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002269 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::KeyStroke))
Tom Sepezd6262c12016-02-03 14:47:06 -08002270 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002271
Tom Sepezd6262c12016-02-03 14:47:06 -08002272 CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke);
Wei Li0fc6b252016-03-01 16:29:41 -08002273 if (!action.GetDict())
Tom Sepezd6262c12016-02-03 14:47:06 -08002274 return TRUE;
2275
2276 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2277 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2278 PDFSDK_FieldAction fa;
2279 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2280 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2281 fa.sValue = csValue;
2282 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::KeyStroke,
2283 m_pDocument, pFormField, fa);
2284 return fa.bRC;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002285}
2286
Tom Sepezd6262c12016-02-03 14:47:06 -08002287FX_BOOL CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField,
2288 const CFX_WideString& csValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002289 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002290 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Validate))
Tom Sepezd6262c12016-02-03 14:47:06 -08002291 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002292
Tom Sepezd6262c12016-02-03 14:47:06 -08002293 CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate);
Wei Li0fc6b252016-03-01 16:29:41 -08002294 if (!action.GetDict())
Tom Sepezd6262c12016-02-03 14:47:06 -08002295 return TRUE;
2296
2297 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2298 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2299 PDFSDK_FieldAction fa;
2300 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2301 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2302 fa.sValue = csValue;
2303 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::Validate,
2304 m_pDocument, pFormField, fa);
2305 return fa.bRC;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002306}
2307
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002308FX_BOOL CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action) {
Wei Li0fc6b252016-03-01 16:29:41 -08002309 ASSERT(action.GetDict());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002310
Wei Li0fc6b252016-03-01 16:29:41 -08002311 CPDF_ActionFields af(&action);
Lei Zhangd88a3642015-11-10 09:38:57 -08002312 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2313 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002314
2315 FX_BOOL bHide = action.GetHideStatus();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002316 FX_BOOL bChanged = FALSE;
2317
Lei Zhangd88a3642015-11-10 09:38:57 -08002318 for (CPDF_FormField* pField : fields) {
2319 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
2320 CPDF_FormControl* pControl = pField->GetControl(i);
2321 ASSERT(pControl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002322
2323 if (CPDFSDK_Widget* pWidget = GetWidget(pControl)) {
tsepezc3255f52016-03-25 14:52:27 -07002324 uint32_t nFlags = pWidget->GetFlags();
Lei Zhangd88a3642015-11-10 09:38:57 -08002325 nFlags &= ~ANNOTFLAG_INVISIBLE;
2326 nFlags &= ~ANNOTFLAG_NOVIEW;
2327 if (bHide)
2328 nFlags |= ANNOTFLAG_HIDDEN;
2329 else
2330 nFlags &= ~ANNOTFLAG_HIDDEN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002331 pWidget->SetFlags(nFlags);
Lei Zhangd88a3642015-11-10 09:38:57 -08002332 pWidget->GetPageView()->UpdateView(pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002333 bChanged = TRUE;
2334 }
2335 }
2336 }
2337
2338 return bChanged;
2339}
2340
2341FX_BOOL CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002342 CFX_WideString sDestination = action.GetFilePath();
2343 if (sDestination.IsEmpty())
2344 return FALSE;
2345
2346 CPDF_Dictionary* pActionDict = action.GetDict();
2347 if (pActionDict->KeyExist("Fields")) {
Wei Li0fc6b252016-03-01 16:29:41 -08002348 CPDF_ActionFields af(&action);
tsepezc3255f52016-03-25 14:52:27 -07002349 uint32_t dwFlags = action.GetFlags();
Lei Zhangd88a3642015-11-10 09:38:57 -08002350 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2351 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
2352 if (!fields.empty()) {
2353 bool bIncludeOrExclude = !(dwFlags & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002354 if (m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002355 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002356
Wei Li97da9762016-03-11 17:00:48 -08002357 return SubmitFields(sDestination, fields, bIncludeOrExclude, false);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002358 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002359 }
Lei Zhangd88a3642015-11-10 09:38:57 -08002360 if (m_pInterForm->CheckRequiredFields(nullptr, true))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002361 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002362
2363 return SubmitForm(sDestination, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002364}
2365
Lei Zhangd88a3642015-11-10 09:38:57 -08002366FX_BOOL CPDFSDK_InterForm::SubmitFields(
2367 const CFX_WideString& csDestination,
2368 const std::vector<CPDF_FormField*>& fields,
Wei Li97da9762016-03-11 17:00:48 -08002369 bool bIncludeOrExclude,
2370 bool bUrlEncoded) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002371 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002372
2373 CFX_ByteTextBuf textBuf;
2374 ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf);
2375
2376 uint8_t* pBuffer = textBuf.GetBuffer();
2377 FX_STRSIZE nBufSize = textBuf.GetLength();
2378
Lei Zhangd88a3642015-11-10 09:38:57 -08002379 if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize))
2380 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002381
2382 pEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002383 return TRUE;
2384}
2385
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002386FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile,
2387 CFX_WideString csTxtFile) {
2388 return TRUE;
2389}
2390
2391FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf,
2392 FX_STRSIZE& nBufSize) {
2393 CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize);
2394 if (pFDF) {
Wei Li9b761132016-01-29 15:44:20 -08002395 CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictBy("FDF");
Lei Zhang997de612015-11-04 18:17:53 -08002396 if (!pMainDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002397 return FALSE;
2398
2399 // Get fields
Wei Li9b761132016-01-29 15:44:20 -08002400 CPDF_Array* pFields = pMainDict->GetArrayBy("Fields");
Lei Zhang997de612015-11-04 18:17:53 -08002401 if (!pFields)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002402 return FALSE;
2403
2404 CFX_ByteTextBuf fdfEncodedData;
tsepezc3255f52016-03-25 14:52:27 -07002405 for (uint32_t i = 0; i < pFields->GetCount(); i++) {
Wei Li9b761132016-01-29 15:44:20 -08002406 CPDF_Dictionary* pField = pFields->GetDictAt(i);
Lei Zhang997de612015-11-04 18:17:53 -08002407 if (!pField)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002408 continue;
2409 CFX_WideString name;
Wei Li9b761132016-01-29 15:44:20 -08002410 name = pField->GetUnicodeTextBy("T");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002411 CFX_ByteString name_b = CFX_ByteString::FromUnicode(name);
Wei Li9b761132016-01-29 15:44:20 -08002412 CFX_ByteString csBValue = pField->GetStringBy("V");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002413 CFX_WideString csWValue = PDF_DecodeText(csBValue);
2414 CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue);
2415
Tom Sepez052a8d92016-02-19 14:41:46 -08002416 fdfEncodedData << name_b.GetBuffer(name_b.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002417 name_b.ReleaseBuffer();
Tom Sepez052a8d92016-02-19 14:41:46 -08002418 fdfEncodedData << "=";
2419 fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002420 csValue_b.ReleaseBuffer();
2421 if (i != pFields->GetCount() - 1)
Tom Sepez052a8d92016-02-19 14:41:46 -08002422 fdfEncodedData << "&";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002423 }
2424
2425 nBufSize = fdfEncodedData.GetLength();
2426 pBuf = FX_Alloc(uint8_t, nBufSize);
2427 FXSYS_memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize);
2428 }
2429 return TRUE;
2430}
2431
Lei Zhangd88a3642015-11-10 09:38:57 -08002432FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(
2433 const std::vector<CPDF_FormField*>& fields,
Wei Li97da9762016-03-11 17:00:48 -08002434 bool bIncludeOrExclude,
Lei Zhangd88a3642015-11-10 09:38:57 -08002435 CFX_ByteTextBuf& textBuf) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08002436 std::unique_ptr<CFDF_Document> pFDF(m_pInterForm->ExportToFDF(
tsepez4c3debb2016-04-08 12:20:38 -07002437 m_pDocument->GetPath().AsStringC(), fields, bIncludeOrExclude));
Lei Zhangd88a3642015-11-10 09:38:57 -08002438 return pFDF ? pFDF->WriteBuf(textBuf) : FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002439}
2440
Tom Sepez51da0932015-11-25 16:05:49 -08002441#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002442void CPDFSDK_InterForm::SynchronizeField(CPDF_FormField* pFormField,
2443 FX_BOOL bSynchronizeElse) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002444 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2445 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002446 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
2447 pWidget->Synchronize(bSynchronizeElse);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002448 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002449 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002450}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002451#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002452
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002453CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName(
2454 const CFX_WideString& sFileExt) {
2455 CFX_WideString sFileName;
2456 return L"";
2457}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002458
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002459FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination,
2460 FX_BOOL bUrlEncoded) {
2461 if (sDestination.IsEmpty())
2462 return FALSE;
2463
Lei Zhang96660d62015-12-14 18:27:25 -08002464 if (!m_pDocument || !m_pInterForm)
2465 return FALSE;
2466
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002467 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002468 CFX_WideString wsPDFFilePath = m_pDocument->GetPath();
tsepez4c3debb2016-04-08 12:20:38 -07002469 CFDF_Document* pFDFDoc = m_pInterForm->ExportToFDF(wsPDFFilePath.AsStringC());
Lei Zhang997de612015-11-04 18:17:53 -08002470 if (!pFDFDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002471 return FALSE;
2472
2473 CFX_ByteTextBuf FdfBuffer;
2474 FX_BOOL bRet = pFDFDoc->WriteBuf(FdfBuffer);
2475 delete pFDFDoc;
2476 if (!bRet)
2477 return FALSE;
2478
2479 uint8_t* pBuffer = FdfBuffer.GetBuffer();
2480 FX_STRSIZE nBufSize = FdfBuffer.GetLength();
2481
2482 if (bUrlEncoded) {
2483 if (!FDFToURLEncodedData(pBuffer, nBufSize))
2484 return FALSE;
2485 }
2486
2487 pEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
2488
Lei Zhangda180e92015-08-14 22:22:13 -07002489 if (bUrlEncoded) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002490 FX_Free(pBuffer);
2491 pBuffer = NULL;
2492 }
2493
2494 return TRUE;
2495}
2496
2497FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) {
tsepezfc58ad12016-04-05 12:22:15 -07002498 CFDF_Document* pFDF =
tsepez4c3debb2016-04-08 12:20:38 -07002499 m_pInterForm->ExportToFDF(m_pDocument->GetPath().AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002500 if (!pFDF)
2501 return FALSE;
2502
2503 FX_BOOL bRet = pFDF->WriteBuf(textBuf);
2504 delete pFDF;
2505
2506 return bRet;
2507}
2508
2509FX_BOOL CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) {
Wei Li0fc6b252016-03-01 16:29:41 -08002510 ASSERT(action.GetDict());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002511
2512 CPDF_Dictionary* pActionDict = action.GetDict();
Lei Zhangd88a3642015-11-10 09:38:57 -08002513 if (!pActionDict->KeyExist("Fields"))
2514 return m_pInterForm->ResetForm(true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002515
Wei Li0fc6b252016-03-01 16:29:41 -08002516 CPDF_ActionFields af(&action);
tsepezc3255f52016-03-25 14:52:27 -07002517 uint32_t dwFlags = action.GetFlags();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002518
Lei Zhangd88a3642015-11-10 09:38:57 -08002519 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2520 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
2521 return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002522}
2523
2524FX_BOOL CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action) {
2525 return FALSE;
2526}
2527
Lei Zhangd88a3642015-11-10 09:38:57 -08002528std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects(
2529 const std::vector<CPDF_Object*>& objects) const {
2530 std::vector<CPDF_FormField*> fields;
2531 for (CPDF_Object* pObject : objects) {
2532 if (pObject && pObject->IsString()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002533 CFX_WideString csName = pObject->GetUnicodeText();
2534 CPDF_FormField* pField = m_pInterForm->GetField(0, csName);
Dan Sinclaird43ebdd2015-10-27 15:37:54 -04002535 if (pField)
Lei Zhangd88a3642015-11-10 09:38:57 -08002536 fields.push_back(pField);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002537 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002538 }
Lei Zhangd88a3642015-11-10 09:38:57 -08002539 return fields;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002540}
2541
Tom Sepezd6262c12016-02-03 14:47:06 -08002542int CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField,
2543 const CFX_WideString& csValue) {
2544 int nType = pField->GetFieldType();
2545 if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002546 return 0;
2547
Tom Sepezd6262c12016-02-03 14:47:06 -08002548 if (!OnKeyStrokeCommit(pField, csValue))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002549 return -1;
2550
Tom Sepezd6262c12016-02-03 14:47:06 -08002551 if (!OnValidate(pField, csValue))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002552 return -1;
2553
2554 return 1;
2555}
2556
Tom Sepezd6262c12016-02-03 14:47:06 -08002557void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) {
2558#ifdef PDF_ENABLE_XFA
2559 SynchronizeField(pField, FALSE);
2560#endif // PDF_ENABLE_XFA
2561 int nType = pField->GetFieldType();
2562 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2563 OnCalculate(pField);
2564 FX_BOOL bFormated = FALSE;
2565 CFX_WideString sValue = OnFormat(pField, bFormated);
2566 ResetFieldAppearance(pField, bFormated ? sValue.c_str() : nullptr, TRUE);
2567 UpdateField(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002568 }
Tom Sepezd6262c12016-02-03 14:47:06 -08002569}
2570
2571int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField,
2572 const CFX_WideString& csValue) {
2573 if (pField->GetFieldType() != FIELDTYPE_LISTBOX)
2574 return 0;
2575
2576 if (!OnKeyStrokeCommit(pField, csValue))
2577 return -1;
2578
2579 if (!OnValidate(pField, csValue))
2580 return -1;
2581
2582 return 1;
2583}
2584
2585void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) {
2586 if (pField->GetFieldType() == FIELDTYPE_LISTBOX) {
2587 OnCalculate(pField);
2588 ResetFieldAppearance(pField, NULL, TRUE);
2589 UpdateField(pField);
2590 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002591}
2592
Tom Sepezed5d7aa2016-02-02 16:02:03 -08002593void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) {
2594 int nType = pField->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002595 if (nType == FIELDTYPE_CHECKBOX || nType == FIELDTYPE_RADIOBUTTON) {
Tom Sepezed5d7aa2016-02-02 16:02:03 -08002596 OnCalculate(pField);
2597 UpdateField(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002598 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002599}
2600
Tom Sepezd6262c12016-02-03 14:47:06 -08002601int CPDFSDK_InterForm::BeforeFormReset(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002602 return 0;
2603}
2604
Tom Sepezd6262c12016-02-03 14:47:06 -08002605void CPDFSDK_InterForm::AfterFormReset(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002606 OnCalculate(nullptr);
Tom Sepezd6262c12016-02-03 14:47:06 -08002607}
2608
2609int CPDFSDK_InterForm::BeforeFormImportData(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002610 return 0;
2611}
2612
Tom Sepezd6262c12016-02-03 14:47:06 -08002613void CPDFSDK_InterForm::AfterFormImportData(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002614 OnCalculate(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002615}
2616
2617FX_BOOL CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002618 if (nFieldType < 1 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002619 return FALSE;
2620 return m_bNeedHightlight[nFieldType - 1];
2621}
2622
2623void CPDFSDK_InterForm::RemoveAllHighLight() {
Tom Sepezae7a9172015-11-23 09:22:46 -08002624 for (int i = 0; i < kNumFieldTypes; ++i)
2625 m_bNeedHightlight[i] = FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002626}
Tom Sepezae7a9172015-11-23 09:22:46 -08002627
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002628void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002629 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002630 return;
2631 switch (nFieldType) {
2632 case 0: {
Tom Sepezae7a9172015-11-23 09:22:46 -08002633 for (int i = 0; i < kNumFieldTypes; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002634 m_aHighlightColor[i] = clr;
2635 m_bNeedHightlight[i] = TRUE;
2636 }
2637 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002638 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002639 default: {
2640 m_aHighlightColor[nFieldType - 1] = clr;
2641 m_bNeedHightlight[nFieldType - 1] = TRUE;
2642 break;
2643 }
2644 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002645}
2646
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002647FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002648 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002649 return FXSYS_RGB(255, 255, 255);
2650 if (nFieldType == 0)
2651 return m_aHighlightColor[0];
2652 return m_aHighlightColor[nFieldType - 1];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002653}
2654
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002655CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView,
2656 const CFX_ByteString& sType,
2657 const CFX_ByteString& sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002658 : m_eTabOrder(STRUCTURE),
2659 m_pPageView(pPageView),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002660 m_sType(sType),
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002661 m_sSubType(sSubType) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002662 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage();
Wei Li9b761132016-01-29 15:44:20 -08002663 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringBy("Tabs");
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002664 if (sTabs == "R")
2665 m_eTabOrder = ROW;
2666 else if (sTabs == "C")
2667 m_eTabOrder = COLUMN;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002668
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002669 GenerateResults();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002670}
2671
Dan Sinclairf766ad22016-03-14 13:51:24 -04002672CBA_AnnotIterator::~CBA_AnnotIterator() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002673
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002674CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002675 return m_Annots.empty() ? nullptr : m_Annots.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002676}
2677
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002678CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002679 return m_Annots.empty() ? nullptr : m_Annots.back();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002680}
2681
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002682CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002683 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2684 if (iter == m_Annots.end())
2685 return nullptr;
2686 ++iter;
2687 if (iter == m_Annots.end())
2688 iter = m_Annots.begin();
2689 return *iter;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002690}
2691
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002692CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002693 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2694 if (iter == m_Annots.end())
2695 return nullptr;
2696 if (iter == m_Annots.begin())
2697 iter = m_Annots.end();
2698 return *(--iter);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002699}
2700
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002701// static
2702bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1,
2703 const CPDFSDK_Annot* p2) {
2704 return GetAnnotRect(p1).left < GetAnnotRect(p2).left;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002705}
2706
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002707// static
2708bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1,
2709 const CPDFSDK_Annot* p2) {
2710 return GetAnnotRect(p1).top > GetAnnotRect(p2).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002711}
2712
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002713void CBA_AnnotIterator::GenerateResults() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002714 switch (m_eTabOrder) {
2715 case STRUCTURE: {
Lei Zhangbf60b292015-10-26 12:14:35 -07002716 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002717 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002718 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002719 m_Annots.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002720 }
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002721 } break;
2722 case ROW: {
2723 std::vector<CPDFSDK_Annot*> sa;
Lei Zhang50218532015-10-30 14:03:43 -07002724 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2725 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2726 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002727 sa.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002728 }
2729
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002730 std::sort(sa.begin(), sa.end(), CompareByLeftAscending);
2731 while (!sa.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002732 int nLeftTopIndex = -1;
Lei Zhang50218532015-10-30 14:03:43 -07002733 FX_FLOAT fTop = 0.0f;
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002734 for (int i = sa.size() - 1; i >= 0; i--) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002735 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002736 if (rcAnnot.top > fTop) {
2737 nLeftTopIndex = i;
2738 fTop = rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002739 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002740 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002741 if (nLeftTopIndex >= 0) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002742 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
Tom Sepez281a9ea2016-02-26 14:24:28 -08002743 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002744 m_Annots.push_back(pLeftTopAnnot);
2745 sa.erase(sa.begin() + nLeftTopIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002746
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002747 std::vector<int> aSelect;
Wei Li89409932016-03-28 10:33:33 -07002748 for (size_t i = 0; i < sa.size(); ++i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002749 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002750 FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f;
2751 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002752 aSelect.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002753 }
Wei Li89409932016-03-28 10:33:33 -07002754 for (size_t i = 0; i < aSelect.size(); ++i)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002755 m_Annots.push_back(sa[aSelect[i]]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002756
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002757 for (int i = aSelect.size() - 1; i >= 0; --i)
2758 sa.erase(sa.begin() + aSelect[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002759 }
2760 }
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002761 } break;
2762 case COLUMN: {
2763 std::vector<CPDFSDK_Annot*> sa;
Lei Zhang50218532015-10-30 14:03:43 -07002764 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2765 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2766 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002767 sa.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002768 }
2769
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002770 std::sort(sa.begin(), sa.end(), CompareByTopDescending);
2771 while (!sa.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002772 int nLeftTopIndex = -1;
Lei Zhang50218532015-10-30 14:03:43 -07002773 FX_FLOAT fLeft = -1.0f;
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002774 for (int i = sa.size() - 1; i >= 0; --i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002775 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002776 if (fLeft < 0) {
2777 nLeftTopIndex = 0;
2778 fLeft = rcAnnot.left;
2779 } else if (rcAnnot.left < fLeft) {
2780 nLeftTopIndex = i;
2781 fLeft = rcAnnot.left;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002782 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002783 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002784
2785 if (nLeftTopIndex >= 0) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002786 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
Tom Sepez281a9ea2016-02-26 14:24:28 -08002787 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002788 m_Annots.push_back(pLeftTopAnnot);
2789 sa.erase(sa.begin() + nLeftTopIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002790
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002791 std::vector<int> aSelect;
Wei Li89409932016-03-28 10:33:33 -07002792 for (size_t i = 0; i < sa.size(); ++i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002793 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002794 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f;
2795 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002796 aSelect.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002797 }
Wei Li89409932016-03-28 10:33:33 -07002798 for (size_t i = 0; i < aSelect.size(); ++i)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002799 m_Annots.push_back(sa[aSelect[i]]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002800
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002801 for (int i = aSelect.size() - 1; i >= 0; --i)
2802 sa.erase(sa.begin() + aSelect[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002803 }
2804 }
Lei Zhang50218532015-10-30 14:03:43 -07002805 break;
2806 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002807 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002808}
2809
Tom Sepez281a9ea2016-02-26 14:24:28 -08002810CFX_FloatRect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) {
2811 CFX_FloatRect rcAnnot;
Lei Zhang50218532015-10-30 14:03:43 -07002812 pAnnot->GetPDFAnnot()->GetRect(rcAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002813 return rcAnnot;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002814}