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
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 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
DrawThisAppearance(CFX_RenderDevice * pDevice,const CFX_Matrix & mtUser2Device)168 void 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(),
174 GetTransparency());
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
OnLButtonDown(Mask<FWL_EVENTFLAG> nFlag,const CFX_PointF & point)188 bool CPWL_ScrollBar::OnLButtonDown(Mask<FWL_EVENTFLAG> nFlag,
189 const CFX_PointF& point) {
190 CPWL_Wnd::OnLButtonDown(nFlag, point);
191
192 if (HasFlag(PWS_AUTOTRANSPARENT)) {
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
OnLButtonUp(Mask<FWL_EVENTFLAG> nFlag,const CFX_PointF & point)231 bool CPWL_ScrollBar::OnLButtonUp(Mask<FWL_EVENTFLAG> nFlag,
232 const CFX_PointF& point) {
233 CPWL_Wnd::OnLButtonUp(nFlag, point);
234
235 if (HasFlag(PWS_AUTOTRANSPARENT)) {
236 if (GetTransparency() != kTransparency) {
237 SetTransparency(kTransparency);
238 if (!InvalidateRect(nullptr))
239 return true;
240 }
241 }
242
243 m_pTimer.reset();
244 m_bMouseDown = false;
245 return true;
246 }
247
SetScrollInfo(const PWL_SCROLL_INFO & info)248 void 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
SetScrollPosition(float pos)259 void CPWL_ScrollBar::SetScrollPosition(float pos) {
260 pos = m_OriginInfo.fContentMax - pos;
261 SetScrollPos(pos);
262 }
263
NotifyLButtonDown(CPWL_Wnd * child,const CFX_PointF & pos)264 void 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
NotifyLButtonUp(CPWL_Wnd * child,const CFX_PointF & pos)273 void 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
NotifyMouseMove(CPWL_Wnd * child,const CFX_PointF & pos)282 void 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
CreateButtons(const CreateParams & cp)291 void CPWL_ScrollBar::CreateButtons(const CreateParams& cp) {
292 ObservedPtr<CPWL_ScrollBar> this_observed(this);
293
294 CreateParams scp = cp;
295 scp.dwBorderWidth = 2;
296 scp.nBorderStyle = BorderStyle::kBeveled;
297 scp.dwFlags = PWS_VISIBLE | PWS_BORDER | PWS_BACKGROUND | PWS_NOREFRESHCLIP;
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
GetScrollBarWidth() const327 float CPWL_ScrollBar::GetScrollBarWidth() const {
328 return IsVisible() ? kWidth : 0.0f;
329 }
330
SetScrollRange(float fMin,float fMax,float fClientWidth)331 void 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
SetScrollPos(float fPos)358 void 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
SetScrollStep(float fBigStep,float fSmallStep)368 void CPWL_ScrollBar::SetScrollStep(float fBigStep, float fSmallStep) {
369 m_sData.SetBigStep(fBigStep);
370 m_sData.SetSmallStep(fSmallStep);
371 }
372
MovePosButton(bool bRefresh)373 bool 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
OnMinButtonLBDown(const CFX_PointF & point)403 void 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
OnMinButtonLBUp(const CFX_PointF & point)413 void CPWL_ScrollBar::OnMinButtonLBUp(const CFX_PointF& point) {}
414
OnMinButtonMouseMove(const CFX_PointF & point)415 void CPWL_ScrollBar::OnMinButtonMouseMove(const CFX_PointF& point) {}
416
OnMaxButtonLBDown(const CFX_PointF & point)417 void 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
OnMaxButtonLBUp(const CFX_PointF & point)427 void CPWL_ScrollBar::OnMaxButtonLBUp(const CFX_PointF& point) {}
428
OnMaxButtonMouseMove(const CFX_PointF & point)429 void CPWL_ScrollBar::OnMaxButtonMouseMove(const CFX_PointF& point) {}
430
OnPosButtonLBDown(const CFX_PointF & point)431 void 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
OnPosButtonLBUp(const CFX_PointF & point)441 void CPWL_ScrollBar::OnPosButtonLBUp(const CFX_PointF& point) {
442 m_bMouseDown = false;
443 }
444
OnPosButtonMouseMove(const CFX_PointF & point)445 void 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
NotifyScrollWindow()471 void 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
GetScrollArea() const480 CFX_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
TrueToFace(float fTrue)503 float 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
FaceToTrue(float fFace)511 float 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
CreateChildWnd(const CreateParams & cp)519 void CPWL_ScrollBar::CreateChildWnd(const CreateParams& cp) {
520 CreateButtons(cp);
521 }
522
OnTimerFired()523 void 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 }
538