blob: 31f2ff8b7761395e23431dc5aec2cec5e3fd5a82 [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2016 The PDFium Authors
jaepark611adb82016-08-17 11:34:36 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
dsinclair114e46a2016-09-29 17:18:21 -07007#ifndef FPDFSDK_CPDFSDK_WIDGET_H_
8#define FPDFSDK_CPDFSDK_WIDGET_H_
jaepark611adb82016-08-17 11:34:36 -07009
Lei Zhang24c6be62024-02-08 20:06:48 +000010#include <optional>
11
dsinclair1727aee2016-09-29 13:12:56 -070012#include "core/fpdfdoc/cpdf_aaction.h"
13#include "core/fpdfdoc/cpdf_action.h"
14#include "core/fpdfdoc/cpdf_annot.h"
Dan Sinclaira17a0e22018-03-28 21:10:35 +000015#include "core/fpdfdoc/cpdf_formfield.h"
dsinclaira52ab742016-09-29 13:59:29 -070016#include "core/fxcrt/fx_coordinates.h"
Dan Sinclairaee0db02017-09-21 16:53:58 -040017#include "core/fxcrt/unowned_ptr.h"
Tom Sepez1e151c62021-11-22 23:46:09 +000018#include "core/fxcrt/widestring.h"
Dan Sinclair7f55a542017-07-13 14:17:10 -040019#include "core/fxge/cfx_color.h"
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/cpdfsdk_baannot.h"
jaepark611adb82016-08-17 11:34:36 -070021
Lei Zhangf342da72022-04-06 23:54:50 +000022class CFFL_InteractiveFormFiller;
jaepark611adb82016-08-17 11:34:36 -070023class CFX_RenderDevice;
24class CPDF_Annot;
jaepark611adb82016-08-17 11:34:36 -070025class CPDF_FormControl;
26class CPDF_FormField;
Tom Sepezbe0591a2021-04-16 18:59:33 +000027class CPDFSDK_FormFillEnvironment;
Lei Zhang073ecf42018-10-11 16:56:00 +000028class CPDFSDK_InteractiveForm;
jaepark611adb82016-08-17 11:34:36 -070029class CPDFSDK_PageView;
Tom Sepezc6b91df2021-09-11 02:14:19 +000030struct CFFL_FieldAction;
jaepark611adb82016-08-17 11:34:36 -070031
32#ifdef PDF_ENABLE_XFA
33class CXFA_FFWidget;
34class CXFA_FFWidgetHandler;
Tom Sepezea38bf52020-01-15 23:11:12 +000035
36enum PDFSDK_XFAAActionType {
37 PDFSDK_XFA_Click = 0,
38 PDFSDK_XFA_Full,
39 PDFSDK_XFA_PreOpen,
40 PDFSDK_XFA_PostOpen
41};
jaepark611adb82016-08-17 11:34:36 -070042#endif // PDF_ENABLE_XFA
43
Tom Sepez55865452018-08-27 20:18:04 +000044class CPDFSDK_Widget final : public CPDFSDK_BAAnnot {
jaepark611adb82016-08-17 11:34:36 -070045 public:
Lei Zhangcaf0bb82021-06-07 18:41:25 +000046 enum ValueChanged : bool { kValueUnchanged = false, kValueChanged = true };
47
jaepark611adb82016-08-17 11:34:36 -070048 CPDFSDK_Widget(CPDF_Annot* pAnnot,
49 CPDFSDK_PageView* pPageView,
Lei Zhang073ecf42018-10-11 16:56:00 +000050 CPDFSDK_InteractiveForm* pInteractiveForm);
jaepark611adb82016-08-17 11:34:36 -070051 ~CPDFSDK_Widget() override;
52
Tom Sepez24193462020-02-03 18:01:29 +000053 // CPDFSDK_BAAnnot:
Lei Zhang5214eeb2022-04-04 23:37:28 +000054 void OnLoad() override;
jaepark611adb82016-08-17 11:34:36 -070055 CPDF_Action GetAAction(CPDF_AAction::AActionType eAAT) override;
tsepez4cf55152016-11-02 14:37:54 -070056 bool IsAppearanceValid() override;
jaepark611adb82016-08-17 11:34:36 -070057 int GetLayoutOrder() const override;
Lei Zhang2ea512b2022-04-08 21:10:01 +000058 void OnDraw(CFX_RenderDevice* pDevice,
59 const CFX_Matrix& mtUser2Device,
60 bool bDrawAnnots) override;
Lei Zhang37cef932022-04-05 18:23:29 +000061 bool DoHitTest(const CFX_PointF& point) override;
Lei Zhang23a042f2022-04-05 23:45:19 +000062 CFX_FloatRect GetViewBBox() override;
Lei Zhangf342da72022-04-06 23:54:50 +000063 bool CanUndo() override;
64 bool CanRedo() override;
65 bool Undo() override;
66 bool Redo() override;
Lei Zhang889fce72022-04-08 21:13:51 +000067 WideString GetText() override;
68 WideString GetSelectedText() override;
Keren Zhu74e7eb72023-01-19 19:21:41 +000069 void ReplaceAndKeepSelection(const WideString& text) override;
Lei Zhang889fce72022-04-08 21:13:51 +000070 void ReplaceSelection(const WideString& text) override;
71 bool SelectAllText() override;
72 bool SetIndexSelected(int index, bool selected) override;
73 bool IsIndexSelected(int index) override;
Tom Sepezaa0b2b92021-10-28 18:38:22 +000074 void DrawAppearance(CFX_RenderDevice* pDevice,
75 const CFX_Matrix& mtUser2Device,
76 CPDF_Annot::AppearanceMode mode) override;
jaepark611adb82016-08-17 11:34:36 -070077
Tom Sepez63150f12021-09-23 01:28:39 +000078 bool IsSignatureWidget() const;
Tom Sepezc1415542021-09-21 21:48:59 +000079 void SetRect(const CFX_FloatRect& rect);
Ryan Harrison9baf31f2018-01-12 18:36:30 +000080 FormFieldType GetFieldType() const;
jaepark611adb82016-08-17 11:34:36 -070081 int GetFieldFlags() const;
82 int GetRotate() const;
83
Lei Zhang24c6be62024-02-08 20:06:48 +000084 std::optional<FX_COLORREF> GetFillColor() const;
85 std::optional<FX_COLORREF> GetBorderColor() const;
86 std::optional<FX_COLORREF> GetTextColor() const;
Dan Sinclair05df0752017-03-14 14:43:42 -040087 float GetFontSize() const;
jaepark611adb82016-08-17 11:34:36 -070088
89 int GetSelectedIndex(int nIndex) const;
Tom Sepezca0273392018-05-25 23:37:50 +000090 WideString GetValue() const;
Mansi Awasthi23bba0e2020-05-15 12:18:25 +000091 WideString GetExportValue() const;
Ryan Harrison275e2602017-09-18 14:23:18 -040092 WideString GetOptionLabel(int nIndex) const;
Tom Sepezcc90b0c2022-10-27 19:28:22 +000093 WideString GetSelectExportText(int nIndex) const;
94
jaepark611adb82016-08-17 11:34:36 -070095 int CountOptions() const;
tsepez4cf55152016-11-02 14:37:54 -070096 bool IsOptionSelected(int nIndex) const;
jaepark611adb82016-08-17 11:34:36 -070097 int GetTopVisibleIndex() const;
98 bool IsChecked() const;
99 int GetAlignment() const;
100 int GetMaxLen() const;
jaepark611adb82016-08-17 11:34:36 -0700101
Tom Sepez6d6bdc32021-04-15 22:05:16 +0000102 void SetCheck(bool bChecked);
103 void SetValue(const WideString& sValue);
104 void SetOptionSelection(int index);
105 void ClearSelection();
jaepark611adb82016-08-17 11:34:36 -0700106 void SetTopVisibleIndex(int index);
107
108#ifdef PDF_ENABLE_XFA
Tom Sepez24193462020-02-03 18:01:29 +0000109 CXFA_FFWidget* GetMixXFAWidget() const;
110 bool HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT) const;
111 bool OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
Tom Sepezc6b91df2021-09-11 02:14:19 +0000112 CFFL_FieldAction* data,
Lei Zhang2a4010e2021-06-07 19:18:25 +0000113 const CPDFSDK_PageView* pPageView);
Tom Sepez24193462020-02-03 18:01:29 +0000114 void Synchronize(bool bSynchronizeElse);
Lei Zhange858d1f2019-07-29 23:43:41 +0000115 // TODO(thestig): Figure out if the parameter should be used or removed.
Lei Zhangcaf0bb82021-06-07 18:41:25 +0000116 void ResetXFAAppearance(ValueChanged bValueChanged);
jaepark611adb82016-08-17 11:34:36 -0700117#endif // PDF_ENABLE_XFA
Tom Sepez24193462020-02-03 18:01:29 +0000118
Lei Zhang24c6be62024-02-08 20:06:48 +0000119 void ResetAppearance(std::optional<WideString> sValue,
Lei Zhang2c495302021-10-07 23:13:30 +0000120 ValueChanged bValueChanged);
Lei Zhang0cbfa4a2019-07-29 23:10:11 +0000121 void ResetFieldAppearance();
jaepark611adb82016-08-17 11:34:36 -0700122 void UpdateField();
Lei Zhang24c6be62024-02-08 20:06:48 +0000123 std::optional<WideString> OnFormat();
jaepark611adb82016-08-17 11:34:36 -0700124
tsepez4cf55152016-11-02 14:37:54 -0700125 bool OnAAction(CPDF_AAction::AActionType type,
Tom Sepezc6b91df2021-09-11 02:14:19 +0000126 CFFL_FieldAction* data,
Lei Zhang2a4010e2021-06-07 19:18:25 +0000127 const CPDFSDK_PageView* pPageView);
jaepark611adb82016-08-17 11:34:36 -0700128
jaepark611adb82016-08-17 11:34:36 -0700129 CPDF_FormField* GetFormField() const;
130 CPDF_FormControl* GetFormControl() const;
jaepark611adb82016-08-17 11:34:36 -0700131
132 void DrawShadow(CFX_RenderDevice* pDevice, CPDFSDK_PageView* pPageView);
133
134 void SetAppModified();
135 void ClearAppModified();
tsepez4cf55152016-11-02 14:37:54 -0700136 bool IsAppModified() const;
jaepark611adb82016-08-17 11:34:36 -0700137
Tom Sepez7b1e53c2017-09-15 15:43:11 -0700138 uint32_t GetAppearanceAge() const { return m_nAppearanceAge; }
139 uint32_t GetValueAge() const { return m_nValueAge; }
jaepark611adb82016-08-17 11:34:36 -0700140
Tom Sepeza5bd2fa2022-10-25 17:54:27 +0000141 bool IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode mode) const;
142 bool IsPushHighlighted() const;
Dan Sinclaircb2ea422017-07-19 15:24:49 -0400143 CFX_Matrix GetMatrix() const;
jaepark611adb82016-08-17 11:34:36 -0700144 CFX_FloatRect GetClientRect() const;
145 CFX_FloatRect GetRotatedRect() const;
Dan Sinclair7f55a542017-07-13 14:17:10 -0400146 CFX_Color GetTextPWLColor() const;
147 CFX_Color GetBorderPWLColor() const;
148 CFX_Color GetFillPWLColor() const;
jaepark611adb82016-08-17 11:34:36 -0700149
Dan Sinclaircb2ea422017-07-19 15:24:49 -0400150 private:
Lei Zhang76ef4c92022-04-08 19:03:51 +0000151 // CPDFSDK_Annot::UnsafeInputHandlers:
152 void OnMouseEnter(Mask<FWL_EVENTFLAG> nFlags) override;
153 void OnMouseExit(Mask<FWL_EVENTFLAG> nFlags) override;
154 bool OnLButtonDown(Mask<FWL_EVENTFLAG> nFlags,
155 const CFX_PointF& point) override;
156 bool OnLButtonUp(Mask<FWL_EVENTFLAG> nFlags,
157 const CFX_PointF& point) override;
158 bool OnLButtonDblClk(Mask<FWL_EVENTFLAG> nFlags,
159 const CFX_PointF& point) override;
160 bool OnMouseMove(Mask<FWL_EVENTFLAG> nFlags,
161 const CFX_PointF& point) override;
162 bool OnMouseWheel(Mask<FWL_EVENTFLAG> nFlags,
163 const CFX_PointF& point,
164 const CFX_Vector& delta) override;
165 bool OnRButtonDown(Mask<FWL_EVENTFLAG> nFlags,
166 const CFX_PointF& point) override;
167 bool OnRButtonUp(Mask<FWL_EVENTFLAG> nFlags,
168 const CFX_PointF& point) override;
Lei Zhang2ea512b2022-04-08 21:10:01 +0000169 bool OnChar(uint32_t nChar, Mask<FWL_EVENTFLAG> nFlags) override;
170 bool OnKeyDown(FWL_VKEYCODE nKeyCode, Mask<FWL_EVENTFLAG> nFlags) override;
171 bool OnSetFocus(Mask<FWL_EVENTFLAG> nFlags) override;
172 bool OnKillFocus(Mask<FWL_EVENTFLAG> nFlags) override;
Lei Zhang76ef4c92022-04-08 19:03:51 +0000173
Lei Zhangf342da72022-04-06 23:54:50 +0000174 CFFL_InteractiveFormFiller* GetInteractiveFormFiller();
175
Dan Sinclair1917cdd2018-01-30 17:29:21 +0000176#ifdef PDF_ENABLE_XFA
Tom Sepez24193462020-02-03 18:01:29 +0000177 CXFA_FFWidgetHandler* GetXFAWidgetHandler() const;
Lei Zhangb353f8c2018-10-10 18:39:14 +0000178 CXFA_FFWidget* GetGroupMixXFAWidget() const;
Dan Sinclair1917cdd2018-01-30 17:29:21 +0000179 WideString GetName() const;
Tom Sepezbe0591a2021-04-16 18:59:33 +0000180 bool HandleXFAAAction(CPDF_AAction::AActionType type,
Tom Sepezc6b91df2021-09-11 02:14:19 +0000181 CFFL_FieldAction* data,
Tom Sepezbe0591a2021-04-16 18:59:33 +0000182 CPDFSDK_FormFillEnvironment* pFormFillEnv);
Dan Sinclair1917cdd2018-01-30 17:29:21 +0000183#endif // PDF_ENABLE_XFA
184
Lei Zhang073ecf42018-10-11 16:56:00 +0000185 UnownedPtr<CPDFSDK_InteractiveForm> const m_pInteractiveForm;
Lei Zhangc1bf9982018-10-09 19:14:15 +0000186 bool m_bAppModified = false;
187 uint32_t m_nAppearanceAge = 0;
188 uint32_t m_nValueAge = 0;
jaepark611adb82016-08-17 11:34:36 -0700189};
190
Tom Sepez11a6bec2018-07-25 23:25:55 +0000191inline CPDFSDK_Widget* ToCPDFSDKWidget(CPDFSDK_Annot* pAnnot) {
192 return pAnnot && pAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET
193 ? static_cast<CPDFSDK_Widget*>(pAnnot)
194 : nullptr;
195}
196
dsinclair114e46a2016-09-29 17:18:21 -0700197#endif // FPDFSDK_CPDFSDK_WIDGET_H_