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/unowned_ptr_exclusion.h" 18 #include "core/fxcrt/widestring.h" 19 #include "core/fxge/cfx_color.h" 20 #include "core/fxge/cfx_renderdevice.h" 21 #include "fpdfsdk/pwl/ipwl_fillernotify.h" 22 #include "public/fpdf_fwlevent.h" 23 24 class CPWL_Edit; 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 SharedCaptureFocusState; 83 84 class ProviderIface : public Observable { 85 public: 86 virtual ~ProviderIface() = default; 87 88 // get a matrix which map user space to CWnd client space 89 virtual CFX_Matrix GetWindowMatrix( 90 const IPWL_FillerNotify::PerWindowData* pAttached) = 0; 91 92 virtual void OnSetFocusForEdit(CPWL_Edit* pEdit) = 0; 93 }; 94 95 // Caller-provided options for window creation. 96 class CreateParams { 97 public: 98 CreateParams(CFX_Timer::HandlerIface* timer_handler, 99 IPWL_FillerNotify* filler_notify, 100 ProviderIface* provider); 101 CreateParams(const CreateParams& other); 102 ~CreateParams(); 103 104 // Required: 105 CFX_FloatRect rcRectWnd; 106 ObservedPtr<CFX_Timer::HandlerIface> const pTimerHandler; 107 UnownedPtr<IPWL_FillerNotify> const pFillerNotify; 108 UnownedPtr<IPVT_FontMap> pFontMap; 109 ObservedPtr<ProviderIface> pProvider; 110 111 // Optional: 112 uint32_t dwFlags = 0; 113 CFX_Color sBackgroundColor; 114 BorderStyle nBorderStyle = BorderStyle::kSolid; 115 int32_t dwBorderWidth = 1; 116 CFX_Color sBorderColor; 117 CFX_Color sTextColor; 118 int32_t nTransparency = 255; 119 float fFontSize; 120 CPWL_Dash sDash; 121 122 // Ignore, used internally only: 123 // TODO(tsepez): fix murky ownership, bare delete. 124 UNOWNED_PTR_EXCLUSION SharedCaptureFocusState* pSharedCaptureFocusState = 125 nullptr; 126 IPWL_FillerNotify::CursorStyle eCursorType = 127 IPWL_FillerNotify::CursorStyle::kArrow; 128 }; 129 130 static bool IsSHIFTKeyDown(Mask<FWL_EVENTFLAG> nFlag); 131 static bool IsCTRLKeyDown(Mask<FWL_EVENTFLAG> nFlag); 132 static bool IsALTKeyDown(Mask<FWL_EVENTFLAG> nFlag); 133 static bool IsMETAKeyDown(Mask<FWL_EVENTFLAG> nFlag); 134 135 // Selects between IsCTRLKeyDown() and IsMETAKeyDown() depending on platform. 136 static bool IsPlatformShortcutKey(Mask<FWL_EVENTFLAG> nFlag); 137 138 CPWL_Wnd(const CreateParams& cp, 139 std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData); 140 virtual ~CPWL_Wnd(); 141 142 // Returns |true| iff this instance is still allocated. 143 [[nodiscard]] virtual bool InvalidateRect(const CFX_FloatRect* pRect); 144 145 virtual bool OnKeyDown(FWL_VKEYCODE nKeyCode, Mask<FWL_EVENTFLAG> nFlag); 146 virtual bool OnChar(uint16_t nChar, Mask<FWL_EVENTFLAG> nFlag); 147 virtual bool OnLButtonDblClk(Mask<FWL_EVENTFLAG> nFlag, 148 const CFX_PointF& point); 149 virtual bool OnLButtonDown(Mask<FWL_EVENTFLAG> nFlag, 150 const CFX_PointF& point); 151 virtual bool OnLButtonUp(Mask<FWL_EVENTFLAG> nFlag, const CFX_PointF& point); 152 virtual bool OnRButtonDown(Mask<FWL_EVENTFLAG> nFlag, 153 const CFX_PointF& point); 154 virtual bool OnRButtonUp(Mask<FWL_EVENTFLAG> nFlag, const CFX_PointF& point); 155 virtual bool OnMouseMove(Mask<FWL_EVENTFLAG> nFlag, const CFX_PointF& point); 156 virtual bool OnMouseWheel(Mask<FWL_EVENTFLAG> nFlag, 157 const CFX_PointF& point, 158 const CFX_Vector& delta); 159 virtual void SetScrollInfo(const PWL_SCROLL_INFO& info); 160 virtual void SetScrollPosition(float pos); 161 virtual void ScrollWindowVertically(float pos); 162 virtual void NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos); 163 virtual void NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos); 164 virtual void NotifyMouseMove(CPWL_Wnd* child, const CFX_PointF& pos); 165 virtual void SetFocus(); 166 virtual void KillFocus(); 167 virtual void SetCursor(); 168 169 // Returns |true| iff this instance is still allocated. 170 [[nodiscard]] virtual bool SetVisible(bool bVisible); 171 virtual void SetFontSize(float fFontSize); 172 virtual float GetFontSize() const; 173 174 virtual WideString GetText(); 175 virtual WideString GetSelectedText(); 176 virtual void ReplaceAndKeepSelection(const WideString& text); 177 virtual void ReplaceSelection(const WideString& text); 178 virtual bool SelectAllText(); 179 180 virtual bool CanUndo(); 181 virtual bool CanRedo(); 182 virtual bool Undo(); 183 virtual bool Redo(); 184 185 virtual CFX_FloatRect GetFocusRect() const; 186 virtual CFX_FloatRect GetClientRect() const; 187 188 virtual void OnSetFocus(); 189 virtual void OnKillFocus(); 190 191 void AddChild(std::unique_ptr<CPWL_Wnd> pWnd); 192 void RemoveChild(CPWL_Wnd* pWnd); 193 void Realize(); 194 void Destroy(); 195 bool Move(const CFX_FloatRect& rcNew, bool bReset, bool bRefresh); 196 197 void InvalidateProvider(ProviderIface* provider); 198 void DrawAppearance(CFX_RenderDevice* pDevice, 199 const CFX_Matrix& mtUser2Device); 200 201 int32_t GetBorderWidth() const; 202 CFX_FloatRect GetWindowRect() const; 203 IsVisible()204 bool IsVisible() const { return m_bVisible; } 205 bool HasFlag(uint32_t dwFlags) const; 206 void RemoveFlag(uint32_t dwFlags); 207 void SetClipRect(const CFX_FloatRect& rect); 208 GetAttachedData()209 IPWL_FillerNotify::PerWindowData* GetAttachedData() const { 210 return m_pAttachedData.get(); 211 } 212 std::unique_ptr<IPWL_FillerNotify::PerWindowData> CloneAttachedData() const; 213 std::vector<UnownedPtr<CPWL_Wnd>> GetAncestors(); 214 215 bool WndHitTest(const CFX_PointF& point) const; 216 bool ClientHitTest(const CFX_PointF& point) const; 217 bool IsCaptureMouse() const; 218 219 bool IsFocused() const; 220 bool IsReadOnly() const; 221 222 void SetTransparency(int32_t nTransparency); 223 CFX_Matrix GetWindowMatrix() const; 224 225 protected: 226 virtual void CreateChildWnd(const CreateParams& cp); 227 228 // Returns |true| iff this instance is still allocated. 229 [[nodiscard]] virtual bool RepositionChildWnd(); 230 231 virtual void DrawThisAppearance(CFX_RenderDevice* pDevice, 232 const CFX_Matrix& mtUser2Device); 233 234 virtual void OnCreated(); 235 virtual void OnDestroy(); 236 IsValid()237 bool IsValid() const { return m_bCreated; } GetCreationParams()238 CreateParams* GetCreationParams() { return &m_CreationParams; } GetProvider()239 ProviderIface* GetProvider() const { 240 return m_CreationParams.pProvider.Get(); 241 } GetTimerHandler()242 CFX_Timer::HandlerIface* GetTimerHandler() const { 243 return m_CreationParams.pTimerHandler.Get(); 244 } GetFillerNotify()245 IPWL_FillerNotify* GetFillerNotify() const { 246 return m_CreationParams.pFillerNotify; 247 } 248 GetParentWindow()249 CPWL_Wnd* GetParentWindow() const { return m_pParent; } 250 CPWL_ScrollBar* GetVScrollBar() const; 251 252 // Returns |true| iff this instance is still allocated. 253 [[nodiscard]] bool InvalidateRectMove(const CFX_FloatRect& rcOld, 254 const CFX_FloatRect& rcNew); 255 256 void SetCapture(); 257 void ReleaseCapture(); 258 bool IsWndCaptureMouse(const CPWL_Wnd* pWnd) const; 259 bool IsWndCaptureKeyboard(const CPWL_Wnd* pWnd) const; 260 261 CFX_Color GetBackgroundColor() const; 262 CFX_Color GetBorderColor() const; 263 CFX_Color GetTextColor() const; 264 CFX_Color GetBorderLeftTopColor(BorderStyle nBorderStyle) const; 265 CFX_Color GetBorderRightBottomColor(BorderStyle nBorderStyle) const; 266 BorderStyle GetBorderStyle() const; 267 const CPWL_Dash& GetBorderDash() const; 268 269 int32_t GetTransparency(); 270 int32_t GetInnerBorderWidth() const; 271 CFX_PointF GetCenterPoint() const; 272 const CFX_FloatRect& GetClipRect() const; 273 GetFontMap()274 IPVT_FontMap* GetFontMap() const { return m_CreationParams.pFontMap; } 275 276 private: 277 void DrawChildAppearance(CFX_RenderDevice* pDevice, 278 const CFX_Matrix& mtUser2Device); 279 280 CFX_FloatRect PWLtoWnd(const CFX_FloatRect& rect) const; 281 282 void CreateVScrollBar(const CreateParams& cp); 283 284 void AdjustStyle(); 285 void CreateSharedCaptureFocusState(); 286 void DestroySharedCaptureFocusState(); 287 SharedCaptureFocusState* GetSharedCaptureFocusState() const; 288 289 CreateParams m_CreationParams; 290 std::unique_ptr<IPWL_FillerNotify::PerWindowData> m_pAttachedData; 291 UnownedPtr<CPWL_Wnd> m_pParent; 292 std::vector<std::unique_ptr<CPWL_Wnd>> m_Children; 293 UnownedPtr<CPWL_ScrollBar> m_pVScrollBar; 294 CFX_FloatRect m_rcWindow; 295 CFX_FloatRect m_rcClip; 296 bool m_bCreated = false; 297 bool m_bVisible = false; 298 }; 299 300 #endif // FPDFSDK_PWL_CPWL_WND_H_ 301