• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 XFA_FWL_CFWL_WIDGET_H_
8 #define XFA_FWL_CFWL_WIDGET_H_
9 
10 #include <stdint.h>
11 
12 #include "core/fxcrt/fx_coordinates.h"
13 #include "core/fxcrt/unowned_ptr.h"
14 #include "core/fxcrt/widestring.h"
15 #include "core/fxge/dib/fx_dib.h"
16 #include "fxjs/gc/heap.h"
17 #include "v8/include/cppgc/garbage-collected.h"
18 #include "v8/include/cppgc/macros.h"
19 #include "v8/include/cppgc/member.h"
20 #include "v8/include/cppgc/prefinalizer.h"
21 #include "xfa/fde/cfde_data.h"
22 #include "xfa/fwl/cfwl_themepart.h"
23 #include "xfa/fwl/cfwl_widgetmgr.h"
24 #include "xfa/fwl/fwl_widgethit.h"
25 #include "xfa/fwl/ifwl_widgetdelegate.h"
26 
27 namespace pdfium {
28 
29 class CFWL_App;
30 class CFWL_Event;
31 class CFWL_Widget;
32 class IFWL_ThemeProvider;
33 
34 #define FWL_STYLE_WGT_OverLapper 0
35 #define FWL_STYLE_WGT_Popup (1L << 0)
36 #define FWL_STYLE_WGT_Child (2L << 0)
37 #define FWL_STYLE_WGT_WindowTypeMask (3L << 0)
38 #define FWL_STYLE_WGT_Border (1L << 2)
39 #define FWL_STYLE_WGT_VScroll (1L << 11)
40 #define FWL_STYLE_WGT_Group (1L << 22)
41 #define FWL_STYLE_WGT_NoBackground (1L << 28)
42 
43 #define FWL_STATE_WGT_Disabled (1L << 2)
44 #define FWL_STATE_WGT_Focused (1L << 4)
45 #define FWL_STATE_WGT_Invisible (1L << 5)
46 #define FWL_STATE_WGT_MAX 6
47 
48 enum class FWL_Type {
49   Unknown = 0,
50 
51   Barcode,
52   Caret,
53   CheckBox,
54   ComboBox,
55   DateTimePicker,
56   Edit,
57   Form,
58   FormProxy,
59   ListBox,
60   MonthCalendar,
61   PictureBox,
62   PushButton,
63   ScrollBar,
64   SpinButton,
65   ToolTip
66 };
67 
68 // NOTE: CFWL_Widget serves as its own delegate until replaced at runtime.
69 class CFWL_Widget : public cppgc::GarbageCollected<CFWL_Widget>,
70                     public IFWL_WidgetDelegate {
71   CPPGC_USING_PRE_FINALIZER(CFWL_Widget, PreFinalize);
72 
73  public:
74   class AdapterIface : public cppgc::GarbageCollectedMixin {
75    public:
76     virtual ~AdapterIface() = default;
77     virtual CFX_Matrix GetRotateMatrix() = 0;
78     virtual void DisplayCaret(bool bVisible, const CFX_RectF* pRtAnchor) = 0;
79     virtual void GetBorderColorAndThickness(FX_ARGB* cr, float* fWidth) = 0;
80   };
81 
82   class Properties {
83    public:
84     uint32_t m_dwStyles = FWL_STYLE_WGT_Child;  // Mask of FWL_STYLE_*_*.
85     uint32_t m_dwStyleExts = 0;                 // Mask of FWL_STYLEEXT_*_*.
86     uint32_t m_dwStates = 0;                    // Mask of FWL_STATE_*_*.
87   };
88 
89   class ScopedUpdateLock {
90     CPPGC_STACK_ALLOCATED();  // Allow raw/unowned pointers.
91 
92    public:
93     explicit ScopedUpdateLock(CFWL_Widget* widget);
94     ~ScopedUpdateLock();
95 
96    private:
97     UnownedPtr<CFWL_Widget> const widget_;  // Ok, stack-only.
98   };
99 
100   CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED;
101   ~CFWL_Widget() override;
102 
103   virtual void PreFinalize();
104   void Trace(cppgc::Visitor* visitor) const override;
105 
106   virtual FWL_Type GetClassID() const = 0;
107   virtual bool IsForm() const;
108   virtual CFX_RectF GetAutosizedWidgetRect();
109   virtual CFX_RectF GetWidgetRect();
110   virtual CFX_RectF GetClientRect();
111   virtual void ModifyStyleExts(uint32_t dwStyleExtsAdded,
112                                uint32_t dwStyleExtsRemoved);
113   virtual void SetStates(uint32_t dwStates);
114   virtual void RemoveStates(uint32_t dwStates);
115   virtual void Update() = 0;
116   virtual FWL_WidgetHit HitTest(const CFX_PointF& point);
117   virtual void DrawWidget(CFGAS_GEGraphics* pGraphics,
118                           const CFX_Matrix& matrix) = 0;
119 
120   // IFWL_WidgetDelegate:
121   void OnProcessMessage(CFWL_Message* pMessage) override;
122   void OnProcessEvent(CFWL_Event* pEvent) override;
123 
124   void InflateWidgetRect(CFX_RectF& rect);
125   void SetWidgetRect(const CFX_RectF& rect);
126 
127   bool IsVisible() const;
128   bool IsOverLapper() const;
129   bool IsPopup() const;
130   bool IsChild() const;
131 
GetWidgetMgr()132   CFWL_WidgetMgr* GetWidgetMgr() const { return m_pWidgetMgr; }
GetOuter()133   CFWL_Widget* GetOuter() const { return m_pOuter; }
134   CFWL_Widget* GetOutmost() const;
135 
136   void ModifyStyles(uint32_t dwStylesAdded, uint32_t dwStylesRemoved);
GetStyleExts()137   uint32_t GetStyleExts() const { return m_Properties.m_dwStyleExts; }
GetStates()138   uint32_t GetStates() const { return m_Properties.m_dwStates; }
139 
140   CFX_PointF TransformTo(CFWL_Widget* pWidget, const CFX_PointF& point);
141   CFX_Matrix GetMatrix() const;
142   IFWL_ThemeProvider* GetThemeProvider() const;
SetDelegate(IFWL_WidgetDelegate * delegate)143   void SetDelegate(IFWL_WidgetDelegate* delegate) { m_pDelegate = delegate; }
GetDelegate()144   IFWL_WidgetDelegate* GetDelegate() {
145     return m_pDelegate ? m_pDelegate.Get() : this;
146   }
GetDelegate()147   const IFWL_WidgetDelegate* GetDelegate() const {
148     return m_pDelegate ? m_pDelegate.Get() : this;
149   }
150 
GetFWLApp()151   CFWL_App* GetFWLApp() const { return m_pFWLApp; }
GetEventKey()152   uint64_t GetEventKey() const { return m_nEventKey; }
SetEventKey(uint64_t key)153   void SetEventKey(uint64_t key) { m_nEventKey = key; }
154 
GetAdapterIface()155   AdapterIface* GetAdapterIface() const { return m_pAdapterIface; }
SetAdapterIface(AdapterIface * pItem)156   void SetAdapterIface(AdapterIface* pItem) { m_pAdapterIface = pItem; }
157   void RepaintRect(const CFX_RectF& pRect);
158 
159  protected:
160   CFWL_Widget(CFWL_App* app, const Properties& properties, CFWL_Widget* pOuter);
161 
162   bool IsEnabled() const;
IsLocked()163   bool IsLocked() const { return m_iLock > 0; }
164   bool HasBorder() const;
165   CFX_RectF GetEdgeRect() const;
166   float GetCXBorderSize() const;
167   float GetCYBorderSize() const;
168   CFX_RectF GetRelativeRect() const;
169   CFX_SizeF CalcTextSize(const WideString& wsText, bool bMultiLine);
170   void CalcTextRect(const WideString& wsText,
171                     const FDE_TextStyle& dwTTOStyles,
172                     FDE_TextAlignment iTTOAlign,
173                     CFX_RectF* pRect);
174   void SetGrab(bool bSet);
175   void UnregisterEventTarget();
176   void DispatchEvent(CFWL_Event* pEvent);
177   void DrawBorder(CFGAS_GEGraphics* pGraphics,
178                   CFWL_ThemePart::Part iPartBorder,
179                   const CFX_Matrix& pMatrix);
180 
181   Properties m_Properties;
182   CFX_RectF m_WidgetRect;
183 
184  private:
LockUpdate()185   void LockUpdate() { m_iLock++; }
UnlockUpdate()186   void UnlockUpdate() {
187     if (IsLocked())
188       m_iLock--;
189   }
190 
GetParent()191   CFWL_Widget* GetParent() const { return m_pWidgetMgr->GetParentWidget(this); }
192   CFX_SizeF GetOffsetFromParent(CFWL_Widget* pParent);
193   void DrawBackground(CFGAS_GEGraphics* pGraphics,
194                       CFWL_ThemePart::Part iPartBk,
195                       const CFX_Matrix& mtMatrix);
196   void NotifyDriver();
197   bool IsParent(CFWL_Widget* pParent);
198 
199   int32_t m_iLock = 0;
200   uint64_t m_nEventKey = 0;
201   cppgc::Member<AdapterIface> m_pAdapterIface;
202   cppgc::Member<CFWL_App> const m_pFWLApp;
203   cppgc::Member<CFWL_WidgetMgr> const m_pWidgetMgr;
204   cppgc::Member<IFWL_WidgetDelegate> m_pDelegate;
205   cppgc::Member<CFWL_Widget> const m_pOuter;
206 };
207 
208 }  // namespace pdfium
209 
210 // TODO(crbug.com/42271761): Remove.
211 using pdfium::CFWL_Widget;
212 using pdfium::FWL_Type;
213 
214 #endif  // XFA_FWL_CFWL_WIDGET_H_
215