blob: 46e9c46fb1ef09db432fa61ad7d937d4e303393f [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();
883 int32_t nBorderStyle = 0;
884 CPWL_Dash dsBorder(3, 0, 0);
885 CPWL_Color crLeftTop, crRightBottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700886
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887 switch (GetBorderStyle()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700888 case BBS_DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 nBorderStyle = PBS_DASH;
890 dsBorder = CPWL_Dash(3, 3, 0);
891 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700892 case BBS_BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 nBorderStyle = PBS_BEVELED;
894 fBorderWidth *= 2;
895 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
896 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
897 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700898 case BBS_INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 nBorderStyle = PBS_INSET;
900 fBorderWidth *= 2;
901 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
902 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
903 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700904 case BBS_UNDERLINE:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700905 nBorderStyle = PBS_UNDERLINED;
906 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700907 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 nBorderStyle = PBS_SOLID;
909 break;
910 }
911
Tom Sepez281a9ea2016-02-26 14:24:28 -0800912 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700913
914 CPWL_Color crText(COLORTYPE_GRAY, 0);
915
916 FX_FLOAT fFontSize = 12.0f;
917 CFX_ByteString csNameTag;
918
919 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
920 if (da.HasColor()) {
921 da.GetColor(iColorType, fc);
922 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
923 }
924
925 if (da.HasFont())
926 da.GetFont(csNameTag, fFontSize);
927
928 CFX_WideString csWCaption;
929 CFX_WideString csNormalCaption, csRolloverCaption, csDownCaption;
930
931 if (pControl->HasMKEntry("CA")) {
932 csNormalCaption = pControl->GetNormalCaption();
933 }
934 if (pControl->HasMKEntry("RC")) {
935 csRolloverCaption = pControl->GetRolloverCaption();
936 }
937 if (pControl->HasMKEntry("AC")) {
938 csDownCaption = pControl->GetDownCaption();
939 }
940
941 CPDF_Stream* pNormalIcon = NULL;
942 CPDF_Stream* pRolloverIcon = NULL;
943 CPDF_Stream* pDownIcon = NULL;
944
945 if (pControl->HasMKEntry("I")) {
946 pNormalIcon = pControl->GetNormalIcon();
947 }
948 if (pControl->HasMKEntry("RI")) {
949 pRolloverIcon = pControl->GetRolloverIcon();
950 }
951 if (pControl->HasMKEntry("IX")) {
952 pDownIcon = pControl->GetDownIcon();
953 }
954
955 if (pNormalIcon) {
956 if (CPDF_Dictionary* pImageDict = pNormalIcon->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", "ImgA");
959 }
960 }
961
962 if (pRolloverIcon) {
963 if (CPDF_Dictionary* pImageDict = pRolloverIcon->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", "ImgB");
966 }
967 }
968
969 if (pDownIcon) {
970 if (CPDF_Dictionary* pImageDict = pDownIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800971 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700972 pImageDict->SetAtString("Name", "ImgC");
973 }
974 }
975
976 CPDF_IconFit iconFit = pControl->GetIconFit();
977
978 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700979 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
980
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800981 CBA_FontMap font_map(this, pEnv->GetSysHandler());
982 font_map.SetAPType("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983
984 CFX_ByteString csAP =
985 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
986 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
987 crLeftTop, crRightBottom, nBorderStyle,
988 dsBorder) +
989 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -0800990 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700991 pNormalIcon, iconFit, csNormalCaption, crText, fFontSize, nLayout);
992
993 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP);
994 if (pNormalIcon)
995 AddImageToAppearance("N", pNormalIcon);
996
997 CPDF_FormControl::HighlightingMode eHLM = pControl->GetHighlightingMode();
998 if (eHLM == CPDF_FormControl::Push || eHLM == CPDF_FormControl::Toggle) {
999 if (csRolloverCaption.IsEmpty() && !pRolloverIcon) {
1000 csRolloverCaption = csNormalCaption;
1001 pRolloverIcon = pNormalIcon;
1002 }
1003
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001004 font_map.SetAPType("R");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005
1006 csAP = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1007 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1008 crLeftTop, crRightBottom,
1009 nBorderStyle, dsBorder) +
1010 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001011 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012 pRolloverIcon, iconFit, csRolloverCaption, crText, fFontSize,
1013 nLayout);
1014
1015 WriteAppearance("R", GetRotatedRect(), GetMatrix(), csAP);
1016 if (pRolloverIcon)
1017 AddImageToAppearance("R", pRolloverIcon);
1018
1019 if (csDownCaption.IsEmpty() && !pDownIcon) {
1020 csDownCaption = csNormalCaption;
1021 pDownIcon = pNormalIcon;
1022 }
1023
1024 switch (nBorderStyle) {
1025 case PBS_BEVELED: {
1026 CPWL_Color crTemp = crLeftTop;
1027 crLeftTop = crRightBottom;
1028 crRightBottom = crTemp;
1029 } break;
1030 case PBS_INSET:
1031 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1032 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001033 break;
1034 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001035
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001036 font_map.SetAPType("D");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001037
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038 csAP = CPWL_Utils::GetRectFillAppStream(
1039 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1040 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1041 crLeftTop, crRightBottom,
1042 nBorderStyle, dsBorder) +
1043 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001044 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045 pDownIcon, iconFit, csDownCaption, crText, fFontSize, nLayout);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001046
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001047 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001048 if (pDownIcon)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 AddImageToAppearance("D", pDownIcon);
1050 } else {
1051 RemoveAppearance("D");
1052 RemoveAppearance("R");
1053 }
1054}
1055
1056void CPDFSDK_Widget::ResetAppearance_CheckBox() {
1057 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058 CPWL_Color crBackground, crBorder, crText;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 int iColorType;
1060 FX_FLOAT fc[4];
1061
1062 pControl->GetOriginalBackgroundColor(iColorType, fc);
1063 if (iColorType > 0)
1064 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1065
1066 pControl->GetOriginalBorderColor(iColorType, fc);
1067 if (iColorType > 0)
1068 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1069
1070 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1071 int32_t nBorderStyle = 0;
1072 CPWL_Dash dsBorder(3, 0, 0);
1073 CPWL_Color crLeftTop, crRightBottom;
1074
1075 switch (GetBorderStyle()) {
1076 case BBS_DASH:
1077 nBorderStyle = PBS_DASH;
1078 dsBorder = CPWL_Dash(3, 3, 0);
1079 break;
1080 case BBS_BEVELED:
1081 nBorderStyle = PBS_BEVELED;
1082 fBorderWidth *= 2;
1083 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1084 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1085 break;
1086 case BBS_INSET:
1087 nBorderStyle = PBS_INSET;
1088 fBorderWidth *= 2;
1089 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1090 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1091 break;
1092 case BBS_UNDERLINE:
1093 nBorderStyle = PBS_UNDERLINED;
1094 break;
1095 default:
1096 nBorderStyle = PBS_SOLID;
1097 break;
1098 }
1099
Tom Sepez281a9ea2016-02-26 14:24:28 -08001100 CFX_FloatRect rcWindow = GetRotatedRect();
1101 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001102
1103 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
1104 if (da.HasColor()) {
1105 da.GetColor(iColorType, fc);
1106 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1107 }
1108
1109 int32_t nStyle = 0;
1110
1111 CFX_WideString csWCaption = pControl->GetNormalCaption();
1112 if (csWCaption.GetLength() > 0) {
1113 switch (csWCaption[0]) {
1114 case L'l':
1115 nStyle = PCS_CIRCLE;
1116 break;
1117 case L'8':
1118 nStyle = PCS_CROSS;
1119 break;
1120 case L'u':
1121 nStyle = PCS_DIAMOND;
1122 break;
1123 case L'n':
1124 nStyle = PCS_SQUARE;
1125 break;
1126 case L'H':
1127 nStyle = PCS_STAR;
1128 break;
1129 default: // L'4'
1130 nStyle = PCS_CHECK;
1131 break;
1132 }
1133 } else {
1134 nStyle = PCS_CHECK;
1135 }
1136
1137 CFX_ByteString csAP_N_ON =
1138 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1139 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1140 crLeftTop, crRightBottom, nBorderStyle,
1141 dsBorder);
1142
1143 CFX_ByteString csAP_N_OFF = csAP_N_ON;
1144
1145 switch (nBorderStyle) {
1146 case PBS_BEVELED: {
1147 CPWL_Color crTemp = crLeftTop;
1148 crLeftTop = crRightBottom;
1149 crRightBottom = crTemp;
1150 } break;
1151 case PBS_INSET:
1152 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1153 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1154 break;
1155 }
1156
1157 CFX_ByteString csAP_D_ON =
1158 CPWL_Utils::GetRectFillAppStream(
1159 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1160 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1161 crLeftTop, crRightBottom, nBorderStyle,
1162 dsBorder);
1163
1164 CFX_ByteString csAP_D_OFF = csAP_D_ON;
1165
1166 csAP_N_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
1167 csAP_D_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
1168
1169 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
1170 pControl->GetCheckedAPState());
1171 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1172
1173 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
1174 pControl->GetCheckedAPState());
1175 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1176
1177 CFX_ByteString csAS = GetAppState();
1178 if (csAS.IsEmpty())
1179 SetAppState("Off");
1180}
1181
1182void CPDFSDK_Widget::ResetAppearance_RadioButton() {
1183 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184 CPWL_Color crBackground, crBorder, crText;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001185 int iColorType;
1186 FX_FLOAT fc[4];
1187
1188 pControl->GetOriginalBackgroundColor(iColorType, fc);
1189 if (iColorType > 0)
1190 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1191
1192 pControl->GetOriginalBorderColor(iColorType, fc);
1193 if (iColorType > 0)
1194 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1195
1196 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1197 int32_t nBorderStyle = 0;
1198 CPWL_Dash dsBorder(3, 0, 0);
1199 CPWL_Color crLeftTop, crRightBottom;
1200
1201 switch (GetBorderStyle()) {
1202 case BBS_DASH:
1203 nBorderStyle = PBS_DASH;
1204 dsBorder = CPWL_Dash(3, 3, 0);
1205 break;
1206 case BBS_BEVELED:
1207 nBorderStyle = PBS_BEVELED;
1208 fBorderWidth *= 2;
1209 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1210 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1211 break;
1212 case BBS_INSET:
1213 nBorderStyle = PBS_INSET;
1214 fBorderWidth *= 2;
1215 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1216 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1217 break;
1218 case BBS_UNDERLINE:
1219 nBorderStyle = PBS_UNDERLINED;
1220 break;
1221 default:
1222 nBorderStyle = PBS_SOLID;
1223 break;
1224 }
1225
Tom Sepez281a9ea2016-02-26 14:24:28 -08001226 CFX_FloatRect rcWindow = GetRotatedRect();
1227 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001228
1229 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
1230 if (da.HasColor()) {
1231 da.GetColor(iColorType, fc);
1232 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1233 }
1234
1235 int32_t nStyle = 0;
1236
1237 CFX_WideString csWCaption = pControl->GetNormalCaption();
1238 if (csWCaption.GetLength() > 0) {
1239 switch (csWCaption[0]) {
1240 default: // L'l':
1241 nStyle = PCS_CIRCLE;
1242 break;
1243 case L'8':
1244 nStyle = PCS_CROSS;
1245 break;
1246 case L'u':
1247 nStyle = PCS_DIAMOND;
1248 break;
1249 case L'n':
1250 nStyle = PCS_SQUARE;
1251 break;
1252 case L'H':
1253 nStyle = PCS_STAR;
1254 break;
1255 case L'4':
1256 nStyle = PCS_CHECK;
1257 break;
1258 }
1259 } else {
1260 nStyle = PCS_CIRCLE;
1261 }
1262
1263 CFX_ByteString csAP_N_ON;
1264
Tom Sepez281a9ea2016-02-26 14:24:28 -08001265 CFX_FloatRect rcCenter =
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001266 CPWL_Utils::DeflateRect(CPWL_Utils::GetCenterSquare(rcWindow), 1.0f);
1267
1268 if (nStyle == PCS_CIRCLE) {
1269 if (nBorderStyle == PBS_BEVELED) {
1270 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1271 crRightBottom = CPWL_Utils::SubstractColor(crBackground, 0.25f);
1272 } else if (nBorderStyle == PBS_INSET) {
1273 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5f);
1274 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75f);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001275 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001276
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001277 csAP_N_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBackground) +
1278 CPWL_Utils::GetCircleBorderAppStream(
1279 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
1280 nBorderStyle, dsBorder);
1281 } else {
1282 csAP_N_ON = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1283 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1284 crLeftTop, crRightBottom,
1285 nBorderStyle, dsBorder);
1286 }
1287
1288 CFX_ByteString csAP_N_OFF = csAP_N_ON;
1289
1290 switch (nBorderStyle) {
1291 case PBS_BEVELED: {
1292 CPWL_Color crTemp = crLeftTop;
1293 crLeftTop = crRightBottom;
1294 crRightBottom = crTemp;
1295 } break;
1296 case PBS_INSET:
1297 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1298 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1299 break;
1300 }
1301
1302 CFX_ByteString csAP_D_ON;
1303
1304 if (nStyle == PCS_CIRCLE) {
1305 CPWL_Color crBK = CPWL_Utils::SubstractColor(crBackground, 0.25f);
1306 if (nBorderStyle == PBS_BEVELED) {
1307 crLeftTop = CPWL_Utils::SubstractColor(crBackground, 0.25f);
1308 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1309 crBK = crBackground;
1310 } else if (nBorderStyle == PBS_INSET) {
1311 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1312 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1313 }
1314
1315 csAP_D_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBK) +
1316 CPWL_Utils::GetCircleBorderAppStream(
1317 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
1318 nBorderStyle, dsBorder);
1319 } else {
1320 csAP_D_ON = CPWL_Utils::GetRectFillAppStream(
1321 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1322 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1323 crLeftTop, crRightBottom,
1324 nBorderStyle, dsBorder);
1325 }
1326
1327 CFX_ByteString csAP_D_OFF = csAP_D_ON;
1328
1329 csAP_N_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
1330 csAP_D_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
1331
1332 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
1333 pControl->GetCheckedAPState());
1334 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1335
1336 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
1337 pControl->GetCheckedAPState());
1338 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1339
1340 CFX_ByteString csAS = GetAppState();
1341 if (csAS.IsEmpty())
1342 SetAppState("Off");
1343}
1344
1345void CPDFSDK_Widget::ResetAppearance_ComboBox(const FX_WCHAR* sValue) {
1346 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001347 CPDF_FormField* pField = pControl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001348 CFX_ByteTextBuf sBody, sLines;
1349
Tom Sepez281a9ea2016-02-26 14:24:28 -08001350 CFX_FloatRect rcClient = GetClientRect();
1351 CFX_FloatRect rcButton = rcClient;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001352 rcButton.left = rcButton.right - 13;
1353 rcButton.Normalize();
1354
thestig732f6a02016-05-12 10:41:56 -07001355 IFX_Edit* pEdit = IFX_Edit::NewEdit();
1356 pEdit->EnableRefresh(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001357
thestig732f6a02016-05-12 10:41:56 -07001358 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1359 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
1360 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1361 pEdit->SetFontMap(&font_map);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001362
thestig732f6a02016-05-12 10:41:56 -07001363 CFX_FloatRect rcEdit = rcClient;
1364 rcEdit.right = rcButton.left;
1365 rcEdit.Normalize();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001366
thestig732f6a02016-05-12 10:41:56 -07001367 pEdit->SetPlateRect(rcEdit);
1368 pEdit->SetAlignmentV(1);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001369
thestig732f6a02016-05-12 10:41:56 -07001370 FX_FLOAT fFontSize = GetFontSize();
1371 if (IsFloatZero(fFontSize))
1372 pEdit->SetAutoFontSize(TRUE);
1373 else
1374 pEdit->SetFontSize(fFontSize);
1375
1376 pEdit->Initialize();
1377
1378 if (sValue) {
1379 pEdit->SetText(sValue);
1380 } else {
1381 int32_t nCurSel = pField->GetSelectedIndex(0);
1382
1383 if (nCurSel < 0)
1384 pEdit->SetText(pField->GetValue().c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001385 else
thestig732f6a02016-05-12 10:41:56 -07001386 pEdit->SetText(pField->GetOptionLabel(nCurSel).c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387 }
1388
thestig732f6a02016-05-12 10:41:56 -07001389 CFX_FloatRect rcContent = pEdit->GetContentRect();
1390
1391 CFX_ByteString sEdit =
1392 CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, 0.0f));
1393 if (sEdit.GetLength() > 0) {
1394 sBody << "/Tx BMC\n"
1395 << "q\n";
1396 if (rcContent.Width() > rcEdit.Width() ||
1397 rcContent.Height() > rcEdit.Height()) {
1398 sBody << rcEdit.left << " " << rcEdit.bottom << " " << rcEdit.Width()
1399 << " " << rcEdit.Height() << " re\nW\nn\n";
1400 }
1401
1402 CPWL_Color crText = GetTextPWLColor();
1403 sBody << "BT\n"
1404 << CPWL_Utils::GetColorAppStream(crText) << sEdit << "ET\n"
1405 << "Q\nEMC\n";
1406 }
1407
1408 IFX_Edit::DelEdit(pEdit);
1409
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001410 sBody << CPWL_Utils::GetDropButtonAppStream(rcButton);
1411
1412 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001413 sLines.AsStringC() + sBody.AsStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001414
1415 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1416}
1417
1418void CPDFSDK_Widget::ResetAppearance_ListBox() {
1419 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001420 CPDF_FormField* pField = pControl->GetField();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001421 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001422 CFX_ByteTextBuf sBody, sLines;
1423
thestig732f6a02016-05-12 10:41:56 -07001424 IFX_Edit* pEdit = IFX_Edit::NewEdit();
1425 pEdit->EnableRefresh(FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001426
thestig732f6a02016-05-12 10:41:56 -07001427 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1428 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001429
thestig732f6a02016-05-12 10:41:56 -07001430 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1431 pEdit->SetFontMap(&font_map);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001432
thestig732f6a02016-05-12 10:41:56 -07001433 pEdit->SetPlateRect(CFX_FloatRect(rcClient.left, 0.0f, rcClient.right, 0.0f));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001434
thestig732f6a02016-05-12 10:41:56 -07001435 FX_FLOAT fFontSize = GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001436
thestig732f6a02016-05-12 10:41:56 -07001437 if (IsFloatZero(fFontSize))
1438 pEdit->SetFontSize(12.0f);
1439 else
1440 pEdit->SetFontSize(fFontSize);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001441
thestig732f6a02016-05-12 10:41:56 -07001442 pEdit->Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001443
thestig732f6a02016-05-12 10:41:56 -07001444 CFX_ByteTextBuf sList;
1445 FX_FLOAT fy = rcClient.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001446
thestig732f6a02016-05-12 10:41:56 -07001447 int32_t nTop = pField->GetTopVisibleIndex();
1448 int32_t nCount = pField->CountOptions();
1449 int32_t nSelCount = pField->CountSelectedItems();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001450
thestig732f6a02016-05-12 10:41:56 -07001451 for (int32_t i = nTop; i < nCount; i++) {
1452 FX_BOOL bSelected = FALSE;
1453 for (int32_t j = 0; j < nSelCount; j++) {
1454 if (pField->GetSelectedIndex(j) == i) {
1455 bSelected = TRUE;
1456 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001457 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001458 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001459
thestig732f6a02016-05-12 10:41:56 -07001460 pEdit->SetText(pField->GetOptionLabel(i).c_str());
1461
1462 CFX_FloatRect rcContent = pEdit->GetContentRect();
1463 FX_FLOAT fItemHeight = rcContent.Height();
1464
1465 if (bSelected) {
1466 CFX_FloatRect rcItem =
1467 CFX_FloatRect(rcClient.left, fy - fItemHeight, rcClient.right, fy);
1468 sList << "q\n"
1469 << CPWL_Utils::GetColorAppStream(
1470 CPWL_Color(COLORTYPE_RGB, 0, 51.0f / 255.0f,
1471 113.0f / 255.0f),
1472 TRUE)
1473 << rcItem.left << " " << rcItem.bottom << " " << rcItem.Width()
1474 << " " << rcItem.Height() << " re f\n"
1475 << "Q\n";
1476
1477 sList << "BT\n"
1478 << CPWL_Utils::GetColorAppStream(CPWL_Color(COLORTYPE_GRAY, 1),
1479 TRUE)
1480 << CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, fy))
1481 << "ET\n";
1482 } else {
1483 CPWL_Color crText = GetTextPWLColor();
1484 sList << "BT\n"
1485 << CPWL_Utils::GetColorAppStream(crText, TRUE)
1486 << CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, fy))
1487 << "ET\n";
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001488 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001489
thestig732f6a02016-05-12 10:41:56 -07001490 fy -= fItemHeight;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001491 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001492
thestig732f6a02016-05-12 10:41:56 -07001493 if (sList.GetSize() > 0) {
1494 sBody << "/Tx BMC\n"
1495 << "q\n"
1496 << rcClient.left << " " << rcClient.bottom << " " << rcClient.Width()
1497 << " " << rcClient.Height() << " re\nW\nn\n";
1498 sBody << sList << "Q\nEMC\n";
1499 }
1500
1501 IFX_Edit::DelEdit(pEdit);
1502
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001503 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001504 sLines.AsStringC() + sBody.AsStringC();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001505
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001506 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001507}
1508
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509void CPDFSDK_Widget::ResetAppearance_TextField(const FX_WCHAR* sValue) {
1510 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001511 CPDF_FormField* pField = pControl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001512 CFX_ByteTextBuf sBody, sLines;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001513
thestig732f6a02016-05-12 10:41:56 -07001514 IFX_Edit* pEdit = IFX_Edit::NewEdit();
1515 pEdit->EnableRefresh(FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001516
thestig732f6a02016-05-12 10:41:56 -07001517 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
1518 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001519
thestig732f6a02016-05-12 10:41:56 -07001520 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1521 pEdit->SetFontMap(&font_map);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001522
thestig732f6a02016-05-12 10:41:56 -07001523 CFX_FloatRect rcClient = GetClientRect();
1524 pEdit->SetPlateRect(rcClient);
1525 pEdit->SetAlignmentH(pControl->GetControlAlignment());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001526
thestig732f6a02016-05-12 10:41:56 -07001527 uint32_t dwFieldFlags = pField->GetFieldFlags();
1528 FX_BOOL bMultiLine = (dwFieldFlags >> 12) & 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001529
thestig732f6a02016-05-12 10:41:56 -07001530 if (bMultiLine) {
1531 pEdit->SetMultiLine(TRUE);
1532 pEdit->SetAutoReturn(TRUE);
1533 } else {
1534 pEdit->SetAlignmentV(1);
1535 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001536
thestig732f6a02016-05-12 10:41:56 -07001537 uint16_t subWord = 0;
1538 if ((dwFieldFlags >> 13) & 1) {
1539 subWord = '*';
1540 pEdit->SetPasswordChar(subWord);
1541 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001542
thestig732f6a02016-05-12 10:41:56 -07001543 int nMaxLen = pField->GetMaxLen();
1544 FX_BOOL bCharArray = (dwFieldFlags >> 24) & 1;
1545 FX_FLOAT fFontSize = GetFontSize();
Bo Xufdc00a72014-10-28 23:03:33 -07001546
Tom Sepez51da0932015-11-25 16:05:49 -08001547#ifdef PDF_ENABLE_XFA
thestig732f6a02016-05-12 10:41:56 -07001548 CFX_WideString sValueTmp;
1549 if (!sValue && GetMixXFAWidget()) {
1550 sValueTmp = GetValue(TRUE);
1551 sValue = sValueTmp.c_str();
1552 }
Tom Sepez40e9ff32015-11-30 12:39:54 -08001553#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001554
thestig732f6a02016-05-12 10:41:56 -07001555 if (nMaxLen > 0) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001556 if (bCharArray) {
thestig732f6a02016-05-12 10:41:56 -07001557 pEdit->SetCharArray(nMaxLen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001558
thestig732f6a02016-05-12 10:41:56 -07001559 if (IsFloatZero(fFontSize)) {
1560 fFontSize = CPWL_Edit::GetCharArrayAutoFontSize(font_map.GetPDFFont(0),
1561 rcClient, nMaxLen);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001562 }
thestig732f6a02016-05-12 10:41:56 -07001563 } else {
1564 if (sValue)
1565 nMaxLen = wcslen((const wchar_t*)sValue);
1566 pEdit->SetLimitChar(nMaxLen);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001567 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001568 }
1569
thestig732f6a02016-05-12 10:41:56 -07001570 if (IsFloatZero(fFontSize))
1571 pEdit->SetAutoFontSize(TRUE);
1572 else
1573 pEdit->SetFontSize(fFontSize);
1574
1575 pEdit->Initialize();
1576
1577 if (sValue)
1578 pEdit->SetText(sValue);
1579 else
1580 pEdit->SetText(pField->GetValue().c_str());
1581
1582 CFX_FloatRect rcContent = pEdit->GetContentRect();
1583
1584 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(
1585 pEdit, CFX_FloatPoint(0.0f, 0.0f), NULL, !bCharArray, subWord);
1586
1587 if (sEdit.GetLength() > 0) {
1588 sBody << "/Tx BMC\n"
1589 << "q\n";
1590 if (rcContent.Width() > rcClient.Width() ||
1591 rcContent.Height() > rcClient.Height()) {
1592 sBody << rcClient.left << " " << rcClient.bottom << " "
1593 << rcClient.Width() << " " << rcClient.Height() << " re\nW\nn\n";
1594 }
1595 CPWL_Color crText = GetTextPWLColor();
1596 sBody << "BT\n"
1597 << CPWL_Utils::GetColorAppStream(crText) << sEdit << "ET\n"
1598 << "Q\nEMC\n";
1599 }
1600
1601 if (bCharArray) {
1602 switch (GetBorderStyle()) {
1603 case BBS_SOLID: {
1604 CFX_ByteString sColor =
1605 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1606 if (sColor.GetLength() > 0) {
1607 sLines << "q\n"
1608 << GetBorderWidth() << " w\n"
1609 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1610 << " 2 J 0 j\n";
1611
1612 for (int32_t i = 1; i < nMaxLen; i++) {
1613 sLines << rcClient.left +
1614 ((rcClient.right - rcClient.left) / nMaxLen) * i
1615 << " " << rcClient.bottom << " m\n"
1616 << rcClient.left +
1617 ((rcClient.right - rcClient.left) / nMaxLen) * i
1618 << " " << rcClient.top << " l S\n";
1619 }
1620
1621 sLines << "Q\n";
1622 }
1623 } break;
1624 case BBS_DASH: {
1625 CFX_ByteString sColor =
1626 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1627 if (sColor.GetLength() > 0) {
1628 CPWL_Dash dsBorder = CPWL_Dash(3, 3, 0);
1629
1630 sLines << "q\n"
1631 << GetBorderWidth() << " w\n"
1632 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1633 << "[" << dsBorder.nDash << " " << dsBorder.nGap << "] "
1634 << dsBorder.nPhase << " d\n";
1635
1636 for (int32_t i = 1; i < nMaxLen; i++) {
1637 sLines << rcClient.left +
1638 ((rcClient.right - rcClient.left) / nMaxLen) * i
1639 << " " << rcClient.bottom << " m\n"
1640 << rcClient.left +
1641 ((rcClient.right - rcClient.left) / nMaxLen) * i
1642 << " " << rcClient.top << " l S\n";
1643 }
1644
1645 sLines << "Q\n";
1646 }
1647 } break;
1648 }
1649 }
1650
1651 IFX_Edit::DelEdit(pEdit);
1652
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001653 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
tsepez8e4c5052016-04-14 13:42:44 -07001654 sLines.AsStringC() + sBody.AsStringC();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001655 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001656}
1657
Tom Sepez281a9ea2016-02-26 14:24:28 -08001658CFX_FloatRect CPDFSDK_Widget::GetClientRect() const {
1659 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001660 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1661 switch (GetBorderStyle()) {
1662 case BBS_BEVELED:
1663 case BBS_INSET:
1664 fBorderWidth *= 2.0f;
1665 break;
1666 }
1667
1668 return CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001669}
1670
Tom Sepez281a9ea2016-02-26 14:24:28 -08001671CFX_FloatRect CPDFSDK_Widget::GetRotatedRect() const {
1672 CFX_FloatRect rectAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001673 FX_FLOAT fWidth = rectAnnot.right - rectAnnot.left;
1674 FX_FLOAT fHeight = rectAnnot.top - rectAnnot.bottom;
1675
1676 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001677 CFX_FloatRect rcPDFWindow;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001678 switch (abs(pControl->GetRotation() % 360)) {
1679 case 0:
1680 case 180:
1681 default:
Tom Sepez281a9ea2016-02-26 14:24:28 -08001682 rcPDFWindow = CFX_FloatRect(0, 0, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001683 break;
1684 case 90:
1685 case 270:
Tom Sepez281a9ea2016-02-26 14:24:28 -08001686 rcPDFWindow = CFX_FloatRect(0, 0, fHeight, fWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001687 break;
1688 }
1689
1690 return rcPDFWindow;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001691}
1692
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001693CFX_ByteString CPDFSDK_Widget::GetBackgroundAppStream() const {
1694 CPWL_Color crBackground = GetFillPWLColor();
1695 if (crBackground.nColorType != COLORTYPE_TRANSPARENT) {
1696 return CPWL_Utils::GetRectFillAppStream(GetRotatedRect(), crBackground);
1697 }
1698 return "";
Bo Xufdc00a72014-10-28 23:03:33 -07001699}
1700
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001701CFX_ByteString CPDFSDK_Widget::GetBorderAppStream() const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001702 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001703 CPWL_Color crBorder = GetBorderPWLColor();
1704 CPWL_Color crBackground = GetFillPWLColor();
1705 CPWL_Color crLeftTop, crRightBottom;
1706
1707 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1708 int32_t nBorderStyle = 0;
1709 CPWL_Dash dsBorder(3, 0, 0);
1710
1711 switch (GetBorderStyle()) {
1712 case BBS_DASH:
1713 nBorderStyle = PBS_DASH;
1714 dsBorder = CPWL_Dash(3, 3, 0);
1715 break;
1716 case BBS_BEVELED:
1717 nBorderStyle = PBS_BEVELED;
1718 fBorderWidth *= 2;
1719 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1720 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1721 break;
1722 case BBS_INSET:
1723 nBorderStyle = PBS_INSET;
1724 fBorderWidth *= 2;
1725 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1726 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1727 break;
1728 case BBS_UNDERLINE:
1729 nBorderStyle = PBS_UNDERLINED;
1730 break;
1731 default:
1732 nBorderStyle = PBS_SOLID;
1733 break;
1734 }
1735
1736 return CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1737 crLeftTop, crRightBottom, nBorderStyle,
1738 dsBorder);
Bo Xufdc00a72014-10-28 23:03:33 -07001739}
1740
Tom Sepez60d909e2015-12-10 15:34:55 -08001741CFX_Matrix CPDFSDK_Widget::GetMatrix() const {
1742 CFX_Matrix mt;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001743 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001744 CFX_FloatRect rcAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001745 FX_FLOAT fWidth = rcAnnot.right - rcAnnot.left;
1746 FX_FLOAT fHeight = rcAnnot.top - rcAnnot.bottom;
1747
1748 switch (abs(pControl->GetRotation() % 360)) {
1749 case 0:
1750 default:
Tom Sepez60d909e2015-12-10 15:34:55 -08001751 mt = CFX_Matrix(1, 0, 0, 1, 0, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001752 break;
1753 case 90:
Tom Sepez60d909e2015-12-10 15:34:55 -08001754 mt = CFX_Matrix(0, 1, -1, 0, fWidth, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001755 break;
1756 case 180:
Tom Sepez60d909e2015-12-10 15:34:55 -08001757 mt = CFX_Matrix(-1, 0, 0, -1, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001758 break;
1759 case 270:
Tom Sepez60d909e2015-12-10 15:34:55 -08001760 mt = CFX_Matrix(0, -1, 1, 0, 0, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001761 break;
1762 }
1763
1764 return mt;
Bo Xufdc00a72014-10-28 23:03:33 -07001765}
1766
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001767CPWL_Color CPDFSDK_Widget::GetTextPWLColor() const {
1768 CPWL_Color crText = CPWL_Color(COLORTYPE_GRAY, 0);
1769
1770 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001771 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
1772 if (da.HasColor()) {
1773 int32_t iColorType;
1774 FX_FLOAT fc[4];
1775 da.GetColor(iColorType, fc);
1776 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1777 }
1778
1779 return crText;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001780}
1781
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001782CPWL_Color CPDFSDK_Widget::GetBorderPWLColor() const {
1783 CPWL_Color crBorder;
1784
1785 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001786 int32_t iColorType;
1787 FX_FLOAT fc[4];
1788 pFormCtrl->GetOriginalBorderColor(iColorType, fc);
1789 if (iColorType > 0)
1790 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1791
1792 return crBorder;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001793}
1794
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001795CPWL_Color CPDFSDK_Widget::GetFillPWLColor() const {
1796 CPWL_Color crFill;
1797
1798 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001799 int32_t iColorType;
1800 FX_FLOAT fc[4];
1801 pFormCtrl->GetOriginalBackgroundColor(iColorType, fc);
1802 if (iColorType > 0)
1803 crFill = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1804
1805 return crFill;
Bo Xufdc00a72014-10-28 23:03:33 -07001806}
1807
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001808void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType,
1809 CPDF_Stream* pImage) {
Tom Sepez33420902015-10-13 15:00:10 -07001810 CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
Lei Zhang96660d62015-12-14 18:27:25 -08001811 ASSERT(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812
Wei Li9b761132016-01-29 15:44:20 -08001813 CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP");
tsepez7b1ccf92016-04-14 11:04:57 -07001814 CPDF_Stream* pStream = pAPDict->GetStreamBy(sAPType);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001815 CPDF_Dictionary* pStreamDict = pStream->GetDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001816 CFX_ByteString sImageAlias = "IMG";
1817
1818 if (CPDF_Dictionary* pImageDict = pImage->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -08001819 sImageAlias = pImageDict->GetStringBy("Name");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001820 if (sImageAlias.IsEmpty())
1821 sImageAlias = "IMG";
1822 }
1823
Wei Li9b761132016-01-29 15:44:20 -08001824 CPDF_Dictionary* pStreamResList = pStreamDict->GetDictBy("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001825 if (!pStreamResList) {
Tom Sepezae51c812015-08-05 12:34:06 -07001826 pStreamResList = new CPDF_Dictionary();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001827 pStreamDict->SetAt("Resources", pStreamResList);
1828 }
1829
1830 if (pStreamResList) {
Tom Sepezae51c812015-08-05 12:34:06 -07001831 CPDF_Dictionary* pXObject = new CPDF_Dictionary;
tsepez7b1ccf92016-04-14 11:04:57 -07001832 pXObject->SetAtReference(sImageAlias, pDoc, pImage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001833 pStreamResList->SetAt("XObject", pXObject);
1834 }
Bo Xufdc00a72014-10-28 23:03:33 -07001835}
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001836
1837void CPDFSDK_Widget::RemoveAppearance(const CFX_ByteString& sAPType) {
tsepez7b1ccf92016-04-14 11:04:57 -07001838 if (CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP"))
1839 pAPDict->RemoveAt(sAPType);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001840}
1841
1842FX_BOOL CPDFSDK_Widget::OnAAction(CPDF_AAction::AActionType type,
1843 PDFSDK_FieldAction& data,
1844 CPDFSDK_PageView* pPageView) {
1845 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001846 CPDFDoc_Environment* pEnv = pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001847
Tom Sepez40e9ff32015-11-30 12:39:54 -08001848#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -08001849 CPDFXFA_Document* pDoc = pDocument->GetXFADocument();
dsinclairdf4bc592016-03-31 20:34:43 -07001850 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001851 XFA_EVENTTYPE eEventType = GetXFAEventType(type, data.bWillCommit);
1852
1853 if (eEventType != XFA_EVENT_Unknown) {
dsinclairdf4bc592016-03-31 20:34:43 -07001854 if (CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001855 CXFA_EventParam param;
1856 param.m_eType = eEventType;
1857 param.m_wsChange = data.sChange;
1858 param.m_iCommitKey = data.nCommitKey;
1859 param.m_bShift = data.bShift;
1860 param.m_iSelStart = data.nSelStart;
1861 param.m_iSelEnd = data.nSelEnd;
1862 param.m_wsFullText = data.sValue;
1863 param.m_bKeyDown = data.bKeyDown;
1864 param.m_bModifier = data.bModifier;
1865 param.m_wsNewText = data.sValue;
1866 if (data.nSelEnd > data.nSelStart)
1867 param.m_wsNewText.Delete(data.nSelStart,
1868 data.nSelEnd - data.nSelStart);
1869 for (int i = data.sChange.GetLength() - 1; i >= 0; i--)
1870 param.m_wsNewText.Insert(data.nSelStart, data.sChange[i]);
1871 param.m_wsPrevText = data.sValue;
1872
dsinclair221caf62016-04-04 12:08:40 -07001873 CXFA_WidgetAcc* pAcc = hWidget->GetDataAcc();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001874 param.m_pTarget = pAcc;
1875 int32_t nRet = pXFAWidgetHandler->ProcessEvent(pAcc, &param);
1876
dsinclairdf4bc592016-03-31 20:34:43 -07001877 if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001878 pDocView->UpdateDocView();
1879 }
1880
Wei Lie98ac2e2016-03-18 15:43:04 -07001881 if (nRet == XFA_EVENTERROR_Success)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001882 return TRUE;
1883 }
1884 }
1885 }
Tom Sepez40e9ff32015-11-30 12:39:54 -08001886#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001887
1888 CPDF_Action action = GetAAction(type);
Wei Li0fc6b252016-03-01 16:29:41 -08001889 if (action.GetDict() && action.GetType() != CPDF_Action::Unknown) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001890 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
1891 return pActionHandler->DoAction_Field(action, type, pDocument,
1892 GetFormField(), data);
1893 }
1894 return FALSE;
1895}
1896
1897CPDF_Action CPDFSDK_Widget::GetAAction(CPDF_AAction::AActionType eAAT) {
1898 switch (eAAT) {
1899 case CPDF_AAction::CursorEnter:
1900 case CPDF_AAction::CursorExit:
1901 case CPDF_AAction::ButtonDown:
1902 case CPDF_AAction::ButtonUp:
1903 case CPDF_AAction::GetFocus:
1904 case CPDF_AAction::LoseFocus:
1905 case CPDF_AAction::PageOpen:
1906 case CPDF_AAction::PageClose:
1907 case CPDF_AAction::PageVisible:
1908 case CPDF_AAction::PageInvisible:
1909 return CPDFSDK_BAAnnot::GetAAction(eAAT);
1910
1911 case CPDF_AAction::KeyStroke:
1912 case CPDF_AAction::Format:
1913 case CPDF_AAction::Validate:
1914 case CPDF_AAction::Calculate: {
1915 CPDF_FormField* pField = GetFormField();
Wei Li0fc6b252016-03-01 16:29:41 -08001916 if (pField->GetAdditionalAction().GetDict())
1917 return pField->GetAdditionalAction().GetAction(eAAT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001918 return CPDFSDK_BAAnnot::GetAAction(eAAT);
1919 }
1920 default:
1921 break;
1922 }
1923
1924 return CPDF_Action();
1925}
1926
1927CFX_WideString CPDFSDK_Widget::GetAlternateName() const {
1928 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001929 return pFormField->GetAlternateName();
1930}
1931
1932int32_t CPDFSDK_Widget::GetAppearanceAge() const {
1933 return m_nAppAge;
1934}
1935
1936int32_t CPDFSDK_Widget::GetValueAge() const {
1937 return m_nValueAge;
1938}
1939
1940FX_BOOL CPDFSDK_Widget::HitTest(FX_FLOAT pageX, FX_FLOAT pageY) {
1941 CPDF_Annot* pAnnot = GetPDFAnnot();
1942 CFX_FloatRect annotRect;
1943 pAnnot->GetRect(annotRect);
1944 if (annotRect.Contains(pageX, pageY)) {
1945 if (!IsVisible())
1946 return FALSE;
1947
1948 int nFieldFlags = GetFieldFlags();
1949 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
1950 return FALSE;
1951
1952 return TRUE;
1953 }
1954 return FALSE;
1955}
1956
Tom Sepez51da0932015-11-25 16:05:49 -08001957#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -07001958CPDFSDK_XFAWidget::CPDFSDK_XFAWidget(CXFA_FFWidget* pAnnot,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001959 CPDFSDK_PageView* pPageView,
1960 CPDFSDK_InterForm* pInterForm)
Dan Sinclairf766ad22016-03-14 13:51:24 -04001961 : CPDFSDK_Annot(pPageView),
1962 m_pInterForm(pInterForm),
1963 m_hXFAWidget(pAnnot) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001964
1965FX_BOOL CPDFSDK_XFAWidget::IsXFAField() {
1966 return TRUE;
1967}
1968
1969CFX_ByteString CPDFSDK_XFAWidget::GetType() const {
1970 return FSDK_XFAWIDGET_TYPENAME;
1971}
1972
Tom Sepez3343d142015-11-02 09:54:54 -08001973CFX_FloatRect CPDFSDK_XFAWidget::GetRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001974 CFX_RectF rcBBox;
dsinclair221caf62016-04-04 12:08:40 -07001975 GetXFAWidget()->GetRect(rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001976 return CFX_FloatRect(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
1977 rcBBox.top + rcBBox.height);
1978}
Tom Sepez40e9ff32015-11-30 12:39:54 -08001979#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001980
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001981CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_Document* pDocument)
1982 : m_pDocument(pDocument),
1983 m_pInterForm(NULL),
Tom Sepez51da0932015-11-25 16:05:49 -08001984#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001985 m_bXfaCalculate(TRUE),
1986 m_bXfaValidationsEnabled(TRUE),
Tom Sepez40e9ff32015-11-30 12:39:54 -08001987#endif // PDF_ENABLE_XFA
1988 m_bCalculate(TRUE),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001989 m_bBusy(FALSE) {
Tom Sepez50d12ad2015-11-24 09:50:51 -08001990 m_pInterForm = new CPDF_InterForm(m_pDocument->GetPDFDocument(), FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001991 m_pInterForm->SetFormNotify(this);
1992
Tom Sepezae7a9172015-11-23 09:22:46 -08001993 for (int i = 0; i < kNumFieldTypes; ++i)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001994 m_bNeedHightlight[i] = FALSE;
1995 m_iHighlightAlpha = 0;
1996}
1997
1998CPDFSDK_InterForm::~CPDFSDK_InterForm() {
1999 delete m_pInterForm;
2000 m_pInterForm = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002001 m_Map.clear();
Tom Sepez51da0932015-11-25 16:05:49 -08002002#ifdef PDF_ENABLE_XFA
tsepez16a20332016-04-22 14:00:47 -07002003 m_XFAMap.clear();
Tom Sepez40e9ff32015-11-30 12:39:54 -08002004#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002005}
2006
2007FX_BOOL CPDFSDK_InterForm::HighlightWidgets() {
2008 return FALSE;
2009}
2010
2011CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget,
2012 FX_BOOL bNext) const {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08002013 std::unique_ptr<CBA_AnnotIterator> pIterator(
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002014 new CBA_AnnotIterator(pWidget->GetPageView(), "Widget", ""));
2015
2016 if (bNext) {
2017 return (CPDFSDK_Widget*)pIterator->GetNextAnnot(pWidget);
2018 }
2019 return (CPDFSDK_Widget*)pIterator->GetPrevAnnot(pWidget);
2020}
2021
2022CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const {
2023 if (!pControl || !m_pInterForm)
2024 return nullptr;
2025
2026 CPDFSDK_Widget* pWidget = nullptr;
2027 const auto it = m_Map.find(pControl);
2028 if (it != m_Map.end())
2029 pWidget = it->second;
2030
2031 if (pWidget)
2032 return pWidget;
2033
2034 CPDF_Dictionary* pControlDict = pControl->GetWidget();
Tom Sepez50d12ad2015-11-24 09:50:51 -08002035 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002036 CPDFSDK_PageView* pPage = nullptr;
2037
Wei Li9b761132016-01-29 15:44:20 -08002038 if (CPDF_Dictionary* pPageDict = pControlDict->GetDictBy("P")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002039 int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
2040 if (nPageIndex >= 0) {
2041 pPage = m_pDocument->GetPageView(nPageIndex);
2042 }
2043 }
2044
2045 if (!pPage) {
2046 int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict);
2047 if (nPageIndex >= 0) {
2048 pPage = m_pDocument->GetPageView(nPageIndex);
2049 }
2050 }
2051
2052 if (!pPage)
2053 return nullptr;
2054 return (CPDFSDK_Widget*)pPage->GetAnnotByDict(pControlDict);
2055}
2056
Lei Zhangd88a3642015-11-10 09:38:57 -08002057void CPDFSDK_InterForm::GetWidgets(
2058 const CFX_WideString& sFieldName,
2059 std::vector<CPDFSDK_Widget*>* widgets) const {
2060 for (int i = 0, sz = m_pInterForm->CountFields(sFieldName); i < sz; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002061 CPDF_FormField* pFormField = m_pInterForm->GetField(i, sFieldName);
Lei Zhangd88a3642015-11-10 09:38:57 -08002062 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002063 GetWidgets(pFormField, widgets);
2064 }
2065}
2066
Lei Zhangd88a3642015-11-10 09:38:57 -08002067void CPDFSDK_InterForm::GetWidgets(
2068 CPDF_FormField* pField,
2069 std::vector<CPDFSDK_Widget*>* widgets) const {
2070 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002071 CPDF_FormControl* pFormCtrl = pField->GetControl(i);
Lei Zhangd88a3642015-11-10 09:38:57 -08002072 ASSERT(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002073 CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002074 if (pWidget)
Lei Zhangd88a3642015-11-10 09:38:57 -08002075 widgets->push_back(pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002076 }
2077}
2078
2079int CPDFSDK_InterForm::GetPageIndexByAnnotDict(
2080 CPDF_Document* pDocument,
2081 CPDF_Dictionary* pAnnotDict) const {
Lei Zhang96660d62015-12-14 18:27:25 -08002082 ASSERT(pAnnotDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002083
2084 for (int i = 0, sz = pDocument->GetPageCount(); i < sz; i++) {
2085 if (CPDF_Dictionary* pPageDict = pDocument->GetPage(i)) {
Wei Li9b761132016-01-29 15:44:20 -08002086 if (CPDF_Array* pAnnots = pPageDict->GetArrayBy("Annots")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002087 for (int j = 0, jsz = pAnnots->GetCount(); j < jsz; j++) {
tsepezbd567552016-03-29 14:51:50 -07002088 CPDF_Object* pDict = pAnnots->GetDirectObjectAt(j);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002089 if (pAnnotDict == pDict) {
2090 return i;
2091 }
2092 }
2093 }
2094 }
2095 }
2096
2097 return -1;
2098}
2099
2100void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl,
2101 CPDFSDK_Widget* pWidget) {
2102 m_Map[pControl] = pWidget;
2103}
2104
2105void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl) {
2106 m_Map.erase(pControl);
2107}
2108
Tom Sepez40e9ff32015-11-30 12:39:54 -08002109void CPDFSDK_InterForm::EnableCalculate(FX_BOOL bEnabled) {
2110 m_bCalculate = bEnabled;
2111}
2112
2113FX_BOOL CPDFSDK_InterForm::IsCalculateEnabled() const {
2114 return m_bCalculate;
2115}
2116
Tom Sepez51da0932015-11-25 16:05:49 -08002117#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -07002118void CPDFSDK_InterForm::AddXFAMap(CXFA_FFWidget* hWidget,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002119 CPDFSDK_XFAWidget* pWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002120 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002121 m_XFAMap[hWidget] = pWidget;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002122}
2123
dsinclairdf4bc592016-03-31 20:34:43 -07002124void CPDFSDK_InterForm::RemoveXFAMap(CXFA_FFWidget* hWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002125 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002126 m_XFAMap.erase(hWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002127}
2128
dsinclairdf4bc592016-03-31 20:34:43 -07002129CPDFSDK_XFAWidget* CPDFSDK_InterForm::GetXFAWidget(CXFA_FFWidget* hWidget) {
dsinclair43854a52016-04-27 12:26:00 -07002130 ASSERT(hWidget);
tsepez16a20332016-04-22 14:00:47 -07002131 auto it = m_XFAMap.find(hWidget);
2132 return it != m_XFAMap.end() ? it->second : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002133}
2134
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002135void CPDFSDK_InterForm::XfaEnableCalculate(FX_BOOL bEnabled) {
2136 m_bXfaCalculate = bEnabled;
2137}
2138FX_BOOL CPDFSDK_InterForm::IsXfaCalculateEnabled() const {
2139 return m_bXfaCalculate;
2140}
2141
2142FX_BOOL CPDFSDK_InterForm::IsXfaValidationsEnabled() {
2143 return m_bXfaValidationsEnabled;
2144}
2145void CPDFSDK_InterForm::XfaSetValidationsEnabled(FX_BOOL bEnabled) {
2146 m_bXfaValidationsEnabled = bEnabled;
Bo Xufdc00a72014-10-28 23:03:33 -07002147}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002148#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -07002149
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002150void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002151 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2152 ASSERT(pEnv);
2153 if (!pEnv->IsJSInitiated())
2154 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002155
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002156 if (m_bBusy)
2157 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002158
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002159 m_bBusy = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002160
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002161 if (IsCalculateEnabled()) {
Tom Sepezba038bc2015-10-08 12:03:00 -07002162 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002163 pRuntime->SetReaderDocument(m_pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002164
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002165 int nSize = m_pInterForm->CountFieldsInCalculationOrder();
2166 for (int i = 0; i < nSize; i++) {
2167 if (CPDF_FormField* pField =
2168 m_pInterForm->GetFieldInCalculationOrder(i)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002169 int nType = pField->GetFieldType();
2170 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2171 CPDF_AAction aAction = pField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002172 if (aAction.GetDict() &&
2173 aAction.ActionExist(CPDF_AAction::Calculate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002174 CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate);
Wei Li0fc6b252016-03-01 16:29:41 -08002175 if (action.GetDict()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002176 CFX_WideString csJS = action.GetJavaScript();
2177 if (!csJS.IsEmpty()) {
Tom Sepezba038bc2015-10-08 12:03:00 -07002178 IJS_Context* pContext = pRuntime->NewContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002179 CFX_WideString sOldValue = pField->GetValue();
2180 CFX_WideString sValue = sOldValue;
2181 FX_BOOL bRC = TRUE;
2182 pContext->OnField_Calculate(pFormField, pField, sValue, bRC);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002183
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002184 CFX_WideString sInfo;
Tom Sepez33420902015-10-13 15:00:10 -07002185 FX_BOOL bRet = pContext->RunScript(csJS, &sInfo);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002186 pRuntime->ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002187
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002188 if (bRet) {
2189 if (bRC) {
2190 if (sValue.Compare(sOldValue) != 0)
2191 pField->SetValue(sValue, TRUE);
2192 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002193 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002194 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002195 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002196 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002197 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002198 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002199 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002200 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002201
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002202 m_bBusy = FALSE;
2203}
2204
2205CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField,
2206 FX_BOOL& bFormated) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002207 CFX_WideString sValue = pFormField->GetValue();
2208 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2209 ASSERT(pEnv);
2210 if (!pEnv->IsJSInitiated()) {
2211 bFormated = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002212 return sValue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002213 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002214
Tom Sepezba038bc2015-10-08 12:03:00 -07002215 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002216 pRuntime->SetReaderDocument(m_pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002217
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002218 if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX) {
2219 if (pFormField->CountSelectedItems() > 0) {
2220 int index = pFormField->GetSelectedIndex(0);
2221 if (index >= 0)
2222 sValue = pFormField->GetOptionLabel(index);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002223 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002224 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002225
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002226 bFormated = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002227
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002228 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002229 if (aAction.GetDict() && aAction.ActionExist(CPDF_AAction::Format)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002230 CPDF_Action action = aAction.GetAction(CPDF_AAction::Format);
Wei Li0fc6b252016-03-01 16:29:41 -08002231 if (action.GetDict()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002232 CFX_WideString script = action.GetJavaScript();
2233 if (!script.IsEmpty()) {
2234 CFX_WideString Value = sValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002235
Tom Sepezba038bc2015-10-08 12:03:00 -07002236 IJS_Context* pContext = pRuntime->NewContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002237 pContext->OnField_Format(pFormField, Value, TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002238
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002239 CFX_WideString sInfo;
Tom Sepez33420902015-10-13 15:00:10 -07002240 FX_BOOL bRet = pContext->RunScript(script, &sInfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002241 pRuntime->ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002242
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002243 if (bRet) {
2244 sValue = Value;
2245 bFormated = TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002246 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002247 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002248 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002249 }
2250
2251 return sValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002252}
2253
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002254void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField,
2255 const FX_WCHAR* sValue,
2256 FX_BOOL bValueChanged) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002257 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2258 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang96660d62015-12-14 18:27:25 -08002259 ASSERT(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002260 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
2261 pWidget->ResetAppearance(sValue, bValueChanged);
2262 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002263}
2264
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002265void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002266 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2267 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang96660d62015-12-14 18:27:25 -08002268 ASSERT(pFormCtrl);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002269
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002270 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
2271 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2272 CFFL_IFormFiller* pIFormFiller = pEnv->GetIFormFiller();
Tom Sepez540c4362015-11-24 13:33:57 -08002273 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002274 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002275 FX_RECT rcBBox = pIFormFiller->GetViewBBox(pPageView, pWidget);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002276
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002277 pEnv->FFI_Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right,
2278 rcBBox.bottom);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002279 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002280 }
2281}
2282
Tom Sepezd6262c12016-02-03 14:47:06 -08002283FX_BOOL CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField,
2284 const CFX_WideString& csValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002285 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002286 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::KeyStroke))
Tom Sepezd6262c12016-02-03 14:47:06 -08002287 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002288
Tom Sepezd6262c12016-02-03 14:47:06 -08002289 CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke);
Wei Li0fc6b252016-03-01 16:29:41 -08002290 if (!action.GetDict())
Tom Sepezd6262c12016-02-03 14:47:06 -08002291 return TRUE;
2292
2293 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2294 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2295 PDFSDK_FieldAction fa;
2296 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2297 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2298 fa.sValue = csValue;
2299 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::KeyStroke,
2300 m_pDocument, pFormField, fa);
2301 return fa.bRC;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002302}
2303
Tom Sepezd6262c12016-02-03 14:47:06 -08002304FX_BOOL CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField,
2305 const CFX_WideString& csValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002306 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002307 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Validate))
Tom Sepezd6262c12016-02-03 14:47:06 -08002308 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002309
Tom Sepezd6262c12016-02-03 14:47:06 -08002310 CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate);
Wei Li0fc6b252016-03-01 16:29:41 -08002311 if (!action.GetDict())
Tom Sepezd6262c12016-02-03 14:47:06 -08002312 return TRUE;
2313
2314 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2315 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2316 PDFSDK_FieldAction fa;
2317 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2318 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2319 fa.sValue = csValue;
2320 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::Validate,
2321 m_pDocument, pFormField, fa);
2322 return fa.bRC;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002323}
2324
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002325FX_BOOL CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action) {
Wei Li0fc6b252016-03-01 16:29:41 -08002326 ASSERT(action.GetDict());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002327
Wei Li0fc6b252016-03-01 16:29:41 -08002328 CPDF_ActionFields af(&action);
Lei Zhangd88a3642015-11-10 09:38:57 -08002329 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2330 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002331
2332 FX_BOOL bHide = action.GetHideStatus();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002333 FX_BOOL bChanged = FALSE;
2334
Lei Zhangd88a3642015-11-10 09:38:57 -08002335 for (CPDF_FormField* pField : fields) {
2336 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
2337 CPDF_FormControl* pControl = pField->GetControl(i);
2338 ASSERT(pControl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002339
2340 if (CPDFSDK_Widget* pWidget = GetWidget(pControl)) {
tsepezc3255f52016-03-25 14:52:27 -07002341 uint32_t nFlags = pWidget->GetFlags();
Lei Zhangd88a3642015-11-10 09:38:57 -08002342 nFlags &= ~ANNOTFLAG_INVISIBLE;
2343 nFlags &= ~ANNOTFLAG_NOVIEW;
2344 if (bHide)
2345 nFlags |= ANNOTFLAG_HIDDEN;
2346 else
2347 nFlags &= ~ANNOTFLAG_HIDDEN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002348 pWidget->SetFlags(nFlags);
Lei Zhangd88a3642015-11-10 09:38:57 -08002349 pWidget->GetPageView()->UpdateView(pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002350 bChanged = TRUE;
2351 }
2352 }
2353 }
2354
2355 return bChanged;
2356}
2357
2358FX_BOOL CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002359 CFX_WideString sDestination = action.GetFilePath();
2360 if (sDestination.IsEmpty())
2361 return FALSE;
2362
2363 CPDF_Dictionary* pActionDict = action.GetDict();
2364 if (pActionDict->KeyExist("Fields")) {
Wei Li0fc6b252016-03-01 16:29:41 -08002365 CPDF_ActionFields af(&action);
tsepezc3255f52016-03-25 14:52:27 -07002366 uint32_t dwFlags = action.GetFlags();
Lei Zhangd88a3642015-11-10 09:38:57 -08002367 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2368 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
2369 if (!fields.empty()) {
2370 bool bIncludeOrExclude = !(dwFlags & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002371 if (m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002372 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002373
Wei Li97da9762016-03-11 17:00:48 -08002374 return SubmitFields(sDestination, fields, bIncludeOrExclude, false);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002375 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002376 }
Lei Zhangd88a3642015-11-10 09:38:57 -08002377 if (m_pInterForm->CheckRequiredFields(nullptr, true))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002378 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002379
2380 return SubmitForm(sDestination, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002381}
2382
Lei Zhangd88a3642015-11-10 09:38:57 -08002383FX_BOOL CPDFSDK_InterForm::SubmitFields(
2384 const CFX_WideString& csDestination,
2385 const std::vector<CPDF_FormField*>& fields,
Wei Li97da9762016-03-11 17:00:48 -08002386 bool bIncludeOrExclude,
2387 bool bUrlEncoded) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002388 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002389
2390 CFX_ByteTextBuf textBuf;
2391 ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf);
2392
2393 uint8_t* pBuffer = textBuf.GetBuffer();
2394 FX_STRSIZE nBufSize = textBuf.GetLength();
2395
Lei Zhangd88a3642015-11-10 09:38:57 -08002396 if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize))
2397 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002398
2399 pEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002400 return TRUE;
2401}
2402
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002403FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile,
2404 CFX_WideString csTxtFile) {
2405 return TRUE;
2406}
2407
2408FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf,
2409 FX_STRSIZE& nBufSize) {
2410 CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize);
2411 if (pFDF) {
Wei Li9b761132016-01-29 15:44:20 -08002412 CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictBy("FDF");
Lei Zhang997de612015-11-04 18:17:53 -08002413 if (!pMainDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002414 return FALSE;
2415
2416 // Get fields
Wei Li9b761132016-01-29 15:44:20 -08002417 CPDF_Array* pFields = pMainDict->GetArrayBy("Fields");
Lei Zhang997de612015-11-04 18:17:53 -08002418 if (!pFields)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002419 return FALSE;
2420
2421 CFX_ByteTextBuf fdfEncodedData;
tsepezc3255f52016-03-25 14:52:27 -07002422 for (uint32_t i = 0; i < pFields->GetCount(); i++) {
Wei Li9b761132016-01-29 15:44:20 -08002423 CPDF_Dictionary* pField = pFields->GetDictAt(i);
Lei Zhang997de612015-11-04 18:17:53 -08002424 if (!pField)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002425 continue;
2426 CFX_WideString name;
Wei Li9b761132016-01-29 15:44:20 -08002427 name = pField->GetUnicodeTextBy("T");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002428 CFX_ByteString name_b = CFX_ByteString::FromUnicode(name);
Wei Li9b761132016-01-29 15:44:20 -08002429 CFX_ByteString csBValue = pField->GetStringBy("V");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002430 CFX_WideString csWValue = PDF_DecodeText(csBValue);
2431 CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue);
2432
Tom Sepez052a8d92016-02-19 14:41:46 -08002433 fdfEncodedData << name_b.GetBuffer(name_b.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002434 name_b.ReleaseBuffer();
Tom Sepez052a8d92016-02-19 14:41:46 -08002435 fdfEncodedData << "=";
2436 fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002437 csValue_b.ReleaseBuffer();
2438 if (i != pFields->GetCount() - 1)
Tom Sepez052a8d92016-02-19 14:41:46 -08002439 fdfEncodedData << "&";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002440 }
2441
2442 nBufSize = fdfEncodedData.GetLength();
2443 pBuf = FX_Alloc(uint8_t, nBufSize);
2444 FXSYS_memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize);
2445 }
2446 return TRUE;
2447}
2448
Lei Zhangd88a3642015-11-10 09:38:57 -08002449FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(
2450 const std::vector<CPDF_FormField*>& fields,
Wei Li97da9762016-03-11 17:00:48 -08002451 bool bIncludeOrExclude,
Lei Zhangd88a3642015-11-10 09:38:57 -08002452 CFX_ByteTextBuf& textBuf) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08002453 std::unique_ptr<CFDF_Document> pFDF(m_pInterForm->ExportToFDF(
tsepez4c3debb2016-04-08 12:20:38 -07002454 m_pDocument->GetPath().AsStringC(), fields, bIncludeOrExclude));
Lei Zhangd88a3642015-11-10 09:38:57 -08002455 return pFDF ? pFDF->WriteBuf(textBuf) : FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002456}
2457
Tom Sepez51da0932015-11-25 16:05:49 -08002458#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002459void CPDFSDK_InterForm::SynchronizeField(CPDF_FormField* pFormField,
2460 FX_BOOL bSynchronizeElse) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002461 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2462 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002463 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
2464 pWidget->Synchronize(bSynchronizeElse);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002465 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002466 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002467}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002468#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002469
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002470CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName(
2471 const CFX_WideString& sFileExt) {
2472 CFX_WideString sFileName;
2473 return L"";
2474}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002475
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002476FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination,
2477 FX_BOOL bUrlEncoded) {
2478 if (sDestination.IsEmpty())
2479 return FALSE;
2480
Lei Zhang96660d62015-12-14 18:27:25 -08002481 if (!m_pDocument || !m_pInterForm)
2482 return FALSE;
2483
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002484 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002485 CFX_WideString wsPDFFilePath = m_pDocument->GetPath();
tsepez4c3debb2016-04-08 12:20:38 -07002486 CFDF_Document* pFDFDoc = m_pInterForm->ExportToFDF(wsPDFFilePath.AsStringC());
Lei Zhang997de612015-11-04 18:17:53 -08002487 if (!pFDFDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002488 return FALSE;
2489
2490 CFX_ByteTextBuf FdfBuffer;
2491 FX_BOOL bRet = pFDFDoc->WriteBuf(FdfBuffer);
2492 delete pFDFDoc;
2493 if (!bRet)
2494 return FALSE;
2495
2496 uint8_t* pBuffer = FdfBuffer.GetBuffer();
2497 FX_STRSIZE nBufSize = FdfBuffer.GetLength();
2498
2499 if (bUrlEncoded) {
2500 if (!FDFToURLEncodedData(pBuffer, nBufSize))
2501 return FALSE;
2502 }
2503
2504 pEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
2505
Lei Zhangda180e92015-08-14 22:22:13 -07002506 if (bUrlEncoded) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002507 FX_Free(pBuffer);
2508 pBuffer = NULL;
2509 }
2510
2511 return TRUE;
2512}
2513
2514FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) {
tsepezfc58ad12016-04-05 12:22:15 -07002515 CFDF_Document* pFDF =
tsepez4c3debb2016-04-08 12:20:38 -07002516 m_pInterForm->ExportToFDF(m_pDocument->GetPath().AsStringC());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002517 if (!pFDF)
2518 return FALSE;
2519
2520 FX_BOOL bRet = pFDF->WriteBuf(textBuf);
2521 delete pFDF;
2522
2523 return bRet;
2524}
2525
2526FX_BOOL CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) {
Wei Li0fc6b252016-03-01 16:29:41 -08002527 ASSERT(action.GetDict());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002528
2529 CPDF_Dictionary* pActionDict = action.GetDict();
Lei Zhangd88a3642015-11-10 09:38:57 -08002530 if (!pActionDict->KeyExist("Fields"))
2531 return m_pInterForm->ResetForm(true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002532
Wei Li0fc6b252016-03-01 16:29:41 -08002533 CPDF_ActionFields af(&action);
tsepezc3255f52016-03-25 14:52:27 -07002534 uint32_t dwFlags = action.GetFlags();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002535
Lei Zhangd88a3642015-11-10 09:38:57 -08002536 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2537 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
2538 return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002539}
2540
2541FX_BOOL CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action) {
2542 return FALSE;
2543}
2544
Lei Zhangd88a3642015-11-10 09:38:57 -08002545std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects(
2546 const std::vector<CPDF_Object*>& objects) const {
2547 std::vector<CPDF_FormField*> fields;
2548 for (CPDF_Object* pObject : objects) {
2549 if (pObject && pObject->IsString()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002550 CFX_WideString csName = pObject->GetUnicodeText();
2551 CPDF_FormField* pField = m_pInterForm->GetField(0, csName);
Dan Sinclaird43ebdd2015-10-27 15:37:54 -04002552 if (pField)
Lei Zhangd88a3642015-11-10 09:38:57 -08002553 fields.push_back(pField);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002554 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002555 }
Lei Zhangd88a3642015-11-10 09:38:57 -08002556 return fields;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002557}
2558
Tom Sepezd6262c12016-02-03 14:47:06 -08002559int CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField,
2560 const CFX_WideString& csValue) {
2561 int nType = pField->GetFieldType();
2562 if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002563 return 0;
2564
Tom Sepezd6262c12016-02-03 14:47:06 -08002565 if (!OnKeyStrokeCommit(pField, csValue))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002566 return -1;
2567
Tom Sepezd6262c12016-02-03 14:47:06 -08002568 if (!OnValidate(pField, csValue))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002569 return -1;
2570
2571 return 1;
2572}
2573
Tom Sepezd6262c12016-02-03 14:47:06 -08002574void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) {
2575#ifdef PDF_ENABLE_XFA
2576 SynchronizeField(pField, FALSE);
2577#endif // PDF_ENABLE_XFA
2578 int nType = pField->GetFieldType();
2579 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2580 OnCalculate(pField);
2581 FX_BOOL bFormated = FALSE;
2582 CFX_WideString sValue = OnFormat(pField, bFormated);
2583 ResetFieldAppearance(pField, bFormated ? sValue.c_str() : nullptr, TRUE);
2584 UpdateField(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002585 }
Tom Sepezd6262c12016-02-03 14:47:06 -08002586}
2587
2588int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField,
2589 const CFX_WideString& csValue) {
2590 if (pField->GetFieldType() != FIELDTYPE_LISTBOX)
2591 return 0;
2592
2593 if (!OnKeyStrokeCommit(pField, csValue))
2594 return -1;
2595
2596 if (!OnValidate(pField, csValue))
2597 return -1;
2598
2599 return 1;
2600}
2601
2602void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) {
2603 if (pField->GetFieldType() == FIELDTYPE_LISTBOX) {
2604 OnCalculate(pField);
2605 ResetFieldAppearance(pField, NULL, TRUE);
2606 UpdateField(pField);
2607 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002608}
2609
Tom Sepezed5d7aa2016-02-02 16:02:03 -08002610void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) {
2611 int nType = pField->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002612 if (nType == FIELDTYPE_CHECKBOX || nType == FIELDTYPE_RADIOBUTTON) {
Tom Sepezed5d7aa2016-02-02 16:02:03 -08002613 OnCalculate(pField);
2614 UpdateField(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002615 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002616}
2617
Tom Sepezd6262c12016-02-03 14:47:06 -08002618int CPDFSDK_InterForm::BeforeFormReset(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002619 return 0;
2620}
2621
Tom Sepezd6262c12016-02-03 14:47:06 -08002622void CPDFSDK_InterForm::AfterFormReset(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002623 OnCalculate(nullptr);
Tom Sepezd6262c12016-02-03 14:47:06 -08002624}
2625
2626int CPDFSDK_InterForm::BeforeFormImportData(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002627 return 0;
2628}
2629
Tom Sepezd6262c12016-02-03 14:47:06 -08002630void CPDFSDK_InterForm::AfterFormImportData(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002631 OnCalculate(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002632}
2633
2634FX_BOOL CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002635 if (nFieldType < 1 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002636 return FALSE;
2637 return m_bNeedHightlight[nFieldType - 1];
2638}
2639
2640void CPDFSDK_InterForm::RemoveAllHighLight() {
Tom Sepezae7a9172015-11-23 09:22:46 -08002641 for (int i = 0; i < kNumFieldTypes; ++i)
2642 m_bNeedHightlight[i] = FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002643}
Tom Sepezae7a9172015-11-23 09:22:46 -08002644
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002645void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002646 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002647 return;
2648 switch (nFieldType) {
2649 case 0: {
Tom Sepezae7a9172015-11-23 09:22:46 -08002650 for (int i = 0; i < kNumFieldTypes; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002651 m_aHighlightColor[i] = clr;
2652 m_bNeedHightlight[i] = TRUE;
2653 }
2654 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002655 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002656 default: {
2657 m_aHighlightColor[nFieldType - 1] = clr;
2658 m_bNeedHightlight[nFieldType - 1] = TRUE;
2659 break;
2660 }
2661 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002662}
2663
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002664FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002665 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002666 return FXSYS_RGB(255, 255, 255);
2667 if (nFieldType == 0)
2668 return m_aHighlightColor[0];
2669 return m_aHighlightColor[nFieldType - 1];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002670}
2671
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002672CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView,
2673 const CFX_ByteString& sType,
2674 const CFX_ByteString& sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002675 : m_eTabOrder(STRUCTURE),
2676 m_pPageView(pPageView),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002677 m_sType(sType),
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002678 m_sSubType(sSubType) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002679 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage();
Wei Li9b761132016-01-29 15:44:20 -08002680 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringBy("Tabs");
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002681 if (sTabs == "R")
2682 m_eTabOrder = ROW;
2683 else if (sTabs == "C")
2684 m_eTabOrder = COLUMN;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002685
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002686 GenerateResults();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002687}
2688
Dan Sinclairf766ad22016-03-14 13:51:24 -04002689CBA_AnnotIterator::~CBA_AnnotIterator() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002690
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002691CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002692 return m_Annots.empty() ? nullptr : m_Annots.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002693}
2694
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002695CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002696 return m_Annots.empty() ? nullptr : m_Annots.back();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002697}
2698
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002699CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002700 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2701 if (iter == m_Annots.end())
2702 return nullptr;
2703 ++iter;
2704 if (iter == m_Annots.end())
2705 iter = m_Annots.begin();
2706 return *iter;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002707}
2708
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002709CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002710 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2711 if (iter == m_Annots.end())
2712 return nullptr;
2713 if (iter == m_Annots.begin())
2714 iter = m_Annots.end();
2715 return *(--iter);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002716}
2717
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002718// static
2719bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1,
2720 const CPDFSDK_Annot* p2) {
2721 return GetAnnotRect(p1).left < GetAnnotRect(p2).left;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002722}
2723
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002724// static
2725bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1,
2726 const CPDFSDK_Annot* p2) {
2727 return GetAnnotRect(p1).top > GetAnnotRect(p2).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002728}
2729
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002730void CBA_AnnotIterator::GenerateResults() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002731 switch (m_eTabOrder) {
2732 case STRUCTURE: {
Lei Zhangbf60b292015-10-26 12:14:35 -07002733 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002734 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002735 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002736 m_Annots.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002737 }
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002738 } break;
2739 case ROW: {
2740 std::vector<CPDFSDK_Annot*> sa;
Lei Zhang50218532015-10-30 14:03:43 -07002741 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2742 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2743 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002744 sa.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002745 }
2746
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002747 std::sort(sa.begin(), sa.end(), CompareByLeftAscending);
2748 while (!sa.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002749 int nLeftTopIndex = -1;
Lei Zhang50218532015-10-30 14:03:43 -07002750 FX_FLOAT fTop = 0.0f;
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002751 for (int i = sa.size() - 1; i >= 0; i--) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002752 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002753 if (rcAnnot.top > fTop) {
2754 nLeftTopIndex = i;
2755 fTop = rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002756 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002757 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002758 if (nLeftTopIndex >= 0) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002759 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
Tom Sepez281a9ea2016-02-26 14:24:28 -08002760 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002761 m_Annots.push_back(pLeftTopAnnot);
2762 sa.erase(sa.begin() + nLeftTopIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002763
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002764 std::vector<int> aSelect;
Wei Li89409932016-03-28 10:33:33 -07002765 for (size_t i = 0; i < sa.size(); ++i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002766 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002767 FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f;
2768 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002769 aSelect.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002770 }
Wei Li89409932016-03-28 10:33:33 -07002771 for (size_t i = 0; i < aSelect.size(); ++i)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002772 m_Annots.push_back(sa[aSelect[i]]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002773
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002774 for (int i = aSelect.size() - 1; i >= 0; --i)
2775 sa.erase(sa.begin() + aSelect[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002776 }
2777 }
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002778 } break;
2779 case COLUMN: {
2780 std::vector<CPDFSDK_Annot*> sa;
Lei Zhang50218532015-10-30 14:03:43 -07002781 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2782 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2783 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002784 sa.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002785 }
2786
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002787 std::sort(sa.begin(), sa.end(), CompareByTopDescending);
2788 while (!sa.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002789 int nLeftTopIndex = -1;
Lei Zhang50218532015-10-30 14:03:43 -07002790 FX_FLOAT fLeft = -1.0f;
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002791 for (int i = sa.size() - 1; i >= 0; --i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002792 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002793 if (fLeft < 0) {
2794 nLeftTopIndex = 0;
2795 fLeft = rcAnnot.left;
2796 } else if (rcAnnot.left < fLeft) {
2797 nLeftTopIndex = i;
2798 fLeft = rcAnnot.left;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002799 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002800 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002801
2802 if (nLeftTopIndex >= 0) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002803 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
Tom Sepez281a9ea2016-02-26 14:24:28 -08002804 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002805 m_Annots.push_back(pLeftTopAnnot);
2806 sa.erase(sa.begin() + nLeftTopIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002807
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002808 std::vector<int> aSelect;
Wei Li89409932016-03-28 10:33:33 -07002809 for (size_t i = 0; i < sa.size(); ++i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002810 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002811 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f;
2812 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002813 aSelect.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002814 }
Wei Li89409932016-03-28 10:33:33 -07002815 for (size_t i = 0; i < aSelect.size(); ++i)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002816 m_Annots.push_back(sa[aSelect[i]]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002817
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002818 for (int i = aSelect.size() - 1; i >= 0; --i)
2819 sa.erase(sa.begin() + aSelect[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002820 }
2821 }
Lei Zhang50218532015-10-30 14:03:43 -07002822 break;
2823 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002824 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002825}
2826
Tom Sepez281a9ea2016-02-26 14:24:28 -08002827CFX_FloatRect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) {
2828 CFX_FloatRect rcAnnot;
Lei Zhang50218532015-10-30 14:03:43 -07002829 pAnnot->GetPDFAnnot()->GetRect(rcAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002830 return rcAnnot;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002831}