1 // Copyright 2014 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 XFA_FXFA_CXFA_FFWIDGET_H_
8 #define XFA_FXFA_CXFA_FFWIDGET_H_
9
10 #include <vector>
11
12 #include "core/fpdfdoc/cpdf_formfield.h"
13 #include "core/fxcodec/fx_codec_def.h"
14 #include "core/fxcrt/observed_ptr.h"
15 #include "core/fxge/cfx_graphstatedata.h"
16 #include "xfa/fwl/cfwl_app.h"
17 #include "xfa/fwl/cfwl_messagemouse.h"
18 #include "xfa/fwl/cfwl_widget.h"
19 #include "xfa/fxfa/cxfa_eventparam.h"
20 #include "xfa/fxfa/fxfa.h"
21 #include "xfa/fxfa/layout/cxfa_contentlayoutitem.h"
22
23 class CFX_DIBitmap;
24 class CXFA_Box;
25 class CXFA_FFApp;
26 class CXFA_FFDoc;
27 class CXFA_FFDocView;
28 class CXFA_FFField;
29 class CXFA_FFPageView;
30 class CXFA_FFWidgetHandler;
31 class CXFA_Graphics;
32 class CXFA_Image;
33 class CXFA_Margin;
34 enum class FWL_WidgetHit;
35
XFA_UnitPx2Pt(float fPx,float fDpi)36 inline float XFA_UnitPx2Pt(float fPx, float fDpi) {
37 return fPx * 72.0f / fDpi;
38 }
39
40 constexpr float kXFAWidgetPrecision = 0.001f;
41
42 void XFA_DrawImage(CXFA_Graphics* pGS,
43 const CFX_RectF& rtImage,
44 const CFX_Matrix& matrix,
45 const RetainPtr<CFX_DIBitmap>& pDIBitmap,
46 XFA_AttributeValue iAspect,
47 const CFX_Size& dpi,
48 XFA_AttributeValue iHorzAlign = XFA_AttributeValue::Left,
49 XFA_AttributeValue iVertAlign = XFA_AttributeValue::Top);
50
51 RetainPtr<CFX_DIBitmap> XFA_LoadImageFromBuffer(
52 const RetainPtr<IFX_SeekableReadStream>& pImageFileRead,
53 FXCODEC_IMAGE_TYPE type,
54 int32_t& iImageXDpi,
55 int32_t& iImageYDpi);
56
57 void XFA_RectWithoutMargin(CFX_RectF* rt, const CXFA_Margin* margin);
58 CXFA_FFWidget* XFA_GetWidgetFromLayoutItem(CXFA_LayoutItem* pLayoutItem);
59
60 class CXFA_CalcData {
61 public:
62 CXFA_CalcData();
63 ~CXFA_CalcData();
64
65 std::vector<CXFA_Node*> m_Globals;
66 };
67
68 class CXFA_FFWidget : public Observable, public CFWL_Widget::AdapterIface {
69 public:
70 enum FocusOption { kDoNotDrawFocus = 0, kDrawFocus };
71 enum HighlightOption { kNoHighlight = 0, kHighlight };
72
73 explicit CXFA_FFWidget(CXFA_Node* pNode);
74 ~CXFA_FFWidget() override;
75
76 // CFWL_Widget::AdapterIface:
77 CFX_Matrix GetRotateMatrix() override;
78 void DisplayCaret(bool bVisible, const CFX_RectF* pRtAnchor) override;
79 void GetBorderColorAndThickness(FX_ARGB* cr, float* fWidth) override;
80
81 virtual CXFA_FFField* AsField();
82
83 virtual CFX_RectF GetBBox(FocusOption focus);
84 virtual void RenderWidget(CXFA_Graphics* pGS,
85 const CFX_Matrix& matrix,
86 HighlightOption highlight);
87 virtual bool IsLoaded();
88 virtual bool LoadWidget();
89 virtual bool PerformLayout();
90 virtual bool UpdateFWLData();
91 virtual void UpdateWidgetProperty();
92 // |command| must be LeftButtonDown or RightButtonDown.
93 virtual bool AcceptsFocusOnButtonDown(uint32_t dwFlags,
94 const CFX_PointF& point,
95 FWL_MouseCommand command);
96
97 // Caution: Returning false from an On* method may mean |this| is destroyed.
98 virtual bool OnMouseEnter() WARN_UNUSED_RESULT;
99 virtual bool OnMouseExit() WARN_UNUSED_RESULT;
100 virtual bool OnLButtonDown(uint32_t dwFlags,
101 const CFX_PointF& point) WARN_UNUSED_RESULT;
102 virtual bool OnLButtonUp(uint32_t dwFlags,
103 const CFX_PointF& point) WARN_UNUSED_RESULT;
104 virtual bool OnLButtonDblClk(uint32_t dwFlags,
105 const CFX_PointF& point) WARN_UNUSED_RESULT;
106 virtual bool OnMouseMove(uint32_t dwFlags,
107 const CFX_PointF& point) WARN_UNUSED_RESULT;
108 virtual bool OnMouseWheel(uint32_t dwFlags,
109 int16_t zDelta,
110 const CFX_PointF& point) WARN_UNUSED_RESULT;
111 virtual bool OnRButtonDown(uint32_t dwFlags,
112 const CFX_PointF& point) WARN_UNUSED_RESULT;
113 virtual bool OnRButtonUp(uint32_t dwFlags,
114 const CFX_PointF& point) WARN_UNUSED_RESULT;
115 virtual bool OnRButtonDblClk(uint32_t dwFlags,
116 const CFX_PointF& point) WARN_UNUSED_RESULT;
117 virtual bool OnSetFocus(CXFA_FFWidget* pOldWidget) WARN_UNUSED_RESULT;
118 virtual bool OnKillFocus(CXFA_FFWidget* pNewWidget) WARN_UNUSED_RESULT;
119 virtual bool OnKeyDown(uint32_t dwKeyCode,
120 uint32_t dwFlags) WARN_UNUSED_RESULT;
121 virtual bool OnKeyUp(uint32_t dwKeyCode, uint32_t dwFlags) WARN_UNUSED_RESULT;
122 virtual bool OnChar(uint32_t dwChar, uint32_t dwFlags) WARN_UNUSED_RESULT;
123 virtual bool OnSetCursor(const CFX_PointF& point) WARN_UNUSED_RESULT;
124
125 virtual FWL_WidgetHit HitTest(const CFX_PointF& point);
126 virtual bool CanUndo();
127 virtual bool CanRedo();
128 virtual bool Undo();
129 virtual bool Redo();
130 virtual bool CanCopy();
131 virtual bool CanCut();
132 virtual bool CanPaste();
133 virtual bool CanSelectAll();
134 virtual bool CanDelete();
135 virtual bool CanDeSelect();
136 virtual Optional<WideString> Copy();
137 virtual Optional<WideString> Cut();
138 virtual bool Paste(const WideString& wsPaste);
139 virtual void SelectAll();
140 virtual void Delete();
141 virtual void DeSelect();
142 virtual WideString GetText();
143 virtual FormFieldType GetFormFieldType();
144
GetNode()145 CXFA_Node* GetNode() const { return m_pNode.Get(); }
GetLayoutItem()146 CXFA_ContentLayoutItem* GetLayoutItem() const { return m_pLayoutItem.Get(); }
SetLayoutItem(CXFA_ContentLayoutItem * pItem)147 void SetLayoutItem(CXFA_ContentLayoutItem* pItem) { m_pLayoutItem = pItem; }
GetPageView()148 CXFA_FFPageView* GetPageView() const { return m_pPageView.Get(); }
SetPageView(CXFA_FFPageView * pPageView)149 void SetPageView(CXFA_FFPageView* pPageView) { m_pPageView = pPageView; }
GetDocView()150 CXFA_FFDocView* GetDocView() const { return m_pDocView.Get(); }
SetDocView(CXFA_FFDocView * pDocView)151 void SetDocView(CXFA_FFDocView* pDocView) { m_pDocView = pDocView; }
152
153 CXFA_FFWidget* GetNextFFWidget() const;
154 const CFX_RectF& GetWidgetRect() const;
155 const CFX_RectF& RecacheWidgetRect() const;
156 void ModifyStatus(uint32_t dwAdded, uint32_t dwRemoved);
157
158 CXFA_FFDoc* GetDoc();
159 CXFA_FFApp* GetApp();
160 IXFA_AppProvider* GetAppProvider();
161 void InvalidateRect();
IsFocused()162 bool IsFocused() const {
163 return GetLayoutItem()->TestStatusBits(XFA_WidgetStatus_Focused);
164 }
165 CFX_PointF Rotate2Normal(const CFX_PointF& point);
166 bool IsLayoutRectEmpty();
167 CXFA_LayoutItem* GetParent();
168 bool IsAncestorOf(CXFA_FFWidget* pWidget);
169 const CFWL_App* GetFWLApp();
170
171 bool HasEventUnderHandler(XFA_EVENTTYPE eEventType,
172 CXFA_FFWidgetHandler* pHandler);
173 bool ProcessEventUnderHandler(CXFA_EventParam* params,
174 CXFA_FFWidgetHandler* pHandler);
175
176 protected:
177 virtual bool PtInActiveRect(const CFX_PointF& point);
178
179 void DrawBorder(CXFA_Graphics* pGS,
180 CXFA_Box* box,
181 const CFX_RectF& rtBorder,
182 const CFX_Matrix& matrix);
183 void DrawBorderWithFlag(CXFA_Graphics* pGS,
184 CXFA_Box* box,
185 const CFX_RectF& rtBorder,
186 const CFX_Matrix& matrix,
187 bool forceRound);
188
189 CFX_RectF GetRectWithoutRotate();
190 bool HasVisibleStatus() const;
191 void EventKillFocus();
192 bool IsButtonDown();
193 void SetButtonDown(bool bSet);
194
195 UnownedPtr<CXFA_ContentLayoutItem> m_pLayoutItem;
196 UnownedPtr<CXFA_FFDocView> m_pDocView;
197 UnownedPtr<CXFA_FFPageView> m_pPageView;
198 UnownedPtr<CXFA_Node> const m_pNode;
199 mutable CFX_RectF m_rtWidget;
200 };
201
ToField(CXFA_FFWidget * widget)202 inline CXFA_FFField* ToField(CXFA_FFWidget* widget) {
203 return widget ? widget->AsField() : nullptr;
204 }
205
206 #endif // XFA_FXFA_CXFA_FFWIDGET_H_
207