1 // Copyright 2016 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.
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 "core/fpdfdoc/cpdf_aaction.h"
11 #include "core/fpdfdoc/cpdf_action.h"
12 #include "core/fpdfdoc/cpdf_annot.h"
13 #include "core/fpdfdoc/cpdf_formfield.h"
14 #include "core/fxcrt/fx_coordinates.h"
15 #include "core/fxcrt/fx_string.h"
16 #include "core/fxcrt/unowned_ptr.h"
17 #include "core/fxge/cfx_color.h"
18 #include "fpdfsdk/cpdfsdk_baannot.h"
19 #include "third_party/base/optional.h"
20 
21 class CFX_RenderDevice;
22 class CPDF_Annot;
23 class CPDF_Dictionary;
24 class CPDF_FormControl;
25 class CPDF_FormField;
26 class CPDF_RenderOptions;
27 class CPDF_Stream;
28 class CPDFSDK_InteractiveForm;
29 class CPDFSDK_PageView;
30 struct CPDFSDK_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 #ifdef PDF_ENABLE_XFA
47   CXFA_FFWidget* GetMixXFAWidget() const;
48   CXFA_FFWidgetHandler* GetXFAWidgetHandler() const;
49 
50   bool HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT) const;
51   bool OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
52                     CPDFSDK_FieldAction* data,
53                     CPDFSDK_PageView* pPageView);
54 
55   void Synchronize(bool bSynchronizeElse);
56 #endif  // PDF_ENABLE_XFA
57 
58   CPDFSDK_Widget(CPDF_Annot* pAnnot,
59                  CPDFSDK_PageView* pPageView,
60                  CPDFSDK_InteractiveForm* pInteractiveForm);
61   ~CPDFSDK_Widget() override;
62 
63   bool IsSignatureWidget() const override;
64   CPDF_Action GetAAction(CPDF_AAction::AActionType eAAT) override;
65   bool IsAppearanceValid() override;
66 
67   int GetLayoutOrder() const override;
68 
69   FormFieldType GetFieldType() const;
70   int GetFieldFlags() const;
71   int GetRotate() const;
72 
73   Optional<FX_COLORREF> GetFillColor() const;
74   Optional<FX_COLORREF> GetBorderColor() const;
75   Optional<FX_COLORREF> GetTextColor() const;
76   float GetFontSize() const;
77 
78   int GetSelectedIndex(int nIndex) const;
79   WideString GetValue() const;
80   WideString GetDefaultValue() const;
81   WideString GetOptionLabel(int nIndex) const;
82   int CountOptions() const;
83   bool IsOptionSelected(int nIndex) const;
84   int GetTopVisibleIndex() const;
85   bool IsChecked() const;
86   int GetAlignment() const;
87   int GetMaxLen() const;
88   WideString GetAlternateName() const;
89 
90   void SetCheck(bool bChecked, NotificationOption notify);
91   void SetValue(const WideString& sValue, NotificationOption notify);
92   void SetOptionSelection(int index, bool bSelected, NotificationOption notify);
93   void ClearSelection(NotificationOption notify);
94   void SetTopVisibleIndex(int index);
95 
96 #ifdef PDF_ENABLE_XFA
97   // TODO(thestig): Figure out if the parameter should be used or removed.
98   void ResetXFAAppearance(bool bValueChanged);
99 #endif  // PDF_ENABLE_XFA
100   void ResetAppearance(Optional<WideString> sValue, bool bValueChanged);
101   void ResetFieldAppearance();
102   void UpdateField();
103   Optional<WideString> OnFormat();
104 
105   bool OnAAction(CPDF_AAction::AActionType type,
106                  CPDFSDK_FieldAction* data,
107                  CPDFSDK_PageView* pPageView);
108 
GetInteractiveForm()109   CPDFSDK_InteractiveForm* GetInteractiveForm() const {
110     return m_pInteractiveForm.Get();
111   }
112   CPDF_FormField* GetFormField() const;
113   CPDF_FormControl* GetFormControl() const;
114 
115   void DrawShadow(CFX_RenderDevice* pDevice, CPDFSDK_PageView* pPageView);
116 
117   void SetAppModified();
118   void ClearAppModified();
119   bool IsAppModified() const;
120 
GetAppearanceAge()121   uint32_t GetAppearanceAge() const { return m_nAppearanceAge; }
GetValueAge()122   uint32_t GetValueAge() const { return m_nValueAge; }
123 
124   bool IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode mode);
125   void DrawAppearance(CFX_RenderDevice* pDevice,
126                       const CFX_Matrix& mtUser2Device,
127                       CPDF_Annot::AppearanceMode mode,
128                       const CPDF_RenderOptions* pOptions) override;
129 
130   CFX_Matrix GetMatrix() const;
131   CFX_FloatRect GetClientRect() const;
132   CFX_FloatRect GetRotatedRect() const;
133   CFX_Color GetTextPWLColor() const;
134   CFX_Color GetBorderPWLColor() const;
135   CFX_Color GetFillPWLColor() const;
136 
137  private:
138 #ifdef PDF_ENABLE_XFA
139   CXFA_FFWidget* GetGroupMixXFAWidget() const;
140   WideString GetName() const;
141 #endif  // PDF_ENABLE_XFA
142 
143   UnownedPtr<CPDFSDK_InteractiveForm> const m_pInteractiveForm;
144   bool m_bAppModified = false;
145   uint32_t m_nAppearanceAge = 0;
146   uint32_t m_nValueAge = 0;
147 
148 #ifdef PDF_ENABLE_XFA
149   mutable UnownedPtr<CXFA_FFWidgetHandler> m_pWidgetHandler;
150 #endif  // PDF_ENABLE_XFA
151 };
152 
ToCPDFSDKWidget(CPDFSDK_Annot * pAnnot)153 inline CPDFSDK_Widget* ToCPDFSDKWidget(CPDFSDK_Annot* pAnnot) {
154   return pAnnot && pAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET
155              ? static_cast<CPDFSDK_Widget*>(pAnnot)
156              : nullptr;
157 }
158 
159 #endif  // FPDFSDK_CPDFSDK_WIDGET_H_
160