blob: 60e7289234154390b22be1c9d95300ae5ad97249 [file] [log] [blame]
jaepark611adb82016-08-17 11:34:36 -07001// Copyright 2016 PDFium Authors. All rights reserved.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07002// 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
dsinclair114e46a2016-09-29 17:18:21 -07007#include "fpdfsdk/cpdfsdk_widget.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -08008
Lei Zhang12885992019-02-02 00:29:49 +00009#include "constants/annotation_common.h"
Lei Zhange9826282021-06-08 08:30:48 +000010#include "constants/appearance.h"
tsepez0e606b52016-11-18 16:22:41 -080011#include "core/fpdfapi/parser/cpdf_array.h"
dsinclair488b7ad2016-10-04 11:55:50 -070012#include "core/fpdfapi/parser/cpdf_dictionary.h"
13#include "core/fpdfapi/parser/cpdf_document.h"
tsepez0e606b52016-11-18 16:22:41 -080014#include "core/fpdfapi/parser/cpdf_reference.h"
dsinclair488b7ad2016-10-04 11:55:50 -070015#include "core/fpdfapi/parser/cpdf_stream.h"
tsepez0e606b52016-11-18 16:22:41 -080016#include "core/fpdfapi/parser/cpdf_string.h"
Tom Sepez719b2fd2021-04-23 21:26:14 +000017#include "core/fpdfdoc/cpdf_bafontmap.h"
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/cpdf_defaultappearance.h"
19#include "core/fpdfdoc/cpdf_formcontrol.h"
20#include "core/fpdfdoc/cpdf_formfield.h"
21#include "core/fpdfdoc/cpdf_iconfit.h"
Lei Zhangc3450652018-10-11 16:54:42 +000022#include "core/fpdfdoc/cpdf_interactiveform.h"
Hui Yingstd47c4012020-07-07 20:24:05 +000023#include "core/fxge/cfx_fillrenderoptions.h"
dsinclair74a34fc2016-09-29 16:41:42 -070024#include "core/fxge/cfx_graphstatedata.h"
Lei Zhang36865762021-06-12 01:07:37 +000025#include "core/fxge/cfx_path.h"
dsinclair74a34fc2016-09-29 16:41:42 -070026#include "core/fxge/cfx_renderdevice.h"
Dan Sinclair7d125322018-03-28 18:49:34 +000027#include "fpdfsdk/cpdfsdk_actionhandler.h"
Tom Sepez6c62e0e2019-08-01 21:49:43 +000028#include "fpdfsdk/cpdfsdk_appstream.h"
dsinclair735606d2016-10-05 15:47:02 -070029#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
Lei Zhangc3450652018-10-11 16:54:42 +000030#include "fpdfsdk/cpdfsdk_interactiveform.h"
dsinclair114e46a2016-09-29 17:18:21 -070031#include "fpdfsdk/cpdfsdk_pageview.h"
Tom Sepezc6b91df2021-09-11 02:14:19 +000032#include "fpdfsdk/formfiller/cffl_fieldaction.h"
Dan Sinclairc411eb92017-07-25 09:39:30 -040033#include "fpdfsdk/pwl/cpwl_edit.h"
Tom Sepez9c78c0d2021-01-27 20:13:14 +000034#include "third_party/base/check.h"
Lei Zhang30632072020-10-02 01:57:54 +000035#include "third_party/base/notreached.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
Tom Sepez40e9ff32015-11-30 12:39:54 -080037#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070038#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair5b493092016-09-29 20:20:24 -070039#include "xfa/fxfa/cxfa_eventparam.h"
Dan Sinclair80c48782017-03-23 12:11:20 -040040#include "xfa/fxfa/cxfa_ffdocview.h"
41#include "xfa/fxfa/cxfa_ffwidget.h"
42#include "xfa/fxfa/cxfa_ffwidgethandler.h"
Dan Sinclair0c53b002018-01-08 14:30:00 -050043#include "xfa/fxfa/parser/cxfa_node.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080044#endif // PDF_ENABLE_XFA
45
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046CPDFSDK_Widget::CPDFSDK_Widget(CPDF_Annot* pAnnot,
47 CPDFSDK_PageView* pPageView,
Lei Zhang073ecf42018-10-11 16:56:00 +000048 CPDFSDK_InteractiveForm* pInteractiveForm)
49 : CPDFSDK_BAAnnot(pAnnot, pPageView),
50 m_pInteractiveForm(pInteractiveForm) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
Lei Zhangc1bf9982018-10-09 19:14:15 +000052CPDFSDK_Widget::~CPDFSDK_Widget() = default;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053
Tom Sepez51da0932015-11-25 16:05:49 -080054#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -070055CXFA_FFWidget* CPDFSDK_Widget::GetMixXFAWidget() const {
Daniel Hosseinian7891b462020-01-16 22:45:17 +000056 CPDF_Document::Extension* pContext =
57 m_pPageView->GetFormFillEnv()->GetDocExtension();
58 if (!pContext || !pContext->ContainsExtensionForegroundForm())
Tom Sepez51229b62019-01-07 20:24:59 +000059 return nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -070060
Tom Sepez0126b032019-08-19 16:48:25 +000061 CXFA_FFDocView* pDocView =
62 static_cast<CPDFXFA_Context*>(pContext)->GetXFADocView();
Tom Sepez51229b62019-01-07 20:24:59 +000063 if (!pDocView)
64 return nullptr;
65
66 WideString sName;
67 if (GetFieldType() == FormFieldType::kRadioButton) {
68 sName = GetAnnotName();
69 if (sName.IsEmpty())
70 sName = GetName();
71 } else {
72 sName = GetName();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 }
Tom Sepez51229b62019-01-07 20:24:59 +000074
75 if (sName.IsEmpty())
76 return nullptr;
77
78 return pDocView->GetWidgetByName(sName, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079}
80
Lei Zhangb353f8c2018-10-10 18:39:14 +000081CXFA_FFWidget* CPDFSDK_Widget::GetGroupMixXFAWidget() const {
Daniel Hosseinian7891b462020-01-16 22:45:17 +000082 CPDF_Document::Extension* pContext =
83 m_pPageView->GetFormFillEnv()->GetDocExtension();
84 if (!pContext || !pContext->ContainsExtensionForegroundForm())
Dan Sinclaircdba7472017-03-23 09:17:10 -040085 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086
Tom Sepez0126b032019-08-19 16:48:25 +000087 CXFA_FFDocView* pDocView =
88 static_cast<CPDFXFA_Context*>(pContext)->GetXFADocView();
Dan Sinclaircdba7472017-03-23 09:17:10 -040089 if (!pDocView)
90 return nullptr;
91
Ryan Harrison275e2602017-09-18 14:23:18 -040092 WideString sName = GetName();
Dan Sinclaircdba7472017-03-23 09:17:10 -040093 return !sName.IsEmpty() ? pDocView->GetWidgetByName(sName, nullptr) : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094}
95
dsinclairdf4bc592016-03-31 20:34:43 -070096CXFA_FFWidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() const {
Daniel Hosseinian7891b462020-01-16 22:45:17 +000097 CPDF_Document::Extension* pContext =
98 m_pPageView->GetFormFillEnv()->GetDocExtension();
99 if (!pContext || !pContext->ContainsExtensionForegroundForm())
Dan Sinclaircdba7472017-03-23 09:17:10 -0400100 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101
Tom Sepezecb8d872020-09-29 18:50:00 +0000102 CXFA_FFDocView* pDocView =
103 static_cast<CPDFXFA_Context*>(pContext)->GetXFADocView();
104 return pDocView ? pDocView->GetWidgetHandler() : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105}
106
107static XFA_EVENTTYPE GetXFAEventType(PDFSDK_XFAAActionType eXFAAAT) {
108 XFA_EVENTTYPE eEventType = XFA_EVENT_Unknown;
109
110 switch (eXFAAAT) {
111 case PDFSDK_XFA_Click:
112 eEventType = XFA_EVENT_Click;
113 break;
114 case PDFSDK_XFA_Full:
115 eEventType = XFA_EVENT_Full;
116 break;
117 case PDFSDK_XFA_PreOpen:
118 eEventType = XFA_EVENT_PreOpen;
119 break;
120 case PDFSDK_XFA_PostOpen:
121 eEventType = XFA_EVENT_PostOpen;
122 break;
123 }
124
125 return eEventType;
126}
127
128static XFA_EVENTTYPE GetXFAEventType(CPDF_AAction::AActionType eAAT,
tsepez4cf55152016-11-02 14:37:54 -0700129 bool bWillCommit) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 XFA_EVENTTYPE eEventType = XFA_EVENT_Unknown;
131
132 switch (eAAT) {
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000133 case CPDF_AAction::kCursorEnter:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 eEventType = XFA_EVENT_MouseEnter;
135 break;
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000136 case CPDF_AAction::kCursorExit:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 eEventType = XFA_EVENT_MouseExit;
138 break;
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000139 case CPDF_AAction::kButtonDown:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 eEventType = XFA_EVENT_MouseDown;
141 break;
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000142 case CPDF_AAction::kButtonUp:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143 eEventType = XFA_EVENT_MouseUp;
144 break;
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000145 case CPDF_AAction::kGetFocus:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 eEventType = XFA_EVENT_Enter;
147 break;
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000148 case CPDF_AAction::kLoseFocus:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 eEventType = XFA_EVENT_Exit;
150 break;
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000151 case CPDF_AAction::kPageOpen:
152 case CPDF_AAction::kPageClose:
153 case CPDF_AAction::kPageVisible:
154 case CPDF_AAction::kPageInvisible:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 break;
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000156 case CPDF_AAction::kKeyStroke:
jaepark611adb82016-08-17 11:34:36 -0700157 if (!bWillCommit)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 eEventType = XFA_EVENT_Change;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 break;
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000160 case CPDF_AAction::kValidate:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 eEventType = XFA_EVENT_Validate;
162 break;
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000163 case CPDF_AAction::kOpenPage:
164 case CPDF_AAction::kClosePage:
165 case CPDF_AAction::kFormat:
166 case CPDF_AAction::kCalculate:
167 case CPDF_AAction::kCloseDocument:
168 case CPDF_AAction::kSaveDocument:
169 case CPDF_AAction::kDocumentSaved:
170 case CPDF_AAction::kPrintDocument:
171 case CPDF_AAction::kDocumentPrinted:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 break;
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000173 case CPDF_AAction::kDocumentOpen:
174 case CPDF_AAction::kNumberOfActions:
Lei Zhang69fe7112017-09-14 18:26:26 -0700175 NOTREACHED();
176 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 }
178
179 return eEventType;
180}
181
Lei Zhangb353f8c2018-10-10 18:39:14 +0000182bool CPDFSDK_Widget::HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT) const {
Tom Sepez78d60bc2020-09-09 16:43:59 +0000183 CXFA_FFWidget* pWidget = GetMixXFAWidget();
Lei Zhang1bd6c152019-07-08 23:33:08 +0000184 if (!pWidget)
tsepez4cf55152016-11-02 14:37:54 -0700185 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186
jaepark611adb82016-08-17 11:34:36 -0700187 CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler();
188 if (!pXFAWidgetHandler)
tsepez4cf55152016-11-02 14:37:54 -0700189 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190
jaepark611adb82016-08-17 11:34:36 -0700191 XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
jaepark611adb82016-08-17 11:34:36 -0700192 if ((eEventType == XFA_EVENT_Click || eEventType == XFA_EVENT_Change) &&
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000193 GetFieldType() == FormFieldType::kRadioButton) {
Tom Sepez20325642019-12-20 22:22:34 +0000194 CXFA_FFWidget* hGroupWidget = GetGroupMixXFAWidget();
195 if (hGroupWidget &&
196 hGroupWidget->HasEventUnderHandler(eEventType, pXFAWidgetHandler)) {
197 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 }
199 }
Lei Zhang1bd6c152019-07-08 23:33:08 +0000200
Tom Sepez78d60bc2020-09-09 16:43:59 +0000201 return pWidget->HasEventUnderHandler(eEventType, pXFAWidgetHandler);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202}
203
tsepez4cf55152016-11-02 14:37:54 -0700204bool CPDFSDK_Widget::OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
Tom Sepezc6b91df2021-09-11 02:14:19 +0000205 CFFL_FieldAction* data,
Lei Zhang2a4010e2021-06-07 19:18:25 +0000206 const CPDFSDK_PageView* pPageView) {
Tom Sepezd48bd292019-08-14 19:48:55 +0000207 auto* pContext = static_cast<CPDFXFA_Context*>(
208 m_pPageView->GetFormFillEnv()->GetDocExtension());
Daniel Hosseinian7891b462020-01-16 22:45:17 +0000209 if (!pContext)
210 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211
Tom Sepez78d60bc2020-09-09 16:43:59 +0000212 CXFA_FFWidget* pWidget = GetMixXFAWidget();
Lei Zhang1bd6c152019-07-08 23:33:08 +0000213 if (!pWidget)
tsepez4cf55152016-11-02 14:37:54 -0700214 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215
jaepark611adb82016-08-17 11:34:36 -0700216 XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
217 if (eEventType == XFA_EVENT_Unknown)
tsepez4cf55152016-11-02 14:37:54 -0700218 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219
jaepark611adb82016-08-17 11:34:36 -0700220 CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler();
221 if (!pXFAWidgetHandler)
tsepez4cf55152016-11-02 14:37:54 -0700222 return false;
jaepark611adb82016-08-17 11:34:36 -0700223
224 CXFA_EventParam param;
225 param.m_eType = eEventType;
Dan Sinclair8cdea722018-01-30 18:56:50 +0000226 param.m_wsChange = data->sChange;
Dan Sinclair1d82ba42018-01-30 19:24:00 +0000227 param.m_iCommitKey = 0;
Dan Sinclair8cdea722018-01-30 18:56:50 +0000228 param.m_bShift = data->bShift;
229 param.m_iSelStart = data->nSelStart;
230 param.m_iSelEnd = data->nSelEnd;
231 param.m_wsFullText = data->sValue;
232 param.m_bKeyDown = data->bKeyDown;
233 param.m_bModifier = data->bModifier;
Dan Sinclair8cdea722018-01-30 18:56:50 +0000234 param.m_wsPrevText = data->sValue;
jaepark611adb82016-08-17 11:34:36 -0700235 if ((eEventType == XFA_EVENT_Click || eEventType == XFA_EVENT_Change) &&
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000236 GetFieldType() == FormFieldType::kRadioButton) {
Tom Sepez20325642019-12-20 22:22:34 +0000237 CXFA_FFWidget* hGroupWidget = GetGroupMixXFAWidget();
238 if (hGroupWidget &&
239 !hGroupWidget->ProcessEventUnderHandler(&param, pXFAWidgetHandler)) {
240 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 }
242 }
243
Tom Sepez20325642019-12-20 22:22:34 +0000244 bool ret = pWidget->ProcessEventUnderHandler(&param, pXFAWidgetHandler);
Tom Sepez78d60bc2020-09-09 16:43:59 +0000245 CXFA_FFDocView* pDocView = pContext->GetXFADocView();
246 if (pDocView)
jaepark611adb82016-08-17 11:34:36 -0700247 pDocView->UpdateDocView();
248
Tom Sepez20325642019-12-20 22:22:34 +0000249 return ret;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250}
251
tsepez4cf55152016-11-02 14:37:54 -0700252void CPDFSDK_Widget::Synchronize(bool bSynchronizeElse) {
jaepark611adb82016-08-17 11:34:36 -0700253 CXFA_FFWidget* hWidget = GetMixXFAWidget();
254 if (!hWidget)
255 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256
Dan Sinclaird7d584d2018-01-22 18:41:36 +0000257 CXFA_Node* node = hWidget->GetNode();
258 if (!node->IsWidgetReady())
jaepark611adb82016-08-17 11:34:36 -0700259 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260
jaepark611adb82016-08-17 11:34:36 -0700261 CPDF_FormField* pFormField = GetFormField();
262 switch (GetFieldType()) {
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000263 case FormFieldType::kCheckBox:
264 case FormFieldType::kRadioButton: {
jaepark611adb82016-08-17 11:34:36 -0700265 CPDF_FormControl* pFormCtrl = GetFormControl();
Tom Sepez4fd3f632021-05-21 00:15:00 +0000266 XFA_CheckState eCheckState =
267 pFormCtrl->IsChecked() ? XFA_CheckState::kOn : XFA_CheckState::kOff;
Tom Sepezc317e8f2021-04-15 17:07:13 +0000268 node->SetCheckState(eCheckState);
jaepark611adb82016-08-17 11:34:36 -0700269 break;
270 }
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000271 case FormFieldType::kTextField:
Tom Sepez4fd3f632021-05-21 00:15:00 +0000272 node->SetValue(XFA_ValuePicture::kEdit, pFormField->GetValue());
jaepark611adb82016-08-17 11:34:36 -0700273 break;
Dan Sinclair95b232f2018-01-29 22:21:36 +0000274 case FormFieldType::kComboBox:
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000275 case FormFieldType::kListBox: {
Dan Sinclair414cc9c2018-01-23 21:17:43 +0000276 node->ClearAllSelections();
Dan Sinclair95b232f2018-01-29 22:21:36 +0000277 for (int i = 0; i < pFormField->CountSelectedItems(); ++i) {
jaepark611adb82016-08-17 11:34:36 -0700278 int nIndex = pFormField->GetSelectedIndex(i);
Dan Sinclair414cc9c2018-01-23 21:17:43 +0000279 if (nIndex > -1 && nIndex < node->CountChoiceListItems(false))
280 node->SetItemState(nIndex, true, false, false, true);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 }
Dan Sinclair95b232f2018-01-29 22:21:36 +0000282 if (GetFieldType() == FormFieldType::kComboBox)
Tom Sepez4fd3f632021-05-21 00:15:00 +0000283 node->SetValue(XFA_ValuePicture::kEdit, pFormField->GetValue());
jaepark611adb82016-08-17 11:34:36 -0700284 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 }
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000286 default:
287 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 }
jaepark611adb82016-08-17 11:34:36 -0700289
Dan Sinclairc1e57562018-01-04 11:38:38 -0500290 if (bSynchronizeElse) {
Tom Sepezd48bd292019-08-14 19:48:55 +0000291 auto* context = static_cast<CPDFXFA_Context*>(
292 m_pPageView->GetFormFillEnv()->GetDocExtension());
Dan Sinclair8a0941d2018-01-23 16:52:14 +0000293 context->GetXFADocView()->ProcessValueChanged(node);
Dan Sinclairc1e57562018-01-04 11:38:38 -0500294 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295}
Tom Sepezbe0591a2021-04-16 18:59:33 +0000296
297bool CPDFSDK_Widget::HandleXFAAAction(
298 CPDF_AAction::AActionType type,
Tom Sepezc6b91df2021-09-11 02:14:19 +0000299 CFFL_FieldAction* data,
Tom Sepezbe0591a2021-04-16 18:59:33 +0000300 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
301 auto* pContext =
302 static_cast<CPDFXFA_Context*>(pFormFillEnv->GetDocExtension());
303 if (!pContext)
304 return false;
305
306 CXFA_FFWidget* hWidget = GetMixXFAWidget();
307 if (!hWidget)
308 return false;
309
310 XFA_EVENTTYPE eEventType = GetXFAEventType(type, data->bWillCommit);
311 if (eEventType == XFA_EVENT_Unknown)
312 return false;
313
314 CXFA_FFWidgetHandler* pXFAWidgetHandler = GetXFAWidgetHandler();
315 if (!pXFAWidgetHandler)
316 return false;
317
318 CXFA_EventParam param;
319 param.m_eType = eEventType;
320 param.m_wsChange = data->sChange;
321 param.m_iCommitKey = 0;
322 param.m_bShift = data->bShift;
323 param.m_iSelStart = data->nSelStart;
324 param.m_iSelEnd = data->nSelEnd;
325 param.m_wsFullText = data->sValue;
326 param.m_bKeyDown = data->bKeyDown;
327 param.m_bModifier = data->bModifier;
328 param.m_wsPrevText = data->sValue;
329 bool ret = hWidget->ProcessEventUnderHandler(&param, pXFAWidgetHandler);
330 CXFA_FFDocView* pDocView = pContext->GetXFADocView();
331 if (pDocView)
332 pDocView->UpdateDocView();
333
334 return ret;
335}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800336#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337
tsepez4cf55152016-11-02 14:37:54 -0700338bool CPDFSDK_Widget::IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode mode) {
Lei Zhang12885992019-02-02 00:29:49 +0000339 const CPDF_Dictionary* pAP =
340 GetAnnotDict()->GetDictFor(pdfium::annotation::kAP);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 if (!pAP)
tsepez4cf55152016-11-02 14:37:54 -0700342 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 // Choose the right sub-ap
Dan Sinclair812e96c2017-03-13 16:43:37 -0400345 const char* ap_entry = "N";
Tom Sepez90b07c22021-08-26 20:35:34 +0000346 if (mode == CPDF_Annot::AppearanceMode::kDown)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 ap_entry = "D";
Tom Sepez90b07c22021-08-26 20:35:34 +0000348 else if (mode == CPDF_Annot::AppearanceMode::kRollover)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 ap_entry = "R";
350 if (!pAP->KeyExist(ap_entry))
351 ap_entry = "N";
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 // Get the AP stream or subdirectory
Lei Zhang12885992019-02-02 00:29:49 +0000354 const CPDF_Object* pSub = pAP->GetDirectObjectFor(ap_entry);
355 if (!pSub)
tsepez4cf55152016-11-02 14:37:54 -0700356 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700357
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000358 FormFieldType fieldType = GetFieldType();
359 switch (fieldType) {
360 case FormFieldType::kPushButton:
361 case FormFieldType::kComboBox:
362 case FormFieldType::kListBox:
363 case FormFieldType::kTextField:
364 case FormFieldType::kSignature:
Lei Zhang12885992019-02-02 00:29:49 +0000365 return pSub->IsStream();
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000366 case FormFieldType::kCheckBox:
367 case FormFieldType::kRadioButton:
Lei Zhang12885992019-02-02 00:29:49 +0000368 if (const CPDF_Dictionary* pSubDict = pSub->AsDictionary()) {
dsinclair38fd8442016-09-15 10:15:32 -0700369 return !!pSubDict->GetStreamFor(GetAppState());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 }
tsepez4cf55152016-11-02 14:37:54 -0700371 return false;
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000372 default:
373 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375}
376
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000377FormFieldType CPDFSDK_Widget::GetFieldType() const {
dsinclair92828192016-08-17 13:28:51 -0700378 CPDF_FormField* pField = GetFormField();
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000379 return pField ? pField->GetFieldType() : FormFieldType::kUnknown;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380}
381
Tom Sepezc1415542021-09-21 21:48:59 +0000382void CPDFSDK_Widget::SetRect(const CFX_FloatRect& rect) {
383 DCHECK(rect.right - rect.left >= 1.0f);
384 DCHECK(rect.top - rect.bottom >= 1.0f);
385 GetAnnotDict()->SetRectFor(pdfium::annotation::kRect, rect);
386}
387
tsepez4cf55152016-11-02 14:37:54 -0700388bool CPDFSDK_Widget::IsAppearanceValid() {
Tom Sepez51da0932015-11-25 16:05:49 -0800389#ifdef PDF_ENABLE_XFA
Daniel Hosseinian7891b462020-01-16 22:45:17 +0000390 CPDF_Document::Extension* pContext =
391 m_pPageView->GetFormFillEnv()->GetDocExtension();
392 if (pContext && pContext->ContainsExtensionFullForm())
tsepez4cf55152016-11-02 14:37:54 -0700393 return true;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800394#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800395 return CPDFSDK_BAAnnot::IsAppearanceValid();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396}
397
weili625ad662016-06-15 11:21:33 -0700398int CPDFSDK_Widget::GetLayoutOrder() const {
399 return 2;
400}
401
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402int CPDFSDK_Widget::GetFieldFlags() const {
Lei Zhang19ebc122019-03-25 17:58:11 +0000403 return GetFormField()->GetFieldFlags();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404}
405
jaepark9ed91372016-08-26 16:16:10 -0700406bool CPDFSDK_Widget::IsSignatureWidget() const {
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000407 return GetFieldType() == FormFieldType::kSignature;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408}
409
410CPDF_FormField* CPDFSDK_Widget::GetFormField() const {
dsinclair92828192016-08-17 13:28:51 -0700411 CPDF_FormControl* pControl = GetFormControl();
412 return pControl ? pControl->GetField() : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700413}
414
415CPDF_FormControl* CPDFSDK_Widget::GetFormControl() const {
Lei Zhang073ecf42018-10-11 16:56:00 +0000416 CPDF_InteractiveForm* pPDFInteractiveForm =
417 m_pInteractiveForm->GetInteractiveForm();
418 return pPDFInteractiveForm->GetControlByDict(GetAnnotDict());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419}
420
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421int CPDFSDK_Widget::GetRotate() const {
422 CPDF_FormControl* pCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 return pCtrl->GetRotation() % 360;
424}
425
Tom Sepez51da0932015-11-25 16:05:49 -0800426#ifdef PDF_ENABLE_XFA
Ryan Harrison275e2602017-09-18 14:23:18 -0400427WideString CPDFSDK_Widget::GetName() const {
Dan Sinclair1917cdd2018-01-30 17:29:21 +0000428 return GetFormField()->GetFullName();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800430#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431
Lei Zhang2c495302021-10-07 23:13:30 +0000432absl::optional<FX_COLORREF> CPDFSDK_Widget::GetFillColor() const {
Lei Zhangbe79d1b2021-06-07 19:21:05 +0000433 CFX_Color::TypeAndARGB type_argb_pair =
Lei Zhange9826282021-06-08 08:30:48 +0000434 GetFormControl()->GetColorARGB(pdfium::appearance::kBG);
Tom Sepez33baa9f2021-05-18 19:02:49 +0000435
Lei Zhangbe79d1b2021-06-07 19:21:05 +0000436 if (type_argb_pair.color_type == CFX_Color::Type::kTransparent)
Lei Zhanga3a739d2021-10-07 20:46:31 +0000437 return absl::nullopt;
Tom Sepez33baa9f2021-05-18 19:02:49 +0000438
Lei Zhangbe79d1b2021-06-07 19:21:05 +0000439 return ArgbToColorRef(type_argb_pair.argb);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440}
441
Lei Zhang2c495302021-10-07 23:13:30 +0000442absl::optional<FX_COLORREF> CPDFSDK_Widget::GetBorderColor() const {
Lei Zhangbe79d1b2021-06-07 19:21:05 +0000443 CFX_Color::TypeAndARGB type_argb_pair =
Lei Zhange9826282021-06-08 08:30:48 +0000444 GetFormControl()->GetColorARGB(pdfium::appearance::kBC);
Lei Zhangbe79d1b2021-06-07 19:21:05 +0000445 if (type_argb_pair.color_type == CFX_Color::Type::kTransparent)
Lei Zhanga3a739d2021-10-07 20:46:31 +0000446 return absl::nullopt;
Tom Sepez33baa9f2021-05-18 19:02:49 +0000447
Lei Zhangbe79d1b2021-06-07 19:21:05 +0000448 return ArgbToColorRef(type_argb_pair.argb);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449}
450
Lei Zhang2c495302021-10-07 23:13:30 +0000451absl::optional<FX_COLORREF> CPDFSDK_Widget::GetTextColor() const {
Tom Sepez33baa9f2021-05-18 19:02:49 +0000452 CPDF_DefaultAppearance da = GetFormControl()->GetDefaultAppearance();
Lei Zhang2c495302021-10-07 23:13:30 +0000453 absl::optional<CFX_Color::TypeAndARGB> maybe_type_argb_pair =
454 da.GetColorARGB();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455
Tom Sepez33baa9f2021-05-18 19:02:49 +0000456 if (!maybe_type_argb_pair.has_value())
Lei Zhanga3a739d2021-10-07 20:46:31 +0000457 return absl::nullopt;
Tom Sepez33baa9f2021-05-18 19:02:49 +0000458
Lei Zhangbe79d1b2021-06-07 19:21:05 +0000459 if (maybe_type_argb_pair.value().color_type == CFX_Color::Type::kTransparent)
Lei Zhanga3a739d2021-10-07 20:46:31 +0000460 return absl::nullopt;
Tom Sepez33baa9f2021-05-18 19:02:49 +0000461
Lei Zhangbe79d1b2021-06-07 19:21:05 +0000462 return ArgbToColorRef(maybe_type_argb_pair.value().argb);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463}
464
Dan Sinclair05df0752017-03-14 14:43:42 -0400465float CPDFSDK_Widget::GetFontSize() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 CPDF_DefaultAppearance pDa = pFormCtrl->GetDefaultAppearance();
Tom Sepezc4a2b752017-04-07 13:56:13 -0700468 float fFontSize;
469 pDa.GetFont(&fFontSize);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 return fFontSize;
471}
472
Tom Sepezbf59a072015-10-21 14:07:23 -0700473int CPDFSDK_Widget::GetSelectedIndex(int nIndex) const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700474#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -0700475 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Dan Sinclaird7d584d2018-01-22 18:41:36 +0000476 CXFA_Node* node = hWidget->GetNode();
477 if (node->IsWidgetReady()) {
Dan Sinclair414cc9c2018-01-23 21:17:43 +0000478 if (nIndex < node->CountSelectedItems())
479 return node->GetSelectedItem(nIndex);
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700480 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700482#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 return pFormField->GetSelectedIndex(nIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700485}
486
Tom Sepez5f032992018-06-13 16:14:26 +0000487WideString CPDFSDK_Widget::GetValue() const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700488#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -0700489 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Dan Sinclaird7d584d2018-01-22 18:41:36 +0000490 CXFA_Node* node = hWidget->GetNode();
Tom Sepez5f032992018-06-13 16:14:26 +0000491 if (node->IsWidgetReady())
Tom Sepez4fd3f632021-05-21 00:15:00 +0000492 return node->GetValue(XFA_ValuePicture::kDisplay);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700493 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700494#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 return pFormField->GetValue();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700497}
498
Mansi Awasthi23bba0e2020-05-15 12:18:25 +0000499WideString CPDFSDK_Widget::GetExportValue() const {
500 CPDF_FormControl* pFormCtrl = GetFormControl();
501 return pFormCtrl->GetExportValue();
502}
503
Ryan Harrison275e2602017-09-18 14:23:18 -0400504WideString CPDFSDK_Widget::GetOptionLabel(int nIndex) const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506 return pFormField->GetOptionLabel(nIndex);
507}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700508
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509int CPDFSDK_Widget::CountOptions() const {
510 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700511 return pFormField->CountOptions();
512}
513
tsepez4cf55152016-11-02 14:37:54 -0700514bool CPDFSDK_Widget::IsOptionSelected(int nIndex) const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700515#ifdef PDF_ENABLE_XFA
dsinclairdf4bc592016-03-31 20:34:43 -0700516 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Dan Sinclaird7d584d2018-01-22 18:41:36 +0000517 CXFA_Node* node = hWidget->GetNode();
518 if (node->IsWidgetReady()) {
Dan Sinclair414cc9c2018-01-23 21:17:43 +0000519 if (nIndex > -1 && nIndex < node->CountChoiceListItems(false))
520 return node->GetItemState(nIndex);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521
tsepez4cf55152016-11-02 14:37:54 -0700522 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523 }
524 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700525#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700526 CPDF_FormField* pFormField = GetFormField();
527 return pFormField->IsItemSelected(nIndex);
528}
529
530int CPDFSDK_Widget::GetTopVisibleIndex() const {
531 CPDF_FormField* pFormField = GetFormField();
532 return pFormField->GetTopVisibleIndex();
533}
534
Wei Li97da9762016-03-11 17:00:48 -0800535bool CPDFSDK_Widget::IsChecked() const {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700536#ifdef PDF_ENABLE_XFA
dsinclair221caf62016-04-04 12:08:40 -0700537 if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
Dan Sinclaird7d584d2018-01-22 18:41:36 +0000538 CXFA_Node* node = hWidget->GetNode();
539 if (node->IsWidgetReady())
Tom Sepez4fd3f632021-05-21 00:15:00 +0000540 return node->GetCheckState() == XFA_CheckState::kOn;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700542#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544 return pFormCtrl->IsChecked();
545}
546
547int CPDFSDK_Widget::GetAlignment() const {
548 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549 return pFormCtrl->GetControlAlignment();
550}
551
552int CPDFSDK_Widget::GetMaxLen() const {
553 CPDF_FormField* pFormField = GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554 return pFormField->GetMaxLen();
555}
556
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000557void CPDFSDK_Widget::SetCheck(bool bChecked) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700558 CPDF_FormControl* pFormCtrl = GetFormControl();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559 CPDF_FormField* pFormField = pFormCtrl->GetField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560 pFormField->CheckControl(pFormField->GetControlIndex(pFormCtrl), bChecked,
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000561 NotificationOption::kDoNotNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800562#ifdef PDF_ENABLE_XFA
Tom Sepez90b07c22021-08-26 20:35:34 +0000563 if (!IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode::kNormal))
Lei Zhangcaf0bb82021-06-07 18:41:25 +0000564 ResetXFAAppearance(CPDFSDK_Widget::kValueChanged);
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000565 Synchronize(true);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800566#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700567}
568
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000569void CPDFSDK_Widget::SetValue(const WideString& sValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700570 CPDF_FormField* pFormField = GetFormField();
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000571 pFormField->SetValue(sValue, NotificationOption::kDoNotNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800572#ifdef PDF_ENABLE_XFA
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000573 Synchronize(true);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800574#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575}
576
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000577void CPDFSDK_Widget::SetOptionSelection(int index) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700578 CPDF_FormField* pFormField = GetFormField();
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000579 pFormField->SetItemSelection(index, NotificationOption::kDoNotNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800580#ifdef PDF_ENABLE_XFA
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000581 Synchronize(true);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800582#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583}
584
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000585void CPDFSDK_Widget::ClearSelection() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 CPDF_FormField* pFormField = GetFormField();
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000587 pFormField->ClearSelection(NotificationOption::kDoNotNotify);
Tom Sepez51da0932015-11-25 16:05:49 -0800588#ifdef PDF_ENABLE_XFA
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000589 Synchronize(true);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800590#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591}
592
593void CPDFSDK_Widget::SetTopVisibleIndex(int index) {}
594
595void CPDFSDK_Widget::SetAppModified() {
tsepez4cf55152016-11-02 14:37:54 -0700596 m_bAppModified = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700597}
598
599void CPDFSDK_Widget::ClearAppModified() {
tsepez4cf55152016-11-02 14:37:54 -0700600 m_bAppModified = false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601}
602
tsepez4cf55152016-11-02 14:37:54 -0700603bool CPDFSDK_Widget::IsAppModified() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 return m_bAppModified;
605}
606
Tom Sepez51da0932015-11-25 16:05:49 -0800607#ifdef PDF_ENABLE_XFA
Lei Zhangcaf0bb82021-06-07 18:41:25 +0000608void CPDFSDK_Widget::ResetXFAAppearance(ValueChanged bValueChanged) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 switch (GetFieldType()) {
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000610 case FormFieldType::kTextField:
611 case FormFieldType::kComboBox: {
Lei Zhangcaf0bb82021-06-07 18:41:25 +0000612 ResetAppearance(OnFormat(), kValueChanged);
jaepark611adb82016-08-17 11:34:36 -0700613 break;
614 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700615 default:
Lei Zhanga3a739d2021-10-07 20:46:31 +0000616 ResetAppearance(absl::nullopt, kValueUnchanged);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 break;
618 }
619}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800620#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621
Lei Zhang2c495302021-10-07 23:13:30 +0000622void CPDFSDK_Widget::ResetAppearance(absl::optional<WideString> sValue,
Lei Zhangcaf0bb82021-06-07 18:41:25 +0000623 ValueChanged bValueChanged) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700624 SetAppModified();
625
Tom Sepez7b1e53c2017-09-15 15:43:11 -0700626 m_nAppearanceAge++;
Lei Zhangcaf0bb82021-06-07 18:41:25 +0000627 if (bValueChanged == kValueChanged)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700628 m_nValueAge++;
629
Tom Sepez6c62e0e2019-08-01 21:49:43 +0000630 CPDFSDK_AppStream appStream(this, GetAPDict());
Dan Sinclaircb2ea422017-07-19 15:24:49 -0400631 switch (GetFieldType()) {
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000632 case FormFieldType::kPushButton:
Dan Sinclaircb2ea422017-07-19 15:24:49 -0400633 appStream.SetAsPushButton();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 break;
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000635 case FormFieldType::kCheckBox:
Dan Sinclaircb2ea422017-07-19 15:24:49 -0400636 appStream.SetAsCheckBox();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637 break;
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000638 case FormFieldType::kRadioButton:
Dan Sinclaircb2ea422017-07-19 15:24:49 -0400639 appStream.SetAsRadioButton();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 break;
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000641 case FormFieldType::kComboBox:
Dan Sinclaircb2ea422017-07-19 15:24:49 -0400642 appStream.SetAsComboBox(sValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 break;
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000644 case FormFieldType::kListBox:
Dan Sinclaircb2ea422017-07-19 15:24:49 -0400645 appStream.SetAsListBox();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646 break;
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000647 case FormFieldType::kTextField:
Dan Sinclaircb2ea422017-07-19 15:24:49 -0400648 appStream.SetAsTextField(sValue);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700649 break;
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000650 default:
651 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652 }
653
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700654 m_pAnnot->ClearCachedAP();
655}
656
Lei Zhang2c495302021-10-07 23:13:30 +0000657absl::optional<WideString> CPDFSDK_Widget::OnFormat() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700658 CPDF_FormField* pFormField = GetFormField();
Tom Sepez9c78c0d2021-01-27 20:13:14 +0000659 DCHECK(pFormField);
Lei Zhang073ecf42018-10-11 16:56:00 +0000660 return m_pInteractiveForm->OnFormat(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661}
662
Lei Zhang0cbfa4a2019-07-29 23:10:11 +0000663void CPDFSDK_Widget::ResetFieldAppearance() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700664 CPDF_FormField* pFormField = GetFormField();
Tom Sepez9c78c0d2021-01-27 20:13:14 +0000665 DCHECK(pFormField);
Lei Zhanga3a739d2021-10-07 20:46:31 +0000666 m_pInteractiveForm->ResetFieldAppearance(pFormField, absl::nullopt);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667}
668
669void CPDFSDK_Widget::DrawAppearance(CFX_RenderDevice* pDevice,
Lei Zhang8a449402017-08-17 15:07:47 -0700670 const CFX_Matrix& mtUser2Device,
Tom Sepezaa0b2b92021-10-28 18:38:22 +0000671 CPDF_Annot::AppearanceMode mode) {
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000672 FormFieldType fieldType = GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700673
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000674 if ((fieldType == FormFieldType::kCheckBox ||
675 fieldType == FormFieldType::kRadioButton) &&
Tom Sepez90b07c22021-08-26 20:35:34 +0000676 mode == CPDF_Annot::AppearanceMode::kNormal &&
677 !IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode::kNormal)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700678 CFX_GraphStateData gsd;
679 gsd.m_LineWidth = 0.0f;
680
Lei Zhang36865762021-06-12 01:07:37 +0000681 CFX_Path path;
682 path.AppendFloatRect(GetRect());
Tom Sepez0daab1a2021-10-26 19:59:32 +0000683 pDevice->DrawPath(path, &mtUser2Device, &gsd, 0, 0xFFAAAAAA,
Hui Yingstd47c4012020-07-07 20:24:05 +0000684 CFX_FillRenderOptions::EvenOddOptions());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700685 } else {
Tom Sepezaa0b2b92021-10-28 18:38:22 +0000686 CPDFSDK_BAAnnot::DrawAppearance(pDevice, mtUser2Device, mode);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687 }
688}
689
690void CPDFSDK_Widget::UpdateField() {
691 CPDF_FormField* pFormField = GetFormField();
Tom Sepez9c78c0d2021-01-27 20:13:14 +0000692 DCHECK(pFormField);
Lei Zhang073ecf42018-10-11 16:56:00 +0000693 m_pInteractiveForm->UpdateField(pFormField);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700694}
695
696void CPDFSDK_Widget::DrawShadow(CFX_RenderDevice* pDevice,
697 CPDFSDK_PageView* pPageView) {
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000698 FormFieldType fieldType = GetFieldType();
Lei Zhang073ecf42018-10-11 16:56:00 +0000699 if (!m_pInteractiveForm->IsNeedHighLight(fieldType))
jaepark611adb82016-08-17 11:34:36 -0700700 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700701
Lei Zhang831fa112018-12-19 20:30:14 +0000702 CFX_Matrix page2device = pPageView->GetCurrentMatrix();
Dan Sinclairafb44562017-02-09 13:07:43 -0500703 CFX_FloatRect rcDevice = GetRect();
Dan Sinclaira0061af2017-02-23 09:25:17 -0500704 CFX_PointF tmp =
705 page2device.Transform(CFX_PointF(rcDevice.left, rcDevice.bottom));
706 rcDevice.left = tmp.x;
707 rcDevice.bottom = tmp.y;
708
709 tmp = page2device.Transform(CFX_PointF(rcDevice.right, rcDevice.top));
710 rcDevice.right = tmp.x;
711 rcDevice.top = tmp.y;
jaepark611adb82016-08-17 11:34:36 -0700712 rcDevice.Normalize();
713
Lei Zhang073ecf42018-10-11 16:56:00 +0000714 pDevice->FillRect(
715 rcDevice.ToFxRect(),
716 AlphaAndColorRefToArgb(
717 static_cast<int>(m_pInteractiveForm->GetHighlightAlpha()),
718 m_pInteractiveForm->GetHighlightColor(fieldType)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719}
720
Tom Sepez281a9ea2016-02-26 14:24:28 -0800721CFX_FloatRect CPDFSDK_Widget::GetClientRect() const {
722 CFX_FloatRect rcWindow = GetRotatedRect();
Lei Zhang4f261ff2018-10-10 18:44:45 +0000723 float fBorderWidth = GetBorderWidth();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724 switch (GetBorderStyle()) {
Lei Zhang99f44ba2020-06-18 17:41:38 +0000725 case BorderStyle::kBeveled:
726 case BorderStyle::kInset:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727 fBorderWidth *= 2.0f;
728 break;
dsinclair92cb5e52016-05-16 11:38:28 -0700729 default:
730 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731 }
dan sinclaird6aff2f2017-07-13 21:36:29 -0400732 return rcWindow.GetDeflated(fBorderWidth, fBorderWidth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700733}
734
Tom Sepez281a9ea2016-02-26 14:24:28 -0800735CFX_FloatRect CPDFSDK_Widget::GetRotatedRect() const {
736 CFX_FloatRect rectAnnot = GetRect();
Lei Zhang98758892018-03-15 15:02:22 +0000737 float fWidth = rectAnnot.Width();
738 float fHeight = rectAnnot.Height();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700739
740 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez823e2522019-08-06 23:23:37 +0000741 CFX_FloatRect rcPWLWindow;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700742 switch (abs(pControl->GetRotation() % 360)) {
743 case 0:
744 case 180:
745 default:
Tom Sepez823e2522019-08-06 23:23:37 +0000746 rcPWLWindow = CFX_FloatRect(0, 0, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700747 break;
748 case 90:
749 case 270:
Tom Sepez823e2522019-08-06 23:23:37 +0000750 rcPWLWindow = CFX_FloatRect(0, 0, fHeight, fWidth);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751 break;
752 }
753
Tom Sepez823e2522019-08-06 23:23:37 +0000754 return rcPWLWindow;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700755}
756
Tom Sepez60d909e2015-12-10 15:34:55 -0800757CFX_Matrix CPDFSDK_Widget::GetMatrix() const {
758 CFX_Matrix mt;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759 CPDF_FormControl* pControl = GetFormControl();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800760 CFX_FloatRect rcAnnot = GetRect();
Lei Zhang98758892018-03-15 15:02:22 +0000761 float fWidth = rcAnnot.Width();
762 float fHeight = rcAnnot.Height();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763
764 switch (abs(pControl->GetRotation() % 360)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700765 default:
Lei Zhang0a913942017-06-01 01:37:51 -0700766 case 0:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767 break;
768 case 90:
Tom Sepez60d909e2015-12-10 15:34:55 -0800769 mt = CFX_Matrix(0, 1, -1, 0, fWidth, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770 break;
771 case 180:
Tom Sepez60d909e2015-12-10 15:34:55 -0800772 mt = CFX_Matrix(-1, 0, 0, -1, fWidth, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700773 break;
774 case 270:
Tom Sepez60d909e2015-12-10 15:34:55 -0800775 mt = CFX_Matrix(0, -1, 1, 0, 0, fHeight);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700776 break;
777 }
778
779 return mt;
Bo Xufdc00a72014-10-28 23:03:33 -0700780}
781
Dan Sinclair7f55a542017-07-13 14:17:10 -0400782CFX_Color CPDFSDK_Widget::GetTextPWLColor() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700783 CPDF_FormControl* pFormCtrl = GetFormControl();
Lei Zhang2c495302021-10-07 23:13:30 +0000784 absl::optional<CFX_Color> crText =
785 pFormCtrl->GetDefaultAppearance().GetColor();
Tom Sepez21085b82021-05-18 19:56:49 +0000786 return crText.value_or(CFX_Color(CFX_Color::Type::kGray, 0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700787}
788
Dan Sinclair7f55a542017-07-13 14:17:10 -0400789CFX_Color CPDFSDK_Widget::GetBorderPWLColor() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700790 CPDF_FormControl* pFormCtrl = GetFormControl();
Tom Sepezfa1da412021-05-18 15:42:23 +0000791 return pFormCtrl->GetOriginalBorderColor();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700792}
793
Dan Sinclair7f55a542017-07-13 14:17:10 -0400794CFX_Color CPDFSDK_Widget::GetFillPWLColor() const {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700795 CPDF_FormControl* pFormCtrl = GetFormControl();
Tom Sepezfa1da412021-05-18 15:42:23 +0000796 return pFormCtrl->GetOriginalBackgroundColor();
Bo Xufdc00a72014-10-28 23:03:33 -0700797}
798
tsepez4cf55152016-11-02 14:37:54 -0700799bool CPDFSDK_Widget::OnAAction(CPDF_AAction::AActionType type,
Tom Sepezc6b91df2021-09-11 02:14:19 +0000800 CFFL_FieldAction* data,
Lei Zhang2a4010e2021-06-07 19:18:25 +0000801 const CPDFSDK_PageView* pPageView) {
dsinclairb402b172016-10-11 09:26:32 -0700802 CPDFSDK_FormFillEnvironment* pFormFillEnv = pPageView->GetFormFillEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700803
Tom Sepez40e9ff32015-11-30 12:39:54 -0800804#ifdef PDF_ENABLE_XFA
Tom Sepezbe0591a2021-04-16 18:59:33 +0000805 if (HandleXFAAAction(type, data, pFormFillEnv))
806 return true;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800807#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700808
809 CPDF_Action action = GetAAction(type);
Lei Zhang7f195a12021-04-13 18:56:36 +0000810 if (action.GetType() != CPDF_Action::Type::kUnknown) {
Lei Zhang7db136a2018-10-10 21:34:17 +0000811 pFormFillEnv->GetActionHandler()->DoAction_Field(action, type, pFormFillEnv,
812 GetFormField(), data);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700813 }
tsepez4cf55152016-11-02 14:37:54 -0700814 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815}
816
817CPDF_Action CPDFSDK_Widget::GetAAction(CPDF_AAction::AActionType eAAT) {
818 switch (eAAT) {
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000819 case CPDF_AAction::kCursorEnter:
820 case CPDF_AAction::kCursorExit:
821 case CPDF_AAction::kButtonDown:
822 case CPDF_AAction::kButtonUp:
823 case CPDF_AAction::kGetFocus:
824 case CPDF_AAction::kLoseFocus:
825 case CPDF_AAction::kPageOpen:
826 case CPDF_AAction::kPageClose:
827 case CPDF_AAction::kPageVisible:
828 case CPDF_AAction::kPageInvisible:
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829 return CPDFSDK_BAAnnot::GetAAction(eAAT);
830
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000831 case CPDF_AAction::kKeyStroke:
832 case CPDF_AAction::kFormat:
833 case CPDF_AAction::kValidate:
834 case CPDF_AAction::kCalculate: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835 CPDF_FormField* pField = GetFormField();
Wei Li0fc6b252016-03-01 16:29:41 -0800836 if (pField->GetAdditionalAction().GetDict())
837 return pField->GetAdditionalAction().GetAction(eAAT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700838 return CPDFSDK_BAAnnot::GetAAction(eAAT);
839 }
840 default:
841 break;
842 }
843
Lei Zhang4ecf9a82017-12-11 14:34:58 +0000844 return CPDF_Action(nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845}