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/fxcrt/check.h"
16#include "core/fxge/cfx_fillrenderoptions.h"
17#include "core/fxge/cfx_path.h"
18#include "core/fxge/cfx_renderdevice.h"
19#include "fpdfsdk/pwl/cpwl_wnd.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 ObservedPtr<CPWL_ScrollBar> this_observed(this);
130 CFX_FloatRect rcClient = this_observed->GetClientRect();
131 CFX_FloatRect rcMinButton;
132 CFX_FloatRect rcMaxButton;
133 if (FXSYS_IsFloatBigger(rcClient.top - rcClient.bottom,
134 kButtonWidth * 2 + kPosButtonMinWidth + 2)) {
135 rcMinButton = CFX_FloatRect(rcClient.left, rcClient.top - kButtonWidth,
136 rcClient.right, rcClient.top);
137 rcMaxButton = CFX_FloatRect(rcClient.left, rcClient.bottom, rcClient.right,
138 rcClient.bottom + kButtonWidth);
139 } else {
140 float fBWidth =
141 (rcClient.top - rcClient.bottom - kPosButtonMinWidth - 2) / 2;
142 if (FXSYS_IsFloatBigger(fBWidth, 0)) {
143 rcMinButton = CFX_FloatRect(rcClient.left, rcClient.top - fBWidth,
144 rcClient.right, rcClient.top);
145 rcMaxButton = CFX_FloatRect(rcClient.left, rcClient.bottom,
146 rcClient.right, rcClient.bottom + fBWidth);
147 } else {
148 if (!this_observed->SetVisible(false)) {
149 return false;
150 }
151 }
152 }
153 if (this_observed->m_pMinButton) {
154 this_observed->m_pMinButton->Move(rcMinButton, true, false);
155 if (!this_observed) {
156 return false;
157 }
158 }
159 if (this_observed->m_pMaxButton) {
160 this_observed->m_pMaxButton->Move(rcMaxButton, true, false);
161 if (!this_observed) {
162 return false;
163 }
164 }
165 return this_observed->MovePosButton(false);
166}
167
168void CPWL_ScrollBar::DrawThisAppearance(CFX_RenderDevice* pDevice,
169 const CFX_Matrix& mtUser2Device) {
170 CFX_FloatRect rectWnd = GetWindowRect();
171
172 if (IsVisible() && !rectWnd.IsEmpty()) {
173 pDevice->DrawFillRect(&mtUser2Device, rectWnd, GetBackgroundColor(),
175
176 pDevice->DrawStrokeLine(
177 &mtUser2Device, CFX_PointF(rectWnd.left + 2.0f, rectWnd.top - 2.0f),
178 CFX_PointF(rectWnd.left + 2.0f, rectWnd.bottom + 2.0f),
179 ArgbEncode(GetTransparency(), 100, 100, 100), 1.0f);
180
181 pDevice->DrawStrokeLine(
182 &mtUser2Device, CFX_PointF(rectWnd.right - 2.0f, rectWnd.top - 2.0f),
183 CFX_PointF(rectWnd.right - 2.0f, rectWnd.bottom + 2.0f),
184 ArgbEncode(GetTransparency(), 100, 100, 100), 1.0f);
185 }
186}
187
188bool CPWL_ScrollBar::OnLButtonDown(Mask<FWL_EVENTFLAG> nFlag,
189 const CFX_PointF& point) {
191
193 if (GetTransparency() != 255) {
194 SetTransparency(255);
195 if (!InvalidateRect(nullptr))
196 return true;
197 }
198 }
199
200 if (m_pPosButton && m_pPosButton->IsVisible()) {
201 CFX_FloatRect rcClient = GetClientRect();
202 CFX_FloatRect rcPosButton = m_pPosButton->GetWindowRect();
203 CFX_FloatRect rcMinArea =
204 CFX_FloatRect(rcClient.left, rcPosButton.top, rcClient.right,
205 rcClient.top - kButtonWidth);
206 CFX_FloatRect rcMaxArea =
207 CFX_FloatRect(rcClient.left, rcClient.bottom + kButtonWidth,
208 rcClient.right, rcPosButton.bottom);
209
210 rcMinArea.Normalize();
211 rcMaxArea.Normalize();
212
213 if (rcMinArea.Contains(point)) {
214 m_sData.SubBig();
215 if (!MovePosButton(true))
216 return true;
217 NotifyScrollWindow();
218 }
219
220 if (rcMaxArea.Contains(point)) {
221 m_sData.AddBig();
222 if (!MovePosButton(true))
223 return true;
224 NotifyScrollWindow();
225 }
226 }
227
228 return true;
229}
230
231bool CPWL_ScrollBar::OnLButtonUp(Mask<FWL_EVENTFLAG> nFlag,
232 const CFX_PointF& point) {
233 CPWL_Wnd::OnLButtonUp(nFlag, point);
234
238 if (!InvalidateRect(nullptr))
239 return true;
240 }
241 }
242
243 m_pTimer.reset();
244 m_bMouseDown = false;
245 return true;
246}
247
248void CPWL_ScrollBar::SetScrollInfo(const PWL_SCROLL_INFO& info) {
249 if (info == m_OriginInfo)
250 return;
251
252 m_OriginInfo = info;
253 float fMax =
254 std::max(0.0f, info.fContentMax - info.fContentMin - info.fPlateWidth);
255 SetScrollRange(0, fMax, info.fPlateWidth);
256 SetScrollStep(info.fBigStep, info.fSmallStep);
257}
258
259void CPWL_ScrollBar::SetScrollPosition(float pos) {
260 pos = m_OriginInfo.fContentMax - pos;
261 SetScrollPos(pos);
262}
263
264void CPWL_ScrollBar::NotifyLButtonDown(CPWL_Wnd* child, const CFX_PointF& pos) {
265 if (child == m_pMinButton)
266 OnMinButtonLBDown(pos);
267 else if (child == m_pMaxButton)
268 OnMaxButtonLBDown(pos);
269 else if (child == m_pPosButton)
270 OnPosButtonLBDown(pos);
271}
272
273void CPWL_ScrollBar::NotifyLButtonUp(CPWL_Wnd* child, const CFX_PointF& pos) {
274 if (child == m_pMinButton)
275 OnMinButtonLBUp(pos);
276 else if (child == m_pMaxButton)
277 OnMaxButtonLBUp(pos);
278 else if (child == m_pPosButton)
279 OnPosButtonLBUp(pos);
280}
281
282void CPWL_ScrollBar::NotifyMouseMove(CPWL_Wnd* child, const CFX_PointF& pos) {
283 if (child == m_pMinButton)
284 OnMinButtonMouseMove(pos);
285 else if (child == m_pMaxButton)
286 OnMaxButtonMouseMove(pos);
287 else if (child == m_pPosButton)
288 OnPosButtonMouseMove(pos);
289}
290
291void CPWL_ScrollBar::CreateButtons(const CreateParams& cp) {
292 ObservedPtr<CPWL_ScrollBar> this_observed(this);
293
294 CreateParams scp = cp;
295 scp.dwBorderWidth = 2;
298
299 if (!this_observed->m_pMinButton) {
300 auto pButton =
301 std::make_unique<CPWL_SBButton>(scp, this_observed->CloneAttachedData(),
302 CPWL_SBButton::Type::kMinButton);
303 this_observed->m_pMinButton = pButton.get();
304 this_observed->AddChild(std::move(pButton));
305 this_observed->m_pMinButton->Realize();
306 }
307 if (!this_observed->m_pMaxButton) {
308 auto pButton =
309 std::make_unique<CPWL_SBButton>(scp, this_observed->CloneAttachedData(),
310 CPWL_SBButton::Type::kMaxButton);
311 this_observed->m_pMaxButton = pButton.get();
312 this_observed->AddChild(std::move(pButton));
313 this_observed->m_pMaxButton->Realize();
314 }
315 if (!this_observed->m_pPosButton) {
316 auto pButton =
317 std::make_unique<CPWL_SBButton>(scp, this_observed->CloneAttachedData(),
318 CPWL_SBButton::Type::kPosButton);
319 this_observed->m_pPosButton = pButton.get();
320 if (this_observed->m_pPosButton->SetVisible(false) && this_observed) {
321 this_observed->AddChild(std::move(pButton));
322 this_observed->m_pPosButton->Realize();
323 }
324 }
325}
326
327float CPWL_ScrollBar::GetScrollBarWidth() const {
328 return IsVisible() ? kWidth : 0.0f;
329}
330
331void CPWL_ScrollBar::SetScrollRange(float fMin,
332 float fMax,
333 float fClientWidth) {
334 ObservedPtr<CPWL_ScrollBar> this_observed(this);
335 if (!this_observed->m_pPosButton) {
336 return;
337 }
338 this_observed->m_sData.SetScrollRange(fMin, fMax);
339 this_observed->m_sData.SetClientWidth(fClientWidth);
340
341 if (FXSYS_IsFloatSmaller(this_observed->m_sData.ScrollRange.GetWidth(),
342 0.0f)) {
343 (void)this_observed->m_pPosButton->SetVisible(false);
344 // Note, |this| may no longer be viable at this point. If more work needs
345 // to be done, check this_observed.
346 return;
347 }
348
349 if (!this_observed->m_pPosButton->SetVisible(true) || !this_observed) {
350 return;
351 }
352
353 (void)this_observed->MovePosButton(true);
354 // Note, |this| may no longer be viable at this point. If more work needs
355 // to be done, check the return value of MovePosButton().
356}
357
358void CPWL_ScrollBar::SetScrollPos(float fPos) {
359 float fOldPos = m_sData.fScrollPos;
360 m_sData.SetPos(fPos);
361 if (!FXSYS_IsFloatEqual(m_sData.fScrollPos, fOldPos)) {
362 (void)MovePosButton(true);
363 // Note, |this| may no longer be viable at this point. If more work needs
364 // to be done, check the return value of MovePosButton().
365 }
366}
367
368void CPWL_ScrollBar::SetScrollStep(float fBigStep, float fSmallStep) {
369 m_sData.SetBigStep(fBigStep);
370 m_sData.SetSmallStep(fSmallStep);
371}
372
373bool CPWL_ScrollBar::MovePosButton(bool bRefresh) {
374 ObservedPtr<CPWL_ScrollBar> this_observed(this);
375
376 DCHECK(m_pMinButton);
377 DCHECK(m_pMaxButton);
378
379 if (this_observed->m_pPosButton->IsVisible()) {
380 CFX_FloatRect rcPosArea = this_observed->GetScrollArea();
381 float fTop = this_observed->TrueToFace(m_sData.fScrollPos);
382 float fBottom =
383 this_observed->TrueToFace(m_sData.fScrollPos + m_sData.fClientWidth);
384 if (FXSYS_IsFloatSmaller(fTop - fBottom, kPosButtonMinWidth)) {
385 fBottom = fTop - kPosButtonMinWidth;
386 }
387 if (FXSYS_IsFloatSmaller(fBottom, rcPosArea.bottom)) {
388 fBottom = rcPosArea.bottom;
389 fTop = fBottom + kPosButtonMinWidth;
390 }
391
392 CFX_FloatRect rcPosButton =
393 CFX_FloatRect(rcPosArea.left, fBottom, rcPosArea.right, fTop);
394
395 this_observed->m_pPosButton->Move(rcPosButton, true, bRefresh);
396 if (!this_observed) {
397 return false;
398 }
399 }
400 return true;
401}
402
403void CPWL_ScrollBar::OnMinButtonLBDown(const CFX_PointF& point) {
404 m_sData.SubSmall();
405 if (!MovePosButton(true))
406 return;
407
408 NotifyScrollWindow();
409 m_bMinOrMax = true;
410 m_pTimer = std::make_unique<CFX_Timer>(GetTimerHandler(), this, 100);
411}
412
413void CPWL_ScrollBar::OnMinButtonLBUp(const CFX_PointF& point) {}
414
415void CPWL_ScrollBar::OnMinButtonMouseMove(const CFX_PointF& point) {}
416
417void CPWL_ScrollBar::OnMaxButtonLBDown(const CFX_PointF& point) {
418 m_sData.AddSmall();
419 if (!MovePosButton(true))
420 return;
421
422 NotifyScrollWindow();
423 m_bMinOrMax = false;
424 m_pTimer = std::make_unique<CFX_Timer>(GetTimerHandler(), this, 100);
425}
426
427void CPWL_ScrollBar::OnMaxButtonLBUp(const CFX_PointF& point) {}
428
429void CPWL_ScrollBar::OnMaxButtonMouseMove(const CFX_PointF& point) {}
430
431void CPWL_ScrollBar::OnPosButtonLBDown(const CFX_PointF& point) {
432 m_bMouseDown = true;
433
434 if (m_pPosButton) {
435 CFX_FloatRect rcPosButton = m_pPosButton->GetWindowRect();
436 m_nOldPos = point.y;
437 m_fOldPosButton = rcPosButton.top;
438 }
439}
440
441void CPWL_ScrollBar::OnPosButtonLBUp(const CFX_PointF& point) {
442 m_bMouseDown = false;
443}
444
445void CPWL_ScrollBar::OnPosButtonMouseMove(const CFX_PointF& point) {
446 if (fabs(point.y - m_nOldPos) < 1)
447 return;
448
449 float fOldScrollPos = m_sData.fScrollPos;
450 float fNewPos = FaceToTrue(m_fOldPosButton + point.y - m_nOldPos);
451 if (m_bMouseDown) {
452 if (FXSYS_IsFloatSmaller(fNewPos, m_sData.ScrollRange.fMin)) {
453 fNewPos = m_sData.ScrollRange.fMin;
454 }
455
456 if (FXSYS_IsFloatBigger(fNewPos, m_sData.ScrollRange.fMax)) {
457 fNewPos = m_sData.ScrollRange.fMax;
458 }
459
460 m_sData.SetPos(fNewPos);
461
462 if (!FXSYS_IsFloatEqual(fOldScrollPos, m_sData.fScrollPos)) {
463 if (!MovePosButton(true))
464 return;
465
466 NotifyScrollWindow();
467 }
468 }
469}
470
471void CPWL_ScrollBar::NotifyScrollWindow() {
472 CPWL_Wnd* pParent = GetParentWindow();
473 if (!pParent)
474 return;
475
476 pParent->ScrollWindowVertically(m_OriginInfo.fContentMax -
477 m_sData.fScrollPos);
478}
479
480CFX_FloatRect CPWL_ScrollBar::GetScrollArea() const {
481 CFX_FloatRect rcClient = GetClientRect();
482 if (!m_pMinButton || !m_pMaxButton)
483 return rcClient;
484
485 CFX_FloatRect rcMin = m_pMinButton->GetWindowRect();
486 CFX_FloatRect rcMax = m_pMaxButton->GetWindowRect();
487 float fMinHeight = rcMin.Height();
488 float fMaxHeight = rcMax.Height();
489
490 CFX_FloatRect rcArea;
491 if (rcClient.top - rcClient.bottom > fMinHeight + fMaxHeight + 2) {
492 rcArea = CFX_FloatRect(rcClient.left, rcClient.bottom + fMinHeight + 1,
493 rcClient.right, rcClient.top - fMaxHeight - 1);
494 } else {
495 rcArea = CFX_FloatRect(rcClient.left, rcClient.bottom + fMinHeight + 1,
496 rcClient.right, rcClient.bottom + fMinHeight + 1);
497 }
498
499 rcArea.Normalize();
500 return rcArea;
501}
502
503float CPWL_ScrollBar::TrueToFace(float fTrue) {
504 CFX_FloatRect rcPosArea = GetScrollArea();
505 float fFactWidth = m_sData.ScrollRange.GetWidth() + m_sData.fClientWidth;
506 fFactWidth = fFactWidth == 0 ? 1 : fFactWidth;
507 return rcPosArea.top -
508 fTrue * (rcPosArea.top - rcPosArea.bottom) / fFactWidth;
509}
510
511float CPWL_ScrollBar::FaceToTrue(float fFace) {
512 CFX_FloatRect rcPosArea = GetScrollArea();
513 float fFactWidth = m_sData.ScrollRange.GetWidth() + m_sData.fClientWidth;
514 fFactWidth = fFactWidth == 0 ? 1 : fFactWidth;
515 return (rcPosArea.top - fFace) * fFactWidth /
516 (rcPosArea.top - rcPosArea.bottom);
517}
518
519void CPWL_ScrollBar::CreateChildWnd(const CreateParams& cp) {
520 CreateButtons(cp);
521}
522
523void CPWL_ScrollBar::OnTimerFired() {
524 PWL_SCROLL_PRIVATEDATA sTemp = m_sData;
525 if (m_bMinOrMax)
526 m_sData.SubSmall();
527 else
528 m_sData.AddSmall();
529
530 if (sTemp == m_sData)
531 return;
532
533 if (!MovePosButton(true))
534 return;
535
536 NotifyScrollWindow();
537}
BorderStyle
#define DCHECK
Definition check.h:33
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:433
CFX_Color GetBackgroundColor() const
Definition cpwl_wnd.cpp:471
bool HasFlag(uint32_t dwFlags) const
Definition cpwl_wnd.cpp:463
virtual CFX_FloatRect GetClientRect() const
Definition cpwl_wnd.cpp:445
CFX_FloatRect GetWindowRect() const
Definition cpwl_wnd.cpp:441
CPWL_Wnd * GetParentWindow() const
Definition cpwl_wnd.h:249
void SetTransparency(int32_t nTransparency)
Definition cpwl_wnd.cpp:729
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
int32_t GetTransparency()
Definition cpwl_wnd.cpp:725
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
CFX_PTemplate< float > CFX_PointF
#define FXSYS_IsFloatBigger(fa, fb)
Definition fx_system.h:37
#define FXSYS_IsFloatEqual(fa, fb)
Definition fx_system.h:41
#define FXSYS_IsFloatSmaller(fa, fb)
Definition fx_system.h:39
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