blob: 1e124623f7df15719386f3cf51e70e9c3ce3eb64 [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>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080010#include <memory>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050011#include <vector>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080012
Dan Sinclair455a4192016-03-16 09:48:56 -040013#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040014#include "core/fpdfapi/fpdf_parser/include/cfdf_document.h"
15#include "core/fpdfapi/fpdf_parser/include/cpdf_array.h"
16#include "core/fpdfapi/fpdf_parser/include/cpdf_document.h"
Dan Sinclair584b1e62016-03-21 09:15:45 -040017#include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h"
Dan Sinclairedbb3192016-03-21 09:08:24 -040018#include "fpdfsdk/formfiller/cffl_formfiller.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080019#include "fpdfsdk/include/fsdk_actionhandler.h"
20#include "fpdfsdk/include/fsdk_baseannot.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080021#include "fpdfsdk/include/fsdk_define.h"
22#include "fpdfsdk/include/fsdk_mgr.h"
23#include "fpdfsdk/include/javascript/IJavaScript.h"
dan sinclair89e904b2016-03-23 19:29:15 -040024#include "fpdfsdk/pdfwindow/PWL_Utils.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
Tom Sepez40e9ff32015-11-30 12:39:54 -080026#ifdef PDF_ENABLE_XFA
Lei Zhang875b9c92016-01-08 13:51:10 -080027#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
28#include "fpdfsdk/include/fpdfxfa/fpdfxfa_util.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080029#endif // PDF_ENABLE_XFA
30
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031#define IsFloatZero(f) ((f) < 0.01 && (f) > -0.01)
32#define IsFloatBigger(fa, fb) ((fa) > (fb) && !IsFloatZero((fa) - (fb)))
33#define IsFloatSmaller(fa, fb) ((fa) < (fb) && !IsFloatZero((fa) - (fb)))
34#define IsFloatEqual(fa, fb) IsFloatZero((fa) - (fb))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036CPDFSDK_Widget::CPDFSDK_Widget(CPDF_Annot* pAnnot,
37 CPDFSDK_PageView* pPageView,
38 CPDFSDK_InterForm* pInterForm)
39 : CPDFSDK_BAAnnot(pAnnot, pPageView),
40 m_pInterForm(pInterForm),
41 m_nAppAge(0),
Tom Sepez40e9ff32015-11-30 12:39:54 -080042 m_nValueAge(0)
43#ifdef PDF_ENABLE_XFA
44 ,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 m_hMixXFAWidget(NULL),
Tom Sepez40e9ff32015-11-30 12:39:54 -080046 m_pWidgetHandler(NULL)
47#endif // PDF_ENABLE_XFA
48{
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049}
50
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051CPDFSDK_Widget::~CPDFSDK_Widget() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
Tom Sepez51da0932015-11-25 16:05:49 -080053#ifdef PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -070054IXFA_Widget* CPDFSDK_Widget::GetMixXFAWidget() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080056 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
58 if (!m_hMixXFAWidget) {
59 if (IXFA_DocView* pDocView = pDoc->GetXFADocView()) {
60 CFX_WideString sName;
Lei Zhang99766722016-02-23 11:21:48 -080061 if (GetFieldType() == FIELDTYPE_RADIOBUTTON) {
62 sName = GetAnnotName();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 if (sName.IsEmpty())
64 sName = GetName();
Dan Sinclair738b08c2016-03-01 14:45:20 -050065 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 sName = GetName();
Dan Sinclair738b08c2016-03-01 14:45:20 -050067 }
Bo Xufdc00a72014-10-28 23:03:33 -070068
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 if (!sName.IsEmpty())
70 m_hMixXFAWidget = pDocView->GetWidgetByName(sName);
71 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -070072 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 return m_hMixXFAWidget;
74 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070075
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 return NULL;
77}
78
79IXFA_Widget* CPDFSDK_Widget::GetGroupMixXFAWidget() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080081 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
83 if (IXFA_DocView* pDocView = pDoc->GetXFADocView()) {
84 CFX_WideString sName = GetName();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 if (!sName.IsEmpty())
86 return pDocView->GetWidgetByName(sName);
87 }
88 }
89
Tom Sepez50d12ad2015-11-24 09:50:51 -080090 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091}
92
Tom Sepezbf59a072015-10-21 14:07:23 -070093IXFA_WidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -080095 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
97 if (!m_pWidgetHandler) {
98 if (IXFA_DocView* pDocView = pDoc->GetXFADocView()) {
99 m_pWidgetHandler = pDocView->GetWidgetHandler();
100 }
101 }
102 return m_pWidgetHandler;
103 }
104
105 return NULL;
106}
107
108static XFA_EVENTTYPE GetXFAEventType(PDFSDK_XFAAActionType eXFAAAT) {
109 XFA_EVENTTYPE eEventType = XFA_EVENT_Unknown;
110
111 switch (eXFAAAT) {
112 case PDFSDK_XFA_Click:
113 eEventType = XFA_EVENT_Click;
114 break;
115 case PDFSDK_XFA_Full:
116 eEventType = XFA_EVENT_Full;
117 break;
118 case PDFSDK_XFA_PreOpen:
119 eEventType = XFA_EVENT_PreOpen;
120 break;
121 case PDFSDK_XFA_PostOpen:
122 eEventType = XFA_EVENT_PostOpen;
123 break;
124 }
125
126 return eEventType;
127}
128
129static XFA_EVENTTYPE GetXFAEventType(CPDF_AAction::AActionType eAAT,
130 FX_BOOL bWillCommit) {
131 XFA_EVENTTYPE eEventType = XFA_EVENT_Unknown;
132
133 switch (eAAT) {
134 case CPDF_AAction::CursorEnter:
135 eEventType = XFA_EVENT_MouseEnter;
136 break;
137 case CPDF_AAction::CursorExit:
138 eEventType = XFA_EVENT_MouseExit;
139 break;
140 case CPDF_AAction::ButtonDown:
141 eEventType = XFA_EVENT_MouseDown;
142 break;
143 case CPDF_AAction::ButtonUp:
144 eEventType = XFA_EVENT_MouseUp;
145 break;
146 case CPDF_AAction::GetFocus:
147 eEventType = XFA_EVENT_Enter;
148 break;
149 case CPDF_AAction::LoseFocus:
150 eEventType = XFA_EVENT_Exit;
151 break;
152 case CPDF_AAction::PageOpen:
153 break;
154 case CPDF_AAction::PageClose:
155 break;
156 case CPDF_AAction::PageVisible:
157 break;
158 case CPDF_AAction::PageInvisible:
159 break;
160 case CPDF_AAction::KeyStroke:
161 if (!bWillCommit) {
162 eEventType = XFA_EVENT_Change;
163 }
164 break;
165 case CPDF_AAction::Validate:
166 eEventType = XFA_EVENT_Validate;
167 break;
168 case CPDF_AAction::OpenPage:
169 case CPDF_AAction::ClosePage:
170 case CPDF_AAction::Format:
171 case CPDF_AAction::Calculate:
172 case CPDF_AAction::CloseDocument:
173 case CPDF_AAction::SaveDocument:
174 case CPDF_AAction::DocumentSaved:
175 case CPDF_AAction::PrintDocument:
176 case CPDF_AAction::DocumentPrinted:
177 break;
178 }
179
180 return eEventType;
181}
182
183FX_BOOL CPDFSDK_Widget::HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT) {
Lei Zhang99766722016-02-23 11:21:48 -0800184 if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
185 if (IXFA_WidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
187
188 if ((eEventType == XFA_EVENT_Click || eEventType == XFA_EVENT_Change) &&
189 GetFieldType() == FIELDTYPE_RADIOBUTTON) {
190 if (IXFA_Widget* hGroupWidget = GetGroupMixXFAWidget()) {
191 CXFA_WidgetAcc* pAcc = pXFAWidgetHandler->GetDataAcc(hGroupWidget);
192 if (pXFAWidgetHandler->HasEvent(pAcc, eEventType))
193 return TRUE;
194 }
195 }
196
197 {
198 CXFA_WidgetAcc* pAcc = pXFAWidgetHandler->GetDataAcc(hWidget);
199 return pXFAWidgetHandler->HasEvent(pAcc, eEventType);
200 }
201 }
202 }
203
204 return FALSE;
205}
206
207FX_BOOL CPDFSDK_Widget::OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
208 PDFSDK_FieldAction& data,
209 CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800211 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
212 if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
214
215 if (eEventType != XFA_EVENT_Unknown) {
Lei Zhang99766722016-02-23 11:21:48 -0800216 if (IXFA_WidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 CXFA_EventParam param;
218 param.m_eType = eEventType;
219 param.m_wsChange = data.sChange;
220 param.m_iCommitKey = data.nCommitKey;
221 param.m_bShift = data.bShift;
222 param.m_iSelStart = data.nSelStart;
223 param.m_iSelEnd = data.nSelEnd;
224 param.m_wsFullText = data.sValue;
225 param.m_bKeyDown = data.bKeyDown;
226 param.m_bModifier = data.bModifier;
227 param.m_wsNewText = data.sValue;
228 if (data.nSelEnd > data.nSelStart)
229 param.m_wsNewText.Delete(data.nSelStart,
230 data.nSelEnd - data.nSelStart);
231 for (int i = 0; i < data.sChange.GetLength(); i++)
232 param.m_wsNewText.Insert(data.nSelStart, data.sChange[i]);
233 param.m_wsPrevText = data.sValue;
234
235 if ((eEventType == XFA_EVENT_Click || eEventType == XFA_EVENT_Change) &&
236 GetFieldType() == FIELDTYPE_RADIOBUTTON) {
237 if (IXFA_Widget* hGroupWidget = GetGroupMixXFAWidget()) {
238 CXFA_WidgetAcc* pAcc = pXFAWidgetHandler->GetDataAcc(hGroupWidget);
239 param.m_pTarget = pAcc;
Wei Li42d1c1c2016-03-22 12:52:50 -0700240 if (pXFAWidgetHandler->ProcessEvent(pAcc, &param) !=
241 XFA_EVENTERROR_Success) {
242 return FALSE;
243 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 }
Wei Li42d1c1c2016-03-22 12:52:50 -0700246 CXFA_WidgetAcc* pAcc = pXFAWidgetHandler->GetDataAcc(hWidget);
247 param.m_pTarget = pAcc;
248 int32_t nRet = pXFAWidgetHandler->ProcessEvent(pAcc, &param);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249
250 if (IXFA_DocView* pDocView = pDoc->GetXFADocView()) {
251 pDocView->UpdateDocView();
252 }
Wei Li42d1c1c2016-03-22 12:52:50 -0700253 return nRet == XFA_EVENTERROR_Success;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 }
255 }
256 }
257
258 return FALSE;
259}
260
261void CPDFSDK_Widget::Synchronize(FX_BOOL bSynchronizeElse) {
Lei Zhang99766722016-02-23 11:21:48 -0800262 if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
263 if (IXFA_WidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 if (CXFA_WidgetAcc* pWidgetAcc = pXFAWidgetHandler->GetDataAcc(hWidget)) {
266 switch (GetFieldType()) {
267 case FIELDTYPE_CHECKBOX:
268 case FIELDTYPE_RADIOBUTTON: {
269 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 XFA_CHECKSTATE eCheckState =
271 pFormCtrl->IsChecked() ? XFA_CHECKSTATE_On : XFA_CHECKSTATE_Off;
272 pWidgetAcc->SetCheckState(eCheckState);
273 } break;
274 case FIELDTYPE_TEXTFIELD:
275 pWidgetAcc->SetValue(pFormField->GetValue(), XFA_VALUEPICTURE_Edit);
276 break;
277 case FIELDTYPE_LISTBOX: {
278 pWidgetAcc->ClearAllSelections();
279
280 for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz;
281 i++) {
282 int nIndex = pFormField->GetSelectedIndex(i);
283 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
284 pWidgetAcc->SetItemState(nIndex, TRUE, FALSE);
285 }
286 } break;
287 case FIELDTYPE_COMBOBOX: {
288 pWidgetAcc->ClearAllSelections();
289
290 for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz;
291 i++) {
292 int nIndex = pFormField->GetSelectedIndex(i);
293 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
294 pWidgetAcc->SetItemState(nIndex, TRUE, FALSE);
295 }
296 }
297
298 pWidgetAcc->SetValue(pFormField->GetValue(), XFA_VALUEPICTURE_Edit);
299 break;
300 }
301
302 if (bSynchronizeElse)
303 pWidgetAcc->ProcessValueChanged();
304 }
305 }
306 }
307}
308
309void CPDFSDK_Widget::SynchronizeXFAValue() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800311 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 IXFA_DocView* pXFADocView = pDoc->GetXFADocView();
313 if (!pXFADocView)
314 return;
315
Tom Sepezbf59a072015-10-21 14:07:23 -0700316 if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
317 if (GetXFAWidgetHandler()) {
318 CPDFSDK_Widget::SynchronizeXFAValue(pXFADocView, hWidget, GetFormField(),
319 GetFormControl());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 }
321 }
322}
323
324void CPDFSDK_Widget::SynchronizeXFAItems() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800326 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 IXFA_DocView* pXFADocView = pDoc->GetXFADocView();
328 if (!pXFADocView)
329 return;
330
Tom Sepezbf59a072015-10-21 14:07:23 -0700331 if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
332 if (GetXFAWidgetHandler())
333 SynchronizeXFAItems(pXFADocView, hWidget, GetFormField(), nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 }
335}
336
337void CPDFSDK_Widget::SynchronizeXFAValue(IXFA_DocView* pXFADocView,
338 IXFA_Widget* hWidget,
339 CPDF_FormField* pFormField,
340 CPDF_FormControl* pFormControl) {
Lei Zhang5eca3052016-02-22 20:32:21 -0800341 ASSERT(hWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342
343 if (IXFA_WidgetHandler* pXFAWidgetHandler = pXFADocView->GetWidgetHandler()) {
Lei Zhang5eca3052016-02-22 20:32:21 -0800344 ASSERT(pFormControl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345
346 switch (pFormField->GetFieldType()) {
347 case FIELDTYPE_CHECKBOX: {
348 if (CXFA_WidgetAcc* pWidgetAcc =
349 pXFAWidgetHandler->GetDataAcc(hWidget)) {
Wei Lie4076482016-03-15 10:58:40 -0700350 pFormField->CheckControl(
351 pFormField->GetControlIndex(pFormControl),
352 pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 }
354 } break;
355 case FIELDTYPE_RADIOBUTTON: {
Wei Lie4076482016-03-15 10:58:40 -0700356 // TODO(weili): Check whether we need to handle checkbox and radio
357 // button differently, otherwise, merge these two cases.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358 if (CXFA_WidgetAcc* pWidgetAcc =
359 pXFAWidgetHandler->GetDataAcc(hWidget)) {
Wei Lie4076482016-03-15 10:58:40 -0700360 pFormField->CheckControl(
361 pFormField->GetControlIndex(pFormControl),
362 pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 }
364 } break;
365 case FIELDTYPE_TEXTFIELD: {
366 if (CXFA_WidgetAcc* pWidgetAcc =
367 pXFAWidgetHandler->GetDataAcc(hWidget)) {
368 CFX_WideString sValue;
369 pWidgetAcc->GetValue(sValue, XFA_VALUEPICTURE_Display);
370 pFormField->SetValue(sValue, TRUE);
371 }
372 } break;
373 case FIELDTYPE_LISTBOX: {
374 pFormField->ClearSelection(FALSE);
375
376 if (CXFA_WidgetAcc* pWidgetAcc =
377 pXFAWidgetHandler->GetDataAcc(hWidget)) {
378 for (int i = 0, sz = pWidgetAcc->CountSelectedItems(); i < sz; i++) {
379 int nIndex = pWidgetAcc->GetSelectedItem(i);
380
381 if (nIndex > -1 && nIndex < pFormField->CountOptions()) {
382 pFormField->SetItemSelection(nIndex, TRUE, TRUE);
383 }
384 }
385 }
386 } break;
387 case FIELDTYPE_COMBOBOX: {
388 pFormField->ClearSelection(FALSE);
389
390 if (CXFA_WidgetAcc* pWidgetAcc =
391 pXFAWidgetHandler->GetDataAcc(hWidget)) {
392 for (int i = 0, sz = pWidgetAcc->CountSelectedItems(); i < sz; i++) {
393 int nIndex = pWidgetAcc->GetSelectedItem(i);
394
395 if (nIndex > -1 && nIndex < pFormField->CountOptions()) {
396 pFormField->SetItemSelection(nIndex, TRUE, TRUE);
397 }
398 }
399
400 CFX_WideString sValue;
401 pWidgetAcc->GetValue(sValue, XFA_VALUEPICTURE_Display);
402 pFormField->SetValue(sValue, TRUE);
403 }
404 } break;
405 }
406 }
407}
408
409void CPDFSDK_Widget::SynchronizeXFAItems(IXFA_DocView* pXFADocView,
410 IXFA_Widget* hWidget,
411 CPDF_FormField* pFormField,
412 CPDF_FormControl* pFormControl) {
Lei Zhang5eca3052016-02-22 20:32:21 -0800413 ASSERT(hWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414
415 if (IXFA_WidgetHandler* pXFAWidgetHandler = pXFADocView->GetWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 switch (pFormField->GetFieldType()) {
417 case FIELDTYPE_LISTBOX: {
418 pFormField->ClearSelection(FALSE);
419 pFormField->ClearOptions(TRUE);
420
421 if (CXFA_WidgetAcc* pWidgetAcc =
422 pXFAWidgetHandler->GetDataAcc(hWidget)) {
423 for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(); i < sz;
424 i++) {
425 CFX_WideString swText;
426 pWidgetAcc->GetChoiceListItem(swText, i);
427
428 pFormField->InsertOption(swText, i, TRUE);
429 }
430 }
431 } break;
432 case FIELDTYPE_COMBOBOX: {
433 pFormField->ClearSelection(FALSE);
434 pFormField->ClearOptions(FALSE);
435
436 if (CXFA_WidgetAcc* pWidgetAcc =
437 pXFAWidgetHandler->GetDataAcc(hWidget)) {
438 for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(); i < sz;
439 i++) {
440 CFX_WideString swText;
441 pWidgetAcc->GetChoiceListItem(swText, i);
442
443 pFormField->InsertOption(swText, i, FALSE);
444 }
445 }
446
447 pFormField->SetValue(L"", TRUE);
448 } break;
449 }
450 }
451}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800452#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453
454FX_BOOL CPDFSDK_Widget::IsWidgetAppearanceValid(
455 CPDF_Annot::AppearanceMode mode) {
Wei Li9b761132016-01-29 15:44:20 -0800456 CPDF_Dictionary* pAP = m_pAnnot->GetAnnotDict()->GetDictBy("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457 if (!pAP)
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700458 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700459
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 // Choose the right sub-ap
461 const FX_CHAR* ap_entry = "N";
462 if (mode == CPDF_Annot::Down)
463 ap_entry = "D";
464 else if (mode == CPDF_Annot::Rollover)
465 ap_entry = "R";
466 if (!pAP->KeyExist(ap_entry))
467 ap_entry = "N";
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700468
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469 // Get the AP stream or subdirectory
470 CPDF_Object* psub = pAP->GetElementValue(ap_entry);
471 if (!psub)
472 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700473
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474 int nFieldType = GetFieldType();
475 switch (nFieldType) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700476 case FIELDTYPE_PUSHBUTTON:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700477 case FIELDTYPE_COMBOBOX:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700478 case FIELDTYPE_LISTBOX:
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700479 case FIELDTYPE_TEXTFIELD:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 case FIELDTYPE_SIGNATURE:
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400481 return psub->IsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482 case FIELDTYPE_CHECKBOX:
483 case FIELDTYPE_RADIOBUTTON:
Dan Sinclairf1251c12015-10-20 16:24:45 -0400484 if (CPDF_Dictionary* pSubDict = psub->AsDictionary()) {
Wei Li9b761132016-01-29 15:44:20 -0800485 return pSubDict->GetStreamBy(GetAppState()) != NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 }
487 return FALSE;
488 }
489 return TRUE;
490}
491
492int CPDFSDK_Widget::GetFieldType() const {
Lei Zhang96660d62015-12-14 18:27:25 -0800493 return GetFormField()->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494}
495
496FX_BOOL CPDFSDK_Widget::IsAppearanceValid() {
Tom Sepez51da0932015-11-25 16:05:49 -0800497#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800499 CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500 int nDocType = pDoc->GetDocType();
Tom Sepez540c4362015-11-24 13:33:57 -0800501 if (nDocType != DOCTYPE_PDF && nDocType != DOCTYPE_STATIC_XFA)
502 return TRUE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800503#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800504 return CPDFSDK_BAAnnot::IsAppearanceValid();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505}
506
507int CPDFSDK_Widget::GetFieldFlags() const {
508 CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 CPDF_FormControl* pFormControl =
510 pPDFInterForm->GetControlByDict(m_pAnnot->GetAnnotDict());
511 CPDF_FormField* pFormField = pFormControl->GetField();
512 return pFormField->GetFieldFlags();
513}
514
515CFX_ByteString CPDFSDK_Widget::GetSubType() const {
516 int nType = GetFieldType();
517
518 if (nType == FIELDTYPE_SIGNATURE)
519 return BFFT_SIGNATURE;
520 return CPDFSDK_Annot::GetSubType();
521}
522
523CPDF_FormField* CPDFSDK_Widget::GetFormField() const {
Lei Zhang1b700c32015-10-30 23:55:35 -0700524 return GetFormControl()->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525}
526
527CPDF_FormControl* CPDFSDK_Widget::GetFormControl() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528 CPDF_InterForm* pPDFInterForm = m_pInterForm->GetInterForm();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700529 return pPDFInterForm->GetControlByDict(GetAnnotDict());
530}
531
Lei Zhang1b700c32015-10-30 23:55:35 -0700532CPDF_FormControl* CPDFSDK_Widget::GetFormControl(
533 CPDF_InterForm* pInterForm,
534 const CPDF_Dictionary* pAnnotDict) {
Lei Zhang96660d62015-12-14 18:27:25 -0800535 ASSERT(pAnnotDict);
Lei Zhang1b700c32015-10-30 23:55:35 -0700536 return pInterForm->GetControlByDict(pAnnotDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537}
538
539int CPDFSDK_Widget::GetRotate() const {
540 CPDF_FormControl* pCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 return pCtrl->GetRotation() % 360;
542}
543
Tom Sepez51da0932015-11-25 16:05:49 -0800544#ifdef PDF_ENABLE_XFA
Tom Sepezbf59a072015-10-21 14:07:23 -0700545CFX_WideString CPDFSDK_Widget::GetName() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700547 return pFormField->GetFullName();
548}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800549#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550
551FX_BOOL CPDFSDK_Widget::GetFillColor(FX_COLORREF& color) const {
552 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553 int iColorType = 0;
554 color = FX_ARGBTOCOLORREF(pFormCtrl->GetBackgroundColor(iColorType));
555
556 return iColorType != COLORTYPE_TRANSPARENT;
557}
558
559FX_BOOL CPDFSDK_Widget::GetBorderColor(FX_COLORREF& color) const {
560 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 int iColorType = 0;
562 color = FX_ARGBTOCOLORREF(pFormCtrl->GetBorderColor(iColorType));
563
564 return iColorType != COLORTYPE_TRANSPARENT;
565}
566
567FX_BOOL CPDFSDK_Widget::GetTextColor(FX_COLORREF& color) const {
568 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
570 if (da.HasColor()) {
571 FX_ARGB argb;
572 int iColorType = COLORTYPE_TRANSPARENT;
573 da.GetColor(argb, iColorType);
574 color = FX_ARGBTOCOLORREF(argb);
575
576 return iColorType != COLORTYPE_TRANSPARENT;
577 }
578
579 return FALSE;
580}
581
582FX_FLOAT CPDFSDK_Widget::GetFontSize() const {
583 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 CPDF_DefaultAppearance pDa = pFormCtrl->GetDefaultAppearance();
585 CFX_ByteString csFont = "";
586 FX_FLOAT fFontSize = 0.0f;
587 pDa.GetFont(csFont, fFontSize);
588
589 return fFontSize;
590}
591
Tom Sepezbf59a072015-10-21 14:07:23 -0700592int CPDFSDK_Widget::GetSelectedIndex(int nIndex) const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700593#ifdef PDF_ENABLE_XFA
Lei Zhang99766722016-02-23 11:21:48 -0800594 if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
595 if (IXFA_WidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596 if (CXFA_WidgetAcc* pWidgetAcc = pXFAWidgetHandler->GetDataAcc(hWidget)) {
597 if (nIndex < pWidgetAcc->CountSelectedItems())
598 return pWidgetAcc->GetSelectedItem(nIndex);
599 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700600 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700602#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 return pFormField->GetSelectedIndex(nIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700605}
606
Tom Sepeza8a39e22015-10-12 15:47:07 -0700607#ifdef PDF_ENABLE_XFA
Tom Sepez40e9ff32015-11-30 12:39:54 -0800608CFX_WideString CPDFSDK_Widget::GetValue(FX_BOOL bDisplay) const {
Lei Zhang99766722016-02-23 11:21:48 -0800609 if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
610 if (IXFA_WidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 if (CXFA_WidgetAcc* pWidgetAcc = pXFAWidgetHandler->GetDataAcc(hWidget)) {
612 CFX_WideString sValue;
613 pWidgetAcc->GetValue(sValue, bDisplay ? XFA_VALUEPICTURE_Display
614 : XFA_VALUEPICTURE_Edit);
615 return sValue;
616 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700617 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800619#else
620CFX_WideString CPDFSDK_Widget::GetValue() const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700621#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700622 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700623 return pFormField->GetValue();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700624}
625
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700626CFX_WideString CPDFSDK_Widget::GetDefaultValue() const {
627 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700628 return pFormField->GetDefaultValue();
629}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700630
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700631CFX_WideString CPDFSDK_Widget::GetOptionLabel(int nIndex) const {
632 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700633 return pFormField->GetOptionLabel(nIndex);
634}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700635
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700636int CPDFSDK_Widget::CountOptions() const {
637 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 return pFormField->CountOptions();
639}
640
Tom Sepezbf59a072015-10-21 14:07:23 -0700641FX_BOOL CPDFSDK_Widget::IsOptionSelected(int nIndex) const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700642#ifdef PDF_ENABLE_XFA
Lei Zhang99766722016-02-23 11:21:48 -0800643 if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
644 if (IXFA_WidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 if (CXFA_WidgetAcc* pWidgetAcc = pXFAWidgetHandler->GetDataAcc(hWidget)) {
646 if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
647 return pWidgetAcc->GetItemState(nIndex);
648
649 return FALSE;
650 }
651 }
652 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700653#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700654 CPDF_FormField* pFormField = GetFormField();
655 return pFormField->IsItemSelected(nIndex);
656}
657
658int CPDFSDK_Widget::GetTopVisibleIndex() const {
659 CPDF_FormField* pFormField = GetFormField();
660 return pFormField->GetTopVisibleIndex();
661}
662
Wei Li97da9762016-03-11 17:00:48 -0800663bool CPDFSDK_Widget::IsChecked() const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700664#ifdef PDF_ENABLE_XFA
Lei Zhang99766722016-02-23 11:21:48 -0800665 if (IXFA_WidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
666 if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
Tom Sepez007e6c02016-02-26 14:31:56 -0800667 if (CXFA_WidgetAcc* pWidgetAcc = pXFAWidgetHandler->GetDataAcc(hWidget))
668 return pWidgetAcc->GetCheckState() == XFA_CHECKSTATE_On;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669 }
670 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700671#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700673 return pFormCtrl->IsChecked();
674}
675
676int CPDFSDK_Widget::GetAlignment() const {
677 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700678 return pFormCtrl->GetControlAlignment();
679}
680
681int CPDFSDK_Widget::GetMaxLen() const {
682 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700683 return pFormField->GetMaxLen();
684}
685
Wei Li97da9762016-03-11 17:00:48 -0800686void CPDFSDK_Widget::SetCheck(bool bChecked, bool bNotify) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 CPDF_FormField* pFormField = pFormCtrl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700689 pFormField->CheckControl(pFormField->GetControlIndex(pFormCtrl), bChecked,
690 bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800691#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700692 if (!IsWidgetAppearanceValid(CPDF_Annot::Normal))
693 ResetAppearance(TRUE);
694 if (!bNotify)
695 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800696#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700697}
698
699void CPDFSDK_Widget::SetValue(const CFX_WideString& sValue, FX_BOOL bNotify) {
700 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700701 pFormField->SetValue(sValue, bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800702#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700703 if (!bNotify)
704 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800705#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700706}
707
708void CPDFSDK_Widget::SetDefaultValue(const CFX_WideString& sValue) {}
709void CPDFSDK_Widget::SetOptionSelection(int index,
710 FX_BOOL bSelected,
711 FX_BOOL bNotify) {
712 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700713 pFormField->SetItemSelection(index, bSelected, bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800714#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715 if (!bNotify)
716 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800717#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718}
719
720void CPDFSDK_Widget::ClearSelection(FX_BOOL bNotify) {
721 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700722 pFormField->ClearSelection(bNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800723#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724 if (!bNotify)
725 Synchronize(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800726#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727}
728
729void CPDFSDK_Widget::SetTopVisibleIndex(int index) {}
730
731void CPDFSDK_Widget::SetAppModified() {
732 m_bAppModified = TRUE;
733}
734
735void CPDFSDK_Widget::ClearAppModified() {
736 m_bAppModified = FALSE;
737}
738
739FX_BOOL CPDFSDK_Widget::IsAppModified() const {
740 return m_bAppModified;
741}
742
Tom Sepez51da0932015-11-25 16:05:49 -0800743#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700744void CPDFSDK_Widget::ResetAppearance(FX_BOOL bValueChanged) {
745 switch (GetFieldType()) {
746 case FIELDTYPE_TEXTFIELD:
747 case FIELDTYPE_COMBOBOX: {
748 FX_BOOL bFormated = FALSE;
Lei Zhang99766722016-02-23 11:21:48 -0800749 CFX_WideString sValue = OnFormat(bFormated);
750 ResetAppearance(bFormated ? sValue : nullptr, TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751 } break;
752 default:
Lei Zhang99766722016-02-23 11:21:48 -0800753 ResetAppearance(nullptr, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700754 break;
755 }
756}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800757#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700758
759void CPDFSDK_Widget::ResetAppearance(const FX_WCHAR* sValue,
760 FX_BOOL bValueChanged) {
761 SetAppModified();
762
763 m_nAppAge++;
764 if (m_nAppAge > 999999)
765 m_nAppAge = 0;
766 if (bValueChanged)
767 m_nValueAge++;
768
769 int nFieldType = GetFieldType();
770
771 switch (nFieldType) {
772 case FIELDTYPE_PUSHBUTTON:
773 ResetAppearance_PushButton();
774 break;
775 case FIELDTYPE_CHECKBOX:
776 ResetAppearance_CheckBox();
777 break;
778 case FIELDTYPE_RADIOBUTTON:
779 ResetAppearance_RadioButton();
780 break;
781 case FIELDTYPE_COMBOBOX:
782 ResetAppearance_ComboBox(sValue);
783 break;
784 case FIELDTYPE_LISTBOX:
785 ResetAppearance_ListBox();
786 break;
787 case FIELDTYPE_TEXTFIELD:
788 ResetAppearance_TextField(sValue);
789 break;
790 }
791
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792 m_pAnnot->ClearCachedAP();
793}
794
795CFX_WideString CPDFSDK_Widget::OnFormat(FX_BOOL& bFormated) {
796 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800797 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700798 return m_pInterForm->OnFormat(pFormField, bFormated);
799}
800
801void CPDFSDK_Widget::ResetFieldAppearance(FX_BOOL bValueChanged) {
802 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800803 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700804 m_pInterForm->ResetFieldAppearance(pFormField, NULL, bValueChanged);
805}
806
807void CPDFSDK_Widget::DrawAppearance(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800808 const CFX_Matrix* pUser2Device,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700809 CPDF_Annot::AppearanceMode mode,
810 const CPDF_RenderOptions* pOptions) {
811 int nFieldType = GetFieldType();
812
813 if ((nFieldType == FIELDTYPE_CHECKBOX ||
814 nFieldType == FIELDTYPE_RADIOBUTTON) &&
815 mode == CPDF_Annot::Normal &&
816 !IsWidgetAppearanceValid(CPDF_Annot::Normal)) {
817 CFX_PathData pathData;
818
Tom Sepez281a9ea2016-02-26 14:24:28 -0800819 CFX_FloatRect rcAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820
821 pathData.AppendRect(rcAnnot.left, rcAnnot.bottom, rcAnnot.right,
822 rcAnnot.top);
823
824 CFX_GraphStateData gsd;
825 gsd.m_LineWidth = 0.0f;
826
827 pDevice->DrawPath(&pathData, pUser2Device, &gsd, 0, 0xFFAAAAAA,
828 FXFILL_ALTERNATE);
829 } else {
830 CPDFSDK_BAAnnot::DrawAppearance(pDevice, pUser2Device, mode, pOptions);
831 }
832}
833
834void CPDFSDK_Widget::UpdateField() {
835 CPDF_FormField* pFormField = GetFormField();
Lei Zhang96660d62015-12-14 18:27:25 -0800836 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700837 m_pInterForm->UpdateField(pFormField);
838}
839
840void CPDFSDK_Widget::DrawShadow(CFX_RenderDevice* pDevice,
841 CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700842 int nFieldType = GetFieldType();
843 if (m_pInterForm->IsNeedHighLight(nFieldType)) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800844 CFX_FloatRect rc = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845 FX_COLORREF color = m_pInterForm->GetHighlightColor(nFieldType);
846 uint8_t alpha = m_pInterForm->GetHighlightAlpha();
847
848 CFX_FloatRect rcDevice;
849 ASSERT(m_pInterForm->GetDocument());
850 CPDFDoc_Environment* pEnv = m_pInterForm->GetDocument()->GetEnv();
851 if (!pEnv)
852 return;
Tom Sepez60d909e2015-12-10 15:34:55 -0800853 CFX_Matrix page2device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 pPageView->GetCurrentMatrix(page2device);
855 page2device.Transform(((FX_FLOAT)rc.left), ((FX_FLOAT)rc.bottom),
856 rcDevice.left, rcDevice.bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700857 page2device.Transform(((FX_FLOAT)rc.right), ((FX_FLOAT)rc.top),
858 rcDevice.right, rcDevice.top);
859
860 rcDevice.Normalize();
861
862 FX_ARGB argb = ArgbEncode((int)alpha, color);
863 FX_RECT rcDev((int)rcDevice.left, (int)rcDevice.top, (int)rcDevice.right,
864 (int)rcDevice.bottom);
865 pDevice->FillRect(&rcDev, argb);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866 }
867}
868
869void CPDFSDK_Widget::ResetAppearance_PushButton() {
870 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800871 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872 int32_t nLayout = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873 switch (pControl->GetTextPosition()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700874 case TEXTPOS_ICON:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700875 nLayout = PPBL_ICON;
876 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700877 case TEXTPOS_BELOW:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878 nLayout = PPBL_ICONTOPLABELBOTTOM;
879 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700880 case TEXTPOS_ABOVE:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881 nLayout = PPBL_LABELTOPICONBOTTOM;
882 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700883 case TEXTPOS_RIGHT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884 nLayout = PPBL_ICONLEFTLABELRIGHT;
885 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700886 case TEXTPOS_LEFT:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887 nLayout = PPBL_LABELLEFTICONRIGHT;
888 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700889 case TEXTPOS_OVERLAID:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890 nLayout = PPBL_LABELOVERICON;
891 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700892 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 nLayout = PPBL_LABEL;
894 break;
895 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700896
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897 CPWL_Color crBackground, crBorder;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700898
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899 int iColorType;
900 FX_FLOAT fc[4];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700901
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700902 pControl->GetOriginalBackgroundColor(iColorType, fc);
903 if (iColorType > 0)
904 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700905
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906 pControl->GetOriginalBorderColor(iColorType, fc);
907 if (iColorType > 0)
908 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700909
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
911 int32_t nBorderStyle = 0;
912 CPWL_Dash dsBorder(3, 0, 0);
913 CPWL_Color crLeftTop, crRightBottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700914
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 switch (GetBorderStyle()) {
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700916 case BBS_DASH:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 nBorderStyle = PBS_DASH;
918 dsBorder = CPWL_Dash(3, 3, 0);
919 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700920 case BBS_BEVELED:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921 nBorderStyle = PBS_BEVELED;
922 fBorderWidth *= 2;
923 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
924 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
925 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700926 case BBS_INSET:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927 nBorderStyle = PBS_INSET;
928 fBorderWidth *= 2;
929 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
930 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
931 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700932 case BBS_UNDERLINE:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700933 nBorderStyle = PBS_UNDERLINED;
934 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700935 default:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936 nBorderStyle = PBS_SOLID;
937 break;
938 }
939
Tom Sepez281a9ea2016-02-26 14:24:28 -0800940 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941
942 CPWL_Color crText(COLORTYPE_GRAY, 0);
943
944 FX_FLOAT fFontSize = 12.0f;
945 CFX_ByteString csNameTag;
946
947 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
948 if (da.HasColor()) {
949 da.GetColor(iColorType, fc);
950 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
951 }
952
953 if (da.HasFont())
954 da.GetFont(csNameTag, fFontSize);
955
956 CFX_WideString csWCaption;
957 CFX_WideString csNormalCaption, csRolloverCaption, csDownCaption;
958
959 if (pControl->HasMKEntry("CA")) {
960 csNormalCaption = pControl->GetNormalCaption();
961 }
962 if (pControl->HasMKEntry("RC")) {
963 csRolloverCaption = pControl->GetRolloverCaption();
964 }
965 if (pControl->HasMKEntry("AC")) {
966 csDownCaption = pControl->GetDownCaption();
967 }
968
969 CPDF_Stream* pNormalIcon = NULL;
970 CPDF_Stream* pRolloverIcon = NULL;
971 CPDF_Stream* pDownIcon = NULL;
972
973 if (pControl->HasMKEntry("I")) {
974 pNormalIcon = pControl->GetNormalIcon();
975 }
976 if (pControl->HasMKEntry("RI")) {
977 pRolloverIcon = pControl->GetRolloverIcon();
978 }
979 if (pControl->HasMKEntry("IX")) {
980 pDownIcon = pControl->GetDownIcon();
981 }
982
983 if (pNormalIcon) {
984 if (CPDF_Dictionary* pImageDict = pNormalIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800985 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 pImageDict->SetAtString("Name", "ImgA");
987 }
988 }
989
990 if (pRolloverIcon) {
991 if (CPDF_Dictionary* pImageDict = pRolloverIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800992 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993 pImageDict->SetAtString("Name", "ImgB");
994 }
995 }
996
997 if (pDownIcon) {
998 if (CPDF_Dictionary* pImageDict = pDownIcon->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -0800999 if (pImageDict->GetStringBy("Name").IsEmpty())
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 pImageDict->SetAtString("Name", "ImgC");
1001 }
1002 }
1003
1004 CPDF_IconFit iconFit = pControl->GetIconFit();
1005
1006 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
1008
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001009 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1010 font_map.SetAPType("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011
1012 CFX_ByteString csAP =
1013 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1014 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1015 crLeftTop, crRightBottom, nBorderStyle,
1016 dsBorder) +
1017 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001018 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 pNormalIcon, iconFit, csNormalCaption, crText, fFontSize, nLayout);
1020
1021 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP);
1022 if (pNormalIcon)
1023 AddImageToAppearance("N", pNormalIcon);
1024
1025 CPDF_FormControl::HighlightingMode eHLM = pControl->GetHighlightingMode();
1026 if (eHLM == CPDF_FormControl::Push || eHLM == CPDF_FormControl::Toggle) {
1027 if (csRolloverCaption.IsEmpty() && !pRolloverIcon) {
1028 csRolloverCaption = csNormalCaption;
1029 pRolloverIcon = pNormalIcon;
1030 }
1031
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001032 font_map.SetAPType("R");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033
1034 csAP = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1035 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1036 crLeftTop, crRightBottom,
1037 nBorderStyle, dsBorder) +
1038 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001039 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 pRolloverIcon, iconFit, csRolloverCaption, crText, fFontSize,
1041 nLayout);
1042
1043 WriteAppearance("R", GetRotatedRect(), GetMatrix(), csAP);
1044 if (pRolloverIcon)
1045 AddImageToAppearance("R", pRolloverIcon);
1046
1047 if (csDownCaption.IsEmpty() && !pDownIcon) {
1048 csDownCaption = csNormalCaption;
1049 pDownIcon = pNormalIcon;
1050 }
1051
1052 switch (nBorderStyle) {
1053 case PBS_BEVELED: {
1054 CPWL_Color crTemp = crLeftTop;
1055 crLeftTop = crRightBottom;
1056 crRightBottom = crTemp;
1057 } break;
1058 case PBS_INSET:
1059 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1060 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001061 break;
1062 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001063
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001064 font_map.SetAPType("D");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001065
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 csAP = CPWL_Utils::GetRectFillAppStream(
1067 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1068 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1069 crLeftTop, crRightBottom,
1070 nBorderStyle, dsBorder) +
1071 CPWL_Utils::GetPushButtonAppStream(
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001072 iconFit.GetFittingBounds() ? rcWindow : rcClient, &font_map,
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001073 pDownIcon, iconFit, csDownCaption, crText, fFontSize, nLayout);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001074
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001075 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001076 if (pDownIcon)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077 AddImageToAppearance("D", pDownIcon);
1078 } else {
1079 RemoveAppearance("D");
1080 RemoveAppearance("R");
1081 }
1082}
1083
1084void CPDFSDK_Widget::ResetAppearance_CheckBox() {
1085 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086 CPWL_Color crBackground, crBorder, crText;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087 int iColorType;
1088 FX_FLOAT fc[4];
1089
1090 pControl->GetOriginalBackgroundColor(iColorType, fc);
1091 if (iColorType > 0)
1092 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1093
1094 pControl->GetOriginalBorderColor(iColorType, fc);
1095 if (iColorType > 0)
1096 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1097
1098 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1099 int32_t nBorderStyle = 0;
1100 CPWL_Dash dsBorder(3, 0, 0);
1101 CPWL_Color crLeftTop, crRightBottom;
1102
1103 switch (GetBorderStyle()) {
1104 case BBS_DASH:
1105 nBorderStyle = PBS_DASH;
1106 dsBorder = CPWL_Dash(3, 3, 0);
1107 break;
1108 case BBS_BEVELED:
1109 nBorderStyle = PBS_BEVELED;
1110 fBorderWidth *= 2;
1111 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1112 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1113 break;
1114 case BBS_INSET:
1115 nBorderStyle = PBS_INSET;
1116 fBorderWidth *= 2;
1117 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1118 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1119 break;
1120 case BBS_UNDERLINE:
1121 nBorderStyle = PBS_UNDERLINED;
1122 break;
1123 default:
1124 nBorderStyle = PBS_SOLID;
1125 break;
1126 }
1127
Tom Sepez281a9ea2016-02-26 14:24:28 -08001128 CFX_FloatRect rcWindow = GetRotatedRect();
1129 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001130
1131 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
1132 if (da.HasColor()) {
1133 da.GetColor(iColorType, fc);
1134 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1135 }
1136
1137 int32_t nStyle = 0;
1138
1139 CFX_WideString csWCaption = pControl->GetNormalCaption();
1140 if (csWCaption.GetLength() > 0) {
1141 switch (csWCaption[0]) {
1142 case L'l':
1143 nStyle = PCS_CIRCLE;
1144 break;
1145 case L'8':
1146 nStyle = PCS_CROSS;
1147 break;
1148 case L'u':
1149 nStyle = PCS_DIAMOND;
1150 break;
1151 case L'n':
1152 nStyle = PCS_SQUARE;
1153 break;
1154 case L'H':
1155 nStyle = PCS_STAR;
1156 break;
1157 default: // L'4'
1158 nStyle = PCS_CHECK;
1159 break;
1160 }
1161 } else {
1162 nStyle = PCS_CHECK;
1163 }
1164
1165 CFX_ByteString csAP_N_ON =
1166 CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1167 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1168 crLeftTop, crRightBottom, nBorderStyle,
1169 dsBorder);
1170
1171 CFX_ByteString csAP_N_OFF = csAP_N_ON;
1172
1173 switch (nBorderStyle) {
1174 case PBS_BEVELED: {
1175 CPWL_Color crTemp = crLeftTop;
1176 crLeftTop = crRightBottom;
1177 crRightBottom = crTemp;
1178 } break;
1179 case PBS_INSET:
1180 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1181 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1182 break;
1183 }
1184
1185 CFX_ByteString csAP_D_ON =
1186 CPWL_Utils::GetRectFillAppStream(
1187 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1188 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1189 crLeftTop, crRightBottom, nBorderStyle,
1190 dsBorder);
1191
1192 CFX_ByteString csAP_D_OFF = csAP_D_ON;
1193
1194 csAP_N_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
1195 csAP_D_ON += CPWL_Utils::GetCheckBoxAppStream(rcClient, nStyle, crText);
1196
1197 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
1198 pControl->GetCheckedAPState());
1199 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1200
1201 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
1202 pControl->GetCheckedAPState());
1203 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1204
1205 CFX_ByteString csAS = GetAppState();
1206 if (csAS.IsEmpty())
1207 SetAppState("Off");
1208}
1209
1210void CPDFSDK_Widget::ResetAppearance_RadioButton() {
1211 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001212 CPWL_Color crBackground, crBorder, crText;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001213 int iColorType;
1214 FX_FLOAT fc[4];
1215
1216 pControl->GetOriginalBackgroundColor(iColorType, fc);
1217 if (iColorType > 0)
1218 crBackground = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1219
1220 pControl->GetOriginalBorderColor(iColorType, fc);
1221 if (iColorType > 0)
1222 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1223
1224 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1225 int32_t nBorderStyle = 0;
1226 CPWL_Dash dsBorder(3, 0, 0);
1227 CPWL_Color crLeftTop, crRightBottom;
1228
1229 switch (GetBorderStyle()) {
1230 case BBS_DASH:
1231 nBorderStyle = PBS_DASH;
1232 dsBorder = CPWL_Dash(3, 3, 0);
1233 break;
1234 case BBS_BEVELED:
1235 nBorderStyle = PBS_BEVELED;
1236 fBorderWidth *= 2;
1237 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1238 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1239 break;
1240 case BBS_INSET:
1241 nBorderStyle = PBS_INSET;
1242 fBorderWidth *= 2;
1243 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1244 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1245 break;
1246 case BBS_UNDERLINE:
1247 nBorderStyle = PBS_UNDERLINED;
1248 break;
1249 default:
1250 nBorderStyle = PBS_SOLID;
1251 break;
1252 }
1253
Tom Sepez281a9ea2016-02-26 14:24:28 -08001254 CFX_FloatRect rcWindow = GetRotatedRect();
1255 CFX_FloatRect rcClient = CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001256
1257 CPDF_DefaultAppearance da = pControl->GetDefaultAppearance();
1258 if (da.HasColor()) {
1259 da.GetColor(iColorType, fc);
1260 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1261 }
1262
1263 int32_t nStyle = 0;
1264
1265 CFX_WideString csWCaption = pControl->GetNormalCaption();
1266 if (csWCaption.GetLength() > 0) {
1267 switch (csWCaption[0]) {
1268 default: // L'l':
1269 nStyle = PCS_CIRCLE;
1270 break;
1271 case L'8':
1272 nStyle = PCS_CROSS;
1273 break;
1274 case L'u':
1275 nStyle = PCS_DIAMOND;
1276 break;
1277 case L'n':
1278 nStyle = PCS_SQUARE;
1279 break;
1280 case L'H':
1281 nStyle = PCS_STAR;
1282 break;
1283 case L'4':
1284 nStyle = PCS_CHECK;
1285 break;
1286 }
1287 } else {
1288 nStyle = PCS_CIRCLE;
1289 }
1290
1291 CFX_ByteString csAP_N_ON;
1292
Tom Sepez281a9ea2016-02-26 14:24:28 -08001293 CFX_FloatRect rcCenter =
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001294 CPWL_Utils::DeflateRect(CPWL_Utils::GetCenterSquare(rcWindow), 1.0f);
1295
1296 if (nStyle == PCS_CIRCLE) {
1297 if (nBorderStyle == PBS_BEVELED) {
1298 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1299 crRightBottom = CPWL_Utils::SubstractColor(crBackground, 0.25f);
1300 } else if (nBorderStyle == PBS_INSET) {
1301 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5f);
1302 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75f);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001303 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001304
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001305 csAP_N_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBackground) +
1306 CPWL_Utils::GetCircleBorderAppStream(
1307 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
1308 nBorderStyle, dsBorder);
1309 } else {
1310 csAP_N_ON = CPWL_Utils::GetRectFillAppStream(rcWindow, crBackground) +
1311 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1312 crLeftTop, crRightBottom,
1313 nBorderStyle, dsBorder);
1314 }
1315
1316 CFX_ByteString csAP_N_OFF = csAP_N_ON;
1317
1318 switch (nBorderStyle) {
1319 case PBS_BEVELED: {
1320 CPWL_Color crTemp = crLeftTop;
1321 crLeftTop = crRightBottom;
1322 crRightBottom = crTemp;
1323 } break;
1324 case PBS_INSET:
1325 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1326 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1327 break;
1328 }
1329
1330 CFX_ByteString csAP_D_ON;
1331
1332 if (nStyle == PCS_CIRCLE) {
1333 CPWL_Color crBK = CPWL_Utils::SubstractColor(crBackground, 0.25f);
1334 if (nBorderStyle == PBS_BEVELED) {
1335 crLeftTop = CPWL_Utils::SubstractColor(crBackground, 0.25f);
1336 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1337 crBK = crBackground;
1338 } else if (nBorderStyle == PBS_INSET) {
1339 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0);
1340 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 1);
1341 }
1342
1343 csAP_D_ON = CPWL_Utils::GetCircleFillAppStream(rcCenter, crBK) +
1344 CPWL_Utils::GetCircleBorderAppStream(
1345 rcCenter, fBorderWidth, crBorder, crLeftTop, crRightBottom,
1346 nBorderStyle, dsBorder);
1347 } else {
1348 csAP_D_ON = CPWL_Utils::GetRectFillAppStream(
1349 rcWindow, CPWL_Utils::SubstractColor(crBackground, 0.25f)) +
1350 CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1351 crLeftTop, crRightBottom,
1352 nBorderStyle, dsBorder);
1353 }
1354
1355 CFX_ByteString csAP_D_OFF = csAP_D_ON;
1356
1357 csAP_N_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
1358 csAP_D_ON += CPWL_Utils::GetRadioButtonAppStream(rcClient, nStyle, crText);
1359
1360 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_ON,
1361 pControl->GetCheckedAPState());
1362 WriteAppearance("N", GetRotatedRect(), GetMatrix(), csAP_N_OFF, "Off");
1363
1364 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_ON,
1365 pControl->GetCheckedAPState());
1366 WriteAppearance("D", GetRotatedRect(), GetMatrix(), csAP_D_OFF, "Off");
1367
1368 CFX_ByteString csAS = GetAppState();
1369 if (csAS.IsEmpty())
1370 SetAppState("Off");
1371}
1372
1373void CPDFSDK_Widget::ResetAppearance_ComboBox(const FX_WCHAR* sValue) {
1374 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001375 CPDF_FormField* pField = pControl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001376 CFX_ByteTextBuf sBody, sLines;
1377
Tom Sepez281a9ea2016-02-26 14:24:28 -08001378 CFX_FloatRect rcClient = GetClientRect();
1379 CFX_FloatRect rcButton = rcClient;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001380 rcButton.left = rcButton.right - 13;
1381 rcButton.Normalize();
1382
1383 if (IFX_Edit* pEdit = IFX_Edit::NewEdit()) {
1384 pEdit->EnableRefresh(FALSE);
1385
1386 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001387 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001388 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1389 pEdit->SetFontMap(&font_map);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001390
Tom Sepez281a9ea2016-02-26 14:24:28 -08001391 CFX_FloatRect rcEdit = rcClient;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001392 rcEdit.right = rcButton.left;
1393 rcEdit.Normalize();
1394
1395 pEdit->SetPlateRect(rcEdit);
1396 pEdit->SetAlignmentV(1);
1397
1398 FX_FLOAT fFontSize = GetFontSize();
1399 if (IsFloatZero(fFontSize))
1400 pEdit->SetAutoFontSize(TRUE);
1401 else
1402 pEdit->SetFontSize(fFontSize);
1403
1404 pEdit->Initialize();
1405
Lei Zhangd88a3642015-11-10 09:38:57 -08001406 if (sValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001407 pEdit->SetText(sValue);
Lei Zhangd88a3642015-11-10 09:38:57 -08001408 } else {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001409 int32_t nCurSel = pField->GetSelectedIndex(0);
1410
1411 if (nCurSel < 0)
1412 pEdit->SetText(pField->GetValue().c_str());
1413 else
1414 pEdit->SetText(pField->GetOptionLabel(nCurSel).c_str());
1415 }
1416
Tom Sepez281a9ea2016-02-26 14:24:28 -08001417 CFX_FloatRect rcContent = pEdit->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001418
1419 CFX_ByteString sEdit =
Tom Sepez281a9ea2016-02-26 14:24:28 -08001420 CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, 0.0f));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001421 if (sEdit.GetLength() > 0) {
1422 sBody << "/Tx BMC\n"
1423 << "q\n";
1424 if (rcContent.Width() > rcEdit.Width() ||
1425 rcContent.Height() > rcEdit.Height()) {
1426 sBody << rcEdit.left << " " << rcEdit.bottom << " " << rcEdit.Width()
1427 << " " << rcEdit.Height() << " re\nW\nn\n";
1428 }
1429
1430 CPWL_Color crText = GetTextPWLColor();
1431 sBody << "BT\n" << CPWL_Utils::GetColorAppStream(crText) << sEdit
1432 << "ET\n"
1433 << "Q\nEMC\n";
1434 }
1435
1436 IFX_Edit::DelEdit(pEdit);
1437 }
1438
1439 sBody << CPWL_Utils::GetDropButtonAppStream(rcButton);
1440
1441 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
1442 sLines.GetByteString() + sBody.GetByteString();
1443
1444 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
1445}
1446
1447void CPDFSDK_Widget::ResetAppearance_ListBox() {
1448 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001449 CPDF_FormField* pField = pControl->GetField();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001450 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001451 CFX_ByteTextBuf sBody, sLines;
1452
1453 if (IFX_Edit* pEdit = IFX_Edit::NewEdit()) {
1454 pEdit->EnableRefresh(FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001455
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001456 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001457 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001458
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001459 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1460 pEdit->SetFontMap(&font_map);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001461
Tom Sepez281a9ea2016-02-26 14:24:28 -08001462 pEdit->SetPlateRect(
1463 CFX_FloatRect(rcClient.left, 0.0f, rcClient.right, 0.0f));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001464
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001465 FX_FLOAT fFontSize = GetFontSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001466
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001467 if (IsFloatZero(fFontSize))
1468 pEdit->SetFontSize(12.0f);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001469 else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001470 pEdit->SetFontSize(fFontSize);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001471
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001472 pEdit->Initialize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001473
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001474 CFX_ByteTextBuf sList;
1475 FX_FLOAT fy = rcClient.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001476
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001477 int32_t nTop = pField->GetTopVisibleIndex();
1478 int32_t nCount = pField->CountOptions();
1479 int32_t nSelCount = pField->CountSelectedItems();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001480
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001481 for (int32_t i = nTop; i < nCount; i++) {
1482 FX_BOOL bSelected = FALSE;
1483 for (int32_t j = 0; j < nSelCount; j++) {
1484 if (pField->GetSelectedIndex(j) == i) {
1485 bSelected = TRUE;
1486 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001487 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001488 }
1489
1490 pEdit->SetText(pField->GetOptionLabel(i).c_str());
1491
Tom Sepez281a9ea2016-02-26 14:24:28 -08001492 CFX_FloatRect rcContent = pEdit->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001493 FX_FLOAT fItemHeight = rcContent.Height();
1494
1495 if (bSelected) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001496 CFX_FloatRect rcItem =
1497 CFX_FloatRect(rcClient.left, fy - fItemHeight, rcClient.right, fy);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001498 sList << "q\n" << CPWL_Utils::GetColorAppStream(
1499 CPWL_Color(COLORTYPE_RGB, 0, 51.0f / 255.0f,
1500 113.0f / 255.0f),
1501 TRUE)
1502 << rcItem.left << " " << rcItem.bottom << " " << rcItem.Width()
1503 << " " << rcItem.Height() << " re f\n"
1504 << "Q\n";
1505
1506 sList << "BT\n" << CPWL_Utils::GetColorAppStream(
1507 CPWL_Color(COLORTYPE_GRAY, 1), TRUE)
Tom Sepez281a9ea2016-02-26 14:24:28 -08001508 << CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, fy))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001509 << "ET\n";
1510 } else {
1511 CPWL_Color crText = GetTextPWLColor();
1512 sList << "BT\n" << CPWL_Utils::GetColorAppStream(crText, TRUE)
Tom Sepez281a9ea2016-02-26 14:24:28 -08001513 << CPWL_Utils::GetEditAppStream(pEdit, CFX_FloatPoint(0.0f, fy))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001514 << "ET\n";
1515 }
1516
1517 fy -= fItemHeight;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001518 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001519
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001520 if (sList.GetSize() > 0) {
1521 sBody << "/Tx BMC\n"
1522 << "q\n" << rcClient.left << " " << rcClient.bottom << " "
1523 << rcClient.Width() << " " << rcClient.Height() << " re\nW\nn\n";
1524 sBody << sList << "Q\nEMC\n";
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001525 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001526
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001527 IFX_Edit::DelEdit(pEdit);
1528 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001529
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001530 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
1531 sLines.GetByteString() + sBody.GetByteString();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001532
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001533 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001534}
1535
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001536void CPDFSDK_Widget::ResetAppearance_TextField(const FX_WCHAR* sValue) {
1537 CPDF_FormControl* pControl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001538 CPDF_FormField* pField = pControl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001539 CFX_ByteTextBuf sBody, sLines;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001540
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001541 if (IFX_Edit* pEdit = IFX_Edit::NewEdit()) {
1542 pEdit->EnableRefresh(FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001543
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001544 CPDFSDK_Document* pDoc = m_pInterForm->GetDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001545 CPDFDoc_Environment* pEnv = pDoc->GetEnv();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001546
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001547 CBA_FontMap font_map(this, pEnv->GetSysHandler());
1548 pEdit->SetFontMap(&font_map);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001549
Tom Sepez281a9ea2016-02-26 14:24:28 -08001550 CFX_FloatRect rcClient = GetClientRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001551 pEdit->SetPlateRect(rcClient);
1552 pEdit->SetAlignmentH(pControl->GetControlAlignment());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001553
tsepezc3255f52016-03-25 14:52:27 -07001554 uint32_t dwFieldFlags = pField->GetFieldFlags();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001555 FX_BOOL bMultiLine = (dwFieldFlags >> 12) & 1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001556
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001557 if (bMultiLine) {
1558 pEdit->SetMultiLine(TRUE);
1559 pEdit->SetAutoReturn(TRUE);
1560 } else {
1561 pEdit->SetAlignmentV(1);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001562 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001563
Tom Sepez62a70f92016-03-21 15:00:20 -07001564 uint16_t subWord = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001565 if ((dwFieldFlags >> 13) & 1) {
1566 subWord = '*';
1567 pEdit->SetPasswordChar(subWord);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001568 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001569
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001570 int nMaxLen = pField->GetMaxLen();
1571 FX_BOOL bCharArray = (dwFieldFlags >> 24) & 1;
1572 FX_FLOAT fFontSize = GetFontSize();
Bo Xufdc00a72014-10-28 23:03:33 -07001573
Tom Sepez51da0932015-11-25 16:05:49 -08001574#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001575 CFX_WideString sValueTmp;
Lei Zhang99766722016-02-23 11:21:48 -08001576 if (!sValue && GetMixXFAWidget()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001577 sValueTmp = GetValue(TRUE);
1578 sValue = sValueTmp;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001579 }
Tom Sepez40e9ff32015-11-30 12:39:54 -08001580#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001581
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001582 if (nMaxLen > 0) {
1583 if (bCharArray) {
1584 pEdit->SetCharArray(nMaxLen);
Tom Sepez7b5bc262015-03-05 16:44:22 -08001585
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001586 if (IsFloatZero(fFontSize)) {
Lei Zhangfcfa3b82015-12-24 21:07:28 -08001587 fFontSize = CPWL_Edit::GetCharArrayAutoFontSize(
1588 font_map.GetPDFFont(0), rcClient, nMaxLen);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001589 }
1590 } else {
1591 if (sValue)
1592 nMaxLen = wcslen((const wchar_t*)sValue);
1593 pEdit->SetLimitChar(nMaxLen);
Lei Zhang606346f2015-06-19 18:11:07 -07001594 }
1595 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001596
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001597 if (IsFloatZero(fFontSize))
1598 pEdit->SetAutoFontSize(TRUE);
1599 else
1600 pEdit->SetFontSize(fFontSize);
1601
1602 pEdit->Initialize();
1603
1604 if (sValue)
1605 pEdit->SetText(sValue);
1606 else
1607 pEdit->SetText(pField->GetValue().c_str());
1608
Tom Sepez281a9ea2016-02-26 14:24:28 -08001609 CFX_FloatRect rcContent = pEdit->GetContentRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001610
1611 CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(
Tom Sepez281a9ea2016-02-26 14:24:28 -08001612 pEdit, CFX_FloatPoint(0.0f, 0.0f), NULL, !bCharArray, subWord);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001613
1614 if (sEdit.GetLength() > 0) {
1615 sBody << "/Tx BMC\n"
1616 << "q\n";
1617 if (rcContent.Width() > rcClient.Width() ||
1618 rcContent.Height() > rcClient.Height()) {
1619 sBody << rcClient.left << " " << rcClient.bottom << " "
1620 << rcClient.Width() << " " << rcClient.Height() << " re\nW\nn\n";
1621 }
1622 CPWL_Color crText = GetTextPWLColor();
1623 sBody << "BT\n" << CPWL_Utils::GetColorAppStream(crText) << sEdit
1624 << "ET\n"
1625 << "Q\nEMC\n";
Lei Zhang606346f2015-06-19 18:11:07 -07001626 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001627
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001628 if (bCharArray) {
1629 switch (GetBorderStyle()) {
1630 case BBS_SOLID: {
1631 CFX_ByteString sColor =
1632 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1633 if (sColor.GetLength() > 0) {
1634 sLines << "q\n" << GetBorderWidth() << " w\n"
1635 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1636 << " 2 J 0 j\n";
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001637
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001638 for (int32_t i = 1; i < nMaxLen; i++) {
1639 sLines << rcClient.left +
1640 ((rcClient.right - rcClient.left) / nMaxLen) * i
1641 << " " << rcClient.bottom << " m\n"
1642 << rcClient.left +
1643 ((rcClient.right - rcClient.left) / nMaxLen) * i
1644 << " " << rcClient.top << " l S\n";
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001645 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001646
1647 sLines << "Q\n";
1648 }
1649 } break;
1650 case BBS_DASH: {
1651 CFX_ByteString sColor =
1652 CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE);
1653 if (sColor.GetLength() > 0) {
1654 CPWL_Dash dsBorder = CPWL_Dash(3, 3, 0);
1655
1656 sLines << "q\n" << GetBorderWidth() << " w\n"
1657 << CPWL_Utils::GetColorAppStream(GetBorderPWLColor(), FALSE)
1658 << "[" << dsBorder.nDash << " " << dsBorder.nGap << "] "
1659 << dsBorder.nPhase << " d\n";
1660
1661 for (int32_t i = 1; i < nMaxLen; i++) {
1662 sLines << rcClient.left +
1663 ((rcClient.right - rcClient.left) / nMaxLen) * i
1664 << " " << rcClient.bottom << " m\n"
1665 << rcClient.left +
1666 ((rcClient.right - rcClient.left) / nMaxLen) * i
1667 << " " << rcClient.top << " l S\n";
1668 }
1669
1670 sLines << "Q\n";
1671 }
1672 } break;
1673 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07001674 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001675
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001676 IFX_Edit::DelEdit(pEdit);
1677 }
1678
1679 CFX_ByteString sAP = GetBackgroundAppStream() + GetBorderAppStream() +
1680 sLines.GetByteString() + sBody.GetByteString();
1681 WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001682}
1683
Tom Sepez281a9ea2016-02-26 14:24:28 -08001684CFX_FloatRect CPDFSDK_Widget::GetClientRect() const {
1685 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001686 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1687 switch (GetBorderStyle()) {
1688 case BBS_BEVELED:
1689 case BBS_INSET:
1690 fBorderWidth *= 2.0f;
1691 break;
1692 }
1693
1694 return CPWL_Utils::DeflateRect(rcWindow, fBorderWidth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001695}
1696
Tom Sepez281a9ea2016-02-26 14:24:28 -08001697CFX_FloatRect CPDFSDK_Widget::GetRotatedRect() const {
1698 CFX_FloatRect rectAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001699 FX_FLOAT fWidth = rectAnnot.right - rectAnnot.left;
1700 FX_FLOAT fHeight = rectAnnot.top - rectAnnot.bottom;
1701
1702 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001703 CFX_FloatRect rcPDFWindow;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001704 switch (abs(pControl->GetRotation() % 360)) {
1705 case 0:
1706 case 180:
1707 default:
Tom Sepez281a9ea2016-02-26 14:24:28 -08001708 rcPDFWindow = CFX_FloatRect(0, 0, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001709 break;
1710 case 90:
1711 case 270:
Tom Sepez281a9ea2016-02-26 14:24:28 -08001712 rcPDFWindow = CFX_FloatRect(0, 0, fHeight, fWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001713 break;
1714 }
1715
1716 return rcPDFWindow;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001717}
1718
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001719CFX_ByteString CPDFSDK_Widget::GetBackgroundAppStream() const {
1720 CPWL_Color crBackground = GetFillPWLColor();
1721 if (crBackground.nColorType != COLORTYPE_TRANSPARENT) {
1722 return CPWL_Utils::GetRectFillAppStream(GetRotatedRect(), crBackground);
1723 }
1724 return "";
Bo Xufdc00a72014-10-28 23:03:33 -07001725}
1726
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001727CFX_ByteString CPDFSDK_Widget::GetBorderAppStream() const {
Tom Sepez281a9ea2016-02-26 14:24:28 -08001728 CFX_FloatRect rcWindow = GetRotatedRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001729 CPWL_Color crBorder = GetBorderPWLColor();
1730 CPWL_Color crBackground = GetFillPWLColor();
1731 CPWL_Color crLeftTop, crRightBottom;
1732
1733 FX_FLOAT fBorderWidth = (FX_FLOAT)GetBorderWidth();
1734 int32_t nBorderStyle = 0;
1735 CPWL_Dash dsBorder(3, 0, 0);
1736
1737 switch (GetBorderStyle()) {
1738 case BBS_DASH:
1739 nBorderStyle = PBS_DASH;
1740 dsBorder = CPWL_Dash(3, 3, 0);
1741 break;
1742 case BBS_BEVELED:
1743 nBorderStyle = PBS_BEVELED;
1744 fBorderWidth *= 2;
1745 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 1);
1746 crRightBottom = CPWL_Utils::DevideColor(crBackground, 2);
1747 break;
1748 case BBS_INSET:
1749 nBorderStyle = PBS_INSET;
1750 fBorderWidth *= 2;
1751 crLeftTop = CPWL_Color(COLORTYPE_GRAY, 0.5);
1752 crRightBottom = CPWL_Color(COLORTYPE_GRAY, 0.75);
1753 break;
1754 case BBS_UNDERLINE:
1755 nBorderStyle = PBS_UNDERLINED;
1756 break;
1757 default:
1758 nBorderStyle = PBS_SOLID;
1759 break;
1760 }
1761
1762 return CPWL_Utils::GetBorderAppStream(rcWindow, fBorderWidth, crBorder,
1763 crLeftTop, crRightBottom, nBorderStyle,
1764 dsBorder);
Bo Xufdc00a72014-10-28 23:03:33 -07001765}
1766
Tom Sepez60d909e2015-12-10 15:34:55 -08001767CFX_Matrix CPDFSDK_Widget::GetMatrix() const {
1768 CFX_Matrix mt;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001769 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -08001770 CFX_FloatRect rcAnnot = GetRect();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001771 FX_FLOAT fWidth = rcAnnot.right - rcAnnot.left;
1772 FX_FLOAT fHeight = rcAnnot.top - rcAnnot.bottom;
1773
1774 switch (abs(pControl->GetRotation() % 360)) {
1775 case 0:
1776 default:
Tom Sepez60d909e2015-12-10 15:34:55 -08001777 mt = CFX_Matrix(1, 0, 0, 1, 0, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001778 break;
1779 case 90:
Tom Sepez60d909e2015-12-10 15:34:55 -08001780 mt = CFX_Matrix(0, 1, -1, 0, fWidth, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001781 break;
1782 case 180:
Tom Sepez60d909e2015-12-10 15:34:55 -08001783 mt = CFX_Matrix(-1, 0, 0, -1, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001784 break;
1785 case 270:
Tom Sepez60d909e2015-12-10 15:34:55 -08001786 mt = CFX_Matrix(0, -1, 1, 0, 0, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001787 break;
1788 }
1789
1790 return mt;
Bo Xufdc00a72014-10-28 23:03:33 -07001791}
1792
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001793CPWL_Color CPDFSDK_Widget::GetTextPWLColor() const {
1794 CPWL_Color crText = CPWL_Color(COLORTYPE_GRAY, 0);
1795
1796 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001797 CPDF_DefaultAppearance da = pFormCtrl->GetDefaultAppearance();
1798 if (da.HasColor()) {
1799 int32_t iColorType;
1800 FX_FLOAT fc[4];
1801 da.GetColor(iColorType, fc);
1802 crText = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1803 }
1804
1805 return crText;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001806}
1807
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001808CPWL_Color CPDFSDK_Widget::GetBorderPWLColor() const {
1809 CPWL_Color crBorder;
1810
1811 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001812 int32_t iColorType;
1813 FX_FLOAT fc[4];
1814 pFormCtrl->GetOriginalBorderColor(iColorType, fc);
1815 if (iColorType > 0)
1816 crBorder = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1817
1818 return crBorder;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001819}
1820
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001821CPWL_Color CPDFSDK_Widget::GetFillPWLColor() const {
1822 CPWL_Color crFill;
1823
1824 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001825 int32_t iColorType;
1826 FX_FLOAT fc[4];
1827 pFormCtrl->GetOriginalBackgroundColor(iColorType, fc);
1828 if (iColorType > 0)
1829 crFill = CPWL_Color(iColorType, fc[0], fc[1], fc[2], fc[3]);
1830
1831 return crFill;
Bo Xufdc00a72014-10-28 23:03:33 -07001832}
1833
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001834void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType,
1835 CPDF_Stream* pImage) {
Tom Sepez33420902015-10-13 15:00:10 -07001836 CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
Lei Zhang96660d62015-12-14 18:27:25 -08001837 ASSERT(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001838
Wei Li9b761132016-01-29 15:44:20 -08001839 CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP");
1840 CPDF_Stream* pStream = pAPDict->GetStreamBy(sAPType);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001841 CPDF_Dictionary* pStreamDict = pStream->GetDict();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001842 CFX_ByteString sImageAlias = "IMG";
1843
1844 if (CPDF_Dictionary* pImageDict = pImage->GetDict()) {
Wei Li9b761132016-01-29 15:44:20 -08001845 sImageAlias = pImageDict->GetStringBy("Name");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001846 if (sImageAlias.IsEmpty())
1847 sImageAlias = "IMG";
1848 }
1849
Wei Li9b761132016-01-29 15:44:20 -08001850 CPDF_Dictionary* pStreamResList = pStreamDict->GetDictBy("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001851 if (!pStreamResList) {
Tom Sepezae51c812015-08-05 12:34:06 -07001852 pStreamResList = new CPDF_Dictionary();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001853 pStreamDict->SetAt("Resources", pStreamResList);
1854 }
1855
1856 if (pStreamResList) {
Tom Sepezae51c812015-08-05 12:34:06 -07001857 CPDF_Dictionary* pXObject = new CPDF_Dictionary;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001858 pXObject->SetAtReference(sImageAlias, pDoc, pImage);
1859 pStreamResList->SetAt("XObject", pXObject);
1860 }
Bo Xufdc00a72014-10-28 23:03:33 -07001861}
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001862
1863void CPDFSDK_Widget::RemoveAppearance(const CFX_ByteString& sAPType) {
Wei Li9b761132016-01-29 15:44:20 -08001864 if (CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictBy("AP")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001865 pAPDict->RemoveAt(sAPType);
1866 }
1867}
1868
1869FX_BOOL CPDFSDK_Widget::OnAAction(CPDF_AAction::AActionType type,
1870 PDFSDK_FieldAction& data,
1871 CPDFSDK_PageView* pPageView) {
1872 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001873 CPDFDoc_Environment* pEnv = pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001874
Tom Sepez40e9ff32015-11-30 12:39:54 -08001875#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -08001876 CPDFXFA_Document* pDoc = pDocument->GetXFADocument();
1877 if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001878 XFA_EVENTTYPE eEventType = GetXFAEventType(type, data.bWillCommit);
1879
1880 if (eEventType != XFA_EVENT_Unknown) {
Tom Sepez540c4362015-11-24 13:33:57 -08001881 if (IXFA_WidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001882 CXFA_EventParam param;
1883 param.m_eType = eEventType;
1884 param.m_wsChange = data.sChange;
1885 param.m_iCommitKey = data.nCommitKey;
1886 param.m_bShift = data.bShift;
1887 param.m_iSelStart = data.nSelStart;
1888 param.m_iSelEnd = data.nSelEnd;
1889 param.m_wsFullText = data.sValue;
1890 param.m_bKeyDown = data.bKeyDown;
1891 param.m_bModifier = data.bModifier;
1892 param.m_wsNewText = data.sValue;
1893 if (data.nSelEnd > data.nSelStart)
1894 param.m_wsNewText.Delete(data.nSelStart,
1895 data.nSelEnd - data.nSelStart);
1896 for (int i = data.sChange.GetLength() - 1; i >= 0; i--)
1897 param.m_wsNewText.Insert(data.nSelStart, data.sChange[i]);
1898 param.m_wsPrevText = data.sValue;
1899
1900 CXFA_WidgetAcc* pAcc = pXFAWidgetHandler->GetDataAcc(hWidget);
1901 param.m_pTarget = pAcc;
1902 int32_t nRet = pXFAWidgetHandler->ProcessEvent(pAcc, &param);
1903
1904 if (IXFA_DocView* pDocView = pDoc->GetXFADocView()) {
1905 pDocView->UpdateDocView();
1906 }
1907
Wei Lie98ac2e2016-03-18 15:43:04 -07001908 if (nRet == XFA_EVENTERROR_Success)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001909 return TRUE;
1910 }
1911 }
1912 }
Tom Sepez40e9ff32015-11-30 12:39:54 -08001913#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001914
1915 CPDF_Action action = GetAAction(type);
Wei Li0fc6b252016-03-01 16:29:41 -08001916 if (action.GetDict() && action.GetType() != CPDF_Action::Unknown) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001917 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
1918 return pActionHandler->DoAction_Field(action, type, pDocument,
1919 GetFormField(), data);
1920 }
1921 return FALSE;
1922}
1923
1924CPDF_Action CPDFSDK_Widget::GetAAction(CPDF_AAction::AActionType eAAT) {
1925 switch (eAAT) {
1926 case CPDF_AAction::CursorEnter:
1927 case CPDF_AAction::CursorExit:
1928 case CPDF_AAction::ButtonDown:
1929 case CPDF_AAction::ButtonUp:
1930 case CPDF_AAction::GetFocus:
1931 case CPDF_AAction::LoseFocus:
1932 case CPDF_AAction::PageOpen:
1933 case CPDF_AAction::PageClose:
1934 case CPDF_AAction::PageVisible:
1935 case CPDF_AAction::PageInvisible:
1936 return CPDFSDK_BAAnnot::GetAAction(eAAT);
1937
1938 case CPDF_AAction::KeyStroke:
1939 case CPDF_AAction::Format:
1940 case CPDF_AAction::Validate:
1941 case CPDF_AAction::Calculate: {
1942 CPDF_FormField* pField = GetFormField();
Wei Li0fc6b252016-03-01 16:29:41 -08001943 if (pField->GetAdditionalAction().GetDict())
1944 return pField->GetAdditionalAction().GetAction(eAAT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001945 return CPDFSDK_BAAnnot::GetAAction(eAAT);
1946 }
1947 default:
1948 break;
1949 }
1950
1951 return CPDF_Action();
1952}
1953
1954CFX_WideString CPDFSDK_Widget::GetAlternateName() const {
1955 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001956 return pFormField->GetAlternateName();
1957}
1958
1959int32_t CPDFSDK_Widget::GetAppearanceAge() const {
1960 return m_nAppAge;
1961}
1962
1963int32_t CPDFSDK_Widget::GetValueAge() const {
1964 return m_nValueAge;
1965}
1966
1967FX_BOOL CPDFSDK_Widget::HitTest(FX_FLOAT pageX, FX_FLOAT pageY) {
1968 CPDF_Annot* pAnnot = GetPDFAnnot();
1969 CFX_FloatRect annotRect;
1970 pAnnot->GetRect(annotRect);
1971 if (annotRect.Contains(pageX, pageY)) {
1972 if (!IsVisible())
1973 return FALSE;
1974
1975 int nFieldFlags = GetFieldFlags();
1976 if ((nFieldFlags & FIELDFLAG_READONLY) == FIELDFLAG_READONLY)
1977 return FALSE;
1978
1979 return TRUE;
1980 }
1981 return FALSE;
1982}
1983
Tom Sepez51da0932015-11-25 16:05:49 -08001984#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001985CPDFSDK_XFAWidget::CPDFSDK_XFAWidget(IXFA_Widget* pAnnot,
1986 CPDFSDK_PageView* pPageView,
1987 CPDFSDK_InterForm* pInterForm)
Dan Sinclairf766ad22016-03-14 13:51:24 -04001988 : CPDFSDK_Annot(pPageView),
1989 m_pInterForm(pInterForm),
1990 m_hXFAWidget(pAnnot) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001991
1992FX_BOOL CPDFSDK_XFAWidget::IsXFAField() {
1993 return TRUE;
1994}
1995
1996CFX_ByteString CPDFSDK_XFAWidget::GetType() const {
1997 return FSDK_XFAWIDGET_TYPENAME;
1998}
1999
Tom Sepez3343d142015-11-02 09:54:54 -08002000CFX_FloatRect CPDFSDK_XFAWidget::GetRect() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002001 CPDFSDK_PageView* pPageView = GetPageView();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002002 CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
Tom Sepez50d12ad2015-11-24 09:50:51 -08002003 CPDFXFA_Document* pDoc = pDocument->GetXFADocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002004 IXFA_DocView* pDocView = pDoc->GetXFADocView();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002005 IXFA_WidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002006
2007 CFX_RectF rcBBox;
2008 pWidgetHandler->GetRect(GetXFAWidget(), rcBBox);
2009
2010 return CFX_FloatRect(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
2011 rcBBox.top + rcBBox.height);
2012}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002013#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002014
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002015CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_Document* pDocument)
2016 : m_pDocument(pDocument),
2017 m_pInterForm(NULL),
Tom Sepez51da0932015-11-25 16:05:49 -08002018#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002019 m_bXfaCalculate(TRUE),
2020 m_bXfaValidationsEnabled(TRUE),
Tom Sepez40e9ff32015-11-30 12:39:54 -08002021#endif // PDF_ENABLE_XFA
2022 m_bCalculate(TRUE),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002023 m_bBusy(FALSE) {
Tom Sepez50d12ad2015-11-24 09:50:51 -08002024 m_pInterForm = new CPDF_InterForm(m_pDocument->GetPDFDocument(), FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002025 m_pInterForm->SetFormNotify(this);
2026
Tom Sepezae7a9172015-11-23 09:22:46 -08002027 for (int i = 0; i < kNumFieldTypes; ++i)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002028 m_bNeedHightlight[i] = FALSE;
2029 m_iHighlightAlpha = 0;
2030}
2031
2032CPDFSDK_InterForm::~CPDFSDK_InterForm() {
2033 delete m_pInterForm;
2034 m_pInterForm = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002035 m_Map.clear();
Tom Sepez51da0932015-11-25 16:05:49 -08002036#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002037 m_XFAMap.RemoveAll();
Tom Sepez40e9ff32015-11-30 12:39:54 -08002038#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002039}
2040
2041FX_BOOL CPDFSDK_InterForm::HighlightWidgets() {
2042 return FALSE;
2043}
2044
2045CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget,
2046 FX_BOOL bNext) const {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08002047 std::unique_ptr<CBA_AnnotIterator> pIterator(
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002048 new CBA_AnnotIterator(pWidget->GetPageView(), "Widget", ""));
2049
2050 if (bNext) {
2051 return (CPDFSDK_Widget*)pIterator->GetNextAnnot(pWidget);
2052 }
2053 return (CPDFSDK_Widget*)pIterator->GetPrevAnnot(pWidget);
2054}
2055
2056CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const {
2057 if (!pControl || !m_pInterForm)
2058 return nullptr;
2059
2060 CPDFSDK_Widget* pWidget = nullptr;
2061 const auto it = m_Map.find(pControl);
2062 if (it != m_Map.end())
2063 pWidget = it->second;
2064
2065 if (pWidget)
2066 return pWidget;
2067
2068 CPDF_Dictionary* pControlDict = pControl->GetWidget();
Tom Sepez50d12ad2015-11-24 09:50:51 -08002069 CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002070 CPDFSDK_PageView* pPage = nullptr;
2071
Wei Li9b761132016-01-29 15:44:20 -08002072 if (CPDF_Dictionary* pPageDict = pControlDict->GetDictBy("P")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002073 int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
2074 if (nPageIndex >= 0) {
2075 pPage = m_pDocument->GetPageView(nPageIndex);
2076 }
2077 }
2078
2079 if (!pPage) {
2080 int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict);
2081 if (nPageIndex >= 0) {
2082 pPage = m_pDocument->GetPageView(nPageIndex);
2083 }
2084 }
2085
2086 if (!pPage)
2087 return nullptr;
2088 return (CPDFSDK_Widget*)pPage->GetAnnotByDict(pControlDict);
2089}
2090
Lei Zhangd88a3642015-11-10 09:38:57 -08002091void CPDFSDK_InterForm::GetWidgets(
2092 const CFX_WideString& sFieldName,
2093 std::vector<CPDFSDK_Widget*>* widgets) const {
2094 for (int i = 0, sz = m_pInterForm->CountFields(sFieldName); i < sz; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002095 CPDF_FormField* pFormField = m_pInterForm->GetField(i, sFieldName);
Lei Zhangd88a3642015-11-10 09:38:57 -08002096 ASSERT(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002097 GetWidgets(pFormField, widgets);
2098 }
2099}
2100
Lei Zhangd88a3642015-11-10 09:38:57 -08002101void CPDFSDK_InterForm::GetWidgets(
2102 CPDF_FormField* pField,
2103 std::vector<CPDFSDK_Widget*>* widgets) const {
2104 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002105 CPDF_FormControl* pFormCtrl = pField->GetControl(i);
Lei Zhangd88a3642015-11-10 09:38:57 -08002106 ASSERT(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002107 CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002108 if (pWidget)
Lei Zhangd88a3642015-11-10 09:38:57 -08002109 widgets->push_back(pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002110 }
2111}
2112
2113int CPDFSDK_InterForm::GetPageIndexByAnnotDict(
2114 CPDF_Document* pDocument,
2115 CPDF_Dictionary* pAnnotDict) const {
Lei Zhang96660d62015-12-14 18:27:25 -08002116 ASSERT(pAnnotDict);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002117
2118 for (int i = 0, sz = pDocument->GetPageCount(); i < sz; i++) {
2119 if (CPDF_Dictionary* pPageDict = pDocument->GetPage(i)) {
Wei Li9b761132016-01-29 15:44:20 -08002120 if (CPDF_Array* pAnnots = pPageDict->GetArrayBy("Annots")) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002121 for (int j = 0, jsz = pAnnots->GetCount(); j < jsz; j++) {
2122 CPDF_Object* pDict = pAnnots->GetElementValue(j);
2123 if (pAnnotDict == pDict) {
2124 return i;
2125 }
2126 }
2127 }
2128 }
2129 }
2130
2131 return -1;
2132}
2133
2134void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl,
2135 CPDFSDK_Widget* pWidget) {
2136 m_Map[pControl] = pWidget;
2137}
2138
2139void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl) {
2140 m_Map.erase(pControl);
2141}
2142
Tom Sepez40e9ff32015-11-30 12:39:54 -08002143void CPDFSDK_InterForm::EnableCalculate(FX_BOOL bEnabled) {
2144 m_bCalculate = bEnabled;
2145}
2146
2147FX_BOOL CPDFSDK_InterForm::IsCalculateEnabled() const {
2148 return m_bCalculate;
2149}
2150
Tom Sepez51da0932015-11-25 16:05:49 -08002151#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002152void CPDFSDK_InterForm::AddXFAMap(IXFA_Widget* hWidget,
2153 CPDFSDK_XFAWidget* pWidget) {
2154 m_XFAMap.SetAt(hWidget, pWidget);
2155}
2156
2157void CPDFSDK_InterForm::RemoveXFAMap(IXFA_Widget* hWidget) {
2158 m_XFAMap.RemoveKey(hWidget);
2159}
2160
2161CPDFSDK_XFAWidget* CPDFSDK_InterForm::GetXFAWidget(IXFA_Widget* hWidget) {
2162 CPDFSDK_XFAWidget* pWidget = NULL;
2163 m_XFAMap.Lookup(hWidget, pWidget);
2164
2165 return pWidget;
2166}
2167
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002168void CPDFSDK_InterForm::XfaEnableCalculate(FX_BOOL bEnabled) {
2169 m_bXfaCalculate = bEnabled;
2170}
2171FX_BOOL CPDFSDK_InterForm::IsXfaCalculateEnabled() const {
2172 return m_bXfaCalculate;
2173}
2174
2175FX_BOOL CPDFSDK_InterForm::IsXfaValidationsEnabled() {
2176 return m_bXfaValidationsEnabled;
2177}
2178void CPDFSDK_InterForm::XfaSetValidationsEnabled(FX_BOOL bEnabled) {
2179 m_bXfaValidationsEnabled = bEnabled;
Bo Xufdc00a72014-10-28 23:03:33 -07002180}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002181#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -07002182
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002183void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002184 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2185 ASSERT(pEnv);
2186 if (!pEnv->IsJSInitiated())
2187 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002188
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002189 if (m_bBusy)
2190 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002191
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002192 m_bBusy = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002193
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002194 if (IsCalculateEnabled()) {
Tom Sepezba038bc2015-10-08 12:03:00 -07002195 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002196 pRuntime->SetReaderDocument(m_pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002197
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002198 int nSize = m_pInterForm->CountFieldsInCalculationOrder();
2199 for (int i = 0; i < nSize; i++) {
2200 if (CPDF_FormField* pField =
2201 m_pInterForm->GetFieldInCalculationOrder(i)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002202 int nType = pField->GetFieldType();
2203 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2204 CPDF_AAction aAction = pField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002205 if (aAction.GetDict() &&
2206 aAction.ActionExist(CPDF_AAction::Calculate)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002207 CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate);
Wei Li0fc6b252016-03-01 16:29:41 -08002208 if (action.GetDict()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002209 CFX_WideString csJS = action.GetJavaScript();
2210 if (!csJS.IsEmpty()) {
Tom Sepezba038bc2015-10-08 12:03:00 -07002211 IJS_Context* pContext = pRuntime->NewContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002212 CFX_WideString sOldValue = pField->GetValue();
2213 CFX_WideString sValue = sOldValue;
2214 FX_BOOL bRC = TRUE;
2215 pContext->OnField_Calculate(pFormField, pField, sValue, bRC);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002216
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002217 CFX_WideString sInfo;
Tom Sepez33420902015-10-13 15:00:10 -07002218 FX_BOOL bRet = pContext->RunScript(csJS, &sInfo);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002219 pRuntime->ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002220
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002221 if (bRet) {
2222 if (bRC) {
2223 if (sValue.Compare(sOldValue) != 0)
2224 pField->SetValue(sValue, TRUE);
2225 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002226 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002227 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002228 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002229 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002230 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002231 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002232 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002233 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002234
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002235 m_bBusy = FALSE;
2236}
2237
2238CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField,
2239 FX_BOOL& bFormated) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002240 CFX_WideString sValue = pFormField->GetValue();
2241 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2242 ASSERT(pEnv);
2243 if (!pEnv->IsJSInitiated()) {
2244 bFormated = FALSE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002245 return sValue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002246 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002247
Tom Sepezba038bc2015-10-08 12:03:00 -07002248 IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002249 pRuntime->SetReaderDocument(m_pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002250
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002251 if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX) {
2252 if (pFormField->CountSelectedItems() > 0) {
2253 int index = pFormField->GetSelectedIndex(0);
2254 if (index >= 0)
2255 sValue = pFormField->GetOptionLabel(index);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002256 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002257 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002258
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002259 bFormated = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002260
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002261 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002262 if (aAction.GetDict() && aAction.ActionExist(CPDF_AAction::Format)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002263 CPDF_Action action = aAction.GetAction(CPDF_AAction::Format);
Wei Li0fc6b252016-03-01 16:29:41 -08002264 if (action.GetDict()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002265 CFX_WideString script = action.GetJavaScript();
2266 if (!script.IsEmpty()) {
2267 CFX_WideString Value = sValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002268
Tom Sepezba038bc2015-10-08 12:03:00 -07002269 IJS_Context* pContext = pRuntime->NewContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002270 pContext->OnField_Format(pFormField, Value, TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002271
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002272 CFX_WideString sInfo;
Tom Sepez33420902015-10-13 15:00:10 -07002273 FX_BOOL bRet = pContext->RunScript(script, &sInfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002274 pRuntime->ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002275
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002276 if (bRet) {
2277 sValue = Value;
2278 bFormated = TRUE;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002279 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002280 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002281 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002282 }
2283
2284 return sValue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002285}
2286
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002287void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField,
2288 const FX_WCHAR* sValue,
2289 FX_BOOL bValueChanged) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002290 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2291 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang96660d62015-12-14 18:27:25 -08002292 ASSERT(pFormCtrl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002293 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
2294 pWidget->ResetAppearance(sValue, bValueChanged);
2295 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002296}
2297
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002298void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002299 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2300 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang96660d62015-12-14 18:27:25 -08002301 ASSERT(pFormCtrl);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002302
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002303 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
2304 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2305 CFFL_IFormFiller* pIFormFiller = pEnv->GetIFormFiller();
Tom Sepez540c4362015-11-24 13:33:57 -08002306 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002307 CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002308 FX_RECT rcBBox = pIFormFiller->GetViewBBox(pPageView, pWidget);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07002309
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002310 pEnv->FFI_Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right,
2311 rcBBox.bottom);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002312 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002313 }
2314}
2315
Tom Sepezd6262c12016-02-03 14:47:06 -08002316FX_BOOL CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField,
2317 const CFX_WideString& csValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002318 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002319 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::KeyStroke))
Tom Sepezd6262c12016-02-03 14:47:06 -08002320 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002321
Tom Sepezd6262c12016-02-03 14:47:06 -08002322 CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke);
Wei Li0fc6b252016-03-01 16:29:41 -08002323 if (!action.GetDict())
Tom Sepezd6262c12016-02-03 14:47:06 -08002324 return TRUE;
2325
2326 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2327 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2328 PDFSDK_FieldAction fa;
2329 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2330 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2331 fa.sValue = csValue;
2332 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::KeyStroke,
2333 m_pDocument, pFormField, fa);
2334 return fa.bRC;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002335}
2336
Tom Sepezd6262c12016-02-03 14:47:06 -08002337FX_BOOL CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField,
2338 const CFX_WideString& csValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002339 CPDF_AAction aAction = pFormField->GetAdditionalAction();
Wei Li0fc6b252016-03-01 16:29:41 -08002340 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Validate))
Tom Sepezd6262c12016-02-03 14:47:06 -08002341 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002342
Tom Sepezd6262c12016-02-03 14:47:06 -08002343 CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate);
Wei Li0fc6b252016-03-01 16:29:41 -08002344 if (!action.GetDict())
Tom Sepezd6262c12016-02-03 14:47:06 -08002345 return TRUE;
2346
2347 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
2348 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
2349 PDFSDK_FieldAction fa;
2350 fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0);
2351 fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0);
2352 fa.sValue = csValue;
2353 pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::Validate,
2354 m_pDocument, pFormField, fa);
2355 return fa.bRC;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002356}
2357
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002358FX_BOOL CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action) {
Wei Li0fc6b252016-03-01 16:29:41 -08002359 ASSERT(action.GetDict());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002360
Wei Li0fc6b252016-03-01 16:29:41 -08002361 CPDF_ActionFields af(&action);
Lei Zhangd88a3642015-11-10 09:38:57 -08002362 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2363 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002364
2365 FX_BOOL bHide = action.GetHideStatus();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002366 FX_BOOL bChanged = FALSE;
2367
Lei Zhangd88a3642015-11-10 09:38:57 -08002368 for (CPDF_FormField* pField : fields) {
2369 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
2370 CPDF_FormControl* pControl = pField->GetControl(i);
2371 ASSERT(pControl);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002372
2373 if (CPDFSDK_Widget* pWidget = GetWidget(pControl)) {
tsepezc3255f52016-03-25 14:52:27 -07002374 uint32_t nFlags = pWidget->GetFlags();
Lei Zhangd88a3642015-11-10 09:38:57 -08002375 nFlags &= ~ANNOTFLAG_INVISIBLE;
2376 nFlags &= ~ANNOTFLAG_NOVIEW;
2377 if (bHide)
2378 nFlags |= ANNOTFLAG_HIDDEN;
2379 else
2380 nFlags &= ~ANNOTFLAG_HIDDEN;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002381 pWidget->SetFlags(nFlags);
Lei Zhangd88a3642015-11-10 09:38:57 -08002382 pWidget->GetPageView()->UpdateView(pWidget);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002383 bChanged = TRUE;
2384 }
2385 }
2386 }
2387
2388 return bChanged;
2389}
2390
2391FX_BOOL CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002392 CFX_WideString sDestination = action.GetFilePath();
2393 if (sDestination.IsEmpty())
2394 return FALSE;
2395
2396 CPDF_Dictionary* pActionDict = action.GetDict();
2397 if (pActionDict->KeyExist("Fields")) {
Wei Li0fc6b252016-03-01 16:29:41 -08002398 CPDF_ActionFields af(&action);
tsepezc3255f52016-03-25 14:52:27 -07002399 uint32_t dwFlags = action.GetFlags();
Lei Zhangd88a3642015-11-10 09:38:57 -08002400 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2401 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
2402 if (!fields.empty()) {
2403 bool bIncludeOrExclude = !(dwFlags & 0x01);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002404 if (m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002405 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002406
Wei Li97da9762016-03-11 17:00:48 -08002407 return SubmitFields(sDestination, fields, bIncludeOrExclude, false);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002408 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002409 }
Lei Zhangd88a3642015-11-10 09:38:57 -08002410 if (m_pInterForm->CheckRequiredFields(nullptr, true))
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002411 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002412
2413 return SubmitForm(sDestination, FALSE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002414}
2415
Lei Zhangd88a3642015-11-10 09:38:57 -08002416FX_BOOL CPDFSDK_InterForm::SubmitFields(
2417 const CFX_WideString& csDestination,
2418 const std::vector<CPDF_FormField*>& fields,
Wei Li97da9762016-03-11 17:00:48 -08002419 bool bIncludeOrExclude,
2420 bool bUrlEncoded) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002421 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002422
2423 CFX_ByteTextBuf textBuf;
2424 ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf);
2425
2426 uint8_t* pBuffer = textBuf.GetBuffer();
2427 FX_STRSIZE nBufSize = textBuf.GetLength();
2428
Lei Zhangd88a3642015-11-10 09:38:57 -08002429 if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize))
2430 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002431
2432 pEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002433 return TRUE;
2434}
2435
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002436FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile,
2437 CFX_WideString csTxtFile) {
2438 return TRUE;
2439}
2440
2441FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf,
2442 FX_STRSIZE& nBufSize) {
2443 CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize);
2444 if (pFDF) {
Wei Li9b761132016-01-29 15:44:20 -08002445 CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictBy("FDF");
Lei Zhang997de612015-11-04 18:17:53 -08002446 if (!pMainDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002447 return FALSE;
2448
2449 // Get fields
Wei Li9b761132016-01-29 15:44:20 -08002450 CPDF_Array* pFields = pMainDict->GetArrayBy("Fields");
Lei Zhang997de612015-11-04 18:17:53 -08002451 if (!pFields)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002452 return FALSE;
2453
2454 CFX_ByteTextBuf fdfEncodedData;
tsepezc3255f52016-03-25 14:52:27 -07002455 for (uint32_t i = 0; i < pFields->GetCount(); i++) {
Wei Li9b761132016-01-29 15:44:20 -08002456 CPDF_Dictionary* pField = pFields->GetDictAt(i);
Lei Zhang997de612015-11-04 18:17:53 -08002457 if (!pField)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002458 continue;
2459 CFX_WideString name;
Wei Li9b761132016-01-29 15:44:20 -08002460 name = pField->GetUnicodeTextBy("T");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002461 CFX_ByteString name_b = CFX_ByteString::FromUnicode(name);
Wei Li9b761132016-01-29 15:44:20 -08002462 CFX_ByteString csBValue = pField->GetStringBy("V");
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002463 CFX_WideString csWValue = PDF_DecodeText(csBValue);
2464 CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue);
2465
Tom Sepez052a8d92016-02-19 14:41:46 -08002466 fdfEncodedData << name_b.GetBuffer(name_b.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002467 name_b.ReleaseBuffer();
Tom Sepez052a8d92016-02-19 14:41:46 -08002468 fdfEncodedData << "=";
2469 fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002470 csValue_b.ReleaseBuffer();
2471 if (i != pFields->GetCount() - 1)
Tom Sepez052a8d92016-02-19 14:41:46 -08002472 fdfEncodedData << "&";
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002473 }
2474
2475 nBufSize = fdfEncodedData.GetLength();
2476 pBuf = FX_Alloc(uint8_t, nBufSize);
2477 FXSYS_memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize);
2478 }
2479 return TRUE;
2480}
2481
Lei Zhangd88a3642015-11-10 09:38:57 -08002482FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(
2483 const std::vector<CPDF_FormField*>& fields,
Wei Li97da9762016-03-11 17:00:48 -08002484 bool bIncludeOrExclude,
Lei Zhangd88a3642015-11-10 09:38:57 -08002485 CFX_ByteTextBuf& textBuf) {
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08002486 std::unique_ptr<CFDF_Document> pFDF(m_pInterForm->ExportToFDF(
Lei Zhangd88a3642015-11-10 09:38:57 -08002487 m_pDocument->GetPath(), fields, bIncludeOrExclude));
2488 return pFDF ? pFDF->WriteBuf(textBuf) : FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002489}
2490
Tom Sepez51da0932015-11-25 16:05:49 -08002491#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002492void CPDFSDK_InterForm::SynchronizeField(CPDF_FormField* pFormField,
2493 FX_BOOL bSynchronizeElse) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002494 int x = 0;
2495 if (m_FieldSynchronizeMap.Lookup(pFormField, x))
2496 return;
2497
2498 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
2499 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
Lei Zhang5eca3052016-02-22 20:32:21 -08002500 ASSERT(pFormCtrl);
2501 ASSERT(m_pInterForm);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002502 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) {
2503 pWidget->Synchronize(bSynchronizeElse);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002504 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002505 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002506}
Tom Sepez40e9ff32015-11-30 12:39:54 -08002507#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002508
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002509CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName(
2510 const CFX_WideString& sFileExt) {
2511 CFX_WideString sFileName;
2512 return L"";
2513}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002514
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002515FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination,
2516 FX_BOOL bUrlEncoded) {
2517 if (sDestination.IsEmpty())
2518 return FALSE;
2519
Lei Zhang96660d62015-12-14 18:27:25 -08002520 if (!m_pDocument || !m_pInterForm)
2521 return FALSE;
2522
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002523 CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002524 CFX_WideString wsPDFFilePath = m_pDocument->GetPath();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002525 CFDF_Document* pFDFDoc = m_pInterForm->ExportToFDF(wsPDFFilePath);
Lei Zhang997de612015-11-04 18:17:53 -08002526 if (!pFDFDoc)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002527 return FALSE;
2528
2529 CFX_ByteTextBuf FdfBuffer;
2530 FX_BOOL bRet = pFDFDoc->WriteBuf(FdfBuffer);
2531 delete pFDFDoc;
2532 if (!bRet)
2533 return FALSE;
2534
2535 uint8_t* pBuffer = FdfBuffer.GetBuffer();
2536 FX_STRSIZE nBufSize = FdfBuffer.GetLength();
2537
2538 if (bUrlEncoded) {
2539 if (!FDFToURLEncodedData(pBuffer, nBufSize))
2540 return FALSE;
2541 }
2542
2543 pEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
2544
Lei Zhangda180e92015-08-14 22:22:13 -07002545 if (bUrlEncoded) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002546 FX_Free(pBuffer);
2547 pBuffer = NULL;
2548 }
2549
2550 return TRUE;
2551}
2552
2553FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002554 CFDF_Document* pFDF = m_pInterForm->ExportToFDF(m_pDocument->GetPath());
2555 if (!pFDF)
2556 return FALSE;
2557
2558 FX_BOOL bRet = pFDF->WriteBuf(textBuf);
2559 delete pFDF;
2560
2561 return bRet;
2562}
2563
2564FX_BOOL CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) {
Wei Li0fc6b252016-03-01 16:29:41 -08002565 ASSERT(action.GetDict());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002566
2567 CPDF_Dictionary* pActionDict = action.GetDict();
Lei Zhangd88a3642015-11-10 09:38:57 -08002568 if (!pActionDict->KeyExist("Fields"))
2569 return m_pInterForm->ResetForm(true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002570
Wei Li0fc6b252016-03-01 16:29:41 -08002571 CPDF_ActionFields af(&action);
tsepezc3255f52016-03-25 14:52:27 -07002572 uint32_t dwFlags = action.GetFlags();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002573
Lei Zhangd88a3642015-11-10 09:38:57 -08002574 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
2575 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
2576 return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002577}
2578
2579FX_BOOL CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action) {
2580 return FALSE;
2581}
2582
Lei Zhangd88a3642015-11-10 09:38:57 -08002583std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects(
2584 const std::vector<CPDF_Object*>& objects) const {
2585 std::vector<CPDF_FormField*> fields;
2586 for (CPDF_Object* pObject : objects) {
2587 if (pObject && pObject->IsString()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002588 CFX_WideString csName = pObject->GetUnicodeText();
2589 CPDF_FormField* pField = m_pInterForm->GetField(0, csName);
Dan Sinclaird43ebdd2015-10-27 15:37:54 -04002590 if (pField)
Lei Zhangd88a3642015-11-10 09:38:57 -08002591 fields.push_back(pField);
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002592 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002593 }
Lei Zhangd88a3642015-11-10 09:38:57 -08002594 return fields;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002595}
2596
Tom Sepezd6262c12016-02-03 14:47:06 -08002597int CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField,
2598 const CFX_WideString& csValue) {
2599 int nType = pField->GetFieldType();
2600 if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002601 return 0;
2602
Tom Sepezd6262c12016-02-03 14:47:06 -08002603 if (!OnKeyStrokeCommit(pField, csValue))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002604 return -1;
2605
Tom Sepezd6262c12016-02-03 14:47:06 -08002606 if (!OnValidate(pField, csValue))
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002607 return -1;
2608
2609 return 1;
2610}
2611
Tom Sepezd6262c12016-02-03 14:47:06 -08002612void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) {
2613#ifdef PDF_ENABLE_XFA
2614 SynchronizeField(pField, FALSE);
2615#endif // PDF_ENABLE_XFA
2616 int nType = pField->GetFieldType();
2617 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
2618 OnCalculate(pField);
2619 FX_BOOL bFormated = FALSE;
2620 CFX_WideString sValue = OnFormat(pField, bFormated);
2621 ResetFieldAppearance(pField, bFormated ? sValue.c_str() : nullptr, TRUE);
2622 UpdateField(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002623 }
Tom Sepezd6262c12016-02-03 14:47:06 -08002624}
2625
2626int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField,
2627 const CFX_WideString& csValue) {
2628 if (pField->GetFieldType() != FIELDTYPE_LISTBOX)
2629 return 0;
2630
2631 if (!OnKeyStrokeCommit(pField, csValue))
2632 return -1;
2633
2634 if (!OnValidate(pField, csValue))
2635 return -1;
2636
2637 return 1;
2638}
2639
2640void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) {
2641 if (pField->GetFieldType() == FIELDTYPE_LISTBOX) {
2642 OnCalculate(pField);
2643 ResetFieldAppearance(pField, NULL, TRUE);
2644 UpdateField(pField);
2645 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002646}
2647
Tom Sepezed5d7aa2016-02-02 16:02:03 -08002648void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) {
2649 int nType = pField->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002650 if (nType == FIELDTYPE_CHECKBOX || nType == FIELDTYPE_RADIOBUTTON) {
Tom Sepezed5d7aa2016-02-02 16:02:03 -08002651 OnCalculate(pField);
2652 UpdateField(pField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002653 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002654}
2655
Tom Sepezd6262c12016-02-03 14:47:06 -08002656int CPDFSDK_InterForm::BeforeFormReset(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002657 return 0;
2658}
2659
Tom Sepezd6262c12016-02-03 14:47:06 -08002660void CPDFSDK_InterForm::AfterFormReset(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002661 OnCalculate(nullptr);
Tom Sepezd6262c12016-02-03 14:47:06 -08002662}
2663
2664int CPDFSDK_InterForm::BeforeFormImportData(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002665 return 0;
2666}
2667
Tom Sepezd6262c12016-02-03 14:47:06 -08002668void CPDFSDK_InterForm::AfterFormImportData(CPDF_InterForm* pForm) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002669 OnCalculate(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002670}
2671
2672FX_BOOL CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002673 if (nFieldType < 1 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002674 return FALSE;
2675 return m_bNeedHightlight[nFieldType - 1];
2676}
2677
2678void CPDFSDK_InterForm::RemoveAllHighLight() {
Tom Sepezae7a9172015-11-23 09:22:46 -08002679 for (int i = 0; i < kNumFieldTypes; ++i)
2680 m_bNeedHightlight[i] = FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002681}
Tom Sepezae7a9172015-11-23 09:22:46 -08002682
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002683void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002684 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002685 return;
2686 switch (nFieldType) {
2687 case 0: {
Tom Sepezae7a9172015-11-23 09:22:46 -08002688 for (int i = 0; i < kNumFieldTypes; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002689 m_aHighlightColor[i] = clr;
2690 m_bNeedHightlight[i] = TRUE;
2691 }
2692 break;
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002693 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002694 default: {
2695 m_aHighlightColor[nFieldType - 1] = clr;
2696 m_bNeedHightlight[nFieldType - 1] = TRUE;
2697 break;
2698 }
2699 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002700}
2701
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002702FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) {
Tom Sepezae7a9172015-11-23 09:22:46 -08002703 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002704 return FXSYS_RGB(255, 255, 255);
2705 if (nFieldType == 0)
2706 return m_aHighlightColor[0];
2707 return m_aHighlightColor[nFieldType - 1];
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002708}
2709
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002710CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView,
2711 const CFX_ByteString& sType,
2712 const CFX_ByteString& sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002713 : m_eTabOrder(STRUCTURE),
2714 m_pPageView(pPageView),
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002715 m_sType(sType),
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002716 m_sSubType(sSubType) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002717 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage();
Wei Li9b761132016-01-29 15:44:20 -08002718 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringBy("Tabs");
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002719 if (sTabs == "R")
2720 m_eTabOrder = ROW;
2721 else if (sTabs == "C")
2722 m_eTabOrder = COLUMN;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002723
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002724 GenerateResults();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002725}
2726
Dan Sinclairf766ad22016-03-14 13:51:24 -04002727CBA_AnnotIterator::~CBA_AnnotIterator() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002728
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002729CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002730 return m_Annots.empty() ? nullptr : m_Annots.front();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002731}
2732
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002733CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002734 return m_Annots.empty() ? nullptr : m_Annots.back();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002735}
2736
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002737CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002738 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2739 if (iter == m_Annots.end())
2740 return nullptr;
2741 ++iter;
2742 if (iter == m_Annots.end())
2743 iter = m_Annots.begin();
2744 return *iter;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002745}
2746
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002747CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002748 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
2749 if (iter == m_Annots.end())
2750 return nullptr;
2751 if (iter == m_Annots.begin())
2752 iter = m_Annots.end();
2753 return *(--iter);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002754}
2755
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002756// static
2757bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1,
2758 const CPDFSDK_Annot* p2) {
2759 return GetAnnotRect(p1).left < GetAnnotRect(p2).left;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002760}
2761
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002762// static
2763bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1,
2764 const CPDFSDK_Annot* p2) {
2765 return GetAnnotRect(p1).top > GetAnnotRect(p2).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002766}
2767
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002768void CBA_AnnotIterator::GenerateResults() {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002769 switch (m_eTabOrder) {
2770 case STRUCTURE: {
Lei Zhangbf60b292015-10-26 12:14:35 -07002771 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002772 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002773 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002774 m_Annots.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002775 }
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002776 } break;
2777 case ROW: {
2778 std::vector<CPDFSDK_Annot*> sa;
Lei Zhang50218532015-10-30 14:03:43 -07002779 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2780 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2781 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002782 sa.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002783 }
2784
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002785 std::sort(sa.begin(), sa.end(), CompareByLeftAscending);
2786 while (!sa.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002787 int nLeftTopIndex = -1;
Lei Zhang50218532015-10-30 14:03:43 -07002788 FX_FLOAT fTop = 0.0f;
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002789 for (int i = sa.size() - 1; i >= 0; i--) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002790 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002791 if (rcAnnot.top > fTop) {
2792 nLeftTopIndex = i;
2793 fTop = rcAnnot.top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002794 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002795 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002796 if (nLeftTopIndex >= 0) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002797 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
Tom Sepez281a9ea2016-02-26 14:24:28 -08002798 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002799 m_Annots.push_back(pLeftTopAnnot);
2800 sa.erase(sa.begin() + nLeftTopIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002801
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002802 std::vector<int> aSelect;
Wei Li89409932016-03-28 10:33:33 -07002803 for (size_t i = 0; i < sa.size(); ++i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002804 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002805 FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f;
2806 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002807 aSelect.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002808 }
Wei Li89409932016-03-28 10:33:33 -07002809 for (size_t i = 0; i < aSelect.size(); ++i)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002810 m_Annots.push_back(sa[aSelect[i]]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002811
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002812 for (int i = aSelect.size() - 1; i >= 0; --i)
2813 sa.erase(sa.begin() + aSelect[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002814 }
2815 }
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002816 } break;
2817 case COLUMN: {
2818 std::vector<CPDFSDK_Annot*> sa;
Lei Zhang50218532015-10-30 14:03:43 -07002819 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
2820 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
2821 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002822 sa.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002823 }
2824
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002825 std::sort(sa.begin(), sa.end(), CompareByTopDescending);
2826 while (!sa.empty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002827 int nLeftTopIndex = -1;
Lei Zhang50218532015-10-30 14:03:43 -07002828 FX_FLOAT fLeft = -1.0f;
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002829 for (int i = sa.size() - 1; i >= 0; --i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002830 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002831 if (fLeft < 0) {
2832 nLeftTopIndex = 0;
2833 fLeft = rcAnnot.left;
2834 } else if (rcAnnot.left < fLeft) {
2835 nLeftTopIndex = i;
2836 fLeft = rcAnnot.left;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002837 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -07002838 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002839
2840 if (nLeftTopIndex >= 0) {
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002841 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex];
Tom Sepez281a9ea2016-02-26 14:24:28 -08002842 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002843 m_Annots.push_back(pLeftTopAnnot);
2844 sa.erase(sa.begin() + nLeftTopIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002845
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002846 std::vector<int> aSelect;
Wei Li89409932016-03-28 10:33:33 -07002847 for (size_t i = 0; i < sa.size(); ++i) {
Tom Sepez281a9ea2016-02-26 14:24:28 -08002848 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Lei Zhang50218532015-10-30 14:03:43 -07002849 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f;
2850 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002851 aSelect.push_back(i);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002852 }
Wei Li89409932016-03-28 10:33:33 -07002853 for (size_t i = 0; i < aSelect.size(); ++i)
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002854 m_Annots.push_back(sa[aSelect[i]]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002855
Tom Sepezb9cc7a02016-02-01 13:42:30 -08002856 for (int i = aSelect.size() - 1; i >= 0; --i)
2857 sa.erase(sa.begin() + aSelect[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002858 }
2859 }
Lei Zhang50218532015-10-30 14:03:43 -07002860 break;
2861 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002862 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002863}
2864
Tom Sepez281a9ea2016-02-26 14:24:28 -08002865CFX_FloatRect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) {
2866 CFX_FloatRect rcAnnot;
Lei Zhang50218532015-10-30 14:03:43 -07002867 pAnnot->GetPDFAnnot()->GetRect(rcAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07002868 return rcAnnot;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002869}