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_SCROLL_BAR_H_ 8 #define FPDFSDK_PWL_CPWL_SCROLL_BAR_H_ 9 10 #include <memory> 11 12 #include "core/fxcrt/cfx_timer.h" 13 #include "core/fxcrt/unowned_ptr.h" 14 #include "fpdfsdk/pwl/cpwl_sbbutton.h" 15 #include "fpdfsdk/pwl/cpwl_wnd.h" 16 17 struct PWL_SCROLL_INFO { 18 public: PWL_SCROLL_INFOPWL_SCROLL_INFO19 PWL_SCROLL_INFO() 20 : fContentMin(0.0f), 21 fContentMax(0.0f), 22 fPlateWidth(0.0f), 23 fBigStep(0.0f), 24 fSmallStep(0.0f) {} 25 26 bool operator==(const PWL_SCROLL_INFO& that) const { 27 return fContentMin == that.fContentMin && fContentMax == that.fContentMax && 28 fPlateWidth == that.fPlateWidth && fBigStep == that.fBigStep && 29 fSmallStep == that.fSmallStep; 30 } 31 bool operator!=(const PWL_SCROLL_INFO& that) const { 32 return !(*this == that); 33 } 34 35 float fContentMin; 36 float fContentMax; 37 float fPlateWidth; 38 float fBigStep; 39 float fSmallStep; 40 }; 41 42 struct PWL_FLOATRANGE { 43 public: 44 PWL_FLOATRANGE() = default; 45 46 bool operator==(const PWL_FLOATRANGE& that) const { 47 return fMin == that.fMin && fMax == that.fMax; 48 } 49 bool operator!=(const PWL_FLOATRANGE& that) const { return !(*this == that); } 50 51 void Reset(); 52 void Set(float min, float max); 53 bool In(float x) const; 54 float GetWidth() const; 55 56 float fMin = 0.0f; 57 float fMax = 0.0f; 58 }; 59 60 struct PWL_SCROLL_PRIVATEDATA { 61 public: 62 PWL_SCROLL_PRIVATEDATA(); 63 64 bool operator==(const PWL_SCROLL_PRIVATEDATA& that) const { 65 return ScrollRange == that.ScrollRange && 66 fClientWidth == that.fClientWidth && fScrollPos == that.fScrollPos && 67 fBigStep == that.fBigStep && fSmallStep == that.fSmallStep; 68 } 69 bool operator!=(const PWL_SCROLL_PRIVATEDATA& that) const { 70 return !(*this == that); 71 } 72 73 void Default(); 74 void SetScrollRange(float min, float max); 75 void SetClientWidth(float width); 76 void SetSmallStep(float step); 77 void SetBigStep(float step); 78 bool SetPos(float pos); 79 80 void AddSmall(); 81 void SubSmall(); 82 void AddBig(); 83 void SubBig(); 84 85 PWL_FLOATRANGE ScrollRange; 86 float fClientWidth; 87 float fScrollPos; 88 float fBigStep; 89 float fSmallStep; 90 }; 91 92 class CPWL_ScrollBar final : public CPWL_Wnd, public CFX_Timer::CallbackIface { 93 public: 94 static constexpr float kWidth = 12.0f; 95 static constexpr uint8_t kTransparency = 150; 96 97 CPWL_ScrollBar( 98 const CreateParams& cp, 99 std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData); 100 ~CPWL_ScrollBar() override; 101 102 // CPWL_Wnd: 103 void OnDestroy() override; 104 bool RepositionChildWnd() override; 105 void DrawThisAppearance(CFX_RenderDevice* pDevice, 106 const CFX_Matrix& mtUser2Device) override; 107 bool OnLButtonDown(Mask<FWL_EVENTFLAG> nFlag, 108 const CFX_PointF& point) override; 109 bool OnLButtonUp(Mask<FWL_EVENTFLAG> nFlag, const CFX_PointF& point) override; 110 void SetScrollInfo(const PWL_SCROLL_INFO& info) override; 111 void SetScrollPosition(float pos) override; 112 void NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos) override; 113 void NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos) override; 114 void NotifyMouseMove(CPWL_Wnd* child, const CFX_PointF& pos) override; 115 void CreateChildWnd(const CreateParams& cp) override; 116 117 // CFX_Timer::CallbackIface: 118 void OnTimerFired() override; 119 120 float GetScrollBarWidth() const; 121 122 private: 123 void SetScrollRange(float fMin, float fMax, float fClientWidth); 124 void SetScrollPos(float fPos); 125 126 // Returns |true| iff this instance is still allocated. 127 [[nodiscard]] bool MovePosButton(bool bRefresh); 128 void SetScrollStep(float fBigStep, float fSmallStep); 129 void NotifyScrollWindow(); 130 CFX_FloatRect GetScrollArea() const; 131 132 void CreateButtons(const CreateParams& cp); 133 134 void OnMinButtonLBDown(const CFX_PointF& point); 135 void OnMinButtonLBUp(const CFX_PointF& point); 136 void OnMinButtonMouseMove(const CFX_PointF& point); 137 138 void OnMaxButtonLBDown(const CFX_PointF& point); 139 void OnMaxButtonLBUp(const CFX_PointF& point); 140 void OnMaxButtonMouseMove(const CFX_PointF& point); 141 142 void OnPosButtonLBDown(const CFX_PointF& point); 143 void OnPosButtonLBUp(const CFX_PointF& point); 144 void OnPosButtonMouseMove(const CFX_PointF& point); 145 146 float TrueToFace(float); 147 float FaceToTrue(float); 148 149 PWL_SCROLL_INFO m_OriginInfo; 150 UnownedPtr<CPWL_SBButton> m_pMinButton; 151 UnownedPtr<CPWL_SBButton> m_pMaxButton; 152 UnownedPtr<CPWL_SBButton> m_pPosButton; 153 std::unique_ptr<CFX_Timer> m_pTimer; 154 PWL_SCROLL_PRIVATEDATA m_sData; 155 bool m_bMouseDown = false; 156 bool m_bMinOrMax = false; 157 float m_nOldPos = 0.0f; 158 float m_fOldPosButton = 0.0f; 159 }; 160 161 #endif // FPDFSDK_PWL_CPWL_SCROLL_BAR_H_ 162