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
21 namespace {
22
23 constexpr float kButtonWidth = 9.0f;
24 constexpr float kPosButtonMinWidth = 2.0f;
25
26 } // namespace
27
Reset()28 void PWL_FLOATRANGE::Reset() {
29 fMin = 0.0f;
30 fMax = 0.0f;
31 }
32
Set(float min,float max)33 void PWL_FLOATRANGE::Set(float min, float max) {
34 fMin = std::min(min, max);
35 fMax = std::max(min, max);
36 }
37
In(float x) const38 bool PWL_FLOATRANGE::In(float x) const {
39 return (FXSYS_IsFloatBigger(x, fMin) || FXSYS_IsFloatEqual(x, fMin)) &&
40 (FXSYS_IsFloatSmaller(x, fMax) || FXSYS_IsFloatEqual(x, fMax));
41 }
42
GetWidth() const43 float PWL_FLOATRANGE::GetWidth() const {
44 return fMax - fMin;
45 }
46
PWL_SCROLL_PRIVATEDATA()47 PWL_SCROLL_PRIVATEDATA::PWL_SCROLL_PRIVATEDATA() {
48 Default();
49 }
50
Default()51 void PWL_SCROLL_PRIVATEDATA::Default() {
52 ScrollRange.Reset();
53 fScrollPos = ScrollRange.fMin;
54 fClientWidth = 0;
55 fBigStep = 10;
56 fSmallStep = 1;
57 }
58
SetScrollRange(float min,float max)59 void PWL_SCROLL_PRIVATEDATA::SetScrollRange(float min, float max) {
60 ScrollRange.Set(min, max);
61
62 if (FXSYS_IsFloatSmaller(fScrollPos, ScrollRange.fMin))
63 fScrollPos = ScrollRange.fMin;
64 if (FXSYS_IsFloatBigger(fScrollPos, ScrollRange.fMax))
65 fScrollPos = ScrollRange.fMax;
66 }
67
SetClientWidth(float width)68 void PWL_SCROLL_PRIVATEDATA::SetClientWidth(float width) {
69 fClientWidth = width;
70 }
71
SetSmallStep(float step)72 void PWL_SCROLL_PRIVATEDATA::SetSmallStep(float step) {
73 fSmallStep = step;
74 }
75
SetBigStep(float step)76 void PWL_SCROLL_PRIVATEDATA::SetBigStep(float step) {
77 fBigStep = step;
78 }
79
SetPos(float pos)80 bool PWL_SCROLL_PRIVATEDATA::SetPos(float pos) {
81 if (ScrollRange.In(pos)) {
82 fScrollPos = pos;
83 return true;
84 }
85 return false;
86 }
87
AddSmall()88 void PWL_SCROLL_PRIVATEDATA::AddSmall() {
89 if (!SetPos(fScrollPos + fSmallStep))
90 SetPos(ScrollRange.fMax);
91 }
92
SubSmall()93 void PWL_SCROLL_PRIVATEDATA::SubSmall() {
94 if (!SetPos(fScrollPos - fSmallStep))
95 SetPos(ScrollRange.fMin);
96 }
97
AddBig()98 void PWL_SCROLL_PRIVATEDATA::AddBig() {
99 if (!SetPos(fScrollPos + fBigStep))
100 SetPos(ScrollRange.fMax);
101 }
102
SubBig()103 void PWL_SCROLL_PRIVATEDATA::SubBig() {
104 if (!SetPos(fScrollPos - fBigStep))
105 SetPos(ScrollRange.fMin);
106 }
107
CPWL_ScrollBar(const CreateParams & cp,std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData)108 CPWL_ScrollBar::CPWL_ScrollBar(
109 const CreateParams& cp,
110 std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData)
111 : CPWL_Wnd(cp, std::move(pAttachedData)) {
112 GetCreationParams()->eCursorType = IPWL_FillerNotify::CursorStyle::kArrow;
113 }
114
115 CPWL_ScrollBar::~CPWL_ScrollBar() = default;
116
OnDestroy()117 void 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();
125 CPWL_Wnd::OnDestroy();
126 }
127
RepositionChildWnd()128 bool 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
DrawThisAppearance(CFX_RenderDevice * pDevice,const CFX_Matrix & mtUser2Device)169 void 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(),
175 GetTransparency());
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
OnLButtonDown(Mask<FWL_EVENTFLAG> nFlag,const CFX_PointF & point)189 bool CPWL_ScrollBar::OnLButtonDown(Mask<FWL_EVENTFLAG> nFlag,
190 const CFX_PointF& point) {
191 CPWL_Wnd::OnLButtonDown(nFlag, point);
192
193 if (HasFlag(PWS_AUTOTRANSPARENT)) {
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
OnLButtonUp(Mask<FWL_EVENTFLAG> nFlag,const CFX_PointF & point)232 bool CPWL_ScrollBar::OnLButtonUp(Mask<FWL_EVENTFLAG> nFlag,
233 const CFX_PointF& point) {
234 CPWL_Wnd::OnLButtonUp(nFlag, point);
235
236 if (HasFlag(PWS_AUTOTRANSPARENT)) {
237 if (GetTransparency() != kTransparency) {
238 SetTransparency(kTransparency);
239 if (!InvalidateRect(nullptr))
240 return true;
241 }
242 }
243
244 m_pTimer.reset();
245 m_bMouseDown = false;
246 return true;
247 }
248
SetScrollInfo(const PWL_SCROLL_INFO & info)249 void 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
SetScrollPosition(float pos)260 void CPWL_ScrollBar::SetScrollPosition(float pos) {
261 pos = m_OriginInfo.fContentMax - pos;
262 SetScrollPos(pos);
263 }
264
NotifyLButtonDown(CPWL_Wnd * child,const CFX_PointF & pos)265 void 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
NotifyLButtonUp(CPWL_Wnd * child,const CFX_PointF & pos)274 void 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
NotifyMouseMove(CPWL_Wnd * child,const CFX_PointF & pos)283 void 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
CreateButtons(const CreateParams & cp)292 void CPWL_ScrollBar::CreateButtons(const CreateParams& cp) {
293 CreateParams scp = cp;
294 scp.dwBorderWidth = 2;
295 scp.nBorderStyle = BorderStyle::kBeveled;
296 scp.dwFlags = PWS_VISIBLE | PWS_BORDER | PWS_BACKGROUND | PWS_NOREFRESHCLIP;
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
GetScrollBarWidth() const326 float CPWL_ScrollBar::GetScrollBarWidth() const {
327 return IsVisible() ? kWidth : 0.0f;
328 }
329
SetScrollRange(float fMin,float fMax,float fClientWidth)330 void 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
340 if (FXSYS_IsFloatSmaller(m_sData.ScrollRange.GetWidth(), 0.0f)) {
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
SetScrollPos(float fPos)356 void 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
SetScrollStep(float fBigStep,float fSmallStep)366 void CPWL_ScrollBar::SetScrollStep(float fBigStep, float fSmallStep) {
367 m_sData.SetBigStep(fBigStep);
368 m_sData.SetSmallStep(fSmallStep);
369 }
370
MovePosButton(bool bRefresh)371 bool 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
OnMinButtonLBDown(const CFX_PointF & point)401 void 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
OnMinButtonLBUp(const CFX_PointF & point)411 void CPWL_ScrollBar::OnMinButtonLBUp(const CFX_PointF& point) {}
412
OnMinButtonMouseMove(const CFX_PointF & point)413 void CPWL_ScrollBar::OnMinButtonMouseMove(const CFX_PointF& point) {}
414
OnMaxButtonLBDown(const CFX_PointF & point)415 void 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
OnMaxButtonLBUp(const CFX_PointF & point)425 void CPWL_ScrollBar::OnMaxButtonLBUp(const CFX_PointF& point) {}
426
OnMaxButtonMouseMove(const CFX_PointF & point)427 void CPWL_ScrollBar::OnMaxButtonMouseMove(const CFX_PointF& point) {}
428
OnPosButtonLBDown(const CFX_PointF & point)429 void 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
OnPosButtonLBUp(const CFX_PointF & point)439 void CPWL_ScrollBar::OnPosButtonLBUp(const CFX_PointF& point) {
440 m_bMouseDown = false;
441 }
442
OnPosButtonMouseMove(const CFX_PointF & point)443 void 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
NotifyScrollWindow()469 void 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
GetScrollArea() const478 CFX_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
TrueToFace(float fTrue)501 float 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
FaceToTrue(float fFace)509 float 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
CreateChildWnd(const CreateParams & cp)517 void CPWL_ScrollBar::CreateChildWnd(const CreateParams& cp) {
518 CreateButtons(cp);
519 }
520
OnTimerFired()521 void 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 }
536