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