• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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_wnd.h"
15 
16 struct PWL_SCROLL_INFO {
17  public:
PWL_SCROLL_INFOPWL_SCROLL_INFO18   PWL_SCROLL_INFO()
19       : fContentMin(0.0f),
20         fContentMax(0.0f),
21         fPlateWidth(0.0f),
22         fBigStep(0.0f),
23         fSmallStep(0.0f) {}
24 
25   bool operator==(const PWL_SCROLL_INFO& that) const {
26     return fContentMin == that.fContentMin && fContentMax == that.fContentMax &&
27            fPlateWidth == that.fPlateWidth && fBigStep == that.fBigStep &&
28            fSmallStep == that.fSmallStep;
29   }
30   bool operator!=(const PWL_SCROLL_INFO& that) const {
31     return !(*this == that);
32   }
33 
34   float fContentMin;
35   float fContentMax;
36   float fPlateWidth;
37   float fBigStep;
38   float fSmallStep;
39 };
40 
41 enum PWL_SCROLLBAR_TYPE { SBT_HSCROLL, SBT_VSCROLL };
42 
43 enum PWL_SBBUTTON_TYPE { PSBT_MIN, PSBT_MAX, PSBT_POS };
44 
45 class CPWL_SBButton final : public CPWL_Wnd {
46  public:
47   CPWL_SBButton(
48       const CreateParams& cp,
49       std::unique_ptr<IPWL_SystemHandler::PerWindowData> pAttachedData,
50       PWL_SCROLLBAR_TYPE eScrollBarType,
51       PWL_SBBUTTON_TYPE eButtonType);
52   ~CPWL_SBButton() override;
53 
54   // CPWL_Wnd
55   void DrawThisAppearance(CFX_RenderDevice* pDevice,
56                           const CFX_Matrix& mtUser2Device) override;
57   bool OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) override;
58   bool OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) override;
59   bool OnMouseMove(const CFX_PointF& point, uint32_t nFlag) override;
60 
61  private:
62   PWL_SCROLLBAR_TYPE m_eScrollBarType;
63   PWL_SBBUTTON_TYPE m_eSBButtonType;
64   bool m_bMouseDown = false;
65 };
66 
67 struct PWL_FLOATRANGE {
68  public:
69   PWL_FLOATRANGE() = default;
70 
71   bool operator==(const PWL_FLOATRANGE& that) const {
72     return fMin == that.fMin && fMax == that.fMax;
73   }
74   bool operator!=(const PWL_FLOATRANGE& that) const { return !(*this == that); }
75 
76   void Reset();
77   void Set(float min, float max);
78   bool In(float x) const;
79   float GetWidth() const;
80 
81   float fMin = 0.0f;
82   float fMax = 0.0f;
83 };
84 
85 struct PWL_SCROLL_PRIVATEDATA {
86  public:
87   PWL_SCROLL_PRIVATEDATA();
88 
89   bool operator==(const PWL_SCROLL_PRIVATEDATA& that) const {
90     return ScrollRange == that.ScrollRange &&
91            fClientWidth == that.fClientWidth && fScrollPos == that.fScrollPos &&
92            fBigStep == that.fBigStep && fSmallStep == that.fSmallStep;
93   }
94   bool operator!=(const PWL_SCROLL_PRIVATEDATA& that) const {
95     return !(*this == that);
96   }
97 
98   void Default();
99   void SetScrollRange(float min, float max);
100   void SetClientWidth(float width);
101   void SetSmallStep(float step);
102   void SetBigStep(float step);
103   bool SetPos(float pos);
104 
105   void AddSmall();
106   void SubSmall();
107   void AddBig();
108   void SubBig();
109 
110   PWL_FLOATRANGE ScrollRange;
111   float fClientWidth;
112   float fScrollPos;
113   float fBigStep;
114   float fSmallStep;
115 };
116 
117 class CPWL_ScrollBar final : public CPWL_Wnd, public CFX_Timer::CallbackIface {
118  public:
119   CPWL_ScrollBar(
120       const CreateParams& cp,
121       std::unique_ptr<IPWL_SystemHandler::PerWindowData> pAttachedData,
122       PWL_SCROLLBAR_TYPE sbType);
123   ~CPWL_ScrollBar() override;
124 
125   // CPWL_Wnd:
126   void OnDestroy() override;
127   bool RePosChildWnd() override;
128   void DrawThisAppearance(CFX_RenderDevice* pDevice,
129                           const CFX_Matrix& mtUser2Device) override;
130   bool OnLButtonDown(const CFX_PointF& point, uint32_t nFlag) override;
131   bool OnLButtonUp(const CFX_PointF& point, uint32_t nFlag) override;
132   void SetScrollInfo(const PWL_SCROLL_INFO& info) override;
133   void SetScrollPosition(float pos) override;
134   void NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos) override;
135   void NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos) override;
136   void NotifyMouseMove(CPWL_Wnd* child, const CFX_PointF& pos) override;
137   void CreateChildWnd(const CreateParams& cp) override;
138 
139   // CFX_Timer::CallbackIface:
140   void OnTimerFired() override;
141 
142   float GetScrollBarWidth() const;
GetScrollBarType()143   PWL_SCROLLBAR_TYPE GetScrollBarType() const { return m_sbType; }
144 
SetNotifyForever(bool bForever)145   void SetNotifyForever(bool bForever) { m_bNotifyForever = bForever; }
146 
147  private:
148   void SetScrollRange(float fMin, float fMax, float fClientWidth);
149   void SetScrollPos(float fPos);
150 
151   // Returns |true| iff this instance is still allocated.
152   bool MovePosButton(bool bRefresh);
153   void SetScrollStep(float fBigStep, float fSmallStep);
154   void NotifyScrollWindow();
155   CFX_FloatRect GetScrollArea() const;
156 
157   void CreateButtons(const CreateParams& cp);
158 
159   void OnMinButtonLBDown(const CFX_PointF& point);
160   void OnMinButtonLBUp(const CFX_PointF& point);
161   void OnMinButtonMouseMove(const CFX_PointF& point);
162 
163   void OnMaxButtonLBDown(const CFX_PointF& point);
164   void OnMaxButtonLBUp(const CFX_PointF& point);
165   void OnMaxButtonMouseMove(const CFX_PointF& point);
166 
167   void OnPosButtonLBDown(const CFX_PointF& point);
168   void OnPosButtonLBUp(const CFX_PointF& point);
169   void OnPosButtonMouseMove(const CFX_PointF& point);
170 
171   float TrueToFace(float);
172   float FaceToTrue(float);
173 
174   PWL_SCROLLBAR_TYPE m_sbType;
175   PWL_SCROLL_INFO m_OriginInfo;
176   UnownedPtr<CPWL_SBButton> m_pMinButton;
177   UnownedPtr<CPWL_SBButton> m_pMaxButton;
178   UnownedPtr<CPWL_SBButton> m_pPosButton;
179   std::unique_ptr<CFX_Timer> m_pTimer;
180   PWL_SCROLL_PRIVATEDATA m_sData;
181   bool m_bMouseDown = false;
182   bool m_bMinOrMax = false;
183   bool m_bNotifyForever = true;
184   float m_nOldPos = 0.0f;
185   float m_fOldPosButton = 0.0f;
186 };
187 
188 #endif  // FPDFSDK_PWL_CPWL_SCROLL_BAR_H_
189