1 // Copyright 2016 The PDFium Authors
2 // 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
7 #ifndef FPDFSDK_CPDFSDK_WIDGET_H_
8 #define FPDFSDK_CPDFSDK_WIDGET_H_
9
10 #include <optional>
11
12 #include "core/fpdfdoc/cpdf_aaction.h"
13 #include "core/fpdfdoc/cpdf_action.h"
14 #include "core/fpdfdoc/cpdf_annot.h"
15 #include "core/fpdfdoc/cpdf_formfield.h"
16 #include "core/fxcrt/fx_coordinates.h"
17 #include "core/fxcrt/unowned_ptr.h"
18 #include "core/fxcrt/widestring.h"
19 #include "core/fxge/cfx_color.h"
20 #include "fpdfsdk/cpdfsdk_baannot.h"
21
22 class CFFL_InteractiveFormFiller;
23 class CFX_RenderDevice;
24 class CPDF_Annot;
25 class CPDF_FormControl;
26 class CPDF_FormField;
27 class CPDFSDK_FormFillEnvironment;
28 class CPDFSDK_InteractiveForm;
29 class CPDFSDK_PageView;
30 struct CFFL_FieldAction;
31
32 #ifdef PDF_ENABLE_XFA
33 class CXFA_FFWidget;
34 class CXFA_FFWidgetHandler;
35
36 enum PDFSDK_XFAAActionType {
37 PDFSDK_XFA_Click = 0,
38 PDFSDK_XFA_Full,
39 PDFSDK_XFA_PreOpen,
40 PDFSDK_XFA_PostOpen
41 };
42 #endif // PDF_ENABLE_XFA
43
44 class CPDFSDK_Widget final : public CPDFSDK_BAAnnot {
45 public:
46 enum ValueChanged : bool { kValueUnchanged = false, kValueChanged = true };
47
48 CPDFSDK_Widget(CPDF_Annot* pAnnot,
49 CPDFSDK_PageView* pPageView,
50 CPDFSDK_InteractiveForm* pInteractiveForm);
51 ~CPDFSDK_Widget() override;
52
53 // CPDFSDK_BAAnnot:
54 void OnLoad() override;
55 CPDF_Action GetAAction(CPDF_AAction::AActionType eAAT) override;
56 bool IsAppearanceValid() override;
57 int GetLayoutOrder() const override;
58 void OnDraw(CFX_RenderDevice* pDevice,
59 const CFX_Matrix& mtUser2Device,
60 bool bDrawAnnots) override;
61 bool DoHitTest(const CFX_PointF& point) override;
62 CFX_FloatRect GetViewBBox() override;
63 bool CanUndo() override;
64 bool CanRedo() override;
65 bool Undo() override;
66 bool Redo() override;
67 WideString GetText() override;
68 WideString GetSelectedText() override;
69 void ReplaceAndKeepSelection(const WideString& text) override;
70 void ReplaceSelection(const WideString& text) override;
71 bool SelectAllText() override;
72 bool SetIndexSelected(int index, bool selected) override;
73 bool IsIndexSelected(int index) override;
74 void DrawAppearance(CFX_RenderDevice* pDevice,
75 const CFX_Matrix& mtUser2Device,
76 CPDF_Annot::AppearanceMode mode) override;
77
78 bool IsSignatureWidget() const;
79 void SetRect(const CFX_FloatRect& rect);
80 FormFieldType GetFieldType() const;
81 int GetFieldFlags() const;
82 int GetRotate() const;
83
84 std::optional<FX_COLORREF> GetFillColor() const;
85 std::optional<FX_COLORREF> GetBorderColor() const;
86 std::optional<FX_COLORREF> GetTextColor() const;
87 float GetFontSize() const;
88
89 int GetSelectedIndex(int nIndex) const;
90 WideString GetValue() const;
91 WideString GetExportValue() const;
92 WideString GetOptionLabel(int nIndex) const;
93 WideString GetSelectExportText(int nIndex) const;
94
95 int CountOptions() const;
96 bool IsOptionSelected(int nIndex) const;
97 int GetTopVisibleIndex() const;
98 bool IsChecked() const;
99 int GetAlignment() const;
100 int GetMaxLen() const;
101
102 void SetCheck(bool bChecked);
103 void SetValue(const WideString& sValue);
104 void SetOptionSelection(int index);
105 void ClearSelection();
106 void SetTopVisibleIndex(int index);
107
108 #ifdef PDF_ENABLE_XFA
109 CXFA_FFWidget* GetMixXFAWidget() const;
110 bool HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT) const;
111 bool OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
112 CFFL_FieldAction* data,
113 const CPDFSDK_PageView* pPageView);
114 void Synchronize(bool bSynchronizeElse);
115 // TODO(thestig): Figure out if the parameter should be used or removed.
116 void ResetXFAAppearance(ValueChanged bValueChanged);
117 #endif // PDF_ENABLE_XFA
118
119 void ResetAppearance(std::optional<WideString> sValue,
120 ValueChanged bValueChanged);
121 void ResetFieldAppearance();
122 void UpdateField();
123 std::optional<WideString> OnFormat();
124
125 bool OnAAction(CPDF_AAction::AActionType type,
126 CFFL_FieldAction* data,
127 const CPDFSDK_PageView* pPageView);
128
129 CPDF_FormField* GetFormField() const;
130 CPDF_FormControl* GetFormControl() const;
131
132 void DrawShadow(CFX_RenderDevice* pDevice, CPDFSDK_PageView* pPageView);
133
134 void SetAppModified();
135 void ClearAppModified();
136 bool IsAppModified() const;
137
GetAppearanceAge()138 uint32_t GetAppearanceAge() const { return m_nAppearanceAge; }
GetValueAge()139 uint32_t GetValueAge() const { return m_nValueAge; }
140
141 bool IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode mode) const;
142 bool IsPushHighlighted() const;
143 CFX_Matrix GetMatrix() const;
144 CFX_FloatRect GetClientRect() const;
145 CFX_FloatRect GetRotatedRect() const;
146 CFX_Color GetTextPWLColor() const;
147 CFX_Color GetBorderPWLColor() const;
148 CFX_Color GetFillPWLColor() const;
149
150 private:
151 // 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;
169 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;
173
174 CFFL_InteractiveFormFiller* GetInteractiveFormFiller();
175
176 #ifdef PDF_ENABLE_XFA
177 CXFA_FFWidgetHandler* GetXFAWidgetHandler() const;
178 CXFA_FFWidget* GetGroupMixXFAWidget() const;
179 WideString GetName() const;
180 bool HandleXFAAAction(CPDF_AAction::AActionType type,
181 CFFL_FieldAction* data,
182 CPDFSDK_FormFillEnvironment* pFormFillEnv);
183 #endif // PDF_ENABLE_XFA
184
185 UnownedPtr<CPDFSDK_InteractiveForm> const m_pInteractiveForm;
186 bool m_bAppModified = false;
187 uint32_t m_nAppearanceAge = 0;
188 uint32_t m_nValueAge = 0;
189 };
190
ToCPDFSDKWidget(CPDFSDK_Annot * pAnnot)191 inline 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
197 #endif // FPDFSDK_CPDFSDK_WIDGET_H_
198