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 <set>
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/fxcrt/fx_coordinates.h"
16 #include "core/fxcrt/fx_string.h"
17 #include "fpdfsdk/cpdfsdk_baannot.h"
18 #include "fpdfsdk/pdfsdk_fieldaction.h"
19 #include "fpdfsdk/pdfwindow/cpwl_color.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_InterForm;
29 class CPDFSDK_PageView;
30 
31 #ifdef PDF_ENABLE_XFA
32 class CXFA_FFWidget;
33 class CXFA_FFWidgetHandler;
34 #endif  // PDF_ENABLE_XFA
35 
36 class CPDFSDK_Widget : public CPDFSDK_BAAnnot {
37  public:
38 #ifdef PDF_ENABLE_XFA
39   CXFA_FFWidget* GetMixXFAWidget() const;
40   CXFA_FFWidget* GetGroupMixXFAWidget();
41   CXFA_FFWidgetHandler* GetXFAWidgetHandler() const;
42 
43   bool HasXFAAAction(PDFSDK_XFAAActionType eXFAAAT);
44   bool OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
45                     PDFSDK_FieldAction& data,
46                     CPDFSDK_PageView* pPageView);
47 
48   void Synchronize(bool bSynchronizeElse);
49   void SynchronizeXFAValue();
50   void SynchronizeXFAItems();
51 
52   static void SynchronizeXFAValue(CXFA_FFDocView* pXFADocView,
53                                   CXFA_FFWidget* hWidget,
54                                   CPDF_FormField* pFormField,
55                                   CPDF_FormControl* pFormControl);
56   static void SynchronizeXFAItems(CXFA_FFDocView* pXFADocView,
57                                   CXFA_FFWidget* hWidget,
58                                   CPDF_FormField* pFormField,
59                                   CPDF_FormControl* pFormControl);
60 #endif  // PDF_ENABLE_XFA
61 
62   CPDFSDK_Widget(CPDF_Annot* pAnnot,
63                  CPDFSDK_PageView* pPageView,
64                  CPDFSDK_InterForm* pInterForm);
65   ~CPDFSDK_Widget() override;
66 
67   bool IsSignatureWidget() const override;
68   CPDF_Action GetAAction(CPDF_AAction::AActionType eAAT) override;
69   bool IsAppearanceValid() override;
70 
71   int GetLayoutOrder() const override;
72 
73   int GetFieldType() const;
74   int GetFieldFlags() const;
75   int GetRotate() const;
76 
77   bool GetFillColor(FX_COLORREF& color) const;
78   bool GetBorderColor(FX_COLORREF& color) const;
79   bool GetTextColor(FX_COLORREF& color) const;
80   FX_FLOAT GetFontSize() const;
81 
82   int GetSelectedIndex(int nIndex) const;
83 #ifndef PDF_ENABLE_XFA
84   CFX_WideString GetValue() const;
85 #else
86   CFX_WideString GetValue(bool bDisplay = true) const;
87 #endif  // PDF_ENABLE_XFA
88   CFX_WideString GetDefaultValue() const;
89   CFX_WideString GetOptionLabel(int nIndex) const;
90   int CountOptions() const;
91   bool IsOptionSelected(int nIndex) const;
92   int GetTopVisibleIndex() const;
93   bool IsChecked() const;
94   int GetAlignment() const;
95   int GetMaxLen() const;
96 #ifdef PDF_ENABLE_XFA
97   CFX_WideString GetName() const;
98 #endif  // PDF_ENABLE_XFA
99   CFX_WideString GetAlternateName() const;
100 
101   void SetCheck(bool bChecked, bool bNotify);
102   void SetValue(const CFX_WideString& sValue, bool bNotify);
103   void SetDefaultValue(const CFX_WideString& sValue);
104   void SetOptionSelection(int index, bool bSelected, bool bNotify);
105   void ClearSelection(bool bNotify);
106   void SetTopVisibleIndex(int index);
107 
108 #ifdef PDF_ENABLE_XFA
109   void ResetAppearance(bool bValueChanged);
110 #endif  // PDF_ENABLE_XFA
111   void ResetAppearance(const CFX_WideString* sValue, bool bValueChanged);
112   void ResetFieldAppearance(bool bValueChanged);
113   void UpdateField();
114   CFX_WideString OnFormat(bool& bFormatted);
115 
116   bool OnAAction(CPDF_AAction::AActionType type,
117                  PDFSDK_FieldAction& data,
118                  CPDFSDK_PageView* pPageView);
119 
GetInterForm()120   CPDFSDK_InterForm* GetInterForm() const { return m_pInterForm; }
121   CPDF_FormField* GetFormField() const;
122   CPDF_FormControl* GetFormControl() const;
123   static CPDF_FormControl* GetFormControl(CPDF_InterForm* pInterForm,
124                                           const CPDF_Dictionary* pAnnotDict);
125 
126   void DrawShadow(CFX_RenderDevice* pDevice, CPDFSDK_PageView* pPageView);
127 
128   void SetAppModified();
129   void ClearAppModified();
130   bool IsAppModified() const;
131 
132   int32_t GetAppearanceAge() const;
133   int32_t GetValueAge() const;
134 
135   bool IsWidgetAppearanceValid(CPDF_Annot::AppearanceMode mode);
136   void DrawAppearance(CFX_RenderDevice* pDevice,
137                       const CFX_Matrix* pUser2Device,
138                       CPDF_Annot::AppearanceMode mode,
139                       const CPDF_RenderOptions* pOptions) override;
140 
141  private:
142   void ResetAppearance_PushButton();
143   void ResetAppearance_CheckBox();
144   void ResetAppearance_RadioButton();
145   void ResetAppearance_ComboBox(const CFX_WideString* sValue);
146   void ResetAppearance_ListBox();
147   void ResetAppearance_TextField(const CFX_WideString* sValue);
148 
149   CFX_FloatRect GetClientRect() const;
150   CFX_FloatRect GetRotatedRect() const;
151 
152   CFX_ByteString GetBackgroundAppStream() const;
153   CFX_ByteString GetBorderAppStream() const;
154   CFX_Matrix GetMatrix() const;
155 
156   CPWL_Color GetTextPWLColor() const;
157   CPWL_Color GetBorderPWLColor() const;
158   CPWL_Color GetFillPWLColor() const;
159 
160   void AddImageToAppearance(const CFX_ByteString& sAPType, CPDF_Stream* pImage);
161   void RemoveAppearance(const CFX_ByteString& sAPType);
162 
163   CPDFSDK_InterForm* const m_pInterForm;
164   bool m_bAppModified;
165   int32_t m_nAppAge;
166   int32_t m_nValueAge;
167 
168 #ifdef PDF_ENABLE_XFA
169   mutable CXFA_FFWidget* m_hMixXFAWidget;
170   mutable CXFA_FFWidgetHandler* m_pWidgetHandler;
171 #endif  // PDF_ENABLE_XFA
172 };
173 
174 #endif  // FPDFSDK_CPDFSDK_WIDGET_H_
175