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 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/mask.h" 15 #include "core/fxcrt/observed_ptr.h" 16 #include "core/fxcrt/unowned_ptr.h" 17 #include "core/fxcrt/widestring.h" 18 #include "core/fxge/cfx_color.h" 19 #include "core/fxge/cfx_renderdevice.h" 20 #include "fpdfsdk/pwl/ipwl_fillernotify.h" 21 #include "public/fpdf_fwlevent.h" 22 23 class CPWL_Edit; 24 class CPWL_MsgControl; 25 class CPWL_ScrollBar; 26 class IPVT_FontMap; 27 struct PWL_SCROLL_INFO; 28 29 // window styles 30 #define PWS_BORDER 0x40000000L 31 #define PWS_BACKGROUND 0x20000000L 32 #define PWS_VSCROLL 0x08000000L 33 #define PWS_VISIBLE 0x04000000L 34 #define PWS_READONLY 0x01000000L 35 #define PWS_AUTOFONTSIZE 0x00800000L 36 #define PWS_AUTOTRANSPARENT 0x00400000L 37 #define PWS_NOREFRESHCLIP 0x00200000L 38 39 // edit and label styles 40 #define PES_MULTILINE 0x0001L 41 #define PES_PASSWORD 0x0002L 42 #define PES_LEFT 0x0004L 43 #define PES_RIGHT 0x0008L 44 #define PES_MIDDLE 0x0010L 45 #define PES_TOP 0x0020L 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_TEXTOVERFLOW 0x4000L 53 54 // listbox styles 55 #define PLBS_MULTIPLESEL 0x0001L 56 #define PLBS_HOVERSEL 0x0008L 57 58 // combobox styles 59 #define PCBS_ALLOWCUSTOMTEXT 0x0001L 60 61 struct CPWL_Dash { CPWL_DashCPWL_Dash62 CPWL_Dash() : nDash(0), nGap(0), nPhase(0) {} CPWL_DashCPWL_Dash63 CPWL_Dash(int32_t dash, int32_t gap, int32_t phase) 64 : nDash(dash), nGap(gap), nPhase(phase) {} 65 ResetCPWL_Dash66 void Reset() { 67 nDash = 0; 68 nGap = 0; 69 nPhase = 0; 70 } 71 72 int32_t nDash; 73 int32_t nGap; 74 int32_t nPhase; 75 }; 76 77 class CPWL_Wnd : public Observable { 78 public: 79 static const CFX_Color kDefaultBlackColor; 80 static const CFX_Color kDefaultWhiteColor; 81 82 class ProviderIface : public Observable { 83 public: 84 virtual ~ProviderIface() = default; 85 86 // get a matrix which map user space to CWnd client space 87 virtual CFX_Matrix GetWindowMatrix( 88 const IPWL_FillerNotify::PerWindowData* pAttached) = 0; 89 90 virtual void OnSetFocusForEdit(CPWL_Edit* pEdit) = 0; 91 }; 92 93 // Caller-provided options for window creation. 94 class CreateParams { 95 public: 96 CreateParams(CFX_Timer::HandlerIface* timer_handler, 97 IPWL_FillerNotify* filler_notify, 98 ProviderIface* provider); 99 CreateParams(const CreateParams& other); 100 ~CreateParams(); 101 102 // Required: 103 CFX_FloatRect rcRectWnd; 104 ObservedPtr<CFX_Timer::HandlerIface> const pTimerHandler; 105 UnownedPtr<IPWL_FillerNotify> const pFillerNotify; 106 UnownedPtr<IPVT_FontMap> pFontMap; 107 ObservedPtr<ProviderIface> pProvider; 108 109 // Optional: 110 uint32_t dwFlags = 0; 111 CFX_Color sBackgroundColor; 112 BorderStyle nBorderStyle = BorderStyle::kSolid; 113 int32_t dwBorderWidth = 1; 114 CFX_Color sBorderColor; 115 CFX_Color sTextColor; 116 int32_t nTransparency = 255; 117 float fFontSize; 118 CPWL_Dash sDash; 119 120 // Ignore, used internally only: 121 CPWL_MsgControl* pMsgControl = nullptr; 122 IPWL_FillerNotify::CursorStyle eCursorType = 123 IPWL_FillerNotify::CursorStyle::kArrow; 124 }; 125 126 static bool IsSHIFTKeyDown(Mask<FWL_EVENTFLAG> nFlag); 127 static bool IsCTRLKeyDown(Mask<FWL_EVENTFLAG> nFlag); 128 static bool IsALTKeyDown(Mask<FWL_EVENTFLAG> nFlag); 129 static bool IsMETAKeyDown(Mask<FWL_EVENTFLAG> nFlag); 130 131 // Selects between IsCTRLKeyDown() and IsMETAKeyDown() depending on platform. 132 static bool IsPlatformShortcutKey(Mask<FWL_EVENTFLAG> nFlag); 133 134 CPWL_Wnd(const CreateParams& cp, 135 std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData); 136 virtual ~CPWL_Wnd(); 137 138 // Returns |true| iff this instance is still allocated. 139 virtual bool InvalidateRect(const CFX_FloatRect* pRect); 140 141 virtual bool OnKeyDown(FWL_VKEYCODE nKeyCode, Mask<FWL_EVENTFLAG> nFlag); 142 virtual bool OnChar(uint16_t nChar, Mask<FWL_EVENTFLAG> nFlag); 143 virtual bool OnLButtonDblClk(Mask<FWL_EVENTFLAG> nFlag, 144 const CFX_PointF& point); 145 virtual bool OnLButtonDown(Mask<FWL_EVENTFLAG> nFlag, 146 const CFX_PointF& point); 147 virtual bool OnLButtonUp(Mask<FWL_EVENTFLAG> nFlag, const CFX_PointF& point); 148 virtual bool OnRButtonDown(Mask<FWL_EVENTFLAG> nFlag, 149 const CFX_PointF& point); 150 virtual bool OnRButtonUp(Mask<FWL_EVENTFLAG> nFlag, const CFX_PointF& point); 151 virtual bool OnMouseMove(Mask<FWL_EVENTFLAG> nFlag, const CFX_PointF& point); 152 virtual bool OnMouseWheel(Mask<FWL_EVENTFLAG> nFlag, 153 const CFX_PointF& point, 154 const CFX_Vector& delta); 155 virtual void SetScrollInfo(const PWL_SCROLL_INFO& info); 156 virtual void SetScrollPosition(float pos); 157 virtual void ScrollWindowVertically(float pos); 158 virtual void NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos); 159 virtual void NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos); 160 virtual void NotifyMouseMove(CPWL_Wnd* child, const CFX_PointF& pos); 161 virtual void SetFocus(); 162 virtual void KillFocus(); 163 virtual void SetCursor(); 164 165 // Returns |true| iff this instance is still allocated. 166 virtual bool SetVisible(bool bVisible); 167 virtual void SetFontSize(float fFontSize); 168 virtual float GetFontSize() const; 169 170 virtual WideString GetText(); 171 virtual WideString GetSelectedText(); 172 virtual void ReplaceAndKeepSelection(const WideString& text); 173 virtual void ReplaceSelection(const WideString& text); 174 virtual bool SelectAllText(); 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 virtual void OnSetFocus(); 185 virtual void OnKillFocus(); 186 187 void AddChild(std::unique_ptr<CPWL_Wnd> pWnd); 188 void RemoveChild(CPWL_Wnd* pWnd); 189 void Realize(); 190 void Destroy(); 191 bool Move(const CFX_FloatRect& rcNew, bool bReset, bool bRefresh); 192 193 void InvalidateProvider(ProviderIface* provider); 194 void DrawAppearance(CFX_RenderDevice* pDevice, 195 const CFX_Matrix& mtUser2Device); 196 197 int32_t GetBorderWidth() const; 198 CFX_FloatRect GetWindowRect() const; 199 IsVisible()200 bool IsVisible() const { return m_bVisible; } 201 bool HasFlag(uint32_t dwFlags) const; 202 void RemoveFlag(uint32_t dwFlags); 203 void SetClipRect(const CFX_FloatRect& rect); 204 GetAttachedData()205 IPWL_FillerNotify::PerWindowData* GetAttachedData() const { 206 return m_pAttachedData.get(); 207 } 208 std::unique_ptr<IPWL_FillerNotify::PerWindowData> CloneAttachedData() const; 209 std::vector<UnownedPtr<CPWL_Wnd>> GetAncestors(); 210 211 bool WndHitTest(const CFX_PointF& point) const; 212 bool ClientHitTest(const CFX_PointF& point) const; 213 bool IsCaptureMouse() const; 214 215 bool IsFocused() const; 216 bool IsReadOnly() const; 217 218 void SetTransparency(int32_t nTransparency); 219 CFX_Matrix GetWindowMatrix() const; 220 221 protected: 222 virtual void CreateChildWnd(const CreateParams& cp); 223 224 // Returns |true| iff this instance is still allocated. 225 virtual bool RePosChildWnd(); 226 227 virtual void DrawThisAppearance(CFX_RenderDevice* pDevice, 228 const CFX_Matrix& mtUser2Device); 229 230 virtual void OnCreated(); 231 virtual void OnDestroy(); 232 IsValid()233 bool IsValid() const { return m_bCreated; } GetCreationParams()234 CreateParams* GetCreationParams() { return &m_CreationParams; } GetProvider()235 ProviderIface* GetProvider() const { 236 return m_CreationParams.pProvider.Get(); 237 } GetTimerHandler()238 CFX_Timer::HandlerIface* GetTimerHandler() const { 239 return m_CreationParams.pTimerHandler.Get(); 240 } GetFillerNotify()241 IPWL_FillerNotify* GetFillerNotify() const { 242 return m_CreationParams.pFillerNotify; 243 } 244 GetParentWindow()245 CPWL_Wnd* GetParentWindow() const { return m_pParent; } 246 CPWL_ScrollBar* GetVScrollBar() const; 247 248 // Returns |true| iff this instance is still allocated. 249 bool InvalidateRectMove(const CFX_FloatRect& rcOld, 250 const CFX_FloatRect& rcNew); 251 252 void SetCapture(); 253 void ReleaseCapture(); 254 bool IsWndCaptureMouse(const CPWL_Wnd* pWnd) const; 255 bool IsWndCaptureKeyboard(const CPWL_Wnd* pWnd) const; 256 257 CFX_Color GetBackgroundColor() const; 258 CFX_Color GetBorderColor() const; 259 CFX_Color GetTextColor() const; 260 CFX_Color GetBorderLeftTopColor(BorderStyle nBorderStyle) const; 261 CFX_Color GetBorderRightBottomColor(BorderStyle nBorderStyle) const; 262 BorderStyle GetBorderStyle() const; 263 const CPWL_Dash& GetBorderDash() const; 264 265 int32_t GetTransparency(); 266 int32_t GetInnerBorderWidth() const; 267 CFX_PointF GetCenterPoint() const; 268 const CFX_FloatRect& GetClipRect() const; 269 GetFontMap()270 IPVT_FontMap* GetFontMap() const { return m_CreationParams.pFontMap; } 271 272 private: 273 void DrawChildAppearance(CFX_RenderDevice* pDevice, 274 const CFX_Matrix& mtUser2Device); 275 276 CFX_FloatRect PWLtoWnd(const CFX_FloatRect& rect) const; 277 278 void CreateScrollBar(const CreateParams& cp); 279 void CreateVScrollBar(const CreateParams& cp); 280 281 void AdjustStyle(); 282 void CreateMsgControl(); 283 void DestroyMsgControl(); 284 CPWL_MsgControl* GetMsgControl() const; 285 286 CreateParams m_CreationParams; 287 std::unique_ptr<IPWL_FillerNotify::PerWindowData> m_pAttachedData; 288 UnownedPtr<CPWL_Wnd> m_pParent; 289 std::vector<std::unique_ptr<CPWL_Wnd>> m_Children; 290 UnownedPtr<CPWL_ScrollBar> m_pVScrollBar; 291 CFX_FloatRect m_rcWindow; 292 CFX_FloatRect m_rcClip; 293 bool m_bCreated = false; 294 bool m_bVisible = false; 295 }; 296 297 #endif // FPDFSDK_PWL_CPWL_WND_H_ 298