Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpwl_scroll_bar.cpp
Go to the documentation of this file.
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#include "fpdfsdk/pwl/cpwl_scroll_bar.h"
8
9#include <math.h>
10
11#include <algorithm>
12#include <sstream>
13#include <utility>
14
15#include "core/fxge/cfx_fillrenderoptions.h"
16#include "core/fxge/cfx_path.h"
17#include "core/fxge/cfx_renderdevice.h"
18#include "fpdfsdk/pwl/cpwl_wnd.h"
19#include "third_party/base/check.h"
20
21namespace {
22
23constexpr float kButtonWidth = 9.0f;
24constexpr float kPosButtonMinWidth = 2.0f;
25
26} // namespace
27
29 fMin = 0.0f;
30 fMax = 0.0f;
31}
32
33void PWL_FLOATRANGE::Set(float min, float max) {
34 fMin = std::min(min, max);
35 fMax = std::max(min, max);
36}
37
38bool PWL_FLOATRANGE::In(float x) const {
39 return (FXSYS_IsFloatBigger(x, fMin) || FXSYS_IsFloatEqual(x, fMin)) &&
41}
42
43float PWL_FLOATRANGE::GetWidth() const {
44 return fMax - fMin;
45}
46
50
58
67
69 fClientWidth = width;
70}
71
73 fSmallStep = step;
74}
75
77 fBigStep = step;
78}
79
81 if (ScrollRange.In(pos)) {
82 fScrollPos = pos;
83 return true;
84 }
85 return false;
86}
87
92
97
102
107
108CPWL_ScrollBar::CPWL_ScrollBar(
109 const CreateParams& cp,
110 std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData)
111 : CPWL_Wnd(cp, std::move(pAttachedData)) {
113}
114
115CPWL_ScrollBar::~CPWL_ScrollBar() = default;
116
117void CPWL_ScrollBar::OnDestroy() {
118 // Until cleanup takes place in the virtual destructor for CPWL_Wnd
119 // subclasses, implement the virtual OnDestroy method that does the
120 // cleanup first, then invokes the superclass OnDestroy ... gee,
121 // like a dtor would.
122 m_pMinButton.ExtractAsDangling();
123 m_pMaxButton.ExtractAsDangling();
124 m_pPosButton.ExtractAsDangling();
126}
127
128bool CPWL_ScrollBar::RepositionChildWnd() {
129 CFX_FloatRect rcClient = GetClientRect();
130 CFX_FloatRect rcMinButton;
131 CFX_FloatRect rcMaxButton;
132 if (FXSYS_IsFloatBigger(rcClient.top - rcClient.bottom,
133 kButtonWidth * 2 + kPosButtonMinWidth + 2)) {
134 rcMinButton = CFX_FloatRect(rcClient.left, rcClient.top - kButtonWidth,
135 rcClient.right, rcClient.top);
136 rcMaxButton = CFX_FloatRect(rcClient.left, rcClient.bottom, rcClient.right,
137 rcClient.bottom + kButtonWidth);
138 } else {
139 float fBWidth =
140 (rcClient.top - rcClient.bottom - kPosButtonMinWidth - 2) / 2;
141 if (FXSYS_IsFloatBigger(fBWidth, 0)) {
142 rcMinButton = CFX_FloatRect(rcClient.left, rcClient.top - fBWidth,
143 rcClient.right, rcClient.top);
144 rcMaxButton = CFX_FloatRect(rcClient.left, rcClient.bottom,
145 rcClient.right, rcClient.bottom + fBWidth);
146 } else {
147 if (!SetVisible(false))
148 return false;
149 }
150 }
151
152 ObservedPtr<CPWL_ScrollBar> this_observed(this);
153 if (m_pMinButton) {
154 m_pMinButton->Move(rcMinButton, true, false);
155 if (!this_observed) {
156 return false;
157 }
158 }
159 if (m_pMaxButton) {
160 m_pMaxButton->Move(rcMaxButton, true, false);
161 if (!this_observed) {
162 return false;
163 }
164 }
165
166 return MovePosButton(false);
167}
168
169void CPWL_ScrollBar::DrawThisAppearance(CFX_RenderDevice* pDevice,
170 const CFX_Matrix& mtUser2Device) {
171 CFX_FloatRect rectWnd = GetWindowRect();
172
173 if (IsVisible() && !rectWnd.IsEmpty()) {
174 pDevice->DrawFillRect(&mtUser2Device, rectWnd, GetBackgroundColor(),
176
177 pDevice->DrawStrokeLine(
178 &mtUser2Device, CFX_PointF(rectWnd.left + 2.0f, rectWnd.top - 2.0f),
179 CFX_PointF(rectWnd.left + 2.0f, rectWnd.bottom + 2.0f),
180 ArgbEncode(GetTransparency(), 100, 100, 100), 1.0f);
181
182 pDevice->DrawStrokeLine(
183 &mtUser2Device, CFX_PointF(rectWnd.right - 2.0f, rectWnd.top - 2.0f),
184 CFX_PointF(rectWnd.right - 2.0f, rectWnd.bottom + 2.0f),
185 ArgbEncode(GetTransparency(), 100, 100, 100), 1.0f);
186 }
187}
188
189bool CPWL_ScrollBar::OnLButtonDown(Mask<FWL_EVENTFLAG> nFlag,
190 const CFX_PointF& point) {
192
194 if (GetTransparency() != 255) {
195 SetTransparency(255);
196 if (!InvalidateRect(nullptr))
197 return true;
198 }
199 }
200
201 if (m_pPosButton && m_pPosButton->IsVisible()) {
202 CFX_FloatRect rcClient = GetClientRect();
203 CFX_FloatRect rcPosButton = m_pPosButton->GetWindowRect();
204 CFX_FloatRect rcMinArea =
205 CFX_FloatRect(rcClient.left, rcPosButton.top, rcClient.right,
206 rcClient.top - kButtonWidth);
207 CFX_FloatRect rcMaxArea =
208 CFX_FloatRect(rcClient.left, rcClient.bottom + kButtonWidth,
209 rcClient.right, rcPosButton.bottom);
210
211 rcMinArea.Normalize();
212 rcMaxArea.Normalize();
213
214 if (rcMinArea.Contains(point)) {
215 m_sData.SubBig();
216 if (!MovePosButton(true))
217 return true;
218 NotifyScrollWindow();
219 }
220
221 if (rcMaxArea.Contains(point)) {
222 m_sData.AddBig();
223 if (!MovePosButton(true))
224 return true;
225 NotifyScrollWindow();
226 }
227 }
228
229 return true;
230}
231
232bool CPWL_ScrollBar::OnLButtonUp(Mask<FWL_EVENTFLAG> nFlag,
233 const CFX_PointF& point) {
234 CPWL_Wnd::OnLButtonUp(nFlag, point);
235
239 if (!InvalidateRect(nullptr))
240 return true;
241 }
242 }
243
244 m_pTimer.reset();
245 m_bMouseDown = false;
246 return true;
247}
248
249void CPWL_ScrollBar::SetScrollInfo(const PWL_SCROLL_INFO& info) {
250 if (info == m_OriginInfo)
251 return;
252
253 m_OriginInfo = info;
254 float fMax =
255 std::max(0.0f, info.fContentMax - info.fContentMin - info.fPlateWidth);
256 SetScrollRange(0, fMax, info.fPlateWidth);
257 SetScrollStep(info.fBigStep, info.fSmallStep);
258}
259
260void CPWL_ScrollBar::SetScrollPosition(float pos) {
261 pos = m_OriginInfo.fContentMax - pos;
262 SetScrollPos(pos);
263}
264
265void CPWL_ScrollBar::NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos) {
266 if (child == m_pMinButton)
267 OnMinButtonLBDown(pos);
268 else if (child == m_pMaxButton)
269 OnMaxButtonLBDown(pos);
270 else if (child == m_pPosButton)
271 OnPosButtonLBDown(pos);
272}
273
274void CPWL_ScrollBar::NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos) {
275 if (child == m_pMinButton)
276 OnMinButtonLBUp(pos);
277 else if (child == m_pMaxButton)
278 OnMaxButtonLBUp(pos);
279 else if (child == m_pPosButton)
280 OnPosButtonLBUp(pos);
281}
282
283void CPWL_ScrollBar::NotifyMouseMove(CPWL_Wnd* child, const CFX_PointF& pos) {
284 if (child == m_pMinButton)
285 OnMinButtonMouseMove(pos);
286 else if (child == m_pMaxButton)
287 OnMaxButtonMouseMove(pos);
288 else if (child == m_pPosButton)
289 OnPosButtonMouseMove(pos);
290}
291
292void CPWL_ScrollBar::CreateButtons(const CreateParams& cp) {
293 CreateParams scp = cp;
294 scp.dwBorderWidth = 2;
297
298 if (!m_pMinButton) {
299 auto pButton = std::make_unique<CPWL_SBButton>(
300 scp, CloneAttachedData(), CPWL_SBButton::Type::kMinButton);
301 m_pMinButton = pButton.get();
302 AddChild(std::move(pButton));
303 m_pMinButton->Realize();
304 }
305
306 if (!m_pMaxButton) {
307 auto pButton = std::make_unique<CPWL_SBButton>(
308 scp, CloneAttachedData(), CPWL_SBButton::Type::kMaxButton);
309 m_pMaxButton = pButton.get();
310 AddChild(std::move(pButton));
311 m_pMaxButton->Realize();
312 }
313
314 if (!m_pPosButton) {
315 auto pButton = std::make_unique<CPWL_SBButton>(
316 scp, CloneAttachedData(), CPWL_SBButton::Type::kPosButton);
317 m_pPosButton = pButton.get();
318 ObservedPtr<CPWL_ScrollBar> this_observed(this);
319 if (m_pPosButton->SetVisible(false) && this_observed) {
320 AddChild(std::move(pButton));
321 m_pPosButton->Realize();
322 }
323 }
324}
325
326float CPWL_ScrollBar::GetScrollBarWidth() const {
327 return IsVisible() ? kWidth : 0.0f;
328}
329
330void CPWL_ScrollBar::SetScrollRange(float fMin,
331 float fMax,
332 float fClientWidth) {
333 if (!m_pPosButton)
334 return;
335
336 ObservedPtr<CPWL_ScrollBar> this_observed(this);
337 m_sData.SetScrollRange(fMin, fMax);
338 m_sData.SetClientWidth(fClientWidth);
339
341 (void)m_pPosButton->SetVisible(false);
342 // Note, |this| may no longer be viable at this point. If more work needs
343 // to be done, check this_observed.
344 return;
345 }
346
347 if (!m_pPosButton->SetVisible(true) || !this_observed) {
348 return;
349 }
350
351 (void)MovePosButton(true);
352 // Note, |this| may no longer be viable at this point. If more work needs
353 // to be done, check the return value of MovePosButton().
354}
355
356void CPWL_ScrollBar::SetScrollPos(float fPos) {
357 float fOldPos = m_sData.fScrollPos;
358 m_sData.SetPos(fPos);
359 if (!FXSYS_IsFloatEqual(m_sData.fScrollPos, fOldPos)) {
360 (void)MovePosButton(true);
361 // Note, |this| may no longer be viable at this point. If more work needs
362 // to be done, check the return value of MovePosButton().
363 }
364}
365
366void CPWL_ScrollBar::SetScrollStep(float fBigStep, float fSmallStep) {
367 m_sData.SetBigStep(fBigStep);
368 m_sData.SetSmallStep(fSmallStep);
369}
370
371bool CPWL_ScrollBar::MovePosButton(bool bRefresh) {
372 DCHECK(m_pMinButton);
373 DCHECK(m_pMaxButton);
374
375 if (m_pPosButton->IsVisible()) {
376 CFX_FloatRect rcPosArea = GetScrollArea();
377 float fBottom = TrueToFace(m_sData.fScrollPos + m_sData.fClientWidth);
378 float fTop = TrueToFace(m_sData.fScrollPos);
379
380 if (FXSYS_IsFloatSmaller(fTop - fBottom, kPosButtonMinWidth))
381 fBottom = fTop - kPosButtonMinWidth;
382
383 if (FXSYS_IsFloatSmaller(fBottom, rcPosArea.bottom)) {
384 fBottom = rcPosArea.bottom;
385 fTop = fBottom + kPosButtonMinWidth;
386 }
387
388 CFX_FloatRect rcPosButton =
389 CFX_FloatRect(rcPosArea.left, fBottom, rcPosArea.right, fTop);
390
391 ObservedPtr<CPWL_ScrollBar> this_observed(this);
392 m_pPosButton->Move(rcPosButton, true, bRefresh);
393 if (!this_observed) {
394 return false;
395 }
396 }
397
398 return true;
399}
400
401void CPWL_ScrollBar::OnMinButtonLBDown(const CFX_PointF& point) {
402 m_sData.SubSmall();
403 if (!MovePosButton(true))
404 return;
405
406 NotifyScrollWindow();
407 m_bMinOrMax = true;
408 m_pTimer = std::make_unique<CFX_Timer>(GetTimerHandler(), this, 100);
409}
410
411void CPWL_ScrollBar::OnMinButtonLBUp(const CFX_PointF& point) {}
412
413void CPWL_ScrollBar::OnMinButtonMouseMove(const CFX_PointF& point) {}
414
415void CPWL_ScrollBar::OnMaxButtonLBDown(const CFX_PointF& point) {
416 m_sData.AddSmall();
417 if (!MovePosButton(true))
418 return;
419
420 NotifyScrollWindow();
421 m_bMinOrMax = false;
422 m_pTimer = std::make_unique<CFX_Timer>(GetTimerHandler(), this, 100);
423}
424
425void CPWL_ScrollBar::OnMaxButtonLBUp(const CFX_PointF& point) {}
426
427void CPWL_ScrollBar::OnMaxButtonMouseMove(const CFX_PointF& point) {}
428
429void CPWL_ScrollBar::OnPosButtonLBDown(const CFX_PointF& point) {
430 m_bMouseDown = true;
431
432 if (m_pPosButton) {
433 CFX_FloatRect rcPosButton = m_pPosButton->GetWindowRect();
434 m_nOldPos = point.y;
435 m_fOldPosButton = rcPosButton.top;
436 }
437}
438
439void CPWL_ScrollBar::OnPosButtonLBUp(const CFX_PointF& point) {
440 m_bMouseDown = false;
441}
442
443void CPWL_ScrollBar::OnPosButtonMouseMove(const CFX_PointF& point) {
444 if (fabs(point.y - m_nOldPos) < 1)
445 return;
446
447 float fOldScrollPos = m_sData.fScrollPos;
448 float fNewPos = FaceToTrue(m_fOldPosButton + point.y - m_nOldPos);
449 if (m_bMouseDown) {
450 if (FXSYS_IsFloatSmaller(fNewPos, m_sData.ScrollRange.fMin)) {
451 fNewPos = m_sData.ScrollRange.fMin;
452 }
453
454 if (FXSYS_IsFloatBigger(fNewPos, m_sData.ScrollRange.fMax)) {
455 fNewPos = m_sData.ScrollRange.fMax;
456 }
457
458 m_sData.SetPos(fNewPos);
459
460 if (!FXSYS_IsFloatEqual(fOldScrollPos, m_sData.fScrollPos)) {
461 if (!MovePosButton(true))
462 return;
463
464 NotifyScrollWindow();
465 }
466 }
467}
468
469void CPWL_ScrollBar::NotifyScrollWindow() {
470 CPWL_Wnd* pParent = GetParentWindow();
471 if (!pParent)
472 return;
473
474 pParent->ScrollWindowVertically(m_OriginInfo.fContentMax -
475 m_sData.fScrollPos);
476}
477
478CFX_FloatRect CPWL_ScrollBar::GetScrollArea() const {
479 CFX_FloatRect rcClient = GetClientRect();
480 if (!m_pMinButton || !m_pMaxButton)
481 return rcClient;
482
483 CFX_FloatRect rcMin = m_pMinButton->GetWindowRect();
484 CFX_FloatRect rcMax = m_pMaxButton->GetWindowRect();
485 float fMinHeight = rcMin.Height();
486 float fMaxHeight = rcMax.Height();
487
488 CFX_FloatRect rcArea;
489 if (rcClient.top - rcClient.bottom > fMinHeight + fMaxHeight + 2) {
490 rcArea = CFX_FloatRect(rcClient.left, rcClient.bottom + fMinHeight + 1,
491 rcClient.right, rcClient.top - fMaxHeight - 1);
492 } else {
493 rcArea = CFX_FloatRect(rcClient.left, rcClient.bottom + fMinHeight + 1,
494 rcClient.right, rcClient.bottom + fMinHeight + 1);
495 }
496
497 rcArea.Normalize();
498 return rcArea;
499}
500
501float CPWL_ScrollBar::TrueToFace(float fTrue) {
502 CFX_FloatRect rcPosArea = GetScrollArea();
503 float fFactWidth = m_sData.ScrollRange.GetWidth() + m_sData.fClientWidth;
504 fFactWidth = fFactWidth == 0 ? 1 : fFactWidth;
505 return rcPosArea.top -
506 fTrue * (rcPosArea.top - rcPosArea.bottom) / fFactWidth;
507}
508
509float CPWL_ScrollBar::FaceToTrue(float fFace) {
510 CFX_FloatRect rcPosArea = GetScrollArea();
511 float fFactWidth = m_sData.ScrollRange.GetWidth() + m_sData.fClientWidth;
512 fFactWidth = fFactWidth == 0 ? 1 : fFactWidth;
513 return (rcPosArea.top - fFace) * fFactWidth /
514 (rcPosArea.top - rcPosArea.bottom);
515}
516
517void CPWL_ScrollBar::CreateChildWnd(const CreateParams& cp) {
518 CreateButtons(cp);
519}
520
521void CPWL_ScrollBar::OnTimerFired() {
522 PWL_SCROLL_PRIVATEDATA sTemp = m_sData;
523 if (m_bMinOrMax)
524 m_sData.SubSmall();
525 else
526 m_sData.AddSmall();
527
528 if (sTemp == m_sData)
529 return;
530
531 if (!MovePosButton(true))
532 return;
533
534 NotifyScrollWindow();
535}
BorderStyle
bool Contains(const CFX_PointF &point) const
constexpr CFX_FloatRect(float l, float b, float r, float t)
bool IsEmpty() const
CFX_FloatRect & operator=(const CFX_FloatRect &that)=default
float Height() const
void DrawStrokeLine(const CFX_Matrix *pUser2Device, const CFX_PointF &ptMoveTo, const CFX_PointF &ptLineTo, const FX_COLORREF &color, float fWidth)
static constexpr uint8_t kTransparency
static constexpr float kWidth
bool OnLButtonUp(Mask< FWL_EVENTFLAG > nFlag, const CFX_PointF &point) override
void OnDestroy() override
void NotifyLButtonUp(CPWL_Wnd *child, const CFX_PointF &pos) override
CPWL_ScrollBar(const CreateParams &cp, std::unique_ptr< IPWL_FillerNotify::PerWindowData > pAttachedData)
void SetScrollPosition(float pos) override
float GetScrollBarWidth() const
void CreateChildWnd(const CreateParams &cp) override
bool OnLButtonDown(Mask< FWL_EVENTFLAG > nFlag, const CFX_PointF &point) override
void OnTimerFired() override
void DrawThisAppearance(CFX_RenderDevice *pDevice, const CFX_Matrix &mtUser2Device) override
void NotifyLButtonDown(CPWL_Wnd *child, const CFX_PointF &pos) override
void NotifyMouseMove(CPWL_Wnd *child, const CFX_PointF &pos) override
void SetScrollInfo(const PWL_SCROLL_INFO &info) override
~CPWL_ScrollBar() override
bool RepositionChildWnd() override
BorderStyle nBorderStyle
Definition cpwl_wnd.h:114
virtual void ScrollWindowVertically(float pos)
Definition cpwl_wnd.cpp:432
CFX_Color GetBackgroundColor() const
Definition cpwl_wnd.cpp:470
bool HasFlag(uint32_t dwFlags) const
Definition cpwl_wnd.cpp:462
virtual CFX_FloatRect GetClientRect() const
Definition cpwl_wnd.cpp:444
CFX_FloatRect GetWindowRect() const
Definition cpwl_wnd.cpp:440
CPWL_Wnd * GetParentWindow() const
Definition cpwl_wnd.h:249
void SetTransparency(int32_t nTransparency)
Definition cpwl_wnd.cpp:732
virtual bool OnLButtonUp(Mask< FWL_EVENTFLAG > nFlag, const CFX_PointF &point)
CreateParams * GetCreationParams()
Definition cpwl_wnd.h:238
virtual bool InvalidateRect(const CFX_FloatRect *pRect)
Definition cpwl_wnd.cpp:278
bool IsVisible() const
Definition cpwl_wnd.h:204
virtual void OnDestroy()
Definition cpwl_wnd.cpp:187
virtual bool SetVisible(bool bVisible)
Definition cpwl_wnd.cpp:575
int32_t GetTransparency()
Definition cpwl_wnd.cpp:728
virtual bool OnLButtonDown(Mask< FWL_EVENTFLAG > nFlag, const CFX_PointF &point)
#define PWS_BACKGROUND
Definition cpwl_wnd.h:31
#define PWS_VISIBLE
Definition cpwl_wnd.h:33
#define PWS_BORDER
Definition cpwl_wnd.h:30
#define PWS_AUTOTRANSPARENT
Definition cpwl_wnd.h:36
#define PWS_NOREFRESHCLIP
Definition cpwl_wnd.h:37
#define FXSYS_IsFloatBigger(fa, fb)
Definition fx_system.h:36
#define FXSYS_IsFloatEqual(fa, fb)
Definition fx_system.h:40
#define FXSYS_IsFloatSmaller(fa, fb)
Definition fx_system.h:38
float GetWidth() const
bool In(float x) const
void Set(float min, float max)
bool operator==(const PWL_SCROLL_INFO &that) const
void SetScrollRange(float min, float max)
void SetBigStep(float step)
void SetClientWidth(float width)
PWL_FLOATRANGE ScrollRange
void SetSmallStep(float step)
bool operator==(const PWL_SCROLL_PRIVATEDATA &that) const