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 FPDFSDK_PWL_CPWL_WND_H_
8 #define FPDFSDK_PWL_CPWL_WND_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fxcrt/cfx_timer.h"
14 #include "core/fxcrt/observed_ptr.h"
15 #include "core/fxcrt/unowned_ptr.h"
16 #include "core/fxge/cfx_color.h"
17 #include "core/fxge/cfx_renderdevice.h"
18 #include "fpdfsdk/pwl/ipwl_systemhandler.h"
19 
20 class CPWL_Edit;
21 class CPWL_MsgControl;
22 class CPWL_ScrollBar;
23 class IPVT_FontMap;
24 struct PWL_SCROLL_INFO;
25 
26 // window styles
27 #define PWS_CHILD 0x80000000L
28 #define PWS_BORDER 0x40000000L
29 #define PWS_BACKGROUND 0x20000000L
30 #define PWS_HSCROLL 0x10000000L
31 #define PWS_VSCROLL 0x08000000L
32 #define PWS_VISIBLE 0x04000000L
33 #define PWS_READONLY 0x01000000L
34 #define PWS_AUTOFONTSIZE 0x00800000L
35 #define PWS_AUTOTRANSPARENT 0x00400000L
36 #define PWS_NOREFRESHCLIP 0x00200000L
37 
38 // edit and label styles
39 #define PES_MULTILINE 0x0001L
40 #define PES_PASSWORD 0x0002L
41 #define PES_LEFT 0x0004L
42 #define PES_RIGHT 0x0008L
43 #define PES_MIDDLE 0x0010L
44 #define PES_TOP 0x0020L
45 #define PES_BOTTOM 0x0040L
46 #define PES_CENTER 0x0080L
47 #define PES_CHARARRAY 0x0100L
48 #define PES_AUTOSCROLL 0x0200L
49 #define PES_AUTORETURN 0x0400L
50 #define PES_UNDO 0x0800L
51 #define PES_RICH 0x1000L
52 #define PES_SPELLCHECK 0x2000L
53 #define PES_TEXTOVERFLOW 0x4000L
54 #define PES_NOREAD 0x8000L
55 
56 // listbox styles
57 #define PLBS_MULTIPLESEL 0x0001L
58 #define PLBS_HOVERSEL 0x0008L
59 
60 // combobox styles
61 #define PCBS_ALLOWCUSTOMTEXT 0x0001L
62 
63 // Cursor style. These must match the values in public/fpdf_formfill.h
64 #define FXCT_ARROW 0
65 #define FXCT_NESW 1
66 #define FXCT_NWSE 2
67 #define FXCT_VBEAM 3
68 #define FXCT_HBEAM 4
69 #define FXCT_HAND 5
70 
71 struct CPWL_Dash {
CPWL_DashCPWL_Dash72   CPWL_Dash() : nDash(0), nGap(0), nPhase(0) {}
CPWL_DashCPWL_Dash73   CPWL_Dash(int32_t dash, int32_t gap, int32_t phase)
74       : nDash(dash), nGap(gap), nPhase(phase) {}
75 
ResetCPWL_Dash76   void Reset() {
77     nDash = 0;
78     nGap = 0;
79     nPhase = 0;
80   }
81 
82   int32_t nDash;
83   int32_t nGap;
84   int32_t nPhase;
85 };
86 
87 #define PWL_SCROLLBAR_WIDTH 12.0f
88 #define PWL_SCROLLBAR_TRANSPARENCY 150
89 #define PWL_DEFAULT_BLACKCOLOR CFX_Color(CFX_Color::kGray, 0)
90 #define PWL_DEFAULT_WHITECOLOR CFX_Color(CFX_Color::kGray, 1)
91 
92 class CPWL_Wnd : public Observable {
93  public:
94   class ProviderIface : public Observable {
95    public:
96     virtual ~ProviderIface() = default;
97 
98     // get a matrix which map user space to CWnd client space
99     virtual CFX_Matrix GetWindowMatrix(
100         const IPWL_SystemHandler::PerWindowData* pAttached) = 0;
101   };
102 
103   class FocusHandlerIface {
104    public:
105     virtual ~FocusHandlerIface() = default;
106     virtual void OnSetFocus(CPWL_Edit* pEdit) = 0;
107   };
108 
109   class CreateParams {
110    public:
111     CreateParams();
112     CreateParams(const CreateParams& other);
113     ~CreateParams();
114 
115     CFX_FloatRect rcRectWnd;                        // required
116     UnownedPtr<TimerHandlerIface> pTimerHandler;    // required
117     UnownedPtr<IPWL_SystemHandler> pSystemHandler;  // required
118     UnownedPtr<IPVT_FontMap> pFontMap;              // required
119     ObservedPtr<ProviderIface> pProvider;           // required
120     UnownedPtr<FocusHandlerIface> pFocusHandler;    // optional
121     uint32_t dwFlags = 0;                           // optional
122     CFX_Color sBackgroundColor;                     // optional
123     BorderStyle nBorderStyle = BorderStyle::SOLID;  // optional
124     int32_t dwBorderWidth = 1;                      // optional
125     CFX_Color sBorderColor;                         // optional
126     CFX_Color sTextColor;                           // optional
127     int32_t nTransparency = 255;                    // optional
128     float fFontSize;                                // optional
129     CPWL_Dash sDash;                                // optional
130     CPWL_MsgControl* pMsgControl = nullptr;         // ignore
131     int32_t eCursorType = FXCT_ARROW;               // ignore
132     CFX_Matrix mtChild;                             // ignore
133   };
134 
135   static bool IsSHIFTKeyDown(uint32_t nFlag);
136   static bool IsCTRLKeyDown(uint32_t nFlag);
137   static bool IsALTKeyDown(uint32_t nFlag);
138 
139   CPWL_Wnd(const CreateParams& cp,
140            std::unique_ptr<IPWL_SystemHandler::PerWindowData> pAttachedData);
141   virtual ~CPWL_Wnd();
142 
143   // Returns |true| iff this instance is still allocated.
144   virtual bool InvalidateRect(CFX_FloatRect* pRect);
145 
146   virtual bool OnKeyDown(uint16_t nChar, uint32_t nFlag);
147   virtual bool OnChar(uint16_t nChar, uint32_t nFlag);
148   virtual bool OnLButtonDblClk(const CFX_PointF& point, uint32_t nFlag);
149   virtual bool OnLButtonDown(const CFX_PointF& point, uint32_t nFlag);
150   virtual bool OnLButtonUp(const CFX_PointF& point, uint32_t nFlag);
151   virtual bool OnRButtonDown(const CFX_PointF& point, uint32_t nFlag);
152   virtual bool OnRButtonUp(const CFX_PointF& point, uint32_t nFlag);
153   virtual bool OnMouseMove(const CFX_PointF& point, uint32_t nFlag);
154   virtual bool OnMouseWheel(short zDelta,
155                             const CFX_PointF& point,
156                             uint32_t nFlag);
157   virtual void SetScrollInfo(const PWL_SCROLL_INFO& info);
158   virtual void SetScrollPosition(float pos);
159   virtual void ScrollWindowVertically(float pos);
160   virtual void NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos);
161   virtual void NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos);
162   virtual void NotifyMouseMove(CPWL_Wnd* child, const CFX_PointF& pos);
163   virtual void SetFocus();
164   virtual void KillFocus();
165   virtual void SetCursor();
166 
167   // Returns |true| iff this instance is still allocated.
168   virtual bool SetVisible(bool bVisible);
169   virtual void SetFontSize(float fFontSize);
170   virtual float GetFontSize() const;
171 
172   virtual WideString GetText();
173   virtual WideString GetSelectedText();
174   virtual void ReplaceSelection(const WideString& text);
175 
176   virtual bool CanUndo();
177   virtual bool CanRedo();
178   virtual bool Undo();
179   virtual bool Redo();
180 
181   virtual CFX_FloatRect GetFocusRect() const;
182   virtual CFX_FloatRect GetClientRect() const;
183 
184   void AddChild(std::unique_ptr<CPWL_Wnd> pWnd);
185   void RemoveChild(CPWL_Wnd* pWnd);
186   void Realize();
187   void Destroy();
188   bool Move(const CFX_FloatRect& rcNew, bool bReset, bool bRefresh);
189 
190   void InvalidateFocusHandler(FocusHandlerIface* handler);
191   void InvalidateProvider(ProviderIface* provider);
192   void SetCapture();
193   void ReleaseCapture();
194   void DrawAppearance(CFX_RenderDevice* pDevice,
195                       const CFX_Matrix& mtUser2Device);
196 
197   CFX_Color GetBackgroundColor() const;
198   void SetBackgroundColor(const CFX_Color& color);
199   CFX_Color GetBorderColor() const;
200   CFX_Color GetTextColor() const;
201   void SetTextColor(const CFX_Color& color);
202   CFX_Color GetBorderLeftTopColor(BorderStyle nBorderStyle) const;
203   CFX_Color GetBorderRightBottomColor(BorderStyle nBorderStyle) const;
204 
205   void SetBorderStyle(BorderStyle nBorderStyle);
206   BorderStyle GetBorderStyle() const;
207   const CPWL_Dash& GetBorderDash() const;
208 
209   int32_t GetBorderWidth() const;
210   int32_t GetInnerBorderWidth() const;
211   CFX_FloatRect GetWindowRect() const;
212   CFX_PointF GetCenterPoint() const;
213 
IsVisible()214   bool IsVisible() const { return m_bVisible; }
215   bool HasFlag(uint32_t dwFlags) const;
216   void AddFlag(uint32_t dwFlags);
217   void RemoveFlag(uint32_t dwFlags);
218 
219   void SetClipRect(const CFX_FloatRect& rect);
220   const CFX_FloatRect& GetClipRect() const;
221 
GetParentWindow()222   CPWL_Wnd* GetParentWindow() const { return m_pParent.Get(); }
GetAttachedData()223   const IPWL_SystemHandler::PerWindowData* GetAttachedData() const {
224     return m_pAttachedData.get();
225   }
226   std::unique_ptr<IPWL_SystemHandler::PerWindowData> CloneAttachedData() const;
227 
228   bool WndHitTest(const CFX_PointF& point) const;
229   bool ClientHitTest(const CFX_PointF& point) const;
230   bool IsCaptureMouse() const;
231 
232   void EnableWindow(bool bEnable);
IsEnabled()233   bool IsEnabled() const { return m_bEnabled; }
234   const CPWL_Wnd* GetFocused() const;
235   bool IsFocused() const;
236   bool IsReadOnly() const;
237   CPWL_ScrollBar* GetVScrollBar() const;
238 
GetFontMap()239   IPVT_FontMap* GetFontMap() const { return m_CreationParams.pFontMap.Get(); }
GetProvider()240   ProviderIface* GetProvider() const {
241     return m_CreationParams.pProvider.Get();
242   }
GetFocusHandler()243   FocusHandlerIface* GetFocusHandler() const {
244     return m_CreationParams.pFocusHandler.Get();
245   }
246 
247   int32_t GetTransparency();
248   void SetTransparency(int32_t nTransparency);
249 
250   CFX_Matrix GetChildToRoot() const;
251   CFX_Matrix GetChildMatrix() const;
252   void SetChildMatrix(const CFX_Matrix& mt);
253   CFX_Matrix GetWindowMatrix() const;
254 
255   virtual void OnSetFocus();
256   virtual void OnKillFocus();
257 
258  protected:
259   virtual void CreateChildWnd(const CreateParams& cp);
260 
261   // Returns |true| iff this instance is still allocated.
262   virtual bool RePosChildWnd();
263 
264   virtual void DrawThisAppearance(CFX_RenderDevice* pDevice,
265                                   const CFX_Matrix& mtUser2Device);
266 
267   virtual void OnCreated();
268   virtual void OnDestroy();
269 
IsNotifying()270   bool IsNotifying() const { return m_bNotifying; }
IsValid()271   bool IsValid() const { return m_bCreated; }
GetCreationParams()272   CreateParams* GetCreationParams() { return &m_CreationParams; }
GetTimerHandler()273   TimerHandlerIface* GetTimerHandler() const {
274     return m_CreationParams.pTimerHandler.Get();
275   }
GetSystemHandler()276   IPWL_SystemHandler* GetSystemHandler() const {
277     return m_CreationParams.pSystemHandler.Get();
278   }
279 
280   // Returns |true| iff this instance is still allocated.
281   bool InvalidateRectMove(const CFX_FloatRect& rcOld,
282                           const CFX_FloatRect& rcNew);
283 
284   bool IsWndCaptureMouse(const CPWL_Wnd* pWnd) const;
285   bool IsWndCaptureKeyboard(const CPWL_Wnd* pWnd) const;
286 
IsCTRLpressed(uint32_t nFlag)287   static bool IsCTRLpressed(uint32_t nFlag) {
288     return CPWL_Wnd::IsCTRLKeyDown(nFlag);
289   }
IsSHIFTpressed(uint32_t nFlag)290   static bool IsSHIFTpressed(uint32_t nFlag) {
291     return CPWL_Wnd::IsSHIFTKeyDown(nFlag);
292   }
IsALTpressed(uint32_t nFlag)293   static bool IsALTpressed(uint32_t nFlag) {
294     return CPWL_Wnd::IsALTKeyDown(nFlag);
295   }
296 
297  private:
298   CFX_PointF ParentToChild(const CFX_PointF& point) const;
299   CFX_FloatRect ParentToChild(const CFX_FloatRect& rect) const;
300 
301   void DrawChildAppearance(CFX_RenderDevice* pDevice,
302                            const CFX_Matrix& mtUser2Device);
303 
304   CFX_FloatRect PWLtoWnd(const CFX_FloatRect& rect) const;
305 
306   void CreateScrollBar(const CreateParams& cp);
307   void CreateVScrollBar(const CreateParams& cp);
308 
309   void AdjustStyle();
310   void CreateMsgControl();
311   void DestroyMsgControl();
312 
313   CPWL_MsgControl* GetMsgControl() const;
314 
315   CreateParams m_CreationParams;
316   std::unique_ptr<IPWL_SystemHandler::PerWindowData> m_pAttachedData;
317   UnownedPtr<CPWL_Wnd> m_pParent;
318   std::vector<std::unique_ptr<CPWL_Wnd>> m_Children;
319   UnownedPtr<CPWL_ScrollBar> m_pVScrollBar;
320   CFX_FloatRect m_rcWindow;
321   CFX_FloatRect m_rcClip;
322   bool m_bCreated = false;
323   bool m_bVisible = false;
324   bool m_bNotifying = false;
325   bool m_bEnabled = true;
326 };
327 
328 #endif  // FPDFSDK_PWL_CPWL_WND_H_
329